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