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