zpool_vdev.c revision f94275ce205810a201404c5f35f4cc96057022b1
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 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
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 * The only function exported by this file is 'make_root_vdev'. The
2N/A * 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. Call libzfs to label any whole disks with an EFI label.
2N/A */
2N/A
2N/A#include <assert.h>
2N/A#include <devid.h>
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <libdiskmgt.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
2N/A#include "zpool_util.h"
2N/A
2N/A#define DISK_ROOT "/dev/dsk"
2N/A#define RDISK_ROOT "/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/Aboolean_t error_seen;
2N/Aboolean_t is_force;
2N/A
2N/A/*PRINTFLIKE1*/
2N/Astatic void
2N/Avdev_error(const char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A if (!error_seen) {
2N/A (void) fprintf(stderr, gettext("invalid vdev specification\n"));
2N/A if (!is_force)
2N/A (void) fprintf(stderr, gettext("use '-f' to override "
2N/A "the following errors:\n"));
2N/A else
2N/A (void) fprintf(stderr, gettext("the following errors "
2N/A "must be manually repaired:\n"));
2N/A error_seen = B_TRUE;
2N/A }
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/Alibdiskmgt_error(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
2N/A (void) fprintf(stderr, gettext("warning: device in use checking "
2N/A "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(const char *path, int force, 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 = DM_WHO_ZPOOL_FORCE;
2N/A else if (isspare)
2N/A who = DM_WHO_ZPOOL_SPARE;
2N/A else
2N/A who = DM_WHO_ZPOOL;
2N/A
2N/A if (dm_inuse((char *)path, &msg, who, &error) || error) {
2N/A if (error != 0) {
2N/A libdiskmgt_error(error);
2N/A return (0);
2N/A } else {
2N/A vdev_error("%s", msg);
2N/A free(msg);
2N/A return (-1);
2N/A }
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(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 libdiskmgt_error(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(const char *name, dm_descriptor_t disk, int force, int isspare)
2N/A{
2N/A dm_descriptor_t *drive, *media, *slice;
2N/A int err = 0;
2N/A int i;
2N/A int 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 libdiskmgt_error(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 libdiskmgt_error(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(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 libdiskmgt_error(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(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(const char *path, boolean_t force, 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(path, desc, force, isspare);
2N/A dm_free_descriptor(desc);
2N/A return (err);
2N/A }
2N/A
2N/A return (check_slice(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(const char *file, boolean_t force, 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 libdiskmgt_error(err);
2N/A else
2N/A vdev_error(gettext("%s is currently used by swap. "
2N/A "Please see swap(1M).\n"), file);
2N/A return (-1);
2N/A }
2N/A
2N/A if ((fd = open(file, O_RDONLY)) < 0)
2N/A return (0);
2N/A
2N/A if (zpool_in_use(g_zfs, 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(gettext("%s is reserved as a hot "
2N/A "spare for pool %s\n"), file, name);
2N/A break;
2N/A default:
2N/A vdev_error(gettext("%s is part of %s pool "
2N/A "'%s'\n"), 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 */
2N/Astatic nvlist_t *
2N/Amake_leaf_vdev(const char *arg, uint64_t is_log)
2N/A{
2N/A char path[MAXPATHLEN];
2N/A struct stat64 statbuf;
2N/A nvlist_t *vdev = NULL;
2N/A char *type = NULL;
2N/A boolean_t wholedisk = B_FALSE;
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 (arg[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(arg);
2N/A if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
2N/A (void) fprintf(stderr,
2N/A gettext("cannot open '%s': %s\n"),
2N/A arg, strerror(errno));
2N/A return (NULL);
2N/A }
2N/A
2N/A (void) strlcpy(path, arg, 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 arg);
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 (void) fprintf(stderr,
2N/A gettext("cannot open '%s': no such "
2N/A "device in %s\n"), arg, DISK_ROOT);
2N/A (void) fprintf(stderr,
2N/A gettext("must be a full path or "
2N/A "shorthand device name\n"));
2N/A return (NULL);
2N/A } else {
2N/A (void) fprintf(stderr,
2N/A gettext("cannot open '%s': %s\n"),
2N/A path, strerror(errno));
2N/A return (NULL);
2N/A }
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 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
2N/A "block device or regular file\n"), 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 * 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 (void) fprintf(stderr, gettext("cannot open '%s': "
2N/A "%s\n"), 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 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 * Given a list of toplevel vdevs, return the current replication level. If
2N/A * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
2N/A * an error message will be displayed for each self-inconsistent vdev.
2N/A */
2N/Astatic replication_level_t *
2N/Aget_replication(nvlist_t *nvroot, boolean_t fatal)
2N/A{
2N/A nvlist_t **top;
2N/A uint_t t, toplevels;
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A nvlist_t *nv;
2N/A char *type;
2N/A replication_level_t lastrep, rep, *ret;
2N/A boolean_t dontreport;
2N/A
2N/A ret = safe_malloc(sizeof (replication_level_t));
2N/A
2N/A verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2N/A &top, &toplevels) == 0);
2N/A
2N/A lastrep.zprl_type = NULL;
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 if (is_log)
2N/A continue;
2N/A
2N/A verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
2N/A &type) == 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_type = type;
2N/A rep.zprl_children = 1;
2N/A rep.zprl_parity = 0;
2N/A } else {
2N/A uint64_t vdev_size;
2N/A
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
2N/A * 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 rep.zprl_type = type;
2N/A rep.zprl_children = 0;
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 } else {
2N/A rep.zprl_parity = 0;
2N/A }
2N/A
2N/A /*
2N/A * The 'dontreport' 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 dontreport = 0;
2N/A vdev_size = -1ULL;
2N/A for (c = 0; c < children; c++) {
2N/A nvlist_t *cnv = child[c];
2N/A char *path;
2N/A struct stat64 statbuf;
2N/A uint64_t size = -1ULL;
2N/A char *childtype;
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 if (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 == 2);
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 (!dontreport && type != NULL &&
2N/A strcmp(type, childtype) != 0) {
2N/A if (ret != NULL)
2N/A free(ret);
2N/A ret = NULL;
2N/A if (fatal)
2N/A vdev_error(gettext(
2N/A "mismatched replication "
2N/A "level: %s contains both "
2N/A "files and devices\n"),
2N/A rep.zprl_type);
2N/A else
2N/A return (NULL);
2N/A dontreport = B_TRUE;
2N/A }
2N/A
2N/A /*
2N/A * According to stat(2), the value of 'st_size'
2N/A * is undefined for block devices and character
2N/A * devices. But there is no effective way to
2N/A * determine the real size in userland.
2N/A *
2N/A * Instead, we'll take advantage of an
2N/A * implementation detail of spec_size(). If the
2N/A * device is currently open, then we (should)
2N/A * return a valid size.
2N/A *
2N/A * If we still don't get a valid size (indicated
2N/A * by a size of 0 or MAXOFFSET_T), then ignore
2N/A * 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
2N/A * slices have a consistent size. If
2N/A * they differ by a significant amount
2N/A * (~16MB) then report an error.
2N/A */
2N/A if (!dontreport &&
2N/A (vdev_size != -1ULL &&
2N/A (labs(size - vdev_size) >
2N/A ZPOOL_FUZZ))) {
2N/A if (ret != NULL)
2N/A free(ret);
2N/A ret = NULL;
2N/A if (fatal)
2N/A vdev_error(gettext(
2N/A "%s contains devices of "
2N/A "different sizes\n"),
2N/A rep.zprl_type);
2N/A else
2N/A return (NULL);
2N/A dontreport = B_TRUE;
2N/A }
2N/A
2N/A type = childtype;
2N/A vdev_size = size;
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 'lastrep' to see if its
2N/A * different.
2N/A */
2N/A if (lastrep.zprl_type != NULL) {
2N/A if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
2N/A if (ret != NULL)
2N/A free(ret);
2N/A ret = NULL;
2N/A if (fatal)
2N/A vdev_error(gettext(
2N/A "mismatched replication level: "
2N/A "both %s and %s vdevs are "
2N/A "present\n"),
2N/A lastrep.zprl_type, rep.zprl_type);
2N/A else
2N/A return (NULL);
2N/A } else if (lastrep.zprl_parity != rep.zprl_parity) {
2N/A if (ret)
2N/A free(ret);
2N/A ret = NULL;
2N/A if (fatal)
2N/A vdev_error(gettext(
2N/A "mismatched replication level: "
2N/A "both %llu and %llu device parity "
2N/A "%s vdevs are present\n"),
2N/A lastrep.zprl_parity,
2N/A rep.zprl_parity,
2N/A rep.zprl_type);
2N/A else
2N/A return (NULL);
2N/A } else if (lastrep.zprl_children != rep.zprl_children) {
2N/A if (ret)
2N/A free(ret);
2N/A ret = NULL;
2N/A if (fatal)
2N/A vdev_error(gettext(
2N/A "mismatched replication level: "
2N/A "both %llu-way and %llu-way %s "
2N/A "vdevs are present\n"),
2N/A lastrep.zprl_children,
2N/A rep.zprl_children,
2N/A rep.zprl_type);
2N/A else
2N/A return (NULL);
2N/A }
2N/A }
2N/A lastrep = rep;
2N/A }
2N/A
2N/A if (ret != NULL)
2N/A *ret = rep;
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 * get_replication() to make sure the new spec is self-consistent. If the pool
2N/A * has a consistent replication level, then we ignore any errors. Otherwise,
2N/A * report any difference between the two.
2N/A */
2N/Astatic int
2N/Acheck_replication(nvlist_t *config, nvlist_t *newroot)
2N/A{
2N/A nvlist_t **child;
2N/A uint_t children;
2N/A replication_level_t *current = NULL, *new;
2N/A int ret;
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 ((current = get_replication(nvroot, B_FALSE)) == NULL)
2N/A return (0);
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 free(current);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * If all we have is logs then there's no replication level to check.
2N/A */
2N/A if (num_logs(newroot) == children) {
2N/A free(current);
2N/A return (0);
2N/A }
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 ((new = get_replication(newroot, B_TRUE)) == NULL) {
2N/A free(current);
2N/A return (-1);
2N/A }
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 ret = 0;
2N/A if (current != NULL) {
2N/A if (strcmp(current->zprl_type, new->zprl_type) != 0) {
2N/A vdev_error(gettext(
2N/A "mismatched replication level: pool uses %s "
2N/A "and new vdev is %s\n"),
2N/A current->zprl_type, new->zprl_type);
2N/A ret = -1;
2N/A } else if (current->zprl_parity != new->zprl_parity) {
2N/A vdev_error(gettext(
2N/A "mismatched replication level: pool uses %llu "
2N/A "device parity and new vdev uses %llu\n"),
2N/A current->zprl_parity, new->zprl_parity);
2N/A ret = -1;
2N/A } else if (current->zprl_children != new->zprl_children) {
2N/A vdev_error(gettext(
2N/A "mismatched replication level: pool uses %llu-way "
2N/A "%s 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 ret = -1;
2N/A }
2N/A }
2N/A
2N/A free(new);
2N/A if (current != NULL)
2N/A free(current);
2N/A
2N/A return (ret);
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(zpool_handle_t *zhp, nvlist_t *nv)
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
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 if (zpool_label_disk(g_zfs, zhp, diskname) == -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), "%ss0", path);
2N/A if ((fd = open(buf, O_RDONLY)) < 0) {
2N/A (void) fprintf(stderr,
2N/A gettext("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 's0' 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
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(zhp, child[c])) != 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(zhp, child[c])) != 0)
2N/A return (ret);
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(zhp, child[c])) != 0)
2N/A return (ret);
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(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(g_zfs, 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
2N/A (void) close(fd);
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(nvlist_t *config, nvlist_t *nv, int force, int isreplacing,
2N/A int isspare)
2N/A{
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A char *type, *path;
2N/A int ret;
2N/A char buf[MAXPATHLEN];
2N/A uint64_t wholedisk;
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 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2N/A
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 (isreplacing) {
2N/A if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2N/A &wholedisk) == 0 && wholedisk)
2N/A (void) snprintf(buf, sizeof (buf), "%ss0",
2N/A path);
2N/A else
(void) strlcpy(buf, path, sizeof (buf));
if (is_spare(config, buf))
return (0);
}
if (strcmp(type, VDEV_TYPE_DISK) == 0)
ret = check_device(path, force, isspare);
if (strcmp(type, VDEV_TYPE_FILE) == 0)
ret = check_file(path, force, isspare);
return (ret);
}
for (c = 0; c < children; c++)
if ((ret = check_in_use(config, child[c], force,
isreplacing, B_FALSE)) != 0)
return (ret);
if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
&child, &children) == 0)
for (c = 0; c < children; c++)
if ((ret = check_in_use(config, child[c], force,
isreplacing, B_TRUE)) != 0)
return (ret);
if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
&child, &children) == 0)
for (c = 0; c < children; c++)
if ((ret = check_in_use(config, child[c], force,
isreplacing, B_FALSE)) != 0)
return (ret);
return (0);
}
static const char *
is_grouping(const char *type, int *mindev, int *maxdev)
{
if (strncmp(type, "raidz", 5) == 0) {
const char *p = type + 5;
char *end;
long nparity;
if (*p == '\0') {
nparity = 1;
} else if (*p == '0') {
return (NULL); /* no zero prefixes allowed */
} else {
errno = 0;
nparity = strtol(p, &end, 10);
if (errno != 0 || nparity < 1 || nparity >= 255 ||
*end != '\0')
return (NULL);
}
if (mindev != NULL)
*mindev = nparity + 1;
if (maxdev != NULL)
*maxdev = 255;
return (VDEV_TYPE_RAIDZ);
}
if (maxdev != NULL)
*maxdev = INT_MAX;
if (strcmp(type, "mirror") == 0) {
if (mindev != NULL)
*mindev = 2;
return (VDEV_TYPE_MIRROR);
}
if (strcmp(type, "spare") == 0) {
if (mindev != NULL)
*mindev = 1;
return (VDEV_TYPE_SPARE);
}
if (strcmp(type, "log") == 0) {
if (mindev != NULL)
*mindev = 1;
return (VDEV_TYPE_LOG);
}
if (strcmp(type, "cache") == 0) {
if (mindev != NULL)
*mindev = 1;
return (VDEV_TYPE_L2CACHE);
}
return (NULL);
}
/*
* Construct a syntactically valid vdev specification,
* and ensure that all devices and files exist and can be opened.
* Note: we don't bother freeing anything in the error paths
* because the program is just going to exit anyway.
*/
nvlist_t *
construct_spec(int argc, char **argv)
{
nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
const char *type;
uint64_t is_log;
boolean_t seen_logs;
top = NULL;
toplevels = 0;
spares = NULL;
l2cache = NULL;
nspares = 0;
nlogs = 0;
nl2cache = 0;
is_log = B_FALSE;
seen_logs = B_FALSE;
while (argc > 0) {
nv = NULL;
/*
* If it's a mirror or raidz, the subsequent arguments are
* its leaves -- until we encounter the next mirror or raidz.
*/
if ((type = is_grouping(argv[0], &mindev, &maxdev)) != NULL) {
nvlist_t **child = NULL;
int c, children = 0;
if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
if (spares != NULL) {
(void) fprintf(stderr,
gettext("invalid vdev "
"specification: 'spare' can be "
"specified only once\n"));
return (NULL);
}
is_log = B_FALSE;
}
if (strcmp(type, VDEV_TYPE_LOG) == 0) {
if (seen_logs) {
(void) fprintf(stderr,
gettext("invalid vdev "
"specification: 'log' can be "
"specified only once\n"));
return (NULL);
}
seen_logs = B_TRUE;
is_log = B_TRUE;
argc--;
argv++;
/*
* A log is not a real grouping device.
* We just set is_log and continue.
*/
continue;
}
if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
if (l2cache != NULL) {
(void) fprintf(stderr,
gettext("invalid vdev "
"specification: 'cache' can be "
"specified only once\n"));
return (NULL);
}
is_log = B_FALSE;
}
if (is_log) {
if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
(void) fprintf(stderr,
gettext("invalid vdev "
"specification: unsupported 'log' "
"device: %s\n"), type);
return (NULL);
}
nlogs++;
}
for (c = 1; c < argc; c++) {
if (is_grouping(argv[c], NULL, NULL) != NULL)
break;
children++;
child = realloc(child,
children * sizeof (nvlist_t *));
if (child == NULL)
zpool_no_memory();
if ((nv = make_leaf_vdev(argv[c], B_FALSE))
== NULL)
return (NULL);
child[children - 1] = nv;
}
if (children < mindev) {
(void) fprintf(stderr, gettext("invalid vdev "
"specification: %s requires at least %d "
"devices\n"), argv[0], mindev);
return (NULL);
}
if (children > maxdev) {
(void) fprintf(stderr, gettext("invalid vdev "
"specification: %s supports no more than "
"%d devices\n"), argv[0], maxdev);
return (NULL);
}
argc -= c;
argv += c;
if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
spares = child;
nspares = children;
continue;
} else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
l2cache = child;
nl2cache = children;
continue;
} else {
verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
0) == 0);
verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
type) == 0);
verify(nvlist_add_uint64(nv,
ZPOOL_CONFIG_IS_LOG, is_log) == 0);
if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
verify(nvlist_add_uint64(nv,
ZPOOL_CONFIG_NPARITY,
mindev - 1) == 0);
}
verify(nvlist_add_nvlist_array(nv,
ZPOOL_CONFIG_CHILDREN, child,
children) == 0);
for (c = 0; c < children; c++)
nvlist_free(child[c]);
free(child);
}
} else {
/*
* We have a device. Pass off to make_leaf_vdev() to
* construct the appropriate nvlist describing the vdev.
*/
if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
return (NULL);
if (is_log)
nlogs++;
argc--;
argv++;
}
toplevels++;
top = realloc(top, toplevels * sizeof (nvlist_t *));
if (top == NULL)
zpool_no_memory();
top[toplevels - 1] = nv;
}
if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
(void) fprintf(stderr, gettext("invalid vdev "
"specification: at least one toplevel vdev must be "
"specified\n"));
return (NULL);
}
if (seen_logs && nlogs == 0) {
(void) fprintf(stderr, gettext("invalid vdev specification: "
"log requires at least 1 device\n"));
return (NULL);
}
/*
* Finally, create nvroot and add all top-level vdevs to it.
*/
verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
VDEV_TYPE_ROOT) == 0);
verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
top, toplevels) == 0);
if (nspares != 0)
verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
spares, nspares) == 0);
if (nl2cache != 0)
verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
l2cache, nl2cache) == 0);
for (t = 0; t < toplevels; t++)
nvlist_free(top[t]);
for (t = 0; t < nspares; t++)
nvlist_free(spares[t]);
for (t = 0; t < nl2cache; t++)
nvlist_free(l2cache[t]);
if (spares)
free(spares);
if (l2cache)
free(l2cache);
free(top);
return (nvroot);
}
/*
* Get and validate the contents of the given vdev specification. This ensures
* that the nvlist returned is well-formed, that all the devices exist, and that
* they are not currently in use by any other known consumer. The 'poolconfig'
* parameter is the current configuration of the pool when adding devices
* existing pool, and is used to perform additional checks, such as changing the
* replication level of the pool. It can be 'NULL' to indicate that this is a
* new pool. The 'force' flag controls whether devices should be forcefully
* added, even if they appear in use.
*/
nvlist_t *
make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
boolean_t isreplacing, boolean_t dryrun, int argc, char **argv)
{
nvlist_t *newroot;
nvlist_t *poolconfig = NULL;
is_force = force;
/*
* Construct the vdev specification. If this is successful, we know
* that we have a valid specification, and that all devices can be
* opened.
*/
if ((newroot = construct_spec(argc, argv)) == NULL)
return (NULL);
if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
return (NULL);
/*
* Validate each device to make sure that its not shared with another
* subsystem. We do this even if 'force' is set, because there are some
* uses (such as a dedicated dump device) that even '-f' cannot
* override.
*/
if (check_in_use(poolconfig, newroot, force, isreplacing,
B_FALSE) != 0) {
nvlist_free(newroot);
return (NULL);
}
/*
* Check the replication level of the given vdevs and report any errors
* found. We include the existing pool spec, if any, as we need to
* catch changes against the existing replication level.
*/
if (check_rep && check_replication(poolconfig, newroot) != 0) {
nvlist_free(newroot);
return (NULL);
}
/*
* Run through the vdev specification and label any whole disks found.
*/
if (!dryrun && make_disks(zhp, newroot) != 0) {
nvlist_free(newroot);
return (NULL);
}
return (newroot);
}