2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Functions to convert between a list of vdevs and an nvlist representing the
2N/A * configuration. Each entry in the list can be one of:
2N/A *
2N/A * Device vdevs
2N/A * disk=(path=..., devid=...)
2N/A * file=(path=...)
2N/A *
2N/A * Group vdevs
2N/A * raidz[1|2]=(...)
2N/A * mirror=(...)
2N/A *
2N/A * Hot spares
2N/A *
2N/A * While the underlying implementation supports it, group vdevs cannot contain
2N/A * other group vdevs. All userland verification of devices is contained within
2N/A * this file. If successful, the nvlist returned can be passed directly to the
2N/A * kernel; we've done as much verification as possible in userland.
2N/A *
2N/A * Hot spares are a special case, and passed down as an array of disk vdevs, at
2N/A * the same level as the root of the vdev tree.
2N/A *
2N/A * Functions exported by this file:
2N/A * 'zpool_make_root_vdev' - the function performs several passes:
2N/A *
2N/A * 1. Construct the vdev specification. Performs syntax validation and
2N/A * makes sure each device is valid.
2N/A * 2. Check for devices in use. Using libdiskmgt, makes sure that no
2N/A * devices are also in use. Some can be overridden using the 'force'
2N/A * flag, others cannot.
2N/A * 3. Check for replication errors if the 'force' flag is not specified.
2N/A * validates that the replication level is consistent across the
2N/A * entire pool.
2N/A * 4. Label any whole disks with an EFI label.
2N/A *
2N/A * 'zpool_split_root_vdev' - the function performs several passes:
2N/A *
2N/A * 1. Construct the vdev specification. Performs syntax validation and
2N/A * makes sure each device is valid.
2N/A * 2. Check target device if its eligable for splitting.
2N/A * 3. Initiate the actual split.
2N/A */
2N/A
2N/A#include <assert.h>
2N/A#include <ctype.h>
2N/A#include <devid.h>
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <libdiskmgt.h>
2N/A#include <libdevinfo.h>
2N/A#include <libintl.h>
2N/A#include <libnvpair.h>
2N/A#include <limits.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A#include <sys/efi_partition.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/vtoc.h>
2N/A#include <sys/mntent.h>
2N/A#include <sys/paths.h>
2N/A
2N/A#include <libzfs_impl.h>
2N/A
2N/A#define DISK_ROOT _PATH_DEV "dsk"
2N/A#define RDISK_ROOT _PATH_DEV "rdsk"
2N/A#define BACKUP_SLICE "s2"
2N/A
2N/A/*
2N/A * For any given vdev specification, we can have multiple errors. The
2N/A * vdev_error() function keeps track of whether we have seen an error yet, and
2N/A * prints out a header if its the first error we've seen.
2N/A */
2N/A/*PRINTFLIKE4*/
2N/Astatic void
2N/Avdev_error(libzfs_handle_t *hdl, boolean_t force, int error,
2N/A const char *fmt, ...)
2N/A{
2N/A static boolean_t error_seen = B_FALSE;
2N/A char msg[1024];
2N/A va_list ap;
2N/A
2N/A if (!error_seen) {
2N/A error_seen = B_TRUE;
2N/A if (!force) {
2N/A (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2N/A "use -f to override the following errors:"));
2N/A } else {
2N/A (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2N/A "the following errors must be manually repaired:"));
2N/A }
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s"), msg);
2N/A (void) zfs_error(hdl, error, dgettext(TEXT_DOMAIN,
2N/A "vdev verification failed"));
2N/A }
2N/A va_start(ap, fmt);
2N/A (void) vfprintf(stderr, fmt, ap);
2N/A va_end(ap);
2N/A}
2N/A
2N/Astatic void
2N/Ainternal_error(libzfs_handle_t *hdl, int error)
2N/A{
2N/A /*
2N/A * ENXIO/ENODEV is a valid error message if the device doesn't live in
2N/A * /dev/dsk. Don't bother printing an error message in this case.
2N/A */
2N/A if (error == ENXIO || error == ENODEV)
2N/A return;
2N/A (void) zfs_error_fmt(hdl, EZFS_INVALCONFIG, dgettext(TEXT_DOMAIN,
2N/A "device in use checking failed: %s\n"), strerror(error));
2N/A}
2N/A
2N/A/*
2N/A * Validate a device, passing the bulk of the work off to libdiskmgt.
2N/A */
2N/Astatic int
2N/Acheck_slice(libzfs_handle_t *hdl, const char *path, boolean_t force,
2N/A boolean_t wholedisk, boolean_t isspare)
2N/A{
2N/A char *msg;
2N/A int error = 0;
2N/A dm_who_type_t who;
2N/A
2N/A if (force)
2N/A who = isspare ? DM_WHO_ZPOOL_SPARE_FORCE : DM_WHO_ZPOOL_FORCE;
2N/A else
2N/A who = isspare ? DM_WHO_ZPOOL_SPARE : DM_WHO_ZPOOL;
2N/A
2N/A if (dm_inuse((char *)path, &msg, who, &error) || error) {
2N/A if (error != 0) {
2N/A internal_error(hdl, error);
2N/A return (0);
2N/A }
2N/A vdev_error(hdl, force, EZFS_DEV_INUSE, "%s", msg);
2N/A free(msg);
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * If we're given a whole disk, ignore overlapping slices since we're
2N/A * about to label it anyway.
2N/A */
2N/A error = 0;
2N/A if (!wholedisk && !force &&
2N/A (dm_isoverlapping((char *)path, &msg, &error) || error)) {
2N/A if (error == 0) {
2N/A /* dm_isoverlapping returned -1 */
2N/A vdev_error(hdl, force, EZFS_DEV_INUSE,
2N/A gettext("%s overlaps with %s\n"), path, msg);
2N/A free(msg);
2N/A return (-1);
2N/A } else if (error != ENODEV) {
2N/A /* libdiskmgt's devcache only handles physical drives */
2N/A internal_error(hdl, error);
2N/A return (0);
2N/A }
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Validate a whole disk. Iterate over all slices on the disk and make sure
2N/A * that none is in use by calling check_slice().
2N/A */
2N/Astatic int
2N/Acheck_disk(libzfs_handle_t *hdl, const char *name, dm_descriptor_t disk,
2N/A boolean_t force, int isspare)
2N/A{
2N/A dm_descriptor_t *drive, *media, *slice;
2N/A int err = 0;
2N/A int i, ret;
2N/A
2N/A /*
2N/A * Get the drive associated with this disk. This should never fail,
2N/A * because we already have an alias handle open for the device.
2N/A */
2N/A if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE,
2N/A &err)) == NULL || *drive == NULL) {
2N/A if (err)
2N/A internal_error(hdl, err);
2N/A return (0);
2N/A }
2N/A
2N/A if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA,
2N/A &err)) == NULL) {
2N/A dm_free_descriptors(drive);
2N/A if (err)
2N/A internal_error(hdl, err);
2N/A return (0);
2N/A }
2N/A
2N/A dm_free_descriptors(drive);
2N/A
2N/A /*
2N/A * It is possible that the user has specified a removable media drive,
2N/A * and the media is not present.
2N/A */
2N/A if (*media == NULL) {
2N/A dm_free_descriptors(media);
2N/A vdev_error(hdl, force, EZFS_BADDEV,
2N/A gettext("'%s' has no media in drive\n"), name);
2N/A return (-1);
2N/A }
2N/A
2N/A if ((slice = dm_get_associated_descriptors(*media, DM_SLICE,
2N/A &err)) == NULL) {
2N/A dm_free_descriptors(media);
2N/A if (err)
2N/A internal_error(hdl, err);
2N/A return (0);
2N/A }
2N/A
2N/A dm_free_descriptors(media);
2N/A
2N/A ret = 0;
2N/A
2N/A /*
2N/A * Iterate over all slices and report any errors. We don't care about
2N/A * overlapping slices because we are using the whole disk.
2N/A */
2N/A for (i = 0; slice[i] != NULL; i++) {
2N/A char *name = dm_get_name(slice[i], &err);
2N/A
2N/A if (check_slice(hdl, name, force, B_TRUE, isspare) != 0)
2N/A ret = -1;
2N/A
2N/A dm_free_name(name);
2N/A }
2N/A
2N/A dm_free_descriptors(slice);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Validate a device.
2N/A */
2N/Astatic int
2N/Acheck_device(libzfs_handle_t *hdl, const char *path, boolean_t force,
2N/A boolean_t isspare)
2N/A{
2N/A dm_descriptor_t desc;
2N/A int err;
2N/A char *dev;
2N/A
2N/A /*
2N/A * For whole disks, libdiskmgt does not include the leading dev path.
2N/A */
2N/A dev = strrchr(path, '/');
2N/A assert(dev != NULL);
2N/A dev++;
2N/A if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err)) != NULL) {
2N/A err = check_disk(hdl, path, desc, force, isspare);
2N/A dm_free_descriptor(desc);
2N/A return (err);
2N/A }
2N/A
2N/A return (check_slice(hdl, path, force, B_FALSE, isspare));
2N/A}
2N/A
2N/A/*
2N/A * Check that a file is valid. All we can do in this case is check that it's
2N/A * not in use by another pool, and not in use by swap.
2N/A */
2N/Astatic int
2N/Acheck_file(libzfs_handle_t *hdl, const char *file, boolean_t force,
2N/A boolean_t isspare)
2N/A{
2N/A char *name;
2N/A int fd;
2N/A int ret = 0;
2N/A int err;
2N/A pool_state_t state;
2N/A boolean_t inuse;
2N/A
2N/A if (dm_inuse_swap(file, &err)) {
2N/A if (err) {
2N/A internal_error(hdl, err);
2N/A } else {
2N/A vdev_error(hdl, force, EZFS_DEV_INUSE,
2N/A gettext("%s is currently used "
2N/A "by swap. Please see swap(1M).\n"), file);
2N/A }
2N/A return (-1);
2N/A }
2N/A
2N/A if ((fd = open64(file, O_RDONLY)) < 0) {
2N/A (void) zfs_error_fmt(hdl, EZFS_OPENFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot open '%s': %s\n"),
2N/A file, strerror(errno));
2N/A return (-1);
2N/A }
2N/A
2N/A if (zpool_in_use(hdl, fd, &state, &name, &inuse) == 0 && inuse) {
2N/A const char *desc;
2N/A
2N/A switch (state) {
2N/A case POOL_STATE_ACTIVE:
2N/A desc = gettext("active");
2N/A break;
2N/A
2N/A case POOL_STATE_EXPORTED:
2N/A desc = gettext("exported");
2N/A break;
2N/A
2N/A case POOL_STATE_POTENTIALLY_ACTIVE:
2N/A desc = gettext("potentially active");
2N/A break;
2N/A
2N/A default:
2N/A desc = gettext("unknown");
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A * Allow hot spares to be shared between pools.
2N/A */
2N/A if (state == POOL_STATE_SPARE && isspare)
2N/A return (0);
2N/A
2N/A if (state == POOL_STATE_ACTIVE ||
2N/A state == POOL_STATE_SPARE || !force) {
2N/A switch (state) {
2N/A case POOL_STATE_SPARE:
2N/A vdev_error(hdl, force, EZFS_DEV_INUSE,
2N/A gettext("%s is reserved as a hot spare "
2N/A "for ZFS pool '%s'. Please see "
2N/A "zpool(1M)\n"), file, name);
2N/A break;
2N/A default:
2N/A vdev_error(hdl, force, EZFS_DEV_INUSE,
2N/A gettext("%s is part of %s ZFS pool "
2N/A "'%s'. Please see zpool(1M)\n"),
2N/A file, desc, name);
2N/A break;
2N/A }
2N/A ret = -1;
2N/A }
2N/A
2N/A free(name);
2N/A }
2N/A
2N/A (void) close(fd);
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * By "whole disk" we mean an entire physical disk (something we can
2N/A * label, toggle the write cache on, etc.) as opposed to the full
2N/A * capacity of a pseudo-device such as lofi or did. We act as if we
2N/A * are labeling the disk, which should be a pretty good test of whether
2N/A * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
2N/A * it isn't.
2N/A */
2N/Astatic boolean_t
2N/Ais_whole_disk(const char *arg)
2N/A{
2N/A struct dk_gpt *label;
2N/A int fd;
2N/A char path[MAXPATHLEN];
2N/A
2N/A (void) snprintf(path, sizeof (path), "%s%s%s",
2N/A RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE);
2N/A if ((fd = open(path, O_RDWR | O_NDELAY)) < 0)
2N/A return (B_FALSE);
2N/A if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
2N/A (void) close(fd);
2N/A return (B_FALSE);
2N/A }
2N/A efi_free(label);
2N/A (void) close(fd);
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Create a leaf vdev. Determine if this is a file or a device. If it's a
2N/A * device, fill in the device id to make a complete nvlist. Valid forms for a
2N/A * leaf vdev are:
2N/A *
2N/A * /dev/dsk/xxx Complete disk path
2N/A * /xxx Full path to file
2N/A * xxx Shorthand for /dev/dsk/xxx
2N/A * /dev/chassis/.../disk Symlink to whole-disk (xxx shorthand)
2N/A *
2N/A * It is sufficient to only record the failure cause as we fail immediately,
2N/A * and the caller will issue the entire error message with zfs_error().
2N/A */
2N/Astatic nvlist_t *
2N/Amake_leaf_vdev(libzfs_handle_t *hdl, const char *arg, uint64_t is_log)
2N/A{
2N/A char *lvdev = NULL;
2N/A struct stat64 statbuf;
2N/A char *rp;
2N/A char path[MAXPATHLEN];
2N/A nvlist_t *vdev = NULL;
2N/A char *type = NULL;
2N/A boolean_t wholedisk = B_FALSE;
2N/A char *tmp;
2N/A
2N/A /*
2N/A * If 'arg' is a symlink, read the link and try to convert it into
2N/A * a whole-disk ctd name. This is done to support references to
2N/A * /dev/chassis/.../disk, where the link value resolves to the
2N/A * /devices path. If the di_cro interfaces can convert the link value
2N/A * into a disk ctd name, then we proceed with that ctd name.
2N/A *
2N/A * NOTE: To insulate us from new cro fields, we use the string-based
2N/A * query interface.
2N/A */
2N/A if (libzfs_get_cro_hdl(hdl) && (lstat64(arg, &statbuf) == 0) &&
2N/A S_ISLNK(statbuf.st_mode) &&
2N/A (rp = realpath(arg, NULL))) {
2N/A di_cro_reca_t ra;
2N/A di_cro_rec_t r;
2N/A int query_len;
2N/A char *query;
2N/A static const char *query_fmt =
2N/A DI_CRO_Q_OCCUPANT_TYPE DI_CRO_QREFMT ","
2N/A DI_CRO_Q_OCCUPANT_DEVICES DI_CRO_QREFMT;
2N/A
2N/A query_len = strlen(query_fmt) +
2N/A strlen("disk") + strlen(rp) + 1;
2N/A if ((query = zfs_alloc(hdl, query_len)) == NULL)
2N/A goto noloc;
2N/A (void) snprintf(query, query_len, query_fmt, "disk", rp);
2N/A ra = di_cro_reca_create_query((di_cro_hdl_t)
2N/A libzfs_get_cro_hdl(hdl), 0, query);
2N/A r = di_cro_reca_next(ra, NULL);
2N/A lvdev = di_cro_rec_fgeti_occupant_compdev(r, 0, NULL, NULL);
2N/A di_cro_reca_destroy(ra);
2N/A free(query);
2N/Anoloc: free(rp);
2N/A }
2N/A
2N/A if (lvdev == NULL)
2N/A lvdev = (char *)arg;
2N/A
2N/A /*
2N/A * Determine what type of vdev this is, and put the full path into
2N/A * 'path'. We detect whether this is a device of file afterwards by
2N/A * checking the st_mode of the file.
2N/A */
2N/A if (lvdev[0] == '/') {
2N/A /*
2N/A * Complete device or file path. Exact type is determined by
2N/A * examining the file descriptor afterwards.
2N/A */
2N/A wholedisk = is_whole_disk(lvdev);
2N/A if (!wholedisk && (stat64(lvdev, &statbuf) != 0)) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "cannot open '%s': %s"), arg, strerror(errno));
2N/A return (NULL);
2N/A }
2N/A
2N/A (void) strlcpy(path, lvdev, sizeof (path));
2N/A } else {
2N/A /*
2N/A * This may be a short path for a device, or it could be total
2N/A * gibberish. Check to see if it's a known device in
2N/A * /dev/dsk/. As part of this check, see if we've been given a
2N/A * an entire disk (minus the slice number).
2N/A */
2N/A (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT,
2N/A lvdev);
2N/A wholedisk = is_whole_disk(path);
2N/A if (!wholedisk && (stat64(path, &statbuf) != 0)) {
2N/A /*
2N/A * If we got ENOENT, then the user gave us
2N/A * gibberish, so try to direct them with a
2N/A * reasonable error message. Otherwise,
2N/A * regurgitate strerror() since it's the best we
2N/A * can do.
2N/A */
2N/A if (errno == ENOENT) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "cannot open '%s': no such device in %s\n"
2N/A "must be a full path or shorthand device "
2N/A "name"), arg, DISK_ROOT);
2N/A } else {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "cannot open '%s': %s"),
2N/A path, strerror(errno));
2N/A }
2N/A return (NULL);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Determine whether this is a device or a file.
2N/A */
2N/A if (wholedisk || S_ISBLK(statbuf.st_mode)) {
2N/A type = VDEV_TYPE_DISK;
2N/A } else if (S_ISREG(statbuf.st_mode)) {
2N/A type = VDEV_TYPE_FILE;
2N/A } else {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot use '%s': "
2N/A "must be a block device or regular file"), path);
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Finally, we have the complete device or file, and we know that it is
2N/A * acceptable to use. Construct the nvlist to describe this vdev. All
2N/A * vdevs have a 'path' element, and devices also have a 'devid' element.
2N/A */
2N/A verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
2N/A verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
2N/A verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
2N/A verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
2N/A if (strcmp(type, VDEV_TYPE_DISK) == 0)
2N/A verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
2N/A (uint64_t)wholedisk) == 0);
2N/A
2N/A /*
2N/A * Use di_cro interfaces to get /dev/chassis path.
2N/A */
2N/A if ((tmp = zfs_alloc(hdl, strlen(path) + 1)) != NULL) {
2N/A if (strncmp(path, "/dev/dsk/", 9) == 0)
2N/A (void) strcpy(tmp, path + 9);
2N/A else
2N/A (void) strcpy(tmp, path);
2N/A if (tmp[0] == 'c' && isdigit(tmp[1])) {
2N/A di_cro_reca_t ra;
2N/A di_cro_rec_t r;
2N/A int query_len;
2N/A char *query, *slice, *dp = NULL;
2N/A static const char *query_fmt =
2N/A DI_CRO_Q_OCCUPANT_TYPE DI_CRO_QREFMT ","
2N/A DI_CRO_Q_OCCUPANT_COMPDEV DI_CRO_QREFMT;
2N/A
2N/A if ((slice = strchr(tmp, 's')) != NULL)
2N/A *slice = '\0';
2N/A query_len = strlen(query_fmt) + strlen("disk") +
2N/A strlen(tmp) + 1;
2N/A if ((query = zfs_alloc(hdl, query_len)) != NULL) {
2N/A (void) snprintf(query, query_len, query_fmt,
2N/A "disk", tmp);
2N/A ra = di_cro_reca_create_query((di_cro_hdl_t)
2N/A libzfs_get_cro_hdl(hdl), 0, query);
2N/A r = di_cro_reca_next(ra, NULL);
2N/A if ((dp = di_cro_rec_fgeti_devchassis_path(r, 0,
2N/A NULL, NULL)) != NULL)
2N/A verify(nvlist_add_string(vdev,
2N/A ZPOOL_CONFIG_DEVCHASSIS, dp) == 0);
2N/A if ((dp = di_cro_rec_fgeti_chassis_id(r, 0,
2N/A NULL, NULL)) != NULL)
2N/A verify(nvlist_add_string(vdev,
2N/A ZPOOL_CONFIG_CHASSISSN, dp) == 0);
2N/A if ((dp = di_cro_rec_fgeti_receptacle_name(r, 0,
2N/A NULL, NULL)) != NULL)
2N/A verify(nvlist_add_string(vdev,
2N/A ZPOOL_CONFIG_LOCATION, dp) == 0);
2N/A di_cro_reca_destroy(ra);
2N/A free(query);
2N/A }
2N/A }
2N/A free(tmp);
2N/A }
2N/A
2N/A /*
2N/A * For a whole disk, defer getting its devid until after labeling it.
2N/A */
2N/A if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
2N/A /*
2N/A * Get the devid for the device.
2N/A */
2N/A int fd;
2N/A ddi_devid_t devid;
2N/A char *minor = NULL, *devid_str = NULL;
2N/A
2N/A if ((fd = open(path, O_RDONLY)) < 0) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "cannot open '%s': %s"), path, strerror(errno));
2N/A nvlist_free(vdev);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (devid_get(fd, &devid) == 0) {
2N/A if (devid_get_minor_name(fd, &minor) == 0 &&
2N/A (devid_str = devid_str_encode(devid, minor)) !=
2N/A NULL) {
2N/A verify(nvlist_add_string(vdev,
2N/A ZPOOL_CONFIG_DEVID, devid_str) == 0);
2N/A }
2N/A if (devid_str != NULL)
2N/A devid_str_free(devid_str);
2N/A if (minor != NULL)
2N/A devid_str_free(minor);
2N/A devid_free(devid);
2N/A }
2N/A
2N/A (void) close(fd);
2N/A }
2N/A
2N/A return (vdev);
2N/A}
2N/A
2N/A/*
2N/A * Go through and verify the replication level of the pool is consistent.
2N/A * Performs the following checks:
2N/A *
2N/A * For the new spec, verifies that devices in mirrors and raidz are the
2N/A * same size.
2N/A *
2N/A * If the current configuration already has inconsistent replication
2N/A * levels, ignore any other potential problems in the new spec.
2N/A *
2N/A * Otherwise, make sure that the current spec (if there is one) and the new
2N/A * spec have consistent replication levels.
2N/A */
2N/Atypedef struct replication_level {
2N/A const char *zprl_type;
2N/A uint64_t zprl_children;
2N/A uint64_t zprl_parity;
2N/A} replication_level_t;
2N/A
2N/A#define ZPOOL_FUZZ (16 * 1024 * 1024)
2N/A
2N/A/*
2N/A * Check one top-level vdev and its children to ensure we don't mix devices
2N/A * and files, and that the leaf vdevs are of approximately the same size.
2N/A */
2N/Astatic boolean_t
2N/Acheck_tvdevs(libzfs_handle_t *hdl, nvlist_t **child, uint_t children,
2N/A replication_level_t *rep, boolean_t fatal, boolean_t force)
2N/A{
2N/A uint64_t vdev_size;
2N/A boolean_t reported;
2N/A const char *type;
2N/A int c;
2N/A /*
2N/A * This is a mirror or RAID-Z vdev. Go through and make
2N/A * sure the contents are all the same (files vs. disks),
2N/A * keeping track of the number of elements in the process.
2N/A *
2N/A * We also check that the size of each vdev (if it can
2N/A * be determined) is the same.
2N/A *
2N/A * The 'reported' variable indicates that we've
2N/A * already reported an error for this spec, so don't
2N/A * bother doing it again.
2N/A */
2N/A type = NULL;
2N/A reported = B_FALSE;
2N/A vdev_size = -1ULL;
2N/A rep->zprl_children = 0;
2N/A
2N/A for (c = 0; c < children; c++) {
2N/A nvlist_t *cnv = child[c];
2N/A char *path, *childtype;
2N/A struct stat64 statbuf;
2N/A uint64_t size = -1ULL;
2N/A int fd, err;
2N/A
2N/A rep->zprl_children++;
2N/A
2N/A verify(nvlist_lookup_string(cnv,
2N/A ZPOOL_CONFIG_TYPE, &childtype) == 0);
2N/A
2N/A /*
2N/A * If this is a replacing or spare vdev, then
2N/A * get the real first child of the vdev.
2N/A */
2N/A while (strcmp(childtype,
2N/A VDEV_TYPE_REPLACING) == 0 ||
2N/A strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
2N/A nvlist_t **rchild;
2N/A uint_t rchildren;
2N/A
2N/A verify(nvlist_lookup_nvlist_array(cnv,
2N/A ZPOOL_CONFIG_CHILDREN, &rchild,
2N/A &rchildren) == 0);
2N/A assert(rchildren > 1);
2N/A cnv = rchild[0];
2N/A
2N/A verify(nvlist_lookup_string(cnv,
2N/A ZPOOL_CONFIG_TYPE,
2N/A &childtype) == 0);
2N/A }
2N/A
2N/A verify(nvlist_lookup_string(cnv,
2N/A ZPOOL_CONFIG_PATH, &path) == 0);
2N/A
2N/A /*
2N/A * If we have a raidz/mirror that combines disks
2N/A * with files, report it as an error.
2N/A */
2N/A if (!reported && type != NULL && strcmp(type, childtype) != 0) {
2N/A if (!fatal)
2N/A return (B_FALSE);
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "mismatched replication level: %s contains both "
2N/A "files and devices\n"), rep->zprl_type);
2N/A reported = B_TRUE;
2N/A }
2N/A type = childtype;
2N/A
2N/A /*
2N/A * According to stat(2), the value of 'st_size' is undefined
2N/A * for block devices and character devices. But there is no
2N/A * effective way to determine the real size in userland.
2N/A *
2N/A * Instead, we'll take advantage of an implementation detail of
2N/A * spec_size(). If the device is currently open, then we
2N/A * (should) return a valid size.
2N/A *
2N/A * If we still don't get a valid size (indicated by a size of 0
2N/A * or MAXOFFSET_T), then ignore this device altogether.
2N/A */
2N/A if ((fd = open(path, O_RDONLY)) >= 0) {
2N/A err = fstat64(fd, &statbuf);
2N/A (void) close(fd);
2N/A } else {
2N/A err = stat64(path, &statbuf);
2N/A }
2N/A
2N/A if (err != 0 ||
2N/A statbuf.st_size == 0 ||
2N/A statbuf.st_size == MAXOFFSET_T)
2N/A continue;
2N/A
2N/A size = statbuf.st_size;
2N/A
2N/A /*
2N/A * Also make sure that devices and slices have a consistent
2N/A * size. If they differ by a significant amount then report
2N/A * an error.
2N/A */
2N/A if (!reported &&
2N/A (vdev_size != -1ULL &&
2N/A (labs(size - vdev_size) > ZPOOL_FUZZ))) {
2N/A if (!fatal)
2N/A return (B_FALSE);
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "%s contains devices of different sizes\n"),
2N/A rep->zprl_type);
2N/A reported = B_TRUE;
2N/A }
2N/A
2N/A vdev_size = size;
2N/A }
2N/A
2N/A /* success if we haven't reported an error */
2N/A return (!reported);
2N/A}
2N/A
2N/A/*
2N/A * Check the replication levels in the existing pool's config and report
2N/A * an error if there is a mismatch.
2N/A */
2N/Astatic boolean_t
2N/Acompare_rep_levels(libzfs_handle_t *hdl, replication_level_t *lastrep,
2N/A replication_level_t *rep, boolean_t fatal, boolean_t force)
2N/A{
2N/A if (lastrep->zprl_type == NULL)
2N/A return (B_TRUE);
2N/A if (strcmp(lastrep->zprl_type, rep->zprl_type) != 0) {
2N/A if (fatal) {
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "mismatched replication level: both %s "
2N/A "and %s vdevs are present\n"),
2N/A lastrep->zprl_type, rep->zprl_type);
2N/A }
2N/A } else if (lastrep->zprl_parity != rep->zprl_parity) {
2N/A if (fatal) {
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "mismatched replication level: both %llu "
2N/A "and %llu and %llu device parity %s "
2N/A "vdevs are present\n"),
2N/A lastrep->zprl_parity,
2N/A rep->zprl_parity, rep->zprl_type);
2N/A }
2N/A } else if (lastrep->zprl_children != rep->zprl_children) {
2N/A if (fatal) {
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "mismatched replication level: both "
2N/A "%llu-way and %llu-way %s vdevs are "
2N/A "present\n"),
2N/A lastrep->zprl_children,
2N/A rep->zprl_children, rep->zprl_type);
2N/A }
2N/A } else {
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Check the replication levels between the existing config and the
2N/A * config that the user wants to have. This differs from compare_rep_levels()
2N/A * only in the strings we output.
2N/A */
2N/Astatic boolean_t
2N/Acheck_new_levels(libzfs_handle_t *hdl, replication_level_t *current,
2N/A replication_level_t *new, boolean_t force)
2N/A{
2N/A if (current->zprl_type == NULL || new->zprl_type == NULL)
2N/A return (B_TRUE);
2N/A if (strcmp(current->zprl_type, new->zprl_type) != 0) {
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "mismatched replication level: pool uses %s and new "
2N/A "vdev is %s\n"),
2N/A current->zprl_type, new->zprl_type);
2N/A } else if (current->zprl_parity != new->zprl_parity) {
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "mismatched replication level: pool uses %llu device "
2N/A "parity and new vdev uses %llu\n"),
2N/A current->zprl_parity, new->zprl_parity);
2N/A } else if (current->zprl_children != new->zprl_children) {
2N/A vdev_error(hdl, force, EZFS_INVALCONFIG, gettext(
2N/A "mismatched replication level: pool uses %llu-way %s "
2N/A "and new vdev uses %llu-way %s\n"),
2N/A current->zprl_children, current->zprl_type,
2N/A new->zprl_children, new->zprl_type);
2N/A } else {
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Given a list of toplevel vdevs, determine if the configuration is
2N/A * self-consistent. If the config is inconsistent, return false. If 'fatal' is
2N/A * set, then an error message will be displayed for each self-inconsistent
2N/A * vdev.
2N/A */
2N/Astatic boolean_t
2N/Avalid_replication(libzfs_handle_t *hdl, nvlist_t *nvroot,
2N/A replication_level_t *pool, replication_level_t *slog, boolean_t fatal,
2N/A boolean_t force)
2N/A{
2N/A nvlist_t **top;
2N/A uint_t t, toplevels;
2N/A nvlist_t **child;
2N/A uint_t children;
2N/A nvlist_t *nv;
2N/A char *type;
2N/A replication_level_t *prev, rep;
2N/A boolean_t ret = B_TRUE;
2N/A
2N/A pool->zprl_type = NULL;
2N/A slog->zprl_type = NULL;
2N/A
2N/A verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2N/A &top, &toplevels) == 0);
2N/A
2N/A for (t = 0; t < toplevels; t++) {
2N/A uint64_t is_log = B_FALSE;
2N/A
2N/A nv = top[t];
2N/A
2N/A /*
2N/A * For separate logs we ignore the top level vdev replication
2N/A * constraints.
2N/A */
2N/A (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
2N/A
2N/A verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
2N/A &type) == 0);
2N/A rep.zprl_type = type;
2N/A rep.zprl_parity = 0;
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2N/A &child, &children) != 0) {
2N/A /*
2N/A * This is a 'file' or 'disk' vdev.
2N/A */
2N/A rep.zprl_children = 1;
2N/A } else {
2N/A /*
2N/A * If this is a raidz device, update the parity.
2N/A */
2N/A if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
2N/A verify(nvlist_lookup_uint64(nv,
2N/A ZPOOL_CONFIG_NPARITY,
2N/A &rep.zprl_parity) == 0);
2N/A assert(rep.zprl_parity != 0);
2N/A }
2N/A
2N/A if (!check_tvdevs(hdl, child, children, &rep, fatal,
2N/A force)) {
2N/A if (!fatal)
2N/A return (B_FALSE);
2N/A ret = B_FALSE;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * At this point, we have the replication of the last toplevel
2N/A * vdev in 'rep'. Compare it to the pool or the slog.
2N/A */
2N/A prev = is_log ? slog : pool;
2N/A if (!compare_rep_levels(hdl, prev, &rep, fatal, force)) {
2N/A if (!fatal)
2N/A return (B_FALSE);
2N/A ret = B_FALSE;
2N/A }
2N/A *prev = rep;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Check the replication level of the vdev spec against the current pool. Calls
2N/A * valid_replication() to make sure the new spec is self-consistent. If the
2N/A * pool has a consistent replication level, then we ignore any errors.
2N/A * Otherwise, report any difference between the two.
2N/A */
2N/Astatic int
2N/Acheck_replication(libzfs_handle_t *hdl, nvlist_t *config, nvlist_t *newroot,
2N/A boolean_t force)
2N/A{
2N/A nvlist_t **child;
2N/A uint_t children;
2N/A replication_level_t p_curr = {0}, p_new; /* pool info */
2N/A replication_level_t s_curr = {0}, s_new; /* slog info */
2N/A
2N/A /*
2N/A * If we have a current pool configuration, check to see if it's
2N/A * self-consistent. If not, simply return success.
2N/A */
2N/A if (config != NULL) {
2N/A nvlist_t *nvroot;
2N/A
2N/A verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2N/A &nvroot) == 0);
2N/A if (!valid_replication(hdl, nvroot, &p_curr, &s_curr, B_FALSE,
2N/A force))
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * for spares there may be no children, and therefore no
2N/A * replication level to check
2N/A */
2N/A if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
2N/A &child, &children) != 0) || (children == 0))
2N/A return (0);
2N/A
2N/A /*
2N/A * Get the replication level of the new vdev spec, reporting any
2N/A * inconsistencies found.
2N/A */
2N/A if (!valid_replication(hdl, newroot, &p_new, &s_new, B_TRUE, force))
2N/A return (-1);
2N/A
2N/A /*
2N/A * Check to see if the new vdev spec matches the replication level of
2N/A * the current pool.
2N/A */
2N/A if (!check_new_levels(hdl, &p_curr, &p_new, force) ||
2N/A !check_new_levels(hdl, &s_curr, &s_new, force))
2N/A return (-1);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Go through and find any whole disks in the vdev specification, labelling them
2N/A * as appropriate. When constructing the vdev spec, we were unable to open this
2N/A * device in order to provide a devid. Now that we have labelled the disk and
2N/A * know that slice 0 is valid, we can construct the devid now.
2N/A *
2N/A * If the disk was already labeled with an EFI label, we will have gotten the
2N/A * devid already (because we were able to open the whole disk). Otherwise, we
2N/A * need to get the devid after we label the disk.
2N/A */
2N/Astatic int
2N/Amake_disks(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
2N/A int create_req_part)
2N/A{
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A char *type, *path, *diskname;
2N/A char buf[MAXPATHLEN];
2N/A uint64_t wholedisk;
2N/A int fd;
2N/A int ret;
2N/A ddi_devid_t devid;
2N/A char *minor = NULL, *devid_str = NULL;
2N/A int slicenum;
2N/A
2N/A verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2N/A
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2N/A &child, &children) != 0) {
2N/A
2N/A if (strcmp(type, VDEV_TYPE_DISK) != 0)
2N/A return (0);
2N/A
2N/A /*
2N/A * We have a disk device. Get the path to the device
2N/A * and see if it's a whole disk by appending the backup
2N/A * slice and stat()ing the device.
2N/A */
2N/A verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2N/A if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2N/A &wholedisk) != 0 || !wholedisk)
2N/A return (0);
2N/A
2N/A diskname = strrchr(path, '/');
2N/A assert(diskname != NULL);
2N/A diskname++;
2N/A
2N/A if (zpool_label_disk(hdl, zhp, diskname, create_req_part,
2N/A &slicenum) == -1)
2N/A return (-1);
2N/A
2N/A /*
2N/A * Fill in the devid, now that we've labeled the disk.
2N/A */
2N/A (void) snprintf(buf, sizeof (buf), "%ss%d", path, slicenum);
2N/A if ((fd = open(buf, O_RDONLY)) < 0) {
2N/A (void) zfs_error_fmt(hdl, EZFS_OPENFAILED,
2N/A dgettext(TEXT_DOMAIN, "cannot open '%s': %s\n"),
2N/A buf, strerror(errno));
2N/A return (-1);
2N/A }
2N/A
2N/A if (devid_get(fd, &devid) == 0) {
2N/A if (devid_get_minor_name(fd, &minor) == 0 &&
2N/A (devid_str = devid_str_encode(devid, minor)) !=
2N/A NULL) {
2N/A verify(nvlist_add_string(nv,
2N/A ZPOOL_CONFIG_DEVID, devid_str) == 0);
2N/A }
2N/A if (devid_str != NULL)
2N/A devid_str_free(devid_str);
2N/A if (minor != NULL)
2N/A devid_str_free(minor);
2N/A devid_free(devid);
2N/A }
2N/A
2N/A /*
2N/A * Update the path to refer to the slice. The presence of
2N/A * the 'whole_disk' field indicates to the CLI that we should
2N/A * chop off the slice number when displaying the device in
2N/A * future output.
2N/A */
2N/A verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0);
2N/A (void) close(fd);
2N/A
2N/A return (0);
2N/A }
2N/A
2N/A for (c = 0; c < children; c++) {
2N/A if ((ret = make_disks(hdl, zhp, child[c],
2N/A create_req_part)) != 0)
2N/A return (ret);
2N/A }
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2N/A &child, &children) == 0) {
2N/A for (c = 0; c < children; c++) {
2N/A if ((ret = make_disks(hdl, zhp, child[c],
2N/A create_req_part)) != 0)
2N/A return (ret);
2N/A }
2N/A }
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2N/A &child, &children) == 0) {
2N/A for (c = 0; c < children; c++) {
2N/A if ((ret = make_disks(hdl, zhp, child[c],
2N/A create_req_part)) != 0)
2N/A return (ret);
2N/A }
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Determine if the given path is a hot spare within the given configuration.
2N/A */
2N/Astatic boolean_t
2N/Ais_spare(libzfs_handle_t *hdl, nvlist_t *config, const char *path)
2N/A{
2N/A int fd;
2N/A pool_state_t state;
2N/A char *name = NULL;
2N/A nvlist_t *label;
2N/A uint64_t guid, spareguid;
2N/A nvlist_t *nvroot;
2N/A nvlist_t **spares;
2N/A uint_t i, nspares;
2N/A boolean_t inuse;
2N/A
2N/A if ((fd = open(path, O_RDONLY)) < 0)
2N/A return (B_FALSE);
2N/A
2N/A if (zpool_in_use(hdl, fd, &state, &name, &inuse) != 0 ||
2N/A !inuse ||
2N/A state != POOL_STATE_SPARE ||
2N/A zpool_read_label(fd, &label) != 0) {
2N/A free(name);
2N/A (void) close(fd);
2N/A return (B_FALSE);
2N/A }
2N/A free(name);
2N/A (void) close(fd);
2N/A
2N/A verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
2N/A nvlist_free(label);
2N/A
2N/A verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2N/A &nvroot) == 0);
2N/A if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2N/A &spares, &nspares) == 0) {
2N/A for (i = 0; i < nspares; i++) {
2N/A verify(nvlist_lookup_uint64(spares[i],
2N/A ZPOOL_CONFIG_GUID, &spareguid) == 0);
2N/A if (spareguid == guid)
2N/A return (B_TRUE);
2N/A }
2N/A }
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Go through and find any devices that are in use. We rely on libdiskmgt for
2N/A * the majority of this task.
2N/A */
2N/Astatic int
2N/Acheck_in_use(libzfs_handle_t *hdl, nvlist_t *config, nvlist_t *nv,
2N/A boolean_t force, boolean_t replacing, boolean_t isspare)
2N/A{
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A int ret;
2N/A
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2N/A &child, &children) != 0) {
2N/A char buf[MAXPATHLEN];
2N/A char *type, *path;
2N/A uint64_t wholedisk;
2N/A
2N/A verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2N/A verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2N/A /*
2N/A * As a generic check, we look to see if this is a replace of a
2N/A * hot spare within the same pool. If so, we allow it
2N/A * regardless of what libdiskmgt or zpool_in_use() says.
2N/A */
2N/A if (replacing) {
2N/A if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2N/A &wholedisk) == 0 && wholedisk) {
2N/A
2N/A (void) snprintf(buf, sizeof (buf),
2N/A "%ss%d", path, 0);
2N/A
2N/A if (is_spare(hdl, config, buf)) {
2N/A return (0);
2N/A } else {
2N/A (void) snprintf(buf,
2N/A sizeof (buf),
2N/A "%ss%d", path, 1);
2N/A if (is_spare(hdl, config, buf))
2N/A return (0);
2N/A }
2N/A
2N/A } else {
2N/A (void) strlcpy(buf, path, sizeof (buf));
2N/A
2N/A if (is_spare(hdl, config, buf))
2N/A return (0);
2N/A }
2N/A }
2N/A
2N/A if (strcmp(type, VDEV_TYPE_DISK) == 0)
2N/A ret = check_device(hdl, path, force, isspare);
2N/A
2N/A if (strcmp(type, VDEV_TYPE_FILE) == 0)
2N/A ret = check_file(hdl, path, force, isspare);
2N/A
2N/A return (ret);
2N/A }
2N/A
2N/A for (c = 0; c < children; c++) {
2N/A if ((ret = check_in_use(hdl, config, child[c], force,
2N/A replacing, B_FALSE)) != 0)
2N/A return (ret);
2N/A }
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2N/A &child, &children) == 0) {
2N/A for (c = 0; c < children; c++) {
2N/A if ((ret = check_in_use(hdl, config, child[c], force,
2N/A replacing, B_TRUE)) != 0)
2N/A return (ret);
2N/A }
2N/A }
2N/A if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2N/A &child, &children) == 0) {
2N/A for (c = 0; c < children; c++) {
2N/A if ((ret = check_in_use(hdl, config, child[c], force,
2N/A replacing, B_FALSE)) != 0)
2N/A return (ret);
2N/A }
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Astatic const char *
2N/Ais_grouping(const char *type, int *mindev, int *maxdev)
2N/A{
2N/A if (strncmp(type, "raidz", 5) == 0) {
2N/A const char *p = type + 5;
2N/A char *end;
2N/A long nparity;
2N/A
2N/A if (*p == '\0') {
2N/A nparity = 1;
2N/A } else if (*p == '0') {
2N/A return (NULL); /* no zero prefixes allowed */
2N/A } else {
2N/A errno = 0;
2N/A nparity = strtol(p, &end, 10);
2N/A if (errno != 0 || nparity < 1 || nparity >= 255 ||
2N/A *end != '\0')
2N/A return (NULL);
2N/A }
2N/A
2N/A if (mindev != NULL)
2N/A *mindev = nparity + 1;
2N/A if (maxdev != NULL)
2N/A *maxdev = 255;
2N/A return (VDEV_TYPE_RAIDZ);
2N/A }
2N/A
2N/A if (maxdev != NULL)
2N/A *maxdev = INT_MAX;
2N/A
2N/A if (strcmp(type, "mirror") == 0) {
2N/A if (mindev != NULL)
2N/A *mindev = 2;
2N/A return (VDEV_TYPE_MIRROR);
2N/A }
2N/A
2N/A if (strcmp(type, "spare") == 0) {
2N/A if (mindev != NULL)
2N/A *mindev = 1;
2N/A return (VDEV_TYPE_SPARE);
2N/A }
2N/A
2N/A if (strcmp(type, "log") == 0) {
2N/A if (mindev != NULL)
2N/A *mindev = 1;
2N/A return (VDEV_TYPE_LOG);
2N/A }
2N/A
2N/A if (strcmp(type, "cache") == 0) {
2N/A if (mindev != NULL)
2N/A *mindev = 1;
2N/A return (VDEV_TYPE_L2CACHE);
2N/A }
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Construct a syntactically valid vdev specification,
2N/A * and ensure that all devices and files exist and can be opened.
2N/A * It is sufficient to only record the failure cause as we fail immediately,
2N/A * and the caller will issue the entire error message with zfs_error().
2N/A */
2N/Astatic nvlist_t *
2N/Aconstruct_spec(libzfs_handle_t *hdl, int vdevcount, char **vdevlist)
2N/A{
2N/A nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
2N/A int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
2N/A const char *type;
2N/A uint64_t is_log;
2N/A boolean_t seen_logs;
2N/A char msg[1024];
2N/A
2N/A nvroot = NULL;
2N/A top = NULL;
2N/A toplevels = 0;
2N/A spares = NULL;
2N/A l2cache = NULL;
2N/A nspares = 0;
2N/A nlogs = 0;
2N/A nl2cache = 0;
2N/A is_log = B_FALSE;
2N/A seen_logs = B_FALSE;
2N/A (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2N/A "invalid vdev specification: "));
2N/A
2N/A while (vdevcount > 0) {
2N/A nv = NULL;
2N/A /*
2N/A * If it's a mirror or raidz, the subsequent arguments are
2N/A * its leaves -- until we encounter the next mirror or raidz.
2N/A */
2N/A if ((type = is_grouping(vdevlist[0], &mindev, &maxdev))
2N/A != NULL) {
2N/A nvlist_t **child = NULL;
2N/A int c, children = 0;
2N/A
2N/A if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
2N/A if (spares != NULL) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%s'%s' can be specified only "
2N/A "once"), msg, VDEV_TYPE_SPARE);
2N/A goto errout;
2N/A }
2N/A is_log = B_FALSE;
2N/A }
2N/A if (strcmp(type, VDEV_TYPE_LOG) == 0) {
2N/A if (seen_logs) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%s'%s' can be specified only "
2N/A "once"), msg, VDEV_TYPE_LOG);
2N/A goto errout;
2N/A }
2N/A seen_logs = B_TRUE;
2N/A is_log = B_TRUE;
2N/A vdevcount--;
2N/A vdevlist++;
2N/A /*
2N/A * A log is not a real grouping device.
2N/A * We just set is_log and continue.
2N/A */
2N/A continue;
2N/A }
2N/A if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
2N/A if (l2cache != NULL) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%s'%s' can be specified only "
2N/A "once"), msg, VDEV_TYPE_L2CACHE);
2N/A goto errout;
2N/A }
2N/A is_log = B_FALSE;
2N/A }
2N/A if (is_log) {
2N/A if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%sunsupported '%s' device: %s"),
2N/A msg, VDEV_TYPE_LOG, type);
2N/A goto errout;
2N/A }
2N/A nlogs++;
2N/A }
2N/A for (c = 1; c < vdevcount; c++) {
2N/A if (is_grouping(vdevlist[c], NULL, NULL)
2N/A != NULL)
2N/A break;
2N/A child = zfs_realloc(hdl, child, children *
2N/A sizeof (nvlist_t *), (children + 1) *
2N/A sizeof (nvlist_t *));
2N/A if ((nv = make_leaf_vdev(hdl, vdevlist[c],
2N/A B_FALSE)) == NULL) {
2N/A for (int i = 0; i < children; i++)
2N/A nvlist_free(child[i]);
2N/A free(child);
2N/A goto errout;
2N/A }
2N/A child[children] = nv;
2N/A children++;
2N/A }
2N/A if (children < mindev) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%s%s requires at least %d devices"),
2N/A msg, vdevlist[0], mindev);
2N/A if (child) {
2N/A for (int i = 0; i < children; i++)
2N/A nvlist_free(child[i]);
2N/A free(child);
2N/A }
2N/A goto errout;
2N/A }
2N/A if (children > maxdev) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%s%s supports no more then %d devices"),
2N/A msg, vdevlist[0], maxdev);
2N/A if (child) {
2N/A for (int i = 0; i < children; i++)
2N/A nvlist_free(child[i]);
2N/A free(child);
2N/A }
2N/A goto errout;
2N/A }
2N/A
2N/A vdevcount -= c;
2N/A vdevlist += c;
2N/A
2N/A if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
2N/A spares = child;
2N/A nspares = children;
2N/A continue;
2N/A } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
2N/A l2cache = child;
2N/A nl2cache = children;
2N/A continue;
2N/A } else {
2N/A verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
2N/A 0) == 0);
2N/A verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
2N/A type) == 0);
2N/A verify(nvlist_add_uint64(nv,
2N/A ZPOOL_CONFIG_IS_LOG, is_log) == 0);
2N/A if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
2N/A verify(nvlist_add_uint64(nv,
2N/A ZPOOL_CONFIG_NPARITY,
2N/A mindev - 1) == 0);
2N/A }
2N/A verify(nvlist_add_nvlist_array(nv,
2N/A ZPOOL_CONFIG_CHILDREN, child,
2N/A children) == 0);
2N/A
2N/A for (c = 0; c < children; c++)
2N/A nvlist_free(child[c]);
2N/A free(child);
2N/A }
2N/A } else {
2N/A /*
2N/A * We have a device. Pass off to make_leaf_vdev() to
2N/A * construct the appropriate nvlist describing the vdev.
2N/A */
2N/A if ((nv = make_leaf_vdev(hdl, vdevlist[0], is_log))
2N/A == NULL)
2N/A goto errout;
2N/A if (is_log)
2N/A nlogs++;
2N/A vdevcount--;
2N/A vdevlist++;
2N/A }
2N/A
2N/A top = zfs_realloc(hdl, top, toplevels * sizeof (nvlist_t *),
2N/A (toplevels + 1) * sizeof (nvlist_t *));
2N/A top[toplevels] = nv;
2N/A toplevels++;
2N/A }
2N/A
2N/A if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "%sat least one toplevel vdev must be specified"), msg);
2N/A goto errout;
2N/A }
2N/A
2N/A if (seen_logs && nlogs == 0) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s'%s' "
2N/A "requires at least 1 device"), msg, VDEV_TYPE_LOG);
2N/A goto errout;
2N/A }
2N/A
2N/A /*
2N/A * Finally, create nvroot and add all top-level vdevs to it.
2N/A */
2N/A verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
2N/A verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
2N/A VDEV_TYPE_ROOT) == 0);
2N/A verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2N/A top, toplevels) == 0);
2N/A if (nspares != 0)
2N/A verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2N/A spares, nspares) == 0);
2N/A if (nl2cache != 0)
2N/A verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2N/A l2cache, nl2cache) == 0);
2N/Aerrout:
2N/A for (t = 0; t < toplevels; t++)
2N/A nvlist_free(top[t]);
2N/A for (t = 0; t < nspares; t++)
2N/A nvlist_free(spares[t]);
2N/A for (t = 0; t < nl2cache; t++)
2N/A nvlist_free(l2cache[t]);
2N/A free(spares);
2N/A free(l2cache);
2N/A free(top);
2N/A
2N/A return (nvroot);
2N/A}
2N/A
2N/Anvlist_t *
2N/Azpool_split_root_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
2N/A splitflags_t flags, int vdevcount, char **vdevlist)
2N/A{
2N/A nvlist_t *newroot = NULL;
2N/A
2N/A assert(zhp != NULL);
2N/A
2N/A if (vdevcount > 0) {
2N/A char msg[1024];
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A libzfs_handle_t *hdl = zhp->zpool_hdl;
2N/A
2N/A (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2N/A "Unable to split '%s'"), zhp->zpool_name);
2N/A
2N/A if ((newroot = construct_spec(hdl, vdevcount, vdevlist))
2N/A == NULL) {
2N/A (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2N/A return (NULL);
2N/A }
2N/A if (!flags.dryrun && make_disks(hdl, zhp, newroot,
2N/A ZPOOL_LABEL_MATCH_REQ_PART) != 0) {
2N/A (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2N/A nvlist_free(newroot);
2N/A return (NULL);
2N/A }
2N/A /* avoid any tricks in the spec */
2N/A verify(nvlist_lookup_nvlist_array(newroot,
2N/A ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
2N/A for (c = 0; c < children; c++) {
2N/A char *path;
2N/A const char *type;
2N/A int min, max;
2N/A
2N/A verify(nvlist_lookup_string(child[c],
2N/A ZPOOL_CONFIG_PATH, &path) == 0);
2N/A if ((type = is_grouping(path, &min, &max)) != NULL) {
2N/A zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2N/A "invalid vdev specification: cannot use "
2N/A "'%s' as a device for splitting"),
2N/A type);
2N/A (void) zfs_error(hdl, EZFS_BADDEV, msg);
2N/A nvlist_free(newroot);
2N/A return (NULL);
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
2N/A /* zpool_vdev_split() does the error messaging here */
2N/A if (newroot != NULL)
2N/A nvlist_free(newroot);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (newroot);
2N/A}
2N/A
2N/A/*
2N/A * Get and validate the contents of the given vdev specification. This ensures
2N/A * that the nvlist returned is well-formed, that all the devices exist, and that
2N/A * they are not currently in use by any other known consumer. The 'poolconfig'
2N/A * parameter is the current configuration of the pool when adding devices
2N/A * existing pool, and is used to perform additional checks, such as changing the
2N/A * replication level of the pool. It can be 'NULL' to indicate that this is a
2N/A * new pool. The 'force' flag controls whether devices should be forcefully
2N/A * added, even if they appear in use.
2N/A */
2N/Anvlist_t *
2N/Azpool_make_root_vdev(libzfs_handle_t *hdl, zpool_handle_t *zhp, boolean_t force,
2N/A int check_rep, boolean_t replacing, boolean_t dryrun, int create_req_part,
2N/A int vdevcount, char **vdevlist)
2N/A{
2N/A nvlist_t *newroot;
2N/A nvlist_t *poolconfig = NULL;
2N/A char msg[1024];
2N/A
2N/A (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2N/A "Unable to build pool from specified devices"));
2N/A /*
2N/A * Construct the vdev specification. If this is successful, we know
2N/A * that we have a valid specification, and that all devices can be
2N/A * opened.
2N/A */
2N/A if ((newroot = construct_spec(hdl, vdevcount, vdevlist)) == NULL) {
2N/A (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2N/A return (NULL);
2N/A }
2N/A if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL)) {
2N/A (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2N/A return (NULL);
2N/A }
2N/A /*
2N/A * Validate each device to make sure that its not shared with another
2N/A * subsystem. We do this even if 'force' is set, because there are some
2N/A * uses (such as a dedicated dump device) that even '-f' cannot
2N/A * override.
2N/A */
2N/A if (check_in_use(hdl, poolconfig, newroot, force, replacing, B_FALSE)
2N/A != 0) {
2N/A nvlist_free(newroot);
2N/A (void) zfs_error(hdl, EZFS_DEV_INUSE, msg);
2N/A return (NULL);
2N/A }
2N/A /*
2N/A * Check the replication level of the given vdevs and report any errors
2N/A * found. We include the existing pool spec, if any, as we need to
2N/A * catch changes against the existing replication level.
2N/A */
2N/A if (check_rep && check_replication(hdl, poolconfig, newroot, force)
2N/A != 0) {
2N/A nvlist_free(newroot);
2N/A (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2N/A return (NULL);
2N/A }
2N/A /*
2N/A * Run through the vdev specification and label any whole disks found.
2N/A */
2N/A if (!dryrun && make_disks(hdl, zhp, newroot, create_req_part) != 0) {
2N/A nvlist_free(newroot);
2N/A (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (newroot);
2N/A}