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 /* 225807Sck153898 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens /* 29789Sahrens * Internal utility routines for the ZFS library. 30789Sahrens */ 31789Sahrens 32789Sahrens #include <errno.h> 33789Sahrens #include <fcntl.h> 34789Sahrens #include <libintl.h> 35789Sahrens #include <stdarg.h> 36789Sahrens #include <stdio.h> 37789Sahrens #include <stdlib.h> 38789Sahrens #include <strings.h> 39789Sahrens #include <unistd.h> 405094Slling #include <ctype.h> 415094Slling #include <math.h> 42789Sahrens #include <sys/mnttab.h> 433635Sck153898 #include <sys/mntent.h> 443635Sck153898 #include <sys/types.h> 45789Sahrens 46789Sahrens #include <libzfs.h> 47789Sahrens 48789Sahrens #include "libzfs_impl.h" 494787Sahrens #include "zfs_prop.h" 50789Sahrens 512082Seschrock int 522082Seschrock libzfs_errno(libzfs_handle_t *hdl) 532082Seschrock { 542082Seschrock return (hdl->libzfs_error); 552082Seschrock } 56789Sahrens 572082Seschrock const char * 582082Seschrock libzfs_error_action(libzfs_handle_t *hdl) 592082Seschrock { 602082Seschrock return (hdl->libzfs_action); 612082Seschrock } 622082Seschrock 632082Seschrock const char * 642082Seschrock libzfs_error_description(libzfs_handle_t *hdl) 652082Seschrock { 662082Seschrock if (hdl->libzfs_desc[0] != '\0') 672082Seschrock return (hdl->libzfs_desc); 68789Sahrens 692082Seschrock switch (hdl->libzfs_error) { 702082Seschrock case EZFS_NOMEM: 712082Seschrock return (dgettext(TEXT_DOMAIN, "out of memory")); 722082Seschrock case EZFS_BADPROP: 732082Seschrock return (dgettext(TEXT_DOMAIN, "invalid property value")); 742082Seschrock case EZFS_PROPREADONLY: 752082Seschrock return (dgettext(TEXT_DOMAIN, "read only property")); 762082Seschrock case EZFS_PROPTYPE: 772082Seschrock return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 782082Seschrock "datasets of this type")); 792082Seschrock case EZFS_PROPNONINHERIT: 802082Seschrock return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 812082Seschrock case EZFS_PROPSPACE: 822082Seschrock return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 832082Seschrock case EZFS_BADTYPE: 842082Seschrock return (dgettext(TEXT_DOMAIN, "operation not applicable to " 852082Seschrock "datasets of this type")); 862082Seschrock case EZFS_BUSY: 872082Seschrock return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 882082Seschrock case EZFS_EXISTS: 892082Seschrock return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 902082Seschrock case EZFS_NOENT: 912082Seschrock return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 922082Seschrock case EZFS_BADSTREAM: 932082Seschrock return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 942082Seschrock case EZFS_DSREADONLY: 952082Seschrock return (dgettext(TEXT_DOMAIN, "dataset is read only")); 962082Seschrock case EZFS_VOLTOOBIG: 972082Seschrock return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 982082Seschrock "this system")); 992082Seschrock case EZFS_VOLHASDATA: 1002082Seschrock return (dgettext(TEXT_DOMAIN, "volume has data")); 1012082Seschrock case EZFS_INVALIDNAME: 1022082Seschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 1032082Seschrock case EZFS_BADRESTORE: 1042082Seschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 1052082Seschrock "destination")); 1062082Seschrock case EZFS_BADBACKUP: 1072082Seschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 1082082Seschrock case EZFS_BADTARGET: 1092082Seschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 1102082Seschrock case EZFS_NODEVICE: 1112082Seschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 1122082Seschrock case EZFS_BADDEV: 1132082Seschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 1142082Seschrock case EZFS_NOREPLICAS: 1152082Seschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 1162082Seschrock case EZFS_RESILVERING: 1172082Seschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 1182082Seschrock case EZFS_BADVERSION: 1192082Seschrock return (dgettext(TEXT_DOMAIN, "unsupported version")); 1202082Seschrock case EZFS_POOLUNAVAIL: 1212082Seschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 1222082Seschrock case EZFS_DEVOVERFLOW: 1232082Seschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 1242082Seschrock case EZFS_BADPATH: 1252082Seschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 1262082Seschrock case EZFS_CROSSTARGET: 1272082Seschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 1282082Seschrock "pools")); 1292082Seschrock case EZFS_ZONED: 1302082Seschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 1312082Seschrock case EZFS_MOUNTFAILED: 1322082Seschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 1332082Seschrock case EZFS_UMOUNTFAILED: 1342082Seschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 1353126Sahl case EZFS_UNSHARENFSFAILED: 1362082Seschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 1373126Sahl case EZFS_SHARENFSFAILED: 1382082Seschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 1395331Samw case EZFS_UNSHARESMBFAILED: 1405331Samw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 1415331Samw case EZFS_SHARESMBFAILED: 1425331Samw return (dgettext(TEXT_DOMAIN, "smb add share failed")); 1434543Smarks case EZFS_ISCSISVCUNAVAIL: 1444543Smarks return (dgettext(TEXT_DOMAIN, 1454543Smarks "iscsitgt service need to be enabled by " 1464543Smarks "a privileged user")); 1472082Seschrock case EZFS_DEVLINKS: 1482082Seschrock return (dgettext(TEXT_DOMAIN, "failed to create /dev links")); 1492082Seschrock case EZFS_PERM: 1502082Seschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 1512082Seschrock case EZFS_NOSPC: 1522082Seschrock return (dgettext(TEXT_DOMAIN, "out of space")); 1532082Seschrock case EZFS_IO: 1542082Seschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 1552082Seschrock case EZFS_INTR: 1562082Seschrock return (dgettext(TEXT_DOMAIN, "signal received")); 1572082Seschrock case EZFS_ISSPARE: 1582082Seschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 1592082Seschrock "spare")); 1602082Seschrock case EZFS_INVALCONFIG: 1612082Seschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1622474Seschrock case EZFS_RECURSIVE: 1632474Seschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 1642926Sek110237 case EZFS_NOHISTORY: 1652926Sek110237 return (dgettext(TEXT_DOMAIN, "no history available")); 1663126Sahl case EZFS_UNSHAREISCSIFAILED: 1673126Sahl return (dgettext(TEXT_DOMAIN, 1683126Sahl "iscsitgtd failed request to unshare")); 1693126Sahl case EZFS_SHAREISCSIFAILED: 1703126Sahl return (dgettext(TEXT_DOMAIN, 1713126Sahl "iscsitgtd failed request to share")); 1723912Slling case EZFS_POOLPROPS: 1733912Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 1743912Slling "pool properties")); 1753912Slling case EZFS_POOL_NOTSUP: 1763912Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 1773912Slling "on this type of pool")); 1783912Slling case EZFS_POOL_INVALARG: 1793912Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 1803912Slling "this pool operation")); 1813978Smmusante case EZFS_NAMETOOLONG: 1823978Smmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 1834276Staylor case EZFS_OPENFAILED: 1844276Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 1854276Staylor case EZFS_NOCAP: 1864276Staylor return (dgettext(TEXT_DOMAIN, 1874276Staylor "disk capacity information could not be retrieved")); 1884276Staylor case EZFS_LABELFAILED: 1894276Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 1904543Smarks case EZFS_BADWHO: 1914543Smarks return (dgettext(TEXT_DOMAIN, "invalid user/group")); 1924543Smarks case EZFS_BADPERM: 1934543Smarks return (dgettext(TEXT_DOMAIN, "invalid permission")); 1944543Smarks case EZFS_BADPERMSET: 1954543Smarks return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 1964543Smarks case EZFS_NODELEGATION: 1974543Smarks return (dgettext(TEXT_DOMAIN, "delegated administration is " 1984543Smarks "disabled on pool")); 1994543Smarks case EZFS_PERMRDONLY: 2004543Smarks return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be" 2014543Smarks " modified")); 2025363Seschrock case EZFS_BADCACHE: 2035363Seschrock return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 2045450Sbrendan case EZFS_ISL2CACHE: 2055450Sbrendan return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 206*6423Sgw25295 case EZFS_VDEVNOTSUP: 207*6423Sgw25295 return (dgettext(TEXT_DOMAIN, "vdev specification is not " 208*6423Sgw25295 "supported")); 2092082Seschrock case EZFS_UNKNOWN: 2102082Seschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 2112082Seschrock default: 2122500Seschrock assert(hdl->libzfs_error == 0); 2132500Seschrock return (dgettext(TEXT_DOMAIN, "no error")); 2142082Seschrock } 2152082Seschrock } 2162082Seschrock 2172082Seschrock /*PRINTFLIKE2*/ 218789Sahrens void 2192082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 220789Sahrens { 221789Sahrens va_list ap; 222789Sahrens 223789Sahrens va_start(ap, fmt); 224789Sahrens 2252082Seschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 2262082Seschrock fmt, ap); 2272082Seschrock hdl->libzfs_desc_active = 1; 228789Sahrens 229789Sahrens va_end(ap); 230789Sahrens } 231789Sahrens 2322082Seschrock static void 2332082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 2342082Seschrock { 2352082Seschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 2362082Seschrock fmt, ap); 2372082Seschrock hdl->libzfs_error = error; 2382082Seschrock 2392082Seschrock if (hdl->libzfs_desc_active) 2402082Seschrock hdl->libzfs_desc_active = 0; 2412082Seschrock else 2422082Seschrock hdl->libzfs_desc[0] = '\0'; 2432082Seschrock 2442082Seschrock if (hdl->libzfs_printerr) { 2452082Seschrock if (error == EZFS_UNKNOWN) { 2462082Seschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 2472082Seschrock "error: %s\n"), libzfs_error_description(hdl)); 2482082Seschrock abort(); 2492082Seschrock } 2502082Seschrock 2512082Seschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 2523912Slling libzfs_error_description(hdl)); 2532082Seschrock if (error == EZFS_NOMEM) 2542082Seschrock exit(1); 2552082Seschrock } 2562082Seschrock } 2572082Seschrock 2583237Slling int 2593237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 2603237Slling { 2613237Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 2623237Slling } 2633237Slling 2642082Seschrock /*PRINTFLIKE3*/ 2652082Seschrock int 2663237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 2672082Seschrock { 2682082Seschrock va_list ap; 2692082Seschrock 2702082Seschrock va_start(ap, fmt); 2712082Seschrock 2722082Seschrock zfs_verror(hdl, error, fmt, ap); 2732082Seschrock 2742082Seschrock va_end(ap); 2752082Seschrock 2762082Seschrock return (-1); 2772082Seschrock } 2782082Seschrock 2792082Seschrock static int 2802082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 2812082Seschrock va_list ap) 2822082Seschrock { 2832082Seschrock switch (error) { 2842082Seschrock case EPERM: 2852082Seschrock case EACCES: 2862082Seschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 2872082Seschrock return (-1); 2882082Seschrock 2894543Smarks case ECANCELED: 2904543Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 2914543Smarks return (-1); 2924543Smarks 2932082Seschrock case EIO: 2942082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 2952082Seschrock return (-1); 2962082Seschrock 2972082Seschrock case EINTR: 2982082Seschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 2992082Seschrock return (-1); 3002082Seschrock } 3012082Seschrock 3022082Seschrock return (0); 3032082Seschrock } 3042082Seschrock 3053237Slling int 3063237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3073237Slling { 3083237Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 3093237Slling } 3103237Slling 3112082Seschrock /*PRINTFLIKE3*/ 3122082Seschrock int 3133237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 314789Sahrens { 315789Sahrens va_list ap; 316789Sahrens 317789Sahrens va_start(ap, fmt); 318789Sahrens 3192082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3202082Seschrock va_end(ap); 3212082Seschrock return (-1); 3222082Seschrock } 3232082Seschrock 3242082Seschrock switch (error) { 3252082Seschrock case ENXIO: 3265807Sck153898 case ENODEV: 3272082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 3282082Seschrock break; 3292082Seschrock 3302082Seschrock case ENOENT: 3312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3322082Seschrock "dataset does not exist")); 3332082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 3342082Seschrock break; 3352082Seschrock 3362082Seschrock case ENOSPC: 3372082Seschrock case EDQUOT: 3382082Seschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 3392082Seschrock return (-1); 3402082Seschrock 3412082Seschrock case EEXIST: 3422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3432082Seschrock "dataset already exists")); 3442082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 3452082Seschrock break; 3462082Seschrock 3472082Seschrock case EBUSY: 3482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3492082Seschrock "dataset is busy")); 3502082Seschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 3512082Seschrock break; 3524543Smarks case EROFS: 3534543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3544543Smarks "snapshot permissions cannot be modified")); 3554543Smarks zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 3564543Smarks break; 3573978Smmusante case ENAMETOOLONG: 3583978Smmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 3593978Smmusante break; 3605860Sck153898 case ENOTSUP: 3615860Sck153898 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 3625860Sck153898 break; 3632082Seschrock default: 3642082Seschrock zfs_error_aux(hdl, strerror(errno)); 3652082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 3662082Seschrock break; 367789Sahrens } 368789Sahrens 369789Sahrens va_end(ap); 3702082Seschrock return (-1); 371789Sahrens } 372789Sahrens 3733237Slling int 3743237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3753237Slling { 3763237Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 3773237Slling } 3783237Slling 3792082Seschrock /*PRINTFLIKE3*/ 3802082Seschrock int 3813237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 382789Sahrens { 3832082Seschrock va_list ap; 3842082Seschrock 3852082Seschrock va_start(ap, fmt); 3862082Seschrock 3872082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3882082Seschrock va_end(ap); 3892082Seschrock return (-1); 3902082Seschrock } 3912082Seschrock 3922082Seschrock switch (error) { 3932082Seschrock case ENODEV: 3942082Seschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 3952082Seschrock break; 3962082Seschrock 3972082Seschrock case ENOENT: 3983912Slling zfs_error_aux(hdl, 3993912Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 4002082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 4012082Seschrock break; 4022082Seschrock 4032082Seschrock case EEXIST: 4042082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4052082Seschrock "pool already exists")); 4062082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 4072082Seschrock break; 4082082Seschrock 4092082Seschrock case EBUSY: 4102082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 4112082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 4122082Seschrock break; 4132082Seschrock 4142082Seschrock case ENXIO: 4152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4162082Seschrock "one or more devices is currently unavailable")); 4172082Seschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 4182082Seschrock break; 4192082Seschrock 4202082Seschrock case ENAMETOOLONG: 4212082Seschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 4222082Seschrock break; 4232082Seschrock 4243912Slling case ENOTSUP: 4253912Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 4263912Slling break; 4273912Slling 4283912Slling case EINVAL: 4293912Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 4303912Slling break; 4313912Slling 4324543Smarks case ENOSPC: 4334543Smarks case EDQUOT: 4344543Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 4354543Smarks return (-1); 4364543Smarks 4372082Seschrock default: 4382082Seschrock zfs_error_aux(hdl, strerror(error)); 4392082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 4402082Seschrock } 4412082Seschrock 4422082Seschrock va_end(ap); 4432082Seschrock return (-1); 444789Sahrens } 445789Sahrens 446789Sahrens /* 447789Sahrens * Display an out of memory error message and abort the current program. 448789Sahrens */ 4492082Seschrock int 4502082Seschrock no_memory(libzfs_handle_t *hdl) 451789Sahrens { 4522082Seschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 453789Sahrens } 454789Sahrens 455789Sahrens /* 456789Sahrens * A safe form of malloc() which will die if the allocation fails. 457789Sahrens */ 458789Sahrens void * 4592082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 460789Sahrens { 461789Sahrens void *data; 462789Sahrens 463789Sahrens if ((data = calloc(1, size)) == NULL) 4642082Seschrock (void) no_memory(hdl); 465789Sahrens 466789Sahrens return (data); 467789Sahrens } 468789Sahrens 469789Sahrens /* 4702676Seschrock * A safe form of realloc(), which also zeroes newly allocated space. 4712676Seschrock */ 4722676Seschrock void * 4732676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 4742676Seschrock { 4752676Seschrock void *ret; 4762676Seschrock 4772676Seschrock if ((ret = realloc(ptr, newsize)) == NULL) { 4782676Seschrock (void) no_memory(hdl); 4792676Seschrock free(ptr); 4802676Seschrock return (NULL); 4812676Seschrock } 4822676Seschrock 4832676Seschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 4842676Seschrock return (ret); 4852676Seschrock } 4862676Seschrock 4872676Seschrock /* 488789Sahrens * A safe form of strdup() which will die if the allocation fails. 489789Sahrens */ 490789Sahrens char * 4912082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 492789Sahrens { 493789Sahrens char *ret; 494789Sahrens 495789Sahrens if ((ret = strdup(str)) == NULL) 4962082Seschrock (void) no_memory(hdl); 497789Sahrens 498789Sahrens return (ret); 499789Sahrens } 500789Sahrens 501789Sahrens /* 502789Sahrens * Convert a number to an appropriately human-readable output. 503789Sahrens */ 504789Sahrens void 505789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 506789Sahrens { 507789Sahrens uint64_t n = num; 508789Sahrens int index = 0; 509789Sahrens char u; 510789Sahrens 511789Sahrens while (n >= 1024) { 5121162Seschrock n /= 1024; 513789Sahrens index++; 514789Sahrens } 515789Sahrens 516789Sahrens u = " KMGTPE"[index]; 517789Sahrens 5181162Seschrock if (index == 0) { 519789Sahrens (void) snprintf(buf, buflen, "%llu", n); 5201162Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5211162Seschrock /* 5221162Seschrock * If this is an even multiple of the base, always display 5231162Seschrock * without any decimal precision. 5241162Seschrock */ 525789Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5261162Seschrock } else { 5271162Seschrock /* 5281162Seschrock * We want to choose a precision that reflects the best choice 5291162Seschrock * for fitting in 5 characters. This can get rather tricky when 5301162Seschrock * we have numbers that are very close to an order of magnitude. 5311162Seschrock * For example, when displaying 10239 (which is really 9.999K), 5321162Seschrock * we want only a single place of precision for 10.0K. We could 5331162Seschrock * develop some complex heuristics for this, but it's much 5341162Seschrock * easier just to try each combination in turn. 5351162Seschrock */ 5361162Seschrock int i; 5371162Seschrock for (i = 2; i >= 0; i--) { 5384451Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5394451Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5401162Seschrock break; 5411162Seschrock } 5421162Seschrock } 543789Sahrens } 5442082Seschrock 5452082Seschrock void 5462082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 5472082Seschrock { 5482082Seschrock hdl->libzfs_printerr = printerr; 5492082Seschrock } 5502082Seschrock 5512082Seschrock libzfs_handle_t * 5522082Seschrock libzfs_init(void) 5532082Seschrock { 5542082Seschrock libzfs_handle_t *hdl; 5552082Seschrock 5562082Seschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 5572082Seschrock return (NULL); 5582082Seschrock } 5592082Seschrock 5602500Seschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 5612082Seschrock free(hdl); 5622082Seschrock return (NULL); 5632082Seschrock } 5642082Seschrock 5652082Seschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 5662082Seschrock (void) close(hdl->libzfs_fd); 5672082Seschrock free(hdl); 5682082Seschrock return (NULL); 5692082Seschrock } 5702082Seschrock 5712082Seschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 5722082Seschrock 5734787Sahrens zfs_prop_init(); 5745094Slling zpool_prop_init(); 5754787Sahrens 5762082Seschrock return (hdl); 5772082Seschrock } 5782082Seschrock 5792082Seschrock void 5802082Seschrock libzfs_fini(libzfs_handle_t *hdl) 5812082Seschrock { 5822082Seschrock (void) close(hdl->libzfs_fd); 5832082Seschrock if (hdl->libzfs_mnttab) 5842082Seschrock (void) fclose(hdl->libzfs_mnttab); 5852082Seschrock if (hdl->libzfs_sharetab) 5862082Seschrock (void) fclose(hdl->libzfs_sharetab); 5874180Sdougm zfs_uninit_libshare(hdl); 5884543Smarks if (hdl->libzfs_log_str) 5894543Smarks (void) free(hdl->libzfs_log_str); 5902082Seschrock namespace_clear(hdl); 5912082Seschrock free(hdl); 5922082Seschrock } 5932082Seschrock 5942082Seschrock libzfs_handle_t * 5952082Seschrock zpool_get_handle(zpool_handle_t *zhp) 5962082Seschrock { 5972082Seschrock return (zhp->zpool_hdl); 5982082Seschrock } 5992082Seschrock 6002082Seschrock libzfs_handle_t * 6012082Seschrock zfs_get_handle(zfs_handle_t *zhp) 6022082Seschrock { 6032082Seschrock return (zhp->zfs_hdl); 6042082Seschrock } 6052676Seschrock 6062676Seschrock /* 6073635Sck153898 * Given a name, determine whether or not it's a valid path 6083635Sck153898 * (starts with '/' or "./"). If so, walk the mnttab trying 6093635Sck153898 * to match the device number. If not, treat the path as an 6103635Sck153898 * fs/vol/snap name. 6113635Sck153898 */ 6123635Sck153898 zfs_handle_t * 6133635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6143635Sck153898 { 6153635Sck153898 struct stat64 statbuf; 6163635Sck153898 struct extmnttab entry; 6173635Sck153898 int ret; 6183635Sck153898 6193635Sck153898 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6203635Sck153898 /* 6213635Sck153898 * It's not a valid path, assume it's a name of type 'argtype'. 6223635Sck153898 */ 6233635Sck153898 return (zfs_open(hdl, path, argtype)); 6243635Sck153898 } 6253635Sck153898 6263635Sck153898 if (stat64(path, &statbuf) != 0) { 6273635Sck153898 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6283635Sck153898 return (NULL); 6293635Sck153898 } 6303635Sck153898 6313635Sck153898 rewind(hdl->libzfs_mnttab); 6323635Sck153898 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6333635Sck153898 if (makedevice(entry.mnt_major, entry.mnt_minor) == 6343635Sck153898 statbuf.st_dev) { 6353635Sck153898 break; 6363635Sck153898 } 6373635Sck153898 } 6383635Sck153898 if (ret != 0) { 6393635Sck153898 return (NULL); 6403635Sck153898 } 6413635Sck153898 6423635Sck153898 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6433635Sck153898 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 6443635Sck153898 path); 6453635Sck153898 return (NULL); 6463635Sck153898 } 6473635Sck153898 6483635Sck153898 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6493635Sck153898 } 6503635Sck153898 6513635Sck153898 /* 6522676Seschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 6532676Seschrock * an ioctl(). 6542676Seschrock */ 6552676Seschrock int 6562676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 6572676Seschrock { 6582676Seschrock if (len == 0) 6592885Sahrens len = 2048; 6602676Seschrock zc->zc_nvlist_dst_size = len; 6612676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6622676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 6632676Seschrock return (-1); 6642676Seschrock 6652676Seschrock return (0); 6662676Seschrock } 6672676Seschrock 6682676Seschrock /* 6692676Seschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 6702676Seschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 6712676Seschrock * filled in by the kernel to indicate the actual required size. 6722676Seschrock */ 6732676Seschrock int 6742676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 6752676Seschrock { 6762676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 6772676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6782676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 6792676Seschrock == NULL) 6802676Seschrock return (-1); 6812676Seschrock 6822676Seschrock return (0); 6832676Seschrock } 6842676Seschrock 6852676Seschrock /* 6862885Sahrens * Called to free the src and dst nvlists stored in the command structure. 6872676Seschrock */ 6882676Seschrock void 6892676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc) 6902676Seschrock { 6915094Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 6922676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 6932676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 6942676Seschrock } 6952676Seschrock 6965094Slling static int 6975094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 6985094Slling nvlist_t *nvl) 6992676Seschrock { 7002676Seschrock char *packed; 7012676Seschrock size_t len; 7022676Seschrock 7032676Seschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 7042676Seschrock 7052676Seschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 7062676Seschrock return (-1); 7072676Seschrock 7082676Seschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 7092676Seschrock 7105094Slling *outnv = (uint64_t)(uintptr_t)packed; 7115094Slling *outlen = len; 7125094Slling 7135094Slling return (0); 7145094Slling } 7152676Seschrock 7165094Slling int 7175094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7185094Slling { 7195094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 7205094Slling &zc->zc_nvlist_conf_size, nvl)); 7215094Slling } 7225094Slling 7235094Slling int 7245094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7255094Slling { 7265094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 7275094Slling &zc->zc_nvlist_src_size, nvl)); 7282676Seschrock } 7292676Seschrock 7302676Seschrock /* 7312676Seschrock * Unpacks an nvlist from the ZFS ioctl command structure. 7322676Seschrock */ 7332676Seschrock int 7342676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 7352676Seschrock { 7362676Seschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 7372676Seschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 7382676Seschrock return (no_memory(hdl)); 7392676Seschrock 7402676Seschrock return (0); 7412676Seschrock } 7423912Slling 7435094Slling int 7445094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 7455094Slling { 7465094Slling int error; 7475094Slling 7485094Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 7495094Slling error = ioctl(hdl->libzfs_fd, request, zc); 7505094Slling if (hdl->libzfs_log_str) { 7515094Slling free(hdl->libzfs_log_str); 7525094Slling hdl->libzfs_log_str = NULL; 7535094Slling } 7545094Slling zc->zc_history = 0; 7555094Slling 7565094Slling return (error); 7575094Slling } 7585094Slling 7595094Slling /* 7605094Slling * ================================================================ 7615094Slling * API shared by zfs and zpool property management 7625094Slling * ================================================================ 7635094Slling */ 7645094Slling 7653912Slling static void 7665094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 7673912Slling { 7685094Slling zprop_list_t *pl = cbp->cb_proplist; 7693912Slling int i; 7703912Slling char *title; 7713912Slling size_t len; 7723912Slling 7733912Slling cbp->cb_first = B_FALSE; 7743912Slling if (cbp->cb_scripted) 7753912Slling return; 7763912Slling 7773912Slling /* 7783912Slling * Start with the length of the column headers. 7793912Slling */ 7803912Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 7813912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 7823912Slling "PROPERTY")); 7833912Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 7843912Slling "VALUE")); 7853912Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 7863912Slling "SOURCE")); 7873912Slling 7883912Slling /* 7893912Slling * Go through and calculate the widths for each column. For the 7903912Slling * 'source' column, we kludge it up by taking the worst-case scenario of 7913912Slling * inheriting from the longest name. This is acceptable because in the 7923912Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 7933912Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 7943912Slling * if the name of the property is much longer the any values we find. 7953912Slling */ 7963912Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 7973912Slling /* 7983912Slling * 'PROPERTY' column 7993912Slling */ 8005094Slling if (pl->pl_prop != ZPROP_INVAL) { 8015094Slling const char *propname = (type == ZFS_TYPE_POOL) ? 8025094Slling zpool_prop_to_name(pl->pl_prop) : 8035094Slling zfs_prop_to_name(pl->pl_prop); 8045094Slling 8055094Slling len = strlen(propname); 8063912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8073912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8083912Slling } else { 8093912Slling len = strlen(pl->pl_user_prop); 8103912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8113912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8123912Slling } 8133912Slling 8143912Slling /* 8153912Slling * 'VALUE' column 8163912Slling */ 8173912Slling if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) && 8183912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 8193912Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 8203912Slling 8213912Slling /* 8223912Slling * 'NAME' and 'SOURCE' columns 8233912Slling */ 8245094Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 8255094Slling ZFS_PROP_NAME) && 8263912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 8273912Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 8283912Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 8293912Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 8303912Slling } 8313912Slling } 8323912Slling 8333912Slling /* 8343912Slling * Now go through and print the headers. 8353912Slling */ 8363912Slling for (i = 0; i < 4; i++) { 8373912Slling switch (cbp->cb_columns[i]) { 8383912Slling case GET_COL_NAME: 8393912Slling title = dgettext(TEXT_DOMAIN, "NAME"); 8403912Slling break; 8413912Slling case GET_COL_PROPERTY: 8423912Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 8433912Slling break; 8443912Slling case GET_COL_VALUE: 8453912Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 8463912Slling break; 8473912Slling case GET_COL_SOURCE: 8483912Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 8493912Slling break; 8503912Slling default: 8513912Slling title = NULL; 8523912Slling } 8533912Slling 8543912Slling if (title != NULL) { 8553912Slling if (i == 3 || cbp->cb_columns[i + 1] == 0) 8563912Slling (void) printf("%s", title); 8573912Slling else 8583912Slling (void) printf("%-*s ", 8593912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 8603912Slling title); 8613912Slling } 8623912Slling } 8633912Slling (void) printf("\n"); 8643912Slling } 8653912Slling 8663912Slling /* 8673912Slling * Display a single line of output, according to the settings in the callback 8683912Slling * structure. 8693912Slling */ 8703912Slling void 8715094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 8725094Slling const char *propname, const char *value, zprop_source_t sourcetype, 8733912Slling const char *source) 8743912Slling { 8753912Slling int i; 8763912Slling const char *str; 8773912Slling char buf[128]; 8783912Slling 8793912Slling /* 8803912Slling * Ignore those source types that the user has chosen to ignore. 8813912Slling */ 8823912Slling if ((sourcetype & cbp->cb_sources) == 0) 8833912Slling return; 8843912Slling 8853912Slling if (cbp->cb_first) 8865094Slling zprop_print_headers(cbp, cbp->cb_type); 8873912Slling 8883912Slling for (i = 0; i < 4; i++) { 8893912Slling switch (cbp->cb_columns[i]) { 8903912Slling case GET_COL_NAME: 8913912Slling str = name; 8923912Slling break; 8933912Slling 8943912Slling case GET_COL_PROPERTY: 8953912Slling str = propname; 8963912Slling break; 8973912Slling 8983912Slling case GET_COL_VALUE: 8993912Slling str = value; 9003912Slling break; 9013912Slling 9023912Slling case GET_COL_SOURCE: 9033912Slling switch (sourcetype) { 9045094Slling case ZPROP_SRC_NONE: 9053912Slling str = "-"; 9063912Slling break; 9073912Slling 9085094Slling case ZPROP_SRC_DEFAULT: 9093912Slling str = "default"; 9103912Slling break; 9113912Slling 9125094Slling case ZPROP_SRC_LOCAL: 9133912Slling str = "local"; 9143912Slling break; 9153912Slling 9165094Slling case ZPROP_SRC_TEMPORARY: 9173912Slling str = "temporary"; 9183912Slling break; 9193912Slling 9205094Slling case ZPROP_SRC_INHERITED: 9213912Slling (void) snprintf(buf, sizeof (buf), 9223912Slling "inherited from %s", source); 9233912Slling str = buf; 9243912Slling break; 9253912Slling } 9263912Slling break; 9273912Slling 9283912Slling default: 9293912Slling continue; 9303912Slling } 9313912Slling 9323912Slling if (cbp->cb_columns[i + 1] == 0) 9333912Slling (void) printf("%s", str); 9343912Slling else if (cbp->cb_scripted) 9353912Slling (void) printf("%s\t", str); 9363912Slling else 9373912Slling (void) printf("%-*s ", 9383912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 9393912Slling str); 9403912Slling 9413912Slling } 9423912Slling 9433912Slling (void) printf("\n"); 9443912Slling } 9454543Smarks 9465094Slling /* 9475094Slling * Given a numeric suffix, convert the value into a number of bits that the 9485094Slling * resulting value must be shifted. 9495094Slling */ 9505094Slling static int 9515094Slling str2shift(libzfs_handle_t *hdl, const char *buf) 9525094Slling { 9535094Slling const char *ends = "BKMGTPEZ"; 9545094Slling int i; 9555094Slling 9565094Slling if (buf[0] == '\0') 9575094Slling return (0); 9585094Slling for (i = 0; i < strlen(ends); i++) { 9595094Slling if (toupper(buf[0]) == ends[i]) 9605094Slling break; 9615094Slling } 9625094Slling if (i == strlen(ends)) { 9635094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9645094Slling "invalid numeric suffix '%s'"), buf); 9655094Slling return (-1); 9665094Slling } 9675094Slling 9685094Slling /* 9695094Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 9705094Slling * allow 'BB' - that's just weird. 9715094Slling */ 9725094Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 9735094Slling toupper(buf[0]) != 'B')) 9745094Slling return (10*i); 9755094Slling 9765094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9775094Slling "invalid numeric suffix '%s'"), buf); 9785094Slling return (-1); 9795094Slling } 9805094Slling 9815094Slling /* 9825094Slling * Convert a string of the form '100G' into a real number. Used when setting 9835094Slling * properties or creating a volume. 'buf' is used to place an extended error 9845094Slling * message for the caller to use. 9855094Slling */ 9864543Smarks int 9875094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 9885094Slling { 9895094Slling char *end; 9905094Slling int shift; 9915094Slling 9925094Slling *num = 0; 9935094Slling 9945094Slling /* Check to see if this looks like a number. */ 9955094Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 9965094Slling if (hdl) 9975094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9985094Slling "bad numeric value '%s'"), value); 9995094Slling return (-1); 10005094Slling } 10015094Slling 10025094Slling /* Rely on stroll() to process the numeric portion. */ 10035094Slling errno = 0; 10045094Slling *num = strtoll(value, &end, 10); 10055094Slling 10065094Slling /* 10075094Slling * Check for ERANGE, which indicates that the value is too large to fit 10085094Slling * in a 64-bit value. 10095094Slling */ 10105094Slling if (errno == ERANGE) { 10115094Slling if (hdl) 10125094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10135094Slling "numeric value is too large")); 10145094Slling return (-1); 10155094Slling } 10165094Slling 10175094Slling /* 10185094Slling * If we have a decimal value, then do the computation with floating 10195094Slling * point arithmetic. Otherwise, use standard arithmetic. 10205094Slling */ 10215094Slling if (*end == '.') { 10225094Slling double fval = strtod(value, &end); 10235094Slling 10245094Slling if ((shift = str2shift(hdl, end)) == -1) 10255094Slling return (-1); 10265094Slling 10275094Slling fval *= pow(2, shift); 10285094Slling 10295094Slling if (fval > UINT64_MAX) { 10305094Slling if (hdl) 10315094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10325094Slling "numeric value is too large")); 10335094Slling return (-1); 10345094Slling } 10355094Slling 10365094Slling *num = (uint64_t)fval; 10375094Slling } else { 10385094Slling if ((shift = str2shift(hdl, end)) == -1) 10395094Slling return (-1); 10405094Slling 10415094Slling /* Check for overflow */ 10425094Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 10435094Slling if (hdl) 10445094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10455094Slling "numeric value is too large")); 10465094Slling return (-1); 10475094Slling } 10485094Slling 10495094Slling *num <<= shift; 10505094Slling } 10515094Slling 10525094Slling return (0); 10535094Slling } 10545094Slling 10555094Slling /* 10565094Slling * Given a propname=value nvpair to set, parse any numeric properties 10575094Slling * (index, boolean, etc) if they are specified as strings and add the 10585094Slling * resulting nvpair to the returned nvlist. 10595094Slling * 10605094Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 10615094Slling * We want the user to be able to ignore this fact and specify properties 10625094Slling * as native values (numbers, for example) or as strings (to simplify 10635094Slling * command line utilities). This also handles converting index types 10645094Slling * (compression, checksum, etc) from strings to their on-disk index. 10655094Slling */ 10665094Slling int 10675094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 10685094Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 10695094Slling const char *errbuf) 10704543Smarks { 10715094Slling data_type_t datatype = nvpair_type(elem); 10725094Slling zprop_type_t proptype; 10735094Slling const char *propname; 10745094Slling char *value; 10755094Slling boolean_t isnone = B_FALSE; 10765094Slling 10775094Slling if (type == ZFS_TYPE_POOL) { 10785094Slling proptype = zpool_prop_get_type(prop); 10795094Slling propname = zpool_prop_to_name(prop); 10805094Slling } else { 10815094Slling proptype = zfs_prop_get_type(prop); 10825094Slling propname = zfs_prop_to_name(prop); 10835094Slling } 10845094Slling 10855094Slling /* 10865094Slling * Convert any properties to the internal DSL value types. 10875094Slling */ 10885094Slling *svalp = NULL; 10895094Slling *ivalp = 0; 10905094Slling 10915094Slling switch (proptype) { 10925094Slling case PROP_TYPE_STRING: 10935094Slling if (datatype != DATA_TYPE_STRING) { 10945094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10955094Slling "'%s' must be a string"), nvpair_name(elem)); 10965094Slling goto error; 10975094Slling } 10985094Slling (void) nvpair_value_string(elem, svalp); 10995094Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 11005094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11015094Slling "'%s' is too long"), nvpair_name(elem)); 11025094Slling goto error; 11035094Slling } 11045094Slling break; 11055094Slling 11065094Slling case PROP_TYPE_NUMBER: 11075094Slling if (datatype == DATA_TYPE_STRING) { 11085094Slling (void) nvpair_value_string(elem, &value); 11095094Slling if (strcmp(value, "none") == 0) { 11105094Slling isnone = B_TRUE; 11115094Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 11125094Slling != 0) { 11135094Slling goto error; 11145094Slling } 11155094Slling } else if (datatype == DATA_TYPE_UINT64) { 11165094Slling (void) nvpair_value_uint64(elem, ivalp); 11175094Slling } else { 11185094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11195094Slling "'%s' must be a number"), nvpair_name(elem)); 11205094Slling goto error; 11215094Slling } 11225094Slling 11235094Slling /* 11245094Slling * Quota special: force 'none' and don't allow 0. 11255094Slling */ 11265378Sck153898 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 11275378Sck153898 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 11285094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11295378Sck153898 "use 'none' to disable quota/refquota")); 11305094Slling goto error; 11315094Slling } 11325094Slling break; 11335094Slling 11345094Slling case PROP_TYPE_INDEX: 11355378Sck153898 if (datatype != DATA_TYPE_STRING) { 11365094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11375094Slling "'%s' must be a string"), nvpair_name(elem)); 11385094Slling goto error; 11395094Slling } 11405094Slling 11415378Sck153898 (void) nvpair_value_string(elem, &value); 11425378Sck153898 11435094Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 11445094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11455094Slling "'%s' must be one of '%s'"), propname, 11465094Slling zprop_values(prop, type)); 11475094Slling goto error; 11485094Slling } 11495094Slling break; 11505094Slling 11515094Slling default: 11525094Slling abort(); 11535094Slling } 11544543Smarks 11555094Slling /* 11565094Slling * Add the result to our return set of properties. 11575094Slling */ 11585094Slling if (*svalp != NULL) { 11595094Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 11605094Slling (void) no_memory(hdl); 11615094Slling return (-1); 11625094Slling } 11635094Slling } else { 11645094Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 11655094Slling (void) no_memory(hdl); 11665094Slling return (-1); 11675094Slling } 11685094Slling } 11695094Slling 11705094Slling return (0); 11715094Slling error: 11725094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11735094Slling return (-1); 11745094Slling } 11755094Slling 11765094Slling /* 11775094Slling * Given a comma-separated list of properties, construct a property list 11785094Slling * containing both user-defined and native properties. This function will 11795094Slling * return a NULL list if 'all' is specified, which can later be expanded 11805094Slling * by zprop_expand_list(). 11815094Slling */ 11825094Slling int 11835094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 11845094Slling zfs_type_t type) 11855094Slling { 11865094Slling size_t len; 11875094Slling char *s, *p; 11885094Slling char c; 11895094Slling int prop; 11905094Slling zprop_list_t *entry; 11915094Slling zprop_list_t **last; 11925094Slling 11935094Slling *listp = NULL; 11945094Slling last = listp; 11955094Slling 11965094Slling /* 11975094Slling * If 'all' is specified, return a NULL list. 11985094Slling */ 11995094Slling if (strcmp(props, "all") == 0) 12005094Slling return (0); 12015094Slling 12025094Slling /* 12035094Slling * If no props were specified, return an error. 12045094Slling */ 12055094Slling if (props[0] == '\0') { 12065094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12075094Slling "no properties specified")); 12085094Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 12095094Slling "bad property list"))); 12104543Smarks } 12115094Slling 12125094Slling /* 12135094Slling * It would be nice to use getsubopt() here, but the inclusion of column 12145094Slling * aliases makes this more effort than it's worth. 12155094Slling */ 12165094Slling s = props; 12175094Slling while (*s != '\0') { 12185094Slling if ((p = strchr(s, ',')) == NULL) { 12195094Slling len = strlen(s); 12205094Slling p = s + len; 12215094Slling } else { 12225094Slling len = p - s; 12235094Slling } 12245094Slling 12255094Slling /* 12265094Slling * Check for empty options. 12275094Slling */ 12285094Slling if (len == 0) { 12295094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12305094Slling "empty property name")); 12315094Slling return (zfs_error(hdl, EZFS_BADPROP, 12325094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 12335094Slling } 12345094Slling 12355094Slling /* 12365094Slling * Check all regular property names. 12375094Slling */ 12385094Slling c = s[len]; 12395094Slling s[len] = '\0'; 12405094Slling prop = zprop_name_to_prop(s, type); 12415094Slling 12425094Slling if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 12435094Slling prop = ZPROP_INVAL; 12445094Slling 12455094Slling /* 12465094Slling * When no property table entry can be found, return failure if 12475094Slling * this is a pool property or if this isn't a user-defined 12485094Slling * dataset property, 12495094Slling */ 12505094Slling if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 12515094Slling !zfs_prop_user(s))) { 12525094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12535094Slling "invalid property '%s'"), s); 12545094Slling return (zfs_error(hdl, EZFS_BADPROP, 12555094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 12565094Slling } 12575094Slling 12585094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 12595094Slling return (-1); 12605094Slling 12615094Slling entry->pl_prop = prop; 12625094Slling if (prop == ZPROP_INVAL) { 12635094Slling if ((entry->pl_user_prop = zfs_strdup(hdl, s)) 12645094Slling == NULL) { 12655094Slling free(entry); 12665094Slling return (-1); 12675094Slling } 12685094Slling entry->pl_width = strlen(s); 12695094Slling } else { 12705094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, 12715094Slling type); 12725094Slling } 12735094Slling 12745094Slling *last = entry; 12755094Slling last = &entry->pl_next; 12765094Slling 12775094Slling s = p; 12785094Slling if (c == ',') 12795094Slling s++; 12805094Slling } 12815094Slling 12825094Slling return (0); 12835094Slling } 12845094Slling 12855094Slling void 12865094Slling zprop_free_list(zprop_list_t *pl) 12875094Slling { 12885094Slling zprop_list_t *next; 12894543Smarks 12905094Slling while (pl != NULL) { 12915094Slling next = pl->pl_next; 12925094Slling free(pl->pl_user_prop); 12935094Slling free(pl); 12945094Slling pl = next; 12955094Slling } 12965094Slling } 12975094Slling 12985094Slling typedef struct expand_data { 12995094Slling zprop_list_t **last; 13005094Slling libzfs_handle_t *hdl; 13015094Slling zfs_type_t type; 13025094Slling } expand_data_t; 13035094Slling 13045094Slling int 13055094Slling zprop_expand_list_cb(int prop, void *cb) 13065094Slling { 13075094Slling zprop_list_t *entry; 13085094Slling expand_data_t *edp = cb; 13095094Slling 13105094Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 13115094Slling return (ZPROP_INVAL); 13125094Slling 13135094Slling entry->pl_prop = prop; 13145094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 13155094Slling entry->pl_all = B_TRUE; 13165094Slling 13175094Slling *(edp->last) = entry; 13185094Slling edp->last = &entry->pl_next; 13195094Slling 13205094Slling return (ZPROP_CONT); 13214543Smarks } 13225094Slling 13235094Slling int 13245094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 13255094Slling { 13265094Slling zprop_list_t *entry; 13275094Slling zprop_list_t **last; 13285094Slling expand_data_t exp; 13295094Slling 13305094Slling if (*plp == NULL) { 13315094Slling /* 13325094Slling * If this is the very first time we've been called for an 'all' 13335094Slling * specification, expand the list to include all native 13345094Slling * properties. 13355094Slling */ 13365094Slling last = plp; 13375094Slling 13385094Slling exp.last = last; 13395094Slling exp.hdl = hdl; 13405094Slling exp.type = type; 13415094Slling 13425094Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 13435094Slling B_FALSE, type) == ZPROP_INVAL) 13445094Slling return (-1); 13455094Slling 13465094Slling /* 13475094Slling * Add 'name' to the beginning of the list, which is handled 13485094Slling * specially. 13495094Slling */ 13505094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 13515094Slling return (-1); 13525094Slling 13535094Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 13545094Slling ZFS_PROP_NAME; 13555094Slling entry->pl_width = zprop_width(entry->pl_prop, 13565094Slling &entry->pl_fixed, type); 13575094Slling entry->pl_all = B_TRUE; 13585094Slling entry->pl_next = *plp; 13595094Slling *plp = entry; 13605094Slling } 13615094Slling return (0); 13625094Slling } 13635094Slling 13645094Slling int 13655094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 13665094Slling zfs_type_t type) 13675094Slling { 13685094Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 13695094Slling } 1370