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>
66 #include <thread_pool.h>
68 #include <libnvpair.h>
70 #include "zutil_import.h"
72 static __attribute__((format(printf
, 2, 3))) void
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
);
106 static __attribute__((format(printf
, 3, 4))) int
107 zutil_error_fmt(libpc_handle_t
*hdl
, const char *error
, const char *fmt
, ...)
113 zutil_verror(hdl
, error
, fmt
, ap
);
121 zutil_error(libpc_handle_t
*hdl
, const char *error
, const char *msg
)
123 return (zutil_error_fmt(hdl
, error
, "%s", msg
));
127 zutil_no_memory(libpc_handle_t
*hdl
)
129 zutil_error(hdl
, EZFS_NOMEM
, "internal error");
134 zutil_alloc(libpc_handle_t
*hdl
, size_t size
)
138 if ((data
= calloc(1, size
)) == NULL
)
139 (void) zutil_no_memory(hdl
);
145 zutil_strdup(libpc_handle_t
*hdl
, const char *str
)
149 if ((ret
= strdup(str
)) == NULL
)
150 (void) zutil_no_memory(hdl
);
156 zutil_strndup(libpc_handle_t
*hdl
, const char *str
, size_t n
)
160 if ((ret
= strndup(str
, n
)) == NULL
)
161 (void) zutil_no_memory(hdl
);
167 * Intermediate structures used to gather configuration information.
169 typedef struct config_entry
{
172 struct config_entry
*ce_next
;
175 typedef struct vdev_entry
{
177 config_entry_t
*ve_configs
;
178 struct vdev_entry
*ve_next
;
181 typedef struct pool_entry
{
183 vdev_entry_t
*pe_vdevs
;
184 struct pool_entry
*pe_next
;
187 typedef struct name_entry
{
191 uint64_t ne_num_labels
;
192 struct name_entry
*ne_next
;
195 typedef struct pool_list
{
201 * Go through and fix up any path and/or devid information for the given vdev
205 fix_paths(libpc_handle_t
*hdl
, nvlist_t
*nv
, name_entry_t
*names
)
210 name_entry_t
*ne
, *best
;
213 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
214 &child
, &children
) == 0) {
215 for (c
= 0; c
< children
; c
++)
216 if (fix_paths(hdl
, child
[c
], names
) != 0)
222 * This is a leaf (file or disk) vdev. In either case, go through
223 * the name list and see if we find a matching guid. If so, replace
224 * the path and see if we can calculate a new devid.
226 * There may be multiple names associated with a particular guid, in
227 * which case we have overlapping partitions or multiple paths to the
228 * same disk. In this case we prefer to use the path name which
229 * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we
230 * use the lowest order device which corresponds to the first match
231 * while traversing the ZPOOL_IMPORT_PATH search path.
233 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
234 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) != 0)
238 for (ne
= names
; ne
!= NULL
; ne
= ne
->ne_next
) {
239 if (ne
->ne_guid
== guid
) {
245 if ((strlen(path
) == strlen(ne
->ne_name
)) &&
246 strncmp(path
, ne
->ne_name
, strlen(path
)) == 0) {
256 /* Prefer paths with move vdev labels. */
257 if (ne
->ne_num_labels
> best
->ne_num_labels
) {
262 /* Prefer paths earlier in the search order. */
263 if (ne
->ne_num_labels
== best
->ne_num_labels
&&
264 ne
->ne_order
< best
->ne_order
) {
274 if (nvlist_add_string(nv
, ZPOOL_CONFIG_PATH
, best
->ne_name
) != 0)
277 update_vdev_config_dev_strs(nv
);
283 * Add the given configuration to the list of known devices.
286 add_config(libpc_handle_t
*hdl
, pool_list_t
*pl
, const char *path
,
287 int order
, int num_labels
, nvlist_t
*config
)
289 uint64_t pool_guid
, vdev_guid
, top_guid
, txg
, state
;
296 * If this is a hot spare not currently in use or level 2 cache
297 * device, add it to the list of names to translate, but don't do
300 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
302 (state
== POOL_STATE_SPARE
|| state
== POOL_STATE_L2CACHE
) &&
303 nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, &vdev_guid
) == 0) {
304 if ((ne
= zutil_alloc(hdl
, sizeof (name_entry_t
))) == NULL
)
307 if ((ne
->ne_name
= zutil_strdup(hdl
, path
)) == NULL
) {
311 ne
->ne_guid
= vdev_guid
;
312 ne
->ne_order
= order
;
313 ne
->ne_num_labels
= num_labels
;
314 ne
->ne_next
= pl
->names
;
321 * If we have a valid config but cannot read any of these fields, then
322 * it means we have a half-initialized label. In vdev_label_init()
323 * we write a label with txg == 0 so that we can identify the device
324 * in case the user refers to the same disk later on. If we fail to
325 * create the pool, we'll be left with a label in this state
326 * which should not be considered part of a valid pool.
328 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
330 nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
,
332 nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TOP_GUID
,
334 nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
335 &txg
) != 0 || txg
== 0) {
340 * First, see if we know about this pool. If not, then add it to the
341 * list of known pools.
343 for (pe
= pl
->pools
; pe
!= NULL
; pe
= pe
->pe_next
) {
344 if (pe
->pe_guid
== pool_guid
)
349 if ((pe
= zutil_alloc(hdl
, sizeof (pool_entry_t
))) == NULL
) {
352 pe
->pe_guid
= pool_guid
;
353 pe
->pe_next
= pl
->pools
;
358 * Second, see if we know about this toplevel vdev. Add it if its
361 for (ve
= pe
->pe_vdevs
; ve
!= NULL
; ve
= ve
->ve_next
) {
362 if (ve
->ve_guid
== top_guid
)
367 if ((ve
= zutil_alloc(hdl
, sizeof (vdev_entry_t
))) == NULL
) {
370 ve
->ve_guid
= top_guid
;
371 ve
->ve_next
= pe
->pe_vdevs
;
376 * Third, see if we have a config with a matching transaction group. If
377 * so, then we do nothing. Otherwise, add it to the list of known
380 for (ce
= ve
->ve_configs
; ce
!= NULL
; ce
= ce
->ce_next
) {
381 if (ce
->ce_txg
== txg
)
386 if ((ce
= zutil_alloc(hdl
, sizeof (config_entry_t
))) == NULL
) {
390 ce
->ce_config
= fnvlist_dup(config
);
391 ce
->ce_next
= ve
->ve_configs
;
396 * At this point we've successfully added our config to the list of
397 * known configs. The last thing to do is add the vdev guid -> path
398 * mappings so that we can fix up the configuration as necessary before
401 if ((ne
= zutil_alloc(hdl
, sizeof (name_entry_t
))) == NULL
)
404 if ((ne
->ne_name
= zutil_strdup(hdl
, path
)) == NULL
) {
409 ne
->ne_guid
= vdev_guid
;
410 ne
->ne_order
= order
;
411 ne
->ne_num_labels
= num_labels
;
412 ne
->ne_next
= pl
->names
;
419 zutil_pool_active(libpc_handle_t
*hdl
, const char *name
, uint64_t guid
,
422 ASSERT(hdl
->lpc_ops
->pco_pool_active
!= NULL
);
424 int error
= hdl
->lpc_ops
->pco_pool_active(hdl
->lpc_lib_handle
, name
,
431 zutil_refresh_config(libpc_handle_t
*hdl
, nvlist_t
*tryconfig
)
433 ASSERT(hdl
->lpc_ops
->pco_refresh_config
!= NULL
);
435 return (hdl
->lpc_ops
->pco_refresh_config(hdl
->lpc_lib_handle
,
440 * Determine if the vdev id is a hole in the namespace.
443 vdev_is_hole(uint64_t *hole_array
, uint_t holes
, uint_t id
)
447 for (c
= 0; c
< holes
; c
++) {
449 /* Top-level is a hole */
450 if (hole_array
[c
] == id
)
457 * Convert our list of pools into the definitive set of configurations. We
458 * start by picking the best config for each toplevel vdev. Once that's done,
459 * we assemble the toplevel vdevs into a full config for the pool. We make a
460 * pass to fix up any incorrect paths, and then add it to the main list to
461 * return to the user.
464 get_configs(libpc_handle_t
*hdl
, pool_list_t
*pl
, boolean_t active_ok
,
470 nvlist_t
*ret
= NULL
, *config
= NULL
, *tmp
= NULL
, *nvtop
, *nvroot
;
471 nvlist_t
**spares
, **l2cache
;
472 uint_t i
, nspares
, nl2cache
;
473 boolean_t config_seen
;
475 char *name
, *hostname
= NULL
;
478 nvlist_t
**child
= NULL
;
480 uint64_t *hole_array
, max_id
;
485 boolean_t valid_top_config
= B_FALSE
;
487 if (nvlist_alloc(&ret
, 0, 0) != 0)
490 for (pe
= pl
->pools
; pe
!= NULL
; pe
= pe
->pe_next
) {
491 uint64_t id
, max_txg
= 0;
493 if (nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) != 0)
495 config_seen
= B_FALSE
;
498 * Iterate over all toplevel vdevs. Grab the pool configuration
499 * from the first one we find, and then go through the rest and
500 * add them as necessary to the 'vdevs' member of the config.
502 for (ve
= pe
->pe_vdevs
; ve
!= NULL
; ve
= ve
->ve_next
) {
505 * Determine the best configuration for this vdev by
506 * selecting the config with the latest transaction
510 for (ce
= ve
->ve_configs
; ce
!= NULL
;
513 if (ce
->ce_txg
> best_txg
) {
515 best_txg
= ce
->ce_txg
;
520 * We rely on the fact that the max txg for the
521 * pool will contain the most up-to-date information
522 * about the valid top-levels in the vdev namespace.
524 if (best_txg
> max_txg
) {
525 (void) nvlist_remove(config
,
526 ZPOOL_CONFIG_VDEV_CHILDREN
,
528 (void) nvlist_remove(config
,
529 ZPOOL_CONFIG_HOLE_ARRAY
,
530 DATA_TYPE_UINT64_ARRAY
);
536 valid_top_config
= B_FALSE
;
538 if (nvlist_lookup_uint64(tmp
,
539 ZPOOL_CONFIG_VDEV_CHILDREN
, &max_id
) == 0) {
540 verify(nvlist_add_uint64(config
,
541 ZPOOL_CONFIG_VDEV_CHILDREN
,
543 valid_top_config
= B_TRUE
;
546 if (nvlist_lookup_uint64_array(tmp
,
547 ZPOOL_CONFIG_HOLE_ARRAY
, &hole_array
,
549 verify(nvlist_add_uint64_array(config
,
550 ZPOOL_CONFIG_HOLE_ARRAY
,
551 hole_array
, holes
) == 0);
557 * Copy the relevant pieces of data to the pool
563 * comment (if available)
564 * compatibility features (if available)
566 * hostid (if available)
567 * hostname (if available)
569 uint64_t state
, version
;
570 char *comment
= NULL
;
571 char *compatibility
= NULL
;
573 version
= fnvlist_lookup_uint64(tmp
,
574 ZPOOL_CONFIG_VERSION
);
575 fnvlist_add_uint64(config
,
576 ZPOOL_CONFIG_VERSION
, version
);
577 guid
= fnvlist_lookup_uint64(tmp
,
578 ZPOOL_CONFIG_POOL_GUID
);
579 fnvlist_add_uint64(config
,
580 ZPOOL_CONFIG_POOL_GUID
, guid
);
581 name
= fnvlist_lookup_string(tmp
,
582 ZPOOL_CONFIG_POOL_NAME
);
583 fnvlist_add_string(config
,
584 ZPOOL_CONFIG_POOL_NAME
, name
);
586 if (nvlist_lookup_string(tmp
,
587 ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
588 fnvlist_add_string(config
,
589 ZPOOL_CONFIG_COMMENT
, comment
);
591 if (nvlist_lookup_string(tmp
,
592 ZPOOL_CONFIG_COMPATIBILITY
,
593 &compatibility
) == 0)
594 fnvlist_add_string(config
,
595 ZPOOL_CONFIG_COMPATIBILITY
,
598 state
= fnvlist_lookup_uint64(tmp
,
599 ZPOOL_CONFIG_POOL_STATE
);
600 fnvlist_add_uint64(config
,
601 ZPOOL_CONFIG_POOL_STATE
, state
);
604 if (nvlist_lookup_uint64(tmp
,
605 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
606 fnvlist_add_uint64(config
,
607 ZPOOL_CONFIG_HOSTID
, hostid
);
608 hostname
= fnvlist_lookup_string(tmp
,
609 ZPOOL_CONFIG_HOSTNAME
);
610 fnvlist_add_string(config
,
611 ZPOOL_CONFIG_HOSTNAME
, hostname
);
614 config_seen
= B_TRUE
;
618 * Add this top-level vdev to the child array.
620 verify(nvlist_lookup_nvlist(tmp
,
621 ZPOOL_CONFIG_VDEV_TREE
, &nvtop
) == 0);
622 verify(nvlist_lookup_uint64(nvtop
, ZPOOL_CONFIG_ID
,
625 if (id
>= children
) {
628 newchild
= zutil_alloc(hdl
, (id
+ 1) *
629 sizeof (nvlist_t
*));
630 if (newchild
== NULL
)
633 for (c
= 0; c
< children
; c
++)
634 newchild
[c
] = child
[c
];
640 if (nvlist_dup(nvtop
, &child
[id
], 0) != 0)
646 * If we have information about all the top-levels then
647 * clean up the nvlist which we've constructed. This
648 * means removing any extraneous devices that are
649 * beyond the valid range or adding devices to the end
650 * of our array which appear to be missing.
652 if (valid_top_config
) {
653 if (max_id
< children
) {
654 for (c
= max_id
; c
< children
; c
++)
655 nvlist_free(child
[c
]);
657 } else if (max_id
> children
) {
660 newchild
= zutil_alloc(hdl
, (max_id
) *
661 sizeof (nvlist_t
*));
662 if (newchild
== NULL
)
665 for (c
= 0; c
< children
; c
++)
666 newchild
[c
] = child
[c
];
674 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
678 * The vdev namespace may contain holes as a result of
679 * device removal. We must add them back into the vdev
680 * tree before we process any missing devices.
683 ASSERT(valid_top_config
);
685 for (c
= 0; c
< children
; c
++) {
688 if (child
[c
] != NULL
||
689 !vdev_is_hole(hole_array
, holes
, c
))
692 if (nvlist_alloc(&holey
, NV_UNIQUE_NAME
,
697 * Holes in the namespace are treated as
698 * "hole" top-level vdevs and have a
699 * special flag set on them.
701 if (nvlist_add_string(holey
,
703 VDEV_TYPE_HOLE
) != 0 ||
704 nvlist_add_uint64(holey
,
705 ZPOOL_CONFIG_ID
, c
) != 0 ||
706 nvlist_add_uint64(holey
,
707 ZPOOL_CONFIG_GUID
, 0ULL) != 0) {
716 * Look for any missing top-level vdevs. If this is the case,
717 * create a faked up 'missing' vdev as a placeholder. We cannot
718 * simply compress the child array, because the kernel performs
719 * certain checks to make sure the vdev IDs match their location
720 * in the configuration.
722 for (c
= 0; c
< children
; c
++) {
723 if (child
[c
] == NULL
) {
725 if (nvlist_alloc(&missing
, NV_UNIQUE_NAME
,
728 if (nvlist_add_string(missing
,
730 VDEV_TYPE_MISSING
) != 0 ||
731 nvlist_add_uint64(missing
,
732 ZPOOL_CONFIG_ID
, c
) != 0 ||
733 nvlist_add_uint64(missing
,
734 ZPOOL_CONFIG_GUID
, 0ULL) != 0) {
735 nvlist_free(missing
);
743 * Put all of this pool's top-level vdevs into a root vdev.
745 if (nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, 0) != 0)
747 if (nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
748 VDEV_TYPE_ROOT
) != 0 ||
749 nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) != 0 ||
750 nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, guid
) != 0 ||
751 nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
752 child
, children
) != 0) {
757 for (c
= 0; c
< children
; c
++)
758 nvlist_free(child
[c
]);
764 * Go through and fix up any paths and/or devids based on our
765 * known list of vdev GUID -> path mappings.
767 if (fix_paths(hdl
, nvroot
, pl
->names
) != 0) {
773 * Add the root vdev to this pool's configuration.
775 if (nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
783 * zdb uses this path to report on active pools that were
784 * imported or created using -R.
790 * Determine if this pool is currently active, in which case we
791 * can't actually import it.
793 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
795 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
798 if (zutil_pool_active(hdl
, name
, guid
, &isactive
) != 0)
807 if (policy
!= NULL
) {
808 if (nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
813 if ((nvl
= zutil_refresh_config(hdl
, config
)) == NULL
) {
823 * Go through and update the paths for spares, now that we have
826 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
828 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
829 &spares
, &nspares
) == 0) {
830 for (i
= 0; i
< nspares
; i
++) {
831 if (fix_paths(hdl
, spares
[i
], pl
->names
) != 0)
837 * Update the paths for l2cache devices.
839 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
840 &l2cache
, &nl2cache
) == 0) {
841 for (i
= 0; i
< nl2cache
; i
++) {
842 if (fix_paths(hdl
, l2cache
[i
], pl
->names
) != 0)
848 * Restore the original information read from the actual label.
850 (void) nvlist_remove(config
, ZPOOL_CONFIG_HOSTID
,
852 (void) nvlist_remove(config
, ZPOOL_CONFIG_HOSTNAME
,
855 verify(nvlist_add_uint64(config
, ZPOOL_CONFIG_HOSTID
,
857 verify(nvlist_add_string(config
, ZPOOL_CONFIG_HOSTNAME
,
863 * Add this pool to the list of configs.
865 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
868 if (nvlist_add_nvlist(ret
, name
, config
) != 0)
878 (void) zutil_no_memory(hdl
);
882 for (c
= 0; c
< children
; c
++)
883 nvlist_free(child
[c
]);
890 * Return the offset of the given label.
893 label_offset(uint64_t size
, int l
)
895 ASSERT(P2PHASE_TYPED(size
, sizeof (vdev_label_t
), uint64_t) == 0);
896 return (l
* sizeof (vdev_label_t
) + (l
< VDEV_LABELS
/ 2 ?
897 0 : size
- VDEV_LABELS
* sizeof (vdev_label_t
)));
901 * The same description applies as to zpool_read_label below,
902 * except here we do it without aio, presumably because an aio call
903 * errored out in a way we think not using it could circumvent.
906 zpool_read_label_slow(int fd
, nvlist_t
**config
, int *num_labels
)
908 struct stat64 statbuf
;
911 nvlist_t
*expected_config
= NULL
;
912 uint64_t expected_guid
= 0, size
;
917 if (fstat64_blk(fd
, &statbuf
) == -1)
919 size
= P2ALIGN_TYPED(statbuf
.st_size
, sizeof (vdev_label_t
), uint64_t);
921 error
= posix_memalign((void **)&label
, PAGESIZE
, sizeof (*label
));
925 for (l
= 0; l
< VDEV_LABELS
; l
++) {
926 uint64_t state
, guid
, txg
;
927 off_t offset
= label_offset(size
, l
) + VDEV_SKIP_SIZE
;
929 if (pread64(fd
, label
, sizeof (vdev_phys_t
),
930 offset
) != sizeof (vdev_phys_t
))
933 if (nvlist_unpack(label
->vp_nvlist
,
934 sizeof (label
->vp_nvlist
), config
, 0) != 0)
937 if (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_GUID
,
938 &guid
) != 0 || guid
== 0) {
939 nvlist_free(*config
);
943 if (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_POOL_STATE
,
944 &state
) != 0 || state
> POOL_STATE_L2CACHE
) {
945 nvlist_free(*config
);
949 if (state
!= POOL_STATE_SPARE
&& state
!= POOL_STATE_L2CACHE
&&
950 (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_POOL_TXG
,
951 &txg
) != 0 || txg
== 0)) {
952 nvlist_free(*config
);
957 if (expected_guid
== guid
)
960 nvlist_free(*config
);
962 expected_config
= *config
;
963 expected_guid
= guid
;
968 if (num_labels
!= NULL
)
972 *config
= expected_config
;
978 * Given a file descriptor, read the label information and return an nvlist
979 * describing the configuration, if there is one. The number of valid
980 * labels found will be returned in num_labels when non-NULL.
983 zpool_read_label(int fd
, nvlist_t
**config
, int *num_labels
)
985 struct stat64 statbuf
;
986 struct aiocb aiocbs
[VDEV_LABELS
];
987 struct aiocb
*aiocbps
[VDEV_LABELS
];
989 nvlist_t
*expected_config
= NULL
;
990 uint64_t expected_guid
= 0, size
;
991 int error
, l
, count
= 0;
995 if (fstat64_blk(fd
, &statbuf
) == -1)
997 size
= P2ALIGN_TYPED(statbuf
.st_size
, sizeof (vdev_label_t
), uint64_t);
999 error
= posix_memalign((void **)&labels
, PAGESIZE
,
1000 VDEV_LABELS
* sizeof (*labels
));
1004 memset(aiocbs
, 0, sizeof (aiocbs
));
1005 for (l
= 0; l
< VDEV_LABELS
; l
++) {
1006 off_t offset
= label_offset(size
, l
) + VDEV_SKIP_SIZE
;
1008 aiocbs
[l
].aio_fildes
= fd
;
1009 aiocbs
[l
].aio_offset
= offset
;
1010 aiocbs
[l
].aio_buf
= &labels
[l
];
1011 aiocbs
[l
].aio_nbytes
= sizeof (vdev_phys_t
);
1012 aiocbs
[l
].aio_lio_opcode
= LIO_READ
;
1013 aiocbps
[l
] = &aiocbs
[l
];
1016 if (lio_listio(LIO_WAIT
, aiocbps
, VDEV_LABELS
, NULL
) != 0) {
1017 int saved_errno
= errno
;
1018 boolean_t do_slow
= B_FALSE
;
1021 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== EIO
) {
1023 * A portion of the requests may have been submitted.
1026 for (l
= 0; l
< VDEV_LABELS
; l
++) {
1028 switch (aio_error(&aiocbs
[l
])) {
1032 // This shouldn't be possible to
1033 // encounter, die if we do.
1042 (void) aio_return(&aiocbs
[l
]);
1048 * At least some IO involved access unsafe-for-AIO
1049 * files. Let's try again, without AIO this time.
1051 error
= zpool_read_label_slow(fd
, config
, num_labels
);
1052 saved_errno
= errno
;
1055 errno
= saved_errno
;
1059 for (l
= 0; l
< VDEV_LABELS
; l
++) {
1060 uint64_t state
, guid
, txg
;
1062 if (aio_return(&aiocbs
[l
]) != sizeof (vdev_phys_t
))
1065 if (nvlist_unpack(labels
[l
].vp_nvlist
,
1066 sizeof (labels
[l
].vp_nvlist
), config
, 0) != 0)
1069 if (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_GUID
,
1070 &guid
) != 0 || guid
== 0) {
1071 nvlist_free(*config
);
1075 if (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_POOL_STATE
,
1076 &state
) != 0 || state
> POOL_STATE_L2CACHE
) {
1077 nvlist_free(*config
);
1081 if (state
!= POOL_STATE_SPARE
&& state
!= POOL_STATE_L2CACHE
&&
1082 (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_POOL_TXG
,
1083 &txg
) != 0 || txg
== 0)) {
1084 nvlist_free(*config
);
1088 if (expected_guid
) {
1089 if (expected_guid
== guid
)
1092 nvlist_free(*config
);
1094 expected_config
= *config
;
1095 expected_guid
= guid
;
1100 if (num_labels
!= NULL
)
1101 *num_labels
= count
;
1104 *config
= expected_config
;
1110 * Sorted by full path and then vdev guid to allow for multiple entries with
1111 * the same full path name. This is required because it's possible to
1112 * have multiple block devices with labels that refer to the same
1113 * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both
1114 * entries need to be added to the cache. Scenarios where this can occur
1115 * include overwritten pool labels, devices which are visible from multiple
1116 * hosts and multipath devices.
1119 slice_cache_compare(const void *arg1
, const void *arg2
)
1121 const char *nm1
= ((rdsk_node_t
*)arg1
)->rn_name
;
1122 const char *nm2
= ((rdsk_node_t
*)arg2
)->rn_name
;
1123 uint64_t guid1
= ((rdsk_node_t
*)arg1
)->rn_vdev_guid
;
1124 uint64_t guid2
= ((rdsk_node_t
*)arg2
)->rn_vdev_guid
;
1127 rv
= TREE_ISIGN(strcmp(nm1
, nm2
));
1131 return (TREE_CMP(guid1
, guid2
));
1135 label_paths_impl(libpc_handle_t
*hdl
, nvlist_t
*nvroot
, uint64_t pool_guid
,
1136 uint64_t vdev_guid
, char **path
, char **devid
)
1144 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1145 &child
, &children
) == 0) {
1146 for (c
= 0; c
< children
; c
++) {
1147 error
= label_paths_impl(hdl
, child
[c
],
1148 pool_guid
, vdev_guid
, path
, devid
);
1158 error
= nvlist_lookup_uint64(nvroot
, ZPOOL_CONFIG_GUID
, &guid
);
1159 if ((error
!= 0) || (guid
!= vdev_guid
))
1162 error
= nvlist_lookup_string(nvroot
, ZPOOL_CONFIG_PATH
, &val
);
1166 error
= nvlist_lookup_string(nvroot
, ZPOOL_CONFIG_DEVID
, &val
);
1174 * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1175 * and store these strings as config_path and devid_path respectively.
1176 * The returned pointers are only valid as long as label remains valid.
1179 label_paths(libpc_handle_t
*hdl
, nvlist_t
*label
, char **path
, char **devid
)
1188 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) ||
1189 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
) ||
1190 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &vdev_guid
))
1193 return (label_paths_impl(hdl
, nvroot
, pool_guid
, vdev_guid
, path
,
1198 zpool_find_import_scan_add_slice(libpc_handle_t
*hdl
, pthread_mutex_t
*lock
,
1199 avl_tree_t
*cache
, const char *path
, const char *name
, int order
)
1204 slice
= zutil_alloc(hdl
, sizeof (rdsk_node_t
));
1205 if (asprintf(&slice
->rn_name
, "%s/%s", path
, name
) == -1) {
1209 slice
->rn_vdev_guid
= 0;
1210 slice
->rn_lock
= lock
;
1211 slice
->rn_avl
= cache
;
1212 slice
->rn_hdl
= hdl
;
1213 slice
->rn_order
= order
+ IMPORT_ORDER_SCAN_OFFSET
;
1214 slice
->rn_labelpaths
= B_FALSE
;
1216 pthread_mutex_lock(lock
);
1217 if (avl_find(cache
, slice
, &where
)) {
1218 free(slice
->rn_name
);
1221 avl_insert(cache
, slice
, where
);
1223 pthread_mutex_unlock(lock
);
1227 zpool_find_import_scan_dir(libpc_handle_t
*hdl
, pthread_mutex_t
*lock
,
1228 avl_tree_t
*cache
, const char *dir
, int order
)
1231 char path
[MAXPATHLEN
];
1232 struct dirent64
*dp
;
1235 if (realpath(dir
, path
) == NULL
) {
1237 if (error
== ENOENT
)
1240 zutil_error_aux(hdl
, "%s", strerror(error
));
1241 (void) zutil_error_fmt(hdl
, EZFS_BADPATH
, dgettext(
1242 TEXT_DOMAIN
, "cannot resolve path '%s'"), dir
);
1246 dirp
= opendir(path
);
1249 zutil_error_aux(hdl
, "%s", strerror(error
));
1250 (void) zutil_error_fmt(hdl
, EZFS_BADPATH
,
1251 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
1255 while ((dp
= readdir64(dirp
)) != NULL
) {
1256 const char *name
= dp
->d_name
;
1257 if (strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0)
1260 switch (dp
->d_type
) {
1273 zpool_find_import_scan_add_slice(hdl
, lock
, cache
, path
, name
,
1277 (void) closedir(dirp
);
1282 zpool_find_import_scan_path(libpc_handle_t
*hdl
, pthread_mutex_t
*lock
,
1283 avl_tree_t
*cache
, const char *dir
, int order
)
1286 char path
[MAXPATHLEN
];
1289 const char *dpath
, *name
;
1292 * Separate the directory and the basename.
1293 * We do this so that we can get the realpath of
1294 * the directory. We don't get the realpath on the
1295 * whole path because if it's a symlink, we want the
1296 * path of the symlink not where it points to.
1298 name
= zfs_basename(dir
);
1299 if ((dl
= zfs_dirnamelen(dir
)) == -1)
1302 dpath
= d
= zutil_strndup(hdl
, dir
, dl
);
1304 if (realpath(dpath
, path
) == NULL
) {
1306 if (error
== ENOENT
) {
1311 zutil_error_aux(hdl
, "%s", strerror(error
));
1312 (void) zutil_error_fmt(hdl
, EZFS_BADPATH
, dgettext(
1313 TEXT_DOMAIN
, "cannot resolve path '%s'"), dir
);
1317 zpool_find_import_scan_add_slice(hdl
, lock
, cache
, path
, name
, order
);
1325 * Scan a list of directories for zfs devices.
1328 zpool_find_import_scan(libpc_handle_t
*hdl
, pthread_mutex_t
*lock
,
1329 avl_tree_t
**slice_cache
, const char * const *dir
, size_t dirs
)
1336 *slice_cache
= NULL
;
1337 cache
= zutil_alloc(hdl
, sizeof (avl_tree_t
));
1338 avl_create(cache
, slice_cache_compare
, sizeof (rdsk_node_t
),
1339 offsetof(rdsk_node_t
, rn_node
));
1341 for (i
= 0; i
< dirs
; i
++) {
1344 if (stat(dir
[i
], &sbuf
) != 0) {
1346 if (error
== ENOENT
)
1349 zutil_error_aux(hdl
, "%s", strerror(error
));
1350 (void) zutil_error_fmt(hdl
, EZFS_BADPATH
, dgettext(
1351 TEXT_DOMAIN
, "cannot resolve path '%s'"), dir
[i
]);
1356 * If dir[i] is a directory, we walk through it and add all
1357 * the entries to the cache. If it's not a directory, we just
1358 * add it to the cache.
1360 if (S_ISDIR(sbuf
.st_mode
)) {
1361 if ((error
= zpool_find_import_scan_dir(hdl
, lock
,
1362 cache
, dir
[i
], i
)) != 0)
1365 if ((error
= zpool_find_import_scan_path(hdl
, lock
,
1366 cache
, dir
[i
], i
)) != 0)
1371 *slice_cache
= cache
;
1376 while ((slice
= avl_destroy_nodes(cache
, &cookie
)) != NULL
) {
1377 free(slice
->rn_name
);
1386 * Given a list of directories to search, find all pools stored on disk. This
1387 * includes partial pools which are not available to import. If no args are
1388 * given (argc is 0), then the default directory (/dev/dsk) is searched.
1389 * poolname or guid (but not both) are provided by the caller when trying
1390 * to import a specific pool.
1393 zpool_find_import_impl(libpc_handle_t
*hdl
, importargs_t
*iarg
,
1394 pthread_mutex_t
*lock
, avl_tree_t
*cache
)
1396 nvlist_t
*ret
= NULL
;
1397 pool_list_t pools
= { 0 };
1398 pool_entry_t
*pe
, *penext
;
1399 vdev_entry_t
*ve
, *venext
;
1400 config_entry_t
*ce
, *cenext
;
1401 name_entry_t
*ne
, *nenext
;
1406 verify(iarg
->poolname
== NULL
|| iarg
->guid
== 0);
1409 * Create a thread pool to parallelize the process of reading and
1410 * validating labels, a large number of threads can be used due to
1411 * minimal contention.
1413 t
= tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN
), 0, NULL
);
1414 for (slice
= avl_first(cache
); slice
;
1415 (slice
= avl_walk(cache
, slice
, AVL_AFTER
)))
1416 (void) tpool_dispatch(t
, zpool_open_func
, slice
);
1422 * Process the cache, filtering out any entries which are not
1423 * for the specified pool then adding matching label configs.
1426 while ((slice
= avl_destroy_nodes(cache
, &cookie
)) != NULL
) {
1427 if (slice
->rn_config
!= NULL
) {
1428 nvlist_t
*config
= slice
->rn_config
;
1429 boolean_t matched
= B_TRUE
;
1430 boolean_t aux
= B_FALSE
;
1434 * Check if it's a spare or l2cache device. If it is,
1435 * we need to skip the name and guid check since they
1436 * don't exist on aux device label.
1438 if (iarg
->poolname
!= NULL
|| iarg
->guid
!= 0) {
1440 aux
= nvlist_lookup_uint64(config
,
1441 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0 &&
1442 (state
== POOL_STATE_SPARE
||
1443 state
== POOL_STATE_L2CACHE
);
1446 if (iarg
->poolname
!= NULL
&& !aux
) {
1449 matched
= nvlist_lookup_string(config
,
1450 ZPOOL_CONFIG_POOL_NAME
, &pname
) == 0 &&
1451 strcmp(iarg
->poolname
, pname
) == 0;
1452 } else if (iarg
->guid
!= 0 && !aux
) {
1455 matched
= nvlist_lookup_uint64(config
,
1456 ZPOOL_CONFIG_POOL_GUID
, &this_guid
) == 0 &&
1457 iarg
->guid
== this_guid
;
1461 * Verify all remaining entries can be opened
1462 * exclusively. This will prune all underlying
1463 * multipath devices which otherwise could
1464 * result in the vdev appearing as UNAVAIL.
1466 * Under zdb, this step isn't required and
1467 * would prevent a zdb -e of active pools with
1470 fd
= open(slice
->rn_name
,
1471 O_RDONLY
| O_EXCL
| O_CLOEXEC
);
1472 if (fd
>= 0 || iarg
->can_be_active
) {
1475 add_config(hdl
, &pools
,
1476 slice
->rn_name
, slice
->rn_order
,
1477 slice
->rn_num_labels
, config
);
1480 nvlist_free(config
);
1482 free(slice
->rn_name
);
1488 ret
= get_configs(hdl
, &pools
, iarg
->can_be_active
, iarg
->policy
);
1490 for (pe
= pools
.pools
; pe
!= NULL
; pe
= penext
) {
1491 penext
= pe
->pe_next
;
1492 for (ve
= pe
->pe_vdevs
; ve
!= NULL
; ve
= venext
) {
1493 venext
= ve
->ve_next
;
1494 for (ce
= ve
->ve_configs
; ce
!= NULL
; ce
= cenext
) {
1495 cenext
= ce
->ce_next
;
1496 nvlist_free(ce
->ce_config
);
1504 for (ne
= pools
.names
; ne
!= NULL
; ne
= nenext
) {
1505 nenext
= ne
->ne_next
;
1514 * Given a config, discover the paths for the devices which
1515 * exist in the config.
1518 discover_cached_paths(libpc_handle_t
*hdl
, nvlist_t
*nv
,
1519 avl_tree_t
*cache
, pthread_mutex_t
*lock
)
1526 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1527 &child
, &children
) == 0) {
1528 for (int c
= 0; c
< children
; c
++) {
1529 discover_cached_paths(hdl
, child
[c
], cache
, lock
);
1534 * Once we have the path, we need to add the directory to
1535 * our directory cache.
1537 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1538 if ((dl
= zfs_dirnamelen(path
)) == -1)
1542 return (zpool_find_import_scan_dir(hdl
, lock
, cache
,
1549 * Given a cache file, return the contents as a list of importable pools.
1550 * poolname or guid (but not both) are provided by the caller when trying
1551 * to import a specific pool.
1554 zpool_find_import_cached(libpc_handle_t
*hdl
, importargs_t
*iarg
)
1558 struct stat64 statbuf
;
1559 nvlist_t
*raw
, *src
, *dst
;
1566 verify(iarg
->poolname
== NULL
|| iarg
->guid
== 0);
1568 if ((fd
= open(iarg
->cachefile
, O_RDONLY
| O_CLOEXEC
)) < 0) {
1569 zutil_error_aux(hdl
, "%s", strerror(errno
));
1570 (void) zutil_error(hdl
, EZFS_BADCACHE
,
1571 dgettext(TEXT_DOMAIN
, "failed to open cache file"));
1575 if (fstat64(fd
, &statbuf
) != 0) {
1576 zutil_error_aux(hdl
, "%s", strerror(errno
));
1578 (void) zutil_error(hdl
, EZFS_BADCACHE
,
1579 dgettext(TEXT_DOMAIN
, "failed to get size of cache file"));
1583 if ((buf
= zutil_alloc(hdl
, statbuf
.st_size
)) == NULL
) {
1588 if (read(fd
, buf
, statbuf
.st_size
) != statbuf
.st_size
) {
1591 (void) zutil_error(hdl
, EZFS_BADCACHE
,
1592 dgettext(TEXT_DOMAIN
,
1593 "failed to read cache file contents"));
1599 if (nvlist_unpack(buf
, statbuf
.st_size
, &raw
, 0) != 0) {
1601 (void) zutil_error(hdl
, EZFS_BADCACHE
,
1602 dgettext(TEXT_DOMAIN
,
1603 "invalid or corrupt cache file contents"));
1610 * Go through and get the current state of the pools and refresh their
1613 if (nvlist_alloc(&pools
, 0, 0) != 0) {
1614 (void) zutil_no_memory(hdl
);
1620 while ((elem
= nvlist_next_nvpair(raw
, elem
)) != NULL
) {
1621 src
= fnvpair_value_nvlist(elem
);
1623 name
= fnvlist_lookup_string(src
, ZPOOL_CONFIG_POOL_NAME
);
1624 if (iarg
->poolname
!= NULL
&& strcmp(iarg
->poolname
, name
) != 0)
1627 this_guid
= fnvlist_lookup_uint64(src
, ZPOOL_CONFIG_POOL_GUID
);
1628 if (iarg
->guid
!= 0 && iarg
->guid
!= this_guid
)
1631 if (zutil_pool_active(hdl
, name
, this_guid
, &active
) != 0) {
1641 uint64_t saved_guid
= iarg
->guid
;
1642 const char *saved_poolname
= iarg
->poolname
;
1643 pthread_mutex_t lock
;
1646 * Create the device cache that will hold the
1647 * devices we will scan based on the cachefile.
1648 * This will get destroyed and freed by
1649 * zpool_find_import_impl.
1651 avl_tree_t
*cache
= zutil_alloc(hdl
,
1652 sizeof (avl_tree_t
));
1653 avl_create(cache
, slice_cache_compare
,
1654 sizeof (rdsk_node_t
),
1655 offsetof(rdsk_node_t
, rn_node
));
1656 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(src
,
1657 ZPOOL_CONFIG_VDEV_TREE
);
1660 * We only want to find the pool with this_guid.
1661 * We will reset these values back later.
1663 iarg
->guid
= this_guid
;
1664 iarg
->poolname
= NULL
;
1667 * We need to build up a cache of devices that exists
1668 * in the paths pointed to by the cachefile. This allows
1669 * us to preserve the device namespace that was
1670 * originally specified by the user but also lets us
1671 * scan devices in those directories in case they had
1674 pthread_mutex_init(&lock
, NULL
);
1675 discover_cached_paths(hdl
, nvroot
, cache
, &lock
);
1676 nvlist_t
*nv
= zpool_find_import_impl(hdl
, iarg
,
1678 pthread_mutex_destroy(&lock
);
1681 * zpool_find_import_impl will return back
1682 * a list of pools that it found based on the
1683 * device cache. There should only be one pool
1684 * since we're looking for a specific guid.
1685 * We will use that pool to build up the final
1686 * pool nvlist which is returned back to the
1689 nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
1690 fnvlist_add_nvlist(pools
, nvpair_name(pair
),
1691 fnvpair_value_nvlist(pair
));
1693 VERIFY3P(nvlist_next_nvpair(nv
, pair
), ==, NULL
);
1695 iarg
->guid
= saved_guid
;
1696 iarg
->poolname
= saved_poolname
;
1700 if (nvlist_add_string(src
, ZPOOL_CONFIG_CACHEFILE
,
1701 iarg
->cachefile
) != 0) {
1702 (void) zutil_no_memory(hdl
);
1708 update_vdevs_config_dev_sysfs_path(src
);
1710 if ((dst
= zutil_refresh_config(hdl
, src
)) == NULL
) {
1716 if (nvlist_add_nvlist(pools
, nvpair_name(elem
), dst
) != 0) {
1717 (void) zutil_no_memory(hdl
);
1730 zpool_find_import(libpc_handle_t
*hdl
, importargs_t
*iarg
)
1732 pthread_mutex_t lock
;
1734 nvlist_t
*pools
= NULL
;
1736 verify(iarg
->poolname
== NULL
|| iarg
->guid
== 0);
1737 pthread_mutex_init(&lock
, NULL
);
1740 * Locate pool member vdevs by blkid or by directory scanning.
1741 * On success a newly allocated AVL tree which is populated with an
1742 * entry for each discovered vdev will be returned in the cache.
1743 * It's the caller's responsibility to consume and destroy this tree.
1745 if (iarg
->scan
|| iarg
->paths
!= 0) {
1746 size_t dirs
= iarg
->paths
;
1747 const char * const *dir
= (const char * const *)iarg
->path
;
1750 dir
= zpool_default_search_paths(&dirs
);
1752 if (zpool_find_import_scan(hdl
, &lock
, &cache
,
1754 pthread_mutex_destroy(&lock
);
1758 if (zpool_find_import_blkid(hdl
, &lock
, &cache
) != 0) {
1759 pthread_mutex_destroy(&lock
);
1764 pools
= zpool_find_import_impl(hdl
, iarg
, &lock
, cache
);
1765 pthread_mutex_destroy(&lock
);
1771 zpool_search_import(void *hdl
, importargs_t
*import
,
1772 const pool_config_ops_t
*pco
)
1774 libpc_handle_t handle
= { 0 };
1775 nvlist_t
*pools
= NULL
;
1777 handle
.lpc_lib_handle
= hdl
;
1778 handle
.lpc_ops
= pco
;
1779 handle
.lpc_printerr
= B_TRUE
;
1781 verify(import
->poolname
== NULL
|| import
->guid
== 0);
1783 if (import
->cachefile
!= NULL
)
1784 pools
= zpool_find_import_cached(&handle
, import
);
1786 pools
= zpool_find_import(&handle
, import
);
1788 if ((pools
== NULL
|| nvlist_empty(pools
)) &&
1789 handle
.lpc_open_access_error
&& geteuid() != 0) {
1790 (void) zutil_error(&handle
, EZFS_EACESS
, dgettext(TEXT_DOMAIN
,
1798 pool_match(nvlist_t
*cfg
, char *tgt
)
1800 uint64_t v
, guid
= strtoull(tgt
, NULL
, 0);
1804 if (nvlist_lookup_uint64(cfg
, ZPOOL_CONFIG_POOL_GUID
, &v
) == 0)
1807 if (nvlist_lookup_string(cfg
, ZPOOL_CONFIG_POOL_NAME
, &s
) == 0)
1808 return (strcmp(s
, tgt
) == 0);
1814 zpool_find_config(void *hdl
, const char *target
, nvlist_t
**configp
,
1815 importargs_t
*args
, const pool_config_ops_t
*pco
)
1818 nvlist_t
*match
= NULL
;
1819 nvlist_t
*config
= NULL
;
1822 char *targetdup
= strdup(target
);
1826 if ((sepp
= strpbrk(targetdup
, "/@")) != NULL
)
1829 pools
= zpool_search_import(hdl
, args
, pco
);
1831 if (pools
!= NULL
) {
1832 nvpair_t
*elem
= NULL
;
1833 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
1834 VERIFY0(nvpair_value_nvlist(elem
, &config
));
1835 if (pool_match(config
, targetdup
)) {
1837 if (match
!= NULL
) {
1838 /* multiple matches found */
1841 match
= fnvlist_dup(config
);
1845 fnvlist_free(pools
);
1855 fnvlist_free(match
);
1866 * Internal function for iterating over the vdevs.
1868 * For each vdev, func() will be called and will be passed 'zhp' (which is
1869 * typically the zpool_handle_t cast as a void pointer), the vdev's nvlist, and
1870 * a user-defined data pointer).
1872 * The return values from all the func() calls will be OR'd together and
1876 for_each_vdev_cb(void *zhp
, nvlist_t
*nv
, pool_vdev_iter_f func
,
1885 const char *list
[] = {
1886 ZPOOL_CONFIG_SPARES
,
1887 ZPOOL_CONFIG_L2CACHE
,
1888 ZPOOL_CONFIG_CHILDREN
1891 for (i
= 0; i
< ARRAY_SIZE(list
); i
++) {
1892 if (nvlist_lookup_nvlist_array(nv
, list
[i
], &child
,
1894 for (c
= 0; c
< children
; c
++) {
1895 uint64_t ishole
= 0;
1897 (void) nvlist_lookup_uint64(child
[c
],
1898 ZPOOL_CONFIG_IS_HOLE
, &ishole
);
1903 ret
|= for_each_vdev_cb(zhp
, child
[c
],
1909 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
1912 /* Don't run our function on root vdevs */
1913 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0) {
1914 ret
|= func(zhp
, nv
, data
);
1921 * Given an ZPOOL_CONFIG_VDEV_TREE nvpair, iterate over all the vdevs, calling
1922 * func() for each one. func() is passed the vdev's nvlist and an optional
1923 * user-defined 'data' pointer.
1926 for_each_vdev_in_nvlist(nvlist_t
*nvroot
, pool_vdev_iter_f func
, void *data
)
1928 return (for_each_vdev_cb(NULL
, nvroot
, func
, data
));