Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / cmd / devfsadm / disk_link.c
blobcfc87d712faa73608b96cac15d64fd716edeff00
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2016 Toomas Soome <tsoome@me.com>
24 * Copyright 2017 Nexenta Systems, Inc.
25 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #include <devfsadm.h>
30 #include <stdio.h>
31 #include <strings.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <sys/int_fmtio.h>
37 #include <sys/stat.h>
38 #include <sys/scsi/scsi_address.h>
39 #include <sys/libdevid.h>
40 #include <sys/lofi.h>
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)
48 #define SLICE_SMI "s7"
49 #define SLICE_EFI ""
51 #define MN_SMI "h"
52 #define MN_EFI "wd"
53 #define ASCIIWWNSIZE 255
54 #if defined(__i386) || defined(__amd64)
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)
59 #define NUM_MINORS_PER_INSTANCE 64
60 #endif
63 static int disk_callback_chan(di_minor_t minor, di_node_t node);
64 static int disk_callback_nchan(di_minor_t minor, di_node_t node);
65 static int disk_callback_blkdev(di_minor_t minor, di_node_t node);
66 static int disk_callback_wwn(di_minor_t minor, di_node_t node);
67 static int disk_callback_xvmd(di_minor_t minor, di_node_t node);
68 static int disk_callback_fabric(di_minor_t minor, di_node_t node);
69 static int disk_callback_sas(di_minor_t minor, di_node_t node);
70 static void disk_common(di_minor_t minor, di_node_t node, char *disk,
71 int flags);
72 static char *diskctrl(di_node_t node, di_minor_t minor);
73 static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
74 static void disk_rm_lofi_all(char *file);
77 static devfsadm_create_t disk_cbt[] = {
78 { "disk", DDI_NT_BLOCK, NULL,
79 TYPE_EXACT, ILEVEL_0, disk_callback_nchan
81 { "disk", DDI_NT_BLOCK_CHAN, NULL,
82 TYPE_EXACT, ILEVEL_0, disk_callback_chan
84 { "disk", DDI_NT_BLOCK_BLKDEV, NULL,
85 TYPE_EXACT, ILEVEL_0, disk_callback_blkdev
87 { "disk", DDI_NT_BLOCK_FABRIC, NULL,
88 TYPE_EXACT, ILEVEL_0, disk_callback_fabric
90 { "disk", DDI_NT_BLOCK_WWN, NULL,
91 TYPE_EXACT, ILEVEL_0, disk_callback_wwn
93 { "disk", DDI_NT_BLOCK_SAS, NULL,
94 TYPE_EXACT, ILEVEL_0, disk_callback_sas
96 { "disk", DDI_NT_CD, NULL,
97 TYPE_EXACT, ILEVEL_0, disk_callback_nchan
99 { "disk", DDI_NT_CD_CHAN, NULL,
100 TYPE_EXACT, ILEVEL_0, disk_callback_chan
102 { "disk", DDI_NT_BLOCK_XVMD, NULL,
103 TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
105 { "disk", DDI_NT_CD_XVMD, NULL,
106 TYPE_EXACT, ILEVEL_0, disk_callback_xvmd
110 DEVFSADM_CREATE_INIT_V0(disk_cbt);
113 * HOT auto cleanup of disks is done for lofi devices only.
115 static devfsadm_remove_t disk_remove_cbt[] = {
116 { "disk", DISK_LINK_RE, RM_HOT | RM_POST | RM_ALWAYS,
117 ILEVEL_0, disk_rm_lofi_all
119 { "disk", DISK_LINK_RE, RM_POST,
120 ILEVEL_0, devfsadm_rm_all
124 DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
126 static devlink_re_t disks_re_array[] = {
127 {"^r?dsk/c([0-9]+)", 1},
128 {"^cfg/c([0-9]+)$", 1},
129 {"^scsi/.+/c([0-9]+)", 1},
130 {NULL}
133 static char *disk_mid = "disk_mid";
134 static char *modname = "disk_link";
137 * Check if link is from lofi by checking path from readlink().
139 static int
140 is_lofi_disk(char *file)
142 char buf[PATH_MAX + 1];
143 char filepath[PATH_MAX];
144 char *ptr;
145 ssize_t size;
147 size = snprintf(filepath, sizeof (filepath), "%s/dev/%s",
148 devfsadm_root_path(), file);
149 if (size > sizeof (filepath))
150 return (0);
152 size = readlink(filepath, buf, sizeof (buf) - 1);
153 if (size == -1)
154 return (0);
155 buf[size] = '\0';
156 ptr = strchr(buf, '@');
157 if (ptr == NULL)
158 return (0);
159 ptr[1] = '\0';
160 if (strcmp(buf, "../../devices/pseudo/lofi@") != 0)
161 return (0);
162 return (1);
166 * Wrapper around devfsadm_rm_link() for lofi devices.
168 static void disk_rm_lofi_all(char *file)
170 if (is_lofi_disk(file))
171 devfsadm_rm_link(file);
175 minor_init()
177 devfsadm_print(disk_mid,
178 "%s: minor_init(): Creating disks reserved ID cache\n",
179 modname);
180 return (devfsadm_reserve_id_cache(disks_re_array, NULL));
183 static int
184 disk_callback_chan(di_minor_t minor, di_node_t node)
186 char *addr;
187 char disk[23];
188 char *driver;
189 uint_t targ = 0;
190 uint_t lun = 0;
192 driver = di_driver_name(node);
193 if (strcmp(driver, LOFI_DRIVER_NAME) != 0) {
194 addr = di_bus_addr(node);
195 (void) sscanf(addr, "%X,%X", &targ, &lun);
196 } else {
197 targ = di_instance(node);
200 (void) snprintf(disk, sizeof (disk), "t%dd%d", targ, lun);
201 disk_common(minor, node, disk, 0);
202 return (DEVFSADM_CONTINUE);
206 static int
207 disk_callback_nchan(di_minor_t minor, di_node_t node)
209 char *addr;
210 char disk[10];
211 uint_t lun;
213 addr = di_bus_addr(node);
214 (void) sscanf(addr, "%X", &lun);
215 (void) sprintf(disk, "d%d", lun);
216 disk_common(minor, node, disk, 0);
217 return (DEVFSADM_CONTINUE);
221 static int
222 disk_callback_blkdev(di_minor_t minor, di_node_t node)
224 char *addr;
225 char disk[DISK_SUBPATH_MAX];
226 uint64_t eui64;
227 uint_t lun = 0;
229 addr = di_bus_addr(node);
230 (void) sscanf(addr, "w%016"PRIx64",%X", &eui64, &lun);
231 (void) snprintf(disk, DISK_SUBPATH_MAX, "t%016"PRIX64"d%d", eui64, lun);
232 disk_common(minor, node, disk, RM_STALE);
233 return (DEVFSADM_CONTINUE);
236 static int
237 disk_callback_wwn(di_minor_t minor, di_node_t node)
239 char disk[10];
240 int lun;
241 int targ;
242 int *intp;
244 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_TARGET,
245 &intp) <= 0) {
246 return (DEVFSADM_CONTINUE);
248 targ = *intp;
249 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, SCSI_ADDR_PROP_LUN,
250 &intp) <= 0) {
251 lun = 0;
252 } else {
253 lun = *intp;
255 (void) sprintf(disk, "t%dd%d", targ, lun);
257 disk_common(minor, node, disk, RM_STALE);
259 return (DEVFSADM_CONTINUE);
262 static int
263 disk_callback_fabric(di_minor_t minor, di_node_t node)
265 char disk[DISK_SUBPATH_MAX];
266 int lun;
267 int count;
268 int *intp;
269 uchar_t *str;
270 uchar_t *wwn;
271 uchar_t ascii_wwn[ASCIIWWNSIZE];
273 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
274 "client-guid", (char **)&wwn) > 0) {
275 if (strlcpy((char *)ascii_wwn, (char *)wwn,
276 sizeof (ascii_wwn)) >= sizeof (ascii_wwn)) {
277 devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
278 strlen((char *)wwn));
279 return (DEVFSADM_CONTINUE);
281 lun = 0;
282 } else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
283 "port-wwn", &wwn) > 0) {
284 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
285 SCSI_ADDR_PROP_LUN, &intp) > 0) {
286 lun = *intp;
287 } else {
288 lun = 0;
291 for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
292 (void) sprintf((caddr_t)str, "%02x", wwn[count]);
294 *str = '\0';
295 } else {
296 return (DEVFSADM_CONTINUE);
299 for (str = ascii_wwn; *str != '\0'; str++) {
300 *str = DISK_LINK_TO_UPPER(*str);
303 (void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
305 disk_common(minor, node, disk, RM_STALE);
307 return (DEVFSADM_CONTINUE);
310 static int
311 disk_callback_sas(di_minor_t minor, di_node_t node)
313 char disk[DISK_SUBPATH_MAX];
314 int lun64_found = 0;
315 scsi_lun64_t lun64, sl;
316 scsi_lun_t lun;
317 int64_t *lun64p;
318 uint64_t wwn;
319 int *intp;
320 char *tgt_port;
321 uchar_t addr_method;
323 /* Get lun property */
324 if (di_prop_lookup_int64(DDI_DEV_T_ANY, node,
325 SCSI_ADDR_PROP_LUN64, &lun64p) > 0) {
326 if (*lun64p != SCSI_LUN64_ILLEGAL) {
327 lun64_found = 1;
328 lun64 = (uint64_t)*lun64p;
331 if ((!lun64_found) && (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
332 SCSI_ADDR_PROP_LUN, &intp) > 0)) {
333 lun64 = (uint64_t)*intp;
336 lun = scsi_lun64_to_lun(lun64);
338 addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
340 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
341 SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
342 (void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
343 if ((addr_method == SCSI_LUN_AM_PDEV) &&
344 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
345 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
346 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
347 (void) snprintf(disk, DISK_SUBPATH_MAX,
348 "t%"PRIX64"d%"PRId64, wwn, lun64);
349 } else if ((addr_method == SCSI_LUN_AM_FLAT) &&
350 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
351 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
352 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
353 sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
354 (void) snprintf(disk, DISK_SUBPATH_MAX,
355 "t%"PRIX64"d%"PRIX16, wwn, sl);
356 } else {
357 (void) snprintf(disk, DISK_SUBPATH_MAX,
358 "t%"PRIX64"d%"PRIX64, wwn, lun64);
360 } else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
361 SCSI_ADDR_PROP_SATA_PHY, &intp) > 0) {
362 /* Use phy format naming, for SATA devices without wwn */
363 if ((addr_method == SCSI_LUN_AM_PDEV) &&
364 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
365 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
366 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
367 (void) snprintf(disk, DISK_SUBPATH_MAX,
368 "t%dd%"PRId64, *intp, lun64);
369 } else if ((addr_method == SCSI_LUN_AM_FLAT) &&
370 (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
371 (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
372 (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
373 sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
374 (void) snprintf(disk, DISK_SUBPATH_MAX,
375 "t%dd%"PRIX16, *intp, sl);
376 } else {
377 (void) snprintf(disk, DISK_SUBPATH_MAX,
378 "t%dd%"PRIX64, *intp, lun64);
380 } else {
381 return (DEVFSADM_CONTINUE);
384 disk_common(minor, node, disk, RM_STALE);
386 return (DEVFSADM_CONTINUE);
390 * xVM virtual block device
392 * Xen passes device number in the following format:
394 * 1 << 28 | disk << 8 | partition xvd, disks or partitions 16 onwards
395 * 202 << 8 | disk << 4 | partition xvd, disks and partitions up to 15
396 * 8 << 8 | disk << 4 | partition sd, disks and partitions up to 15
397 * 3 << 8 | disk << 6 | partition hd, disks 0..1, partitions 0..63
398 * 22 << 8 | (disk-2) << 6 | partition hd, disks 2..3, partitions 0..63
399 * 2 << 28 onwards reserved for future use
400 * other values less than 1 << 28 deprecated / reserved
402 * The corresponding /dev/dsk name can be:
404 * c0tYdX
406 * where Y,X >= 0.
408 * For PV guests using the legacy naming (0, 1, 2, ...)
409 * the disk names created will be c0d[0..767].
412 #define HD_BASE (3 << 8)
413 #define XEN_EXT_SHIFT (28)
416 * Return: Number of parsed and written parameters
418 static int
419 decode_xen_device(uint_t device, uint_t *disk, uint_t *plun)
421 uint_t dsk, lun = 0;
422 int ret = 1;
424 if ((device >> XEN_EXT_SHIFT) > 1)
425 return (0);
427 if (device < HD_BASE) {
428 /* legacy device address */
429 dsk = device;
430 goto end;
433 ret = 2;
434 if (device & (1 << XEN_EXT_SHIFT)) {
435 /* extended */
436 dsk = device & (~0xff);
437 lun = device & 0xff;
438 goto end;
441 switch (device >> 8) {
442 case 202: /* xvd */
443 dsk = (device >> 4) & 0xf;
444 lun = device & 0xf;
445 break;
446 case 8: /* sd */
447 dsk = device & (~0xf);
448 lun = device & 0xf;
449 break;
450 case 3: /* hd, disk 0..1 */
451 dsk = (device >> 6) & 0x1;
452 lun = device & 0x3f;
453 break;
454 case 22: /* hd, disk 2..3 */
455 dsk = ((device >> 6) & 0x1) + 2;
456 lun = device & 0x3f;
457 break;
458 default:
459 return (0);
461 end:
462 *disk = dsk;
463 *plun = lun;
464 return (ret);
467 static int
468 disk_callback_xvmd(di_minor_t minor, di_node_t node)
470 char *addr;
471 char disk[16];
472 uint_t targ;
473 uint_t dsk, lun;
474 int res;
476 addr = di_bus_addr(node);
477 targ = strtol(addr, (char **)NULL, 10);
479 res = decode_xen_device(targ, &dsk, &lun);
481 /* HVM device names are generated using the standard generator */
483 if (res == 1)
484 (void) snprintf(disk, sizeof (disk), "d%d", dsk);
485 else if (res == 2)
486 (void) snprintf(disk, sizeof (disk), "t%dd%d", dsk, lun);
487 else {
488 devfsadm_errprint("%s: invalid disk device number (%s)\n",
489 modname, addr);
490 return (DEVFSADM_CONTINUE);
492 disk_common(minor, node, disk, 0);
493 return (DEVFSADM_CONTINUE);
497 * This function is called for every disk minor node.
498 * Calls enumerate to assign a logical controller number, and
499 * then devfsadm_mklink to make the link.
501 static void
502 disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
504 char l_path[PATH_MAX + 1];
505 char sec_path[PATH_MAX + 1];
506 char stale_re[DISK_SUBPATH_MAX];
507 char *dir;
508 char slice[4];
509 char *mn;
510 char *ctrl;
511 int *int_prop;
512 int nflags = 0;
513 #if defined(__i386) || defined(__amd64)
514 char mn_copy[4];
515 char *part;
516 int part_num;
517 #endif
519 mn = di_minor_name(minor);
520 if (strstr(mn, ",raw")) {
521 dir = "rdsk";
522 #if defined(__i386) || defined(__amd64)
523 (void) strncpy(mn_copy, mn, 4);
524 part = strtok(mn_copy, ",");
525 #endif
526 } else {
527 dir = "dsk";
528 #if defined(__i386) || defined(__amd64)
529 part = mn;
530 #endif
533 #if defined(__i386) || defined(__amd64)
535 * The following is a table describing the allocation of
536 * minor numbers, minor names and /dev/dsk names for partitions
537 * and slices on x86 systems.
539 * Minor Number Minor Name /dev/dsk name
540 * ---------------------------------------------
541 * 0 to 15 "a" to "p" s0 to s15
542 * 16 "q" p0
543 * 17 to 20 "r" to "u" p1 to p4
544 * 21 to 52 "p5" to "p36" p5 to p36
547 part_num = atoi(part + 1);
549 if ((mn[0] == 'p') && (part_num >= 5)) {
550 /* logical drive */
551 (void) snprintf(slice, 4, "%s", part);
552 } else {
553 #endif
554 if (mn[0] < 'q') {
555 (void) sprintf(slice, "s%d", mn[0] - 'a');
556 } else if (strncmp(mn, MN_EFI, 2) != 0) {
557 (void) sprintf(slice, "p%d", mn[0] - 'q');
558 } else {
559 /* For EFI label */
560 (void) sprintf(slice, SLICE_EFI);
562 #if defined(__i386) || defined(__amd64)
564 #endif
566 nflags = 0;
567 if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
568 devfsadm_print(disk_mid, "Reserved link exists. Not "
569 "creating links for slice %s\n", slice);
570 return;
573 if (NULL == (ctrl = diskctrl(node, minor)))
574 return;
576 (void) strcpy(l_path, dir);
577 (void) strcat(l_path, "/c");
578 (void) strcat(l_path, ctrl);
579 (void) strcat(l_path, disk);
582 * If switching between SMI and EFI label or vice versa
583 * cleanup the previous label's devlinks.
585 if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
586 char *s, tpath[PATH_MAX + 1];
587 struct stat sb;
589 s = l_path + strlen(l_path);
590 (void) strcat(l_path, (*mn == *(MN_SMI))
591 ? SLICE_EFI : SLICE_SMI);
593 * Attempt the remove only if the stale link exists
595 (void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
596 devfsadm_root_path(), l_path);
597 if (lstat(tpath, &sb) != -1)
598 devfsadm_rm_all(l_path);
599 *s = '\0';
601 (void) strcat(l_path, slice);
603 (void) devfsadm_mklink(l_path, node, minor, nflags);
605 /* secondary links for removable and hotpluggable devices */
606 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
607 &int_prop) >= 0) {
608 (void) strcpy(sec_path, "removable-media/");
609 (void) strcat(sec_path, l_path);
610 (void) devfsadm_secondary_link(sec_path, l_path, 0);
612 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
613 &int_prop) >= 0) {
614 (void) strcpy(sec_path, "hotpluggable/");
615 (void) strcat(sec_path, l_path);
616 (void) devfsadm_secondary_link(sec_path, l_path, 0);
619 if ((flags & RM_STALE) == RM_STALE) {
620 (void) strcpy(stale_re, "^");
621 (void) strcat(stale_re, dir);
622 (void) strcat(stale_re, "/c");
623 (void) strcat(stale_re, ctrl);
624 (void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
626 * optimizations are made inside of devfsadm_rm_stale_links
627 * instead of before calling the function, as it always
628 * needs to add the valid link to the cache.
630 devfsadm_rm_stale_links(stale_re, l_path, node, minor);
633 free(ctrl);
637 /* index of enumeration rule applicable to this module */
638 #define RULE_INDEX 0
640 static char *
641 diskctrl(di_node_t node, di_minor_t minor)
643 char path[PATH_MAX + 1];
644 char *devfspath;
645 char *buf, *mn;
647 devfsadm_enumerate_t rules[3] = {
648 {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
649 {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
650 {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
653 mn = di_minor_name(minor);
655 if ((devfspath = di_devfs_path(node)) == NULL) {
656 return (NULL);
658 (void) strcpy(path, devfspath);
659 (void) strcat(path, ":");
660 (void) strcat(path, mn);
661 di_devfs_path_free(devfspath);
664 * Use controller component of disk path
666 if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
667 DEVFSADM_MULTIPLE) {
670 * We failed because there are multiple logical controller
671 * numbers for a single physical controller. If we use node
672 * name also in the match it should fix this and only find one
673 * logical controller. (See 4045879).
674 * NOTE: Rules for controllers are not changed, as there is
675 * no unique controller number for them in this case.
677 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
678 * modules. NOT to be used by other modules.
681 rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
682 rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
683 if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
684 return (NULL);
688 return (buf);
691 typedef struct dvlist {
692 char *dv_link;
693 struct dvlist *dv_next;
694 } dvlist_t;
696 static void
697 free_dvlist(dvlist_t **pp)
699 dvlist_t *entry;
701 while (*pp) {
702 entry = *pp;
703 *pp = entry->dv_next;
704 assert(entry->dv_link);
705 free(entry->dv_link);
706 free(entry);
709 static int
710 dvlink_cb(di_devlink_t devlink, void *arg)
712 char *path;
713 char *can_path;
714 dvlist_t **pp = (dvlist_t **)arg;
715 dvlist_t *entry = NULL;
717 entry = calloc(1, sizeof (dvlist_t));
718 if (entry == NULL) {
719 devfsadm_errprint("%s: calloc failed\n", modname);
720 goto error;
723 path = (char *)di_devlink_path(devlink);
724 assert(path);
725 if (path == NULL) {
726 devfsadm_errprint("%s: di_devlink_path() returned NULL\n",
727 modname);
728 goto error;
731 devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n",
732 modname, path);
735 * Return linkname in canonical form i.e. without the
736 * "/dev/" prefix
738 can_path = strstr(path, "/dev/");
739 if (can_path == NULL) {
740 devfsadm_errprint("%s: devlink path %s has no /dev/\n",
741 modname, path);
742 goto error;
745 entry->dv_link = s_strdup(can_path + strlen("/dev/"));
746 entry->dv_next = *pp;
747 *pp = entry;
749 return (DI_WALK_CONTINUE);
751 error:
752 free(entry);
753 free_dvlist(pp);
754 *pp = NULL;
755 return (DI_WALK_TERMINATE);
759 * Returns success only if all goes well. If there is no matching reserved link
760 * or if there is an error, we assume no match. It is better to err on the side
761 * of caution by creating extra links than to miss out creating a required link.
763 static int
764 reserved_links_exist(di_node_t node, di_minor_t minor, int nflags)
766 di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache();
767 char phys_path[PATH_MAX];
768 char *minor_path;
769 dvlist_t *head;
770 dvlist_t *entry;
771 char *s;
772 char l[PATH_MAX];
773 int switch_link = 0;
774 char *mn = di_minor_name(minor);
776 if (dvlink_cache == NULL || mn == NULL) {
777 devfsadm_errprint("%s: No minor or devlink cache\n", modname);
778 return (DEVFSADM_FAILURE);
781 if (!devfsadm_have_reserved()) {
782 devfsadm_print(disk_mid, "%s: No reserved links\n", modname);
783 return (DEVFSADM_FAILURE);
786 minor_path = di_devfs_minor_path(minor);
787 if (minor_path == NULL) {
788 devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname);
789 return (DEVFSADM_FAILURE);
792 (void) strlcpy(phys_path, minor_path, sizeof (phys_path));
794 di_devfs_path_free(minor_path);
796 head = NULL;
797 (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path,
798 DI_PRIMARY_LINK, &head, dvlink_cb);
801 * We may be switching between EFI label and SMI label in which case
802 * we only have minors of the other type.
804 if (head == NULL && (*mn == *(MN_SMI) ||
805 (strncmp(mn, MN_EFI, 2) == 0))) {
806 devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. "
807 "Trying another label\n", modname, mn);
808 s = strrchr(phys_path, ':');
809 if (s == NULL) {
810 devfsadm_errprint("%s: invalid minor path: %s\n",
811 modname, phys_path);
812 return (DEVFSADM_FAILURE);
814 (void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path),
815 "%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI,
816 strstr(s, ",raw") ? ",raw" : "");
817 (void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE,
818 phys_path, DI_PRIMARY_LINK, &head, dvlink_cb);
821 if (head == NULL) {
822 devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n",
823 modname, phys_path);
824 /* no links on disk */
825 return (DEVFSADM_FAILURE);
829 * It suffices to use 1 link to this minor, since
830 * we are matching with reserved IDs on the basis of
831 * the controller number which will be the same for
832 * all links to this minor.
834 if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) {
835 /* not reserved links */
836 devfsadm_print(disk_mid, "%s: devlink %s and its minor "
837 "are NOT reserved\n", modname, head->dv_link);
838 free_dvlist(&head);
839 return (DEVFSADM_FAILURE);
842 devfsadm_print(disk_mid, "%s: devlink %s and its minor are on "
843 "reserved list\n", modname, head->dv_link);
846 * Switch between SMI and EFI labels if required
848 switch_link = 0;
849 if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
850 for (entry = head; entry; entry = entry->dv_next) {
851 s = strrchr(entry->dv_link, '/');
852 assert(s);
853 if (s == NULL) {
854 devfsadm_errprint("%s: disk link %s has no "
855 "directory\n", modname, entry->dv_link);
856 continue;
858 if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) {
859 (void) snprintf(l, sizeof (l), "%s%s",
860 entry->dv_link, SLICE_SMI);
861 switch_link = 1;
862 devfsadm_print(disk_mid, "%s: switching "
863 "reserved link from EFI to SMI label. "
864 "New link is %s\n", modname, l);
865 } else if (strncmp(mn, MN_EFI, 2) == 0 &&
866 (s = strchr(s, 's'))) {
867 *s = '\0';
868 (void) snprintf(l, sizeof (l), "%s",
869 entry->dv_link);
870 *s = 's';
871 switch_link = 1;
872 devfsadm_print(disk_mid, "%s: switching "
873 "reserved link from SMI to EFI label. "
874 "New link is %s\n", modname, l);
876 if (switch_link) {
877 devfsadm_print(disk_mid, "%s: switching "
878 "link: deleting %s and creating %s\n",
879 modname, entry->dv_link, l);
880 devfsadm_rm_link(entry->dv_link);
881 (void) devfsadm_mklink(l, node, minor, nflags);
885 free_dvlist(&head);
888 * return SUCCESS to indicate that new links to this minor should not
889 * be created so that only compatibility links to this minor remain.
891 return (DEVFSADM_SUCCESS);