]> git.proxmox.com Git - mirror_zfs-debian.git/blob - lib/libzfs/libzfs_import.c
Imported Upstream version 0.6.5.8
[mirror_zfs-debian.git] / 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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 */
26
27 /*
28 * Pool import support functions.
29 *
30 * To import a pool, we rely on reading the configuration information from the
31 * ZFS label of each device. If we successfully read the label, then we
32 * organize the configuration information in the following hierarchy:
33 *
34 * pool guid -> toplevel vdev guid -> label txg
35 *
36 * Duplicate entries matching this same tuple will be discarded. Once we have
37 * examined every device, we pick the best label txg config for each toplevel
38 * vdev. We then arrange these toplevel vdevs into a complete pool config, and
39 * update any paths that have changed. Finally, we attempt to import the pool
40 * using our derived config, and record the results.
41 */
42
43 #include <ctype.h>
44 #include <devid.h>
45 #include <dirent.h>
46 #include <errno.h>
47 #include <libintl.h>
48 #include <stddef.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <sys/vtoc.h>
55 #include <sys/dktp/fdisk.h>
56 #include <sys/efi_partition.h>
57
58 #include <sys/vdev_impl.h>
59 #ifdef HAVE_LIBBLKID
60 #include <blkid/blkid.h>
61 #endif
62
63 #include "libzfs.h"
64 #include "libzfs_impl.h"
65
66 /*
67 * Intermediate structures used to gather configuration information.
68 */
69 typedef struct config_entry {
70 uint64_t ce_txg;
71 nvlist_t *ce_config;
72 struct config_entry *ce_next;
73 } config_entry_t;
74
75 typedef struct vdev_entry {
76 uint64_t ve_guid;
77 config_entry_t *ve_configs;
78 struct vdev_entry *ve_next;
79 } vdev_entry_t;
80
81 typedef struct pool_entry {
82 uint64_t pe_guid;
83 vdev_entry_t *pe_vdevs;
84 struct pool_entry *pe_next;
85 } pool_entry_t;
86
87 typedef struct name_entry {
88 char *ne_name;
89 uint64_t ne_guid;
90 uint64_t ne_order;
91 uint64_t ne_num_labels;
92 struct name_entry *ne_next;
93 } name_entry_t;
94
95 typedef struct pool_list {
96 pool_entry_t *pools;
97 name_entry_t *names;
98 } pool_list_t;
99
100 #define DEV_BYID_PATH "/dev/disk/by-id/"
101
102 static char *
103 get_devid(const char *path)
104 {
105 int fd;
106 ddi_devid_t devid;
107 char *minor, *ret;
108
109 if ((fd = open(path, O_RDONLY)) < 0)
110 return (NULL);
111
112 minor = NULL;
113 ret = NULL;
114 if (devid_get(fd, &devid) == 0) {
115 if (devid_get_minor_name(fd, &minor) == 0)
116 ret = devid_str_encode(devid, minor);
117 if (minor != NULL)
118 devid_str_free(minor);
119 devid_free(devid);
120 }
121 (void) close(fd);
122
123 return (ret);
124 }
125
126 /*
127 * Wait up to timeout_ms for udev to set up the device node. The device is
128 * considered ready when the provided path have been verified to exist and
129 * it has been allowed to settle. At this point the device the device can
130 * be accessed reliably. Depending on the complexity of the udev rules thisi
131 * process could take several seconds.
132 */
133 int
134 zpool_label_disk_wait(char *path, int timeout_ms)
135 {
136 int settle_ms = 50;
137 long sleep_ms = 10;
138 hrtime_t start, settle;
139 struct stat64 statbuf;
140
141 start = gethrtime();
142 settle = 0;
143
144 do {
145 errno = 0;
146 if ((stat64(path, &statbuf) == 0) && (errno == 0)) {
147 if (settle == 0)
148 settle = gethrtime();
149 else if (NSEC2MSEC(gethrtime() - settle) >= settle_ms)
150 return (0);
151 } else if (errno != ENOENT) {
152 return (errno);
153 }
154
155 usleep(sleep_ms * MILLISEC);
156 } while (NSEC2MSEC(gethrtime() - start) < timeout_ms);
157
158 return (ENODEV);
159 }
160
161 /*
162 * Go through and fix up any path and/or devid information for the given vdev
163 * configuration.
164 */
165 static int
166 fix_paths(nvlist_t *nv, name_entry_t *names)
167 {
168 nvlist_t **child;
169 uint_t c, children;
170 uint64_t guid;
171 name_entry_t *ne, *best;
172 char *path, *devid;
173
174 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
175 &child, &children) == 0) {
176 for (c = 0; c < children; c++)
177 if (fix_paths(child[c], names) != 0)
178 return (-1);
179 return (0);
180 }
181
182 /*
183 * This is a leaf (file or disk) vdev. In either case, go through
184 * the name list and see if we find a matching guid. If so, replace
185 * the path and see if we can calculate a new devid.
186 *
187 * There may be multiple names associated with a particular guid, in
188 * which case we have overlapping partitions or multiple paths to the
189 * same disk. In this case we prefer to use the path name which
190 * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we
191 * use the lowest order device which corresponds to the first match
192 * while traversing the ZPOOL_IMPORT_PATH search path.
193 */
194 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
195 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
196 path = NULL;
197
198 best = NULL;
199 for (ne = names; ne != NULL; ne = ne->ne_next) {
200 if (ne->ne_guid == guid) {
201 if (path == NULL) {
202 best = ne;
203 break;
204 }
205
206 if ((strlen(path) == strlen(ne->ne_name)) &&
207 strncmp(path, ne->ne_name, strlen(path)) == 0) {
208 best = ne;
209 break;
210 }
211
212 if (best == NULL) {
213 best = ne;
214 continue;
215 }
216
217 /* Prefer paths with move vdev labels. */
218 if (ne->ne_num_labels > best->ne_num_labels) {
219 best = ne;
220 continue;
221 }
222
223 /* Prefer paths earlier in the search order. */
224 if (ne->ne_num_labels == best->ne_num_labels &&
225 ne->ne_order < best->ne_order) {
226 best = ne;
227 continue;
228 }
229 }
230 }
231
232 if (best == NULL)
233 return (0);
234
235 if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
236 return (-1);
237
238 if ((devid = get_devid(best->ne_name)) == NULL) {
239 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
240 } else {
241 if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
242 return (-1);
243 devid_str_free(devid);
244 }
245
246 return (0);
247 }
248
249 /*
250 * Add the given configuration to the list of known devices.
251 */
252 static int
253 add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
254 int order, int num_labels, nvlist_t *config)
255 {
256 uint64_t pool_guid, vdev_guid, top_guid, txg, state;
257 pool_entry_t *pe;
258 vdev_entry_t *ve;
259 config_entry_t *ce;
260 name_entry_t *ne;
261
262 /*
263 * If this is a hot spare not currently in use or level 2 cache
264 * device, add it to the list of names to translate, but don't do
265 * anything else.
266 */
267 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
268 &state) == 0 &&
269 (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
270 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
271 if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
272 return (-1);
273
274 if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
275 free(ne);
276 return (-1);
277 }
278 ne->ne_guid = vdev_guid;
279 ne->ne_order = order;
280 ne->ne_num_labels = num_labels;
281 ne->ne_next = pl->names;
282 pl->names = ne;
283 return (0);
284 }
285
286 /*
287 * If we have a valid config but cannot read any of these fields, then
288 * it means we have a half-initialized label. In vdev_label_init()
289 * we write a label with txg == 0 so that we can identify the device
290 * in case the user refers to the same disk later on. If we fail to
291 * create the pool, we'll be left with a label in this state
292 * which should not be considered part of a valid pool.
293 */
294 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
295 &pool_guid) != 0 ||
296 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
297 &vdev_guid) != 0 ||
298 nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
299 &top_guid) != 0 ||
300 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
301 &txg) != 0 || txg == 0) {
302 nvlist_free(config);
303 return (0);
304 }
305
306 /*
307 * First, see if we know about this pool. If not, then add it to the
308 * list of known pools.
309 */
310 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
311 if (pe->pe_guid == pool_guid)
312 break;
313 }
314
315 if (pe == NULL) {
316 if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
317 nvlist_free(config);
318 return (-1);
319 }
320 pe->pe_guid = pool_guid;
321 pe->pe_next = pl->pools;
322 pl->pools = pe;
323 }
324
325 /*
326 * Second, see if we know about this toplevel vdev. Add it if its
327 * missing.
328 */
329 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
330 if (ve->ve_guid == top_guid)
331 break;
332 }
333
334 if (ve == NULL) {
335 if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
336 nvlist_free(config);
337 return (-1);
338 }
339 ve->ve_guid = top_guid;
340 ve->ve_next = pe->pe_vdevs;
341 pe->pe_vdevs = ve;
342 }
343
344 /*
345 * Third, see if we have a config with a matching transaction group. If
346 * so, then we do nothing. Otherwise, add it to the list of known
347 * configs.
348 */
349 for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
350 if (ce->ce_txg == txg)
351 break;
352 }
353
354 if (ce == NULL) {
355 if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
356 nvlist_free(config);
357 return (-1);
358 }
359 ce->ce_txg = txg;
360 ce->ce_config = config;
361 ce->ce_next = ve->ve_configs;
362 ve->ve_configs = ce;
363 } else {
364 nvlist_free(config);
365 }
366
367 /*
368 * At this point we've successfully added our config to the list of
369 * known configs. The last thing to do is add the vdev guid -> path
370 * mappings so that we can fix up the configuration as necessary before
371 * doing the import.
372 */
373 if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
374 return (-1);
375
376 if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
377 free(ne);
378 return (-1);
379 }
380
381 ne->ne_guid = vdev_guid;
382 ne->ne_order = order;
383 ne->ne_num_labels = num_labels;
384 ne->ne_next = pl->names;
385 pl->names = ne;
386
387 return (0);
388 }
389
390 #ifdef HAVE_LIBBLKID
391 static int
392 add_path(libzfs_handle_t *hdl, pool_list_t *pools, uint64_t pool_guid,
393 uint64_t vdev_guid, const char *path, int order)
394 {
395 nvlist_t *label;
396 uint64_t guid;
397 int error, fd, num_labels;
398
399 fd = open64(path, O_RDONLY);
400 if (fd < 0)
401 return (errno);
402
403 error = zpool_read_label(fd, &label, &num_labels);
404 close(fd);
405
406 if (error || label == NULL)
407 return (ENOENT);
408
409 error = nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid);
410 if (error || guid != pool_guid) {
411 nvlist_free(label);
412 return (EINVAL);
413 }
414
415 error = nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid);
416 if (error || guid != vdev_guid) {
417 nvlist_free(label);
418 return (EINVAL);
419 }
420
421 error = add_config(hdl, pools, path, order, num_labels, label);
422
423 return (error);
424 }
425
426 static int
427 add_configs_from_label_impl(libzfs_handle_t *hdl, pool_list_t *pools,
428 nvlist_t *nvroot, uint64_t pool_guid, uint64_t vdev_guid)
429 {
430 char udevpath[MAXPATHLEN];
431 char *path;
432 nvlist_t **child;
433 uint_t c, children;
434 uint64_t guid;
435 int error;
436
437 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
438 &child, &children) == 0) {
439 for (c = 0; c < children; c++) {
440 error = add_configs_from_label_impl(hdl, pools,
441 child[c], pool_guid, vdev_guid);
442 if (error)
443 return (error);
444 }
445 return (0);
446 }
447
448 if (nvroot == NULL)
449 return (0);
450
451 error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid);
452 if ((error != 0) || (guid != vdev_guid))
453 return (0);
454
455 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &path);
456 if (error == 0)
457 (void) add_path(hdl, pools, pool_guid, vdev_guid, path, 0);
458
459 error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &path);
460 if (error == 0) {
461 sprintf(udevpath, "%s%s", DEV_BYID_PATH, path);
462 (void) add_path(hdl, pools, pool_guid, vdev_guid, udevpath, 1);
463 }
464
465 return (0);
466 }
467
468 /*
469 * Given a disk label call add_config() for all known paths to the device
470 * as described by the label itself. The paths are added in the following
471 * priority order: 'path', 'devid', 'devnode'. As these alternate paths are
472 * added the labels are verified to make sure they refer to the same device.
473 */
474 static int
475 add_configs_from_label(libzfs_handle_t *hdl, pool_list_t *pools,
476 char *devname, int num_labels, nvlist_t *label)
477 {
478 nvlist_t *nvroot;
479 uint64_t pool_guid;
480 uint64_t vdev_guid;
481 int error;
482
483 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
484 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
485 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid))
486 return (ENOENT);
487
488 /* Allow devlinks to stabilize so all paths are available. */
489 zpool_label_disk_wait(devname, DISK_LABEL_WAIT);
490
491 /* Add alternate paths as described by the label vdev_tree. */
492 (void) add_configs_from_label_impl(hdl, pools, nvroot,
493 pool_guid, vdev_guid);
494
495 /* Add the device node /dev/sdX path as a last resort. */
496 error = add_config(hdl, pools, devname, 100, num_labels, label);
497
498 return (error);
499 }
500 #endif /* HAVE_LIBBLKID */
501
502 /*
503 * Returns true if the named pool matches the given GUID.
504 */
505 static int
506 pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
507 boolean_t *isactive)
508 {
509 zpool_handle_t *zhp;
510 uint64_t theguid;
511
512 if (zpool_open_silent(hdl, name, &zhp) != 0)
513 return (-1);
514
515 if (zhp == NULL) {
516 *isactive = B_FALSE;
517 return (0);
518 }
519
520 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
521 &theguid) == 0);
522
523 zpool_close(zhp);
524
525 *isactive = (theguid == guid);
526 return (0);
527 }
528
529 static nvlist_t *
530 refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
531 {
532 nvlist_t *nvl;
533 zfs_cmd_t zc = {"\0"};
534 int err;
535
536 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
537 return (NULL);
538
539 if (zcmd_alloc_dst_nvlist(hdl, &zc,
540 zc.zc_nvlist_conf_size * 2) != 0) {
541 zcmd_free_nvlists(&zc);
542 return (NULL);
543 }
544
545 while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
546 &zc)) != 0 && errno == ENOMEM) {
547 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
548 zcmd_free_nvlists(&zc);
549 return (NULL);
550 }
551 }
552
553 if (err) {
554 zcmd_free_nvlists(&zc);
555 return (NULL);
556 }
557
558 if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
559 zcmd_free_nvlists(&zc);
560 return (NULL);
561 }
562
563 zcmd_free_nvlists(&zc);
564 return (nvl);
565 }
566
567 /*
568 * Determine if the vdev id is a hole in the namespace.
569 */
570 boolean_t
571 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
572 {
573 int c;
574
575 for (c = 0; c < holes; c++) {
576
577 /* Top-level is a hole */
578 if (hole_array[c] == id)
579 return (B_TRUE);
580 }
581 return (B_FALSE);
582 }
583
584 /*
585 * Convert our list of pools into the definitive set of configurations. We
586 * start by picking the best config for each toplevel vdev. Once that's done,
587 * we assemble the toplevel vdevs into a full config for the pool. We make a
588 * pass to fix up any incorrect paths, and then add it to the main list to
589 * return to the user.
590 */
591 static nvlist_t *
592 get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
593 {
594 pool_entry_t *pe;
595 vdev_entry_t *ve;
596 config_entry_t *ce;
597 nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
598 nvlist_t **spares, **l2cache;
599 uint_t i, nspares, nl2cache;
600 boolean_t config_seen;
601 uint64_t best_txg;
602 char *name, *hostname = NULL;
603 uint64_t guid;
604 uint_t children = 0;
605 nvlist_t **child = NULL;
606 uint_t holes;
607 uint64_t *hole_array, max_id;
608 uint_t c;
609 boolean_t isactive;
610 uint64_t hostid;
611 nvlist_t *nvl;
612 boolean_t valid_top_config = B_FALSE;
613
614 if (nvlist_alloc(&ret, 0, 0) != 0)
615 goto nomem;
616
617 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
618 uint64_t id, max_txg = 0;
619
620 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
621 goto nomem;
622 config_seen = B_FALSE;
623
624 /*
625 * Iterate over all toplevel vdevs. Grab the pool configuration
626 * from the first one we find, and then go through the rest and
627 * add them as necessary to the 'vdevs' member of the config.
628 */
629 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
630
631 /*
632 * Determine the best configuration for this vdev by
633 * selecting the config with the latest transaction
634 * group.
635 */
636 best_txg = 0;
637 for (ce = ve->ve_configs; ce != NULL;
638 ce = ce->ce_next) {
639
640 if (ce->ce_txg > best_txg) {
641 tmp = ce->ce_config;
642 best_txg = ce->ce_txg;
643 }
644 }
645
646 /*
647 * We rely on the fact that the max txg for the
648 * pool will contain the most up-to-date information
649 * about the valid top-levels in the vdev namespace.
650 */
651 if (best_txg > max_txg) {
652 (void) nvlist_remove(config,
653 ZPOOL_CONFIG_VDEV_CHILDREN,
654 DATA_TYPE_UINT64);
655 (void) nvlist_remove(config,
656 ZPOOL_CONFIG_HOLE_ARRAY,
657 DATA_TYPE_UINT64_ARRAY);
658
659 max_txg = best_txg;
660 hole_array = NULL;
661 holes = 0;
662 max_id = 0;
663 valid_top_config = B_FALSE;
664
665 if (nvlist_lookup_uint64(tmp,
666 ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
667 verify(nvlist_add_uint64(config,
668 ZPOOL_CONFIG_VDEV_CHILDREN,
669 max_id) == 0);
670 valid_top_config = B_TRUE;
671 }
672
673 if (nvlist_lookup_uint64_array(tmp,
674 ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
675 &holes) == 0) {
676 verify(nvlist_add_uint64_array(config,
677 ZPOOL_CONFIG_HOLE_ARRAY,
678 hole_array, holes) == 0);
679 }
680 }
681
682 if (!config_seen) {
683 /*
684 * Copy the relevant pieces of data to the pool
685 * configuration:
686 *
687 * version
688 * pool guid
689 * name
690 * comment (if available)
691 * pool state
692 * hostid (if available)
693 * hostname (if available)
694 */
695 uint64_t state, version;
696 char *comment = NULL;
697
698 version = fnvlist_lookup_uint64(tmp,
699 ZPOOL_CONFIG_VERSION);
700 fnvlist_add_uint64(config,
701 ZPOOL_CONFIG_VERSION, version);
702 guid = fnvlist_lookup_uint64(tmp,
703 ZPOOL_CONFIG_POOL_GUID);
704 fnvlist_add_uint64(config,
705 ZPOOL_CONFIG_POOL_GUID, guid);
706 name = fnvlist_lookup_string(tmp,
707 ZPOOL_CONFIG_POOL_NAME);
708 fnvlist_add_string(config,
709 ZPOOL_CONFIG_POOL_NAME, name);
710
711 if (nvlist_lookup_string(tmp,
712 ZPOOL_CONFIG_COMMENT, &comment) == 0)
713 fnvlist_add_string(config,
714 ZPOOL_CONFIG_COMMENT, comment);
715
716 state = fnvlist_lookup_uint64(tmp,
717 ZPOOL_CONFIG_POOL_STATE);
718 fnvlist_add_uint64(config,
719 ZPOOL_CONFIG_POOL_STATE, state);
720
721 hostid = 0;
722 if (nvlist_lookup_uint64(tmp,
723 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
724 fnvlist_add_uint64(config,
725 ZPOOL_CONFIG_HOSTID, hostid);
726 hostname = fnvlist_lookup_string(tmp,
727 ZPOOL_CONFIG_HOSTNAME);
728 fnvlist_add_string(config,
729 ZPOOL_CONFIG_HOSTNAME, hostname);
730 }
731
732 config_seen = B_TRUE;
733 }
734
735 /*
736 * Add this top-level vdev to the child array.
737 */
738 verify(nvlist_lookup_nvlist(tmp,
739 ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
740 verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
741 &id) == 0);
742
743 if (id >= children) {
744 nvlist_t **newchild;
745
746 newchild = zfs_alloc(hdl, (id + 1) *
747 sizeof (nvlist_t *));
748 if (newchild == NULL)
749 goto nomem;
750
751 for (c = 0; c < children; c++)
752 newchild[c] = child[c];
753
754 free(child);
755 child = newchild;
756 children = id + 1;
757 }
758 if (nvlist_dup(nvtop, &child[id], 0) != 0)
759 goto nomem;
760
761 }
762
763 /*
764 * If we have information about all the top-levels then
765 * clean up the nvlist which we've constructed. This
766 * means removing any extraneous devices that are
767 * beyond the valid range or adding devices to the end
768 * of our array which appear to be missing.
769 */
770 if (valid_top_config) {
771 if (max_id < children) {
772 for (c = max_id; c < children; c++)
773 nvlist_free(child[c]);
774 children = max_id;
775 } else if (max_id > children) {
776 nvlist_t **newchild;
777
778 newchild = zfs_alloc(hdl, (max_id) *
779 sizeof (nvlist_t *));
780 if (newchild == NULL)
781 goto nomem;
782
783 for (c = 0; c < children; c++)
784 newchild[c] = child[c];
785
786 free(child);
787 child = newchild;
788 children = max_id;
789 }
790 }
791
792 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
793 &guid) == 0);
794
795 /*
796 * The vdev namespace may contain holes as a result of
797 * device removal. We must add them back into the vdev
798 * tree before we process any missing devices.
799 */
800 if (holes > 0) {
801 ASSERT(valid_top_config);
802
803 for (c = 0; c < children; c++) {
804 nvlist_t *holey;
805
806 if (child[c] != NULL ||
807 !vdev_is_hole(hole_array, holes, c))
808 continue;
809
810 if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
811 0) != 0)
812 goto nomem;
813
814 /*
815 * Holes in the namespace are treated as
816 * "hole" top-level vdevs and have a
817 * special flag set on them.
818 */
819 if (nvlist_add_string(holey,
820 ZPOOL_CONFIG_TYPE,
821 VDEV_TYPE_HOLE) != 0 ||
822 nvlist_add_uint64(holey,
823 ZPOOL_CONFIG_ID, c) != 0 ||
824 nvlist_add_uint64(holey,
825 ZPOOL_CONFIG_GUID, 0ULL) != 0)
826 goto nomem;
827 child[c] = holey;
828 }
829 }
830
831 /*
832 * Look for any missing top-level vdevs. If this is the case,
833 * create a faked up 'missing' vdev as a placeholder. We cannot
834 * simply compress the child array, because the kernel performs
835 * certain checks to make sure the vdev IDs match their location
836 * in the configuration.
837 */
838 for (c = 0; c < children; c++) {
839 if (child[c] == NULL) {
840 nvlist_t *missing;
841 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
842 0) != 0)
843 goto nomem;
844 if (nvlist_add_string(missing,
845 ZPOOL_CONFIG_TYPE,
846 VDEV_TYPE_MISSING) != 0 ||
847 nvlist_add_uint64(missing,
848 ZPOOL_CONFIG_ID, c) != 0 ||
849 nvlist_add_uint64(missing,
850 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
851 nvlist_free(missing);
852 goto nomem;
853 }
854 child[c] = missing;
855 }
856 }
857
858 /*
859 * Put all of this pool's top-level vdevs into a root vdev.
860 */
861 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
862 goto nomem;
863 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
864 VDEV_TYPE_ROOT) != 0 ||
865 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
866 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
867 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
868 child, children) != 0) {
869 nvlist_free(nvroot);
870 goto nomem;
871 }
872
873 for (c = 0; c < children; c++)
874 nvlist_free(child[c]);
875 free(child);
876 children = 0;
877 child = NULL;
878
879 /*
880 * Go through and fix up any paths and/or devids based on our
881 * known list of vdev GUID -> path mappings.
882 */
883 if (fix_paths(nvroot, pl->names) != 0) {
884 nvlist_free(nvroot);
885 goto nomem;
886 }
887
888 /*
889 * Add the root vdev to this pool's configuration.
890 */
891 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
892 nvroot) != 0) {
893 nvlist_free(nvroot);
894 goto nomem;
895 }
896 nvlist_free(nvroot);
897
898 /*
899 * zdb uses this path to report on active pools that were
900 * imported or created using -R.
901 */
902 if (active_ok)
903 goto add_pool;
904
905 /*
906 * Determine if this pool is currently active, in which case we
907 * can't actually import it.
908 */
909 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
910 &name) == 0);
911 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
912 &guid) == 0);
913
914 if (pool_active(hdl, name, guid, &isactive) != 0)
915 goto error;
916
917 if (isactive) {
918 nvlist_free(config);
919 config = NULL;
920 continue;
921 }
922
923 if ((nvl = refresh_config(hdl, config)) == NULL) {
924 nvlist_free(config);
925 config = NULL;
926 continue;
927 }
928
929 nvlist_free(config);
930 config = nvl;
931
932 /*
933 * Go through and update the paths for spares, now that we have
934 * them.
935 */
936 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
937 &nvroot) == 0);
938 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
939 &spares, &nspares) == 0) {
940 for (i = 0; i < nspares; i++) {
941 if (fix_paths(spares[i], pl->names) != 0)
942 goto nomem;
943 }
944 }
945
946 /*
947 * Update the paths for l2cache devices.
948 */
949 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
950 &l2cache, &nl2cache) == 0) {
951 for (i = 0; i < nl2cache; i++) {
952 if (fix_paths(l2cache[i], pl->names) != 0)
953 goto nomem;
954 }
955 }
956
957 /*
958 * Restore the original information read from the actual label.
959 */
960 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
961 DATA_TYPE_UINT64);
962 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
963 DATA_TYPE_STRING);
964 if (hostid != 0) {
965 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
966 hostid) == 0);
967 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
968 hostname) == 0);
969 }
970
971 add_pool:
972 /*
973 * Add this pool to the list of configs.
974 */
975 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
976 &name) == 0);
977 if (nvlist_add_nvlist(ret, name, config) != 0)
978 goto nomem;
979
980 nvlist_free(config);
981 config = NULL;
982 }
983
984 return (ret);
985
986 nomem:
987 (void) no_memory(hdl);
988 error:
989 nvlist_free(config);
990 nvlist_free(ret);
991 for (c = 0; c < children; c++)
992 nvlist_free(child[c]);
993 free(child);
994
995 return (NULL);
996 }
997
998 /*
999 * Return the offset of the given label.
1000 */
1001 static uint64_t
1002 label_offset(uint64_t size, int l)
1003 {
1004 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
1005 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
1006 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
1007 }
1008
1009 /*
1010 * Given a file descriptor, read the label information and return an nvlist
1011 * describing the configuration, if there is one. The number of valid
1012 * labels found will be returned in num_labels when non-NULL.
1013 */
1014 int
1015 zpool_read_label(int fd, nvlist_t **config, int *num_labels)
1016 {
1017 struct stat64 statbuf;
1018 int l, count = 0;
1019 vdev_label_t *label;
1020 nvlist_t *expected_config = NULL;
1021 uint64_t expected_guid = 0, size;
1022
1023 *config = NULL;
1024
1025 if (fstat64_blk(fd, &statbuf) == -1)
1026 return (0);
1027 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1028
1029 if ((label = malloc(sizeof (vdev_label_t))) == NULL)
1030 return (-1);
1031
1032 for (l = 0; l < VDEV_LABELS; l++) {
1033 uint64_t state, guid, txg;
1034
1035 if (pread64(fd, label, sizeof (vdev_label_t),
1036 label_offset(size, l)) != sizeof (vdev_label_t))
1037 continue;
1038
1039 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
1040 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
1041 continue;
1042
1043 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID,
1044 &guid) != 0 || guid == 0) {
1045 nvlist_free(*config);
1046 continue;
1047 }
1048
1049 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
1050 &state) != 0 || state > POOL_STATE_L2CACHE) {
1051 nvlist_free(*config);
1052 continue;
1053 }
1054
1055 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1056 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
1057 &txg) != 0 || txg == 0)) {
1058 nvlist_free(*config);
1059 continue;
1060 }
1061
1062 if (expected_guid) {
1063 if (expected_guid == guid)
1064 count++;
1065
1066 nvlist_free(*config);
1067 } else {
1068 expected_config = *config;
1069 expected_guid = guid;
1070 count++;
1071 }
1072 }
1073
1074 if (num_labels != NULL)
1075 *num_labels = count;
1076
1077 free(label);
1078 *config = expected_config;
1079
1080 return (0);
1081 }
1082
1083 /*
1084 * Given a file descriptor, clear (zero) the label information. This function
1085 * is used in the appliance stack as part of the ZFS sysevent module and
1086 * to implement the "zpool labelclear" command.
1087 */
1088 int
1089 zpool_clear_label(int fd)
1090 {
1091 struct stat64 statbuf;
1092 int l;
1093 vdev_label_t *label;
1094 uint64_t size;
1095
1096 if (fstat64_blk(fd, &statbuf) == -1)
1097 return (0);
1098 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1099
1100 if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
1101 return (-1);
1102
1103 for (l = 0; l < VDEV_LABELS; l++) {
1104 if (pwrite64(fd, label, sizeof (vdev_label_t),
1105 label_offset(size, l)) != sizeof (vdev_label_t)) {
1106 free(label);
1107 return (-1);
1108 }
1109 }
1110
1111 free(label);
1112 return (0);
1113 }
1114
1115 #ifdef HAVE_LIBBLKID
1116 /*
1117 * Use libblkid to quickly search for zfs devices
1118 */
1119 static int
1120 zpool_find_import_blkid(libzfs_handle_t *hdl, pool_list_t *pools)
1121 {
1122 blkid_cache cache;
1123 blkid_dev_iterate iter;
1124 blkid_dev dev;
1125 int err;
1126
1127 err = blkid_get_cache(&cache, NULL);
1128 if (err != 0) {
1129 (void) zfs_error_fmt(hdl, EZFS_BADCACHE,
1130 dgettext(TEXT_DOMAIN, "blkid_get_cache() %d"), err);
1131 goto err_blkid1;
1132 }
1133
1134 err = blkid_probe_all(cache);
1135 if (err != 0) {
1136 (void) zfs_error_fmt(hdl, EZFS_BADCACHE,
1137 dgettext(TEXT_DOMAIN, "blkid_probe_all() %d"), err);
1138 goto err_blkid2;
1139 }
1140
1141 iter = blkid_dev_iterate_begin(cache);
1142 if (iter == NULL) {
1143 (void) zfs_error_fmt(hdl, EZFS_BADCACHE,
1144 dgettext(TEXT_DOMAIN, "blkid_dev_iterate_begin()"));
1145 goto err_blkid2;
1146 }
1147
1148 err = blkid_dev_set_search(iter, "TYPE", "zfs_member");
1149 if (err != 0) {
1150 (void) zfs_error_fmt(hdl, EZFS_BADCACHE,
1151 dgettext(TEXT_DOMAIN, "blkid_dev_set_search() %d"), err);
1152 goto err_blkid3;
1153 }
1154
1155 while (blkid_dev_next(iter, &dev) == 0) {
1156 nvlist_t *label;
1157 char *devname;
1158 int fd, num_labels;
1159
1160 devname = (char *) blkid_dev_devname(dev);
1161 if ((fd = open64(devname, O_RDONLY)) < 0)
1162 continue;
1163
1164 err = zpool_read_label(fd, &label, &num_labels);
1165 (void) close(fd);
1166
1167 if (err || label == NULL)
1168 continue;
1169
1170 add_configs_from_label(hdl, pools, devname, num_labels, label);
1171 }
1172 err = 0;
1173
1174 err_blkid3:
1175 blkid_dev_iterate_end(iter);
1176 err_blkid2:
1177 blkid_put_cache(cache);
1178 err_blkid1:
1179 return (err);
1180 }
1181 #endif /* HAVE_LIBBLKID */
1182
1183 char *
1184 zpool_default_import_path[DEFAULT_IMPORT_PATH_SIZE] = {
1185 "/dev/disk/by-vdev", /* Custom rules, use first if they exist */
1186 "/dev/mapper", /* Use multipath devices before components */
1187 "/dev/disk/by-uuid", /* Single unique entry and persistent */
1188 "/dev/disk/by-id", /* May be multiple entries and persistent */
1189 "/dev/disk/by-path", /* Encodes physical location and persistent */
1190 "/dev/disk/by-label", /* Custom persistent labels */
1191 "/dev" /* UNSAFE device names will change */
1192 };
1193
1194 /*
1195 * Given a list of directories to search, find all pools stored on disk. This
1196 * includes partial pools which are not available to import. If no args are
1197 * given (argc is 0), then the default directory (/dev/dsk) is searched.
1198 * poolname or guid (but not both) are provided by the caller when trying
1199 * to import a specific pool.
1200 */
1201 static nvlist_t *
1202 zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg)
1203 {
1204 int i, num_labels, dirs = iarg->paths;
1205 DIR *dirp = NULL;
1206 struct dirent64 *dp;
1207 char path[MAXPATHLEN];
1208 char *end, **dir = iarg->path;
1209 size_t pathleft;
1210 struct stat64 statbuf;
1211 nvlist_t *ret = NULL, *config;
1212 int fd;
1213 pool_list_t pools = { 0 };
1214 pool_entry_t *pe, *penext;
1215 vdev_entry_t *ve, *venext;
1216 config_entry_t *ce, *cenext;
1217 name_entry_t *ne, *nenext;
1218
1219 verify(iarg->poolname == NULL || iarg->guid == 0);
1220
1221 if (dirs == 0) {
1222 #ifdef HAVE_LIBBLKID
1223 /* Use libblkid to scan all device for their type */
1224 if (zpool_find_import_blkid(hdl, &pools) == 0)
1225 goto skip_scanning;
1226
1227 (void) zfs_error_fmt(hdl, EZFS_BADCACHE,
1228 dgettext(TEXT_DOMAIN, "blkid failure falling back "
1229 "to manual probing"));
1230 #endif /* HAVE_LIBBLKID */
1231
1232 dir = zpool_default_import_path;
1233 dirs = DEFAULT_IMPORT_PATH_SIZE;
1234 }
1235
1236 /*
1237 * Go through and read the label configuration information from every
1238 * possible device, organizing the information according to pool GUID
1239 * and toplevel GUID.
1240 */
1241 for (i = 0; i < dirs; i++) {
1242 char *rdsk;
1243 int dfd;
1244
1245 /* use realpath to normalize the path */
1246 if (realpath(dir[i], path) == 0) {
1247
1248 /* it is safe to skip missing search paths */
1249 if (errno == ENOENT)
1250 continue;
1251
1252 zfs_error_aux(hdl, strerror(errno));
1253 (void) zfs_error_fmt(hdl, EZFS_BADPATH,
1254 dgettext(TEXT_DOMAIN, "cannot open '%s'"), dir[i]);
1255 goto error;
1256 }
1257 end = &path[strlen(path)];
1258 *end++ = '/';
1259 *end = 0;
1260 pathleft = &path[sizeof (path)] - end;
1261
1262 /*
1263 * Using raw devices instead of block devices when we're
1264 * reading the labels skips a bunch of slow operations during
1265 * close(2) processing, so we replace /dev/dsk with /dev/rdsk.
1266 */
1267 if (strcmp(path, "/dev/dsk/") == 0)
1268 rdsk = "/dev/rdsk/";
1269 else
1270 rdsk = path;
1271
1272 if ((dfd = open64(rdsk, O_RDONLY)) < 0 ||
1273 (dirp = fdopendir(dfd)) == NULL) {
1274 zfs_error_aux(hdl, strerror(errno));
1275 (void) zfs_error_fmt(hdl, EZFS_BADPATH,
1276 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1277 rdsk);
1278 goto error;
1279 }
1280
1281 /*
1282 * This is not MT-safe, but we have no MT consumers of libzfs
1283 */
1284 while ((dp = readdir64(dirp)) != NULL) {
1285 const char *name = dp->d_name;
1286 if (name[0] == '.' &&
1287 (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1288 continue;
1289
1290 /*
1291 * Skip checking devices with well known prefixes:
1292 * watchdog - A special close is required to avoid
1293 * triggering it and resetting the system.
1294 * fuse - Fuse control device.
1295 * ppp - Generic PPP driver.
1296 * tty* - Generic serial interface.
1297 * vcs* - Virtual console memory.
1298 * parport* - Parallel port interface.
1299 * lp* - Printer interface.
1300 * fd* - Floppy interface.
1301 * hpet - High Precision Event Timer, crashes qemu
1302 * when accessed from a virtual machine.
1303 * core - Symlink to /proc/kcore, causes a crash
1304 * when access from Xen dom0.
1305 */
1306 if ((strncmp(name, "watchdog", 8) == 0) ||
1307 (strncmp(name, "fuse", 4) == 0) ||
1308 (strncmp(name, "ppp", 3) == 0) ||
1309 (strncmp(name, "tty", 3) == 0) ||
1310 (strncmp(name, "vcs", 3) == 0) ||
1311 (strncmp(name, "parport", 7) == 0) ||
1312 (strncmp(name, "lp", 2) == 0) ||
1313 (strncmp(name, "fd", 2) == 0) ||
1314 (strncmp(name, "hpet", 4) == 0) ||
1315 (strncmp(name, "core", 4) == 0))
1316 continue;
1317
1318 /*
1319 * Ignore failed stats. We only want regular
1320 * files and block devices.
1321 */
1322 if ((fstatat64(dfd, name, &statbuf, 0) != 0) ||
1323 (!S_ISREG(statbuf.st_mode) &&
1324 !S_ISBLK(statbuf.st_mode)))
1325 continue;
1326
1327 if ((fd = openat64(dfd, name, O_RDONLY)) < 0)
1328 continue;
1329
1330 if ((zpool_read_label(fd, &config, &num_labels))) {
1331 (void) close(fd);
1332 (void) no_memory(hdl);
1333 goto error;
1334 }
1335
1336 (void) close(fd);
1337
1338 if (config != NULL) {
1339 boolean_t matched = B_TRUE;
1340 char *pname;
1341
1342 if ((iarg->poolname != NULL) &&
1343 (nvlist_lookup_string(config,
1344 ZPOOL_CONFIG_POOL_NAME, &pname) == 0)) {
1345
1346 if (strcmp(iarg->poolname, pname))
1347 matched = B_FALSE;
1348
1349 } else if (iarg->guid != 0) {
1350 uint64_t this_guid;
1351
1352 matched = nvlist_lookup_uint64(config,
1353 ZPOOL_CONFIG_POOL_GUID,
1354 &this_guid) == 0 &&
1355 iarg->guid == this_guid;
1356 }
1357 if (!matched) {
1358 nvlist_free(config);
1359 config = NULL;
1360 continue;
1361 }
1362 /* use the non-raw path for the config */
1363 (void) strlcpy(end, name, pathleft);
1364 if (add_config(hdl, &pools, path, i+1,
1365 num_labels, config))
1366 goto error;
1367 }
1368 }
1369
1370 (void) closedir(dirp);
1371 dirp = NULL;
1372 }
1373
1374 #ifdef HAVE_LIBBLKID
1375 skip_scanning:
1376 #endif
1377 ret = get_configs(hdl, &pools, iarg->can_be_active);
1378
1379 error:
1380 for (pe = pools.pools; pe != NULL; pe = penext) {
1381 penext = pe->pe_next;
1382 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1383 venext = ve->ve_next;
1384 for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1385 cenext = ce->ce_next;
1386 if (ce->ce_config)
1387 nvlist_free(ce->ce_config);
1388 free(ce);
1389 }
1390 free(ve);
1391 }
1392 free(pe);
1393 }
1394
1395 for (ne = pools.names; ne != NULL; ne = nenext) {
1396 nenext = ne->ne_next;
1397 if (ne->ne_name)
1398 free(ne->ne_name);
1399 free(ne);
1400 }
1401
1402 if (dirp)
1403 (void) closedir(dirp);
1404
1405 return (ret);
1406 }
1407
1408 nvlist_t *
1409 zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
1410 {
1411 importargs_t iarg = { 0 };
1412
1413 iarg.paths = argc;
1414 iarg.path = argv;
1415
1416 return (zpool_find_import_impl(hdl, &iarg));
1417 }
1418
1419 /*
1420 * Given a cache file, return the contents as a list of importable pools.
1421 * poolname or guid (but not both) are provided by the caller when trying
1422 * to import a specific pool.
1423 */
1424 nvlist_t *
1425 zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
1426 char *poolname, uint64_t guid)
1427 {
1428 char *buf;
1429 int fd;
1430 struct stat64 statbuf;
1431 nvlist_t *raw, *src, *dst;
1432 nvlist_t *pools;
1433 nvpair_t *elem;
1434 char *name;
1435 uint64_t this_guid;
1436 boolean_t active;
1437
1438 verify(poolname == NULL || guid == 0);
1439
1440 if ((fd = open(cachefile, O_RDONLY)) < 0) {
1441 zfs_error_aux(hdl, "%s", strerror(errno));
1442 (void) zfs_error(hdl, EZFS_BADCACHE,
1443 dgettext(TEXT_DOMAIN, "failed to open cache file"));
1444 return (NULL);
1445 }
1446
1447 if (fstat64(fd, &statbuf) != 0) {
1448 zfs_error_aux(hdl, "%s", strerror(errno));
1449 (void) close(fd);
1450 (void) zfs_error(hdl, EZFS_BADCACHE,
1451 dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
1452 return (NULL);
1453 }
1454
1455 if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
1456 (void) close(fd);
1457 return (NULL);
1458 }
1459
1460 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1461 (void) close(fd);
1462 free(buf);
1463 (void) zfs_error(hdl, EZFS_BADCACHE,
1464 dgettext(TEXT_DOMAIN,
1465 "failed to read cache file contents"));
1466 return (NULL);
1467 }
1468
1469 (void) close(fd);
1470
1471 if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1472 free(buf);
1473 (void) zfs_error(hdl, EZFS_BADCACHE,
1474 dgettext(TEXT_DOMAIN,
1475 "invalid or corrupt cache file contents"));
1476 return (NULL);
1477 }
1478
1479 free(buf);
1480
1481 /*
1482 * Go through and get the current state of the pools and refresh their
1483 * state.
1484 */
1485 if (nvlist_alloc(&pools, 0, 0) != 0) {
1486 (void) no_memory(hdl);
1487 nvlist_free(raw);
1488 return (NULL);
1489 }
1490
1491 elem = NULL;
1492 while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1493 src = fnvpair_value_nvlist(elem);
1494
1495 name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME);
1496 if (poolname != NULL && strcmp(poolname, name) != 0)
1497 continue;
1498
1499 this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID);
1500 if (guid != 0 && guid != this_guid)
1501 continue;
1502
1503 if (pool_active(hdl, name, this_guid, &active) != 0) {
1504 nvlist_free(raw);
1505 nvlist_free(pools);
1506 return (NULL);
1507 }
1508
1509 if (active)
1510 continue;
1511
1512 if ((dst = refresh_config(hdl, src)) == NULL) {
1513 nvlist_free(raw);
1514 nvlist_free(pools);
1515 return (NULL);
1516 }
1517
1518 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1519 (void) no_memory(hdl);
1520 nvlist_free(dst);
1521 nvlist_free(raw);
1522 nvlist_free(pools);
1523 return (NULL);
1524 }
1525 nvlist_free(dst);
1526 }
1527
1528 nvlist_free(raw);
1529 return (pools);
1530 }
1531
1532 static int
1533 name_or_guid_exists(zpool_handle_t *zhp, void *data)
1534 {
1535 importargs_t *import = data;
1536 int found = 0;
1537
1538 if (import->poolname != NULL) {
1539 char *pool_name;
1540
1541 verify(nvlist_lookup_string(zhp->zpool_config,
1542 ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0);
1543 if (strcmp(pool_name, import->poolname) == 0)
1544 found = 1;
1545 } else {
1546 uint64_t pool_guid;
1547
1548 verify(nvlist_lookup_uint64(zhp->zpool_config,
1549 ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0);
1550 if (pool_guid == import->guid)
1551 found = 1;
1552 }
1553
1554 zpool_close(zhp);
1555 return (found);
1556 }
1557
1558 nvlist_t *
1559 zpool_search_import(libzfs_handle_t *hdl, importargs_t *import)
1560 {
1561 verify(import->poolname == NULL || import->guid == 0);
1562
1563 if (import->unique)
1564 import->exists = zpool_iter(hdl, name_or_guid_exists, import);
1565
1566 if (import->cachefile != NULL)
1567 return (zpool_find_import_cached(hdl, import->cachefile,
1568 import->poolname, import->guid));
1569
1570 return (zpool_find_import_impl(hdl, import));
1571 }
1572
1573 boolean_t
1574 find_guid(nvlist_t *nv, uint64_t guid)
1575 {
1576 uint64_t tmp;
1577 nvlist_t **child;
1578 uint_t c, children;
1579
1580 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
1581 if (tmp == guid)
1582 return (B_TRUE);
1583
1584 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1585 &child, &children) == 0) {
1586 for (c = 0; c < children; c++)
1587 if (find_guid(child[c], guid))
1588 return (B_TRUE);
1589 }
1590
1591 return (B_FALSE);
1592 }
1593
1594 typedef struct aux_cbdata {
1595 const char *cb_type;
1596 uint64_t cb_guid;
1597 zpool_handle_t *cb_zhp;
1598 } aux_cbdata_t;
1599
1600 static int
1601 find_aux(zpool_handle_t *zhp, void *data)
1602 {
1603 aux_cbdata_t *cbp = data;
1604 nvlist_t **list;
1605 uint_t i, count;
1606 uint64_t guid;
1607 nvlist_t *nvroot;
1608
1609 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1610 &nvroot) == 0);
1611
1612 if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
1613 &list, &count) == 0) {
1614 for (i = 0; i < count; i++) {
1615 verify(nvlist_lookup_uint64(list[i],
1616 ZPOOL_CONFIG_GUID, &guid) == 0);
1617 if (guid == cbp->cb_guid) {
1618 cbp->cb_zhp = zhp;
1619 return (1);
1620 }
1621 }
1622 }
1623
1624 zpool_close(zhp);
1625 return (0);
1626 }
1627
1628 /*
1629 * Determines if the pool is in use. If so, it returns true and the state of
1630 * the pool as well as the name of the pool. Both strings are allocated and
1631 * must be freed by the caller.
1632 */
1633 int
1634 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
1635 boolean_t *inuse)
1636 {
1637 nvlist_t *config;
1638 char *name;
1639 boolean_t ret;
1640 uint64_t guid, vdev_guid;
1641 zpool_handle_t *zhp;
1642 nvlist_t *pool_config;
1643 uint64_t stateval, isspare;
1644 aux_cbdata_t cb = { 0 };
1645 boolean_t isactive;
1646
1647 *inuse = B_FALSE;
1648
1649 if (zpool_read_label(fd, &config, NULL) != 0) {
1650 (void) no_memory(hdl);
1651 return (-1);
1652 }
1653
1654 if (config == NULL)
1655 return (0);
1656
1657 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1658 &stateval) == 0);
1659 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
1660 &vdev_guid) == 0);
1661
1662 if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
1663 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1664 &name) == 0);
1665 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1666 &guid) == 0);
1667 }
1668
1669 switch (stateval) {
1670 case POOL_STATE_EXPORTED:
1671 /*
1672 * A pool with an exported state may in fact be imported
1673 * read-only, so check the in-core state to see if it's
1674 * active and imported read-only. If it is, set
1675 * its state to active.
1676 */
1677 if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
1678 (zhp = zpool_open_canfail(hdl, name)) != NULL) {
1679 if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
1680 stateval = POOL_STATE_ACTIVE;
1681
1682 /*
1683 * All we needed the zpool handle for is the
1684 * readonly prop check.
1685 */
1686 zpool_close(zhp);
1687 }
1688
1689 ret = B_TRUE;
1690 break;
1691
1692 case POOL_STATE_ACTIVE:
1693 /*
1694 * For an active pool, we have to determine if it's really part
1695 * of a currently active pool (in which case the pool will exist
1696 * and the guid will be the same), or whether it's part of an
1697 * active pool that was disconnected without being explicitly
1698 * exported.
1699 */
1700 if (pool_active(hdl, name, guid, &isactive) != 0) {
1701 nvlist_free(config);
1702 return (-1);
1703 }
1704
1705 if (isactive) {
1706 /*
1707 * Because the device may have been removed while
1708 * offlined, we only report it as active if the vdev is
1709 * still present in the config. Otherwise, pretend like
1710 * it's not in use.
1711 */
1712 if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
1713 (pool_config = zpool_get_config(zhp, NULL))
1714 != NULL) {
1715 nvlist_t *nvroot;
1716
1717 verify(nvlist_lookup_nvlist(pool_config,
1718 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1719 ret = find_guid(nvroot, vdev_guid);
1720 } else {
1721 ret = B_FALSE;
1722 }
1723
1724 /*
1725 * If this is an active spare within another pool, we
1726 * treat it like an unused hot spare. This allows the
1727 * user to create a pool with a hot spare that currently
1728 * in use within another pool. Since we return B_TRUE,
1729 * libdiskmgt will continue to prevent generic consumers
1730 * from using the device.
1731 */
1732 if (ret && nvlist_lookup_uint64(config,
1733 ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
1734 stateval = POOL_STATE_SPARE;
1735
1736 if (zhp != NULL)
1737 zpool_close(zhp);
1738 } else {
1739 stateval = POOL_STATE_POTENTIALLY_ACTIVE;
1740 ret = B_TRUE;
1741 }
1742 break;
1743
1744 case POOL_STATE_SPARE:
1745 /*
1746 * For a hot spare, it can be either definitively in use, or
1747 * potentially active. To determine if it's in use, we iterate
1748 * over all pools in the system and search for one with a spare
1749 * with a matching guid.
1750 *
1751 * Due to the shared nature of spares, we don't actually report
1752 * the potentially active case as in use. This means the user
1753 * can freely create pools on the hot spares of exported pools,
1754 * but to do otherwise makes the resulting code complicated, and
1755 * we end up having to deal with this case anyway.
1756 */
1757 cb.cb_zhp = NULL;
1758 cb.cb_guid = vdev_guid;
1759 cb.cb_type = ZPOOL_CONFIG_SPARES;
1760 if (zpool_iter(hdl, find_aux, &cb) == 1) {
1761 name = (char *)zpool_get_name(cb.cb_zhp);
1762 ret = TRUE;
1763 } else {
1764 ret = FALSE;
1765 }
1766 break;
1767
1768 case POOL_STATE_L2CACHE:
1769
1770 /*
1771 * Check if any pool is currently using this l2cache device.
1772 */
1773 cb.cb_zhp = NULL;
1774 cb.cb_guid = vdev_guid;
1775 cb.cb_type = ZPOOL_CONFIG_L2CACHE;
1776 if (zpool_iter(hdl, find_aux, &cb) == 1) {
1777 name = (char *)zpool_get_name(cb.cb_zhp);
1778 ret = TRUE;
1779 } else {
1780 ret = FALSE;
1781 }
1782 break;
1783
1784 default:
1785 ret = B_FALSE;
1786 }
1787
1788
1789 if (ret) {
1790 if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
1791 if (cb.cb_zhp)
1792 zpool_close(cb.cb_zhp);
1793 nvlist_free(config);
1794 return (-1);
1795 }
1796 *state = (pool_state_t)stateval;
1797 }
1798
1799 if (cb.cb_zhp)
1800 zpool_close(cb.cb_zhp);
1801
1802 nvlist_free(config);
1803 *inuse = ret;
1804 return (0);
1805 }