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