1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 225aba80dbSck * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * Internal utility routines for the ZFS library. 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens #include <errno.h> 33fa9e4066Sahrens #include <fcntl.h> 34fa9e4066Sahrens #include <libintl.h> 35fa9e4066Sahrens #include <stdarg.h> 36fa9e4066Sahrens #include <stdio.h> 37fa9e4066Sahrens #include <stdlib.h> 38fa9e4066Sahrens #include <strings.h> 39fa9e4066Sahrens #include <unistd.h> 40fa9e4066Sahrens #include <sys/mnttab.h> 415aba80dbSck #include <sys/mntent.h> 425aba80dbSck #include <sys/types.h> 43fa9e4066Sahrens 44fa9e4066Sahrens #include <libzfs.h> 45fa9e4066Sahrens 46fa9e4066Sahrens #include "libzfs_impl.h" 47fa9e4066Sahrens 4899653d4eSeschrock int 4999653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl) 5099653d4eSeschrock { 5199653d4eSeschrock return (hdl->libzfs_error); 5299653d4eSeschrock } 53fa9e4066Sahrens 5499653d4eSeschrock const char * 5599653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl) 5699653d4eSeschrock { 5799653d4eSeschrock return (hdl->libzfs_action); 5899653d4eSeschrock } 59fa9e4066Sahrens 6099653d4eSeschrock const char * 6199653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl) 6299653d4eSeschrock { 6399653d4eSeschrock if (hdl->libzfs_desc[0] != '\0') 6499653d4eSeschrock return (hdl->libzfs_desc); 6599653d4eSeschrock 6699653d4eSeschrock switch (hdl->libzfs_error) { 6799653d4eSeschrock case EZFS_NOMEM: 6899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of memory")); 6999653d4eSeschrock case EZFS_BADPROP: 7099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid property value")); 7199653d4eSeschrock case EZFS_PROPREADONLY: 7299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "read only property")); 7399653d4eSeschrock case EZFS_PROPTYPE: 7499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 7599653d4eSeschrock "datasets of this type")); 7699653d4eSeschrock case EZFS_PROPNONINHERIT: 7799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 7899653d4eSeschrock case EZFS_PROPSPACE: 7999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 8099653d4eSeschrock case EZFS_BADTYPE: 8199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation not applicable to " 8299653d4eSeschrock "datasets of this type")); 8399653d4eSeschrock case EZFS_BUSY: 8499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 8599653d4eSeschrock case EZFS_EXISTS: 8699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 8799653d4eSeschrock case EZFS_NOENT: 8899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 8999653d4eSeschrock case EZFS_BADSTREAM: 9099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 9199653d4eSeschrock case EZFS_DSREADONLY: 9299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "dataset is read only")); 9399653d4eSeschrock case EZFS_VOLTOOBIG: 9499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 9599653d4eSeschrock "this system")); 9699653d4eSeschrock case EZFS_VOLHASDATA: 9799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "volume has data")); 9899653d4eSeschrock case EZFS_INVALIDNAME: 9999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 10099653d4eSeschrock case EZFS_BADRESTORE: 10199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 10299653d4eSeschrock "destination")); 10399653d4eSeschrock case EZFS_BADBACKUP: 10499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 10599653d4eSeschrock case EZFS_BADTARGET: 10699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 10799653d4eSeschrock case EZFS_NODEVICE: 10899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 10999653d4eSeschrock case EZFS_BADDEV: 11099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 11199653d4eSeschrock case EZFS_NOREPLICAS: 11299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 11399653d4eSeschrock case EZFS_RESILVERING: 11499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 11599653d4eSeschrock case EZFS_BADVERSION: 11699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unsupported version")); 11799653d4eSeschrock case EZFS_POOLUNAVAIL: 11899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 11999653d4eSeschrock case EZFS_DEVOVERFLOW: 12099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 12199653d4eSeschrock case EZFS_BADPATH: 12299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 12399653d4eSeschrock case EZFS_CROSSTARGET: 12499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 12599653d4eSeschrock "pools")); 12699653d4eSeschrock case EZFS_ZONED: 12799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 12899653d4eSeschrock case EZFS_MOUNTFAILED: 12999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 13099653d4eSeschrock case EZFS_UMOUNTFAILED: 13199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 132f3861e1aSahl case EZFS_UNSHARENFSFAILED: 13399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 134f3861e1aSahl case EZFS_SHARENFSFAILED: 13599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 13699653d4eSeschrock case EZFS_DEVLINKS: 13799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "failed to create /dev links")); 13899653d4eSeschrock case EZFS_PERM: 13999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 14099653d4eSeschrock case EZFS_NOSPC: 14199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of space")); 14299653d4eSeschrock case EZFS_IO: 14399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 14499653d4eSeschrock case EZFS_INTR: 14599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "signal received")); 14699653d4eSeschrock case EZFS_ISSPARE: 14799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 14899653d4eSeschrock "spare")); 14999653d4eSeschrock case EZFS_INVALCONFIG: 15099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1513bb79becSeschrock case EZFS_RECURSIVE: 1523bb79becSeschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 15306eeb2adSek case EZFS_NOHISTORY: 15406eeb2adSek return (dgettext(TEXT_DOMAIN, "no history available")); 155f3861e1aSahl case EZFS_UNSHAREISCSIFAILED: 156f3861e1aSahl return (dgettext(TEXT_DOMAIN, 157f3861e1aSahl "iscsitgtd failed request to unshare")); 158f3861e1aSahl case EZFS_SHAREISCSIFAILED: 159f3861e1aSahl return (dgettext(TEXT_DOMAIN, 160f3861e1aSahl "iscsitgtd failed request to share")); 161b1b8ab34Slling case EZFS_POOLPROPS: 162b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 163b1b8ab34Slling "pool properties")); 164b1b8ab34Slling case EZFS_POOL_NOTSUP: 165b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 166b1b8ab34Slling "on this type of pool")); 167b1b8ab34Slling case EZFS_POOL_INVALARG: 168b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 169b1b8ab34Slling "this pool operation")); 170b7661cccSmmusante case EZFS_NAMETOOLONG: 171b7661cccSmmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 172*8488aeb5Staylor case EZFS_OPENFAILED: 173*8488aeb5Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 174*8488aeb5Staylor case EZFS_NOCAP: 175*8488aeb5Staylor return (dgettext(TEXT_DOMAIN, 176*8488aeb5Staylor "disk capacity information could not be retrieved")); 177*8488aeb5Staylor case EZFS_LABELFAILED: 178*8488aeb5Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 17999653d4eSeschrock case EZFS_UNKNOWN: 18099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 18199653d4eSeschrock default: 182c08432ebSeschrock assert(hdl->libzfs_error == 0); 183c08432ebSeschrock return (dgettext(TEXT_DOMAIN, "no error")); 18499653d4eSeschrock } 18599653d4eSeschrock } 18699653d4eSeschrock 18799653d4eSeschrock /*PRINTFLIKE2*/ 188fa9e4066Sahrens void 18999653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 190fa9e4066Sahrens { 191fa9e4066Sahrens va_list ap; 192fa9e4066Sahrens 193fa9e4066Sahrens va_start(ap, fmt); 194fa9e4066Sahrens 19599653d4eSeschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 19699653d4eSeschrock fmt, ap); 19799653d4eSeschrock hdl->libzfs_desc_active = 1; 19899653d4eSeschrock 19999653d4eSeschrock va_end(ap); 20099653d4eSeschrock } 20199653d4eSeschrock 20299653d4eSeschrock static void 20399653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 20499653d4eSeschrock { 20599653d4eSeschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 20699653d4eSeschrock fmt, ap); 20799653d4eSeschrock hdl->libzfs_error = error; 20899653d4eSeschrock 20999653d4eSeschrock if (hdl->libzfs_desc_active) 21099653d4eSeschrock hdl->libzfs_desc_active = 0; 21199653d4eSeschrock else 21299653d4eSeschrock hdl->libzfs_desc[0] = '\0'; 21399653d4eSeschrock 21499653d4eSeschrock if (hdl->libzfs_printerr) { 21599653d4eSeschrock if (error == EZFS_UNKNOWN) { 21699653d4eSeschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 21799653d4eSeschrock "error: %s\n"), libzfs_error_description(hdl)); 21899653d4eSeschrock abort(); 21999653d4eSeschrock } 22099653d4eSeschrock 22199653d4eSeschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 222b1b8ab34Slling libzfs_error_description(hdl)); 22399653d4eSeschrock if (error == EZFS_NOMEM) 22499653d4eSeschrock exit(1); 225fa9e4066Sahrens } 22699653d4eSeschrock } 22799653d4eSeschrock 228ece3d9b3Slling int 229ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 230ece3d9b3Slling { 231ece3d9b3Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 232ece3d9b3Slling } 233ece3d9b3Slling 23499653d4eSeschrock /*PRINTFLIKE3*/ 23599653d4eSeschrock int 236ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 23799653d4eSeschrock { 23899653d4eSeschrock va_list ap; 23999653d4eSeschrock 24099653d4eSeschrock va_start(ap, fmt); 24199653d4eSeschrock 24299653d4eSeschrock zfs_verror(hdl, error, fmt, ap); 243fa9e4066Sahrens 244fa9e4066Sahrens va_end(ap); 24599653d4eSeschrock 24699653d4eSeschrock return (-1); 247fa9e4066Sahrens } 248fa9e4066Sahrens 24999653d4eSeschrock static int 25099653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 25199653d4eSeschrock va_list ap) 25299653d4eSeschrock { 25399653d4eSeschrock switch (error) { 25499653d4eSeschrock case EPERM: 25599653d4eSeschrock case EACCES: 25699653d4eSeschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 25799653d4eSeschrock return (-1); 25899653d4eSeschrock 25999653d4eSeschrock case EIO: 26099653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 26199653d4eSeschrock return (-1); 26299653d4eSeschrock 26399653d4eSeschrock case EINTR: 26499653d4eSeschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 26599653d4eSeschrock return (-1); 26699653d4eSeschrock } 26799653d4eSeschrock 26899653d4eSeschrock return (0); 26999653d4eSeschrock } 27099653d4eSeschrock 271ece3d9b3Slling int 272ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 273ece3d9b3Slling { 274ece3d9b3Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 275ece3d9b3Slling } 276ece3d9b3Slling 27799653d4eSeschrock /*PRINTFLIKE3*/ 27899653d4eSeschrock int 279ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 280fa9e4066Sahrens { 281fa9e4066Sahrens va_list ap; 282fa9e4066Sahrens 283fa9e4066Sahrens va_start(ap, fmt); 284fa9e4066Sahrens 28599653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 28699653d4eSeschrock va_end(ap); 28799653d4eSeschrock return (-1); 288fa9e4066Sahrens } 289fa9e4066Sahrens 290fa9e4066Sahrens 29199653d4eSeschrock switch (error) { 29299653d4eSeschrock case ENXIO: 29399653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 29499653d4eSeschrock break; 29599653d4eSeschrock 29699653d4eSeschrock case ENOENT: 29799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29899653d4eSeschrock "dataset does not exist")); 29999653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 30099653d4eSeschrock break; 30199653d4eSeschrock 30299653d4eSeschrock case ENOSPC: 30399653d4eSeschrock case EDQUOT: 30499653d4eSeschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 30599653d4eSeschrock return (-1); 30699653d4eSeschrock 30799653d4eSeschrock case EEXIST: 30899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30999653d4eSeschrock "dataset already exists")); 31099653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 31199653d4eSeschrock break; 31299653d4eSeschrock 31399653d4eSeschrock case EBUSY: 31499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31599653d4eSeschrock "dataset is busy")); 31699653d4eSeschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 31799653d4eSeschrock break; 318b7661cccSmmusante 319b7661cccSmmusante case ENAMETOOLONG: 320b7661cccSmmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 321b7661cccSmmusante break; 322b7661cccSmmusante 32399653d4eSeschrock default: 32499653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 32599653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 32699653d4eSeschrock break; 32799653d4eSeschrock } 32899653d4eSeschrock 32999653d4eSeschrock va_end(ap); 33099653d4eSeschrock return (-1); 331fa9e4066Sahrens } 332fa9e4066Sahrens 333ece3d9b3Slling int 334ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 335ece3d9b3Slling { 336ece3d9b3Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 337ece3d9b3Slling } 338ece3d9b3Slling 33999653d4eSeschrock /*PRINTFLIKE3*/ 34099653d4eSeschrock int 341ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 342fa9e4066Sahrens { 34399653d4eSeschrock va_list ap; 34499653d4eSeschrock 34599653d4eSeschrock va_start(ap, fmt); 34699653d4eSeschrock 34799653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 34899653d4eSeschrock va_end(ap); 34999653d4eSeschrock return (-1); 35099653d4eSeschrock } 35199653d4eSeschrock 35299653d4eSeschrock switch (error) { 35399653d4eSeschrock case ENODEV: 35499653d4eSeschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 35599653d4eSeschrock break; 35699653d4eSeschrock 35799653d4eSeschrock case ENOENT: 358b1b8ab34Slling zfs_error_aux(hdl, 359b1b8ab34Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 36099653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 36199653d4eSeschrock break; 36299653d4eSeschrock 36399653d4eSeschrock case EEXIST: 36499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36599653d4eSeschrock "pool already exists")); 36699653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 36799653d4eSeschrock break; 36899653d4eSeschrock 36999653d4eSeschrock case EBUSY: 37099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 37199653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 37299653d4eSeschrock break; 37399653d4eSeschrock 37499653d4eSeschrock case ENXIO: 37599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37699653d4eSeschrock "one or more devices is currently unavailable")); 37799653d4eSeschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 37899653d4eSeschrock break; 37999653d4eSeschrock 38099653d4eSeschrock case ENAMETOOLONG: 38199653d4eSeschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 38299653d4eSeschrock break; 38399653d4eSeschrock 384b1b8ab34Slling case ENOTSUP: 385b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 386b1b8ab34Slling break; 387b1b8ab34Slling 388b1b8ab34Slling case EINVAL: 389b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 390b1b8ab34Slling break; 391b1b8ab34Slling 39299653d4eSeschrock default: 39399653d4eSeschrock zfs_error_aux(hdl, strerror(error)); 39499653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 39599653d4eSeschrock } 39699653d4eSeschrock 39799653d4eSeschrock va_end(ap); 39899653d4eSeschrock return (-1); 399fa9e4066Sahrens } 400fa9e4066Sahrens 401fa9e4066Sahrens /* 402fa9e4066Sahrens * Display an out of memory error message and abort the current program. 403fa9e4066Sahrens */ 40499653d4eSeschrock int 40599653d4eSeschrock no_memory(libzfs_handle_t *hdl) 406fa9e4066Sahrens { 40799653d4eSeschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 408fa9e4066Sahrens } 409fa9e4066Sahrens 410fa9e4066Sahrens /* 411fa9e4066Sahrens * A safe form of malloc() which will die if the allocation fails. 412fa9e4066Sahrens */ 413fa9e4066Sahrens void * 41499653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 415fa9e4066Sahrens { 416fa9e4066Sahrens void *data; 417fa9e4066Sahrens 418fa9e4066Sahrens if ((data = calloc(1, size)) == NULL) 41999653d4eSeschrock (void) no_memory(hdl); 420fa9e4066Sahrens 421fa9e4066Sahrens return (data); 422fa9e4066Sahrens } 423fa9e4066Sahrens 424e9dbad6fSeschrock /* 425e9dbad6fSeschrock * A safe form of realloc(), which also zeroes newly allocated space. 426e9dbad6fSeschrock */ 427e9dbad6fSeschrock void * 428e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 429e9dbad6fSeschrock { 430e9dbad6fSeschrock void *ret; 431e9dbad6fSeschrock 432e9dbad6fSeschrock if ((ret = realloc(ptr, newsize)) == NULL) { 433e9dbad6fSeschrock (void) no_memory(hdl); 434e9dbad6fSeschrock free(ptr); 435e9dbad6fSeschrock return (NULL); 436e9dbad6fSeschrock } 437e9dbad6fSeschrock 438e9dbad6fSeschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 439e9dbad6fSeschrock return (ret); 440e9dbad6fSeschrock } 441e9dbad6fSeschrock 442fa9e4066Sahrens /* 443fa9e4066Sahrens * A safe form of strdup() which will die if the allocation fails. 444fa9e4066Sahrens */ 445fa9e4066Sahrens char * 44699653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 447fa9e4066Sahrens { 448fa9e4066Sahrens char *ret; 449fa9e4066Sahrens 450fa9e4066Sahrens if ((ret = strdup(str)) == NULL) 45199653d4eSeschrock (void) no_memory(hdl); 452fa9e4066Sahrens 453fa9e4066Sahrens return (ret); 454fa9e4066Sahrens } 455fa9e4066Sahrens 456fa9e4066Sahrens /* 457fa9e4066Sahrens * Convert a number to an appropriately human-readable output. 458fa9e4066Sahrens */ 459fa9e4066Sahrens void 460fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 461fa9e4066Sahrens { 462fa9e4066Sahrens uint64_t n = num; 463fa9e4066Sahrens int index = 0; 464fa9e4066Sahrens char u; 465fa9e4066Sahrens 466fa9e4066Sahrens while (n >= 1024) { 4675c709891Seschrock n /= 1024; 468fa9e4066Sahrens index++; 469fa9e4066Sahrens } 470fa9e4066Sahrens 471fa9e4066Sahrens u = " KMGTPE"[index]; 472fa9e4066Sahrens 4735c709891Seschrock if (index == 0) { 474fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu", n); 4755c709891Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 4765c709891Seschrock /* 4775c709891Seschrock * If this is an even multiple of the base, always display 4785c709891Seschrock * without any decimal precision. 4795c709891Seschrock */ 480fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 4815c709891Seschrock } else { 4825c709891Seschrock /* 4835c709891Seschrock * We want to choose a precision that reflects the best choice 4845c709891Seschrock * for fitting in 5 characters. This can get rather tricky when 4855c709891Seschrock * we have numbers that are very close to an order of magnitude. 4865c709891Seschrock * For example, when displaying 10239 (which is really 9.999K), 4875c709891Seschrock * we want only a single place of precision for 10.0K. We could 4885c709891Seschrock * develop some complex heuristics for this, but it's much 4895c709891Seschrock * easier just to try each combination in turn. 4905c709891Seschrock */ 4915c709891Seschrock int i; 4925c709891Seschrock for (i = 2; i >= 0; i--) { 4935c709891Seschrock (void) snprintf(buf, buflen, "%.*f%c", i, 4945c709891Seschrock (double)num / (1ULL << 10 * index), u); 4955c709891Seschrock if (strlen(buf) <= 5) 4965c709891Seschrock break; 4975c709891Seschrock } 4985c709891Seschrock } 499fa9e4066Sahrens } 50099653d4eSeschrock 50199653d4eSeschrock void 50299653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 50399653d4eSeschrock { 50499653d4eSeschrock hdl->libzfs_printerr = printerr; 50599653d4eSeschrock } 50699653d4eSeschrock 50799653d4eSeschrock libzfs_handle_t * 50899653d4eSeschrock libzfs_init(void) 50999653d4eSeschrock { 51099653d4eSeschrock libzfs_handle_t *hdl; 51199653d4eSeschrock 51299653d4eSeschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 51399653d4eSeschrock return (NULL); 51499653d4eSeschrock } 51599653d4eSeschrock 516c08432ebSeschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 51799653d4eSeschrock free(hdl); 51899653d4eSeschrock return (NULL); 51999653d4eSeschrock } 52099653d4eSeschrock 52199653d4eSeschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 52299653d4eSeschrock (void) close(hdl->libzfs_fd); 52399653d4eSeschrock free(hdl); 52499653d4eSeschrock return (NULL); 52599653d4eSeschrock } 52699653d4eSeschrock 52799653d4eSeschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 52899653d4eSeschrock 52999653d4eSeschrock return (hdl); 53099653d4eSeschrock } 53199653d4eSeschrock 53299653d4eSeschrock void 53399653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl) 53499653d4eSeschrock { 53599653d4eSeschrock (void) close(hdl->libzfs_fd); 53699653d4eSeschrock if (hdl->libzfs_mnttab) 53799653d4eSeschrock (void) fclose(hdl->libzfs_mnttab); 53899653d4eSeschrock if (hdl->libzfs_sharetab) 53999653d4eSeschrock (void) fclose(hdl->libzfs_sharetab); 54067331909Sdougm zfs_uninit_libshare(hdl); 54199653d4eSeschrock namespace_clear(hdl); 54299653d4eSeschrock free(hdl); 54399653d4eSeschrock } 54499653d4eSeschrock 54599653d4eSeschrock libzfs_handle_t * 54699653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp) 54799653d4eSeschrock { 54899653d4eSeschrock return (zhp->zpool_hdl); 54999653d4eSeschrock } 55099653d4eSeschrock 55199653d4eSeschrock libzfs_handle_t * 55299653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp) 55399653d4eSeschrock { 55499653d4eSeschrock return (zhp->zfs_hdl); 55599653d4eSeschrock } 556e9dbad6fSeschrock 5575aba80dbSck /* 5585aba80dbSck * Given a name, determine whether or not it's a valid path 5595aba80dbSck * (starts with '/' or "./"). If so, walk the mnttab trying 5605aba80dbSck * to match the device number. If not, treat the path as an 5615aba80dbSck * fs/vol/snap name. 5625aba80dbSck */ 5635aba80dbSck zfs_handle_t * 5645aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 5655aba80dbSck { 5665aba80dbSck struct stat64 statbuf; 5675aba80dbSck struct extmnttab entry; 5685aba80dbSck int ret; 5695aba80dbSck 5705aba80dbSck if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 5715aba80dbSck /* 5725aba80dbSck * It's not a valid path, assume it's a name of type 'argtype'. 5735aba80dbSck */ 5745aba80dbSck return (zfs_open(hdl, path, argtype)); 5755aba80dbSck } 5765aba80dbSck 5775aba80dbSck if (stat64(path, &statbuf) != 0) { 5785aba80dbSck (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 5795aba80dbSck return (NULL); 5805aba80dbSck } 5815aba80dbSck 5825aba80dbSck rewind(hdl->libzfs_mnttab); 5835aba80dbSck while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 5845aba80dbSck if (makedevice(entry.mnt_major, entry.mnt_minor) == 5855aba80dbSck statbuf.st_dev) { 5865aba80dbSck break; 5875aba80dbSck } 5885aba80dbSck } 5895aba80dbSck if (ret != 0) { 5905aba80dbSck return (NULL); 5915aba80dbSck } 5925aba80dbSck 5935aba80dbSck if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 5945aba80dbSck (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 5955aba80dbSck path); 5965aba80dbSck return (NULL); 5975aba80dbSck } 5985aba80dbSck 5995aba80dbSck return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6005aba80dbSck } 6015aba80dbSck 602e9dbad6fSeschrock /* 603e9dbad6fSeschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 604e9dbad6fSeschrock * an ioctl(). 605e9dbad6fSeschrock */ 606e9dbad6fSeschrock int 607e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 608e9dbad6fSeschrock { 609e9dbad6fSeschrock if (len == 0) 610a2eea2e1Sahrens len = 2048; 611e9dbad6fSeschrock zc->zc_nvlist_dst_size = len; 612e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 613e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 614e9dbad6fSeschrock return (-1); 615e9dbad6fSeschrock 616e9dbad6fSeschrock return (0); 617e9dbad6fSeschrock } 618e9dbad6fSeschrock 619e9dbad6fSeschrock /* 620e9dbad6fSeschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 621e9dbad6fSeschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 622e9dbad6fSeschrock * filled in by the kernel to indicate the actual required size. 623e9dbad6fSeschrock */ 624e9dbad6fSeschrock int 625e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 626e9dbad6fSeschrock { 627e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 628e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 629e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 630e9dbad6fSeschrock == NULL) 631e9dbad6fSeschrock return (-1); 632e9dbad6fSeschrock 633e9dbad6fSeschrock return (0); 634e9dbad6fSeschrock } 635e9dbad6fSeschrock 636e9dbad6fSeschrock /* 637a2eea2e1Sahrens * Called to free the src and dst nvlists stored in the command structure. 638e9dbad6fSeschrock */ 639e9dbad6fSeschrock void 640e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc) 641e9dbad6fSeschrock { 642e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 643e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 644e9dbad6fSeschrock } 645e9dbad6fSeschrock 646e9dbad6fSeschrock int 647e9dbad6fSeschrock zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl, 648e9dbad6fSeschrock size_t *size) 649e9dbad6fSeschrock { 650e9dbad6fSeschrock char *packed; 651e9dbad6fSeschrock size_t len; 652e9dbad6fSeschrock 653e9dbad6fSeschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 654e9dbad6fSeschrock 655e9dbad6fSeschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 656e9dbad6fSeschrock return (-1); 657e9dbad6fSeschrock 658e9dbad6fSeschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 659e9dbad6fSeschrock 660e9dbad6fSeschrock zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed; 661e9dbad6fSeschrock zc->zc_nvlist_src_size = len; 662e9dbad6fSeschrock 663e9dbad6fSeschrock if (size) 664e9dbad6fSeschrock *size = len; 665e9dbad6fSeschrock return (0); 666e9dbad6fSeschrock } 667e9dbad6fSeschrock 668e9dbad6fSeschrock /* 669e9dbad6fSeschrock * Unpacks an nvlist from the ZFS ioctl command structure. 670e9dbad6fSeschrock */ 671e9dbad6fSeschrock int 672e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 673e9dbad6fSeschrock { 674e9dbad6fSeschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 675e9dbad6fSeschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 676e9dbad6fSeschrock return (no_memory(hdl)); 677e9dbad6fSeschrock 678e9dbad6fSeschrock return (0); 679e9dbad6fSeschrock } 680b1b8ab34Slling 681b1b8ab34Slling static void 682b1b8ab34Slling zfs_print_prop_headers(libzfs_get_cbdata_t *cbp) 683b1b8ab34Slling { 684b1b8ab34Slling zfs_proplist_t *pl = cbp->cb_proplist; 685b1b8ab34Slling int i; 686b1b8ab34Slling char *title; 687b1b8ab34Slling size_t len; 688b1b8ab34Slling 689b1b8ab34Slling cbp->cb_first = B_FALSE; 690b1b8ab34Slling if (cbp->cb_scripted) 691b1b8ab34Slling return; 692b1b8ab34Slling 693b1b8ab34Slling /* 694b1b8ab34Slling * Start with the length of the column headers. 695b1b8ab34Slling */ 696b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 697b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 698b1b8ab34Slling "PROPERTY")); 699b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 700b1b8ab34Slling "VALUE")); 701b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 702b1b8ab34Slling "SOURCE")); 703b1b8ab34Slling 704b1b8ab34Slling /* 705b1b8ab34Slling * Go through and calculate the widths for each column. For the 706b1b8ab34Slling * 'source' column, we kludge it up by taking the worst-case scenario of 707b1b8ab34Slling * inheriting from the longest name. This is acceptable because in the 708b1b8ab34Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 709b1b8ab34Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 710b1b8ab34Slling * if the name of the property is much longer the any values we find. 711b1b8ab34Slling */ 712b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 713b1b8ab34Slling /* 714b1b8ab34Slling * 'PROPERTY' column 715b1b8ab34Slling */ 716b1b8ab34Slling if (pl->pl_prop != ZFS_PROP_INVAL) { 717b1b8ab34Slling len = strlen(zfs_prop_to_name(pl->pl_prop)); 718b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 719b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 720b1b8ab34Slling } else { 721b1b8ab34Slling len = strlen(pl->pl_user_prop); 722b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 723b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 724b1b8ab34Slling } 725b1b8ab34Slling 726b1b8ab34Slling /* 727b1b8ab34Slling * 'VALUE' column 728b1b8ab34Slling */ 729b1b8ab34Slling if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) && 730b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 731b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 732b1b8ab34Slling 733b1b8ab34Slling /* 734b1b8ab34Slling * 'NAME' and 'SOURCE' columns 735b1b8ab34Slling */ 736b1b8ab34Slling if (pl->pl_prop == ZFS_PROP_NAME && 737b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 738b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 739b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 740b1b8ab34Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 741b1b8ab34Slling } 742b1b8ab34Slling } 743b1b8ab34Slling 744b1b8ab34Slling /* 745b1b8ab34Slling * Now go through and print the headers. 746b1b8ab34Slling */ 747b1b8ab34Slling for (i = 0; i < 4; i++) { 748b1b8ab34Slling switch (cbp->cb_columns[i]) { 749b1b8ab34Slling case GET_COL_NAME: 750b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "NAME"); 751b1b8ab34Slling break; 752b1b8ab34Slling case GET_COL_PROPERTY: 753b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 754b1b8ab34Slling break; 755b1b8ab34Slling case GET_COL_VALUE: 756b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 757b1b8ab34Slling break; 758b1b8ab34Slling case GET_COL_SOURCE: 759b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 760b1b8ab34Slling break; 761b1b8ab34Slling default: 762b1b8ab34Slling title = NULL; 763b1b8ab34Slling } 764b1b8ab34Slling 765b1b8ab34Slling if (title != NULL) { 766b1b8ab34Slling if (i == 3 || cbp->cb_columns[i + 1] == 0) 767b1b8ab34Slling (void) printf("%s", title); 768b1b8ab34Slling else 769b1b8ab34Slling (void) printf("%-*s ", 770b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 771b1b8ab34Slling title); 772b1b8ab34Slling } 773b1b8ab34Slling } 774b1b8ab34Slling (void) printf("\n"); 775b1b8ab34Slling } 776b1b8ab34Slling 777b1b8ab34Slling /* 778b1b8ab34Slling * Display a single line of output, according to the settings in the callback 779b1b8ab34Slling * structure. 780b1b8ab34Slling */ 781b1b8ab34Slling void 782b1b8ab34Slling libzfs_print_one_property(const char *name, libzfs_get_cbdata_t *cbp, 783b1b8ab34Slling const char *propname, const char *value, zfs_source_t sourcetype, 784b1b8ab34Slling const char *source) 785b1b8ab34Slling { 786b1b8ab34Slling int i; 787b1b8ab34Slling const char *str; 788b1b8ab34Slling char buf[128]; 789b1b8ab34Slling 790b1b8ab34Slling /* 791b1b8ab34Slling * Ignore those source types that the user has chosen to ignore. 792b1b8ab34Slling */ 793b1b8ab34Slling if ((sourcetype & cbp->cb_sources) == 0) 794b1b8ab34Slling return; 795b1b8ab34Slling 796b1b8ab34Slling if (cbp->cb_first) 797b1b8ab34Slling zfs_print_prop_headers(cbp); 798b1b8ab34Slling 799b1b8ab34Slling for (i = 0; i < 4; i++) { 800b1b8ab34Slling switch (cbp->cb_columns[i]) { 801b1b8ab34Slling case GET_COL_NAME: 802b1b8ab34Slling str = name; 803b1b8ab34Slling break; 804b1b8ab34Slling 805b1b8ab34Slling case GET_COL_PROPERTY: 806b1b8ab34Slling str = propname; 807b1b8ab34Slling break; 808b1b8ab34Slling 809b1b8ab34Slling case GET_COL_VALUE: 810b1b8ab34Slling str = value; 811b1b8ab34Slling break; 812b1b8ab34Slling 813b1b8ab34Slling case GET_COL_SOURCE: 814b1b8ab34Slling switch (sourcetype) { 815b1b8ab34Slling case ZFS_SRC_NONE: 816b1b8ab34Slling str = "-"; 817b1b8ab34Slling break; 818b1b8ab34Slling 819b1b8ab34Slling case ZFS_SRC_DEFAULT: 820b1b8ab34Slling str = "default"; 821b1b8ab34Slling break; 822b1b8ab34Slling 823b1b8ab34Slling case ZFS_SRC_LOCAL: 824b1b8ab34Slling str = "local"; 825b1b8ab34Slling break; 826b1b8ab34Slling 827b1b8ab34Slling case ZFS_SRC_TEMPORARY: 828b1b8ab34Slling str = "temporary"; 829b1b8ab34Slling break; 830b1b8ab34Slling 831b1b8ab34Slling case ZFS_SRC_INHERITED: 832b1b8ab34Slling (void) snprintf(buf, sizeof (buf), 833b1b8ab34Slling "inherited from %s", source); 834b1b8ab34Slling str = buf; 835b1b8ab34Slling break; 836b1b8ab34Slling } 837b1b8ab34Slling break; 838b1b8ab34Slling 839b1b8ab34Slling default: 840b1b8ab34Slling continue; 841b1b8ab34Slling } 842b1b8ab34Slling 843b1b8ab34Slling if (cbp->cb_columns[i + 1] == 0) 844b1b8ab34Slling (void) printf("%s", str); 845b1b8ab34Slling else if (cbp->cb_scripted) 846b1b8ab34Slling (void) printf("%s\t", str); 847b1b8ab34Slling else 848b1b8ab34Slling (void) printf("%-*s ", 849b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 850b1b8ab34Slling str); 851b1b8ab34Slling 852b1b8ab34Slling } 853b1b8ab34Slling 854b1b8ab34Slling (void) printf("\n"); 855b1b8ab34Slling } 856