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