xref: /illumos-gate/usr/src/cmd/devfsadm/disk_link.c (revision aa1b14e7)
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 /*
22d88e498aSjiang wu - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <devfsadm.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
3197869ac5Sjhd #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
3345916cd2Sjpk #include <bsm/devalloc.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define	DISK_SUBPATH_MAX 100
367c478bd9Sstevel@tonic-gate #define	RM_STALE 0x01
377c478bd9Sstevel@tonic-gate #define	DISK_LINK_RE	"^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
387c478bd9Sstevel@tonic-gate #define	DISK_LINK_TO_UPPER(ch)\
397c478bd9Sstevel@tonic-gate 	(((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	SLICE_SMI	"s7"
427c478bd9Sstevel@tonic-gate #define	SLICE_EFI	""
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	MN_SMI		"h"
457c478bd9Sstevel@tonic-gate #define	MN_EFI		"wd"
467c478bd9Sstevel@tonic-gate #define	ASCIIWWNSIZE	255
47*aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
48*aa1b14e7SSheshadri Vasudevan /*
49*aa1b14e7SSheshadri Vasudevan  * The number of minor nodes per LUN is defined by the disk drivers.
50*aa1b14e7SSheshadri Vasudevan  * Currently it is set to 64. Refer CMLBUNIT_SHIFT (cmlb_impl.h)
51*aa1b14e7SSheshadri Vasudevan  */
52*aa1b14e7SSheshadri Vasudevan #define	NUM_MINORS_PER_INSTANCE	64
53*aa1b14e7SSheshadri Vasudevan #endif
54*aa1b14e7SSheshadri Vasudevan 
557c478bd9Sstevel@tonic-gate 
5645916cd2Sjpk extern int system_labeled;
5745916cd2Sjpk 
587c478bd9Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node);
597c478bd9Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node);
607c478bd9Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node);
6197869ac5Sjhd static int disk_callback_xvmd(di_minor_t minor, di_node_t node);
627c478bd9Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node);
63d88e498aSjiang wu - Sun Microsystems - Beijing China static int disk_callback_sas(di_minor_t minor, di_node_t node);
647c478bd9Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk,
657c478bd9Sstevel@tonic-gate 				int flags);
667c478bd9Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor);
678d483882Smlf static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = {
717c478bd9Sstevel@tonic-gate 	{ "disk", "ddi_block", NULL,
727c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
737c478bd9Sstevel@tonic-gate 	},
747c478bd9Sstevel@tonic-gate 	{ "disk", "ddi_block:channel", NULL,
757c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
767c478bd9Sstevel@tonic-gate 	},
777c478bd9Sstevel@tonic-gate 	{ "disk", "ddi_block:fabric", NULL,
787c478bd9Sstevel@tonic-gate 		TYPE_EXACT, ILEVEL_0, disk_callback_fabric
797c478bd9Sstevel@tonic-gate 	},
807c478bd9Sstevel@tonic-gate 	{ "disk", "ddi_block:wwn", NULL,
817c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_wwn
827c478bd9Sstevel@tonic-gate 	},
83d88e498aSjiang wu - Sun Microsystems - Beijing China 	{ "disk", "ddi_block:sas", NULL,
84d88e498aSjiang wu - Sun Microsystems - Beijing China 	    TYPE_EXACT, ILEVEL_0, disk_callback_sas
85d88e498aSjiang wu - Sun Microsystems - Beijing China 	},
867c478bd9Sstevel@tonic-gate 	{ "disk", "ddi_block:cdrom", NULL,
877c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
887c478bd9Sstevel@tonic-gate 	},
897c478bd9Sstevel@tonic-gate 	{ "disk", "ddi_block:cdrom:channel", NULL,
907c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
917c478bd9Sstevel@tonic-gate 	},
9297869ac5Sjhd 	{ "disk", "ddi_block:xvmd", NULL,
9397869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
9497869ac5Sjhd 	},
9597869ac5Sjhd 	{ "disk", "ddi_block:cdrom:xvmd", NULL,
9697869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
9797869ac5Sjhd 	},
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * HOT auto cleanup of disks not desired.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = {
1067c478bd9Sstevel@tonic-gate 	{ "disk", DISK_LINK_RE, RM_POST,
1077c478bd9Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
1127c478bd9Sstevel@tonic-gate 
1138d483882Smlf static devlink_re_t disks_re_array[] = {
1148d483882Smlf 	{"^r?dsk/c([0-9]+)", 1},
1158d483882Smlf 	{"^cfg/c([0-9]+)$", 1},
1168d483882Smlf 	{"^scsi/.+/c([0-9]+)", 1},
1178d483882Smlf 	{NULL}
1188d483882Smlf };
1198d483882Smlf 
1208d483882Smlf static char *disk_mid = "disk_mid";
1218d483882Smlf static char *modname = "disk_link";
1228d483882Smlf 
1238d483882Smlf int
1248d483882Smlf minor_init()
1258d483882Smlf {
1268d483882Smlf 	devfsadm_print(disk_mid,
1278d483882Smlf 	    "%s: minor_init(): Creating disks reserved ID cache\n",
1288d483882Smlf 	    modname);
1298d483882Smlf 	return (devfsadm_reserve_id_cache(disks_re_array, NULL));
1308d483882Smlf }
1318d483882Smlf 
1327c478bd9Sstevel@tonic-gate static int
1337c478bd9Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	char *addr;
1367c478bd9Sstevel@tonic-gate 	char disk[20];
1377c478bd9Sstevel@tonic-gate 	uint_t targ;
1387c478bd9Sstevel@tonic-gate 	uint_t lun;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	addr = di_bus_addr(node);
1417c478bd9Sstevel@tonic-gate 	(void) sscanf(addr, "%X,%X", &targ, &lun);
1427c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1437c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1447c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static int
1497c478bd9Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	char *addr;
1527c478bd9Sstevel@tonic-gate 	char disk[10];
1537c478bd9Sstevel@tonic-gate 	uint_t lun;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	addr = di_bus_addr(node);
1567c478bd9Sstevel@tonic-gate 	(void) sscanf(addr, "%X", &lun);
1577c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "d%d", lun);
1587c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1597c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static int
1647c478bd9Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	char disk[10];
1677c478bd9Sstevel@tonic-gate 	int lun;
1687c478bd9Sstevel@tonic-gate 	int targ;
1697c478bd9Sstevel@tonic-gate 	int *intp;
1707c478bd9Sstevel@tonic-gate 
171e37c6c37Scth 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "target", &intp) <= 0) {
1727c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	targ = *intp;
175e37c6c37Scth 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "lun", &intp) <= 0) {
176e37c6c37Scth 		lun = 0;
1777c478bd9Sstevel@tonic-gate 	} else {
178e37c6c37Scth 		lun = *intp;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static int
1887c478bd9Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	char disk[DISK_SUBPATH_MAX];
1917c478bd9Sstevel@tonic-gate 	int lun;
1927c478bd9Sstevel@tonic-gate 	int count;
1937c478bd9Sstevel@tonic-gate 	int *intp;
1947c478bd9Sstevel@tonic-gate 	uchar_t *str;
1957c478bd9Sstevel@tonic-gate 	uchar_t *wwn;
1967c478bd9Sstevel@tonic-gate 	uchar_t ascii_wwn[ASCIIWWNSIZE];
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
1997c478bd9Sstevel@tonic-gate 	    "client-guid", (char **)&wwn) > 0) {
200e37c6c37Scth 		if (strlcpy((char *)ascii_wwn, (char *)wwn,
201e37c6c37Scth 		    sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) {
2027c478bd9Sstevel@tonic-gate 			devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
203e37c6c37Scth 			    strlen((char *)wwn));
2047c478bd9Sstevel@tonic-gate 			return (DEVFSADM_CONTINUE);
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 		lun = 0;
2077c478bd9Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
2087c478bd9Sstevel@tonic-gate 	    "port-wwn", &wwn) > 0) {
2097c478bd9Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2107c478bd9Sstevel@tonic-gate 		    "lun", &intp) > 0) {
2117c478bd9Sstevel@tonic-gate 			lun = *intp;
2127c478bd9Sstevel@tonic-gate 		} else {
2137c478bd9Sstevel@tonic-gate 			lun = 0;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
2177c478bd9Sstevel@tonic-gate 			(void) sprintf((caddr_t)str, "%02x", wwn[count]);
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 		*str = '\0';
2207c478bd9Sstevel@tonic-gate 	} else {
2217c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	for (str = ascii_wwn; *str != '\0'; str++) {
2257c478bd9Sstevel@tonic-gate 		*str = DISK_LINK_TO_UPPER(*str);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
235d88e498aSjiang wu - Sun Microsystems - Beijing China static int
236d88e498aSjiang wu - Sun Microsystems - Beijing China disk_callback_sas(di_minor_t minor, di_node_t node)
237d88e498aSjiang wu - Sun Microsystems - Beijing China {
238d88e498aSjiang wu - Sun Microsystems - Beijing China 	char disk[DISK_SUBPATH_MAX];
239d88e498aSjiang wu - Sun Microsystems - Beijing China 	int lun;
240d88e498aSjiang wu - Sun Microsystems - Beijing China 	int *intp;
241d88e498aSjiang wu - Sun Microsystems - Beijing China 	char *str;
242d88e498aSjiang wu - Sun Microsystems - Beijing China 	char *wwn;
243d88e498aSjiang wu - Sun Microsystems - Beijing China 
244d88e498aSjiang wu - Sun Microsystems - Beijing China 	/*
245d88e498aSjiang wu - Sun Microsystems - Beijing China 	 * get LUN property
246d88e498aSjiang wu - Sun Microsystems - Beijing China 	 */
247d88e498aSjiang wu - Sun Microsystems - Beijing China 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
248d88e498aSjiang wu - Sun Microsystems - Beijing China 	    "lun", &intp) > 0) {
249d88e498aSjiang wu - Sun Microsystems - Beijing China 		lun = *intp;
250d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else {
251d88e498aSjiang wu - Sun Microsystems - Beijing China 		lun = 0;
252d88e498aSjiang wu - Sun Microsystems - Beijing China 	}
253d88e498aSjiang wu - Sun Microsystems - Beijing China 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
254d88e498aSjiang wu - Sun Microsystems - Beijing China 	    "target-port", &wwn) > 0) {
255d88e498aSjiang wu - Sun Microsystems - Beijing China 		/*
256d88e498aSjiang wu - Sun Microsystems - Beijing China 		 * If the target-port property exist
257d88e498aSjiang wu - Sun Microsystems - Beijing China 		 * we use wwn format naming
258d88e498aSjiang wu - Sun Microsystems - Beijing China 		 */
259d88e498aSjiang wu - Sun Microsystems - Beijing China 		for (str = wwn; *str != '\0'; str++) {
260d88e498aSjiang wu - Sun Microsystems - Beijing China 			*str = DISK_LINK_TO_UPPER(*str);
261d88e498aSjiang wu - Sun Microsystems - Beijing China 		}
262d88e498aSjiang wu - Sun Microsystems - Beijing China 		(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", wwn, lun);
263d88e498aSjiang wu - Sun Microsystems - Beijing China 
264d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
265d88e498aSjiang wu - Sun Microsystems - Beijing China 	    "sata-phy", &intp) > 0) {
266d88e498aSjiang wu - Sun Microsystems - Beijing China 		/*
267d88e498aSjiang wu - Sun Microsystems - Beijing China 		 * For direct attached SATA device without Device Name,
268d88e498aSjiang wu - Sun Microsystems - Beijing China 		 * no wwn exist, we use phy format naming
269d88e498aSjiang wu - Sun Microsystems - Beijing China 		 */
270d88e498aSjiang wu - Sun Microsystems - Beijing China 		(void) snprintf(disk, DISK_SUBPATH_MAX, "t%dd%d", *intp, lun);
271d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else {
272d88e498aSjiang wu - Sun Microsystems - Beijing China 		return (DEVFSADM_CONTINUE);
273d88e498aSjiang wu - Sun Microsystems - Beijing China 	}
274d88e498aSjiang wu - Sun Microsystems - Beijing China 
275d88e498aSjiang wu - Sun Microsystems - Beijing China 	disk_common(minor, node, disk, RM_STALE);
276d88e498aSjiang wu - Sun Microsystems - Beijing China 
277d88e498aSjiang wu - Sun Microsystems - Beijing China 	return (DEVFSADM_CONTINUE);
278d88e498aSjiang wu - Sun Microsystems - Beijing China }
279d88e498aSjiang wu - Sun Microsystems - Beijing China 
28097869ac5Sjhd /*
28197869ac5Sjhd  * xVM virtual block device
28297869ac5Sjhd  *
28397869ac5Sjhd  * VBDs are enumerated into xenstore by xend and named using
28497869ac5Sjhd  * the linux dev_t values for 'hd' and 'xvd' devices.  Linux
28597869ac5Sjhd  * dev_t's are 16-bit values.  The upper 8 bits identify the major #
28697869ac5Sjhd  * of the device (hd, xvd) and the lower 8 bits the instance and partition
28797869ac5Sjhd  *
28897869ac5Sjhd  * For PV guests, VBDs are named by the virt-tools using
28997869ac5Sjhd  * the form xvd[a-p][1-15].  The corresponding Solaris /dev/dsk name
29097869ac5Sjhd  * created by this generator will be c0t[0-15]d[0-15]sN,
29197869ac5Sjhd  * were the target (t) value represents [a-p] and the
29297869ac5Sjhd  * disk (d) value is either 0 (e.g. xvda) or contains the partition
29397869ac5Sjhd  * information if it has been specified [1-15] (e.g. xvda1)
29497869ac5Sjhd  *
29597869ac5Sjhd  * For PV guests using the legacy naming (0, 1, 2, ...)
29697869ac5Sjhd  * the Solaris disk names created will be c0d[0..767]sN
29797869ac5Sjhd  * The Solaris version of virt-install based on virtinst.101
29897869ac5Sjhd  * named PV disks as sequential integers.  With virtinst.300_1 and
29997869ac5Sjhd  * beyond, the virt-* tools will no longer create legacy disk
30097869ac5Sjhd  * names.
30197869ac5Sjhd  */
30297869ac5Sjhd static int
30397869ac5Sjhd disk_callback_xvmd(di_minor_t minor, di_node_t node)
30497869ac5Sjhd {
30597869ac5Sjhd #define	HD_BASE (3 << 8)
30697869ac5Sjhd #define	XVBDMAJ 202
30797869ac5Sjhd 
30897869ac5Sjhd 	char *addr;
30997869ac5Sjhd 	char disk[16];
31097869ac5Sjhd 	uint_t targ;
31197869ac5Sjhd 	uint_t lun = 0;
31297869ac5Sjhd 	uint_t fmaj;
31397869ac5Sjhd 
31497869ac5Sjhd 	addr = di_bus_addr(node);
31597869ac5Sjhd 	targ = strtol(addr, (char **)NULL, 10);
31697869ac5Sjhd 	fmaj = targ >> 8;
31797869ac5Sjhd 
31897869ac5Sjhd 	/* legacy device address */
31997869ac5Sjhd 	if (targ < HD_BASE)
32097869ac5Sjhd 		(void) snprintf(disk, sizeof (disk),  "d%d", targ);
32197869ac5Sjhd 	/* PV VBD */
32297869ac5Sjhd 	else if (fmaj == XVBDMAJ) {
32397869ac5Sjhd 		lun = targ & 0xf;
32497869ac5Sjhd 		targ = (targ & 0xff) >> 4;
32597869ac5Sjhd 		(void) snprintf(disk, sizeof (disk), "t%dd%d", targ, lun);
32697869ac5Sjhd 	/* HVM device names are generated using the standard generator */
32797869ac5Sjhd 	} else {
32897869ac5Sjhd 		devfsadm_errprint("%s: invalid disk device number (%s)\n",
32997869ac5Sjhd 		    modname, addr);
33097869ac5Sjhd 		return (DEVFSADM_CONTINUE);
33197869ac5Sjhd 	}
33297869ac5Sjhd 	disk_common(minor, node, disk, 0);
33397869ac5Sjhd 	return (DEVFSADM_CONTINUE);
33497869ac5Sjhd 
33597869ac5Sjhd }
33697869ac5Sjhd 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * This function is called for every disk minor node.
3397c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical controller number, and
3407c478bd9Sstevel@tonic-gate  * then devfsadm_mklink to make the link.
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate static void
3437c478bd9Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
34618c2aff7Sartem 	char sec_path[PATH_MAX + 1];
3477c478bd9Sstevel@tonic-gate 	char stale_re[DISK_SUBPATH_MAX];
3487c478bd9Sstevel@tonic-gate 	char *dir;
3497c478bd9Sstevel@tonic-gate 	char slice[4];
3507c478bd9Sstevel@tonic-gate 	char *mn;
3517c478bd9Sstevel@tonic-gate 	char *ctrl;
35245916cd2Sjpk 	char *nt = NULL;
35318c2aff7Sartem 	int *int_prop;
35445916cd2Sjpk 	int  nflags = 0;
355*aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
356*aa1b14e7SSheshadri Vasudevan 	char mn_copy[4];
357*aa1b14e7SSheshadri Vasudevan 	char *part;
358*aa1b14e7SSheshadri Vasudevan 	int part_num;
359*aa1b14e7SSheshadri Vasudevan #endif
360*aa1b14e7SSheshadri Vasudevan 
361*aa1b14e7SSheshadri Vasudevan 	mn = di_minor_name(minor);
362*aa1b14e7SSheshadri Vasudevan 	if (strstr(mn, ",raw")) {
3637c478bd9Sstevel@tonic-gate 		dir = "rdsk";
364*aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
365*aa1b14e7SSheshadri Vasudevan 		(void) strncpy(mn_copy, mn, 4);
366*aa1b14e7SSheshadri Vasudevan 		part = strtok(mn_copy, ",");
367*aa1b14e7SSheshadri Vasudevan #endif
3687c478bd9Sstevel@tonic-gate 	} else {
3697c478bd9Sstevel@tonic-gate 		dir = "dsk";
370*aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
371*aa1b14e7SSheshadri Vasudevan 		part = mn;
372*aa1b14e7SSheshadri Vasudevan #endif
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
375*aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
376*aa1b14e7SSheshadri Vasudevan 	/*
377*aa1b14e7SSheshadri Vasudevan 	 * The following is a table describing the allocation of
378*aa1b14e7SSheshadri Vasudevan 	 * minor numbers, minor names and /dev/dsk names for partitions
379*aa1b14e7SSheshadri Vasudevan 	 * and slices on x86 systems.
380*aa1b14e7SSheshadri Vasudevan 	 *
381*aa1b14e7SSheshadri Vasudevan 	 *	Minor Number	Minor Name	/dev/dsk name
382*aa1b14e7SSheshadri Vasudevan 	 *	---------------------------------------------
383*aa1b14e7SSheshadri Vasudevan 	 *	0 to 15		"a" to "p"	s0 to s15
384*aa1b14e7SSheshadri Vasudevan 	 *	16		"q"		p0
385*aa1b14e7SSheshadri Vasudevan 	 *	17 to 20	"r" to "u"	p1 to p4
386*aa1b14e7SSheshadri Vasudevan 	 *	21 to 52	"p5" to "p36"	p5 to p36
387*aa1b14e7SSheshadri Vasudevan 	 *
388*aa1b14e7SSheshadri Vasudevan 	 */
389*aa1b14e7SSheshadri Vasudevan 	part_num = atoi(part + 1);
390*aa1b14e7SSheshadri Vasudevan 
391*aa1b14e7SSheshadri Vasudevan 	if ((mn[0] == 'p') && (part_num >= 5)) {
392*aa1b14e7SSheshadri Vasudevan 		/* logical drive */
393*aa1b14e7SSheshadri Vasudevan 		(void) snprintf(slice, 4, "%s", part);
394*aa1b14e7SSheshadri Vasudevan 	} else {
395*aa1b14e7SSheshadri Vasudevan #endif
396*aa1b14e7SSheshadri Vasudevan 	if (mn[0] < 'q') {
3977c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "s%d", mn[0] - 'a');
3987c478bd9Sstevel@tonic-gate 	} else if (strncmp(mn, MN_EFI, 2) != 0) {
3997c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "p%d", mn[0] - 'q');
4007c478bd9Sstevel@tonic-gate 	} else {
4017c478bd9Sstevel@tonic-gate 		/* For EFI label */
4027c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, SLICE_EFI);
4037c478bd9Sstevel@tonic-gate 	}
404*aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
405*aa1b14e7SSheshadri Vasudevan 	}
406*aa1b14e7SSheshadri Vasudevan #endif
4077c478bd9Sstevel@tonic-gate 
4088d483882Smlf 	nflags = 0;
4098d483882Smlf 	if (system_labeled) {
4108d483882Smlf 		nt = di_minor_nodetype(minor);
4118d483882Smlf 		if ((nt != NULL) &&
4128d483882Smlf 		    ((strcmp(nt, DDI_NT_CD) == 0) ||
4138d483882Smlf 		    (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
4148d483882Smlf 		    (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
4158d483882Smlf 			nflags = DA_ADD|DA_CD;
4168d483882Smlf 		}
4178d483882Smlf 	}
4188d483882Smlf 
4198d483882Smlf 	if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
4208d483882Smlf 		devfsadm_print(disk_mid, "Reserved link exists. Not "
4218d483882Smlf 		    "creating links for slice %s\n", slice);
4228d483882Smlf 		return;
4238d483882Smlf 	}
4248d483882Smlf 
4257c478bd9Sstevel@tonic-gate 	if (NULL == (ctrl = diskctrl(node, minor)))
4267c478bd9Sstevel@tonic-gate 		return;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, dir);
4297c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, "/c");
4307c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, ctrl);
4317c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, disk);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * If switching between SMI and EFI label or vice versa
4357c478bd9Sstevel@tonic-gate 	 * cleanup the previous label's devlinks.
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
4387c478bd9Sstevel@tonic-gate 		char *s, tpath[PATH_MAX + 1];
4397c478bd9Sstevel@tonic-gate 		struct stat sb;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		s = l_path + strlen(l_path);
4427c478bd9Sstevel@tonic-gate 		(void) strcat(l_path, (*mn == *(MN_SMI))
4437c478bd9Sstevel@tonic-gate 		    ? SLICE_EFI : SLICE_SMI);
4447c478bd9Sstevel@tonic-gate 		/*
4457c478bd9Sstevel@tonic-gate 		 * Attempt the remove only if the stale link exists
4467c478bd9Sstevel@tonic-gate 		 */
4477c478bd9Sstevel@tonic-gate 		(void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
4487c478bd9Sstevel@tonic-gate 		    devfsadm_root_path(), l_path);
4497c478bd9Sstevel@tonic-gate 		if (lstat(tpath, &sb) != -1)
4507c478bd9Sstevel@tonic-gate 			devfsadm_rm_all(l_path);
4517c478bd9Sstevel@tonic-gate 		*s = '\0';
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, slice);
4547c478bd9Sstevel@tonic-gate 
45545916cd2Sjpk 	(void) devfsadm_mklink(l_path, node, minor, nflags);
4567c478bd9Sstevel@tonic-gate 
45718c2aff7Sartem 	/* secondary links for removable and hotpluggable devices */
45818c2aff7Sartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
45918c2aff7Sartem 	    &int_prop) >= 0) {
46018c2aff7Sartem 		(void) strcpy(sec_path, "removable-media/");
46118c2aff7Sartem 		(void) strcat(sec_path, l_path);
46218c2aff7Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
4637544909dSartem 	}
4647544909dSartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
46518c2aff7Sartem 	    &int_prop) >= 0) {
46618c2aff7Sartem 		(void) strcpy(sec_path, "hotpluggable/");
46718c2aff7Sartem 		(void) strcat(sec_path, l_path);
46818c2aff7Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
46918c2aff7Sartem 	}
47018c2aff7Sartem 
4717c478bd9Sstevel@tonic-gate 	if ((flags & RM_STALE) == RM_STALE) {
4727c478bd9Sstevel@tonic-gate 		(void) strcpy(stale_re, "^");
4737c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, dir);
4747c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, "/c");
4757c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, ctrl);
4767c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
4777c478bd9Sstevel@tonic-gate 		/*
4787c478bd9Sstevel@tonic-gate 		 * optimizations are made inside of devfsadm_rm_stale_links
4797c478bd9Sstevel@tonic-gate 		 * instead of before calling the function, as it always
4807c478bd9Sstevel@tonic-gate 		 * needs to add the valid link to the cache.
4817c478bd9Sstevel@tonic-gate 		 */
4827c478bd9Sstevel@tonic-gate 		devfsadm_rm_stale_links(stale_re, l_path, node, minor);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	free(ctrl);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
4907c478bd9Sstevel@tonic-gate #define	RULE_INDEX	0
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate static char *
4937c478bd9Sstevel@tonic-gate diskctrl(di_node_t node, di_minor_t minor)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
4967c478bd9Sstevel@tonic-gate 	char *devfspath;
4977c478bd9Sstevel@tonic-gate 	char *buf, *mn;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
5007c478bd9Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
5017c478bd9Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
5027c478bd9Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
5037c478bd9Sstevel@tonic-gate 	};
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
5087c478bd9Sstevel@tonic-gate 		return (NULL);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
5117c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
5127c478bd9Sstevel@tonic-gate 	(void) strcat(path, mn);
5137c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/*
5167c478bd9Sstevel@tonic-gate 	 * Use controller component of disk path
5177c478bd9Sstevel@tonic-gate 	 */
5187c478bd9Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
5197c478bd9Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		/*
5227c478bd9Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
5237c478bd9Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
5247c478bd9Sstevel@tonic-gate 		 * name also in the match it should fix this and only find one
5257c478bd9Sstevel@tonic-gate 		 * logical controller. (See 4045879).
5267c478bd9Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
5277c478bd9Sstevel@tonic-gate 		 * no unique controller number for them in this case.
5287c478bd9Sstevel@tonic-gate 		 *
5297c478bd9Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
5307c478bd9Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
5317c478bd9Sstevel@tonic-gate 		 */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
5347c478bd9Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
5357c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
5367c478bd9Sstevel@tonic-gate 			return (NULL);
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	return (buf);
5417c478bd9Sstevel@tonic-gate }
5428d483882Smlf 
5438d483882Smlf typedef struct dvlist {
5448d483882Smlf 	char *dv_link;
5458d483882Smlf 	struct dvlist *dv_next;
5468d483882Smlf } dvlist_t;
5478d483882Smlf 
5488d483882Smlf static void
5498d483882Smlf free_dvlist(dvlist_t **pp)
5508d483882Smlf {
5518d483882Smlf 	dvlist_t *entry;
5528d483882Smlf 
5538d483882Smlf 	while (*pp) {
5548d483882Smlf 		entry = *pp;
5558d483882Smlf 		*pp = entry->dv_next;
5568d483882Smlf 		assert(entry->dv_link);
5578d483882Smlf 		free(entry->dv_link);
5588d483882Smlf 		free(entry);
5598d483882Smlf 	}
5608d483882Smlf }
5618d483882Smlf static int
5628d483882Smlf dvlink_cb(di_devlink_t devlink, void *arg)
5638d483882Smlf {
5648d483882Smlf 	char *path;
5658d483882Smlf 	char *can_path;
5668d483882Smlf 	dvlist_t **pp = (dvlist_t **)arg;
5678d483882Smlf 	dvlist_t *entry = NULL;
5688d483882Smlf 
5698d483882Smlf 	entry = calloc(1, sizeof (dvlist_t));
5708d483882Smlf 	if (entry == NULL) {
5718d483882Smlf 		devfsadm_errprint("%s: calloc failed\n", modname);
5728d483882Smlf 		goto error;
5738d483882Smlf 	}
5748d483882Smlf 
5758d483882Smlf 	path = (char *)di_devlink_path(devlink);
5768d483882Smlf 	assert(path);
5778d483882Smlf 	if (path == NULL) {
5788d483882Smlf 		devfsadm_errprint("%s: di_devlink_path() returned NULL\n",
5798d483882Smlf 		    modname);
5808d483882Smlf 		goto error;
5818d483882Smlf 	}
5828d483882Smlf 
5838d483882Smlf 	devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n",
5848d483882Smlf 	    modname, path);
5858d483882Smlf 
5868d483882Smlf 	/*
5878d483882Smlf 	 * Return linkname in canonical form i.e. without the
5888d483882Smlf 	 * "/dev/" prefix
5898d483882Smlf 	 */
5908d483882Smlf 	can_path = strstr(path, "/dev/");
5918d483882Smlf 	if (can_path == NULL) {
5928d483882Smlf 		devfsadm_errprint("%s: devlink path %s has no /dev/\n",
5938d483882Smlf 		    modname, path);
5948d483882Smlf 		goto error;
5958d483882Smlf 	}
5968d483882Smlf 
5978d483882Smlf 	entry->dv_link = s_strdup(can_path + strlen("/dev/"));
5988d483882Smlf 	entry->dv_next = *pp;
5998d483882Smlf 	*pp = entry;
6008d483882Smlf 
6018d483882Smlf 	return (DI_WALK_CONTINUE);
6028d483882Smlf 
6038d483882Smlf error:
6048d483882Smlf 	free(entry);
6058d483882Smlf 	free_dvlist(pp);
6068d483882Smlf 	*pp = NULL;
6078d483882Smlf 	return (DI_WALK_TERMINATE);
6088d483882Smlf }
6098d483882Smlf 
6108d483882Smlf /*
6118d483882Smlf  * Returns success only if all goes well. If there is no matching reserved link
6128d483882Smlf  * or if there is an error, we assume no match. It is better to err on the side
6138d483882Smlf  * of caution by creating extra links than to miss out creating a required link.
6148d483882Smlf  */
6158d483882Smlf static int
6168d483882Smlf reserved_links_exist(di_node_t node, di_minor_t minor, int nflags)
6178d483882Smlf {
6188d483882Smlf 	di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache();
6198d483882Smlf 	char phys_path[PATH_MAX];
6208d483882Smlf 	char *minor_path;
6218d483882Smlf 	dvlist_t *head;
6228d483882Smlf 	dvlist_t *entry;
6238d483882Smlf 	char *s;
6248d483882Smlf 	char l[PATH_MAX];
6258d483882Smlf 	int switch_link = 0;
6268d483882Smlf 	char *mn = di_minor_name(minor);
6278d483882Smlf 
6288d483882Smlf 	if (dvlink_cache == NULL || mn == NULL) {
6298d483882Smlf 		devfsadm_errprint("%s: No minor or devlink cache\n", modname);
6308d483882Smlf 		return (DEVFSADM_FAILURE);
6318d483882Smlf 	}
6328d483882Smlf 
633e37c6c37Scth 	if (!devfsadm_have_reserved()) {
634e37c6c37Scth 		devfsadm_print(disk_mid, "%s: No reserved links\n", modname);
6358d483882Smlf 		return (DEVFSADM_FAILURE);
6368d483882Smlf 	}
6378d483882Smlf 
6388d483882Smlf 	minor_path = di_devfs_minor_path(minor);
6398d483882Smlf 	if (minor_path == NULL) {
6408d483882Smlf 		devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname);
6418d483882Smlf 		return (DEVFSADM_FAILURE);
6428d483882Smlf 	}
6438d483882Smlf 
6448d483882Smlf 	(void) strlcpy(phys_path, minor_path, sizeof (phys_path));
6458d483882Smlf 
6468d483882Smlf 	di_devfs_path_free(minor_path);
6478d483882Smlf 
6488d483882Smlf 	head = NULL;
6498d483882Smlf 	(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path,
6508d483882Smlf 	    DI_PRIMARY_LINK, &head, dvlink_cb);
6518d483882Smlf 
6528d483882Smlf 	/*
6538d483882Smlf 	 * We may be switching between EFI label and SMI label in which case
6548d483882Smlf 	 * we only have minors of the other type.
6558d483882Smlf 	 */
6568d483882Smlf 	if (head == NULL && (*mn == *(MN_SMI) ||
6578d483882Smlf 	    (strncmp(mn, MN_EFI, 2) == 0))) {
6588d483882Smlf 		devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. "
6598d483882Smlf 		    "Trying another label\n", modname, mn);
6608d483882Smlf 		s = strrchr(phys_path, ':');
6618d483882Smlf 		if (s == NULL) {
6628d483882Smlf 			devfsadm_errprint("%s: invalid minor path: %s\n",
6638d483882Smlf 			    modname, phys_path);
6648d483882Smlf 			return (DEVFSADM_FAILURE);
6658d483882Smlf 		}
6668d483882Smlf 		(void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path),
667e37c6c37Scth 		    "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI,
668e37c6c37Scth 		    strstr(s, ",raw") ? ",raw" : "");
6698d483882Smlf 		(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE,
6708d483882Smlf 		    phys_path, DI_PRIMARY_LINK, &head, dvlink_cb);
6718d483882Smlf 	}
6728d483882Smlf 
6738d483882Smlf 	if (head == NULL) {
6748d483882Smlf 		devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n",
6758d483882Smlf 		    modname, phys_path);
6768d483882Smlf 		/* no links on disk */
6778d483882Smlf 		return (DEVFSADM_FAILURE);
6788d483882Smlf 	}
6798d483882Smlf 
6808d483882Smlf 	/*
6818d483882Smlf 	 * It suffices to use 1 link to this minor, since
6828d483882Smlf 	 * we are matching with reserved IDs on the basis of
6838d483882Smlf 	 * the controller number which will be the same for
6848d483882Smlf 	 * all links to this minor.
6858d483882Smlf 	 */
6868d483882Smlf 	if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) {
6878d483882Smlf 		/* not reserved links */
6888d483882Smlf 		devfsadm_print(disk_mid, "%s: devlink %s and its minor "
6898d483882Smlf 		    "are NOT reserved\n", modname, head->dv_link);
6908d483882Smlf 		free_dvlist(&head);
6918d483882Smlf 		return (DEVFSADM_FAILURE);
6928d483882Smlf 	}
6938d483882Smlf 
6948d483882Smlf 	devfsadm_print(disk_mid, "%s: devlink %s and its minor are on "
6958d483882Smlf 	    "reserved list\n", modname, head->dv_link);
6968d483882Smlf 
6978d483882Smlf 	/*
6988d483882Smlf 	 * Switch between SMI and EFI labels if required
6998d483882Smlf 	 */
7008d483882Smlf 	switch_link = 0;
7018d483882Smlf 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
7028d483882Smlf 		for (entry = head; entry; entry = entry->dv_next) {
7038d483882Smlf 			s = strrchr(entry->dv_link, '/');
7048d483882Smlf 			assert(s);
7058d483882Smlf 			if (s == NULL) {
7068d483882Smlf 				devfsadm_errprint("%s: disk link %s has no "
7078d483882Smlf 				    "directory\n", modname, entry->dv_link);
7088d483882Smlf 				continue;
7098d483882Smlf 			}
7108d483882Smlf 			if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) {
7118d483882Smlf 				(void) snprintf(l, sizeof (l), "%s%s",
7128d483882Smlf 				    entry->dv_link, SLICE_SMI);
7138d483882Smlf 				switch_link = 1;
7148d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
7158d483882Smlf 				    "reserved link from EFI to SMI label. "
7168d483882Smlf 				    "New link is %s\n", modname, l);
7178d483882Smlf 			} else if (strncmp(mn, MN_EFI, 2) == 0 &&
7188d483882Smlf 			    (s = strchr(s, 's'))) {
7198d483882Smlf 				*s = '\0';
7208d483882Smlf 				(void) snprintf(l, sizeof (l), "%s",
7218d483882Smlf 				    entry->dv_link);
7228d483882Smlf 				*s = 's';
7238d483882Smlf 				switch_link = 1;
7248d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
7258d483882Smlf 				    "reserved link from SMI to EFI label. "
7268d483882Smlf 				    "New link is %s\n", modname, l);
7278d483882Smlf 			}
7288d483882Smlf 			if (switch_link) {
7298d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
7308d483882Smlf 				    "link: deleting %s and creating %s\n",
7318d483882Smlf 				    modname, entry->dv_link, l);
7328d483882Smlf 				devfsadm_rm_link(entry->dv_link);
7338d483882Smlf 				(void) devfsadm_mklink(l, node, minor, nflags);
7348d483882Smlf 			}
7358d483882Smlf 		}
7368d483882Smlf 	}
7378d483882Smlf 	free_dvlist(&head);
7388d483882Smlf 
7398d483882Smlf 	/*
7408d483882Smlf 	 * return SUCCESS to indicate that new links to this minor should not
7418d483882Smlf 	 * be created so that only compatibility links to this minor remain.
7428d483882Smlf 	 */
7438d483882Smlf 	return (DEVFSADM_SUCCESS);
7448d483882Smlf }
745