1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2288ecc943SGeorge Wilson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
27fa9e4066Sahrens  * Pool import support functions.
28fa9e4066Sahrens  *
29fa9e4066Sahrens  * To import a pool, we rely on reading the configuration information from the
30fa9e4066Sahrens  * ZFS label of each device.  If we successfully read the label, then we
31fa9e4066Sahrens  * organize the configuration information in the following hierarchy:
32fa9e4066Sahrens  *
33fa9e4066Sahrens  * 	pool guid -> toplevel vdev guid -> label txg
34fa9e4066Sahrens  *
35fa9e4066Sahrens  * Duplicate entries matching this same tuple will be discarded.  Once we have
36fa9e4066Sahrens  * examined every device, we pick the best label txg config for each toplevel
37fa9e4066Sahrens  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
38fa9e4066Sahrens  * update any paths that have changed.  Finally, we attempt to import the pool
39fa9e4066Sahrens  * using our derived config, and record the results.
40fa9e4066Sahrens  */
41fa9e4066Sahrens 
42*4f67d755SEric Taylor #include <ctype.h>
43fa9e4066Sahrens #include <devid.h>
44fa9e4066Sahrens #include <dirent.h>
45fa9e4066Sahrens #include <errno.h>
46fa9e4066Sahrens #include <libintl.h>
47*4f67d755SEric Taylor #include <stddef.h>
48fa9e4066Sahrens #include <stdlib.h>
49fa9e4066Sahrens #include <string.h>
50fa9e4066Sahrens #include <sys/stat.h>
51fa9e4066Sahrens #include <unistd.h>
52fa9e4066Sahrens #include <fcntl.h>
53*4f67d755SEric Taylor #include <sys/vtoc.h>
54*4f67d755SEric Taylor #include <sys/dktp/fdisk.h>
55*4f67d755SEric Taylor #include <sys/efi_partition.h>
56*4f67d755SEric Taylor #include <thread_pool.h>
57fa9e4066Sahrens 
58fa9e4066Sahrens #include <sys/vdev_impl.h>
59fa9e4066Sahrens 
60fa9e4066Sahrens #include "libzfs.h"
61fa9e4066Sahrens #include "libzfs_impl.h"
62fa9e4066Sahrens 
63fa9e4066Sahrens /*
64fa9e4066Sahrens  * Intermediate structures used to gather configuration information.
65fa9e4066Sahrens  */
66fa9e4066Sahrens typedef struct config_entry {
67fa9e4066Sahrens 	uint64_t		ce_txg;
68fa9e4066Sahrens 	nvlist_t		*ce_config;
69fa9e4066Sahrens 	struct config_entry	*ce_next;
70fa9e4066Sahrens } config_entry_t;
71fa9e4066Sahrens 
72fa9e4066Sahrens typedef struct vdev_entry {
73fa9e4066Sahrens 	uint64_t		ve_guid;
74fa9e4066Sahrens 	config_entry_t		*ve_configs;
75fa9e4066Sahrens 	struct vdev_entry	*ve_next;
76fa9e4066Sahrens } vdev_entry_t;
77fa9e4066Sahrens 
78fa9e4066Sahrens typedef struct pool_entry {
79fa9e4066Sahrens 	uint64_t		pe_guid;
80fa9e4066Sahrens 	vdev_entry_t		*pe_vdevs;
81fa9e4066Sahrens 	struct pool_entry	*pe_next;
82fa9e4066Sahrens } pool_entry_t;
83fa9e4066Sahrens 
84fa9e4066Sahrens typedef struct name_entry {
8599653d4eSeschrock 	char			*ne_name;
86fa9e4066Sahrens 	uint64_t		ne_guid;
87fa9e4066Sahrens 	struct name_entry	*ne_next;
88fa9e4066Sahrens } name_entry_t;
89fa9e4066Sahrens 
90fa9e4066Sahrens typedef struct pool_list {
91fa9e4066Sahrens 	pool_entry_t		*pools;
92fa9e4066Sahrens 	name_entry_t		*names;
93fa9e4066Sahrens } pool_list_t;
94fa9e4066Sahrens 
95fa9e4066Sahrens static char *
96fa9e4066Sahrens get_devid(const char *path)
97fa9e4066Sahrens {
98fa9e4066Sahrens 	int fd;
99fa9e4066Sahrens 	ddi_devid_t devid;
100fa9e4066Sahrens 	char *minor, *ret;
101fa9e4066Sahrens 
102fa9e4066Sahrens 	if ((fd = open(path, O_RDONLY)) < 0)
103fa9e4066Sahrens 		return (NULL);
104fa9e4066Sahrens 
105fa9e4066Sahrens 	minor = NULL;
106fa9e4066Sahrens 	ret = NULL;
107fa9e4066Sahrens 	if (devid_get(fd, &devid) == 0) {
108fa9e4066Sahrens 		if (devid_get_minor_name(fd, &minor) == 0)
109fa9e4066Sahrens 			ret = devid_str_encode(devid, minor);
110fa9e4066Sahrens 		if (minor != NULL)
111fa9e4066Sahrens 			devid_str_free(minor);
112fa9e4066Sahrens 		devid_free(devid);
113fa9e4066Sahrens 	}
114c67d9675Seschrock 	(void) close(fd);
115fa9e4066Sahrens 
116fa9e4066Sahrens 	return (ret);
117fa9e4066Sahrens }
118fa9e4066Sahrens 
119fa9e4066Sahrens 
120fa9e4066Sahrens /*
121fa9e4066Sahrens  * Go through and fix up any path and/or devid information for the given vdev
122fa9e4066Sahrens  * configuration.
123fa9e4066Sahrens  */
12499653d4eSeschrock static int
125fa9e4066Sahrens fix_paths(nvlist_t *nv, name_entry_t *names)
126fa9e4066Sahrens {
127fa9e4066Sahrens 	nvlist_t **child;
128fa9e4066Sahrens 	uint_t c, children;
129fa9e4066Sahrens 	uint64_t guid;
130c67d9675Seschrock 	name_entry_t *ne, *best;
131c67d9675Seschrock 	char *path, *devid;
132c67d9675Seschrock 	int matched;
133fa9e4066Sahrens 
134fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
135fa9e4066Sahrens 	    &child, &children) == 0) {
136fa9e4066Sahrens 		for (c = 0; c < children; c++)
13799653d4eSeschrock 			if (fix_paths(child[c], names) != 0)
13899653d4eSeschrock 				return (-1);
13999653d4eSeschrock 		return (0);
140fa9e4066Sahrens 	}
141fa9e4066Sahrens 
142fa9e4066Sahrens 	/*
143fa9e4066Sahrens 	 * This is a leaf (file or disk) vdev.  In either case, go through
144fa9e4066Sahrens 	 * the name list and see if we find a matching guid.  If so, replace
145fa9e4066Sahrens 	 * the path and see if we can calculate a new devid.
146c67d9675Seschrock 	 *
147c67d9675Seschrock 	 * There may be multiple names associated with a particular guid, in
148c67d9675Seschrock 	 * which case we have overlapping slices or multiple paths to the same
149c67d9675Seschrock 	 * disk.  If this is the case, then we want to pick the path that is
150c67d9675Seschrock 	 * the most similar to the original, where "most similar" is the number
151c67d9675Seschrock 	 * of matching characters starting from the end of the path.  This will
152c67d9675Seschrock 	 * preserve slice numbers even if the disks have been reorganized, and
153c67d9675Seschrock 	 * will also catch preferred disk names if multiple paths exist.
154fa9e4066Sahrens 	 */
155fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
156c67d9675Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
157c67d9675Seschrock 		path = NULL;
158c67d9675Seschrock 
159c67d9675Seschrock 	matched = 0;
160c67d9675Seschrock 	best = NULL;
161c67d9675Seschrock 	for (ne = names; ne != NULL; ne = ne->ne_next) {
162c67d9675Seschrock 		if (ne->ne_guid == guid) {
163c67d9675Seschrock 			const char *src, *dst;
164c67d9675Seschrock 			int count;
165c67d9675Seschrock 
166c67d9675Seschrock 			if (path == NULL) {
167c67d9675Seschrock 				best = ne;
168c67d9675Seschrock 				break;
169c67d9675Seschrock 			}
170c67d9675Seschrock 
171c67d9675Seschrock 			src = ne->ne_name + strlen(ne->ne_name) - 1;
172c67d9675Seschrock 			dst = path + strlen(path) - 1;
173c67d9675Seschrock 			for (count = 0; src >= ne->ne_name && dst >= path;
174c67d9675Seschrock 			    src--, dst--, count++)
175c67d9675Seschrock 				if (*src != *dst)
176c67d9675Seschrock 					break;
177c67d9675Seschrock 
178c67d9675Seschrock 			/*
179c67d9675Seschrock 			 * At this point, 'count' is the number of characters
180c67d9675Seschrock 			 * matched from the end.
181c67d9675Seschrock 			 */
182c67d9675Seschrock 			if (count > matched || best == NULL) {
183c67d9675Seschrock 				best = ne;
184c67d9675Seschrock 				matched = count;
185c67d9675Seschrock 			}
186c67d9675Seschrock 		}
187c67d9675Seschrock 	}
188fa9e4066Sahrens 
189c67d9675Seschrock 	if (best == NULL)
19099653d4eSeschrock 		return (0);
191fa9e4066Sahrens 
19299653d4eSeschrock 	if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
19399653d4eSeschrock 		return (-1);
194fa9e4066Sahrens 
195c67d9675Seschrock 	if ((devid = get_devid(best->ne_name)) == NULL) {
196fa9e4066Sahrens 		(void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
197fa9e4066Sahrens 	} else {
19899653d4eSeschrock 		if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
19999653d4eSeschrock 			return (-1);
200fa9e4066Sahrens 		devid_str_free(devid);
201fa9e4066Sahrens 	}
20299653d4eSeschrock 
20399653d4eSeschrock 	return (0);
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens /*
207fa9e4066Sahrens  * Add the given configuration to the list of known devices.
208fa9e4066Sahrens  */
20999653d4eSeschrock static int
21099653d4eSeschrock add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
21199653d4eSeschrock     nvlist_t *config)
212fa9e4066Sahrens {
21399653d4eSeschrock 	uint64_t pool_guid, vdev_guid, top_guid, txg, state;
214fa9e4066Sahrens 	pool_entry_t *pe;
215fa9e4066Sahrens 	vdev_entry_t *ve;
216fa9e4066Sahrens 	config_entry_t *ce;
217fa9e4066Sahrens 	name_entry_t *ne;
218fa9e4066Sahrens 
21999653d4eSeschrock 	/*
220fa94a07fSbrendan 	 * If this is a hot spare not currently in use or level 2 cache
221fa94a07fSbrendan 	 * device, add it to the list of names to translate, but don't do
222fa94a07fSbrendan 	 * anything else.
22399653d4eSeschrock 	 */
22499653d4eSeschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
225fa94a07fSbrendan 	    &state) == 0 &&
226fa94a07fSbrendan 	    (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
22799653d4eSeschrock 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
22899653d4eSeschrock 		if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
229ccae0b50Seschrock 			return (-1);
23099653d4eSeschrock 
23199653d4eSeschrock 		if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
23299653d4eSeschrock 			free(ne);
23399653d4eSeschrock 			return (-1);
23499653d4eSeschrock 		}
23599653d4eSeschrock 		ne->ne_guid = vdev_guid;
23699653d4eSeschrock 		ne->ne_next = pl->names;
23799653d4eSeschrock 		pl->names = ne;
23899653d4eSeschrock 		return (0);
23999653d4eSeschrock 	}
24099653d4eSeschrock 
241fa9e4066Sahrens 	/*
242fa9e4066Sahrens 	 * If we have a valid config but cannot read any of these fields, then
243fa9e4066Sahrens 	 * it means we have a half-initialized label.  In vdev_label_init()
244fa9e4066Sahrens 	 * we write a label with txg == 0 so that we can identify the device
245fa9e4066Sahrens 	 * in case the user refers to the same disk later on.  If we fail to
246fa9e4066Sahrens 	 * create the pool, we'll be left with a label in this state
247fa9e4066Sahrens 	 * which should not be considered part of a valid pool.
248fa9e4066Sahrens 	 */
249fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
250fa9e4066Sahrens 	    &pool_guid) != 0 ||
251fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
252fa9e4066Sahrens 	    &vdev_guid) != 0 ||
253fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
254fa9e4066Sahrens 	    &top_guid) != 0 ||
255fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
256fa9e4066Sahrens 	    &txg) != 0 || txg == 0) {
257fa9e4066Sahrens 		nvlist_free(config);
25899653d4eSeschrock 		return (0);
259fa9e4066Sahrens 	}
260fa9e4066Sahrens 
261fa9e4066Sahrens 	/*
262fa9e4066Sahrens 	 * First, see if we know about this pool.  If not, then add it to the
263fa9e4066Sahrens 	 * list of known pools.
264fa9e4066Sahrens 	 */
265fa9e4066Sahrens 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
266fa9e4066Sahrens 		if (pe->pe_guid == pool_guid)
267fa9e4066Sahrens 			break;
268fa9e4066Sahrens 	}
269fa9e4066Sahrens 
270fa9e4066Sahrens 	if (pe == NULL) {
27199653d4eSeschrock 		if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
27299653d4eSeschrock 			nvlist_free(config);
27399653d4eSeschrock 			return (-1);
27499653d4eSeschrock 		}
275fa9e4066Sahrens 		pe->pe_guid = pool_guid;
276fa9e4066Sahrens 		pe->pe_next = pl->pools;
277fa9e4066Sahrens 		pl->pools = pe;
278fa9e4066Sahrens 	}
279fa9e4066Sahrens 
280fa9e4066Sahrens 	/*
281fa9e4066Sahrens 	 * Second, see if we know about this toplevel vdev.  Add it if its
282fa9e4066Sahrens 	 * missing.
283fa9e4066Sahrens 	 */
284fa9e4066Sahrens 	for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
285fa9e4066Sahrens 		if (ve->ve_guid == top_guid)
286fa9e4066Sahrens 			break;
287fa9e4066Sahrens 	}
288fa9e4066Sahrens 
289fa9e4066Sahrens 	if (ve == NULL) {
29099653d4eSeschrock 		if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
29199653d4eSeschrock 			nvlist_free(config);
29299653d4eSeschrock 			return (-1);
29399653d4eSeschrock 		}
294fa9e4066Sahrens 		ve->ve_guid = top_guid;
295fa9e4066Sahrens 		ve->ve_next = pe->pe_vdevs;
296fa9e4066Sahrens 		pe->pe_vdevs = ve;
297fa9e4066Sahrens 	}
298fa9e4066Sahrens 
299fa9e4066Sahrens 	/*
300fa9e4066Sahrens 	 * Third, see if we have a config with a matching transaction group.  If
301fa9e4066Sahrens 	 * so, then we do nothing.  Otherwise, add it to the list of known
302fa9e4066Sahrens 	 * configs.
303fa9e4066Sahrens 	 */
304fa9e4066Sahrens 	for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
305fa9e4066Sahrens 		if (ce->ce_txg == txg)
306fa9e4066Sahrens 			break;
307fa9e4066Sahrens 	}
308fa9e4066Sahrens 
309fa9e4066Sahrens 	if (ce == NULL) {
31099653d4eSeschrock 		if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
31199653d4eSeschrock 			nvlist_free(config);
31299653d4eSeschrock 			return (-1);
31399653d4eSeschrock 		}
314fa9e4066Sahrens 		ce->ce_txg = txg;
315fa9e4066Sahrens 		ce->ce_config = config;
316fa9e4066Sahrens 		ce->ce_next = ve->ve_configs;
317fa9e4066Sahrens 		ve->ve_configs = ce;
318fa9e4066Sahrens 	} else {
319fa9e4066Sahrens 		nvlist_free(config);
320fa9e4066Sahrens 	}
321fa9e4066Sahrens 
322fa9e4066Sahrens 	/*
323fa9e4066Sahrens 	 * At this point we've successfully added our config to the list of
324fa9e4066Sahrens 	 * known configs.  The last thing to do is add the vdev guid -> path
325fa9e4066Sahrens 	 * mappings so that we can fix up the configuration as necessary before
326fa9e4066Sahrens 	 * doing the import.
327fa9e4066Sahrens 	 */
32899653d4eSeschrock 	if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
32999653d4eSeschrock 		return (-1);
33099653d4eSeschrock 
33199653d4eSeschrock 	if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
33299653d4eSeschrock 		free(ne);
33399653d4eSeschrock 		return (-1);
33499653d4eSeschrock 	}
335fa9e4066Sahrens 
336fa9e4066Sahrens 	ne->ne_guid = vdev_guid;
337fa9e4066Sahrens 	ne->ne_next = pl->names;
338fa9e4066Sahrens 	pl->names = ne;
33999653d4eSeschrock 
34099653d4eSeschrock 	return (0);
341fa9e4066Sahrens }
342fa9e4066Sahrens 
343eaca9bbdSeschrock /*
344eaca9bbdSeschrock  * Returns true if the named pool matches the given GUID.
345eaca9bbdSeschrock  */
34694de1d4cSeschrock static int
34794de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
34894de1d4cSeschrock     boolean_t *isactive)
349eaca9bbdSeschrock {
350eaca9bbdSeschrock 	zpool_handle_t *zhp;
351eaca9bbdSeschrock 	uint64_t theguid;
352eaca9bbdSeschrock 
35394de1d4cSeschrock 	if (zpool_open_silent(hdl, name, &zhp) != 0)
35494de1d4cSeschrock 		return (-1);
35594de1d4cSeschrock 
35694de1d4cSeschrock 	if (zhp == NULL) {
35794de1d4cSeschrock 		*isactive = B_FALSE;
35894de1d4cSeschrock 		return (0);
35994de1d4cSeschrock 	}
360eaca9bbdSeschrock 
361eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
362eaca9bbdSeschrock 	    &theguid) == 0);
363eaca9bbdSeschrock 
364eaca9bbdSeschrock 	zpool_close(zhp);
365eaca9bbdSeschrock 
36694de1d4cSeschrock 	*isactive = (theguid == guid);
36794de1d4cSeschrock 	return (0);
368eaca9bbdSeschrock }
369eaca9bbdSeschrock 
3702f8aaab3Seschrock static nvlist_t *
3712f8aaab3Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
3722f8aaab3Seschrock {
3732f8aaab3Seschrock 	nvlist_t *nvl;
3742f8aaab3Seschrock 	zfs_cmd_t zc = { 0 };
3752f8aaab3Seschrock 	int err;
3762f8aaab3Seschrock 
3772f8aaab3Seschrock 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
3782f8aaab3Seschrock 		return (NULL);
3792f8aaab3Seschrock 
3802f8aaab3Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc,
3812f8aaab3Seschrock 	    zc.zc_nvlist_conf_size * 2) != 0) {
3822f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
3832f8aaab3Seschrock 		return (NULL);
3842f8aaab3Seschrock 	}
3852f8aaab3Seschrock 
3862f8aaab3Seschrock 	while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
3872f8aaab3Seschrock 	    &zc)) != 0 && errno == ENOMEM) {
3882f8aaab3Seschrock 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3892f8aaab3Seschrock 			zcmd_free_nvlists(&zc);
3902f8aaab3Seschrock 			return (NULL);
3912f8aaab3Seschrock 		}
3922f8aaab3Seschrock 	}
3932f8aaab3Seschrock 
3942f8aaab3Seschrock 	if (err) {
3952f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
3962f8aaab3Seschrock 		return (NULL);
3972f8aaab3Seschrock 	}
3982f8aaab3Seschrock 
3992f8aaab3Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
4002f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
4012f8aaab3Seschrock 		return (NULL);
4022f8aaab3Seschrock 	}
4032f8aaab3Seschrock 
4042f8aaab3Seschrock 	zcmd_free_nvlists(&zc);
4052f8aaab3Seschrock 	return (nvl);
4062f8aaab3Seschrock }
4072f8aaab3Seschrock 
40888ecc943SGeorge Wilson /*
40988ecc943SGeorge Wilson  * Determine if the vdev id is a hole in the namespace.
41088ecc943SGeorge Wilson  */
41188ecc943SGeorge Wilson boolean_t
41288ecc943SGeorge Wilson vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
41388ecc943SGeorge Wilson {
41488ecc943SGeorge Wilson 	for (int c = 0; c < holes; c++) {
41588ecc943SGeorge Wilson 
41688ecc943SGeorge Wilson 		/* Top-level is a hole */
41788ecc943SGeorge Wilson 		if (hole_array[c] == id)
41888ecc943SGeorge Wilson 			return (B_TRUE);
41988ecc943SGeorge Wilson 	}
42088ecc943SGeorge Wilson 	return (B_FALSE);
42188ecc943SGeorge Wilson }
42288ecc943SGeorge Wilson 
423fa9e4066Sahrens /*
424fa9e4066Sahrens  * Convert our list of pools into the definitive set of configurations.  We
425fa9e4066Sahrens  * start by picking the best config for each toplevel vdev.  Once that's done,
426fa9e4066Sahrens  * we assemble the toplevel vdevs into a full config for the pool.  We make a
427fa9e4066Sahrens  * pass to fix up any incorrect paths, and then add it to the main list to
428fa9e4066Sahrens  * return to the user.
429fa9e4066Sahrens  */
430fa9e4066Sahrens static nvlist_t *
4313a57275aSck get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
432fa9e4066Sahrens {
43399653d4eSeschrock 	pool_entry_t *pe;
43499653d4eSeschrock 	vdev_entry_t *ve;
43599653d4eSeschrock 	config_entry_t *ce;
43699653d4eSeschrock 	nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot;
437fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
438fa94a07fSbrendan 	uint_t i, nspares, nl2cache;
43999653d4eSeschrock 	boolean_t config_seen;
440fa9e4066Sahrens 	uint64_t best_txg;
44195173954Sek 	char *name, *hostname;
44299653d4eSeschrock 	uint64_t version, guid;
44399653d4eSeschrock 	uint_t children = 0;
44499653d4eSeschrock 	nvlist_t **child = NULL;
44588ecc943SGeorge Wilson 	uint_t holes;
44688ecc943SGeorge Wilson 	uint64_t *hole_array, max_id;
44799653d4eSeschrock 	uint_t c;
44894de1d4cSeschrock 	boolean_t isactive;
44995173954Sek 	uint64_t hostid;
4502f8aaab3Seschrock 	nvlist_t *nvl;
45124e697d4Sck 	boolean_t found_one = B_FALSE;
45288ecc943SGeorge Wilson 	boolean_t valid_top_config = B_FALSE;
453fa9e4066Sahrens 
45499653d4eSeschrock 	if (nvlist_alloc(&ret, 0, 0) != 0)
45599653d4eSeschrock 		goto nomem;
456fa9e4066Sahrens 
45799653d4eSeschrock 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
45888ecc943SGeorge Wilson 		uint64_t id, max_txg = 0;
459fa9e4066Sahrens 
46099653d4eSeschrock 		if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
46199653d4eSeschrock 			goto nomem;
46299653d4eSeschrock 		config_seen = B_FALSE;
463fa9e4066Sahrens 
464fa9e4066Sahrens 		/*
465fa9e4066Sahrens 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
466fa9e4066Sahrens 		 * from the first one we find, and then go through the rest and
467fa9e4066Sahrens 		 * add them as necessary to the 'vdevs' member of the config.
468fa9e4066Sahrens 		 */
46999653d4eSeschrock 		for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
470fa9e4066Sahrens 
471fa9e4066Sahrens 			/*
472fa9e4066Sahrens 			 * Determine the best configuration for this vdev by
473fa9e4066Sahrens 			 * selecting the config with the latest transaction
474fa9e4066Sahrens 			 * group.
475fa9e4066Sahrens 			 */
476fa9e4066Sahrens 			best_txg = 0;
477fa9e4066Sahrens 			for (ce = ve->ve_configs; ce != NULL;
478fa9e4066Sahrens 			    ce = ce->ce_next) {
479fa9e4066Sahrens 
48099653d4eSeschrock 				if (ce->ce_txg > best_txg) {
481fa9e4066Sahrens 					tmp = ce->ce_config;
48299653d4eSeschrock 					best_txg = ce->ce_txg;
48399653d4eSeschrock 				}
484fa9e4066Sahrens 			}
485fa9e4066Sahrens 
48688ecc943SGeorge Wilson 			/*
48788ecc943SGeorge Wilson 			 * We rely on the fact that the max txg for the
48888ecc943SGeorge Wilson 			 * pool will contain the most up-to-date information
48988ecc943SGeorge Wilson 			 * about the valid top-levels in the vdev namespace.
49088ecc943SGeorge Wilson 			 */
49188ecc943SGeorge Wilson 			if (best_txg > max_txg) {
49288ecc943SGeorge Wilson 				(void) nvlist_remove(config,
49388ecc943SGeorge Wilson 				    ZPOOL_CONFIG_VDEV_CHILDREN,
49488ecc943SGeorge Wilson 				    DATA_TYPE_UINT64);
49588ecc943SGeorge Wilson 				(void) nvlist_remove(config,
49688ecc943SGeorge Wilson 				    ZPOOL_CONFIG_HOLE_ARRAY,
49788ecc943SGeorge Wilson 				    DATA_TYPE_UINT64_ARRAY);
49888ecc943SGeorge Wilson 
49988ecc943SGeorge Wilson 				max_txg = best_txg;
50088ecc943SGeorge Wilson 				hole_array = NULL;
50188ecc943SGeorge Wilson 				holes = 0;
50288ecc943SGeorge Wilson 				max_id = 0;
50388ecc943SGeorge Wilson 				valid_top_config = B_FALSE;
50488ecc943SGeorge Wilson 
50588ecc943SGeorge Wilson 				if (nvlist_lookup_uint64(tmp,
50688ecc943SGeorge Wilson 				    ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
50788ecc943SGeorge Wilson 					verify(nvlist_add_uint64(config,
50888ecc943SGeorge Wilson 					    ZPOOL_CONFIG_VDEV_CHILDREN,
50988ecc943SGeorge Wilson 					    max_id) == 0);
51088ecc943SGeorge Wilson 					valid_top_config = B_TRUE;
51188ecc943SGeorge Wilson 				}
51288ecc943SGeorge Wilson 
51388ecc943SGeorge Wilson 				if (nvlist_lookup_uint64_array(tmp,
51488ecc943SGeorge Wilson 				    ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
51588ecc943SGeorge Wilson 				    &holes) == 0) {
51688ecc943SGeorge Wilson 					verify(nvlist_add_uint64_array(config,
51788ecc943SGeorge Wilson 					    ZPOOL_CONFIG_HOLE_ARRAY,
51888ecc943SGeorge Wilson 					    hole_array, holes) == 0);
51988ecc943SGeorge Wilson 				}
52088ecc943SGeorge Wilson 			}
52188ecc943SGeorge Wilson 
522fa9e4066Sahrens 			if (!config_seen) {
523fa9e4066Sahrens 				/*
524fa9e4066Sahrens 				 * Copy the relevant pieces of data to the pool
525fa9e4066Sahrens 				 * configuration:
526fa9e4066Sahrens 				 *
52799653d4eSeschrock 				 *	version
528fa9e4066Sahrens 				 * 	pool guid
529fa9e4066Sahrens 				 * 	name
530fa9e4066Sahrens 				 * 	pool state
53195173954Sek 				 *	hostid (if available)
53295173954Sek 				 *	hostname (if available)
533fa9e4066Sahrens 				 */
534fa9e4066Sahrens 				uint64_t state;
535fa9e4066Sahrens 
53699653d4eSeschrock 				verify(nvlist_lookup_uint64(tmp,
53799653d4eSeschrock 				    ZPOOL_CONFIG_VERSION, &version) == 0);
53899653d4eSeschrock 				if (nvlist_add_uint64(config,
53999653d4eSeschrock 				    ZPOOL_CONFIG_VERSION, version) != 0)
54099653d4eSeschrock 					goto nomem;
541fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
542fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
54399653d4eSeschrock 				if (nvlist_add_uint64(config,
54499653d4eSeschrock 				    ZPOOL_CONFIG_POOL_GUID, guid) != 0)
54599653d4eSeschrock 					goto nomem;
546fa9e4066Sahrens 				verify(nvlist_lookup_string(tmp,
547fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
54899653d4eSeschrock 				if (nvlist_add_string(config,
54999653d4eSeschrock 				    ZPOOL_CONFIG_POOL_NAME, name) != 0)
55099653d4eSeschrock 					goto nomem;
551fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
552fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
55399653d4eSeschrock 				if (nvlist_add_uint64(config,
55499653d4eSeschrock 				    ZPOOL_CONFIG_POOL_STATE, state) != 0)
55599653d4eSeschrock 					goto nomem;
55695173954Sek 				hostid = 0;
55795173954Sek 				if (nvlist_lookup_uint64(tmp,
55895173954Sek 				    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
55995173954Sek 					if (nvlist_add_uint64(config,
56095173954Sek 					    ZPOOL_CONFIG_HOSTID, hostid) != 0)
56195173954Sek 						goto nomem;
56295173954Sek 					verify(nvlist_lookup_string(tmp,
56395173954Sek 					    ZPOOL_CONFIG_HOSTNAME,
56495173954Sek 					    &hostname) == 0);
56595173954Sek 					if (nvlist_add_string(config,
56695173954Sek 					    ZPOOL_CONFIG_HOSTNAME,
56795173954Sek 					    hostname) != 0)
56895173954Sek 						goto nomem;
56995173954Sek 				}
570fa9e4066Sahrens 
57199653d4eSeschrock 				config_seen = B_TRUE;
572fa9e4066Sahrens 			}
573fa9e4066Sahrens 
574fa9e4066Sahrens 			/*
575fa9e4066Sahrens 			 * Add this top-level vdev to the child array.
576fa9e4066Sahrens 			 */
577fa9e4066Sahrens 			verify(nvlist_lookup_nvlist(tmp,
578fa9e4066Sahrens 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
579fa9e4066Sahrens 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
580fa9e4066Sahrens 			    &id) == 0);
58188ecc943SGeorge Wilson 
582fa9e4066Sahrens 			if (id >= children) {
583fa9e4066Sahrens 				nvlist_t **newchild;
584fa9e4066Sahrens 
58599653d4eSeschrock 				newchild = zfs_alloc(hdl, (id + 1) *
586fa9e4066Sahrens 				    sizeof (nvlist_t *));
58799653d4eSeschrock 				if (newchild == NULL)
58899653d4eSeschrock 					goto nomem;
589fa9e4066Sahrens 
590fa9e4066Sahrens 				for (c = 0; c < children; c++)
591fa9e4066Sahrens 					newchild[c] = child[c];
592fa9e4066Sahrens 
593fa9e4066Sahrens 				free(child);
594fa9e4066Sahrens 				child = newchild;
595fa9e4066Sahrens 				children = id + 1;
596fa9e4066Sahrens 			}
59799653d4eSeschrock 			if (nvlist_dup(nvtop, &child[id], 0) != 0)
59899653d4eSeschrock 				goto nomem;
599fa9e4066Sahrens 
600fa9e4066Sahrens 		}
601fa9e4066Sahrens 
60288ecc943SGeorge Wilson 		/*
60388ecc943SGeorge Wilson 		 * If we have information about all the top-levels then
60488ecc943SGeorge Wilson 		 * clean up the nvlist which we've constructed. This
60588ecc943SGeorge Wilson 		 * means removing any extraneous devices that are
60688ecc943SGeorge Wilson 		 * beyond the valid range or adding devices to the end
60788ecc943SGeorge Wilson 		 * of our array which appear to be missing.
60888ecc943SGeorge Wilson 		 */
60988ecc943SGeorge Wilson 		if (valid_top_config) {
61088ecc943SGeorge Wilson 			if (max_id < children) {
61188ecc943SGeorge Wilson 				for (c = max_id; c < children; c++)
61288ecc943SGeorge Wilson 					nvlist_free(child[c]);
61388ecc943SGeorge Wilson 				children = max_id;
61488ecc943SGeorge Wilson 			} else if (max_id > children) {
61588ecc943SGeorge Wilson 				nvlist_t **newchild;
61688ecc943SGeorge Wilson 
61788ecc943SGeorge Wilson 				newchild = zfs_alloc(hdl, (max_id) *
61888ecc943SGeorge Wilson 				    sizeof (nvlist_t *));
61988ecc943SGeorge Wilson 				if (newchild == NULL)
62088ecc943SGeorge Wilson 					goto nomem;
62188ecc943SGeorge Wilson 
62288ecc943SGeorge Wilson 				for (c = 0; c < children; c++)
62388ecc943SGeorge Wilson 					newchild[c] = child[c];
62488ecc943SGeorge Wilson 
62588ecc943SGeorge Wilson 				free(child);
62688ecc943SGeorge Wilson 				child = newchild;
62788ecc943SGeorge Wilson 				children = max_id;
62888ecc943SGeorge Wilson 			}
62988ecc943SGeorge Wilson 		}
63088ecc943SGeorge Wilson 
631fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
632fa9e4066Sahrens 		    &guid) == 0);
633fa9e4066Sahrens 
63488ecc943SGeorge Wilson 		/*
63588ecc943SGeorge Wilson 		 * The vdev namespace may contain holes as a result of
63688ecc943SGeorge Wilson 		 * device removal. We must add them back into the vdev
63788ecc943SGeorge Wilson 		 * tree before we process any missing devices.
63888ecc943SGeorge Wilson 		 */
63988ecc943SGeorge Wilson 		if (holes > 0) {
64088ecc943SGeorge Wilson 			ASSERT(valid_top_config);
64188ecc943SGeorge Wilson 
64288ecc943SGeorge Wilson 			for (c = 0; c < children; c++) {
64388ecc943SGeorge Wilson 				nvlist_t *holey;
64488ecc943SGeorge Wilson 
64588ecc943SGeorge Wilson 				if (child[c] != NULL ||
64688ecc943SGeorge Wilson 				    !vdev_is_hole(hole_array, holes, c))
64788ecc943SGeorge Wilson 					continue;
64888ecc943SGeorge Wilson 
64988ecc943SGeorge Wilson 				if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
65088ecc943SGeorge Wilson 				    0) != 0)
65188ecc943SGeorge Wilson 					goto nomem;
65288ecc943SGeorge Wilson 
65388ecc943SGeorge Wilson 				/*
65488ecc943SGeorge Wilson 				 * Holes in the namespace are treated as
65588ecc943SGeorge Wilson 				 * "hole" top-level vdevs and have a
65688ecc943SGeorge Wilson 				 * special flag set on them.
65788ecc943SGeorge Wilson 				 */
65888ecc943SGeorge Wilson 				if (nvlist_add_string(holey,
65988ecc943SGeorge Wilson 				    ZPOOL_CONFIG_TYPE,
66088ecc943SGeorge Wilson 				    VDEV_TYPE_HOLE) != 0 ||
66188ecc943SGeorge Wilson 				    nvlist_add_uint64(holey,
66288ecc943SGeorge Wilson 				    ZPOOL_CONFIG_ID, c) != 0 ||
66388ecc943SGeorge Wilson 				    nvlist_add_uint64(holey,
66488ecc943SGeorge Wilson 				    ZPOOL_CONFIG_GUID, 0ULL) != 0)
66588ecc943SGeorge Wilson 					goto nomem;
66688ecc943SGeorge Wilson 				child[c] = holey;
66788ecc943SGeorge Wilson 			}
66888ecc943SGeorge Wilson 		}
66988ecc943SGeorge Wilson 
670fa9e4066Sahrens 		/*
671fa9e4066Sahrens 		 * Look for any missing top-level vdevs.  If this is the case,
672fa9e4066Sahrens 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
673fa9e4066Sahrens 		 * simply compress the child array, because the kernel performs
674fa9e4066Sahrens 		 * certain checks to make sure the vdev IDs match their location
675fa9e4066Sahrens 		 * in the configuration.
676fa9e4066Sahrens 		 */
67788ecc943SGeorge Wilson 		for (c = 0; c < children; c++) {
678fa9e4066Sahrens 			if (child[c] == NULL) {
679fa9e4066Sahrens 				nvlist_t *missing;
68099653d4eSeschrock 				if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
68199653d4eSeschrock 				    0) != 0)
68299653d4eSeschrock 					goto nomem;
68399653d4eSeschrock 				if (nvlist_add_string(missing,
68499653d4eSeschrock 				    ZPOOL_CONFIG_TYPE,
68599653d4eSeschrock 				    VDEV_TYPE_MISSING) != 0 ||
68699653d4eSeschrock 				    nvlist_add_uint64(missing,
68799653d4eSeschrock 				    ZPOOL_CONFIG_ID, c) != 0 ||
68899653d4eSeschrock 				    nvlist_add_uint64(missing,
68999653d4eSeschrock 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
69099653d4eSeschrock 					nvlist_free(missing);
69199653d4eSeschrock 					goto nomem;
69299653d4eSeschrock 				}
693fa9e4066Sahrens 				child[c] = missing;
694fa9e4066Sahrens 			}
69588ecc943SGeorge Wilson 		}
696fa9e4066Sahrens 
697fa9e4066Sahrens 		/*
698fa9e4066Sahrens 		 * Put all of this pool's top-level vdevs into a root vdev.
699fa9e4066Sahrens 		 */
70099653d4eSeschrock 		if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
70199653d4eSeschrock 			goto nomem;
70299653d4eSeschrock 		if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
70399653d4eSeschrock 		    VDEV_TYPE_ROOT) != 0 ||
70499653d4eSeschrock 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
70599653d4eSeschrock 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
70699653d4eSeschrock 		    nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
70799653d4eSeschrock 		    child, children) != 0) {
70899653d4eSeschrock 			nvlist_free(nvroot);
70999653d4eSeschrock 			goto nomem;
71099653d4eSeschrock 		}
711fa9e4066Sahrens 
712fa9e4066Sahrens 		for (c = 0; c < children; c++)
713fa9e4066Sahrens 			nvlist_free(child[c]);
714fa9e4066Sahrens 		free(child);
71599653d4eSeschrock 		children = 0;
71699653d4eSeschrock 		child = NULL;
717fa9e4066Sahrens 
718fa9e4066Sahrens 		/*
719fa9e4066Sahrens 		 * Go through and fix up any paths and/or devids based on our
720fa9e4066Sahrens 		 * known list of vdev GUID -> path mappings.
721fa9e4066Sahrens 		 */
72299653d4eSeschrock 		if (fix_paths(nvroot, pl->names) != 0) {
72399653d4eSeschrock 			nvlist_free(nvroot);
72499653d4eSeschrock 			goto nomem;
72599653d4eSeschrock 		}
726fa9e4066Sahrens 
727fa9e4066Sahrens 		/*
728fa9e4066Sahrens 		 * Add the root vdev to this pool's configuration.
729fa9e4066Sahrens 		 */
73099653d4eSeschrock 		if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
73199653d4eSeschrock 		    nvroot) != 0) {
73299653d4eSeschrock 			nvlist_free(nvroot);
73399653d4eSeschrock 			goto nomem;
73499653d4eSeschrock 		}
735fa9e4066Sahrens 		nvlist_free(nvroot);
736fa9e4066Sahrens 
7373a57275aSck 		/*
7383a57275aSck 		 * zdb uses this path to report on active pools that were
7393a57275aSck 		 * imported or created using -R.
7403a57275aSck 		 */
7413a57275aSck 		if (active_ok)
7423a57275aSck 			goto add_pool;
7433a57275aSck 
744fa9e4066Sahrens 		/*
745fa9e4066Sahrens 		 * Determine if this pool is currently active, in which case we
746fa9e4066Sahrens 		 * can't actually import it.
747fa9e4066Sahrens 		 */
748fa9e4066Sahrens 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
749fa9e4066Sahrens 		    &name) == 0);
750fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
751fa9e4066Sahrens 		    &guid) == 0);
752fa9e4066Sahrens 
75394de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0)
75494de1d4cSeschrock 			goto error;
75594de1d4cSeschrock 
7560192a278Seschrock 		if (isactive) {
757fa9e4066Sahrens 			nvlist_free(config);
75899653d4eSeschrock 			config = NULL;
759fa9e4066Sahrens 			continue;
760fa9e4066Sahrens 		}
761fa9e4066Sahrens 
76288ecc943SGeorge Wilson 		if ((nvl = refresh_config(hdl, config)) == NULL) {
76388ecc943SGeorge Wilson 			nvlist_free(config);
76488ecc943SGeorge Wilson 			config = NULL;
76588ecc943SGeorge Wilson 			continue;
76688ecc943SGeorge Wilson 		}
767fa9e4066Sahrens 
768fa9e4066Sahrens 		nvlist_free(config);
7692f8aaab3Seschrock 		config = nvl;
770fa9e4066Sahrens 
77199653d4eSeschrock 		/*
77299653d4eSeschrock 		 * Go through and update the paths for spares, now that we have
77399653d4eSeschrock 		 * them.
77499653d4eSeschrock 		 */
77599653d4eSeschrock 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
77699653d4eSeschrock 		    &nvroot) == 0);
77799653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
77899653d4eSeschrock 		    &spares, &nspares) == 0) {
77999653d4eSeschrock 			for (i = 0; i < nspares; i++) {
78099653d4eSeschrock 				if (fix_paths(spares[i], pl->names) != 0)
78199653d4eSeschrock 					goto nomem;
78299653d4eSeschrock 			}
78399653d4eSeschrock 		}
78499653d4eSeschrock 
785fa94a07fSbrendan 		/*
786fa94a07fSbrendan 		 * Update the paths for l2cache devices.
787fa94a07fSbrendan 		 */
788fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
789fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0) {
790fa94a07fSbrendan 			for (i = 0; i < nl2cache; i++) {
791fa94a07fSbrendan 				if (fix_paths(l2cache[i], pl->names) != 0)
792fa94a07fSbrendan 					goto nomem;
793fa94a07fSbrendan 			}
794fa94a07fSbrendan 		}
795fa94a07fSbrendan 
79695173954Sek 		/*
79795173954Sek 		 * Restore the original information read from the actual label.
79895173954Sek 		 */
79995173954Sek 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
80095173954Sek 		    DATA_TYPE_UINT64);
80195173954Sek 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
80295173954Sek 		    DATA_TYPE_STRING);
80395173954Sek 		if (hostid != 0) {
80495173954Sek 			verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
80595173954Sek 			    hostid) == 0);
80695173954Sek 			verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
80795173954Sek 			    hostname) == 0);
80895173954Sek 		}
80995173954Sek 
8103a57275aSck add_pool:
811fa9e4066Sahrens 		/*
812fa9e4066Sahrens 		 * Add this pool to the list of configs.
813fa9e4066Sahrens 		 */
814e9dbad6fSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
815e9dbad6fSeschrock 		    &name) == 0);
81699653d4eSeschrock 		if (nvlist_add_nvlist(ret, name, config) != 0)
81799653d4eSeschrock 			goto nomem;
818fa9e4066Sahrens 
81924e697d4Sck 		found_one = B_TRUE;
820fa9e4066Sahrens 		nvlist_free(config);
82199653d4eSeschrock 		config = NULL;
822fa9e4066Sahrens 	}
823fa9e4066Sahrens 
82424e697d4Sck 	if (!found_one) {
82524e697d4Sck 		nvlist_free(ret);
82624e697d4Sck 		ret = NULL;
82724e697d4Sck 	}
82824e697d4Sck 
829fa9e4066Sahrens 	return (ret);
83099653d4eSeschrock 
83199653d4eSeschrock nomem:
83299653d4eSeschrock 	(void) no_memory(hdl);
83399653d4eSeschrock error:
83494de1d4cSeschrock 	nvlist_free(config);
83594de1d4cSeschrock 	nvlist_free(ret);
83699653d4eSeschrock 	for (c = 0; c < children; c++)
83799653d4eSeschrock 		nvlist_free(child[c]);
83894de1d4cSeschrock 	free(child);
83999653d4eSeschrock 
84099653d4eSeschrock 	return (NULL);
841fa9e4066Sahrens }
842fa9e4066Sahrens 
843fa9e4066Sahrens /*
844fa9e4066Sahrens  * Return the offset of the given label.
845fa9e4066Sahrens  */
846fa9e4066Sahrens static uint64_t
847e7437265Sahrens label_offset(uint64_t size, int l)
848fa9e4066Sahrens {
849e7437265Sahrens 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
850fa9e4066Sahrens 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
851fa9e4066Sahrens 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
852fa9e4066Sahrens }
853fa9e4066Sahrens 
854fa9e4066Sahrens /*
855fa9e4066Sahrens  * Given a file descriptor, read the label information and return an nvlist
856fa9e4066Sahrens  * describing the configuration, if there is one.
857fa9e4066Sahrens  */
85899653d4eSeschrock int
85999653d4eSeschrock zpool_read_label(int fd, nvlist_t **config)
860fa9e4066Sahrens {
861fa9e4066Sahrens 	struct stat64 statbuf;
862fa9e4066Sahrens 	int l;
863fa9e4066Sahrens 	vdev_label_t *label;
864e7437265Sahrens 	uint64_t state, txg, size;
865fa9e4066Sahrens 
86699653d4eSeschrock 	*config = NULL;
86799653d4eSeschrock 
868fa9e4066Sahrens 	if (fstat64(fd, &statbuf) == -1)
86999653d4eSeschrock 		return (0);
870e7437265Sahrens 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
871fa9e4066Sahrens 
87299653d4eSeschrock 	if ((label = malloc(sizeof (vdev_label_t))) == NULL)
87399653d4eSeschrock 		return (-1);
874fa9e4066Sahrens 
875fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
876c5904d13Seschrock 		if (pread64(fd, label, sizeof (vdev_label_t),
877e7437265Sahrens 		    label_offset(size, l)) != sizeof (vdev_label_t))
878fa9e4066Sahrens 			continue;
879fa9e4066Sahrens 
880fa9e4066Sahrens 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
88199653d4eSeschrock 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
882fa9e4066Sahrens 			continue;
883fa9e4066Sahrens 
88499653d4eSeschrock 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
885fa94a07fSbrendan 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
88699653d4eSeschrock 			nvlist_free(*config);
887fa9e4066Sahrens 			continue;
888fa9e4066Sahrens 		}
889fa9e4066Sahrens 
890fa94a07fSbrendan 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
89199653d4eSeschrock 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
89299653d4eSeschrock 		    &txg) != 0 || txg == 0)) {
89399653d4eSeschrock 			nvlist_free(*config);
894fa9e4066Sahrens 			continue;
895fa9e4066Sahrens 		}
896fa9e4066Sahrens 
897fa9e4066Sahrens 		free(label);
89899653d4eSeschrock 		return (0);
899fa9e4066Sahrens 	}
900fa9e4066Sahrens 
901fa9e4066Sahrens 	free(label);
90299653d4eSeschrock 	*config = NULL;
90399653d4eSeschrock 	return (0);
904fa9e4066Sahrens }
905fa9e4066Sahrens 
906*4f67d755SEric Taylor typedef struct rdsk_node {
907*4f67d755SEric Taylor 	char *rn_name;
908*4f67d755SEric Taylor 	int rn_dfd;
909*4f67d755SEric Taylor 	libzfs_handle_t *rn_hdl;
910*4f67d755SEric Taylor 	nvlist_t *rn_config;
911*4f67d755SEric Taylor 	avl_tree_t *rn_avl;
912*4f67d755SEric Taylor 	avl_node_t rn_node;
913*4f67d755SEric Taylor 	boolean_t rn_nozpool;
914*4f67d755SEric Taylor } rdsk_node_t;
915*4f67d755SEric Taylor 
916*4f67d755SEric Taylor static int
917*4f67d755SEric Taylor slice_cache_compare(const void *arg1, const void *arg2)
918*4f67d755SEric Taylor {
919*4f67d755SEric Taylor 	const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
920*4f67d755SEric Taylor 	const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
921*4f67d755SEric Taylor 	char *nm1slice, *nm2slice;
922*4f67d755SEric Taylor 	int rv;
923*4f67d755SEric Taylor 
924*4f67d755SEric Taylor 	/*
925*4f67d755SEric Taylor 	 * slices zero and two are the most likely to provide results,
926*4f67d755SEric Taylor 	 * so put those first
927*4f67d755SEric Taylor 	 */
928*4f67d755SEric Taylor 	nm1slice = strstr(nm1, "s0");
929*4f67d755SEric Taylor 	nm2slice = strstr(nm2, "s0");
930*4f67d755SEric Taylor 	if (nm1slice && !nm2slice) {
931*4f67d755SEric Taylor 		return (-1);
932*4f67d755SEric Taylor 	}
933*4f67d755SEric Taylor 	if (!nm1slice && nm2slice) {
934*4f67d755SEric Taylor 		return (1);
935*4f67d755SEric Taylor 	}
936*4f67d755SEric Taylor 	nm1slice = strstr(nm1, "s2");
937*4f67d755SEric Taylor 	nm2slice = strstr(nm2, "s2");
938*4f67d755SEric Taylor 	if (nm1slice && !nm2slice) {
939*4f67d755SEric Taylor 		return (-1);
940*4f67d755SEric Taylor 	}
941*4f67d755SEric Taylor 	if (!nm1slice && nm2slice) {
942*4f67d755SEric Taylor 		return (1);
943*4f67d755SEric Taylor 	}
944*4f67d755SEric Taylor 
945*4f67d755SEric Taylor 	rv = strcmp(nm1, nm2);
946*4f67d755SEric Taylor 	if (rv == 0)
947*4f67d755SEric Taylor 		return (0);
948*4f67d755SEric Taylor 	return (rv > 0 ? 1 : -1);
949*4f67d755SEric Taylor }
950*4f67d755SEric Taylor 
951*4f67d755SEric Taylor static void
952*4f67d755SEric Taylor check_one_slice(avl_tree_t *r, char *diskname, uint_t partno,
953*4f67d755SEric Taylor     diskaddr_t size, uint_t blksz)
954*4f67d755SEric Taylor {
955*4f67d755SEric Taylor 	rdsk_node_t tmpnode;
956*4f67d755SEric Taylor 	rdsk_node_t *node;
957*4f67d755SEric Taylor 	char sname[MAXNAMELEN];
958*4f67d755SEric Taylor 
959*4f67d755SEric Taylor 	tmpnode.rn_name = &sname[0];
960*4f67d755SEric Taylor 	(void) snprintf(tmpnode.rn_name, MAXNAMELEN, "%s%u",
961*4f67d755SEric Taylor 	    diskname, partno);
962*4f67d755SEric Taylor 	/* too small to contain a zpool? */
963*4f67d755SEric Taylor 	if ((size < (SPA_MINDEVSIZE / blksz)) &&
964*4f67d755SEric Taylor 	    (node = avl_find(r, &tmpnode, NULL)))
965*4f67d755SEric Taylor 		node->rn_nozpool = B_TRUE;
966*4f67d755SEric Taylor }
967*4f67d755SEric Taylor 
968*4f67d755SEric Taylor static void
969*4f67d755SEric Taylor nozpool_all_slices(avl_tree_t *r, const char *sname)
970*4f67d755SEric Taylor {
971*4f67d755SEric Taylor 	char diskname[MAXNAMELEN];
972*4f67d755SEric Taylor 	char *ptr;
973*4f67d755SEric Taylor 	int i;
974*4f67d755SEric Taylor 
975*4f67d755SEric Taylor 	(void) strncpy(diskname, sname, MAXNAMELEN);
976*4f67d755SEric Taylor 	if (((ptr = strrchr(diskname, 's')) == NULL) &&
977*4f67d755SEric Taylor 	    ((ptr = strrchr(diskname, 'p')) == NULL))
978*4f67d755SEric Taylor 		return;
979*4f67d755SEric Taylor 	ptr[0] = 's';
980*4f67d755SEric Taylor 	ptr[1] = '\0';
981*4f67d755SEric Taylor 	for (i = 0; i < NDKMAP; i++)
982*4f67d755SEric Taylor 		check_one_slice(r, diskname, i, 0, 1);
983*4f67d755SEric Taylor 	ptr[0] = 'p';
984*4f67d755SEric Taylor 	for (i = 0; i <= FD_NUMPART; i++)
985*4f67d755SEric Taylor 		check_one_slice(r, diskname, i, 0, 1);
986*4f67d755SEric Taylor }
987*4f67d755SEric Taylor 
988*4f67d755SEric Taylor static void
989*4f67d755SEric Taylor check_slices(avl_tree_t *r, int fd, const char *sname)
990*4f67d755SEric Taylor {
991*4f67d755SEric Taylor 	struct extvtoc vtoc;
992*4f67d755SEric Taylor 	struct dk_gpt *gpt;
993*4f67d755SEric Taylor 	char diskname[MAXNAMELEN];
994*4f67d755SEric Taylor 	char *ptr;
995*4f67d755SEric Taylor 	int i;
996*4f67d755SEric Taylor 
997*4f67d755SEric Taylor 	(void) strncpy(diskname, sname, MAXNAMELEN);
998*4f67d755SEric Taylor 	if ((ptr = strrchr(diskname, 's')) == NULL || !isdigit(ptr[1]))
999*4f67d755SEric Taylor 		return;
1000*4f67d755SEric Taylor 	ptr[1] = '\0';
1001*4f67d755SEric Taylor 
1002*4f67d755SEric Taylor 	if (read_extvtoc(fd, &vtoc) >= 0) {
1003*4f67d755SEric Taylor 		for (i = 0; i < NDKMAP; i++)
1004*4f67d755SEric Taylor 			check_one_slice(r, diskname, i,
1005*4f67d755SEric Taylor 			    vtoc.v_part[i].p_size, vtoc.v_sectorsz);
1006*4f67d755SEric Taylor 	} else if (efi_alloc_and_read(fd, &gpt) >= 0) {
1007*4f67d755SEric Taylor 		/*
1008*4f67d755SEric Taylor 		 * on x86 we'll still have leftover links that point
1009*4f67d755SEric Taylor 		 * to slices s[9-15], so use NDKMAP instead
1010*4f67d755SEric Taylor 		 */
1011*4f67d755SEric Taylor 		for (i = 0; i < NDKMAP; i++)
1012*4f67d755SEric Taylor 			check_one_slice(r, diskname, i,
1013*4f67d755SEric Taylor 			    gpt->efi_parts[i].p_size, gpt->efi_lbasize);
1014*4f67d755SEric Taylor 		/* nodes p[1-4] are never used with EFI labels */
1015*4f67d755SEric Taylor 		ptr[0] = 'p';
1016*4f67d755SEric Taylor 		for (i = 1; i <= FD_NUMPART; i++)
1017*4f67d755SEric Taylor 			check_one_slice(r, diskname, i, 0, 1);
1018*4f67d755SEric Taylor 		efi_free(gpt);
1019*4f67d755SEric Taylor 	}
1020*4f67d755SEric Taylor }
1021*4f67d755SEric Taylor 
1022*4f67d755SEric Taylor static void
1023*4f67d755SEric Taylor zpool_open_func(void *arg)
1024*4f67d755SEric Taylor {
1025*4f67d755SEric Taylor 	rdsk_node_t *rn = arg;
1026*4f67d755SEric Taylor 	struct stat64 statbuf;
1027*4f67d755SEric Taylor 	nvlist_t *config;
1028*4f67d755SEric Taylor 	int fd;
1029*4f67d755SEric Taylor 
1030*4f67d755SEric Taylor 	if (rn->rn_nozpool)
1031*4f67d755SEric Taylor 		return;
1032*4f67d755SEric Taylor 	if ((fd = openat64(rn->rn_dfd, rn->rn_name, O_RDONLY)) < 0) {
1033*4f67d755SEric Taylor 		/* symlink to a device that's no longer there */
1034*4f67d755SEric Taylor 		if (errno == ENOENT)
1035*4f67d755SEric Taylor 			nozpool_all_slices(rn->rn_avl, rn->rn_name);
1036*4f67d755SEric Taylor 		return;
1037*4f67d755SEric Taylor 	}
1038*4f67d755SEric Taylor 	/*
1039*4f67d755SEric Taylor 	 * Ignore failed stats.  We only want regular
1040*4f67d755SEric Taylor 	 * files, character devs and block devs.
1041*4f67d755SEric Taylor 	 */
1042*4f67d755SEric Taylor 	if (fstat64(fd, &statbuf) != 0 ||
1043*4f67d755SEric Taylor 	    (!S_ISREG(statbuf.st_mode) &&
1044*4f67d755SEric Taylor 	    !S_ISCHR(statbuf.st_mode) &&
1045*4f67d755SEric Taylor 	    !S_ISBLK(statbuf.st_mode))) {
1046*4f67d755SEric Taylor 		(void) close(fd);
1047*4f67d755SEric Taylor 		return;
1048*4f67d755SEric Taylor 	}
1049*4f67d755SEric Taylor 	/* this file is too small to hold a zpool */
1050*4f67d755SEric Taylor 	if (S_ISREG(statbuf.st_mode) &&
1051*4f67d755SEric Taylor 	    statbuf.st_size < SPA_MINDEVSIZE) {
1052*4f67d755SEric Taylor 		(void) close(fd);
1053*4f67d755SEric Taylor 		return;
1054*4f67d755SEric Taylor 	} else if (!S_ISREG(statbuf.st_mode)) {
1055*4f67d755SEric Taylor 		/*
1056*4f67d755SEric Taylor 		 * Try to read the disk label first so we don't have to
1057*4f67d755SEric Taylor 		 * open a bunch of minor nodes that can't have a zpool.
1058*4f67d755SEric Taylor 		 */
1059*4f67d755SEric Taylor 		check_slices(rn->rn_avl, fd, rn->rn_name);
1060*4f67d755SEric Taylor 	}
1061*4f67d755SEric Taylor 
1062*4f67d755SEric Taylor 	if ((zpool_read_label(fd, &config)) != 0) {
1063*4f67d755SEric Taylor 		(void) close(fd);
1064*4f67d755SEric Taylor 		(void) no_memory(rn->rn_hdl);
1065*4f67d755SEric Taylor 		return;
1066*4f67d755SEric Taylor 	}
1067*4f67d755SEric Taylor 	(void) close(fd);
1068*4f67d755SEric Taylor 
1069*4f67d755SEric Taylor 
1070*4f67d755SEric Taylor 	rn->rn_config = config;
1071*4f67d755SEric Taylor 	if (config != NULL) {
1072*4f67d755SEric Taylor 		assert(rn->rn_nozpool == B_FALSE);
1073*4f67d755SEric Taylor 	}
1074*4f67d755SEric Taylor }
1075*4f67d755SEric Taylor 
1076096d22d4SEric Schrock /*
1077096d22d4SEric Schrock  * Given a file descriptor, clear (zero) the label information.  This function
1078096d22d4SEric Schrock  * is currently only used in the appliance stack as part of the ZFS sysevent
1079096d22d4SEric Schrock  * module.
1080096d22d4SEric Schrock  */
1081096d22d4SEric Schrock int
1082096d22d4SEric Schrock zpool_clear_label(int fd)
1083096d22d4SEric Schrock {
1084096d22d4SEric Schrock 	struct stat64 statbuf;
1085096d22d4SEric Schrock 	int l;
1086096d22d4SEric Schrock 	vdev_label_t *label;
1087096d22d4SEric Schrock 	uint64_t size;
1088096d22d4SEric Schrock 
1089096d22d4SEric Schrock 	if (fstat64(fd, &statbuf) == -1)
1090096d22d4SEric Schrock 		return (0);
1091096d22d4SEric Schrock 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1092096d22d4SEric Schrock 
1093096d22d4SEric Schrock 	if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
1094096d22d4SEric Schrock 		return (-1);
1095096d22d4SEric Schrock 
1096096d22d4SEric Schrock 	for (l = 0; l < VDEV_LABELS; l++) {
1097096d22d4SEric Schrock 		if (pwrite64(fd, label, sizeof (vdev_label_t),
1098096d22d4SEric Schrock 		    label_offset(size, l)) != sizeof (vdev_label_t))
1099096d22d4SEric Schrock 			return (-1);
1100096d22d4SEric Schrock 	}
1101096d22d4SEric Schrock 
1102096d22d4SEric Schrock 	free(label);
1103096d22d4SEric Schrock 	return (0);
1104096d22d4SEric Schrock }
1105096d22d4SEric Schrock 
1106fa9e4066Sahrens /*
1107fa9e4066Sahrens  * Given a list of directories to search, find all pools stored on disk.  This
1108fa9e4066Sahrens  * includes partial pools which are not available to import.  If no args are
1109fa9e4066Sahrens  * given (argc is 0), then the default directory (/dev/dsk) is searched.
111024e697d4Sck  * poolname or guid (but not both) are provided by the caller when trying
111124e697d4Sck  * to import a specific pool.
1112fa9e4066Sahrens  */
111324e697d4Sck static nvlist_t *
111424e697d4Sck zpool_find_import_impl(libzfs_handle_t *hdl, int argc, char **argv,
111524e697d4Sck     boolean_t active_ok, char *poolname, uint64_t guid)
1116fa9e4066Sahrens {
1117fa9e4066Sahrens 	int i;
1118ccae0b50Seschrock 	DIR *dirp = NULL;
1119fa9e4066Sahrens 	struct dirent64 *dp;
1120fa9e4066Sahrens 	char path[MAXPATHLEN];
1121842c5645Sjwadams 	char *end;
1122842c5645Sjwadams 	size_t pathleft;
1123*4f67d755SEric Taylor 	nvlist_t *ret = NULL;
1124fa9e4066Sahrens 	static char *default_dir = "/dev/dsk";
1125fa9e4066Sahrens 	pool_list_t pools = { 0 };
112699653d4eSeschrock 	pool_entry_t *pe, *penext;
112799653d4eSeschrock 	vdev_entry_t *ve, *venext;
112899653d4eSeschrock 	config_entry_t *ce, *cenext;
112999653d4eSeschrock 	name_entry_t *ne, *nenext;
1130*4f67d755SEric Taylor 	avl_tree_t slice_cache;
1131*4f67d755SEric Taylor 	rdsk_node_t *slice;
1132*4f67d755SEric Taylor 	void *cookie;
113399653d4eSeschrock 
113424e697d4Sck 	verify(poolname == NULL || guid == 0);
113524e697d4Sck 
1136fa9e4066Sahrens 	if (argc == 0) {
1137fa9e4066Sahrens 		argc = 1;
1138fa9e4066Sahrens 		argv = &default_dir;
1139fa9e4066Sahrens 	}
1140fa9e4066Sahrens 
1141fa9e4066Sahrens 	/*
1142fa9e4066Sahrens 	 * Go through and read the label configuration information from every
1143fa9e4066Sahrens 	 * possible device, organizing the information according to pool GUID
1144fa9e4066Sahrens 	 * and toplevel GUID.
1145fa9e4066Sahrens 	 */
1146fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
1147*4f67d755SEric Taylor 		tpool_t *t;
1148842c5645Sjwadams 		char *rdsk;
1149842c5645Sjwadams 		int dfd;
1150842c5645Sjwadams 
1151842c5645Sjwadams 		/* use realpath to normalize the path */
1152842c5645Sjwadams 		if (realpath(argv[i], path) == 0) {
1153ece3d9b3Slling 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
115499653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1155fa9e4066Sahrens 			    argv[i]);
115699653d4eSeschrock 			goto error;
1157fa9e4066Sahrens 		}
1158842c5645Sjwadams 		end = &path[strlen(path)];
1159842c5645Sjwadams 		*end++ = '/';
1160842c5645Sjwadams 		*end = 0;
1161842c5645Sjwadams 		pathleft = &path[sizeof (path)] - end;
1162842c5645Sjwadams 
1163842c5645Sjwadams 		/*
1164842c5645Sjwadams 		 * Using raw devices instead of block devices when we're
1165842c5645Sjwadams 		 * reading the labels skips a bunch of slow operations during
1166842c5645Sjwadams 		 * close(2) processing, so we replace /dev/dsk with /dev/rdsk.
1167842c5645Sjwadams 		 */
1168842c5645Sjwadams 		if (strcmp(path, "/dev/dsk/") == 0)
1169842c5645Sjwadams 			rdsk = "/dev/rdsk/";
1170842c5645Sjwadams 		else
1171842c5645Sjwadams 			rdsk = path;
1172fa9e4066Sahrens 
1173842c5645Sjwadams 		if ((dfd = open64(rdsk, O_RDONLY)) < 0 ||
1174842c5645Sjwadams 		    (dirp = fdopendir(dfd)) == NULL) {
117599653d4eSeschrock 			zfs_error_aux(hdl, strerror(errno));
1176ece3d9b3Slling 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
117799653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1178842c5645Sjwadams 			    rdsk);
117999653d4eSeschrock 			goto error;
1180fa9e4066Sahrens 		}
1181fa9e4066Sahrens 
1182*4f67d755SEric Taylor 		avl_create(&slice_cache, slice_cache_compare,
1183*4f67d755SEric Taylor 		    sizeof (rdsk_node_t), offsetof(rdsk_node_t, rn_node));
1184fa9e4066Sahrens 		/*
1185fa9e4066Sahrens 		 * This is not MT-safe, but we have no MT consumers of libzfs
1186fa9e4066Sahrens 		 */
1187fa9e4066Sahrens 		while ((dp = readdir64(dirp)) != NULL) {
1188842c5645Sjwadams 			const char *name = dp->d_name;
1189842c5645Sjwadams 			if (name[0] == '.' &&
1190842c5645Sjwadams 			    (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1191fa9e4066Sahrens 				continue;
1192fa9e4066Sahrens 
1193*4f67d755SEric Taylor 			slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
1194*4f67d755SEric Taylor 			slice->rn_name = zfs_strdup(hdl, name);
1195*4f67d755SEric Taylor 			slice->rn_avl = &slice_cache;
1196*4f67d755SEric Taylor 			slice->rn_dfd = dfd;
1197*4f67d755SEric Taylor 			slice->rn_hdl = hdl;
1198*4f67d755SEric Taylor 			slice->rn_nozpool = B_FALSE;
1199*4f67d755SEric Taylor 			avl_add(&slice_cache, slice);
1200*4f67d755SEric Taylor 		}
1201*4f67d755SEric Taylor 		/*
1202*4f67d755SEric Taylor 		 * create a thread pool to do all of this in parallel;
1203*4f67d755SEric Taylor 		 * rn_nozpool is not protected, so this is racy in that
1204*4f67d755SEric Taylor 		 * multiple tasks could decide that the same slice can
1205*4f67d755SEric Taylor 		 * not hold a zpool, which is benign.  Also choose
1206*4f67d755SEric Taylor 		 * double the number of processors; we hold a lot of
1207*4f67d755SEric Taylor 		 * locks in the kernel, so going beyond this doesn't
1208*4f67d755SEric Taylor 		 * buy us much.
1209*4f67d755SEric Taylor 		 */
1210*4f67d755SEric Taylor 		t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN),
1211*4f67d755SEric Taylor 		    0, NULL);
1212*4f67d755SEric Taylor 		for (slice = avl_first(&slice_cache); slice;
1213*4f67d755SEric Taylor 		    (slice = avl_walk(&slice_cache, slice,
1214*4f67d755SEric Taylor 		    AVL_AFTER)))
1215*4f67d755SEric Taylor 			(void) tpool_dispatch(t, zpool_open_func, slice);
1216*4f67d755SEric Taylor 		tpool_wait(t);
1217*4f67d755SEric Taylor 		tpool_destroy(t);
1218*4f67d755SEric Taylor 
1219*4f67d755SEric Taylor 		cookie = NULL;
1220*4f67d755SEric Taylor 		while ((slice = avl_destroy_nodes(&slice_cache,
1221*4f67d755SEric Taylor 		    &cookie)) != NULL) {
1222*4f67d755SEric Taylor 			if (slice->rn_config != NULL) {
1223*4f67d755SEric Taylor 				nvlist_t *config = slice->rn_config;
122424e697d4Sck 				boolean_t matched = B_TRUE;
122524e697d4Sck 
122624e697d4Sck 				if (poolname != NULL) {
122724e697d4Sck 					char *pname;
12287b0e68eaSck 
12297b0e68eaSck 					matched = nvlist_lookup_string(config,
123024e697d4Sck 					    ZPOOL_CONFIG_POOL_NAME,
12317b0e68eaSck 					    &pname) == 0 &&
12327b0e68eaSck 					    strcmp(poolname, pname) == 0;
123324e697d4Sck 				} else if (guid != 0) {
123424e697d4Sck 					uint64_t this_guid;
1235913f2d80Sck 
1236913f2d80Sck 					matched = nvlist_lookup_uint64(config,
123724e697d4Sck 					    ZPOOL_CONFIG_POOL_GUID,
1238913f2d80Sck 					    &this_guid) == 0 &&
1239913f2d80Sck 					    guid == this_guid;
124024e697d4Sck 				}
124124e697d4Sck 				if (!matched) {
124224e697d4Sck 					nvlist_free(config);
124324e697d4Sck 					config = NULL;
124424e697d4Sck 					continue;
124524e697d4Sck 				}
1246842c5645Sjwadams 				/* use the non-raw path for the config */
1247*4f67d755SEric Taylor 				(void) strlcpy(end, slice->rn_name, pathleft);
124899653d4eSeschrock 				if (add_config(hdl, &pools, path, config) != 0)
124999653d4eSeschrock 					goto error;
1250842c5645Sjwadams 			}
1251*4f67d755SEric Taylor 			free(slice->rn_name);
1252*4f67d755SEric Taylor 			free(slice);
1253fa9e4066Sahrens 		}
1254*4f67d755SEric Taylor 		avl_destroy(&slice_cache);
1255ccae0b50Seschrock 
1256ccae0b50Seschrock 		(void) closedir(dirp);
1257ccae0b50Seschrock 		dirp = NULL;
1258fa9e4066Sahrens 	}
1259fa9e4066Sahrens 
12603a57275aSck 	ret = get_configs(hdl, &pools, active_ok);
126199653d4eSeschrock 
126299653d4eSeschrock error:
126399653d4eSeschrock 	for (pe = pools.pools; pe != NULL; pe = penext) {
126499653d4eSeschrock 		penext = pe->pe_next;
126599653d4eSeschrock 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
126699653d4eSeschrock 			venext = ve->ve_next;
126799653d4eSeschrock 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
126899653d4eSeschrock 				cenext = ce->ce_next;
126999653d4eSeschrock 				if (ce->ce_config)
127099653d4eSeschrock 					nvlist_free(ce->ce_config);
127199653d4eSeschrock 				free(ce);
127299653d4eSeschrock 			}
127399653d4eSeschrock 			free(ve);
127499653d4eSeschrock 		}
127599653d4eSeschrock 		free(pe);
127699653d4eSeschrock 	}
127799653d4eSeschrock 
127899653d4eSeschrock 	for (ne = pools.names; ne != NULL; ne = nenext) {
127999653d4eSeschrock 		nenext = ne->ne_next;
128099653d4eSeschrock 		if (ne->ne_name)
128199653d4eSeschrock 			free(ne->ne_name);
128299653d4eSeschrock 		free(ne);
128399653d4eSeschrock 	}
128499653d4eSeschrock 
1285ccae0b50Seschrock 	if (dirp)
1286ccae0b50Seschrock 		(void) closedir(dirp);
1287fa9e4066Sahrens 
1288fa9e4066Sahrens 	return (ret);
1289fa9e4066Sahrens }
1290fa9e4066Sahrens 
129124e697d4Sck nvlist_t *
129224e697d4Sck zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
129324e697d4Sck {
129424e697d4Sck 	return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, NULL, 0));
129524e697d4Sck }
129624e697d4Sck 
129724e697d4Sck nvlist_t *
129824e697d4Sck zpool_find_import_byname(libzfs_handle_t *hdl, int argc, char **argv,
129924e697d4Sck     char *pool)
130024e697d4Sck {
130124e697d4Sck 	return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, pool, 0));
130224e697d4Sck }
130324e697d4Sck 
130424e697d4Sck nvlist_t *
130524e697d4Sck zpool_find_import_byguid(libzfs_handle_t *hdl, int argc, char **argv,
130624e697d4Sck     uint64_t guid)
130724e697d4Sck {
130824e697d4Sck 	return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, NULL, guid));
130924e697d4Sck }
131024e697d4Sck 
131124e697d4Sck nvlist_t *
131224e697d4Sck zpool_find_import_activeok(libzfs_handle_t *hdl, int argc, char **argv)
131324e697d4Sck {
131424e697d4Sck 	return (zpool_find_import_impl(hdl, argc, argv, B_TRUE, NULL, 0));
131524e697d4Sck }
131624e697d4Sck 
13172f8aaab3Seschrock /*
13182f8aaab3Seschrock  * Given a cache file, return the contents as a list of importable pools.
131924e697d4Sck  * poolname or guid (but not both) are provided by the caller when trying
132024e697d4Sck  * to import a specific pool.
13212f8aaab3Seschrock  */
13222f8aaab3Seschrock nvlist_t *
13233a57275aSck zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
1324e829d913Sck     char *poolname, uint64_t guid)
13252f8aaab3Seschrock {
13262f8aaab3Seschrock 	char *buf;
13272f8aaab3Seschrock 	int fd;
13282f8aaab3Seschrock 	struct stat64 statbuf;
13292f8aaab3Seschrock 	nvlist_t *raw, *src, *dst;
13302f8aaab3Seschrock 	nvlist_t *pools;
13312f8aaab3Seschrock 	nvpair_t *elem;
13322f8aaab3Seschrock 	char *name;
133324e697d4Sck 	uint64_t this_guid;
13342f8aaab3Seschrock 	boolean_t active;
13352f8aaab3Seschrock 
133624e697d4Sck 	verify(poolname == NULL || guid == 0);
133724e697d4Sck 
13382f8aaab3Seschrock 	if ((fd = open(cachefile, O_RDONLY)) < 0) {
13392f8aaab3Seschrock 		zfs_error_aux(hdl, "%s", strerror(errno));
13402f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13412f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN, "failed to open cache file"));
13422f8aaab3Seschrock 		return (NULL);
13432f8aaab3Seschrock 	}
13442f8aaab3Seschrock 
13452f8aaab3Seschrock 	if (fstat64(fd, &statbuf) != 0) {
13462f8aaab3Seschrock 		zfs_error_aux(hdl, "%s", strerror(errno));
13472f8aaab3Seschrock 		(void) close(fd);
13482f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13492f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
13502f8aaab3Seschrock 		return (NULL);
13512f8aaab3Seschrock 	}
13522f8aaab3Seschrock 
13532f8aaab3Seschrock 	if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
13542f8aaab3Seschrock 		(void) close(fd);
13552f8aaab3Seschrock 		return (NULL);
13562f8aaab3Seschrock 	}
13572f8aaab3Seschrock 
13582f8aaab3Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
13592f8aaab3Seschrock 		(void) close(fd);
13602f8aaab3Seschrock 		free(buf);
13612f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13622f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN,
13632f8aaab3Seschrock 		    "failed to read cache file contents"));
13642f8aaab3Seschrock 		return (NULL);
13652f8aaab3Seschrock 	}
13662f8aaab3Seschrock 
13672f8aaab3Seschrock 	(void) close(fd);
13682f8aaab3Seschrock 
13692f8aaab3Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
13702f8aaab3Seschrock 		free(buf);
13712f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13722f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN,
13732f8aaab3Seschrock 		    "invalid or corrupt cache file contents"));
13742f8aaab3Seschrock 		return (NULL);
13752f8aaab3Seschrock 	}
13762f8aaab3Seschrock 
13772f8aaab3Seschrock 	free(buf);
13782f8aaab3Seschrock 
13792f8aaab3Seschrock 	/*
13802f8aaab3Seschrock 	 * Go through and get the current state of the pools and refresh their
13812f8aaab3Seschrock 	 * state.
13822f8aaab3Seschrock 	 */
13832f8aaab3Seschrock 	if (nvlist_alloc(&pools, 0, 0) != 0) {
13842f8aaab3Seschrock 		(void) no_memory(hdl);
13852f8aaab3Seschrock 		nvlist_free(raw);
13862f8aaab3Seschrock 		return (NULL);
13872f8aaab3Seschrock 	}
13882f8aaab3Seschrock 
13892f8aaab3Seschrock 	elem = NULL;
13902f8aaab3Seschrock 	while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
13912f8aaab3Seschrock 		verify(nvpair_value_nvlist(elem, &src) == 0);
13922f8aaab3Seschrock 
13932f8aaab3Seschrock 		verify(nvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME,
13942f8aaab3Seschrock 		    &name) == 0);
139524e697d4Sck 		if (poolname != NULL && strcmp(poolname, name) != 0)
139624e697d4Sck 			continue;
139724e697d4Sck 
13982f8aaab3Seschrock 		verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID,
139924e697d4Sck 		    &this_guid) == 0);
140024e697d4Sck 		if (guid != 0) {
140124e697d4Sck 			verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID,
140224e697d4Sck 			    &this_guid) == 0);
140324e697d4Sck 			if (guid != this_guid)
140424e697d4Sck 				continue;
140524e697d4Sck 		}
14062f8aaab3Seschrock 
1407e829d913Sck 		if (pool_active(hdl, name, this_guid, &active) != 0) {
1408e829d913Sck 			nvlist_free(raw);
1409e829d913Sck 			nvlist_free(pools);
1410e829d913Sck 			return (NULL);
1411e829d913Sck 		}
14122f8aaab3Seschrock 
1413e829d913Sck 		if (active)
1414e829d913Sck 			continue;
14152f8aaab3Seschrock 
1416e829d913Sck 		if ((dst = refresh_config(hdl, src)) == NULL) {
1417e829d913Sck 			nvlist_free(raw);
1418e829d913Sck 			nvlist_free(pools);
1419e829d913Sck 			return (NULL);
1420e829d913Sck 		}
14212f8aaab3Seschrock 
1422e829d913Sck 		if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1423e829d913Sck 			(void) no_memory(hdl);
14242f8aaab3Seschrock 			nvlist_free(dst);
1425e829d913Sck 			nvlist_free(raw);
1426e829d913Sck 			nvlist_free(pools);
1427e829d913Sck 			return (NULL);
14282f8aaab3Seschrock 		}
1429e829d913Sck 		nvlist_free(dst);
14302f8aaab3Seschrock 	}
14312f8aaab3Seschrock 
14322f8aaab3Seschrock 	nvlist_free(raw);
14332f8aaab3Seschrock 	return (pools);
14342f8aaab3Seschrock }
14352f8aaab3Seschrock 
14362f8aaab3Seschrock 
143799653d4eSeschrock boolean_t
1438fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid)
1439fa9e4066Sahrens {
1440fa9e4066Sahrens 	uint64_t tmp;
1441fa9e4066Sahrens 	nvlist_t **child;
1442fa9e4066Sahrens 	uint_t c, children;
1443fa9e4066Sahrens 
1444fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
1445fa9e4066Sahrens 	if (tmp == guid)
144699653d4eSeschrock 		return (B_TRUE);
1447fa9e4066Sahrens 
1448fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1449fa9e4066Sahrens 	    &child, &children) == 0) {
1450fa9e4066Sahrens 		for (c = 0; c < children; c++)
1451fa9e4066Sahrens 			if (find_guid(child[c], guid))
145299653d4eSeschrock 				return (B_TRUE);
145399653d4eSeschrock 	}
145499653d4eSeschrock 
145599653d4eSeschrock 	return (B_FALSE);
145699653d4eSeschrock }
145799653d4eSeschrock 
1458fa94a07fSbrendan typedef struct aux_cbdata {
1459fa94a07fSbrendan 	const char	*cb_type;
146099653d4eSeschrock 	uint64_t	cb_guid;
146199653d4eSeschrock 	zpool_handle_t	*cb_zhp;
1462fa94a07fSbrendan } aux_cbdata_t;
146399653d4eSeschrock 
146499653d4eSeschrock static int
1465fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data)
146699653d4eSeschrock {
1467fa94a07fSbrendan 	aux_cbdata_t *cbp = data;
1468fa94a07fSbrendan 	nvlist_t **list;
1469fa94a07fSbrendan 	uint_t i, count;
147099653d4eSeschrock 	uint64_t guid;
147199653d4eSeschrock 	nvlist_t *nvroot;
147299653d4eSeschrock 
147399653d4eSeschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
147499653d4eSeschrock 	    &nvroot) == 0);
147599653d4eSeschrock 
1476fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
1477fa94a07fSbrendan 	    &list, &count) == 0) {
1478fa94a07fSbrendan 		for (i = 0; i < count; i++) {
1479fa94a07fSbrendan 			verify(nvlist_lookup_uint64(list[i],
148099653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
148199653d4eSeschrock 			if (guid == cbp->cb_guid) {
148299653d4eSeschrock 				cbp->cb_zhp = zhp;
148399653d4eSeschrock 				return (1);
148499653d4eSeschrock 			}
148599653d4eSeschrock 		}
1486fa9e4066Sahrens 	}
1487fa9e4066Sahrens 
148899653d4eSeschrock 	zpool_close(zhp);
148999653d4eSeschrock 	return (0);
1490fa9e4066Sahrens }
1491fa9e4066Sahrens 
1492fa9e4066Sahrens /*
149399653d4eSeschrock  * Determines if the pool is in use.  If so, it returns true and the state of
1494fa9e4066Sahrens  * the pool as well as the name of the pool.  Both strings are allocated and
1495fa9e4066Sahrens  * must be freed by the caller.
1496fa9e4066Sahrens  */
1497fa9e4066Sahrens int
149899653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
149999653d4eSeschrock     boolean_t *inuse)
1500fa9e4066Sahrens {
1501fa9e4066Sahrens 	nvlist_t *config;
1502fa9e4066Sahrens 	char *name;
150399653d4eSeschrock 	boolean_t ret;
1504fa9e4066Sahrens 	uint64_t guid, vdev_guid;
1505fa9e4066Sahrens 	zpool_handle_t *zhp;
1506fa9e4066Sahrens 	nvlist_t *pool_config;
150739c23413Seschrock 	uint64_t stateval, isspare;
1508fa94a07fSbrendan 	aux_cbdata_t cb = { 0 };
150994de1d4cSeschrock 	boolean_t isactive;
151099653d4eSeschrock 
151199653d4eSeschrock 	*inuse = B_FALSE;
1512fa9e4066Sahrens 
151399653d4eSeschrock 	if (zpool_read_label(fd, &config) != 0) {
151499653d4eSeschrock 		(void) no_memory(hdl);
151599653d4eSeschrock 		return (-1);
151699653d4eSeschrock 	}
151799653d4eSeschrock 
151899653d4eSeschrock 	if (config == NULL)
151999653d4eSeschrock 		return (0);
1520fa9e4066Sahrens 
1521fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
152246a2abf2Seschrock 	    &stateval) == 0);
1523fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
1524fa9e4066Sahrens 	    &vdev_guid) == 0);
1525fa9e4066Sahrens 
1526fa94a07fSbrendan 	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
152799653d4eSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
152899653d4eSeschrock 		    &name) == 0);
152999653d4eSeschrock 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
153099653d4eSeschrock 		    &guid) == 0);
153199653d4eSeschrock 	}
153299653d4eSeschrock 
153346a2abf2Seschrock 	switch (stateval) {
1534fa9e4066Sahrens 	case POOL_STATE_EXPORTED:
153599653d4eSeschrock 		ret = B_TRUE;
1536fa9e4066Sahrens 		break;
1537fa9e4066Sahrens 
1538fa9e4066Sahrens 	case POOL_STATE_ACTIVE:
1539fa9e4066Sahrens 		/*
1540fa9e4066Sahrens 		 * For an active pool, we have to determine if it's really part
1541eaca9bbdSeschrock 		 * of a currently active pool (in which case the pool will exist
1542eaca9bbdSeschrock 		 * and the guid will be the same), or whether it's part of an
1543eaca9bbdSeschrock 		 * active pool that was disconnected without being explicitly
1544eaca9bbdSeschrock 		 * exported.
1545fa9e4066Sahrens 		 */
154694de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0) {
154794de1d4cSeschrock 			nvlist_free(config);
154894de1d4cSeschrock 			return (-1);
154994de1d4cSeschrock 		}
155094de1d4cSeschrock 
155194de1d4cSeschrock 		if (isactive) {
1552fa9e4066Sahrens 			/*
1553fa9e4066Sahrens 			 * Because the device may have been removed while
1554fa9e4066Sahrens 			 * offlined, we only report it as active if the vdev is
1555fa9e4066Sahrens 			 * still present in the config.  Otherwise, pretend like
1556fa9e4066Sahrens 			 * it's not in use.
1557fa9e4066Sahrens 			 */
155899653d4eSeschrock 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
1559088e9d47Seschrock 			    (pool_config = zpool_get_config(zhp, NULL))
1560088e9d47Seschrock 			    != NULL) {
1561fa9e4066Sahrens 				nvlist_t *nvroot;
1562fa9e4066Sahrens 
1563fa9e4066Sahrens 				verify(nvlist_lookup_nvlist(pool_config,
1564fa9e4066Sahrens 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
156546a2abf2Seschrock 				ret = find_guid(nvroot, vdev_guid);
1566fa9e4066Sahrens 			} else {
156799653d4eSeschrock 				ret = B_FALSE;
1568fa9e4066Sahrens 			}
156999653d4eSeschrock 
157039c23413Seschrock 			/*
157139c23413Seschrock 			 * If this is an active spare within another pool, we
157239c23413Seschrock 			 * treat it like an unused hot spare.  This allows the
157339c23413Seschrock 			 * user to create a pool with a hot spare that currently
157439c23413Seschrock 			 * in use within another pool.  Since we return B_TRUE,
157539c23413Seschrock 			 * libdiskmgt will continue to prevent generic consumers
157639c23413Seschrock 			 * from using the device.
157739c23413Seschrock 			 */
157839c23413Seschrock 			if (ret && nvlist_lookup_uint64(config,
157939c23413Seschrock 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
158039c23413Seschrock 				stateval = POOL_STATE_SPARE;
158139c23413Seschrock 
158299653d4eSeschrock 			if (zhp != NULL)
158399653d4eSeschrock 				zpool_close(zhp);
1584fa9e4066Sahrens 		} else {
158546a2abf2Seschrock 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
158699653d4eSeschrock 			ret = B_TRUE;
158799653d4eSeschrock 		}
158899653d4eSeschrock 		break;
158999653d4eSeschrock 
159099653d4eSeschrock 	case POOL_STATE_SPARE:
159199653d4eSeschrock 		/*
159299653d4eSeschrock 		 * For a hot spare, it can be either definitively in use, or
159399653d4eSeschrock 		 * potentially active.  To determine if it's in use, we iterate
159499653d4eSeschrock 		 * over all pools in the system and search for one with a spare
159599653d4eSeschrock 		 * with a matching guid.
159699653d4eSeschrock 		 *
159799653d4eSeschrock 		 * Due to the shared nature of spares, we don't actually report
159899653d4eSeschrock 		 * the potentially active case as in use.  This means the user
159999653d4eSeschrock 		 * can freely create pools on the hot spares of exported pools,
160099653d4eSeschrock 		 * but to do otherwise makes the resulting code complicated, and
160199653d4eSeschrock 		 * we end up having to deal with this case anyway.
160299653d4eSeschrock 		 */
160399653d4eSeschrock 		cb.cb_zhp = NULL;
160499653d4eSeschrock 		cb.cb_guid = vdev_guid;
1605fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_SPARES;
1606fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
1607fa94a07fSbrendan 			name = (char *)zpool_get_name(cb.cb_zhp);
1608fa94a07fSbrendan 			ret = TRUE;
1609fa94a07fSbrendan 		} else {
1610fa94a07fSbrendan 			ret = FALSE;
1611fa94a07fSbrendan 		}
1612fa94a07fSbrendan 		break;
1613fa94a07fSbrendan 
1614fa94a07fSbrendan 	case POOL_STATE_L2CACHE:
1615fa94a07fSbrendan 
1616fa94a07fSbrendan 		/*
1617fa94a07fSbrendan 		 * Check if any pool is currently using this l2cache device.
1618fa94a07fSbrendan 		 */
1619fa94a07fSbrendan 		cb.cb_zhp = NULL;
1620fa94a07fSbrendan 		cb.cb_guid = vdev_guid;
1621fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
1622fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
162399653d4eSeschrock 			name = (char *)zpool_get_name(cb.cb_zhp);
1624fa9e4066Sahrens 			ret = TRUE;
162599653d4eSeschrock 		} else {
162699653d4eSeschrock 			ret = FALSE;
1627fa9e4066Sahrens 		}
1628fa9e4066Sahrens 		break;
1629fa9e4066Sahrens 
1630fa9e4066Sahrens 	default:
163199653d4eSeschrock 		ret = B_FALSE;
1632fa9e4066Sahrens 	}
1633fa9e4066Sahrens 
163446a2abf2Seschrock 
163546a2abf2Seschrock 	if (ret) {
163699653d4eSeschrock 		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
1637c6765aabSeschrock 			if (cb.cb_zhp)
1638c6765aabSeschrock 				zpool_close(cb.cb_zhp);
163999653d4eSeschrock 			nvlist_free(config);
164099653d4eSeschrock 			return (-1);
164199653d4eSeschrock 		}
164246a2abf2Seschrock 		*state = (pool_state_t)stateval;
164346a2abf2Seschrock 	}
164446a2abf2Seschrock 
164599653d4eSeschrock 	if (cb.cb_zhp)
164699653d4eSeschrock 		zpool_close(cb.cb_zhp);
164799653d4eSeschrock 
1648fa9e4066Sahrens 	nvlist_free(config);
164999653d4eSeschrock 	*inuse = ret;
165099653d4eSeschrock 	return (0);
1651fa9e4066Sahrens }
1652