xref: /onnv-gate/usr/src/cmd/devfsadm/disk_link.c (revision 11350:6a6fb5fce6d1)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229249SJaven.Wu@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <devfsadm.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <limits.h>
316500Sjhd #include <ctype.h>
3210696SDavid.Hollister@Sun.COM #include <sys/int_fmtio.h>
330Sstevel@tonic-gate #include <sys/stat.h>
341676Sjpk #include <bsm/devalloc.h>
3510696SDavid.Hollister@Sun.COM #include <sys/scsi/scsi_address.h>
3610696SDavid.Hollister@Sun.COM #include <sys/libdevid.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	DISK_SUBPATH_MAX 100
390Sstevel@tonic-gate #define	RM_STALE 0x01
400Sstevel@tonic-gate #define	DISK_LINK_RE	"^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
410Sstevel@tonic-gate #define	DISK_LINK_TO_UPPER(ch)\
420Sstevel@tonic-gate 	(((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	SLICE_SMI	"s7"
450Sstevel@tonic-gate #define	SLICE_EFI	""
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	MN_SMI		"h"
480Sstevel@tonic-gate #define	MN_EFI		"wd"
490Sstevel@tonic-gate #define	ASCIIWWNSIZE	255
5010021SSheshadri.Vasudevan@Sun.COM #if defined(__i386) || defined(__amd64)
5110021SSheshadri.Vasudevan@Sun.COM /*
5210021SSheshadri.Vasudevan@Sun.COM  * The number of minor nodes per LUN is defined by the disk drivers.
5310021SSheshadri.Vasudevan@Sun.COM  * Currently it is set to 64. Refer CMLBUNIT_SHIFT (cmlb_impl.h)
5410021SSheshadri.Vasudevan@Sun.COM  */
5510021SSheshadri.Vasudevan@Sun.COM #define	NUM_MINORS_PER_INSTANCE	64
5610021SSheshadri.Vasudevan@Sun.COM #endif
5710021SSheshadri.Vasudevan@Sun.COM 
580Sstevel@tonic-gate 
591676Sjpk extern int system_labeled;
601676Sjpk 
610Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node);
620Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node);
630Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node);
646500Sjhd static int disk_callback_xvmd(di_minor_t minor, di_node_t node);
650Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node);
669249SJaven.Wu@Sun.COM static int disk_callback_sas(di_minor_t minor, di_node_t node);
670Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk,
680Sstevel@tonic-gate 				int flags);
690Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor);
704876Smlf static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = {
7410696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_BLOCK, NULL,
750Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
760Sstevel@tonic-gate 	},
7710696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_BLOCK_CHAN, NULL,
780Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
790Sstevel@tonic-gate 	},
8010696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_BLOCK_FABRIC, NULL,
810Sstevel@tonic-gate 		TYPE_EXACT, ILEVEL_0, disk_callback_fabric
820Sstevel@tonic-gate 	},
8310696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_BLOCK_WWN, NULL,
840Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_wwn
850Sstevel@tonic-gate 	},
8610696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_BLOCK_SAS, NULL,
879249SJaven.Wu@Sun.COM 	    TYPE_EXACT, ILEVEL_0, disk_callback_sas
889249SJaven.Wu@Sun.COM 	},
8910696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_CD, NULL,
900Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
910Sstevel@tonic-gate 	},
9210696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_CD_CHAN, NULL,
930Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
940Sstevel@tonic-gate 	},
9510696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_BLOCK_XVMD, NULL,
966500Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
976500Sjhd 	},
9810696SDavid.Hollister@Sun.COM 	{ "disk", DDI_NT_CD_XVMD, NULL,
996500Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
1006500Sjhd 	},
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * HOT auto cleanup of disks not desired.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = {
1090Sstevel@tonic-gate 	{ "disk", DISK_LINK_RE, RM_POST,
1100Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
1150Sstevel@tonic-gate 
1164876Smlf static devlink_re_t disks_re_array[] = {
1174876Smlf 	{"^r?dsk/c([0-9]+)", 1},
1184876Smlf 	{"^cfg/c([0-9]+)$", 1},
1194876Smlf 	{"^scsi/.+/c([0-9]+)", 1},
1204876Smlf 	{NULL}
1214876Smlf };
1224876Smlf 
1234876Smlf static char *disk_mid = "disk_mid";
1244876Smlf static char *modname = "disk_link";
1254876Smlf 
1264876Smlf int
minor_init()1274876Smlf minor_init()
1284876Smlf {
1294876Smlf 	devfsadm_print(disk_mid,
1304876Smlf 	    "%s: minor_init(): Creating disks reserved ID cache\n",
1314876Smlf 	    modname);
1324876Smlf 	return (devfsadm_reserve_id_cache(disks_re_array, NULL));
1334876Smlf }
1344876Smlf 
1350Sstevel@tonic-gate static int
disk_callback_chan(di_minor_t minor,di_node_t node)1360Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	char *addr;
1390Sstevel@tonic-gate 	char disk[20];
1400Sstevel@tonic-gate 	uint_t targ;
1410Sstevel@tonic-gate 	uint_t lun;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	addr = di_bus_addr(node);
1440Sstevel@tonic-gate 	(void) sscanf(addr, "%X,%X", &targ, &lun);
1450Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1460Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1470Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static int
disk_callback_nchan(di_minor_t minor,di_node_t node)1520Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	char *addr;
1550Sstevel@tonic-gate 	char disk[10];
1560Sstevel@tonic-gate 	uint_t lun;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	addr = di_bus_addr(node);
1590Sstevel@tonic-gate 	(void) sscanf(addr, "%X", &lun);
1600Sstevel@tonic-gate 	(void) sprintf(disk, "d%d", lun);
1610Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1620Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static int
disk_callback_wwn(di_minor_t minor,di_node_t node)1670Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	char disk[10];
1700Sstevel@tonic-gate 	int lun;
1710Sstevel@tonic-gate 	int targ;
1720Sstevel@tonic-gate 	int *intp;
1730Sstevel@tonic-gate 
17410696SDavid.Hollister@Sun.COM 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_TARGET,
17510696SDavid.Hollister@Sun.COM 	    &intp) <= 0) {
1760Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 	targ = *intp;
17910696SDavid.Hollister@Sun.COM 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_LUN,
18010696SDavid.Hollister@Sun.COM 	    &intp) <= 0) {
1816065Scth 		lun = 0;
1820Sstevel@tonic-gate 	} else {
1836065Scth 		lun = *intp;
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate static int
disk_callback_fabric(di_minor_t minor,di_node_t node)1930Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	char disk[DISK_SUBPATH_MAX];
1960Sstevel@tonic-gate 	int lun;
1970Sstevel@tonic-gate 	int count;
1980Sstevel@tonic-gate 	int *intp;
1990Sstevel@tonic-gate 	uchar_t *str;
2000Sstevel@tonic-gate 	uchar_t *wwn;
2010Sstevel@tonic-gate 	uchar_t ascii_wwn[ASCIIWWNSIZE];
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2040Sstevel@tonic-gate 	    "client-guid", (char **)&wwn) > 0) {
2056065Scth 		if (strlcpy((char *)ascii_wwn, (char *)wwn,
2066065Scth 		    sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) {
2070Sstevel@tonic-gate 			devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
2086065Scth 			    strlen((char *)wwn));
2090Sstevel@tonic-gate 			return (DEVFSADM_CONTINUE);
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 		lun = 0;
2120Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
2130Sstevel@tonic-gate 	    "port-wwn", &wwn) > 0) {
2140Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
21510696SDavid.Hollister@Sun.COM 		    SCSI_ADDR_PROP_LUN, &intp) > 0) {
2160Sstevel@tonic-gate 			lun = *intp;
2170Sstevel@tonic-gate 		} else {
2180Sstevel@tonic-gate 			lun = 0;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
2220Sstevel@tonic-gate 			(void) sprintf((caddr_t)str, "%02x", wwn[count]);
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 		*str = '\0';
2250Sstevel@tonic-gate 	} else {
2260Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	for (str = ascii_wwn; *str != '\0'; str++) {
2300Sstevel@tonic-gate 		*str = DISK_LINK_TO_UPPER(*str);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2409249SJaven.Wu@Sun.COM static int
disk_callback_sas(di_minor_t minor,di_node_t node)2419249SJaven.Wu@Sun.COM disk_callback_sas(di_minor_t minor, di_node_t node)
2429249SJaven.Wu@Sun.COM {
2439249SJaven.Wu@Sun.COM 	char disk[DISK_SUBPATH_MAX];
24410696SDavid.Hollister@Sun.COM 	int lun64_found = 0;
24510696SDavid.Hollister@Sun.COM 	scsi_lun64_t lun64, sl;
24610696SDavid.Hollister@Sun.COM 	scsi_lun_t lun;
24710696SDavid.Hollister@Sun.COM 	int64_t *lun64p;
24810696SDavid.Hollister@Sun.COM 	uint64_t wwn;
2499249SJaven.Wu@Sun.COM 	int *intp;
25010696SDavid.Hollister@Sun.COM 	char *tgt_port;
25110696SDavid.Hollister@Sun.COM 	uchar_t addr_method;
2529249SJaven.Wu@Sun.COM 
25310696SDavid.Hollister@Sun.COM 	/* Get lun property */
25410696SDavid.Hollister@Sun.COM 	if (di_prop_lookup_int64(DDI_DEV_T_ANY, node,
25510696SDavid.Hollister@Sun.COM 	    SCSI_ADDR_PROP_LUN64, &lun64p) > 0) {
25610696SDavid.Hollister@Sun.COM 		if (*lun64p != SCSI_LUN64_ILLEGAL) {
25710696SDavid.Hollister@Sun.COM 			lun64_found = 1;
25810696SDavid.Hollister@Sun.COM 			lun64 = (uint64_t)*lun64p;
25910696SDavid.Hollister@Sun.COM 		}
2609249SJaven.Wu@Sun.COM 	}
26110696SDavid.Hollister@Sun.COM 	if ((!lun64_found) && (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
26210696SDavid.Hollister@Sun.COM 	    SCSI_ADDR_PROP_LUN, &intp) > 0)) {
26310696SDavid.Hollister@Sun.COM 		lun64 = (uint64_t)*intp;
26410696SDavid.Hollister@Sun.COM 	}
26510696SDavid.Hollister@Sun.COM 
26610696SDavid.Hollister@Sun.COM 	lun = scsi_lun64_to_lun(lun64);
26710696SDavid.Hollister@Sun.COM 
26810696SDavid.Hollister@Sun.COM 	addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
26910696SDavid.Hollister@Sun.COM 
2709249SJaven.Wu@Sun.COM 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
27110696SDavid.Hollister@Sun.COM 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
27210696SDavid.Hollister@Sun.COM 		(void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
27310696SDavid.Hollister@Sun.COM 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
27410696SDavid.Hollister@Sun.COM 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
27510696SDavid.Hollister@Sun.COM 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
27610696SDavid.Hollister@Sun.COM 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
27710696SDavid.Hollister@Sun.COM 			(void) snprintf(disk, DISK_SUBPATH_MAX,
27810696SDavid.Hollister@Sun.COM 			    "t%"PRIX64"d%"PRId64, wwn, lun64);
27910696SDavid.Hollister@Sun.COM 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
28010696SDavid.Hollister@Sun.COM 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
28110696SDavid.Hollister@Sun.COM 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
28210696SDavid.Hollister@Sun.COM 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
28310696SDavid.Hollister@Sun.COM 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
28410696SDavid.Hollister@Sun.COM 			(void) snprintf(disk, DISK_SUBPATH_MAX,
28510696SDavid.Hollister@Sun.COM 			    "t%"PRIX64"d%"PRIX16, wwn, sl);
28610696SDavid.Hollister@Sun.COM 		} else {
28710696SDavid.Hollister@Sun.COM 			(void) snprintf(disk, DISK_SUBPATH_MAX,
28810696SDavid.Hollister@Sun.COM 			    "t%"PRIX64"d%"PRIX64, wwn, lun64);
2899249SJaven.Wu@Sun.COM 		}
2909249SJaven.Wu@Sun.COM 	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
29110696SDavid.Hollister@Sun.COM 	    SCSI_ADDR_PROP_SATA_PHY, &intp) > 0) {
29210696SDavid.Hollister@Sun.COM 		/* Use phy format naming, for SATA devices without wwn */
29310696SDavid.Hollister@Sun.COM 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
29410696SDavid.Hollister@Sun.COM 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
29510696SDavid.Hollister@Sun.COM 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
29610696SDavid.Hollister@Sun.COM 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
29710696SDavid.Hollister@Sun.COM 			(void) snprintf(disk, DISK_SUBPATH_MAX,
298*11350SBo.Zhou@Sun.COM 			    "t%dd%"PRId64, *intp, lun64);
29910696SDavid.Hollister@Sun.COM 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
30010696SDavid.Hollister@Sun.COM 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
30110696SDavid.Hollister@Sun.COM 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
30210696SDavid.Hollister@Sun.COM 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
30310696SDavid.Hollister@Sun.COM 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
30410696SDavid.Hollister@Sun.COM 			(void) snprintf(disk, DISK_SUBPATH_MAX,
305*11350SBo.Zhou@Sun.COM 			    "t%dd%"PRIX16, *intp, sl);
30610696SDavid.Hollister@Sun.COM 		} else {
30710696SDavid.Hollister@Sun.COM 			(void) snprintf(disk, DISK_SUBPATH_MAX,
308*11350SBo.Zhou@Sun.COM 			    "t%dd%"PRIX64, *intp, lun64);
30910696SDavid.Hollister@Sun.COM 		}
3109249SJaven.Wu@Sun.COM 	} else {
3119249SJaven.Wu@Sun.COM 		return (DEVFSADM_CONTINUE);
3129249SJaven.Wu@Sun.COM 	}
3139249SJaven.Wu@Sun.COM 
3149249SJaven.Wu@Sun.COM 	disk_common(minor, node, disk, RM_STALE);
3159249SJaven.Wu@Sun.COM 
3169249SJaven.Wu@Sun.COM 	return (DEVFSADM_CONTINUE);
3179249SJaven.Wu@Sun.COM }
3189249SJaven.Wu@Sun.COM 
3190Sstevel@tonic-gate /*
3206500Sjhd  * xVM virtual block device
3216500Sjhd  *
3226500Sjhd  * VBDs are enumerated into xenstore by xend and named using
3236500Sjhd  * the linux dev_t values for 'hd' and 'xvd' devices.  Linux
3246500Sjhd  * dev_t's are 16-bit values.  The upper 8 bits identify the major #
3256500Sjhd  * of the device (hd, xvd) and the lower 8 bits the instance and partition
3266500Sjhd  *
3276500Sjhd  * For PV guests, VBDs are named by the virt-tools using
3286500Sjhd  * the form xvd[a-p][1-15].  The corresponding Solaris /dev/dsk name
3296500Sjhd  * created by this generator will be c0t[0-15]d[0-15]sN,
3306500Sjhd  * were the target (t) value represents [a-p] and the
3316500Sjhd  * disk (d) value is either 0 (e.g. xvda) or contains the partition
3326500Sjhd  * information if it has been specified [1-15] (e.g. xvda1)
3336500Sjhd  *
3346500Sjhd  * For PV guests using the legacy naming (0, 1, 2, ...)
3356500Sjhd  * the Solaris disk names created will be c0d[0..767]sN
3366500Sjhd  * The Solaris version of virt-install based on virtinst.101
3376500Sjhd  * named PV disks as sequential integers.  With virtinst.300_1 and
3386500Sjhd  * beyond, the virt-* tools will no longer create legacy disk
3396500Sjhd  * names.
3406500Sjhd  */
3416500Sjhd static int
disk_callback_xvmd(di_minor_t minor,di_node_t node)3426500Sjhd disk_callback_xvmd(di_minor_t minor, di_node_t node)
3436500Sjhd {
3446500Sjhd #define	HD_BASE (3 << 8)
3456500Sjhd #define	XVBDMAJ 202
3466500Sjhd 
3476500Sjhd 	char *addr;
3486500Sjhd 	char disk[16];
3496500Sjhd 	uint_t targ;
3506500Sjhd 	uint_t lun = 0;
3516500Sjhd 	uint_t fmaj;
3526500Sjhd 
3536500Sjhd 	addr = di_bus_addr(node);
3546500Sjhd 	targ = strtol(addr, (char **)NULL, 10);
3556500Sjhd 	fmaj = targ >> 8;
3566500Sjhd 
3576500Sjhd 	/* legacy device address */
3586500Sjhd 	if (targ < HD_BASE)
3596500Sjhd 		(void) snprintf(disk, sizeof (disk),  "d%d", targ);
3606500Sjhd 	/* PV VBD */
3616500Sjhd 	else if (fmaj == XVBDMAJ) {
3626500Sjhd 		lun = targ & 0xf;
3636500Sjhd 		targ = (targ & 0xff) >> 4;
3646500Sjhd 		(void) snprintf(disk, sizeof (disk), "t%dd%d", targ, lun);
3656500Sjhd 	/* HVM device names are generated using the standard generator */
3666500Sjhd 	} else {
3676500Sjhd 		devfsadm_errprint("%s: invalid disk device number (%s)\n",
3686500Sjhd 		    modname, addr);
3696500Sjhd 		return (DEVFSADM_CONTINUE);
3706500Sjhd 	}
3716500Sjhd 	disk_common(minor, node, disk, 0);
3726500Sjhd 	return (DEVFSADM_CONTINUE);
3736500Sjhd 
3746500Sjhd }
3756500Sjhd 
3766500Sjhd /*
3770Sstevel@tonic-gate  * This function is called for every disk minor node.
3780Sstevel@tonic-gate  * Calls enumerate to assign a logical controller number, and
3790Sstevel@tonic-gate  * then devfsadm_mklink to make the link.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate static void
disk_common(di_minor_t minor,di_node_t node,char * disk,int flags)3820Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
3852912Sartem 	char sec_path[PATH_MAX + 1];
3860Sstevel@tonic-gate 	char stale_re[DISK_SUBPATH_MAX];
3870Sstevel@tonic-gate 	char *dir;
3880Sstevel@tonic-gate 	char slice[4];
3890Sstevel@tonic-gate 	char *mn;
3900Sstevel@tonic-gate 	char *ctrl;
3911676Sjpk 	char *nt = NULL;
3922912Sartem 	int *int_prop;
3931676Sjpk 	int  nflags = 0;
39410021SSheshadri.Vasudevan@Sun.COM #if defined(__i386) || defined(__amd64)
39510021SSheshadri.Vasudevan@Sun.COM 	char mn_copy[4];
39610021SSheshadri.Vasudevan@Sun.COM 	char *part;
39710021SSheshadri.Vasudevan@Sun.COM 	int part_num;
39810021SSheshadri.Vasudevan@Sun.COM #endif
3990Sstevel@tonic-gate 
40010021SSheshadri.Vasudevan@Sun.COM 	mn = di_minor_name(minor);
40110021SSheshadri.Vasudevan@Sun.COM 	if (strstr(mn, ",raw")) {
4020Sstevel@tonic-gate 		dir = "rdsk";
40310021SSheshadri.Vasudevan@Sun.COM #if defined(__i386) || defined(__amd64)
40410021SSheshadri.Vasudevan@Sun.COM 		(void) strncpy(mn_copy, mn, 4);
40510021SSheshadri.Vasudevan@Sun.COM 		part = strtok(mn_copy, ",");
40610021SSheshadri.Vasudevan@Sun.COM #endif
4070Sstevel@tonic-gate 	} else {
4080Sstevel@tonic-gate 		dir = "dsk";
40910021SSheshadri.Vasudevan@Sun.COM #if defined(__i386) || defined(__amd64)
41010021SSheshadri.Vasudevan@Sun.COM 		part = mn;
41110021SSheshadri.Vasudevan@Sun.COM #endif
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
41410021SSheshadri.Vasudevan@Sun.COM #if defined(__i386) || defined(__amd64)
41510021SSheshadri.Vasudevan@Sun.COM 	/*
41610021SSheshadri.Vasudevan@Sun.COM 	 * The following is a table describing the allocation of
41710021SSheshadri.Vasudevan@Sun.COM 	 * minor numbers, minor names and /dev/dsk names for partitions
41810021SSheshadri.Vasudevan@Sun.COM 	 * and slices on x86 systems.
41910021SSheshadri.Vasudevan@Sun.COM 	 *
42010021SSheshadri.Vasudevan@Sun.COM 	 *	Minor Number	Minor Name	/dev/dsk name
42110021SSheshadri.Vasudevan@Sun.COM 	 *	---------------------------------------------
42210021SSheshadri.Vasudevan@Sun.COM 	 *	0 to 15		"a" to "p"	s0 to s15
42310021SSheshadri.Vasudevan@Sun.COM 	 *	16		"q"		p0
42410021SSheshadri.Vasudevan@Sun.COM 	 *	17 to 20	"r" to "u"	p1 to p4
42510021SSheshadri.Vasudevan@Sun.COM 	 *	21 to 52	"p5" to "p36"	p5 to p36
42610021SSheshadri.Vasudevan@Sun.COM 	 *
42710021SSheshadri.Vasudevan@Sun.COM 	 */
42810021SSheshadri.Vasudevan@Sun.COM 	part_num = atoi(part + 1);
42910021SSheshadri.Vasudevan@Sun.COM 
43010021SSheshadri.Vasudevan@Sun.COM 	if ((mn[0] == 'p') && (part_num >= 5)) {
43110021SSheshadri.Vasudevan@Sun.COM 		/* logical drive */
43210021SSheshadri.Vasudevan@Sun.COM 		(void) snprintf(slice, 4, "%s", part);
43310021SSheshadri.Vasudevan@Sun.COM 	} else {
43410021SSheshadri.Vasudevan@Sun.COM #endif
43510021SSheshadri.Vasudevan@Sun.COM 	if (mn[0] < 'q') {
4360Sstevel@tonic-gate 		(void) sprintf(slice, "s%d", mn[0] - 'a');
4370Sstevel@tonic-gate 	} else if (strncmp(mn, MN_EFI, 2) != 0) {
4380Sstevel@tonic-gate 		(void) sprintf(slice, "p%d", mn[0] - 'q');
4390Sstevel@tonic-gate 	} else {
4400Sstevel@tonic-gate 		/* For EFI label */
4410Sstevel@tonic-gate 		(void) sprintf(slice, SLICE_EFI);
4420Sstevel@tonic-gate 	}
44310021SSheshadri.Vasudevan@Sun.COM #if defined(__i386) || defined(__amd64)
44410021SSheshadri.Vasudevan@Sun.COM 	}
44510021SSheshadri.Vasudevan@Sun.COM #endif
4460Sstevel@tonic-gate 
4474876Smlf 	nflags = 0;
4484876Smlf 	if (system_labeled) {
4494876Smlf 		nt = di_minor_nodetype(minor);
4504876Smlf 		if ((nt != NULL) &&
4514876Smlf 		    ((strcmp(nt, DDI_NT_CD) == 0) ||
4524876Smlf 		    (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
4534876Smlf 		    (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
4544876Smlf 			nflags = DA_ADD|DA_CD;
4554876Smlf 		}
4564876Smlf 	}
4574876Smlf 
4584876Smlf 	if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
4594876Smlf 		devfsadm_print(disk_mid, "Reserved link exists. Not "
4604876Smlf 		    "creating links for slice %s\n", slice);
4614876Smlf 		return;
4624876Smlf 	}
4634876Smlf 
4640Sstevel@tonic-gate 	if (NULL == (ctrl = diskctrl(node, minor)))
4650Sstevel@tonic-gate 		return;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	(void) strcpy(l_path, dir);
4680Sstevel@tonic-gate 	(void) strcat(l_path, "/c");
4690Sstevel@tonic-gate 	(void) strcat(l_path, ctrl);
4700Sstevel@tonic-gate 	(void) strcat(l_path, disk);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/*
4730Sstevel@tonic-gate 	 * If switching between SMI and EFI label or vice versa
4740Sstevel@tonic-gate 	 * cleanup the previous label's devlinks.
4750Sstevel@tonic-gate 	 */
4760Sstevel@tonic-gate 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
4770Sstevel@tonic-gate 		char *s, tpath[PATH_MAX + 1];
4780Sstevel@tonic-gate 		struct stat sb;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		s = l_path + strlen(l_path);
4810Sstevel@tonic-gate 		(void) strcat(l_path, (*mn == *(MN_SMI))
4820Sstevel@tonic-gate 		    ? SLICE_EFI : SLICE_SMI);
4830Sstevel@tonic-gate 		/*
4840Sstevel@tonic-gate 		 * Attempt the remove only if the stale link exists
4850Sstevel@tonic-gate 		 */
4860Sstevel@tonic-gate 		(void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
4870Sstevel@tonic-gate 		    devfsadm_root_path(), l_path);
4880Sstevel@tonic-gate 		if (lstat(tpath, &sb) != -1)
4890Sstevel@tonic-gate 			devfsadm_rm_all(l_path);
4900Sstevel@tonic-gate 		*s = '\0';
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	(void) strcat(l_path, slice);
4930Sstevel@tonic-gate 
4941676Sjpk 	(void) devfsadm_mklink(l_path, node, minor, nflags);
4950Sstevel@tonic-gate 
4962912Sartem 	/* secondary links for removable and hotpluggable devices */
4972912Sartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
4982912Sartem 	    &int_prop) >= 0) {
4992912Sartem 		(void) strcpy(sec_path, "removable-media/");
5002912Sartem 		(void) strcat(sec_path, l_path);
5012912Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
5023020Sartem 	}
5033020Sartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
5042912Sartem 	    &int_prop) >= 0) {
5052912Sartem 		(void) strcpy(sec_path, "hotpluggable/");
5062912Sartem 		(void) strcat(sec_path, l_path);
5072912Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
5082912Sartem 	}
5092912Sartem 
5100Sstevel@tonic-gate 	if ((flags & RM_STALE) == RM_STALE) {
5110Sstevel@tonic-gate 		(void) strcpy(stale_re, "^");
5120Sstevel@tonic-gate 		(void) strcat(stale_re, dir);
5130Sstevel@tonic-gate 		(void) strcat(stale_re, "/c");
5140Sstevel@tonic-gate 		(void) strcat(stale_re, ctrl);
5150Sstevel@tonic-gate 		(void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
5160Sstevel@tonic-gate 		/*
5170Sstevel@tonic-gate 		 * optimizations are made inside of devfsadm_rm_stale_links
5180Sstevel@tonic-gate 		 * instead of before calling the function, as it always
5190Sstevel@tonic-gate 		 * needs to add the valid link to the cache.
5200Sstevel@tonic-gate 		 */
5210Sstevel@tonic-gate 		devfsadm_rm_stale_links(stale_re, l_path, node, minor);
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	free(ctrl);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
5290Sstevel@tonic-gate #define	RULE_INDEX	0
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate static char *
diskctrl(di_node_t node,di_minor_t minor)5320Sstevel@tonic-gate diskctrl(di_node_t node, di_minor_t minor)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	char path[PATH_MAX + 1];
5350Sstevel@tonic-gate 	char *devfspath;
5360Sstevel@tonic-gate 	char *buf, *mn;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
5390Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
5400Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
5410Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
5420Sstevel@tonic-gate 	};
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	mn = di_minor_name(minor);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
5470Sstevel@tonic-gate 		return (NULL);
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
5500Sstevel@tonic-gate 	(void) strcat(path, ":");
5510Sstevel@tonic-gate 	(void) strcat(path, mn);
5520Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * Use controller component of disk path
5560Sstevel@tonic-gate 	 */
5570Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
5580Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 		/*
5610Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
5620Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
5630Sstevel@tonic-gate 		 * name also in the match it should fix this and only find one
5640Sstevel@tonic-gate 		 * logical controller. (See 4045879).
5650Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
5660Sstevel@tonic-gate 		 * no unique controller number for them in this case.
5670Sstevel@tonic-gate 		 *
5680Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
5690Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
5700Sstevel@tonic-gate 		 */
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
5730Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
5740Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
5750Sstevel@tonic-gate 			return (NULL);
5760Sstevel@tonic-gate 		}
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	return (buf);
5800Sstevel@tonic-gate }
5814876Smlf 
5824876Smlf typedef struct dvlist {
5834876Smlf 	char *dv_link;
5844876Smlf 	struct dvlist *dv_next;
5854876Smlf } dvlist_t;
5864876Smlf 
5874876Smlf static void
free_dvlist(dvlist_t ** pp)5884876Smlf free_dvlist(dvlist_t **pp)
5894876Smlf {
5904876Smlf 	dvlist_t *entry;
5914876Smlf 
5924876Smlf 	while (*pp) {
5934876Smlf 		entry = *pp;
5944876Smlf 		*pp = entry->dv_next;
5954876Smlf 		assert(entry->dv_link);
5964876Smlf 		free(entry->dv_link);
5974876Smlf 		free(entry);
5984876Smlf 	}
5994876Smlf }
6004876Smlf static int
dvlink_cb(di_devlink_t devlink,void * arg)6014876Smlf dvlink_cb(di_devlink_t devlink, void *arg)
6024876Smlf {
6034876Smlf 	char *path;
6044876Smlf 	char *can_path;
6054876Smlf 	dvlist_t **pp = (dvlist_t **)arg;
6064876Smlf 	dvlist_t *entry = NULL;
6074876Smlf 
6084876Smlf 	entry = calloc(1, sizeof (dvlist_t));
6094876Smlf 	if (entry == NULL) {
6104876Smlf 		devfsadm_errprint("%s: calloc failed\n", modname);
6114876Smlf 		goto error;
6124876Smlf 	}
6134876Smlf 
6144876Smlf 	path = (char *)di_devlink_path(devlink);
6154876Smlf 	assert(path);
6164876Smlf 	if (path == NULL) {
6174876Smlf 		devfsadm_errprint("%s: di_devlink_path() returned NULL\n",
6184876Smlf 		    modname);
6194876Smlf 		goto error;
6204876Smlf 	}
6214876Smlf 
6224876Smlf 	devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n",
6234876Smlf 	    modname, path);
6244876Smlf 
6254876Smlf 	/*
6264876Smlf 	 * Return linkname in canonical form i.e. without the
6274876Smlf 	 * "/dev/" prefix
6284876Smlf 	 */
6294876Smlf 	can_path = strstr(path, "/dev/");
6304876Smlf 	if (can_path == NULL) {
6314876Smlf 		devfsadm_errprint("%s: devlink path %s has no /dev/\n",
6324876Smlf 		    modname, path);
6334876Smlf 		goto error;
6344876Smlf 	}
6354876Smlf 
6364876Smlf 	entry->dv_link = s_strdup(can_path + strlen("/dev/"));
6374876Smlf 	entry->dv_next = *pp;
6384876Smlf 	*pp = entry;
6394876Smlf 
6404876Smlf 	return (DI_WALK_CONTINUE);
6414876Smlf 
6424876Smlf error:
6434876Smlf 	free(entry);
6444876Smlf 	free_dvlist(pp);
6454876Smlf 	*pp = NULL;
6464876Smlf 	return (DI_WALK_TERMINATE);
6474876Smlf }
6484876Smlf 
6494876Smlf /*
6504876Smlf  * Returns success only if all goes well. If there is no matching reserved link
6514876Smlf  * or if there is an error, we assume no match. It is better to err on the side
6524876Smlf  * of caution by creating extra links than to miss out creating a required link.
6534876Smlf  */
6544876Smlf static int
reserved_links_exist(di_node_t node,di_minor_t minor,int nflags)6554876Smlf reserved_links_exist(di_node_t node, di_minor_t minor, int nflags)
6564876Smlf {
6574876Smlf 	di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache();
6584876Smlf 	char phys_path[PATH_MAX];
6594876Smlf 	char *minor_path;
6604876Smlf 	dvlist_t *head;
6614876Smlf 	dvlist_t *entry;
6624876Smlf 	char *s;
6634876Smlf 	char l[PATH_MAX];
6644876Smlf 	int switch_link = 0;
6654876Smlf 	char *mn = di_minor_name(minor);
6664876Smlf 
6674876Smlf 	if (dvlink_cache == NULL || mn == NULL) {
6684876Smlf 		devfsadm_errprint("%s: No minor or devlink cache\n", modname);
6694876Smlf 		return (DEVFSADM_FAILURE);
6704876Smlf 	}
6714876Smlf 
6726065Scth 	if (!devfsadm_have_reserved()) {
6736065Scth 		devfsadm_print(disk_mid, "%s: No reserved links\n", modname);
6744876Smlf 		return (DEVFSADM_FAILURE);
6754876Smlf 	}
6764876Smlf 
6774876Smlf 	minor_path = di_devfs_minor_path(minor);
6784876Smlf 	if (minor_path == NULL) {
6794876Smlf 		devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname);
6804876Smlf 		return (DEVFSADM_FAILURE);
6814876Smlf 	}
6824876Smlf 
6834876Smlf 	(void) strlcpy(phys_path, minor_path, sizeof (phys_path));
6844876Smlf 
6854876Smlf 	di_devfs_path_free(minor_path);
6864876Smlf 
6874876Smlf 	head = NULL;
6884876Smlf 	(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path,
6894876Smlf 	    DI_PRIMARY_LINK, &head, dvlink_cb);
6904876Smlf 
6914876Smlf 	/*
6924876Smlf 	 * We may be switching between EFI label and SMI label in which case
6934876Smlf 	 * we only have minors of the other type.
6944876Smlf 	 */
6954876Smlf 	if (head == NULL && (*mn == *(MN_SMI) ||
6964876Smlf 	    (strncmp(mn, MN_EFI, 2) == 0))) {
6974876Smlf 		devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. "
6984876Smlf 		    "Trying another label\n", modname, mn);
6994876Smlf 		s = strrchr(phys_path, ':');
7004876Smlf 		if (s == NULL) {
7014876Smlf 			devfsadm_errprint("%s: invalid minor path: %s\n",
7024876Smlf 			    modname, phys_path);
7034876Smlf 			return (DEVFSADM_FAILURE);
7044876Smlf 		}
7054876Smlf 		(void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path),
7066065Scth 		    "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI,
7076065Scth 		    strstr(s, ",raw") ? ",raw" : "");
7084876Smlf 		(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE,
7094876Smlf 		    phys_path, DI_PRIMARY_LINK, &head, dvlink_cb);
7104876Smlf 	}
7114876Smlf 
7124876Smlf 	if (head == NULL) {
7134876Smlf 		devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n",
7144876Smlf 		    modname, phys_path);
7154876Smlf 		/* no links on disk */
7164876Smlf 		return (DEVFSADM_FAILURE);
7174876Smlf 	}
7184876Smlf 
7194876Smlf 	/*
7204876Smlf 	 * It suffices to use 1 link to this minor, since
7214876Smlf 	 * we are matching with reserved IDs on the basis of
7224876Smlf 	 * the controller number which will be the same for
7234876Smlf 	 * all links to this minor.
7244876Smlf 	 */
7254876Smlf 	if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) {
7264876Smlf 		/* not reserved links */
7274876Smlf 		devfsadm_print(disk_mid, "%s: devlink %s and its minor "
7284876Smlf 		    "are NOT reserved\n", modname, head->dv_link);
7294876Smlf 		free_dvlist(&head);
7304876Smlf 		return (DEVFSADM_FAILURE);
7314876Smlf 	}
7324876Smlf 
7334876Smlf 	devfsadm_print(disk_mid, "%s: devlink %s and its minor are on "
7344876Smlf 	    "reserved list\n", modname, head->dv_link);
7354876Smlf 
7364876Smlf 	/*
7374876Smlf 	 * Switch between SMI and EFI labels if required
7384876Smlf 	 */
7394876Smlf 	switch_link = 0;
7404876Smlf 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
7414876Smlf 		for (entry = head; entry; entry = entry->dv_next) {
7424876Smlf 			s = strrchr(entry->dv_link, '/');
7434876Smlf 			assert(s);
7444876Smlf 			if (s == NULL) {
7454876Smlf 				devfsadm_errprint("%s: disk link %s has no "
7464876Smlf 				    "directory\n", modname, entry->dv_link);
7474876Smlf 				continue;
7484876Smlf 			}
7494876Smlf 			if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) {
7504876Smlf 				(void) snprintf(l, sizeof (l), "%s%s",
7514876Smlf 				    entry->dv_link, SLICE_SMI);
7524876Smlf 				switch_link = 1;
7534876Smlf 				devfsadm_print(disk_mid, "%s: switching "
7544876Smlf 				    "reserved link from EFI to SMI label. "
7554876Smlf 				    "New link is %s\n", modname, l);
7564876Smlf 			} else if (strncmp(mn, MN_EFI, 2) == 0 &&
7574876Smlf 			    (s = strchr(s, 's'))) {
7584876Smlf 				*s = '\0';
7594876Smlf 				(void) snprintf(l, sizeof (l), "%s",
7604876Smlf 				    entry->dv_link);
7614876Smlf 				*s = 's';
7624876Smlf 				switch_link = 1;
7634876Smlf 				devfsadm_print(disk_mid, "%s: switching "
7644876Smlf 				    "reserved link from SMI to EFI label. "
7654876Smlf 				    "New link is %s\n", modname, l);
7664876Smlf 			}
7674876Smlf 			if (switch_link) {
7684876Smlf 				devfsadm_print(disk_mid, "%s: switching "
7694876Smlf 				    "link: deleting %s and creating %s\n",
7704876Smlf 				    modname, entry->dv_link, l);
7714876Smlf 				devfsadm_rm_link(entry->dv_link);
7724876Smlf 				(void) devfsadm_mklink(l, node, minor, nflags);
7734876Smlf 			}
7744876Smlf 		}
7754876Smlf 	}
7764876Smlf 	free_dvlist(&head);
7774876Smlf 
7784876Smlf 	/*
7794876Smlf 	 * return SUCCESS to indicate that new links to this minor should not
7804876Smlf 	 * be created so that only compatibility links to this minor remain.
7814876Smlf 	 */
7824876Smlf 	return (DEVFSADM_SUCCESS);
7834876Smlf }
784