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