Handle possible null pointers from malloc/strdup/strndup()
[zfs.git] / lib / libzutil / zutil_import.c
blobe7a755dcbd970597344390185a1a2814f6e43f2a
1 /*
2 * CDDL HEADER START
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 https://opensource.org/licenses/CDDL-1.0.
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]
19 * CDDL HEADER END
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.
50 #ifdef HAVE_AIO_H
51 #include <aio.h>
52 #endif
53 #include <ctype.h>
54 #include <dirent.h>
55 #include <errno.h>
56 #include <libintl.h>
57 #include <libgen.h>
58 #include <stddef.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sys/stat.h>
62 #include <unistd.h>
63 #include <fcntl.h>
64 #include <sys/dktp/fdisk.h>
65 #include <sys/vdev_impl.h>
66 #include <sys/fs/zfs.h>
68 #include <thread_pool.h>
69 #include <libzutil.h>
70 #include <libnvpair.h>
72 #include "zutil_import.h"
74 const char *
75 libpc_error_description(libpc_handle_t *hdl)
77 if (hdl->lpc_desc[0] != '\0')
78 return (hdl->lpc_desc);
80 switch (hdl->lpc_error) {
81 case LPC_BADCACHE:
82 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
83 case LPC_BADPATH:
84 return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
85 case LPC_NOMEM:
86 return (dgettext(TEXT_DOMAIN, "out of memory"));
87 case LPC_EACCESS:
88 return (dgettext(TEXT_DOMAIN, "some devices require root "
89 "privileges"));
90 case LPC_UNKNOWN:
91 return (dgettext(TEXT_DOMAIN, "unknown error"));
92 default:
93 assert(hdl->lpc_error == 0);
94 return (dgettext(TEXT_DOMAIN, "no error"));
98 static __attribute__((format(printf, 2, 3))) void
99 zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...)
101 va_list ap;
103 va_start(ap, fmt);
105 (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap);
106 hdl->lpc_desc_active = B_TRUE;
108 va_end(ap);
111 static void
112 zutil_verror(libpc_handle_t *hdl, lpc_error_t error, const char *fmt,
113 va_list ap)
115 char action[1024];
117 (void) vsnprintf(action, sizeof (action), fmt, ap);
118 hdl->lpc_error = error;
120 if (hdl->lpc_desc_active)
121 hdl->lpc_desc_active = B_FALSE;
122 else
123 hdl->lpc_desc[0] = '\0';
125 if (hdl->lpc_printerr)
126 (void) fprintf(stderr, "%s: %s\n", action,
127 libpc_error_description(hdl));
130 static __attribute__((format(printf, 3, 4))) int
131 zutil_error_fmt(libpc_handle_t *hdl, lpc_error_t error,
132 const char *fmt, ...)
134 va_list ap;
136 va_start(ap, fmt);
138 zutil_verror(hdl, error, fmt, ap);
140 va_end(ap);
142 return (-1);
145 static int
146 zutil_error(libpc_handle_t *hdl, lpc_error_t error, const char *msg)
148 return (zutil_error_fmt(hdl, error, "%s", msg));
151 static int
152 zutil_no_memory(libpc_handle_t *hdl)
154 zutil_error(hdl, LPC_NOMEM, "internal error");
155 exit(1);
158 void *
159 zutil_alloc(libpc_handle_t *hdl, size_t size)
161 void *data;
163 if ((data = calloc(1, size)) == NULL)
164 (void) zutil_no_memory(hdl);
166 return (data);
169 char *
170 zutil_strdup(libpc_handle_t *hdl, const char *str)
172 char *ret;
174 if ((ret = strdup(str)) == NULL)
175 (void) zutil_no_memory(hdl);
177 return (ret);
180 static char *
181 zutil_strndup(libpc_handle_t *hdl, const char *str, size_t n)
183 char *ret;
185 if ((ret = strndup(str, n)) == NULL)
186 (void) zutil_no_memory(hdl);
188 return (ret);
192 * Intermediate structures used to gather configuration information.
194 typedef struct config_entry {
195 uint64_t ce_txg;
196 nvlist_t *ce_config;
197 struct config_entry *ce_next;
198 } config_entry_t;
200 typedef struct vdev_entry {
201 uint64_t ve_guid;
202 config_entry_t *ve_configs;
203 struct vdev_entry *ve_next;
204 } vdev_entry_t;
206 typedef struct pool_entry {
207 uint64_t pe_guid;
208 vdev_entry_t *pe_vdevs;
209 struct pool_entry *pe_next;
210 } pool_entry_t;
212 typedef struct name_entry {
213 char *ne_name;
214 uint64_t ne_guid;
215 uint64_t ne_order;
216 uint64_t ne_num_labels;
217 struct name_entry *ne_next;
218 } name_entry_t;
220 typedef struct pool_list {
221 pool_entry_t *pools;
222 name_entry_t *names;
223 } pool_list_t;
226 * Go through and fix up any path and/or devid information for the given vdev
227 * configuration.
229 static int
230 fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names)
232 nvlist_t **child;
233 uint_t c, children;
234 uint64_t guid;
235 name_entry_t *ne, *best;
236 char *path;
238 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
239 &child, &children) == 0) {
240 for (c = 0; c < children; c++)
241 if (fix_paths(hdl, child[c], names) != 0)
242 return (-1);
243 return (0);
247 * This is a leaf (file or disk) vdev. In either case, go through
248 * the name list and see if we find a matching guid. If so, replace
249 * the path and see if we can calculate a new devid.
251 * There may be multiple names associated with a particular guid, in
252 * which case we have overlapping partitions or multiple paths to the
253 * same disk. In this case we prefer to use the path name which
254 * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we
255 * use the lowest order device which corresponds to the first match
256 * while traversing the ZPOOL_IMPORT_PATH search path.
258 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
259 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
260 path = NULL;
262 best = NULL;
263 for (ne = names; ne != NULL; ne = ne->ne_next) {
264 if (ne->ne_guid == guid) {
265 if (path == NULL) {
266 best = ne;
267 break;
270 if ((strlen(path) == strlen(ne->ne_name)) &&
271 strncmp(path, ne->ne_name, strlen(path)) == 0) {
272 best = ne;
273 break;
276 if (best == NULL) {
277 best = ne;
278 continue;
281 /* Prefer paths with move vdev labels. */
282 if (ne->ne_num_labels > best->ne_num_labels) {
283 best = ne;
284 continue;
287 /* Prefer paths earlier in the search order. */
288 if (ne->ne_num_labels == best->ne_num_labels &&
289 ne->ne_order < best->ne_order) {
290 best = ne;
291 continue;
296 if (best == NULL)
297 return (0);
299 if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
300 return (-1);
302 update_vdev_config_dev_strs(nv);
304 return (0);
308 * Add the given configuration to the list of known devices.
310 static int
311 add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path,
312 int order, int num_labels, nvlist_t *config)
314 uint64_t pool_guid, vdev_guid, top_guid, txg, state;
315 pool_entry_t *pe;
316 vdev_entry_t *ve;
317 config_entry_t *ce;
318 name_entry_t *ne;
321 * If this is a hot spare not currently in use or level 2 cache
322 * device, add it to the list of names to translate, but don't do
323 * anything else.
325 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
326 &state) == 0 &&
327 (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
328 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
329 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
330 return (-1);
332 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
333 free(ne);
334 return (-1);
336 ne->ne_guid = vdev_guid;
337 ne->ne_order = order;
338 ne->ne_num_labels = num_labels;
339 ne->ne_next = pl->names;
340 pl->names = ne;
342 return (0);
346 * If we have a valid config but cannot read any of these fields, then
347 * it means we have a half-initialized label. In vdev_label_init()
348 * we write a label with txg == 0 so that we can identify the device
349 * in case the user refers to the same disk later on. If we fail to
350 * create the pool, we'll be left with a label in this state
351 * which should not be considered part of a valid pool.
353 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
354 &pool_guid) != 0 ||
355 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
356 &vdev_guid) != 0 ||
357 nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
358 &top_guid) != 0 ||
359 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
360 &txg) != 0 || txg == 0) {
361 return (0);
365 * First, see if we know about this pool. If not, then add it to the
366 * list of known pools.
368 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
369 if (pe->pe_guid == pool_guid)
370 break;
373 if (pe == NULL) {
374 if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
375 return (-1);
377 pe->pe_guid = pool_guid;
378 pe->pe_next = pl->pools;
379 pl->pools = pe;
383 * Second, see if we know about this toplevel vdev. Add it if its
384 * missing.
386 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
387 if (ve->ve_guid == top_guid)
388 break;
391 if (ve == NULL) {
392 if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
393 return (-1);
395 ve->ve_guid = top_guid;
396 ve->ve_next = pe->pe_vdevs;
397 pe->pe_vdevs = ve;
401 * Third, see if we have a config with a matching transaction group. If
402 * so, then we do nothing. Otherwise, add it to the list of known
403 * configs.
405 for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
406 if (ce->ce_txg == txg)
407 break;
410 if (ce == NULL) {
411 if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) {
412 return (-1);
414 ce->ce_txg = txg;
415 ce->ce_config = fnvlist_dup(config);
416 ce->ce_next = ve->ve_configs;
417 ve->ve_configs = ce;
421 * At this point we've successfully added our config to the list of
422 * known configs. The last thing to do is add the vdev guid -> path
423 * mappings so that we can fix up the configuration as necessary before
424 * doing the import.
426 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
427 return (-1);
429 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
430 free(ne);
431 return (-1);
434 ne->ne_guid = vdev_guid;
435 ne->ne_order = order;
436 ne->ne_num_labels = num_labels;
437 ne->ne_next = pl->names;
438 pl->names = ne;
440 return (0);
443 static int
444 zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid,
445 boolean_t *isactive)
447 ASSERT(hdl->lpc_ops->pco_pool_active != NULL);
449 int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name,
450 guid, isactive);
452 return (error);
455 static nvlist_t *
456 zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig)
458 ASSERT(hdl->lpc_ops->pco_refresh_config != NULL);
460 return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle,
461 tryconfig));
465 * Determine if the vdev id is a hole in the namespace.
467 static boolean_t
468 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
470 int c;
472 for (c = 0; c < holes; c++) {
474 /* Top-level is a hole */
475 if (hole_array[c] == id)
476 return (B_TRUE);
478 return (B_FALSE);
482 * Convert our list of pools into the definitive set of configurations. We
483 * start by picking the best config for each toplevel vdev. Once that's done,
484 * we assemble the toplevel vdevs into a full config for the pool. We make a
485 * pass to fix up any incorrect paths, and then add it to the main list to
486 * return to the user.
488 static nvlist_t *
489 get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok,
490 nvlist_t *policy)
492 pool_entry_t *pe;
493 vdev_entry_t *ve;
494 config_entry_t *ce;
495 nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
496 nvlist_t **spares, **l2cache;
497 uint_t i, nspares, nl2cache;
498 boolean_t config_seen;
499 uint64_t best_txg;
500 char *name, *hostname = NULL;
501 uint64_t guid;
502 uint_t children = 0;
503 nvlist_t **child = NULL;
504 uint_t holes;
505 uint64_t *hole_array, max_id;
506 uint_t c;
507 boolean_t isactive;
508 uint64_t hostid;
509 nvlist_t *nvl;
510 boolean_t valid_top_config = B_FALSE;
512 if (nvlist_alloc(&ret, 0, 0) != 0)
513 goto nomem;
515 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
516 uint64_t id, max_txg = 0;
518 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
519 goto nomem;
520 config_seen = B_FALSE;
523 * Iterate over all toplevel vdevs. Grab the pool configuration
524 * from the first one we find, and then go through the rest and
525 * add them as necessary to the 'vdevs' member of the config.
527 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
530 * Determine the best configuration for this vdev by
531 * selecting the config with the latest transaction
532 * group.
534 best_txg = 0;
535 for (ce = ve->ve_configs; ce != NULL;
536 ce = ce->ce_next) {
538 if (ce->ce_txg > best_txg) {
539 tmp = ce->ce_config;
540 best_txg = ce->ce_txg;
545 * We rely on the fact that the max txg for the
546 * pool will contain the most up-to-date information
547 * about the valid top-levels in the vdev namespace.
549 if (best_txg > max_txg) {
550 (void) nvlist_remove(config,
551 ZPOOL_CONFIG_VDEV_CHILDREN,
552 DATA_TYPE_UINT64);
553 (void) nvlist_remove(config,
554 ZPOOL_CONFIG_HOLE_ARRAY,
555 DATA_TYPE_UINT64_ARRAY);
557 max_txg = best_txg;
558 hole_array = NULL;
559 holes = 0;
560 max_id = 0;
561 valid_top_config = B_FALSE;
563 if (nvlist_lookup_uint64(tmp,
564 ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
565 verify(nvlist_add_uint64(config,
566 ZPOOL_CONFIG_VDEV_CHILDREN,
567 max_id) == 0);
568 valid_top_config = B_TRUE;
571 if (nvlist_lookup_uint64_array(tmp,
572 ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
573 &holes) == 0) {
574 verify(nvlist_add_uint64_array(config,
575 ZPOOL_CONFIG_HOLE_ARRAY,
576 hole_array, holes) == 0);
580 if (!config_seen) {
582 * Copy the relevant pieces of data to the pool
583 * configuration:
585 * version
586 * pool guid
587 * name
588 * comment (if available)
589 * compatibility features (if available)
590 * pool state
591 * hostid (if available)
592 * hostname (if available)
594 uint64_t state, version;
595 char *comment = NULL;
596 char *compatibility = NULL;
598 version = fnvlist_lookup_uint64(tmp,
599 ZPOOL_CONFIG_VERSION);
600 fnvlist_add_uint64(config,
601 ZPOOL_CONFIG_VERSION, version);
602 guid = fnvlist_lookup_uint64(tmp,
603 ZPOOL_CONFIG_POOL_GUID);
604 fnvlist_add_uint64(config,
605 ZPOOL_CONFIG_POOL_GUID, guid);
606 name = fnvlist_lookup_string(tmp,
607 ZPOOL_CONFIG_POOL_NAME);
608 fnvlist_add_string(config,
609 ZPOOL_CONFIG_POOL_NAME, name);
611 if (nvlist_lookup_string(tmp,
612 ZPOOL_CONFIG_COMMENT, &comment) == 0)
613 fnvlist_add_string(config,
614 ZPOOL_CONFIG_COMMENT, comment);
616 if (nvlist_lookup_string(tmp,
617 ZPOOL_CONFIG_COMPATIBILITY,
618 &compatibility) == 0)
619 fnvlist_add_string(config,
620 ZPOOL_CONFIG_COMPATIBILITY,
621 compatibility);
623 state = fnvlist_lookup_uint64(tmp,
624 ZPOOL_CONFIG_POOL_STATE);
625 fnvlist_add_uint64(config,
626 ZPOOL_CONFIG_POOL_STATE, state);
628 hostid = 0;
629 if (nvlist_lookup_uint64(tmp,
630 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
631 fnvlist_add_uint64(config,
632 ZPOOL_CONFIG_HOSTID, hostid);
633 hostname = fnvlist_lookup_string(tmp,
634 ZPOOL_CONFIG_HOSTNAME);
635 fnvlist_add_string(config,
636 ZPOOL_CONFIG_HOSTNAME, hostname);
639 config_seen = B_TRUE;
643 * Add this top-level vdev to the child array.
645 verify(nvlist_lookup_nvlist(tmp,
646 ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
647 verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
648 &id) == 0);
650 if (id >= children) {
651 nvlist_t **newchild;
653 newchild = zutil_alloc(hdl, (id + 1) *
654 sizeof (nvlist_t *));
655 if (newchild == NULL)
656 goto nomem;
658 for (c = 0; c < children; c++)
659 newchild[c] = child[c];
661 free(child);
662 child = newchild;
663 children = id + 1;
665 if (nvlist_dup(nvtop, &child[id], 0) != 0)
666 goto nomem;
671 * If we have information about all the top-levels then
672 * clean up the nvlist which we've constructed. This
673 * means removing any extraneous devices that are
674 * beyond the valid range or adding devices to the end
675 * of our array which appear to be missing.
677 if (valid_top_config) {
678 if (max_id < children) {
679 for (c = max_id; c < children; c++)
680 nvlist_free(child[c]);
681 children = max_id;
682 } else if (max_id > children) {
683 nvlist_t **newchild;
685 newchild = zutil_alloc(hdl, (max_id) *
686 sizeof (nvlist_t *));
687 if (newchild == NULL)
688 goto nomem;
690 for (c = 0; c < children; c++)
691 newchild[c] = child[c];
693 free(child);
694 child = newchild;
695 children = max_id;
699 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
700 &guid) == 0);
703 * The vdev namespace may contain holes as a result of
704 * device removal. We must add them back into the vdev
705 * tree before we process any missing devices.
707 if (holes > 0) {
708 ASSERT(valid_top_config);
710 for (c = 0; c < children; c++) {
711 nvlist_t *holey;
713 if (child[c] != NULL ||
714 !vdev_is_hole(hole_array, holes, c))
715 continue;
717 if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
718 0) != 0)
719 goto nomem;
722 * Holes in the namespace are treated as
723 * "hole" top-level vdevs and have a
724 * special flag set on them.
726 if (nvlist_add_string(holey,
727 ZPOOL_CONFIG_TYPE,
728 VDEV_TYPE_HOLE) != 0 ||
729 nvlist_add_uint64(holey,
730 ZPOOL_CONFIG_ID, c) != 0 ||
731 nvlist_add_uint64(holey,
732 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
733 nvlist_free(holey);
734 goto nomem;
736 child[c] = holey;
741 * Look for any missing top-level vdevs. If this is the case,
742 * create a faked up 'missing' vdev as a placeholder. We cannot
743 * simply compress the child array, because the kernel performs
744 * certain checks to make sure the vdev IDs match their location
745 * in the configuration.
747 for (c = 0; c < children; c++) {
748 if (child[c] == NULL) {
749 nvlist_t *missing;
750 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
751 0) != 0)
752 goto nomem;
753 if (nvlist_add_string(missing,
754 ZPOOL_CONFIG_TYPE,
755 VDEV_TYPE_MISSING) != 0 ||
756 nvlist_add_uint64(missing,
757 ZPOOL_CONFIG_ID, c) != 0 ||
758 nvlist_add_uint64(missing,
759 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
760 nvlist_free(missing);
761 goto nomem;
763 child[c] = missing;
768 * Put all of this pool's top-level vdevs into a root vdev.
770 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
771 goto nomem;
772 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
773 VDEV_TYPE_ROOT) != 0 ||
774 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
775 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
776 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
777 (const nvlist_t **)child, children) != 0) {
778 nvlist_free(nvroot);
779 goto nomem;
782 for (c = 0; c < children; c++)
783 nvlist_free(child[c]);
784 free(child);
785 children = 0;
786 child = NULL;
789 * Go through and fix up any paths and/or devids based on our
790 * known list of vdev GUID -> path mappings.
792 if (fix_paths(hdl, nvroot, pl->names) != 0) {
793 nvlist_free(nvroot);
794 goto nomem;
798 * Add the root vdev to this pool's configuration.
800 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
801 nvroot) != 0) {
802 nvlist_free(nvroot);
803 goto nomem;
805 nvlist_free(nvroot);
808 * zdb uses this path to report on active pools that were
809 * imported or created using -R.
811 if (active_ok)
812 goto add_pool;
815 * Determine if this pool is currently active, in which case we
816 * can't actually import it.
818 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
819 &name) == 0);
820 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
821 &guid) == 0);
823 if (zutil_pool_active(hdl, name, guid, &isactive) != 0)
824 goto error;
826 if (isactive) {
827 nvlist_free(config);
828 config = NULL;
829 continue;
832 if (policy != NULL) {
833 if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
834 policy) != 0)
835 goto nomem;
838 if ((nvl = zutil_refresh_config(hdl, config)) == NULL) {
839 nvlist_free(config);
840 config = NULL;
841 continue;
844 nvlist_free(config);
845 config = nvl;
848 * Go through and update the paths for spares, now that we have
849 * them.
851 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
852 &nvroot) == 0);
853 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
854 &spares, &nspares) == 0) {
855 for (i = 0; i < nspares; i++) {
856 if (fix_paths(hdl, spares[i], pl->names) != 0)
857 goto nomem;
862 * Update the paths for l2cache devices.
864 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
865 &l2cache, &nl2cache) == 0) {
866 for (i = 0; i < nl2cache; i++) {
867 if (fix_paths(hdl, l2cache[i], pl->names) != 0)
868 goto nomem;
873 * Restore the original information read from the actual label.
875 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
876 DATA_TYPE_UINT64);
877 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
878 DATA_TYPE_STRING);
879 if (hostid != 0) {
880 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
881 hostid) == 0);
882 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
883 hostname) == 0);
886 add_pool:
888 * Add this pool to the list of configs.
890 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
891 &name) == 0);
893 if (nvlist_add_nvlist(ret, name, config) != 0)
894 goto nomem;
896 nvlist_free(config);
897 config = NULL;
900 return (ret);
902 nomem:
903 (void) zutil_no_memory(hdl);
904 error:
905 nvlist_free(config);
906 nvlist_free(ret);
907 for (c = 0; c < children; c++)
908 nvlist_free(child[c]);
909 free(child);
911 return (NULL);
915 * Return the offset of the given label.
917 static uint64_t
918 label_offset(uint64_t size, int l)
920 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
921 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
922 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
926 * The same description applies as to zpool_read_label below,
927 * except here we do it without aio, presumably because an aio call
928 * errored out in a way we think not using it could circumvent.
930 static int
931 zpool_read_label_slow(int fd, nvlist_t **config, int *num_labels)
933 struct stat64 statbuf;
934 int l, count = 0;
935 vdev_phys_t *label;
936 nvlist_t *expected_config = NULL;
937 uint64_t expected_guid = 0, size;
938 int error;
940 *config = NULL;
942 if (fstat64_blk(fd, &statbuf) == -1)
943 return (0);
944 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
946 error = posix_memalign((void **)&label, PAGESIZE, sizeof (*label));
947 if (error)
948 return (-1);
950 for (l = 0; l < VDEV_LABELS; l++) {
951 uint64_t state, guid, txg;
952 off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE;
954 if (pread64(fd, label, sizeof (vdev_phys_t),
955 offset) != sizeof (vdev_phys_t))
956 continue;
958 if (nvlist_unpack(label->vp_nvlist,
959 sizeof (label->vp_nvlist), config, 0) != 0)
960 continue;
962 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
963 &guid) != 0 || guid == 0) {
964 nvlist_free(*config);
965 continue;
968 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
969 &state) != 0 || state > POOL_STATE_L2CACHE) {
970 nvlist_free(*config);
971 continue;
974 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
975 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
976 &txg) != 0 || txg == 0)) {
977 nvlist_free(*config);
978 continue;
981 if (expected_guid) {
982 if (expected_guid == guid)
983 count++;
985 nvlist_free(*config);
986 } else {
987 expected_config = *config;
988 expected_guid = guid;
989 count++;
993 if (num_labels != NULL)
994 *num_labels = count;
996 free(label);
997 *config = expected_config;
999 return (0);
1003 * Given a file descriptor, read the label information and return an nvlist
1004 * describing the configuration, if there is one. The number of valid
1005 * labels found will be returned in num_labels when non-NULL.
1008 zpool_read_label(int fd, nvlist_t **config, int *num_labels)
1010 #ifndef HAVE_AIO_H
1011 return (zpool_read_label_slow(fd, config, num_labels));
1012 #else
1013 struct stat64 statbuf;
1014 struct aiocb aiocbs[VDEV_LABELS];
1015 struct aiocb *aiocbps[VDEV_LABELS];
1016 vdev_phys_t *labels;
1017 nvlist_t *expected_config = NULL;
1018 uint64_t expected_guid = 0, size;
1019 int error, l, count = 0;
1021 *config = NULL;
1023 if (fstat64_blk(fd, &statbuf) == -1)
1024 return (0);
1025 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1027 error = posix_memalign((void **)&labels, PAGESIZE,
1028 VDEV_LABELS * sizeof (*labels));
1029 if (error)
1030 return (-1);
1032 memset(aiocbs, 0, sizeof (aiocbs));
1033 for (l = 0; l < VDEV_LABELS; l++) {
1034 off_t offset = label_offset(size, l) + VDEV_SKIP_SIZE;
1036 aiocbs[l].aio_fildes = fd;
1037 aiocbs[l].aio_offset = offset;
1038 aiocbs[l].aio_buf = &labels[l];
1039 aiocbs[l].aio_nbytes = sizeof (vdev_phys_t);
1040 aiocbs[l].aio_lio_opcode = LIO_READ;
1041 aiocbps[l] = &aiocbs[l];
1044 if (lio_listio(LIO_WAIT, aiocbps, VDEV_LABELS, NULL) != 0) {
1045 int saved_errno = errno;
1046 boolean_t do_slow = B_FALSE;
1047 error = -1;
1049 if (errno == EAGAIN || errno == EINTR || errno == EIO) {
1051 * A portion of the requests may have been submitted.
1052 * Clean them up.
1054 for (l = 0; l < VDEV_LABELS; l++) {
1055 errno = 0;
1056 switch (aio_error(&aiocbs[l])) {
1057 case EINVAL:
1058 break;
1059 case EINPROGRESS:
1060 // This shouldn't be possible to
1061 // encounter, die if we do.
1062 ASSERT(B_FALSE);
1063 zfs_fallthrough;
1064 case EOPNOTSUPP:
1065 case ENOSYS:
1066 do_slow = B_TRUE;
1067 zfs_fallthrough;
1068 case 0:
1069 default:
1070 (void) aio_return(&aiocbs[l]);
1074 if (do_slow) {
1076 * At least some IO involved access unsafe-for-AIO
1077 * files. Let's try again, without AIO this time.
1079 error = zpool_read_label_slow(fd, config, num_labels);
1080 saved_errno = errno;
1082 free(labels);
1083 errno = saved_errno;
1084 return (error);
1087 for (l = 0; l < VDEV_LABELS; l++) {
1088 uint64_t state, guid, txg;
1090 if (aio_return(&aiocbs[l]) != sizeof (vdev_phys_t))
1091 continue;
1093 if (nvlist_unpack(labels[l].vp_nvlist,
1094 sizeof (labels[l].vp_nvlist), config, 0) != 0)
1095 continue;
1097 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
1098 &guid) != 0 || guid == 0) {
1099 nvlist_free(*config);
1100 continue;
1103 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
1104 &state) != 0 || state > POOL_STATE_L2CACHE) {
1105 nvlist_free(*config);
1106 continue;
1109 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1110 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
1111 &txg) != 0 || txg == 0)) {
1112 nvlist_free(*config);
1113 continue;
1116 if (expected_guid) {
1117 if (expected_guid == guid)
1118 count++;
1120 nvlist_free(*config);
1121 } else {
1122 expected_config = *config;
1123 expected_guid = guid;
1124 count++;
1128 if (num_labels != NULL)
1129 *num_labels = count;
1131 free(labels);
1132 *config = expected_config;
1134 return (0);
1135 #endif
1139 * Sorted by full path and then vdev guid to allow for multiple entries with
1140 * the same full path name. This is required because it's possible to
1141 * have multiple block devices with labels that refer to the same
1142 * ZPOOL_CONFIG_PATH yet have different vdev guids. In this case both
1143 * entries need to be added to the cache. Scenarios where this can occur
1144 * include overwritten pool labels, devices which are visible from multiple
1145 * hosts and multipath devices.
1148 slice_cache_compare(const void *arg1, const void *arg2)
1150 const char *nm1 = ((rdsk_node_t *)arg1)->rn_name;
1151 const char *nm2 = ((rdsk_node_t *)arg2)->rn_name;
1152 uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
1153 uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
1154 int rv;
1156 rv = TREE_ISIGN(strcmp(nm1, nm2));
1157 if (rv)
1158 return (rv);
1160 return (TREE_CMP(guid1, guid2));
1163 static int
1164 label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
1165 uint64_t vdev_guid, char **path, char **devid)
1167 nvlist_t **child;
1168 uint_t c, children;
1169 uint64_t guid;
1170 char *val;
1171 int error;
1173 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1174 &child, &children) == 0) {
1175 for (c = 0; c < children; c++) {
1176 error = label_paths_impl(hdl, child[c],
1177 pool_guid, vdev_guid, path, devid);
1178 if (error)
1179 return (error);
1181 return (0);
1184 if (nvroot == NULL)
1185 return (0);
1187 error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1188 if ((error != 0) || (guid != vdev_guid))
1189 return (0);
1191 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1192 if (error == 0)
1193 *path = val;
1195 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1196 if (error == 0)
1197 *devid = val;
1199 return (0);
1203 * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1204 * and store these strings as config_path and devid_path respectively.
1205 * The returned pointers are only valid as long as label remains valid.
1208 label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1210 nvlist_t *nvroot;
1211 uint64_t pool_guid;
1212 uint64_t vdev_guid;
1214 *path = NULL;
1215 *devid = NULL;
1217 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1218 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1219 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1220 return (ENOENT);
1222 return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1223 devid));
1226 static void
1227 zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock,
1228 avl_tree_t *cache, const char *path, const char *name, int order)
1230 avl_index_t where;
1231 rdsk_node_t *slice;
1233 slice = zutil_alloc(hdl, sizeof (rdsk_node_t));
1234 if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) {
1235 free(slice);
1236 return;
1238 slice->rn_vdev_guid = 0;
1239 slice->rn_lock = lock;
1240 slice->rn_avl = cache;
1241 slice->rn_hdl = hdl;
1242 slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET;
1243 slice->rn_labelpaths = B_FALSE;
1245 pthread_mutex_lock(lock);
1246 if (avl_find(cache, slice, &where)) {
1247 free(slice->rn_name);
1248 free(slice);
1249 } else {
1250 avl_insert(cache, slice, where);
1252 pthread_mutex_unlock(lock);
1255 static int
1256 zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock,
1257 avl_tree_t *cache, const char *dir, int order)
1259 int error;
1260 char path[MAXPATHLEN];
1261 struct dirent64 *dp;
1262 DIR *dirp;
1264 if (realpath(dir, path) == NULL) {
1265 error = errno;
1266 if (error == ENOENT)
1267 return (0);
1269 zutil_error_aux(hdl, "%s", strerror(error));
1270 (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN,
1271 "cannot resolve path '%s'"), dir);
1272 return (error);
1275 dirp = opendir(path);
1276 if (dirp == NULL) {
1277 error = errno;
1278 zutil_error_aux(hdl, "%s", strerror(error));
1279 (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN,
1280 "cannot open '%s'"), path);
1281 return (error);
1284 while ((dp = readdir64(dirp)) != NULL) {
1285 const char *name = dp->d_name;
1286 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
1287 continue;
1289 switch (dp->d_type) {
1290 case DT_UNKNOWN:
1291 case DT_BLK:
1292 case DT_LNK:
1293 #ifdef __FreeBSD__
1294 case DT_CHR:
1295 #endif
1296 case DT_REG:
1297 break;
1298 default:
1299 continue;
1302 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name,
1303 order);
1306 (void) closedir(dirp);
1307 return (0);
1310 static int
1311 zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
1312 avl_tree_t *cache, const char *dir, int order)
1314 int error = 0;
1315 char path[MAXPATHLEN];
1316 char *d = NULL;
1317 ssize_t dl;
1318 const char *dpath, *name;
1321 * Separate the directory and the basename.
1322 * We do this so that we can get the realpath of
1323 * the directory. We don't get the realpath on the
1324 * whole path because if it's a symlink, we want the
1325 * path of the symlink not where it points to.
1327 name = zfs_basename(dir);
1328 if ((dl = zfs_dirnamelen(dir)) == -1)
1329 dpath = ".";
1330 else
1331 dpath = d = zutil_strndup(hdl, dir, dl);
1333 if (realpath(dpath, path) == NULL) {
1334 error = errno;
1335 if (error == ENOENT) {
1336 error = 0;
1337 goto out;
1340 zutil_error_aux(hdl, "%s", strerror(error));
1341 (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(TEXT_DOMAIN,
1342 "cannot resolve path '%s'"), dir);
1343 goto out;
1346 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
1348 out:
1349 free(d);
1350 return (error);
1354 * Scan a list of directories for zfs devices.
1356 static int
1357 zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock,
1358 avl_tree_t **slice_cache, const char * const *dir, size_t dirs)
1360 avl_tree_t *cache;
1361 rdsk_node_t *slice;
1362 void *cookie;
1363 int i, error;
1365 *slice_cache = NULL;
1366 cache = zutil_alloc(hdl, sizeof (avl_tree_t));
1367 avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1368 offsetof(rdsk_node_t, rn_node));
1370 for (i = 0; i < dirs; i++) {
1371 struct stat sbuf;
1373 if (stat(dir[i], &sbuf) != 0) {
1374 error = errno;
1375 if (error == ENOENT)
1376 continue;
1378 zutil_error_aux(hdl, "%s", strerror(error));
1379 (void) zutil_error_fmt(hdl, LPC_BADPATH, dgettext(
1380 TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1381 goto error;
1385 * If dir[i] is a directory, we walk through it and add all
1386 * the entries to the cache. If it's not a directory, we just
1387 * add it to the cache.
1389 if (S_ISDIR(sbuf.st_mode)) {
1390 if ((error = zpool_find_import_scan_dir(hdl, lock,
1391 cache, dir[i], i)) != 0)
1392 goto error;
1393 } else {
1394 if ((error = zpool_find_import_scan_path(hdl, lock,
1395 cache, dir[i], i)) != 0)
1396 goto error;
1400 *slice_cache = cache;
1401 return (0);
1403 error:
1404 cookie = NULL;
1405 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1406 free(slice->rn_name);
1407 free(slice);
1409 free(cache);
1411 return (error);
1415 * Given a list of directories to search, find all pools stored on disk. This
1416 * includes partial pools which are not available to import. If no args are
1417 * given (argc is 0), then the default directory (/dev/dsk) is searched.
1418 * poolname or guid (but not both) are provided by the caller when trying
1419 * to import a specific pool.
1421 static nvlist_t *
1422 zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg,
1423 pthread_mutex_t *lock, avl_tree_t *cache)
1425 (void) lock;
1426 nvlist_t *ret = NULL;
1427 pool_list_t pools = { 0 };
1428 pool_entry_t *pe, *penext;
1429 vdev_entry_t *ve, *venext;
1430 config_entry_t *ce, *cenext;
1431 name_entry_t *ne, *nenext;
1432 rdsk_node_t *slice;
1433 void *cookie;
1434 tpool_t *t;
1436 verify(iarg->poolname == NULL || iarg->guid == 0);
1439 * Create a thread pool to parallelize the process of reading and
1440 * validating labels, a large number of threads can be used due to
1441 * minimal contention.
1443 t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
1444 for (slice = avl_first(cache); slice;
1445 (slice = avl_walk(cache, slice, AVL_AFTER)))
1446 (void) tpool_dispatch(t, zpool_open_func, slice);
1448 tpool_wait(t);
1449 tpool_destroy(t);
1452 * Process the cache, filtering out any entries which are not
1453 * for the specified pool then adding matching label configs.
1455 cookie = NULL;
1456 while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1457 if (slice->rn_config != NULL) {
1458 nvlist_t *config = slice->rn_config;
1459 boolean_t matched = B_TRUE;
1460 boolean_t aux = B_FALSE;
1461 int fd;
1464 * Check if it's a spare or l2cache device. If it is,
1465 * we need to skip the name and guid check since they
1466 * don't exist on aux device label.
1468 if (iarg->poolname != NULL || iarg->guid != 0) {
1469 uint64_t state;
1470 aux = nvlist_lookup_uint64(config,
1471 ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1472 (state == POOL_STATE_SPARE ||
1473 state == POOL_STATE_L2CACHE);
1476 if (iarg->poolname != NULL && !aux) {
1477 char *pname;
1479 matched = nvlist_lookup_string(config,
1480 ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1481 strcmp(iarg->poolname, pname) == 0;
1482 } else if (iarg->guid != 0 && !aux) {
1483 uint64_t this_guid;
1485 matched = nvlist_lookup_uint64(config,
1486 ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1487 iarg->guid == this_guid;
1489 if (matched) {
1491 * Verify all remaining entries can be opened
1492 * exclusively. This will prune all underlying
1493 * multipath devices which otherwise could
1494 * result in the vdev appearing as UNAVAIL.
1496 * Under zdb, this step isn't required and
1497 * would prevent a zdb -e of active pools with
1498 * no cachefile.
1500 fd = open(slice->rn_name,
1501 O_RDONLY | O_EXCL | O_CLOEXEC);
1502 if (fd >= 0 || iarg->can_be_active) {
1503 if (fd >= 0)
1504 close(fd);
1505 add_config(hdl, &pools,
1506 slice->rn_name, slice->rn_order,
1507 slice->rn_num_labels, config);
1510 nvlist_free(config);
1512 free(slice->rn_name);
1513 free(slice);
1515 avl_destroy(cache);
1516 free(cache);
1518 ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy);
1520 for (pe = pools.pools; pe != NULL; pe = penext) {
1521 penext = pe->pe_next;
1522 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1523 venext = ve->ve_next;
1524 for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1525 cenext = ce->ce_next;
1526 nvlist_free(ce->ce_config);
1527 free(ce);
1529 free(ve);
1531 free(pe);
1534 for (ne = pools.names; ne != NULL; ne = nenext) {
1535 nenext = ne->ne_next;
1536 free(ne->ne_name);
1537 free(ne);
1540 return (ret);
1544 * Given a config, discover the paths for the devices which
1545 * exist in the config.
1547 static int
1548 discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv,
1549 avl_tree_t *cache, pthread_mutex_t *lock)
1551 char *path = NULL;
1552 ssize_t dl;
1553 uint_t children;
1554 nvlist_t **child;
1556 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1557 &child, &children) == 0) {
1558 for (int c = 0; c < children; c++) {
1559 discover_cached_paths(hdl, child[c], cache, lock);
1564 * Once we have the path, we need to add the directory to
1565 * our directory cache.
1567 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1568 if ((dl = zfs_dirnamelen(path)) == -1)
1569 path = (char *)".";
1570 else
1571 path[dl] = '\0';
1572 return (zpool_find_import_scan_dir(hdl, lock, cache,
1573 path, 0));
1575 return (0);
1579 * Given a cache file, return the contents as a list of importable pools.
1580 * poolname or guid (but not both) are provided by the caller when trying
1581 * to import a specific pool.
1583 static nvlist_t *
1584 zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg)
1586 char *buf;
1587 int fd;
1588 struct stat64 statbuf;
1589 nvlist_t *raw, *src, *dst;
1590 nvlist_t *pools;
1591 nvpair_t *elem;
1592 char *name;
1593 uint64_t this_guid;
1594 boolean_t active;
1596 verify(iarg->poolname == NULL || iarg->guid == 0);
1598 if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) {
1599 zutil_error_aux(hdl, "%s", strerror(errno));
1600 (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1601 "failed to open cache file"));
1602 return (NULL);
1605 if (fstat64(fd, &statbuf) != 0) {
1606 zutil_error_aux(hdl, "%s", strerror(errno));
1607 (void) close(fd);
1608 (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1609 "failed to get size of cache file"));
1610 return (NULL);
1613 if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) {
1614 (void) close(fd);
1615 return (NULL);
1618 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1619 (void) close(fd);
1620 free(buf);
1621 (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1622 "failed to read cache file contents"));
1623 return (NULL);
1626 (void) close(fd);
1628 if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1629 free(buf);
1630 (void) zutil_error(hdl, LPC_BADCACHE, dgettext(TEXT_DOMAIN,
1631 "invalid or corrupt cache file contents"));
1632 return (NULL);
1635 free(buf);
1638 * Go through and get the current state of the pools and refresh their
1639 * state.
1641 if (nvlist_alloc(&pools, 0, 0) != 0) {
1642 (void) zutil_no_memory(hdl);
1643 nvlist_free(raw);
1644 return (NULL);
1647 elem = NULL;
1648 while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1649 src = fnvpair_value_nvlist(elem);
1651 name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
1652 if (iarg->poolname != NULL && strcmp(iarg->poolname, name) != 0)
1653 continue;
1655 this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1656 if (iarg->guid != 0 && iarg->guid != this_guid)
1657 continue;
1659 if (zutil_pool_active(hdl, name, this_guid, &active) != 0) {
1660 nvlist_free(raw);
1661 nvlist_free(pools);
1662 return (NULL);
1665 if (active)
1666 continue;
1668 if (iarg->scan) {
1669 uint64_t saved_guid = iarg->guid;
1670 const char *saved_poolname = iarg->poolname;
1671 pthread_mutex_t lock;
1674 * Create the device cache that will hold the
1675 * devices we will scan based on the cachefile.
1676 * This will get destroyed and freed by
1677 * zpool_find_import_impl.
1679 avl_tree_t *cache = zutil_alloc(hdl,
1680 sizeof (avl_tree_t));
1681 avl_create(cache, slice_cache_compare,
1682 sizeof (rdsk_node_t),
1683 offsetof(rdsk_node_t, rn_node));
1684 nvlist_t *nvroot = fnvlist_lookup_nvlist(src,
1685 ZPOOL_CONFIG_VDEV_TREE);
1688 * We only want to find the pool with this_guid.
1689 * We will reset these values back later.
1691 iarg->guid = this_guid;
1692 iarg->poolname = NULL;
1695 * We need to build up a cache of devices that exists
1696 * in the paths pointed to by the cachefile. This allows
1697 * us to preserve the device namespace that was
1698 * originally specified by the user but also lets us
1699 * scan devices in those directories in case they had
1700 * been renamed.
1702 pthread_mutex_init(&lock, NULL);
1703 discover_cached_paths(hdl, nvroot, cache, &lock);
1704 nvlist_t *nv = zpool_find_import_impl(hdl, iarg,
1705 &lock, cache);
1706 pthread_mutex_destroy(&lock);
1709 * zpool_find_import_impl will return back
1710 * a list of pools that it found based on the
1711 * device cache. There should only be one pool
1712 * since we're looking for a specific guid.
1713 * We will use that pool to build up the final
1714 * pool nvlist which is returned back to the
1715 * caller.
1717 nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
1718 if (pair == NULL)
1719 continue;
1720 fnvlist_add_nvlist(pools, nvpair_name(pair),
1721 fnvpair_value_nvlist(pair));
1723 VERIFY3P(nvlist_next_nvpair(nv, pair), ==, NULL);
1725 iarg->guid = saved_guid;
1726 iarg->poolname = saved_poolname;
1727 continue;
1730 if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE,
1731 iarg->cachefile) != 0) {
1732 (void) zutil_no_memory(hdl);
1733 nvlist_free(raw);
1734 nvlist_free(pools);
1735 return (NULL);
1738 update_vdevs_config_dev_sysfs_path(src);
1740 if ((dst = zutil_refresh_config(hdl, src)) == NULL) {
1741 nvlist_free(raw);
1742 nvlist_free(pools);
1743 return (NULL);
1746 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1747 (void) zutil_no_memory(hdl);
1748 nvlist_free(dst);
1749 nvlist_free(raw);
1750 nvlist_free(pools);
1751 return (NULL);
1753 nvlist_free(dst);
1755 nvlist_free(raw);
1756 return (pools);
1759 static nvlist_t *
1760 zpool_find_import(libpc_handle_t *hdl, importargs_t *iarg)
1762 pthread_mutex_t lock;
1763 avl_tree_t *cache;
1764 nvlist_t *pools = NULL;
1766 verify(iarg->poolname == NULL || iarg->guid == 0);
1767 pthread_mutex_init(&lock, NULL);
1770 * Locate pool member vdevs by blkid or by directory scanning.
1771 * On success a newly allocated AVL tree which is populated with an
1772 * entry for each discovered vdev will be returned in the cache.
1773 * It's the caller's responsibility to consume and destroy this tree.
1775 if (iarg->scan || iarg->paths != 0) {
1776 size_t dirs = iarg->paths;
1777 const char * const *dir = (const char * const *)iarg->path;
1779 if (dirs == 0)
1780 dir = zpool_default_search_paths(&dirs);
1782 if (zpool_find_import_scan(hdl, &lock, &cache,
1783 dir, dirs) != 0) {
1784 pthread_mutex_destroy(&lock);
1785 return (NULL);
1787 } else {
1788 if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) {
1789 pthread_mutex_destroy(&lock);
1790 return (NULL);
1794 pools = zpool_find_import_impl(hdl, iarg, &lock, cache);
1795 pthread_mutex_destroy(&lock);
1796 return (pools);
1800 nvlist_t *
1801 zpool_search_import(libpc_handle_t *hdl, importargs_t *import)
1803 nvlist_t *pools = NULL;
1805 verify(import->poolname == NULL || import->guid == 0);
1807 if (import->cachefile != NULL)
1808 pools = zpool_find_import_cached(hdl, import);
1809 else
1810 pools = zpool_find_import(hdl, import);
1812 if ((pools == NULL || nvlist_empty(pools)) &&
1813 hdl->lpc_open_access_error && geteuid() != 0) {
1814 (void) zutil_error(hdl, LPC_EACCESS, dgettext(TEXT_DOMAIN,
1815 "no pools found"));
1818 return (pools);
1821 static boolean_t
1822 pool_match(nvlist_t *cfg, char *tgt)
1824 uint64_t v, guid = strtoull(tgt, NULL, 0);
1825 char *s;
1827 if (guid != 0) {
1828 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
1829 return (v == guid);
1830 } else {
1831 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
1832 return (strcmp(s, tgt) == 0);
1834 return (B_FALSE);
1838 zpool_find_config(libpc_handle_t *hdl, const char *target, nvlist_t **configp,
1839 importargs_t *args)
1841 nvlist_t *pools;
1842 nvlist_t *match = NULL;
1843 nvlist_t *config = NULL;
1844 char *sepp = NULL;
1845 int count = 0;
1846 char *targetdup = strdup(target);
1848 if (targetdup == NULL)
1849 return (ENOMEM);
1851 *configp = NULL;
1853 if ((sepp = strpbrk(targetdup, "/@")) != NULL)
1854 *sepp = '\0';
1856 pools = zpool_search_import(hdl, args);
1858 if (pools != NULL) {
1859 nvpair_t *elem = NULL;
1860 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1861 VERIFY0(nvpair_value_nvlist(elem, &config));
1862 if (pool_match(config, targetdup)) {
1863 count++;
1864 if (match != NULL) {
1865 /* multiple matches found */
1866 continue;
1867 } else {
1868 match = fnvlist_dup(config);
1872 fnvlist_free(pools);
1875 if (count == 0) {
1876 free(targetdup);
1877 return (ENOENT);
1880 if (count > 1) {
1881 free(targetdup);
1882 fnvlist_free(match);
1883 return (EINVAL);
1886 *configp = match;
1887 free(targetdup);
1889 return (0);
1893 * Internal function for iterating over the vdevs.
1895 * For each vdev, func() will be called and will be passed 'zhp' (which is
1896 * typically the zpool_handle_t cast as a void pointer), the vdev's nvlist, and
1897 * a user-defined data pointer).
1899 * The return values from all the func() calls will be OR'd together and
1900 * returned.
1903 for_each_vdev_cb(void *zhp, nvlist_t *nv, pool_vdev_iter_f func,
1904 void *data)
1906 nvlist_t **child;
1907 uint_t c, children;
1908 int ret = 0;
1909 int i;
1910 char *type;
1912 const char *list[] = {
1913 ZPOOL_CONFIG_SPARES,
1914 ZPOOL_CONFIG_L2CACHE,
1915 ZPOOL_CONFIG_CHILDREN
1918 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1919 return (ret);
1921 /* Don't run our function on root or indirect vdevs */
1922 if ((strcmp(type, VDEV_TYPE_ROOT) != 0) &&
1923 (strcmp(type, VDEV_TYPE_INDIRECT) != 0)) {
1924 ret |= func(zhp, nv, data);
1927 for (i = 0; i < ARRAY_SIZE(list); i++) {
1928 if (nvlist_lookup_nvlist_array(nv, list[i], &child,
1929 &children) == 0) {
1930 for (c = 0; c < children; c++) {
1931 uint64_t ishole = 0;
1933 (void) nvlist_lookup_uint64(child[c],
1934 ZPOOL_CONFIG_IS_HOLE, &ishole);
1936 if (ishole)
1937 continue;
1939 ret |= for_each_vdev_cb(zhp, child[c],
1940 func, data);
1945 return (ret);
1949 * Given an ZPOOL_CONFIG_VDEV_TREE nvpair, iterate over all the vdevs, calling
1950 * func() for each one. func() is passed the vdev's nvlist and an optional
1951 * user-defined 'data' pointer.
1954 for_each_vdev_in_nvlist(nvlist_t *nvroot, pool_vdev_iter_f func, void *data)
1956 return (for_each_vdev_cb(NULL, nvroot, func, data));