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 /* 223635Sck153898 * Copyright 2007 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")); 139*5331Samw case EZFS_UNSHARESMBFAILED: 140*5331Samw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 141*5331Samw case EZFS_SHARESMBFAILED: 142*5331Samw 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")); 2022082Seschrock case EZFS_UNKNOWN: 2032082Seschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 2042082Seschrock default: 2052500Seschrock assert(hdl->libzfs_error == 0); 2062500Seschrock return (dgettext(TEXT_DOMAIN, "no error")); 2072082Seschrock } 2082082Seschrock } 2092082Seschrock 2102082Seschrock /*PRINTFLIKE2*/ 211789Sahrens void 2122082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 213789Sahrens { 214789Sahrens va_list ap; 215789Sahrens 216789Sahrens va_start(ap, fmt); 217789Sahrens 2182082Seschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 2192082Seschrock fmt, ap); 2202082Seschrock hdl->libzfs_desc_active = 1; 221789Sahrens 222789Sahrens va_end(ap); 223789Sahrens } 224789Sahrens 2252082Seschrock static void 2262082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 2272082Seschrock { 2282082Seschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 2292082Seschrock fmt, ap); 2302082Seschrock hdl->libzfs_error = error; 2312082Seschrock 2322082Seschrock if (hdl->libzfs_desc_active) 2332082Seschrock hdl->libzfs_desc_active = 0; 2342082Seschrock else 2352082Seschrock hdl->libzfs_desc[0] = '\0'; 2362082Seschrock 2372082Seschrock if (hdl->libzfs_printerr) { 2382082Seschrock if (error == EZFS_UNKNOWN) { 2392082Seschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 2402082Seschrock "error: %s\n"), libzfs_error_description(hdl)); 2412082Seschrock abort(); 2422082Seschrock } 2432082Seschrock 2442082Seschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 2453912Slling libzfs_error_description(hdl)); 2462082Seschrock if (error == EZFS_NOMEM) 2472082Seschrock exit(1); 2482082Seschrock } 2492082Seschrock } 2502082Seschrock 2513237Slling int 2523237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 2533237Slling { 2543237Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 2553237Slling } 2563237Slling 2572082Seschrock /*PRINTFLIKE3*/ 2582082Seschrock int 2593237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 2602082Seschrock { 2612082Seschrock va_list ap; 2622082Seschrock 2632082Seschrock va_start(ap, fmt); 2642082Seschrock 2652082Seschrock zfs_verror(hdl, error, fmt, ap); 2662082Seschrock 2672082Seschrock va_end(ap); 2682082Seschrock 2692082Seschrock return (-1); 2702082Seschrock } 2712082Seschrock 2722082Seschrock static int 2732082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 2742082Seschrock va_list ap) 2752082Seschrock { 2762082Seschrock switch (error) { 2772082Seschrock case EPERM: 2782082Seschrock case EACCES: 2792082Seschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 2802082Seschrock return (-1); 2812082Seschrock 2824543Smarks case ECANCELED: 2834543Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 2844543Smarks return (-1); 2854543Smarks 2862082Seschrock case EIO: 2872082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 2882082Seschrock return (-1); 2892082Seschrock 2902082Seschrock case EINTR: 2912082Seschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 2922082Seschrock return (-1); 2932082Seschrock } 2942082Seschrock 2952082Seschrock return (0); 2962082Seschrock } 2972082Seschrock 2983237Slling int 2993237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3003237Slling { 3013237Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 3023237Slling } 3033237Slling 3042082Seschrock /*PRINTFLIKE3*/ 3052082Seschrock int 3063237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 307789Sahrens { 308789Sahrens va_list ap; 309789Sahrens 310789Sahrens va_start(ap, fmt); 311789Sahrens 3122082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3132082Seschrock va_end(ap); 3142082Seschrock return (-1); 3152082Seschrock } 3162082Seschrock 3172082Seschrock 3182082Seschrock switch (error) { 3192082Seschrock case ENXIO: 3202082Seschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 3212082Seschrock break; 3222082Seschrock 3232082Seschrock case ENOENT: 3242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3252082Seschrock "dataset does not exist")); 3262082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 3272082Seschrock break; 3282082Seschrock 3292082Seschrock case ENOSPC: 3302082Seschrock case EDQUOT: 3312082Seschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 3322082Seschrock return (-1); 3332082Seschrock 3342082Seschrock case EEXIST: 3352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3362082Seschrock "dataset already exists")); 3372082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 3382082Seschrock break; 3392082Seschrock 3402082Seschrock case EBUSY: 3412082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3422082Seschrock "dataset is busy")); 3432082Seschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 3442082Seschrock break; 3454543Smarks case EROFS: 3464543Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3474543Smarks "snapshot permissions cannot be modified")); 3484543Smarks zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 3494543Smarks break; 3503978Smmusante case ENAMETOOLONG: 3513978Smmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 3523978Smmusante break; 3532082Seschrock default: 3542082Seschrock zfs_error_aux(hdl, strerror(errno)); 3552082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 3562082Seschrock break; 357789Sahrens } 358789Sahrens 359789Sahrens va_end(ap); 3602082Seschrock return (-1); 361789Sahrens } 362789Sahrens 3633237Slling int 3643237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 3653237Slling { 3663237Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 3673237Slling } 3683237Slling 3692082Seschrock /*PRINTFLIKE3*/ 3702082Seschrock int 3713237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 372789Sahrens { 3732082Seschrock va_list ap; 3742082Seschrock 3752082Seschrock va_start(ap, fmt); 3762082Seschrock 3772082Seschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 3782082Seschrock va_end(ap); 3792082Seschrock return (-1); 3802082Seschrock } 3812082Seschrock 3822082Seschrock switch (error) { 3832082Seschrock case ENODEV: 3842082Seschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 3852082Seschrock break; 3862082Seschrock 3872082Seschrock case ENOENT: 3883912Slling zfs_error_aux(hdl, 3893912Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 3902082Seschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 3912082Seschrock break; 3922082Seschrock 3932082Seschrock case EEXIST: 3942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3952082Seschrock "pool already exists")); 3962082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 3972082Seschrock break; 3982082Seschrock 3992082Seschrock case EBUSY: 4002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 4012082Seschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 4022082Seschrock break; 4032082Seschrock 4042082Seschrock case ENXIO: 4052082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4062082Seschrock "one or more devices is currently unavailable")); 4072082Seschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 4082082Seschrock break; 4092082Seschrock 4102082Seschrock case ENAMETOOLONG: 4112082Seschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 4122082Seschrock break; 4132082Seschrock 4143912Slling case ENOTSUP: 4153912Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 4163912Slling break; 4173912Slling 4183912Slling case EINVAL: 4193912Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 4203912Slling break; 4213912Slling 4224543Smarks case ENOSPC: 4234543Smarks case EDQUOT: 4244543Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 4254543Smarks return (-1); 4264543Smarks 4272082Seschrock default: 4282082Seschrock zfs_error_aux(hdl, strerror(error)); 4292082Seschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 4302082Seschrock } 4312082Seschrock 4322082Seschrock va_end(ap); 4332082Seschrock return (-1); 434789Sahrens } 435789Sahrens 436789Sahrens /* 437789Sahrens * Display an out of memory error message and abort the current program. 438789Sahrens */ 4392082Seschrock int 4402082Seschrock no_memory(libzfs_handle_t *hdl) 441789Sahrens { 4422082Seschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 443789Sahrens } 444789Sahrens 445789Sahrens /* 446789Sahrens * A safe form of malloc() which will die if the allocation fails. 447789Sahrens */ 448789Sahrens void * 4492082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 450789Sahrens { 451789Sahrens void *data; 452789Sahrens 453789Sahrens if ((data = calloc(1, size)) == NULL) 4542082Seschrock (void) no_memory(hdl); 455789Sahrens 456789Sahrens return (data); 457789Sahrens } 458789Sahrens 459789Sahrens /* 4602676Seschrock * A safe form of realloc(), which also zeroes newly allocated space. 4612676Seschrock */ 4622676Seschrock void * 4632676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 4642676Seschrock { 4652676Seschrock void *ret; 4662676Seschrock 4672676Seschrock if ((ret = realloc(ptr, newsize)) == NULL) { 4682676Seschrock (void) no_memory(hdl); 4692676Seschrock free(ptr); 4702676Seschrock return (NULL); 4712676Seschrock } 4722676Seschrock 4732676Seschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 4742676Seschrock return (ret); 4752676Seschrock } 4762676Seschrock 4772676Seschrock /* 478789Sahrens * A safe form of strdup() which will die if the allocation fails. 479789Sahrens */ 480789Sahrens char * 4812082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 482789Sahrens { 483789Sahrens char *ret; 484789Sahrens 485789Sahrens if ((ret = strdup(str)) == NULL) 4862082Seschrock (void) no_memory(hdl); 487789Sahrens 488789Sahrens return (ret); 489789Sahrens } 490789Sahrens 491789Sahrens /* 492789Sahrens * Convert a number to an appropriately human-readable output. 493789Sahrens */ 494789Sahrens void 495789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 496789Sahrens { 497789Sahrens uint64_t n = num; 498789Sahrens int index = 0; 499789Sahrens char u; 500789Sahrens 501789Sahrens while (n >= 1024) { 5021162Seschrock n /= 1024; 503789Sahrens index++; 504789Sahrens } 505789Sahrens 506789Sahrens u = " KMGTPE"[index]; 507789Sahrens 5081162Seschrock if (index == 0) { 509789Sahrens (void) snprintf(buf, buflen, "%llu", n); 5101162Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5111162Seschrock /* 5121162Seschrock * If this is an even multiple of the base, always display 5131162Seschrock * without any decimal precision. 5141162Seschrock */ 515789Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5161162Seschrock } else { 5171162Seschrock /* 5181162Seschrock * We want to choose a precision that reflects the best choice 5191162Seschrock * for fitting in 5 characters. This can get rather tricky when 5201162Seschrock * we have numbers that are very close to an order of magnitude. 5211162Seschrock * For example, when displaying 10239 (which is really 9.999K), 5221162Seschrock * we want only a single place of precision for 10.0K. We could 5231162Seschrock * develop some complex heuristics for this, but it's much 5241162Seschrock * easier just to try each combination in turn. 5251162Seschrock */ 5261162Seschrock int i; 5271162Seschrock for (i = 2; i >= 0; i--) { 5284451Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5294451Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5301162Seschrock break; 5311162Seschrock } 5321162Seschrock } 533789Sahrens } 5342082Seschrock 5352082Seschrock void 5362082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 5372082Seschrock { 5382082Seschrock hdl->libzfs_printerr = printerr; 5392082Seschrock } 5402082Seschrock 5412082Seschrock libzfs_handle_t * 5422082Seschrock libzfs_init(void) 5432082Seschrock { 5442082Seschrock libzfs_handle_t *hdl; 5452082Seschrock 5462082Seschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 5472082Seschrock return (NULL); 5482082Seschrock } 5492082Seschrock 5502500Seschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 5512082Seschrock free(hdl); 5522082Seschrock return (NULL); 5532082Seschrock } 5542082Seschrock 5552082Seschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 5562082Seschrock (void) close(hdl->libzfs_fd); 5572082Seschrock free(hdl); 5582082Seschrock return (NULL); 5592082Seschrock } 5602082Seschrock 5612082Seschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 5622082Seschrock 5634787Sahrens zfs_prop_init(); 5645094Slling zpool_prop_init(); 5654787Sahrens 5662082Seschrock return (hdl); 5672082Seschrock } 5682082Seschrock 5692082Seschrock void 5702082Seschrock libzfs_fini(libzfs_handle_t *hdl) 5712082Seschrock { 5722082Seschrock (void) close(hdl->libzfs_fd); 5732082Seschrock if (hdl->libzfs_mnttab) 5742082Seschrock (void) fclose(hdl->libzfs_mnttab); 5752082Seschrock if (hdl->libzfs_sharetab) 5762082Seschrock (void) fclose(hdl->libzfs_sharetab); 5774180Sdougm zfs_uninit_libshare(hdl); 5784543Smarks if (hdl->libzfs_log_str) 5794543Smarks (void) free(hdl->libzfs_log_str); 5802082Seschrock namespace_clear(hdl); 5812082Seschrock free(hdl); 5822082Seschrock } 5832082Seschrock 5842082Seschrock libzfs_handle_t * 5852082Seschrock zpool_get_handle(zpool_handle_t *zhp) 5862082Seschrock { 5872082Seschrock return (zhp->zpool_hdl); 5882082Seschrock } 5892082Seschrock 5902082Seschrock libzfs_handle_t * 5912082Seschrock zfs_get_handle(zfs_handle_t *zhp) 5922082Seschrock { 5932082Seschrock return (zhp->zfs_hdl); 5942082Seschrock } 5952676Seschrock 5962676Seschrock /* 5973635Sck153898 * Given a name, determine whether or not it's a valid path 5983635Sck153898 * (starts with '/' or "./"). If so, walk the mnttab trying 5993635Sck153898 * to match the device number. If not, treat the path as an 6003635Sck153898 * fs/vol/snap name. 6013635Sck153898 */ 6023635Sck153898 zfs_handle_t * 6033635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6043635Sck153898 { 6053635Sck153898 struct stat64 statbuf; 6063635Sck153898 struct extmnttab entry; 6073635Sck153898 int ret; 6083635Sck153898 6093635Sck153898 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6103635Sck153898 /* 6113635Sck153898 * It's not a valid path, assume it's a name of type 'argtype'. 6123635Sck153898 */ 6133635Sck153898 return (zfs_open(hdl, path, argtype)); 6143635Sck153898 } 6153635Sck153898 6163635Sck153898 if (stat64(path, &statbuf) != 0) { 6173635Sck153898 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6183635Sck153898 return (NULL); 6193635Sck153898 } 6203635Sck153898 6213635Sck153898 rewind(hdl->libzfs_mnttab); 6223635Sck153898 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6233635Sck153898 if (makedevice(entry.mnt_major, entry.mnt_minor) == 6243635Sck153898 statbuf.st_dev) { 6253635Sck153898 break; 6263635Sck153898 } 6273635Sck153898 } 6283635Sck153898 if (ret != 0) { 6293635Sck153898 return (NULL); 6303635Sck153898 } 6313635Sck153898 6323635Sck153898 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6333635Sck153898 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 6343635Sck153898 path); 6353635Sck153898 return (NULL); 6363635Sck153898 } 6373635Sck153898 6383635Sck153898 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6393635Sck153898 } 6403635Sck153898 6413635Sck153898 /* 6422676Seschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 6432676Seschrock * an ioctl(). 6442676Seschrock */ 6452676Seschrock int 6462676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 6472676Seschrock { 6482676Seschrock if (len == 0) 6492885Sahrens len = 2048; 6502676Seschrock zc->zc_nvlist_dst_size = len; 6512676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6522676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 6532676Seschrock return (-1); 6542676Seschrock 6552676Seschrock return (0); 6562676Seschrock } 6572676Seschrock 6582676Seschrock /* 6592676Seschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 6602676Seschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 6612676Seschrock * filled in by the kernel to indicate the actual required size. 6622676Seschrock */ 6632676Seschrock int 6642676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 6652676Seschrock { 6662676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 6672676Seschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 6682676Seschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 6692676Seschrock == NULL) 6702676Seschrock return (-1); 6712676Seschrock 6722676Seschrock return (0); 6732676Seschrock } 6742676Seschrock 6752676Seschrock /* 6762885Sahrens * Called to free the src and dst nvlists stored in the command structure. 6772676Seschrock */ 6782676Seschrock void 6792676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc) 6802676Seschrock { 6815094Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 6822676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 6832676Seschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 6842676Seschrock } 6852676Seschrock 6865094Slling static int 6875094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 6885094Slling nvlist_t *nvl) 6892676Seschrock { 6902676Seschrock char *packed; 6912676Seschrock size_t len; 6922676Seschrock 6932676Seschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 6942676Seschrock 6952676Seschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 6962676Seschrock return (-1); 6972676Seschrock 6982676Seschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 6992676Seschrock 7005094Slling *outnv = (uint64_t)(uintptr_t)packed; 7015094Slling *outlen = len; 7025094Slling 7035094Slling return (0); 7045094Slling } 7052676Seschrock 7065094Slling int 7075094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7085094Slling { 7095094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 7105094Slling &zc->zc_nvlist_conf_size, nvl)); 7115094Slling } 7125094Slling 7135094Slling int 7145094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 7155094Slling { 7165094Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 7175094Slling &zc->zc_nvlist_src_size, nvl)); 7182676Seschrock } 7192676Seschrock 7202676Seschrock /* 7212676Seschrock * Unpacks an nvlist from the ZFS ioctl command structure. 7222676Seschrock */ 7232676Seschrock int 7242676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 7252676Seschrock { 7262676Seschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 7272676Seschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 7282676Seschrock return (no_memory(hdl)); 7292676Seschrock 7302676Seschrock return (0); 7312676Seschrock } 7323912Slling 7335094Slling int 7345094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 7355094Slling { 7365094Slling int error; 7375094Slling 7385094Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 7395094Slling error = ioctl(hdl->libzfs_fd, request, zc); 7405094Slling if (hdl->libzfs_log_str) { 7415094Slling free(hdl->libzfs_log_str); 7425094Slling hdl->libzfs_log_str = NULL; 7435094Slling } 7445094Slling zc->zc_history = 0; 7455094Slling 7465094Slling return (error); 7475094Slling } 7485094Slling 7495094Slling /* 7505094Slling * ================================================================ 7515094Slling * API shared by zfs and zpool property management 7525094Slling * ================================================================ 7535094Slling */ 7545094Slling 7553912Slling static void 7565094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 7573912Slling { 7585094Slling zprop_list_t *pl = cbp->cb_proplist; 7593912Slling int i; 7603912Slling char *title; 7613912Slling size_t len; 7623912Slling 7633912Slling cbp->cb_first = B_FALSE; 7643912Slling if (cbp->cb_scripted) 7653912Slling return; 7663912Slling 7673912Slling /* 7683912Slling * Start with the length of the column headers. 7693912Slling */ 7703912Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 7713912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 7723912Slling "PROPERTY")); 7733912Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 7743912Slling "VALUE")); 7753912Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 7763912Slling "SOURCE")); 7773912Slling 7783912Slling /* 7793912Slling * Go through and calculate the widths for each column. For the 7803912Slling * 'source' column, we kludge it up by taking the worst-case scenario of 7813912Slling * inheriting from the longest name. This is acceptable because in the 7823912Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 7833912Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 7843912Slling * if the name of the property is much longer the any values we find. 7853912Slling */ 7863912Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 7873912Slling /* 7883912Slling * 'PROPERTY' column 7893912Slling */ 7905094Slling if (pl->pl_prop != ZPROP_INVAL) { 7915094Slling const char *propname = (type == ZFS_TYPE_POOL) ? 7925094Slling zpool_prop_to_name(pl->pl_prop) : 7935094Slling zfs_prop_to_name(pl->pl_prop); 7945094Slling 7955094Slling len = strlen(propname); 7963912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 7973912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 7983912Slling } else { 7993912Slling len = strlen(pl->pl_user_prop); 8003912Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 8013912Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 8023912Slling } 8033912Slling 8043912Slling /* 8053912Slling * 'VALUE' column 8063912Slling */ 8073912Slling if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) && 8083912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 8093912Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 8103912Slling 8113912Slling /* 8123912Slling * 'NAME' and 'SOURCE' columns 8133912Slling */ 8145094Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 8155094Slling ZFS_PROP_NAME) && 8163912Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 8173912Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 8183912Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 8193912Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 8203912Slling } 8213912Slling } 8223912Slling 8233912Slling /* 8243912Slling * Now go through and print the headers. 8253912Slling */ 8263912Slling for (i = 0; i < 4; i++) { 8273912Slling switch (cbp->cb_columns[i]) { 8283912Slling case GET_COL_NAME: 8293912Slling title = dgettext(TEXT_DOMAIN, "NAME"); 8303912Slling break; 8313912Slling case GET_COL_PROPERTY: 8323912Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 8333912Slling break; 8343912Slling case GET_COL_VALUE: 8353912Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 8363912Slling break; 8373912Slling case GET_COL_SOURCE: 8383912Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 8393912Slling break; 8403912Slling default: 8413912Slling title = NULL; 8423912Slling } 8433912Slling 8443912Slling if (title != NULL) { 8453912Slling if (i == 3 || cbp->cb_columns[i + 1] == 0) 8463912Slling (void) printf("%s", title); 8473912Slling else 8483912Slling (void) printf("%-*s ", 8493912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 8503912Slling title); 8513912Slling } 8523912Slling } 8533912Slling (void) printf("\n"); 8543912Slling } 8553912Slling 8563912Slling /* 8573912Slling * Display a single line of output, according to the settings in the callback 8583912Slling * structure. 8593912Slling */ 8603912Slling void 8615094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 8625094Slling const char *propname, const char *value, zprop_source_t sourcetype, 8633912Slling const char *source) 8643912Slling { 8653912Slling int i; 8663912Slling const char *str; 8673912Slling char buf[128]; 8683912Slling 8693912Slling /* 8703912Slling * Ignore those source types that the user has chosen to ignore. 8713912Slling */ 8723912Slling if ((sourcetype & cbp->cb_sources) == 0) 8733912Slling return; 8743912Slling 8753912Slling if (cbp->cb_first) 8765094Slling zprop_print_headers(cbp, cbp->cb_type); 8773912Slling 8783912Slling for (i = 0; i < 4; i++) { 8793912Slling switch (cbp->cb_columns[i]) { 8803912Slling case GET_COL_NAME: 8813912Slling str = name; 8823912Slling break; 8833912Slling 8843912Slling case GET_COL_PROPERTY: 8853912Slling str = propname; 8863912Slling break; 8873912Slling 8883912Slling case GET_COL_VALUE: 8893912Slling str = value; 8903912Slling break; 8913912Slling 8923912Slling case GET_COL_SOURCE: 8933912Slling switch (sourcetype) { 8945094Slling case ZPROP_SRC_NONE: 8953912Slling str = "-"; 8963912Slling break; 8973912Slling 8985094Slling case ZPROP_SRC_DEFAULT: 8993912Slling str = "default"; 9003912Slling break; 9013912Slling 9025094Slling case ZPROP_SRC_LOCAL: 9033912Slling str = "local"; 9043912Slling break; 9053912Slling 9065094Slling case ZPROP_SRC_TEMPORARY: 9073912Slling str = "temporary"; 9083912Slling break; 9093912Slling 9105094Slling case ZPROP_SRC_INHERITED: 9113912Slling (void) snprintf(buf, sizeof (buf), 9123912Slling "inherited from %s", source); 9133912Slling str = buf; 9143912Slling break; 9153912Slling } 9163912Slling break; 9173912Slling 9183912Slling default: 9193912Slling continue; 9203912Slling } 9213912Slling 9223912Slling if (cbp->cb_columns[i + 1] == 0) 9233912Slling (void) printf("%s", str); 9243912Slling else if (cbp->cb_scripted) 9253912Slling (void) printf("%s\t", str); 9263912Slling else 9273912Slling (void) printf("%-*s ", 9283912Slling cbp->cb_colwidths[cbp->cb_columns[i]], 9293912Slling str); 9303912Slling 9313912Slling } 9323912Slling 9333912Slling (void) printf("\n"); 9343912Slling } 9354543Smarks 9365094Slling /* 9375094Slling * Given a numeric suffix, convert the value into a number of bits that the 9385094Slling * resulting value must be shifted. 9395094Slling */ 9405094Slling static int 9415094Slling str2shift(libzfs_handle_t *hdl, const char *buf) 9425094Slling { 9435094Slling const char *ends = "BKMGTPEZ"; 9445094Slling int i; 9455094Slling 9465094Slling if (buf[0] == '\0') 9475094Slling return (0); 9485094Slling for (i = 0; i < strlen(ends); i++) { 9495094Slling if (toupper(buf[0]) == ends[i]) 9505094Slling break; 9515094Slling } 9525094Slling if (i == strlen(ends)) { 9535094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9545094Slling "invalid numeric suffix '%s'"), buf); 9555094Slling return (-1); 9565094Slling } 9575094Slling 9585094Slling /* 9595094Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 9605094Slling * allow 'BB' - that's just weird. 9615094Slling */ 9625094Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 9635094Slling toupper(buf[0]) != 'B')) 9645094Slling return (10*i); 9655094Slling 9665094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9675094Slling "invalid numeric suffix '%s'"), buf); 9685094Slling return (-1); 9695094Slling } 9705094Slling 9715094Slling /* 9725094Slling * Convert a string of the form '100G' into a real number. Used when setting 9735094Slling * properties or creating a volume. 'buf' is used to place an extended error 9745094Slling * message for the caller to use. 9755094Slling */ 9764543Smarks int 9775094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 9785094Slling { 9795094Slling char *end; 9805094Slling int shift; 9815094Slling 9825094Slling *num = 0; 9835094Slling 9845094Slling /* Check to see if this looks like a number. */ 9855094Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 9865094Slling if (hdl) 9875094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9885094Slling "bad numeric value '%s'"), value); 9895094Slling return (-1); 9905094Slling } 9915094Slling 9925094Slling /* Rely on stroll() to process the numeric portion. */ 9935094Slling errno = 0; 9945094Slling *num = strtoll(value, &end, 10); 9955094Slling 9965094Slling /* 9975094Slling * Check for ERANGE, which indicates that the value is too large to fit 9985094Slling * in a 64-bit value. 9995094Slling */ 10005094Slling if (errno == ERANGE) { 10015094Slling if (hdl) 10025094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10035094Slling "numeric value is too large")); 10045094Slling return (-1); 10055094Slling } 10065094Slling 10075094Slling /* 10085094Slling * If we have a decimal value, then do the computation with floating 10095094Slling * point arithmetic. Otherwise, use standard arithmetic. 10105094Slling */ 10115094Slling if (*end == '.') { 10125094Slling double fval = strtod(value, &end); 10135094Slling 10145094Slling if ((shift = str2shift(hdl, end)) == -1) 10155094Slling return (-1); 10165094Slling 10175094Slling fval *= pow(2, shift); 10185094Slling 10195094Slling if (fval > UINT64_MAX) { 10205094Slling if (hdl) 10215094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10225094Slling "numeric value is too large")); 10235094Slling return (-1); 10245094Slling } 10255094Slling 10265094Slling *num = (uint64_t)fval; 10275094Slling } else { 10285094Slling if ((shift = str2shift(hdl, end)) == -1) 10295094Slling return (-1); 10305094Slling 10315094Slling /* Check for overflow */ 10325094Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 10335094Slling if (hdl) 10345094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10355094Slling "numeric value is too large")); 10365094Slling return (-1); 10375094Slling } 10385094Slling 10395094Slling *num <<= shift; 10405094Slling } 10415094Slling 10425094Slling return (0); 10435094Slling } 10445094Slling 10455094Slling /* 10465094Slling * Given a propname=value nvpair to set, parse any numeric properties 10475094Slling * (index, boolean, etc) if they are specified as strings and add the 10485094Slling * resulting nvpair to the returned nvlist. 10495094Slling * 10505094Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 10515094Slling * We want the user to be able to ignore this fact and specify properties 10525094Slling * as native values (numbers, for example) or as strings (to simplify 10535094Slling * command line utilities). This also handles converting index types 10545094Slling * (compression, checksum, etc) from strings to their on-disk index. 10555094Slling */ 10565094Slling int 10575094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 10585094Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 10595094Slling const char *errbuf) 10604543Smarks { 10615094Slling data_type_t datatype = nvpair_type(elem); 10625094Slling zprop_type_t proptype; 10635094Slling const char *propname; 10645094Slling char *value; 10655094Slling boolean_t isnone = B_FALSE; 10665094Slling 10675094Slling if (type == ZFS_TYPE_POOL) { 10685094Slling proptype = zpool_prop_get_type(prop); 10695094Slling propname = zpool_prop_to_name(prop); 10705094Slling } else { 10715094Slling proptype = zfs_prop_get_type(prop); 10725094Slling propname = zfs_prop_to_name(prop); 10735094Slling } 10745094Slling 10755094Slling /* 10765094Slling * Convert any properties to the internal DSL value types. 10775094Slling */ 10785094Slling *svalp = NULL; 10795094Slling *ivalp = 0; 10805094Slling 10815094Slling switch (proptype) { 10825094Slling case PROP_TYPE_STRING: 10835094Slling if (datatype != DATA_TYPE_STRING) { 10845094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10855094Slling "'%s' must be a string"), nvpair_name(elem)); 10865094Slling goto error; 10875094Slling } 10885094Slling (void) nvpair_value_string(elem, svalp); 10895094Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 10905094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10915094Slling "'%s' is too long"), nvpair_name(elem)); 10925094Slling goto error; 10935094Slling } 10945094Slling break; 10955094Slling 10965094Slling case PROP_TYPE_NUMBER: 10975094Slling if (datatype == DATA_TYPE_STRING) { 10985094Slling (void) nvpair_value_string(elem, &value); 10995094Slling if (strcmp(value, "none") == 0) { 11005094Slling isnone = B_TRUE; 11015094Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 11025094Slling != 0) { 11035094Slling goto error; 11045094Slling } 11055094Slling } else if (datatype == DATA_TYPE_UINT64) { 11065094Slling (void) nvpair_value_uint64(elem, ivalp); 11075094Slling } else { 11085094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11095094Slling "'%s' must be a number"), nvpair_name(elem)); 11105094Slling goto error; 11115094Slling } 11125094Slling 11135094Slling /* 11145094Slling * Quota special: force 'none' and don't allow 0. 11155094Slling */ 11165094Slling if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && 11175094Slling !isnone && prop == ZFS_PROP_QUOTA) { 11185094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11195094Slling "use 'none' to disable quota")); 11205094Slling goto error; 11215094Slling } 11225094Slling break; 11235094Slling 11245094Slling case PROP_TYPE_INDEX: 11255094Slling if (datatype != DATA_TYPE_STRING) { 11265094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11275094Slling "'%s' must be a string"), nvpair_name(elem)); 11285094Slling goto error; 11295094Slling } 11305094Slling 11315094Slling (void) nvpair_value_string(elem, &value); 11325094Slling 11335094Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 11345094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11355094Slling "'%s' must be one of '%s'"), propname, 11365094Slling zprop_values(prop, type)); 11375094Slling goto error; 11385094Slling } 11395094Slling break; 11405094Slling 11415094Slling default: 11425094Slling abort(); 11435094Slling } 11444543Smarks 11455094Slling /* 11465094Slling * Add the result to our return set of properties. 11475094Slling */ 11485094Slling if (*svalp != NULL) { 11495094Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 11505094Slling (void) no_memory(hdl); 11515094Slling return (-1); 11525094Slling } 11535094Slling } else { 11545094Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 11555094Slling (void) no_memory(hdl); 11565094Slling return (-1); 11575094Slling } 11585094Slling } 11595094Slling 11605094Slling return (0); 11615094Slling error: 11625094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11635094Slling return (-1); 11645094Slling } 11655094Slling 11665094Slling /* 11675094Slling * Given a comma-separated list of properties, construct a property list 11685094Slling * containing both user-defined and native properties. This function will 11695094Slling * return a NULL list if 'all' is specified, which can later be expanded 11705094Slling * by zprop_expand_list(). 11715094Slling */ 11725094Slling int 11735094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 11745094Slling zfs_type_t type) 11755094Slling { 11765094Slling size_t len; 11775094Slling char *s, *p; 11785094Slling char c; 11795094Slling int prop; 11805094Slling zprop_list_t *entry; 11815094Slling zprop_list_t **last; 11825094Slling 11835094Slling *listp = NULL; 11845094Slling last = listp; 11855094Slling 11865094Slling /* 11875094Slling * If 'all' is specified, return a NULL list. 11885094Slling */ 11895094Slling if (strcmp(props, "all") == 0) 11905094Slling return (0); 11915094Slling 11925094Slling /* 11935094Slling * If no props were specified, return an error. 11945094Slling */ 11955094Slling if (props[0] == '\0') { 11965094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11975094Slling "no properties specified")); 11985094Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 11995094Slling "bad property list"))); 12004543Smarks } 12015094Slling 12025094Slling /* 12035094Slling * It would be nice to use getsubopt() here, but the inclusion of column 12045094Slling * aliases makes this more effort than it's worth. 12055094Slling */ 12065094Slling s = props; 12075094Slling while (*s != '\0') { 12085094Slling if ((p = strchr(s, ',')) == NULL) { 12095094Slling len = strlen(s); 12105094Slling p = s + len; 12115094Slling } else { 12125094Slling len = p - s; 12135094Slling } 12145094Slling 12155094Slling /* 12165094Slling * Check for empty options. 12175094Slling */ 12185094Slling if (len == 0) { 12195094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12205094Slling "empty property name")); 12215094Slling return (zfs_error(hdl, EZFS_BADPROP, 12225094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 12235094Slling } 12245094Slling 12255094Slling /* 12265094Slling * Check all regular property names. 12275094Slling */ 12285094Slling c = s[len]; 12295094Slling s[len] = '\0'; 12305094Slling prop = zprop_name_to_prop(s, type); 12315094Slling 12325094Slling if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 12335094Slling prop = ZPROP_INVAL; 12345094Slling 12355094Slling /* 12365094Slling * When no property table entry can be found, return failure if 12375094Slling * this is a pool property or if this isn't a user-defined 12385094Slling * dataset property, 12395094Slling */ 12405094Slling if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 12415094Slling !zfs_prop_user(s))) { 12425094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12435094Slling "invalid property '%s'"), s); 12445094Slling return (zfs_error(hdl, EZFS_BADPROP, 12455094Slling dgettext(TEXT_DOMAIN, "bad property list"))); 12465094Slling } 12475094Slling 12485094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 12495094Slling return (-1); 12505094Slling 12515094Slling entry->pl_prop = prop; 12525094Slling if (prop == ZPROP_INVAL) { 12535094Slling if ((entry->pl_user_prop = zfs_strdup(hdl, s)) 12545094Slling == NULL) { 12555094Slling free(entry); 12565094Slling return (-1); 12575094Slling } 12585094Slling entry->pl_width = strlen(s); 12595094Slling } else { 12605094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, 12615094Slling type); 12625094Slling } 12635094Slling 12645094Slling *last = entry; 12655094Slling last = &entry->pl_next; 12665094Slling 12675094Slling s = p; 12685094Slling if (c == ',') 12695094Slling s++; 12705094Slling } 12715094Slling 12725094Slling return (0); 12735094Slling } 12745094Slling 12755094Slling void 12765094Slling zprop_free_list(zprop_list_t *pl) 12775094Slling { 12785094Slling zprop_list_t *next; 12794543Smarks 12805094Slling while (pl != NULL) { 12815094Slling next = pl->pl_next; 12825094Slling free(pl->pl_user_prop); 12835094Slling free(pl); 12845094Slling pl = next; 12855094Slling } 12865094Slling } 12875094Slling 12885094Slling typedef struct expand_data { 12895094Slling zprop_list_t **last; 12905094Slling libzfs_handle_t *hdl; 12915094Slling zfs_type_t type; 12925094Slling } expand_data_t; 12935094Slling 12945094Slling int 12955094Slling zprop_expand_list_cb(int prop, void *cb) 12965094Slling { 12975094Slling zprop_list_t *entry; 12985094Slling expand_data_t *edp = cb; 12995094Slling 13005094Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 13015094Slling return (ZPROP_INVAL); 13025094Slling 13035094Slling entry->pl_prop = prop; 13045094Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 13055094Slling entry->pl_all = B_TRUE; 13065094Slling 13075094Slling *(edp->last) = entry; 13085094Slling edp->last = &entry->pl_next; 13095094Slling 13105094Slling return (ZPROP_CONT); 13114543Smarks } 13125094Slling 13135094Slling int 13145094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 13155094Slling { 13165094Slling zprop_list_t *entry; 13175094Slling zprop_list_t **last; 13185094Slling expand_data_t exp; 13195094Slling 13205094Slling if (*plp == NULL) { 13215094Slling /* 13225094Slling * If this is the very first time we've been called for an 'all' 13235094Slling * specification, expand the list to include all native 13245094Slling * properties. 13255094Slling */ 13265094Slling last = plp; 13275094Slling 13285094Slling exp.last = last; 13295094Slling exp.hdl = hdl; 13305094Slling exp.type = type; 13315094Slling 13325094Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 13335094Slling B_FALSE, type) == ZPROP_INVAL) 13345094Slling return (-1); 13355094Slling 13365094Slling /* 13375094Slling * Add 'name' to the beginning of the list, which is handled 13385094Slling * specially. 13395094Slling */ 13405094Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 13415094Slling return (-1); 13425094Slling 13435094Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 13445094Slling ZFS_PROP_NAME; 13455094Slling entry->pl_width = zprop_width(entry->pl_prop, 13465094Slling &entry->pl_fixed, type); 13475094Slling entry->pl_all = B_TRUE; 13485094Slling entry->pl_next = *plp; 13495094Slling *plp = entry; 13505094Slling } 13515094Slling return (0); 13525094Slling } 13535094Slling 13545094Slling int 13555094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 13565094Slling zfs_type_t type) 13575094Slling { 13585094Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 13595094Slling } 1360