4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
28 * Functions to convert between a list of vdevs and an nvlist representing the
29 * configuration. Each entry in the list can be one of:
32 * disk=(path=..., devid=...)
41 * While the underlying implementation supports it, group vdevs cannot contain
42 * other group vdevs. All userland verification of devices is contained within
43 * this file. If successful, the nvlist returned can be passed directly to the
44 * kernel; we've done as much verification as possible in userland.
46 * Hot spares are a special case, and passed down as an array of disk vdevs, at
47 * the same level as the root of the vdev tree.
49 * The only function exported by this file is 'make_root_vdev'. The
50 * function performs several passes:
52 * 1. Construct the vdev specification. Performs syntax validation and
53 * makes sure each device is valid.
54 * 2. Check for devices in use. Using libdiskmgt, makes sure that no
55 * devices are also in use. Some can be overridden using the 'force'
56 * flag, others cannot.
57 * 3. Check for replication errors if the 'force' flag is not specified.
58 * validates that the replication level is consistent across the
60 * 4. Call libzfs to label any whole disks with an EFI label.
68 #include <libnvpair.h>
76 #include <sys/mntent.h>
79 #include "zpool_util.h"
81 #define DISK_ROOT "/dev/dsk"
82 #define RDISK_ROOT "/dev/rdsk"
83 #define BACKUP_SLICE "s2"
86 * For any given vdev specification, we can have multiple errors. The
87 * vdev_error() function keeps track of whether we have seen an error yet, and
88 * prints out a header if its the first error we've seen.
95 vdev_error(const char *fmt, ...)
100 (void) fprintf(stderr, gettext("invalid vdev specification\n"));
102 (void) fprintf(stderr, gettext("use '-f' to override "
103 "the following errors:\n"));
105 (void) fprintf(stderr, gettext("the following errors "
106 "must be manually repaired:\n"));
111 (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));
280 * Check that a file is valid. All we can do in this case is check that it's
281 * not in use by another pool, and not in use by swap.
284 check_file(const char *file, boolean_t force, boolean_t isspare)
294 if (dm_inuse_swap(file, &err)) {
296 libdiskmgt_error(err);
298 vdev_error(gettext("%s is currently used by swap. "
299 "Please see swap(1M).\n"), file);
304 if ((fd = open(file, O_RDONLY)) < 0)
307 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
311 case POOL_STATE_ACTIVE:
312 desc = gettext("active");
315 case POOL_STATE_EXPORTED:
316 desc = gettext("exported");
319 case POOL_STATE_POTENTIALLY_ACTIVE:
320 desc = gettext("potentially active");
324 desc = gettext("unknown");
329 * Allow hot spares to be shared between pools.
331 if (state == POOL_STATE_SPARE && isspare)
334 if (state == POOL_STATE_ACTIVE ||
335 state == POOL_STATE_SPARE || !force) {
337 case POOL_STATE_SPARE:
338 vdev_error(gettext("%s is reserved as a hot "
339 "spare for pool %s\n"), file, name);
342 vdev_error(gettext("%s is part of %s pool "
343 "'%s'\n"), file, desc, name);
357 check_device(const char *name, boolean_t force, boolean_t isspare)
359 char path[MAXPATHLEN];
361 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) != 0)
362 snprintf(path, sizeof(path), "%s%s", _PATH_DEV, name);
364 strlcpy(path, name, sizeof(path));
366 return (check_file(path, force, isspare));
370 * By "whole disk" we mean an entire physical disk (something we can
371 * label, toggle the write cache on, etc.) as opposed to the full
372 * capacity of a pseudo-device such as lofi or did. We act as if we
373 * are labeling the disk, which should be a pretty good test of whether
374 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
378 is_whole_disk(const char *arg)
381 struct dk_gpt *label;
383 char path[MAXPATHLEN];
385 (void) snprintf(path, sizeof (path), "%s%s%s",
386 RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE);
387 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0)
389 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
409 * Create a leaf vdev. Determine if this is a file or a device. If it's a
410 * device, fill in the device id to make a complete nvlist. Valid forms for a
413 * /dev/dsk/xxx Complete disk path
414 * /xxx Full path to file
415 * xxx Shorthand for /dev/dsk/xxx
418 make_leaf_vdev(const char *arg, uint64_t is_log)
420 char path[MAXPATHLEN];
421 struct stat64 statbuf;
422 nvlist_t *vdev = NULL;
424 boolean_t wholedisk = B_FALSE;
427 * Determine what type of vdev this is, and put the full path into
428 * 'path'. We detect whether this is a device of file afterwards by
429 * checking the st_mode of the file.
433 * Complete device or file path. Exact type is determined by
434 * examining the file descriptor afterwards.
436 wholedisk = is_whole_disk(arg);
437 if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
438 (void) fprintf(stderr,
439 gettext("cannot open '%s': %s\n"),
440 arg, strerror(errno));
444 (void) strlcpy(path, arg, sizeof (path));
447 * This may be a short path for a device, or it could be total
448 * gibberish. Check to see if it's a known device in
449 * /dev/dsk/. As part of this check, see if we've been given a
450 * an entire disk (minus the slice number).
452 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
453 strlcpy(path, arg, sizeof (path));
455 snprintf(path, sizeof (path), "%s%s", _PATH_DEV, arg);
456 wholedisk = is_whole_disk(path);
457 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
459 * If we got ENOENT, then the user gave us
460 * gibberish, so try to direct them with a
461 * reasonable error message. Otherwise,
462 * regurgitate strerror() since it's the best we
465 if (errno == ENOENT) {
466 (void) fprintf(stderr,
467 gettext("cannot open '%s': no such "
468 "GEOM provider\n"), arg);
469 (void) fprintf(stderr,
470 gettext("must be a full path or "
471 "shorthand device name\n"));
474 (void) fprintf(stderr,
475 gettext("cannot open '%s': %s\n"),
476 path, strerror(errno));
483 if (S_ISCHR(statbuf.st_mode)) {
484 statbuf.st_mode &= ~S_IFCHR;
485 statbuf.st_mode |= S_IFBLK;
491 * Determine whether this is a device or a file.
493 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
494 type = VDEV_TYPE_DISK;
495 } else if (S_ISREG(statbuf.st_mode)) {
496 type = VDEV_TYPE_FILE;
498 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
499 "GEOM provider or regular file\n"), path);
504 * Finally, we have the complete device or file, and we know that it is
505 * acceptable to use. Construct the nvlist to describe this vdev. All
506 * vdevs have a 'path' element, and devices also have a 'devid' element.
508 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
509 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
510 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
511 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
512 if (strcmp(type, VDEV_TYPE_DISK) == 0)
513 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
514 (uint64_t)wholedisk) == 0);
518 * For a whole disk, defer getting its devid until after labeling it.
520 if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
522 * Get the devid for the device.
526 char *minor = NULL, *devid_str = NULL;
528 if ((fd = open(path, O_RDONLY)) < 0) {
529 (void) fprintf(stderr, gettext("cannot open '%s': "
530 "%s\n"), path, strerror(errno));
535 if (devid_get(fd, &devid) == 0) {
536 if (devid_get_minor_name(fd, &minor) == 0 &&
537 (devid_str = devid_str_encode(devid, minor)) !=
539 verify(nvlist_add_string(vdev,
540 ZPOOL_CONFIG_DEVID, devid_str) == 0);
542 if (devid_str != NULL)
543 devid_str_free(devid_str);
545 devid_str_free(minor);
557 * Go through and verify the replication level of the pool is consistent.
558 * Performs the following checks:
560 * For the new spec, verifies that devices in mirrors and raidz are the
563 * If the current configuration already has inconsistent replication
564 * levels, ignore any other potential problems in the new spec.
566 * Otherwise, make sure that the current spec (if there is one) and the new
567 * spec have consistent replication levels.
569 typedef struct replication_level {
571 uint64_t zprl_children;
572 uint64_t zprl_parity;
573 } replication_level_t;
575 #define ZPOOL_FUZZ (16 * 1024 * 1024)
578 * Given a list of toplevel vdevs, return the current replication level. If
579 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
580 * an error message will be displayed for each self-inconsistent vdev.
582 static replication_level_t *
583 get_replication(nvlist_t *nvroot, boolean_t fatal)
591 replication_level_t lastrep, rep, *ret;
592 boolean_t dontreport;
594 ret = safe_malloc(sizeof (replication_level_t));
596 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
597 &top, &toplevels) == 0);
599 lastrep.zprl_type = NULL;
600 for (t = 0; t < toplevels; t++) {
601 uint64_t is_log = B_FALSE;
606 * For separate logs we ignore the top level vdev replication
609 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
613 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
615 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
616 &child, &children) != 0) {
618 * This is a 'file' or 'disk' vdev.
620 rep.zprl_type = type;
621 rep.zprl_children = 1;
627 * This is a mirror or RAID-Z vdev. Go through and make
628 * sure the contents are all the same (files vs. disks),
629 * keeping track of the number of elements in the
632 * We also check that the size of each vdev (if it can
633 * be determined) is the same.
635 rep.zprl_type = type;
636 rep.zprl_children = 0;
638 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
639 verify(nvlist_lookup_uint64(nv,
640 ZPOOL_CONFIG_NPARITY,
641 &rep.zprl_parity) == 0);
642 assert(rep.zprl_parity != 0);
648 * The 'dontreport' variable indicates that we've
649 * already reported an error for this spec, so don't
650 * bother doing it again.
655 for (c = 0; c < children; c++) {
656 nvlist_t *cnv = child[c];
658 struct stat64 statbuf;
659 uint64_t size = -1ULL;
665 verify(nvlist_lookup_string(cnv,
666 ZPOOL_CONFIG_TYPE, &childtype) == 0);
669 * If this is a replacing or spare vdev, then
670 * get the real first child of the vdev.
672 if (strcmp(childtype,
673 VDEV_TYPE_REPLACING) == 0 ||
674 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
678 verify(nvlist_lookup_nvlist_array(cnv,
679 ZPOOL_CONFIG_CHILDREN, &rchild,
681 assert(rchildren == 2);
684 verify(nvlist_lookup_string(cnv,
689 verify(nvlist_lookup_string(cnv,
690 ZPOOL_CONFIG_PATH, &path) == 0);
693 * If we have a raidz/mirror that combines disks
694 * with files, report it as an error.
696 if (!dontreport && type != NULL &&
697 strcmp(type, childtype) != 0) {
703 "mismatched replication "
704 "level: %s contains both "
705 "files and devices\n"),
713 * According to stat(2), the value of 'st_size'
714 * is undefined for block devices and character
715 * devices. But there is no effective way to
716 * determine the real size in userland.
718 * Instead, we'll take advantage of an
719 * implementation detail of spec_size(). If the
720 * device is currently open, then we (should)
721 * return a valid size.
723 * If we still don't get a valid size (indicated
724 * by a size of 0 or MAXOFFSET_T), then ignore
725 * this device altogether.
727 if ((fd = open(path, O_RDONLY)) >= 0) {
728 err = fstat64(fd, &statbuf);
731 err = stat64(path, &statbuf);
735 statbuf.st_size == 0 ||
736 statbuf.st_size == MAXOFFSET_T)
739 size = statbuf.st_size;
742 * Also make sure that devices and
743 * slices have a consistent size. If
744 * they differ by a significant amount
745 * (~16MB) then report an error.
748 (vdev_size != -1ULL &&
749 (labs(size - vdev_size) >
756 "%s contains devices of "
757 "different sizes\n"),
770 * At this point, we have the replication of the last toplevel
771 * vdev in 'rep'. Compare it to 'lastrep' to see if its
774 if (lastrep.zprl_type != NULL) {
775 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
781 "mismatched replication level: "
782 "both %s and %s vdevs are "
784 lastrep.zprl_type, rep.zprl_type);
787 } else if (lastrep.zprl_parity != rep.zprl_parity) {
793 "mismatched replication level: "
794 "both %llu and %llu device parity "
795 "%s vdevs are present\n"),
801 } else if (lastrep.zprl_children != rep.zprl_children) {
807 "mismatched replication level: "
808 "both %llu-way and %llu-way %s "
809 "vdevs are present\n"),
810 lastrep.zprl_children,
827 * Check the replication level of the vdev spec against the current pool. Calls
828 * get_replication() to make sure the new spec is self-consistent. If the pool
829 * has a consistent replication level, then we ignore any errors. Otherwise,
830 * report any difference between the two.
833 check_replication(nvlist_t *config, nvlist_t *newroot)
837 replication_level_t *current = NULL, *new;
841 * If we have a current pool configuration, check to see if it's
842 * self-consistent. If not, simply return success.
844 if (config != NULL) {
847 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
849 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
853 * for spares there may be no children, and therefore no
854 * replication level to check
856 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
857 &child, &children) != 0) || (children == 0)) {
863 * If all we have is logs then there's no replication level to check.
865 if (num_logs(newroot) == children) {
871 * Get the replication level of the new vdev spec, reporting any
872 * inconsistencies found.
874 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
880 * Check to see if the new vdev spec matches the replication level of
884 if (current != NULL) {
885 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
887 "mismatched replication level: pool uses %s "
888 "and new vdev is %s\n"),
889 current->zprl_type, new->zprl_type);
891 } else if (current->zprl_parity != new->zprl_parity) {
893 "mismatched replication level: pool uses %llu "
894 "device parity and new vdev uses %llu\n"),
895 current->zprl_parity, new->zprl_parity);
897 } else if (current->zprl_children != new->zprl_children) {
899 "mismatched replication level: pool uses %llu-way "
900 "%s and new vdev uses %llu-way %s\n"),
901 current->zprl_children, current->zprl_type,
902 new->zprl_children, new->zprl_type);
916 * Go through and find any whole disks in the vdev specification, labelling them
917 * as appropriate. When constructing the vdev spec, we were unable to open this
918 * device in order to provide a devid. Now that we have labelled the disk and
919 * know that slice 0 is valid, we can construct the devid now.
921 * If the disk was already labeled with an EFI label, we will have gotten the
922 * devid already (because we were able to open the whole disk). Otherwise, we
923 * need to get the devid after we label the disk.
926 make_disks(zpool_handle_t *zhp, nvlist_t *nv)
930 char *type, *path, *diskname;
931 char buf[MAXPATHLEN];
936 char *minor = NULL, *devid_str = NULL;
938 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
940 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
941 &child, &children) != 0) {
943 if (strcmp(type, VDEV_TYPE_DISK) != 0)
947 * We have a disk device. Get the path to the device
948 * and see if it's a whole disk by appending the backup
949 * slice and stat()ing the device.
951 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
952 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
953 &wholedisk) != 0 || !wholedisk)
956 diskname = strrchr(path, '/');
957 assert(diskname != NULL);
959 if (zpool_label_disk(g_zfs, zhp, diskname) == -1)
963 * Fill in the devid, now that we've labeled the disk.
965 (void) snprintf(buf, sizeof (buf), "%ss0", path);
966 if ((fd = open(buf, O_RDONLY)) < 0) {
967 (void) fprintf(stderr,
968 gettext("cannot open '%s': %s\n"),
969 buf, strerror(errno));
973 if (devid_get(fd, &devid) == 0) {
974 if (devid_get_minor_name(fd, &minor) == 0 &&
975 (devid_str = devid_str_encode(devid, minor)) !=
977 verify(nvlist_add_string(nv,
978 ZPOOL_CONFIG_DEVID, devid_str) == 0);
980 if (devid_str != NULL)
981 devid_str_free(devid_str);
983 devid_str_free(minor);
988 * Update the path to refer to the 's0' slice. The presence of
989 * the 'whole_disk' field indicates to the CLI that we should
990 * chop off the slice number when displaying the device in
993 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0);
1000 for (c = 0; c < children; c++)
1001 if ((ret = make_disks(zhp, child[c])) != 0)
1004 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1005 &child, &children) == 0)
1006 for (c = 0; c < children; c++)
1007 if ((ret = make_disks(zhp, child[c])) != 0)
1010 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1011 &child, &children) == 0)
1012 for (c = 0; c < children; c++)
1013 if ((ret = make_disks(zhp, child[c])) != 0)
1018 #endif /* illumos */
1021 * Determine if the given path is a hot spare within the given configuration.
1024 is_spare(nvlist_t *config, const char *path)
1030 uint64_t guid, spareguid;
1036 if ((fd = open(path, O_RDONLY)) < 0)
1039 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
1041 state != POOL_STATE_SPARE ||
1042 zpool_read_label(fd, &label) != 0) {
1050 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
1053 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1055 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1056 &spares, &nspares) == 0) {
1057 for (i = 0; i < nspares; i++) {
1058 verify(nvlist_lookup_uint64(spares[i],
1059 ZPOOL_CONFIG_GUID, &spareguid) == 0);
1060 if (spareguid == guid)
1069 * Go through and find any devices that are in use. We rely on libdiskmgt for
1070 * the majority of this task.
1073 is_device_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1074 boolean_t replacing, boolean_t isspare)
1080 char buf[MAXPATHLEN];
1082 boolean_t anyinuse = B_FALSE;
1084 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1086 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1087 &child, &children) != 0) {
1089 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1092 * As a generic check, we look to see if this is a replace of a
1093 * hot spare within the same pool. If so, we allow it
1094 * regardless of what libdiskmgt or zpool_in_use() says.
1098 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1099 &wholedisk) == 0 && wholedisk)
1100 (void) snprintf(buf, sizeof (buf), "%ss0",
1104 (void) strlcpy(buf, path, sizeof (buf));
1106 if (is_spare(config, buf))
1110 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1111 ret = check_device(path, force, isspare);
1112 else if (strcmp(type, VDEV_TYPE_FILE) == 0)
1113 ret = check_file(path, force, isspare);
1118 for (c = 0; c < children; c++)
1119 if (is_device_in_use(config, child[c], force, replacing,
1123 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1124 &child, &children) == 0)
1125 for (c = 0; c < children; c++)
1126 if (is_device_in_use(config, child[c], force, replacing,
1130 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1131 &child, &children) == 0)
1132 for (c = 0; c < children; c++)
1133 if (is_device_in_use(config, child[c], force, replacing,
1141 is_grouping(const char *type, int *mindev, int *maxdev)
1143 if (strncmp(type, "raidz", 5) == 0) {
1144 const char *p = type + 5;
1150 } else if (*p == '0') {
1151 return (NULL); /* no zero prefixes allowed */
1154 nparity = strtol(p, &end, 10);
1155 if (errno != 0 || nparity < 1 || nparity >= 255 ||
1161 *mindev = nparity + 1;
1164 return (VDEV_TYPE_RAIDZ);
1170 if (strcmp(type, "mirror") == 0) {
1173 return (VDEV_TYPE_MIRROR);
1176 if (strcmp(type, "spare") == 0) {
1179 return (VDEV_TYPE_SPARE);
1182 if (strcmp(type, "log") == 0) {
1185 return (VDEV_TYPE_LOG);
1188 if (strcmp(type, "cache") == 0) {
1191 return (VDEV_TYPE_L2CACHE);
1198 * Construct a syntactically valid vdev specification,
1199 * and ensure that all devices and files exist and can be opened.
1200 * Note: we don't bother freeing anything in the error paths
1201 * because the program is just going to exit anyway.
1204 construct_spec(int argc, char **argv)
1206 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1207 int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
1210 boolean_t seen_logs;
1220 seen_logs = B_FALSE;
1226 * If it's a mirror or raidz, the subsequent arguments are
1227 * its leaves -- until we encounter the next mirror or raidz.
1229 if ((type = is_grouping(argv[0], &mindev, &maxdev)) != NULL) {
1230 nvlist_t **child = NULL;
1231 int c, children = 0;
1233 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1234 if (spares != NULL) {
1235 (void) fprintf(stderr,
1236 gettext("invalid vdev "
1237 "specification: 'spare' can be "
1238 "specified only once\n"));
1244 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1246 (void) fprintf(stderr,
1247 gettext("invalid vdev "
1248 "specification: 'log' can be "
1249 "specified only once\n"));
1257 * A log is not a real grouping device.
1258 * We just set is_log and continue.
1263 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1264 if (l2cache != NULL) {
1265 (void) fprintf(stderr,
1266 gettext("invalid vdev "
1267 "specification: 'cache' can be "
1268 "specified only once\n"));
1275 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1276 (void) fprintf(stderr,
1277 gettext("invalid vdev "
1278 "specification: unsupported 'log' "
1279 "device: %s\n"), type);
1285 for (c = 1; c < argc; c++) {
1286 if (is_grouping(argv[c], NULL, NULL) != NULL)
1289 child = realloc(child,
1290 children * sizeof (nvlist_t *));
1293 if ((nv = make_leaf_vdev(argv[c], B_FALSE))
1296 child[children - 1] = nv;
1299 if (children < mindev) {
1300 (void) fprintf(stderr, gettext("invalid vdev "
1301 "specification: %s requires at least %d "
1302 "devices\n"), argv[0], mindev);
1306 if (children > maxdev) {
1307 (void) fprintf(stderr, gettext("invalid vdev "
1308 "specification: %s supports no more than "
1309 "%d devices\n"), argv[0], maxdev);
1316 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1320 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1322 nl2cache = children;
1325 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1327 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1329 verify(nvlist_add_uint64(nv,
1330 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1331 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1332 verify(nvlist_add_uint64(nv,
1333 ZPOOL_CONFIG_NPARITY,
1336 verify(nvlist_add_nvlist_array(nv,
1337 ZPOOL_CONFIG_CHILDREN, child,
1340 for (c = 0; c < children; c++)
1341 nvlist_free(child[c]);
1346 * We have a device. Pass off to make_leaf_vdev() to
1347 * construct the appropriate nvlist describing the vdev.
1349 if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
1358 top = realloc(top, toplevels * sizeof (nvlist_t *));
1361 top[toplevels - 1] = nv;
1364 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1365 (void) fprintf(stderr, gettext("invalid vdev "
1366 "specification: at least one toplevel vdev must be "
1371 if (seen_logs && nlogs == 0) {
1372 (void) fprintf(stderr, gettext("invalid vdev specification: "
1373 "log requires at least 1 device\n"));
1378 * Finally, create nvroot and add all top-level vdevs to it.
1380 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1381 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1382 VDEV_TYPE_ROOT) == 0);
1383 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1384 top, toplevels) == 0);
1386 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1387 spares, nspares) == 0);
1389 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1390 l2cache, nl2cache) == 0);
1392 for (t = 0; t < toplevels; t++)
1393 nvlist_free(top[t]);
1394 for (t = 0; t < nspares; t++)
1395 nvlist_free(spares[t]);
1396 for (t = 0; t < nl2cache; t++)
1397 nvlist_free(l2cache[t]);
1408 split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1409 splitflags_t flags, int argc, char **argv)
1411 nvlist_t *newroot = NULL, **child;
1415 if ((newroot = construct_spec(argc, argv)) == NULL) {
1416 (void) fprintf(stderr, gettext("Unable to build a "
1417 "pool from the specified devices\n"));
1422 if (!flags.dryrun && make_disks(zhp, newroot) != 0) {
1423 nvlist_free(newroot);
1428 /* avoid any tricks in the spec */
1429 verify(nvlist_lookup_nvlist_array(newroot,
1430 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1431 for (c = 0; c < children; c++) {
1436 verify(nvlist_lookup_string(child[c],
1437 ZPOOL_CONFIG_PATH, &path) == 0);
1438 if ((type = is_grouping(path, &min, &max)) != NULL) {
1439 (void) fprintf(stderr, gettext("Cannot use "
1440 "'%s' as a device for splitting\n"), type);
1441 nvlist_free(newroot);
1447 if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1448 if (newroot != NULL)
1449 nvlist_free(newroot);
1457 * Get and validate the contents of the given vdev specification. This ensures
1458 * that the nvlist returned is well-formed, that all the devices exist, and that
1459 * they are not currently in use by any other known consumer. The 'poolconfig'
1460 * parameter is the current configuration of the pool when adding devices
1461 * existing pool, and is used to perform additional checks, such as changing the
1462 * replication level of the pool. It can be 'NULL' to indicate that this is a
1463 * new pool. The 'force' flag controls whether devices should be forcefully
1464 * added, even if they appear in use.
1467 make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
1468 boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1471 nvlist_t *poolconfig = NULL;
1475 * Construct the vdev specification. If this is successful, we know
1476 * that we have a valid specification, and that all devices can be
1479 if ((newroot = construct_spec(argc, argv)) == NULL)
1482 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1486 * Validate each device to make sure that its not shared with another
1487 * subsystem. We do this even if 'force' is set, because there are some
1488 * uses (such as a dedicated dump device) that even '-f' cannot
1491 if (is_device_in_use(poolconfig, newroot, force, replacing, B_FALSE)) {
1492 nvlist_free(newroot);
1497 * Check the replication level of the given vdevs and report any errors
1498 * found. We include the existing pool spec, if any, as we need to
1499 * catch changes against the existing replication level.
1501 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1502 nvlist_free(newroot);
1508 * Run through the vdev specification and label any whole disks found.
1510 if (!dryrun && make_disks(zhp, newroot) != 0) {
1511 nvlist_free(newroot);