1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5*ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6*ea8dc4b6Seschrock  * 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 
295fa9e4066Sahrens /*
296fa9e4066Sahrens  * Convert our list of pools into the definitive set of configurations.  We
297fa9e4066Sahrens  * start by picking the best config for each toplevel vdev.  Once that's done,
298fa9e4066Sahrens  * we assemble the toplevel vdevs into a full config for the pool.  We make a
299fa9e4066Sahrens  * pass to fix up any incorrect paths, and then add it to the main list to
300fa9e4066Sahrens  * return to the user.
301fa9e4066Sahrens  */
302fa9e4066Sahrens static nvlist_t *
303fa9e4066Sahrens get_configs(pool_list_t *pl)
304fa9e4066Sahrens {
305fa9e4066Sahrens 	pool_entry_t *pe, *penext;
306fa9e4066Sahrens 	vdev_entry_t *ve, *venext;
307fa9e4066Sahrens 	config_entry_t *ce, *cenext;
308fa9e4066Sahrens 	nvlist_t *ret, *config, *tmp, *nvtop, *nvroot;
309fa9e4066Sahrens 	int config_seen;
310fa9e4066Sahrens 	uint64_t best_txg;
311fa9e4066Sahrens 	char *name;
312fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
313fa9e4066Sahrens 	uint64_t guid;
314fa9e4066Sahrens 	char *packed;
315fa9e4066Sahrens 	size_t len;
316fa9e4066Sahrens 	int err;
317fa9e4066Sahrens 
318fa9e4066Sahrens 	verify(nvlist_alloc(&ret, 0, 0) == 0);
319fa9e4066Sahrens 
320fa9e4066Sahrens 	for (pe = pl->pools; pe != NULL; pe = penext) {
321fa9e4066Sahrens 		uint_t c;
322fa9e4066Sahrens 		uint_t children = 0;
323fa9e4066Sahrens 		uint64_t id;
324fa9e4066Sahrens 		nvlist_t **child = NULL;
325fa9e4066Sahrens 
326fa9e4066Sahrens 		penext = pe->pe_next;
327fa9e4066Sahrens 
328fa9e4066Sahrens 		verify(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
329fa9e4066Sahrens 		config_seen = FALSE;
330fa9e4066Sahrens 
331fa9e4066Sahrens 		/*
332fa9e4066Sahrens 		 * Iterate over all toplevel vdevs.  Grab the pool configuration
333fa9e4066Sahrens 		 * from the first one we find, and then go through the rest and
334fa9e4066Sahrens 		 * add them as necessary to the 'vdevs' member of the config.
335fa9e4066Sahrens 		 */
336fa9e4066Sahrens 		for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
337fa9e4066Sahrens 			venext = ve->ve_next;
338fa9e4066Sahrens 
339fa9e4066Sahrens 			/*
340fa9e4066Sahrens 			 * Determine the best configuration for this vdev by
341fa9e4066Sahrens 			 * selecting the config with the latest transaction
342fa9e4066Sahrens 			 * group.
343fa9e4066Sahrens 			 */
344fa9e4066Sahrens 			best_txg = 0;
345fa9e4066Sahrens 			for (ce = ve->ve_configs; ce != NULL;
346fa9e4066Sahrens 			    ce = ce->ce_next) {
347fa9e4066Sahrens 
348fa9e4066Sahrens 				if (ce->ce_txg > best_txg)
349fa9e4066Sahrens 					tmp = ce->ce_config;
350fa9e4066Sahrens 			}
351fa9e4066Sahrens 
352fa9e4066Sahrens 			if (!config_seen) {
353fa9e4066Sahrens 				/*
354fa9e4066Sahrens 				 * Copy the relevant pieces of data to the pool
355fa9e4066Sahrens 				 * configuration:
356fa9e4066Sahrens 				 *
357fa9e4066Sahrens 				 * 	pool guid
358fa9e4066Sahrens 				 * 	name
359fa9e4066Sahrens 				 * 	pool state
360fa9e4066Sahrens 				 */
361fa9e4066Sahrens 				uint64_t state;
362fa9e4066Sahrens 
363fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
364fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
365fa9e4066Sahrens 				verify(nvlist_add_uint64(config,
366fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_GUID, guid) == 0);
367fa9e4066Sahrens 				verify(nvlist_lookup_string(tmp,
368fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
369fa9e4066Sahrens 				verify(nvlist_add_string(config,
370fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_NAME, name) == 0);
371fa9e4066Sahrens 				verify(nvlist_lookup_uint64(tmp,
372fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
373fa9e4066Sahrens 				verify(nvlist_add_uint64(config,
374fa9e4066Sahrens 				    ZPOOL_CONFIG_POOL_STATE, state) == 0);
375fa9e4066Sahrens 
376fa9e4066Sahrens 				config_seen = TRUE;
377fa9e4066Sahrens 			}
378fa9e4066Sahrens 
379fa9e4066Sahrens 			/*
380fa9e4066Sahrens 			 * Add this top-level vdev to the child array.
381fa9e4066Sahrens 			 */
382fa9e4066Sahrens 			verify(nvlist_lookup_nvlist(tmp,
383fa9e4066Sahrens 			    ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
384fa9e4066Sahrens 			verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
385fa9e4066Sahrens 			    &id) == 0);
386fa9e4066Sahrens 			if (id >= children) {
387fa9e4066Sahrens 				nvlist_t **newchild;
388fa9e4066Sahrens 
389fa9e4066Sahrens 				newchild = zfs_malloc((id + 1) *
390fa9e4066Sahrens 				    sizeof (nvlist_t *));
391fa9e4066Sahrens 
392fa9e4066Sahrens 				for (c = 0; c < children; c++)
393fa9e4066Sahrens 					newchild[c] = child[c];
394fa9e4066Sahrens 
395fa9e4066Sahrens 				free(child);
396fa9e4066Sahrens 				child = newchild;
397fa9e4066Sahrens 				children = id + 1;
398fa9e4066Sahrens 			}
399fa9e4066Sahrens 			verify(nvlist_dup(nvtop, &child[id], 0) == 0);
400fa9e4066Sahrens 
401fa9e4066Sahrens 			/*
402fa9e4066Sahrens 			 * Go through and free all config information.
403fa9e4066Sahrens 			 */
404fa9e4066Sahrens 			for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
405fa9e4066Sahrens 				cenext = ce->ce_next;
406fa9e4066Sahrens 
407fa9e4066Sahrens 				nvlist_free(ce->ce_config);
408fa9e4066Sahrens 				free(ce);
409fa9e4066Sahrens 			}
410fa9e4066Sahrens 
411fa9e4066Sahrens 			/*
412fa9e4066Sahrens 			 * Free this vdev entry, since it has now been merged
413fa9e4066Sahrens 			 * into the main config.
414fa9e4066Sahrens 			 */
415fa9e4066Sahrens 			free(ve);
416fa9e4066Sahrens 		}
417fa9e4066Sahrens 
418fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
419fa9e4066Sahrens 		    &guid) == 0);
420fa9e4066Sahrens 
421fa9e4066Sahrens 		/*
422fa9e4066Sahrens 		 * Look for any missing top-level vdevs.  If this is the case,
423fa9e4066Sahrens 		 * create a faked up 'missing' vdev as a placeholder.  We cannot
424fa9e4066Sahrens 		 * simply compress the child array, because the kernel performs
425fa9e4066Sahrens 		 * certain checks to make sure the vdev IDs match their location
426fa9e4066Sahrens 		 * in the configuration.
427fa9e4066Sahrens 		 */
428fa9e4066Sahrens 		for (c = 0; c < children; c++)
429fa9e4066Sahrens 			if (child[c] == NULL) {
430fa9e4066Sahrens 				nvlist_t *missing;
431fa9e4066Sahrens 				verify(nvlist_alloc(&missing, NV_UNIQUE_NAME,
432fa9e4066Sahrens 				    0) == 0);
433fa9e4066Sahrens 				verify(nvlist_add_string(missing,
434fa9e4066Sahrens 				    ZPOOL_CONFIG_TYPE, VDEV_TYPE_MISSING) == 0);
435fa9e4066Sahrens 				verify(nvlist_add_uint64(missing,
436fa9e4066Sahrens 				    ZPOOL_CONFIG_ID, c) == 0);
437fa9e4066Sahrens 				verify(nvlist_add_uint64(missing,
438fa9e4066Sahrens 				    ZPOOL_CONFIG_GUID, 0ULL) == 0);
439fa9e4066Sahrens 				child[c] = missing;
440fa9e4066Sahrens 			}
441fa9e4066Sahrens 
442fa9e4066Sahrens 		/*
443fa9e4066Sahrens 		 * Put all of this pool's top-level vdevs into a root vdev.
444fa9e4066Sahrens 		 */
445fa9e4066Sahrens 		verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
446fa9e4066Sahrens 		verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
447fa9e4066Sahrens 		    VDEV_TYPE_ROOT) == 0);
448fa9e4066Sahrens 		verify(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
449fa9e4066Sahrens 		verify(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) == 0);
450fa9e4066Sahrens 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
451fa9e4066Sahrens 		    child, children) == 0);
452fa9e4066Sahrens 
453fa9e4066Sahrens 		for (c = 0; c < children; c++)
454fa9e4066Sahrens 			nvlist_free(child[c]);
455fa9e4066Sahrens 		free(child);
456fa9e4066Sahrens 
457fa9e4066Sahrens 		/*
458fa9e4066Sahrens 		 * Go through and fix up any paths and/or devids based on our
459fa9e4066Sahrens 		 * known list of vdev GUID -> path mappings.
460fa9e4066Sahrens 		 */
461fa9e4066Sahrens 		fix_paths(nvroot, pl->names);
462fa9e4066Sahrens 
463fa9e4066Sahrens 		/*
464fa9e4066Sahrens 		 * Add the root vdev to this pool's configuration.
465fa9e4066Sahrens 		 */
466fa9e4066Sahrens 		verify(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
467fa9e4066Sahrens 		    nvroot) == 0);
468fa9e4066Sahrens 		nvlist_free(nvroot);
469fa9e4066Sahrens 
470fa9e4066Sahrens 		/*
471fa9e4066Sahrens 		 * Free this pool entry.
472fa9e4066Sahrens 		 */
473fa9e4066Sahrens 		free(pe);
474fa9e4066Sahrens 
475fa9e4066Sahrens 		/*
476fa9e4066Sahrens 		 * Determine if this pool is currently active, in which case we
477fa9e4066Sahrens 		 * can't actually import it.
478fa9e4066Sahrens 		 */
479fa9e4066Sahrens 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
480fa9e4066Sahrens 		    &name) == 0);
481fa9e4066Sahrens 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
482fa9e4066Sahrens 		    &guid) == 0);
483fa9e4066Sahrens 
484fa9e4066Sahrens 		(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
485*ea8dc4b6Seschrock 		if (zfs_ioctl(ZFS_IOC_POOL_GUID, &zc) == 0 &&
486*ea8dc4b6Seschrock 		    guid == zc.zc_guid) {
487fa9e4066Sahrens 			nvlist_free(config);
488fa9e4066Sahrens 			continue;
489fa9e4066Sahrens 		}
490fa9e4066Sahrens 
491fa9e4066Sahrens 		/*
492fa9e4066Sahrens 		 * Try to do the import in order to get vdev state.
493fa9e4066Sahrens 		 */
494fa9e4066Sahrens 		if ((err = nvlist_size(config, &len, NV_ENCODE_NATIVE)) != 0)
495fa9e4066Sahrens 			zfs_baderror(err);
496fa9e4066Sahrens 
497fa9e4066Sahrens 		packed = zfs_malloc(len);
498fa9e4066Sahrens 
499fa9e4066Sahrens 		if ((err = nvlist_pack(config, &packed, &len,
500fa9e4066Sahrens 		    NV_ENCODE_NATIVE, 0)) != 0)
501fa9e4066Sahrens 			zfs_baderror(err);
502fa9e4066Sahrens 
503fa9e4066Sahrens 		nvlist_free(config);
504fa9e4066Sahrens 		config = NULL;
505fa9e4066Sahrens 
506fa9e4066Sahrens 		zc.zc_config_src_size = len;
507fa9e4066Sahrens 		zc.zc_config_src = (uint64_t)(uintptr_t)packed;
508fa9e4066Sahrens 
509fa9e4066Sahrens 		zc.zc_config_dst_size = 2 * len;
510fa9e4066Sahrens 		zc.zc_config_dst = (uint64_t)(uintptr_t)
511fa9e4066Sahrens 		    zfs_malloc(zc.zc_config_dst_size);
512fa9e4066Sahrens 
513*ea8dc4b6Seschrock 		while ((err = zfs_ioctl(ZFS_IOC_POOL_TRYIMPORT,
514fa9e4066Sahrens 		    &zc)) != 0 && errno == ENOMEM) {
515fa9e4066Sahrens 			free((void *)(uintptr_t)zc.zc_config_dst);
516fa9e4066Sahrens 			zc.zc_config_dst = (uint64_t)(uintptr_t)
517fa9e4066Sahrens 			    zfs_malloc(zc.zc_config_dst_size);
518fa9e4066Sahrens 		}
519fa9e4066Sahrens 
520fa9e4066Sahrens 		free(packed);
521fa9e4066Sahrens 
522fa9e4066Sahrens 		if (err)
523fa9e4066Sahrens 			zfs_baderror(errno);
524fa9e4066Sahrens 
525fa9e4066Sahrens 		verify(nvlist_unpack((void *)(uintptr_t)zc.zc_config_dst,
526fa9e4066Sahrens 		    zc.zc_config_dst_size, &config, 0) == 0);
527fa9e4066Sahrens 
528fa9e4066Sahrens 		set_pool_health(config);
529fa9e4066Sahrens 
530fa9e4066Sahrens 		/*
531fa9e4066Sahrens 		 * Add this pool to the list of configs.
532fa9e4066Sahrens 		 */
533fa9e4066Sahrens 		verify(nvlist_add_nvlist(ret, name, config) == 0);
534fa9e4066Sahrens 
535fa9e4066Sahrens 		nvlist_free(config);
536fa9e4066Sahrens 
537fa9e4066Sahrens 		free((void *)(uintptr_t)zc.zc_config_dst);
538fa9e4066Sahrens 	}
539fa9e4066Sahrens 
540fa9e4066Sahrens 	return (ret);
541fa9e4066Sahrens }
542fa9e4066Sahrens 
543fa9e4066Sahrens /*
544fa9e4066Sahrens  * Return the offset of the given label.
545fa9e4066Sahrens  */
546fa9e4066Sahrens static uint64_t
547fa9e4066Sahrens label_offset(size_t size, int l)
548fa9e4066Sahrens {
549fa9e4066Sahrens 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
550fa9e4066Sahrens 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
551fa9e4066Sahrens }
552fa9e4066Sahrens 
553fa9e4066Sahrens /*
554fa9e4066Sahrens  * Given a file descriptor, read the label information and return an nvlist
555fa9e4066Sahrens  * describing the configuration, if there is one.
556fa9e4066Sahrens  */
557fa9e4066Sahrens nvlist_t *
558fa9e4066Sahrens zpool_read_label(int fd)
559fa9e4066Sahrens {
560fa9e4066Sahrens 	struct stat64 statbuf;
561fa9e4066Sahrens 	int l;
562fa9e4066Sahrens 	vdev_label_t *label;
563fa9e4066Sahrens 	nvlist_t *config;
564*ea8dc4b6Seschrock 	uint64_t state, txg;
565fa9e4066Sahrens 
566fa9e4066Sahrens 	if (fstat64(fd, &statbuf) == -1)
567fa9e4066Sahrens 		return (NULL);
568fa9e4066Sahrens 
569fa9e4066Sahrens 	label = zfs_malloc(sizeof (vdev_label_t));
570fa9e4066Sahrens 
571fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
572fa9e4066Sahrens 		if (pread(fd, label, sizeof (vdev_label_t),
573fa9e4066Sahrens 		    label_offset(statbuf.st_size, l)) != sizeof (vdev_label_t))
574fa9e4066Sahrens 			continue;
575fa9e4066Sahrens 
576fa9e4066Sahrens 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
577fa9e4066Sahrens 		    sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0)
578fa9e4066Sahrens 			continue;
579fa9e4066Sahrens 
580fa9e4066Sahrens 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
581fa9e4066Sahrens 		    &state) != 0 || state > POOL_STATE_EXPORTED) {
582fa9e4066Sahrens 			nvlist_free(config);
583fa9e4066Sahrens 			continue;
584fa9e4066Sahrens 		}
585fa9e4066Sahrens 
586fa9e4066Sahrens 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
587fa9e4066Sahrens 		    &txg) != 0 || txg == 0) {
588fa9e4066Sahrens 			nvlist_free(config);
589fa9e4066Sahrens 			continue;
590fa9e4066Sahrens 		}
591fa9e4066Sahrens 
592fa9e4066Sahrens 		free(label);
593fa9e4066Sahrens 		return (config);
594fa9e4066Sahrens 	}
595fa9e4066Sahrens 
596fa9e4066Sahrens 	free(label);
597fa9e4066Sahrens 	return (NULL);
598fa9e4066Sahrens }
599fa9e4066Sahrens 
600fa9e4066Sahrens /*
601fa9e4066Sahrens  * Given a list of directories to search, find all pools stored on disk.  This
602fa9e4066Sahrens  * includes partial pools which are not available to import.  If no args are
603fa9e4066Sahrens  * given (argc is 0), then the default directory (/dev/dsk) is searched.
604fa9e4066Sahrens  */
605fa9e4066Sahrens nvlist_t *
606fa9e4066Sahrens zpool_find_import(int argc, char **argv)
607fa9e4066Sahrens {
608fa9e4066Sahrens 	int i;
609fa9e4066Sahrens 	DIR *dirp;
610fa9e4066Sahrens 	struct dirent64 *dp;
611fa9e4066Sahrens 	char path[MAXPATHLEN];
612fa9e4066Sahrens 	struct stat64 statbuf;
613fa9e4066Sahrens 	nvlist_t *ret, *config;
614fa9e4066Sahrens 	static char *default_dir = "/dev/dsk";
615fa9e4066Sahrens 	int fd;
616fa9e4066Sahrens 	pool_list_t pools = { 0 };
617fa9e4066Sahrens 
618fa9e4066Sahrens 	if (argc == 0) {
619fa9e4066Sahrens 		argc = 1;
620fa9e4066Sahrens 		argv = &default_dir;
621fa9e4066Sahrens 	}
622fa9e4066Sahrens 
623fa9e4066Sahrens 	/*
624fa9e4066Sahrens 	 * Go through and read the label configuration information from every
625fa9e4066Sahrens 	 * possible device, organizing the information according to pool GUID
626fa9e4066Sahrens 	 * and toplevel GUID.
627fa9e4066Sahrens 	 */
628fa9e4066Sahrens 	for (i = 0; i < argc; i++) {
629fa9e4066Sahrens 		if (argv[i][0] != '/') {
630fa9e4066Sahrens 			zfs_error(dgettext(TEXT_DOMAIN,
631fa9e4066Sahrens 			    "cannot open '%s': must be an absolute path"),
632fa9e4066Sahrens 			    argv[i]);
633fa9e4066Sahrens 			return (NULL);
634fa9e4066Sahrens 		}
635fa9e4066Sahrens 
636fa9e4066Sahrens 		if ((dirp = opendir(argv[i])) == NULL) {
637fa9e4066Sahrens 			zfs_error(dgettext(TEXT_DOMAIN,
638fa9e4066Sahrens 			    "cannot open '%s': %s"), argv[i],
639fa9e4066Sahrens 			    strerror(errno));
640fa9e4066Sahrens 			return (NULL);
641fa9e4066Sahrens 		}
642fa9e4066Sahrens 
643fa9e4066Sahrens 		/*
644fa9e4066Sahrens 		 * This is not MT-safe, but we have no MT consumers of libzfs
645fa9e4066Sahrens 		 */
646fa9e4066Sahrens 		while ((dp = readdir64(dirp)) != NULL) {
647fa9e4066Sahrens 
648fa9e4066Sahrens 			(void) snprintf(path, sizeof (path), "%s/%s",
649fa9e4066Sahrens 			    argv[i], dp->d_name);
650fa9e4066Sahrens 
651fa9e4066Sahrens 			if (stat64(path, &statbuf) != 0)
652fa9e4066Sahrens 				continue;
653fa9e4066Sahrens 
654fa9e4066Sahrens 			/*
655fa9e4066Sahrens 			 * Ignore directories (which includes "." and "..").
656fa9e4066Sahrens 			 */
657fa9e4066Sahrens 			if (S_ISDIR(statbuf.st_mode))
658fa9e4066Sahrens 				continue;
659fa9e4066Sahrens 
660fa9e4066Sahrens 			if ((fd = open64(path, O_RDONLY)) < 0)
661fa9e4066Sahrens 				continue;
662fa9e4066Sahrens 
663fa9e4066Sahrens 			config = zpool_read_label(fd);
664fa9e4066Sahrens 
665fa9e4066Sahrens 			(void) close(fd);
666fa9e4066Sahrens 
667fa9e4066Sahrens 			if (config != NULL)
668fa9e4066Sahrens 				add_config(&pools, path, config);
669fa9e4066Sahrens 		}
670fa9e4066Sahrens 	}
671fa9e4066Sahrens 
672fa9e4066Sahrens 	ret = get_configs(&pools);
673fa9e4066Sahrens 
674fa9e4066Sahrens 	return (ret);
675fa9e4066Sahrens }
676fa9e4066Sahrens 
677fa9e4066Sahrens int
678fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid)
679fa9e4066Sahrens {
680fa9e4066Sahrens 	uint64_t tmp;
681fa9e4066Sahrens 	nvlist_t **child;
682fa9e4066Sahrens 	uint_t c, children;
683fa9e4066Sahrens 
684fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
685fa9e4066Sahrens 	if (tmp == guid)
686fa9e4066Sahrens 		return (TRUE);
687fa9e4066Sahrens 
688fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
689fa9e4066Sahrens 	    &child, &children) == 0) {
690fa9e4066Sahrens 		for (c = 0; c < children; c++)
691fa9e4066Sahrens 			if (find_guid(child[c], guid))
692fa9e4066Sahrens 				return (TRUE);
693fa9e4066Sahrens 	}
694fa9e4066Sahrens 
695fa9e4066Sahrens 	return (FALSE);
696fa9e4066Sahrens }
697fa9e4066Sahrens 
698fa9e4066Sahrens /*
699fa9e4066Sahrens  * Determines if the pool is in use.  If so, it returns TRUE and the state of
700fa9e4066Sahrens  * the pool as well as the name of the pool.  Both strings are allocated and
701fa9e4066Sahrens  * must be freed by the caller.
702fa9e4066Sahrens  */
703fa9e4066Sahrens int
70446a2abf2Seschrock zpool_in_use(int fd, pool_state_t *state, char **namestr)
705fa9e4066Sahrens {
706fa9e4066Sahrens 	nvlist_t *config;
707fa9e4066Sahrens 	char *name;
708fa9e4066Sahrens 	int ret;
709fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
710fa9e4066Sahrens 	uint64_t guid, vdev_guid;
711fa9e4066Sahrens 	zpool_handle_t *zhp;
712fa9e4066Sahrens 	nvlist_t *pool_config;
71346a2abf2Seschrock 	uint64_t stateval;
714fa9e4066Sahrens 
715fa9e4066Sahrens 	if ((config = zpool_read_label(fd)) == NULL)
716fa9e4066Sahrens 		return (FALSE);
717fa9e4066Sahrens 
718fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
719fa9e4066Sahrens 	    &name) == 0);
720fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
72146a2abf2Seschrock 	    &stateval) == 0);
722fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
723fa9e4066Sahrens 	    &guid) == 0);
724fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
725fa9e4066Sahrens 	    &vdev_guid) == 0);
726fa9e4066Sahrens 
72746a2abf2Seschrock 	switch (stateval) {
728fa9e4066Sahrens 	case POOL_STATE_EXPORTED:
729fa9e4066Sahrens 		ret = TRUE;
730fa9e4066Sahrens 		break;
731fa9e4066Sahrens 
732fa9e4066Sahrens 	case POOL_STATE_ACTIVE:
733fa9e4066Sahrens 		/*
734fa9e4066Sahrens 		 * For an active pool, we have to determine if it's really part
735fa9e4066Sahrens 		 * of an active pool (in which case the pool will exist and the
736fa9e4066Sahrens 		 * guid will be the same), or whether it's part of an active
737fa9e4066Sahrens 		 * pool that was disconnected without being explicitly exported.
738fa9e4066Sahrens 		 *
739fa9e4066Sahrens 		 * We use the direct ioctl() first to avoid triggering an error
740fa9e4066Sahrens 		 * message if the pool cannot be opened.
741fa9e4066Sahrens 		 */
742fa9e4066Sahrens 		(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
743*ea8dc4b6Seschrock 		if (zfs_ioctl(ZFS_IOC_POOL_GUID, &zc) == 0 &&
744*ea8dc4b6Seschrock 		    guid == zc.zc_guid) {
745fa9e4066Sahrens 			/*
746fa9e4066Sahrens 			 * Because the device may have been removed while
747fa9e4066Sahrens 			 * offlined, we only report it as active if the vdev is
748fa9e4066Sahrens 			 * still present in the config.  Otherwise, pretend like
749fa9e4066Sahrens 			 * it's not in use.
750fa9e4066Sahrens 			 */
751fa9e4066Sahrens 			if ((zhp = zpool_open_canfail(name)) != NULL &&
752088e9d47Seschrock 			    (pool_config = zpool_get_config(zhp, NULL))
753088e9d47Seschrock 			    != NULL) {
754fa9e4066Sahrens 				nvlist_t *nvroot;
755fa9e4066Sahrens 
756fa9e4066Sahrens 				verify(nvlist_lookup_nvlist(pool_config,
757fa9e4066Sahrens 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
75846a2abf2Seschrock 				ret = find_guid(nvroot, vdev_guid);
759fa9e4066Sahrens 			} else {
760fa9e4066Sahrens 				ret = FALSE;
761fa9e4066Sahrens 			}
762fa9e4066Sahrens 		} else {
76346a2abf2Seschrock 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
764fa9e4066Sahrens 			ret = TRUE;
765fa9e4066Sahrens 		}
766fa9e4066Sahrens 		break;
767fa9e4066Sahrens 
768fa9e4066Sahrens 	default:
769fa9e4066Sahrens 		ret = FALSE;
770fa9e4066Sahrens 	}
771fa9e4066Sahrens 
77246a2abf2Seschrock 
77346a2abf2Seschrock 	if (ret) {
77446a2abf2Seschrock 		*namestr = zfs_strdup(name);
77546a2abf2Seschrock 		*state = (pool_state_t)stateval;
77646a2abf2Seschrock 	}
77746a2abf2Seschrock 
778fa9e4066Sahrens 	nvlist_free(config);
779fa9e4066Sahrens 	return (ret);
780fa9e4066Sahrens }
781