libzfs_dataset.c revision 31fd60d36d9ae794bbedd5e834b8be6d412a853f
/*
* 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 <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"), buflen);
break;
case NAME_ERR_LEADING_SLASH:
"leading slash"), buflen);
break;
case NAME_ERR_EMPTY_COMPONENT:
"empty component"), buflen);
break;
case NAME_ERR_TRAILING_SLASH:
"trailing slash"), buflen);
break;
case NAME_ERR_INVALCHAR:
"'%c'"), what);
break;
case NAME_ERR_MULTIPLE_AT:
"multiple '@' delimiters"), buflen);
break;
}
}
return (0);
}
"snapshot delimiter '@'"), buflen);
return (0);
}
return (1);
}
int
{
}
/*
* Utility function to gather stats (objset and zpl) for the given object.
*/
static int
{
} else {
return (-1);
}
}
sizeof (zc.zc_objset_stats));
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.
*/
make_dataset_handle(const char *path)
{
top:
return (NULL);
}
int err;
/*
* 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
/* we should never see any other dataset 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.
*/
{
/*
* Validate the name before we even try to open it. We don't care about
* the verbose invalid messages here; just report a generic error.
*/
"cannot open '%s': invalid %s name"), path,
return (NULL);
}
/*
* Try to get stats for the dataset, which will tell us if it exists.
*/
errno = 0;
switch (errno) {
case ENOENT:
/*
* The dataset doesn't exist.
*/
"cannot open '%s': no such %s"), path,
break;
case EBUSY:
/*
* We were able to open the dataset but couldn't
* get the stats.
*/
"cannot open '%s': %s is busy"), path,
break;
case ENXIO:
case EIO:
/*
* I/O error from the underlying pool.
*/
"cannot open '%s': I/O error"), path,
break;
default:
}
return (NULL);
}
"not supported for %ss"), path,
return (NULL);
}
return (zhp);
}
/*
* Release a ZFS handle. Nothing to do but free the associated memory.
*/
void
{
if (zhp->zfs_mntopts)
}
struct {
const char *name;
} checksum_table[] = {
{ "on", ZIO_CHECKSUM_ON },
{ "off", ZIO_CHECKSUM_OFF },
{ "fletcher2", ZIO_CHECKSUM_FLETCHER_2 },
{ "fletcher4", ZIO_CHECKSUM_FLETCHER_4 },
{ "sha256", ZIO_CHECKSUM_SHA256 },
{ NULL }
};
struct {
const char *name;
} compress_table[] = {
{ "on", ZIO_COMPRESS_ON },
{ "off", ZIO_COMPRESS_OFF },
{ "lzjb", ZIO_COMPRESS_LZJB },
{ NULL }
};
struct {
const char *name;
} snapdir_table[] = {
{ "hidden", ZFS_SNAPDIR_HIDDEN },
{ "visible", ZFS_SNAPDIR_VISIBLE },
{ NULL }
};
struct {
const char *name;
} acl_mode_table[] = {
{ "discard", DISCARD },
{ "groupmask", GROUPMASK },
{ "passthrough", PASSTHROUGH },
{ NULL }
};
struct {
const char *name;
} acl_inherit_table[] = {
{ "discard", DISCARD },
{ "noallow", NOALLOW },
{ "secure", SECURE },
{ "passthrough", PASSTHROUGH },
{ NULL }
};
/*
* 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;
}
"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);
}
"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. */
"must be a numeric value"), buflen);
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.
*/
"value is too large"), buflen);
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) {
"value is too large"), buflen);
return (-1);
}
} else {
return (-1);
/* Check for overflow */
"value is too large"), buflen);
return (-1);
}
}
return (0);
}
int
{
char buf[1];
}
/*
* Given a property type and value, verify that the value is appropriate. Used
* by zfs_prop_set() and some libzfs consumers.
*/
int
{
char reason[64];
int i;
/*
* Check to see if this a read-only property.
*/
if (zfs_prop_readonly(prop)) {
"cannot set %s property: read-only property"), propname);
return (-1);
}
/* See if the property value is too long */
"bad %s value '%s': value is too long"), propname,
value);
return (-1);
}
/* Perform basic checking based on property type */
switch (zfs_prop_get_type(prop)) {
case prop_type_boolean:
number = 1;
number = 0;
} else {
"bad %s value '%s': must be 'on' or 'off'"),
return (-1);
}
break;
case prop_type_number:
/* treat 'none' as 0 */
number = 0;
break;
}
sizeof (reason)) != 0) {
reason);
return (-1);
}
/* don't allow 0 for quota, use 'none' instead */
"bad %s value '%s': use '%s=none' to disable"),
return (-1);
}
/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
if (prop == ZFS_PROP_RECORDSIZE ||
prop == ZFS_PROP_VOLBLOCKSIZE) {
if (number < SPA_MINBLOCKSIZE ||
"bad %s value '%s': "
"must be power of 2 from %u to %uk"),
return (-1);
}
}
break;
case prop_type_string:
case prop_type_index:
/*
* The two writable string values, 'mountpoint' and
* 'checksum' need special consideration. The 'index' types are
* specified as strings by the user, but passed to the kernel as
* integers.
*/
switch (prop) {
case ZFS_PROP_MOUNTPOINT:
break;
if (value[0] != '/') {
"bad %s value '%s': must be an absolute "
"path, 'none', or 'legacy'"),
return (-1);
}
break;
case ZFS_PROP_CHECKSUM:
== 0) {
break;
}
}
"bad %s value '%s': must be 'on', 'off', "
"'fletcher2', 'fletcher4', or 'sha256'"),
return (-1);
}
break;
case ZFS_PROP_COMPRESSION:
== 0) {
break;
}
}
"bad %s value '%s': must be 'on', 'off', "
"or 'lzjb'"),
return (-1);
}
break;
case ZFS_PROP_SNAPDIR:
break;
}
}
"bad %s value '%s': must be 'hidden' "
"or 'visible'"),
return (-1);
}
break;
case ZFS_PROP_ACLMODE:
== 0) {
break;
}
}
"bad %s value '%s': must be 'discard', "
"'groupmask' or 'passthrough'"),
return (-1);
}
break;
case ZFS_PROP_ACLINHERIT:
== 0) {
break;
}
}
"bad %s value '%s': must be 'discard', "
"'noallow', 'secure' or 'passthrough'"),
return (-1);
}
break;
case ZFS_PROP_SHARENFS:
/*
* Nothing to do for 'sharenfs', this gets passed on to
* share(1M) verbatim.
*/
break;
}
}
return (0);
}
/*
* Given a property name and value, set the property for the given dataset.
*/
int
{
int ret;
return (-1);
/*
* Check to see if the value applies to this type
*/
"cannot set %s for '%s': property does not apply to %ss"),
return (-1);
}
/*
* 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 (getzoneid() == GLOBAL_ZONEID) {
"cannot set %s for '%s': "
"dataset is used in a non-global zone"),
return (-1);
} else if (prop == ZFS_PROP_SHARENFS) {
"cannot set %s for '%s': filesystems "
"cannot be shared in a non-global zone"),
return (-1);
}
} else if (getzoneid() != GLOBAL_ZONEID) {
/*
* If zoned property is 'off', this must be in
* a globle zone. If not, something is wrong.
*/
"cannot set %s for '%s': dataset is "
"used in a non-global zone, but 'zoned' "
"property is not set"),
return (-1);
}
}
return (-1);
"child dataset with inherited mountpoint is used "
"in a non-global zone"),
ret = -1;
goto error;
}
goto error;
/*
* Execute the corresponding ioctl() to set this property.
*/
switch (prop) {
case ZFS_PROP_QUOTA:
break;
case ZFS_PROP_RESERVATION:
break;
case ZFS_PROP_MOUNTPOINT:
case ZFS_PROP_SHARENFS:
/*
* These properties are passed down as real strings.
*/
sizeof (zc.zc_prop_name));
sizeof (zc.zc_prop_value));
break;
case ZFS_PROP_VOLSIZE:
break;
case ZFS_PROP_VOLBLOCKSIZE:
break;
default:
sizeof (zc.zc_prop_name));
/* LINTED - alignment */
break;
}
if (ret != 0) {
switch (errno) {
case EPERM:
"cannot set %s for '%s': permission "
break;
case ENOENT:
break;
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:
"for '%s': size is less than current "
"used or reserved space"), propname,
break;
case ZFS_PROP_RESERVATION:
"for '%s': size is greater than available "
break;
default:
"cannot set %s for '%s': out of space"),
break;
}
break;
case EBUSY:
if (prop == ZFS_PROP_VOLBLOCKSIZE) {
"cannot set %s for '%s': "
"volume already contains data"),
} else {
}
break;
case EROFS:
break;
case EOVERFLOW:
/*
* This platform can't address a volume this big.
*/
#ifdef _ILP32
if (prop == ZFS_PROP_VOLSIZE) {
"cannot set %s for '%s': "
"max volume size is 1TB on 32-bit systems"),
break;
}
#endif
default:
}
} else {
/*
* Refresh the statistics so the new property value
* is reflected.
*/
goto error;
}
return (ret);
}
/*
* Given a property, inherit the value from the parent dataset.
*/
int
{
int ret;
/*
* Verify that this property is inheritable.
*/
if (zfs_prop_readonly(prop)) {
"cannot inherit %s for '%s': property is read-only"),
return (-1);
}
if (!zfs_prop_inheritable(prop)) {
"cannot inherit %s for '%s': property is not inheritable"),
return (-1);
}
/*
* Check to see if the value applies to this type
*/
"cannot inherit %s for '%s': property does "
return (-1);
}
"dataset is used in a non-global zone"), propname,
return (-1);
}
/*
* 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"),
ret = -1;
goto error;
}
goto error;
zc.zc_numints = 0;
switch (errno) {
case EPERM:
"cannot inherit %s for '%s': permission "
break;
case ENOENT:
break;
case ENOSPC:
"cannot inherit %s for '%s': "
break;
default:
}
} 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 uint64_t
char **source)
{
else
switch (prop) {
case ZFS_PROP_ATIME:
if (src)
*src = ZFS_SRC_TEMPORARY;
if (src)
*src = ZFS_SRC_TEMPORARY;
}
return (val);
case ZFS_PROP_AVAILABLE:
case ZFS_PROP_DEVICES:
if (src)
*src = ZFS_SRC_TEMPORARY;
if (src)
*src = ZFS_SRC_TEMPORARY;
}
return (val);
case ZFS_PROP_EXEC:
if (src)
*src = ZFS_SRC_TEMPORARY;
if (src)
*src = ZFS_SRC_TEMPORARY;
}
return (val);
case ZFS_PROP_RECORDSIZE:
case ZFS_PROP_COMPRESSION:
case ZFS_PROP_ZONED:
return (val);
case ZFS_PROP_READONLY:
if (src)
*src = ZFS_SRC_TEMPORARY;
if (src)
*src = ZFS_SRC_TEMPORARY;
}
return (val);
case ZFS_PROP_CREATION:
case ZFS_PROP_QUOTA:
else
case ZFS_PROP_RESERVATION:
else
case ZFS_PROP_COMPRESSRATIO:
/*
* Using physical space and logical space, calculate the
* compression ratio. We return the number as a multiple of
* 100, so '2.5x' would be returned as 250.
*/
return (100ULL);
else
case ZFS_PROP_REFERENCED:
/*
* 'referenced' refers to the amount of physical space
* referenced (possibly shared) by this object.
*/
case ZFS_PROP_SETUID:
if (src)
*src = ZFS_SRC_TEMPORARY;
if (src)
*src = ZFS_SRC_TEMPORARY;
}
return (val);
case ZFS_PROP_VOLSIZE:
return (zhp->zfs_volsize);
case ZFS_PROP_VOLBLOCKSIZE:
return (zhp->zfs_volblocksize);
case ZFS_PROP_USED:
case ZFS_PROP_CREATETXG:
case ZFS_PROP_MOUNTED:
/*
* Unlike other properties, we defer calculation of 'MOUNTED'
* until actually requested. This is because the getmntany()
* call can be extremely expensive on systems with a large
* number of filesystems, and the property isn't needed in
* normal use cases.
*/
rewind(zfs_mnttab());
zhp->zfs_mntopts =
}
default:
}
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;
int i;
const char *root;
/*
* 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:
/*
* Basic boolean values are built on top of
* get_numeric_property().
*/
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().
*/
if (literal)
else
break;
case ZFS_PROP_COMPRESSION:
break;
}
break;
case ZFS_PROP_CHECKSUM:
break;
}
break;
case ZFS_PROP_SNAPDIR:
break;
}
break;
case ZFS_PROP_ACLMODE:
break;
}
break;
case ZFS_PROP_ACLINHERIT:
break;
}
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:
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:
/*
* 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)
else
}
break;
case ZFS_PROP_COMPRESSRATIO:
val % 100);
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:
}
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".
*/
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:
}
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;
}
/*
* 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
*/
return (-1);
if (src)
*src = ZFS_SRC_NONE;
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;
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.
*/
static int
{
char parent[ZFS_MAXNAMELEN];
char *slash;
/* get parent, and check to see if this is just a pool */
"cannot create '%s': missing dataset name"),
"use 'zpool create' to create a storage pool"));
return (-1);
}
/* check to see if the pool exists */
return (-1);
}
/* check to see if the parent dataset exists */
switch (errno) {
case ENOENT:
"cannot create '%s': parent does not exist"), path);
return (-1);
default:
}
}
/* we are in a non-global zone, but parent is in the global zone */
if (getzoneid() != GLOBAL_ZONEID &&
"cannot create '%s': permission denied"), path);
return (-1);
}
/* make sure parent is a filesystem */
"cannot create '%s': parent is not a filesystem"),
path);
return (-1);
}
return (0);
}
/*
* Create a new filesystem or volume. 'sizestr' and 'blocksizestr' are used
* only for volumes, and indicate the size and blocksize of the volume.
*/
int
const char *sizestr, const char *blocksizestr)
{
char reason[64];
int ret;
/* convert sizestr into integer size */
return (-1);
}
/* convert blocksizestr into integer blocksize */
return (-1);
}
/* validate the path, taking care to note the extended error message */
"use 'zfs snapshot' to create a snapshot"));
return (-1);
}
/* 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.
*/
"cannot create '%s': dataset exists"), path);
return (-1);
}
if (type == ZFS_TYPE_VOLUME)
else
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.
*/
if (size == 0) {
"bad volume size '%s': cannot be zero"), sizestr);
return (-1);
}
if (blocksize < SPA_MINBLOCKSIZE ||
"bad volume block size '%s': "
"must be power of 2 from %u to %uk"),
return (-1);
}
char buf[64];
"bad volume size '%s': "
"must be multiple of volume block size (%s)"),
return (-1);
}
}
/* create the dataset */
/* check for failure */
if (ret != 0) {
char parent[ZFS_MAXNAMELEN];
switch (errno) {
case ENOENT:
/*
* The parent dataset has been deleted since our
* previous check.
*/
"cannot create '%s': no such parent '%s'"),
break;
case EPERM:
/*
* The user doesn't have permission to create a new
* dataset here.
*/
"cannot create '%s': permission denied"), path);
break;
case EDQUOT:
case ENOSPC:
/*
* The parent dataset does not have enough free space
* to create a new dataset.
*/
"cannot create '%s': not enough space in '%s'"),
break;
case EEXIST:
/*
* The target dataset already exists. We should have
* caught this above, but there may be some unexplained
* race condition.
*/
"cannot create '%s': dataset exists"), path);
break;
case EINVAL:
/*
* The target dataset does not support children.
*/
"cannot create '%s': children unsupported in '%s'"),
break;
case EDOM:
"must be power of 2 from %u to %uk"),
break;
#ifdef _ILP32
case EOVERFLOW:
/*
* This platform can't address a volume this big.
*/
if (type == ZFS_TYPE_VOLUME) {
"cannot create '%s': "
"max volume size is 1TB on 32-bit systems"),
path);
break;
}
#endif
default:
}
return (-1);
}
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
{
int ret;
/*
* We use the check for 'zfs_volblocksize' instead of ZFS_TYPE_VOLUME
* so that we do the right thing for snapshots of volumes.
*/
if (zhp->zfs_volblocksize != 0) {
return (-1);
} else {
}
if (ret != 0) {
switch (errno) {
case EPERM:
/*
* We don't have permission to destroy this dataset.
*/
"cannot destroy '%s': permission denied"),
break;
case ENOENT:
/*
* We've hit a race condition where the dataset has been
* destroyed since we opened it.
*/
"cannot destroy '%s': no such %s"),
break;
case EBUSY:
/*
* Even if we destroy all children, there is a chance we
* can hit this case if:
*
* - A child dataset has since been created
* - A filesystem is mounted
*
* This error message is awful, but hopefully we've
* already caught the common cases (and aborted more
* appropriately) before calling this function. There's
* nothing else we can do at this point.
*/
"cannot destroy '%s': %s is busy"),
break;
default:
}
return (-1);
}
return (0);
}
/*
* Clones the given dataset. The target must be of the same type as the source.
*/
int
{
char reason[64];
char parent[ZFS_MAXNAMELEN];
int ret;
/* validate the target name */
sizeof (reason))) {
"cannot create '%s': %s in filesystem name"), target,
return (-1);
}
/* validate parents exist */
return (-1);
/* do the clone */
if (zhp->zfs_volblocksize != 0)
else
if (ret != 0) {
switch (errno) {
case EPERM:
/*
* The user doesn't have permission to create the clone.
*/
"cannot create '%s': permission denied"),
target);
break;
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.
*/
"cannot create '%s': no such parent '%s'"),
break;
case EDQUOT:
case ENOSPC:
/*
* There is not enough space in the target dataset
*/
"cannot create '%s': not enough space in '%s'"),
break;
case EEXIST:
/*
* The target already exists.
*/
"cannot create '%s': dataset exists"), target);
break;
case EXDEV:
/*
* The source and target pools differ.
*/
"source and target pools differ"), target);
break;
default:
}
} else if (zhp->zfs_volblocksize != 0) {
}
return (ret);
}
/*
* Takes a snapshot of the given dataset
*/
int
zfs_snapshot(const char *path)
{
char reason[64];
const char *delim;
char *parent;
int ret;
/* validate the snapshot name */
sizeof (reason))) {
"cannot snapshot '%s': %s in snapshot name"), path,
reason);
return (-1);
}
/* make sure we have a snapshot */
"cannot snapshot '%s': missing '@' delim in snapshot "
"name"), path);
"use 'zfs create' to create a filesystem"));
return (-1);
}
/* make sure the parent exists and is of the appropriate type */
ZFS_TYPE_VOLUME)) == NULL) {
return (-1);
}
else
if (ret != 0)
}
if (ret != 0) {
switch (errno) {
case EPERM:
/*
* User doesn't have permission to create a snapshot
*/
"permission denied"), path);
break;
case EDQUOT:
case ENOSPC:
/*
* Out of space in parent.
*/
break;
case EEXIST:
/*
* Snapshot already exists.
*/
"snapshot exists"), path);
break;
case ENOENT:
/*
* Shouldn't happen because we verified the parent
* above. But there may be a race condition where it
* has since been removed.
*/
"no such %s"), parent,
break;
default:
}
}
return (ret);
}
/*
* Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL.
*/
int
{
int ret;
/* do the ioctl() */
if (zhp_from) {
} else {
}
if (ret != 0) {
switch (errno) {
case EPERM:
/*
* User doesn't have permission to do a send
*/
break;
case EXDEV:
"cannot send incremental from %s:\n"
"it is not an earlier snapshot from the "
"same fs as %s"),
break;
case ENOENT:
/*
* Shouldn't happen because we verified the parent
* above. But there may be a race condition where it
* has since been removed.
*/
"no such snapshot"));
break;
case EDQUOT:
case EFBIG:
case EIO:
case ENOLINK:
case ENOSPC:
case ENOSTR:
case ENXIO:
case EPIPE:
case ERANGE:
case EFAULT:
case EROFS:
"cannot write stream: %s"),
break;
case EINTR:
"send failed: signal received"));
break;
default:
}
}
return (ret);
}
/*
* Restores a backup of tosnap from stdin.
*/
int
{
char *cp;
/* trim off snapname, if any */
if (cp)
*cp = '\0';
/* read in the BEGIN record */
bytes = 0;
do {
} while (size > 0);
"cannot receive: invalid stream "
"(couldn't read first record)"));
return (-1);
}
"cannot receive: invalid stream "
"(invalid magic number)"));
return (-1);
}
"cannot receive: only stream version 0x%llx is supported, "
"stream is version %llx."),
return (-1);
}
/*
* Determine name of destination snapshot.
*/
if (isprefix) {
"cannot receive: "
"argument to -d must be a filesystem"));
return (-1);
}
else
cp++;
/*
* they specified just a filesystem; tack on the
* snapname from the backup.
*/
"cannot receive: invalid snapshot name"));
return (-1);
}
}
if (drrb->drr_fromguid) {
zfs_handle_t *h;
/* incremental backup stream */
/* do the ioctl to the containing fs */
*cp = '\0';
/* make sure destination fs exists */
if (h == NULL) {
"cannot receive incrememtal stream: destination\n"
"filesystem %s does not exist"),
return (-1);
}
if (!dryrun) {
/* unmount destination fs or remove device link. */
if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
(void) zfs_unmount(h, NULL, 0);
} else {
(void) zvol_remove_link(h->zfs_name);
}
}
zfs_close(h);
} else {
/* full backup stream */
/* make sure they aren't trying to receive into the root */
if (cp)
*cp = '\0';
"cannot receive: destination fs %s already exists"),
return (-1);
}
if (isprefix) {
zfs_handle_t *h;
/* make sure prefix exists */
if (h == NULL) {
"cannot receive: "
"%s is an invalid destination"),
tosnap);
return (-1);
}
zfs_close(h);
/* create any necessary ancestors up to prefix */
/*
* zc.zc_name is now the full name of the snap
* we're restoring into. Attempt to create,
* mount, and share any ancestor filesystems, up
* to the one that was named.
*/
const char *opname;
*cp = '\0';
opname = "create";
continue;
goto ancestorerr;
}
opname = "open";
if (h == NULL)
goto ancestorerr;
opname = "mount";
goto ancestorerr;
opname = "share";
if (zfs_share(h) != 0)
goto ancestorerr;
zfs_close(h);
continue;
"cannot receive: couldn't %s ancestor %s"),
return (-1);
}
}
/* Make sure destination fs does not exist */
*cp = '\0';
"cannot receive full stream: "
"destination filesystem %s already exists"),
return (-1);
}
/* Do the recvbackup ioctl to the fs's parent. */
*cp = '\0';
}
if (verbose) {
(void) printf("%s %s stream of %s into %s\n",
}
if (dryrun)
return (0);
if (ioctl_err != 0) {
switch (errno) {
case ENODEV:
"cannot receive: "
"most recent snapshot does not "
"match incremental source"));
break;
case ETXTBSY:
"cannot receive: "
"destination has been modified since "
"most recent snapshot --\n"
"use 'zfs rollback' to discard changes"));
break;
case EEXIST:
if (drrb->drr_fromguid == 0) {
/* it's the containing fs that exists */
*cp = '\0';
}
"cannot receive to %s: destination already exists"),
break;
case ENOENT:
"cannot receive: destination does not exist"));
break;
case EBUSY:
"cannot receive: destination is in use"));
break;
case ENOSPC:
"cannot receive: out of space"));
break;
case EDQUOT:
"cannot receive: quota exceeded"));
break;
case EINTR:
"receive failed: signal received"));
break;
case EINVAL:
"cannot receive: invalid stream"));
break;
case ECKSUM:
"cannot receive: invalid stream "
"(checksum mismatch)"));
break;
case EPERM:
"cannot receive: permission denied"));
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).
*/
zfs_handle_t *h;
*cp = '\0';
*cp = '@';
if (h) {
if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
} 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;
int cb_dependent;
static int
{
if (!cbp->cb_dependent) {
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 (zhp->zfs_volblocksize != 0)
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.
*/
switch (errno) {
case EPERM:
/*
* The user doesn't have permission to rollback the
* given dataset.
*/
break;
case EDQUOT:
case ENOSPC:
/*
* The parent dataset doesn't have enough space to
* rollback to the last snapshot.
*/
{
char parent[ZFS_MAXNAMELEN];
sizeof (parent));
"rollback '%s': out of space"), parent);
}
break;
case ENOENT:
/*
* The dataset doesn't exist. This shouldn't happen
* except in race conditions.
*/
break;
case EBUSY:
/*
* The filesystem is busy. This should have been caught
* by the caller before getting here, but there may be
* an unexpected problem.
*/
break;
default:
}
}
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;
for (i = 0; i < count; i++) {
continue;
break;
}
for (i = 0; i < count; i++)
free(dependents[i]);
return (ret);
}
/*
* Renames the given dataset.
*/
int
{
int ret;
char reason[64];
char *delim;
char parent[ZFS_MAXNAMELEN];
/* if we have the same exact name, just return success */
return (0);
/*
* Make sure the target name is valid
*/
sizeof (reason))) {
return (-1);
}
"cannot rename to '%s': not a snapshot"), target);
return (-1);
}
/*
* Make sure we're renaming within the same dataset.
*/
"cannot rename to '%s': snapshots must be part "
"of same dataset"), target);
return (-1);
}
} else {
/* validate parents */
return (-1);
/* make sure we're in the same pool */
"cannot rename to '%s': "
"datasets must be within same pool"), target);
return (-1);
}
}
if (getzoneid() == GLOBAL_ZONEID &&
return (-1);
}
return (1);
if (changelist_haszonedchild(cl)) {
"cannot rename '%s': child dataset with inherited "
ret = -1;
goto error;
}
goto error;
if (zhp->zfs_volblocksize != 0)
else
switch (errno) {
case EPERM:
/*
* The user doesn't have permission to rename the
* given dataset.
*/
break;
case EDQUOT:
case ENOSPC:
/*
* Not enough space in the parent dataset.
*/
"rename '%s': not enough space in '%s'"),
break;
case ENOENT:
/*
* The destination doesn't exist.
*/
"to '%s': destination doesn't exist"),
break;
case EEXIST:
/*
* The destination already exists.
*/
"to '%s': destination already exists"),
break;
case EBUSY:
/*
* The filesystem is busy. This should have been caught
* by the caller before getting here, but there may be
* an unexpected problem.
*/
break;
default:
}
/*
* 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
zvol_create_link(const char *dataset)
{
/*
* Issue the appropriate ioctl.
*/
switch (errno) {
case EPERM:
"device links for '%s': permission denied"),
dataset);
break;
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:
}
return (-1);
}
/*
* Call devfsadm and wait for the links to magically appear.
*/
"cannot create device links for '%s'"), dataset);
return (-1);
} else {
(void) di_devlink_fini(&hdl);
}
return (0);
}
/*
* Remove a minor node for the given zvol and the associated /dev links.
*/
int
zvol_remove_link(const char *dataset)
{
switch (errno) {
case EPERM:
"device links for '%s': permission denied"),
dataset);
break;
case EBUSY:
"device links for '%s': volume is in use"),
dataset);
break;
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:
}
return (-1);
}
return (0);
}