libzfs_import.c revision 88ecc943b4eb72f7c4fbbd8435997b85ef171fc3
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * CDDL HEADER START
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * The contents of this file are subject to the terms of the
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Common Development and Distribution License (the "License").
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * You may not use this file except in compliance with the License.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * See the License for the specific language governing permissions
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * and limitations under the License.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * When distributing Covered Code, include this CDDL HEADER in each
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * If applicable, add the following below this CDDL HEADER, with the
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * fields enclosed by brackets "[]" replaced with your own identifying
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * information: Portions Copyright [yyyy] [name of copyright owner]
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * CDDL HEADER END
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Use is subject to license terms.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Pool import support functions.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * To import a pool, we rely on reading the configuration information from the
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * ZFS label of each device. If we successfully read the label, then we
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * organize the configuration information in the following hierarchy:
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * pool guid -> toplevel vdev guid -> label txg
9719310a57482091af0a7f0ee31b5e2eec35f154David Hollister * Duplicate entries matching this same tuple will be discarded. Once we have
9719310a57482091af0a7f0ee31b5e2eec35f154David Hollister * examined every device, we pick the best label txg config for each toplevel
9719310a57482091af0a7f0ee31b5e2eec35f154David Hollister * vdev. We then arrange these toplevel vdevs into a complete pool config, and
9719310a57482091af0a7f0ee31b5e2eec35f154David Hollister * update any paths that have changed. Finally, we attempt to import the pool
9719310a57482091af0a7f0ee31b5e2eec35f154David Hollister * using our derived config, and record the results.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Intermediate structures used to gather configuration information.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdhtypedef struct config_entry {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhtypedef struct vdev_entry {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhtypedef struct pool_entry {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhtypedef struct name_entry {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhtypedef struct pool_list {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhstatic char *
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Go through and fix up any path and/or devid information for the given vdev
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * configuration.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (c = 0; c < children; c++)
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (0);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * This is a leaf (file or disk) vdev. In either case, go through
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * the name list and see if we find a matching guid. If so, replace
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * the path and see if we can calculate a new devid.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * There may be multiple names associated with a particular guid, in
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * which case we have overlapping slices or multiple paths to the same
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * disk. If this is the case, then we want to pick the path that is
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * the most similar to the original, where "most similar" is the number
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * of matching characters starting from the end of the path. This will
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * preserve slice numbers even if the disks have been reorganized, and
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * will also catch preferred disk names if multiple paths exist.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * At this point, 'count' is the number of characters
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * matched from the end.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (0);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (0);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * Add the given configuration to the list of known devices.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reedadd_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * If this is a hot spare not currently in use or level 2 cache
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * device, add it to the list of names to translate, but don't do
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * anything else.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (0);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * If we have a valid config but cannot read any of these fields, then
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * it means we have a half-initialized label. In vdev_label_init()
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * we write a label with txg == 0 so that we can identify the device
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * in case the user refers to the same disk later on. If we fail to
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * create the pool, we'll be left with a label in this state
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * which should not be considered part of a valid pool.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (0);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * First, see if we know about this pool. If not, then add it to the
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * list of known pools.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * Second, see if we know about this toplevel vdev. Add it if its
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * missing.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * Third, see if we have a config with a matching transaction group. If
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * so, then we do nothing. Otherwise, add it to the list of known
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * configs.
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed return (-1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * At this point we've successfully added our config to the list of
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * known configs. The last thing to do is add the vdev guid -> path
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * mappings so that we can fix up the configuration as necessary before
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed * doing the import.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (-1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (-1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Returns true if the named pool matches the given GUID.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhpool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (-1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (0);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (0);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Determine if the vdev id is a hole in the namespace.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhvdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (int c = 0; c < holes; c++) {
f7aef0b0ce0e9a2c3da9c2e3aa9122f3642c1459Reed /* Top-level is a hole */
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * Convert our list of pools into the definitive set of configurations. We
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * start by picking the best config for each toplevel vdev. Once that's done,
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * we assemble the toplevel vdevs into a full config for the pool. We make a
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * pass to fix up any incorrect paths, and then add it to the main list to
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * return to the user.
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdhget_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot;
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * Iterate over all toplevel vdevs. Grab the pool configuration
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * from the first one we find, and then go through the rest and
c3bc407cfbd238a18e4728ad5f36f39cecdb062fdh * add them as necessary to the 'vdevs' member of the config.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Determine the best configuration for this vdev by
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * selecting the config with the latest transaction
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * We rely on the fact that the max txg for the
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * pool will contain the most up-to-date information
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * about the valid top-levels in the vdev namespace.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Copy the relevant pieces of data to the pool
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * configuration:
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * pool guid
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * pool state
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * hostid (if available)
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * hostname (if available)
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Add this top-level vdev to the child array.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh sizeof (nvlist_t *));
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (c = 0; c < children; c++)
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * If we have information about all the top-levels then
b18a19c275d2531444fcd2f66664cbe3c6897f6aJesse Butler * clean up the nvlist which we've constructed. This
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * means removing any extraneous devices that are
b18a19c275d2531444fcd2f66664cbe3c6897f6aJesse Butler * beyond the valid range or adding devices to the end
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * of our array which appear to be missing.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh sizeof (nvlist_t *));
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (c = 0; c < children; c++)
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * The vdev namespace may contain holes as a result of
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * device removal. We must add them back into the vdev
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * tree before we process any missing devices.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (holes > 0) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (c = 0; c < children; c++) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Holes in the namespace are treated as
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * "hole" top-level vdevs and have a
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * special flag set on them.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Look for any missing top-level vdevs. If this is the case,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * create a faked up 'missing' vdev as a placeholder. We cannot
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * simply compress the child array, because the kernel performs
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * certain checks to make sure the vdev IDs match their location
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * in the configuration.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (c = 0; c < children; c++) {
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Put all of this pool's top-level vdevs into a root vdev.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh for (c = 0; c < children; c++)
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Go through and fix up any paths and/or devids based on our
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * known list of vdev GUID -> path mappings.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Add the root vdev to this pool's configuration.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * zdb uses this path to report on active pools that were
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * imported or created using -R.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Determine if this pool is currently active, in which case we
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * can't actually import it.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Go through and update the paths for spares, now that we have
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister for (i = 0; i < nspares; i++) {
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Update the paths for l2cache devices.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (i = 0; i < nl2cache; i++) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Restore the original information read from the actual label.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh if (hostid != 0) {
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Add this pool to the list of configs.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister if (nvlist_add_nvlist(ret, name, config) != 0)
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh for (c = 0; c < children; c++)
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Return the offset of the given label.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * Given a file descriptor, read the label information and return an nvlist
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh * describing the configuration, if there is one.
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh return (0);
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh return (-1);
14d6cf0a831fafb130183d4a32c30acbe84b9f4bdh for (l = 0; l < VDEV_LABELS; l++) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (0);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (0);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Given a list of directories to search, find all pools stored on disk. This
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * includes partial pools which are not available to import. If no args are
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * given (argc is 0), then the default directory (/dev/dsk) is searched.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * poolname or guid (but not both) are provided by the caller when trying
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * to import a specific pool.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhzpool_find_import_impl(libzfs_handle_t *hdl, int argc, char **argv,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (argc == 0) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Go through and read the label configuration information from every
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * possible device, organizing the information according to pool GUID
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * and toplevel GUID.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (i = 0; i < argc; i++) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh /* use realpath to normalize the path */
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Using raw devices instead of block devices when we're
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * reading the labels skips a bunch of slow operations during
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * close(2) processing, so we replace /dev/dsk with /dev/rdsk.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * This is not MT-safe, but we have no MT consumers of libzfs
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Ignore failed stats. We only want regular
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * files, character devs and block devs.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh } else if (guid != 0) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh /* use the non-raw path for the config */
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister for (pe = pools.pools; pe != NULL; pe = penext) {
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister for (ne = pools.names; ne != NULL; ne = nenext) {
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollisterzpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, NULL, 0));
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollisterzpool_find_import_byname(libzfs_handle_t *hdl, int argc, char **argv,
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, pool, 0));
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollisterzpool_find_import_byguid(libzfs_handle_t *hdl, int argc, char **argv,
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister return (zpool_find_import_impl(hdl, argc, argv, B_FALSE, NULL, guid));
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollisterzpool_find_import_activeok(libzfs_handle_t *hdl, int argc, char **argv)
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister return (zpool_find_import_impl(hdl, argc, argv, B_TRUE, NULL, 0));
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister * Given a cache file, return the contents as a list of importable pools.
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister * poolname or guid (but not both) are provided by the caller when trying
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister * to import a specific pool.
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollisterzpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh "failed to read cache file contents"));
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh "invalid or corrupt cache file contents"));
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Go through and get the current state of the pools and refresh their
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (guid != 0) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (c = 0; c < children; c++)
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhtypedef struct aux_cbdata {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh const char *cb_type;
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh for (i = 0; i < count; i++) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (0);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Determines if the pool is in use. If so, it returns true and the state of
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * the pool as well as the name of the pool. Both strings are allocated and
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * must be freed by the caller.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dhzpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (-1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (0);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * For an active pool, we have to determine if it's really part
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * of a currently active pool (in which case the pool will exist
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * and the guid will be the same), or whether it's part of an
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * active pool that was disconnected without being explicitly
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * exported.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (-1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * Because the device may have been removed while
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * offlined, we only report it as active if the vdev is
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * still present in the config. Otherwise, pretend like
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * it's not in use.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * If this is an active spare within another pool, we
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * treat it like an unused hot spare. This allows the
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * user to create a pool with a hot spare that currently
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * in use within another pool. Since we return B_TRUE,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * libdiskmgt will continue to prevent generic consumers
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * from using the device.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * For a hot spare, it can be either definitively in use, or
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * potentially active. To determine if it's in use, we iterate
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * over all pools in the system and search for one with a spare
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * with a matching guid.
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister * Due to the shared nature of spares, we don't actually report
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * the potentially active case as in use. This means the user
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * can freely create pools on the hot spares of exported pools,
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * but to do otherwise makes the resulting code complicated, and
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh * we end up having to deal with this case anyway.
9aed162131f1840d0bc1cd0275f4d7144f3690f0David Hollister * Check if any pool is currently using this l2cache device.
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (-1);
4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6dh return (0);