4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Functions to convert between a list of vdevs and an nvlist representing the
28 * configuration. Each entry in the list can be one of:
31 * disk=(path=..., devid=...)
40 * While the underlying implementation supports it, group vdevs cannot contain
41 * other group vdevs. All userland verification of devices is contained within
42 * this file. If successful, the nvlist returned can be passed directly to the
43 * kernel; we've done as much verification as possible in userland.
45 * Hot spares are a special case, and passed down as an array of disk vdevs, at
46 * the same level as the root of the vdev tree.
48 * The only function exported by this file is 'make_root_vdev'. The
49 * function performs several passes:
51 * 1. Construct the vdev specification. Performs syntax validation and
52 * makes sure each device is valid.
53 * 2. Check for devices in use. Using libblkid to make sure that no
54 * devices are also in use. Some can be overridden using the 'force'
55 * flag, others cannot.
56 * 3. Check for replication errors if the 'force' flag is not specified.
57 * validates that the replication level is consistent across the
59 * 4. Call libzfs to label any whole disks with an EFI label.
68 #include <libnvpair.h>
73 #include <sys/efi_partition.h>
76 #include <sys/mntent.h>
77 #include <uuid/uuid.h>
79 #include <blkid/blkid.h>
81 #define blkid_cache void *
82 #endif /* HAVE_LIBBLKID */
84 #include "zpool_util.h"
87 * For any given vdev specification, we can have multiple errors. The
88 * vdev_error() function keeps track of whether we have seen an error yet, and
89 * prints out a header if its the first error we've seen.
96 vdev_error(const char *fmt
, ...)
101 (void) fprintf(stderr
, gettext("invalid vdev specification\n"));
103 (void) fprintf(stderr
, gettext("use '-f' to override "
104 "the following errors:\n"));
106 (void) fprintf(stderr
, gettext("the following errors "
107 "must be manually repaired:\n"));
112 (void) vfprintf(stderr
, fmt
, ap
);
117 * Check that a file is valid. All we can do in this case is check that it's
118 * not in use by another pool, and not in use by swap.
121 check_file(const char *file
, boolean_t force
, boolean_t isspare
)
129 if ((fd
= open(file
, O_RDONLY
)) < 0)
132 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) == 0 && inuse
) {
136 case POOL_STATE_ACTIVE
:
137 desc
= gettext("active");
140 case POOL_STATE_EXPORTED
:
141 desc
= gettext("exported");
144 case POOL_STATE_POTENTIALLY_ACTIVE
:
145 desc
= gettext("potentially active");
149 desc
= gettext("unknown");
154 * Allow hot spares to be shared between pools.
156 if (state
== POOL_STATE_SPARE
&& isspare
)
159 if (state
== POOL_STATE_ACTIVE
||
160 state
== POOL_STATE_SPARE
|| !force
) {
162 case POOL_STATE_SPARE
:
163 vdev_error(gettext("%s is reserved as a hot "
164 "spare for pool %s\n"), file
, name
);
167 vdev_error(gettext("%s is part of %s pool "
168 "'%s'\n"), file
, desc
, name
);
184 (void) fprintf(stderr
, gettext("warning: device in use checking "
185 "failed: %s\n"), strerror(err
));
189 check_slice(const char *path
, blkid_cache cache
, int force
, boolean_t isspare
)
191 struct stat64 statbuf
;
195 #endif /* HAVE_LIBBLKID */
197 if (stat64(path
, &statbuf
) != 0) {
198 vdev_error(gettext("cannot stat %s: %s\n"),
199 path
, strerror(errno
));
204 /* No valid type detected device is safe to use */
205 value
= blkid_get_tag_value(cache
, "TYPE", path
);
210 * If libblkid detects a ZFS device, we check the device
211 * using check_file() to see if it's safe. The one safe
212 * case is a spare device shared between multiple pools.
214 if (strcmp(value
, "zfs") == 0) {
215 err
= check_file(path
, force
, isspare
);
221 vdev_error(gettext("%s contains a filesystem of "
222 "type '%s'\n"), path
, value
);
228 err
= check_file(path
, force
, isspare
);
229 #endif /* HAVE_LIBBLKID */
235 * Validate a whole disk. Iterate over all slices on the disk and make sure
236 * that none is in use by calling check_slice().
239 check_disk(const char *path
, blkid_cache cache
, int force
,
240 boolean_t isspare
, boolean_t iswholedisk
)
243 char slice_path
[MAXPATHLEN
];
247 /* This is not a wholedisk we only check the given partition */
249 return check_slice(path
, cache
, force
, isspare
);
252 * When the device is a whole disk try to read the efi partition
253 * label. If this is successful we safely check the all of the
254 * partitions. However, when it fails it may simply be because
255 * the disk is partitioned via the MBR. Since we currently can
256 * not easily decode the MBR return a failure and prompt to the
257 * user to use force option since we cannot check the partitions.
259 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
264 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
270 vdev_error(gettext("%s does not contain an EFI "
271 "label but it may contain partition\n"
272 "information in the MBR.\n"), path
);
278 * The primary efi partition label is damaged however the secondary
279 * label at the end of the device is intact. Rather than use this
280 * label we should play it safe and treat this as a non efi device.
282 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
287 /* Partitions will no be created using the backup */
290 vdev_error(gettext("%s contains a corrupt primary "
291 "EFI label.\n"), path
);
296 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
298 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
||
299 uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_guid
))
302 if (strncmp(path
, UDISK_ROOT
, strlen(UDISK_ROOT
)) == 0)
303 (void) snprintf(slice_path
, sizeof (slice_path
),
304 "%s%s%d", path
, "-part", i
+1);
306 (void) snprintf(slice_path
, sizeof (slice_path
),
307 "%s%s%d", path
, isdigit(path
[strlen(path
)-1]) ?
310 err
= check_slice(slice_path
, cache
, force
, isspare
);
322 check_device(const char *path
, boolean_t force
,
323 boolean_t isspare
, boolean_t iswholedisk
)
325 static blkid_cache cache
= NULL
;
329 * There is no easy way to add a correct blkid_put_cache() call,
330 * memory will be reclaimed when the command exits.
335 if ((err
= blkid_get_cache(&cache
, NULL
)) != 0) {
340 if ((err
= blkid_probe_all(cache
)) != 0) {
341 blkid_put_cache(cache
);
346 #endif /* HAVE_LIBBLKID */
348 return check_disk(path
, cache
, force
, isspare
, iswholedisk
);
352 * By "whole disk" we mean an entire physical disk (something we can
353 * label, toggle the write cache on, etc.) as opposed to the full
354 * capacity of a pseudo-device such as lofi or did. We act as if we
355 * are labeling the disk, which should be a pretty good test of whether
356 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
360 is_whole_disk(const char *path
)
362 struct dk_gpt
*label
;
365 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0)
367 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
377 * This may be a shorthand device path or it could be total gibberish.
378 * Check to see if it's a known device in /dev/, /dev/disk/by-id,
379 * /dev/disk/by-label, /dev/disk/by-path, /dev/disk/by-uuid, or
380 * /dev/disk/zpool/. As part of this check, see if we've been given
381 * an entire disk (minus the slice number).
384 is_shorthand_path(const char *arg
, char *path
,
385 struct stat64
*statbuf
, boolean_t
*wholedisk
)
387 if (zfs_resolve_shortname(arg
, path
, MAXPATHLEN
) == 0) {
388 *wholedisk
= is_whole_disk(path
);
389 if (*wholedisk
|| (stat64(path
, statbuf
) == 0))
393 strlcpy(path
, arg
, sizeof(path
));
394 memset(statbuf
, 0, sizeof(*statbuf
));
395 *wholedisk
= B_FALSE
;
401 * Create a leaf vdev. Determine if this is a file or a device. If it's a
402 * device, fill in the device id to make a complete nvlist. Valid forms for a
405 * /dev/xxx Complete disk path
406 * /xxx Full path to file
407 * xxx Shorthand for /dev/disk/yyy/xxx
410 make_leaf_vdev(nvlist_t
*props
, const char *arg
, uint64_t is_log
)
412 char path
[MAXPATHLEN
];
413 struct stat64 statbuf
;
414 nvlist_t
*vdev
= NULL
;
416 boolean_t wholedisk
= B_FALSE
;
420 * Determine what type of vdev this is, and put the full path into
421 * 'path'. We detect whether this is a device of file afterwards by
422 * checking the st_mode of the file.
426 * Complete device or file path. Exact type is determined by
427 * examining the file descriptor afterwards. Symbolic links
428 * are resolved to their real paths for the is_whole_disk()
429 * and S_ISBLK/S_ISREG type checks. However, we are careful
430 * to store the given path as ZPOOL_CONFIG_PATH to ensure we
431 * can leverage udev's persistent device labels.
433 if (realpath(arg
, path
) == NULL
) {
434 (void) fprintf(stderr
,
435 gettext("cannot resolve path '%s'\n"), arg
);
439 wholedisk
= is_whole_disk(path
);
440 if (!wholedisk
&& (stat64(path
, &statbuf
) != 0)) {
441 (void) fprintf(stderr
,
442 gettext("cannot open '%s': %s\n"),
443 path
, strerror(errno
));
447 /* After is_whole_disk() check restore original passed path */
448 strlcpy(path
, arg
, MAXPATHLEN
);
450 err
= is_shorthand_path(arg
, path
, &statbuf
, &wholedisk
);
453 * If we got ENOENT, then the user gave us
454 * gibberish, so try to direct them with a
455 * reasonable error message. Otherwise,
456 * regurgitate strerror() since it's the best we
460 (void) fprintf(stderr
,
461 gettext("cannot open '%s': no such "
462 "device in %s\n"), arg
, DISK_ROOT
);
463 (void) fprintf(stderr
,
464 gettext("must be a full path or "
465 "shorthand device name\n"));
468 (void) fprintf(stderr
,
469 gettext("cannot open '%s': %s\n"),
470 path
, strerror(errno
));
477 * Determine whether this is a device or a file.
479 if (wholedisk
|| S_ISBLK(statbuf
.st_mode
)) {
480 type
= VDEV_TYPE_DISK
;
481 } else if (S_ISREG(statbuf
.st_mode
)) {
482 type
= VDEV_TYPE_FILE
;
484 (void) fprintf(stderr
, gettext("cannot use '%s': must be a "
485 "block device or regular file\n"), path
);
490 * Finally, we have the complete device or file, and we know that it is
491 * acceptable to use. Construct the nvlist to describe this vdev. All
492 * vdevs have a 'path' element, and devices also have a 'devid' element.
494 verify(nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) == 0);
495 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_PATH
, path
) == 0);
496 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
, type
) == 0);
497 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
498 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
499 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_WHOLE_DISK
,
500 (uint64_t)wholedisk
) == 0);
506 if (nvlist_lookup_string(props
,
507 zpool_prop_to_name(ZPOOL_PROP_ASHIFT
), &value
) == 0)
508 zfs_nicestrtonum(NULL
, value
, &ashift
);
511 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_ASHIFT
,
519 * Go through and verify the replication level of the pool is consistent.
520 * Performs the following checks:
522 * For the new spec, verifies that devices in mirrors and raidz are the
525 * If the current configuration already has inconsistent replication
526 * levels, ignore any other potential problems in the new spec.
528 * Otherwise, make sure that the current spec (if there is one) and the new
529 * spec have consistent replication levels.
531 typedef struct replication_level
{
533 uint64_t zprl_children
;
534 uint64_t zprl_parity
;
535 } replication_level_t
;
537 #define ZPOOL_FUZZ (16 * 1024 * 1024)
540 * Given a list of toplevel vdevs, return the current replication level. If
541 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
542 * an error message will be displayed for each self-inconsistent vdev.
544 static replication_level_t
*
545 get_replication(nvlist_t
*nvroot
, boolean_t fatal
)
553 replication_level_t lastrep
= { 0 }, rep
, *ret
;
554 boolean_t dontreport
;
556 ret
= safe_malloc(sizeof (replication_level_t
));
558 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
559 &top
, &toplevels
) == 0);
561 lastrep
.zprl_type
= NULL
;
562 for (t
= 0; t
< toplevels
; t
++) {
563 uint64_t is_log
= B_FALSE
;
568 * For separate logs we ignore the top level vdev replication
571 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &is_log
);
575 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
,
577 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
578 &child
, &children
) != 0) {
580 * This is a 'file' or 'disk' vdev.
582 rep
.zprl_type
= type
;
583 rep
.zprl_children
= 1;
589 * This is a mirror or RAID-Z vdev. Go through and make
590 * sure the contents are all the same (files vs. disks),
591 * keeping track of the number of elements in the
594 * We also check that the size of each vdev (if it can
595 * be determined) is the same.
597 rep
.zprl_type
= type
;
598 rep
.zprl_children
= 0;
600 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
601 verify(nvlist_lookup_uint64(nv
,
602 ZPOOL_CONFIG_NPARITY
,
603 &rep
.zprl_parity
) == 0);
604 assert(rep
.zprl_parity
!= 0);
610 * The 'dontreport' variable indicates that we've
611 * already reported an error for this spec, so don't
612 * bother doing it again.
617 for (c
= 0; c
< children
; c
++) {
618 nvlist_t
*cnv
= child
[c
];
620 struct stat64 statbuf
;
621 uint64_t size
= -1ULL;
627 verify(nvlist_lookup_string(cnv
,
628 ZPOOL_CONFIG_TYPE
, &childtype
) == 0);
631 * If this is a replacing or spare vdev, then
632 * get the real first child of the vdev.
634 if (strcmp(childtype
,
635 VDEV_TYPE_REPLACING
) == 0 ||
636 strcmp(childtype
, VDEV_TYPE_SPARE
) == 0) {
640 verify(nvlist_lookup_nvlist_array(cnv
,
641 ZPOOL_CONFIG_CHILDREN
, &rchild
,
643 assert(rchildren
== 2);
646 verify(nvlist_lookup_string(cnv
,
651 verify(nvlist_lookup_string(cnv
,
652 ZPOOL_CONFIG_PATH
, &path
) == 0);
655 * If we have a raidz/mirror that combines disks
656 * with files, report it as an error.
658 if (!dontreport
&& type
!= NULL
&&
659 strcmp(type
, childtype
) != 0) {
665 "mismatched replication "
666 "level: %s contains both "
667 "files and devices\n"),
675 * According to stat(2), the value of 'st_size'
676 * is undefined for block devices and character
677 * devices. But there is no effective way to
678 * determine the real size in userland.
680 * Instead, we'll take advantage of an
681 * implementation detail of spec_size(). If the
682 * device is currently open, then we (should)
683 * return a valid size.
685 * If we still don't get a valid size (indicated
686 * by a size of 0 or MAXOFFSET_T), then ignore
687 * this device altogether.
689 if ((fd
= open(path
, O_RDONLY
)) >= 0) {
690 err
= fstat64(fd
, &statbuf
);
693 err
= stat64(path
, &statbuf
);
697 statbuf
.st_size
== 0 ||
698 statbuf
.st_size
== MAXOFFSET_T
)
701 size
= statbuf
.st_size
;
704 * Also make sure that devices and
705 * slices have a consistent size. If
706 * they differ by a significant amount
707 * (~16MB) then report an error.
710 (vdev_size
!= -1ULL &&
711 (labs(size
- vdev_size
) >
718 "%s contains devices of "
719 "different sizes\n"),
732 * At this point, we have the replication of the last toplevel
733 * vdev in 'rep'. Compare it to 'lastrep' to see if its
736 if (lastrep
.zprl_type
!= NULL
) {
737 if (strcmp(lastrep
.zprl_type
, rep
.zprl_type
) != 0) {
743 "mismatched replication level: "
744 "both %s and %s vdevs are "
746 lastrep
.zprl_type
, rep
.zprl_type
);
749 } else if (lastrep
.zprl_parity
!= rep
.zprl_parity
) {
755 "mismatched replication level: "
756 "both %llu and %llu device parity "
757 "%s vdevs are present\n"),
763 } else if (lastrep
.zprl_children
!= rep
.zprl_children
) {
769 "mismatched replication level: "
770 "both %llu-way and %llu-way %s "
771 "vdevs are present\n"),
772 lastrep
.zprl_children
,
789 * Check the replication level of the vdev spec against the current pool. Calls
790 * get_replication() to make sure the new spec is self-consistent. If the pool
791 * has a consistent replication level, then we ignore any errors. Otherwise,
792 * report any difference between the two.
795 check_replication(nvlist_t
*config
, nvlist_t
*newroot
)
799 replication_level_t
*current
= NULL
, *new;
803 * If we have a current pool configuration, check to see if it's
804 * self-consistent. If not, simply return success.
806 if (config
!= NULL
) {
809 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
811 if ((current
= get_replication(nvroot
, B_FALSE
)) == NULL
)
815 * for spares there may be no children, and therefore no
816 * replication level to check
818 if ((nvlist_lookup_nvlist_array(newroot
, ZPOOL_CONFIG_CHILDREN
,
819 &child
, &children
) != 0) || (children
== 0)) {
825 * If all we have is logs then there's no replication level to check.
827 if (num_logs(newroot
) == children
) {
833 * Get the replication level of the new vdev spec, reporting any
834 * inconsistencies found.
836 if ((new = get_replication(newroot
, B_TRUE
)) == NULL
) {
842 * Check to see if the new vdev spec matches the replication level of
846 if (current
!= NULL
) {
847 if (strcmp(current
->zprl_type
, new->zprl_type
) != 0) {
849 "mismatched replication level: pool uses %s "
850 "and new vdev is %s\n"),
851 current
->zprl_type
, new->zprl_type
);
853 } else if (current
->zprl_parity
!= new->zprl_parity
) {
855 "mismatched replication level: pool uses %llu "
856 "device parity and new vdev uses %llu\n"),
857 current
->zprl_parity
, new->zprl_parity
);
859 } else if (current
->zprl_children
!= new->zprl_children
) {
861 "mismatched replication level: pool uses %llu-way "
862 "%s and new vdev uses %llu-way %s\n"),
863 current
->zprl_children
, current
->zprl_type
,
864 new->zprl_children
, new->zprl_type
);
877 zero_label(char *path
)
879 const int size
= 4096;
883 if ((fd
= open(path
, O_WRONLY
|O_EXCL
)) < 0) {
884 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
885 path
, strerror(errno
));
889 memset(buf
, 0, size
);
890 err
= write(fd
, buf
, size
);
891 (void) fdatasync(fd
);
895 (void) fprintf(stderr
, gettext("cannot zero first %d bytes "
896 "of '%s': %s\n"), size
, path
, strerror(errno
));
901 (void) fprintf(stderr
, gettext("could only zero %d/%d bytes "
902 "of '%s'\n"), err
, size
, path
);
910 * Go through and find any whole disks in the vdev specification, labelling them
911 * as appropriate. When constructing the vdev spec, we were unable to open this
912 * device in order to provide a devid. Now that we have labelled the disk and
913 * know that slice 0 is valid, we can construct the devid now.
915 * If the disk was already labeled with an EFI label, we will have gotten the
916 * devid already (because we were able to open the whole disk). Otherwise, we
917 * need to get the devid after we label the disk.
920 make_disks(zpool_handle_t
*zhp
, nvlist_t
*nv
)
924 char *type
, *path
, *diskname
;
925 char devpath
[MAXPATHLEN
];
926 char udevpath
[MAXPATHLEN
];
928 struct stat64 statbuf
;
931 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
933 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
934 &child
, &children
) != 0) {
936 if (strcmp(type
, VDEV_TYPE_DISK
) != 0)
940 * We have a disk device. If this is a whole disk write
941 * out the efi partition table, otherwise write zero's to
942 * the first 4k of the partition. This is to ensure that
943 * libblkid will not misidentify the partition due to a
944 * magic value left by the previous filesystem.
946 verify(!nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
));
947 verify(!nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
951 ret
= zero_label(path
);
955 if (realpath(path
, devpath
) == NULL
) {
957 (void) fprintf(stderr
,
958 gettext("cannot resolve path '%s'\n"), path
);
963 * Remove any previously existing symlink from a udev path to
964 * the device before labeling the disk. This makes
965 * zpool_label_disk_wait() truly wait for the new link to show
966 * up instead of returning if it finds an old link still in
967 * place. Otherwise there is a window between when udev
968 * deletes and recreates the link during which access attempts
969 * will fail with ENOENT.
971 zfs_append_partition(path
, udevpath
, sizeof (udevpath
));
972 if ((strncmp(udevpath
, UDISK_ROOT
, strlen(UDISK_ROOT
)) == 0) &&
973 (lstat64(udevpath
, &statbuf
) == 0) &&
974 S_ISLNK(statbuf
.st_mode
))
975 (void) unlink(udevpath
);
977 diskname
= strrchr(devpath
, '/');
978 assert(diskname
!= NULL
);
980 if (zpool_label_disk(g_zfs
, zhp
, diskname
) == -1)
984 * Now we've labeled the disk and the partitions have been
985 * created. We still need to wait for udev to create the
986 * symlinks to those partitions.
988 if ((ret
= zpool_label_disk_wait(udevpath
, 1000)) != 0) {
989 (void) fprintf(stderr
,
990 gettext( "cannot resolve path '%s'\n"), udevpath
);
995 * Update the path to refer to FIRST_SLICE. The presence of
996 * the 'whole_disk' field indicates to the CLI that we should
997 * chop off the slice number when displaying the device in
1000 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_PATH
, udevpath
) == 0);
1002 /* Just in case this partition already existed. */
1003 (void) zero_label(udevpath
);
1008 for (c
= 0; c
< children
; c
++)
1009 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
1012 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1013 &child
, &children
) == 0)
1014 for (c
= 0; c
< children
; c
++)
1015 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
1018 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1019 &child
, &children
) == 0)
1020 for (c
= 0; c
< children
; c
++)
1021 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
1028 * Determine if the given path is a hot spare within the given configuration.
1031 is_spare(nvlist_t
*config
, const char *path
)
1037 uint64_t guid
, spareguid
;
1043 if ((fd
= open(path
, O_RDONLY
|O_EXCL
)) < 0)
1046 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0 ||
1048 state
!= POOL_STATE_SPARE
||
1049 zpool_read_label(fd
, &label
) != 0) {
1057 verify(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
1060 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1062 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1063 &spares
, &nspares
) == 0) {
1064 for (i
= 0; i
< nspares
; i
++) {
1065 verify(nvlist_lookup_uint64(spares
[i
],
1066 ZPOOL_CONFIG_GUID
, &spareguid
) == 0);
1067 if (spareguid
== guid
)
1076 * Go through and find any devices that are in use. We rely on libdiskmgt for
1077 * the majority of this task.
1080 check_in_use(nvlist_t
*config
, nvlist_t
*nv
, boolean_t force
,
1081 boolean_t replacing
, boolean_t isspare
)
1087 char buf
[MAXPATHLEN
];
1088 uint64_t wholedisk
= B_FALSE
;
1090 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1092 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1093 &child
, &children
) != 0) {
1095 verify(!nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
));
1096 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1097 verify(!nvlist_lookup_uint64(nv
,
1098 ZPOOL_CONFIG_WHOLE_DISK
, &wholedisk
));
1101 * As a generic check, we look to see if this is a replace of a
1102 * hot spare within the same pool. If so, we allow it
1103 * regardless of what libblkid or zpool_in_use() says.
1107 (void) snprintf(buf
, sizeof (buf
), "%ss0",
1110 (void) strlcpy(buf
, path
, sizeof (buf
));
1112 if (is_spare(config
, buf
))
1116 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1117 ret
= check_device(path
, force
, isspare
, wholedisk
);
1119 if (strcmp(type
, VDEV_TYPE_FILE
) == 0)
1120 ret
= check_file(path
, force
, isspare
);
1125 for (c
= 0; c
< children
; c
++)
1126 if ((ret
= check_in_use(config
, child
[c
], force
,
1127 replacing
, B_FALSE
)) != 0)
1130 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1131 &child
, &children
) == 0)
1132 for (c
= 0; c
< children
; c
++)
1133 if ((ret
= check_in_use(config
, child
[c
], force
,
1134 replacing
, B_TRUE
)) != 0)
1137 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1138 &child
, &children
) == 0)
1139 for (c
= 0; c
< children
; c
++)
1140 if ((ret
= check_in_use(config
, child
[c
], force
,
1141 replacing
, B_FALSE
)) != 0)
1148 is_grouping(const char *type
, int *mindev
, int *maxdev
)
1150 if (strncmp(type
, "raidz", 5) == 0) {
1151 const char *p
= type
+ 5;
1157 } else if (*p
== '0') {
1158 return (NULL
); /* no zero prefixes allowed */
1161 nparity
= strtol(p
, &end
, 10);
1162 if (errno
!= 0 || nparity
< 1 || nparity
>= 255 ||
1168 *mindev
= nparity
+ 1;
1171 return (VDEV_TYPE_RAIDZ
);
1177 if (strcmp(type
, "mirror") == 0) {
1180 return (VDEV_TYPE_MIRROR
);
1183 if (strcmp(type
, "spare") == 0) {
1186 return (VDEV_TYPE_SPARE
);
1189 if (strcmp(type
, "log") == 0) {
1192 return (VDEV_TYPE_LOG
);
1195 if (strcmp(type
, "cache") == 0) {
1198 return (VDEV_TYPE_L2CACHE
);
1205 * Construct a syntactically valid vdev specification,
1206 * and ensure that all devices and files exist and can be opened.
1207 * Note: we don't bother freeing anything in the error paths
1208 * because the program is just going to exit anyway.
1211 construct_spec(nvlist_t
*props
, int argc
, char **argv
)
1213 nvlist_t
*nvroot
, *nv
, **top
, **spares
, **l2cache
;
1214 int t
, toplevels
, mindev
, maxdev
, nspares
, nlogs
, nl2cache
;
1217 boolean_t seen_logs
;
1227 seen_logs
= B_FALSE
;
1233 * If it's a mirror or raidz, the subsequent arguments are
1234 * its leaves -- until we encounter the next mirror or raidz.
1236 if ((type
= is_grouping(argv
[0], &mindev
, &maxdev
)) != NULL
) {
1237 nvlist_t
**child
= NULL
;
1238 int c
, children
= 0;
1240 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1241 if (spares
!= NULL
) {
1242 (void) fprintf(stderr
,
1243 gettext("invalid vdev "
1244 "specification: 'spare' can be "
1245 "specified only once\n"));
1251 if (strcmp(type
, VDEV_TYPE_LOG
) == 0) {
1253 (void) fprintf(stderr
,
1254 gettext("invalid vdev "
1255 "specification: 'log' can be "
1256 "specified only once\n"));
1264 * A log is not a real grouping device.
1265 * We just set is_log and continue.
1270 if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1271 if (l2cache
!= NULL
) {
1272 (void) fprintf(stderr
,
1273 gettext("invalid vdev "
1274 "specification: 'cache' can be "
1275 "specified only once\n"));
1282 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
1283 (void) fprintf(stderr
,
1284 gettext("invalid vdev "
1285 "specification: unsupported 'log' "
1286 "device: %s\n"), type
);
1292 for (c
= 1; c
< argc
; c
++) {
1293 if (is_grouping(argv
[c
], NULL
, NULL
) != NULL
)
1296 child
= realloc(child
,
1297 children
* sizeof (nvlist_t
*));
1300 if ((nv
= make_leaf_vdev(props
, argv
[c
], B_FALSE
))
1303 child
[children
- 1] = nv
;
1306 if (children
< mindev
) {
1307 (void) fprintf(stderr
, gettext("invalid vdev "
1308 "specification: %s requires at least %d "
1309 "devices\n"), argv
[0], mindev
);
1313 if (children
> maxdev
) {
1314 (void) fprintf(stderr
, gettext("invalid vdev "
1315 "specification: %s supports no more than "
1316 "%d devices\n"), argv
[0], maxdev
);
1323 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1327 } else if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1329 nl2cache
= children
;
1332 verify(nvlist_alloc(&nv
, NV_UNIQUE_NAME
,
1334 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_TYPE
,
1336 verify(nvlist_add_uint64(nv
,
1337 ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
1338 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
1339 verify(nvlist_add_uint64(nv
,
1340 ZPOOL_CONFIG_NPARITY
,
1343 verify(nvlist_add_nvlist_array(nv
,
1344 ZPOOL_CONFIG_CHILDREN
, child
,
1347 for (c
= 0; c
< children
; c
++)
1348 nvlist_free(child
[c
]);
1353 * We have a device. Pass off to make_leaf_vdev() to
1354 * construct the appropriate nvlist describing the vdev.
1356 if ((nv
= make_leaf_vdev(props
, argv
[0], is_log
)) == NULL
)
1365 top
= realloc(top
, toplevels
* sizeof (nvlist_t
*));
1368 top
[toplevels
- 1] = nv
;
1371 if (toplevels
== 0 && nspares
== 0 && nl2cache
== 0) {
1372 (void) fprintf(stderr
, gettext("invalid vdev "
1373 "specification: at least one toplevel vdev must be "
1378 if (seen_logs
&& nlogs
== 0) {
1379 (void) fprintf(stderr
, gettext("invalid vdev specification: "
1380 "log requires at least 1 device\n"));
1385 * Finally, create nvroot and add all top-level vdevs to it.
1387 verify(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, 0) == 0);
1388 verify(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
1389 VDEV_TYPE_ROOT
) == 0);
1390 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1391 top
, toplevels
) == 0);
1393 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1394 spares
, nspares
) == 0);
1396 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1397 l2cache
, nl2cache
) == 0);
1399 for (t
= 0; t
< toplevels
; t
++)
1400 nvlist_free(top
[t
]);
1401 for (t
= 0; t
< nspares
; t
++)
1402 nvlist_free(spares
[t
]);
1403 for (t
= 0; t
< nl2cache
; t
++)
1404 nvlist_free(l2cache
[t
]);
1415 split_mirror_vdev(zpool_handle_t
*zhp
, char *newname
, nvlist_t
*props
,
1416 splitflags_t flags
, int argc
, char **argv
)
1418 nvlist_t
*newroot
= NULL
, **child
;
1422 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
) {
1423 (void) fprintf(stderr
, gettext("Unable to build a "
1424 "pool from the specified devices\n"));
1428 if (!flags
.dryrun
&& make_disks(zhp
, newroot
) != 0) {
1429 nvlist_free(newroot
);
1433 /* avoid any tricks in the spec */
1434 verify(nvlist_lookup_nvlist_array(newroot
,
1435 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
1436 for (c
= 0; c
< children
; c
++) {
1441 verify(nvlist_lookup_string(child
[c
],
1442 ZPOOL_CONFIG_PATH
, &path
) == 0);
1443 if ((type
= is_grouping(path
, &min
, &max
)) != NULL
) {
1444 (void) fprintf(stderr
, gettext("Cannot use "
1445 "'%s' as a device for splitting\n"), type
);
1446 nvlist_free(newroot
);
1452 if (zpool_vdev_split(zhp
, newname
, &newroot
, props
, flags
) != 0) {
1453 if (newroot
!= NULL
)
1454 nvlist_free(newroot
);
1462 * Get and validate the contents of the given vdev specification. This ensures
1463 * that the nvlist returned is well-formed, that all the devices exist, and that
1464 * they are not currently in use by any other known consumer. The 'poolconfig'
1465 * parameter is the current configuration of the pool when adding devices
1466 * existing pool, and is used to perform additional checks, such as changing the
1467 * replication level of the pool. It can be 'NULL' to indicate that this is a
1468 * new pool. The 'force' flag controls whether devices should be forcefully
1469 * added, even if they appear in use.
1472 make_root_vdev(zpool_handle_t
*zhp
, nvlist_t
*props
, int force
, int check_rep
,
1473 boolean_t replacing
, boolean_t dryrun
, int argc
, char **argv
)
1476 nvlist_t
*poolconfig
= NULL
;
1480 * Construct the vdev specification. If this is successful, we know
1481 * that we have a valid specification, and that all devices can be
1484 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
)
1487 if (zhp
&& ((poolconfig
= zpool_get_config(zhp
, NULL
)) == NULL
))
1491 * Validate each device to make sure that its not shared with another
1492 * subsystem. We do this even if 'force' is set, because there are some
1493 * uses (such as a dedicated dump device) that even '-f' cannot
1496 if (check_in_use(poolconfig
, newroot
, force
, replacing
, B_FALSE
) != 0) {
1497 nvlist_free(newroot
);
1502 * Check the replication level of the given vdevs and report any errors
1503 * found. We include the existing pool spec, if any, as we need to
1504 * catch changes against the existing replication level.
1506 if (check_rep
&& check_replication(poolconfig
, newroot
) != 0) {
1507 nvlist_free(newroot
);
1512 * Run through the vdev specification and label any whole disks found.
1514 if (!dryrun
&& make_disks(zhp
, newroot
) != 0) {
1515 nvlist_free(newroot
);