xref: /illumos-gate/usr/src/cmd/devfsadm/disk_link.c (revision bc37da3a)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <devfsadm.h>
29 #include <stdio.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 #include <limits.h>
33 #include <sys/stat.h>
34 #include <bsm/devalloc.h>
35 
36 #define	DISK_SUBPATH_MAX 100
37 #define	RM_STALE 0x01
38 #define	DISK_LINK_RE	"^r?dsk/c[0-9]+(t[0-9A-F]+)?d[0-9]+(((s|p))[0-9]+)?$"
39 #define	DISK_LINK_TO_UPPER(ch)\
40 	(((ch) >= 'a' && (ch) <= 'z') ? (ch - 'a' + 'A') : ch)
41 
42 #define	SLICE_SMI	"s7"
43 #define	SLICE_EFI	""
44 
45 #define	MN_SMI		"h"
46 #define	MN_EFI		"wd"
47 #define	ASCIIWWNSIZE	255
48 
49 extern int system_labeled;
50 
51 static int disk_callback_chan(di_minor_t minor, di_node_t node);
52 static int disk_callback_nchan(di_minor_t minor, di_node_t node);
53 static int disk_callback_wwn(di_minor_t minor, di_node_t node);
54 static int disk_callback_fabric(di_minor_t minor, di_node_t node);
55 static void disk_common(di_minor_t minor, di_node_t node, char *disk,
56 				int flags);
57 static char *diskctrl(di_node_t node, di_minor_t minor);
58 static int reserved_links_exist(di_node_t node, di_minor_t minor, int nflags);
59 
60 
61 static devfsadm_create_t disk_cbt[] = {
62 	{ "disk", "ddi_block", NULL,
63 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
64 	},
65 	{ "disk", "ddi_block:channel", NULL,
66 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
67 	},
68 	{ "disk", "ddi_block:fabric", NULL,
69 		TYPE_EXACT, ILEVEL_0, disk_callback_fabric
70 	},
71 	{ "disk", "ddi_block:wwn", NULL,
72 	    TYPE_EXACT, ILEVEL_0, disk_callback_wwn
73 	},
74 	{ "disk", "ddi_block:cdrom", NULL,
75 	    TYPE_EXACT, ILEVEL_0, disk_callback_nchan
76 	},
77 	{ "disk", "ddi_block:cdrom:channel", NULL,
78 	    TYPE_EXACT, ILEVEL_0, disk_callback_chan
79 	},
80 };
81 
82 DEVFSADM_CREATE_INIT_V0(disk_cbt);
83 
84 /*
85  * HOT auto cleanup of disks not desired.
86  */
87 static devfsadm_remove_t disk_remove_cbt[] = {
88 	{ "disk", DISK_LINK_RE, RM_POST,
89 		ILEVEL_0, devfsadm_rm_all
90 	}
91 };
92 
93 DEVFSADM_REMOVE_INIT_V0(disk_remove_cbt);
94 
95 static devlink_re_t disks_re_array[] = {
96 	{"^r?dsk/c([0-9]+)", 1},
97 	{"^cfg/c([0-9]+)$", 1},
98 	{"^scsi/.+/c([0-9]+)", 1},
99 	{NULL}
100 };
101 
102 static char *disk_mid = "disk_mid";
103 static char *modname = "disk_link";
104 
105 int
106 minor_init()
107 {
108 	devfsadm_print(disk_mid,
109 	    "%s: minor_init(): Creating disks reserved ID cache\n",
110 	    modname);
111 	return (devfsadm_reserve_id_cache(disks_re_array, NULL));
112 }
113 
114 static int
115 disk_callback_chan(di_minor_t minor, di_node_t node)
116 {
117 	char *addr;
118 	char disk[20];
119 	uint_t targ;
120 	uint_t lun;
121 
122 	addr = di_bus_addr(node);
123 	(void) sscanf(addr, "%X,%X", &targ, &lun);
124 	(void) sprintf(disk, "t%dd%d", targ, lun);
125 	disk_common(minor, node, disk, 0);
126 	return (DEVFSADM_CONTINUE);
127 
128 }
129 
130 static int
131 disk_callback_nchan(di_minor_t minor, di_node_t node)
132 {
133 	char *addr;
134 	char disk[10];
135 	uint_t lun;
136 
137 	addr = di_bus_addr(node);
138 	(void) sscanf(addr, "%X", &lun);
139 	(void) sprintf(disk, "d%d", lun);
140 	disk_common(minor, node, disk, 0);
141 	return (DEVFSADM_CONTINUE);
142 
143 }
144 
145 static int
146 disk_callback_wwn(di_minor_t minor, di_node_t node)
147 {
148 	char disk[10];
149 	int lun;
150 	int targ;
151 	int *intp;
152 
153 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
154 		"target", &intp) <= 0) {
155 		return (DEVFSADM_CONTINUE);
156 	}
157 	targ = *intp;
158 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
159 		    "lun", &intp) <= 0) {
160 		    lun = 0;
161 	} else {
162 		    lun = *intp;
163 	}
164 	(void) sprintf(disk, "t%dd%d", targ, lun);
165 
166 	disk_common(minor, node, disk, RM_STALE);
167 
168 	return (DEVFSADM_CONTINUE);
169 }
170 
171 static int
172 disk_callback_fabric(di_minor_t minor, di_node_t node)
173 {
174 	char disk[DISK_SUBPATH_MAX];
175 	int lun;
176 	int count;
177 	int *intp;
178 	uchar_t *str;
179 	uchar_t *wwn;
180 	uchar_t ascii_wwn[ASCIIWWNSIZE];
181 
182 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
183 	    "client-guid", (char **)&wwn) > 0) {
184 		if (strlcpy((char *)ascii_wwn, (char *)wwn, sizeof (ascii_wwn))
185 			>= sizeof (ascii_wwn)) {
186 			devfsadm_errprint("SUNW_disk_link: GUID too long:%d",
187 				strlen((char *)wwn));
188 			return (DEVFSADM_CONTINUE);
189 		}
190 		lun = 0;
191 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
192 	    "port-wwn", &wwn) > 0) {
193 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
194 		    "lun", &intp) > 0) {
195 			lun = *intp;
196 		} else {
197 			lun = 0;
198 		}
199 
200 		for (count = 0, str = ascii_wwn; count < 8; count++, str += 2) {
201 			(void) sprintf((caddr_t)str, "%02x", wwn[count]);
202 		}
203 		*str = '\0';
204 	} else {
205 		return (DEVFSADM_CONTINUE);
206 	}
207 
208 	for (str = ascii_wwn; *str != '\0'; str++) {
209 		*str = DISK_LINK_TO_UPPER(*str);
210 	}
211 
212 	(void) snprintf(disk, DISK_SUBPATH_MAX, "t%sd%d", ascii_wwn, lun);
213 
214 	disk_common(minor, node, disk, RM_STALE);
215 
216 	return (DEVFSADM_CONTINUE);
217 }
218 
219 /*
220  * This function is called for every disk minor node.
221  * Calls enumerate to assign a logical controller number, and
222  * then devfsadm_mklink to make the link.
223  */
224 static void
225 disk_common(di_minor_t minor, di_node_t node, char *disk, int flags)
226 {
227 	char l_path[PATH_MAX + 1];
228 	char sec_path[PATH_MAX + 1];
229 	char stale_re[DISK_SUBPATH_MAX];
230 	char *dir;
231 	char slice[4];
232 	char *mn;
233 	char *ctrl;
234 	char *nt = NULL;
235 	int *int_prop;
236 	int  nflags = 0;
237 
238 	if (strstr(mn = di_minor_name(minor), ",raw")) {
239 		dir = "rdsk";
240 	} else {
241 		dir = "dsk";
242 	}
243 
244 	if (mn[0] < 113) {
245 		(void) sprintf(slice, "s%d", mn[0] - 'a');
246 	} else if (strncmp(mn, MN_EFI, 2) != 0) {
247 		(void) sprintf(slice, "p%d", mn[0] - 'q');
248 	} else {
249 		/* For EFI label */
250 		(void) sprintf(slice, SLICE_EFI);
251 	}
252 
253 	nflags = 0;
254 	if (system_labeled) {
255 		nt = di_minor_nodetype(minor);
256 		if ((nt != NULL) &&
257 		    ((strcmp(nt, DDI_NT_CD) == 0) ||
258 		    (strcmp(nt, DDI_NT_CD_CHAN) == 0) ||
259 		    (strcmp(nt, DDI_NT_BLOCK_CHAN) == 0))) {
260 			nflags = DA_ADD|DA_CD;
261 		}
262 	}
263 
264 	if (reserved_links_exist(node, minor, nflags) == DEVFSADM_SUCCESS) {
265 		devfsadm_print(disk_mid, "Reserved link exists. Not "
266 		    "creating links for slice %s\n", slice);
267 		return;
268 	}
269 
270 	if (NULL == (ctrl = diskctrl(node, minor)))
271 		return;
272 
273 	(void) strcpy(l_path, dir);
274 	(void) strcat(l_path, "/c");
275 	(void) strcat(l_path, ctrl);
276 	(void) strcat(l_path, disk);
277 
278 	/*
279 	 * If switching between SMI and EFI label or vice versa
280 	 * cleanup the previous label's devlinks.
281 	 */
282 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
283 		char *s, tpath[PATH_MAX + 1];
284 		struct stat sb;
285 
286 		s = l_path + strlen(l_path);
287 		(void) strcat(l_path, (*mn == *(MN_SMI))
288 		    ? SLICE_EFI : SLICE_SMI);
289 		/*
290 		 * Attempt the remove only if the stale link exists
291 		 */
292 		(void) snprintf(tpath, sizeof (tpath), "%s/dev/%s",
293 		    devfsadm_root_path(), l_path);
294 		if (lstat(tpath, &sb) != -1)
295 			devfsadm_rm_all(l_path);
296 		*s = '\0';
297 	}
298 	(void) strcat(l_path, slice);
299 
300 	(void) devfsadm_mklink(l_path, node, minor, nflags);
301 
302 	/* secondary links for removable and hotpluggable devices */
303 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "removable-media",
304 	    &int_prop) >= 0) {
305 		(void) strcpy(sec_path, "removable-media/");
306 		(void) strcat(sec_path, l_path);
307 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
308 	}
309 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "hotpluggable",
310 	    &int_prop) >= 0) {
311 		(void) strcpy(sec_path, "hotpluggable/");
312 		(void) strcat(sec_path, l_path);
313 		(void) devfsadm_secondary_link(sec_path, l_path, 0);
314 	}
315 
316 	if ((flags & RM_STALE) == RM_STALE) {
317 		(void) strcpy(stale_re, "^");
318 		(void) strcat(stale_re, dir);
319 		(void) strcat(stale_re, "/c");
320 		(void) strcat(stale_re, ctrl);
321 		(void) strcat(stale_re, "t[0-9A-F]+d[0-9]+(s[0-9]+)?$");
322 		/*
323 		 * optimizations are made inside of devfsadm_rm_stale_links
324 		 * instead of before calling the function, as it always
325 		 * needs to add the valid link to the cache.
326 		 */
327 		devfsadm_rm_stale_links(stale_re, l_path, node, minor);
328 	}
329 
330 	free(ctrl);
331 }
332 
333 
334 /* index of enumeration rule applicable to this module */
335 #define	RULE_INDEX	0
336 
337 static char *
338 diskctrl(di_node_t node, di_minor_t minor)
339 {
340 	char path[PATH_MAX + 1];
341 	char *devfspath;
342 	char *buf, *mn;
343 
344 	devfsadm_enumerate_t rules[3] = {
345 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
346 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
347 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
348 	};
349 
350 	mn = di_minor_name(minor);
351 
352 	if ((devfspath = di_devfs_path(node)) == NULL) {
353 		return (NULL);
354 	}
355 	(void) strcpy(path, devfspath);
356 	(void) strcat(path, ":");
357 	(void) strcat(path, mn);
358 	di_devfs_path_free(devfspath);
359 
360 	/*
361 	 * Use controller component of disk path
362 	 */
363 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
364 	    DEVFSADM_MULTIPLE) {
365 
366 		/*
367 		 * We failed because there are multiple logical controller
368 		 * numbers for a single physical controller.  If we use node
369 		 * name also in the match it should fix this and only find one
370 		 * logical controller. (See 4045879).
371 		 * NOTE: Rules for controllers are not changed, as there is
372 		 * no unique controller number for them in this case.
373 		 *
374 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
375 		 * modules. NOT to be used by other modules.
376 		 */
377 
378 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
379 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
380 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
381 			return (NULL);
382 		}
383 	}
384 
385 	return (buf);
386 }
387 
388 typedef struct dvlist {
389 	char *dv_link;
390 	struct dvlist *dv_next;
391 } dvlist_t;
392 
393 static void
394 free_dvlist(dvlist_t **pp)
395 {
396 	dvlist_t *entry;
397 
398 	while (*pp) {
399 		entry = *pp;
400 		*pp = entry->dv_next;
401 		assert(entry->dv_link);
402 		free(entry->dv_link);
403 		free(entry);
404 	}
405 }
406 static int
407 dvlink_cb(di_devlink_t devlink, void *arg)
408 {
409 	char *path;
410 	char *can_path;
411 	dvlist_t **pp = (dvlist_t **)arg;
412 	dvlist_t *entry = NULL;
413 
414 	entry = calloc(1, sizeof (dvlist_t));
415 	if (entry == NULL) {
416 		devfsadm_errprint("%s: calloc failed\n", modname);
417 		goto error;
418 	}
419 
420 	path = (char *)di_devlink_path(devlink);
421 	assert(path);
422 	if (path == NULL) {
423 		devfsadm_errprint("%s: di_devlink_path() returned NULL\n",
424 		    modname);
425 		goto error;
426 	}
427 
428 	devfsadm_print(disk_mid, "%s: found link %s in reverse link cache\n",
429 	    modname, path);
430 
431 	/*
432 	 * Return linkname in canonical form i.e. without the
433 	 * "/dev/" prefix
434 	 */
435 	can_path = strstr(path, "/dev/");
436 	if (can_path == NULL) {
437 		devfsadm_errprint("%s: devlink path %s has no /dev/\n",
438 		    modname, path);
439 		goto error;
440 	}
441 
442 	entry->dv_link = s_strdup(can_path + strlen("/dev/"));
443 	entry->dv_next = *pp;
444 	*pp = entry;
445 
446 	return (DI_WALK_CONTINUE);
447 
448 error:
449 	free(entry);
450 	free_dvlist(pp);
451 	*pp = NULL;
452 	return (DI_WALK_TERMINATE);
453 }
454 
455 /*
456  * Returns success only if all goes well. If there is no matching reserved link
457  * or if there is an error, we assume no match. It is better to err on the side
458  * of caution by creating extra links than to miss out creating a required link.
459  */
460 static int
461 reserved_links_exist(di_node_t node, di_minor_t minor, int nflags)
462 {
463 	di_devlink_handle_t dvlink_cache = devfsadm_devlink_cache();
464 	char phys_path[PATH_MAX];
465 	char *minor_path;
466 	dvlist_t *head;
467 	dvlist_t *entry;
468 	char *s;
469 	char l[PATH_MAX];
470 	int switch_link = 0;
471 	struct stat sb;
472 	char *mn = di_minor_name(minor);
473 
474 	if (dvlink_cache == NULL || mn == NULL) {
475 		devfsadm_errprint("%s: No minor or devlink cache\n", modname);
476 		return (DEVFSADM_FAILURE);
477 	}
478 
479 	if (stat(ENUMERATE_RESERVED, &sb) == -1) {
480 		devfsadm_print(disk_mid, "%s: No reserved file: %s. Will "
481 		    "not bypass new link creation\n",
482 		    modname, ENUMERATE_RESERVED);
483 		return (DEVFSADM_FAILURE);
484 	}
485 
486 	minor_path = di_devfs_minor_path(minor);
487 	if (minor_path == NULL) {
488 		devfsadm_errprint("%s: di_devfs_minor_path failed\n", modname);
489 		return (DEVFSADM_FAILURE);
490 	}
491 
492 	(void) strlcpy(phys_path, minor_path, sizeof (phys_path));
493 
494 	di_devfs_path_free(minor_path);
495 
496 	head = NULL;
497 	(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE, phys_path,
498 	    DI_PRIMARY_LINK, &head, dvlink_cb);
499 
500 	/*
501 	 * We may be switching between EFI label and SMI label in which case
502 	 * we only have minors of the other type.
503 	 */
504 	if (head == NULL && (*mn == *(MN_SMI) ||
505 	    (strncmp(mn, MN_EFI, 2) == 0))) {
506 		devfsadm_print(disk_mid, "%s: No links for minor %s in /dev. "
507 		    "Trying another label\n", modname, mn);
508 		s = strrchr(phys_path, ':');
509 		if (s == NULL) {
510 			devfsadm_errprint("%s: invalid minor path: %s\n",
511 			    modname, phys_path);
512 			return (DEVFSADM_FAILURE);
513 		}
514 		(void) snprintf(s+1, sizeof (phys_path) - (s + 1 - phys_path),
515 			"%s%s", *mn == *(MN_SMI) ? MN_EFI : MN_SMI,
516 			strstr(s, ",raw") ? ",raw" : "");
517 		(void) di_devlink_cache_walk(dvlink_cache, DISK_LINK_RE,
518 		    phys_path, DI_PRIMARY_LINK, &head, dvlink_cb);
519 	}
520 
521 	if (head == NULL) {
522 		devfsadm_print(disk_mid, "%s: minor %s has no links in /dev\n",
523 		    modname, phys_path);
524 		/* no links on disk */
525 		return (DEVFSADM_FAILURE);
526 	}
527 
528 	/*
529 	 * It suffices to use 1 link to this minor, since
530 	 * we are matching with reserved IDs on the basis of
531 	 * the controller number which will be the same for
532 	 * all links to this minor.
533 	 */
534 	if (!devfsadm_is_reserved(disks_re_array, head->dv_link)) {
535 		/* not reserved links */
536 		devfsadm_print(disk_mid, "%s: devlink %s and its minor "
537 		    "are NOT reserved\n", modname, head->dv_link);
538 		free_dvlist(&head);
539 		return (DEVFSADM_FAILURE);
540 	}
541 
542 	devfsadm_print(disk_mid, "%s: devlink %s and its minor are on "
543 	    "reserved list\n", modname, head->dv_link);
544 
545 	/*
546 	 * Switch between SMI and EFI labels if required
547 	 */
548 	switch_link = 0;
549 	if (*mn == *(MN_SMI) || (strncmp(mn, MN_EFI, 2) == 0)) {
550 		for (entry = head; entry; entry = entry->dv_next) {
551 			s = strrchr(entry->dv_link, '/');
552 			assert(s);
553 			if (s == NULL) {
554 				devfsadm_errprint("%s: disk link %s has no "
555 				    "directory\n", modname, entry->dv_link);
556 				continue;
557 			}
558 			if (*mn == *(MN_SMI) && strchr(s, 's') == NULL) {
559 				(void) snprintf(l, sizeof (l), "%s%s",
560 				    entry->dv_link, SLICE_SMI);
561 				switch_link = 1;
562 				devfsadm_print(disk_mid, "%s: switching "
563 				    "reserved link from EFI to SMI label. "
564 				    "New link is %s\n", modname, l);
565 			} else if (strncmp(mn, MN_EFI, 2) == 0 &&
566 			    (s = strchr(s, 's'))) {
567 				*s = '\0';
568 				(void) snprintf(l, sizeof (l), "%s",
569 				    entry->dv_link);
570 				*s = 's';
571 				switch_link = 1;
572 				devfsadm_print(disk_mid, "%s: switching "
573 				    "reserved link from SMI to EFI label. "
574 				    "New link is %s\n", modname, l);
575 			}
576 			if (switch_link) {
577 				devfsadm_print(disk_mid, "%s: switching "
578 				    "link: deleting %s and creating %s\n",
579 				    modname, entry->dv_link, l);
580 				devfsadm_rm_link(entry->dv_link);
581 				(void) devfsadm_mklink(l, node, minor, nflags);
582 			}
583 		}
584 	}
585 	free_dvlist(&head);
586 
587 	/*
588 	 * return SUCCESS to indicate that new links to this minor should not
589 	 * be created so that only compatibility links to this minor remain.
590 	 */
591 	return (DEVFSADM_SUCCESS);
592 }
593