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 libblkid to make 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.
68 #include <libnvpair.h>
73 #include <sys/efi_partition.h>
76 #include <sys/mntent.h>
77 #include <uuid/uuid.h>
79 #include <blkid/blkid.h>
81 #define blkid_cache void *
82 #endif /* HAVE_LIBBLKID */
84 #include "zpool_util.h"
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 * Check that a file is valid. All we can do in this case is check that it's
118 * not in use by another pool, and not in use by swap.
121 check_file(const char *file, boolean_t force, boolean_t isspare)
129 if ((fd = open(file, O_RDONLY)) < 0)
132 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
136 case POOL_STATE_ACTIVE:
137 desc = gettext("active");
140 case POOL_STATE_EXPORTED:
141 desc = gettext("exported");
144 case POOL_STATE_POTENTIALLY_ACTIVE:
145 desc = gettext("potentially active");
149 desc = gettext("unknown");
154 * Allow hot spares to be shared between pools.
156 if (state == POOL_STATE_SPARE && isspare)
159 if (state == POOL_STATE_ACTIVE ||
160 state == POOL_STATE_SPARE || !force) {
162 case POOL_STATE_SPARE:
163 vdev_error(gettext("%s is reserved as a hot "
164 "spare for pool %s\n"), file, name);
167 vdev_error(gettext("%s is part of %s pool "
168 "'%s'\n"), file, desc, name);
184 (void) fprintf(stderr, gettext("warning: device in use checking "
185 "failed: %s\n"), strerror(err));
189 check_slice(const char *path, blkid_cache cache, int force, boolean_t isspare)
195 /* No valid type detected device is safe to use */
196 value = blkid_get_tag_value(cache, "TYPE", path);
201 * If libblkid detects a ZFS device, we check the device
202 * using check_file() to see if it's safe. The one safe
203 * case is a spare device shared between multiple pools.
205 if (strcmp(value, "zfs") == 0) {
206 err = check_file(path, force, isspare);
212 vdev_error(gettext("%s contains a filesystem of "
213 "type '%s'\n"), path, value);
219 err = check_file(path, force, isspare);
220 #endif /* HAVE_LIBBLKID */
226 * Validate a whole disk. Iterate over all slices on the disk and make sure
227 * that none is in use by calling check_slice().
230 check_disk(const char *path, blkid_cache cache, int force,
231 boolean_t isspare, boolean_t iswholedisk)
234 char slice_path[MAXPATHLEN];
238 /* This is not a wholedisk we only check the given partition */
240 return check_slice(path, cache, force, isspare);
243 * When the device is a whole disk try to read the efi partition
244 * label. If this is successful we safely check the all of the
245 * partitions. However, when it fails it may simply be because
246 * the disk is partitioned via the MBR. Since we currently can
247 * not easily decode the MBR return a failure and prompt to the
248 * user to use force option since we cannot check the partitions.
250 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0) {
255 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
261 vdev_error(gettext("%s does not contain an EFI "
262 "label but it may contain partition\n"
263 "information in the MBR.\n"), path);
269 * The primary efi partition label is damaged however the secondary
270 * label at the end of the device is intact. Rather than use this
271 * label we should play it safe and treat this as a non efi device.
273 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
278 /* Partitions will no be created using the backup */
281 vdev_error(gettext("%s contains a corrupt primary "
282 "EFI label.\n"), path);
287 for (i = 0; i < vtoc->efi_nparts; i++) {
289 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED ||
290 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
293 if (strncmp(path, UDISK_ROOT, strlen(UDISK_ROOT)) == 0)
294 (void) snprintf(slice_path, sizeof (slice_path),
295 "%s%s%d", path, "-part", i+1);
297 (void) snprintf(slice_path, sizeof (slice_path),
298 "%s%s%d", path, isdigit(path[strlen(path)-1]) ?
301 err = check_slice(slice_path, cache, force, isspare);
313 check_device(const char *path, boolean_t force,
314 boolean_t isspare, boolean_t iswholedisk)
316 static blkid_cache cache = NULL;
320 * There is no easy way to add a correct blkid_put_cache() call,
321 * memory will be reclaimed when the command exits.
326 if ((err = blkid_get_cache(&cache, NULL)) != 0) {
331 if ((err = blkid_probe_all(cache)) != 0) {
332 blkid_put_cache(cache);
337 #endif /* HAVE_LIBBLKID */
339 return check_disk(path, cache, force, isspare, iswholedisk);
343 * By "whole disk" we mean an entire physical disk (something we can
344 * label, toggle the write cache on, etc.) as opposed to the full
345 * capacity of a pseudo-device such as lofi or did. We act as if we
346 * are labeling the disk, which should be a pretty good test of whether
347 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
351 is_whole_disk(const char *path)
353 struct dk_gpt *label;
356 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
358 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
368 * This may be a shorthand device path or it could be total gibberish.
369 * Check to see if it is a known device available in zfs_vdev_paths.
370 * As part of this check, see if we've been given an entire disk
371 * (minus the slice number).
374 is_shorthand_path(const char *arg, char *path,
375 struct stat64 *statbuf, boolean_t *wholedisk)
379 error = zfs_resolve_shortname(arg, path, MAXPATHLEN);
381 *wholedisk = is_whole_disk(path);
382 if (*wholedisk || (stat64(path, statbuf) == 0))
386 strlcpy(path, arg, sizeof(path));
387 memset(statbuf, 0, sizeof(*statbuf));
388 *wholedisk = B_FALSE;
394 * Determine if the given path is a hot spare within the given configuration.
395 * If no configuration is given we rely solely on the label.
398 is_spare(nvlist_t *config, const char *path)
404 uint64_t guid, spareguid;
410 if ((fd = open(path, O_RDONLY)) < 0)
413 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
415 state != POOL_STATE_SPARE ||
416 zpool_read_label(fd, &label) != 0) {
427 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
430 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
432 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
433 &spares, &nspares) == 0) {
434 for (i = 0; i < nspares; i++) {
435 verify(nvlist_lookup_uint64(spares[i],
436 ZPOOL_CONFIG_GUID, &spareguid) == 0);
437 if (spareguid == guid)
446 * Create a leaf vdev. Determine if this is a file or a device. If it's a
447 * device, fill in the device id to make a complete nvlist. Valid forms for a
450 * /dev/xxx Complete disk path
451 * /xxx Full path to file
452 * xxx Shorthand for <zfs_vdev_paths>/xxx
455 make_leaf_vdev(nvlist_t *props, const char *arg, uint64_t is_log)
457 char path[MAXPATHLEN];
458 struct stat64 statbuf;
459 nvlist_t *vdev = NULL;
461 boolean_t wholedisk = B_FALSE;
465 * Determine what type of vdev this is, and put the full path into
466 * 'path'. We detect whether this is a device of file afterwards by
467 * checking the st_mode of the file.
471 * Complete device or file path. Exact type is determined by
472 * examining the file descriptor afterwards. Symbolic links
473 * are resolved to their real paths for the is_whole_disk()
474 * and S_ISBLK/S_ISREG type checks. However, we are careful
475 * to store the given path as ZPOOL_CONFIG_PATH to ensure we
476 * can leverage udev's persistent device labels.
478 if (realpath(arg, path) == NULL) {
479 (void) fprintf(stderr,
480 gettext("cannot resolve path '%s'\n"), arg);
484 wholedisk = is_whole_disk(path);
485 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
486 (void) fprintf(stderr,
487 gettext("cannot open '%s': %s\n"),
488 path, strerror(errno));
492 /* After is_whole_disk() check restore original passed path */
493 strlcpy(path, arg, MAXPATHLEN);
495 err = is_shorthand_path(arg, path, &statbuf, &wholedisk);
498 * If we got ENOENT, then the user gave us
499 * gibberish, so try to direct them with a
500 * reasonable error message. Otherwise,
501 * regurgitate strerror() since it's the best we
505 (void) fprintf(stderr,
506 gettext("cannot open '%s': no such "
507 "device in %s\n"), arg, DISK_ROOT);
508 (void) fprintf(stderr,
509 gettext("must be a full path or "
510 "shorthand device name\n"));
513 (void) fprintf(stderr,
514 gettext("cannot open '%s': %s\n"),
515 path, strerror(errno));
522 * Determine whether this is a device or a file.
524 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
525 type = VDEV_TYPE_DISK;
526 } else if (S_ISREG(statbuf.st_mode)) {
527 type = VDEV_TYPE_FILE;
529 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
530 "block device or regular file\n"), path);
535 * Finally, we have the complete device or file, and we know that it is
536 * acceptable to use. Construct the nvlist to describe this vdev. All
537 * vdevs have a 'path' element, and devices also have a 'devid' element.
539 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
540 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
541 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
542 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
543 if (strcmp(type, VDEV_TYPE_DISK) == 0)
544 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
545 (uint64_t)wholedisk) == 0);
551 if (nvlist_lookup_string(props,
552 zpool_prop_to_name(ZPOOL_PROP_ASHIFT), &value) == 0)
553 zfs_nicestrtonum(NULL, value, &ashift);
556 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_ASHIFT,
564 * Go through and verify the replication level of the pool is consistent.
565 * Performs the following checks:
567 * For the new spec, verifies that devices in mirrors and raidz are the
570 * If the current configuration already has inconsistent replication
571 * levels, ignore any other potential problems in the new spec.
573 * Otherwise, make sure that the current spec (if there is one) and the new
574 * spec have consistent replication levels.
576 typedef struct replication_level {
578 uint64_t zprl_children;
579 uint64_t zprl_parity;
580 } replication_level_t;
582 #define ZPOOL_FUZZ (16 * 1024 * 1024)
585 * Given a list of toplevel vdevs, return the current replication level. If
586 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
587 * an error message will be displayed for each self-inconsistent vdev.
589 static replication_level_t *
590 get_replication(nvlist_t *nvroot, boolean_t fatal)
598 replication_level_t lastrep = { 0 }, rep, *ret;
599 boolean_t dontreport;
601 ret = safe_malloc(sizeof (replication_level_t));
603 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
604 &top, &toplevels) == 0);
606 lastrep.zprl_type = NULL;
607 for (t = 0; t < toplevels; t++) {
608 uint64_t is_log = B_FALSE;
613 * For separate logs we ignore the top level vdev replication
616 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
620 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
622 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
623 &child, &children) != 0) {
625 * This is a 'file' or 'disk' vdev.
627 rep.zprl_type = type;
628 rep.zprl_children = 1;
634 * This is a mirror or RAID-Z vdev. Go through and make
635 * sure the contents are all the same (files vs. disks),
636 * keeping track of the number of elements in the
639 * We also check that the size of each vdev (if it can
640 * be determined) is the same.
642 rep.zprl_type = type;
643 rep.zprl_children = 0;
645 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
646 verify(nvlist_lookup_uint64(nv,
647 ZPOOL_CONFIG_NPARITY,
648 &rep.zprl_parity) == 0);
649 assert(rep.zprl_parity != 0);
655 * The 'dontreport' variable indicates that we've
656 * already reported an error for this spec, so don't
657 * bother doing it again.
662 for (c = 0; c < children; c++) {
663 nvlist_t *cnv = child[c];
665 struct stat64 statbuf;
666 uint64_t size = -1ULL;
672 verify(nvlist_lookup_string(cnv,
673 ZPOOL_CONFIG_TYPE, &childtype) == 0);
676 * If this is a replacing or spare vdev, then
677 * get the real first child of the vdev.
679 if (strcmp(childtype,
680 VDEV_TYPE_REPLACING) == 0 ||
681 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
685 verify(nvlist_lookup_nvlist_array(cnv,
686 ZPOOL_CONFIG_CHILDREN, &rchild,
688 assert(rchildren == 2);
691 verify(nvlist_lookup_string(cnv,
696 verify(nvlist_lookup_string(cnv,
697 ZPOOL_CONFIG_PATH, &path) == 0);
700 * If we have a raidz/mirror that combines disks
701 * with files, report it as an error.
703 if (!dontreport && type != NULL &&
704 strcmp(type, childtype) != 0) {
710 "mismatched replication "
711 "level: %s contains both "
712 "files and devices\n"),
720 * According to stat(2), the value of 'st_size'
721 * is undefined for block devices and character
722 * devices. But there is no effective way to
723 * determine the real size in userland.
725 * Instead, we'll take advantage of an
726 * implementation detail of spec_size(). If the
727 * device is currently open, then we (should)
728 * return a valid size.
730 * If we still don't get a valid size (indicated
731 * by a size of 0 or MAXOFFSET_T), then ignore
732 * this device altogether.
734 if ((fd = open(path, O_RDONLY)) >= 0) {
735 err = fstat64(fd, &statbuf);
738 err = stat64(path, &statbuf);
742 statbuf.st_size == 0 ||
743 statbuf.st_size == MAXOFFSET_T)
746 size = statbuf.st_size;
749 * Also make sure that devices and
750 * slices have a consistent size. If
751 * they differ by a significant amount
752 * (~16MB) then report an error.
755 (vdev_size != -1ULL &&
756 (labs(size - vdev_size) >
763 "%s contains devices of "
764 "different sizes\n"),
777 * At this point, we have the replication of the last toplevel
778 * vdev in 'rep'. Compare it to 'lastrep' to see if its
781 if (lastrep.zprl_type != NULL) {
782 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
788 "mismatched replication level: "
789 "both %s and %s vdevs are "
791 lastrep.zprl_type, rep.zprl_type);
794 } else if (lastrep.zprl_parity != rep.zprl_parity) {
800 "mismatched replication level: "
801 "both %llu and %llu device parity "
802 "%s vdevs are present\n"),
808 } else if (lastrep.zprl_children != rep.zprl_children) {
814 "mismatched replication level: "
815 "both %llu-way and %llu-way %s "
816 "vdevs are present\n"),
817 lastrep.zprl_children,
834 * Check the replication level of the vdev spec against the current pool. Calls
835 * get_replication() to make sure the new spec is self-consistent. If the pool
836 * has a consistent replication level, then we ignore any errors. Otherwise,
837 * report any difference between the two.
840 check_replication(nvlist_t *config, nvlist_t *newroot)
844 replication_level_t *current = NULL, *new;
848 * If we have a current pool configuration, check to see if it's
849 * self-consistent. If not, simply return success.
851 if (config != NULL) {
854 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
856 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
860 * for spares there may be no children, and therefore no
861 * replication level to check
863 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
864 &child, &children) != 0) || (children == 0)) {
870 * If all we have is logs then there's no replication level to check.
872 if (num_logs(newroot) == children) {
878 * Get the replication level of the new vdev spec, reporting any
879 * inconsistencies found.
881 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
887 * Check to see if the new vdev spec matches the replication level of
891 if (current != NULL) {
892 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
894 "mismatched replication level: pool uses %s "
895 "and new vdev is %s\n"),
896 current->zprl_type, new->zprl_type);
898 } else if (current->zprl_parity != new->zprl_parity) {
900 "mismatched replication level: pool uses %llu "
901 "device parity and new vdev uses %llu\n"),
902 current->zprl_parity, new->zprl_parity);
904 } else if (current->zprl_children != new->zprl_children) {
906 "mismatched replication level: pool uses %llu-way "
907 "%s and new vdev uses %llu-way %s\n"),
908 current->zprl_children, current->zprl_type,
909 new->zprl_children, new->zprl_type);
922 zero_label(char *path)
924 const int size = 4096;
928 if ((fd = open(path, O_WRONLY|O_EXCL)) < 0) {
929 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
930 path, strerror(errno));
934 memset(buf, 0, size);
935 err = write(fd, buf, size);
936 (void) fdatasync(fd);
940 (void) fprintf(stderr, gettext("cannot zero first %d bytes "
941 "of '%s': %s\n"), size, path, strerror(errno));
946 (void) fprintf(stderr, gettext("could only zero %d/%d bytes "
947 "of '%s'\n"), err, size, path);
955 * Go through and find any whole disks in the vdev specification, labelling them
956 * as appropriate. When constructing the vdev spec, we were unable to open this
957 * device in order to provide a devid. Now that we have labelled the disk and
958 * know that slice 0 is valid, we can construct the devid now.
960 * If the disk was already labeled with an EFI label, we will have gotten the
961 * devid already (because we were able to open the whole disk). Otherwise, we
962 * need to get the devid after we label the disk.
965 make_disks(zpool_handle_t *zhp, nvlist_t *nv)
970 char devpath[MAXPATHLEN];
971 char udevpath[MAXPATHLEN];
973 struct stat64 statbuf;
974 int is_exclusive = 0;
978 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
980 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
981 &child, &children) != 0) {
983 if (strcmp(type, VDEV_TYPE_DISK) != 0)
987 * We have a disk device. If this is a whole disk write
988 * out the efi partition table, otherwise write zero's to
989 * the first 4k of the partition. This is to ensure that
990 * libblkid will not misidentify the partition due to a
991 * magic value left by the previous filesystem.
993 verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
994 verify(!nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
998 (void) zero_label(path);
1002 if (realpath(path, devpath) == NULL) {
1004 (void) fprintf(stderr,
1005 gettext("cannot resolve path '%s'\n"), path);
1010 * Remove any previously existing symlink from a udev path to
1011 * the device before labeling the disk. This makes
1012 * zpool_label_disk_wait() truly wait for the new link to show
1013 * up instead of returning if it finds an old link still in
1014 * place. Otherwise there is a window between when udev
1015 * deletes and recreates the link during which access attempts
1016 * will fail with ENOENT.
1018 strncpy(udevpath, path, MAXPATHLEN);
1019 (void) zfs_append_partition(udevpath, MAXPATHLEN);
1021 fd = open(devpath, O_RDWR|O_EXCL);
1030 * If the partition exists, contains a valid spare label,
1031 * and is opened exclusively there is no need to partition
1032 * it. Hot spares have already been partitioned and are
1033 * held open exclusively by the kernel as a safety measure.
1035 * If the provided path is for a /dev/disk/ device its
1036 * symbolic link will be removed, partition table created,
1037 * and then block until udev creates the new link.
1039 if (!is_exclusive || !is_spare(NULL, udevpath)) {
1040 ret = strncmp(udevpath,UDISK_ROOT,strlen(UDISK_ROOT));
1042 ret = lstat64(udevpath, &statbuf);
1043 if (ret == 0 && S_ISLNK(statbuf.st_mode))
1044 (void) unlink(udevpath);
1047 if (zpool_label_disk(g_zfs, zhp,
1048 strrchr(devpath, '/') + 1) == -1)
1051 ret = zpool_label_disk_wait(udevpath, 1000);
1053 (void) fprintf(stderr, gettext("cannot "
1054 "resolve path '%s': %d\n"), udevpath, ret);
1058 (void) zero_label(udevpath);
1062 * Update the path to refer to the partition. The presence of
1063 * the 'whole_disk' field indicates to the CLI that we should
1064 * chop off the partition number when displaying the device in
1067 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);
1072 for (c = 0; c < children; c++)
1073 if ((ret = make_disks(zhp, child[c])) != 0)
1076 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1077 &child, &children) == 0)
1078 for (c = 0; c < children; c++)
1079 if ((ret = make_disks(zhp, child[c])) != 0)
1082 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1083 &child, &children) == 0)
1084 for (c = 0; c < children; c++)
1085 if ((ret = make_disks(zhp, child[c])) != 0)
1092 * Go through and find any devices that are in use. We rely on libdiskmgt for
1093 * the majority of this task.
1096 check_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1097 boolean_t replacing, boolean_t isspare)
1103 char buf[MAXPATHLEN];
1104 uint64_t wholedisk = B_FALSE;
1106 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1108 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1109 &child, &children) != 0) {
1111 verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1112 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1113 verify(!nvlist_lookup_uint64(nv,
1114 ZPOOL_CONFIG_WHOLE_DISK, &wholedisk));
1117 * As a generic check, we look to see if this is a replace of a
1118 * hot spare within the same pool. If so, we allow it
1119 * regardless of what libblkid or zpool_in_use() says.
1122 (void) strlcpy(buf, path, sizeof (buf));
1124 ret = zfs_append_partition(buf, sizeof (buf));
1129 if (is_spare(config, buf))
1133 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1134 ret = check_device(path, force, isspare, wholedisk);
1136 if (strcmp(type, VDEV_TYPE_FILE) == 0)
1137 ret = check_file(path, force, isspare);
1142 for (c = 0; c < children; c++)
1143 if ((ret = check_in_use(config, child[c], force,
1144 replacing, B_FALSE)) != 0)
1147 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1148 &child, &children) == 0)
1149 for (c = 0; c < children; c++)
1150 if ((ret = check_in_use(config, child[c], force,
1151 replacing, B_TRUE)) != 0)
1154 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1155 &child, &children) == 0)
1156 for (c = 0; c < children; c++)
1157 if ((ret = check_in_use(config, child[c], force,
1158 replacing, B_FALSE)) != 0)
1165 is_grouping(const char *type, int *mindev, int *maxdev)
1167 if (strncmp(type, "raidz", 5) == 0) {
1168 const char *p = type + 5;
1174 } else if (*p == '0') {
1175 return (NULL); /* no zero prefixes allowed */
1178 nparity = strtol(p, &end, 10);
1179 if (errno != 0 || nparity < 1 || nparity >= 255 ||
1185 *mindev = nparity + 1;
1188 return (VDEV_TYPE_RAIDZ);
1194 if (strcmp(type, "mirror") == 0) {
1197 return (VDEV_TYPE_MIRROR);
1200 if (strcmp(type, "spare") == 0) {
1203 return (VDEV_TYPE_SPARE);
1206 if (strcmp(type, "log") == 0) {
1209 return (VDEV_TYPE_LOG);
1212 if (strcmp(type, "cache") == 0) {
1215 return (VDEV_TYPE_L2CACHE);
1222 * Construct a syntactically valid vdev specification,
1223 * and ensure that all devices and files exist and can be opened.
1224 * Note: we don't bother freeing anything in the error paths
1225 * because the program is just going to exit anyway.
1228 construct_spec(nvlist_t *props, int argc, char **argv)
1230 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1231 int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
1234 boolean_t seen_logs;
1244 seen_logs = B_FALSE;
1250 * If it's a mirror or raidz, the subsequent arguments are
1251 * its leaves -- until we encounter the next mirror or raidz.
1253 if ((type = is_grouping(argv[0], &mindev, &maxdev)) != NULL) {
1254 nvlist_t **child = NULL;
1255 int c, children = 0;
1257 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1258 if (spares != NULL) {
1259 (void) fprintf(stderr,
1260 gettext("invalid vdev "
1261 "specification: 'spare' can be "
1262 "specified only once\n"));
1268 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1270 (void) fprintf(stderr,
1271 gettext("invalid vdev "
1272 "specification: 'log' can be "
1273 "specified only once\n"));
1281 * A log is not a real grouping device.
1282 * We just set is_log and continue.
1287 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1288 if (l2cache != NULL) {
1289 (void) fprintf(stderr,
1290 gettext("invalid vdev "
1291 "specification: 'cache' can be "
1292 "specified only once\n"));
1299 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1300 (void) fprintf(stderr,
1301 gettext("invalid vdev "
1302 "specification: unsupported 'log' "
1303 "device: %s\n"), type);
1309 for (c = 1; c < argc; c++) {
1310 if (is_grouping(argv[c], NULL, NULL) != NULL)
1313 child = realloc(child,
1314 children * sizeof (nvlist_t *));
1317 if ((nv = make_leaf_vdev(props, argv[c], B_FALSE))
1320 child[children - 1] = nv;
1323 if (children < mindev) {
1324 (void) fprintf(stderr, gettext("invalid vdev "
1325 "specification: %s requires at least %d "
1326 "devices\n"), argv[0], mindev);
1330 if (children > maxdev) {
1331 (void) fprintf(stderr, gettext("invalid vdev "
1332 "specification: %s supports no more than "
1333 "%d devices\n"), argv[0], maxdev);
1340 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1344 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1346 nl2cache = children;
1349 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1351 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1353 verify(nvlist_add_uint64(nv,
1354 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1355 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1356 verify(nvlist_add_uint64(nv,
1357 ZPOOL_CONFIG_NPARITY,
1360 verify(nvlist_add_nvlist_array(nv,
1361 ZPOOL_CONFIG_CHILDREN, child,
1364 for (c = 0; c < children; c++)
1365 nvlist_free(child[c]);
1370 * We have a device. Pass off to make_leaf_vdev() to
1371 * construct the appropriate nvlist describing the vdev.
1373 if ((nv = make_leaf_vdev(props, argv[0], is_log)) == NULL)
1382 top = realloc(top, toplevels * sizeof (nvlist_t *));
1385 top[toplevels - 1] = nv;
1388 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1389 (void) fprintf(stderr, gettext("invalid vdev "
1390 "specification: at least one toplevel vdev must be "
1395 if (seen_logs && nlogs == 0) {
1396 (void) fprintf(stderr, gettext("invalid vdev specification: "
1397 "log requires at least 1 device\n"));
1402 * Finally, create nvroot and add all top-level vdevs to it.
1404 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1405 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1406 VDEV_TYPE_ROOT) == 0);
1407 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1408 top, toplevels) == 0);
1410 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1411 spares, nspares) == 0);
1413 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1414 l2cache, nl2cache) == 0);
1416 for (t = 0; t < toplevels; t++)
1417 nvlist_free(top[t]);
1418 for (t = 0; t < nspares; t++)
1419 nvlist_free(spares[t]);
1420 for (t = 0; t < nl2cache; t++)
1421 nvlist_free(l2cache[t]);
1432 split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1433 splitflags_t flags, int argc, char **argv)
1435 nvlist_t *newroot = NULL, **child;
1439 if ((newroot = construct_spec(props, argc, argv)) == NULL) {
1440 (void) fprintf(stderr, gettext("Unable to build a "
1441 "pool from the specified devices\n"));
1445 if (!flags.dryrun && make_disks(zhp, newroot) != 0) {
1446 nvlist_free(newroot);
1450 /* avoid any tricks in the spec */
1451 verify(nvlist_lookup_nvlist_array(newroot,
1452 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1453 for (c = 0; c < children; c++) {
1458 verify(nvlist_lookup_string(child[c],
1459 ZPOOL_CONFIG_PATH, &path) == 0);
1460 if ((type = is_grouping(path, &min, &max)) != NULL) {
1461 (void) fprintf(stderr, gettext("Cannot use "
1462 "'%s' as a device for splitting\n"), type);
1463 nvlist_free(newroot);
1469 if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1470 if (newroot != NULL)
1471 nvlist_free(newroot);
1479 * Get and validate the contents of the given vdev specification. This ensures
1480 * that the nvlist returned is well-formed, that all the devices exist, and that
1481 * they are not currently in use by any other known consumer. The 'poolconfig'
1482 * parameter is the current configuration of the pool when adding devices
1483 * existing pool, and is used to perform additional checks, such as changing the
1484 * replication level of the pool. It can be 'NULL' to indicate that this is a
1485 * new pool. The 'force' flag controls whether devices should be forcefully
1486 * added, even if they appear in use.
1489 make_root_vdev(zpool_handle_t *zhp, nvlist_t *props, int force, int check_rep,
1490 boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1493 nvlist_t *poolconfig = NULL;
1497 * Construct the vdev specification. If this is successful, we know
1498 * that we have a valid specification, and that all devices can be
1501 if ((newroot = construct_spec(props, argc, argv)) == NULL)
1504 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1508 * Validate each device to make sure that its not shared with another
1509 * subsystem. We do this even if 'force' is set, because there are some
1510 * uses (such as a dedicated dump device) that even '-f' cannot
1513 if (check_in_use(poolconfig, newroot, force, replacing, B_FALSE) != 0) {
1514 nvlist_free(newroot);
1519 * Check the replication level of the given vdevs and report any errors
1520 * found. We include the existing pool spec, if any, as we need to
1521 * catch changes against the existing replication level.
1523 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1524 nvlist_free(newroot);
1529 * Run through the vdev specification and label any whole disks found.
1531 if (!dryrun && make_disks(zhp, newroot) != 0) {
1532 nvlist_free(newroot);