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 /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25fa9e4066Sahrens /* 26fa9e4066Sahrens * Internal utility routines for the ZFS library. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <errno.h> 30fa9e4066Sahrens #include <fcntl.h> 31fa9e4066Sahrens #include <libintl.h> 32fa9e4066Sahrens #include <stdarg.h> 33fa9e4066Sahrens #include <stdio.h> 34fa9e4066Sahrens #include <stdlib.h> 35fa9e4066Sahrens #include <strings.h> 36fa9e4066Sahrens #include <unistd.h> 37990b4856Slling #include <ctype.h> 38990b4856Slling #include <math.h> 39fa9e4066Sahrens #include <sys/mnttab.h> 405aba80dbSck #include <sys/mntent.h> 415aba80dbSck #include <sys/types.h> 42fa9e4066Sahrens 43fa9e4066Sahrens #include <libzfs.h> 44fa9e4066Sahrens 45fa9e4066Sahrens #include "libzfs_impl.h" 4691ebeef5Sahrens #include "zfs_prop.h" 47fa9e4066Sahrens 4899653d4eSeschrock int 4999653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl) 5099653d4eSeschrock { 5199653d4eSeschrock return (hdl->libzfs_error); 5299653d4eSeschrock } 53fa9e4066Sahrens 5499653d4eSeschrock const char * 5599653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl) 5699653d4eSeschrock { 5799653d4eSeschrock return (hdl->libzfs_action); 5899653d4eSeschrock } 59fa9e4066Sahrens 6099653d4eSeschrock const char * 6199653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl) 6299653d4eSeschrock { 6399653d4eSeschrock if (hdl->libzfs_desc[0] != '\0') 6499653d4eSeschrock return (hdl->libzfs_desc); 6599653d4eSeschrock 6699653d4eSeschrock switch (hdl->libzfs_error) { 6799653d4eSeschrock case EZFS_NOMEM: 6899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of memory")); 6999653d4eSeschrock case EZFS_BADPROP: 7099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid property value")); 7199653d4eSeschrock case EZFS_PROPREADONLY: 72*f9af39baSGeorge Wilson return (dgettext(TEXT_DOMAIN, "read-only property")); 7399653d4eSeschrock case EZFS_PROPTYPE: 7499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 7599653d4eSeschrock "datasets of this type")); 7699653d4eSeschrock case EZFS_PROPNONINHERIT: 7799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 7899653d4eSeschrock case EZFS_PROPSPACE: 7999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 8099653d4eSeschrock case EZFS_BADTYPE: 8199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation not applicable to " 8299653d4eSeschrock "datasets of this type")); 8399653d4eSeschrock case EZFS_BUSY: 8499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 8599653d4eSeschrock case EZFS_EXISTS: 8699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 8799653d4eSeschrock case EZFS_NOENT: 8899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 8999653d4eSeschrock case EZFS_BADSTREAM: 9099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 9199653d4eSeschrock case EZFS_DSREADONLY: 92*f9af39baSGeorge Wilson return (dgettext(TEXT_DOMAIN, "dataset is read-only")); 9399653d4eSeschrock case EZFS_VOLTOOBIG: 9499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 9599653d4eSeschrock "this system")); 9699653d4eSeschrock case EZFS_INVALIDNAME: 9799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid name")); 9899653d4eSeschrock case EZFS_BADRESTORE: 9999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unable to restore to " 10099653d4eSeschrock "destination")); 10199653d4eSeschrock case EZFS_BADBACKUP: 10299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "backup failed")); 10399653d4eSeschrock case EZFS_BADTARGET: 10499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 10599653d4eSeschrock case EZFS_NODEVICE: 10699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no such device in pool")); 10799653d4eSeschrock case EZFS_BADDEV: 10899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid device")); 10999653d4eSeschrock case EZFS_NOREPLICAS: 11099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "no valid replicas")); 11199653d4eSeschrock case EZFS_RESILVERING: 11299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "currently resilvering")); 11399653d4eSeschrock case EZFS_BADVERSION: 11499653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unsupported version")); 11599653d4eSeschrock case EZFS_POOLUNAVAIL: 11699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 11799653d4eSeschrock case EZFS_DEVOVERFLOW: 11899653d4eSeschrock return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 11999653d4eSeschrock case EZFS_BADPATH: 12099653d4eSeschrock return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 12199653d4eSeschrock case EZFS_CROSSTARGET: 12299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 12399653d4eSeschrock "pools")); 12499653d4eSeschrock case EZFS_ZONED: 12599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 12699653d4eSeschrock case EZFS_MOUNTFAILED: 12799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "mount failed")); 12899653d4eSeschrock case EZFS_UMOUNTFAILED: 12999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "umount failed")); 130f3861e1aSahl case EZFS_UNSHARENFSFAILED: 13199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 132f3861e1aSahl case EZFS_SHARENFSFAILED: 13399653d4eSeschrock return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 134da6c28aaSamw case EZFS_UNSHARESMBFAILED: 135da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 136da6c28aaSamw case EZFS_SHARESMBFAILED: 137da6c28aaSamw return (dgettext(TEXT_DOMAIN, "smb add share failed")); 13899653d4eSeschrock case EZFS_PERM: 13999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "permission denied")); 14099653d4eSeschrock case EZFS_NOSPC: 14199653d4eSeschrock return (dgettext(TEXT_DOMAIN, "out of space")); 1426e27f868SSam Falkner case EZFS_FAULT: 1436e27f868SSam Falkner return (dgettext(TEXT_DOMAIN, "bad address")); 14499653d4eSeschrock case EZFS_IO: 14599653d4eSeschrock return (dgettext(TEXT_DOMAIN, "I/O error")); 14699653d4eSeschrock case EZFS_INTR: 14799653d4eSeschrock return (dgettext(TEXT_DOMAIN, "signal received")); 14899653d4eSeschrock case EZFS_ISSPARE: 14999653d4eSeschrock return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 15099653d4eSeschrock "spare")); 15199653d4eSeschrock case EZFS_INVALCONFIG: 15299653d4eSeschrock return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 1533bb79becSeschrock case EZFS_RECURSIVE: 1543bb79becSeschrock return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 15506eeb2adSek case EZFS_NOHISTORY: 15606eeb2adSek return (dgettext(TEXT_DOMAIN, "no history available")); 157b1b8ab34Slling case EZFS_POOLPROPS: 158b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "failed to retrieve " 159b1b8ab34Slling "pool properties")); 160b1b8ab34Slling case EZFS_POOL_NOTSUP: 161b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "operation not supported " 162b1b8ab34Slling "on this type of pool")); 163b1b8ab34Slling case EZFS_POOL_INVALARG: 164b1b8ab34Slling return (dgettext(TEXT_DOMAIN, "invalid argument for " 165b1b8ab34Slling "this pool operation")); 166b7661cccSmmusante case EZFS_NAMETOOLONG: 167b7661cccSmmusante return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 1688488aeb5Staylor case EZFS_OPENFAILED: 1698488aeb5Staylor return (dgettext(TEXT_DOMAIN, "open failed")); 1708488aeb5Staylor case EZFS_NOCAP: 1718488aeb5Staylor return (dgettext(TEXT_DOMAIN, 1728488aeb5Staylor "disk capacity information could not be retrieved")); 1738488aeb5Staylor case EZFS_LABELFAILED: 1748488aeb5Staylor return (dgettext(TEXT_DOMAIN, "write of label failed")); 175ecd6cf80Smarks case EZFS_BADWHO: 176ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid user/group")); 177ecd6cf80Smarks case EZFS_BADPERM: 178ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission")); 179ecd6cf80Smarks case EZFS_BADPERMSET: 180ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 181ecd6cf80Smarks case EZFS_NODELEGATION: 182ecd6cf80Smarks return (dgettext(TEXT_DOMAIN, "delegated administration is " 183ecd6cf80Smarks "disabled on pool")); 1842f8aaab3Seschrock case EZFS_BADCACHE: 1852f8aaab3Seschrock return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 186fa94a07fSbrendan case EZFS_ISL2CACHE: 187fa94a07fSbrendan return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 188e7cbe64fSgw case EZFS_VDEVNOTSUP: 189e7cbe64fSgw return (dgettext(TEXT_DOMAIN, "vdev specification is not " 190e7cbe64fSgw "supported")); 19115e6edf1Sgw case EZFS_NOTSUP: 19215e6edf1Sgw return (dgettext(TEXT_DOMAIN, "operation not supported " 19315e6edf1Sgw "on this dataset")); 19489a89ebfSlling case EZFS_ACTIVE_SPARE: 19589a89ebfSlling return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 19689a89ebfSlling "device")); 197e6ca193dSGeorge Wilson case EZFS_UNPLAYED_LOGS: 198e6ca193dSGeorge Wilson return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 199e6ca193dSGeorge Wilson "logs")); 200842727c2SChris Kirby case EZFS_REFTAG_RELE: 201842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 202842727c2SChris Kirby case EZFS_REFTAG_HOLD: 203842727c2SChris Kirby return (dgettext(TEXT_DOMAIN, "tag already exists on this " 204842727c2SChris Kirby "dataset")); 205ca45db41SChris Kirby case EZFS_TAGTOOLONG: 206ca45db41SChris Kirby return (dgettext(TEXT_DOMAIN, "tag too long")); 2079e69d7d0SLori Alt case EZFS_PIPEFAILED: 2089e69d7d0SLori Alt return (dgettext(TEXT_DOMAIN, "pipe create failed")); 2099e69d7d0SLori Alt case EZFS_THREADCREATEFAILED: 2109e69d7d0SLori Alt return (dgettext(TEXT_DOMAIN, "thread create failed")); 2111195e687SMark J Musante case EZFS_POSTSPLIT_ONLINE: 2121195e687SMark J Musante return (dgettext(TEXT_DOMAIN, "disk was split from this pool " 2131195e687SMark J Musante "into a new one")); 2143f9d6ad7SLin Ling case EZFS_SCRUBBING: 2153f9d6ad7SLin Ling return (dgettext(TEXT_DOMAIN, "currently scrubbing; " 2163f9d6ad7SLin Ling "use 'zpool scrub -s' to cancel current scrub")); 2173f9d6ad7SLin Ling case EZFS_NO_SCRUB: 2183f9d6ad7SLin Ling return (dgettext(TEXT_DOMAIN, "there is no active scrub")); 21999d5e173STim Haley case EZFS_DIFF: 22099d5e173STim Haley return (dgettext(TEXT_DOMAIN, "unable to generate diffs")); 22199d5e173STim Haley case EZFS_DIFFDATA: 22299d5e173STim Haley return (dgettext(TEXT_DOMAIN, "invalid diff data")); 223*f9af39baSGeorge Wilson case EZFS_POOLREADONLY: 224*f9af39baSGeorge Wilson return (dgettext(TEXT_DOMAIN, "pool is read-only")); 22599653d4eSeschrock case EZFS_UNKNOWN: 22699653d4eSeschrock return (dgettext(TEXT_DOMAIN, "unknown error")); 22799653d4eSeschrock default: 228c08432ebSeschrock assert(hdl->libzfs_error == 0); 229c08432ebSeschrock return (dgettext(TEXT_DOMAIN, "no error")); 23099653d4eSeschrock } 23199653d4eSeschrock } 23299653d4eSeschrock 23399653d4eSeschrock /*PRINTFLIKE2*/ 234fa9e4066Sahrens void 23599653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 236fa9e4066Sahrens { 237fa9e4066Sahrens va_list ap; 238fa9e4066Sahrens 239fa9e4066Sahrens va_start(ap, fmt); 240fa9e4066Sahrens 24199653d4eSeschrock (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 24299653d4eSeschrock fmt, ap); 24399653d4eSeschrock hdl->libzfs_desc_active = 1; 24499653d4eSeschrock 24599653d4eSeschrock va_end(ap); 24699653d4eSeschrock } 24799653d4eSeschrock 24899653d4eSeschrock static void 24999653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 25099653d4eSeschrock { 25199653d4eSeschrock (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 25299653d4eSeschrock fmt, ap); 25399653d4eSeschrock hdl->libzfs_error = error; 25499653d4eSeschrock 25599653d4eSeschrock if (hdl->libzfs_desc_active) 25699653d4eSeschrock hdl->libzfs_desc_active = 0; 25799653d4eSeschrock else 25899653d4eSeschrock hdl->libzfs_desc[0] = '\0'; 25999653d4eSeschrock 26099653d4eSeschrock if (hdl->libzfs_printerr) { 26199653d4eSeschrock if (error == EZFS_UNKNOWN) { 26299653d4eSeschrock (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 26399653d4eSeschrock "error: %s\n"), libzfs_error_description(hdl)); 26499653d4eSeschrock abort(); 26599653d4eSeschrock } 26699653d4eSeschrock 26799653d4eSeschrock (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 268b1b8ab34Slling libzfs_error_description(hdl)); 26999653d4eSeschrock if (error == EZFS_NOMEM) 27099653d4eSeschrock exit(1); 271fa9e4066Sahrens } 27299653d4eSeschrock } 27399653d4eSeschrock 274ece3d9b3Slling int 275ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 276ece3d9b3Slling { 277ece3d9b3Slling return (zfs_error_fmt(hdl, error, "%s", msg)); 278ece3d9b3Slling } 279ece3d9b3Slling 28099653d4eSeschrock /*PRINTFLIKE3*/ 28199653d4eSeschrock int 282ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 28399653d4eSeschrock { 28499653d4eSeschrock va_list ap; 28599653d4eSeschrock 28699653d4eSeschrock va_start(ap, fmt); 28799653d4eSeschrock 28899653d4eSeschrock zfs_verror(hdl, error, fmt, ap); 289fa9e4066Sahrens 290fa9e4066Sahrens va_end(ap); 29199653d4eSeschrock 29299653d4eSeschrock return (-1); 293fa9e4066Sahrens } 294fa9e4066Sahrens 29599653d4eSeschrock static int 29699653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 29799653d4eSeschrock va_list ap) 29899653d4eSeschrock { 29999653d4eSeschrock switch (error) { 30099653d4eSeschrock case EPERM: 30199653d4eSeschrock case EACCES: 30299653d4eSeschrock zfs_verror(hdl, EZFS_PERM, fmt, ap); 30399653d4eSeschrock return (-1); 30499653d4eSeschrock 305ecd6cf80Smarks case ECANCELED: 306ecd6cf80Smarks zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 307ecd6cf80Smarks return (-1); 308ecd6cf80Smarks 30999653d4eSeschrock case EIO: 31099653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 31199653d4eSeschrock return (-1); 31299653d4eSeschrock 3136e27f868SSam Falkner case EFAULT: 3146e27f868SSam Falkner zfs_verror(hdl, EZFS_FAULT, fmt, ap); 3156e27f868SSam Falkner return (-1); 3166e27f868SSam Falkner 31799653d4eSeschrock case EINTR: 31899653d4eSeschrock zfs_verror(hdl, EZFS_INTR, fmt, ap); 31999653d4eSeschrock return (-1); 32099653d4eSeschrock } 32199653d4eSeschrock 32299653d4eSeschrock return (0); 32399653d4eSeschrock } 32499653d4eSeschrock 325ece3d9b3Slling int 326ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 327ece3d9b3Slling { 328ece3d9b3Slling return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 329ece3d9b3Slling } 330ece3d9b3Slling 33199653d4eSeschrock /*PRINTFLIKE3*/ 33299653d4eSeschrock int 333ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 334fa9e4066Sahrens { 335fa9e4066Sahrens va_list ap; 336fa9e4066Sahrens 337fa9e4066Sahrens va_start(ap, fmt); 338fa9e4066Sahrens 33999653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 34099653d4eSeschrock va_end(ap); 34199653d4eSeschrock return (-1); 342fa9e4066Sahrens } 343fa9e4066Sahrens 34499653d4eSeschrock switch (error) { 34599653d4eSeschrock case ENXIO: 34697d9e3a6Sck case ENODEV: 34799653d4eSeschrock zfs_verror(hdl, EZFS_IO, fmt, ap); 34899653d4eSeschrock break; 34999653d4eSeschrock 35099653d4eSeschrock case ENOENT: 35199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35299653d4eSeschrock "dataset does not exist")); 35399653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 35499653d4eSeschrock break; 35599653d4eSeschrock 35699653d4eSeschrock case ENOSPC: 35799653d4eSeschrock case EDQUOT: 35899653d4eSeschrock zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 35999653d4eSeschrock return (-1); 36099653d4eSeschrock 36199653d4eSeschrock case EEXIST: 36299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36399653d4eSeschrock "dataset already exists")); 36499653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 36599653d4eSeschrock break; 36699653d4eSeschrock 36799653d4eSeschrock case EBUSY: 36899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36999653d4eSeschrock "dataset is busy")); 37099653d4eSeschrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 37199653d4eSeschrock break; 372ecd6cf80Smarks case EROFS: 373*f9af39baSGeorge Wilson zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 374ecd6cf80Smarks break; 375b7661cccSmmusante case ENAMETOOLONG: 376b7661cccSmmusante zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 377b7661cccSmmusante break; 37840ff3960Sck case ENOTSUP: 37940ff3960Sck zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 38040ff3960Sck break; 38154d692b7SGeorge Wilson case EAGAIN: 38254d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38354d692b7SGeorge Wilson "pool I/O is currently suspended")); 38454d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 38554d692b7SGeorge Wilson break; 38699653d4eSeschrock default: 38792241e0bSTom Erickson zfs_error_aux(hdl, strerror(error)); 38899653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 38999653d4eSeschrock break; 39099653d4eSeschrock } 39199653d4eSeschrock 39299653d4eSeschrock va_end(ap); 39399653d4eSeschrock return (-1); 394fa9e4066Sahrens } 395fa9e4066Sahrens 396ece3d9b3Slling int 397ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 398ece3d9b3Slling { 399ece3d9b3Slling return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 400ece3d9b3Slling } 401ece3d9b3Slling 40299653d4eSeschrock /*PRINTFLIKE3*/ 40399653d4eSeschrock int 404ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 405fa9e4066Sahrens { 40699653d4eSeschrock va_list ap; 40799653d4eSeschrock 40899653d4eSeschrock va_start(ap, fmt); 40999653d4eSeschrock 41099653d4eSeschrock if (zfs_common_error(hdl, error, fmt, ap) != 0) { 41199653d4eSeschrock va_end(ap); 41299653d4eSeschrock return (-1); 41399653d4eSeschrock } 41499653d4eSeschrock 41599653d4eSeschrock switch (error) { 41699653d4eSeschrock case ENODEV: 41799653d4eSeschrock zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 41899653d4eSeschrock break; 41999653d4eSeschrock 42099653d4eSeschrock case ENOENT: 421b1b8ab34Slling zfs_error_aux(hdl, 422b1b8ab34Slling dgettext(TEXT_DOMAIN, "no such pool or dataset")); 42399653d4eSeschrock zfs_verror(hdl, EZFS_NOENT, fmt, ap); 42499653d4eSeschrock break; 42599653d4eSeschrock 42699653d4eSeschrock case EEXIST: 42799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42899653d4eSeschrock "pool already exists")); 42999653d4eSeschrock zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 43099653d4eSeschrock break; 43199653d4eSeschrock 43299653d4eSeschrock case EBUSY: 43399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 43443afaaa8SEric Schrock zfs_verror(hdl, EZFS_BUSY, fmt, ap); 43599653d4eSeschrock break; 43699653d4eSeschrock 43799653d4eSeschrock case ENXIO: 43899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43999653d4eSeschrock "one or more devices is currently unavailable")); 44099653d4eSeschrock zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 44199653d4eSeschrock break; 44299653d4eSeschrock 44399653d4eSeschrock case ENAMETOOLONG: 44499653d4eSeschrock zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 44599653d4eSeschrock break; 44699653d4eSeschrock 447b1b8ab34Slling case ENOTSUP: 448b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 449b1b8ab34Slling break; 450b1b8ab34Slling 451b1b8ab34Slling case EINVAL: 452b1b8ab34Slling zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 453b1b8ab34Slling break; 454b1b8ab34Slling 455ecd6cf80Smarks case ENOSPC: 456ecd6cf80Smarks case EDQUOT: 457ecd6cf80Smarks zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 458ecd6cf80Smarks return (-1); 459*f9af39baSGeorge Wilson 46054d692b7SGeorge Wilson case EAGAIN: 46154d692b7SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46254d692b7SGeorge Wilson "pool I/O is currently suspended")); 46354d692b7SGeorge Wilson zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 46454d692b7SGeorge Wilson break; 465ecd6cf80Smarks 466*f9af39baSGeorge Wilson case EROFS: 467*f9af39baSGeorge Wilson zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 468*f9af39baSGeorge Wilson break; 469*f9af39baSGeorge Wilson 47099653d4eSeschrock default: 47199653d4eSeschrock zfs_error_aux(hdl, strerror(error)); 47299653d4eSeschrock zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 47399653d4eSeschrock } 47499653d4eSeschrock 47599653d4eSeschrock va_end(ap); 47699653d4eSeschrock return (-1); 477fa9e4066Sahrens } 478fa9e4066Sahrens 479fa9e4066Sahrens /* 480fa9e4066Sahrens * Display an out of memory error message and abort the current program. 481fa9e4066Sahrens */ 48299653d4eSeschrock int 48399653d4eSeschrock no_memory(libzfs_handle_t *hdl) 484fa9e4066Sahrens { 48599653d4eSeschrock return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 486fa9e4066Sahrens } 487fa9e4066Sahrens 488fa9e4066Sahrens /* 489fa9e4066Sahrens * A safe form of malloc() which will die if the allocation fails. 490fa9e4066Sahrens */ 491fa9e4066Sahrens void * 49299653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size) 493fa9e4066Sahrens { 494fa9e4066Sahrens void *data; 495fa9e4066Sahrens 496fa9e4066Sahrens if ((data = calloc(1, size)) == NULL) 49799653d4eSeschrock (void) no_memory(hdl); 498fa9e4066Sahrens 499fa9e4066Sahrens return (data); 500fa9e4066Sahrens } 501fa9e4066Sahrens 50299d5e173STim Haley /* 50399d5e173STim Haley * A safe form of asprintf() which will die if the allocation fails. 50499d5e173STim Haley */ 50599d5e173STim Haley /*PRINTFLIKE2*/ 50699d5e173STim Haley char * 50799d5e173STim Haley zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) 50899d5e173STim Haley { 50999d5e173STim Haley va_list ap; 51099d5e173STim Haley char *ret; 51199d5e173STim Haley int err; 51299d5e173STim Haley 51399d5e173STim Haley va_start(ap, fmt); 51499d5e173STim Haley 51599d5e173STim Haley err = vasprintf(&ret, fmt, ap); 51699d5e173STim Haley 51799d5e173STim Haley va_end(ap); 51899d5e173STim Haley 51999d5e173STim Haley if (err < 0) 52099d5e173STim Haley (void) no_memory(hdl); 52199d5e173STim Haley 52299d5e173STim Haley return (ret); 52399d5e173STim Haley } 52499d5e173STim Haley 525e9dbad6fSeschrock /* 526e9dbad6fSeschrock * A safe form of realloc(), which also zeroes newly allocated space. 527e9dbad6fSeschrock */ 528e9dbad6fSeschrock void * 529e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 530e9dbad6fSeschrock { 531e9dbad6fSeschrock void *ret; 532e9dbad6fSeschrock 533e9dbad6fSeschrock if ((ret = realloc(ptr, newsize)) == NULL) { 534e9dbad6fSeschrock (void) no_memory(hdl); 535e9dbad6fSeschrock return (NULL); 536e9dbad6fSeschrock } 537e9dbad6fSeschrock 538e9dbad6fSeschrock bzero((char *)ret + oldsize, (newsize - oldsize)); 539e9dbad6fSeschrock return (ret); 540e9dbad6fSeschrock } 541e9dbad6fSeschrock 542fa9e4066Sahrens /* 543fa9e4066Sahrens * A safe form of strdup() which will die if the allocation fails. 544fa9e4066Sahrens */ 545fa9e4066Sahrens char * 54699653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str) 547fa9e4066Sahrens { 548fa9e4066Sahrens char *ret; 549fa9e4066Sahrens 550fa9e4066Sahrens if ((ret = strdup(str)) == NULL) 55199653d4eSeschrock (void) no_memory(hdl); 552fa9e4066Sahrens 553fa9e4066Sahrens return (ret); 554fa9e4066Sahrens } 555fa9e4066Sahrens 556fa9e4066Sahrens /* 557fa9e4066Sahrens * Convert a number to an appropriately human-readable output. 558fa9e4066Sahrens */ 559fa9e4066Sahrens void 560fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen) 561fa9e4066Sahrens { 562fa9e4066Sahrens uint64_t n = num; 563fa9e4066Sahrens int index = 0; 564fa9e4066Sahrens char u; 565fa9e4066Sahrens 566fa9e4066Sahrens while (n >= 1024) { 5675c709891Seschrock n /= 1024; 568fa9e4066Sahrens index++; 569fa9e4066Sahrens } 570fa9e4066Sahrens 571fa9e4066Sahrens u = " KMGTPE"[index]; 572fa9e4066Sahrens 5735c709891Seschrock if (index == 0) { 574fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu", n); 5755c709891Seschrock } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 5765c709891Seschrock /* 5775c709891Seschrock * If this is an even multiple of the base, always display 5785c709891Seschrock * without any decimal precision. 5795c709891Seschrock */ 580fa9e4066Sahrens (void) snprintf(buf, buflen, "%llu%c", n, u); 5815c709891Seschrock } else { 5825c709891Seschrock /* 5835c709891Seschrock * We want to choose a precision that reflects the best choice 5845c709891Seschrock * for fitting in 5 characters. This can get rather tricky when 5855c709891Seschrock * we have numbers that are very close to an order of magnitude. 5865c709891Seschrock * For example, when displaying 10239 (which is really 9.999K), 5875c709891Seschrock * we want only a single place of precision for 10.0K. We could 5885c709891Seschrock * develop some complex heuristics for this, but it's much 5895c709891Seschrock * easier just to try each combination in turn. 5905c709891Seschrock */ 5915c709891Seschrock int i; 5925c709891Seschrock for (i = 2; i >= 0; i--) { 5933d7072f8Seschrock if (snprintf(buf, buflen, "%.*f%c", i, 5943d7072f8Seschrock (double)num / (1ULL << 10 * index), u) <= 5) 5955c709891Seschrock break; 5965c709891Seschrock } 5975c709891Seschrock } 598fa9e4066Sahrens } 59999653d4eSeschrock 60099653d4eSeschrock void 60199653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 60299653d4eSeschrock { 60399653d4eSeschrock hdl->libzfs_printerr = printerr; 60499653d4eSeschrock } 60599653d4eSeschrock 60699653d4eSeschrock libzfs_handle_t * 60799653d4eSeschrock libzfs_init(void) 60899653d4eSeschrock { 60999653d4eSeschrock libzfs_handle_t *hdl; 61099653d4eSeschrock 61199d5e173STim Haley if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { 61299653d4eSeschrock return (NULL); 61399653d4eSeschrock } 61499653d4eSeschrock 615c08432ebSeschrock if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 61699653d4eSeschrock free(hdl); 61799653d4eSeschrock return (NULL); 61899653d4eSeschrock } 61999653d4eSeschrock 62099653d4eSeschrock if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 62199653d4eSeschrock (void) close(hdl->libzfs_fd); 62299653d4eSeschrock free(hdl); 62399653d4eSeschrock return (NULL); 62499653d4eSeschrock } 62599653d4eSeschrock 62699653d4eSeschrock hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 62799653d4eSeschrock 62891ebeef5Sahrens zfs_prop_init(); 629990b4856Slling zpool_prop_init(); 630b2634b9cSEric Taylor libzfs_mnttab_init(hdl); 63191ebeef5Sahrens 63299653d4eSeschrock return (hdl); 63399653d4eSeschrock } 63499653d4eSeschrock 63599653d4eSeschrock void 63699653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl) 63799653d4eSeschrock { 63899653d4eSeschrock (void) close(hdl->libzfs_fd); 63999653d4eSeschrock if (hdl->libzfs_mnttab) 64099653d4eSeschrock (void) fclose(hdl->libzfs_mnttab); 64199653d4eSeschrock if (hdl->libzfs_sharetab) 64299653d4eSeschrock (void) fclose(hdl->libzfs_sharetab); 64367331909Sdougm zfs_uninit_libshare(hdl); 644ecd6cf80Smarks if (hdl->libzfs_log_str) 645ecd6cf80Smarks (void) free(hdl->libzfs_log_str); 64629ab75c9Srm zpool_free_handles(hdl); 647069f55e2SEric Schrock libzfs_fru_clear(hdl, B_TRUE); 64899653d4eSeschrock namespace_clear(hdl); 649b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 65099653d4eSeschrock free(hdl); 65199653d4eSeschrock } 65299653d4eSeschrock 65399653d4eSeschrock libzfs_handle_t * 65499653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp) 65599653d4eSeschrock { 65699653d4eSeschrock return (zhp->zpool_hdl); 65799653d4eSeschrock } 65899653d4eSeschrock 65999653d4eSeschrock libzfs_handle_t * 66099653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp) 66199653d4eSeschrock { 66299653d4eSeschrock return (zhp->zfs_hdl); 66399653d4eSeschrock } 664e9dbad6fSeschrock 665d5b5bb25SRich Morris zpool_handle_t * 666d5b5bb25SRich Morris zfs_get_pool_handle(const zfs_handle_t *zhp) 667d5b5bb25SRich Morris { 668d5b5bb25SRich Morris return (zhp->zpool_hdl); 669d5b5bb25SRich Morris } 670d5b5bb25SRich Morris 6715aba80dbSck /* 6725aba80dbSck * Given a name, determine whether or not it's a valid path 6735aba80dbSck * (starts with '/' or "./"). If so, walk the mnttab trying 6745aba80dbSck * to match the device number. If not, treat the path as an 6755aba80dbSck * fs/vol/snap name. 6765aba80dbSck */ 6775aba80dbSck zfs_handle_t * 6785aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 6795aba80dbSck { 6805aba80dbSck struct stat64 statbuf; 6815aba80dbSck struct extmnttab entry; 6825aba80dbSck int ret; 6835aba80dbSck 6845aba80dbSck if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 6855aba80dbSck /* 6865aba80dbSck * It's not a valid path, assume it's a name of type 'argtype'. 6875aba80dbSck */ 6885aba80dbSck return (zfs_open(hdl, path, argtype)); 6895aba80dbSck } 6905aba80dbSck 6915aba80dbSck if (stat64(path, &statbuf) != 0) { 6925aba80dbSck (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 6935aba80dbSck return (NULL); 6945aba80dbSck } 6955aba80dbSck 6965aba80dbSck rewind(hdl->libzfs_mnttab); 6975aba80dbSck while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 6985aba80dbSck if (makedevice(entry.mnt_major, entry.mnt_minor) == 6995aba80dbSck statbuf.st_dev) { 7005aba80dbSck break; 7015aba80dbSck } 7025aba80dbSck } 7035aba80dbSck if (ret != 0) { 7045aba80dbSck return (NULL); 7055aba80dbSck } 7065aba80dbSck 7075aba80dbSck if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 7085aba80dbSck (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 7095aba80dbSck path); 7105aba80dbSck return (NULL); 7115aba80dbSck } 7125aba80dbSck 7135aba80dbSck return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 7145aba80dbSck } 7155aba80dbSck 716e9dbad6fSeschrock /* 717e9dbad6fSeschrock * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 718e9dbad6fSeschrock * an ioctl(). 719e9dbad6fSeschrock */ 720e9dbad6fSeschrock int 721e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 722e9dbad6fSeschrock { 723e9dbad6fSeschrock if (len == 0) 7244b964adaSGeorge Wilson len = 16 * 1024; 725e9dbad6fSeschrock zc->zc_nvlist_dst_size = len; 726e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 727e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 728e9dbad6fSeschrock return (-1); 729e9dbad6fSeschrock 730e9dbad6fSeschrock return (0); 731e9dbad6fSeschrock } 732e9dbad6fSeschrock 733e9dbad6fSeschrock /* 734e9dbad6fSeschrock * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 735e9dbad6fSeschrock * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 736e9dbad6fSeschrock * filled in by the kernel to indicate the actual required size. 737e9dbad6fSeschrock */ 738e9dbad6fSeschrock int 739e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 740e9dbad6fSeschrock { 741e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 742e9dbad6fSeschrock if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 743e9dbad6fSeschrock zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 744e9dbad6fSeschrock == NULL) 745e9dbad6fSeschrock return (-1); 746e9dbad6fSeschrock 747e9dbad6fSeschrock return (0); 748e9dbad6fSeschrock } 749e9dbad6fSeschrock 750e9dbad6fSeschrock /* 751a2eea2e1Sahrens * Called to free the src and dst nvlists stored in the command structure. 752e9dbad6fSeschrock */ 753e9dbad6fSeschrock void 754e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc) 755e9dbad6fSeschrock { 756990b4856Slling free((void *)(uintptr_t)zc->zc_nvlist_conf); 757e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_src); 758e9dbad6fSeschrock free((void *)(uintptr_t)zc->zc_nvlist_dst); 759e9dbad6fSeschrock } 760e9dbad6fSeschrock 761990b4856Slling static int 762990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 763990b4856Slling nvlist_t *nvl) 764e9dbad6fSeschrock { 765e9dbad6fSeschrock char *packed; 766e9dbad6fSeschrock size_t len; 767e9dbad6fSeschrock 768e9dbad6fSeschrock verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 769e9dbad6fSeschrock 770e9dbad6fSeschrock if ((packed = zfs_alloc(hdl, len)) == NULL) 771e9dbad6fSeschrock return (-1); 772e9dbad6fSeschrock 773e9dbad6fSeschrock verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 774e9dbad6fSeschrock 775990b4856Slling *outnv = (uint64_t)(uintptr_t)packed; 776990b4856Slling *outlen = len; 777e9dbad6fSeschrock 778e9dbad6fSeschrock return (0); 779e9dbad6fSeschrock } 780e9dbad6fSeschrock 781990b4856Slling int 782990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 783990b4856Slling { 784990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 785990b4856Slling &zc->zc_nvlist_conf_size, nvl)); 786990b4856Slling } 787990b4856Slling 788990b4856Slling int 789990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 790990b4856Slling { 791990b4856Slling return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 792990b4856Slling &zc->zc_nvlist_src_size, nvl)); 793990b4856Slling } 794990b4856Slling 795e9dbad6fSeschrock /* 796e9dbad6fSeschrock * Unpacks an nvlist from the ZFS ioctl command structure. 797e9dbad6fSeschrock */ 798e9dbad6fSeschrock int 799e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 800e9dbad6fSeschrock { 801e9dbad6fSeschrock if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 802e9dbad6fSeschrock zc->zc_nvlist_dst_size, nvlp, 0) != 0) 803e9dbad6fSeschrock return (no_memory(hdl)); 804e9dbad6fSeschrock 805e9dbad6fSeschrock return (0); 806e9dbad6fSeschrock } 807b1b8ab34Slling 808990b4856Slling int 809990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 810990b4856Slling { 811990b4856Slling int error; 812990b4856Slling 813990b4856Slling zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 814990b4856Slling error = ioctl(hdl->libzfs_fd, request, zc); 815990b4856Slling if (hdl->libzfs_log_str) { 816990b4856Slling free(hdl->libzfs_log_str); 817990b4856Slling hdl->libzfs_log_str = NULL; 818990b4856Slling } 819990b4856Slling zc->zc_history = 0; 820990b4856Slling 821990b4856Slling return (error); 822990b4856Slling } 823990b4856Slling 824990b4856Slling /* 825990b4856Slling * ================================================================ 826990b4856Slling * API shared by zfs and zpool property management 827990b4856Slling * ================================================================ 828990b4856Slling */ 829990b4856Slling 830b1b8ab34Slling static void 831990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 832b1b8ab34Slling { 833990b4856Slling zprop_list_t *pl = cbp->cb_proplist; 834b1b8ab34Slling int i; 835b1b8ab34Slling char *title; 836b1b8ab34Slling size_t len; 837b1b8ab34Slling 838b1b8ab34Slling cbp->cb_first = B_FALSE; 839b1b8ab34Slling if (cbp->cb_scripted) 840b1b8ab34Slling return; 841b1b8ab34Slling 842b1b8ab34Slling /* 843b1b8ab34Slling * Start with the length of the column headers. 844b1b8ab34Slling */ 845b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 846b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 847b1b8ab34Slling "PROPERTY")); 848b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 849b1b8ab34Slling "VALUE")); 85092241e0bSTom Erickson cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, 85192241e0bSTom Erickson "RECEIVED")); 852b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 853b1b8ab34Slling "SOURCE")); 854b1b8ab34Slling 855deb8317bSMark J Musante /* first property is always NAME */ 856deb8317bSMark J Musante assert(cbp->cb_proplist->pl_prop == 857deb8317bSMark J Musante ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 858deb8317bSMark J Musante 859b1b8ab34Slling /* 860b1b8ab34Slling * Go through and calculate the widths for each column. For the 861b1b8ab34Slling * 'source' column, we kludge it up by taking the worst-case scenario of 862b1b8ab34Slling * inheriting from the longest name. This is acceptable because in the 863b1b8ab34Slling * majority of cases 'SOURCE' is the last column displayed, and we don't 864b1b8ab34Slling * use the width anyway. Note that the 'VALUE' column can be oversized, 86592241e0bSTom Erickson * if the name of the property is much longer than any values we find. 866b1b8ab34Slling */ 867b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 868b1b8ab34Slling /* 869b1b8ab34Slling * 'PROPERTY' column 870b1b8ab34Slling */ 871990b4856Slling if (pl->pl_prop != ZPROP_INVAL) { 872990b4856Slling const char *propname = (type == ZFS_TYPE_POOL) ? 873990b4856Slling zpool_prop_to_name(pl->pl_prop) : 874990b4856Slling zfs_prop_to_name(pl->pl_prop); 875990b4856Slling 876990b4856Slling len = strlen(propname); 877b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 878b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 879b1b8ab34Slling } else { 880b1b8ab34Slling len = strlen(pl->pl_user_prop); 881b1b8ab34Slling if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 882b1b8ab34Slling cbp->cb_colwidths[GET_COL_PROPERTY] = len; 883b1b8ab34Slling } 884b1b8ab34Slling 885b1b8ab34Slling /* 886deb8317bSMark J Musante * 'VALUE' column. The first property is always the 'name' 887deb8317bSMark J Musante * property that was tacked on either by /sbin/zfs's 888deb8317bSMark J Musante * zfs_do_get() or when calling zprop_expand_list(), so we 889deb8317bSMark J Musante * ignore its width. If the user specified the name property 890deb8317bSMark J Musante * to display, then it will be later in the list in any case. 891b1b8ab34Slling */ 892deb8317bSMark J Musante if (pl != cbp->cb_proplist && 893b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 894b1b8ab34Slling cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 895b1b8ab34Slling 89692241e0bSTom Erickson /* 'RECEIVED' column. */ 89792241e0bSTom Erickson if (pl != cbp->cb_proplist && 89892241e0bSTom Erickson pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) 89992241e0bSTom Erickson cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; 90092241e0bSTom Erickson 901b1b8ab34Slling /* 902b1b8ab34Slling * 'NAME' and 'SOURCE' columns 903b1b8ab34Slling */ 904990b4856Slling if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 905990b4856Slling ZFS_PROP_NAME) && 906b1b8ab34Slling pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 907b1b8ab34Slling cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 908b1b8ab34Slling cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 909b1b8ab34Slling strlen(dgettext(TEXT_DOMAIN, "inherited from")); 910b1b8ab34Slling } 911b1b8ab34Slling } 912b1b8ab34Slling 913b1b8ab34Slling /* 914b1b8ab34Slling * Now go through and print the headers. 915b1b8ab34Slling */ 91692241e0bSTom Erickson for (i = 0; i < ZFS_GET_NCOLS; i++) { 917b1b8ab34Slling switch (cbp->cb_columns[i]) { 918b1b8ab34Slling case GET_COL_NAME: 919b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "NAME"); 920b1b8ab34Slling break; 921b1b8ab34Slling case GET_COL_PROPERTY: 922b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "PROPERTY"); 923b1b8ab34Slling break; 924b1b8ab34Slling case GET_COL_VALUE: 925b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "VALUE"); 926b1b8ab34Slling break; 92792241e0bSTom Erickson case GET_COL_RECVD: 92892241e0bSTom Erickson title = dgettext(TEXT_DOMAIN, "RECEIVED"); 92992241e0bSTom Erickson break; 930b1b8ab34Slling case GET_COL_SOURCE: 931b1b8ab34Slling title = dgettext(TEXT_DOMAIN, "SOURCE"); 932b1b8ab34Slling break; 933b1b8ab34Slling default: 934b1b8ab34Slling title = NULL; 935b1b8ab34Slling } 936b1b8ab34Slling 937b1b8ab34Slling if (title != NULL) { 93892241e0bSTom Erickson if (i == (ZFS_GET_NCOLS - 1) || 93992241e0bSTom Erickson cbp->cb_columns[i + 1] == GET_COL_NONE) 940b1b8ab34Slling (void) printf("%s", title); 941b1b8ab34Slling else 942b1b8ab34Slling (void) printf("%-*s ", 943b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 944b1b8ab34Slling title); 945b1b8ab34Slling } 946b1b8ab34Slling } 947b1b8ab34Slling (void) printf("\n"); 948b1b8ab34Slling } 949b1b8ab34Slling 950b1b8ab34Slling /* 951b1b8ab34Slling * Display a single line of output, according to the settings in the callback 952b1b8ab34Slling * structure. 953b1b8ab34Slling */ 954b1b8ab34Slling void 955990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 956990b4856Slling const char *propname, const char *value, zprop_source_t sourcetype, 95792241e0bSTom Erickson const char *source, const char *recvd_value) 958b1b8ab34Slling { 959b1b8ab34Slling int i; 960b1b8ab34Slling const char *str; 961b1b8ab34Slling char buf[128]; 962b1b8ab34Slling 963b1b8ab34Slling /* 964b1b8ab34Slling * Ignore those source types that the user has chosen to ignore. 965b1b8ab34Slling */ 966b1b8ab34Slling if ((sourcetype & cbp->cb_sources) == 0) 967b1b8ab34Slling return; 968b1b8ab34Slling 969b1b8ab34Slling if (cbp->cb_first) 970990b4856Slling zprop_print_headers(cbp, cbp->cb_type); 971b1b8ab34Slling 97292241e0bSTom Erickson for (i = 0; i < ZFS_GET_NCOLS; i++) { 973b1b8ab34Slling switch (cbp->cb_columns[i]) { 974b1b8ab34Slling case GET_COL_NAME: 975b1b8ab34Slling str = name; 976b1b8ab34Slling break; 977b1b8ab34Slling 978b1b8ab34Slling case GET_COL_PROPERTY: 979b1b8ab34Slling str = propname; 980b1b8ab34Slling break; 981b1b8ab34Slling 982b1b8ab34Slling case GET_COL_VALUE: 983b1b8ab34Slling str = value; 984b1b8ab34Slling break; 985b1b8ab34Slling 986b1b8ab34Slling case GET_COL_SOURCE: 987b1b8ab34Slling switch (sourcetype) { 988990b4856Slling case ZPROP_SRC_NONE: 989b1b8ab34Slling str = "-"; 990b1b8ab34Slling break; 991b1b8ab34Slling 992990b4856Slling case ZPROP_SRC_DEFAULT: 993b1b8ab34Slling str = "default"; 994b1b8ab34Slling break; 995b1b8ab34Slling 996990b4856Slling case ZPROP_SRC_LOCAL: 997b1b8ab34Slling str = "local"; 998b1b8ab34Slling break; 999b1b8ab34Slling 1000990b4856Slling case ZPROP_SRC_TEMPORARY: 1001b1b8ab34Slling str = "temporary"; 1002b1b8ab34Slling break; 1003b1b8ab34Slling 1004990b4856Slling case ZPROP_SRC_INHERITED: 1005b1b8ab34Slling (void) snprintf(buf, sizeof (buf), 1006b1b8ab34Slling "inherited from %s", source); 1007b1b8ab34Slling str = buf; 1008b1b8ab34Slling break; 100992241e0bSTom Erickson case ZPROP_SRC_RECEIVED: 101092241e0bSTom Erickson str = "received"; 101192241e0bSTom Erickson break; 1012b1b8ab34Slling } 1013b1b8ab34Slling break; 1014b1b8ab34Slling 101592241e0bSTom Erickson case GET_COL_RECVD: 101692241e0bSTom Erickson str = (recvd_value == NULL ? "-" : recvd_value); 101792241e0bSTom Erickson break; 101892241e0bSTom Erickson 1019b1b8ab34Slling default: 1020b1b8ab34Slling continue; 1021b1b8ab34Slling } 1022b1b8ab34Slling 102392241e0bSTom Erickson if (cbp->cb_columns[i + 1] == GET_COL_NONE) 1024b1b8ab34Slling (void) printf("%s", str); 1025b1b8ab34Slling else if (cbp->cb_scripted) 1026b1b8ab34Slling (void) printf("%s\t", str); 1027b1b8ab34Slling else 1028b1b8ab34Slling (void) printf("%-*s ", 1029b1b8ab34Slling cbp->cb_colwidths[cbp->cb_columns[i]], 1030b1b8ab34Slling str); 1031b1b8ab34Slling } 1032b1b8ab34Slling 1033b1b8ab34Slling (void) printf("\n"); 1034b1b8ab34Slling } 1035ecd6cf80Smarks 1036990b4856Slling /* 1037990b4856Slling * Given a numeric suffix, convert the value into a number of bits that the 1038990b4856Slling * resulting value must be shifted. 1039990b4856Slling */ 1040990b4856Slling static int 1041990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf) 1042990b4856Slling { 1043990b4856Slling const char *ends = "BKMGTPEZ"; 1044990b4856Slling int i; 1045990b4856Slling 1046990b4856Slling if (buf[0] == '\0') 1047990b4856Slling return (0); 1048990b4856Slling for (i = 0; i < strlen(ends); i++) { 1049990b4856Slling if (toupper(buf[0]) == ends[i]) 1050990b4856Slling break; 1051990b4856Slling } 1052990b4856Slling if (i == strlen(ends)) { 1053990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1054990b4856Slling "invalid numeric suffix '%s'"), buf); 1055990b4856Slling return (-1); 1056990b4856Slling } 1057990b4856Slling 1058990b4856Slling /* 1059990b4856Slling * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 1060990b4856Slling * allow 'BB' - that's just weird. 1061990b4856Slling */ 1062990b4856Slling if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 1063990b4856Slling toupper(buf[0]) != 'B')) 1064990b4856Slling return (10*i); 1065990b4856Slling 1066990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1067990b4856Slling "invalid numeric suffix '%s'"), buf); 1068990b4856Slling return (-1); 1069990b4856Slling } 1070990b4856Slling 1071990b4856Slling /* 1072990b4856Slling * Convert a string of the form '100G' into a real number. Used when setting 1073990b4856Slling * properties or creating a volume. 'buf' is used to place an extended error 1074990b4856Slling * message for the caller to use. 1075990b4856Slling */ 1076ecd6cf80Smarks int 1077990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1078ecd6cf80Smarks { 1079990b4856Slling char *end; 1080990b4856Slling int shift; 1081ecd6cf80Smarks 1082990b4856Slling *num = 0; 1083990b4856Slling 1084990b4856Slling /* Check to see if this looks like a number. */ 1085990b4856Slling if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1086990b4856Slling if (hdl) 1087990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1088990b4856Slling "bad numeric value '%s'"), value); 1089990b4856Slling return (-1); 1090ecd6cf80Smarks } 1091ecd6cf80Smarks 1092069f55e2SEric Schrock /* Rely on strtoull() to process the numeric portion. */ 1093990b4856Slling errno = 0; 1094f67f35c3SEric Schrock *num = strtoull(value, &end, 10); 1095990b4856Slling 1096990b4856Slling /* 1097990b4856Slling * Check for ERANGE, which indicates that the value is too large to fit 1098990b4856Slling * in a 64-bit value. 1099990b4856Slling */ 1100990b4856Slling if (errno == ERANGE) { 1101990b4856Slling if (hdl) 1102990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1103990b4856Slling "numeric value is too large")); 1104990b4856Slling return (-1); 1105990b4856Slling } 1106990b4856Slling 1107990b4856Slling /* 1108990b4856Slling * If we have a decimal value, then do the computation with floating 1109990b4856Slling * point arithmetic. Otherwise, use standard arithmetic. 1110990b4856Slling */ 1111990b4856Slling if (*end == '.') { 1112990b4856Slling double fval = strtod(value, &end); 1113990b4856Slling 1114990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1115990b4856Slling return (-1); 1116990b4856Slling 1117990b4856Slling fval *= pow(2, shift); 1118990b4856Slling 1119990b4856Slling if (fval > UINT64_MAX) { 1120990b4856Slling if (hdl) 1121990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1122990b4856Slling "numeric value is too large")); 1123990b4856Slling return (-1); 1124990b4856Slling } 1125990b4856Slling 1126990b4856Slling *num = (uint64_t)fval; 1127990b4856Slling } else { 1128990b4856Slling if ((shift = str2shift(hdl, end)) == -1) 1129990b4856Slling return (-1); 1130990b4856Slling 1131990b4856Slling /* Check for overflow */ 1132990b4856Slling if (shift >= 64 || (*num << shift) >> shift != *num) { 1133990b4856Slling if (hdl) 1134990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1135990b4856Slling "numeric value is too large")); 1136990b4856Slling return (-1); 1137990b4856Slling } 1138990b4856Slling 1139990b4856Slling *num <<= shift; 1140990b4856Slling } 1141990b4856Slling 1142990b4856Slling return (0); 1143990b4856Slling } 1144990b4856Slling 1145990b4856Slling /* 1146990b4856Slling * Given a propname=value nvpair to set, parse any numeric properties 1147990b4856Slling * (index, boolean, etc) if they are specified as strings and add the 1148990b4856Slling * resulting nvpair to the returned nvlist. 1149990b4856Slling * 1150990b4856Slling * At the DSL layer, all properties are either 64-bit numbers or strings. 1151990b4856Slling * We want the user to be able to ignore this fact and specify properties 1152990b4856Slling * as native values (numbers, for example) or as strings (to simplify 1153990b4856Slling * command line utilities). This also handles converting index types 1154990b4856Slling * (compression, checksum, etc) from strings to their on-disk index. 1155990b4856Slling */ 1156990b4856Slling int 1157990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1158990b4856Slling zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1159990b4856Slling const char *errbuf) 1160990b4856Slling { 1161990b4856Slling data_type_t datatype = nvpair_type(elem); 1162990b4856Slling zprop_type_t proptype; 1163990b4856Slling const char *propname; 1164990b4856Slling char *value; 1165990b4856Slling boolean_t isnone = B_FALSE; 1166990b4856Slling 1167990b4856Slling if (type == ZFS_TYPE_POOL) { 1168990b4856Slling proptype = zpool_prop_get_type(prop); 1169990b4856Slling propname = zpool_prop_to_name(prop); 1170990b4856Slling } else { 1171990b4856Slling proptype = zfs_prop_get_type(prop); 1172990b4856Slling propname = zfs_prop_to_name(prop); 1173990b4856Slling } 1174990b4856Slling 1175990b4856Slling /* 1176990b4856Slling * Convert any properties to the internal DSL value types. 1177990b4856Slling */ 1178990b4856Slling *svalp = NULL; 1179990b4856Slling *ivalp = 0; 1180990b4856Slling 1181990b4856Slling switch (proptype) { 1182990b4856Slling case PROP_TYPE_STRING: 1183990b4856Slling if (datatype != DATA_TYPE_STRING) { 1184990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1185990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1186990b4856Slling goto error; 1187990b4856Slling } 1188990b4856Slling (void) nvpair_value_string(elem, svalp); 1189990b4856Slling if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1190990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1191990b4856Slling "'%s' is too long"), nvpair_name(elem)); 1192990b4856Slling goto error; 1193990b4856Slling } 1194990b4856Slling break; 1195990b4856Slling 1196990b4856Slling case PROP_TYPE_NUMBER: 1197990b4856Slling if (datatype == DATA_TYPE_STRING) { 1198990b4856Slling (void) nvpair_value_string(elem, &value); 1199990b4856Slling if (strcmp(value, "none") == 0) { 1200990b4856Slling isnone = B_TRUE; 1201990b4856Slling } else if (zfs_nicestrtonum(hdl, value, ivalp) 1202990b4856Slling != 0) { 1203990b4856Slling goto error; 1204990b4856Slling } 1205990b4856Slling } else if (datatype == DATA_TYPE_UINT64) { 1206990b4856Slling (void) nvpair_value_uint64(elem, ivalp); 1207990b4856Slling } else { 1208990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1209990b4856Slling "'%s' must be a number"), nvpair_name(elem)); 1210990b4856Slling goto error; 1211990b4856Slling } 1212990b4856Slling 1213990b4856Slling /* 1214990b4856Slling * Quota special: force 'none' and don't allow 0. 1215990b4856Slling */ 1216a9799022Sck if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1217a9799022Sck (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1218990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1219a9799022Sck "use 'none' to disable quota/refquota")); 1220990b4856Slling goto error; 1221990b4856Slling } 1222990b4856Slling break; 1223990b4856Slling 1224990b4856Slling case PROP_TYPE_INDEX: 1225a9799022Sck if (datatype != DATA_TYPE_STRING) { 1226990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1227990b4856Slling "'%s' must be a string"), nvpair_name(elem)); 1228990b4856Slling goto error; 1229990b4856Slling } 1230990b4856Slling 1231a9799022Sck (void) nvpair_value_string(elem, &value); 1232a9799022Sck 1233990b4856Slling if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1234990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1235990b4856Slling "'%s' must be one of '%s'"), propname, 1236990b4856Slling zprop_values(prop, type)); 1237990b4856Slling goto error; 1238990b4856Slling } 1239990b4856Slling break; 1240990b4856Slling 1241990b4856Slling default: 1242990b4856Slling abort(); 1243990b4856Slling } 1244990b4856Slling 1245990b4856Slling /* 1246990b4856Slling * Add the result to our return set of properties. 1247990b4856Slling */ 1248990b4856Slling if (*svalp != NULL) { 1249990b4856Slling if (nvlist_add_string(ret, propname, *svalp) != 0) { 1250990b4856Slling (void) no_memory(hdl); 1251990b4856Slling return (-1); 1252990b4856Slling } 1253990b4856Slling } else { 1254990b4856Slling if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1255990b4856Slling (void) no_memory(hdl); 1256990b4856Slling return (-1); 1257990b4856Slling } 1258990b4856Slling } 1259990b4856Slling 1260990b4856Slling return (0); 1261990b4856Slling error: 1262990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1263990b4856Slling return (-1); 1264990b4856Slling } 1265990b4856Slling 126674e7dc98SMatthew Ahrens static int 126774e7dc98SMatthew Ahrens addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 126874e7dc98SMatthew Ahrens zfs_type_t type) 126974e7dc98SMatthew Ahrens { 127074e7dc98SMatthew Ahrens int prop; 127174e7dc98SMatthew Ahrens zprop_list_t *entry; 127274e7dc98SMatthew Ahrens 127374e7dc98SMatthew Ahrens prop = zprop_name_to_prop(propname, type); 127474e7dc98SMatthew Ahrens 127574e7dc98SMatthew Ahrens if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 127674e7dc98SMatthew Ahrens prop = ZPROP_INVAL; 127774e7dc98SMatthew Ahrens 127874e7dc98SMatthew Ahrens /* 127974e7dc98SMatthew Ahrens * When no property table entry can be found, return failure if 128074e7dc98SMatthew Ahrens * this is a pool property or if this isn't a user-defined 128174e7dc98SMatthew Ahrens * dataset property, 128274e7dc98SMatthew Ahrens */ 128374e7dc98SMatthew Ahrens if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 128414843421SMatthew Ahrens (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) { 128574e7dc98SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 128674e7dc98SMatthew Ahrens "invalid property '%s'"), propname); 128774e7dc98SMatthew Ahrens return (zfs_error(hdl, EZFS_BADPROP, 128874e7dc98SMatthew Ahrens dgettext(TEXT_DOMAIN, "bad property list"))); 128974e7dc98SMatthew Ahrens } 129074e7dc98SMatthew Ahrens 129174e7dc98SMatthew Ahrens if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 129274e7dc98SMatthew Ahrens return (-1); 129374e7dc98SMatthew Ahrens 129474e7dc98SMatthew Ahrens entry->pl_prop = prop; 129574e7dc98SMatthew Ahrens if (prop == ZPROP_INVAL) { 129674e7dc98SMatthew Ahrens if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) { 129774e7dc98SMatthew Ahrens free(entry); 129874e7dc98SMatthew Ahrens return (-1); 129974e7dc98SMatthew Ahrens } 130074e7dc98SMatthew Ahrens entry->pl_width = strlen(propname); 130174e7dc98SMatthew Ahrens } else { 130274e7dc98SMatthew Ahrens entry->pl_width = zprop_width(prop, &entry->pl_fixed, 130374e7dc98SMatthew Ahrens type); 130474e7dc98SMatthew Ahrens } 130574e7dc98SMatthew Ahrens 130674e7dc98SMatthew Ahrens *listp = entry; 130774e7dc98SMatthew Ahrens 130874e7dc98SMatthew Ahrens return (0); 130974e7dc98SMatthew Ahrens } 131074e7dc98SMatthew Ahrens 1311990b4856Slling /* 1312990b4856Slling * Given a comma-separated list of properties, construct a property list 1313990b4856Slling * containing both user-defined and native properties. This function will 1314990b4856Slling * return a NULL list if 'all' is specified, which can later be expanded 1315990b4856Slling * by zprop_expand_list(). 1316990b4856Slling */ 1317990b4856Slling int 1318990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1319990b4856Slling zfs_type_t type) 1320990b4856Slling { 1321990b4856Slling *listp = NULL; 1322990b4856Slling 1323990b4856Slling /* 1324990b4856Slling * If 'all' is specified, return a NULL list. 1325990b4856Slling */ 1326990b4856Slling if (strcmp(props, "all") == 0) 1327990b4856Slling return (0); 1328990b4856Slling 1329990b4856Slling /* 1330990b4856Slling * If no props were specified, return an error. 1331990b4856Slling */ 1332990b4856Slling if (props[0] == '\0') { 1333990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1334990b4856Slling "no properties specified")); 1335990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1336990b4856Slling "bad property list"))); 1337990b4856Slling } 1338990b4856Slling 1339990b4856Slling /* 1340990b4856Slling * It would be nice to use getsubopt() here, but the inclusion of column 1341990b4856Slling * aliases makes this more effort than it's worth. 1342990b4856Slling */ 134374e7dc98SMatthew Ahrens while (*props != '\0') { 134474e7dc98SMatthew Ahrens size_t len; 134574e7dc98SMatthew Ahrens char *p; 134674e7dc98SMatthew Ahrens char c; 134774e7dc98SMatthew Ahrens 134874e7dc98SMatthew Ahrens if ((p = strchr(props, ',')) == NULL) { 134974e7dc98SMatthew Ahrens len = strlen(props); 135074e7dc98SMatthew Ahrens p = props + len; 1351990b4856Slling } else { 135274e7dc98SMatthew Ahrens len = p - props; 1353990b4856Slling } 1354990b4856Slling 1355990b4856Slling /* 1356990b4856Slling * Check for empty options. 1357990b4856Slling */ 1358990b4856Slling if (len == 0) { 1359990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1360990b4856Slling "empty property name")); 1361990b4856Slling return (zfs_error(hdl, EZFS_BADPROP, 1362990b4856Slling dgettext(TEXT_DOMAIN, "bad property list"))); 1363990b4856Slling } 1364990b4856Slling 1365990b4856Slling /* 1366990b4856Slling * Check all regular property names. 1367990b4856Slling */ 136874e7dc98SMatthew Ahrens c = props[len]; 136974e7dc98SMatthew Ahrens props[len] = '\0'; 137074e7dc98SMatthew Ahrens 137174e7dc98SMatthew Ahrens if (strcmp(props, "space") == 0) { 137274e7dc98SMatthew Ahrens static char *spaceprops[] = { 137374e7dc98SMatthew Ahrens "name", "avail", "used", "usedbysnapshots", 137474e7dc98SMatthew Ahrens "usedbydataset", "usedbyrefreservation", 137574e7dc98SMatthew Ahrens "usedbychildren", NULL 137674e7dc98SMatthew Ahrens }; 137774e7dc98SMatthew Ahrens int i; 137874e7dc98SMatthew Ahrens 137974e7dc98SMatthew Ahrens for (i = 0; spaceprops[i]; i++) { 138074e7dc98SMatthew Ahrens if (addlist(hdl, spaceprops[i], listp, type)) 138174e7dc98SMatthew Ahrens return (-1); 138274e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1383990b4856Slling } 1384990b4856Slling } else { 138574e7dc98SMatthew Ahrens if (addlist(hdl, props, listp, type)) 138674e7dc98SMatthew Ahrens return (-1); 138774e7dc98SMatthew Ahrens listp = &(*listp)->pl_next; 1388990b4856Slling } 1389990b4856Slling 139074e7dc98SMatthew Ahrens props = p; 1391990b4856Slling if (c == ',') 139274e7dc98SMatthew Ahrens props++; 1393990b4856Slling } 1394990b4856Slling 1395990b4856Slling return (0); 1396990b4856Slling } 1397990b4856Slling 1398990b4856Slling void 1399990b4856Slling zprop_free_list(zprop_list_t *pl) 1400990b4856Slling { 1401990b4856Slling zprop_list_t *next; 1402990b4856Slling 1403990b4856Slling while (pl != NULL) { 1404990b4856Slling next = pl->pl_next; 1405990b4856Slling free(pl->pl_user_prop); 1406990b4856Slling free(pl); 1407990b4856Slling pl = next; 1408990b4856Slling } 1409990b4856Slling } 1410990b4856Slling 1411990b4856Slling typedef struct expand_data { 1412990b4856Slling zprop_list_t **last; 1413990b4856Slling libzfs_handle_t *hdl; 1414990b4856Slling zfs_type_t type; 1415990b4856Slling } expand_data_t; 1416990b4856Slling 1417990b4856Slling int 1418990b4856Slling zprop_expand_list_cb(int prop, void *cb) 1419990b4856Slling { 1420990b4856Slling zprop_list_t *entry; 1421990b4856Slling expand_data_t *edp = cb; 1422990b4856Slling 1423990b4856Slling if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1424990b4856Slling return (ZPROP_INVAL); 1425990b4856Slling 1426990b4856Slling entry->pl_prop = prop; 1427990b4856Slling entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1428990b4856Slling entry->pl_all = B_TRUE; 1429990b4856Slling 1430990b4856Slling *(edp->last) = entry; 1431990b4856Slling edp->last = &entry->pl_next; 1432990b4856Slling 1433990b4856Slling return (ZPROP_CONT); 1434990b4856Slling } 1435990b4856Slling 1436990b4856Slling int 1437990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1438990b4856Slling { 1439990b4856Slling zprop_list_t *entry; 1440990b4856Slling zprop_list_t **last; 1441990b4856Slling expand_data_t exp; 1442990b4856Slling 1443990b4856Slling if (*plp == NULL) { 1444990b4856Slling /* 1445990b4856Slling * If this is the very first time we've been called for an 'all' 1446990b4856Slling * specification, expand the list to include all native 1447990b4856Slling * properties. 1448990b4856Slling */ 1449990b4856Slling last = plp; 1450990b4856Slling 1451990b4856Slling exp.last = last; 1452990b4856Slling exp.hdl = hdl; 1453990b4856Slling exp.type = type; 1454990b4856Slling 1455990b4856Slling if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1456990b4856Slling B_FALSE, type) == ZPROP_INVAL) 1457990b4856Slling return (-1); 1458990b4856Slling 1459990b4856Slling /* 1460990b4856Slling * Add 'name' to the beginning of the list, which is handled 1461990b4856Slling * specially. 1462990b4856Slling */ 1463990b4856Slling if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1464990b4856Slling return (-1); 1465990b4856Slling 1466990b4856Slling entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1467990b4856Slling ZFS_PROP_NAME; 1468990b4856Slling entry->pl_width = zprop_width(entry->pl_prop, 1469990b4856Slling &entry->pl_fixed, type); 1470990b4856Slling entry->pl_all = B_TRUE; 1471990b4856Slling entry->pl_next = *plp; 1472990b4856Slling *plp = entry; 1473990b4856Slling } 1474990b4856Slling return (0); 1475990b4856Slling } 1476990b4856Slling 1477990b4856Slling int 1478990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1479990b4856Slling zfs_type_t type) 1480990b4856Slling { 1481990b4856Slling return (zprop_iter_common(func, cb, show_all, ordered, type)); 1482ecd6cf80Smarks } 1483