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 /* 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")); 2132082Seschrock case EZFS_UNKNOWN: 2142082Seschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 2152082Seschrock default: 2162500Seschrock assert(hdl->libzfs_error == 0); 2172500Seschrock return (dgettext(TEXT_DOMAIN, "no error")); 2182082Seschrock } 2192082Seschrock } 2202082Seschrock 2212082Seschrock /*PRINTFLIKE2*/ 222789Sahrens void 2232082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 224789Sahrens { 225789Sahrens va_list ap; 226789Sahrens 227789Sahrens va_start(ap, fmt); 228789Sahrens 2292082Seschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 2302082Seschrock fmt, ap); 2312082Seschrock hdl->libzfs_desc_active = 1; 232789Sahrens 233789Sahrens va_end(ap); 234789Sahrens } 235789Sahrens 2362082Seschrock static void 2372082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 2382082Seschrock { 2392082Seschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 2402082Seschrock fmt, ap); 2412082Seschrock hdl->libzfs_error = error; 2422082Seschrock 2432082Seschrock if (hdl->libzfs_desc_active) 2442082Seschrock hdl->libzfs_desc_active = 0; 2452082Seschrock else 2462082Seschrock hdl->libzfs_desc[0] = '\0'; 2472082Seschrock 2482082Seschrock if (hdl->libzfs_printerr) { 2492082Seschrock if (error == EZFS_UNKNOWN) { 2502082Seschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 2512082Seschrock "error: %s\n"), libzfs_error_description(hdl)); 2522082Seschrock abort(); 2532082Seschrock } 2542082Seschrock 2552082Seschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 2563912Slling libzfs_error_description(hdl)); 2572082Seschrock if (error == EZFS_NOMEM) 2582082Seschrock exit(1); 2592082Seschrock } 2602082Seschrock } 2612082Seschrock 2623237Slling int 2633237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 2643237Slling { 2653237Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 2663237Slling } 2673237Slling 2682082Seschrock /*PRINTFLIKE3*/ 2692082Seschrock int 2703237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 2712082Seschrock { 2722082Seschrock va_list ap; 2732082Seschrock 2742082Seschrock va_start(ap, fmt); 2752082Seschrock 2762082Seschrock zfs_verror(hdl, error, fmt, ap); 2772082Seschrock 2782082Seschrock va_end(ap); 2792082Seschrock 2802082Seschrock return (-1); 2812082Seschrock } 2822082Seschrock 2832082Seschrock static int 2842082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 2852082Seschrock va_list ap) 2862082Seschrock { 2872082Seschrock switch (error) { 2882082Seschrock case EPERM: 2892082Seschrock case EACCES: 2902082Seschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 2912082Seschrock return (-1); 2922082Seschrock 2934543Smarks case ECANCELED: 2944543Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 2954543Smarks return (-1); 2964543Smarks 2972082Seschrock case EIO: 2982082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 2992082Seschrock return (-1); 3002082Seschrock 3012082Seschrock case EINTR: 3022082Seschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 3032082Seschrock return (-1); 3042082Seschrock } 3052082Seschrock 3062082Seschrock return (0); 3072082Seschrock } 3082082Seschrock 3093237Slling int 3103237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3113237Slling { 3123237Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 3133237Slling } 3143237Slling 3152082Seschrock /*PRINTFLIKE3*/ 3162082Seschrock int 3173237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 318789Sahrens { 319789Sahrens va_list ap; 320789Sahrens 321789Sahrens va_start(ap, fmt); 322789Sahrens 3232082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3242082Seschrock va_end(ap); 3252082Seschrock return (-1); 3262082Seschrock } 3272082Seschrock 3282082Seschrock switch (error) { 3292082Seschrock case ENXIO: 3305807Sck153898 case ENODEV: 3312082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 3322082Seschrock break; 3332082Seschrock 3342082Seschrock case ENOENT: 3352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3362082Seschrock "dataset does not exist")); 3372082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 3382082Seschrock break; 3392082Seschrock 3402082Seschrock case ENOSPC: 3412082Seschrock case EDQUOT: 3422082Seschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 3432082Seschrock return (-1); 3442082Seschrock 3452082Seschrock case EEXIST: 3462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3472082Seschrock "dataset already exists")); 3482082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 3492082Seschrock break; 3502082Seschrock 3512082Seschrock case EBUSY: 3522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3532082Seschrock "dataset is busy")); 3542082Seschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 3552082Seschrock break; 3564543Smarks case EROFS: 3574543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3584543Smarks "snapshot permissions cannot be modified")); 3594543Smarks zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 3604543Smarks break; 3613978Smmusante case ENAMETOOLONG: 3623978Smmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 3633978Smmusante break; 3645860Sck153898 case ENOTSUP: 3655860Sck153898 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 3665860Sck153898 break; 3672082Seschrock default: 3682082Seschrock zfs_error_aux(hdl, strerror(errno)); 3692082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 3702082Seschrock break; 371789Sahrens } 372789Sahrens 373789Sahrens va_end(ap); 3742082Seschrock return (-1); 375789Sahrens } 376789Sahrens 3773237Slling int 3783237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3793237Slling { 3803237Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 3813237Slling } 3823237Slling 3832082Seschrock /*PRINTFLIKE3*/ 3842082Seschrock int 3853237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 386789Sahrens { 3872082Seschrock va_list ap; 3882082Seschrock 3892082Seschrock va_start(ap, fmt); 3902082Seschrock 3912082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3922082Seschrock va_end(ap); 3932082Seschrock return (-1); 3942082Seschrock } 3952082Seschrock 3962082Seschrock switch (error) { 3972082Seschrock case ENODEV: 3982082Seschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 3992082Seschrock break; 4002082Seschrock 4012082Seschrock case ENOENT: 4023912Slling zfs_error_aux(hdl, 4033912Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 4042082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 4052082Seschrock break; 4062082Seschrock 4072082Seschrock case EEXIST: 4082082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4092082Seschrock "pool already exists")); 4102082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 4112082Seschrock break; 4122082Seschrock 4132082Seschrock case EBUSY: 4142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 415*7341SEric.Schrock@Sun.COM zfs_verror(hdl, EZFS_BUSY, fmt, ap); 4162082Seschrock break; 4172082Seschrock 4182082Seschrock case ENXIO: 4192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4202082Seschrock "one or more devices is currently unavailable")); 4212082Seschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 4222082Seschrock break; 4232082Seschrock 4242082Seschrock case ENAMETOOLONG: 4252082Seschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 4262082Seschrock break; 4272082Seschrock 4283912Slling case ENOTSUP: 4293912Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 4303912Slling break; 4313912Slling 4323912Slling case EINVAL: 4333912Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 4343912Slling break; 4353912Slling 4364543Smarks case ENOSPC: 4374543Smarks case EDQUOT: 4384543Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 4394543Smarks return (-1); 4404543Smarks 4412082Seschrock default: 4422082Seschrock zfs_error_aux(hdl, strerror(error)); 4432082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 4442082Seschrock } 4452082Seschrock 4462082Seschrock va_end(ap); 4472082Seschrock return (-1); 448789Sahrens } 449789Sahrens 450789Sahrens /* 451789Sahrens * Display an out of memory error message and abort the current program. 452789Sahrens */ 4532082Seschrock int 4542082Seschrock no_memory(libzfs_handle_t *hdl) 455789Sahrens { 4562082Seschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 457789Sahrens } 458789Sahrens 459789Sahrens /* 460789Sahrens * A safe form of malloc() which will die if the allocation fails. 461789Sahrens */ 462789Sahrens void * 4632082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 464789Sahrens { 465789Sahrens void *data; 466789Sahrens 467789Sahrens if ((data = calloc(1, size)) == NULL) 4682082Seschrock (void) no_memory(hdl); 469789Sahrens 470789Sahrens return (data); 471789Sahrens } 472789Sahrens 473789Sahrens /* 4742676Seschrock * A safe form of realloc(), which also zeroes newly allocated space. 4752676Seschrock */ 4762676Seschrock void * 4772676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 4782676Seschrock { 4792676Seschrock void *ret; 4802676Seschrock 4812676Seschrock if ((ret = realloc(ptr, newsize)) == NULL) { 4822676Seschrock (void) no_memory(hdl); 4832676Seschrock free(ptr); 4842676Seschrock return (NULL); 4852676Seschrock } 4862676Seschrock 4872676Seschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 4882676Seschrock return (ret); 4892676Seschrock } 4902676Seschrock 4912676Seschrock /* 492789Sahrens * A safe form of strdup() which will die if the allocation fails. 493789Sahrens */ 494789Sahrens char * 4952082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 496789Sahrens { 497789Sahrens char *ret; 498789Sahrens 499789Sahrens if ((ret = strdup(str)) == NULL) 5002082Seschrock (void) no_memory(hdl); 501789Sahrens 502789Sahrens return (ret); 503789Sahrens } 504789Sahrens 505789Sahrens /* 506789Sahrens * Convert a number to an appropriately human-readable output. 507789Sahrens */ 508789Sahrens void 509789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 510789Sahrens { 511789Sahrens uint64_t n = num; 512789Sahrens int index = 0; 513789Sahrens char u; 514789Sahrens 515789Sahrens while (n >= 1024) { 5161162Seschrock n /= 1024; 517789Sahrens index++; 518789Sahrens } 519789Sahrens 520789Sahrens u = " KMGTPE"[index]; 521789Sahrens 5221162Seschrock if (index == 0) { 523789Sahrens (void) snprintf(buf, buflen, "%llu", n); 5241162Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5251162Seschrock /* 5261162Seschrock * If this is an even multiple of the base, always display 5271162Seschrock * without any decimal precision. 5281162Seschrock */ 529789Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5301162Seschrock } else { 5311162Seschrock /* 5321162Seschrock * We want to choose a precision that reflects the best choice 5331162Seschrock * for fitting in 5 characters. This can get rather tricky when 5341162Seschrock * we have numbers that are very close to an order of magnitude. 5351162Seschrock * For example, when displaying 10239 (which is really 9.999K), 5361162Seschrock * we want only a single place of precision for 10.0K. We could 5371162Seschrock * develop some complex heuristics for this, but it's much 5381162Seschrock * easier just to try each combination in turn. 5391162Seschrock */ 5401162Seschrock int i; 5411162Seschrock for (i = 2; i >= 0; i--) { 5424451Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5434451Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5441162Seschrock break; 5451162Seschrock } 5461162Seschrock } 547789Sahrens } 5482082Seschrock 5492082Seschrock void 5502082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 5512082Seschrock { 5522082Seschrock hdl->libzfs_printerr = printerr; 5532082Seschrock } 5542082Seschrock 5552082Seschrock libzfs_handle_t * 5562082Seschrock libzfs_init(void) 5572082Seschrock { 5582082Seschrock libzfs_handle_t *hdl; 5592082Seschrock 5602082Seschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 5612082Seschrock return (NULL); 5622082Seschrock } 5632082Seschrock 5642500Seschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 5652082Seschrock free(hdl); 5662082Seschrock return (NULL); 5672082Seschrock } 5682082Seschrock 5692082Seschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 5702082Seschrock (void) close(hdl->libzfs_fd); 5712082Seschrock free(hdl); 5722082Seschrock return (NULL); 5732082Seschrock } 5742082Seschrock 5752082Seschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 5762082Seschrock 5774787Sahrens zfs_prop_init(); 5785094Slling zpool_prop_init(); 5794787Sahrens 5802082Seschrock return (hdl); 5812082Seschrock } 5822082Seschrock 5832082Seschrock void 5842082Seschrock libzfs_fini(libzfs_handle_t *hdl) 5852082Seschrock { 5862082Seschrock (void) close(hdl->libzfs_fd); 5872082Seschrock if (hdl->libzfs_mnttab) 5882082Seschrock (void) fclose(hdl->libzfs_mnttab); 5892082Seschrock if (hdl->libzfs_sharetab) 5902082Seschrock (void) fclose(hdl->libzfs_sharetab); 5914180Sdougm zfs_uninit_libshare(hdl); 5924543Smarks if (hdl->libzfs_log_str) 5934543Smarks (void) free(hdl->libzfs_log_str); 5946865Srm160521 zpool_free_handles(hdl); 5952082Seschrock namespace_clear(hdl); 5962082Seschrock free(hdl); 5972082Seschrock } 5982082Seschrock 5992082Seschrock libzfs_handle_t * 6002082Seschrock zpool_get_handle(zpool_handle_t *zhp) 6012082Seschrock { 6022082Seschrock return (zhp->zpool_hdl); 6032082Seschrock } 6042082Seschrock 6052082Seschrock libzfs_handle_t * 6062082Seschrock zfs_get_handle(zfs_handle_t *zhp) 6072082Seschrock { 6082082Seschrock return (zhp->zfs_hdl); 6092082Seschrock } 6102676Seschrock 6112676Seschrock /* 6123635Sck153898 * Given a name, determine whether or not it's a valid path 6133635Sck153898 * (starts with '/' or "./"). If so, walk the mnttab trying 6143635Sck153898 * to match the device number. If not, treat the path as an 6153635Sck153898 * fs/vol/snap name. 6163635Sck153898 */ 6173635Sck153898 zfs_handle_t * 6183635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6193635Sck153898 { 6203635Sck153898 struct stat64 statbuf; 6213635Sck153898 struct extmnttab entry; 6223635Sck153898 int ret; 6233635Sck153898 6243635Sck153898 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6253635Sck153898 /* 6263635Sck153898 * It's not a valid path, assume it's a name of type 'argtype'. 6273635Sck153898 */ 6283635Sck153898 return (zfs_open(hdl, path, argtype)); 6293635Sck153898 } 6303635Sck153898 6313635Sck153898 if (stat64(path, &statbuf) != 0) { 6323635Sck153898 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6333635Sck153898 return (NULL); 6343635Sck153898 } 6353635Sck153898 6363635Sck153898 rewind(hdl->libzfs_mnttab); 6373635Sck153898 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6383635Sck153898 if (makedevice(entry.mnt_major, entry.mnt_minor) == 6393635Sck153898 statbuf.st_dev) { 6403635Sck153898 break; 6413635Sck153898 } 6423635Sck153898 } 6433635Sck153898 if (ret != 0) { 6443635Sck153898 return (NULL); 6453635Sck153898 } 6463635Sck153898 6473635Sck153898 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6483635Sck153898 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 6493635Sck153898 path); 6503635Sck153898 return (NULL); 6513635Sck153898 } 6523635Sck153898 6533635Sck153898 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6543635Sck153898 } 6553635Sck153898 6563635Sck153898 /* 6572676Seschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 6582676Seschrock * an ioctl(). 6592676Seschrock */ 6602676Seschrock int 6612676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 6622676Seschrock { 6632676Seschrock if (len == 0) 6642885Sahrens len = 2048; 6652676Seschrock zc->zc_nvlist_dst_size = len; 6662676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6672676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 6682676Seschrock return (-1); 6692676Seschrock 6702676Seschrock return (0); 6712676Seschrock } 6722676Seschrock 6732676Seschrock /* 6742676Seschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 6752676Seschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 6762676Seschrock * filled in by the kernel to indicate the actual required size. 6772676Seschrock */ 6782676Seschrock int 6792676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 6802676Seschrock { 6812676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 6822676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6832676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 6842676Seschrock == NULL) 6852676Seschrock return (-1); 6862676Seschrock 6872676Seschrock return (0); 6882676Seschrock } 6892676Seschrock 6902676Seschrock /* 6912885Sahrens * Called to free the src and dst nvlists stored in the command structure. 6922676Seschrock */ 6932676Seschrock void 6942676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc) 6952676Seschrock { 6965094Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 6972676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 6982676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 6992676Seschrock } 7002676Seschrock 7015094Slling static int 7025094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 7035094Slling nvlist_t *nvl) 7042676Seschrock { 7052676Seschrock char *packed; 7062676Seschrock size_t len; 7072676Seschrock 7082676Seschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 7092676Seschrock 7102676Seschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 7112676Seschrock return (-1); 7122676Seschrock 7132676Seschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 7142676Seschrock 7155094Slling *outnv = (uint64_t)(uintptr_t)packed; 7165094Slling *outlen = len; 7175094Slling 7185094Slling return (0); 7195094Slling } 7202676Seschrock 7215094Slling int 7225094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7235094Slling { 7245094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 7255094Slling &zc->zc_nvlist_conf_size, nvl)); 7265094Slling } 7275094Slling 7285094Slling int 7295094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7305094Slling { 7315094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 7325094Slling &zc->zc_nvlist_src_size, nvl)); 7332676Seschrock } 7342676Seschrock 7352676Seschrock /* 7362676Seschrock * Unpacks an nvlist from the ZFS ioctl command structure. 7372676Seschrock */ 7382676Seschrock int 7392676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 7402676Seschrock { 7412676Seschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 7422676Seschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 7432676Seschrock return (no_memory(hdl)); 7442676Seschrock 7452676Seschrock return (0); 7462676Seschrock } 7473912Slling 7485094Slling int 7495094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 7505094Slling { 7515094Slling int error; 7525094Slling 7535094Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 7545094Slling error = ioctl(hdl->libzfs_fd, request, zc); 7555094Slling if (hdl->libzfs_log_str) { 7565094Slling free(hdl->libzfs_log_str); 7575094Slling hdl->libzfs_log_str = NULL; 7585094Slling } 7595094Slling zc->zc_history = 0; 7605094Slling 7615094Slling return (error); 7625094Slling } 7635094Slling 7645094Slling /* 7655094Slling * ================================================================ 7665094Slling * API shared by zfs and zpool property management 7675094Slling * ================================================================ 7685094Slling */ 7695094Slling 7703912Slling static void 7715094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 7723912Slling { 7735094Slling zprop_list_t *pl = cbp->cb_proplist; 7743912Slling int i; 7753912Slling char *title; 7763912Slling size_t len; 7773912Slling 7783912Slling cbp->cb_first = B_FALSE; 7793912Slling if (cbp->cb_scripted) 7803912Slling return; 7813912Slling 7823912Slling /* 7833912Slling * Start with the length of the column headers. 7843912Slling */ 7853912Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 7863912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 7873912Slling "PROPERTY")); 7883912Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 7893912Slling "VALUE")); 7903912Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 7913912Slling "SOURCE")); 7923912Slling 7933912Slling /* 7943912Slling * Go through and calculate the widths for each column. For the 7953912Slling * 'source' column, we kludge it up by taking the worst-case scenario of 7963912Slling * inheriting from the longest name. This is acceptable because in the 7973912Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 7983912Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 7993912Slling * if the name of the property is much longer the any values we find. 8003912Slling */ 8013912Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 8023912Slling /* 8033912Slling * 'PROPERTY' column 8043912Slling */ 8055094Slling if (pl->pl_prop != ZPROP_INVAL) { 8065094Slling const char *propname = (type == ZFS_TYPE_POOL) ? 8075094Slling zpool_prop_to_name(pl->pl_prop) : 8085094Slling zfs_prop_to_name(pl->pl_prop); 8095094Slling 8105094Slling len = strlen(propname); 8113912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8123912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8133912Slling } else { 8143912Slling len = strlen(pl->pl_user_prop); 8153912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8163912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8173912Slling } 8183912Slling 8193912Slling /* 8203912Slling * 'VALUE' column 8213912Slling */ 8223912Slling if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) && 8233912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 8243912Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 8253912Slling 8263912Slling /* 8273912Slling * 'NAME' and 'SOURCE' columns 8283912Slling */ 8295094Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 8305094Slling ZFS_PROP_NAME) && 8313912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 8323912Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 8333912Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 8343912Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 8353912Slling } 8363912Slling } 8373912Slling 8383912Slling /* 8393912Slling * Now go through and print the headers. 8403912Slling */ 8413912Slling for (i = 0; i < 4; i++) { 8423912Slling switch (cbp->cb_columns[i]) { 8433912Slling case GET_COL_NAME: 8443912Slling title = dgettext(TEXT_DOMAIN, "NAME"); 8453912Slling break; 8463912Slling case GET_COL_PROPERTY: 8473912Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 8483912Slling break; 8493912Slling case GET_COL_VALUE: 8503912Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 8513912Slling break; 8523912Slling case GET_COL_SOURCE: 8533912Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 8543912Slling break; 8553912Slling default: 8563912Slling title = NULL; 8573912Slling } 8583912Slling 8593912Slling if (title != NULL) { 8603912Slling if (i == 3 || cbp->cb_columns[i + 1] == 0) 8613912Slling (void) printf("%s", title); 8623912Slling else 8633912Slling (void) printf("%-*s ", 8643912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 8653912Slling title); 8663912Slling } 8673912Slling } 8683912Slling (void) printf("\n"); 8693912Slling } 8703912Slling 8713912Slling /* 8723912Slling * Display a single line of output, according to the settings in the callback 8733912Slling * structure. 8743912Slling */ 8753912Slling void 8765094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 8775094Slling const char *propname, const char *value, zprop_source_t sourcetype, 8783912Slling const char *source) 8793912Slling { 8803912Slling int i; 8813912Slling const char *str; 8823912Slling char buf[128]; 8833912Slling 8843912Slling /* 8853912Slling * Ignore those source types that the user has chosen to ignore. 8863912Slling */ 8873912Slling if ((sourcetype & cbp->cb_sources) == 0) 8883912Slling return; 8893912Slling 8903912Slling if (cbp->cb_first) 8915094Slling zprop_print_headers(cbp, cbp->cb_type); 8923912Slling 8933912Slling for (i = 0; i < 4; i++) { 8943912Slling switch (cbp->cb_columns[i]) { 8953912Slling case GET_COL_NAME: 8963912Slling str = name; 8973912Slling break; 8983912Slling 8993912Slling case GET_COL_PROPERTY: 9003912Slling str = propname; 9013912Slling break; 9023912Slling 9033912Slling case GET_COL_VALUE: 9043912Slling str = value; 9053912Slling break; 9063912Slling 9073912Slling case GET_COL_SOURCE: 9083912Slling switch (sourcetype) { 9095094Slling case ZPROP_SRC_NONE: 9103912Slling str = "-"; 9113912Slling break; 9123912Slling 9135094Slling case ZPROP_SRC_DEFAULT: 9143912Slling str = "default"; 9153912Slling break; 9163912Slling 9175094Slling case ZPROP_SRC_LOCAL: 9183912Slling str = "local"; 9193912Slling break; 9203912Slling 9215094Slling case ZPROP_SRC_TEMPORARY: 9223912Slling str = "temporary"; 9233912Slling break; 9243912Slling 9255094Slling case ZPROP_SRC_INHERITED: 9263912Slling (void) snprintf(buf, sizeof (buf), 9273912Slling "inherited from %s", source); 9283912Slling str = buf; 9293912Slling break; 9303912Slling } 9313912Slling break; 9323912Slling 9333912Slling default: 9343912Slling continue; 9353912Slling } 9363912Slling 9373912Slling if (cbp->cb_columns[i + 1] == 0) 9383912Slling (void) printf("%s", str); 9393912Slling else if (cbp->cb_scripted) 9403912Slling (void) printf("%s\t", str); 9413912Slling else 9423912Slling (void) printf("%-*s ", 9433912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 9443912Slling str); 9453912Slling 9463912Slling } 9473912Slling 9483912Slling (void) printf("\n"); 9493912Slling } 9504543Smarks 9515094Slling /* 9525094Slling * Given a numeric suffix, convert the value into a number of bits that the 9535094Slling * resulting value must be shifted. 9545094Slling */ 9555094Slling static int 9565094Slling str2shift(libzfs_handle_t *hdl, const char *buf) 9575094Slling { 9585094Slling const char *ends = "BKMGTPEZ"; 9595094Slling int i; 9605094Slling 9615094Slling if (buf[0] == '\0') 9625094Slling return (0); 9635094Slling for (i = 0; i < strlen(ends); i++) { 9645094Slling if (toupper(buf[0]) == ends[i]) 9655094Slling break; 9665094Slling } 9675094Slling if (i == strlen(ends)) { 9685094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9695094Slling "invalid numeric suffix '%s'"), buf); 9705094Slling return (-1); 9715094Slling } 9725094Slling 9735094Slling /* 9745094Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 9755094Slling * allow 'BB' - that's just weird. 9765094Slling */ 9775094Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 9785094Slling toupper(buf[0]) != 'B')) 9795094Slling return (10*i); 9805094Slling 9815094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9825094Slling "invalid numeric suffix '%s'"), buf); 9835094Slling return (-1); 9845094Slling } 9855094Slling 9865094Slling /* 9875094Slling * Convert a string of the form '100G' into a real number. Used when setting 9885094Slling * properties or creating a volume. 'buf' is used to place an extended error 9895094Slling * message for the caller to use. 9905094Slling */ 9914543Smarks int 9925094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 9935094Slling { 9945094Slling char *end; 9955094Slling int shift; 9965094Slling 9975094Slling *num = 0; 9985094Slling 9995094Slling /* Check to see if this looks like a number. */ 10005094Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 10015094Slling if (hdl) 10025094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10035094Slling "bad numeric value '%s'"), value); 10045094Slling return (-1); 10055094Slling } 10065094Slling 10075094Slling /* Rely on stroll() to process the numeric portion. */ 10085094Slling errno = 0; 10095094Slling *num = strtoll(value, &end, 10); 10105094Slling 10115094Slling /* 10125094Slling * Check for ERANGE, which indicates that the value is too large to fit 10135094Slling * in a 64-bit value. 10145094Slling */ 10155094Slling if (errno == ERANGE) { 10165094Slling if (hdl) 10175094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10185094Slling "numeric value is too large")); 10195094Slling return (-1); 10205094Slling } 10215094Slling 10225094Slling /* 10235094Slling * If we have a decimal value, then do the computation with floating 10245094Slling * point arithmetic. Otherwise, use standard arithmetic. 10255094Slling */ 10265094Slling if (*end == '.') { 10275094Slling double fval = strtod(value, &end); 10285094Slling 10295094Slling if ((shift = str2shift(hdl, end)) == -1) 10305094Slling return (-1); 10315094Slling 10325094Slling fval *= pow(2, shift); 10335094Slling 10345094Slling if (fval > UINT64_MAX) { 10355094Slling if (hdl) 10365094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10375094Slling "numeric value is too large")); 10385094Slling return (-1); 10395094Slling } 10405094Slling 10415094Slling *num = (uint64_t)fval; 10425094Slling } else { 10435094Slling if ((shift = str2shift(hdl, end)) == -1) 10445094Slling return (-1); 10455094Slling 10465094Slling /* Check for overflow */ 10475094Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 10485094Slling if (hdl) 10495094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10505094Slling "numeric value is too large")); 10515094Slling return (-1); 10525094Slling } 10535094Slling 10545094Slling *num <<= shift; 10555094Slling } 10565094Slling 10575094Slling return (0); 10585094Slling } 10595094Slling 10605094Slling /* 10615094Slling * Given a propname=value nvpair to set, parse any numeric properties 10625094Slling * (index, boolean, etc) if they are specified as strings and add the 10635094Slling * resulting nvpair to the returned nvlist. 10645094Slling * 10655094Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 10665094Slling * We want the user to be able to ignore this fact and specify properties 10675094Slling * as native values (numbers, for example) or as strings (to simplify 10685094Slling * command line utilities). This also handles converting index types 10695094Slling * (compression, checksum, etc) from strings to their on-disk index. 10705094Slling */ 10715094Slling int 10725094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 10735094Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 10745094Slling const char *errbuf) 10754543Smarks { 10765094Slling data_type_t datatype = nvpair_type(elem); 10775094Slling zprop_type_t proptype; 10785094Slling const char *propname; 10795094Slling char *value; 10805094Slling boolean_t isnone = B_FALSE; 10815094Slling 10825094Slling if (type == ZFS_TYPE_POOL) { 10835094Slling proptype = zpool_prop_get_type(prop); 10845094Slling propname = zpool_prop_to_name(prop); 10855094Slling } else { 10865094Slling proptype = zfs_prop_get_type(prop); 10875094Slling propname = zfs_prop_to_name(prop); 10885094Slling } 10895094Slling 10905094Slling /* 10915094Slling * Convert any properties to the internal DSL value types. 10925094Slling */ 10935094Slling *svalp = NULL; 10945094Slling *ivalp = 0; 10955094Slling 10965094Slling switch (proptype) { 10975094Slling case PROP_TYPE_STRING: 10985094Slling if (datatype != DATA_TYPE_STRING) { 10995094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11005094Slling "'%s' must be a string"), nvpair_name(elem)); 11015094Slling goto error; 11025094Slling } 11035094Slling (void) nvpair_value_string(elem, svalp); 11045094Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 11055094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11065094Slling "'%s' is too long"), nvpair_name(elem)); 11075094Slling goto error; 11085094Slling } 11095094Slling break; 11105094Slling 11115094Slling case PROP_TYPE_NUMBER: 11125094Slling if (datatype == DATA_TYPE_STRING) { 11135094Slling (void) nvpair_value_string(elem, &value); 11145094Slling if (strcmp(value, "none") == 0) { 11155094Slling isnone = B_TRUE; 11165094Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 11175094Slling != 0) { 11185094Slling goto error; 11195094Slling } 11205094Slling } else if (datatype == DATA_TYPE_UINT64) { 11215094Slling (void) nvpair_value_uint64(elem, ivalp); 11225094Slling } else { 11235094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11245094Slling "'%s' must be a number"), nvpair_name(elem)); 11255094Slling goto error; 11265094Slling } 11275094Slling 11285094Slling /* 11295094Slling * Quota special: force 'none' and don't allow 0. 11305094Slling */ 11315378Sck153898 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 11325378Sck153898 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 11335094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11345378Sck153898 "use 'none' to disable quota/refquota")); 11355094Slling goto error; 11365094Slling } 11375094Slling break; 11385094Slling 11395094Slling case PROP_TYPE_INDEX: 11405378Sck153898 if (datatype != DATA_TYPE_STRING) { 11415094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11425094Slling "'%s' must be a string"), nvpair_name(elem)); 11435094Slling goto error; 11445094Slling } 11455094Slling 11465378Sck153898 (void) nvpair_value_string(elem, &value); 11475378Sck153898 11485094Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 11495094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11505094Slling "'%s' must be one of '%s'"), propname, 11515094Slling zprop_values(prop, type)); 11525094Slling goto error; 11535094Slling } 11545094Slling break; 11555094Slling 11565094Slling default: 11575094Slling abort(); 11585094Slling } 11594543Smarks 11605094Slling /* 11615094Slling * Add the result to our return set of properties. 11625094Slling */ 11635094Slling if (*svalp != NULL) { 11645094Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 11655094Slling (void) no_memory(hdl); 11665094Slling return (-1); 11675094Slling } 11685094Slling } else { 11695094Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 11705094Slling (void) no_memory(hdl); 11715094Slling return (-1); 11725094Slling } 11735094Slling } 11745094Slling 11755094Slling return (0); 11765094Slling error: 11775094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11785094Slling return (-1); 11795094Slling } 11805094Slling 11815094Slling /* 11825094Slling * Given a comma-separated list of properties, construct a property list 11835094Slling * containing both user-defined and native properties. This function will 11845094Slling * return a NULL list if 'all' is specified, which can later be expanded 11855094Slling * by zprop_expand_list(). 11865094Slling */ 11875094Slling int 11885094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 11895094Slling zfs_type_t type) 11905094Slling { 11915094Slling size_t len; 11925094Slling char *s, *p; 11935094Slling char c; 11945094Slling int prop; 11955094Slling zprop_list_t *entry; 11965094Slling zprop_list_t **last; 11975094Slling 11985094Slling *listp = NULL; 11995094Slling last = listp; 12005094Slling 12015094Slling /* 12025094Slling * If 'all' is specified, return a NULL list. 12035094Slling */ 12045094Slling if (strcmp(props, "all") == 0) 12055094Slling return (0); 12065094Slling 12075094Slling /* 12085094Slling * If no props were specified, return an error. 12095094Slling */ 12105094Slling if (props[0] == '\0') { 12115094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12125094Slling "no properties specified")); 12135094Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 12145094Slling "bad property list"))); 12154543Smarks } 12165094Slling 12175094Slling /* 12185094Slling * It would be nice to use getsubopt() here, but the inclusion of column 12195094Slling * aliases makes this more effort than it's worth. 12205094Slling */ 12215094Slling s = props; 12225094Slling while (*s != '\0') { 12235094Slling if ((p = strchr(s, ',')) == NULL) { 12245094Slling len = strlen(s); 12255094Slling p = s + len; 12265094Slling } else { 12275094Slling len = p - s; 12285094Slling } 12295094Slling 12305094Slling /* 12315094Slling * Check for empty options. 12325094Slling */ 12335094Slling if (len == 0) { 12345094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12355094Slling "empty property name")); 12365094Slling return (zfs_error(hdl, EZFS_BADPROP, 12375094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 12385094Slling } 12395094Slling 12405094Slling /* 12415094Slling * Check all regular property names. 12425094Slling */ 12435094Slling c = s[len]; 12445094Slling s[len] = '\0'; 12455094Slling prop = zprop_name_to_prop(s, type); 12465094Slling 12475094Slling if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 12485094Slling prop = ZPROP_INVAL; 12495094Slling 12505094Slling /* 12515094Slling * When no property table entry can be found, return failure if 12525094Slling * this is a pool property or if this isn't a user-defined 12535094Slling * dataset property, 12545094Slling */ 12555094Slling if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 12565094Slling !zfs_prop_user(s))) { 12575094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12585094Slling "invalid property '%s'"), s); 12595094Slling return (zfs_error(hdl, EZFS_BADPROP, 12605094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 12615094Slling } 12625094Slling 12635094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 12645094Slling return (-1); 12655094Slling 12665094Slling entry->pl_prop = prop; 12675094Slling if (prop == ZPROP_INVAL) { 12685094Slling if ((entry->pl_user_prop = zfs_strdup(hdl, s)) 12695094Slling == NULL) { 12705094Slling free(entry); 12715094Slling return (-1); 12725094Slling } 12735094Slling entry->pl_width = strlen(s); 12745094Slling } else { 12755094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, 12765094Slling type); 12775094Slling } 12785094Slling 12795094Slling *last = entry; 12805094Slling last = &entry->pl_next; 12815094Slling 12825094Slling s = p; 12835094Slling if (c == ',') 12845094Slling s++; 12855094Slling } 12865094Slling 12875094Slling return (0); 12885094Slling } 12895094Slling 12905094Slling void 12915094Slling zprop_free_list(zprop_list_t *pl) 12925094Slling { 12935094Slling zprop_list_t *next; 12944543Smarks 12955094Slling while (pl != NULL) { 12965094Slling next = pl->pl_next; 12975094Slling free(pl->pl_user_prop); 12985094Slling free(pl); 12995094Slling pl = next; 13005094Slling } 13015094Slling } 13025094Slling 13035094Slling typedef struct expand_data { 13045094Slling zprop_list_t **last; 13055094Slling libzfs_handle_t *hdl; 13065094Slling zfs_type_t type; 13075094Slling } expand_data_t; 13085094Slling 13095094Slling int 13105094Slling zprop_expand_list_cb(int prop, void *cb) 13115094Slling { 13125094Slling zprop_list_t *entry; 13135094Slling expand_data_t *edp = cb; 13145094Slling 13155094Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 13165094Slling return (ZPROP_INVAL); 13175094Slling 13185094Slling entry->pl_prop = prop; 13195094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 13205094Slling entry->pl_all = B_TRUE; 13215094Slling 13225094Slling *(edp->last) = entry; 13235094Slling edp->last = &entry->pl_next; 13245094Slling 13255094Slling return (ZPROP_CONT); 13264543Smarks } 13275094Slling 13285094Slling int 13295094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 13305094Slling { 13315094Slling zprop_list_t *entry; 13325094Slling zprop_list_t **last; 13335094Slling expand_data_t exp; 13345094Slling 13355094Slling if (*plp == NULL) { 13365094Slling /* 13375094Slling * If this is the very first time we've been called for an 'all' 13385094Slling * specification, expand the list to include all native 13395094Slling * properties. 13405094Slling */ 13415094Slling last = plp; 13425094Slling 13435094Slling exp.last = last; 13445094Slling exp.hdl = hdl; 13455094Slling exp.type = type; 13465094Slling 13475094Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 13485094Slling B_FALSE, type) == ZPROP_INVAL) 13495094Slling return (-1); 13505094Slling 13515094Slling /* 13525094Slling * Add 'name' to the beginning of the list, which is handled 13535094Slling * specially. 13545094Slling */ 13555094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 13565094Slling return (-1); 13575094Slling 13585094Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 13595094Slling ZFS_PROP_NAME; 13605094Slling entry->pl_width = zprop_width(entry->pl_prop, 13615094Slling &entry->pl_fixed, type); 13625094Slling entry->pl_all = B_TRUE; 13635094Slling entry->pl_next = *plp; 13645094Slling *plp = entry; 13655094Slling } 13665094Slling return (0); 13675094Slling } 13685094Slling 13695094Slling int 13705094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 13715094Slling zfs_type_t type) 13725094Slling { 13735094Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 13745094Slling } 1375