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> 62d8ab6e12SDon 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; 98d8ab6e12SDon 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 111d8ab6e12SDon 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 133d8ab6e12SDon Brady static nvlist_t * 134d8ab6e12SDon Brady refresh_config_libzfs(void *handle, nvlist_t *tryconfig) 13588ecc943SGeorge Wilson { 136d8ab6e12SDon Brady return (refresh_config((libzfs_handle_t *)handle, tryconfig)); 13788ecc943SGeorge Wilson } 13888ecc943SGeorge Wilson 139d8ab6e12SDon Brady static int 140d8ab6e12SDon Brady pool_active_libzfs(void *handle, const char *name, uint64_t guid, 141d8ab6e12SDon Brady boolean_t *isactive) 142fa9e4066Sahrens { 143d8ab6e12SDon Brady return (pool_active((libzfs_handle_t *)handle, name, guid, isactive)); 144fa9e4066Sahrens } 145fa9e4066Sahrens 146d8ab6e12SDon Brady const pool_config_ops_t libzfs_config_ops = { 147d8ab6e12SDon Brady .pco_refresh_config = refresh_config_libzfs, 148d8ab6e12SDon Brady .pco_pool_active = pool_active_libzfs, 149d8ab6e12SDon Brady }; 150d8ab6e12SDon 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; 172*0ac89930SBrian Behlendorf int labels_cleared = 0; 173096d22d4SEric Schrock 174096d22d4SEric Schrock if (fstat64(fd, &statbuf) == -1) 175096d22d4SEric Schrock return (0); 176*0ac89930SBrian Behlendorf 177096d22d4SEric Schrock size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 178096d22d4SEric Schrock 179096d22d4SEric Schrock if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL) 180096d22d4SEric Schrock return (-1); 181096d22d4SEric Schrock 182096d22d4SEric Schrock for (l = 0; l < VDEV_LABELS; l++) { 183*0ac89930SBrian Behlendorf uint64_t state, guid; 184*0ac89930SBrian Behlendorf nvlist_t *config; 185*0ac89930SBrian Behlendorf 186*0ac89930SBrian Behlendorf if (pread64(fd, label, sizeof (vdev_label_t), 187078266a5SMarcel Telka label_offset(size, l)) != sizeof (vdev_label_t)) { 188*0ac89930SBrian Behlendorf continue; 189*0ac89930SBrian Behlendorf } 190*0ac89930SBrian Behlendorf 191*0ac89930SBrian Behlendorf if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 192*0ac89930SBrian Behlendorf sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0) { 193*0ac89930SBrian Behlendorf continue; 194*0ac89930SBrian Behlendorf } 195*0ac89930SBrian Behlendorf 196*0ac89930SBrian Behlendorf /* Skip labels which do not have a valid guid. */ 197*0ac89930SBrian Behlendorf if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 198*0ac89930SBrian Behlendorf &guid) != 0 || guid == 0) { 199*0ac89930SBrian Behlendorf nvlist_free(config); 200*0ac89930SBrian Behlendorf continue; 201*0ac89930SBrian Behlendorf } 202*0ac89930SBrian Behlendorf 203*0ac89930SBrian Behlendorf /* Skip labels which are not in a known valid state. */ 204*0ac89930SBrian Behlendorf if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 205*0ac89930SBrian Behlendorf &state) != 0 || state > POOL_STATE_L2CACHE) { 206*0ac89930SBrian Behlendorf nvlist_free(config); 207*0ac89930SBrian Behlendorf continue; 208*0ac89930SBrian Behlendorf } 209*0ac89930SBrian Behlendorf 210*0ac89930SBrian Behlendorf nvlist_free(config); 211*0ac89930SBrian Behlendorf 212*0ac89930SBrian Behlendorf /* 213*0ac89930SBrian Behlendorf * A valid label was found, overwrite this label's nvlist 214*0ac89930SBrian Behlendorf * and uberblocks with zeros on disk. This is done to prevent 215*0ac89930SBrian Behlendorf * system utilities, like blkid, from incorrectly detecting a 216*0ac89930SBrian Behlendorf * partial label. The leading pad space is left untouched. 217*0ac89930SBrian Behlendorf */ 218*0ac89930SBrian Behlendorf memset(label, 0, sizeof (vdev_label_t)); 219*0ac89930SBrian Behlendorf size_t label_size = sizeof (vdev_label_t) - (2 * VDEV_PAD_SIZE); 220*0ac89930SBrian Behlendorf 221*0ac89930SBrian Behlendorf if (pwrite64(fd, label, label_size, label_offset(size, l) + 222*0ac89930SBrian Behlendorf (2 * VDEV_PAD_SIZE)) == label_size) { 223*0ac89930SBrian Behlendorf labels_cleared++; 224078266a5SMarcel Telka } 225096d22d4SEric Schrock } 226096d22d4SEric Schrock 227096d22d4SEric Schrock free(label); 228*0ac89930SBrian Behlendorf 229*0ac89930SBrian Behlendorf if (labels_cleared == 0) 230*0ac89930SBrian Behlendorf return (-1); 231*0ac89930SBrian Behlendorf 232096d22d4SEric Schrock return (0); 233096d22d4SEric Schrock } 234096d22d4SEric Schrock 23599653d4eSeschrock boolean_t 236fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid) 237fa9e4066Sahrens { 238fa9e4066Sahrens uint64_t tmp; 239fa9e4066Sahrens nvlist_t **child; 240fa9e4066Sahrens uint_t c, children; 241fa9e4066Sahrens 242fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0); 243fa9e4066Sahrens if (tmp == guid) 24499653d4eSeschrock return (B_TRUE); 245fa9e4066Sahrens 246fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 247fa9e4066Sahrens &child, &children) == 0) { 248fa9e4066Sahrens for (c = 0; c < children; c++) 249fa9e4066Sahrens if (find_guid(child[c], guid)) 25099653d4eSeschrock return (B_TRUE); 25199653d4eSeschrock } 25299653d4eSeschrock 25399653d4eSeschrock return (B_FALSE); 25499653d4eSeschrock } 25599653d4eSeschrock 256fa94a07fSbrendan typedef struct aux_cbdata { 257fa94a07fSbrendan const char *cb_type; 25899653d4eSeschrock uint64_t cb_guid; 25999653d4eSeschrock zpool_handle_t *cb_zhp; 260fa94a07fSbrendan } aux_cbdata_t; 26199653d4eSeschrock 26299653d4eSeschrock static int 263fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data) 26499653d4eSeschrock { 265fa94a07fSbrendan aux_cbdata_t *cbp = data; 266fa94a07fSbrendan nvlist_t **list; 267fa94a07fSbrendan uint_t i, count; 26899653d4eSeschrock uint64_t guid; 26999653d4eSeschrock nvlist_t *nvroot; 27099653d4eSeschrock 27199653d4eSeschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 27299653d4eSeschrock &nvroot) == 0); 27399653d4eSeschrock 274fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type, 275fa94a07fSbrendan &list, &count) == 0) { 276fa94a07fSbrendan for (i = 0; i < count; i++) { 277fa94a07fSbrendan verify(nvlist_lookup_uint64(list[i], 27899653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 27999653d4eSeschrock if (guid == cbp->cb_guid) { 28099653d4eSeschrock cbp->cb_zhp = zhp; 28199653d4eSeschrock return (1); 28299653d4eSeschrock } 28399653d4eSeschrock } 284fa9e4066Sahrens } 285fa9e4066Sahrens 28699653d4eSeschrock zpool_close(zhp); 28799653d4eSeschrock return (0); 288fa9e4066Sahrens } 289fa9e4066Sahrens 290fa9e4066Sahrens /* 29199653d4eSeschrock * Determines if the pool is in use. If so, it returns true and the state of 292fa9e4066Sahrens * the pool as well as the name of the pool. Both strings are allocated and 293fa9e4066Sahrens * must be freed by the caller. 294fa9e4066Sahrens */ 295fa9e4066Sahrens int 29699653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr, 29799653d4eSeschrock boolean_t *inuse) 298fa9e4066Sahrens { 299fa9e4066Sahrens nvlist_t *config; 300fa9e4066Sahrens char *name; 30199653d4eSeschrock boolean_t ret; 302fa9e4066Sahrens uint64_t guid, vdev_guid; 303fa9e4066Sahrens zpool_handle_t *zhp; 304fa9e4066Sahrens nvlist_t *pool_config; 30539c23413Seschrock uint64_t stateval, isspare; 306fa94a07fSbrendan aux_cbdata_t cb = { 0 }; 30794de1d4cSeschrock boolean_t isactive; 30899653d4eSeschrock 30999653d4eSeschrock *inuse = B_FALSE; 310fa9e4066Sahrens 311d8ab6e12SDon Brady if (zpool_read_label(fd, &config, NULL) != 0 && errno == ENOMEM) { 31299653d4eSeschrock (void) no_memory(hdl); 31399653d4eSeschrock return (-1); 31499653d4eSeschrock } 31599653d4eSeschrock 31699653d4eSeschrock if (config == NULL) 31799653d4eSeschrock return (0); 318fa9e4066Sahrens 319fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 32046a2abf2Seschrock &stateval) == 0); 321fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 322fa9e4066Sahrens &vdev_guid) == 0); 323fa9e4066Sahrens 324fa94a07fSbrendan if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) { 32599653d4eSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 32699653d4eSeschrock &name) == 0); 32799653d4eSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 32899653d4eSeschrock &guid) == 0); 32999653d4eSeschrock } 33099653d4eSeschrock 33146a2abf2Seschrock switch (stateval) { 332fa9e4066Sahrens case POOL_STATE_EXPORTED: 333f9af39baSGeorge Wilson /* 334f9af39baSGeorge Wilson * A pool with an exported state may in fact be imported 335f9af39baSGeorge Wilson * read-only, so check the in-core state to see if it's 336f9af39baSGeorge Wilson * active and imported read-only. If it is, set 337f9af39baSGeorge Wilson * its state to active. 338f9af39baSGeorge Wilson */ 339f9af39baSGeorge Wilson if (pool_active(hdl, name, guid, &isactive) == 0 && isactive && 340fb13f48fSJosef 'Jeff' Sipek (zhp = zpool_open_canfail(hdl, name)) != NULL) { 341fb13f48fSJosef 'Jeff' Sipek if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL)) 342fb13f48fSJosef 'Jeff' Sipek stateval = POOL_STATE_ACTIVE; 343fb13f48fSJosef 'Jeff' Sipek 344fb13f48fSJosef 'Jeff' Sipek /* 345fb13f48fSJosef 'Jeff' Sipek * All we needed the zpool handle for is the 346fb13f48fSJosef 'Jeff' Sipek * readonly prop check. 347fb13f48fSJosef 'Jeff' Sipek */ 348fb13f48fSJosef 'Jeff' Sipek zpool_close(zhp); 349fb13f48fSJosef 'Jeff' Sipek } 350f9af39baSGeorge Wilson 35199653d4eSeschrock ret = B_TRUE; 352fa9e4066Sahrens break; 353fa9e4066Sahrens 354fa9e4066Sahrens case POOL_STATE_ACTIVE: 355fa9e4066Sahrens /* 356fa9e4066Sahrens * For an active pool, we have to determine if it's really part 357eaca9bbdSeschrock * of a currently active pool (in which case the pool will exist 358eaca9bbdSeschrock * and the guid will be the same), or whether it's part of an 359eaca9bbdSeschrock * active pool that was disconnected without being explicitly 360eaca9bbdSeschrock * exported. 361fa9e4066Sahrens */ 36294de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) { 36394de1d4cSeschrock nvlist_free(config); 36494de1d4cSeschrock return (-1); 36594de1d4cSeschrock } 36694de1d4cSeschrock 36794de1d4cSeschrock if (isactive) { 368fa9e4066Sahrens /* 369fa9e4066Sahrens * Because the device may have been removed while 370fa9e4066Sahrens * offlined, we only report it as active if the vdev is 371fa9e4066Sahrens * still present in the config. Otherwise, pretend like 372fa9e4066Sahrens * it's not in use. 373fa9e4066Sahrens */ 37499653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, name)) != NULL && 375088e9d47Seschrock (pool_config = zpool_get_config(zhp, NULL)) 376088e9d47Seschrock != NULL) { 377fa9e4066Sahrens nvlist_t *nvroot; 378fa9e4066Sahrens 379fa9e4066Sahrens verify(nvlist_lookup_nvlist(pool_config, 380fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 38146a2abf2Seschrock ret = find_guid(nvroot, vdev_guid); 382fa9e4066Sahrens } else { 38399653d4eSeschrock ret = B_FALSE; 384fa9e4066Sahrens } 38599653d4eSeschrock 38639c23413Seschrock /* 38739c23413Seschrock * If this is an active spare within another pool, we 38839c23413Seschrock * treat it like an unused hot spare. This allows the 38939c23413Seschrock * user to create a pool with a hot spare that currently 39039c23413Seschrock * in use within another pool. Since we return B_TRUE, 39139c23413Seschrock * libdiskmgt will continue to prevent generic consumers 39239c23413Seschrock * from using the device. 39339c23413Seschrock */ 39439c23413Seschrock if (ret && nvlist_lookup_uint64(config, 39539c23413Seschrock ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) 39639c23413Seschrock stateval = POOL_STATE_SPARE; 39739c23413Seschrock 39899653d4eSeschrock if (zhp != NULL) 39999653d4eSeschrock zpool_close(zhp); 400fa9e4066Sahrens } else { 40146a2abf2Seschrock stateval = POOL_STATE_POTENTIALLY_ACTIVE; 40299653d4eSeschrock ret = B_TRUE; 40399653d4eSeschrock } 40499653d4eSeschrock break; 40599653d4eSeschrock 40699653d4eSeschrock case POOL_STATE_SPARE: 40799653d4eSeschrock /* 40899653d4eSeschrock * For a hot spare, it can be either definitively in use, or 40999653d4eSeschrock * potentially active. To determine if it's in use, we iterate 41099653d4eSeschrock * over all pools in the system and search for one with a spare 41199653d4eSeschrock * with a matching guid. 41299653d4eSeschrock * 41399653d4eSeschrock * Due to the shared nature of spares, we don't actually report 41499653d4eSeschrock * the potentially active case as in use. This means the user 41599653d4eSeschrock * can freely create pools on the hot spares of exported pools, 41699653d4eSeschrock * but to do otherwise makes the resulting code complicated, and 41799653d4eSeschrock * we end up having to deal with this case anyway. 41899653d4eSeschrock */ 41999653d4eSeschrock cb.cb_zhp = NULL; 42099653d4eSeschrock cb.cb_guid = vdev_guid; 421fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_SPARES; 422fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 423fa94a07fSbrendan name = (char *)zpool_get_name(cb.cb_zhp); 424078266a5SMarcel Telka ret = B_TRUE; 425fa94a07fSbrendan } else { 426078266a5SMarcel Telka ret = B_FALSE; 427fa94a07fSbrendan } 428fa94a07fSbrendan break; 429fa94a07fSbrendan 430fa94a07fSbrendan case POOL_STATE_L2CACHE: 431fa94a07fSbrendan 432fa94a07fSbrendan /* 433fa94a07fSbrendan * Check if any pool is currently using this l2cache device. 434fa94a07fSbrendan */ 435fa94a07fSbrendan cb.cb_zhp = NULL; 436fa94a07fSbrendan cb.cb_guid = vdev_guid; 437fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_L2CACHE; 438fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 43999653d4eSeschrock name = (char *)zpool_get_name(cb.cb_zhp); 440078266a5SMarcel Telka ret = B_TRUE; 44199653d4eSeschrock } else { 442078266a5SMarcel Telka ret = B_FALSE; 443fa9e4066Sahrens } 444fa9e4066Sahrens break; 445fa9e4066Sahrens 446fa9e4066Sahrens default: 44799653d4eSeschrock ret = B_FALSE; 448fa9e4066Sahrens } 449fa9e4066Sahrens 45046a2abf2Seschrock 45146a2abf2Seschrock if (ret) { 45299653d4eSeschrock if ((*namestr = zfs_strdup(hdl, name)) == NULL) { 453c6765aabSeschrock if (cb.cb_zhp) 454c6765aabSeschrock zpool_close(cb.cb_zhp); 45599653d4eSeschrock nvlist_free(config); 45699653d4eSeschrock return (-1); 45799653d4eSeschrock } 45846a2abf2Seschrock *state = (pool_state_t)stateval; 45946a2abf2Seschrock } 46046a2abf2Seschrock 46199653d4eSeschrock if (cb.cb_zhp) 46299653d4eSeschrock zpool_close(cb.cb_zhp); 46399653d4eSeschrock 464fa9e4066Sahrens nvlist_free(config); 46599653d4eSeschrock *inuse = ret; 46699653d4eSeschrock return (0); 467fa9e4066Sahrens } 468