xref: /titanic_50/usr/src/cmd/devfsadm/disk_link.c (revision 82bff4367ead41db97d1e0a2e52483a9f64bebaf)
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>
324c06356bSdh142964 #include <sys/int_fmtio.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
3445916cd2Sjpk #include <bsm/devalloc.h>
354c06356bSdh142964 #include <sys/scsi/scsi_address.h>
364c06356bSdh142964 #include <sys/libdevid.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	DISK_SUBPATH_MAX 100
397c478bd9Sstevel@tonic-gate #define	RM_STALE 0x01
407c478bd9Sstevel@tonic-gate #define	DISK_LINK_RE	"^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
417c478bd9Sstevel@tonic-gate #define	DISK_LINK_TO_UPPER(ch)\
427c478bd9Sstevel@tonic-gate 	(((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	SLICE_SMI	"s7"
457c478bd9Sstevel@tonic-gate #define	SLICE_EFI	""
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	MN_SMI		"h"
487c478bd9Sstevel@tonic-gate #define	MN_EFI		"wd"
497c478bd9Sstevel@tonic-gate #define	ASCIIWWNSIZE	255
50aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
51aa1b14e7SSheshadri Vasudevan /*
52aa1b14e7SSheshadri Vasudevan  * The number of minor nodes per LUN is defined by the disk drivers.
53aa1b14e7SSheshadri Vasudevan  * Currently it is set to 64. Refer CMLBUNIT_SHIFT (cmlb_impl.h)
54aa1b14e7SSheshadri Vasudevan  */
55aa1b14e7SSheshadri Vasudevan #define	NUM_MINORS_PER_INSTANCE	64
56aa1b14e7SSheshadri Vasudevan #endif
57aa1b14e7SSheshadri Vasudevan 
587c478bd9Sstevel@tonic-gate 
5945916cd2Sjpk extern int system_labeled;
6045916cd2Sjpk 
617c478bd9Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node);
627c478bd9Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node);
637c478bd9Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node);
6497869ac5Sjhd static int disk_callback_xvmd(di_minor_t minor, di_node_t node);
657c478bd9Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node);
66d88e498aSjiang wu - Sun Microsystems - Beijing China static int disk_callback_sas(di_minor_t minor, di_node_t node);
677c478bd9Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk,
687c478bd9Sstevel@tonic-gate 				int flags);
697c478bd9Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor);
708d483882Smlf static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = {
744c06356bSdh142964 	{ "disk", DDI_NT_BLOCK, NULL,
757c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
767c478bd9Sstevel@tonic-gate 	},
774c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_CHAN, NULL,
787c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
797c478bd9Sstevel@tonic-gate 	},
804c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_FABRIC, NULL,
817c478bd9Sstevel@tonic-gate 		TYPE_EXACT, ILEVEL_0, disk_callback_fabric
827c478bd9Sstevel@tonic-gate 	},
834c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_WWN, NULL,
847c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_wwn
857c478bd9Sstevel@tonic-gate 	},
864c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_SAS, NULL,
87d88e498aSjiang wu - Sun Microsystems - Beijing China 	    TYPE_EXACT, ILEVEL_0, disk_callback_sas
88d88e498aSjiang wu - Sun Microsystems - Beijing China 	},
894c06356bSdh142964 	{ "disk", DDI_NT_CD, NULL,
907c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
917c478bd9Sstevel@tonic-gate 	},
924c06356bSdh142964 	{ "disk", DDI_NT_CD_CHAN, NULL,
937c478bd9Sstevel@tonic-gate 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
947c478bd9Sstevel@tonic-gate 	},
954c06356bSdh142964 	{ "disk", DDI_NT_BLOCK_XVMD, NULL,
9697869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
9797869ac5Sjhd 	},
984c06356bSdh142964 	{ "disk", DDI_NT_CD_XVMD, NULL,
9997869ac5Sjhd 	    TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
10097869ac5Sjhd 	},
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * HOT auto cleanup of disks not desired.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = {
1097c478bd9Sstevel@tonic-gate 	{ "disk", DISK_LINK_RE, RM_POST,
1107c478bd9Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
1157c478bd9Sstevel@tonic-gate 
1168d483882Smlf static devlink_re_t disks_re_array[] = {
1178d483882Smlf 	{"^r?dsk/c([0-9]+)", 1},
1188d483882Smlf 	{"^cfg/c([0-9]+)$", 1},
1198d483882Smlf 	{"^scsi/.+/c([0-9]+)", 1},
1208d483882Smlf 	{NULL}
1218d483882Smlf };
1228d483882Smlf 
1238d483882Smlf static char *disk_mid = "disk_mid";
1248d483882Smlf static char *modname = "disk_link";
1258d483882Smlf 
1268d483882Smlf int
1278d483882Smlf minor_init()
1288d483882Smlf {
1298d483882Smlf 	devfsadm_print(disk_mid,
1308d483882Smlf 	    "%s: minor_init(): Creating disks reserved ID cache\n",
1318d483882Smlf 	    modname);
1328d483882Smlf 	return (devfsadm_reserve_id_cache(disks_re_array, NULL));
1338d483882Smlf }
1348d483882Smlf 
1357c478bd9Sstevel@tonic-gate static int
1367c478bd9Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	char *addr;
1397c478bd9Sstevel@tonic-gate 	char disk[20];
1407c478bd9Sstevel@tonic-gate 	uint_t targ;
1417c478bd9Sstevel@tonic-gate 	uint_t lun;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	addr = di_bus_addr(node);
1447c478bd9Sstevel@tonic-gate 	(void) sscanf(addr, "%X,%X", &targ, &lun);
1457c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1467c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1477c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static int
1527c478bd9Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	char *addr;
1557c478bd9Sstevel@tonic-gate 	char disk[10];
1567c478bd9Sstevel@tonic-gate 	uint_t lun;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	addr = di_bus_addr(node);
1597c478bd9Sstevel@tonic-gate 	(void) sscanf(addr, "%X", &lun);
1607c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "d%d", lun);
1617c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, 0);
1627c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static int
1677c478bd9Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	char disk[10];
1707c478bd9Sstevel@tonic-gate 	int lun;
1717c478bd9Sstevel@tonic-gate 	int targ;
1727c478bd9Sstevel@tonic-gate 	int *intp;
1737c478bd9Sstevel@tonic-gate 
1744c06356bSdh142964 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_TARGET,
1754c06356bSdh142964 	    &intp) <= 0) {
1767c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	targ = *intp;
1794c06356bSdh142964 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_LUN,
1804c06356bSdh142964 	    &intp) <= 0) {
1817c478bd9Sstevel@tonic-gate 		lun = 0;
1827c478bd9Sstevel@tonic-gate 	} else {
1837c478bd9Sstevel@tonic-gate 		lun = *intp;
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	(void) sprintf(disk, "t%dd%d", targ, lun);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static int
1937c478bd9Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	char disk[DISK_SUBPATH_MAX];
1967c478bd9Sstevel@tonic-gate 	int lun;
1977c478bd9Sstevel@tonic-gate 	int count;
1987c478bd9Sstevel@tonic-gate 	int *intp;
1997c478bd9Sstevel@tonic-gate 	uchar_t *str;
2007c478bd9Sstevel@tonic-gate 	uchar_t *wwn;
2017c478bd9Sstevel@tonic-gate 	uchar_t ascii_wwn[ASCIIWWNSIZE];
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2047c478bd9Sstevel@tonic-gate 	    "client-guid", (char **)&wwn) > 0) {
205e37c6c37Scth 		if (strlcpy((char *)ascii_wwn, (char *)wwn,
206e37c6c37Scth 		    sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) {
2077c478bd9Sstevel@tonic-gate 			devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
2087c478bd9Sstevel@tonic-gate 			    strlen((char *)wwn));
2097c478bd9Sstevel@tonic-gate 			return (DEVFSADM_CONTINUE);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 		lun = 0;
2127c478bd9Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
2137c478bd9Sstevel@tonic-gate 	    "port-wwn", &wwn) > 0) {
2147c478bd9Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2154c06356bSdh142964 		    SCSI_ADDR_PROP_LUN, &intp) > 0) {
2167c478bd9Sstevel@tonic-gate 			lun = *intp;
2177c478bd9Sstevel@tonic-gate 		} else {
2187c478bd9Sstevel@tonic-gate 			lun = 0;
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
2227c478bd9Sstevel@tonic-gate 			(void) sprintf((caddr_t)str, "%02x", wwn[count]);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 		*str = '\0';
2257c478bd9Sstevel@tonic-gate 	} else {
2267c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	for (str = ascii_wwn; *str != '\0'; str++) {
2307c478bd9Sstevel@tonic-gate 		*str = DISK_LINK_TO_UPPER(*str);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	disk_common(minor, node, disk, RM_STALE);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
240d88e498aSjiang wu - Sun Microsystems - Beijing China static int
241d88e498aSjiang wu - Sun Microsystems - Beijing China disk_callback_sas(di_minor_t minor, di_node_t node)
242d88e498aSjiang wu - Sun Microsystems - Beijing China {
243d88e498aSjiang wu - Sun Microsystems - Beijing China 	char disk[DISK_SUBPATH_MAX];
2444c06356bSdh142964 	int lun64_found = 0;
2454c06356bSdh142964 	scsi_lun64_t lun64, sl;
2464c06356bSdh142964 	scsi_lun_t lun;
2474c06356bSdh142964 	int64_t *lun64p;
2484c06356bSdh142964 	uint64_t wwn;
249d88e498aSjiang wu - Sun Microsystems - Beijing China 	int *intp;
2504c06356bSdh142964 	char *tgt_port;
2514c06356bSdh142964 	uchar_t addr_method;
252d88e498aSjiang wu - Sun Microsystems - Beijing China 
2534c06356bSdh142964 	/* Get lun property */
2544c06356bSdh142964 	if (di_prop_lookup_int64(DDI_DEV_T_ANY, node,
2554c06356bSdh142964 	    SCSI_ADDR_PROP_LUN64, &lun64p) > 0) {
2564c06356bSdh142964 		if (*lun64p != SCSI_LUN64_ILLEGAL) {
2574c06356bSdh142964 			lun64_found = 1;
2584c06356bSdh142964 			lun64 = (uint64_t)*lun64p;
259d88e498aSjiang wu - Sun Microsystems - Beijing China 		}
2604c06356bSdh142964 	}
2614c06356bSdh142964 	if ((!lun64_found) && (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2624c06356bSdh142964 	    SCSI_ADDR_PROP_LUN, &intp) > 0)) {
2634c06356bSdh142964 		lun64 = (uint64_t)*intp;
2644c06356bSdh142964 	}
2654c06356bSdh142964 
2664c06356bSdh142964 	lun = scsi_lun64_to_lun(lun64);
2674c06356bSdh142964 
2684c06356bSdh142964 	addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
2694c06356bSdh142964 
270d88e498aSjiang wu - Sun Microsystems - Beijing China 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2714c06356bSdh142964 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
2724c06356bSdh142964 		(void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
2734c06356bSdh142964 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
2744c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
2754c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
2764c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
2774c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
2784c06356bSdh142964 			    "t%"PRIX64"d%"PRId64, wwn, lun64);
2794c06356bSdh142964 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
2804c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
2814c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
2824c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
2834c06356bSdh142964 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
2844c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
2854c06356bSdh142964 			    "t%"PRIX64"d%"PRIX16, wwn, sl);
2864c06356bSdh142964 		} else {
2874c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
2884c06356bSdh142964 			    "t%"PRIX64"d%"PRIX64, wwn, lun64);
289d88e498aSjiang wu - Sun Microsystems - Beijing China 		}
290d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2914c06356bSdh142964 	    SCSI_ADDR_PROP_SATA_PHY, &intp) > 0) {
2924c06356bSdh142964 		/* Use phy format naming, for SATA devices without wwn */
2934c06356bSdh142964 		if ((addr_method == SCSI_LUN_AM_PDEV) &&
2944c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
2954c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
2964c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
2974c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
298*82bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRId64, *intp, lun64);
2994c06356bSdh142964 		} else if ((addr_method == SCSI_LUN_AM_FLAT) &&
3004c06356bSdh142964 		    (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3014c06356bSdh142964 		    (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3024c06356bSdh142964 		    (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3034c06356bSdh142964 			sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
3044c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
305*82bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRIX16, *intp, sl);
3064c06356bSdh142964 		} else {
3074c06356bSdh142964 			(void) snprintf(disk, DISK_SUBPATH_MAX,
308*82bff436Sbo zhou - Sun Microsystems - Beijing China 			    "t%dd%"PRIX64, *intp, lun64);
3094c06356bSdh142964 		}
310d88e498aSjiang wu - Sun Microsystems - Beijing China 	} else {
311d88e498aSjiang wu - Sun Microsystems - Beijing China 		return (DEVFSADM_CONTINUE);
312d88e498aSjiang wu - Sun Microsystems - Beijing China 	}
313d88e498aSjiang wu - Sun Microsystems - Beijing China 
314d88e498aSjiang wu - Sun Microsystems - Beijing China 	disk_common(minor, node, disk, RM_STALE);
315d88e498aSjiang wu - Sun Microsystems - Beijing China 
316d88e498aSjiang wu - Sun Microsystems - Beijing China 	return (DEVFSADM_CONTINUE);
317d88e498aSjiang wu - Sun Microsystems - Beijing China }
318d88e498aSjiang wu - Sun Microsystems - Beijing China 
3197c478bd9Sstevel@tonic-gate /*
32097869ac5Sjhd  * xVM virtual block device
32197869ac5Sjhd  *
32297869ac5Sjhd  * VBDs are enumerated into xenstore by xend and named using
32397869ac5Sjhd  * the linux dev_t values for 'hd' and 'xvd' devices.  Linux
32497869ac5Sjhd  * dev_t's are 16-bit values.  The upper 8 bits identify the major #
32597869ac5Sjhd  * of the device (hd, xvd) and the lower 8 bits the instance and partition
32697869ac5Sjhd  *
32797869ac5Sjhd  * For PV guests, VBDs are named by the virt-tools using
32897869ac5Sjhd  * the form xvd[a-p][1-15].  The corresponding Solaris /dev/dsk name
32997869ac5Sjhd  * created by this generator will be c0t[0-15]d[0-15]sN,
33097869ac5Sjhd  * were the target (t) value represents [a-p] and the
33197869ac5Sjhd  * disk (d) value is either 0 (e.g. xvda) or contains the partition
33297869ac5Sjhd  * information if it has been specified [1-15] (e.g. xvda1)
33397869ac5Sjhd  *
33497869ac5Sjhd  * For PV guests using the legacy naming (0, 1, 2, ...)
33597869ac5Sjhd  * the Solaris disk names created will be c0d[0..767]sN
33697869ac5Sjhd  * The Solaris version of virt-install based on virtinst.101
33797869ac5Sjhd  * named PV disks as sequential integers.  With virtinst.300_1 and
33897869ac5Sjhd  * beyond, the virt-* tools will no longer create legacy disk
33997869ac5Sjhd  * names.
34097869ac5Sjhd  */
34197869ac5Sjhd static int
34297869ac5Sjhd disk_callback_xvmd(di_minor_t minor, di_node_t node)
34397869ac5Sjhd {
34497869ac5Sjhd #define	HD_BASE (3 << 8)
34597869ac5Sjhd #define	XVBDMAJ 202
34697869ac5Sjhd 
34797869ac5Sjhd 	char *addr;
34897869ac5Sjhd 	char disk[16];
34997869ac5Sjhd 	uint_t targ;
35097869ac5Sjhd 	uint_t lun = 0;
35197869ac5Sjhd 	uint_t fmaj;
35297869ac5Sjhd 
35397869ac5Sjhd 	addr = di_bus_addr(node);
35497869ac5Sjhd 	targ = strtol(addr, (char **)NULL, 10);
35597869ac5Sjhd 	fmaj = targ >> 8;
35697869ac5Sjhd 
35797869ac5Sjhd 	/* legacy device address */
35897869ac5Sjhd 	if (targ < HD_BASE)
35997869ac5Sjhd 		(void) snprintf(disk, sizeof (disk),  "d%d", targ);
36097869ac5Sjhd 	/* PV VBD */
36197869ac5Sjhd 	else if (fmaj == XVBDMAJ) {
36297869ac5Sjhd 		lun = targ & 0xf;
36397869ac5Sjhd 		targ = (targ & 0xff) >> 4;
36497869ac5Sjhd 		(void) snprintf(disk, sizeof (disk), "t%dd%d", targ, lun);
36597869ac5Sjhd 	/* HVM device names are generated using the standard generator */
36697869ac5Sjhd 	} else {
36797869ac5Sjhd 		devfsadm_errprint("%s: invalid disk device number (%s)\n",
36897869ac5Sjhd 		    modname, addr);
36997869ac5Sjhd 		return (DEVFSADM_CONTINUE);
37097869ac5Sjhd 	}
37197869ac5Sjhd 	disk_common(minor, node, disk, 0);
37297869ac5Sjhd 	return (DEVFSADM_CONTINUE);
37397869ac5Sjhd 
37497869ac5Sjhd }
37597869ac5Sjhd 
37697869ac5Sjhd /*
3777c478bd9Sstevel@tonic-gate  * This function is called for every disk minor node.
3787c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical controller number, and
3797c478bd9Sstevel@tonic-gate  * then devfsadm_mklink to make the link.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate static void
3827c478bd9Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
38518c2aff7Sartem 	char sec_path[PATH_MAX + 1];
3867c478bd9Sstevel@tonic-gate 	char stale_re[DISK_SUBPATH_MAX];
3877c478bd9Sstevel@tonic-gate 	char *dir;
3887c478bd9Sstevel@tonic-gate 	char slice[4];
3897c478bd9Sstevel@tonic-gate 	char *mn;
3907c478bd9Sstevel@tonic-gate 	char *ctrl;
39145916cd2Sjpk 	char *nt = NULL;
39218c2aff7Sartem 	int *int_prop;
39345916cd2Sjpk 	int  nflags = 0;
394aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
395aa1b14e7SSheshadri Vasudevan 	char mn_copy[4];
396aa1b14e7SSheshadri Vasudevan 	char *part;
397aa1b14e7SSheshadri Vasudevan 	int part_num;
398aa1b14e7SSheshadri Vasudevan #endif
3997c478bd9Sstevel@tonic-gate 
400aa1b14e7SSheshadri Vasudevan 	mn = di_minor_name(minor);
401aa1b14e7SSheshadri Vasudevan 	if (strstr(mn, ",raw")) {
4027c478bd9Sstevel@tonic-gate 		dir = "rdsk";
403aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
404aa1b14e7SSheshadri Vasudevan 		(void) strncpy(mn_copy, mn, 4);
405aa1b14e7SSheshadri Vasudevan 		part = strtok(mn_copy, ",");
406aa1b14e7SSheshadri Vasudevan #endif
4077c478bd9Sstevel@tonic-gate 	} else {
4087c478bd9Sstevel@tonic-gate 		dir = "dsk";
409aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
410aa1b14e7SSheshadri Vasudevan 		part = mn;
411aa1b14e7SSheshadri Vasudevan #endif
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
414aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
415aa1b14e7SSheshadri Vasudevan 	/*
416aa1b14e7SSheshadri Vasudevan 	 * The following is a table describing the allocation of
417aa1b14e7SSheshadri Vasudevan 	 * minor numbers, minor names and /dev/dsk names for partitions
418aa1b14e7SSheshadri Vasudevan 	 * and slices on x86 systems.
419aa1b14e7SSheshadri Vasudevan 	 *
420aa1b14e7SSheshadri Vasudevan 	 *	Minor Number	Minor Name	/dev/dsk name
421aa1b14e7SSheshadri Vasudevan 	 *	---------------------------------------------
422aa1b14e7SSheshadri Vasudevan 	 *	0 to 15		"a" to "p"	s0 to s15
423aa1b14e7SSheshadri Vasudevan 	 *	16		"q"		p0
424aa1b14e7SSheshadri Vasudevan 	 *	17 to 20	"r" to "u"	p1 to p4
425aa1b14e7SSheshadri Vasudevan 	 *	21 to 52	"p5" to "p36"	p5 to p36
426aa1b14e7SSheshadri Vasudevan 	 *
427aa1b14e7SSheshadri Vasudevan 	 */
428aa1b14e7SSheshadri Vasudevan 	part_num = atoi(part + 1);
429aa1b14e7SSheshadri Vasudevan 
430aa1b14e7SSheshadri Vasudevan 	if ((mn[0] == 'p') && (part_num >= 5)) {
431aa1b14e7SSheshadri Vasudevan 		/* logical drive */
432aa1b14e7SSheshadri Vasudevan 		(void) snprintf(slice, 4, "%s", part);
433aa1b14e7SSheshadri Vasudevan 	} else {
434aa1b14e7SSheshadri Vasudevan #endif
435aa1b14e7SSheshadri Vasudevan 	if (mn[0] < 'q') {
4367c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "s%d", mn[0] - 'a');
4377c478bd9Sstevel@tonic-gate 	} else if (strncmp(mn, MN_EFI, 2) != 0) {
4387c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, "p%d", mn[0] - 'q');
4397c478bd9Sstevel@tonic-gate 	} else {
4407c478bd9Sstevel@tonic-gate 		/* For EFI label */
4417c478bd9Sstevel@tonic-gate 		(void) sprintf(slice, SLICE_EFI);
4427c478bd9Sstevel@tonic-gate 	}
443aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
444aa1b14e7SSheshadri Vasudevan 	}
445aa1b14e7SSheshadri Vasudevan #endif
4467c478bd9Sstevel@tonic-gate 
4478d483882Smlf 	nflags = 0;
4488d483882Smlf 	if (system_labeled) {
4498d483882Smlf 		nt = di_minor_nodetype(minor);
4508d483882Smlf 		if ((nt != NULL) &&
4518d483882Smlf 		    ((strcmp(nt, DDI_NT_CD) == 0) ||
4528d483882Smlf 		    (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
4538d483882Smlf 		    (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
4548d483882Smlf 			nflags = DA_ADD|DA_CD;
4558d483882Smlf 		}
4568d483882Smlf 	}
4578d483882Smlf 
4588d483882Smlf 	if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
4598d483882Smlf 		devfsadm_print(disk_mid, "Reserved link exists. Not "
4608d483882Smlf 		    "creating links for slice %s\n", slice);
4618d483882Smlf 		return;
4628d483882Smlf 	}
4638d483882Smlf 
4647c478bd9Sstevel@tonic-gate 	if (NULL == (ctrl = diskctrl(node, minor)))
4657c478bd9Sstevel@tonic-gate 		return;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, dir);
4687c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, "/c");
4697c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, ctrl);
4707c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, disk);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/*
4737c478bd9Sstevel@tonic-gate 	 * If switching between SMI and EFI label or vice versa
4747c478bd9Sstevel@tonic-gate 	 * cleanup the previous label's devlinks.
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
4777c478bd9Sstevel@tonic-gate 		char *s, tpath[PATH_MAX + 1];
4787c478bd9Sstevel@tonic-gate 		struct stat sb;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		s = l_path + strlen(l_path);
4817c478bd9Sstevel@tonic-gate 		(void) strcat(l_path, (*mn == *(MN_SMI))
4827c478bd9Sstevel@tonic-gate 		    ? SLICE_EFI : SLICE_SMI);
4837c478bd9Sstevel@tonic-gate 		/*
4847c478bd9Sstevel@tonic-gate 		 * Attempt the remove only if the stale link exists
4857c478bd9Sstevel@tonic-gate 		 */
4867c478bd9Sstevel@tonic-gate 		(void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
4877c478bd9Sstevel@tonic-gate 		    devfsadm_root_path(), l_path);
4887c478bd9Sstevel@tonic-gate 		if (lstat(tpath, &sb) != -1)
4897c478bd9Sstevel@tonic-gate 			devfsadm_rm_all(l_path);
4907c478bd9Sstevel@tonic-gate 		*s = '\0';
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, slice);
4937c478bd9Sstevel@tonic-gate 
49445916cd2Sjpk 	(void) devfsadm_mklink(l_path, node, minor, nflags);
4957c478bd9Sstevel@tonic-gate 
49618c2aff7Sartem 	/* secondary links for removable and hotpluggable devices */
49718c2aff7Sartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
49818c2aff7Sartem 	    &int_prop) >= 0) {
49918c2aff7Sartem 		(void) strcpy(sec_path, "removable-media/");
50018c2aff7Sartem 		(void) strcat(sec_path, l_path);
50118c2aff7Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
5027544909dSartem 	}
5037544909dSartem 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
50418c2aff7Sartem 	    &int_prop) >= 0) {
50518c2aff7Sartem 		(void) strcpy(sec_path, "hotpluggable/");
50618c2aff7Sartem 		(void) strcat(sec_path, l_path);
50718c2aff7Sartem 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
50818c2aff7Sartem 	}
50918c2aff7Sartem 
5107c478bd9Sstevel@tonic-gate 	if ((flags & RM_STALE) == RM_STALE) {
5117c478bd9Sstevel@tonic-gate 		(void) strcpy(stale_re, "^");
5127c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, dir);
5137c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, "/c");
5147c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, ctrl);
5157c478bd9Sstevel@tonic-gate 		(void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
5167c478bd9Sstevel@tonic-gate 		/*
5177c478bd9Sstevel@tonic-gate 		 * optimizations are made inside of devfsadm_rm_stale_links
5187c478bd9Sstevel@tonic-gate 		 * instead of before calling the function, as it always
5197c478bd9Sstevel@tonic-gate 		 * needs to add the valid link to the cache.
5207c478bd9Sstevel@tonic-gate 		 */
5217c478bd9Sstevel@tonic-gate 		devfsadm_rm_stale_links(stale_re, l_path, node, minor);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	free(ctrl);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
5297c478bd9Sstevel@tonic-gate #define	RULE_INDEX	0
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate static char *
5327c478bd9Sstevel@tonic-gate diskctrl(di_node_t node, di_minor_t minor)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
5357c478bd9Sstevel@tonic-gate 	char *devfspath;
5367c478bd9Sstevel@tonic-gate 	char *buf, *mn;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
5397c478bd9Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
5407c478bd9Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
5417c478bd9Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
5427c478bd9Sstevel@tonic-gate 	};
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
5477c478bd9Sstevel@tonic-gate 		return (NULL);
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
5507c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
5517c478bd9Sstevel@tonic-gate 	(void) strcat(path, mn);
5527c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Use controller component of disk path
5567c478bd9Sstevel@tonic-gate 	 */
5577c478bd9Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
5587c478bd9Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		/*
5617c478bd9Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
5627c478bd9Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
5637c478bd9Sstevel@tonic-gate 		 * name also in the match it should fix this and only find one
5647c478bd9Sstevel@tonic-gate 		 * logical controller. (See 4045879).
5657c478bd9Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
5667c478bd9Sstevel@tonic-gate 		 * no unique controller number for them in this case.
5677c478bd9Sstevel@tonic-gate 		 *
5687c478bd9Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
5697c478bd9Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
5707c478bd9Sstevel@tonic-gate 		 */
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
5737c478bd9Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
5747c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
5757c478bd9Sstevel@tonic-gate 			return (NULL);
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	return (buf);
5807c478bd9Sstevel@tonic-gate }
5818d483882Smlf 
5828d483882Smlf typedef struct dvlist {
5838d483882Smlf 	char *dv_link;
5848d483882Smlf 	struct dvlist *dv_next;
5858d483882Smlf } dvlist_t;
5868d483882Smlf 
5878d483882Smlf static void
5888d483882Smlf free_dvlist(dvlist_t **pp)
5898d483882Smlf {
5908d483882Smlf 	dvlist_t *entry;
5918d483882Smlf 
5928d483882Smlf 	while (*pp) {
5938d483882Smlf 		entry = *pp;
5948d483882Smlf 		*pp = entry->dv_next;
5958d483882Smlf 		assert(entry->dv_link);
5968d483882Smlf 		free(entry->dv_link);
5978d483882Smlf 		free(entry);
5988d483882Smlf 	}
5998d483882Smlf }
6008d483882Smlf static int
6018d483882Smlf dvlink_cb(di_devlink_t devlink, void *arg)
6028d483882Smlf {
6038d483882Smlf 	char *path;
6048d483882Smlf 	char *can_path;
6058d483882Smlf 	dvlist_t **pp = (dvlist_t **)arg;
6068d483882Smlf 	dvlist_t *entry = NULL;
6078d483882Smlf 
6088d483882Smlf 	entry = calloc(1, sizeof (dvlist_t));
6098d483882Smlf 	if (entry == NULL) {
6108d483882Smlf 		devfsadm_errprint("%s: calloc failed\n", modname);
6118d483882Smlf 		goto error;
6128d483882Smlf 	}
6138d483882Smlf 
6148d483882Smlf 	path = (char *)di_devlink_path(devlink);
6158d483882Smlf 	assert(path);
6168d483882Smlf 	if (path == NULL) {
6178d483882Smlf 		devfsadm_errprint("%s: di_devlink_path() returned NULL\n",
6188d483882Smlf 		    modname);
6198d483882Smlf 		goto error;
6208d483882Smlf 	}
6218d483882Smlf 
6228d483882Smlf 	devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n",
6238d483882Smlf 	    modname, path);
6248d483882Smlf 
6258d483882Smlf 	/*
6268d483882Smlf 	 * Return linkname in canonical form i.e. without the
6278d483882Smlf 	 * "/dev/" prefix
6288d483882Smlf 	 */
6298d483882Smlf 	can_path = strstr(path, "/dev/");
6308d483882Smlf 	if (can_path == NULL) {
6318d483882Smlf 		devfsadm_errprint("%s: devlink path %s has no /dev/\n",
6328d483882Smlf 		    modname, path);
6338d483882Smlf 		goto error;
6348d483882Smlf 	}
6358d483882Smlf 
6368d483882Smlf 	entry->dv_link = s_strdup(can_path + strlen("/dev/"));
6378d483882Smlf 	entry->dv_next = *pp;
6388d483882Smlf 	*pp = entry;
6398d483882Smlf 
6408d483882Smlf 	return (DI_WALK_CONTINUE);
6418d483882Smlf 
6428d483882Smlf error:
6438d483882Smlf 	free(entry);
6448d483882Smlf 	free_dvlist(pp);
6458d483882Smlf 	*pp = NULL;
6468d483882Smlf 	return (DI_WALK_TERMINATE);
6478d483882Smlf }
6488d483882Smlf 
6498d483882Smlf /*
6508d483882Smlf  * Returns success only if all goes well. If there is no matching reserved link
6518d483882Smlf  * or if there is an error, we assume no match. It is better to err on the side
6528d483882Smlf  * of caution by creating extra links than to miss out creating a required link.
6538d483882Smlf  */
6548d483882Smlf static int
6558d483882Smlf reserved_links_exist(di_node_t node, di_minor_t minor, int nflags)
6568d483882Smlf {
6578d483882Smlf 	di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache();
6588d483882Smlf 	char phys_path[PATH_MAX];
6598d483882Smlf 	char *minor_path;
6608d483882Smlf 	dvlist_t *head;
6618d483882Smlf 	dvlist_t *entry;
6628d483882Smlf 	char *s;
6638d483882Smlf 	char l[PATH_MAX];
6648d483882Smlf 	int switch_link = 0;
6658d483882Smlf 	char *mn = di_minor_name(minor);
6668d483882Smlf 
6678d483882Smlf 	if (dvlink_cache == NULL || mn == NULL) {
6688d483882Smlf 		devfsadm_errprint("%s: No minor or devlink cache\n", modname);
6698d483882Smlf 		return (DEVFSADM_FAILURE);
6708d483882Smlf 	}
6718d483882Smlf 
672e37c6c37Scth 	if (!devfsadm_have_reserved()) {
673e37c6c37Scth 		devfsadm_print(disk_mid, "%s: No reserved links\n", modname);
6748d483882Smlf 		return (DEVFSADM_FAILURE);
6758d483882Smlf 	}
6768d483882Smlf 
6778d483882Smlf 	minor_path = di_devfs_minor_path(minor);
6788d483882Smlf 	if (minor_path == NULL) {
6798d483882Smlf 		devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname);
6808d483882Smlf 		return (DEVFSADM_FAILURE);
6818d483882Smlf 	}
6828d483882Smlf 
6838d483882Smlf 	(void) strlcpy(phys_path, minor_path, sizeof (phys_path));
6848d483882Smlf 
6858d483882Smlf 	di_devfs_path_free(minor_path);
6868d483882Smlf 
6878d483882Smlf 	head = NULL;
6888d483882Smlf 	(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path,
6898d483882Smlf 	    DI_PRIMARY_LINK, &head, dvlink_cb);
6908d483882Smlf 
6918d483882Smlf 	/*
6928d483882Smlf 	 * We may be switching between EFI label and SMI label in which case
6938d483882Smlf 	 * we only have minors of the other type.
6948d483882Smlf 	 */
6958d483882Smlf 	if (head == NULL && (*mn == *(MN_SMI) ||
6968d483882Smlf 	    (strncmp(mn, MN_EFI, 2) == 0))) {
6978d483882Smlf 		devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. "
6988d483882Smlf 		    "Trying another label\n", modname, mn);
6998d483882Smlf 		s = strrchr(phys_path, ':');
7008d483882Smlf 		if (s == NULL) {
7018d483882Smlf 			devfsadm_errprint("%s: invalid minor path: %s\n",
7028d483882Smlf 			    modname, phys_path);
7038d483882Smlf 			return (DEVFSADM_FAILURE);
7048d483882Smlf 		}
7058d483882Smlf 		(void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path),
7068d483882Smlf 		    "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI,
7078d483882Smlf 		    strstr(s, ",raw") ? ",raw" : "");
7088d483882Smlf 		(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE,
7098d483882Smlf 		    phys_path, DI_PRIMARY_LINK, &head, dvlink_cb);
7108d483882Smlf 	}
7118d483882Smlf 
7128d483882Smlf 	if (head == NULL) {
7138d483882Smlf 		devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n",
7148d483882Smlf 		    modname, phys_path);
7158d483882Smlf 		/* no links on disk */
7168d483882Smlf 		return (DEVFSADM_FAILURE);
7178d483882Smlf 	}
7188d483882Smlf 
7198d483882Smlf 	/*
7208d483882Smlf 	 * It suffices to use 1 link to this minor, since
7218d483882Smlf 	 * we are matching with reserved IDs on the basis of
7228d483882Smlf 	 * the controller number which will be the same for
7238d483882Smlf 	 * all links to this minor.
7248d483882Smlf 	 */
7258d483882Smlf 	if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) {
7268d483882Smlf 		/* not reserved links */
7278d483882Smlf 		devfsadm_print(disk_mid, "%s: devlink %s and its minor "
7288d483882Smlf 		    "are NOT reserved\n", modname, head->dv_link);
7298d483882Smlf 		free_dvlist(&head);
7308d483882Smlf 		return (DEVFSADM_FAILURE);
7318d483882Smlf 	}
7328d483882Smlf 
7338d483882Smlf 	devfsadm_print(disk_mid, "%s: devlink %s and its minor are on "
7348d483882Smlf 	    "reserved list\n", modname, head->dv_link);
7358d483882Smlf 
7368d483882Smlf 	/*
7378d483882Smlf 	 * Switch between SMI and EFI labels if required
7388d483882Smlf 	 */
7398d483882Smlf 	switch_link = 0;
7408d483882Smlf 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
7418d483882Smlf 		for (entry = head; entry; entry = entry->dv_next) {
7428d483882Smlf 			s = strrchr(entry->dv_link, '/');
7438d483882Smlf 			assert(s);
7448d483882Smlf 			if (s == NULL) {
7458d483882Smlf 				devfsadm_errprint("%s: disk link %s has no "
7468d483882Smlf 				    "directory\n", modname, entry->dv_link);
7478d483882Smlf 				continue;
7488d483882Smlf 			}
7498d483882Smlf 			if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) {
7508d483882Smlf 				(void) snprintf(l, sizeof (l), "%s%s",
7518d483882Smlf 				    entry->dv_link, SLICE_SMI);
7528d483882Smlf 				switch_link = 1;
7538d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
7548d483882Smlf 				    "reserved link from EFI to SMI label. "
7558d483882Smlf 				    "New link is %s\n", modname, l);
7568d483882Smlf 			} else if (strncmp(mn, MN_EFI, 2) == 0 &&
7578d483882Smlf 			    (s = strchr(s, 's'))) {
7588d483882Smlf 				*s = '\0';
7598d483882Smlf 				(void) snprintf(l, sizeof (l), "%s",
7608d483882Smlf 				    entry->dv_link);
7618d483882Smlf 				*s = 's';
7628d483882Smlf 				switch_link = 1;
7638d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
7648d483882Smlf 				    "reserved link from SMI to EFI label. "
7658d483882Smlf 				    "New link is %s\n", modname, l);
7668d483882Smlf 			}
7678d483882Smlf 			if (switch_link) {
7688d483882Smlf 				devfsadm_print(disk_mid, "%s: switching "
7698d483882Smlf 				    "link: deleting %s and creating %s\n",
7708d483882Smlf 				    modname, entry->dv_link, l);
7718d483882Smlf 				devfsadm_rm_link(entry->dv_link);
7728d483882Smlf 				(void) devfsadm_mklink(l, node, minor, nflags);
7738d483882Smlf 			}
7748d483882Smlf 		}
7758d483882Smlf 	}
7768d483882Smlf 	free_dvlist(&head);
7778d483882Smlf 
7788d483882Smlf 	/*
7798d483882Smlf 	 * return SUCCESS to indicate that new links to this minor should not
7808d483882Smlf 	 * be created so that only compatibility links to this minor remain.
7818d483882Smlf 	 */
7828d483882Smlf 	return (DEVFSADM_SUCCESS);
7838d483882Smlf }
784