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 */ 21ad135b5dSChristopher Siden 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24a2afb611SJerry Jelinek * Copyright (c) 2013, Joyent, Inc. All rights reserved. 259a686fbcSPaul Dagnelie * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 2688f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 271702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens /* 31fa9e4066Sahrens * Internal utility routines for the ZFS library. 32fa9e4066Sahrens */ 33fa9e4066Sahrens 34fa9e4066Sahrens #include <errno.h> 35fa9e4066Sahrens #include <fcntl.h> 36fa9e4066Sahrens #include <libintl.h> 37fa9e4066Sahrens #include <stdarg.h> 38fa9e4066Sahrens #include <stdio.h> 39fa9e4066Sahrens #include <stdlib.h> 40fa9e4066Sahrens #include <strings.h> 41fa9e4066Sahrens #include <unistd.h> 42990b4856Slling #include <ctype.h> 43990b4856Slling #include <math.h> 442bcf0248SMax Grossman #include <sys/filio.h> 45fa9e4066Sahrens #include <sys/mnttab.h> 465aba80dbSck #include <sys/mntent.h> 475aba80dbSck #include <sys/types.h> 48*0a055120SJason King #include <libcmdutils.h> 49fa9e4066Sahrens 50fa9e4066Sahrens #include <libzfs.h> 514445fffbSMatthew Ahrens #include <libzfs_core.h> 52fa9e4066Sahrens 53fa9e4066Sahrens #include "libzfs_impl.h" 5491ebeef5Sahrens #include "zfs_prop.h" 55ad135b5dSChristopher Siden #include "zfeature_common.h" 56fa9e4066Sahrens 5799653d4eSeschrock int 5899653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl) 5999653d4eSeschrock { 6099653d4eSeschrock return (hdl->libzfs_error); 6199653d4eSeschrock } 62fa9e4066Sahrens 6399653d4eSeschrock const char * 6499653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl) 6599653d4eSeschrock { 6699653d4eSeschrock return (hdl->libzfs_action); 6799653d4eSeschrock } 68fa9e4066Sahrens 6999653d4eSeschrock const char * 7099653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl) 7199653d4eSeschrock { 7299653d4eSeschrock if (hdl->libzfs_desc[0] != '\0') 7399653d4eSeschrock return (hdl->libzfs_desc); 7499653d4eSeschrock 7599653d4eSeschrock switch (hdl->libzfs_error) { 7699653d4eSeschrock case EZFS_NOMEM: 7799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of memory")); 7899653d4eSeschrock case EZFS_BADPROP: 7999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid property value")); 8099653d4eSeschrock case EZFS_PROPREADONLY: 81f9af39baSGeorge Wilson return (dgettext(TEXT_DOMAIN, "read-only property")); 8299653d4eSeschrock case EZFS_PROPTYPE: 8399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 8499653d4eSeschrock "datasets of this type")); 8599653d4eSeschrock case EZFS_PROPNONINHERIT: 8699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 8799653d4eSeschrock case EZFS_PROPSPACE: 8899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 8999653d4eSeschrock case EZFS_BADTYPE: 9099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation not applicable to " 9199653d4eSeschrock "datasets of this type")); 9299653d4eSeschrock case EZFS_BUSY: 9399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 9499653d4eSeschrock case EZFS_EXISTS: 9599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 9699653d4eSeschrock case EZFS_NOENT: 9799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 9899653d4eSeschrock case EZFS_BADSTREAM: 9999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 10099653d4eSeschrock case EZFS_DSREADONLY: 101f9af39baSGeorge Wilson return (dgettext(TEXT_DOMAIN, "dataset is read-only")); 10299653d4eSeschrock case EZFS_VOLTOOBIG: 10399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 10499653d4eSeschrock "this system")); 10599653d4eSeschrock case EZFS_INVALIDNAME: 10699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 10799653d4eSeschrock case EZFS_BADRESTORE: 10899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 10999653d4eSeschrock "destination")); 11099653d4eSeschrock case EZFS_BADBACKUP: 11199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 11299653d4eSeschrock case EZFS_BADTARGET: 11399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 11499653d4eSeschrock case EZFS_NODEVICE: 11599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 11699653d4eSeschrock case EZFS_BADDEV: 11799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 11899653d4eSeschrock case EZFS_NOREPLICAS: 11999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 12099653d4eSeschrock case EZFS_RESILVERING: 12199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 12299653d4eSeschrock case EZFS_BADVERSION: 123ad135b5dSChristopher Siden return (dgettext(TEXT_DOMAIN, "unsupported version or " 124ad135b5dSChristopher Siden "feature")); 12599653d4eSeschrock case EZFS_POOLUNAVAIL: 12699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 12799653d4eSeschrock case EZFS_DEVOVERFLOW: 12899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 12999653d4eSeschrock case EZFS_BADPATH: 13099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 13199653d4eSeschrock case EZFS_CROSSTARGET: 13299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 13399653d4eSeschrock "pools")); 13499653d4eSeschrock case EZFS_ZONED: 13599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 13699653d4eSeschrock case EZFS_MOUNTFAILED: 13799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 13899653d4eSeschrock case EZFS_UMOUNTFAILED: 13999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 140f3861e1aSahl case EZFS_UNSHARENFSFAILED: 14199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 142f3861e1aSahl case EZFS_SHARENFSFAILED: 14399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 144da6c28aaSamw case EZFS_UNSHARESMBFAILED: 145da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 146da6c28aaSamw case EZFS_SHARESMBFAILED: 147da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb add share failed")); 14899653d4eSeschrock case EZFS_PERM: 14999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 15099653d4eSeschrock case EZFS_NOSPC: 15199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of space")); 1526e27f868SSam Falkner case EZFS_FAULT: 1536e27f868SSam Falkner return (dgettext(TEXT_DOMAIN, "bad address")); 15499653d4eSeschrock case EZFS_IO: 15599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 15699653d4eSeschrock case EZFS_INTR: 15799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "signal received")); 15899653d4eSeschrock case EZFS_ISSPARE: 15999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 16099653d4eSeschrock "spare")); 16199653d4eSeschrock case EZFS_INVALCONFIG: 16299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1633bb79becSeschrock case EZFS_RECURSIVE: 1643bb79becSeschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 16506eeb2adSek case EZFS_NOHISTORY: 16606eeb2adSek return (dgettext(TEXT_DOMAIN, "no history available")); 167b1b8ab34Slling case EZFS_POOLPROPS: 168b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 169b1b8ab34Slling "pool properties")); 170b1b8ab34Slling case EZFS_POOL_NOTSUP: 171b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 172b1b8ab34Slling "on this type of pool")); 173b1b8ab34Slling case EZFS_POOL_INVALARG: 174b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 175b1b8ab34Slling "this pool operation")); 176b7661cccSmmusante case EZFS_NAMETOOLONG: 177b7661cccSmmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 1788488aeb5Staylor case EZFS_OPENFAILED: 1798488aeb5Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 1808488aeb5Staylor case EZFS_NOCAP: 1818488aeb5Staylor return (dgettext(TEXT_DOMAIN, 1828488aeb5Staylor "disk capacity information could not be retrieved")); 1838488aeb5Staylor case EZFS_LABELFAILED: 1848488aeb5Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 185ecd6cf80Smarks case EZFS_BADWHO: 186ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid user/group")); 187ecd6cf80Smarks case EZFS_BADPERM: 188ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission")); 189ecd6cf80Smarks case EZFS_BADPERMSET: 190ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 191ecd6cf80Smarks case EZFS_NODELEGATION: 192ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "delegated administration is " 193ecd6cf80Smarks "disabled on pool")); 1942f8aaab3Seschrock case EZFS_BADCACHE: 1952f8aaab3Seschrock return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 196fa94a07fSbrendan case EZFS_ISL2CACHE: 197fa94a07fSbrendan return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 198e7cbe64fSgw case EZFS_VDEVNOTSUP: 199e7cbe64fSgw return (dgettext(TEXT_DOMAIN, "vdev specification is not " 200e7cbe64fSgw "supported")); 20115e6edf1Sgw case EZFS_NOTSUP: 20215e6edf1Sgw return (dgettext(TEXT_DOMAIN, "operation not supported " 20315e6edf1Sgw "on this dataset")); 20489a89ebfSlling case EZFS_ACTIVE_SPARE: 20589a89ebfSlling return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 20689a89ebfSlling "device")); 207e6ca193dSGeorge Wilson case EZFS_UNPLAYED_LOGS: 208e6ca193dSGeorge Wilson return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 209e6ca193dSGeorge Wilson "logs")); 210842727c2SChris Kirby case EZFS_REFTAG_RELE: 211842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 212842727c2SChris Kirby case EZFS_REFTAG_HOLD: 213842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "tag already exists on this " 214842727c2SChris Kirby "dataset")); 215ca45db41SChris Kirby case EZFS_TAGTOOLONG: 216ca45db41SChris Kirby return (dgettext(TEXT_DOMAIN, "tag too long")); 2179e69d7d0SLori Alt case EZFS_PIPEFAILED: 2189e69d7d0SLori Alt return (dgettext(TEXT_DOMAIN, "pipe create failed")); 2199e69d7d0SLori Alt case EZFS_THREADCREATEFAILED: 2209e69d7d0SLori Alt return (dgettext(TEXT_DOMAIN, "thread create failed")); 2211195e687SMark J Musante case EZFS_POSTSPLIT_ONLINE: 2221195e687SMark J Musante return (dgettext(TEXT_DOMAIN, "disk was split from this pool " 2231195e687SMark J Musante "into a new one")); 2241702cce7SAlek Pinchuk case EZFS_SCRUB_PAUSED: 2251702cce7SAlek Pinchuk return (dgettext(TEXT_DOMAIN, "scrub is paused; " 2261702cce7SAlek Pinchuk "use 'zpool scrub' to resume")); 2273f9d6ad7SLin Ling case EZFS_SCRUBBING: 2283f9d6ad7SLin Ling return (dgettext(TEXT_DOMAIN, "currently scrubbing; " 2293f9d6ad7SLin Ling "use 'zpool scrub -s' to cancel current scrub")); 2303f9d6ad7SLin Ling case EZFS_NO_SCRUB: 2313f9d6ad7SLin Ling return (dgettext(TEXT_DOMAIN, "there is no active scrub")); 23299d5e173STim Haley case EZFS_DIFF: 23399d5e173STim Haley return (dgettext(TEXT_DOMAIN, "unable to generate diffs")); 23499d5e173STim Haley case EZFS_DIFFDATA: 23599d5e173STim Haley return (dgettext(TEXT_DOMAIN, "invalid diff data")); 236f9af39baSGeorge Wilson case EZFS_POOLREADONLY: 237f9af39baSGeorge Wilson return (dgettext(TEXT_DOMAIN, "pool is read-only")); 23899653d4eSeschrock case EZFS_UNKNOWN: 23999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 24099653d4eSeschrock default: 241c08432ebSeschrock assert(hdl->libzfs_error == 0); 242c08432ebSeschrock return (dgettext(TEXT_DOMAIN, "no error")); 24399653d4eSeschrock } 24499653d4eSeschrock } 24599653d4eSeschrock 24699653d4eSeschrock /*PRINTFLIKE2*/ 247fa9e4066Sahrens void 24899653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 249fa9e4066Sahrens { 250fa9e4066Sahrens va_list ap; 251fa9e4066Sahrens 252fa9e4066Sahrens va_start(ap, fmt); 253fa9e4066Sahrens 25499653d4eSeschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 25599653d4eSeschrock fmt, ap); 25699653d4eSeschrock hdl->libzfs_desc_active = 1; 25799653d4eSeschrock 25899653d4eSeschrock va_end(ap); 25999653d4eSeschrock } 26099653d4eSeschrock 26199653d4eSeschrock static void 26299653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 26399653d4eSeschrock { 26499653d4eSeschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 26599653d4eSeschrock fmt, ap); 26699653d4eSeschrock hdl->libzfs_error = error; 26799653d4eSeschrock 26899653d4eSeschrock if (hdl->libzfs_desc_active) 26999653d4eSeschrock hdl->libzfs_desc_active = 0; 27099653d4eSeschrock else 27199653d4eSeschrock hdl->libzfs_desc[0] = '\0'; 27299653d4eSeschrock 27399653d4eSeschrock if (hdl->libzfs_printerr) { 27499653d4eSeschrock if (error == EZFS_UNKNOWN) { 27599653d4eSeschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 27699653d4eSeschrock "error: %s\n"), libzfs_error_description(hdl)); 27799653d4eSeschrock abort(); 27899653d4eSeschrock } 27999653d4eSeschrock 28099653d4eSeschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 281b1b8ab34Slling libzfs_error_description(hdl)); 28299653d4eSeschrock if (error == EZFS_NOMEM) 28399653d4eSeschrock exit(1); 284fa9e4066Sahrens } 28599653d4eSeschrock } 28699653d4eSeschrock 287ece3d9b3Slling int 288ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 289ece3d9b3Slling { 290ece3d9b3Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 291ece3d9b3Slling } 292ece3d9b3Slling 29399653d4eSeschrock /*PRINTFLIKE3*/ 29499653d4eSeschrock int 295ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 29699653d4eSeschrock { 29799653d4eSeschrock va_list ap; 29899653d4eSeschrock 29999653d4eSeschrock va_start(ap, fmt); 30099653d4eSeschrock 30199653d4eSeschrock zfs_verror(hdl, error, fmt, ap); 302fa9e4066Sahrens 303fa9e4066Sahrens va_end(ap); 30499653d4eSeschrock 30599653d4eSeschrock return (-1); 306fa9e4066Sahrens } 307fa9e4066Sahrens 30899653d4eSeschrock static int 30999653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 31099653d4eSeschrock va_list ap) 31199653d4eSeschrock { 31299653d4eSeschrock switch (error) { 31399653d4eSeschrock case EPERM: 31499653d4eSeschrock case EACCES: 31599653d4eSeschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 31699653d4eSeschrock return (-1); 31799653d4eSeschrock 318ecd6cf80Smarks case ECANCELED: 319ecd6cf80Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 320ecd6cf80Smarks return (-1); 321ecd6cf80Smarks 32299653d4eSeschrock case EIO: 32399653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 32499653d4eSeschrock return (-1); 32599653d4eSeschrock 3266e27f868SSam Falkner case EFAULT: 3276e27f868SSam Falkner zfs_verror(hdl, EZFS_FAULT, fmt, ap); 3286e27f868SSam Falkner return (-1); 3296e27f868SSam Falkner 33099653d4eSeschrock case EINTR: 33199653d4eSeschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 33299653d4eSeschrock return (-1); 33399653d4eSeschrock } 33499653d4eSeschrock 33599653d4eSeschrock return (0); 33699653d4eSeschrock } 33799653d4eSeschrock 338ece3d9b3Slling int 339ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 340ece3d9b3Slling { 341ece3d9b3Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 342ece3d9b3Slling } 343ece3d9b3Slling 34499653d4eSeschrock /*PRINTFLIKE3*/ 34599653d4eSeschrock int 346ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 347fa9e4066Sahrens { 348fa9e4066Sahrens va_list ap; 349fa9e4066Sahrens 350fa9e4066Sahrens va_start(ap, fmt); 351fa9e4066Sahrens 35299653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 35399653d4eSeschrock va_end(ap); 35499653d4eSeschrock return (-1); 355fa9e4066Sahrens } 356fa9e4066Sahrens 35799653d4eSeschrock switch (error) { 35899653d4eSeschrock case ENXIO: 35997d9e3a6Sck case ENODEV: 36019b94df9SMatthew Ahrens case EPIPE: 36199653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 36299653d4eSeschrock break; 36399653d4eSeschrock 36499653d4eSeschrock case ENOENT: 36599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36699653d4eSeschrock "dataset does not exist")); 36799653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 36899653d4eSeschrock break; 36999653d4eSeschrock 37099653d4eSeschrock case ENOSPC: 37199653d4eSeschrock case EDQUOT: 37299653d4eSeschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 37399653d4eSeschrock return (-1); 37499653d4eSeschrock 37599653d4eSeschrock case EEXIST: 37699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37799653d4eSeschrock "dataset already exists")); 37899653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 37999653d4eSeschrock break; 38099653d4eSeschrock 38199653d4eSeschrock case EBUSY: 38299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38399653d4eSeschrock "dataset is busy")); 38499653d4eSeschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 38599653d4eSeschrock break; 386ecd6cf80Smarks case EROFS: 387f9af39baSGeorge Wilson zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 388ecd6cf80Smarks break; 389b7661cccSmmusante case ENAMETOOLONG: 390b7661cccSmmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 391b7661cccSmmusante break; 39240ff3960Sck case ENOTSUP: 39340ff3960Sck zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 39440ff3960Sck break; 39554d692b7SGeorge Wilson case EAGAIN: 39654d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 39754d692b7SGeorge Wilson "pool I/O is currently suspended")); 39854d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 39954d692b7SGeorge Wilson break; 40099653d4eSeschrock default: 40192241e0bSTom Erickson zfs_error_aux(hdl, strerror(error)); 40299653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 40399653d4eSeschrock break; 40499653d4eSeschrock } 40599653d4eSeschrock 40699653d4eSeschrock va_end(ap); 40799653d4eSeschrock return (-1); 408fa9e4066Sahrens } 409fa9e4066Sahrens 410ece3d9b3Slling int 411ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 412ece3d9b3Slling { 413ece3d9b3Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 414ece3d9b3Slling } 415ece3d9b3Slling 41699653d4eSeschrock /*PRINTFLIKE3*/ 41799653d4eSeschrock int 418ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 419fa9e4066Sahrens { 42099653d4eSeschrock va_list ap; 42199653d4eSeschrock 42299653d4eSeschrock va_start(ap, fmt); 42399653d4eSeschrock 42499653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 42599653d4eSeschrock va_end(ap); 42699653d4eSeschrock return (-1); 42799653d4eSeschrock } 42899653d4eSeschrock 42999653d4eSeschrock switch (error) { 43099653d4eSeschrock case ENODEV: 43199653d4eSeschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 43299653d4eSeschrock break; 43399653d4eSeschrock 43499653d4eSeschrock case ENOENT: 435b1b8ab34Slling zfs_error_aux(hdl, 436b1b8ab34Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 43799653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 43899653d4eSeschrock break; 43999653d4eSeschrock 44099653d4eSeschrock case EEXIST: 44199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44299653d4eSeschrock "pool already exists")); 44399653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 44499653d4eSeschrock break; 44599653d4eSeschrock 44699653d4eSeschrock case EBUSY: 44799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 44843afaaa8SEric Schrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 44999653d4eSeschrock break; 45099653d4eSeschrock 45199653d4eSeschrock case ENXIO: 45299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45399653d4eSeschrock "one or more devices is currently unavailable")); 45499653d4eSeschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 45599653d4eSeschrock break; 45699653d4eSeschrock 45799653d4eSeschrock case ENAMETOOLONG: 45899653d4eSeschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 45999653d4eSeschrock break; 46099653d4eSeschrock 461b1b8ab34Slling case ENOTSUP: 462b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 463b1b8ab34Slling break; 464b1b8ab34Slling 465b1b8ab34Slling case EINVAL: 466b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 467b1b8ab34Slling break; 468b1b8ab34Slling 469ecd6cf80Smarks case ENOSPC: 470ecd6cf80Smarks case EDQUOT: 471ecd6cf80Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 472ecd6cf80Smarks return (-1); 473f9af39baSGeorge Wilson 47454d692b7SGeorge Wilson case EAGAIN: 47554d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 47654d692b7SGeorge Wilson "pool I/O is currently suspended")); 47754d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 47854d692b7SGeorge Wilson break; 479ecd6cf80Smarks 480f9af39baSGeorge Wilson case EROFS: 481f9af39baSGeorge Wilson zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 482f9af39baSGeorge Wilson break; 483f9af39baSGeorge Wilson 48499653d4eSeschrock default: 48599653d4eSeschrock zfs_error_aux(hdl, strerror(error)); 48699653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 48799653d4eSeschrock } 48899653d4eSeschrock 48999653d4eSeschrock va_end(ap); 49099653d4eSeschrock return (-1); 491fa9e4066Sahrens } 492fa9e4066Sahrens 493fa9e4066Sahrens /* 494fa9e4066Sahrens * Display an out of memory error message and abort the current program. 495fa9e4066Sahrens */ 49699653d4eSeschrock int 49799653d4eSeschrock no_memory(libzfs_handle_t *hdl) 498fa9e4066Sahrens { 49999653d4eSeschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 500fa9e4066Sahrens } 501fa9e4066Sahrens 502fa9e4066Sahrens /* 503fa9e4066Sahrens * A safe form of malloc() which will die if the allocation fails. 504fa9e4066Sahrens */ 505fa9e4066Sahrens void * 50699653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 507fa9e4066Sahrens { 508fa9e4066Sahrens void *data; 509fa9e4066Sahrens 510fa9e4066Sahrens if ((data = calloc(1, size)) == NULL) 51199653d4eSeschrock (void) no_memory(hdl); 512fa9e4066Sahrens 513fa9e4066Sahrens return (data); 514fa9e4066Sahrens } 515fa9e4066Sahrens 51699d5e173STim Haley /* 51799d5e173STim Haley * A safe form of asprintf() which will die if the allocation fails. 51899d5e173STim Haley */ 51999d5e173STim Haley /*PRINTFLIKE2*/ 52099d5e173STim Haley char * 52199d5e173STim Haley zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) 52299d5e173STim Haley { 52399d5e173STim Haley va_list ap; 52499d5e173STim Haley char *ret; 52599d5e173STim Haley int err; 52699d5e173STim Haley 52799d5e173STim Haley va_start(ap, fmt); 52899d5e173STim Haley 52999d5e173STim Haley err = vasprintf(&ret, fmt, ap); 53099d5e173STim Haley 53199d5e173STim Haley va_end(ap); 53299d5e173STim Haley 53399d5e173STim Haley if (err < 0) 53499d5e173STim Haley (void) no_memory(hdl); 53599d5e173STim Haley 53699d5e173STim Haley return (ret); 53799d5e173STim Haley } 53899d5e173STim Haley 539e9dbad6fSeschrock /* 540e9dbad6fSeschrock * A safe form of realloc(), which also zeroes newly allocated space. 541e9dbad6fSeschrock */ 542e9dbad6fSeschrock void * 543e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 544e9dbad6fSeschrock { 545e9dbad6fSeschrock void *ret; 546e9dbad6fSeschrock 547e9dbad6fSeschrock if ((ret = realloc(ptr, newsize)) == NULL) { 548e9dbad6fSeschrock (void) no_memory(hdl); 549e9dbad6fSeschrock return (NULL); 550e9dbad6fSeschrock } 551e9dbad6fSeschrock 552e9dbad6fSeschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 553e9dbad6fSeschrock return (ret); 554e9dbad6fSeschrock } 555e9dbad6fSeschrock 556fa9e4066Sahrens /* 557fa9e4066Sahrens * A safe form of strdup() which will die if the allocation fails. 558fa9e4066Sahrens */ 559fa9e4066Sahrens char * 56099653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 561fa9e4066Sahrens { 562fa9e4066Sahrens char *ret; 563fa9e4066Sahrens 564fa9e4066Sahrens if ((ret = strdup(str)) == NULL) 56599653d4eSeschrock (void) no_memory(hdl); 566fa9e4066Sahrens 567fa9e4066Sahrens return (ret); 568fa9e4066Sahrens } 569fa9e4066Sahrens 570fa9e4066Sahrens /* 571fa9e4066Sahrens * Convert a number to an appropriately human-readable output. 572fa9e4066Sahrens */ 573fa9e4066Sahrens void 574fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 575fa9e4066Sahrens { 576*0a055120SJason King nicenum(num, buf, buflen); 577fa9e4066Sahrens } 57899653d4eSeschrock 57999653d4eSeschrock void 58099653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 58199653d4eSeschrock { 58299653d4eSeschrock hdl->libzfs_printerr = printerr; 58399653d4eSeschrock } 58499653d4eSeschrock 58599653d4eSeschrock libzfs_handle_t * 58699653d4eSeschrock libzfs_init(void) 58799653d4eSeschrock { 58899653d4eSeschrock libzfs_handle_t *hdl; 58999653d4eSeschrock 59099d5e173STim Haley if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { 59199653d4eSeschrock return (NULL); 59299653d4eSeschrock } 59399653d4eSeschrock 594c08432ebSeschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 59599653d4eSeschrock free(hdl); 59699653d4eSeschrock return (NULL); 59799653d4eSeschrock } 59899653d4eSeschrock 599bde3d612SSimon Klinkert if ((hdl->libzfs_mnttab = fopen(MNTTAB, "rF")) == NULL) { 60099653d4eSeschrock (void) close(hdl->libzfs_fd); 60199653d4eSeschrock free(hdl); 60299653d4eSeschrock return (NULL); 60399653d4eSeschrock } 60499653d4eSeschrock 605bde3d612SSimon Klinkert hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "rF"); 60699653d4eSeschrock 6074445fffbSMatthew Ahrens if (libzfs_core_init() != 0) { 6084445fffbSMatthew Ahrens (void) close(hdl->libzfs_fd); 6094445fffbSMatthew Ahrens (void) fclose(hdl->libzfs_mnttab); 6104445fffbSMatthew Ahrens (void) fclose(hdl->libzfs_sharetab); 6114445fffbSMatthew Ahrens free(hdl); 6124445fffbSMatthew Ahrens return (NULL); 6134445fffbSMatthew Ahrens } 6144445fffbSMatthew Ahrens 61591ebeef5Sahrens zfs_prop_init(); 616990b4856Slling zpool_prop_init(); 617ad135b5dSChristopher Siden zpool_feature_init(); 618b2634b9cSEric Taylor libzfs_mnttab_init(hdl); 61991ebeef5Sahrens 620dfc11533SChris Williamson if (getenv("ZFS_PROP_DEBUG") != NULL) { 621dfc11533SChris Williamson hdl->libzfs_prop_debug = B_TRUE; 622dfc11533SChris Williamson } 623dfc11533SChris Williamson 62499653d4eSeschrock return (hdl); 62599653d4eSeschrock } 62699653d4eSeschrock 62799653d4eSeschrock void 62899653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl) 62999653d4eSeschrock { 63099653d4eSeschrock (void) close(hdl->libzfs_fd); 63199653d4eSeschrock if (hdl->libzfs_mnttab) 63299653d4eSeschrock (void) fclose(hdl->libzfs_mnttab); 63399653d4eSeschrock if (hdl->libzfs_sharetab) 63499653d4eSeschrock (void) fclose(hdl->libzfs_sharetab); 63567331909Sdougm zfs_uninit_libshare(hdl); 63629ab75c9Srm zpool_free_handles(hdl); 637069f55e2SEric Schrock libzfs_fru_clear(hdl, B_TRUE); 63899653d4eSeschrock namespace_clear(hdl); 639b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 6404445fffbSMatthew Ahrens libzfs_core_fini(); 64199653d4eSeschrock free(hdl); 64299653d4eSeschrock } 64399653d4eSeschrock 64499653d4eSeschrock libzfs_handle_t * 64599653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp) 64699653d4eSeschrock { 64799653d4eSeschrock return (zhp->zpool_hdl); 64899653d4eSeschrock } 64999653d4eSeschrock 65099653d4eSeschrock libzfs_handle_t * 65199653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp) 65299653d4eSeschrock { 65399653d4eSeschrock return (zhp->zfs_hdl); 65499653d4eSeschrock } 655e9dbad6fSeschrock 656d5b5bb25SRich Morris zpool_handle_t * 657d5b5bb25SRich Morris zfs_get_pool_handle(const zfs_handle_t *zhp) 658d5b5bb25SRich Morris { 659d5b5bb25SRich Morris return (zhp->zpool_hdl); 660d5b5bb25SRich Morris } 661d5b5bb25SRich Morris 6625aba80dbSck /* 6635aba80dbSck * Given a name, determine whether or not it's a valid path 6645aba80dbSck * (starts with '/' or "./"). If so, walk the mnttab trying 6655aba80dbSck * to match the device number. If not, treat the path as an 666edb901aaSMarcel Telka * fs/vol/snap/bkmark name. 6675aba80dbSck */ 6685aba80dbSck zfs_handle_t * 6695aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6705aba80dbSck { 6715aba80dbSck struct stat64 statbuf; 6725aba80dbSck struct extmnttab entry; 6735aba80dbSck int ret; 6745aba80dbSck 6755aba80dbSck if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6765aba80dbSck /* 6775aba80dbSck * It's not a valid path, assume it's a name of type 'argtype'. 6785aba80dbSck */ 6795aba80dbSck return (zfs_open(hdl, path, argtype)); 6805aba80dbSck } 6815aba80dbSck 6825aba80dbSck if (stat64(path, &statbuf) != 0) { 6835aba80dbSck (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6845aba80dbSck return (NULL); 6855aba80dbSck } 6865aba80dbSck 6875aba80dbSck rewind(hdl->libzfs_mnttab); 6885aba80dbSck while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6895aba80dbSck if (makedevice(entry.mnt_major, entry.mnt_minor) == 6905aba80dbSck statbuf.st_dev) { 6915aba80dbSck break; 6925aba80dbSck } 6935aba80dbSck } 6945aba80dbSck if (ret != 0) { 6955aba80dbSck return (NULL); 6965aba80dbSck } 6975aba80dbSck 6985aba80dbSck if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6995aba80dbSck (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 7005aba80dbSck path); 7015aba80dbSck return (NULL); 7025aba80dbSck } 7035aba80dbSck 7045aba80dbSck return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 7055aba80dbSck } 7065aba80dbSck 707e9dbad6fSeschrock /* 708e9dbad6fSeschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 709e9dbad6fSeschrock * an ioctl(). 710e9dbad6fSeschrock */ 711e9dbad6fSeschrock int 712e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 713e9dbad6fSeschrock { 714e9dbad6fSeschrock if (len == 0) 7154b964adaSGeorge Wilson len = 16 * 1024; 716e9dbad6fSeschrock zc->zc_nvlist_dst_size = len; 71730925561SChris Williamson zc->zc_nvlist_dst = 71830925561SChris Williamson (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 71930925561SChris Williamson if (zc->zc_nvlist_dst == 0) 720e9dbad6fSeschrock return (-1); 721e9dbad6fSeschrock 722e9dbad6fSeschrock return (0); 723e9dbad6fSeschrock } 724e9dbad6fSeschrock 725e9dbad6fSeschrock /* 726e9dbad6fSeschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 727e9dbad6fSeschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 728e9dbad6fSeschrock * filled in by the kernel to indicate the actual required size. 729e9dbad6fSeschrock */ 730e9dbad6fSeschrock int 731e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 732e9dbad6fSeschrock { 733e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 73430925561SChris Williamson zc->zc_nvlist_dst = 73530925561SChris Williamson (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 73630925561SChris Williamson if (zc->zc_nvlist_dst == 0) 737e9dbad6fSeschrock return (-1); 738e9dbad6fSeschrock 739e9dbad6fSeschrock return (0); 740e9dbad6fSeschrock } 741e9dbad6fSeschrock 742e9dbad6fSeschrock /* 743a2eea2e1Sahrens * Called to free the src and dst nvlists stored in the command structure. 744e9dbad6fSeschrock */ 745e9dbad6fSeschrock void 746e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc) 747e9dbad6fSeschrock { 748990b4856Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 749e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 750e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 75130925561SChris Williamson zc->zc_nvlist_conf = NULL; 75230925561SChris Williamson zc->zc_nvlist_src = NULL; 75330925561SChris Williamson zc->zc_nvlist_dst = NULL; 754e9dbad6fSeschrock } 755e9dbad6fSeschrock 756990b4856Slling static int 757990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 758990b4856Slling nvlist_t *nvl) 759e9dbad6fSeschrock { 760e9dbad6fSeschrock char *packed; 761e9dbad6fSeschrock size_t len; 762e9dbad6fSeschrock 763e9dbad6fSeschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 764e9dbad6fSeschrock 765e9dbad6fSeschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 766e9dbad6fSeschrock return (-1); 767e9dbad6fSeschrock 768e9dbad6fSeschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 769e9dbad6fSeschrock 770990b4856Slling *outnv = (uint64_t)(uintptr_t)packed; 771990b4856Slling *outlen = len; 772e9dbad6fSeschrock 773e9dbad6fSeschrock return (0); 774e9dbad6fSeschrock } 775e9dbad6fSeschrock 776990b4856Slling int 777990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 778990b4856Slling { 779990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 780990b4856Slling &zc->zc_nvlist_conf_size, nvl)); 781990b4856Slling } 782990b4856Slling 783990b4856Slling int 784990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 785990b4856Slling { 786990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 787990b4856Slling &zc->zc_nvlist_src_size, nvl)); 788990b4856Slling } 789990b4856Slling 790e9dbad6fSeschrock /* 791e9dbad6fSeschrock * Unpacks an nvlist from the ZFS ioctl command structure. 792e9dbad6fSeschrock */ 793e9dbad6fSeschrock int 794e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 795e9dbad6fSeschrock { 796e9dbad6fSeschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 797e9dbad6fSeschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 798e9dbad6fSeschrock return (no_memory(hdl)); 799e9dbad6fSeschrock 800e9dbad6fSeschrock return (0); 801e9dbad6fSeschrock } 802b1b8ab34Slling 803990b4856Slling int 804990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 805990b4856Slling { 8064445fffbSMatthew Ahrens return (ioctl(hdl->libzfs_fd, request, zc)); 807990b4856Slling } 808990b4856Slling 809990b4856Slling /* 810990b4856Slling * ================================================================ 811990b4856Slling * API shared by zfs and zpool property management 812990b4856Slling * ================================================================ 813990b4856Slling */ 814990b4856Slling 815b1b8ab34Slling static void 816990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 817b1b8ab34Slling { 818990b4856Slling zprop_list_t *pl = cbp->cb_proplist; 819b1b8ab34Slling int i; 820b1b8ab34Slling char *title; 821b1b8ab34Slling size_t len; 822b1b8ab34Slling 823b1b8ab34Slling cbp->cb_first = B_FALSE; 824b1b8ab34Slling if (cbp->cb_scripted) 825b1b8ab34Slling return; 826b1b8ab34Slling 827b1b8ab34Slling /* 828b1b8ab34Slling * Start with the length of the column headers. 829b1b8ab34Slling */ 830b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 831b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 832b1b8ab34Slling "PROPERTY")); 833b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 834b1b8ab34Slling "VALUE")); 83592241e0bSTom Erickson cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, 83692241e0bSTom Erickson "RECEIVED")); 837b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 838b1b8ab34Slling "SOURCE")); 839b1b8ab34Slling 840deb8317bSMark J Musante /* first property is always NAME */ 841deb8317bSMark J Musante assert(cbp->cb_proplist->pl_prop == 842deb8317bSMark J Musante ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 843deb8317bSMark J Musante 844b1b8ab34Slling /* 845b1b8ab34Slling * Go through and calculate the widths for each column. For the 846b1b8ab34Slling * 'source' column, we kludge it up by taking the worst-case scenario of 847b1b8ab34Slling * inheriting from the longest name. This is acceptable because in the 848b1b8ab34Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 849b1b8ab34Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 85092241e0bSTom Erickson * if the name of the property is much longer than any values we find. 851b1b8ab34Slling */ 852b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 853b1b8ab34Slling /* 854b1b8ab34Slling * 'PROPERTY' column 855b1b8ab34Slling */ 856990b4856Slling if (pl->pl_prop != ZPROP_INVAL) { 857990b4856Slling const char *propname = (type == ZFS_TYPE_POOL) ? 858990b4856Slling zpool_prop_to_name(pl->pl_prop) : 859990b4856Slling zfs_prop_to_name(pl->pl_prop); 860990b4856Slling 861990b4856Slling len = strlen(propname); 862b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 863b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 864b1b8ab34Slling } else { 865b1b8ab34Slling len = strlen(pl->pl_user_prop); 866b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 867b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 868b1b8ab34Slling } 869b1b8ab34Slling 870b1b8ab34Slling /* 871deb8317bSMark J Musante * 'VALUE' column. The first property is always the 'name' 872deb8317bSMark J Musante * property that was tacked on either by /sbin/zfs's 873deb8317bSMark J Musante * zfs_do_get() or when calling zprop_expand_list(), so we 874deb8317bSMark J Musante * ignore its width. If the user specified the name property 875deb8317bSMark J Musante * to display, then it will be later in the list in any case. 876b1b8ab34Slling */ 877deb8317bSMark J Musante if (pl != cbp->cb_proplist && 878b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 879b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 880b1b8ab34Slling 88192241e0bSTom Erickson /* 'RECEIVED' column. */ 88292241e0bSTom Erickson if (pl != cbp->cb_proplist && 88392241e0bSTom Erickson pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) 88492241e0bSTom Erickson cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; 88592241e0bSTom Erickson 886b1b8ab34Slling /* 887b1b8ab34Slling * 'NAME' and 'SOURCE' columns 888b1b8ab34Slling */ 889990b4856Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 890990b4856Slling ZFS_PROP_NAME) && 891b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 892b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 893b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 894b1b8ab34Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 895b1b8ab34Slling } 896b1b8ab34Slling } 897b1b8ab34Slling 898b1b8ab34Slling /* 899b1b8ab34Slling * Now go through and print the headers. 900b1b8ab34Slling */ 90192241e0bSTom Erickson for (i = 0; i < ZFS_GET_NCOLS; i++) { 902b1b8ab34Slling switch (cbp->cb_columns[i]) { 903b1b8ab34Slling case GET_COL_NAME: 904b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "NAME"); 905b1b8ab34Slling break; 906b1b8ab34Slling case GET_COL_PROPERTY: 907b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 908b1b8ab34Slling break; 909b1b8ab34Slling case GET_COL_VALUE: 910b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 911b1b8ab34Slling break; 91292241e0bSTom Erickson case GET_COL_RECVD: 91392241e0bSTom Erickson title = dgettext(TEXT_DOMAIN, "RECEIVED"); 91492241e0bSTom Erickson break; 915b1b8ab34Slling case GET_COL_SOURCE: 916b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 917b1b8ab34Slling break; 918b1b8ab34Slling default: 919b1b8ab34Slling title = NULL; 920b1b8ab34Slling } 921b1b8ab34Slling 922b1b8ab34Slling if (title != NULL) { 92392241e0bSTom Erickson if (i == (ZFS_GET_NCOLS - 1) || 92492241e0bSTom Erickson cbp->cb_columns[i + 1] == GET_COL_NONE) 925b1b8ab34Slling (void) printf("%s", title); 926b1b8ab34Slling else 927b1b8ab34Slling (void) printf("%-*s ", 928b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 929b1b8ab34Slling title); 930b1b8ab34Slling } 931b1b8ab34Slling } 932b1b8ab34Slling (void) printf("\n"); 933b1b8ab34Slling } 934b1b8ab34Slling 935b1b8ab34Slling /* 936b1b8ab34Slling * Display a single line of output, according to the settings in the callback 937b1b8ab34Slling * structure. 938b1b8ab34Slling */ 939b1b8ab34Slling void 940990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 941990b4856Slling const char *propname, const char *value, zprop_source_t sourcetype, 94292241e0bSTom Erickson const char *source, const char *recvd_value) 943b1b8ab34Slling { 944b1b8ab34Slling int i; 945f83b46baSPaul Dagnelie const char *str = NULL; 946b1b8ab34Slling char buf[128]; 947b1b8ab34Slling 948b1b8ab34Slling /* 949b1b8ab34Slling * Ignore those source types that the user has chosen to ignore. 950b1b8ab34Slling */ 951b1b8ab34Slling if ((sourcetype & cbp->cb_sources) == 0) 952b1b8ab34Slling return; 953b1b8ab34Slling 954b1b8ab34Slling if (cbp->cb_first) 955990b4856Slling zprop_print_headers(cbp, cbp->cb_type); 956b1b8ab34Slling 95792241e0bSTom Erickson for (i = 0; i < ZFS_GET_NCOLS; i++) { 958b1b8ab34Slling switch (cbp->cb_columns[i]) { 959b1b8ab34Slling case GET_COL_NAME: 960b1b8ab34Slling str = name; 961b1b8ab34Slling break; 962b1b8ab34Slling 963b1b8ab34Slling case GET_COL_PROPERTY: 964b1b8ab34Slling str = propname; 965b1b8ab34Slling break; 966b1b8ab34Slling 967b1b8ab34Slling case GET_COL_VALUE: 968b1b8ab34Slling str = value; 969b1b8ab34Slling break; 970b1b8ab34Slling 971b1b8ab34Slling case GET_COL_SOURCE: 972b1b8ab34Slling switch (sourcetype) { 973990b4856Slling case ZPROP_SRC_NONE: 974b1b8ab34Slling str = "-"; 975b1b8ab34Slling break; 976b1b8ab34Slling 977990b4856Slling case ZPROP_SRC_DEFAULT: 978b1b8ab34Slling str = "default"; 979b1b8ab34Slling break; 980b1b8ab34Slling 981990b4856Slling case ZPROP_SRC_LOCAL: 982b1b8ab34Slling str = "local"; 983b1b8ab34Slling break; 984b1b8ab34Slling 985990b4856Slling case ZPROP_SRC_TEMPORARY: 986b1b8ab34Slling str = "temporary"; 987b1b8ab34Slling break; 988b1b8ab34Slling 989990b4856Slling case ZPROP_SRC_INHERITED: 990b1b8ab34Slling (void) snprintf(buf, sizeof (buf), 991b1b8ab34Slling "inherited from %s", source); 992b1b8ab34Slling str = buf; 993b1b8ab34Slling break; 99492241e0bSTom Erickson case ZPROP_SRC_RECEIVED: 99592241e0bSTom Erickson str = "received"; 99692241e0bSTom Erickson break; 99788f61deeSIgor Kozhukhov 99888f61deeSIgor Kozhukhov default: 99988f61deeSIgor Kozhukhov str = NULL; 100088f61deeSIgor Kozhukhov assert(!"unhandled zprop_source_t"); 1001b1b8ab34Slling } 1002b1b8ab34Slling break; 1003b1b8ab34Slling 100492241e0bSTom Erickson case GET_COL_RECVD: 100592241e0bSTom Erickson str = (recvd_value == NULL ? "-" : recvd_value); 100692241e0bSTom Erickson break; 100792241e0bSTom Erickson 1008b1b8ab34Slling default: 1009b1b8ab34Slling continue; 1010b1b8ab34Slling } 1011b1b8ab34Slling 101292241e0bSTom Erickson if (cbp->cb_columns[i + 1] == GET_COL_NONE) 1013b1b8ab34Slling (void) printf("%s", str); 1014b1b8ab34Slling else if (cbp->cb_scripted) 1015b1b8ab34Slling (void) printf("%s\t", str); 1016b1b8ab34Slling else 1017b1b8ab34Slling (void) printf("%-*s ", 1018b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 1019b1b8ab34Slling str); 1020b1b8ab34Slling } 1021b1b8ab34Slling 1022b1b8ab34Slling (void) printf("\n"); 1023b1b8ab34Slling } 1024ecd6cf80Smarks 1025990b4856Slling /* 1026990b4856Slling * Given a numeric suffix, convert the value into a number of bits that the 1027990b4856Slling * resulting value must be shifted. 1028990b4856Slling */ 1029990b4856Slling static int 1030990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf) 1031990b4856Slling { 1032990b4856Slling const char *ends = "BKMGTPEZ"; 1033990b4856Slling int i; 1034990b4856Slling 1035990b4856Slling if (buf[0] == '\0') 1036990b4856Slling return (0); 1037990b4856Slling for (i = 0; i < strlen(ends); i++) { 1038990b4856Slling if (toupper(buf[0]) == ends[i]) 1039990b4856Slling break; 1040990b4856Slling } 1041990b4856Slling if (i == strlen(ends)) { 1042990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1043990b4856Slling "invalid numeric suffix '%s'"), buf); 1044990b4856Slling return (-1); 1045990b4856Slling } 1046990b4856Slling 1047990b4856Slling /* 1048990b4856Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 1049990b4856Slling * allow 'BB' - that's just weird. 1050990b4856Slling */ 1051990b4856Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 1052990b4856Slling toupper(buf[0]) != 'B')) 1053990b4856Slling return (10*i); 1054990b4856Slling 1055990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1056990b4856Slling "invalid numeric suffix '%s'"), buf); 1057990b4856Slling return (-1); 1058990b4856Slling } 1059990b4856Slling 1060990b4856Slling /* 1061990b4856Slling * Convert a string of the form '100G' into a real number. Used when setting 1062990b4856Slling * properties or creating a volume. 'buf' is used to place an extended error 1063990b4856Slling * message for the caller to use. 1064990b4856Slling */ 1065ecd6cf80Smarks int 1066990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1067ecd6cf80Smarks { 1068990b4856Slling char *end; 1069990b4856Slling int shift; 1070ecd6cf80Smarks 1071990b4856Slling *num = 0; 1072990b4856Slling 1073990b4856Slling /* Check to see if this looks like a number. */ 1074990b4856Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1075990b4856Slling if (hdl) 1076990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1077990b4856Slling "bad numeric value '%s'"), value); 1078990b4856Slling return (-1); 1079ecd6cf80Smarks } 1080ecd6cf80Smarks 1081069f55e2SEric Schrock /* Rely on strtoull() to process the numeric portion. */ 1082990b4856Slling errno = 0; 1083f67f35c3SEric Schrock *num = strtoull(value, &end, 10); 1084990b4856Slling 1085990b4856Slling /* 1086990b4856Slling * Check for ERANGE, which indicates that the value is too large to fit 1087990b4856Slling * in a 64-bit value. 1088990b4856Slling */ 1089990b4856Slling if (errno == ERANGE) { 1090990b4856Slling if (hdl) 1091990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1092990b4856Slling "numeric value is too large")); 1093990b4856Slling return (-1); 1094990b4856Slling } 1095990b4856Slling 1096990b4856Slling /* 1097990b4856Slling * If we have a decimal value, then do the computation with floating 1098990b4856Slling * point arithmetic. Otherwise, use standard arithmetic. 1099990b4856Slling */ 1100990b4856Slling if (*end == '.') { 1101990b4856Slling double fval = strtod(value, &end); 1102990b4856Slling 1103990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1104990b4856Slling return (-1); 1105990b4856Slling 1106990b4856Slling fval *= pow(2, shift); 1107990b4856Slling 1108990b4856Slling if (fval > UINT64_MAX) { 1109990b4856Slling if (hdl) 1110990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1111990b4856Slling "numeric value is too large")); 1112990b4856Slling return (-1); 1113990b4856Slling } 1114990b4856Slling 1115990b4856Slling *num = (uint64_t)fval; 1116990b4856Slling } else { 1117990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1118990b4856Slling return (-1); 1119990b4856Slling 1120990b4856Slling /* Check for overflow */ 1121990b4856Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 1122990b4856Slling if (hdl) 1123990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1124990b4856Slling "numeric value is too large")); 1125990b4856Slling return (-1); 1126990b4856Slling } 1127990b4856Slling 1128990b4856Slling *num <<= shift; 1129990b4856Slling } 1130990b4856Slling 1131990b4856Slling return (0); 1132990b4856Slling } 1133990b4856Slling 1134990b4856Slling /* 1135990b4856Slling * Given a propname=value nvpair to set, parse any numeric properties 1136990b4856Slling * (index, boolean, etc) if they are specified as strings and add the 1137990b4856Slling * resulting nvpair to the returned nvlist. 1138990b4856Slling * 1139990b4856Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 1140990b4856Slling * We want the user to be able to ignore this fact and specify properties 1141990b4856Slling * as native values (numbers, for example) or as strings (to simplify 1142990b4856Slling * command line utilities). This also handles converting index types 1143990b4856Slling * (compression, checksum, etc) from strings to their on-disk index. 1144990b4856Slling */ 1145990b4856Slling int 1146990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1147990b4856Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1148990b4856Slling const char *errbuf) 1149990b4856Slling { 1150990b4856Slling data_type_t datatype = nvpair_type(elem); 1151990b4856Slling zprop_type_t proptype; 1152990b4856Slling const char *propname; 1153990b4856Slling char *value; 1154990b4856Slling boolean_t isnone = B_FALSE; 1155990b4856Slling 1156990b4856Slling if (type == ZFS_TYPE_POOL) { 1157990b4856Slling proptype = zpool_prop_get_type(prop); 1158990b4856Slling propname = zpool_prop_to_name(prop); 1159990b4856Slling } else { 1160990b4856Slling proptype = zfs_prop_get_type(prop); 1161990b4856Slling propname = zfs_prop_to_name(prop); 1162990b4856Slling } 1163990b4856Slling 1164990b4856Slling /* 1165990b4856Slling * Convert any properties to the internal DSL value types. 1166990b4856Slling */ 1167990b4856Slling *svalp = NULL; 1168990b4856Slling *ivalp = 0; 1169990b4856Slling 1170990b4856Slling switch (proptype) { 1171990b4856Slling case PROP_TYPE_STRING: 1172990b4856Slling if (datatype != DATA_TYPE_STRING) { 1173990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1174990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1175990b4856Slling goto error; 1176990b4856Slling } 1177990b4856Slling (void) nvpair_value_string(elem, svalp); 1178990b4856Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1179990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1180990b4856Slling "'%s' is too long"), nvpair_name(elem)); 1181990b4856Slling goto error; 1182990b4856Slling } 1183990b4856Slling break; 1184990b4856Slling 1185990b4856Slling case PROP_TYPE_NUMBER: 1186990b4856Slling if (datatype == DATA_TYPE_STRING) { 1187990b4856Slling (void) nvpair_value_string(elem, &value); 1188990b4856Slling if (strcmp(value, "none") == 0) { 1189990b4856Slling isnone = B_TRUE; 1190990b4856Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 1191990b4856Slling != 0) { 1192990b4856Slling goto error; 1193990b4856Slling } 1194990b4856Slling } else if (datatype == DATA_TYPE_UINT64) { 1195990b4856Slling (void) nvpair_value_uint64(elem, ivalp); 1196990b4856Slling } else { 1197990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1198990b4856Slling "'%s' must be a number"), nvpair_name(elem)); 1199990b4856Slling goto error; 1200990b4856Slling } 1201990b4856Slling 1202990b4856Slling /* 1203990b4856Slling * Quota special: force 'none' and don't allow 0. 1204990b4856Slling */ 1205a9799022Sck if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1206a9799022Sck (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1207990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1208a9799022Sck "use 'none' to disable quota/refquota")); 1209990b4856Slling goto error; 1210990b4856Slling } 1211a2afb611SJerry Jelinek 1212a2afb611SJerry Jelinek /* 1213a2afb611SJerry Jelinek * Special handling for "*_limit=none". In this case it's not 1214a2afb611SJerry Jelinek * 0 but UINT64_MAX. 1215a2afb611SJerry Jelinek */ 1216a2afb611SJerry Jelinek if ((type & ZFS_TYPE_DATASET) && isnone && 1217a2afb611SJerry Jelinek (prop == ZFS_PROP_FILESYSTEM_LIMIT || 1218a2afb611SJerry Jelinek prop == ZFS_PROP_SNAPSHOT_LIMIT)) { 1219a2afb611SJerry Jelinek *ivalp = UINT64_MAX; 1220a2afb611SJerry Jelinek } 1221990b4856Slling break; 1222990b4856Slling 1223990b4856Slling case PROP_TYPE_INDEX: 1224a9799022Sck if (datatype != DATA_TYPE_STRING) { 1225990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1226990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1227990b4856Slling goto error; 1228990b4856Slling } 1229990b4856Slling 1230a9799022Sck (void) nvpair_value_string(elem, &value); 1231a9799022Sck 1232990b4856Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1233990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1234990b4856Slling "'%s' must be one of '%s'"), propname, 1235990b4856Slling zprop_values(prop, type)); 1236990b4856Slling goto error; 1237990b4856Slling } 1238990b4856Slling break; 1239990b4856Slling 1240990b4856Slling default: 1241990b4856Slling abort(); 1242990b4856Slling } 1243990b4856Slling 1244990b4856Slling /* 1245990b4856Slling * Add the result to our return set of properties. 1246990b4856Slling */ 1247990b4856Slling if (*svalp != NULL) { 1248990b4856Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 1249990b4856Slling (void) no_memory(hdl); 1250990b4856Slling return (-1); 1251990b4856Slling } 1252990b4856Slling } else { 1253990b4856Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1254990b4856Slling (void) no_memory(hdl); 1255990b4856Slling return (-1); 1256990b4856Slling } 1257990b4856Slling } 1258990b4856Slling 1259990b4856Slling return (0); 1260990b4856Slling error: 1261990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1262990b4856Slling return (-1); 1263990b4856Slling } 1264990b4856Slling 126574e7dc98SMatthew Ahrens static int 126674e7dc98SMatthew Ahrens addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 126774e7dc98SMatthew Ahrens zfs_type_t type) 126874e7dc98SMatthew Ahrens { 126974e7dc98SMatthew Ahrens int prop; 127074e7dc98SMatthew Ahrens zprop_list_t *entry; 127174e7dc98SMatthew Ahrens 127274e7dc98SMatthew Ahrens prop = zprop_name_to_prop(propname, type); 127374e7dc98SMatthew Ahrens 127474e7dc98SMatthew Ahrens if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 127574e7dc98SMatthew Ahrens prop = ZPROP_INVAL; 127674e7dc98SMatthew Ahrens 127774e7dc98SMatthew Ahrens /* 127874e7dc98SMatthew Ahrens * When no property table entry can be found, return failure if 127974e7dc98SMatthew Ahrens * this is a pool property or if this isn't a user-defined 128074e7dc98SMatthew Ahrens * dataset property, 128174e7dc98SMatthew Ahrens */ 1282ad135b5dSChristopher Siden if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL && 1283ad135b5dSChristopher Siden !zpool_prop_feature(propname) && 1284ad135b5dSChristopher Siden !zpool_prop_unsupported(propname)) || 1285ad135b5dSChristopher Siden (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) && 1286ad135b5dSChristopher Siden !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) { 128774e7dc98SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 128874e7dc98SMatthew Ahrens "invalid property '%s'"), propname); 128974e7dc98SMatthew Ahrens return (zfs_error(hdl, EZFS_BADPROP, 129074e7dc98SMatthew Ahrens dgettext(TEXT_DOMAIN, "bad property list"))); 129174e7dc98SMatthew Ahrens } 129274e7dc98SMatthew Ahrens 129374e7dc98SMatthew Ahrens if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 129474e7dc98SMatthew Ahrens return (-1); 129574e7dc98SMatthew Ahrens 129674e7dc98SMatthew Ahrens entry->pl_prop = prop; 129774e7dc98SMatthew Ahrens if (prop == ZPROP_INVAL) { 1298ad135b5dSChristopher Siden if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == 1299ad135b5dSChristopher Siden NULL) { 130074e7dc98SMatthew Ahrens free(entry); 130174e7dc98SMatthew Ahrens return (-1); 130274e7dc98SMatthew Ahrens } 130374e7dc98SMatthew Ahrens entry->pl_width = strlen(propname); 130474e7dc98SMatthew Ahrens } else { 130574e7dc98SMatthew Ahrens entry->pl_width = zprop_width(prop, &entry->pl_fixed, 130674e7dc98SMatthew Ahrens type); 130774e7dc98SMatthew Ahrens } 130874e7dc98SMatthew Ahrens 130974e7dc98SMatthew Ahrens *listp = entry; 131074e7dc98SMatthew Ahrens 131174e7dc98SMatthew Ahrens return (0); 131274e7dc98SMatthew Ahrens } 131374e7dc98SMatthew Ahrens 1314990b4856Slling /* 1315990b4856Slling * Given a comma-separated list of properties, construct a property list 1316990b4856Slling * containing both user-defined and native properties. This function will 1317990b4856Slling * return a NULL list if 'all' is specified, which can later be expanded 1318990b4856Slling * by zprop_expand_list(). 1319990b4856Slling */ 1320990b4856Slling int 1321990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1322990b4856Slling zfs_type_t type) 1323990b4856Slling { 1324990b4856Slling *listp = NULL; 1325990b4856Slling 1326990b4856Slling /* 1327990b4856Slling * If 'all' is specified, return a NULL list. 1328990b4856Slling */ 1329990b4856Slling if (strcmp(props, "all") == 0) 1330990b4856Slling return (0); 1331990b4856Slling 1332990b4856Slling /* 1333990b4856Slling * If no props were specified, return an error. 1334990b4856Slling */ 1335990b4856Slling if (props[0] == '\0') { 1336990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1337990b4856Slling "no properties specified")); 1338990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1339990b4856Slling "bad property list"))); 1340990b4856Slling } 1341990b4856Slling 1342990b4856Slling /* 1343990b4856Slling * It would be nice to use getsubopt() here, but the inclusion of column 1344990b4856Slling * aliases makes this more effort than it's worth. 1345990b4856Slling */ 134674e7dc98SMatthew Ahrens while (*props != '\0') { 134774e7dc98SMatthew Ahrens size_t len; 134874e7dc98SMatthew Ahrens char *p; 134974e7dc98SMatthew Ahrens char c; 135074e7dc98SMatthew Ahrens 135174e7dc98SMatthew Ahrens if ((p = strchr(props, ',')) == NULL) { 135274e7dc98SMatthew Ahrens len = strlen(props); 135374e7dc98SMatthew Ahrens p = props + len; 1354990b4856Slling } else { 135574e7dc98SMatthew Ahrens len = p - props; 1356990b4856Slling } 1357990b4856Slling 1358990b4856Slling /* 1359990b4856Slling * Check for empty options. 1360990b4856Slling */ 1361990b4856Slling if (len == 0) { 1362990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1363990b4856Slling "empty property name")); 1364990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, 1365990b4856Slling dgettext(TEXT_DOMAIN, "bad property list"))); 1366990b4856Slling } 1367990b4856Slling 1368990b4856Slling /* 1369990b4856Slling * Check all regular property names. 1370990b4856Slling */ 137174e7dc98SMatthew Ahrens c = props[len]; 137274e7dc98SMatthew Ahrens props[len] = '\0'; 137374e7dc98SMatthew Ahrens 137474e7dc98SMatthew Ahrens if (strcmp(props, "space") == 0) { 137574e7dc98SMatthew Ahrens static char *spaceprops[] = { 137674e7dc98SMatthew Ahrens "name", "avail", "used", "usedbysnapshots", 137774e7dc98SMatthew Ahrens "usedbydataset", "usedbyrefreservation", 137874e7dc98SMatthew Ahrens "usedbychildren", NULL 137974e7dc98SMatthew Ahrens }; 138074e7dc98SMatthew Ahrens int i; 138174e7dc98SMatthew Ahrens 138274e7dc98SMatthew Ahrens for (i = 0; spaceprops[i]; i++) { 138374e7dc98SMatthew Ahrens if (addlist(hdl, spaceprops[i], listp, type)) 138474e7dc98SMatthew Ahrens return (-1); 138574e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1386990b4856Slling } 1387990b4856Slling } else { 138874e7dc98SMatthew Ahrens if (addlist(hdl, props, listp, type)) 138974e7dc98SMatthew Ahrens return (-1); 139074e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1391990b4856Slling } 1392990b4856Slling 139374e7dc98SMatthew Ahrens props = p; 1394990b4856Slling if (c == ',') 139574e7dc98SMatthew Ahrens props++; 1396990b4856Slling } 1397990b4856Slling 1398990b4856Slling return (0); 1399990b4856Slling } 1400990b4856Slling 1401990b4856Slling void 1402990b4856Slling zprop_free_list(zprop_list_t *pl) 1403990b4856Slling { 1404990b4856Slling zprop_list_t *next; 1405990b4856Slling 1406990b4856Slling while (pl != NULL) { 1407990b4856Slling next = pl->pl_next; 1408990b4856Slling free(pl->pl_user_prop); 1409990b4856Slling free(pl); 1410990b4856Slling pl = next; 1411990b4856Slling } 1412990b4856Slling } 1413990b4856Slling 1414990b4856Slling typedef struct expand_data { 1415990b4856Slling zprop_list_t **last; 1416990b4856Slling libzfs_handle_t *hdl; 1417990b4856Slling zfs_type_t type; 1418990b4856Slling } expand_data_t; 1419990b4856Slling 1420990b4856Slling int 1421990b4856Slling zprop_expand_list_cb(int prop, void *cb) 1422990b4856Slling { 1423990b4856Slling zprop_list_t *entry; 1424990b4856Slling expand_data_t *edp = cb; 1425990b4856Slling 1426990b4856Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1427990b4856Slling return (ZPROP_INVAL); 1428990b4856Slling 1429990b4856Slling entry->pl_prop = prop; 1430990b4856Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1431990b4856Slling entry->pl_all = B_TRUE; 1432990b4856Slling 1433990b4856Slling *(edp->last) = entry; 1434990b4856Slling edp->last = &entry->pl_next; 1435990b4856Slling 1436990b4856Slling return (ZPROP_CONT); 1437990b4856Slling } 1438990b4856Slling 1439990b4856Slling int 1440990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1441990b4856Slling { 1442990b4856Slling zprop_list_t *entry; 1443990b4856Slling zprop_list_t **last; 1444990b4856Slling expand_data_t exp; 1445990b4856Slling 1446990b4856Slling if (*plp == NULL) { 1447990b4856Slling /* 1448990b4856Slling * If this is the very first time we've been called for an 'all' 1449990b4856Slling * specification, expand the list to include all native 1450990b4856Slling * properties. 1451990b4856Slling */ 1452990b4856Slling last = plp; 1453990b4856Slling 1454990b4856Slling exp.last = last; 1455990b4856Slling exp.hdl = hdl; 1456990b4856Slling exp.type = type; 1457990b4856Slling 1458990b4856Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1459990b4856Slling B_FALSE, type) == ZPROP_INVAL) 1460990b4856Slling return (-1); 1461990b4856Slling 1462990b4856Slling /* 1463990b4856Slling * Add 'name' to the beginning of the list, which is handled 1464990b4856Slling * specially. 1465990b4856Slling */ 1466990b4856Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1467990b4856Slling return (-1); 1468990b4856Slling 1469990b4856Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1470990b4856Slling ZFS_PROP_NAME; 1471990b4856Slling entry->pl_width = zprop_width(entry->pl_prop, 1472990b4856Slling &entry->pl_fixed, type); 1473990b4856Slling entry->pl_all = B_TRUE; 1474990b4856Slling entry->pl_next = *plp; 1475990b4856Slling *plp = entry; 1476990b4856Slling } 1477990b4856Slling return (0); 1478990b4856Slling } 1479990b4856Slling 1480990b4856Slling int 1481990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1482990b4856Slling zfs_type_t type) 1483990b4856Slling { 1484990b4856Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 1485ecd6cf80Smarks } 14862bcf0248SMax Grossman 14872bcf0248SMax Grossman /* 14882bcf0248SMax Grossman * zfs_get_hole_count retrieves the number of holes (blocks which are 14892bcf0248SMax Grossman * zero-filled) in the specified file using the _FIO_COUNT_FILLED ioctl. It 14902bcf0248SMax Grossman * also optionally fetches the block size when bs is non-NULL. With hole count 14912bcf0248SMax Grossman * and block size the full space consumed by the holes of a file can be 14922bcf0248SMax Grossman * calculated. 14932bcf0248SMax Grossman * 14942bcf0248SMax Grossman * On success, zero is returned, the count argument is set to the 14952bcf0248SMax Grossman * number of holes, and the bs argument is set to the block size (if it is 14962bcf0248SMax Grossman * not NULL). On error, a non-zero errno is returned and the values in count 14972bcf0248SMax Grossman * and bs are undefined. 14982bcf0248SMax Grossman */ 14992bcf0248SMax Grossman int 15009a686fbcSPaul Dagnelie zfs_get_hole_count(const char *path, uint64_t *count, uint64_t *bs) 15019a686fbcSPaul Dagnelie { 15022bcf0248SMax Grossman int fd, err; 15032bcf0248SMax Grossman struct stat64 ss; 15042bcf0248SMax Grossman uint64_t fill; 15052bcf0248SMax Grossman 15062bcf0248SMax Grossman fd = open(path, O_RDONLY | O_LARGEFILE); 15072bcf0248SMax Grossman if (fd == -1) 15082bcf0248SMax Grossman return (errno); 15092bcf0248SMax Grossman 15102bcf0248SMax Grossman if (ioctl(fd, _FIO_COUNT_FILLED, &fill) == -1) { 15112bcf0248SMax Grossman err = errno; 15122bcf0248SMax Grossman (void) close(fd); 15132bcf0248SMax Grossman return (err); 15142bcf0248SMax Grossman } 15152bcf0248SMax Grossman 15162bcf0248SMax Grossman if (fstat64(fd, &ss) == -1) { 15172bcf0248SMax Grossman err = errno; 15182bcf0248SMax Grossman (void) close(fd); 15192bcf0248SMax Grossman return (err); 15202bcf0248SMax Grossman } 15212bcf0248SMax Grossman 15222bcf0248SMax Grossman *count = (ss.st_size + ss.st_blksize - 1) / ss.st_blksize - fill; 15232bcf0248SMax Grossman VERIFY3S(*count, >=, 0); 15242bcf0248SMax Grossman if (bs != NULL) { 15252bcf0248SMax Grossman *bs = ss.st_blksize; 15262bcf0248SMax Grossman } 15272bcf0248SMax Grossman 15282bcf0248SMax Grossman if (close(fd) == -1) { 15292bcf0248SMax Grossman return (errno); 15302bcf0248SMax Grossman } 15312bcf0248SMax Grossman return (0); 15322bcf0248SMax Grossman } 1533