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 */ 21fa9e4066Sahrens /* 22*88ecc943SGeorge Wilson * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens /* 27fa9e4066Sahrens * Pool import support functions. 28fa9e4066Sahrens * 29fa9e4066Sahrens * To import a pool, we rely on reading the configuration information from the 30fa9e4066Sahrens * ZFS label of each device. If we successfully read the label, then we 31fa9e4066Sahrens * organize the configuration information in the following hierarchy: 32fa9e4066Sahrens * 33fa9e4066Sahrens * pool guid -> toplevel vdev guid -> label txg 34fa9e4066Sahrens * 35fa9e4066Sahrens * Duplicate entries matching this same tuple will be discarded. Once we have 36fa9e4066Sahrens * examined every device, we pick the best label txg config for each toplevel 37fa9e4066Sahrens * vdev. We then arrange these toplevel vdevs into a complete pool config, and 38fa9e4066Sahrens * update any paths that have changed. Finally, we attempt to import the pool 39fa9e4066Sahrens * using our derived config, and record the results. 40fa9e4066Sahrens */ 41fa9e4066Sahrens 42fa9e4066Sahrens #include <devid.h> 43fa9e4066Sahrens #include <dirent.h> 44fa9e4066Sahrens #include <errno.h> 45fa9e4066Sahrens #include <libintl.h> 46fa9e4066Sahrens #include <stdlib.h> 47fa9e4066Sahrens #include <string.h> 48fa9e4066Sahrens #include <sys/stat.h> 49fa9e4066Sahrens #include <unistd.h> 50fa9e4066Sahrens #include <fcntl.h> 51fa9e4066Sahrens 52fa9e4066Sahrens #include <sys/vdev_impl.h> 53fa9e4066Sahrens 54fa9e4066Sahrens #include "libzfs.h" 55fa9e4066Sahrens #include "libzfs_impl.h" 56fa9e4066Sahrens 57fa9e4066Sahrens /* 58fa9e4066Sahrens * Intermediate structures used to gather configuration information. 59fa9e4066Sahrens */ 60fa9e4066Sahrens typedef struct config_entry { 61fa9e4066Sahrens uint64_t ce_txg; 62fa9e4066Sahrens nvlist_t *ce_config; 63fa9e4066Sahrens struct config_entry *ce_next; 64fa9e4066Sahrens } config_entry_t; 65fa9e4066Sahrens 66fa9e4066Sahrens typedef struct vdev_entry { 67fa9e4066Sahrens uint64_t ve_guid; 68fa9e4066Sahrens config_entry_t *ve_configs; 69fa9e4066Sahrens struct vdev_entry *ve_next; 70fa9e4066Sahrens } vdev_entry_t; 71fa9e4066Sahrens 72fa9e4066Sahrens typedef struct pool_entry { 73fa9e4066Sahrens uint64_t pe_guid; 74fa9e4066Sahrens vdev_entry_t *pe_vdevs; 75fa9e4066Sahrens struct pool_entry *pe_next; 76fa9e4066Sahrens } pool_entry_t; 77fa9e4066Sahrens 78fa9e4066Sahrens typedef struct name_entry { 7999653d4eSeschrock char *ne_name; 80fa9e4066Sahrens uint64_t ne_guid; 81fa9e4066Sahrens struct name_entry *ne_next; 82fa9e4066Sahrens } name_entry_t; 83fa9e4066Sahrens 84fa9e4066Sahrens typedef struct pool_list { 85fa9e4066Sahrens pool_entry_t *pools; 86fa9e4066Sahrens name_entry_t *names; 87fa9e4066Sahrens } pool_list_t; 88fa9e4066Sahrens 89fa9e4066Sahrens static char * 90fa9e4066Sahrens get_devid(const char *path) 91fa9e4066Sahrens { 92fa9e4066Sahrens int fd; 93fa9e4066Sahrens ddi_devid_t devid; 94fa9e4066Sahrens char *minor, *ret; 95fa9e4066Sahrens 96fa9e4066Sahrens if ((fd = open(path, O_RDONLY)) < 0) 97fa9e4066Sahrens return (NULL); 98fa9e4066Sahrens 99fa9e4066Sahrens minor = NULL; 100fa9e4066Sahrens ret = NULL; 101fa9e4066Sahrens if (devid_get(fd, &devid) == 0) { 102fa9e4066Sahrens if (devid_get_minor_name(fd, &minor) == 0) 103fa9e4066Sahrens ret = devid_str_encode(devid, minor); 104fa9e4066Sahrens if (minor != NULL) 105fa9e4066Sahrens devid_str_free(minor); 106fa9e4066Sahrens devid_free(devid); 107fa9e4066Sahrens } 108c67d9675Seschrock (void) close(fd); 109fa9e4066Sahrens 110fa9e4066Sahrens return (ret); 111fa9e4066Sahrens } 112fa9e4066Sahrens 113fa9e4066Sahrens 114fa9e4066Sahrens /* 115fa9e4066Sahrens * Go through and fix up any path and/or devid information for the given vdev 116fa9e4066Sahrens * configuration. 117fa9e4066Sahrens */ 11899653d4eSeschrock static int 119fa9e4066Sahrens fix_paths(nvlist_t *nv, name_entry_t *names) 120fa9e4066Sahrens { 121fa9e4066Sahrens nvlist_t **child; 122fa9e4066Sahrens uint_t c, children; 123fa9e4066Sahrens uint64_t guid; 124c67d9675Seschrock name_entry_t *ne, *best; 125c67d9675Seschrock char *path, *devid; 126c67d9675Seschrock int matched; 127fa9e4066Sahrens 128fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 129fa9e4066Sahrens &child, &children) == 0) { 130fa9e4066Sahrens for (c = 0; c < children; c++) 13199653d4eSeschrock if (fix_paths(child[c], names) != 0) 13299653d4eSeschrock return (-1); 13399653d4eSeschrock return (0); 134fa9e4066Sahrens } 135fa9e4066Sahrens 136fa9e4066Sahrens /* 137fa9e4066Sahrens * This is a leaf (file or disk) vdev. In either case, go through 138fa9e4066Sahrens * the name list and see if we find a matching guid. If so, replace 139fa9e4066Sahrens * the path and see if we can calculate a new devid. 140c67d9675Seschrock * 141c67d9675Seschrock * There may be multiple names associated with a particular guid, in 142c67d9675Seschrock * which case we have overlapping slices or multiple paths to the same 143c67d9675Seschrock * disk. If this is the case, then we want to pick the path that is 144c67d9675Seschrock * the most similar to the original, where "most similar" is the number 145c67d9675Seschrock * of matching characters starting from the end of the path. This will 146c67d9675Seschrock * preserve slice numbers even if the disks have been reorganized, and 147c67d9675Seschrock * will also catch preferred disk names if multiple paths exist. 148fa9e4066Sahrens */ 149fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); 150c67d9675Seschrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 151c67d9675Seschrock path = NULL; 152c67d9675Seschrock 153c67d9675Seschrock matched = 0; 154c67d9675Seschrock best = NULL; 155c67d9675Seschrock for (ne = names; ne != NULL; ne = ne->ne_next) { 156c67d9675Seschrock if (ne->ne_guid == guid) { 157c67d9675Seschrock const char *src, *dst; 158c67d9675Seschrock int count; 159c67d9675Seschrock 160c67d9675Seschrock if (path == NULL) { 161c67d9675Seschrock best = ne; 162c67d9675Seschrock break; 163c67d9675Seschrock } 164c67d9675Seschrock 165c67d9675Seschrock src = ne->ne_name + strlen(ne->ne_name) - 1; 166c67d9675Seschrock dst = path + strlen(path) - 1; 167c67d9675Seschrock for (count = 0; src >= ne->ne_name && dst >= path; 168c67d9675Seschrock src--, dst--, count++) 169c67d9675Seschrock if (*src != *dst) 170c67d9675Seschrock break; 171c67d9675Seschrock 172c67d9675Seschrock /* 173c67d9675Seschrock * At this point, 'count' is the number of characters 174c67d9675Seschrock * matched from the end. 175c67d9675Seschrock */ 176c67d9675Seschrock if (count > matched || best == NULL) { 177c67d9675Seschrock best = ne; 178c67d9675Seschrock matched = count; 179c67d9675Seschrock } 180c67d9675Seschrock } 181c67d9675Seschrock } 182fa9e4066Sahrens 183c67d9675Seschrock if (best == NULL) 18499653d4eSeschrock return (0); 185fa9e4066Sahrens 18699653d4eSeschrock if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) 18799653d4eSeschrock return (-1); 188fa9e4066Sahrens 189c67d9675Seschrock if ((devid = get_devid(best->ne_name)) == NULL) { 190fa9e4066Sahrens (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID); 191fa9e4066Sahrens } else { 19299653d4eSeschrock if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0) 19399653d4eSeschrock return (-1); 194fa9e4066Sahrens devid_str_free(devid); 195fa9e4066Sahrens } 19699653d4eSeschrock 19799653d4eSeschrock return (0); 198fa9e4066Sahrens } 199fa9e4066Sahrens 200fa9e4066Sahrens /* 201fa9e4066Sahrens * Add the given configuration to the list of known devices. 202fa9e4066Sahrens */ 20399653d4eSeschrock static int 20499653d4eSeschrock add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path, 20599653d4eSeschrock nvlist_t *config) 206fa9e4066Sahrens { 20799653d4eSeschrock uint64_t pool_guid, vdev_guid, top_guid, txg, state; 208fa9e4066Sahrens pool_entry_t *pe; 209fa9e4066Sahrens vdev_entry_t *ve; 210fa9e4066Sahrens config_entry_t *ce; 211fa9e4066Sahrens name_entry_t *ne; 212fa9e4066Sahrens 21399653d4eSeschrock /* 214fa94a07fSbrendan * If this is a hot spare not currently in use or level 2 cache 215fa94a07fSbrendan * device, add it to the list of names to translate, but don't do 216fa94a07fSbrendan * anything else. 21799653d4eSeschrock */ 21899653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 219fa94a07fSbrendan &state) == 0 && 220fa94a07fSbrendan (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && 22199653d4eSeschrock nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 22299653d4eSeschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) 223ccae0b50Seschrock return (-1); 22499653d4eSeschrock 22599653d4eSeschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { 22699653d4eSeschrock free(ne); 22799653d4eSeschrock return (-1); 22899653d4eSeschrock } 22999653d4eSeschrock ne->ne_guid = vdev_guid; 23099653d4eSeschrock ne->ne_next = pl->names; 23199653d4eSeschrock pl->names = ne; 23299653d4eSeschrock return (0); 23399653d4eSeschrock } 23499653d4eSeschrock 235fa9e4066Sahrens /* 236fa9e4066Sahrens * If we have a valid config but cannot read any of these fields, then 237fa9e4066Sahrens * it means we have a half-initialized label. In vdev_label_init() 238fa9e4066Sahrens * we write a label with txg == 0 so that we can identify the device 239fa9e4066Sahrens * in case the user refers to the same disk later on. If we fail to 240fa9e4066Sahrens * create the pool, we'll be left with a label in this state 241fa9e4066Sahrens * which should not be considered part of a valid pool. 242fa9e4066Sahrens */ 243fa9e4066Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 244fa9e4066Sahrens &pool_guid) != 0 || 245fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 246fa9e4066Sahrens &vdev_guid) != 0 || 247fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, 248fa9e4066Sahrens &top_guid) != 0 || 249fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 250fa9e4066Sahrens &txg) != 0 || txg == 0) { 251fa9e4066Sahrens nvlist_free(config); 25299653d4eSeschrock return (0); 253fa9e4066Sahrens } 254fa9e4066Sahrens 255fa9e4066Sahrens /* 256fa9e4066Sahrens * First, see if we know about this pool. If not, then add it to the 257fa9e4066Sahrens * list of known pools. 258fa9e4066Sahrens */ 259fa9e4066Sahrens for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 260fa9e4066Sahrens if (pe->pe_guid == pool_guid) 261fa9e4066Sahrens break; 262fa9e4066Sahrens } 263fa9e4066Sahrens 264fa9e4066Sahrens if (pe == NULL) { 26599653d4eSeschrock if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) { 26699653d4eSeschrock nvlist_free(config); 26799653d4eSeschrock return (-1); 26899653d4eSeschrock } 269fa9e4066Sahrens pe->pe_guid = pool_guid; 270fa9e4066Sahrens pe->pe_next = pl->pools; 271fa9e4066Sahrens pl->pools = pe; 272fa9e4066Sahrens } 273fa9e4066Sahrens 274fa9e4066Sahrens /* 275fa9e4066Sahrens * Second, see if we know about this toplevel vdev. Add it if its 276fa9e4066Sahrens * missing. 277fa9e4066Sahrens */ 278fa9e4066Sahrens for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 279fa9e4066Sahrens if (ve->ve_guid == top_guid) 280fa9e4066Sahrens break; 281fa9e4066Sahrens } 282fa9e4066Sahrens 283fa9e4066Sahrens if (ve == NULL) { 28499653d4eSeschrock if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { 28599653d4eSeschrock nvlist_free(config); 28699653d4eSeschrock return (-1); 28799653d4eSeschrock } 288fa9e4066Sahrens ve->ve_guid = top_guid; 289fa9e4066Sahrens ve->ve_next = pe->pe_vdevs; 290fa9e4066Sahrens pe->pe_vdevs = ve; 291fa9e4066Sahrens } 292fa9e4066Sahrens 293fa9e4066Sahrens /* 294fa9e4066Sahrens * Third, see if we have a config with a matching transaction group. If 295fa9e4066Sahrens * so, then we do nothing. Otherwise, add it to the list of known 296fa9e4066Sahrens * configs. 297fa9e4066Sahrens */ 298fa9e4066Sahrens for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { 299fa9e4066Sahrens if (ce->ce_txg == txg) 300fa9e4066Sahrens break; 301fa9e4066Sahrens } 302fa9e4066Sahrens 303fa9e4066Sahrens if (ce == NULL) { 30499653d4eSeschrock if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) { 30599653d4eSeschrock nvlist_free(config); 30699653d4eSeschrock return (-1); 30799653d4eSeschrock } 308fa9e4066Sahrens ce->ce_txg = txg; 309fa9e4066Sahrens ce->ce_config = config; 310fa9e4066Sahrens ce->ce_next = ve->ve_configs; 311fa9e4066Sahrens ve->ve_configs = ce; 312fa9e4066Sahrens } else { 313fa9e4066Sahrens nvlist_free(config); 314fa9e4066Sahrens } 315fa9e4066Sahrens 316fa9e4066Sahrens /* 317fa9e4066Sahrens * At this point we've successfully added our config to the list of 318fa9e4066Sahrens * known configs. The last thing to do is add the vdev guid -> path 319fa9e4066Sahrens * mappings so that we can fix up the configuration as necessary before 320fa9e4066Sahrens * doing the import. 321fa9e4066Sahrens */ 32299653d4eSeschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) 32399653d4eSeschrock return (-1); 32499653d4eSeschrock 32599653d4eSeschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { 32699653d4eSeschrock free(ne); 32799653d4eSeschrock return (-1); 32899653d4eSeschrock } 329fa9e4066Sahrens 330fa9e4066Sahrens ne->ne_guid = vdev_guid; 331fa9e4066Sahrens ne->ne_next = pl->names; 332fa9e4066Sahrens pl->names = ne; 33399653d4eSeschrock 33499653d4eSeschrock return (0); 335fa9e4066Sahrens } 336fa9e4066Sahrens 337eaca9bbdSeschrock /* 338eaca9bbdSeschrock * Returns true if the named pool matches the given GUID. 339eaca9bbdSeschrock */ 34094de1d4cSeschrock static int 34194de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid, 34294de1d4cSeschrock boolean_t *isactive) 343eaca9bbdSeschrock { 344eaca9bbdSeschrock zpool_handle_t *zhp; 345eaca9bbdSeschrock uint64_t theguid; 346eaca9bbdSeschrock 34794de1d4cSeschrock if (zpool_open_silent(hdl, name, &zhp) != 0) 34894de1d4cSeschrock return (-1); 34994de1d4cSeschrock 35094de1d4cSeschrock if (zhp == NULL) { 35194de1d4cSeschrock *isactive = B_FALSE; 35294de1d4cSeschrock return (0); 35394de1d4cSeschrock } 354eaca9bbdSeschrock 355eaca9bbdSeschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID, 356eaca9bbdSeschrock &theguid) == 0); 357eaca9bbdSeschrock 358eaca9bbdSeschrock zpool_close(zhp); 359eaca9bbdSeschrock 36094de1d4cSeschrock *isactive = (theguid == guid); 36194de1d4cSeschrock return (0); 362eaca9bbdSeschrock } 363eaca9bbdSeschrock 3642f8aaab3Seschrock static nvlist_t * 3652f8aaab3Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config) 3662f8aaab3Seschrock { 3672f8aaab3Seschrock nvlist_t *nvl; 3682f8aaab3Seschrock zfs_cmd_t zc = { 0 }; 3692f8aaab3Seschrock int err; 3702f8aaab3Seschrock 3712f8aaab3Seschrock if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) 3722f8aaab3Seschrock return (NULL); 3732f8aaab3Seschrock 3742f8aaab3Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 3752f8aaab3Seschrock zc.zc_nvlist_conf_size * 2) != 0) { 3762f8aaab3Seschrock zcmd_free_nvlists(&zc); 3772f8aaab3Seschrock return (NULL); 3782f8aaab3Seschrock } 3792f8aaab3Seschrock 3802f8aaab3Seschrock while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT, 3812f8aaab3Seschrock &zc)) != 0 && errno == ENOMEM) { 3822f8aaab3Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 3832f8aaab3Seschrock zcmd_free_nvlists(&zc); 3842f8aaab3Seschrock return (NULL); 3852f8aaab3Seschrock } 3862f8aaab3Seschrock } 3872f8aaab3Seschrock 3882f8aaab3Seschrock if (err) { 3892f8aaab3Seschrock zcmd_free_nvlists(&zc); 3902f8aaab3Seschrock return (NULL); 3912f8aaab3Seschrock } 3922f8aaab3Seschrock 3932f8aaab3Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) { 3942f8aaab3Seschrock zcmd_free_nvlists(&zc); 3952f8aaab3Seschrock return (NULL); 3962f8aaab3Seschrock } 3972f8aaab3Seschrock 3982f8aaab3Seschrock zcmd_free_nvlists(&zc); 3992f8aaab3Seschrock return (nvl); 4002f8aaab3Seschrock } 4012f8aaab3Seschrock 402*88ecc943SGeorge Wilson /* 403*88ecc943SGeorge Wilson * Determine if the vdev id is a hole in the namespace. 404*88ecc943SGeorge Wilson */ 405*88ecc943SGeorge Wilson boolean_t 406*88ecc943SGeorge Wilson vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id) 407*88ecc943SGeorge Wilson { 408*88ecc943SGeorge Wilson for (int c = 0; c < holes; c++) { 409*88ecc943SGeorge Wilson 410*88ecc943SGeorge Wilson /* Top-level is a hole */ 411*88ecc943SGeorge Wilson if (hole_array[c] == id) 412*88ecc943SGeorge Wilson return (B_TRUE); 413*88ecc943SGeorge Wilson } 414*88ecc943SGeorge Wilson return (B_FALSE); 415*88ecc943SGeorge Wilson } 416*88ecc943SGeorge Wilson 417fa9e4066Sahrens /* 418fa9e4066Sahrens * Convert our list of pools into the definitive set of configurations. We 419fa9e4066Sahrens * start by picking the best config for each toplevel vdev. Once that's done, 420fa9e4066Sahrens * we assemble the toplevel vdevs into a full config for the pool. We make a 421fa9e4066Sahrens * pass to fix up any incorrect paths, and then add it to the main list to 422fa9e4066Sahrens * return to the user. 423fa9e4066Sahrens */ 424fa9e4066Sahrens static nvlist_t * 4253a57275aSck get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok) 426fa9e4066Sahrens { 42799653d4eSeschrock pool_entry_t *pe; 42899653d4eSeschrock vdev_entry_t *ve; 42999653d4eSeschrock config_entry_t *ce; 43099653d4eSeschrock nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot; 431fa94a07fSbrendan nvlist_t **spares, **l2cache; 432fa94a07fSbrendan uint_t i, nspares, nl2cache; 43399653d4eSeschrock boolean_t config_seen; 434fa9e4066Sahrens uint64_t best_txg; 43595173954Sek char *name, *hostname; 43699653d4eSeschrock uint64_t version, guid; 43799653d4eSeschrock uint_t children = 0; 43899653d4eSeschrock nvlist_t **child = NULL; 439*88ecc943SGeorge Wilson uint_t holes; 440*88ecc943SGeorge Wilson uint64_t *hole_array, max_id; 44199653d4eSeschrock uint_t c; 44294de1d4cSeschrock boolean_t isactive; 44395173954Sek uint64_t hostid; 4442f8aaab3Seschrock nvlist_t *nvl; 44524e697d4Sck boolean_t found_one = B_FALSE; 446*88ecc943SGeorge Wilson boolean_t valid_top_config = B_FALSE; 447fa9e4066Sahrens 44899653d4eSeschrock if (nvlist_alloc(&ret, 0, 0) != 0) 44999653d4eSeschrock goto nomem; 450fa9e4066Sahrens 45199653d4eSeschrock for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 452*88ecc943SGeorge Wilson uint64_t id, max_txg = 0; 453fa9e4066Sahrens 45499653d4eSeschrock if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 45599653d4eSeschrock goto nomem; 45699653d4eSeschrock config_seen = B_FALSE; 457fa9e4066Sahrens 458fa9e4066Sahrens /* 459fa9e4066Sahrens * Iterate over all toplevel vdevs. Grab the pool configuration 460fa9e4066Sahrens * from the first one we find, and then go through the rest and 461fa9e4066Sahrens * add them as necessary to the 'vdevs' member of the config. 462fa9e4066Sahrens */ 46399653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 464fa9e4066Sahrens 465fa9e4066Sahrens /* 466fa9e4066Sahrens * Determine the best configuration for this vdev by 467fa9e4066Sahrens * selecting the config with the latest transaction 468fa9e4066Sahrens * group. 469fa9e4066Sahrens */ 470fa9e4066Sahrens best_txg = 0; 471fa9e4066Sahrens for (ce = ve->ve_configs; ce != NULL; 472fa9e4066Sahrens ce = ce->ce_next) { 473fa9e4066Sahrens 47499653d4eSeschrock if (ce->ce_txg > best_txg) { 475fa9e4066Sahrens tmp = ce->ce_config; 47699653d4eSeschrock best_txg = ce->ce_txg; 47799653d4eSeschrock } 478fa9e4066Sahrens } 479fa9e4066Sahrens 480*88ecc943SGeorge Wilson /* 481*88ecc943SGeorge Wilson * We rely on the fact that the max txg for the 482*88ecc943SGeorge Wilson * pool will contain the most up-to-date information 483*88ecc943SGeorge Wilson * about the valid top-levels in the vdev namespace. 484*88ecc943SGeorge Wilson */ 485*88ecc943SGeorge Wilson if (best_txg > max_txg) { 486*88ecc943SGeorge Wilson (void) nvlist_remove(config, 487*88ecc943SGeorge Wilson ZPOOL_CONFIG_VDEV_CHILDREN, 488*88ecc943SGeorge Wilson DATA_TYPE_UINT64); 489*88ecc943SGeorge Wilson (void) nvlist_remove(config, 490*88ecc943SGeorge Wilson ZPOOL_CONFIG_HOLE_ARRAY, 491*88ecc943SGeorge Wilson DATA_TYPE_UINT64_ARRAY); 492*88ecc943SGeorge Wilson 493*88ecc943SGeorge Wilson max_txg = best_txg; 494*88ecc943SGeorge Wilson hole_array = NULL; 495*88ecc943SGeorge Wilson holes = 0; 496*88ecc943SGeorge Wilson max_id = 0; 497*88ecc943SGeorge Wilson valid_top_config = B_FALSE; 498*88ecc943SGeorge Wilson 499*88ecc943SGeorge Wilson if (nvlist_lookup_uint64(tmp, 500*88ecc943SGeorge Wilson ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) { 501*88ecc943SGeorge Wilson verify(nvlist_add_uint64(config, 502*88ecc943SGeorge Wilson ZPOOL_CONFIG_VDEV_CHILDREN, 503*88ecc943SGeorge Wilson max_id) == 0); 504*88ecc943SGeorge Wilson valid_top_config = B_TRUE; 505*88ecc943SGeorge Wilson } 506*88ecc943SGeorge Wilson 507*88ecc943SGeorge Wilson if (nvlist_lookup_uint64_array(tmp, 508*88ecc943SGeorge Wilson ZPOOL_CONFIG_HOLE_ARRAY, &hole_array, 509*88ecc943SGeorge Wilson &holes) == 0) { 510*88ecc943SGeorge Wilson verify(nvlist_add_uint64_array(config, 511*88ecc943SGeorge Wilson ZPOOL_CONFIG_HOLE_ARRAY, 512*88ecc943SGeorge Wilson hole_array, holes) == 0); 513*88ecc943SGeorge Wilson } 514*88ecc943SGeorge Wilson } 515*88ecc943SGeorge Wilson 516fa9e4066Sahrens if (!config_seen) { 517fa9e4066Sahrens /* 518fa9e4066Sahrens * Copy the relevant pieces of data to the pool 519fa9e4066Sahrens * configuration: 520fa9e4066Sahrens * 52199653d4eSeschrock * version 522fa9e4066Sahrens * pool guid 523fa9e4066Sahrens * name 524fa9e4066Sahrens * pool state 52595173954Sek * hostid (if available) 52695173954Sek * hostname (if available) 527fa9e4066Sahrens */ 528fa9e4066Sahrens uint64_t state; 529fa9e4066Sahrens 53099653d4eSeschrock verify(nvlist_lookup_uint64(tmp, 53199653d4eSeschrock ZPOOL_CONFIG_VERSION, &version) == 0); 53299653d4eSeschrock if (nvlist_add_uint64(config, 53399653d4eSeschrock ZPOOL_CONFIG_VERSION, version) != 0) 53499653d4eSeschrock goto nomem; 535fa9e4066Sahrens verify(nvlist_lookup_uint64(tmp, 536fa9e4066Sahrens ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 53799653d4eSeschrock if (nvlist_add_uint64(config, 53899653d4eSeschrock ZPOOL_CONFIG_POOL_GUID, guid) != 0) 53999653d4eSeschrock goto nomem; 540fa9e4066Sahrens verify(nvlist_lookup_string(tmp, 541fa9e4066Sahrens ZPOOL_CONFIG_POOL_NAME, &name) == 0); 54299653d4eSeschrock if (nvlist_add_string(config, 54399653d4eSeschrock ZPOOL_CONFIG_POOL_NAME, name) != 0) 54499653d4eSeschrock goto nomem; 545fa9e4066Sahrens verify(nvlist_lookup_uint64(tmp, 546fa9e4066Sahrens ZPOOL_CONFIG_POOL_STATE, &state) == 0); 54799653d4eSeschrock if (nvlist_add_uint64(config, 54899653d4eSeschrock ZPOOL_CONFIG_POOL_STATE, state) != 0) 54999653d4eSeschrock goto nomem; 55095173954Sek hostid = 0; 55195173954Sek if (nvlist_lookup_uint64(tmp, 55295173954Sek ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 55395173954Sek if (nvlist_add_uint64(config, 55495173954Sek ZPOOL_CONFIG_HOSTID, hostid) != 0) 55595173954Sek goto nomem; 55695173954Sek verify(nvlist_lookup_string(tmp, 55795173954Sek ZPOOL_CONFIG_HOSTNAME, 55895173954Sek &hostname) == 0); 55995173954Sek if (nvlist_add_string(config, 56095173954Sek ZPOOL_CONFIG_HOSTNAME, 56195173954Sek hostname) != 0) 56295173954Sek goto nomem; 56395173954Sek } 564fa9e4066Sahrens 56599653d4eSeschrock config_seen = B_TRUE; 566fa9e4066Sahrens } 567fa9e4066Sahrens 568fa9e4066Sahrens /* 569fa9e4066Sahrens * Add this top-level vdev to the child array. 570fa9e4066Sahrens */ 571fa9e4066Sahrens verify(nvlist_lookup_nvlist(tmp, 572fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 573fa9e4066Sahrens verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 574fa9e4066Sahrens &id) == 0); 575*88ecc943SGeorge Wilson 576fa9e4066Sahrens if (id >= children) { 577fa9e4066Sahrens nvlist_t **newchild; 578fa9e4066Sahrens 57999653d4eSeschrock newchild = zfs_alloc(hdl, (id + 1) * 580fa9e4066Sahrens sizeof (nvlist_t *)); 58199653d4eSeschrock if (newchild == NULL) 58299653d4eSeschrock goto nomem; 583fa9e4066Sahrens 584fa9e4066Sahrens for (c = 0; c < children; c++) 585fa9e4066Sahrens newchild[c] = child[c]; 586fa9e4066Sahrens 587fa9e4066Sahrens free(child); 588fa9e4066Sahrens child = newchild; 589fa9e4066Sahrens children = id + 1; 590fa9e4066Sahrens } 59199653d4eSeschrock if (nvlist_dup(nvtop, &child[id], 0) != 0) 59299653d4eSeschrock goto nomem; 593fa9e4066Sahrens 594fa9e4066Sahrens } 595fa9e4066Sahrens 596*88ecc943SGeorge Wilson /* 597*88ecc943SGeorge Wilson * If we have information about all the top-levels then 598*88ecc943SGeorge Wilson * clean up the nvlist which we've constructed. This 599*88ecc943SGeorge Wilson * means removing any extraneous devices that are 600*88ecc943SGeorge Wilson * beyond the valid range or adding devices to the end 601*88ecc943SGeorge Wilson * of our array which appear to be missing. 602*88ecc943SGeorge Wilson */ 603*88ecc943SGeorge Wilson if (valid_top_config) { 604*88ecc943SGeorge Wilson if (max_id < children) { 605*88ecc943SGeorge Wilson for (c = max_id; c < children; c++) 606*88ecc943SGeorge Wilson nvlist_free(child[c]); 607*88ecc943SGeorge Wilson children = max_id; 608*88ecc943SGeorge Wilson } else if (max_id > children) { 609*88ecc943SGeorge Wilson nvlist_t **newchild; 610*88ecc943SGeorge Wilson 611*88ecc943SGeorge Wilson newchild = zfs_alloc(hdl, (max_id) * 612*88ecc943SGeorge Wilson sizeof (nvlist_t *)); 613*88ecc943SGeorge Wilson if (newchild == NULL) 614*88ecc943SGeorge Wilson goto nomem; 615*88ecc943SGeorge Wilson 616*88ecc943SGeorge Wilson for (c = 0; c < children; c++) 617*88ecc943SGeorge Wilson newchild[c] = child[c]; 618*88ecc943SGeorge Wilson 619*88ecc943SGeorge Wilson free(child); 620*88ecc943SGeorge Wilson child = newchild; 621*88ecc943SGeorge Wilson children = max_id; 622*88ecc943SGeorge Wilson } 623*88ecc943SGeorge Wilson } 624*88ecc943SGeorge Wilson 625fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 626fa9e4066Sahrens &guid) == 0); 627fa9e4066Sahrens 628*88ecc943SGeorge Wilson /* 629*88ecc943SGeorge Wilson * The vdev namespace may contain holes as a result of 630*88ecc943SGeorge Wilson * device removal. We must add them back into the vdev 631*88ecc943SGeorge Wilson * tree before we process any missing devices. 632*88ecc943SGeorge Wilson */ 633*88ecc943SGeorge Wilson if (holes > 0) { 634*88ecc943SGeorge Wilson ASSERT(valid_top_config); 635*88ecc943SGeorge Wilson 636*88ecc943SGeorge Wilson for (c = 0; c < children; c++) { 637*88ecc943SGeorge Wilson nvlist_t *holey; 638*88ecc943SGeorge Wilson 639*88ecc943SGeorge Wilson if (child[c] != NULL || 640*88ecc943SGeorge Wilson !vdev_is_hole(hole_array, holes, c)) 641*88ecc943SGeorge Wilson continue; 642*88ecc943SGeorge Wilson 643*88ecc943SGeorge Wilson if (nvlist_alloc(&holey, NV_UNIQUE_NAME, 644*88ecc943SGeorge Wilson 0) != 0) 645*88ecc943SGeorge Wilson goto nomem; 646*88ecc943SGeorge Wilson 647*88ecc943SGeorge Wilson /* 648*88ecc943SGeorge Wilson * Holes in the namespace are treated as 649*88ecc943SGeorge Wilson * "hole" top-level vdevs and have a 650*88ecc943SGeorge Wilson * special flag set on them. 651*88ecc943SGeorge Wilson */ 652*88ecc943SGeorge Wilson if (nvlist_add_string(holey, 653*88ecc943SGeorge Wilson ZPOOL_CONFIG_TYPE, 654*88ecc943SGeorge Wilson VDEV_TYPE_HOLE) != 0 || 655*88ecc943SGeorge Wilson nvlist_add_uint64(holey, 656*88ecc943SGeorge Wilson ZPOOL_CONFIG_ID, c) != 0 || 657*88ecc943SGeorge Wilson nvlist_add_uint64(holey, 658*88ecc943SGeorge Wilson ZPOOL_CONFIG_GUID, 0ULL) != 0) 659*88ecc943SGeorge Wilson goto nomem; 660*88ecc943SGeorge Wilson child[c] = holey; 661*88ecc943SGeorge Wilson } 662*88ecc943SGeorge Wilson } 663*88ecc943SGeorge Wilson 664fa9e4066Sahrens /* 665fa9e4066Sahrens * Look for any missing top-level vdevs. If this is the case, 666fa9e4066Sahrens * create a faked up 'missing' vdev as a placeholder. We cannot 667fa9e4066Sahrens * simply compress the child array, because the kernel performs 668fa9e4066Sahrens * certain checks to make sure the vdev IDs match their location 669fa9e4066Sahrens * in the configuration. 670fa9e4066Sahrens */ 671*88ecc943SGeorge Wilson for (c = 0; c < children; c++) { 672fa9e4066Sahrens if (child[c] == NULL) { 673fa9e4066Sahrens nvlist_t *missing; 67499653d4eSeschrock if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 67599653d4eSeschrock 0) != 0) 67699653d4eSeschrock goto nomem; 67799653d4eSeschrock if (nvlist_add_string(missing, 67899653d4eSeschrock ZPOOL_CONFIG_TYPE, 67999653d4eSeschrock VDEV_TYPE_MISSING) != 0 || 68099653d4eSeschrock nvlist_add_uint64(missing, 68199653d4eSeschrock ZPOOL_CONFIG_ID, c) != 0 || 68299653d4eSeschrock nvlist_add_uint64(missing, 68399653d4eSeschrock ZPOOL_CONFIG_GUID, 0ULL) != 0) { 68499653d4eSeschrock nvlist_free(missing); 68599653d4eSeschrock goto nomem; 68699653d4eSeschrock } 687fa9e4066Sahrens child[c] = missing; 688fa9e4066Sahrens } 689*88ecc943SGeorge Wilson } 690fa9e4066Sahrens 691fa9e4066Sahrens /* 692fa9e4066Sahrens * Put all of this pool's top-level vdevs into a root vdev. 693fa9e4066Sahrens */ 69499653d4eSeschrock if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 69599653d4eSeschrock goto nomem; 69699653d4eSeschrock if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 69799653d4eSeschrock VDEV_TYPE_ROOT) != 0 || 69899653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 69999653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 70099653d4eSeschrock nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 70199653d4eSeschrock child, children) != 0) { 70299653d4eSeschrock nvlist_free(nvroot); 70399653d4eSeschrock goto nomem; 70499653d4eSeschrock } 705fa9e4066Sahrens 706fa9e4066Sahrens for (c = 0; c < children; c++) 707fa9e4066Sahrens nvlist_free(child[c]); 708fa9e4066Sahrens free(child); 70999653d4eSeschrock children = 0; 71099653d4eSeschrock child = NULL; 711fa9e4066Sahrens 712fa9e4066Sahrens /* 713fa9e4066Sahrens * Go through and fix up any paths and/or devids based on our 714fa9e4066Sahrens * known list of vdev GUID -> path mappings. 715fa9e4066Sahrens */ 71699653d4eSeschrock if (fix_paths(nvroot, pl->names) != 0) { 71799653d4eSeschrock nvlist_free(nvroot); 71899653d4eSeschrock goto nomem; 71999653d4eSeschrock } 720fa9e4066Sahrens 721fa9e4066Sahrens /* 722fa9e4066Sahrens * Add the root vdev to this pool's configuration. 723fa9e4066Sahrens */ 72499653d4eSeschrock if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 72599653d4eSeschrock nvroot) != 0) { 72699653d4eSeschrock nvlist_free(nvroot); 72799653d4eSeschrock goto nomem; 72899653d4eSeschrock } 729fa9e4066Sahrens nvlist_free(nvroot); 730fa9e4066Sahrens 7313a57275aSck /* 7323a57275aSck * zdb uses this path to report on active pools that were 7333a57275aSck * imported or created using -R. 7343a57275aSck */ 7353a57275aSck if (active_ok) 7363a57275aSck goto add_pool; 7373a57275aSck 738fa9e4066Sahrens /* 739fa9e4066Sahrens * Determine if this pool is currently active, in which case we 740fa9e4066Sahrens * can't actually import it. 741fa9e4066Sahrens */ 742fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 743fa9e4066Sahrens &name) == 0); 744fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 745fa9e4066Sahrens &guid) == 0); 746fa9e4066Sahrens 74794de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) 74894de1d4cSeschrock goto error; 74994de1d4cSeschrock 7500192a278Seschrock if (isactive) { 751fa9e4066Sahrens nvlist_free(config); 75299653d4eSeschrock config = NULL; 753fa9e4066Sahrens continue; 754fa9e4066Sahrens } 755fa9e4066Sahrens 756*88ecc943SGeorge Wilson if ((nvl = refresh_config(hdl, config)) == NULL) { 757*88ecc943SGeorge Wilson nvlist_free(config); 758*88ecc943SGeorge Wilson config = NULL; 759*88ecc943SGeorge Wilson continue; 760*88ecc943SGeorge Wilson } 761fa9e4066Sahrens 762fa9e4066Sahrens nvlist_free(config); 7632f8aaab3Seschrock config = nvl; 764fa9e4066Sahrens 76599653d4eSeschrock /* 76699653d4eSeschrock * Go through and update the paths for spares, now that we have 76799653d4eSeschrock * them. 76899653d4eSeschrock */ 76999653d4eSeschrock verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 77099653d4eSeschrock &nvroot) == 0); 77199653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 77299653d4eSeschrock &spares, &nspares) == 0) { 77399653d4eSeschrock for (i = 0; i < nspares; i++) { 77499653d4eSeschrock if (fix_paths(spares[i], pl->names) != 0) 77599653d4eSeschrock goto nomem; 77699653d4eSeschrock } 77799653d4eSeschrock } 77899653d4eSeschrock 779fa94a07fSbrendan /* 780fa94a07fSbrendan * Update the paths for l2cache devices. 781fa94a07fSbrendan */ 782fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 783fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 784fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 785fa94a07fSbrendan if (fix_paths(l2cache[i], pl->names) != 0) 786fa94a07fSbrendan goto nomem; 787fa94a07fSbrendan } 788fa94a07fSbrendan } 789fa94a07fSbrendan 79095173954Sek /* 79195173954Sek * Restore the original information read from the actual label. 79295173954Sek */ 79395173954Sek (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, 79495173954Sek DATA_TYPE_UINT64); 79595173954Sek (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, 79695173954Sek DATA_TYPE_STRING); 79795173954Sek if (hostid != 0) { 79895173954Sek verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 79995173954Sek hostid) == 0); 80095173954Sek verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 80195173954Sek hostname) == 0); 80295173954Sek } 80395173954Sek 8043a57275aSck add_pool: 805fa9e4066Sahrens /* 806fa9e4066Sahrens * Add this pool to the list of configs. 807fa9e4066Sahrens */ 808e9dbad6fSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 809e9dbad6fSeschrock &name) == 0); 81099653d4eSeschrock if (nvlist_add_nvlist(ret, name, config) != 0) 81199653d4eSeschrock goto nomem; 812fa9e4066Sahrens 81324e697d4Sck found_one = B_TRUE; 814fa9e4066Sahrens nvlist_free(config); 81599653d4eSeschrock config = NULL; 816fa9e4066Sahrens } 817fa9e4066Sahrens 81824e697d4Sck if (!found_one) { 81924e697d4Sck nvlist_free(ret); 82024e697d4Sck ret = NULL; 82124e697d4Sck } 82224e697d4Sck 823fa9e4066Sahrens return (ret); 82499653d4eSeschrock 82599653d4eSeschrock nomem: 82699653d4eSeschrock (void) no_memory(hdl); 82799653d4eSeschrock error: 82894de1d4cSeschrock nvlist_free(config); 82994de1d4cSeschrock nvlist_free(ret); 83099653d4eSeschrock for (c = 0; c < children; c++) 83199653d4eSeschrock nvlist_free(child[c]); 83294de1d4cSeschrock free(child); 83399653d4eSeschrock 83499653d4eSeschrock return (NULL); 835fa9e4066Sahrens } 836fa9e4066Sahrens 837fa9e4066Sahrens /* 838fa9e4066Sahrens * Return the offset of the given label. 839fa9e4066Sahrens */ 840fa9e4066Sahrens static uint64_t 841e7437265Sahrens label_offset(uint64_t size, int l) 842fa9e4066Sahrens { 843e7437265Sahrens ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); 844fa9e4066Sahrens return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 845fa9e4066Sahrens 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 846fa9e4066Sahrens } 847fa9e4066Sahrens 848fa9e4066Sahrens /* 849fa9e4066Sahrens * Given a file descriptor, read the label information and return an nvlist 850fa9e4066Sahrens * describing the configuration, if there is one. 851fa9e4066Sahrens */ 85299653d4eSeschrock int 85399653d4eSeschrock zpool_read_label(int fd, nvlist_t **config) 854fa9e4066Sahrens { 855fa9e4066Sahrens struct stat64 statbuf; 856fa9e4066Sahrens int l; 857fa9e4066Sahrens vdev_label_t *label; 858e7437265Sahrens uint64_t state, txg, size; 859fa9e4066Sahrens 86099653d4eSeschrock *config = NULL; 86199653d4eSeschrock 862fa9e4066Sahrens if (fstat64(fd, &statbuf) == -1) 86399653d4eSeschrock return (0); 864e7437265Sahrens size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 865fa9e4066Sahrens 86699653d4eSeschrock if ((label = malloc(sizeof (vdev_label_t))) == NULL) 86799653d4eSeschrock return (-1); 868fa9e4066Sahrens 869fa9e4066Sahrens for (l = 0; l < VDEV_LABELS; l++) { 870c5904d13Seschrock if (pread64(fd, label, sizeof (vdev_label_t), 871e7437265Sahrens label_offset(size, l)) != sizeof (vdev_label_t)) 872fa9e4066Sahrens continue; 873fa9e4066Sahrens 874fa9e4066Sahrens if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 87599653d4eSeschrock sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) 876fa9e4066Sahrens continue; 877fa9e4066Sahrens 87899653d4eSeschrock if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 879fa94a07fSbrendan &state) != 0 || state > POOL_STATE_L2CACHE) { 88099653d4eSeschrock nvlist_free(*config); 881fa9e4066Sahrens continue; 882fa9e4066Sahrens } 883fa9e4066Sahrens 884fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 88599653d4eSeschrock (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 88699653d4eSeschrock &txg) != 0 || txg == 0)) { 88799653d4eSeschrock nvlist_free(*config); 888fa9e4066Sahrens continue; 889fa9e4066Sahrens } 890fa9e4066Sahrens 891fa9e4066Sahrens free(label); 89299653d4eSeschrock return (0); 893fa9e4066Sahrens } 894fa9e4066Sahrens 895fa9e4066Sahrens free(label); 89699653d4eSeschrock *config = NULL; 89799653d4eSeschrock return (0); 898fa9e4066Sahrens } 899fa9e4066Sahrens 900fa9e4066Sahrens /* 901fa9e4066Sahrens * Given a list of directories to search, find all pools stored on disk. This 902fa9e4066Sahrens * includes partial pools which are not available to import. If no args are 903fa9e4066Sahrens * given (argc is 0), then the default directory (/dev/dsk) is searched. 90424e697d4Sck * poolname or guid (but not both) are provided by the caller when trying 90524e697d4Sck * to import a specific pool. 906fa9e4066Sahrens */ 90724e697d4Sck static nvlist_t * 90824e697d4Sck zpool_find_import_impl(libzfs_handle_t *hdl, int argc, char **argv, 90924e697d4Sck boolean_t active_ok, char *poolname, uint64_t guid) 910fa9e4066Sahrens { 911fa9e4066Sahrens int i; 912ccae0b50Seschrock DIR *dirp = NULL; 913fa9e4066Sahrens struct dirent64 *dp; 914fa9e4066Sahrens char path[MAXPATHLEN]; 915842c5645Sjwadams char *end; 916842c5645Sjwadams size_t pathleft; 917fa9e4066Sahrens struct stat64 statbuf; 91899653d4eSeschrock nvlist_t *ret = NULL, *config; 919fa9e4066Sahrens static char *default_dir = "/dev/dsk"; 920fa9e4066Sahrens int fd; 921fa9e4066Sahrens pool_list_t pools = { 0 }; 92299653d4eSeschrock pool_entry_t *pe, *penext; 92399653d4eSeschrock vdev_entry_t *ve, *venext; 92499653d4eSeschrock config_entry_t *ce, *cenext; 92599653d4eSeschrock name_entry_t *ne, *nenext; 92699653d4eSeschrock 92724e697d4Sck verify(poolname == NULL || guid == 0); 92824e697d4Sck 929fa9e4066Sahrens if (argc == 0) { 930fa9e4066Sahrens argc = 1; 931fa9e4066Sahrens argv = &default_dir; 932fa9e4066Sahrens } 933fa9e4066Sahrens 934fa9e4066Sahrens /* 935fa9e4066Sahrens * Go through and read the label configuration information from every 936fa9e4066Sahrens * possible device, organizing the information according to pool GUID 937fa9e4066Sahrens * and toplevel GUID. 938fa9e4066Sahrens */ 939fa9e4066Sahrens for (i = 0; i < argc; i++) { 940842c5645Sjwadams char *rdsk; 941842c5645Sjwadams int dfd; 942842c5645Sjwadams 943842c5645Sjwadams /* use realpath to normalize the path */ 944842c5645Sjwadams if (realpath(argv[i], path) == 0) { 945ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 94699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 947fa9e4066Sahrens argv[i]); 94899653d4eSeschrock goto error; 949fa9e4066Sahrens } 950842c5645Sjwadams end = &path[strlen(path)]; 951842c5645Sjwadams *end++ = '/'; 952842c5645Sjwadams *end = 0; 953842c5645Sjwadams pathleft = &path[sizeof (path)] - end; 954842c5645Sjwadams 955842c5645Sjwadams /* 956842c5645Sjwadams * Using raw devices instead of block devices when we're 957842c5645Sjwadams * reading the labels skips a bunch of slow operations during 958842c5645Sjwadams * close(2) processing, so we replace /dev/dsk with /dev/rdsk. 959842c5645Sjwadams */ 960842c5645Sjwadams if (strcmp(path, "/dev/dsk/") == 0) 961842c5645Sjwadams rdsk = "/dev/rdsk/"; 962842c5645Sjwadams else 963842c5645Sjwadams rdsk = path; 964fa9e4066Sahrens 965842c5645Sjwadams if ((dfd = open64(rdsk, O_RDONLY)) < 0 || 966842c5645Sjwadams (dirp = fdopendir(dfd)) == NULL) { 96799653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 968ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 96999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 970842c5645Sjwadams rdsk); 97199653d4eSeschrock goto error; 972fa9e4066Sahrens } 973fa9e4066Sahrens 974fa9e4066Sahrens /* 975fa9e4066Sahrens * This is not MT-safe, but we have no MT consumers of libzfs 976fa9e4066Sahrens */ 977fa9e4066Sahrens while ((dp = readdir64(dirp)) != NULL) { 978842c5645Sjwadams const char *name = dp->d_name; 979842c5645Sjwadams if (name[0] == '.' && 980842c5645Sjwadams (name[1] == 0 || (name[1] == '.' && name[2] == 0))) 981fa9e4066Sahrens continue; 982fa9e4066Sahrens 983842c5645Sjwadams if ((fd = openat64(dfd, name, O_RDONLY)) < 0) 984fa9e4066Sahrens continue; 9853bb79becSeschrock 9863bb79becSeschrock /* 987842c5645Sjwadams * Ignore failed stats. We only want regular 988842c5645Sjwadams * files, character devs and block devs. 9893bb79becSeschrock */ 990842c5645Sjwadams if (fstat64(fd, &statbuf) != 0 || 991842c5645Sjwadams (!S_ISREG(statbuf.st_mode) && 992842c5645Sjwadams !S_ISCHR(statbuf.st_mode) && 993842c5645Sjwadams !S_ISBLK(statbuf.st_mode))) { 994842c5645Sjwadams (void) close(fd); 995fa9e4066Sahrens continue; 996842c5645Sjwadams } 997fa9e4066Sahrens 99899653d4eSeschrock if ((zpool_read_label(fd, &config)) != 0) { 999ccae0b50Seschrock (void) close(fd); 100099653d4eSeschrock (void) no_memory(hdl); 100199653d4eSeschrock goto error; 100299653d4eSeschrock } 1003fa9e4066Sahrens 1004fa9e4066Sahrens (void) close(fd); 1005fa9e4066Sahrens 1006842c5645Sjwadams if (config != NULL) { 100724e697d4Sck boolean_t matched = B_TRUE; 100824e697d4Sck 100924e697d4Sck if (poolname != NULL) { 101024e697d4Sck char *pname; 10117b0e68eaSck 10127b0e68eaSck matched = nvlist_lookup_string(config, 101324e697d4Sck ZPOOL_CONFIG_POOL_NAME, 10147b0e68eaSck &pname) == 0 && 10157b0e68eaSck strcmp(poolname, pname) == 0; 101624e697d4Sck } else if (guid != 0) { 101724e697d4Sck uint64_t this_guid; 1018913f2d80Sck 1019913f2d80Sck matched = nvlist_lookup_uint64(config, 102024e697d4Sck ZPOOL_CONFIG_POOL_GUID, 1021913f2d80Sck &this_guid) == 0 && 1022913f2d80Sck guid == this_guid; 102324e697d4Sck } 102424e697d4Sck if (!matched) { 102524e697d4Sck nvlist_free(config); 102624e697d4Sck config = NULL; 102724e697d4Sck continue; 102824e697d4Sck } 1029842c5645Sjwadams /* use the non-raw path for the config */ 1030842c5645Sjwadams (void) strlcpy(end, name, pathleft); 103199653d4eSeschrock if (add_config(hdl, &pools, path, config) != 0) 103299653d4eSeschrock goto error; 1033842c5645Sjwadams } 1034fa9e4066Sahrens } 1035ccae0b50Seschrock 1036ccae0b50Seschrock (void) closedir(dirp); 1037ccae0b50Seschrock dirp = NULL; 1038fa9e4066Sahrens } 1039fa9e4066Sahrens 10403a57275aSck ret = get_configs(hdl, &pools, active_ok); 104199653d4eSeschrock 104299653d4eSeschrock error: 104399653d4eSeschrock for (pe = pools.pools; pe != NULL; pe = penext) { 104499653d4eSeschrock penext = pe->pe_next; 104599653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 104699653d4eSeschrock venext = ve->ve_next; 104799653d4eSeschrock for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 104899653d4eSeschrock cenext = ce->ce_next; 104999653d4eSeschrock if (ce->ce_config) 105099653d4eSeschrock nvlist_free(ce->ce_config); 105199653d4eSeschrock free(ce); 105299653d4eSeschrock } 105399653d4eSeschrock free(ve); 105499653d4eSeschrock } 105599653d4eSeschrock free(pe); 105699653d4eSeschrock } 105799653d4eSeschrock 105899653d4eSeschrock for (ne = pools.names; ne != NULL; ne = nenext) { 105999653d4eSeschrock nenext = ne->ne_next; 106099653d4eSeschrock if (ne->ne_name) 106199653d4eSeschrock free(ne->ne_name); 106299653d4eSeschrock free(ne); 106399653d4eSeschrock } 106499653d4eSeschrock 1065ccae0b50Seschrock if (dirp) 1066ccae0b50Seschrock (void) closedir(dirp); 1067fa9e4066Sahrens 1068fa9e4066Sahrens return (ret); 1069fa9e4066Sahrens } 1070fa9e4066Sahrens 107124e697d4Sck nvlist_t * 107224e697d4Sck zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv) 107324e697d4Sck { 107424e697d4Sck return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, NULL, 0)); 107524e697d4Sck } 107624e697d4Sck 107724e697d4Sck nvlist_t * 107824e697d4Sck zpool_find_import_byname(libzfs_handle_t *hdl, int argc, char **argv, 107924e697d4Sck char *pool) 108024e697d4Sck { 108124e697d4Sck return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, pool, 0)); 108224e697d4Sck } 108324e697d4Sck 108424e697d4Sck nvlist_t * 108524e697d4Sck zpool_find_import_byguid(libzfs_handle_t *hdl, int argc, char **argv, 108624e697d4Sck uint64_t guid) 108724e697d4Sck { 108824e697d4Sck return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, NULL, guid)); 108924e697d4Sck } 109024e697d4Sck 109124e697d4Sck nvlist_t * 109224e697d4Sck zpool_find_import_activeok(libzfs_handle_t *hdl, int argc, char **argv) 109324e697d4Sck { 109424e697d4Sck return (zpool_find_import_impl(hdl, argc, argv, B_TRUE, NULL, 0)); 109524e697d4Sck } 109624e697d4Sck 10972f8aaab3Seschrock /* 10982f8aaab3Seschrock * Given a cache file, return the contents as a list of importable pools. 109924e697d4Sck * poolname or guid (but not both) are provided by the caller when trying 110024e697d4Sck * to import a specific pool. 11012f8aaab3Seschrock */ 11022f8aaab3Seschrock nvlist_t * 11033a57275aSck zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile, 1104e829d913Sck char *poolname, uint64_t guid) 11052f8aaab3Seschrock { 11062f8aaab3Seschrock char *buf; 11072f8aaab3Seschrock int fd; 11082f8aaab3Seschrock struct stat64 statbuf; 11092f8aaab3Seschrock nvlist_t *raw, *src, *dst; 11102f8aaab3Seschrock nvlist_t *pools; 11112f8aaab3Seschrock nvpair_t *elem; 11122f8aaab3Seschrock char *name; 111324e697d4Sck uint64_t this_guid; 11142f8aaab3Seschrock boolean_t active; 11152f8aaab3Seschrock 111624e697d4Sck verify(poolname == NULL || guid == 0); 111724e697d4Sck 11182f8aaab3Seschrock if ((fd = open(cachefile, O_RDONLY)) < 0) { 11192f8aaab3Seschrock zfs_error_aux(hdl, "%s", strerror(errno)); 11202f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 11212f8aaab3Seschrock dgettext(TEXT_DOMAIN, "failed to open cache file")); 11222f8aaab3Seschrock return (NULL); 11232f8aaab3Seschrock } 11242f8aaab3Seschrock 11252f8aaab3Seschrock if (fstat64(fd, &statbuf) != 0) { 11262f8aaab3Seschrock zfs_error_aux(hdl, "%s", strerror(errno)); 11272f8aaab3Seschrock (void) close(fd); 11282f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 11292f8aaab3Seschrock dgettext(TEXT_DOMAIN, "failed to get size of cache file")); 11302f8aaab3Seschrock return (NULL); 11312f8aaab3Seschrock } 11322f8aaab3Seschrock 11332f8aaab3Seschrock if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) { 11342f8aaab3Seschrock (void) close(fd); 11352f8aaab3Seschrock return (NULL); 11362f8aaab3Seschrock } 11372f8aaab3Seschrock 11382f8aaab3Seschrock if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 11392f8aaab3Seschrock (void) close(fd); 11402f8aaab3Seschrock free(buf); 11412f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 11422f8aaab3Seschrock dgettext(TEXT_DOMAIN, 11432f8aaab3Seschrock "failed to read cache file contents")); 11442f8aaab3Seschrock return (NULL); 11452f8aaab3Seschrock } 11462f8aaab3Seschrock 11472f8aaab3Seschrock (void) close(fd); 11482f8aaab3Seschrock 11492f8aaab3Seschrock if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { 11502f8aaab3Seschrock free(buf); 11512f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 11522f8aaab3Seschrock dgettext(TEXT_DOMAIN, 11532f8aaab3Seschrock "invalid or corrupt cache file contents")); 11542f8aaab3Seschrock return (NULL); 11552f8aaab3Seschrock } 11562f8aaab3Seschrock 11572f8aaab3Seschrock free(buf); 11582f8aaab3Seschrock 11592f8aaab3Seschrock /* 11602f8aaab3Seschrock * Go through and get the current state of the pools and refresh their 11612f8aaab3Seschrock * state. 11622f8aaab3Seschrock */ 11632f8aaab3Seschrock if (nvlist_alloc(&pools, 0, 0) != 0) { 11642f8aaab3Seschrock (void) no_memory(hdl); 11652f8aaab3Seschrock nvlist_free(raw); 11662f8aaab3Seschrock return (NULL); 11672f8aaab3Seschrock } 11682f8aaab3Seschrock 11692f8aaab3Seschrock elem = NULL; 11702f8aaab3Seschrock while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { 11712f8aaab3Seschrock verify(nvpair_value_nvlist(elem, &src) == 0); 11722f8aaab3Seschrock 11732f8aaab3Seschrock verify(nvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME, 11742f8aaab3Seschrock &name) == 0); 117524e697d4Sck if (poolname != NULL && strcmp(poolname, name) != 0) 117624e697d4Sck continue; 117724e697d4Sck 11782f8aaab3Seschrock verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID, 117924e697d4Sck &this_guid) == 0); 118024e697d4Sck if (guid != 0) { 118124e697d4Sck verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID, 118224e697d4Sck &this_guid) == 0); 118324e697d4Sck if (guid != this_guid) 118424e697d4Sck continue; 118524e697d4Sck } 11862f8aaab3Seschrock 1187e829d913Sck if (pool_active(hdl, name, this_guid, &active) != 0) { 1188e829d913Sck nvlist_free(raw); 1189e829d913Sck nvlist_free(pools); 1190e829d913Sck return (NULL); 1191e829d913Sck } 11922f8aaab3Seschrock 1193e829d913Sck if (active) 1194e829d913Sck continue; 11952f8aaab3Seschrock 1196e829d913Sck if ((dst = refresh_config(hdl, src)) == NULL) { 1197e829d913Sck nvlist_free(raw); 1198e829d913Sck nvlist_free(pools); 1199e829d913Sck return (NULL); 1200e829d913Sck } 12012f8aaab3Seschrock 1202e829d913Sck if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { 1203e829d913Sck (void) no_memory(hdl); 12042f8aaab3Seschrock nvlist_free(dst); 1205e829d913Sck nvlist_free(raw); 1206e829d913Sck nvlist_free(pools); 1207e829d913Sck return (NULL); 12082f8aaab3Seschrock } 1209e829d913Sck nvlist_free(dst); 12102f8aaab3Seschrock } 12112f8aaab3Seschrock 12122f8aaab3Seschrock nvlist_free(raw); 12132f8aaab3Seschrock return (pools); 12142f8aaab3Seschrock } 12152f8aaab3Seschrock 12162f8aaab3Seschrock 121799653d4eSeschrock boolean_t 1218fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid) 1219fa9e4066Sahrens { 1220fa9e4066Sahrens uint64_t tmp; 1221fa9e4066Sahrens nvlist_t **child; 1222fa9e4066Sahrens uint_t c, children; 1223fa9e4066Sahrens 1224fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0); 1225fa9e4066Sahrens if (tmp == guid) 122699653d4eSeschrock return (B_TRUE); 1227fa9e4066Sahrens 1228fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1229fa9e4066Sahrens &child, &children) == 0) { 1230fa9e4066Sahrens for (c = 0; c < children; c++) 1231fa9e4066Sahrens if (find_guid(child[c], guid)) 123299653d4eSeschrock return (B_TRUE); 123399653d4eSeschrock } 123499653d4eSeschrock 123599653d4eSeschrock return (B_FALSE); 123699653d4eSeschrock } 123799653d4eSeschrock 1238fa94a07fSbrendan typedef struct aux_cbdata { 1239fa94a07fSbrendan const char *cb_type; 124099653d4eSeschrock uint64_t cb_guid; 124199653d4eSeschrock zpool_handle_t *cb_zhp; 1242fa94a07fSbrendan } aux_cbdata_t; 124399653d4eSeschrock 124499653d4eSeschrock static int 1245fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data) 124699653d4eSeschrock { 1247fa94a07fSbrendan aux_cbdata_t *cbp = data; 1248fa94a07fSbrendan nvlist_t **list; 1249fa94a07fSbrendan uint_t i, count; 125099653d4eSeschrock uint64_t guid; 125199653d4eSeschrock nvlist_t *nvroot; 125299653d4eSeschrock 125399653d4eSeschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 125499653d4eSeschrock &nvroot) == 0); 125599653d4eSeschrock 1256fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type, 1257fa94a07fSbrendan &list, &count) == 0) { 1258fa94a07fSbrendan for (i = 0; i < count; i++) { 1259fa94a07fSbrendan verify(nvlist_lookup_uint64(list[i], 126099653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 126199653d4eSeschrock if (guid == cbp->cb_guid) { 126299653d4eSeschrock cbp->cb_zhp = zhp; 126399653d4eSeschrock return (1); 126499653d4eSeschrock } 126599653d4eSeschrock } 1266fa9e4066Sahrens } 1267fa9e4066Sahrens 126899653d4eSeschrock zpool_close(zhp); 126999653d4eSeschrock return (0); 1270fa9e4066Sahrens } 1271fa9e4066Sahrens 1272fa9e4066Sahrens /* 127399653d4eSeschrock * Determines if the pool is in use. If so, it returns true and the state of 1274fa9e4066Sahrens * the pool as well as the name of the pool. Both strings are allocated and 1275fa9e4066Sahrens * must be freed by the caller. 1276fa9e4066Sahrens */ 1277fa9e4066Sahrens int 127899653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr, 127999653d4eSeschrock boolean_t *inuse) 1280fa9e4066Sahrens { 1281fa9e4066Sahrens nvlist_t *config; 1282fa9e4066Sahrens char *name; 128399653d4eSeschrock boolean_t ret; 1284fa9e4066Sahrens uint64_t guid, vdev_guid; 1285fa9e4066Sahrens zpool_handle_t *zhp; 1286fa9e4066Sahrens nvlist_t *pool_config; 128739c23413Seschrock uint64_t stateval, isspare; 1288fa94a07fSbrendan aux_cbdata_t cb = { 0 }; 128994de1d4cSeschrock boolean_t isactive; 129099653d4eSeschrock 129199653d4eSeschrock *inuse = B_FALSE; 1292fa9e4066Sahrens 129399653d4eSeschrock if (zpool_read_label(fd, &config) != 0) { 129499653d4eSeschrock (void) no_memory(hdl); 129599653d4eSeschrock return (-1); 129699653d4eSeschrock } 129799653d4eSeschrock 129899653d4eSeschrock if (config == NULL) 129999653d4eSeschrock return (0); 1300fa9e4066Sahrens 1301fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 130246a2abf2Seschrock &stateval) == 0); 1303fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 1304fa9e4066Sahrens &vdev_guid) == 0); 1305fa9e4066Sahrens 1306fa94a07fSbrendan if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) { 130799653d4eSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 130899653d4eSeschrock &name) == 0); 130999653d4eSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 131099653d4eSeschrock &guid) == 0); 131199653d4eSeschrock } 131299653d4eSeschrock 131346a2abf2Seschrock switch (stateval) { 1314fa9e4066Sahrens case POOL_STATE_EXPORTED: 131599653d4eSeschrock ret = B_TRUE; 1316fa9e4066Sahrens break; 1317fa9e4066Sahrens 1318fa9e4066Sahrens case POOL_STATE_ACTIVE: 1319fa9e4066Sahrens /* 1320fa9e4066Sahrens * For an active pool, we have to determine if it's really part 1321eaca9bbdSeschrock * of a currently active pool (in which case the pool will exist 1322eaca9bbdSeschrock * and the guid will be the same), or whether it's part of an 1323eaca9bbdSeschrock * active pool that was disconnected without being explicitly 1324eaca9bbdSeschrock * exported. 1325fa9e4066Sahrens */ 132694de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) { 132794de1d4cSeschrock nvlist_free(config); 132894de1d4cSeschrock return (-1); 132994de1d4cSeschrock } 133094de1d4cSeschrock 133194de1d4cSeschrock if (isactive) { 1332fa9e4066Sahrens /* 1333fa9e4066Sahrens * Because the device may have been removed while 1334fa9e4066Sahrens * offlined, we only report it as active if the vdev is 1335fa9e4066Sahrens * still present in the config. Otherwise, pretend like 1336fa9e4066Sahrens * it's not in use. 1337fa9e4066Sahrens */ 133899653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, name)) != NULL && 1339088e9d47Seschrock (pool_config = zpool_get_config(zhp, NULL)) 1340088e9d47Seschrock != NULL) { 1341fa9e4066Sahrens nvlist_t *nvroot; 1342fa9e4066Sahrens 1343fa9e4066Sahrens verify(nvlist_lookup_nvlist(pool_config, 1344fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 134546a2abf2Seschrock ret = find_guid(nvroot, vdev_guid); 1346fa9e4066Sahrens } else { 134799653d4eSeschrock ret = B_FALSE; 1348fa9e4066Sahrens } 134999653d4eSeschrock 135039c23413Seschrock /* 135139c23413Seschrock * If this is an active spare within another pool, we 135239c23413Seschrock * treat it like an unused hot spare. This allows the 135339c23413Seschrock * user to create a pool with a hot spare that currently 135439c23413Seschrock * in use within another pool. Since we return B_TRUE, 135539c23413Seschrock * libdiskmgt will continue to prevent generic consumers 135639c23413Seschrock * from using the device. 135739c23413Seschrock */ 135839c23413Seschrock if (ret && nvlist_lookup_uint64(config, 135939c23413Seschrock ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) 136039c23413Seschrock stateval = POOL_STATE_SPARE; 136139c23413Seschrock 136299653d4eSeschrock if (zhp != NULL) 136399653d4eSeschrock zpool_close(zhp); 1364fa9e4066Sahrens } else { 136546a2abf2Seschrock stateval = POOL_STATE_POTENTIALLY_ACTIVE; 136699653d4eSeschrock ret = B_TRUE; 136799653d4eSeschrock } 136899653d4eSeschrock break; 136999653d4eSeschrock 137099653d4eSeschrock case POOL_STATE_SPARE: 137199653d4eSeschrock /* 137299653d4eSeschrock * For a hot spare, it can be either definitively in use, or 137399653d4eSeschrock * potentially active. To determine if it's in use, we iterate 137499653d4eSeschrock * over all pools in the system and search for one with a spare 137599653d4eSeschrock * with a matching guid. 137699653d4eSeschrock * 137799653d4eSeschrock * Due to the shared nature of spares, we don't actually report 137899653d4eSeschrock * the potentially active case as in use. This means the user 137999653d4eSeschrock * can freely create pools on the hot spares of exported pools, 138099653d4eSeschrock * but to do otherwise makes the resulting code complicated, and 138199653d4eSeschrock * we end up having to deal with this case anyway. 138299653d4eSeschrock */ 138399653d4eSeschrock cb.cb_zhp = NULL; 138499653d4eSeschrock cb.cb_guid = vdev_guid; 1385fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_SPARES; 1386fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 1387fa94a07fSbrendan name = (char *)zpool_get_name(cb.cb_zhp); 1388fa94a07fSbrendan ret = TRUE; 1389fa94a07fSbrendan } else { 1390fa94a07fSbrendan ret = FALSE; 1391fa94a07fSbrendan } 1392fa94a07fSbrendan break; 1393fa94a07fSbrendan 1394fa94a07fSbrendan case POOL_STATE_L2CACHE: 1395fa94a07fSbrendan 1396fa94a07fSbrendan /* 1397fa94a07fSbrendan * Check if any pool is currently using this l2cache device. 1398fa94a07fSbrendan */ 1399fa94a07fSbrendan cb.cb_zhp = NULL; 1400fa94a07fSbrendan cb.cb_guid = vdev_guid; 1401fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_L2CACHE; 1402fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 140399653d4eSeschrock name = (char *)zpool_get_name(cb.cb_zhp); 1404fa9e4066Sahrens ret = TRUE; 140599653d4eSeschrock } else { 140699653d4eSeschrock ret = FALSE; 1407fa9e4066Sahrens } 1408fa9e4066Sahrens break; 1409fa9e4066Sahrens 1410fa9e4066Sahrens default: 141199653d4eSeschrock ret = B_FALSE; 1412fa9e4066Sahrens } 1413fa9e4066Sahrens 141446a2abf2Seschrock 141546a2abf2Seschrock if (ret) { 141699653d4eSeschrock if ((*namestr = zfs_strdup(hdl, name)) == NULL) { 1417c6765aabSeschrock if (cb.cb_zhp) 1418c6765aabSeschrock zpool_close(cb.cb_zhp); 141999653d4eSeschrock nvlist_free(config); 142099653d4eSeschrock return (-1); 142199653d4eSeschrock } 142246a2abf2Seschrock *state = (pool_state_t)stateval; 142346a2abf2Seschrock } 142446a2abf2Seschrock 142599653d4eSeschrock if (cb.cb_zhp) 142699653d4eSeschrock zpool_close(cb.cb_zhp); 142799653d4eSeschrock 1428fa9e4066Sahrens nvlist_free(config); 142999653d4eSeschrock *inuse = ret; 143099653d4eSeschrock return (0); 1431fa9e4066Sahrens } 1432