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 /* 2239c23413Seschrock * 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 /* 216fa94a07fSbrendan * If this is a hot spare not currently in use or level 2 cache 217fa94a07fSbrendan * device, add it to the list of names to translate, but don't do 218fa94a07fSbrendan * anything else. 21999653d4eSeschrock */ 22099653d4eSeschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 221fa94a07fSbrendan &state) == 0 && 222fa94a07fSbrendan (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && 22399653d4eSeschrock nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { 22499653d4eSeschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) 225ccae0b50Seschrock return (-1); 22699653d4eSeschrock 22799653d4eSeschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { 22899653d4eSeschrock free(ne); 22999653d4eSeschrock return (-1); 23099653d4eSeschrock } 23199653d4eSeschrock ne->ne_guid = vdev_guid; 23299653d4eSeschrock ne->ne_next = pl->names; 23399653d4eSeschrock pl->names = ne; 23499653d4eSeschrock return (0); 23599653d4eSeschrock } 23699653d4eSeschrock 237fa9e4066Sahrens /* 238fa9e4066Sahrens * If we have a valid config but cannot read any of these fields, then 239fa9e4066Sahrens * it means we have a half-initialized label. In vdev_label_init() 240fa9e4066Sahrens * we write a label with txg == 0 so that we can identify the device 241fa9e4066Sahrens * in case the user refers to the same disk later on. If we fail to 242fa9e4066Sahrens * create the pool, we'll be left with a label in this state 243fa9e4066Sahrens * which should not be considered part of a valid pool. 244fa9e4066Sahrens */ 245fa9e4066Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 246fa9e4066Sahrens &pool_guid) != 0 || 247fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 248fa9e4066Sahrens &vdev_guid) != 0 || 249fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, 250fa9e4066Sahrens &top_guid) != 0 || 251fa9e4066Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 252fa9e4066Sahrens &txg) != 0 || txg == 0) { 253fa9e4066Sahrens nvlist_free(config); 25499653d4eSeschrock return (0); 255fa9e4066Sahrens } 256fa9e4066Sahrens 257fa9e4066Sahrens /* 258fa9e4066Sahrens * First, see if we know about this pool. If not, then add it to the 259fa9e4066Sahrens * list of known pools. 260fa9e4066Sahrens */ 261fa9e4066Sahrens for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 262fa9e4066Sahrens if (pe->pe_guid == pool_guid) 263fa9e4066Sahrens break; 264fa9e4066Sahrens } 265fa9e4066Sahrens 266fa9e4066Sahrens if (pe == NULL) { 26799653d4eSeschrock if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) { 26899653d4eSeschrock nvlist_free(config); 26999653d4eSeschrock return (-1); 27099653d4eSeschrock } 271fa9e4066Sahrens pe->pe_guid = pool_guid; 272fa9e4066Sahrens pe->pe_next = pl->pools; 273fa9e4066Sahrens pl->pools = pe; 274fa9e4066Sahrens } 275fa9e4066Sahrens 276fa9e4066Sahrens /* 277fa9e4066Sahrens * Second, see if we know about this toplevel vdev. Add it if its 278fa9e4066Sahrens * missing. 279fa9e4066Sahrens */ 280fa9e4066Sahrens for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 281fa9e4066Sahrens if (ve->ve_guid == top_guid) 282fa9e4066Sahrens break; 283fa9e4066Sahrens } 284fa9e4066Sahrens 285fa9e4066Sahrens if (ve == NULL) { 28699653d4eSeschrock if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { 28799653d4eSeschrock nvlist_free(config); 28899653d4eSeschrock return (-1); 28999653d4eSeschrock } 290fa9e4066Sahrens ve->ve_guid = top_guid; 291fa9e4066Sahrens ve->ve_next = pe->pe_vdevs; 292fa9e4066Sahrens pe->pe_vdevs = ve; 293fa9e4066Sahrens } 294fa9e4066Sahrens 295fa9e4066Sahrens /* 296fa9e4066Sahrens * Third, see if we have a config with a matching transaction group. If 297fa9e4066Sahrens * so, then we do nothing. Otherwise, add it to the list of known 298fa9e4066Sahrens * configs. 299fa9e4066Sahrens */ 300fa9e4066Sahrens for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { 301fa9e4066Sahrens if (ce->ce_txg == txg) 302fa9e4066Sahrens break; 303fa9e4066Sahrens } 304fa9e4066Sahrens 305fa9e4066Sahrens if (ce == NULL) { 30699653d4eSeschrock if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) { 30799653d4eSeschrock nvlist_free(config); 30899653d4eSeschrock return (-1); 30999653d4eSeschrock } 310fa9e4066Sahrens ce->ce_txg = txg; 311fa9e4066Sahrens ce->ce_config = config; 312fa9e4066Sahrens ce->ce_next = ve->ve_configs; 313fa9e4066Sahrens ve->ve_configs = ce; 314fa9e4066Sahrens } else { 315fa9e4066Sahrens nvlist_free(config); 316fa9e4066Sahrens } 317fa9e4066Sahrens 318fa9e4066Sahrens /* 319fa9e4066Sahrens * At this point we've successfully added our config to the list of 320fa9e4066Sahrens * known configs. The last thing to do is add the vdev guid -> path 321fa9e4066Sahrens * mappings so that we can fix up the configuration as necessary before 322fa9e4066Sahrens * doing the import. 323fa9e4066Sahrens */ 32499653d4eSeschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) 32599653d4eSeschrock return (-1); 32699653d4eSeschrock 32799653d4eSeschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { 32899653d4eSeschrock free(ne); 32999653d4eSeschrock return (-1); 33099653d4eSeschrock } 331fa9e4066Sahrens 332fa9e4066Sahrens ne->ne_guid = vdev_guid; 333fa9e4066Sahrens ne->ne_next = pl->names; 334fa9e4066Sahrens pl->names = ne; 33599653d4eSeschrock 33699653d4eSeschrock return (0); 337fa9e4066Sahrens } 338fa9e4066Sahrens 339eaca9bbdSeschrock /* 340eaca9bbdSeschrock * Returns true if the named pool matches the given GUID. 341eaca9bbdSeschrock */ 34294de1d4cSeschrock static int 34394de1d4cSeschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid, 34494de1d4cSeschrock boolean_t *isactive) 345eaca9bbdSeschrock { 346eaca9bbdSeschrock zpool_handle_t *zhp; 347eaca9bbdSeschrock uint64_t theguid; 348eaca9bbdSeschrock 34994de1d4cSeschrock if (zpool_open_silent(hdl, name, &zhp) != 0) 35094de1d4cSeschrock return (-1); 35194de1d4cSeschrock 35294de1d4cSeschrock if (zhp == NULL) { 35394de1d4cSeschrock *isactive = B_FALSE; 35494de1d4cSeschrock return (0); 35594de1d4cSeschrock } 356eaca9bbdSeschrock 357eaca9bbdSeschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID, 358eaca9bbdSeschrock &theguid) == 0); 359eaca9bbdSeschrock 360eaca9bbdSeschrock zpool_close(zhp); 361eaca9bbdSeschrock 36294de1d4cSeschrock *isactive = (theguid == guid); 36394de1d4cSeschrock return (0); 364eaca9bbdSeschrock } 365eaca9bbdSeschrock 3662f8aaab3Seschrock static nvlist_t * 3672f8aaab3Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config) 3682f8aaab3Seschrock { 3692f8aaab3Seschrock nvlist_t *nvl; 3702f8aaab3Seschrock zfs_cmd_t zc = { 0 }; 3712f8aaab3Seschrock int err; 3722f8aaab3Seschrock 3732f8aaab3Seschrock if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) 3742f8aaab3Seschrock return (NULL); 3752f8aaab3Seschrock 3762f8aaab3Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 3772f8aaab3Seschrock zc.zc_nvlist_conf_size * 2) != 0) { 3782f8aaab3Seschrock zcmd_free_nvlists(&zc); 3792f8aaab3Seschrock return (NULL); 3802f8aaab3Seschrock } 3812f8aaab3Seschrock 3822f8aaab3Seschrock while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT, 3832f8aaab3Seschrock &zc)) != 0 && errno == ENOMEM) { 3842f8aaab3Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 3852f8aaab3Seschrock zcmd_free_nvlists(&zc); 3862f8aaab3Seschrock return (NULL); 3872f8aaab3Seschrock } 3882f8aaab3Seschrock } 3892f8aaab3Seschrock 3902f8aaab3Seschrock if (err) { 3912f8aaab3Seschrock (void) zpool_standard_error(hdl, errno, 3922f8aaab3Seschrock dgettext(TEXT_DOMAIN, "cannot discover pools")); 3932f8aaab3Seschrock zcmd_free_nvlists(&zc); 3942f8aaab3Seschrock return (NULL); 3952f8aaab3Seschrock } 3962f8aaab3Seschrock 3972f8aaab3Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) { 3982f8aaab3Seschrock zcmd_free_nvlists(&zc); 3992f8aaab3Seschrock return (NULL); 4002f8aaab3Seschrock } 4012f8aaab3Seschrock 4022f8aaab3Seschrock zcmd_free_nvlists(&zc); 4032f8aaab3Seschrock return (nvl); 4042f8aaab3Seschrock } 4052f8aaab3Seschrock 406fa9e4066Sahrens /* 407fa9e4066Sahrens * Convert our list of pools into the definitive set of configurations. We 408fa9e4066Sahrens * start by picking the best config for each toplevel vdev. Once that's done, 409fa9e4066Sahrens * we assemble the toplevel vdevs into a full config for the pool. We make a 410fa9e4066Sahrens * pass to fix up any incorrect paths, and then add it to the main list to 411fa9e4066Sahrens * return to the user. 412fa9e4066Sahrens */ 413fa9e4066Sahrens static nvlist_t * 41499653d4eSeschrock get_configs(libzfs_handle_t *hdl, pool_list_t *pl) 415fa9e4066Sahrens { 41699653d4eSeschrock pool_entry_t *pe; 41799653d4eSeschrock vdev_entry_t *ve; 41899653d4eSeschrock config_entry_t *ce; 41999653d4eSeschrock nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot; 420fa94a07fSbrendan nvlist_t **spares, **l2cache; 421fa94a07fSbrendan uint_t i, nspares, nl2cache; 42299653d4eSeschrock boolean_t config_seen; 423fa9e4066Sahrens uint64_t best_txg; 42495173954Sek char *name, *hostname; 42599653d4eSeschrock uint64_t version, guid; 42699653d4eSeschrock uint_t children = 0; 42799653d4eSeschrock nvlist_t **child = NULL; 42899653d4eSeschrock uint_t c; 42994de1d4cSeschrock boolean_t isactive; 43095173954Sek uint64_t hostid; 4312f8aaab3Seschrock nvlist_t *nvl; 432fa9e4066Sahrens 43399653d4eSeschrock if (nvlist_alloc(&ret, 0, 0) != 0) 43499653d4eSeschrock goto nomem; 435fa9e4066Sahrens 43699653d4eSeschrock for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { 437fa9e4066Sahrens uint64_t id; 438fa9e4066Sahrens 43999653d4eSeschrock if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) 44099653d4eSeschrock goto nomem; 44199653d4eSeschrock config_seen = B_FALSE; 442fa9e4066Sahrens 443fa9e4066Sahrens /* 444fa9e4066Sahrens * Iterate over all toplevel vdevs. Grab the pool configuration 445fa9e4066Sahrens * from the first one we find, and then go through the rest and 446fa9e4066Sahrens * add them as necessary to the 'vdevs' member of the config. 447fa9e4066Sahrens */ 44899653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { 449fa9e4066Sahrens 450fa9e4066Sahrens /* 451fa9e4066Sahrens * Determine the best configuration for this vdev by 452fa9e4066Sahrens * selecting the config with the latest transaction 453fa9e4066Sahrens * group. 454fa9e4066Sahrens */ 455fa9e4066Sahrens best_txg = 0; 456fa9e4066Sahrens for (ce = ve->ve_configs; ce != NULL; 457fa9e4066Sahrens ce = ce->ce_next) { 458fa9e4066Sahrens 45999653d4eSeschrock if (ce->ce_txg > best_txg) { 460fa9e4066Sahrens tmp = ce->ce_config; 46199653d4eSeschrock best_txg = ce->ce_txg; 46299653d4eSeschrock } 463fa9e4066Sahrens } 464fa9e4066Sahrens 465fa9e4066Sahrens if (!config_seen) { 466fa9e4066Sahrens /* 467fa9e4066Sahrens * Copy the relevant pieces of data to the pool 468fa9e4066Sahrens * configuration: 469fa9e4066Sahrens * 47099653d4eSeschrock * version 471fa9e4066Sahrens * pool guid 472fa9e4066Sahrens * name 473fa9e4066Sahrens * pool state 47495173954Sek * hostid (if available) 47595173954Sek * hostname (if available) 476fa9e4066Sahrens */ 477fa9e4066Sahrens uint64_t state; 478fa9e4066Sahrens 47999653d4eSeschrock verify(nvlist_lookup_uint64(tmp, 48099653d4eSeschrock ZPOOL_CONFIG_VERSION, &version) == 0); 48199653d4eSeschrock if (nvlist_add_uint64(config, 48299653d4eSeschrock ZPOOL_CONFIG_VERSION, version) != 0) 48399653d4eSeschrock goto nomem; 484fa9e4066Sahrens verify(nvlist_lookup_uint64(tmp, 485fa9e4066Sahrens ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 48699653d4eSeschrock if (nvlist_add_uint64(config, 48799653d4eSeschrock ZPOOL_CONFIG_POOL_GUID, guid) != 0) 48899653d4eSeschrock goto nomem; 489fa9e4066Sahrens verify(nvlist_lookup_string(tmp, 490fa9e4066Sahrens ZPOOL_CONFIG_POOL_NAME, &name) == 0); 49199653d4eSeschrock if (nvlist_add_string(config, 49299653d4eSeschrock ZPOOL_CONFIG_POOL_NAME, name) != 0) 49399653d4eSeschrock goto nomem; 494fa9e4066Sahrens verify(nvlist_lookup_uint64(tmp, 495fa9e4066Sahrens ZPOOL_CONFIG_POOL_STATE, &state) == 0); 49699653d4eSeschrock if (nvlist_add_uint64(config, 49799653d4eSeschrock ZPOOL_CONFIG_POOL_STATE, state) != 0) 49899653d4eSeschrock goto nomem; 49995173954Sek hostid = 0; 50095173954Sek if (nvlist_lookup_uint64(tmp, 50195173954Sek ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 50295173954Sek if (nvlist_add_uint64(config, 50395173954Sek ZPOOL_CONFIG_HOSTID, hostid) != 0) 50495173954Sek goto nomem; 50595173954Sek verify(nvlist_lookup_string(tmp, 50695173954Sek ZPOOL_CONFIG_HOSTNAME, 50795173954Sek &hostname) == 0); 50895173954Sek if (nvlist_add_string(config, 50995173954Sek ZPOOL_CONFIG_HOSTNAME, 51095173954Sek hostname) != 0) 51195173954Sek goto nomem; 51295173954Sek } 513fa9e4066Sahrens 51499653d4eSeschrock config_seen = B_TRUE; 515fa9e4066Sahrens } 516fa9e4066Sahrens 517fa9e4066Sahrens /* 518fa9e4066Sahrens * Add this top-level vdev to the child array. 519fa9e4066Sahrens */ 520fa9e4066Sahrens verify(nvlist_lookup_nvlist(tmp, 521fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); 522fa9e4066Sahrens verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, 523fa9e4066Sahrens &id) == 0); 524fa9e4066Sahrens if (id >= children) { 525fa9e4066Sahrens nvlist_t **newchild; 526fa9e4066Sahrens 52799653d4eSeschrock newchild = zfs_alloc(hdl, (id + 1) * 528fa9e4066Sahrens sizeof (nvlist_t *)); 52999653d4eSeschrock if (newchild == NULL) 53099653d4eSeschrock goto nomem; 531fa9e4066Sahrens 532fa9e4066Sahrens for (c = 0; c < children; c++) 533fa9e4066Sahrens newchild[c] = child[c]; 534fa9e4066Sahrens 535fa9e4066Sahrens free(child); 536fa9e4066Sahrens child = newchild; 537fa9e4066Sahrens children = id + 1; 538fa9e4066Sahrens } 53999653d4eSeschrock if (nvlist_dup(nvtop, &child[id], 0) != 0) 54099653d4eSeschrock goto nomem; 541fa9e4066Sahrens 542fa9e4066Sahrens } 543fa9e4066Sahrens 544fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 545fa9e4066Sahrens &guid) == 0); 546fa9e4066Sahrens 547fa9e4066Sahrens /* 548fa9e4066Sahrens * Look for any missing top-level vdevs. If this is the case, 549fa9e4066Sahrens * create a faked up 'missing' vdev as a placeholder. We cannot 550fa9e4066Sahrens * simply compress the child array, because the kernel performs 551fa9e4066Sahrens * certain checks to make sure the vdev IDs match their location 552fa9e4066Sahrens * in the configuration. 553fa9e4066Sahrens */ 554fa9e4066Sahrens for (c = 0; c < children; c++) 555fa9e4066Sahrens if (child[c] == NULL) { 556fa9e4066Sahrens nvlist_t *missing; 55799653d4eSeschrock if (nvlist_alloc(&missing, NV_UNIQUE_NAME, 55899653d4eSeschrock 0) != 0) 55999653d4eSeschrock goto nomem; 56099653d4eSeschrock if (nvlist_add_string(missing, 56199653d4eSeschrock ZPOOL_CONFIG_TYPE, 56299653d4eSeschrock VDEV_TYPE_MISSING) != 0 || 56399653d4eSeschrock nvlist_add_uint64(missing, 56499653d4eSeschrock ZPOOL_CONFIG_ID, c) != 0 || 56599653d4eSeschrock nvlist_add_uint64(missing, 56699653d4eSeschrock ZPOOL_CONFIG_GUID, 0ULL) != 0) { 56799653d4eSeschrock nvlist_free(missing); 56899653d4eSeschrock goto nomem; 56999653d4eSeschrock } 570fa9e4066Sahrens child[c] = missing; 571fa9e4066Sahrens } 572fa9e4066Sahrens 573fa9e4066Sahrens /* 574fa9e4066Sahrens * Put all of this pool's top-level vdevs into a root vdev. 575fa9e4066Sahrens */ 57699653d4eSeschrock if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 57799653d4eSeschrock goto nomem; 57899653d4eSeschrock if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 57999653d4eSeschrock VDEV_TYPE_ROOT) != 0 || 58099653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || 58199653d4eSeschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || 58299653d4eSeschrock nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 58399653d4eSeschrock child, children) != 0) { 58499653d4eSeschrock nvlist_free(nvroot); 58599653d4eSeschrock goto nomem; 58699653d4eSeschrock } 587fa9e4066Sahrens 588fa9e4066Sahrens for (c = 0; c < children; c++) 589fa9e4066Sahrens nvlist_free(child[c]); 590fa9e4066Sahrens free(child); 59199653d4eSeschrock children = 0; 59299653d4eSeschrock child = NULL; 593fa9e4066Sahrens 594fa9e4066Sahrens /* 595fa9e4066Sahrens * Go through and fix up any paths and/or devids based on our 596fa9e4066Sahrens * known list of vdev GUID -> path mappings. 597fa9e4066Sahrens */ 59899653d4eSeschrock if (fix_paths(nvroot, pl->names) != 0) { 59999653d4eSeschrock nvlist_free(nvroot); 60099653d4eSeschrock goto nomem; 60199653d4eSeschrock } 602fa9e4066Sahrens 603fa9e4066Sahrens /* 604fa9e4066Sahrens * Add the root vdev to this pool's configuration. 605fa9e4066Sahrens */ 60699653d4eSeschrock if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 60799653d4eSeschrock nvroot) != 0) { 60899653d4eSeschrock nvlist_free(nvroot); 60999653d4eSeschrock goto nomem; 61099653d4eSeschrock } 611fa9e4066Sahrens nvlist_free(nvroot); 612fa9e4066Sahrens 613fa9e4066Sahrens /* 614fa9e4066Sahrens * Determine if this pool is currently active, in which case we 615fa9e4066Sahrens * can't actually import it. 616fa9e4066Sahrens */ 617fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 618fa9e4066Sahrens &name) == 0); 619fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 620fa9e4066Sahrens &guid) == 0); 621fa9e4066Sahrens 62294de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) 62394de1d4cSeschrock goto error; 62494de1d4cSeschrock 6250192a278Seschrock if (isactive) { 626fa9e4066Sahrens nvlist_free(config); 62799653d4eSeschrock config = NULL; 628fa9e4066Sahrens continue; 629fa9e4066Sahrens } 630fa9e4066Sahrens 6312f8aaab3Seschrock if ((nvl = refresh_config(hdl, config)) == NULL) 632e9dbad6fSeschrock goto error; 633fa9e4066Sahrens 634fa9e4066Sahrens nvlist_free(config); 6352f8aaab3Seschrock config = nvl; 636fa9e4066Sahrens 63799653d4eSeschrock /* 63899653d4eSeschrock * Go through and update the paths for spares, now that we have 63999653d4eSeschrock * them. 64099653d4eSeschrock */ 64199653d4eSeschrock verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 64299653d4eSeschrock &nvroot) == 0); 64399653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 64499653d4eSeschrock &spares, &nspares) == 0) { 64599653d4eSeschrock for (i = 0; i < nspares; i++) { 64699653d4eSeschrock if (fix_paths(spares[i], pl->names) != 0) 64799653d4eSeschrock goto nomem; 64899653d4eSeschrock } 64999653d4eSeschrock } 65099653d4eSeschrock 651fa94a07fSbrendan /* 652fa94a07fSbrendan * Update the paths for l2cache devices. 653fa94a07fSbrendan */ 654fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 655fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 656fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 657fa94a07fSbrendan if (fix_paths(l2cache[i], pl->names) != 0) 658fa94a07fSbrendan goto nomem; 659fa94a07fSbrendan } 660fa94a07fSbrendan } 661fa94a07fSbrendan 66295173954Sek /* 66395173954Sek * Restore the original information read from the actual label. 66495173954Sek */ 66595173954Sek (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, 66695173954Sek DATA_TYPE_UINT64); 66795173954Sek (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, 66895173954Sek DATA_TYPE_STRING); 66995173954Sek if (hostid != 0) { 67095173954Sek verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 67195173954Sek hostid) == 0); 67295173954Sek verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 67395173954Sek hostname) == 0); 67495173954Sek } 67595173954Sek 676fa9e4066Sahrens /* 677fa9e4066Sahrens * Add this pool to the list of configs. 678fa9e4066Sahrens */ 679e9dbad6fSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 680e9dbad6fSeschrock &name) == 0); 68199653d4eSeschrock if (nvlist_add_nvlist(ret, name, config) != 0) 68299653d4eSeschrock goto nomem; 683fa9e4066Sahrens 684fa9e4066Sahrens nvlist_free(config); 68599653d4eSeschrock config = NULL; 686fa9e4066Sahrens } 687fa9e4066Sahrens 688fa9e4066Sahrens return (ret); 68999653d4eSeschrock 69099653d4eSeschrock nomem: 69199653d4eSeschrock (void) no_memory(hdl); 69299653d4eSeschrock error: 69394de1d4cSeschrock nvlist_free(config); 69494de1d4cSeschrock nvlist_free(ret); 69599653d4eSeschrock for (c = 0; c < children; c++) 69699653d4eSeschrock nvlist_free(child[c]); 69794de1d4cSeschrock free(child); 69899653d4eSeschrock 69999653d4eSeschrock return (NULL); 700fa9e4066Sahrens } 701fa9e4066Sahrens 702fa9e4066Sahrens /* 703fa9e4066Sahrens * Return the offset of the given label. 704fa9e4066Sahrens */ 705fa9e4066Sahrens static uint64_t 706e7437265Sahrens label_offset(uint64_t size, int l) 707fa9e4066Sahrens { 708e7437265Sahrens ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); 709fa9e4066Sahrens return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 710fa9e4066Sahrens 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); 711fa9e4066Sahrens } 712fa9e4066Sahrens 713fa9e4066Sahrens /* 714fa9e4066Sahrens * Given a file descriptor, read the label information and return an nvlist 715fa9e4066Sahrens * describing the configuration, if there is one. 716fa9e4066Sahrens */ 71799653d4eSeschrock int 71899653d4eSeschrock zpool_read_label(int fd, nvlist_t **config) 719fa9e4066Sahrens { 720fa9e4066Sahrens struct stat64 statbuf; 721fa9e4066Sahrens int l; 722fa9e4066Sahrens vdev_label_t *label; 723e7437265Sahrens uint64_t state, txg, size; 724fa9e4066Sahrens 72599653d4eSeschrock *config = NULL; 72699653d4eSeschrock 727fa9e4066Sahrens if (fstat64(fd, &statbuf) == -1) 72899653d4eSeschrock return (0); 729e7437265Sahrens size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 730fa9e4066Sahrens 73199653d4eSeschrock if ((label = malloc(sizeof (vdev_label_t))) == NULL) 73299653d4eSeschrock return (-1); 733fa9e4066Sahrens 734fa9e4066Sahrens for (l = 0; l < VDEV_LABELS; l++) { 735fa9e4066Sahrens if (pread(fd, label, sizeof (vdev_label_t), 736e7437265Sahrens label_offset(size, l)) != sizeof (vdev_label_t)) 737fa9e4066Sahrens continue; 738fa9e4066Sahrens 739fa9e4066Sahrens if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 74099653d4eSeschrock sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) 741fa9e4066Sahrens continue; 742fa9e4066Sahrens 74399653d4eSeschrock if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 744fa94a07fSbrendan &state) != 0 || state > POOL_STATE_L2CACHE) { 74599653d4eSeschrock nvlist_free(*config); 746fa9e4066Sahrens continue; 747fa9e4066Sahrens } 748fa9e4066Sahrens 749fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 75099653d4eSeschrock (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 75199653d4eSeschrock &txg) != 0 || txg == 0)) { 75299653d4eSeschrock nvlist_free(*config); 753fa9e4066Sahrens continue; 754fa9e4066Sahrens } 755fa9e4066Sahrens 756fa9e4066Sahrens free(label); 75799653d4eSeschrock return (0); 758fa9e4066Sahrens } 759fa9e4066Sahrens 760fa9e4066Sahrens free(label); 76199653d4eSeschrock *config = NULL; 76299653d4eSeschrock return (0); 763fa9e4066Sahrens } 764fa9e4066Sahrens 765fa9e4066Sahrens /* 766fa9e4066Sahrens * Given a list of directories to search, find all pools stored on disk. This 767fa9e4066Sahrens * includes partial pools which are not available to import. If no args are 768fa9e4066Sahrens * given (argc is 0), then the default directory (/dev/dsk) is searched. 769fa9e4066Sahrens */ 770fa9e4066Sahrens nvlist_t * 77199653d4eSeschrock zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv) 772fa9e4066Sahrens { 773fa9e4066Sahrens int i; 774ccae0b50Seschrock DIR *dirp = NULL; 775fa9e4066Sahrens struct dirent64 *dp; 776fa9e4066Sahrens char path[MAXPATHLEN]; 777fa9e4066Sahrens struct stat64 statbuf; 77899653d4eSeschrock nvlist_t *ret = NULL, *config; 779fa9e4066Sahrens static char *default_dir = "/dev/dsk"; 780fa9e4066Sahrens int fd; 781fa9e4066Sahrens pool_list_t pools = { 0 }; 78299653d4eSeschrock pool_entry_t *pe, *penext; 78399653d4eSeschrock vdev_entry_t *ve, *venext; 78499653d4eSeschrock config_entry_t *ce, *cenext; 78599653d4eSeschrock name_entry_t *ne, *nenext; 78699653d4eSeschrock 787fa9e4066Sahrens 788fa9e4066Sahrens if (argc == 0) { 789fa9e4066Sahrens argc = 1; 790fa9e4066Sahrens argv = &default_dir; 791fa9e4066Sahrens } 792fa9e4066Sahrens 793fa9e4066Sahrens /* 794fa9e4066Sahrens * Go through and read the label configuration information from every 795fa9e4066Sahrens * possible device, organizing the information according to pool GUID 796fa9e4066Sahrens * and toplevel GUID. 797fa9e4066Sahrens */ 798fa9e4066Sahrens for (i = 0; i < argc; i++) { 799fa9e4066Sahrens if (argv[i][0] != '/') { 800ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 80199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 802fa9e4066Sahrens argv[i]); 80399653d4eSeschrock goto error; 804fa9e4066Sahrens } 805fa9e4066Sahrens 806fa9e4066Sahrens if ((dirp = opendir(argv[i])) == NULL) { 80799653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 808ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH, 80999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 81099653d4eSeschrock argv[i]); 81199653d4eSeschrock goto error; 812fa9e4066Sahrens } 813fa9e4066Sahrens 814fa9e4066Sahrens /* 815fa9e4066Sahrens * This is not MT-safe, but we have no MT consumers of libzfs 816fa9e4066Sahrens */ 817fa9e4066Sahrens while ((dp = readdir64(dirp)) != NULL) { 818fa9e4066Sahrens 819fa9e4066Sahrens (void) snprintf(path, sizeof (path), "%s/%s", 820fa9e4066Sahrens argv[i], dp->d_name); 821fa9e4066Sahrens 822fa9e4066Sahrens if (stat64(path, &statbuf) != 0) 823fa9e4066Sahrens continue; 824fa9e4066Sahrens 825fa9e4066Sahrens /* 826fa9e4066Sahrens * Ignore directories (which includes "." and ".."). 827fa9e4066Sahrens */ 828fa9e4066Sahrens if (S_ISDIR(statbuf.st_mode)) 829fa9e4066Sahrens continue; 8303bb79becSeschrock 8313bb79becSeschrock /* 8323bb79becSeschrock * Ignore special (non-character or non-block) files. 8333bb79becSeschrock */ 8343bb79becSeschrock if (!S_ISREG(statbuf.st_mode) && 8353bb79becSeschrock !S_ISBLK(statbuf.st_mode)) 8363bb79becSeschrock continue; 837fa9e4066Sahrens 838fa9e4066Sahrens if ((fd = open64(path, O_RDONLY)) < 0) 839fa9e4066Sahrens continue; 840fa9e4066Sahrens 84199653d4eSeschrock if ((zpool_read_label(fd, &config)) != 0) { 842ccae0b50Seschrock (void) close(fd); 84399653d4eSeschrock (void) no_memory(hdl); 84499653d4eSeschrock goto error; 84599653d4eSeschrock } 846fa9e4066Sahrens 847fa9e4066Sahrens (void) close(fd); 848fa9e4066Sahrens 849fa9e4066Sahrens if (config != NULL) 85099653d4eSeschrock if (add_config(hdl, &pools, path, config) != 0) 85199653d4eSeschrock goto error; 852fa9e4066Sahrens } 853ccae0b50Seschrock 854ccae0b50Seschrock (void) closedir(dirp); 855ccae0b50Seschrock dirp = NULL; 856fa9e4066Sahrens } 857fa9e4066Sahrens 85899653d4eSeschrock ret = get_configs(hdl, &pools); 85999653d4eSeschrock 86099653d4eSeschrock error: 86199653d4eSeschrock for (pe = pools.pools; pe != NULL; pe = penext) { 86299653d4eSeschrock penext = pe->pe_next; 86399653d4eSeschrock for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { 86499653d4eSeschrock venext = ve->ve_next; 86599653d4eSeschrock for (ce = ve->ve_configs; ce != NULL; ce = cenext) { 86699653d4eSeschrock cenext = ce->ce_next; 86799653d4eSeschrock if (ce->ce_config) 86899653d4eSeschrock nvlist_free(ce->ce_config); 86999653d4eSeschrock free(ce); 87099653d4eSeschrock } 87199653d4eSeschrock free(ve); 87299653d4eSeschrock } 87399653d4eSeschrock free(pe); 87499653d4eSeschrock } 87599653d4eSeschrock 87699653d4eSeschrock for (ne = pools.names; ne != NULL; ne = nenext) { 87799653d4eSeschrock nenext = ne->ne_next; 87899653d4eSeschrock if (ne->ne_name) 87999653d4eSeschrock free(ne->ne_name); 88099653d4eSeschrock free(ne); 88199653d4eSeschrock } 88299653d4eSeschrock 883ccae0b50Seschrock if (dirp) 884ccae0b50Seschrock (void) closedir(dirp); 885fa9e4066Sahrens 886fa9e4066Sahrens return (ret); 887fa9e4066Sahrens } 888fa9e4066Sahrens 8892f8aaab3Seschrock /* 8902f8aaab3Seschrock * Given a cache file, return the contents as a list of importable pools. 8912f8aaab3Seschrock */ 8922f8aaab3Seschrock nvlist_t * 8932f8aaab3Seschrock zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile) 8942f8aaab3Seschrock { 8952f8aaab3Seschrock char *buf; 8962f8aaab3Seschrock int fd; 8972f8aaab3Seschrock struct stat64 statbuf; 8982f8aaab3Seschrock nvlist_t *raw, *src, *dst; 8992f8aaab3Seschrock nvlist_t *pools; 9002f8aaab3Seschrock nvpair_t *elem; 9012f8aaab3Seschrock char *name; 9022f8aaab3Seschrock uint64_t guid; 9032f8aaab3Seschrock boolean_t active; 9042f8aaab3Seschrock 9052f8aaab3Seschrock if ((fd = open(cachefile, O_RDONLY)) < 0) { 9062f8aaab3Seschrock zfs_error_aux(hdl, "%s", strerror(errno)); 9072f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 9082f8aaab3Seschrock dgettext(TEXT_DOMAIN, "failed to open cache file")); 9092f8aaab3Seschrock return (NULL); 9102f8aaab3Seschrock } 9112f8aaab3Seschrock 9122f8aaab3Seschrock if (fstat64(fd, &statbuf) != 0) { 9132f8aaab3Seschrock zfs_error_aux(hdl, "%s", strerror(errno)); 9142f8aaab3Seschrock (void) close(fd); 9152f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 9162f8aaab3Seschrock dgettext(TEXT_DOMAIN, "failed to get size of cache file")); 9172f8aaab3Seschrock return (NULL); 9182f8aaab3Seschrock } 9192f8aaab3Seschrock 9202f8aaab3Seschrock if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) { 9212f8aaab3Seschrock (void) close(fd); 9222f8aaab3Seschrock return (NULL); 9232f8aaab3Seschrock } 9242f8aaab3Seschrock 9252f8aaab3Seschrock if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 9262f8aaab3Seschrock (void) close(fd); 9272f8aaab3Seschrock free(buf); 9282f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 9292f8aaab3Seschrock dgettext(TEXT_DOMAIN, 9302f8aaab3Seschrock "failed to read cache file contents")); 9312f8aaab3Seschrock return (NULL); 9322f8aaab3Seschrock } 9332f8aaab3Seschrock 9342f8aaab3Seschrock (void) close(fd); 9352f8aaab3Seschrock 9362f8aaab3Seschrock if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { 9372f8aaab3Seschrock free(buf); 9382f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADCACHE, 9392f8aaab3Seschrock dgettext(TEXT_DOMAIN, 9402f8aaab3Seschrock "invalid or corrupt cache file contents")); 9412f8aaab3Seschrock return (NULL); 9422f8aaab3Seschrock } 9432f8aaab3Seschrock 9442f8aaab3Seschrock free(buf); 9452f8aaab3Seschrock 9462f8aaab3Seschrock /* 9472f8aaab3Seschrock * Go through and get the current state of the pools and refresh their 9482f8aaab3Seschrock * state. 9492f8aaab3Seschrock */ 9502f8aaab3Seschrock if (nvlist_alloc(&pools, 0, 0) != 0) { 9512f8aaab3Seschrock (void) no_memory(hdl); 9522f8aaab3Seschrock nvlist_free(raw); 9532f8aaab3Seschrock return (NULL); 9542f8aaab3Seschrock } 9552f8aaab3Seschrock 9562f8aaab3Seschrock elem = NULL; 9572f8aaab3Seschrock while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { 9582f8aaab3Seschrock verify(nvpair_value_nvlist(elem, &src) == 0); 9592f8aaab3Seschrock 9602f8aaab3Seschrock verify(nvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME, 9612f8aaab3Seschrock &name) == 0); 9622f8aaab3Seschrock verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID, 9632f8aaab3Seschrock &guid) == 0); 9642f8aaab3Seschrock 9652f8aaab3Seschrock if (pool_active(hdl, name, guid, &active) != 0) { 9662f8aaab3Seschrock nvlist_free(raw); 9672f8aaab3Seschrock nvlist_free(pools); 9682f8aaab3Seschrock return (NULL); 9692f8aaab3Seschrock } 9702f8aaab3Seschrock 9712f8aaab3Seschrock if (active) 9722f8aaab3Seschrock continue; 9732f8aaab3Seschrock 9742f8aaab3Seschrock if ((dst = refresh_config(hdl, src)) == NULL) { 9752f8aaab3Seschrock nvlist_free(raw); 9762f8aaab3Seschrock nvlist_free(pools); 9772f8aaab3Seschrock return (NULL); 9782f8aaab3Seschrock } 9792f8aaab3Seschrock 9802f8aaab3Seschrock if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { 9812f8aaab3Seschrock (void) no_memory(hdl); 9822f8aaab3Seschrock nvlist_free(dst); 9832f8aaab3Seschrock nvlist_free(raw); 9842f8aaab3Seschrock nvlist_free(pools); 9852f8aaab3Seschrock return (NULL); 9862f8aaab3Seschrock } 9872f8aaab3Seschrock 9882f8aaab3Seschrock nvlist_free(dst); 9892f8aaab3Seschrock } 9902f8aaab3Seschrock 9912f8aaab3Seschrock nvlist_free(raw); 9922f8aaab3Seschrock return (pools); 9932f8aaab3Seschrock } 9942f8aaab3Seschrock 9952f8aaab3Seschrock 99699653d4eSeschrock boolean_t 997fa9e4066Sahrens find_guid(nvlist_t *nv, uint64_t guid) 998fa9e4066Sahrens { 999fa9e4066Sahrens uint64_t tmp; 1000fa9e4066Sahrens nvlist_t **child; 1001fa9e4066Sahrens uint_t c, children; 1002fa9e4066Sahrens 1003fa9e4066Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0); 1004fa9e4066Sahrens if (tmp == guid) 100599653d4eSeschrock return (B_TRUE); 1006fa9e4066Sahrens 1007fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1008fa9e4066Sahrens &child, &children) == 0) { 1009fa9e4066Sahrens for (c = 0; c < children; c++) 1010fa9e4066Sahrens if (find_guid(child[c], guid)) 101199653d4eSeschrock return (B_TRUE); 101299653d4eSeschrock } 101399653d4eSeschrock 101499653d4eSeschrock return (B_FALSE); 101599653d4eSeschrock } 101699653d4eSeschrock 1017fa94a07fSbrendan typedef struct aux_cbdata { 1018fa94a07fSbrendan const char *cb_type; 101999653d4eSeschrock uint64_t cb_guid; 102099653d4eSeschrock zpool_handle_t *cb_zhp; 1021fa94a07fSbrendan } aux_cbdata_t; 102299653d4eSeschrock 102399653d4eSeschrock static int 1024fa94a07fSbrendan find_aux(zpool_handle_t *zhp, void *data) 102599653d4eSeschrock { 1026fa94a07fSbrendan aux_cbdata_t *cbp = data; 1027fa94a07fSbrendan nvlist_t **list; 1028fa94a07fSbrendan uint_t i, count; 102999653d4eSeschrock uint64_t guid; 103099653d4eSeschrock nvlist_t *nvroot; 103199653d4eSeschrock 103299653d4eSeschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 103399653d4eSeschrock &nvroot) == 0); 103499653d4eSeschrock 1035fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type, 1036fa94a07fSbrendan &list, &count) == 0) { 1037fa94a07fSbrendan for (i = 0; i < count; i++) { 1038fa94a07fSbrendan verify(nvlist_lookup_uint64(list[i], 103999653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 104099653d4eSeschrock if (guid == cbp->cb_guid) { 104199653d4eSeschrock cbp->cb_zhp = zhp; 104299653d4eSeschrock return (1); 104399653d4eSeschrock } 104499653d4eSeschrock } 1045fa9e4066Sahrens } 1046fa9e4066Sahrens 104799653d4eSeschrock zpool_close(zhp); 104899653d4eSeschrock return (0); 1049fa9e4066Sahrens } 1050fa9e4066Sahrens 1051fa9e4066Sahrens /* 105299653d4eSeschrock * Determines if the pool is in use. If so, it returns true and the state of 1053fa9e4066Sahrens * the pool as well as the name of the pool. Both strings are allocated and 1054fa9e4066Sahrens * must be freed by the caller. 1055fa9e4066Sahrens */ 1056fa9e4066Sahrens int 105799653d4eSeschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr, 105899653d4eSeschrock boolean_t *inuse) 1059fa9e4066Sahrens { 1060fa9e4066Sahrens nvlist_t *config; 1061fa9e4066Sahrens char *name; 106299653d4eSeschrock boolean_t ret; 1063fa9e4066Sahrens uint64_t guid, vdev_guid; 1064fa9e4066Sahrens zpool_handle_t *zhp; 1065fa9e4066Sahrens nvlist_t *pool_config; 106639c23413Seschrock uint64_t stateval, isspare; 1067fa94a07fSbrendan aux_cbdata_t cb = { 0 }; 106894de1d4cSeschrock boolean_t isactive; 106999653d4eSeschrock 107099653d4eSeschrock *inuse = B_FALSE; 1071fa9e4066Sahrens 107299653d4eSeschrock if (zpool_read_label(fd, &config) != 0) { 107399653d4eSeschrock (void) no_memory(hdl); 107499653d4eSeschrock return (-1); 107599653d4eSeschrock } 107699653d4eSeschrock 107799653d4eSeschrock if (config == NULL) 107899653d4eSeschrock return (0); 1079fa9e4066Sahrens 1080fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 108146a2abf2Seschrock &stateval) == 0); 1082fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 1083fa9e4066Sahrens &vdev_guid) == 0); 1084fa9e4066Sahrens 1085fa94a07fSbrendan if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) { 108699653d4eSeschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 108799653d4eSeschrock &name) == 0); 108899653d4eSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 108999653d4eSeschrock &guid) == 0); 109099653d4eSeschrock } 109199653d4eSeschrock 109246a2abf2Seschrock switch (stateval) { 1093fa9e4066Sahrens case POOL_STATE_EXPORTED: 109499653d4eSeschrock ret = B_TRUE; 1095fa9e4066Sahrens break; 1096fa9e4066Sahrens 1097fa9e4066Sahrens case POOL_STATE_ACTIVE: 1098fa9e4066Sahrens /* 1099fa9e4066Sahrens * For an active pool, we have to determine if it's really part 1100eaca9bbdSeschrock * of a currently active pool (in which case the pool will exist 1101eaca9bbdSeschrock * and the guid will be the same), or whether it's part of an 1102eaca9bbdSeschrock * active pool that was disconnected without being explicitly 1103eaca9bbdSeschrock * exported. 1104fa9e4066Sahrens */ 110594de1d4cSeschrock if (pool_active(hdl, name, guid, &isactive) != 0) { 110694de1d4cSeschrock nvlist_free(config); 110794de1d4cSeschrock return (-1); 110894de1d4cSeschrock } 110994de1d4cSeschrock 111094de1d4cSeschrock if (isactive) { 1111fa9e4066Sahrens /* 1112fa9e4066Sahrens * Because the device may have been removed while 1113fa9e4066Sahrens * offlined, we only report it as active if the vdev is 1114fa9e4066Sahrens * still present in the config. Otherwise, pretend like 1115fa9e4066Sahrens * it's not in use. 1116fa9e4066Sahrens */ 111799653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, name)) != NULL && 1118088e9d47Seschrock (pool_config = zpool_get_config(zhp, NULL)) 1119088e9d47Seschrock != NULL) { 1120fa9e4066Sahrens nvlist_t *nvroot; 1121fa9e4066Sahrens 1122fa9e4066Sahrens verify(nvlist_lookup_nvlist(pool_config, 1123fa9e4066Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 112446a2abf2Seschrock ret = find_guid(nvroot, vdev_guid); 1125fa9e4066Sahrens } else { 112699653d4eSeschrock ret = B_FALSE; 1127fa9e4066Sahrens } 112899653d4eSeschrock 112939c23413Seschrock /* 113039c23413Seschrock * If this is an active spare within another pool, we 113139c23413Seschrock * treat it like an unused hot spare. This allows the 113239c23413Seschrock * user to create a pool with a hot spare that currently 113339c23413Seschrock * in use within another pool. Since we return B_TRUE, 113439c23413Seschrock * libdiskmgt will continue to prevent generic consumers 113539c23413Seschrock * from using the device. 113639c23413Seschrock */ 113739c23413Seschrock if (ret && nvlist_lookup_uint64(config, 113839c23413Seschrock ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) 113939c23413Seschrock stateval = POOL_STATE_SPARE; 114039c23413Seschrock 114199653d4eSeschrock if (zhp != NULL) 114299653d4eSeschrock zpool_close(zhp); 1143fa9e4066Sahrens } else { 114446a2abf2Seschrock stateval = POOL_STATE_POTENTIALLY_ACTIVE; 114599653d4eSeschrock ret = B_TRUE; 114699653d4eSeschrock } 114799653d4eSeschrock break; 114899653d4eSeschrock 114999653d4eSeschrock case POOL_STATE_SPARE: 115099653d4eSeschrock /* 115199653d4eSeschrock * For a hot spare, it can be either definitively in use, or 115299653d4eSeschrock * potentially active. To determine if it's in use, we iterate 115399653d4eSeschrock * over all pools in the system and search for one with a spare 115499653d4eSeschrock * with a matching guid. 115599653d4eSeschrock * 115699653d4eSeschrock * Due to the shared nature of spares, we don't actually report 115799653d4eSeschrock * the potentially active case as in use. This means the user 115899653d4eSeschrock * can freely create pools on the hot spares of exported pools, 115999653d4eSeschrock * but to do otherwise makes the resulting code complicated, and 116099653d4eSeschrock * we end up having to deal with this case anyway. 116199653d4eSeschrock */ 116299653d4eSeschrock cb.cb_zhp = NULL; 116399653d4eSeschrock cb.cb_guid = vdev_guid; 1164fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_SPARES; 1165fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 1166fa94a07fSbrendan name = (char *)zpool_get_name(cb.cb_zhp); 1167fa94a07fSbrendan ret = TRUE; 1168fa94a07fSbrendan } else { 1169fa94a07fSbrendan ret = FALSE; 1170fa94a07fSbrendan } 1171fa94a07fSbrendan break; 1172fa94a07fSbrendan 1173fa94a07fSbrendan case POOL_STATE_L2CACHE: 1174fa94a07fSbrendan 1175fa94a07fSbrendan /* 1176fa94a07fSbrendan * Check if any pool is currently using this l2cache device. 1177fa94a07fSbrendan */ 1178fa94a07fSbrendan cb.cb_zhp = NULL; 1179fa94a07fSbrendan cb.cb_guid = vdev_guid; 1180fa94a07fSbrendan cb.cb_type = ZPOOL_CONFIG_L2CACHE; 1181fa94a07fSbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) { 118299653d4eSeschrock name = (char *)zpool_get_name(cb.cb_zhp); 1183fa9e4066Sahrens ret = TRUE; 118499653d4eSeschrock } else { 118599653d4eSeschrock ret = FALSE; 1186fa9e4066Sahrens } 1187fa9e4066Sahrens break; 1188fa9e4066Sahrens 1189fa9e4066Sahrens default: 119099653d4eSeschrock ret = B_FALSE; 1191fa9e4066Sahrens } 1192fa9e4066Sahrens 119346a2abf2Seschrock 119446a2abf2Seschrock if (ret) { 119599653d4eSeschrock if ((*namestr = zfs_strdup(hdl, name)) == NULL) { 1196*c6765aabSeschrock if (cb.cb_zhp) 1197*c6765aabSeschrock zpool_close(cb.cb_zhp); 119899653d4eSeschrock nvlist_free(config); 119999653d4eSeschrock return (-1); 120099653d4eSeschrock } 120146a2abf2Seschrock *state = (pool_state_t)stateval; 120246a2abf2Seschrock } 120346a2abf2Seschrock 120499653d4eSeschrock if (cb.cb_zhp) 120599653d4eSeschrock zpool_close(cb.cb_zhp); 120699653d4eSeschrock 1207fa9e4066Sahrens nvlist_free(config); 120899653d4eSeschrock *inuse = ret; 120999653d4eSeschrock return (0); 1210fa9e4066Sahrens } 1211