zpool_vdev.c revision fa9e4066f08beec538e775443c5be79dd423fcab
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Functions to convert between a list of vdevs and an nvlist representing the
* configuration. Each entry in the list can be one of:
*
* Device vdevs
* disk=(path=..., devid=...)
* file=(path=...)
*
* Group vdevs
* raidz=(...)
* mirror=(...)
*
* While the underlying implementation supports it, group vdevs cannot contain
* other group vdevs. All userland verification of devices is contained within
* this file. If successful, the nvlist returned can be passed directly to the
* kernel; we've done as much verification as possible in userland.
*
* The only function exported by this file is 'get_vdev_spec'. The function
* performs several passes:
*
* 1. Construct the vdev specification. Performs syntax validation and
* makes sure each device is valid.
* 2. Check for devices in use. Using libdiskmgt, makes sure that no
* devices are also in use. Some can be overridden using the 'force'
* flag, others cannot.
* 3. Check for replication errors if the 'force' flag is not specified.
* validates that the replication level is consistent across the
* entire pool.
* 4. Label any whole disks with an EFI label.
*/
#include <assert.h>
#include <devid.h>
#include <errno.h>
#include <fcntl.h>
#include <libdiskmgt.h>
#include <libintl.h>
#include <libnvpair.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/efi_partition.h>
#include <libzfs.h>
#include "zpool_util.h"
#define RDISK_ROOT "/dev/rdsk"
#define BACKUP_SLICE "s2"
/*
* For any given vdev specification, we can have multiple errors. The
* vdev_error() function keeps track of whether we have seen an error yet, and
* prints out a header if its the first error we've seen.
*/
int error_seen;
int is_force;
void
vdev_error(const char *fmt, ...)
{
if (!error_seen) {
if (!is_force)
"the following errors:\n"));
else
"must be manually repaired:\n"));
error_seen = TRUE;
}
}
void
{
if (err == 0)
no_memory();
/*
* Some of the libdiskmgt stuff requires root privileges in order to
* examine devices. Bail out gracefully in this case.
*/
"configuration: permission denied\n"));
exit(1);
}
abort();
}
/*
* Checks whether a single slice overlaps with any of the slices in the provided
* list. Called by check_overlapping().
*/
int
{
int i = 0;
uint64_t start_block = 0;
uint64_t media_size = 0;
if (*error != 0) {
return (-1);
}
if (media_attrs == NULL) {
return (0);
}
if (*error != 0) {
return (-1);
}
if (*error != 0) {
return (-1);
}
/*
* Not really possible, but the error above would catch any system
* errors.
*/
if (slice_attrs == NULL) {
return (0);
}
if (*error != 0) {
return (-1);
}
if (*error != 0) {
return (-1);
}
if (*error != 0) {
return (-1);
}
for (i = 0; slice_list[i]; i ++) {
if (*error != 0) {
return (-1);
}
if (other_attrs == NULL)
continue;
&other_start);
if (*error) {
return (-1);
}
&other_size);
if (*error) {
return (-1);
}
&snum);
if (*error) {
return (-1);
}
/*
* Check to see if there are > 2 overlapping regions
* on this media in the same region as this slice.
* This is done by assuming the following:
* Slice 2 is the backup slice if it is the size
* of the whole disk
* If slice 2 is the overlap and slice 2 is the size of
* the whole disk, continue. If another slice is found
* that overlaps with our slice, return it.
* There is the potential that there is more than one slice
* that our slice overlaps with, however, we only return
* the first overlapping slice we find.
*
*/
continue;
} else {
if (*error != 0) {
return (-1);
}
return (1);
}
} else if (other_start >= start_block &&
other_start <= end_block) {
continue;
} else {
if (*error != 0) {
return (-1);
}
return (1);
}
}
}
return (0);
}
/*
* Check to see whether the given slice overlaps with any other slices. Get the
* associated slice information and pass on to is_overlapping().
*/
int
{
int error;
char *overlaps;
int ret = 0;
/*
* Get the list of slices be fetching the associated media, and then all
* associated slices.
*/
ret = -1;
}
return (ret);
}
/*
* Validate the given slice. If 'diskname' is non-NULL, then this is a single
* slice on a complete disk. If 'force' is set, then the user specified '-f'
* and we only want to report error for completely forbidden uses.
*/
int
int overlap)
{
int err;
int fd;
/*
* Always check to see if this is used by an active ZFS pool.
*/
if (!force) {
}
}
}
/*
* This slice is in use. Print out a descriptive message describing who
* is using it. The 'used_by' nvlist is formatted as:
*
* (used_by=what, used_name=desc, ...)
*
* Each 'used_by' must be accompanied by a 'used_name'.
*/
for (;;) {
break;
/*
* For currently mounted filesystems, filesystems in
* them, even if '-f' is specified. The rest of the errors
* indicate that a filesystem was detected on disk, which can be
* overridden with '-f'.
*/
"currently mounted on %s\n"),
"dedicated dump device\n"), slicename);
}
} else if (!force) {
/*
* We should have already caught ZFS in-use
* filesystems above. If the ZFS version is
* different, or there was some other critical
* failure, it's possible for fstyp to report it
* as in-use, but zpool_open_by_dev() to fail.
*/
else if (!found_zfs)
"outdated or damaged ZFS "
"pool\n"), slicename);
} else {
}
} else {
}
}
/*
* Perform any overlap checking if requested to do so.
*/
return (found ? -1 : 0);
}
/*
* Validate a whole disk. Iterate over all slices on the disk and make sure
* that none is in use by calling check_slice().
*/
/* ARGSUSED */
int
{
int err = 0;
int i;
int ret;
/*
* Get the drive associated with this disk. This should never fail,
* because we already have an alias handle open for the device.
*/
/*
* It is possible that the user has specified a removable media drive,
* and the media is not present.
*/
return (-1);
}
ret = 0;
/*
* Iterate over all slices and report any errors. We don't care about
* overlapping slices because we are using the whole disk.
*/
ret = -1;
}
return (ret);
}
/*
* Validate a device. Determines whether the device is a disk, slice, or
* partition, and passes it off to an appropriate function.
*/
int
{
int err;
/*
* For whole disks, libdiskmgt does not include the leading dev path.
*/
dev++;
/*
* If 'err' is not ENODEV, then we've had an unexpected error from
* libdiskmgt. The only explanation is that we ran out of memory.
*/
/*
* Determine if this is a slice.
*/
!= NULL)
/*
* dealing with partitions, so convert it.
*/
!= NULL) {
/* XXZFS perform checking on partitions */
return (0);
}
/*
* At this point, libdiskmgt failed to find the device as either a whole
* disk or a slice. Ignore these errors, as we know that it at least a
* block device. The user may have provided us with some unknown device
* that libdiskmgt doesn't know about.
*/
return (0);
}
/*
* Check that a file is valid. All we can do in this case is check that it's
* not in use by another pool.
*/
int
{
int fd;
int ret = 0;
return (0);
!force) {
ret = -1;
}
}
return (ret);
}
static int
{
char path[MAXPATHLEN];
return (TRUE);
return (FALSE);
}
/*
* Create a leaf vdev. Determine if this is a file or a device. If it's a
* device, fill in the device id to make a complete nvlist. Valid forms for a
* leaf vdev are:
*
* /xxx Full path to file
*/
nvlist_t *
make_leaf_vdev(const char *arg)
{
char path[MAXPATHLEN];
/*
* Determine what type of vdev this is, and put the full path into
* 'path'. We detect whether this is a device of file afterwards by
* checking the st_mode of the file.
*/
if (arg[0] == '/') {
/*
* Complete device or file path. Exact type is determined by
* examining the file descriptor afterwards.
*/
gettext("cannot open '%s': %s\n"),
return (NULL);
}
} else {
/*
* This may be a short path for a device, or it could be total
* gibberish. Check to see if it's a known device in
* an entire disk (minus the slice number).
*/
arg);
/*
* If we got ENOENT, then the user gave us
* gibberish, so try to direct them with a
* reasonable error message. Otherwise,
* regurgitate strerror() since it's the best we
* can do.
*/
gettext("cannot open '%s': no such "
gettext("must be a full path or "
"shorthand device name\n"));
return (NULL);
} else {
gettext("cannot open '%s': %s\n"),
return (NULL);
}
}
}
/*
* Determine whether this is a device or a file.
*/
} else {
"block device or regular file\n"), path);
return (NULL);
}
/*
* Finally, we have the complete device or file, and we know that it is
* acceptable to use. Construct the nvlist to describe this vdev. All
* vdevs have a 'path' element, and devices also have a 'devid' element.
*/
/*
* For a whole disk, defer getting its devid until after labeling it.
*/
/*
* Get the devid for the device.
*/
int fd;
return (NULL);
}
NULL) {
ZPOOL_CONFIG_DEVID, devid_str) == 0);
}
}
}
return (vdev);
}
/*
* Go through and verify the replication level of the pool is consistent.
* Performs the following checks:
*
* For the new spec, verifies that devices in mirrors and raidz are the
* same size.
*
* If the current configuration already has inconsistent replication
* levels, ignore any other potential problems in the new spec.
*
* Otherwise, make sure that the current spec (if there is one) and the new
* spec have consistent replication levels.
*/
typedef struct replication_level {
char *type;
int level;
/*
* Given a list of toplevel vdevs, return the current replication level. If
* the config is inconsistent, then NULL is returned. If 'fatal' is set, then
* an error message will be displayed for each self-inconsistent vdev.
*/
{
char *type;
int dontreport;
for (t = 0; t < toplevels; t++) {
/*
* This is a 'file' or 'disk' vdev.
*/
} else {
/*
* This is a mirror or RAID-Z vdev. Go through and make
* sure the contents are all the same (files vs. disks),
* keeping track of the number of elements in the
* process.
*
* We also check that the size of each vdev (if it can
* be determined) is the same.
*/
/*
* The 'dontreport' variable indicatest that we've
* already reported an error for this spec, so don't
* bother doing it again.
*/
dontreport = 0;
vdev_size = -1ULL;
for (c = 0; c < children; c++) {
char *path;
char *childtype;
ZPOOL_CONFIG_TYPE, &childtype) == 0);
ZPOOL_CONFIG_PATH, &path) == 0);
/*
* with files, report it as an error.
*/
if (fatal)
"mismatched replication "
"level: %s contains both "
"files and devices\n"),
else
return (NULL);
dontreport = TRUE;
}
/*
* According to stat(2), the value of 'st_size'
* is undefined for block devices and character
* devices. But there is no effective way to
* determine the real size in userland.
*
* Instead, we'll take advantage of an
* implementation detail of spec_size(). If the
* device is currently open, then we (should)
* return a valid size.
*
* If we still don't get a valid size (indicated
* by a size of 0 or MAXOFFSET_T), then ignore
* this device altogether.
*/
} else {
}
if (err != 0 ||
continue;
/*
* Also check the size of each device. If they
* differ, then report an error.
*/
if (fatal)
"%s contains devices of "
"different sizes\n"),
else
return (NULL);
dontreport = TRUE;
}
}
}
/*
* At this point, we have the replication of the last toplevel
* vdev in 'rep'. Compare it to 'lastrep' to see if its
* different.
*/
if (fatal)
"mismatched replication "
"level: both %s and %s vdevs are "
"present\n"),
else
return (NULL);
if (ret)
if (fatal)
"mismatched replication "
"level: %d-way %s and %d-way %s "
"vdevs are present\n"),
else
return (NULL);
}
}
}
}
return (ret);
}
/*
* Check the replication level of the vdev spec against the current pool. Calls
* get_replication() to make sure the new spec is self-consistent. If the pool
* has a consistent replication level, then we ignore any errors. Otherwise,
* report any difference between the two.
*/
int
{
int ret;
/*
* If we have a current pool configuration, check to see if it's
* self-consistent. If not, simply return success.
*/
&nvroot) == 0);
return (0);
}
/*
* Get the replication level of the new vdev spec, reporting any
* inconsistencies found.
*/
return (-1);
}
/*
* Check to see if the new vdev spec matches the replication level of
* the current pool.
*/
ret = 0;
"mismatched replication level: pool uses %d-way %s "
"and new vdev uses %d-way %s\n"),
ret = -1;
}
}
return (ret);
}
/*
* Label an individual disk. The name provided is the short name, stripped of
* any leading /dev path.
*/
int
label_disk(char *name)
{
char path[MAXPATHLEN];
int fd;
/*
* This shouldn't happen. We've long since verified that this
* is a valid device.
*/
return (-1);
}
/*
* The only way this can fail is if we run out of memory, or we
* were unable to read the disk geometry.
*/
no_memory();
"read disk geometry\n"), name);
return (-1);
}
/*
* Why we use V_USR: V_BACKUP confuses users, and is considered
* disposable by some EFI utilities (since EFI doesn't have a backup
* slice). V_UNASSIGNED is supposed to be used only for zero size
* partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
* etc. were all pretty specific. V_USR is as close to reality as we
* can get, in the absence of V_OTHER.
*/
/*
* Currently, EFI labels are not supported for IDE disks, and it
* is likely that they will not be supported on other drives for
* some time. Print out a helpful error message directing the
* user to manually label the disk and give a specific slice.
*/
"write EFI label\n"), name);
"the disk, and provide a specific slice\n"));
return (-1);
}
return (0);
}
/*
* Go through and find any whole disks in the vdev specification, labelling them
* as appropriate. When constructing the vdev spec, we were unable to open this
* device in order to provide a devid. Now that we have labelled the disk and
* know that slice 0 is valid, we can construct the devid now.
*
* If the disk was already labelled with an EFI label, we will have gotten the
* devid already (because we were able to open the whole disk). Otherwise, we
* need to get the devid after we label the disk.
*/
int
{
char buf[MAXPATHLEN];
int fd;
int ret;
return (0);
/*
* We have a disk device. Get the path to the device
* and see if its a whole disk by appending the backup
* slice and stat()ing the device.
*/
return (0);
diskname++;
if (label_disk(diskname) != 0)
return (-1);
/*
* Fill in the devid, now that we've labeled the disk.
*/
gettext("cannot open '%s': %s\n"),
return (-1);
}
NULL) {
ZPOOL_CONFIG_DEVID, devid_str) == 0);
}
}
return (0);
}
for (c = 0; c < children; c++)
return (ret);
return (0);
}
/*
* Go through and find any devices that are in use. We rely on libdiskmgt for
* the majority of this task.
*/
int
{
int ret;
return (ret);
}
for (c = 0; c < children; c++)
return (ret);
return (0);
}
/*
* 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 *
{
int t, toplevels;
toplevels = 0;
while (argc > 0) {
/*
* If it's a mirror or raidz, the subsequent arguments are
* its leaves -- until we encounter the next mirror or raidz.
*/
int children = 0;
int c;
for (c = 1; c < argc; c++) {
break;
children++;
no_memory();
return (NULL);
}
argc -= c;
argv += c;
/*
* Mirrors and RAID-Z devices require at least
* two components.
*/
if (children < 2) {
gettext("invalid vdev specification: "
"%s requires at least 2 devices\n"), type);
return (NULL);
}
type) == 0);
for (c = 0; c < children; c++)
nvlist_free(child[c]);
} else {
/*
* We have a device. Pass off to make_leaf_vdev() to
* construct the appropriate nvlist describing the vdev.
*/
return (NULL);
argc--;
argv++;
}
toplevels++;
no_memory();
}
/*
* Finally, create nvroot and add all top-level vdevs to it.
*/
VDEV_TYPE_ROOT) == 0);
for (t = 0; t < toplevels; t++)
nvlist_free(top[t]);
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 *
{
/*
* Construct the vdev specification. If this is successful, we know
* that we have a valid specification, and that all devices can be
* opened.
*/
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.
*/
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.
*/
return (NULL);
}
/*
* Run through the vdev specification and label any whole disks found.
*/
if (make_disks(newroot) != 0) {
return (NULL);
}
return (newroot);
}