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