17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*fa27e351SHans Rosenfeld * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
23d88e498aSjiang wu - Sun Microsystems - Beijing China * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <devfsadm.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
3297869ac5Sjhd #include <ctype.h>
334c06356bSdh142964 #include <sys/int_fmtio.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
3545916cd2Sjpk #include <bsm/devalloc.h>
364c06356bSdh142964 #include <sys/scsi/scsi_address.h>
374c06356bSdh142964 #include <sys/libdevid.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #define DISK_SUBPATH_MAX 100
407c478bd9Sstevel@tonic-gate #define RM_STALE 0x01
417c478bd9Sstevel@tonic-gate #define DISK_LINK_RE "^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
427c478bd9Sstevel@tonic-gate #define DISK_LINK_TO_UPPER(ch)\
437c478bd9Sstevel@tonic-gate (((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #define SLICE_SMI "s7"
467c478bd9Sstevel@tonic-gate #define SLICE_EFI ""
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define MN_SMI "h"
497c478bd9Sstevel@tonic-gate #define MN_EFI "wd"
507c478bd9Sstevel@tonic-gate #define ASCIIWWNSIZE 255
51aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
52aa1b14e7SSheshadri Vasudevan /*
53aa1b14e7SSheshadri Vasudevan * The number of minor nodes per LUN is defined by the disk drivers.
54aa1b14e7SSheshadri Vasudevan * Currently it is set to 64. Refer CMLBUNIT_SHIFT (cmlb_impl.h)
55aa1b14e7SSheshadri Vasudevan */
56aa1b14e7SSheshadri Vasudevan #define NUM_MINORS_PER_INSTANCE 64
57aa1b14e7SSheshadri Vasudevan #endif
58aa1b14e7SSheshadri Vasudevan
597c478bd9Sstevel@tonic-gate
6045916cd2Sjpk extern int system_labeled;
6145916cd2Sjpk
627c478bd9Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node);
637c478bd9Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node);
64*fa27e351SHans Rosenfeld static int disk_callback_blkdev(di_minor_t minor, di_node_t node);
657c478bd9Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node);
6697869ac5Sjhd static int disk_callback_xvmd(di_minor_t minor, di_node_t node);
677c478bd9Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node);
68d88e498aSjiang wu - Sun Microsystems - Beijing China static int disk_callback_sas(di_minor_t minor, di_node_t node);
697c478bd9Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk,
707c478bd9Sstevel@tonic-gate int flags);
717c478bd9Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor);
728d483882Smlf static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = {
764c06356bSdh142964 { "disk", DDI_NT_BLOCK, NULL,
777c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_nchan
787c478bd9Sstevel@tonic-gate },
794c06356bSdh142964 { "disk", DDI_NT_BLOCK_CHAN, NULL,
807c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_chan
817c478bd9Sstevel@tonic-gate },
82*fa27e351SHans Rosenfeld { "disk", DDI_NT_BLOCK_BLKDEV, NULL,
83*fa27e351SHans Rosenfeld TYPE_EXACT, ILEVEL_0, disk_callback_blkdev
84*fa27e351SHans Rosenfeld },
854c06356bSdh142964 { "disk", DDI_NT_BLOCK_FABRIC, NULL,
867c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_fabric
877c478bd9Sstevel@tonic-gate },
884c06356bSdh142964 { "disk", DDI_NT_BLOCK_WWN, NULL,
897c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_wwn
907c478bd9Sstevel@tonic-gate },
914c06356bSdh142964 { "disk", DDI_NT_BLOCK_SAS, NULL,
92d88e498aSjiang wu - Sun Microsystems - Beijing China TYPE_EXACT, ILEVEL_0, disk_callback_sas
93d88e498aSjiang wu - Sun Microsystems - Beijing China },
944c06356bSdh142964 { "disk", DDI_NT_CD, NULL,
957c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_nchan
967c478bd9Sstevel@tonic-gate },
974c06356bSdh142964 { "disk", DDI_NT_CD_CHAN, NULL,
987c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_chan
997c478bd9Sstevel@tonic-gate },
1004c06356bSdh142964 { "disk", DDI_NT_BLOCK_XVMD, NULL,
10197869ac5Sjhd TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
10297869ac5Sjhd },
1034c06356bSdh142964 { "disk", DDI_NT_CD_XVMD, NULL,
10497869ac5Sjhd TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
10597869ac5Sjhd },
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt);
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * HOT auto cleanup of disks not desired.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = {
1147c478bd9Sstevel@tonic-gate { "disk", DISK_LINK_RE, RM_POST,
1157c478bd9Sstevel@tonic-gate ILEVEL_0, devfsadm_rm_all
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
1207c478bd9Sstevel@tonic-gate
1218d483882Smlf static devlink_re_t disks_re_array[] = {
1228d483882Smlf {"^r?dsk/c([0-9]+)", 1},
1238d483882Smlf {"^cfg/c([0-9]+)$", 1},
1248d483882Smlf {"^scsi/.+/c([0-9]+)", 1},
1258d483882Smlf {NULL}
1268d483882Smlf };
1278d483882Smlf
1288d483882Smlf static char *disk_mid = "disk_mid";
1298d483882Smlf static char *modname = "disk_link";
1308d483882Smlf
1318d483882Smlf int
minor_init()1328d483882Smlf minor_init()
1338d483882Smlf {
1348d483882Smlf devfsadm_print(disk_mid,
1358d483882Smlf "%s: minor_init(): Creating disks reserved ID cache\n",
1368d483882Smlf modname);
1378d483882Smlf return (devfsadm_reserve_id_cache(disks_re_array, NULL));
1388d483882Smlf }
1398d483882Smlf
1407c478bd9Sstevel@tonic-gate static int
disk_callback_chan(di_minor_t minor,di_node_t node)1417c478bd9Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate char *addr;
1447c478bd9Sstevel@tonic-gate char disk[20];
1457c478bd9Sstevel@tonic-gate uint_t targ;
1467c478bd9Sstevel@tonic-gate uint_t lun;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate addr = di_bus_addr(node);
1497c478bd9Sstevel@tonic-gate (void) sscanf(addr, "%X,%X", &targ, &lun);
1507c478bd9Sstevel@tonic-gate (void) sprintf(disk, "t%dd%d", targ, lun);
1517c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, 0);
1527c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate static int
disk_callback_nchan(di_minor_t minor,di_node_t node)1577c478bd9Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate char *addr;
1607c478bd9Sstevel@tonic-gate char disk[10];
1617c478bd9Sstevel@tonic-gate uint_t lun;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate addr = di_bus_addr(node);
1647c478bd9Sstevel@tonic-gate (void) sscanf(addr, "%X", &lun);
1657c478bd9Sstevel@tonic-gate (void) sprintf(disk, "d%d", lun);
1667c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, 0);
1677c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate static int
disk_callback_blkdev(di_minor_t minor,di_node_t node)172*fa27e351SHans Rosenfeld disk_callback_blkdev(di_minor_t minor, di_node_t node)
173*fa27e351SHans Rosenfeld {
174*fa27e351SHans Rosenfeld char *addr;
175*fa27e351SHans Rosenfeld char disk[DISK_SUBPATH_MAX];
176*fa27e351SHans Rosenfeld uint64_t eui64;
177*fa27e351SHans Rosenfeld uint_t lun = 0;
178*fa27e351SHans Rosenfeld
179*fa27e351SHans Rosenfeld addr = di_bus_addr(node);
180*fa27e351SHans Rosenfeld (void) sscanf(addr, "w%016"PRIx64",%X", &eui64, &lun);
181*fa27e351SHans Rosenfeld (void) snprintf(disk, DISK_SUBPATH_MAX, "t%016"PRIX64"d%d", eui64, lun);
182*fa27e351SHans Rosenfeld disk_common(minor, node, disk, RM_STALE);
183*fa27e351SHans Rosenfeld return (DEVFSADM_CONTINUE);
184*fa27e351SHans Rosenfeld }
185*fa27e351SHans Rosenfeld
186*fa27e351SHans Rosenfeld static int
disk_callback_wwn(di_minor_t minor,di_node_t node)1877c478bd9Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate char disk[10];
1907c478bd9Sstevel@tonic-gate int lun;
1917c478bd9Sstevel@tonic-gate int targ;
1927c478bd9Sstevel@tonic-gate int *intp;
1937c478bd9Sstevel@tonic-gate
1944c06356bSdh142964 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_TARGET,
1954c06356bSdh142964 &intp) <= 0) {
1967c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate targ = *intp;
1994c06356bSdh142964 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_LUN,
2004c06356bSdh142964 &intp) <= 0) {
2017c478bd9Sstevel@tonic-gate lun = 0;
2027c478bd9Sstevel@tonic-gate } else {
2037c478bd9Sstevel@tonic-gate lun = *intp;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate (void) sprintf(disk, "t%dd%d", targ, lun);
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, RM_STALE);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate static int
disk_callback_fabric(di_minor_t minor,di_node_t node)2137c478bd9Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate char disk[DISK_SUBPATH_MAX];
2167c478bd9Sstevel@tonic-gate int lun;
2177c478bd9Sstevel@tonic-gate int count;
2187c478bd9Sstevel@tonic-gate int *intp;
2197c478bd9Sstevel@tonic-gate uchar_t *str;
2207c478bd9Sstevel@tonic-gate uchar_t *wwn;
2217c478bd9Sstevel@tonic-gate uchar_t ascii_wwn[ASCIIWWNSIZE];
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2247c478bd9Sstevel@tonic-gate "client-guid", (char **)&wwn) > 0) {
225e37c6c37Scth if (strlcpy((char *)ascii_wwn, (char *)wwn,
226e37c6c37Scth sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) {
2277c478bd9Sstevel@tonic-gate devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
2287c478bd9Sstevel@tonic-gate strlen((char *)wwn));
2297c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate lun = 0;
2327c478bd9Sstevel@tonic-gate } else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
2337c478bd9Sstevel@tonic-gate "port-wwn", &wwn) > 0) {
2347c478bd9Sstevel@tonic-gate if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2354c06356bSdh142964 SCSI_ADDR_PROP_LUN, &intp) > 0) {
2367c478bd9Sstevel@tonic-gate lun = *intp;
2377c478bd9Sstevel@tonic-gate } else {
2387c478bd9Sstevel@tonic-gate lun = 0;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
2427c478bd9Sstevel@tonic-gate (void) sprintf((caddr_t)str, "%02x", wwn[count]);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate *str = '\0';
2457c478bd9Sstevel@tonic-gate } else {
2467c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate for (str = ascii_wwn; *str != '\0'; str++) {
2507c478bd9Sstevel@tonic-gate *str = DISK_LINK_TO_UPPER(*str);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate (void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, RM_STALE);
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate
260d88e498aSjiang wu - Sun Microsystems - Beijing China static int
disk_callback_sas(di_minor_t minor,di_node_t node)261d88e498aSjiang wu - Sun Microsystems - Beijing China disk_callback_sas(di_minor_t minor, di_node_t node)
262d88e498aSjiang wu - Sun Microsystems - Beijing China {
263d88e498aSjiang wu - Sun Microsystems - Beijing China char disk[DISK_SUBPATH_MAX];
2644c06356bSdh142964 int lun64_found = 0;
2654c06356bSdh142964 scsi_lun64_t lun64, sl;
2664c06356bSdh142964 scsi_lun_t lun;
2674c06356bSdh142964 int64_t *lun64p;
2684c06356bSdh142964 uint64_t wwn;
269d88e498aSjiang wu - Sun Microsystems - Beijing China int *intp;
2704c06356bSdh142964 char *tgt_port;
2714c06356bSdh142964 uchar_t addr_method;
272d88e498aSjiang wu - Sun Microsystems - Beijing China
2734c06356bSdh142964 /* Get lun property */
2744c06356bSdh142964 if (di_prop_lookup_int64(DDI_DEV_T_ANY, node,
2754c06356bSdh142964 SCSI_ADDR_PROP_LUN64, &lun64p) > 0) {
2764c06356bSdh142964 if (*lun64p != SCSI_LUN64_ILLEGAL) {
2774c06356bSdh142964 lun64_found = 1;
2784c06356bSdh142964 lun64 = (uint64_t)*lun64p;
279d88e498aSjiang wu - Sun Microsystems - Beijing China }
2804c06356bSdh142964 }
2814c06356bSdh142964 if ((!lun64_found) && (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
2824c06356bSdh142964 SCSI_ADDR_PROP_LUN, &intp) > 0)) {
2834c06356bSdh142964 lun64 = (uint64_t)*intp;
2844c06356bSdh142964 }
2854c06356bSdh142964
2864c06356bSdh142964 lun = scsi_lun64_to_lun(lun64);
2874c06356bSdh142964
2884c06356bSdh142964 addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
2894c06356bSdh142964
290d88e498aSjiang wu - Sun Microsystems - Beijing China if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
2914c06356bSdh142964 SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
2924c06356bSdh142964 (void) scsi_wwnstr_to_wwn(tgt_port, &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,
2984c06356bSdh142964 "t%"PRIX64"d%"PRId64, wwn, 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,
3054c06356bSdh142964 "t%"PRIX64"d%"PRIX16, wwn, sl);
3064c06356bSdh142964 } else {
3074c06356bSdh142964 (void) snprintf(disk, DISK_SUBPATH_MAX,
3084c06356bSdh142964 "t%"PRIX64"d%"PRIX64, wwn, lun64);
309d88e498aSjiang wu - Sun Microsystems - Beijing China }
310d88e498aSjiang wu - Sun Microsystems - Beijing China } else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
3114c06356bSdh142964 SCSI_ADDR_PROP_SATA_PHY, &intp) > 0) {
3124c06356bSdh142964 /* Use phy format naming, for SATA devices without wwn */
3134c06356bSdh142964 if ((addr_method == SCSI_LUN_AM_PDEV) &&
3144c06356bSdh142964 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3154c06356bSdh142964 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3164c06356bSdh142964 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3174c06356bSdh142964 (void) snprintf(disk, DISK_SUBPATH_MAX,
31882bff436Sbo zhou - Sun Microsystems - Beijing China "t%dd%"PRId64, *intp, lun64);
3194c06356bSdh142964 } else if ((addr_method == SCSI_LUN_AM_FLAT) &&
3204c06356bSdh142964 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
3214c06356bSdh142964 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
3224c06356bSdh142964 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
3234c06356bSdh142964 sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
3244c06356bSdh142964 (void) snprintf(disk, DISK_SUBPATH_MAX,
32582bff436Sbo zhou - Sun Microsystems - Beijing China "t%dd%"PRIX16, *intp, sl);
3264c06356bSdh142964 } else {
3274c06356bSdh142964 (void) snprintf(disk, DISK_SUBPATH_MAX,
32882bff436Sbo zhou - Sun Microsystems - Beijing China "t%dd%"PRIX64, *intp, lun64);
3294c06356bSdh142964 }
330d88e498aSjiang wu - Sun Microsystems - Beijing China } else {
331d88e498aSjiang wu - Sun Microsystems - Beijing China return (DEVFSADM_CONTINUE);
332d88e498aSjiang wu - Sun Microsystems - Beijing China }
333d88e498aSjiang wu - Sun Microsystems - Beijing China
334d88e498aSjiang wu - Sun Microsystems - Beijing China disk_common(minor, node, disk, RM_STALE);
335d88e498aSjiang wu - Sun Microsystems - Beijing China
336d88e498aSjiang wu - Sun Microsystems - Beijing China return (DEVFSADM_CONTINUE);
337d88e498aSjiang wu - Sun Microsystems - Beijing China }
338d88e498aSjiang wu - Sun Microsystems - Beijing China
3397c478bd9Sstevel@tonic-gate /*
34097869ac5Sjhd * xVM virtual block device
34197869ac5Sjhd *
342efc44fd7SVitaliy Gusev * Xen passes device number in next format:
34397869ac5Sjhd *
344efc44fd7SVitaliy Gusev * 1 << 28 | disk << 8 | partition xvd, disks or partitions 16 onwards
345efc44fd7SVitaliy Gusev * 202 << 8 | disk << 4 | partition xvd, disks and partitions up to 15
346efc44fd7SVitaliy Gusev * 8 << 8 | disk << 4 | partition sd, disks and partitions up to 15
347efc44fd7SVitaliy Gusev * 3 << 8 | disk << 6 | partition hd, disks 0..1, partitions 0..63
348efc44fd7SVitaliy Gusev * 22 << 8 | (disk-2) << 6 | partition hd, disks 2..3, partitions 0..63
349efc44fd7SVitaliy Gusev * 2 << 28 onwards reserved for future use
350efc44fd7SVitaliy Gusev * other values less than 1 << 28 deprecated / reserved
351efc44fd7SVitaliy Gusev *
352efc44fd7SVitaliy Gusev * The corresponding Solaris /dev/dsk name can be:
353efc44fd7SVitaliy Gusev *
354efc44fd7SVitaliy Gusev * c0tYdXsN
355efc44fd7SVitaliy Gusev *
356efc44fd7SVitaliy Gusev * where Y,X >= 0.
35797869ac5Sjhd *
35897869ac5Sjhd * For PV guests using the legacy naming (0, 1, 2, ...)
35997869ac5Sjhd * the Solaris disk names created will be c0d[0..767]sN
36097869ac5Sjhd */
361efc44fd7SVitaliy Gusev
362efc44fd7SVitaliy Gusev #define HD_BASE (3 << 8)
363efc44fd7SVitaliy Gusev #define XEN_EXT_SHIFT (28)
364efc44fd7SVitaliy Gusev
365efc44fd7SVitaliy Gusev /*
366efc44fd7SVitaliy Gusev * Return: Number of parsed and written parameters
367efc44fd7SVitaliy Gusev */
368efc44fd7SVitaliy Gusev static int
decode_xen_device(uint_t device,uint_t * disk,uint_t * plun)369efc44fd7SVitaliy Gusev decode_xen_device(uint_t device, uint_t *disk, uint_t *plun)
370efc44fd7SVitaliy Gusev {
371efc44fd7SVitaliy Gusev uint_t dsk, lun = 0;
372efc44fd7SVitaliy Gusev int ret = 1;
373efc44fd7SVitaliy Gusev
374efc44fd7SVitaliy Gusev if ((device >> XEN_EXT_SHIFT) > 1)
375efc44fd7SVitaliy Gusev return (0);
376efc44fd7SVitaliy Gusev
377efc44fd7SVitaliy Gusev if (device < HD_BASE) {
378efc44fd7SVitaliy Gusev /* legacy device address */
379efc44fd7SVitaliy Gusev dsk = device;
380efc44fd7SVitaliy Gusev goto end;
381efc44fd7SVitaliy Gusev }
382efc44fd7SVitaliy Gusev
383efc44fd7SVitaliy Gusev ret = 2;
384efc44fd7SVitaliy Gusev if (device & (1 << XEN_EXT_SHIFT)) {
385efc44fd7SVitaliy Gusev /* extended */
386efc44fd7SVitaliy Gusev dsk = device & (~0xff);
387efc44fd7SVitaliy Gusev lun = device & 0xff;
388efc44fd7SVitaliy Gusev goto end;
389efc44fd7SVitaliy Gusev }
390efc44fd7SVitaliy Gusev
391efc44fd7SVitaliy Gusev switch (device >> 8) {
392efc44fd7SVitaliy Gusev case 202: /* xvd */
393efc44fd7SVitaliy Gusev dsk = (device >> 4) & 0xf;
394efc44fd7SVitaliy Gusev lun = device & 0xf;
395efc44fd7SVitaliy Gusev break;
396efc44fd7SVitaliy Gusev case 8: /* sd */
397efc44fd7SVitaliy Gusev dsk = device & (~0xf);
398efc44fd7SVitaliy Gusev lun = device & 0xf;
399efc44fd7SVitaliy Gusev break;
400efc44fd7SVitaliy Gusev case 3: /* hd, disk 0..1 */
401efc44fd7SVitaliy Gusev dsk = device & (~0x3f);
402efc44fd7SVitaliy Gusev lun = device & 0x3f;
403efc44fd7SVitaliy Gusev break;
404efc44fd7SVitaliy Gusev case 22: /* hd, disk 2..3 */
405efc44fd7SVitaliy Gusev dsk = device & (~0x3f);
406efc44fd7SVitaliy Gusev lun = device & 0x3f;
407efc44fd7SVitaliy Gusev break;
408efc44fd7SVitaliy Gusev default:
409efc44fd7SVitaliy Gusev return (0);
410efc44fd7SVitaliy Gusev }
411efc44fd7SVitaliy Gusev end:
412efc44fd7SVitaliy Gusev *disk = dsk;
413efc44fd7SVitaliy Gusev *plun = lun;
414efc44fd7SVitaliy Gusev return (ret);
415efc44fd7SVitaliy Gusev }
416efc44fd7SVitaliy Gusev
41797869ac5Sjhd static int
disk_callback_xvmd(di_minor_t minor,di_node_t node)41897869ac5Sjhd disk_callback_xvmd(di_minor_t minor, di_node_t node)
41997869ac5Sjhd {
42097869ac5Sjhd char *addr;
42197869ac5Sjhd char disk[16];
42297869ac5Sjhd uint_t targ;
423efc44fd7SVitaliy Gusev uint_t dsk, lun;
424efc44fd7SVitaliy Gusev int res;
42597869ac5Sjhd
42697869ac5Sjhd addr = di_bus_addr(node);
42797869ac5Sjhd targ = strtol(addr, (char **)NULL, 10);
42897869ac5Sjhd
429efc44fd7SVitaliy Gusev res = decode_xen_device(targ, &dsk, &lun);
430efc44fd7SVitaliy Gusev
43197869ac5Sjhd /* HVM device names are generated using the standard generator */
432efc44fd7SVitaliy Gusev
433efc44fd7SVitaliy Gusev if (res == 1)
434efc44fd7SVitaliy Gusev (void) snprintf(disk, sizeof (disk), "d%d", dsk);
435efc44fd7SVitaliy Gusev else if (res == 2)
436efc44fd7SVitaliy Gusev (void) snprintf(disk, sizeof (disk), "t%dd%d", dsk, lun);
437efc44fd7SVitaliy Gusev else {
43897869ac5Sjhd devfsadm_errprint("%s: invalid disk device number (%s)\n",
43997869ac5Sjhd modname, addr);
44097869ac5Sjhd return (DEVFSADM_CONTINUE);
44197869ac5Sjhd }
44297869ac5Sjhd disk_common(minor, node, disk, 0);
44397869ac5Sjhd return (DEVFSADM_CONTINUE);
44497869ac5Sjhd
44597869ac5Sjhd }
44697869ac5Sjhd
44797869ac5Sjhd /*
4487c478bd9Sstevel@tonic-gate * This function is called for every disk minor node.
4497c478bd9Sstevel@tonic-gate * Calls enumerate to assign a logical controller number, and
4507c478bd9Sstevel@tonic-gate * then devfsadm_mklink to make the link.
4517c478bd9Sstevel@tonic-gate */
4527c478bd9Sstevel@tonic-gate static void
disk_common(di_minor_t minor,di_node_t node,char * disk,int flags)4537c478bd9Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate char l_path[PATH_MAX + 1];
45618c2aff7Sartem char sec_path[PATH_MAX + 1];
4577c478bd9Sstevel@tonic-gate char stale_re[DISK_SUBPATH_MAX];
4587c478bd9Sstevel@tonic-gate char *dir;
4597c478bd9Sstevel@tonic-gate char slice[4];
4607c478bd9Sstevel@tonic-gate char *mn;
4617c478bd9Sstevel@tonic-gate char *ctrl;
46245916cd2Sjpk char *nt = NULL;
46318c2aff7Sartem int *int_prop;
46445916cd2Sjpk int nflags = 0;
465aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
466aa1b14e7SSheshadri Vasudevan char mn_copy[4];
467aa1b14e7SSheshadri Vasudevan char *part;
468aa1b14e7SSheshadri Vasudevan int part_num;
469aa1b14e7SSheshadri Vasudevan #endif
4707c478bd9Sstevel@tonic-gate
471aa1b14e7SSheshadri Vasudevan mn = di_minor_name(minor);
472aa1b14e7SSheshadri Vasudevan if (strstr(mn, ",raw")) {
4737c478bd9Sstevel@tonic-gate dir = "rdsk";
474aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
475aa1b14e7SSheshadri Vasudevan (void) strncpy(mn_copy, mn, 4);
476aa1b14e7SSheshadri Vasudevan part = strtok(mn_copy, ",");
477aa1b14e7SSheshadri Vasudevan #endif
4787c478bd9Sstevel@tonic-gate } else {
4797c478bd9Sstevel@tonic-gate dir = "dsk";
480aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
481aa1b14e7SSheshadri Vasudevan part = mn;
482aa1b14e7SSheshadri Vasudevan #endif
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate
485aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
486aa1b14e7SSheshadri Vasudevan /*
487aa1b14e7SSheshadri Vasudevan * The following is a table describing the allocation of
488aa1b14e7SSheshadri Vasudevan * minor numbers, minor names and /dev/dsk names for partitions
489aa1b14e7SSheshadri Vasudevan * and slices on x86 systems.
490aa1b14e7SSheshadri Vasudevan *
491aa1b14e7SSheshadri Vasudevan * Minor Number Minor Name /dev/dsk name
492aa1b14e7SSheshadri Vasudevan * ---------------------------------------------
493aa1b14e7SSheshadri Vasudevan * 0 to 15 "a" to "p" s0 to s15
494aa1b14e7SSheshadri Vasudevan * 16 "q" p0
495aa1b14e7SSheshadri Vasudevan * 17 to 20 "r" to "u" p1 to p4
496aa1b14e7SSheshadri Vasudevan * 21 to 52 "p5" to "p36" p5 to p36
497aa1b14e7SSheshadri Vasudevan *
498aa1b14e7SSheshadri Vasudevan */
499aa1b14e7SSheshadri Vasudevan part_num = atoi(part + 1);
500aa1b14e7SSheshadri Vasudevan
501aa1b14e7SSheshadri Vasudevan if ((mn[0] == 'p') && (part_num >= 5)) {
502aa1b14e7SSheshadri Vasudevan /* logical drive */
503aa1b14e7SSheshadri Vasudevan (void) snprintf(slice, 4, "%s", part);
504aa1b14e7SSheshadri Vasudevan } else {
505aa1b14e7SSheshadri Vasudevan #endif
506aa1b14e7SSheshadri Vasudevan if (mn[0] < 'q') {
5077c478bd9Sstevel@tonic-gate (void) sprintf(slice, "s%d", mn[0] - 'a');
5087c478bd9Sstevel@tonic-gate } else if (strncmp(mn, MN_EFI, 2) != 0) {
5097c478bd9Sstevel@tonic-gate (void) sprintf(slice, "p%d", mn[0] - 'q');
5107c478bd9Sstevel@tonic-gate } else {
5117c478bd9Sstevel@tonic-gate /* For EFI label */
5127c478bd9Sstevel@tonic-gate (void) sprintf(slice, SLICE_EFI);
5137c478bd9Sstevel@tonic-gate }
514aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
515aa1b14e7SSheshadri Vasudevan }
516aa1b14e7SSheshadri Vasudevan #endif
5177c478bd9Sstevel@tonic-gate
5188d483882Smlf nflags = 0;
5198d483882Smlf if (system_labeled) {
5208d483882Smlf nt = di_minor_nodetype(minor);
5218d483882Smlf if ((nt != NULL) &&
5228d483882Smlf ((strcmp(nt, DDI_NT_CD) == 0) ||
5238d483882Smlf (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
5248d483882Smlf (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
5258d483882Smlf nflags = DA_ADD|DA_CD;
5268d483882Smlf }
5278d483882Smlf }
5288d483882Smlf
5298d483882Smlf if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
5308d483882Smlf devfsadm_print(disk_mid, "Reserved link exists. Not "
5318d483882Smlf "creating links for slice %s\n", slice);
5328d483882Smlf return;
5338d483882Smlf }
5348d483882Smlf
5357c478bd9Sstevel@tonic-gate if (NULL == (ctrl = diskctrl(node, minor)))
5367c478bd9Sstevel@tonic-gate return;
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate (void) strcpy(l_path, dir);
5397c478bd9Sstevel@tonic-gate (void) strcat(l_path, "/c");
5407c478bd9Sstevel@tonic-gate (void) strcat(l_path, ctrl);
5417c478bd9Sstevel@tonic-gate (void) strcat(l_path, disk);
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate * If switching between SMI and EFI label or vice versa
5457c478bd9Sstevel@tonic-gate * cleanup the previous label's devlinks.
5467c478bd9Sstevel@tonic-gate */
5477c478bd9Sstevel@tonic-gate if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
5487c478bd9Sstevel@tonic-gate char *s, tpath[PATH_MAX + 1];
5497c478bd9Sstevel@tonic-gate struct stat sb;
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate s = l_path + strlen(l_path);
5527c478bd9Sstevel@tonic-gate (void) strcat(l_path, (*mn == *(MN_SMI))
5537c478bd9Sstevel@tonic-gate ? SLICE_EFI : SLICE_SMI);
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate * Attempt the remove only if the stale link exists
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate (void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
5587c478bd9Sstevel@tonic-gate devfsadm_root_path(), l_path);
5597c478bd9Sstevel@tonic-gate if (lstat(tpath, &sb) != -1)
5607c478bd9Sstevel@tonic-gate devfsadm_rm_all(l_path);
5617c478bd9Sstevel@tonic-gate *s = '\0';
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate (void) strcat(l_path, slice);
5647c478bd9Sstevel@tonic-gate
56545916cd2Sjpk (void) devfsadm_mklink(l_path, node, minor, nflags);
5667c478bd9Sstevel@tonic-gate
56718c2aff7Sartem /* secondary links for removable and hotpluggable devices */
56818c2aff7Sartem if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
56918c2aff7Sartem &int_prop) >= 0) {
57018c2aff7Sartem (void) strcpy(sec_path, "removable-media/");
57118c2aff7Sartem (void) strcat(sec_path, l_path);
57218c2aff7Sartem (void) devfsadm_secondary_link(sec_path, l_path, 0);
5737544909dSartem }
5747544909dSartem if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
57518c2aff7Sartem &int_prop) >= 0) {
57618c2aff7Sartem (void) strcpy(sec_path, "hotpluggable/");
57718c2aff7Sartem (void) strcat(sec_path, l_path);
57818c2aff7Sartem (void) devfsadm_secondary_link(sec_path, l_path, 0);
57918c2aff7Sartem }
58018c2aff7Sartem
5817c478bd9Sstevel@tonic-gate if ((flags & RM_STALE) == RM_STALE) {
5827c478bd9Sstevel@tonic-gate (void) strcpy(stale_re, "^");
5837c478bd9Sstevel@tonic-gate (void) strcat(stale_re, dir);
5847c478bd9Sstevel@tonic-gate (void) strcat(stale_re, "/c");
5857c478bd9Sstevel@tonic-gate (void) strcat(stale_re, ctrl);
5867c478bd9Sstevel@tonic-gate (void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * optimizations are made inside of devfsadm_rm_stale_links
5897c478bd9Sstevel@tonic-gate * instead of before calling the function, as it always
5907c478bd9Sstevel@tonic-gate * needs to add the valid link to the cache.
5917c478bd9Sstevel@tonic-gate */
5927c478bd9Sstevel@tonic-gate devfsadm_rm_stale_links(stale_re, l_path, node, minor);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate free(ctrl);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
6007c478bd9Sstevel@tonic-gate #define RULE_INDEX 0
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate static char *
diskctrl(di_node_t node,di_minor_t minor)6037c478bd9Sstevel@tonic-gate diskctrl(di_node_t node, di_minor_t minor)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate char path[PATH_MAX + 1];
6067c478bd9Sstevel@tonic-gate char *devfspath;
6077c478bd9Sstevel@tonic-gate char *buf, *mn;
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[3] = {
6107c478bd9Sstevel@tonic-gate {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
6117c478bd9Sstevel@tonic-gate {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
6127c478bd9Sstevel@tonic-gate {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
6137c478bd9Sstevel@tonic-gate };
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate mn = di_minor_name(minor);
6167c478bd9Sstevel@tonic-gate
6177c478bd9Sstevel@tonic-gate if ((devfspath = di_devfs_path(node)) == NULL) {
6187c478bd9Sstevel@tonic-gate return (NULL);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate (void) strcpy(path, devfspath);
6217c478bd9Sstevel@tonic-gate (void) strcat(path, ":");
6227c478bd9Sstevel@tonic-gate (void) strcat(path, mn);
6237c478bd9Sstevel@tonic-gate di_devfs_path_free(devfspath);
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate * Use controller component of disk path
6277c478bd9Sstevel@tonic-gate */
6287c478bd9Sstevel@tonic-gate if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
6297c478bd9Sstevel@tonic-gate DEVFSADM_MULTIPLE) {
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate /*
6327c478bd9Sstevel@tonic-gate * We failed because there are multiple logical controller
6337c478bd9Sstevel@tonic-gate * numbers for a single physical controller. If we use node
6347c478bd9Sstevel@tonic-gate * name also in the match it should fix this and only find one
6357c478bd9Sstevel@tonic-gate * logical controller. (See 4045879).
6367c478bd9Sstevel@tonic-gate * NOTE: Rules for controllers are not changed, as there is
6377c478bd9Sstevel@tonic-gate * no unique controller number for them in this case.
6387c478bd9Sstevel@tonic-gate *
6397c478bd9Sstevel@tonic-gate * MATCH_UNCACHED flag is private to the "disks" and "sgen"
6407c478bd9Sstevel@tonic-gate * modules. NOT to be used by other modules.
6417c478bd9Sstevel@tonic-gate */
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
6447c478bd9Sstevel@tonic-gate rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
6457c478bd9Sstevel@tonic-gate if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
6467c478bd9Sstevel@tonic-gate return (NULL);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate return (buf);
6517c478bd9Sstevel@tonic-gate }
6528d483882Smlf
6538d483882Smlf typedef struct dvlist {
6548d483882Smlf char *dv_link;
6558d483882Smlf struct dvlist *dv_next;
6568d483882Smlf } dvlist_t;
6578d483882Smlf
6588d483882Smlf static void
free_dvlist(dvlist_t ** pp)6598d483882Smlf free_dvlist(dvlist_t **pp)
6608d483882Smlf {
6618d483882Smlf dvlist_t *entry;
6628d483882Smlf
6638d483882Smlf while (*pp) {
6648d483882Smlf entry = *pp;
6658d483882Smlf *pp = entry->dv_next;
6668d483882Smlf assert(entry->dv_link);
6678d483882Smlf free(entry->dv_link);
6688d483882Smlf free(entry);
6698d483882Smlf }
6708d483882Smlf }
6718d483882Smlf static int
dvlink_cb(di_devlink_t devlink,void * arg)6728d483882Smlf dvlink_cb(di_devlink_t devlink, void *arg)
6738d483882Smlf {
6748d483882Smlf char *path;
6758d483882Smlf char *can_path;
6768d483882Smlf dvlist_t **pp = (dvlist_t **)arg;
6778d483882Smlf dvlist_t *entry = NULL;
6788d483882Smlf
6798d483882Smlf entry = calloc(1, sizeof (dvlist_t));
6808d483882Smlf if (entry == NULL) {
6818d483882Smlf devfsadm_errprint("%s: calloc failed\n", modname);
6828d483882Smlf goto error;
6838d483882Smlf }
6848d483882Smlf
6858d483882Smlf path = (char *)di_devlink_path(devlink);
6868d483882Smlf assert(path);
6878d483882Smlf if (path == NULL) {
6888d483882Smlf devfsadm_errprint("%s: di_devlink_path() returned NULL\n",
6898d483882Smlf modname);
6908d483882Smlf goto error;
6918d483882Smlf }
6928d483882Smlf
6938d483882Smlf devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n",
6948d483882Smlf modname, path);
6958d483882Smlf
6968d483882Smlf /*
6978d483882Smlf * Return linkname in canonical form i.e. without the
6988d483882Smlf * "/dev/" prefix
6998d483882Smlf */
7008d483882Smlf can_path = strstr(path, "/dev/");
7018d483882Smlf if (can_path == NULL) {
7028d483882Smlf devfsadm_errprint("%s: devlink path %s has no /dev/\n",
7038d483882Smlf modname, path);
7048d483882Smlf goto error;
7058d483882Smlf }
7068d483882Smlf
7078d483882Smlf entry->dv_link = s_strdup(can_path + strlen("/dev/"));
7088d483882Smlf entry->dv_next = *pp;
7098d483882Smlf *pp = entry;
7108d483882Smlf
7118d483882Smlf return (DI_WALK_CONTINUE);
7128d483882Smlf
7138d483882Smlf error:
7148d483882Smlf free(entry);
7158d483882Smlf free_dvlist(pp);
7168d483882Smlf *pp = NULL;
7178d483882Smlf return (DI_WALK_TERMINATE);
7188d483882Smlf }
7198d483882Smlf
7208d483882Smlf /*
7218d483882Smlf * Returns success only if all goes well. If there is no matching reserved link
7228d483882Smlf * or if there is an error, we assume no match. It is better to err on the side
7238d483882Smlf * of caution by creating extra links than to miss out creating a required link.
7248d483882Smlf */
7258d483882Smlf static int
reserved_links_exist(di_node_t node,di_minor_t minor,int nflags)7268d483882Smlf reserved_links_exist(di_node_t node, di_minor_t minor, int nflags)
7278d483882Smlf {
7288d483882Smlf di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache();
7298d483882Smlf char phys_path[PATH_MAX];
7308d483882Smlf char *minor_path;
7318d483882Smlf dvlist_t *head;
7328d483882Smlf dvlist_t *entry;
7338d483882Smlf char *s;
7348d483882Smlf char l[PATH_MAX];
7358d483882Smlf int switch_link = 0;
7368d483882Smlf char *mn = di_minor_name(minor);
7378d483882Smlf
7388d483882Smlf if (dvlink_cache == NULL || mn == NULL) {
7398d483882Smlf devfsadm_errprint("%s: No minor or devlink cache\n", modname);
7408d483882Smlf return (DEVFSADM_FAILURE);
7418d483882Smlf }
7428d483882Smlf
743e37c6c37Scth if (!devfsadm_have_reserved()) {
744e37c6c37Scth devfsadm_print(disk_mid, "%s: No reserved links\n", modname);
7458d483882Smlf return (DEVFSADM_FAILURE);
7468d483882Smlf }
7478d483882Smlf
7488d483882Smlf minor_path = di_devfs_minor_path(minor);
7498d483882Smlf if (minor_path == NULL) {
7508d483882Smlf devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname);
7518d483882Smlf return (DEVFSADM_FAILURE);
7528d483882Smlf }
7538d483882Smlf
7548d483882Smlf (void) strlcpy(phys_path, minor_path, sizeof (phys_path));
7558d483882Smlf
7568d483882Smlf di_devfs_path_free(minor_path);
7578d483882Smlf
7588d483882Smlf head = NULL;
7598d483882Smlf (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path,
7608d483882Smlf DI_PRIMARY_LINK, &head, dvlink_cb);
7618d483882Smlf
7628d483882Smlf /*
7638d483882Smlf * We may be switching between EFI label and SMI label in which case
7648d483882Smlf * we only have minors of the other type.
7658d483882Smlf */
7668d483882Smlf if (head == NULL && (*mn == *(MN_SMI) ||
7678d483882Smlf (strncmp(mn, MN_EFI, 2) == 0))) {
7688d483882Smlf devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. "
7698d483882Smlf "Trying another label\n", modname, mn);
7708d483882Smlf s = strrchr(phys_path, ':');
7718d483882Smlf if (s == NULL) {
7728d483882Smlf devfsadm_errprint("%s: invalid minor path: %s\n",
7738d483882Smlf modname, phys_path);
7748d483882Smlf return (DEVFSADM_FAILURE);
7758d483882Smlf }
7768d483882Smlf (void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path),
7778d483882Smlf "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI,
7788d483882Smlf strstr(s, ",raw") ? ",raw" : "");
7798d483882Smlf (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE,
7808d483882Smlf phys_path, DI_PRIMARY_LINK, &head, dvlink_cb);
7818d483882Smlf }
7828d483882Smlf
7838d483882Smlf if (head == NULL) {
7848d483882Smlf devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n",
7858d483882Smlf modname, phys_path);
7868d483882Smlf /* no links on disk */
7878d483882Smlf return (DEVFSADM_FAILURE);
7888d483882Smlf }
7898d483882Smlf
7908d483882Smlf /*
7918d483882Smlf * It suffices to use 1 link to this minor, since
7928d483882Smlf * we are matching with reserved IDs on the basis of
7938d483882Smlf * the controller number which will be the same for
7948d483882Smlf * all links to this minor.
7958d483882Smlf */
7968d483882Smlf if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) {
7978d483882Smlf /* not reserved links */
7988d483882Smlf devfsadm_print(disk_mid, "%s: devlink %s and its minor "
7998d483882Smlf "are NOT reserved\n", modname, head->dv_link);
8008d483882Smlf free_dvlist(&head);
8018d483882Smlf return (DEVFSADM_FAILURE);
8028d483882Smlf }
8038d483882Smlf
8048d483882Smlf devfsadm_print(disk_mid, "%s: devlink %s and its minor are on "
8058d483882Smlf "reserved list\n", modname, head->dv_link);
8068d483882Smlf
8078d483882Smlf /*
8088d483882Smlf * Switch between SMI and EFI labels if required
8098d483882Smlf */
8108d483882Smlf switch_link = 0;
8118d483882Smlf if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
8128d483882Smlf for (entry = head; entry; entry = entry->dv_next) {
8138d483882Smlf s = strrchr(entry->dv_link, '/');
8148d483882Smlf assert(s);
8158d483882Smlf if (s == NULL) {
8168d483882Smlf devfsadm_errprint("%s: disk link %s has no "
8178d483882Smlf "directory\n", modname, entry->dv_link);
8188d483882Smlf continue;
8198d483882Smlf }
8208d483882Smlf if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) {
8218d483882Smlf (void) snprintf(l, sizeof (l), "%s%s",
8228d483882Smlf entry->dv_link, SLICE_SMI);
8238d483882Smlf switch_link = 1;
8248d483882Smlf devfsadm_print(disk_mid, "%s: switching "
8258d483882Smlf "reserved link from EFI to SMI label. "
8268d483882Smlf "New link is %s\n", modname, l);
8278d483882Smlf } else if (strncmp(mn, MN_EFI, 2) == 0 &&
8288d483882Smlf (s = strchr(s, 's'))) {
8298d483882Smlf *s = '\0';
8308d483882Smlf (void) snprintf(l, sizeof (l), "%s",
8318d483882Smlf entry->dv_link);
8328d483882Smlf *s = 's';
8338d483882Smlf switch_link = 1;
8348d483882Smlf devfsadm_print(disk_mid, "%s: switching "
8358d483882Smlf "reserved link from SMI to EFI label. "
8368d483882Smlf "New link is %s\n", modname, l);
8378d483882Smlf }
8388d483882Smlf if (switch_link) {
8398d483882Smlf devfsadm_print(disk_mid, "%s: switching "
8408d483882Smlf "link: deleting %s and creating %s\n",
8418d483882Smlf modname, entry->dv_link, l);
8428d483882Smlf devfsadm_rm_link(entry->dv_link);
8438d483882Smlf (void) devfsadm_mklink(l, node, minor, nflags);
8448d483882Smlf }
8458d483882Smlf }
8468d483882Smlf }
8478d483882Smlf free_dvlist(&head);
8488d483882Smlf
8498d483882Smlf /*
8508d483882Smlf * return SUCCESS to indicate that new links to this minor should not
8518d483882Smlf * be created so that only compatibility links to this minor remain.
8528d483882Smlf */
8538d483882Smlf return (DEVFSADM_SUCCESS);
8548d483882Smlf }
855