libzfs_dataset.c revision 3ccfa83cd9cddd1e34808ba18082c156758c5ec8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <libdevinfo.h>
#include <libintl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <zone.h>
#include <fcntl.h>
#include <libzfs.h>
#include "zfs_namecheck.h"
#include "zfs_prop.h"
#include "libzfs_impl.h"
/*
* Given a single type (not a mask of types), return the type in a human
* readable form.
*/
const char *
{
switch (type) {
case ZFS_TYPE_FILESYSTEM:
case ZFS_TYPE_SNAPSHOT:
case ZFS_TYPE_VOLUME:
}
return (NULL);
}
/*
* Given a path and mask of ZFS types, return a string describing this dataset.
* This is used when we fail to open a dataset and we cannot get an exact type.
* We guess what the type would have been based on the path and the mask of
* acceptable types.
*/
static const char *
{
/*
* When given a single type, always report the exact type.
*/
if (types == ZFS_TYPE_SNAPSHOT)
if (types == ZFS_TYPE_FILESYSTEM)
if (types == ZFS_TYPE_VOLUME)
/*
* The user is requesting more than one type of dataset. If this is the
* case, consult the path itself. If we're looking for a snapshot, and
* a '@' is found, then report it as "snapshot". Otherwise, remove the
* snapshot attribute and try again.
*/
if (types & ZFS_TYPE_SNAPSHOT) {
}
/*
* The user has requested either filesystems or volumes.
* We have no way of knowing a priori what type this would be, so always
* report it as "filesystem" or "volume", our two primitive types.
*/
if (types & ZFS_TYPE_FILESYSTEM)
}
/*
* Validate a ZFS path. This is used even before trying to open the dataset, to
* provide a more meaningful error message. We place a more useful message in
* 'buf' detailing exactly why the name was not valid.
*/
static int
{
char what;
switch (why) {
case NAME_ERR_TOOLONG:
"name is too long"));
break;
case NAME_ERR_LEADING_SLASH:
"leading slash in name"));
break;
case NAME_ERR_EMPTY_COMPONENT:
"empty component in name"));
break;
case NAME_ERR_TRAILING_SLASH:
"trailing slash in name"));
break;
case NAME_ERR_INVALCHAR:
"'%c' in name"), what);
break;
case NAME_ERR_MULTIPLE_AT:
"multiple '@' delimiters in name"));
break;
case NAME_ERR_NOLETTER:
"pool doesn't begin with a letter"));
break;
case NAME_ERR_RESERVED:
"name is reserved"));
break;
case NAME_ERR_DISKLIKE:
"reserved disk name"));
break;
}
}
return (0);
}
"snapshot delimiter '@' in filesystem name"));
return (0);
}
"missing '@' delimeter in snapshot name"));
return (0);
}
return (-1);
}
int
{
}
/*
* This function takes the raw DSL properties, and filters out the user-defined
* properties into a separate nvlist.
*/
static int
{
continue;
}
return (0);
}
/*
* Utility function to gather stats (objset and zpl) for the given object.
*/
static int
{
return (-1);
return (-1);
}
} else {
return (-1);
}
}
}
return (-1);
}
if (process_user_props(zhp) != 0)
return (-1);
return (0);
}
/*
* Refresh the properties currently stored in the handle.
*/
void
{
}
/*
* Makes a handle from the given dataset name. Used by zfs_open() and
* zfs_iter_* to create child handles on the fly.
*/
{
return (NULL);
top:
return (NULL);
}
/*
* If it is dds_inconsistent, then we've caught it in
* the middle of a 'zfs receive' or 'zfs destroy', and
* it is inconsistent from the ZPL's point of view, so
* can't be mounted. However, it could also be that we
* have crashed in the middle of one of those
* operations, in which case we need to get rid of the
* inconsistent state. We do that by either rolling
* back to the previous snapshot (which will fail if
* there is none), or destroying the filesystem. Note
* that if we are still in the middle of an active
* 'receive' or 'destroy', then the rollback and destroy
* will fail with EBUSY and we will drive on as usual.
*/
} else {
}
/* If we can successfully roll it back, reget the stats */
goto top;
/*
* If we can sucessfully destroy it, pretend that it
* never existed.
*/
return (NULL);
}
}
/*
* We've managed to open the dataset and gather statistics. Determine
* the high-level type.
*/
else
abort();
else
abort(); /* we should never see any other types */
return (zhp);
}
/*
* Opens the given snapshot, filesystem, or volume. The 'types'
* argument is a mask of acceptable types. The function will print an
* appropriate error message and return NULL if it can't be opened.
*/
{
char errbuf[1024];
/*
* Validate the name before we even try to open it.
*/
"invalid dataset name"));
return (NULL);
}
/*
* Try to get stats for the dataset, which will tell us if it exists.
*/
errno = 0;
return (NULL);
}
return (NULL);
}
return (zhp);
}
/*
* Release a ZFS handle. Nothing to do but free the associated memory.
*/
void
{
if (zhp->zfs_mntopts)
}
/*
* Given a numeric suffix, convert the value into a number of bits that the
* resulting value must be shifted.
*/
static int
{
const char *ends = "BKMGTPEZ";
int i;
if (buf[0] == '\0')
return (0);
break;
}
"invalid numeric suffix '%s'"), buf);
return (-1);
}
/*
* We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't
* allow 'BB' - that's just weird.
*/
return (10*i);
"invalid numeric suffix '%s'"), buf);
return (-1);
}
/*
* Convert a string of the form '100G' into a real number. Used when setting
* properties or creating a volume. 'buf' is used to place an extended error
* message for the caller to use.
*/
static int
{
char *end;
int shift;
*num = 0;
/* Check to see if this looks like a number. */
if (hdl)
"bad numeric value '%s'"), value);
return (-1);
}
/* Rely on stroll() to process the numeric portion. */
errno = 0;
/*
* Check for ERANGE, which indicates that the value is too large to fit
* in a 64-bit value.
*/
if (hdl)
"numeric value is too large"));
return (-1);
}
/*
* If we have a decimal value, then do the computation with floating
* point arithmetic. Otherwise, use standard arithmetic.
*/
if (*end == '.') {
return (-1);
if (fval > UINT64_MAX) {
if (hdl)
"numeric value is too large"));
return (-1);
}
} else {
return (-1);
/* Check for overflow */
if (hdl)
"numeric value is too large"));
return (-1);
}
}
return (0);
}
int
{
}
/*
* The prop_parse_*() functions are designed to allow flexibility in callers
* when setting properties. At the DSL layer, all properties are either 64-bit
* numbers or strings. We want the user to be able to ignore this fact and
* specify properties as native values (boolean, for example) or as strings (to
* simplify command line utilities). This also handles converting index types
* (compression, checksum, etc) from strings to their on-disk index.
*/
static int
{
switch (nvpair_type(elem)) {
case DATA_TYPE_STRING:
{
char *value;
ret = 1;
ret = 0;
} else {
"property '%s' must be 'on' or 'off'"),
nvpair_name(elem));
return (-1);
}
break;
}
case DATA_TYPE_UINT64:
{
if (ret > 1) {
"'%s' must be a boolean value"),
nvpair_name(elem));
return (-1);
}
break;
}
case DATA_TYPE_BOOLEAN_VALUE:
{
break;
}
default:
"'%s' must be a boolean value"),
nvpair_name(elem));
return (-1);
}
return (0);
}
static int
{
switch (nvpair_type(elem)) {
case DATA_TYPE_STRING:
{
char *value;
ret = 0;
return (-1);
}
break;
}
case DATA_TYPE_UINT64:
break;
default:
"'%s' must be a number"),
nvpair_name(elem));
return (-1);
}
/*
* Quota special: force 'none' and don't allow 0.
*/
"use 'none' to disable quota"));
return (-1);
}
return (0);
}
static int
{
char *value;
"'%s' must be a string"), propname);
return (-1);
}
"'%s' must be one of '%s'"), propname,
return (-1);
}
return (0);
}
/*
* Given an nvlist of properties to set, validates that they are correct, and
* parses any numeric properties (index, boolean, etc) if they are specified as
* strings.
*/
static nvlist_t *
{
const char *propname;
char *strval;
return (NULL);
}
if (type == ZFS_TYPE_SNAPSHOT) {
"snaphot properties cannot be modified"));
goto error;
}
/*
* Make sure this property is valid and applies to this type.
*/
if (!zfs_prop_user(propname)) {
"invalid property '%s'"),
propname);
goto error;
} else {
/*
* If this is a user property, make sure it's a
* string, and that it's less than
* ZAP_MAXNAMELEN.
*/
"'%s' must be a string"),
propname);
errbuf);
goto error;
}
"property name '%s' is too long"),
propname);
errbuf);
goto error;
}
}
goto error;
}
continue;
}
/*
* Normalize the name, to get rid of shorthand abbrevations.
*/
"apply to datasets of this type"), propname);
goto error;
}
if (zfs_prop_readonly(prop) &&
propname);
goto error;
}
/*
* Convert any properties to the internal DSL value types.
*/
switch (zfs_prop_get_type(prop)) {
case prop_type_boolean:
goto error;
}
break;
case prop_type_string:
"'%s' must be a string"),
propname);
goto error;
}
"'%s' is too long"), propname);
goto error;
}
break;
case prop_type_number:
goto error;
}
break;
case prop_type_index:
goto error;
}
break;
default:
abort();
}
/*
* Add the result to our return set of properties.
*/
if (strval) {
goto error;
}
goto error;
}
/*
* Perform some additional checks for specific properties.
*/
switch (prop) {
case ZFS_PROP_RECORDSIZE:
case ZFS_PROP_VOLBLOCKSIZE:
/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
if (intval < SPA_MINBLOCKSIZE ||
"'%s' must be power of 2 from %u "
"to %uk"), propname,
goto error;
}
break;
case ZFS_PROP_SHAREISCSI:
"'%s' must be 'on', 'off', or 'type=disk'"),
propname);
goto error;
}
break;
case ZFS_PROP_MOUNTPOINT:
break;
if (strval[0] != '/') {
"'%s' must be an absolute path, "
"'none', or 'legacy'"), propname);
goto error;
}
/*FALLTHRU*/
case ZFS_PROP_SHARENFS:
/*
* For the mountpoint and sharenfs properties, check if
* it can be set in a global/non-global zone based on
* the zoned property value:
*
* global zone non-global zone
* --------------------------------------------------
* zoned=on mountpoint (no) mountpoint (yes)
* sharenfs (no) sharenfs (no)
*
* zoned=off mountpoint (yes) N/A
* sharenfs (yes)
*/
if (zoned) {
if (getzoneid() == GLOBAL_ZONEID) {
"'%s' cannot be set on "
"dataset in a non-global zone"),
propname);
errbuf);
goto error;
} else if (prop == ZFS_PROP_SHARENFS) {
"'%s' cannot be set in "
"a non-global zone"), propname);
errbuf);
goto error;
}
} else if (getzoneid() != GLOBAL_ZONEID) {
/*
* If zoned property is 'off', this must be in
* a globle zone. If not, something is wrong.
*/
"'%s' cannot be set while dataset "
"'zoned' property is set"), propname);
goto error;
}
break;
}
/*
* For changes to existing volumes, we have some additional
* checks to enforce.
*/
char buf[64];
switch (prop) {
case ZFS_PROP_RESERVATION:
"'%s' is greater than current "
"volume size"), propname);
errbuf);
goto error;
}
break;
case ZFS_PROP_VOLSIZE:
sizeof (buf));
"'%s' must be a multiple of "
"volume block size (%s)"),
errbuf);
goto error;
}
if (intval == 0) {
"'%s' cannot be zero"),
propname);
errbuf);
goto error;
}
break;
}
}
}
/*
* If this is an existing volume, and someone is setting the volsize,
* make sure that it matches the reservation, or add it if necessary.
*/
&intval) == 0) {
if (old_volsize == old_reservation &&
&new_reservation) != 0) {
if (nvlist_add_uint64(ret,
intval) != 0) {
goto error;
}
}
}
return (ret);
return (NULL);
}
/*
* Given a property name and value, set the property for the given dataset.
*/
int
{
int ret = -1;
char errbuf[1024];
goto error;
}
goto error;
goto error;
"child dataset with inherited mountpoint is used "
"in a non-global zone"));
goto error;
}
goto error;
/*
* Execute the corresponding ioctl() to set this property.
*/
goto error;
if (ret != 0) {
switch (errno) {
case ENOSPC:
/*
* For quotas and reservations, ENOSPC indicates
* something different; setting a quota or reservation
* doesn't use any disk space.
*/
switch (prop) {
case ZFS_PROP_QUOTA:
"size is less than current used or "
"reserved space"));
break;
case ZFS_PROP_RESERVATION:
"size is greater than available space"));
break;
default:
break;
}
break;
case EBUSY:
if (prop == ZFS_PROP_VOLBLOCKSIZE)
else
break;
case EROFS:
break;
case EOVERFLOW:
/*
* This platform can't address a volume this big.
*/
#ifdef _ILP32
if (prop == ZFS_PROP_VOLSIZE) {
break;
}
#endif
/* FALLTHROUGH */
default:
}
} else {
/*
* Refresh the statistics so the new property value
* is reflected.
*/
}
if (cl)
return (ret);
}
/*
* Given a property, inherit the value from the parent dataset.
*/
int
{
int ret;
char errbuf[1024];
/*
* For user properties, the amount of work we have to do is very
* small, so just do it here.
*/
if (!zfs_prop_user(propname)) {
"invalid property"));
}
ZFS_IOC_SET_PROP, &zc) != 0)
return (0);
}
/*
* Verify that this property is inheritable.
*/
if (zfs_prop_readonly(prop))
if (!zfs_prop_inheritable(prop))
/*
* Check to see if the value applies to this type
*/
"dataset is used in a non-global zone"));
}
/*
* Determine datasets which will be affected by this change, if any.
*/
return (-1);
"child dataset with inherited mountpoint is used "
"in a non-global zone"));
goto error;
}
goto error;
ZFS_IOC_SET_PROP, &zc)) != 0) {
} else {
goto error;
/*
* Refresh the statistics so the new property is reflected.
*/
}
return (ret);
}
static void
{
if (value)
else
}
/*
* True DSL properties are stored in an nvlist. The following two functions
* extract them appropriately.
*/
static uint64_t
{
} else {
*source = "";
}
return (value);
}
static char *
{
char *value;
} else {
value = "";
*source = "";
}
return (value);
}
/*
* Internal function for getting a numeric property. Both zfs_prop_get() and
* zfs_prop_get_int() are built using this interface.
*
* Certain properties can be overridden using 'mount -o'. In this case, scan
* If they differ from the on-disk values, report the current values and mark
* the source "temporary".
*/
static int
{
char *mntopt_off = NULL;
switch (prop) {
case ZFS_PROP_ATIME:
break;
case ZFS_PROP_DEVICES:
break;
case ZFS_PROP_EXEC:
break;
case ZFS_PROP_READONLY:
break;
case ZFS_PROP_SETUID:
break;
case ZFS_PROP_XATTR:
break;
}
/*
* Because looking up the mount options is potentially expensive
* we're looking up a property which requires its presence.
*/
if (!zhp->zfs_mntcheck &&
return (-1);
}
}
else
switch (prop) {
case ZFS_PROP_ATIME:
case ZFS_PROP_DEVICES:
case ZFS_PROP_EXEC:
case ZFS_PROP_READONLY:
case ZFS_PROP_SETUID:
case ZFS_PROP_XATTR:
if (src)
*src = ZFS_SRC_TEMPORARY;
if (src)
*src = ZFS_SRC_TEMPORARY;
}
break;
case ZFS_PROP_RECORDSIZE:
case ZFS_PROP_COMPRESSION:
case ZFS_PROP_ZONED:
case ZFS_PROP_CREATION:
case ZFS_PROP_COMPRESSRATIO:
case ZFS_PROP_REFERENCED:
case ZFS_PROP_USED:
case ZFS_PROP_CREATETXG:
case ZFS_PROP_AVAILABLE:
case ZFS_PROP_VOLSIZE:
case ZFS_PROP_VOLBLOCKSIZE:
case ZFS_PROP_CANMOUNT:
break;
case ZFS_PROP_QUOTA:
case ZFS_PROP_RESERVATION:
if (*val == 0)
else
break;
case ZFS_PROP_MOUNTED:
break;
default:
"cannot get non-numeric property"));
}
return (0);
}
/*
* Calculate the source type, given the raw source string.
*/
static void
{
return;
*srctype = ZFS_SRC_NONE;
} else if (source[0] == '\0') {
} else {
*srctype = ZFS_SRC_LOCAL;
} else {
}
}
}
/*
* Retrieve a property from the given object. If 'literal' is specified, then
* numbers are left as exact values. Otherwise, numbers are converted to a
* human-readable form.
*
* Returns 0 on success, or -1 on error.
*/
int
{
char *str;
const char *root;
const char *strval;
/*
* Check to see if this property applies to our object
*/
return (-1);
if (src)
*src = ZFS_SRC_NONE;
switch (prop) {
case ZFS_PROP_ATIME:
case ZFS_PROP_READONLY:
case ZFS_PROP_SETUID:
case ZFS_PROP_ZONED:
case ZFS_PROP_DEVICES:
case ZFS_PROP_EXEC:
case ZFS_PROP_CANMOUNT:
case ZFS_PROP_XATTR:
/*
* Basic boolean values are built on top of
* get_numeric_property().
*/
return (-1);
break;
case ZFS_PROP_AVAILABLE:
case ZFS_PROP_RECORDSIZE:
case ZFS_PROP_CREATETXG:
case ZFS_PROP_REFERENCED:
case ZFS_PROP_USED:
case ZFS_PROP_VOLSIZE:
case ZFS_PROP_VOLBLOCKSIZE:
/*
* Basic numeric values are built on top of
* get_numeric_property().
*/
return (-1);
if (literal)
(u_longlong_t)val);
else
break;
case ZFS_PROP_COMPRESSION:
case ZFS_PROP_CHECKSUM:
case ZFS_PROP_SNAPDIR:
case ZFS_PROP_ACLMODE:
case ZFS_PROP_ACLINHERIT:
break;
case ZFS_PROP_CREATION:
/*
* 'creation' is a time_t stored in the statistics. We convert
* this into a string unless 'literal' is specified.
*/
{
struct tm t;
if (literal ||
&t) == 0)
}
break;
case ZFS_PROP_MOUNTPOINT:
/*
* Getting the precise mountpoint can be tricky.
*
* - for 'none' or 'legacy', return those values.
* - for default mountpoints, construct it as /zfs/<dataset>
* - for inherited mountpoints, we want to take everything
* after our ancestor and append it to the inherited value.
*
* If the pool has an alternate root, we want to prepend that
* root to any values we return.
*/
if (str[0] == '\0') {
} else if (str[0] == '/') {
if (relpath[0] == '/')
relpath++;
str++;
if (relpath[0] == '\0')
else
relpath);
} else {
/* 'legacy' or 'none' */
}
break;
case ZFS_PROP_SHARENFS:
case ZFS_PROP_SHAREISCSI:
case ZFS_PROP_ISCSIOPTIONS:
proplen);
break;
case ZFS_PROP_ORIGIN:
proplen);
/*
* If there is no parent at all, return failure to indicate that
* it doesn't apply to this dataset.
*/
if (propbuf[0] == '\0')
return (-1);
break;
case ZFS_PROP_QUOTA:
case ZFS_PROP_RESERVATION:
return (-1);
/*
* If quota or reservation is 0, we translate this into 'none'
* (unless literal is set), and indicate that it's the default
* value. Otherwise, we print the number nicely and indicate
* that its set locally.
*/
if (val == 0) {
if (literal)
else
} else {
if (literal)
(u_longlong_t)val);
else
}
break;
case ZFS_PROP_COMPRESSRATIO:
return (-1);
break;
case ZFS_PROP_TYPE:
case ZFS_TYPE_FILESYSTEM:
str = "filesystem";
break;
case ZFS_TYPE_VOLUME:
str = "volume";
break;
case ZFS_TYPE_SNAPSHOT:
str = "snapshot";
break;
default:
abort();
}
break;
case ZFS_PROP_MOUNTED:
/*
* The 'mounted' property is a pseudo-property that described
* whether the filesystem is currently mounted. Even though
* it's a boolean value, the typical values of "on" and "off"
* don't make sense, so we translate to "yes" and "no".
*/
return (-1);
if (val)
else
break;
case ZFS_PROP_NAME:
/*
* The 'name' property is a pseudo-property derived from the
* dataset name. It is presented as a real property to simplify
* consumers.
*/
break;
default:
abort();
}
return (0);
}
/*
* Utility function to get the given numeric property. Does no validation that
* the given property is the appropriate type; should only be used with
* hard-coded property types.
*/
{
char *source;
return (val);
}
/*
* Similar to zfs_prop_get(), but returns the value as an integer.
*/
int
{
char *source;
/*
* Check to see if this property applies to our object
*/
zfs_prop_to_name(prop)));
if (src)
*src = ZFS_SRC_NONE;
return (-1);
return (0);
}
/*
* Returns the name of the given zfs handle.
*/
const char *
{
}
/*
* Returns the type of the given zfs handle.
*/
{
}
/*
* Iterate over all child filesystems
*/
int
{
int ret;
/*
* Ignore private dataset names.
*/
continue;
/*
* Silently ignore errors, as the only plausible explanation is
* that the pool has since been removed.
*/
continue;
return (ret);
}
/*
* An errno value of ESRCH indicates normal completion. If ENOENT is
* returned, then the underlying dataset has been removed since we
* obtained the handle.
*/
return (0);
}
/*
* Iterate over all snapshots
*/
int
{
int ret;
&zc) == 0;
continue;
return (ret);
}
/*
* An errno value of ESRCH indicates normal completion. If ENOENT is
* returned, then the underlying dataset has been removed since we
* obtained the handle. Silently ignore this case, and return success.
*/
return (0);
}
/*
* Iterate over all children, snapshots and filesystems
*/
int
{
int ret;
return (ret);
}
/*
* Given a complete name, return just the portion that refers to the parent.
* Can return NULL if this is a pool.
*/
static int
{
char *loc;
return (-1);
return (0);
}
/*
* Checks to make sure that the given path has a parent, and that it exists. We
* also fetch the 'zoned' property, which is used to validate property settings
* when creating new datasets.
*/
static int
{
char parent[ZFS_MAXNAMELEN];
char *slash;
char errbuf[1024];
path);
/* get parent, and check to see if this is just a pool */
"missing dataset name"));
}
/* check to see if the pool exists */
}
/* check to see if the parent dataset exists */
switch (errno) {
case ENOENT:
"parent does not exist"));
default:
}
}
/* we are in a non-global zone, but parent is in the global zone */
return (-1);
}
/* make sure parent is a filesystem */
"parent is not a filesystem"));
return (-1);
}
return (0);
}
/*
* Create a new filesystem or volume.
*/
int
{
int ret;
char errbuf[1024];
"cannot create '%s'"), path);
/* validate the path, taking care to note the extended error message */
/* validate parents exist */
return (-1);
/*
* The failure modes when creating a dataset of a different type over
* one that already exists is a little strange. In particular, if you
* try to create a dataset on top of an existing dataset, the ioctl()
* will return ENOENT, not EEXIST. To prevent this from happening, we
* first try to see if the dataset exists.
*/
"dataset already exists"));
}
if (type == ZFS_TYPE_VOLUME)
else
return (-1);
if (type == ZFS_TYPE_VOLUME) {
/*
* If we are creating a volume, the size and block size must
* satisfy a few restraints. First, the blocksize must be a
* valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
* volsize must be a multiple of the block size, and cannot be
* zero.
*/
"missing volume size"));
}
&blocksize)) != 0) {
} else {
"missing volume block size"));
}
}
if (size == 0) {
"volume size cannot be zero"));
}
"volume size must be a multiple of volume block "
"size"));
}
}
if (props &&
return (-1);
/* create the dataset */
/* check for failure */
if (ret != 0) {
char parent[ZFS_MAXNAMELEN];
switch (errno) {
case ENOENT:
"no such parent '%s'"), parent);
case EINVAL:
"parent '%s' is not a filesysem"), parent);
case EDOM:
"volume block size must be power of 2 from "
"%u to %uk"),
#ifdef _ILP32
case EOVERFLOW:
/*
* This platform can't address a volume this big.
*/
if (type == ZFS_TYPE_VOLUME)
errbuf));
#endif
/* FALLTHROUGH */
default:
}
}
return (0);
}
/*
* Destroys the given dataset. The caller must make sure that the filesystem
* isn't mounted, and that there are no active dependents.
*/
int
{
if (ZFS_IS_VOLUME(zhp)) {
/*
* Unconditionally unshare this zvol ignoring failure as it
* indicates only that the volume wasn't shared initially.
*/
(void) zfs_unshare_iscsi(zhp);
return (-1);
} else {
}
}
return (0);
}
struct destroydata {
char *snapname;
};
static int
{
char name[ZFS_MAXNAMELEN];
int rv;
if (szhp) {
}
/*
* NB: this is simply a best-effort. We don't want to
* return an error, because then we wouldn't visit all
* the volumes.
*/
}
if (closezhp)
return (rv);
}
/*
* Destroys all snapshots with the given name in zhp & descendants.
*/
int
{
int ret;
struct destroydata dd = { 0 };
}
if (ret != 0) {
char errbuf[1024];
switch (errno) {
case EEXIST:
"snapshot is cloned"));
default:
errbuf));
}
}
return (0);
}
/*
* Clones the given dataset. The target must be of the same type as the source.
*/
int
{
char parent[ZFS_MAXNAMELEN];
int ret;
char errbuf[1024];
"cannot create '%s'"), target);
/* validate the target name */
/* validate parents exist */
return (-1);
/* do the clone */
if (ZFS_IS_VOLUME(zhp)) {
} else {
}
if (props) {
return (-1);
return (-1);
}
}
if (ret != 0) {
switch (errno) {
case ENOENT:
/*
* The parent doesn't exist. We should have caught this
* above, but there may a race condition that has since
* destroyed the parent.
*
* At this point, we don't know whether it's the source
* that doesn't exist anymore, or whether the target
* dataset doesn't exist.
*/
"no such parent '%s'"), parent);
case EXDEV:
"source and target pools differ"));
errbuf));
default:
errbuf));
}
} else if (ZFS_IS_VOLUME(zhp)) {
}
return (ret);
}
typedef struct promote_data {
char cb_mountpoint[MAXPATHLEN];
const char *cb_target;
const char *cb_errbuf;
static int
{
char snapname[MAXPATHLEN];
int rv = 0;
/* We don't care about snapshots after the pivot point */
return (0);
}
/* Remove the device link if it's a zvol. */
if (ZFS_IS_VOLUME(zhp))
/* Check for conflicting names */
"snapshot name '%s' from origin \n"
"conflicts with '%s' from target"),
}
return (rv);
}
static int
{
/* We don't care about snapshots after the pivot point */
/* Create the device link if it's a zvol. */
if (ZFS_IS_VOLUME(zhp))
}
return (0);
}
/*
* Promotes the given clone fs to be the clone parent.
*/
int
{
char parent[MAXPATHLEN];
char *cp;
int ret;
char errbuf[1024];
"snapshots can not be promoted"));
}
if (parent[0] == '\0') {
"not a cloned filesystem"));
}
*cp = '\0';
/* Walk the snapshots we will be moving */
return (-1);
return (-1);
if (ret != 0) {
return (-1);
}
/* issue the ioctl */
if (ret != 0) {
int save_errno = errno;
switch (save_errno) {
case EEXIST:
/*
* There is a conflicting snapshot name. We
* should have caught this above, but they could
* have renamed something in the mean time.
*/
"conflicting snapshot name from parent '%s'"),
parent);
default:
}
} else {
}
return (ret);
}
static int
{
int ret;
char name[MAXPATHLEN];
/*
* NB: this is simply a best-effort. We don't want to
* return an error, because then we wouldn't visit all
* the volumes.
*/
}
return (ret);
}
/*
* Takes a snapshot of the given dataset
*/
int
{
const char *delim;
char *parent;
int ret;
char errbuf[1024];
"cannot snapshot '%s'"), path);
/* validate the target name */
/* make sure the parent exists and is of the appropriate type */
return (-1);
ZFS_TYPE_VOLUME)) == NULL) {
return (-1);
}
/*
* if it was recursive, the one that actually failed will be in
* zc.zc_name.
*/
(void) zfs_iter_filesystems(zhp,
}
if (ret != 0) {
&zc);
}
}
if (ret != 0)
return (ret);
}
/*
* Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL.
*/
int
{
int ret;
char errbuf[1024];
/* do the ioctl() */
if (fromsnap)
if (ret != 0) {
switch (errno) {
case EXDEV:
"not an ealier snapshot from the same fs"));
case EDQUOT:
case EFBIG:
case EIO:
case ENOLINK:
case ENOSPC:
case ENOSTR:
case ENXIO:
case EPIPE:
case ERANGE:
case EFAULT:
case EROFS:
default:
}
}
return (ret);
}
/*
* Create ancestors of 'target', but not target itself, and not
* ancestors whose names are shorter than prefixlen. Die if
* prefixlen-ancestor does not exist.
*/
static int
{
zfs_handle_t *h;
char *cp;
/* make sure prefix exists */
*cp = '\0';
*cp = '/';
if (h == NULL)
return (-1);
zfs_close(h);
/*
* Attempt to create, mount, and share any ancestor filesystems,
* up to the prefixlen-long one.
*/
const char *opname;
*cp = '\0';
if (h) {
/* it already exists, nothing to do here */
zfs_close(h);
continue;
}
NULL) != 0)
goto ancestorerr;
if (h == NULL)
goto ancestorerr;
goto ancestorerr;
if (zfs_share(h) != 0)
goto ancestorerr;
zfs_close(h);
continue;
return (-1);
}
return (0);
}
/*
* Restores a backup of tosnap from stdin.
*/
int
{
char *cp;
char errbuf[1024];
char chopprefix[ZFS_MAXNAMELEN];
"cannot receive"));
/* read in the BEGIN record */
bytes = 0;
do {
} while (size > 0);
"stream (failed to read first record)"));
}
"stream (bad magic number)"));
}
"0x%llx is supported (stream is version 0x%llx)"),
}
"stream (bad snapshot name)"));
}
/*
* Determine how much of the snapshot name stored in the stream
* we are going to tack on to the name they specified on the
* command line, and how much we are going to chop off.
*
* If they specified a snapshot, chop the entire name stored in
* the stream.
*/
if (isprefix) {
/*
* They specified a fs with -d, we want to tack on
* everything but the pool name stored in the stream
*/
"argument - snapshot not allowed with -d"));
}
*cp = '\0';
/*
* If they specified a filesystem without -d, we want to
* tack on everything after the fs specified in the
* first name from the stream.
*/
*cp = '\0';
}
/*
* Determine name of destination snapshot, store in zc_value.
*/
if (drrb->drr_fromguid) {
/* incremental backup stream */
zfs_handle_t *h;
/* do the recvbackup ioctl to the containing fs */
/* make sure destination fs exists */
if (h == NULL)
return (-1);
if (!dryrun) {
/*
* We need to unmount all the dependents of the dataset
* and the dataset itself. If it's a volume
* then remove device link.
*/
if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
return (-1);
if (changelist_prefix(clp) != 0) {
return (-1);
}
} else {
}
}
zfs_close(h);
} else {
/* full backup stream */
/* Make sure destination fs does not exist */
}
/*
* they're trying to do a recv into a
* nonexistant topmost filesystem.
*/
}
/* Do the recvbackup ioctl to the fs's parent. */
}
}
if (verbose) {
(void) printf("%s %s stream of %s into %s\n",
}
if (dryrun)
return (0);
if (ioctl_err != 0) {
switch (errno) {
case ENODEV:
"most recent snapshot does not match incremental "
"source"));
break;
case ETXTBSY:
"destination has been modified since most recent "
"snapshot"));
break;
case EEXIST:
if (drrb->drr_fromguid == 0) {
/* it's the containing fs that exists */
*cp = '\0';
}
"destination already exists"));
break;
case EINVAL:
break;
case ECKSUM:
"invalid stream (checksum mismatch)"));
break;
default:
}
}
/*
* Mount or recreate the /dev links for the target filesystem
* (if created, or if we tore them down to do an incremental
* restore), and the /dev links for the new snapshot (if
* created). Also mount any children of the target filesystem
* if we did an incremental receive.
*/
zfs_handle_t *h;
*cp = '\0';
*cp = '@';
if (h) {
if (h->zfs_type == ZFS_TYPE_VOLUME) {
} else {
if (drrb->drr_fromguid) {
} else {
}
}
zfs_close(h);
}
}
return (-1);
if (verbose) {
char buf1[64];
char buf2[64];
if (delta == 0)
delta = 1;
}
return (0);
}
/*
* Destroy any more recent snapshots. We invoke this callback on any dependents
* of the snapshot first. If the 'cb_dependent' member is non-zero, then this
* is a dependent and we should just destroy it without checking the transaction
* group.
*/
typedef struct rollback_data {
const char *cb_target; /* the snapshot */
int cb_error;
static int
{
if (!cbp->cb_dependent) {
cbp) != 0)
if (zfs_destroy(zhp) != 0)
else
}
} else {
if (zfs_destroy(zhp) != 0)
else
}
return (0);
}
/*
* Rollback the dataset to its latest snapshot.
*/
static int
{
int ret;
return (-1);
if (ZFS_IS_VOLUME(zhp))
else
/*
* We rely on the consumer to verify that there are no newer snapshots
* for the given dataset. Given these constraints, we can simply pass
* the name on to the ioctl() call. There is still an unlikely race
* condition where the user has taken a snapshot since we verified that
* this was the most recent.
*/
&zc)) != 0) {
}
return (ret);
}
/*
* Given a dataset, rollback to a specific snapshot, discarding any
* data changes since then and making it the active dataset.
*
* Any snapshots more recent than the target are destroyed, along with
* their dependents.
*/
int
{
int ret;
rollback_data_t cb = { 0 };
/*
* Unmount all dependendents of the dataset and the dataset itself.
* The list we need to gather is the same as for doing rename
*/
return (-1);
goto out;
/*
* Destroy all recent snapshots and its dependends.
*/
(void) changelist_postfix(clp);
goto out;
}
/*
* Now that we have verified that the snapshot is the latest,
* rollback to the given snapshot.
*/
if (ret != 0) {
(void) changelist_postfix(clp);
goto out;
}
/*
* We only want to re-mount the filesystem if it was mounted in the
* first place.
*/
out:
return (ret);
}
/*
* Iterate over all dependents for a given dataset. This includes both
* hierarchical dependents (children) and data dependents (snapshots and
* clones). The bulk of the processing occurs in get_dependents() in
*/
int
{
char **dependents;
int i;
int ret = 0;
&dependents, &count) != 0)
return (-1);
for (i = 0; i < count; i++) {
dependents[i])) == NULL)
continue;
break;
}
for (i = 0; i < count; i++)
free(dependents[i]);
return (ret);
}
/*
* Renames the given dataset.
*/
int
{
int ret;
char *delim;
char parent[ZFS_MAXNAMELEN];
char errbuf[1024];
/* if we have the same exact name, just return success */
return (0);
"cannot rename to '%s'"), target);
/*
* Make sure the target name is valid
*/
*target == '@') {
/*
* Snapshot target name is abbreviated,
* reconstruct full dataset name
*/
sizeof (parent));
*(++delim) = '\0';
else
*delim = '\0';
} else {
/*
* Make sure we're renaming within the same dataset.
*/
"snapshots must be part of same "
"dataset"));
errbuf));
}
}
} else {
/* validate parents */
return (-1);
/* make sure we're in the same pool */
"datasets must be within same pool"));
}
/* new name cannot be a child of the current dataset name */
"New dataset name cannot be a descendent of "
"current dataset name"));
}
}
if (getzoneid() == GLOBAL_ZONEID &&
"dataset is used in a non-global zone"));
}
return (-1);
if (changelist_haszonedchild(cl)) {
"child dataset with inherited mountpoint is used "
"in a non-global zone"));
goto error;
}
goto error;
if (ZFS_IS_VOLUME(zhp))
else
/*
* On failure, we still want to remount any filesystems that
* were previously mounted, so we don't alter the system state.
*/
(void) changelist_postfix(cl);
} else {
}
return (ret);
}
/*
* Given a zvol dataset, issue the ioctl to create the appropriate minor node,
* poke devfsadm to create the /dev link, and then wait for the link to appear.
*/
int
{
/*
* Issue the appropriate ioctl.
*/
switch (errno) {
case EEXIST:
/*
* Silently ignore the case where the link already
* exists. This allows 'zfs volinit' to be run multiple
* times without errors.
*/
return (0);
default:
"for '%s'"), dataset));
}
}
/*
* Call devfsadm and wait for the links to magically appear.
*/
"for '%s'"), dataset);
return (-1);
} else {
(void) di_devlink_fini(&dhdl);
}
return (0);
}
/*
* Remove a minor node for the given zvol and the associated /dev links.
*/
int
{
switch (errno) {
case ENXIO:
/*
* Silently ignore the case where the link no longer
* exists, so that 'zfs volfini' can be run multiple
* times without errors.
*/
return (0);
default:
"links for '%s'"), dataset));
}
}
return (0);
}
nvlist_t *
{
return (zhp->zfs_user_props);
}
/*
* Given a comma-separated list of properties, contruct a property list
* containing both user-defined and native properties. This function will
* return a NULL list if 'all' is specified, which can later be expanded on a
* per-dataset basis by zfs_expand_proplist().
*/
int
{
int i;
char *s, *p;
char c;
/*
* If 'all' is specified, return a NULL list.
*/
return (0);
/*
* If no fields were specified, return an error.
*/
if (fields[0] == '\0') {
"no properties specified"));
"bad property list")));
}
/*
* It would be nice to use getsubopt() here, but the inclusion of column
* aliases makes this more effort than it's worth.
*/
s = fields;
while (*s != '\0') {
p = s + len;
} else {
len = p - s;
}
/*
* Check for empty options.
*/
if (len == 0) {
"empty property name"));
}
/*
* Check all regular property names.
*/
c = s[len];
s[len] = '\0';
for (i = 0; i < ZFS_NPROP_ALL; i++) {
break;
}
/*
* If no column is specified, and this isn't a user property,
* return failure.
*/
if (i == ZFS_NPROP_ALL && !zfs_prop_user(s)) {
"invalid property '%s'"), s);
}
return (-1);
if (prop == ZFS_PROP_INVAL) {
if ((entry->pl_user_prop =
return (-1);
}
} else {
}
s = p;
if (c == ',')
s++;
}
return (0);
}
void
{
}
}
/*
* This function is used by 'zfs list' to determine the exact set of columns to
* display, and their maximum widths. This does two main things:
*
* - If this is a list of all properties, then expand the list to include
* all native properties, and set a flag so that for each dataset we look
* for new unique user properties and add them to the list.
*
* - For non fixed-width properties, keep track of the maximum width seen
* so that we can size the column appropriately.
*/
int
{
char *strval;
char buf[ZFS_MAXPROPLEN];
/*
* If this is the very first time we've been called for an 'all'
* specification, expand the list to include all native
* properties.
*/
sizeof (zfs_proplist_t))) == NULL)
return (-1);
}
/*
* Add 'name' to the beginning of the list, which is handled
* specially.
*/
sizeof (zfs_proplist_t))) == NULL)
return (-1);
}
/*
* Go through and add any user properties as necessary. We
* start by incrementing our list pointer to the first
* non-native property.
*/
break;
}
/*
* See if we've already found this property in our list.
*/
nvpair_name(elem)) == 0)
break;
}
sizeof (zfs_proplist_t))) == NULL ||
return (-1);
}
}
}
}
/*
* Now go through and check the width of any non-fixed columns
*/
continue;
}
} else if (nvlist_lookup_nvlist(userprops,
ZFS_PROP_VALUE, &strval) == 0);
}
}
return (0);
}