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 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "@(#)zpool_vdev.c 1.15 07/11/09 SMI"
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 libdiskmgt, makes 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.
69 #include <libdiskmgt.h>
71 #include <libnvpair.h>
75 #include <sys/efi_partition.h>
78 #include <sys/mntent.h>
80 #include "zpool_util.h"
82 #define DISK_ROOT "/dev/dsk"
83 #define RDISK_ROOT "/dev/rdsk"
84 #define BACKUP_SLICE "s2"
87 * For any given vdev specification, we can have multiple errors. The
88 * vdev_error() function keeps track of whether we have seen an error yet, and
89 * prints out a header if its the first error we've seen.
96 vdev_error(const char *fmt, ...)
101 (void) fprintf(stderr, gettext("invalid vdev specification\n"));
103 (void) fprintf(stderr, gettext("use '-f' to override "
104 "the following errors:\n"));
106 (void) fprintf(stderr, gettext("the following errors "
107 "must be manually repaired:\n"));
112 (void) vfprintf(stderr, fmt, ap);
117 libdiskmgt_error(int error)
120 * ENXIO/ENODEV is a valid error message if the device doesn't live in
121 * /dev/dsk. Don't bother printing an error message in this case.
123 if (error == ENXIO || error == ENODEV)
126 (void) fprintf(stderr, gettext("warning: device in use checking "
127 "failed: %s\n"), strerror(error));
131 * Validate a device, passing the bulk of the work off to libdiskmgt.
134 check_slice(const char *path, int force, boolean_t wholedisk, boolean_t isspare)
141 who = DM_WHO_ZPOOL_FORCE;
143 who = DM_WHO_ZPOOL_SPARE;
147 if (dm_inuse((char *)path, &msg, who, &error) || error) {
149 libdiskmgt_error(error);
152 vdev_error("%s", msg);
159 * If we're given a whole disk, ignore overlapping slices since we're
160 * about to label it anyway.
163 if (!wholedisk && !force &&
164 (dm_isoverlapping((char *)path, &msg, &error) || error)) {
166 /* dm_isoverlapping returned -1 */
167 vdev_error(gettext("%s overlaps with %s\n"), path, msg);
170 } else if (error != ENODEV) {
171 /* libdiskmgt's devcache only handles physical drives */
172 libdiskmgt_error(error);
182 * Validate a whole disk. Iterate over all slices on the disk and make sure
183 * that none is in use by calling check_slice().
186 check_disk(const char *name, dm_descriptor_t disk, int force, int isspare)
188 dm_descriptor_t *drive, *media, *slice;
194 * Get the drive associated with this disk. This should never fail,
195 * because we already have an alias handle open for the device.
197 if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE,
198 &err)) == NULL || *drive == NULL) {
200 libdiskmgt_error(err);
204 if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA,
206 dm_free_descriptors(drive);
208 libdiskmgt_error(err);
212 dm_free_descriptors(drive);
215 * It is possible that the user has specified a removable media drive,
216 * and the media is not present.
218 if (*media == NULL) {
219 dm_free_descriptors(media);
220 vdev_error(gettext("'%s' has no media in drive\n"), name);
224 if ((slice = dm_get_associated_descriptors(*media, DM_SLICE,
226 dm_free_descriptors(media);
228 libdiskmgt_error(err);
232 dm_free_descriptors(media);
237 * Iterate over all slices and report any errors. We don't care about
238 * overlapping slices because we are using the whole disk.
240 for (i = 0; slice[i] != NULL; i++) {
241 char *name = dm_get_name(slice[i], &err);
243 if (check_slice(name, force, B_TRUE, isspare) != 0)
249 dm_free_descriptors(slice);
257 check_device(const char *path, boolean_t force, boolean_t isspare)
259 dm_descriptor_t desc;
264 * For whole disks, libdiskmgt does not include the leading dev path.
266 dev = strrchr(path, '/');
269 if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err)) != NULL) {
270 err = check_disk(path, desc, force, isspare);
271 dm_free_descriptor(desc);
275 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)
292 if (dm_inuse_swap(file, &err)) {
294 libdiskmgt_error(err);
296 vdev_error(gettext("%s is currently used by swap. "
297 "Please see swap(1M).\n"), file);
301 if ((fd = open(file, O_RDONLY)) < 0)
304 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
308 case POOL_STATE_ACTIVE:
309 desc = gettext("active");
312 case POOL_STATE_EXPORTED:
313 desc = gettext("exported");
316 case POOL_STATE_POTENTIALLY_ACTIVE:
317 desc = gettext("potentially active");
321 desc = gettext("unknown");
326 * Allow hot spares to be shared between pools.
328 if (state == POOL_STATE_SPARE && isspare)
331 if (state == POOL_STATE_ACTIVE ||
332 state == POOL_STATE_SPARE || !force) {
334 case POOL_STATE_SPARE:
335 vdev_error(gettext("%s is reserved as a hot "
336 "spare for pool %s\n"), file, name);
339 vdev_error(gettext("%s is part of %s pool "
340 "'%s'\n"), file, desc, name);
355 * By "whole disk" we mean an entire physical disk (something we can
356 * label, toggle the write cache on, etc.) as opposed to the full
357 * capacity of a pseudo-device such as lofi or did. We act as if we
358 * are labeling the disk, which should be a pretty good test of whether
359 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
363 is_whole_disk(const char *arg)
365 struct dk_gpt *label;
367 char path[MAXPATHLEN];
369 (void) snprintf(path, sizeof (path), "%s%s%s",
370 RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE);
371 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0)
373 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
383 * Create a leaf vdev. Determine if this is a file or a device. If it's a
384 * device, fill in the device id to make a complete nvlist. Valid forms for a
387 * /dev/dsk/xxx Complete disk path
388 * /xxx Full path to file
389 * xxx Shorthand for /dev/dsk/xxx
392 make_leaf_vdev(const char *arg, uint64_t is_log)
394 char path[MAXPATHLEN];
395 struct stat64 statbuf;
396 nvlist_t *vdev = NULL;
398 boolean_t wholedisk = B_FALSE;
401 * Determine what type of vdev this is, and put the full path into
402 * 'path'. We detect whether this is a device of file afterwards by
403 * checking the st_mode of the file.
407 * Complete device or file path. Exact type is determined by
408 * examining the file descriptor afterwards.
410 wholedisk = is_whole_disk(arg);
411 if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
412 (void) fprintf(stderr,
413 gettext("cannot open '%s': %s\n"),
414 arg, strerror(errno));
418 (void) strlcpy(path, arg, sizeof (path));
421 * This may be a short path for a device, or it could be total
422 * gibberish. Check to see if it's a known device in
423 * /dev/dsk/. As part of this check, see if we've been given a
424 * an entire disk (minus the slice number).
426 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT,
428 wholedisk = is_whole_disk(path);
429 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
431 * If we got ENOENT, then the user gave us
432 * gibberish, so try to direct them with a
433 * reasonable error message. Otherwise,
434 * regurgitate strerror() since it's the best we
437 if (errno == ENOENT) {
438 (void) fprintf(stderr,
439 gettext("cannot open '%s': no such "
440 "device in %s\n"), arg, DISK_ROOT);
441 (void) fprintf(stderr,
442 gettext("must be a full path or "
443 "shorthand device name\n"));
446 (void) fprintf(stderr,
447 gettext("cannot open '%s': %s\n"),
448 path, strerror(errno));
455 * Determine whether this is a device or a file.
457 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
458 type = VDEV_TYPE_DISK;
459 } else if (S_ISREG(statbuf.st_mode)) {
460 type = VDEV_TYPE_FILE;
462 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
463 "block device or regular file\n"), path);
468 * Finally, we have the complete device or file, and we know that it is
469 * acceptable to use. Construct the nvlist to describe this vdev. All
470 * vdevs have a 'path' element, and devices also have a 'devid' element.
472 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
473 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
474 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
475 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
476 if (strcmp(type, VDEV_TYPE_DISK) == 0)
477 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
478 (uint64_t)wholedisk) == 0);
481 * For a whole disk, defer getting its devid until after labeling it.
483 if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
485 * Get the devid for the device.
489 char *minor = NULL, *devid_str = NULL;
491 if ((fd = open(path, O_RDONLY)) < 0) {
492 (void) fprintf(stderr, gettext("cannot open '%s': "
493 "%s\n"), path, strerror(errno));
498 if (devid_get(fd, &devid) == 0) {
499 if (devid_get_minor_name(fd, &minor) == 0 &&
500 (devid_str = devid_str_encode(devid, minor)) !=
502 verify(nvlist_add_string(vdev,
503 ZPOOL_CONFIG_DEVID, devid_str) == 0);
505 if (devid_str != NULL)
506 devid_str_free(devid_str);
508 devid_str_free(minor);
519 * Go through and verify the replication level of the pool is consistent.
520 * Performs the following checks:
522 * For the new spec, verifies that devices in mirrors and raidz are the
525 * If the current configuration already has inconsistent replication
526 * levels, ignore any other potential problems in the new spec.
528 * Otherwise, make sure that the current spec (if there is one) and the new
529 * spec have consistent replication levels.
531 typedef struct replication_level {
533 uint64_t zprl_children;
534 uint64_t zprl_parity;
535 } replication_level_t;
537 #define ZPOOL_FUZZ (16 * 1024 * 1024)
540 * Given a list of toplevel vdevs, return the current replication level. If
541 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
542 * an error message will be displayed for each self-inconsistent vdev.
544 static replication_level_t *
545 get_replication(nvlist_t *nvroot, boolean_t fatal)
553 replication_level_t lastrep, rep, *ret;
554 boolean_t dontreport;
556 ret = safe_malloc(sizeof (replication_level_t));
558 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
559 &top, &toplevels) == 0);
561 lastrep.zprl_type = NULL;
562 for (t = 0; t < toplevels; t++) {
563 uint64_t is_log = B_FALSE;
568 * For separate logs we ignore the top level vdev replication
571 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
575 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
577 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
578 &child, &children) != 0) {
580 * This is a 'file' or 'disk' vdev.
582 rep.zprl_type = type;
583 rep.zprl_children = 1;
589 * This is a mirror or RAID-Z vdev. Go through and make
590 * sure the contents are all the same (files vs. disks),
591 * keeping track of the number of elements in the
594 * We also check that the size of each vdev (if it can
595 * be determined) is the same.
597 rep.zprl_type = type;
598 rep.zprl_children = 0;
600 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
601 verify(nvlist_lookup_uint64(nv,
602 ZPOOL_CONFIG_NPARITY,
603 &rep.zprl_parity) == 0);
604 assert(rep.zprl_parity != 0);
610 * The 'dontreport' variable indicates that we've
611 * already reported an error for this spec, so don't
612 * bother doing it again.
617 for (c = 0; c < children; c++) {
618 nvlist_t *cnv = child[c];
620 struct stat64 statbuf;
621 uint64_t size = -1ULL;
627 verify(nvlist_lookup_string(cnv,
628 ZPOOL_CONFIG_TYPE, &childtype) == 0);
631 * If this is a replacing or spare vdev, then
632 * get the real first child of the vdev.
634 if (strcmp(childtype,
635 VDEV_TYPE_REPLACING) == 0 ||
636 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
640 verify(nvlist_lookup_nvlist_array(cnv,
641 ZPOOL_CONFIG_CHILDREN, &rchild,
643 assert(rchildren == 2);
646 verify(nvlist_lookup_string(cnv,
651 verify(nvlist_lookup_string(cnv,
652 ZPOOL_CONFIG_PATH, &path) == 0);
655 * If we have a raidz/mirror that combines disks
656 * with files, report it as an error.
658 if (!dontreport && type != NULL &&
659 strcmp(type, childtype) != 0) {
665 "mismatched replication "
666 "level: %s contains both "
667 "files and devices\n"),
675 * According to stat(2), the value of 'st_size'
676 * is undefined for block devices and character
677 * devices. But there is no effective way to
678 * determine the real size in userland.
680 * Instead, we'll take advantage of an
681 * implementation detail of spec_size(). If the
682 * device is currently open, then we (should)
683 * return a valid size.
685 * If we still don't get a valid size (indicated
686 * by a size of 0 or MAXOFFSET_T), then ignore
687 * this device altogether.
689 if ((fd = open(path, O_RDONLY)) >= 0) {
690 err = fstat64(fd, &statbuf);
693 err = stat64(path, &statbuf);
697 statbuf.st_size == 0 ||
698 statbuf.st_size == MAXOFFSET_T)
701 size = statbuf.st_size;
704 * Also make sure that devices and
705 * slices have a consistent size. If
706 * they differ by a significant amount
707 * (~16MB) then report an error.
710 (vdev_size != -1ULL &&
711 (labs(size - vdev_size) >
718 "%s contains devices of "
719 "different sizes\n"),
732 * At this point, we have the replication of the last toplevel
733 * vdev in 'rep'. Compare it to 'lastrep' to see if its
736 if (lastrep.zprl_type != NULL) {
737 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
743 "mismatched replication level: "
744 "both %s and %s vdevs are "
746 lastrep.zprl_type, rep.zprl_type);
749 } else if (lastrep.zprl_parity != rep.zprl_parity) {
755 "mismatched replication level: "
756 "both %llu and %llu device parity "
757 "%s vdevs are present\n"),
763 } else if (lastrep.zprl_children != rep.zprl_children) {
769 "mismatched replication level: "
770 "both %llu-way and %llu-way %s "
771 "vdevs are present\n"),
772 lastrep.zprl_children,
789 * Check the replication level of the vdev spec against the current pool. Calls
790 * get_replication() to make sure the new spec is self-consistent. If the pool
791 * has a consistent replication level, then we ignore any errors. Otherwise,
792 * report any difference between the two.
795 check_replication(nvlist_t *config, nvlist_t *newroot)
799 replication_level_t *current = NULL, *new;
803 * If we have a current pool configuration, check to see if it's
804 * self-consistent. If not, simply return success.
806 if (config != NULL) {
809 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
811 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
815 * for spares there may be no children, and therefore no
816 * replication level to check
818 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
819 &child, &children) != 0) || (children == 0)) {
825 * If all we have is logs then there's no replication level to check.
827 if (num_logs(newroot) == children) {
833 * Get the replication level of the new vdev spec, reporting any
834 * inconsistencies found.
836 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
842 * Check to see if the new vdev spec matches the replication level of
846 if (current != NULL) {
847 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
849 "mismatched replication level: pool uses %s "
850 "and new vdev is %s\n"),
851 current->zprl_type, new->zprl_type);
853 } else if (current->zprl_parity != new->zprl_parity) {
855 "mismatched replication level: pool uses %llu "
856 "device parity and new vdev uses %llu\n"),
857 current->zprl_parity, new->zprl_parity);
859 } else if (current->zprl_children != new->zprl_children) {
861 "mismatched replication level: pool uses %llu-way "
862 "%s and new vdev uses %llu-way %s\n"),
863 current->zprl_children, current->zprl_type,
864 new->zprl_children, new->zprl_type);
877 * Go through and find any whole disks in the vdev specification, labelling them
878 * as appropriate. When constructing the vdev spec, we were unable to open this
879 * device in order to provide a devid. Now that we have labelled the disk and
880 * know that slice 0 is valid, we can construct the devid now.
882 * If the disk was already labeled with an EFI label, we will have gotten the
883 * devid already (because we were able to open the whole disk). Otherwise, we
884 * need to get the devid after we label the disk.
887 make_disks(zpool_handle_t *zhp, nvlist_t *nv)
891 char *type, *path, *diskname;
892 char buf[MAXPATHLEN];
897 char *minor = NULL, *devid_str = NULL;
899 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
901 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
902 &child, &children) != 0) {
904 if (strcmp(type, VDEV_TYPE_DISK) != 0)
908 * We have a disk device. Get the path to the device
909 * and see if it's a whole disk by appending the backup
910 * slice and stat()ing the device.
912 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
913 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
914 &wholedisk) != 0 || !wholedisk)
917 diskname = strrchr(path, '/');
918 assert(diskname != NULL);
920 if (zpool_label_disk(g_zfs, zhp, diskname) == -1)
924 * Fill in the devid, now that we've labeled the disk.
926 (void) snprintf(buf, sizeof (buf), "%ss0", path);
927 if ((fd = open(buf, O_RDONLY)) < 0) {
928 (void) fprintf(stderr,
929 gettext("cannot open '%s': %s\n"),
930 buf, strerror(errno));
934 if (devid_get(fd, &devid) == 0) {
935 if (devid_get_minor_name(fd, &minor) == 0 &&
936 (devid_str = devid_str_encode(devid, minor)) !=
938 verify(nvlist_add_string(nv,
939 ZPOOL_CONFIG_DEVID, devid_str) == 0);
941 if (devid_str != NULL)
942 devid_str_free(devid_str);
944 devid_str_free(minor);
949 * Update the path to refer to the 's0' slice. The presence of
950 * the 'whole_disk' field indicates to the CLI that we should
951 * chop off the slice number when displaying the device in
954 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0);
961 for (c = 0; c < children; c++)
962 if ((ret = make_disks(zhp, child[c])) != 0)
965 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
966 &child, &children) == 0)
967 for (c = 0; c < children; c++)
968 if ((ret = make_disks(zhp, child[c])) != 0)
971 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
972 &child, &children) == 0)
973 for (c = 0; c < children; c++)
974 if ((ret = make_disks(zhp, child[c])) != 0)
981 * Determine if the given path is a hot spare within the given configuration.
984 is_spare(nvlist_t *config, const char *path)
990 uint64_t guid, spareguid;
996 if ((fd = open(path, O_RDONLY)) < 0)
999 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
1001 state != POOL_STATE_SPARE ||
1002 zpool_read_label(fd, &label) != 0) {
1010 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
1013 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1015 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1016 &spares, &nspares) == 0) {
1017 for (i = 0; i < nspares; i++) {
1018 verify(nvlist_lookup_uint64(spares[i],
1019 ZPOOL_CONFIG_GUID, &spareguid) == 0);
1020 if (spareguid == guid)
1029 * Go through and find any devices that are in use. We rely on libdiskmgt for
1030 * the majority of this task.
1033 check_in_use(nvlist_t *config, nvlist_t *nv, int force, int isreplacing,
1040 char buf[MAXPATHLEN];
1043 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1045 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1046 &child, &children) != 0) {
1048 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1051 * As a generic check, we look to see if this is a replace of a
1052 * hot spare within the same pool. If so, we allow it
1053 * regardless of what libdiskmgt or zpool_in_use() says.
1056 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1057 &wholedisk) == 0 && wholedisk)
1058 (void) snprintf(buf, sizeof (buf), "%ss0",
1061 (void) strlcpy(buf, path, sizeof (buf));
1062 if (is_spare(config, buf))
1066 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1067 ret = check_device(path, force, isspare);
1069 if (strcmp(type, VDEV_TYPE_FILE) == 0)
1070 ret = check_file(path, force, isspare);
1075 for (c = 0; c < children; c++)
1076 if ((ret = check_in_use(config, child[c], force,
1077 isreplacing, B_FALSE)) != 0)
1080 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1081 &child, &children) == 0)
1082 for (c = 0; c < children; c++)
1083 if ((ret = check_in_use(config, child[c], force,
1084 isreplacing, B_TRUE)) != 0)
1087 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1088 &child, &children) == 0)
1089 for (c = 0; c < children; c++)
1090 if ((ret = check_in_use(config, child[c], force,
1091 isreplacing, B_FALSE)) != 0)
1098 is_grouping(const char *type, int *mindev)
1100 if (strcmp(type, "raidz") == 0 || strcmp(type, "raidz1") == 0) {
1103 return (VDEV_TYPE_RAIDZ);
1106 if (strcmp(type, "raidz2") == 0) {
1109 return (VDEV_TYPE_RAIDZ);
1112 if (strcmp(type, "mirror") == 0) {
1115 return (VDEV_TYPE_MIRROR);
1118 if (strcmp(type, "spare") == 0) {
1121 return (VDEV_TYPE_SPARE);
1124 if (strcmp(type, "log") == 0) {
1127 return (VDEV_TYPE_LOG);
1130 if (strcmp(type, "cache") == 0) {
1133 return (VDEV_TYPE_L2CACHE);
1140 * Construct a syntactically valid vdev specification,
1141 * and ensure that all devices and files exist and can be opened.
1142 * Note: we don't bother freeing anything in the error paths
1143 * because the program is just going to exit anyway.
1146 construct_spec(int argc, char **argv)
1148 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1149 int t, toplevels, mindev, nspares, nlogs, nl2cache;
1152 boolean_t seen_logs;
1162 seen_logs = B_FALSE;
1168 * If it's a mirror or raidz, the subsequent arguments are
1169 * its leaves -- until we encounter the next mirror or raidz.
1171 if ((type = is_grouping(argv[0], &mindev)) != NULL) {
1172 nvlist_t **child = NULL;
1173 int c, children = 0;
1175 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1176 if (spares != NULL) {
1177 (void) fprintf(stderr,
1178 gettext("invalid vdev "
1179 "specification: 'spare' can be "
1180 "specified only once\n"));
1186 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1188 (void) fprintf(stderr,
1189 gettext("invalid vdev "
1190 "specification: 'log' can be "
1191 "specified only once\n"));
1199 * A log is not a real grouping device.
1200 * We just set is_log and continue.
1205 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1206 if (l2cache != NULL) {
1207 (void) fprintf(stderr,
1208 gettext("invalid vdev "
1209 "specification: 'cache' can be "
1210 "specified only once\n"));
1217 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1218 (void) fprintf(stderr,
1219 gettext("invalid vdev "
1220 "specification: unsupported 'log' "
1221 "device: %s\n"), type);
1227 for (c = 1; c < argc; c++) {
1228 if (is_grouping(argv[c], NULL) != NULL)
1231 child = realloc(child,
1232 children * sizeof (nvlist_t *));
1235 if ((nv = make_leaf_vdev(argv[c], B_FALSE))
1238 child[children - 1] = nv;
1241 if (children < mindev) {
1242 (void) fprintf(stderr, gettext("invalid vdev "
1243 "specification: %s requires at least %d "
1244 "devices\n"), argv[0], mindev);
1251 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1255 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1257 nl2cache = children;
1260 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1262 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1264 verify(nvlist_add_uint64(nv,
1265 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1266 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1267 verify(nvlist_add_uint64(nv,
1268 ZPOOL_CONFIG_NPARITY,
1271 verify(nvlist_add_nvlist_array(nv,
1272 ZPOOL_CONFIG_CHILDREN, child,
1275 for (c = 0; c < children; c++)
1276 nvlist_free(child[c]);
1281 * We have a device. Pass off to make_leaf_vdev() to
1282 * construct the appropriate nvlist describing the vdev.
1284 if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
1293 top = realloc(top, toplevels * sizeof (nvlist_t *));
1296 top[toplevels - 1] = nv;
1299 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1300 (void) fprintf(stderr, gettext("invalid vdev "
1301 "specification: at least one toplevel vdev must be "
1306 if (seen_logs && nlogs == 0) {
1307 (void) fprintf(stderr, gettext("invalid vdev specification: "
1308 "log requires at least 1 device\n"));
1313 * Finally, create nvroot and add all top-level vdevs to it.
1315 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1316 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1317 VDEV_TYPE_ROOT) == 0);
1318 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1319 top, toplevels) == 0);
1321 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1322 spares, nspares) == 0);
1324 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1325 l2cache, nl2cache) == 0);
1327 for (t = 0; t < toplevels; t++)
1328 nvlist_free(top[t]);
1329 for (t = 0; t < nspares; t++)
1330 nvlist_free(spares[t]);
1331 for (t = 0; t < nl2cache; t++)
1332 nvlist_free(l2cache[t]);
1344 * Get and validate the contents of the given vdev specification. This ensures
1345 * that the nvlist returned is well-formed, that all the devices exist, and that
1346 * they are not currently in use by any other known consumer. The 'poolconfig'
1347 * parameter is the current configuration of the pool when adding devices
1348 * existing pool, and is used to perform additional checks, such as changing the
1349 * replication level of the pool. It can be 'NULL' to indicate that this is a
1350 * new pool. The 'force' flag controls whether devices should be forcefully
1351 * added, even if they appear in use.
1354 make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
1355 boolean_t isreplacing, int argc, char **argv)
1358 nvlist_t *poolconfig = NULL;
1362 * Construct the vdev specification. If this is successful, we know
1363 * that we have a valid specification, and that all devices can be
1366 if ((newroot = construct_spec(argc, argv)) == NULL)
1369 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1373 * Validate each device to make sure that its not shared with another
1374 * subsystem. We do this even if 'force' is set, because there are some
1375 * uses (such as a dedicated dump device) that even '-f' cannot
1378 if (check_in_use(poolconfig, newroot, force, isreplacing,
1380 nvlist_free(newroot);
1385 * Check the replication level of the given vdevs and report any errors
1386 * found. We include the existing pool spec, if any, as we need to
1387 * catch changes against the existing replication level.
1389 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1390 nvlist_free(newroot);
1395 * Run through the vdev specification and label any whole disks found.
1397 if (make_disks(zhp, newroot) != 0) {
1398 nvlist_free(newroot);