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  */
28fa9e4066Sahrens 
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  */
44fa9e4066Sahrens 
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>
60fa9e4066Sahrens 
61fa9e4066Sahrens #include <sys/vdev_impl.h>
62*d8ab6e12SDon Brady #include <libzutil.h>
63fa9e4066Sahrens 
64fa9e4066Sahrens #include "libzfs.h"
65fa9e4066Sahrens #include "libzfs_impl.h"
66fa9e4066Sahrens 
67eaca9bbdSeschrock /*
68eaca9bbdSeschrock  * Returns true if the named pool matches the given GUID.
69eaca9bbdSeschrock  */
7094de1d4cSeschrock static int
7194de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
7294de1d4cSeschrock     boolean_t *isactive)
73eaca9bbdSeschrock {
74eaca9bbdSeschrock 	zpool_handle_t *zhp;
75eaca9bbdSeschrock 	uint64_t theguid;
76eaca9bbdSeschrock 
7794de1d4cSeschrock 	if (zpool_open_silent(hdl, name, &zhp) != 0)
7894de1d4cSeschrock 		return (-1);
7994de1d4cSeschrock 
8094de1d4cSeschrock 	if (zhp == NULL) {
8194de1d4cSeschrock 		*isactive = B_FALSE;
8294de1d4cSeschrock 		return (0);
8394de1d4cSeschrock 	}
84eaca9bbdSeschrock 
85eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
86eaca9bbdSeschrock 	    &theguid) == 0);
87eaca9bbdSeschrock 
88eaca9bbdSeschrock 	zpool_close(zhp);
89eaca9bbdSeschrock 
9094de1d4cSeschrock 	*isactive = (theguid == guid);
9194de1d4cSeschrock 	return (0);
92eaca9bbdSeschrock }
93eaca9bbdSeschrock 
942f8aaab3Seschrock static nvlist_t *
952f8aaab3Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
962f8aaab3Seschrock {
972f8aaab3Seschrock 	nvlist_t *nvl;
98*d8ab6e12SDon Brady 	zfs_cmd_t zc = {"\0"};
998b65a70bSPavel Zakharov 	int err, dstbuf_size;
1002f8aaab3Seschrock 
1012f8aaab3Seschrock 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
1022f8aaab3Seschrock 		return (NULL);
1032f8aaab3Seschrock 
1048b65a70bSPavel Zakharov 	dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4);
1058b65a70bSPavel Zakharov 
1068b65a70bSPavel Zakharov 	if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
1072f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
1082f8aaab3Seschrock 		return (NULL);
1092f8aaab3Seschrock 	}
1102f8aaab3Seschrock 
111*d8ab6e12SDon Brady 	while ((err = zfs_ioctl(hdl, ZFS_IOC_POOL_TRYIMPORT,
1122f8aaab3Seschrock 	    &zc)) != 0 && errno == ENOMEM) {
1132f8aaab3Seschrock 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1142f8aaab3Seschrock 			zcmd_free_nvlists(&zc);
1152f8aaab3Seschrock 			return (NULL);
1162f8aaab3Seschrock 		}
1172f8aaab3Seschrock 	}
1182f8aaab3Seschrock 
1192f8aaab3Seschrock 	if (err) {
1202f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
1212f8aaab3Seschrock 		return (NULL);
1222f8aaab3Seschrock 	}
1232f8aaab3Seschrock 
1242f8aaab3Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
1252f8aaab3Seschrock 		zcmd_free_nvlists(&zc);
1262f8aaab3Seschrock 		return (NULL);
1272f8aaab3Seschrock 	}
1282f8aaab3Seschrock 
1292f8aaab3Seschrock 	zcmd_free_nvlists(&zc);
1302f8aaab3Seschrock 	return (nvl);
1312f8aaab3Seschrock }
1322f8aaab3Seschrock 
133*d8ab6e12SDon Brady static nvlist_t *
134*d8ab6e12SDon Brady refresh_config_libzfs(void *handle, nvlist_t *tryconfig)
13588ecc943SGeorge Wilson {
136*d8ab6e12SDon Brady 	return (refresh_config((libzfs_handle_t *)handle, tryconfig));
13788ecc943SGeorge Wilson }
13888ecc943SGeorge Wilson 
139*d8ab6e12SDon Brady static int
140*d8ab6e12SDon Brady pool_active_libzfs(void *handle, const char *name, uint64_t guid,
141*d8ab6e12SDon Brady     boolean_t *isactive)
142fa9e4066Sahrens {
143*d8ab6e12SDon Brady 	return (pool_active((libzfs_handle_t *)handle, name, guid, isactive));
144fa9e4066Sahrens }
145fa9e4066Sahrens 
146*d8ab6e12SDon Brady const pool_config_ops_t libzfs_config_ops = {
147*d8ab6e12SDon Brady 	.pco_refresh_config = refresh_config_libzfs,
148*d8ab6e12SDon Brady 	.pco_pool_active = pool_active_libzfs,
149*d8ab6e12SDon Brady };
150*d8ab6e12SDon Brady 
151fa9e4066Sahrens /*
152fa9e4066Sahrens  * Return the offset of the given label.
153fa9e4066Sahrens  */
154fa9e4066Sahrens static uint64_t
155e7437265Sahrens label_offset(uint64_t size, int l)
156fa9e4066Sahrens {
157e7437265Sahrens 	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
158fa9e4066Sahrens 	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
159fa9e4066Sahrens 	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
160fa9e4066Sahrens }
161fa9e4066Sahrens 
162096d22d4SEric Schrock /*
1636401734dSWill Andrews  * Given a file descriptor, clear (zero) the label information.
164096d22d4SEric Schrock  */
165096d22d4SEric Schrock int
166096d22d4SEric Schrock zpool_clear_label(int fd)
167096d22d4SEric Schrock {
168096d22d4SEric Schrock 	struct stat64 statbuf;
169096d22d4SEric Schrock 	int l;
170096d22d4SEric Schrock 	vdev_label_t *label;
171096d22d4SEric Schrock 	uint64_t size;
172096d22d4SEric Schrock 
173096d22d4SEric Schrock 	if (fstat64(fd, &statbuf) == -1)
174096d22d4SEric Schrock 		return (0);
175096d22d4SEric Schrock 	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
176096d22d4SEric Schrock 
177096d22d4SEric Schrock 	if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
178096d22d4SEric Schrock 		return (-1);
179096d22d4SEric Schrock 
180096d22d4SEric Schrock 	for (l = 0; l < VDEV_LABELS; l++) {
181096d22d4SEric Schrock 		if (pwrite64(fd, label, sizeof (vdev_label_t),
182078266a5SMarcel Telka 		    label_offset(size, l)) != sizeof (vdev_label_t)) {
183078266a5SMarcel Telka 			free(label);
184096d22d4SEric Schrock 			return (-1);
185078266a5SMarcel Telka 		}
186096d22d4SEric Schrock 	}
187096d22d4SEric Schrock 
188096d22d4SEric Schrock 	free(label);
189096d22d4SEric Schrock 	return (0);
190096d22d4SEric Schrock }
191096d22d4SEric Schrock 
19299653d4eSeschrock boolean_t
193fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	uint64_t tmp;
196fa9e4066Sahrens 	nvlist_t **child;
197fa9e4066Sahrens 	uint_t c, children;
198fa9e4066Sahrens 
199fa9e4066Sahrens 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
200fa9e4066Sahrens 	if (tmp == guid)
20199653d4eSeschrock 		return (B_TRUE);
202fa9e4066Sahrens 
203fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
204fa9e4066Sahrens 	    &child, &children) == 0) {
205fa9e4066Sahrens 		for (c = 0; c < children; c++)
206fa9e4066Sahrens 			if (find_guid(child[c], guid))
20799653d4eSeschrock 				return (B_TRUE);
20899653d4eSeschrock 	}
20999653d4eSeschrock 
21099653d4eSeschrock 	return (B_FALSE);
21199653d4eSeschrock }
21299653d4eSeschrock 
213fa94a07fSbrendan typedef struct aux_cbdata {
214fa94a07fSbrendan 	const char	*cb_type;
21599653d4eSeschrock 	uint64_t	cb_guid;
21699653d4eSeschrock 	zpool_handle_t	*cb_zhp;
217fa94a07fSbrendan } aux_cbdata_t;
21899653d4eSeschrock 
21999653d4eSeschrock static int
220fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data)
22199653d4eSeschrock {
222fa94a07fSbrendan 	aux_cbdata_t *cbp = data;
223fa94a07fSbrendan 	nvlist_t **list;
224fa94a07fSbrendan 	uint_t i, count;
22599653d4eSeschrock 	uint64_t guid;
22699653d4eSeschrock 	nvlist_t *nvroot;
22799653d4eSeschrock 
22899653d4eSeschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
22999653d4eSeschrock 	    &nvroot) == 0);
23099653d4eSeschrock 
231fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
232fa94a07fSbrendan 	    &list, &count) == 0) {
233fa94a07fSbrendan 		for (i = 0; i < count; i++) {
234fa94a07fSbrendan 			verify(nvlist_lookup_uint64(list[i],
23599653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
23699653d4eSeschrock 			if (guid == cbp->cb_guid) {
23799653d4eSeschrock 				cbp->cb_zhp = zhp;
23899653d4eSeschrock 				return (1);
23999653d4eSeschrock 			}
24099653d4eSeschrock 		}
241fa9e4066Sahrens 	}
242fa9e4066Sahrens 
24399653d4eSeschrock 	zpool_close(zhp);
24499653d4eSeschrock 	return (0);
245fa9e4066Sahrens }
246fa9e4066Sahrens 
247fa9e4066Sahrens /*
24899653d4eSeschrock  * Determines if the pool is in use.  If so, it returns true and the state of
249fa9e4066Sahrens  * the pool as well as the name of the pool.  Both strings are allocated and
250fa9e4066Sahrens  * must be freed by the caller.
251fa9e4066Sahrens  */
252fa9e4066Sahrens int
25399653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
25499653d4eSeschrock     boolean_t *inuse)
255fa9e4066Sahrens {
256fa9e4066Sahrens 	nvlist_t *config;
257fa9e4066Sahrens 	char *name;
25899653d4eSeschrock 	boolean_t ret;
259fa9e4066Sahrens 	uint64_t guid, vdev_guid;
260fa9e4066Sahrens 	zpool_handle_t *zhp;
261fa9e4066Sahrens 	nvlist_t *pool_config;
26239c23413Seschrock 	uint64_t stateval, isspare;
263fa94a07fSbrendan 	aux_cbdata_t cb = { 0 };
26494de1d4cSeschrock 	boolean_t isactive;
26599653d4eSeschrock 
26699653d4eSeschrock 	*inuse = B_FALSE;
267fa9e4066Sahrens 
268*d8ab6e12SDon Brady 	if (zpool_read_label(fd, &config, NULL) != 0 && errno == ENOMEM) {
26999653d4eSeschrock 		(void) no_memory(hdl);
27099653d4eSeschrock 		return (-1);
27199653d4eSeschrock 	}
27299653d4eSeschrock 
27399653d4eSeschrock 	if (config == NULL)
27499653d4eSeschrock 		return (0);
275fa9e4066Sahrens 
276fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
27746a2abf2Seschrock 	    &stateval) == 0);
278fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
279fa9e4066Sahrens 	    &vdev_guid) == 0);
280fa9e4066Sahrens 
281fa94a07fSbrendan 	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
28299653d4eSeschrock 		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
28399653d4eSeschrock 		    &name) == 0);
28499653d4eSeschrock 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
28599653d4eSeschrock 		    &guid) == 0);
28699653d4eSeschrock 	}
28799653d4eSeschrock 
28846a2abf2Seschrock 	switch (stateval) {
289fa9e4066Sahrens 	case POOL_STATE_EXPORTED:
290f9af39baSGeorge Wilson 		/*
291f9af39baSGeorge Wilson 		 * A pool with an exported state may in fact be imported
292f9af39baSGeorge Wilson 		 * read-only, so check the in-core state to see if it's
293f9af39baSGeorge Wilson 		 * active and imported read-only.  If it is, set
294f9af39baSGeorge Wilson 		 * its state to active.
295f9af39baSGeorge Wilson 		 */
296f9af39baSGeorge Wilson 		if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
297fb13f48fSJosef 'Jeff' Sipek 		    (zhp = zpool_open_canfail(hdl, name)) != NULL) {
298fb13f48fSJosef 'Jeff' Sipek 			if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
299fb13f48fSJosef 'Jeff' Sipek 				stateval = POOL_STATE_ACTIVE;
300fb13f48fSJosef 'Jeff' Sipek 
301fb13f48fSJosef 'Jeff' Sipek 			/*
302fb13f48fSJosef 'Jeff' Sipek 			 * All we needed the zpool handle for is the
303fb13f48fSJosef 'Jeff' Sipek 			 * readonly prop check.
304fb13f48fSJosef 'Jeff' Sipek 			 */
305fb13f48fSJosef 'Jeff' Sipek 			zpool_close(zhp);
306fb13f48fSJosef 'Jeff' Sipek 		}
307f9af39baSGeorge Wilson 
30899653d4eSeschrock 		ret = B_TRUE;
309fa9e4066Sahrens 		break;
310fa9e4066Sahrens 
311fa9e4066Sahrens 	case POOL_STATE_ACTIVE:
312fa9e4066Sahrens 		/*
313fa9e4066Sahrens 		 * For an active pool, we have to determine if it's really part
314eaca9bbdSeschrock 		 * of a currently active pool (in which case the pool will exist
315eaca9bbdSeschrock 		 * and the guid will be the same), or whether it's part of an
316eaca9bbdSeschrock 		 * active pool that was disconnected without being explicitly
317eaca9bbdSeschrock 		 * exported.
318fa9e4066Sahrens 		 */
31994de1d4cSeschrock 		if (pool_active(hdl, name, guid, &isactive) != 0) {
32094de1d4cSeschrock 			nvlist_free(config);
32194de1d4cSeschrock 			return (-1);
32294de1d4cSeschrock 		}
32394de1d4cSeschrock 
32494de1d4cSeschrock 		if (isactive) {
325fa9e4066Sahrens 			/*
326fa9e4066Sahrens 			 * Because the device may have been removed while
327fa9e4066Sahrens 			 * offlined, we only report it as active if the vdev is
328fa9e4066Sahrens 			 * still present in the config.  Otherwise, pretend like
329fa9e4066Sahrens 			 * it's not in use.
330fa9e4066Sahrens 			 */
33199653d4eSeschrock 			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
332088e9d47Seschrock 			    (pool_config = zpool_get_config(zhp, NULL))
333088e9d47Seschrock 			    != NULL) {
334fa9e4066Sahrens 				nvlist_t *nvroot;
335fa9e4066Sahrens 
336fa9e4066Sahrens 				verify(nvlist_lookup_nvlist(pool_config,
337fa9e4066Sahrens 				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
33846a2abf2Seschrock 				ret = find_guid(nvroot, vdev_guid);
339fa9e4066Sahrens 			} else {
34099653d4eSeschrock 				ret = B_FALSE;
341fa9e4066Sahrens 			}
34299653d4eSeschrock 
34339c23413Seschrock 			/*
34439c23413Seschrock 			 * If this is an active spare within another pool, we
34539c23413Seschrock 			 * treat it like an unused hot spare.  This allows the
34639c23413Seschrock 			 * user to create a pool with a hot spare that currently
34739c23413Seschrock 			 * in use within another pool.  Since we return B_TRUE,
34839c23413Seschrock 			 * libdiskmgt will continue to prevent generic consumers
34939c23413Seschrock 			 * from using the device.
35039c23413Seschrock 			 */
35139c23413Seschrock 			if (ret && nvlist_lookup_uint64(config,
35239c23413Seschrock 			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
35339c23413Seschrock 				stateval = POOL_STATE_SPARE;
35439c23413Seschrock 
35599653d4eSeschrock 			if (zhp != NULL)
35699653d4eSeschrock 				zpool_close(zhp);
357fa9e4066Sahrens 		} else {
35846a2abf2Seschrock 			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
35999653d4eSeschrock 			ret = B_TRUE;
36099653d4eSeschrock 		}
36199653d4eSeschrock 		break;
36299653d4eSeschrock 
36399653d4eSeschrock 	case POOL_STATE_SPARE:
36499653d4eSeschrock 		/*
36599653d4eSeschrock 		 * For a hot spare, it can be either definitively in use, or
36699653d4eSeschrock 		 * potentially active.  To determine if it's in use, we iterate
36799653d4eSeschrock 		 * over all pools in the system and search for one with a spare
36899653d4eSeschrock 		 * with a matching guid.
36999653d4eSeschrock 		 *
37099653d4eSeschrock 		 * Due to the shared nature of spares, we don't actually report
37199653d4eSeschrock 		 * the potentially active case as in use.  This means the user
37299653d4eSeschrock 		 * can freely create pools on the hot spares of exported pools,
37399653d4eSeschrock 		 * but to do otherwise makes the resulting code complicated, and
37499653d4eSeschrock 		 * we end up having to deal with this case anyway.
37599653d4eSeschrock 		 */
37699653d4eSeschrock 		cb.cb_zhp = NULL;
37799653d4eSeschrock 		cb.cb_guid = vdev_guid;
378fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_SPARES;
379fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
380fa94a07fSbrendan 			name = (char *)zpool_get_name(cb.cb_zhp);
381078266a5SMarcel Telka 			ret = B_TRUE;
382fa94a07fSbrendan 		} else {
383078266a5SMarcel Telka 			ret = B_FALSE;
384fa94a07fSbrendan 		}
385fa94a07fSbrendan 		break;
386fa94a07fSbrendan 
387fa94a07fSbrendan 	case POOL_STATE_L2CACHE:
388fa94a07fSbrendan 
389fa94a07fSbrendan 		/*
390fa94a07fSbrendan 		 * Check if any pool is currently using this l2cache device.
391fa94a07fSbrendan 		 */
392fa94a07fSbrendan 		cb.cb_zhp = NULL;
393fa94a07fSbrendan 		cb.cb_guid = vdev_guid;
394fa94a07fSbrendan 		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
395fa94a07fSbrendan 		if (zpool_iter(hdl, find_aux, &cb) == 1) {
39699653d4eSeschrock 			name = (char *)zpool_get_name(cb.cb_zhp);
397078266a5SMarcel Telka 			ret = B_TRUE;
39899653d4eSeschrock 		} else {
399078266a5SMarcel Telka 			ret = B_FALSE;
400fa9e4066Sahrens 		}
401fa9e4066Sahrens 		break;
402fa9e4066Sahrens 
403fa9e4066Sahrens 	default:
40499653d4eSeschrock 		ret = B_FALSE;
405fa9e4066Sahrens 	}
406fa9e4066Sahrens 
40746a2abf2Seschrock 
40846a2abf2Seschrock 	if (ret) {
40999653d4eSeschrock 		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
410c6765aabSeschrock 			if (cb.cb_zhp)
411c6765aabSeschrock 				zpool_close(cb.cb_zhp);
41299653d4eSeschrock 			nvlist_free(config);
41399653d4eSeschrock 			return (-1);
41499653d4eSeschrock 		}
41546a2abf2Seschrock 		*state = (pool_state_t)stateval;
41646a2abf2Seschrock 	}
41746a2abf2Seschrock 
41899653d4eSeschrock 	if (cb.cb_zhp)
41999653d4eSeschrock 		zpool_close(cb.cb_zhp);
42099653d4eSeschrock 
421fa9e4066Sahrens 	nvlist_free(config);
42299653d4eSeschrock 	*inuse = ret;
42399653d4eSeschrock 	return (0);
424fa9e4066Sahrens }
425