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.
27 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
31 * Pool import support functions.
33 * Used by zpool, ztest, zdb, and zhack to locate importable configs. Since
34 * these commands are expected to run in the global zone, we can assume
35 * that the devices are all readable when called.
37 * To import a pool, we rely on reading the configuration information from the
38 * ZFS label of each device. If we successfully read the label, then we
39 * organize the configuration information in the following hierarchy:
41 * pool guid -> toplevel vdev guid -> label txg
43 * Duplicate entries matching this same tuple will be discarded. Once we have
44 * examined every device, we pick the best label txg config for each toplevel
45 * vdev. We then arrange these toplevel vdevs into a complete pool config, and
46 * update any paths that have changed. Finally, we attempt to import the pool
47 * using our derived config, and record the results.
62 #include <sys/dktp/fdisk.h>
63 #include <sys/vdev_impl.h>
64 #include <sys/fs/zfs.h>
65 #include <sys/vdev_impl.h>
67 #include <thread_pool.h>
69 #include <libnvpair.h>
71 #include "zutil_import.h"
75 zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...)
81 (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap);
82 hdl->lpc_desc_active = B_TRUE;
88 zutil_verror(libpc_handle_t *hdl, const char *error, const char *fmt,
93 (void) vsnprintf(action, sizeof (action), fmt, ap);
95 if (hdl->lpc_desc_active)
96 hdl->lpc_desc_active = B_FALSE;
98 hdl->lpc_desc[0] = '\0';
100 if (hdl->lpc_printerr) {
101 if (hdl->lpc_desc[0] != '\0')
102 error = hdl->lpc_desc;
104 (void) fprintf(stderr, "%s: %s\n", action, error);
110 zutil_error_fmt(libpc_handle_t *hdl, const char *error, const char *fmt, ...)
116 zutil_verror(hdl, error, fmt, ap);
124 zutil_error(libpc_handle_t *hdl, const char *error, const char *msg)
126 return (zutil_error_fmt(hdl, error, "%s", msg));
130 zutil_no_memory(libpc_handle_t *hdl)
132 zutil_error(hdl, EZFS_NOMEM, "internal error");
137 zutil_alloc(libpc_handle_t *hdl, size_t size)
141 if ((data = calloc(1, size)) == NULL)
142 (void) zutil_no_memory(hdl);
148 zutil_strdup(libpc_handle_t *hdl, const char *str)
152 if ((ret = strdup(str)) == NULL)
153 (void) zutil_no_memory(hdl);
159 * Intermediate structures used to gather configuration information.
161 typedef struct config_entry {
164 struct config_entry *ce_next;
167 typedef struct vdev_entry {
169 config_entry_t *ve_configs;
170 struct vdev_entry *ve_next;
173 typedef struct pool_entry {
175 vdev_entry_t *pe_vdevs;
176 struct pool_entry *pe_next;
179 typedef struct name_entry {
183 uint64_t ne_num_labels;
184 struct name_entry *ne_next;
187 typedef struct pool_list {
193 * Go through and fix up any path and/or devid information for the given vdev
197 fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names)
202 name_entry_t *ne, *best;
205 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
206 &child, &children) == 0) {
207 for (c = 0; c < children; c++)
208 if (fix_paths(hdl, child[c], names) != 0)
214 * This is a leaf (file or disk) vdev. In either case, go through
215 * the name list and see if we find a matching guid. If so, replace
216 * the path and see if we can calculate a new devid.
218 * There may be multiple names associated with a particular guid, in
219 * which case we have overlapping partitions or multiple paths to the
220 * same disk. In this case we prefer to use the path name which
221 * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we
222 * use the lowest order device which corresponds to the first match
223 * while traversing the ZPOOL_IMPORT_PATH search path.
225 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
226 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
230 for (ne = names; ne != NULL; ne = ne->ne_next) {
231 if (ne->ne_guid == guid) {
237 if ((strlen(path) == strlen(ne->ne_name)) &&
238 strncmp(path, ne->ne_name, strlen(path)) == 0) {
248 /* Prefer paths with move vdev labels. */
249 if (ne->ne_num_labels > best->ne_num_labels) {
254 /* Prefer paths earlier in the search order. */
255 if (ne->ne_num_labels == best->ne_num_labels &&
256 ne->ne_order < best->ne_order) {
266 if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
269 update_vdev_config_dev_strs(nv);
275 * Add the given configuration to the list of known devices.
278 add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path,
279 int order, int num_labels, nvlist_t *config)
281 uint64_t pool_guid, vdev_guid, top_guid, txg, state;
288 * If this is a hot spare not currently in use or level 2 cache
289 * device, add it to the list of names to translate, but don't do
292 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
294 (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
295 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
296 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
299 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
303 ne->ne_guid = vdev_guid;
304 ne->ne_order = order;
305 ne->ne_num_labels = num_labels;
306 ne->ne_next = pl->names;
313 * If we have a valid config but cannot read any of these fields, then
314 * it means we have a half-initialized label. In vdev_label_init()
315 * we write a label with txg == 0 so that we can identify the device
316 * in case the user refers to the same disk later on. If we fail to
317 * create the pool, we'll be left with a label in this state
318 * which should not be considered part of a valid pool.
320 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
322 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
324 nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
326 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
327 &txg) != 0 || txg == 0) {
332 * First, see if we know about this pool. If not, then add it to the
333 * list of known pools.
335 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
336 if (pe->pe_guid == pool_guid)
341 if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
344 pe->pe_guid = pool_guid;
345 pe->pe_next = pl->pools;
350 * Second, see if we know about this toplevel vdev. Add it if its
353 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
354 if (ve->ve_guid == top_guid)
359 if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
362 ve->ve_guid = top_guid;
363 ve->ve_next = pe->pe_vdevs;
368 * Third, see if we have a config with a matching transaction group. If
369 * so, then we do nothing. Otherwise, add it to the list of known
372 for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
373 if (ce->ce_txg == txg)
378 if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) {
382 ce->ce_config = fnvlist_dup(config);
383 ce->ce_next = ve->ve_configs;
388 * At this point we've successfully added our config to the list of
389 * known configs. The last thing to do is add the vdev guid -> path
390 * mappings so that we can fix up the configuration as necessary before
393 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
396 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
401 ne->ne_guid = vdev_guid;
402 ne->ne_order = order;
403 ne->ne_num_labels = num_labels;
404 ne->ne_next = pl->names;
411 zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid,
414 ASSERT(hdl->lpc_ops->pco_pool_active != NULL);
416 int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name,
423 zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig)
425 ASSERT(hdl->lpc_ops->pco_refresh_config != NULL);
427 return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle,
432 * Determine if the vdev id is a hole in the namespace.
435 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
439 for (c = 0; c < holes; c++) {
441 /* Top-level is a hole */
442 if (hole_array[c] == id)
449 * Convert our list of pools into the definitive set of configurations. We
450 * start by picking the best config for each toplevel vdev. Once that's done,
451 * we assemble the toplevel vdevs into a full config for the pool. We make a
452 * pass to fix up any incorrect paths, and then add it to the main list to
453 * return to the user.
456 get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok,
462 nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
463 nvlist_t **spares, **l2cache;
464 uint_t i, nspares, nl2cache;
465 boolean_t config_seen;
467 char *name, *hostname = NULL;
470 nvlist_t **child = NULL;
472 uint64_t *hole_array, max_id;
477 boolean_t valid_top_config = B_FALSE;
479 if (nvlist_alloc(&ret, 0, 0) != 0)
482 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
483 uint64_t id, max_txg = 0;
485 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
487 config_seen = B_FALSE;
490 * Iterate over all toplevel vdevs. Grab the pool configuration
491 * from the first one we find, and then go through the rest and
492 * add them as necessary to the 'vdevs' member of the config.
494 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
497 * Determine the best configuration for this vdev by
498 * selecting the config with the latest transaction
502 for (ce = ve->ve_configs; ce != NULL;
505 if (ce->ce_txg > best_txg) {
507 best_txg = ce->ce_txg;
512 * We rely on the fact that the max txg for the
513 * pool will contain the most up-to-date information
514 * about the valid top-levels in the vdev namespace.
516 if (best_txg > max_txg) {
517 (void) nvlist_remove(config,
518 ZPOOL_CONFIG_VDEV_CHILDREN,
520 (void) nvlist_remove(config,
521 ZPOOL_CONFIG_HOLE_ARRAY,
522 DATA_TYPE_UINT64_ARRAY);
528 valid_top_config = B_FALSE;
530 if (nvlist_lookup_uint64(tmp,
531 ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
532 verify(nvlist_add_uint64(config,
533 ZPOOL_CONFIG_VDEV_CHILDREN,
535 valid_top_config = B_TRUE;
538 if (nvlist_lookup_uint64_array(tmp,
539 ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
541 verify(nvlist_add_uint64_array(config,
542 ZPOOL_CONFIG_HOLE_ARRAY,
543 hole_array, holes) == 0);
549 * Copy the relevant pieces of data to the pool
555 * comment (if available)
556 * compatibility features (if available)
558 * hostid (if available)
559 * hostname (if available)
561 uint64_t state, version;
562 char *comment = NULL;
563 char *compatibility = NULL;
565 version = fnvlist_lookup_uint64(tmp,
566 ZPOOL_CONFIG_VERSION);
567 fnvlist_add_uint64(config,
568 ZPOOL_CONFIG_VERSION, version);
569 guid = fnvlist_lookup_uint64(tmp,
570 ZPOOL_CONFIG_POOL_GUID);
571 fnvlist_add_uint64(config,
572 ZPOOL_CONFIG_POOL_GUID, guid);
573 name = fnvlist_lookup_string(tmp,
574 ZPOOL_CONFIG_POOL_NAME);
575 fnvlist_add_string(config,
576 ZPOOL_CONFIG_POOL_NAME, name);
578 if (nvlist_lookup_string(tmp,
579 ZPOOL_CONFIG_COMMENT, &comment) == 0)
580 fnvlist_add_string(config,
581 ZPOOL_CONFIG_COMMENT, comment);
583 if (nvlist_lookup_string(tmp,
584 ZPOOL_CONFIG_COMPATIBILITY,
585 &compatibility) == 0)
586 fnvlist_add_string(config,
587 ZPOOL_CONFIG_COMPATIBILITY,
590 state = fnvlist_lookup_uint64(tmp,
591 ZPOOL_CONFIG_POOL_STATE);
592 fnvlist_add_uint64(config,
593 ZPOOL_CONFIG_POOL_STATE, state);
596 if (nvlist_lookup_uint64(tmp,
597 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
598 fnvlist_add_uint64(config,
599 ZPOOL_CONFIG_HOSTID, hostid);
600 hostname = fnvlist_lookup_string(tmp,
601 ZPOOL_CONFIG_HOSTNAME);
602 fnvlist_add_string(config,
603 ZPOOL_CONFIG_HOSTNAME, hostname);
606 config_seen = B_TRUE;
610 * Add this top-level vdev to the child array.
612 verify(nvlist_lookup_nvlist(tmp,
613 ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
614 verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
617 if (id >= children) {
620 newchild = zutil_alloc(hdl, (id + 1) *
621 sizeof (nvlist_t *));
622 if (newchild == NULL)
625 for (c = 0; c < children; c++)
626 newchild[c] = child[c];
632 if (nvlist_dup(nvtop, &child[id], 0) != 0)
638 * If we have information about all the top-levels then
639 * clean up the nvlist which we've constructed. This
640 * means removing any extraneous devices that are
641 * beyond the valid range or adding devices to the end
642 * of our array which appear to be missing.
644 if (valid_top_config) {
645 if (max_id < children) {
646 for (c = max_id; c < children; c++)
647 nvlist_free(child[c]);
649 } else if (max_id > children) {
652 newchild = zutil_alloc(hdl, (max_id) *
653 sizeof (nvlist_t *));
654 if (newchild == NULL)
657 for (c = 0; c < children; c++)
658 newchild[c] = child[c];
666 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
670 * The vdev namespace may contain holes as a result of
671 * device removal. We must add them back into the vdev
672 * tree before we process any missing devices.
675 ASSERT(valid_top_config);
677 for (c = 0; c < children; c++) {
680 if (child[c] != NULL ||
681 !vdev_is_hole(hole_array, holes, c))
684 if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
689 * Holes in the namespace are treated as
690 * "hole" top-level vdevs and have a
691 * special flag set on them.
693 if (nvlist_add_string(holey,
695 VDEV_TYPE_HOLE) != 0 ||
696 nvlist_add_uint64(holey,
697 ZPOOL_CONFIG_ID, c) != 0 ||
698 nvlist_add_uint64(holey,
699 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
708 * Look for any missing top-level vdevs. If this is the case,
709 * create a faked up 'missing' vdev as a placeholder. We cannot
710 * simply compress the child array, because the kernel performs
711 * certain checks to make sure the vdev IDs match their location
712 * in the configuration.
714 for (c = 0; c < children; c++) {
715 if (child[c] == NULL) {
717 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
720 if (nvlist_add_string(missing,
722 VDEV_TYPE_MISSING) != 0 ||
723 nvlist_add_uint64(missing,
724 ZPOOL_CONFIG_ID, c) != 0 ||
725 nvlist_add_uint64(missing,
726 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
727 nvlist_free(missing);
735 * Put all of this pool's top-level vdevs into a root vdev.
737 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
739 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
740 VDEV_TYPE_ROOT) != 0 ||
741 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
742 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
743 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
744 child, children) != 0) {
749 for (c = 0; c < children; c++)
750 nvlist_free(child[c]);
756 * Go through and fix up any paths and/or devids based on our
757 * known list of vdev GUID -> path mappings.
759 if (fix_paths(hdl, nvroot, pl->names) != 0) {
765 * Add the root vdev to this pool's configuration.
767 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
775 * zdb uses this path to report on active pools that were
776 * imported or created using -R.
782 * Determine if this pool is currently active, in which case we
783 * can't actually import it.
785 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
787 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
790 if (zutil_pool_active(hdl, name, guid, &isactive) != 0)
799 if (policy != NULL) {
800 if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
805 if ((nvl = zutil_refresh_config(hdl, config)) == NULL) {
815 * Go through and update the paths for spares, now that we have
818 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
820 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
821 &spares, &nspares) == 0) {
822 for (i = 0; i < nspares; i++) {
823 if (fix_paths(hdl, spares[i], pl->names) != 0)
829 * Update the paths for l2cache devices.
831 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
832 &l2cache, &nl2cache) == 0) {
833 for (i = 0; i < nl2cache; i++) {
834 if (fix_paths(hdl, l2cache[i], pl->names) != 0)
840 * Restore the original information read from the actual label.
842 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
844 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
847 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
849 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
855 * Add this pool to the list of configs.
857 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
860 if (nvlist_add_nvlist(ret, name, config) != 0)
870 (void) zutil_no_memory(hdl);
874 for (c = 0; c < children; c++)
875 nvlist_free(child[c]);
882 * Return the offset of the given label.
885 label_offset(uint64_t size, int l)
887 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
888 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
889 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
893 * Given a file descriptor, read the label information and return an nvlist
894 * describing the configuration, if there is one. The number of valid
895 * labels found will be returned in num_labels when non-NULL.
898 zpool_read_label(int fd, nvlist_t **config, int *num_labels)
900 struct stat64 statbuf;
901 struct aiocb aiocbs[VDEV_LABELS];
902 struct aiocb *aiocbps[VDEV_LABELS];
904 nvlist_t *expected_config = NULL;
905 uint64_t expected_guid = 0, size;
906 int error, l, count = 0;
910 if (fstat64_blk(fd, &statbuf) == -1)
912 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
914 error = posix_memalign((void **)&labels, PAGESIZE,
915 VDEV_LABELS * sizeof (*labels));
919 memset(aiocbs, 0, sizeof (aiocbs));
920 for (l = 0; l < VDEV_LABELS; l++) {
921 off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE;
923 aiocbs[l].aio_fildes = fd;
924 aiocbs[l].aio_offset = offset;
925 aiocbs[l].aio_buf = &labels[l];
926 aiocbs[l].aio_nbytes = sizeof (vdev_phys_t);
927 aiocbs[l].aio_lio_opcode = LIO_READ;
928 aiocbps[l] = &aiocbs[l];
931 if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) {
932 int saved_errno = errno;
934 if (errno == EAGAIN || errno == EINTR || errno == EIO) {
936 * A portion of the requests may have been submitted.
939 for (l = 0; l < VDEV_LABELS; l++) {
941 int r = aio_error(&aiocbs[l]);
943 (void) aio_return(&aiocbs[l]);
951 for (l = 0; l < VDEV_LABELS; l++) {
952 uint64_t state, guid, txg;
954 if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t))
957 if (nvlist_unpack(labels[l].vp_nvlist,
958 sizeof (labels[l].vp_nvlist), config, 0) != 0)
961 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
962 &guid) != 0 || guid == 0) {
963 nvlist_free(*config);
967 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
968 &state) != 0 || state > POOL_STATE_L2CACHE) {
969 nvlist_free(*config);
973 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
974 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
975 &txg) != 0 || txg == 0)) {
976 nvlist_free(*config);
981 if (expected_guid == guid)
984 nvlist_free(*config);
986 expected_config = *config;
987 expected_guid = guid;
992 if (num_labels != NULL)
996 *config = expected_config;
1002 * Sorted by full path and then vdev guid to allow for multiple entries with
1003 * the same full path name. This is required because it's possible to
1004 * have multiple block devices with labels that refer to the same
1005 * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both
1006 * entries need to be added to the cache. Scenarios where this can occur
1007 * include overwritten pool labels, devices which are visible from multiple
1008 * hosts and multipath devices.
1011 slice_cache_compare(const void *arg1, const void *arg2)
1013 const char *nm1 = ((rdsk_node_t *)arg1)->rn_name;
1014 const char *nm2 = ((rdsk_node_t *)arg2)->rn_name;
1015 uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
1016 uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
1019 rv = TREE_ISIGN(strcmp(nm1, nm2));
1023 return (TREE_CMP(guid1, guid2));
1027 label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
1028 uint64_t vdev_guid, char **path, char **devid)
1036 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1037 &child, &children) == 0) {
1038 for (c = 0; c < children; c++) {
1039 error = label_paths_impl(hdl, child[c],
1040 pool_guid, vdev_guid, path, devid);
1050 error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1051 if ((error != 0) || (guid != vdev_guid))
1054 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1058 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1066 * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1067 * and store these strings as config_path and devid_path respectively.
1068 * The returned pointers are only valid as long as label remains valid.
1071 label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1080 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1081 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1082 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1085 return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1090 zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock,
1091 avl_tree_t *cache, const char *path, const char *name, int order)
1096 slice = zutil_alloc(hdl, sizeof (rdsk_node_t));
1097 if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) {
1101 slice->rn_vdev_guid = 0;
1102 slice->rn_lock = lock;
1103 slice->rn_avl = cache;
1104 slice->rn_hdl = hdl;
1105 slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET;
1106 slice->rn_labelpaths = B_FALSE;
1108 pthread_mutex_lock(lock);
1109 if (avl_find(cache, slice, &where)) {
1110 free(slice->rn_name);
1113 avl_insert(cache, slice, where);
1115 pthread_mutex_unlock(lock);
1119 zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock,
1120 avl_tree_t *cache, const char *dir, int order)
1123 char path[MAXPATHLEN];
1124 struct dirent64 *dp;
1127 if (realpath(dir, path) == NULL) {
1129 if (error == ENOENT)
1132 zutil_error_aux(hdl, strerror(error));
1133 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1134 TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1138 dirp = opendir(path);
1141 zutil_error_aux(hdl, strerror(error));
1142 (void) zutil_error_fmt(hdl, EZFS_BADPATH,
1143 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
1147 while ((dp = readdir64(dirp)) != NULL) {
1148 const char *name = dp->d_name;
1149 if (name[0] == '.' &&
1150 (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1153 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name,
1157 (void) closedir(dirp);
1162 zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
1163 avl_tree_t *cache, const char *dir, int order)
1166 char path[MAXPATHLEN];
1171 * Separate the directory part and last part of the
1172 * path. We do this so that we can get the realpath of
1173 * the directory. We don't get the realpath on the
1174 * whole path because if it's a symlink, we want the
1175 * path of the symlink not where it points to.
1177 d = zutil_strdup(hdl, dir);
1178 b = zutil_strdup(hdl, dir);
1182 if (realpath(dpath, path) == NULL) {
1184 if (error == ENOENT) {
1189 zutil_error_aux(hdl, strerror(error));
1190 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1191 TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1195 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
1204 * Scan a list of directories for zfs devices.
1207 zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock,
1208 avl_tree_t **slice_cache, const char * const *dir, size_t dirs)
1215 *slice_cache = NULL;
1216 cache = zutil_alloc(hdl, sizeof (avl_tree_t));
1217 avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1218 offsetof(rdsk_node_t, rn_node));
1220 for (i = 0; i < dirs; i++) {
1223 if (stat(dir[i], &sbuf) != 0) {
1225 if (error == ENOENT)
1228 zutil_error_aux(hdl, strerror(error));
1229 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1230 TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1235 * If dir[i] is a directory, we walk through it and add all
1236 * the entries to the cache. If it's not a directory, we just
1237 * add it to the cache.
1239 if (S_ISDIR(sbuf.st_mode)) {
1240 if ((error = zpool_find_import_scan_dir(hdl, lock,
1241 cache, dir[i], i)) != 0)
1244 if ((error = zpool_find_import_scan_path(hdl, lock,
1245 cache, dir[i], i)) != 0)
1250 *slice_cache = cache;
1255 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1256 free(slice->rn_name);
1265 * Given a list of directories to search, find all pools stored on disk. This
1266 * includes partial pools which are not available to import. If no args are
1267 * given (argc is 0), then the default directory (/dev/dsk) is searched.
1268 * poolname or guid (but not both) are provided by the caller when trying
1269 * to import a specific pool.
1272 zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg,
1273 pthread_mutex_t *lock, avl_tree_t *cache)
1275 nvlist_t *ret = NULL;
1276 pool_list_t pools = { 0 };
1277 pool_entry_t *pe, *penext;
1278 vdev_entry_t *ve, *venext;
1279 config_entry_t *ce, *cenext;
1280 name_entry_t *ne, *nenext;
1285 verify(iarg->poolname == NULL || iarg->guid == 0);
1288 * Create a thread pool to parallelize the process of reading and
1289 * validating labels, a large number of threads can be used due to
1290 * minimal contention.
1292 t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
1293 for (slice = avl_first(cache); slice;
1294 (slice = avl_walk(cache, slice, AVL_AFTER)))
1295 (void) tpool_dispatch(t, zpool_open_func, slice);
1301 * Process the cache, filtering out any entries which are not
1302 * for the specified pool then adding matching label configs.
1305 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1306 if (slice->rn_config != NULL) {
1307 nvlist_t *config = slice->rn_config;
1308 boolean_t matched = B_TRUE;
1309 boolean_t aux = B_FALSE;
1313 * Check if it's a spare or l2cache device. If it is,
1314 * we need to skip the name and guid check since they
1315 * don't exist on aux device label.
1317 if (iarg->poolname != NULL || iarg->guid != 0) {
1319 aux = nvlist_lookup_uint64(config,
1320 ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1321 (state == POOL_STATE_SPARE ||
1322 state == POOL_STATE_L2CACHE);
1325 if (iarg->poolname != NULL && !aux) {
1328 matched = nvlist_lookup_string(config,
1329 ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1330 strcmp(iarg->poolname, pname) == 0;
1331 } else if (iarg->guid != 0 && !aux) {
1334 matched = nvlist_lookup_uint64(config,
1335 ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1336 iarg->guid == this_guid;
1340 * Verify all remaining entries can be opened
1341 * exclusively. This will prune all underlying
1342 * multipath devices which otherwise could
1343 * result in the vdev appearing as UNAVAIL.
1345 * Under zdb, this step isn't required and
1346 * would prevent a zdb -e of active pools with
1349 fd = open(slice->rn_name, O_RDONLY | O_EXCL);
1350 if (fd >= 0 || iarg->can_be_active) {
1353 add_config(hdl, &pools,
1354 slice->rn_name, slice->rn_order,
1355 slice->rn_num_labels, config);
1358 nvlist_free(config);
1360 free(slice->rn_name);
1366 ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy);
1368 for (pe = pools.pools; pe != NULL; pe = penext) {
1369 penext = pe->pe_next;
1370 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1371 venext = ve->ve_next;
1372 for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1373 cenext = ce->ce_next;
1374 nvlist_free(ce->ce_config);
1382 for (ne = pools.names; ne != NULL; ne = nenext) {
1383 nenext = ne->ne_next;
1392 * Given a config, discover the paths for the devices which
1393 * exist in the config.
1396 discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv,
1397 avl_tree_t *cache, pthread_mutex_t *lock)
1403 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1404 &child, &children) == 0) {
1405 for (int c = 0; c < children; c++) {
1406 discover_cached_paths(hdl, child[c], cache, lock);
1411 * Once we have the path, we need to add the directory to
1412 * our directoy cache.
1414 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1415 return (zpool_find_import_scan_dir(hdl, lock, cache,
1422 * Given a cache file, return the contents as a list of importable pools.
1423 * poolname or guid (but not both) are provided by the caller when trying
1424 * to import a specific pool.
1427 zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg)
1431 struct stat64 statbuf;
1432 nvlist_t *raw, *src, *dst;
1439 verify(iarg->poolname == NULL || iarg->guid == 0);
1441 if ((fd = open(iarg->cachefile, O_RDONLY)) < 0) {
1442 zutil_error_aux(hdl, "%s", strerror(errno));
1443 (void) zutil_error(hdl, EZFS_BADCACHE,
1444 dgettext(TEXT_DOMAIN, "failed to open cache file"));
1448 if (fstat64(fd, &statbuf) != 0) {
1449 zutil_error_aux(hdl, "%s", strerror(errno));
1451 (void) zutil_error(hdl, EZFS_BADCACHE,
1452 dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
1456 if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) {
1461 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1464 (void) zutil_error(hdl, EZFS_BADCACHE,
1465 dgettext(TEXT_DOMAIN,
1466 "failed to read cache file contents"));
1472 if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1474 (void) zutil_error(hdl, EZFS_BADCACHE,
1475 dgettext(TEXT_DOMAIN,
1476 "invalid or corrupt cache file contents"));
1483 * Go through and get the current state of the pools and refresh their
1486 if (nvlist_alloc(&pools, 0, 0) != 0) {
1487 (void) zutil_no_memory(hdl);
1493 while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1494 src = fnvpair_value_nvlist(elem);
1496 name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
1497 if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0)
1500 this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1501 if (iarg->guid != 0 && iarg->guid != this_guid)
1504 if (zutil_pool_active(hdl, name, this_guid, &active) != 0) {
1514 uint64_t saved_guid = iarg->guid;
1515 const char *saved_poolname = iarg->poolname;
1516 pthread_mutex_t lock;
1519 * Create the device cache that will hold the
1520 * devices we will scan based on the cachefile.
1521 * This will get destroyed and freed by
1522 * zpool_find_import_impl.
1524 avl_tree_t *cache = zutil_alloc(hdl,
1525 sizeof (avl_tree_t));
1526 avl_create(cache, slice_cache_compare,
1527 sizeof (rdsk_node_t),
1528 offsetof(rdsk_node_t, rn_node));
1529 nvlist_t *nvroot = fnvlist_lookup_nvlist(src,
1530 ZPOOL_CONFIG_VDEV_TREE);
1533 * We only want to find the pool with this_guid.
1534 * We will reset these values back later.
1536 iarg->guid = this_guid;
1537 iarg->poolname = NULL;
1540 * We need to build up a cache of devices that exists
1541 * in the paths pointed to by the cachefile. This allows
1542 * us to preserve the device namespace that was
1543 * originally specified by the user but also lets us
1544 * scan devices in those directories in case they had
1547 pthread_mutex_init(&lock, NULL);
1548 discover_cached_paths(hdl, nvroot, cache, &lock);
1549 nvlist_t *nv = zpool_find_import_impl(hdl, iarg,
1551 pthread_mutex_destroy(&lock);
1554 * zpool_find_import_impl will return back
1555 * a list of pools that it found based on the
1556 * device cache. There should only be one pool
1557 * since we're looking for a specific guid.
1558 * We will use that pool to build up the final
1559 * pool nvlist which is returned back to the
1562 nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
1563 fnvlist_add_nvlist(pools, nvpair_name(pair),
1564 fnvpair_value_nvlist(pair));
1566 VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL);
1568 iarg->guid = saved_guid;
1569 iarg->poolname = saved_poolname;
1573 if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE,
1574 iarg->cachefile) != 0) {
1575 (void) zutil_no_memory(hdl);
1581 if ((dst = zutil_refresh_config(hdl, src)) == NULL) {
1587 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1588 (void) zutil_no_memory(hdl);
1601 zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg)
1603 pthread_mutex_t lock;
1605 nvlist_t *pools = NULL;
1607 verify(iarg->poolname == NULL || iarg->guid == 0);
1608 pthread_mutex_init(&lock, NULL);
1611 * Locate pool member vdevs by blkid or by directory scanning.
1612 * On success a newly allocated AVL tree which is populated with an
1613 * entry for each discovered vdev will be returned in the cache.
1614 * It's the caller's responsibility to consume and destroy this tree.
1616 if (iarg->scan || iarg->paths != 0) {
1617 size_t dirs = iarg->paths;
1618 const char * const *dir = (const char * const *)iarg->path;
1621 dir = zpool_default_search_paths(&dirs);
1623 if (zpool_find_import_scan(hdl, &lock, &cache,
1625 pthread_mutex_destroy(&lock);
1629 if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) {
1630 pthread_mutex_destroy(&lock);
1635 pools = zpool_find_import_impl(hdl, iarg, &lock, cache);
1636 pthread_mutex_destroy(&lock);
1642 zpool_search_import(void *hdl, importargs_t *import,
1643 const pool_config_ops_t *pco)
1645 libpc_handle_t handle = { 0 };
1646 nvlist_t *pools = NULL;
1648 handle.lpc_lib_handle = hdl;
1649 handle.lpc_ops = pco;
1650 handle.lpc_printerr = B_TRUE;
1652 verify(import->poolname == NULL || import->guid == 0);
1654 if (import->cachefile != NULL)
1655 pools = zpool_find_import_cached(&handle, import);
1657 pools = zpool_find_import(&handle, import);
1659 if ((pools == NULL || nvlist_empty(pools)) &&
1660 handle.lpc_open_access_error && geteuid() != 0) {
1661 (void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN,
1669 pool_match(nvlist_t *cfg, char *tgt)
1671 uint64_t v, guid = strtoull(tgt, NULL, 0);
1675 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
1678 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
1679 return (strcmp(s, tgt) == 0);
1685 zpool_find_config(void *hdl, const char *target, nvlist_t **configp,
1686 importargs_t *args, const pool_config_ops_t *pco)
1689 nvlist_t *match = NULL;
1690 nvlist_t *config = NULL;
1694 char *targetdup = strdup(target);
1698 if ((sepp = strpbrk(targetdup, "/@")) != NULL) {
1703 pools = zpool_search_import(hdl, args, pco);
1705 if (pools != NULL) {
1706 nvpair_t *elem = NULL;
1707 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1708 VERIFY0(nvpair_value_nvlist(elem, &config));
1709 if (pool_match(config, targetdup)) {
1711 if (match != NULL) {
1712 /* multiple matches found */
1715 match = fnvlist_dup(config);
1719 fnvlist_free(pools);
1729 fnvlist_free(match);