4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Pool import support functions.
31 * To import a pool, we rely on reading the configuration information from the
32 * ZFS label of each device. If we successfully read the label, then we
33 * organize the configuration information in the following hierarchy:
35 * pool guid -> toplevel vdev guid -> label txg
37 * Duplicate entries matching this same tuple will be discarded. Once we have
38 * examined every device, we pick the best label txg config for each toplevel
39 * vdev. We then arrange these toplevel vdevs into a complete pool config, and
40 * update any paths that have changed. Finally, we attempt to import the pool
41 * using our derived config, and record the results.
55 #include <sys/vdev_impl.h>
58 #include "libzfs_impl.h"
61 * Intermediate structures used to gather configuration information.
63 typedef struct config_entry {
66 struct config_entry *ce_next;
69 typedef struct vdev_entry {
71 config_entry_t *ve_configs;
72 struct vdev_entry *ve_next;
75 typedef struct pool_entry {
77 vdev_entry_t *pe_vdevs;
78 struct pool_entry *pe_next;
81 typedef struct name_entry {
84 struct name_entry *ne_next;
87 typedef struct pool_list {
93 get_devid(const char *path)
99 if ((fd = open(path, O_RDONLY)) < 0)
104 if (devid_get(fd, &devid) == 0) {
105 if (devid_get_minor_name(fd, &minor) == 0)
106 ret = devid_str_encode(devid, minor);
108 devid_str_free(minor);
117 * Go through and fix up any path and/or devid information for the given vdev
121 fix_paths(nvlist_t *nv, name_entry_t *names)
126 name_entry_t *ne, *best;
130 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
131 &child, &children) == 0) {
132 for (c = 0; c < children; c++)
133 if (fix_paths(child[c], names) != 0)
139 * This is a leaf (file or disk) vdev. In either case, go through
140 * the name list and see if we find a matching guid. If so, replace
141 * the path and see if we can calculate a new devid.
143 * There may be multiple names associated with a particular guid, in
144 * which case we have overlapping slices or multiple paths to the same
145 * disk. If this is the case, then we want to pick the path that is
146 * the most similar to the original, where "most similar" is the number
147 * of matching characters starting from the end of the path. This will
148 * preserve slice numbers even if the disks have been reorganized, and
149 * will also catch preferred disk names if multiple paths exist.
151 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
152 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
157 for (ne = names; ne != NULL; ne = ne->ne_next) {
158 if (ne->ne_guid == guid) {
159 const char *src, *dst;
167 src = ne->ne_name + strlen(ne->ne_name) - 1;
168 dst = path + strlen(path) - 1;
169 for (count = 0; src >= ne->ne_name && dst >= path;
170 src--, dst--, count++)
175 * At this point, 'count' is the number of characters
176 * matched from the end.
178 if (count > matched || best == NULL) {
188 if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
191 if ((devid = get_devid(best->ne_name)) == NULL) {
192 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
194 if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
196 devid_str_free(devid);
203 * Add the given configuration to the list of known devices.
206 add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
209 uint64_t pool_guid, vdev_guid, top_guid, txg, state;
216 * If this is a hot spare not currently in use, add it to the list of
217 * names to translate, but don't do anything else.
219 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
220 &state) == 0 && state == POOL_STATE_SPARE &&
221 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
222 if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
225 if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
229 ne->ne_guid = vdev_guid;
230 ne->ne_next = pl->names;
236 * If we have a valid config but cannot read any of these fields, then
237 * it means we have a half-initialized label. In vdev_label_init()
238 * we write a label with txg == 0 so that we can identify the device
239 * in case the user refers to the same disk later on. If we fail to
240 * create the pool, we'll be left with a label in this state
241 * which should not be considered part of a valid pool.
243 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
245 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
247 nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
249 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
250 &txg) != 0 || txg == 0) {
256 * First, see if we know about this pool. If not, then add it to the
257 * list of known pools.
259 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
260 if (pe->pe_guid == pool_guid)
265 if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
269 pe->pe_guid = pool_guid;
270 pe->pe_next = pl->pools;
275 * Second, see if we know about this toplevel vdev. Add it if its
278 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
279 if (ve->ve_guid == top_guid)
284 if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
288 ve->ve_guid = top_guid;
289 ve->ve_next = pe->pe_vdevs;
294 * Third, see if we have a config with a matching transaction group. If
295 * so, then we do nothing. Otherwise, add it to the list of known
298 for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
299 if (ce->ce_txg == txg)
304 if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
309 ce->ce_config = config;
310 ce->ce_next = ve->ve_configs;
317 * At this point we've successfully added our config to the list of
318 * known configs. The last thing to do is add the vdev guid -> path
319 * mappings so that we can fix up the configuration as necessary before
322 if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
325 if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
330 ne->ne_guid = vdev_guid;
331 ne->ne_next = pl->names;
338 * Returns true if the named pool matches the given GUID.
341 pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
347 if (zpool_open_silent(hdl, name, &zhp) != 0)
355 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
360 *isactive = (theguid == guid);
365 * Convert our list of pools into the definitive set of configurations. We
366 * start by picking the best config for each toplevel vdev. Once that's done,
367 * we assemble the toplevel vdevs into a full config for the pool. We make a
368 * pass to fix up any incorrect paths, and then add it to the main list to
369 * return to the user.
372 get_configs(libzfs_handle_t *hdl, pool_list_t *pl)
377 nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot;
380 boolean_t config_seen;
382 char *name, *hostname;
383 zfs_cmd_t zc = { 0 };
384 uint64_t version, guid;
388 nvlist_t **child = NULL;
393 if (nvlist_alloc(&ret, 0, 0) != 0)
396 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
399 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
401 config_seen = B_FALSE;
404 * Iterate over all toplevel vdevs. Grab the pool configuration
405 * from the first one we find, and then go through the rest and
406 * add them as necessary to the 'vdevs' member of the config.
408 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
411 * Determine the best configuration for this vdev by
412 * selecting the config with the latest transaction
416 for (ce = ve->ve_configs; ce != NULL;
419 if (ce->ce_txg > best_txg) {
421 best_txg = ce->ce_txg;
427 * Copy the relevant pieces of data to the pool
434 * hostid (if available)
435 * hostname (if available)
439 verify(nvlist_lookup_uint64(tmp,
440 ZPOOL_CONFIG_VERSION, &version) == 0);
441 if (nvlist_add_uint64(config,
442 ZPOOL_CONFIG_VERSION, version) != 0)
444 verify(nvlist_lookup_uint64(tmp,
445 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
446 if (nvlist_add_uint64(config,
447 ZPOOL_CONFIG_POOL_GUID, guid) != 0)
449 verify(nvlist_lookup_string(tmp,
450 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
451 if (nvlist_add_string(config,
452 ZPOOL_CONFIG_POOL_NAME, name) != 0)
454 verify(nvlist_lookup_uint64(tmp,
455 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
456 if (nvlist_add_uint64(config,
457 ZPOOL_CONFIG_POOL_STATE, state) != 0)
460 if (nvlist_lookup_uint64(tmp,
461 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
462 if (nvlist_add_uint64(config,
463 ZPOOL_CONFIG_HOSTID, hostid) != 0)
465 verify(nvlist_lookup_string(tmp,
466 ZPOOL_CONFIG_HOSTNAME,
468 if (nvlist_add_string(config,
469 ZPOOL_CONFIG_HOSTNAME,
474 config_seen = B_TRUE;
478 * Add this top-level vdev to the child array.
480 verify(nvlist_lookup_nvlist(tmp,
481 ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
482 verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
484 if (id >= children) {
487 newchild = zfs_alloc(hdl, (id + 1) *
488 sizeof (nvlist_t *));
489 if (newchild == NULL)
492 for (c = 0; c < children; c++)
493 newchild[c] = child[c];
499 if (nvlist_dup(nvtop, &child[id], 0) != 0)
504 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
508 * Look for any missing top-level vdevs. If this is the case,
509 * create a faked up 'missing' vdev as a placeholder. We cannot
510 * simply compress the child array, because the kernel performs
511 * certain checks to make sure the vdev IDs match their location
512 * in the configuration.
514 for (c = 0; c < children; c++)
515 if (child[c] == NULL) {
517 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
520 if (nvlist_add_string(missing,
522 VDEV_TYPE_MISSING) != 0 ||
523 nvlist_add_uint64(missing,
524 ZPOOL_CONFIG_ID, c) != 0 ||
525 nvlist_add_uint64(missing,
526 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
527 nvlist_free(missing);
534 * Put all of this pool's top-level vdevs into a root vdev.
536 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
538 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
539 VDEV_TYPE_ROOT) != 0 ||
540 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
541 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
542 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
543 child, children) != 0) {
548 for (c = 0; c < children; c++)
549 nvlist_free(child[c]);
555 * Go through and fix up any paths and/or devids based on our
556 * known list of vdev GUID -> path mappings.
558 if (fix_paths(nvroot, pl->names) != 0) {
564 * Add the root vdev to this pool's configuration.
566 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
574 * Determine if this pool is currently active, in which case we
575 * can't actually import it.
577 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
579 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
582 if (pool_active(hdl, name, guid, &isactive) != 0)
592 * Try to do the import in order to get vdev state.
594 if (zcmd_write_src_nvlist(hdl, &zc, config, &len) != 0)
600 if (zcmd_alloc_dst_nvlist(hdl, &zc, len * 2) != 0) {
601 zcmd_free_nvlists(&zc);
605 while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
606 &zc)) != 0 && errno == ENOMEM) {
607 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
608 zcmd_free_nvlists(&zc);
614 (void) zpool_standard_error(hdl, errno,
615 dgettext(TEXT_DOMAIN, "cannot discover pools"));
616 zcmd_free_nvlists(&zc);
620 if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
621 zcmd_free_nvlists(&zc);
625 zcmd_free_nvlists(&zc);
628 * Go through and update the paths for spares, now that we have
631 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
633 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
634 &spares, &nspares) == 0) {
635 for (i = 0; i < nspares; i++) {
636 if (fix_paths(spares[i], pl->names) != 0)
642 * Restore the original information read from the actual label.
644 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
646 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
649 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
651 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
656 * Add this pool to the list of configs.
658 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
660 if (nvlist_add_nvlist(ret, name, config) != 0)
670 (void) no_memory(hdl);
674 for (c = 0; c < children; c++)
675 nvlist_free(child[c]);
682 * Return the offset of the given label.
685 label_offset(size_t size, int l)
687 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
688 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
692 * Given a file descriptor, read the label information and return an nvlist
693 * describing the configuration, if there is one.
696 zpool_read_label(int fd, nvlist_t **config)
698 struct stat64 statbuf;
705 if (fstat64(fd, &statbuf) == -1)
708 if ((label = malloc(sizeof (vdev_label_t))) == NULL)
711 for (l = 0; l < VDEV_LABELS; l++) {
712 if (pread(fd, label, sizeof (vdev_label_t),
713 label_offset(statbuf.st_size, l)) != sizeof (vdev_label_t))
716 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
717 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
720 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
721 &state) != 0 || state > POOL_STATE_SPARE) {
722 nvlist_free(*config);
726 if (state != POOL_STATE_SPARE &&
727 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
728 &txg) != 0 || txg == 0)) {
729 nvlist_free(*config);
743 * Given a list of directories to search, find all pools stored on disk. This
744 * includes partial pools which are not available to import. If no args are
745 * given (argc is 0), then the default directory (/dev) is searched.
748 zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
751 char path[MAXPATHLEN];
752 nvlist_t *ret = NULL, *config;
754 pool_list_t pools = { 0 };
755 pool_entry_t *pe, *penext;
756 vdev_entry_t *ve, *venext;
757 config_entry_t *ce, *cenext;
758 name_entry_t *ne, *nenext;
762 struct gprovider *pp;
765 * Go through and read the label configuration information from every
766 * possible device, organizing the information according to pool GUID
770 fd = geom_gettree(&mesh);
773 LIST_FOREACH(mp, &mesh.lg_class, lg_class) {
774 LIST_FOREACH(gp, &mp->lg_geom, lg_geom) {
775 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
777 (void) snprintf(path, sizeof (path), "%s%s",
778 _PATH_DEV, pp->lg_name);
780 if ((fd = open64(path, O_RDONLY)) < 0)
783 if ((zpool_read_label(fd, &config)) != 0) {
784 (void) no_memory(hdl);
793 if (add_config(hdl, &pools, path, config) != 0)
799 geom_deletetree(&mesh);
801 ret = get_configs(hdl, &pools);
804 for (pe = pools.pools; pe != NULL; pe = penext) {
805 penext = pe->pe_next;
806 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
807 venext = ve->ve_next;
808 for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
809 cenext = ce->ce_next;
811 nvlist_free(ce->ce_config);
819 for (ne = pools.names; ne != NULL; ne = nenext) {
820 nenext = ne->ne_next;
830 find_guid(nvlist_t *nv, uint64_t guid)
836 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
840 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
841 &child, &children) == 0) {
842 for (c = 0; c < children; c++)
843 if (find_guid(child[c], guid))
850 typedef struct spare_cbdata {
852 zpool_handle_t *cb_zhp;
856 find_spare(zpool_handle_t *zhp, void *data)
858 spare_cbdata_t *cbp = data;
864 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
867 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
868 &spares, &nspares) == 0) {
869 for (i = 0; i < nspares; i++) {
870 verify(nvlist_lookup_uint64(spares[i],
871 ZPOOL_CONFIG_GUID, &guid) == 0);
872 if (guid == cbp->cb_guid) {
884 * Determines if the pool is in use. If so, it returns true and the state of
885 * the pool as well as the name of the pool. Both strings are allocated and
886 * must be freed by the caller.
889 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
895 uint64_t guid, vdev_guid;
897 nvlist_t *pool_config;
898 uint64_t stateval, isspare;
899 spare_cbdata_t cb = { 0 };
904 if (zpool_read_label(fd, &config) != 0) {
905 (void) no_memory(hdl);
912 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
914 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
917 if (stateval != POOL_STATE_SPARE) {
918 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
920 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
925 case POOL_STATE_EXPORTED:
929 case POOL_STATE_ACTIVE:
931 * For an active pool, we have to determine if it's really part
932 * of a currently active pool (in which case the pool will exist
933 * and the guid will be the same), or whether it's part of an
934 * active pool that was disconnected without being explicitly
937 if (pool_active(hdl, name, guid, &isactive) != 0) {
944 * Because the device may have been removed while
945 * offlined, we only report it as active if the vdev is
946 * still present in the config. Otherwise, pretend like
949 if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
950 (pool_config = zpool_get_config(zhp, NULL))
954 verify(nvlist_lookup_nvlist(pool_config,
955 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
956 ret = find_guid(nvroot, vdev_guid);
962 * If this is an active spare within another pool, we
963 * treat it like an unused hot spare. This allows the
964 * user to create a pool with a hot spare that currently
965 * in use within another pool. Since we return B_TRUE,
966 * libdiskmgt will continue to prevent generic consumers
967 * from using the device.
969 if (ret && nvlist_lookup_uint64(config,
970 ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
971 stateval = POOL_STATE_SPARE;
976 stateval = POOL_STATE_POTENTIALLY_ACTIVE;
981 case POOL_STATE_SPARE:
983 * For a hot spare, it can be either definitively in use, or
984 * potentially active. To determine if it's in use, we iterate
985 * over all pools in the system and search for one with a spare
986 * with a matching guid.
988 * Due to the shared nature of spares, we don't actually report
989 * the potentially active case as in use. This means the user
990 * can freely create pools on the hot spares of exported pools,
991 * but to do otherwise makes the resulting code complicated, and
992 * we end up having to deal with this case anyway.
995 cb.cb_guid = vdev_guid;
996 if (zpool_iter(hdl, find_spare, &cb) == 1) {
997 name = (char *)zpool_get_name(cb.cb_zhp);
1010 if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
1011 nvlist_free(config);
1014 *state = (pool_state_t)stateval;
1018 zpool_close(cb.cb_zhp);
1020 nvlist_free(config);