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*39c23413Seschrock * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * Pool import support functions. 30fa9e4066Sahrens * 31fa9e4066Sahrens * To import a pool, we rely on reading the configuration information from the 32fa9e4066Sahrens * ZFS label of each device. If we successfully read the label, then we 33fa9e4066Sahrens * organize the configuration information in the following hierarchy: 34fa9e4066Sahrens * 35fa9e4066Sahrens * pool guid -> toplevel vdev guid -> label txg 36fa9e4066Sahrens * 37fa9e4066Sahrens * Duplicate entries matching this same tuple will be discarded. Once we have 38fa9e4066Sahrens * examined every device, we pick the best label txg config for each toplevel 39fa9e4066Sahrens * vdev. We then arrange these toplevel vdevs into a complete pool config, and 40fa9e4066Sahrens * update any paths that have changed. Finally, we attempt to import the pool 41fa9e4066Sahrens * using our derived config, and record the results. 42fa9e4066Sahrens */ 43fa9e4066Sahrens 44fa9e4066Sahrens #include <devid.h> 45fa9e4066Sahrens #include <dirent.h> 46fa9e4066Sahrens #include <errno.h> 47fa9e4066Sahrens #include <libintl.h> 48fa9e4066Sahrens #include <stdlib.h> 49fa9e4066Sahrens #include <string.h> 50fa9e4066Sahrens #include <sys/stat.h> 51fa9e4066Sahrens #include <unistd.h> 52fa9e4066Sahrens #include <fcntl.h> 53fa9e4066Sahrens 54fa9e4066Sahrens #include <sys/vdev_impl.h> 55fa9e4066Sahrens 56fa9e4066Sahrens #include "libzfs.h" 57fa9e4066Sahrens #include "libzfs_impl.h" 58fa9e4066Sahrens 59fa9e4066Sahrens /* 60fa9e4066Sahrens * Intermediate structures used to gather configuration information. 61fa9e4066Sahrens */ 62fa9e4066Sahrens typedef struct config_entry { 63fa9e4066Sahrens uint64_t ce_txg; 64fa9e4066Sahrens nvlist_t *ce_config; 65fa9e4066Sahrens struct config_entry *ce_next; 66fa9e4066Sahrens } config_entry_t; 67fa9e4066Sahrens 68fa9e4066Sahrens typedef struct vdev_entry { 69fa9e4066Sahrens uint64_t ve_guid; 70fa9e4066Sahrens config_entry_t *ve_configs; 71fa9e4066Sahrens struct vdev_entry *ve_next; 72fa9e4066Sahrens } vdev_entry_t; 73fa9e4066Sahrens 74fa9e4066Sahrens typedef struct pool_entry { 75fa9e4066Sahrens uint64_t pe_guid; 76fa9e4066Sahrens vdev_entry_t *pe_vdevs; 77fa9e4066Sahrens struct pool_entry *pe_next; 78fa9e4066Sahrens } pool_entry_t; 79fa9e4066Sahrens 80fa9e4066Sahrens typedef struct name_entry { 8199653d4eSeschrock char *ne_name; 82fa9e4066Sahrens uint64_t ne_guid; 83fa9e4066Sahrens struct name_entry *ne_next; 84fa9e4066Sahrens } name_entry_t; 85fa9e4066Sahrens 86fa9e4066Sahrens typedef struct pool_list { 87fa9e4066Sahrens pool_entry_t *pools; 88fa9e4066Sahrens name_entry_t *names; 89fa9e4066Sahrens } pool_list_t; 90fa9e4066Sahrens 91fa9e4066Sahrens static char * 92fa9e4066Sahrens get_devid(const char *path) 93fa9e4066Sahrens { 94fa9e4066Sahrens int fd; 95fa9e4066Sahrens ddi_devid_t devid; 96fa9e4066Sahrens char *minor, *ret; 97fa9e4066Sahrens 98fa9e4066Sahrens if ((fd = open(path, O_RDONLY)) < 0) 99fa9e4066Sahrens return (NULL); 100fa9e4066Sahrens 101fa9e4066Sahrens minor = NULL; 102fa9e4066Sahrens ret = NULL; 103fa9e4066Sahrens if (devid_get(fd, &devid) == 0) { 104fa9e4066Sahrens if (devid_get_minor_name(fd, &minor) == 0) 105fa9e4066Sahrens ret = devid_str_encode(devid, minor); 106fa9e4066Sahrens if (minor != NULL) 107fa9e4066Sahrens devid_str_free(minor); 108fa9e4066Sahrens devid_free(devid); 109fa9e4066Sahrens } 110c67d9675Seschrock (void) close(fd); 111fa9e4066Sahrens 112fa9e4066Sahrens return (ret); 113fa9e4066Sahrens } 114fa9e4066Sahrens 115fa9e4066Sahrens 116fa9e4066Sahrens /* 117fa9e4066Sahrens * Go through and fix up any path and/or devid information for the given vdev 118fa9e4066Sahrens * configuration. 119fa9e4066Sahrens */ 12099653d4eSeschrock static int 121fa9e4066Sahrens fix_paths(nvlist_t *nv, name_entry_t *names) 122fa9e4066Sahrens { 123fa9e4066Sahrens nvlist_t **child; 124fa9e4066Sahrens uint_t c, children; 125fa9e4066Sahrens uint64_t guid; 126c67d9675Seschrock name_entry_t *ne, *best; 127c67d9675Seschrock char *path, *devid; 128c67d9675Seschrock int matched; 129fa9e4066Sahrens 130fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 131fa9e4066Sahrens &child, &children) == 0) { 132fa9e4066Sahrens for (c = 0; c < children; c++) 13399653d4eSeschrock if (fix_paths(child[c], names) != 0) 13499653d4eSeschrock return (-1); 13599653d4eSeschrock return (0); 136fa9e4066Sahrens } 137fa9e4066Sahrens 138fa9e4066Sahrens /* 139fa9e4066Sahrens * This is a leaf (file or disk) vdev. In either case, go through 140fa9e4066Sahrens * the name list and see if we find a matching guid. If so, replace 141fa9e4066Sahrens * the path and see if we can calculate a new devid. 142c67d9675Seschrock * 143c67d9675Seschrock * There may be multiple names associated with a particular guid, in 144c67d9675Seschrock * which case we have overlapping slices or multiple paths to the same 145c67d9675Seschrock * disk. If this is the case, then we want to pick the path that is 146c67d9675Seschrock * the most similar to the original, where "most similar" is the number 147c67d9675Seschrock * of matching characters starting from the end of the path. This will 148c67d9675Seschrock * preserve slice numbers even if the disks have been reorganized, and 149c67d9675Seschrock * will also catch preferred disk names if multiple paths exist. 150fa9e4066Sahrens */ 151fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); 152c67d9675Seschrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 153c67d9675Seschrock path = NULL; 154c67d9675Seschrock 155c67d9675Seschrock matched = 0; 156c67d9675Seschrock best = NULL; 157c67d9675Seschrock for (ne = names; ne != NULL; ne = ne->ne_next) { 158c67d9675Seschrock if (ne->ne_guid == guid) { 159c67d9675Seschrock const char *src, *dst; 160c67d9675Seschrock int count; 161c67d9675Seschrock 162c67d9675Seschrock if (path == NULL) { 163c67d9675Seschrock best = ne; 164c67d9675Seschrock break; 165c67d9675Seschrock } 166c67d9675Seschrock 167c67d9675Seschrock src = ne->ne_name + strlen(ne->ne_name) - 1; 168c67d9675Seschrock dst = path + strlen(path) - 1; 169c67d9675Seschrock for (count = 0; src >= ne->ne_name && dst >= path; 170c67d9675Seschrock src--, dst--, count++) 171c67d9675Seschrock if (*src != *dst) 172c67d9675Seschrock break; 173c67d9675Seschrock 174c67d9675Seschrock /* 175c67d9675Seschrock * At this point, 'count' is the number of characters 176c67d9675Seschrock * matched from the end. 177c67d9675Seschrock */ 178c67d9675Seschrock if (count > matched || best == NULL) { 179c67d9675Seschrock best = ne; 180c67d9675Seschrock matched = count; 181c67d9675Seschrock } 182c67d9675Seschrock } 183c67d9675Seschrock } 184fa9e4066Sahrens 185c67d9675Seschrock if (best == NULL) 18699653d4eSeschrock return (0); 187fa9e4066Sahrens 18899653d4eSeschrock if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) 18999653d4eSeschrock return (-1); 190fa9e4066Sahrens 191c67d9675Seschrock if ((devid = get_devid(best->ne_name)) == NULL) { 192fa9e4066Sahrens (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID); 193fa9e4066Sahrens } else { 19499653d4eSeschrock if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0) 19599653d4eSeschrock return (-1); 196fa9e4066Sahrens devid_str_free(devid); 197fa9e4066Sahrens } 19899653d4eSeschrock 19999653d4eSeschrock return (0); 200fa9e4066Sahrens } 201fa9e4066Sahrens 202fa9e4066Sahrens /* 203fa9e4066Sahrens * Add the given configuration to the list of known devices. 204fa9e4066Sahrens */ 20599653d4eSeschrock static int 20699653d4eSeschrock add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path, 20799653d4eSeschrock nvlist_t *config) 208fa9e4066Sahrens { 20999653d4eSeschrock uint64_t pool_guid, vdev_guid, top_guid, txg, state; 210fa9e4066Sahrens pool_entry_t *pe; 211fa9e4066Sahrens vdev_entry_t *ve; 212fa9e4066Sahrens config_entry_t *ce; 213fa9e4066Sahrens name_entry_t *ne; 214fa9e4066Sahrens 21599653d4eSeschrock /* 21699653d4eSeschrock * If this is a hot spare not currently in use, add it to the list of 21799653d4eSeschrock * names to translate, but don't do anything else. 21899653d4eSeschrock */ 21999653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 22099653d4eSeschrock &state) == 0 && state == POOL_STATE_SPARE && 22199653d4eSeschrock nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 22299653d4eSeschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) 22399653d4eSeschrock 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 364fa9e4066Sahrens /* 365fa9e4066Sahrens * Convert our list of pools into the definitive set of configurations. We 366fa9e4066Sahrens * start by picking the best config for each toplevel vdev. Once that's done, 367fa9e4066Sahrens * we assemble the toplevel vdevs into a full config for the pool. We make a 368fa9e4066Sahrens * pass to fix up any incorrect paths, and then add it to the main list to 369fa9e4066Sahrens * return to the user. 370fa9e4066Sahrens */ 371fa9e4066Sahrens static nvlist_t * 37299653d4eSeschrock get_configs(libzfs_handle_t *hdl, pool_list_t *pl) 373fa9e4066Sahrens { 37499653d4eSeschrock pool_entry_t *pe; 37599653d4eSeschrock vdev_entry_t *ve; 37699653d4eSeschrock config_entry_t *ce; 37799653d4eSeschrock nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot; 37899653d4eSeschrock nvlist_t **spares; 37999653d4eSeschrock uint_t i, nspares; 38099653d4eSeschrock boolean_t config_seen; 381fa9e4066Sahrens uint64_t best_txg; 382fa9e4066Sahrens char *name; 383fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 38499653d4eSeschrock uint64_t version, guid; 385fa9e4066Sahrens size_t len; 386fa9e4066Sahrens int err; 38799653d4eSeschrock uint_t children = 0; 38899653d4eSeschrock nvlist_t **child = NULL; 38999653d4eSeschrock uint_t c; 39094de1d4cSeschrock boolean_t isactive; 391fa9e4066Sahrens 39299653d4eSeschrock if (nvlist_alloc(&ret, 0, 0) != 0) 39399653d4eSeschrock goto nomem; 394fa9e4066Sahrens 39599653d4eSeschrock for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 396fa9e4066Sahrens uint64_t id; 397fa9e4066Sahrens 39899653d4eSeschrock if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 39999653d4eSeschrock goto nomem; 40099653d4eSeschrock config_seen = B_FALSE; 401fa9e4066Sahrens 402fa9e4066Sahrens /* 403fa9e4066Sahrens * Iterate over all toplevel vdevs. Grab the pool configuration 404fa9e4066Sahrens * from the first one we find, and then go through the rest and 405fa9e4066Sahrens * add them as necessary to the 'vdevs' member of the config. 406fa9e4066Sahrens */ 40799653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 408fa9e4066Sahrens 409fa9e4066Sahrens /* 410fa9e4066Sahrens * Determine the best configuration for this vdev by 411fa9e4066Sahrens * selecting the config with the latest transaction 412fa9e4066Sahrens * group. 413fa9e4066Sahrens */ 414fa9e4066Sahrens best_txg = 0; 415fa9e4066Sahrens for (ce = ve->ve_configs; ce != NULL; 416fa9e4066Sahrens ce = ce->ce_next) { 417fa9e4066Sahrens 41899653d4eSeschrock if (ce->ce_txg > best_txg) { 419fa9e4066Sahrens tmp = ce->ce_config; 42099653d4eSeschrock best_txg = ce->ce_txg; 42199653d4eSeschrock } 422fa9e4066Sahrens } 423fa9e4066Sahrens 424fa9e4066Sahrens if (!config_seen) { 425fa9e4066Sahrens /* 426fa9e4066Sahrens * Copy the relevant pieces of data to the pool 427fa9e4066Sahrens * configuration: 428fa9e4066Sahrens * 42999653d4eSeschrock * version 430fa9e4066Sahrens * pool guid 431fa9e4066Sahrens * name 432fa9e4066Sahrens * pool state 433fa9e4066Sahrens */ 434fa9e4066Sahrens uint64_t state; 435fa9e4066Sahrens 43699653d4eSeschrock verify(nvlist_lookup_uint64(tmp, 43799653d4eSeschrock ZPOOL_CONFIG_VERSION, &version) == 0); 43899653d4eSeschrock if (nvlist_add_uint64(config, 43999653d4eSeschrock ZPOOL_CONFIG_VERSION, version) != 0) 44099653d4eSeschrock goto nomem; 441fa9e4066Sahrens verify(nvlist_lookup_uint64(tmp, 442fa9e4066Sahrens ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 44399653d4eSeschrock if (nvlist_add_uint64(config, 44499653d4eSeschrock ZPOOL_CONFIG_POOL_GUID, guid) != 0) 44599653d4eSeschrock goto nomem; 446fa9e4066Sahrens verify(nvlist_lookup_string(tmp, 447fa9e4066Sahrens ZPOOL_CONFIG_POOL_NAME, &name) == 0); 44899653d4eSeschrock if (nvlist_add_string(config, 44999653d4eSeschrock ZPOOL_CONFIG_POOL_NAME, name) != 0) 45099653d4eSeschrock goto nomem; 451fa9e4066Sahrens verify(nvlist_lookup_uint64(tmp, 452fa9e4066Sahrens ZPOOL_CONFIG_POOL_STATE, &state) == 0); 45399653d4eSeschrock if (nvlist_add_uint64(config, 45499653d4eSeschrock ZPOOL_CONFIG_POOL_STATE, state) != 0) 45599653d4eSeschrock goto nomem; 456fa9e4066Sahrens 45799653d4eSeschrock config_seen = B_TRUE; 458fa9e4066Sahrens } 459fa9e4066Sahrens 460fa9e4066Sahrens /* 461fa9e4066Sahrens * Add this top-level vdev to the child array. 462fa9e4066Sahrens */ 463fa9e4066Sahrens verify(nvlist_lookup_nvlist(tmp, 464fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 465fa9e4066Sahrens verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 466fa9e4066Sahrens &id) == 0); 467fa9e4066Sahrens if (id >= children) { 468fa9e4066Sahrens nvlist_t **newchild; 469fa9e4066Sahrens 47099653d4eSeschrock newchild = zfs_alloc(hdl, (id + 1) * 471fa9e4066Sahrens sizeof (nvlist_t *)); 47299653d4eSeschrock if (newchild == NULL) 47399653d4eSeschrock goto nomem; 474fa9e4066Sahrens 475fa9e4066Sahrens for (c = 0; c < children; c++) 476fa9e4066Sahrens newchild[c] = child[c]; 477fa9e4066Sahrens 478fa9e4066Sahrens free(child); 479fa9e4066Sahrens child = newchild; 480fa9e4066Sahrens children = id + 1; 481fa9e4066Sahrens } 48299653d4eSeschrock if (nvlist_dup(nvtop, &child[id], 0) != 0) 48399653d4eSeschrock goto nomem; 484fa9e4066Sahrens 485fa9e4066Sahrens } 486fa9e4066Sahrens 487fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 488fa9e4066Sahrens &guid) == 0); 489fa9e4066Sahrens 490fa9e4066Sahrens /* 491fa9e4066Sahrens * Look for any missing top-level vdevs. If this is the case, 492fa9e4066Sahrens * create a faked up 'missing' vdev as a placeholder. We cannot 493fa9e4066Sahrens * simply compress the child array, because the kernel performs 494fa9e4066Sahrens * certain checks to make sure the vdev IDs match their location 495fa9e4066Sahrens * in the configuration. 496fa9e4066Sahrens */ 497fa9e4066Sahrens for (c = 0; c < children; c++) 498fa9e4066Sahrens if (child[c] == NULL) { 499fa9e4066Sahrens nvlist_t *missing; 50099653d4eSeschrock if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 50199653d4eSeschrock 0) != 0) 50299653d4eSeschrock goto nomem; 50399653d4eSeschrock if (nvlist_add_string(missing, 50499653d4eSeschrock ZPOOL_CONFIG_TYPE, 50599653d4eSeschrock VDEV_TYPE_MISSING) != 0 || 50699653d4eSeschrock nvlist_add_uint64(missing, 50799653d4eSeschrock ZPOOL_CONFIG_ID, c) != 0 || 50899653d4eSeschrock nvlist_add_uint64(missing, 50999653d4eSeschrock ZPOOL_CONFIG_GUID, 0ULL) != 0) { 51099653d4eSeschrock nvlist_free(missing); 51199653d4eSeschrock goto nomem; 51299653d4eSeschrock } 513fa9e4066Sahrens child[c] = missing; 514fa9e4066Sahrens } 515fa9e4066Sahrens 516fa9e4066Sahrens /* 517fa9e4066Sahrens * Put all of this pool's top-level vdevs into a root vdev. 518fa9e4066Sahrens */ 51999653d4eSeschrock if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 52099653d4eSeschrock goto nomem; 52199653d4eSeschrock if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 52299653d4eSeschrock VDEV_TYPE_ROOT) != 0 || 52399653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 52499653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 52599653d4eSeschrock nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 52699653d4eSeschrock child, children) != 0) { 52799653d4eSeschrock nvlist_free(nvroot); 52899653d4eSeschrock goto nomem; 52999653d4eSeschrock } 530fa9e4066Sahrens 531fa9e4066Sahrens for (c = 0; c < children; c++) 532fa9e4066Sahrens nvlist_free(child[c]); 533fa9e4066Sahrens free(child); 53499653d4eSeschrock children = 0; 53599653d4eSeschrock child = NULL; 536fa9e4066Sahrens 537fa9e4066Sahrens /* 538fa9e4066Sahrens * Go through and fix up any paths and/or devids based on our 539fa9e4066Sahrens * known list of vdev GUID -> path mappings. 540fa9e4066Sahrens */ 54199653d4eSeschrock if (fix_paths(nvroot, pl->names) != 0) { 54299653d4eSeschrock nvlist_free(nvroot); 54399653d4eSeschrock goto nomem; 54499653d4eSeschrock } 545fa9e4066Sahrens 546fa9e4066Sahrens /* 547fa9e4066Sahrens * Add the root vdev to this pool's configuration. 548fa9e4066Sahrens */ 54999653d4eSeschrock if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 55099653d4eSeschrock nvroot) != 0) { 55199653d4eSeschrock nvlist_free(nvroot); 55299653d4eSeschrock goto nomem; 55399653d4eSeschrock } 554fa9e4066Sahrens nvlist_free(nvroot); 555fa9e4066Sahrens 556fa9e4066Sahrens /* 557fa9e4066Sahrens * Determine if this pool is currently active, in which case we 558fa9e4066Sahrens * can't actually import it. 559fa9e4066Sahrens */ 560fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 561fa9e4066Sahrens &name) == 0); 562fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 563fa9e4066Sahrens &guid) == 0); 564fa9e4066Sahrens 56594de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) 56694de1d4cSeschrock goto error; 56794de1d4cSeschrock 5680192a278Seschrock if (isactive) { 569fa9e4066Sahrens nvlist_free(config); 57099653d4eSeschrock config = NULL; 571fa9e4066Sahrens continue; 572fa9e4066Sahrens } 573fa9e4066Sahrens 574fa9e4066Sahrens /* 575fa9e4066Sahrens * Try to do the import in order to get vdev state. 576fa9e4066Sahrens */ 577e9dbad6fSeschrock if (zcmd_write_src_nvlist(hdl, &zc, config, &len) != 0) 578e9dbad6fSeschrock goto error; 579fa9e4066Sahrens 580fa9e4066Sahrens nvlist_free(config); 581fa9e4066Sahrens config = NULL; 582fa9e4066Sahrens 583e9dbad6fSeschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, len * 2) != 0) { 584e9dbad6fSeschrock zcmd_free_nvlists(&zc); 585e9dbad6fSeschrock goto error; 586e9dbad6fSeschrock } 587fa9e4066Sahrens 58899653d4eSeschrock while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT, 589fa9e4066Sahrens &zc)) != 0 && errno == ENOMEM) { 590e9dbad6fSeschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 591e9dbad6fSeschrock zcmd_free_nvlists(&zc); 592e9dbad6fSeschrock goto error; 593e9dbad6fSeschrock } 594fa9e4066Sahrens } 595fa9e4066Sahrens 59699653d4eSeschrock if (err) { 59799653d4eSeschrock (void) zpool_standard_error(hdl, errno, 59899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot discover pools")); 599e9dbad6fSeschrock zcmd_free_nvlists(&zc); 60099653d4eSeschrock goto error; 60199653d4eSeschrock } 602fa9e4066Sahrens 603e9dbad6fSeschrock if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) { 604e9dbad6fSeschrock zcmd_free_nvlists(&zc); 605e9dbad6fSeschrock goto error; 60699653d4eSeschrock } 607e9dbad6fSeschrock 608e9dbad6fSeschrock zcmd_free_nvlists(&zc); 609fa9e4066Sahrens 61099653d4eSeschrock /* 61199653d4eSeschrock * Go through and update the paths for spares, now that we have 61299653d4eSeschrock * them. 61399653d4eSeschrock */ 61499653d4eSeschrock verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 61599653d4eSeschrock &nvroot) == 0); 61699653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 61799653d4eSeschrock &spares, &nspares) == 0) { 61899653d4eSeschrock for (i = 0; i < nspares; i++) { 61999653d4eSeschrock if (fix_paths(spares[i], pl->names) != 0) 62099653d4eSeschrock goto nomem; 62199653d4eSeschrock } 62299653d4eSeschrock } 62399653d4eSeschrock 62499653d4eSeschrock if (set_pool_health(config) != 0) 62599653d4eSeschrock goto nomem; 626fa9e4066Sahrens 627fa9e4066Sahrens /* 628fa9e4066Sahrens * Add this pool to the list of configs. 629fa9e4066Sahrens */ 630e9dbad6fSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 631e9dbad6fSeschrock &name) == 0); 63299653d4eSeschrock if (nvlist_add_nvlist(ret, name, config) != 0) 63399653d4eSeschrock goto nomem; 634fa9e4066Sahrens 635fa9e4066Sahrens nvlist_free(config); 63699653d4eSeschrock config = NULL; 637fa9e4066Sahrens } 638fa9e4066Sahrens 639fa9e4066Sahrens return (ret); 64099653d4eSeschrock 64199653d4eSeschrock nomem: 64299653d4eSeschrock (void) no_memory(hdl); 64399653d4eSeschrock error: 64494de1d4cSeschrock nvlist_free(config); 64594de1d4cSeschrock nvlist_free(ret); 64699653d4eSeschrock for (c = 0; c < children; c++) 64799653d4eSeschrock nvlist_free(child[c]); 64894de1d4cSeschrock free(child); 64999653d4eSeschrock 65099653d4eSeschrock return (NULL); 651fa9e4066Sahrens } 652fa9e4066Sahrens 653fa9e4066Sahrens /* 654fa9e4066Sahrens * Return the offset of the given label. 655fa9e4066Sahrens */ 656fa9e4066Sahrens static uint64_t 657fa9e4066Sahrens label_offset(size_t size, int l) 658fa9e4066Sahrens { 659fa9e4066Sahrens return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 660fa9e4066Sahrens 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 661fa9e4066Sahrens } 662fa9e4066Sahrens 663fa9e4066Sahrens /* 664fa9e4066Sahrens * Given a file descriptor, read the label information and return an nvlist 665fa9e4066Sahrens * describing the configuration, if there is one. 666fa9e4066Sahrens */ 66799653d4eSeschrock int 66899653d4eSeschrock zpool_read_label(int fd, nvlist_t **config) 669fa9e4066Sahrens { 670fa9e4066Sahrens struct stat64 statbuf; 671fa9e4066Sahrens int l; 672fa9e4066Sahrens vdev_label_t *label; 673ea8dc4b6Seschrock uint64_t state, txg; 674fa9e4066Sahrens 67599653d4eSeschrock *config = NULL; 67699653d4eSeschrock 677fa9e4066Sahrens if (fstat64(fd, &statbuf) == -1) 67899653d4eSeschrock return (0); 679fa9e4066Sahrens 68099653d4eSeschrock if ((label = malloc(sizeof (vdev_label_t))) == NULL) 68199653d4eSeschrock return (-1); 682fa9e4066Sahrens 683fa9e4066Sahrens for (l = 0; l < VDEV_LABELS; l++) { 684fa9e4066Sahrens if (pread(fd, label, sizeof (vdev_label_t), 685fa9e4066Sahrens label_offset(statbuf.st_size, l)) != sizeof (vdev_label_t)) 686fa9e4066Sahrens continue; 687fa9e4066Sahrens 688fa9e4066Sahrens if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 68999653d4eSeschrock sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) 690fa9e4066Sahrens continue; 691fa9e4066Sahrens 69299653d4eSeschrock if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 69399653d4eSeschrock &state) != 0 || state > POOL_STATE_SPARE) { 69499653d4eSeschrock nvlist_free(*config); 695fa9e4066Sahrens continue; 696fa9e4066Sahrens } 697fa9e4066Sahrens 69899653d4eSeschrock if (state != POOL_STATE_SPARE && 69999653d4eSeschrock (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 70099653d4eSeschrock &txg) != 0 || txg == 0)) { 70199653d4eSeschrock nvlist_free(*config); 702fa9e4066Sahrens continue; 703fa9e4066Sahrens } 704fa9e4066Sahrens 705fa9e4066Sahrens free(label); 70699653d4eSeschrock return (0); 707fa9e4066Sahrens } 708fa9e4066Sahrens 709fa9e4066Sahrens free(label); 71099653d4eSeschrock *config = NULL; 71199653d4eSeschrock return (0); 712fa9e4066Sahrens } 713fa9e4066Sahrens 714fa9e4066Sahrens /* 715fa9e4066Sahrens * Given a list of directories to search, find all pools stored on disk. This 716fa9e4066Sahrens * includes partial pools which are not available to import. If no args are 717fa9e4066Sahrens * given (argc is 0), then the default directory (/dev/dsk) is searched. 718fa9e4066Sahrens */ 719fa9e4066Sahrens nvlist_t * 72099653d4eSeschrock zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv) 721fa9e4066Sahrens { 722fa9e4066Sahrens int i; 723fa9e4066Sahrens DIR *dirp; 724fa9e4066Sahrens struct dirent64 *dp; 725fa9e4066Sahrens char path[MAXPATHLEN]; 726fa9e4066Sahrens struct stat64 statbuf; 72799653d4eSeschrock nvlist_t *ret = NULL, *config; 728fa9e4066Sahrens static char *default_dir = "/dev/dsk"; 729fa9e4066Sahrens int fd; 730fa9e4066Sahrens pool_list_t pools = { 0 }; 73199653d4eSeschrock pool_entry_t *pe, *penext; 73299653d4eSeschrock vdev_entry_t *ve, *venext; 73399653d4eSeschrock config_entry_t *ce, *cenext; 73499653d4eSeschrock name_entry_t *ne, *nenext; 73599653d4eSeschrock 736fa9e4066Sahrens 737fa9e4066Sahrens if (argc == 0) { 738fa9e4066Sahrens argc = 1; 739fa9e4066Sahrens argv = &default_dir; 740fa9e4066Sahrens } 741fa9e4066Sahrens 742fa9e4066Sahrens /* 743fa9e4066Sahrens * Go through and read the label configuration information from every 744fa9e4066Sahrens * possible device, organizing the information according to pool GUID 745fa9e4066Sahrens * and toplevel GUID. 746fa9e4066Sahrens */ 747fa9e4066Sahrens for (i = 0; i < argc; i++) { 748fa9e4066Sahrens if (argv[i][0] != '/') { 749ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 75099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 751fa9e4066Sahrens argv[i]); 75299653d4eSeschrock goto error; 753fa9e4066Sahrens } 754fa9e4066Sahrens 755fa9e4066Sahrens if ((dirp = opendir(argv[i])) == NULL) { 75699653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 757ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 75899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 75999653d4eSeschrock argv[i]); 76099653d4eSeschrock goto error; 761fa9e4066Sahrens } 762fa9e4066Sahrens 763fa9e4066Sahrens /* 764fa9e4066Sahrens * This is not MT-safe, but we have no MT consumers of libzfs 765fa9e4066Sahrens */ 766fa9e4066Sahrens while ((dp = readdir64(dirp)) != NULL) { 767fa9e4066Sahrens 768fa9e4066Sahrens (void) snprintf(path, sizeof (path), "%s/%s", 769fa9e4066Sahrens argv[i], dp->d_name); 770fa9e4066Sahrens 771fa9e4066Sahrens if (stat64(path, &statbuf) != 0) 772fa9e4066Sahrens continue; 773fa9e4066Sahrens 774fa9e4066Sahrens /* 775fa9e4066Sahrens * Ignore directories (which includes "." and ".."). 776fa9e4066Sahrens */ 777fa9e4066Sahrens if (S_ISDIR(statbuf.st_mode)) 778fa9e4066Sahrens continue; 7793bb79becSeschrock 7803bb79becSeschrock /* 7813bb79becSeschrock * Ignore special (non-character or non-block) files. 7823bb79becSeschrock */ 7833bb79becSeschrock if (!S_ISREG(statbuf.st_mode) && 7843bb79becSeschrock !S_ISBLK(statbuf.st_mode)) 7853bb79becSeschrock continue; 786fa9e4066Sahrens 787fa9e4066Sahrens if ((fd = open64(path, O_RDONLY)) < 0) 788fa9e4066Sahrens continue; 789fa9e4066Sahrens 79099653d4eSeschrock if ((zpool_read_label(fd, &config)) != 0) { 79199653d4eSeschrock (void) no_memory(hdl); 79299653d4eSeschrock goto error; 79399653d4eSeschrock } 794fa9e4066Sahrens 795fa9e4066Sahrens (void) close(fd); 796fa9e4066Sahrens 797fa9e4066Sahrens if (config != NULL) 79899653d4eSeschrock if (add_config(hdl, &pools, path, config) != 0) 79999653d4eSeschrock goto error; 800fa9e4066Sahrens } 801fa9e4066Sahrens } 802fa9e4066Sahrens 80399653d4eSeschrock ret = get_configs(hdl, &pools); 80499653d4eSeschrock 80599653d4eSeschrock error: 80699653d4eSeschrock for (pe = pools.pools; pe != NULL; pe = penext) { 80799653d4eSeschrock penext = pe->pe_next; 80899653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 80999653d4eSeschrock venext = ve->ve_next; 81099653d4eSeschrock for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 81199653d4eSeschrock cenext = ce->ce_next; 81299653d4eSeschrock if (ce->ce_config) 81399653d4eSeschrock nvlist_free(ce->ce_config); 81499653d4eSeschrock free(ce); 81599653d4eSeschrock } 81699653d4eSeschrock free(ve); 81799653d4eSeschrock } 81899653d4eSeschrock free(pe); 81999653d4eSeschrock } 82099653d4eSeschrock 82199653d4eSeschrock for (ne = pools.names; ne != NULL; ne = nenext) { 82299653d4eSeschrock nenext = ne->ne_next; 82399653d4eSeschrock if (ne->ne_name) 82499653d4eSeschrock free(ne->ne_name); 82599653d4eSeschrock free(ne); 82699653d4eSeschrock } 82799653d4eSeschrock 828fa9e4066Sahrens 829fa9e4066Sahrens return (ret); 830fa9e4066Sahrens } 831fa9e4066Sahrens 83299653d4eSeschrock boolean_t 833fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid) 834fa9e4066Sahrens { 835fa9e4066Sahrens uint64_t tmp; 836fa9e4066Sahrens nvlist_t **child; 837fa9e4066Sahrens uint_t c, children; 838fa9e4066Sahrens 839fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0); 840fa9e4066Sahrens if (tmp == guid) 84199653d4eSeschrock return (B_TRUE); 842fa9e4066Sahrens 843fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 844fa9e4066Sahrens &child, &children) == 0) { 845fa9e4066Sahrens for (c = 0; c < children; c++) 846fa9e4066Sahrens if (find_guid(child[c], guid)) 84799653d4eSeschrock return (B_TRUE); 84899653d4eSeschrock } 84999653d4eSeschrock 85099653d4eSeschrock return (B_FALSE); 85199653d4eSeschrock } 85299653d4eSeschrock 85399653d4eSeschrock typedef struct spare_cbdata { 85499653d4eSeschrock uint64_t cb_guid; 85599653d4eSeschrock zpool_handle_t *cb_zhp; 85699653d4eSeschrock } spare_cbdata_t; 85799653d4eSeschrock 85899653d4eSeschrock static int 85999653d4eSeschrock find_spare(zpool_handle_t *zhp, void *data) 86099653d4eSeschrock { 86199653d4eSeschrock spare_cbdata_t *cbp = data; 86299653d4eSeschrock nvlist_t **spares; 86399653d4eSeschrock uint_t i, nspares; 86499653d4eSeschrock uint64_t guid; 86599653d4eSeschrock nvlist_t *nvroot; 86699653d4eSeschrock 86799653d4eSeschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 86899653d4eSeschrock &nvroot) == 0); 86999653d4eSeschrock 87099653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 87199653d4eSeschrock &spares, &nspares) == 0) { 87299653d4eSeschrock for (i = 0; i < nspares; i++) { 87399653d4eSeschrock verify(nvlist_lookup_uint64(spares[i], 87499653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 87599653d4eSeschrock if (guid == cbp->cb_guid) { 87699653d4eSeschrock cbp->cb_zhp = zhp; 87799653d4eSeschrock return (1); 87899653d4eSeschrock } 87999653d4eSeschrock } 880fa9e4066Sahrens } 881fa9e4066Sahrens 88299653d4eSeschrock zpool_close(zhp); 88399653d4eSeschrock return (0); 884fa9e4066Sahrens } 885fa9e4066Sahrens 886fa9e4066Sahrens /* 88799653d4eSeschrock * Determines if the pool is in use. If so, it returns true and the state of 888fa9e4066Sahrens * the pool as well as the name of the pool. Both strings are allocated and 889fa9e4066Sahrens * must be freed by the caller. 890fa9e4066Sahrens */ 891fa9e4066Sahrens int 89299653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr, 89399653d4eSeschrock boolean_t *inuse) 894fa9e4066Sahrens { 895fa9e4066Sahrens nvlist_t *config; 896fa9e4066Sahrens char *name; 89799653d4eSeschrock boolean_t ret; 898fa9e4066Sahrens uint64_t guid, vdev_guid; 899fa9e4066Sahrens zpool_handle_t *zhp; 900fa9e4066Sahrens nvlist_t *pool_config; 901*39c23413Seschrock uint64_t stateval, isspare; 90299653d4eSeschrock spare_cbdata_t cb = { 0 }; 90394de1d4cSeschrock boolean_t isactive; 90499653d4eSeschrock 90599653d4eSeschrock *inuse = B_FALSE; 906fa9e4066Sahrens 90799653d4eSeschrock if (zpool_read_label(fd, &config) != 0) { 90899653d4eSeschrock (void) no_memory(hdl); 90999653d4eSeschrock return (-1); 91099653d4eSeschrock } 91199653d4eSeschrock 91299653d4eSeschrock if (config == NULL) 91399653d4eSeschrock return (0); 914fa9e4066Sahrens 915fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 91646a2abf2Seschrock &stateval) == 0); 917fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 918fa9e4066Sahrens &vdev_guid) == 0); 919fa9e4066Sahrens 92099653d4eSeschrock if (stateval != POOL_STATE_SPARE) { 92199653d4eSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 92299653d4eSeschrock &name) == 0); 92399653d4eSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 92499653d4eSeschrock &guid) == 0); 92599653d4eSeschrock } 92699653d4eSeschrock 92746a2abf2Seschrock switch (stateval) { 928fa9e4066Sahrens case POOL_STATE_EXPORTED: 92999653d4eSeschrock ret = B_TRUE; 930fa9e4066Sahrens break; 931fa9e4066Sahrens 932fa9e4066Sahrens case POOL_STATE_ACTIVE: 933fa9e4066Sahrens /* 934fa9e4066Sahrens * For an active pool, we have to determine if it's really part 935eaca9bbdSeschrock * of a currently active pool (in which case the pool will exist 936eaca9bbdSeschrock * and the guid will be the same), or whether it's part of an 937eaca9bbdSeschrock * active pool that was disconnected without being explicitly 938eaca9bbdSeschrock * exported. 939fa9e4066Sahrens */ 94094de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) { 94194de1d4cSeschrock nvlist_free(config); 94294de1d4cSeschrock return (-1); 94394de1d4cSeschrock } 94494de1d4cSeschrock 94594de1d4cSeschrock if (isactive) { 946fa9e4066Sahrens /* 947fa9e4066Sahrens * Because the device may have been removed while 948fa9e4066Sahrens * offlined, we only report it as active if the vdev is 949fa9e4066Sahrens * still present in the config. Otherwise, pretend like 950fa9e4066Sahrens * it's not in use. 951fa9e4066Sahrens */ 95299653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, name)) != NULL && 953088e9d47Seschrock (pool_config = zpool_get_config(zhp, NULL)) 954088e9d47Seschrock != NULL) { 955fa9e4066Sahrens nvlist_t *nvroot; 956fa9e4066Sahrens 957fa9e4066Sahrens verify(nvlist_lookup_nvlist(pool_config, 958fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 95946a2abf2Seschrock ret = find_guid(nvroot, vdev_guid); 960fa9e4066Sahrens } else { 96199653d4eSeschrock ret = B_FALSE; 962fa9e4066Sahrens } 96399653d4eSeschrock 964*39c23413Seschrock /* 965*39c23413Seschrock * If this is an active spare within another pool, we 966*39c23413Seschrock * treat it like an unused hot spare. This allows the 967*39c23413Seschrock * user to create a pool with a hot spare that currently 968*39c23413Seschrock * in use within another pool. Since we return B_TRUE, 969*39c23413Seschrock * libdiskmgt will continue to prevent generic consumers 970*39c23413Seschrock * from using the device. 971*39c23413Seschrock */ 972*39c23413Seschrock if (ret && nvlist_lookup_uint64(config, 973*39c23413Seschrock ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) 974*39c23413Seschrock stateval = POOL_STATE_SPARE; 975*39c23413Seschrock 97699653d4eSeschrock if (zhp != NULL) 97799653d4eSeschrock zpool_close(zhp); 978fa9e4066Sahrens } else { 97946a2abf2Seschrock stateval = POOL_STATE_POTENTIALLY_ACTIVE; 98099653d4eSeschrock ret = B_TRUE; 98199653d4eSeschrock } 98299653d4eSeschrock break; 98399653d4eSeschrock 98499653d4eSeschrock case POOL_STATE_SPARE: 98599653d4eSeschrock /* 98699653d4eSeschrock * For a hot spare, it can be either definitively in use, or 98799653d4eSeschrock * potentially active. To determine if it's in use, we iterate 98899653d4eSeschrock * over all pools in the system and search for one with a spare 98999653d4eSeschrock * with a matching guid. 99099653d4eSeschrock * 99199653d4eSeschrock * Due to the shared nature of spares, we don't actually report 99299653d4eSeschrock * the potentially active case as in use. This means the user 99399653d4eSeschrock * can freely create pools on the hot spares of exported pools, 99499653d4eSeschrock * but to do otherwise makes the resulting code complicated, and 99599653d4eSeschrock * we end up having to deal with this case anyway. 99699653d4eSeschrock */ 99799653d4eSeschrock cb.cb_zhp = NULL; 99899653d4eSeschrock cb.cb_guid = vdev_guid; 99999653d4eSeschrock if (zpool_iter(hdl, find_spare, &cb) == 1) { 100099653d4eSeschrock name = (char *)zpool_get_name(cb.cb_zhp); 1001fa9e4066Sahrens ret = TRUE; 100299653d4eSeschrock } else { 100399653d4eSeschrock ret = FALSE; 1004fa9e4066Sahrens } 1005fa9e4066Sahrens break; 1006fa9e4066Sahrens 1007fa9e4066Sahrens default: 100899653d4eSeschrock ret = B_FALSE; 1009fa9e4066Sahrens } 1010fa9e4066Sahrens 101146a2abf2Seschrock 101246a2abf2Seschrock if (ret) { 101399653d4eSeschrock if ((*namestr = zfs_strdup(hdl, name)) == NULL) { 101499653d4eSeschrock nvlist_free(config); 101599653d4eSeschrock return (-1); 101699653d4eSeschrock } 101746a2abf2Seschrock *state = (pool_state_t)stateval; 101846a2abf2Seschrock } 101946a2abf2Seschrock 102099653d4eSeschrock if (cb.cb_zhp) 102199653d4eSeschrock zpool_close(cb.cb_zhp); 102299653d4eSeschrock 1023fa9e4066Sahrens nvlist_free(config); 102499653d4eSeschrock *inuse = ret; 102599653d4eSeschrock return (0); 1026fa9e4066Sahrens } 1027