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