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 /* 22b2634b9cSEric Taylor * Copyright 2009 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> 415aba80dbSck153898 #include <sys/mntent.h> 425aba80dbSck153898 #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_VOLHASDATA: 9899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "volume has data")); 9999653d4eSeschrock case EZFS_INVALIDNAME: 10099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 10199653d4eSeschrock case EZFS_BADRESTORE: 10299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 10399653d4eSeschrock "destination")); 10499653d4eSeschrock case EZFS_BADBACKUP: 10599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 10699653d4eSeschrock case EZFS_BADTARGET: 10799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 10899653d4eSeschrock case EZFS_NODEVICE: 10999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 11099653d4eSeschrock case EZFS_BADDEV: 11199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 11299653d4eSeschrock case EZFS_NOREPLICAS: 11399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 11499653d4eSeschrock case EZFS_RESILVERING: 11599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 11699653d4eSeschrock case EZFS_BADVERSION: 11799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unsupported version")); 11899653d4eSeschrock case EZFS_POOLUNAVAIL: 11999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 12099653d4eSeschrock case EZFS_DEVOVERFLOW: 12199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 12299653d4eSeschrock case EZFS_BADPATH: 12399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 12499653d4eSeschrock case EZFS_CROSSTARGET: 12599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 12699653d4eSeschrock "pools")); 12799653d4eSeschrock case EZFS_ZONED: 12899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 12999653d4eSeschrock case EZFS_MOUNTFAILED: 13099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 13199653d4eSeschrock case EZFS_UMOUNTFAILED: 13299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 133f3861e1aSahl case EZFS_UNSHARENFSFAILED: 13499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 135f3861e1aSahl case EZFS_SHARENFSFAILED: 13699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 137da6c28aaSamw case EZFS_UNSHARESMBFAILED: 138da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 139da6c28aaSamw case EZFS_SHARESMBFAILED: 140da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb add share failed")); 141ecd6cf80Smarks case EZFS_ISCSISVCUNAVAIL: 142ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, 143ecd6cf80Smarks "iscsitgt service need to be enabled by " 144ecd6cf80Smarks "a privileged user")); 14599653d4eSeschrock case EZFS_DEVLINKS: 14699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "failed to create /dev links")); 14799653d4eSeschrock case EZFS_PERM: 14899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 14999653d4eSeschrock case EZFS_NOSPC: 15099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of space")); 15199653d4eSeschrock case EZFS_IO: 15299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 15399653d4eSeschrock case EZFS_INTR: 15499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "signal received")); 15599653d4eSeschrock case EZFS_ISSPARE: 15699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 15799653d4eSeschrock "spare")); 15899653d4eSeschrock case EZFS_INVALCONFIG: 15999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1603bb79becSeschrock case EZFS_RECURSIVE: 1613bb79becSeschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 16206eeb2adSek110237 case EZFS_NOHISTORY: 16306eeb2adSek110237 return (dgettext(TEXT_DOMAIN, "no history available")); 164f3861e1aSahl case EZFS_UNSHAREISCSIFAILED: 165f3861e1aSahl return (dgettext(TEXT_DOMAIN, 166f3861e1aSahl "iscsitgtd failed request to unshare")); 167f3861e1aSahl case EZFS_SHAREISCSIFAILED: 168f3861e1aSahl return (dgettext(TEXT_DOMAIN, 169f3861e1aSahl "iscsitgtd failed request to share")); 170b1b8ab34Slling case EZFS_POOLPROPS: 171b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 172b1b8ab34Slling "pool properties")); 173b1b8ab34Slling case EZFS_POOL_NOTSUP: 174b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 175b1b8ab34Slling "on this type of pool")); 176b1b8ab34Slling case EZFS_POOL_INVALARG: 177b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 178b1b8ab34Slling "this pool operation")); 179b7661cccSmmusante case EZFS_NAMETOOLONG: 180b7661cccSmmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 1818488aeb5Staylor case EZFS_OPENFAILED: 1828488aeb5Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 1838488aeb5Staylor case EZFS_NOCAP: 1848488aeb5Staylor return (dgettext(TEXT_DOMAIN, 1858488aeb5Staylor "disk capacity information could not be retrieved")); 1868488aeb5Staylor case EZFS_LABELFAILED: 1878488aeb5Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 188ecd6cf80Smarks case EZFS_BADWHO: 189ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid user/group")); 190ecd6cf80Smarks case EZFS_BADPERM: 191ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission")); 192ecd6cf80Smarks case EZFS_BADPERMSET: 193ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 194ecd6cf80Smarks case EZFS_NODELEGATION: 195ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "delegated administration is " 196ecd6cf80Smarks "disabled on pool")); 197ecd6cf80Smarks case EZFS_PERMRDONLY: 198ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be" 199ecd6cf80Smarks " modified")); 2002f8aaab3Seschrock case EZFS_BADCACHE: 2012f8aaab3Seschrock return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 202fa94a07fSbrendan case EZFS_ISL2CACHE: 203fa94a07fSbrendan return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 204e7cbe64fSgw25295 case EZFS_VDEVNOTSUP: 205e7cbe64fSgw25295 return (dgettext(TEXT_DOMAIN, "vdev specification is not " 206e7cbe64fSgw25295 "supported")); 20715e6edf1Sgw25295 case EZFS_NOTSUP: 20815e6edf1Sgw25295 return (dgettext(TEXT_DOMAIN, "operation not supported " 20915e6edf1Sgw25295 "on this dataset")); 21089a89ebfSlling case EZFS_ACTIVE_SPARE: 21189a89ebfSlling return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 21289a89ebfSlling "device")); 213e6ca193dSGeorge Wilson case EZFS_UNPLAYED_LOGS: 214e6ca193dSGeorge Wilson return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 215e6ca193dSGeorge Wilson "logs")); 216*842727c2SChris Kirby case EZFS_REFTAG_RELE: 217*842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 218*842727c2SChris Kirby case EZFS_REFTAG_HOLD: 219*842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "tag already exists on this " 220*842727c2SChris Kirby "dataset")); 22199653d4eSeschrock case EZFS_UNKNOWN: 22299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 22399653d4eSeschrock default: 224c08432ebSeschrock assert(hdl->libzfs_error == 0); 225c08432ebSeschrock return (dgettext(TEXT_DOMAIN, "no error")); 22699653d4eSeschrock } 22799653d4eSeschrock } 22899653d4eSeschrock 22999653d4eSeschrock /*PRINTFLIKE2*/ 230fa9e4066Sahrens void 23199653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 232fa9e4066Sahrens { 233fa9e4066Sahrens va_list ap; 234fa9e4066Sahrens 235fa9e4066Sahrens va_start(ap, fmt); 236fa9e4066Sahrens 23799653d4eSeschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 23899653d4eSeschrock fmt, ap); 23999653d4eSeschrock hdl->libzfs_desc_active = 1; 240fa9e4066Sahrens 241fa9e4066Sahrens va_end(ap); 242fa9e4066Sahrens } 243fa9e4066Sahrens 24499653d4eSeschrock static void 24599653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 246fa9e4066Sahrens { 24799653d4eSeschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 24899653d4eSeschrock fmt, ap); 24999653d4eSeschrock hdl->libzfs_error = error; 250fa9e4066Sahrens 25199653d4eSeschrock if (hdl->libzfs_desc_active) 25299653d4eSeschrock hdl->libzfs_desc_active = 0; 25399653d4eSeschrock else 25499653d4eSeschrock hdl->libzfs_desc[0] = '\0'; 255fa9e4066Sahrens 25699653d4eSeschrock if (hdl->libzfs_printerr) { 25799653d4eSeschrock if (error == EZFS_UNKNOWN) { 25899653d4eSeschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 25999653d4eSeschrock "error: %s\n"), libzfs_error_description(hdl)); 26099653d4eSeschrock abort(); 261fa9e4066Sahrens } 262fa9e4066Sahrens 26399653d4eSeschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 26499653d4eSeschrock libzfs_error_description(hdl)); 26599653d4eSeschrock if (error == EZFS_NOMEM) 266fa9e4066Sahrens exit(1); 267fa9e4066Sahrens } 26899653d4eSeschrock } 269fa9e4066Sahrens 270ece3d9b3Slling int 271ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 272ece3d9b3Slling { 273ece3d9b3Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 274ece3d9b3Slling } 275ece3d9b3Slling 27699653d4eSeschrock /*PRINTFLIKE3*/ 27799653d4eSeschrock int 278ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 279fa9e4066Sahrens { 28099653d4eSeschrock va_list ap; 28199653d4eSeschrock 28299653d4eSeschrock va_start(ap, fmt); 28399653d4eSeschrock 28499653d4eSeschrock zfs_verror(hdl, error, fmt, ap); 28599653d4eSeschrock 28699653d4eSeschrock va_end(ap); 28799653d4eSeschrock 28899653d4eSeschrock return (-1); 28999653d4eSeschrock } 29099653d4eSeschrock 29199653d4eSeschrock static int 29299653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 29399653d4eSeschrock va_list ap) 29499653d4eSeschrock { 29599653d4eSeschrock switch (error) { 29699653d4eSeschrock case EPERM: 29799653d4eSeschrock case EACCES: 29899653d4eSeschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 29999653d4eSeschrock return (-1); 30099653d4eSeschrock 301ecd6cf80Smarks case ECANCELED: 302ecd6cf80Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 303ecd6cf80Smarks return (-1); 304ecd6cf80Smarks 30599653d4eSeschrock case EIO: 30699653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 30799653d4eSeschrock return (-1); 30899653d4eSeschrock 30999653d4eSeschrock case EINTR: 31099653d4eSeschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 31199653d4eSeschrock return (-1); 31299653d4eSeschrock } 31399653d4eSeschrock 31499653d4eSeschrock return (0); 31599653d4eSeschrock } 31699653d4eSeschrock 317ece3d9b3Slling int 318ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 319ece3d9b3Slling { 320ece3d9b3Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 321ece3d9b3Slling } 322ece3d9b3Slling 32399653d4eSeschrock /*PRINTFLIKE3*/ 32499653d4eSeschrock int 325ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 32699653d4eSeschrock { 32799653d4eSeschrock va_list ap; 32899653d4eSeschrock 32999653d4eSeschrock va_start(ap, fmt); 33099653d4eSeschrock 33199653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 33299653d4eSeschrock va_end(ap); 33399653d4eSeschrock return (-1); 33499653d4eSeschrock } 33599653d4eSeschrock 33699653d4eSeschrock switch (error) { 33799653d4eSeschrock case ENXIO: 33897d9e3a6Sck153898 case ENODEV: 33999653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 34099653d4eSeschrock break; 34199653d4eSeschrock 34299653d4eSeschrock case ENOENT: 34399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34499653d4eSeschrock "dataset does not exist")); 34599653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 34699653d4eSeschrock break; 34799653d4eSeschrock 34899653d4eSeschrock case ENOSPC: 34999653d4eSeschrock case EDQUOT: 35099653d4eSeschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 35199653d4eSeschrock return (-1); 35299653d4eSeschrock 35399653d4eSeschrock case EEXIST: 35499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35599653d4eSeschrock "dataset already exists")); 35699653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 35799653d4eSeschrock break; 35899653d4eSeschrock 35999653d4eSeschrock case EBUSY: 36099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36199653d4eSeschrock "dataset is busy")); 36299653d4eSeschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 36399653d4eSeschrock break; 364ecd6cf80Smarks case EROFS: 365ecd6cf80Smarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 366ecd6cf80Smarks "snapshot permissions cannot be modified")); 367ecd6cf80Smarks zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 368ecd6cf80Smarks break; 369b7661cccSmmusante case ENAMETOOLONG: 370b7661cccSmmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 371b7661cccSmmusante break; 37240ff3960Sck153898 case ENOTSUP: 37340ff3960Sck153898 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 37440ff3960Sck153898 break; 37554d692b7SGeorge Wilson case EAGAIN: 37654d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37754d692b7SGeorge Wilson "pool I/O is currently suspended")); 37854d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 37954d692b7SGeorge Wilson break; 38099653d4eSeschrock default: 38199653d4eSeschrock zfs_error_aux(hdl, strerror(errno)); 38299653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 38399653d4eSeschrock break; 38499653d4eSeschrock } 38599653d4eSeschrock 38699653d4eSeschrock va_end(ap); 38799653d4eSeschrock return (-1); 38899653d4eSeschrock } 38999653d4eSeschrock 390ece3d9b3Slling int 391ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 392ece3d9b3Slling { 393ece3d9b3Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 394ece3d9b3Slling } 395ece3d9b3Slling 39699653d4eSeschrock /*PRINTFLIKE3*/ 39799653d4eSeschrock int 398ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 39999653d4eSeschrock { 40099653d4eSeschrock va_list ap; 40199653d4eSeschrock 40299653d4eSeschrock va_start(ap, fmt); 40399653d4eSeschrock 40499653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 40599653d4eSeschrock va_end(ap); 40699653d4eSeschrock return (-1); 40799653d4eSeschrock } 40899653d4eSeschrock 40999653d4eSeschrock switch (error) { 41099653d4eSeschrock case ENODEV: 41199653d4eSeschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 41299653d4eSeschrock break; 41399653d4eSeschrock 41499653d4eSeschrock case ENOENT: 415b1b8ab34Slling zfs_error_aux(hdl, 416b1b8ab34Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 41799653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 41899653d4eSeschrock break; 41999653d4eSeschrock 42099653d4eSeschrock case EEXIST: 42199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42299653d4eSeschrock "pool already exists")); 42399653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 42499653d4eSeschrock break; 42599653d4eSeschrock 42699653d4eSeschrock case EBUSY: 42799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 42843afaaa8SEric Schrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 42999653d4eSeschrock break; 43099653d4eSeschrock 43199653d4eSeschrock case ENXIO: 43299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43399653d4eSeschrock "one or more devices is currently unavailable")); 43499653d4eSeschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 43599653d4eSeschrock break; 43699653d4eSeschrock 43799653d4eSeschrock case ENAMETOOLONG: 43899653d4eSeschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 43999653d4eSeschrock break; 44099653d4eSeschrock 441b1b8ab34Slling case ENOTSUP: 442b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 443b1b8ab34Slling break; 444b1b8ab34Slling 445b1b8ab34Slling case EINVAL: 446b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 447b1b8ab34Slling break; 448b1b8ab34Slling 449ecd6cf80Smarks case ENOSPC: 450ecd6cf80Smarks case EDQUOT: 451ecd6cf80Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 452ecd6cf80Smarks return (-1); 45354d692b7SGeorge Wilson case EAGAIN: 45454d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45554d692b7SGeorge Wilson "pool I/O is currently suspended")); 45654d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 45754d692b7SGeorge Wilson break; 458ecd6cf80Smarks 45999653d4eSeschrock default: 46099653d4eSeschrock zfs_error_aux(hdl, strerror(error)); 46199653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 46299653d4eSeschrock } 46399653d4eSeschrock 46499653d4eSeschrock va_end(ap); 46599653d4eSeschrock return (-1); 466fa9e4066Sahrens } 467fa9e4066Sahrens 468fa9e4066Sahrens /* 469fa9e4066Sahrens * Display an out of memory error message and abort the current program. 470fa9e4066Sahrens */ 47199653d4eSeschrock int 47299653d4eSeschrock no_memory(libzfs_handle_t *hdl) 473fa9e4066Sahrens { 47499653d4eSeschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 475fa9e4066Sahrens } 476fa9e4066Sahrens 477fa9e4066Sahrens /* 478fa9e4066Sahrens * A safe form of malloc() which will die if the allocation fails. 479fa9e4066Sahrens */ 480fa9e4066Sahrens void * 48199653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 482fa9e4066Sahrens { 483fa9e4066Sahrens void *data; 484fa9e4066Sahrens 485fa9e4066Sahrens if ((data = calloc(1, size)) == NULL) 48699653d4eSeschrock (void) no_memory(hdl); 487fa9e4066Sahrens 488fa9e4066Sahrens return (data); 489fa9e4066Sahrens } 490fa9e4066Sahrens 491fa9e4066Sahrens /* 492e9dbad6fSeschrock * A safe form of realloc(), which also zeroes newly allocated space. 493e9dbad6fSeschrock */ 494e9dbad6fSeschrock void * 495e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 496e9dbad6fSeschrock { 497e9dbad6fSeschrock void *ret; 498e9dbad6fSeschrock 499e9dbad6fSeschrock if ((ret = realloc(ptr, newsize)) == NULL) { 500e9dbad6fSeschrock (void) no_memory(hdl); 501e9dbad6fSeschrock return (NULL); 502e9dbad6fSeschrock } 503e9dbad6fSeschrock 504e9dbad6fSeschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 505e9dbad6fSeschrock return (ret); 506e9dbad6fSeschrock } 507e9dbad6fSeschrock 508e9dbad6fSeschrock /* 509fa9e4066Sahrens * A safe form of strdup() which will die if the allocation fails. 510fa9e4066Sahrens */ 511fa9e4066Sahrens char * 51299653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 513fa9e4066Sahrens { 514fa9e4066Sahrens char *ret; 515fa9e4066Sahrens 516fa9e4066Sahrens if ((ret = strdup(str)) == NULL) 51799653d4eSeschrock (void) no_memory(hdl); 518fa9e4066Sahrens 519fa9e4066Sahrens return (ret); 520fa9e4066Sahrens } 521fa9e4066Sahrens 522fa9e4066Sahrens /* 523fa9e4066Sahrens * Convert a number to an appropriately human-readable output. 524fa9e4066Sahrens */ 525fa9e4066Sahrens void 526fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 527fa9e4066Sahrens { 528fa9e4066Sahrens uint64_t n = num; 529fa9e4066Sahrens int index = 0; 530fa9e4066Sahrens char u; 531fa9e4066Sahrens 532fa9e4066Sahrens while (n >= 1024) { 5335c709891Seschrock n /= 1024; 534fa9e4066Sahrens index++; 535fa9e4066Sahrens } 536fa9e4066Sahrens 537fa9e4066Sahrens u = " KMGTPE"[index]; 538fa9e4066Sahrens 5395c709891Seschrock if (index == 0) { 540fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu", n); 5415c709891Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5425c709891Seschrock /* 5435c709891Seschrock * If this is an even multiple of the base, always display 5445c709891Seschrock * without any decimal precision. 5455c709891Seschrock */ 546fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5475c709891Seschrock } else { 5485c709891Seschrock /* 5495c709891Seschrock * We want to choose a precision that reflects the best choice 5505c709891Seschrock * for fitting in 5 characters. This can get rather tricky when 5515c709891Seschrock * we have numbers that are very close to an order of magnitude. 5525c709891Seschrock * For example, when displaying 10239 (which is really 9.999K), 5535c709891Seschrock * we want only a single place of precision for 10.0K. We could 5545c709891Seschrock * develop some complex heuristics for this, but it's much 5555c709891Seschrock * easier just to try each combination in turn. 5565c709891Seschrock */ 5575c709891Seschrock int i; 5585c709891Seschrock for (i = 2; i >= 0; i--) { 5593d7072f8Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5603d7072f8Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5615c709891Seschrock break; 5625c709891Seschrock } 5635c709891Seschrock } 564fa9e4066Sahrens } 56599653d4eSeschrock 56699653d4eSeschrock void 56799653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 56899653d4eSeschrock { 56999653d4eSeschrock hdl->libzfs_printerr = printerr; 57099653d4eSeschrock } 57199653d4eSeschrock 57299653d4eSeschrock libzfs_handle_t * 57399653d4eSeschrock libzfs_init(void) 57499653d4eSeschrock { 57599653d4eSeschrock libzfs_handle_t *hdl; 57699653d4eSeschrock 57799653d4eSeschrock if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 57899653d4eSeschrock return (NULL); 57999653d4eSeschrock } 58099653d4eSeschrock 581c08432ebSeschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 58299653d4eSeschrock free(hdl); 58399653d4eSeschrock return (NULL); 58499653d4eSeschrock } 58599653d4eSeschrock 58699653d4eSeschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 58799653d4eSeschrock (void) close(hdl->libzfs_fd); 58899653d4eSeschrock free(hdl); 58999653d4eSeschrock return (NULL); 59099653d4eSeschrock } 59199653d4eSeschrock 59299653d4eSeschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 59399653d4eSeschrock 59491ebeef5Sahrens zfs_prop_init(); 595990b4856Slling zpool_prop_init(); 596b2634b9cSEric Taylor libzfs_mnttab_init(hdl); 59791ebeef5Sahrens 59899653d4eSeschrock return (hdl); 59999653d4eSeschrock } 60099653d4eSeschrock 60199653d4eSeschrock void 60299653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl) 60399653d4eSeschrock { 60499653d4eSeschrock (void) close(hdl->libzfs_fd); 60599653d4eSeschrock if (hdl->libzfs_mnttab) 60699653d4eSeschrock (void) fclose(hdl->libzfs_mnttab); 60799653d4eSeschrock if (hdl->libzfs_sharetab) 60899653d4eSeschrock (void) fclose(hdl->libzfs_sharetab); 60967331909Sdougm zfs_uninit_libshare(hdl); 610ecd6cf80Smarks if (hdl->libzfs_log_str) 611ecd6cf80Smarks (void) free(hdl->libzfs_log_str); 61229ab75c9Srm160521 zpool_free_handles(hdl); 61399653d4eSeschrock namespace_clear(hdl); 614b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 61599653d4eSeschrock free(hdl); 61699653d4eSeschrock } 61799653d4eSeschrock 61899653d4eSeschrock libzfs_handle_t * 61999653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp) 62099653d4eSeschrock { 62199653d4eSeschrock return (zhp->zpool_hdl); 62299653d4eSeschrock } 62399653d4eSeschrock 62499653d4eSeschrock libzfs_handle_t * 62599653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp) 62699653d4eSeschrock { 62799653d4eSeschrock return (zhp->zfs_hdl); 62899653d4eSeschrock } 629e9dbad6fSeschrock 630d5b5bb25SRich Morris zpool_handle_t * 631d5b5bb25SRich Morris zfs_get_pool_handle(const zfs_handle_t *zhp) 632d5b5bb25SRich Morris { 633d5b5bb25SRich Morris return (zhp->zpool_hdl); 634d5b5bb25SRich Morris } 635d5b5bb25SRich Morris 636e9dbad6fSeschrock /* 6375aba80dbSck153898 * Given a name, determine whether or not it's a valid path 6385aba80dbSck153898 * (starts with '/' or "./"). If so, walk the mnttab trying 6395aba80dbSck153898 * to match the device number. If not, treat the path as an 6405aba80dbSck153898 * fs/vol/snap name. 6415aba80dbSck153898 */ 6425aba80dbSck153898 zfs_handle_t * 6435aba80dbSck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6445aba80dbSck153898 { 6455aba80dbSck153898 struct stat64 statbuf; 6465aba80dbSck153898 struct extmnttab entry; 6475aba80dbSck153898 int ret; 6485aba80dbSck153898 6495aba80dbSck153898 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6505aba80dbSck153898 /* 6515aba80dbSck153898 * It's not a valid path, assume it's a name of type 'argtype'. 6525aba80dbSck153898 */ 6535aba80dbSck153898 return (zfs_open(hdl, path, argtype)); 6545aba80dbSck153898 } 6555aba80dbSck153898 6565aba80dbSck153898 if (stat64(path, &statbuf) != 0) { 6575aba80dbSck153898 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6585aba80dbSck153898 return (NULL); 6595aba80dbSck153898 } 6605aba80dbSck153898 6615aba80dbSck153898 rewind(hdl->libzfs_mnttab); 6625aba80dbSck153898 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6635aba80dbSck153898 if (makedevice(entry.mnt_major, entry.mnt_minor) == 6645aba80dbSck153898 statbuf.st_dev) { 6655aba80dbSck153898 break; 6665aba80dbSck153898 } 6675aba80dbSck153898 } 6685aba80dbSck153898 if (ret != 0) { 6695aba80dbSck153898 return (NULL); 6705aba80dbSck153898 } 6715aba80dbSck153898 6725aba80dbSck153898 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6735aba80dbSck153898 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 6745aba80dbSck153898 path); 6755aba80dbSck153898 return (NULL); 6765aba80dbSck153898 } 6775aba80dbSck153898 6785aba80dbSck153898 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 6795aba80dbSck153898 } 6805aba80dbSck153898 6815aba80dbSck153898 /* 682e9dbad6fSeschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 683e9dbad6fSeschrock * an ioctl(). 684e9dbad6fSeschrock */ 685e9dbad6fSeschrock int 686e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 687e9dbad6fSeschrock { 688e9dbad6fSeschrock if (len == 0) 689a2eea2e1Sahrens len = 2048; 690e9dbad6fSeschrock zc->zc_nvlist_dst_size = len; 691e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 692e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 693e9dbad6fSeschrock return (-1); 694e9dbad6fSeschrock 695e9dbad6fSeschrock return (0); 696e9dbad6fSeschrock } 697e9dbad6fSeschrock 698e9dbad6fSeschrock /* 699e9dbad6fSeschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 700e9dbad6fSeschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 701e9dbad6fSeschrock * filled in by the kernel to indicate the actual required size. 702e9dbad6fSeschrock */ 703e9dbad6fSeschrock int 704e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 705e9dbad6fSeschrock { 706e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 707e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 708e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 709e9dbad6fSeschrock == NULL) 710e9dbad6fSeschrock return (-1); 711e9dbad6fSeschrock 712e9dbad6fSeschrock return (0); 713e9dbad6fSeschrock } 714e9dbad6fSeschrock 715e9dbad6fSeschrock /* 716a2eea2e1Sahrens * Called to free the src and dst nvlists stored in the command structure. 717e9dbad6fSeschrock */ 718e9dbad6fSeschrock void 719e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc) 720e9dbad6fSeschrock { 721990b4856Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 722e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 723e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 724e9dbad6fSeschrock } 725e9dbad6fSeschrock 726990b4856Slling static int 727990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 728990b4856Slling nvlist_t *nvl) 729e9dbad6fSeschrock { 730e9dbad6fSeschrock char *packed; 731e9dbad6fSeschrock size_t len; 732e9dbad6fSeschrock 733e9dbad6fSeschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 734e9dbad6fSeschrock 735e9dbad6fSeschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 736e9dbad6fSeschrock return (-1); 737e9dbad6fSeschrock 738e9dbad6fSeschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 739e9dbad6fSeschrock 740990b4856Slling *outnv = (uint64_t)(uintptr_t)packed; 741990b4856Slling *outlen = len; 742e9dbad6fSeschrock 743e9dbad6fSeschrock return (0); 744e9dbad6fSeschrock } 745e9dbad6fSeschrock 746990b4856Slling int 747990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 748990b4856Slling { 749990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 750990b4856Slling &zc->zc_nvlist_conf_size, nvl)); 751990b4856Slling } 752990b4856Slling 753990b4856Slling int 754990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 755990b4856Slling { 756990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 757990b4856Slling &zc->zc_nvlist_src_size, nvl)); 758990b4856Slling } 759990b4856Slling 760e9dbad6fSeschrock /* 761e9dbad6fSeschrock * Unpacks an nvlist from the ZFS ioctl command structure. 762e9dbad6fSeschrock */ 763e9dbad6fSeschrock int 764e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 765e9dbad6fSeschrock { 766e9dbad6fSeschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 767e9dbad6fSeschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 768e9dbad6fSeschrock return (no_memory(hdl)); 769e9dbad6fSeschrock 770e9dbad6fSeschrock return (0); 771e9dbad6fSeschrock } 772b1b8ab34Slling 773990b4856Slling int 774990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 775b1b8ab34Slling { 776990b4856Slling int error; 777990b4856Slling 778990b4856Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 779990b4856Slling error = ioctl(hdl->libzfs_fd, request, zc); 780990b4856Slling if (hdl->libzfs_log_str) { 781990b4856Slling free(hdl->libzfs_log_str); 782990b4856Slling hdl->libzfs_log_str = NULL; 783990b4856Slling } 784990b4856Slling zc->zc_history = 0; 785990b4856Slling 786990b4856Slling return (error); 787990b4856Slling } 788990b4856Slling 789990b4856Slling /* 790990b4856Slling * ================================================================ 791990b4856Slling * API shared by zfs and zpool property management 792990b4856Slling * ================================================================ 793990b4856Slling */ 794990b4856Slling 795990b4856Slling static void 796990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 797990b4856Slling { 798990b4856Slling zprop_list_t *pl = cbp->cb_proplist; 799b1b8ab34Slling int i; 800b1b8ab34Slling char *title; 801b1b8ab34Slling size_t len; 802b1b8ab34Slling 803b1b8ab34Slling cbp->cb_first = B_FALSE; 804b1b8ab34Slling if (cbp->cb_scripted) 805b1b8ab34Slling return; 806b1b8ab34Slling 807b1b8ab34Slling /* 808b1b8ab34Slling * Start with the length of the column headers. 809b1b8ab34Slling */ 810b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 811b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 812b1b8ab34Slling "PROPERTY")); 813b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 814b1b8ab34Slling "VALUE")); 815b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 816b1b8ab34Slling "SOURCE")); 817b1b8ab34Slling 818deb8317bSMark J Musante /* first property is always NAME */ 819deb8317bSMark J Musante assert(cbp->cb_proplist->pl_prop == 820deb8317bSMark J Musante ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 821deb8317bSMark J Musante 822b1b8ab34Slling /* 823b1b8ab34Slling * Go through and calculate the widths for each column. For the 824b1b8ab34Slling * 'source' column, we kludge it up by taking the worst-case scenario of 825b1b8ab34Slling * inheriting from the longest name. This is acceptable because in the 826b1b8ab34Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 827b1b8ab34Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 828b1b8ab34Slling * if the name of the property is much longer the any values we find. 829b1b8ab34Slling */ 830b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 831b1b8ab34Slling /* 832b1b8ab34Slling * 'PROPERTY' column 833b1b8ab34Slling */ 834990b4856Slling if (pl->pl_prop != ZPROP_INVAL) { 835990b4856Slling const char *propname = (type == ZFS_TYPE_POOL) ? 836990b4856Slling zpool_prop_to_name(pl->pl_prop) : 837990b4856Slling zfs_prop_to_name(pl->pl_prop); 838990b4856Slling 839990b4856Slling len = strlen(propname); 840b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 841b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 842b1b8ab34Slling } else { 843b1b8ab34Slling len = strlen(pl->pl_user_prop); 844b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 845b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 846b1b8ab34Slling } 847b1b8ab34Slling 848b1b8ab34Slling /* 849deb8317bSMark J Musante * 'VALUE' column. The first property is always the 'name' 850deb8317bSMark J Musante * property that was tacked on either by /sbin/zfs's 851deb8317bSMark J Musante * zfs_do_get() or when calling zprop_expand_list(), so we 852deb8317bSMark J Musante * ignore its width. If the user specified the name property 853deb8317bSMark J Musante * to display, then it will be later in the list in any case. 854b1b8ab34Slling */ 855deb8317bSMark J Musante if (pl != cbp->cb_proplist && 856b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 857b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 858b1b8ab34Slling 859b1b8ab34Slling /* 860b1b8ab34Slling * 'NAME' and 'SOURCE' columns 861b1b8ab34Slling */ 862990b4856Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 863990b4856Slling ZFS_PROP_NAME) && 864b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 865b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 866b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 867b1b8ab34Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 868b1b8ab34Slling } 869b1b8ab34Slling } 870b1b8ab34Slling 871b1b8ab34Slling /* 872b1b8ab34Slling * Now go through and print the headers. 873b1b8ab34Slling */ 874b1b8ab34Slling for (i = 0; i < 4; i++) { 875b1b8ab34Slling switch (cbp->cb_columns[i]) { 876b1b8ab34Slling case GET_COL_NAME: 877b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "NAME"); 878b1b8ab34Slling break; 879b1b8ab34Slling case GET_COL_PROPERTY: 880b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 881b1b8ab34Slling break; 882b1b8ab34Slling case GET_COL_VALUE: 883b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 884b1b8ab34Slling break; 885b1b8ab34Slling case GET_COL_SOURCE: 886b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 887b1b8ab34Slling break; 888b1b8ab34Slling default: 889b1b8ab34Slling title = NULL; 890b1b8ab34Slling } 891b1b8ab34Slling 892b1b8ab34Slling if (title != NULL) { 893b1b8ab34Slling if (i == 3 || cbp->cb_columns[i + 1] == 0) 894b1b8ab34Slling (void) printf("%s", title); 895b1b8ab34Slling else 896b1b8ab34Slling (void) printf("%-*s ", 897b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 898b1b8ab34Slling title); 899b1b8ab34Slling } 900b1b8ab34Slling } 901b1b8ab34Slling (void) printf("\n"); 902b1b8ab34Slling } 903b1b8ab34Slling 904b1b8ab34Slling /* 905b1b8ab34Slling * Display a single line of output, according to the settings in the callback 906b1b8ab34Slling * structure. 907b1b8ab34Slling */ 908b1b8ab34Slling void 909990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 910990b4856Slling const char *propname, const char *value, zprop_source_t sourcetype, 911b1b8ab34Slling const char *source) 912b1b8ab34Slling { 913b1b8ab34Slling int i; 914b1b8ab34Slling const char *str; 915b1b8ab34Slling char buf[128]; 916b1b8ab34Slling 917b1b8ab34Slling /* 918b1b8ab34Slling * Ignore those source types that the user has chosen to ignore. 919b1b8ab34Slling */ 920b1b8ab34Slling if ((sourcetype & cbp->cb_sources) == 0) 921b1b8ab34Slling return; 922b1b8ab34Slling 923b1b8ab34Slling if (cbp->cb_first) 924990b4856Slling zprop_print_headers(cbp, cbp->cb_type); 925b1b8ab34Slling 926b1b8ab34Slling for (i = 0; i < 4; i++) { 927b1b8ab34Slling switch (cbp->cb_columns[i]) { 928b1b8ab34Slling case GET_COL_NAME: 929b1b8ab34Slling str = name; 930b1b8ab34Slling break; 931b1b8ab34Slling 932b1b8ab34Slling case GET_COL_PROPERTY: 933b1b8ab34Slling str = propname; 934b1b8ab34Slling break; 935b1b8ab34Slling 936b1b8ab34Slling case GET_COL_VALUE: 937b1b8ab34Slling str = value; 938b1b8ab34Slling break; 939b1b8ab34Slling 940b1b8ab34Slling case GET_COL_SOURCE: 941b1b8ab34Slling switch (sourcetype) { 942990b4856Slling case ZPROP_SRC_NONE: 943b1b8ab34Slling str = "-"; 944b1b8ab34Slling break; 945b1b8ab34Slling 946990b4856Slling case ZPROP_SRC_DEFAULT: 947b1b8ab34Slling str = "default"; 948b1b8ab34Slling break; 949b1b8ab34Slling 950990b4856Slling case ZPROP_SRC_LOCAL: 951b1b8ab34Slling str = "local"; 952b1b8ab34Slling break; 953b1b8ab34Slling 954990b4856Slling case ZPROP_SRC_TEMPORARY: 955b1b8ab34Slling str = "temporary"; 956b1b8ab34Slling break; 957b1b8ab34Slling 958990b4856Slling case ZPROP_SRC_INHERITED: 959b1b8ab34Slling (void) snprintf(buf, sizeof (buf), 960b1b8ab34Slling "inherited from %s", source); 961b1b8ab34Slling str = buf; 962b1b8ab34Slling break; 963b1b8ab34Slling } 964b1b8ab34Slling break; 965b1b8ab34Slling 966b1b8ab34Slling default: 967b1b8ab34Slling continue; 968b1b8ab34Slling } 969b1b8ab34Slling 970b1b8ab34Slling if (cbp->cb_columns[i + 1] == 0) 971b1b8ab34Slling (void) printf("%s", str); 972b1b8ab34Slling else if (cbp->cb_scripted) 973b1b8ab34Slling (void) printf("%s\t", str); 974b1b8ab34Slling else 975b1b8ab34Slling (void) printf("%-*s ", 976b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 977b1b8ab34Slling str); 978b1b8ab34Slling 979b1b8ab34Slling } 980b1b8ab34Slling 981b1b8ab34Slling (void) printf("\n"); 982b1b8ab34Slling } 983ecd6cf80Smarks 984990b4856Slling /* 985990b4856Slling * Given a numeric suffix, convert the value into a number of bits that the 986990b4856Slling * resulting value must be shifted. 987990b4856Slling */ 988990b4856Slling static int 989990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf) 990ecd6cf80Smarks { 991990b4856Slling const char *ends = "BKMGTPEZ"; 992990b4856Slling int i; 993ecd6cf80Smarks 994990b4856Slling if (buf[0] == '\0') 995990b4856Slling return (0); 996990b4856Slling for (i = 0; i < strlen(ends); i++) { 997990b4856Slling if (toupper(buf[0]) == ends[i]) 998990b4856Slling break; 999ecd6cf80Smarks } 1000990b4856Slling if (i == strlen(ends)) { 1001990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1002990b4856Slling "invalid numeric suffix '%s'"), buf); 1003990b4856Slling return (-1); 1004990b4856Slling } 1005ecd6cf80Smarks 1006990b4856Slling /* 1007990b4856Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 1008990b4856Slling * allow 'BB' - that's just weird. 1009990b4856Slling */ 1010990b4856Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 1011990b4856Slling toupper(buf[0]) != 'B')) 1012990b4856Slling return (10*i); 1013990b4856Slling 1014990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1015990b4856Slling "invalid numeric suffix '%s'"), buf); 1016990b4856Slling return (-1); 1017990b4856Slling } 1018990b4856Slling 1019990b4856Slling /* 1020990b4856Slling * Convert a string of the form '100G' into a real number. Used when setting 1021990b4856Slling * properties or creating a volume. 'buf' is used to place an extended error 1022990b4856Slling * message for the caller to use. 1023990b4856Slling */ 1024990b4856Slling int 1025990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1026990b4856Slling { 1027990b4856Slling char *end; 1028990b4856Slling int shift; 1029990b4856Slling 1030990b4856Slling *num = 0; 1031990b4856Slling 1032990b4856Slling /* Check to see if this looks like a number. */ 1033990b4856Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1034990b4856Slling if (hdl) 1035990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1036990b4856Slling "bad numeric value '%s'"), value); 1037990b4856Slling return (-1); 1038990b4856Slling } 1039990b4856Slling 1040f67f35c3SEric Schrock /* Rely on stroull() to process the numeric portion. */ 1041990b4856Slling errno = 0; 1042f67f35c3SEric Schrock *num = strtoull(value, &end, 10); 1043990b4856Slling 1044990b4856Slling /* 1045990b4856Slling * Check for ERANGE, which indicates that the value is too large to fit 1046990b4856Slling * in a 64-bit value. 1047990b4856Slling */ 1048990b4856Slling if (errno == ERANGE) { 1049990b4856Slling if (hdl) 1050990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1051990b4856Slling "numeric value is too large")); 1052990b4856Slling return (-1); 1053990b4856Slling } 1054990b4856Slling 1055990b4856Slling /* 1056990b4856Slling * If we have a decimal value, then do the computation with floating 1057990b4856Slling * point arithmetic. Otherwise, use standard arithmetic. 1058990b4856Slling */ 1059990b4856Slling if (*end == '.') { 1060990b4856Slling double fval = strtod(value, &end); 1061990b4856Slling 1062990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1063990b4856Slling return (-1); 1064990b4856Slling 1065990b4856Slling fval *= pow(2, shift); 1066990b4856Slling 1067990b4856Slling if (fval > UINT64_MAX) { 1068990b4856Slling if (hdl) 1069990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1070990b4856Slling "numeric value is too large")); 1071990b4856Slling return (-1); 1072990b4856Slling } 1073990b4856Slling 1074990b4856Slling *num = (uint64_t)fval; 1075990b4856Slling } else { 1076990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1077990b4856Slling return (-1); 1078990b4856Slling 1079990b4856Slling /* Check for overflow */ 1080990b4856Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 1081990b4856Slling if (hdl) 1082990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1083990b4856Slling "numeric value is too large")); 1084990b4856Slling return (-1); 1085990b4856Slling } 1086990b4856Slling 1087990b4856Slling *num <<= shift; 1088990b4856Slling } 1089990b4856Slling 1090990b4856Slling return (0); 1091990b4856Slling } 1092990b4856Slling 1093990b4856Slling /* 1094990b4856Slling * Given a propname=value nvpair to set, parse any numeric properties 1095990b4856Slling * (index, boolean, etc) if they are specified as strings and add the 1096990b4856Slling * resulting nvpair to the returned nvlist. 1097990b4856Slling * 1098990b4856Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 1099990b4856Slling * We want the user to be able to ignore this fact and specify properties 1100990b4856Slling * as native values (numbers, for example) or as strings (to simplify 1101990b4856Slling * command line utilities). This also handles converting index types 1102990b4856Slling * (compression, checksum, etc) from strings to their on-disk index. 1103990b4856Slling */ 1104990b4856Slling int 1105990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1106990b4856Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1107990b4856Slling const char *errbuf) 1108990b4856Slling { 1109990b4856Slling data_type_t datatype = nvpair_type(elem); 1110990b4856Slling zprop_type_t proptype; 1111990b4856Slling const char *propname; 1112990b4856Slling char *value; 1113990b4856Slling boolean_t isnone = B_FALSE; 1114990b4856Slling 1115990b4856Slling if (type == ZFS_TYPE_POOL) { 1116990b4856Slling proptype = zpool_prop_get_type(prop); 1117990b4856Slling propname = zpool_prop_to_name(prop); 1118990b4856Slling } else { 1119990b4856Slling proptype = zfs_prop_get_type(prop); 1120990b4856Slling propname = zfs_prop_to_name(prop); 1121990b4856Slling } 1122990b4856Slling 1123990b4856Slling /* 1124990b4856Slling * Convert any properties to the internal DSL value types. 1125990b4856Slling */ 1126990b4856Slling *svalp = NULL; 1127990b4856Slling *ivalp = 0; 1128990b4856Slling 1129990b4856Slling switch (proptype) { 1130990b4856Slling case PROP_TYPE_STRING: 1131990b4856Slling if (datatype != DATA_TYPE_STRING) { 1132990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1133990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1134990b4856Slling goto error; 1135990b4856Slling } 1136990b4856Slling (void) nvpair_value_string(elem, svalp); 1137990b4856Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1138990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1139990b4856Slling "'%s' is too long"), nvpair_name(elem)); 1140990b4856Slling goto error; 1141990b4856Slling } 1142990b4856Slling break; 1143990b4856Slling 1144990b4856Slling case PROP_TYPE_NUMBER: 1145990b4856Slling if (datatype == DATA_TYPE_STRING) { 1146990b4856Slling (void) nvpair_value_string(elem, &value); 1147990b4856Slling if (strcmp(value, "none") == 0) { 1148990b4856Slling isnone = B_TRUE; 1149990b4856Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 1150990b4856Slling != 0) { 1151990b4856Slling goto error; 1152990b4856Slling } 1153990b4856Slling } else if (datatype == DATA_TYPE_UINT64) { 1154990b4856Slling (void) nvpair_value_uint64(elem, ivalp); 1155990b4856Slling } else { 1156990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1157990b4856Slling "'%s' must be a number"), nvpair_name(elem)); 1158990b4856Slling goto error; 1159990b4856Slling } 1160990b4856Slling 1161990b4856Slling /* 1162990b4856Slling * Quota special: force 'none' and don't allow 0. 1163990b4856Slling */ 1164a9799022Sck153898 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1165a9799022Sck153898 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1166990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1167a9799022Sck153898 "use 'none' to disable quota/refquota")); 1168990b4856Slling goto error; 1169990b4856Slling } 1170990b4856Slling break; 1171990b4856Slling 1172990b4856Slling case PROP_TYPE_INDEX: 1173a9799022Sck153898 if (datatype != DATA_TYPE_STRING) { 1174990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1175990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1176990b4856Slling goto error; 1177990b4856Slling } 1178990b4856Slling 1179a9799022Sck153898 (void) nvpair_value_string(elem, &value); 1180a9799022Sck153898 1181990b4856Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1182990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1183990b4856Slling "'%s' must be one of '%s'"), propname, 1184990b4856Slling zprop_values(prop, type)); 1185990b4856Slling goto error; 1186990b4856Slling } 1187990b4856Slling break; 1188990b4856Slling 1189990b4856Slling default: 1190990b4856Slling abort(); 1191990b4856Slling } 1192990b4856Slling 1193990b4856Slling /* 1194990b4856Slling * Add the result to our return set of properties. 1195990b4856Slling */ 1196990b4856Slling if (*svalp != NULL) { 1197990b4856Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 1198990b4856Slling (void) no_memory(hdl); 1199990b4856Slling return (-1); 1200990b4856Slling } 1201990b4856Slling } else { 1202990b4856Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1203990b4856Slling (void) no_memory(hdl); 1204990b4856Slling return (-1); 1205990b4856Slling } 1206990b4856Slling } 1207990b4856Slling 1208990b4856Slling return (0); 1209990b4856Slling error: 1210990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1211990b4856Slling return (-1); 1212990b4856Slling } 1213990b4856Slling 121474e7dc98SMatthew Ahrens static int 121574e7dc98SMatthew Ahrens addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 121674e7dc98SMatthew Ahrens zfs_type_t type) 121774e7dc98SMatthew Ahrens { 121874e7dc98SMatthew Ahrens int prop; 121974e7dc98SMatthew Ahrens zprop_list_t *entry; 122074e7dc98SMatthew Ahrens 122174e7dc98SMatthew Ahrens prop = zprop_name_to_prop(propname, type); 122274e7dc98SMatthew Ahrens 122374e7dc98SMatthew Ahrens if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 122474e7dc98SMatthew Ahrens prop = ZPROP_INVAL; 122574e7dc98SMatthew Ahrens 122674e7dc98SMatthew Ahrens /* 122774e7dc98SMatthew Ahrens * When no property table entry can be found, return failure if 122874e7dc98SMatthew Ahrens * this is a pool property or if this isn't a user-defined 122974e7dc98SMatthew Ahrens * dataset property, 123074e7dc98SMatthew Ahrens */ 123174e7dc98SMatthew Ahrens if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 123214843421SMatthew Ahrens (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) { 123374e7dc98SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 123474e7dc98SMatthew Ahrens "invalid property '%s'"), propname); 123574e7dc98SMatthew Ahrens return (zfs_error(hdl, EZFS_BADPROP, 123674e7dc98SMatthew Ahrens dgettext(TEXT_DOMAIN, "bad property list"))); 123774e7dc98SMatthew Ahrens } 123874e7dc98SMatthew Ahrens 123974e7dc98SMatthew Ahrens if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 124074e7dc98SMatthew Ahrens return (-1); 124174e7dc98SMatthew Ahrens 124274e7dc98SMatthew Ahrens entry->pl_prop = prop; 124374e7dc98SMatthew Ahrens if (prop == ZPROP_INVAL) { 124474e7dc98SMatthew Ahrens if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) { 124574e7dc98SMatthew Ahrens free(entry); 124674e7dc98SMatthew Ahrens return (-1); 124774e7dc98SMatthew Ahrens } 124874e7dc98SMatthew Ahrens entry->pl_width = strlen(propname); 124974e7dc98SMatthew Ahrens } else { 125074e7dc98SMatthew Ahrens entry->pl_width = zprop_width(prop, &entry->pl_fixed, 125174e7dc98SMatthew Ahrens type); 125274e7dc98SMatthew Ahrens } 125374e7dc98SMatthew Ahrens 125474e7dc98SMatthew Ahrens *listp = entry; 125574e7dc98SMatthew Ahrens 125674e7dc98SMatthew Ahrens return (0); 125774e7dc98SMatthew Ahrens } 125874e7dc98SMatthew Ahrens 1259990b4856Slling /* 1260990b4856Slling * Given a comma-separated list of properties, construct a property list 1261990b4856Slling * containing both user-defined and native properties. This function will 1262990b4856Slling * return a NULL list if 'all' is specified, which can later be expanded 1263990b4856Slling * by zprop_expand_list(). 1264990b4856Slling */ 1265990b4856Slling int 1266990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1267990b4856Slling zfs_type_t type) 1268990b4856Slling { 1269990b4856Slling *listp = NULL; 1270990b4856Slling 1271990b4856Slling /* 1272990b4856Slling * If 'all' is specified, return a NULL list. 1273990b4856Slling */ 1274990b4856Slling if (strcmp(props, "all") == 0) 1275990b4856Slling return (0); 1276990b4856Slling 1277990b4856Slling /* 1278990b4856Slling * If no props were specified, return an error. 1279990b4856Slling */ 1280990b4856Slling if (props[0] == '\0') { 1281990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1282990b4856Slling "no properties specified")); 1283990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1284990b4856Slling "bad property list"))); 1285990b4856Slling } 1286990b4856Slling 1287990b4856Slling /* 1288990b4856Slling * It would be nice to use getsubopt() here, but the inclusion of column 1289990b4856Slling * aliases makes this more effort than it's worth. 1290990b4856Slling */ 129174e7dc98SMatthew Ahrens while (*props != '\0') { 129274e7dc98SMatthew Ahrens size_t len; 129374e7dc98SMatthew Ahrens char *p; 129474e7dc98SMatthew Ahrens char c; 129574e7dc98SMatthew Ahrens 129674e7dc98SMatthew Ahrens if ((p = strchr(props, ',')) == NULL) { 129774e7dc98SMatthew Ahrens len = strlen(props); 129874e7dc98SMatthew Ahrens p = props + len; 1299990b4856Slling } else { 130074e7dc98SMatthew Ahrens len = p - props; 1301990b4856Slling } 1302990b4856Slling 1303990b4856Slling /* 1304990b4856Slling * Check for empty options. 1305990b4856Slling */ 1306990b4856Slling if (len == 0) { 1307990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1308990b4856Slling "empty property name")); 1309990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, 1310990b4856Slling dgettext(TEXT_DOMAIN, "bad property list"))); 1311990b4856Slling } 1312990b4856Slling 1313990b4856Slling /* 1314990b4856Slling * Check all regular property names. 1315990b4856Slling */ 131674e7dc98SMatthew Ahrens c = props[len]; 131774e7dc98SMatthew Ahrens props[len] = '\0'; 1318990b4856Slling 131974e7dc98SMatthew Ahrens if (strcmp(props, "space") == 0) { 132074e7dc98SMatthew Ahrens static char *spaceprops[] = { 132174e7dc98SMatthew Ahrens "name", "avail", "used", "usedbysnapshots", 132274e7dc98SMatthew Ahrens "usedbydataset", "usedbyrefreservation", 132374e7dc98SMatthew Ahrens "usedbychildren", NULL 132474e7dc98SMatthew Ahrens }; 132574e7dc98SMatthew Ahrens int i; 1326990b4856Slling 132774e7dc98SMatthew Ahrens for (i = 0; spaceprops[i]; i++) { 132874e7dc98SMatthew Ahrens if (addlist(hdl, spaceprops[i], listp, type)) 1329990b4856Slling return (-1); 133074e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1331990b4856Slling } 1332990b4856Slling } else { 133374e7dc98SMatthew Ahrens if (addlist(hdl, props, listp, type)) 133474e7dc98SMatthew Ahrens return (-1); 133574e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1336990b4856Slling } 1337990b4856Slling 133874e7dc98SMatthew Ahrens props = p; 1339990b4856Slling if (c == ',') 134074e7dc98SMatthew Ahrens props++; 1341990b4856Slling } 1342990b4856Slling 1343990b4856Slling return (0); 1344990b4856Slling } 1345990b4856Slling 1346990b4856Slling void 1347990b4856Slling zprop_free_list(zprop_list_t *pl) 1348990b4856Slling { 1349990b4856Slling zprop_list_t *next; 1350990b4856Slling 1351990b4856Slling while (pl != NULL) { 1352990b4856Slling next = pl->pl_next; 1353990b4856Slling free(pl->pl_user_prop); 1354990b4856Slling free(pl); 1355990b4856Slling pl = next; 1356990b4856Slling } 1357990b4856Slling } 1358990b4856Slling 1359990b4856Slling typedef struct expand_data { 1360990b4856Slling zprop_list_t **last; 1361990b4856Slling libzfs_handle_t *hdl; 1362990b4856Slling zfs_type_t type; 1363990b4856Slling } expand_data_t; 1364990b4856Slling 1365990b4856Slling int 1366990b4856Slling zprop_expand_list_cb(int prop, void *cb) 1367990b4856Slling { 1368990b4856Slling zprop_list_t *entry; 1369990b4856Slling expand_data_t *edp = cb; 1370990b4856Slling 1371990b4856Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1372990b4856Slling return (ZPROP_INVAL); 1373990b4856Slling 1374990b4856Slling entry->pl_prop = prop; 1375990b4856Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1376990b4856Slling entry->pl_all = B_TRUE; 1377990b4856Slling 1378990b4856Slling *(edp->last) = entry; 1379990b4856Slling edp->last = &entry->pl_next; 1380990b4856Slling 1381990b4856Slling return (ZPROP_CONT); 1382990b4856Slling } 1383990b4856Slling 1384990b4856Slling int 1385990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1386990b4856Slling { 1387990b4856Slling zprop_list_t *entry; 1388990b4856Slling zprop_list_t **last; 1389990b4856Slling expand_data_t exp; 1390990b4856Slling 1391990b4856Slling if (*plp == NULL) { 1392990b4856Slling /* 1393990b4856Slling * If this is the very first time we've been called for an 'all' 1394990b4856Slling * specification, expand the list to include all native 1395990b4856Slling * properties. 1396990b4856Slling */ 1397990b4856Slling last = plp; 1398990b4856Slling 1399990b4856Slling exp.last = last; 1400990b4856Slling exp.hdl = hdl; 1401990b4856Slling exp.type = type; 1402990b4856Slling 1403990b4856Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1404990b4856Slling B_FALSE, type) == ZPROP_INVAL) 1405990b4856Slling return (-1); 1406990b4856Slling 1407990b4856Slling /* 1408990b4856Slling * Add 'name' to the beginning of the list, which is handled 1409990b4856Slling * specially. 1410990b4856Slling */ 1411990b4856Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1412990b4856Slling return (-1); 1413990b4856Slling 1414990b4856Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1415990b4856Slling ZFS_PROP_NAME; 1416990b4856Slling entry->pl_width = zprop_width(entry->pl_prop, 1417990b4856Slling &entry->pl_fixed, type); 1418990b4856Slling entry->pl_all = B_TRUE; 1419990b4856Slling entry->pl_next = *plp; 1420990b4856Slling *plp = entry; 1421990b4856Slling } 1422990b4856Slling return (0); 1423990b4856Slling } 1424990b4856Slling 1425990b4856Slling int 1426990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1427990b4856Slling zfs_type_t type) 1428990b4856Slling { 1429990b4856Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 1430ecd6cf80Smarks } 1431