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.
24 * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2016, 2017 Intel Corporation.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Functions to convert between a list of vdevs and an nvlist representing the
31 * configuration. Each entry in the list can be one of:
34 * disk=(path=..., devid=...)
43 * While the underlying implementation supports it, group vdevs cannot contain
44 * other group vdevs. All userland verification of devices is contained within
45 * this file. If successful, the nvlist returned can be passed directly to the
46 * kernel; we've done as much verification as possible in userland.
48 * Hot spares are a special case, and passed down as an array of disk vdevs, at
49 * the same level as the root of the vdev tree.
51 * The only function exported by this file is 'make_root_vdev'. The
52 * function performs several passes:
54 * 1. Construct the vdev specification. Performs syntax validation and
55 * makes sure each device is valid.
56 * 2. Check for devices in use. Using libblkid to make sure that no
57 * devices are also in use. Some can be overridden using the 'force'
58 * flag, others cannot.
59 * 3. Check for replication errors if the 'force' flag is not specified.
60 * validates that the replication level is consistent across the
62 * 4. Call libzfs to label any whole disks with an EFI label.
70 #include <libnvpair.h>
77 #include "zpool_util.h"
78 #include <sys/zfs_context.h>
82 * For any given vdev specification, we can have multiple errors. The
83 * vdev_error() function keeps track of whether we have seen an error yet, and
84 * prints out a header if its the first error we've seen.
94 vdev_error(const char *fmt
, ...)
99 (void) fprintf(stderr
, gettext("invalid vdev specification\n"));
101 (void) fprintf(stderr
, gettext("use '-f' to override "
102 "the following errors:\n"));
104 (void) fprintf(stderr
, gettext("the following errors "
105 "must be manually repaired:\n"));
110 (void) vfprintf(stderr
, fmt
, ap
);
115 * Check that a file is valid. All we can do in this case is check that it's
116 * not in use by another pool, and not in use by swap.
119 check_file(const char *file
, boolean_t force
, boolean_t isspare
)
127 if ((fd
= open(file
, O_RDONLY
)) < 0)
130 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) == 0 && inuse
) {
134 case POOL_STATE_ACTIVE
:
135 desc
= gettext("active");
138 case POOL_STATE_EXPORTED
:
139 desc
= gettext("exported");
142 case POOL_STATE_POTENTIALLY_ACTIVE
:
143 desc
= gettext("potentially active");
147 desc
= gettext("unknown");
152 * Allow hot spares to be shared between pools.
154 if (state
== POOL_STATE_SPARE
&& isspare
) {
160 if (state
== POOL_STATE_ACTIVE
||
161 state
== POOL_STATE_SPARE
|| !force
) {
163 case POOL_STATE_SPARE
:
164 vdev_error(gettext("%s is reserved as a hot "
165 "spare for pool %s\n"), file
, name
);
168 vdev_error(gettext("%s is part of %s pool "
169 "'%s'\n"), file
, desc
, name
);
183 * This may be a shorthand device path or it could be total gibberish.
184 * Check to see if it is a known device available in zfs_vdev_paths.
185 * As part of this check, see if we've been given an entire disk
186 * (minus the slice number).
189 is_shorthand_path(const char *arg
, char *path
, size_t path_size
,
190 struct stat64
*statbuf
, boolean_t
*wholedisk
)
194 error
= zfs_resolve_shortname(arg
, path
, path_size
);
196 *wholedisk
= zfs_dev_is_whole_disk(path
);
197 if (*wholedisk
|| (stat64(path
, statbuf
) == 0))
201 strlcpy(path
, arg
, path_size
);
202 memset(statbuf
, 0, sizeof (*statbuf
));
203 *wholedisk
= B_FALSE
;
209 * Determine if the given path is a hot spare within the given configuration.
210 * If no configuration is given we rely solely on the label.
213 is_spare(nvlist_t
*config
, const char *path
)
219 uint64_t guid
, spareguid
;
225 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
228 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0 ||
230 state
!= POOL_STATE_SPARE
||
231 zpool_read_label(fd
, &label
, NULL
) != 0) {
239 if (config
== NULL
) {
244 verify(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
247 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
249 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
250 &spares
, &nspares
) == 0) {
251 for (i
= 0; i
< nspares
; i
++) {
252 verify(nvlist_lookup_uint64(spares
[i
],
253 ZPOOL_CONFIG_GUID
, &spareguid
) == 0);
254 if (spareguid
== guid
)
263 * Create a leaf vdev. Determine if this is a file or a device. If it's a
264 * device, fill in the device id to make a complete nvlist. Valid forms for a
267 * /dev/xxx Complete disk path
268 * /xxx Full path to file
269 * xxx Shorthand for <zfs_vdev_paths>/xxx
272 make_leaf_vdev(nvlist_t
*props
, const char *arg
, uint64_t is_log
)
274 char path
[MAXPATHLEN
];
275 struct stat64 statbuf
;
276 nvlist_t
*vdev
= NULL
;
278 boolean_t wholedisk
= B_FALSE
;
283 * Determine what type of vdev this is, and put the full path into
284 * 'path'. We detect whether this is a device of file afterwards by
285 * checking the st_mode of the file.
289 * Complete device or file path. Exact type is determined by
290 * examining the file descriptor afterwards. Symbolic links
291 * are resolved to their real paths to determine whole disk
292 * and S_ISBLK/S_ISREG type checks. However, we are careful
293 * to store the given path as ZPOOL_CONFIG_PATH to ensure we
294 * can leverage udev's persistent device labels.
296 if (realpath(arg
, path
) == NULL
) {
297 (void) fprintf(stderr
,
298 gettext("cannot resolve path '%s'\n"), arg
);
302 wholedisk
= zfs_dev_is_whole_disk(path
);
303 if (!wholedisk
&& (stat64(path
, &statbuf
) != 0)) {
304 (void) fprintf(stderr
,
305 gettext("cannot open '%s': %s\n"),
306 path
, strerror(errno
));
310 /* After whole disk check restore original passed path */
311 strlcpy(path
, arg
, sizeof (path
));
313 err
= is_shorthand_path(arg
, path
, sizeof (path
),
314 &statbuf
, &wholedisk
);
317 * If we got ENOENT, then the user gave us
318 * gibberish, so try to direct them with a
319 * reasonable error message. Otherwise,
320 * regurgitate strerror() since it's the best we
324 (void) fprintf(stderr
,
325 gettext("cannot open '%s': no such "
326 "device in %s\n"), arg
, DISK_ROOT
);
327 (void) fprintf(stderr
,
328 gettext("must be a full path or "
329 "shorthand device name\n"));
332 (void) fprintf(stderr
,
333 gettext("cannot open '%s': %s\n"),
334 path
, strerror(errno
));
341 * Determine whether this is a device or a file.
343 if (wholedisk
|| S_ISBLK(statbuf
.st_mode
)) {
344 type
= VDEV_TYPE_DISK
;
345 } else if (S_ISREG(statbuf
.st_mode
)) {
346 type
= VDEV_TYPE_FILE
;
348 (void) fprintf(stderr
, gettext("cannot use '%s': must be a "
349 "block device or regular file\n"), path
);
354 * Finally, we have the complete device or file, and we know that it is
355 * acceptable to use. Construct the nvlist to describe this vdev. All
356 * vdevs have a 'path' element, and devices also have a 'devid' element.
358 verify(nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) == 0);
359 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_PATH
, path
) == 0);
360 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
, type
) == 0);
361 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
363 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_ALLOCATION_BIAS
,
364 VDEV_ALLOC_BIAS_LOG
) == 0);
365 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
366 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_WHOLE_DISK
,
367 (uint64_t)wholedisk
) == 0);
370 * Override defaults if custom properties are provided.
375 if (nvlist_lookup_string(props
,
376 zpool_prop_to_name(ZPOOL_PROP_ASHIFT
), &value
) == 0) {
377 if (zfs_nicestrtonum(NULL
, value
, &ashift
) != 0) {
378 (void) fprintf(stderr
,
379 gettext("ashift must be a number.\n"));
383 (ashift
< ASHIFT_MIN
|| ashift
> ASHIFT_MAX
)) {
384 (void) fprintf(stderr
,
385 gettext("invalid 'ashift=%" PRIu64
"' "
386 "property: only values between %" PRId32
" "
387 "and %" PRId32
" are allowed.\n"),
388 ashift
, ASHIFT_MIN
, ASHIFT_MAX
);
395 * If the device is known to incorrectly report its physical sector
396 * size explicitly provide the known correct value.
401 if (check_sector_size_database(path
, §or_size
) == B_TRUE
)
402 ashift
= highbit64(sector_size
) - 1;
406 (void) nvlist_add_uint64(vdev
, ZPOOL_CONFIG_ASHIFT
, ashift
);
412 * Go through and verify the replication level of the pool is consistent.
413 * Performs the following checks:
415 * For the new spec, verifies that devices in mirrors and raidz are the
418 * If the current configuration already has inconsistent replication
419 * levels, ignore any other potential problems in the new spec.
421 * Otherwise, make sure that the current spec (if there is one) and the new
422 * spec have consistent replication levels.
424 * If there is no current spec (create), make sure new spec has at least
425 * one general purpose vdev.
427 typedef struct replication_level
{
429 uint64_t zprl_children
;
430 uint64_t zprl_parity
;
431 } replication_level_t
;
433 #define ZPOOL_FUZZ (16 * 1024 * 1024)
436 is_raidz_mirror(replication_level_t
*a
, replication_level_t
*b
,
437 replication_level_t
**raidz
, replication_level_t
**mirror
)
439 if (strcmp(a
->zprl_type
, "raidz") == 0 &&
440 strcmp(b
->zprl_type
, "mirror") == 0) {
449 * Given a list of toplevel vdevs, return the current replication level. If
450 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
451 * an error message will be displayed for each self-inconsistent vdev.
453 static replication_level_t
*
454 get_replication(nvlist_t
*nvroot
, boolean_t fatal
)
462 replication_level_t lastrep
= {0};
463 replication_level_t rep
;
464 replication_level_t
*ret
;
465 replication_level_t
*raidz
, *mirror
;
466 boolean_t dontreport
;
468 ret
= safe_malloc(sizeof (replication_level_t
));
470 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
471 &top
, &toplevels
) == 0);
473 for (t
= 0; t
< toplevels
; t
++) {
474 uint64_t is_log
= B_FALSE
;
479 * For separate logs we ignore the top level vdev replication
482 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &is_log
);
486 /* Ignore holes introduced by removing aux devices */
487 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
488 if (strcmp(type
, VDEV_TYPE_HOLE
) == 0)
491 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
492 &child
, &children
) != 0) {
494 * This is a 'file' or 'disk' vdev.
496 rep
.zprl_type
= type
;
497 rep
.zprl_children
= 1;
503 * This is a mirror or RAID-Z vdev. Go through and make
504 * sure the contents are all the same (files vs. disks),
505 * keeping track of the number of elements in the
508 * We also check that the size of each vdev (if it can
509 * be determined) is the same.
511 rep
.zprl_type
= type
;
512 rep
.zprl_children
= 0;
514 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
515 verify(nvlist_lookup_uint64(nv
,
516 ZPOOL_CONFIG_NPARITY
,
517 &rep
.zprl_parity
) == 0);
518 assert(rep
.zprl_parity
!= 0);
524 * The 'dontreport' variable indicates that we've
525 * already reported an error for this spec, so don't
526 * bother doing it again.
531 for (c
= 0; c
< children
; c
++) {
532 nvlist_t
*cnv
= child
[c
];
534 struct stat64 statbuf
;
541 verify(nvlist_lookup_string(cnv
,
542 ZPOOL_CONFIG_TYPE
, &childtype
) == 0);
545 * If this is a replacing or spare vdev, then
546 * get the real first child of the vdev: do this
547 * in a loop because replacing and spare vdevs
550 while (strcmp(childtype
,
551 VDEV_TYPE_REPLACING
) == 0 ||
552 strcmp(childtype
, VDEV_TYPE_SPARE
) == 0) {
556 verify(nvlist_lookup_nvlist_array(cnv
,
557 ZPOOL_CONFIG_CHILDREN
, &rchild
,
559 assert(rchildren
== 2);
562 verify(nvlist_lookup_string(cnv
,
567 verify(nvlist_lookup_string(cnv
,
568 ZPOOL_CONFIG_PATH
, &path
) == 0);
571 * If we have a raidz/mirror that combines disks
572 * with files, report it as an error.
574 if (!dontreport
&& type
!= NULL
&&
575 strcmp(type
, childtype
) != 0) {
581 "mismatched replication "
582 "level: %s contains both "
583 "files and devices\n"),
591 * According to stat(2), the value of 'st_size'
592 * is undefined for block devices and character
593 * devices. But there is no effective way to
594 * determine the real size in userland.
596 * Instead, we'll take advantage of an
597 * implementation detail of spec_size(). If the
598 * device is currently open, then we (should)
599 * return a valid size.
601 * If we still don't get a valid size (indicated
602 * by a size of 0 or MAXOFFSET_T), then ignore
603 * this device altogether.
605 if ((fd
= open(path
, O_RDONLY
)) >= 0) {
606 err
= fstat64_blk(fd
, &statbuf
);
609 err
= stat64(path
, &statbuf
);
613 statbuf
.st_size
== 0 ||
614 statbuf
.st_size
== MAXOFFSET_T
)
617 size
= statbuf
.st_size
;
620 * Also make sure that devices and
621 * slices have a consistent size. If
622 * they differ by a significant amount
623 * (~16MB) then report an error.
626 (vdev_size
!= -1LL &&
627 (llabs(size
- vdev_size
) >
634 "%s contains devices of "
635 "different sizes\n"),
648 * At this point, we have the replication of the last toplevel
649 * vdev in 'rep'. Compare it to 'lastrep' to see if it is
652 if (lastrep
.zprl_type
!= NULL
) {
653 if (is_raidz_mirror(&lastrep
, &rep
, &raidz
, &mirror
) ||
654 is_raidz_mirror(&rep
, &lastrep
, &raidz
, &mirror
)) {
656 * Accepted raidz and mirror when they can
657 * handle the same number of disk failures.
659 if (raidz
->zprl_parity
!=
660 mirror
->zprl_children
- 1) {
666 "mismatched replication "
668 "%s and %s vdevs with "
669 "different redundancy, "
670 "%llu vs. %llu (%llu-way) "
675 mirror
->zprl_children
- 1,
676 mirror
->zprl_children
);
680 } else if (strcmp(lastrep
.zprl_type
, rep
.zprl_type
) !=
687 "mismatched replication level: "
688 "both %s and %s vdevs are "
690 lastrep
.zprl_type
, rep
.zprl_type
);
693 } else if (lastrep
.zprl_parity
!= rep
.zprl_parity
) {
699 "mismatched replication level: "
700 "both %llu and %llu device parity "
701 "%s vdevs are present\n"),
707 } else if (lastrep
.zprl_children
!= rep
.zprl_children
) {
713 "mismatched replication level: "
714 "both %llu-way and %llu-way %s "
715 "vdevs are present\n"),
716 lastrep
.zprl_children
,
733 * Check the replication level of the vdev spec against the current pool. Calls
734 * get_replication() to make sure the new spec is self-consistent. If the pool
735 * has a consistent replication level, then we ignore any errors. Otherwise,
736 * report any difference between the two.
739 check_replication(nvlist_t
*config
, nvlist_t
*newroot
)
743 replication_level_t
*current
= NULL
, *new;
744 replication_level_t
*raidz
, *mirror
;
748 * If we have a current pool configuration, check to see if it's
749 * self-consistent. If not, simply return success.
751 if (config
!= NULL
) {
754 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
756 if ((current
= get_replication(nvroot
, B_FALSE
)) == NULL
)
760 * for spares there may be no children, and therefore no
761 * replication level to check
763 if ((nvlist_lookup_nvlist_array(newroot
, ZPOOL_CONFIG_CHILDREN
,
764 &child
, &children
) != 0) || (children
== 0)) {
770 * If all we have is logs then there's no replication level to check.
772 if (num_logs(newroot
) == children
) {
778 * Get the replication level of the new vdev spec, reporting any
779 * inconsistencies found.
781 if ((new = get_replication(newroot
, B_TRUE
)) == NULL
) {
787 * Check to see if the new vdev spec matches the replication level of
791 if (current
!= NULL
) {
792 if (is_raidz_mirror(current
, new, &raidz
, &mirror
) ||
793 is_raidz_mirror(new, current
, &raidz
, &mirror
)) {
794 if (raidz
->zprl_parity
!= mirror
->zprl_children
- 1) {
796 "mismatched replication level: pool and "
797 "new vdev with different redundancy, %s "
798 "and %s vdevs, %llu vs. %llu (%llu-way)\n"),
802 mirror
->zprl_children
- 1,
803 mirror
->zprl_children
);
806 } else if (strcmp(current
->zprl_type
, new->zprl_type
) != 0) {
808 "mismatched replication level: pool uses %s "
809 "and new vdev is %s\n"),
810 current
->zprl_type
, new->zprl_type
);
812 } else if (current
->zprl_parity
!= new->zprl_parity
) {
814 "mismatched replication level: pool uses %llu "
815 "device parity and new vdev uses %llu\n"),
816 current
->zprl_parity
, new->zprl_parity
);
818 } else if (current
->zprl_children
!= new->zprl_children
) {
820 "mismatched replication level: pool uses %llu-way "
821 "%s and new vdev uses %llu-way %s\n"),
822 current
->zprl_children
, current
->zprl_type
,
823 new->zprl_children
, new->zprl_type
);
836 zero_label(char *path
)
838 const int size
= 4096;
842 if ((fd
= open(path
, O_WRONLY
|O_EXCL
)) < 0) {
843 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
844 path
, strerror(errno
));
848 memset(buf
, 0, size
);
849 err
= write(fd
, buf
, size
);
850 (void) fdatasync(fd
);
854 (void) fprintf(stderr
, gettext("cannot zero first %d bytes "
855 "of '%s': %s\n"), size
, path
, strerror(errno
));
860 (void) fprintf(stderr
, gettext("could only zero %d/%d bytes "
861 "of '%s'\n"), err
, size
, path
);
869 * Go through and find any whole disks in the vdev specification, labelling them
870 * as appropriate. When constructing the vdev spec, we were unable to open this
871 * device in order to provide a devid. Now that we have labelled the disk and
872 * know that slice 0 is valid, we can construct the devid now.
874 * If the disk was already labeled with an EFI label, we will have gotten the
875 * devid already (because we were able to open the whole disk). Otherwise, we
876 * need to get the devid after we label the disk.
879 make_disks(zpool_handle_t
*zhp
, nvlist_t
*nv
)
884 char devpath
[MAXPATHLEN
];
885 char udevpath
[MAXPATHLEN
];
887 struct stat64 statbuf
;
888 int is_exclusive
= 0;
892 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
894 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
895 &child
, &children
) != 0) {
897 if (strcmp(type
, VDEV_TYPE_DISK
) != 0)
901 * We have a disk device. If this is a whole disk write
902 * out the efi partition table, otherwise write zero's to
903 * the first 4k of the partition. This is to ensure that
904 * libblkid will not misidentify the partition due to a
905 * magic value left by the previous filesystem.
907 verify(!nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
));
908 verify(!nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
913 * Update device id string for mpath nodes (Linux only)
915 if (is_mpath_whole_disk(path
))
916 update_vdev_config_dev_strs(nv
);
918 if (!is_spare(NULL
, path
))
919 (void) zero_label(path
);
923 if (realpath(path
, devpath
) == NULL
) {
925 (void) fprintf(stderr
,
926 gettext("cannot resolve path '%s'\n"), path
);
931 * Remove any previously existing symlink from a udev path to
932 * the device before labeling the disk. This ensures that
933 * only newly created links are used. Otherwise there is a
934 * window between when udev deletes and recreates the link
935 * during which access attempts will fail with ENOENT.
937 strlcpy(udevpath
, path
, MAXPATHLEN
);
938 (void) zfs_append_partition(udevpath
, MAXPATHLEN
);
940 fd
= open(devpath
, O_RDWR
|O_EXCL
);
953 * If the partition exists, contains a valid spare label,
954 * and is opened exclusively there is no need to partition
955 * it. Hot spares have already been partitioned and are
956 * held open exclusively by the kernel as a safety measure.
958 * If the provided path is for a /dev/disk/ device its
959 * symbolic link will be removed, partition table created,
960 * and then block until udev creates the new link.
962 if (!is_exclusive
&& !is_spare(NULL
, udevpath
)) {
963 char *devnode
= strrchr(devpath
, '/') + 1;
965 ret
= strncmp(udevpath
, UDISK_ROOT
, strlen(UDISK_ROOT
));
967 ret
= lstat64(udevpath
, &statbuf
);
968 if (ret
== 0 && S_ISLNK(statbuf
.st_mode
))
969 (void) unlink(udevpath
);
973 * When labeling a pool the raw device node name
974 * is provided as it appears under /dev/.
976 if (zpool_label_disk(g_zfs
, zhp
, devnode
) == -1)
980 * Wait for udev to signal the device is available
981 * by the provided path.
983 ret
= zpool_label_disk_wait(udevpath
, DISK_LABEL_WAIT
);
985 (void) fprintf(stderr
,
986 gettext("missing link: %s was "
987 "partitioned but %s is missing\n"),
992 ret
= zero_label(udevpath
);
998 * Update the path to refer to the partition. The presence of
999 * the 'whole_disk' field indicates to the CLI that we should
1000 * chop off the partition number when displaying the device in
1003 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_PATH
, udevpath
) == 0);
1006 * Update device id strings for whole disks (Linux only)
1008 update_vdev_config_dev_strs(nv
);
1013 for (c
= 0; c
< children
; c
++)
1014 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
1017 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1018 &child
, &children
) == 0)
1019 for (c
= 0; c
< children
; c
++)
1020 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
1023 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1024 &child
, &children
) == 0)
1025 for (c
= 0; c
< children
; c
++)
1026 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
1033 * Go through and find any devices that are in use. We rely on libdiskmgt for
1034 * the majority of this task.
1037 is_device_in_use(nvlist_t
*config
, nvlist_t
*nv
, boolean_t force
,
1038 boolean_t replacing
, boolean_t isspare
)
1044 char buf
[MAXPATHLEN
];
1045 uint64_t wholedisk
= B_FALSE
;
1046 boolean_t anyinuse
= B_FALSE
;
1048 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1050 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1051 &child
, &children
) != 0) {
1053 verify(!nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
));
1054 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1055 verify(!nvlist_lookup_uint64(nv
,
1056 ZPOOL_CONFIG_WHOLE_DISK
, &wholedisk
));
1059 * As a generic check, we look to see if this is a replace of a
1060 * hot spare within the same pool. If so, we allow it
1061 * regardless of what libblkid or zpool_in_use() says.
1064 (void) strlcpy(buf
, path
, sizeof (buf
));
1066 ret
= zfs_append_partition(buf
, sizeof (buf
));
1071 if (is_spare(config
, buf
))
1075 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1076 ret
= check_device(path
, force
, isspare
, wholedisk
);
1078 else if (strcmp(type
, VDEV_TYPE_FILE
) == 0)
1079 ret
= check_file(path
, force
, isspare
);
1084 for (c
= 0; c
< children
; c
++)
1085 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1089 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1090 &child
, &children
) == 0)
1091 for (c
= 0; c
< children
; c
++)
1092 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1096 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1097 &child
, &children
) == 0)
1098 for (c
= 0; c
< children
; c
++)
1099 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1107 is_grouping(const char *type
, int *mindev
, int *maxdev
)
1109 if (strncmp(type
, "raidz", 5) == 0) {
1110 const char *p
= type
+ 5;
1116 } else if (*p
== '0') {
1117 return (NULL
); /* no zero prefixes allowed */
1120 nparity
= strtol(p
, &end
, 10);
1121 if (errno
!= 0 || nparity
< 1 || nparity
>= 255 ||
1127 *mindev
= nparity
+ 1;
1130 return (VDEV_TYPE_RAIDZ
);
1136 if (strcmp(type
, "mirror") == 0) {
1139 return (VDEV_TYPE_MIRROR
);
1142 if (strcmp(type
, "spare") == 0) {
1145 return (VDEV_TYPE_SPARE
);
1148 if (strcmp(type
, "log") == 0) {
1151 return (VDEV_TYPE_LOG
);
1154 if (strcmp(type
, VDEV_ALLOC_BIAS_SPECIAL
) == 0 ||
1155 strcmp(type
, VDEV_ALLOC_BIAS_DEDUP
) == 0) {
1161 if (strcmp(type
, "cache") == 0) {
1164 return (VDEV_TYPE_L2CACHE
);
1171 * Construct a syntactically valid vdev specification,
1172 * and ensure that all devices and files exist and can be opened.
1173 * Note: we don't bother freeing anything in the error paths
1174 * because the program is just going to exit anyway.
1177 construct_spec(nvlist_t
*props
, int argc
, char **argv
)
1179 nvlist_t
*nvroot
, *nv
, **top
, **spares
, **l2cache
;
1180 int t
, toplevels
, mindev
, maxdev
, nspares
, nlogs
, nl2cache
;
1182 uint64_t is_log
, is_special
, is_dedup
;
1183 boolean_t seen_logs
;
1192 is_log
= is_special
= is_dedup
= B_FALSE
;
1193 seen_logs
= B_FALSE
;
1200 * If it's a mirror or raidz, the subsequent arguments are
1201 * its leaves -- until we encounter the next mirror or raidz.
1203 if ((type
= is_grouping(argv
[0], &mindev
, &maxdev
)) != NULL
) {
1204 nvlist_t
**child
= NULL
;
1205 int c
, children
= 0;
1207 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1208 if (spares
!= NULL
) {
1209 (void) fprintf(stderr
,
1210 gettext("invalid vdev "
1211 "specification: 'spare' can be "
1212 "specified only once\n"));
1215 is_log
= is_special
= is_dedup
= B_FALSE
;
1218 if (strcmp(type
, VDEV_TYPE_LOG
) == 0) {
1220 (void) fprintf(stderr
,
1221 gettext("invalid vdev "
1222 "specification: 'log' can be "
1223 "specified only once\n"));
1228 is_special
= B_FALSE
;
1233 * A log is not a real grouping device.
1234 * We just set is_log and continue.
1239 if (strcmp(type
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1240 is_special
= B_TRUE
;
1248 if (strcmp(type
, VDEV_ALLOC_BIAS_DEDUP
) == 0) {
1251 is_special
= B_FALSE
;
1257 if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1258 if (l2cache
!= NULL
) {
1259 (void) fprintf(stderr
,
1260 gettext("invalid vdev "
1261 "specification: 'cache' can be "
1262 "specified only once\n"));
1265 is_log
= is_special
= is_dedup
= B_FALSE
;
1268 if (is_log
|| is_special
|| is_dedup
) {
1269 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
1270 (void) fprintf(stderr
,
1271 gettext("invalid vdev "
1272 "specification: unsupported '%s' "
1273 "device: %s\n"), is_log
? "log" :
1280 for (c
= 1; c
< argc
; c
++) {
1281 if (is_grouping(argv
[c
], NULL
, NULL
) != NULL
)
1284 child
= realloc(child
,
1285 children
* sizeof (nvlist_t
*));
1288 if ((nv
= make_leaf_vdev(props
, argv
[c
],
1289 B_FALSE
)) == NULL
) {
1290 for (c
= 0; c
< children
- 1; c
++)
1291 nvlist_free(child
[c
]);
1296 child
[children
- 1] = nv
;
1299 if (children
< mindev
) {
1300 (void) fprintf(stderr
, gettext("invalid vdev "
1301 "specification: %s requires at least %d "
1302 "devices\n"), argv
[0], mindev
);
1303 for (c
= 0; c
< children
; c
++)
1304 nvlist_free(child
[c
]);
1309 if (children
> maxdev
) {
1310 (void) fprintf(stderr
, gettext("invalid vdev "
1311 "specification: %s supports no more than "
1312 "%d devices\n"), argv
[0], maxdev
);
1313 for (c
= 0; c
< children
; c
++)
1314 nvlist_free(child
[c
]);
1322 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1326 } else if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1328 nl2cache
= children
;
1331 /* create a top-level vdev with 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);
1339 verify(nvlist_add_string(nv
,
1340 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1341 VDEV_ALLOC_BIAS_LOG
) == 0);
1343 verify(nvlist_add_string(nv
,
1344 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1345 VDEV_ALLOC_BIAS_SPECIAL
) == 0);
1348 verify(nvlist_add_string(nv
,
1349 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1350 VDEV_ALLOC_BIAS_DEDUP
) == 0);
1352 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
1353 verify(nvlist_add_uint64(nv
,
1354 ZPOOL_CONFIG_NPARITY
,
1357 verify(nvlist_add_nvlist_array(nv
,
1358 ZPOOL_CONFIG_CHILDREN
, child
,
1361 for (c
= 0; c
< children
; c
++)
1362 nvlist_free(child
[c
]);
1367 * We have a device. Pass off to make_leaf_vdev() to
1368 * construct the appropriate nvlist describing the vdev.
1370 if ((nv
= make_leaf_vdev(props
, argv
[0],
1377 verify(nvlist_add_string(nv
,
1378 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1379 VDEV_ALLOC_BIAS_SPECIAL
) == 0);
1382 verify(nvlist_add_string(nv
,
1383 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1384 VDEV_ALLOC_BIAS_DEDUP
) == 0);
1391 top
= realloc(top
, toplevels
* sizeof (nvlist_t
*));
1394 top
[toplevels
- 1] = nv
;
1397 if (toplevels
== 0 && nspares
== 0 && nl2cache
== 0) {
1398 (void) fprintf(stderr
, gettext("invalid vdev "
1399 "specification: at least one toplevel vdev must be "
1404 if (seen_logs
&& nlogs
== 0) {
1405 (void) fprintf(stderr
, gettext("invalid vdev specification: "
1406 "log requires at least 1 device\n"));
1411 * Finally, create nvroot and add all top-level vdevs to it.
1413 verify(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, 0) == 0);
1414 verify(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
1415 VDEV_TYPE_ROOT
) == 0);
1416 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1417 top
, toplevels
) == 0);
1419 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1420 spares
, nspares
) == 0);
1422 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1423 l2cache
, nl2cache
) == 0);
1426 for (t
= 0; t
< toplevels
; t
++)
1427 nvlist_free(top
[t
]);
1428 for (t
= 0; t
< nspares
; t
++)
1429 nvlist_free(spares
[t
]);
1430 for (t
= 0; t
< nl2cache
; t
++)
1431 nvlist_free(l2cache
[t
]);
1441 split_mirror_vdev(zpool_handle_t
*zhp
, char *newname
, nvlist_t
*props
,
1442 splitflags_t flags
, int argc
, char **argv
)
1444 nvlist_t
*newroot
= NULL
, **child
;
1448 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
) {
1449 (void) fprintf(stderr
, gettext("Unable to build a "
1450 "pool from the specified devices\n"));
1454 if (!flags
.dryrun
&& make_disks(zhp
, newroot
) != 0) {
1455 nvlist_free(newroot
);
1459 /* avoid any tricks in the spec */
1460 verify(nvlist_lookup_nvlist_array(newroot
,
1461 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
1462 for (c
= 0; c
< children
; c
++) {
1467 verify(nvlist_lookup_string(child
[c
],
1468 ZPOOL_CONFIG_PATH
, &path
) == 0);
1469 if ((type
= is_grouping(path
, &min
, &max
)) != NULL
) {
1470 (void) fprintf(stderr
, gettext("Cannot use "
1471 "'%s' as a device for splitting\n"), type
);
1472 nvlist_free(newroot
);
1478 if (zpool_vdev_split(zhp
, newname
, &newroot
, props
, flags
) != 0) {
1479 nvlist_free(newroot
);
1487 num_normal_vdevs(nvlist_t
*nvroot
)
1490 uint_t t
, toplevels
, normal
= 0;
1492 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1493 &top
, &toplevels
) == 0);
1495 for (t
= 0; t
< toplevels
; t
++) {
1496 uint64_t log
= B_FALSE
;
1498 (void) nvlist_lookup_uint64(top
[t
], ZPOOL_CONFIG_IS_LOG
, &log
);
1501 if (nvlist_exists(top
[t
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
1511 * Get and validate the contents of the given vdev specification. This ensures
1512 * that the nvlist returned is well-formed, that all the devices exist, and that
1513 * they are not currently in use by any other known consumer. The 'poolconfig'
1514 * parameter is the current configuration of the pool when adding devices
1515 * existing pool, and is used to perform additional checks, such as changing the
1516 * replication level of the pool. It can be 'NULL' to indicate that this is a
1517 * new pool. The 'force' flag controls whether devices should be forcefully
1518 * added, even if they appear in use.
1521 make_root_vdev(zpool_handle_t
*zhp
, nvlist_t
*props
, int force
, int check_rep
,
1522 boolean_t replacing
, boolean_t dryrun
, int argc
, char **argv
)
1525 nvlist_t
*poolconfig
= NULL
;
1529 * Construct the vdev specification. If this is successful, we know
1530 * that we have a valid specification, and that all devices can be
1533 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
)
1536 if (zhp
&& ((poolconfig
= zpool_get_config(zhp
, NULL
)) == NULL
)) {
1537 nvlist_free(newroot
);
1542 * Validate each device to make sure that it's not shared with another
1543 * subsystem. We do this even if 'force' is set, because there are some
1544 * uses (such as a dedicated dump device) that even '-f' cannot
1547 if (is_device_in_use(poolconfig
, newroot
, force
, replacing
, B_FALSE
)) {
1548 nvlist_free(newroot
);
1553 * Check the replication level of the given vdevs and report any errors
1554 * found. We include the existing pool spec, if any, as we need to
1555 * catch changes against the existing replication level.
1557 if (check_rep
&& check_replication(poolconfig
, newroot
) != 0) {
1558 nvlist_free(newroot
);
1563 * On pool create the new vdev spec must have one normal vdev.
1565 if (poolconfig
== NULL
&& num_normal_vdevs(newroot
) == 0) {
1566 vdev_error(gettext("at least one general top-level vdev must "
1568 nvlist_free(newroot
);
1573 * Run through the vdev specification and label any whole disks found.
1575 if (!dryrun
&& make_disks(zhp
, newroot
) != 0) {
1576 nvlist_free(newroot
);