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 libdiskmgt, makes 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.
67 #include <libnvpair.h>
75 #include <sys/mntent.h>
78 #include "zpool_util.h"
80 #define DISK_ROOT "/dev/dsk"
81 #define RDISK_ROOT "/dev/rdsk"
82 #define BACKUP_SLICE "s2"
85 * For any given vdev specification, we can have multiple errors. The
86 * vdev_error() function keeps track of whether we have seen an error yet, and
87 * 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);
116 libdiskmgt_error(int error)
119 * ENXIO/ENODEV is a valid error message if the device doesn't live in
120 * /dev/dsk. Don't bother printing an error message in this case.
122 if (error == ENXIO || error == ENODEV)
125 (void) fprintf(stderr, gettext("warning: device in use checking "
126 "failed: %s\n"), strerror(error));
130 * Validate a device, passing the bulk of the work off to libdiskmgt.
133 check_slice(const char *path, int force, boolean_t wholedisk, boolean_t isspare)
140 who = DM_WHO_ZPOOL_FORCE;
142 who = DM_WHO_ZPOOL_SPARE;
146 if (dm_inuse((char *)path, &msg, who, &error) || error) {
148 libdiskmgt_error(error);
151 vdev_error("%s", msg);
158 * If we're given a whole disk, ignore overlapping slices since we're
159 * about to label it anyway.
162 if (!wholedisk && !force &&
163 (dm_isoverlapping((char *)path, &msg, &error) || error)) {
165 /* dm_isoverlapping returned -1 */
166 vdev_error(gettext("%s overlaps with %s\n"), path, msg);
169 } else if (error != ENODEV) {
170 /* libdiskmgt's devcache only handles physical drives */
171 libdiskmgt_error(error);
181 * Validate a whole disk. Iterate over all slices on the disk and make sure
182 * that none is in use by calling check_slice().
185 check_disk(const char *name, dm_descriptor_t disk, int force, int isspare)
187 dm_descriptor_t *drive, *media, *slice;
193 * Get the drive associated with this disk. This should never fail,
194 * because we already have an alias handle open for the device.
196 if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE,
197 &err)) == NULL || *drive == NULL) {
199 libdiskmgt_error(err);
203 if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA,
205 dm_free_descriptors(drive);
207 libdiskmgt_error(err);
211 dm_free_descriptors(drive);
214 * It is possible that the user has specified a removable media drive,
215 * and the media is not present.
217 if (*media == NULL) {
218 dm_free_descriptors(media);
219 vdev_error(gettext("'%s' has no media in drive\n"), name);
223 if ((slice = dm_get_associated_descriptors(*media, DM_SLICE,
225 dm_free_descriptors(media);
227 libdiskmgt_error(err);
231 dm_free_descriptors(media);
236 * Iterate over all slices and report any errors. We don't care about
237 * overlapping slices because we are using the whole disk.
239 for (i = 0; slice[i] != NULL; i++) {
240 char *name = dm_get_name(slice[i], &err);
242 if (check_slice(name, force, B_TRUE, isspare) != 0)
248 dm_free_descriptors(slice);
256 check_device(const char *path, boolean_t force, boolean_t isspare)
258 dm_descriptor_t desc;
263 * For whole disks, libdiskmgt does not include the leading dev path.
265 dev = strrchr(path, '/');
268 if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err)) != NULL) {
269 err = check_disk(path, desc, force, isspare);
270 dm_free_descriptor(desc);
274 return (check_slice(path, force, B_FALSE, isspare));
279 * Check that a file is valid. All we can do in this case is check that it's
280 * not in use by another pool, and not in use by swap.
283 check_file(const char *file, boolean_t force, boolean_t isspare)
293 if (dm_inuse_swap(file, &err)) {
295 libdiskmgt_error(err);
297 vdev_error(gettext("%s is currently used by swap. "
298 "Please see swap(1M).\n"), file);
303 if ((fd = open(file, O_RDONLY)) < 0)
306 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
310 case POOL_STATE_ACTIVE:
311 desc = gettext("active");
314 case POOL_STATE_EXPORTED:
315 desc = gettext("exported");
318 case POOL_STATE_POTENTIALLY_ACTIVE:
319 desc = gettext("potentially active");
323 desc = gettext("unknown");
328 * Allow hot spares to be shared between pools.
330 if (state == POOL_STATE_SPARE && isspare)
333 if (state == POOL_STATE_ACTIVE ||
334 state == POOL_STATE_SPARE || !force) {
336 case POOL_STATE_SPARE:
337 vdev_error(gettext("%s is reserved as a hot "
338 "spare for pool %s\n"), file, name);
341 vdev_error(gettext("%s is part of %s pool "
342 "'%s'\n"), file, desc, name);
356 check_device(const char *name, boolean_t force, boolean_t isspare)
358 char path[MAXPATHLEN];
360 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) != 0)
361 snprintf(path, sizeof(path), "%s%s", _PATH_DEV, name);
363 strlcpy(path, name, sizeof(path));
365 return (check_file(path, force, isspare));
369 * By "whole disk" we mean an entire physical disk (something we can
370 * label, toggle the write cache on, etc.) as opposed to the full
371 * capacity of a pseudo-device such as lofi or did. We act as if we
372 * are labeling the disk, which should be a pretty good test of whether
373 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
377 is_whole_disk(const char *arg)
380 struct dk_gpt *label;
382 char path[MAXPATHLEN];
384 (void) snprintf(path, sizeof (path), "%s%s%s",
385 RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE);
386 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0)
388 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
408 * Create a leaf vdev. Determine if this is a file or a device. If it's a
409 * device, fill in the device id to make a complete nvlist. Valid forms for a
412 * /dev/dsk/xxx Complete disk path
413 * /xxx Full path to file
414 * xxx Shorthand for /dev/dsk/xxx
417 make_leaf_vdev(const char *arg, uint64_t is_log)
419 char path[MAXPATHLEN];
420 struct stat64 statbuf;
421 nvlist_t *vdev = NULL;
423 boolean_t wholedisk = B_FALSE;
426 * Determine what type of vdev this is, and put the full path into
427 * 'path'. We detect whether this is a device of file afterwards by
428 * checking the st_mode of the file.
432 * Complete device or file path. Exact type is determined by
433 * examining the file descriptor afterwards.
435 wholedisk = is_whole_disk(arg);
436 if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
437 (void) fprintf(stderr,
438 gettext("cannot open '%s': %s\n"),
439 arg, strerror(errno));
443 (void) strlcpy(path, arg, sizeof (path));
446 * This may be a short path for a device, or it could be total
447 * gibberish. Check to see if it's a known device in
448 * /dev/dsk/. As part of this check, see if we've been given a
449 * an entire disk (minus the slice number).
451 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
452 strlcpy(path, arg, sizeof (path));
454 snprintf(path, sizeof (path), "%s%s", _PATH_DEV, arg);
455 wholedisk = is_whole_disk(path);
456 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
458 * If we got ENOENT, then the user gave us
459 * gibberish, so try to direct them with a
460 * reasonable error message. Otherwise,
461 * regurgitate strerror() since it's the best we
464 if (errno == ENOENT) {
465 (void) fprintf(stderr,
466 gettext("cannot open '%s': no such "
467 "GEOM provider\n"), arg);
468 (void) fprintf(stderr,
469 gettext("must be a full path or "
470 "shorthand device name\n"));
473 (void) fprintf(stderr,
474 gettext("cannot open '%s': %s\n"),
475 path, strerror(errno));
482 if (S_ISCHR(statbuf.st_mode)) {
483 statbuf.st_mode &= ~S_IFCHR;
484 statbuf.st_mode |= S_IFBLK;
490 * Determine whether this is a device or a file.
492 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
493 type = VDEV_TYPE_DISK;
494 } else if (S_ISREG(statbuf.st_mode)) {
495 type = VDEV_TYPE_FILE;
497 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
498 "GEOM provider or regular file\n"), path);
503 * Finally, we have the complete device or file, and we know that it is
504 * acceptable to use. Construct the nvlist to describe this vdev. All
505 * vdevs have a 'path' element, and devices also have a 'devid' element.
507 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
508 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
509 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
510 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
511 if (strcmp(type, VDEV_TYPE_DISK) == 0)
512 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
513 (uint64_t)wholedisk) == 0);
517 * For a whole disk, defer getting its devid until after labeling it.
519 if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
521 * Get the devid for the device.
525 char *minor = NULL, *devid_str = NULL;
527 if ((fd = open(path, O_RDONLY)) < 0) {
528 (void) fprintf(stderr, gettext("cannot open '%s': "
529 "%s\n"), path, strerror(errno));
534 if (devid_get(fd, &devid) == 0) {
535 if (devid_get_minor_name(fd, &minor) == 0 &&
536 (devid_str = devid_str_encode(devid, minor)) !=
538 verify(nvlist_add_string(vdev,
539 ZPOOL_CONFIG_DEVID, devid_str) == 0);
541 if (devid_str != NULL)
542 devid_str_free(devid_str);
544 devid_str_free(minor);
556 * Go through and verify the replication level of the pool is consistent.
557 * Performs the following checks:
559 * For the new spec, verifies that devices in mirrors and raidz are the
562 * If the current configuration already has inconsistent replication
563 * levels, ignore any other potential problems in the new spec.
565 * Otherwise, make sure that the current spec (if there is one) and the new
566 * spec have consistent replication levels.
568 typedef struct replication_level {
570 uint64_t zprl_children;
571 uint64_t zprl_parity;
572 } replication_level_t;
574 #define ZPOOL_FUZZ (16 * 1024 * 1024)
577 * Given a list of toplevel vdevs, return the current replication level. If
578 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
579 * an error message will be displayed for each self-inconsistent vdev.
581 static replication_level_t *
582 get_replication(nvlist_t *nvroot, boolean_t fatal)
590 replication_level_t lastrep, rep, *ret;
591 boolean_t dontreport;
593 ret = safe_malloc(sizeof (replication_level_t));
595 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
596 &top, &toplevels) == 0);
598 lastrep.zprl_type = NULL;
599 for (t = 0; t < toplevels; t++) {
600 uint64_t is_log = B_FALSE;
605 * For separate logs we ignore the top level vdev replication
608 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
612 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
614 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
615 &child, &children) != 0) {
617 * This is a 'file' or 'disk' vdev.
619 rep.zprl_type = type;
620 rep.zprl_children = 1;
626 * This is a mirror or RAID-Z vdev. Go through and make
627 * sure the contents are all the same (files vs. disks),
628 * keeping track of the number of elements in the
631 * We also check that the size of each vdev (if it can
632 * be determined) is the same.
634 rep.zprl_type = type;
635 rep.zprl_children = 0;
637 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
638 verify(nvlist_lookup_uint64(nv,
639 ZPOOL_CONFIG_NPARITY,
640 &rep.zprl_parity) == 0);
641 assert(rep.zprl_parity != 0);
647 * The 'dontreport' variable indicates that we've
648 * already reported an error for this spec, so don't
649 * bother doing it again.
654 for (c = 0; c < children; c++) {
655 nvlist_t *cnv = child[c];
657 struct stat64 statbuf;
658 uint64_t size = -1ULL;
664 verify(nvlist_lookup_string(cnv,
665 ZPOOL_CONFIG_TYPE, &childtype) == 0);
668 * If this is a replacing or spare vdev, then
669 * get the real first child of the vdev.
671 if (strcmp(childtype,
672 VDEV_TYPE_REPLACING) == 0 ||
673 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
677 verify(nvlist_lookup_nvlist_array(cnv,
678 ZPOOL_CONFIG_CHILDREN, &rchild,
680 assert(rchildren == 2);
683 verify(nvlist_lookup_string(cnv,
688 verify(nvlist_lookup_string(cnv,
689 ZPOOL_CONFIG_PATH, &path) == 0);
692 * If we have a raidz/mirror that combines disks
693 * with files, report it as an error.
695 if (!dontreport && type != NULL &&
696 strcmp(type, childtype) != 0) {
702 "mismatched replication "
703 "level: %s contains both "
704 "files and devices\n"),
712 * According to stat(2), the value of 'st_size'
713 * is undefined for block devices and character
714 * devices. But there is no effective way to
715 * determine the real size in userland.
717 * Instead, we'll take advantage of an
718 * implementation detail of spec_size(). If the
719 * device is currently open, then we (should)
720 * return a valid size.
722 * If we still don't get a valid size (indicated
723 * by a size of 0 or MAXOFFSET_T), then ignore
724 * this device altogether.
726 if ((fd = open(path, O_RDONLY)) >= 0) {
727 err = fstat64(fd, &statbuf);
730 err = stat64(path, &statbuf);
734 statbuf.st_size == 0 ||
735 statbuf.st_size == MAXOFFSET_T)
738 size = statbuf.st_size;
741 * Also make sure that devices and
742 * slices have a consistent size. If
743 * they differ by a significant amount
744 * (~16MB) then report an error.
747 (vdev_size != -1ULL &&
748 (labs(size - vdev_size) >
755 "%s contains devices of "
756 "different sizes\n"),
769 * At this point, we have the replication of the last toplevel
770 * vdev in 'rep'. Compare it to 'lastrep' to see if its
773 if (lastrep.zprl_type != NULL) {
774 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
780 "mismatched replication level: "
781 "both %s and %s vdevs are "
783 lastrep.zprl_type, rep.zprl_type);
786 } else if (lastrep.zprl_parity != rep.zprl_parity) {
792 "mismatched replication level: "
793 "both %llu and %llu device parity "
794 "%s vdevs are present\n"),
800 } else if (lastrep.zprl_children != rep.zprl_children) {
806 "mismatched replication level: "
807 "both %llu-way and %llu-way %s "
808 "vdevs are present\n"),
809 lastrep.zprl_children,
826 * Check the replication level of the vdev spec against the current pool. Calls
827 * get_replication() to make sure the new spec is self-consistent. If the pool
828 * has a consistent replication level, then we ignore any errors. Otherwise,
829 * report any difference between the two.
832 check_replication(nvlist_t *config, nvlist_t *newroot)
836 replication_level_t *current = NULL, *new;
840 * If we have a current pool configuration, check to see if it's
841 * self-consistent. If not, simply return success.
843 if (config != NULL) {
846 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
848 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
852 * for spares there may be no children, and therefore no
853 * replication level to check
855 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
856 &child, &children) != 0) || (children == 0)) {
862 * If all we have is logs then there's no replication level to check.
864 if (num_logs(newroot) == children) {
870 * Get the replication level of the new vdev spec, reporting any
871 * inconsistencies found.
873 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
879 * Check to see if the new vdev spec matches the replication level of
883 if (current != NULL) {
884 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
886 "mismatched replication level: pool uses %s "
887 "and new vdev is %s\n"),
888 current->zprl_type, new->zprl_type);
890 } else if (current->zprl_parity != new->zprl_parity) {
892 "mismatched replication level: pool uses %llu "
893 "device parity and new vdev uses %llu\n"),
894 current->zprl_parity, new->zprl_parity);
896 } else if (current->zprl_children != new->zprl_children) {
898 "mismatched replication level: pool uses %llu-way "
899 "%s and new vdev uses %llu-way %s\n"),
900 current->zprl_children, current->zprl_type,
901 new->zprl_children, new->zprl_type);
915 * Go through and find any whole disks in the vdev specification, labelling them
916 * as appropriate. When constructing the vdev spec, we were unable to open this
917 * device in order to provide a devid. Now that we have labelled the disk and
918 * know that slice 0 is valid, we can construct the devid now.
920 * If the disk was already labeled with an EFI label, we will have gotten the
921 * devid already (because we were able to open the whole disk). Otherwise, we
922 * need to get the devid after we label the disk.
925 make_disks(zpool_handle_t *zhp, nvlist_t *nv)
929 char *type, *path, *diskname;
930 char buf[MAXPATHLEN];
935 char *minor = NULL, *devid_str = NULL;
937 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
939 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
940 &child, &children) != 0) {
942 if (strcmp(type, VDEV_TYPE_DISK) != 0)
946 * We have a disk device. Get the path to the device
947 * and see if it's a whole disk by appending the backup
948 * slice and stat()ing the device.
950 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
951 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
952 &wholedisk) != 0 || !wholedisk)
955 diskname = strrchr(path, '/');
956 assert(diskname != NULL);
958 if (zpool_label_disk(g_zfs, zhp, diskname) == -1)
962 * Fill in the devid, now that we've labeled the disk.
964 (void) snprintf(buf, sizeof (buf), "%ss0", path);
965 if ((fd = open(buf, O_RDONLY)) < 0) {
966 (void) fprintf(stderr,
967 gettext("cannot open '%s': %s\n"),
968 buf, strerror(errno));
972 if (devid_get(fd, &devid) == 0) {
973 if (devid_get_minor_name(fd, &minor) == 0 &&
974 (devid_str = devid_str_encode(devid, minor)) !=
976 verify(nvlist_add_string(nv,
977 ZPOOL_CONFIG_DEVID, devid_str) == 0);
979 if (devid_str != NULL)
980 devid_str_free(devid_str);
982 devid_str_free(minor);
987 * Update the path to refer to the 's0' slice. The presence of
988 * the 'whole_disk' field indicates to the CLI that we should
989 * chop off the slice number when displaying the device in
992 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0);
999 for (c = 0; c < children; c++)
1000 if ((ret = make_disks(zhp, child[c])) != 0)
1003 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1004 &child, &children) == 0)
1005 for (c = 0; c < children; c++)
1006 if ((ret = make_disks(zhp, child[c])) != 0)
1009 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1010 &child, &children) == 0)
1011 for (c = 0; c < children; c++)
1012 if ((ret = make_disks(zhp, child[c])) != 0)
1020 * Determine if the given path is a hot spare within the given configuration.
1023 is_spare(nvlist_t *config, const char *path)
1029 uint64_t guid, spareguid;
1035 if ((fd = open(path, O_RDONLY)) < 0)
1038 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
1040 state != POOL_STATE_SPARE ||
1041 zpool_read_label(fd, &label) != 0) {
1049 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
1052 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1054 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1055 &spares, &nspares) == 0) {
1056 for (i = 0; i < nspares; i++) {
1057 verify(nvlist_lookup_uint64(spares[i],
1058 ZPOOL_CONFIG_GUID, &spareguid) == 0);
1059 if (spareguid == guid)
1068 * Go through and find any devices that are in use. We rely on libdiskmgt for
1069 * the majority of this task.
1072 check_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1073 boolean_t replacing, boolean_t isspare)
1079 char buf[MAXPATHLEN];
1082 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1084 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1085 &child, &children) != 0) {
1087 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1090 * As a generic check, we look to see if this is a replace of a
1091 * hot spare within the same pool. If so, we allow it
1092 * regardless of what libdiskmgt or zpool_in_use() says.
1096 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1097 &wholedisk) == 0 && wholedisk)
1098 (void) snprintf(buf, sizeof (buf), "%ss0",
1102 (void) strlcpy(buf, path, sizeof (buf));
1104 if (is_spare(config, buf))
1108 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1109 ret = check_device(path, force, isspare);
1111 if (strcmp(type, VDEV_TYPE_FILE) == 0)
1112 ret = check_file(path, force, isspare);
1117 for (c = 0; c < children; c++)
1118 if ((ret = check_in_use(config, child[c], force,
1119 replacing, B_FALSE)) != 0)
1122 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1123 &child, &children) == 0)
1124 for (c = 0; c < children; c++)
1125 if ((ret = check_in_use(config, child[c], force,
1126 replacing, B_TRUE)) != 0)
1129 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1130 &child, &children) == 0)
1131 for (c = 0; c < children; c++)
1132 if ((ret = check_in_use(config, child[c], force,
1133 replacing, B_FALSE)) != 0)
1140 is_grouping(const char *type, int *mindev, int *maxdev)
1142 if (strncmp(type, "raidz", 5) == 0) {
1143 const char *p = type + 5;
1149 } else if (*p == '0') {
1150 return (NULL); /* no zero prefixes allowed */
1153 nparity = strtol(p, &end, 10);
1154 if (errno != 0 || nparity < 1 || nparity >= 255 ||
1160 *mindev = nparity + 1;
1163 return (VDEV_TYPE_RAIDZ);
1169 if (strcmp(type, "mirror") == 0) {
1172 return (VDEV_TYPE_MIRROR);
1175 if (strcmp(type, "spare") == 0) {
1178 return (VDEV_TYPE_SPARE);
1181 if (strcmp(type, "log") == 0) {
1184 return (VDEV_TYPE_LOG);
1187 if (strcmp(type, "cache") == 0) {
1190 return (VDEV_TYPE_L2CACHE);
1197 * Construct a syntactically valid vdev specification,
1198 * and ensure that all devices and files exist and can be opened.
1199 * Note: we don't bother freeing anything in the error paths
1200 * because the program is just going to exit anyway.
1203 construct_spec(int argc, char **argv)
1205 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1206 int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
1209 boolean_t seen_logs;
1219 seen_logs = B_FALSE;
1225 * If it's a mirror or raidz, the subsequent arguments are
1226 * its leaves -- until we encounter the next mirror or raidz.
1228 if ((type = is_grouping(argv[0], &mindev, &maxdev)) != NULL) {
1229 nvlist_t **child = NULL;
1230 int c, children = 0;
1232 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1233 if (spares != NULL) {
1234 (void) fprintf(stderr,
1235 gettext("invalid vdev "
1236 "specification: 'spare' can be "
1237 "specified only once\n"));
1243 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1245 (void) fprintf(stderr,
1246 gettext("invalid vdev "
1247 "specification: 'log' can be "
1248 "specified only once\n"));
1256 * A log is not a real grouping device.
1257 * We just set is_log and continue.
1262 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1263 if (l2cache != NULL) {
1264 (void) fprintf(stderr,
1265 gettext("invalid vdev "
1266 "specification: 'cache' can be "
1267 "specified only once\n"));
1274 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1275 (void) fprintf(stderr,
1276 gettext("invalid vdev "
1277 "specification: unsupported 'log' "
1278 "device: %s\n"), type);
1284 for (c = 1; c < argc; c++) {
1285 if (is_grouping(argv[c], NULL, NULL) != NULL)
1288 child = realloc(child,
1289 children * sizeof (nvlist_t *));
1292 if ((nv = make_leaf_vdev(argv[c], B_FALSE))
1295 child[children - 1] = nv;
1298 if (children < mindev) {
1299 (void) fprintf(stderr, gettext("invalid vdev "
1300 "specification: %s requires at least %d "
1301 "devices\n"), argv[0], mindev);
1305 if (children > maxdev) {
1306 (void) fprintf(stderr, gettext("invalid vdev "
1307 "specification: %s supports no more than "
1308 "%d devices\n"), argv[0], maxdev);
1315 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1319 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1321 nl2cache = children;
1324 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1326 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1328 verify(nvlist_add_uint64(nv,
1329 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1330 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1331 verify(nvlist_add_uint64(nv,
1332 ZPOOL_CONFIG_NPARITY,
1335 verify(nvlist_add_nvlist_array(nv,
1336 ZPOOL_CONFIG_CHILDREN, child,
1339 for (c = 0; c < children; c++)
1340 nvlist_free(child[c]);
1345 * We have a device. Pass off to make_leaf_vdev() to
1346 * construct the appropriate nvlist describing the vdev.
1348 if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
1357 top = realloc(top, toplevels * sizeof (nvlist_t *));
1360 top[toplevels - 1] = nv;
1363 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1364 (void) fprintf(stderr, gettext("invalid vdev "
1365 "specification: at least one toplevel vdev must be "
1370 if (seen_logs && nlogs == 0) {
1371 (void) fprintf(stderr, gettext("invalid vdev specification: "
1372 "log requires at least 1 device\n"));
1377 * Finally, create nvroot and add all top-level vdevs to it.
1379 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1380 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1381 VDEV_TYPE_ROOT) == 0);
1382 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1383 top, toplevels) == 0);
1385 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1386 spares, nspares) == 0);
1388 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1389 l2cache, nl2cache) == 0);
1391 for (t = 0; t < toplevels; t++)
1392 nvlist_free(top[t]);
1393 for (t = 0; t < nspares; t++)
1394 nvlist_free(spares[t]);
1395 for (t = 0; t < nl2cache; t++)
1396 nvlist_free(l2cache[t]);
1407 split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1408 splitflags_t flags, int argc, char **argv)
1410 nvlist_t *newroot = NULL, **child;
1414 if ((newroot = construct_spec(argc, argv)) == NULL) {
1415 (void) fprintf(stderr, gettext("Unable to build a "
1416 "pool from the specified devices\n"));
1421 if (!flags.dryrun && make_disks(zhp, newroot) != 0) {
1422 nvlist_free(newroot);
1427 /* avoid any tricks in the spec */
1428 verify(nvlist_lookup_nvlist_array(newroot,
1429 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1430 for (c = 0; c < children; c++) {
1435 verify(nvlist_lookup_string(child[c],
1436 ZPOOL_CONFIG_PATH, &path) == 0);
1437 if ((type = is_grouping(path, &min, &max)) != NULL) {
1438 (void) fprintf(stderr, gettext("Cannot use "
1439 "'%s' as a device for splitting\n"), type);
1440 nvlist_free(newroot);
1446 if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1447 if (newroot != NULL)
1448 nvlist_free(newroot);
1456 * Get and validate the contents of the given vdev specification. This ensures
1457 * that the nvlist returned is well-formed, that all the devices exist, and that
1458 * they are not currently in use by any other known consumer. The 'poolconfig'
1459 * parameter is the current configuration of the pool when adding devices
1460 * existing pool, and is used to perform additional checks, such as changing the
1461 * replication level of the pool. It can be 'NULL' to indicate that this is a
1462 * new pool. The 'force' flag controls whether devices should be forcefully
1463 * added, even if they appear in use.
1466 make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
1467 boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1470 nvlist_t *poolconfig = NULL;
1474 * Construct the vdev specification. If this is successful, we know
1475 * that we have a valid specification, and that all devices can be
1478 if ((newroot = construct_spec(argc, argv)) == NULL)
1481 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1485 * Validate each device to make sure that its not shared with another
1486 * subsystem. We do this even if 'force' is set, because there are some
1487 * uses (such as a dedicated dump device) that even '-f' cannot
1490 if (check_in_use(poolconfig, newroot, force, replacing, B_FALSE) != 0) {
1491 nvlist_free(newroot);
1496 * Check the replication level of the given vdevs and report any errors
1497 * found. We include the existing pool spec, if any, as we need to
1498 * catch changes against the existing replication level.
1500 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1501 nvlist_free(newroot);
1507 * Run through the vdev specification and label any whole disks found.
1509 if (!dryrun && make_disks(zhp, newroot) != 0) {
1510 nvlist_free(newroot);