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_VOLHASDATA: 982082Seschrock return (dgettext(TEXT_DOMAIN, "volume has data")); 992082Seschrock case EZFS_INVALIDNAME: 1002082Seschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 1012082Seschrock case EZFS_BADRESTORE: 1022082Seschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 1032082Seschrock "destination")); 1042082Seschrock case EZFS_BADBACKUP: 1052082Seschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 1062082Seschrock case EZFS_BADTARGET: 1072082Seschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 1082082Seschrock case EZFS_NODEVICE: 1092082Seschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 1102082Seschrock case EZFS_BADDEV: 1112082Seschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 1122082Seschrock case EZFS_NOREPLICAS: 1132082Seschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 1142082Seschrock case EZFS_RESILVERING: 1152082Seschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 1162082Seschrock case EZFS_BADVERSION: 1172082Seschrock return (dgettext(TEXT_DOMAIN, "unsupported version")); 1182082Seschrock case EZFS_POOLUNAVAIL: 1192082Seschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 1202082Seschrock case EZFS_DEVOVERFLOW: 1212082Seschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 1222082Seschrock case EZFS_BADPATH: 1232082Seschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 1242082Seschrock case EZFS_CROSSTARGET: 1252082Seschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 1262082Seschrock "pools")); 1272082Seschrock case EZFS_ZONED: 1282082Seschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 1292082Seschrock case EZFS_MOUNTFAILED: 1302082Seschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 1312082Seschrock case EZFS_UMOUNTFAILED: 1322082Seschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 1333126Sahl case EZFS_UNSHARENFSFAILED: 1342082Seschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 1353126Sahl case EZFS_SHARENFSFAILED: 1362082Seschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 1375331Samw case EZFS_UNSHARESMBFAILED: 1385331Samw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 1395331Samw case EZFS_SHARESMBFAILED: 1405331Samw return (dgettext(TEXT_DOMAIN, "smb add share failed")); 1414543Smarks case EZFS_ISCSISVCUNAVAIL: 1424543Smarks return (dgettext(TEXT_DOMAIN, 1434543Smarks "iscsitgt service need to be enabled by " 1444543Smarks "a privileged user")); 1452082Seschrock case EZFS_DEVLINKS: 1462082Seschrock return (dgettext(TEXT_DOMAIN, "failed to create /dev links")); 1472082Seschrock case EZFS_PERM: 1482082Seschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 1492082Seschrock case EZFS_NOSPC: 1502082Seschrock return (dgettext(TEXT_DOMAIN, "out of space")); 1512082Seschrock case EZFS_IO: 1522082Seschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 1532082Seschrock case EZFS_INTR: 1542082Seschrock return (dgettext(TEXT_DOMAIN, "signal received")); 1552082Seschrock case EZFS_ISSPARE: 1562082Seschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 1572082Seschrock "spare")); 1582082Seschrock case EZFS_INVALCONFIG: 1592082Seschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1602474Seschrock case EZFS_RECURSIVE: 1612474Seschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 1622926Sek110237 case EZFS_NOHISTORY: 1632926Sek110237 return (dgettext(TEXT_DOMAIN, "no history available")); 1643126Sahl case EZFS_UNSHAREISCSIFAILED: 1653126Sahl return (dgettext(TEXT_DOMAIN, 1663126Sahl "iscsitgtd failed request to unshare")); 1673126Sahl case EZFS_SHAREISCSIFAILED: 1683126Sahl return (dgettext(TEXT_DOMAIN, 1693126Sahl "iscsitgtd failed request to share")); 1703912Slling case EZFS_POOLPROPS: 1713912Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 1723912Slling "pool properties")); 1733912Slling case EZFS_POOL_NOTSUP: 1743912Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 1753912Slling "on this type of pool")); 1763912Slling case EZFS_POOL_INVALARG: 1773912Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 1783912Slling "this pool operation")); 1793978Smmusante case EZFS_NAMETOOLONG: 1803978Smmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 1814276Staylor case EZFS_OPENFAILED: 1824276Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 1834276Staylor case EZFS_NOCAP: 1844276Staylor return (dgettext(TEXT_DOMAIN, 1854276Staylor "disk capacity information could not be retrieved")); 1864276Staylor case EZFS_LABELFAILED: 1874276Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 1884543Smarks case EZFS_BADWHO: 1894543Smarks return (dgettext(TEXT_DOMAIN, "invalid user/group")); 1904543Smarks case EZFS_BADPERM: 1914543Smarks return (dgettext(TEXT_DOMAIN, "invalid permission")); 1924543Smarks case EZFS_BADPERMSET: 1934543Smarks return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 1944543Smarks case EZFS_NODELEGATION: 1954543Smarks return (dgettext(TEXT_DOMAIN, "delegated administration is " 1964543Smarks "disabled on pool")); 1974543Smarks case EZFS_PERMRDONLY: 1984543Smarks return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be" 1994543Smarks " modified")); 2005363Seschrock case EZFS_BADCACHE: 2015363Seschrock return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 2025450Sbrendan case EZFS_ISL2CACHE: 2035450Sbrendan return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 2046423Sgw25295 case EZFS_VDEVNOTSUP: 2056423Sgw25295 return (dgettext(TEXT_DOMAIN, "vdev specification is not " 2066423Sgw25295 "supported")); 2077042Sgw25295 case EZFS_NOTSUP: 2087042Sgw25295 return (dgettext(TEXT_DOMAIN, "operation not supported " 2097042Sgw25295 "on this dataset")); 2107214Slling case EZFS_ACTIVE_SPARE: 2117214Slling return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 2127214Slling "device")); 2139701SGeorge.Wilson@Sun.COM case EZFS_UNPLAYED_LOGS: 2149701SGeorge.Wilson@Sun.COM return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 2159701SGeorge.Wilson@Sun.COM "logs")); 216*10242Schris.kirby@sun.com case EZFS_REFTAG_RELE: 217*10242Schris.kirby@sun.com return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 218*10242Schris.kirby@sun.com case EZFS_REFTAG_HOLD: 219*10242Schris.kirby@sun.com return (dgettext(TEXT_DOMAIN, "tag already exists on this " 220*10242Schris.kirby@sun.com "dataset")); 2212082Seschrock case EZFS_UNKNOWN: 2222082Seschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 2232082Seschrock default: 2242500Seschrock assert(hdl->libzfs_error == 0); 2252500Seschrock return (dgettext(TEXT_DOMAIN, "no error")); 2262082Seschrock } 2272082Seschrock } 2282082Seschrock 2292082Seschrock /*PRINTFLIKE2*/ 230789Sahrens void 2312082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 232789Sahrens { 233789Sahrens va_list ap; 234789Sahrens 235789Sahrens va_start(ap, fmt); 236789Sahrens 2372082Seschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 2382082Seschrock fmt, ap); 2392082Seschrock hdl->libzfs_desc_active = 1; 240789Sahrens 241789Sahrens va_end(ap); 242789Sahrens } 243789Sahrens 2442082Seschrock static void 2452082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 2462082Seschrock { 2472082Seschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 2482082Seschrock fmt, ap); 2492082Seschrock hdl->libzfs_error = error; 2502082Seschrock 2512082Seschrock if (hdl->libzfs_desc_active) 2522082Seschrock hdl->libzfs_desc_active = 0; 2532082Seschrock else 2542082Seschrock hdl->libzfs_desc[0] = '\0'; 2552082Seschrock 2562082Seschrock if (hdl->libzfs_printerr) { 2572082Seschrock if (error == EZFS_UNKNOWN) { 2582082Seschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 2592082Seschrock "error: %s\n"), libzfs_error_description(hdl)); 2602082Seschrock abort(); 2612082Seschrock } 2622082Seschrock 2632082Seschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 2643912Slling libzfs_error_description(hdl)); 2652082Seschrock if (error == EZFS_NOMEM) 2662082Seschrock exit(1); 2672082Seschrock } 2682082Seschrock } 2692082Seschrock 2703237Slling int 2713237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 2723237Slling { 2733237Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 2743237Slling } 2753237Slling 2762082Seschrock /*PRINTFLIKE3*/ 2772082Seschrock int 2783237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 2792082Seschrock { 2802082Seschrock va_list ap; 2812082Seschrock 2822082Seschrock va_start(ap, fmt); 2832082Seschrock 2842082Seschrock zfs_verror(hdl, error, fmt, ap); 2852082Seschrock 2862082Seschrock va_end(ap); 2872082Seschrock 2882082Seschrock return (-1); 2892082Seschrock } 2902082Seschrock 2912082Seschrock static int 2922082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 2932082Seschrock va_list ap) 2942082Seschrock { 2952082Seschrock switch (error) { 2962082Seschrock case EPERM: 2972082Seschrock case EACCES: 2982082Seschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 2992082Seschrock return (-1); 3002082Seschrock 3014543Smarks case ECANCELED: 3024543Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 3034543Smarks return (-1); 3044543Smarks 3052082Seschrock case EIO: 3062082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 3072082Seschrock return (-1); 3082082Seschrock 3092082Seschrock case EINTR: 3102082Seschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 3112082Seschrock return (-1); 3122082Seschrock } 3132082Seschrock 3142082Seschrock return (0); 3152082Seschrock } 3162082Seschrock 3173237Slling int 3183237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3193237Slling { 3203237Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 3213237Slling } 3223237Slling 3232082Seschrock /*PRINTFLIKE3*/ 3242082Seschrock int 3253237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 326789Sahrens { 327789Sahrens va_list ap; 328789Sahrens 329789Sahrens va_start(ap, fmt); 330789Sahrens 3312082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3322082Seschrock va_end(ap); 3332082Seschrock return (-1); 3342082Seschrock } 3352082Seschrock 3362082Seschrock switch (error) { 3372082Seschrock case ENXIO: 3385807Sck153898 case ENODEV: 3392082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 3402082Seschrock break; 3412082Seschrock 3422082Seschrock case ENOENT: 3432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3442082Seschrock "dataset does not exist")); 3452082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 3462082Seschrock break; 3472082Seschrock 3482082Seschrock case ENOSPC: 3492082Seschrock case EDQUOT: 3502082Seschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 3512082Seschrock return (-1); 3522082Seschrock 3532082Seschrock case EEXIST: 3542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3552082Seschrock "dataset already exists")); 3562082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 3572082Seschrock break; 3582082Seschrock 3592082Seschrock case EBUSY: 3602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3612082Seschrock "dataset is busy")); 3622082Seschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 3632082Seschrock break; 3644543Smarks case EROFS: 3654543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3664543Smarks "snapshot permissions cannot be modified")); 3674543Smarks zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 3684543Smarks break; 3693978Smmusante case ENAMETOOLONG: 3703978Smmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 3713978Smmusante break; 3725860Sck153898 case ENOTSUP: 3735860Sck153898 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 3745860Sck153898 break; 3759234SGeorge.Wilson@Sun.COM case EAGAIN: 3769234SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3779234SGeorge.Wilson@Sun.COM "pool I/O is currently suspended")); 3789234SGeorge.Wilson@Sun.COM zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 3799234SGeorge.Wilson@Sun.COM break; 3802082Seschrock default: 3812082Seschrock zfs_error_aux(hdl, strerror(errno)); 3822082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 3832082Seschrock break; 384789Sahrens } 385789Sahrens 386789Sahrens va_end(ap); 3872082Seschrock return (-1); 388789Sahrens } 389789Sahrens 3903237Slling int 3913237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3923237Slling { 3933237Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 3943237Slling } 3953237Slling 3962082Seschrock /*PRINTFLIKE3*/ 3972082Seschrock int 3983237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 399789Sahrens { 4002082Seschrock va_list ap; 4012082Seschrock 4022082Seschrock va_start(ap, fmt); 4032082Seschrock 4042082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 4052082Seschrock va_end(ap); 4062082Seschrock return (-1); 4072082Seschrock } 4082082Seschrock 4092082Seschrock switch (error) { 4102082Seschrock case ENODEV: 4112082Seschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 4122082Seschrock break; 4132082Seschrock 4142082Seschrock case ENOENT: 4153912Slling zfs_error_aux(hdl, 4163912Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 4172082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 4182082Seschrock break; 4192082Seschrock 4202082Seschrock case EEXIST: 4212082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4222082Seschrock "pool already exists")); 4232082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 4242082Seschrock break; 4252082Seschrock 4262082Seschrock case EBUSY: 4272082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 4287341SEric.Schrock@Sun.COM zfs_verror(hdl, EZFS_BUSY, fmt, ap); 4292082Seschrock break; 4302082Seschrock 4312082Seschrock case ENXIO: 4322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4332082Seschrock "one or more devices is currently unavailable")); 4342082Seschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 4352082Seschrock break; 4362082Seschrock 4372082Seschrock case ENAMETOOLONG: 4382082Seschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 4392082Seschrock break; 4402082Seschrock 4413912Slling case ENOTSUP: 4423912Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 4433912Slling break; 4443912Slling 4453912Slling case EINVAL: 4463912Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 4473912Slling break; 4483912Slling 4494543Smarks case ENOSPC: 4504543Smarks case EDQUOT: 4514543Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 4524543Smarks return (-1); 4539234SGeorge.Wilson@Sun.COM case EAGAIN: 4549234SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4559234SGeorge.Wilson@Sun.COM "pool I/O is currently suspended")); 4569234SGeorge.Wilson@Sun.COM zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 4579234SGeorge.Wilson@Sun.COM break; 4584543Smarks 4592082Seschrock default: 4602082Seschrock zfs_error_aux(hdl, strerror(error)); 4612082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 4622082Seschrock } 4632082Seschrock 4642082Seschrock va_end(ap); 4652082Seschrock return (-1); 466789Sahrens } 467789Sahrens 468789Sahrens /* 469789Sahrens * Display an out of memory error message and abort the current program. 470789Sahrens */ 4712082Seschrock int 4722082Seschrock no_memory(libzfs_handle_t *hdl) 473789Sahrens { 4742082Seschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 475789Sahrens } 476789Sahrens 477789Sahrens /* 478789Sahrens * A safe form of malloc() which will die if the allocation fails. 479789Sahrens */ 480789Sahrens void * 4812082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 482789Sahrens { 483789Sahrens void *data; 484789Sahrens 485789Sahrens if ((data = calloc(1, size)) == NULL) 4862082Seschrock (void) no_memory(hdl); 487789Sahrens 488789Sahrens return (data); 489789Sahrens } 490789Sahrens 491789Sahrens /* 4922676Seschrock * A safe form of realloc(), which also zeroes newly allocated space. 4932676Seschrock */ 4942676Seschrock void * 4952676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 4962676Seschrock { 4972676Seschrock void *ret; 4982676Seschrock 4992676Seschrock if ((ret = realloc(ptr, newsize)) == NULL) { 5002676Seschrock (void) no_memory(hdl); 5012676Seschrock return (NULL); 5022676Seschrock } 5032676Seschrock 5042676Seschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 5052676Seschrock return (ret); 5062676Seschrock } 5072676Seschrock 5082676Seschrock /* 509789Sahrens * A safe form of strdup() which will die if the allocation fails. 510789Sahrens */ 511789Sahrens char * 5122082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 513789Sahrens { 514789Sahrens char *ret; 515789Sahrens 516789Sahrens if ((ret = strdup(str)) == NULL) 5172082Seschrock (void) no_memory(hdl); 518789Sahrens 519789Sahrens return (ret); 520789Sahrens } 521789Sahrens 522789Sahrens /* 523789Sahrens * Convert a number to an appropriately human-readable output. 524789Sahrens */ 525789Sahrens void 526789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 527789Sahrens { 528789Sahrens uint64_t n = num; 529789Sahrens int index = 0; 530789Sahrens char u; 531789Sahrens 532789Sahrens while (n >= 1024) { 5331162Seschrock n /= 1024; 534789Sahrens index++; 535789Sahrens } 536789Sahrens 537789Sahrens u = " KMGTPE"[index]; 538789Sahrens 5391162Seschrock if (index == 0) { 540789Sahrens (void) snprintf(buf, buflen, "%llu", n); 5411162Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5421162Seschrock /* 5431162Seschrock * If this is an even multiple of the base, always display 5441162Seschrock * without any decimal precision. 5451162Seschrock */ 546789Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5471162Seschrock } else { 5481162Seschrock /* 5491162Seschrock * We want to choose a precision that reflects the best choice 5501162Seschrock * for fitting in 5 characters. This can get rather tricky when 5511162Seschrock * we have numbers that are very close to an order of magnitude. 5521162Seschrock * For example, when displaying 10239 (which is really 9.999K), 5531162Seschrock * we want only a single place of precision for 10.0K. We could 5541162Seschrock * develop some complex heuristics for this, but it's much 5551162Seschrock * easier just to try each combination in turn. 5561162Seschrock */ 5571162Seschrock int i; 5581162Seschrock for (i = 2; i >= 0; i--) { 5594451Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5604451Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5611162Seschrock break; 5621162Seschrock } 5631162Seschrock } 564789Sahrens } 5652082Seschrock 5662082Seschrock void 5672082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 5682082Seschrock { 5692082Seschrock hdl->libzfs_printerr = printerr; 5702082Seschrock } 5712082Seschrock 5722082Seschrock libzfs_handle_t * 5732082Seschrock libzfs_init(void) 5742082Seschrock { 5752082Seschrock libzfs_handle_t *hdl; 5762082Seschrock 5772082Seschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 5782082Seschrock return (NULL); 5792082Seschrock } 5802082Seschrock 5812500Seschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 5822082Seschrock free(hdl); 5832082Seschrock return (NULL); 5842082Seschrock } 5852082Seschrock 5862082Seschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 5872082Seschrock (void) close(hdl->libzfs_fd); 5882082Seschrock free(hdl); 5892082Seschrock return (NULL); 5902082Seschrock } 5912082Seschrock 5922082Seschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 5932082Seschrock 5944787Sahrens zfs_prop_init(); 5955094Slling zpool_prop_init(); 5968811SEric.Taylor@Sun.COM libzfs_mnttab_init(hdl); 5974787Sahrens 5982082Seschrock return (hdl); 5992082Seschrock } 6002082Seschrock 6012082Seschrock void 6022082Seschrock libzfs_fini(libzfs_handle_t *hdl) 6032082Seschrock { 6042082Seschrock (void) close(hdl->libzfs_fd); 6052082Seschrock if (hdl->libzfs_mnttab) 6062082Seschrock (void) fclose(hdl->libzfs_mnttab); 6072082Seschrock if (hdl->libzfs_sharetab) 6082082Seschrock (void) fclose(hdl->libzfs_sharetab); 6094180Sdougm zfs_uninit_libshare(hdl); 6104543Smarks if (hdl->libzfs_log_str) 6114543Smarks (void) free(hdl->libzfs_log_str); 6126865Srm160521 zpool_free_handles(hdl); 6132082Seschrock namespace_clear(hdl); 6148811SEric.Taylor@Sun.COM libzfs_mnttab_fini(hdl); 6152082Seschrock free(hdl); 6162082Seschrock } 6172082Seschrock 6182082Seschrock libzfs_handle_t * 6192082Seschrock zpool_get_handle(zpool_handle_t *zhp) 6202082Seschrock { 6212082Seschrock return (zhp->zpool_hdl); 6222082Seschrock } 6232082Seschrock 6242082Seschrock libzfs_handle_t * 6252082Seschrock zfs_get_handle(zfs_handle_t *zhp) 6262082Seschrock { 6272082Seschrock return (zhp->zfs_hdl); 6282082Seschrock } 6292676Seschrock 6307538SRichard.Morris@Sun.COM zpool_handle_t * 6317538SRichard.Morris@Sun.COM zfs_get_pool_handle(const zfs_handle_t *zhp) 6327538SRichard.Morris@Sun.COM { 6337538SRichard.Morris@Sun.COM return (zhp->zpool_hdl); 6347538SRichard.Morris@Sun.COM } 6357538SRichard.Morris@Sun.COM 6362676Seschrock /* 6373635Sck153898 * Given a name, determine whether or not it's a valid path 6383635Sck153898 * (starts with '/' or "./"). If so, walk the mnttab trying 6393635Sck153898 * to match the device number. If not, treat the path as an 6403635Sck153898 * fs/vol/snap name. 6413635Sck153898 */ 6423635Sck153898 zfs_handle_t * 6433635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6443635Sck153898 { 6453635Sck153898 struct stat64 statbuf; 6463635Sck153898 struct extmnttab entry; 6473635Sck153898 int ret; 6483635Sck153898 6493635Sck153898 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6503635Sck153898 /* 6513635Sck153898 * It's not a valid path, assume it's a name of type 'argtype'. 6523635Sck153898 */ 6533635Sck153898 return (zfs_open(hdl, path, argtype)); 6543635Sck153898 } 6553635Sck153898 6563635Sck153898 if (stat64(path, &statbuf) != 0) { 6573635Sck153898 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6583635Sck153898 return (NULL); 6593635Sck153898 } 6603635Sck153898 6613635Sck153898 rewind(hdl->libzfs_mnttab); 6623635Sck153898 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6633635Sck153898 if (makedevice(entry.mnt_major, entry.mnt_minor) == 6643635Sck153898 statbuf.st_dev) { 6653635Sck153898 break; 6663635Sck153898 } 6673635Sck153898 } 6683635Sck153898 if (ret != 0) { 6693635Sck153898 return (NULL); 6703635Sck153898 } 6713635Sck153898 6723635Sck153898 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6733635Sck153898 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 6743635Sck153898 path); 6753635Sck153898 return (NULL); 6763635Sck153898 } 6773635Sck153898 6783635Sck153898 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6793635Sck153898 } 6803635Sck153898 6813635Sck153898 /* 6822676Seschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 6832676Seschrock * an ioctl(). 6842676Seschrock */ 6852676Seschrock int 6862676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 6872676Seschrock { 6882676Seschrock if (len == 0) 6892885Sahrens len = 2048; 6902676Seschrock zc->zc_nvlist_dst_size = len; 6912676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6922676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 6932676Seschrock return (-1); 6942676Seschrock 6952676Seschrock return (0); 6962676Seschrock } 6972676Seschrock 6982676Seschrock /* 6992676Seschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 7002676Seschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 7012676Seschrock * filled in by the kernel to indicate the actual required size. 7022676Seschrock */ 7032676Seschrock int 7042676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 7052676Seschrock { 7062676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 7072676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 7082676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 7092676Seschrock == NULL) 7102676Seschrock return (-1); 7112676Seschrock 7122676Seschrock return (0); 7132676Seschrock } 7142676Seschrock 7152676Seschrock /* 7162885Sahrens * Called to free the src and dst nvlists stored in the command structure. 7172676Seschrock */ 7182676Seschrock void 7192676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc) 7202676Seschrock { 7215094Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 7222676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 7232676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 7242676Seschrock } 7252676Seschrock 7265094Slling static int 7275094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 7285094Slling nvlist_t *nvl) 7292676Seschrock { 7302676Seschrock char *packed; 7312676Seschrock size_t len; 7322676Seschrock 7332676Seschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 7342676Seschrock 7352676Seschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 7362676Seschrock return (-1); 7372676Seschrock 7382676Seschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 7392676Seschrock 7405094Slling *outnv = (uint64_t)(uintptr_t)packed; 7415094Slling *outlen = len; 7425094Slling 7435094Slling return (0); 7445094Slling } 7452676Seschrock 7465094Slling int 7475094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7485094Slling { 7495094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 7505094Slling &zc->zc_nvlist_conf_size, nvl)); 7515094Slling } 7525094Slling 7535094Slling int 7545094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7555094Slling { 7565094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 7575094Slling &zc->zc_nvlist_src_size, nvl)); 7582676Seschrock } 7592676Seschrock 7602676Seschrock /* 7612676Seschrock * Unpacks an nvlist from the ZFS ioctl command structure. 7622676Seschrock */ 7632676Seschrock int 7642676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 7652676Seschrock { 7662676Seschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 7672676Seschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 7682676Seschrock return (no_memory(hdl)); 7692676Seschrock 7702676Seschrock return (0); 7712676Seschrock } 7723912Slling 7735094Slling int 7745094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 7755094Slling { 7765094Slling int error; 7775094Slling 7785094Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 7795094Slling error = ioctl(hdl->libzfs_fd, request, zc); 7805094Slling if (hdl->libzfs_log_str) { 7815094Slling free(hdl->libzfs_log_str); 7825094Slling hdl->libzfs_log_str = NULL; 7835094Slling } 7845094Slling zc->zc_history = 0; 7855094Slling 7865094Slling return (error); 7875094Slling } 7885094Slling 7895094Slling /* 7905094Slling * ================================================================ 7915094Slling * API shared by zfs and zpool property management 7925094Slling * ================================================================ 7935094Slling */ 7945094Slling 7953912Slling static void 7965094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 7973912Slling { 7985094Slling zprop_list_t *pl = cbp->cb_proplist; 7993912Slling int i; 8003912Slling char *title; 8013912Slling size_t len; 8023912Slling 8033912Slling cbp->cb_first = B_FALSE; 8043912Slling if (cbp->cb_scripted) 8053912Slling return; 8063912Slling 8073912Slling /* 8083912Slling * Start with the length of the column headers. 8093912Slling */ 8103912Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 8113912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 8123912Slling "PROPERTY")); 8133912Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 8143912Slling "VALUE")); 8153912Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 8163912Slling "SOURCE")); 8173912Slling 8188269SMark.Musante@Sun.COM /* first property is always NAME */ 8198269SMark.Musante@Sun.COM assert(cbp->cb_proplist->pl_prop == 8208269SMark.Musante@Sun.COM ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 8218269SMark.Musante@Sun.COM 8223912Slling /* 8233912Slling * Go through and calculate the widths for each column. For the 8243912Slling * 'source' column, we kludge it up by taking the worst-case scenario of 8253912Slling * inheriting from the longest name. This is acceptable because in the 8263912Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 8273912Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 8283912Slling * if the name of the property is much longer the any values we find. 8293912Slling */ 8303912Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 8313912Slling /* 8323912Slling * 'PROPERTY' column 8333912Slling */ 8345094Slling if (pl->pl_prop != ZPROP_INVAL) { 8355094Slling const char *propname = (type == ZFS_TYPE_POOL) ? 8365094Slling zpool_prop_to_name(pl->pl_prop) : 8375094Slling zfs_prop_to_name(pl->pl_prop); 8385094Slling 8395094Slling len = strlen(propname); 8403912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8413912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8423912Slling } else { 8433912Slling len = strlen(pl->pl_user_prop); 8443912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8453912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8463912Slling } 8473912Slling 8483912Slling /* 8498269SMark.Musante@Sun.COM * 'VALUE' column. The first property is always the 'name' 8508269SMark.Musante@Sun.COM * property that was tacked on either by /sbin/zfs's 8518269SMark.Musante@Sun.COM * zfs_do_get() or when calling zprop_expand_list(), so we 8528269SMark.Musante@Sun.COM * ignore its width. If the user specified the name property 8538269SMark.Musante@Sun.COM * to display, then it will be later in the list in any case. 8543912Slling */ 8558269SMark.Musante@Sun.COM if (pl != cbp->cb_proplist && 8563912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 8573912Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 8583912Slling 8593912Slling /* 8603912Slling * 'NAME' and 'SOURCE' columns 8613912Slling */ 8625094Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 8635094Slling ZFS_PROP_NAME) && 8643912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 8653912Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 8663912Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 8673912Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 8683912Slling } 8693912Slling } 8703912Slling 8713912Slling /* 8723912Slling * Now go through and print the headers. 8733912Slling */ 8743912Slling for (i = 0; i < 4; i++) { 8753912Slling switch (cbp->cb_columns[i]) { 8763912Slling case GET_COL_NAME: 8773912Slling title = dgettext(TEXT_DOMAIN, "NAME"); 8783912Slling break; 8793912Slling case GET_COL_PROPERTY: 8803912Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 8813912Slling break; 8823912Slling case GET_COL_VALUE: 8833912Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 8843912Slling break; 8853912Slling case GET_COL_SOURCE: 8863912Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 8873912Slling break; 8883912Slling default: 8893912Slling title = NULL; 8903912Slling } 8913912Slling 8923912Slling if (title != NULL) { 8933912Slling if (i == 3 || cbp->cb_columns[i + 1] == 0) 8943912Slling (void) printf("%s", title); 8953912Slling else 8963912Slling (void) printf("%-*s ", 8973912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 8983912Slling title); 8993912Slling } 9003912Slling } 9013912Slling (void) printf("\n"); 9023912Slling } 9033912Slling 9043912Slling /* 9053912Slling * Display a single line of output, according to the settings in the callback 9063912Slling * structure. 9073912Slling */ 9083912Slling void 9095094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 9105094Slling const char *propname, const char *value, zprop_source_t sourcetype, 9113912Slling const char *source) 9123912Slling { 9133912Slling int i; 9143912Slling const char *str; 9153912Slling char buf[128]; 9163912Slling 9173912Slling /* 9183912Slling * Ignore those source types that the user has chosen to ignore. 9193912Slling */ 9203912Slling if ((sourcetype & cbp->cb_sources) == 0) 9213912Slling return; 9223912Slling 9233912Slling if (cbp->cb_first) 9245094Slling zprop_print_headers(cbp, cbp->cb_type); 9253912Slling 9263912Slling for (i = 0; i < 4; i++) { 9273912Slling switch (cbp->cb_columns[i]) { 9283912Slling case GET_COL_NAME: 9293912Slling str = name; 9303912Slling break; 9313912Slling 9323912Slling case GET_COL_PROPERTY: 9333912Slling str = propname; 9343912Slling break; 9353912Slling 9363912Slling case GET_COL_VALUE: 9373912Slling str = value; 9383912Slling break; 9393912Slling 9403912Slling case GET_COL_SOURCE: 9413912Slling switch (sourcetype) { 9425094Slling case ZPROP_SRC_NONE: 9433912Slling str = "-"; 9443912Slling break; 9453912Slling 9465094Slling case ZPROP_SRC_DEFAULT: 9473912Slling str = "default"; 9483912Slling break; 9493912Slling 9505094Slling case ZPROP_SRC_LOCAL: 9513912Slling str = "local"; 9523912Slling break; 9533912Slling 9545094Slling case ZPROP_SRC_TEMPORARY: 9553912Slling str = "temporary"; 9563912Slling break; 9573912Slling 9585094Slling case ZPROP_SRC_INHERITED: 9593912Slling (void) snprintf(buf, sizeof (buf), 9603912Slling "inherited from %s", source); 9613912Slling str = buf; 9623912Slling break; 9633912Slling } 9643912Slling break; 9653912Slling 9663912Slling default: 9673912Slling continue; 9683912Slling } 9693912Slling 9703912Slling if (cbp->cb_columns[i + 1] == 0) 9713912Slling (void) printf("%s", str); 9723912Slling else if (cbp->cb_scripted) 9733912Slling (void) printf("%s\t", str); 9743912Slling else 9753912Slling (void) printf("%-*s ", 9763912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 9773912Slling str); 9783912Slling 9793912Slling } 9803912Slling 9813912Slling (void) printf("\n"); 9823912Slling } 9834543Smarks 9845094Slling /* 9855094Slling * Given a numeric suffix, convert the value into a number of bits that the 9865094Slling * resulting value must be shifted. 9875094Slling */ 9885094Slling static int 9895094Slling str2shift(libzfs_handle_t *hdl, const char *buf) 9905094Slling { 9915094Slling const char *ends = "BKMGTPEZ"; 9925094Slling int i; 9935094Slling 9945094Slling if (buf[0] == '\0') 9955094Slling return (0); 9965094Slling for (i = 0; i < strlen(ends); i++) { 9975094Slling if (toupper(buf[0]) == ends[i]) 9985094Slling break; 9995094Slling } 10005094Slling if (i == strlen(ends)) { 10015094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10025094Slling "invalid numeric suffix '%s'"), buf); 10035094Slling return (-1); 10045094Slling } 10055094Slling 10065094Slling /* 10075094Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 10085094Slling * allow 'BB' - that's just weird. 10095094Slling */ 10105094Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 10115094Slling toupper(buf[0]) != 'B')) 10125094Slling return (10*i); 10135094Slling 10145094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10155094Slling "invalid numeric suffix '%s'"), buf); 10165094Slling return (-1); 10175094Slling } 10185094Slling 10195094Slling /* 10205094Slling * Convert a string of the form '100G' into a real number. Used when setting 10215094Slling * properties or creating a volume. 'buf' is used to place an extended error 10225094Slling * message for the caller to use. 10235094Slling */ 10244543Smarks int 10255094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 10265094Slling { 10275094Slling char *end; 10285094Slling int shift; 10295094Slling 10305094Slling *num = 0; 10315094Slling 10325094Slling /* Check to see if this looks like a number. */ 10335094Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 10345094Slling if (hdl) 10355094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10365094Slling "bad numeric value '%s'"), value); 10375094Slling return (-1); 10385094Slling } 10395094Slling 10408343SEric.Schrock@Sun.COM /* Rely on stroull() to process the numeric portion. */ 10415094Slling errno = 0; 10428343SEric.Schrock@Sun.COM *num = strtoull(value, &end, 10); 10435094Slling 10445094Slling /* 10455094Slling * Check for ERANGE, which indicates that the value is too large to fit 10465094Slling * in a 64-bit value. 10475094Slling */ 10485094Slling if (errno == ERANGE) { 10495094Slling if (hdl) 10505094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10515094Slling "numeric value is too large")); 10525094Slling return (-1); 10535094Slling } 10545094Slling 10555094Slling /* 10565094Slling * If we have a decimal value, then do the computation with floating 10575094Slling * point arithmetic. Otherwise, use standard arithmetic. 10585094Slling */ 10595094Slling if (*end == '.') { 10605094Slling double fval = strtod(value, &end); 10615094Slling 10625094Slling if ((shift = str2shift(hdl, end)) == -1) 10635094Slling return (-1); 10645094Slling 10655094Slling fval *= pow(2, shift); 10665094Slling 10675094Slling if (fval > UINT64_MAX) { 10685094Slling if (hdl) 10695094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10705094Slling "numeric value is too large")); 10715094Slling return (-1); 10725094Slling } 10735094Slling 10745094Slling *num = (uint64_t)fval; 10755094Slling } else { 10765094Slling if ((shift = str2shift(hdl, end)) == -1) 10775094Slling return (-1); 10785094Slling 10795094Slling /* Check for overflow */ 10805094Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 10815094Slling if (hdl) 10825094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10835094Slling "numeric value is too large")); 10845094Slling return (-1); 10855094Slling } 10865094Slling 10875094Slling *num <<= shift; 10885094Slling } 10895094Slling 10905094Slling return (0); 10915094Slling } 10925094Slling 10935094Slling /* 10945094Slling * Given a propname=value nvpair to set, parse any numeric properties 10955094Slling * (index, boolean, etc) if they are specified as strings and add the 10965094Slling * resulting nvpair to the returned nvlist. 10975094Slling * 10985094Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 10995094Slling * We want the user to be able to ignore this fact and specify properties 11005094Slling * as native values (numbers, for example) or as strings (to simplify 11015094Slling * command line utilities). This also handles converting index types 11025094Slling * (compression, checksum, etc) from strings to their on-disk index. 11035094Slling */ 11045094Slling int 11055094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 11065094Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 11075094Slling const char *errbuf) 11084543Smarks { 11095094Slling data_type_t datatype = nvpair_type(elem); 11105094Slling zprop_type_t proptype; 11115094Slling const char *propname; 11125094Slling char *value; 11135094Slling boolean_t isnone = B_FALSE; 11145094Slling 11155094Slling if (type == ZFS_TYPE_POOL) { 11165094Slling proptype = zpool_prop_get_type(prop); 11175094Slling propname = zpool_prop_to_name(prop); 11185094Slling } else { 11195094Slling proptype = zfs_prop_get_type(prop); 11205094Slling propname = zfs_prop_to_name(prop); 11215094Slling } 11225094Slling 11235094Slling /* 11245094Slling * Convert any properties to the internal DSL value types. 11255094Slling */ 11265094Slling *svalp = NULL; 11275094Slling *ivalp = 0; 11285094Slling 11295094Slling switch (proptype) { 11305094Slling case PROP_TYPE_STRING: 11315094Slling if (datatype != DATA_TYPE_STRING) { 11325094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11335094Slling "'%s' must be a string"), nvpair_name(elem)); 11345094Slling goto error; 11355094Slling } 11365094Slling (void) nvpair_value_string(elem, svalp); 11375094Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 11385094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11395094Slling "'%s' is too long"), nvpair_name(elem)); 11405094Slling goto error; 11415094Slling } 11425094Slling break; 11435094Slling 11445094Slling case PROP_TYPE_NUMBER: 11455094Slling if (datatype == DATA_TYPE_STRING) { 11465094Slling (void) nvpair_value_string(elem, &value); 11475094Slling if (strcmp(value, "none") == 0) { 11485094Slling isnone = B_TRUE; 11495094Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 11505094Slling != 0) { 11515094Slling goto error; 11525094Slling } 11535094Slling } else if (datatype == DATA_TYPE_UINT64) { 11545094Slling (void) nvpair_value_uint64(elem, ivalp); 11555094Slling } else { 11565094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11575094Slling "'%s' must be a number"), nvpair_name(elem)); 11585094Slling goto error; 11595094Slling } 11605094Slling 11615094Slling /* 11625094Slling * Quota special: force 'none' and don't allow 0. 11635094Slling */ 11645378Sck153898 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 11655378Sck153898 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 11665094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11675378Sck153898 "use 'none' to disable quota/refquota")); 11685094Slling goto error; 11695094Slling } 11705094Slling break; 11715094Slling 11725094Slling case PROP_TYPE_INDEX: 11735378Sck153898 if (datatype != DATA_TYPE_STRING) { 11745094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11755094Slling "'%s' must be a string"), nvpair_name(elem)); 11765094Slling goto error; 11775094Slling } 11785094Slling 11795378Sck153898 (void) nvpair_value_string(elem, &value); 11805378Sck153898 11815094Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 11825094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11835094Slling "'%s' must be one of '%s'"), propname, 11845094Slling zprop_values(prop, type)); 11855094Slling goto error; 11865094Slling } 11875094Slling break; 11885094Slling 11895094Slling default: 11905094Slling abort(); 11915094Slling } 11924543Smarks 11935094Slling /* 11945094Slling * Add the result to our return set of properties. 11955094Slling */ 11965094Slling if (*svalp != NULL) { 11975094Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 11985094Slling (void) no_memory(hdl); 11995094Slling return (-1); 12005094Slling } 12015094Slling } else { 12025094Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 12035094Slling (void) no_memory(hdl); 12045094Slling return (-1); 12055094Slling } 12065094Slling } 12075094Slling 12085094Slling return (0); 12095094Slling error: 12105094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 12115094Slling return (-1); 12125094Slling } 12135094Slling 12147390SMatthew.Ahrens@Sun.COM static int 12157390SMatthew.Ahrens@Sun.COM addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 12167390SMatthew.Ahrens@Sun.COM zfs_type_t type) 12177390SMatthew.Ahrens@Sun.COM { 12187390SMatthew.Ahrens@Sun.COM int prop; 12197390SMatthew.Ahrens@Sun.COM zprop_list_t *entry; 12207390SMatthew.Ahrens@Sun.COM 12217390SMatthew.Ahrens@Sun.COM prop = zprop_name_to_prop(propname, type); 12227390SMatthew.Ahrens@Sun.COM 12237390SMatthew.Ahrens@Sun.COM if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 12247390SMatthew.Ahrens@Sun.COM prop = ZPROP_INVAL; 12257390SMatthew.Ahrens@Sun.COM 12267390SMatthew.Ahrens@Sun.COM /* 12277390SMatthew.Ahrens@Sun.COM * When no property table entry can be found, return failure if 12287390SMatthew.Ahrens@Sun.COM * this is a pool property or if this isn't a user-defined 12297390SMatthew.Ahrens@Sun.COM * dataset property, 12307390SMatthew.Ahrens@Sun.COM */ 12317390SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 12329396SMatthew.Ahrens@Sun.COM (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) { 12337390SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12347390SMatthew.Ahrens@Sun.COM "invalid property '%s'"), propname); 12357390SMatthew.Ahrens@Sun.COM return (zfs_error(hdl, EZFS_BADPROP, 12367390SMatthew.Ahrens@Sun.COM dgettext(TEXT_DOMAIN, "bad property list"))); 12377390SMatthew.Ahrens@Sun.COM } 12387390SMatthew.Ahrens@Sun.COM 12397390SMatthew.Ahrens@Sun.COM if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 12407390SMatthew.Ahrens@Sun.COM return (-1); 12417390SMatthew.Ahrens@Sun.COM 12427390SMatthew.Ahrens@Sun.COM entry->pl_prop = prop; 12437390SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL) { 12447390SMatthew.Ahrens@Sun.COM if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) { 12457390SMatthew.Ahrens@Sun.COM free(entry); 12467390SMatthew.Ahrens@Sun.COM return (-1); 12477390SMatthew.Ahrens@Sun.COM } 12487390SMatthew.Ahrens@Sun.COM entry->pl_width = strlen(propname); 12497390SMatthew.Ahrens@Sun.COM } else { 12507390SMatthew.Ahrens@Sun.COM entry->pl_width = zprop_width(prop, &entry->pl_fixed, 12517390SMatthew.Ahrens@Sun.COM type); 12527390SMatthew.Ahrens@Sun.COM } 12537390SMatthew.Ahrens@Sun.COM 12547390SMatthew.Ahrens@Sun.COM *listp = entry; 12557390SMatthew.Ahrens@Sun.COM 12567390SMatthew.Ahrens@Sun.COM return (0); 12577390SMatthew.Ahrens@Sun.COM } 12587390SMatthew.Ahrens@Sun.COM 12595094Slling /* 12605094Slling * Given a comma-separated list of properties, construct a property list 12615094Slling * containing both user-defined and native properties. This function will 12625094Slling * return a NULL list if 'all' is specified, which can later be expanded 12635094Slling * by zprop_expand_list(). 12645094Slling */ 12655094Slling int 12665094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 12675094Slling zfs_type_t type) 12685094Slling { 12695094Slling *listp = NULL; 12705094Slling 12715094Slling /* 12725094Slling * If 'all' is specified, return a NULL list. 12735094Slling */ 12745094Slling if (strcmp(props, "all") == 0) 12755094Slling return (0); 12765094Slling 12775094Slling /* 12785094Slling * If no props were specified, return an error. 12795094Slling */ 12805094Slling if (props[0] == '\0') { 12815094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12825094Slling "no properties specified")); 12835094Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 12845094Slling "bad property list"))); 12854543Smarks } 12865094Slling 12875094Slling /* 12885094Slling * It would be nice to use getsubopt() here, but the inclusion of column 12895094Slling * aliases makes this more effort than it's worth. 12905094Slling */ 12917390SMatthew.Ahrens@Sun.COM while (*props != '\0') { 12927390SMatthew.Ahrens@Sun.COM size_t len; 12937390SMatthew.Ahrens@Sun.COM char *p; 12947390SMatthew.Ahrens@Sun.COM char c; 12957390SMatthew.Ahrens@Sun.COM 12967390SMatthew.Ahrens@Sun.COM if ((p = strchr(props, ',')) == NULL) { 12977390SMatthew.Ahrens@Sun.COM len = strlen(props); 12987390SMatthew.Ahrens@Sun.COM p = props + len; 12995094Slling } else { 13007390SMatthew.Ahrens@Sun.COM len = p - props; 13015094Slling } 13025094Slling 13035094Slling /* 13045094Slling * Check for empty options. 13055094Slling */ 13065094Slling if (len == 0) { 13075094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13085094Slling "empty property name")); 13095094Slling return (zfs_error(hdl, EZFS_BADPROP, 13105094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 13115094Slling } 13125094Slling 13135094Slling /* 13145094Slling * Check all regular property names. 13155094Slling */ 13167390SMatthew.Ahrens@Sun.COM c = props[len]; 13177390SMatthew.Ahrens@Sun.COM props[len] = '\0'; 13185094Slling 13197390SMatthew.Ahrens@Sun.COM if (strcmp(props, "space") == 0) { 13207390SMatthew.Ahrens@Sun.COM static char *spaceprops[] = { 13217390SMatthew.Ahrens@Sun.COM "name", "avail", "used", "usedbysnapshots", 13227390SMatthew.Ahrens@Sun.COM "usedbydataset", "usedbyrefreservation", 13237390SMatthew.Ahrens@Sun.COM "usedbychildren", NULL 13247390SMatthew.Ahrens@Sun.COM }; 13257390SMatthew.Ahrens@Sun.COM int i; 13267390SMatthew.Ahrens@Sun.COM 13277390SMatthew.Ahrens@Sun.COM for (i = 0; spaceprops[i]; i++) { 13287390SMatthew.Ahrens@Sun.COM if (addlist(hdl, spaceprops[i], listp, type)) 13297390SMatthew.Ahrens@Sun.COM return (-1); 13307390SMatthew.Ahrens@Sun.COM listp = &(*listp)->pl_next; 13317390SMatthew.Ahrens@Sun.COM } 13327390SMatthew.Ahrens@Sun.COM } else { 13337390SMatthew.Ahrens@Sun.COM if (addlist(hdl, props, listp, type)) 13347390SMatthew.Ahrens@Sun.COM return (-1); 13357390SMatthew.Ahrens@Sun.COM listp = &(*listp)->pl_next; 13365094Slling } 13375094Slling 13387390SMatthew.Ahrens@Sun.COM props = p; 13395094Slling if (c == ',') 13407390SMatthew.Ahrens@Sun.COM props++; 13415094Slling } 13425094Slling 13435094Slling return (0); 13445094Slling } 13455094Slling 13465094Slling void 13475094Slling zprop_free_list(zprop_list_t *pl) 13485094Slling { 13495094Slling zprop_list_t *next; 13504543Smarks 13515094Slling while (pl != NULL) { 13525094Slling next = pl->pl_next; 13535094Slling free(pl->pl_user_prop); 13545094Slling free(pl); 13555094Slling pl = next; 13565094Slling } 13575094Slling } 13585094Slling 13595094Slling typedef struct expand_data { 13605094Slling zprop_list_t **last; 13615094Slling libzfs_handle_t *hdl; 13625094Slling zfs_type_t type; 13635094Slling } expand_data_t; 13645094Slling 13655094Slling int 13665094Slling zprop_expand_list_cb(int prop, void *cb) 13675094Slling { 13685094Slling zprop_list_t *entry; 13695094Slling expand_data_t *edp = cb; 13705094Slling 13715094Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 13725094Slling return (ZPROP_INVAL); 13735094Slling 13745094Slling entry->pl_prop = prop; 13755094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 13765094Slling entry->pl_all = B_TRUE; 13775094Slling 13785094Slling *(edp->last) = entry; 13795094Slling edp->last = &entry->pl_next; 13805094Slling 13815094Slling return (ZPROP_CONT); 13824543Smarks } 13835094Slling 13845094Slling int 13855094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 13865094Slling { 13875094Slling zprop_list_t *entry; 13885094Slling zprop_list_t **last; 13895094Slling expand_data_t exp; 13905094Slling 13915094Slling if (*plp == NULL) { 13925094Slling /* 13935094Slling * If this is the very first time we've been called for an 'all' 13945094Slling * specification, expand the list to include all native 13955094Slling * properties. 13965094Slling */ 13975094Slling last = plp; 13985094Slling 13995094Slling exp.last = last; 14005094Slling exp.hdl = hdl; 14015094Slling exp.type = type; 14025094Slling 14035094Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 14045094Slling B_FALSE, type) == ZPROP_INVAL) 14055094Slling return (-1); 14065094Slling 14075094Slling /* 14085094Slling * Add 'name' to the beginning of the list, which is handled 14095094Slling * specially. 14105094Slling */ 14115094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 14125094Slling return (-1); 14135094Slling 14145094Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 14155094Slling ZFS_PROP_NAME; 14165094Slling entry->pl_width = zprop_width(entry->pl_prop, 14175094Slling &entry->pl_fixed, type); 14185094Slling entry->pl_all = B_TRUE; 14195094Slling entry->pl_next = *plp; 14205094Slling *plp = entry; 14215094Slling } 14225094Slling return (0); 14235094Slling } 14245094Slling 14255094Slling int 14265094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 14275094Slling zfs_type_t type) 14285094Slling { 14295094Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 14305094Slling } 1431