2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Pool import support functions.
2N/A *
2N/A * To import a pool, we rely on reading the configuration information from the
2N/A * ZFS label of each device. If we successfully read the label, then we
2N/A * organize the configuration information in the following hierarchy:
2N/A *
2N/A * pool guid -> toplevel vdev guid -> label txg
2N/A *
2N/A * Duplicate entries matching this same tuple will be discarded. Once we have
2N/A * examined every device, we pick the best label txg config for each toplevel
2N/A * vdev. We then arrange these toplevel vdevs into a complete pool config, and
2N/A * update any paths that have changed. Finally, we attempt to import the pool
2N/A * using our derived config, and record the results.
2N/A */
2N/A
2N/A#include <ctype.h>
2N/A#include <devid.h>
2N/A#include <dirent.h>
2N/A#include <errno.h>
2N/A#include <libintl.h>
2N/A#include <stddef.h>
2N/A#include <stdlib.h>
2N/A#include <libgen.h>
2N/A#include <string.h>
2N/A#include <sys/stat.h>
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <sys/vtoc.h>
2N/A#include <sys/dktp/fdisk.h>
2N/A#include <sys/efi_partition.h>
2N/A#include <thread_pool.h>
2N/A
2N/A#include <sys/vdev_impl.h>
2N/A
2N/A#include "libzfs.h"
2N/A#include "libzfs_impl.h"
2N/A
2N/A/*
2N/A * Intermediate structures used to gather configuration information.
2N/A */
2N/Atypedef struct config_entry {
2N/A uint64_t ce_txg;
2N/A nvlist_t *ce_config;
2N/A struct config_entry *ce_next;
2N/A} config_entry_t;
2N/A
2N/Atypedef struct vdev_entry {
2N/A uint64_t ve_guid;
2N/A config_entry_t *ve_configs;
2N/A struct vdev_entry *ve_next;
2N/A} vdev_entry_t;
2N/A
2N/Atypedef struct pool_entry {
2N/A uint64_t pe_guid;
2N/A vdev_entry_t *pe_vdevs;
2N/A struct pool_entry *pe_next;
2N/A} pool_entry_t;
2N/A
2N/Atypedef struct name_entry {
2N/A char *ne_name;
2N/A uint64_t ne_guid;
2N/A struct name_entry *ne_next;
2N/A} name_entry_t;
2N/A
2N/Atypedef struct pool_list {
2N/A pool_entry_t *pools;
2N/A name_entry_t *names;
2N/A} pool_list_t;
2N/A
2N/Astatic char *
2N/Aget_devid(const char *path)
2N/A{
2N/A int fd;
2N/A ddi_devid_t devid;
2N/A char *minor, *ret;
2N/A
2N/A if ((fd = open(path, O_RDONLY)) < 0)
2N/A return (NULL);
2N/A
2N/A minor = NULL;
2N/A ret = NULL;
2N/A if (devid_get(fd, &devid) == 0) {
2N/A if (devid_get_minor_name(fd, &minor) == 0)
2N/A ret = devid_str_encode(devid, minor);
2N/A if (minor != NULL)
2N/A devid_str_free(minor);
2N/A devid_free(devid);
2N/A }
2N/A (void) close(fd);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Go through and fix up any path and/or devid information for the given vdev
2N/A * configuration.
2N/A */
2N/Astatic int
2N/Afix_paths(nvlist_t *nv, name_entry_t *names)
2N/A{
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A uint64_t guid;
2N/A name_entry_t *ne, *best;
2N/A char *path, *devid;
2N/A int matched;
2N/A
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2N/A &child, &children) == 0) {
2N/A for (c = 0; c < children; c++)
2N/A if (fix_paths(child[c], names) != 0)
2N/A return (-1);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * This is a leaf (file or disk) vdev. In either case, go through
2N/A * the name list and see if we find a matching guid. If so, replace
2N/A * the path and see if we can calculate a new devid.
2N/A *
2N/A * There may be multiple names associated with a particular guid, in
2N/A * which case we have overlapping slices or multiple paths to the same
2N/A * disk. If this is the case, then we want to pick the path that is
2N/A * the most similar to the original, where "most similar" is the number
2N/A * of matching characters starting from the end of the path. This will
2N/A * preserve slice numbers even if the disks have been reorganized, and
2N/A * will also catch preferred disk names if multiple paths exist.
2N/A */
2N/A verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
2N/A if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
2N/A path = NULL;
2N/A
2N/A matched = 0;
2N/A best = NULL;
2N/A for (ne = names; ne != NULL; ne = ne->ne_next) {
2N/A if (ne->ne_guid == guid) {
2N/A const char *src, *dst;
2N/A int count;
2N/A
2N/A if (path == NULL) {
2N/A best = ne;
2N/A break;
2N/A }
2N/A
2N/A src = ne->ne_name + strlen(ne->ne_name) - 1;
2N/A dst = path + strlen(path) - 1;
2N/A for (count = 0; src >= ne->ne_name && dst >= path;
2N/A src--, dst--, count++)
2N/A if (*src != *dst)
2N/A break;
2N/A
2N/A /*
2N/A * At this point, 'count' is the number of characters
2N/A * matched from the end.
2N/A */
2N/A if (count > matched || best == NULL) {
2N/A best = ne;
2N/A matched = count;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (best == NULL)
2N/A return (0);
2N/A
2N/A if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
2N/A return (-1);
2N/A
2N/A if ((devid = get_devid(best->ne_name)) == NULL) {
2N/A (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
2N/A } else {
2N/A if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
2N/A return (-1);
2N/A devid_str_free(devid);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Add the given configuration to the list of known devices.
2N/A */
2N/Astatic int
2N/Aadd_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
2N/A nvlist_t *config)
2N/A{
2N/A uint64_t pool_guid, vdev_guid, top_guid, txg, state;
2N/A pool_entry_t *pe;
2N/A vdev_entry_t *ve;
2N/A config_entry_t *ce;
2N/A name_entry_t *ne;
2N/A
2N/A /*
2N/A * If this is a hot spare not currently in use or level 2 cache
2N/A * device, add it to the list of names to translate, but don't do
2N/A * anything else.
2N/A */
2N/A if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2N/A &state) == 0 &&
2N/A (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
2N/A nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
2N/A if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
2N/A return (-1);
2N/A
2N/A if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
2N/A free(ne);
2N/A return (-1);
2N/A }
2N/A ne->ne_guid = vdev_guid;
2N/A ne->ne_next = pl->names;
2N/A pl->names = ne;
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * If we have a valid config but cannot read any of these fields, then
2N/A * it means we have a half-initialized label. In vdev_label_init()
2N/A * we write a label with txg == 0 so that we can identify the device
2N/A * in case the user refers to the same disk later on. If we fail to
2N/A * create the pool, we'll be left with a label in this state
2N/A * which should not be considered part of a valid pool.
2N/A */
2N/A if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2N/A &pool_guid) != 0 ||
2N/A nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
2N/A &vdev_guid) != 0 ||
2N/A nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
2N/A &top_guid) != 0 ||
2N/A nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2N/A &txg) != 0 || txg == 0) {
2N/A nvlist_free(config);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * First, see if we know about this pool. If not, then add it to the
2N/A * list of known pools.
2N/A */
2N/A for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
2N/A if (pe->pe_guid == pool_guid)
2N/A break;
2N/A }
2N/A
2N/A if (pe == NULL) {
2N/A if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
2N/A nvlist_free(config);
2N/A return (-1);
2N/A }
2N/A pe->pe_guid = pool_guid;
2N/A pe->pe_next = pl->pools;
2N/A pl->pools = pe;
2N/A }
2N/A
2N/A /*
2N/A * Second, see if we know about this toplevel vdev. Add it if its
2N/A * missing.
2N/A */
2N/A for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
2N/A if (ve->ve_guid == top_guid)
2N/A break;
2N/A }
2N/A
2N/A if (ve == NULL) {
2N/A if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
2N/A nvlist_free(config);
2N/A return (-1);
2N/A }
2N/A ve->ve_guid = top_guid;
2N/A ve->ve_next = pe->pe_vdevs;
2N/A pe->pe_vdevs = ve;
2N/A }
2N/A
2N/A /*
2N/A * Third, see if we have a config with a matching transaction group. If
2N/A * so, then we do nothing. Otherwise, add it to the list of known
2N/A * configs.
2N/A */
2N/A for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
2N/A if (ce->ce_txg == txg)
2N/A break;
2N/A }
2N/A
2N/A if (ce == NULL) {
2N/A if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
2N/A nvlist_free(config);
2N/A return (-1);
2N/A }
2N/A ce->ce_txg = txg;
2N/A ce->ce_config = config;
2N/A ce->ce_next = ve->ve_configs;
2N/A ve->ve_configs = ce;
2N/A } else {
2N/A nvlist_free(config);
2N/A }
2N/A
2N/A /*
2N/A * At this point we've successfully added our config to the list of
2N/A * known configs. The last thing to do is add the vdev guid -> path
2N/A * mappings so that we can fix up the configuration as necessary before
2N/A * doing the import.
2N/A */
2N/A if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
2N/A return (-1);
2N/A
2N/A if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
2N/A free(ne);
2N/A return (-1);
2N/A }
2N/A
2N/A ne->ne_guid = vdev_guid;
2N/A ne->ne_next = pl->names;
2N/A pl->names = ne;
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Returns true if the named pool matches the given GUID.
2N/A */
2N/Astatic int
2N/Apool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
2N/A boolean_t *isactive)
2N/A{
2N/A zpool_handle_t *zhp;
2N/A uint64_t theguid;
2N/A
2N/A if (zpool_open_quiet(hdl, name, &zhp) != 0)
2N/A return (-1);
2N/A
2N/A if (zhp == NULL) {
2N/A *isactive = B_FALSE;
2N/A return (0);
2N/A }
2N/A
2N/A verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
2N/A &theguid) == 0);
2N/A
2N/A zpool_close(zhp);
2N/A
2N/A *isactive = (theguid == guid);
2N/A return (0);
2N/A}
2N/A
2N/Astatic nvlist_t *
2N/Arefresh_config(libzfs_handle_t *hdl, nvlist_t *config, boolean_t trusted)
2N/A{
2N/A nvlist_t *nvl;
2N/A zfs_cmd_t zc = { 0 };
2N/A int err;
2N/A
2N/A if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
2N/A return (NULL);
2N/A
2N/A if (zcmd_alloc_dst_nvlist(hdl, &zc,
2N/A zc.zc_nvlist_conf_size * 2) != 0) {
2N/A zcmd_free_nvlists(&zc);
2N/A return (NULL);
2N/A }
2N/A
2N/A zc.zc_cookie = trusted;
2N/A
2N/A while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
2N/A &zc)) != 0 && errno == ENOMEM) {
2N/A if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2N/A zcmd_free_nvlists(&zc);
2N/A return (NULL);
2N/A }
2N/A }
2N/A
2N/A if (err) {
2N/A zcmd_free_nvlists(&zc);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
2N/A zcmd_free_nvlists(&zc);
2N/A return (NULL);
2N/A }
2N/A
2N/A zcmd_free_nvlists(&zc);
2N/A return (nvl);
2N/A}
2N/A
2N/A/*
2N/A * Determine if the vdev id is a hole in the namespace.
2N/A */
2N/Aboolean_t
2N/Avdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
2N/A{
2N/A for (int c = 0; c < holes; c++) {
2N/A
2N/A /* Top-level is a hole */
2N/A if (hole_array[c] == id)
2N/A return (B_TRUE);
2N/A }
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Convert our list of pools into the definitive set of configurations. We
2N/A * start by picking the best config for each toplevel vdev. Once that's done,
2N/A * we assemble the toplevel vdevs into a full config for the pool. We make a
2N/A * pass to fix up any incorrect paths, and then add it to the main list to
2N/A * return to the user.
2N/A */
2N/Astatic nvlist_t *
2N/Aget_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
2N/A{
2N/A pool_entry_t *pe;
2N/A vdev_entry_t *ve;
2N/A config_entry_t *ce;
2N/A nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot;
2N/A nvlist_t **spares, **l2cache;
2N/A uint_t i, nspares, nl2cache;
2N/A boolean_t config_seen;
2N/A uint64_t best_txg;
2N/A char *name, *hostname;
2N/A uint64_t version, guid;
2N/A uint_t children = 0;
2N/A nvlist_t **child = NULL;
2N/A uint_t holes;
2N/A uint64_t *hole_array, max_id;
2N/A uint_t c;
2N/A boolean_t isactive;
2N/A uint64_t hostid;
2N/A nvlist_t *nvl;
2N/A boolean_t found_one = B_FALSE;
2N/A boolean_t valid_top_config = B_FALSE;
2N/A
2N/A if (nvlist_alloc(&ret, 0, 0) != 0)
2N/A goto nomem;
2N/A
2N/A for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
2N/A uint64_t id, max_txg = 0;
2N/A
2N/A if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
2N/A goto nomem;
2N/A config_seen = B_FALSE;
2N/A
2N/A /*
2N/A * Iterate over all toplevel vdevs. Grab the pool configuration
2N/A * from the first one we find, and then go through the rest and
2N/A * add them as necessary to the 'vdevs' member of the config.
2N/A */
2N/A for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
2N/A
2N/A /*
2N/A * Determine the best configuration for this vdev by
2N/A * selecting the config with the latest transaction
2N/A * group.
2N/A */
2N/A best_txg = 0;
2N/A for (ce = ve->ve_configs; ce != NULL;
2N/A ce = ce->ce_next) {
2N/A
2N/A if (ce->ce_txg > best_txg) {
2N/A tmp = ce->ce_config;
2N/A best_txg = ce->ce_txg;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * We rely on the fact that the max txg for the
2N/A * pool will contain the most up-to-date information
2N/A * about the valid top-levels in the vdev namespace.
2N/A */
2N/A if (best_txg > max_txg) {
2N/A (void) nvlist_remove(config,
2N/A ZPOOL_CONFIG_VDEV_CHILDREN,
2N/A DATA_TYPE_UINT64);
2N/A (void) nvlist_remove(config,
2N/A ZPOOL_CONFIG_HOLE_ARRAY,
2N/A DATA_TYPE_UINT64_ARRAY);
2N/A
2N/A max_txg = best_txg;
2N/A hole_array = NULL;
2N/A holes = 0;
2N/A max_id = 0;
2N/A valid_top_config = B_FALSE;
2N/A
2N/A if (nvlist_lookup_uint64(tmp,
2N/A ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
2N/A verify(nvlist_add_uint64(config,
2N/A ZPOOL_CONFIG_VDEV_CHILDREN,
2N/A max_id) == 0);
2N/A valid_top_config = B_TRUE;
2N/A }
2N/A
2N/A if (nvlist_lookup_uint64_array(tmp,
2N/A ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
2N/A &holes) == 0) {
2N/A verify(nvlist_add_uint64_array(config,
2N/A ZPOOL_CONFIG_HOLE_ARRAY,
2N/A hole_array, holes) == 0);
2N/A }
2N/A }
2N/A
2N/A if (!config_seen) {
2N/A /*
2N/A * Copy the relevant pieces of data to the pool
2N/A * configuration:
2N/A *
2N/A * version
2N/A * pool guid
2N/A * name
2N/A * pool state
2N/A * hostid (if available)
2N/A * hostname (if available)
2N/A */
2N/A uint64_t state;
2N/A
2N/A verify(nvlist_lookup_uint64(tmp,
2N/A ZPOOL_CONFIG_VERSION, &version) == 0);
2N/A if (nvlist_add_uint64(config,
2N/A ZPOOL_CONFIG_VERSION, version) != 0)
2N/A goto nomem;
2N/A verify(nvlist_lookup_uint64(tmp,
2N/A ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2N/A if (nvlist_add_uint64(config,
2N/A ZPOOL_CONFIG_POOL_GUID, guid) != 0)
2N/A goto nomem;
2N/A verify(nvlist_lookup_string(tmp,
2N/A ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2N/A if (nvlist_add_string(config,
2N/A ZPOOL_CONFIG_POOL_NAME, name) != 0)
2N/A goto nomem;
2N/A verify(nvlist_lookup_uint64(tmp,
2N/A ZPOOL_CONFIG_POOL_STATE, &state) == 0);
2N/A if (nvlist_add_uint64(config,
2N/A ZPOOL_CONFIG_POOL_STATE, state) != 0)
2N/A goto nomem;
2N/A hostid = 0;
2N/A if (nvlist_lookup_uint64(tmp,
2N/A ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2N/A if (nvlist_add_uint64(config,
2N/A ZPOOL_CONFIG_HOSTID, hostid) != 0)
2N/A goto nomem;
2N/A verify(nvlist_lookup_string(tmp,
2N/A ZPOOL_CONFIG_HOSTNAME,
2N/A &hostname) == 0);
2N/A if (nvlist_add_string(config,
2N/A ZPOOL_CONFIG_HOSTNAME,
2N/A hostname) != 0)
2N/A goto nomem;
2N/A }
2N/A
2N/A config_seen = B_TRUE;
2N/A }
2N/A
2N/A /*
2N/A * Add this top-level vdev to the child array.
2N/A */
2N/A verify(nvlist_lookup_nvlist(tmp,
2N/A ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
2N/A verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
2N/A &id) == 0);
2N/A
2N/A if (id >= children) {
2N/A nvlist_t **newchild;
2N/A
2N/A newchild = zfs_alloc(hdl, (id + 1) *
2N/A sizeof (nvlist_t *));
2N/A if (newchild == NULL)
2N/A goto nomem;
2N/A
2N/A for (c = 0; c < children; c++)
2N/A newchild[c] = child[c];
2N/A
2N/A free(child);
2N/A child = newchild;
2N/A children = id + 1;
2N/A }
2N/A if (nvlist_dup(nvtop, &child[id], 0) != 0)
2N/A goto nomem;
2N/A
2N/A }
2N/A
2N/A /*
2N/A * If we have information about all the top-levels then
2N/A * clean up the nvlist which we've constructed. This
2N/A * means removing any extraneous devices that are
2N/A * beyond the valid range or adding devices to the end
2N/A * of our array which appear to be missing.
2N/A */
2N/A if (valid_top_config) {
2N/A if (max_id < children) {
2N/A for (c = max_id; c < children; c++)
2N/A nvlist_free(child[c]);
2N/A children = max_id;
2N/A } else if (max_id > children) {
2N/A nvlist_t **newchild;
2N/A
2N/A newchild = zfs_alloc(hdl, (max_id) *
2N/A sizeof (nvlist_t *));
2N/A if (newchild == NULL)
2N/A goto nomem;
2N/A
2N/A for (c = 0; c < children; c++)
2N/A newchild[c] = child[c];
2N/A
2N/A free(child);
2N/A child = newchild;
2N/A children = max_id;
2N/A }
2N/A }
2N/A
2N/A verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2N/A &guid) == 0);
2N/A
2N/A /*
2N/A * The vdev namespace may contain holes as a result of
2N/A * device removal. We must add them back into the vdev
2N/A * tree before we process any missing devices.
2N/A */
2N/A if (holes > 0) {
2N/A ASSERT(valid_top_config);
2N/A
2N/A for (c = 0; c < children; c++) {
2N/A nvlist_t *holey;
2N/A
2N/A if (child[c] != NULL ||
2N/A !vdev_is_hole(hole_array, holes, c))
2N/A continue;
2N/A
2N/A if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
2N/A 0) != 0)
2N/A goto nomem;
2N/A
2N/A /*
2N/A * Holes in the namespace are treated as
2N/A * "hole" top-level vdevs and have a
2N/A * special flag set on them.
2N/A */
2N/A if (nvlist_add_string(holey,
2N/A ZPOOL_CONFIG_TYPE,
2N/A VDEV_TYPE_HOLE) != 0 ||
2N/A nvlist_add_uint64(holey,
2N/A ZPOOL_CONFIG_ID, c) != 0 ||
2N/A nvlist_add_uint64(holey,
2N/A ZPOOL_CONFIG_GUID, 0ULL) != 0)
2N/A goto nomem;
2N/A child[c] = holey;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Look for any missing top-level vdevs. If this is the case,
2N/A * create a faked up 'missing' vdev as a placeholder. We cannot
2N/A * simply compress the child array, because the kernel performs
2N/A * certain checks to make sure the vdev IDs match their location
2N/A * in the configuration.
2N/A */
2N/A for (c = 0; c < children; c++) {
2N/A if (child[c] == NULL) {
2N/A nvlist_t *missing;
2N/A if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
2N/A 0) != 0)
2N/A goto nomem;
2N/A if (nvlist_add_string(missing,
2N/A ZPOOL_CONFIG_TYPE,
2N/A VDEV_TYPE_MISSING) != 0 ||
2N/A nvlist_add_uint64(missing,
2N/A ZPOOL_CONFIG_ID, c) != 0 ||
2N/A nvlist_add_uint64(missing,
2N/A ZPOOL_CONFIG_GUID, 0ULL) != 0) {
2N/A nvlist_free(missing);
2N/A goto nomem;
2N/A }
2N/A child[c] = missing;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Put all of this pool's top-level vdevs into a root vdev.
2N/A */
2N/A if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
2N/A goto nomem;
2N/A if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
2N/A VDEV_TYPE_ROOT) != 0 ||
2N/A nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
2N/A nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
2N/A nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2N/A child, children) != 0) {
2N/A nvlist_free(nvroot);
2N/A goto nomem;
2N/A }
2N/A
2N/A for (c = 0; c < children; c++)
2N/A nvlist_free(child[c]);
2N/A free(child);
2N/A children = 0;
2N/A child = NULL;
2N/A
2N/A /*
2N/A * Go through and fix up any paths and/or devids based on our
2N/A * known list of vdev GUID -> path mappings.
2N/A */
2N/A if (fix_paths(nvroot, pl->names) != 0) {
2N/A nvlist_free(nvroot);
2N/A goto nomem;
2N/A }
2N/A
2N/A /*
2N/A * Add the root vdev to this pool's configuration.
2N/A */
2N/A if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2N/A nvroot) != 0) {
2N/A nvlist_free(nvroot);
2N/A goto nomem;
2N/A }
2N/A nvlist_free(nvroot);
2N/A
2N/A /*
2N/A * zdb uses this path to report on active pools that were
2N/A * imported or created using -R.
2N/A */
2N/A if (active_ok)
2N/A goto add_pool;
2N/A
2N/A /*
2N/A * Determine if this pool is currently active, in which case we
2N/A * can't actually import it.
2N/A */
2N/A verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2N/A &name) == 0);
2N/A verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2N/A &guid) == 0);
2N/A
2N/A if (pool_active(hdl, name, guid, &isactive) != 0)
2N/A goto error;
2N/A
2N/A if (isactive) {
2N/A nvlist_free(config);
2N/A config = NULL;
2N/A continue;
2N/A }
2N/A
2N/A if ((nvl = refresh_config(hdl, config, B_TRUE)) == NULL) {
2N/A nvlist_free(config);
2N/A config = NULL;
2N/A continue;
2N/A }
2N/A
2N/A nvlist_free(config);
2N/A config = nvl;
2N/A
2N/A /*
2N/A * Go through and update the paths for spares, now that we have
2N/A * them.
2N/A */
2N/A verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2N/A &nvroot) == 0);
2N/A if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2N/A &spares, &nspares) == 0) {
2N/A for (i = 0; i < nspares; i++) {
2N/A if (fix_paths(spares[i], pl->names) != 0)
2N/A goto nomem;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Update the paths for l2cache devices.
2N/A */
2N/A if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2N/A &l2cache, &nl2cache) == 0) {
2N/A for (i = 0; i < nl2cache; i++) {
2N/A if (fix_paths(l2cache[i], pl->names) != 0)
2N/A goto nomem;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Restore the original information read from the actual label.
2N/A */
2N/A (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
2N/A DATA_TYPE_UINT64);
2N/A (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
2N/A DATA_TYPE_STRING);
2N/A if (hostid != 0) {
2N/A verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
2N/A hostid) == 0);
2N/A verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
2N/A hostname) == 0);
2N/A }
2N/A
2N/Aadd_pool:
2N/A /*
2N/A * Add this pool to the list of configs.
2N/A */
2N/A verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2N/A &name) == 0);
2N/A if (nvlist_add_nvlist(ret, name, config) != 0)
2N/A goto nomem;
2N/A
2N/A found_one = B_TRUE;
2N/A nvlist_free(config);
2N/A config = NULL;
2N/A }
2N/A
2N/A if (!found_one) {
2N/A nvlist_free(ret);
2N/A ret = NULL;
2N/A }
2N/A
2N/A return (ret);
2N/A
2N/Anomem:
2N/A (void) no_memory(hdl);
2N/Aerror:
2N/A nvlist_free(config);
2N/A nvlist_free(ret);
2N/A for (c = 0; c < children; c++)
2N/A nvlist_free(child[c]);
2N/A free(child);
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Return the offset of the given label.
2N/A */
2N/Astatic uint64_t
2N/Alabel_offset(uint64_t size, int l)
2N/A{
2N/A ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
2N/A return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
2N/A 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
2N/A}
2N/A
2N/A/*
2N/A * Given a file descriptor, read the label information and return an nvlist
2N/A * describing the configuration, if there is one.
2N/A */
2N/Aint
2N/Azpool_read_label(int fd, nvlist_t **config)
2N/A{
2N/A struct stat64 statbuf;
2N/A int l;
2N/A vdev_label_t *label;
2N/A uint64_t state, txg, size;
2N/A
2N/A *config = NULL;
2N/A
2N/A if (fstat64(fd, &statbuf) == -1)
2N/A return (0);
2N/A size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
2N/A
2N/A if ((label = malloc(sizeof (vdev_label_t))) == NULL)
2N/A return (-1);
2N/A
2N/A for (l = 0; l < VDEV_LABELS; l++) {
2N/A if (pread64(fd, label, sizeof (vdev_label_t),
2N/A label_offset(size, l)) != sizeof (vdev_label_t))
2N/A continue;
2N/A
2N/A if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
2N/A sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
2N/A continue;
2N/A
2N/A if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
2N/A &state) != 0 || state > POOL_STATE_L2CACHE) {
2N/A nvlist_free(*config);
2N/A continue;
2N/A }
2N/A
2N/A if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
2N/A (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
2N/A &txg) != 0 || txg == 0)) {
2N/A nvlist_free(*config);
2N/A continue;
2N/A }
2N/A
2N/A free(label);
2N/A return (0);
2N/A }
2N/A
2N/A free(label);
2N/A *config = NULL;
2N/A return (0);
2N/A}
2N/A
2N/Atypedef struct rdsk_node {
2N/A char *rn_path; /* always used in the AVLs compare function */
2N/A char *rn_rawpath;
2N/A libzfs_handle_t *rn_hdl;
2N/A nvlist_t *rn_config;
2N/A avl_tree_t *rn_avl;
2N/A avl_node_t rn_node;
2N/A boolean_t rn_nozpool;
2N/A} rdsk_node_t;
2N/A
2N/Astatic int
2N/Aslice_cache_compare(const void *arg1, const void *arg2)
2N/A{
2N/A const char *nm1 = ((rdsk_node_t *)arg1)->rn_path;
2N/A const char *nm2 = ((rdsk_node_t *)arg2)->rn_path;
2N/A char *nm1slice, *nm2slice;
2N/A int rv;
2N/A
2N/A /*
2N/A * slices zero and two are the most likely to provide results,
2N/A * so put those first
2N/A */
2N/A nm1slice = strstr(nm1, "s0");
2N/A nm2slice = strstr(nm2, "s0");
2N/A if (nm1slice && !nm2slice) {
2N/A return (-1);
2N/A }
2N/A if (!nm1slice && nm2slice) {
2N/A return (1);
2N/A }
2N/A nm1slice = strstr(nm1, "s2");
2N/A nm2slice = strstr(nm2, "s2");
2N/A if (nm1slice && !nm2slice) {
2N/A return (-1);
2N/A }
2N/A if (!nm1slice && nm2slice) {
2N/A return (1);
2N/A }
2N/A
2N/A rv = strcmp(nm1, nm2);
2N/A if (rv == 0)
2N/A return (0);
2N/A return (rv > 0 ? 1 : -1);
2N/A}
2N/A
2N/Astatic void
2N/Acheck_one_slice(avl_tree_t *r, char *diskname, uint_t partno,
2N/A diskaddr_t size, uint_t blksz)
2N/A{
2N/A rdsk_node_t tmpnode;
2N/A rdsk_node_t *node;
2N/A char sname[MAXNAMELEN];
2N/A
2N/A tmpnode.rn_path = &sname[0];
2N/A (void) snprintf(tmpnode.rn_path, MAXNAMELEN, "%s%u",
2N/A diskname, partno);
2N/A /*
2N/A * protect against division by zero for disk labels that
2N/A * contain a bogus sector size
2N/A */
2N/A if (blksz == 0)
2N/A blksz = DEV_BSIZE;
2N/A /* too small to contain a zpool? */
2N/A if ((size < (SPA_MINDEVSIZE / blksz)) &&
2N/A (node = avl_find(r, &tmpnode, NULL)))
2N/A node->rn_nozpool = B_TRUE;
2N/A}
2N/A
2N/Astatic void
2N/Anozpool_all_slices(avl_tree_t *r, const char *sname)
2N/A{
2N/A char diskname[MAXNAMELEN];
2N/A char *ptr;
2N/A int i;
2N/A
2N/A (void) strncpy(diskname, sname, MAXNAMELEN);
2N/A if (((ptr = strrchr(diskname, 's')) == NULL) &&
2N/A ((ptr = strrchr(diskname, 'p')) == NULL))
2N/A return;
2N/A ptr[0] = 's';
2N/A ptr[1] = '\0';
2N/A for (i = 0; i < NDKMAP; i++)
2N/A check_one_slice(r, diskname, i, 0, 1);
2N/A ptr[0] = 'p';
2N/A for (i = 0; i <= FD_NUMPART; i++)
2N/A check_one_slice(r, diskname, i, 0, 1);
2N/A}
2N/A
2N/Astatic void
2N/Acheck_slices(avl_tree_t *r, int fd, const char *sname)
2N/A{
2N/A struct extvtoc vtoc;
2N/A struct dk_gpt *gpt;
2N/A char diskname[MAXNAMELEN];
2N/A char *ptr;
2N/A int i;
2N/A
2N/A (void) strncpy(diskname, sname, MAXNAMELEN);
2N/A if ((ptr = strrchr(diskname, 's')) == NULL || !isdigit(ptr[1]))
2N/A return;
2N/A ptr[1] = '\0';
2N/A
2N/A if (read_extvtoc(fd, &vtoc) >= 0) {
2N/A for (i = 0; i < NDKMAP; i++)
2N/A check_one_slice(r, diskname, i,
2N/A vtoc.v_part[i].p_size, vtoc.v_sectorsz);
2N/A } else if (efi_alloc_and_read(fd, &gpt) >= 0) {
2N/A /*
2N/A * on x86 we'll still have leftover links that point
2N/A * to slices s[9-15], so use NDKMAP instead
2N/A */
2N/A for (i = 0; i < NDKMAP; i++)
2N/A check_one_slice(r, diskname, i,
2N/A gpt->efi_parts[i].p_size, gpt->efi_lbasize);
2N/A /* nodes p[1-4] are never used with EFI labels */
2N/A ptr[0] = 'p';
2N/A for (i = 1; i <= FD_NUMPART; i++)
2N/A check_one_slice(r, diskname, i, 0, 1);
2N/A efi_free(gpt);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Azpool_open_func(void *arg)
2N/A{
2N/A rdsk_node_t *rn = arg;
2N/A struct stat64 statbuf;
2N/A nvlist_t *config;
2N/A int fd;
2N/A
2N/A if (rn->rn_nozpool)
2N/A return;
2N/A /*
2N/A * Using raw devices instead of block devices when we're reading
2N/A * labels skips a bunch of slow operations during close(2).
2N/A */
2N/A if ((fd = open64(rn->rn_rawpath, O_RDONLY)) < 0) {
2N/A /* symlink to a device that's no longer there */
2N/A if (errno == ENOENT)
2N/A nozpool_all_slices(rn->rn_avl, rn->rn_path);
2N/A return;
2N/A }
2N/A /*
2N/A * Ignore failed stats. We only want regular
2N/A * files, character devs and block devs.
2N/A */
2N/A if (fstat64(fd, &statbuf) != 0 ||
2N/A (!S_ISREG(statbuf.st_mode) &&
2N/A !S_ISCHR(statbuf.st_mode) &&
2N/A !S_ISBLK(statbuf.st_mode))) {
2N/A (void) close(fd);
2N/A return;
2N/A }
2N/A /* this file is too small to hold a zpool */
2N/A if (S_ISREG(statbuf.st_mode) &&
2N/A statbuf.st_size < SPA_MINDEVSIZE) {
2N/A (void) close(fd);
2N/A return;
2N/A } else if (!S_ISREG(statbuf.st_mode)) {
2N/A /*
2N/A * Try to read the disk label first so we don't have to
2N/A * open a bunch of minor nodes that can't have a zpool.
2N/A */
2N/A check_slices(rn->rn_avl, fd, rn->rn_path);
2N/A }
2N/A
2N/A if ((zpool_read_label(fd, &config)) != 0) {
2N/A (void) close(fd);
2N/A (void) no_memory(rn->rn_hdl);
2N/A return;
2N/A }
2N/A (void) close(fd);
2N/A
2N/A
2N/A rn->rn_config = config;
2N/A if (config != NULL) {
2N/A assert(rn->rn_nozpool == B_FALSE);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Given a file descriptor, clear (zero) the label information. This function
2N/A * is currently only used in the appliance stack as part of the ZFS sysevent
2N/A * module.
2N/A */
2N/Aint
2N/Azpool_clear_label(int fd)
2N/A{
2N/A struct stat64 statbuf;
2N/A int l;
2N/A vdev_label_t *label;
2N/A uint64_t psize, offset, size;
2N/A int rv = 0;
2N/A
2N/A if (fstat64(fd, &statbuf) == -1)
2N/A return (0);
2N/A psize = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
2N/A
2N/A if ((label = calloc(1, sizeof (vdev_label_t))) == NULL)
2N/A return (-1);
2N/A
2N/A for (l = 0; l < VDEV_LABELS; l++) {
2N/A size = sizeof (label->vl_vdev_phys);
2N/A offset = offsetof(vdev_label_t, vl_vdev_phys);
2N/A if (pwrite64(fd, &label->vl_vdev_phys, size,
2N/A label_offset(psize, l) + offset) != size)
2N/A rv = -1;
2N/A size = sizeof (label->vl_uberblock);
2N/A offset = offsetof(vdev_label_t, vl_uberblock);
2N/A if (pwrite64(fd, &label->vl_vdev_phys, size,
2N/A label_offset(psize, l) + offset) != size)
2N/A rv = -1;
2N/A }
2N/A
2N/A free(label);
2N/A return (rv);
2N/A}
2N/A
2N/A/*
2N/A * Normalize input path, remove any superfluous slashes.
2N/A * If we deal with a directory we can use realpath(3C) instantly
2N/A * to normalize our path.
2N/A * We can not use realpath(3C) directly for files as it resolves
2N/A * symbolic links delivering device paths into /devices which are
2N/A * of no use for us later.
2N/A * Returns malloc'ed memory on success, NULL on failure.
2N/A */
2N/Astatic char *
2N/Azpool_normalize_path(libzfs_handle_t *hdl, const char *name, boolean_t file)
2N/A{
2N/A char *path = NULL, *tmp = NULL, *dn = NULL;
2N/A
2N/A if (*name != '/') {
2N/A (void) zfs_error_fmt(hdl, EZFS_BADPATH,
2N/A dgettext(TEXT_DOMAIN, "not an absolute path '%s'"),
2N/A name);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((path = zfs_alloc(hdl, MAXPATHLEN)) == NULL)
2N/A return (NULL);
2N/A
2N/A if (!file) {
2N/A /*
2N/A * If we are dealing with a directory we're almost
2N/A * done. Copy at most MAXPATHLEN - 2 to always
2N/A * leave room for the trailing slash and NUL
2N/A * and return it.
2N/A */
2N/A if ((tmp = realpath(name, NULL)) == NULL) {
2N/A (void) zfs_error_fmt(hdl, EZFS_BADPATH,
2N/A dgettext(TEXT_DOMAIN, "cannot determine '%s'"),
2N/A name);
2N/A goto error;
2N/A }
2N/A (void) strlcpy(path, tmp, MAXPATHLEN - 2);
2N/A free(tmp);
2N/A if (strcmp(path, "/") != 0)
2N/A (void) strcat(path, "/");
2N/A return (path);
2N/A }
2N/A
2N/A /* Caution, dirname(3C) below modifies its input */
2N/A if ((tmp = zfs_strdup(hdl, name)) == NULL)
2N/A goto error;
2N/A
2N/A if ((dn = realpath(dirname(tmp), NULL)) == NULL) {
2N/A (void) zfs_error_fmt(hdl, EZFS_BADPATH,
2N/A dgettext(TEXT_DOMAIN, "cannot determine '%s'"),
2N/A name);
2N/A goto error;
2N/A }
2N/A free(tmp);
2N/A
2N/A /* Caution, basename(3C) below modifies its input */
2N/A if ((tmp = zfs_strdup(hdl, name)) == NULL)
2N/A goto error;
2N/A
2N/A (void) snprintf(path, MAXPATHLEN, "%s/%s", dn, basename(tmp));
2N/A free(tmp);
2N/A free(dn);
2N/A
2N/A return (path);
2N/Aerror:
2N/A free(path);
2N/A free(tmp);
2N/A free(dn);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Produce a raw and non-raw version of the input path name
2N/A * for every object living in the /dev/ namespace
2N/A * that we recognize and return it.
2N/A * Returns malloc'ed memory on success, NULL on failure.
2N/A */
2N/Avoid
2N/Azpool_rawunrawpath(libzfs_handle_t *hdl, const char *path, char **raw,
2N/A char **blk)
2N/A{
2N/A *blk = zfs_alloc(hdl, MAXPATHLEN);
2N/A *raw = zfs_alloc(hdl, MAXPATHLEN);
2N/A
2N/A if (*blk == NULL || *raw == NULL) {
2N/A free(*raw);
2N/A free(*blk);
2N/A *raw = *blk = NULL;
2N/A return;
2N/A }
2N/A /*
2N/A * If we are in the /dev/ namespace, produce a raw and
2N/A * non-raw version of the input path.
2N/A */
2N/A if (strncmp(path, "/dev/", 5) == 0) {
2N/A const char *p = path;
2N/A int pos;
2N/A
2N/A if (((p = strstr(path, "/dsk/")) == NULL) &&
2N/A ((p = strstr(path, "/lofi/")) == NULL) &&
2N/A ((p = strstr(path, "/ramdisk/")) == NULL) &&
2N/A ((p = strstr(path, "/dmp/")) == NULL)) {
2N/A /*
2N/A * Assume already got a raw name, copy it,
2N/A * unraw path if needed and return.
2N/A */
2N/A (void) strlcpy(*raw, path, MAXPATHLEN);
2N/A
2N/A if (((p = strstr(path, "/rdsk/")) != NULL) ||
2N/A ((p = strstr(path, "/rlofi/")) != NULL) ||
2N/A ((p = strstr(path, "/rramdisk/")) != NULL) ||
2N/A ((p = strstr(path, "/rdmp/")) != NULL)) {
2N/A pos = (uintptr_t)p - (uintptr_t)path;
2N/A (void) snprintf(*blk, MAXPATHLEN, "%.*s%s",
2N/A pos + 1, path, p + 2);
2N/A } else {
2N/A (void) strlcpy(*blk, path, MAXPATHLEN);
2N/A }
2N/A return;
2N/A }
2N/A /*
2N/A * Assume already got a block name, copy it,
2N/A * setup raw path if needed and return.
2N/A */
2N/A (void) strlcpy(*blk, path, MAXPATHLEN);
2N/A
2N/A if (((p = strstr(path, "/dsk/")) != NULL) ||
2N/A ((p = strstr(path, "/lofi/")) != NULL) ||
2N/A ((p = strstr(path, "/ramdisk/")) != NULL) ||
2N/A ((p = strstr(path, "/dmp/")) != NULL)) {
2N/A pos = (uintptr_t)p - (uintptr_t)path;
2N/A (void) snprintf(*raw, MAXPATHLEN, "%.*sr%s", pos + 1,
2N/A path, p + 1);
2N/A } else {
2N/A (void) strlcpy(*raw, path, MAXPATHLEN);
2N/A }
2N/A return;
2N/A }
2N/A /*
2N/A * We are left with either a file or directory path
2N/A * outside /dev/, just copy it.
2N/A */
2N/A (void) strlcpy(*blk, path, MAXPATHLEN);
2N/A (void) strlcpy(*raw, path, MAXPATHLEN);
2N/A}
2N/A
2N/A/*
2N/A * Given a list of paths to search, find all pools stored on disk.
2N/A * This includes partial pools which are not available to import.
2N/A * If no args are given (iarg->path is 0), then the default directory
2N/A * (/dev/rdsk) is searched. poolname or guid (but not both) are
2N/A * provided by the caller when trying to import a specific pool.
2N/A */
2N/Astatic nvlist_t *
2N/Azpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg)
2N/A{
2N/A int i;
2N/A int dirs = iarg->paths;
2N/A struct dirent64 *dp;
2N/A char *rawpath = NULL, *devpath = NULL, *path = NULL;
2N/A char **dir = iarg->path;
2N/A nvlist_t *ret = NULL;
2N/A static char *default_dir = "/dev/rdsk";
2N/A pool_list_t pools = { 0 };
2N/A pool_entry_t *pe, *penext;
2N/A vdev_entry_t *ve, *venext;
2N/A config_entry_t *ce, *cenext;
2N/A name_entry_t *ne, *nenext;
2N/A avl_tree_t slice_cache;
2N/A rdsk_node_t *slice;
2N/A void *cookie;
2N/A
2N/A if (dirs == 0) {
2N/A dirs = 1;
2N/A dir = &default_dir;
2N/A }
2N/A
2N/A avl_create(&slice_cache, slice_cache_compare,
2N/A sizeof (rdsk_node_t), offsetof(rdsk_node_t, rn_node));
2N/A /*
2N/A * Go through and read the label configuration information from every
2N/A * possible device, organizing the information according to pool GUID
2N/A * and toplevel GUID.
2N/A */
2N/A for (i = 0; i < dirs; i++) {
2N/A tpool_t *t;
2N/A int dfd;
2N/A boolean_t isfile;
2N/A struct stat64 statbuf;
2N/A
2N/A if (stat64(dir[i], &statbuf) != 0) {
2N/A (void) zfs_error_fmt(hdl, EZFS_BADPATH,
2N/A dgettext(TEXT_DOMAIN, "cannot stat '%s'"), dir[i]);
2N/A goto error;
2N/A }
2N/A isfile = !S_ISDIR(statbuf.st_mode);
2N/A
2N/A if ((path = zpool_normalize_path(hdl, dir[i], isfile)) == NULL)
2N/A goto error;
2N/A /*
2N/A * Using raw devices instead of block devices when we're
2N/A * reading the labels skips a bunch of slow operations during
2N/A * close(2) processing, so replace /dev/dsk with /dev/rdsk etc.
2N/A * Determine the raw and non-raw version of the input path.
2N/A */
2N/A zpool_rawunrawpath(hdl, path, &rawpath, &devpath);
2N/A
2N/A if (rawpath == NULL || devpath == NULL)
2N/A goto error;
2N/A /*
2N/A * If opening the rawpath fails, we fall back to using
2N/A * the normalized user supplied input path and ignore
2N/A * this optimization.
2N/A */
2N/A if ((dfd = open64(rawpath, O_RDONLY)) < 0) {
2N/A if ((dfd = open64(path, O_RDONLY)) < 0) {
2N/A zfs_error_aux(hdl, strerror(errno));
2N/A (void) zfs_error_fmt(hdl, EZFS_BADPATH,
2N/A dgettext(TEXT_DOMAIN, "cannot open '%s'"),
2N/A path);
2N/A goto error;
2N/A }
2N/A /*
2N/A * Revert back to normalized input path.
2N/A */
2N/A free(rawpath);
2N/A free(devpath);
2N/A rawpath = zfs_strdup(hdl, path);
2N/A devpath = zfs_strdup(hdl, path);
2N/A if (rawpath == NULL || devpath == NULL) {
2N/A (void) close(dfd);
2N/A goto error;
2N/A }
2N/A }
2N/A
2N/A if (isfile) {
2N/A /*
2N/A * Treat this as a regular file.
2N/A */
2N/A slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
2N/A slice->rn_path = zfs_strdup(hdl, devpath);
2N/A slice->rn_rawpath = zfs_strdup(hdl, rawpath);
2N/A slice->rn_avl = &slice_cache;
2N/A slice->rn_hdl = hdl;
2N/A slice->rn_nozpool = B_FALSE;
2N/A avl_add(&slice_cache, slice);
2N/A } else {
2N/A DIR *dirp = NULL;
2N/A
2N/A if ((dirp = fdopendir(dfd)) == NULL) {
2N/A zfs_error_aux(hdl, strerror(errno));
2N/A (void) zfs_error_fmt(hdl, EZFS_BADPATH,
2N/A dgettext(TEXT_DOMAIN, "cannot open '%s'"),
2N/A rawpath);
2N/A (void) close(dfd);
2N/A goto error;
2N/A }
2N/A /*
2N/A * This is not MT-safe, but we have no MT consumers
2N/A * of libzfs
2N/A */
2N/A while ((dp = readdir64(dirp)) != NULL) {
2N/A const char *name = dp->d_name;
2N/A size_t len;
2N/A
2N/A if (name[0] == '.' &&
2N/A (name[1] == 0 ||
2N/A (name[1] == '.' && name[2] == 0)))
2N/A continue;
2N/A
2N/A slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
2N/A len = strlen(rawpath) + strlen(name) + 2;
2N/A slice->rn_rawpath = zfs_alloc(hdl, len);
2N/A (void) snprintf(slice->rn_rawpath, len,
2N/A "%s%s", rawpath, name);
2N/A len = strlen(devpath) + strlen(name) + 2;
2N/A slice->rn_path = zfs_alloc(hdl, len);
2N/A (void) snprintf(slice->rn_path, len,
2N/A "%s%s", devpath, name);
2N/A slice->rn_avl = &slice_cache;
2N/A slice->rn_hdl = hdl;
2N/A slice->rn_nozpool = B_FALSE;
2N/A avl_add(&slice_cache, slice);
2N/A }
2N/A (void) closedir(dirp);
2N/A }
2N/A (void) close(dfd);
2N/A free(path);
2N/A free(devpath);
2N/A free(rawpath);
2N/A devpath = rawpath = path = NULL;
2N/A
2N/A /*
2N/A * Create a thread pool to do all of this in parallel;
2N/A * rn_nozpool is not protected, so this is racy in that
2N/A * multiple tasks could decide that the same slice can
2N/A * not hold a zpool, which is benign. Also choose
2N/A * double the number of processors; we hold a lot of
2N/A * locks in the kernel, so going beyond this doesn't
2N/A * buy us much.
2N/A */
2N/A t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN),
2N/A 0, NULL);
2N/A for (slice = avl_first(&slice_cache); slice;
2N/A (slice = avl_walk(&slice_cache, slice,
2N/A AVL_AFTER)))
2N/A (void) tpool_dispatch(t, zpool_open_func, slice);
2N/A
2N/A tpool_wait(t);
2N/A tpool_destroy(t);
2N/A
2N/A for (slice = avl_first(&slice_cache); slice != NULL;
2N/A slice = avl_first(&slice_cache)) {
2N/A avl_remove(&slice_cache, slice);
2N/A if (slice->rn_config != NULL) {
2N/A nvlist_t *config = slice->rn_config;
2N/A boolean_t matched = B_TRUE;
2N/A
2N/A if (iarg->poolname != NULL) {
2N/A char *pname;
2N/A
2N/A matched = nvlist_lookup_string(config,
2N/A ZPOOL_CONFIG_POOL_NAME,
2N/A &pname) == 0 &&
2N/A strcmp(iarg->poolname, pname) == 0;
2N/A } else if (iarg->guid != 0) {
2N/A uint64_t this_guid;
2N/A
2N/A matched = nvlist_lookup_uint64(config,
2N/A ZPOOL_CONFIG_POOL_GUID,
2N/A &this_guid) == 0 &&
2N/A iarg->guid == this_guid;
2N/A }
2N/A if (!matched) {
2N/A nvlist_free(config);
2N/A config = NULL;
2N/A } else {
2N/A /* Use non-raw path for the config */
2N/A if (add_config(hdl, &pools,
2N/A slice->rn_path, config)
2N/A != 0) {
2N/A free(slice->rn_path);
2N/A free(slice->rn_rawpath);
2N/A free(slice);
2N/A goto error;
2N/A }
2N/A }
2N/A }
2N/A free(slice->rn_path);
2N/A free(slice->rn_rawpath);
2N/A free(slice);
2N/A }
2N/A }
2N/A
2N/A ret = get_configs(hdl, &pools, iarg->can_be_active);
2N/A
2N/Aerror:
2N/A for (pe = pools.pools; pe != NULL; pe = penext) {
2N/A penext = pe->pe_next;
2N/A for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
2N/A venext = ve->ve_next;
2N/A for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
2N/A cenext = ce->ce_next;
2N/A if (ce->ce_config)
2N/A nvlist_free(ce->ce_config);
2N/A free(ce);
2N/A }
2N/A free(ve);
2N/A }
2N/A free(pe);
2N/A }
2N/A
2N/A for (ne = pools.names; ne != NULL; ne = nenext) {
2N/A nenext = ne->ne_next;
2N/A if (ne->ne_name)
2N/A free(ne->ne_name);
2N/A free(ne);
2N/A }
2N/A
2N/A cookie = NULL;
2N/A while ((slice = avl_destroy_nodes(&slice_cache,
2N/A &cookie)) != NULL) {
2N/A free(slice->rn_path);
2N/A free(slice->rn_rawpath);
2N/A free(slice);
2N/A }
2N/A avl_destroy(&slice_cache);
2N/A
2N/A free(path);
2N/A free(rawpath);
2N/A free(devpath);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Anvlist_t *
2N/Azpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
2N/A{
2N/A importargs_t iarg = { 0 };
2N/A
2N/A iarg.paths = argc;
2N/A iarg.path = argv;
2N/A
2N/A return (zpool_find_import_impl(hdl, &iarg));
2N/A}
2N/A
2N/Anvlist_t *
2N/Azpool_read_cachefile(libzfs_handle_t *hdl, const char *cachefile)
2N/A{
2N/A char *buf;
2N/A int fd;
2N/A struct stat64 statbuf;
2N/A nvlist_t *nvl;
2N/A
2N/A if ((fd = open(cachefile, O_RDONLY)) < 0) {
2N/A zfs_error_aux(hdl, "%s", strerror(errno));
2N/A (void) zfs_error(hdl, EZFS_BADCACHE,
2N/A dgettext(TEXT_DOMAIN, "failed to open cache file"));
2N/A return (NULL);
2N/A }
2N/A
2N/A if (fstat64(fd, &statbuf) != 0) {
2N/A zfs_error_aux(hdl, "%s", strerror(errno));
2N/A (void) close(fd);
2N/A (void) zfs_error(hdl, EZFS_BADCACHE,
2N/A dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
2N/A (void) close(fd);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2N/A (void) close(fd);
2N/A free(buf);
2N/A (void) zfs_error(hdl, EZFS_BADCACHE,
2N/A dgettext(TEXT_DOMAIN,
2N/A "failed to read cache file contents"));
2N/A return (NULL);
2N/A }
2N/A
2N/A (void) close(fd);
2N/A
2N/A if (nvlist_unpack(buf, statbuf.st_size, &nvl, 0) != 0) {
2N/A free(buf);
2N/A (void) zfs_error(hdl, EZFS_BADCACHE,
2N/A dgettext(TEXT_DOMAIN,
2N/A "invalid or corrupt cache file contents"));
2N/A return (NULL);
2N/A }
2N/A
2N/A free(buf);
2N/A return (nvl);
2N/A}
2N/A/*
2N/A * Given a cache file, return the contents as a list of importable pools.
2N/A * poolname or guid (but not both) are provided by the caller when trying
2N/A * to import a specific pool. If 'trusted' is specified, then we don't
2N/A * attempt a TRYIMPORT and treat the cachefile as the absolute truth. When
2N/A * using this option, the resulting config won't be complete in the traditional
2N/A * sense, as there will be no vdev statistics or state information.
2N/A */
2N/Astatic nvlist_t *
2N/Azpool_find_import_cached_impl(libzfs_handle_t *hdl, const char *cachefile,
2N/A char *poolname, uint64_t guid, boolean_t trusted, boolean_t validate)
2N/A{
2N/A nvlist_t *raw, *src, *dst;
2N/A nvlist_t *pools;
2N/A nvpair_t *elem;
2N/A char *src_name, *dst_name;
2N/A uint64_t src_guid, dst_guid;
2N/A boolean_t active;
2N/A
2N/A verify(poolname == NULL || guid == 0);
2N/A
2N/A if ((raw = zpool_read_cachefile(hdl, cachefile)) == NULL)
2N/A return (NULL);
2N/A
2N/A /*
2N/A * Go through and get the current state of the pools and refresh their
2N/A * state.
2N/A */
2N/A if (nvlist_alloc(&pools, 0, 0) != 0) {
2N/A (void) no_memory(hdl);
2N/A nvlist_free(raw);
2N/A return (NULL);
2N/A }
2N/A
2N/A elem = NULL;
2N/A while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
2N/A verify(nvpair_value_nvlist(elem, &src) == 0);
2N/A
2N/A verify(nvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME,
2N/A &src_name) == 0);
2N/A if (poolname != NULL && strcmp(poolname, src_name) != 0)
2N/A continue;
2N/A
2N/A verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID,
2N/A &src_guid) == 0);
2N/A if (guid != 0 && guid != src_guid)
2N/A continue;
2N/A
2N/A if (pool_active(hdl, src_name, src_guid, &active) != 0) {
2N/A nvlist_free(raw);
2N/A nvlist_free(pools);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (active)
2N/A continue;
2N/A
2N/A if (!validate) {
2N/A if (nvlist_add_nvlist(pools, nvpair_name(elem),
2N/A src) != 0) {
2N/A nvlist_free(raw);
2N/A nvlist_free(pools);
2N/A return (NULL);
2N/A }
2N/A continue;
2N/A }
2N/A
2N/A if ((dst = refresh_config(hdl, src, trusted)) == NULL) {
2N/A nvlist_free(raw);
2N/A nvlist_free(pools);
2N/A return (NULL);
2N/A }
2N/A
2N/A verify(nvlist_lookup_string(dst, ZPOOL_CONFIG_POOL_NAME,
2N/A &dst_name) == 0);
2N/A verify(nvlist_lookup_uint64(dst, ZPOOL_CONFIG_POOL_GUID,
2N/A &dst_guid) == 0);
2N/A
2N/A if (!trusted &&
2N/A (strcmp(src_name, dst_name) != 0 || src_guid != dst_guid)) {
2N/A (void) zfs_error_fmt(hdl, EZFS_BADCACHE,
2N/A dgettext(TEXT_DOMAIN,
2N/A "stale configuration for pool '%s'"), src_name);
2N/A nvlist_free(dst);
2N/A continue;
2N/A }
2N/A
2N/A if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
2N/A (void) no_memory(hdl);
2N/A nvlist_free(dst);
2N/A nvlist_free(raw);
2N/A nvlist_free(pools);
2N/A return (NULL);
2N/A }
2N/A nvlist_free(dst);
2N/A }
2N/A nvlist_free(raw);
2N/A
2N/A return (pools);
2N/A}
2N/A
2N/Anvlist_t *
2N/Azpool_find_import_cached(libzfs_handle_t *hdl, const char *cache,
2N/A char *name, uint64_t guid)
2N/A{
2N/A return (zpool_find_import_cached_impl(hdl, cache, name, guid, B_TRUE,
2N/A B_TRUE));
2N/A}
2N/A
2N/Astatic int
2N/Aname_or_guid_exists(zpool_handle_t *zhp, void *data)
2N/A{
2N/A importargs_t *import = data;
2N/A int found = 0;
2N/A
2N/A if (import->poolname != NULL) {
2N/A char *pool_name;
2N/A
2N/A verify(nvlist_lookup_string(zhp->zpool_config,
2N/A ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0);
2N/A if (strcmp(pool_name, import->poolname) == 0)
2N/A found = 1;
2N/A } else {
2N/A uint64_t pool_guid;
2N/A
2N/A verify(nvlist_lookup_uint64(zhp->zpool_config,
2N/A ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0);
2N/A if (pool_guid == import->guid)
2N/A found = 1;
2N/A }
2N/A
2N/A zpool_close(zhp);
2N/A return (found);
2N/A}
2N/A
2N/Anvlist_t *
2N/Azpool_search_import(libzfs_handle_t *hdl, importargs_t *import)
2N/A{
2N/A verify(import->poolname == NULL || import->guid == 0);
2N/A
2N/A if (import->unique)
2N/A import->exists = zpool_iter(hdl, name_or_guid_exists, import);
2N/A
2N/A if (import->cachefile != NULL) {
2N/A nvlist_t *pools;
2N/A
2N/A pools = zpool_find_import_cached_impl(hdl, import->cachefile,
2N/A import->poolname, import->guid, import->trust_cache,
2N/A !import->no_validation);
2N/A
2N/A if (pools != NULL && nvlist_next_nvpair(pools, NULL) == NULL) {
2N/A nvlist_free(pools);
2N/A pools = NULL;
2N/A }
2N/A
2N/A return (pools);
2N/A }
2N/A
2N/A return (zpool_find_import_impl(hdl, import));
2N/A}
2N/A
2N/A/*
2N/A * Contracted interface for cluster to find the pool(s) configuration
2N/A * from specified cachefile path or set of locations/devices.
2N/A */
2N/Anvlist_t *
2N/Azpool_find_import_cluster(libzfs_handle_t *hdl, char *poolname,
2N/A uint64_t guid, const char *cachefile, boolean_t cachefile_trusted,
2N/A char **searchpath, int npaths)
2N/A{
2N/A importargs_t iarg = { 0 };
2N/A
2N/A if (cachefile != NULL) {
2N/A return (zpool_find_import_cached_impl(hdl, cachefile,
2N/A poolname, guid, cachefile_trusted, B_TRUE));
2N/A }
2N/A
2N/A iarg.poolname = poolname;
2N/A iarg.guid = guid;
2N/A iarg.paths = npaths;
2N/A iarg.path = searchpath;
2N/A
2N/A return (zpool_find_import_impl(hdl, &iarg));
2N/A}
2N/A
2N/Aboolean_t
2N/Afind_guid(nvlist_t *nv, uint64_t guid)
2N/A{
2N/A uint64_t tmp;
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A
2N/A verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
2N/A if (tmp == guid)
2N/A return (B_TRUE);
2N/A
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2N/A &child, &children) == 0) {
2N/A for (c = 0; c < children; c++)
2N/A if (find_guid(child[c], guid))
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/Atypedef struct aux_cbdata {
2N/A const char *cb_type;
2N/A uint64_t cb_guid;
2N/A zpool_handle_t *cb_zhp;
2N/A} aux_cbdata_t;
2N/A
2N/Astatic int
2N/Afind_aux(zpool_handle_t *zhp, void *data)
2N/A{
2N/A aux_cbdata_t *cbp = data;
2N/A nvlist_t **list;
2N/A uint_t i, count;
2N/A uint64_t guid;
2N/A nvlist_t *nvroot;
2N/A
2N/A verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2N/A &nvroot) == 0);
2N/A
2N/A if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
2N/A &list, &count) == 0) {
2N/A for (i = 0; i < count; i++) {
2N/A verify(nvlist_lookup_uint64(list[i],
2N/A ZPOOL_CONFIG_GUID, &guid) == 0);
2N/A if (guid == cbp->cb_guid) {
2N/A cbp->cb_zhp = zhp;
2N/A return (1);
2N/A }
2N/A }
2N/A }
2N/A
2N/A zpool_close(zhp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Determines if the pool is in use. If so, it returns true and the state of
2N/A * the pool as well as the name of the pool. Both strings are allocated and
2N/A * must be freed by the caller.
2N/A */
2N/Aint
2N/Azpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
2N/A boolean_t *inuse)
2N/A{
2N/A nvlist_t *config;
2N/A char *name;
2N/A boolean_t ret;
2N/A uint64_t guid, vdev_guid;
2N/A zpool_handle_t *zhp;
2N/A nvlist_t *pool_config;
2N/A uint64_t stateval, isspare;
2N/A aux_cbdata_t cb = { 0 };
2N/A boolean_t isactive;
2N/A
2N/A *inuse = B_FALSE;
2N/A
2N/A if (zpool_read_label(fd, &config) != 0) {
2N/A (void) no_memory(hdl);
2N/A return (-1);
2N/A }
2N/A
2N/A if (config == NULL)
2N/A return (0);
2N/A
2N/A verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2N/A &stateval) == 0);
2N/A verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
2N/A &vdev_guid) == 0);
2N/A
2N/A if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
2N/A verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2N/A &name) == 0);
2N/A verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2N/A &guid) == 0);
2N/A }
2N/A
2N/A switch (stateval) {
2N/A case POOL_STATE_EXPORTED:
2N/A /*
2N/A * A pool with an exported state may in fact be imported
2N/A * read-only, so check the in-core state to see if it's
2N/A * active and imported read-only. If it is, set
2N/A * its state to active.
2N/A */
2N/A if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
2N/A (zhp = zpool_open_canfail(hdl, name)) != NULL &&
2N/A zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
2N/A stateval = POOL_STATE_ACTIVE;
2N/A
2N/A ret = B_TRUE;
2N/A break;
2N/A
2N/A case POOL_STATE_ACTIVE:
2N/A /*
2N/A * For an active pool, we have to determine if it's really part
2N/A * of a currently active pool (in which case the pool will exist
2N/A * and the guid will be the same), or whether it's part of an
2N/A * active pool that was disconnected without being explicitly
2N/A * exported.
2N/A */
2N/A if (pool_active(hdl, name, guid, &isactive) != 0) {
2N/A nvlist_free(config);
2N/A return (-1);
2N/A }
2N/A
2N/A if (isactive) {
2N/A /*
2N/A * Because the device may have been removed while
2N/A * offlined, we only report it as active if the vdev is
2N/A * still present in the config. Otherwise, pretend like
2N/A * it's not in use.
2N/A */
2N/A if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
2N/A (pool_config = zpool_get_config(zhp, NULL))
2N/A != NULL) {
2N/A nvlist_t *nvroot;
2N/A
2N/A verify(nvlist_lookup_nvlist(pool_config,
2N/A ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2N/A ret = find_guid(nvroot, vdev_guid);
2N/A } else {
2N/A ret = B_FALSE;
2N/A }
2N/A
2N/A /*
2N/A * If this is an active spare within another pool, we
2N/A * treat it like an unused hot spare. This allows the
2N/A * user to create a pool with a hot spare that currently
2N/A * in use within another pool. Since we return B_TRUE,
2N/A * libdiskmgt will continue to prevent generic consumers
2N/A * from using the device.
2N/A */
2N/A if (ret && nvlist_lookup_uint64(config,
2N/A ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
2N/A stateval = POOL_STATE_SPARE;
2N/A
2N/A if (zhp != NULL)
2N/A zpool_close(zhp);
2N/A } else {
2N/A stateval = POOL_STATE_POTENTIALLY_ACTIVE;
2N/A ret = B_TRUE;
2N/A }
2N/A break;
2N/A
2N/A case POOL_STATE_SPARE:
2N/A /*
2N/A * For a hot spare, it can be either definitively in use, or
2N/A * potentially active. To determine if it's in use, we iterate
2N/A * over all pools in the system and search for one with a spare
2N/A * with a matching guid.
2N/A *
2N/A * Due to the shared nature of spares, we don't actually report
2N/A * the potentially active case as in use. This means the user
2N/A * can freely create pools on the hot spares of exported pools,
2N/A * but to do otherwise makes the resulting code complicated, and
2N/A * we end up having to deal with this case anyway.
2N/A */
2N/A cb.cb_zhp = NULL;
2N/A cb.cb_guid = vdev_guid;
2N/A cb.cb_type = ZPOOL_CONFIG_SPARES;
2N/A if (zpool_iter(hdl, find_aux, &cb) == 1) {
2N/A name = (char *)zpool_get_name(cb.cb_zhp);
2N/A ret = TRUE;
2N/A } else {
2N/A ret = FALSE;
2N/A }
2N/A break;
2N/A
2N/A case POOL_STATE_L2CACHE:
2N/A
2N/A /*
2N/A * Check if any pool is currently using this l2cache device.
2N/A */
2N/A cb.cb_zhp = NULL;
2N/A cb.cb_guid = vdev_guid;
2N/A cb.cb_type = ZPOOL_CONFIG_L2CACHE;
2N/A if (zpool_iter(hdl, find_aux, &cb) == 1) {
2N/A name = (char *)zpool_get_name(cb.cb_zhp);
2N/A ret = TRUE;
2N/A } else {
2N/A ret = FALSE;
2N/A }
2N/A break;
2N/A
2N/A default:
2N/A ret = B_FALSE;
2N/A }
2N/A
2N/A
2N/A if (ret) {
2N/A if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
2N/A if (cb.cb_zhp)
2N/A zpool_close(cb.cb_zhp);
2N/A nvlist_free(config);
2N/A return (-1);
2N/A }
2N/A *state = (pool_state_t)stateval;
2N/A }
2N/A
2N/A if (cb.cb_zhp)
2N/A zpool_close(cb.cb_zhp);
2N/A
2N/A nvlist_free(config);
2N/A *inuse = ret;
2N/A return (0);
2N/A}