xref: /freebsd-src/sys/contrib/openzfs/lib/libzutil/zutil_import.c (revision c7046f76c2c027b00c0e6ba57cfd28f1a78f5e23)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright 2015 RackTop Systems.
26eda14cbcSMatt Macy  * Copyright (c) 2016, Intel Corporation.
27ee36e25aSMartin Matuska  * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy /*
31eda14cbcSMatt Macy  * Pool import support functions.
32eda14cbcSMatt Macy  *
33eda14cbcSMatt Macy  * Used by zpool, ztest, zdb, and zhack to locate importable configs. Since
34eda14cbcSMatt Macy  * these commands are expected to run in the global zone, we can assume
35eda14cbcSMatt Macy  * that the devices are all readable when called.
36eda14cbcSMatt Macy  *
37eda14cbcSMatt Macy  * To import a pool, we rely on reading the configuration information from the
38eda14cbcSMatt Macy  * ZFS label of each device.  If we successfully read the label, then we
39eda14cbcSMatt Macy  * organize the configuration information in the following hierarchy:
40eda14cbcSMatt Macy  *
41eda14cbcSMatt Macy  *	pool guid -> toplevel vdev guid -> label txg
42eda14cbcSMatt Macy  *
43eda14cbcSMatt Macy  * Duplicate entries matching this same tuple will be discarded.  Once we have
44eda14cbcSMatt Macy  * examined every device, we pick the best label txg config for each toplevel
45eda14cbcSMatt Macy  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
46eda14cbcSMatt Macy  * update any paths that have changed.  Finally, we attempt to import the pool
47eda14cbcSMatt Macy  * using our derived config, and record the results.
48eda14cbcSMatt Macy  */
49eda14cbcSMatt Macy 
50da5137abSMartin Matuska #ifdef HAVE_AIO_H
51184c1b94SMartin Matuska #include <aio.h>
52da5137abSMartin Matuska #endif
53eda14cbcSMatt Macy #include <ctype.h>
54eda14cbcSMatt Macy #include <dirent.h>
55eda14cbcSMatt Macy #include <errno.h>
56eda14cbcSMatt Macy #include <libintl.h>
57eda14cbcSMatt Macy #include <libgen.h>
58eda14cbcSMatt Macy #include <stddef.h>
59eda14cbcSMatt Macy #include <stdlib.h>
60eda14cbcSMatt Macy #include <string.h>
61eda14cbcSMatt Macy #include <sys/stat.h>
62eda14cbcSMatt Macy #include <unistd.h>
63eda14cbcSMatt Macy #include <fcntl.h>
64eda14cbcSMatt Macy #include <sys/dktp/fdisk.h>
65eda14cbcSMatt Macy #include <sys/vdev_impl.h>
66eda14cbcSMatt Macy #include <sys/fs/zfs.h>
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy #include <thread_pool.h>
69eda14cbcSMatt Macy #include <libzutil.h>
70eda14cbcSMatt Macy #include <libnvpair.h>
71eda14cbcSMatt Macy 
72eda14cbcSMatt Macy #include "zutil_import.h"
73eda14cbcSMatt Macy 
741f88aa09SMartin Matuska static __attribute__((format(printf, 2, 3))) void
75eda14cbcSMatt Macy zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...)
76eda14cbcSMatt Macy {
77eda14cbcSMatt Macy 	va_list ap;
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy 	va_start(ap, fmt);
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 	(void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap);
82eda14cbcSMatt Macy 	hdl->lpc_desc_active = B_TRUE;
83eda14cbcSMatt Macy 
84eda14cbcSMatt Macy 	va_end(ap);
85eda14cbcSMatt Macy }
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy static void
88eda14cbcSMatt Macy zutil_verror(libpc_handle_t *hdl, const char *error, const char *fmt,
89eda14cbcSMatt Macy     va_list ap)
90eda14cbcSMatt Macy {
91eda14cbcSMatt Macy 	char action[1024];
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy 	(void) vsnprintf(action, sizeof (action), fmt, ap);
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy 	if (hdl->lpc_desc_active)
96eda14cbcSMatt Macy 		hdl->lpc_desc_active = B_FALSE;
97eda14cbcSMatt Macy 	else
98eda14cbcSMatt Macy 		hdl->lpc_desc[0] = '\0';
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy 	if (hdl->lpc_printerr) {
101eda14cbcSMatt Macy 		if (hdl->lpc_desc[0] != '\0')
102eda14cbcSMatt Macy 			error = hdl->lpc_desc;
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 		(void) fprintf(stderr, "%s: %s\n", action, error);
105eda14cbcSMatt Macy 	}
106eda14cbcSMatt Macy }
107eda14cbcSMatt Macy 
1081f88aa09SMartin Matuska static __attribute__((format(printf, 3, 4))) int
109eda14cbcSMatt Macy zutil_error_fmt(libpc_handle_t *hdl, const char *error, const char *fmt, ...)
110eda14cbcSMatt Macy {
111eda14cbcSMatt Macy 	va_list ap;
112eda14cbcSMatt Macy 
113eda14cbcSMatt Macy 	va_start(ap, fmt);
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 	zutil_verror(hdl, error, fmt, ap);
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy 	va_end(ap);
118eda14cbcSMatt Macy 
119eda14cbcSMatt Macy 	return (-1);
120eda14cbcSMatt Macy }
121eda14cbcSMatt Macy 
122eda14cbcSMatt Macy static int
123eda14cbcSMatt Macy zutil_error(libpc_handle_t *hdl, const char *error, const char *msg)
124eda14cbcSMatt Macy {
125eda14cbcSMatt Macy 	return (zutil_error_fmt(hdl, error, "%s", msg));
126eda14cbcSMatt Macy }
127eda14cbcSMatt Macy 
128eda14cbcSMatt Macy static int
129eda14cbcSMatt Macy zutil_no_memory(libpc_handle_t *hdl)
130eda14cbcSMatt Macy {
131eda14cbcSMatt Macy 	zutil_error(hdl, EZFS_NOMEM, "internal error");
132eda14cbcSMatt Macy 	exit(1);
133eda14cbcSMatt Macy }
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy void *
136eda14cbcSMatt Macy zutil_alloc(libpc_handle_t *hdl, size_t size)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy 	void *data;
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 	if ((data = calloc(1, size)) == NULL)
141eda14cbcSMatt Macy 		(void) zutil_no_memory(hdl);
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy 	return (data);
144eda14cbcSMatt Macy }
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy char *
147eda14cbcSMatt Macy zutil_strdup(libpc_handle_t *hdl, const char *str)
148eda14cbcSMatt Macy {
149eda14cbcSMatt Macy 	char *ret;
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy 	if ((ret = strdup(str)) == NULL)
152eda14cbcSMatt Macy 		(void) zutil_no_memory(hdl);
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy 	return (ret);
155eda14cbcSMatt Macy }
156eda14cbcSMatt Macy 
1573ff01b23SMartin Matuska static char *
1583ff01b23SMartin Matuska zutil_strndup(libpc_handle_t *hdl, const char *str, size_t n)
1593ff01b23SMartin Matuska {
1603ff01b23SMartin Matuska 	char *ret;
1613ff01b23SMartin Matuska 
1623ff01b23SMartin Matuska 	if ((ret = strndup(str, n)) == NULL)
1633ff01b23SMartin Matuska 		(void) zutil_no_memory(hdl);
1643ff01b23SMartin Matuska 
1653ff01b23SMartin Matuska 	return (ret);
1663ff01b23SMartin Matuska }
1673ff01b23SMartin Matuska 
168eda14cbcSMatt Macy /*
169eda14cbcSMatt Macy  * Intermediate structures used to gather configuration information.
170eda14cbcSMatt Macy  */
171eda14cbcSMatt Macy typedef struct config_entry {
172eda14cbcSMatt Macy 	uint64_t		ce_txg;
173eda14cbcSMatt Macy 	nvlist_t		*ce_config;
174eda14cbcSMatt Macy 	struct config_entry	*ce_next;
175eda14cbcSMatt Macy } config_entry_t;
176eda14cbcSMatt Macy 
177eda14cbcSMatt Macy typedef struct vdev_entry {
178eda14cbcSMatt Macy 	uint64_t		ve_guid;
179eda14cbcSMatt Macy 	config_entry_t		*ve_configs;
180eda14cbcSMatt Macy 	struct vdev_entry	*ve_next;
181eda14cbcSMatt Macy } vdev_entry_t;
182eda14cbcSMatt Macy 
183eda14cbcSMatt Macy typedef struct pool_entry {
184eda14cbcSMatt Macy 	uint64_t		pe_guid;
185eda14cbcSMatt Macy 	vdev_entry_t		*pe_vdevs;
186eda14cbcSMatt Macy 	struct pool_entry	*pe_next;
187eda14cbcSMatt Macy } pool_entry_t;
188eda14cbcSMatt Macy 
189eda14cbcSMatt Macy typedef struct name_entry {
190eda14cbcSMatt Macy 	char			*ne_name;
191eda14cbcSMatt Macy 	uint64_t		ne_guid;
192eda14cbcSMatt Macy 	uint64_t		ne_order;
193eda14cbcSMatt Macy 	uint64_t		ne_num_labels;
194eda14cbcSMatt Macy 	struct name_entry	*ne_next;
195eda14cbcSMatt Macy } name_entry_t;
196eda14cbcSMatt Macy 
197eda14cbcSMatt Macy typedef struct pool_list {
198eda14cbcSMatt Macy 	pool_entry_t		*pools;
199eda14cbcSMatt Macy 	name_entry_t		*names;
200eda14cbcSMatt Macy } pool_list_t;
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy /*
203eda14cbcSMatt Macy  * Go through and fix up any path and/or devid information for the given vdev
204eda14cbcSMatt Macy  * configuration.
205eda14cbcSMatt Macy  */
206eda14cbcSMatt Macy static int
207eda14cbcSMatt Macy fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names)
208eda14cbcSMatt Macy {
209eda14cbcSMatt Macy 	nvlist_t **child;
210eda14cbcSMatt Macy 	uint_t c, children;
211eda14cbcSMatt Macy 	uint64_t guid;
212eda14cbcSMatt Macy 	name_entry_t *ne, *best;
213eda14cbcSMatt Macy 	char *path;
214eda14cbcSMatt Macy 
215eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
216eda14cbcSMatt Macy 	    &child, &children) == 0) {
217eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
218eda14cbcSMatt Macy 			if (fix_paths(hdl, child[c], names) != 0)
219eda14cbcSMatt Macy 				return (-1);
220eda14cbcSMatt Macy 		return (0);
221eda14cbcSMatt Macy 	}
222eda14cbcSMatt Macy 
223eda14cbcSMatt Macy 	/*
224eda14cbcSMatt Macy 	 * This is a leaf (file or disk) vdev.  In either case, go through
225eda14cbcSMatt Macy 	 * the name list and see if we find a matching guid.  If so, replace
226eda14cbcSMatt Macy 	 * the path and see if we can calculate a new devid.
227eda14cbcSMatt Macy 	 *
228eda14cbcSMatt Macy 	 * There may be multiple names associated with a particular guid, in
229eda14cbcSMatt Macy 	 * which case we have overlapping partitions or multiple paths to the
230eda14cbcSMatt Macy 	 * same disk.  In this case we prefer to use the path name which
231eda14cbcSMatt Macy 	 * matches the ZPOOL_CONFIG_PATH.  If no matching entry is found we
232eda14cbcSMatt Macy 	 * use the lowest order device which corresponds to the first match
233eda14cbcSMatt Macy 	 * while traversing the ZPOOL_IMPORT_PATH search path.
234eda14cbcSMatt Macy 	 */
235eda14cbcSMatt Macy 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
236eda14cbcSMatt Macy 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
237eda14cbcSMatt Macy 		path = NULL;
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy 	best = NULL;
240eda14cbcSMatt Macy 	for (ne = names; ne != NULL; ne = ne->ne_next) {
241eda14cbcSMatt Macy 		if (ne->ne_guid == guid) {
242eda14cbcSMatt Macy 			if (path == NULL) {
243eda14cbcSMatt Macy 				best = ne;
244eda14cbcSMatt Macy 				break;
245eda14cbcSMatt Macy 			}
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 			if ((strlen(path) == strlen(ne->ne_name)) &&
248eda14cbcSMatt Macy 			    strncmp(path, ne->ne_name, strlen(path)) == 0) {
249eda14cbcSMatt Macy 				best = ne;
250eda14cbcSMatt Macy 				break;
251eda14cbcSMatt Macy 			}
252eda14cbcSMatt Macy 
253eda14cbcSMatt Macy 			if (best == NULL) {
254eda14cbcSMatt Macy 				best = ne;
255eda14cbcSMatt Macy 				continue;
256eda14cbcSMatt Macy 			}
257eda14cbcSMatt Macy 
258eda14cbcSMatt Macy 			/* Prefer paths with move vdev labels. */
259eda14cbcSMatt Macy 			if (ne->ne_num_labels > best->ne_num_labels) {
260eda14cbcSMatt Macy 				best = ne;
261eda14cbcSMatt Macy 				continue;
262eda14cbcSMatt Macy 			}
263eda14cbcSMatt Macy 
264eda14cbcSMatt Macy 			/* Prefer paths earlier in the search order. */
265eda14cbcSMatt Macy 			if (ne->ne_num_labels == best->ne_num_labels &&
266eda14cbcSMatt Macy 			    ne->ne_order < best->ne_order) {
267eda14cbcSMatt Macy 				best = ne;
268eda14cbcSMatt Macy 				continue;
269eda14cbcSMatt Macy 			}
270eda14cbcSMatt Macy 		}
271eda14cbcSMatt Macy 	}
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy 	if (best == NULL)
274eda14cbcSMatt Macy 		return (0);
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy 	if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
277eda14cbcSMatt Macy 		return (-1);
278eda14cbcSMatt Macy 
279eda14cbcSMatt Macy 	update_vdev_config_dev_strs(nv);
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy 	return (0);
282eda14cbcSMatt Macy }
283eda14cbcSMatt Macy 
284eda14cbcSMatt Macy /*
285eda14cbcSMatt Macy  * Add the given configuration to the list of known devices.
286eda14cbcSMatt Macy  */
287eda14cbcSMatt Macy static int
288eda14cbcSMatt Macy add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path,
289eda14cbcSMatt Macy     int order, int num_labels, nvlist_t *config)
290eda14cbcSMatt Macy {
291eda14cbcSMatt Macy 	uint64_t pool_guid, vdev_guid, top_guid, txg, state;
292eda14cbcSMatt Macy 	pool_entry_t *pe;
293eda14cbcSMatt Macy 	vdev_entry_t *ve;
294eda14cbcSMatt Macy 	config_entry_t *ce;
295eda14cbcSMatt Macy 	name_entry_t *ne;
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy 	/*
298eda14cbcSMatt Macy 	 * If this is a hot spare not currently in use or level 2 cache
299eda14cbcSMatt Macy 	 * device, add it to the list of names to translate, but don't do
300eda14cbcSMatt Macy 	 * anything else.
301eda14cbcSMatt Macy 	 */
302eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
303eda14cbcSMatt Macy 	    &state) == 0 &&
304eda14cbcSMatt Macy 	    (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
305eda14cbcSMatt Macy 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
306eda14cbcSMatt Macy 		if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
307eda14cbcSMatt Macy 			return (-1);
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 		if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
310eda14cbcSMatt Macy 			free(ne);
311eda14cbcSMatt Macy 			return (-1);
312eda14cbcSMatt Macy 		}
313eda14cbcSMatt Macy 		ne->ne_guid = vdev_guid;
314eda14cbcSMatt Macy 		ne->ne_order = order;
315eda14cbcSMatt Macy 		ne->ne_num_labels = num_labels;
316eda14cbcSMatt Macy 		ne->ne_next = pl->names;
317eda14cbcSMatt Macy 		pl->names = ne;
318eda14cbcSMatt Macy 
319eda14cbcSMatt Macy 		return (0);
320eda14cbcSMatt Macy 	}
321eda14cbcSMatt Macy 
322eda14cbcSMatt Macy 	/*
323eda14cbcSMatt Macy 	 * If we have a valid config but cannot read any of these fields, then
324eda14cbcSMatt Macy 	 * it means we have a half-initialized label.  In vdev_label_init()
325eda14cbcSMatt Macy 	 * we write a label with txg == 0 so that we can identify the device
326eda14cbcSMatt Macy 	 * in case the user refers to the same disk later on.  If we fail to
327eda14cbcSMatt Macy 	 * create the pool, we'll be left with a label in this state
328eda14cbcSMatt Macy 	 * which should not be considered part of a valid pool.
329eda14cbcSMatt Macy 	 */
330eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
331eda14cbcSMatt Macy 	    &pool_guid) != 0 ||
332eda14cbcSMatt Macy 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
333eda14cbcSMatt Macy 	    &vdev_guid) != 0 ||
334eda14cbcSMatt Macy 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
335eda14cbcSMatt Macy 	    &top_guid) != 0 ||
336eda14cbcSMatt Macy 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
337eda14cbcSMatt Macy 	    &txg) != 0 || txg == 0) {
338eda14cbcSMatt Macy 		return (0);
339eda14cbcSMatt Macy 	}
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy 	/*
342eda14cbcSMatt Macy 	 * First, see if we know about this pool.  If not, then add it to the
343eda14cbcSMatt Macy 	 * list of known pools.
344eda14cbcSMatt Macy 	 */
345eda14cbcSMatt Macy 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
346eda14cbcSMatt Macy 		if (pe->pe_guid == pool_guid)
347eda14cbcSMatt Macy 			break;
348eda14cbcSMatt Macy 	}
349eda14cbcSMatt Macy 
350eda14cbcSMatt Macy 	if (pe == NULL) {
351eda14cbcSMatt Macy 		if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
352eda14cbcSMatt Macy 			return (-1);
353eda14cbcSMatt Macy 		}
354eda14cbcSMatt Macy 		pe->pe_guid = pool_guid;
355eda14cbcSMatt Macy 		pe->pe_next = pl->pools;
356eda14cbcSMatt Macy 		pl->pools = pe;
357eda14cbcSMatt Macy 	}
358eda14cbcSMatt Macy 
359eda14cbcSMatt Macy 	/*
360eda14cbcSMatt Macy 	 * Second, see if we know about this toplevel vdev.  Add it if its
361eda14cbcSMatt Macy 	 * missing.
362eda14cbcSMatt Macy 	 */
363eda14cbcSMatt Macy 	for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
364eda14cbcSMatt Macy 		if (ve->ve_guid == top_guid)
365eda14cbcSMatt Macy 			break;
366eda14cbcSMatt Macy 	}
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 	if (ve == NULL) {
369eda14cbcSMatt Macy 		if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
370eda14cbcSMatt Macy 			return (-1);
371eda14cbcSMatt Macy 		}
372eda14cbcSMatt Macy 		ve->ve_guid = top_guid;
373eda14cbcSMatt Macy 		ve->ve_next = pe->pe_vdevs;
374eda14cbcSMatt Macy 		pe->pe_vdevs = ve;
375eda14cbcSMatt Macy 	}
376eda14cbcSMatt Macy 
377eda14cbcSMatt Macy 	/*
378eda14cbcSMatt Macy 	 * Third, see if we have a config with a matching transaction group.  If
379eda14cbcSMatt Macy 	 * so, then we do nothing.  Otherwise, add it to the list of known
380eda14cbcSMatt Macy 	 * configs.
381eda14cbcSMatt Macy 	 */
382eda14cbcSMatt Macy 	for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
383eda14cbcSMatt Macy 		if (ce->ce_txg == txg)
384eda14cbcSMatt Macy 			break;
385eda14cbcSMatt Macy 	}
386eda14cbcSMatt Macy 
387eda14cbcSMatt Macy 	if (ce == NULL) {
388eda14cbcSMatt Macy 		if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) {
389eda14cbcSMatt Macy 			return (-1);
390eda14cbcSMatt Macy 		}
391eda14cbcSMatt Macy 		ce->ce_txg = txg;
392eda14cbcSMatt Macy 		ce->ce_config = fnvlist_dup(config);
393eda14cbcSMatt Macy 		ce->ce_next = ve->ve_configs;
394eda14cbcSMatt Macy 		ve->ve_configs = ce;
395eda14cbcSMatt Macy 	}
396eda14cbcSMatt Macy 
397eda14cbcSMatt Macy 	/*
398eda14cbcSMatt Macy 	 * At this point we've successfully added our config to the list of
399eda14cbcSMatt Macy 	 * known configs.  The last thing to do is add the vdev guid -> path
400eda14cbcSMatt Macy 	 * mappings so that we can fix up the configuration as necessary before
401eda14cbcSMatt Macy 	 * doing the import.
402eda14cbcSMatt Macy 	 */
403eda14cbcSMatt Macy 	if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
404eda14cbcSMatt Macy 		return (-1);
405eda14cbcSMatt Macy 
406eda14cbcSMatt Macy 	if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
407eda14cbcSMatt Macy 		free(ne);
408eda14cbcSMatt Macy 		return (-1);
409eda14cbcSMatt Macy 	}
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy 	ne->ne_guid = vdev_guid;
412eda14cbcSMatt Macy 	ne->ne_order = order;
413eda14cbcSMatt Macy 	ne->ne_num_labels = num_labels;
414eda14cbcSMatt Macy 	ne->ne_next = pl->names;
415eda14cbcSMatt Macy 	pl->names = ne;
416eda14cbcSMatt Macy 
417eda14cbcSMatt Macy 	return (0);
418eda14cbcSMatt Macy }
419eda14cbcSMatt Macy 
420eda14cbcSMatt Macy static int
421eda14cbcSMatt Macy zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid,
422eda14cbcSMatt Macy     boolean_t *isactive)
423eda14cbcSMatt Macy {
424eda14cbcSMatt Macy 	ASSERT(hdl->lpc_ops->pco_pool_active != NULL);
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy 	int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name,
427eda14cbcSMatt Macy 	    guid, isactive);
428eda14cbcSMatt Macy 
429eda14cbcSMatt Macy 	return (error);
430eda14cbcSMatt Macy }
431eda14cbcSMatt Macy 
432eda14cbcSMatt Macy static nvlist_t *
433eda14cbcSMatt Macy zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig)
434eda14cbcSMatt Macy {
435eda14cbcSMatt Macy 	ASSERT(hdl->lpc_ops->pco_refresh_config != NULL);
436eda14cbcSMatt Macy 
437eda14cbcSMatt Macy 	return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle,
438eda14cbcSMatt Macy 	    tryconfig));
439eda14cbcSMatt Macy }
440eda14cbcSMatt Macy 
441eda14cbcSMatt Macy /*
442eda14cbcSMatt Macy  * Determine if the vdev id is a hole in the namespace.
443eda14cbcSMatt Macy  */
444eda14cbcSMatt Macy static boolean_t
445eda14cbcSMatt Macy vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
446eda14cbcSMatt Macy {
447eda14cbcSMatt Macy 	int c;
448eda14cbcSMatt Macy 
449eda14cbcSMatt Macy 	for (c = 0; c < holes; c++) {
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 		/* Top-level is a hole */
452eda14cbcSMatt Macy 		if (hole_array[c] == id)
453eda14cbcSMatt Macy 			return (B_TRUE);
454eda14cbcSMatt Macy 	}
455eda14cbcSMatt Macy 	return (B_FALSE);
456eda14cbcSMatt Macy }
457eda14cbcSMatt Macy 
458eda14cbcSMatt Macy /*
459eda14cbcSMatt Macy  * Convert our list of pools into the definitive set of configurations.  We
460eda14cbcSMatt Macy  * start by picking the best config for each toplevel vdev.  Once that's done,
461eda14cbcSMatt Macy  * we assemble the toplevel vdevs into a full config for the pool.  We make a
462eda14cbcSMatt Macy  * pass to fix up any incorrect paths, and then add it to the main list to
463eda14cbcSMatt Macy  * return to the user.
464eda14cbcSMatt Macy  */
465eda14cbcSMatt Macy static nvlist_t *
466eda14cbcSMatt Macy get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok,
467eda14cbcSMatt Macy     nvlist_t *policy)
468eda14cbcSMatt Macy {
469eda14cbcSMatt Macy 	pool_entry_t *pe;
470eda14cbcSMatt Macy 	vdev_entry_t *ve;
471eda14cbcSMatt Macy 	config_entry_t *ce;
472eda14cbcSMatt Macy 	nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
473eda14cbcSMatt Macy 	nvlist_t **spares, **l2cache;
474eda14cbcSMatt Macy 	uint_t i, nspares, nl2cache;
475eda14cbcSMatt Macy 	boolean_t config_seen;
476eda14cbcSMatt Macy 	uint64_t best_txg;
477eda14cbcSMatt Macy 	char *name, *hostname = NULL;
478eda14cbcSMatt Macy 	uint64_t guid;
479eda14cbcSMatt Macy 	uint_t children = 0;
480eda14cbcSMatt Macy 	nvlist_t **child = NULL;
481eda14cbcSMatt Macy 	uint_t holes;
482eda14cbcSMatt Macy 	uint64_t *hole_array, max_id;
483eda14cbcSMatt Macy 	uint_t c;
484eda14cbcSMatt Macy 	boolean_t isactive;
485eda14cbcSMatt Macy 	uint64_t hostid;
486eda14cbcSMatt Macy 	nvlist_t *nvl;
487eda14cbcSMatt Macy 	boolean_t valid_top_config = B_FALSE;
488eda14cbcSMatt Macy 
489eda14cbcSMatt Macy 	if (nvlist_alloc(&ret, 0, 0) != 0)
490eda14cbcSMatt Macy 		goto nomem;
491eda14cbcSMatt Macy 
492eda14cbcSMatt Macy 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
493eda14cbcSMatt Macy 		uint64_t id, max_txg = 0;
494eda14cbcSMatt Macy 
495eda14cbcSMatt Macy 		if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
496eda14cbcSMatt Macy 			goto nomem;
497eda14cbcSMatt Macy 		config_seen = B_FALSE;
498eda14cbcSMatt Macy 
499eda14cbcSMatt Macy 		/*
500eda14cbcSMatt Macy 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
501eda14cbcSMatt Macy 		 * from the first one we find, and then go through the rest and
502eda14cbcSMatt Macy 		 * add them as necessary to the 'vdevs' member of the config.
503eda14cbcSMatt Macy 		 */
504eda14cbcSMatt Macy 		for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy 			/*
507eda14cbcSMatt Macy 			 * Determine the best configuration for this vdev by
508eda14cbcSMatt Macy 			 * selecting the config with the latest transaction
509eda14cbcSMatt Macy 			 * group.
510eda14cbcSMatt Macy 			 */
511eda14cbcSMatt Macy 			best_txg = 0;
512eda14cbcSMatt Macy 			for (ce = ve->ve_configs; ce != NULL;
513eda14cbcSMatt Macy 			    ce = ce->ce_next) {
514eda14cbcSMatt Macy 
515eda14cbcSMatt Macy 				if (ce->ce_txg > best_txg) {
516eda14cbcSMatt Macy 					tmp = ce->ce_config;
517eda14cbcSMatt Macy 					best_txg = ce->ce_txg;
518eda14cbcSMatt Macy 				}
519eda14cbcSMatt Macy 			}
520eda14cbcSMatt Macy 
521eda14cbcSMatt Macy 			/*
522eda14cbcSMatt Macy 			 * We rely on the fact that the max txg for the
523eda14cbcSMatt Macy 			 * pool will contain the most up-to-date information
524eda14cbcSMatt Macy 			 * about the valid top-levels in the vdev namespace.
525eda14cbcSMatt Macy 			 */
526eda14cbcSMatt Macy 			if (best_txg > max_txg) {
527eda14cbcSMatt Macy 				(void) nvlist_remove(config,
528eda14cbcSMatt Macy 				    ZPOOL_CONFIG_VDEV_CHILDREN,
529eda14cbcSMatt Macy 				    DATA_TYPE_UINT64);
530eda14cbcSMatt Macy 				(void) nvlist_remove(config,
531eda14cbcSMatt Macy 				    ZPOOL_CONFIG_HOLE_ARRAY,
532eda14cbcSMatt Macy 				    DATA_TYPE_UINT64_ARRAY);
533eda14cbcSMatt Macy 
534eda14cbcSMatt Macy 				max_txg = best_txg;
535eda14cbcSMatt Macy 				hole_array = NULL;
536eda14cbcSMatt Macy 				holes = 0;
537eda14cbcSMatt Macy 				max_id = 0;
538eda14cbcSMatt Macy 				valid_top_config = B_FALSE;
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy 				if (nvlist_lookup_uint64(tmp,
541eda14cbcSMatt Macy 				    ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
542eda14cbcSMatt Macy 					verify(nvlist_add_uint64(config,
543eda14cbcSMatt Macy 					    ZPOOL_CONFIG_VDEV_CHILDREN,
544eda14cbcSMatt Macy 					    max_id) == 0);
545eda14cbcSMatt Macy 					valid_top_config = B_TRUE;
546eda14cbcSMatt Macy 				}
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy 				if (nvlist_lookup_uint64_array(tmp,
549eda14cbcSMatt Macy 				    ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
550eda14cbcSMatt Macy 				    &holes) == 0) {
551eda14cbcSMatt Macy 					verify(nvlist_add_uint64_array(config,
552eda14cbcSMatt Macy 					    ZPOOL_CONFIG_HOLE_ARRAY,
553eda14cbcSMatt Macy 					    hole_array, holes) == 0);
554eda14cbcSMatt Macy 				}
555eda14cbcSMatt Macy 			}
556eda14cbcSMatt Macy 
557eda14cbcSMatt Macy 			if (!config_seen) {
558eda14cbcSMatt Macy 				/*
559eda14cbcSMatt Macy 				 * Copy the relevant pieces of data to the pool
560eda14cbcSMatt Macy 				 * configuration:
561eda14cbcSMatt Macy 				 *
562eda14cbcSMatt Macy 				 *	version
563eda14cbcSMatt Macy 				 *	pool guid
564eda14cbcSMatt Macy 				 *	name
565eda14cbcSMatt Macy 				 *	comment (if available)
566ee36e25aSMartin Matuska 				 *	compatibility features (if available)
567eda14cbcSMatt Macy 				 *	pool state
568eda14cbcSMatt Macy 				 *	hostid (if available)
569eda14cbcSMatt Macy 				 *	hostname (if available)
570eda14cbcSMatt Macy 				 */
571eda14cbcSMatt Macy 				uint64_t state, version;
572eda14cbcSMatt Macy 				char *comment = NULL;
573ee36e25aSMartin Matuska 				char *compatibility = NULL;
574eda14cbcSMatt Macy 
575eda14cbcSMatt Macy 				version = fnvlist_lookup_uint64(tmp,
576eda14cbcSMatt Macy 				    ZPOOL_CONFIG_VERSION);
577eda14cbcSMatt Macy 				fnvlist_add_uint64(config,
578eda14cbcSMatt Macy 				    ZPOOL_CONFIG_VERSION, version);
579eda14cbcSMatt Macy 				guid = fnvlist_lookup_uint64(tmp,
580eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_GUID);
581eda14cbcSMatt Macy 				fnvlist_add_uint64(config,
582eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_GUID, guid);
583eda14cbcSMatt Macy 				name = fnvlist_lookup_string(tmp,
584eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_NAME);
585eda14cbcSMatt Macy 				fnvlist_add_string(config,
586eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_NAME, name);
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy 				if (nvlist_lookup_string(tmp,
589eda14cbcSMatt Macy 				    ZPOOL_CONFIG_COMMENT, &comment) == 0)
590eda14cbcSMatt Macy 					fnvlist_add_string(config,
591eda14cbcSMatt Macy 					    ZPOOL_CONFIG_COMMENT, comment);
592eda14cbcSMatt Macy 
593ee36e25aSMartin Matuska 				if (nvlist_lookup_string(tmp,
594ee36e25aSMartin Matuska 				    ZPOOL_CONFIG_COMPATIBILITY,
595ee36e25aSMartin Matuska 				    &compatibility) == 0)
596ee36e25aSMartin Matuska 					fnvlist_add_string(config,
597ee36e25aSMartin Matuska 					    ZPOOL_CONFIG_COMPATIBILITY,
598ee36e25aSMartin Matuska 					    compatibility);
599ee36e25aSMartin Matuska 
600eda14cbcSMatt Macy 				state = fnvlist_lookup_uint64(tmp,
601eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_STATE);
602eda14cbcSMatt Macy 				fnvlist_add_uint64(config,
603eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_STATE, state);
604eda14cbcSMatt Macy 
605eda14cbcSMatt Macy 				hostid = 0;
606eda14cbcSMatt Macy 				if (nvlist_lookup_uint64(tmp,
607eda14cbcSMatt Macy 				    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
608eda14cbcSMatt Macy 					fnvlist_add_uint64(config,
609eda14cbcSMatt Macy 					    ZPOOL_CONFIG_HOSTID, hostid);
610eda14cbcSMatt Macy 					hostname = fnvlist_lookup_string(tmp,
611eda14cbcSMatt Macy 					    ZPOOL_CONFIG_HOSTNAME);
612eda14cbcSMatt Macy 					fnvlist_add_string(config,
613eda14cbcSMatt Macy 					    ZPOOL_CONFIG_HOSTNAME, hostname);
614eda14cbcSMatt Macy 				}
615eda14cbcSMatt Macy 
616eda14cbcSMatt Macy 				config_seen = B_TRUE;
617eda14cbcSMatt Macy 			}
618eda14cbcSMatt Macy 
619eda14cbcSMatt Macy 			/*
620eda14cbcSMatt Macy 			 * Add this top-level vdev to the child array.
621eda14cbcSMatt Macy 			 */
622eda14cbcSMatt Macy 			verify(nvlist_lookup_nvlist(tmp,
623eda14cbcSMatt Macy 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
624eda14cbcSMatt Macy 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
625eda14cbcSMatt Macy 			    &id) == 0);
626eda14cbcSMatt Macy 
627eda14cbcSMatt Macy 			if (id >= children) {
628eda14cbcSMatt Macy 				nvlist_t **newchild;
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy 				newchild = zutil_alloc(hdl, (id + 1) *
631eda14cbcSMatt Macy 				    sizeof (nvlist_t *));
632eda14cbcSMatt Macy 				if (newchild == NULL)
633eda14cbcSMatt Macy 					goto nomem;
634eda14cbcSMatt Macy 
635eda14cbcSMatt Macy 				for (c = 0; c < children; c++)
636eda14cbcSMatt Macy 					newchild[c] = child[c];
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 				free(child);
639eda14cbcSMatt Macy 				child = newchild;
640eda14cbcSMatt Macy 				children = id + 1;
641eda14cbcSMatt Macy 			}
642eda14cbcSMatt Macy 			if (nvlist_dup(nvtop, &child[id], 0) != 0)
643eda14cbcSMatt Macy 				goto nomem;
644eda14cbcSMatt Macy 
645eda14cbcSMatt Macy 		}
646eda14cbcSMatt Macy 
647eda14cbcSMatt Macy 		/*
648eda14cbcSMatt Macy 		 * If we have information about all the top-levels then
649eda14cbcSMatt Macy 		 * clean up the nvlist which we've constructed. This
650eda14cbcSMatt Macy 		 * means removing any extraneous devices that are
651eda14cbcSMatt Macy 		 * beyond the valid range or adding devices to the end
652eda14cbcSMatt Macy 		 * of our array which appear to be missing.
653eda14cbcSMatt Macy 		 */
654eda14cbcSMatt Macy 		if (valid_top_config) {
655eda14cbcSMatt Macy 			if (max_id < children) {
656eda14cbcSMatt Macy 				for (c = max_id; c < children; c++)
657eda14cbcSMatt Macy 					nvlist_free(child[c]);
658eda14cbcSMatt Macy 				children = max_id;
659eda14cbcSMatt Macy 			} else if (max_id > children) {
660eda14cbcSMatt Macy 				nvlist_t **newchild;
661eda14cbcSMatt Macy 
662eda14cbcSMatt Macy 				newchild = zutil_alloc(hdl, (max_id) *
663eda14cbcSMatt Macy 				    sizeof (nvlist_t *));
664eda14cbcSMatt Macy 				if (newchild == NULL)
665eda14cbcSMatt Macy 					goto nomem;
666eda14cbcSMatt Macy 
667eda14cbcSMatt Macy 				for (c = 0; c < children; c++)
668eda14cbcSMatt Macy 					newchild[c] = child[c];
669eda14cbcSMatt Macy 
670eda14cbcSMatt Macy 				free(child);
671eda14cbcSMatt Macy 				child = newchild;
672eda14cbcSMatt Macy 				children = max_id;
673eda14cbcSMatt Macy 			}
674eda14cbcSMatt Macy 		}
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
677eda14cbcSMatt Macy 		    &guid) == 0);
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy 		/*
680eda14cbcSMatt Macy 		 * The vdev namespace may contain holes as a result of
681eda14cbcSMatt Macy 		 * device removal. We must add them back into the vdev
682eda14cbcSMatt Macy 		 * tree before we process any missing devices.
683eda14cbcSMatt Macy 		 */
684eda14cbcSMatt Macy 		if (holes > 0) {
685eda14cbcSMatt Macy 			ASSERT(valid_top_config);
686eda14cbcSMatt Macy 
687eda14cbcSMatt Macy 			for (c = 0; c < children; c++) {
688eda14cbcSMatt Macy 				nvlist_t *holey;
689eda14cbcSMatt Macy 
690eda14cbcSMatt Macy 				if (child[c] != NULL ||
691eda14cbcSMatt Macy 				    !vdev_is_hole(hole_array, holes, c))
692eda14cbcSMatt Macy 					continue;
693eda14cbcSMatt Macy 
694eda14cbcSMatt Macy 				if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
695eda14cbcSMatt Macy 				    0) != 0)
696eda14cbcSMatt Macy 					goto nomem;
697eda14cbcSMatt Macy 
698eda14cbcSMatt Macy 				/*
699eda14cbcSMatt Macy 				 * Holes in the namespace are treated as
700eda14cbcSMatt Macy 				 * "hole" top-level vdevs and have a
701eda14cbcSMatt Macy 				 * special flag set on them.
702eda14cbcSMatt Macy 				 */
703eda14cbcSMatt Macy 				if (nvlist_add_string(holey,
704eda14cbcSMatt Macy 				    ZPOOL_CONFIG_TYPE,
705eda14cbcSMatt Macy 				    VDEV_TYPE_HOLE) != 0 ||
706eda14cbcSMatt Macy 				    nvlist_add_uint64(holey,
707eda14cbcSMatt Macy 				    ZPOOL_CONFIG_ID, c) != 0 ||
708eda14cbcSMatt Macy 				    nvlist_add_uint64(holey,
709eda14cbcSMatt Macy 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
710eda14cbcSMatt Macy 					nvlist_free(holey);
711eda14cbcSMatt Macy 					goto nomem;
712eda14cbcSMatt Macy 				}
713eda14cbcSMatt Macy 				child[c] = holey;
714eda14cbcSMatt Macy 			}
715eda14cbcSMatt Macy 		}
716eda14cbcSMatt Macy 
717eda14cbcSMatt Macy 		/*
718eda14cbcSMatt Macy 		 * Look for any missing top-level vdevs.  If this is the case,
719eda14cbcSMatt Macy 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
720eda14cbcSMatt Macy 		 * simply compress the child array, because the kernel performs
721eda14cbcSMatt Macy 		 * certain checks to make sure the vdev IDs match their location
722eda14cbcSMatt Macy 		 * in the configuration.
723eda14cbcSMatt Macy 		 */
724eda14cbcSMatt Macy 		for (c = 0; c < children; c++) {
725eda14cbcSMatt Macy 			if (child[c] == NULL) {
726eda14cbcSMatt Macy 				nvlist_t *missing;
727eda14cbcSMatt Macy 				if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
728eda14cbcSMatt Macy 				    0) != 0)
729eda14cbcSMatt Macy 					goto nomem;
730eda14cbcSMatt Macy 				if (nvlist_add_string(missing,
731eda14cbcSMatt Macy 				    ZPOOL_CONFIG_TYPE,
732eda14cbcSMatt Macy 				    VDEV_TYPE_MISSING) != 0 ||
733eda14cbcSMatt Macy 				    nvlist_add_uint64(missing,
734eda14cbcSMatt Macy 				    ZPOOL_CONFIG_ID, c) != 0 ||
735eda14cbcSMatt Macy 				    nvlist_add_uint64(missing,
736eda14cbcSMatt Macy 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
737eda14cbcSMatt Macy 					nvlist_free(missing);
738eda14cbcSMatt Macy 					goto nomem;
739eda14cbcSMatt Macy 				}
740eda14cbcSMatt Macy 				child[c] = missing;
741eda14cbcSMatt Macy 			}
742eda14cbcSMatt Macy 		}
743eda14cbcSMatt Macy 
744eda14cbcSMatt Macy 		/*
745eda14cbcSMatt Macy 		 * Put all of this pool's top-level vdevs into a root vdev.
746eda14cbcSMatt Macy 		 */
747eda14cbcSMatt Macy 		if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
748eda14cbcSMatt Macy 			goto nomem;
749eda14cbcSMatt Macy 		if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
750eda14cbcSMatt Macy 		    VDEV_TYPE_ROOT) != 0 ||
751eda14cbcSMatt Macy 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
752eda14cbcSMatt Macy 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
753eda14cbcSMatt Macy 		    nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
754681ce946SMartin Matuska 		    (const nvlist_t **)child, children) != 0) {
755eda14cbcSMatt Macy 			nvlist_free(nvroot);
756eda14cbcSMatt Macy 			goto nomem;
757eda14cbcSMatt Macy 		}
758eda14cbcSMatt Macy 
759eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
760eda14cbcSMatt Macy 			nvlist_free(child[c]);
761eda14cbcSMatt Macy 		free(child);
762eda14cbcSMatt Macy 		children = 0;
763eda14cbcSMatt Macy 		child = NULL;
764eda14cbcSMatt Macy 
765eda14cbcSMatt Macy 		/*
766eda14cbcSMatt Macy 		 * Go through and fix up any paths and/or devids based on our
767eda14cbcSMatt Macy 		 * known list of vdev GUID -> path mappings.
768eda14cbcSMatt Macy 		 */
769eda14cbcSMatt Macy 		if (fix_paths(hdl, nvroot, pl->names) != 0) {
770eda14cbcSMatt Macy 			nvlist_free(nvroot);
771eda14cbcSMatt Macy 			goto nomem;
772eda14cbcSMatt Macy 		}
773eda14cbcSMatt Macy 
774eda14cbcSMatt Macy 		/*
775eda14cbcSMatt Macy 		 * Add the root vdev to this pool's configuration.
776eda14cbcSMatt Macy 		 */
777eda14cbcSMatt Macy 		if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
778eda14cbcSMatt Macy 		    nvroot) != 0) {
779eda14cbcSMatt Macy 			nvlist_free(nvroot);
780eda14cbcSMatt Macy 			goto nomem;
781eda14cbcSMatt Macy 		}
782eda14cbcSMatt Macy 		nvlist_free(nvroot);
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy 		/*
785eda14cbcSMatt Macy 		 * zdb uses this path to report on active pools that were
786eda14cbcSMatt Macy 		 * imported or created using -R.
787eda14cbcSMatt Macy 		 */
788eda14cbcSMatt Macy 		if (active_ok)
789eda14cbcSMatt Macy 			goto add_pool;
790eda14cbcSMatt Macy 
791eda14cbcSMatt Macy 		/*
792eda14cbcSMatt Macy 		 * Determine if this pool is currently active, in which case we
793eda14cbcSMatt Macy 		 * can't actually import it.
794eda14cbcSMatt Macy 		 */
795eda14cbcSMatt Macy 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
796eda14cbcSMatt Macy 		    &name) == 0);
797eda14cbcSMatt Macy 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
798eda14cbcSMatt Macy 		    &guid) == 0);
799eda14cbcSMatt Macy 
800eda14cbcSMatt Macy 		if (zutil_pool_active(hdl, name, guid, &isactive) != 0)
801eda14cbcSMatt Macy 			goto error;
802eda14cbcSMatt Macy 
803eda14cbcSMatt Macy 		if (isactive) {
804eda14cbcSMatt Macy 			nvlist_free(config);
805eda14cbcSMatt Macy 			config = NULL;
806eda14cbcSMatt Macy 			continue;
807eda14cbcSMatt Macy 		}
808eda14cbcSMatt Macy 
809eda14cbcSMatt Macy 		if (policy != NULL) {
810eda14cbcSMatt Macy 			if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
811eda14cbcSMatt Macy 			    policy) != 0)
812eda14cbcSMatt Macy 				goto nomem;
813eda14cbcSMatt Macy 		}
814eda14cbcSMatt Macy 
815eda14cbcSMatt Macy 		if ((nvl = zutil_refresh_config(hdl, config)) == NULL) {
816eda14cbcSMatt Macy 			nvlist_free(config);
817eda14cbcSMatt Macy 			config = NULL;
818eda14cbcSMatt Macy 			continue;
819eda14cbcSMatt Macy 		}
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy 		nvlist_free(config);
822eda14cbcSMatt Macy 		config = nvl;
823eda14cbcSMatt Macy 
824eda14cbcSMatt Macy 		/*
825eda14cbcSMatt Macy 		 * Go through and update the paths for spares, now that we have
826eda14cbcSMatt Macy 		 * them.
827eda14cbcSMatt Macy 		 */
828eda14cbcSMatt Macy 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
829eda14cbcSMatt Macy 		    &nvroot) == 0);
830eda14cbcSMatt Macy 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
831eda14cbcSMatt Macy 		    &spares, &nspares) == 0) {
832eda14cbcSMatt Macy 			for (i = 0; i < nspares; i++) {
833eda14cbcSMatt Macy 				if (fix_paths(hdl, spares[i], pl->names) != 0)
834eda14cbcSMatt Macy 					goto nomem;
835eda14cbcSMatt Macy 			}
836eda14cbcSMatt Macy 		}
837eda14cbcSMatt Macy 
838eda14cbcSMatt Macy 		/*
839eda14cbcSMatt Macy 		 * Update the paths for l2cache devices.
840eda14cbcSMatt Macy 		 */
841eda14cbcSMatt Macy 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
842eda14cbcSMatt Macy 		    &l2cache, &nl2cache) == 0) {
843eda14cbcSMatt Macy 			for (i = 0; i < nl2cache; i++) {
844eda14cbcSMatt Macy 				if (fix_paths(hdl, l2cache[i], pl->names) != 0)
845eda14cbcSMatt Macy 					goto nomem;
846eda14cbcSMatt Macy 			}
847eda14cbcSMatt Macy 		}
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy 		/*
850eda14cbcSMatt Macy 		 * Restore the original information read from the actual label.
851eda14cbcSMatt Macy 		 */
852eda14cbcSMatt Macy 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
853eda14cbcSMatt Macy 		    DATA_TYPE_UINT64);
854eda14cbcSMatt Macy 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
855eda14cbcSMatt Macy 		    DATA_TYPE_STRING);
856eda14cbcSMatt Macy 		if (hostid != 0) {
857eda14cbcSMatt Macy 			verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
858eda14cbcSMatt Macy 			    hostid) == 0);
859eda14cbcSMatt Macy 			verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
860eda14cbcSMatt Macy 			    hostname) == 0);
861eda14cbcSMatt Macy 		}
862eda14cbcSMatt Macy 
863eda14cbcSMatt Macy add_pool:
864eda14cbcSMatt Macy 		/*
865eda14cbcSMatt Macy 		 * Add this pool to the list of configs.
866eda14cbcSMatt Macy 		 */
867eda14cbcSMatt Macy 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
868eda14cbcSMatt Macy 		    &name) == 0);
869eda14cbcSMatt Macy 
870eda14cbcSMatt Macy 		if (nvlist_add_nvlist(ret, name, config) != 0)
871eda14cbcSMatt Macy 			goto nomem;
872eda14cbcSMatt Macy 
873eda14cbcSMatt Macy 		nvlist_free(config);
874eda14cbcSMatt Macy 		config = NULL;
875eda14cbcSMatt Macy 	}
876eda14cbcSMatt Macy 
877eda14cbcSMatt Macy 	return (ret);
878eda14cbcSMatt Macy 
879eda14cbcSMatt Macy nomem:
880eda14cbcSMatt Macy 	(void) zutil_no_memory(hdl);
881eda14cbcSMatt Macy error:
882eda14cbcSMatt Macy 	nvlist_free(config);
883eda14cbcSMatt Macy 	nvlist_free(ret);
884eda14cbcSMatt Macy 	for (c = 0; c < children; c++)
885eda14cbcSMatt Macy 		nvlist_free(child[c]);
886eda14cbcSMatt Macy 	free(child);
887eda14cbcSMatt Macy 
888eda14cbcSMatt Macy 	return (NULL);
889eda14cbcSMatt Macy }
890eda14cbcSMatt Macy 
891eda14cbcSMatt Macy /*
892eda14cbcSMatt Macy  * Return the offset of the given label.
893eda14cbcSMatt Macy  */
894eda14cbcSMatt Macy static uint64_t
895eda14cbcSMatt Macy label_offset(uint64_t size, int l)
896eda14cbcSMatt Macy {
897eda14cbcSMatt Macy 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
898eda14cbcSMatt Macy 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
899eda14cbcSMatt Macy 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
900eda14cbcSMatt Macy }
901eda14cbcSMatt Macy 
902eda14cbcSMatt Macy /*
90316038816SMartin Matuska  * The same description applies as to zpool_read_label below,
90416038816SMartin Matuska  * except here we do it without aio, presumably because an aio call
90516038816SMartin Matuska  * errored out in a way we think not using it could circumvent.
90616038816SMartin Matuska  */
90716038816SMartin Matuska static int
90816038816SMartin Matuska zpool_read_label_slow(int fd, nvlist_t **config, int *num_labels)
90916038816SMartin Matuska {
91016038816SMartin Matuska 	struct stat64 statbuf;
91116038816SMartin Matuska 	int l, count = 0;
91216038816SMartin Matuska 	vdev_phys_t *label;
91316038816SMartin Matuska 	nvlist_t *expected_config = NULL;
91416038816SMartin Matuska 	uint64_t expected_guid = 0, size;
91516038816SMartin Matuska 	int error;
91616038816SMartin Matuska 
91716038816SMartin Matuska 	*config = NULL;
91816038816SMartin Matuska 
91916038816SMartin Matuska 	if (fstat64_blk(fd, &statbuf) == -1)
92016038816SMartin Matuska 		return (0);
92116038816SMartin Matuska 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
92216038816SMartin Matuska 
92316038816SMartin Matuska 	error = posix_memalign((void **)&label, PAGESIZE, sizeof (*label));
92416038816SMartin Matuska 	if (error)
92516038816SMartin Matuska 		return (-1);
92616038816SMartin Matuska 
92716038816SMartin Matuska 	for (l = 0; l < VDEV_LABELS; l++) {
92816038816SMartin Matuska 		uint64_t state, guid, txg;
92916038816SMartin Matuska 		off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE;
93016038816SMartin Matuska 
93116038816SMartin Matuska 		if (pread64(fd, label, sizeof (vdev_phys_t),
93216038816SMartin Matuska 		    offset) != sizeof (vdev_phys_t))
93316038816SMartin Matuska 			continue;
93416038816SMartin Matuska 
93516038816SMartin Matuska 		if (nvlist_unpack(label->vp_nvlist,
93616038816SMartin Matuska 		    sizeof (label->vp_nvlist), config, 0) != 0)
93716038816SMartin Matuska 			continue;
93816038816SMartin Matuska 
93916038816SMartin Matuska 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
94016038816SMartin Matuska 		    &guid) != 0 || guid == 0) {
94116038816SMartin Matuska 			nvlist_free(*config);
94216038816SMartin Matuska 			continue;
94316038816SMartin Matuska 		}
94416038816SMartin Matuska 
94516038816SMartin Matuska 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
94616038816SMartin Matuska 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
94716038816SMartin Matuska 			nvlist_free(*config);
94816038816SMartin Matuska 			continue;
94916038816SMartin Matuska 		}
95016038816SMartin Matuska 
95116038816SMartin Matuska 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
95216038816SMartin Matuska 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
95316038816SMartin Matuska 		    &txg) != 0 || txg == 0)) {
95416038816SMartin Matuska 			nvlist_free(*config);
95516038816SMartin Matuska 			continue;
95616038816SMartin Matuska 		}
95716038816SMartin Matuska 
95816038816SMartin Matuska 		if (expected_guid) {
95916038816SMartin Matuska 			if (expected_guid == guid)
96016038816SMartin Matuska 				count++;
96116038816SMartin Matuska 
96216038816SMartin Matuska 			nvlist_free(*config);
96316038816SMartin Matuska 		} else {
96416038816SMartin Matuska 			expected_config = *config;
96516038816SMartin Matuska 			expected_guid = guid;
96616038816SMartin Matuska 			count++;
96716038816SMartin Matuska 		}
96816038816SMartin Matuska 	}
96916038816SMartin Matuska 
97016038816SMartin Matuska 	if (num_labels != NULL)
97116038816SMartin Matuska 		*num_labels = count;
97216038816SMartin Matuska 
97316038816SMartin Matuska 	free(label);
97416038816SMartin Matuska 	*config = expected_config;
97516038816SMartin Matuska 
97616038816SMartin Matuska 	return (0);
97716038816SMartin Matuska }
97816038816SMartin Matuska 
97916038816SMartin Matuska /*
980eda14cbcSMatt Macy  * Given a file descriptor, read the label information and return an nvlist
981eda14cbcSMatt Macy  * describing the configuration, if there is one.  The number of valid
982eda14cbcSMatt Macy  * labels found will be returned in num_labels when non-NULL.
983eda14cbcSMatt Macy  */
984eda14cbcSMatt Macy int
985eda14cbcSMatt Macy zpool_read_label(int fd, nvlist_t **config, int *num_labels)
986eda14cbcSMatt Macy {
987da5137abSMartin Matuska #ifndef HAVE_AIO_H
988da5137abSMartin Matuska 	return (zpool_read_label_slow(fd, config, num_labels));
989da5137abSMartin Matuska #else
990eda14cbcSMatt Macy 	struct stat64 statbuf;
991184c1b94SMartin Matuska 	struct aiocb aiocbs[VDEV_LABELS];
992184c1b94SMartin Matuska 	struct aiocb *aiocbps[VDEV_LABELS];
993184c1b94SMartin Matuska 	vdev_phys_t *labels;
994eda14cbcSMatt Macy 	nvlist_t *expected_config = NULL;
995eda14cbcSMatt Macy 	uint64_t expected_guid = 0, size;
996184c1b94SMartin Matuska 	int error, l, count = 0;
997eda14cbcSMatt Macy 
998eda14cbcSMatt Macy 	*config = NULL;
999eda14cbcSMatt Macy 
1000eda14cbcSMatt Macy 	if (fstat64_blk(fd, &statbuf) == -1)
1001eda14cbcSMatt Macy 		return (0);
1002eda14cbcSMatt Macy 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1003eda14cbcSMatt Macy 
1004184c1b94SMartin Matuska 	error = posix_memalign((void **)&labels, PAGESIZE,
1005184c1b94SMartin Matuska 	    VDEV_LABELS * sizeof (*labels));
1006eda14cbcSMatt Macy 	if (error)
1007eda14cbcSMatt Macy 		return (-1);
1008eda14cbcSMatt Macy 
1009184c1b94SMartin Matuska 	memset(aiocbs, 0, sizeof (aiocbs));
1010184c1b94SMartin Matuska 	for (l = 0; l < VDEV_LABELS; l++) {
1011184c1b94SMartin Matuska 		off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE;
1012184c1b94SMartin Matuska 
1013184c1b94SMartin Matuska 		aiocbs[l].aio_fildes = fd;
1014184c1b94SMartin Matuska 		aiocbs[l].aio_offset = offset;
1015184c1b94SMartin Matuska 		aiocbs[l].aio_buf = &labels[l];
1016184c1b94SMartin Matuska 		aiocbs[l].aio_nbytes = sizeof (vdev_phys_t);
1017184c1b94SMartin Matuska 		aiocbs[l].aio_lio_opcode = LIO_READ;
1018184c1b94SMartin Matuska 		aiocbps[l] = &aiocbs[l];
1019184c1b94SMartin Matuska 	}
1020184c1b94SMartin Matuska 
1021184c1b94SMartin Matuska 	if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) {
1022184c1b94SMartin Matuska 		int saved_errno = errno;
102316038816SMartin Matuska 		boolean_t do_slow = B_FALSE;
102416038816SMartin Matuska 		error = -1;
1025184c1b94SMartin Matuska 
1026184c1b94SMartin Matuska 		if (errno == EAGAIN || errno == EINTR || errno == EIO) {
1027184c1b94SMartin Matuska 			/*
1028184c1b94SMartin Matuska 			 * A portion of the requests may have been submitted.
1029184c1b94SMartin Matuska 			 * Clean them up.
1030184c1b94SMartin Matuska 			 */
1031184c1b94SMartin Matuska 			for (l = 0; l < VDEV_LABELS; l++) {
1032184c1b94SMartin Matuska 				errno = 0;
103316038816SMartin Matuska 				switch (aio_error(&aiocbs[l])) {
103416038816SMartin Matuska 				case EINVAL:
103516038816SMartin Matuska 					break;
103616038816SMartin Matuska 				case EINPROGRESS:
103716038816SMartin Matuska 					// This shouldn't be possible to
103816038816SMartin Matuska 					// encounter, die if we do.
103916038816SMartin Matuska 					ASSERT(B_FALSE);
1040c03c5b1cSMartin Matuska 					zfs_fallthrough;
104116038816SMartin Matuska 				case EOPNOTSUPP:
104216038816SMartin Matuska 				case ENOSYS:
104316038816SMartin Matuska 					do_slow = B_TRUE;
1044c03c5b1cSMartin Matuska 					zfs_fallthrough;
104516038816SMartin Matuska 				case 0:
104616038816SMartin Matuska 				default:
1047184c1b94SMartin Matuska 					(void) aio_return(&aiocbs[l]);
1048184c1b94SMartin Matuska 				}
1049184c1b94SMartin Matuska 			}
105016038816SMartin Matuska 		}
105116038816SMartin Matuska 		if (do_slow) {
105216038816SMartin Matuska 			/*
105316038816SMartin Matuska 			 * At least some IO involved access unsafe-for-AIO
105416038816SMartin Matuska 			 * files. Let's try again, without AIO this time.
105516038816SMartin Matuska 			 */
105616038816SMartin Matuska 			error = zpool_read_label_slow(fd, config, num_labels);
105716038816SMartin Matuska 			saved_errno = errno;
105816038816SMartin Matuska 		}
1059184c1b94SMartin Matuska 		free(labels);
1060184c1b94SMartin Matuska 		errno = saved_errno;
106116038816SMartin Matuska 		return (error);
1062184c1b94SMartin Matuska 	}
1063184c1b94SMartin Matuska 
1064eda14cbcSMatt Macy 	for (l = 0; l < VDEV_LABELS; l++) {
1065eda14cbcSMatt Macy 		uint64_t state, guid, txg;
1066eda14cbcSMatt Macy 
1067184c1b94SMartin Matuska 		if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t))
1068eda14cbcSMatt Macy 			continue;
1069eda14cbcSMatt Macy 
1070184c1b94SMartin Matuska 		if (nvlist_unpack(labels[l].vp_nvlist,
1071184c1b94SMartin Matuska 		    sizeof (labels[l].vp_nvlist), config, 0) != 0)
1072eda14cbcSMatt Macy 			continue;
1073eda14cbcSMatt Macy 
1074eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
1075eda14cbcSMatt Macy 		    &guid) != 0 || guid == 0) {
1076eda14cbcSMatt Macy 			nvlist_free(*config);
1077eda14cbcSMatt Macy 			continue;
1078eda14cbcSMatt Macy 		}
1079eda14cbcSMatt Macy 
1080eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
1081eda14cbcSMatt Macy 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
1082eda14cbcSMatt Macy 			nvlist_free(*config);
1083eda14cbcSMatt Macy 			continue;
1084eda14cbcSMatt Macy 		}
1085eda14cbcSMatt Macy 
1086eda14cbcSMatt Macy 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1087eda14cbcSMatt Macy 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
1088eda14cbcSMatt Macy 		    &txg) != 0 || txg == 0)) {
1089eda14cbcSMatt Macy 			nvlist_free(*config);
1090eda14cbcSMatt Macy 			continue;
1091eda14cbcSMatt Macy 		}
1092eda14cbcSMatt Macy 
1093eda14cbcSMatt Macy 		if (expected_guid) {
1094eda14cbcSMatt Macy 			if (expected_guid == guid)
1095eda14cbcSMatt Macy 				count++;
1096eda14cbcSMatt Macy 
1097eda14cbcSMatt Macy 			nvlist_free(*config);
1098eda14cbcSMatt Macy 		} else {
1099eda14cbcSMatt Macy 			expected_config = *config;
1100eda14cbcSMatt Macy 			expected_guid = guid;
1101eda14cbcSMatt Macy 			count++;
1102eda14cbcSMatt Macy 		}
1103eda14cbcSMatt Macy 	}
1104eda14cbcSMatt Macy 
1105eda14cbcSMatt Macy 	if (num_labels != NULL)
1106eda14cbcSMatt Macy 		*num_labels = count;
1107eda14cbcSMatt Macy 
1108184c1b94SMartin Matuska 	free(labels);
1109eda14cbcSMatt Macy 	*config = expected_config;
1110eda14cbcSMatt Macy 
1111eda14cbcSMatt Macy 	return (0);
1112da5137abSMartin Matuska #endif
1113eda14cbcSMatt Macy }
1114eda14cbcSMatt Macy 
1115eda14cbcSMatt Macy /*
1116eda14cbcSMatt Macy  * Sorted by full path and then vdev guid to allow for multiple entries with
1117eda14cbcSMatt Macy  * the same full path name.  This is required because it's possible to
1118eda14cbcSMatt Macy  * have multiple block devices with labels that refer to the same
1119eda14cbcSMatt Macy  * ZPOOL_CONFIG_PATH yet have different vdev guids.  In this case both
1120eda14cbcSMatt Macy  * entries need to be added to the cache.  Scenarios where this can occur
1121eda14cbcSMatt Macy  * include overwritten pool labels, devices which are visible from multiple
1122eda14cbcSMatt Macy  * hosts and multipath devices.
1123eda14cbcSMatt Macy  */
1124eda14cbcSMatt Macy int
1125eda14cbcSMatt Macy slice_cache_compare(const void *arg1, const void *arg2)
1126eda14cbcSMatt Macy {
1127eda14cbcSMatt Macy 	const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
1128eda14cbcSMatt Macy 	const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
1129eda14cbcSMatt Macy 	uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
1130eda14cbcSMatt Macy 	uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
1131eda14cbcSMatt Macy 	int rv;
1132eda14cbcSMatt Macy 
1133eda14cbcSMatt Macy 	rv = TREE_ISIGN(strcmp(nm1, nm2));
1134eda14cbcSMatt Macy 	if (rv)
1135eda14cbcSMatt Macy 		return (rv);
1136eda14cbcSMatt Macy 
1137eda14cbcSMatt Macy 	return (TREE_CMP(guid1, guid2));
1138eda14cbcSMatt Macy }
1139eda14cbcSMatt Macy 
1140eda14cbcSMatt Macy static int
1141eda14cbcSMatt Macy label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
1142eda14cbcSMatt Macy     uint64_t vdev_guid, char **path, char **devid)
1143eda14cbcSMatt Macy {
1144eda14cbcSMatt Macy 	nvlist_t **child;
1145eda14cbcSMatt Macy 	uint_t c, children;
1146eda14cbcSMatt Macy 	uint64_t guid;
1147eda14cbcSMatt Macy 	char *val;
1148eda14cbcSMatt Macy 	int error;
1149eda14cbcSMatt Macy 
1150eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1151eda14cbcSMatt Macy 	    &child, &children) == 0) {
1152eda14cbcSMatt Macy 		for (c = 0; c < children; c++) {
1153eda14cbcSMatt Macy 			error  = label_paths_impl(hdl, child[c],
1154eda14cbcSMatt Macy 			    pool_guid, vdev_guid, path, devid);
1155eda14cbcSMatt Macy 			if (error)
1156eda14cbcSMatt Macy 				return (error);
1157eda14cbcSMatt Macy 		}
1158eda14cbcSMatt Macy 		return (0);
1159eda14cbcSMatt Macy 	}
1160eda14cbcSMatt Macy 
1161eda14cbcSMatt Macy 	if (nvroot == NULL)
1162eda14cbcSMatt Macy 		return (0);
1163eda14cbcSMatt Macy 
1164eda14cbcSMatt Macy 	error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1165eda14cbcSMatt Macy 	if ((error != 0) || (guid != vdev_guid))
1166eda14cbcSMatt Macy 		return (0);
1167eda14cbcSMatt Macy 
1168eda14cbcSMatt Macy 	error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1169eda14cbcSMatt Macy 	if (error == 0)
1170eda14cbcSMatt Macy 		*path = val;
1171eda14cbcSMatt Macy 
1172eda14cbcSMatt Macy 	error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1173eda14cbcSMatt Macy 	if (error == 0)
1174eda14cbcSMatt Macy 		*devid = val;
1175eda14cbcSMatt Macy 
1176eda14cbcSMatt Macy 	return (0);
1177eda14cbcSMatt Macy }
1178eda14cbcSMatt Macy 
1179eda14cbcSMatt Macy /*
1180eda14cbcSMatt Macy  * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1181eda14cbcSMatt Macy  * and store these strings as config_path and devid_path respectively.
1182eda14cbcSMatt Macy  * The returned pointers are only valid as long as label remains valid.
1183eda14cbcSMatt Macy  */
1184eda14cbcSMatt Macy int
1185eda14cbcSMatt Macy label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1186eda14cbcSMatt Macy {
1187eda14cbcSMatt Macy 	nvlist_t *nvroot;
1188eda14cbcSMatt Macy 	uint64_t pool_guid;
1189eda14cbcSMatt Macy 	uint64_t vdev_guid;
1190eda14cbcSMatt Macy 
1191eda14cbcSMatt Macy 	*path = NULL;
1192eda14cbcSMatt Macy 	*devid = NULL;
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1195eda14cbcSMatt Macy 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1196eda14cbcSMatt Macy 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1197eda14cbcSMatt Macy 		return (ENOENT);
1198eda14cbcSMatt Macy 
1199eda14cbcSMatt Macy 	return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1200eda14cbcSMatt Macy 	    devid));
1201eda14cbcSMatt Macy }
1202eda14cbcSMatt Macy 
1203eda14cbcSMatt Macy static void
1204eda14cbcSMatt Macy zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock,
1205eda14cbcSMatt Macy     avl_tree_t *cache, const char *path, const char *name, int order)
1206eda14cbcSMatt Macy {
1207eda14cbcSMatt Macy 	avl_index_t where;
1208eda14cbcSMatt Macy 	rdsk_node_t *slice;
1209eda14cbcSMatt Macy 
1210eda14cbcSMatt Macy 	slice = zutil_alloc(hdl, sizeof (rdsk_node_t));
1211eda14cbcSMatt Macy 	if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) {
1212eda14cbcSMatt Macy 		free(slice);
1213eda14cbcSMatt Macy 		return;
1214eda14cbcSMatt Macy 	}
1215eda14cbcSMatt Macy 	slice->rn_vdev_guid = 0;
1216eda14cbcSMatt Macy 	slice->rn_lock = lock;
1217eda14cbcSMatt Macy 	slice->rn_avl = cache;
1218eda14cbcSMatt Macy 	slice->rn_hdl = hdl;
1219eda14cbcSMatt Macy 	slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET;
1220eda14cbcSMatt Macy 	slice->rn_labelpaths = B_FALSE;
1221eda14cbcSMatt Macy 
1222eda14cbcSMatt Macy 	pthread_mutex_lock(lock);
1223eda14cbcSMatt Macy 	if (avl_find(cache, slice, &where)) {
1224eda14cbcSMatt Macy 		free(slice->rn_name);
1225eda14cbcSMatt Macy 		free(slice);
1226eda14cbcSMatt Macy 	} else {
1227eda14cbcSMatt Macy 		avl_insert(cache, slice, where);
1228eda14cbcSMatt Macy 	}
1229eda14cbcSMatt Macy 	pthread_mutex_unlock(lock);
1230eda14cbcSMatt Macy }
1231eda14cbcSMatt Macy 
1232eda14cbcSMatt Macy static int
1233eda14cbcSMatt Macy zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock,
1234eda14cbcSMatt Macy     avl_tree_t *cache, const char *dir, int order)
1235eda14cbcSMatt Macy {
1236eda14cbcSMatt Macy 	int error;
1237eda14cbcSMatt Macy 	char path[MAXPATHLEN];
1238eda14cbcSMatt Macy 	struct dirent64 *dp;
1239eda14cbcSMatt Macy 	DIR *dirp;
1240eda14cbcSMatt Macy 
1241eda14cbcSMatt Macy 	if (realpath(dir, path) == NULL) {
1242eda14cbcSMatt Macy 		error = errno;
1243eda14cbcSMatt Macy 		if (error == ENOENT)
1244eda14cbcSMatt Macy 			return (0);
1245eda14cbcSMatt Macy 
12461f88aa09SMartin Matuska 		zutil_error_aux(hdl, "%s", strerror(error));
1247eda14cbcSMatt Macy 		(void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1248eda14cbcSMatt Macy 		    TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1249eda14cbcSMatt Macy 		return (error);
1250eda14cbcSMatt Macy 	}
1251eda14cbcSMatt Macy 
1252eda14cbcSMatt Macy 	dirp = opendir(path);
1253eda14cbcSMatt Macy 	if (dirp == NULL) {
1254eda14cbcSMatt Macy 		error = errno;
12551f88aa09SMartin Matuska 		zutil_error_aux(hdl, "%s", strerror(error));
1256eda14cbcSMatt Macy 		(void) zutil_error_fmt(hdl, EZFS_BADPATH,
1257eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
1258eda14cbcSMatt Macy 		return (error);
1259eda14cbcSMatt Macy 	}
1260eda14cbcSMatt Macy 
1261eda14cbcSMatt Macy 	while ((dp = readdir64(dirp)) != NULL) {
1262eda14cbcSMatt Macy 		const char *name = dp->d_name;
12633ff01b23SMartin Matuska 		if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
1264eda14cbcSMatt Macy 			continue;
1265eda14cbcSMatt Macy 
12663ff01b23SMartin Matuska 		switch (dp->d_type) {
12673ff01b23SMartin Matuska 		case DT_UNKNOWN:
12683ff01b23SMartin Matuska 		case DT_BLK:
12690d8fe237SMartin Matuska 		case DT_LNK:
12703ff01b23SMartin Matuska #ifdef __FreeBSD__
12713ff01b23SMartin Matuska 		case DT_CHR:
12723ff01b23SMartin Matuska #endif
12733ff01b23SMartin Matuska 		case DT_REG:
12743ff01b23SMartin Matuska 			break;
12753ff01b23SMartin Matuska 		default:
12763ff01b23SMartin Matuska 			continue;
12773ff01b23SMartin Matuska 		}
12783ff01b23SMartin Matuska 
1279eda14cbcSMatt Macy 		zpool_find_import_scan_add_slice(hdl, lock, cache, path, name,
1280eda14cbcSMatt Macy 		    order);
1281eda14cbcSMatt Macy 	}
1282eda14cbcSMatt Macy 
1283eda14cbcSMatt Macy 	(void) closedir(dirp);
1284eda14cbcSMatt Macy 	return (0);
1285eda14cbcSMatt Macy }
1286eda14cbcSMatt Macy 
1287eda14cbcSMatt Macy static int
1288eda14cbcSMatt Macy zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
1289eda14cbcSMatt Macy     avl_tree_t *cache, const char *dir, int order)
1290eda14cbcSMatt Macy {
1291eda14cbcSMatt Macy 	int error = 0;
1292eda14cbcSMatt Macy 	char path[MAXPATHLEN];
12933ff01b23SMartin Matuska 	char *d = NULL;
12943ff01b23SMartin Matuska 	ssize_t dl;
12953ff01b23SMartin Matuska 	const char *dpath, *name;
1296eda14cbcSMatt Macy 
1297eda14cbcSMatt Macy 	/*
12983ff01b23SMartin Matuska 	 * Separate the directory and the basename.
12993ff01b23SMartin Matuska 	 * We do this so that we can get the realpath of
1300eda14cbcSMatt Macy 	 * the directory. We don't get the realpath on the
1301eda14cbcSMatt Macy 	 * whole path because if it's a symlink, we want the
1302eda14cbcSMatt Macy 	 * path of the symlink not where it points to.
1303eda14cbcSMatt Macy 	 */
13043ff01b23SMartin Matuska 	name = zfs_basename(dir);
13053ff01b23SMartin Matuska 	if ((dl = zfs_dirnamelen(dir)) == -1)
13063ff01b23SMartin Matuska 		dpath = ".";
13073ff01b23SMartin Matuska 	else
13083ff01b23SMartin Matuska 		dpath = d = zutil_strndup(hdl, dir, dl);
1309eda14cbcSMatt Macy 
1310eda14cbcSMatt Macy 	if (realpath(dpath, path) == NULL) {
1311eda14cbcSMatt Macy 		error = errno;
1312eda14cbcSMatt Macy 		if (error == ENOENT) {
1313eda14cbcSMatt Macy 			error = 0;
1314eda14cbcSMatt Macy 			goto out;
1315eda14cbcSMatt Macy 		}
1316eda14cbcSMatt Macy 
13171f88aa09SMartin Matuska 		zutil_error_aux(hdl, "%s", strerror(error));
1318eda14cbcSMatt Macy 		(void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1319eda14cbcSMatt Macy 		    TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1320eda14cbcSMatt Macy 		goto out;
1321eda14cbcSMatt Macy 	}
1322eda14cbcSMatt Macy 
1323eda14cbcSMatt Macy 	zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
1324eda14cbcSMatt Macy 
1325eda14cbcSMatt Macy out:
1326eda14cbcSMatt Macy 	free(d);
1327eda14cbcSMatt Macy 	return (error);
1328eda14cbcSMatt Macy }
1329eda14cbcSMatt Macy 
1330eda14cbcSMatt Macy /*
1331eda14cbcSMatt Macy  * Scan a list of directories for zfs devices.
1332eda14cbcSMatt Macy  */
1333eda14cbcSMatt Macy static int
1334eda14cbcSMatt Macy zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock,
1335eda14cbcSMatt Macy     avl_tree_t **slice_cache, const char * const *dir, size_t dirs)
1336eda14cbcSMatt Macy {
1337eda14cbcSMatt Macy 	avl_tree_t *cache;
1338eda14cbcSMatt Macy 	rdsk_node_t *slice;
1339eda14cbcSMatt Macy 	void *cookie;
1340eda14cbcSMatt Macy 	int i, error;
1341eda14cbcSMatt Macy 
1342eda14cbcSMatt Macy 	*slice_cache = NULL;
1343eda14cbcSMatt Macy 	cache = zutil_alloc(hdl, sizeof (avl_tree_t));
1344eda14cbcSMatt Macy 	avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1345eda14cbcSMatt Macy 	    offsetof(rdsk_node_t, rn_node));
1346eda14cbcSMatt Macy 
1347eda14cbcSMatt Macy 	for (i = 0; i < dirs; i++) {
1348eda14cbcSMatt Macy 		struct stat sbuf;
1349eda14cbcSMatt Macy 
1350eda14cbcSMatt Macy 		if (stat(dir[i], &sbuf) != 0) {
1351eda14cbcSMatt Macy 			error = errno;
1352eda14cbcSMatt Macy 			if (error == ENOENT)
1353eda14cbcSMatt Macy 				continue;
1354eda14cbcSMatt Macy 
13551f88aa09SMartin Matuska 			zutil_error_aux(hdl, "%s", strerror(error));
1356eda14cbcSMatt Macy 			(void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1357eda14cbcSMatt Macy 			    TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1358eda14cbcSMatt Macy 			goto error;
1359eda14cbcSMatt Macy 		}
1360eda14cbcSMatt Macy 
1361eda14cbcSMatt Macy 		/*
1362eda14cbcSMatt Macy 		 * If dir[i] is a directory, we walk through it and add all
1363eda14cbcSMatt Macy 		 * the entries to the cache. If it's not a directory, we just
1364eda14cbcSMatt Macy 		 * add it to the cache.
1365eda14cbcSMatt Macy 		 */
1366eda14cbcSMatt Macy 		if (S_ISDIR(sbuf.st_mode)) {
1367eda14cbcSMatt Macy 			if ((error = zpool_find_import_scan_dir(hdl, lock,
1368eda14cbcSMatt Macy 			    cache, dir[i], i)) != 0)
1369eda14cbcSMatt Macy 				goto error;
1370eda14cbcSMatt Macy 		} else {
1371eda14cbcSMatt Macy 			if ((error = zpool_find_import_scan_path(hdl, lock,
1372eda14cbcSMatt Macy 			    cache, dir[i], i)) != 0)
1373eda14cbcSMatt Macy 				goto error;
1374eda14cbcSMatt Macy 		}
1375eda14cbcSMatt Macy 	}
1376eda14cbcSMatt Macy 
1377eda14cbcSMatt Macy 	*slice_cache = cache;
1378eda14cbcSMatt Macy 	return (0);
1379eda14cbcSMatt Macy 
1380eda14cbcSMatt Macy error:
1381eda14cbcSMatt Macy 	cookie = NULL;
1382eda14cbcSMatt Macy 	while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1383eda14cbcSMatt Macy 		free(slice->rn_name);
1384eda14cbcSMatt Macy 		free(slice);
1385eda14cbcSMatt Macy 	}
1386eda14cbcSMatt Macy 	free(cache);
1387eda14cbcSMatt Macy 
1388eda14cbcSMatt Macy 	return (error);
1389eda14cbcSMatt Macy }
1390eda14cbcSMatt Macy 
1391eda14cbcSMatt Macy /*
1392eda14cbcSMatt Macy  * Given a list of directories to search, find all pools stored on disk.  This
1393eda14cbcSMatt Macy  * includes partial pools which are not available to import.  If no args are
1394eda14cbcSMatt Macy  * given (argc is 0), then the default directory (/dev/dsk) is searched.
1395eda14cbcSMatt Macy  * poolname or guid (but not both) are provided by the caller when trying
1396eda14cbcSMatt Macy  * to import a specific pool.
1397eda14cbcSMatt Macy  */
1398eda14cbcSMatt Macy static nvlist_t *
13999db44a8eSMartin Matuska zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg,
14009db44a8eSMartin Matuska     pthread_mutex_t *lock, avl_tree_t *cache)
1401eda14cbcSMatt Macy {
1402e92ffd9bSMartin Matuska 	(void) lock;
1403eda14cbcSMatt Macy 	nvlist_t *ret = NULL;
1404eda14cbcSMatt Macy 	pool_list_t pools = { 0 };
1405eda14cbcSMatt Macy 	pool_entry_t *pe, *penext;
1406eda14cbcSMatt Macy 	vdev_entry_t *ve, *venext;
1407eda14cbcSMatt Macy 	config_entry_t *ce, *cenext;
1408eda14cbcSMatt Macy 	name_entry_t *ne, *nenext;
1409eda14cbcSMatt Macy 	rdsk_node_t *slice;
1410eda14cbcSMatt Macy 	void *cookie;
1411eda14cbcSMatt Macy 	tpool_t *t;
1412eda14cbcSMatt Macy 
1413eda14cbcSMatt Macy 	verify(iarg->poolname == NULL || iarg->guid == 0);
1414eda14cbcSMatt Macy 
1415eda14cbcSMatt Macy 	/*
1416eda14cbcSMatt Macy 	 * Create a thread pool to parallelize the process of reading and
1417eda14cbcSMatt Macy 	 * validating labels, a large number of threads can be used due to
1418eda14cbcSMatt Macy 	 * minimal contention.
1419eda14cbcSMatt Macy 	 */
1420eda14cbcSMatt Macy 	t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
1421eda14cbcSMatt Macy 	for (slice = avl_first(cache); slice;
1422eda14cbcSMatt Macy 	    (slice = avl_walk(cache, slice, AVL_AFTER)))
1423eda14cbcSMatt Macy 		(void) tpool_dispatch(t, zpool_open_func, slice);
1424eda14cbcSMatt Macy 
1425eda14cbcSMatt Macy 	tpool_wait(t);
1426eda14cbcSMatt Macy 	tpool_destroy(t);
1427eda14cbcSMatt Macy 
1428eda14cbcSMatt Macy 	/*
1429eda14cbcSMatt Macy 	 * Process the cache, filtering out any entries which are not
1430eda14cbcSMatt Macy 	 * for the specified pool then adding matching label configs.
1431eda14cbcSMatt Macy 	 */
1432eda14cbcSMatt Macy 	cookie = NULL;
1433eda14cbcSMatt Macy 	while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1434eda14cbcSMatt Macy 		if (slice->rn_config != NULL) {
1435eda14cbcSMatt Macy 			nvlist_t *config = slice->rn_config;
1436eda14cbcSMatt Macy 			boolean_t matched = B_TRUE;
1437eda14cbcSMatt Macy 			boolean_t aux = B_FALSE;
1438eda14cbcSMatt Macy 			int fd;
1439eda14cbcSMatt Macy 
1440eda14cbcSMatt Macy 			/*
1441eda14cbcSMatt Macy 			 * Check if it's a spare or l2cache device. If it is,
1442eda14cbcSMatt Macy 			 * we need to skip the name and guid check since they
1443eda14cbcSMatt Macy 			 * don't exist on aux device label.
1444eda14cbcSMatt Macy 			 */
1445eda14cbcSMatt Macy 			if (iarg->poolname != NULL || iarg->guid != 0) {
1446eda14cbcSMatt Macy 				uint64_t state;
1447eda14cbcSMatt Macy 				aux = nvlist_lookup_uint64(config,
1448eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1449eda14cbcSMatt Macy 				    (state == POOL_STATE_SPARE ||
1450eda14cbcSMatt Macy 				    state == POOL_STATE_L2CACHE);
1451eda14cbcSMatt Macy 			}
1452eda14cbcSMatt Macy 
1453eda14cbcSMatt Macy 			if (iarg->poolname != NULL && !aux) {
1454eda14cbcSMatt Macy 				char *pname;
1455eda14cbcSMatt Macy 
1456eda14cbcSMatt Macy 				matched = nvlist_lookup_string(config,
1457eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1458eda14cbcSMatt Macy 				    strcmp(iarg->poolname, pname) == 0;
1459eda14cbcSMatt Macy 			} else if (iarg->guid != 0 && !aux) {
1460eda14cbcSMatt Macy 				uint64_t this_guid;
1461eda14cbcSMatt Macy 
1462eda14cbcSMatt Macy 				matched = nvlist_lookup_uint64(config,
1463eda14cbcSMatt Macy 				    ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1464eda14cbcSMatt Macy 				    iarg->guid == this_guid;
1465eda14cbcSMatt Macy 			}
1466eda14cbcSMatt Macy 			if (matched) {
1467eda14cbcSMatt Macy 				/*
1468eda14cbcSMatt Macy 				 * Verify all remaining entries can be opened
1469eda14cbcSMatt Macy 				 * exclusively. This will prune all underlying
1470eda14cbcSMatt Macy 				 * multipath devices which otherwise could
1471eda14cbcSMatt Macy 				 * result in the vdev appearing as UNAVAIL.
1472eda14cbcSMatt Macy 				 *
1473eda14cbcSMatt Macy 				 * Under zdb, this step isn't required and
1474eda14cbcSMatt Macy 				 * would prevent a zdb -e of active pools with
1475eda14cbcSMatt Macy 				 * no cachefile.
1476eda14cbcSMatt Macy 				 */
147716038816SMartin Matuska 				fd = open(slice->rn_name,
147816038816SMartin Matuska 				    O_RDONLY | O_EXCL | O_CLOEXEC);
1479eda14cbcSMatt Macy 				if (fd >= 0 || iarg->can_be_active) {
1480eda14cbcSMatt Macy 					if (fd >= 0)
1481eda14cbcSMatt Macy 						close(fd);
1482eda14cbcSMatt Macy 					add_config(hdl, &pools,
1483eda14cbcSMatt Macy 					    slice->rn_name, slice->rn_order,
1484eda14cbcSMatt Macy 					    slice->rn_num_labels, config);
1485eda14cbcSMatt Macy 				}
1486eda14cbcSMatt Macy 			}
1487eda14cbcSMatt Macy 			nvlist_free(config);
1488eda14cbcSMatt Macy 		}
1489eda14cbcSMatt Macy 		free(slice->rn_name);
1490eda14cbcSMatt Macy 		free(slice);
1491eda14cbcSMatt Macy 	}
1492eda14cbcSMatt Macy 	avl_destroy(cache);
1493eda14cbcSMatt Macy 	free(cache);
1494eda14cbcSMatt Macy 
1495eda14cbcSMatt Macy 	ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy);
1496eda14cbcSMatt Macy 
1497eda14cbcSMatt Macy 	for (pe = pools.pools; pe != NULL; pe = penext) {
1498eda14cbcSMatt Macy 		penext = pe->pe_next;
1499eda14cbcSMatt Macy 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1500eda14cbcSMatt Macy 			venext = ve->ve_next;
1501eda14cbcSMatt Macy 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1502eda14cbcSMatt Macy 				cenext = ce->ce_next;
1503eda14cbcSMatt Macy 				nvlist_free(ce->ce_config);
1504eda14cbcSMatt Macy 				free(ce);
1505eda14cbcSMatt Macy 			}
1506eda14cbcSMatt Macy 			free(ve);
1507eda14cbcSMatt Macy 		}
1508eda14cbcSMatt Macy 		free(pe);
1509eda14cbcSMatt Macy 	}
1510eda14cbcSMatt Macy 
1511eda14cbcSMatt Macy 	for (ne = pools.names; ne != NULL; ne = nenext) {
1512eda14cbcSMatt Macy 		nenext = ne->ne_next;
1513eda14cbcSMatt Macy 		free(ne->ne_name);
1514eda14cbcSMatt Macy 		free(ne);
1515eda14cbcSMatt Macy 	}
1516eda14cbcSMatt Macy 
1517eda14cbcSMatt Macy 	return (ret);
1518eda14cbcSMatt Macy }
1519eda14cbcSMatt Macy 
1520eda14cbcSMatt Macy /*
15219db44a8eSMartin Matuska  * Given a config, discover the paths for the devices which
15229db44a8eSMartin Matuska  * exist in the config.
15239db44a8eSMartin Matuska  */
15249db44a8eSMartin Matuska static int
15259db44a8eSMartin Matuska discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv,
15269db44a8eSMartin Matuska     avl_tree_t *cache, pthread_mutex_t *lock)
15279db44a8eSMartin Matuska {
15289db44a8eSMartin Matuska 	char *path = NULL;
15293ff01b23SMartin Matuska 	ssize_t dl;
15309db44a8eSMartin Matuska 	uint_t children;
15319db44a8eSMartin Matuska 	nvlist_t **child;
15329db44a8eSMartin Matuska 
15339db44a8eSMartin Matuska 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
15349db44a8eSMartin Matuska 	    &child, &children) == 0) {
15359db44a8eSMartin Matuska 		for (int c = 0; c < children; c++) {
15369db44a8eSMartin Matuska 			discover_cached_paths(hdl, child[c], cache, lock);
15379db44a8eSMartin Matuska 		}
15389db44a8eSMartin Matuska 	}
15399db44a8eSMartin Matuska 
15409db44a8eSMartin Matuska 	/*
15419db44a8eSMartin Matuska 	 * Once we have the path, we need to add the directory to
154216038816SMartin Matuska 	 * our directory cache.
15439db44a8eSMartin Matuska 	 */
15449db44a8eSMartin Matuska 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
15453ff01b23SMartin Matuska 		if ((dl = zfs_dirnamelen(path)) == -1)
1546a0b956f5SMartin Matuska 			path = (char *)".";
15473ff01b23SMartin Matuska 		else
15483ff01b23SMartin Matuska 			path[dl] = '\0';
15499db44a8eSMartin Matuska 		return (zpool_find_import_scan_dir(hdl, lock, cache,
15503ff01b23SMartin Matuska 		    path, 0));
15519db44a8eSMartin Matuska 	}
15529db44a8eSMartin Matuska 	return (0);
15539db44a8eSMartin Matuska }
15549db44a8eSMartin Matuska 
15559db44a8eSMartin Matuska /*
1556eda14cbcSMatt Macy  * Given a cache file, return the contents as a list of importable pools.
1557eda14cbcSMatt Macy  * poolname or guid (but not both) are provided by the caller when trying
1558eda14cbcSMatt Macy  * to import a specific pool.
1559eda14cbcSMatt Macy  */
1560eda14cbcSMatt Macy static nvlist_t *
15619db44a8eSMartin Matuska zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg)
1562eda14cbcSMatt Macy {
1563eda14cbcSMatt Macy 	char *buf;
1564eda14cbcSMatt Macy 	int fd;
1565eda14cbcSMatt Macy 	struct stat64 statbuf;
1566eda14cbcSMatt Macy 	nvlist_t *raw, *src, *dst;
1567eda14cbcSMatt Macy 	nvlist_t *pools;
1568eda14cbcSMatt Macy 	nvpair_t *elem;
1569eda14cbcSMatt Macy 	char *name;
1570eda14cbcSMatt Macy 	uint64_t this_guid;
1571eda14cbcSMatt Macy 	boolean_t active;
1572eda14cbcSMatt Macy 
15739db44a8eSMartin Matuska 	verify(iarg->poolname == NULL || iarg->guid == 0);
1574eda14cbcSMatt Macy 
157516038816SMartin Matuska 	if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) {
1576eda14cbcSMatt Macy 		zutil_error_aux(hdl, "%s", strerror(errno));
1577eda14cbcSMatt Macy 		(void) zutil_error(hdl, EZFS_BADCACHE,
1578eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "failed to open cache file"));
1579eda14cbcSMatt Macy 		return (NULL);
1580eda14cbcSMatt Macy 	}
1581eda14cbcSMatt Macy 
1582eda14cbcSMatt Macy 	if (fstat64(fd, &statbuf) != 0) {
1583eda14cbcSMatt Macy 		zutil_error_aux(hdl, "%s", strerror(errno));
1584eda14cbcSMatt Macy 		(void) close(fd);
1585eda14cbcSMatt Macy 		(void) zutil_error(hdl, EZFS_BADCACHE,
1586eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
1587eda14cbcSMatt Macy 		return (NULL);
1588eda14cbcSMatt Macy 	}
1589eda14cbcSMatt Macy 
1590eda14cbcSMatt Macy 	if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) {
1591eda14cbcSMatt Macy 		(void) close(fd);
1592eda14cbcSMatt Macy 		return (NULL);
1593eda14cbcSMatt Macy 	}
1594eda14cbcSMatt Macy 
1595eda14cbcSMatt Macy 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1596eda14cbcSMatt Macy 		(void) close(fd);
1597eda14cbcSMatt Macy 		free(buf);
1598eda14cbcSMatt Macy 		(void) zutil_error(hdl, EZFS_BADCACHE,
1599eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN,
1600eda14cbcSMatt Macy 		    "failed to read cache file contents"));
1601eda14cbcSMatt Macy 		return (NULL);
1602eda14cbcSMatt Macy 	}
1603eda14cbcSMatt Macy 
1604eda14cbcSMatt Macy 	(void) close(fd);
1605eda14cbcSMatt Macy 
1606eda14cbcSMatt Macy 	if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1607eda14cbcSMatt Macy 		free(buf);
1608eda14cbcSMatt Macy 		(void) zutil_error(hdl, EZFS_BADCACHE,
1609eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN,
1610eda14cbcSMatt Macy 		    "invalid or corrupt cache file contents"));
1611eda14cbcSMatt Macy 		return (NULL);
1612eda14cbcSMatt Macy 	}
1613eda14cbcSMatt Macy 
1614eda14cbcSMatt Macy 	free(buf);
1615eda14cbcSMatt Macy 
1616eda14cbcSMatt Macy 	/*
1617eda14cbcSMatt Macy 	 * Go through and get the current state of the pools and refresh their
1618eda14cbcSMatt Macy 	 * state.
1619eda14cbcSMatt Macy 	 */
1620eda14cbcSMatt Macy 	if (nvlist_alloc(&pools, 0, 0) != 0) {
1621eda14cbcSMatt Macy 		(void) zutil_no_memory(hdl);
1622eda14cbcSMatt Macy 		nvlist_free(raw);
1623eda14cbcSMatt Macy 		return (NULL);
1624eda14cbcSMatt Macy 	}
1625eda14cbcSMatt Macy 
1626eda14cbcSMatt Macy 	elem = NULL;
1627eda14cbcSMatt Macy 	while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1628eda14cbcSMatt Macy 		src = fnvpair_value_nvlist(elem);
1629eda14cbcSMatt Macy 
1630eda14cbcSMatt Macy 		name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
16319db44a8eSMartin Matuska 		if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0)
1632eda14cbcSMatt Macy 			continue;
1633eda14cbcSMatt Macy 
1634eda14cbcSMatt Macy 		this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
16359db44a8eSMartin Matuska 		if (iarg->guid != 0 && iarg->guid != this_guid)
1636eda14cbcSMatt Macy 			continue;
1637eda14cbcSMatt Macy 
1638eda14cbcSMatt Macy 		if (zutil_pool_active(hdl, name, this_guid, &active) != 0) {
1639eda14cbcSMatt Macy 			nvlist_free(raw);
1640eda14cbcSMatt Macy 			nvlist_free(pools);
1641eda14cbcSMatt Macy 			return (NULL);
1642eda14cbcSMatt Macy 		}
1643eda14cbcSMatt Macy 
1644eda14cbcSMatt Macy 		if (active)
1645eda14cbcSMatt Macy 			continue;
1646eda14cbcSMatt Macy 
16479db44a8eSMartin Matuska 		if (iarg->scan) {
16489db44a8eSMartin Matuska 			uint64_t saved_guid = iarg->guid;
16499db44a8eSMartin Matuska 			const char *saved_poolname = iarg->poolname;
16509db44a8eSMartin Matuska 			pthread_mutex_t lock;
16519db44a8eSMartin Matuska 
16529db44a8eSMartin Matuska 			/*
16539db44a8eSMartin Matuska 			 * Create the device cache that will hold the
16549db44a8eSMartin Matuska 			 * devices we will scan based on the cachefile.
16559db44a8eSMartin Matuska 			 * This will get destroyed and freed by
16569db44a8eSMartin Matuska 			 * zpool_find_import_impl.
16579db44a8eSMartin Matuska 			 */
16589db44a8eSMartin Matuska 			avl_tree_t *cache = zutil_alloc(hdl,
16599db44a8eSMartin Matuska 			    sizeof (avl_tree_t));
16609db44a8eSMartin Matuska 			avl_create(cache, slice_cache_compare,
16619db44a8eSMartin Matuska 			    sizeof (rdsk_node_t),
16629db44a8eSMartin Matuska 			    offsetof(rdsk_node_t, rn_node));
16639db44a8eSMartin Matuska 			nvlist_t *nvroot = fnvlist_lookup_nvlist(src,
16649db44a8eSMartin Matuska 			    ZPOOL_CONFIG_VDEV_TREE);
16659db44a8eSMartin Matuska 
16669db44a8eSMartin Matuska 			/*
16679db44a8eSMartin Matuska 			 * We only want to find the pool with this_guid.
16689db44a8eSMartin Matuska 			 * We will reset these values back later.
16699db44a8eSMartin Matuska 			 */
16709db44a8eSMartin Matuska 			iarg->guid = this_guid;
16719db44a8eSMartin Matuska 			iarg->poolname = NULL;
16729db44a8eSMartin Matuska 
16739db44a8eSMartin Matuska 			/*
16749db44a8eSMartin Matuska 			 * We need to build up a cache of devices that exists
16759db44a8eSMartin Matuska 			 * in the paths pointed to by the cachefile. This allows
16769db44a8eSMartin Matuska 			 * us to preserve the device namespace that was
16779db44a8eSMartin Matuska 			 * originally specified by the user but also lets us
16789db44a8eSMartin Matuska 			 * scan devices in those directories in case they had
16799db44a8eSMartin Matuska 			 * been renamed.
16809db44a8eSMartin Matuska 			 */
16819db44a8eSMartin Matuska 			pthread_mutex_init(&lock, NULL);
16829db44a8eSMartin Matuska 			discover_cached_paths(hdl, nvroot, cache, &lock);
16839db44a8eSMartin Matuska 			nvlist_t *nv = zpool_find_import_impl(hdl, iarg,
16849db44a8eSMartin Matuska 			    &lock, cache);
16859db44a8eSMartin Matuska 			pthread_mutex_destroy(&lock);
16869db44a8eSMartin Matuska 
16879db44a8eSMartin Matuska 			/*
16889db44a8eSMartin Matuska 			 * zpool_find_import_impl will return back
16899db44a8eSMartin Matuska 			 * a list of pools that it found based on the
16909db44a8eSMartin Matuska 			 * device cache. There should only be one pool
16919db44a8eSMartin Matuska 			 * since we're looking for a specific guid.
16929db44a8eSMartin Matuska 			 * We will use that pool to build up the final
16939db44a8eSMartin Matuska 			 * pool nvlist which is returned back to the
16949db44a8eSMartin Matuska 			 * caller.
16959db44a8eSMartin Matuska 			 */
16969db44a8eSMartin Matuska 			nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
1697*c7046f76SMartin Matuska 			if (pair == NULL)
1698*c7046f76SMartin Matuska 				continue;
16999db44a8eSMartin Matuska 			fnvlist_add_nvlist(pools, nvpair_name(pair),
17009db44a8eSMartin Matuska 			    fnvpair_value_nvlist(pair));
17019db44a8eSMartin Matuska 
17029db44a8eSMartin Matuska 			VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL);
17039db44a8eSMartin Matuska 
17049db44a8eSMartin Matuska 			iarg->guid = saved_guid;
17059db44a8eSMartin Matuska 			iarg->poolname = saved_poolname;
17069db44a8eSMartin Matuska 			continue;
17079db44a8eSMartin Matuska 		}
17089db44a8eSMartin Matuska 
1709eda14cbcSMatt Macy 		if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE,
17109db44a8eSMartin Matuska 		    iarg->cachefile) != 0) {
1711eda14cbcSMatt Macy 			(void) zutil_no_memory(hdl);
1712eda14cbcSMatt Macy 			nvlist_free(raw);
1713eda14cbcSMatt Macy 			nvlist_free(pools);
1714eda14cbcSMatt Macy 			return (NULL);
1715eda14cbcSMatt Macy 		}
1716eda14cbcSMatt Macy 
17176ba2210eSMartin Matuska 		update_vdevs_config_dev_sysfs_path(src);
17186ba2210eSMartin Matuska 
1719eda14cbcSMatt Macy 		if ((dst = zutil_refresh_config(hdl, src)) == NULL) {
1720eda14cbcSMatt Macy 			nvlist_free(raw);
1721eda14cbcSMatt Macy 			nvlist_free(pools);
1722eda14cbcSMatt Macy 			return (NULL);
1723eda14cbcSMatt Macy 		}
1724eda14cbcSMatt Macy 
1725eda14cbcSMatt Macy 		if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1726eda14cbcSMatt Macy 			(void) zutil_no_memory(hdl);
1727eda14cbcSMatt Macy 			nvlist_free(dst);
1728eda14cbcSMatt Macy 			nvlist_free(raw);
1729eda14cbcSMatt Macy 			nvlist_free(pools);
1730eda14cbcSMatt Macy 			return (NULL);
1731eda14cbcSMatt Macy 		}
1732eda14cbcSMatt Macy 		nvlist_free(dst);
1733eda14cbcSMatt Macy 	}
1734eda14cbcSMatt Macy 	nvlist_free(raw);
1735eda14cbcSMatt Macy 	return (pools);
1736eda14cbcSMatt Macy }
1737eda14cbcSMatt Macy 
17389db44a8eSMartin Matuska static nvlist_t *
17399db44a8eSMartin Matuska zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg)
17409db44a8eSMartin Matuska {
17419db44a8eSMartin Matuska 	pthread_mutex_t lock;
17429db44a8eSMartin Matuska 	avl_tree_t *cache;
17439db44a8eSMartin Matuska 	nvlist_t *pools = NULL;
17449db44a8eSMartin Matuska 
17459db44a8eSMartin Matuska 	verify(iarg->poolname == NULL || iarg->guid == 0);
17469db44a8eSMartin Matuska 	pthread_mutex_init(&lock, NULL);
17479db44a8eSMartin Matuska 
17489db44a8eSMartin Matuska 	/*
17499db44a8eSMartin Matuska 	 * Locate pool member vdevs by blkid or by directory scanning.
17509db44a8eSMartin Matuska 	 * On success a newly allocated AVL tree which is populated with an
17519db44a8eSMartin Matuska 	 * entry for each discovered vdev will be returned in the cache.
17529db44a8eSMartin Matuska 	 * It's the caller's responsibility to consume and destroy this tree.
17539db44a8eSMartin Matuska 	 */
17549db44a8eSMartin Matuska 	if (iarg->scan || iarg->paths != 0) {
17559db44a8eSMartin Matuska 		size_t dirs = iarg->paths;
17569db44a8eSMartin Matuska 		const char * const *dir = (const char * const *)iarg->path;
17579db44a8eSMartin Matuska 
17589db44a8eSMartin Matuska 		if (dirs == 0)
17599db44a8eSMartin Matuska 			dir = zpool_default_search_paths(&dirs);
17609db44a8eSMartin Matuska 
17619db44a8eSMartin Matuska 		if (zpool_find_import_scan(hdl, &lock, &cache,
17629db44a8eSMartin Matuska 		    dir, dirs) != 0) {
17639db44a8eSMartin Matuska 			pthread_mutex_destroy(&lock);
17649db44a8eSMartin Matuska 			return (NULL);
17659db44a8eSMartin Matuska 		}
17669db44a8eSMartin Matuska 	} else {
17679db44a8eSMartin Matuska 		if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) {
17689db44a8eSMartin Matuska 			pthread_mutex_destroy(&lock);
17699db44a8eSMartin Matuska 			return (NULL);
17709db44a8eSMartin Matuska 		}
17719db44a8eSMartin Matuska 	}
17729db44a8eSMartin Matuska 
17739db44a8eSMartin Matuska 	pools = zpool_find_import_impl(hdl, iarg, &lock, cache);
17749db44a8eSMartin Matuska 	pthread_mutex_destroy(&lock);
17759db44a8eSMartin Matuska 	return (pools);
17769db44a8eSMartin Matuska }
17779db44a8eSMartin Matuska 
17789db44a8eSMartin Matuska 
1779eda14cbcSMatt Macy nvlist_t *
1780eda14cbcSMatt Macy zpool_search_import(void *hdl, importargs_t *import,
1781eda14cbcSMatt Macy     const pool_config_ops_t *pco)
1782eda14cbcSMatt Macy {
1783eda14cbcSMatt Macy 	libpc_handle_t handle = { 0 };
1784eda14cbcSMatt Macy 	nvlist_t *pools = NULL;
1785eda14cbcSMatt Macy 
1786eda14cbcSMatt Macy 	handle.lpc_lib_handle = hdl;
1787eda14cbcSMatt Macy 	handle.lpc_ops = pco;
1788eda14cbcSMatt Macy 	handle.lpc_printerr = B_TRUE;
1789eda14cbcSMatt Macy 
1790eda14cbcSMatt Macy 	verify(import->poolname == NULL || import->guid == 0);
1791eda14cbcSMatt Macy 
1792eda14cbcSMatt Macy 	if (import->cachefile != NULL)
17939db44a8eSMartin Matuska 		pools = zpool_find_import_cached(&handle, import);
1794eda14cbcSMatt Macy 	else
17959db44a8eSMartin Matuska 		pools = zpool_find_import(&handle, import);
1796eda14cbcSMatt Macy 
1797eda14cbcSMatt Macy 	if ((pools == NULL || nvlist_empty(pools)) &&
1798eda14cbcSMatt Macy 	    handle.lpc_open_access_error && geteuid() != 0) {
1799eda14cbcSMatt Macy 		(void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN,
1800eda14cbcSMatt Macy 		    "no pools found"));
1801eda14cbcSMatt Macy 	}
1802eda14cbcSMatt Macy 
1803eda14cbcSMatt Macy 	return (pools);
1804eda14cbcSMatt Macy }
1805eda14cbcSMatt Macy 
1806eda14cbcSMatt Macy static boolean_t
1807eda14cbcSMatt Macy pool_match(nvlist_t *cfg, char *tgt)
1808eda14cbcSMatt Macy {
1809eda14cbcSMatt Macy 	uint64_t v, guid = strtoull(tgt, NULL, 0);
1810eda14cbcSMatt Macy 	char *s;
1811eda14cbcSMatt Macy 
1812eda14cbcSMatt Macy 	if (guid != 0) {
1813eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
1814eda14cbcSMatt Macy 			return (v == guid);
1815eda14cbcSMatt Macy 	} else {
1816eda14cbcSMatt Macy 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
1817eda14cbcSMatt Macy 			return (strcmp(s, tgt) == 0);
1818eda14cbcSMatt Macy 	}
1819eda14cbcSMatt Macy 	return (B_FALSE);
1820eda14cbcSMatt Macy }
1821eda14cbcSMatt Macy 
1822eda14cbcSMatt Macy int
1823eda14cbcSMatt Macy zpool_find_config(void *hdl, const char *target, nvlist_t **configp,
1824eda14cbcSMatt Macy     importargs_t *args, const pool_config_ops_t *pco)
1825eda14cbcSMatt Macy {
1826eda14cbcSMatt Macy 	nvlist_t *pools;
1827eda14cbcSMatt Macy 	nvlist_t *match = NULL;
1828eda14cbcSMatt Macy 	nvlist_t *config = NULL;
18297877fdebSMatt Macy 	char *sepp = NULL;
1830eda14cbcSMatt Macy 	int count = 0;
1831eda14cbcSMatt Macy 	char *targetdup = strdup(target);
1832eda14cbcSMatt Macy 
1833eda14cbcSMatt Macy 	*configp = NULL;
1834eda14cbcSMatt Macy 
183516038816SMartin Matuska 	if ((sepp = strpbrk(targetdup, "/@")) != NULL)
1836eda14cbcSMatt Macy 		*sepp = '\0';
1837eda14cbcSMatt Macy 
1838eda14cbcSMatt Macy 	pools = zpool_search_import(hdl, args, pco);
1839eda14cbcSMatt Macy 
1840eda14cbcSMatt Macy 	if (pools != NULL) {
1841eda14cbcSMatt Macy 		nvpair_t *elem = NULL;
1842eda14cbcSMatt Macy 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1843eda14cbcSMatt Macy 			VERIFY0(nvpair_value_nvlist(elem, &config));
1844eda14cbcSMatt Macy 			if (pool_match(config, targetdup)) {
1845eda14cbcSMatt Macy 				count++;
1846eda14cbcSMatt Macy 				if (match != NULL) {
1847eda14cbcSMatt Macy 					/* multiple matches found */
1848eda14cbcSMatt Macy 					continue;
1849eda14cbcSMatt Macy 				} else {
18507877fdebSMatt Macy 					match = fnvlist_dup(config);
1851eda14cbcSMatt Macy 				}
1852eda14cbcSMatt Macy 			}
1853eda14cbcSMatt Macy 		}
18547877fdebSMatt Macy 		fnvlist_free(pools);
1855eda14cbcSMatt Macy 	}
1856eda14cbcSMatt Macy 
1857eda14cbcSMatt Macy 	if (count == 0) {
1858eda14cbcSMatt Macy 		free(targetdup);
1859eda14cbcSMatt Macy 		return (ENOENT);
1860eda14cbcSMatt Macy 	}
1861eda14cbcSMatt Macy 
1862eda14cbcSMatt Macy 	if (count > 1) {
1863eda14cbcSMatt Macy 		free(targetdup);
18647877fdebSMatt Macy 		fnvlist_free(match);
1865eda14cbcSMatt Macy 		return (EINVAL);
1866eda14cbcSMatt Macy 	}
1867eda14cbcSMatt Macy 
1868eda14cbcSMatt Macy 	*configp = match;
1869eda14cbcSMatt Macy 	free(targetdup);
1870eda14cbcSMatt Macy 
1871eda14cbcSMatt Macy 	return (0);
1872eda14cbcSMatt Macy }
18736ba2210eSMartin Matuska 
18746ba2210eSMartin Matuska /*
18756ba2210eSMartin Matuska  * Internal function for iterating over the vdevs.
18766ba2210eSMartin Matuska  *
18776ba2210eSMartin Matuska  * For each vdev, func() will be called and will be passed 'zhp' (which is
18786ba2210eSMartin Matuska  * typically the zpool_handle_t cast as a void pointer), the vdev's nvlist, and
18796ba2210eSMartin Matuska  * a user-defined data pointer).
18806ba2210eSMartin Matuska  *
18816ba2210eSMartin Matuska  * The return values from all the func() calls will be OR'd together and
18826ba2210eSMartin Matuska  * returned.
18836ba2210eSMartin Matuska  */
18846ba2210eSMartin Matuska int
18856ba2210eSMartin Matuska for_each_vdev_cb(void *zhp, nvlist_t *nv, pool_vdev_iter_f func,
18866ba2210eSMartin Matuska     void *data)
18876ba2210eSMartin Matuska {
18886ba2210eSMartin Matuska 	nvlist_t **child;
18896ba2210eSMartin Matuska 	uint_t c, children;
18906ba2210eSMartin Matuska 	int ret = 0;
18916ba2210eSMartin Matuska 	int i;
18926ba2210eSMartin Matuska 	char *type;
18936ba2210eSMartin Matuska 
18946ba2210eSMartin Matuska 	const char *list[] = {
18956ba2210eSMartin Matuska 	    ZPOOL_CONFIG_SPARES,
18966ba2210eSMartin Matuska 	    ZPOOL_CONFIG_L2CACHE,
18976ba2210eSMartin Matuska 	    ZPOOL_CONFIG_CHILDREN
18986ba2210eSMartin Matuska 	};
18996ba2210eSMartin Matuska 
1900681ce946SMartin Matuska 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1901681ce946SMartin Matuska 		return (ret);
1902681ce946SMartin Matuska 
1903681ce946SMartin Matuska 	/* Don't run our function on root or indirect vdevs */
1904681ce946SMartin Matuska 	if ((strcmp(type, VDEV_TYPE_ROOT) != 0) &&
1905681ce946SMartin Matuska 	    (strcmp(type, VDEV_TYPE_INDIRECT) != 0)) {
1906681ce946SMartin Matuska 		ret |= func(zhp, nv, data);
1907681ce946SMartin Matuska 	}
1908681ce946SMartin Matuska 
19096ba2210eSMartin Matuska 	for (i = 0; i < ARRAY_SIZE(list); i++) {
19106ba2210eSMartin Matuska 		if (nvlist_lookup_nvlist_array(nv, list[i], &child,
19116ba2210eSMartin Matuska 		    &children) == 0) {
19126ba2210eSMartin Matuska 			for (c = 0; c < children; c++) {
19136ba2210eSMartin Matuska 				uint64_t ishole = 0;
19146ba2210eSMartin Matuska 
19156ba2210eSMartin Matuska 				(void) nvlist_lookup_uint64(child[c],
19166ba2210eSMartin Matuska 				    ZPOOL_CONFIG_IS_HOLE, &ishole);
19176ba2210eSMartin Matuska 
19186ba2210eSMartin Matuska 				if (ishole)
19196ba2210eSMartin Matuska 					continue;
19206ba2210eSMartin Matuska 
19216ba2210eSMartin Matuska 				ret |= for_each_vdev_cb(zhp, child[c],
19226ba2210eSMartin Matuska 				    func, data);
19236ba2210eSMartin Matuska 			}
19246ba2210eSMartin Matuska 		}
19256ba2210eSMartin Matuska 	}
19266ba2210eSMartin Matuska 
19276ba2210eSMartin Matuska 	return (ret);
19286ba2210eSMartin Matuska }
19296ba2210eSMartin Matuska 
19306ba2210eSMartin Matuska /*
19316ba2210eSMartin Matuska  * Given an ZPOOL_CONFIG_VDEV_TREE nvpair, iterate over all the vdevs, calling
19326ba2210eSMartin Matuska  * func() for each one.  func() is passed the vdev's nvlist and an optional
19336ba2210eSMartin Matuska  * user-defined 'data' pointer.
19346ba2210eSMartin Matuska  */
19356ba2210eSMartin Matuska int
19366ba2210eSMartin Matuska for_each_vdev_in_nvlist(nvlist_t *nvroot, pool_vdev_iter_f func, void *data)
19376ba2210eSMartin Matuska {
19386ba2210eSMartin Matuska 	return (for_each_vdev_cb(NULL, nvroot, func, data));
19396ba2210eSMartin Matuska }
1940