xref: /titanic_53/usr/src/cmd/devfsadm/disk_link.c (revision efc44fd77cb81028389b8598b063646228f9e1d0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*efc44fd7SVitaliy Gusev  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
23d88e498aSjiang wu - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <devfsadm.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
3297869ac5Sjhd #include <ctype.h>
334c06356bSdh142964 #include <sys/int_fmtio.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
3545916cd2Sjpk #include <bsm/devalloc.h>
364c06356bSdh142964 #include <sys/scsi/scsi_address.h>
374c06356bSdh142964 #include <sys/libdevid.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #define	DISK_SUBPATH_MAX 100
407c478bd9Sstevel@tonic-gate #define	RM_STALE 0x01
417c478bd9Sstevel@tonic-gate #define	DISK_LINK_RE	"^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
427c478bd9Sstevel@tonic-gate #define	DISK_LINK_TO_UPPER(ch)\
437c478bd9Sstevel@tonic-gate 	(((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	SLICE_SMI	"s7"
467c478bd9Sstevel@tonic-gate #define	SLICE_EFI	""
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	MN_SMI		"h"
497c478bd9Sstevel@tonic-gate #define	MN_EFI		"wd"
507c478bd9Sstevel@tonic-gate #define	ASCIIWWNSIZE	255
51aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
52aa1b14e7SSheshadri Vasudevan /*
53aa1b14e7SSheshadri Vasudevan  * The number of minor nodes per LUN is defined by the disk drivers.
54aa1b14e7SSheshadri Vasudevan  * Currently it is set to 64. Refer CMLBUNIT_SHIFT (cmlb_impl.h)
55aa1b14e7SSheshadri Vasudevan  */
56aa1b14e7SSheshadri Vasudevan #define	NUM_MINORS_PER_INSTANCE	64
57aa1b14e7SSheshadri Vasudevan #endif
58aa1b14e7SSheshadri Vasudevan 
597c478bd9Sstevel@tonic-gate 
6045916cd2Sjpk extern int system_labeled;
6145916cd2Sjpk 
627c478bd9Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node);
637c478bd9Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node);
647c478bd9Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node);
6597869ac5Sjhd static int disk_callback_xvmd(di_minor_t minor, di_node_t node);
667c478bd9Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node);
67d88e498aSjiang wu - Sun Microsystems - Beijing China static int disk_callback_sas(di_minor_t minor, di_node_t node);
687c478bd9Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk,
697c478bd9Sstevel@tonic-gate 				int flags);
707c478bd9Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor);
718d483882Smlf static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = {
754c06356bSdh142964 	{ "disk", DDI_NT_BLOCK, NULL,
767c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
777c478bd9Sstevel@tonic-gate 	},
784c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_CHAN, NULL,
797c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
807c478bd9Sstevel@tonic-gate 	},
814c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_FABRIC, NULL,
827c478bd9Sstevel@tonic-gate 		TYPE_EXACT, ILEVEL_0, disk_callback_fabric
837c478bd9Sstevel@tonic-gate 	},
844c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_WWN, NULL,
857c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_wwn
867c478bd9Sstevel@tonic-gate 	},
874c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_SAS, NULL,
88d88e498aSjiang wu - Sun Microsystems - Beijing China 	    TYPE_EXACT, ILEVEL_0, disk_callback_sas
89d88e498aSjiang wu - Sun Microsystems - Beijing China 	},
904c06356bSdh142964 	{ "disk", DDI_NT_CD, NULL,
917c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
927c478bd9Sstevel@tonic-gate 	},
934c06356bSdh142964 	{ "disk", DDI_NT_CD_CHAN, NULL,
947c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
957c478bd9Sstevel@tonic-gate 	},
964c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_XVMD, NULL,
9797869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
9897869ac5Sjhd 	},
994c06356bSdh142964 	{ "disk", DDI_NT_CD_XVMD, NULL,
10097869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
10197869ac5Sjhd 	},
1027c478bd9Sstevel@tonic-gate };
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * HOT auto cleanup of disks not desired.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = {
1107c478bd9Sstevel@tonic-gate 	{ "disk", DISK_LINK_RE, RM_POST,
1117c478bd9Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
1167c478bd9Sstevel@tonic-gate 
1178d483882Smlf static devlink_re_t disks_re_array[] = {
1188d483882Smlf 	{"^r?dsk/c([0-9]+)", 1},
1198d483882Smlf 	{"^cfg/c([0-9]+)$", 1},
1208d483882Smlf 	{"^scsi/.+/c([0-9]+)", 1},
1218d483882Smlf 	{NULL}
1228d483882Smlf };
1238d483882Smlf 
1248d483882Smlf static char *disk_mid = "disk_mid";
1258d483882Smlf static char *modname = "disk_link";
1268d483882Smlf 
1278d483882Smlf int
1288d483882Smlf minor_init()
1298d483882Smlf {
1308d483882Smlf 	devfsadm_print(disk_mid,
1318d483882Smlf 	    "%s: minor_init(): Creating disks reserved ID cache\n",
1328d483882Smlf 	    modname);
1338d483882Smlf 	return (devfsadm_reserve_id_cache(disks_re_array, NULL));
1348d483882Smlf }
1358d483882Smlf 
1367c478bd9Sstevel@tonic-gate static int
1377c478bd9Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	char *addr;
1407c478bd9Sstevel@tonic-gate 	char disk[20];
1417c478bd9Sstevel@tonic-gate 	uint_t targ;
1427c478bd9Sstevel@tonic-gate 	uint_t lun;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	addr = di_bus_addr(node);
1457c478bd9Sstevel@tonic-gate 	(void) sscanf(addr, "%X,%X", &targ, &lun);
1467c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1477c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1487c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static int
1537c478bd9Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	char *addr;
1567c478bd9Sstevel@tonic-gate 	char disk[10];
1577c478bd9Sstevel@tonic-gate 	uint_t lun;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	addr = di_bus_addr(node);
1607c478bd9Sstevel@tonic-gate 	(void) sscanf(addr, "%X", &lun);
1617c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "d%d", lun);
1627c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1637c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static int
1687c478bd9Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	char disk[10];
1717c478bd9Sstevel@tonic-gate 	int lun;
1727c478bd9Sstevel@tonic-gate 	int targ;
1737c478bd9Sstevel@tonic-gate 	int *intp;
1747c478bd9Sstevel@tonic-gate 
1754c06356bSdh142964 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_TARGET,
1764c06356bSdh142964 	    &intp) <= 0) {
1777c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	targ = *intp;
1804c06356bSdh142964 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_LUN,
1814c06356bSdh142964 	    &intp) <= 0) {
1827c478bd9Sstevel@tonic-gate 		lun = 0;
1837c478bd9Sstevel@tonic-gate 	} else {
1847c478bd9Sstevel@tonic-gate 		lun = *intp;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static int
1947c478bd9Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	char disk[DISK_SUBPATH_MAX];
1977c478bd9Sstevel@tonic-gate 	int lun;
1987c478bd9Sstevel@tonic-gate 	int count;
1997c478bd9Sstevel@tonic-gate 	int *intp;
2007c478bd9Sstevel@tonic-gate 	uchar_t *str;
2017c478bd9Sstevel@tonic-gate 	uchar_t *wwn;
2027c478bd9Sstevel@tonic-gate 	uchar_t ascii_wwn[ASCIIWWNSIZE];
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2057c478bd9Sstevel@tonic-gate 	    "client-guid", (char **)&wwn) > 0) {
206e37c6c37Scth 		if (strlcpy((char *)ascii_wwn, (char *)wwn,
207e37c6c37Scth 		    sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) {
2087c478bd9Sstevel@tonic-gate 			devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
2097c478bd9Sstevel@tonic-gate 			    strlen((char *)wwn));
2107c478bd9Sstevel@tonic-gate 			return (DEVFSADM_CONTINUE);
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 		lun = 0;
2137c478bd9Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
2147c478bd9Sstevel@tonic-gate 	    "port-wwn", &wwn) > 0) {
2157c478bd9Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2164c06356bSdh142964 		    SCSI_ADDR_PROP_LUN, &intp) > 0) {
2177c478bd9Sstevel@tonic-gate 			lun = *intp;
2187c478bd9Sstevel@tonic-gate 		} else {
2197c478bd9Sstevel@tonic-gate 			lun = 0;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
2237c478bd9Sstevel@tonic-gate 			(void) sprintf((caddr_t)str, "%02x", wwn[count]);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 		*str = '\0';
2267c478bd9Sstevel@tonic-gate 	} else {
2277c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	for (str = ascii_wwn; *str != '\0'; str++) {
2317c478bd9Sstevel@tonic-gate 		*str = DISK_LINK_TO_UPPER(*str);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
241d88e498aSjiang wu - Sun Microsystems - Beijing China static int
242d88e498aSjiang wu - Sun Microsystems - Beijing China disk_callback_sas(di_minor_t minor, di_node_t node)
243d88e498aSjiang wu - Sun Microsystems - Beijing China {
244d88e498aSjiang wu - Sun Microsystems - Beijing China 	char disk[DISK_SUBPATH_MAX];
2454c06356bSdh142964 	int lun64_found = 0;
2464c06356bSdh142964 	scsi_lun64_t lun64, sl;
2474c06356bSdh142964 	scsi_lun_t lun;
2484c06356bSdh142964 	int64_t *lun64p;
2494c06356bSdh142964 	uint64_t wwn;
250d88e498aSjiang wu - Sun Microsystems - Beijing China 	int *intp;
2514c06356bSdh142964 	char *tgt_port;
2524c06356bSdh142964 	uchar_t addr_method;
253d88e498aSjiang wu - Sun Microsystems - Beijing China 
2544c06356bSdh142964 	/* Get lun property */
2554c06356bSdh142964 	if (di_prop_lookup_int64(DDI_DEV_T_ANY, node,
2564c06356bSdh142964 	    SCSI_ADDR_PROP_LUN64, &lun64p) > 0) {
2574c06356bSdh142964 		if (*lun64p != SCSI_LUN64_ILLEGAL) {
2584c06356bSdh142964 			lun64_found = 1;
2594c06356bSdh142964 			lun64 = (uint64_t)*lun64p;
260d88e498aSjiang wu - Sun Microsystems - Beijing China 		}
2614c06356bSdh142964 	}
2624c06356bSdh142964 	if ((!lun64_found) && (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2634c06356bSdh142964 	    SCSI_ADDR_PROP_LUN, &intp) > 0)) {
2644c06356bSdh142964 		lun64 = (uint64_t)*intp;
2654c06356bSdh142964 	}
2664c06356bSdh142964 
2674c06356bSdh142964 	lun = scsi_lun64_to_lun(lun64);
2684c06356bSdh142964 
2694c06356bSdh142964 	addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
2704c06356bSdh142964 
271d88e498aSjiang wu - Sun Microsystems - Beijing China 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2724c06356bSdh142964 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
2734c06356bSdh142964 		(void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
2744c06356bSdh142964 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
2754c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
2764c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
2774c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
2784c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
2794c06356bSdh142964 			    "t%"PRIX64"d%"PRId64, wwn, lun64);
2804c06356bSdh142964 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
2814c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
2824c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
2834c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
2844c06356bSdh142964 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
2854c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
2864c06356bSdh142964 			    "t%"PRIX64"d%"PRIX16, wwn, sl);
2874c06356bSdh142964 		} else {
2884c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
2894c06356bSdh142964 			    "t%"PRIX64"d%"PRIX64, wwn, lun64);
290d88e498aSjiang wu - Sun Microsystems - Beijing China 		}
291d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2924c06356bSdh142964 	    SCSI_ADDR_PROP_SATA_PHY, &intp) > 0) {
2934c06356bSdh142964 		/* Use phy format naming, for SATA devices without wwn */
2944c06356bSdh142964 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
2954c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
2964c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
2974c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
2984c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
29982bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRId64, *intp, lun64);
3004c06356bSdh142964 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
3014c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3024c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3034c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3044c06356bSdh142964 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
3054c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
30682bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRIX16, *intp, sl);
3074c06356bSdh142964 		} else {
3084c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
30982bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRIX64, *intp, lun64);
3104c06356bSdh142964 		}
311d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else {
312d88e498aSjiang wu - Sun Microsystems - Beijing China 		return (DEVFSADM_CONTINUE);
313d88e498aSjiang wu - Sun Microsystems - Beijing China 	}
314d88e498aSjiang wu - Sun Microsystems - Beijing China 
315d88e498aSjiang wu - Sun Microsystems - Beijing China 	disk_common(minor, node, disk, RM_STALE);
316d88e498aSjiang wu - Sun Microsystems - Beijing China 
317d88e498aSjiang wu - Sun Microsystems - Beijing China 	return (DEVFSADM_CONTINUE);
318d88e498aSjiang wu - Sun Microsystems - Beijing China }
319d88e498aSjiang wu - Sun Microsystems - Beijing China 
3207c478bd9Sstevel@tonic-gate /*
32197869ac5Sjhd  * xVM virtual block device
32297869ac5Sjhd  *
323*efc44fd7SVitaliy Gusev  * Xen passes device number in next format:
32497869ac5Sjhd  *
325*efc44fd7SVitaliy Gusev  *    1 << 28 | disk << 8 | partition      xvd, disks or partitions 16 onwards
326*efc44fd7SVitaliy Gusev  *  202 <<  8 | disk << 4 | partition      xvd, disks and partitions up to 15
327*efc44fd7SVitaliy Gusev  *    8 <<  8 | disk << 4 | partition      sd, disks and partitions up to 15
328*efc44fd7SVitaliy Gusev  *    3 <<  8 | disk << 6 | partition      hd, disks 0..1, partitions 0..63
329*efc44fd7SVitaliy Gusev  *   22 <<  8 | (disk-2) << 6 | partition  hd, disks 2..3, partitions 0..63
330*efc44fd7SVitaliy Gusev  *    2 << 28 onwards                      reserved for future use
331*efc44fd7SVitaliy Gusev  *   other values less than 1 << 28        deprecated / reserved
332*efc44fd7SVitaliy Gusev  *
333*efc44fd7SVitaliy Gusev  * The corresponding Solaris /dev/dsk name can be:
334*efc44fd7SVitaliy Gusev  *
335*efc44fd7SVitaliy Gusev  *          c0tYdXsN
336*efc44fd7SVitaliy Gusev  *
337*efc44fd7SVitaliy Gusev  * where Y,X >= 0.
33897869ac5Sjhd  *
33997869ac5Sjhd  * For PV guests using the legacy naming (0, 1, 2, ...)
34097869ac5Sjhd  * the Solaris disk names created will be c0d[0..767]sN
34197869ac5Sjhd  */
342*efc44fd7SVitaliy Gusev 
343*efc44fd7SVitaliy Gusev #define	HD_BASE		(3 << 8)
344*efc44fd7SVitaliy Gusev #define	XEN_EXT_SHIFT	(28)
345*efc44fd7SVitaliy Gusev 
346*efc44fd7SVitaliy Gusev /*
347*efc44fd7SVitaliy Gusev  * Return: Number of parsed and written parameters
348*efc44fd7SVitaliy Gusev  */
349*efc44fd7SVitaliy Gusev static int
350*efc44fd7SVitaliy Gusev decode_xen_device(uint_t device, uint_t *disk, uint_t *plun)
351*efc44fd7SVitaliy Gusev {
352*efc44fd7SVitaliy Gusev 	uint_t dsk, lun = 0;
353*efc44fd7SVitaliy Gusev 	int ret = 1;
354*efc44fd7SVitaliy Gusev 
355*efc44fd7SVitaliy Gusev 	if ((device >> XEN_EXT_SHIFT) > 1)
356*efc44fd7SVitaliy Gusev 		return (0);
357*efc44fd7SVitaliy Gusev 
358*efc44fd7SVitaliy Gusev 	if (device < HD_BASE) {
359*efc44fd7SVitaliy Gusev 		/* legacy device address */
360*efc44fd7SVitaliy Gusev 		dsk = device;
361*efc44fd7SVitaliy Gusev 		goto end;
362*efc44fd7SVitaliy Gusev 	}
363*efc44fd7SVitaliy Gusev 
364*efc44fd7SVitaliy Gusev 	ret = 2;
365*efc44fd7SVitaliy Gusev 	if (device & (1 << XEN_EXT_SHIFT)) {
366*efc44fd7SVitaliy Gusev 		/* extended */
367*efc44fd7SVitaliy Gusev 		dsk = device & (~0xff);
368*efc44fd7SVitaliy Gusev 		lun = device & 0xff;
369*efc44fd7SVitaliy Gusev 		goto end;
370*efc44fd7SVitaliy Gusev 	}
371*efc44fd7SVitaliy Gusev 
372*efc44fd7SVitaliy Gusev 	switch (device >> 8) {
373*efc44fd7SVitaliy Gusev 	case 202:				/* xvd */
374*efc44fd7SVitaliy Gusev 		dsk = (device >> 4) & 0xf;
375*efc44fd7SVitaliy Gusev 		lun =  device & 0xf;
376*efc44fd7SVitaliy Gusev 		break;
377*efc44fd7SVitaliy Gusev 	case 8:					/* sd */
378*efc44fd7SVitaliy Gusev 		dsk = device & (~0xf);
379*efc44fd7SVitaliy Gusev 		lun = device & 0xf;
380*efc44fd7SVitaliy Gusev 		break;
381*efc44fd7SVitaliy Gusev 	case 3:					/* hd, disk 0..1 */
382*efc44fd7SVitaliy Gusev 		dsk = device & (~0x3f);
383*efc44fd7SVitaliy Gusev 		lun = device & 0x3f;
384*efc44fd7SVitaliy Gusev 		break;
385*efc44fd7SVitaliy Gusev 	case 22:				/* hd, disk 2..3 */
386*efc44fd7SVitaliy Gusev 		dsk = device & (~0x3f);
387*efc44fd7SVitaliy Gusev 		lun = device & 0x3f;
388*efc44fd7SVitaliy Gusev 		break;
389*efc44fd7SVitaliy Gusev 	default:
390*efc44fd7SVitaliy Gusev 		return (0);
391*efc44fd7SVitaliy Gusev 	}
392*efc44fd7SVitaliy Gusev end:
393*efc44fd7SVitaliy Gusev 	*disk = dsk;
394*efc44fd7SVitaliy Gusev 	*plun = lun;
395*efc44fd7SVitaliy Gusev 	return (ret);
396*efc44fd7SVitaliy Gusev }
397*efc44fd7SVitaliy Gusev 
39897869ac5Sjhd static int
39997869ac5Sjhd disk_callback_xvmd(di_minor_t minor, di_node_t node)
40097869ac5Sjhd {
40197869ac5Sjhd 	char *addr;
40297869ac5Sjhd 	char disk[16];
40397869ac5Sjhd 	uint_t targ;
404*efc44fd7SVitaliy Gusev 	uint_t dsk, lun;
405*efc44fd7SVitaliy Gusev 	int res;
40697869ac5Sjhd 
40797869ac5Sjhd 	addr = di_bus_addr(node);
40897869ac5Sjhd 	targ = strtol(addr, (char **)NULL, 10);
40997869ac5Sjhd 
410*efc44fd7SVitaliy Gusev 	res = decode_xen_device(targ, &dsk, &lun);
411*efc44fd7SVitaliy Gusev 
41297869ac5Sjhd 	/* HVM device names are generated using the standard generator */
413*efc44fd7SVitaliy Gusev 
414*efc44fd7SVitaliy Gusev 	if (res == 1)
415*efc44fd7SVitaliy Gusev 		(void) snprintf(disk, sizeof (disk),  "d%d", dsk);
416*efc44fd7SVitaliy Gusev 	else if (res == 2)
417*efc44fd7SVitaliy Gusev 		(void) snprintf(disk, sizeof (disk), "t%dd%d", dsk, lun);
418*efc44fd7SVitaliy Gusev 	else {
41997869ac5Sjhd 		devfsadm_errprint("%s: invalid disk device number (%s)\n",
42097869ac5Sjhd 		    modname, addr);
42197869ac5Sjhd 		return (DEVFSADM_CONTINUE);
42297869ac5Sjhd 	}
42397869ac5Sjhd 	disk_common(minor, node, disk, 0);
42497869ac5Sjhd 	return (DEVFSADM_CONTINUE);
42597869ac5Sjhd 
42697869ac5Sjhd }
42797869ac5Sjhd 
42897869ac5Sjhd /*
4297c478bd9Sstevel@tonic-gate  * This function is called for every disk minor node.
4307c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical controller number, and
4317c478bd9Sstevel@tonic-gate  * then devfsadm_mklink to make the link.
4327c478bd9Sstevel@tonic-gate  */
4337c478bd9Sstevel@tonic-gate static void
4347c478bd9Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
43718c2aff7Sartem 	char sec_path[PATH_MAX + 1];
4387c478bd9Sstevel@tonic-gate 	char stale_re[DISK_SUBPATH_MAX];
4397c478bd9Sstevel@tonic-gate 	char *dir;
4407c478bd9Sstevel@tonic-gate 	char slice[4];
4417c478bd9Sstevel@tonic-gate 	char *mn;
4427c478bd9Sstevel@tonic-gate 	char *ctrl;
44345916cd2Sjpk 	char *nt = NULL;
44418c2aff7Sartem 	int *int_prop;
44545916cd2Sjpk 	int  nflags = 0;
446aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
447aa1b14e7SSheshadri Vasudevan 	char mn_copy[4];
448aa1b14e7SSheshadri Vasudevan 	char *part;
449aa1b14e7SSheshadri Vasudevan 	int part_num;
450aa1b14e7SSheshadri Vasudevan #endif
4517c478bd9Sstevel@tonic-gate 
452aa1b14e7SSheshadri Vasudevan 	mn = di_minor_name(minor);
453aa1b14e7SSheshadri Vasudevan 	if (strstr(mn, ",raw")) {
4547c478bd9Sstevel@tonic-gate 		dir = "rdsk";
455aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
456aa1b14e7SSheshadri Vasudevan 		(void) strncpy(mn_copy, mn, 4);
457aa1b14e7SSheshadri Vasudevan 		part = strtok(mn_copy, ",");
458aa1b14e7SSheshadri Vasudevan #endif
4597c478bd9Sstevel@tonic-gate 	} else {
4607c478bd9Sstevel@tonic-gate 		dir = "dsk";
461aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
462aa1b14e7SSheshadri Vasudevan 		part = mn;
463aa1b14e7SSheshadri Vasudevan #endif
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
466aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
467aa1b14e7SSheshadri Vasudevan 	/*
468aa1b14e7SSheshadri Vasudevan 	 * The following is a table describing the allocation of
469aa1b14e7SSheshadri Vasudevan 	 * minor numbers, minor names and /dev/dsk names for partitions
470aa1b14e7SSheshadri Vasudevan 	 * and slices on x86 systems.
471aa1b14e7SSheshadri Vasudevan 	 *
472aa1b14e7SSheshadri Vasudevan 	 *	Minor Number	Minor Name	/dev/dsk name
473aa1b14e7SSheshadri Vasudevan 	 *	---------------------------------------------
474aa1b14e7SSheshadri Vasudevan 	 *	0 to 15		"a" to "p"	s0 to s15
475aa1b14e7SSheshadri Vasudevan 	 *	16		"q"		p0
476aa1b14e7SSheshadri Vasudevan 	 *	17 to 20	"r" to "u"	p1 to p4
477aa1b14e7SSheshadri Vasudevan 	 *	21 to 52	"p5" to "p36"	p5 to p36
478aa1b14e7SSheshadri Vasudevan 	 *
479aa1b14e7SSheshadri Vasudevan 	 */
480aa1b14e7SSheshadri Vasudevan 	part_num = atoi(part + 1);
481aa1b14e7SSheshadri Vasudevan 
482aa1b14e7SSheshadri Vasudevan 	if ((mn[0] == 'p') && (part_num >= 5)) {
483aa1b14e7SSheshadri Vasudevan 		/* logical drive */
484aa1b14e7SSheshadri Vasudevan 		(void) snprintf(slice, 4, "%s", part);
485aa1b14e7SSheshadri Vasudevan 	} else {
486aa1b14e7SSheshadri Vasudevan #endif
487aa1b14e7SSheshadri Vasudevan 	if (mn[0] < 'q') {
4887c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "s%d", mn[0] - 'a');
4897c478bd9Sstevel@tonic-gate 	} else if (strncmp(mn, MN_EFI, 2) != 0) {
4907c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "p%d", mn[0] - 'q');
4917c478bd9Sstevel@tonic-gate 	} else {
4927c478bd9Sstevel@tonic-gate 		/* For EFI label */
4937c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, SLICE_EFI);
4947c478bd9Sstevel@tonic-gate 	}
495aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
496aa1b14e7SSheshadri Vasudevan 	}
497aa1b14e7SSheshadri Vasudevan #endif
4987c478bd9Sstevel@tonic-gate 
4998d483882Smlf 	nflags = 0;
5008d483882Smlf 	if (system_labeled) {
5018d483882Smlf 		nt = di_minor_nodetype(minor);
5028d483882Smlf 		if ((nt != NULL) &&
5038d483882Smlf 		    ((strcmp(nt, DDI_NT_CD) == 0) ||
5048d483882Smlf 		    (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
5058d483882Smlf 		    (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
5068d483882Smlf 			nflags = DA_ADD|DA_CD;
5078d483882Smlf 		}
5088d483882Smlf 	}
5098d483882Smlf 
5108d483882Smlf 	if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
5118d483882Smlf 		devfsadm_print(disk_mid, "Reserved link exists. Not "
5128d483882Smlf 		    "creating links for slice %s\n", slice);
5138d483882Smlf 		return;
5148d483882Smlf 	}
5158d483882Smlf 
5167c478bd9Sstevel@tonic-gate 	if (NULL == (ctrl = diskctrl(node, minor)))
5177c478bd9Sstevel@tonic-gate 		return;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, dir);
5207c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, "/c");
5217c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, ctrl);
5227c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, disk);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * If switching between SMI and EFI label or vice versa
5267c478bd9Sstevel@tonic-gate 	 * cleanup the previous label's devlinks.
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
5297c478bd9Sstevel@tonic-gate 		char *s, tpath[PATH_MAX + 1];
5307c478bd9Sstevel@tonic-gate 		struct stat sb;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		s = l_path + strlen(l_path);
5337c478bd9Sstevel@tonic-gate 		(void) strcat(l_path, (*mn == *(MN_SMI))
5347c478bd9Sstevel@tonic-gate 		    ? SLICE_EFI : SLICE_SMI);
5357c478bd9Sstevel@tonic-gate 		/*
5367c478bd9Sstevel@tonic-gate 		 * Attempt the remove only if the stale link exists
5377c478bd9Sstevel@tonic-gate 		 */
5387c478bd9Sstevel@tonic-gate 		(void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
5397c478bd9Sstevel@tonic-gate 		    devfsadm_root_path(), l_path);
5407c478bd9Sstevel@tonic-gate 		if (lstat(tpath, &sb) != -1)
5417c478bd9Sstevel@tonic-gate 			devfsadm_rm_all(l_path);
5427c478bd9Sstevel@tonic-gate 		*s = '\0';
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, slice);
5457c478bd9Sstevel@tonic-gate 
54645916cd2Sjpk 	(void) devfsadm_mklink(l_path, node, minor, nflags);
5477c478bd9Sstevel@tonic-gate 
54818c2aff7Sartem 	/* secondary links for removable and hotpluggable devices */
54918c2aff7Sartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
55018c2aff7Sartem 	    &int_prop) >= 0) {
55118c2aff7Sartem 		(void) strcpy(sec_path, "removable-media/");
55218c2aff7Sartem 		(void) strcat(sec_path, l_path);
55318c2aff7Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
5547544909dSartem 	}
5557544909dSartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
55618c2aff7Sartem 	    &int_prop) >= 0) {
55718c2aff7Sartem 		(void) strcpy(sec_path, "hotpluggable/");
55818c2aff7Sartem 		(void) strcat(sec_path, l_path);
55918c2aff7Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
56018c2aff7Sartem 	}
56118c2aff7Sartem 
5627c478bd9Sstevel@tonic-gate 	if ((flags & RM_STALE) == RM_STALE) {
5637c478bd9Sstevel@tonic-gate 		(void) strcpy(stale_re, "^");
5647c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, dir);
5657c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, "/c");
5667c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, ctrl);
5677c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
5687c478bd9Sstevel@tonic-gate 		/*
5697c478bd9Sstevel@tonic-gate 		 * optimizations are made inside of devfsadm_rm_stale_links
5707c478bd9Sstevel@tonic-gate 		 * instead of before calling the function, as it always
5717c478bd9Sstevel@tonic-gate 		 * needs to add the valid link to the cache.
5727c478bd9Sstevel@tonic-gate 		 */
5737c478bd9Sstevel@tonic-gate 		devfsadm_rm_stale_links(stale_re, l_path, node, minor);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	free(ctrl);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
5817c478bd9Sstevel@tonic-gate #define	RULE_INDEX	0
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate static char *
5847c478bd9Sstevel@tonic-gate diskctrl(di_node_t node, di_minor_t minor)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
5877c478bd9Sstevel@tonic-gate 	char *devfspath;
5887c478bd9Sstevel@tonic-gate 	char *buf, *mn;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
5917c478bd9Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
5927c478bd9Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
5937c478bd9Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
5947c478bd9Sstevel@tonic-gate 	};
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
5997c478bd9Sstevel@tonic-gate 		return (NULL);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
6027c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
6037c478bd9Sstevel@tonic-gate 	(void) strcat(path, mn);
6047c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/*
6077c478bd9Sstevel@tonic-gate 	 * Use controller component of disk path
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
6107c478bd9Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		/*
6137c478bd9Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
6147c478bd9Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
6157c478bd9Sstevel@tonic-gate 		 * name also in the match it should fix this and only find one
6167c478bd9Sstevel@tonic-gate 		 * logical controller. (See 4045879).
6177c478bd9Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
6187c478bd9Sstevel@tonic-gate 		 * no unique controller number for them in this case.
6197c478bd9Sstevel@tonic-gate 		 *
6207c478bd9Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
6217c478bd9Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
6227c478bd9Sstevel@tonic-gate 		 */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
6257c478bd9Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
6267c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
6277c478bd9Sstevel@tonic-gate 			return (NULL);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	return (buf);
6327c478bd9Sstevel@tonic-gate }
6338d483882Smlf 
6348d483882Smlf typedef struct dvlist {
6358d483882Smlf 	char *dv_link;
6368d483882Smlf 	struct dvlist *dv_next;
6378d483882Smlf } dvlist_t;
6388d483882Smlf 
6398d483882Smlf static void
6408d483882Smlf free_dvlist(dvlist_t **pp)
6418d483882Smlf {
6428d483882Smlf 	dvlist_t *entry;
6438d483882Smlf 
6448d483882Smlf 	while (*pp) {
6458d483882Smlf 		entry = *pp;
6468d483882Smlf 		*pp = entry->dv_next;
6478d483882Smlf 		assert(entry->dv_link);
6488d483882Smlf 		free(entry->dv_link);
6498d483882Smlf 		free(entry);
6508d483882Smlf 	}
6518d483882Smlf }
6528d483882Smlf static int
6538d483882Smlf dvlink_cb(di_devlink_t devlink, void *arg)
6548d483882Smlf {
6558d483882Smlf 	char *path;
6568d483882Smlf 	char *can_path;
6578d483882Smlf 	dvlist_t **pp = (dvlist_t **)arg;
6588d483882Smlf 	dvlist_t *entry = NULL;
6598d483882Smlf 
6608d483882Smlf 	entry = calloc(1, sizeof (dvlist_t));
6618d483882Smlf 	if (entry == NULL) {
6628d483882Smlf 		devfsadm_errprint("%s: calloc failed\n", modname);
6638d483882Smlf 		goto error;
6648d483882Smlf 	}
6658d483882Smlf 
6668d483882Smlf 	path = (char *)di_devlink_path(devlink);
6678d483882Smlf 	assert(path);
6688d483882Smlf 	if (path == NULL) {
6698d483882Smlf 		devfsadm_errprint("%s: di_devlink_path() returned NULL\n",
6708d483882Smlf 		    modname);
6718d483882Smlf 		goto error;
6728d483882Smlf 	}
6738d483882Smlf 
6748d483882Smlf 	devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n",
6758d483882Smlf 	    modname, path);
6768d483882Smlf 
6778d483882Smlf 	/*
6788d483882Smlf 	 * Return linkname in canonical form i.e. without the
6798d483882Smlf 	 * "/dev/" prefix
6808d483882Smlf 	 */
6818d483882Smlf 	can_path = strstr(path, "/dev/");
6828d483882Smlf 	if (can_path == NULL) {
6838d483882Smlf 		devfsadm_errprint("%s: devlink path %s has no /dev/\n",
6848d483882Smlf 		    modname, path);
6858d483882Smlf 		goto error;
6868d483882Smlf 	}
6878d483882Smlf 
6888d483882Smlf 	entry->dv_link = s_strdup(can_path + strlen("/dev/"));
6898d483882Smlf 	entry->dv_next = *pp;
6908d483882Smlf 	*pp = entry;
6918d483882Smlf 
6928d483882Smlf 	return (DI_WALK_CONTINUE);
6938d483882Smlf 
6948d483882Smlf error:
6958d483882Smlf 	free(entry);
6968d483882Smlf 	free_dvlist(pp);
6978d483882Smlf 	*pp = NULL;
6988d483882Smlf 	return (DI_WALK_TERMINATE);
6998d483882Smlf }
7008d483882Smlf 
7018d483882Smlf /*
7028d483882Smlf  * Returns success only if all goes well. If there is no matching reserved link
7038d483882Smlf  * or if there is an error, we assume no match. It is better to err on the side
7048d483882Smlf  * of caution by creating extra links than to miss out creating a required link.
7058d483882Smlf  */
7068d483882Smlf static int
7078d483882Smlf reserved_links_exist(di_node_t node, di_minor_t minor, int nflags)
7088d483882Smlf {
7098d483882Smlf 	di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache();
7108d483882Smlf 	char phys_path[PATH_MAX];
7118d483882Smlf 	char *minor_path;
7128d483882Smlf 	dvlist_t *head;
7138d483882Smlf 	dvlist_t *entry;
7148d483882Smlf 	char *s;
7158d483882Smlf 	char l[PATH_MAX];
7168d483882Smlf 	int switch_link = 0;
7178d483882Smlf 	char *mn = di_minor_name(minor);
7188d483882Smlf 
7198d483882Smlf 	if (dvlink_cache == NULL || mn == NULL) {
7208d483882Smlf 		devfsadm_errprint("%s: No minor or devlink cache\n", modname);
7218d483882Smlf 		return (DEVFSADM_FAILURE);
7228d483882Smlf 	}
7238d483882Smlf 
724e37c6c37Scth 	if (!devfsadm_have_reserved()) {
725e37c6c37Scth 		devfsadm_print(disk_mid, "%s: No reserved links\n", modname);
7268d483882Smlf 		return (DEVFSADM_FAILURE);
7278d483882Smlf 	}
7288d483882Smlf 
7298d483882Smlf 	minor_path = di_devfs_minor_path(minor);
7308d483882Smlf 	if (minor_path == NULL) {
7318d483882Smlf 		devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname);
7328d483882Smlf 		return (DEVFSADM_FAILURE);
7338d483882Smlf 	}
7348d483882Smlf 
7358d483882Smlf 	(void) strlcpy(phys_path, minor_path, sizeof (phys_path));
7368d483882Smlf 
7378d483882Smlf 	di_devfs_path_free(minor_path);
7388d483882Smlf 
7398d483882Smlf 	head = NULL;
7408d483882Smlf 	(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path,
7418d483882Smlf 	    DI_PRIMARY_LINK, &head, dvlink_cb);
7428d483882Smlf 
7438d483882Smlf 	/*
7448d483882Smlf 	 * We may be switching between EFI label and SMI label in which case
7458d483882Smlf 	 * we only have minors of the other type.
7468d483882Smlf 	 */
7478d483882Smlf 	if (head == NULL && (*mn == *(MN_SMI) ||
7488d483882Smlf 	    (strncmp(mn, MN_EFI, 2) == 0))) {
7498d483882Smlf 		devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. "
7508d483882Smlf 		    "Trying another label\n", modname, mn);
7518d483882Smlf 		s = strrchr(phys_path, ':');
7528d483882Smlf 		if (s == NULL) {
7538d483882Smlf 			devfsadm_errprint("%s: invalid minor path: %s\n",
7548d483882Smlf 			    modname, phys_path);
7558d483882Smlf 			return (DEVFSADM_FAILURE);
7568d483882Smlf 		}
7578d483882Smlf 		(void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path),
7588d483882Smlf 		    "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI,
7598d483882Smlf 		    strstr(s, ",raw") ? ",raw" : "");
7608d483882Smlf 		(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE,
7618d483882Smlf 		    phys_path, DI_PRIMARY_LINK, &head, dvlink_cb);
7628d483882Smlf 	}
7638d483882Smlf 
7648d483882Smlf 	if (head == NULL) {
7658d483882Smlf 		devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n",
7668d483882Smlf 		    modname, phys_path);
7678d483882Smlf 		/* no links on disk */
7688d483882Smlf 		return (DEVFSADM_FAILURE);
7698d483882Smlf 	}
7708d483882Smlf 
7718d483882Smlf 	/*
7728d483882Smlf 	 * It suffices to use 1 link to this minor, since
7738d483882Smlf 	 * we are matching with reserved IDs on the basis of
7748d483882Smlf 	 * the controller number which will be the same for
7758d483882Smlf 	 * all links to this minor.
7768d483882Smlf 	 */
7778d483882Smlf 	if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) {
7788d483882Smlf 		/* not reserved links */
7798d483882Smlf 		devfsadm_print(disk_mid, "%s: devlink %s and its minor "
7808d483882Smlf 		    "are NOT reserved\n", modname, head->dv_link);
7818d483882Smlf 		free_dvlist(&head);
7828d483882Smlf 		return (DEVFSADM_FAILURE);
7838d483882Smlf 	}
7848d483882Smlf 
7858d483882Smlf 	devfsadm_print(disk_mid, "%s: devlink %s and its minor are on "
7868d483882Smlf 	    "reserved list\n", modname, head->dv_link);
7878d483882Smlf 
7888d483882Smlf 	/*
7898d483882Smlf 	 * Switch between SMI and EFI labels if required
7908d483882Smlf 	 */
7918d483882Smlf 	switch_link = 0;
7928d483882Smlf 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
7938d483882Smlf 		for (entry = head; entry; entry = entry->dv_next) {
7948d483882Smlf 			s = strrchr(entry->dv_link, '/');
7958d483882Smlf 			assert(s);
7968d483882Smlf 			if (s == NULL) {
7978d483882Smlf 				devfsadm_errprint("%s: disk link %s has no "
7988d483882Smlf 				    "directory\n", modname, entry->dv_link);
7998d483882Smlf 				continue;
8008d483882Smlf 			}
8018d483882Smlf 			if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) {
8028d483882Smlf 				(void) snprintf(l, sizeof (l), "%s%s",
8038d483882Smlf 				    entry->dv_link, SLICE_SMI);
8048d483882Smlf 				switch_link = 1;
8058d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
8068d483882Smlf 				    "reserved link from EFI to SMI label. "
8078d483882Smlf 				    "New link is %s\n", modname, l);
8088d483882Smlf 			} else if (strncmp(mn, MN_EFI, 2) == 0 &&
8098d483882Smlf 			    (s = strchr(s, 's'))) {
8108d483882Smlf 				*s = '\0';
8118d483882Smlf 				(void) snprintf(l, sizeof (l), "%s",
8128d483882Smlf 				    entry->dv_link);
8138d483882Smlf 				*s = 's';
8148d483882Smlf 				switch_link = 1;
8158d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
8168d483882Smlf 				    "reserved link from SMI to EFI label. "
8178d483882Smlf 				    "New link is %s\n", modname, l);
8188d483882Smlf 			}
8198d483882Smlf 			if (switch_link) {
8208d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
8218d483882Smlf 				    "link: deleting %s and creating %s\n",
8228d483882Smlf 				    modname, entry->dv_link, l);
8238d483882Smlf 				devfsadm_rm_link(entry->dv_link);
8248d483882Smlf 				(void) devfsadm_mklink(l, node, minor, nflags);
8258d483882Smlf 			}
8268d483882Smlf 		}
8278d483882Smlf 	}
8288d483882Smlf 	free_dvlist(&head);
8298d483882Smlf 
8308d483882Smlf 	/*
8318d483882Smlf 	 * return SUCCESS to indicate that new links to this minor should not
8328d483882Smlf 	 * be created so that only compatibility links to this minor remain.
8338d483882Smlf 	 */
8348d483882Smlf 	return (DEVFSADM_SUCCESS);
8358d483882Smlf }
836