libzfs_util.c revision e6ca193ded880d478cc39e34ef82d4be36e4445d
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Internal utility routines for the ZFS library.
*/
#include <errno.h>
#include <fcntl.h>
#include <libintl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#include <libzfs.h>
#include "libzfs_impl.h"
#include "zfs_prop.h"
int
{
return (hdl->libzfs_error);
}
const char *
{
return (hdl->libzfs_action);
}
const char *
{
return (hdl->libzfs_desc);
switch (hdl->libzfs_error) {
case EZFS_NOMEM:
case EZFS_BADPROP:
case EZFS_PROPREADONLY:
case EZFS_PROPTYPE:
"datasets of this type"));
case EZFS_PROPNONINHERIT:
case EZFS_PROPSPACE:
case EZFS_BADTYPE:
"datasets of this type"));
case EZFS_BUSY:
case EZFS_EXISTS:
case EZFS_NOENT:
case EZFS_BADSTREAM:
case EZFS_DSREADONLY:
case EZFS_VOLTOOBIG:
"this system"));
case EZFS_VOLHASDATA:
case EZFS_INVALIDNAME:
case EZFS_BADRESTORE:
"destination"));
case EZFS_BADBACKUP:
case EZFS_BADTARGET:
case EZFS_NODEVICE:
case EZFS_BADDEV:
case EZFS_NOREPLICAS:
case EZFS_RESILVERING:
case EZFS_BADVERSION:
case EZFS_POOLUNAVAIL:
case EZFS_DEVOVERFLOW:
case EZFS_BADPATH:
case EZFS_CROSSTARGET:
"pools"));
case EZFS_ZONED:
case EZFS_MOUNTFAILED:
case EZFS_UMOUNTFAILED:
case EZFS_UNSHARENFSFAILED:
case EZFS_SHARENFSFAILED:
case EZFS_UNSHARESMBFAILED:
case EZFS_SHARESMBFAILED:
case EZFS_ISCSISVCUNAVAIL:
return (dgettext(TEXT_DOMAIN,
"iscsitgt service need to be enabled by "
"a privileged user"));
case EZFS_DEVLINKS:
case EZFS_PERM:
case EZFS_NOSPC:
case EZFS_IO:
case EZFS_INTR:
case EZFS_ISSPARE:
"spare"));
case EZFS_INVALCONFIG:
case EZFS_RECURSIVE:
case EZFS_NOHISTORY:
case EZFS_UNSHAREISCSIFAILED:
return (dgettext(TEXT_DOMAIN,
"iscsitgtd failed request to unshare"));
case EZFS_SHAREISCSIFAILED:
return (dgettext(TEXT_DOMAIN,
"iscsitgtd failed request to share"));
case EZFS_POOLPROPS:
"pool properties"));
case EZFS_POOL_NOTSUP:
"on this type of pool"));
case EZFS_POOL_INVALARG:
"this pool operation"));
case EZFS_NAMETOOLONG:
case EZFS_OPENFAILED:
case EZFS_NOCAP:
return (dgettext(TEXT_DOMAIN,
"disk capacity information could not be retrieved"));
case EZFS_LABELFAILED:
case EZFS_BADWHO:
case EZFS_BADPERM:
case EZFS_BADPERMSET:
case EZFS_NODELEGATION:
"disabled on pool"));
case EZFS_PERMRDONLY:
" modified"));
case EZFS_BADCACHE:
case EZFS_ISL2CACHE:
case EZFS_VDEVNOTSUP:
"supported"));
case EZFS_NOTSUP:
"on this dataset"));
case EZFS_ACTIVE_SPARE:
"device"));
case EZFS_UNPLAYED_LOGS:
"logs"));
case EZFS_UNKNOWN:
default:
}
}
/*PRINTFLIKE2*/
void
{
}
static void
{
if (hdl->libzfs_desc_active)
hdl->libzfs_desc_active = 0;
else
if (hdl->libzfs_printerr) {
if (error == EZFS_UNKNOWN) {
abort();
}
if (error == EZFS_NOMEM)
exit(1);
}
}
int
{
}
/*PRINTFLIKE3*/
int
{
return (-1);
}
static int
{
switch (error) {
case EPERM:
case EACCES:
return (-1);
case ECANCELED:
return (-1);
case EIO:
return (-1);
case EINTR:
return (-1);
}
return (0);
}
int
{
}
/*PRINTFLIKE3*/
int
{
return (-1);
}
switch (error) {
case ENXIO:
case ENODEV:
break;
case ENOENT:
"dataset does not exist"));
break;
case ENOSPC:
case EDQUOT:
return (-1);
case EEXIST:
"dataset already exists"));
break;
case EBUSY:
"dataset is busy"));
break;
case EROFS:
"snapshot permissions cannot be modified"));
break;
case ENAMETOOLONG:
break;
case ENOTSUP:
break;
case EAGAIN:
"pool I/O is currently suspended"));
break;
default:
break;
}
return (-1);
}
int
{
}
/*PRINTFLIKE3*/
int
{
return (-1);
}
switch (error) {
case ENODEV:
break;
case ENOENT:
break;
case EEXIST:
"pool already exists"));
break;
case EBUSY:
break;
case ENXIO:
"one or more devices is currently unavailable"));
break;
case ENAMETOOLONG:
break;
case ENOTSUP:
break;
case EINVAL:
break;
case ENOSPC:
case EDQUOT:
return (-1);
case EAGAIN:
"pool I/O is currently suspended"));
break;
default:
}
return (-1);
}
/*
* Display an out of memory error message and abort the current program.
*/
int
{
}
/*
* A safe form of malloc() which will die if the allocation fails.
*/
void *
{
void *data;
return (data);
}
/*
* A safe form of realloc(), which also zeroes newly allocated space.
*/
void *
{
void *ret;
return (NULL);
}
return (ret);
}
/*
* A safe form of strdup() which will die if the allocation fails.
*/
char *
{
char *ret;
return (ret);
}
/*
* Convert a number to an appropriately human-readable output.
*/
void
{
int index = 0;
char u;
while (n >= 1024) {
n /= 1024;
index++;
}
u = " KMGTPE"[index];
if (index == 0) {
/*
* If this is an even multiple of the base, always display
* without any decimal precision.
*/
} else {
/*
* We want to choose a precision that reflects the best choice
* for fitting in 5 characters. This can get rather tricky when
* we have numbers that are very close to an order of magnitude.
* For example, when displaying 10239 (which is really 9.999K),
* we want only a single place of precision for 10.0K. We could
* develop some complex heuristics for this, but it's much
* easier just to try each combination in turn.
*/
int i;
for (i = 2; i >= 0; i--) {
break;
}
}
}
void
{
}
libzfs_init(void)
{
return (NULL);
}
return (NULL);
}
return (NULL);
}
return (hdl);
}
void
{
if (hdl->libzfs_mnttab)
if (hdl->libzfs_sharetab)
if (hdl->libzfs_log_str)
}
{
}
{
}
{
}
/*
* Given a name, determine whether or not it's a valid path
* (starts with '/' or "./"). If so, walk the mnttab trying
* to match the device number. If not, treat the path as an
*/
{
int ret;
/*
* It's not a valid path, assume it's a name of type 'argtype'.
*/
}
return (NULL);
}
break;
}
}
if (ret != 0) {
return (NULL);
}
path);
return (NULL);
}
}
/*
* Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
* an ioctl().
*/
int
{
if (len == 0)
len = 2048;
return (-1);
return (0);
}
/*
* Called when an ioctl() which returns an nvlist fails with ENOMEM. This will
* expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
* filled in by the kernel to indicate the actual required size.
*/
int
{
== NULL)
return (-1);
return (0);
}
/*
* Called to free the src and dst nvlists stored in the command structure.
*/
void
{
}
static int
{
char *packed;
return (-1);
return (0);
}
int
{
}
int
{
}
/*
* Unpacks an nvlist from the ZFS ioctl command structure.
*/
int
{
return (0);
}
int
{
int error;
if (hdl->libzfs_log_str) {
}
zc->zc_history = 0;
return (error);
}
/*
* ================================================================
* API shared by zfs and zpool property management
* ================================================================
*/
static void
{
int i;
char *title;
if (cbp->cb_scripted)
return;
/*
* Start with the length of the column headers.
*/
"PROPERTY"));
"VALUE"));
"SOURCE"));
/* first property is always NAME */
/*
* Go through and calculate the widths for each column. For the
* 'source' column, we kludge it up by taking the worst-case scenario of
* inheriting from the longest name. This is acceptable because in the
* majority of cases 'SOURCE' is the last column displayed, and we don't
* use the width anyway. Note that the 'VALUE' column can be oversized,
* if the name of the property is much longer the any values we find.
*/
/*
* 'PROPERTY' column
*/
} else {
}
/*
* 'VALUE' column. The first property is always the 'name'
* zfs_do_get() or when calling zprop_expand_list(), so we
* ignore its width. If the user specified the name property
* to display, then it will be later in the list in any case.
*/
/*
* 'NAME' and 'SOURCE' columns
*/
ZFS_PROP_NAME) &&
}
}
/*
* Now go through and print the headers.
*/
for (i = 0; i < 4; i++) {
switch (cbp->cb_columns[i]) {
case GET_COL_NAME:
break;
case GET_COL_PROPERTY:
break;
case GET_COL_VALUE:
break;
case GET_COL_SOURCE:
break;
default:
}
else
(void) printf("%-*s ",
title);
}
}
(void) printf("\n");
}
/*
* Display a single line of output, according to the settings in the callback
* structure.
*/
void
const char *source)
{
int i;
const char *str;
char buf[128];
/*
* Ignore those source types that the user has chosen to ignore.
*/
return;
for (i = 0; i < 4; i++) {
switch (cbp->cb_columns[i]) {
case GET_COL_NAME:
break;
case GET_COL_PROPERTY:
break;
case GET_COL_VALUE:
break;
case GET_COL_SOURCE:
switch (sourcetype) {
case ZPROP_SRC_NONE:
str = "-";
break;
case ZPROP_SRC_DEFAULT:
str = "default";
break;
case ZPROP_SRC_LOCAL:
str = "local";
break;
case ZPROP_SRC_TEMPORARY:
str = "temporary";
break;
case ZPROP_SRC_INHERITED:
"inherited from %s", source);
break;
}
break;
default:
continue;
}
else if (cbp->cb_scripted)
else
(void) printf("%-*s ",
str);
}
(void) printf("\n");
}
/*
* 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.
*/
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 stroull() 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);
}
/*
* Given a propname=value nvpair to set, parse any numeric properties
* (index, boolean, etc) if they are specified as strings and add the
* resulting nvpair to the returned nvlist.
*
* 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 (numbers, 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.
*/
int
const char *errbuf)
{
const char *propname;
char *value;
if (type == ZFS_TYPE_POOL) {
} else {
}
/*
* Convert any properties to the internal DSL value types.
*/
*ivalp = 0;
switch (proptype) {
case PROP_TYPE_STRING:
if (datatype != DATA_TYPE_STRING) {
goto error;
}
goto error;
}
break;
case PROP_TYPE_NUMBER:
if (datatype == DATA_TYPE_STRING) {
!= 0) {
goto error;
}
} else if (datatype == DATA_TYPE_UINT64) {
} else {
goto error;
}
/*
* Quota special: force 'none' and don't allow 0.
*/
goto error;
}
break;
case PROP_TYPE_INDEX:
if (datatype != DATA_TYPE_STRING) {
goto error;
}
"'%s' must be one of '%s'"), propname,
goto error;
}
break;
default:
abort();
}
/*
* Add the result to our return set of properties.
*/
return (-1);
}
} else {
return (-1);
}
}
return (0);
return (-1);
}
static int
{
int prop;
prop = ZPROP_INVAL;
/*
* When no property table entry can be found, return failure if
* this is a pool property or if this isn't a user-defined
* dataset property,
*/
"invalid property '%s'"), propname);
}
return (-1);
if (prop == ZPROP_INVAL) {
return (-1);
}
} else {
type);
}
return (0);
}
/*
* Given a comma-separated list of properties, construct 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
* by zprop_expand_list().
*/
int
{
/*
* If 'all' is specified, return a NULL list.
*/
return (0);
/*
* If no props were specified, return an error.
*/
if (props[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.
*/
while (*props != '\0') {
char *p;
char c;
} else {
}
/*
* Check for empty options.
*/
if (len == 0) {
"empty property name"));
}
/*
* Check all regular property names.
*/
static char *spaceprops[] = {
"name", "avail", "used", "usedbysnapshots",
"usedbydataset", "usedbyrefreservation",
"usedbychildren", NULL
};
int i;
for (i = 0; spaceprops[i]; i++) {
return (-1);
}
} else {
return (-1);
}
props = p;
if (c == ',')
props++;
}
return (0);
}
void
{
}
}
typedef struct expand_data {
zprop_list_t **last;
int
{
return (ZPROP_INVAL);
return (ZPROP_CONT);
}
int
{
zprop_list_t **last;
/*
* If this is the very first time we've been called for an 'all'
* specification, expand the list to include all native
* properties.
*/
return (-1);
/*
* Add 'name' to the beginning of the list, which is handled
* specially.
*/
return (-1);
}
return (0);
}
int
{
}