1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 228811SEric.Taylor@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 27789Sahrens * Internal utility routines for the ZFS library. 28789Sahrens */ 29789Sahrens 30789Sahrens #include <errno.h> 31789Sahrens #include <fcntl.h> 32789Sahrens #include <libintl.h> 33789Sahrens #include <stdarg.h> 34789Sahrens #include <stdio.h> 35789Sahrens #include <stdlib.h> 36789Sahrens #include <strings.h> 37789Sahrens #include <unistd.h> 385094Slling #include <ctype.h> 395094Slling #include <math.h> 40789Sahrens #include <sys/mnttab.h> 413635Sck153898 #include <sys/mntent.h> 423635Sck153898 #include <sys/types.h> 43789Sahrens 44789Sahrens #include <libzfs.h> 45789Sahrens 46789Sahrens #include "libzfs_impl.h" 474787Sahrens #include "zfs_prop.h" 48789Sahrens 492082Seschrock int 502082Seschrock libzfs_errno(libzfs_handle_t *hdl) 512082Seschrock { 522082Seschrock return (hdl->libzfs_error); 532082Seschrock } 54789Sahrens 552082Seschrock const char * 562082Seschrock libzfs_error_action(libzfs_handle_t *hdl) 572082Seschrock { 582082Seschrock return (hdl->libzfs_action); 592082Seschrock } 602082Seschrock 612082Seschrock const char * 622082Seschrock libzfs_error_description(libzfs_handle_t *hdl) 632082Seschrock { 642082Seschrock if (hdl->libzfs_desc[0] != '\0') 652082Seschrock return (hdl->libzfs_desc); 66789Sahrens 672082Seschrock switch (hdl->libzfs_error) { 682082Seschrock case EZFS_NOMEM: 692082Seschrock return (dgettext(TEXT_DOMAIN, "out of memory")); 702082Seschrock case EZFS_BADPROP: 712082Seschrock return (dgettext(TEXT_DOMAIN, "invalid property value")); 722082Seschrock case EZFS_PROPREADONLY: 732082Seschrock return (dgettext(TEXT_DOMAIN, "read only property")); 742082Seschrock case EZFS_PROPTYPE: 752082Seschrock return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 762082Seschrock "datasets of this type")); 772082Seschrock case EZFS_PROPNONINHERIT: 782082Seschrock return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 792082Seschrock case EZFS_PROPSPACE: 802082Seschrock return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 812082Seschrock case EZFS_BADTYPE: 822082Seschrock return (dgettext(TEXT_DOMAIN, "operation not applicable to " 832082Seschrock "datasets of this type")); 842082Seschrock case EZFS_BUSY: 852082Seschrock return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 862082Seschrock case EZFS_EXISTS: 872082Seschrock return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 882082Seschrock case EZFS_NOENT: 892082Seschrock return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 902082Seschrock case EZFS_BADSTREAM: 912082Seschrock return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 922082Seschrock case EZFS_DSREADONLY: 932082Seschrock return (dgettext(TEXT_DOMAIN, "dataset is read only")); 942082Seschrock case EZFS_VOLTOOBIG: 952082Seschrock return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 962082Seschrock "this system")); 972082Seschrock case EZFS_INVALIDNAME: 982082Seschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 992082Seschrock case EZFS_BADRESTORE: 1002082Seschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 1012082Seschrock "destination")); 1022082Seschrock case EZFS_BADBACKUP: 1032082Seschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 1042082Seschrock case EZFS_BADTARGET: 1052082Seschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 1062082Seschrock case EZFS_NODEVICE: 1072082Seschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 1082082Seschrock case EZFS_BADDEV: 1092082Seschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 1102082Seschrock case EZFS_NOREPLICAS: 1112082Seschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 1122082Seschrock case EZFS_RESILVERING: 1132082Seschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 1142082Seschrock case EZFS_BADVERSION: 1152082Seschrock return (dgettext(TEXT_DOMAIN, "unsupported version")); 1162082Seschrock case EZFS_POOLUNAVAIL: 1172082Seschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 1182082Seschrock case EZFS_DEVOVERFLOW: 1192082Seschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 1202082Seschrock case EZFS_BADPATH: 1212082Seschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 1222082Seschrock case EZFS_CROSSTARGET: 1232082Seschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 1242082Seschrock "pools")); 1252082Seschrock case EZFS_ZONED: 1262082Seschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 1272082Seschrock case EZFS_MOUNTFAILED: 1282082Seschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 1292082Seschrock case EZFS_UMOUNTFAILED: 1302082Seschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 1313126Sahl case EZFS_UNSHARENFSFAILED: 1322082Seschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 1333126Sahl case EZFS_SHARENFSFAILED: 1342082Seschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 1355331Samw case EZFS_UNSHARESMBFAILED: 1365331Samw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 1375331Samw case EZFS_SHARESMBFAILED: 1385331Samw return (dgettext(TEXT_DOMAIN, "smb add share failed")); 1394543Smarks case EZFS_ISCSISVCUNAVAIL: 1404543Smarks return (dgettext(TEXT_DOMAIN, 1414543Smarks "iscsitgt service need to be enabled by " 1424543Smarks "a privileged user")); 1432082Seschrock case EZFS_PERM: 1442082Seschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 1452082Seschrock case EZFS_NOSPC: 1462082Seschrock return (dgettext(TEXT_DOMAIN, "out of space")); 1472082Seschrock case EZFS_IO: 1482082Seschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 1492082Seschrock case EZFS_INTR: 1502082Seschrock return (dgettext(TEXT_DOMAIN, "signal received")); 1512082Seschrock case EZFS_ISSPARE: 1522082Seschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 1532082Seschrock "spare")); 1542082Seschrock case EZFS_INVALCONFIG: 1552082Seschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1562474Seschrock case EZFS_RECURSIVE: 1572474Seschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 1582926Sek110237 case EZFS_NOHISTORY: 1592926Sek110237 return (dgettext(TEXT_DOMAIN, "no history available")); 1603126Sahl case EZFS_UNSHAREISCSIFAILED: 1613126Sahl return (dgettext(TEXT_DOMAIN, 1623126Sahl "iscsitgtd failed request to unshare")); 1633126Sahl case EZFS_SHAREISCSIFAILED: 1643126Sahl return (dgettext(TEXT_DOMAIN, 1653126Sahl "iscsitgtd failed request to share")); 1663912Slling case EZFS_POOLPROPS: 1673912Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 1683912Slling "pool properties")); 1693912Slling case EZFS_POOL_NOTSUP: 1703912Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 1713912Slling "on this type of pool")); 1723912Slling case EZFS_POOL_INVALARG: 1733912Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 1743912Slling "this pool operation")); 1753978Smmusante case EZFS_NAMETOOLONG: 1763978Smmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 1774276Staylor case EZFS_OPENFAILED: 1784276Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 1794276Staylor case EZFS_NOCAP: 1804276Staylor return (dgettext(TEXT_DOMAIN, 1814276Staylor "disk capacity information could not be retrieved")); 1824276Staylor case EZFS_LABELFAILED: 1834276Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 1844543Smarks case EZFS_BADWHO: 1854543Smarks return (dgettext(TEXT_DOMAIN, "invalid user/group")); 1864543Smarks case EZFS_BADPERM: 1874543Smarks return (dgettext(TEXT_DOMAIN, "invalid permission")); 1884543Smarks case EZFS_BADPERMSET: 1894543Smarks return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 1904543Smarks case EZFS_NODELEGATION: 1914543Smarks return (dgettext(TEXT_DOMAIN, "delegated administration is " 1924543Smarks "disabled on pool")); 1934543Smarks case EZFS_PERMRDONLY: 1944543Smarks return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be" 1954543Smarks " modified")); 1965363Seschrock case EZFS_BADCACHE: 1975363Seschrock return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 1985450Sbrendan case EZFS_ISL2CACHE: 1995450Sbrendan return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 2006423Sgw25295 case EZFS_VDEVNOTSUP: 2016423Sgw25295 return (dgettext(TEXT_DOMAIN, "vdev specification is not " 2026423Sgw25295 "supported")); 2037042Sgw25295 case EZFS_NOTSUP: 2047042Sgw25295 return (dgettext(TEXT_DOMAIN, "operation not supported " 2057042Sgw25295 "on this dataset")); 2067214Slling case EZFS_ACTIVE_SPARE: 2077214Slling return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 2087214Slling "device")); 2099701SGeorge.Wilson@Sun.COM case EZFS_UNPLAYED_LOGS: 2109701SGeorge.Wilson@Sun.COM return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 2119701SGeorge.Wilson@Sun.COM "logs")); 21210242Schris.kirby@sun.com case EZFS_REFTAG_RELE: 21310242Schris.kirby@sun.com return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 21410242Schris.kirby@sun.com case EZFS_REFTAG_HOLD: 21510242Schris.kirby@sun.com return (dgettext(TEXT_DOMAIN, "tag already exists on this " 21610242Schris.kirby@sun.com "dataset")); 21710342Schris.kirby@sun.com case EZFS_TAGTOOLONG: 21810342Schris.kirby@sun.com return (dgettext(TEXT_DOMAIN, "tag too long")); 2192082Seschrock case EZFS_UNKNOWN: 2202082Seschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 2212082Seschrock default: 2222500Seschrock assert(hdl->libzfs_error == 0); 2232500Seschrock return (dgettext(TEXT_DOMAIN, "no error")); 2242082Seschrock } 2252082Seschrock } 2262082Seschrock 2272082Seschrock /*PRINTFLIKE2*/ 228789Sahrens void 2292082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 230789Sahrens { 231789Sahrens va_list ap; 232789Sahrens 233789Sahrens va_start(ap, fmt); 234789Sahrens 2352082Seschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 2362082Seschrock fmt, ap); 2372082Seschrock hdl->libzfs_desc_active = 1; 238789Sahrens 239789Sahrens va_end(ap); 240789Sahrens } 241789Sahrens 2422082Seschrock static void 2432082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 2442082Seschrock { 2452082Seschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 2462082Seschrock fmt, ap); 2472082Seschrock hdl->libzfs_error = error; 2482082Seschrock 2492082Seschrock if (hdl->libzfs_desc_active) 2502082Seschrock hdl->libzfs_desc_active = 0; 2512082Seschrock else 2522082Seschrock hdl->libzfs_desc[0] = '\0'; 2532082Seschrock 2542082Seschrock if (hdl->libzfs_printerr) { 2552082Seschrock if (error == EZFS_UNKNOWN) { 2562082Seschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 2572082Seschrock "error: %s\n"), libzfs_error_description(hdl)); 2582082Seschrock abort(); 2592082Seschrock } 2602082Seschrock 2612082Seschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 2623912Slling libzfs_error_description(hdl)); 2632082Seschrock if (error == EZFS_NOMEM) 2642082Seschrock exit(1); 2652082Seschrock } 2662082Seschrock } 2672082Seschrock 2683237Slling int 2693237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 2703237Slling { 2713237Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 2723237Slling } 2733237Slling 2742082Seschrock /*PRINTFLIKE3*/ 2752082Seschrock int 2763237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 2772082Seschrock { 2782082Seschrock va_list ap; 2792082Seschrock 2802082Seschrock va_start(ap, fmt); 2812082Seschrock 2822082Seschrock zfs_verror(hdl, error, fmt, ap); 2832082Seschrock 2842082Seschrock va_end(ap); 2852082Seschrock 2862082Seschrock return (-1); 2872082Seschrock } 2882082Seschrock 2892082Seschrock static int 2902082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 2912082Seschrock va_list ap) 2922082Seschrock { 2932082Seschrock switch (error) { 2942082Seschrock case EPERM: 2952082Seschrock case EACCES: 2962082Seschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 2972082Seschrock return (-1); 2982082Seschrock 2994543Smarks case ECANCELED: 3004543Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 3014543Smarks return (-1); 3024543Smarks 3032082Seschrock case EIO: 3042082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 3052082Seschrock return (-1); 3062082Seschrock 3072082Seschrock case EINTR: 3082082Seschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 3092082Seschrock return (-1); 3102082Seschrock } 3112082Seschrock 3122082Seschrock return (0); 3132082Seschrock } 3142082Seschrock 3153237Slling int 3163237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3173237Slling { 3183237Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 3193237Slling } 3203237Slling 3212082Seschrock /*PRINTFLIKE3*/ 3222082Seschrock int 3233237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 324789Sahrens { 325789Sahrens va_list ap; 326789Sahrens 327789Sahrens va_start(ap, fmt); 328789Sahrens 3292082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3302082Seschrock va_end(ap); 3312082Seschrock return (-1); 3322082Seschrock } 3332082Seschrock 3342082Seschrock switch (error) { 3352082Seschrock case ENXIO: 3365807Sck153898 case ENODEV: 3372082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 3382082Seschrock break; 3392082Seschrock 3402082Seschrock case ENOENT: 3412082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3422082Seschrock "dataset does not exist")); 3432082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 3442082Seschrock break; 3452082Seschrock 3462082Seschrock case ENOSPC: 3472082Seschrock case EDQUOT: 3482082Seschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 3492082Seschrock return (-1); 3502082Seschrock 3512082Seschrock case EEXIST: 3522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3532082Seschrock "dataset already exists")); 3542082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 3552082Seschrock break; 3562082Seschrock 3572082Seschrock case EBUSY: 3582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3592082Seschrock "dataset is busy")); 3602082Seschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 3612082Seschrock break; 3624543Smarks case EROFS: 3634543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3644543Smarks "snapshot permissions cannot be modified")); 3654543Smarks zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 3664543Smarks break; 3673978Smmusante case ENAMETOOLONG: 3683978Smmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 3693978Smmusante break; 3705860Sck153898 case ENOTSUP: 3715860Sck153898 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 3725860Sck153898 break; 3739234SGeorge.Wilson@Sun.COM case EAGAIN: 3749234SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3759234SGeorge.Wilson@Sun.COM "pool I/O is currently suspended")); 3769234SGeorge.Wilson@Sun.COM zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 3779234SGeorge.Wilson@Sun.COM break; 3782082Seschrock default: 3792082Seschrock zfs_error_aux(hdl, strerror(errno)); 3802082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 3812082Seschrock break; 382789Sahrens } 383789Sahrens 384789Sahrens va_end(ap); 3852082Seschrock return (-1); 386789Sahrens } 387789Sahrens 3883237Slling int 3893237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3903237Slling { 3913237Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 3923237Slling } 3933237Slling 3942082Seschrock /*PRINTFLIKE3*/ 3952082Seschrock int 3963237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 397789Sahrens { 3982082Seschrock va_list ap; 3992082Seschrock 4002082Seschrock va_start(ap, fmt); 4012082Seschrock 4022082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 4032082Seschrock va_end(ap); 4042082Seschrock return (-1); 4052082Seschrock } 4062082Seschrock 4072082Seschrock switch (error) { 4082082Seschrock case ENODEV: 4092082Seschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 4102082Seschrock break; 4112082Seschrock 4122082Seschrock case ENOENT: 4133912Slling zfs_error_aux(hdl, 4143912Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 4152082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 4162082Seschrock break; 4172082Seschrock 4182082Seschrock case EEXIST: 4192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4202082Seschrock "pool already exists")); 4212082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 4222082Seschrock break; 4232082Seschrock 4242082Seschrock case EBUSY: 4252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 4267341SEric.Schrock@Sun.COM zfs_verror(hdl, EZFS_BUSY, fmt, ap); 4272082Seschrock break; 4282082Seschrock 4292082Seschrock case ENXIO: 4302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4312082Seschrock "one or more devices is currently unavailable")); 4322082Seschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 4332082Seschrock break; 4342082Seschrock 4352082Seschrock case ENAMETOOLONG: 4362082Seschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 4372082Seschrock break; 4382082Seschrock 4393912Slling case ENOTSUP: 4403912Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 4413912Slling break; 4423912Slling 4433912Slling case EINVAL: 4443912Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 4453912Slling break; 4463912Slling 4474543Smarks case ENOSPC: 4484543Smarks case EDQUOT: 4494543Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 4504543Smarks return (-1); 4519234SGeorge.Wilson@Sun.COM case EAGAIN: 4529234SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4539234SGeorge.Wilson@Sun.COM "pool I/O is currently suspended")); 4549234SGeorge.Wilson@Sun.COM zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 4559234SGeorge.Wilson@Sun.COM break; 4564543Smarks 4572082Seschrock default: 4582082Seschrock zfs_error_aux(hdl, strerror(error)); 4592082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 4602082Seschrock } 4612082Seschrock 4622082Seschrock va_end(ap); 4632082Seschrock return (-1); 464789Sahrens } 465789Sahrens 466789Sahrens /* 467789Sahrens * Display an out of memory error message and abort the current program. 468789Sahrens */ 4692082Seschrock int 4702082Seschrock no_memory(libzfs_handle_t *hdl) 471789Sahrens { 4722082Seschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 473789Sahrens } 474789Sahrens 475789Sahrens /* 476789Sahrens * A safe form of malloc() which will die if the allocation fails. 477789Sahrens */ 478789Sahrens void * 4792082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 480789Sahrens { 481789Sahrens void *data; 482789Sahrens 483789Sahrens if ((data = calloc(1, size)) == NULL) 4842082Seschrock (void) no_memory(hdl); 485789Sahrens 486789Sahrens return (data); 487789Sahrens } 488789Sahrens 489789Sahrens /* 4902676Seschrock * A safe form of realloc(), which also zeroes newly allocated space. 4912676Seschrock */ 4922676Seschrock void * 4932676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 4942676Seschrock { 4952676Seschrock void *ret; 4962676Seschrock 4972676Seschrock if ((ret = realloc(ptr, newsize)) == NULL) { 4982676Seschrock (void) no_memory(hdl); 4992676Seschrock return (NULL); 5002676Seschrock } 5012676Seschrock 5022676Seschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 5032676Seschrock return (ret); 5042676Seschrock } 5052676Seschrock 5062676Seschrock /* 507789Sahrens * A safe form of strdup() which will die if the allocation fails. 508789Sahrens */ 509789Sahrens char * 5102082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 511789Sahrens { 512789Sahrens char *ret; 513789Sahrens 514789Sahrens if ((ret = strdup(str)) == NULL) 5152082Seschrock (void) no_memory(hdl); 516789Sahrens 517789Sahrens return (ret); 518789Sahrens } 519789Sahrens 520789Sahrens /* 521789Sahrens * Convert a number to an appropriately human-readable output. 522789Sahrens */ 523789Sahrens void 524789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 525789Sahrens { 526789Sahrens uint64_t n = num; 527789Sahrens int index = 0; 528789Sahrens char u; 529789Sahrens 530789Sahrens while (n >= 1024) { 5311162Seschrock n /= 1024; 532789Sahrens index++; 533789Sahrens } 534789Sahrens 535789Sahrens u = " KMGTPE"[index]; 536789Sahrens 5371162Seschrock if (index == 0) { 538789Sahrens (void) snprintf(buf, buflen, "%llu", n); 5391162Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5401162Seschrock /* 5411162Seschrock * If this is an even multiple of the base, always display 5421162Seschrock * without any decimal precision. 5431162Seschrock */ 544789Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5451162Seschrock } else { 5461162Seschrock /* 5471162Seschrock * We want to choose a precision that reflects the best choice 5481162Seschrock * for fitting in 5 characters. This can get rather tricky when 5491162Seschrock * we have numbers that are very close to an order of magnitude. 5501162Seschrock * For example, when displaying 10239 (which is really 9.999K), 5511162Seschrock * we want only a single place of precision for 10.0K. We could 5521162Seschrock * develop some complex heuristics for this, but it's much 5531162Seschrock * easier just to try each combination in turn. 5541162Seschrock */ 5551162Seschrock int i; 5561162Seschrock for (i = 2; i >= 0; i--) { 5574451Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5584451Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5591162Seschrock break; 5601162Seschrock } 5611162Seschrock } 562789Sahrens } 5632082Seschrock 5642082Seschrock void 5652082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 5662082Seschrock { 5672082Seschrock hdl->libzfs_printerr = printerr; 5682082Seschrock } 5692082Seschrock 5702082Seschrock libzfs_handle_t * 5712082Seschrock libzfs_init(void) 5722082Seschrock { 5732082Seschrock libzfs_handle_t *hdl; 5742082Seschrock 5752082Seschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 5762082Seschrock return (NULL); 5772082Seschrock } 5782082Seschrock 5792500Seschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 5802082Seschrock free(hdl); 5812082Seschrock return (NULL); 5822082Seschrock } 5832082Seschrock 5842082Seschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 5852082Seschrock (void) close(hdl->libzfs_fd); 5862082Seschrock free(hdl); 5872082Seschrock return (NULL); 5882082Seschrock } 5892082Seschrock 5902082Seschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 5912082Seschrock 5924787Sahrens zfs_prop_init(); 5935094Slling zpool_prop_init(); 5948811SEric.Taylor@Sun.COM libzfs_mnttab_init(hdl); 5954787Sahrens 5962082Seschrock return (hdl); 5972082Seschrock } 5982082Seschrock 5992082Seschrock void 6002082Seschrock libzfs_fini(libzfs_handle_t *hdl) 6012082Seschrock { 6022082Seschrock (void) close(hdl->libzfs_fd); 6032082Seschrock if (hdl->libzfs_mnttab) 6042082Seschrock (void) fclose(hdl->libzfs_mnttab); 6052082Seschrock if (hdl->libzfs_sharetab) 6062082Seschrock (void) fclose(hdl->libzfs_sharetab); 6074180Sdougm zfs_uninit_libshare(hdl); 6084543Smarks if (hdl->libzfs_log_str) 6094543Smarks (void) free(hdl->libzfs_log_str); 6106865Srm160521 zpool_free_handles(hdl); 611*10817SEric.Schrock@Sun.COM libzfs_fru_clear(hdl, B_TRUE); 6122082Seschrock namespace_clear(hdl); 6138811SEric.Taylor@Sun.COM libzfs_mnttab_fini(hdl); 6142082Seschrock free(hdl); 6152082Seschrock } 6162082Seschrock 6172082Seschrock libzfs_handle_t * 6182082Seschrock zpool_get_handle(zpool_handle_t *zhp) 6192082Seschrock { 6202082Seschrock return (zhp->zpool_hdl); 6212082Seschrock } 6222082Seschrock 6232082Seschrock libzfs_handle_t * 6242082Seschrock zfs_get_handle(zfs_handle_t *zhp) 6252082Seschrock { 6262082Seschrock return (zhp->zfs_hdl); 6272082Seschrock } 6282676Seschrock 6297538SRichard.Morris@Sun.COM zpool_handle_t * 6307538SRichard.Morris@Sun.COM zfs_get_pool_handle(const zfs_handle_t *zhp) 6317538SRichard.Morris@Sun.COM { 6327538SRichard.Morris@Sun.COM return (zhp->zpool_hdl); 6337538SRichard.Morris@Sun.COM } 6347538SRichard.Morris@Sun.COM 6352676Seschrock /* 6363635Sck153898 * Given a name, determine whether or not it's a valid path 6373635Sck153898 * (starts with '/' or "./"). If so, walk the mnttab trying 6383635Sck153898 * to match the device number. If not, treat the path as an 6393635Sck153898 * fs/vol/snap name. 6403635Sck153898 */ 6413635Sck153898 zfs_handle_t * 6423635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6433635Sck153898 { 6443635Sck153898 struct stat64 statbuf; 6453635Sck153898 struct extmnttab entry; 6463635Sck153898 int ret; 6473635Sck153898 6483635Sck153898 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6493635Sck153898 /* 6503635Sck153898 * It's not a valid path, assume it's a name of type 'argtype'. 6513635Sck153898 */ 6523635Sck153898 return (zfs_open(hdl, path, argtype)); 6533635Sck153898 } 6543635Sck153898 6553635Sck153898 if (stat64(path, &statbuf) != 0) { 6563635Sck153898 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6573635Sck153898 return (NULL); 6583635Sck153898 } 6593635Sck153898 6603635Sck153898 rewind(hdl->libzfs_mnttab); 6613635Sck153898 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6623635Sck153898 if (makedevice(entry.mnt_major, entry.mnt_minor) == 6633635Sck153898 statbuf.st_dev) { 6643635Sck153898 break; 6653635Sck153898 } 6663635Sck153898 } 6673635Sck153898 if (ret != 0) { 6683635Sck153898 return (NULL); 6693635Sck153898 } 6703635Sck153898 6713635Sck153898 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6723635Sck153898 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 6733635Sck153898 path); 6743635Sck153898 return (NULL); 6753635Sck153898 } 6763635Sck153898 6773635Sck153898 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6783635Sck153898 } 6793635Sck153898 6803635Sck153898 /* 6812676Seschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 6822676Seschrock * an ioctl(). 6832676Seschrock */ 6842676Seschrock int 6852676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 6862676Seschrock { 6872676Seschrock if (len == 0) 68810407SMatthew.Ahrens@Sun.COM len = 4*1024; 6892676Seschrock zc->zc_nvlist_dst_size = len; 6902676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6912676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 6922676Seschrock return (-1); 6932676Seschrock 6942676Seschrock return (0); 6952676Seschrock } 6962676Seschrock 6972676Seschrock /* 6982676Seschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 6992676Seschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 7002676Seschrock * filled in by the kernel to indicate the actual required size. 7012676Seschrock */ 7022676Seschrock int 7032676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 7042676Seschrock { 7052676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 7062676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 7072676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 7082676Seschrock == NULL) 7092676Seschrock return (-1); 7102676Seschrock 7112676Seschrock return (0); 7122676Seschrock } 7132676Seschrock 7142676Seschrock /* 7152885Sahrens * Called to free the src and dst nvlists stored in the command structure. 7162676Seschrock */ 7172676Seschrock void 7182676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc) 7192676Seschrock { 7205094Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 7212676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 7222676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 7232676Seschrock } 7242676Seschrock 7255094Slling static int 7265094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 7275094Slling nvlist_t *nvl) 7282676Seschrock { 7292676Seschrock char *packed; 7302676Seschrock size_t len; 7312676Seschrock 7322676Seschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 7332676Seschrock 7342676Seschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 7352676Seschrock return (-1); 7362676Seschrock 7372676Seschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 7382676Seschrock 7395094Slling *outnv = (uint64_t)(uintptr_t)packed; 7405094Slling *outlen = len; 7415094Slling 7425094Slling return (0); 7435094Slling } 7442676Seschrock 7455094Slling int 7465094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7475094Slling { 7485094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 7495094Slling &zc->zc_nvlist_conf_size, nvl)); 7505094Slling } 7515094Slling 7525094Slling int 7535094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7545094Slling { 7555094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 7565094Slling &zc->zc_nvlist_src_size, nvl)); 7572676Seschrock } 7582676Seschrock 7592676Seschrock /* 7602676Seschrock * Unpacks an nvlist from the ZFS ioctl command structure. 7612676Seschrock */ 7622676Seschrock int 7632676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 7642676Seschrock { 7652676Seschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 7662676Seschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 7672676Seschrock return (no_memory(hdl)); 7682676Seschrock 7692676Seschrock return (0); 7702676Seschrock } 7713912Slling 7725094Slling int 7735094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 7745094Slling { 7755094Slling int error; 7765094Slling 7775094Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 7785094Slling error = ioctl(hdl->libzfs_fd, request, zc); 7795094Slling if (hdl->libzfs_log_str) { 7805094Slling free(hdl->libzfs_log_str); 7815094Slling hdl->libzfs_log_str = NULL; 7825094Slling } 7835094Slling zc->zc_history = 0; 7845094Slling 7855094Slling return (error); 7865094Slling } 7875094Slling 7885094Slling /* 7895094Slling * ================================================================ 7905094Slling * API shared by zfs and zpool property management 7915094Slling * ================================================================ 7925094Slling */ 7935094Slling 7943912Slling static void 7955094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 7963912Slling { 7975094Slling zprop_list_t *pl = cbp->cb_proplist; 7983912Slling int i; 7993912Slling char *title; 8003912Slling size_t len; 8013912Slling 8023912Slling cbp->cb_first = B_FALSE; 8033912Slling if (cbp->cb_scripted) 8043912Slling return; 8053912Slling 8063912Slling /* 8073912Slling * Start with the length of the column headers. 8083912Slling */ 8093912Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 8103912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 8113912Slling "PROPERTY")); 8123912Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 8133912Slling "VALUE")); 8143912Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 8153912Slling "SOURCE")); 8163912Slling 8178269SMark.Musante@Sun.COM /* first property is always NAME */ 8188269SMark.Musante@Sun.COM assert(cbp->cb_proplist->pl_prop == 8198269SMark.Musante@Sun.COM ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 8208269SMark.Musante@Sun.COM 8213912Slling /* 8223912Slling * Go through and calculate the widths for each column. For the 8233912Slling * 'source' column, we kludge it up by taking the worst-case scenario of 8243912Slling * inheriting from the longest name. This is acceptable because in the 8253912Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 8263912Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 8273912Slling * if the name of the property is much longer the any values we find. 8283912Slling */ 8293912Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 8303912Slling /* 8313912Slling * 'PROPERTY' column 8323912Slling */ 8335094Slling if (pl->pl_prop != ZPROP_INVAL) { 8345094Slling const char *propname = (type == ZFS_TYPE_POOL) ? 8355094Slling zpool_prop_to_name(pl->pl_prop) : 8365094Slling zfs_prop_to_name(pl->pl_prop); 8375094Slling 8385094Slling len = strlen(propname); 8393912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8403912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8413912Slling } else { 8423912Slling len = strlen(pl->pl_user_prop); 8433912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8443912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8453912Slling } 8463912Slling 8473912Slling /* 8488269SMark.Musante@Sun.COM * 'VALUE' column. The first property is always the 'name' 8498269SMark.Musante@Sun.COM * property that was tacked on either by /sbin/zfs's 8508269SMark.Musante@Sun.COM * zfs_do_get() or when calling zprop_expand_list(), so we 8518269SMark.Musante@Sun.COM * ignore its width. If the user specified the name property 8528269SMark.Musante@Sun.COM * to display, then it will be later in the list in any case. 8533912Slling */ 8548269SMark.Musante@Sun.COM if (pl != cbp->cb_proplist && 8553912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 8563912Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 8573912Slling 8583912Slling /* 8593912Slling * 'NAME' and 'SOURCE' columns 8603912Slling */ 8615094Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 8625094Slling ZFS_PROP_NAME) && 8633912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 8643912Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 8653912Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 8663912Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 8673912Slling } 8683912Slling } 8693912Slling 8703912Slling /* 8713912Slling * Now go through and print the headers. 8723912Slling */ 8733912Slling for (i = 0; i < 4; i++) { 8743912Slling switch (cbp->cb_columns[i]) { 8753912Slling case GET_COL_NAME: 8763912Slling title = dgettext(TEXT_DOMAIN, "NAME"); 8773912Slling break; 8783912Slling case GET_COL_PROPERTY: 8793912Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 8803912Slling break; 8813912Slling case GET_COL_VALUE: 8823912Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 8833912Slling break; 8843912Slling case GET_COL_SOURCE: 8853912Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 8863912Slling break; 8873912Slling default: 8883912Slling title = NULL; 8893912Slling } 8903912Slling 8913912Slling if (title != NULL) { 8923912Slling if (i == 3 || cbp->cb_columns[i + 1] == 0) 8933912Slling (void) printf("%s", title); 8943912Slling else 8953912Slling (void) printf("%-*s ", 8963912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 8973912Slling title); 8983912Slling } 8993912Slling } 9003912Slling (void) printf("\n"); 9013912Slling } 9023912Slling 9033912Slling /* 9043912Slling * Display a single line of output, according to the settings in the callback 9053912Slling * structure. 9063912Slling */ 9073912Slling void 9085094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 9095094Slling const char *propname, const char *value, zprop_source_t sourcetype, 9103912Slling const char *source) 9113912Slling { 9123912Slling int i; 9133912Slling const char *str; 9143912Slling char buf[128]; 9153912Slling 9163912Slling /* 9173912Slling * Ignore those source types that the user has chosen to ignore. 9183912Slling */ 9193912Slling if ((sourcetype & cbp->cb_sources) == 0) 9203912Slling return; 9213912Slling 9223912Slling if (cbp->cb_first) 9235094Slling zprop_print_headers(cbp, cbp->cb_type); 9243912Slling 9253912Slling for (i = 0; i < 4; i++) { 9263912Slling switch (cbp->cb_columns[i]) { 9273912Slling case GET_COL_NAME: 9283912Slling str = name; 9293912Slling break; 9303912Slling 9313912Slling case GET_COL_PROPERTY: 9323912Slling str = propname; 9333912Slling break; 9343912Slling 9353912Slling case GET_COL_VALUE: 9363912Slling str = value; 9373912Slling break; 9383912Slling 9393912Slling case GET_COL_SOURCE: 9403912Slling switch (sourcetype) { 9415094Slling case ZPROP_SRC_NONE: 9423912Slling str = "-"; 9433912Slling break; 9443912Slling 9455094Slling case ZPROP_SRC_DEFAULT: 9463912Slling str = "default"; 9473912Slling break; 9483912Slling 9495094Slling case ZPROP_SRC_LOCAL: 9503912Slling str = "local"; 9513912Slling break; 9523912Slling 9535094Slling case ZPROP_SRC_TEMPORARY: 9543912Slling str = "temporary"; 9553912Slling break; 9563912Slling 9575094Slling case ZPROP_SRC_INHERITED: 9583912Slling (void) snprintf(buf, sizeof (buf), 9593912Slling "inherited from %s", source); 9603912Slling str = buf; 9613912Slling break; 9623912Slling } 9633912Slling break; 9643912Slling 9653912Slling default: 9663912Slling continue; 9673912Slling } 9683912Slling 9693912Slling if (cbp->cb_columns[i + 1] == 0) 9703912Slling (void) printf("%s", str); 9713912Slling else if (cbp->cb_scripted) 9723912Slling (void) printf("%s\t", str); 9733912Slling else 9743912Slling (void) printf("%-*s ", 9753912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 9763912Slling str); 9773912Slling 9783912Slling } 9793912Slling 9803912Slling (void) printf("\n"); 9813912Slling } 9824543Smarks 9835094Slling /* 9845094Slling * Given a numeric suffix, convert the value into a number of bits that the 9855094Slling * resulting value must be shifted. 9865094Slling */ 9875094Slling static int 9885094Slling str2shift(libzfs_handle_t *hdl, const char *buf) 9895094Slling { 9905094Slling const char *ends = "BKMGTPEZ"; 9915094Slling int i; 9925094Slling 9935094Slling if (buf[0] == '\0') 9945094Slling return (0); 9955094Slling for (i = 0; i < strlen(ends); i++) { 9965094Slling if (toupper(buf[0]) == ends[i]) 9975094Slling break; 9985094Slling } 9995094Slling if (i == strlen(ends)) { 10005094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10015094Slling "invalid numeric suffix '%s'"), buf); 10025094Slling return (-1); 10035094Slling } 10045094Slling 10055094Slling /* 10065094Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 10075094Slling * allow 'BB' - that's just weird. 10085094Slling */ 10095094Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 10105094Slling toupper(buf[0]) != 'B')) 10115094Slling return (10*i); 10125094Slling 10135094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10145094Slling "invalid numeric suffix '%s'"), buf); 10155094Slling return (-1); 10165094Slling } 10175094Slling 10185094Slling /* 10195094Slling * Convert a string of the form '100G' into a real number. Used when setting 10205094Slling * properties or creating a volume. 'buf' is used to place an extended error 10215094Slling * message for the caller to use. 10225094Slling */ 10234543Smarks int 10245094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 10255094Slling { 10265094Slling char *end; 10275094Slling int shift; 10285094Slling 10295094Slling *num = 0; 10305094Slling 10315094Slling /* Check to see if this looks like a number. */ 10325094Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 10335094Slling if (hdl) 10345094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10355094Slling "bad numeric value '%s'"), value); 10365094Slling return (-1); 10375094Slling } 10385094Slling 1039*10817SEric.Schrock@Sun.COM /* Rely on strtoull() to process the numeric portion. */ 10405094Slling errno = 0; 10418343SEric.Schrock@Sun.COM *num = strtoull(value, &end, 10); 10425094Slling 10435094Slling /* 10445094Slling * Check for ERANGE, which indicates that the value is too large to fit 10455094Slling * in a 64-bit value. 10465094Slling */ 10475094Slling if (errno == ERANGE) { 10485094Slling if (hdl) 10495094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10505094Slling "numeric value is too large")); 10515094Slling return (-1); 10525094Slling } 10535094Slling 10545094Slling /* 10555094Slling * If we have a decimal value, then do the computation with floating 10565094Slling * point arithmetic. Otherwise, use standard arithmetic. 10575094Slling */ 10585094Slling if (*end == '.') { 10595094Slling double fval = strtod(value, &end); 10605094Slling 10615094Slling if ((shift = str2shift(hdl, end)) == -1) 10625094Slling return (-1); 10635094Slling 10645094Slling fval *= pow(2, shift); 10655094Slling 10665094Slling if (fval > UINT64_MAX) { 10675094Slling if (hdl) 10685094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10695094Slling "numeric value is too large")); 10705094Slling return (-1); 10715094Slling } 10725094Slling 10735094Slling *num = (uint64_t)fval; 10745094Slling } else { 10755094Slling if ((shift = str2shift(hdl, end)) == -1) 10765094Slling return (-1); 10775094Slling 10785094Slling /* Check for overflow */ 10795094Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 10805094Slling if (hdl) 10815094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10825094Slling "numeric value is too large")); 10835094Slling return (-1); 10845094Slling } 10855094Slling 10865094Slling *num <<= shift; 10875094Slling } 10885094Slling 10895094Slling return (0); 10905094Slling } 10915094Slling 10925094Slling /* 10935094Slling * Given a propname=value nvpair to set, parse any numeric properties 10945094Slling * (index, boolean, etc) if they are specified as strings and add the 10955094Slling * resulting nvpair to the returned nvlist. 10965094Slling * 10975094Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 10985094Slling * We want the user to be able to ignore this fact and specify properties 10995094Slling * as native values (numbers, for example) or as strings (to simplify 11005094Slling * command line utilities). This also handles converting index types 11015094Slling * (compression, checksum, etc) from strings to their on-disk index. 11025094Slling */ 11035094Slling int 11045094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 11055094Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 11065094Slling const char *errbuf) 11074543Smarks { 11085094Slling data_type_t datatype = nvpair_type(elem); 11095094Slling zprop_type_t proptype; 11105094Slling const char *propname; 11115094Slling char *value; 11125094Slling boolean_t isnone = B_FALSE; 11135094Slling 11145094Slling if (type == ZFS_TYPE_POOL) { 11155094Slling proptype = zpool_prop_get_type(prop); 11165094Slling propname = zpool_prop_to_name(prop); 11175094Slling } else { 11185094Slling proptype = zfs_prop_get_type(prop); 11195094Slling propname = zfs_prop_to_name(prop); 11205094Slling } 11215094Slling 11225094Slling /* 11235094Slling * Convert any properties to the internal DSL value types. 11245094Slling */ 11255094Slling *svalp = NULL; 11265094Slling *ivalp = 0; 11275094Slling 11285094Slling switch (proptype) { 11295094Slling case PROP_TYPE_STRING: 11305094Slling if (datatype != DATA_TYPE_STRING) { 11315094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11325094Slling "'%s' must be a string"), nvpair_name(elem)); 11335094Slling goto error; 11345094Slling } 11355094Slling (void) nvpair_value_string(elem, svalp); 11365094Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 11375094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11385094Slling "'%s' is too long"), nvpair_name(elem)); 11395094Slling goto error; 11405094Slling } 11415094Slling break; 11425094Slling 11435094Slling case PROP_TYPE_NUMBER: 11445094Slling if (datatype == DATA_TYPE_STRING) { 11455094Slling (void) nvpair_value_string(elem, &value); 11465094Slling if (strcmp(value, "none") == 0) { 11475094Slling isnone = B_TRUE; 11485094Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 11495094Slling != 0) { 11505094Slling goto error; 11515094Slling } 11525094Slling } else if (datatype == DATA_TYPE_UINT64) { 11535094Slling (void) nvpair_value_uint64(elem, ivalp); 11545094Slling } else { 11555094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11565094Slling "'%s' must be a number"), nvpair_name(elem)); 11575094Slling goto error; 11585094Slling } 11595094Slling 11605094Slling /* 11615094Slling * Quota special: force 'none' and don't allow 0. 11625094Slling */ 11635378Sck153898 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 11645378Sck153898 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 11655094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11665378Sck153898 "use 'none' to disable quota/refquota")); 11675094Slling goto error; 11685094Slling } 11695094Slling break; 11705094Slling 11715094Slling case PROP_TYPE_INDEX: 11725378Sck153898 if (datatype != DATA_TYPE_STRING) { 11735094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11745094Slling "'%s' must be a string"), nvpair_name(elem)); 11755094Slling goto error; 11765094Slling } 11775094Slling 11785378Sck153898 (void) nvpair_value_string(elem, &value); 11795378Sck153898 11805094Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 11815094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11825094Slling "'%s' must be one of '%s'"), propname, 11835094Slling zprop_values(prop, type)); 11845094Slling goto error; 11855094Slling } 11865094Slling break; 11875094Slling 11885094Slling default: 11895094Slling abort(); 11905094Slling } 11914543Smarks 11925094Slling /* 11935094Slling * Add the result to our return set of properties. 11945094Slling */ 11955094Slling if (*svalp != NULL) { 11965094Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 11975094Slling (void) no_memory(hdl); 11985094Slling return (-1); 11995094Slling } 12005094Slling } else { 12015094Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 12025094Slling (void) no_memory(hdl); 12035094Slling return (-1); 12045094Slling } 12055094Slling } 12065094Slling 12075094Slling return (0); 12085094Slling error: 12095094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 12105094Slling return (-1); 12115094Slling } 12125094Slling 12137390SMatthew.Ahrens@Sun.COM static int 12147390SMatthew.Ahrens@Sun.COM addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 12157390SMatthew.Ahrens@Sun.COM zfs_type_t type) 12167390SMatthew.Ahrens@Sun.COM { 12177390SMatthew.Ahrens@Sun.COM int prop; 12187390SMatthew.Ahrens@Sun.COM zprop_list_t *entry; 12197390SMatthew.Ahrens@Sun.COM 12207390SMatthew.Ahrens@Sun.COM prop = zprop_name_to_prop(propname, type); 12217390SMatthew.Ahrens@Sun.COM 12227390SMatthew.Ahrens@Sun.COM if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 12237390SMatthew.Ahrens@Sun.COM prop = ZPROP_INVAL; 12247390SMatthew.Ahrens@Sun.COM 12257390SMatthew.Ahrens@Sun.COM /* 12267390SMatthew.Ahrens@Sun.COM * When no property table entry can be found, return failure if 12277390SMatthew.Ahrens@Sun.COM * this is a pool property or if this isn't a user-defined 12287390SMatthew.Ahrens@Sun.COM * dataset property, 12297390SMatthew.Ahrens@Sun.COM */ 12307390SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 12319396SMatthew.Ahrens@Sun.COM (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) { 12327390SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12337390SMatthew.Ahrens@Sun.COM "invalid property '%s'"), propname); 12347390SMatthew.Ahrens@Sun.COM return (zfs_error(hdl, EZFS_BADPROP, 12357390SMatthew.Ahrens@Sun.COM dgettext(TEXT_DOMAIN, "bad property list"))); 12367390SMatthew.Ahrens@Sun.COM } 12377390SMatthew.Ahrens@Sun.COM 12387390SMatthew.Ahrens@Sun.COM if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 12397390SMatthew.Ahrens@Sun.COM return (-1); 12407390SMatthew.Ahrens@Sun.COM 12417390SMatthew.Ahrens@Sun.COM entry->pl_prop = prop; 12427390SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL) { 12437390SMatthew.Ahrens@Sun.COM if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) { 12447390SMatthew.Ahrens@Sun.COM free(entry); 12457390SMatthew.Ahrens@Sun.COM return (-1); 12467390SMatthew.Ahrens@Sun.COM } 12477390SMatthew.Ahrens@Sun.COM entry->pl_width = strlen(propname); 12487390SMatthew.Ahrens@Sun.COM } else { 12497390SMatthew.Ahrens@Sun.COM entry->pl_width = zprop_width(prop, &entry->pl_fixed, 12507390SMatthew.Ahrens@Sun.COM type); 12517390SMatthew.Ahrens@Sun.COM } 12527390SMatthew.Ahrens@Sun.COM 12537390SMatthew.Ahrens@Sun.COM *listp = entry; 12547390SMatthew.Ahrens@Sun.COM 12557390SMatthew.Ahrens@Sun.COM return (0); 12567390SMatthew.Ahrens@Sun.COM } 12577390SMatthew.Ahrens@Sun.COM 12585094Slling /* 12595094Slling * Given a comma-separated list of properties, construct a property list 12605094Slling * containing both user-defined and native properties. This function will 12615094Slling * return a NULL list if 'all' is specified, which can later be expanded 12625094Slling * by zprop_expand_list(). 12635094Slling */ 12645094Slling int 12655094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 12665094Slling zfs_type_t type) 12675094Slling { 12685094Slling *listp = NULL; 12695094Slling 12705094Slling /* 12715094Slling * If 'all' is specified, return a NULL list. 12725094Slling */ 12735094Slling if (strcmp(props, "all") == 0) 12745094Slling return (0); 12755094Slling 12765094Slling /* 12775094Slling * If no props were specified, return an error. 12785094Slling */ 12795094Slling if (props[0] == '\0') { 12805094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12815094Slling "no properties specified")); 12825094Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 12835094Slling "bad property list"))); 12844543Smarks } 12855094Slling 12865094Slling /* 12875094Slling * It would be nice to use getsubopt() here, but the inclusion of column 12885094Slling * aliases makes this more effort than it's worth. 12895094Slling */ 12907390SMatthew.Ahrens@Sun.COM while (*props != '\0') { 12917390SMatthew.Ahrens@Sun.COM size_t len; 12927390SMatthew.Ahrens@Sun.COM char *p; 12937390SMatthew.Ahrens@Sun.COM char c; 12947390SMatthew.Ahrens@Sun.COM 12957390SMatthew.Ahrens@Sun.COM if ((p = strchr(props, ',')) == NULL) { 12967390SMatthew.Ahrens@Sun.COM len = strlen(props); 12977390SMatthew.Ahrens@Sun.COM p = props + len; 12985094Slling } else { 12997390SMatthew.Ahrens@Sun.COM len = p - props; 13005094Slling } 13015094Slling 13025094Slling /* 13035094Slling * Check for empty options. 13045094Slling */ 13055094Slling if (len == 0) { 13065094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13075094Slling "empty property name")); 13085094Slling return (zfs_error(hdl, EZFS_BADPROP, 13095094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 13105094Slling } 13115094Slling 13125094Slling /* 13135094Slling * Check all regular property names. 13145094Slling */ 13157390SMatthew.Ahrens@Sun.COM c = props[len]; 13167390SMatthew.Ahrens@Sun.COM props[len] = '\0'; 13175094Slling 13187390SMatthew.Ahrens@Sun.COM if (strcmp(props, "space") == 0) { 13197390SMatthew.Ahrens@Sun.COM static char *spaceprops[] = { 13207390SMatthew.Ahrens@Sun.COM "name", "avail", "used", "usedbysnapshots", 13217390SMatthew.Ahrens@Sun.COM "usedbydataset", "usedbyrefreservation", 13227390SMatthew.Ahrens@Sun.COM "usedbychildren", NULL 13237390SMatthew.Ahrens@Sun.COM }; 13247390SMatthew.Ahrens@Sun.COM int i; 13257390SMatthew.Ahrens@Sun.COM 13267390SMatthew.Ahrens@Sun.COM for (i = 0; spaceprops[i]; i++) { 13277390SMatthew.Ahrens@Sun.COM if (addlist(hdl, spaceprops[i], listp, type)) 13287390SMatthew.Ahrens@Sun.COM return (-1); 13297390SMatthew.Ahrens@Sun.COM listp = &(*listp)->pl_next; 13307390SMatthew.Ahrens@Sun.COM } 13317390SMatthew.Ahrens@Sun.COM } else { 13327390SMatthew.Ahrens@Sun.COM if (addlist(hdl, props, listp, type)) 13337390SMatthew.Ahrens@Sun.COM return (-1); 13347390SMatthew.Ahrens@Sun.COM listp = &(*listp)->pl_next; 13355094Slling } 13365094Slling 13377390SMatthew.Ahrens@Sun.COM props = p; 13385094Slling if (c == ',') 13397390SMatthew.Ahrens@Sun.COM props++; 13405094Slling } 13415094Slling 13425094Slling return (0); 13435094Slling } 13445094Slling 13455094Slling void 13465094Slling zprop_free_list(zprop_list_t *pl) 13475094Slling { 13485094Slling zprop_list_t *next; 13494543Smarks 13505094Slling while (pl != NULL) { 13515094Slling next = pl->pl_next; 13525094Slling free(pl->pl_user_prop); 13535094Slling free(pl); 13545094Slling pl = next; 13555094Slling } 13565094Slling } 13575094Slling 13585094Slling typedef struct expand_data { 13595094Slling zprop_list_t **last; 13605094Slling libzfs_handle_t *hdl; 13615094Slling zfs_type_t type; 13625094Slling } expand_data_t; 13635094Slling 13645094Slling int 13655094Slling zprop_expand_list_cb(int prop, void *cb) 13665094Slling { 13675094Slling zprop_list_t *entry; 13685094Slling expand_data_t *edp = cb; 13695094Slling 13705094Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 13715094Slling return (ZPROP_INVAL); 13725094Slling 13735094Slling entry->pl_prop = prop; 13745094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 13755094Slling entry->pl_all = B_TRUE; 13765094Slling 13775094Slling *(edp->last) = entry; 13785094Slling edp->last = &entry->pl_next; 13795094Slling 13805094Slling return (ZPROP_CONT); 13814543Smarks } 13825094Slling 13835094Slling int 13845094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 13855094Slling { 13865094Slling zprop_list_t *entry; 13875094Slling zprop_list_t **last; 13885094Slling expand_data_t exp; 13895094Slling 13905094Slling if (*plp == NULL) { 13915094Slling /* 13925094Slling * If this is the very first time we've been called for an 'all' 13935094Slling * specification, expand the list to include all native 13945094Slling * properties. 13955094Slling */ 13965094Slling last = plp; 13975094Slling 13985094Slling exp.last = last; 13995094Slling exp.hdl = hdl; 14005094Slling exp.type = type; 14015094Slling 14025094Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 14035094Slling B_FALSE, type) == ZPROP_INVAL) 14045094Slling return (-1); 14055094Slling 14065094Slling /* 14075094Slling * Add 'name' to the beginning of the list, which is handled 14085094Slling * specially. 14095094Slling */ 14105094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 14115094Slling return (-1); 14125094Slling 14135094Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 14145094Slling ZFS_PROP_NAME; 14155094Slling entry->pl_width = zprop_width(entry->pl_prop, 14165094Slling &entry->pl_fixed, type); 14175094Slling entry->pl_all = B_TRUE; 14185094Slling entry->pl_next = *plp; 14195094Slling *plp = entry; 14205094Slling } 14215094Slling return (0); 14225094Slling } 14235094Slling 14245094Slling int 14255094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 14265094Slling zfs_type_t type) 14275094Slling { 14285094Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 14295094Slling } 1430