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 2015 Nexenta Systems, Inc. All rights reserved.
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright 2015 RackTop Systems.
26 * Copyright (c) 2016, Intel Corporation.
30 * Pool import support functions.
32 * Used by zpool, ztest, zdb, and zhack to locate importable configs. Since
33 * these commands are expected to run in the global zone, we can assume
34 * that the devices are all readable when called.
36 * To import a pool, we rely on reading the configuration information from the
37 * ZFS label of each device. If we successfully read the label, then we
38 * organize the configuration information in the following hierarchy:
40 * pool guid -> toplevel vdev guid -> label txg
42 * Duplicate entries matching this same tuple will be discarded. Once we have
43 * examined every device, we pick the best label txg config for each toplevel
44 * vdev. We then arrange these toplevel vdevs into a complete pool config, and
45 * update any paths that have changed. Finally, we attempt to import the pool
46 * using our derived config, and record the results.
60 #include <sys/dktp/fdisk.h>
61 #include <sys/vdev_impl.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/vdev_impl.h>
65 #include <thread_pool.h>
67 #include <libnvpair.h>
69 #include "zutil_import.h"
73 zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...)
79 (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap);
80 hdl->lpc_desc_active = B_TRUE;
86 zutil_verror(libpc_handle_t *hdl, const char *error, const char *fmt,
91 (void) vsnprintf(action, sizeof (action), fmt, ap);
93 if (hdl->lpc_desc_active)
94 hdl->lpc_desc_active = B_FALSE;
96 hdl->lpc_desc[0] = '\0';
98 if (hdl->lpc_printerr) {
99 if (hdl->lpc_desc[0] != '\0')
100 error = hdl->lpc_desc;
102 (void) fprintf(stderr, "%s: %s\n", action, error);
108 zutil_error_fmt(libpc_handle_t *hdl, const char *error, const char *fmt, ...)
114 zutil_verror(hdl, error, fmt, ap);
122 zutil_error(libpc_handle_t *hdl, const char *error, const char *msg)
124 return (zutil_error_fmt(hdl, error, "%s", msg));
128 zutil_no_memory(libpc_handle_t *hdl)
130 zutil_error(hdl, EZFS_NOMEM, "internal error");
135 zutil_alloc(libpc_handle_t *hdl, size_t size)
139 if ((data = calloc(1, size)) == NULL)
140 (void) zutil_no_memory(hdl);
146 zutil_strdup(libpc_handle_t *hdl, const char *str)
150 if ((ret = strdup(str)) == NULL)
151 (void) zutil_no_memory(hdl);
157 * Intermediate structures used to gather configuration information.
159 typedef struct config_entry {
162 struct config_entry *ce_next;
165 typedef struct vdev_entry {
167 config_entry_t *ve_configs;
168 struct vdev_entry *ve_next;
171 typedef struct pool_entry {
173 vdev_entry_t *pe_vdevs;
174 struct pool_entry *pe_next;
177 typedef struct name_entry {
181 uint64_t ne_num_labels;
182 struct name_entry *ne_next;
185 typedef struct pool_list {
191 * Go through and fix up any path and/or devid information for the given vdev
195 fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names)
200 name_entry_t *ne, *best;
203 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
204 &child, &children) == 0) {
205 for (c = 0; c < children; c++)
206 if (fix_paths(hdl, child[c], names) != 0)
212 * This is a leaf (file or disk) vdev. In either case, go through
213 * the name list and see if we find a matching guid. If so, replace
214 * the path and see if we can calculate a new devid.
216 * There may be multiple names associated with a particular guid, in
217 * which case we have overlapping partitions or multiple paths to the
218 * same disk. In this case we prefer to use the path name which
219 * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we
220 * use the lowest order device which corresponds to the first match
221 * while traversing the ZPOOL_IMPORT_PATH search path.
223 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
224 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
228 for (ne = names; ne != NULL; ne = ne->ne_next) {
229 if (ne->ne_guid == guid) {
235 if ((strlen(path) == strlen(ne->ne_name)) &&
236 strncmp(path, ne->ne_name, strlen(path)) == 0) {
246 /* Prefer paths with move vdev labels. */
247 if (ne->ne_num_labels > best->ne_num_labels) {
252 /* Prefer paths earlier in the search order. */
253 if (ne->ne_num_labels == best->ne_num_labels &&
254 ne->ne_order < best->ne_order) {
264 if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
267 update_vdev_config_dev_strs(nv);
273 * Add the given configuration to the list of known devices.
276 add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path,
277 int order, int num_labels, nvlist_t *config)
279 uint64_t pool_guid, vdev_guid, top_guid, txg, state;
286 * If this is a hot spare not currently in use or level 2 cache
287 * device, add it to the list of names to translate, but don't do
290 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
292 (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
293 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
294 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
297 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
301 ne->ne_guid = vdev_guid;
302 ne->ne_order = order;
303 ne->ne_num_labels = num_labels;
304 ne->ne_next = pl->names;
311 * If we have a valid config but cannot read any of these fields, then
312 * it means we have a half-initialized label. In vdev_label_init()
313 * we write a label with txg == 0 so that we can identify the device
314 * in case the user refers to the same disk later on. If we fail to
315 * create the pool, we'll be left with a label in this state
316 * which should not be considered part of a valid pool.
318 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
320 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
322 nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
324 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
325 &txg) != 0 || txg == 0) {
330 * First, see if we know about this pool. If not, then add it to the
331 * list of known pools.
333 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
334 if (pe->pe_guid == pool_guid)
339 if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
342 pe->pe_guid = pool_guid;
343 pe->pe_next = pl->pools;
348 * Second, see if we know about this toplevel vdev. Add it if its
351 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
352 if (ve->ve_guid == top_guid)
357 if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
360 ve->ve_guid = top_guid;
361 ve->ve_next = pe->pe_vdevs;
366 * Third, see if we have a config with a matching transaction group. If
367 * so, then we do nothing. Otherwise, add it to the list of known
370 for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
371 if (ce->ce_txg == txg)
376 if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) {
380 ce->ce_config = fnvlist_dup(config);
381 ce->ce_next = ve->ve_configs;
386 * At this point we've successfully added our config to the list of
387 * known configs. The last thing to do is add the vdev guid -> path
388 * mappings so that we can fix up the configuration as necessary before
391 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
394 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
399 ne->ne_guid = vdev_guid;
400 ne->ne_order = order;
401 ne->ne_num_labels = num_labels;
402 ne->ne_next = pl->names;
409 zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid,
412 ASSERT(hdl->lpc_ops->pco_pool_active != NULL);
414 int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name,
421 zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig)
423 ASSERT(hdl->lpc_ops->pco_refresh_config != NULL);
425 return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle,
430 * Determine if the vdev id is a hole in the namespace.
433 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
437 for (c = 0; c < holes; c++) {
439 /* Top-level is a hole */
440 if (hole_array[c] == id)
447 * Convert our list of pools into the definitive set of configurations. We
448 * start by picking the best config for each toplevel vdev. Once that's done,
449 * we assemble the toplevel vdevs into a full config for the pool. We make a
450 * pass to fix up any incorrect paths, and then add it to the main list to
451 * return to the user.
454 get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok,
460 nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
461 nvlist_t **spares, **l2cache;
462 uint_t i, nspares, nl2cache;
463 boolean_t config_seen;
465 char *name, *hostname = NULL;
468 nvlist_t **child = NULL;
470 uint64_t *hole_array, max_id;
475 boolean_t valid_top_config = B_FALSE;
477 if (nvlist_alloc(&ret, 0, 0) != 0)
480 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
481 uint64_t id, max_txg = 0;
483 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
485 config_seen = B_FALSE;
488 * Iterate over all toplevel vdevs. Grab the pool configuration
489 * from the first one we find, and then go through the rest and
490 * add them as necessary to the 'vdevs' member of the config.
492 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
495 * Determine the best configuration for this vdev by
496 * selecting the config with the latest transaction
500 for (ce = ve->ve_configs; ce != NULL;
503 if (ce->ce_txg > best_txg) {
505 best_txg = ce->ce_txg;
510 * We rely on the fact that the max txg for the
511 * pool will contain the most up-to-date information
512 * about the valid top-levels in the vdev namespace.
514 if (best_txg > max_txg) {
515 (void) nvlist_remove(config,
516 ZPOOL_CONFIG_VDEV_CHILDREN,
518 (void) nvlist_remove(config,
519 ZPOOL_CONFIG_HOLE_ARRAY,
520 DATA_TYPE_UINT64_ARRAY);
526 valid_top_config = B_FALSE;
528 if (nvlist_lookup_uint64(tmp,
529 ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
530 verify(nvlist_add_uint64(config,
531 ZPOOL_CONFIG_VDEV_CHILDREN,
533 valid_top_config = B_TRUE;
536 if (nvlist_lookup_uint64_array(tmp,
537 ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
539 verify(nvlist_add_uint64_array(config,
540 ZPOOL_CONFIG_HOLE_ARRAY,
541 hole_array, holes) == 0);
547 * Copy the relevant pieces of data to the pool
553 * comment (if available)
555 * hostid (if available)
556 * hostname (if available)
558 uint64_t state, version;
559 char *comment = NULL;
561 version = fnvlist_lookup_uint64(tmp,
562 ZPOOL_CONFIG_VERSION);
563 fnvlist_add_uint64(config,
564 ZPOOL_CONFIG_VERSION, version);
565 guid = fnvlist_lookup_uint64(tmp,
566 ZPOOL_CONFIG_POOL_GUID);
567 fnvlist_add_uint64(config,
568 ZPOOL_CONFIG_POOL_GUID, guid);
569 name = fnvlist_lookup_string(tmp,
570 ZPOOL_CONFIG_POOL_NAME);
571 fnvlist_add_string(config,
572 ZPOOL_CONFIG_POOL_NAME, name);
574 if (nvlist_lookup_string(tmp,
575 ZPOOL_CONFIG_COMMENT, &comment) == 0)
576 fnvlist_add_string(config,
577 ZPOOL_CONFIG_COMMENT, comment);
579 state = fnvlist_lookup_uint64(tmp,
580 ZPOOL_CONFIG_POOL_STATE);
581 fnvlist_add_uint64(config,
582 ZPOOL_CONFIG_POOL_STATE, state);
585 if (nvlist_lookup_uint64(tmp,
586 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
587 fnvlist_add_uint64(config,
588 ZPOOL_CONFIG_HOSTID, hostid);
589 hostname = fnvlist_lookup_string(tmp,
590 ZPOOL_CONFIG_HOSTNAME);
591 fnvlist_add_string(config,
592 ZPOOL_CONFIG_HOSTNAME, hostname);
595 config_seen = B_TRUE;
599 * Add this top-level vdev to the child array.
601 verify(nvlist_lookup_nvlist(tmp,
602 ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
603 verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
606 if (id >= children) {
609 newchild = zutil_alloc(hdl, (id + 1) *
610 sizeof (nvlist_t *));
611 if (newchild == NULL)
614 for (c = 0; c < children; c++)
615 newchild[c] = child[c];
621 if (nvlist_dup(nvtop, &child[id], 0) != 0)
627 * If we have information about all the top-levels then
628 * clean up the nvlist which we've constructed. This
629 * means removing any extraneous devices that are
630 * beyond the valid range or adding devices to the end
631 * of our array which appear to be missing.
633 if (valid_top_config) {
634 if (max_id < children) {
635 for (c = max_id; c < children; c++)
636 nvlist_free(child[c]);
638 } else if (max_id > children) {
641 newchild = zutil_alloc(hdl, (max_id) *
642 sizeof (nvlist_t *));
643 if (newchild == NULL)
646 for (c = 0; c < children; c++)
647 newchild[c] = child[c];
655 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
659 * The vdev namespace may contain holes as a result of
660 * device removal. We must add them back into the vdev
661 * tree before we process any missing devices.
664 ASSERT(valid_top_config);
666 for (c = 0; c < children; c++) {
669 if (child[c] != NULL ||
670 !vdev_is_hole(hole_array, holes, c))
673 if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
678 * Holes in the namespace are treated as
679 * "hole" top-level vdevs and have a
680 * special flag set on them.
682 if (nvlist_add_string(holey,
684 VDEV_TYPE_HOLE) != 0 ||
685 nvlist_add_uint64(holey,
686 ZPOOL_CONFIG_ID, c) != 0 ||
687 nvlist_add_uint64(holey,
688 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
697 * Look for any missing top-level vdevs. If this is the case,
698 * create a faked up 'missing' vdev as a placeholder. We cannot
699 * simply compress the child array, because the kernel performs
700 * certain checks to make sure the vdev IDs match their location
701 * in the configuration.
703 for (c = 0; c < children; c++) {
704 if (child[c] == NULL) {
706 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
709 if (nvlist_add_string(missing,
711 VDEV_TYPE_MISSING) != 0 ||
712 nvlist_add_uint64(missing,
713 ZPOOL_CONFIG_ID, c) != 0 ||
714 nvlist_add_uint64(missing,
715 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
716 nvlist_free(missing);
724 * Put all of this pool's top-level vdevs into a root vdev.
726 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
728 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
729 VDEV_TYPE_ROOT) != 0 ||
730 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
731 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
732 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
733 child, children) != 0) {
738 for (c = 0; c < children; c++)
739 nvlist_free(child[c]);
745 * Go through and fix up any paths and/or devids based on our
746 * known list of vdev GUID -> path mappings.
748 if (fix_paths(hdl, nvroot, pl->names) != 0) {
754 * Add the root vdev to this pool's configuration.
756 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
764 * zdb uses this path to report on active pools that were
765 * imported or created using -R.
771 * Determine if this pool is currently active, in which case we
772 * can't actually import it.
774 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
776 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
779 if (zutil_pool_active(hdl, name, guid, &isactive) != 0)
788 if (policy != NULL) {
789 if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
794 if ((nvl = zutil_refresh_config(hdl, config)) == NULL) {
804 * Go through and update the paths for spares, now that we have
807 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
809 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
810 &spares, &nspares) == 0) {
811 for (i = 0; i < nspares; i++) {
812 if (fix_paths(hdl, spares[i], pl->names) != 0)
818 * Update the paths for l2cache devices.
820 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
821 &l2cache, &nl2cache) == 0) {
822 for (i = 0; i < nl2cache; i++) {
823 if (fix_paths(hdl, l2cache[i], pl->names) != 0)
829 * Restore the original information read from the actual label.
831 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
833 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
836 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
838 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
844 * Add this pool to the list of configs.
846 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
849 if (nvlist_add_nvlist(ret, name, config) != 0)
859 (void) zutil_no_memory(hdl);
863 for (c = 0; c < children; c++)
864 nvlist_free(child[c]);
871 * Return the offset of the given label.
874 label_offset(uint64_t size, int l)
876 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
877 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
878 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
882 * Given a file descriptor, read the label information and return an nvlist
883 * describing the configuration, if there is one. The number of valid
884 * labels found will be returned in num_labels when non-NULL.
887 zpool_read_label(int fd, nvlist_t **config, int *num_labels)
889 struct stat64 statbuf;
892 nvlist_t *expected_config = NULL;
893 uint64_t expected_guid = 0, size;
898 if (fstat64_blk(fd, &statbuf) == -1)
900 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
902 error = posix_memalign((void **)&label, PAGESIZE, sizeof (*label));
906 for (l = 0; l < VDEV_LABELS; l++) {
907 uint64_t state, guid, txg;
909 if (pread64(fd, label, sizeof (vdev_label_t),
910 label_offset(size, l)) != sizeof (vdev_label_t))
913 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
914 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
917 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
918 &guid) != 0 || guid == 0) {
919 nvlist_free(*config);
923 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
924 &state) != 0 || state > POOL_STATE_L2CACHE) {
925 nvlist_free(*config);
929 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
930 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
931 &txg) != 0 || txg == 0)) {
932 nvlist_free(*config);
937 if (expected_guid == guid)
940 nvlist_free(*config);
942 expected_config = *config;
943 expected_guid = guid;
948 if (num_labels != NULL)
952 *config = expected_config;
958 * Sorted by full path and then vdev guid to allow for multiple entries with
959 * the same full path name. This is required because it's possible to
960 * have multiple block devices with labels that refer to the same
961 * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both
962 * entries need to be added to the cache. Scenarios where this can occur
963 * include overwritten pool labels, devices which are visible from multiple
964 * hosts and multipath devices.
967 slice_cache_compare(const void *arg1, const void *arg2)
969 const char *nm1 = ((rdsk_node_t *)arg1)->rn_name;
970 const char *nm2 = ((rdsk_node_t *)arg2)->rn_name;
971 uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
972 uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
975 rv = TREE_ISIGN(strcmp(nm1, nm2));
979 return (TREE_CMP(guid1, guid2));
983 label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
984 uint64_t vdev_guid, char **path, char **devid)
992 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
993 &child, &children) == 0) {
994 for (c = 0; c < children; c++) {
995 error = label_paths_impl(hdl, child[c],
996 pool_guid, vdev_guid, path, devid);
1006 error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1007 if ((error != 0) || (guid != vdev_guid))
1010 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1014 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1022 * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1023 * and store these strings as config_path and devid_path respectively.
1024 * The returned pointers are only valid as long as label remains valid.
1027 label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1036 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1037 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1038 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1041 return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1046 zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock,
1047 avl_tree_t *cache, const char *path, const char *name, int order)
1052 slice = zutil_alloc(hdl, sizeof (rdsk_node_t));
1053 if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) {
1057 slice->rn_vdev_guid = 0;
1058 slice->rn_lock = lock;
1059 slice->rn_avl = cache;
1060 slice->rn_hdl = hdl;
1061 slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET;
1062 slice->rn_labelpaths = B_FALSE;
1064 pthread_mutex_lock(lock);
1065 if (avl_find(cache, slice, &where)) {
1066 free(slice->rn_name);
1069 avl_insert(cache, slice, where);
1071 pthread_mutex_unlock(lock);
1075 zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock,
1076 avl_tree_t *cache, const char *dir, int order)
1079 char path[MAXPATHLEN];
1080 struct dirent64 *dp;
1083 if (realpath(dir, path) == NULL) {
1085 if (error == ENOENT)
1088 zutil_error_aux(hdl, strerror(error));
1089 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1090 TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1094 dirp = opendir(path);
1097 zutil_error_aux(hdl, strerror(error));
1098 (void) zutil_error_fmt(hdl, EZFS_BADPATH,
1099 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
1103 while ((dp = readdir64(dirp)) != NULL) {
1104 const char *name = dp->d_name;
1105 if (name[0] == '.' &&
1106 (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1109 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name,
1113 (void) closedir(dirp);
1118 zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
1119 avl_tree_t *cache, const char *dir, int order)
1122 char path[MAXPATHLEN];
1127 * Separate the directory part and last part of the
1128 * path. We do this so that we can get the realpath of
1129 * the directory. We don't get the realpath on the
1130 * whole path because if it's a symlink, we want the
1131 * path of the symlink not where it points to.
1133 d = zutil_strdup(hdl, dir);
1134 b = zutil_strdup(hdl, dir);
1138 if (realpath(dpath, path) == NULL) {
1140 if (error == ENOENT) {
1145 zutil_error_aux(hdl, strerror(error));
1146 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1147 TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1151 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
1160 * Scan a list of directories for zfs devices.
1163 zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock,
1164 avl_tree_t **slice_cache, const char * const *dir, size_t dirs)
1171 *slice_cache = NULL;
1172 cache = zutil_alloc(hdl, sizeof (avl_tree_t));
1173 avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1174 offsetof(rdsk_node_t, rn_node));
1176 for (i = 0; i < dirs; i++) {
1179 if (stat(dir[i], &sbuf) != 0) {
1181 if (error == ENOENT)
1184 zutil_error_aux(hdl, strerror(error));
1185 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1186 TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1191 * If dir[i] is a directory, we walk through it and add all
1192 * the entries to the cache. If it's not a directory, we just
1193 * add it to the cache.
1195 if (S_ISDIR(sbuf.st_mode)) {
1196 if ((error = zpool_find_import_scan_dir(hdl, lock,
1197 cache, dir[i], i)) != 0)
1200 if ((error = zpool_find_import_scan_path(hdl, lock,
1201 cache, dir[i], i)) != 0)
1206 *slice_cache = cache;
1211 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1212 free(slice->rn_name);
1221 * Given a list of directories to search, find all pools stored on disk. This
1222 * includes partial pools which are not available to import. If no args are
1223 * given (argc is 0), then the default directory (/dev/dsk) is searched.
1224 * poolname or guid (but not both) are provided by the caller when trying
1225 * to import a specific pool.
1228 zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg)
1230 nvlist_t *ret = NULL;
1231 pool_list_t pools = { 0 };
1232 pool_entry_t *pe, *penext;
1233 vdev_entry_t *ve, *venext;
1234 config_entry_t *ce, *cenext;
1235 name_entry_t *ne, *nenext;
1236 pthread_mutex_t lock;
1242 verify(iarg->poolname == NULL || iarg->guid == 0);
1243 pthread_mutex_init(&lock, NULL);
1246 * Locate pool member vdevs by blkid or by directory scanning.
1247 * On success a newly allocated AVL tree which is populated with an
1248 * entry for each discovered vdev will be returned in the cache.
1249 * It's the caller's responsibility to consume and destroy this tree.
1251 if (iarg->scan || iarg->paths != 0) {
1252 size_t dirs = iarg->paths;
1253 const char * const *dir = (const char * const *)iarg->path;
1256 dir = zpool_default_search_paths(&dirs);
1258 if (zpool_find_import_scan(hdl, &lock, &cache, dir, dirs) != 0)
1261 if (zpool_find_import_blkid(hdl, &lock, &cache) != 0)
1266 * Create a thread pool to parallelize the process of reading and
1267 * validating labels, a large number of threads can be used due to
1268 * minimal contention.
1270 t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
1271 for (slice = avl_first(cache); slice;
1272 (slice = avl_walk(cache, slice, AVL_AFTER)))
1273 (void) tpool_dispatch(t, zpool_open_func, slice);
1279 * Process the cache, filtering out any entries which are not
1280 * for the specified pool then adding matching label configs.
1283 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1284 if (slice->rn_config != NULL) {
1285 nvlist_t *config = slice->rn_config;
1286 boolean_t matched = B_TRUE;
1287 boolean_t aux = B_FALSE;
1291 * Check if it's a spare or l2cache device. If it is,
1292 * we need to skip the name and guid check since they
1293 * don't exist on aux device label.
1295 if (iarg->poolname != NULL || iarg->guid != 0) {
1297 aux = nvlist_lookup_uint64(config,
1298 ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1299 (state == POOL_STATE_SPARE ||
1300 state == POOL_STATE_L2CACHE);
1303 if (iarg->poolname != NULL && !aux) {
1306 matched = nvlist_lookup_string(config,
1307 ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1308 strcmp(iarg->poolname, pname) == 0;
1309 } else if (iarg->guid != 0 && !aux) {
1312 matched = nvlist_lookup_uint64(config,
1313 ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1314 iarg->guid == this_guid;
1318 * Verify all remaining entries can be opened
1319 * exclusively. This will prune all underlying
1320 * multipath devices which otherwise could
1321 * result in the vdev appearing as UNAVAIL.
1323 * Under zdb, this step isn't required and
1324 * would prevent a zdb -e of active pools with
1327 fd = open(slice->rn_name, O_RDONLY | O_EXCL);
1328 if (fd >= 0 || iarg->can_be_active) {
1331 add_config(hdl, &pools,
1332 slice->rn_name, slice->rn_order,
1333 slice->rn_num_labels, config);
1336 nvlist_free(config);
1338 free(slice->rn_name);
1343 pthread_mutex_destroy(&lock);
1345 ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy);
1347 for (pe = pools.pools; pe != NULL; pe = penext) {
1348 penext = pe->pe_next;
1349 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1350 venext = ve->ve_next;
1351 for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1352 cenext = ce->ce_next;
1353 nvlist_free(ce->ce_config);
1361 for (ne = pools.names; ne != NULL; ne = nenext) {
1362 nenext = ne->ne_next;
1371 * Given a cache file, return the contents as a list of importable pools.
1372 * poolname or guid (but not both) are provided by the caller when trying
1373 * to import a specific pool.
1376 zpool_find_import_cached(libpc_handle_t *hdl, const char *cachefile,
1377 const char *poolname, uint64_t guid)
1381 struct stat64 statbuf;
1382 nvlist_t *raw, *src, *dst;
1389 verify(poolname == NULL || guid == 0);
1391 if ((fd = open(cachefile, O_RDONLY)) < 0) {
1392 zutil_error_aux(hdl, "%s", strerror(errno));
1393 (void) zutil_error(hdl, EZFS_BADCACHE,
1394 dgettext(TEXT_DOMAIN, "failed to open cache file"));
1398 if (fstat64(fd, &statbuf) != 0) {
1399 zutil_error_aux(hdl, "%s", strerror(errno));
1401 (void) zutil_error(hdl, EZFS_BADCACHE,
1402 dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
1406 if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) {
1411 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1414 (void) zutil_error(hdl, EZFS_BADCACHE,
1415 dgettext(TEXT_DOMAIN,
1416 "failed to read cache file contents"));
1422 if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1424 (void) zutil_error(hdl, EZFS_BADCACHE,
1425 dgettext(TEXT_DOMAIN,
1426 "invalid or corrupt cache file contents"));
1433 * Go through and get the current state of the pools and refresh their
1436 if (nvlist_alloc(&pools, 0, 0) != 0) {
1437 (void) zutil_no_memory(hdl);
1443 while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1444 src = fnvpair_value_nvlist(elem);
1446 name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
1447 if (poolname != NULL && strcmp(poolname, name) != 0)
1450 this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1451 if (guid != 0 && guid != this_guid)
1454 if (zutil_pool_active(hdl, name, this_guid, &active) != 0) {
1463 if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE,
1465 (void) zutil_no_memory(hdl);
1471 if ((dst = zutil_refresh_config(hdl, src)) == NULL) {
1477 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1478 (void) zutil_no_memory(hdl);
1492 zpool_search_import(void *hdl, importargs_t *import,
1493 const pool_config_ops_t *pco)
1495 libpc_handle_t handle = { 0 };
1496 nvlist_t *pools = NULL;
1498 handle.lpc_lib_handle = hdl;
1499 handle.lpc_ops = pco;
1500 handle.lpc_printerr = B_TRUE;
1502 verify(import->poolname == NULL || import->guid == 0);
1504 if (import->cachefile != NULL)
1505 pools = zpool_find_import_cached(&handle, import->cachefile,
1506 import->poolname, import->guid);
1508 pools = zpool_find_import_impl(&handle, import);
1510 if ((pools == NULL || nvlist_empty(pools)) &&
1511 handle.lpc_open_access_error && geteuid() != 0) {
1512 (void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN,
1520 pool_match(nvlist_t *cfg, char *tgt)
1522 uint64_t v, guid = strtoull(tgt, NULL, 0);
1526 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
1529 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
1530 return (strcmp(s, tgt) == 0);
1536 zpool_find_config(void *hdl, const char *target, nvlist_t **configp,
1537 importargs_t *args, const pool_config_ops_t *pco)
1540 nvlist_t *match = NULL;
1541 nvlist_t *config = NULL;
1542 char *name = NULL, *sepp = NULL;
1545 char *targetdup = strdup(target);
1549 if ((sepp = strpbrk(targetdup, "/@")) != NULL) {
1554 pools = zpool_search_import(hdl, args, pco);
1556 if (pools != NULL) {
1557 nvpair_t *elem = NULL;
1558 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1559 VERIFY0(nvpair_value_nvlist(elem, &config));
1560 if (pool_match(config, targetdup)) {
1562 if (match != NULL) {
1563 /* multiple matches found */
1567 name = nvpair_name(elem);