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 		    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 				case EOPNOTSUPP:
1036 				case ENOSYS:
1037 					do_slow = B_TRUE;
1038 					/* FALLTHROUGH */
1039 				case 0:
1040 				default:
1041 					(void) aio_return(&aiocbs[l]);
1042 				}
1043 			}
1044 		}
1045 		if (do_slow) {
1046 			/*
1047 			 * At least some IO involved access unsafe-for-AIO
1048 			 * files. Let's try again, without AIO this time.
1049 			 */
1050 			error = zpool_read_label_slow(fd, config, num_labels);
1051 			saved_errno = errno;
1052 		}
1053 		free(labels);
1054 		errno = saved_errno;
1055 		return (error);
1056 	}
1057 
1058 	for (l = 0; l < VDEV_LABELS; l++) {
1059 		uint64_t state, guid, txg;
1060 
1061 		if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t))
1062 			continue;
1063 
1064 		if (nvlist_unpack(labels[l].vp_nvlist,
1065 		    sizeof (labels[l].vp_nvlist), config, 0) != 0)
1066 			continue;
1067 
1068 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
1069 		    &guid) != 0 || guid == 0) {
1070 			nvlist_free(*config);
1071 			continue;
1072 		}
1073 
1074 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
1075 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
1076 			nvlist_free(*config);
1077 			continue;
1078 		}
1079 
1080 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1081 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
1082 		    &txg) != 0 || txg == 0)) {
1083 			nvlist_free(*config);
1084 			continue;
1085 		}
1086 
1087 		if (expected_guid) {
1088 			if (expected_guid == guid)
1089 				count++;
1090 
1091 			nvlist_free(*config);
1092 		} else {
1093 			expected_config = *config;
1094 			expected_guid = guid;
1095 			count++;
1096 		}
1097 	}
1098 
1099 	if (num_labels != NULL)
1100 		*num_labels = count;
1101 
1102 	free(labels);
1103 	*config = expected_config;
1104 
1105 	return (0);
1106 }
1107 
1108 /*
1109  * Sorted by full path and then vdev guid to allow for multiple entries with
1110  * the same full path name.  This is required because it's possible to
1111  * have multiple block devices with labels that refer to the same
1112  * ZPOOL_CONFIG_PATH yet have different vdev guids.  In this case both
1113  * entries need to be added to the cache.  Scenarios where this can occur
1114  * include overwritten pool labels, devices which are visible from multiple
1115  * hosts and multipath devices.
1116  */
1117 int
1118 slice_cache_compare(const void *arg1, const void *arg2)
1119 {
1120 	const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
1121 	const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
1122 	uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
1123 	uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
1124 	int rv;
1125 
1126 	rv = TREE_ISIGN(strcmp(nm1, nm2));
1127 	if (rv)
1128 		return (rv);
1129 
1130 	return (TREE_CMP(guid1, guid2));
1131 }
1132 
1133 static int
1134 label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
1135     uint64_t vdev_guid, char **path, char **devid)
1136 {
1137 	nvlist_t **child;
1138 	uint_t c, children;
1139 	uint64_t guid;
1140 	char *val;
1141 	int error;
1142 
1143 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1144 	    &child, &children) == 0) {
1145 		for (c = 0; c < children; c++) {
1146 			error  = label_paths_impl(hdl, child[c],
1147 			    pool_guid, vdev_guid, path, devid);
1148 			if (error)
1149 				return (error);
1150 		}
1151 		return (0);
1152 	}
1153 
1154 	if (nvroot == NULL)
1155 		return (0);
1156 
1157 	error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1158 	if ((error != 0) || (guid != vdev_guid))
1159 		return (0);
1160 
1161 	error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1162 	if (error == 0)
1163 		*path = val;
1164 
1165 	error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1166 	if (error == 0)
1167 		*devid = val;
1168 
1169 	return (0);
1170 }
1171 
1172 /*
1173  * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1174  * and store these strings as config_path and devid_path respectively.
1175  * The returned pointers are only valid as long as label remains valid.
1176  */
1177 int
1178 label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1179 {
1180 	nvlist_t *nvroot;
1181 	uint64_t pool_guid;
1182 	uint64_t vdev_guid;
1183 
1184 	*path = NULL;
1185 	*devid = NULL;
1186 
1187 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1188 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1189 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1190 		return (ENOENT);
1191 
1192 	return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1193 	    devid));
1194 }
1195 
1196 static void
1197 zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock,
1198     avl_tree_t *cache, const char *path, const char *name, int order)
1199 {
1200 	avl_index_t where;
1201 	rdsk_node_t *slice;
1202 
1203 	slice = zutil_alloc(hdl, sizeof (rdsk_node_t));
1204 	if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) {
1205 		free(slice);
1206 		return;
1207 	}
1208 	slice->rn_vdev_guid = 0;
1209 	slice->rn_lock = lock;
1210 	slice->rn_avl = cache;
1211 	slice->rn_hdl = hdl;
1212 	slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET;
1213 	slice->rn_labelpaths = B_FALSE;
1214 
1215 	pthread_mutex_lock(lock);
1216 	if (avl_find(cache, slice, &where)) {
1217 		free(slice->rn_name);
1218 		free(slice);
1219 	} else {
1220 		avl_insert(cache, slice, where);
1221 	}
1222 	pthread_mutex_unlock(lock);
1223 }
1224 
1225 static int
1226 zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock,
1227     avl_tree_t *cache, const char *dir, int order)
1228 {
1229 	int error;
1230 	char path[MAXPATHLEN];
1231 	struct dirent64 *dp;
1232 	DIR *dirp;
1233 
1234 	if (realpath(dir, path) == NULL) {
1235 		error = errno;
1236 		if (error == ENOENT)
1237 			return (0);
1238 
1239 		zutil_error_aux(hdl, "%s", strerror(error));
1240 		(void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1241 		    TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1242 		return (error);
1243 	}
1244 
1245 	dirp = opendir(path);
1246 	if (dirp == NULL) {
1247 		error = errno;
1248 		zutil_error_aux(hdl, "%s", strerror(error));
1249 		(void) zutil_error_fmt(hdl, EZFS_BADPATH,
1250 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
1251 		return (error);
1252 	}
1253 
1254 	while ((dp = readdir64(dirp)) != NULL) {
1255 		const char *name = dp->d_name;
1256 		if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
1257 			continue;
1258 
1259 		switch (dp->d_type) {
1260 		case DT_UNKNOWN:
1261 		case DT_BLK:
1262 		case DT_LNK:
1263 #ifdef __FreeBSD__
1264 		case DT_CHR:
1265 #endif
1266 		case DT_REG:
1267 			break;
1268 		default:
1269 			continue;
1270 		}
1271 
1272 		zpool_find_import_scan_add_slice(hdl, lock, cache, path, name,
1273 		    order);
1274 	}
1275 
1276 	(void) closedir(dirp);
1277 	return (0);
1278 }
1279 
1280 static int
1281 zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
1282     avl_tree_t *cache, const char *dir, int order)
1283 {
1284 	int error = 0;
1285 	char path[MAXPATHLEN];
1286 	char *d = NULL;
1287 	ssize_t dl;
1288 	const char *dpath, *name;
1289 
1290 	/*
1291 	 * Separate the directory and the basename.
1292 	 * We do this so that we can get the realpath of
1293 	 * the directory. We don't get the realpath on the
1294 	 * whole path because if it's a symlink, we want the
1295 	 * path of the symlink not where it points to.
1296 	 */
1297 	name = zfs_basename(dir);
1298 	if ((dl = zfs_dirnamelen(dir)) == -1)
1299 		dpath = ".";
1300 	else
1301 		dpath = d = zutil_strndup(hdl, dir, dl);
1302 
1303 	if (realpath(dpath, path) == NULL) {
1304 		error = errno;
1305 		if (error == ENOENT) {
1306 			error = 0;
1307 			goto out;
1308 		}
1309 
1310 		zutil_error_aux(hdl, "%s", strerror(error));
1311 		(void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1312 		    TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1313 		goto out;
1314 	}
1315 
1316 	zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
1317 
1318 out:
1319 	free(d);
1320 	return (error);
1321 }
1322 
1323 /*
1324  * Scan a list of directories for zfs devices.
1325  */
1326 static int
1327 zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock,
1328     avl_tree_t **slice_cache, const char * const *dir, size_t dirs)
1329 {
1330 	avl_tree_t *cache;
1331 	rdsk_node_t *slice;
1332 	void *cookie;
1333 	int i, error;
1334 
1335 	*slice_cache = NULL;
1336 	cache = zutil_alloc(hdl, sizeof (avl_tree_t));
1337 	avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1338 	    offsetof(rdsk_node_t, rn_node));
1339 
1340 	for (i = 0; i < dirs; i++) {
1341 		struct stat sbuf;
1342 
1343 		if (stat(dir[i], &sbuf) != 0) {
1344 			error = errno;
1345 			if (error == ENOENT)
1346 				continue;
1347 
1348 			zutil_error_aux(hdl, "%s", strerror(error));
1349 			(void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1350 			    TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1351 			goto error;
1352 		}
1353 
1354 		/*
1355 		 * If dir[i] is a directory, we walk through it and add all
1356 		 * the entries to the cache. If it's not a directory, we just
1357 		 * add it to the cache.
1358 		 */
1359 		if (S_ISDIR(sbuf.st_mode)) {
1360 			if ((error = zpool_find_import_scan_dir(hdl, lock,
1361 			    cache, dir[i], i)) != 0)
1362 				goto error;
1363 		} else {
1364 			if ((error = zpool_find_import_scan_path(hdl, lock,
1365 			    cache, dir[i], i)) != 0)
1366 				goto error;
1367 		}
1368 	}
1369 
1370 	*slice_cache = cache;
1371 	return (0);
1372 
1373 error:
1374 	cookie = NULL;
1375 	while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1376 		free(slice->rn_name);
1377 		free(slice);
1378 	}
1379 	free(cache);
1380 
1381 	return (error);
1382 }
1383 
1384 /*
1385  * Given a list of directories to search, find all pools stored on disk.  This
1386  * includes partial pools which are not available to import.  If no args are
1387  * given (argc is 0), then the default directory (/dev/dsk) is searched.
1388  * poolname or guid (but not both) are provided by the caller when trying
1389  * to import a specific pool.
1390  */
1391 static nvlist_t *
1392 zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg,
1393     pthread_mutex_t *lock, avl_tree_t *cache)
1394 {
1395 	nvlist_t *ret = NULL;
1396 	pool_list_t pools = { 0 };
1397 	pool_entry_t *pe, *penext;
1398 	vdev_entry_t *ve, *venext;
1399 	config_entry_t *ce, *cenext;
1400 	name_entry_t *ne, *nenext;
1401 	rdsk_node_t *slice;
1402 	void *cookie;
1403 	tpool_t *t;
1404 
1405 	verify(iarg->poolname == NULL || iarg->guid == 0);
1406 
1407 	/*
1408 	 * Create a thread pool to parallelize the process of reading and
1409 	 * validating labels, a large number of threads can be used due to
1410 	 * minimal contention.
1411 	 */
1412 	t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
1413 	for (slice = avl_first(cache); slice;
1414 	    (slice = avl_walk(cache, slice, AVL_AFTER)))
1415 		(void) tpool_dispatch(t, zpool_open_func, slice);
1416 
1417 	tpool_wait(t);
1418 	tpool_destroy(t);
1419 
1420 	/*
1421 	 * Process the cache, filtering out any entries which are not
1422 	 * for the specified pool then adding matching label configs.
1423 	 */
1424 	cookie = NULL;
1425 	while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1426 		if (slice->rn_config != NULL) {
1427 			nvlist_t *config = slice->rn_config;
1428 			boolean_t matched = B_TRUE;
1429 			boolean_t aux = B_FALSE;
1430 			int fd;
1431 
1432 			/*
1433 			 * Check if it's a spare or l2cache device. If it is,
1434 			 * we need to skip the name and guid check since they
1435 			 * don't exist on aux device label.
1436 			 */
1437 			if (iarg->poolname != NULL || iarg->guid != 0) {
1438 				uint64_t state;
1439 				aux = nvlist_lookup_uint64(config,
1440 				    ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1441 				    (state == POOL_STATE_SPARE ||
1442 				    state == POOL_STATE_L2CACHE);
1443 			}
1444 
1445 			if (iarg->poolname != NULL && !aux) {
1446 				char *pname;
1447 
1448 				matched = nvlist_lookup_string(config,
1449 				    ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1450 				    strcmp(iarg->poolname, pname) == 0;
1451 			} else if (iarg->guid != 0 && !aux) {
1452 				uint64_t this_guid;
1453 
1454 				matched = nvlist_lookup_uint64(config,
1455 				    ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1456 				    iarg->guid == this_guid;
1457 			}
1458 			if (matched) {
1459 				/*
1460 				 * Verify all remaining entries can be opened
1461 				 * exclusively. This will prune all underlying
1462 				 * multipath devices which otherwise could
1463 				 * result in the vdev appearing as UNAVAIL.
1464 				 *
1465 				 * Under zdb, this step isn't required and
1466 				 * would prevent a zdb -e of active pools with
1467 				 * no cachefile.
1468 				 */
1469 				fd = open(slice->rn_name,
1470 				    O_RDONLY | O_EXCL | O_CLOEXEC);
1471 				if (fd >= 0 || iarg->can_be_active) {
1472 					if (fd >= 0)
1473 						close(fd);
1474 					add_config(hdl, &pools,
1475 					    slice->rn_name, slice->rn_order,
1476 					    slice->rn_num_labels, config);
1477 				}
1478 			}
1479 			nvlist_free(config);
1480 		}
1481 		free(slice->rn_name);
1482 		free(slice);
1483 	}
1484 	avl_destroy(cache);
1485 	free(cache);
1486 
1487 	ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy);
1488 
1489 	for (pe = pools.pools; pe != NULL; pe = penext) {
1490 		penext = pe->pe_next;
1491 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1492 			venext = ve->ve_next;
1493 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1494 				cenext = ce->ce_next;
1495 				nvlist_free(ce->ce_config);
1496 				free(ce);
1497 			}
1498 			free(ve);
1499 		}
1500 		free(pe);
1501 	}
1502 
1503 	for (ne = pools.names; ne != NULL; ne = nenext) {
1504 		nenext = ne->ne_next;
1505 		free(ne->ne_name);
1506 		free(ne);
1507 	}
1508 
1509 	return (ret);
1510 }
1511 
1512 /*
1513  * Given a config, discover the paths for the devices which
1514  * exist in the config.
1515  */
1516 static int
1517 discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv,
1518     avl_tree_t *cache, pthread_mutex_t *lock)
1519 {
1520 	char *path = NULL;
1521 	ssize_t dl;
1522 	uint_t children;
1523 	nvlist_t **child;
1524 
1525 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1526 	    &child, &children) == 0) {
1527 		for (int c = 0; c < children; c++) {
1528 			discover_cached_paths(hdl, child[c], cache, lock);
1529 		}
1530 	}
1531 
1532 	/*
1533 	 * Once we have the path, we need to add the directory to
1534 	 * our directory cache.
1535 	 */
1536 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1537 		if ((dl = zfs_dirnamelen(path)) == -1)
1538 			path = ".";
1539 		else
1540 			path[dl] = '\0';
1541 		return (zpool_find_import_scan_dir(hdl, lock, cache,
1542 		    path, 0));
1543 	}
1544 	return (0);
1545 }
1546 
1547 /*
1548  * Given a cache file, return the contents as a list of importable pools.
1549  * poolname or guid (but not both) are provided by the caller when trying
1550  * to import a specific pool.
1551  */
1552 static nvlist_t *
1553 zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg)
1554 {
1555 	char *buf;
1556 	int fd;
1557 	struct stat64 statbuf;
1558 	nvlist_t *raw, *src, *dst;
1559 	nvlist_t *pools;
1560 	nvpair_t *elem;
1561 	char *name;
1562 	uint64_t this_guid;
1563 	boolean_t active;
1564 
1565 	verify(iarg->poolname == NULL || iarg->guid == 0);
1566 
1567 	if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) {
1568 		zutil_error_aux(hdl, "%s", strerror(errno));
1569 		(void) zutil_error(hdl, EZFS_BADCACHE,
1570 		    dgettext(TEXT_DOMAIN, "failed to open cache file"));
1571 		return (NULL);
1572 	}
1573 
1574 	if (fstat64(fd, &statbuf) != 0) {
1575 		zutil_error_aux(hdl, "%s", strerror(errno));
1576 		(void) close(fd);
1577 		(void) zutil_error(hdl, EZFS_BADCACHE,
1578 		    dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
1579 		return (NULL);
1580 	}
1581 
1582 	if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) {
1583 		(void) close(fd);
1584 		return (NULL);
1585 	}
1586 
1587 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1588 		(void) close(fd);
1589 		free(buf);
1590 		(void) zutil_error(hdl, EZFS_BADCACHE,
1591 		    dgettext(TEXT_DOMAIN,
1592 		    "failed to read cache file contents"));
1593 		return (NULL);
1594 	}
1595 
1596 	(void) close(fd);
1597 
1598 	if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1599 		free(buf);
1600 		(void) zutil_error(hdl, EZFS_BADCACHE,
1601 		    dgettext(TEXT_DOMAIN,
1602 		    "invalid or corrupt cache file contents"));
1603 		return (NULL);
1604 	}
1605 
1606 	free(buf);
1607 
1608 	/*
1609 	 * Go through and get the current state of the pools and refresh their
1610 	 * state.
1611 	 */
1612 	if (nvlist_alloc(&pools, 0, 0) != 0) {
1613 		(void) zutil_no_memory(hdl);
1614 		nvlist_free(raw);
1615 		return (NULL);
1616 	}
1617 
1618 	elem = NULL;
1619 	while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1620 		src = fnvpair_value_nvlist(elem);
1621 
1622 		name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
1623 		if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0)
1624 			continue;
1625 
1626 		this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1627 		if (iarg->guid != 0 && iarg->guid != this_guid)
1628 			continue;
1629 
1630 		if (zutil_pool_active(hdl, name, this_guid, &active) != 0) {
1631 			nvlist_free(raw);
1632 			nvlist_free(pools);
1633 			return (NULL);
1634 		}
1635 
1636 		if (active)
1637 			continue;
1638 
1639 		if (iarg->scan) {
1640 			uint64_t saved_guid = iarg->guid;
1641 			const char *saved_poolname = iarg->poolname;
1642 			pthread_mutex_t lock;
1643 
1644 			/*
1645 			 * Create the device cache that will hold the
1646 			 * devices we will scan based on the cachefile.
1647 			 * This will get destroyed and freed by
1648 			 * zpool_find_import_impl.
1649 			 */
1650 			avl_tree_t *cache = zutil_alloc(hdl,
1651 			    sizeof (avl_tree_t));
1652 			avl_create(cache, slice_cache_compare,
1653 			    sizeof (rdsk_node_t),
1654 			    offsetof(rdsk_node_t, rn_node));
1655 			nvlist_t *nvroot = fnvlist_lookup_nvlist(src,
1656 			    ZPOOL_CONFIG_VDEV_TREE);
1657 
1658 			/*
1659 			 * We only want to find the pool with this_guid.
1660 			 * We will reset these values back later.
1661 			 */
1662 			iarg->guid = this_guid;
1663 			iarg->poolname = NULL;
1664 
1665 			/*
1666 			 * We need to build up a cache of devices that exists
1667 			 * in the paths pointed to by the cachefile. This allows
1668 			 * us to preserve the device namespace that was
1669 			 * originally specified by the user but also lets us
1670 			 * scan devices in those directories in case they had
1671 			 * been renamed.
1672 			 */
1673 			pthread_mutex_init(&lock, NULL);
1674 			discover_cached_paths(hdl, nvroot, cache, &lock);
1675 			nvlist_t *nv = zpool_find_import_impl(hdl, iarg,
1676 			    &lock, cache);
1677 			pthread_mutex_destroy(&lock);
1678 
1679 			/*
1680 			 * zpool_find_import_impl will return back
1681 			 * a list of pools that it found based on the
1682 			 * device cache. There should only be one pool
1683 			 * since we're looking for a specific guid.
1684 			 * We will use that pool to build up the final
1685 			 * pool nvlist which is returned back to the
1686 			 * caller.
1687 			 */
1688 			nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
1689 			fnvlist_add_nvlist(pools, nvpair_name(pair),
1690 			    fnvpair_value_nvlist(pair));
1691 
1692 			VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL);
1693 
1694 			iarg->guid = saved_guid;
1695 			iarg->poolname = saved_poolname;
1696 			continue;
1697 		}
1698 
1699 		if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE,
1700 		    iarg->cachefile) != 0) {
1701 			(void) zutil_no_memory(hdl);
1702 			nvlist_free(raw);
1703 			nvlist_free(pools);
1704 			return (NULL);
1705 		}
1706 
1707 		if ((dst = zutil_refresh_config(hdl, src)) == NULL) {
1708 			nvlist_free(raw);
1709 			nvlist_free(pools);
1710 			return (NULL);
1711 		}
1712 
1713 		if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1714 			(void) zutil_no_memory(hdl);
1715 			nvlist_free(dst);
1716 			nvlist_free(raw);
1717 			nvlist_free(pools);
1718 			return (NULL);
1719 		}
1720 		nvlist_free(dst);
1721 	}
1722 	nvlist_free(raw);
1723 	return (pools);
1724 }
1725 
1726 static nvlist_t *
1727 zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg)
1728 {
1729 	pthread_mutex_t lock;
1730 	avl_tree_t *cache;
1731 	nvlist_t *pools = NULL;
1732 
1733 	verify(iarg->poolname == NULL || iarg->guid == 0);
1734 	pthread_mutex_init(&lock, NULL);
1735 
1736 	/*
1737 	 * Locate pool member vdevs by blkid or by directory scanning.
1738 	 * On success a newly allocated AVL tree which is populated with an
1739 	 * entry for each discovered vdev will be returned in the cache.
1740 	 * It's the caller's responsibility to consume and destroy this tree.
1741 	 */
1742 	if (iarg->scan || iarg->paths != 0) {
1743 		size_t dirs = iarg->paths;
1744 		const char * const *dir = (const char * const *)iarg->path;
1745 
1746 		if (dirs == 0)
1747 			dir = zpool_default_search_paths(&dirs);
1748 
1749 		if (zpool_find_import_scan(hdl, &lock, &cache,
1750 		    dir, dirs) != 0) {
1751 			pthread_mutex_destroy(&lock);
1752 			return (NULL);
1753 		}
1754 	} else {
1755 		if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) {
1756 			pthread_mutex_destroy(&lock);
1757 			return (NULL);
1758 		}
1759 	}
1760 
1761 	pools = zpool_find_import_impl(hdl, iarg, &lock, cache);
1762 	pthread_mutex_destroy(&lock);
1763 	return (pools);
1764 }
1765 
1766 
1767 nvlist_t *
1768 zpool_search_import(void *hdl, importargs_t *import,
1769     const pool_config_ops_t *pco)
1770 {
1771 	libpc_handle_t handle = { 0 };
1772 	nvlist_t *pools = NULL;
1773 
1774 	handle.lpc_lib_handle = hdl;
1775 	handle.lpc_ops = pco;
1776 	handle.lpc_printerr = B_TRUE;
1777 
1778 	verify(import->poolname == NULL || import->guid == 0);
1779 
1780 	if (import->cachefile != NULL)
1781 		pools = zpool_find_import_cached(&handle, import);
1782 	else
1783 		pools = zpool_find_import(&handle, import);
1784 
1785 	if ((pools == NULL || nvlist_empty(pools)) &&
1786 	    handle.lpc_open_access_error && geteuid() != 0) {
1787 		(void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN,
1788 		    "no pools found"));
1789 	}
1790 
1791 	return (pools);
1792 }
1793 
1794 static boolean_t
1795 pool_match(nvlist_t *cfg, char *tgt)
1796 {
1797 	uint64_t v, guid = strtoull(tgt, NULL, 0);
1798 	char *s;
1799 
1800 	if (guid != 0) {
1801 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
1802 			return (v == guid);
1803 	} else {
1804 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
1805 			return (strcmp(s, tgt) == 0);
1806 	}
1807 	return (B_FALSE);
1808 }
1809 
1810 int
1811 zpool_find_config(void *hdl, const char *target, nvlist_t **configp,
1812     importargs_t *args, const pool_config_ops_t *pco)
1813 {
1814 	nvlist_t *pools;
1815 	nvlist_t *match = NULL;
1816 	nvlist_t *config = NULL;
1817 	char *sepp = NULL;
1818 	int count = 0;
1819 	char *targetdup = strdup(target);
1820 
1821 	*configp = NULL;
1822 
1823 	if ((sepp = strpbrk(targetdup, "/@")) != NULL)
1824 		*sepp = '\0';
1825 
1826 	pools = zpool_search_import(hdl, args, pco);
1827 
1828 	if (pools != NULL) {
1829 		nvpair_t *elem = NULL;
1830 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1831 			VERIFY0(nvpair_value_nvlist(elem, &config));
1832 			if (pool_match(config, targetdup)) {
1833 				count++;
1834 				if (match != NULL) {
1835 					/* multiple matches found */
1836 					continue;
1837 				} else {
1838 					match = fnvlist_dup(config);
1839 				}
1840 			}
1841 		}
1842 		fnvlist_free(pools);
1843 	}
1844 
1845 	if (count == 0) {
1846 		free(targetdup);
1847 		return (ENOENT);
1848 	}
1849 
1850 	if (count > 1) {
1851 		free(targetdup);
1852 		fnvlist_free(match);
1853 		return (EINVAL);
1854 	}
1855 
1856 	*configp = match;
1857 	free(targetdup);
1858 
1859 	return (0);
1860 }
1861