1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 221195e687SMark J Musante * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens /* 27fa9e4066Sahrens * Internal utility routines for the ZFS library. 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens #include <errno.h> 31fa9e4066Sahrens #include <fcntl.h> 32fa9e4066Sahrens #include <libintl.h> 33fa9e4066Sahrens #include <stdarg.h> 34fa9e4066Sahrens #include <stdio.h> 35fa9e4066Sahrens #include <stdlib.h> 36fa9e4066Sahrens #include <strings.h> 37fa9e4066Sahrens #include <unistd.h> 38990b4856Slling #include <ctype.h> 39990b4856Slling #include <math.h> 40fa9e4066Sahrens #include <sys/mnttab.h> 415aba80dbSck #include <sys/mntent.h> 425aba80dbSck #include <sys/types.h> 43fa9e4066Sahrens 44fa9e4066Sahrens #include <libzfs.h> 45fa9e4066Sahrens 46fa9e4066Sahrens #include "libzfs_impl.h" 4791ebeef5Sahrens #include "zfs_prop.h" 48fa9e4066Sahrens 4999653d4eSeschrock int 5099653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl) 5199653d4eSeschrock { 5299653d4eSeschrock return (hdl->libzfs_error); 5399653d4eSeschrock } 54fa9e4066Sahrens 5599653d4eSeschrock const char * 5699653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl) 5799653d4eSeschrock { 5899653d4eSeschrock return (hdl->libzfs_action); 5999653d4eSeschrock } 60fa9e4066Sahrens 6199653d4eSeschrock const char * 6299653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl) 6399653d4eSeschrock { 6499653d4eSeschrock if (hdl->libzfs_desc[0] != '\0') 6599653d4eSeschrock return (hdl->libzfs_desc); 6699653d4eSeschrock 6799653d4eSeschrock switch (hdl->libzfs_error) { 6899653d4eSeschrock case EZFS_NOMEM: 6999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of memory")); 7099653d4eSeschrock case EZFS_BADPROP: 7199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid property value")); 7299653d4eSeschrock case EZFS_PROPREADONLY: 7399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "read only property")); 7499653d4eSeschrock case EZFS_PROPTYPE: 7599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 7699653d4eSeschrock "datasets of this type")); 7799653d4eSeschrock case EZFS_PROPNONINHERIT: 7899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 7999653d4eSeschrock case EZFS_PROPSPACE: 8099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 8199653d4eSeschrock case EZFS_BADTYPE: 8299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation not applicable to " 8399653d4eSeschrock "datasets of this type")); 8499653d4eSeschrock case EZFS_BUSY: 8599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 8699653d4eSeschrock case EZFS_EXISTS: 8799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 8899653d4eSeschrock case EZFS_NOENT: 8999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 9099653d4eSeschrock case EZFS_BADSTREAM: 9199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 9299653d4eSeschrock case EZFS_DSREADONLY: 9399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "dataset is read only")); 9499653d4eSeschrock case EZFS_VOLTOOBIG: 9599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 9699653d4eSeschrock "this system")); 9799653d4eSeschrock case EZFS_INVALIDNAME: 9899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 9999653d4eSeschrock case EZFS_BADRESTORE: 10099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 10199653d4eSeschrock "destination")); 10299653d4eSeschrock case EZFS_BADBACKUP: 10399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 10499653d4eSeschrock case EZFS_BADTARGET: 10599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 10699653d4eSeschrock case EZFS_NODEVICE: 10799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 10899653d4eSeschrock case EZFS_BADDEV: 10999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 11099653d4eSeschrock case EZFS_NOREPLICAS: 11199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 11299653d4eSeschrock case EZFS_RESILVERING: 11399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 11499653d4eSeschrock case EZFS_BADVERSION: 11599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unsupported version")); 11699653d4eSeschrock case EZFS_POOLUNAVAIL: 11799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 11899653d4eSeschrock case EZFS_DEVOVERFLOW: 11999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 12099653d4eSeschrock case EZFS_BADPATH: 12199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 12299653d4eSeschrock case EZFS_CROSSTARGET: 12399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 12499653d4eSeschrock "pools")); 12599653d4eSeschrock case EZFS_ZONED: 12699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 12799653d4eSeschrock case EZFS_MOUNTFAILED: 12899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 12999653d4eSeschrock case EZFS_UMOUNTFAILED: 13099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 131f3861e1aSahl case EZFS_UNSHARENFSFAILED: 13299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 133f3861e1aSahl case EZFS_SHARENFSFAILED: 13499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 135da6c28aaSamw case EZFS_UNSHARESMBFAILED: 136da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 137da6c28aaSamw case EZFS_SHARESMBFAILED: 138da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb add share failed")); 139ecd6cf80Smarks case EZFS_ISCSISVCUNAVAIL: 140ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, 141ecd6cf80Smarks "iscsitgt service need to be enabled by " 142ecd6cf80Smarks "a privileged user")); 14399653d4eSeschrock case EZFS_PERM: 14499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 14599653d4eSeschrock case EZFS_NOSPC: 14699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of space")); 147*6e27f868SSam Falkner case EZFS_FAULT: 148*6e27f868SSam Falkner return (dgettext(TEXT_DOMAIN, "bad address")); 14999653d4eSeschrock case EZFS_IO: 15099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 15199653d4eSeschrock case EZFS_INTR: 15299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "signal received")); 15399653d4eSeschrock case EZFS_ISSPARE: 15499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 15599653d4eSeschrock "spare")); 15699653d4eSeschrock case EZFS_INVALCONFIG: 15799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1583bb79becSeschrock case EZFS_RECURSIVE: 1593bb79becSeschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 16006eeb2adSek case EZFS_NOHISTORY: 16106eeb2adSek return (dgettext(TEXT_DOMAIN, "no history available")); 162f3861e1aSahl case EZFS_UNSHAREISCSIFAILED: 163f3861e1aSahl return (dgettext(TEXT_DOMAIN, 164f3861e1aSahl "iscsitgtd failed request to unshare")); 165f3861e1aSahl case EZFS_SHAREISCSIFAILED: 166f3861e1aSahl return (dgettext(TEXT_DOMAIN, 167f3861e1aSahl "iscsitgtd failed request to share")); 168b1b8ab34Slling case EZFS_POOLPROPS: 169b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 170b1b8ab34Slling "pool properties")); 171b1b8ab34Slling case EZFS_POOL_NOTSUP: 172b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 173b1b8ab34Slling "on this type of pool")); 174b1b8ab34Slling case EZFS_POOL_INVALARG: 175b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 176b1b8ab34Slling "this pool operation")); 177b7661cccSmmusante case EZFS_NAMETOOLONG: 178b7661cccSmmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 1798488aeb5Staylor case EZFS_OPENFAILED: 1808488aeb5Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 1818488aeb5Staylor case EZFS_NOCAP: 1828488aeb5Staylor return (dgettext(TEXT_DOMAIN, 1838488aeb5Staylor "disk capacity information could not be retrieved")); 1848488aeb5Staylor case EZFS_LABELFAILED: 1858488aeb5Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 186ecd6cf80Smarks case EZFS_BADWHO: 187ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid user/group")); 188ecd6cf80Smarks case EZFS_BADPERM: 189ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission")); 190ecd6cf80Smarks case EZFS_BADPERMSET: 191ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 192ecd6cf80Smarks case EZFS_NODELEGATION: 193ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "delegated administration is " 194ecd6cf80Smarks "disabled on pool")); 195ecd6cf80Smarks case EZFS_PERMRDONLY: 196ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be" 197ecd6cf80Smarks " modified")); 1982f8aaab3Seschrock case EZFS_BADCACHE: 1992f8aaab3Seschrock return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 200fa94a07fSbrendan case EZFS_ISL2CACHE: 201fa94a07fSbrendan return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 202e7cbe64fSgw case EZFS_VDEVNOTSUP: 203e7cbe64fSgw return (dgettext(TEXT_DOMAIN, "vdev specification is not " 204e7cbe64fSgw "supported")); 20515e6edf1Sgw case EZFS_NOTSUP: 20615e6edf1Sgw return (dgettext(TEXT_DOMAIN, "operation not supported " 20715e6edf1Sgw "on this dataset")); 20889a89ebfSlling case EZFS_ACTIVE_SPARE: 20989a89ebfSlling return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 21089a89ebfSlling "device")); 211e6ca193dSGeorge Wilson case EZFS_UNPLAYED_LOGS: 212e6ca193dSGeorge Wilson return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 213e6ca193dSGeorge Wilson "logs")); 214842727c2SChris Kirby case EZFS_REFTAG_RELE: 215842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 216842727c2SChris Kirby case EZFS_REFTAG_HOLD: 217842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "tag already exists on this " 218842727c2SChris Kirby "dataset")); 219ca45db41SChris Kirby case EZFS_TAGTOOLONG: 220ca45db41SChris Kirby return (dgettext(TEXT_DOMAIN, "tag too long")); 2219e69d7d0SLori Alt case EZFS_PIPEFAILED: 2229e69d7d0SLori Alt return (dgettext(TEXT_DOMAIN, "pipe create failed")); 2239e69d7d0SLori Alt case EZFS_THREADCREATEFAILED: 2249e69d7d0SLori Alt return (dgettext(TEXT_DOMAIN, "thread create failed")); 2251195e687SMark J Musante case EZFS_POSTSPLIT_ONLINE: 2261195e687SMark J Musante return (dgettext(TEXT_DOMAIN, "disk was split from this pool " 2271195e687SMark J Musante "into a new one")); 22899653d4eSeschrock case EZFS_UNKNOWN: 22999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 23099653d4eSeschrock default: 231c08432ebSeschrock assert(hdl->libzfs_error == 0); 232c08432ebSeschrock return (dgettext(TEXT_DOMAIN, "no error")); 23399653d4eSeschrock } 23499653d4eSeschrock } 23599653d4eSeschrock 23699653d4eSeschrock /*PRINTFLIKE2*/ 237fa9e4066Sahrens void 23899653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 239fa9e4066Sahrens { 240fa9e4066Sahrens va_list ap; 241fa9e4066Sahrens 242fa9e4066Sahrens va_start(ap, fmt); 243fa9e4066Sahrens 24499653d4eSeschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 24599653d4eSeschrock fmt, ap); 24699653d4eSeschrock hdl->libzfs_desc_active = 1; 24799653d4eSeschrock 24899653d4eSeschrock va_end(ap); 24999653d4eSeschrock } 25099653d4eSeschrock 25199653d4eSeschrock static void 25299653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 25399653d4eSeschrock { 25499653d4eSeschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 25599653d4eSeschrock fmt, ap); 25699653d4eSeschrock hdl->libzfs_error = error; 25799653d4eSeschrock 25899653d4eSeschrock if (hdl->libzfs_desc_active) 25999653d4eSeschrock hdl->libzfs_desc_active = 0; 26099653d4eSeschrock else 26199653d4eSeschrock hdl->libzfs_desc[0] = '\0'; 26299653d4eSeschrock 26399653d4eSeschrock if (hdl->libzfs_printerr) { 26499653d4eSeschrock if (error == EZFS_UNKNOWN) { 26599653d4eSeschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 26699653d4eSeschrock "error: %s\n"), libzfs_error_description(hdl)); 26799653d4eSeschrock abort(); 26899653d4eSeschrock } 26999653d4eSeschrock 27099653d4eSeschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 271b1b8ab34Slling libzfs_error_description(hdl)); 27299653d4eSeschrock if (error == EZFS_NOMEM) 27399653d4eSeschrock exit(1); 274fa9e4066Sahrens } 27599653d4eSeschrock } 27699653d4eSeschrock 277ece3d9b3Slling int 278ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 279ece3d9b3Slling { 280ece3d9b3Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 281ece3d9b3Slling } 282ece3d9b3Slling 28399653d4eSeschrock /*PRINTFLIKE3*/ 28499653d4eSeschrock int 285ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 28699653d4eSeschrock { 28799653d4eSeschrock va_list ap; 28899653d4eSeschrock 28999653d4eSeschrock va_start(ap, fmt); 29099653d4eSeschrock 29199653d4eSeschrock zfs_verror(hdl, error, fmt, ap); 292fa9e4066Sahrens 293fa9e4066Sahrens va_end(ap); 29499653d4eSeschrock 29599653d4eSeschrock return (-1); 296fa9e4066Sahrens } 297fa9e4066Sahrens 29899653d4eSeschrock static int 29999653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 30099653d4eSeschrock va_list ap) 30199653d4eSeschrock { 30299653d4eSeschrock switch (error) { 30399653d4eSeschrock case EPERM: 30499653d4eSeschrock case EACCES: 30599653d4eSeschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 30699653d4eSeschrock return (-1); 30799653d4eSeschrock 308ecd6cf80Smarks case ECANCELED: 309ecd6cf80Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 310ecd6cf80Smarks return (-1); 311ecd6cf80Smarks 31299653d4eSeschrock case EIO: 31399653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 31499653d4eSeschrock return (-1); 31599653d4eSeschrock 316*6e27f868SSam Falkner case EFAULT: 317*6e27f868SSam Falkner zfs_verror(hdl, EZFS_FAULT, fmt, ap); 318*6e27f868SSam Falkner return (-1); 319*6e27f868SSam Falkner 32099653d4eSeschrock case EINTR: 32199653d4eSeschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 32299653d4eSeschrock return (-1); 32399653d4eSeschrock } 32499653d4eSeschrock 32599653d4eSeschrock return (0); 32699653d4eSeschrock } 32799653d4eSeschrock 328ece3d9b3Slling int 329ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 330ece3d9b3Slling { 331ece3d9b3Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 332ece3d9b3Slling } 333ece3d9b3Slling 33499653d4eSeschrock /*PRINTFLIKE3*/ 33599653d4eSeschrock int 336ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 337fa9e4066Sahrens { 338fa9e4066Sahrens va_list ap; 339fa9e4066Sahrens 340fa9e4066Sahrens va_start(ap, fmt); 341fa9e4066Sahrens 34299653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 34399653d4eSeschrock va_end(ap); 34499653d4eSeschrock return (-1); 345fa9e4066Sahrens } 346fa9e4066Sahrens 34799653d4eSeschrock switch (error) { 34899653d4eSeschrock case ENXIO: 34997d9e3a6Sck case ENODEV: 35099653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 35199653d4eSeschrock break; 35299653d4eSeschrock 35399653d4eSeschrock case ENOENT: 35499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35599653d4eSeschrock "dataset does not exist")); 35699653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 35799653d4eSeschrock break; 35899653d4eSeschrock 35999653d4eSeschrock case ENOSPC: 36099653d4eSeschrock case EDQUOT: 36199653d4eSeschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 36299653d4eSeschrock return (-1); 36399653d4eSeschrock 36499653d4eSeschrock case EEXIST: 36599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36699653d4eSeschrock "dataset already exists")); 36799653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 36899653d4eSeschrock break; 36999653d4eSeschrock 37099653d4eSeschrock case EBUSY: 37199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37299653d4eSeschrock "dataset is busy")); 37399653d4eSeschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 37499653d4eSeschrock break; 375ecd6cf80Smarks case EROFS: 376ecd6cf80Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 377ecd6cf80Smarks "snapshot permissions cannot be modified")); 378ecd6cf80Smarks zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 379ecd6cf80Smarks break; 380b7661cccSmmusante case ENAMETOOLONG: 381b7661cccSmmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 382b7661cccSmmusante break; 38340ff3960Sck case ENOTSUP: 38440ff3960Sck zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 38540ff3960Sck break; 38654d692b7SGeorge Wilson case EAGAIN: 38754d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38854d692b7SGeorge Wilson "pool I/O is currently suspended")); 38954d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 39054d692b7SGeorge Wilson break; 39199653d4eSeschrock default: 39292241e0bSTom Erickson zfs_error_aux(hdl, strerror(error)); 39399653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 39499653d4eSeschrock break; 39599653d4eSeschrock } 39699653d4eSeschrock 39799653d4eSeschrock va_end(ap); 39899653d4eSeschrock return (-1); 399fa9e4066Sahrens } 400fa9e4066Sahrens 401ece3d9b3Slling int 402ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 403ece3d9b3Slling { 404ece3d9b3Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 405ece3d9b3Slling } 406ece3d9b3Slling 40799653d4eSeschrock /*PRINTFLIKE3*/ 40899653d4eSeschrock int 409ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 410fa9e4066Sahrens { 41199653d4eSeschrock va_list ap; 41299653d4eSeschrock 41399653d4eSeschrock va_start(ap, fmt); 41499653d4eSeschrock 41599653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 41699653d4eSeschrock va_end(ap); 41799653d4eSeschrock return (-1); 41899653d4eSeschrock } 41999653d4eSeschrock 42099653d4eSeschrock switch (error) { 42199653d4eSeschrock case ENODEV: 42299653d4eSeschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 42399653d4eSeschrock break; 42499653d4eSeschrock 42599653d4eSeschrock case ENOENT: 426b1b8ab34Slling zfs_error_aux(hdl, 427b1b8ab34Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 42899653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 42999653d4eSeschrock break; 43099653d4eSeschrock 43199653d4eSeschrock case EEXIST: 43299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43399653d4eSeschrock "pool already exists")); 43499653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 43599653d4eSeschrock break; 43699653d4eSeschrock 43799653d4eSeschrock case EBUSY: 43899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 43943afaaa8SEric Schrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 44099653d4eSeschrock break; 44199653d4eSeschrock 44299653d4eSeschrock case ENXIO: 44399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44499653d4eSeschrock "one or more devices is currently unavailable")); 44599653d4eSeschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 44699653d4eSeschrock break; 44799653d4eSeschrock 44899653d4eSeschrock case ENAMETOOLONG: 44999653d4eSeschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 45099653d4eSeschrock break; 45199653d4eSeschrock 452b1b8ab34Slling case ENOTSUP: 453b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 454b1b8ab34Slling break; 455b1b8ab34Slling 456b1b8ab34Slling case EINVAL: 457b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 458b1b8ab34Slling break; 459b1b8ab34Slling 460ecd6cf80Smarks case ENOSPC: 461ecd6cf80Smarks case EDQUOT: 462ecd6cf80Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 463ecd6cf80Smarks return (-1); 46454d692b7SGeorge Wilson case EAGAIN: 46554d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46654d692b7SGeorge Wilson "pool I/O is currently suspended")); 46754d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 46854d692b7SGeorge Wilson break; 469ecd6cf80Smarks 47099653d4eSeschrock default: 47199653d4eSeschrock zfs_error_aux(hdl, strerror(error)); 47299653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 47399653d4eSeschrock } 47499653d4eSeschrock 47599653d4eSeschrock va_end(ap); 47699653d4eSeschrock return (-1); 477fa9e4066Sahrens } 478fa9e4066Sahrens 479fa9e4066Sahrens /* 480fa9e4066Sahrens * Display an out of memory error message and abort the current program. 481fa9e4066Sahrens */ 48299653d4eSeschrock int 48399653d4eSeschrock no_memory(libzfs_handle_t *hdl) 484fa9e4066Sahrens { 48599653d4eSeschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 486fa9e4066Sahrens } 487fa9e4066Sahrens 488fa9e4066Sahrens /* 489fa9e4066Sahrens * A safe form of malloc() which will die if the allocation fails. 490fa9e4066Sahrens */ 491fa9e4066Sahrens void * 49299653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 493fa9e4066Sahrens { 494fa9e4066Sahrens void *data; 495fa9e4066Sahrens 496fa9e4066Sahrens if ((data = calloc(1, size)) == NULL) 49799653d4eSeschrock (void) no_memory(hdl); 498fa9e4066Sahrens 499fa9e4066Sahrens return (data); 500fa9e4066Sahrens } 501fa9e4066Sahrens 502e9dbad6fSeschrock /* 503e9dbad6fSeschrock * A safe form of realloc(), which also zeroes newly allocated space. 504e9dbad6fSeschrock */ 505e9dbad6fSeschrock void * 506e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 507e9dbad6fSeschrock { 508e9dbad6fSeschrock void *ret; 509e9dbad6fSeschrock 510e9dbad6fSeschrock if ((ret = realloc(ptr, newsize)) == NULL) { 511e9dbad6fSeschrock (void) no_memory(hdl); 512e9dbad6fSeschrock return (NULL); 513e9dbad6fSeschrock } 514e9dbad6fSeschrock 515e9dbad6fSeschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 516e9dbad6fSeschrock return (ret); 517e9dbad6fSeschrock } 518e9dbad6fSeschrock 519fa9e4066Sahrens /* 520fa9e4066Sahrens * A safe form of strdup() which will die if the allocation fails. 521fa9e4066Sahrens */ 522fa9e4066Sahrens char * 52399653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 524fa9e4066Sahrens { 525fa9e4066Sahrens char *ret; 526fa9e4066Sahrens 527fa9e4066Sahrens if ((ret = strdup(str)) == NULL) 52899653d4eSeschrock (void) no_memory(hdl); 529fa9e4066Sahrens 530fa9e4066Sahrens return (ret); 531fa9e4066Sahrens } 532fa9e4066Sahrens 533fa9e4066Sahrens /* 534fa9e4066Sahrens * Convert a number to an appropriately human-readable output. 535fa9e4066Sahrens */ 536fa9e4066Sahrens void 537fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 538fa9e4066Sahrens { 539fa9e4066Sahrens uint64_t n = num; 540fa9e4066Sahrens int index = 0; 541fa9e4066Sahrens char u; 542fa9e4066Sahrens 543fa9e4066Sahrens while (n >= 1024) { 5445c709891Seschrock n /= 1024; 545fa9e4066Sahrens index++; 546fa9e4066Sahrens } 547fa9e4066Sahrens 548fa9e4066Sahrens u = " KMGTPE"[index]; 549fa9e4066Sahrens 5505c709891Seschrock if (index == 0) { 551fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu", n); 5525c709891Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5535c709891Seschrock /* 5545c709891Seschrock * If this is an even multiple of the base, always display 5555c709891Seschrock * without any decimal precision. 5565c709891Seschrock */ 557fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5585c709891Seschrock } else { 5595c709891Seschrock /* 5605c709891Seschrock * We want to choose a precision that reflects the best choice 5615c709891Seschrock * for fitting in 5 characters. This can get rather tricky when 5625c709891Seschrock * we have numbers that are very close to an order of magnitude. 5635c709891Seschrock * For example, when displaying 10239 (which is really 9.999K), 5645c709891Seschrock * we want only a single place of precision for 10.0K. We could 5655c709891Seschrock * develop some complex heuristics for this, but it's much 5665c709891Seschrock * easier just to try each combination in turn. 5675c709891Seschrock */ 5685c709891Seschrock int i; 5695c709891Seschrock for (i = 2; i >= 0; i--) { 5703d7072f8Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5713d7072f8Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5725c709891Seschrock break; 5735c709891Seschrock } 5745c709891Seschrock } 575fa9e4066Sahrens } 57699653d4eSeschrock 57799653d4eSeschrock void 57899653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 57999653d4eSeschrock { 58099653d4eSeschrock hdl->libzfs_printerr = printerr; 58199653d4eSeschrock } 58299653d4eSeschrock 58399653d4eSeschrock libzfs_handle_t * 58499653d4eSeschrock libzfs_init(void) 58599653d4eSeschrock { 58699653d4eSeschrock libzfs_handle_t *hdl; 58799653d4eSeschrock 58899653d4eSeschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 58999653d4eSeschrock return (NULL); 59099653d4eSeschrock } 59199653d4eSeschrock 592c08432ebSeschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 59399653d4eSeschrock free(hdl); 59499653d4eSeschrock return (NULL); 59599653d4eSeschrock } 59699653d4eSeschrock 59799653d4eSeschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 59899653d4eSeschrock (void) close(hdl->libzfs_fd); 59999653d4eSeschrock free(hdl); 60099653d4eSeschrock return (NULL); 60199653d4eSeschrock } 60299653d4eSeschrock 60399653d4eSeschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 60499653d4eSeschrock 60591ebeef5Sahrens zfs_prop_init(); 606990b4856Slling zpool_prop_init(); 607b2634b9cSEric Taylor libzfs_mnttab_init(hdl); 60891ebeef5Sahrens 60999653d4eSeschrock return (hdl); 61099653d4eSeschrock } 61199653d4eSeschrock 61299653d4eSeschrock void 61399653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl) 61499653d4eSeschrock { 61599653d4eSeschrock (void) close(hdl->libzfs_fd); 61699653d4eSeschrock if (hdl->libzfs_mnttab) 61799653d4eSeschrock (void) fclose(hdl->libzfs_mnttab); 61899653d4eSeschrock if (hdl->libzfs_sharetab) 61999653d4eSeschrock (void) fclose(hdl->libzfs_sharetab); 62067331909Sdougm zfs_uninit_libshare(hdl); 621ecd6cf80Smarks if (hdl->libzfs_log_str) 622ecd6cf80Smarks (void) free(hdl->libzfs_log_str); 62329ab75c9Srm zpool_free_handles(hdl); 624069f55e2SEric Schrock libzfs_fru_clear(hdl, B_TRUE); 62599653d4eSeschrock namespace_clear(hdl); 626b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 62799653d4eSeschrock free(hdl); 62899653d4eSeschrock } 62999653d4eSeschrock 63099653d4eSeschrock libzfs_handle_t * 63199653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp) 63299653d4eSeschrock { 63399653d4eSeschrock return (zhp->zpool_hdl); 63499653d4eSeschrock } 63599653d4eSeschrock 63699653d4eSeschrock libzfs_handle_t * 63799653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp) 63899653d4eSeschrock { 63999653d4eSeschrock return (zhp->zfs_hdl); 64099653d4eSeschrock } 641e9dbad6fSeschrock 642d5b5bb25SRich Morris zpool_handle_t * 643d5b5bb25SRich Morris zfs_get_pool_handle(const zfs_handle_t *zhp) 644d5b5bb25SRich Morris { 645d5b5bb25SRich Morris return (zhp->zpool_hdl); 646d5b5bb25SRich Morris } 647d5b5bb25SRich Morris 6485aba80dbSck /* 6495aba80dbSck * Given a name, determine whether or not it's a valid path 6505aba80dbSck * (starts with '/' or "./"). If so, walk the mnttab trying 6515aba80dbSck * to match the device number. If not, treat the path as an 6525aba80dbSck * fs/vol/snap name. 6535aba80dbSck */ 6545aba80dbSck zfs_handle_t * 6555aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6565aba80dbSck { 6575aba80dbSck struct stat64 statbuf; 6585aba80dbSck struct extmnttab entry; 6595aba80dbSck int ret; 6605aba80dbSck 6615aba80dbSck if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6625aba80dbSck /* 6635aba80dbSck * It's not a valid path, assume it's a name of type 'argtype'. 6645aba80dbSck */ 6655aba80dbSck return (zfs_open(hdl, path, argtype)); 6665aba80dbSck } 6675aba80dbSck 6685aba80dbSck if (stat64(path, &statbuf) != 0) { 6695aba80dbSck (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6705aba80dbSck return (NULL); 6715aba80dbSck } 6725aba80dbSck 6735aba80dbSck rewind(hdl->libzfs_mnttab); 6745aba80dbSck while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6755aba80dbSck if (makedevice(entry.mnt_major, entry.mnt_minor) == 6765aba80dbSck statbuf.st_dev) { 6775aba80dbSck break; 6785aba80dbSck } 6795aba80dbSck } 6805aba80dbSck if (ret != 0) { 6815aba80dbSck return (NULL); 6825aba80dbSck } 6835aba80dbSck 6845aba80dbSck if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6855aba80dbSck (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 6865aba80dbSck path); 6875aba80dbSck return (NULL); 6885aba80dbSck } 6895aba80dbSck 6905aba80dbSck return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6915aba80dbSck } 6925aba80dbSck 693e9dbad6fSeschrock /* 694e9dbad6fSeschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 695e9dbad6fSeschrock * an ioctl(). 696e9dbad6fSeschrock */ 697e9dbad6fSeschrock int 698e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 699e9dbad6fSeschrock { 700e9dbad6fSeschrock if (len == 0) 7019966ca11SMatthew Ahrens len = 4*1024; 702e9dbad6fSeschrock zc->zc_nvlist_dst_size = len; 703e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 704e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 705e9dbad6fSeschrock return (-1); 706e9dbad6fSeschrock 707e9dbad6fSeschrock return (0); 708e9dbad6fSeschrock } 709e9dbad6fSeschrock 710e9dbad6fSeschrock /* 711e9dbad6fSeschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 712e9dbad6fSeschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 713e9dbad6fSeschrock * filled in by the kernel to indicate the actual required size. 714e9dbad6fSeschrock */ 715e9dbad6fSeschrock int 716e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 717e9dbad6fSeschrock { 718e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 719e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 720e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 721e9dbad6fSeschrock == NULL) 722e9dbad6fSeschrock return (-1); 723e9dbad6fSeschrock 724e9dbad6fSeschrock return (0); 725e9dbad6fSeschrock } 726e9dbad6fSeschrock 727e9dbad6fSeschrock /* 728a2eea2e1Sahrens * Called to free the src and dst nvlists stored in the command structure. 729e9dbad6fSeschrock */ 730e9dbad6fSeschrock void 731e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc) 732e9dbad6fSeschrock { 733990b4856Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 734e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 735e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 736e9dbad6fSeschrock } 737e9dbad6fSeschrock 738990b4856Slling static int 739990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 740990b4856Slling nvlist_t *nvl) 741e9dbad6fSeschrock { 742e9dbad6fSeschrock char *packed; 743e9dbad6fSeschrock size_t len; 744e9dbad6fSeschrock 745e9dbad6fSeschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 746e9dbad6fSeschrock 747e9dbad6fSeschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 748e9dbad6fSeschrock return (-1); 749e9dbad6fSeschrock 750e9dbad6fSeschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 751e9dbad6fSeschrock 752990b4856Slling *outnv = (uint64_t)(uintptr_t)packed; 753990b4856Slling *outlen = len; 754e9dbad6fSeschrock 755e9dbad6fSeschrock return (0); 756e9dbad6fSeschrock } 757e9dbad6fSeschrock 758990b4856Slling int 759990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 760990b4856Slling { 761990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 762990b4856Slling &zc->zc_nvlist_conf_size, nvl)); 763990b4856Slling } 764990b4856Slling 765990b4856Slling int 766990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 767990b4856Slling { 768990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 769990b4856Slling &zc->zc_nvlist_src_size, nvl)); 770990b4856Slling } 771990b4856Slling 772e9dbad6fSeschrock /* 773e9dbad6fSeschrock * Unpacks an nvlist from the ZFS ioctl command structure. 774e9dbad6fSeschrock */ 775e9dbad6fSeschrock int 776e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 777e9dbad6fSeschrock { 778e9dbad6fSeschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 779e9dbad6fSeschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 780e9dbad6fSeschrock return (no_memory(hdl)); 781e9dbad6fSeschrock 782e9dbad6fSeschrock return (0); 783e9dbad6fSeschrock } 784b1b8ab34Slling 785990b4856Slling int 786990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 787990b4856Slling { 788990b4856Slling int error; 789990b4856Slling 790990b4856Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 791990b4856Slling error = ioctl(hdl->libzfs_fd, request, zc); 792990b4856Slling if (hdl->libzfs_log_str) { 793990b4856Slling free(hdl->libzfs_log_str); 794990b4856Slling hdl->libzfs_log_str = NULL; 795990b4856Slling } 796990b4856Slling zc->zc_history = 0; 797990b4856Slling 798990b4856Slling return (error); 799990b4856Slling } 800990b4856Slling 801990b4856Slling /* 802990b4856Slling * ================================================================ 803990b4856Slling * API shared by zfs and zpool property management 804990b4856Slling * ================================================================ 805990b4856Slling */ 806990b4856Slling 807b1b8ab34Slling static void 808990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 809b1b8ab34Slling { 810990b4856Slling zprop_list_t *pl = cbp->cb_proplist; 811b1b8ab34Slling int i; 812b1b8ab34Slling char *title; 813b1b8ab34Slling size_t len; 814b1b8ab34Slling 815b1b8ab34Slling cbp->cb_first = B_FALSE; 816b1b8ab34Slling if (cbp->cb_scripted) 817b1b8ab34Slling return; 818b1b8ab34Slling 819b1b8ab34Slling /* 820b1b8ab34Slling * Start with the length of the column headers. 821b1b8ab34Slling */ 822b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 823b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 824b1b8ab34Slling "PROPERTY")); 825b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 826b1b8ab34Slling "VALUE")); 82792241e0bSTom Erickson cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, 82892241e0bSTom Erickson "RECEIVED")); 829b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 830b1b8ab34Slling "SOURCE")); 831b1b8ab34Slling 832deb8317bSMark J Musante /* first property is always NAME */ 833deb8317bSMark J Musante assert(cbp->cb_proplist->pl_prop == 834deb8317bSMark J Musante ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 835deb8317bSMark J Musante 836b1b8ab34Slling /* 837b1b8ab34Slling * Go through and calculate the widths for each column. For the 838b1b8ab34Slling * 'source' column, we kludge it up by taking the worst-case scenario of 839b1b8ab34Slling * inheriting from the longest name. This is acceptable because in the 840b1b8ab34Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 841b1b8ab34Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 84292241e0bSTom Erickson * if the name of the property is much longer than any values we find. 843b1b8ab34Slling */ 844b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 845b1b8ab34Slling /* 846b1b8ab34Slling * 'PROPERTY' column 847b1b8ab34Slling */ 848990b4856Slling if (pl->pl_prop != ZPROP_INVAL) { 849990b4856Slling const char *propname = (type == ZFS_TYPE_POOL) ? 850990b4856Slling zpool_prop_to_name(pl->pl_prop) : 851990b4856Slling zfs_prop_to_name(pl->pl_prop); 852990b4856Slling 853990b4856Slling len = strlen(propname); 854b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 855b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 856b1b8ab34Slling } else { 857b1b8ab34Slling len = strlen(pl->pl_user_prop); 858b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 859b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 860b1b8ab34Slling } 861b1b8ab34Slling 862b1b8ab34Slling /* 863deb8317bSMark J Musante * 'VALUE' column. The first property is always the 'name' 864deb8317bSMark J Musante * property that was tacked on either by /sbin/zfs's 865deb8317bSMark J Musante * zfs_do_get() or when calling zprop_expand_list(), so we 866deb8317bSMark J Musante * ignore its width. If the user specified the name property 867deb8317bSMark J Musante * to display, then it will be later in the list in any case. 868b1b8ab34Slling */ 869deb8317bSMark J Musante if (pl != cbp->cb_proplist && 870b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 871b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 872b1b8ab34Slling 87392241e0bSTom Erickson /* 'RECEIVED' column. */ 87492241e0bSTom Erickson if (pl != cbp->cb_proplist && 87592241e0bSTom Erickson pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) 87692241e0bSTom Erickson cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; 87792241e0bSTom Erickson 878b1b8ab34Slling /* 879b1b8ab34Slling * 'NAME' and 'SOURCE' columns 880b1b8ab34Slling */ 881990b4856Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 882990b4856Slling ZFS_PROP_NAME) && 883b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 884b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 885b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 886b1b8ab34Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 887b1b8ab34Slling } 888b1b8ab34Slling } 889b1b8ab34Slling 890b1b8ab34Slling /* 891b1b8ab34Slling * Now go through and print the headers. 892b1b8ab34Slling */ 89392241e0bSTom Erickson for (i = 0; i < ZFS_GET_NCOLS; i++) { 894b1b8ab34Slling switch (cbp->cb_columns[i]) { 895b1b8ab34Slling case GET_COL_NAME: 896b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "NAME"); 897b1b8ab34Slling break; 898b1b8ab34Slling case GET_COL_PROPERTY: 899b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 900b1b8ab34Slling break; 901b1b8ab34Slling case GET_COL_VALUE: 902b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 903b1b8ab34Slling break; 90492241e0bSTom Erickson case GET_COL_RECVD: 90592241e0bSTom Erickson title = dgettext(TEXT_DOMAIN, "RECEIVED"); 90692241e0bSTom Erickson break; 907b1b8ab34Slling case GET_COL_SOURCE: 908b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 909b1b8ab34Slling break; 910b1b8ab34Slling default: 911b1b8ab34Slling title = NULL; 912b1b8ab34Slling } 913b1b8ab34Slling 914b1b8ab34Slling if (title != NULL) { 91592241e0bSTom Erickson if (i == (ZFS_GET_NCOLS - 1) || 91692241e0bSTom Erickson cbp->cb_columns[i + 1] == GET_COL_NONE) 917b1b8ab34Slling (void) printf("%s", title); 918b1b8ab34Slling else 919b1b8ab34Slling (void) printf("%-*s ", 920b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 921b1b8ab34Slling title); 922b1b8ab34Slling } 923b1b8ab34Slling } 924b1b8ab34Slling (void) printf("\n"); 925b1b8ab34Slling } 926b1b8ab34Slling 927b1b8ab34Slling /* 928b1b8ab34Slling * Display a single line of output, according to the settings in the callback 929b1b8ab34Slling * structure. 930b1b8ab34Slling */ 931b1b8ab34Slling void 932990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 933990b4856Slling const char *propname, const char *value, zprop_source_t sourcetype, 93492241e0bSTom Erickson const char *source, const char *recvd_value) 935b1b8ab34Slling { 936b1b8ab34Slling int i; 937b1b8ab34Slling const char *str; 938b1b8ab34Slling char buf[128]; 939b1b8ab34Slling 940b1b8ab34Slling /* 941b1b8ab34Slling * Ignore those source types that the user has chosen to ignore. 942b1b8ab34Slling */ 943b1b8ab34Slling if ((sourcetype & cbp->cb_sources) == 0) 944b1b8ab34Slling return; 945b1b8ab34Slling 946b1b8ab34Slling if (cbp->cb_first) 947990b4856Slling zprop_print_headers(cbp, cbp->cb_type); 948b1b8ab34Slling 94992241e0bSTom Erickson for (i = 0; i < ZFS_GET_NCOLS; i++) { 950b1b8ab34Slling switch (cbp->cb_columns[i]) { 951b1b8ab34Slling case GET_COL_NAME: 952b1b8ab34Slling str = name; 953b1b8ab34Slling break; 954b1b8ab34Slling 955b1b8ab34Slling case GET_COL_PROPERTY: 956b1b8ab34Slling str = propname; 957b1b8ab34Slling break; 958b1b8ab34Slling 959b1b8ab34Slling case GET_COL_VALUE: 960b1b8ab34Slling str = value; 961b1b8ab34Slling break; 962b1b8ab34Slling 963b1b8ab34Slling case GET_COL_SOURCE: 964b1b8ab34Slling switch (sourcetype) { 965990b4856Slling case ZPROP_SRC_NONE: 966b1b8ab34Slling str = "-"; 967b1b8ab34Slling break; 968b1b8ab34Slling 969990b4856Slling case ZPROP_SRC_DEFAULT: 970b1b8ab34Slling str = "default"; 971b1b8ab34Slling break; 972b1b8ab34Slling 973990b4856Slling case ZPROP_SRC_LOCAL: 974b1b8ab34Slling str = "local"; 975b1b8ab34Slling break; 976b1b8ab34Slling 977990b4856Slling case ZPROP_SRC_TEMPORARY: 978b1b8ab34Slling str = "temporary"; 979b1b8ab34Slling break; 980b1b8ab34Slling 981990b4856Slling case ZPROP_SRC_INHERITED: 982b1b8ab34Slling (void) snprintf(buf, sizeof (buf), 983b1b8ab34Slling "inherited from %s", source); 984b1b8ab34Slling str = buf; 985b1b8ab34Slling break; 98692241e0bSTom Erickson case ZPROP_SRC_RECEIVED: 98792241e0bSTom Erickson str = "received"; 98892241e0bSTom Erickson break; 989b1b8ab34Slling } 990b1b8ab34Slling break; 991b1b8ab34Slling 99292241e0bSTom Erickson case GET_COL_RECVD: 99392241e0bSTom Erickson str = (recvd_value == NULL ? "-" : recvd_value); 99492241e0bSTom Erickson break; 99592241e0bSTom Erickson 996b1b8ab34Slling default: 997b1b8ab34Slling continue; 998b1b8ab34Slling } 999b1b8ab34Slling 100092241e0bSTom Erickson if (cbp->cb_columns[i + 1] == GET_COL_NONE) 1001b1b8ab34Slling (void) printf("%s", str); 1002b1b8ab34Slling else if (cbp->cb_scripted) 1003b1b8ab34Slling (void) printf("%s\t", str); 1004b1b8ab34Slling else 1005b1b8ab34Slling (void) printf("%-*s ", 1006b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 1007b1b8ab34Slling str); 1008b1b8ab34Slling } 1009b1b8ab34Slling 1010b1b8ab34Slling (void) printf("\n"); 1011b1b8ab34Slling } 1012ecd6cf80Smarks 1013990b4856Slling /* 1014990b4856Slling * Given a numeric suffix, convert the value into a number of bits that the 1015990b4856Slling * resulting value must be shifted. 1016990b4856Slling */ 1017990b4856Slling static int 1018990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf) 1019990b4856Slling { 1020990b4856Slling const char *ends = "BKMGTPEZ"; 1021990b4856Slling int i; 1022990b4856Slling 1023990b4856Slling if (buf[0] == '\0') 1024990b4856Slling return (0); 1025990b4856Slling for (i = 0; i < strlen(ends); i++) { 1026990b4856Slling if (toupper(buf[0]) == ends[i]) 1027990b4856Slling break; 1028990b4856Slling } 1029990b4856Slling if (i == strlen(ends)) { 1030990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1031990b4856Slling "invalid numeric suffix '%s'"), buf); 1032990b4856Slling return (-1); 1033990b4856Slling } 1034990b4856Slling 1035990b4856Slling /* 1036990b4856Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 1037990b4856Slling * allow 'BB' - that's just weird. 1038990b4856Slling */ 1039990b4856Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 1040990b4856Slling toupper(buf[0]) != 'B')) 1041990b4856Slling return (10*i); 1042990b4856Slling 1043990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1044990b4856Slling "invalid numeric suffix '%s'"), buf); 1045990b4856Slling return (-1); 1046990b4856Slling } 1047990b4856Slling 1048990b4856Slling /* 1049990b4856Slling * Convert a string of the form '100G' into a real number. Used when setting 1050990b4856Slling * properties or creating a volume. 'buf' is used to place an extended error 1051990b4856Slling * message for the caller to use. 1052990b4856Slling */ 1053ecd6cf80Smarks int 1054990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1055ecd6cf80Smarks { 1056990b4856Slling char *end; 1057990b4856Slling int shift; 1058ecd6cf80Smarks 1059990b4856Slling *num = 0; 1060990b4856Slling 1061990b4856Slling /* Check to see if this looks like a number. */ 1062990b4856Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1063990b4856Slling if (hdl) 1064990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1065990b4856Slling "bad numeric value '%s'"), value); 1066990b4856Slling return (-1); 1067ecd6cf80Smarks } 1068ecd6cf80Smarks 1069069f55e2SEric Schrock /* Rely on strtoull() to process the numeric portion. */ 1070990b4856Slling errno = 0; 1071f67f35c3SEric Schrock *num = strtoull(value, &end, 10); 1072990b4856Slling 1073990b4856Slling /* 1074990b4856Slling * Check for ERANGE, which indicates that the value is too large to fit 1075990b4856Slling * in a 64-bit value. 1076990b4856Slling */ 1077990b4856Slling if (errno == ERANGE) { 1078990b4856Slling if (hdl) 1079990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1080990b4856Slling "numeric value is too large")); 1081990b4856Slling return (-1); 1082990b4856Slling } 1083990b4856Slling 1084990b4856Slling /* 1085990b4856Slling * If we have a decimal value, then do the computation with floating 1086990b4856Slling * point arithmetic. Otherwise, use standard arithmetic. 1087990b4856Slling */ 1088990b4856Slling if (*end == '.') { 1089990b4856Slling double fval = strtod(value, &end); 1090990b4856Slling 1091990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1092990b4856Slling return (-1); 1093990b4856Slling 1094990b4856Slling fval *= pow(2, shift); 1095990b4856Slling 1096990b4856Slling if (fval > UINT64_MAX) { 1097990b4856Slling if (hdl) 1098990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1099990b4856Slling "numeric value is too large")); 1100990b4856Slling return (-1); 1101990b4856Slling } 1102990b4856Slling 1103990b4856Slling *num = (uint64_t)fval; 1104990b4856Slling } else { 1105990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1106990b4856Slling return (-1); 1107990b4856Slling 1108990b4856Slling /* Check for overflow */ 1109990b4856Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 1110990b4856Slling if (hdl) 1111990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1112990b4856Slling "numeric value is too large")); 1113990b4856Slling return (-1); 1114990b4856Slling } 1115990b4856Slling 1116990b4856Slling *num <<= shift; 1117990b4856Slling } 1118990b4856Slling 1119990b4856Slling return (0); 1120990b4856Slling } 1121990b4856Slling 1122990b4856Slling /* 1123990b4856Slling * Given a propname=value nvpair to set, parse any numeric properties 1124990b4856Slling * (index, boolean, etc) if they are specified as strings and add the 1125990b4856Slling * resulting nvpair to the returned nvlist. 1126990b4856Slling * 1127990b4856Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 1128990b4856Slling * We want the user to be able to ignore this fact and specify properties 1129990b4856Slling * as native values (numbers, for example) or as strings (to simplify 1130990b4856Slling * command line utilities). This also handles converting index types 1131990b4856Slling * (compression, checksum, etc) from strings to their on-disk index. 1132990b4856Slling */ 1133990b4856Slling int 1134990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1135990b4856Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1136990b4856Slling const char *errbuf) 1137990b4856Slling { 1138990b4856Slling data_type_t datatype = nvpair_type(elem); 1139990b4856Slling zprop_type_t proptype; 1140990b4856Slling const char *propname; 1141990b4856Slling char *value; 1142990b4856Slling boolean_t isnone = B_FALSE; 1143990b4856Slling 1144990b4856Slling if (type == ZFS_TYPE_POOL) { 1145990b4856Slling proptype = zpool_prop_get_type(prop); 1146990b4856Slling propname = zpool_prop_to_name(prop); 1147990b4856Slling } else { 1148990b4856Slling proptype = zfs_prop_get_type(prop); 1149990b4856Slling propname = zfs_prop_to_name(prop); 1150990b4856Slling } 1151990b4856Slling 1152990b4856Slling /* 1153990b4856Slling * Convert any properties to the internal DSL value types. 1154990b4856Slling */ 1155990b4856Slling *svalp = NULL; 1156990b4856Slling *ivalp = 0; 1157990b4856Slling 1158990b4856Slling switch (proptype) { 1159990b4856Slling case PROP_TYPE_STRING: 1160990b4856Slling if (datatype != DATA_TYPE_STRING) { 1161990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1162990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1163990b4856Slling goto error; 1164990b4856Slling } 1165990b4856Slling (void) nvpair_value_string(elem, svalp); 1166990b4856Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1167990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1168990b4856Slling "'%s' is too long"), nvpair_name(elem)); 1169990b4856Slling goto error; 1170990b4856Slling } 1171990b4856Slling break; 1172990b4856Slling 1173990b4856Slling case PROP_TYPE_NUMBER: 1174990b4856Slling if (datatype == DATA_TYPE_STRING) { 1175990b4856Slling (void) nvpair_value_string(elem, &value); 1176990b4856Slling if (strcmp(value, "none") == 0) { 1177990b4856Slling isnone = B_TRUE; 1178990b4856Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 1179990b4856Slling != 0) { 1180990b4856Slling goto error; 1181990b4856Slling } 1182990b4856Slling } else if (datatype == DATA_TYPE_UINT64) { 1183990b4856Slling (void) nvpair_value_uint64(elem, ivalp); 1184990b4856Slling } else { 1185990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1186990b4856Slling "'%s' must be a number"), nvpair_name(elem)); 1187990b4856Slling goto error; 1188990b4856Slling } 1189990b4856Slling 1190990b4856Slling /* 1191990b4856Slling * Quota special: force 'none' and don't allow 0. 1192990b4856Slling */ 1193a9799022Sck if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1194a9799022Sck (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1195990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1196a9799022Sck "use 'none' to disable quota/refquota")); 1197990b4856Slling goto error; 1198990b4856Slling } 1199990b4856Slling break; 1200990b4856Slling 1201990b4856Slling case PROP_TYPE_INDEX: 1202a9799022Sck if (datatype != DATA_TYPE_STRING) { 1203990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1204990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1205990b4856Slling goto error; 1206990b4856Slling } 1207990b4856Slling 1208a9799022Sck (void) nvpair_value_string(elem, &value); 1209a9799022Sck 1210990b4856Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1211990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1212990b4856Slling "'%s' must be one of '%s'"), propname, 1213990b4856Slling zprop_values(prop, type)); 1214990b4856Slling goto error; 1215990b4856Slling } 1216990b4856Slling break; 1217990b4856Slling 1218990b4856Slling default: 1219990b4856Slling abort(); 1220990b4856Slling } 1221990b4856Slling 1222990b4856Slling /* 1223990b4856Slling * Add the result to our return set of properties. 1224990b4856Slling */ 1225990b4856Slling if (*svalp != NULL) { 1226990b4856Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 1227990b4856Slling (void) no_memory(hdl); 1228990b4856Slling return (-1); 1229990b4856Slling } 1230990b4856Slling } else { 1231990b4856Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1232990b4856Slling (void) no_memory(hdl); 1233990b4856Slling return (-1); 1234990b4856Slling } 1235990b4856Slling } 1236990b4856Slling 1237990b4856Slling return (0); 1238990b4856Slling error: 1239990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1240990b4856Slling return (-1); 1241990b4856Slling } 1242990b4856Slling 124374e7dc98SMatthew Ahrens static int 124474e7dc98SMatthew Ahrens addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 124574e7dc98SMatthew Ahrens zfs_type_t type) 124674e7dc98SMatthew Ahrens { 124774e7dc98SMatthew Ahrens int prop; 124874e7dc98SMatthew Ahrens zprop_list_t *entry; 124974e7dc98SMatthew Ahrens 125074e7dc98SMatthew Ahrens prop = zprop_name_to_prop(propname, type); 125174e7dc98SMatthew Ahrens 125274e7dc98SMatthew Ahrens if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 125374e7dc98SMatthew Ahrens prop = ZPROP_INVAL; 125474e7dc98SMatthew Ahrens 125574e7dc98SMatthew Ahrens /* 125674e7dc98SMatthew Ahrens * When no property table entry can be found, return failure if 125774e7dc98SMatthew Ahrens * this is a pool property or if this isn't a user-defined 125874e7dc98SMatthew Ahrens * dataset property, 125974e7dc98SMatthew Ahrens */ 126074e7dc98SMatthew Ahrens if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 126114843421SMatthew Ahrens (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) { 126274e7dc98SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 126374e7dc98SMatthew Ahrens "invalid property '%s'"), propname); 126474e7dc98SMatthew Ahrens return (zfs_error(hdl, EZFS_BADPROP, 126574e7dc98SMatthew Ahrens dgettext(TEXT_DOMAIN, "bad property list"))); 126674e7dc98SMatthew Ahrens } 126774e7dc98SMatthew Ahrens 126874e7dc98SMatthew Ahrens if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 126974e7dc98SMatthew Ahrens return (-1); 127074e7dc98SMatthew Ahrens 127174e7dc98SMatthew Ahrens entry->pl_prop = prop; 127274e7dc98SMatthew Ahrens if (prop == ZPROP_INVAL) { 127374e7dc98SMatthew Ahrens if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) { 127474e7dc98SMatthew Ahrens free(entry); 127574e7dc98SMatthew Ahrens return (-1); 127674e7dc98SMatthew Ahrens } 127774e7dc98SMatthew Ahrens entry->pl_width = strlen(propname); 127874e7dc98SMatthew Ahrens } else { 127974e7dc98SMatthew Ahrens entry->pl_width = zprop_width(prop, &entry->pl_fixed, 128074e7dc98SMatthew Ahrens type); 128174e7dc98SMatthew Ahrens } 128274e7dc98SMatthew Ahrens 128374e7dc98SMatthew Ahrens *listp = entry; 128474e7dc98SMatthew Ahrens 128574e7dc98SMatthew Ahrens return (0); 128674e7dc98SMatthew Ahrens } 128774e7dc98SMatthew Ahrens 1288990b4856Slling /* 1289990b4856Slling * Given a comma-separated list of properties, construct a property list 1290990b4856Slling * containing both user-defined and native properties. This function will 1291990b4856Slling * return a NULL list if 'all' is specified, which can later be expanded 1292990b4856Slling * by zprop_expand_list(). 1293990b4856Slling */ 1294990b4856Slling int 1295990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1296990b4856Slling zfs_type_t type) 1297990b4856Slling { 1298990b4856Slling *listp = NULL; 1299990b4856Slling 1300990b4856Slling /* 1301990b4856Slling * If 'all' is specified, return a NULL list. 1302990b4856Slling */ 1303990b4856Slling if (strcmp(props, "all") == 0) 1304990b4856Slling return (0); 1305990b4856Slling 1306990b4856Slling /* 1307990b4856Slling * If no props were specified, return an error. 1308990b4856Slling */ 1309990b4856Slling if (props[0] == '\0') { 1310990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1311990b4856Slling "no properties specified")); 1312990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1313990b4856Slling "bad property list"))); 1314990b4856Slling } 1315990b4856Slling 1316990b4856Slling /* 1317990b4856Slling * It would be nice to use getsubopt() here, but the inclusion of column 1318990b4856Slling * aliases makes this more effort than it's worth. 1319990b4856Slling */ 132074e7dc98SMatthew Ahrens while (*props != '\0') { 132174e7dc98SMatthew Ahrens size_t len; 132274e7dc98SMatthew Ahrens char *p; 132374e7dc98SMatthew Ahrens char c; 132474e7dc98SMatthew Ahrens 132574e7dc98SMatthew Ahrens if ((p = strchr(props, ',')) == NULL) { 132674e7dc98SMatthew Ahrens len = strlen(props); 132774e7dc98SMatthew Ahrens p = props + len; 1328990b4856Slling } else { 132974e7dc98SMatthew Ahrens len = p - props; 1330990b4856Slling } 1331990b4856Slling 1332990b4856Slling /* 1333990b4856Slling * Check for empty options. 1334990b4856Slling */ 1335990b4856Slling if (len == 0) { 1336990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1337990b4856Slling "empty property name")); 1338990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, 1339990b4856Slling dgettext(TEXT_DOMAIN, "bad property list"))); 1340990b4856Slling } 1341990b4856Slling 1342990b4856Slling /* 1343990b4856Slling * Check all regular property names. 1344990b4856Slling */ 134574e7dc98SMatthew Ahrens c = props[len]; 134674e7dc98SMatthew Ahrens props[len] = '\0'; 134774e7dc98SMatthew Ahrens 134874e7dc98SMatthew Ahrens if (strcmp(props, "space") == 0) { 134974e7dc98SMatthew Ahrens static char *spaceprops[] = { 135074e7dc98SMatthew Ahrens "name", "avail", "used", "usedbysnapshots", 135174e7dc98SMatthew Ahrens "usedbydataset", "usedbyrefreservation", 135274e7dc98SMatthew Ahrens "usedbychildren", NULL 135374e7dc98SMatthew Ahrens }; 135474e7dc98SMatthew Ahrens int i; 135574e7dc98SMatthew Ahrens 135674e7dc98SMatthew Ahrens for (i = 0; spaceprops[i]; i++) { 135774e7dc98SMatthew Ahrens if (addlist(hdl, spaceprops[i], listp, type)) 135874e7dc98SMatthew Ahrens return (-1); 135974e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1360990b4856Slling } 1361990b4856Slling } else { 136274e7dc98SMatthew Ahrens if (addlist(hdl, props, listp, type)) 136374e7dc98SMatthew Ahrens return (-1); 136474e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1365990b4856Slling } 1366990b4856Slling 136774e7dc98SMatthew Ahrens props = p; 1368990b4856Slling if (c == ',') 136974e7dc98SMatthew Ahrens props++; 1370990b4856Slling } 1371990b4856Slling 1372990b4856Slling return (0); 1373990b4856Slling } 1374990b4856Slling 1375990b4856Slling void 1376990b4856Slling zprop_free_list(zprop_list_t *pl) 1377990b4856Slling { 1378990b4856Slling zprop_list_t *next; 1379990b4856Slling 1380990b4856Slling while (pl != NULL) { 1381990b4856Slling next = pl->pl_next; 1382990b4856Slling free(pl->pl_user_prop); 1383990b4856Slling free(pl); 1384990b4856Slling pl = next; 1385990b4856Slling } 1386990b4856Slling } 1387990b4856Slling 1388990b4856Slling typedef struct expand_data { 1389990b4856Slling zprop_list_t **last; 1390990b4856Slling libzfs_handle_t *hdl; 1391990b4856Slling zfs_type_t type; 1392990b4856Slling } expand_data_t; 1393990b4856Slling 1394990b4856Slling int 1395990b4856Slling zprop_expand_list_cb(int prop, void *cb) 1396990b4856Slling { 1397990b4856Slling zprop_list_t *entry; 1398990b4856Slling expand_data_t *edp = cb; 1399990b4856Slling 1400990b4856Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1401990b4856Slling return (ZPROP_INVAL); 1402990b4856Slling 1403990b4856Slling entry->pl_prop = prop; 1404990b4856Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1405990b4856Slling entry->pl_all = B_TRUE; 1406990b4856Slling 1407990b4856Slling *(edp->last) = entry; 1408990b4856Slling edp->last = &entry->pl_next; 1409990b4856Slling 1410990b4856Slling return (ZPROP_CONT); 1411990b4856Slling } 1412990b4856Slling 1413990b4856Slling int 1414990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1415990b4856Slling { 1416990b4856Slling zprop_list_t *entry; 1417990b4856Slling zprop_list_t **last; 1418990b4856Slling expand_data_t exp; 1419990b4856Slling 1420990b4856Slling if (*plp == NULL) { 1421990b4856Slling /* 1422990b4856Slling * If this is the very first time we've been called for an 'all' 1423990b4856Slling * specification, expand the list to include all native 1424990b4856Slling * properties. 1425990b4856Slling */ 1426990b4856Slling last = plp; 1427990b4856Slling 1428990b4856Slling exp.last = last; 1429990b4856Slling exp.hdl = hdl; 1430990b4856Slling exp.type = type; 1431990b4856Slling 1432990b4856Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1433990b4856Slling B_FALSE, type) == ZPROP_INVAL) 1434990b4856Slling return (-1); 1435990b4856Slling 1436990b4856Slling /* 1437990b4856Slling * Add 'name' to the beginning of the list, which is handled 1438990b4856Slling * specially. 1439990b4856Slling */ 1440990b4856Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1441990b4856Slling return (-1); 1442990b4856Slling 1443990b4856Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1444990b4856Slling ZFS_PROP_NAME; 1445990b4856Slling entry->pl_width = zprop_width(entry->pl_prop, 1446990b4856Slling &entry->pl_fixed, type); 1447990b4856Slling entry->pl_all = B_TRUE; 1448990b4856Slling entry->pl_next = *plp; 1449990b4856Slling *plp = entry; 1450990b4856Slling } 1451990b4856Slling return (0); 1452990b4856Slling } 1453990b4856Slling 1454990b4856Slling int 1455990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1456990b4856Slling zfs_type_t type) 1457990b4856Slling { 1458990b4856Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 1459ecd6cf80Smarks } 1460