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*8d483882Smlf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <devfsadm.h> 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <strings.h> 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <limits.h> 337c478bd9Sstevel@tonic-gate #include <sys/stat.h> 3445916cd2Sjpk #include <bsm/devalloc.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #define DISK_SUBPATH_MAX 100 377c478bd9Sstevel@tonic-gate #define RM_STALE 0x01 387c478bd9Sstevel@tonic-gate #define DISK_LINK_RE "^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$" 397c478bd9Sstevel@tonic-gate #define DISK_LINK_TO_UPPER(ch)\ 407c478bd9Sstevel@tonic-gate (((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch) 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #define SLICE_SMI "s7" 437c478bd9Sstevel@tonic-gate #define SLICE_EFI "" 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #define MN_SMI "h" 467c478bd9Sstevel@tonic-gate #define MN_EFI "wd" 477c478bd9Sstevel@tonic-gate #define ASCIIWWNSIZE 255 487c478bd9Sstevel@tonic-gate 4945916cd2Sjpk extern int system_labeled; 5045916cd2Sjpk 517c478bd9Sstevel@tonic-gate static int disk_callback_chan(di_minor_t minor, di_node_t node); 527c478bd9Sstevel@tonic-gate static int disk_callback_nchan(di_minor_t minor, di_node_t node); 537c478bd9Sstevel@tonic-gate static int disk_callback_wwn(di_minor_t minor, di_node_t node); 547c478bd9Sstevel@tonic-gate static int disk_callback_fabric(di_minor_t minor, di_node_t node); 557c478bd9Sstevel@tonic-gate static void disk_common(di_minor_t minor, di_node_t node, char *disk, 567c478bd9Sstevel@tonic-gate int flags); 577c478bd9Sstevel@tonic-gate static char *diskctrl(di_node_t node, di_minor_t minor); 58*8d483882Smlf static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags); 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static devfsadm_create_t disk_cbt[] = { 627c478bd9Sstevel@tonic-gate { "disk", "ddi_block", NULL, 637c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_nchan 647c478bd9Sstevel@tonic-gate }, 657c478bd9Sstevel@tonic-gate { "disk", "ddi_block:channel", NULL, 667c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_chan 677c478bd9Sstevel@tonic-gate }, 687c478bd9Sstevel@tonic-gate { "disk", "ddi_block:fabric", NULL, 697c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_fabric 707c478bd9Sstevel@tonic-gate }, 717c478bd9Sstevel@tonic-gate { "disk", "ddi_block:wwn", NULL, 727c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_wwn 737c478bd9Sstevel@tonic-gate }, 747c478bd9Sstevel@tonic-gate { "disk", "ddi_block:cdrom", NULL, 757c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_nchan 767c478bd9Sstevel@tonic-gate }, 777c478bd9Sstevel@tonic-gate { "disk", "ddi_block:cdrom:channel", NULL, 787c478bd9Sstevel@tonic-gate TYPE_EXACT, ILEVEL_0, disk_callback_chan 797c478bd9Sstevel@tonic-gate }, 807c478bd9Sstevel@tonic-gate }; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(disk_cbt); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* 857c478bd9Sstevel@tonic-gate * HOT auto cleanup of disks not desired. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate static devfsadm_remove_t disk_remove_cbt[] = { 887c478bd9Sstevel@tonic-gate { "disk", DISK_LINK_RE, RM_POST, 897c478bd9Sstevel@tonic-gate ILEVEL_0, devfsadm_rm_all 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate }; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt); 947c478bd9Sstevel@tonic-gate 95*8d483882Smlf static devlink_re_t disks_re_array[] = { 96*8d483882Smlf {"^r?dsk/c([0-9]+)", 1}, 97*8d483882Smlf {"^cfg/c([0-9]+)$", 1}, 98*8d483882Smlf {"^scsi/.+/c([0-9]+)", 1}, 99*8d483882Smlf {NULL} 100*8d483882Smlf }; 101*8d483882Smlf 102*8d483882Smlf static char *disk_mid = "disk_mid"; 103*8d483882Smlf static char *modname = "disk_link"; 104*8d483882Smlf 105*8d483882Smlf int 106*8d483882Smlf minor_init() 107*8d483882Smlf { 108*8d483882Smlf devfsadm_print(disk_mid, 109*8d483882Smlf "%s: minor_init(): Creating disks reserved ID cache\n", 110*8d483882Smlf modname); 111*8d483882Smlf return (devfsadm_reserve_id_cache(disks_re_array, NULL)); 112*8d483882Smlf } 113*8d483882Smlf 1147c478bd9Sstevel@tonic-gate static int 1157c478bd9Sstevel@tonic-gate disk_callback_chan(di_minor_t minor, di_node_t node) 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate char *addr; 1187c478bd9Sstevel@tonic-gate char disk[20]; 1197c478bd9Sstevel@tonic-gate uint_t targ; 1207c478bd9Sstevel@tonic-gate uint_t lun; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate addr = di_bus_addr(node); 1237c478bd9Sstevel@tonic-gate (void) sscanf(addr, "%X,%X", &targ, &lun); 1247c478bd9Sstevel@tonic-gate (void) sprintf(disk, "t%dd%d", targ, lun); 1257c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, 0); 1267c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE); 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate static int 1317c478bd9Sstevel@tonic-gate disk_callback_nchan(di_minor_t minor, di_node_t node) 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate char *addr; 1347c478bd9Sstevel@tonic-gate char disk[10]; 1357c478bd9Sstevel@tonic-gate uint_t lun; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate addr = di_bus_addr(node); 1387c478bd9Sstevel@tonic-gate (void) sscanf(addr, "%X", &lun); 1397c478bd9Sstevel@tonic-gate (void) sprintf(disk, "d%d", lun); 1407c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, 0); 1417c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE); 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate static int 1467c478bd9Sstevel@tonic-gate disk_callback_wwn(di_minor_t minor, di_node_t node) 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate char disk[10]; 1497c478bd9Sstevel@tonic-gate int lun; 1507c478bd9Sstevel@tonic-gate int targ; 1517c478bd9Sstevel@tonic-gate int *intp; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 1547c478bd9Sstevel@tonic-gate "target", &intp) <= 0) { 1557c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate targ = *intp; 1587c478bd9Sstevel@tonic-gate if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 1597c478bd9Sstevel@tonic-gate "lun", &intp) <= 0) { 1607c478bd9Sstevel@tonic-gate lun = 0; 1617c478bd9Sstevel@tonic-gate } else { 1627c478bd9Sstevel@tonic-gate lun = *intp; 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate (void) sprintf(disk, "t%dd%d", targ, lun); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, RM_STALE); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate static int 1727c478bd9Sstevel@tonic-gate disk_callback_fabric(di_minor_t minor, di_node_t node) 1737c478bd9Sstevel@tonic-gate { 1747c478bd9Sstevel@tonic-gate char disk[DISK_SUBPATH_MAX]; 1757c478bd9Sstevel@tonic-gate int lun; 1767c478bd9Sstevel@tonic-gate int count; 1777c478bd9Sstevel@tonic-gate int *intp; 1787c478bd9Sstevel@tonic-gate uchar_t *str; 1797c478bd9Sstevel@tonic-gate uchar_t *wwn; 1807c478bd9Sstevel@tonic-gate uchar_t ascii_wwn[ASCIIWWNSIZE]; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 1837c478bd9Sstevel@tonic-gate "client-guid", (char **)&wwn) > 0) { 1847c478bd9Sstevel@tonic-gate if (strlcpy((char *)ascii_wwn, (char *)wwn, sizeof (ascii_wwn)) 1857c478bd9Sstevel@tonic-gate >= sizeof (ascii_wwn)) { 1867c478bd9Sstevel@tonic-gate devfsadm_errprint("SUNW_disk_link: GUID too long:%d", 1877c478bd9Sstevel@tonic-gate strlen((char *)wwn)); 1887c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate lun = 0; 1917c478bd9Sstevel@tonic-gate } else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node, 1927c478bd9Sstevel@tonic-gate "port-wwn", &wwn) > 0) { 1937c478bd9Sstevel@tonic-gate if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 1947c478bd9Sstevel@tonic-gate "lun", &intp) > 0) { 1957c478bd9Sstevel@tonic-gate lun = *intp; 1967c478bd9Sstevel@tonic-gate } else { 1977c478bd9Sstevel@tonic-gate lun = 0; 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) { 2017c478bd9Sstevel@tonic-gate (void) sprintf((caddr_t)str, "%02x", wwn[count]); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate *str = '\0'; 2047c478bd9Sstevel@tonic-gate } else { 2057c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate for (str = ascii_wwn; *str != '\0'; str++) { 2097c478bd9Sstevel@tonic-gate *str = DISK_LINK_TO_UPPER(*str); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate (void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate disk_common(minor, node, disk, RM_STALE); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate /* 2207c478bd9Sstevel@tonic-gate * This function is called for every disk minor node. 2217c478bd9Sstevel@tonic-gate * Calls enumerate to assign a logical controller number, and 2227c478bd9Sstevel@tonic-gate * then devfsadm_mklink to make the link. 2237c478bd9Sstevel@tonic-gate */ 2247c478bd9Sstevel@tonic-gate static void 2257c478bd9Sstevel@tonic-gate disk_common(di_minor_t minor, di_node_t node, char *disk, int flags) 2267c478bd9Sstevel@tonic-gate { 2277c478bd9Sstevel@tonic-gate char l_path[PATH_MAX + 1]; 22818c2aff7Sartem char sec_path[PATH_MAX + 1]; 2297c478bd9Sstevel@tonic-gate char stale_re[DISK_SUBPATH_MAX]; 2307c478bd9Sstevel@tonic-gate char *dir; 2317c478bd9Sstevel@tonic-gate char slice[4]; 2327c478bd9Sstevel@tonic-gate char *mn; 2337c478bd9Sstevel@tonic-gate char *ctrl; 23445916cd2Sjpk char *nt = NULL; 23518c2aff7Sartem int *int_prop; 23645916cd2Sjpk int nflags = 0; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if (strstr(mn = di_minor_name(minor), ",raw")) { 2397c478bd9Sstevel@tonic-gate dir = "rdsk"; 2407c478bd9Sstevel@tonic-gate } else { 2417c478bd9Sstevel@tonic-gate dir = "dsk"; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (mn[0] < 113) { 2457c478bd9Sstevel@tonic-gate (void) sprintf(slice, "s%d", mn[0] - 'a'); 2467c478bd9Sstevel@tonic-gate } else if (strncmp(mn, MN_EFI, 2) != 0) { 2477c478bd9Sstevel@tonic-gate (void) sprintf(slice, "p%d", mn[0] - 'q'); 2487c478bd9Sstevel@tonic-gate } else { 2497c478bd9Sstevel@tonic-gate /* For EFI label */ 2507c478bd9Sstevel@tonic-gate (void) sprintf(slice, SLICE_EFI); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 253*8d483882Smlf nflags = 0; 254*8d483882Smlf if (system_labeled) { 255*8d483882Smlf nt = di_minor_nodetype(minor); 256*8d483882Smlf if ((nt != NULL) && 257*8d483882Smlf ((strcmp(nt, DDI_NT_CD) == 0) || 258*8d483882Smlf (strcmp(nt, DDI_NT_CD_CHAN) == 0) || 259*8d483882Smlf (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) { 260*8d483882Smlf nflags = DA_ADD|DA_CD; 261*8d483882Smlf } 262*8d483882Smlf } 263*8d483882Smlf 264*8d483882Smlf if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) { 265*8d483882Smlf devfsadm_print(disk_mid, "Reserved link exists. Not " 266*8d483882Smlf "creating links for slice %s\n", slice); 267*8d483882Smlf return; 268*8d483882Smlf } 269*8d483882Smlf 2707c478bd9Sstevel@tonic-gate if (NULL == (ctrl = diskctrl(node, minor))) 2717c478bd9Sstevel@tonic-gate return; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate (void) strcpy(l_path, dir); 2747c478bd9Sstevel@tonic-gate (void) strcat(l_path, "/c"); 2757c478bd9Sstevel@tonic-gate (void) strcat(l_path, ctrl); 2767c478bd9Sstevel@tonic-gate (void) strcat(l_path, disk); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate /* 2797c478bd9Sstevel@tonic-gate * If switching between SMI and EFI label or vice versa 2807c478bd9Sstevel@tonic-gate * cleanup the previous label's devlinks. 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) { 2837c478bd9Sstevel@tonic-gate char *s, tpath[PATH_MAX + 1]; 2847c478bd9Sstevel@tonic-gate struct stat sb; 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate s = l_path + strlen(l_path); 2877c478bd9Sstevel@tonic-gate (void) strcat(l_path, (*mn == *(MN_SMI)) 2887c478bd9Sstevel@tonic-gate ? SLICE_EFI : SLICE_SMI); 2897c478bd9Sstevel@tonic-gate /* 2907c478bd9Sstevel@tonic-gate * Attempt the remove only if the stale link exists 2917c478bd9Sstevel@tonic-gate */ 2927c478bd9Sstevel@tonic-gate (void) snprintf(tpath, sizeof (tpath), "%s/dev/%s", 2937c478bd9Sstevel@tonic-gate devfsadm_root_path(), l_path); 2947c478bd9Sstevel@tonic-gate if (lstat(tpath, &sb) != -1) 2957c478bd9Sstevel@tonic-gate devfsadm_rm_all(l_path); 2967c478bd9Sstevel@tonic-gate *s = '\0'; 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate (void) strcat(l_path, slice); 2997c478bd9Sstevel@tonic-gate 30045916cd2Sjpk (void) devfsadm_mklink(l_path, node, minor, nflags); 3017c478bd9Sstevel@tonic-gate 30218c2aff7Sartem /* secondary links for removable and hotpluggable devices */ 30318c2aff7Sartem if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media", 30418c2aff7Sartem &int_prop) >= 0) { 30518c2aff7Sartem (void) strcpy(sec_path, "removable-media/"); 30618c2aff7Sartem (void) strcat(sec_path, l_path); 30718c2aff7Sartem (void) devfsadm_secondary_link(sec_path, l_path, 0); 3087544909dSartem } 3097544909dSartem if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable", 31018c2aff7Sartem &int_prop) >= 0) { 31118c2aff7Sartem (void) strcpy(sec_path, "hotpluggable/"); 31218c2aff7Sartem (void) strcat(sec_path, l_path); 31318c2aff7Sartem (void) devfsadm_secondary_link(sec_path, l_path, 0); 31418c2aff7Sartem } 31518c2aff7Sartem 3167c478bd9Sstevel@tonic-gate if ((flags & RM_STALE) == RM_STALE) { 3177c478bd9Sstevel@tonic-gate (void) strcpy(stale_re, "^"); 3187c478bd9Sstevel@tonic-gate (void) strcat(stale_re, dir); 3197c478bd9Sstevel@tonic-gate (void) strcat(stale_re, "/c"); 3207c478bd9Sstevel@tonic-gate (void) strcat(stale_re, ctrl); 3217c478bd9Sstevel@tonic-gate (void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$"); 3227c478bd9Sstevel@tonic-gate /* 3237c478bd9Sstevel@tonic-gate * optimizations are made inside of devfsadm_rm_stale_links 3247c478bd9Sstevel@tonic-gate * instead of before calling the function, as it always 3257c478bd9Sstevel@tonic-gate * needs to add the valid link to the cache. 3267c478bd9Sstevel@tonic-gate */ 3277c478bd9Sstevel@tonic-gate devfsadm_rm_stale_links(stale_re, l_path, node, minor); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate free(ctrl); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */ 3357c478bd9Sstevel@tonic-gate #define RULE_INDEX 0 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate static char * 3387c478bd9Sstevel@tonic-gate diskctrl(di_node_t node, di_minor_t minor) 3397c478bd9Sstevel@tonic-gate { 3407c478bd9Sstevel@tonic-gate char path[PATH_MAX + 1]; 3417c478bd9Sstevel@tonic-gate char *devfspath; 3427c478bd9Sstevel@tonic-gate char *buf, *mn; 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[3] = { 3457c478bd9Sstevel@tonic-gate {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT}, 3467c478bd9Sstevel@tonic-gate {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR}, 3477c478bd9Sstevel@tonic-gate {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT} 3487c478bd9Sstevel@tonic-gate }; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate mn = di_minor_name(minor); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if ((devfspath = di_devfs_path(node)) == NULL) { 3537c478bd9Sstevel@tonic-gate return (NULL); 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate (void) strcpy(path, devfspath); 3567c478bd9Sstevel@tonic-gate (void) strcat(path, ":"); 3577c478bd9Sstevel@tonic-gate (void) strcat(path, mn); 3587c478bd9Sstevel@tonic-gate di_devfs_path_free(devfspath); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate /* 3617c478bd9Sstevel@tonic-gate * Use controller component of disk path 3627c478bd9Sstevel@tonic-gate */ 3637c478bd9Sstevel@tonic-gate if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) == 3647c478bd9Sstevel@tonic-gate DEVFSADM_MULTIPLE) { 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * We failed because there are multiple logical controller 3687c478bd9Sstevel@tonic-gate * numbers for a single physical controller. If we use node 3697c478bd9Sstevel@tonic-gate * name also in the match it should fix this and only find one 3707c478bd9Sstevel@tonic-gate * logical controller. (See 4045879). 3717c478bd9Sstevel@tonic-gate * NOTE: Rules for controllers are not changed, as there is 3727c478bd9Sstevel@tonic-gate * no unique controller number for them in this case. 3737c478bd9Sstevel@tonic-gate * 3747c478bd9Sstevel@tonic-gate * MATCH_UNCACHED flag is private to the "disks" and "sgen" 3757c478bd9Sstevel@tonic-gate * modules. NOT to be used by other modules. 3767c478bd9Sstevel@tonic-gate */ 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */ 3797c478bd9Sstevel@tonic-gate rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */ 3807c478bd9Sstevel@tonic-gate if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) { 3817c478bd9Sstevel@tonic-gate return (NULL); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate return (buf); 3867c478bd9Sstevel@tonic-gate } 387*8d483882Smlf 388*8d483882Smlf typedef struct dvlist { 389*8d483882Smlf char *dv_link; 390*8d483882Smlf struct dvlist *dv_next; 391*8d483882Smlf } dvlist_t; 392*8d483882Smlf 393*8d483882Smlf static void 394*8d483882Smlf free_dvlist(dvlist_t **pp) 395*8d483882Smlf { 396*8d483882Smlf dvlist_t *entry; 397*8d483882Smlf 398*8d483882Smlf while (*pp) { 399*8d483882Smlf entry = *pp; 400*8d483882Smlf *pp = entry->dv_next; 401*8d483882Smlf assert(entry->dv_link); 402*8d483882Smlf free(entry->dv_link); 403*8d483882Smlf free(entry); 404*8d483882Smlf } 405*8d483882Smlf } 406*8d483882Smlf static int 407*8d483882Smlf dvlink_cb(di_devlink_t devlink, void *arg) 408*8d483882Smlf { 409*8d483882Smlf char *path; 410*8d483882Smlf char *can_path; 411*8d483882Smlf dvlist_t **pp = (dvlist_t **)arg; 412*8d483882Smlf dvlist_t *entry = NULL; 413*8d483882Smlf 414*8d483882Smlf entry = calloc(1, sizeof (dvlist_t)); 415*8d483882Smlf if (entry == NULL) { 416*8d483882Smlf devfsadm_errprint("%s: calloc failed\n", modname); 417*8d483882Smlf goto error; 418*8d483882Smlf } 419*8d483882Smlf 420*8d483882Smlf path = (char *)di_devlink_path(devlink); 421*8d483882Smlf assert(path); 422*8d483882Smlf if (path == NULL) { 423*8d483882Smlf devfsadm_errprint("%s: di_devlink_path() returned NULL\n", 424*8d483882Smlf modname); 425*8d483882Smlf goto error; 426*8d483882Smlf } 427*8d483882Smlf 428*8d483882Smlf devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n", 429*8d483882Smlf modname, path); 430*8d483882Smlf 431*8d483882Smlf /* 432*8d483882Smlf * Return linkname in canonical form i.e. without the 433*8d483882Smlf * "/dev/" prefix 434*8d483882Smlf */ 435*8d483882Smlf can_path = strstr(path, "/dev/"); 436*8d483882Smlf if (can_path == NULL) { 437*8d483882Smlf devfsadm_errprint("%s: devlink path %s has no /dev/\n", 438*8d483882Smlf modname, path); 439*8d483882Smlf goto error; 440*8d483882Smlf } 441*8d483882Smlf 442*8d483882Smlf entry->dv_link = s_strdup(can_path + strlen("/dev/")); 443*8d483882Smlf entry->dv_next = *pp; 444*8d483882Smlf *pp = entry; 445*8d483882Smlf 446*8d483882Smlf return (DI_WALK_CONTINUE); 447*8d483882Smlf 448*8d483882Smlf error: 449*8d483882Smlf free(entry); 450*8d483882Smlf free_dvlist(pp); 451*8d483882Smlf *pp = NULL; 452*8d483882Smlf return (DI_WALK_TERMINATE); 453*8d483882Smlf } 454*8d483882Smlf 455*8d483882Smlf /* 456*8d483882Smlf * Returns success only if all goes well. If there is no matching reserved link 457*8d483882Smlf * or if there is an error, we assume no match. It is better to err on the side 458*8d483882Smlf * of caution by creating extra links than to miss out creating a required link. 459*8d483882Smlf */ 460*8d483882Smlf static int 461*8d483882Smlf reserved_links_exist(di_node_t node, di_minor_t minor, int nflags) 462*8d483882Smlf { 463*8d483882Smlf di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache(); 464*8d483882Smlf char phys_path[PATH_MAX]; 465*8d483882Smlf char *minor_path; 466*8d483882Smlf dvlist_t *head; 467*8d483882Smlf dvlist_t *entry; 468*8d483882Smlf char *s; 469*8d483882Smlf char l[PATH_MAX]; 470*8d483882Smlf int switch_link = 0; 471*8d483882Smlf struct stat sb; 472*8d483882Smlf char *mn = di_minor_name(minor); 473*8d483882Smlf 474*8d483882Smlf if (dvlink_cache == NULL || mn == NULL) { 475*8d483882Smlf devfsadm_errprint("%s: No minor or devlink cache\n", modname); 476*8d483882Smlf return (DEVFSADM_FAILURE); 477*8d483882Smlf } 478*8d483882Smlf 479*8d483882Smlf if (stat(ENUMERATE_RESERVED, &sb) == -1) { 480*8d483882Smlf devfsadm_print(disk_mid, "%s: No reserved file: %s. Will " 481*8d483882Smlf "not bypass new link creation\n", 482*8d483882Smlf modname, ENUMERATE_RESERVED); 483*8d483882Smlf return (DEVFSADM_FAILURE); 484*8d483882Smlf } 485*8d483882Smlf 486*8d483882Smlf minor_path = di_devfs_minor_path(minor); 487*8d483882Smlf if (minor_path == NULL) { 488*8d483882Smlf devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname); 489*8d483882Smlf return (DEVFSADM_FAILURE); 490*8d483882Smlf } 491*8d483882Smlf 492*8d483882Smlf (void) strlcpy(phys_path, minor_path, sizeof (phys_path)); 493*8d483882Smlf 494*8d483882Smlf di_devfs_path_free(minor_path); 495*8d483882Smlf 496*8d483882Smlf head = NULL; 497*8d483882Smlf (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path, 498*8d483882Smlf DI_PRIMARY_LINK, &head, dvlink_cb); 499*8d483882Smlf 500*8d483882Smlf /* 501*8d483882Smlf * We may be switching between EFI label and SMI label in which case 502*8d483882Smlf * we only have minors of the other type. 503*8d483882Smlf */ 504*8d483882Smlf if (head == NULL && (*mn == *(MN_SMI) || 505*8d483882Smlf (strncmp(mn, MN_EFI, 2) == 0))) { 506*8d483882Smlf devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. " 507*8d483882Smlf "Trying another label\n", modname, mn); 508*8d483882Smlf s = strrchr(phys_path, ':'); 509*8d483882Smlf if (s == NULL) { 510*8d483882Smlf devfsadm_errprint("%s: invalid minor path: %s\n", 511*8d483882Smlf modname, phys_path); 512*8d483882Smlf return (DEVFSADM_FAILURE); 513*8d483882Smlf } 514*8d483882Smlf (void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path), 515*8d483882Smlf "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI, 516*8d483882Smlf strstr(s, ",raw") ? ",raw" : ""); 517*8d483882Smlf (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, 518*8d483882Smlf phys_path, DI_PRIMARY_LINK, &head, dvlink_cb); 519*8d483882Smlf } 520*8d483882Smlf 521*8d483882Smlf if (head == NULL) { 522*8d483882Smlf devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n", 523*8d483882Smlf modname, phys_path); 524*8d483882Smlf /* no links on disk */ 525*8d483882Smlf return (DEVFSADM_FAILURE); 526*8d483882Smlf } 527*8d483882Smlf 528*8d483882Smlf /* 529*8d483882Smlf * It suffices to use 1 link to this minor, since 530*8d483882Smlf * we are matching with reserved IDs on the basis of 531*8d483882Smlf * the controller number which will be the same for 532*8d483882Smlf * all links to this minor. 533*8d483882Smlf */ 534*8d483882Smlf if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) { 535*8d483882Smlf /* not reserved links */ 536*8d483882Smlf devfsadm_print(disk_mid, "%s: devlink %s and its minor " 537*8d483882Smlf "are NOT reserved\n", modname, head->dv_link); 538*8d483882Smlf free_dvlist(&head); 539*8d483882Smlf return (DEVFSADM_FAILURE); 540*8d483882Smlf } 541*8d483882Smlf 542*8d483882Smlf devfsadm_print(disk_mid, "%s: devlink %s and its minor are on " 543*8d483882Smlf "reserved list\n", modname, head->dv_link); 544*8d483882Smlf 545*8d483882Smlf /* 546*8d483882Smlf * Switch between SMI and EFI labels if required 547*8d483882Smlf */ 548*8d483882Smlf switch_link = 0; 549*8d483882Smlf if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) { 550*8d483882Smlf for (entry = head; entry; entry = entry->dv_next) { 551*8d483882Smlf s = strrchr(entry->dv_link, '/'); 552*8d483882Smlf assert(s); 553*8d483882Smlf if (s == NULL) { 554*8d483882Smlf devfsadm_errprint("%s: disk link %s has no " 555*8d483882Smlf "directory\n", modname, entry->dv_link); 556*8d483882Smlf continue; 557*8d483882Smlf } 558*8d483882Smlf if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) { 559*8d483882Smlf (void) snprintf(l, sizeof (l), "%s%s", 560*8d483882Smlf entry->dv_link, SLICE_SMI); 561*8d483882Smlf switch_link = 1; 562*8d483882Smlf devfsadm_print(disk_mid, "%s: switching " 563*8d483882Smlf "reserved link from EFI to SMI label. " 564*8d483882Smlf "New link is %s\n", modname, l); 565*8d483882Smlf } else if (strncmp(mn, MN_EFI, 2) == 0 && 566*8d483882Smlf (s = strchr(s, 's'))) { 567*8d483882Smlf *s = '\0'; 568*8d483882Smlf (void) snprintf(l, sizeof (l), "%s", 569*8d483882Smlf entry->dv_link); 570*8d483882Smlf *s = 's'; 571*8d483882Smlf switch_link = 1; 572*8d483882Smlf devfsadm_print(disk_mid, "%s: switching " 573*8d483882Smlf "reserved link from SMI to EFI label. " 574*8d483882Smlf "New link is %s\n", modname, l); 575*8d483882Smlf } 576*8d483882Smlf if (switch_link) { 577*8d483882Smlf devfsadm_print(disk_mid, "%s: switching " 578*8d483882Smlf "link: deleting %s and creating %s\n", 579*8d483882Smlf modname, entry->dv_link, l); 580*8d483882Smlf devfsadm_rm_link(entry->dv_link); 581*8d483882Smlf (void) devfsadm_mklink(l, node, minor, nflags); 582*8d483882Smlf } 583*8d483882Smlf } 584*8d483882Smlf } 585*8d483882Smlf free_dvlist(&head); 586*8d483882Smlf 587*8d483882Smlf /* 588*8d483882Smlf * return SUCCESS to indicate that new links to this minor should not 589*8d483882Smlf * be created so that only compatibility links to this minor remain. 590*8d483882Smlf */ 591*8d483882Smlf return (DEVFSADM_SUCCESS); 592*8d483882Smlf } 593