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