xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_import.c (revision 8b65a70b763232c90a91f31eb2010314c02ed338)
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  */
21078266a5SMarcel Telka 
22fa9e4066Sahrens /*
23f9af39baSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*8b65a70bSPavel Zakharov  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
25bd0f7091SAndrew Stormont  * Copyright 2015 RackTop Systems.
266401734dSWill Andrews  * Copyright 2016 Nexenta Systems, Inc.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * Pool import support functions.
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * To import a pool, we rely on reading the configuration information from the
33fa9e4066Sahrens  * ZFS label of each device.  If we successfully read the label, then we
34fa9e4066Sahrens  * organize the configuration information in the following hierarchy:
35fa9e4066Sahrens  *
36fa9e4066Sahrens  * 	pool guid -> toplevel vdev guid -> label txg
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * Duplicate entries matching this same tuple will be discarded.  Once we have
39fa9e4066Sahrens  * examined every device, we pick the best label txg config for each toplevel
40fa9e4066Sahrens  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
41fa9e4066Sahrens  * update any paths that have changed.  Finally, we attempt to import the pool
42fa9e4066Sahrens  * using our derived config, and record the results.
43fa9e4066Sahrens  */
44fa9e4066Sahrens 
454f67d755SEric Taylor #include <ctype.h>
46fa9e4066Sahrens #include <devid.h>
47fa9e4066Sahrens #include <dirent.h>
48fa9e4066Sahrens #include <errno.h>
49fa9e4066Sahrens #include <libintl.h>
504f67d755SEric Taylor #include <stddef.h>
51fa9e4066Sahrens #include <stdlib.h>
52fa9e4066Sahrens #include <string.h>
53fa9e4066Sahrens #include <sys/stat.h>
54fa9e4066Sahrens #include <unistd.h>
55fa9e4066Sahrens #include <fcntl.h>
564f67d755SEric Taylor #include <sys/vtoc.h>
574f67d755SEric Taylor #include <sys/dktp/fdisk.h>
584f67d755SEric Taylor #include <sys/efi_partition.h>
594f67d755SEric Taylor #include <thread_pool.h>
60fa9e4066Sahrens 
61fa9e4066Sahrens #include <sys/vdev_impl.h>
62fa9e4066Sahrens 
63fa9e4066Sahrens #include "libzfs.h"
64fa9e4066Sahrens #include "libzfs_impl.h"
65fa9e4066Sahrens 
66fa9e4066Sahrens /*
67fa9e4066Sahrens  * Intermediate structures used to gather configuration information.
68fa9e4066Sahrens  */
69fa9e4066Sahrens typedef struct config_entry {
70fa9e4066Sahrens 	uint64_t		ce_txg;
71fa9e4066Sahrens 	nvlist_t		*ce_config;
72fa9e4066Sahrens 	struct config_entry	*ce_next;
73fa9e4066Sahrens } config_entry_t;
74fa9e4066Sahrens 
75fa9e4066Sahrens typedef struct vdev_entry {
76fa9e4066Sahrens 	uint64_t		ve_guid;
77fa9e4066Sahrens 	config_entry_t		*ve_configs;
78fa9e4066Sahrens 	struct vdev_entry	*ve_next;
79fa9e4066Sahrens } vdev_entry_t;
80fa9e4066Sahrens 
81fa9e4066Sahrens typedef struct pool_entry {
82fa9e4066Sahrens 	uint64_t		pe_guid;
83fa9e4066Sahrens 	vdev_entry_t		*pe_vdevs;
84fa9e4066Sahrens 	struct pool_entry	*pe_next;
85fa9e4066Sahrens } pool_entry_t;
86fa9e4066Sahrens 
87fa9e4066Sahrens typedef struct name_entry {
8899653d4eSeschrock 	char			*ne_name;
89fa9e4066Sahrens 	uint64_t		ne_guid;
90fa9e4066Sahrens 	struct name_entry	*ne_next;
91fa9e4066Sahrens } name_entry_t;
92fa9e4066Sahrens 
93fa9e4066Sahrens typedef struct pool_list {
94fa9e4066Sahrens 	pool_entry_t		*pools;
95fa9e4066Sahrens 	name_entry_t		*names;
96fa9e4066Sahrens } pool_list_t;
97fa9e4066Sahrens 
98fa9e4066Sahrens static char *
99fa9e4066Sahrens get_devid(const char *path)
100fa9e4066Sahrens {
101fa9e4066Sahrens 	int fd;
102fa9e4066Sahrens 	ddi_devid_t devid;
103fa9e4066Sahrens 	char *minor, *ret;
104fa9e4066Sahrens 
105fa9e4066Sahrens 	if ((fd = open(path, O_RDONLY)) < 0)
106fa9e4066Sahrens 		return (NULL);
107fa9e4066Sahrens 
108fa9e4066Sahrens 	minor = NULL;
109fa9e4066Sahrens 	ret = NULL;
110fa9e4066Sahrens 	if (devid_get(fd, &devid) == 0) {
111fa9e4066Sahrens 		if (devid_get_minor_name(fd, &minor) == 0)
112fa9e4066Sahrens 			ret = devid_str_encode(devid, minor);
113fa9e4066Sahrens 		if (minor != NULL)
114fa9e4066Sahrens 			devid_str_free(minor);
115fa9e4066Sahrens 		devid_free(devid);
116fa9e4066Sahrens 	}
117c67d9675Seschrock 	(void) close(fd);
118fa9e4066Sahrens 
119fa9e4066Sahrens 	return (ret);
120fa9e4066Sahrens }
121fa9e4066Sahrens 
122fa9e4066Sahrens 
123fa9e4066Sahrens /*
124fa9e4066Sahrens  * Go through and fix up any path and/or devid information for the given vdev
125fa9e4066Sahrens  * configuration.
126fa9e4066Sahrens  */
12799653d4eSeschrock static int
128fa9e4066Sahrens fix_paths(nvlist_t *nv, name_entry_t *names)
129fa9e4066Sahrens {
130fa9e4066Sahrens 	nvlist_t **child;
131fa9e4066Sahrens 	uint_t c, children;
132fa9e4066Sahrens 	uint64_t guid;
133c67d9675Seschrock 	name_entry_t *ne, *best;
134c67d9675Seschrock 	char *path, *devid;
135c67d9675Seschrock 	int matched;
136fa9e4066Sahrens 
137fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
138fa9e4066Sahrens 	    &child, &children) == 0) {
139fa9e4066Sahrens 		for (c = 0; c < children; c++)
14099653d4eSeschrock 			if (fix_paths(child[c], names) != 0)
14199653d4eSeschrock 				return (-1);
14299653d4eSeschrock 		return (0);
143fa9e4066Sahrens 	}
144fa9e4066Sahrens 
145fa9e4066Sahrens 	/*
146fa9e4066Sahrens 	 * This is a leaf (file or disk) vdev.  In either case, go through
147fa9e4066Sahrens 	 * the name list and see if we find a matching guid.  If so, replace
148fa9e4066Sahrens 	 * the path and see if we can calculate a new devid.
149c67d9675Seschrock 	 *
150c67d9675Seschrock 	 * There may be multiple names associated with a particular guid, in
151c67d9675Seschrock 	 * which case we have overlapping slices or multiple paths to the same
152c67d9675Seschrock 	 * disk.  If this is the case, then we want to pick the path that is
153c67d9675Seschrock 	 * the most similar to the original, where "most similar" is the number
154c67d9675Seschrock 	 * of matching characters starting from the end of the path.  This will
155c67d9675Seschrock 	 * preserve slice numbers even if the disks have been reorganized, and
156c67d9675Seschrock 	 * will also catch preferred disk names if multiple paths exist.
157fa9e4066Sahrens 	 */
158fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
159c67d9675Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
160c67d9675Seschrock 		path = NULL;
161c67d9675Seschrock 
162c67d9675Seschrock 	matched = 0;
163c67d9675Seschrock 	best = NULL;
164c67d9675Seschrock 	for (ne = names; ne != NULL; ne = ne->ne_next) {
165c67d9675Seschrock 		if (ne->ne_guid == guid) {
166c67d9675Seschrock 			const char *src, *dst;
167c67d9675Seschrock 			int count;
168c67d9675Seschrock 
169c67d9675Seschrock 			if (path == NULL) {
170c67d9675Seschrock 				best = ne;
171c67d9675Seschrock 				break;
172c67d9675Seschrock 			}
173c67d9675Seschrock 
174c67d9675Seschrock 			src = ne->ne_name + strlen(ne->ne_name) - 1;
175c67d9675Seschrock 			dst = path + strlen(path) - 1;
176c67d9675Seschrock 			for (count = 0; src >= ne->ne_name && dst >= path;
177c67d9675Seschrock 			    src--, dst--, count++)
178c67d9675Seschrock 				if (*src != *dst)
179c67d9675Seschrock 					break;
180c67d9675Seschrock 
181c67d9675Seschrock 			/*
182c67d9675Seschrock 			 * At this point, 'count' is the number of characters
183c67d9675Seschrock 			 * matched from the end.
184c67d9675Seschrock 			 */
185c67d9675Seschrock 			if (count > matched || best == NULL) {
186c67d9675Seschrock 				best = ne;
187c67d9675Seschrock 				matched = count;
188c67d9675Seschrock 			}
189c67d9675Seschrock 		}
190c67d9675Seschrock 	}
191fa9e4066Sahrens 
192c67d9675Seschrock 	if (best == NULL)
19399653d4eSeschrock 		return (0);
194fa9e4066Sahrens 
19599653d4eSeschrock 	if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
19699653d4eSeschrock 		return (-1);
197fa9e4066Sahrens 
198c67d9675Seschrock 	if ((devid = get_devid(best->ne_name)) == NULL) {
199fa9e4066Sahrens 		(void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
200fa9e4066Sahrens 	} else {
201078266a5SMarcel Telka 		if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0) {
202078266a5SMarcel Telka 			devid_str_free(devid);
20399653d4eSeschrock 			return (-1);
204078266a5SMarcel Telka 		}
205fa9e4066Sahrens 		devid_str_free(devid);
206fa9e4066Sahrens 	}
20799653d4eSeschrock 
20899653d4eSeschrock 	return (0);
209fa9e4066Sahrens }
210fa9e4066Sahrens 
211fa9e4066Sahrens /*
212fa9e4066Sahrens  * Add the given configuration to the list of known devices.
213fa9e4066Sahrens  */
21499653d4eSeschrock static int
21599653d4eSeschrock add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
21699653d4eSeschrock     nvlist_t *config)
217fa9e4066Sahrens {
21899653d4eSeschrock 	uint64_t pool_guid, vdev_guid, top_guid, txg, state;
219fa9e4066Sahrens 	pool_entry_t *pe;
220fa9e4066Sahrens 	vdev_entry_t *ve;
221fa9e4066Sahrens 	config_entry_t *ce;
222fa9e4066Sahrens 	name_entry_t *ne;
223fa9e4066Sahrens 
22499653d4eSeschrock 	/*
225fa94a07fSbrendan 	 * If this is a hot spare not currently in use or level 2 cache
226fa94a07fSbrendan 	 * device, add it to the list of names to translate, but don't do
227fa94a07fSbrendan 	 * anything else.
22899653d4eSeschrock 	 */
22999653d4eSeschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
230fa94a07fSbrendan 	    &state) == 0 &&
231fa94a07fSbrendan 	    (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
23299653d4eSeschrock 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
23399653d4eSeschrock 		if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
234ccae0b50Seschrock 			return (-1);
23599653d4eSeschrock 
23699653d4eSeschrock 		if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
23799653d4eSeschrock 			free(ne);
23899653d4eSeschrock 			return (-1);
23999653d4eSeschrock 		}
24010568655SYuri Pankov 
24199653d4eSeschrock 		ne->ne_guid = vdev_guid;
24299653d4eSeschrock 		ne->ne_next = pl->names;
24399653d4eSeschrock 		pl->names = ne;
24410568655SYuri Pankov 
24510568655SYuri Pankov 		nvlist_free(config);
24699653d4eSeschrock 		return (0);
24799653d4eSeschrock 	}
24899653d4eSeschrock 
249fa9e4066Sahrens 	/*
250fa9e4066Sahrens 	 * If we have a valid config but cannot read any of these fields, then
251fa9e4066Sahrens 	 * it means we have a half-initialized label.  In vdev_label_init()
252fa9e4066Sahrens 	 * we write a label with txg == 0 so that we can identify the device
253fa9e4066Sahrens 	 * in case the user refers to the same disk later on.  If we fail to
254fa9e4066Sahrens 	 * create the pool, we'll be left with a label in this state
255fa9e4066Sahrens 	 * which should not be considered part of a valid pool.
256fa9e4066Sahrens 	 */
257fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
258fa9e4066Sahrens 	    &pool_guid) != 0 ||
259fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
260fa9e4066Sahrens 	    &vdev_guid) != 0 ||
261fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
262fa9e4066Sahrens 	    &top_guid) != 0 ||
263fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
264fa9e4066Sahrens 	    &txg) != 0 || txg == 0) {
265fa9e4066Sahrens 		nvlist_free(config);
26699653d4eSeschrock 		return (0);
267fa9e4066Sahrens 	}
268fa9e4066Sahrens 
269fa9e4066Sahrens 	/*
270fa9e4066Sahrens 	 * First, see if we know about this pool.  If not, then add it to the
271fa9e4066Sahrens 	 * list of known pools.
272fa9e4066Sahrens 	 */
273fa9e4066Sahrens 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
274fa9e4066Sahrens 		if (pe->pe_guid == pool_guid)
275fa9e4066Sahrens 			break;
276fa9e4066Sahrens 	}
277fa9e4066Sahrens 
278fa9e4066Sahrens 	if (pe == NULL) {
27999653d4eSeschrock 		if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
28099653d4eSeschrock 			nvlist_free(config);
28199653d4eSeschrock 			return (-1);
28299653d4eSeschrock 		}
283fa9e4066Sahrens 		pe->pe_guid = pool_guid;
284fa9e4066Sahrens 		pe->pe_next = pl->pools;
285fa9e4066Sahrens 		pl->pools = pe;
286fa9e4066Sahrens 	}
287fa9e4066Sahrens 
288fa9e4066Sahrens 	/*
289fa9e4066Sahrens 	 * Second, see if we know about this toplevel vdev.  Add it if its
290fa9e4066Sahrens 	 * missing.
291fa9e4066Sahrens 	 */
292fa9e4066Sahrens 	for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
293fa9e4066Sahrens 		if (ve->ve_guid == top_guid)
294fa9e4066Sahrens 			break;
295fa9e4066Sahrens 	}
296fa9e4066Sahrens 
297fa9e4066Sahrens 	if (ve == NULL) {
29899653d4eSeschrock 		if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
29999653d4eSeschrock 			nvlist_free(config);
30099653d4eSeschrock 			return (-1);
30199653d4eSeschrock 		}
302fa9e4066Sahrens 		ve->ve_guid = top_guid;
303fa9e4066Sahrens 		ve->ve_next = pe->pe_vdevs;
304fa9e4066Sahrens 		pe->pe_vdevs = ve;
305fa9e4066Sahrens 	}
306fa9e4066Sahrens 
307fa9e4066Sahrens 	/*
308fa9e4066Sahrens 	 * Third, see if we have a config with a matching transaction group.  If
309fa9e4066Sahrens 	 * so, then we do nothing.  Otherwise, add it to the list of known
310fa9e4066Sahrens 	 * configs.
311fa9e4066Sahrens 	 */
312fa9e4066Sahrens 	for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
313fa9e4066Sahrens 		if (ce->ce_txg == txg)
314fa9e4066Sahrens 			break;
315fa9e4066Sahrens 	}
316fa9e4066Sahrens 
317fa9e4066Sahrens 	if (ce == NULL) {
31899653d4eSeschrock 		if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
31999653d4eSeschrock 			nvlist_free(config);
32099653d4eSeschrock 			return (-1);
32199653d4eSeschrock 		}
322fa9e4066Sahrens 		ce->ce_txg = txg;
323fa9e4066Sahrens 		ce->ce_config = config;
324fa9e4066Sahrens 		ce->ce_next = ve->ve_configs;
325fa9e4066Sahrens 		ve->ve_configs = ce;
326fa9e4066Sahrens 	} else {
327fa9e4066Sahrens 		nvlist_free(config);
328fa9e4066Sahrens 	}
329fa9e4066Sahrens 
330fa9e4066Sahrens 	/*
331fa9e4066Sahrens 	 * At this point we've successfully added our config to the list of
332fa9e4066Sahrens 	 * known configs.  The last thing to do is add the vdev guid -> path
333fa9e4066Sahrens 	 * mappings so that we can fix up the configuration as necessary before
334fa9e4066Sahrens 	 * doing the import.
335fa9e4066Sahrens 	 */
33699653d4eSeschrock 	if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
33799653d4eSeschrock 		return (-1);
33899653d4eSeschrock 
33999653d4eSeschrock 	if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
34099653d4eSeschrock 		free(ne);
34199653d4eSeschrock 		return (-1);
34299653d4eSeschrock 	}
343fa9e4066Sahrens 
344fa9e4066Sahrens 	ne->ne_guid = vdev_guid;
345fa9e4066Sahrens 	ne->ne_next = pl->names;
346fa9e4066Sahrens 	pl->names = ne;
34799653d4eSeschrock 
34899653d4eSeschrock 	return (0);
349fa9e4066Sahrens }
350fa9e4066Sahrens 
351eaca9bbdSeschrock /*
352eaca9bbdSeschrock  * Returns true if the named pool matches the given GUID.
353eaca9bbdSeschrock  */
35494de1d4cSeschrock static int
35594de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
35694de1d4cSeschrock     boolean_t *isactive)
357eaca9bbdSeschrock {
358eaca9bbdSeschrock 	zpool_handle_t *zhp;
359eaca9bbdSeschrock 	uint64_t theguid;
360eaca9bbdSeschrock 
36194de1d4cSeschrock 	if (zpool_open_silent(hdl, name, &zhp) != 0)
36294de1d4cSeschrock 		return (-1);
36394de1d4cSeschrock 
36494de1d4cSeschrock 	if (zhp == NULL) {
36594de1d4cSeschrock 		*isactive = B_FALSE;
36694de1d4cSeschrock 		return (0);
36794de1d4cSeschrock 	}
368eaca9bbdSeschrock 
369eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
370eaca9bbdSeschrock 	    &theguid) == 0);
371eaca9bbdSeschrock 
372eaca9bbdSeschrock 	zpool_close(zhp);
373eaca9bbdSeschrock 
37494de1d4cSeschrock 	*isactive = (theguid == guid);
37594de1d4cSeschrock 	return (0);
376eaca9bbdSeschrock }
377eaca9bbdSeschrock 
3782f8aaab3Seschrock static nvlist_t *
3792f8aaab3Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
3802f8aaab3Seschrock {
3812f8aaab3Seschrock 	nvlist_t *nvl;
3822f8aaab3Seschrock 	zfs_cmd_t zc = { 0 };
383*8b65a70bSPavel Zakharov 	int err, dstbuf_size;
3842f8aaab3Seschrock 
3852f8aaab3Seschrock 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
3862f8aaab3Seschrock 		return (NULL);
3872f8aaab3Seschrock 
388*8b65a70bSPavel Zakharov 	dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4);
389*8b65a70bSPavel Zakharov 
390*8b65a70bSPavel Zakharov 	if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
3912f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
3922f8aaab3Seschrock 		return (NULL);
3932f8aaab3Seschrock 	}
3942f8aaab3Seschrock 
3952f8aaab3Seschrock 	while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
3962f8aaab3Seschrock 	    &zc)) != 0 && errno == ENOMEM) {
3972f8aaab3Seschrock 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3982f8aaab3Seschrock 			zcmd_free_nvlists(&zc);
3992f8aaab3Seschrock 			return (NULL);
4002f8aaab3Seschrock 		}
4012f8aaab3Seschrock 	}
4022f8aaab3Seschrock 
4032f8aaab3Seschrock 	if (err) {
4042f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
4052f8aaab3Seschrock 		return (NULL);
4062f8aaab3Seschrock 	}
4072f8aaab3Seschrock 
4082f8aaab3Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
4092f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
4102f8aaab3Seschrock 		return (NULL);
4112f8aaab3Seschrock 	}
4122f8aaab3Seschrock 
4132f8aaab3Seschrock 	zcmd_free_nvlists(&zc);
4142f8aaab3Seschrock 	return (nvl);
4152f8aaab3Seschrock }
4162f8aaab3Seschrock 
41788ecc943SGeorge Wilson /*
41888ecc943SGeorge Wilson  * Determine if the vdev id is a hole in the namespace.
41988ecc943SGeorge Wilson  */
42088ecc943SGeorge Wilson boolean_t
42188ecc943SGeorge Wilson vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
42288ecc943SGeorge Wilson {
42388ecc943SGeorge Wilson 	for (int c = 0; c < holes; c++) {
42488ecc943SGeorge Wilson 
42588ecc943SGeorge Wilson 		/* Top-level is a hole */
42688ecc943SGeorge Wilson 		if (hole_array[c] == id)
42788ecc943SGeorge Wilson 			return (B_TRUE);
42888ecc943SGeorge Wilson 	}
42988ecc943SGeorge Wilson 	return (B_FALSE);
43088ecc943SGeorge Wilson }
43188ecc943SGeorge Wilson 
432fa9e4066Sahrens /*
433fa9e4066Sahrens  * Convert our list of pools into the definitive set of configurations.  We
434fa9e4066Sahrens  * start by picking the best config for each toplevel vdev.  Once that's done,
435fa9e4066Sahrens  * we assemble the toplevel vdevs into a full config for the pool.  We make a
436fa9e4066Sahrens  * pass to fix up any incorrect paths, and then add it to the main list to
437fa9e4066Sahrens  * return to the user.
438fa9e4066Sahrens  */
439fa9e4066Sahrens static nvlist_t *
4403a57275aSck get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
441fa9e4066Sahrens {
44299653d4eSeschrock 	pool_entry_t *pe;
44399653d4eSeschrock 	vdev_entry_t *ve;
44499653d4eSeschrock 	config_entry_t *ce;
445f83b46baSPaul Dagnelie 	nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
446fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
447fa94a07fSbrendan 	uint_t i, nspares, nl2cache;
44899653d4eSeschrock 	boolean_t config_seen;
449fa9e4066Sahrens 	uint64_t best_txg;
450f83b46baSPaul Dagnelie 	char *name, *hostname = NULL;
451dfbb9432SGeorge Wilson 	uint64_t guid;
45299653d4eSeschrock 	uint_t children = 0;
45399653d4eSeschrock 	nvlist_t **child = NULL;
45488ecc943SGeorge Wilson 	uint_t holes;
45588ecc943SGeorge Wilson 	uint64_t *hole_array, max_id;
45699653d4eSeschrock 	uint_t c;
45794de1d4cSeschrock 	boolean_t isactive;
45895173954Sek 	uint64_t hostid;
4592f8aaab3Seschrock 	nvlist_t *nvl;
46024e697d4Sck 	boolean_t found_one = B_FALSE;
46188ecc943SGeorge Wilson 	boolean_t valid_top_config = B_FALSE;
462fa9e4066Sahrens 
46399653d4eSeschrock 	if (nvlist_alloc(&ret, 0, 0) != 0)
46499653d4eSeschrock 		goto nomem;
465fa9e4066Sahrens 
46699653d4eSeschrock 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
46788ecc943SGeorge Wilson 		uint64_t id, max_txg = 0;
468fa9e4066Sahrens 
46999653d4eSeschrock 		if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
47099653d4eSeschrock 			goto nomem;
47199653d4eSeschrock 		config_seen = B_FALSE;
472fa9e4066Sahrens 
473fa9e4066Sahrens 		/*
474fa9e4066Sahrens 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
475fa9e4066Sahrens 		 * from the first one we find, and then go through the rest and
476fa9e4066Sahrens 		 * add them as necessary to the 'vdevs' member of the config.
477fa9e4066Sahrens 		 */
47899653d4eSeschrock 		for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
479fa9e4066Sahrens 
480fa9e4066Sahrens 			/*
481fa9e4066Sahrens 			 * Determine the best configuration for this vdev by
482fa9e4066Sahrens 			 * selecting the config with the latest transaction
483fa9e4066Sahrens 			 * group.
484fa9e4066Sahrens 			 */
485fa9e4066Sahrens 			best_txg = 0;
486fa9e4066Sahrens 			for (ce = ve->ve_configs; ce != NULL;
487fa9e4066Sahrens 			    ce = ce->ce_next) {
488fa9e4066Sahrens 
48999653d4eSeschrock 				if (ce->ce_txg > best_txg) {
490fa9e4066Sahrens 					tmp = ce->ce_config;
49199653d4eSeschrock 					best_txg = ce->ce_txg;
49299653d4eSeschrock 				}
493fa9e4066Sahrens 			}
494fa9e4066Sahrens 
49588ecc943SGeorge Wilson 			/*
49688ecc943SGeorge Wilson 			 * We rely on the fact that the max txg for the
49788ecc943SGeorge Wilson 			 * pool will contain the most up-to-date information
49888ecc943SGeorge Wilson 			 * about the valid top-levels in the vdev namespace.
49988ecc943SGeorge Wilson 			 */
50088ecc943SGeorge Wilson 			if (best_txg > max_txg) {
50188ecc943SGeorge Wilson 				(void) nvlist_remove(config,
50288ecc943SGeorge Wilson 				    ZPOOL_CONFIG_VDEV_CHILDREN,
50388ecc943SGeorge Wilson 				    DATA_TYPE_UINT64);
50488ecc943SGeorge Wilson 				(void) nvlist_remove(config,
50588ecc943SGeorge Wilson 				    ZPOOL_CONFIG_HOLE_ARRAY,
50688ecc943SGeorge Wilson 				    DATA_TYPE_UINT64_ARRAY);
50788ecc943SGeorge Wilson 
50888ecc943SGeorge Wilson 				max_txg = best_txg;
50988ecc943SGeorge Wilson 				hole_array = NULL;
51088ecc943SGeorge Wilson 				holes = 0;
51188ecc943SGeorge Wilson 				max_id = 0;
51288ecc943SGeorge Wilson 				valid_top_config = B_FALSE;
51388ecc943SGeorge Wilson 
51488ecc943SGeorge Wilson 				if (nvlist_lookup_uint64(tmp,
51588ecc943SGeorge Wilson 				    ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
51688ecc943SGeorge Wilson 					verify(nvlist_add_uint64(config,
51788ecc943SGeorge Wilson 					    ZPOOL_CONFIG_VDEV_CHILDREN,
51888ecc943SGeorge Wilson 					    max_id) == 0);
51988ecc943SGeorge Wilson 					valid_top_config = B_TRUE;
52088ecc943SGeorge Wilson 				}
52188ecc943SGeorge Wilson 
52288ecc943SGeorge Wilson 				if (nvlist_lookup_uint64_array(tmp,
52388ecc943SGeorge Wilson 				    ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
52488ecc943SGeorge Wilson 				    &holes) == 0) {
52588ecc943SGeorge Wilson 					verify(nvlist_add_uint64_array(config,
52688ecc943SGeorge Wilson 					    ZPOOL_CONFIG_HOLE_ARRAY,
52788ecc943SGeorge Wilson 					    hole_array, holes) == 0);
52888ecc943SGeorge Wilson 				}
52988ecc943SGeorge Wilson 			}
53088ecc943SGeorge Wilson 
531fa9e4066Sahrens 			if (!config_seen) {
532fa9e4066Sahrens 				/*
533fa9e4066Sahrens 				 * Copy the relevant pieces of data to the pool
534fa9e4066Sahrens 				 * configuration:
535fa9e4066Sahrens 				 *
53699653d4eSeschrock 				 *	version
537dfbb9432SGeorge Wilson 				 *	pool guid
538dfbb9432SGeorge Wilson 				 *	name
5398704186eSDan McDonald 				 *	comment (if available)
540dfbb9432SGeorge Wilson 				 *	pool state
54195173954Sek 				 *	hostid (if available)
54295173954Sek 				 *	hostname (if available)
543fa9e4066Sahrens 				 */
544bda88194SGeorge Wilson 				uint64_t state, version;
545dfbb9432SGeorge Wilson 				char *comment = NULL;
546dfbb9432SGeorge Wilson 
547dfbb9432SGeorge Wilson 				version = fnvlist_lookup_uint64(tmp,
548dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_VERSION);
549dfbb9432SGeorge Wilson 				fnvlist_add_uint64(config,
550dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_VERSION, version);
551dfbb9432SGeorge Wilson 				guid = fnvlist_lookup_uint64(tmp,
552dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_POOL_GUID);
553dfbb9432SGeorge Wilson 				fnvlist_add_uint64(config,
554dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_POOL_GUID, guid);
555dfbb9432SGeorge Wilson 				name = fnvlist_lookup_string(tmp,
556dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_POOL_NAME);
557dfbb9432SGeorge Wilson 				fnvlist_add_string(config,
558dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_POOL_NAME, name);
559dfbb9432SGeorge Wilson 
5608704186eSDan McDonald 				if (nvlist_lookup_string(tmp,
561dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_COMMENT, &comment) == 0)
562dfbb9432SGeorge Wilson 					fnvlist_add_string(config,
563dfbb9432SGeorge Wilson 					    ZPOOL_CONFIG_COMMENT, comment);
564dfbb9432SGeorge Wilson 
565dfbb9432SGeorge Wilson 				state = fnvlist_lookup_uint64(tmp,
566dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_POOL_STATE);
567dfbb9432SGeorge Wilson 				fnvlist_add_uint64(config,
568dfbb9432SGeorge Wilson 				    ZPOOL_CONFIG_POOL_STATE, state);
5698704186eSDan McDonald 
57095173954Sek 				hostid = 0;
57195173954Sek 				if (nvlist_lookup_uint64(tmp,
57295173954Sek 				    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
573dfbb9432SGeorge Wilson 					fnvlist_add_uint64(config,
574dfbb9432SGeorge Wilson 					    ZPOOL_CONFIG_HOSTID, hostid);
575dfbb9432SGeorge Wilson 					hostname = fnvlist_lookup_string(tmp,
576dfbb9432SGeorge Wilson 					    ZPOOL_CONFIG_HOSTNAME);
577dfbb9432SGeorge Wilson 					fnvlist_add_string(config,
578dfbb9432SGeorge Wilson 					    ZPOOL_CONFIG_HOSTNAME, hostname);
57995173954Sek 				}
580fa9e4066Sahrens 
58199653d4eSeschrock 				config_seen = B_TRUE;
582fa9e4066Sahrens 			}
583fa9e4066Sahrens 
584fa9e4066Sahrens 			/*
585fa9e4066Sahrens 			 * Add this top-level vdev to the child array.
586fa9e4066Sahrens 			 */
587fa9e4066Sahrens 			verify(nvlist_lookup_nvlist(tmp,
588fa9e4066Sahrens 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
589fa9e4066Sahrens 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
590fa9e4066Sahrens 			    &id) == 0);
59188ecc943SGeorge Wilson 
592fa9e4066Sahrens 			if (id >= children) {
593fa9e4066Sahrens 				nvlist_t **newchild;
594fa9e4066Sahrens 
59599653d4eSeschrock 				newchild = zfs_alloc(hdl, (id + 1) *
596fa9e4066Sahrens 				    sizeof (nvlist_t *));
59799653d4eSeschrock 				if (newchild == NULL)
59899653d4eSeschrock 					goto nomem;
599fa9e4066Sahrens 
600fa9e4066Sahrens 				for (c = 0; c < children; c++)
601fa9e4066Sahrens 					newchild[c] = child[c];
602fa9e4066Sahrens 
603fa9e4066Sahrens 				free(child);
604fa9e4066Sahrens 				child = newchild;
605fa9e4066Sahrens 				children = id + 1;
606fa9e4066Sahrens 			}
60799653d4eSeschrock 			if (nvlist_dup(nvtop, &child[id], 0) != 0)
60899653d4eSeschrock 				goto nomem;
609fa9e4066Sahrens 
610fa9e4066Sahrens 		}
611fa9e4066Sahrens 
61288ecc943SGeorge Wilson 		/*
61388ecc943SGeorge Wilson 		 * If we have information about all the top-levels then
61488ecc943SGeorge Wilson 		 * clean up the nvlist which we've constructed. This
61588ecc943SGeorge Wilson 		 * means removing any extraneous devices that are
61688ecc943SGeorge Wilson 		 * beyond the valid range or adding devices to the end
61788ecc943SGeorge Wilson 		 * of our array which appear to be missing.
61888ecc943SGeorge Wilson 		 */
61988ecc943SGeorge Wilson 		if (valid_top_config) {
62088ecc943SGeorge Wilson 			if (max_id < children) {
62188ecc943SGeorge Wilson 				for (c = max_id; c < children; c++)
62288ecc943SGeorge Wilson 					nvlist_free(child[c]);
62388ecc943SGeorge Wilson 				children = max_id;
62488ecc943SGeorge Wilson 			} else if (max_id > children) {
62588ecc943SGeorge Wilson 				nvlist_t **newchild;
62688ecc943SGeorge Wilson 
62788ecc943SGeorge Wilson 				newchild = zfs_alloc(hdl, (max_id) *
62888ecc943SGeorge Wilson 				    sizeof (nvlist_t *));
62988ecc943SGeorge Wilson 				if (newchild == NULL)
63088ecc943SGeorge Wilson 					goto nomem;
63188ecc943SGeorge Wilson 
63288ecc943SGeorge Wilson 				for (c = 0; c < children; c++)
63388ecc943SGeorge Wilson 					newchild[c] = child[c];
63488ecc943SGeorge Wilson 
63588ecc943SGeorge Wilson 				free(child);
63688ecc943SGeorge Wilson 				child = newchild;
63788ecc943SGeorge Wilson 				children = max_id;
63888ecc943SGeorge Wilson 			}
63988ecc943SGeorge Wilson 		}
64088ecc943SGeorge Wilson 
641fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
642fa9e4066Sahrens 		    &guid) == 0);
643fa9e4066Sahrens 
64488ecc943SGeorge Wilson 		/*
64588ecc943SGeorge Wilson 		 * The vdev namespace may contain holes as a result of
64688ecc943SGeorge Wilson 		 * device removal. We must add them back into the vdev
64788ecc943SGeorge Wilson 		 * tree before we process any missing devices.
64888ecc943SGeorge Wilson 		 */
64988ecc943SGeorge Wilson 		if (holes > 0) {
65088ecc943SGeorge Wilson 			ASSERT(valid_top_config);
65188ecc943SGeorge Wilson 
65288ecc943SGeorge Wilson 			for (c = 0; c < children; c++) {
65388ecc943SGeorge Wilson 				nvlist_t *holey;
65488ecc943SGeorge Wilson 
65588ecc943SGeorge Wilson 				if (child[c] != NULL ||
65688ecc943SGeorge Wilson 				    !vdev_is_hole(hole_array, holes, c))
65788ecc943SGeorge Wilson 					continue;
65888ecc943SGeorge Wilson 
65988ecc943SGeorge Wilson 				if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
66088ecc943SGeorge Wilson 				    0) != 0)
66188ecc943SGeorge Wilson 					goto nomem;
66288ecc943SGeorge Wilson 
66388ecc943SGeorge Wilson 				/*
66488ecc943SGeorge Wilson 				 * Holes in the namespace are treated as
66588ecc943SGeorge Wilson 				 * "hole" top-level vdevs and have a
66688ecc943SGeorge Wilson 				 * special flag set on them.
66788ecc943SGeorge Wilson 				 */
66888ecc943SGeorge Wilson 				if (nvlist_add_string(holey,
66988ecc943SGeorge Wilson 				    ZPOOL_CONFIG_TYPE,
67088ecc943SGeorge Wilson 				    VDEV_TYPE_HOLE) != 0 ||
67188ecc943SGeorge Wilson 				    nvlist_add_uint64(holey,
67288ecc943SGeorge Wilson 				    ZPOOL_CONFIG_ID, c) != 0 ||
67388ecc943SGeorge Wilson 				    nvlist_add_uint64(holey,
674078266a5SMarcel Telka 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
675078266a5SMarcel Telka 					nvlist_free(holey);
67688ecc943SGeorge Wilson 					goto nomem;
677078266a5SMarcel Telka 				}
67888ecc943SGeorge Wilson 				child[c] = holey;
67988ecc943SGeorge Wilson 			}
68088ecc943SGeorge Wilson 		}
68188ecc943SGeorge Wilson 
682fa9e4066Sahrens 		/*
683fa9e4066Sahrens 		 * Look for any missing top-level vdevs.  If this is the case,
684fa9e4066Sahrens 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
685fa9e4066Sahrens 		 * simply compress the child array, because the kernel performs
686fa9e4066Sahrens 		 * certain checks to make sure the vdev IDs match their location
687fa9e4066Sahrens 		 * in the configuration.
688fa9e4066Sahrens 		 */
68988ecc943SGeorge Wilson 		for (c = 0; c < children; c++) {
690fa9e4066Sahrens 			if (child[c] == NULL) {
691fa9e4066Sahrens 				nvlist_t *missing;
69299653d4eSeschrock 				if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
69399653d4eSeschrock 				    0) != 0)
69499653d4eSeschrock 					goto nomem;
69599653d4eSeschrock 				if (nvlist_add_string(missing,
69699653d4eSeschrock 				    ZPOOL_CONFIG_TYPE,
69799653d4eSeschrock 				    VDEV_TYPE_MISSING) != 0 ||
69899653d4eSeschrock 				    nvlist_add_uint64(missing,
69999653d4eSeschrock 				    ZPOOL_CONFIG_ID, c) != 0 ||
70099653d4eSeschrock 				    nvlist_add_uint64(missing,
70199653d4eSeschrock 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
70299653d4eSeschrock 					nvlist_free(missing);
70399653d4eSeschrock 					goto nomem;
70499653d4eSeschrock 				}
705fa9e4066Sahrens 				child[c] = missing;
706fa9e4066Sahrens 			}
70788ecc943SGeorge Wilson 		}
708fa9e4066Sahrens 
709fa9e4066Sahrens 		/*
710fa9e4066Sahrens 		 * Put all of this pool's top-level vdevs into a root vdev.
711fa9e4066Sahrens 		 */
71299653d4eSeschrock 		if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
71399653d4eSeschrock 			goto nomem;
71499653d4eSeschrock 		if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
71599653d4eSeschrock 		    VDEV_TYPE_ROOT) != 0 ||
71699653d4eSeschrock 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
71799653d4eSeschrock 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
71899653d4eSeschrock 		    nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
71999653d4eSeschrock 		    child, children) != 0) {
72099653d4eSeschrock 			nvlist_free(nvroot);
72199653d4eSeschrock 			goto nomem;
72299653d4eSeschrock 		}
723fa9e4066Sahrens 
724fa9e4066Sahrens 		for (c = 0; c < children; c++)
725fa9e4066Sahrens 			nvlist_free(child[c]);
726fa9e4066Sahrens 		free(child);
72799653d4eSeschrock 		children = 0;
72899653d4eSeschrock 		child = NULL;
729fa9e4066Sahrens 
730fa9e4066Sahrens 		/*
731fa9e4066Sahrens 		 * Go through and fix up any paths and/or devids based on our
732fa9e4066Sahrens 		 * known list of vdev GUID -> path mappings.
733fa9e4066Sahrens 		 */
73499653d4eSeschrock 		if (fix_paths(nvroot, pl->names) != 0) {
73599653d4eSeschrock 			nvlist_free(nvroot);
73699653d4eSeschrock 			goto nomem;
73799653d4eSeschrock 		}
738fa9e4066Sahrens 
739fa9e4066Sahrens 		/*
740fa9e4066Sahrens 		 * Add the root vdev to this pool's configuration.
741fa9e4066Sahrens 		 */
74299653d4eSeschrock 		if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
74399653d4eSeschrock 		    nvroot) != 0) {
74499653d4eSeschrock 			nvlist_free(nvroot);
74599653d4eSeschrock 			goto nomem;
74699653d4eSeschrock 		}
747fa9e4066Sahrens 		nvlist_free(nvroot);
748fa9e4066Sahrens 
7493a57275aSck 		/*
7503a57275aSck 		 * zdb uses this path to report on active pools that were
7513a57275aSck 		 * imported or created using -R.
7523a57275aSck 		 */
7533a57275aSck 		if (active_ok)
7543a57275aSck 			goto add_pool;
7553a57275aSck 
756fa9e4066Sahrens 		/*
757fa9e4066Sahrens 		 * Determine if this pool is currently active, in which case we
758fa9e4066Sahrens 		 * can't actually import it.
759fa9e4066Sahrens 		 */
760fa9e4066Sahrens 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
761fa9e4066Sahrens 		    &name) == 0);
762fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
763fa9e4066Sahrens 		    &guid) == 0);
764fa9e4066Sahrens 
76594de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0)
76694de1d4cSeschrock 			goto error;
76794de1d4cSeschrock 
7680192a278Seschrock 		if (isactive) {
769fa9e4066Sahrens 			nvlist_free(config);
77099653d4eSeschrock 			config = NULL;
771fa9e4066Sahrens 			continue;
772fa9e4066Sahrens 		}
773fa9e4066Sahrens 
77488ecc943SGeorge Wilson 		if ((nvl = refresh_config(hdl, config)) == NULL) {
77588ecc943SGeorge Wilson 			nvlist_free(config);
77688ecc943SGeorge Wilson 			config = NULL;
77788ecc943SGeorge Wilson 			continue;
77888ecc943SGeorge Wilson 		}
779fa9e4066Sahrens 
780fa9e4066Sahrens 		nvlist_free(config);
7812f8aaab3Seschrock 		config = nvl;
782fa9e4066Sahrens 
78399653d4eSeschrock 		/*
78499653d4eSeschrock 		 * Go through and update the paths for spares, now that we have
78599653d4eSeschrock 		 * them.
78699653d4eSeschrock 		 */
78799653d4eSeschrock 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
78899653d4eSeschrock 		    &nvroot) == 0);
78999653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
79099653d4eSeschrock 		    &spares, &nspares) == 0) {
79199653d4eSeschrock 			for (i = 0; i < nspares; i++) {
79299653d4eSeschrock 				if (fix_paths(spares[i], pl->names) != 0)
79399653d4eSeschrock 					goto nomem;
79499653d4eSeschrock 			}
79599653d4eSeschrock 		}
79699653d4eSeschrock 
797fa94a07fSbrendan 		/*
798fa94a07fSbrendan 		 * Update the paths for l2cache devices.
799fa94a07fSbrendan 		 */
800fa94a07fSbrendan 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
801fa94a07fSbrendan 		    &l2cache, &nl2cache) == 0) {
802fa94a07fSbrendan 			for (i = 0; i < nl2cache; i++) {
803fa94a07fSbrendan 				if (fix_paths(l2cache[i], pl->names) != 0)
804fa94a07fSbrendan 					goto nomem;
805fa94a07fSbrendan 			}
806fa94a07fSbrendan 		}
807fa94a07fSbrendan 
80895173954Sek 		/*
80995173954Sek 		 * Restore the original information read from the actual label.
81095173954Sek 		 */
81195173954Sek 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
81295173954Sek 		    DATA_TYPE_UINT64);
81395173954Sek 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
81495173954Sek 		    DATA_TYPE_STRING);
81595173954Sek 		if (hostid != 0) {
81695173954Sek 			verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
81795173954Sek 			    hostid) == 0);
81895173954Sek 			verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
81995173954Sek 			    hostname) == 0);
82095173954Sek 		}
82195173954Sek 
8223a57275aSck add_pool:
823fa9e4066Sahrens 		/*
824fa9e4066Sahrens 		 * Add this pool to the list of configs.
825fa9e4066Sahrens 		 */
826e9dbad6fSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
827e9dbad6fSeschrock 		    &name) == 0);
82899653d4eSeschrock 		if (nvlist_add_nvlist(ret, name, config) != 0)
82999653d4eSeschrock 			goto nomem;
830fa9e4066Sahrens 
83124e697d4Sck 		found_one = B_TRUE;
832fa9e4066Sahrens 		nvlist_free(config);
83399653d4eSeschrock 		config = NULL;
834fa9e4066Sahrens 	}
835fa9e4066Sahrens 
83624e697d4Sck 	if (!found_one) {
83724e697d4Sck 		nvlist_free(ret);
83824e697d4Sck 		ret = NULL;
83924e697d4Sck 	}
84024e697d4Sck 
841fa9e4066Sahrens 	return (ret);
84299653d4eSeschrock 
84399653d4eSeschrock nomem:
84499653d4eSeschrock 	(void) no_memory(hdl);
84599653d4eSeschrock error:
84694de1d4cSeschrock 	nvlist_free(config);
84794de1d4cSeschrock 	nvlist_free(ret);
84899653d4eSeschrock 	for (c = 0; c < children; c++)
84999653d4eSeschrock 		nvlist_free(child[c]);
85094de1d4cSeschrock 	free(child);
85199653d4eSeschrock 
85299653d4eSeschrock 	return (NULL);
853fa9e4066Sahrens }
854fa9e4066Sahrens 
855fa9e4066Sahrens /*
856fa9e4066Sahrens  * Return the offset of the given label.
857fa9e4066Sahrens  */
858fa9e4066Sahrens static uint64_t
859e7437265Sahrens label_offset(uint64_t size, int l)
860fa9e4066Sahrens {
861e7437265Sahrens 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
862fa9e4066Sahrens 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
863fa9e4066Sahrens 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
864fa9e4066Sahrens }
865fa9e4066Sahrens 
866fa9e4066Sahrens /*
867fa9e4066Sahrens  * Given a file descriptor, read the label information and return an nvlist
868fa9e4066Sahrens  * describing the configuration, if there is one.
869fa9e4066Sahrens  */
87099653d4eSeschrock int
87199653d4eSeschrock zpool_read_label(int fd, nvlist_t **config)
872fa9e4066Sahrens {
873fa9e4066Sahrens 	struct stat64 statbuf;
874fa9e4066Sahrens 	int l;
875fa9e4066Sahrens 	vdev_label_t *label;
876e7437265Sahrens 	uint64_t state, txg, size;
877fa9e4066Sahrens 
87899653d4eSeschrock 	*config = NULL;
87999653d4eSeschrock 
880fa9e4066Sahrens 	if (fstat64(fd, &statbuf) == -1)
88199653d4eSeschrock 		return (0);
882e7437265Sahrens 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
883fa9e4066Sahrens 
88499653d4eSeschrock 	if ((label = malloc(sizeof (vdev_label_t))) == NULL)
88599653d4eSeschrock 		return (-1);
886fa9e4066Sahrens 
887fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
888c5904d13Seschrock 		if (pread64(fd, label, sizeof (vdev_label_t),
889e7437265Sahrens 		    label_offset(size, l)) != sizeof (vdev_label_t))
890fa9e4066Sahrens 			continue;
891fa9e4066Sahrens 
892fa9e4066Sahrens 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
89399653d4eSeschrock 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
894fa9e4066Sahrens 			continue;
895fa9e4066Sahrens 
89699653d4eSeschrock 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
897fa94a07fSbrendan 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
89899653d4eSeschrock 			nvlist_free(*config);
899fa9e4066Sahrens 			continue;
900fa9e4066Sahrens 		}
901fa9e4066Sahrens 
902fa94a07fSbrendan 		if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
90399653d4eSeschrock 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
90499653d4eSeschrock 		    &txg) != 0 || txg == 0)) {
90599653d4eSeschrock 			nvlist_free(*config);
906fa9e4066Sahrens 			continue;
907fa9e4066Sahrens 		}
908fa9e4066Sahrens 
909fa9e4066Sahrens 		free(label);
91099653d4eSeschrock 		return (0);
911fa9e4066Sahrens 	}
912fa9e4066Sahrens 
913fa9e4066Sahrens 	free(label);
91499653d4eSeschrock 	*config = NULL;
91599653d4eSeschrock 	return (0);
916fa9e4066Sahrens }
917fa9e4066Sahrens 
9184f67d755SEric Taylor typedef struct rdsk_node {
9194f67d755SEric Taylor 	char *rn_name;
9204f67d755SEric Taylor 	int rn_dfd;
9214f67d755SEric Taylor 	libzfs_handle_t *rn_hdl;
9224f67d755SEric Taylor 	nvlist_t *rn_config;
9234f67d755SEric Taylor 	avl_tree_t *rn_avl;
9244f67d755SEric Taylor 	avl_node_t rn_node;
9254f67d755SEric Taylor 	boolean_t rn_nozpool;
9264f67d755SEric Taylor } rdsk_node_t;
9274f67d755SEric Taylor 
9284f67d755SEric Taylor static int
9294f67d755SEric Taylor slice_cache_compare(const void *arg1, const void *arg2)
9304f67d755SEric Taylor {
9314f67d755SEric Taylor 	const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
9324f67d755SEric Taylor 	const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
9334f67d755SEric Taylor 	char *nm1slice, *nm2slice;
9344f67d755SEric Taylor 	int rv;
9354f67d755SEric Taylor 
9364f67d755SEric Taylor 	/*
9374f67d755SEric Taylor 	 * slices zero and two are the most likely to provide results,
9384f67d755SEric Taylor 	 * so put those first
9394f67d755SEric Taylor 	 */
9404f67d755SEric Taylor 	nm1slice = strstr(nm1, "s0");
9414f67d755SEric Taylor 	nm2slice = strstr(nm2, "s0");
9424f67d755SEric Taylor 	if (nm1slice && !nm2slice) {
9434f67d755SEric Taylor 		return (-1);
9444f67d755SEric Taylor 	}
9454f67d755SEric Taylor 	if (!nm1slice && nm2slice) {
9464f67d755SEric Taylor 		return (1);
9474f67d755SEric Taylor 	}
9484f67d755SEric Taylor 	nm1slice = strstr(nm1, "s2");
9494f67d755SEric Taylor 	nm2slice = strstr(nm2, "s2");
9504f67d755SEric Taylor 	if (nm1slice && !nm2slice) {
9514f67d755SEric Taylor 		return (-1);
9524f67d755SEric Taylor 	}
9534f67d755SEric Taylor 	if (!nm1slice && nm2slice) {
9544f67d755SEric Taylor 		return (1);
9554f67d755SEric Taylor 	}
9564f67d755SEric Taylor 
9574f67d755SEric Taylor 	rv = strcmp(nm1, nm2);
9584f67d755SEric Taylor 	if (rv == 0)
9594f67d755SEric Taylor 		return (0);
9604f67d755SEric Taylor 	return (rv > 0 ? 1 : -1);
9614f67d755SEric Taylor }
9624f67d755SEric Taylor 
9634f67d755SEric Taylor static void
9644f67d755SEric Taylor check_one_slice(avl_tree_t *r, char *diskname, uint_t partno,
9654f67d755SEric Taylor     diskaddr_t size, uint_t blksz)
9664f67d755SEric Taylor {
9674f67d755SEric Taylor 	rdsk_node_t tmpnode;
9684f67d755SEric Taylor 	rdsk_node_t *node;
9694f67d755SEric Taylor 	char sname[MAXNAMELEN];
9704f67d755SEric Taylor 
9714f67d755SEric Taylor 	tmpnode.rn_name = &sname[0];
9724f67d755SEric Taylor 	(void) snprintf(tmpnode.rn_name, MAXNAMELEN, "%s%u",
9734f67d755SEric Taylor 	    diskname, partno);
97416d2251eSEric Taylor 	/*
97516d2251eSEric Taylor 	 * protect against division by zero for disk labels that
97616d2251eSEric Taylor 	 * contain a bogus sector size
97716d2251eSEric Taylor 	 */
97816d2251eSEric Taylor 	if (blksz == 0)
97916d2251eSEric Taylor 		blksz = DEV_BSIZE;
9804f67d755SEric Taylor 	/* too small to contain a zpool? */
9814f67d755SEric Taylor 	if ((size < (SPA_MINDEVSIZE / blksz)) &&
9824f67d755SEric Taylor 	    (node = avl_find(r, &tmpnode, NULL)))
9834f67d755SEric Taylor 		node->rn_nozpool = B_TRUE;
9844f67d755SEric Taylor }
9854f67d755SEric Taylor 
9864f67d755SEric Taylor static void
9874f67d755SEric Taylor nozpool_all_slices(avl_tree_t *r, const char *sname)
9884f67d755SEric Taylor {
9894f67d755SEric Taylor 	char diskname[MAXNAMELEN];
9904f67d755SEric Taylor 	char *ptr;
9914f67d755SEric Taylor 	int i;
9924f67d755SEric Taylor 
9934f67d755SEric Taylor 	(void) strncpy(diskname, sname, MAXNAMELEN);
9944f67d755SEric Taylor 	if (((ptr = strrchr(diskname, 's')) == NULL) &&
9954f67d755SEric Taylor 	    ((ptr = strrchr(diskname, 'p')) == NULL))
9964f67d755SEric Taylor 		return;
9974f67d755SEric Taylor 	ptr[0] = 's';
9984f67d755SEric Taylor 	ptr[1] = '\0';
9994f67d755SEric Taylor 	for (i = 0; i < NDKMAP; i++)
10004f67d755SEric Taylor 		check_one_slice(r, diskname, i, 0, 1);
10014f67d755SEric Taylor 	ptr[0] = 'p';
10024f67d755SEric Taylor 	for (i = 0; i <= FD_NUMPART; i++)
10034f67d755SEric Taylor 		check_one_slice(r, diskname, i, 0, 1);
10044f67d755SEric Taylor }
10054f67d755SEric Taylor 
10064f67d755SEric Taylor static void
10074f67d755SEric Taylor check_slices(avl_tree_t *r, int fd, const char *sname)
10084f67d755SEric Taylor {
10094f67d755SEric Taylor 	struct extvtoc vtoc;
10104f67d755SEric Taylor 	struct dk_gpt *gpt;
10114f67d755SEric Taylor 	char diskname[MAXNAMELEN];
10124f67d755SEric Taylor 	char *ptr;
10134f67d755SEric Taylor 	int i;
10144f67d755SEric Taylor 
10154f67d755SEric Taylor 	(void) strncpy(diskname, sname, MAXNAMELEN);
10164f67d755SEric Taylor 	if ((ptr = strrchr(diskname, 's')) == NULL || !isdigit(ptr[1]))
10174f67d755SEric Taylor 		return;
10184f67d755SEric Taylor 	ptr[1] = '\0';
10194f67d755SEric Taylor 
10204f67d755SEric Taylor 	if (read_extvtoc(fd, &vtoc) >= 0) {
10214f67d755SEric Taylor 		for (i = 0; i < NDKMAP; i++)
10224f67d755SEric Taylor 			check_one_slice(r, diskname, i,
10234f67d755SEric Taylor 			    vtoc.v_part[i].p_size, vtoc.v_sectorsz);
10244f67d755SEric Taylor 	} else if (efi_alloc_and_read(fd, &gpt) >= 0) {
10254f67d755SEric Taylor 		/*
10264f67d755SEric Taylor 		 * on x86 we'll still have leftover links that point
10274f67d755SEric Taylor 		 * to slices s[9-15], so use NDKMAP instead
10284f67d755SEric Taylor 		 */
10294f67d755SEric Taylor 		for (i = 0; i < NDKMAP; i++)
10304f67d755SEric Taylor 			check_one_slice(r, diskname, i,
10314f67d755SEric Taylor 			    gpt->efi_parts[i].p_size, gpt->efi_lbasize);
10324f67d755SEric Taylor 		/* nodes p[1-4] are never used with EFI labels */
10334f67d755SEric Taylor 		ptr[0] = 'p';
10344f67d755SEric Taylor 		for (i = 1; i <= FD_NUMPART; i++)
10354f67d755SEric Taylor 			check_one_slice(r, diskname, i, 0, 1);
10364f67d755SEric Taylor 		efi_free(gpt);
10374f67d755SEric Taylor 	}
10384f67d755SEric Taylor }
10394f67d755SEric Taylor 
10404f67d755SEric Taylor static void
10414f67d755SEric Taylor zpool_open_func(void *arg)
10424f67d755SEric Taylor {
10434f67d755SEric Taylor 	rdsk_node_t *rn = arg;
10444f67d755SEric Taylor 	struct stat64 statbuf;
10454f67d755SEric Taylor 	nvlist_t *config;
10464f67d755SEric Taylor 	int fd;
10474f67d755SEric Taylor 
10484f67d755SEric Taylor 	if (rn->rn_nozpool)
10494f67d755SEric Taylor 		return;
10504f67d755SEric Taylor 	if ((fd = openat64(rn->rn_dfd, rn->rn_name, O_RDONLY)) < 0) {
10514f67d755SEric Taylor 		/* symlink to a device that's no longer there */
10524f67d755SEric Taylor 		if (errno == ENOENT)
10534f67d755SEric Taylor 			nozpool_all_slices(rn->rn_avl, rn->rn_name);
10544f67d755SEric Taylor 		return;
10554f67d755SEric Taylor 	}
10564f67d755SEric Taylor 	/*
10574f67d755SEric Taylor 	 * Ignore failed stats.  We only want regular
10584f67d755SEric Taylor 	 * files, character devs and block devs.
10594f67d755SEric Taylor 	 */
10604f67d755SEric Taylor 	if (fstat64(fd, &statbuf) != 0 ||
10614f67d755SEric Taylor 	    (!S_ISREG(statbuf.st_mode) &&
10624f67d755SEric Taylor 	    !S_ISCHR(statbuf.st_mode) &&
10634f67d755SEric Taylor 	    !S_ISBLK(statbuf.st_mode))) {
10644f67d755SEric Taylor 		(void) close(fd);
10654f67d755SEric Taylor 		return;
10664f67d755SEric Taylor 	}
10674f67d755SEric Taylor 	/* this file is too small to hold a zpool */
10684f67d755SEric Taylor 	if (S_ISREG(statbuf.st_mode) &&
10694f67d755SEric Taylor 	    statbuf.st_size < SPA_MINDEVSIZE) {
10704f67d755SEric Taylor 		(void) close(fd);
10714f67d755SEric Taylor 		return;
10724f67d755SEric Taylor 	} else if (!S_ISREG(statbuf.st_mode)) {
10734f67d755SEric Taylor 		/*
10744f67d755SEric Taylor 		 * Try to read the disk label first so we don't have to
10754f67d755SEric Taylor 		 * open a bunch of minor nodes that can't have a zpool.
10764f67d755SEric Taylor 		 */
10774f67d755SEric Taylor 		check_slices(rn->rn_avl, fd, rn->rn_name);
10784f67d755SEric Taylor 	}
10794f67d755SEric Taylor 
10804f67d755SEric Taylor 	if ((zpool_read_label(fd, &config)) != 0) {
10814f67d755SEric Taylor 		(void) close(fd);
10824f67d755SEric Taylor 		(void) no_memory(rn->rn_hdl);
10834f67d755SEric Taylor 		return;
10844f67d755SEric Taylor 	}
10854f67d755SEric Taylor 	(void) close(fd);
10864f67d755SEric Taylor 
10874f67d755SEric Taylor 	rn->rn_config = config;
10884f67d755SEric Taylor }
10894f67d755SEric Taylor 
1090096d22d4SEric Schrock /*
10916401734dSWill Andrews  * Given a file descriptor, clear (zero) the label information.
1092096d22d4SEric Schrock  */
1093096d22d4SEric Schrock int
1094096d22d4SEric Schrock zpool_clear_label(int fd)
1095096d22d4SEric Schrock {
1096096d22d4SEric Schrock 	struct stat64 statbuf;
1097096d22d4SEric Schrock 	int l;
1098096d22d4SEric Schrock 	vdev_label_t *label;
1099096d22d4SEric Schrock 	uint64_t size;
1100096d22d4SEric Schrock 
1101096d22d4SEric Schrock 	if (fstat64(fd, &statbuf) == -1)
1102096d22d4SEric Schrock 		return (0);
1103096d22d4SEric Schrock 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1104096d22d4SEric Schrock 
1105096d22d4SEric Schrock 	if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
1106096d22d4SEric Schrock 		return (-1);
1107096d22d4SEric Schrock 
1108096d22d4SEric Schrock 	for (l = 0; l < VDEV_LABELS; l++) {
1109096d22d4SEric Schrock 		if (pwrite64(fd, label, sizeof (vdev_label_t),
1110078266a5SMarcel Telka 		    label_offset(size, l)) != sizeof (vdev_label_t)) {
1111078266a5SMarcel Telka 			free(label);
1112096d22d4SEric Schrock 			return (-1);
1113078266a5SMarcel Telka 		}
1114096d22d4SEric Schrock 	}
1115096d22d4SEric Schrock 
1116096d22d4SEric Schrock 	free(label);
1117096d22d4SEric Schrock 	return (0);
1118096d22d4SEric Schrock }
1119096d22d4SEric Schrock 
1120fa9e4066Sahrens /*
1121fa9e4066Sahrens  * Given a list of directories to search, find all pools stored on disk.  This
1122fa9e4066Sahrens  * includes partial pools which are not available to import.  If no args are
1123fa9e4066Sahrens  * given (argc is 0), then the default directory (/dev/dsk) is searched.
112424e697d4Sck  * poolname or guid (but not both) are provided by the caller when trying
112524e697d4Sck  * to import a specific pool.
1126fa9e4066Sahrens  */
112724e697d4Sck static nvlist_t *
1128d41c4376SMark J Musante zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg)
1129fa9e4066Sahrens {
1130d41c4376SMark J Musante 	int i, dirs = iarg->paths;
1131fa9e4066Sahrens 	struct dirent64 *dp;
1132fa9e4066Sahrens 	char path[MAXPATHLEN];
1133d41c4376SMark J Musante 	char *end, **dir = iarg->path;
1134842c5645Sjwadams 	size_t pathleft;
11354f67d755SEric Taylor 	nvlist_t *ret = NULL;
11366401734dSWill Andrews 	static char *default_dir = ZFS_DISK_ROOT;
1137fa9e4066Sahrens 	pool_list_t pools = { 0 };
113899653d4eSeschrock 	pool_entry_t *pe, *penext;
113999653d4eSeschrock 	vdev_entry_t *ve, *venext;
114099653d4eSeschrock 	config_entry_t *ce, *cenext;
114199653d4eSeschrock 	name_entry_t *ne, *nenext;
11424f67d755SEric Taylor 	avl_tree_t slice_cache;
11434f67d755SEric Taylor 	rdsk_node_t *slice;
11444f67d755SEric Taylor 	void *cookie;
114599653d4eSeschrock 
1146d41c4376SMark J Musante 	if (dirs == 0) {
1147d41c4376SMark J Musante 		dirs = 1;
1148d41c4376SMark J Musante 		dir = &default_dir;
1149fa9e4066Sahrens 	}
1150fa9e4066Sahrens 
1151fa9e4066Sahrens 	/*
1152fa9e4066Sahrens 	 * Go through and read the label configuration information from every
1153fa9e4066Sahrens 	 * possible device, organizing the information according to pool GUID
1154fa9e4066Sahrens 	 * and toplevel GUID.
1155fa9e4066Sahrens 	 */
1156d41c4376SMark J Musante 	for (i = 0; i < dirs; i++) {
11574f67d755SEric Taylor 		tpool_t *t;
11586401734dSWill Andrews 		char rdsk[MAXPATHLEN];
1159842c5645Sjwadams 		int dfd;
1160078266a5SMarcel Telka 		boolean_t config_failed = B_FALSE;
1161078266a5SMarcel Telka 		DIR *dirp;
1162842c5645Sjwadams 
1163842c5645Sjwadams 		/* use realpath to normalize the path */
1164d41c4376SMark J Musante 		if (realpath(dir[i], path) == 0) {
1165ece3d9b3Slling 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
1166d41c4376SMark J Musante 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"), dir[i]);
116799653d4eSeschrock 			goto error;
1168fa9e4066Sahrens 		}
1169842c5645Sjwadams 		end = &path[strlen(path)];
1170842c5645Sjwadams 		*end++ = '/';
1171842c5645Sjwadams 		*end = 0;
1172842c5645Sjwadams 		pathleft = &path[sizeof (path)] - end;
1173842c5645Sjwadams 
1174842c5645Sjwadams 		/*
1175842c5645Sjwadams 		 * Using raw devices instead of block devices when we're
1176842c5645Sjwadams 		 * reading the labels skips a bunch of slow operations during
1177842c5645Sjwadams 		 * close(2) processing, so we replace /dev/dsk with /dev/rdsk.
1178842c5645Sjwadams 		 */
11796401734dSWill Andrews 		if (strcmp(path, ZFS_DISK_ROOTD) == 0)
11806401734dSWill Andrews 			(void) strlcpy(rdsk, ZFS_RDISK_ROOTD, sizeof (rdsk));
1181842c5645Sjwadams 		else
11826401734dSWill Andrews 			(void) strlcpy(rdsk, path, sizeof (rdsk));
1183fa9e4066Sahrens 
1184842c5645Sjwadams 		if ((dfd = open64(rdsk, O_RDONLY)) < 0 ||
1185842c5645Sjwadams 		    (dirp = fdopendir(dfd)) == NULL) {
1186078266a5SMarcel Telka 			if (dfd >= 0)
1187078266a5SMarcel Telka 				(void) close(dfd);
118899653d4eSeschrock 			zfs_error_aux(hdl, strerror(errno));
1189ece3d9b3Slling 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
119099653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1191842c5645Sjwadams 			    rdsk);
119299653d4eSeschrock 			goto error;
1193fa9e4066Sahrens 		}
1194fa9e4066Sahrens 
11954f67d755SEric Taylor 		avl_create(&slice_cache, slice_cache_compare,
11964f67d755SEric Taylor 		    sizeof (rdsk_node_t), offsetof(rdsk_node_t, rn_node));
1197fa9e4066Sahrens 		/*
1198fa9e4066Sahrens 		 * This is not MT-safe, but we have no MT consumers of libzfs
1199fa9e4066Sahrens 		 */
1200fa9e4066Sahrens 		while ((dp = readdir64(dirp)) != NULL) {
1201842c5645Sjwadams 			const char *name = dp->d_name;
1202842c5645Sjwadams 			if (name[0] == '.' &&
1203842c5645Sjwadams 			    (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1204fa9e4066Sahrens 				continue;
1205fa9e4066Sahrens 
12064f67d755SEric Taylor 			slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
12074f67d755SEric Taylor 			slice->rn_name = zfs_strdup(hdl, name);
12084f67d755SEric Taylor 			slice->rn_avl = &slice_cache;
12094f67d755SEric Taylor 			slice->rn_dfd = dfd;
12104f67d755SEric Taylor 			slice->rn_hdl = hdl;
12114f67d755SEric Taylor 			slice->rn_nozpool = B_FALSE;
12124f67d755SEric Taylor 			avl_add(&slice_cache, slice);
12134f67d755SEric Taylor 		}
12144f67d755SEric Taylor 		/*
12154f67d755SEric Taylor 		 * create a thread pool to do all of this in parallel;
12164f67d755SEric Taylor 		 * rn_nozpool is not protected, so this is racy in that
12174f67d755SEric Taylor 		 * multiple tasks could decide that the same slice can
12184f67d755SEric Taylor 		 * not hold a zpool, which is benign.  Also choose
12194f67d755SEric Taylor 		 * double the number of processors; we hold a lot of
12204f67d755SEric Taylor 		 * locks in the kernel, so going beyond this doesn't
12214f67d755SEric Taylor 		 * buy us much.
12224f67d755SEric Taylor 		 */
12234f67d755SEric Taylor 		t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN),
12244f67d755SEric Taylor 		    0, NULL);
12254f67d755SEric Taylor 		for (slice = avl_first(&slice_cache); slice;
12264f67d755SEric Taylor 		    (slice = avl_walk(&slice_cache, slice,
12274f67d755SEric Taylor 		    AVL_AFTER)))
12284f67d755SEric Taylor 			(void) tpool_dispatch(t, zpool_open_func, slice);
12294f67d755SEric Taylor 		tpool_wait(t);
12304f67d755SEric Taylor 		tpool_destroy(t);
12314f67d755SEric Taylor 
12324f67d755SEric Taylor 		cookie = NULL;
12334f67d755SEric Taylor 		while ((slice = avl_destroy_nodes(&slice_cache,
12344f67d755SEric Taylor 		    &cookie)) != NULL) {
1235078266a5SMarcel Telka 			if (slice->rn_config != NULL && !config_failed) {
12364f67d755SEric Taylor 				nvlist_t *config = slice->rn_config;
123724e697d4Sck 				boolean_t matched = B_TRUE;
123824e697d4Sck 
1239d41c4376SMark J Musante 				if (iarg->poolname != NULL) {
124024e697d4Sck 					char *pname;
12417b0e68eaSck 
12427b0e68eaSck 					matched = nvlist_lookup_string(config,
124324e697d4Sck 					    ZPOOL_CONFIG_POOL_NAME,
12447b0e68eaSck 					    &pname) == 0 &&
1245d41c4376SMark J Musante 					    strcmp(iarg->poolname, pname) == 0;
1246d41c4376SMark J Musante 				} else if (iarg->guid != 0) {
124724e697d4Sck 					uint64_t this_guid;
1248913f2d80Sck 
1249913f2d80Sck 					matched = nvlist_lookup_uint64(config,
125024e697d4Sck 					    ZPOOL_CONFIG_POOL_GUID,
1251913f2d80Sck 					    &this_guid) == 0 &&
1252d41c4376SMark J Musante 					    iarg->guid == this_guid;
125324e697d4Sck 				}
125424e697d4Sck 				if (!matched) {
125524e697d4Sck 					nvlist_free(config);
1256078266a5SMarcel Telka 				} else {
1257078266a5SMarcel Telka 					/*
1258078266a5SMarcel Telka 					 * use the non-raw path for the config
1259078266a5SMarcel Telka 					 */
1260078266a5SMarcel Telka 					(void) strlcpy(end, slice->rn_name,
1261078266a5SMarcel Telka 					    pathleft);
1262078266a5SMarcel Telka 					if (add_config(hdl, &pools, path,
1263078266a5SMarcel Telka 					    config) != 0)
1264078266a5SMarcel Telka 						config_failed = B_TRUE;
126524e697d4Sck 				}
1266842c5645Sjwadams 			}
12674f67d755SEric Taylor 			free(slice->rn_name);
12684f67d755SEric Taylor 			free(slice);
1269fa9e4066Sahrens 		}
12704f67d755SEric Taylor 		avl_destroy(&slice_cache);
1271ccae0b50Seschrock 
1272ccae0b50Seschrock 		(void) closedir(dirp);
1273078266a5SMarcel Telka 
1274078266a5SMarcel Telka 		if (config_failed)
1275078266a5SMarcel Telka 			goto error;
1276fa9e4066Sahrens 	}
1277fa9e4066Sahrens 
1278d41c4376SMark J Musante 	ret = get_configs(hdl, &pools, iarg->can_be_active);
127999653d4eSeschrock 
128099653d4eSeschrock error:
128199653d4eSeschrock 	for (pe = pools.pools; pe != NULL; pe = penext) {
128299653d4eSeschrock 		penext = pe->pe_next;
128399653d4eSeschrock 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
128499653d4eSeschrock 			venext = ve->ve_next;
128599653d4eSeschrock 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
128699653d4eSeschrock 				cenext = ce->ce_next;
1287aab83bb8SJosef 'Jeff' Sipek 				nvlist_free(ce->ce_config);
128899653d4eSeschrock 				free(ce);
128999653d4eSeschrock 			}
129099653d4eSeschrock 			free(ve);
129199653d4eSeschrock 		}
129299653d4eSeschrock 		free(pe);
129399653d4eSeschrock 	}
129499653d4eSeschrock 
129599653d4eSeschrock 	for (ne = pools.names; ne != NULL; ne = nenext) {
129699653d4eSeschrock 		nenext = ne->ne_next;
1297078266a5SMarcel Telka 		free(ne->ne_name);
129899653d4eSeschrock 		free(ne);
129999653d4eSeschrock 	}
130099653d4eSeschrock 
1301fa9e4066Sahrens 	return (ret);
1302fa9e4066Sahrens }
1303fa9e4066Sahrens 
130424e697d4Sck nvlist_t *
130524e697d4Sck zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
130624e697d4Sck {
1307d41c4376SMark J Musante 	importargs_t iarg = { 0 };
130824e697d4Sck 
1309d41c4376SMark J Musante 	iarg.paths = argc;
1310d41c4376SMark J Musante 	iarg.path = argv;
131124e697d4Sck 
1312d41c4376SMark J Musante 	return (zpool_find_import_impl(hdl, &iarg));
131324e697d4Sck }
131424e697d4Sck 
13152f8aaab3Seschrock /*
13162f8aaab3Seschrock  * Given a cache file, return the contents as a list of importable pools.
131724e697d4Sck  * poolname or guid (but not both) are provided by the caller when trying
131824e697d4Sck  * to import a specific pool.
13192f8aaab3Seschrock  */
13202f8aaab3Seschrock nvlist_t *
13213a57275aSck zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
1322e829d913Sck     char *poolname, uint64_t guid)
13232f8aaab3Seschrock {
13242f8aaab3Seschrock 	char *buf;
13252f8aaab3Seschrock 	int fd;
13262f8aaab3Seschrock 	struct stat64 statbuf;
13272f8aaab3Seschrock 	nvlist_t *raw, *src, *dst;
13282f8aaab3Seschrock 	nvlist_t *pools;
13292f8aaab3Seschrock 	nvpair_t *elem;
13302f8aaab3Seschrock 	char *name;
133124e697d4Sck 	uint64_t this_guid;
13322f8aaab3Seschrock 	boolean_t active;
13332f8aaab3Seschrock 
133424e697d4Sck 	verify(poolname == NULL || guid == 0);
133524e697d4Sck 
13362f8aaab3Seschrock 	if ((fd = open(cachefile, O_RDONLY)) < 0) {
13372f8aaab3Seschrock 		zfs_error_aux(hdl, "%s", strerror(errno));
13382f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13392f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN, "failed to open cache file"));
13402f8aaab3Seschrock 		return (NULL);
13412f8aaab3Seschrock 	}
13422f8aaab3Seschrock 
13432f8aaab3Seschrock 	if (fstat64(fd, &statbuf) != 0) {
13442f8aaab3Seschrock 		zfs_error_aux(hdl, "%s", strerror(errno));
13452f8aaab3Seschrock 		(void) close(fd);
13462f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13472f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
13482f8aaab3Seschrock 		return (NULL);
13492f8aaab3Seschrock 	}
13502f8aaab3Seschrock 
13512f8aaab3Seschrock 	if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
13522f8aaab3Seschrock 		(void) close(fd);
13532f8aaab3Seschrock 		return (NULL);
13542f8aaab3Seschrock 	}
13552f8aaab3Seschrock 
13562f8aaab3Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
13572f8aaab3Seschrock 		(void) close(fd);
13582f8aaab3Seschrock 		free(buf);
13592f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13602f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN,
13612f8aaab3Seschrock 		    "failed to read cache file contents"));
13622f8aaab3Seschrock 		return (NULL);
13632f8aaab3Seschrock 	}
13642f8aaab3Seschrock 
13652f8aaab3Seschrock 	(void) close(fd);
13662f8aaab3Seschrock 
13672f8aaab3Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
13682f8aaab3Seschrock 		free(buf);
13692f8aaab3Seschrock 		(void) zfs_error(hdl, EZFS_BADCACHE,
13702f8aaab3Seschrock 		    dgettext(TEXT_DOMAIN,
13712f8aaab3Seschrock 		    "invalid or corrupt cache file contents"));
13722f8aaab3Seschrock 		return (NULL);
13732f8aaab3Seschrock 	}
13742f8aaab3Seschrock 
13752f8aaab3Seschrock 	free(buf);
13762f8aaab3Seschrock 
13772f8aaab3Seschrock 	/*
13782f8aaab3Seschrock 	 * Go through and get the current state of the pools and refresh their
13792f8aaab3Seschrock 	 * state.
13802f8aaab3Seschrock 	 */
13812f8aaab3Seschrock 	if (nvlist_alloc(&pools, 0, 0) != 0) {
13822f8aaab3Seschrock 		(void) no_memory(hdl);
13832f8aaab3Seschrock 		nvlist_free(raw);
13842f8aaab3Seschrock 		return (NULL);
13852f8aaab3Seschrock 	}
13862f8aaab3Seschrock 
13872f8aaab3Seschrock 	elem = NULL;
13882f8aaab3Seschrock 	while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1389b18d6b0eSMatthew Ahrens 		src = fnvpair_value_nvlist(elem);
13902f8aaab3Seschrock 
1391b18d6b0eSMatthew Ahrens 		name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
139224e697d4Sck 		if (poolname != NULL && strcmp(poolname, name) != 0)
139324e697d4Sck 			continue;
139424e697d4Sck 
1395b18d6b0eSMatthew Ahrens 		this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1396b18d6b0eSMatthew Ahrens 		if (guid != 0 && guid != this_guid)
1397b18d6b0eSMatthew Ahrens 			continue;
13982f8aaab3Seschrock 
1399e829d913Sck 		if (pool_active(hdl, name, this_guid, &active) != 0) {
1400e829d913Sck 			nvlist_free(raw);
1401e829d913Sck 			nvlist_free(pools);
1402e829d913Sck 			return (NULL);
1403e829d913Sck 		}
14042f8aaab3Seschrock 
1405e829d913Sck 		if (active)
1406e829d913Sck 			continue;
14072f8aaab3Seschrock 
1408e829d913Sck 		if ((dst = refresh_config(hdl, src)) == NULL) {
1409e829d913Sck 			nvlist_free(raw);
1410e829d913Sck 			nvlist_free(pools);
1411e829d913Sck 			return (NULL);
1412e829d913Sck 		}
14132f8aaab3Seschrock 
1414e829d913Sck 		if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1415e829d913Sck 			(void) no_memory(hdl);
14162f8aaab3Seschrock 			nvlist_free(dst);
1417e829d913Sck 			nvlist_free(raw);
1418e829d913Sck 			nvlist_free(pools);
1419e829d913Sck 			return (NULL);
14202f8aaab3Seschrock 		}
1421e829d913Sck 		nvlist_free(dst);
14222f8aaab3Seschrock 	}
14232f8aaab3Seschrock 
14242f8aaab3Seschrock 	nvlist_free(raw);
14252f8aaab3Seschrock 	return (pools);
14262f8aaab3Seschrock }
14272f8aaab3Seschrock 
1428d41c4376SMark J Musante static int
1429d41c4376SMark J Musante name_or_guid_exists(zpool_handle_t *zhp, void *data)
1430d41c4376SMark J Musante {
1431d41c4376SMark J Musante 	importargs_t *import = data;
1432d41c4376SMark J Musante 	int found = 0;
1433d41c4376SMark J Musante 
1434d41c4376SMark J Musante 	if (import->poolname != NULL) {
1435d41c4376SMark J Musante 		char *pool_name;
1436d41c4376SMark J Musante 
1437d41c4376SMark J Musante 		verify(nvlist_lookup_string(zhp->zpool_config,
1438d41c4376SMark J Musante 		    ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0);
1439d41c4376SMark J Musante 		if (strcmp(pool_name, import->poolname) == 0)
1440d41c4376SMark J Musante 			found = 1;
1441d41c4376SMark J Musante 	} else {
1442d41c4376SMark J Musante 		uint64_t pool_guid;
1443d41c4376SMark J Musante 
1444d41c4376SMark J Musante 		verify(nvlist_lookup_uint64(zhp->zpool_config,
1445d41c4376SMark J Musante 		    ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0);
1446d41c4376SMark J Musante 		if (pool_guid == import->guid)
1447d41c4376SMark J Musante 			found = 1;
1448d41c4376SMark J Musante 	}
1449d41c4376SMark J Musante 
1450d41c4376SMark J Musante 	zpool_close(zhp);
1451d41c4376SMark J Musante 	return (found);
1452d41c4376SMark J Musante }
1453d41c4376SMark J Musante 
1454d41c4376SMark J Musante nvlist_t *
1455d41c4376SMark J Musante zpool_search_import(libzfs_handle_t *hdl, importargs_t *import)
1456d41c4376SMark J Musante {
1457d41c4376SMark J Musante 	verify(import->poolname == NULL || import->guid == 0);
1458d41c4376SMark J Musante 
1459d41c4376SMark J Musante 	if (import->unique)
1460d41c4376SMark J Musante 		import->exists = zpool_iter(hdl, name_or_guid_exists, import);
1461d41c4376SMark J Musante 
1462d41c4376SMark J Musante 	if (import->cachefile != NULL)
1463d41c4376SMark J Musante 		return (zpool_find_import_cached(hdl, import->cachefile,
1464d41c4376SMark J Musante 		    import->poolname, import->guid));
1465d41c4376SMark J Musante 
1466d41c4376SMark J Musante 	return (zpool_find_import_impl(hdl, import));
1467d41c4376SMark J Musante }
14682f8aaab3Seschrock 
146999653d4eSeschrock boolean_t
1470fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid)
1471fa9e4066Sahrens {
1472fa9e4066Sahrens 	uint64_t tmp;
1473fa9e4066Sahrens 	nvlist_t **child;
1474fa9e4066Sahrens 	uint_t c, children;
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
1477fa9e4066Sahrens 	if (tmp == guid)
147899653d4eSeschrock 		return (B_TRUE);
1479fa9e4066Sahrens 
1480fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1481fa9e4066Sahrens 	    &child, &children) == 0) {
1482fa9e4066Sahrens 		for (c = 0; c < children; c++)
1483fa9e4066Sahrens 			if (find_guid(child[c], guid))
148499653d4eSeschrock 				return (B_TRUE);
148599653d4eSeschrock 	}
148699653d4eSeschrock 
148799653d4eSeschrock 	return (B_FALSE);
148899653d4eSeschrock }
148999653d4eSeschrock 
1490fa94a07fSbrendan typedef struct aux_cbdata {
1491fa94a07fSbrendan 	const char	*cb_type;
149299653d4eSeschrock 	uint64_t	cb_guid;
149399653d4eSeschrock 	zpool_handle_t	*cb_zhp;
1494fa94a07fSbrendan } aux_cbdata_t;
149599653d4eSeschrock 
149699653d4eSeschrock static int
1497fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data)
149899653d4eSeschrock {
1499fa94a07fSbrendan 	aux_cbdata_t *cbp = data;
1500fa94a07fSbrendan 	nvlist_t **list;
1501fa94a07fSbrendan 	uint_t i, count;
150299653d4eSeschrock 	uint64_t guid;
150399653d4eSeschrock 	nvlist_t *nvroot;
150499653d4eSeschrock 
150599653d4eSeschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
150699653d4eSeschrock 	    &nvroot) == 0);
150799653d4eSeschrock 
1508fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
1509fa94a07fSbrendan 	    &list, &count) == 0) {
1510fa94a07fSbrendan 		for (i = 0; i < count; i++) {
1511fa94a07fSbrendan 			verify(nvlist_lookup_uint64(list[i],
151299653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
151399653d4eSeschrock 			if (guid == cbp->cb_guid) {
151499653d4eSeschrock 				cbp->cb_zhp = zhp;
151599653d4eSeschrock 				return (1);
151699653d4eSeschrock 			}
151799653d4eSeschrock 		}
1518fa9e4066Sahrens 	}
1519fa9e4066Sahrens 
152099653d4eSeschrock 	zpool_close(zhp);
152199653d4eSeschrock 	return (0);
1522fa9e4066Sahrens }
1523fa9e4066Sahrens 
1524fa9e4066Sahrens /*
152599653d4eSeschrock  * Determines if the pool is in use.  If so, it returns true and the state of
1526fa9e4066Sahrens  * the pool as well as the name of the pool.  Both strings are allocated and
1527fa9e4066Sahrens  * must be freed by the caller.
1528fa9e4066Sahrens  */
1529fa9e4066Sahrens int
153099653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
153199653d4eSeschrock     boolean_t *inuse)
1532fa9e4066Sahrens {
1533fa9e4066Sahrens 	nvlist_t *config;
1534fa9e4066Sahrens 	char *name;
153599653d4eSeschrock 	boolean_t ret;
1536fa9e4066Sahrens 	uint64_t guid, vdev_guid;
1537fa9e4066Sahrens 	zpool_handle_t *zhp;
1538fa9e4066Sahrens 	nvlist_t *pool_config;
153939c23413Seschrock 	uint64_t stateval, isspare;
1540fa94a07fSbrendan 	aux_cbdata_t cb = { 0 };
154194de1d4cSeschrock 	boolean_t isactive;
154299653d4eSeschrock 
154399653d4eSeschrock 	*inuse = B_FALSE;
1544fa9e4066Sahrens 
154599653d4eSeschrock 	if (zpool_read_label(fd, &config) != 0) {
154699653d4eSeschrock 		(void) no_memory(hdl);
154799653d4eSeschrock 		return (-1);
154899653d4eSeschrock 	}
154999653d4eSeschrock 
155099653d4eSeschrock 	if (config == NULL)
155199653d4eSeschrock 		return (0);
1552fa9e4066Sahrens 
1553fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
155446a2abf2Seschrock 	    &stateval) == 0);
1555fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
1556fa9e4066Sahrens 	    &vdev_guid) == 0);
1557fa9e4066Sahrens 
1558fa94a07fSbrendan 	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
155999653d4eSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
156099653d4eSeschrock 		    &name) == 0);
156199653d4eSeschrock 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
156299653d4eSeschrock 		    &guid) == 0);
156399653d4eSeschrock 	}
156499653d4eSeschrock 
156546a2abf2Seschrock 	switch (stateval) {
1566fa9e4066Sahrens 	case POOL_STATE_EXPORTED:
1567f9af39baSGeorge Wilson 		/*
1568f9af39baSGeorge Wilson 		 * A pool with an exported state may in fact be imported
1569f9af39baSGeorge Wilson 		 * read-only, so check the in-core state to see if it's
1570f9af39baSGeorge Wilson 		 * active and imported read-only.  If it is, set
1571f9af39baSGeorge Wilson 		 * its state to active.
1572f9af39baSGeorge Wilson 		 */
1573f9af39baSGeorge Wilson 		if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
1574fb13f48fSJosef 'Jeff' Sipek 		    (zhp = zpool_open_canfail(hdl, name)) != NULL) {
1575fb13f48fSJosef 'Jeff' Sipek 			if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
1576fb13f48fSJosef 'Jeff' Sipek 				stateval = POOL_STATE_ACTIVE;
1577fb13f48fSJosef 'Jeff' Sipek 
1578fb13f48fSJosef 'Jeff' Sipek 			/*
1579fb13f48fSJosef 'Jeff' Sipek 			 * All we needed the zpool handle for is the
1580fb13f48fSJosef 'Jeff' Sipek 			 * readonly prop check.
1581fb13f48fSJosef 'Jeff' Sipek 			 */
1582fb13f48fSJosef 'Jeff' Sipek 			zpool_close(zhp);
1583fb13f48fSJosef 'Jeff' Sipek 		}
1584f9af39baSGeorge Wilson 
158599653d4eSeschrock 		ret = B_TRUE;
1586fa9e4066Sahrens 		break;
1587fa9e4066Sahrens 
1588fa9e4066Sahrens 	case POOL_STATE_ACTIVE:
1589fa9e4066Sahrens 		/*
1590fa9e4066Sahrens 		 * For an active pool, we have to determine if it's really part
1591eaca9bbdSeschrock 		 * of a currently active pool (in which case the pool will exist
1592eaca9bbdSeschrock 		 * and the guid will be the same), or whether it's part of an
1593eaca9bbdSeschrock 		 * active pool that was disconnected without being explicitly
1594eaca9bbdSeschrock 		 * exported.
1595fa9e4066Sahrens 		 */
159694de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0) {
159794de1d4cSeschrock 			nvlist_free(config);
159894de1d4cSeschrock 			return (-1);
159994de1d4cSeschrock 		}
160094de1d4cSeschrock 
160194de1d4cSeschrock 		if (isactive) {
1602fa9e4066Sahrens 			/*
1603fa9e4066Sahrens 			 * Because the device may have been removed while
1604fa9e4066Sahrens 			 * offlined, we only report it as active if the vdev is
1605fa9e4066Sahrens 			 * still present in the config.  Otherwise, pretend like
1606fa9e4066Sahrens 			 * it's not in use.
1607fa9e4066Sahrens 			 */
160899653d4eSeschrock 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
1609088e9d47Seschrock 			    (pool_config = zpool_get_config(zhp, NULL))
1610088e9d47Seschrock 			    != NULL) {
1611fa9e4066Sahrens 				nvlist_t *nvroot;
1612fa9e4066Sahrens 
1613fa9e4066Sahrens 				verify(nvlist_lookup_nvlist(pool_config,
1614fa9e4066Sahrens 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
161546a2abf2Seschrock 				ret = find_guid(nvroot, vdev_guid);
1616fa9e4066Sahrens 			} else {
161799653d4eSeschrock 				ret = B_FALSE;
1618fa9e4066Sahrens 			}
161999653d4eSeschrock 
162039c23413Seschrock 			/*
162139c23413Seschrock 			 * If this is an active spare within another pool, we
162239c23413Seschrock 			 * treat it like an unused hot spare.  This allows the
162339c23413Seschrock 			 * user to create a pool with a hot spare that currently
162439c23413Seschrock 			 * in use within another pool.  Since we return B_TRUE,
162539c23413Seschrock 			 * libdiskmgt will continue to prevent generic consumers
162639c23413Seschrock 			 * from using the device.
162739c23413Seschrock 			 */
162839c23413Seschrock 			if (ret && nvlist_lookup_uint64(config,
162939c23413Seschrock 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
163039c23413Seschrock 				stateval = POOL_STATE_SPARE;
163139c23413Seschrock 
163299653d4eSeschrock 			if (zhp != NULL)
163399653d4eSeschrock 				zpool_close(zhp);
1634fa9e4066Sahrens 		} else {
163546a2abf2Seschrock 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
163699653d4eSeschrock 			ret = B_TRUE;
163799653d4eSeschrock 		}
163899653d4eSeschrock 		break;
163999653d4eSeschrock 
164099653d4eSeschrock 	case POOL_STATE_SPARE:
164199653d4eSeschrock 		/*
164299653d4eSeschrock 		 * For a hot spare, it can be either definitively in use, or
164399653d4eSeschrock 		 * potentially active.  To determine if it's in use, we iterate
164499653d4eSeschrock 		 * over all pools in the system and search for one with a spare
164599653d4eSeschrock 		 * with a matching guid.
164699653d4eSeschrock 		 *
164799653d4eSeschrock 		 * Due to the shared nature of spares, we don't actually report
164899653d4eSeschrock 		 * the potentially active case as in use.  This means the user
164999653d4eSeschrock 		 * can freely create pools on the hot spares of exported pools,
165099653d4eSeschrock 		 * but to do otherwise makes the resulting code complicated, and
165199653d4eSeschrock 		 * we end up having to deal with this case anyway.
165299653d4eSeschrock 		 */
165399653d4eSeschrock 		cb.cb_zhp = NULL;
165499653d4eSeschrock 		cb.cb_guid = vdev_guid;
1655fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_SPARES;
1656fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
1657fa94a07fSbrendan 			name = (char *)zpool_get_name(cb.cb_zhp);
1658078266a5SMarcel Telka 			ret = B_TRUE;
1659fa94a07fSbrendan 		} else {
1660078266a5SMarcel Telka 			ret = B_FALSE;
1661fa94a07fSbrendan 		}
1662fa94a07fSbrendan 		break;
1663fa94a07fSbrendan 
1664fa94a07fSbrendan 	case POOL_STATE_L2CACHE:
1665fa94a07fSbrendan 
1666fa94a07fSbrendan 		/*
1667fa94a07fSbrendan 		 * Check if any pool is currently using this l2cache device.
1668fa94a07fSbrendan 		 */
1669fa94a07fSbrendan 		cb.cb_zhp = NULL;
1670fa94a07fSbrendan 		cb.cb_guid = vdev_guid;
1671fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
1672fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
167399653d4eSeschrock 			name = (char *)zpool_get_name(cb.cb_zhp);
1674078266a5SMarcel Telka 			ret = B_TRUE;
167599653d4eSeschrock 		} else {
1676078266a5SMarcel Telka 			ret = B_FALSE;
1677fa9e4066Sahrens 		}
1678fa9e4066Sahrens 		break;
1679fa9e4066Sahrens 
1680fa9e4066Sahrens 	default:
168199653d4eSeschrock 		ret = B_FALSE;
1682fa9e4066Sahrens 	}
1683fa9e4066Sahrens 
168446a2abf2Seschrock 
168546a2abf2Seschrock 	if (ret) {
168699653d4eSeschrock 		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
1687c6765aabSeschrock 			if (cb.cb_zhp)
1688c6765aabSeschrock 				zpool_close(cb.cb_zhp);
168999653d4eSeschrock 			nvlist_free(config);
169099653d4eSeschrock 			return (-1);
169199653d4eSeschrock 		}
169246a2abf2Seschrock 		*state = (pool_state_t)stateval;
169346a2abf2Seschrock 	}
169446a2abf2Seschrock 
169599653d4eSeschrock 	if (cb.cb_zhp)
169699653d4eSeschrock 		zpool_close(cb.cb_zhp);
169799653d4eSeschrock 
1698fa9e4066Sahrens 	nvlist_free(config);
169999653d4eSeschrock 	*inuse = ret;
170099653d4eSeschrock 	return (0);
1701fa9e4066Sahrens }
1702