xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_import.c (revision eaca9bbd5f5d1e4e554da4c7108e8a03c8c33481)
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 /*
2246a2abf2Seschrock  * Copyright 2006 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 {
81fa9e4066Sahrens 	const 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  */
120fa9e4066Sahrens static void
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++)
133fa9e4066Sahrens 			fix_paths(child[c], names);
134fa9e4066Sahrens 		return;
135fa9e4066Sahrens 	}
136fa9e4066Sahrens 
137fa9e4066Sahrens 	/*
138fa9e4066Sahrens 	 * This is a leaf (file or disk) vdev.  In either case, go through
139fa9e4066Sahrens 	 * the name list and see if we find a matching guid.  If so, replace
140fa9e4066Sahrens 	 * the path and see if we can calculate a new devid.
141c67d9675Seschrock 	 *
142c67d9675Seschrock 	 * There may be multiple names associated with a particular guid, in
143c67d9675Seschrock 	 * which case we have overlapping slices or multiple paths to the same
144c67d9675Seschrock 	 * disk.  If this is the case, then we want to pick the path that is
145c67d9675Seschrock 	 * the most similar to the original, where "most similar" is the number
146c67d9675Seschrock 	 * of matching characters starting from the end of the path.  This will
147c67d9675Seschrock 	 * preserve slice numbers even if the disks have been reorganized, and
148c67d9675Seschrock 	 * will also catch preferred disk names if multiple paths exist.
149fa9e4066Sahrens 	 */
150fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
151c67d9675Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
152c67d9675Seschrock 		path = NULL;
153c67d9675Seschrock 
154c67d9675Seschrock 	matched = 0;
155c67d9675Seschrock 	best = NULL;
156c67d9675Seschrock 	for (ne = names; ne != NULL; ne = ne->ne_next) {
157c67d9675Seschrock 		if (ne->ne_guid == guid) {
158c67d9675Seschrock 			const char *src, *dst;
159c67d9675Seschrock 			int count;
160c67d9675Seschrock 
161c67d9675Seschrock 			if (path == NULL) {
162c67d9675Seschrock 				best = ne;
163c67d9675Seschrock 				break;
164c67d9675Seschrock 			}
165c67d9675Seschrock 
166c67d9675Seschrock 			src = ne->ne_name + strlen(ne->ne_name) - 1;
167c67d9675Seschrock 			dst = path + strlen(path) - 1;
168c67d9675Seschrock 			for (count = 0; src >= ne->ne_name && dst >= path;
169c67d9675Seschrock 			    src--, dst--, count++)
170c67d9675Seschrock 				if (*src != *dst)
171c67d9675Seschrock 					break;
172c67d9675Seschrock 
173c67d9675Seschrock 			/*
174c67d9675Seschrock 			 * At this point, 'count' is the number of characters
175c67d9675Seschrock 			 * matched from the end.
176c67d9675Seschrock 			 */
177c67d9675Seschrock 			if (count > matched || best == NULL) {
178c67d9675Seschrock 				best = ne;
179c67d9675Seschrock 				matched = count;
180c67d9675Seschrock 			}
181c67d9675Seschrock 		}
182c67d9675Seschrock 	}
183fa9e4066Sahrens 
184c67d9675Seschrock 	if (best == NULL)
185fa9e4066Sahrens 		return;
186fa9e4066Sahrens 
187c67d9675Seschrock 	verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) == 0);
188fa9e4066Sahrens 
189c67d9675Seschrock 	if ((devid = get_devid(best->ne_name)) == NULL) {
190fa9e4066Sahrens 		(void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
191fa9e4066Sahrens 	} else {
192fa9e4066Sahrens 		verify(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) == 0);
193fa9e4066Sahrens 		devid_str_free(devid);
194fa9e4066Sahrens 	}
195fa9e4066Sahrens }
196fa9e4066Sahrens 
197fa9e4066Sahrens /*
198fa9e4066Sahrens  * Add the given configuration to the list of known devices.
199fa9e4066Sahrens  */
200fa9e4066Sahrens static void
201fa9e4066Sahrens add_config(pool_list_t *pl, const char *path, nvlist_t *config)
202fa9e4066Sahrens {
203fa9e4066Sahrens 	uint64_t pool_guid, vdev_guid, top_guid, txg;
204fa9e4066Sahrens 	pool_entry_t *pe;
205fa9e4066Sahrens 	vdev_entry_t *ve;
206fa9e4066Sahrens 	config_entry_t *ce;
207fa9e4066Sahrens 	name_entry_t *ne;
208fa9e4066Sahrens 
209fa9e4066Sahrens 	/*
210fa9e4066Sahrens 	 * If we have a valid config but cannot read any of these fields, then
211fa9e4066Sahrens 	 * it means we have a half-initialized label.  In vdev_label_init()
212fa9e4066Sahrens 	 * we write a label with txg == 0 so that we can identify the device
213fa9e4066Sahrens 	 * in case the user refers to the same disk later on.  If we fail to
214fa9e4066Sahrens 	 * create the pool, we'll be left with a label in this state
215fa9e4066Sahrens 	 * which should not be considered part of a valid pool.
216fa9e4066Sahrens 	 */
217fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
218fa9e4066Sahrens 	    &pool_guid) != 0 ||
219fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
220fa9e4066Sahrens 	    &vdev_guid) != 0 ||
221fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
222fa9e4066Sahrens 	    &top_guid) != 0 ||
223fa9e4066Sahrens 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
224fa9e4066Sahrens 	    &txg) != 0 || txg == 0) {
225fa9e4066Sahrens 		nvlist_free(config);
226fa9e4066Sahrens 		return;
227fa9e4066Sahrens 	}
228fa9e4066Sahrens 
229fa9e4066Sahrens 	/*
230fa9e4066Sahrens 	 * First, see if we know about this pool.  If not, then add it to the
231fa9e4066Sahrens 	 * list of known pools.
232fa9e4066Sahrens 	 */
233fa9e4066Sahrens 	for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
234fa9e4066Sahrens 		if (pe->pe_guid == pool_guid)
235fa9e4066Sahrens 			break;
236fa9e4066Sahrens 	}
237fa9e4066Sahrens 
238fa9e4066Sahrens 	if (pe == NULL) {
239fa9e4066Sahrens 		pe = zfs_malloc(sizeof (pool_entry_t));
240fa9e4066Sahrens 		pe->pe_guid = pool_guid;
241fa9e4066Sahrens 		pe->pe_next = pl->pools;
242fa9e4066Sahrens 		pl->pools = pe;
243fa9e4066Sahrens 	}
244fa9e4066Sahrens 
245fa9e4066Sahrens 	/*
246fa9e4066Sahrens 	 * Second, see if we know about this toplevel vdev.  Add it if its
247fa9e4066Sahrens 	 * missing.
248fa9e4066Sahrens 	 */
249fa9e4066Sahrens 	for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
250fa9e4066Sahrens 		if (ve->ve_guid == top_guid)
251fa9e4066Sahrens 			break;
252fa9e4066Sahrens 	}
253fa9e4066Sahrens 
254fa9e4066Sahrens 	if (ve == NULL) {
255fa9e4066Sahrens 		ve = zfs_malloc(sizeof (vdev_entry_t));
256fa9e4066Sahrens 		ve->ve_guid = top_guid;
257fa9e4066Sahrens 		ve->ve_next = pe->pe_vdevs;
258fa9e4066Sahrens 		pe->pe_vdevs = ve;
259fa9e4066Sahrens 	}
260fa9e4066Sahrens 
261fa9e4066Sahrens 	/*
262fa9e4066Sahrens 	 * Third, see if we have a config with a matching transaction group.  If
263fa9e4066Sahrens 	 * so, then we do nothing.  Otherwise, add it to the list of known
264fa9e4066Sahrens 	 * configs.
265fa9e4066Sahrens 	 */
266fa9e4066Sahrens 	for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
267fa9e4066Sahrens 		if (ce->ce_txg == txg)
268fa9e4066Sahrens 			break;
269fa9e4066Sahrens 	}
270fa9e4066Sahrens 
271fa9e4066Sahrens 	if (ce == NULL) {
272fa9e4066Sahrens 		ce = zfs_malloc(sizeof (config_entry_t));
273fa9e4066Sahrens 		ce->ce_txg = txg;
274fa9e4066Sahrens 		ce->ce_config = config;
275fa9e4066Sahrens 		ce->ce_next = ve->ve_configs;
276fa9e4066Sahrens 		ve->ve_configs = ce;
277fa9e4066Sahrens 	} else {
278fa9e4066Sahrens 		nvlist_free(config);
279fa9e4066Sahrens 	}
280fa9e4066Sahrens 
281fa9e4066Sahrens 	/*
282fa9e4066Sahrens 	 * At this point we've successfully added our config to the list of
283fa9e4066Sahrens 	 * known configs.  The last thing to do is add the vdev guid -> path
284fa9e4066Sahrens 	 * mappings so that we can fix up the configuration as necessary before
285fa9e4066Sahrens 	 * doing the import.
286fa9e4066Sahrens 	 */
287fa9e4066Sahrens 	ne = zfs_malloc(sizeof (name_entry_t));
288fa9e4066Sahrens 
289fa9e4066Sahrens 	ne->ne_name = zfs_strdup(path);
290fa9e4066Sahrens 	ne->ne_guid = vdev_guid;
291fa9e4066Sahrens 	ne->ne_next = pl->names;
292fa9e4066Sahrens 	pl->names = ne;
293fa9e4066Sahrens }
294fa9e4066Sahrens 
295*eaca9bbdSeschrock /*
296*eaca9bbdSeschrock  * Returns true if the named pool matches the given GUID.
297*eaca9bbdSeschrock  */
298*eaca9bbdSeschrock boolean_t
299*eaca9bbdSeschrock pool_active(const char *name, uint64_t guid)
300*eaca9bbdSeschrock {
301*eaca9bbdSeschrock 	zpool_handle_t *zhp;
302*eaca9bbdSeschrock 	uint64_t theguid;
303*eaca9bbdSeschrock 
304*eaca9bbdSeschrock 	if ((zhp = zpool_open_silent(name)) == NULL)
305*eaca9bbdSeschrock 		return (B_FALSE);
306*eaca9bbdSeschrock 
307*eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
308*eaca9bbdSeschrock 	    &theguid) == 0);
309*eaca9bbdSeschrock 
310*eaca9bbdSeschrock 	zpool_close(zhp);
311*eaca9bbdSeschrock 
312*eaca9bbdSeschrock 	return (theguid == guid);
313*eaca9bbdSeschrock }
314*eaca9bbdSeschrock 
315fa9e4066Sahrens /*
316fa9e4066Sahrens  * Convert our list of pools into the definitive set of configurations.  We
317fa9e4066Sahrens  * start by picking the best config for each toplevel vdev.  Once that's done,
318fa9e4066Sahrens  * we assemble the toplevel vdevs into a full config for the pool.  We make a
319fa9e4066Sahrens  * pass to fix up any incorrect paths, and then add it to the main list to
320fa9e4066Sahrens  * return to the user.
321fa9e4066Sahrens  */
322fa9e4066Sahrens static nvlist_t *
323fa9e4066Sahrens get_configs(pool_list_t *pl)
324fa9e4066Sahrens {
325fa9e4066Sahrens 	pool_entry_t *pe, *penext;
326fa9e4066Sahrens 	vdev_entry_t *ve, *venext;
327fa9e4066Sahrens 	config_entry_t *ce, *cenext;
328fa9e4066Sahrens 	nvlist_t *ret, *config, *tmp, *nvtop, *nvroot;
329fa9e4066Sahrens 	int config_seen;
330fa9e4066Sahrens 	uint64_t best_txg;
331fa9e4066Sahrens 	char *name;
332fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
333fa9e4066Sahrens 	uint64_t guid;
334fa9e4066Sahrens 	char *packed;
335fa9e4066Sahrens 	size_t len;
336fa9e4066Sahrens 	int err;
337fa9e4066Sahrens 
338fa9e4066Sahrens 	verify(nvlist_alloc(&ret, 0, 0) == 0);
339fa9e4066Sahrens 
340fa9e4066Sahrens 	for (pe = pl->pools; pe != NULL; pe = penext) {
341fa9e4066Sahrens 		uint_t c;
342fa9e4066Sahrens 		uint_t children = 0;
343fa9e4066Sahrens 		uint64_t id;
344fa9e4066Sahrens 		nvlist_t **child = NULL;
345fa9e4066Sahrens 
346fa9e4066Sahrens 		penext = pe->pe_next;
347fa9e4066Sahrens 
348fa9e4066Sahrens 		verify(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
349fa9e4066Sahrens 		config_seen = FALSE;
350fa9e4066Sahrens 
351fa9e4066Sahrens 		/*
352fa9e4066Sahrens 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
353fa9e4066Sahrens 		 * from the first one we find, and then go through the rest and
354fa9e4066Sahrens 		 * add them as necessary to the 'vdevs' member of the config.
355fa9e4066Sahrens 		 */
356fa9e4066Sahrens 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
357fa9e4066Sahrens 			venext = ve->ve_next;
358fa9e4066Sahrens 
359fa9e4066Sahrens 			/*
360fa9e4066Sahrens 			 * Determine the best configuration for this vdev by
361fa9e4066Sahrens 			 * selecting the config with the latest transaction
362fa9e4066Sahrens 			 * group.
363fa9e4066Sahrens 			 */
364fa9e4066Sahrens 			best_txg = 0;
365fa9e4066Sahrens 			for (ce = ve->ve_configs; ce != NULL;
366fa9e4066Sahrens 			    ce = ce->ce_next) {
367fa9e4066Sahrens 
368fa9e4066Sahrens 				if (ce->ce_txg > best_txg)
369fa9e4066Sahrens 					tmp = ce->ce_config;
370fa9e4066Sahrens 			}
371fa9e4066Sahrens 
372fa9e4066Sahrens 			if (!config_seen) {
373fa9e4066Sahrens 				/*
374fa9e4066Sahrens 				 * Copy the relevant pieces of data to the pool
375fa9e4066Sahrens 				 * configuration:
376fa9e4066Sahrens 				 *
377fa9e4066Sahrens 				 * 	pool guid
378fa9e4066Sahrens 				 * 	name
379fa9e4066Sahrens 				 * 	pool state
380fa9e4066Sahrens 				 */
381fa9e4066Sahrens 				uint64_t state;
382fa9e4066Sahrens 
383fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
384fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
385fa9e4066Sahrens 				verify(nvlist_add_uint64(config,
386fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_GUID, guid) == 0);
387fa9e4066Sahrens 				verify(nvlist_lookup_string(tmp,
388fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
389fa9e4066Sahrens 				verify(nvlist_add_string(config,
390fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_NAME, name) == 0);
391fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
392fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
393fa9e4066Sahrens 				verify(nvlist_add_uint64(config,
394fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_STATE, state) == 0);
395fa9e4066Sahrens 
396fa9e4066Sahrens 				config_seen = TRUE;
397fa9e4066Sahrens 			}
398fa9e4066Sahrens 
399fa9e4066Sahrens 			/*
400fa9e4066Sahrens 			 * Add this top-level vdev to the child array.
401fa9e4066Sahrens 			 */
402fa9e4066Sahrens 			verify(nvlist_lookup_nvlist(tmp,
403fa9e4066Sahrens 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
404fa9e4066Sahrens 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
405fa9e4066Sahrens 			    &id) == 0);
406fa9e4066Sahrens 			if (id >= children) {
407fa9e4066Sahrens 				nvlist_t **newchild;
408fa9e4066Sahrens 
409fa9e4066Sahrens 				newchild = zfs_malloc((id + 1) *
410fa9e4066Sahrens 				    sizeof (nvlist_t *));
411fa9e4066Sahrens 
412fa9e4066Sahrens 				for (c = 0; c < children; c++)
413fa9e4066Sahrens 					newchild[c] = child[c];
414fa9e4066Sahrens 
415fa9e4066Sahrens 				free(child);
416fa9e4066Sahrens 				child = newchild;
417fa9e4066Sahrens 				children = id + 1;
418fa9e4066Sahrens 			}
419fa9e4066Sahrens 			verify(nvlist_dup(nvtop, &child[id], 0) == 0);
420fa9e4066Sahrens 
421fa9e4066Sahrens 			/*
422fa9e4066Sahrens 			 * Go through and free all config information.
423fa9e4066Sahrens 			 */
424fa9e4066Sahrens 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
425fa9e4066Sahrens 				cenext = ce->ce_next;
426fa9e4066Sahrens 
427fa9e4066Sahrens 				nvlist_free(ce->ce_config);
428fa9e4066Sahrens 				free(ce);
429fa9e4066Sahrens 			}
430fa9e4066Sahrens 
431fa9e4066Sahrens 			/*
432fa9e4066Sahrens 			 * Free this vdev entry, since it has now been merged
433fa9e4066Sahrens 			 * into the main config.
434fa9e4066Sahrens 			 */
435fa9e4066Sahrens 			free(ve);
436fa9e4066Sahrens 		}
437fa9e4066Sahrens 
438fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
439fa9e4066Sahrens 		    &guid) == 0);
440fa9e4066Sahrens 
441fa9e4066Sahrens 		/*
442fa9e4066Sahrens 		 * Look for any missing top-level vdevs.  If this is the case,
443fa9e4066Sahrens 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
444fa9e4066Sahrens 		 * simply compress the child array, because the kernel performs
445fa9e4066Sahrens 		 * certain checks to make sure the vdev IDs match their location
446fa9e4066Sahrens 		 * in the configuration.
447fa9e4066Sahrens 		 */
448fa9e4066Sahrens 		for (c = 0; c < children; c++)
449fa9e4066Sahrens 			if (child[c] == NULL) {
450fa9e4066Sahrens 				nvlist_t *missing;
451fa9e4066Sahrens 				verify(nvlist_alloc(&missing, NV_UNIQUE_NAME,
452fa9e4066Sahrens 				    0) == 0);
453fa9e4066Sahrens 				verify(nvlist_add_string(missing,
454fa9e4066Sahrens 				    ZPOOL_CONFIG_TYPE, VDEV_TYPE_MISSING) == 0);
455fa9e4066Sahrens 				verify(nvlist_add_uint64(missing,
456fa9e4066Sahrens 				    ZPOOL_CONFIG_ID, c) == 0);
457fa9e4066Sahrens 				verify(nvlist_add_uint64(missing,
458fa9e4066Sahrens 				    ZPOOL_CONFIG_GUID, 0ULL) == 0);
459fa9e4066Sahrens 				child[c] = missing;
460fa9e4066Sahrens 			}
461fa9e4066Sahrens 
462fa9e4066Sahrens 		/*
463fa9e4066Sahrens 		 * Put all of this pool's top-level vdevs into a root vdev.
464fa9e4066Sahrens 		 */
465fa9e4066Sahrens 		verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
466fa9e4066Sahrens 		verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
467fa9e4066Sahrens 		    VDEV_TYPE_ROOT) == 0);
468fa9e4066Sahrens 		verify(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
469fa9e4066Sahrens 		verify(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) == 0);
470fa9e4066Sahrens 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
471fa9e4066Sahrens 		    child, children) == 0);
472fa9e4066Sahrens 
473fa9e4066Sahrens 		for (c = 0; c < children; c++)
474fa9e4066Sahrens 			nvlist_free(child[c]);
475fa9e4066Sahrens 		free(child);
476fa9e4066Sahrens 
477fa9e4066Sahrens 		/*
478fa9e4066Sahrens 		 * Go through and fix up any paths and/or devids based on our
479fa9e4066Sahrens 		 * known list of vdev GUID -> path mappings.
480fa9e4066Sahrens 		 */
481fa9e4066Sahrens 		fix_paths(nvroot, pl->names);
482fa9e4066Sahrens 
483fa9e4066Sahrens 		/*
484fa9e4066Sahrens 		 * Add the root vdev to this pool's configuration.
485fa9e4066Sahrens 		 */
486fa9e4066Sahrens 		verify(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
487fa9e4066Sahrens 		    nvroot) == 0);
488fa9e4066Sahrens 		nvlist_free(nvroot);
489fa9e4066Sahrens 
490fa9e4066Sahrens 		/*
491fa9e4066Sahrens 		 * Free this pool entry.
492fa9e4066Sahrens 		 */
493fa9e4066Sahrens 		free(pe);
494fa9e4066Sahrens 
495fa9e4066Sahrens 		/*
496fa9e4066Sahrens 		 * Determine if this pool is currently active, in which case we
497fa9e4066Sahrens 		 * can't actually import it.
498fa9e4066Sahrens 		 */
499fa9e4066Sahrens 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
500fa9e4066Sahrens 		    &name) == 0);
501fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
502fa9e4066Sahrens 		    &guid) == 0);
503fa9e4066Sahrens 
504*eaca9bbdSeschrock 		if (pool_active(name, guid)) {
505fa9e4066Sahrens 			nvlist_free(config);
506fa9e4066Sahrens 			continue;
507fa9e4066Sahrens 		}
508fa9e4066Sahrens 
509fa9e4066Sahrens 		/*
510fa9e4066Sahrens 		 * Try to do the import in order to get vdev state.
511fa9e4066Sahrens 		 */
512fa9e4066Sahrens 		if ((err = nvlist_size(config, &len, NV_ENCODE_NATIVE)) != 0)
513fa9e4066Sahrens 			zfs_baderror(err);
514fa9e4066Sahrens 
515fa9e4066Sahrens 		packed = zfs_malloc(len);
516fa9e4066Sahrens 
517fa9e4066Sahrens 		if ((err = nvlist_pack(config, &packed, &len,
518fa9e4066Sahrens 		    NV_ENCODE_NATIVE, 0)) != 0)
519fa9e4066Sahrens 			zfs_baderror(err);
520fa9e4066Sahrens 
521fa9e4066Sahrens 		nvlist_free(config);
522fa9e4066Sahrens 		config = NULL;
523fa9e4066Sahrens 
524fa9e4066Sahrens 		zc.zc_config_src_size = len;
525fa9e4066Sahrens 		zc.zc_config_src = (uint64_t)(uintptr_t)packed;
526fa9e4066Sahrens 
527fa9e4066Sahrens 		zc.zc_config_dst_size = 2 * len;
528fa9e4066Sahrens 		zc.zc_config_dst = (uint64_t)(uintptr_t)
529fa9e4066Sahrens 		    zfs_malloc(zc.zc_config_dst_size);
530fa9e4066Sahrens 
531ea8dc4b6Seschrock 		while ((err = zfs_ioctl(ZFS_IOC_POOL_TRYIMPORT,
532fa9e4066Sahrens 		    &zc)) != 0 && errno == ENOMEM) {
533fa9e4066Sahrens 			free((void *)(uintptr_t)zc.zc_config_dst);
534fa9e4066Sahrens 			zc.zc_config_dst = (uint64_t)(uintptr_t)
535fa9e4066Sahrens 			    zfs_malloc(zc.zc_config_dst_size);
536fa9e4066Sahrens 		}
537fa9e4066Sahrens 
538fa9e4066Sahrens 		free(packed);
539fa9e4066Sahrens 
540fa9e4066Sahrens 		if (err)
541fa9e4066Sahrens 			zfs_baderror(errno);
542fa9e4066Sahrens 
543fa9e4066Sahrens 		verify(nvlist_unpack((void *)(uintptr_t)zc.zc_config_dst,
544fa9e4066Sahrens 		    zc.zc_config_dst_size, &config, 0) == 0);
545fa9e4066Sahrens 
546fa9e4066Sahrens 		set_pool_health(config);
547fa9e4066Sahrens 
548fa9e4066Sahrens 		/*
549fa9e4066Sahrens 		 * Add this pool to the list of configs.
550fa9e4066Sahrens 		 */
551fa9e4066Sahrens 		verify(nvlist_add_nvlist(ret, name, config) == 0);
552fa9e4066Sahrens 
553fa9e4066Sahrens 		nvlist_free(config);
554fa9e4066Sahrens 
555fa9e4066Sahrens 		free((void *)(uintptr_t)zc.zc_config_dst);
556fa9e4066Sahrens 	}
557fa9e4066Sahrens 
558fa9e4066Sahrens 	return (ret);
559fa9e4066Sahrens }
560fa9e4066Sahrens 
561fa9e4066Sahrens /*
562fa9e4066Sahrens  * Return the offset of the given label.
563fa9e4066Sahrens  */
564fa9e4066Sahrens static uint64_t
565fa9e4066Sahrens label_offset(size_t size, int l)
566fa9e4066Sahrens {
567fa9e4066Sahrens 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
568fa9e4066Sahrens 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
569fa9e4066Sahrens }
570fa9e4066Sahrens 
571fa9e4066Sahrens /*
572fa9e4066Sahrens  * Given a file descriptor, read the label information and return an nvlist
573fa9e4066Sahrens  * describing the configuration, if there is one.
574fa9e4066Sahrens  */
575fa9e4066Sahrens nvlist_t *
576fa9e4066Sahrens zpool_read_label(int fd)
577fa9e4066Sahrens {
578fa9e4066Sahrens 	struct stat64 statbuf;
579fa9e4066Sahrens 	int l;
580fa9e4066Sahrens 	vdev_label_t *label;
581fa9e4066Sahrens 	nvlist_t *config;
582ea8dc4b6Seschrock 	uint64_t state, txg;
583fa9e4066Sahrens 
584fa9e4066Sahrens 	if (fstat64(fd, &statbuf) == -1)
585fa9e4066Sahrens 		return (NULL);
586fa9e4066Sahrens 
587fa9e4066Sahrens 	label = zfs_malloc(sizeof (vdev_label_t));
588fa9e4066Sahrens 
589fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
590fa9e4066Sahrens 		if (pread(fd, label, sizeof (vdev_label_t),
591fa9e4066Sahrens 		    label_offset(statbuf.st_size, l)) != sizeof (vdev_label_t))
592fa9e4066Sahrens 			continue;
593fa9e4066Sahrens 
594fa9e4066Sahrens 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
595fa9e4066Sahrens 		    sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0)
596fa9e4066Sahrens 			continue;
597fa9e4066Sahrens 
598fa9e4066Sahrens 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5994c58d714Sdarrenm 		    &state) != 0 || state > POOL_STATE_DESTROYED) {
600fa9e4066Sahrens 			nvlist_free(config);
601fa9e4066Sahrens 			continue;
602fa9e4066Sahrens 		}
603fa9e4066Sahrens 
604fa9e4066Sahrens 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
605fa9e4066Sahrens 		    &txg) != 0 || txg == 0) {
606fa9e4066Sahrens 			nvlist_free(config);
607fa9e4066Sahrens 			continue;
608fa9e4066Sahrens 		}
609fa9e4066Sahrens 
610fa9e4066Sahrens 		free(label);
611fa9e4066Sahrens 		return (config);
612fa9e4066Sahrens 	}
613fa9e4066Sahrens 
614fa9e4066Sahrens 	free(label);
615fa9e4066Sahrens 	return (NULL);
616fa9e4066Sahrens }
617fa9e4066Sahrens 
618fa9e4066Sahrens /*
619fa9e4066Sahrens  * Given a list of directories to search, find all pools stored on disk.  This
620fa9e4066Sahrens  * includes partial pools which are not available to import.  If no args are
621fa9e4066Sahrens  * given (argc is 0), then the default directory (/dev/dsk) is searched.
622fa9e4066Sahrens  */
623fa9e4066Sahrens nvlist_t *
624fa9e4066Sahrens zpool_find_import(int argc, char **argv)
625fa9e4066Sahrens {
626fa9e4066Sahrens 	int i;
627fa9e4066Sahrens 	DIR *dirp;
628fa9e4066Sahrens 	struct dirent64 *dp;
629fa9e4066Sahrens 	char path[MAXPATHLEN];
630fa9e4066Sahrens 	struct stat64 statbuf;
631fa9e4066Sahrens 	nvlist_t *ret, *config;
632fa9e4066Sahrens 	static char *default_dir = "/dev/dsk";
633fa9e4066Sahrens 	int fd;
634fa9e4066Sahrens 	pool_list_t pools = { 0 };
635fa9e4066Sahrens 
636fa9e4066Sahrens 	if (argc == 0) {
637fa9e4066Sahrens 		argc = 1;
638fa9e4066Sahrens 		argv = &default_dir;
639fa9e4066Sahrens 	}
640fa9e4066Sahrens 
641fa9e4066Sahrens 	/*
642fa9e4066Sahrens 	 * Go through and read the label configuration information from every
643fa9e4066Sahrens 	 * possible device, organizing the information according to pool GUID
644fa9e4066Sahrens 	 * and toplevel GUID.
645fa9e4066Sahrens 	 */
646fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
647fa9e4066Sahrens 		if (argv[i][0] != '/') {
648fa9e4066Sahrens 			zfs_error(dgettext(TEXT_DOMAIN,
649fa9e4066Sahrens 			    "cannot open '%s': must be an absolute path"),
650fa9e4066Sahrens 			    argv[i]);
651fa9e4066Sahrens 			return (NULL);
652fa9e4066Sahrens 		}
653fa9e4066Sahrens 
654fa9e4066Sahrens 		if ((dirp = opendir(argv[i])) == NULL) {
655fa9e4066Sahrens 			zfs_error(dgettext(TEXT_DOMAIN,
656fa9e4066Sahrens 			    "cannot open '%s': %s"), argv[i],
657fa9e4066Sahrens 			    strerror(errno));
658fa9e4066Sahrens 			return (NULL);
659fa9e4066Sahrens 		}
660fa9e4066Sahrens 
661fa9e4066Sahrens 		/*
662fa9e4066Sahrens 		 * This is not MT-safe, but we have no MT consumers of libzfs
663fa9e4066Sahrens 		 */
664fa9e4066Sahrens 		while ((dp = readdir64(dirp)) != NULL) {
665fa9e4066Sahrens 
666fa9e4066Sahrens 			(void) snprintf(path, sizeof (path), "%s/%s",
667fa9e4066Sahrens 			    argv[i], dp->d_name);
668fa9e4066Sahrens 
669fa9e4066Sahrens 			if (stat64(path, &statbuf) != 0)
670fa9e4066Sahrens 				continue;
671fa9e4066Sahrens 
672fa9e4066Sahrens 			/*
673fa9e4066Sahrens 			 * Ignore directories (which includes "." and "..").
674fa9e4066Sahrens 			 */
675fa9e4066Sahrens 			if (S_ISDIR(statbuf.st_mode))
676fa9e4066Sahrens 				continue;
677fa9e4066Sahrens 
678fa9e4066Sahrens 			if ((fd = open64(path, O_RDONLY)) < 0)
679fa9e4066Sahrens 				continue;
680fa9e4066Sahrens 
681fa9e4066Sahrens 			config = zpool_read_label(fd);
682fa9e4066Sahrens 
683fa9e4066Sahrens 			(void) close(fd);
684fa9e4066Sahrens 
685fa9e4066Sahrens 			if (config != NULL)
686fa9e4066Sahrens 				add_config(&pools, path, config);
687fa9e4066Sahrens 		}
688fa9e4066Sahrens 	}
689fa9e4066Sahrens 
690fa9e4066Sahrens 	ret = get_configs(&pools);
691fa9e4066Sahrens 
692fa9e4066Sahrens 	return (ret);
693fa9e4066Sahrens }
694fa9e4066Sahrens 
695fa9e4066Sahrens int
696fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid)
697fa9e4066Sahrens {
698fa9e4066Sahrens 	uint64_t tmp;
699fa9e4066Sahrens 	nvlist_t **child;
700fa9e4066Sahrens 	uint_t c, children;
701fa9e4066Sahrens 
702fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
703fa9e4066Sahrens 	if (tmp == guid)
704fa9e4066Sahrens 		return (TRUE);
705fa9e4066Sahrens 
706fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
707fa9e4066Sahrens 	    &child, &children) == 0) {
708fa9e4066Sahrens 		for (c = 0; c < children; c++)
709fa9e4066Sahrens 			if (find_guid(child[c], guid))
710fa9e4066Sahrens 				return (TRUE);
711fa9e4066Sahrens 	}
712fa9e4066Sahrens 
713fa9e4066Sahrens 	return (FALSE);
714fa9e4066Sahrens }
715fa9e4066Sahrens 
716fa9e4066Sahrens /*
717fa9e4066Sahrens  * Determines if the pool is in use.  If so, it returns TRUE and the state of
718fa9e4066Sahrens  * the pool as well as the name of the pool.  Both strings are allocated and
719fa9e4066Sahrens  * must be freed by the caller.
720fa9e4066Sahrens  */
721fa9e4066Sahrens int
72246a2abf2Seschrock zpool_in_use(int fd, pool_state_t *state, char **namestr)
723fa9e4066Sahrens {
724fa9e4066Sahrens 	nvlist_t *config;
725fa9e4066Sahrens 	char *name;
726fa9e4066Sahrens 	int ret;
727fa9e4066Sahrens 	uint64_t guid, vdev_guid;
728fa9e4066Sahrens 	zpool_handle_t *zhp;
729fa9e4066Sahrens 	nvlist_t *pool_config;
73046a2abf2Seschrock 	uint64_t stateval;
731fa9e4066Sahrens 
732fa9e4066Sahrens 	if ((config = zpool_read_label(fd)) == NULL)
733fa9e4066Sahrens 		return (FALSE);
734fa9e4066Sahrens 
735fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
736fa9e4066Sahrens 	    &name) == 0);
737fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
73846a2abf2Seschrock 	    &stateval) == 0);
739fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
740fa9e4066Sahrens 	    &guid) == 0);
741fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
742fa9e4066Sahrens 	    &vdev_guid) == 0);
743fa9e4066Sahrens 
74446a2abf2Seschrock 	switch (stateval) {
745fa9e4066Sahrens 	case POOL_STATE_EXPORTED:
746fa9e4066Sahrens 		ret = TRUE;
747fa9e4066Sahrens 		break;
748fa9e4066Sahrens 
749fa9e4066Sahrens 	case POOL_STATE_ACTIVE:
750fa9e4066Sahrens 		/*
751fa9e4066Sahrens 		 * For an active pool, we have to determine if it's really part
752*eaca9bbdSeschrock 		 * of a currently active pool (in which case the pool will exist
753*eaca9bbdSeschrock 		 * and the guid will be the same), or whether it's part of an
754*eaca9bbdSeschrock 		 * active pool that was disconnected without being explicitly
755*eaca9bbdSeschrock 		 * exported.
756fa9e4066Sahrens 		 */
757*eaca9bbdSeschrock 		if (pool_active(name, guid)) {
758fa9e4066Sahrens 			/*
759fa9e4066Sahrens 			 * Because the device may have been removed while
760fa9e4066Sahrens 			 * offlined, we only report it as active if the vdev is
761fa9e4066Sahrens 			 * still present in the config.  Otherwise, pretend like
762fa9e4066Sahrens 			 * it's not in use.
763fa9e4066Sahrens 			 */
764fa9e4066Sahrens 			if ((zhp = zpool_open_canfail(name)) != NULL &&
765088e9d47Seschrock 			    (pool_config = zpool_get_config(zhp, NULL))
766088e9d47Seschrock 			    != NULL) {
767fa9e4066Sahrens 				nvlist_t *nvroot;
768fa9e4066Sahrens 
769fa9e4066Sahrens 				verify(nvlist_lookup_nvlist(pool_config,
770fa9e4066Sahrens 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
77146a2abf2Seschrock 				ret = find_guid(nvroot, vdev_guid);
772fa9e4066Sahrens 			} else {
773fa9e4066Sahrens 				ret = FALSE;
774fa9e4066Sahrens 			}
775fa9e4066Sahrens 		} else {
77646a2abf2Seschrock 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
777fa9e4066Sahrens 			ret = TRUE;
778fa9e4066Sahrens 		}
779fa9e4066Sahrens 		break;
780fa9e4066Sahrens 
781fa9e4066Sahrens 	default:
782fa9e4066Sahrens 		ret = FALSE;
783fa9e4066Sahrens 	}
784fa9e4066Sahrens 
78546a2abf2Seschrock 
78646a2abf2Seschrock 	if (ret) {
78746a2abf2Seschrock 		*namestr = zfs_strdup(name);
78846a2abf2Seschrock 		*state = (pool_state_t)stateval;
78946a2abf2Seschrock 	}
79046a2abf2Seschrock 
791fa9e4066Sahrens 	nvlist_free(config);
792fa9e4066Sahrens 	return (ret);
793fa9e4066Sahrens }
794