1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2016 Toomas Soome <tsoome@me.com> 23 * Copyright 2012 Nexenta Systems, Inc. All rights reserved. 24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #include <devfsadm.h> 29 #include <stdio.h> 30 #include <strings.h> 31 #include <stdlib.h> 32 #include <limits.h> 33 #include <ctype.h> 34 #include <unistd.h> 35 #include <sys/int_fmtio.h> 36 #include <sys/stat.h> 37 #include <bsm/devalloc.h> 38 #include <sys/scsi/scsi_address.h> 39 #include <sys/libdevid.h> 40 #include <sys/lofi.h> 41 42 #define DISK_SUBPATH_MAX 100 43 #define RM_STALE 0x01 44 #define DISK_LINK_RE "^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$" 45 #define DISK_LINK_TO_UPPER(ch)\ 46 (((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch) 47 48 #define SLICE_SMI "s7" 49 #define SLICE_EFI "" 50 51 #define MN_SMI "h" 52 #define MN_EFI "wd" 53 #define ASCIIWWNSIZE 255 54 #if defined(__i386) || defined(__amd64) 55 /* 56 * The number of minor nodes per LUN is defined by the disk drivers. 57 * Currently it is set to 64. Refer CMLBUNIT_SHIFT (cmlb_impl.h) 58 */ 59 #define NUM_MINORS_PER_INSTANCE 64 60 #endif 61 62 63 extern int system_labeled; 64 65 static int disk_callback_chan(di_minor_t minor, di_node_t node); 66 static int disk_callback_nchan(di_minor_t minor, di_node_t node); 67 static int disk_callback_wwn(di_minor_t minor, di_node_t node); 68 static int disk_callback_xvmd(di_minor_t minor, di_node_t node); 69 static int disk_callback_fabric(di_minor_t minor, di_node_t node); 70 static int disk_callback_sas(di_minor_t minor, di_node_t node); 71 static void disk_common(di_minor_t minor, di_node_t node, char *disk, 72 int flags); 73 static char *diskctrl(di_node_t node, di_minor_t minor); 74 static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags); 75 static void disk_rm_lofi_all(char *file); 76 77 78 static devfsadm_create_t disk_cbt[] = { 79 { "disk", DDI_NT_BLOCK, NULL, 80 TYPE_EXACT, ILEVEL_0, disk_callback_nchan 81 }, 82 { "disk", DDI_NT_BLOCK_CHAN, NULL, 83 TYPE_EXACT, ILEVEL_0, disk_callback_chan 84 }, 85 { "disk", DDI_NT_BLOCK_FABRIC, NULL, 86 TYPE_EXACT, ILEVEL_0, disk_callback_fabric 87 }, 88 { "disk", DDI_NT_BLOCK_WWN, NULL, 89 TYPE_EXACT, ILEVEL_0, disk_callback_wwn 90 }, 91 { "disk", DDI_NT_BLOCK_SAS, NULL, 92 TYPE_EXACT, ILEVEL_0, disk_callback_sas 93 }, 94 { "disk", DDI_NT_CD, NULL, 95 TYPE_EXACT, ILEVEL_0, disk_callback_nchan 96 }, 97 { "disk", DDI_NT_CD_CHAN, NULL, 98 TYPE_EXACT, ILEVEL_0, disk_callback_chan 99 }, 100 { "disk", DDI_NT_BLOCK_XVMD, NULL, 101 TYPE_EXACT, ILEVEL_0, disk_callback_xvmd 102 }, 103 { "disk", DDI_NT_CD_XVMD, NULL, 104 TYPE_EXACT, ILEVEL_0, disk_callback_xvmd 105 }, 106 }; 107 108 DEVFSADM_CREATE_INIT_V0(disk_cbt); 109 110 /* 111 * HOT auto cleanup of disks is done for lofi devices only. 112 */ 113 static devfsadm_remove_t disk_remove_cbt[] = { 114 { "disk", DISK_LINK_RE, RM_HOT | RM_POST | RM_ALWAYS, 115 ILEVEL_0, disk_rm_lofi_all 116 }, 117 { "disk", DISK_LINK_RE, RM_POST, 118 ILEVEL_0, devfsadm_rm_all 119 } 120 }; 121 122 DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt); 123 124 static devlink_re_t disks_re_array[] = { 125 {"^r?dsk/c([0-9]+)", 1}, 126 {"^cfg/c([0-9]+)$", 1}, 127 {"^scsi/.+/c([0-9]+)", 1}, 128 {NULL} 129 }; 130 131 static char *disk_mid = "disk_mid"; 132 static char *modname = "disk_link"; 133 134 /* 135 * Check if link is from lofi by checking path from readlink(). 136 */ 137 static int 138 is_lofi_disk(char *file) 139 { 140 char buf[PATH_MAX + 1]; 141 char filepath[PATH_MAX]; 142 char *ptr; 143 ssize_t size; 144 145 size = snprintf(filepath, sizeof (filepath), "%s/dev/%s", 146 devfsadm_root_path(), file); 147 if (size > sizeof (filepath)) 148 return (0); 149 150 size = readlink(filepath, buf, sizeof (buf) - 1); 151 if (size == -1) 152 return (0); 153 buf[size] = '\0'; 154 ptr = strchr(buf, '@'); 155 if (ptr == NULL) 156 return (0); 157 ptr[1] = '\0'; 158 if (strcmp(buf, "../../devices/pseudo/lofi@") != 0) 159 return (0); 160 return (1); 161 } 162 163 /* 164 * Wrapper around devfsadm_rm_link() for lofi devices. 165 */ 166 static void disk_rm_lofi_all(char *file) 167 { 168 if (is_lofi_disk(file)) 169 devfsadm_rm_link(file); 170 } 171 172 int 173 minor_init() 174 { 175 devfsadm_print(disk_mid, 176 "%s: minor_init(): Creating disks reserved ID cache\n", 177 modname); 178 return (devfsadm_reserve_id_cache(disks_re_array, NULL)); 179 } 180 181 static int 182 disk_callback_chan(di_minor_t minor, di_node_t node) 183 { 184 char *addr; 185 char disk[23]; 186 char *driver; 187 uint_t targ = 0; 188 uint_t lun = 0; 189 190 driver = di_driver_name(node); 191 if (strcmp(driver, LOFI_DRIVER_NAME) != 0) { 192 addr = di_bus_addr(node); 193 (void) sscanf(addr, "%X,%X", &targ, &lun); 194 } else { 195 targ = di_instance(node); 196 } 197 198 (void) snprintf(disk, sizeof (disk), "t%dd%d", targ, lun); 199 disk_common(minor, node, disk, 0); 200 return (DEVFSADM_CONTINUE); 201 202 } 203 204 static int 205 disk_callback_nchan(di_minor_t minor, di_node_t node) 206 { 207 char *addr; 208 char disk[10]; 209 uint_t lun; 210 211 addr = di_bus_addr(node); 212 (void) sscanf(addr, "%X", &lun); 213 (void) sprintf(disk, "d%d", lun); 214 disk_common(minor, node, disk, 0); 215 return (DEVFSADM_CONTINUE); 216 217 } 218 219 static int 220 disk_callback_wwn(di_minor_t minor, di_node_t node) 221 { 222 char disk[10]; 223 int lun; 224 int targ; 225 int *intp; 226 227 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_TARGET, 228 &intp) <= 0) { 229 return (DEVFSADM_CONTINUE); 230 } 231 targ = *intp; 232 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_LUN, 233 &intp) <= 0) { 234 lun = 0; 235 } else { 236 lun = *intp; 237 } 238 (void) sprintf(disk, "t%dd%d", targ, lun); 239 240 disk_common(minor, node, disk, RM_STALE); 241 242 return (DEVFSADM_CONTINUE); 243 } 244 245 static int 246 disk_callback_fabric(di_minor_t minor, di_node_t node) 247 { 248 char disk[DISK_SUBPATH_MAX]; 249 int lun; 250 int count; 251 int *intp; 252 uchar_t *str; 253 uchar_t *wwn; 254 uchar_t ascii_wwn[ASCIIWWNSIZE]; 255 256 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 257 "client-guid", (char **)&wwn) > 0) { 258 if (strlcpy((char *)ascii_wwn, (char *)wwn, 259 sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) { 260 devfsadm_errprint("SUNW_disk_link: GUID too long:%d", 261 strlen((char *)wwn)); 262 return (DEVFSADM_CONTINUE); 263 } 264 lun = 0; 265 } else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node, 266 "port-wwn", &wwn) > 0) { 267 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 268 SCSI_ADDR_PROP_LUN, &intp) > 0) { 269 lun = *intp; 270 } else { 271 lun = 0; 272 } 273 274 for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) { 275 (void) sprintf((caddr_t)str, "%02x", wwn[count]); 276 } 277 *str = '\0'; 278 } else { 279 return (DEVFSADM_CONTINUE); 280 } 281 282 for (str = ascii_wwn; *str != '\0'; str++) { 283 *str = DISK_LINK_TO_UPPER(*str); 284 } 285 286 (void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun); 287 288 disk_common(minor, node, disk, RM_STALE); 289 290 return (DEVFSADM_CONTINUE); 291 } 292 293 static int 294 disk_callback_sas(di_minor_t minor, di_node_t node) 295 { 296 char disk[DISK_SUBPATH_MAX]; 297 int lun64_found = 0; 298 scsi_lun64_t lun64, sl; 299 scsi_lun_t lun; 300 int64_t *lun64p; 301 uint64_t wwn; 302 int *intp; 303 char *tgt_port; 304 uchar_t addr_method; 305 306 /* Get lun property */ 307 if (di_prop_lookup_int64(DDI_DEV_T_ANY, node, 308 SCSI_ADDR_PROP_LUN64, &lun64p) > 0) { 309 if (*lun64p != SCSI_LUN64_ILLEGAL) { 310 lun64_found = 1; 311 lun64 = (uint64_t)*lun64p; 312 } 313 } 314 if ((!lun64_found) && (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 315 SCSI_ADDR_PROP_LUN, &intp) > 0)) { 316 lun64 = (uint64_t)*intp; 317 } 318 319 lun = scsi_lun64_to_lun(lun64); 320 321 addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK); 322 323 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 324 SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) { 325 (void) scsi_wwnstr_to_wwn(tgt_port, &wwn); 326 if ((addr_method == SCSI_LUN_AM_PDEV) && 327 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) && 328 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) && 329 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) { 330 (void) snprintf(disk, DISK_SUBPATH_MAX, 331 "t%"PRIX64"d%"PRId64, wwn, lun64); 332 } else if ((addr_method == SCSI_LUN_AM_FLAT) && 333 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) && 334 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) && 335 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) { 336 sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb; 337 (void) snprintf(disk, DISK_SUBPATH_MAX, 338 "t%"PRIX64"d%"PRIX16, wwn, sl); 339 } else { 340 (void) snprintf(disk, DISK_SUBPATH_MAX, 341 "t%"PRIX64"d%"PRIX64, wwn, lun64); 342 } 343 } else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 344 SCSI_ADDR_PROP_SATA_PHY, &intp) > 0) { 345 /* Use phy format naming, for SATA devices without wwn */ 346 if ((addr_method == SCSI_LUN_AM_PDEV) && 347 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) && 348 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) && 349 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) { 350 (void) snprintf(disk, DISK_SUBPATH_MAX, 351 "t%dd%"PRId64, *intp, lun64); 352 } else if ((addr_method == SCSI_LUN_AM_FLAT) && 353 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) && 354 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) && 355 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) { 356 sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb; 357 (void) snprintf(disk, DISK_SUBPATH_MAX, 358 "t%dd%"PRIX16, *intp, sl); 359 } else { 360 (void) snprintf(disk, DISK_SUBPATH_MAX, 361 "t%dd%"PRIX64, *intp, lun64); 362 } 363 } else { 364 return (DEVFSADM_CONTINUE); 365 } 366 367 disk_common(minor, node, disk, RM_STALE); 368 369 return (DEVFSADM_CONTINUE); 370 } 371 372 /* 373 * xVM virtual block device 374 * 375 * Xen passes device number in next format: 376 * 377 * 1 << 28 | disk << 8 | partition xvd, disks or partitions 16 onwards 378 * 202 << 8 | disk << 4 | partition xvd, disks and partitions up to 15 379 * 8 << 8 | disk << 4 | partition sd, disks and partitions up to 15 380 * 3 << 8 | disk << 6 | partition hd, disks 0..1, partitions 0..63 381 * 22 << 8 | (disk-2) << 6 | partition hd, disks 2..3, partitions 0..63 382 * 2 << 28 onwards reserved for future use 383 * other values less than 1 << 28 deprecated / reserved 384 * 385 * The corresponding Solaris /dev/dsk name can be: 386 * 387 * c0tYdXsN 388 * 389 * where Y,X >= 0. 390 * 391 * For PV guests using the legacy naming (0, 1, 2, ...) 392 * the Solaris disk names created will be c0d[0..767]sN 393 */ 394 395 #define HD_BASE (3 << 8) 396 #define XEN_EXT_SHIFT (28) 397 398 /* 399 * Return: Number of parsed and written parameters 400 */ 401 static int 402 decode_xen_device(uint_t device, uint_t *disk, uint_t *plun) 403 { 404 uint_t dsk, lun = 0; 405 int ret = 1; 406 407 if ((device >> XEN_EXT_SHIFT) > 1) 408 return (0); 409 410 if (device < HD_BASE) { 411 /* legacy device address */ 412 dsk = device; 413 goto end; 414 } 415 416 ret = 2; 417 if (device & (1 << XEN_EXT_SHIFT)) { 418 /* extended */ 419 dsk = device & (~0xff); 420 lun = device & 0xff; 421 goto end; 422 } 423 424 switch (device >> 8) { 425 case 202: /* xvd */ 426 dsk = (device >> 4) & 0xf; 427 lun = device & 0xf; 428 break; 429 case 8: /* sd */ 430 dsk = device & (~0xf); 431 lun = device & 0xf; 432 break; 433 case 3: /* hd, disk 0..1 */ 434 dsk = device & (~0x3f); 435 lun = device & 0x3f; 436 break; 437 case 22: /* hd, disk 2..3 */ 438 dsk = device & (~0x3f); 439 lun = device & 0x3f; 440 break; 441 default: 442 return (0); 443 } 444 end: 445 *disk = dsk; 446 *plun = lun; 447 return (ret); 448 } 449 450 static int 451 disk_callback_xvmd(di_minor_t minor, di_node_t node) 452 { 453 char *addr; 454 char disk[16]; 455 uint_t targ; 456 uint_t dsk, lun; 457 int res; 458 459 addr = di_bus_addr(node); 460 targ = strtol(addr, (char **)NULL, 10); 461 462 res = decode_xen_device(targ, &dsk, &lun); 463 464 /* HVM device names are generated using the standard generator */ 465 466 if (res == 1) 467 (void) snprintf(disk, sizeof (disk), "d%d", dsk); 468 else if (res == 2) 469 (void) snprintf(disk, sizeof (disk), "t%dd%d", dsk, lun); 470 else { 471 devfsadm_errprint("%s: invalid disk device number (%s)\n", 472 modname, addr); 473 return (DEVFSADM_CONTINUE); 474 } 475 disk_common(minor, node, disk, 0); 476 return (DEVFSADM_CONTINUE); 477 478 } 479 480 /* 481 * This function is called for every disk minor node. 482 * Calls enumerate to assign a logical controller number, and 483 * then devfsadm_mklink to make the link. 484 */ 485 static void 486 disk_common(di_minor_t minor, di_node_t node, char *disk, int flags) 487 { 488 char l_path[PATH_MAX + 1]; 489 char sec_path[PATH_MAX + 1]; 490 char stale_re[DISK_SUBPATH_MAX]; 491 char *dir; 492 char slice[4]; 493 char *mn; 494 char *ctrl; 495 char *nt = NULL; 496 int *int_prop; 497 int nflags = 0; 498 #if defined(__i386) || defined(__amd64) 499 char mn_copy[4]; 500 char *part; 501 int part_num; 502 #endif 503 504 mn = di_minor_name(minor); 505 if (strstr(mn, ",raw")) { 506 dir = "rdsk"; 507 #if defined(__i386) || defined(__amd64) 508 (void) strncpy(mn_copy, mn, 4); 509 part = strtok(mn_copy, ","); 510 #endif 511 } else { 512 dir = "dsk"; 513 #if defined(__i386) || defined(__amd64) 514 part = mn; 515 #endif 516 } 517 518 #if defined(__i386) || defined(__amd64) 519 /* 520 * The following is a table describing the allocation of 521 * minor numbers, minor names and /dev/dsk names for partitions 522 * and slices on x86 systems. 523 * 524 * Minor Number Minor Name /dev/dsk name 525 * --------------------------------------------- 526 * 0 to 15 "a" to "p" s0 to s15 527 * 16 "q" p0 528 * 17 to 20 "r" to "u" p1 to p4 529 * 21 to 52 "p5" to "p36" p5 to p36 530 * 531 */ 532 part_num = atoi(part + 1); 533 534 if ((mn[0] == 'p') && (part_num >= 5)) { 535 /* logical drive */ 536 (void) snprintf(slice, 4, "%s", part); 537 } else { 538 #endif 539 if (mn[0] < 'q') { 540 (void) sprintf(slice, "s%d", mn[0] - 'a'); 541 } else if (strncmp(mn, MN_EFI, 2) != 0) { 542 (void) sprintf(slice, "p%d", mn[0] - 'q'); 543 } else { 544 /* For EFI label */ 545 (void) sprintf(slice, SLICE_EFI); 546 } 547 #if defined(__i386) || defined(__amd64) 548 } 549 #endif 550 551 nflags = 0; 552 if (system_labeled) { 553 nt = di_minor_nodetype(minor); 554 if ((nt != NULL) && 555 ((strcmp(nt, DDI_NT_CD) == 0) || 556 (strcmp(nt, DDI_NT_CD_CHAN) == 0) || 557 (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) { 558 nflags = DA_ADD|DA_CD; 559 } 560 } 561 562 if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) { 563 devfsadm_print(disk_mid, "Reserved link exists. Not " 564 "creating links for slice %s\n", slice); 565 return; 566 } 567 568 if (NULL == (ctrl = diskctrl(node, minor))) 569 return; 570 571 (void) strcpy(l_path, dir); 572 (void) strcat(l_path, "/c"); 573 (void) strcat(l_path, ctrl); 574 (void) strcat(l_path, disk); 575 576 /* 577 * If switching between SMI and EFI label or vice versa 578 * cleanup the previous label's devlinks. 579 */ 580 if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) { 581 char *s, tpath[PATH_MAX + 1]; 582 struct stat sb; 583 584 s = l_path + strlen(l_path); 585 (void) strcat(l_path, (*mn == *(MN_SMI)) 586 ? SLICE_EFI : SLICE_SMI); 587 /* 588 * Attempt the remove only if the stale link exists 589 */ 590 (void) snprintf(tpath, sizeof (tpath), "%s/dev/%s", 591 devfsadm_root_path(), l_path); 592 if (lstat(tpath, &sb) != -1) 593 devfsadm_rm_all(l_path); 594 *s = '\0'; 595 } 596 (void) strcat(l_path, slice); 597 598 (void) devfsadm_mklink(l_path, node, minor, nflags); 599 600 /* secondary links for removable and hotpluggable devices */ 601 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media", 602 &int_prop) >= 0) { 603 (void) strcpy(sec_path, "removable-media/"); 604 (void) strcat(sec_path, l_path); 605 (void) devfsadm_secondary_link(sec_path, l_path, 0); 606 } 607 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable", 608 &int_prop) >= 0) { 609 (void) strcpy(sec_path, "hotpluggable/"); 610 (void) strcat(sec_path, l_path); 611 (void) devfsadm_secondary_link(sec_path, l_path, 0); 612 } 613 614 if ((flags & RM_STALE) == RM_STALE) { 615 (void) strcpy(stale_re, "^"); 616 (void) strcat(stale_re, dir); 617 (void) strcat(stale_re, "/c"); 618 (void) strcat(stale_re, ctrl); 619 (void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$"); 620 /* 621 * optimizations are made inside of devfsadm_rm_stale_links 622 * instead of before calling the function, as it always 623 * needs to add the valid link to the cache. 624 */ 625 devfsadm_rm_stale_links(stale_re, l_path, node, minor); 626 } 627 628 free(ctrl); 629 } 630 631 632 /* index of enumeration rule applicable to this module */ 633 #define RULE_INDEX 0 634 635 static char * 636 diskctrl(di_node_t node, di_minor_t minor) 637 { 638 char path[PATH_MAX + 1]; 639 char *devfspath; 640 char *buf, *mn; 641 642 devfsadm_enumerate_t rules[3] = { 643 {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT}, 644 {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR}, 645 {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT} 646 }; 647 648 mn = di_minor_name(minor); 649 650 if ((devfspath = di_devfs_path(node)) == NULL) { 651 return (NULL); 652 } 653 (void) strcpy(path, devfspath); 654 (void) strcat(path, ":"); 655 (void) strcat(path, mn); 656 di_devfs_path_free(devfspath); 657 658 /* 659 * Use controller component of disk path 660 */ 661 if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) == 662 DEVFSADM_MULTIPLE) { 663 664 /* 665 * We failed because there are multiple logical controller 666 * numbers for a single physical controller. If we use node 667 * name also in the match it should fix this and only find one 668 * logical controller. (See 4045879). 669 * NOTE: Rules for controllers are not changed, as there is 670 * no unique controller number for them in this case. 671 * 672 * MATCH_UNCACHED flag is private to the "disks" and "sgen" 673 * modules. NOT to be used by other modules. 674 */ 675 676 rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */ 677 rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */ 678 if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) { 679 return (NULL); 680 } 681 } 682 683 return (buf); 684 } 685 686 typedef struct dvlist { 687 char *dv_link; 688 struct dvlist *dv_next; 689 } dvlist_t; 690 691 static void 692 free_dvlist(dvlist_t **pp) 693 { 694 dvlist_t *entry; 695 696 while (*pp) { 697 entry = *pp; 698 *pp = entry->dv_next; 699 assert(entry->dv_link); 700 free(entry->dv_link); 701 free(entry); 702 } 703 } 704 static int 705 dvlink_cb(di_devlink_t devlink, void *arg) 706 { 707 char *path; 708 char *can_path; 709 dvlist_t **pp = (dvlist_t **)arg; 710 dvlist_t *entry = NULL; 711 712 entry = calloc(1, sizeof (dvlist_t)); 713 if (entry == NULL) { 714 devfsadm_errprint("%s: calloc failed\n", modname); 715 goto error; 716 } 717 718 path = (char *)di_devlink_path(devlink); 719 assert(path); 720 if (path == NULL) { 721 devfsadm_errprint("%s: di_devlink_path() returned NULL\n", 722 modname); 723 goto error; 724 } 725 726 devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n", 727 modname, path); 728 729 /* 730 * Return linkname in canonical form i.e. without the 731 * "/dev/" prefix 732 */ 733 can_path = strstr(path, "/dev/"); 734 if (can_path == NULL) { 735 devfsadm_errprint("%s: devlink path %s has no /dev/\n", 736 modname, path); 737 goto error; 738 } 739 740 entry->dv_link = s_strdup(can_path + strlen("/dev/")); 741 entry->dv_next = *pp; 742 *pp = entry; 743 744 return (DI_WALK_CONTINUE); 745 746 error: 747 free(entry); 748 free_dvlist(pp); 749 *pp = NULL; 750 return (DI_WALK_TERMINATE); 751 } 752 753 /* 754 * Returns success only if all goes well. If there is no matching reserved link 755 * or if there is an error, we assume no match. It is better to err on the side 756 * of caution by creating extra links than to miss out creating a required link. 757 */ 758 static int 759 reserved_links_exist(di_node_t node, di_minor_t minor, int nflags) 760 { 761 di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache(); 762 char phys_path[PATH_MAX]; 763 char *minor_path; 764 dvlist_t *head; 765 dvlist_t *entry; 766 char *s; 767 char l[PATH_MAX]; 768 int switch_link = 0; 769 char *mn = di_minor_name(minor); 770 771 if (dvlink_cache == NULL || mn == NULL) { 772 devfsadm_errprint("%s: No minor or devlink cache\n", modname); 773 return (DEVFSADM_FAILURE); 774 } 775 776 if (!devfsadm_have_reserved()) { 777 devfsadm_print(disk_mid, "%s: No reserved links\n", modname); 778 return (DEVFSADM_FAILURE); 779 } 780 781 minor_path = di_devfs_minor_path(minor); 782 if (minor_path == NULL) { 783 devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname); 784 return (DEVFSADM_FAILURE); 785 } 786 787 (void) strlcpy(phys_path, minor_path, sizeof (phys_path)); 788 789 di_devfs_path_free(minor_path); 790 791 head = NULL; 792 (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path, 793 DI_PRIMARY_LINK, &head, dvlink_cb); 794 795 /* 796 * We may be switching between EFI label and SMI label in which case 797 * we only have minors of the other type. 798 */ 799 if (head == NULL && (*mn == *(MN_SMI) || 800 (strncmp(mn, MN_EFI, 2) == 0))) { 801 devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. " 802 "Trying another label\n", modname, mn); 803 s = strrchr(phys_path, ':'); 804 if (s == NULL) { 805 devfsadm_errprint("%s: invalid minor path: %s\n", 806 modname, phys_path); 807 return (DEVFSADM_FAILURE); 808 } 809 (void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path), 810 "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI, 811 strstr(s, ",raw") ? ",raw" : ""); 812 (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, 813 phys_path, DI_PRIMARY_LINK, &head, dvlink_cb); 814 } 815 816 if (head == NULL) { 817 devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n", 818 modname, phys_path); 819 /* no links on disk */ 820 return (DEVFSADM_FAILURE); 821 } 822 823 /* 824 * It suffices to use 1 link to this minor, since 825 * we are matching with reserved IDs on the basis of 826 * the controller number which will be the same for 827 * all links to this minor. 828 */ 829 if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) { 830 /* not reserved links */ 831 devfsadm_print(disk_mid, "%s: devlink %s and its minor " 832 "are NOT reserved\n", modname, head->dv_link); 833 free_dvlist(&head); 834 return (DEVFSADM_FAILURE); 835 } 836 837 devfsadm_print(disk_mid, "%s: devlink %s and its minor are on " 838 "reserved list\n", modname, head->dv_link); 839 840 /* 841 * Switch between SMI and EFI labels if required 842 */ 843 switch_link = 0; 844 if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) { 845 for (entry = head; entry; entry = entry->dv_next) { 846 s = strrchr(entry->dv_link, '/'); 847 assert(s); 848 if (s == NULL) { 849 devfsadm_errprint("%s: disk link %s has no " 850 "directory\n", modname, entry->dv_link); 851 continue; 852 } 853 if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) { 854 (void) snprintf(l, sizeof (l), "%s%s", 855 entry->dv_link, SLICE_SMI); 856 switch_link = 1; 857 devfsadm_print(disk_mid, "%s: switching " 858 "reserved link from EFI to SMI label. " 859 "New link is %s\n", modname, l); 860 } else if (strncmp(mn, MN_EFI, 2) == 0 && 861 (s = strchr(s, 's'))) { 862 *s = '\0'; 863 (void) snprintf(l, sizeof (l), "%s", 864 entry->dv_link); 865 *s = 's'; 866 switch_link = 1; 867 devfsadm_print(disk_mid, "%s: switching " 868 "reserved link from SMI to EFI label. " 869 "New link is %s\n", modname, l); 870 } 871 if (switch_link) { 872 devfsadm_print(disk_mid, "%s: switching " 873 "link: deleting %s and creating %s\n", 874 modname, entry->dv_link, l); 875 devfsadm_rm_link(entry->dv_link); 876 (void) devfsadm_mklink(l, node, minor, nflags); 877 } 878 } 879 } 880 free_dvlist(&head); 881 882 /* 883 * return SUCCESS to indicate that new links to this minor should not 884 * be created so that only compatibility links to this minor remain. 885 */ 886 return (DEVFSADM_SUCCESS); 887 } 888