xref: /freebsd/sys/contrib/openzfs/lib/libzutil/zutil_import.c (revision 058ac3e8063366dafa634d9107642e12b038bf09)
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 https://opensource.org/licenses/CDDL-1.0.
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 2015 Nexenta Systems, Inc. All rights reserved.
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25  * Copyright 2015 RackTop Systems.
26  * Copyright (c) 2016, Intel Corporation.
27  * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
28  */
29 
30 /*
31  * Pool import support functions.
32  *
33  * Used by zpool, ztest, zdb, and zhack to locate importable configs. Since
34  * these commands are expected to run in the global zone, we can assume
35  * that the devices are all readable when called.
36  *
37  * To import a pool, we rely on reading the configuration information from the
38  * ZFS label of each device.  If we successfully read the label, then we
39  * organize the configuration information in the following hierarchy:
40  *
41  *	pool guid -> toplevel vdev guid -> label txg
42  *
43  * Duplicate entries matching this same tuple will be discarded.  Once we have
44  * examined every device, we pick the best label txg config for each toplevel
45  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
46  * update any paths that have changed.  Finally, we attempt to import the pool
47  * using our derived config, and record the results.
48  */
49 
50 #ifdef HAVE_AIO_H
51 #include <aio.h>
52 #endif
53 #include <ctype.h>
54 #include <dirent.h>
55 #include <errno.h>
56 #include <libintl.h>
57 #include <libgen.h>
58 #include <stddef.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sys/stat.h>
62 #include <unistd.h>
63 #include <fcntl.h>
64 #include <sys/dktp/fdisk.h>
65 #include <sys/vdev_impl.h>
66 #include <sys/fs/zfs.h>
67 
68 #include <thread_pool.h>
69 #include <libzutil.h>
70 #include <libnvpair.h>
71 
72 #include "zutil_import.h"
73 
74 const char *
75 libpc_error_description(libpc_handle_t *hdl)
76 {
77 	if (hdl->lpc_desc[0] != '\0')
78 		return (hdl->lpc_desc);
79 
80 	switch (hdl->lpc_error) {
81 	case LPC_BADCACHE:
82 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
83 	case LPC_BADPATH:
84 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
85 	case LPC_NOMEM:
86 		return (dgettext(TEXT_DOMAIN, "out of memory"));
87 	case LPC_EACCESS:
88 		return (dgettext(TEXT_DOMAIN, "some devices require root "
89 		    "privileges"));
90 	case LPC_UNKNOWN:
91 		return (dgettext(TEXT_DOMAIN, "unknown error"));
92 	default:
93 		assert(hdl->lpc_error == 0);
94 		return (dgettext(TEXT_DOMAIN, "no error"));
95 	}
96 }
97 
98 static __attribute__((format(printf, 2, 3))) void
99 zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...)
100 {
101 	va_list ap;
102 
103 	va_start(ap, fmt);
104 
105 	(void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap);
106 	hdl->lpc_desc_active = B_TRUE;
107 
108 	va_end(ap);
109 }
110 
111 static void
112 zutil_verror(libpc_handle_t *hdl, lpc_error_t error, const char *fmt,
113     va_list ap)
114 {
115 	char action[1024];
116 
117 	(void) vsnprintf(action, sizeof (action), fmt, ap);
118 	hdl->lpc_error = error;
119 
120 	if (hdl->lpc_desc_active)
121 		hdl->lpc_desc_active = B_FALSE;
122 	else
123 		hdl->lpc_desc[0] = '\0';
124 
125 	if (hdl->lpc_printerr)
126 		(void) fprintf(stderr, "%s: %s\n", action,
127 		    libpc_error_description(hdl));
128 }
129 
130 static __attribute__((format(printf, 3, 4))) int
131 zutil_error_fmt(libpc_handle_t *hdl, lpc_error_t error,
132     const char *fmt, ...)
133 {
134 	va_list ap;
135 
136 	va_start(ap, fmt);
137 
138 	zutil_verror(hdl, error, fmt, ap);
139 
140 	va_end(ap);
141 
142 	return (-1);
143 }
144 
145 static int
146 zutil_error(libpc_handle_t *hdl, lpc_error_t error, const char *msg)
147 {
148 	return (zutil_error_fmt(hdl, error, "%s", msg));
149 }
150 
151 static int
152 zutil_no_memory(libpc_handle_t *hdl)
153 {
154 	zutil_error(hdl, LPC_NOMEM, "internal error");
155 	exit(1);
156 }
157 
158 void *
159 zutil_alloc(libpc_handle_t *hdl, size_t size)
160 {
161 	void *data;
162 
163 	if ((data = calloc(1, size)) == NULL)
164 		(void) zutil_no_memory(hdl);
165 
166 	return (data);
167 }
168 
169 char *
170 zutil_strdup(libpc_handle_t *hdl, const char *str)
171 {
172 	char *ret;
173 
174 	if ((ret = strdup(str)) == NULL)
175 		(void) zutil_no_memory(hdl);
176 
177 	return (ret);
178 }
179 
180 static char *
181 zutil_strndup(libpc_handle_t *hdl, const char *str, size_t n)
182 {
183 	char *ret;
184 
185 	if ((ret = strndup(str, n)) == NULL)
186 		(void) zutil_no_memory(hdl);
187 
188 	return (ret);
189 }
190 
191 /*
192  * Intermediate structures used to gather configuration information.
193  */
194 typedef struct config_entry {
195 	uint64_t		ce_txg;
196 	nvlist_t		*ce_config;
197 	struct config_entry	*ce_next;
198 } config_entry_t;
199 
200 typedef struct vdev_entry {
201 	uint64_t		ve_guid;
202 	config_entry_t		*ve_configs;
203 	struct vdev_entry	*ve_next;
204 } vdev_entry_t;
205 
206 typedef struct pool_entry {
207 	uint64_t		pe_guid;
208 	vdev_entry_t		*pe_vdevs;
209 	struct pool_entry	*pe_next;
210 } pool_entry_t;
211 
212 typedef struct name_entry {
213 	char			*ne_name;
214 	uint64_t		ne_guid;
215 	uint64_t		ne_order;
216 	uint64_t		ne_num_labels;
217 	struct name_entry	*ne_next;
218 } name_entry_t;
219 
220 typedef struct pool_list {
221 	pool_entry_t		*pools;
222 	name_entry_t		*names;
223 } pool_list_t;
224 
225 /*
226  * Go through and fix up any path and/or devid information for the given vdev
227  * configuration.
228  */
229 static int
230 fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names)
231 {
232 	nvlist_t **child;
233 	uint_t c, children;
234 	uint64_t guid;
235 	name_entry_t *ne, *best;
236 	char *path;
237 
238 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
239 	    &child, &children) == 0) {
240 		for (c = 0; c < children; c++)
241 			if (fix_paths(hdl, child[c], names) != 0)
242 				return (-1);
243 		return (0);
244 	}
245 
246 	/*
247 	 * This is a leaf (file or disk) vdev.  In either case, go through
248 	 * the name list and see if we find a matching guid.  If so, replace
249 	 * the path and see if we can calculate a new devid.
250 	 *
251 	 * There may be multiple names associated with a particular guid, in
252 	 * which case we have overlapping partitions or multiple paths to the
253 	 * same disk.  In this case we prefer to use the path name which
254 	 * matches the ZPOOL_CONFIG_PATH.  If no matching entry is found we
255 	 * use the lowest order device which corresponds to the first match
256 	 * while traversing the ZPOOL_IMPORT_PATH search path.
257 	 */
258 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
259 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
260 		path = NULL;
261 
262 	best = NULL;
263 	for (ne = names; ne != NULL; ne = ne->ne_next) {
264 		if (ne->ne_guid == guid) {
265 			if (path == NULL) {
266 				best = ne;
267 				break;
268 			}
269 
270 			if ((strlen(path) == strlen(ne->ne_name)) &&
271 			    strncmp(path, ne->ne_name, strlen(path)) == 0) {
272 				best = ne;
273 				break;
274 			}
275 
276 			if (best == NULL) {
277 				best = ne;
278 				continue;
279 			}
280 
281 			/* Prefer paths with move vdev labels. */
282 			if (ne->ne_num_labels > best->ne_num_labels) {
283 				best = ne;
284 				continue;
285 			}
286 
287 			/* Prefer paths earlier in the search order. */
288 			if (ne->ne_num_labels == best->ne_num_labels &&
289 			    ne->ne_order < best->ne_order) {
290 				best = ne;
291 				continue;
292 			}
293 		}
294 	}
295 
296 	if (best == NULL)
297 		return (0);
298 
299 	if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
300 		return (-1);
301 
302 	update_vdev_config_dev_strs(nv);
303 
304 	return (0);
305 }
306 
307 /*
308  * Add the given configuration to the list of known devices.
309  */
310 static int
311 add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path,
312     int order, int num_labels, nvlist_t *config)
313 {
314 	uint64_t pool_guid, vdev_guid, top_guid, txg, state;
315 	pool_entry_t *pe;
316 	vdev_entry_t *ve;
317 	config_entry_t *ce;
318 	name_entry_t *ne;
319 
320 	/*
321 	 * If this is a hot spare not currently in use or level 2 cache
322 	 * device, add it to the list of names to translate, but don't do
323 	 * anything else.
324 	 */
325 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
326 	    &state) == 0 &&
327 	    (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
328 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
329 		if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
330 			return (-1);
331 
332 		if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
333 			free(ne);
334 			return (-1);
335 		}
336 		ne->ne_guid = vdev_guid;
337 		ne->ne_order = order;
338 		ne->ne_num_labels = num_labels;
339 		ne->ne_next = pl->names;
340 		pl->names = ne;
341 
342 		return (0);
343 	}
344 
345 	/*
346 	 * If we have a valid config but cannot read any of these fields, then
347 	 * it means we have a half-initialized label.  In vdev_label_init()
348 	 * we write a label with txg == 0 so that we can identify the device
349 	 * in case the user refers to the same disk later on.  If we fail to
350 	 * create the pool, we'll be left with a label in this state
351 	 * which should not be considered part of a valid pool.
352 	 */
353 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
354 	    &pool_guid) != 0 ||
355 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
356 	    &vdev_guid) != 0 ||
357 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
358 	    &top_guid) != 0 ||
359 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
360 	    &txg) != 0 || txg == 0) {
361 		return (0);
362 	}
363 
364 	/*
365 	 * First, see if we know about this pool.  If not, then add it to the
366 	 * list of known pools.
367 	 */
368 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
369 		if (pe->pe_guid == pool_guid)
370 			break;
371 	}
372 
373 	if (pe == NULL) {
374 		if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
375 			return (-1);
376 		}
377 		pe->pe_guid = pool_guid;
378 		pe->pe_next = pl->pools;
379 		pl->pools = pe;
380 	}
381 
382 	/*
383 	 * Second, see if we know about this toplevel vdev.  Add it if its
384 	 * missing.
385 	 */
386 	for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
387 		if (ve->ve_guid == top_guid)
388 			break;
389 	}
390 
391 	if (ve == NULL) {
392 		if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
393 			return (-1);
394 		}
395 		ve->ve_guid = top_guid;
396 		ve->ve_next = pe->pe_vdevs;
397 		pe->pe_vdevs = ve;
398 	}
399 
400 	/*
401 	 * Third, see if we have a config with a matching transaction group.  If
402 	 * so, then we do nothing.  Otherwise, add it to the list of known
403 	 * configs.
404 	 */
405 	for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
406 		if (ce->ce_txg == txg)
407 			break;
408 	}
409 
410 	if (ce == NULL) {
411 		if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) {
412 			return (-1);
413 		}
414 		ce->ce_txg = txg;
415 		ce->ce_config = fnvlist_dup(config);
416 		ce->ce_next = ve->ve_configs;
417 		ve->ve_configs = ce;
418 	}
419 
420 	/*
421 	 * At this point we've successfully added our config to the list of
422 	 * known configs.  The last thing to do is add the vdev guid -> path
423 	 * mappings so that we can fix up the configuration as necessary before
424 	 * doing the import.
425 	 */
426 	if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
427 		return (-1);
428 
429 	if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
430 		free(ne);
431 		return (-1);
432 	}
433 
434 	ne->ne_guid = vdev_guid;
435 	ne->ne_order = order;
436 	ne->ne_num_labels = num_labels;
437 	ne->ne_next = pl->names;
438 	pl->names = ne;
439 
440 	return (0);
441 }
442 
443 static int
444 zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid,
445     boolean_t *isactive)
446 {
447 	ASSERT(hdl->lpc_ops->pco_pool_active != NULL);
448 
449 	int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name,
450 	    guid, isactive);
451 
452 	return (error);
453 }
454 
455 static nvlist_t *
456 zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig)
457 {
458 	ASSERT(hdl->lpc_ops->pco_refresh_config != NULL);
459 
460 	return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle,
461 	    tryconfig));
462 }
463 
464 /*
465  * Determine if the vdev id is a hole in the namespace.
466  */
467 static boolean_t
468 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
469 {
470 	int c;
471 
472 	for (c = 0; c < holes; c++) {
473 
474 		/* Top-level is a hole */
475 		if (hole_array[c] == id)
476 			return (B_TRUE);
477 	}
478 	return (B_FALSE);
479 }
480 
481 /*
482  * Convert our list of pools into the definitive set of configurations.  We
483  * start by picking the best config for each toplevel vdev.  Once that's done,
484  * we assemble the toplevel vdevs into a full config for the pool.  We make a
485  * pass to fix up any incorrect paths, and then add it to the main list to
486  * return to the user.
487  */
488 static nvlist_t *
489 get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok,
490     nvlist_t *policy)
491 {
492 	pool_entry_t *pe;
493 	vdev_entry_t *ve;
494 	config_entry_t *ce;
495 	nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
496 	nvlist_t **spares, **l2cache;
497 	uint_t i, nspares, nl2cache;
498 	boolean_t config_seen;
499 	uint64_t best_txg;
500 	char *name, *hostname = NULL;
501 	uint64_t guid;
502 	uint_t children = 0;
503 	nvlist_t **child = NULL;
504 	uint64_t *hole_array, max_id;
505 	uint_t c;
506 	boolean_t isactive;
507 	nvlist_t *nvl;
508 	boolean_t valid_top_config = B_FALSE;
509 
510 	if (nvlist_alloc(&ret, 0, 0) != 0)
511 		goto nomem;
512 
513 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
514 		uint64_t id, max_txg = 0, hostid = 0;
515 		uint_t holes = 0;
516 
517 		if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
518 			goto nomem;
519 		config_seen = B_FALSE;
520 
521 		/*
522 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
523 		 * from the first one we find, and then go through the rest and
524 		 * add them as necessary to the 'vdevs' member of the config.
525 		 */
526 		for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
527 
528 			/*
529 			 * Determine the best configuration for this vdev by
530 			 * selecting the config with the latest transaction
531 			 * group.
532 			 */
533 			best_txg = 0;
534 			for (ce = ve->ve_configs; ce != NULL;
535 			    ce = ce->ce_next) {
536 
537 				if (ce->ce_txg > best_txg) {
538 					tmp = ce->ce_config;
539 					best_txg = ce->ce_txg;
540 				}
541 			}
542 
543 			/*
544 			 * We rely on the fact that the max txg for the
545 			 * pool will contain the most up-to-date information
546 			 * about the valid top-levels in the vdev namespace.
547 			 */
548 			if (best_txg > max_txg) {
549 				(void) nvlist_remove(config,
550 				    ZPOOL_CONFIG_VDEV_CHILDREN,
551 				    DATA_TYPE_UINT64);
552 				(void) nvlist_remove(config,
553 				    ZPOOL_CONFIG_HOLE_ARRAY,
554 				    DATA_TYPE_UINT64_ARRAY);
555 
556 				max_txg = best_txg;
557 				hole_array = NULL;
558 				holes = 0;
559 				max_id = 0;
560 				valid_top_config = B_FALSE;
561 
562 				if (nvlist_lookup_uint64(tmp,
563 				    ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
564 					verify(nvlist_add_uint64(config,
565 					    ZPOOL_CONFIG_VDEV_CHILDREN,
566 					    max_id) == 0);
567 					valid_top_config = B_TRUE;
568 				}
569 
570 				if (nvlist_lookup_uint64_array(tmp,
571 				    ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
572 				    &holes) == 0) {
573 					verify(nvlist_add_uint64_array(config,
574 					    ZPOOL_CONFIG_HOLE_ARRAY,
575 					    hole_array, holes) == 0);
576 				}
577 			}
578 
579 			if (!config_seen) {
580 				/*
581 				 * Copy the relevant pieces of data to the pool
582 				 * configuration:
583 				 *
584 				 *	version
585 				 *	pool guid
586 				 *	name
587 				 *	comment (if available)
588 				 *	compatibility features (if available)
589 				 *	pool state
590 				 *	hostid (if available)
591 				 *	hostname (if available)
592 				 */
593 				uint64_t state, version;
594 				char *comment = NULL;
595 				char *compatibility = NULL;
596 
597 				version = fnvlist_lookup_uint64(tmp,
598 				    ZPOOL_CONFIG_VERSION);
599 				fnvlist_add_uint64(config,
600 				    ZPOOL_CONFIG_VERSION, version);
601 				guid = fnvlist_lookup_uint64(tmp,
602 				    ZPOOL_CONFIG_POOL_GUID);
603 				fnvlist_add_uint64(config,
604 				    ZPOOL_CONFIG_POOL_GUID, guid);
605 				name = fnvlist_lookup_string(tmp,
606 				    ZPOOL_CONFIG_POOL_NAME);
607 				fnvlist_add_string(config,
608 				    ZPOOL_CONFIG_POOL_NAME, name);
609 
610 				if (nvlist_lookup_string(tmp,
611 				    ZPOOL_CONFIG_COMMENT, &comment) == 0)
612 					fnvlist_add_string(config,
613 					    ZPOOL_CONFIG_COMMENT, comment);
614 
615 				if (nvlist_lookup_string(tmp,
616 				    ZPOOL_CONFIG_COMPATIBILITY,
617 				    &compatibility) == 0)
618 					fnvlist_add_string(config,
619 					    ZPOOL_CONFIG_COMPATIBILITY,
620 					    compatibility);
621 
622 				state = fnvlist_lookup_uint64(tmp,
623 				    ZPOOL_CONFIG_POOL_STATE);
624 				fnvlist_add_uint64(config,
625 				    ZPOOL_CONFIG_POOL_STATE, state);
626 
627 				hostid = 0;
628 				if (nvlist_lookup_uint64(tmp,
629 				    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
630 					fnvlist_add_uint64(config,
631 					    ZPOOL_CONFIG_HOSTID, hostid);
632 					hostname = fnvlist_lookup_string(tmp,
633 					    ZPOOL_CONFIG_HOSTNAME);
634 					fnvlist_add_string(config,
635 					    ZPOOL_CONFIG_HOSTNAME, hostname);
636 				}
637 
638 				config_seen = B_TRUE;
639 			}
640 
641 			/*
642 			 * Add this top-level vdev to the child array.
643 			 */
644 			verify(nvlist_lookup_nvlist(tmp,
645 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
646 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
647 			    &id) == 0);
648 
649 			if (id >= children) {
650 				nvlist_t **newchild;
651 
652 				newchild = zutil_alloc(hdl, (id + 1) *
653 				    sizeof (nvlist_t *));
654 				if (newchild == NULL)
655 					goto nomem;
656 
657 				for (c = 0; c < children; c++)
658 					newchild[c] = child[c];
659 
660 				free(child);
661 				child = newchild;
662 				children = id + 1;
663 			}
664 			if (nvlist_dup(nvtop, &child[id], 0) != 0)
665 				goto nomem;
666 
667 		}
668 
669 		/*
670 		 * If we have information about all the top-levels then
671 		 * clean up the nvlist which we've constructed. This
672 		 * means removing any extraneous devices that are
673 		 * beyond the valid range or adding devices to the end
674 		 * of our array which appear to be missing.
675 		 */
676 		if (valid_top_config) {
677 			if (max_id < children) {
678 				for (c = max_id; c < children; c++)
679 					nvlist_free(child[c]);
680 				children = max_id;
681 			} else if (max_id > children) {
682 				nvlist_t **newchild;
683 
684 				newchild = zutil_alloc(hdl, (max_id) *
685 				    sizeof (nvlist_t *));
686 				if (newchild == NULL)
687 					goto nomem;
688 
689 				for (c = 0; c < children; c++)
690 					newchild[c] = child[c];
691 
692 				free(child);
693 				child = newchild;
694 				children = max_id;
695 			}
696 		}
697 
698 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
699 		    &guid) == 0);
700 
701 		/*
702 		 * The vdev namespace may contain holes as a result of
703 		 * device removal. We must add them back into the vdev
704 		 * tree before we process any missing devices.
705 		 */
706 		if (holes > 0) {
707 			ASSERT(valid_top_config);
708 
709 			for (c = 0; c < children; c++) {
710 				nvlist_t *holey;
711 
712 				if (child[c] != NULL ||
713 				    !vdev_is_hole(hole_array, holes, c))
714 					continue;
715 
716 				if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
717 				    0) != 0)
718 					goto nomem;
719 
720 				/*
721 				 * Holes in the namespace are treated as
722 				 * "hole" top-level vdevs and have a
723 				 * special flag set on them.
724 				 */
725 				if (nvlist_add_string(holey,
726 				    ZPOOL_CONFIG_TYPE,
727 				    VDEV_TYPE_HOLE) != 0 ||
728 				    nvlist_add_uint64(holey,
729 				    ZPOOL_CONFIG_ID, c) != 0 ||
730 				    nvlist_add_uint64(holey,
731 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
732 					nvlist_free(holey);
733 					goto nomem;
734 				}
735 				child[c] = holey;
736 			}
737 		}
738 
739 		/*
740 		 * Look for any missing top-level vdevs.  If this is the case,
741 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
742 		 * simply compress the child array, because the kernel performs
743 		 * certain checks to make sure the vdev IDs match their location
744 		 * in the configuration.
745 		 */
746 		for (c = 0; c < children; c++) {
747 			if (child[c] == NULL) {
748 				nvlist_t *missing;
749 				if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
750 				    0) != 0)
751 					goto nomem;
752 				if (nvlist_add_string(missing,
753 				    ZPOOL_CONFIG_TYPE,
754 				    VDEV_TYPE_MISSING) != 0 ||
755 				    nvlist_add_uint64(missing,
756 				    ZPOOL_CONFIG_ID, c) != 0 ||
757 				    nvlist_add_uint64(missing,
758 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
759 					nvlist_free(missing);
760 					goto nomem;
761 				}
762 				child[c] = missing;
763 			}
764 		}
765 
766 		/*
767 		 * Put all of this pool's top-level vdevs into a root vdev.
768 		 */
769 		if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
770 			goto nomem;
771 		if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
772 		    VDEV_TYPE_ROOT) != 0 ||
773 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
774 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
775 		    nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
776 		    (const nvlist_t **)child, children) != 0) {
777 			nvlist_free(nvroot);
778 			goto nomem;
779 		}
780 
781 		for (c = 0; c < children; c++)
782 			nvlist_free(child[c]);
783 		free(child);
784 		children = 0;
785 		child = NULL;
786 
787 		/*
788 		 * Go through and fix up any paths and/or devids based on our
789 		 * known list of vdev GUID -> path mappings.
790 		 */
791 		if (fix_paths(hdl, nvroot, pl->names) != 0) {
792 			nvlist_free(nvroot);
793 			goto nomem;
794 		}
795 
796 		/*
797 		 * Add the root vdev to this pool's configuration.
798 		 */
799 		if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
800 		    nvroot) != 0) {
801 			nvlist_free(nvroot);
802 			goto nomem;
803 		}
804 		nvlist_free(nvroot);
805 
806 		/*
807 		 * zdb uses this path to report on active pools that were
808 		 * imported or created using -R.
809 		 */
810 		if (active_ok)
811 			goto add_pool;
812 
813 		/*
814 		 * Determine if this pool is currently active, in which case we
815 		 * can't actually import it.
816 		 */
817 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
818 		    &name) == 0);
819 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
820 		    &guid) == 0);
821 
822 		if (zutil_pool_active(hdl, name, guid, &isactive) != 0)
823 			goto error;
824 
825 		if (isactive) {
826 			nvlist_free(config);
827 			config = NULL;
828 			continue;
829 		}
830 
831 		if (policy != NULL) {
832 			if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
833 			    policy) != 0)
834 				goto nomem;
835 		}
836 
837 		if ((nvl = zutil_refresh_config(hdl, config)) == NULL) {
838 			nvlist_free(config);
839 			config = NULL;
840 			continue;
841 		}
842 
843 		nvlist_free(config);
844 		config = nvl;
845 
846 		/*
847 		 * Go through and update the paths for spares, now that we have
848 		 * them.
849 		 */
850 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
851 		    &nvroot) == 0);
852 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
853 		    &spares, &nspares) == 0) {
854 			for (i = 0; i < nspares; i++) {
855 				if (fix_paths(hdl, spares[i], pl->names) != 0)
856 					goto nomem;
857 			}
858 		}
859 
860 		/*
861 		 * Update the paths for l2cache devices.
862 		 */
863 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
864 		    &l2cache, &nl2cache) == 0) {
865 			for (i = 0; i < nl2cache; i++) {
866 				if (fix_paths(hdl, l2cache[i], pl->names) != 0)
867 					goto nomem;
868 			}
869 		}
870 
871 		/*
872 		 * Restore the original information read from the actual label.
873 		 */
874 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
875 		    DATA_TYPE_UINT64);
876 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
877 		    DATA_TYPE_STRING);
878 		if (hostid != 0) {
879 			verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
880 			    hostid) == 0);
881 			verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
882 			    hostname) == 0);
883 		}
884 
885 add_pool:
886 		/*
887 		 * Add this pool to the list of configs.
888 		 */
889 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
890 		    &name) == 0);
891 
892 		if (nvlist_add_nvlist(ret, name, config) != 0)
893 			goto nomem;
894 
895 		nvlist_free(config);
896 		config = NULL;
897 	}
898 
899 	return (ret);
900 
901 nomem:
902 	(void) zutil_no_memory(hdl);
903 error:
904 	nvlist_free(config);
905 	nvlist_free(ret);
906 	for (c = 0; c < children; c++)
907 		nvlist_free(child[c]);
908 	free(child);
909 
910 	return (NULL);
911 }
912 
913 /*
914  * Return the offset of the given label.
915  */
916 static uint64_t
917 label_offset(uint64_t size, int l)
918 {
919 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
920 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
921 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
922 }
923 
924 /*
925  * The same description applies as to zpool_read_label below,
926  * except here we do it without aio, presumably because an aio call
927  * errored out in a way we think not using it could circumvent.
928  */
929 static int
930 zpool_read_label_slow(int fd, nvlist_t **config, int *num_labels)
931 {
932 	struct stat64 statbuf;
933 	int l, count = 0;
934 	vdev_phys_t *label;
935 	nvlist_t *expected_config = NULL;
936 	uint64_t expected_guid = 0, size;
937 
938 	*config = NULL;
939 
940 	if (fstat64_blk(fd, &statbuf) == -1)
941 		return (0);
942 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
943 
944 	label = (vdev_phys_t *)umem_alloc_aligned(sizeof (*label), PAGESIZE,
945 	    UMEM_DEFAULT);
946 	if (label == NULL)
947 		return (-1);
948 
949 	for (l = 0; l < VDEV_LABELS; l++) {
950 		uint64_t state, guid, txg;
951 		off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE;
952 
953 		if (pread64(fd, label, sizeof (vdev_phys_t),
954 		    offset) != sizeof (vdev_phys_t))
955 			continue;
956 
957 		if (nvlist_unpack(label->vp_nvlist,
958 		    sizeof (label->vp_nvlist), config, 0) != 0)
959 			continue;
960 
961 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
962 		    &guid) != 0 || guid == 0) {
963 			nvlist_free(*config);
964 			continue;
965 		}
966 
967 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
968 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
969 			nvlist_free(*config);
970 			continue;
971 		}
972 
973 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
974 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
975 		    &txg) != 0 || txg == 0)) {
976 			nvlist_free(*config);
977 			continue;
978 		}
979 
980 		if (expected_guid) {
981 			if (expected_guid == guid)
982 				count++;
983 
984 			nvlist_free(*config);
985 		} else {
986 			expected_config = *config;
987 			expected_guid = guid;
988 			count++;
989 		}
990 	}
991 
992 	if (num_labels != NULL)
993 		*num_labels = count;
994 
995 	umem_free_aligned(label, sizeof (*label));
996 	*config = expected_config;
997 
998 	return (0);
999 }
1000 
1001 /*
1002  * Given a file descriptor, read the label information and return an nvlist
1003  * describing the configuration, if there is one.  The number of valid
1004  * labels found will be returned in num_labels when non-NULL.
1005  */
1006 int
1007 zpool_read_label(int fd, nvlist_t **config, int *num_labels)
1008 {
1009 #ifndef HAVE_AIO_H
1010 	return (zpool_read_label_slow(fd, config, num_labels));
1011 #else
1012 	struct stat64 statbuf;
1013 	struct aiocb aiocbs[VDEV_LABELS];
1014 	struct aiocb *aiocbps[VDEV_LABELS];
1015 	vdev_phys_t *labels;
1016 	nvlist_t *expected_config = NULL;
1017 	uint64_t expected_guid = 0, size;
1018 	int error, l, count = 0;
1019 
1020 	*config = NULL;
1021 
1022 	if (fstat64_blk(fd, &statbuf) == -1)
1023 		return (0);
1024 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1025 
1026 	labels = (vdev_phys_t *)umem_alloc_aligned(
1027 	    VDEV_LABELS * sizeof (*labels), PAGESIZE, UMEM_DEFAULT);
1028 	if (labels == NULL)
1029 		return (-1);
1030 
1031 	memset(aiocbs, 0, sizeof (aiocbs));
1032 	for (l = 0; l < VDEV_LABELS; l++) {
1033 		off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE;
1034 
1035 		aiocbs[l].aio_fildes = fd;
1036 		aiocbs[l].aio_offset = offset;
1037 		aiocbs[l].aio_buf = &labels[l];
1038 		aiocbs[l].aio_nbytes = sizeof (vdev_phys_t);
1039 		aiocbs[l].aio_lio_opcode = LIO_READ;
1040 		aiocbps[l] = &aiocbs[l];
1041 	}
1042 
1043 	if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) {
1044 		int saved_errno = errno;
1045 		boolean_t do_slow = B_FALSE;
1046 		error = -1;
1047 
1048 		if (errno == EAGAIN || errno == EINTR || errno == EIO) {
1049 			/*
1050 			 * A portion of the requests may have been submitted.
1051 			 * Clean them up.
1052 			 */
1053 			for (l = 0; l < VDEV_LABELS; l++) {
1054 				errno = 0;
1055 				switch (aio_error(&aiocbs[l])) {
1056 				case EINVAL:
1057 					break;
1058 				case EINPROGRESS:
1059 					// This shouldn't be possible to
1060 					// encounter, die if we do.
1061 					ASSERT(B_FALSE);
1062 					zfs_fallthrough;
1063 				case EOPNOTSUPP:
1064 				case ENOSYS:
1065 					do_slow = B_TRUE;
1066 					zfs_fallthrough;
1067 				case 0:
1068 				default:
1069 					(void) aio_return(&aiocbs[l]);
1070 				}
1071 			}
1072 		}
1073 		if (do_slow) {
1074 			/*
1075 			 * At least some IO involved access unsafe-for-AIO
1076 			 * files. Let's try again, without AIO this time.
1077 			 */
1078 			error = zpool_read_label_slow(fd, config, num_labels);
1079 			saved_errno = errno;
1080 		}
1081 		umem_free_aligned(labels, VDEV_LABELS * sizeof (*labels));
1082 		errno = saved_errno;
1083 		return (error);
1084 	}
1085 
1086 	for (l = 0; l < VDEV_LABELS; l++) {
1087 		uint64_t state, guid, txg;
1088 
1089 		if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t))
1090 			continue;
1091 
1092 		if (nvlist_unpack(labels[l].vp_nvlist,
1093 		    sizeof (labels[l].vp_nvlist), config, 0) != 0)
1094 			continue;
1095 
1096 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
1097 		    &guid) != 0 || guid == 0) {
1098 			nvlist_free(*config);
1099 			continue;
1100 		}
1101 
1102 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
1103 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
1104 			nvlist_free(*config);
1105 			continue;
1106 		}
1107 
1108 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1109 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
1110 		    &txg) != 0 || txg == 0)) {
1111 			nvlist_free(*config);
1112 			continue;
1113 		}
1114 
1115 		if (expected_guid) {
1116 			if (expected_guid == guid)
1117 				count++;
1118 
1119 			nvlist_free(*config);
1120 		} else {
1121 			expected_config = *config;
1122 			expected_guid = guid;
1123 			count++;
1124 		}
1125 	}
1126 
1127 	if (num_labels != NULL)
1128 		*num_labels = count;
1129 
1130 	umem_free_aligned(labels, VDEV_LABELS * sizeof (*labels));
1131 	*config = expected_config;
1132 
1133 	return (0);
1134 #endif
1135 }
1136 
1137 /*
1138  * Sorted by full path and then vdev guid to allow for multiple entries with
1139  * the same full path name.  This is required because it's possible to
1140  * have multiple block devices with labels that refer to the same
1141  * ZPOOL_CONFIG_PATH yet have different vdev guids.  In this case both
1142  * entries need to be added to the cache.  Scenarios where this can occur
1143  * include overwritten pool labels, devices which are visible from multiple
1144  * hosts and multipath devices.
1145  */
1146 int
1147 slice_cache_compare(const void *arg1, const void *arg2)
1148 {
1149 	const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
1150 	const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
1151 	uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
1152 	uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
1153 	int rv;
1154 
1155 	rv = TREE_ISIGN(strcmp(nm1, nm2));
1156 	if (rv)
1157 		return (rv);
1158 
1159 	return (TREE_CMP(guid1, guid2));
1160 }
1161 
1162 static int
1163 label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
1164     uint64_t vdev_guid, char **path, char **devid)
1165 {
1166 	nvlist_t **child;
1167 	uint_t c, children;
1168 	uint64_t guid;
1169 	char *val;
1170 	int error;
1171 
1172 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1173 	    &child, &children) == 0) {
1174 		for (c = 0; c < children; c++) {
1175 			error  = label_paths_impl(hdl, child[c],
1176 			    pool_guid, vdev_guid, path, devid);
1177 			if (error)
1178 				return (error);
1179 		}
1180 		return (0);
1181 	}
1182 
1183 	if (nvroot == NULL)
1184 		return (0);
1185 
1186 	error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1187 	if ((error != 0) || (guid != vdev_guid))
1188 		return (0);
1189 
1190 	error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1191 	if (error == 0)
1192 		*path = val;
1193 
1194 	error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1195 	if (error == 0)
1196 		*devid = val;
1197 
1198 	return (0);
1199 }
1200 
1201 /*
1202  * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1203  * and store these strings as config_path and devid_path respectively.
1204  * The returned pointers are only valid as long as label remains valid.
1205  */
1206 int
1207 label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1208 {
1209 	nvlist_t *nvroot;
1210 	uint64_t pool_guid;
1211 	uint64_t vdev_guid;
1212 
1213 	*path = NULL;
1214 	*devid = NULL;
1215 
1216 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1217 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1218 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1219 		return (ENOENT);
1220 
1221 	return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1222 	    devid));
1223 }
1224 
1225 static void
1226 zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock,
1227     avl_tree_t *cache, const char *path, const char *name, int order)
1228 {
1229 	avl_index_t where;
1230 	rdsk_node_t *slice;
1231 
1232 	slice = zutil_alloc(hdl, sizeof (rdsk_node_t));
1233 	if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) {
1234 		free(slice);
1235 		return;
1236 	}
1237 	slice->rn_vdev_guid = 0;
1238 	slice->rn_lock = lock;
1239 	slice->rn_avl = cache;
1240 	slice->rn_hdl = hdl;
1241 	slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET;
1242 	slice->rn_labelpaths = B_FALSE;
1243 
1244 	pthread_mutex_lock(lock);
1245 	if (avl_find(cache, slice, &where)) {
1246 		free(slice->rn_name);
1247 		free(slice);
1248 	} else {
1249 		avl_insert(cache, slice, where);
1250 	}
1251 	pthread_mutex_unlock(lock);
1252 }
1253 
1254 static int
1255 zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock,
1256     avl_tree_t *cache, const char *dir, int order)
1257 {
1258 	int error;
1259 	char path[MAXPATHLEN];
1260 	struct dirent64 *dp;
1261 	DIR *dirp;
1262 
1263 	if (realpath(dir, path) == NULL) {
1264 		error = errno;
1265 		if (error == ENOENT)
1266 			return (0);
1267 
1268 		zutil_error_aux(hdl, "%s", strerror(error));
1269 		(void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN,
1270 		    "cannot resolve path '%s'"), dir);
1271 		return (error);
1272 	}
1273 
1274 	dirp = opendir(path);
1275 	if (dirp == NULL) {
1276 		error = errno;
1277 		zutil_error_aux(hdl, "%s", strerror(error));
1278 		(void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN,
1279 		    "cannot open '%s'"), path);
1280 		return (error);
1281 	}
1282 
1283 	while ((dp = readdir64(dirp)) != NULL) {
1284 		const char *name = dp->d_name;
1285 		if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
1286 			continue;
1287 
1288 		switch (dp->d_type) {
1289 		case DT_UNKNOWN:
1290 		case DT_BLK:
1291 		case DT_LNK:
1292 #ifdef __FreeBSD__
1293 		case DT_CHR:
1294 #endif
1295 		case DT_REG:
1296 			break;
1297 		default:
1298 			continue;
1299 		}
1300 
1301 		zpool_find_import_scan_add_slice(hdl, lock, cache, path, name,
1302 		    order);
1303 	}
1304 
1305 	(void) closedir(dirp);
1306 	return (0);
1307 }
1308 
1309 static int
1310 zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
1311     avl_tree_t *cache, const char *dir, int order)
1312 {
1313 	int error = 0;
1314 	char path[MAXPATHLEN];
1315 	char *d = NULL;
1316 	ssize_t dl;
1317 	const char *dpath, *name;
1318 
1319 	/*
1320 	 * Separate the directory and the basename.
1321 	 * We do this so that we can get the realpath of
1322 	 * the directory. We don't get the realpath on the
1323 	 * whole path because if it's a symlink, we want the
1324 	 * path of the symlink not where it points to.
1325 	 */
1326 	name = zfs_basename(dir);
1327 	if ((dl = zfs_dirnamelen(dir)) == -1)
1328 		dpath = ".";
1329 	else
1330 		dpath = d = zutil_strndup(hdl, dir, dl);
1331 
1332 	if (realpath(dpath, path) == NULL) {
1333 		error = errno;
1334 		if (error == ENOENT) {
1335 			error = 0;
1336 			goto out;
1337 		}
1338 
1339 		zutil_error_aux(hdl, "%s", strerror(error));
1340 		(void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN,
1341 		    "cannot resolve path '%s'"), dir);
1342 		goto out;
1343 	}
1344 
1345 	zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
1346 
1347 out:
1348 	free(d);
1349 	return (error);
1350 }
1351 
1352 /*
1353  * Scan a list of directories for zfs devices.
1354  */
1355 static int
1356 zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock,
1357     avl_tree_t **slice_cache, const char * const *dir, size_t dirs)
1358 {
1359 	avl_tree_t *cache;
1360 	rdsk_node_t *slice;
1361 	void *cookie;
1362 	int i, error;
1363 
1364 	*slice_cache = NULL;
1365 	cache = zutil_alloc(hdl, sizeof (avl_tree_t));
1366 	avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1367 	    offsetof(rdsk_node_t, rn_node));
1368 
1369 	for (i = 0; i < dirs; i++) {
1370 		struct stat sbuf;
1371 
1372 		if (stat(dir[i], &sbuf) != 0) {
1373 			error = errno;
1374 			if (error == ENOENT)
1375 				continue;
1376 
1377 			zutil_error_aux(hdl, "%s", strerror(error));
1378 			(void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(
1379 			    TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1380 			goto error;
1381 		}
1382 
1383 		/*
1384 		 * If dir[i] is a directory, we walk through it and add all
1385 		 * the entries to the cache. If it's not a directory, we just
1386 		 * add it to the cache.
1387 		 */
1388 		if (S_ISDIR(sbuf.st_mode)) {
1389 			if ((error = zpool_find_import_scan_dir(hdl, lock,
1390 			    cache, dir[i], i)) != 0)
1391 				goto error;
1392 		} else {
1393 			if ((error = zpool_find_import_scan_path(hdl, lock,
1394 			    cache, dir[i], i)) != 0)
1395 				goto error;
1396 		}
1397 	}
1398 
1399 	*slice_cache = cache;
1400 	return (0);
1401 
1402 error:
1403 	cookie = NULL;
1404 	while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1405 		free(slice->rn_name);
1406 		free(slice);
1407 	}
1408 	free(cache);
1409 
1410 	return (error);
1411 }
1412 
1413 /*
1414  * Given a list of directories to search, find all pools stored on disk.  This
1415  * includes partial pools which are not available to import.  If no args are
1416  * given (argc is 0), then the default directory (/dev/dsk) is searched.
1417  * poolname or guid (but not both) are provided by the caller when trying
1418  * to import a specific pool.
1419  */
1420 static nvlist_t *
1421 zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg,
1422     pthread_mutex_t *lock, avl_tree_t *cache)
1423 {
1424 	(void) lock;
1425 	nvlist_t *ret = NULL;
1426 	pool_list_t pools = { 0 };
1427 	pool_entry_t *pe, *penext;
1428 	vdev_entry_t *ve, *venext;
1429 	config_entry_t *ce, *cenext;
1430 	name_entry_t *ne, *nenext;
1431 	rdsk_node_t *slice;
1432 	void *cookie;
1433 	tpool_t *t;
1434 
1435 	verify(iarg->poolname == NULL || iarg->guid == 0);
1436 
1437 	/*
1438 	 * Create a thread pool to parallelize the process of reading and
1439 	 * validating labels, a large number of threads can be used due to
1440 	 * minimal contention.
1441 	 */
1442 	t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
1443 	for (slice = avl_first(cache); slice;
1444 	    (slice = avl_walk(cache, slice, AVL_AFTER)))
1445 		(void) tpool_dispatch(t, zpool_open_func, slice);
1446 
1447 	tpool_wait(t);
1448 	tpool_destroy(t);
1449 
1450 	/*
1451 	 * Process the cache, filtering out any entries which are not
1452 	 * for the specified pool then adding matching label configs.
1453 	 */
1454 	cookie = NULL;
1455 	while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1456 		if (slice->rn_config != NULL) {
1457 			nvlist_t *config = slice->rn_config;
1458 			boolean_t matched = B_TRUE;
1459 			boolean_t aux = B_FALSE;
1460 			int fd;
1461 
1462 			/*
1463 			 * Check if it's a spare or l2cache device. If it is,
1464 			 * we need to skip the name and guid check since they
1465 			 * don't exist on aux device label.
1466 			 */
1467 			if (iarg->poolname != NULL || iarg->guid != 0) {
1468 				uint64_t state;
1469 				aux = nvlist_lookup_uint64(config,
1470 				    ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1471 				    (state == POOL_STATE_SPARE ||
1472 				    state == POOL_STATE_L2CACHE);
1473 			}
1474 
1475 			if (iarg->poolname != NULL && !aux) {
1476 				char *pname;
1477 
1478 				matched = nvlist_lookup_string(config,
1479 				    ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1480 				    strcmp(iarg->poolname, pname) == 0;
1481 			} else if (iarg->guid != 0 && !aux) {
1482 				uint64_t this_guid;
1483 
1484 				matched = nvlist_lookup_uint64(config,
1485 				    ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1486 				    iarg->guid == this_guid;
1487 			}
1488 			if (matched) {
1489 				/*
1490 				 * Verify all remaining entries can be opened
1491 				 * exclusively. This will prune all underlying
1492 				 * multipath devices which otherwise could
1493 				 * result in the vdev appearing as UNAVAIL.
1494 				 *
1495 				 * Under zdb, this step isn't required and
1496 				 * would prevent a zdb -e of active pools with
1497 				 * no cachefile.
1498 				 */
1499 				fd = open(slice->rn_name,
1500 				    O_RDONLY | O_EXCL | O_CLOEXEC);
1501 				if (fd >= 0 || iarg->can_be_active) {
1502 					if (fd >= 0)
1503 						close(fd);
1504 					add_config(hdl, &pools,
1505 					    slice->rn_name, slice->rn_order,
1506 					    slice->rn_num_labels, config);
1507 				}
1508 			}
1509 			nvlist_free(config);
1510 		}
1511 		free(slice->rn_name);
1512 		free(slice);
1513 	}
1514 	avl_destroy(cache);
1515 	free(cache);
1516 
1517 	ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy);
1518 
1519 	for (pe = pools.pools; pe != NULL; pe = penext) {
1520 		penext = pe->pe_next;
1521 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1522 			venext = ve->ve_next;
1523 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1524 				cenext = ce->ce_next;
1525 				nvlist_free(ce->ce_config);
1526 				free(ce);
1527 			}
1528 			free(ve);
1529 		}
1530 		free(pe);
1531 	}
1532 
1533 	for (ne = pools.names; ne != NULL; ne = nenext) {
1534 		nenext = ne->ne_next;
1535 		free(ne->ne_name);
1536 		free(ne);
1537 	}
1538 
1539 	return (ret);
1540 }
1541 
1542 /*
1543  * Given a config, discover the paths for the devices which
1544  * exist in the config.
1545  */
1546 static int
1547 discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv,
1548     avl_tree_t *cache, pthread_mutex_t *lock)
1549 {
1550 	char *path = NULL;
1551 	ssize_t dl;
1552 	uint_t children;
1553 	nvlist_t **child;
1554 
1555 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1556 	    &child, &children) == 0) {
1557 		for (int c = 0; c < children; c++) {
1558 			discover_cached_paths(hdl, child[c], cache, lock);
1559 		}
1560 	}
1561 
1562 	/*
1563 	 * Once we have the path, we need to add the directory to
1564 	 * our directory cache.
1565 	 */
1566 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1567 		if ((dl = zfs_dirnamelen(path)) == -1)
1568 			path = (char *)".";
1569 		else
1570 			path[dl] = '\0';
1571 		return (zpool_find_import_scan_dir(hdl, lock, cache,
1572 		    path, 0));
1573 	}
1574 	return (0);
1575 }
1576 
1577 /*
1578  * Given a cache file, return the contents as a list of importable pools.
1579  * poolname or guid (but not both) are provided by the caller when trying
1580  * to import a specific pool.
1581  */
1582 static nvlist_t *
1583 zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg)
1584 {
1585 	char *buf;
1586 	int fd;
1587 	struct stat64 statbuf;
1588 	nvlist_t *raw, *src, *dst;
1589 	nvlist_t *pools;
1590 	nvpair_t *elem;
1591 	char *name;
1592 	uint64_t this_guid;
1593 	boolean_t active;
1594 
1595 	verify(iarg->poolname == NULL || iarg->guid == 0);
1596 
1597 	if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) {
1598 		zutil_error_aux(hdl, "%s", strerror(errno));
1599 		(void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1600 		    "failed to open cache file"));
1601 		return (NULL);
1602 	}
1603 
1604 	if (fstat64(fd, &statbuf) != 0) {
1605 		zutil_error_aux(hdl, "%s", strerror(errno));
1606 		(void) close(fd);
1607 		(void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1608 		    "failed to get size of cache file"));
1609 		return (NULL);
1610 	}
1611 
1612 	if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) {
1613 		(void) close(fd);
1614 		return (NULL);
1615 	}
1616 
1617 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1618 		(void) close(fd);
1619 		free(buf);
1620 		(void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1621 		    "failed to read cache file contents"));
1622 		return (NULL);
1623 	}
1624 
1625 	(void) close(fd);
1626 
1627 	if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1628 		free(buf);
1629 		(void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1630 		    "invalid or corrupt cache file contents"));
1631 		return (NULL);
1632 	}
1633 
1634 	free(buf);
1635 
1636 	/*
1637 	 * Go through and get the current state of the pools and refresh their
1638 	 * state.
1639 	 */
1640 	if (nvlist_alloc(&pools, 0, 0) != 0) {
1641 		(void) zutil_no_memory(hdl);
1642 		nvlist_free(raw);
1643 		return (NULL);
1644 	}
1645 
1646 	elem = NULL;
1647 	while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1648 		src = fnvpair_value_nvlist(elem);
1649 
1650 		name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
1651 		if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0)
1652 			continue;
1653 
1654 		this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1655 		if (iarg->guid != 0 && iarg->guid != this_guid)
1656 			continue;
1657 
1658 		if (zutil_pool_active(hdl, name, this_guid, &active) != 0) {
1659 			nvlist_free(raw);
1660 			nvlist_free(pools);
1661 			return (NULL);
1662 		}
1663 
1664 		if (active)
1665 			continue;
1666 
1667 		if (iarg->scan) {
1668 			uint64_t saved_guid = iarg->guid;
1669 			const char *saved_poolname = iarg->poolname;
1670 			pthread_mutex_t lock;
1671 
1672 			/*
1673 			 * Create the device cache that will hold the
1674 			 * devices we will scan based on the cachefile.
1675 			 * This will get destroyed and freed by
1676 			 * zpool_find_import_impl.
1677 			 */
1678 			avl_tree_t *cache = zutil_alloc(hdl,
1679 			    sizeof (avl_tree_t));
1680 			avl_create(cache, slice_cache_compare,
1681 			    sizeof (rdsk_node_t),
1682 			    offsetof(rdsk_node_t, rn_node));
1683 			nvlist_t *nvroot = fnvlist_lookup_nvlist(src,
1684 			    ZPOOL_CONFIG_VDEV_TREE);
1685 
1686 			/*
1687 			 * We only want to find the pool with this_guid.
1688 			 * We will reset these values back later.
1689 			 */
1690 			iarg->guid = this_guid;
1691 			iarg->poolname = NULL;
1692 
1693 			/*
1694 			 * We need to build up a cache of devices that exists
1695 			 * in the paths pointed to by the cachefile. This allows
1696 			 * us to preserve the device namespace that was
1697 			 * originally specified by the user but also lets us
1698 			 * scan devices in those directories in case they had
1699 			 * been renamed.
1700 			 */
1701 			pthread_mutex_init(&lock, NULL);
1702 			discover_cached_paths(hdl, nvroot, cache, &lock);
1703 			nvlist_t *nv = zpool_find_import_impl(hdl, iarg,
1704 			    &lock, cache);
1705 			pthread_mutex_destroy(&lock);
1706 
1707 			/*
1708 			 * zpool_find_import_impl will return back
1709 			 * a list of pools that it found based on the
1710 			 * device cache. There should only be one pool
1711 			 * since we're looking for a specific guid.
1712 			 * We will use that pool to build up the final
1713 			 * pool nvlist which is returned back to the
1714 			 * caller.
1715 			 */
1716 			nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
1717 			if (pair == NULL)
1718 				continue;
1719 			fnvlist_add_nvlist(pools, nvpair_name(pair),
1720 			    fnvpair_value_nvlist(pair));
1721 
1722 			VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL);
1723 
1724 			iarg->guid = saved_guid;
1725 			iarg->poolname = saved_poolname;
1726 			continue;
1727 		}
1728 
1729 		if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE,
1730 		    iarg->cachefile) != 0) {
1731 			(void) zutil_no_memory(hdl);
1732 			nvlist_free(raw);
1733 			nvlist_free(pools);
1734 			return (NULL);
1735 		}
1736 
1737 		update_vdevs_config_dev_sysfs_path(src);
1738 
1739 		if ((dst = zutil_refresh_config(hdl, src)) == NULL) {
1740 			nvlist_free(raw);
1741 			nvlist_free(pools);
1742 			return (NULL);
1743 		}
1744 
1745 		if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1746 			(void) zutil_no_memory(hdl);
1747 			nvlist_free(dst);
1748 			nvlist_free(raw);
1749 			nvlist_free(pools);
1750 			return (NULL);
1751 		}
1752 		nvlist_free(dst);
1753 	}
1754 	nvlist_free(raw);
1755 	return (pools);
1756 }
1757 
1758 static nvlist_t *
1759 zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg)
1760 {
1761 	pthread_mutex_t lock;
1762 	avl_tree_t *cache;
1763 	nvlist_t *pools = NULL;
1764 
1765 	verify(iarg->poolname == NULL || iarg->guid == 0);
1766 	pthread_mutex_init(&lock, NULL);
1767 
1768 	/*
1769 	 * Locate pool member vdevs by blkid or by directory scanning.
1770 	 * On success a newly allocated AVL tree which is populated with an
1771 	 * entry for each discovered vdev will be returned in the cache.
1772 	 * It's the caller's responsibility to consume and destroy this tree.
1773 	 */
1774 	if (iarg->scan || iarg->paths != 0) {
1775 		size_t dirs = iarg->paths;
1776 		const char * const *dir = (const char * const *)iarg->path;
1777 
1778 		if (dirs == 0)
1779 			dir = zpool_default_search_paths(&dirs);
1780 
1781 		if (zpool_find_import_scan(hdl, &lock, &cache,
1782 		    dir, dirs) != 0) {
1783 			pthread_mutex_destroy(&lock);
1784 			return (NULL);
1785 		}
1786 	} else {
1787 		if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) {
1788 			pthread_mutex_destroy(&lock);
1789 			return (NULL);
1790 		}
1791 	}
1792 
1793 	pools = zpool_find_import_impl(hdl, iarg, &lock, cache);
1794 	pthread_mutex_destroy(&lock);
1795 	return (pools);
1796 }
1797 
1798 
1799 nvlist_t *
1800 zpool_search_import(libpc_handle_t *hdl, importargs_t *import)
1801 {
1802 	nvlist_t *pools = NULL;
1803 
1804 	verify(import->poolname == NULL || import->guid == 0);
1805 
1806 	if (import->cachefile != NULL)
1807 		pools = zpool_find_import_cached(hdl, import);
1808 	else
1809 		pools = zpool_find_import(hdl, import);
1810 
1811 	if ((pools == NULL || nvlist_empty(pools)) &&
1812 	    hdl->lpc_open_access_error && geteuid() != 0) {
1813 		(void) zutil_error(hdl, LPC_EACCESS, dgettext(TEXT_DOMAIN,
1814 		    "no pools found"));
1815 	}
1816 
1817 	return (pools);
1818 }
1819 
1820 static boolean_t
1821 pool_match(nvlist_t *cfg, char *tgt)
1822 {
1823 	uint64_t v, guid = strtoull(tgt, NULL, 0);
1824 	char *s;
1825 
1826 	if (guid != 0) {
1827 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
1828 			return (v == guid);
1829 	} else {
1830 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
1831 			return (strcmp(s, tgt) == 0);
1832 	}
1833 	return (B_FALSE);
1834 }
1835 
1836 int
1837 zpool_find_config(libpc_handle_t *hdl, const char *target, nvlist_t **configp,
1838     importargs_t *args)
1839 {
1840 	nvlist_t *pools;
1841 	nvlist_t *match = NULL;
1842 	nvlist_t *config = NULL;
1843 	char *sepp = NULL;
1844 	int count = 0;
1845 	char *targetdup = strdup(target);
1846 
1847 	if (targetdup == NULL)
1848 		return (ENOMEM);
1849 
1850 	*configp = NULL;
1851 
1852 	if ((sepp = strpbrk(targetdup, "/@")) != NULL)
1853 		*sepp = '\0';
1854 
1855 	pools = zpool_search_import(hdl, args);
1856 
1857 	if (pools != NULL) {
1858 		nvpair_t *elem = NULL;
1859 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1860 			VERIFY0(nvpair_value_nvlist(elem, &config));
1861 			if (pool_match(config, targetdup)) {
1862 				count++;
1863 				if (match != NULL) {
1864 					/* multiple matches found */
1865 					continue;
1866 				} else {
1867 					match = fnvlist_dup(config);
1868 				}
1869 			}
1870 		}
1871 		fnvlist_free(pools);
1872 	}
1873 
1874 	if (count == 0) {
1875 		free(targetdup);
1876 		return (ENOENT);
1877 	}
1878 
1879 	if (count > 1) {
1880 		free(targetdup);
1881 		fnvlist_free(match);
1882 		return (EINVAL);
1883 	}
1884 
1885 	*configp = match;
1886 	free(targetdup);
1887 
1888 	return (0);
1889 }
1890 
1891 /*
1892  * Internal function for iterating over the vdevs.
1893  *
1894  * For each vdev, func() will be called and will be passed 'zhp' (which is
1895  * typically the zpool_handle_t cast as a void pointer), the vdev's nvlist, and
1896  * a user-defined data pointer).
1897  *
1898  * The return values from all the func() calls will be OR'd together and
1899  * returned.
1900  */
1901 int
1902 for_each_vdev_cb(void *zhp, nvlist_t *nv, pool_vdev_iter_f func,
1903     void *data)
1904 {
1905 	nvlist_t **child;
1906 	uint_t c, children;
1907 	int ret = 0;
1908 	int i;
1909 	char *type;
1910 
1911 	const char *list[] = {
1912 	    ZPOOL_CONFIG_SPARES,
1913 	    ZPOOL_CONFIG_L2CACHE,
1914 	    ZPOOL_CONFIG_CHILDREN
1915 	};
1916 
1917 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1918 		return (ret);
1919 
1920 	/* Don't run our function on root or indirect vdevs */
1921 	if ((strcmp(type, VDEV_TYPE_ROOT) != 0) &&
1922 	    (strcmp(type, VDEV_TYPE_INDIRECT) != 0)) {
1923 		ret |= func(zhp, nv, data);
1924 	}
1925 
1926 	for (i = 0; i < ARRAY_SIZE(list); i++) {
1927 		if (nvlist_lookup_nvlist_array(nv, list[i], &child,
1928 		    &children) == 0) {
1929 			for (c = 0; c < children; c++) {
1930 				uint64_t ishole = 0;
1931 
1932 				(void) nvlist_lookup_uint64(child[c],
1933 				    ZPOOL_CONFIG_IS_HOLE, &ishole);
1934 
1935 				if (ishole)
1936 					continue;
1937 
1938 				ret |= for_each_vdev_cb(zhp, child[c],
1939 				    func, data);
1940 			}
1941 		}
1942 	}
1943 
1944 	return (ret);
1945 }
1946 
1947 /*
1948  * Given an ZPOOL_CONFIG_VDEV_TREE nvpair, iterate over all the vdevs, calling
1949  * func() for each one.  func() is passed the vdev's nvlist and an optional
1950  * user-defined 'data' pointer.
1951  */
1952 int
1953 for_each_vdev_in_nvlist(nvlist_t *nvroot, pool_vdev_iter_f func, void *data)
1954 {
1955 	return (for_each_vdev_cb(NULL, nvroot, func, data));
1956 }
1957