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 https://opensource.org/licenses/CDDL-1.0.
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.
90 vdev_error(const char *fmt
, ...)
95 (void) fprintf(stderr
, gettext("invalid vdev specification\n"));
97 (void) fprintf(stderr
, gettext("use '-f' to override "
98 "the following errors:\n"));
100 (void) fprintf(stderr
, gettext("the following errors "
101 "must be manually repaired:\n"));
106 (void) vfprintf(stderr
, fmt
, ap
);
111 * Check that a file is valid. All we can do in this case is check that it's
112 * not in use by another pool, and not in use by swap.
115 check_file_generic(const char *file
, boolean_t force
, boolean_t isspare
)
123 if ((fd
= open(file
, O_RDONLY
)) < 0)
126 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) == 0 && inuse
) {
130 case POOL_STATE_ACTIVE
:
131 desc
= gettext("active");
134 case POOL_STATE_EXPORTED
:
135 desc
= gettext("exported");
138 case POOL_STATE_POTENTIALLY_ACTIVE
:
139 desc
= gettext("potentially active");
143 desc
= gettext("unknown");
148 * Allow hot spares to be shared between pools.
150 if (state
== POOL_STATE_SPARE
&& isspare
) {
156 if (state
== POOL_STATE_ACTIVE
||
157 state
== POOL_STATE_SPARE
|| !force
) {
159 case POOL_STATE_SPARE
:
160 vdev_error(gettext("%s is reserved as a hot "
161 "spare for pool %s\n"), file
, name
);
164 vdev_error(gettext("%s is part of %s pool "
165 "'%s'\n"), file
, desc
, name
);
179 * This may be a shorthand device path or it could be total gibberish.
180 * Check to see if it is a known device available in zfs_vdev_paths.
181 * As part of this check, see if we've been given an entire disk
182 * (minus the slice number).
185 is_shorthand_path(const char *arg
, char *path
, size_t path_size
,
186 struct stat64
*statbuf
, boolean_t
*wholedisk
)
190 error
= zfs_resolve_shortname(arg
, path
, path_size
);
192 *wholedisk
= zfs_dev_is_whole_disk(path
);
193 if (*wholedisk
|| (stat64(path
, statbuf
) == 0))
197 strlcpy(path
, arg
, path_size
);
198 memset(statbuf
, 0, sizeof (*statbuf
));
199 *wholedisk
= B_FALSE
;
205 * Determine if the given path is a hot spare within the given configuration.
206 * If no configuration is given we rely solely on the label.
209 is_spare(nvlist_t
*config
, const char *path
)
215 uint64_t guid
, spareguid
;
221 if (zpool_is_draid_spare(path
))
224 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
227 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0 ||
229 state
!= POOL_STATE_SPARE
||
230 zpool_read_label(fd
, &label
, NULL
) != 0) {
238 if (config
== NULL
) {
243 verify(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
246 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
248 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
249 &spares
, &nspares
) == 0) {
250 for (i
= 0; i
< nspares
; i
++) {
251 verify(nvlist_lookup_uint64(spares
[i
],
252 ZPOOL_CONFIG_GUID
, &spareguid
) == 0);
253 if (spareguid
== guid
)
262 * Create a leaf vdev. Determine if this is a file or a device. If it's a
263 * device, fill in the device id to make a complete nvlist. Valid forms for a
266 * /dev/xxx Complete disk path
267 * /xxx Full path to file
268 * xxx Shorthand for <zfs_vdev_paths>/xxx
269 * draid* Virtual dRAID spare
272 make_leaf_vdev(nvlist_t
*props
, const char *arg
, boolean_t is_primary
)
274 char path
[MAXPATHLEN
];
275 struct stat64 statbuf
;
276 nvlist_t
*vdev
= NULL
;
277 const char *type
= 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
));
312 } else if (zpool_is_draid_spare(arg
)) {
314 (void) fprintf(stderr
,
315 gettext("cannot open '%s': dRAID spares can only "
316 "be used to replace primary vdevs\n"), arg
);
321 strlcpy(path
, arg
, sizeof (path
));
322 type
= VDEV_TYPE_DRAID_SPARE
;
324 err
= is_shorthand_path(arg
, path
, sizeof (path
),
325 &statbuf
, &wholedisk
);
328 * If we got ENOENT, then the user gave us
329 * gibberish, so try to direct them with a
330 * reasonable error message. Otherwise,
331 * regurgitate strerror() since it's the best we
335 (void) fprintf(stderr
,
336 gettext("cannot open '%s': no such "
337 "device in %s\n"), arg
, DISK_ROOT
);
338 (void) fprintf(stderr
,
339 gettext("must be a full path or "
340 "shorthand device name\n"));
343 (void) fprintf(stderr
,
344 gettext("cannot open '%s': %s\n"),
345 path
, strerror(errno
));
353 * Determine whether this is a device or a file.
355 if (wholedisk
|| S_ISBLK(statbuf
.st_mode
)) {
356 type
= VDEV_TYPE_DISK
;
357 } else if (S_ISREG(statbuf
.st_mode
)) {
358 type
= VDEV_TYPE_FILE
;
360 fprintf(stderr
, gettext("cannot use '%s': must "
361 "be a block device or regular file\n"), path
);
367 * Finally, we have the complete device or file, and we know that it is
368 * acceptable to use. Construct the nvlist to describe this vdev. All
369 * vdevs have a 'path' element, and devices also have a 'devid' element.
371 verify(nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) == 0);
372 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_PATH
, path
) == 0);
373 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
, type
) == 0);
375 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
376 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_WHOLE_DISK
,
377 (uint64_t)wholedisk
) == 0);
380 * Override defaults if custom properties are provided.
383 const char *value
= NULL
;
385 if (nvlist_lookup_string(props
,
386 zpool_prop_to_name(ZPOOL_PROP_ASHIFT
), &value
) == 0) {
387 if (zfs_nicestrtonum(NULL
, value
, &ashift
) != 0) {
388 (void) fprintf(stderr
,
389 gettext("ashift must be a number.\n"));
393 (ashift
< ASHIFT_MIN
|| ashift
> ASHIFT_MAX
)) {
394 (void) fprintf(stderr
,
395 gettext("invalid 'ashift=%" PRIu64
"' "
396 "property: only values between %" PRId32
" "
397 "and %" PRId32
" are allowed.\n"),
398 ashift
, ASHIFT_MIN
, ASHIFT_MAX
);
405 * If the device is known to incorrectly report its physical sector
406 * size explicitly provide the known correct value.
411 if (check_sector_size_database(path
, §or_size
) == B_TRUE
)
412 ashift
= highbit64(sector_size
) - 1;
416 (void) nvlist_add_uint64(vdev
, ZPOOL_CONFIG_ASHIFT
, ashift
);
422 * Go through and verify the replication level of the pool is consistent.
423 * Performs the following checks:
425 * For the new spec, verifies that devices in mirrors and raidz are the
428 * If the current configuration already has inconsistent replication
429 * levels, ignore any other potential problems in the new spec.
431 * Otherwise, make sure that the current spec (if there is one) and the new
432 * spec have consistent replication levels.
434 * If there is no current spec (create), make sure new spec has at least
435 * one general purpose vdev.
437 typedef struct replication_level
{
438 const char *zprl_type
;
439 uint64_t zprl_children
;
440 uint64_t zprl_parity
;
441 } replication_level_t
;
443 #define ZPOOL_FUZZ (16 * 1024 * 1024)
446 * N.B. For the purposes of comparing replication levels dRAID can be
447 * considered functionally equivalent to raidz.
450 is_raidz_mirror(replication_level_t
*a
, replication_level_t
*b
,
451 replication_level_t
**raidz
, replication_level_t
**mirror
)
453 if ((strcmp(a
->zprl_type
, "raidz") == 0 ||
454 strcmp(a
->zprl_type
, "draid") == 0) &&
455 strcmp(b
->zprl_type
, "mirror") == 0) {
464 * Comparison for determining if dRAID and raidz where passed in either order.
467 is_raidz_draid(replication_level_t
*a
, replication_level_t
*b
)
469 if ((strcmp(a
->zprl_type
, "raidz") == 0 ||
470 strcmp(a
->zprl_type
, "draid") == 0) &&
471 (strcmp(b
->zprl_type
, "raidz") == 0 ||
472 strcmp(b
->zprl_type
, "draid") == 0)) {
480 * Given a list of toplevel vdevs, return the current replication level. If
481 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
482 * an error message will be displayed for each self-inconsistent vdev.
484 static replication_level_t
*
485 get_replication(nvlist_t
*nvroot
, boolean_t fatal
)
493 replication_level_t lastrep
= {0};
494 replication_level_t rep
;
495 replication_level_t
*ret
;
496 replication_level_t
*raidz
, *mirror
;
497 boolean_t dontreport
;
499 ret
= safe_malloc(sizeof (replication_level_t
));
501 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
502 &top
, &toplevels
) == 0);
504 for (t
= 0; t
< toplevels
; t
++) {
505 uint64_t is_log
= B_FALSE
;
510 * For separate logs we ignore the top level vdev replication
513 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &is_log
);
518 * Ignore holes introduced by removing aux devices, along
519 * with indirect vdevs introduced by previously removed
522 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
523 if (strcmp(type
, VDEV_TYPE_HOLE
) == 0 ||
524 strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
527 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
528 &child
, &children
) != 0) {
530 * This is a 'file' or 'disk' vdev.
532 rep
.zprl_type
= type
;
533 rep
.zprl_children
= 1;
539 * This is a mirror or RAID-Z vdev. Go through and make
540 * sure the contents are all the same (files vs. disks),
541 * keeping track of the number of elements in the
544 * We also check that the size of each vdev (if it can
545 * be determined) is the same.
547 rep
.zprl_type
= type
;
548 rep
.zprl_children
= 0;
550 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
551 strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
552 verify(nvlist_lookup_uint64(nv
,
553 ZPOOL_CONFIG_NPARITY
,
554 &rep
.zprl_parity
) == 0);
555 assert(rep
.zprl_parity
!= 0);
561 * The 'dontreport' variable indicates that we've
562 * already reported an error for this spec, so don't
563 * bother doing it again.
568 for (c
= 0; c
< children
; c
++) {
569 nvlist_t
*cnv
= child
[c
];
571 struct stat64 statbuf
;
573 const char *childtype
;
578 verify(nvlist_lookup_string(cnv
,
579 ZPOOL_CONFIG_TYPE
, &childtype
) == 0);
582 * If this is a replacing or spare vdev, then
583 * get the real first child of the vdev: do this
584 * in a loop because replacing and spare vdevs
587 while (strcmp(childtype
,
588 VDEV_TYPE_REPLACING
) == 0 ||
589 strcmp(childtype
, VDEV_TYPE_SPARE
) == 0) {
593 verify(nvlist_lookup_nvlist_array(cnv
,
594 ZPOOL_CONFIG_CHILDREN
, &rchild
,
596 assert(rchildren
== 2);
599 verify(nvlist_lookup_string(cnv
,
604 verify(nvlist_lookup_string(cnv
,
605 ZPOOL_CONFIG_PATH
, &path
) == 0);
608 * If we have a raidz/mirror that combines disks
609 * with files, report it as an error.
611 if (!dontreport
&& type
!= NULL
&&
612 strcmp(type
, childtype
) != 0) {
618 "mismatched replication "
619 "level: %s contains both "
620 "files and devices\n"),
628 * According to stat(2), the value of 'st_size'
629 * is undefined for block devices and character
630 * devices. But there is no effective way to
631 * determine the real size in userland.
633 * Instead, we'll take advantage of an
634 * implementation detail of spec_size(). If the
635 * device is currently open, then we (should)
636 * return a valid size.
638 * If we still don't get a valid size (indicated
639 * by a size of 0 or MAXOFFSET_T), then ignore
640 * this device altogether.
642 if ((fd
= open(path
, O_RDONLY
)) >= 0) {
643 err
= fstat64_blk(fd
, &statbuf
);
646 err
= stat64(path
, &statbuf
);
650 statbuf
.st_size
== 0 ||
651 statbuf
.st_size
== MAXOFFSET_T
)
654 size
= statbuf
.st_size
;
657 * Also make sure that devices and
658 * slices have a consistent size. If
659 * they differ by a significant amount
660 * (~16MB) then report an error.
663 (vdev_size
!= -1LL &&
664 (llabs(size
- vdev_size
) >
671 "%s contains devices of "
672 "different sizes\n"),
685 * At this point, we have the replication of the last toplevel
686 * vdev in 'rep'. Compare it to 'lastrep' to see if it is
689 if (lastrep
.zprl_type
!= NULL
) {
690 if (is_raidz_mirror(&lastrep
, &rep
, &raidz
, &mirror
) ||
691 is_raidz_mirror(&rep
, &lastrep
, &raidz
, &mirror
)) {
693 * Accepted raidz and mirror when they can
694 * handle the same number of disk failures.
696 if (raidz
->zprl_parity
!=
697 mirror
->zprl_children
- 1) {
703 "mismatched replication "
705 "%s and %s vdevs with "
706 "different redundancy, "
707 "%llu vs. %llu (%llu-way) "
714 mirror
->zprl_children
- 1,
716 mirror
->zprl_children
);
720 } else if (is_raidz_draid(&lastrep
, &rep
)) {
722 * Accepted raidz and draid when they can
723 * handle the same number of disk failures.
725 if (lastrep
.zprl_parity
!= rep
.zprl_parity
) {
731 "mismatched replication "
732 "level: %s and %s vdevs "
734 "redundancy, %llu vs. "
735 "%llu are present\n"),
745 } else if (strcmp(lastrep
.zprl_type
, rep
.zprl_type
) !=
752 "mismatched replication level: "
753 "both %s and %s vdevs are "
755 lastrep
.zprl_type
, rep
.zprl_type
);
758 } else if (lastrep
.zprl_parity
!= rep
.zprl_parity
) {
764 "mismatched replication level: "
765 "both %llu and %llu device parity "
766 "%s vdevs are present\n"),
769 (u_longlong_t
)rep
.zprl_parity
,
773 } else if (lastrep
.zprl_children
!= rep
.zprl_children
) {
779 "mismatched replication level: "
780 "both %llu-way and %llu-way %s "
781 "vdevs are present\n"),
783 lastrep
.zprl_children
,
801 * Check the replication level of the vdev spec against the current pool. Calls
802 * get_replication() to make sure the new spec is self-consistent. If the pool
803 * has a consistent replication level, then we ignore any errors. Otherwise,
804 * report any difference between the two.
807 check_replication(nvlist_t
*config
, nvlist_t
*newroot
)
811 replication_level_t
*current
= NULL
, *new;
812 replication_level_t
*raidz
, *mirror
;
816 * If we have a current pool configuration, check to see if it's
817 * self-consistent. If not, simply return success.
819 if (config
!= NULL
) {
822 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
824 if ((current
= get_replication(nvroot
, B_FALSE
)) == NULL
)
828 * for spares there may be no children, and therefore no
829 * replication level to check
831 if ((nvlist_lookup_nvlist_array(newroot
, ZPOOL_CONFIG_CHILDREN
,
832 &child
, &children
) != 0) || (children
== 0)) {
838 * If all we have is logs then there's no replication level to check.
840 if (num_logs(newroot
) == children
) {
846 * Get the replication level of the new vdev spec, reporting any
847 * inconsistencies found.
849 if ((new = get_replication(newroot
, B_TRUE
)) == NULL
) {
855 * Check to see if the new vdev spec matches the replication level of
859 if (current
!= NULL
) {
860 if (is_raidz_mirror(current
, new, &raidz
, &mirror
) ||
861 is_raidz_mirror(new, current
, &raidz
, &mirror
)) {
862 if (raidz
->zprl_parity
!= mirror
->zprl_children
- 1) {
864 "mismatched replication level: pool and "
865 "new vdev with different redundancy, %s "
866 "and %s vdevs, %llu vs. %llu (%llu-way)\n"),
869 (u_longlong_t
)raidz
->zprl_parity
,
870 (u_longlong_t
)mirror
->zprl_children
- 1,
871 (u_longlong_t
)mirror
->zprl_children
);
874 } else if (strcmp(current
->zprl_type
, new->zprl_type
) != 0) {
876 "mismatched replication level: pool uses %s "
877 "and new vdev is %s\n"),
878 current
->zprl_type
, new->zprl_type
);
880 } else if (current
->zprl_parity
!= new->zprl_parity
) {
882 "mismatched replication level: pool uses %llu "
883 "device parity and new vdev uses %llu\n"),
884 (u_longlong_t
)current
->zprl_parity
,
885 (u_longlong_t
)new->zprl_parity
);
887 } else if (current
->zprl_children
!= new->zprl_children
) {
889 "mismatched replication level: pool uses %llu-way "
890 "%s and new vdev uses %llu-way %s\n"),
891 (u_longlong_t
)current
->zprl_children
,
893 (u_longlong_t
)new->zprl_children
,
907 zero_label(const char *path
)
909 const int size
= 4096;
913 if ((fd
= open(path
, O_WRONLY
|O_EXCL
)) < 0) {
914 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
915 path
, strerror(errno
));
919 memset(buf
, 0, size
);
920 err
= write(fd
, buf
, size
);
921 (void) fdatasync(fd
);
925 (void) fprintf(stderr
, gettext("cannot zero first %d bytes "
926 "of '%s': %s\n"), size
, path
, strerror(errno
));
931 (void) fprintf(stderr
, gettext("could only zero %d/%d bytes "
932 "of '%s'\n"), err
, size
, path
);
940 lines_to_stderr(char *lines
[], int lines_cnt
)
943 for (i
= 0; i
< lines_cnt
; i
++) {
944 fprintf(stderr
, "%s\n", lines
[i
]);
949 * Go through and find any whole disks in the vdev specification, labelling them
950 * as appropriate. When constructing the vdev spec, we were unable to open this
951 * device in order to provide a devid. Now that we have labelled the disk and
952 * know that slice 0 is valid, we can construct the devid now.
954 * If the disk was already labeled with an EFI label, we will have gotten the
955 * devid already (because we were able to open the whole disk). Otherwise, we
956 * need to get the devid after we label the disk.
959 make_disks(zpool_handle_t
*zhp
, nvlist_t
*nv
, boolean_t replacing
)
963 const char *type
, *path
;
964 char devpath
[MAXPATHLEN
];
965 char udevpath
[MAXPATHLEN
];
967 struct stat64 statbuf
;
968 int is_exclusive
= 0;
972 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
974 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
975 &child
, &children
) != 0) {
977 if (strcmp(type
, VDEV_TYPE_DISK
) != 0)
981 * We have a disk device. If this is a whole disk write
982 * out the efi partition table, otherwise write zero's to
983 * the first 4k of the partition. This is to ensure that
984 * libblkid will not misidentify the partition due to a
985 * magic value left by the previous filesystem.
987 verify(!nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
));
988 verify(!nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
993 * Update device id string for mpath nodes (Linux only)
995 if (is_mpath_whole_disk(path
))
996 update_vdev_config_dev_strs(nv
);
998 if (!is_spare(NULL
, path
))
999 (void) zero_label(path
);
1003 if (realpath(path
, devpath
) == NULL
) {
1005 (void) fprintf(stderr
,
1006 gettext("cannot resolve path '%s'\n"), path
);
1011 * Remove any previously existing symlink from a udev path to
1012 * the device before labeling the disk. This ensures that
1013 * only newly created links are used. Otherwise there is a
1014 * window between when udev deletes and recreates the link
1015 * during which access attempts will fail with ENOENT.
1017 strlcpy(udevpath
, path
, MAXPATHLEN
);
1018 (void) zfs_append_partition(udevpath
, MAXPATHLEN
);
1020 fd
= open(devpath
, O_RDWR
|O_EXCL
);
1033 * If the partition exists, contains a valid spare label,
1034 * and is opened exclusively there is no need to partition
1035 * it. Hot spares have already been partitioned and are
1036 * held open exclusively by the kernel as a safety measure.
1038 * If the provided path is for a /dev/disk/ device its
1039 * symbolic link will be removed, partition table created,
1040 * and then block until udev creates the new link.
1042 if (!is_exclusive
&& !is_spare(NULL
, udevpath
)) {
1043 char *devnode
= strrchr(devpath
, '/') + 1;
1044 char **lines
= NULL
;
1047 ret
= strncmp(udevpath
, UDISK_ROOT
, strlen(UDISK_ROOT
));
1049 ret
= lstat64(udevpath
, &statbuf
);
1050 if (ret
== 0 && S_ISLNK(statbuf
.st_mode
))
1051 (void) unlink(udevpath
);
1055 * When labeling a pool the raw device node name
1056 * is provided as it appears under /dev/.
1058 * Note that 'zhp' will be NULL when we're creating a
1061 if (zpool_prepare_and_label_disk(g_zfs
, zhp
, devnode
,
1062 nv
, zhp
== NULL
? "create" :
1063 replacing
? "replace" : "add", &lines
,
1065 (void) fprintf(stderr
,
1067 "Error preparing/labeling disk.\n"));
1068 if (lines_cnt
> 0) {
1069 (void) fprintf(stderr
,
1070 gettext("zfs_prepare_disk output:\n"));
1071 lines_to_stderr(lines
, lines_cnt
);
1074 libzfs_free_str_array(lines
, lines_cnt
);
1077 libzfs_free_str_array(lines
, lines_cnt
);
1080 * Wait for udev to signal the device is available
1081 * by the provided path.
1083 ret
= zpool_label_disk_wait(udevpath
, DISK_LABEL_WAIT
);
1085 (void) fprintf(stderr
,
1086 gettext("missing link: %s was "
1087 "partitioned but %s is missing\n"),
1092 ret
= zero_label(udevpath
);
1098 * Update the path to refer to the partition. The presence of
1099 * the 'whole_disk' field indicates to the CLI that we should
1100 * chop off the partition number when displaying the device in
1103 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_PATH
, udevpath
) == 0);
1106 * Update device id strings for whole disks (Linux only)
1108 update_vdev_config_dev_strs(nv
);
1113 for (c
= 0; c
< children
; c
++)
1114 if ((ret
= make_disks(zhp
, child
[c
], replacing
)) != 0)
1117 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1118 &child
, &children
) == 0)
1119 for (c
= 0; c
< children
; c
++)
1120 if ((ret
= make_disks(zhp
, child
[c
], replacing
)) != 0)
1123 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1124 &child
, &children
) == 0)
1125 for (c
= 0; c
< children
; c
++)
1126 if ((ret
= make_disks(zhp
, child
[c
], replacing
)) != 0)
1133 * Go through and find any devices that are in use. We rely on libdiskmgt for
1134 * the majority of this task.
1137 is_device_in_use(nvlist_t
*config
, nvlist_t
*nv
, boolean_t force
,
1138 boolean_t replacing
, boolean_t isspare
)
1142 const char *type
, *path
;
1144 char buf
[MAXPATHLEN
];
1145 uint64_t wholedisk
= B_FALSE
;
1146 boolean_t anyinuse
= B_FALSE
;
1148 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1150 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1151 &child
, &children
) != 0) {
1153 verify(!nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
));
1154 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1155 verify(!nvlist_lookup_uint64(nv
,
1156 ZPOOL_CONFIG_WHOLE_DISK
, &wholedisk
));
1159 * As a generic check, we look to see if this is a replace of a
1160 * hot spare within the same pool. If so, we allow it
1161 * regardless of what libblkid or zpool_in_use() says.
1164 (void) strlcpy(buf
, path
, sizeof (buf
));
1166 ret
= zfs_append_partition(buf
, sizeof (buf
));
1171 if (is_spare(config
, buf
))
1175 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1176 ret
= check_device(path
, force
, isspare
, wholedisk
);
1178 else if (strcmp(type
, VDEV_TYPE_FILE
) == 0)
1179 ret
= check_file(path
, force
, isspare
);
1184 for (c
= 0; c
< children
; c
++)
1185 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1189 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1190 &child
, &children
) == 0)
1191 for (c
= 0; c
< children
; c
++)
1192 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1196 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1197 &child
, &children
) == 0)
1198 for (c
= 0; c
< children
; c
++)
1199 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1207 * Returns the parity level extracted from a raidz or draid type.
1208 * If the parity cannot be determined zero is returned.
1211 get_parity(const char *type
)
1216 if (strncmp(type
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0) {
1217 p
= type
+ strlen(VDEV_TYPE_RAIDZ
);
1220 /* when unspecified default to single parity */
1222 } else if (*p
== '0') {
1223 /* no zero prefixes allowed */
1226 /* 0-3, no suffixes allowed */
1229 parity
= strtol(p
, &end
, 10);
1230 if (errno
!= 0 || *end
!= '\0' ||
1231 parity
< 1 || parity
> VDEV_RAIDZ_MAXPARITY
) {
1235 } else if (strncmp(type
, VDEV_TYPE_DRAID
,
1236 strlen(VDEV_TYPE_DRAID
)) == 0) {
1237 p
= type
+ strlen(VDEV_TYPE_DRAID
);
1239 if (*p
== '\0' || *p
== ':') {
1240 /* when unspecified default to single parity */
1242 } else if (*p
== '0') {
1243 /* no zero prefixes allowed */
1246 /* 0-3, allowed suffixes: '\0' or ':' */
1249 parity
= strtol(p
, &end
, 10);
1251 parity
< 1 || parity
> VDEV_DRAID_MAXPARITY
||
1252 (*end
!= '\0' && *end
!= ':')) {
1258 return ((int)parity
);
1262 * Assign the minimum and maximum number of devices allowed for
1263 * the specified type. On error NULL is returned, otherwise the
1264 * type prefix is returned (raidz, mirror, etc).
1267 is_grouping(const char *type
, int *mindev
, int *maxdev
)
1271 if (strncmp(type
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
1272 strncmp(type
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0) {
1273 nparity
= get_parity(type
);
1277 *mindev
= nparity
+ 1;
1281 if (strncmp(type
, VDEV_TYPE_RAIDZ
,
1282 strlen(VDEV_TYPE_RAIDZ
)) == 0) {
1283 return (VDEV_TYPE_RAIDZ
);
1285 return (VDEV_TYPE_DRAID
);
1292 if (strcmp(type
, "mirror") == 0) {
1295 return (VDEV_TYPE_MIRROR
);
1298 if (strcmp(type
, "spare") == 0) {
1301 return (VDEV_TYPE_SPARE
);
1304 if (strcmp(type
, "log") == 0) {
1307 return (VDEV_TYPE_LOG
);
1310 if (strcmp(type
, VDEV_ALLOC_BIAS_SPECIAL
) == 0 ||
1311 strcmp(type
, VDEV_ALLOC_BIAS_DEDUP
) == 0) {
1317 if (strcmp(type
, "cache") == 0) {
1320 return (VDEV_TYPE_L2CACHE
);
1327 * Extract the configuration parameters encoded in the dRAID type and
1328 * use them to generate a dRAID configuration. The expected format is:
1330 * draid[<parity>][:<data><d|D>][:<children><c|C>][:<spares><s|S>]
1332 * The intent is to be able to generate a good configuration when no
1333 * additional information is provided. The only mandatory component
1334 * of the 'type' is the 'draid' prefix. If a value is not provided
1335 * then reasonable defaults are used. The optional components may
1336 * appear in any order but the d/s/c suffix is required.
1339 * - data: number of data devices per group (1-255)
1340 * - parity: number of parity blocks per group (1-3)
1341 * - spares: number of distributed spare (0-100)
1342 * - children: total number of devices (1-255)
1345 * - zpool create tank draid <devices...>
1346 * - zpool create tank draid2:8d:51c:2s <devices...>
1349 draid_config_by_type(nvlist_t
*nv
, const char *type
, uint64_t children
)
1351 uint64_t nparity
= 1;
1352 uint64_t nspares
= 0;
1353 uint64_t ndata
= UINT64_MAX
;
1354 uint64_t ngroups
= 1;
1357 if (strncmp(type
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) != 0)
1360 nparity
= (uint64_t)get_parity(type
);
1361 if (nparity
== 0 || nparity
> VDEV_DRAID_MAXPARITY
) {
1363 gettext("invalid dRAID parity level %llu; must be "
1364 "between 1 and %d\n"), (u_longlong_t
)nparity
,
1365 VDEV_DRAID_MAXPARITY
);
1369 char *p
= (char *)type
;
1370 while ((p
= strchr(p
, ':')) != NULL
) {
1376 if (!isdigit(p
[0])) {
1377 (void) fprintf(stderr
, gettext("invalid dRAID "
1378 "syntax; expected [:<number><c|d|s>] not '%s'\n"),
1383 /* Expected non-zero value with c/d/s suffix */
1384 value
= strtol(p
, &end
, 10);
1385 char suffix
= tolower(*end
);
1387 (suffix
!= 'c' && suffix
!= 'd' && suffix
!= 's')) {
1388 (void) fprintf(stderr
, gettext("invalid dRAID "
1389 "syntax; expected [:<number><c|d|s>] not '%s'\n"),
1394 if (suffix
== 'c') {
1395 if ((uint64_t)value
!= children
) {
1397 gettext("invalid number of dRAID children; "
1398 "%llu required but %llu provided\n"),
1399 (u_longlong_t
)value
,
1400 (u_longlong_t
)children
);
1403 } else if (suffix
== 'd') {
1404 ndata
= (uint64_t)value
;
1405 } else if (suffix
== 's') {
1406 nspares
= (uint64_t)value
;
1408 verify(0); /* Unreachable */
1413 * When a specific number of data disks is not provided limit a
1414 * redundancy group to 8 data disks. This value was selected to
1415 * provide a reasonable tradeoff between capacity and performance.
1417 if (ndata
== UINT64_MAX
) {
1418 if (children
> nspares
+ nparity
) {
1419 ndata
= MIN(children
- nspares
- nparity
, 8);
1421 fprintf(stderr
, gettext("request number of "
1422 "distributed spares %llu and parity level %llu\n"
1423 "leaves no disks available for data\n"),
1424 (u_longlong_t
)nspares
, (u_longlong_t
)nparity
);
1429 /* Verify the maximum allowed group size is never exceeded. */
1430 if (ndata
== 0 || (ndata
+ nparity
> children
- nspares
)) {
1431 fprintf(stderr
, gettext("requested number of dRAID data "
1432 "disks per group %llu is too high,\nat most %llu disks "
1433 "are available for data\n"), (u_longlong_t
)ndata
,
1434 (u_longlong_t
)(children
- nspares
- nparity
));
1439 * Verify the requested number of spares can be satisfied.
1440 * An arbitrary limit of 100 distributed spares is applied.
1442 if (nspares
> 100 || nspares
> (children
- (ndata
+ nparity
))) {
1444 gettext("invalid number of dRAID spares %llu; additional "
1445 "disks would be required\n"), (u_longlong_t
)nspares
);
1449 /* Verify the requested number children is sufficient. */
1450 if (children
< (ndata
+ nparity
+ nspares
)) {
1451 fprintf(stderr
, gettext("%llu disks were provided, but at "
1452 "least %llu disks are required for this config\n"),
1453 (u_longlong_t
)children
,
1454 (u_longlong_t
)(ndata
+ nparity
+ nspares
));
1457 if (children
> VDEV_DRAID_MAX_CHILDREN
) {
1458 fprintf(stderr
, gettext("%llu disks were provided, but "
1459 "dRAID only supports up to %u disks"),
1460 (u_longlong_t
)children
, VDEV_DRAID_MAX_CHILDREN
);
1464 * Calculate the minimum number of groups required to fill a slice.
1465 * This is the LCM of the stripe width (ndata + nparity) and the
1466 * number of data drives (children - nspares).
1468 while (ngroups
* (ndata
+ nparity
) % (children
- nspares
) != 0)
1471 /* Store the basic dRAID configuration. */
1472 fnvlist_add_uint64(nv
, ZPOOL_CONFIG_NPARITY
, nparity
);
1473 fnvlist_add_uint64(nv
, ZPOOL_CONFIG_DRAID_NDATA
, ndata
);
1474 fnvlist_add_uint64(nv
, ZPOOL_CONFIG_DRAID_NSPARES
, nspares
);
1475 fnvlist_add_uint64(nv
, ZPOOL_CONFIG_DRAID_NGROUPS
, ngroups
);
1481 * Construct a syntactically valid vdev specification,
1482 * and ensure that all devices and files exist and can be opened.
1483 * Note: we don't bother freeing anything in the error paths
1484 * because the program is just going to exit anyway.
1487 construct_spec(nvlist_t
*props
, int argc
, char **argv
)
1489 nvlist_t
*nvroot
, *nv
, **top
, **spares
, **l2cache
;
1490 int t
, toplevels
, mindev
, maxdev
, nspares
, nlogs
, nl2cache
;
1491 const char *type
, *fulltype
;
1492 boolean_t is_log
, is_special
, is_dedup
, is_spare
;
1493 boolean_t seen_logs
;
1502 is_log
= is_special
= is_dedup
= is_spare
= B_FALSE
;
1503 seen_logs
= B_FALSE
;
1511 * If it's a mirror, raidz, or draid the subsequent arguments
1512 * are its leaves -- until we encounter the next mirror,
1515 if ((type
= is_grouping(fulltype
, &mindev
, &maxdev
)) != NULL
) {
1516 nvlist_t
**child
= NULL
;
1517 int c
, children
= 0;
1519 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1520 if (spares
!= NULL
) {
1521 (void) fprintf(stderr
,
1522 gettext("invalid vdev "
1523 "specification: 'spare' can be "
1524 "specified only once\n"));
1528 is_log
= is_special
= is_dedup
= B_FALSE
;
1531 if (strcmp(type
, VDEV_TYPE_LOG
) == 0) {
1533 (void) fprintf(stderr
,
1534 gettext("invalid vdev "
1535 "specification: 'log' can be "
1536 "specified only once\n"));
1541 is_special
= is_dedup
= is_spare
= B_FALSE
;
1545 * A log is not a real grouping device.
1546 * We just set is_log and continue.
1551 if (strcmp(type
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1552 is_special
= B_TRUE
;
1553 is_log
= is_dedup
= is_spare
= B_FALSE
;
1559 if (strcmp(type
, VDEV_ALLOC_BIAS_DEDUP
) == 0) {
1561 is_log
= is_special
= is_spare
= B_FALSE
;
1567 if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1568 if (l2cache
!= NULL
) {
1569 (void) fprintf(stderr
,
1570 gettext("invalid vdev "
1571 "specification: 'cache' can be "
1572 "specified only once\n"));
1575 is_log
= is_special
= B_FALSE
;
1576 is_dedup
= is_spare
= B_FALSE
;
1579 if (is_log
|| is_special
|| is_dedup
) {
1580 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
1581 (void) fprintf(stderr
,
1582 gettext("invalid vdev "
1583 "specification: unsupported '%s' "
1584 "device: %s\n"), is_log
? "log" :
1591 for (c
= 1; c
< argc
; c
++) {
1592 if (is_grouping(argv
[c
], NULL
, NULL
) != NULL
)
1596 child
= realloc(child
,
1597 children
* sizeof (nvlist_t
*));
1600 if ((nv
= make_leaf_vdev(props
, argv
[c
],
1601 !(is_log
|| is_special
|| is_dedup
||
1602 is_spare
))) == NULL
) {
1603 for (c
= 0; c
< children
- 1; c
++)
1604 nvlist_free(child
[c
]);
1609 child
[children
- 1] = nv
;
1612 if (children
< mindev
) {
1613 (void) fprintf(stderr
, gettext("invalid vdev "
1614 "specification: %s requires at least %d "
1615 "devices\n"), argv
[0], mindev
);
1616 for (c
= 0; c
< children
; c
++)
1617 nvlist_free(child
[c
]);
1622 if (children
> maxdev
) {
1623 (void) fprintf(stderr
, gettext("invalid vdev "
1624 "specification: %s supports no more than "
1625 "%d devices\n"), argv
[0], maxdev
);
1626 for (c
= 0; c
< children
; c
++)
1627 nvlist_free(child
[c
]);
1635 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1639 } else if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1641 nl2cache
= children
;
1644 /* create a top-level vdev with children */
1645 verify(nvlist_alloc(&nv
, NV_UNIQUE_NAME
,
1647 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_TYPE
,
1649 verify(nvlist_add_uint64(nv
,
1650 ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
1652 verify(nvlist_add_string(nv
,
1653 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1654 VDEV_ALLOC_BIAS_LOG
) == 0);
1657 verify(nvlist_add_string(nv
,
1658 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1659 VDEV_ALLOC_BIAS_SPECIAL
) == 0);
1662 verify(nvlist_add_string(nv
,
1663 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1664 VDEV_ALLOC_BIAS_DEDUP
) == 0);
1666 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
1667 verify(nvlist_add_uint64(nv
,
1668 ZPOOL_CONFIG_NPARITY
,
1671 if (strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
1672 if (draid_config_by_type(nv
,
1673 fulltype
, children
) != 0) {
1674 for (c
= 0; c
< children
; c
++)
1675 nvlist_free(child
[c
]);
1680 verify(nvlist_add_nvlist_array(nv
,
1681 ZPOOL_CONFIG_CHILDREN
,
1682 (const nvlist_t
**)child
, children
) == 0);
1684 for (c
= 0; c
< children
; c
++)
1685 nvlist_free(child
[c
]);
1690 * We have a device. Pass off to make_leaf_vdev() to
1691 * construct the appropriate nvlist describing the vdev.
1693 if ((nv
= make_leaf_vdev(props
, argv
[0], !(is_log
||
1694 is_special
|| is_dedup
|| is_spare
))) == NULL
)
1697 verify(nvlist_add_uint64(nv
,
1698 ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
1700 verify(nvlist_add_string(nv
,
1701 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1702 VDEV_ALLOC_BIAS_LOG
) == 0);
1707 verify(nvlist_add_string(nv
,
1708 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1709 VDEV_ALLOC_BIAS_SPECIAL
) == 0);
1712 verify(nvlist_add_string(nv
,
1713 ZPOOL_CONFIG_ALLOCATION_BIAS
,
1714 VDEV_ALLOC_BIAS_DEDUP
) == 0);
1721 top
= realloc(top
, toplevels
* sizeof (nvlist_t
*));
1724 top
[toplevels
- 1] = nv
;
1727 if (toplevels
== 0 && nspares
== 0 && nl2cache
== 0) {
1728 (void) fprintf(stderr
, gettext("invalid vdev "
1729 "specification: at least one toplevel vdev must be "
1734 if (seen_logs
&& nlogs
== 0) {
1735 (void) fprintf(stderr
, gettext("invalid vdev specification: "
1736 "log requires at least 1 device\n"));
1741 * Finally, create nvroot and add all top-level vdevs to it.
1743 verify(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, 0) == 0);
1744 verify(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
1745 VDEV_TYPE_ROOT
) == 0);
1746 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1747 (const nvlist_t
**)top
, toplevels
) == 0);
1749 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1750 (const nvlist_t
**)spares
, nspares
) == 0);
1752 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1753 (const nvlist_t
**)l2cache
, nl2cache
) == 0);
1756 for (t
= 0; t
< toplevels
; t
++)
1757 nvlist_free(top
[t
]);
1758 for (t
= 0; t
< nspares
; t
++)
1759 nvlist_free(spares
[t
]);
1760 for (t
= 0; t
< nl2cache
; t
++)
1761 nvlist_free(l2cache
[t
]);
1771 split_mirror_vdev(zpool_handle_t
*zhp
, char *newname
, nvlist_t
*props
,
1772 splitflags_t flags
, int argc
, char **argv
)
1774 nvlist_t
*newroot
= NULL
, **child
;
1778 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
) {
1779 (void) fprintf(stderr
, gettext("Unable to build a "
1780 "pool from the specified devices\n"));
1784 if (!flags
.dryrun
&& make_disks(zhp
, newroot
, B_FALSE
) != 0) {
1785 nvlist_free(newroot
);
1789 /* avoid any tricks in the spec */
1790 verify(nvlist_lookup_nvlist_array(newroot
,
1791 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
1792 for (c
= 0; c
< children
; c
++) {
1797 verify(nvlist_lookup_string(child
[c
],
1798 ZPOOL_CONFIG_PATH
, &path
) == 0);
1799 if ((type
= is_grouping(path
, &min
, &max
)) != NULL
) {
1800 (void) fprintf(stderr
, gettext("Cannot use "
1801 "'%s' as a device for splitting\n"), type
);
1802 nvlist_free(newroot
);
1808 if (zpool_vdev_split(zhp
, newname
, &newroot
, props
, flags
) != 0) {
1809 nvlist_free(newroot
);
1817 num_normal_vdevs(nvlist_t
*nvroot
)
1820 uint_t t
, toplevels
, normal
= 0;
1822 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1823 &top
, &toplevels
) == 0);
1825 for (t
= 0; t
< toplevels
; t
++) {
1826 uint64_t log
= B_FALSE
;
1828 (void) nvlist_lookup_uint64(top
[t
], ZPOOL_CONFIG_IS_LOG
, &log
);
1831 if (nvlist_exists(top
[t
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
1841 * Get and validate the contents of the given vdev specification. This ensures
1842 * that the nvlist returned is well-formed, that all the devices exist, and that
1843 * they are not currently in use by any other known consumer. The 'poolconfig'
1844 * parameter is the current configuration of the pool when adding devices
1845 * existing pool, and is used to perform additional checks, such as changing the
1846 * replication level of the pool. It can be 'NULL' to indicate that this is a
1847 * new pool. The 'force' flag controls whether devices should be forcefully
1848 * added, even if they appear in use.
1851 make_root_vdev(zpool_handle_t
*zhp
, nvlist_t
*props
, int force
, int check_rep
,
1852 boolean_t replacing
, boolean_t dryrun
, int argc
, char **argv
)
1855 nvlist_t
*poolconfig
= NULL
;
1859 * Construct the vdev specification. If this is successful, we know
1860 * that we have a valid specification, and that all devices can be
1863 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
)
1866 if (zhp
&& ((poolconfig
= zpool_get_config(zhp
, NULL
)) == NULL
)) {
1867 nvlist_free(newroot
);
1872 * Validate each device to make sure that it's not shared with another
1873 * subsystem. We do this even if 'force' is set, because there are some
1874 * uses (such as a dedicated dump device) that even '-f' cannot
1877 if (is_device_in_use(poolconfig
, newroot
, force
, replacing
, B_FALSE
)) {
1878 nvlist_free(newroot
);
1883 * Check the replication level of the given vdevs and report any errors
1884 * found. We include the existing pool spec, if any, as we need to
1885 * catch changes against the existing replication level.
1887 if (check_rep
&& check_replication(poolconfig
, newroot
) != 0) {
1888 nvlist_free(newroot
);
1893 * On pool create the new vdev spec must have one normal vdev.
1895 if (poolconfig
== NULL
&& num_normal_vdevs(newroot
) == 0) {
1896 vdev_error(gettext("at least one general top-level vdev must "
1898 nvlist_free(newroot
);
1903 * Run through the vdev specification and label any whole disks found.
1905 if (!dryrun
&& make_disks(zhp
, newroot
, replacing
) != 0) {
1906 nvlist_free(newroot
);