]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/lib/libzutil/zutil_import.c
MFV 2.0-rc2
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / lib / libzutil / zutil_import.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
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  */
28
29 /*
30  * Pool import support functions.
31  *
32  * Used by zpool, ztest, zdb, and zhack to locate importable configs. Since
33  * these commands are expected to run in the global zone, we can assume
34  * that the devices are all readable when called.
35  *
36  * To import a pool, we rely on reading the configuration information from the
37  * ZFS label of each device.  If we successfully read the label, then we
38  * organize the configuration information in the following hierarchy:
39  *
40  *      pool guid -> toplevel vdev guid -> label txg
41  *
42  * Duplicate entries matching this same tuple will be discarded.  Once we have
43  * examined every device, we pick the best label txg config for each toplevel
44  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
45  * update any paths that have changed.  Finally, we attempt to import the pool
46  * using our derived config, and record the results.
47  */
48
49 #include <ctype.h>
50 #include <dirent.h>
51 #include <errno.h>
52 #include <libintl.h>
53 #include <libgen.h>
54 #include <stddef.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sys/stat.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60 #include <sys/dktp/fdisk.h>
61 #include <sys/vdev_impl.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/vdev_impl.h>
64
65 #include <thread_pool.h>
66 #include <libzutil.h>
67 #include <libnvpair.h>
68
69 #include "zutil_import.h"
70
71 /*PRINTFLIKE2*/
72 static void
73 zutil_error_aux(libpc_handle_t *hdl, const char *fmt, ...)
74 {
75         va_list ap;
76
77         va_start(ap, fmt);
78
79         (void) vsnprintf(hdl->lpc_desc, sizeof (hdl->lpc_desc), fmt, ap);
80         hdl->lpc_desc_active = B_TRUE;
81
82         va_end(ap);
83 }
84
85 static void
86 zutil_verror(libpc_handle_t *hdl, const char *error, const char *fmt,
87     va_list ap)
88 {
89         char action[1024];
90
91         (void) vsnprintf(action, sizeof (action), fmt, ap);
92
93         if (hdl->lpc_desc_active)
94                 hdl->lpc_desc_active = B_FALSE;
95         else
96                 hdl->lpc_desc[0] = '\0';
97
98         if (hdl->lpc_printerr) {
99                 if (hdl->lpc_desc[0] != '\0')
100                         error = hdl->lpc_desc;
101
102                 (void) fprintf(stderr, "%s: %s\n", action, error);
103         }
104 }
105
106 /*PRINTFLIKE3*/
107 static int
108 zutil_error_fmt(libpc_handle_t *hdl, const char *error, const char *fmt, ...)
109 {
110         va_list ap;
111
112         va_start(ap, fmt);
113
114         zutil_verror(hdl, error, fmt, ap);
115
116         va_end(ap);
117
118         return (-1);
119 }
120
121 static int
122 zutil_error(libpc_handle_t *hdl, const char *error, const char *msg)
123 {
124         return (zutil_error_fmt(hdl, error, "%s", msg));
125 }
126
127 static int
128 zutil_no_memory(libpc_handle_t *hdl)
129 {
130         zutil_error(hdl, EZFS_NOMEM, "internal error");
131         exit(1);
132 }
133
134 void *
135 zutil_alloc(libpc_handle_t *hdl, size_t size)
136 {
137         void *data;
138
139         if ((data = calloc(1, size)) == NULL)
140                 (void) zutil_no_memory(hdl);
141
142         return (data);
143 }
144
145 char *
146 zutil_strdup(libpc_handle_t *hdl, const char *str)
147 {
148         char *ret;
149
150         if ((ret = strdup(str)) == NULL)
151                 (void) zutil_no_memory(hdl);
152
153         return (ret);
154 }
155
156 /*
157  * Intermediate structures used to gather configuration information.
158  */
159 typedef struct config_entry {
160         uint64_t                ce_txg;
161         nvlist_t                *ce_config;
162         struct config_entry     *ce_next;
163 } config_entry_t;
164
165 typedef struct vdev_entry {
166         uint64_t                ve_guid;
167         config_entry_t          *ve_configs;
168         struct vdev_entry       *ve_next;
169 } vdev_entry_t;
170
171 typedef struct pool_entry {
172         uint64_t                pe_guid;
173         vdev_entry_t            *pe_vdevs;
174         struct pool_entry       *pe_next;
175 } pool_entry_t;
176
177 typedef struct name_entry {
178         char                    *ne_name;
179         uint64_t                ne_guid;
180         uint64_t                ne_order;
181         uint64_t                ne_num_labels;
182         struct name_entry       *ne_next;
183 } name_entry_t;
184
185 typedef struct pool_list {
186         pool_entry_t            *pools;
187         name_entry_t            *names;
188 } pool_list_t;
189
190 /*
191  * Go through and fix up any path and/or devid information for the given vdev
192  * configuration.
193  */
194 static int
195 fix_paths(libpc_handle_t *hdl, nvlist_t *nv, name_entry_t *names)
196 {
197         nvlist_t **child;
198         uint_t c, children;
199         uint64_t guid;
200         name_entry_t *ne, *best;
201         char *path;
202
203         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
204             &child, &children) == 0) {
205                 for (c = 0; c < children; c++)
206                         if (fix_paths(hdl, child[c], names) != 0)
207                                 return (-1);
208                 return (0);
209         }
210
211         /*
212          * This is a leaf (file or disk) vdev.  In either case, go through
213          * the name list and see if we find a matching guid.  If so, replace
214          * the path and see if we can calculate a new devid.
215          *
216          * There may be multiple names associated with a particular guid, in
217          * which case we have overlapping partitions or multiple paths to the
218          * same disk.  In this case we prefer to use the path name which
219          * matches the ZPOOL_CONFIG_PATH.  If no matching entry is found we
220          * use the lowest order device which corresponds to the first match
221          * while traversing the ZPOOL_IMPORT_PATH search path.
222          */
223         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
224         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
225                 path = NULL;
226
227         best = NULL;
228         for (ne = names; ne != NULL; ne = ne->ne_next) {
229                 if (ne->ne_guid == guid) {
230                         if (path == NULL) {
231                                 best = ne;
232                                 break;
233                         }
234
235                         if ((strlen(path) == strlen(ne->ne_name)) &&
236                             strncmp(path, ne->ne_name, strlen(path)) == 0) {
237                                 best = ne;
238                                 break;
239                         }
240
241                         if (best == NULL) {
242                                 best = ne;
243                                 continue;
244                         }
245
246                         /* Prefer paths with move vdev labels. */
247                         if (ne->ne_num_labels > best->ne_num_labels) {
248                                 best = ne;
249                                 continue;
250                         }
251
252                         /* Prefer paths earlier in the search order. */
253                         if (ne->ne_num_labels == best->ne_num_labels &&
254                             ne->ne_order < best->ne_order) {
255                                 best = ne;
256                                 continue;
257                         }
258                 }
259         }
260
261         if (best == NULL)
262                 return (0);
263
264         if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
265                 return (-1);
266
267         update_vdev_config_dev_strs(nv);
268
269         return (0);
270 }
271
272 /*
273  * Add the given configuration to the list of known devices.
274  */
275 static int
276 add_config(libpc_handle_t *hdl, pool_list_t *pl, const char *path,
277     int order, int num_labels, nvlist_t *config)
278 {
279         uint64_t pool_guid, vdev_guid, top_guid, txg, state;
280         pool_entry_t *pe;
281         vdev_entry_t *ve;
282         config_entry_t *ce;
283         name_entry_t *ne;
284
285         /*
286          * If this is a hot spare not currently in use or level 2 cache
287          * device, add it to the list of names to translate, but don't do
288          * anything else.
289          */
290         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
291             &state) == 0 &&
292             (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
293             nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
294                 if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
295                         return (-1);
296
297                 if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
298                         free(ne);
299                         return (-1);
300                 }
301                 ne->ne_guid = vdev_guid;
302                 ne->ne_order = order;
303                 ne->ne_num_labels = num_labels;
304                 ne->ne_next = pl->names;
305                 pl->names = ne;
306
307                 return (0);
308         }
309
310         /*
311          * If we have a valid config but cannot read any of these fields, then
312          * it means we have a half-initialized label.  In vdev_label_init()
313          * we write a label with txg == 0 so that we can identify the device
314          * in case the user refers to the same disk later on.  If we fail to
315          * create the pool, we'll be left with a label in this state
316          * which should not be considered part of a valid pool.
317          */
318         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
319             &pool_guid) != 0 ||
320             nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
321             &vdev_guid) != 0 ||
322             nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
323             &top_guid) != 0 ||
324             nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
325             &txg) != 0 || txg == 0) {
326                 return (0);
327         }
328
329         /*
330          * First, see if we know about this pool.  If not, then add it to the
331          * list of known pools.
332          */
333         for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
334                 if (pe->pe_guid == pool_guid)
335                         break;
336         }
337
338         if (pe == NULL) {
339                 if ((pe = zutil_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
340                         return (-1);
341                 }
342                 pe->pe_guid = pool_guid;
343                 pe->pe_next = pl->pools;
344                 pl->pools = pe;
345         }
346
347         /*
348          * Second, see if we know about this toplevel vdev.  Add it if its
349          * missing.
350          */
351         for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
352                 if (ve->ve_guid == top_guid)
353                         break;
354         }
355
356         if (ve == NULL) {
357                 if ((ve = zutil_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
358                         return (-1);
359                 }
360                 ve->ve_guid = top_guid;
361                 ve->ve_next = pe->pe_vdevs;
362                 pe->pe_vdevs = ve;
363         }
364
365         /*
366          * Third, see if we have a config with a matching transaction group.  If
367          * so, then we do nothing.  Otherwise, add it to the list of known
368          * configs.
369          */
370         for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
371                 if (ce->ce_txg == txg)
372                         break;
373         }
374
375         if (ce == NULL) {
376                 if ((ce = zutil_alloc(hdl, sizeof (config_entry_t))) == NULL) {
377                         return (-1);
378                 }
379                 ce->ce_txg = txg;
380                 ce->ce_config = fnvlist_dup(config);
381                 ce->ce_next = ve->ve_configs;
382                 ve->ve_configs = ce;
383         }
384
385         /*
386          * At this point we've successfully added our config to the list of
387          * known configs.  The last thing to do is add the vdev guid -> path
388          * mappings so that we can fix up the configuration as necessary before
389          * doing the import.
390          */
391         if ((ne = zutil_alloc(hdl, sizeof (name_entry_t))) == NULL)
392                 return (-1);
393
394         if ((ne->ne_name = zutil_strdup(hdl, path)) == NULL) {
395                 free(ne);
396                 return (-1);
397         }
398
399         ne->ne_guid = vdev_guid;
400         ne->ne_order = order;
401         ne->ne_num_labels = num_labels;
402         ne->ne_next = pl->names;
403         pl->names = ne;
404
405         return (0);
406 }
407
408 static int
409 zutil_pool_active(libpc_handle_t *hdl, const char *name, uint64_t guid,
410     boolean_t *isactive)
411 {
412         ASSERT(hdl->lpc_ops->pco_pool_active != NULL);
413
414         int error = hdl->lpc_ops->pco_pool_active(hdl->lpc_lib_handle, name,
415             guid, isactive);
416
417         return (error);
418 }
419
420 static nvlist_t *
421 zutil_refresh_config(libpc_handle_t *hdl, nvlist_t *tryconfig)
422 {
423         ASSERT(hdl->lpc_ops->pco_refresh_config != NULL);
424
425         return (hdl->lpc_ops->pco_refresh_config(hdl->lpc_lib_handle,
426             tryconfig));
427 }
428
429 /*
430  * Determine if the vdev id is a hole in the namespace.
431  */
432 static boolean_t
433 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
434 {
435         int c;
436
437         for (c = 0; c < holes; c++) {
438
439                 /* Top-level is a hole */
440                 if (hole_array[c] == id)
441                         return (B_TRUE);
442         }
443         return (B_FALSE);
444 }
445
446 /*
447  * Convert our list of pools into the definitive set of configurations.  We
448  * start by picking the best config for each toplevel vdev.  Once that's done,
449  * we assemble the toplevel vdevs into a full config for the pool.  We make a
450  * pass to fix up any incorrect paths, and then add it to the main list to
451  * return to the user.
452  */
453 static nvlist_t *
454 get_configs(libpc_handle_t *hdl, pool_list_t *pl, boolean_t active_ok,
455     nvlist_t *policy)
456 {
457         pool_entry_t *pe;
458         vdev_entry_t *ve;
459         config_entry_t *ce;
460         nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
461         nvlist_t **spares, **l2cache;
462         uint_t i, nspares, nl2cache;
463         boolean_t config_seen;
464         uint64_t best_txg;
465         char *name, *hostname = NULL;
466         uint64_t guid;
467         uint_t children = 0;
468         nvlist_t **child = NULL;
469         uint_t holes;
470         uint64_t *hole_array, max_id;
471         uint_t c;
472         boolean_t isactive;
473         uint64_t hostid;
474         nvlist_t *nvl;
475         boolean_t valid_top_config = B_FALSE;
476
477         if (nvlist_alloc(&ret, 0, 0) != 0)
478                 goto nomem;
479
480         for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
481                 uint64_t id, max_txg = 0;
482
483                 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
484                         goto nomem;
485                 config_seen = B_FALSE;
486
487                 /*
488                  * Iterate over all toplevel vdevs.  Grab the pool configuration
489                  * from the first one we find, and then go through the rest and
490                  * add them as necessary to the 'vdevs' member of the config.
491                  */
492                 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
493
494                         /*
495                          * Determine the best configuration for this vdev by
496                          * selecting the config with the latest transaction
497                          * group.
498                          */
499                         best_txg = 0;
500                         for (ce = ve->ve_configs; ce != NULL;
501                             ce = ce->ce_next) {
502
503                                 if (ce->ce_txg > best_txg) {
504                                         tmp = ce->ce_config;
505                                         best_txg = ce->ce_txg;
506                                 }
507                         }
508
509                         /*
510                          * We rely on the fact that the max txg for the
511                          * pool will contain the most up-to-date information
512                          * about the valid top-levels in the vdev namespace.
513                          */
514                         if (best_txg > max_txg) {
515                                 (void) nvlist_remove(config,
516                                     ZPOOL_CONFIG_VDEV_CHILDREN,
517                                     DATA_TYPE_UINT64);
518                                 (void) nvlist_remove(config,
519                                     ZPOOL_CONFIG_HOLE_ARRAY,
520                                     DATA_TYPE_UINT64_ARRAY);
521
522                                 max_txg = best_txg;
523                                 hole_array = NULL;
524                                 holes = 0;
525                                 max_id = 0;
526                                 valid_top_config = B_FALSE;
527
528                                 if (nvlist_lookup_uint64(tmp,
529                                     ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
530                                         verify(nvlist_add_uint64(config,
531                                             ZPOOL_CONFIG_VDEV_CHILDREN,
532                                             max_id) == 0);
533                                         valid_top_config = B_TRUE;
534                                 }
535
536                                 if (nvlist_lookup_uint64_array(tmp,
537                                     ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
538                                     &holes) == 0) {
539                                         verify(nvlist_add_uint64_array(config,
540                                             ZPOOL_CONFIG_HOLE_ARRAY,
541                                             hole_array, holes) == 0);
542                                 }
543                         }
544
545                         if (!config_seen) {
546                                 /*
547                                  * Copy the relevant pieces of data to the pool
548                                  * configuration:
549                                  *
550                                  *      version
551                                  *      pool guid
552                                  *      name
553                                  *      comment (if available)
554                                  *      pool state
555                                  *      hostid (if available)
556                                  *      hostname (if available)
557                                  */
558                                 uint64_t state, version;
559                                 char *comment = NULL;
560
561                                 version = fnvlist_lookup_uint64(tmp,
562                                     ZPOOL_CONFIG_VERSION);
563                                 fnvlist_add_uint64(config,
564                                     ZPOOL_CONFIG_VERSION, version);
565                                 guid = fnvlist_lookup_uint64(tmp,
566                                     ZPOOL_CONFIG_POOL_GUID);
567                                 fnvlist_add_uint64(config,
568                                     ZPOOL_CONFIG_POOL_GUID, guid);
569                                 name = fnvlist_lookup_string(tmp,
570                                     ZPOOL_CONFIG_POOL_NAME);
571                                 fnvlist_add_string(config,
572                                     ZPOOL_CONFIG_POOL_NAME, name);
573
574                                 if (nvlist_lookup_string(tmp,
575                                     ZPOOL_CONFIG_COMMENT, &comment) == 0)
576                                         fnvlist_add_string(config,
577                                             ZPOOL_CONFIG_COMMENT, comment);
578
579                                 state = fnvlist_lookup_uint64(tmp,
580                                     ZPOOL_CONFIG_POOL_STATE);
581                                 fnvlist_add_uint64(config,
582                                     ZPOOL_CONFIG_POOL_STATE, state);
583
584                                 hostid = 0;
585                                 if (nvlist_lookup_uint64(tmp,
586                                     ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
587                                         fnvlist_add_uint64(config,
588                                             ZPOOL_CONFIG_HOSTID, hostid);
589                                         hostname = fnvlist_lookup_string(tmp,
590                                             ZPOOL_CONFIG_HOSTNAME);
591                                         fnvlist_add_string(config,
592                                             ZPOOL_CONFIG_HOSTNAME, hostname);
593                                 }
594
595                                 config_seen = B_TRUE;
596                         }
597
598                         /*
599                          * Add this top-level vdev to the child array.
600                          */
601                         verify(nvlist_lookup_nvlist(tmp,
602                             ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
603                         verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
604                             &id) == 0);
605
606                         if (id >= children) {
607                                 nvlist_t **newchild;
608
609                                 newchild = zutil_alloc(hdl, (id + 1) *
610                                     sizeof (nvlist_t *));
611                                 if (newchild == NULL)
612                                         goto nomem;
613
614                                 for (c = 0; c < children; c++)
615                                         newchild[c] = child[c];
616
617                                 free(child);
618                                 child = newchild;
619                                 children = id + 1;
620                         }
621                         if (nvlist_dup(nvtop, &child[id], 0) != 0)
622                                 goto nomem;
623
624                 }
625
626                 /*
627                  * If we have information about all the top-levels then
628                  * clean up the nvlist which we've constructed. This
629                  * means removing any extraneous devices that are
630                  * beyond the valid range or adding devices to the end
631                  * of our array which appear to be missing.
632                  */
633                 if (valid_top_config) {
634                         if (max_id < children) {
635                                 for (c = max_id; c < children; c++)
636                                         nvlist_free(child[c]);
637                                 children = max_id;
638                         } else if (max_id > children) {
639                                 nvlist_t **newchild;
640
641                                 newchild = zutil_alloc(hdl, (max_id) *
642                                     sizeof (nvlist_t *));
643                                 if (newchild == NULL)
644                                         goto nomem;
645
646                                 for (c = 0; c < children; c++)
647                                         newchild[c] = child[c];
648
649                                 free(child);
650                                 child = newchild;
651                                 children = max_id;
652                         }
653                 }
654
655                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
656                     &guid) == 0);
657
658                 /*
659                  * The vdev namespace may contain holes as a result of
660                  * device removal. We must add them back into the vdev
661                  * tree before we process any missing devices.
662                  */
663                 if (holes > 0) {
664                         ASSERT(valid_top_config);
665
666                         for (c = 0; c < children; c++) {
667                                 nvlist_t *holey;
668
669                                 if (child[c] != NULL ||
670                                     !vdev_is_hole(hole_array, holes, c))
671                                         continue;
672
673                                 if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
674                                     0) != 0)
675                                         goto nomem;
676
677                                 /*
678                                  * Holes in the namespace are treated as
679                                  * "hole" top-level vdevs and have a
680                                  * special flag set on them.
681                                  */
682                                 if (nvlist_add_string(holey,
683                                     ZPOOL_CONFIG_TYPE,
684                                     VDEV_TYPE_HOLE) != 0 ||
685                                     nvlist_add_uint64(holey,
686                                     ZPOOL_CONFIG_ID, c) != 0 ||
687                                     nvlist_add_uint64(holey,
688                                     ZPOOL_CONFIG_GUID, 0ULL) != 0) {
689                                         nvlist_free(holey);
690                                         goto nomem;
691                                 }
692                                 child[c] = holey;
693                         }
694                 }
695
696                 /*
697                  * Look for any missing top-level vdevs.  If this is the case,
698                  * create a faked up 'missing' vdev as a placeholder.  We cannot
699                  * simply compress the child array, because the kernel performs
700                  * certain checks to make sure the vdev IDs match their location
701                  * in the configuration.
702                  */
703                 for (c = 0; c < children; c++) {
704                         if (child[c] == NULL) {
705                                 nvlist_t *missing;
706                                 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
707                                     0) != 0)
708                                         goto nomem;
709                                 if (nvlist_add_string(missing,
710                                     ZPOOL_CONFIG_TYPE,
711                                     VDEV_TYPE_MISSING) != 0 ||
712                                     nvlist_add_uint64(missing,
713                                     ZPOOL_CONFIG_ID, c) != 0 ||
714                                     nvlist_add_uint64(missing,
715                                     ZPOOL_CONFIG_GUID, 0ULL) != 0) {
716                                         nvlist_free(missing);
717                                         goto nomem;
718                                 }
719                                 child[c] = missing;
720                         }
721                 }
722
723                 /*
724                  * Put all of this pool's top-level vdevs into a root vdev.
725                  */
726                 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
727                         goto nomem;
728                 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
729                     VDEV_TYPE_ROOT) != 0 ||
730                     nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
731                     nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
732                     nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
733                     child, children) != 0) {
734                         nvlist_free(nvroot);
735                         goto nomem;
736                 }
737
738                 for (c = 0; c < children; c++)
739                         nvlist_free(child[c]);
740                 free(child);
741                 children = 0;
742                 child = NULL;
743
744                 /*
745                  * Go through and fix up any paths and/or devids based on our
746                  * known list of vdev GUID -> path mappings.
747                  */
748                 if (fix_paths(hdl, nvroot, pl->names) != 0) {
749                         nvlist_free(nvroot);
750                         goto nomem;
751                 }
752
753                 /*
754                  * Add the root vdev to this pool's configuration.
755                  */
756                 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
757                     nvroot) != 0) {
758                         nvlist_free(nvroot);
759                         goto nomem;
760                 }
761                 nvlist_free(nvroot);
762
763                 /*
764                  * zdb uses this path to report on active pools that were
765                  * imported or created using -R.
766                  */
767                 if (active_ok)
768                         goto add_pool;
769
770                 /*
771                  * Determine if this pool is currently active, in which case we
772                  * can't actually import it.
773                  */
774                 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
775                     &name) == 0);
776                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
777                     &guid) == 0);
778
779                 if (zutil_pool_active(hdl, name, guid, &isactive) != 0)
780                         goto error;
781
782                 if (isactive) {
783                         nvlist_free(config);
784                         config = NULL;
785                         continue;
786                 }
787
788                 if (policy != NULL) {
789                         if (nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
790                             policy) != 0)
791                                 goto nomem;
792                 }
793
794                 if ((nvl = zutil_refresh_config(hdl, config)) == NULL) {
795                         nvlist_free(config);
796                         config = NULL;
797                         continue;
798                 }
799
800                 nvlist_free(config);
801                 config = nvl;
802
803                 /*
804                  * Go through and update the paths for spares, now that we have
805                  * them.
806                  */
807                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
808                     &nvroot) == 0);
809                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
810                     &spares, &nspares) == 0) {
811                         for (i = 0; i < nspares; i++) {
812                                 if (fix_paths(hdl, spares[i], pl->names) != 0)
813                                         goto nomem;
814                         }
815                 }
816
817                 /*
818                  * Update the paths for l2cache devices.
819                  */
820                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
821                     &l2cache, &nl2cache) == 0) {
822                         for (i = 0; i < nl2cache; i++) {
823                                 if (fix_paths(hdl, l2cache[i], pl->names) != 0)
824                                         goto nomem;
825                         }
826                 }
827
828                 /*
829                  * Restore the original information read from the actual label.
830                  */
831                 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
832                     DATA_TYPE_UINT64);
833                 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
834                     DATA_TYPE_STRING);
835                 if (hostid != 0) {
836                         verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
837                             hostid) == 0);
838                         verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
839                             hostname) == 0);
840                 }
841
842 add_pool:
843                 /*
844                  * Add this pool to the list of configs.
845                  */
846                 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
847                     &name) == 0);
848
849                 if (nvlist_add_nvlist(ret, name, config) != 0)
850                         goto nomem;
851
852                 nvlist_free(config);
853                 config = NULL;
854         }
855
856         return (ret);
857
858 nomem:
859         (void) zutil_no_memory(hdl);
860 error:
861         nvlist_free(config);
862         nvlist_free(ret);
863         for (c = 0; c < children; c++)
864                 nvlist_free(child[c]);
865         free(child);
866
867         return (NULL);
868 }
869
870 /*
871  * Return the offset of the given label.
872  */
873 static uint64_t
874 label_offset(uint64_t size, int l)
875 {
876         ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
877         return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
878             0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
879 }
880
881 /*
882  * Given a file descriptor, read the label information and return an nvlist
883  * describing the configuration, if there is one.  The number of valid
884  * labels found will be returned in num_labels when non-NULL.
885  */
886 int
887 zpool_read_label(int fd, nvlist_t **config, int *num_labels)
888 {
889         struct stat64 statbuf;
890         int l, count = 0;
891         vdev_label_t *label;
892         nvlist_t *expected_config = NULL;
893         uint64_t expected_guid = 0, size;
894         int error;
895
896         *config = NULL;
897
898         if (fstat64_blk(fd, &statbuf) == -1)
899                 return (0);
900         size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
901
902         error = posix_memalign((void **)&label, PAGESIZE, sizeof (*label));
903         if (error)
904                 return (-1);
905
906         for (l = 0; l < VDEV_LABELS; l++) {
907                 uint64_t state, guid, txg;
908
909                 if (pread64(fd, label, sizeof (vdev_label_t),
910                     label_offset(size, l)) != sizeof (vdev_label_t))
911                         continue;
912
913                 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
914                     sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
915                         continue;
916
917                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
918                     &guid) != 0 || guid == 0) {
919                         nvlist_free(*config);
920                         continue;
921                 }
922
923                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
924                     &state) != 0 || state > POOL_STATE_L2CACHE) {
925                         nvlist_free(*config);
926                         continue;
927                 }
928
929                 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
930                     (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
931                     &txg) != 0 || txg == 0)) {
932                         nvlist_free(*config);
933                         continue;
934                 }
935
936                 if (expected_guid) {
937                         if (expected_guid == guid)
938                                 count++;
939
940                         nvlist_free(*config);
941                 } else {
942                         expected_config = *config;
943                         expected_guid = guid;
944                         count++;
945                 }
946         }
947
948         if (num_labels != NULL)
949                 *num_labels = count;
950
951         free(label);
952         *config = expected_config;
953
954         return (0);
955 }
956
957 /*
958  * Sorted by full path and then vdev guid to allow for multiple entries with
959  * the same full path name.  This is required because it's possible to
960  * have multiple block devices with labels that refer to the same
961  * ZPOOL_CONFIG_PATH yet have different vdev guids.  In this case both
962  * entries need to be added to the cache.  Scenarios where this can occur
963  * include overwritten pool labels, devices which are visible from multiple
964  * hosts and multipath devices.
965  */
966 int
967 slice_cache_compare(const void *arg1, const void *arg2)
968 {
969         const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
970         const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
971         uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
972         uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
973         int rv;
974
975         rv = TREE_ISIGN(strcmp(nm1, nm2));
976         if (rv)
977                 return (rv);
978
979         return (TREE_CMP(guid1, guid2));
980 }
981
982 static int
983 label_paths_impl(libpc_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
984     uint64_t vdev_guid, char **path, char **devid)
985 {
986         nvlist_t **child;
987         uint_t c, children;
988         uint64_t guid;
989         char *val;
990         int error;
991
992         if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
993             &child, &children) == 0) {
994                 for (c = 0; c < children; c++) {
995                         error  = label_paths_impl(hdl, child[c],
996                             pool_guid, vdev_guid, path, devid);
997                         if (error)
998                                 return (error);
999                 }
1000                 return (0);
1001         }
1002
1003         if (nvroot == NULL)
1004                 return (0);
1005
1006         error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1007         if ((error != 0) || (guid != vdev_guid))
1008                 return (0);
1009
1010         error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1011         if (error == 0)
1012                 *path = val;
1013
1014         error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1015         if (error == 0)
1016                 *devid = val;
1017
1018         return (0);
1019 }
1020
1021 /*
1022  * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1023  * and store these strings as config_path and devid_path respectively.
1024  * The returned pointers are only valid as long as label remains valid.
1025  */
1026 int
1027 label_paths(libpc_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1028 {
1029         nvlist_t *nvroot;
1030         uint64_t pool_guid;
1031         uint64_t vdev_guid;
1032
1033         *path = NULL;
1034         *devid = NULL;
1035
1036         if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1037             nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1038             nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1039                 return (ENOENT);
1040
1041         return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1042             devid));
1043 }
1044
1045 static void
1046 zpool_find_import_scan_add_slice(libpc_handle_t *hdl, pthread_mutex_t *lock,
1047     avl_tree_t *cache, const char *path, const char *name, int order)
1048 {
1049         avl_index_t where;
1050         rdsk_node_t *slice;
1051
1052         slice = zutil_alloc(hdl, sizeof (rdsk_node_t));
1053         if (asprintf(&slice->rn_name, "%s/%s", path, name) == -1) {
1054                 free(slice);
1055                 return;
1056         }
1057         slice->rn_vdev_guid = 0;
1058         slice->rn_lock = lock;
1059         slice->rn_avl = cache;
1060         slice->rn_hdl = hdl;
1061         slice->rn_order = order + IMPORT_ORDER_SCAN_OFFSET;
1062         slice->rn_labelpaths = B_FALSE;
1063
1064         pthread_mutex_lock(lock);
1065         if (avl_find(cache, slice, &where)) {
1066                 free(slice->rn_name);
1067                 free(slice);
1068         } else {
1069                 avl_insert(cache, slice, where);
1070         }
1071         pthread_mutex_unlock(lock);
1072 }
1073
1074 static int
1075 zpool_find_import_scan_dir(libpc_handle_t *hdl, pthread_mutex_t *lock,
1076     avl_tree_t *cache, const char *dir, int order)
1077 {
1078         int error;
1079         char path[MAXPATHLEN];
1080         struct dirent64 *dp;
1081         DIR *dirp;
1082
1083         if (realpath(dir, path) == NULL) {
1084                 error = errno;
1085                 if (error == ENOENT)
1086                         return (0);
1087
1088                 zutil_error_aux(hdl, strerror(error));
1089                 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1090                     TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1091                 return (error);
1092         }
1093
1094         dirp = opendir(path);
1095         if (dirp == NULL) {
1096                 error = errno;
1097                 zutil_error_aux(hdl, strerror(error));
1098                 (void) zutil_error_fmt(hdl, EZFS_BADPATH,
1099                     dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
1100                 return (error);
1101         }
1102
1103         while ((dp = readdir64(dirp)) != NULL) {
1104                 const char *name = dp->d_name;
1105                 if (name[0] == '.' &&
1106                     (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1107                         continue;
1108
1109                 zpool_find_import_scan_add_slice(hdl, lock, cache, path, name,
1110                     order);
1111         }
1112
1113         (void) closedir(dirp);
1114         return (0);
1115 }
1116
1117 static int
1118 zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
1119     avl_tree_t *cache, const char *dir, int order)
1120 {
1121         int error = 0;
1122         char path[MAXPATHLEN];
1123         char *d, *b;
1124         char *dpath, *name;
1125
1126         /*
1127          * Separate the directory part and last part of the
1128          * path. We do this so that we can get the realpath of
1129          * the directory. We don't get the realpath on the
1130          * whole path because if it's a symlink, we want the
1131          * path of the symlink not where it points to.
1132          */
1133         d = zutil_strdup(hdl, dir);
1134         b = zutil_strdup(hdl, dir);
1135         dpath = dirname(d);
1136         name = basename(b);
1137
1138         if (realpath(dpath, path) == NULL) {
1139                 error = errno;
1140                 if (error == ENOENT) {
1141                         error = 0;
1142                         goto out;
1143                 }
1144
1145                 zutil_error_aux(hdl, strerror(error));
1146                 (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1147                     TEXT_DOMAIN, "cannot resolve path '%s'"), dir);
1148                 goto out;
1149         }
1150
1151         zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
1152
1153 out:
1154         free(b);
1155         free(d);
1156         return (error);
1157 }
1158
1159 /*
1160  * Scan a list of directories for zfs devices.
1161  */
1162 static int
1163 zpool_find_import_scan(libpc_handle_t *hdl, pthread_mutex_t *lock,
1164     avl_tree_t **slice_cache, const char * const *dir, size_t dirs)
1165 {
1166         avl_tree_t *cache;
1167         rdsk_node_t *slice;
1168         void *cookie;
1169         int i, error;
1170
1171         *slice_cache = NULL;
1172         cache = zutil_alloc(hdl, sizeof (avl_tree_t));
1173         avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1174             offsetof(rdsk_node_t, rn_node));
1175
1176         for (i = 0; i < dirs; i++) {
1177                 struct stat sbuf;
1178
1179                 if (stat(dir[i], &sbuf) != 0) {
1180                         error = errno;
1181                         if (error == ENOENT)
1182                                 continue;
1183
1184                         zutil_error_aux(hdl, strerror(error));
1185                         (void) zutil_error_fmt(hdl, EZFS_BADPATH, dgettext(
1186                             TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1187                         goto error;
1188                 }
1189
1190                 /*
1191                  * If dir[i] is a directory, we walk through it and add all
1192                  * the entries to the cache. If it's not a directory, we just
1193                  * add it to the cache.
1194                  */
1195                 if (S_ISDIR(sbuf.st_mode)) {
1196                         if ((error = zpool_find_import_scan_dir(hdl, lock,
1197                             cache, dir[i], i)) != 0)
1198                                 goto error;
1199                 } else {
1200                         if ((error = zpool_find_import_scan_path(hdl, lock,
1201                             cache, dir[i], i)) != 0)
1202                                 goto error;
1203                 }
1204         }
1205
1206         *slice_cache = cache;
1207         return (0);
1208
1209 error:
1210         cookie = NULL;
1211         while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1212                 free(slice->rn_name);
1213                 free(slice);
1214         }
1215         free(cache);
1216
1217         return (error);
1218 }
1219
1220 /*
1221  * Given a list of directories to search, find all pools stored on disk.  This
1222  * includes partial pools which are not available to import.  If no args are
1223  * given (argc is 0), then the default directory (/dev/dsk) is searched.
1224  * poolname or guid (but not both) are provided by the caller when trying
1225  * to import a specific pool.
1226  */
1227 static nvlist_t *
1228 zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg)
1229 {
1230         nvlist_t *ret = NULL;
1231         pool_list_t pools = { 0 };
1232         pool_entry_t *pe, *penext;
1233         vdev_entry_t *ve, *venext;
1234         config_entry_t *ce, *cenext;
1235         name_entry_t *ne, *nenext;
1236         pthread_mutex_t lock;
1237         avl_tree_t *cache;
1238         rdsk_node_t *slice;
1239         void *cookie;
1240         tpool_t *t;
1241
1242         verify(iarg->poolname == NULL || iarg->guid == 0);
1243         pthread_mutex_init(&lock, NULL);
1244
1245         /*
1246          * Locate pool member vdevs by blkid or by directory scanning.
1247          * On success a newly allocated AVL tree which is populated with an
1248          * entry for each discovered vdev will be returned in the cache.
1249          * It's the caller's responsibility to consume and destroy this tree.
1250          */
1251         if (iarg->scan || iarg->paths != 0) {
1252                 size_t dirs = iarg->paths;
1253                 const char * const *dir = (const char * const *)iarg->path;
1254
1255                 if (dirs == 0)
1256                         dir = zpool_default_search_paths(&dirs);
1257
1258                 if (zpool_find_import_scan(hdl, &lock, &cache, dir, dirs) != 0)
1259                         return (NULL);
1260         } else {
1261                 if (zpool_find_import_blkid(hdl, &lock, &cache) != 0)
1262                         return (NULL);
1263         }
1264
1265         /*
1266          * Create a thread pool to parallelize the process of reading and
1267          * validating labels, a large number of threads can be used due to
1268          * minimal contention.
1269          */
1270         t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
1271         for (slice = avl_first(cache); slice;
1272             (slice = avl_walk(cache, slice, AVL_AFTER)))
1273                 (void) tpool_dispatch(t, zpool_open_func, slice);
1274
1275         tpool_wait(t);
1276         tpool_destroy(t);
1277
1278         /*
1279          * Process the cache, filtering out any entries which are not
1280          * for the specified pool then adding matching label configs.
1281          */
1282         cookie = NULL;
1283         while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1284                 if (slice->rn_config != NULL) {
1285                         nvlist_t *config = slice->rn_config;
1286                         boolean_t matched = B_TRUE;
1287                         boolean_t aux = B_FALSE;
1288                         int fd;
1289
1290                         /*
1291                          * Check if it's a spare or l2cache device. If it is,
1292                          * we need to skip the name and guid check since they
1293                          * don't exist on aux device label.
1294                          */
1295                         if (iarg->poolname != NULL || iarg->guid != 0) {
1296                                 uint64_t state;
1297                                 aux = nvlist_lookup_uint64(config,
1298                                     ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1299                                     (state == POOL_STATE_SPARE ||
1300                                     state == POOL_STATE_L2CACHE);
1301                         }
1302
1303                         if (iarg->poolname != NULL && !aux) {
1304                                 char *pname;
1305
1306                                 matched = nvlist_lookup_string(config,
1307                                     ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1308                                     strcmp(iarg->poolname, pname) == 0;
1309                         } else if (iarg->guid != 0 && !aux) {
1310                                 uint64_t this_guid;
1311
1312                                 matched = nvlist_lookup_uint64(config,
1313                                     ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1314                                     iarg->guid == this_guid;
1315                         }
1316                         if (matched) {
1317                                 /*
1318                                  * Verify all remaining entries can be opened
1319                                  * exclusively. This will prune all underlying
1320                                  * multipath devices which otherwise could
1321                                  * result in the vdev appearing as UNAVAIL.
1322                                  *
1323                                  * Under zdb, this step isn't required and
1324                                  * would prevent a zdb -e of active pools with
1325                                  * no cachefile.
1326                                  */
1327                                 fd = open(slice->rn_name, O_RDONLY | O_EXCL);
1328                                 if (fd >= 0 || iarg->can_be_active) {
1329                                         if (fd >= 0)
1330                                                 close(fd);
1331                                         add_config(hdl, &pools,
1332                                             slice->rn_name, slice->rn_order,
1333                                             slice->rn_num_labels, config);
1334                                 }
1335                         }
1336                         nvlist_free(config);
1337                 }
1338                 free(slice->rn_name);
1339                 free(slice);
1340         }
1341         avl_destroy(cache);
1342         free(cache);
1343         pthread_mutex_destroy(&lock);
1344
1345         ret = get_configs(hdl, &pools, iarg->can_be_active, iarg->policy);
1346
1347         for (pe = pools.pools; pe != NULL; pe = penext) {
1348                 penext = pe->pe_next;
1349                 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1350                         venext = ve->ve_next;
1351                         for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1352                                 cenext = ce->ce_next;
1353                                 nvlist_free(ce->ce_config);
1354                                 free(ce);
1355                         }
1356                         free(ve);
1357                 }
1358                 free(pe);
1359         }
1360
1361         for (ne = pools.names; ne != NULL; ne = nenext) {
1362                 nenext = ne->ne_next;
1363                 free(ne->ne_name);
1364                 free(ne);
1365         }
1366
1367         return (ret);
1368 }
1369
1370 /*
1371  * Given a cache file, return the contents as a list of importable pools.
1372  * poolname or guid (but not both) are provided by the caller when trying
1373  * to import a specific pool.
1374  */
1375 static nvlist_t *
1376 zpool_find_import_cached(libpc_handle_t *hdl, const char *cachefile,
1377     const char *poolname, uint64_t guid)
1378 {
1379         char *buf;
1380         int fd;
1381         struct stat64 statbuf;
1382         nvlist_t *raw, *src, *dst;
1383         nvlist_t *pools;
1384         nvpair_t *elem;
1385         char *name;
1386         uint64_t this_guid;
1387         boolean_t active;
1388
1389         verify(poolname == NULL || guid == 0);
1390
1391         if ((fd = open(cachefile, O_RDONLY)) < 0) {
1392                 zutil_error_aux(hdl, "%s", strerror(errno));
1393                 (void) zutil_error(hdl, EZFS_BADCACHE,
1394                     dgettext(TEXT_DOMAIN, "failed to open cache file"));
1395                 return (NULL);
1396         }
1397
1398         if (fstat64(fd, &statbuf) != 0) {
1399                 zutil_error_aux(hdl, "%s", strerror(errno));
1400                 (void) close(fd);
1401                 (void) zutil_error(hdl, EZFS_BADCACHE,
1402                     dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
1403                 return (NULL);
1404         }
1405
1406         if ((buf = zutil_alloc(hdl, statbuf.st_size)) == NULL) {
1407                 (void) close(fd);
1408                 return (NULL);
1409         }
1410
1411         if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1412                 (void) close(fd);
1413                 free(buf);
1414                 (void) zutil_error(hdl, EZFS_BADCACHE,
1415                     dgettext(TEXT_DOMAIN,
1416                     "failed to read cache file contents"));
1417                 return (NULL);
1418         }
1419
1420         (void) close(fd);
1421
1422         if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1423                 free(buf);
1424                 (void) zutil_error(hdl, EZFS_BADCACHE,
1425                     dgettext(TEXT_DOMAIN,
1426                     "invalid or corrupt cache file contents"));
1427                 return (NULL);
1428         }
1429
1430         free(buf);
1431
1432         /*
1433          * Go through and get the current state of the pools and refresh their
1434          * state.
1435          */
1436         if (nvlist_alloc(&pools, 0, 0) != 0) {
1437                 (void) zutil_no_memory(hdl);
1438                 nvlist_free(raw);
1439                 return (NULL);
1440         }
1441
1442         elem = NULL;
1443         while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1444                 src = fnvpair_value_nvlist(elem);
1445
1446                 name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
1447                 if (poolname != NULL && strcmp(poolname, name) != 0)
1448                         continue;
1449
1450                 this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1451                 if (guid != 0 && guid != this_guid)
1452                         continue;
1453
1454                 if (zutil_pool_active(hdl, name, this_guid, &active) != 0) {
1455                         nvlist_free(raw);
1456                         nvlist_free(pools);
1457                         return (NULL);
1458                 }
1459
1460                 if (active)
1461                         continue;
1462
1463                 if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE,
1464                     cachefile) != 0) {
1465                         (void) zutil_no_memory(hdl);
1466                         nvlist_free(raw);
1467                         nvlist_free(pools);
1468                         return (NULL);
1469                 }
1470
1471                 if ((dst = zutil_refresh_config(hdl, src)) == NULL) {
1472                         nvlist_free(raw);
1473                         nvlist_free(pools);
1474                         return (NULL);
1475                 }
1476
1477                 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1478                         (void) zutil_no_memory(hdl);
1479                         nvlist_free(dst);
1480                         nvlist_free(raw);
1481                         nvlist_free(pools);
1482                         return (NULL);
1483                 }
1484                 nvlist_free(dst);
1485         }
1486
1487         nvlist_free(raw);
1488         return (pools);
1489 }
1490
1491 nvlist_t *
1492 zpool_search_import(void *hdl, importargs_t *import,
1493     const pool_config_ops_t *pco)
1494 {
1495         libpc_handle_t handle = { 0 };
1496         nvlist_t *pools = NULL;
1497
1498         handle.lpc_lib_handle = hdl;
1499         handle.lpc_ops = pco;
1500         handle.lpc_printerr = B_TRUE;
1501
1502         verify(import->poolname == NULL || import->guid == 0);
1503
1504         if (import->cachefile != NULL)
1505                 pools = zpool_find_import_cached(&handle, import->cachefile,
1506                     import->poolname, import->guid);
1507         else
1508                 pools = zpool_find_import_impl(&handle, import);
1509
1510         if ((pools == NULL || nvlist_empty(pools)) &&
1511             handle.lpc_open_access_error && geteuid() != 0) {
1512                 (void) zutil_error(&handle, EZFS_EACESS, dgettext(TEXT_DOMAIN,
1513                     "no pools found"));
1514         }
1515
1516         return (pools);
1517 }
1518
1519 static boolean_t
1520 pool_match(nvlist_t *cfg, char *tgt)
1521 {
1522         uint64_t v, guid = strtoull(tgt, NULL, 0);
1523         char *s;
1524
1525         if (guid != 0) {
1526                 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
1527                         return (v == guid);
1528         } else {
1529                 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
1530                         return (strcmp(s, tgt) == 0);
1531         }
1532         return (B_FALSE);
1533 }
1534
1535 int
1536 zpool_find_config(void *hdl, const char *target, nvlist_t **configp,
1537     importargs_t *args, const pool_config_ops_t *pco)
1538 {
1539         nvlist_t *pools;
1540         nvlist_t *match = NULL;
1541         nvlist_t *config = NULL;
1542         char *name = NULL, *sepp = NULL;
1543         char sep = '\0';
1544         int count = 0;
1545         char *targetdup = strdup(target);
1546
1547         *configp = NULL;
1548
1549         if ((sepp = strpbrk(targetdup, "/@")) != NULL) {
1550                 sep = *sepp;
1551                 *sepp = '\0';
1552         }
1553
1554         pools = zpool_search_import(hdl, args, pco);
1555
1556         if (pools != NULL) {
1557                 nvpair_t *elem = NULL;
1558                 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1559                         VERIFY0(nvpair_value_nvlist(elem, &config));
1560                         if (pool_match(config, targetdup)) {
1561                                 count++;
1562                                 if (match != NULL) {
1563                                         /* multiple matches found */
1564                                         continue;
1565                                 } else {
1566                                         match = config;
1567                                         name = nvpair_name(elem);
1568                                 }
1569                         }
1570                 }
1571         }
1572
1573         if (count == 0) {
1574                 free(targetdup);
1575                 return (ENOENT);
1576         }
1577
1578         if (count > 1) {
1579                 free(targetdup);
1580                 return (EINVAL);
1581         }
1582
1583         *configp = match;
1584         free(targetdup);
1585
1586         return (0);
1587 }