libzfs_util.c revision 5aba80db367b061758a29154d304977d00d8e4f4
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* 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 <libzfs.h>
#include "libzfs_impl.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_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_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 EIO:
return (-1);
case EINTR:
return (-1);
}
return (0);
}
int
{
}
/*PRINTFLIKE3*/
int
{
return (-1);
}
switch (error) {
case ENXIO:
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;
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;
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)
}
{
}
{
}
/*
* 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
{
}
int
{
char *packed;
return (-1);
if (size)
return (0);
}
/*
* Unpacks an nvlist from the ZFS ioctl command structure.
*/
int
{
return (0);
}