xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_import.c (revision e7437265dc2a4920c197ed4337665539d358b22c)
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 /*
2239c23413Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * Pool import support functions.
30fa9e4066Sahrens  *
31fa9e4066Sahrens  * To import a pool, we rely on reading the configuration information from the
32fa9e4066Sahrens  * ZFS label of each device.  If we successfully read the label, then we
33fa9e4066Sahrens  * organize the configuration information in the following hierarchy:
34fa9e4066Sahrens  *
35fa9e4066Sahrens  * 	pool guid -> toplevel vdev guid -> label txg
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * Duplicate entries matching this same tuple will be discarded.  Once we have
38fa9e4066Sahrens  * examined every device, we pick the best label txg config for each toplevel
39fa9e4066Sahrens  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
40fa9e4066Sahrens  * update any paths that have changed.  Finally, we attempt to import the pool
41fa9e4066Sahrens  * using our derived config, and record the results.
42fa9e4066Sahrens  */
43fa9e4066Sahrens 
44fa9e4066Sahrens #include <devid.h>
45fa9e4066Sahrens #include <dirent.h>
46fa9e4066Sahrens #include <errno.h>
47fa9e4066Sahrens #include <libintl.h>
48fa9e4066Sahrens #include <stdlib.h>
49fa9e4066Sahrens #include <string.h>
50fa9e4066Sahrens #include <sys/stat.h>
51fa9e4066Sahrens #include <unistd.h>
52fa9e4066Sahrens #include <fcntl.h>
53fa9e4066Sahrens 
54fa9e4066Sahrens #include <sys/vdev_impl.h>
55fa9e4066Sahrens 
56fa9e4066Sahrens #include "libzfs.h"
57fa9e4066Sahrens #include "libzfs_impl.h"
58fa9e4066Sahrens 
59fa9e4066Sahrens /*
60fa9e4066Sahrens  * Intermediate structures used to gather configuration information.
61fa9e4066Sahrens  */
62fa9e4066Sahrens typedef struct config_entry {
63fa9e4066Sahrens 	uint64_t		ce_txg;
64fa9e4066Sahrens 	nvlist_t		*ce_config;
65fa9e4066Sahrens 	struct config_entry	*ce_next;
66fa9e4066Sahrens } config_entry_t;
67fa9e4066Sahrens 
68fa9e4066Sahrens typedef struct vdev_entry {
69fa9e4066Sahrens 	uint64_t		ve_guid;
70fa9e4066Sahrens 	config_entry_t		*ve_configs;
71fa9e4066Sahrens 	struct vdev_entry	*ve_next;
72fa9e4066Sahrens } vdev_entry_t;
73fa9e4066Sahrens 
74fa9e4066Sahrens typedef struct pool_entry {
75fa9e4066Sahrens 	uint64_t		pe_guid;
76fa9e4066Sahrens 	vdev_entry_t		*pe_vdevs;
77fa9e4066Sahrens 	struct pool_entry	*pe_next;
78fa9e4066Sahrens } pool_entry_t;
79fa9e4066Sahrens 
80fa9e4066Sahrens typedef struct name_entry {
8199653d4eSeschrock 	char			*ne_name;
82fa9e4066Sahrens 	uint64_t		ne_guid;
83fa9e4066Sahrens 	struct name_entry	*ne_next;
84fa9e4066Sahrens } name_entry_t;
85fa9e4066Sahrens 
86fa9e4066Sahrens typedef struct pool_list {
87fa9e4066Sahrens 	pool_entry_t		*pools;
88fa9e4066Sahrens 	name_entry_t		*names;
89fa9e4066Sahrens } pool_list_t;
90fa9e4066Sahrens 
91fa9e4066Sahrens static char *
92fa9e4066Sahrens get_devid(const char *path)
93fa9e4066Sahrens {
94fa9e4066Sahrens 	int fd;
95fa9e4066Sahrens 	ddi_devid_t devid;
96fa9e4066Sahrens 	char *minor, *ret;
97fa9e4066Sahrens 
98fa9e4066Sahrens 	if ((fd = open(path, O_RDONLY)) < 0)
99fa9e4066Sahrens 		return (NULL);
100fa9e4066Sahrens 
101fa9e4066Sahrens 	minor = NULL;
102fa9e4066Sahrens 	ret = NULL;
103fa9e4066Sahrens 	if (devid_get(fd, &devid) == 0) {
104fa9e4066Sahrens 		if (devid_get_minor_name(fd, &minor) == 0)
105fa9e4066Sahrens 			ret = devid_str_encode(devid, minor);
106fa9e4066Sahrens 		if (minor != NULL)
107fa9e4066Sahrens 			devid_str_free(minor);
108fa9e4066Sahrens 		devid_free(devid);
109fa9e4066Sahrens 	}
110c67d9675Seschrock 	(void) close(fd);
111fa9e4066Sahrens 
112fa9e4066Sahrens 	return (ret);
113fa9e4066Sahrens }
114fa9e4066Sahrens 
115fa9e4066Sahrens 
116fa9e4066Sahrens /*
117fa9e4066Sahrens  * Go through and fix up any path and/or devid information for the given vdev
118fa9e4066Sahrens  * configuration.
119fa9e4066Sahrens  */
12099653d4eSeschrock static int
121fa9e4066Sahrens fix_paths(nvlist_t *nv, name_entry_t *names)
122fa9e4066Sahrens {
123fa9e4066Sahrens 	nvlist_t **child;
124fa9e4066Sahrens 	uint_t c, children;
125fa9e4066Sahrens 	uint64_t guid;
126c67d9675Seschrock 	name_entry_t *ne, *best;
127c67d9675Seschrock 	char *path, *devid;
128c67d9675Seschrock 	int matched;
129fa9e4066Sahrens 
130fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
131fa9e4066Sahrens 	    &child, &children) == 0) {
132fa9e4066Sahrens 		for (c = 0; c < children; c++)
13399653d4eSeschrock 			if (fix_paths(child[c], names) != 0)
13499653d4eSeschrock 				return (-1);
13599653d4eSeschrock 		return (0);
136fa9e4066Sahrens 	}
137fa9e4066Sahrens 
138fa9e4066Sahrens 	/*
139fa9e4066Sahrens 	 * This is a leaf (file or disk) vdev.  In either case, go through
140fa9e4066Sahrens 	 * the name list and see if we find a matching guid.  If so, replace
141fa9e4066Sahrens 	 * the path and see if we can calculate a new devid.
142c67d9675Seschrock 	 *
143c67d9675Seschrock 	 * There may be multiple names associated with a particular guid, in
144c67d9675Seschrock 	 * which case we have overlapping slices or multiple paths to the same
145c67d9675Seschrock 	 * disk.  If this is the case, then we want to pick the path that is
146c67d9675Seschrock 	 * the most similar to the original, where "most similar" is the number
147c67d9675Seschrock 	 * of matching characters starting from the end of the path.  This will
148c67d9675Seschrock 	 * preserve slice numbers even if the disks have been reorganized, and
149c67d9675Seschrock 	 * will also catch preferred disk names if multiple paths exist.
150fa9e4066Sahrens 	 */
151fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
152c67d9675Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
153c67d9675Seschrock 		path = NULL;
154c67d9675Seschrock 
155c67d9675Seschrock 	matched = 0;
156c67d9675Seschrock 	best = NULL;
157c67d9675Seschrock 	for (ne = names; ne != NULL; ne = ne->ne_next) {
158c67d9675Seschrock 		if (ne->ne_guid == guid) {
159c67d9675Seschrock 			const char *src, *dst;
160c67d9675Seschrock 			int count;
161c67d9675Seschrock 
162c67d9675Seschrock 			if (path == NULL) {
163c67d9675Seschrock 				best = ne;
164c67d9675Seschrock 				break;
165c67d9675Seschrock 			}
166c67d9675Seschrock 
167c67d9675Seschrock 			src = ne->ne_name + strlen(ne->ne_name) - 1;
168c67d9675Seschrock 			dst = path + strlen(path) - 1;
169c67d9675Seschrock 			for (count = 0; src >= ne->ne_name && dst >= path;
170c67d9675Seschrock 			    src--, dst--, count++)
171c67d9675Seschrock 				if (*src != *dst)
172c67d9675Seschrock 					break;
173c67d9675Seschrock 
174c67d9675Seschrock 			/*
175c67d9675Seschrock 			 * At this point, 'count' is the number of characters
176c67d9675Seschrock 			 * matched from the end.
177c67d9675Seschrock 			 */
178c67d9675Seschrock 			if (count > matched || best == NULL) {
179c67d9675Seschrock 				best = ne;
180c67d9675Seschrock 				matched = count;
181c67d9675Seschrock 			}
182c67d9675Seschrock 		}
183c67d9675Seschrock 	}
184fa9e4066Sahrens 
185c67d9675Seschrock 	if (best == NULL)
18699653d4eSeschrock 		return (0);
187fa9e4066Sahrens 
18899653d4eSeschrock 	if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
18999653d4eSeschrock 		return (-1);
190fa9e4066Sahrens 
191c67d9675Seschrock 	if ((devid = get_devid(best->ne_name)) == NULL) {
192fa9e4066Sahrens 		(void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
193fa9e4066Sahrens 	} else {
19499653d4eSeschrock 		if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
19599653d4eSeschrock 			return (-1);
196fa9e4066Sahrens 		devid_str_free(devid);
197fa9e4066Sahrens 	}
19899653d4eSeschrock 
19999653d4eSeschrock 	return (0);
200fa9e4066Sahrens }
201fa9e4066Sahrens 
202fa9e4066Sahrens /*
203fa9e4066Sahrens  * Add the given configuration to the list of known devices.
204fa9e4066Sahrens  */
20599653d4eSeschrock static int
20699653d4eSeschrock add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
20799653d4eSeschrock     nvlist_t *config)
208fa9e4066Sahrens {
20999653d4eSeschrock 	uint64_t pool_guid, vdev_guid, top_guid, txg, state;
210fa9e4066Sahrens 	pool_entry_t *pe;
211fa9e4066Sahrens 	vdev_entry_t *ve;
212fa9e4066Sahrens 	config_entry_t *ce;
213fa9e4066Sahrens 	name_entry_t *ne;
214fa9e4066Sahrens 
21599653d4eSeschrock 	/*
21699653d4eSeschrock 	 * If this is a hot spare not currently in use, add it to the list of
21799653d4eSeschrock 	 * names to translate, but don't do anything else.
21899653d4eSeschrock 	 */
21999653d4eSeschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
22099653d4eSeschrock 	    &state) == 0 && state == POOL_STATE_SPARE &&
22199653d4eSeschrock 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
22299653d4eSeschrock 		if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
223ccae0b50Seschrock 			return (-1);
22499653d4eSeschrock 
22599653d4eSeschrock 		if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
22699653d4eSeschrock 			free(ne);
22799653d4eSeschrock 			return (-1);
22899653d4eSeschrock 		}
22999653d4eSeschrock 		ne->ne_guid = vdev_guid;
23099653d4eSeschrock 		ne->ne_next = pl->names;
23199653d4eSeschrock 		pl->names = ne;
23299653d4eSeschrock 		return (0);
23399653d4eSeschrock 	}
23499653d4eSeschrock 
235fa9e4066Sahrens 	/*
236fa9e4066Sahrens 	 * If we have a valid config but cannot read any of these fields, then
237fa9e4066Sahrens 	 * it means we have a half-initialized label.  In vdev_label_init()
238fa9e4066Sahrens 	 * we write a label with txg == 0 so that we can identify the device
239fa9e4066Sahrens 	 * in case the user refers to the same disk later on.  If we fail to
240fa9e4066Sahrens 	 * create the pool, we'll be left with a label in this state
241fa9e4066Sahrens 	 * which should not be considered part of a valid pool.
242fa9e4066Sahrens 	 */
243fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
244fa9e4066Sahrens 	    &pool_guid) != 0 ||
245fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
246fa9e4066Sahrens 	    &vdev_guid) != 0 ||
247fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
248fa9e4066Sahrens 	    &top_guid) != 0 ||
249fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
250fa9e4066Sahrens 	    &txg) != 0 || txg == 0) {
251fa9e4066Sahrens 		nvlist_free(config);
25299653d4eSeschrock 		return (0);
253fa9e4066Sahrens 	}
254fa9e4066Sahrens 
255fa9e4066Sahrens 	/*
256fa9e4066Sahrens 	 * First, see if we know about this pool.  If not, then add it to the
257fa9e4066Sahrens 	 * list of known pools.
258fa9e4066Sahrens 	 */
259fa9e4066Sahrens 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
260fa9e4066Sahrens 		if (pe->pe_guid == pool_guid)
261fa9e4066Sahrens 			break;
262fa9e4066Sahrens 	}
263fa9e4066Sahrens 
264fa9e4066Sahrens 	if (pe == NULL) {
26599653d4eSeschrock 		if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
26699653d4eSeschrock 			nvlist_free(config);
26799653d4eSeschrock 			return (-1);
26899653d4eSeschrock 		}
269fa9e4066Sahrens 		pe->pe_guid = pool_guid;
270fa9e4066Sahrens 		pe->pe_next = pl->pools;
271fa9e4066Sahrens 		pl->pools = pe;
272fa9e4066Sahrens 	}
273fa9e4066Sahrens 
274fa9e4066Sahrens 	/*
275fa9e4066Sahrens 	 * Second, see if we know about this toplevel vdev.  Add it if its
276fa9e4066Sahrens 	 * missing.
277fa9e4066Sahrens 	 */
278fa9e4066Sahrens 	for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
279fa9e4066Sahrens 		if (ve->ve_guid == top_guid)
280fa9e4066Sahrens 			break;
281fa9e4066Sahrens 	}
282fa9e4066Sahrens 
283fa9e4066Sahrens 	if (ve == NULL) {
28499653d4eSeschrock 		if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
28599653d4eSeschrock 			nvlist_free(config);
28699653d4eSeschrock 			return (-1);
28799653d4eSeschrock 		}
288fa9e4066Sahrens 		ve->ve_guid = top_guid;
289fa9e4066Sahrens 		ve->ve_next = pe->pe_vdevs;
290fa9e4066Sahrens 		pe->pe_vdevs = ve;
291fa9e4066Sahrens 	}
292fa9e4066Sahrens 
293fa9e4066Sahrens 	/*
294fa9e4066Sahrens 	 * Third, see if we have a config with a matching transaction group.  If
295fa9e4066Sahrens 	 * so, then we do nothing.  Otherwise, add it to the list of known
296fa9e4066Sahrens 	 * configs.
297fa9e4066Sahrens 	 */
298fa9e4066Sahrens 	for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
299fa9e4066Sahrens 		if (ce->ce_txg == txg)
300fa9e4066Sahrens 			break;
301fa9e4066Sahrens 	}
302fa9e4066Sahrens 
303fa9e4066Sahrens 	if (ce == NULL) {
30499653d4eSeschrock 		if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
30599653d4eSeschrock 			nvlist_free(config);
30699653d4eSeschrock 			return (-1);
30799653d4eSeschrock 		}
308fa9e4066Sahrens 		ce->ce_txg = txg;
309fa9e4066Sahrens 		ce->ce_config = config;
310fa9e4066Sahrens 		ce->ce_next = ve->ve_configs;
311fa9e4066Sahrens 		ve->ve_configs = ce;
312fa9e4066Sahrens 	} else {
313fa9e4066Sahrens 		nvlist_free(config);
314fa9e4066Sahrens 	}
315fa9e4066Sahrens 
316fa9e4066Sahrens 	/*
317fa9e4066Sahrens 	 * At this point we've successfully added our config to the list of
318fa9e4066Sahrens 	 * known configs.  The last thing to do is add the vdev guid -> path
319fa9e4066Sahrens 	 * mappings so that we can fix up the configuration as necessary before
320fa9e4066Sahrens 	 * doing the import.
321fa9e4066Sahrens 	 */
32299653d4eSeschrock 	if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
32399653d4eSeschrock 		return (-1);
32499653d4eSeschrock 
32599653d4eSeschrock 	if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
32699653d4eSeschrock 		free(ne);
32799653d4eSeschrock 		return (-1);
32899653d4eSeschrock 	}
329fa9e4066Sahrens 
330fa9e4066Sahrens 	ne->ne_guid = vdev_guid;
331fa9e4066Sahrens 	ne->ne_next = pl->names;
332fa9e4066Sahrens 	pl->names = ne;
33399653d4eSeschrock 
33499653d4eSeschrock 	return (0);
335fa9e4066Sahrens }
336fa9e4066Sahrens 
337eaca9bbdSeschrock /*
338eaca9bbdSeschrock  * Returns true if the named pool matches the given GUID.
339eaca9bbdSeschrock  */
34094de1d4cSeschrock static int
34194de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
34294de1d4cSeschrock     boolean_t *isactive)
343eaca9bbdSeschrock {
344eaca9bbdSeschrock 	zpool_handle_t *zhp;
345eaca9bbdSeschrock 	uint64_t theguid;
346eaca9bbdSeschrock 
34794de1d4cSeschrock 	if (zpool_open_silent(hdl, name, &zhp) != 0)
34894de1d4cSeschrock 		return (-1);
34994de1d4cSeschrock 
35094de1d4cSeschrock 	if (zhp == NULL) {
35194de1d4cSeschrock 		*isactive = B_FALSE;
35294de1d4cSeschrock 		return (0);
35394de1d4cSeschrock 	}
354eaca9bbdSeschrock 
355eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
356eaca9bbdSeschrock 	    &theguid) == 0);
357eaca9bbdSeschrock 
358eaca9bbdSeschrock 	zpool_close(zhp);
359eaca9bbdSeschrock 
36094de1d4cSeschrock 	*isactive = (theguid == guid);
36194de1d4cSeschrock 	return (0);
362eaca9bbdSeschrock }
363eaca9bbdSeschrock 
364fa9e4066Sahrens /*
365fa9e4066Sahrens  * Convert our list of pools into the definitive set of configurations.  We
366fa9e4066Sahrens  * start by picking the best config for each toplevel vdev.  Once that's done,
367fa9e4066Sahrens  * we assemble the toplevel vdevs into a full config for the pool.  We make a
368fa9e4066Sahrens  * pass to fix up any incorrect paths, and then add it to the main list to
369fa9e4066Sahrens  * return to the user.
370fa9e4066Sahrens  */
371fa9e4066Sahrens static nvlist_t *
37299653d4eSeschrock get_configs(libzfs_handle_t *hdl, pool_list_t *pl)
373fa9e4066Sahrens {
37499653d4eSeschrock 	pool_entry_t *pe;
37599653d4eSeschrock 	vdev_entry_t *ve;
37699653d4eSeschrock 	config_entry_t *ce;
37799653d4eSeschrock 	nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot;
37899653d4eSeschrock 	nvlist_t **spares;
37999653d4eSeschrock 	uint_t i, nspares;
38099653d4eSeschrock 	boolean_t config_seen;
381fa9e4066Sahrens 	uint64_t best_txg;
38295173954Sek 	char *name, *hostname;
383fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
38499653d4eSeschrock 	uint64_t version, guid;
385fa9e4066Sahrens 	size_t len;
386fa9e4066Sahrens 	int err;
38799653d4eSeschrock 	uint_t children = 0;
38899653d4eSeschrock 	nvlist_t **child = NULL;
38999653d4eSeschrock 	uint_t c;
39094de1d4cSeschrock 	boolean_t isactive;
39195173954Sek 	uint64_t hostid;
392fa9e4066Sahrens 
39399653d4eSeschrock 	if (nvlist_alloc(&ret, 0, 0) != 0)
39499653d4eSeschrock 		goto nomem;
395fa9e4066Sahrens 
39699653d4eSeschrock 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
397fa9e4066Sahrens 		uint64_t id;
398fa9e4066Sahrens 
39999653d4eSeschrock 		if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
40099653d4eSeschrock 			goto nomem;
40199653d4eSeschrock 		config_seen = B_FALSE;
402fa9e4066Sahrens 
403fa9e4066Sahrens 		/*
404fa9e4066Sahrens 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
405fa9e4066Sahrens 		 * from the first one we find, and then go through the rest and
406fa9e4066Sahrens 		 * add them as necessary to the 'vdevs' member of the config.
407fa9e4066Sahrens 		 */
40899653d4eSeschrock 		for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
409fa9e4066Sahrens 
410fa9e4066Sahrens 			/*
411fa9e4066Sahrens 			 * Determine the best configuration for this vdev by
412fa9e4066Sahrens 			 * selecting the config with the latest transaction
413fa9e4066Sahrens 			 * group.
414fa9e4066Sahrens 			 */
415fa9e4066Sahrens 			best_txg = 0;
416fa9e4066Sahrens 			for (ce = ve->ve_configs; ce != NULL;
417fa9e4066Sahrens 			    ce = ce->ce_next) {
418fa9e4066Sahrens 
41999653d4eSeschrock 				if (ce->ce_txg > best_txg) {
420fa9e4066Sahrens 					tmp = ce->ce_config;
42199653d4eSeschrock 					best_txg = ce->ce_txg;
42299653d4eSeschrock 				}
423fa9e4066Sahrens 			}
424fa9e4066Sahrens 
425fa9e4066Sahrens 			if (!config_seen) {
426fa9e4066Sahrens 				/*
427fa9e4066Sahrens 				 * Copy the relevant pieces of data to the pool
428fa9e4066Sahrens 				 * configuration:
429fa9e4066Sahrens 				 *
43099653d4eSeschrock 				 *	version
431fa9e4066Sahrens 				 * 	pool guid
432fa9e4066Sahrens 				 * 	name
433fa9e4066Sahrens 				 * 	pool state
43495173954Sek 				 *	hostid (if available)
43595173954Sek 				 *	hostname (if available)
436fa9e4066Sahrens 				 */
437fa9e4066Sahrens 				uint64_t state;
438fa9e4066Sahrens 
43999653d4eSeschrock 				verify(nvlist_lookup_uint64(tmp,
44099653d4eSeschrock 				    ZPOOL_CONFIG_VERSION, &version) == 0);
44199653d4eSeschrock 				if (nvlist_add_uint64(config,
44299653d4eSeschrock 				    ZPOOL_CONFIG_VERSION, version) != 0)
44399653d4eSeschrock 					goto nomem;
444fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
445fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
44699653d4eSeschrock 				if (nvlist_add_uint64(config,
44799653d4eSeschrock 				    ZPOOL_CONFIG_POOL_GUID, guid) != 0)
44899653d4eSeschrock 					goto nomem;
449fa9e4066Sahrens 				verify(nvlist_lookup_string(tmp,
450fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
45199653d4eSeschrock 				if (nvlist_add_string(config,
45299653d4eSeschrock 				    ZPOOL_CONFIG_POOL_NAME, name) != 0)
45399653d4eSeschrock 					goto nomem;
454fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
455fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
45699653d4eSeschrock 				if (nvlist_add_uint64(config,
45799653d4eSeschrock 				    ZPOOL_CONFIG_POOL_STATE, state) != 0)
45899653d4eSeschrock 					goto nomem;
45995173954Sek 				hostid = 0;
46095173954Sek 				if (nvlist_lookup_uint64(tmp,
46195173954Sek 				    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
46295173954Sek 					if (nvlist_add_uint64(config,
46395173954Sek 					    ZPOOL_CONFIG_HOSTID, hostid) != 0)
46495173954Sek 						goto nomem;
46595173954Sek 					verify(nvlist_lookup_string(tmp,
46695173954Sek 					    ZPOOL_CONFIG_HOSTNAME,
46795173954Sek 					    &hostname) == 0);
46895173954Sek 					if (nvlist_add_string(config,
46995173954Sek 					    ZPOOL_CONFIG_HOSTNAME,
47095173954Sek 					    hostname) != 0)
47195173954Sek 						goto nomem;
47295173954Sek 				}
473fa9e4066Sahrens 
47499653d4eSeschrock 				config_seen = B_TRUE;
475fa9e4066Sahrens 			}
476fa9e4066Sahrens 
477fa9e4066Sahrens 			/*
478fa9e4066Sahrens 			 * Add this top-level vdev to the child array.
479fa9e4066Sahrens 			 */
480fa9e4066Sahrens 			verify(nvlist_lookup_nvlist(tmp,
481fa9e4066Sahrens 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
482fa9e4066Sahrens 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
483fa9e4066Sahrens 			    &id) == 0);
484fa9e4066Sahrens 			if (id >= children) {
485fa9e4066Sahrens 				nvlist_t **newchild;
486fa9e4066Sahrens 
48799653d4eSeschrock 				newchild = zfs_alloc(hdl, (id + 1) *
488fa9e4066Sahrens 				    sizeof (nvlist_t *));
48999653d4eSeschrock 				if (newchild == NULL)
49099653d4eSeschrock 					goto nomem;
491fa9e4066Sahrens 
492fa9e4066Sahrens 				for (c = 0; c < children; c++)
493fa9e4066Sahrens 					newchild[c] = child[c];
494fa9e4066Sahrens 
495fa9e4066Sahrens 				free(child);
496fa9e4066Sahrens 				child = newchild;
497fa9e4066Sahrens 				children = id + 1;
498fa9e4066Sahrens 			}
49999653d4eSeschrock 			if (nvlist_dup(nvtop, &child[id], 0) != 0)
50099653d4eSeschrock 				goto nomem;
501fa9e4066Sahrens 
502fa9e4066Sahrens 		}
503fa9e4066Sahrens 
504fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
505fa9e4066Sahrens 		    &guid) == 0);
506fa9e4066Sahrens 
507fa9e4066Sahrens 		/*
508fa9e4066Sahrens 		 * Look for any missing top-level vdevs.  If this is the case,
509fa9e4066Sahrens 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
510fa9e4066Sahrens 		 * simply compress the child array, because the kernel performs
511fa9e4066Sahrens 		 * certain checks to make sure the vdev IDs match their location
512fa9e4066Sahrens 		 * in the configuration.
513fa9e4066Sahrens 		 */
514fa9e4066Sahrens 		for (c = 0; c < children; c++)
515fa9e4066Sahrens 			if (child[c] == NULL) {
516fa9e4066Sahrens 				nvlist_t *missing;
51799653d4eSeschrock 				if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
51899653d4eSeschrock 				    0) != 0)
51999653d4eSeschrock 					goto nomem;
52099653d4eSeschrock 				if (nvlist_add_string(missing,
52199653d4eSeschrock 				    ZPOOL_CONFIG_TYPE,
52299653d4eSeschrock 				    VDEV_TYPE_MISSING) != 0 ||
52399653d4eSeschrock 				    nvlist_add_uint64(missing,
52499653d4eSeschrock 				    ZPOOL_CONFIG_ID, c) != 0 ||
52599653d4eSeschrock 				    nvlist_add_uint64(missing,
52699653d4eSeschrock 				    ZPOOL_CONFIG_GUID, 0ULL) != 0) {
52799653d4eSeschrock 					nvlist_free(missing);
52899653d4eSeschrock 					goto nomem;
52999653d4eSeschrock 				}
530fa9e4066Sahrens 				child[c] = missing;
531fa9e4066Sahrens 			}
532fa9e4066Sahrens 
533fa9e4066Sahrens 		/*
534fa9e4066Sahrens 		 * Put all of this pool's top-level vdevs into a root vdev.
535fa9e4066Sahrens 		 */
53699653d4eSeschrock 		if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
53799653d4eSeschrock 			goto nomem;
53899653d4eSeschrock 		if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
53999653d4eSeschrock 		    VDEV_TYPE_ROOT) != 0 ||
54099653d4eSeschrock 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
54199653d4eSeschrock 		    nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
54299653d4eSeschrock 		    nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
54399653d4eSeschrock 		    child, children) != 0) {
54499653d4eSeschrock 			nvlist_free(nvroot);
54599653d4eSeschrock 			goto nomem;
54699653d4eSeschrock 		}
547fa9e4066Sahrens 
548fa9e4066Sahrens 		for (c = 0; c < children; c++)
549fa9e4066Sahrens 			nvlist_free(child[c]);
550fa9e4066Sahrens 		free(child);
55199653d4eSeschrock 		children = 0;
55299653d4eSeschrock 		child = NULL;
553fa9e4066Sahrens 
554fa9e4066Sahrens 		/*
555fa9e4066Sahrens 		 * Go through and fix up any paths and/or devids based on our
556fa9e4066Sahrens 		 * known list of vdev GUID -> path mappings.
557fa9e4066Sahrens 		 */
55899653d4eSeschrock 		if (fix_paths(nvroot, pl->names) != 0) {
55999653d4eSeschrock 			nvlist_free(nvroot);
56099653d4eSeschrock 			goto nomem;
56199653d4eSeschrock 		}
562fa9e4066Sahrens 
563fa9e4066Sahrens 		/*
564fa9e4066Sahrens 		 * Add the root vdev to this pool's configuration.
565fa9e4066Sahrens 		 */
56699653d4eSeschrock 		if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
56799653d4eSeschrock 		    nvroot) != 0) {
56899653d4eSeschrock 			nvlist_free(nvroot);
56999653d4eSeschrock 			goto nomem;
57099653d4eSeschrock 		}
571fa9e4066Sahrens 		nvlist_free(nvroot);
572fa9e4066Sahrens 
573fa9e4066Sahrens 		/*
574fa9e4066Sahrens 		 * Determine if this pool is currently active, in which case we
575fa9e4066Sahrens 		 * can't actually import it.
576fa9e4066Sahrens 		 */
577fa9e4066Sahrens 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
578fa9e4066Sahrens 		    &name) == 0);
579fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
580fa9e4066Sahrens 		    &guid) == 0);
581fa9e4066Sahrens 
58294de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0)
58394de1d4cSeschrock 			goto error;
58494de1d4cSeschrock 
5850192a278Seschrock 		if (isactive) {
586fa9e4066Sahrens 			nvlist_free(config);
58799653d4eSeschrock 			config = NULL;
588fa9e4066Sahrens 			continue;
589fa9e4066Sahrens 		}
590fa9e4066Sahrens 
591fa9e4066Sahrens 		/*
592fa9e4066Sahrens 		 * Try to do the import in order to get vdev state.
593fa9e4066Sahrens 		 */
594e9dbad6fSeschrock 		if (zcmd_write_src_nvlist(hdl, &zc, config, &len) != 0)
595e9dbad6fSeschrock 			goto error;
596fa9e4066Sahrens 
597fa9e4066Sahrens 		nvlist_free(config);
598fa9e4066Sahrens 		config = NULL;
599fa9e4066Sahrens 
600e9dbad6fSeschrock 		if (zcmd_alloc_dst_nvlist(hdl, &zc, len * 2) != 0) {
601e9dbad6fSeschrock 			zcmd_free_nvlists(&zc);
602e9dbad6fSeschrock 			goto error;
603e9dbad6fSeschrock 		}
604fa9e4066Sahrens 
60599653d4eSeschrock 		while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
606fa9e4066Sahrens 		    &zc)) != 0 && errno == ENOMEM) {
607e9dbad6fSeschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
608e9dbad6fSeschrock 				zcmd_free_nvlists(&zc);
609e9dbad6fSeschrock 				goto error;
610e9dbad6fSeschrock 			}
611fa9e4066Sahrens 		}
612fa9e4066Sahrens 
61399653d4eSeschrock 		if (err) {
61499653d4eSeschrock 			(void) zpool_standard_error(hdl, errno,
61599653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot discover pools"));
616e9dbad6fSeschrock 			zcmd_free_nvlists(&zc);
61799653d4eSeschrock 			goto error;
61899653d4eSeschrock 		}
619fa9e4066Sahrens 
620e9dbad6fSeschrock 		if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
621e9dbad6fSeschrock 			zcmd_free_nvlists(&zc);
622e9dbad6fSeschrock 			goto error;
62399653d4eSeschrock 		}
624e9dbad6fSeschrock 
625e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
626fa9e4066Sahrens 
62799653d4eSeschrock 		/*
62899653d4eSeschrock 		 * Go through and update the paths for spares, now that we have
62999653d4eSeschrock 		 * them.
63099653d4eSeschrock 		 */
63199653d4eSeschrock 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
63299653d4eSeschrock 		    &nvroot) == 0);
63399653d4eSeschrock 		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
63499653d4eSeschrock 		    &spares, &nspares) == 0) {
63599653d4eSeschrock 			for (i = 0; i < nspares; i++) {
63699653d4eSeschrock 				if (fix_paths(spares[i], pl->names) != 0)
63799653d4eSeschrock 					goto nomem;
63899653d4eSeschrock 			}
63999653d4eSeschrock 		}
64099653d4eSeschrock 
64195173954Sek 		/*
64295173954Sek 		 * Restore the original information read from the actual label.
64395173954Sek 		 */
64495173954Sek 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
64595173954Sek 		    DATA_TYPE_UINT64);
64695173954Sek 		(void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
64795173954Sek 		    DATA_TYPE_STRING);
64895173954Sek 		if (hostid != 0) {
64995173954Sek 			verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
65095173954Sek 			    hostid) == 0);
65195173954Sek 			verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
65295173954Sek 			    hostname) == 0);
65395173954Sek 		}
65495173954Sek 
655fa9e4066Sahrens 		/*
656fa9e4066Sahrens 		 * Add this pool to the list of configs.
657fa9e4066Sahrens 		 */
658e9dbad6fSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
659e9dbad6fSeschrock 		    &name) == 0);
66099653d4eSeschrock 		if (nvlist_add_nvlist(ret, name, config) != 0)
66199653d4eSeschrock 			goto nomem;
662fa9e4066Sahrens 
663fa9e4066Sahrens 		nvlist_free(config);
66499653d4eSeschrock 		config = NULL;
665fa9e4066Sahrens 	}
666fa9e4066Sahrens 
667fa9e4066Sahrens 	return (ret);
66899653d4eSeschrock 
66999653d4eSeschrock nomem:
67099653d4eSeschrock 	(void) no_memory(hdl);
67199653d4eSeschrock error:
67294de1d4cSeschrock 	nvlist_free(config);
67394de1d4cSeschrock 	nvlist_free(ret);
67499653d4eSeschrock 	for (c = 0; c < children; c++)
67599653d4eSeschrock 		nvlist_free(child[c]);
67694de1d4cSeschrock 	free(child);
67799653d4eSeschrock 
67899653d4eSeschrock 	return (NULL);
679fa9e4066Sahrens }
680fa9e4066Sahrens 
681fa9e4066Sahrens /*
682fa9e4066Sahrens  * Return the offset of the given label.
683fa9e4066Sahrens  */
684fa9e4066Sahrens static uint64_t
685*e7437265Sahrens label_offset(uint64_t size, int l)
686fa9e4066Sahrens {
687*e7437265Sahrens 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
688fa9e4066Sahrens 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
689fa9e4066Sahrens 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
690fa9e4066Sahrens }
691fa9e4066Sahrens 
692fa9e4066Sahrens /*
693fa9e4066Sahrens  * Given a file descriptor, read the label information and return an nvlist
694fa9e4066Sahrens  * describing the configuration, if there is one.
695fa9e4066Sahrens  */
69699653d4eSeschrock int
69799653d4eSeschrock zpool_read_label(int fd, nvlist_t **config)
698fa9e4066Sahrens {
699fa9e4066Sahrens 	struct stat64 statbuf;
700fa9e4066Sahrens 	int l;
701fa9e4066Sahrens 	vdev_label_t *label;
702*e7437265Sahrens 	uint64_t state, txg, size;
703fa9e4066Sahrens 
70499653d4eSeschrock 	*config = NULL;
70599653d4eSeschrock 
706fa9e4066Sahrens 	if (fstat64(fd, &statbuf) == -1)
70799653d4eSeschrock 		return (0);
708*e7437265Sahrens 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
709fa9e4066Sahrens 
71099653d4eSeschrock 	if ((label = malloc(sizeof (vdev_label_t))) == NULL)
71199653d4eSeschrock 		return (-1);
712fa9e4066Sahrens 
713fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
714fa9e4066Sahrens 		if (pread(fd, label, sizeof (vdev_label_t),
715*e7437265Sahrens 		    label_offset(size, l)) != sizeof (vdev_label_t))
716fa9e4066Sahrens 			continue;
717fa9e4066Sahrens 
718fa9e4066Sahrens 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
71999653d4eSeschrock 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
720fa9e4066Sahrens 			continue;
721fa9e4066Sahrens 
72299653d4eSeschrock 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
72399653d4eSeschrock 		    &state) != 0 || state > POOL_STATE_SPARE) {
72499653d4eSeschrock 			nvlist_free(*config);
725fa9e4066Sahrens 			continue;
726fa9e4066Sahrens 		}
727fa9e4066Sahrens 
72899653d4eSeschrock 		if (state != POOL_STATE_SPARE &&
72999653d4eSeschrock 		    (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
73099653d4eSeschrock 		    &txg) != 0 || txg == 0)) {
73199653d4eSeschrock 			nvlist_free(*config);
732fa9e4066Sahrens 			continue;
733fa9e4066Sahrens 		}
734fa9e4066Sahrens 
735fa9e4066Sahrens 		free(label);
73699653d4eSeschrock 		return (0);
737fa9e4066Sahrens 	}
738fa9e4066Sahrens 
739fa9e4066Sahrens 	free(label);
74099653d4eSeschrock 	*config = NULL;
74199653d4eSeschrock 	return (0);
742fa9e4066Sahrens }
743fa9e4066Sahrens 
744fa9e4066Sahrens /*
745fa9e4066Sahrens  * Given a list of directories to search, find all pools stored on disk.  This
746fa9e4066Sahrens  * includes partial pools which are not available to import.  If no args are
747fa9e4066Sahrens  * given (argc is 0), then the default directory (/dev/dsk) is searched.
748fa9e4066Sahrens  */
749fa9e4066Sahrens nvlist_t *
75099653d4eSeschrock zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
751fa9e4066Sahrens {
752fa9e4066Sahrens 	int i;
753ccae0b50Seschrock 	DIR *dirp = NULL;
754fa9e4066Sahrens 	struct dirent64 *dp;
755fa9e4066Sahrens 	char path[MAXPATHLEN];
756fa9e4066Sahrens 	struct stat64 statbuf;
75799653d4eSeschrock 	nvlist_t *ret = NULL, *config;
758fa9e4066Sahrens 	static char *default_dir = "/dev/dsk";
759fa9e4066Sahrens 	int fd;
760fa9e4066Sahrens 	pool_list_t pools = { 0 };
76199653d4eSeschrock 	pool_entry_t *pe, *penext;
76299653d4eSeschrock 	vdev_entry_t *ve, *venext;
76399653d4eSeschrock 	config_entry_t *ce, *cenext;
76499653d4eSeschrock 	name_entry_t *ne, *nenext;
76599653d4eSeschrock 
766fa9e4066Sahrens 
767fa9e4066Sahrens 	if (argc == 0) {
768fa9e4066Sahrens 		argc = 1;
769fa9e4066Sahrens 		argv = &default_dir;
770fa9e4066Sahrens 	}
771fa9e4066Sahrens 
772fa9e4066Sahrens 	/*
773fa9e4066Sahrens 	 * Go through and read the label configuration information from every
774fa9e4066Sahrens 	 * possible device, organizing the information according to pool GUID
775fa9e4066Sahrens 	 * and toplevel GUID.
776fa9e4066Sahrens 	 */
777fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
778fa9e4066Sahrens 		if (argv[i][0] != '/') {
779ece3d9b3Slling 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
78099653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
781fa9e4066Sahrens 			    argv[i]);
78299653d4eSeschrock 			goto error;
783fa9e4066Sahrens 		}
784fa9e4066Sahrens 
785fa9e4066Sahrens 		if ((dirp = opendir(argv[i])) == NULL) {
78699653d4eSeschrock 			zfs_error_aux(hdl, strerror(errno));
787ece3d9b3Slling 			(void) zfs_error_fmt(hdl, EZFS_BADPATH,
78899653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
78999653d4eSeschrock 			    argv[i]);
79099653d4eSeschrock 			goto error;
791fa9e4066Sahrens 		}
792fa9e4066Sahrens 
793fa9e4066Sahrens 		/*
794fa9e4066Sahrens 		 * This is not MT-safe, but we have no MT consumers of libzfs
795fa9e4066Sahrens 		 */
796fa9e4066Sahrens 		while ((dp = readdir64(dirp)) != NULL) {
797fa9e4066Sahrens 
798fa9e4066Sahrens 			(void) snprintf(path, sizeof (path), "%s/%s",
799fa9e4066Sahrens 			    argv[i], dp->d_name);
800fa9e4066Sahrens 
801fa9e4066Sahrens 			if (stat64(path, &statbuf) != 0)
802fa9e4066Sahrens 				continue;
803fa9e4066Sahrens 
804fa9e4066Sahrens 			/*
805fa9e4066Sahrens 			 * Ignore directories (which includes "." and "..").
806fa9e4066Sahrens 			 */
807fa9e4066Sahrens 			if (S_ISDIR(statbuf.st_mode))
808fa9e4066Sahrens 				continue;
8093bb79becSeschrock 
8103bb79becSeschrock 			/*
8113bb79becSeschrock 			 * Ignore special (non-character or non-block) files.
8123bb79becSeschrock 			 */
8133bb79becSeschrock 			if (!S_ISREG(statbuf.st_mode) &&
8143bb79becSeschrock 			    !S_ISBLK(statbuf.st_mode))
8153bb79becSeschrock 				continue;
816fa9e4066Sahrens 
817fa9e4066Sahrens 			if ((fd = open64(path, O_RDONLY)) < 0)
818fa9e4066Sahrens 				continue;
819fa9e4066Sahrens 
82099653d4eSeschrock 			if ((zpool_read_label(fd, &config)) != 0) {
821ccae0b50Seschrock 				(void) close(fd);
82299653d4eSeschrock 				(void) no_memory(hdl);
82399653d4eSeschrock 				goto error;
82499653d4eSeschrock 			}
825fa9e4066Sahrens 
826fa9e4066Sahrens 			(void) close(fd);
827fa9e4066Sahrens 
828fa9e4066Sahrens 			if (config != NULL)
82999653d4eSeschrock 				if (add_config(hdl, &pools, path, config) != 0)
83099653d4eSeschrock 					goto error;
831fa9e4066Sahrens 		}
832ccae0b50Seschrock 
833ccae0b50Seschrock 		(void) closedir(dirp);
834ccae0b50Seschrock 		dirp = NULL;
835fa9e4066Sahrens 	}
836fa9e4066Sahrens 
83799653d4eSeschrock 	ret = get_configs(hdl, &pools);
83899653d4eSeschrock 
83999653d4eSeschrock error:
84099653d4eSeschrock 	for (pe = pools.pools; pe != NULL; pe = penext) {
84199653d4eSeschrock 		penext = pe->pe_next;
84299653d4eSeschrock 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
84399653d4eSeschrock 			venext = ve->ve_next;
84499653d4eSeschrock 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
84599653d4eSeschrock 				cenext = ce->ce_next;
84699653d4eSeschrock 				if (ce->ce_config)
84799653d4eSeschrock 					nvlist_free(ce->ce_config);
84899653d4eSeschrock 				free(ce);
84999653d4eSeschrock 			}
85099653d4eSeschrock 			free(ve);
85199653d4eSeschrock 		}
85299653d4eSeschrock 		free(pe);
85399653d4eSeschrock 	}
85499653d4eSeschrock 
85599653d4eSeschrock 	for (ne = pools.names; ne != NULL; ne = nenext) {
85699653d4eSeschrock 		nenext = ne->ne_next;
85799653d4eSeschrock 		if (ne->ne_name)
85899653d4eSeschrock 			free(ne->ne_name);
85999653d4eSeschrock 		free(ne);
86099653d4eSeschrock 	}
86199653d4eSeschrock 
862ccae0b50Seschrock 	if (dirp)
863ccae0b50Seschrock 		(void) closedir(dirp);
864fa9e4066Sahrens 
865fa9e4066Sahrens 	return (ret);
866fa9e4066Sahrens }
867fa9e4066Sahrens 
86899653d4eSeschrock boolean_t
869fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid)
870fa9e4066Sahrens {
871fa9e4066Sahrens 	uint64_t tmp;
872fa9e4066Sahrens 	nvlist_t **child;
873fa9e4066Sahrens 	uint_t c, children;
874fa9e4066Sahrens 
875fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
876fa9e4066Sahrens 	if (tmp == guid)
87799653d4eSeschrock 		return (B_TRUE);
878fa9e4066Sahrens 
879fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
880fa9e4066Sahrens 	    &child, &children) == 0) {
881fa9e4066Sahrens 		for (c = 0; c < children; c++)
882fa9e4066Sahrens 			if (find_guid(child[c], guid))
88399653d4eSeschrock 				return (B_TRUE);
88499653d4eSeschrock 	}
88599653d4eSeschrock 
88699653d4eSeschrock 	return (B_FALSE);
88799653d4eSeschrock }
88899653d4eSeschrock 
88999653d4eSeschrock typedef struct spare_cbdata {
89099653d4eSeschrock 	uint64_t	cb_guid;
89199653d4eSeschrock 	zpool_handle_t	*cb_zhp;
89299653d4eSeschrock } spare_cbdata_t;
89399653d4eSeschrock 
89499653d4eSeschrock static int
89599653d4eSeschrock find_spare(zpool_handle_t *zhp, void *data)
89699653d4eSeschrock {
89799653d4eSeschrock 	spare_cbdata_t *cbp = data;
89899653d4eSeschrock 	nvlist_t **spares;
89999653d4eSeschrock 	uint_t i, nspares;
90099653d4eSeschrock 	uint64_t guid;
90199653d4eSeschrock 	nvlist_t *nvroot;
90299653d4eSeschrock 
90399653d4eSeschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
90499653d4eSeschrock 	    &nvroot) == 0);
90599653d4eSeschrock 
90699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
90799653d4eSeschrock 	    &spares, &nspares) == 0) {
90899653d4eSeschrock 		for (i = 0; i < nspares; i++) {
90999653d4eSeschrock 			verify(nvlist_lookup_uint64(spares[i],
91099653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
91199653d4eSeschrock 			if (guid == cbp->cb_guid) {
91299653d4eSeschrock 				cbp->cb_zhp = zhp;
91399653d4eSeschrock 				return (1);
91499653d4eSeschrock 			}
91599653d4eSeschrock 		}
916fa9e4066Sahrens 	}
917fa9e4066Sahrens 
91899653d4eSeschrock 	zpool_close(zhp);
91999653d4eSeschrock 	return (0);
920fa9e4066Sahrens }
921fa9e4066Sahrens 
922fa9e4066Sahrens /*
92399653d4eSeschrock  * Determines if the pool is in use.  If so, it returns true and the state of
924fa9e4066Sahrens  * the pool as well as the name of the pool.  Both strings are allocated and
925fa9e4066Sahrens  * must be freed by the caller.
926fa9e4066Sahrens  */
927fa9e4066Sahrens int
92899653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
92999653d4eSeschrock     boolean_t *inuse)
930fa9e4066Sahrens {
931fa9e4066Sahrens 	nvlist_t *config;
932fa9e4066Sahrens 	char *name;
93399653d4eSeschrock 	boolean_t ret;
934fa9e4066Sahrens 	uint64_t guid, vdev_guid;
935fa9e4066Sahrens 	zpool_handle_t *zhp;
936fa9e4066Sahrens 	nvlist_t *pool_config;
93739c23413Seschrock 	uint64_t stateval, isspare;
93899653d4eSeschrock 	spare_cbdata_t cb = { 0 };
93994de1d4cSeschrock 	boolean_t isactive;
94099653d4eSeschrock 
94199653d4eSeschrock 	*inuse = B_FALSE;
942fa9e4066Sahrens 
94399653d4eSeschrock 	if (zpool_read_label(fd, &config) != 0) {
94499653d4eSeschrock 		(void) no_memory(hdl);
94599653d4eSeschrock 		return (-1);
94699653d4eSeschrock 	}
94799653d4eSeschrock 
94899653d4eSeschrock 	if (config == NULL)
94999653d4eSeschrock 		return (0);
950fa9e4066Sahrens 
951fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
95246a2abf2Seschrock 	    &stateval) == 0);
953fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
954fa9e4066Sahrens 	    &vdev_guid) == 0);
955fa9e4066Sahrens 
95699653d4eSeschrock 	if (stateval != POOL_STATE_SPARE) {
95799653d4eSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
95899653d4eSeschrock 		    &name) == 0);
95999653d4eSeschrock 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
96099653d4eSeschrock 		    &guid) == 0);
96199653d4eSeschrock 	}
96299653d4eSeschrock 
96346a2abf2Seschrock 	switch (stateval) {
964fa9e4066Sahrens 	case POOL_STATE_EXPORTED:
96599653d4eSeschrock 		ret = B_TRUE;
966fa9e4066Sahrens 		break;
967fa9e4066Sahrens 
968fa9e4066Sahrens 	case POOL_STATE_ACTIVE:
969fa9e4066Sahrens 		/*
970fa9e4066Sahrens 		 * For an active pool, we have to determine if it's really part
971eaca9bbdSeschrock 		 * of a currently active pool (in which case the pool will exist
972eaca9bbdSeschrock 		 * and the guid will be the same), or whether it's part of an
973eaca9bbdSeschrock 		 * active pool that was disconnected without being explicitly
974eaca9bbdSeschrock 		 * exported.
975fa9e4066Sahrens 		 */
97694de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0) {
97794de1d4cSeschrock 			nvlist_free(config);
97894de1d4cSeschrock 			return (-1);
97994de1d4cSeschrock 		}
98094de1d4cSeschrock 
98194de1d4cSeschrock 		if (isactive) {
982fa9e4066Sahrens 			/*
983fa9e4066Sahrens 			 * Because the device may have been removed while
984fa9e4066Sahrens 			 * offlined, we only report it as active if the vdev is
985fa9e4066Sahrens 			 * still present in the config.  Otherwise, pretend like
986fa9e4066Sahrens 			 * it's not in use.
987fa9e4066Sahrens 			 */
98899653d4eSeschrock 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
989088e9d47Seschrock 			    (pool_config = zpool_get_config(zhp, NULL))
990088e9d47Seschrock 			    != NULL) {
991fa9e4066Sahrens 				nvlist_t *nvroot;
992fa9e4066Sahrens 
993fa9e4066Sahrens 				verify(nvlist_lookup_nvlist(pool_config,
994fa9e4066Sahrens 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
99546a2abf2Seschrock 				ret = find_guid(nvroot, vdev_guid);
996fa9e4066Sahrens 			} else {
99799653d4eSeschrock 				ret = B_FALSE;
998fa9e4066Sahrens 			}
99999653d4eSeschrock 
100039c23413Seschrock 			/*
100139c23413Seschrock 			 * If this is an active spare within another pool, we
100239c23413Seschrock 			 * treat it like an unused hot spare.  This allows the
100339c23413Seschrock 			 * user to create a pool with a hot spare that currently
100439c23413Seschrock 			 * in use within another pool.  Since we return B_TRUE,
100539c23413Seschrock 			 * libdiskmgt will continue to prevent generic consumers
100639c23413Seschrock 			 * from using the device.
100739c23413Seschrock 			 */
100839c23413Seschrock 			if (ret && nvlist_lookup_uint64(config,
100939c23413Seschrock 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
101039c23413Seschrock 				stateval = POOL_STATE_SPARE;
101139c23413Seschrock 
101299653d4eSeschrock 			if (zhp != NULL)
101399653d4eSeschrock 				zpool_close(zhp);
1014fa9e4066Sahrens 		} else {
101546a2abf2Seschrock 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
101699653d4eSeschrock 			ret = B_TRUE;
101799653d4eSeschrock 		}
101899653d4eSeschrock 		break;
101999653d4eSeschrock 
102099653d4eSeschrock 	case POOL_STATE_SPARE:
102199653d4eSeschrock 		/*
102299653d4eSeschrock 		 * For a hot spare, it can be either definitively in use, or
102399653d4eSeschrock 		 * potentially active.  To determine if it's in use, we iterate
102499653d4eSeschrock 		 * over all pools in the system and search for one with a spare
102599653d4eSeschrock 		 * with a matching guid.
102699653d4eSeschrock 		 *
102799653d4eSeschrock 		 * Due to the shared nature of spares, we don't actually report
102899653d4eSeschrock 		 * the potentially active case as in use.  This means the user
102999653d4eSeschrock 		 * can freely create pools on the hot spares of exported pools,
103099653d4eSeschrock 		 * but to do otherwise makes the resulting code complicated, and
103199653d4eSeschrock 		 * we end up having to deal with this case anyway.
103299653d4eSeschrock 		 */
103399653d4eSeschrock 		cb.cb_zhp = NULL;
103499653d4eSeschrock 		cb.cb_guid = vdev_guid;
103599653d4eSeschrock 		if (zpool_iter(hdl, find_spare, &cb) == 1) {
103699653d4eSeschrock 			name = (char *)zpool_get_name(cb.cb_zhp);
1037fa9e4066Sahrens 			ret = TRUE;
103899653d4eSeschrock 		} else {
103999653d4eSeschrock 			ret = FALSE;
1040fa9e4066Sahrens 		}
1041fa9e4066Sahrens 		break;
1042fa9e4066Sahrens 
1043fa9e4066Sahrens 	default:
104499653d4eSeschrock 		ret = B_FALSE;
1045fa9e4066Sahrens 	}
1046fa9e4066Sahrens 
104746a2abf2Seschrock 
104846a2abf2Seschrock 	if (ret) {
104999653d4eSeschrock 		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
105099653d4eSeschrock 			nvlist_free(config);
105199653d4eSeschrock 			return (-1);
105299653d4eSeschrock 		}
105346a2abf2Seschrock 		*state = (pool_state_t)stateval;
105446a2abf2Seschrock 	}
105546a2abf2Seschrock 
105699653d4eSeschrock 	if (cb.cb_zhp)
105799653d4eSeschrock 		zpool_close(cb.cb_zhp);
105899653d4eSeschrock 
1059fa9e4066Sahrens 	nvlist_free(config);
106099653d4eSeschrock 	*inuse = ret;
106199653d4eSeschrock 	return (0);
1062fa9e4066Sahrens }
1063