xref: /freebsd-src/sys/contrib/openzfs/lib/libzfs/libzfs_import.c (revision da5137abdf463bb5fee85061958a14dd12bc043e)
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
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
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
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 
60*da5137abSMartin Matuska 	uint64_t theguid = fnvlist_lookup_uint64(zhp->zpool_config,
61*da5137abSMartin 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 *
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 
76eda14cbcSMatt Macy 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
77eda14cbcSMatt Macy 		return (NULL);
78eda14cbcSMatt Macy 
79184c1b94SMartin Matuska 	dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 32);
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 	if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
82eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
83eda14cbcSMatt Macy 		return (NULL);
84eda14cbcSMatt Macy 	}
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 	while ((err = zfs_ioctl(hdl, ZFS_IOC_POOL_TRYIMPORT,
87eda14cbcSMatt Macy 	    &zc)) != 0 && errno == ENOMEM) {
88eda14cbcSMatt Macy 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
89eda14cbcSMatt Macy 			zcmd_free_nvlists(&zc);
90eda14cbcSMatt Macy 			return (NULL);
91eda14cbcSMatt Macy 		}
92eda14cbcSMatt Macy 	}
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 	if (err) {
95eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
96eda14cbcSMatt Macy 		return (NULL);
97eda14cbcSMatt Macy 	}
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy 	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
100eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
101eda14cbcSMatt Macy 		return (NULL);
102eda14cbcSMatt Macy 	}
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
105eda14cbcSMatt Macy 	return (nvl);
106eda14cbcSMatt Macy }
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy static nvlist_t *
109eda14cbcSMatt Macy refresh_config_libzfs(void *handle, nvlist_t *tryconfig)
110eda14cbcSMatt Macy {
111eda14cbcSMatt Macy 	return (refresh_config((libzfs_handle_t *)handle, tryconfig));
112eda14cbcSMatt Macy }
113eda14cbcSMatt Macy 
114eda14cbcSMatt Macy static int
115eda14cbcSMatt Macy pool_active_libzfs(void *handle, const char *name, uint64_t guid,
116eda14cbcSMatt Macy     boolean_t *isactive)
117eda14cbcSMatt Macy {
118eda14cbcSMatt Macy 	return (pool_active((libzfs_handle_t *)handle, name, guid, isactive));
119eda14cbcSMatt Macy }
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy const pool_config_ops_t libzfs_config_ops = {
122eda14cbcSMatt Macy 	.pco_refresh_config = refresh_config_libzfs,
123eda14cbcSMatt Macy 	.pco_pool_active = pool_active_libzfs,
124eda14cbcSMatt Macy };
125eda14cbcSMatt Macy 
126eda14cbcSMatt Macy /*
127eda14cbcSMatt Macy  * Return the offset of the given label.
128eda14cbcSMatt Macy  */
129eda14cbcSMatt Macy static uint64_t
130eda14cbcSMatt Macy label_offset(uint64_t size, int l)
131eda14cbcSMatt Macy {
132eda14cbcSMatt Macy 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
133eda14cbcSMatt Macy 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
134eda14cbcSMatt Macy 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
135eda14cbcSMatt Macy }
136eda14cbcSMatt Macy 
137eda14cbcSMatt Macy /*
138eda14cbcSMatt Macy  * Given a file descriptor, clear (zero) the label information.  This function
139eda14cbcSMatt Macy  * is used in the appliance stack as part of the ZFS sysevent module and
140eda14cbcSMatt Macy  * to implement the "zpool labelclear" command.
141eda14cbcSMatt Macy  */
142eda14cbcSMatt Macy int
143eda14cbcSMatt Macy zpool_clear_label(int fd)
144eda14cbcSMatt Macy {
145eda14cbcSMatt Macy 	struct stat64 statbuf;
146eda14cbcSMatt Macy 	int l;
147eda14cbcSMatt Macy 	vdev_label_t *label;
148eda14cbcSMatt Macy 	l2arc_dev_hdr_phys_t *l2dhdr;
149eda14cbcSMatt Macy 	uint64_t size;
150eda14cbcSMatt Macy 	int labels_cleared = 0, header_cleared = 0;
151eda14cbcSMatt Macy 	boolean_t clear_l2arc_header = B_FALSE;
152eda14cbcSMatt Macy 
153eda14cbcSMatt Macy 	if (fstat64_blk(fd, &statbuf) == -1)
154eda14cbcSMatt Macy 		return (0);
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 	if ((label = calloc(1, sizeof (vdev_label_t))) == NULL)
159eda14cbcSMatt Macy 		return (-1);
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 	if ((l2dhdr = calloc(1, sizeof (l2arc_dev_hdr_phys_t))) == NULL) {
162eda14cbcSMatt Macy 		free(label);
163eda14cbcSMatt Macy 		return (-1);
164eda14cbcSMatt Macy 	}
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy 	for (l = 0; l < VDEV_LABELS; l++) {
167eda14cbcSMatt Macy 		uint64_t state, guid, l2cache;
168eda14cbcSMatt Macy 		nvlist_t *config;
169eda14cbcSMatt Macy 
170eda14cbcSMatt Macy 		if (pread64(fd, label, sizeof (vdev_label_t),
171eda14cbcSMatt Macy 		    label_offset(size, l)) != sizeof (vdev_label_t)) {
172eda14cbcSMatt Macy 			continue;
173eda14cbcSMatt Macy 		}
174eda14cbcSMatt Macy 
175eda14cbcSMatt Macy 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
176eda14cbcSMatt Macy 		    sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0) {
177eda14cbcSMatt Macy 			continue;
178eda14cbcSMatt Macy 		}
179eda14cbcSMatt Macy 
180eda14cbcSMatt Macy 		/* Skip labels which do not have a valid guid. */
181eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
182eda14cbcSMatt Macy 		    &guid) != 0 || guid == 0) {
183eda14cbcSMatt Macy 			nvlist_free(config);
184eda14cbcSMatt Macy 			continue;
185eda14cbcSMatt Macy 		}
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 		/* Skip labels which are not in a known valid state. */
188eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
189eda14cbcSMatt Macy 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
190eda14cbcSMatt Macy 			nvlist_free(config);
191eda14cbcSMatt Macy 			continue;
192eda14cbcSMatt Macy 		}
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 		/* If the device is a cache device clear the header. */
195eda14cbcSMatt Macy 		if (!clear_l2arc_header) {
196eda14cbcSMatt Macy 			if (nvlist_lookup_uint64(config,
197eda14cbcSMatt Macy 			    ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 &&
198eda14cbcSMatt Macy 			    l2cache == POOL_STATE_L2CACHE) {
199eda14cbcSMatt Macy 				clear_l2arc_header = B_TRUE;
200eda14cbcSMatt Macy 			}
201eda14cbcSMatt Macy 		}
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy 		nvlist_free(config);
204eda14cbcSMatt Macy 
205eda14cbcSMatt Macy 		/*
206eda14cbcSMatt Macy 		 * A valid label was found, overwrite this label's nvlist
207eda14cbcSMatt Macy 		 * and uberblocks with zeros on disk.  This is done to prevent
208eda14cbcSMatt Macy 		 * system utilities, like blkid, from incorrectly detecting a
209eda14cbcSMatt Macy 		 * partial label.  The leading pad space is left untouched.
210eda14cbcSMatt Macy 		 */
211eda14cbcSMatt Macy 		memset(label, 0, sizeof (vdev_label_t));
212eda14cbcSMatt Macy 		size_t label_size = sizeof (vdev_label_t) - (2 * VDEV_PAD_SIZE);
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy 		if (pwrite64(fd, label, label_size, label_offset(size, l) +
215eda14cbcSMatt Macy 		    (2 * VDEV_PAD_SIZE)) == label_size) {
216eda14cbcSMatt Macy 			labels_cleared++;
217eda14cbcSMatt Macy 		}
218eda14cbcSMatt Macy 	}
219eda14cbcSMatt Macy 
220eda14cbcSMatt Macy 	/* Clear the L2ARC header. */
221eda14cbcSMatt Macy 	if (clear_l2arc_header) {
222eda14cbcSMatt Macy 		memset(l2dhdr, 0, sizeof (l2arc_dev_hdr_phys_t));
223eda14cbcSMatt Macy 		if (pwrite64(fd, l2dhdr, sizeof (l2arc_dev_hdr_phys_t),
224eda14cbcSMatt Macy 		    VDEV_LABEL_START_SIZE) == sizeof (l2arc_dev_hdr_phys_t)) {
225eda14cbcSMatt Macy 			header_cleared++;
226eda14cbcSMatt Macy 		}
227eda14cbcSMatt Macy 	}
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 	free(label);
230eda14cbcSMatt Macy 	free(l2dhdr);
231eda14cbcSMatt Macy 
232eda14cbcSMatt Macy 	if (labels_cleared == 0)
233eda14cbcSMatt Macy 		return (-1);
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	return (0);
236eda14cbcSMatt Macy }
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy static boolean_t
239eda14cbcSMatt Macy find_guid(nvlist_t *nv, uint64_t guid)
240eda14cbcSMatt Macy {
241eda14cbcSMatt Macy 	nvlist_t **child;
242eda14cbcSMatt Macy 	uint_t c, children;
243eda14cbcSMatt Macy 
244*da5137abSMartin Matuska 	if (fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID) == guid)
245eda14cbcSMatt Macy 		return (B_TRUE);
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
248eda14cbcSMatt Macy 	    &child, &children) == 0) {
249eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
250eda14cbcSMatt Macy 			if (find_guid(child[c], guid))
251eda14cbcSMatt Macy 				return (B_TRUE);
252eda14cbcSMatt Macy 	}
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy 	return (B_FALSE);
255eda14cbcSMatt Macy }
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy typedef struct aux_cbdata {
258eda14cbcSMatt Macy 	const char	*cb_type;
259eda14cbcSMatt Macy 	uint64_t	cb_guid;
260eda14cbcSMatt Macy 	zpool_handle_t	*cb_zhp;
261eda14cbcSMatt Macy } aux_cbdata_t;
262eda14cbcSMatt Macy 
263eda14cbcSMatt Macy static int
264eda14cbcSMatt Macy find_aux(zpool_handle_t *zhp, void *data)
265eda14cbcSMatt Macy {
266eda14cbcSMatt Macy 	aux_cbdata_t *cbp = data;
267eda14cbcSMatt Macy 	nvlist_t **list;
268*da5137abSMartin Matuska 	uint_t count;
269eda14cbcSMatt Macy 
270*da5137abSMartin Matuska 	nvlist_t *nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
271*da5137abSMartin Matuska 	    ZPOOL_CONFIG_VDEV_TREE);
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
274eda14cbcSMatt Macy 	    &list, &count) == 0) {
275*da5137abSMartin Matuska 		for (uint_t i = 0; i < count; i++) {
276*da5137abSMartin Matuska 			uint64_t guid = fnvlist_lookup_uint64(list[i],
277*da5137abSMartin Matuska 			    ZPOOL_CONFIG_GUID);
278eda14cbcSMatt Macy 			if (guid == cbp->cb_guid) {
279eda14cbcSMatt Macy 				cbp->cb_zhp = zhp;
280eda14cbcSMatt Macy 				return (1);
281eda14cbcSMatt Macy 			}
282eda14cbcSMatt Macy 		}
283eda14cbcSMatt Macy 	}
284eda14cbcSMatt Macy 
285eda14cbcSMatt Macy 	zpool_close(zhp);
286eda14cbcSMatt Macy 	return (0);
287eda14cbcSMatt Macy }
288eda14cbcSMatt Macy 
289eda14cbcSMatt Macy /*
290eda14cbcSMatt Macy  * Determines if the pool is in use.  If so, it returns true and the state of
291eda14cbcSMatt Macy  * the pool as well as the name of the pool.  Name string is allocated and
292eda14cbcSMatt Macy  * must be freed by the caller.
293eda14cbcSMatt Macy  */
294eda14cbcSMatt Macy int
295eda14cbcSMatt Macy zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
296eda14cbcSMatt Macy     boolean_t *inuse)
297eda14cbcSMatt Macy {
298eda14cbcSMatt Macy 	nvlist_t *config;
299*da5137abSMartin Matuska 	char *name = NULL;
300eda14cbcSMatt Macy 	boolean_t ret;
301*da5137abSMartin Matuska 	uint64_t guid = 0, vdev_guid;
302eda14cbcSMatt Macy 	zpool_handle_t *zhp;
303eda14cbcSMatt Macy 	nvlist_t *pool_config;
304eda14cbcSMatt Macy 	uint64_t stateval, isspare;
305eda14cbcSMatt Macy 	aux_cbdata_t cb = { 0 };
306eda14cbcSMatt Macy 	boolean_t isactive;
307eda14cbcSMatt Macy 
308eda14cbcSMatt Macy 	*inuse = B_FALSE;
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 	if (zpool_read_label(fd, &config, NULL) != 0) {
311eda14cbcSMatt Macy 		(void) no_memory(hdl);
312eda14cbcSMatt Macy 		return (-1);
313eda14cbcSMatt Macy 	}
314eda14cbcSMatt Macy 
315eda14cbcSMatt Macy 	if (config == NULL)
316eda14cbcSMatt Macy 		return (0);
317eda14cbcSMatt Macy 
318*da5137abSMartin Matuska 	stateval = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
319*da5137abSMartin Matuska 	vdev_guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID);
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
322*da5137abSMartin Matuska 		name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
323*da5137abSMartin Matuska 		guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
324eda14cbcSMatt Macy 	}
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	switch (stateval) {
327eda14cbcSMatt Macy 	case POOL_STATE_EXPORTED:
328eda14cbcSMatt Macy 		/*
329eda14cbcSMatt Macy 		 * A pool with an exported state may in fact be imported
330eda14cbcSMatt Macy 		 * read-only, so check the in-core state to see if it's
331eda14cbcSMatt Macy 		 * active and imported read-only.  If it is, set
332eda14cbcSMatt Macy 		 * its state to active.
333eda14cbcSMatt Macy 		 */
334eda14cbcSMatt Macy 		if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
335eda14cbcSMatt Macy 		    (zhp = zpool_open_canfail(hdl, name)) != NULL) {
336eda14cbcSMatt Macy 			if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
337eda14cbcSMatt Macy 				stateval = POOL_STATE_ACTIVE;
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy 			/*
340eda14cbcSMatt Macy 			 * All we needed the zpool handle for is the
341eda14cbcSMatt Macy 			 * readonly prop check.
342eda14cbcSMatt Macy 			 */
343eda14cbcSMatt Macy 			zpool_close(zhp);
344eda14cbcSMatt Macy 		}
345eda14cbcSMatt Macy 
346eda14cbcSMatt Macy 		ret = B_TRUE;
347eda14cbcSMatt Macy 		break;
348eda14cbcSMatt Macy 
349eda14cbcSMatt Macy 	case POOL_STATE_ACTIVE:
350eda14cbcSMatt Macy 		/*
351eda14cbcSMatt Macy 		 * For an active pool, we have to determine if it's really part
352eda14cbcSMatt Macy 		 * of a currently active pool (in which case the pool will exist
353eda14cbcSMatt Macy 		 * and the guid will be the same), or whether it's part of an
354eda14cbcSMatt Macy 		 * active pool that was disconnected without being explicitly
355eda14cbcSMatt Macy 		 * exported.
356eda14cbcSMatt Macy 		 */
357eda14cbcSMatt Macy 		if (pool_active(hdl, name, guid, &isactive) != 0) {
358eda14cbcSMatt Macy 			nvlist_free(config);
359eda14cbcSMatt Macy 			return (-1);
360eda14cbcSMatt Macy 		}
361eda14cbcSMatt Macy 
362eda14cbcSMatt Macy 		if (isactive) {
363eda14cbcSMatt Macy 			/*
364eda14cbcSMatt Macy 			 * Because the device may have been removed while
365eda14cbcSMatt Macy 			 * offlined, we only report it as active if the vdev is
366eda14cbcSMatt Macy 			 * still present in the config.  Otherwise, pretend like
367eda14cbcSMatt Macy 			 * it's not in use.
368eda14cbcSMatt Macy 			 */
369eda14cbcSMatt Macy 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
370eda14cbcSMatt Macy 			    (pool_config = zpool_get_config(zhp, NULL))
371eda14cbcSMatt Macy 			    != NULL) {
372*da5137abSMartin Matuska 				nvlist_t *nvroot = fnvlist_lookup_nvlist(
373*da5137abSMartin Matuska 				    pool_config, ZPOOL_CONFIG_VDEV_TREE);
374eda14cbcSMatt Macy 				ret = find_guid(nvroot, vdev_guid);
375eda14cbcSMatt Macy 			} else {
376eda14cbcSMatt Macy 				ret = B_FALSE;
377eda14cbcSMatt Macy 			}
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 			/*
380eda14cbcSMatt Macy 			 * If this is an active spare within another pool, we
381eda14cbcSMatt Macy 			 * treat it like an unused hot spare.  This allows the
382eda14cbcSMatt Macy 			 * user to create a pool with a hot spare that currently
383eda14cbcSMatt Macy 			 * in use within another pool.  Since we return B_TRUE,
384eda14cbcSMatt Macy 			 * libdiskmgt will continue to prevent generic consumers
385eda14cbcSMatt Macy 			 * from using the device.
386eda14cbcSMatt Macy 			 */
387eda14cbcSMatt Macy 			if (ret && nvlist_lookup_uint64(config,
388eda14cbcSMatt Macy 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
389eda14cbcSMatt Macy 				stateval = POOL_STATE_SPARE;
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy 			if (zhp != NULL)
392eda14cbcSMatt Macy 				zpool_close(zhp);
393eda14cbcSMatt Macy 		} else {
394eda14cbcSMatt Macy 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
395eda14cbcSMatt Macy 			ret = B_TRUE;
396eda14cbcSMatt Macy 		}
397eda14cbcSMatt Macy 		break;
398eda14cbcSMatt Macy 
399eda14cbcSMatt Macy 	case POOL_STATE_SPARE:
400eda14cbcSMatt Macy 		/*
401eda14cbcSMatt Macy 		 * For a hot spare, it can be either definitively in use, or
402eda14cbcSMatt Macy 		 * potentially active.  To determine if it's in use, we iterate
403eda14cbcSMatt Macy 		 * over all pools in the system and search for one with a spare
404eda14cbcSMatt Macy 		 * with a matching guid.
405eda14cbcSMatt Macy 		 *
406eda14cbcSMatt Macy 		 * Due to the shared nature of spares, we don't actually report
407eda14cbcSMatt Macy 		 * the potentially active case as in use.  This means the user
408eda14cbcSMatt Macy 		 * can freely create pools on the hot spares of exported pools,
409eda14cbcSMatt Macy 		 * but to do otherwise makes the resulting code complicated, and
410eda14cbcSMatt Macy 		 * we end up having to deal with this case anyway.
411eda14cbcSMatt Macy 		 */
412eda14cbcSMatt Macy 		cb.cb_zhp = NULL;
413eda14cbcSMatt Macy 		cb.cb_guid = vdev_guid;
414eda14cbcSMatt Macy 		cb.cb_type = ZPOOL_CONFIG_SPARES;
415eda14cbcSMatt Macy 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
416eda14cbcSMatt Macy 			name = (char *)zpool_get_name(cb.cb_zhp);
417eda14cbcSMatt Macy 			ret = B_TRUE;
418eda14cbcSMatt Macy 		} else {
419eda14cbcSMatt Macy 			ret = B_FALSE;
420eda14cbcSMatt Macy 		}
421eda14cbcSMatt Macy 		break;
422eda14cbcSMatt Macy 
423eda14cbcSMatt Macy 	case POOL_STATE_L2CACHE:
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy 		/*
426eda14cbcSMatt Macy 		 * Check if any pool is currently using this l2cache device.
427eda14cbcSMatt Macy 		 */
428eda14cbcSMatt Macy 		cb.cb_zhp = NULL;
429eda14cbcSMatt Macy 		cb.cb_guid = vdev_guid;
430eda14cbcSMatt Macy 		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
431eda14cbcSMatt Macy 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
432eda14cbcSMatt Macy 			name = (char *)zpool_get_name(cb.cb_zhp);
433eda14cbcSMatt Macy 			ret = B_TRUE;
434eda14cbcSMatt Macy 		} else {
435eda14cbcSMatt Macy 			ret = B_FALSE;
436eda14cbcSMatt Macy 		}
437eda14cbcSMatt Macy 		break;
438eda14cbcSMatt Macy 
439eda14cbcSMatt Macy 	default:
440eda14cbcSMatt Macy 		ret = B_FALSE;
441eda14cbcSMatt Macy 	}
442eda14cbcSMatt Macy 
443eda14cbcSMatt Macy 
444eda14cbcSMatt Macy 	if (ret) {
445eda14cbcSMatt Macy 		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
446eda14cbcSMatt Macy 			if (cb.cb_zhp)
447eda14cbcSMatt Macy 				zpool_close(cb.cb_zhp);
448eda14cbcSMatt Macy 			nvlist_free(config);
449eda14cbcSMatt Macy 			return (-1);
450eda14cbcSMatt Macy 		}
451eda14cbcSMatt Macy 		*state = (pool_state_t)stateval;
452eda14cbcSMatt Macy 	}
453eda14cbcSMatt Macy 
454eda14cbcSMatt Macy 	if (cb.cb_zhp)
455eda14cbcSMatt Macy 		zpool_close(cb.cb_zhp);
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	nvlist_free(config);
458eda14cbcSMatt Macy 	*inuse = ret;
459eda14cbcSMatt Macy 	return (0);
460eda14cbcSMatt Macy }
461