]> granicus.if.org Git - zfs/blob - lib/libzfs/libzfs_import.c
Fix function documentation to correctly mirror code
[zfs] / lib / libzfs / libzfs_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, 2016 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  * To import a pool, we rely on reading the configuration information from the
33  * ZFS label of each device.  If we successfully read the label, then we
34  * organize the configuration information in the following hierarchy:
35  *
36  *      pool guid -> toplevel vdev guid -> label txg
37  *
38  * Duplicate entries matching this same tuple will be discarded.  Once we have
39  * examined every device, we pick the best label txg config for each toplevel
40  * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
41  * update any paths that have changed.  Finally, we attempt to import the pool
42  * using our derived config, and record the results.
43  */
44
45 #include <ctype.h>
46 #include <devid.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <libintl.h>
50 #ifdef HAVE_LIBUDEV
51 #include <libudev.h>
52 #include <sched.h>
53 #endif
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/vtoc.h>
61 #include <sys/dktp/fdisk.h>
62 #include <sys/efi_partition.h>
63 #include <sys/vdev_impl.h>
64 #include <blkid/blkid.h>
65 #include "libzfs.h"
66 #include "libzfs_impl.h"
67 #include <libzfs.h>
68
69 /*
70  * Intermediate structures used to gather configuration information.
71  */
72 typedef struct config_entry {
73         uint64_t                ce_txg;
74         nvlist_t                *ce_config;
75         struct config_entry     *ce_next;
76 } config_entry_t;
77
78 typedef struct vdev_entry {
79         uint64_t                ve_guid;
80         config_entry_t          *ve_configs;
81         struct vdev_entry       *ve_next;
82 } vdev_entry_t;
83
84 typedef struct pool_entry {
85         uint64_t                pe_guid;
86         vdev_entry_t            *pe_vdevs;
87         struct pool_entry       *pe_next;
88 } pool_entry_t;
89
90 typedef struct name_entry {
91         char                    *ne_name;
92         uint64_t                ne_guid;
93         uint64_t                ne_order;
94         uint64_t                ne_num_labels;
95         struct name_entry       *ne_next;
96 } name_entry_t;
97
98 typedef struct pool_list {
99         pool_entry_t            *pools;
100         name_entry_t            *names;
101 } pool_list_t;
102
103 #define DEV_BYID_PATH   "/dev/disk/by-id/"
104
105 /*
106  * Linux persistent device strings for vdev labels
107  *
108  * based on libudev for consistency with libudev disk add/remove events
109  */
110 #ifdef HAVE_LIBUDEV
111
112 typedef struct vdev_dev_strs {
113         char    vds_devid[128];
114         char    vds_devphys[128];
115 } vdev_dev_strs_t;
116
117 /*
118  * Obtain the persistent device id string (describes what)
119  *
120  * used by ZED vdev matching for auto-{online,expand,replace}
121  */
122 int
123 zfs_device_get_devid(struct udev_device *dev, char *bufptr, size_t buflen)
124 {
125         struct udev_list_entry *entry;
126         const char *bus;
127         char devbyid[MAXPATHLEN];
128
129         /* The bus based by-id path is preferred */
130         bus = udev_device_get_property_value(dev, "ID_BUS");
131
132         if (bus == NULL) {
133                 const char *dm_uuid;
134
135                 /*
136                  * For multipath nodes use the persistent uuid based identifier
137                  *
138                  * Example: /dev/disk/by-id/dm-uuid-mpath-35000c5006304de3f
139                  */
140                 dm_uuid = udev_device_get_property_value(dev, "DM_UUID");
141                 if (dm_uuid != NULL) {
142                         (void) snprintf(bufptr, buflen, "dm-uuid-%s", dm_uuid);
143                         return (0);
144                 }
145                 return (ENODATA);
146         }
147
148         /*
149          * locate the bus specific by-id link
150          */
151         (void) snprintf(devbyid, sizeof (devbyid), "%s%s-", DEV_BYID_PATH, bus);
152         entry = udev_device_get_devlinks_list_entry(dev);
153         while (entry != NULL) {
154                 const char *name;
155
156                 name = udev_list_entry_get_name(entry);
157                 if (strncmp(name, devbyid, strlen(devbyid)) == 0) {
158                         name += strlen(DEV_BYID_PATH);
159                         (void) strlcpy(bufptr, name, buflen);
160                         return (0);
161                 }
162                 entry = udev_list_entry_get_next(entry);
163         }
164
165         return (ENODATA);
166 }
167
168 /*
169  * Obtain the persistent physical location string (describes where)
170  *
171  * used by ZED vdev matching for auto-{online,expand,replace}
172  */
173 int
174 zfs_device_get_physical(struct udev_device *dev, char *bufptr, size_t buflen)
175 {
176         const char *physpath = NULL;
177
178         /*
179          * Normal disks use ID_PATH for their physical path.  Device mapper
180          * devices are virtual and don't have a physical path.  For them we
181          * use ID_VDEV instead, which is setup via the /etc/vdev_id.conf file.
182          * ID_VDEV provides a persistent path to a virtual device.  If you
183          * don't have vdev_id.conf setup, you cannot use multipath autoreplace.
184          */
185         if (!((physpath = udev_device_get_property_value(dev, "ID_PATH")) &&
186             physpath[0])) {
187                 if (!((physpath =
188                     udev_device_get_property_value(dev, "ID_VDEV")) &&
189                     physpath[0])) {
190                         return (ENODATA);
191                 }
192         }
193
194         (void) strlcpy(bufptr, physpath, buflen);
195
196         return (0);
197 }
198
199 boolean_t
200 udev_is_mpath(struct udev_device *dev)
201 {
202         return udev_device_get_property_value(dev, "DM_UUID") &&
203             udev_device_get_property_value(dev, "MPATH_SBIN_PATH");
204 }
205
206 /*
207  * A disk is considered a multipath whole disk when:
208  *      DEVNAME key value has "dm-"
209  *      DM_NAME key value has "mpath" prefix
210  *      DM_UUID key exists
211  *      ID_PART_TABLE_TYPE key does not exist or is not gpt
212  */
213 static boolean_t
214 udev_mpath_whole_disk(struct udev_device *dev)
215 {
216         const char *devname, *type, *uuid;
217
218         devname = udev_device_get_property_value(dev, "DEVNAME");
219         type = udev_device_get_property_value(dev, "ID_PART_TABLE_TYPE");
220         uuid = udev_device_get_property_value(dev, "DM_UUID");
221
222         if ((devname != NULL && strncmp(devname, "/dev/dm-", 8) == 0) &&
223             ((type == NULL) || (strcmp(type, "gpt") != 0)) &&
224             (uuid != NULL)) {
225                 return (B_TRUE);
226         }
227
228         return (B_FALSE);
229 }
230
231 /*
232  * Check if a disk is effectively a multipath whole disk
233  */
234 boolean_t
235 is_mpath_whole_disk(const char *path)
236 {
237         struct udev *udev;
238         struct udev_device *dev = NULL;
239         char nodepath[MAXPATHLEN];
240         char *sysname;
241         boolean_t wholedisk = B_FALSE;
242
243         if (realpath(path, nodepath) == NULL)
244                 return (B_FALSE);
245         sysname = strrchr(nodepath, '/') + 1;
246         if (strncmp(sysname, "dm-", 3) != 0)
247                 return (B_FALSE);
248         if ((udev = udev_new()) == NULL)
249                 return (B_FALSE);
250         if ((dev = udev_device_new_from_subsystem_sysname(udev, "block",
251             sysname)) == NULL) {
252                 udev_device_unref(dev);
253                 return (B_FALSE);
254         }
255
256         wholedisk = udev_mpath_whole_disk(dev);
257
258         udev_device_unref(dev);
259         return (wholedisk);
260 }
261
262 static int
263 udev_device_is_ready(struct udev_device *dev)
264 {
265 #ifdef HAVE_LIBUDEV_UDEV_DEVICE_GET_IS_INITIALIZED
266         return (udev_device_get_is_initialized(dev));
267 #else
268         /* wait for DEVLINKS property to be initialized */
269         return (udev_device_get_property_value(dev, "DEVLINKS") != NULL);
270 #endif
271 }
272
273 /*
274  * Wait up to timeout_ms for udev to set up the device node.  The device is
275  * considered ready when libudev determines it has been initialized, all of
276  * the device links have been verified to exist, and it has been allowed to
277  * settle.  At this point the device the device can be accessed reliably.
278  * Depending on the complexity of the udev rules this process could take
279  * several seconds.
280  */
281 int
282 zpool_label_disk_wait(char *path, int timeout_ms)
283 {
284         struct udev *udev;
285         struct udev_device *dev = NULL;
286         char nodepath[MAXPATHLEN];
287         char *sysname = NULL;
288         int ret = ENODEV;
289         int settle_ms = 50;
290         long sleep_ms = 10;
291         hrtime_t start, settle;
292
293         if ((udev = udev_new()) == NULL)
294                 return (ENXIO);
295
296         start = gethrtime();
297         settle = 0;
298
299         do {
300                 if (sysname == NULL) {
301                         if (realpath(path, nodepath) != NULL) {
302                                 sysname = strrchr(nodepath, '/') + 1;
303                         } else {
304                                 (void) usleep(sleep_ms * MILLISEC);
305                                 continue;
306                         }
307                 }
308
309                 dev = udev_device_new_from_subsystem_sysname(udev,
310                     "block", sysname);
311                 if ((dev != NULL) && udev_device_is_ready(dev)) {
312                         struct udev_list_entry *links, *link;
313
314                         ret = 0;
315                         links = udev_device_get_devlinks_list_entry(dev);
316
317                         udev_list_entry_foreach(link, links) {
318                                 struct stat64 statbuf;
319                                 const char *name;
320
321                                 name = udev_list_entry_get_name(link);
322                                 errno = 0;
323                                 if (stat64(name, &statbuf) == 0 && errno == 0)
324                                         continue;
325
326                                 settle = 0;
327                                 ret = ENODEV;
328                                 break;
329                         }
330
331                         if (ret == 0) {
332                                 if (settle == 0) {
333                                         settle = gethrtime();
334                                 } else if (NSEC2MSEC(gethrtime() - settle) >=
335                                     settle_ms) {
336                                         udev_device_unref(dev);
337                                         break;
338                                 }
339                         }
340                 }
341
342                 udev_device_unref(dev);
343                 (void) usleep(sleep_ms * MILLISEC);
344
345         } while (NSEC2MSEC(gethrtime() - start) < timeout_ms);
346
347         udev_unref(udev);
348
349         return (ret);
350 }
351
352
353 /*
354  * Encode the persistent devices strings
355  * used for the vdev disk label
356  */
357 static int
358 encode_device_strings(const char *path, vdev_dev_strs_t *ds,
359     boolean_t wholedisk)
360 {
361         struct udev *udev;
362         struct udev_device *dev = NULL;
363         char nodepath[MAXPATHLEN];
364         char *sysname;
365         int ret = ENODEV;
366         hrtime_t start;
367
368         if ((udev = udev_new()) == NULL)
369                 return (ENXIO);
370
371         /* resolve path to a runtime device node instance */
372         if (realpath(path, nodepath) == NULL)
373                 goto no_dev;
374
375         sysname = strrchr(nodepath, '/') + 1;
376
377         /*
378          * Wait up to 3 seconds for udev to set up the device node context
379          */
380         start = gethrtime();
381         do {
382                 dev = udev_device_new_from_subsystem_sysname(udev, "block",
383                     sysname);
384                 if (dev == NULL)
385                         goto no_dev;
386                 if (udev_device_is_ready(dev))
387                         break;  /* udev ready */
388
389                 udev_device_unref(dev);
390                 dev = NULL;
391
392                 if (NSEC2MSEC(gethrtime() - start) < 10)
393                         (void) sched_yield();   /* yield/busy wait up to 10ms */
394                 else
395                         (void) usleep(10 * MILLISEC);
396
397         } while (NSEC2MSEC(gethrtime() - start) < (3 * MILLISEC));
398
399         if (dev == NULL)
400                 goto no_dev;
401
402         /*
403          * Only whole disks require extra device strings
404          */
405         if (!wholedisk && !udev_mpath_whole_disk(dev))
406                 goto no_dev;
407
408         ret = zfs_device_get_devid(dev, ds->vds_devid, sizeof (ds->vds_devid));
409         if (ret != 0)
410                 goto no_dev_ref;
411
412         /* physical location string (optional) */
413         if (zfs_device_get_physical(dev, ds->vds_devphys,
414             sizeof (ds->vds_devphys)) != 0) {
415                 ds->vds_devphys[0] = '\0'; /* empty string --> not available */
416         }
417
418 no_dev_ref:
419         udev_device_unref(dev);
420 no_dev:
421         udev_unref(udev);
422
423         return (ret);
424 }
425
426 /*
427  * Update a leaf vdev's persistent device strings (Linux only)
428  *
429  * - only applies for a dedicated leaf vdev (aka whole disk)
430  * - updated during pool create|add|attach|import
431  * - used for matching device matching during auto-{online,expand,replace}
432  * - stored in a leaf disk config label (i.e. alongside 'path' NVP)
433  * - these strings are currently not used in kernel (i.e. for vdev_disk_open)
434  *
435  * single device node example:
436  *      devid:          'scsi-MG03SCA300_350000494a8cb3d67-part1'
437  *      phys_path:      'pci-0000:04:00.0-sas-0x50000394a8cb3d67-lun-0'
438  *
439  * multipath device node example:
440  *      devid:          'dm-uuid-mpath-35000c5006304de3f'
441  *
442  * We also store the enclosure sysfs path for turning on enclosure LEDs
443  * (if applicable):
444  *      vdev_enc_sysfs_path: '/sys/class/enclosure/11:0:1:0/SLOT 4'
445  */
446 void
447 update_vdev_config_dev_strs(nvlist_t *nv)
448 {
449         vdev_dev_strs_t vds;
450         char *env, *type, *path;
451         uint64_t wholedisk = 0;
452         char *upath, *spath;
453
454         /*
455          * For the benefit of legacy ZFS implementations, allow
456          * for opting out of devid strings in the vdev label.
457          *
458          * example use:
459          *      env ZFS_VDEV_DEVID_OPT_OUT=YES zpool import dozer
460          *
461          * explanation:
462          * Older ZFS on Linux implementations had issues when attempting to
463          * display pool config VDEV names if a "devid" NVP value is present
464          * in the pool's config.
465          *
466          * For example, a pool that originated on illumos platform would
467          * have a devid value in the config and "zpool status" would fail
468          * when listing the config.
469          *
470          * A pool can be stripped of any "devid" values on import or
471          * prevented from adding them on zpool create|add by setting
472          * ZFS_VDEV_DEVID_OPT_OUT.
473          */
474         env = getenv("ZFS_VDEV_DEVID_OPT_OUT");
475         if (env && (strtoul(env, NULL, 0) > 0 ||
476             !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) {
477                 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
478                 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_PHYS_PATH);
479                 return;
480         }
481
482         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0 ||
483             strcmp(type, VDEV_TYPE_DISK) != 0) {
484                 return;
485         }
486         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
487                 return;
488         (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk);
489
490         /*
491          * Update device string values in config nvlist
492          */
493         if (encode_device_strings(path, &vds, (boolean_t)wholedisk) == 0) {
494                 (void) nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vds.vds_devid);
495                 if (vds.vds_devphys[0] != '\0') {
496                         (void) nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
497                             vds.vds_devphys);
498                 }
499
500                 /* Add enclosure sysfs path (if disk is in an enclosure) */
501                 upath = zfs_get_underlying_path(path);
502                 spath = zfs_get_enclosure_sysfs_path(upath);
503                 if (spath)
504                         nvlist_add_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
505                             spath);
506                 else
507                         nvlist_remove_all(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH);
508
509                 free(upath);
510                 free(spath);
511         } else {
512                 /* clear out any stale entries */
513                 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
514                 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_PHYS_PATH);
515                 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH);
516         }
517 }
518 #else
519
520 boolean_t
521 is_mpath_whole_disk(const char *path)
522 {
523         return (B_FALSE);
524 }
525
526 /*
527  * Wait up to timeout_ms for udev to set up the device node.  The device is
528  * considered ready when the provided path have been verified to exist and
529  * it has been allowed to settle.  At this point the device the device can
530  * be accessed reliably.  Depending on the complexity of the udev rules thisi
531  * process could take several seconds.
532  */
533 int
534 zpool_label_disk_wait(char *path, int timeout_ms)
535 {
536         int settle_ms = 50;
537         long sleep_ms = 10;
538         hrtime_t start, settle;
539         struct stat64 statbuf;
540
541         start = gethrtime();
542         settle = 0;
543
544         do {
545                 errno = 0;
546                 if ((stat64(path, &statbuf) == 0) && (errno == 0)) {
547                         if (settle == 0)
548                                 settle = gethrtime();
549                         else if (NSEC2MSEC(gethrtime() - settle) >= settle_ms)
550                                 return (0);
551                 } else if (errno != ENOENT) {
552                         return (errno);
553                 }
554
555                 usleep(sleep_ms * MILLISEC);
556         } while (NSEC2MSEC(gethrtime() - start) < timeout_ms);
557
558         return (ENODEV);
559 }
560
561 void
562 update_vdev_config_dev_strs(nvlist_t *nv)
563 {
564 }
565
566 #endif /* HAVE_LIBUDEV */
567
568 /*
569  * Go through and fix up any path and/or devid information for the given vdev
570  * configuration.
571  */
572 static int
573 fix_paths(nvlist_t *nv, name_entry_t *names)
574 {
575         nvlist_t **child;
576         uint_t c, children;
577         uint64_t guid;
578         name_entry_t *ne, *best;
579         char *path;
580
581         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
582             &child, &children) == 0) {
583                 for (c = 0; c < children; c++)
584                         if (fix_paths(child[c], names) != 0)
585                                 return (-1);
586                 return (0);
587         }
588
589         /*
590          * This is a leaf (file or disk) vdev.  In either case, go through
591          * the name list and see if we find a matching guid.  If so, replace
592          * the path and see if we can calculate a new devid.
593          *
594          * There may be multiple names associated with a particular guid, in
595          * which case we have overlapping partitions or multiple paths to the
596          * same disk.  In this case we prefer to use the path name which
597          * matches the ZPOOL_CONFIG_PATH.  If no matching entry is found we
598          * use the lowest order device which corresponds to the first match
599          * while traversing the ZPOOL_IMPORT_PATH search path.
600          */
601         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
602         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
603                 path = NULL;
604
605         best = NULL;
606         for (ne = names; ne != NULL; ne = ne->ne_next) {
607                 if (ne->ne_guid == guid) {
608                         if (path == NULL) {
609                                 best = ne;
610                                 break;
611                         }
612
613                         if ((strlen(path) == strlen(ne->ne_name)) &&
614                             strncmp(path, ne->ne_name, strlen(path)) == 0) {
615                                 best = ne;
616                                 break;
617                         }
618
619                         if (best == NULL) {
620                                 best = ne;
621                                 continue;
622                         }
623
624                         /* Prefer paths with move vdev labels. */
625                         if (ne->ne_num_labels > best->ne_num_labels) {
626                                 best = ne;
627                                 continue;
628                         }
629
630                         /* Prefer paths earlier in the search order. */
631                         if (ne->ne_num_labels == best->ne_num_labels &&
632                             ne->ne_order < best->ne_order) {
633                                 best = ne;
634                                 continue;
635                         }
636                 }
637         }
638
639         if (best == NULL)
640                 return (0);
641
642         if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
643                 return (-1);
644
645         /* Linux only - update ZPOOL_CONFIG_DEVID and ZPOOL_CONFIG_PHYS_PATH */
646         update_vdev_config_dev_strs(nv);
647
648         return (0);
649 }
650
651 /*
652  * Add the given configuration to the list of known devices.
653  */
654 static int
655 add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
656     int order, int num_labels, nvlist_t *config)
657 {
658         uint64_t pool_guid, vdev_guid, top_guid, txg, state;
659         pool_entry_t *pe;
660         vdev_entry_t *ve;
661         config_entry_t *ce;
662         name_entry_t *ne;
663
664         /*
665          * If this is a hot spare not currently in use or level 2 cache
666          * device, add it to the list of names to translate, but don't do
667          * anything else.
668          */
669         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
670             &state) == 0 &&
671             (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
672             nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
673                 if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) {
674                         nvlist_free(config);
675                         return (-1);
676                 }
677
678                 if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
679                         free(ne);
680                         nvlist_free(config);
681                         return (-1);
682                 }
683                 ne->ne_guid = vdev_guid;
684                 ne->ne_order = order;
685                 ne->ne_num_labels = num_labels;
686                 ne->ne_next = pl->names;
687                 pl->names = ne;
688                 nvlist_free(config);
689                 return (0);
690         }
691
692         /*
693          * If we have a valid config but cannot read any of these fields, then
694          * it means we have a half-initialized label.  In vdev_label_init()
695          * we write a label with txg == 0 so that we can identify the device
696          * in case the user refers to the same disk later on.  If we fail to
697          * create the pool, we'll be left with a label in this state
698          * which should not be considered part of a valid pool.
699          */
700         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
701             &pool_guid) != 0 ||
702             nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
703             &vdev_guid) != 0 ||
704             nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
705             &top_guid) != 0 ||
706             nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
707             &txg) != 0 || txg == 0) {
708                 nvlist_free(config);
709                 return (0);
710         }
711
712         /*
713          * First, see if we know about this pool.  If not, then add it to the
714          * list of known pools.
715          */
716         for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
717                 if (pe->pe_guid == pool_guid)
718                         break;
719         }
720
721         if (pe == NULL) {
722                 if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
723                         nvlist_free(config);
724                         return (-1);
725                 }
726                 pe->pe_guid = pool_guid;
727                 pe->pe_next = pl->pools;
728                 pl->pools = pe;
729         }
730
731         /*
732          * Second, see if we know about this toplevel vdev.  Add it if its
733          * missing.
734          */
735         for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
736                 if (ve->ve_guid == top_guid)
737                         break;
738         }
739
740         if (ve == NULL) {
741                 if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
742                         nvlist_free(config);
743                         return (-1);
744                 }
745                 ve->ve_guid = top_guid;
746                 ve->ve_next = pe->pe_vdevs;
747                 pe->pe_vdevs = ve;
748         }
749
750         /*
751          * Third, see if we have a config with a matching transaction group.  If
752          * so, then we do nothing.  Otherwise, add it to the list of known
753          * configs.
754          */
755         for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
756                 if (ce->ce_txg == txg)
757                         break;
758         }
759
760         if (ce == NULL) {
761                 if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
762                         nvlist_free(config);
763                         return (-1);
764                 }
765                 ce->ce_txg = txg;
766                 ce->ce_config = config;
767                 ce->ce_next = ve->ve_configs;
768                 ve->ve_configs = ce;
769         } else {
770                 nvlist_free(config);
771         }
772
773         /*
774          * At this point we've successfully added our config to the list of
775          * known configs.  The last thing to do is add the vdev guid -> path
776          * mappings so that we can fix up the configuration as necessary before
777          * doing the import.
778          */
779         if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
780                 return (-1);
781
782         if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
783                 free(ne);
784                 return (-1);
785         }
786
787         ne->ne_guid = vdev_guid;
788         ne->ne_order = order;
789         ne->ne_num_labels = num_labels;
790         ne->ne_next = pl->names;
791         pl->names = ne;
792
793         return (0);
794 }
795
796 /*
797  * Returns true if the named pool matches the given GUID.
798  */
799 static int
800 pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
801     boolean_t *isactive)
802 {
803         zpool_handle_t *zhp;
804         uint64_t theguid;
805
806         if (zpool_open_silent(hdl, name, &zhp) != 0)
807                 return (-1);
808
809         if (zhp == NULL) {
810                 *isactive = B_FALSE;
811                 return (0);
812         }
813
814         verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
815             &theguid) == 0);
816
817         zpool_close(zhp);
818
819         *isactive = (theguid == guid);
820         return (0);
821 }
822
823 static nvlist_t *
824 refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
825 {
826         nvlist_t *nvl;
827         zfs_cmd_t zc = {"\0"};
828         int err, dstbuf_size;
829
830         if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
831                 return (NULL);
832
833         dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4);
834
835         if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
836                 zcmd_free_nvlists(&zc);
837                 return (NULL);
838         }
839
840         while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
841             &zc)) != 0 && errno == ENOMEM) {
842                 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
843                         zcmd_free_nvlists(&zc);
844                         return (NULL);
845                 }
846         }
847
848         if (err) {
849                 zcmd_free_nvlists(&zc);
850                 return (NULL);
851         }
852
853         if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
854                 zcmd_free_nvlists(&zc);
855                 return (NULL);
856         }
857
858         zcmd_free_nvlists(&zc);
859         return (nvl);
860 }
861
862 /*
863  * Determine if the vdev id is a hole in the namespace.
864  */
865 boolean_t
866 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
867 {
868         int c;
869
870         for (c = 0; c < holes; c++) {
871
872                 /* Top-level is a hole */
873                 if (hole_array[c] == id)
874                         return (B_TRUE);
875         }
876         return (B_FALSE);
877 }
878
879 /*
880  * Convert our list of pools into the definitive set of configurations.  We
881  * start by picking the best config for each toplevel vdev.  Once that's done,
882  * we assemble the toplevel vdevs into a full config for the pool.  We make a
883  * pass to fix up any incorrect paths, and then add it to the main list to
884  * return to the user.
885  */
886 static nvlist_t *
887 get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
888 {
889         pool_entry_t *pe;
890         vdev_entry_t *ve;
891         config_entry_t *ce;
892         nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
893         nvlist_t **spares, **l2cache;
894         uint_t i, nspares, nl2cache;
895         boolean_t config_seen;
896         uint64_t best_txg;
897         char *name, *hostname = NULL;
898         uint64_t guid;
899         uint_t children = 0;
900         nvlist_t **child = NULL;
901         uint_t holes;
902         uint64_t *hole_array, max_id;
903         uint_t c;
904         boolean_t isactive;
905         uint64_t hostid;
906         nvlist_t *nvl;
907         boolean_t valid_top_config = B_FALSE;
908
909         if (nvlist_alloc(&ret, 0, 0) != 0)
910                 goto nomem;
911
912         for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
913                 uint64_t id, max_txg = 0;
914
915                 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
916                         goto nomem;
917                 config_seen = B_FALSE;
918
919                 /*
920                  * Iterate over all toplevel vdevs.  Grab the pool configuration
921                  * from the first one we find, and then go through the rest and
922                  * add them as necessary to the 'vdevs' member of the config.
923                  */
924                 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
925
926                         /*
927                          * Determine the best configuration for this vdev by
928                          * selecting the config with the latest transaction
929                          * group.
930                          */
931                         best_txg = 0;
932                         for (ce = ve->ve_configs; ce != NULL;
933                             ce = ce->ce_next) {
934
935                                 if (ce->ce_txg > best_txg) {
936                                         tmp = ce->ce_config;
937                                         best_txg = ce->ce_txg;
938                                 }
939                         }
940
941                         /*
942                          * We rely on the fact that the max txg for the
943                          * pool will contain the most up-to-date information
944                          * about the valid top-levels in the vdev namespace.
945                          */
946                         if (best_txg > max_txg) {
947                                 (void) nvlist_remove(config,
948                                     ZPOOL_CONFIG_VDEV_CHILDREN,
949                                     DATA_TYPE_UINT64);
950                                 (void) nvlist_remove(config,
951                                     ZPOOL_CONFIG_HOLE_ARRAY,
952                                     DATA_TYPE_UINT64_ARRAY);
953
954                                 max_txg = best_txg;
955                                 hole_array = NULL;
956                                 holes = 0;
957                                 max_id = 0;
958                                 valid_top_config = B_FALSE;
959
960                                 if (nvlist_lookup_uint64(tmp,
961                                     ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
962                                         verify(nvlist_add_uint64(config,
963                                             ZPOOL_CONFIG_VDEV_CHILDREN,
964                                             max_id) == 0);
965                                         valid_top_config = B_TRUE;
966                                 }
967
968                                 if (nvlist_lookup_uint64_array(tmp,
969                                     ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
970                                     &holes) == 0) {
971                                         verify(nvlist_add_uint64_array(config,
972                                             ZPOOL_CONFIG_HOLE_ARRAY,
973                                             hole_array, holes) == 0);
974                                 }
975                         }
976
977                         if (!config_seen) {
978                                 /*
979                                  * Copy the relevant pieces of data to the pool
980                                  * configuration:
981                                  *
982                                  *      version
983                                  *      pool guid
984                                  *      name
985                                  *      comment (if available)
986                                  *      pool state
987                                  *      hostid (if available)
988                                  *      hostname (if available)
989                                  */
990                                 uint64_t state, version;
991                                 char *comment = NULL;
992
993                                 version = fnvlist_lookup_uint64(tmp,
994                                     ZPOOL_CONFIG_VERSION);
995                                 fnvlist_add_uint64(config,
996                                     ZPOOL_CONFIG_VERSION, version);
997                                 guid = fnvlist_lookup_uint64(tmp,
998                                     ZPOOL_CONFIG_POOL_GUID);
999                                 fnvlist_add_uint64(config,
1000                                     ZPOOL_CONFIG_POOL_GUID, guid);
1001                                 name = fnvlist_lookup_string(tmp,
1002                                     ZPOOL_CONFIG_POOL_NAME);
1003                                 fnvlist_add_string(config,
1004                                     ZPOOL_CONFIG_POOL_NAME, name);
1005
1006                                 if (nvlist_lookup_string(tmp,
1007                                     ZPOOL_CONFIG_COMMENT, &comment) == 0)
1008                                         fnvlist_add_string(config,
1009                                             ZPOOL_CONFIG_COMMENT, comment);
1010
1011                                 state = fnvlist_lookup_uint64(tmp,
1012                                     ZPOOL_CONFIG_POOL_STATE);
1013                                 fnvlist_add_uint64(config,
1014                                     ZPOOL_CONFIG_POOL_STATE, state);
1015
1016                                 hostid = 0;
1017                                 if (nvlist_lookup_uint64(tmp,
1018                                     ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
1019                                         fnvlist_add_uint64(config,
1020                                             ZPOOL_CONFIG_HOSTID, hostid);
1021                                         hostname = fnvlist_lookup_string(tmp,
1022                                             ZPOOL_CONFIG_HOSTNAME);
1023                                         fnvlist_add_string(config,
1024                                             ZPOOL_CONFIG_HOSTNAME, hostname);
1025                                 }
1026
1027                                 config_seen = B_TRUE;
1028                         }
1029
1030                         /*
1031                          * Add this top-level vdev to the child array.
1032                          */
1033                         verify(nvlist_lookup_nvlist(tmp,
1034                             ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
1035                         verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
1036                             &id) == 0);
1037
1038                         if (id >= children) {
1039                                 nvlist_t **newchild;
1040
1041                                 newchild = zfs_alloc(hdl, (id + 1) *
1042                                     sizeof (nvlist_t *));
1043                                 if (newchild == NULL)
1044                                         goto nomem;
1045
1046                                 for (c = 0; c < children; c++)
1047                                         newchild[c] = child[c];
1048
1049                                 free(child);
1050                                 child = newchild;
1051                                 children = id + 1;
1052                         }
1053                         if (nvlist_dup(nvtop, &child[id], 0) != 0)
1054                                 goto nomem;
1055
1056                 }
1057
1058                 /*
1059                  * If we have information about all the top-levels then
1060                  * clean up the nvlist which we've constructed. This
1061                  * means removing any extraneous devices that are
1062                  * beyond the valid range or adding devices to the end
1063                  * of our array which appear to be missing.
1064                  */
1065                 if (valid_top_config) {
1066                         if (max_id < children) {
1067                                 for (c = max_id; c < children; c++)
1068                                         nvlist_free(child[c]);
1069                                 children = max_id;
1070                         } else if (max_id > children) {
1071                                 nvlist_t **newchild;
1072
1073                                 newchild = zfs_alloc(hdl, (max_id) *
1074                                     sizeof (nvlist_t *));
1075                                 if (newchild == NULL)
1076                                         goto nomem;
1077
1078                                 for (c = 0; c < children; c++)
1079                                         newchild[c] = child[c];
1080
1081                                 free(child);
1082                                 child = newchild;
1083                                 children = max_id;
1084                         }
1085                 }
1086
1087                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1088                     &guid) == 0);
1089
1090                 /*
1091                  * The vdev namespace may contain holes as a result of
1092                  * device removal. We must add them back into the vdev
1093                  * tree before we process any missing devices.
1094                  */
1095                 if (holes > 0) {
1096                         ASSERT(valid_top_config);
1097
1098                         for (c = 0; c < children; c++) {
1099                                 nvlist_t *holey;
1100
1101                                 if (child[c] != NULL ||
1102                                     !vdev_is_hole(hole_array, holes, c))
1103                                         continue;
1104
1105                                 if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
1106                                     0) != 0)
1107                                         goto nomem;
1108
1109                                 /*
1110                                  * Holes in the namespace are treated as
1111                                  * "hole" top-level vdevs and have a
1112                                  * special flag set on them.
1113                                  */
1114                                 if (nvlist_add_string(holey,
1115                                     ZPOOL_CONFIG_TYPE,
1116                                     VDEV_TYPE_HOLE) != 0 ||
1117                                     nvlist_add_uint64(holey,
1118                                     ZPOOL_CONFIG_ID, c) != 0 ||
1119                                     nvlist_add_uint64(holey,
1120                                     ZPOOL_CONFIG_GUID, 0ULL) != 0) {
1121                                         nvlist_free(holey);
1122                                         goto nomem;
1123                                 }
1124                                 child[c] = holey;
1125                         }
1126                 }
1127
1128                 /*
1129                  * Look for any missing top-level vdevs.  If this is the case,
1130                  * create a faked up 'missing' vdev as a placeholder.  We cannot
1131                  * simply compress the child array, because the kernel performs
1132                  * certain checks to make sure the vdev IDs match their location
1133                  * in the configuration.
1134                  */
1135                 for (c = 0; c < children; c++) {
1136                         if (child[c] == NULL) {
1137                                 nvlist_t *missing;
1138                                 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
1139                                     0) != 0)
1140                                         goto nomem;
1141                                 if (nvlist_add_string(missing,
1142                                     ZPOOL_CONFIG_TYPE,
1143                                     VDEV_TYPE_MISSING) != 0 ||
1144                                     nvlist_add_uint64(missing,
1145                                     ZPOOL_CONFIG_ID, c) != 0 ||
1146                                     nvlist_add_uint64(missing,
1147                                     ZPOOL_CONFIG_GUID, 0ULL) != 0) {
1148                                         nvlist_free(missing);
1149                                         goto nomem;
1150                                 }
1151                                 child[c] = missing;
1152                         }
1153                 }
1154
1155                 /*
1156                  * Put all of this pool's top-level vdevs into a root vdev.
1157                  */
1158                 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
1159                         goto nomem;
1160                 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1161                     VDEV_TYPE_ROOT) != 0 ||
1162                     nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
1163                     nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
1164                     nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1165                     child, children) != 0) {
1166                         nvlist_free(nvroot);
1167                         goto nomem;
1168                 }
1169
1170                 for (c = 0; c < children; c++)
1171                         nvlist_free(child[c]);
1172                 free(child);
1173                 children = 0;
1174                 child = NULL;
1175
1176                 /*
1177                  * Go through and fix up any paths and/or devids based on our
1178                  * known list of vdev GUID -> path mappings.
1179                  */
1180                 if (fix_paths(nvroot, pl->names) != 0) {
1181                         nvlist_free(nvroot);
1182                         goto nomem;
1183                 }
1184
1185                 /*
1186                  * Add the root vdev to this pool's configuration.
1187                  */
1188                 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1189                     nvroot) != 0) {
1190                         nvlist_free(nvroot);
1191                         goto nomem;
1192                 }
1193                 nvlist_free(nvroot);
1194
1195                 /*
1196                  * zdb uses this path to report on active pools that were
1197                  * imported or created using -R.
1198                  */
1199                 if (active_ok)
1200                         goto add_pool;
1201
1202                 /*
1203                  * Determine if this pool is currently active, in which case we
1204                  * can't actually import it.
1205                  */
1206                 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1207                     &name) == 0);
1208                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1209                     &guid) == 0);
1210
1211                 if (pool_active(hdl, name, guid, &isactive) != 0)
1212                         goto error;
1213
1214                 if (isactive) {
1215                         nvlist_free(config);
1216                         config = NULL;
1217                         continue;
1218                 }
1219
1220                 if ((nvl = refresh_config(hdl, config)) == NULL) {
1221                         nvlist_free(config);
1222                         config = NULL;
1223                         continue;
1224                 }
1225
1226                 nvlist_free(config);
1227                 config = nvl;
1228
1229                 /*
1230                  * Go through and update the paths for spares, now that we have
1231                  * them.
1232                  */
1233                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1234                     &nvroot) == 0);
1235                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1236                     &spares, &nspares) == 0) {
1237                         for (i = 0; i < nspares; i++) {
1238                                 if (fix_paths(spares[i], pl->names) != 0)
1239                                         goto nomem;
1240                         }
1241                 }
1242
1243                 /*
1244                  * Update the paths for l2cache devices.
1245                  */
1246                 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1247                     &l2cache, &nl2cache) == 0) {
1248                         for (i = 0; i < nl2cache; i++) {
1249                                 if (fix_paths(l2cache[i], pl->names) != 0)
1250                                         goto nomem;
1251                         }
1252                 }
1253
1254                 /*
1255                  * Restore the original information read from the actual label.
1256                  */
1257                 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
1258                     DATA_TYPE_UINT64);
1259                 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
1260                     DATA_TYPE_STRING);
1261                 if (hostid != 0) {
1262                         verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
1263                             hostid) == 0);
1264                         verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
1265                             hostname) == 0);
1266                 }
1267
1268 add_pool:
1269                 /*
1270                  * Add this pool to the list of configs.
1271                  */
1272                 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1273                     &name) == 0);
1274                 if (nvlist_add_nvlist(ret, name, config) != 0)
1275                         goto nomem;
1276
1277                 nvlist_free(config);
1278                 config = NULL;
1279         }
1280
1281         return (ret);
1282
1283 nomem:
1284         (void) no_memory(hdl);
1285 error:
1286         nvlist_free(config);
1287         nvlist_free(ret);
1288         for (c = 0; c < children; c++)
1289                 nvlist_free(child[c]);
1290         free(child);
1291
1292         return (NULL);
1293 }
1294
1295 /*
1296  * Return the offset of the given label.
1297  */
1298 static uint64_t
1299 label_offset(uint64_t size, int l)
1300 {
1301         ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
1302         return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
1303             0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
1304 }
1305
1306 /*
1307  * Given a file descriptor, read the label information and return an nvlist
1308  * describing the configuration, if there is one.  The number of valid
1309  * labels found will be returned in num_labels when non-NULL.
1310  */
1311 int
1312 zpool_read_label(int fd, nvlist_t **config, int *num_labels)
1313 {
1314         struct stat64 statbuf;
1315         int l, count = 0;
1316         vdev_label_t *label;
1317         nvlist_t *expected_config = NULL;
1318         uint64_t expected_guid = 0, size;
1319         int error;
1320
1321         *config = NULL;
1322
1323         if (fstat64_blk(fd, &statbuf) == -1)
1324                 return (0);
1325         size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1326
1327         error = posix_memalign((void **)&label, PAGESIZE, sizeof (*label));
1328         if (error)
1329                 return (-1);
1330
1331         for (l = 0; l < VDEV_LABELS; l++) {
1332                 uint64_t state, guid, txg;
1333
1334                 if (pread64(fd, label, sizeof (vdev_label_t),
1335                     label_offset(size, l)) != sizeof (vdev_label_t))
1336                         continue;
1337
1338                 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
1339                     sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
1340                         continue;
1341
1342                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
1343                     &guid) != 0 || guid == 0) {
1344                         nvlist_free(*config);
1345                         continue;
1346                 }
1347
1348                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
1349                     &state) != 0 || state > POOL_STATE_L2CACHE) {
1350                         nvlist_free(*config);
1351                         continue;
1352                 }
1353
1354                 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1355                     (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
1356                     &txg) != 0 || txg == 0)) {
1357                         nvlist_free(*config);
1358                         continue;
1359                 }
1360
1361                 if (expected_guid) {
1362                         if (expected_guid == guid)
1363                                 count++;
1364
1365                         nvlist_free(*config);
1366                 } else {
1367                         expected_config = *config;
1368                         expected_guid = guid;
1369                         count++;
1370                 }
1371         }
1372
1373         if (num_labels != NULL)
1374                 *num_labels = count;
1375
1376         free(label);
1377         *config = expected_config;
1378
1379         return (0);
1380 }
1381
1382 typedef struct rdsk_node {
1383         char *rn_name;                  /* Full path to device */
1384         int rn_order;                   /* Preferred order (low to high) */
1385         int rn_num_labels;              /* Number of valid labels */
1386         uint64_t rn_vdev_guid;          /* Expected vdev guid when set */
1387         libzfs_handle_t *rn_hdl;
1388         nvlist_t *rn_config;            /* Label config */
1389         avl_tree_t *rn_avl;
1390         avl_node_t rn_node;
1391         kmutex_t *rn_lock;
1392         boolean_t rn_labelpaths;
1393 } rdsk_node_t;
1394
1395 /*
1396  * Sorted by vdev guid and full path to allow for multiple entries with
1397  * the same full path name.  This is required because it's possible to
1398  * have multiple block devices with labels that refer to the same
1399  * ZPOOL_CONFIG_PATH yet have different vdev guids.  In this case both
1400  * entries need to be added to the cache.  Scenarios where this can occur
1401  * include overwritten pool labels, devices which are visible from multiple
1402  * hosts and multipath devices.
1403  */
1404 static int
1405 slice_cache_compare(const void *arg1, const void *arg2)
1406 {
1407         const char  *nm1 = ((rdsk_node_t *)arg1)->rn_name;
1408         const char  *nm2 = ((rdsk_node_t *)arg2)->rn_name;
1409         uint64_t guid1 = ((rdsk_node_t *)arg1)->rn_vdev_guid;
1410         uint64_t guid2 = ((rdsk_node_t *)arg2)->rn_vdev_guid;
1411         int rv;
1412
1413         rv = AVL_CMP(guid1, guid2);
1414         if (rv)
1415                 return (rv);
1416
1417         return (AVL_ISIGN(strcmp(nm1, nm2)));
1418 }
1419
1420 static boolean_t
1421 is_watchdog_dev(char *dev)
1422 {
1423         /* For 'watchdog' dev */
1424         if (strcmp(dev, "watchdog") == 0)
1425                 return (B_TRUE);
1426
1427         /* For 'watchdog<digit><whatever> */
1428         if (strstr(dev, "watchdog") == dev && isdigit(dev[8]))
1429                 return (B_TRUE);
1430
1431         return (B_FALSE);
1432 }
1433
1434 static int
1435 label_paths_impl(libzfs_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid,
1436     uint64_t vdev_guid, char **path, char **devid)
1437 {
1438         nvlist_t **child;
1439         uint_t c, children;
1440         uint64_t guid;
1441         char *val;
1442         int error;
1443
1444         if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1445             &child, &children) == 0) {
1446                 for (c = 0; c < children; c++) {
1447                         error  = label_paths_impl(hdl, child[c],
1448                             pool_guid, vdev_guid, path, devid);
1449                         if (error)
1450                                 return (error);
1451                 }
1452                 return (0);
1453         }
1454
1455         if (nvroot == NULL)
1456                 return (0);
1457
1458         error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
1459         if ((error != 0) || (guid != vdev_guid))
1460                 return (0);
1461
1462         error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val);
1463         if (error == 0)
1464                 *path = val;
1465
1466         error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val);
1467         if (error == 0)
1468                 *devid = val;
1469
1470         return (0);
1471 }
1472
1473 /*
1474  * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID
1475  * and store these strings as config_path and devid_path respectively.
1476  * The returned pointers are only valid as long as label remains valid.
1477  */
1478 static int
1479 label_paths(libzfs_handle_t *hdl, nvlist_t *label, char **path, char **devid)
1480 {
1481         nvlist_t *nvroot;
1482         uint64_t pool_guid;
1483         uint64_t vdev_guid;
1484
1485         *path = NULL;
1486         *devid = NULL;
1487
1488         if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1489             nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
1490             nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
1491                 return (ENOENT);
1492
1493         return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path,
1494             devid));
1495 }
1496
1497 static void
1498 zpool_open_func(void *arg)
1499 {
1500         rdsk_node_t *rn = arg;
1501         libzfs_handle_t *hdl = rn->rn_hdl;
1502         struct stat64 statbuf;
1503         nvlist_t *config;
1504         char *bname, *dupname;
1505         uint64_t vdev_guid = 0;
1506         int error;
1507         int num_labels;
1508         int fd;
1509
1510         /*
1511          * Skip devices with well known prefixes there can be side effects
1512          * when opening devices which need to be avoided.
1513          *
1514          * hpet     - High Precision Event Timer
1515          * watchdog - Watchdog must be closed in a special way.
1516          */
1517         dupname = zfs_strdup(hdl, rn->rn_name);
1518         bname = basename(dupname);
1519         error = ((strcmp(bname, "hpet") == 0) || is_watchdog_dev(bname));
1520         free(dupname);
1521         if (error)
1522                 return;
1523
1524         /*
1525          * Ignore failed stats.  We only want regular files and block devices.
1526          */
1527         if (stat64(rn->rn_name, &statbuf) != 0 ||
1528             (!S_ISREG(statbuf.st_mode) && !S_ISBLK(statbuf.st_mode)))
1529                 return;
1530
1531         /*
1532          * Preferentially open using O_DIRECT to bypass the block device
1533          * cache which may be stale for multipath devices.  An EINVAL errno
1534          * indicates O_DIRECT is unsupported so fallback to just O_RDONLY.
1535          */
1536         fd = open(rn->rn_name, O_RDONLY | O_DIRECT);
1537         if ((fd < 0) && (errno == EINVAL))
1538                 fd = open(rn->rn_name, O_RDONLY);
1539
1540         if (fd < 0)
1541                 return;
1542
1543         /*
1544          * This file is too small to hold a zpool
1545          */
1546         if (S_ISREG(statbuf.st_mode) && statbuf.st_size < SPA_MINDEVSIZE) {
1547                 (void) close(fd);
1548                 return;
1549         }
1550
1551         error = zpool_read_label(fd, &config, &num_labels);
1552         if (error != 0) {
1553                 (void) close(fd);
1554                 return;
1555         }
1556
1557         if (num_labels == 0) {
1558                 (void) close(fd);
1559                 nvlist_free(config);
1560                 return;
1561         }
1562
1563         /*
1564          * Check that the vdev is for the expected guid.  Additional entries
1565          * are speculatively added based on the paths stored in the labels.
1566          * Entries with valid paths but incorrect guids must be removed.
1567          */
1568         error = nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid);
1569         if (error || (rn->rn_vdev_guid && rn->rn_vdev_guid != vdev_guid)) {
1570                 (void) close(fd);
1571                 nvlist_free(config);
1572                 return;
1573         }
1574
1575         (void) close(fd);
1576
1577         rn->rn_config = config;
1578         rn->rn_num_labels = num_labels;
1579
1580         /*
1581          * Add additional entries for paths described by this label.
1582          */
1583         if (rn->rn_labelpaths) {
1584                 char *path = NULL;
1585                 char *devid = NULL;
1586                 rdsk_node_t *slice;
1587                 avl_index_t where;
1588                 int error;
1589
1590                 if (label_paths(rn->rn_hdl, rn->rn_config, &path, &devid))
1591                         return;
1592
1593                 /*
1594                  * Allow devlinks to stabilize so all paths are available.
1595                  */
1596                 zpool_label_disk_wait(rn->rn_name, DISK_LABEL_WAIT);
1597
1598                 if (path != NULL) {
1599                         slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
1600                         slice->rn_name = zfs_strdup(hdl, path);
1601                         slice->rn_vdev_guid = vdev_guid;
1602                         slice->rn_avl = rn->rn_avl;
1603                         slice->rn_hdl = hdl;
1604                         slice->rn_order = IMPORT_ORDER_PREFERRED_1;
1605                         slice->rn_labelpaths = B_FALSE;
1606                         mutex_enter(rn->rn_lock);
1607                         if (avl_find(rn->rn_avl, slice, &where)) {
1608                         mutex_exit(rn->rn_lock);
1609                                 free(slice->rn_name);
1610                                 free(slice);
1611                         } else {
1612                                 avl_insert(rn->rn_avl, slice, where);
1613                                 mutex_exit(rn->rn_lock);
1614                                 zpool_open_func(slice);
1615                         }
1616                 }
1617
1618                 if (devid != NULL) {
1619                         slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
1620                         error = asprintf(&slice->rn_name, "%s%s",
1621                             DEV_BYID_PATH, devid);
1622                         if (error == -1) {
1623                                 free(slice);
1624                                 return;
1625                         }
1626
1627                         slice->rn_vdev_guid = vdev_guid;
1628                         slice->rn_avl = rn->rn_avl;
1629                         slice->rn_hdl = hdl;
1630                         slice->rn_order = IMPORT_ORDER_PREFERRED_2;
1631                         slice->rn_labelpaths = B_FALSE;
1632                         mutex_enter(rn->rn_lock);
1633                         if (avl_find(rn->rn_avl, slice, &where)) {
1634                                 mutex_exit(rn->rn_lock);
1635                                 free(slice->rn_name);
1636                                 free(slice);
1637                         } else {
1638                                 avl_insert(rn->rn_avl, slice, where);
1639                                 mutex_exit(rn->rn_lock);
1640                                 zpool_open_func(slice);
1641                         }
1642                 }
1643         }
1644 }
1645
1646 /*
1647  * Given a file descriptor, clear (zero) the label information.  This function
1648  * is used in the appliance stack as part of the ZFS sysevent module and
1649  * to implement the "zpool labelclear" command.
1650  */
1651 int
1652 zpool_clear_label(int fd)
1653 {
1654         struct stat64 statbuf;
1655         int l;
1656         vdev_label_t *label;
1657         uint64_t size;
1658
1659         if (fstat64_blk(fd, &statbuf) == -1)
1660                 return (0);
1661         size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1662
1663         if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
1664                 return (-1);
1665
1666         for (l = 0; l < VDEV_LABELS; l++) {
1667                 if (pwrite64(fd, label, sizeof (vdev_label_t),
1668                     label_offset(size, l)) != sizeof (vdev_label_t)) {
1669                         free(label);
1670                         return (-1);
1671                 }
1672         }
1673
1674         free(label);
1675         return (0);
1676 }
1677
1678 /*
1679  * Scan a list of directories for zfs devices.
1680  */
1681 static int
1682 zpool_find_import_scan(libzfs_handle_t *hdl, kmutex_t *lock,
1683     avl_tree_t **slice_cache, char **dir, int dirs)
1684 {
1685         avl_tree_t *cache;
1686         rdsk_node_t *slice;
1687         void *cookie;
1688         int i, error;
1689
1690         *slice_cache = NULL;
1691         cache = zfs_alloc(hdl, sizeof (avl_tree_t));
1692         avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t),
1693             offsetof(rdsk_node_t, rn_node));
1694
1695         for (i = 0; i < dirs; i++) {
1696                 char path[MAXPATHLEN];
1697                 struct dirent64 *dp;
1698                 DIR *dirp;
1699
1700                 if (realpath(dir[i], path) == NULL) {
1701                         error = errno;
1702                         if (error == ENOENT)
1703                                 continue;
1704
1705                         zfs_error_aux(hdl, strerror(error));
1706                         (void) zfs_error_fmt(hdl, EZFS_BADPATH, dgettext(
1707                             TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]);
1708                         goto error;
1709                 }
1710
1711                 dirp = opendir(path);
1712                 if (dirp == NULL) {
1713                         error = errno;
1714                         zfs_error_aux(hdl, strerror(error));
1715                         (void) zfs_error_fmt(hdl, EZFS_BADPATH,
1716                             dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
1717                         goto error;
1718                 }
1719
1720                 while ((dp = readdir64(dirp)) != NULL) {
1721                         const char *name = dp->d_name;
1722                         if (name[0] == '.' &&
1723                             (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1724                                 continue;
1725
1726                         slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
1727                         error = asprintf(&slice->rn_name, "%s/%s", path, name);
1728                         if (error == -1) {
1729                                 free(slice);
1730                                 continue;
1731                         }
1732                         slice->rn_vdev_guid = 0;
1733                         slice->rn_lock = lock;
1734                         slice->rn_avl = cache;
1735                         slice->rn_hdl = hdl;
1736                         slice->rn_order = i + IMPORT_ORDER_SCAN_OFFSET;
1737                         slice->rn_labelpaths = B_FALSE;
1738                         mutex_enter(lock);
1739                         avl_add(cache, slice);
1740                         mutex_exit(lock);
1741                 }
1742
1743                 (void) closedir(dirp);
1744         }
1745
1746         *slice_cache = cache;
1747         return (0);
1748
1749 error:
1750         cookie = NULL;
1751         while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1752                 free(slice->rn_name);
1753                 free(slice);
1754         }
1755         free(cache);
1756
1757         return (error);
1758 }
1759
1760 /*
1761  * Use libblkid to quickly enumerate all known zfs devices.
1762  */
1763 static int
1764 zpool_find_import_blkid(libzfs_handle_t *hdl, kmutex_t *lock,
1765     avl_tree_t **slice_cache)
1766 {
1767         rdsk_node_t *slice;
1768         blkid_cache cache;
1769         blkid_dev_iterate iter;
1770         blkid_dev dev;
1771         avl_index_t where;
1772         int error;
1773
1774         *slice_cache = NULL;
1775
1776         error = blkid_get_cache(&cache, NULL);
1777         if (error != 0)
1778                 return (error);
1779
1780         error = blkid_probe_all_new(cache);
1781         if (error != 0) {
1782                 blkid_put_cache(cache);
1783                 return (error);
1784         }
1785
1786         iter = blkid_dev_iterate_begin(cache);
1787         if (iter == NULL) {
1788                 blkid_put_cache(cache);
1789                 return (EINVAL);
1790         }
1791
1792         error = blkid_dev_set_search(iter, "TYPE", "zfs_member");
1793         if (error != 0) {
1794                 blkid_dev_iterate_end(iter);
1795                 blkid_put_cache(cache);
1796                 return (error);
1797         }
1798
1799         *slice_cache = zfs_alloc(hdl, sizeof (avl_tree_t));
1800         avl_create(*slice_cache, slice_cache_compare, sizeof (rdsk_node_t),
1801             offsetof(rdsk_node_t, rn_node));
1802
1803         while (blkid_dev_next(iter, &dev) == 0) {
1804                 slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
1805                 slice->rn_name = zfs_strdup(hdl, blkid_dev_devname(dev));
1806                 slice->rn_vdev_guid = 0;
1807                 slice->rn_lock = lock;
1808                 slice->rn_avl = *slice_cache;
1809                 slice->rn_hdl = hdl;
1810                 slice->rn_labelpaths = B_TRUE;
1811
1812                 error = zfs_path_order(slice->rn_name, &slice->rn_order);
1813                 if (error == 0)
1814                         slice->rn_order += IMPORT_ORDER_SCAN_OFFSET;
1815                 else
1816                         slice->rn_order = IMPORT_ORDER_DEFAULT;
1817
1818                 mutex_enter(lock);
1819                 if (avl_find(*slice_cache, slice, &where)) {
1820                         free(slice->rn_name);
1821                         free(slice);
1822                 } else {
1823                         avl_insert(*slice_cache, slice, where);
1824                 }
1825                 mutex_exit(lock);
1826         }
1827
1828         blkid_dev_iterate_end(iter);
1829         blkid_put_cache(cache);
1830
1831         return (0);
1832 }
1833
1834 char *
1835 zpool_default_import_path[DEFAULT_IMPORT_PATH_SIZE] = {
1836         "/dev/disk/by-vdev",    /* Custom rules, use first if they exist */
1837         "/dev/mapper",          /* Use multipath devices before components */
1838         "/dev/disk/by-partlabel", /* Single unique entry set by user */
1839         "/dev/disk/by-partuuid", /* Generated partition uuid */
1840         "/dev/disk/by-label",   /* Custom persistent labels */
1841         "/dev/disk/by-uuid",    /* Single unique entry and persistent */
1842         "/dev/disk/by-id",      /* May be multiple entries and persistent */
1843         "/dev/disk/by-path",    /* Encodes physical location and persistent */
1844         "/dev"                  /* UNSAFE device names will change */
1845 };
1846
1847 /*
1848  * Given a list of directories to search, find all pools stored on disk.  This
1849  * includes partial pools which are not available to import.  If no args are
1850  * given (argc is 0), then the default directory (/dev/dsk) is searched.
1851  * poolname or guid (but not both) are provided by the caller when trying
1852  * to import a specific pool.
1853  */
1854 static nvlist_t *
1855 zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg)
1856 {
1857         nvlist_t *ret = NULL;
1858         pool_list_t pools = { 0 };
1859         pool_entry_t *pe, *penext;
1860         vdev_entry_t *ve, *venext;
1861         config_entry_t *ce, *cenext;
1862         name_entry_t *ne, *nenext;
1863         kmutex_t lock;
1864         avl_tree_t *cache;
1865         rdsk_node_t *slice;
1866         void *cookie;
1867         taskq_t *t;
1868
1869         verify(iarg->poolname == NULL || iarg->guid == 0);
1870         mutex_init(&lock, NULL, MUTEX_DEFAULT, NULL);
1871
1872         /*
1873          * Locate pool member vdevs using libblkid or by directory scanning.
1874          * On success a newly allocated AVL tree which is populated with an
1875          * entry for each discovered vdev will be returned as the cache.
1876          * It's the callers responsibility to consume and destroy this tree.
1877          */
1878         if (iarg->scan || iarg->paths != 0) {
1879                 int dirs = iarg->paths;
1880                 char **dir = iarg->path;
1881
1882                 if (dirs == 0) {
1883                         dir = zpool_default_import_path;
1884                         dirs = DEFAULT_IMPORT_PATH_SIZE;
1885                 }
1886
1887                 if (zpool_find_import_scan(hdl, &lock, &cache, dir,  dirs) != 0)
1888                         return (NULL);
1889         } else {
1890                 if (zpool_find_import_blkid(hdl, &lock, &cache) != 0)
1891                         return (NULL);
1892         }
1893
1894         /*
1895          * Create a thread pool to parallelize the process of reading and
1896          * validating labels, a large number of threads can be used due to
1897          * minimal contention.
1898          */
1899         t = taskq_create("z_import", 2 * boot_ncpus, defclsyspri,
1900             2 * boot_ncpus, INT_MAX, TASKQ_PREPOPULATE);
1901
1902         for (slice = avl_first(cache); slice;
1903             (slice = avl_walk(cache, slice, AVL_AFTER)))
1904                 (void) taskq_dispatch(t, zpool_open_func, slice, TQ_SLEEP);
1905
1906         taskq_wait(t);
1907         taskq_destroy(t);
1908
1909         /*
1910          * Process the cache filtering out any entries which are not
1911          * for the specificed pool then adding matching label configs.
1912          */
1913         cookie = NULL;
1914         while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) {
1915                 if (slice->rn_config != NULL) {
1916                         nvlist_t *config = slice->rn_config;
1917                         boolean_t matched = B_TRUE;
1918                         boolean_t aux = B_FALSE;
1919                         int fd;
1920
1921                         /*
1922                          * Check if it's a spare or l2cache device. If it is,
1923                          * we need to skip the name and guid check since they
1924                          * don't exist on aux device label.
1925                          */
1926                         if (iarg->poolname != NULL || iarg->guid != 0) {
1927                                 uint64_t state;
1928                                 aux = nvlist_lookup_uint64(config,
1929                                     ZPOOL_CONFIG_POOL_STATE, &state) == 0 &&
1930                                     (state == POOL_STATE_SPARE ||
1931                                     state == POOL_STATE_L2CACHE);
1932                         }
1933
1934                         if (iarg->poolname != NULL && !aux) {
1935                                 char *pname;
1936
1937                                 matched = nvlist_lookup_string(config,
1938                                     ZPOOL_CONFIG_POOL_NAME, &pname) == 0 &&
1939                                     strcmp(iarg->poolname, pname) == 0;
1940                         } else if (iarg->guid != 0 && !aux) {
1941                                 uint64_t this_guid;
1942
1943                                 matched = nvlist_lookup_uint64(config,
1944                                     ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 &&
1945                                     iarg->guid == this_guid;
1946                         }
1947                         if (!matched) {
1948                                 nvlist_free(config);
1949                         } else {
1950                                 /*
1951                                  * Verify all remaining entries can be opened
1952                                  * exclusively. This will prune all underlying
1953                                  * multipath devices which otherwise could
1954                                  * result in the vdev appearing as UNAVAIL.
1955                                  *
1956                                  * Under zdb, this step isn't required and
1957                                  * would prevent a zdb -e of active pools with
1958                                  * no cachefile.
1959                                  */
1960                                 fd = open(slice->rn_name, O_RDONLY | O_EXCL);
1961                                 if (fd >= 0 || iarg->can_be_active) {
1962                                         if (fd >= 0)
1963                                                 close(fd);
1964                                         add_config(hdl, &pools,
1965                                             slice->rn_name, slice->rn_order,
1966                                             slice->rn_num_labels, config);
1967                                 } else {
1968                                         nvlist_free(config);
1969                                 }
1970                         }
1971                 }
1972                 free(slice->rn_name);
1973                 free(slice);
1974         }
1975         avl_destroy(cache);
1976         free(cache);
1977         mutex_destroy(&lock);
1978
1979         ret = get_configs(hdl, &pools, iarg->can_be_active);
1980
1981         for (pe = pools.pools; pe != NULL; pe = penext) {
1982                 penext = pe->pe_next;
1983                 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1984                         venext = ve->ve_next;
1985                         for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1986                                 cenext = ce->ce_next;
1987                                 nvlist_free(ce->ce_config);
1988                                 free(ce);
1989                         }
1990                         free(ve);
1991                 }
1992                 free(pe);
1993         }
1994
1995         for (ne = pools.names; ne != NULL; ne = nenext) {
1996                 nenext = ne->ne_next;
1997                 free(ne->ne_name);
1998                 free(ne);
1999         }
2000
2001         return (ret);
2002 }
2003
2004 nvlist_t *
2005 zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
2006 {
2007         importargs_t iarg = { 0 };
2008
2009         iarg.paths = argc;
2010         iarg.path = argv;
2011
2012         return (zpool_find_import_impl(hdl, &iarg));
2013 }
2014
2015 /*
2016  * Given a cache file, return the contents as a list of importable pools.
2017  * poolname or guid (but not both) are provided by the caller when trying
2018  * to import a specific pool.
2019  */
2020 nvlist_t *
2021 zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
2022     char *poolname, uint64_t guid)
2023 {
2024         char *buf;
2025         int fd;
2026         struct stat64 statbuf;
2027         nvlist_t *raw, *src, *dst;
2028         nvlist_t *pools;
2029         nvpair_t *elem;
2030         char *name;
2031         uint64_t this_guid;
2032         boolean_t active;
2033
2034         verify(poolname == NULL || guid == 0);
2035
2036         if ((fd = open(cachefile, O_RDONLY)) < 0) {
2037                 zfs_error_aux(hdl, "%s", strerror(errno));
2038                 (void) zfs_error(hdl, EZFS_BADCACHE,
2039                     dgettext(TEXT_DOMAIN, "failed to open cache file"));
2040                 return (NULL);
2041         }
2042
2043         if (fstat64(fd, &statbuf) != 0) {
2044                 zfs_error_aux(hdl, "%s", strerror(errno));
2045                 (void) close(fd);
2046                 (void) zfs_error(hdl, EZFS_BADCACHE,
2047                     dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
2048                 return (NULL);
2049         }
2050
2051         if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
2052                 (void) close(fd);
2053                 return (NULL);
2054         }
2055
2056         if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2057                 (void) close(fd);
2058                 free(buf);
2059                 (void) zfs_error(hdl, EZFS_BADCACHE,
2060                     dgettext(TEXT_DOMAIN,
2061                     "failed to read cache file contents"));
2062                 return (NULL);
2063         }
2064
2065         (void) close(fd);
2066
2067         if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
2068                 free(buf);
2069                 (void) zfs_error(hdl, EZFS_BADCACHE,
2070                     dgettext(TEXT_DOMAIN,
2071                     "invalid or corrupt cache file contents"));
2072                 return (NULL);
2073         }
2074
2075         free(buf);
2076
2077         /*
2078          * Go through and get the current state of the pools and refresh their
2079          * state.
2080          */
2081         if (nvlist_alloc(&pools, 0, 0) != 0) {
2082                 (void) no_memory(hdl);
2083                 nvlist_free(raw);
2084                 return (NULL);
2085         }
2086
2087         elem = NULL;
2088         while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
2089                 src = fnvpair_value_nvlist(elem);
2090
2091                 name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
2092                 if (poolname != NULL && strcmp(poolname, name) != 0)
2093                         continue;
2094
2095                 this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
2096                 if (guid != 0 && guid != this_guid)
2097                         continue;
2098
2099                 if (pool_active(hdl, name, this_guid, &active) != 0) {
2100                         nvlist_free(raw);
2101                         nvlist_free(pools);
2102                         return (NULL);
2103                 }
2104
2105                 if (active)
2106                         continue;
2107
2108                 if ((dst = refresh_config(hdl, src)) == NULL) {
2109                         nvlist_free(raw);
2110                         nvlist_free(pools);
2111                         return (NULL);
2112                 }
2113
2114                 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
2115                         (void) no_memory(hdl);
2116                         nvlist_free(dst);
2117                         nvlist_free(raw);
2118                         nvlist_free(pools);
2119                         return (NULL);
2120                 }
2121                 nvlist_free(dst);
2122         }
2123
2124         nvlist_free(raw);
2125         return (pools);
2126 }
2127
2128 static int
2129 name_or_guid_exists(zpool_handle_t *zhp, void *data)
2130 {
2131         importargs_t *import = data;
2132         int found = 0;
2133
2134         if (import->poolname != NULL) {
2135                 char *pool_name;
2136
2137                 verify(nvlist_lookup_string(zhp->zpool_config,
2138                     ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0);
2139                 if (strcmp(pool_name, import->poolname) == 0)
2140                         found = 1;
2141         } else {
2142                 uint64_t pool_guid;
2143
2144                 verify(nvlist_lookup_uint64(zhp->zpool_config,
2145                     ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0);
2146                 if (pool_guid == import->guid)
2147                         found = 1;
2148         }
2149
2150         zpool_close(zhp);
2151         return (found);
2152 }
2153
2154 nvlist_t *
2155 zpool_search_import(libzfs_handle_t *hdl, importargs_t *import)
2156 {
2157         verify(import->poolname == NULL || import->guid == 0);
2158
2159         if (import->unique)
2160                 import->exists = zpool_iter(hdl, name_or_guid_exists, import);
2161
2162         if (import->cachefile != NULL)
2163                 return (zpool_find_import_cached(hdl, import->cachefile,
2164                     import->poolname, import->guid));
2165
2166         return (zpool_find_import_impl(hdl, import));
2167 }
2168
2169 static boolean_t
2170 pool_match(nvlist_t *cfg, char *tgt)
2171 {
2172         uint64_t v, guid = strtoull(tgt, NULL, 0);
2173         char *s;
2174
2175         if (guid != 0) {
2176                 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
2177                         return (v == guid);
2178         } else {
2179                 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
2180                         return (strcmp(s, tgt) == 0);
2181         }
2182         return (B_FALSE);
2183 }
2184
2185 int
2186 zpool_tryimport(libzfs_handle_t *hdl, char *target, nvlist_t **configp,
2187     importargs_t *args)
2188 {
2189         nvlist_t *pools;
2190         nvlist_t *match = NULL;
2191         nvlist_t *config = NULL;
2192         char *name = NULL, *sepp = NULL;
2193         char sep = '\0';
2194         int count = 0;
2195         char *targetdup = strdup(target);
2196
2197         *configp = NULL;
2198
2199         if ((sepp = strpbrk(targetdup, "/@")) != NULL) {
2200                 sep = *sepp;
2201                 *sepp = '\0';
2202         }
2203
2204         pools = zpool_search_import(hdl, args);
2205
2206         if (pools != NULL) {
2207                 nvpair_t *elem = NULL;
2208                 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2209                         VERIFY0(nvpair_value_nvlist(elem, &config));
2210                         if (pool_match(config, targetdup)) {
2211                                 count++;
2212                                 if (match != NULL) {
2213                                         /* multiple matches found */
2214                                         continue;
2215                                 } else {
2216                                         match = config;
2217                                         name = nvpair_name(elem);
2218                                 }
2219                         }
2220                 }
2221         }
2222
2223         if (count == 0) {
2224                 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2225                     "no pools found"));
2226                 free(targetdup);
2227                 return (ENOENT);
2228         }
2229
2230         if (count > 1) {
2231                 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2232                     "%d pools found, use pool GUID\n"), count);
2233                 free(targetdup);
2234                 return (EINVAL);
2235         }
2236
2237         *configp = match;
2238         free(targetdup);
2239
2240         return (0);
2241 }
2242
2243 boolean_t
2244 find_guid(nvlist_t *nv, uint64_t guid)
2245 {
2246         uint64_t tmp;
2247         nvlist_t **child;
2248         uint_t c, children;
2249
2250         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
2251         if (tmp == guid)
2252                 return (B_TRUE);
2253
2254         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2255             &child, &children) == 0) {
2256                 for (c = 0; c < children; c++)
2257                         if (find_guid(child[c], guid))
2258                                 return (B_TRUE);
2259         }
2260
2261         return (B_FALSE);
2262 }
2263
2264 typedef struct aux_cbdata {
2265         const char      *cb_type;
2266         uint64_t        cb_guid;
2267         zpool_handle_t  *cb_zhp;
2268 } aux_cbdata_t;
2269
2270 static int
2271 find_aux(zpool_handle_t *zhp, void *data)
2272 {
2273         aux_cbdata_t *cbp = data;
2274         nvlist_t **list;
2275         uint_t i, count;
2276         uint64_t guid;
2277         nvlist_t *nvroot;
2278
2279         verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2280             &nvroot) == 0);
2281
2282         if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
2283             &list, &count) == 0) {
2284                 for (i = 0; i < count; i++) {
2285                         verify(nvlist_lookup_uint64(list[i],
2286                             ZPOOL_CONFIG_GUID, &guid) == 0);
2287                         if (guid == cbp->cb_guid) {
2288                                 cbp->cb_zhp = zhp;
2289                                 return (1);
2290                         }
2291                 }
2292         }
2293
2294         zpool_close(zhp);
2295         return (0);
2296 }
2297
2298 /*
2299  * Determines if the pool is in use.  If so, it returns true and the state of
2300  * the pool as well as the name of the pool.  Name string is allocated and
2301  * must be freed by the caller.
2302  */
2303 int
2304 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
2305     boolean_t *inuse)
2306 {
2307         nvlist_t *config;
2308         char *name;
2309         boolean_t ret;
2310         uint64_t guid, vdev_guid;
2311         zpool_handle_t *zhp;
2312         nvlist_t *pool_config;
2313         uint64_t stateval, isspare;
2314         aux_cbdata_t cb = { 0 };
2315         boolean_t isactive;
2316
2317         *inuse = B_FALSE;
2318
2319         if (zpool_read_label(fd, &config, NULL) != 0) {
2320                 (void) no_memory(hdl);
2321                 return (-1);
2322         }
2323
2324         if (config == NULL)
2325                 return (0);
2326
2327         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2328             &stateval) == 0);
2329         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
2330             &vdev_guid) == 0);
2331
2332         if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
2333                 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2334                     &name) == 0);
2335                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2336                     &guid) == 0);
2337         }
2338
2339         switch (stateval) {
2340         case POOL_STATE_EXPORTED:
2341                 /*
2342                  * A pool with an exported state may in fact be imported
2343                  * read-only, so check the in-core state to see if it's
2344                  * active and imported read-only.  If it is, set
2345                  * its state to active.
2346                  */
2347                 if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
2348                     (zhp = zpool_open_canfail(hdl, name)) != NULL) {
2349                         if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
2350                                 stateval = POOL_STATE_ACTIVE;
2351
2352                         /*
2353                          * All we needed the zpool handle for is the
2354                          * readonly prop check.
2355                          */
2356                         zpool_close(zhp);
2357                 }
2358
2359                 ret = B_TRUE;
2360                 break;
2361
2362         case POOL_STATE_ACTIVE:
2363                 /*
2364                  * For an active pool, we have to determine if it's really part
2365                  * of a currently active pool (in which case the pool will exist
2366                  * and the guid will be the same), or whether it's part of an
2367                  * active pool that was disconnected without being explicitly
2368                  * exported.
2369                  */
2370                 if (pool_active(hdl, name, guid, &isactive) != 0) {
2371                         nvlist_free(config);
2372                         return (-1);
2373                 }
2374
2375                 if (isactive) {
2376                         /*
2377                          * Because the device may have been removed while
2378                          * offlined, we only report it as active if the vdev is
2379                          * still present in the config.  Otherwise, pretend like
2380                          * it's not in use.
2381                          */
2382                         if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
2383                             (pool_config = zpool_get_config(zhp, NULL))
2384                             != NULL) {
2385                                 nvlist_t *nvroot;
2386
2387                                 verify(nvlist_lookup_nvlist(pool_config,
2388                                     ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2389                                 ret = find_guid(nvroot, vdev_guid);
2390                         } else {
2391                                 ret = B_FALSE;
2392                         }
2393
2394                         /*
2395                          * If this is an active spare within another pool, we
2396                          * treat it like an unused hot spare.  This allows the
2397                          * user to create a pool with a hot spare that currently
2398                          * in use within another pool.  Since we return B_TRUE,
2399                          * libdiskmgt will continue to prevent generic consumers
2400                          * from using the device.
2401                          */
2402                         if (ret && nvlist_lookup_uint64(config,
2403                             ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
2404                                 stateval = POOL_STATE_SPARE;
2405
2406                         if (zhp != NULL)
2407                                 zpool_close(zhp);
2408                 } else {
2409                         stateval = POOL_STATE_POTENTIALLY_ACTIVE;
2410                         ret = B_TRUE;
2411                 }
2412                 break;
2413
2414         case POOL_STATE_SPARE:
2415                 /*
2416                  * For a hot spare, it can be either definitively in use, or
2417                  * potentially active.  To determine if it's in use, we iterate
2418                  * over all pools in the system and search for one with a spare
2419                  * with a matching guid.
2420                  *
2421                  * Due to the shared nature of spares, we don't actually report
2422                  * the potentially active case as in use.  This means the user
2423                  * can freely create pools on the hot spares of exported pools,
2424                  * but to do otherwise makes the resulting code complicated, and
2425                  * we end up having to deal with this case anyway.
2426                  */
2427                 cb.cb_zhp = NULL;
2428                 cb.cb_guid = vdev_guid;
2429                 cb.cb_type = ZPOOL_CONFIG_SPARES;
2430                 if (zpool_iter(hdl, find_aux, &cb) == 1) {
2431                         name = (char *)zpool_get_name(cb.cb_zhp);
2432                         ret = B_TRUE;
2433                 } else {
2434                         ret = B_FALSE;
2435                 }
2436                 break;
2437
2438         case POOL_STATE_L2CACHE:
2439
2440                 /*
2441                  * Check if any pool is currently using this l2cache device.
2442                  */
2443                 cb.cb_zhp = NULL;
2444                 cb.cb_guid = vdev_guid;
2445                 cb.cb_type = ZPOOL_CONFIG_L2CACHE;
2446                 if (zpool_iter(hdl, find_aux, &cb) == 1) {
2447                         name = (char *)zpool_get_name(cb.cb_zhp);
2448                         ret = B_TRUE;
2449                 } else {
2450                         ret = B_FALSE;
2451                 }
2452                 break;
2453
2454         default:
2455                 ret = B_FALSE;
2456         }
2457
2458
2459         if (ret) {
2460                 if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
2461                         if (cb.cb_zhp)
2462                                 zpool_close(cb.cb_zhp);
2463                         nvlist_free(config);
2464                         return (-1);
2465                 }
2466                 *state = (pool_state_t)stateval;
2467         }
2468
2469         if (cb.cb_zhp)
2470                 zpool_close(cb.cb_zhp);
2471
2472         nvlist_free(config);
2473         *inuse = ret;
2474         return (0);
2475 }