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 * 36*ddfe901bSsara 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> 62fa9e4066Sahrens 63fa9e4066Sahrens #include "libzfs.h" 64fa9e4066Sahrens #include "libzfs_impl.h" 65fa9e4066Sahrens 66fa9e4066Sahrens /* 67fa9e4066Sahrens * Intermediate structures used to gather configuration information. 68fa9e4066Sahrens */ 69fa9e4066Sahrens typedef struct config_entry { 70fa9e4066Sahrens uint64_t ce_txg; 71fa9e4066Sahrens nvlist_t *ce_config; 72fa9e4066Sahrens struct config_entry *ce_next; 73fa9e4066Sahrens } config_entry_t; 74fa9e4066Sahrens 75fa9e4066Sahrens typedef struct vdev_entry { 76fa9e4066Sahrens uint64_t ve_guid; 77fa9e4066Sahrens config_entry_t *ve_configs; 78fa9e4066Sahrens struct vdev_entry *ve_next; 79fa9e4066Sahrens } vdev_entry_t; 80fa9e4066Sahrens 81fa9e4066Sahrens typedef struct pool_entry { 82fa9e4066Sahrens uint64_t pe_guid; 83fa9e4066Sahrens vdev_entry_t *pe_vdevs; 84fa9e4066Sahrens struct pool_entry *pe_next; 85fa9e4066Sahrens } pool_entry_t; 86fa9e4066Sahrens 87fa9e4066Sahrens typedef struct name_entry { 8899653d4eSeschrock char *ne_name; 89fa9e4066Sahrens uint64_t ne_guid; 90fa9e4066Sahrens struct name_entry *ne_next; 91fa9e4066Sahrens } name_entry_t; 92fa9e4066Sahrens 93fa9e4066Sahrens typedef struct pool_list { 94fa9e4066Sahrens pool_entry_t *pools; 95fa9e4066Sahrens name_entry_t *names; 96fa9e4066Sahrens } pool_list_t; 97fa9e4066Sahrens 98fa9e4066Sahrens /* 99fa9e4066Sahrens * Go through and fix up any path and/or devid information for the given vdev 100fa9e4066Sahrens * configuration. 101fa9e4066Sahrens */ 10299653d4eSeschrock static int 103fa9e4066Sahrens fix_paths(nvlist_t *nv, name_entry_t *names) 104fa9e4066Sahrens { 105fa9e4066Sahrens nvlist_t **child; 106fa9e4066Sahrens uint_t c, children; 107fa9e4066Sahrens uint64_t guid; 108c67d9675Seschrock name_entry_t *ne, *best; 109c67d9675Seschrock char *path, *devid; 110c67d9675Seschrock int matched; 111fa9e4066Sahrens 112fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 113fa9e4066Sahrens &child, &children) == 0) { 114fa9e4066Sahrens for (c = 0; c < children; c++) 11599653d4eSeschrock if (fix_paths(child[c], names) != 0) 11699653d4eSeschrock return (-1); 11799653d4eSeschrock return (0); 118fa9e4066Sahrens } 119fa9e4066Sahrens 120fa9e4066Sahrens /* 121fa9e4066Sahrens * This is a leaf (file or disk) vdev. In either case, go through 122fa9e4066Sahrens * the name list and see if we find a matching guid. If so, replace 123fa9e4066Sahrens * the path and see if we can calculate a new devid. 124c67d9675Seschrock * 125c67d9675Seschrock * There may be multiple names associated with a particular guid, in 126c67d9675Seschrock * which case we have overlapping slices or multiple paths to the same 127c67d9675Seschrock * disk. If this is the case, then we want to pick the path that is 128c67d9675Seschrock * the most similar to the original, where "most similar" is the number 129c67d9675Seschrock * of matching characters starting from the end of the path. This will 130c67d9675Seschrock * preserve slice numbers even if the disks have been reorganized, and 131c67d9675Seschrock * will also catch preferred disk names if multiple paths exist. 132fa9e4066Sahrens */ 133fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); 134c67d9675Seschrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 135c67d9675Seschrock path = NULL; 136c67d9675Seschrock 137c67d9675Seschrock matched = 0; 138c67d9675Seschrock best = NULL; 139c67d9675Seschrock for (ne = names; ne != NULL; ne = ne->ne_next) { 140c67d9675Seschrock if (ne->ne_guid == guid) { 141c67d9675Seschrock const char *src, *dst; 142c67d9675Seschrock int count; 143c67d9675Seschrock 144c67d9675Seschrock if (path == NULL) { 145c67d9675Seschrock best = ne; 146c67d9675Seschrock break; 147c67d9675Seschrock } 148c67d9675Seschrock 149c67d9675Seschrock src = ne->ne_name + strlen(ne->ne_name) - 1; 150c67d9675Seschrock dst = path + strlen(path) - 1; 151c67d9675Seschrock for (count = 0; src >= ne->ne_name && dst >= path; 152c67d9675Seschrock src--, dst--, count++) 153c67d9675Seschrock if (*src != *dst) 154c67d9675Seschrock break; 155c67d9675Seschrock 156c67d9675Seschrock /* 157c67d9675Seschrock * At this point, 'count' is the number of characters 158c67d9675Seschrock * matched from the end. 159c67d9675Seschrock */ 160c67d9675Seschrock if (count > matched || best == NULL) { 161c67d9675Seschrock best = ne; 162c67d9675Seschrock matched = count; 163c67d9675Seschrock } 164c67d9675Seschrock } 165c67d9675Seschrock } 166fa9e4066Sahrens 167c67d9675Seschrock if (best == NULL) 16899653d4eSeschrock return (0); 169fa9e4066Sahrens 17099653d4eSeschrock if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) 17199653d4eSeschrock return (-1); 172fa9e4066Sahrens 17346d46cd4SYuri Pankov if ((devid = devid_str_from_path(best->ne_name)) == NULL) { 174fa9e4066Sahrens (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID); 175fa9e4066Sahrens } else { 176078266a5SMarcel Telka if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0) { 177078266a5SMarcel Telka devid_str_free(devid); 17899653d4eSeschrock return (-1); 179078266a5SMarcel Telka } 180fa9e4066Sahrens devid_str_free(devid); 181fa9e4066Sahrens } 18299653d4eSeschrock 18399653d4eSeschrock return (0); 184fa9e4066Sahrens } 185fa9e4066Sahrens 186fa9e4066Sahrens /* 187fa9e4066Sahrens * Add the given configuration to the list of known devices. 188fa9e4066Sahrens */ 18999653d4eSeschrock static int 19099653d4eSeschrock add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path, 19199653d4eSeschrock nvlist_t *config) 192fa9e4066Sahrens { 19399653d4eSeschrock uint64_t pool_guid, vdev_guid, top_guid, txg, state; 194fa9e4066Sahrens pool_entry_t *pe; 195fa9e4066Sahrens vdev_entry_t *ve; 196fa9e4066Sahrens config_entry_t *ce; 197fa9e4066Sahrens name_entry_t *ne; 198fa9e4066Sahrens 19999653d4eSeschrock /* 200fa94a07fSbrendan * If this is a hot spare not currently in use or level 2 cache 201fa94a07fSbrendan * device, add it to the list of names to translate, but don't do 202fa94a07fSbrendan * anything else. 20399653d4eSeschrock */ 20499653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 205fa94a07fSbrendan &state) == 0 && 206fa94a07fSbrendan (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && 20799653d4eSeschrock nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 20899653d4eSeschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) 209ccae0b50Seschrock return (-1); 21099653d4eSeschrock 21199653d4eSeschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { 21299653d4eSeschrock free(ne); 21399653d4eSeschrock return (-1); 21499653d4eSeschrock } 21510568655SYuri Pankov 21699653d4eSeschrock ne->ne_guid = vdev_guid; 21799653d4eSeschrock ne->ne_next = pl->names; 21899653d4eSeschrock pl->names = ne; 21910568655SYuri Pankov 22099653d4eSeschrock return (0); 22199653d4eSeschrock } 22299653d4eSeschrock 223fa9e4066Sahrens /* 224fa9e4066Sahrens * If we have a valid config but cannot read any of these fields, then 225fa9e4066Sahrens * it means we have a half-initialized label. In vdev_label_init() 226fa9e4066Sahrens * we write a label with txg == 0 so that we can identify the device 227fa9e4066Sahrens * in case the user refers to the same disk later on. If we fail to 228fa9e4066Sahrens * create the pool, we'll be left with a label in this state 229fa9e4066Sahrens * which should not be considered part of a valid pool. 230fa9e4066Sahrens */ 231fa9e4066Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 232fa9e4066Sahrens &pool_guid) != 0 || 233fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 234fa9e4066Sahrens &vdev_guid) != 0 || 235fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, 236fa9e4066Sahrens &top_guid) != 0 || 237fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 238fa9e4066Sahrens &txg) != 0 || txg == 0) { 23999653d4eSeschrock return (0); 240fa9e4066Sahrens } 241fa9e4066Sahrens 242fa9e4066Sahrens /* 243fa9e4066Sahrens * First, see if we know about this pool. If not, then add it to the 244fa9e4066Sahrens * list of known pools. 245fa9e4066Sahrens */ 246fa9e4066Sahrens for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 247fa9e4066Sahrens if (pe->pe_guid == pool_guid) 248fa9e4066Sahrens break; 249fa9e4066Sahrens } 250fa9e4066Sahrens 251fa9e4066Sahrens if (pe == NULL) { 25299653d4eSeschrock if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) { 25399653d4eSeschrock return (-1); 25499653d4eSeschrock } 255fa9e4066Sahrens pe->pe_guid = pool_guid; 256fa9e4066Sahrens pe->pe_next = pl->pools; 257fa9e4066Sahrens pl->pools = pe; 258fa9e4066Sahrens } 259fa9e4066Sahrens 260fa9e4066Sahrens /* 261fa9e4066Sahrens * Second, see if we know about this toplevel vdev. Add it if its 262fa9e4066Sahrens * missing. 263fa9e4066Sahrens */ 264fa9e4066Sahrens for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 265fa9e4066Sahrens if (ve->ve_guid == top_guid) 266fa9e4066Sahrens break; 267fa9e4066Sahrens } 268fa9e4066Sahrens 269fa9e4066Sahrens if (ve == NULL) { 27099653d4eSeschrock if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { 27199653d4eSeschrock return (-1); 27299653d4eSeschrock } 273fa9e4066Sahrens ve->ve_guid = top_guid; 274fa9e4066Sahrens ve->ve_next = pe->pe_vdevs; 275fa9e4066Sahrens pe->pe_vdevs = ve; 276fa9e4066Sahrens } 277fa9e4066Sahrens 278fa9e4066Sahrens /* 279fa9e4066Sahrens * Third, see if we have a config with a matching transaction group. If 280fa9e4066Sahrens * so, then we do nothing. Otherwise, add it to the list of known 281fa9e4066Sahrens * configs. 282fa9e4066Sahrens */ 283fa9e4066Sahrens for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { 284fa9e4066Sahrens if (ce->ce_txg == txg) 285fa9e4066Sahrens break; 286fa9e4066Sahrens } 287fa9e4066Sahrens 288fa9e4066Sahrens if (ce == NULL) { 28999653d4eSeschrock if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) { 29099653d4eSeschrock return (-1); 29199653d4eSeschrock } 292fa9e4066Sahrens ce->ce_txg = txg; 293*ddfe901bSsara hartse ce->ce_config = fnvlist_dup(config); 294fa9e4066Sahrens ce->ce_next = ve->ve_configs; 295fa9e4066Sahrens ve->ve_configs = ce; 296fa9e4066Sahrens } 297fa9e4066Sahrens 298fa9e4066Sahrens /* 299fa9e4066Sahrens * At this point we've successfully added our config to the list of 300fa9e4066Sahrens * known configs. The last thing to do is add the vdev guid -> path 301fa9e4066Sahrens * mappings so that we can fix up the configuration as necessary before 302fa9e4066Sahrens * doing the import. 303fa9e4066Sahrens */ 30499653d4eSeschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) 30599653d4eSeschrock return (-1); 30699653d4eSeschrock 30799653d4eSeschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { 30899653d4eSeschrock free(ne); 30999653d4eSeschrock return (-1); 31099653d4eSeschrock } 311fa9e4066Sahrens 312fa9e4066Sahrens ne->ne_guid = vdev_guid; 313fa9e4066Sahrens ne->ne_next = pl->names; 314fa9e4066Sahrens pl->names = ne; 31599653d4eSeschrock 31699653d4eSeschrock return (0); 317fa9e4066Sahrens } 318fa9e4066Sahrens 319eaca9bbdSeschrock /* 320eaca9bbdSeschrock * Returns true if the named pool matches the given GUID. 321eaca9bbdSeschrock */ 32294de1d4cSeschrock static int 32394de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid, 32494de1d4cSeschrock boolean_t *isactive) 325eaca9bbdSeschrock { 326eaca9bbdSeschrock zpool_handle_t *zhp; 327eaca9bbdSeschrock uint64_t theguid; 328eaca9bbdSeschrock 32994de1d4cSeschrock if (zpool_open_silent(hdl, name, &zhp) != 0) 33094de1d4cSeschrock return (-1); 33194de1d4cSeschrock 33294de1d4cSeschrock if (zhp == NULL) { 33394de1d4cSeschrock *isactive = B_FALSE; 33494de1d4cSeschrock return (0); 33594de1d4cSeschrock } 336eaca9bbdSeschrock 337eaca9bbdSeschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID, 338eaca9bbdSeschrock &theguid) == 0); 339eaca9bbdSeschrock 340eaca9bbdSeschrock zpool_close(zhp); 341eaca9bbdSeschrock 34294de1d4cSeschrock *isactive = (theguid == guid); 34394de1d4cSeschrock return (0); 344eaca9bbdSeschrock } 345eaca9bbdSeschrock 3462f8aaab3Seschrock static nvlist_t * 3472f8aaab3Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config) 3482f8aaab3Seschrock { 3492f8aaab3Seschrock nvlist_t *nvl; 3502f8aaab3Seschrock zfs_cmd_t zc = { 0 }; 3518b65a70bSPavel Zakharov int err, dstbuf_size; 3522f8aaab3Seschrock 3532f8aaab3Seschrock if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) 3542f8aaab3Seschrock return (NULL); 3552f8aaab3Seschrock 3568b65a70bSPavel Zakharov dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4); 3578b65a70bSPavel Zakharov 3588b65a70bSPavel Zakharov if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) { 3592f8aaab3Seschrock zcmd_free_nvlists(&zc); 3602f8aaab3Seschrock return (NULL); 3612f8aaab3Seschrock } 3622f8aaab3Seschrock 3632f8aaab3Seschrock while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT, 3642f8aaab3Seschrock &zc)) != 0 && errno == ENOMEM) { 3652f8aaab3Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 3662f8aaab3Seschrock zcmd_free_nvlists(&zc); 3672f8aaab3Seschrock return (NULL); 3682f8aaab3Seschrock } 3692f8aaab3Seschrock } 3702f8aaab3Seschrock 3712f8aaab3Seschrock if (err) { 3722f8aaab3Seschrock zcmd_free_nvlists(&zc); 3732f8aaab3Seschrock return (NULL); 3742f8aaab3Seschrock } 3752f8aaab3Seschrock 3762f8aaab3Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) { 3772f8aaab3Seschrock zcmd_free_nvlists(&zc); 3782f8aaab3Seschrock return (NULL); 3792f8aaab3Seschrock } 3802f8aaab3Seschrock 3812f8aaab3Seschrock zcmd_free_nvlists(&zc); 3822f8aaab3Seschrock return (nvl); 3832f8aaab3Seschrock } 3842f8aaab3Seschrock 38588ecc943SGeorge Wilson /* 38688ecc943SGeorge Wilson * Determine if the vdev id is a hole in the namespace. 38788ecc943SGeorge Wilson */ 38888ecc943SGeorge Wilson boolean_t 38988ecc943SGeorge Wilson vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id) 39088ecc943SGeorge Wilson { 39188ecc943SGeorge Wilson for (int c = 0; c < holes; c++) { 39288ecc943SGeorge Wilson 39388ecc943SGeorge Wilson /* Top-level is a hole */ 39488ecc943SGeorge Wilson if (hole_array[c] == id) 39588ecc943SGeorge Wilson return (B_TRUE); 39688ecc943SGeorge Wilson } 39788ecc943SGeorge Wilson return (B_FALSE); 39888ecc943SGeorge Wilson } 39988ecc943SGeorge Wilson 400fa9e4066Sahrens /* 401fa9e4066Sahrens * Convert our list of pools into the definitive set of configurations. We 402fa9e4066Sahrens * start by picking the best config for each toplevel vdev. Once that's done, 403fa9e4066Sahrens * we assemble the toplevel vdevs into a full config for the pool. We make a 404fa9e4066Sahrens * pass to fix up any incorrect paths, and then add it to the main list to 405fa9e4066Sahrens * return to the user. 406fa9e4066Sahrens */ 407fa9e4066Sahrens static nvlist_t * 4086f793812SPavel Zakharov get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok, 4096f793812SPavel Zakharov nvlist_t *policy) 410fa9e4066Sahrens { 41199653d4eSeschrock pool_entry_t *pe; 41299653d4eSeschrock vdev_entry_t *ve; 41399653d4eSeschrock config_entry_t *ce; 414f83b46baSPaul Dagnelie nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot; 415fa94a07fSbrendan nvlist_t **spares, **l2cache; 416fa94a07fSbrendan uint_t i, nspares, nl2cache; 41799653d4eSeschrock boolean_t config_seen; 418fa9e4066Sahrens uint64_t best_txg; 419f83b46baSPaul Dagnelie char *name, *hostname = NULL; 420dfbb9432SGeorge Wilson uint64_t guid; 42199653d4eSeschrock uint_t children = 0; 42299653d4eSeschrock nvlist_t **child = NULL; 42388ecc943SGeorge Wilson uint_t holes; 42488ecc943SGeorge Wilson uint64_t *hole_array, max_id; 42599653d4eSeschrock uint_t c; 42694de1d4cSeschrock boolean_t isactive; 42795173954Sek uint64_t hostid; 4282f8aaab3Seschrock nvlist_t *nvl; 42924e697d4Sck boolean_t found_one = B_FALSE; 43088ecc943SGeorge Wilson boolean_t valid_top_config = B_FALSE; 431fa9e4066Sahrens 43299653d4eSeschrock if (nvlist_alloc(&ret, 0, 0) != 0) 43399653d4eSeschrock goto nomem; 434fa9e4066Sahrens 43599653d4eSeschrock for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 43688ecc943SGeorge Wilson uint64_t id, max_txg = 0; 437fa9e4066Sahrens 43899653d4eSeschrock if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 43999653d4eSeschrock goto nomem; 44099653d4eSeschrock config_seen = B_FALSE; 441fa9e4066Sahrens 442fa9e4066Sahrens /* 443fa9e4066Sahrens * Iterate over all toplevel vdevs. Grab the pool configuration 444fa9e4066Sahrens * from the first one we find, and then go through the rest and 445fa9e4066Sahrens * add them as necessary to the 'vdevs' member of the config. 446fa9e4066Sahrens */ 44799653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 448fa9e4066Sahrens 449fa9e4066Sahrens /* 450fa9e4066Sahrens * Determine the best configuration for this vdev by 451fa9e4066Sahrens * selecting the config with the latest transaction 452fa9e4066Sahrens * group. 453fa9e4066Sahrens */ 454fa9e4066Sahrens best_txg = 0; 455fa9e4066Sahrens for (ce = ve->ve_configs; ce != NULL; 456fa9e4066Sahrens ce = ce->ce_next) { 457fa9e4066Sahrens 45899653d4eSeschrock if (ce->ce_txg > best_txg) { 459fa9e4066Sahrens tmp = ce->ce_config; 46099653d4eSeschrock best_txg = ce->ce_txg; 46199653d4eSeschrock } 462fa9e4066Sahrens } 463fa9e4066Sahrens 46488ecc943SGeorge Wilson /* 46588ecc943SGeorge Wilson * We rely on the fact that the max txg for the 46688ecc943SGeorge Wilson * pool will contain the most up-to-date information 46788ecc943SGeorge Wilson * about the valid top-levels in the vdev namespace. 46888ecc943SGeorge Wilson */ 46988ecc943SGeorge Wilson if (best_txg > max_txg) { 47088ecc943SGeorge Wilson (void) nvlist_remove(config, 47188ecc943SGeorge Wilson ZPOOL_CONFIG_VDEV_CHILDREN, 47288ecc943SGeorge Wilson DATA_TYPE_UINT64); 47388ecc943SGeorge Wilson (void) nvlist_remove(config, 47488ecc943SGeorge Wilson ZPOOL_CONFIG_HOLE_ARRAY, 47588ecc943SGeorge Wilson DATA_TYPE_UINT64_ARRAY); 47688ecc943SGeorge Wilson 47788ecc943SGeorge Wilson max_txg = best_txg; 47888ecc943SGeorge Wilson hole_array = NULL; 47988ecc943SGeorge Wilson holes = 0; 48088ecc943SGeorge Wilson max_id = 0; 48188ecc943SGeorge Wilson valid_top_config = B_FALSE; 48288ecc943SGeorge Wilson 48388ecc943SGeorge Wilson if (nvlist_lookup_uint64(tmp, 48488ecc943SGeorge Wilson ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) { 48588ecc943SGeorge Wilson verify(nvlist_add_uint64(config, 48688ecc943SGeorge Wilson ZPOOL_CONFIG_VDEV_CHILDREN, 48788ecc943SGeorge Wilson max_id) == 0); 48888ecc943SGeorge Wilson valid_top_config = B_TRUE; 48988ecc943SGeorge Wilson } 49088ecc943SGeorge Wilson 49188ecc943SGeorge Wilson if (nvlist_lookup_uint64_array(tmp, 49288ecc943SGeorge Wilson ZPOOL_CONFIG_HOLE_ARRAY, &hole_array, 49388ecc943SGeorge Wilson &holes) == 0) { 49488ecc943SGeorge Wilson verify(nvlist_add_uint64_array(config, 49588ecc943SGeorge Wilson ZPOOL_CONFIG_HOLE_ARRAY, 49688ecc943SGeorge Wilson hole_array, holes) == 0); 49788ecc943SGeorge Wilson } 49888ecc943SGeorge Wilson } 49988ecc943SGeorge Wilson 500fa9e4066Sahrens if (!config_seen) { 501fa9e4066Sahrens /* 502fa9e4066Sahrens * Copy the relevant pieces of data to the pool 503fa9e4066Sahrens * configuration: 504fa9e4066Sahrens * 50599653d4eSeschrock * version 506dfbb9432SGeorge Wilson * pool guid 507dfbb9432SGeorge Wilson * name 5088704186eSDan McDonald * comment (if available) 509dfbb9432SGeorge Wilson * pool state 51095173954Sek * hostid (if available) 51195173954Sek * hostname (if available) 512fa9e4066Sahrens */ 513bda88194SGeorge Wilson uint64_t state, version; 514dfbb9432SGeorge Wilson char *comment = NULL; 515dfbb9432SGeorge Wilson 516dfbb9432SGeorge Wilson version = fnvlist_lookup_uint64(tmp, 517dfbb9432SGeorge Wilson ZPOOL_CONFIG_VERSION); 518dfbb9432SGeorge Wilson fnvlist_add_uint64(config, 519dfbb9432SGeorge Wilson ZPOOL_CONFIG_VERSION, version); 520dfbb9432SGeorge Wilson guid = fnvlist_lookup_uint64(tmp, 521dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_GUID); 522dfbb9432SGeorge Wilson fnvlist_add_uint64(config, 523dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_GUID, guid); 524dfbb9432SGeorge Wilson name = fnvlist_lookup_string(tmp, 525dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_NAME); 526dfbb9432SGeorge Wilson fnvlist_add_string(config, 527dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_NAME, name); 528dfbb9432SGeorge Wilson 5298704186eSDan McDonald if (nvlist_lookup_string(tmp, 530dfbb9432SGeorge Wilson ZPOOL_CONFIG_COMMENT, &comment) == 0) 531dfbb9432SGeorge Wilson fnvlist_add_string(config, 532dfbb9432SGeorge Wilson ZPOOL_CONFIG_COMMENT, comment); 533dfbb9432SGeorge Wilson 534dfbb9432SGeorge Wilson state = fnvlist_lookup_uint64(tmp, 535dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_STATE); 536dfbb9432SGeorge Wilson fnvlist_add_uint64(config, 537dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_STATE, state); 5388704186eSDan McDonald 53995173954Sek hostid = 0; 54095173954Sek if (nvlist_lookup_uint64(tmp, 54195173954Sek ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 542dfbb9432SGeorge Wilson fnvlist_add_uint64(config, 543dfbb9432SGeorge Wilson ZPOOL_CONFIG_HOSTID, hostid); 544dfbb9432SGeorge Wilson hostname = fnvlist_lookup_string(tmp, 545dfbb9432SGeorge Wilson ZPOOL_CONFIG_HOSTNAME); 546dfbb9432SGeorge Wilson fnvlist_add_string(config, 547dfbb9432SGeorge Wilson ZPOOL_CONFIG_HOSTNAME, hostname); 54895173954Sek } 549fa9e4066Sahrens 55099653d4eSeschrock config_seen = B_TRUE; 551fa9e4066Sahrens } 552fa9e4066Sahrens 553fa9e4066Sahrens /* 554fa9e4066Sahrens * Add this top-level vdev to the child array. 555fa9e4066Sahrens */ 556fa9e4066Sahrens verify(nvlist_lookup_nvlist(tmp, 557fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 558fa9e4066Sahrens verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 559fa9e4066Sahrens &id) == 0); 56088ecc943SGeorge Wilson 561fa9e4066Sahrens if (id >= children) { 562fa9e4066Sahrens nvlist_t **newchild; 563fa9e4066Sahrens 56499653d4eSeschrock newchild = zfs_alloc(hdl, (id + 1) * 565fa9e4066Sahrens sizeof (nvlist_t *)); 56699653d4eSeschrock if (newchild == NULL) 56799653d4eSeschrock goto nomem; 568fa9e4066Sahrens 569fa9e4066Sahrens for (c = 0; c < children; c++) 570fa9e4066Sahrens newchild[c] = child[c]; 571fa9e4066Sahrens 572fa9e4066Sahrens free(child); 573fa9e4066Sahrens child = newchild; 574fa9e4066Sahrens children = id + 1; 575fa9e4066Sahrens } 57699653d4eSeschrock if (nvlist_dup(nvtop, &child[id], 0) != 0) 57799653d4eSeschrock goto nomem; 578fa9e4066Sahrens 579fa9e4066Sahrens } 580fa9e4066Sahrens 58188ecc943SGeorge Wilson /* 58288ecc943SGeorge Wilson * If we have information about all the top-levels then 58388ecc943SGeorge Wilson * clean up the nvlist which we've constructed. This 58488ecc943SGeorge Wilson * means removing any extraneous devices that are 58588ecc943SGeorge Wilson * beyond the valid range or adding devices to the end 58688ecc943SGeorge Wilson * of our array which appear to be missing. 58788ecc943SGeorge Wilson */ 58888ecc943SGeorge Wilson if (valid_top_config) { 58988ecc943SGeorge Wilson if (max_id < children) { 59088ecc943SGeorge Wilson for (c = max_id; c < children; c++) 59188ecc943SGeorge Wilson nvlist_free(child[c]); 59288ecc943SGeorge Wilson children = max_id; 59388ecc943SGeorge Wilson } else if (max_id > children) { 59488ecc943SGeorge Wilson nvlist_t **newchild; 59588ecc943SGeorge Wilson 59688ecc943SGeorge Wilson newchild = zfs_alloc(hdl, (max_id) * 59788ecc943SGeorge Wilson sizeof (nvlist_t *)); 59888ecc943SGeorge Wilson if (newchild == NULL) 59988ecc943SGeorge Wilson goto nomem; 60088ecc943SGeorge Wilson 60188ecc943SGeorge Wilson for (c = 0; c < children; c++) 60288ecc943SGeorge Wilson newchild[c] = child[c]; 60388ecc943SGeorge Wilson 60488ecc943SGeorge Wilson free(child); 60588ecc943SGeorge Wilson child = newchild; 60688ecc943SGeorge Wilson children = max_id; 60788ecc943SGeorge Wilson } 60888ecc943SGeorge Wilson } 60988ecc943SGeorge Wilson 610fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 611fa9e4066Sahrens &guid) == 0); 612fa9e4066Sahrens 61388ecc943SGeorge Wilson /* 61488ecc943SGeorge Wilson * The vdev namespace may contain holes as a result of 61588ecc943SGeorge Wilson * device removal. We must add them back into the vdev 61688ecc943SGeorge Wilson * tree before we process any missing devices. 61788ecc943SGeorge Wilson */ 61888ecc943SGeorge Wilson if (holes > 0) { 61988ecc943SGeorge Wilson ASSERT(valid_top_config); 62088ecc943SGeorge Wilson 62188ecc943SGeorge Wilson for (c = 0; c < children; c++) { 62288ecc943SGeorge Wilson nvlist_t *holey; 62388ecc943SGeorge Wilson 62488ecc943SGeorge Wilson if (child[c] != NULL || 62588ecc943SGeorge Wilson !vdev_is_hole(hole_array, holes, c)) 62688ecc943SGeorge Wilson continue; 62788ecc943SGeorge Wilson 62888ecc943SGeorge Wilson if (nvlist_alloc(&holey, NV_UNIQUE_NAME, 62988ecc943SGeorge Wilson 0) != 0) 63088ecc943SGeorge Wilson goto nomem; 63188ecc943SGeorge Wilson 63288ecc943SGeorge Wilson /* 63388ecc943SGeorge Wilson * Holes in the namespace are treated as 63488ecc943SGeorge Wilson * "hole" top-level vdevs and have a 63588ecc943SGeorge Wilson * special flag set on them. 63688ecc943SGeorge Wilson */ 63788ecc943SGeorge Wilson if (nvlist_add_string(holey, 63888ecc943SGeorge Wilson ZPOOL_CONFIG_TYPE, 63988ecc943SGeorge Wilson VDEV_TYPE_HOLE) != 0 || 64088ecc943SGeorge Wilson nvlist_add_uint64(holey, 64188ecc943SGeorge Wilson ZPOOL_CONFIG_ID, c) != 0 || 64288ecc943SGeorge Wilson nvlist_add_uint64(holey, 643078266a5SMarcel Telka ZPOOL_CONFIG_GUID, 0ULL) != 0) { 644078266a5SMarcel Telka nvlist_free(holey); 64588ecc943SGeorge Wilson goto nomem; 646078266a5SMarcel Telka } 64788ecc943SGeorge Wilson child[c] = holey; 64888ecc943SGeorge Wilson } 64988ecc943SGeorge Wilson } 65088ecc943SGeorge Wilson 651fa9e4066Sahrens /* 652fa9e4066Sahrens * Look for any missing top-level vdevs. If this is the case, 653fa9e4066Sahrens * create a faked up 'missing' vdev as a placeholder. We cannot 654fa9e4066Sahrens * simply compress the child array, because the kernel performs 655fa9e4066Sahrens * certain checks to make sure the vdev IDs match their location 656fa9e4066Sahrens * in the configuration. 657fa9e4066Sahrens */ 65888ecc943SGeorge Wilson for (c = 0; c < children; c++) { 659fa9e4066Sahrens if (child[c] == NULL) { 660fa9e4066Sahrens nvlist_t *missing; 66199653d4eSeschrock if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 66299653d4eSeschrock 0) != 0) 66399653d4eSeschrock goto nomem; 66499653d4eSeschrock if (nvlist_add_string(missing, 66599653d4eSeschrock ZPOOL_CONFIG_TYPE, 66699653d4eSeschrock VDEV_TYPE_MISSING) != 0 || 66799653d4eSeschrock nvlist_add_uint64(missing, 66899653d4eSeschrock ZPOOL_CONFIG_ID, c) != 0 || 66999653d4eSeschrock nvlist_add_uint64(missing, 67099653d4eSeschrock ZPOOL_CONFIG_GUID, 0ULL) != 0) { 67199653d4eSeschrock nvlist_free(missing); 67299653d4eSeschrock goto nomem; 67399653d4eSeschrock } 674fa9e4066Sahrens child[c] = missing; 675fa9e4066Sahrens } 67688ecc943SGeorge Wilson } 677fa9e4066Sahrens 678fa9e4066Sahrens /* 679fa9e4066Sahrens * Put all of this pool's top-level vdevs into a root vdev. 680fa9e4066Sahrens */ 68199653d4eSeschrock if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 68299653d4eSeschrock goto nomem; 68399653d4eSeschrock if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 68499653d4eSeschrock VDEV_TYPE_ROOT) != 0 || 68599653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 68699653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 68799653d4eSeschrock nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 68899653d4eSeschrock child, children) != 0) { 68999653d4eSeschrock nvlist_free(nvroot); 69099653d4eSeschrock goto nomem; 69199653d4eSeschrock } 692fa9e4066Sahrens 693fa9e4066Sahrens for (c = 0; c < children; c++) 694fa9e4066Sahrens nvlist_free(child[c]); 695fa9e4066Sahrens free(child); 69699653d4eSeschrock children = 0; 69799653d4eSeschrock child = NULL; 698fa9e4066Sahrens 699fa9e4066Sahrens /* 700fa9e4066Sahrens * Go through and fix up any paths and/or devids based on our 701fa9e4066Sahrens * known list of vdev GUID -> path mappings. 702fa9e4066Sahrens */ 70399653d4eSeschrock if (fix_paths(nvroot, pl->names) != 0) { 70499653d4eSeschrock nvlist_free(nvroot); 70599653d4eSeschrock goto nomem; 70699653d4eSeschrock } 707fa9e4066Sahrens 708fa9e4066Sahrens /* 709fa9e4066Sahrens * Add the root vdev to this pool's configuration. 710fa9e4066Sahrens */ 71199653d4eSeschrock if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 71299653d4eSeschrock nvroot) != 0) { 71399653d4eSeschrock nvlist_free(nvroot); 71499653d4eSeschrock goto nomem; 71599653d4eSeschrock } 716fa9e4066Sahrens nvlist_free(nvroot); 717fa9e4066Sahrens 7183a57275aSck /* 7193a57275aSck * zdb uses this path to report on active pools that were 7203a57275aSck * imported or created using -R. 7213a57275aSck */ 7223a57275aSck if (active_ok) 7233a57275aSck goto add_pool; 7243a57275aSck 725fa9e4066Sahrens /* 726fa9e4066Sahrens * Determine if this pool is currently active, in which case we 727fa9e4066Sahrens * can't actually import it. 728fa9e4066Sahrens */ 729fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 730fa9e4066Sahrens &name) == 0); 731fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 732fa9e4066Sahrens &guid) == 0); 733fa9e4066Sahrens 73494de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) 73594de1d4cSeschrock goto error; 73694de1d4cSeschrock 7370192a278Seschrock if (isactive) { 738fa9e4066Sahrens nvlist_free(config); 73999653d4eSeschrock config = NULL; 740fa9e4066Sahrens continue; 741fa9e4066Sahrens } 742fa9e4066Sahrens 7436f793812SPavel Zakharov if (policy != NULL) { 7445dafeea3SPavel Zakharov if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, 7456f793812SPavel Zakharov policy) != 0) 7466f793812SPavel Zakharov goto nomem; 7476f793812SPavel Zakharov } 7486f793812SPavel Zakharov 74988ecc943SGeorge Wilson if ((nvl = refresh_config(hdl, config)) == NULL) { 75088ecc943SGeorge Wilson nvlist_free(config); 75188ecc943SGeorge Wilson config = NULL; 75288ecc943SGeorge Wilson continue; 75388ecc943SGeorge Wilson } 754fa9e4066Sahrens 755fa9e4066Sahrens nvlist_free(config); 7562f8aaab3Seschrock config = nvl; 757fa9e4066Sahrens 75899653d4eSeschrock /* 75999653d4eSeschrock * Go through and update the paths for spares, now that we have 76099653d4eSeschrock * them. 76199653d4eSeschrock */ 76299653d4eSeschrock verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 76399653d4eSeschrock &nvroot) == 0); 76499653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 76599653d4eSeschrock &spares, &nspares) == 0) { 76699653d4eSeschrock for (i = 0; i < nspares; i++) { 76799653d4eSeschrock if (fix_paths(spares[i], pl->names) != 0) 76899653d4eSeschrock goto nomem; 76999653d4eSeschrock } 77099653d4eSeschrock } 77199653d4eSeschrock 772fa94a07fSbrendan /* 773fa94a07fSbrendan * Update the paths for l2cache devices. 774fa94a07fSbrendan */ 775fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 776fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 777fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 778fa94a07fSbrendan if (fix_paths(l2cache[i], pl->names) != 0) 779fa94a07fSbrendan goto nomem; 780fa94a07fSbrendan } 781fa94a07fSbrendan } 782fa94a07fSbrendan 78395173954Sek /* 78495173954Sek * Restore the original information read from the actual label. 78595173954Sek */ 78695173954Sek (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, 78795173954Sek DATA_TYPE_UINT64); 78895173954Sek (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, 78995173954Sek DATA_TYPE_STRING); 79095173954Sek if (hostid != 0) { 79195173954Sek verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 79295173954Sek hostid) == 0); 79395173954Sek verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 79495173954Sek hostname) == 0); 79595173954Sek } 79695173954Sek 7973a57275aSck add_pool: 798fa9e4066Sahrens /* 799fa9e4066Sahrens * Add this pool to the list of configs. 800fa9e4066Sahrens */ 801e9dbad6fSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 802e9dbad6fSeschrock &name) == 0); 80399653d4eSeschrock if (nvlist_add_nvlist(ret, name, config) != 0) 80499653d4eSeschrock goto nomem; 805fa9e4066Sahrens 80624e697d4Sck found_one = B_TRUE; 807fa9e4066Sahrens nvlist_free(config); 80899653d4eSeschrock config = NULL; 809fa9e4066Sahrens } 810fa9e4066Sahrens 81124e697d4Sck if (!found_one) { 81224e697d4Sck nvlist_free(ret); 81324e697d4Sck ret = NULL; 81424e697d4Sck } 81524e697d4Sck 816fa9e4066Sahrens return (ret); 81799653d4eSeschrock 81899653d4eSeschrock nomem: 81999653d4eSeschrock (void) no_memory(hdl); 82099653d4eSeschrock error: 82194de1d4cSeschrock nvlist_free(config); 82294de1d4cSeschrock nvlist_free(ret); 82399653d4eSeschrock for (c = 0; c < children; c++) 82499653d4eSeschrock nvlist_free(child[c]); 82594de1d4cSeschrock free(child); 82699653d4eSeschrock 82799653d4eSeschrock return (NULL); 828fa9e4066Sahrens } 829fa9e4066Sahrens 830fa9e4066Sahrens /* 831fa9e4066Sahrens * Return the offset of the given label. 832fa9e4066Sahrens */ 833fa9e4066Sahrens static uint64_t 834e7437265Sahrens label_offset(uint64_t size, int l) 835fa9e4066Sahrens { 836e7437265Sahrens ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); 837fa9e4066Sahrens return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 838fa9e4066Sahrens 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 839fa9e4066Sahrens } 840fa9e4066Sahrens 841fa9e4066Sahrens /* 842fa9e4066Sahrens * Given a file descriptor, read the label information and return an nvlist 843fa9e4066Sahrens * describing the configuration, if there is one. 844c861bfbdSAlan Somers * Return 0 on success, or -1 on failure 845fa9e4066Sahrens */ 84699653d4eSeschrock int 84799653d4eSeschrock zpool_read_label(int fd, nvlist_t **config) 848fa9e4066Sahrens { 849fa9e4066Sahrens struct stat64 statbuf; 850fa9e4066Sahrens int l; 851fa9e4066Sahrens vdev_label_t *label; 852e7437265Sahrens uint64_t state, txg, size; 853fa9e4066Sahrens 85499653d4eSeschrock *config = NULL; 85599653d4eSeschrock 856fa9e4066Sahrens if (fstat64(fd, &statbuf) == -1) 857c861bfbdSAlan Somers return (-1); 858e7437265Sahrens size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 859fa9e4066Sahrens 86099653d4eSeschrock if ((label = malloc(sizeof (vdev_label_t))) == NULL) 86199653d4eSeschrock return (-1); 862fa9e4066Sahrens 863fa9e4066Sahrens for (l = 0; l < VDEV_LABELS; l++) { 864c5904d13Seschrock if (pread64(fd, label, sizeof (vdev_label_t), 865e7437265Sahrens label_offset(size, l)) != sizeof (vdev_label_t)) 866fa9e4066Sahrens continue; 867fa9e4066Sahrens 868fa9e4066Sahrens if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 86999653d4eSeschrock sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) 870fa9e4066Sahrens continue; 871fa9e4066Sahrens 87299653d4eSeschrock if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 873fa94a07fSbrendan &state) != 0 || state > POOL_STATE_L2CACHE) { 87499653d4eSeschrock nvlist_free(*config); 875fa9e4066Sahrens continue; 876fa9e4066Sahrens } 877fa9e4066Sahrens 878fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 87999653d4eSeschrock (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 88099653d4eSeschrock &txg) != 0 || txg == 0)) { 88199653d4eSeschrock nvlist_free(*config); 882fa9e4066Sahrens continue; 883fa9e4066Sahrens } 884fa9e4066Sahrens 885fa9e4066Sahrens free(label); 88699653d4eSeschrock return (0); 887fa9e4066Sahrens } 888fa9e4066Sahrens 889fa9e4066Sahrens free(label); 89099653d4eSeschrock *config = NULL; 891ae5ee1bdSAlan Somers errno = ENOENT; 892c861bfbdSAlan Somers return (-1); 893fa9e4066Sahrens } 894fa9e4066Sahrens 8954f67d755SEric Taylor typedef struct rdsk_node { 8964f67d755SEric Taylor char *rn_name; 8974f67d755SEric Taylor int rn_dfd; 8984f67d755SEric Taylor libzfs_handle_t *rn_hdl; 8994f67d755SEric Taylor nvlist_t *rn_config; 9004f67d755SEric Taylor avl_tree_t *rn_avl; 9014f67d755SEric Taylor avl_node_t rn_node; 9024f67d755SEric Taylor boolean_t rn_nozpool; 9034f67d755SEric Taylor } rdsk_node_t; 9044f67d755SEric Taylor 9054f67d755SEric Taylor static int 9064f67d755SEric Taylor slice_cache_compare(const void *arg1, const void *arg2) 9074f67d755SEric Taylor { 9084f67d755SEric Taylor const char *nm1 = ((rdsk_node_t *)arg1)->rn_name; 9094f67d755SEric Taylor const char *nm2 = ((rdsk_node_t *)arg2)->rn_name; 9104f67d755SEric Taylor char *nm1slice, *nm2slice; 9114f67d755SEric Taylor int rv; 9124f67d755SEric Taylor 9134f67d755SEric Taylor /* 9144f67d755SEric Taylor * slices zero and two are the most likely to provide results, 9154f67d755SEric Taylor * so put those first 9164f67d755SEric Taylor */ 9174f67d755SEric Taylor nm1slice = strstr(nm1, "s0"); 9184f67d755SEric Taylor nm2slice = strstr(nm2, "s0"); 9194f67d755SEric Taylor if (nm1slice && !nm2slice) { 9204f67d755SEric Taylor return (-1); 9214f67d755SEric Taylor } 9224f67d755SEric Taylor if (!nm1slice && nm2slice) { 9234f67d755SEric Taylor return (1); 9244f67d755SEric Taylor } 9254f67d755SEric Taylor nm1slice = strstr(nm1, "s2"); 9264f67d755SEric Taylor nm2slice = strstr(nm2, "s2"); 9274f67d755SEric Taylor if (nm1slice && !nm2slice) { 9284f67d755SEric Taylor return (-1); 9294f67d755SEric Taylor } 9304f67d755SEric Taylor if (!nm1slice && nm2slice) { 9314f67d755SEric Taylor return (1); 9324f67d755SEric Taylor } 9334f67d755SEric Taylor 9344f67d755SEric Taylor rv = strcmp(nm1, nm2); 9354f67d755SEric Taylor if (rv == 0) 9364f67d755SEric Taylor return (0); 9374f67d755SEric Taylor return (rv > 0 ? 1 : -1); 9384f67d755SEric Taylor } 9394f67d755SEric Taylor 9404f67d755SEric Taylor static void 9414f67d755SEric Taylor check_one_slice(avl_tree_t *r, char *diskname, uint_t partno, 9424f67d755SEric Taylor diskaddr_t size, uint_t blksz) 9434f67d755SEric Taylor { 9444f67d755SEric Taylor rdsk_node_t tmpnode; 9454f67d755SEric Taylor rdsk_node_t *node; 9464f67d755SEric Taylor char sname[MAXNAMELEN]; 9474f67d755SEric Taylor 9484f67d755SEric Taylor tmpnode.rn_name = &sname[0]; 9494f67d755SEric Taylor (void) snprintf(tmpnode.rn_name, MAXNAMELEN, "%s%u", 9504f67d755SEric Taylor diskname, partno); 95116d2251eSEric Taylor /* 95216d2251eSEric Taylor * protect against division by zero for disk labels that 95316d2251eSEric Taylor * contain a bogus sector size 95416d2251eSEric Taylor */ 95516d2251eSEric Taylor if (blksz == 0) 95616d2251eSEric Taylor blksz = DEV_BSIZE; 9574f67d755SEric Taylor /* too small to contain a zpool? */ 9584f67d755SEric Taylor if ((size < (SPA_MINDEVSIZE / blksz)) && 9594f67d755SEric Taylor (node = avl_find(r, &tmpnode, NULL))) 9604f67d755SEric Taylor node->rn_nozpool = B_TRUE; 9614f67d755SEric Taylor } 9624f67d755SEric Taylor 9634f67d755SEric Taylor static void 9644f67d755SEric Taylor nozpool_all_slices(avl_tree_t *r, const char *sname) 9654f67d755SEric Taylor { 9664f67d755SEric Taylor char diskname[MAXNAMELEN]; 9674f67d755SEric Taylor char *ptr; 9684f67d755SEric Taylor int i; 9694f67d755SEric Taylor 9704f67d755SEric Taylor (void) strncpy(diskname, sname, MAXNAMELEN); 9714f67d755SEric Taylor if (((ptr = strrchr(diskname, 's')) == NULL) && 9724f67d755SEric Taylor ((ptr = strrchr(diskname, 'p')) == NULL)) 9734f67d755SEric Taylor return; 9744f67d755SEric Taylor ptr[0] = 's'; 9754f67d755SEric Taylor ptr[1] = '\0'; 9764f67d755SEric Taylor for (i = 0; i < NDKMAP; i++) 9774f67d755SEric Taylor check_one_slice(r, diskname, i, 0, 1); 9784f67d755SEric Taylor ptr[0] = 'p'; 9794f67d755SEric Taylor for (i = 0; i <= FD_NUMPART; i++) 9804f67d755SEric Taylor check_one_slice(r, diskname, i, 0, 1); 9814f67d755SEric Taylor } 9824f67d755SEric Taylor 9834f67d755SEric Taylor static void 9844f67d755SEric Taylor check_slices(avl_tree_t *r, int fd, const char *sname) 9854f67d755SEric Taylor { 9864f67d755SEric Taylor struct extvtoc vtoc; 9874f67d755SEric Taylor struct dk_gpt *gpt; 9884f67d755SEric Taylor char diskname[MAXNAMELEN]; 9894f67d755SEric Taylor char *ptr; 9904f67d755SEric Taylor int i; 9914f67d755SEric Taylor 9924f67d755SEric Taylor (void) strncpy(diskname, sname, MAXNAMELEN); 9934f67d755SEric Taylor if ((ptr = strrchr(diskname, 's')) == NULL || !isdigit(ptr[1])) 9944f67d755SEric Taylor return; 9954f67d755SEric Taylor ptr[1] = '\0'; 9964f67d755SEric Taylor 9974f67d755SEric Taylor if (read_extvtoc(fd, &vtoc) >= 0) { 9984f67d755SEric Taylor for (i = 0; i < NDKMAP; i++) 9994f67d755SEric Taylor check_one_slice(r, diskname, i, 10004f67d755SEric Taylor vtoc.v_part[i].p_size, vtoc.v_sectorsz); 10014f67d755SEric Taylor } else if (efi_alloc_and_read(fd, &gpt) >= 0) { 10024f67d755SEric Taylor /* 10034f67d755SEric Taylor * on x86 we'll still have leftover links that point 10044f67d755SEric Taylor * to slices s[9-15], so use NDKMAP instead 10054f67d755SEric Taylor */ 10064f67d755SEric Taylor for (i = 0; i < NDKMAP; i++) 10074f67d755SEric Taylor check_one_slice(r, diskname, i, 10084f67d755SEric Taylor gpt->efi_parts[i].p_size, gpt->efi_lbasize); 10094f67d755SEric Taylor /* nodes p[1-4] are never used with EFI labels */ 10104f67d755SEric Taylor ptr[0] = 'p'; 10114f67d755SEric Taylor for (i = 1; i <= FD_NUMPART; i++) 10124f67d755SEric Taylor check_one_slice(r, diskname, i, 0, 1); 10134f67d755SEric Taylor efi_free(gpt); 10144f67d755SEric Taylor } 10154f67d755SEric Taylor } 10164f67d755SEric Taylor 10174f67d755SEric Taylor static void 10184f67d755SEric Taylor zpool_open_func(void *arg) 10194f67d755SEric Taylor { 10204f67d755SEric Taylor rdsk_node_t *rn = arg; 10214f67d755SEric Taylor struct stat64 statbuf; 10224f67d755SEric Taylor nvlist_t *config; 10234f67d755SEric Taylor int fd; 10244f67d755SEric Taylor 10254f67d755SEric Taylor if (rn->rn_nozpool) 10264f67d755SEric Taylor return; 10274f67d755SEric Taylor if ((fd = openat64(rn->rn_dfd, rn->rn_name, O_RDONLY)) < 0) { 10284f67d755SEric Taylor /* symlink to a device that's no longer there */ 10294f67d755SEric Taylor if (errno == ENOENT) 10304f67d755SEric Taylor nozpool_all_slices(rn->rn_avl, rn->rn_name); 10314f67d755SEric Taylor return; 10324f67d755SEric Taylor } 10334f67d755SEric Taylor /* 10344f67d755SEric Taylor * Ignore failed stats. We only want regular 10354f67d755SEric Taylor * files, character devs and block devs. 10364f67d755SEric Taylor */ 10374f67d755SEric Taylor if (fstat64(fd, &statbuf) != 0 || 10384f67d755SEric Taylor (!S_ISREG(statbuf.st_mode) && 10394f67d755SEric Taylor !S_ISCHR(statbuf.st_mode) && 10404f67d755SEric Taylor !S_ISBLK(statbuf.st_mode))) { 10414f67d755SEric Taylor (void) close(fd); 10424f67d755SEric Taylor return; 10434f67d755SEric Taylor } 10444f67d755SEric Taylor /* this file is too small to hold a zpool */ 10454f67d755SEric Taylor if (S_ISREG(statbuf.st_mode) && 10464f67d755SEric Taylor statbuf.st_size < SPA_MINDEVSIZE) { 10474f67d755SEric Taylor (void) close(fd); 10484f67d755SEric Taylor return; 10494f67d755SEric Taylor } else if (!S_ISREG(statbuf.st_mode)) { 10504f67d755SEric Taylor /* 10514f67d755SEric Taylor * Try to read the disk label first so we don't have to 10524f67d755SEric Taylor * open a bunch of minor nodes that can't have a zpool. 10534f67d755SEric Taylor */ 10544f67d755SEric Taylor check_slices(rn->rn_avl, fd, rn->rn_name); 10554f67d755SEric Taylor } 10564f67d755SEric Taylor 1057c861bfbdSAlan Somers if ((zpool_read_label(fd, &config)) != 0 && errno == ENOMEM) { 10584f67d755SEric Taylor (void) close(fd); 10594f67d755SEric Taylor (void) no_memory(rn->rn_hdl); 10604f67d755SEric Taylor return; 10614f67d755SEric Taylor } 10624f67d755SEric Taylor (void) close(fd); 10634f67d755SEric Taylor 10644f67d755SEric Taylor rn->rn_config = config; 10654f67d755SEric Taylor } 10664f67d755SEric Taylor 1067096d22d4SEric Schrock /* 10686401734dSWill Andrews * Given a file descriptor, clear (zero) the label information. 1069096d22d4SEric Schrock */ 1070096d22d4SEric Schrock int 1071096d22d4SEric Schrock zpool_clear_label(int fd) 1072096d22d4SEric Schrock { 1073096d22d4SEric Schrock struct stat64 statbuf; 1074096d22d4SEric Schrock int l; 1075096d22d4SEric Schrock vdev_label_t *label; 1076096d22d4SEric Schrock uint64_t size; 1077096d22d4SEric Schrock 1078096d22d4SEric Schrock if (fstat64(fd, &statbuf) == -1) 1079096d22d4SEric Schrock return (0); 1080096d22d4SEric Schrock size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 1081096d22d4SEric Schrock 1082096d22d4SEric Schrock if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL) 1083096d22d4SEric Schrock return (-1); 1084096d22d4SEric Schrock 1085096d22d4SEric Schrock for (l = 0; l < VDEV_LABELS; l++) { 1086096d22d4SEric Schrock if (pwrite64(fd, label, sizeof (vdev_label_t), 1087078266a5SMarcel Telka label_offset(size, l)) != sizeof (vdev_label_t)) { 1088078266a5SMarcel Telka free(label); 1089096d22d4SEric Schrock return (-1); 1090078266a5SMarcel Telka } 1091096d22d4SEric Schrock } 1092096d22d4SEric Schrock 1093096d22d4SEric Schrock free(label); 1094096d22d4SEric Schrock return (0); 1095096d22d4SEric Schrock } 1096096d22d4SEric Schrock 1097fa9e4066Sahrens /* 1098fa9e4066Sahrens * Given a list of directories to search, find all pools stored on disk. This 1099fa9e4066Sahrens * includes partial pools which are not available to import. If no args are 1100fa9e4066Sahrens * given (argc is 0), then the default directory (/dev/dsk) is searched. 110124e697d4Sck * poolname or guid (but not both) are provided by the caller when trying 110224e697d4Sck * to import a specific pool. 1103fa9e4066Sahrens */ 110424e697d4Sck static nvlist_t * 1105d41c4376SMark J Musante zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg) 1106fa9e4066Sahrens { 1107d41c4376SMark J Musante int i, dirs = iarg->paths; 1108fa9e4066Sahrens struct dirent64 *dp; 1109fa9e4066Sahrens char path[MAXPATHLEN]; 1110d41c4376SMark J Musante char *end, **dir = iarg->path; 1111842c5645Sjwadams size_t pathleft; 11124f67d755SEric Taylor nvlist_t *ret = NULL; 11136401734dSWill Andrews static char *default_dir = ZFS_DISK_ROOT; 1114fa9e4066Sahrens pool_list_t pools = { 0 }; 111599653d4eSeschrock pool_entry_t *pe, *penext; 111699653d4eSeschrock vdev_entry_t *ve, *venext; 111799653d4eSeschrock config_entry_t *ce, *cenext; 111899653d4eSeschrock name_entry_t *ne, *nenext; 11194f67d755SEric Taylor avl_tree_t slice_cache; 11204f67d755SEric Taylor rdsk_node_t *slice; 11214f67d755SEric Taylor void *cookie; 112299653d4eSeschrock 1123d41c4376SMark J Musante if (dirs == 0) { 1124d41c4376SMark J Musante dirs = 1; 1125d41c4376SMark J Musante dir = &default_dir; 1126fa9e4066Sahrens } 1127fa9e4066Sahrens 1128fa9e4066Sahrens /* 1129fa9e4066Sahrens * Go through and read the label configuration information from every 1130fa9e4066Sahrens * possible device, organizing the information according to pool GUID 1131fa9e4066Sahrens * and toplevel GUID. 1132fa9e4066Sahrens */ 1133d41c4376SMark J Musante for (i = 0; i < dirs; i++) { 11344f67d755SEric Taylor tpool_t *t; 11356401734dSWill Andrews char rdsk[MAXPATHLEN]; 1136842c5645Sjwadams int dfd; 1137078266a5SMarcel Telka boolean_t config_failed = B_FALSE; 1138078266a5SMarcel Telka DIR *dirp; 1139842c5645Sjwadams 1140842c5645Sjwadams /* use realpath to normalize the path */ 1141d41c4376SMark J Musante if (realpath(dir[i], path) == 0) { 1142ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 1143d41c4376SMark J Musante dgettext(TEXT_DOMAIN, "cannot open '%s'"), dir[i]); 114499653d4eSeschrock goto error; 1145fa9e4066Sahrens } 1146842c5645Sjwadams end = &path[strlen(path)]; 1147842c5645Sjwadams *end++ = '/'; 1148842c5645Sjwadams *end = 0; 1149842c5645Sjwadams pathleft = &path[sizeof (path)] - end; 1150842c5645Sjwadams 1151842c5645Sjwadams /* 1152842c5645Sjwadams * Using raw devices instead of block devices when we're 1153842c5645Sjwadams * reading the labels skips a bunch of slow operations during 1154842c5645Sjwadams * close(2) processing, so we replace /dev/dsk with /dev/rdsk. 1155842c5645Sjwadams */ 11566401734dSWill Andrews if (strcmp(path, ZFS_DISK_ROOTD) == 0) 11576401734dSWill Andrews (void) strlcpy(rdsk, ZFS_RDISK_ROOTD, sizeof (rdsk)); 1158842c5645Sjwadams else 11596401734dSWill Andrews (void) strlcpy(rdsk, path, sizeof (rdsk)); 1160fa9e4066Sahrens 1161842c5645Sjwadams if ((dfd = open64(rdsk, O_RDONLY)) < 0 || 1162842c5645Sjwadams (dirp = fdopendir(dfd)) == NULL) { 1163078266a5SMarcel Telka if (dfd >= 0) 1164078266a5SMarcel Telka (void) close(dfd); 116599653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 1166ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 116799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 1168842c5645Sjwadams rdsk); 116999653d4eSeschrock goto error; 1170fa9e4066Sahrens } 1171fa9e4066Sahrens 11724f67d755SEric Taylor avl_create(&slice_cache, slice_cache_compare, 11734f67d755SEric Taylor sizeof (rdsk_node_t), offsetof(rdsk_node_t, rn_node)); 1174fa9e4066Sahrens /* 1175fa9e4066Sahrens * This is not MT-safe, but we have no MT consumers of libzfs 1176fa9e4066Sahrens */ 1177fa9e4066Sahrens while ((dp = readdir64(dirp)) != NULL) { 1178842c5645Sjwadams const char *name = dp->d_name; 1179842c5645Sjwadams if (name[0] == '.' && 1180842c5645Sjwadams (name[1] == 0 || (name[1] == '.' && name[2] == 0))) 1181fa9e4066Sahrens continue; 1182fa9e4066Sahrens 11834f67d755SEric Taylor slice = zfs_alloc(hdl, sizeof (rdsk_node_t)); 11844f67d755SEric Taylor slice->rn_name = zfs_strdup(hdl, name); 11854f67d755SEric Taylor slice->rn_avl = &slice_cache; 11864f67d755SEric Taylor slice->rn_dfd = dfd; 11874f67d755SEric Taylor slice->rn_hdl = hdl; 11884f67d755SEric Taylor slice->rn_nozpool = B_FALSE; 11894f67d755SEric Taylor avl_add(&slice_cache, slice); 11904f67d755SEric Taylor } 11914f67d755SEric Taylor /* 11924f67d755SEric Taylor * create a thread pool to do all of this in parallel; 11934f67d755SEric Taylor * rn_nozpool is not protected, so this is racy in that 11944f67d755SEric Taylor * multiple tasks could decide that the same slice can 11954f67d755SEric Taylor * not hold a zpool, which is benign. Also choose 11964f67d755SEric Taylor * double the number of processors; we hold a lot of 11974f67d755SEric Taylor * locks in the kernel, so going beyond this doesn't 11984f67d755SEric Taylor * buy us much. 11994f67d755SEric Taylor */ 12004f67d755SEric Taylor t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 12014f67d755SEric Taylor 0, NULL); 12024f67d755SEric Taylor for (slice = avl_first(&slice_cache); slice; 12034f67d755SEric Taylor (slice = avl_walk(&slice_cache, slice, 12044f67d755SEric Taylor AVL_AFTER))) 12054f67d755SEric Taylor (void) tpool_dispatch(t, zpool_open_func, slice); 12064f67d755SEric Taylor tpool_wait(t); 12074f67d755SEric Taylor tpool_destroy(t); 12084f67d755SEric Taylor 12094f67d755SEric Taylor cookie = NULL; 12104f67d755SEric Taylor while ((slice = avl_destroy_nodes(&slice_cache, 12114f67d755SEric Taylor &cookie)) != NULL) { 1212078266a5SMarcel Telka if (slice->rn_config != NULL && !config_failed) { 12134f67d755SEric Taylor nvlist_t *config = slice->rn_config; 121424e697d4Sck boolean_t matched = B_TRUE; 121524e697d4Sck 1216d41c4376SMark J Musante if (iarg->poolname != NULL) { 121724e697d4Sck char *pname; 12187b0e68eaSck 12197b0e68eaSck matched = nvlist_lookup_string(config, 122024e697d4Sck ZPOOL_CONFIG_POOL_NAME, 12217b0e68eaSck &pname) == 0 && 1222d41c4376SMark J Musante strcmp(iarg->poolname, pname) == 0; 1223d41c4376SMark J Musante } else if (iarg->guid != 0) { 122424e697d4Sck uint64_t this_guid; 1225913f2d80Sck 1226913f2d80Sck matched = nvlist_lookup_uint64(config, 122724e697d4Sck ZPOOL_CONFIG_POOL_GUID, 1228913f2d80Sck &this_guid) == 0 && 1229d41c4376SMark J Musante iarg->guid == this_guid; 123024e697d4Sck } 1231*ddfe901bSsara hartse if (matched) { 1232078266a5SMarcel Telka /* 1233078266a5SMarcel Telka * use the non-raw path for the config 1234078266a5SMarcel Telka */ 1235078266a5SMarcel Telka (void) strlcpy(end, slice->rn_name, 1236078266a5SMarcel Telka pathleft); 1237078266a5SMarcel Telka if (add_config(hdl, &pools, path, 1238078266a5SMarcel Telka config) != 0) 1239078266a5SMarcel Telka config_failed = B_TRUE; 124024e697d4Sck } 1241*ddfe901bSsara hartse nvlist_free(config); 1242842c5645Sjwadams } 12434f67d755SEric Taylor free(slice->rn_name); 12444f67d755SEric Taylor free(slice); 1245fa9e4066Sahrens } 12464f67d755SEric Taylor avl_destroy(&slice_cache); 1247ccae0b50Seschrock 1248ccae0b50Seschrock (void) closedir(dirp); 1249078266a5SMarcel Telka 1250078266a5SMarcel Telka if (config_failed) 1251078266a5SMarcel Telka goto error; 1252fa9e4066Sahrens } 1253fa9e4066Sahrens 12546f793812SPavel Zakharov ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy); 125599653d4eSeschrock 125699653d4eSeschrock error: 125799653d4eSeschrock for (pe = pools.pools; pe != NULL; pe = penext) { 125899653d4eSeschrock penext = pe->pe_next; 125999653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 126099653d4eSeschrock venext = ve->ve_next; 126199653d4eSeschrock for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 126299653d4eSeschrock cenext = ce->ce_next; 1263aab83bb8SJosef 'Jeff' Sipek nvlist_free(ce->ce_config); 126499653d4eSeschrock free(ce); 126599653d4eSeschrock } 126699653d4eSeschrock free(ve); 126799653d4eSeschrock } 126899653d4eSeschrock free(pe); 126999653d4eSeschrock } 127099653d4eSeschrock 127199653d4eSeschrock for (ne = pools.names; ne != NULL; ne = nenext) { 127299653d4eSeschrock nenext = ne->ne_next; 1273078266a5SMarcel Telka free(ne->ne_name); 127499653d4eSeschrock free(ne); 127599653d4eSeschrock } 127699653d4eSeschrock 1277fa9e4066Sahrens return (ret); 1278fa9e4066Sahrens } 1279fa9e4066Sahrens 128024e697d4Sck nvlist_t * 128124e697d4Sck zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv) 128224e697d4Sck { 1283d41c4376SMark J Musante importargs_t iarg = { 0 }; 128424e697d4Sck 1285d41c4376SMark J Musante iarg.paths = argc; 1286d41c4376SMark J Musante iarg.path = argv; 128724e697d4Sck 1288d41c4376SMark J Musante return (zpool_find_import_impl(hdl, &iarg)); 128924e697d4Sck } 129024e697d4Sck 12912f8aaab3Seschrock /* 12922f8aaab3Seschrock * Given a cache file, return the contents as a list of importable pools. 129324e697d4Sck * poolname or guid (but not both) are provided by the caller when trying 129424e697d4Sck * to import a specific pool. 12952f8aaab3Seschrock */ 12962f8aaab3Seschrock nvlist_t * 12973a57275aSck zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile, 1298e829d913Sck char *poolname, uint64_t guid) 12992f8aaab3Seschrock { 13002f8aaab3Seschrock char *buf; 13012f8aaab3Seschrock int fd; 13022f8aaab3Seschrock struct stat64 statbuf; 13032f8aaab3Seschrock nvlist_t *raw, *src, *dst; 13042f8aaab3Seschrock nvlist_t *pools; 13052f8aaab3Seschrock nvpair_t *elem; 13062f8aaab3Seschrock char *name; 130724e697d4Sck uint64_t this_guid; 13082f8aaab3Seschrock boolean_t active; 13092f8aaab3Seschrock 131024e697d4Sck verify(poolname == NULL || guid == 0); 131124e697d4Sck 13122f8aaab3Seschrock if ((fd = open(cachefile, O_RDONLY)) < 0) { 13132f8aaab3Seschrock zfs_error_aux(hdl, "%s", strerror(errno)); 13142f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 13152f8aaab3Seschrock dgettext(TEXT_DOMAIN, "failed to open cache file")); 13162f8aaab3Seschrock return (NULL); 13172f8aaab3Seschrock } 13182f8aaab3Seschrock 13192f8aaab3Seschrock if (fstat64(fd, &statbuf) != 0) { 13202f8aaab3Seschrock zfs_error_aux(hdl, "%s", strerror(errno)); 13212f8aaab3Seschrock (void) close(fd); 13222f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 13232f8aaab3Seschrock dgettext(TEXT_DOMAIN, "failed to get size of cache file")); 13242f8aaab3Seschrock return (NULL); 13252f8aaab3Seschrock } 13262f8aaab3Seschrock 13272f8aaab3Seschrock if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) { 13282f8aaab3Seschrock (void) close(fd); 13292f8aaab3Seschrock return (NULL); 13302f8aaab3Seschrock } 13312f8aaab3Seschrock 13322f8aaab3Seschrock if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 13332f8aaab3Seschrock (void) close(fd); 13342f8aaab3Seschrock free(buf); 13352f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 13362f8aaab3Seschrock dgettext(TEXT_DOMAIN, 13372f8aaab3Seschrock "failed to read cache file contents")); 13382f8aaab3Seschrock return (NULL); 13392f8aaab3Seschrock } 13402f8aaab3Seschrock 13412f8aaab3Seschrock (void) close(fd); 13422f8aaab3Seschrock 13432f8aaab3Seschrock if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { 13442f8aaab3Seschrock free(buf); 13452f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 13462f8aaab3Seschrock dgettext(TEXT_DOMAIN, 13472f8aaab3Seschrock "invalid or corrupt cache file contents")); 13482f8aaab3Seschrock return (NULL); 13492f8aaab3Seschrock } 13502f8aaab3Seschrock 13512f8aaab3Seschrock free(buf); 13522f8aaab3Seschrock 13532f8aaab3Seschrock /* 13542f8aaab3Seschrock * Go through and get the current state of the pools and refresh their 13552f8aaab3Seschrock * state. 13562f8aaab3Seschrock */ 13572f8aaab3Seschrock if (nvlist_alloc(&pools, 0, 0) != 0) { 13582f8aaab3Seschrock (void) no_memory(hdl); 13592f8aaab3Seschrock nvlist_free(raw); 13602f8aaab3Seschrock return (NULL); 13612f8aaab3Seschrock } 13622f8aaab3Seschrock 13632f8aaab3Seschrock elem = NULL; 13642f8aaab3Seschrock while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { 1365b18d6b0eSMatthew Ahrens src = fnvpair_value_nvlist(elem); 13662f8aaab3Seschrock 1367b18d6b0eSMatthew Ahrens name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME); 136824e697d4Sck if (poolname != NULL && strcmp(poolname, name) != 0) 136924e697d4Sck continue; 137024e697d4Sck 1371b18d6b0eSMatthew Ahrens this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID); 1372b18d6b0eSMatthew Ahrens if (guid != 0 && guid != this_guid) 1373b18d6b0eSMatthew Ahrens continue; 13742f8aaab3Seschrock 1375e829d913Sck if (pool_active(hdl, name, this_guid, &active) != 0) { 1376e829d913Sck nvlist_free(raw); 1377e829d913Sck nvlist_free(pools); 1378e829d913Sck return (NULL); 1379e829d913Sck } 13802f8aaab3Seschrock 1381e829d913Sck if (active) 1382e829d913Sck continue; 13832f8aaab3Seschrock 13846f793812SPavel Zakharov if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE, 13856f793812SPavel Zakharov cachefile) != 0) { 13866f793812SPavel Zakharov (void) no_memory(hdl); 13876f793812SPavel Zakharov nvlist_free(raw); 13886f793812SPavel Zakharov nvlist_free(pools); 13896f793812SPavel Zakharov return (NULL); 13906f793812SPavel Zakharov } 13916f793812SPavel Zakharov 1392e829d913Sck if ((dst = refresh_config(hdl, src)) == NULL) { 1393e829d913Sck nvlist_free(raw); 1394e829d913Sck nvlist_free(pools); 1395e829d913Sck return (NULL); 1396e829d913Sck } 13972f8aaab3Seschrock 1398e829d913Sck if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { 1399e829d913Sck (void) no_memory(hdl); 14002f8aaab3Seschrock nvlist_free(dst); 1401e829d913Sck nvlist_free(raw); 1402e829d913Sck nvlist_free(pools); 1403e829d913Sck return (NULL); 14042f8aaab3Seschrock } 1405e829d913Sck nvlist_free(dst); 14062f8aaab3Seschrock } 14072f8aaab3Seschrock 14082f8aaab3Seschrock nvlist_free(raw); 14092f8aaab3Seschrock return (pools); 14102f8aaab3Seschrock } 14112f8aaab3Seschrock 1412d41c4376SMark J Musante static int 1413d41c4376SMark J Musante name_or_guid_exists(zpool_handle_t *zhp, void *data) 1414d41c4376SMark J Musante { 1415d41c4376SMark J Musante importargs_t *import = data; 1416d41c4376SMark J Musante int found = 0; 1417d41c4376SMark J Musante 1418d41c4376SMark J Musante if (import->poolname != NULL) { 1419d41c4376SMark J Musante char *pool_name; 1420d41c4376SMark J Musante 1421d41c4376SMark J Musante verify(nvlist_lookup_string(zhp->zpool_config, 1422d41c4376SMark J Musante ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0); 1423d41c4376SMark J Musante if (strcmp(pool_name, import->poolname) == 0) 1424d41c4376SMark J Musante found = 1; 1425d41c4376SMark J Musante } else { 1426d41c4376SMark J Musante uint64_t pool_guid; 1427d41c4376SMark J Musante 1428d41c4376SMark J Musante verify(nvlist_lookup_uint64(zhp->zpool_config, 1429d41c4376SMark J Musante ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0); 1430d41c4376SMark J Musante if (pool_guid == import->guid) 1431d41c4376SMark J Musante found = 1; 1432d41c4376SMark J Musante } 1433d41c4376SMark J Musante 1434d41c4376SMark J Musante zpool_close(zhp); 1435d41c4376SMark J Musante return (found); 1436d41c4376SMark J Musante } 1437d41c4376SMark J Musante 1438d41c4376SMark J Musante nvlist_t * 1439d41c4376SMark J Musante zpool_search_import(libzfs_handle_t *hdl, importargs_t *import) 1440d41c4376SMark J Musante { 1441d41c4376SMark J Musante verify(import->poolname == NULL || import->guid == 0); 1442d41c4376SMark J Musante 1443d41c4376SMark J Musante if (import->unique) 1444d41c4376SMark J Musante import->exists = zpool_iter(hdl, name_or_guid_exists, import); 1445d41c4376SMark J Musante 1446d41c4376SMark J Musante if (import->cachefile != NULL) 1447d41c4376SMark J Musante return (zpool_find_import_cached(hdl, import->cachefile, 1448d41c4376SMark J Musante import->poolname, import->guid)); 1449d41c4376SMark J Musante 1450d41c4376SMark J Musante return (zpool_find_import_impl(hdl, import)); 1451d41c4376SMark J Musante } 14522f8aaab3Seschrock 145399653d4eSeschrock boolean_t 1454fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid) 1455fa9e4066Sahrens { 1456fa9e4066Sahrens uint64_t tmp; 1457fa9e4066Sahrens nvlist_t **child; 1458fa9e4066Sahrens uint_t c, children; 1459fa9e4066Sahrens 1460fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0); 1461fa9e4066Sahrens if (tmp == guid) 146299653d4eSeschrock return (B_TRUE); 1463fa9e4066Sahrens 1464fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1465fa9e4066Sahrens &child, &children) == 0) { 1466fa9e4066Sahrens for (c = 0; c < children; c++) 1467fa9e4066Sahrens if (find_guid(child[c], guid)) 146899653d4eSeschrock return (B_TRUE); 146999653d4eSeschrock } 147099653d4eSeschrock 147199653d4eSeschrock return (B_FALSE); 147299653d4eSeschrock } 147399653d4eSeschrock 1474fa94a07fSbrendan typedef struct aux_cbdata { 1475fa94a07fSbrendan const char *cb_type; 147699653d4eSeschrock uint64_t cb_guid; 147799653d4eSeschrock zpool_handle_t *cb_zhp; 1478fa94a07fSbrendan } aux_cbdata_t; 147999653d4eSeschrock 148099653d4eSeschrock static int 1481fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data) 148299653d4eSeschrock { 1483fa94a07fSbrendan aux_cbdata_t *cbp = data; 1484fa94a07fSbrendan nvlist_t **list; 1485fa94a07fSbrendan uint_t i, count; 148699653d4eSeschrock uint64_t guid; 148799653d4eSeschrock nvlist_t *nvroot; 148899653d4eSeschrock 148999653d4eSeschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 149099653d4eSeschrock &nvroot) == 0); 149199653d4eSeschrock 1492fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type, 1493fa94a07fSbrendan &list, &count) == 0) { 1494fa94a07fSbrendan for (i = 0; i < count; i++) { 1495fa94a07fSbrendan verify(nvlist_lookup_uint64(list[i], 149699653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 149799653d4eSeschrock if (guid == cbp->cb_guid) { 149899653d4eSeschrock cbp->cb_zhp = zhp; 149999653d4eSeschrock return (1); 150099653d4eSeschrock } 150199653d4eSeschrock } 1502fa9e4066Sahrens } 1503fa9e4066Sahrens 150499653d4eSeschrock zpool_close(zhp); 150599653d4eSeschrock return (0); 1506fa9e4066Sahrens } 1507fa9e4066Sahrens 1508fa9e4066Sahrens /* 150999653d4eSeschrock * Determines if the pool is in use. If so, it returns true and the state of 1510fa9e4066Sahrens * the pool as well as the name of the pool. Both strings are allocated and 1511fa9e4066Sahrens * must be freed by the caller. 1512fa9e4066Sahrens */ 1513fa9e4066Sahrens int 151499653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr, 151599653d4eSeschrock boolean_t *inuse) 1516fa9e4066Sahrens { 1517fa9e4066Sahrens nvlist_t *config; 1518fa9e4066Sahrens char *name; 151999653d4eSeschrock boolean_t ret; 1520fa9e4066Sahrens uint64_t guid, vdev_guid; 1521fa9e4066Sahrens zpool_handle_t *zhp; 1522fa9e4066Sahrens nvlist_t *pool_config; 152339c23413Seschrock uint64_t stateval, isspare; 1524fa94a07fSbrendan aux_cbdata_t cb = { 0 }; 152594de1d4cSeschrock boolean_t isactive; 152699653d4eSeschrock 152799653d4eSeschrock *inuse = B_FALSE; 1528fa9e4066Sahrens 1529c861bfbdSAlan Somers if (zpool_read_label(fd, &config) != 0 && errno == ENOMEM) { 153099653d4eSeschrock (void) no_memory(hdl); 153199653d4eSeschrock return (-1); 153299653d4eSeschrock } 153399653d4eSeschrock 153499653d4eSeschrock if (config == NULL) 153599653d4eSeschrock return (0); 1536fa9e4066Sahrens 1537fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 153846a2abf2Seschrock &stateval) == 0); 1539fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 1540fa9e4066Sahrens &vdev_guid) == 0); 1541fa9e4066Sahrens 1542fa94a07fSbrendan if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) { 154399653d4eSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 154499653d4eSeschrock &name) == 0); 154599653d4eSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 154699653d4eSeschrock &guid) == 0); 154799653d4eSeschrock } 154899653d4eSeschrock 154946a2abf2Seschrock switch (stateval) { 1550fa9e4066Sahrens case POOL_STATE_EXPORTED: 1551f9af39baSGeorge Wilson /* 1552f9af39baSGeorge Wilson * A pool with an exported state may in fact be imported 1553f9af39baSGeorge Wilson * read-only, so check the in-core state to see if it's 1554f9af39baSGeorge Wilson * active and imported read-only. If it is, set 1555f9af39baSGeorge Wilson * its state to active. 1556f9af39baSGeorge Wilson */ 1557f9af39baSGeorge Wilson if (pool_active(hdl, name, guid, &isactive) == 0 && isactive && 1558fb13f48fSJosef 'Jeff' Sipek (zhp = zpool_open_canfail(hdl, name)) != NULL) { 1559fb13f48fSJosef 'Jeff' Sipek if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL)) 1560fb13f48fSJosef 'Jeff' Sipek stateval = POOL_STATE_ACTIVE; 1561fb13f48fSJosef 'Jeff' Sipek 1562fb13f48fSJosef 'Jeff' Sipek /* 1563fb13f48fSJosef 'Jeff' Sipek * All we needed the zpool handle for is the 1564fb13f48fSJosef 'Jeff' Sipek * readonly prop check. 1565fb13f48fSJosef 'Jeff' Sipek */ 1566fb13f48fSJosef 'Jeff' Sipek zpool_close(zhp); 1567fb13f48fSJosef 'Jeff' Sipek } 1568f9af39baSGeorge Wilson 156999653d4eSeschrock ret = B_TRUE; 1570fa9e4066Sahrens break; 1571fa9e4066Sahrens 1572fa9e4066Sahrens case POOL_STATE_ACTIVE: 1573fa9e4066Sahrens /* 1574fa9e4066Sahrens * For an active pool, we have to determine if it's really part 1575eaca9bbdSeschrock * of a currently active pool (in which case the pool will exist 1576eaca9bbdSeschrock * and the guid will be the same), or whether it's part of an 1577eaca9bbdSeschrock * active pool that was disconnected without being explicitly 1578eaca9bbdSeschrock * exported. 1579fa9e4066Sahrens */ 158094de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) { 158194de1d4cSeschrock nvlist_free(config); 158294de1d4cSeschrock return (-1); 158394de1d4cSeschrock } 158494de1d4cSeschrock 158594de1d4cSeschrock if (isactive) { 1586fa9e4066Sahrens /* 1587fa9e4066Sahrens * Because the device may have been removed while 1588fa9e4066Sahrens * offlined, we only report it as active if the vdev is 1589fa9e4066Sahrens * still present in the config. Otherwise, pretend like 1590fa9e4066Sahrens * it's not in use. 1591fa9e4066Sahrens */ 159299653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, name)) != NULL && 1593088e9d47Seschrock (pool_config = zpool_get_config(zhp, NULL)) 1594088e9d47Seschrock != NULL) { 1595fa9e4066Sahrens nvlist_t *nvroot; 1596fa9e4066Sahrens 1597fa9e4066Sahrens verify(nvlist_lookup_nvlist(pool_config, 1598fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 159946a2abf2Seschrock ret = find_guid(nvroot, vdev_guid); 1600fa9e4066Sahrens } else { 160199653d4eSeschrock ret = B_FALSE; 1602fa9e4066Sahrens } 160399653d4eSeschrock 160439c23413Seschrock /* 160539c23413Seschrock * If this is an active spare within another pool, we 160639c23413Seschrock * treat it like an unused hot spare. This allows the 160739c23413Seschrock * user to create a pool with a hot spare that currently 160839c23413Seschrock * in use within another pool. Since we return B_TRUE, 160939c23413Seschrock * libdiskmgt will continue to prevent generic consumers 161039c23413Seschrock * from using the device. 161139c23413Seschrock */ 161239c23413Seschrock if (ret && nvlist_lookup_uint64(config, 161339c23413Seschrock ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) 161439c23413Seschrock stateval = POOL_STATE_SPARE; 161539c23413Seschrock 161699653d4eSeschrock if (zhp != NULL) 161799653d4eSeschrock zpool_close(zhp); 1618fa9e4066Sahrens } else { 161946a2abf2Seschrock stateval = POOL_STATE_POTENTIALLY_ACTIVE; 162099653d4eSeschrock ret = B_TRUE; 162199653d4eSeschrock } 162299653d4eSeschrock break; 162399653d4eSeschrock 162499653d4eSeschrock case POOL_STATE_SPARE: 162599653d4eSeschrock /* 162699653d4eSeschrock * For a hot spare, it can be either definitively in use, or 162799653d4eSeschrock * potentially active. To determine if it's in use, we iterate 162899653d4eSeschrock * over all pools in the system and search for one with a spare 162999653d4eSeschrock * with a matching guid. 163099653d4eSeschrock * 163199653d4eSeschrock * Due to the shared nature of spares, we don't actually report 163299653d4eSeschrock * the potentially active case as in use. This means the user 163399653d4eSeschrock * can freely create pools on the hot spares of exported pools, 163499653d4eSeschrock * but to do otherwise makes the resulting code complicated, and 163599653d4eSeschrock * we end up having to deal with this case anyway. 163699653d4eSeschrock */ 163799653d4eSeschrock cb.cb_zhp = NULL; 163899653d4eSeschrock cb.cb_guid = vdev_guid; 1639fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_SPARES; 1640fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 1641fa94a07fSbrendan name = (char *)zpool_get_name(cb.cb_zhp); 1642078266a5SMarcel Telka ret = B_TRUE; 1643fa94a07fSbrendan } else { 1644078266a5SMarcel Telka ret = B_FALSE; 1645fa94a07fSbrendan } 1646fa94a07fSbrendan break; 1647fa94a07fSbrendan 1648fa94a07fSbrendan case POOL_STATE_L2CACHE: 1649fa94a07fSbrendan 1650fa94a07fSbrendan /* 1651fa94a07fSbrendan * Check if any pool is currently using this l2cache device. 1652fa94a07fSbrendan */ 1653fa94a07fSbrendan cb.cb_zhp = NULL; 1654fa94a07fSbrendan cb.cb_guid = vdev_guid; 1655fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_L2CACHE; 1656fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 165799653d4eSeschrock name = (char *)zpool_get_name(cb.cb_zhp); 1658078266a5SMarcel Telka ret = B_TRUE; 165999653d4eSeschrock } else { 1660078266a5SMarcel Telka ret = B_FALSE; 1661fa9e4066Sahrens } 1662fa9e4066Sahrens break; 1663fa9e4066Sahrens 1664fa9e4066Sahrens default: 166599653d4eSeschrock ret = B_FALSE; 1666fa9e4066Sahrens } 1667fa9e4066Sahrens 166846a2abf2Seschrock 166946a2abf2Seschrock if (ret) { 167099653d4eSeschrock if ((*namestr = zfs_strdup(hdl, name)) == NULL) { 1671c6765aabSeschrock if (cb.cb_zhp) 1672c6765aabSeschrock zpool_close(cb.cb_zhp); 167399653d4eSeschrock nvlist_free(config); 167499653d4eSeschrock return (-1); 167599653d4eSeschrock } 167646a2abf2Seschrock *state = (pool_state_t)stateval; 167746a2abf2Seschrock } 167846a2abf2Seschrock 167999653d4eSeschrock if (cb.cb_zhp) 168099653d4eSeschrock zpool_close(cb.cb_zhp); 168199653d4eSeschrock 1682fa9e4066Sahrens nvlist_free(config); 168399653d4eSeschrock *inuse = ret; 168499653d4eSeschrock return (0); 1685fa9e4066Sahrens } 1686