xref: /titanic_41/usr/src/lib/libdiskmgt/common/findevs.c (revision a31148363f598def767ac48c5d82e1572e44b935)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <fcntl.h>
27 #include <libdevinfo.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/sunddi.h>
33 #include <sys/types.h>
34 #include <sys/mkdev.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <unistd.h>
38 #include <devid.h>
39 #include <sys/fs/zfs.h>
40 
41 #include "libdiskmgt.h"
42 #include "disks_private.h"
43 
44 #define	CLUSTER_DEV	"did"
45 
46 /* specify which disk links to use in the /dev directory */
47 #define	DEVLINK_REGEX		"rdsk/.*"
48 #define	DEVLINK_FLOPPY_REGEX	"rdiskette[0-9]"
49 #define	DEVLINK_DID_REGEX	"did/rdsk/.*"
50 
51 #define	FLOPPY_NAME	"rdiskette"
52 
53 #define	MAXPROPLEN		1024
54 #define	DEVICE_ID_PROP		"devid"
55 #define	PROD_ID_PROP		"inquiry-product-id"
56 #define	PROD_ID_USB_PROP	"usb-product-name"
57 #define	REMOVABLE_PROP		"removable-media"
58 #define	HOTPLUGGABLE_PROP	"hotpluggable"
59 #define	SCSI_OPTIONS_PROP	"scsi-options"
60 #define	VENDOR_ID_PROP		"inquiry-vendor-id"
61 #define	VENDOR_ID_USB_PROP	"usb-vendor-name"
62 #define	WWN_PROP		"node-wwn"
63 
64 static char *ctrltypes[] = {
65 	DDI_NT_SCSI_NEXUS,
66 	DDI_NT_SCSI_ATTACHMENT_POINT,
67 	DDI_NT_FC_ATTACHMENT_POINT,
68 	NULL
69 };
70 
71 static char *bustypes[] = {
72 	"sbus",
73 	"pci",
74 	"usb",
75 	NULL
76 };
77 
78 static bus_t		*add_bus(struct search_args *args, di_node_t node,
79 			    di_minor_t minor, controller_t *cp);
80 static int		add_cluster_devs(di_node_t node, di_minor_t minor,
81 			    void *arg);
82 static controller_t	*add_controller(struct search_args *args,
83 			    di_node_t node, di_minor_t minor);
84 static int		add_devpath(di_devlink_t devlink, void *arg);
85 static int		add_devs(di_node_t node, di_minor_t minor, void *arg);
86 static int		add_disk2controller(disk_t *diskp,
87 			    struct search_args *args);
88 static int		add_disk2path(disk_t *dp, path_t *pp,
89 			    di_path_state_t st, char *wwn);
90 static int		add_int2array(int p, int **parray);
91 static int		add_ptr2array(void *p, void ***parray);
92 static char		*bus_type(di_node_t node, di_minor_t minor,
93 			    di_prom_handle_t ph);
94 static void		remove_controller(controller_t *cp,
95 			    controller_t *currp);
96 static void		clean_paths(struct search_args *args);
97 static disk_t		*create_disk(char *deviceid, char *kernel_name,
98 			    struct search_args *args);
99 static char		*ctype(di_node_t node, di_minor_t minor);
100 static boolean_t	disk_is_cdrom(const char *type);
101 static alias_t		*find_alias(disk_t *diskp, char *kernel_name);
102 static bus_t		*find_bus(struct search_args *args, char *name);
103 static controller_t	*find_controller(struct search_args *args, char *name);
104 static int		fix_cluster_devpath(di_devlink_t devlink, void *arg);
105 static disk_t		*get_disk_by_deviceid(disk_t *listp, char *devid);
106 static void		get_disk_name_from_path(char *path, char *name,
107 			    int size);
108 static char		*get_byte_prop(char *prop_name, di_node_t node);
109 static di_node_t	get_parent_bus(di_node_t node,
110 			    struct search_args *args);
111 static int		get_prom_int(char *prop_name, di_node_t node,
112 			    di_prom_handle_t ph);
113 static char		*get_prom_str(char *prop_name, di_node_t node,
114 			    di_prom_handle_t ph);
115 static int		get_prop(char *prop_name, di_node_t node);
116 static char		*get_str_prop(char *prop_name, di_node_t node);
117 static int		have_disk(struct search_args *args, char *devid,
118 			    char *kernel_name, disk_t **diskp);
119 static int		is_cluster_disk(di_node_t node, di_minor_t minor);
120 static int		is_ctds(char *name);
121 static int		is_drive(di_minor_t minor);
122 static int		is_zvol(di_node_t node, di_minor_t minor);
123 static int		is_HBA(di_node_t node, di_minor_t minor);
124 static int		new_alias(disk_t *diskp, char *kernel_path,
125 			    char *devlink_path, struct search_args *args);
126 static int		new_devpath(alias_t *ap, char *devpath);
127 static path_t		*new_path(controller_t *cp, disk_t *diskp,
128 			    di_node_t node, di_path_state_t st, char *wwn);
129 static void		remove_invalid_controller(char *name,
130 			    controller_t *currp, struct search_args *args);
131 static char		*str_case_index(register char *s1, register char *s2);
132 
133 /*
134  * The functions in this file do a dev tree walk to build up a model of the
135  * disks, controllers and paths on the system.  This model is returned in the
136  * args->disk_listp and args->controller_listp members of the args param.
137  * There is no global data for this file so it is thread safe.  It is up to
138  * the caller to merge the resulting model with any existing model that is
139  * cached.  The caller must also free the memory for this model when it is
140  * no longer needed.
141  */
142 void
143 findevs(struct search_args *args)
144 {
145 	uint_t			flags;
146 	di_node_t		di_root;
147 
148 	args->dev_walk_status = 0;
149 	args->disk_listp = NULL;
150 	args->controller_listp = NULL;
151 	args->bus_listp = NULL;
152 
153 	args->handle = di_devlink_init(NULL, 0);
154 
155 	/*
156 	 * Have to make several passes at this with the new devfs caching.
157 	 * First, we find non-mpxio devices. Then we find mpxio/multipath
158 	 * devices. Finally, we get cluster devices.
159 	 */
160 	flags = DINFOCACHE;
161 	di_root = di_init("/", flags);
162 	args->ph = di_prom_init();
163 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
164 	di_fini(di_root);
165 
166 	flags = DINFOCPYALL | DINFOPATH;
167 	di_root = di_init("/", flags);
168 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
169 	di_fini(di_root);
170 
171 	/* do another pass to clean up cluster devpaths */
172 	flags = DINFOCACHE;
173 	di_root = di_init("/", flags);
174 	(void) di_walk_minor(di_root, DDI_PSEUDO, 0, args, add_cluster_devs);
175 	if (args->ph != DI_PROM_HANDLE_NIL) {
176 		(void) di_prom_fini(args->ph);
177 	}
178 	di_fini(di_root);
179 
180 	(void) di_devlink_fini(&(args->handle));
181 
182 	clean_paths(args);
183 }
184 
185 /*
186  * Definitions of private functions
187  */
188 
189 static bus_t *
190 add_bus(struct search_args *args, di_node_t node, di_minor_t minor,
191 	controller_t *cp)
192 {
193 	char		*btype;
194 	char		*devpath;
195 	bus_t		*bp;
196 	char		kstat_name[MAXPATHLEN];
197 	di_node_t	pnode;
198 
199 	if (node == DI_NODE_NIL) {
200 		return (NULL);
201 	}
202 
203 	if ((btype = bus_type(node, minor, args->ph)) == NULL) {
204 		return (add_bus(args, di_parent_node(node),
205 		    di_minor_next(di_parent_node(node), NULL), cp));
206 	}
207 
208 	devpath = di_devfs_path(node);
209 
210 	if ((bp = find_bus(args, devpath)) != NULL) {
211 		di_devfs_path_free((void *) devpath);
212 
213 		if (cp != NULL) {
214 			if (add_ptr2array(cp,
215 			    (void ***)&bp->controllers) != 0) {
216 				args->dev_walk_status = ENOMEM;
217 				return (NULL);
218 			}
219 		}
220 		return (bp);
221 	}
222 
223 	/* Special handling for root node. */
224 	if (strcmp(devpath, "/") == 0) {
225 		di_devfs_path_free((void *) devpath);
226 		return (NULL);
227 	}
228 
229 	if (dm_debug) {
230 		(void) fprintf(stderr, "INFO: add_bus %s\n", devpath);
231 	}
232 
233 	bp = (bus_t *)calloc(1, sizeof (bus_t));
234 	if (bp == NULL) {
235 		return (NULL);
236 	}
237 
238 	bp->name = strdup(devpath);
239 	di_devfs_path_free((void *) devpath);
240 	if (bp->name == NULL) {
241 		args->dev_walk_status = ENOMEM;
242 		cache_free_bus(bp);
243 		return (NULL);
244 	}
245 
246 	bp->btype = strdup(btype);
247 	if (bp->btype == NULL) {
248 		args->dev_walk_status = ENOMEM;
249 		cache_free_bus(bp);
250 		return (NULL);
251 	}
252 
253 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
254 	    di_node_name(node), di_instance(node));
255 
256 	if ((bp->kstat_name = strdup(kstat_name)) == NULL) {
257 		args->dev_walk_status = ENOMEM;
258 		cache_free_bus(bp);
259 		return (NULL);
260 	}
261 
262 	/* if parent node is a bus, get its name */
263 	if ((pnode = get_parent_bus(node, args)) != NULL) {
264 		devpath = di_devfs_path(pnode);
265 		bp->pname = strdup(devpath);
266 		di_devfs_path_free((void *) devpath);
267 		if (bp->pname == NULL) {
268 			args->dev_walk_status = ENOMEM;
269 			cache_free_bus(bp);
270 			return (NULL);
271 		}
272 
273 	} else {
274 		bp->pname = NULL;
275 	}
276 
277 	bp->freq = get_prom_int("clock-frequency", node, args->ph);
278 
279 	bp->controllers = (controller_t **)calloc(1, sizeof (controller_t *));
280 	if (bp->controllers == NULL) {
281 		args->dev_walk_status = ENOMEM;
282 		cache_free_bus(bp);
283 		return (NULL);
284 	}
285 	bp->controllers[0] = NULL;
286 
287 	if (cp != NULL) {
288 		if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) {
289 			args->dev_walk_status = ENOMEM;
290 			return (NULL);
291 		}
292 	}
293 
294 	bp->next = args->bus_listp;
295 	args->bus_listp = bp;
296 
297 	return (bp);
298 }
299 
300 static int
301 add_cluster_devs(di_node_t node, di_minor_t minor, void *arg)
302 {
303 	struct search_args	*args;
304 	char			*devpath;
305 	char			slice_path[MAXPATHLEN];
306 	int			result = DI_WALK_CONTINUE;
307 
308 	if (!is_cluster_disk(node, minor)) {
309 		return (DI_WALK_CONTINUE);
310 	}
311 
312 	args = (struct search_args *)arg;
313 
314 	if (dm_debug > 1) {
315 		/* This is all just debugging code */
316 		char	*devpath;
317 		char	dev_name[MAXPATHLEN];
318 
319 		devpath = di_devfs_path(node);
320 		(void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
321 		    di_minor_name(minor));
322 		di_devfs_path_free((void *) devpath);
323 
324 		(void) fprintf(stderr, "INFO: cluster dev: %s\n", dev_name);
325 	}
326 
327 	args->node = node;
328 	args->minor = minor;
329 	args->dev_walk_status = 0;
330 
331 	/*
332 	 * Fix the devpaths for the cluster drive.
333 	 *
334 	 * We will come through here once for each raw slice device name.
335 	 */
336 	devpath = di_devfs_path(node);
337 	(void) snprintf(slice_path, sizeof (slice_path), "%s:%s", devpath,
338 	    di_minor_name(minor));
339 	di_devfs_path_free((void *) devpath);
340 
341 	/* Walk the /dev tree to get the cluster devlinks. */
342 	(void) di_devlink_walk(args->handle, DEVLINK_DID_REGEX, slice_path,
343 	    DI_PRIMARY_LINK, arg, fix_cluster_devpath);
344 
345 	if (args->dev_walk_status != 0) {
346 		result = DI_WALK_TERMINATE;
347 	}
348 
349 	return (result);
350 }
351 
352 static controller_t *
353 add_controller(struct search_args *args, di_node_t node, di_minor_t minor)
354 {
355 	char		*devpath;
356 	controller_t	*cp;
357 	char		kstat_name[MAXPATHLEN];
358 	char		*c_type = DM_CTYPE_UNKNOWN;
359 
360 	devpath = di_devfs_path(node);
361 
362 	if ((cp = find_controller(args, devpath)) != NULL) {
363 		di_devfs_path_free((void *) devpath);
364 		return (cp);
365 	}
366 
367 	/* Special handling for fp attachment node. */
368 	if (strcmp(di_node_name(node), "fp") == 0) {
369 		di_node_t pnode;
370 
371 		pnode = di_parent_node(node);
372 		if (pnode != DI_NODE_NIL) {
373 			di_devfs_path_free((void *) devpath);
374 			devpath = di_devfs_path(pnode);
375 
376 			if ((cp = find_controller(args, devpath)) != NULL) {
377 				di_devfs_path_free((void *) devpath);
378 				return (cp);
379 			}
380 
381 			/* not in the list, create it */
382 			node = pnode;
383 			c_type = DM_CTYPE_FIBRE;
384 		}
385 	}
386 
387 	if (dm_debug) {
388 		(void) fprintf(stderr, "INFO: add_controller %s\n", devpath);
389 	}
390 
391 	cp = (controller_t *)calloc(1, sizeof (controller_t));
392 	if (cp == NULL) {
393 		return (NULL);
394 	}
395 
396 	cp->name = strdup(devpath);
397 	di_devfs_path_free((void *) devpath);
398 	if (cp->name == NULL) {
399 		cache_free_controller(cp);
400 		return (NULL);
401 	}
402 
403 	if (strcmp(c_type, DM_CTYPE_UNKNOWN) == 0) {
404 		c_type = ctype(node, minor);
405 	}
406 	cp->ctype = c_type;
407 
408 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
409 	    di_node_name(node), di_instance(node));
410 
411 	if ((cp->kstat_name = strdup(kstat_name)) == NULL) {
412 		cache_free_controller(cp);
413 		return (NULL);
414 	}
415 
416 	if (libdiskmgt_str_eq(cp->ctype, "scsi")) {
417 		cp->scsi_options = get_prop(SCSI_OPTIONS_PROP, node);
418 	}
419 
420 	if (libdiskmgt_str_eq(di_node_name(node), "scsi_vhci")) {
421 		cp->multiplex = 1;
422 	} else {
423 		cp->multiplex = 0;
424 	}
425 
426 	cp->freq = get_prom_int("clock-frequency", node, args->ph);
427 
428 	cp->disks = (disk_t **)calloc(1, sizeof (disk_t *));
429 	if (cp->disks == NULL) {
430 		cache_free_controller(cp);
431 		return (NULL);
432 	}
433 	cp->disks[0] = NULL;
434 
435 	cp->next = args->controller_listp;
436 	args->controller_listp = cp;
437 
438 	cp->bus = add_bus(args, di_parent_node(node),
439 	    di_minor_next(di_parent_node(node), NULL), cp);
440 
441 	return (cp);
442 }
443 
444 static int
445 add_devpath(di_devlink_t devlink, void *arg)
446 {
447 	struct search_args *args;
448 	char		*devidstr;
449 	disk_t		*diskp;
450 	char		kernel_name[MAXPATHLEN];
451 
452 	args =	(struct search_args *)arg;
453 
454 	/*
455 	 * Get the diskp value from calling have_disk. Can either be found
456 	 * by kernel name or devid.
457 	 */
458 
459 	diskp = NULL;
460 	devidstr = get_str_prop(DEVICE_ID_PROP, args->node);
461 	(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
462 	    di_node_name(args->node), di_instance(args->node));
463 
464 	(void) have_disk(args, devidstr, kernel_name, &diskp);
465 
466 	/*
467 	 * The devlink_path is usually of the form /dev/rdsk/c0t0d0s0.
468 	 * For diskettes it is /dev/rdiskette*.
469 	 * On Intel we would also get each fdisk partition as well
470 	 * (e.g. /dev/rdsk/c0t0d0p0).
471 	 */
472 	if (diskp != NULL) {
473 		alias_t	*ap;
474 		char	*devlink_path;
475 
476 		if (diskp->drv_type != DM_DT_FLOPPY) {
477 			/*
478 			 * Add other controllers for multipath disks.
479 			 * This will have no effect if the controller
480 			 * relationship is already set up.
481 			 */
482 			if (add_disk2controller(diskp, args) != 0) {
483 				args->dev_walk_status = ENOMEM;
484 			}
485 		}
486 
487 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
488 		    di_node_name(args->node), di_instance(args->node));
489 		devlink_path = (char *)di_devlink_path(devlink);
490 
491 		if (dm_debug > 1) {
492 			(void) fprintf(stderr,
493 			    "INFO:     devpath %s\n", devlink_path);
494 		}
495 
496 		if ((ap = find_alias(diskp, kernel_name)) == NULL) {
497 			if (new_alias(diskp, kernel_name, devlink_path,
498 			    args) != 0) {
499 				args->dev_walk_status = ENOMEM;
500 			}
501 		} else {
502 			/*
503 			 * It is possible that we have already added this
504 			 * devpath.  Do not add it again. new_devpath will
505 			 * return a 0 if found, and not add the path.
506 			 */
507 			if (new_devpath(ap, devlink_path) != 0) {
508 				args->dev_walk_status = ENOMEM;
509 			}
510 		}
511 	}
512 
513 	return (DI_WALK_CONTINUE);
514 }
515 
516 static int
517 add_devs(di_node_t node, di_minor_t minor, void *arg)
518 {
519 	struct search_args	*args;
520 	int result = DI_WALK_CONTINUE;
521 
522 	args = (struct search_args *)arg;
523 
524 	if (dm_debug > 1) {
525 		/* This is all just debugging code */
526 		char	*devpath;
527 		char	dev_name[MAXPATHLEN];
528 
529 		devpath = di_devfs_path(node);
530 		(void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
531 		    di_minor_name(minor));
532 		di_devfs_path_free((void *) devpath);
533 
534 		(void) fprintf(stderr,
535 		    "INFO: dev: %s, node: %s%d, minor: 0x%x, type: %s\n",
536 		    dev_name, di_node_name(node), di_instance(node),
537 		    di_minor_spectype(minor),
538 		    (di_minor_nodetype(minor) != NULL ?
539 		    di_minor_nodetype(minor) : "NULL"));
540 	}
541 
542 	if (bus_type(node, minor, args->ph) != NULL) {
543 		if (add_bus(args, node, minor, NULL) == NULL) {
544 			args->dev_walk_status = ENOMEM;
545 			result = DI_WALK_TERMINATE;
546 		}
547 
548 	} else if (is_HBA(node, minor)) {
549 		if (add_controller(args, node, minor) == NULL) {
550 			args->dev_walk_status = ENOMEM;
551 			result = DI_WALK_TERMINATE;
552 		}
553 
554 	} else if (di_minor_spectype(minor) == S_IFCHR &&
555 	    (is_drive(minor) || is_zvol(node, minor))) {
556 		char	*devidstr;
557 		char	kernel_name[MAXPATHLEN];
558 		disk_t	*diskp;
559 
560 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
561 		    di_node_name(node), di_instance(node));
562 		devidstr = get_str_prop(DEVICE_ID_PROP, node);
563 
564 		args->node = node;
565 		args->minor = minor;
566 		/*
567 		 * Check if we already got this disk and
568 		 * this is another slice.
569 		 */
570 		if (!have_disk(args, devidstr, kernel_name, &diskp)) {
571 			args->dev_walk_status = 0;
572 			/*
573 			 * This is a newly found disk, create the
574 			 * disk structure.
575 			 */
576 			diskp = create_disk(devidstr, kernel_name, args);
577 			if (diskp == NULL) {
578 				args->dev_walk_status = ENOMEM;
579 			}
580 
581 			if (diskp->drv_type != DM_DT_FLOPPY) {
582 				/* add the controller relationship */
583 				if (args->dev_walk_status == 0) {
584 					if (add_disk2controller(diskp,
585 					    args) != 0) {
586 						args->dev_walk_status = ENOMEM;
587 					}
588 				}
589 			}
590 		}
591 		if (is_zvol(node, minor)) {
592 			char zvdsk[MAXNAMELEN];
593 			char *str;
594 			alias_t *ap;
595 
596 			if (di_prop_lookup_strings(di_minor_devt(minor),
597 			    node, "name", &str) == -1)
598 				return (DI_WALK_CONTINUE);
599 			(void) snprintf(zvdsk, MAXNAMELEN, "/dev/zvol/rdsk/%s",
600 			    str);
601 			if ((ap = find_alias(diskp, kernel_name)) == NULL) {
602 				if (new_alias(diskp, kernel_name,
603 				    zvdsk, args) != 0) {
604 					args->dev_walk_status = ENOMEM;
605 				}
606 			} else {
607 				/*
608 				 * It is possible that we have already added
609 				 * this devpath.
610 				 * Do not add it again. new_devpath will
611 				 * return a 0 if found, and not add the path.
612 				 */
613 				if (new_devpath(ap, zvdsk) != 0) {
614 					args->dev_walk_status = ENOMEM;
615 				}
616 			}
617 		}
618 
619 		/* Add the devpaths for the drive. */
620 		if (args->dev_walk_status == 0) {
621 			char	*devpath;
622 			char	slice_path[MAXPATHLEN];
623 			char	*pattern;
624 
625 			/*
626 			 * We will come through here once for each of
627 			 * the raw slice device names.
628 			 */
629 			devpath = di_devfs_path(node);
630 			(void) snprintf(slice_path,
631 			    sizeof (slice_path), "%s:%s",
632 			    devpath, di_minor_name(minor));
633 			di_devfs_path_free((void *) devpath);
634 
635 			if (libdiskmgt_str_eq(di_minor_nodetype(minor),
636 			    DDI_NT_FD)) {
637 				pattern = DEVLINK_FLOPPY_REGEX;
638 			} else {
639 				pattern = DEVLINK_REGEX;
640 			}
641 
642 			/* Walk the /dev tree to get the devlinks. */
643 			(void) di_devlink_walk(args->handle, pattern,
644 			    slice_path, DI_PRIMARY_LINK, arg, add_devpath);
645 		}
646 
647 		if (args->dev_walk_status != 0) {
648 			result = DI_WALK_TERMINATE;
649 		}
650 	}
651 
652 	return (result);
653 }
654 
655 static int
656 add_disk2controller(disk_t *diskp, struct search_args *args)
657 {
658 	di_node_t	pnode;
659 	controller_t	*cp;
660 	di_minor_t	minor;
661 	di_node_t	node;
662 	int		i;
663 
664 	node = args->node;
665 
666 	pnode = di_parent_node(node);
667 	if (pnode == DI_NODE_NIL) {
668 		return (0);
669 	}
670 
671 	minor = di_minor_next(pnode, NULL);
672 	if (minor == NULL) {
673 		return (0);
674 	}
675 
676 	if ((cp = add_controller(args, pnode, minor)) == NULL) {
677 		return (ENOMEM);
678 	}
679 
680 	/* check if the disk <-> ctrl assoc is already there */
681 	for (i = 0; diskp->controllers[i]; i++) {
682 		if (cp == diskp->controllers[i]) {
683 			return (0);
684 		}
685 	}
686 
687 	/* this is a new controller for this disk */
688 
689 	/* add the disk to the controlller */
690 	if (add_ptr2array(diskp, (void ***)&cp->disks) != 0) {
691 		return (ENOMEM);
692 	}
693 
694 	/* add the controlller to the disk */
695 	if (add_ptr2array(cp, (void ***)&diskp->controllers) != 0) {
696 		return (ENOMEM);
697 	}
698 
699 	/*
700 	 * Set up paths for mpxio controlled drives.
701 	 */
702 	if (libdiskmgt_str_eq(di_node_name(pnode), "scsi_vhci")) {
703 		/* note: mpxio di_path stuff is all consolidation private */
704 		di_path_t   pi = DI_PATH_NIL;
705 
706 		while (
707 		    (pi = di_path_client_next_path(node, pi)) != DI_PATH_NIL) {
708 			int	cnt;
709 			uchar_t	*bytes;
710 			char	str[MAXPATHLEN];
711 			char	*wwn;
712 
713 			di_node_t phci_node = di_path_phci_node(pi);
714 
715 			/* get the node wwn */
716 			cnt = di_path_prop_lookup_bytes(pi, WWN_PROP, &bytes);
717 			wwn = NULL;
718 			if (cnt > 0) {
719 				int	i;
720 				str[0] = 0;
721 
722 				for (i = 0; i < cnt; i++) {
723 					/*
724 					 * A byte is only 2 hex chars + null.
725 					 */
726 					char bstr[8];
727 
728 					(void) snprintf(bstr,
729 					    sizeof (bstr), "%.2x", bytes[i]);
730 					(void) strlcat(str, bstr, sizeof (str));
731 				}
732 				wwn = str;
733 			}
734 
735 			if (new_path(cp, diskp, phci_node,
736 			    di_path_state(pi), wwn) == NULL) {
737 				return (ENOMEM);
738 			}
739 		}
740 	}
741 
742 	return (0);
743 }
744 
745 static int
746 add_disk2path(disk_t *dp, path_t *pp, di_path_state_t st, char *wwn)
747 {
748 	/* add the disk to the path */
749 	if (add_ptr2array(dp, (void ***)&pp->disks) != 0) {
750 		cache_free_path(pp);
751 		return (0);
752 	}
753 
754 	/* add the path to the disk */
755 	if (add_ptr2array(pp, (void ***)&dp->paths) != 0) {
756 		cache_free_path(pp);
757 		return (0);
758 	}
759 
760 	/* add the path state for this disk */
761 	if (add_int2array(st, &pp->states) != 0) {
762 		cache_free_path(pp);
763 		return (0);
764 	}
765 
766 	/* add the path state for this disk */
767 	if (wwn != NULL) {
768 		char	*wp;
769 
770 		if ((wp = strdup(wwn)) != NULL) {
771 			if (add_ptr2array(wp, (void ***)(&pp->wwns)) != 0) {
772 				cache_free_path(pp);
773 				return (0);
774 			}
775 		}
776 	}
777 
778 	return (1);
779 }
780 
781 static int
782 add_int2array(int p, int **parray)
783 {
784 	int		i;
785 	int		cnt;
786 	int		*pa;
787 	int		*new_array;
788 
789 	pa = *parray;
790 
791 	cnt = 0;
792 	if (pa != NULL) {
793 		for (; pa[cnt] != -1; cnt++)
794 			;
795 	}
796 
797 	new_array = (int *)calloc(cnt + 2, sizeof (int *));
798 	if (new_array == NULL) {
799 		return (ENOMEM);
800 	}
801 
802 	/* copy the existing array */
803 	for (i = 0; i < cnt; i++) {
804 		new_array[i] = pa[i];
805 	}
806 
807 	new_array[i] = p;
808 	new_array[i + 1] = -1;
809 
810 	free(pa);
811 	*parray = new_array;
812 
813 	return (0);
814 }
815 
816 static int
817 add_ptr2array(void *p, void ***parray)
818 {
819 	int		i;
820 	int		cnt;
821 	void		**pa;
822 	void		**new_array;
823 
824 	pa = *parray;
825 
826 	cnt = 0;
827 	if (pa != NULL) {
828 		for (; pa[cnt]; cnt++)
829 			;
830 	}
831 
832 	new_array = (void **)calloc(cnt + 2, sizeof (void *));
833 	if (new_array == NULL) {
834 		return (ENOMEM);
835 	}
836 
837 	/* copy the existing array */
838 	for (i = 0; i < cnt; i++) {
839 		new_array[i] = pa[i];
840 	}
841 
842 	new_array[i] = p;
843 	new_array[i + 1] = NULL;
844 
845 	free(pa);
846 	*parray = new_array;
847 
848 	return (0);
849 }
850 
851 /*
852  * This function checks to see if a controller has other associations
853  * that may be valid. If we are calling this function, we have found that
854  * a controller for an mpxio device is showing up independently of the
855  * mpxio controller, noted as /scsi_vhci. This can happen with some FC
856  * cards that have inbound management devices that show up as well, with
857  * the real controller data associated. We do not want to display these
858  * 'devices' as real devices in libdiskmgt.
859  */
860 static void
861 remove_controller(controller_t *cp, controller_t *currp)
862 {
863 	disk_t *dp;
864 	int	i;
865 
866 	if (cp == currp) {
867 		if (dm_debug) {
868 			(void) fprintf(stderr, "ERROR: removing current"
869 			    " controller\n");
870 		}
871 		return;
872 	}
873 
874 	if (cp->disks != NULL && cp->disks[0] != NULL) {
875 		if (dm_debug) {
876 			(void) fprintf(stderr,
877 			    "INFO: removing inbound management controller"
878 			    " with disk ptrs.\n");
879 		}
880 		/*
881 		 * loop through the disks and remove the reference to the
882 		 * controller for this disk structure. The disk itself
883 		 * is still a valid device, the controller being removed
884 		 * is a 'path' so any disk that has a reference to it
885 		 * as a controller needs to have this reference removed.
886 		 */
887 		dp = cp->disks[0];
888 		while (dp != NULL) {
889 			for (i = 0; dp->controllers[i]; i++) {
890 				if (libdiskmgt_str_eq(dp->controllers[i]->name,
891 				    cp->name)) {
892 					int j;
893 
894 					for (j = i; dp->controllers[j]; j++) {
895 						dp->controllers[j] =
896 						    dp->controllers[j + 1];
897 					}
898 				}
899 			}
900 			dp = dp->next;
901 		}
902 	}
903 	/*
904 	 * Paths are removed with the call to cache_free_controller()
905 	 * below.
906 	 */
907 
908 	if (cp->paths != NULL && cp->paths[0] != NULL) {
909 		if (dm_debug) {
910 			(void) fprintf(stderr,
911 			    "INFO: removing inbound management controller"
912 			    " with path ptrs. \n");
913 		}
914 	}
915 	cache_free_controller(cp);
916 }
917 
918 /*
919  * If we have a controller in the list that is really a path then we need to
920  * take that controller out of the list since nodes that are paths are not
921  * considered to be controllers.
922  */
923 static void
924 clean_paths(struct search_args *args)
925 {
926 	controller_t	*cp;
927 
928 	cp = args->controller_listp;
929 	while (cp != NULL) {
930 		path_t	**pp;
931 
932 		pp = cp->paths;
933 		if (pp != NULL) {
934 			int i;
935 
936 			for (i = 0; pp[i]; i++) {
937 				remove_invalid_controller(pp[i]->name, cp,
938 				    args);
939 			}
940 		}
941 		cp = cp->next;
942 	}
943 }
944 
945 static disk_t *
946 create_disk(char *deviceid, char *kernel_name, struct search_args *args)
947 {
948 	disk_t	*diskp;
949 	char	*type;
950 	char	*prod_id;
951 	char	*vendor_id;
952 
953 	if (dm_debug) {
954 		(void) fprintf(stderr, "INFO: create_disk %s\n", kernel_name);
955 	}
956 
957 	diskp = calloc(1, sizeof (disk_t));
958 	if (diskp == NULL) {
959 		return (NULL);
960 	}
961 
962 	diskp->controllers = (controller_t **)
963 	    calloc(1, sizeof (controller_t *));
964 	if (diskp->controllers == NULL) {
965 		cache_free_disk(diskp);
966 		return (NULL);
967 	}
968 	diskp->controllers[0] = NULL;
969 
970 	diskp->devid = NULL;
971 	if (deviceid != NULL) {
972 		if ((diskp->device_id = strdup(deviceid)) == NULL) {
973 			cache_free_disk(diskp);
974 			return (NULL);
975 		}
976 		(void) devid_str_decode(deviceid, &(diskp->devid), NULL);
977 	}
978 
979 	if (kernel_name != NULL) {
980 		diskp->kernel_name = strdup(kernel_name);
981 		if (diskp->kernel_name == NULL) {
982 			cache_free_disk(diskp);
983 			return (NULL);
984 		}
985 	}
986 
987 	diskp->paths = NULL;
988 	diskp->aliases = NULL;
989 
990 	diskp->cd_rom = 0;
991 	diskp->rpm = 0;
992 	type = di_minor_nodetype(args->minor);
993 
994 	prod_id = get_str_prop(PROD_ID_PROP, args->node);
995 	if (prod_id != NULL) {
996 		if ((diskp->product_id = strdup(prod_id)) == NULL) {
997 			cache_free_disk(diskp);
998 			return (NULL);
999 		}
1000 	} else {
1001 		prod_id = get_str_prop(PROD_ID_USB_PROP, args->node);
1002 		if (prod_id != NULL) {
1003 			if ((diskp->product_id = strdup(prod_id)) == NULL) {
1004 				cache_free_disk(diskp);
1005 				return (NULL);
1006 			}
1007 		}
1008 	}
1009 
1010 	vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
1011 	if (vendor_id != NULL) {
1012 		if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
1013 			cache_free_disk(diskp);
1014 			return (NULL);
1015 		}
1016 	} else {
1017 		vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
1018 		if (vendor_id != NULL) {
1019 			if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
1020 				cache_free_disk(diskp);
1021 				return (NULL);
1022 			}
1023 		}
1024 	}
1025 
1026 	/*
1027 	 * DVD, CD-ROM, CD-RW, MO, etc. are all reported as CD-ROMS.
1028 	 * We try to use uscsi later to determine the real type.
1029 	 * The cd_rom flag tells us that the kernel categorized the drive
1030 	 * as a CD-ROM.  We leave the drv_type as UKNOWN for now.
1031 	 * The combination of the cd_rom flag being set with the drv_type of
1032 	 * unknown is what triggers the uscsi probe in drive.c.
1033 	 */
1034 	if (disk_is_cdrom(type)) {
1035 		diskp->drv_type = DM_DT_UNKNOWN;
1036 		diskp->cd_rom = 1;
1037 		diskp->removable = 1;
1038 	} else if (libdiskmgt_str_eq(type, DDI_NT_FD)) {
1039 		diskp->drv_type = DM_DT_FLOPPY;
1040 		diskp->removable = 1;
1041 	} else {
1042 		/* not a "CD-ROM" or Floppy */
1043 		diskp->removable = get_prop(REMOVABLE_PROP, args->node);
1044 
1045 		if (diskp->removable == -1) {
1046 			diskp->removable = 0;
1047 #if defined(i386) || defined(__amd64)
1048 			/*
1049 			 * x86 does not have removable property.
1050 			 * Check for common removable drives, zip & jaz,
1051 			 * and mark those correctly.
1052 			 */
1053 			if (vendor_id != NULL && prod_id != NULL) {
1054 				if (str_case_index(vendor_id,
1055 				    "iomega") != NULL) {
1056 					if (str_case_index(prod_id,
1057 					    "jaz") != NULL) {
1058 						diskp->removable = 1;
1059 					} else if (str_case_index(prod_id,
1060 					    "zip") != NULL) {
1061 						diskp->removable = 1;
1062 					}
1063 				}
1064 			}
1065 #endif
1066 		}
1067 
1068 		if (diskp->removable) {
1069 			/*
1070 			 * For removable jaz or zip drives there is no way
1071 			 * to get the drive type unless media is inserted,so
1072 			 * we look at the product-id for a hint.
1073 			 */
1074 			diskp->drv_type = DM_DT_UNKNOWN;
1075 
1076 			if (prod_id != NULL) {
1077 				if (str_case_index(prod_id, "jaz") != NULL) {
1078 					diskp->drv_type = DM_DT_JAZ;
1079 				} else if (str_case_index(prod_id,
1080 				    "zip") != NULL) {
1081 					diskp->drv_type = DM_DT_ZIP;
1082 				}
1083 			}
1084 		} else {
1085 			diskp->drv_type = DM_DT_FIXED;
1086 		}
1087 	}
1088 
1089 	diskp->next = args->disk_listp;
1090 	args->disk_listp = diskp;
1091 
1092 	return (diskp);
1093 }
1094 
1095 static char *
1096 ctype(di_node_t node, di_minor_t minor)
1097 {
1098 	char	*type;
1099 	char	*name;
1100 
1101 	type = di_minor_nodetype(minor);
1102 	name = di_node_name(node);
1103 
1104 	/* IDE disks use SCSI nexus as the type, so handle this special case */
1105 	if (libdiskmgt_str_eq(name, "ide")) {
1106 		return (DM_CTYPE_ATA);
1107 	}
1108 
1109 	if (libdiskmgt_str_eq(di_minor_name(minor), "scsa2usb")) {
1110 		return (DM_CTYPE_USB);
1111 	}
1112 
1113 	if (libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
1114 	    libdiskmgt_str_eq(type, DDI_NT_SCSI_ATTACHMENT_POINT)) {
1115 			return (DM_CTYPE_SCSI);
1116 	}
1117 
1118 	if (libdiskmgt_str_eq(type, DDI_NT_FC_ATTACHMENT_POINT)) {
1119 		return (DM_CTYPE_FIBRE);
1120 	}
1121 
1122 	if (libdiskmgt_str_eq(type, DDI_NT_NEXUS) &&
1123 	    libdiskmgt_str_eq(name, "fp")) {
1124 		return (DM_CTYPE_FIBRE);
1125 	}
1126 
1127 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1128 	    libdiskmgt_str_eq(name, "ide")) {
1129 		return (DM_CTYPE_ATA);
1130 	}
1131 
1132 	return (DM_CTYPE_UNKNOWN);
1133 }
1134 
1135 static boolean_t
1136 disk_is_cdrom(const char *type)
1137 {
1138 	return (strncmp(type, DDI_NT_CD, strlen(DDI_NT_CD)) == 0);
1139 }
1140 
1141 static alias_t *
1142 find_alias(disk_t *diskp, char *kernel_name)
1143 {
1144 	alias_t	*ap;
1145 
1146 	ap = diskp->aliases;
1147 	while (ap != NULL) {
1148 		if (libdiskmgt_str_eq(ap->kstat_name, kernel_name)) {
1149 			return (ap);
1150 		}
1151 		ap = ap->next;
1152 	}
1153 
1154 	return (NULL);
1155 }
1156 
1157 static bus_t *
1158 find_bus(struct search_args *args, char *name)
1159 {
1160 	bus_t *listp;
1161 
1162 	listp = args->bus_listp;
1163 	while (listp != NULL) {
1164 		if (libdiskmgt_str_eq(listp->name, name)) {
1165 			return (listp);
1166 		}
1167 		listp = listp->next;
1168 	}
1169 
1170 	return (NULL);
1171 }
1172 
1173 static controller_t *
1174 find_controller(struct search_args *args, char *name)
1175 {
1176 	controller_t *listp;
1177 
1178 	listp = args->controller_listp;
1179 	while (listp != NULL) {
1180 		if (libdiskmgt_str_eq(listp->name, name)) {
1181 			return (listp);
1182 		}
1183 		listp = listp->next;
1184 	}
1185 
1186 	return (NULL);
1187 }
1188 
1189 static int
1190 fix_cluster_devpath(di_devlink_t devlink, void *arg)
1191 {
1192 	int			fd;
1193 	struct search_args	*args;
1194 	char			*devlink_path;
1195 	disk_t			*diskp = NULL;
1196 	alias_t			*ap = NULL;
1197 
1198 	/*
1199 	 * The devlink_path is of the form /dev/did/rdsk/d1s0.
1200 	 */
1201 
1202 	args =	(struct search_args *)arg;
1203 
1204 	/* Find the disk by the deviceid we read from the cluster disk. */
1205 	devlink_path = (char *)di_devlink_path(devlink);
1206 	if (devlink_path == NULL) {
1207 		return (DI_WALK_CONTINUE);
1208 	}
1209 
1210 	if ((fd = open(devlink_path, O_RDONLY|O_NDELAY)) >= 0) {
1211 		ddi_devid_t	devid;
1212 
1213 		if (dm_debug > 1) {
1214 			(void) fprintf(stderr, "INFO:     cluster devpath %s\n",
1215 			    devlink_path);
1216 		}
1217 
1218 		if (devid_get(fd, &devid) == 0) {
1219 			char *minor;
1220 			char *devidstr;
1221 
1222 			minor = di_minor_name(args->minor);
1223 
1224 			if ((devidstr =
1225 			    devid_str_encode(devid, minor)) != NULL) {
1226 				diskp = get_disk_by_deviceid(args->disk_listp,
1227 				    devidstr);
1228 				/*
1229 				 * This really shouldn't happen, since
1230 				 * we should have found all of the disks
1231 				 * during our first pass through
1232 				 * the dev tree, but just in case...
1233 				 */
1234 				if (diskp == NULL) {
1235 					if (dm_debug > 1) {
1236 						(void) fprintf(stderr,
1237 						    "INFO:    cluster create"
1238 						    " disk\n");
1239 					}
1240 
1241 					diskp = create_disk(devidstr,
1242 					    NULL, args);
1243 					if (diskp == NULL) {
1244 						args->dev_walk_status = ENOMEM;
1245 					}
1246 
1247 					/* add the controller relationship */
1248 					if (args->dev_walk_status == 0) {
1249 						if (add_disk2controller(diskp,
1250 						    args) != 0) {
1251 							args->dev_walk_status
1252 							    = ENOMEM;
1253 						}
1254 					}
1255 
1256 					if (new_alias(diskp, NULL,
1257 					    devlink_path, args) != 0) {
1258 						args->dev_walk_status = ENOMEM;
1259 					}
1260 				}
1261 				devid_str_free(devidstr);
1262 			}
1263 			devid_free(devid);
1264 		}
1265 		(void) close(fd);
1266 	}
1267 
1268 
1269 	if (diskp != NULL) {
1270 		if (dm_debug > 1) {
1271 			(void) fprintf(stderr, "INFO:     cluster found"
1272 			    " disk\n");
1273 		}
1274 		ap = diskp->aliases;
1275 	}
1276 
1277 	if (ap != NULL) {
1278 		/*
1279 		 * NOTE: if ap->next != NULL have cluster
1280 		 * disks w/ multiple paths.
1281 		 */
1282 
1283 		if (!ap->cluster) {
1284 			char	*basep;
1285 			char	*namep;
1286 			int	cnt = 0;
1287 			int	size;
1288 			char	alias[MAXPATHLEN];
1289 
1290 			/*
1291 			 * First time; save the /dev/rdsk devpaths and
1292 			 * update the alias info with the new alias name.
1293 			 */
1294 			ap->orig_paths = ap->devpaths;
1295 			ap->devpaths = NULL;
1296 
1297 			free(ap->alias);
1298 
1299 			/* get the new cluster alias name */
1300 			basep = strrchr(devlink_path, '/');
1301 			if (basep == NULL) {
1302 				basep = devlink_path;
1303 			} else {
1304 				basep++;
1305 			}
1306 			size = sizeof (alias) - 1;
1307 			namep = alias;
1308 
1309 			while (*basep != 0 && *basep != 's' && cnt < size) {
1310 				*namep++ = *basep++;
1311 				cnt++;
1312 			}
1313 			*namep = 0;
1314 
1315 			if ((ap->alias = strdup(alias)) == NULL) {
1316 				args->dev_walk_status = ENOMEM;
1317 			}
1318 
1319 			ap->cluster = 1;
1320 		}
1321 
1322 		if (new_devpath(ap, devlink_path) != 0) {
1323 			args->dev_walk_status = ENOMEM;
1324 		}
1325 	}
1326 
1327 	return (DI_WALK_CONTINUE);
1328 }
1329 
1330 /*
1331  * Check if we have the drive in our list, based upon the device id.
1332  * We got the device id from the dev tree walk.  This is encoded
1333  * using devid_str_encode(3DEVID).   In order to check the device ids we need
1334  * to use the devid_compare(3DEVID) function, so we need to decode the
1335  * string representation of the device id.
1336  */
1337 static disk_t *
1338 get_disk_by_deviceid(disk_t *listp, char *devidstr)
1339 {
1340 	ddi_devid_t	devid;
1341 
1342 	if (devidstr == NULL || devid_str_decode(devidstr, &devid, NULL) != 0) {
1343 		return (NULL);
1344 	}
1345 
1346 	while (listp != NULL) {
1347 		if (listp->devid != NULL &&
1348 		    devid_compare(listp->devid, devid) == 0) {
1349 			break;
1350 		}
1351 		listp = listp->next;
1352 	}
1353 
1354 	devid_free(devid);
1355 	return (listp);
1356 }
1357 
1358 /*
1359  * Get the base disk name with no path prefix and no slice (if there is one).
1360  * The name parameter should be big enough to hold the name.
1361  * This handles diskette names ok (/dev/rdiskette0) since there is no slice,
1362  * and converts the raw diskette name.
1363  * But, we don't know how to strip off the slice from third party drive
1364  * names.  That just means that their drive name will include a slice on
1365  * it.
1366  */
1367 static void
1368 get_disk_name_from_path(char *path, char *name, int size)
1369 {
1370 	char		*basep;
1371 	int		cnt = 0;
1372 
1373 	basep = strrchr(path, '/');
1374 	if (basep == NULL) {
1375 		basep = path;
1376 	} else {
1377 		basep++;
1378 	}
1379 
1380 	size = size - 1;	/* leave room for terminating 0 */
1381 
1382 	if (is_ctds(basep)) {
1383 		while (*basep != 0 && *basep != 's' && cnt < size) {
1384 			*name++ = *basep++;
1385 				cnt++;
1386 		}
1387 		*name = 0;
1388 	} else {
1389 		if (strncmp(basep, FLOPPY_NAME,
1390 		    sizeof (FLOPPY_NAME) - 1) == 0) {
1391 			/*
1392 			 * a floppy, convert rdiskette name to diskette name,
1393 			 * by skipping over the 'r' for raw diskette
1394 			 */
1395 			basep++;
1396 		}
1397 
1398 		/* not a ctds name, just copy it */
1399 		(void) strlcpy(name, basep, size);
1400 	}
1401 }
1402 
1403 static char *
1404 get_byte_prop(char *prop_name, di_node_t node)
1405 {
1406 	int	cnt;
1407 	uchar_t	*bytes;
1408 	int	i;
1409 	char	str[MAXPATHLEN];
1410 
1411 	cnt = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, prop_name, &bytes);
1412 	if (cnt < 1) {
1413 		return (NULL);
1414 	}
1415 
1416 	str[0] = 0;
1417 	for (i = 0; i < cnt; i++) {
1418 		char bstr[8];	/* a byte is only 2 hex chars + null */
1419 
1420 		(void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]);
1421 		(void) strlcat(str, bstr, sizeof (str));
1422 	}
1423 	return (strdup(str));
1424 }
1425 
1426 static di_node_t
1427 get_parent_bus(di_node_t node, struct search_args *args)
1428 {
1429 	di_node_t pnode;
1430 
1431 	pnode = di_parent_node(node);
1432 	if (pnode == DI_NODE_NIL) {
1433 		return (NULL);
1434 	}
1435 
1436 	if (bus_type(pnode, di_minor_next(pnode, NULL), args->ph) != NULL) {
1437 		return (pnode);
1438 	}
1439 
1440 	return (get_parent_bus(pnode, args));
1441 }
1442 
1443 static int
1444 get_prom_int(char *prop_name, di_node_t node, di_prom_handle_t ph)
1445 {
1446 	int *n;
1447 
1448 	if (di_prom_prop_lookup_ints(ph, node, prop_name, &n) == 1) {
1449 		return (*n);
1450 	}
1451 
1452 	return (0);
1453 }
1454 
1455 static char *
1456 get_prom_str(char *prop_name, di_node_t node, di_prom_handle_t ph)
1457 {
1458 	char *str;
1459 
1460 	if (di_prom_prop_lookup_strings(ph, node, prop_name, &str) == 1) {
1461 		return (str);
1462 	}
1463 
1464 	return (NULL);
1465 }
1466 
1467 /*
1468  * Get one of the positive int or boolean properties.
1469  */
1470 static int
1471 get_prop(char *prop_name, di_node_t node)
1472 {
1473 	int num;
1474 	int *ip;
1475 
1476 	if ((num = di_prop_lookup_ints(DDI_DEV_T_ANY, node, prop_name, &ip))
1477 	    >= 0) {
1478 		if (num == 0) {
1479 			/* boolean */
1480 			return (1);
1481 		} else if (num == 1) {
1482 			/* single int */
1483 			return (*ip);
1484 		}
1485 	}
1486 	return (-1);
1487 }
1488 
1489 static char *
1490 get_str_prop(char *prop_name, di_node_t node)
1491 {
1492 	char *str;
1493 
1494 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, prop_name, &str) == 1) {
1495 		return (str);
1496 	}
1497 
1498 	return (NULL);
1499 }
1500 
1501 /*
1502  * Check if we have the drive in our list, based upon the device id, if the
1503  * drive has a device id, or the kernel name, if it doesn't have a device id.
1504  */
1505 static int
1506 have_disk(struct search_args *args, char *devidstr, char *kernel_name,
1507     disk_t **diskp)
1508 {
1509 	disk_t *listp;
1510 
1511 	*diskp = NULL;
1512 	listp = args->disk_listp;
1513 	if (devidstr != NULL) {
1514 		if ((*diskp = get_disk_by_deviceid(listp, devidstr)) != NULL) {
1515 			return (1);
1516 		}
1517 
1518 	} else {
1519 		/* no devid, try matching the kernel names on the drives */
1520 		while (listp != NULL) {
1521 			if (libdiskmgt_str_eq(kernel_name,
1522 			    listp->kernel_name)) {
1523 				*diskp = listp;
1524 				return (1);
1525 			}
1526 			listp = listp->next;
1527 		}
1528 	}
1529 	return (0);
1530 }
1531 
1532 static char *
1533 bus_type(di_node_t node, di_minor_t minor, di_prom_handle_t ph)
1534 {
1535 	char	*type;
1536 	int	i;
1537 
1538 	type = get_prom_str("device_type", node, ph);
1539 	if (type == NULL) {
1540 		type = di_node_name(node);
1541 	}
1542 
1543 	for (i = 0; bustypes[i]; i++) {
1544 		if (libdiskmgt_str_eq(type, bustypes[i])) {
1545 			return (type);
1546 		}
1547 	}
1548 
1549 	if (minor != NULL && strcmp(di_minor_nodetype(minor),
1550 	    DDI_NT_USB_ATTACHMENT_POINT) == 0) {
1551 		return ("usb");
1552 	}
1553 
1554 	return (NULL);
1555 }
1556 
1557 static int
1558 is_cluster_disk(di_node_t node, di_minor_t minor)
1559 {
1560 	if (di_minor_spectype(minor) == S_IFCHR &&
1561 	    libdiskmgt_str_eq(di_minor_nodetype(minor), DDI_PSEUDO) &&
1562 	    libdiskmgt_str_eq(di_node_name(node), CLUSTER_DEV)) {
1563 		return (1);
1564 	}
1565 
1566 	return (0);
1567 }
1568 
1569 /*
1570  * If the input name is in c[t]ds format then return 1, otherwise return 0.
1571  */
1572 static int
1573 is_ctds(char *name)
1574 {
1575 	char	*p;
1576 
1577 	p = name;
1578 
1579 	if (*p++ != 'c') {
1580 		return (0);
1581 	}
1582 	/* skip controller digits */
1583 	while (isdigit(*p)) {
1584 		p++;
1585 	}
1586 
1587 	/* handle optional target */
1588 	if (*p == 't') {
1589 		p++;
1590 		/* skip over target */
1591 		while (isdigit(*p) || isupper(*p)) {
1592 			p++;
1593 		}
1594 	}
1595 
1596 	if (*p++ != 'd') {
1597 		return (0);
1598 	}
1599 	while (isdigit(*p)) {
1600 		p++;
1601 	}
1602 
1603 	if (*p++ != 's') {
1604 		return (0);
1605 	}
1606 
1607 	/* check the slice number */
1608 	while (isdigit(*p)) {
1609 		p++;
1610 	}
1611 
1612 	if (*p != 0) {
1613 		return (0);
1614 	}
1615 
1616 	return (1);
1617 }
1618 
1619 static int
1620 is_drive(di_minor_t minor)
1621 {
1622 	return (strncmp(di_minor_nodetype(minor), DDI_NT_BLOCK,
1623 	    strlen(DDI_NT_BLOCK)) == 0);
1624 }
1625 
1626 static int
1627 is_zvol(di_node_t node, di_minor_t minor)
1628 {
1629 	if ((strncmp(di_node_name(node), ZFS_DRIVER, 3) == 0) &&
1630 	    minor(di_minor_devt(minor)))
1631 		return (1);
1632 	return (0);
1633 }
1634 
1635 static int
1636 is_HBA(di_node_t node, di_minor_t minor)
1637 {
1638 	char	*type;
1639 	char	*name;
1640 	int	type_index;
1641 
1642 	type = di_minor_nodetype(minor);
1643 	type_index = 0;
1644 
1645 	while (ctrltypes[type_index] != NULL) {
1646 		if (libdiskmgt_str_eq(type, ctrltypes[type_index])) {
1647 			return (1);
1648 		}
1649 		type_index++;
1650 	}
1651 
1652 	name = di_node_name(node);
1653 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1654 	    libdiskmgt_str_eq(name, "ide")) {
1655 		return (1);
1656 	}
1657 
1658 	return (0);
1659 }
1660 
1661 static int
1662 new_alias(disk_t *diskp, char *kernel_name, char *devlink_path,
1663 	struct search_args *args)
1664 {
1665 	alias_t		*aliasp;
1666 	char		alias[MAXPATHLEN];
1667 	di_node_t	pnode;
1668 
1669 	aliasp = malloc(sizeof (alias_t));
1670 	if (aliasp == NULL) {
1671 		return (ENOMEM);
1672 	}
1673 
1674 	aliasp->alias = NULL;
1675 	aliasp->kstat_name = NULL;
1676 	aliasp->wwn = NULL;
1677 	aliasp->devpaths = NULL;
1678 	aliasp->orig_paths = NULL;
1679 
1680 	get_disk_name_from_path(devlink_path, alias, sizeof (alias));
1681 
1682 	aliasp->alias = strdup(alias);
1683 	if (aliasp->alias == NULL) {
1684 		cache_free_alias(aliasp);
1685 		return (ENOMEM);
1686 	}
1687 
1688 	if (kernel_name != NULL) {
1689 		aliasp->kstat_name = strdup(kernel_name);
1690 		if (aliasp->kstat_name == NULL) {
1691 			cache_free_alias(aliasp);
1692 			return (ENOMEM);
1693 		}
1694 	} else {
1695 		aliasp->kstat_name = NULL;
1696 	}
1697 
1698 	aliasp->cluster = 0;
1699 	aliasp->lun = get_prop(DM_LUN, args->node);
1700 	aliasp->target = get_prop(DM_TARGET, args->node);
1701 	aliasp->wwn = get_byte_prop(WWN_PROP, args->node);
1702 
1703 	pnode = di_parent_node(args->node);
1704 	if (pnode != DI_NODE_NIL) {
1705 		char prop_name[MAXPROPLEN];
1706 
1707 		(void) snprintf(prop_name, sizeof (prop_name),
1708 		    "target%d-sync-speed", aliasp->target);
1709 		diskp->sync_speed = get_prop(prop_name, pnode);
1710 		(void) snprintf(prop_name, sizeof (prop_name), "target%d-wide",
1711 		    aliasp->target);
1712 		diskp->wide = get_prop(prop_name, pnode);
1713 	}
1714 
1715 	if (new_devpath(aliasp, devlink_path) != 0) {
1716 		cache_free_alias(aliasp);
1717 		return (ENOMEM);
1718 	}
1719 
1720 	aliasp->next = diskp->aliases;
1721 	diskp->aliases = aliasp;
1722 
1723 	return (0);
1724 }
1725 
1726 /*
1727  * Append the new devpath to the end of the devpath list.  This is important
1728  * since we may want to use the order of the devpaths to match up the vtoc
1729  * entries.
1730  */
1731 static int
1732 new_devpath(alias_t *ap, char *devpath)
1733 {
1734 	slice_t	*newdp;
1735 	slice_t *alistp;
1736 
1737 	/*
1738 	 * First, search the alias list to be sure that this devpath is
1739 	 * not already there.
1740 	 */
1741 
1742 	for (alistp = ap->devpaths; alistp != NULL; alistp = alistp->next) {
1743 		if (libdiskmgt_str_eq(alistp->devpath, devpath)) {
1744 			return (0);
1745 		}
1746 	}
1747 
1748 	/*
1749 	 * Otherwise, not found so add this new devpath to the list.
1750 	 */
1751 
1752 	newdp = malloc(sizeof (slice_t));
1753 	if (newdp == NULL) {
1754 		return (ENOMEM);
1755 	}
1756 
1757 	newdp->devpath = strdup(devpath);
1758 	if (newdp->devpath == NULL) {
1759 		free(newdp);
1760 		return (ENOMEM);
1761 	}
1762 	newdp->slice_num = -1;
1763 	newdp->next = NULL;
1764 
1765 	if (ap->devpaths == NULL) {
1766 		ap->devpaths = newdp;
1767 	} else {
1768 		/* append the devpath to the end of the list */
1769 		slice_t	*dp;
1770 
1771 		dp = ap->devpaths;
1772 		while (dp->next != NULL) {
1773 			dp = dp->next;
1774 		}
1775 
1776 		dp->next = newdp;
1777 	}
1778 
1779 	return (0);
1780 }
1781 
1782 static path_t *
1783 new_path(controller_t *cp, disk_t *dp, di_node_t node, di_path_state_t st,
1784 	char *wwn)
1785 {
1786 	char		*devpath;
1787 	path_t		*pp;
1788 	di_minor_t	minor;
1789 
1790 	/* Special handling for fp attachment node. */
1791 	if (strcmp(di_node_name(node), "fp") == 0) {
1792 		di_node_t pnode;
1793 
1794 		pnode = di_parent_node(node);
1795 		if (pnode != DI_NODE_NIL) {
1796 			node = pnode;
1797 		}
1798 	}
1799 
1800 	devpath = di_devfs_path(node);
1801 
1802 	/* check if the path is already there */
1803 	pp = NULL;
1804 	if (cp->paths != NULL) {
1805 		int i;
1806 
1807 		for (i = 0; cp->paths[i]; i++) {
1808 			if (libdiskmgt_str_eq(devpath, cp->paths[i]->name)) {
1809 				pp = cp->paths[i];
1810 				break;
1811 			}
1812 		}
1813 	}
1814 
1815 	if (pp != NULL) {
1816 		/* the path exists, add this disk to it */
1817 
1818 		di_devfs_path_free((void *) devpath);
1819 		if (!add_disk2path(dp, pp, st, wwn)) {
1820 			return (NULL);
1821 		}
1822 		return (pp);
1823 	}
1824 
1825 	/* create a new path */
1826 
1827 	pp = calloc(1, sizeof (path_t));
1828 	if (pp == NULL) {
1829 		di_devfs_path_free((void *) devpath);
1830 		return (NULL);
1831 	}
1832 
1833 	pp->name = strdup(devpath);
1834 	di_devfs_path_free((void *) devpath);
1835 	if (pp->name == NULL) {
1836 		cache_free_path(pp);
1837 		return (NULL);
1838 	}
1839 
1840 	/* add the disk to the path */
1841 	if (!add_disk2path(dp, pp, st, wwn)) {
1842 		return (NULL);
1843 	}
1844 
1845 	/* add the path to the controller */
1846 	if (add_ptr2array(pp, (void ***)&cp->paths) != 0) {
1847 		cache_free_path(pp);
1848 		return (NULL);
1849 	}
1850 
1851 	/* add the controller to the path */
1852 	pp->controller = cp;
1853 
1854 	minor = di_minor_next(node, NULL);
1855 	if (minor != NULL) {
1856 		pp->ctype = ctype(node, minor);
1857 	} else {
1858 		pp->ctype = DM_CTYPE_UNKNOWN;
1859 	}
1860 
1861 	return (pp);
1862 }
1863 
1864 /*
1865  * We pass in the current controller pointer (currp) so we can double check
1866  * that we aren't corrupting the list by removing the element we are on.  This
1867  * should never happen, but it doesn't hurt to double check.
1868  */
1869 static void
1870 remove_invalid_controller(char *name, controller_t *currp,
1871     struct search_args *args)
1872 {
1873 	controller_t *cp;
1874 	bus_t *bp;
1875 	controller_t *prevp;
1876 
1877 	bp = args->bus_listp;
1878 	while (bp != NULL) {
1879 		int i;
1880 
1881 		for (i = 0; bp->controllers[i]; i++) {
1882 			if (libdiskmgt_str_eq(bp->controllers[i]->name, name)) {
1883 				int j;
1884 				/*
1885 				 * remove pointer to invalid controller.
1886 				 * (it is a path)
1887 				 */
1888 				for (j = i; bp->controllers[j]; j++) {
1889 					bp->controllers[j] =
1890 					    bp->controllers[j + 1];
1891 				}
1892 			}
1893 		}
1894 		bp = bp->next;
1895 	}
1896 
1897 	if (args->controller_listp == NULL) {
1898 		return;
1899 	}
1900 
1901 	cp = args->controller_listp;
1902 	if (libdiskmgt_str_eq(cp->name, name)) {
1903 		args->controller_listp = cp->next;
1904 		if (dm_debug) {
1905 			(void) fprintf(stderr,
1906 			    "INFO: Removed controller %s from list\n",
1907 			    cp->name);
1908 		}
1909 		remove_controller(cp, currp);
1910 		return;
1911 	}
1912 
1913 	prevp = cp;
1914 	cp = cp->next;
1915 	while (cp != NULL) {
1916 		if (libdiskmgt_str_eq(cp->name, name)) {
1917 			if (dm_debug) {
1918 				(void) fprintf(stderr,
1919 				    "INFO: Removed controller %s from list\n",
1920 				    cp->name);
1921 			}
1922 			prevp->next = cp->next;
1923 			remove_controller(cp, currp);
1924 			return;
1925 		}
1926 		prevp = cp;
1927 		cp = cp->next;
1928 	}
1929 }
1930 
1931 /*
1932  * This is the standard strstr code modified for case independence.
1933  */
1934 static char *
1935 str_case_index(register char *s1, register char *s2)
1936 {
1937 	uint_t s2len = strlen(s2); /* length of the second string */
1938 
1939 	/* If the length of the second string is 0, return the first arg. */
1940 	if (s2len == 0) {
1941 		return (s1);
1942 	}
1943 
1944 	while (strlen(s1) >= s2len) {
1945 		if (strncasecmp(s1, s2, s2len) == 0) {
1946 			return (s1);
1947 		}
1948 		s1++;
1949 	}
1950 	return (NULL);
1951 }
1952