xref: /freebsd-src/sys/contrib/openzfs/lib/libzfs/libzfs_import.c (revision 78ae60b447ebf420dd5cebfec30480866fd5cef4)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright 2015 RackTop Systems.
26eda14cbcSMatt Macy  * Copyright (c) 2016, Intel Corporation.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <errno.h>
30eda14cbcSMatt Macy #include <libintl.h>
31eda14cbcSMatt Macy #include <libgen.h>
32eda14cbcSMatt Macy #include <stddef.h>
33eda14cbcSMatt Macy #include <stdlib.h>
34eda14cbcSMatt Macy #include <string.h>
35eda14cbcSMatt Macy #include <sys/stat.h>
36eda14cbcSMatt Macy #include <unistd.h>
37eda14cbcSMatt Macy #include <sys/vdev_impl.h>
38eda14cbcSMatt Macy #include <libzfs.h>
3916038816SMartin Matuska #include "libzfs_impl.h"
40eda14cbcSMatt Macy #include <libzutil.h>
41eda14cbcSMatt Macy #include <sys/arc_impl.h>
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy /*
44eda14cbcSMatt Macy  * Returns true if the named pool matches the given GUID.
45eda14cbcSMatt Macy  */
46eda14cbcSMatt Macy static int
pool_active(libzfs_handle_t * hdl,const char * name,uint64_t guid,boolean_t * isactive)47eda14cbcSMatt Macy pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
48eda14cbcSMatt Macy     boolean_t *isactive)
49eda14cbcSMatt Macy {
50eda14cbcSMatt Macy 	zpool_handle_t *zhp;
51eda14cbcSMatt Macy 
52eda14cbcSMatt Macy 	if (zpool_open_silent(hdl, name, &zhp) != 0)
53eda14cbcSMatt Macy 		return (-1);
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy 	if (zhp == NULL) {
56eda14cbcSMatt Macy 		*isactive = B_FALSE;
57eda14cbcSMatt Macy 		return (0);
58eda14cbcSMatt Macy 	}
59eda14cbcSMatt Macy 
60da5137abSMartin Matuska 	uint64_t theguid = fnvlist_lookup_uint64(zhp->zpool_config,
61da5137abSMartin Matuska 	    ZPOOL_CONFIG_POOL_GUID);
62eda14cbcSMatt Macy 
63eda14cbcSMatt Macy 	zpool_close(zhp);
64eda14cbcSMatt Macy 
65eda14cbcSMatt Macy 	*isactive = (theguid == guid);
66eda14cbcSMatt Macy 	return (0);
67eda14cbcSMatt Macy }
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy static nvlist_t *
refresh_config(libzfs_handle_t * hdl,nvlist_t * config)70eda14cbcSMatt Macy refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
71eda14cbcSMatt Macy {
72eda14cbcSMatt Macy 	nvlist_t *nvl;
73eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
74eda14cbcSMatt Macy 	int err, dstbuf_size;
75eda14cbcSMatt Macy 
76716fd348SMartin Matuska 	zcmd_write_conf_nvlist(hdl, &zc, config);
77eda14cbcSMatt Macy 
78184c1b94SMartin Matuska 	dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 32);
79eda14cbcSMatt Macy 
80716fd348SMartin Matuska 	zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size);
81eda14cbcSMatt Macy 
82eda14cbcSMatt Macy 	while ((err = zfs_ioctl(hdl, ZFS_IOC_POOL_TRYIMPORT,
83716fd348SMartin Matuska 	    &zc)) != 0 && errno == ENOMEM)
84716fd348SMartin Matuska 		zcmd_expand_dst_nvlist(hdl, &zc);
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 	if (err) {
87eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
88eda14cbcSMatt Macy 		return (NULL);
89eda14cbcSMatt Macy 	}
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy 	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
92eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
93eda14cbcSMatt Macy 		return (NULL);
94eda14cbcSMatt Macy 	}
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
97eda14cbcSMatt Macy 	return (nvl);
98eda14cbcSMatt Macy }
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy static nvlist_t *
refresh_config_libzfs(void * handle,nvlist_t * tryconfig)101eda14cbcSMatt Macy refresh_config_libzfs(void *handle, nvlist_t *tryconfig)
102eda14cbcSMatt Macy {
103eda14cbcSMatt Macy 	return (refresh_config((libzfs_handle_t *)handle, tryconfig));
104eda14cbcSMatt Macy }
105eda14cbcSMatt Macy 
106eda14cbcSMatt Macy static int
pool_active_libzfs(void * handle,const char * name,uint64_t guid,boolean_t * isactive)107eda14cbcSMatt Macy pool_active_libzfs(void *handle, const char *name, uint64_t guid,
108eda14cbcSMatt Macy     boolean_t *isactive)
109eda14cbcSMatt Macy {
110eda14cbcSMatt Macy 	return (pool_active((libzfs_handle_t *)handle, name, guid, isactive));
111eda14cbcSMatt Macy }
112eda14cbcSMatt Macy 
113eda14cbcSMatt Macy const pool_config_ops_t libzfs_config_ops = {
114eda14cbcSMatt Macy 	.pco_refresh_config = refresh_config_libzfs,
115eda14cbcSMatt Macy 	.pco_pool_active = pool_active_libzfs,
116eda14cbcSMatt Macy };
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy /*
119eda14cbcSMatt Macy  * Return the offset of the given label.
120eda14cbcSMatt Macy  */
121eda14cbcSMatt Macy static uint64_t
label_offset(uint64_t size,int l)122eda14cbcSMatt Macy label_offset(uint64_t size, int l)
123eda14cbcSMatt Macy {
124eda14cbcSMatt Macy 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
125eda14cbcSMatt Macy 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
126eda14cbcSMatt Macy 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
127eda14cbcSMatt Macy }
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy /*
130eda14cbcSMatt Macy  * Given a file descriptor, clear (zero) the label information.  This function
131eda14cbcSMatt Macy  * is used in the appliance stack as part of the ZFS sysevent module and
132eda14cbcSMatt Macy  * to implement the "zpool labelclear" command.
133eda14cbcSMatt Macy  */
134eda14cbcSMatt Macy int
zpool_clear_label(int fd)135eda14cbcSMatt Macy zpool_clear_label(int fd)
136eda14cbcSMatt Macy {
137eda14cbcSMatt Macy 	struct stat64 statbuf;
138eda14cbcSMatt Macy 	int l;
139eda14cbcSMatt Macy 	vdev_label_t *label;
140eda14cbcSMatt Macy 	uint64_t size;
141716fd348SMartin Matuska 	boolean_t labels_cleared = B_FALSE, clear_l2arc_header = B_FALSE,
142716fd348SMartin Matuska 	    header_cleared = B_FALSE;
143eda14cbcSMatt Macy 
144eda14cbcSMatt Macy 	if (fstat64_blk(fd, &statbuf) == -1)
145eda14cbcSMatt Macy 		return (0);
146eda14cbcSMatt Macy 
147eda14cbcSMatt Macy 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 	if ((label = calloc(1, sizeof (vdev_label_t))) == NULL)
150eda14cbcSMatt Macy 		return (-1);
151eda14cbcSMatt Macy 
152eda14cbcSMatt Macy 	for (l = 0; l < VDEV_LABELS; l++) {
153eda14cbcSMatt Macy 		uint64_t state, guid, l2cache;
154eda14cbcSMatt Macy 		nvlist_t *config;
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 		if (pread64(fd, label, sizeof (vdev_label_t),
157eda14cbcSMatt Macy 		    label_offset(size, l)) != sizeof (vdev_label_t)) {
158eda14cbcSMatt Macy 			continue;
159eda14cbcSMatt Macy 		}
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
162eda14cbcSMatt Macy 		    sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0) {
163eda14cbcSMatt Macy 			continue;
164eda14cbcSMatt Macy 		}
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy 		/* Skip labels which do not have a valid guid. */
167eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
168eda14cbcSMatt Macy 		    &guid) != 0 || guid == 0) {
169eda14cbcSMatt Macy 			nvlist_free(config);
170eda14cbcSMatt Macy 			continue;
171eda14cbcSMatt Macy 		}
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy 		/* Skip labels which are not in a known valid state. */
174eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
175eda14cbcSMatt Macy 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
176eda14cbcSMatt Macy 			nvlist_free(config);
177eda14cbcSMatt Macy 			continue;
178eda14cbcSMatt Macy 		}
179eda14cbcSMatt Macy 
180eda14cbcSMatt Macy 		/* If the device is a cache device clear the header. */
181eda14cbcSMatt Macy 		if (!clear_l2arc_header) {
182eda14cbcSMatt Macy 			if (nvlist_lookup_uint64(config,
183eda14cbcSMatt Macy 			    ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 &&
184eda14cbcSMatt Macy 			    l2cache == POOL_STATE_L2CACHE) {
185eda14cbcSMatt Macy 				clear_l2arc_header = B_TRUE;
186eda14cbcSMatt Macy 			}
187eda14cbcSMatt Macy 		}
188eda14cbcSMatt Macy 
189eda14cbcSMatt Macy 		nvlist_free(config);
190eda14cbcSMatt Macy 
191eda14cbcSMatt Macy 		/*
192eda14cbcSMatt Macy 		 * A valid label was found, overwrite this label's nvlist
193eda14cbcSMatt Macy 		 * and uberblocks with zeros on disk.  This is done to prevent
194eda14cbcSMatt Macy 		 * system utilities, like blkid, from incorrectly detecting a
195eda14cbcSMatt Macy 		 * partial label.  The leading pad space is left untouched.
196eda14cbcSMatt Macy 		 */
197eda14cbcSMatt Macy 		memset(label, 0, sizeof (vdev_label_t));
198eda14cbcSMatt Macy 		size_t label_size = sizeof (vdev_label_t) - (2 * VDEV_PAD_SIZE);
199eda14cbcSMatt Macy 
200eda14cbcSMatt Macy 		if (pwrite64(fd, label, label_size, label_offset(size, l) +
201716fd348SMartin Matuska 		    (2 * VDEV_PAD_SIZE)) == label_size)
202716fd348SMartin Matuska 			labels_cleared = B_TRUE;
203eda14cbcSMatt Macy 	}
204eda14cbcSMatt Macy 
205eda14cbcSMatt Macy 	if (clear_l2arc_header) {
206716fd348SMartin Matuska 		_Static_assert(sizeof (*label) >= sizeof (l2arc_dev_hdr_phys_t),
207716fd348SMartin Matuska 		    "label < l2arc_dev_hdr_phys_t");
208716fd348SMartin Matuska 		memset(label, 0, sizeof (l2arc_dev_hdr_phys_t));
209716fd348SMartin Matuska 		if (pwrite64(fd, label, sizeof (l2arc_dev_hdr_phys_t),
210716fd348SMartin Matuska 		    VDEV_LABEL_START_SIZE) == sizeof (l2arc_dev_hdr_phys_t))
211716fd348SMartin Matuska 			header_cleared = B_TRUE;
212eda14cbcSMatt Macy 	}
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy 	free(label);
215eda14cbcSMatt Macy 
216716fd348SMartin Matuska 	if (!labels_cleared || (clear_l2arc_header && !header_cleared))
217eda14cbcSMatt Macy 		return (-1);
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 	return (0);
220eda14cbcSMatt Macy }
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy static boolean_t
find_guid(nvlist_t * nv,uint64_t guid)223eda14cbcSMatt Macy find_guid(nvlist_t *nv, uint64_t guid)
224eda14cbcSMatt Macy {
225eda14cbcSMatt Macy 	nvlist_t **child;
226eda14cbcSMatt Macy 	uint_t c, children;
227eda14cbcSMatt Macy 
228da5137abSMartin Matuska 	if (fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID) == guid)
229eda14cbcSMatt Macy 		return (B_TRUE);
230eda14cbcSMatt Macy 
231eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
232eda14cbcSMatt Macy 	    &child, &children) == 0) {
233eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
234eda14cbcSMatt Macy 			if (find_guid(child[c], guid))
235eda14cbcSMatt Macy 				return (B_TRUE);
236eda14cbcSMatt Macy 	}
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy 	return (B_FALSE);
239eda14cbcSMatt Macy }
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy typedef struct aux_cbdata {
242eda14cbcSMatt Macy 	const char	*cb_type;
243eda14cbcSMatt Macy 	uint64_t	cb_guid;
244eda14cbcSMatt Macy 	zpool_handle_t	*cb_zhp;
245eda14cbcSMatt Macy } aux_cbdata_t;
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy static int
find_aux(zpool_handle_t * zhp,void * data)248eda14cbcSMatt Macy find_aux(zpool_handle_t *zhp, void *data)
249eda14cbcSMatt Macy {
250eda14cbcSMatt Macy 	aux_cbdata_t *cbp = data;
251eda14cbcSMatt Macy 	nvlist_t **list;
252da5137abSMartin Matuska 	uint_t count;
253eda14cbcSMatt Macy 
254da5137abSMartin Matuska 	nvlist_t *nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
255da5137abSMartin Matuska 	    ZPOOL_CONFIG_VDEV_TREE);
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
258eda14cbcSMatt Macy 	    &list, &count) == 0) {
259da5137abSMartin Matuska 		for (uint_t i = 0; i < count; i++) {
260da5137abSMartin Matuska 			uint64_t guid = fnvlist_lookup_uint64(list[i],
261da5137abSMartin Matuska 			    ZPOOL_CONFIG_GUID);
262eda14cbcSMatt Macy 			if (guid == cbp->cb_guid) {
263eda14cbcSMatt Macy 				cbp->cb_zhp = zhp;
264eda14cbcSMatt Macy 				return (1);
265eda14cbcSMatt Macy 			}
266eda14cbcSMatt Macy 		}
267eda14cbcSMatt Macy 	}
268eda14cbcSMatt Macy 
269eda14cbcSMatt Macy 	zpool_close(zhp);
270eda14cbcSMatt Macy 	return (0);
271eda14cbcSMatt Macy }
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy /*
274eda14cbcSMatt Macy  * Determines if the pool is in use.  If so, it returns true and the state of
275eda14cbcSMatt Macy  * the pool as well as the name of the pool.  Name string is allocated and
276eda14cbcSMatt Macy  * must be freed by the caller.
277eda14cbcSMatt Macy  */
278eda14cbcSMatt Macy int
zpool_in_use(libzfs_handle_t * hdl,int fd,pool_state_t * state,char ** namestr,boolean_t * inuse)279eda14cbcSMatt Macy zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
280eda14cbcSMatt Macy     boolean_t *inuse)
281eda14cbcSMatt Macy {
282eda14cbcSMatt Macy 	nvlist_t *config;
2832a58b312SMartin Matuska 	const char *name = NULL;
284eda14cbcSMatt Macy 	boolean_t ret;
285da5137abSMartin Matuska 	uint64_t guid = 0, vdev_guid;
286eda14cbcSMatt Macy 	zpool_handle_t *zhp;
287eda14cbcSMatt Macy 	nvlist_t *pool_config;
288eda14cbcSMatt Macy 	uint64_t stateval, isspare;
289eda14cbcSMatt Macy 	aux_cbdata_t cb = { 0 };
290eda14cbcSMatt Macy 	boolean_t isactive;
291eda14cbcSMatt Macy 
292eda14cbcSMatt Macy 	*inuse = B_FALSE;
293eda14cbcSMatt Macy 
294*78ae60b4SMartin Matuska 	if (zpool_read_label(fd, &config, NULL) != 0)
295eda14cbcSMatt Macy 		return (-1);
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy 	if (config == NULL)
298eda14cbcSMatt Macy 		return (0);
299eda14cbcSMatt Macy 
300da5137abSMartin Matuska 	stateval = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
301da5137abSMartin Matuska 	vdev_guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID);
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy 	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
304da5137abSMartin Matuska 		name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
305da5137abSMartin Matuska 		guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
306eda14cbcSMatt Macy 	}
307eda14cbcSMatt Macy 
308eda14cbcSMatt Macy 	switch (stateval) {
309eda14cbcSMatt Macy 	case POOL_STATE_EXPORTED:
310eda14cbcSMatt Macy 		/*
311eda14cbcSMatt Macy 		 * A pool with an exported state may in fact be imported
312eda14cbcSMatt Macy 		 * read-only, so check the in-core state to see if it's
313eda14cbcSMatt Macy 		 * active and imported read-only.  If it is, set
314eda14cbcSMatt Macy 		 * its state to active.
315eda14cbcSMatt Macy 		 */
316eda14cbcSMatt Macy 		if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
317eda14cbcSMatt Macy 		    (zhp = zpool_open_canfail(hdl, name)) != NULL) {
318eda14cbcSMatt Macy 			if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
319eda14cbcSMatt Macy 				stateval = POOL_STATE_ACTIVE;
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 			/*
322eda14cbcSMatt Macy 			 * All we needed the zpool handle for is the
323eda14cbcSMatt Macy 			 * readonly prop check.
324eda14cbcSMatt Macy 			 */
325eda14cbcSMatt Macy 			zpool_close(zhp);
326eda14cbcSMatt Macy 		}
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 		ret = B_TRUE;
329eda14cbcSMatt Macy 		break;
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	case POOL_STATE_ACTIVE:
332eda14cbcSMatt Macy 		/*
333eda14cbcSMatt Macy 		 * For an active pool, we have to determine if it's really part
334eda14cbcSMatt Macy 		 * of a currently active pool (in which case the pool will exist
335eda14cbcSMatt Macy 		 * and the guid will be the same), or whether it's part of an
336eda14cbcSMatt Macy 		 * active pool that was disconnected without being explicitly
337eda14cbcSMatt Macy 		 * exported.
338eda14cbcSMatt Macy 		 */
339eda14cbcSMatt Macy 		if (pool_active(hdl, name, guid, &isactive) != 0) {
340eda14cbcSMatt Macy 			nvlist_free(config);
341eda14cbcSMatt Macy 			return (-1);
342eda14cbcSMatt Macy 		}
343eda14cbcSMatt Macy 
344eda14cbcSMatt Macy 		if (isactive) {
345eda14cbcSMatt Macy 			/*
346eda14cbcSMatt Macy 			 * Because the device may have been removed while
347eda14cbcSMatt Macy 			 * offlined, we only report it as active if the vdev is
348eda14cbcSMatt Macy 			 * still present in the config.  Otherwise, pretend like
349eda14cbcSMatt Macy 			 * it's not in use.
350eda14cbcSMatt Macy 			 */
351eda14cbcSMatt Macy 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
352eda14cbcSMatt Macy 			    (pool_config = zpool_get_config(zhp, NULL))
353eda14cbcSMatt Macy 			    != NULL) {
354da5137abSMartin Matuska 				nvlist_t *nvroot = fnvlist_lookup_nvlist(
355da5137abSMartin Matuska 				    pool_config, ZPOOL_CONFIG_VDEV_TREE);
356eda14cbcSMatt Macy 				ret = find_guid(nvroot, vdev_guid);
357eda14cbcSMatt Macy 			} else {
358eda14cbcSMatt Macy 				ret = B_FALSE;
359eda14cbcSMatt Macy 			}
360eda14cbcSMatt Macy 
361eda14cbcSMatt Macy 			/*
362eda14cbcSMatt Macy 			 * If this is an active spare within another pool, we
363eda14cbcSMatt Macy 			 * treat it like an unused hot spare.  This allows the
364eda14cbcSMatt Macy 			 * user to create a pool with a hot spare that currently
365eda14cbcSMatt Macy 			 * in use within another pool.  Since we return B_TRUE,
366eda14cbcSMatt Macy 			 * libdiskmgt will continue to prevent generic consumers
367eda14cbcSMatt Macy 			 * from using the device.
368eda14cbcSMatt Macy 			 */
369eda14cbcSMatt Macy 			if (ret && nvlist_lookup_uint64(config,
370eda14cbcSMatt Macy 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
371eda14cbcSMatt Macy 				stateval = POOL_STATE_SPARE;
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 			if (zhp != NULL)
374eda14cbcSMatt Macy 				zpool_close(zhp);
375eda14cbcSMatt Macy 		} else {
376eda14cbcSMatt Macy 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
377eda14cbcSMatt Macy 			ret = B_TRUE;
378eda14cbcSMatt Macy 		}
379eda14cbcSMatt Macy 		break;
380eda14cbcSMatt Macy 
381eda14cbcSMatt Macy 	case POOL_STATE_SPARE:
382eda14cbcSMatt Macy 		/*
383eda14cbcSMatt Macy 		 * For a hot spare, it can be either definitively in use, or
384eda14cbcSMatt Macy 		 * potentially active.  To determine if it's in use, we iterate
385eda14cbcSMatt Macy 		 * over all pools in the system and search for one with a spare
386eda14cbcSMatt Macy 		 * with a matching guid.
387eda14cbcSMatt Macy 		 *
388eda14cbcSMatt Macy 		 * Due to the shared nature of spares, we don't actually report
389eda14cbcSMatt Macy 		 * the potentially active case as in use.  This means the user
390eda14cbcSMatt Macy 		 * can freely create pools on the hot spares of exported pools,
391eda14cbcSMatt Macy 		 * but to do otherwise makes the resulting code complicated, and
392eda14cbcSMatt Macy 		 * we end up having to deal with this case anyway.
393eda14cbcSMatt Macy 		 */
394eda14cbcSMatt Macy 		cb.cb_zhp = NULL;
395eda14cbcSMatt Macy 		cb.cb_guid = vdev_guid;
396eda14cbcSMatt Macy 		cb.cb_type = ZPOOL_CONFIG_SPARES;
397eda14cbcSMatt Macy 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
398eda14cbcSMatt Macy 			name = (char *)zpool_get_name(cb.cb_zhp);
399eda14cbcSMatt Macy 			ret = B_TRUE;
400eda14cbcSMatt Macy 		} else {
401eda14cbcSMatt Macy 			ret = B_FALSE;
402eda14cbcSMatt Macy 		}
403eda14cbcSMatt Macy 		break;
404eda14cbcSMatt Macy 
405eda14cbcSMatt Macy 	case POOL_STATE_L2CACHE:
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy 		/*
408eda14cbcSMatt Macy 		 * Check if any pool is currently using this l2cache device.
409eda14cbcSMatt Macy 		 */
410eda14cbcSMatt Macy 		cb.cb_zhp = NULL;
411eda14cbcSMatt Macy 		cb.cb_guid = vdev_guid;
412eda14cbcSMatt Macy 		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
413eda14cbcSMatt Macy 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
414eda14cbcSMatt Macy 			name = (char *)zpool_get_name(cb.cb_zhp);
415eda14cbcSMatt Macy 			ret = B_TRUE;
416eda14cbcSMatt Macy 		} else {
417eda14cbcSMatt Macy 			ret = B_FALSE;
418eda14cbcSMatt Macy 		}
419eda14cbcSMatt Macy 		break;
420eda14cbcSMatt Macy 
421eda14cbcSMatt Macy 	default:
422eda14cbcSMatt Macy 		ret = B_FALSE;
423eda14cbcSMatt Macy 	}
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy 	if (ret) {
427716fd348SMartin Matuska 		*namestr = zfs_strdup(hdl, name);
428eda14cbcSMatt Macy 		*state = (pool_state_t)stateval;
429eda14cbcSMatt Macy 	}
430eda14cbcSMatt Macy 
431eda14cbcSMatt Macy 	if (cb.cb_zhp)
432eda14cbcSMatt Macy 		zpool_close(cb.cb_zhp);
433eda14cbcSMatt Macy 
434eda14cbcSMatt Macy 	nvlist_free(config);
435eda14cbcSMatt Macy 	*inuse = ret;
436eda14cbcSMatt Macy 	return (0);
437eda14cbcSMatt Macy }
438