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