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  */
21078266a5SMarcel Telka 
22fa9e4066Sahrens /*
23f9af39baSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
245dafeea3SPavel Zakharov  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25bd0f7091SAndrew Stormont  * Copyright 2015 RackTop Systems.
2646d46cd4SYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
27fa9e4066Sahrens  */
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * Pool import support functions.
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * To import a pool, we rely on reading the configuration information from the
33fa9e4066Sahrens  * ZFS label of each device.  If we successfully read the label, then we
34fa9e4066Sahrens  * organize the configuration information in the following hierarchy:
35fa9e4066Sahrens  *
36ddfe901bSsara hartse  *	pool guid -> toplevel vdev guid -> label txg
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * Duplicate entries matching this same tuple will be discarded.  Once we have
39fa9e4066Sahrens  * examined every device, we pick the best label txg config for each toplevel
40fa9e4066Sahrens  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
41fa9e4066Sahrens  * update any paths that have changed.  Finally, we attempt to import the pool
42fa9e4066Sahrens  * using our derived config, and record the results.
43fa9e4066Sahrens  */
454f67d755SEric Taylor #include <ctype.h>
46fa9e4066Sahrens #include <devid.h>
47fa9e4066Sahrens #include <dirent.h>
48fa9e4066Sahrens #include <errno.h>
49fa9e4066Sahrens #include <libintl.h>
504f67d755SEric Taylor #include <stddef.h>
51fa9e4066Sahrens #include <stdlib.h>
52fa9e4066Sahrens #include <string.h>
53fa9e4066Sahrens #include <sys/stat.h>
54fa9e4066Sahrens #include <unistd.h>
55fa9e4066Sahrens #include <fcntl.h>
564f67d755SEric Taylor #include <sys/vtoc.h>
574f67d755SEric Taylor #include <sys/dktp/fdisk.h>
584f67d755SEric Taylor #include <sys/efi_partition.h>
594f67d755SEric Taylor #include <thread_pool.h>
61fa9e4066Sahrens #include <sys/vdev_impl.h>
62d8ab6e12SDon Brady #include <libzutil.h>
63*f0a05239SGeorge Amanakis #include <sys/arc_impl.h>
65fa9e4066Sahrens #include "libzfs.h"
66fa9e4066Sahrens #include "libzfs_impl.h"
68eaca9bbdSeschrock /*
69eaca9bbdSeschrock  * Returns true if the named pool matches the given GUID.
70eaca9bbdSeschrock  */
7194de1d4cSeschrock static int
pool_active(libzfs_handle_t * hdl,const char * name,uint64_t guid,boolean_t * isactive)7294de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
7394de1d4cSeschrock     boolean_t *isactive)
74eaca9bbdSeschrock {
75eaca9bbdSeschrock 	zpool_handle_t *zhp;
76eaca9bbdSeschrock 	uint64_t theguid;
7894de1d4cSeschrock 	if (zpool_open_silent(hdl, name, &zhp) != 0)
7994de1d4cSeschrock 		return (-1);
8194de1d4cSeschrock 	if (zhp == NULL) {
8294de1d4cSeschrock 		*isactive = B_FALSE;
8394de1d4cSeschrock 		return (0);
8494de1d4cSeschrock 	}
86eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
87eaca9bbdSeschrock 	    &theguid) == 0);
89eaca9bbdSeschrock 	zpool_close(zhp);
9194de1d4cSeschrock 	*isactive = (theguid == guid);
9294de1d4cSeschrock 	return (0);
93eaca9bbdSeschrock }
952f8aaab3Seschrock static nvlist_t *
refresh_config(libzfs_handle_t * hdl,nvlist_t * config)962f8aaab3Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
972f8aaab3Seschrock {
982f8aaab3Seschrock 	nvlist_t *nvl;
99d8ab6e12SDon Brady 	zfs_cmd_t zc = {"\0"};
1008b65a70bSPavel Zakharov 	int err, dstbuf_size;
1022f8aaab3Seschrock 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
1032f8aaab3Seschrock 		return (NULL);
1058b65a70bSPavel Zakharov 	dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4);
1068b65a70bSPavel Zakharov 
1078b65a70bSPavel Zakharov 	if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
1082f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
1092f8aaab3Seschrock 		return (NULL);
1102f8aaab3Seschrock 	}
112d8ab6e12SDon Brady 	while ((err = zfs_ioctl(hdl, ZFS_IOC_POOL_TRYIMPORT,
1132f8aaab3Seschrock 	    &zc)) != 0 && errno == ENOMEM) {
1142f8aaab3Seschrock 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1152f8aaab3Seschrock 			zcmd_free_nvlists(&zc);
1162f8aaab3Seschrock 			return (NULL);
1172f8aaab3Seschrock 		}
1182f8aaab3Seschrock 	}
1202f8aaab3Seschrock 	if (err) {
1212f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
1222f8aaab3Seschrock 		return (NULL);
1232f8aaab3Seschrock 	}
1252f8aaab3Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
1262f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
1272f8aaab3Seschrock 		return (NULL);
1282f8aaab3Seschrock 	}
1302f8aaab3Seschrock 	zcmd_free_nvlists(&zc);
1312f8aaab3Seschrock 	return (nvl);
1322f8aaab3Seschrock }
134d8ab6e12SDon Brady static nvlist_t *
refresh_config_libzfs(void * handle,nvlist_t * tryconfig)135d8ab6e12SDon Brady refresh_config_libzfs(void *handle, nvlist_t *tryconfig)
13688ecc943SGeorge Wilson {
137d8ab6e12SDon Brady 	return (refresh_config((libzfs_handle_t *)handle, tryconfig));
13888ecc943SGeorge Wilson }
13988ecc943SGeorge Wilson 
140d8ab6e12SDon Brady static int
pool_active_libzfs(void * handle,const char * name,uint64_t guid,boolean_t * isactive)141d8ab6e12SDon Brady pool_active_libzfs(void *handle, const char *name, uint64_t guid,
142d8ab6e12SDon Brady     boolean_t *isactive)
143fa9e4066Sahrens {
144d8ab6e12SDon Brady 	return (pool_active((libzfs_handle_t *)handle, name, guid, isactive));
145fa9e4066Sahrens }
147d8ab6e12SDon Brady const pool_config_ops_t libzfs_config_ops = {
148d8ab6e12SDon Brady 	.pco_refresh_config = refresh_config_libzfs,
149d8ab6e12SDon Brady 	.pco_pool_active = pool_active_libzfs,
150d8ab6e12SDon Brady };
151d8ab6e12SDon Brady 
152fa9e4066Sahrens /*
153fa9e4066Sahrens  * Return the offset of the given label.
154fa9e4066Sahrens  */
155fa9e4066Sahrens static uint64_t
label_offset(uint64_t size,int l)156e7437265Sahrens label_offset(uint64_t size, int l)
157fa9e4066Sahrens {
158e7437265Sahrens 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
159fa9e4066Sahrens 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
160fa9e4066Sahrens 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
161fa9e4066Sahrens }
163096d22d4SEric Schrock /*
1646401734dSWill Andrews  * Given a file descriptor, clear (zero) the label information.
165096d22d4SEric Schrock  */
166096d22d4SEric Schrock int
zpool_clear_label(int fd)167096d22d4SEric Schrock zpool_clear_label(int fd)
168096d22d4SEric Schrock {
169096d22d4SEric Schrock 	struct stat64 statbuf;
170096d22d4SEric Schrock 	int l;
171096d22d4SEric Schrock 	vdev_label_t *label;
172*f0a05239SGeorge Amanakis 	l2arc_dev_hdr_phys_t *l2dhdr;
173096d22d4SEric Schrock 	uint64_t size;
174*f0a05239SGeorge Amanakis 	int labels_cleared = 0, header_cleared = 0;
175*f0a05239SGeorge Amanakis 	boolean_t clear_l2arc_header = B_FALSE;
176096d22d4SEric Schrock 
177096d22d4SEric Schrock 	if (fstat64(fd, &statbuf) == -1)
178096d22d4SEric Schrock 		return (0);
1790ac89930SBrian Behlendorf 
180096d22d4SEric Schrock 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
181096d22d4SEric Schrock 
182096d22d4SEric Schrock 	if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
183096d22d4SEric Schrock 		return (-1);
184096d22d4SEric Schrock 
185*f0a05239SGeorge Amanakis 	if ((l2dhdr = calloc(1, sizeof (l2arc_dev_hdr_phys_t))) == NULL) {
186*f0a05239SGeorge Amanakis 		free(label);
187*f0a05239SGeorge Amanakis 		return (-1);
188*f0a05239SGeorge Amanakis 	}
189*f0a05239SGeorge Amanakis 
190096d22d4SEric Schrock 	for (l = 0; l < VDEV_LABELS; l++) {
191*f0a05239SGeorge Amanakis 		uint64_t state, guid, l2cache;
1920ac89930SBrian Behlendorf 		nvlist_t *config;
1930ac89930SBrian Behlendorf 
1940ac89930SBrian Behlendorf 		if (pread64(fd, label, sizeof (vdev_label_t),
195078266a5SMarcel Telka 		    label_offset(size, l)) != sizeof (vdev_label_t)) {
1960ac89930SBrian Behlendorf 			continue;
1970ac89930SBrian Behlendorf 		}
1980ac89930SBrian Behlendorf 
1990ac89930SBrian Behlendorf 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
2000ac89930SBrian Behlendorf 		    sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0) {
2010ac89930SBrian Behlendorf 			continue;
2020ac89930SBrian Behlendorf 		}
2030ac89930SBrian Behlendorf 
2040ac89930SBrian Behlendorf 		/* Skip labels which do not have a valid guid. */
2050ac89930SBrian Behlendorf 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
2060ac89930SBrian Behlendorf 		    &guid) != 0 || guid == 0) {
2070ac89930SBrian Behlendorf 			nvlist_free(config);
2080ac89930SBrian Behlendorf 			continue;
2090ac89930SBrian Behlendorf 		}
2100ac89930SBrian Behlendorf 
2110ac89930SBrian Behlendorf 		/* Skip labels which are not in a known valid state. */
2120ac89930SBrian Behlendorf 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2130ac89930SBrian Behlendorf 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
2140ac89930SBrian Behlendorf 			nvlist_free(config);
2150ac89930SBrian Behlendorf 			continue;
2160ac89930SBrian Behlendorf 		}
2170ac89930SBrian Behlendorf 
218*f0a05239SGeorge Amanakis 		/* If the device is a cache device clear the header. */
219*f0a05239SGeorge Amanakis 		if (!clear_l2arc_header) {
220*f0a05239SGeorge Amanakis 			if (nvlist_lookup_uint64(config,
221*f0a05239SGeorge Amanakis 			    ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 &&
222*f0a05239SGeorge Amanakis 			    l2cache == POOL_STATE_L2CACHE) {
223*f0a05239SGeorge Amanakis 				clear_l2arc_header = B_TRUE;
224*f0a05239SGeorge Amanakis 			}
225*f0a05239SGeorge Amanakis 		}
226*f0a05239SGeorge Amanakis 
2270ac89930SBrian Behlendorf 		nvlist_free(config);
2280ac89930SBrian Behlendorf 
2290ac89930SBrian Behlendorf 		/*
2300ac89930SBrian Behlendorf 		 * A valid label was found, overwrite this label's nvlist
2310ac89930SBrian Behlendorf 		 * and uberblocks with zeros on disk.  This is done to prevent
2320ac89930SBrian Behlendorf 		 * system utilities, like blkid, from incorrectly detecting a
2330ac89930SBrian Behlendorf 		 * partial label.  The leading pad space is left untouched.
2340ac89930SBrian Behlendorf 		 */
2350ac89930SBrian Behlendorf 		memset(label, 0, sizeof (vdev_label_t));
2360ac89930SBrian Behlendorf 		size_t label_size = sizeof (vdev_label_t) - (2 * VDEV_PAD_SIZE);
2370ac89930SBrian Behlendorf 
2380ac89930SBrian Behlendorf 		if (pwrite64(fd, label, label_size, label_offset(size, l) +
2390ac89930SBrian Behlendorf 		    (2 * VDEV_PAD_SIZE)) == label_size) {
2400ac89930SBrian Behlendorf 			labels_cleared++;
241078266a5SMarcel Telka 		}
242096d22d4SEric Schrock 	}
243096d22d4SEric Schrock 
244*f0a05239SGeorge Amanakis 	/* Clear the L2ARC header. */
245*f0a05239SGeorge Amanakis 	if (clear_l2arc_header) {
246*f0a05239SGeorge Amanakis 		memset(l2dhdr, 0, sizeof (l2arc_dev_hdr_phys_t));
247*f0a05239SGeorge Amanakis 		if (pwrite64(fd, l2dhdr, sizeof (l2arc_dev_hdr_phys_t),
248*f0a05239SGeorge Amanakis 		    VDEV_LABEL_START_SIZE) == sizeof (l2arc_dev_hdr_phys_t)) {
249*f0a05239SGeorge Amanakis 			header_cleared++;
250*f0a05239SGeorge Amanakis 		}
251*f0a05239SGeorge Amanakis 	}
252*f0a05239SGeorge Amanakis 
253096d22d4SEric Schrock 	free(label);
254*f0a05239SGeorge Amanakis 	free(l2dhdr);
2550ac89930SBrian Behlendorf 
2560ac89930SBrian Behlendorf 	if (labels_cleared == 0)
2570ac89930SBrian Behlendorf 		return (-1);
2580ac89930SBrian Behlendorf 
259096d22d4SEric Schrock 	return (0);
260096d22d4SEric Schrock }
261096d22d4SEric Schrock 
26299653d4eSeschrock boolean_t
find_guid(nvlist_t * nv,uint64_t guid)263fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid)
264fa9e4066Sahrens {
265fa9e4066Sahrens 	uint64_t tmp;
266fa9e4066Sahrens 	nvlist_t **child;
267fa9e4066Sahrens 	uint_t c, children;
269fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
270fa9e4066Sahrens 	if (tmp == guid)
27199653d4eSeschrock 		return (B_TRUE);
273fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
274fa9e4066Sahrens 	    &child, &children) == 0) {
275fa9e4066Sahrens 		for (c = 0; c < children; c++)
276fa9e4066Sahrens 			if (find_guid(child[c], guid))
27799653d4eSeschrock 				return (B_TRUE);
27899653d4eSeschrock 	}
28099653d4eSeschrock 	return (B_FALSE);
28199653d4eSeschrock }
283fa94a07fSbrendan typedef struct aux_cbdata {
284fa94a07fSbrendan 	const char	*cb_type;
28599653d4eSeschrock 	uint64_t	cb_guid;
28699653d4eSeschrock 	zpool_handle_t	*cb_zhp;
287fa94a07fSbrendan } aux_cbdata_t;
28999653d4eSeschrock static int
find_aux(zpool_handle_t * zhp,void * data)290fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data)
29199653d4eSeschrock {
292fa94a07fSbrendan 	aux_cbdata_t *cbp = data;
293fa94a07fSbrendan 	nvlist_t **list;
294fa94a07fSbrendan 	uint_t i, count;
29599653d4eSeschrock 	uint64_t guid;
29699653d4eSeschrock 	nvlist_t *nvroot;
29899653d4eSeschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
29999653d4eSeschrock 	    &nvroot) == 0);
301fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
302fa94a07fSbrendan 	    &list, &count) == 0) {
303fa94a07fSbrendan 		for (i = 0; i < count; i++) {
304fa94a07fSbrendan 			verify(nvlist_lookup_uint64(list[i],
30599653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
30699653d4eSeschrock 			if (guid == cbp->cb_guid) {
30799653d4eSeschrock 				cbp->cb_zhp = zhp;
30899653d4eSeschrock 				return (1);
30999653d4eSeschrock 			}
31099653d4eSeschrock 		}
311fa9e4066Sahrens 	}
31399653d4eSeschrock 	zpool_close(zhp);
31499653d4eSeschrock 	return (0);
315fa9e4066Sahrens }
317fa9e4066Sahrens /*
31899653d4eSeschrock  * Determines if the pool is in use.  If so, it returns true and the state of
319fa9e4066Sahrens  * the pool as well as the name of the pool.  Both strings are allocated and
320fa9e4066Sahrens  * must be freed by the caller.
321fa9e4066Sahrens  */
322fa9e4066Sahrens int
zpool_in_use(libzfs_handle_t * hdl,int fd,pool_state_t * state,char ** namestr,boolean_t * inuse)32399653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
32499653d4eSeschrock     boolean_t *inuse)
325fa9e4066Sahrens {
326fa9e4066Sahrens 	nvlist_t *config;
327fa9e4066Sahrens 	char *name;
32899653d4eSeschrock 	boolean_t ret;
329fa9e4066Sahrens 	uint64_t guid, vdev_guid;
330fa9e4066Sahrens 	zpool_handle_t *zhp;
331fa9e4066Sahrens 	nvlist_t *pool_config;
33239c23413Seschrock 	uint64_t stateval, isspare;
333fa94a07fSbrendan 	aux_cbdata_t cb = { 0 };
33494de1d4cSeschrock 	boolean_t isactive;
33699653d4eSeschrock 	*inuse = B_FALSE;
338d8ab6e12SDon Brady 	if (zpool_read_label(fd, &config, NULL) != 0 && errno == ENOMEM) {
33999653d4eSeschrock 		(void) no_memory(hdl);
34099653d4eSeschrock 		return (-1);
34199653d4eSeschrock 	}
34399653d4eSeschrock 	if (config == NULL)
34499653d4eSeschrock 		return (0);
346fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
34746a2abf2Seschrock 	    &stateval) == 0);
348fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
349fa9e4066Sahrens 	    &vdev_guid) == 0);
351fa94a07fSbrendan 	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
35299653d4eSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
35399653d4eSeschrock 		    &name) == 0);
35499653d4eSeschrock 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
35599653d4eSeschrock 		    &guid) == 0);
35699653d4eSeschrock 	}
35846a2abf2Seschrock 	switch (stateval) {
359fa9e4066Sahrens 	case POOL_STATE_EXPORTED:
360f9af39baSGeorge Wilson 		/*
361f9af39baSGeorge Wilson 		 * A pool with an exported state may in fact be imported
362f9af39baSGeorge Wilson 		 * read-only, so check the in-core state to see if it's
363f9af39baSGeorge Wilson 		 * active and imported read-only.  If it is, set
364f9af39baSGeorge Wilson 		 * its state to active.
365f9af39baSGeorge Wilson 		 */
366f9af39baSGeorge Wilson 		if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
367fb13f48fSJosef 'Jeff' Sipek 		    (zhp = zpool_open_canfail(hdl, name)) != NULL) {
368fb13f48fSJosef 'Jeff' Sipek 			if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
369fb13f48fSJosef 'Jeff' Sipek 				stateval = POOL_STATE_ACTIVE;
370fb13f48fSJosef 'Jeff' Sipek 
371fb13f48fSJosef 'Jeff' Sipek 			/*
372fb13f48fSJosef 'Jeff' Sipek 			 * All we needed the zpool handle for is the
373fb13f48fSJosef 'Jeff' Sipek 			 * readonly prop check.
374fb13f48fSJosef 'Jeff' Sipek 			 */
375fb13f48fSJosef 'Jeff' Sipek 			zpool_close(zhp);
376fb13f48fSJosef 'Jeff' Sipek 		}
377f9af39baSGeorge Wilson 
37899653d4eSeschrock 		ret = B_TRUE;
379fa9e4066Sahrens 		break;
381fa9e4066Sahrens 	case POOL_STATE_ACTIVE:
382fa9e4066Sahrens 		/*
383fa9e4066Sahrens 		 * For an active pool, we have to determine if it's really part
384eaca9bbdSeschrock 		 * of a currently active pool (in which case the pool will exist
385eaca9bbdSeschrock 		 * and the guid will be the same), or whether it's part of an
386eaca9bbdSeschrock 		 * active pool that was disconnected without being explicitly
387eaca9bbdSeschrock 		 * exported.
388fa9e4066Sahrens 		 */
38994de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0) {
39094de1d4cSeschrock 			nvlist_free(config);
39194de1d4cSeschrock 			return (-1);
39294de1d4cSeschrock 		}
39494de1d4cSeschrock 		if (isactive) {
395fa9e4066Sahrens 			/*
396fa9e4066Sahrens 			 * Because the device may have been removed while
397fa9e4066Sahrens 			 * offlined, we only report it as active if the vdev is
398fa9e4066Sahrens 			 * still present in the config.  Otherwise, pretend like
399fa9e4066Sahrens 			 * it's not in use.
400fa9e4066Sahrens 			 */
40199653d4eSeschrock 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
402088e9d47Seschrock 			    (pool_config = zpool_get_config(zhp, NULL))
403088e9d47Seschrock 			    != NULL) {
404fa9e4066Sahrens 				nvlist_t *nvroot;
406fa9e4066Sahrens 				verify(nvlist_lookup_nvlist(pool_config,
407fa9e4066Sahrens 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
40846a2abf2Seschrock 				ret = find_guid(nvroot, vdev_guid);
409fa9e4066Sahrens 			} else {
41099653d4eSeschrock 				ret = B_FALSE;
411fa9e4066Sahrens 			}
41339c23413Seschrock 			/*
41439c23413Seschrock 			 * If this is an active spare within another pool, we
41539c23413Seschrock 			 * treat it like an unused hot spare.  This allows the
41639c23413Seschrock 			 * user to create a pool with a hot spare that currently
41739c23413Seschrock 			 * in use within another pool.  Since we return B_TRUE,
41839c23413Seschrock 			 * libdiskmgt will continue to prevent generic consumers
41939c23413Seschrock 			 * from using the device.
42039c23413Seschrock 			 */
42139c23413Seschrock 			if (ret && nvlist_lookup_uint64(config,
42239c23413Seschrock 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
42339c23413Seschrock 				stateval = POOL_STATE_SPARE;
42599653d4eSeschrock 			if (zhp != NULL)
42699653d4eSeschrock 				zpool_close(zhp);
427fa9e4066Sahrens 		} else {
42846a2abf2Seschrock 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
42999653d4eSeschrock 			ret = B_TRUE;
43099653d4eSeschrock 		}
43199653d4eSeschrock 		break;
43399653d4eSeschrock 	case POOL_STATE_SPARE:
43499653d4eSeschrock 		/*
43599653d4eSeschrock 		 * For a hot spare, it can be either definitively in use, or
43699653d4eSeschrock 		 * potentially active.  To determine if it's in use, we iterate
43799653d4eSeschrock 		 * over all pools in the system and search for one with a spare
43899653d4eSeschrock 		 * with a matching guid.
43999653d4eSeschrock 		 *
44099653d4eSeschrock 		 * Due to the shared nature of spares, we don't actually report
44199653d4eSeschrock 		 * the potentially active case as in use.  This means the user
44299653d4eSeschrock 		 * can freely create pools on the hot spares of exported pools,
44399653d4eSeschrock 		 * but to do otherwise makes the resulting code complicated, and
44499653d4eSeschrock 		 * we end up having to deal with this case anyway.
44599653d4eSeschrock 		 */
44699653d4eSeschrock 		cb.cb_zhp = NULL;
44799653d4eSeschrock 		cb.cb_guid = vdev_guid;
448fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_SPARES;
449fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
450fa94a07fSbrendan 			name = (char *)zpool_get_name(cb.cb_zhp);
451078266a5SMarcel Telka 			ret = B_TRUE;
452fa94a07fSbrendan 		} else {
453078266a5SMarcel Telka 			ret = B_FALSE;
454fa94a07fSbrendan 		}
455fa94a07fSbrendan 		break;
457fa94a07fSbrendan 	case POOL_STATE_L2CACHE:
459fa94a07fSbrendan 		/*
460fa94a07fSbrendan 		 * Check if any pool is currently using this l2cache device.
461fa94a07fSbrendan 		 */
462fa94a07fSbrendan 		cb.cb_zhp = NULL;
463fa94a07fSbrendan 		cb.cb_guid = vdev_guid;
464fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
465fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
46699653d4eSeschrock 			name = (char *)zpool_get_name(cb.cb_zhp);
467078266a5SMarcel Telka 			ret = B_TRUE;
46899653d4eSeschrock 		} else {
469078266a5SMarcel Telka 			ret = B_FALSE;
470fa9e4066Sahrens 		}
471fa9e4066Sahrens 		break;
473fa9e4066Sahrens 	default:
47499653d4eSeschrock 		ret = B_FALSE;
475fa9e4066Sahrens 	}
47846a2abf2Seschrock 	if (ret) {
47999653d4eSeschrock 		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
480c6765aabSeschrock 			if (cb.cb_zhp)
481c6765aabSeschrock 				zpool_close(cb.cb_zhp);
48299653d4eSeschrock 			nvlist_free(config);
48399653d4eSeschrock 			return (-1);
48499653d4eSeschrock 		}
48546a2abf2Seschrock 		*state = (pool_state_t)stateval;
48646a2abf2Seschrock 	}
48899653d4eSeschrock 	if (cb.cb_zhp)
48999653d4eSeschrock 		zpool_close(cb.cb_zhp);
491fa9e4066Sahrens 	nvlist_free(config);
49299653d4eSeschrock 	*inuse = ret;
49399653d4eSeschrock 	return (0);
494fa9e4066Sahrens }