1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy 22eda14cbcSMatt Macy /* 23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24eda14cbcSMatt Macy * Copyright 2020 Joyent, Inc. All rights reserved. 25eda14cbcSMatt Macy * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 26eda14cbcSMatt Macy * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 27eda14cbcSMatt Macy * Copyright (c) 2017 Datto Inc. 28eda14cbcSMatt Macy * Copyright (c) 2020 The FreeBSD Foundation 29eda14cbcSMatt Macy * 30eda14cbcSMatt Macy * Portions of this software were developed by Allan Jude 31eda14cbcSMatt Macy * under sponsorship from the FreeBSD Foundation. 32eda14cbcSMatt Macy */ 33eda14cbcSMatt Macy 34eda14cbcSMatt Macy /* 35eda14cbcSMatt Macy * Internal utility routines for the ZFS library. 36eda14cbcSMatt Macy */ 37eda14cbcSMatt Macy 38eda14cbcSMatt Macy #include <errno.h> 39eda14cbcSMatt Macy #include <fcntl.h> 40eda14cbcSMatt Macy #include <libintl.h> 41eda14cbcSMatt Macy #include <stdarg.h> 42eda14cbcSMatt Macy #include <stdio.h> 43eda14cbcSMatt Macy #include <stdlib.h> 44eda14cbcSMatt Macy #include <strings.h> 45eda14cbcSMatt Macy #include <unistd.h> 46eda14cbcSMatt Macy #include <math.h> 4716038816SMartin Matuska #if LIBFETCH_DYNAMIC 4816038816SMartin Matuska #include <dlfcn.h> 4916038816SMartin Matuska #endif 50eda14cbcSMatt Macy #include <sys/stat.h> 51eda14cbcSMatt Macy #include <sys/mnttab.h> 52eda14cbcSMatt Macy #include <sys/mntent.h> 53eda14cbcSMatt Macy #include <sys/types.h> 54eda14cbcSMatt Macy #include <sys/wait.h> 55eda14cbcSMatt Macy 56eda14cbcSMatt Macy #include <libzfs.h> 57eda14cbcSMatt Macy #include <libzfs_core.h> 58eda14cbcSMatt Macy 59eda14cbcSMatt Macy #include "libzfs_impl.h" 60eda14cbcSMatt Macy #include "zfs_prop.h" 61eda14cbcSMatt Macy #include "zfeature_common.h" 62eda14cbcSMatt Macy #include <zfs_fletcher.h> 63eda14cbcSMatt Macy #include <libzutil.h> 64eda14cbcSMatt Macy 65eda14cbcSMatt Macy /* 66eda14cbcSMatt Macy * We only care about the scheme in order to match the scheme 67eda14cbcSMatt Macy * with the handler. Each handler should validate the full URI 68eda14cbcSMatt Macy * as necessary. 69eda14cbcSMatt Macy */ 70eda14cbcSMatt Macy #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):" 71eda14cbcSMatt Macy 72eda14cbcSMatt Macy int 73eda14cbcSMatt Macy libzfs_errno(libzfs_handle_t *hdl) 74eda14cbcSMatt Macy { 75eda14cbcSMatt Macy return (hdl->libzfs_error); 76eda14cbcSMatt Macy } 77eda14cbcSMatt Macy 78eda14cbcSMatt Macy const char * 79eda14cbcSMatt Macy libzfs_error_action(libzfs_handle_t *hdl) 80eda14cbcSMatt Macy { 81eda14cbcSMatt Macy return (hdl->libzfs_action); 82eda14cbcSMatt Macy } 83eda14cbcSMatt Macy 84eda14cbcSMatt Macy const char * 85eda14cbcSMatt Macy libzfs_error_description(libzfs_handle_t *hdl) 86eda14cbcSMatt Macy { 87eda14cbcSMatt Macy if (hdl->libzfs_desc[0] != '\0') 88eda14cbcSMatt Macy return (hdl->libzfs_desc); 89eda14cbcSMatt Macy 90eda14cbcSMatt Macy switch (hdl->libzfs_error) { 91eda14cbcSMatt Macy case EZFS_NOMEM: 92eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "out of memory")); 93eda14cbcSMatt Macy case EZFS_BADPROP: 94eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid property value")); 95eda14cbcSMatt Macy case EZFS_PROPREADONLY: 96eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "read-only property")); 97eda14cbcSMatt Macy case EZFS_PROPTYPE: 98eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 99eda14cbcSMatt Macy "datasets of this type")); 100eda14cbcSMatt Macy case EZFS_PROPNONINHERIT: 101eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 102eda14cbcSMatt Macy case EZFS_PROPSPACE: 103eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 104eda14cbcSMatt Macy case EZFS_BADTYPE: 105eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not applicable to " 106eda14cbcSMatt Macy "datasets of this type")); 107eda14cbcSMatt Macy case EZFS_BUSY: 108eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 109eda14cbcSMatt Macy case EZFS_EXISTS: 110eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 111eda14cbcSMatt Macy case EZFS_NOENT: 112eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 113eda14cbcSMatt Macy case EZFS_BADSTREAM: 114eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 115eda14cbcSMatt Macy case EZFS_DSREADONLY: 116eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "dataset is read-only")); 117eda14cbcSMatt Macy case EZFS_VOLTOOBIG: 118eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 119eda14cbcSMatt Macy "this system")); 120eda14cbcSMatt Macy case EZFS_INVALIDNAME: 121eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid name")); 122eda14cbcSMatt Macy case EZFS_BADRESTORE: 123eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unable to restore to " 124eda14cbcSMatt Macy "destination")); 125eda14cbcSMatt Macy case EZFS_BADBACKUP: 126eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "backup failed")); 127eda14cbcSMatt Macy case EZFS_BADTARGET: 128eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 129eda14cbcSMatt Macy case EZFS_NODEVICE: 130eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no such device in pool")); 131eda14cbcSMatt Macy case EZFS_BADDEV: 132eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid device")); 133eda14cbcSMatt Macy case EZFS_NOREPLICAS: 134eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no valid replicas")); 135eda14cbcSMatt Macy case EZFS_RESILVERING: 136eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently resilvering")); 137eda14cbcSMatt Macy case EZFS_BADVERSION: 138eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unsupported version or " 139eda14cbcSMatt Macy "feature")); 140eda14cbcSMatt Macy case EZFS_POOLUNAVAIL: 141eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 142eda14cbcSMatt Macy case EZFS_DEVOVERFLOW: 143eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 144eda14cbcSMatt Macy case EZFS_BADPATH: 145eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 146eda14cbcSMatt Macy case EZFS_CROSSTARGET: 147eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 148eda14cbcSMatt Macy "pools")); 149eda14cbcSMatt Macy case EZFS_ZONED: 150eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 151eda14cbcSMatt Macy case EZFS_MOUNTFAILED: 152eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "mount failed")); 153eda14cbcSMatt Macy case EZFS_UMOUNTFAILED: 1547877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "unmount failed")); 155eda14cbcSMatt Macy case EZFS_UNSHARENFSFAILED: 1567877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "NFS share removal failed")); 157eda14cbcSMatt Macy case EZFS_SHARENFSFAILED: 1587877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "NFS share creation failed")); 159eda14cbcSMatt Macy case EZFS_UNSHARESMBFAILED: 1607877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "SMB share removal failed")); 161eda14cbcSMatt Macy case EZFS_SHARESMBFAILED: 1627877fdebSMatt Macy return (dgettext(TEXT_DOMAIN, "SMB share creation failed")); 163eda14cbcSMatt Macy case EZFS_PERM: 164eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "permission denied")); 165eda14cbcSMatt Macy case EZFS_NOSPC: 166eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "out of space")); 167eda14cbcSMatt Macy case EZFS_FAULT: 168eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "bad address")); 169eda14cbcSMatt Macy case EZFS_IO: 170eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "I/O error")); 171eda14cbcSMatt Macy case EZFS_INTR: 172eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "signal received")); 173eda14cbcSMatt Macy case EZFS_ISSPARE: 174eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 175eda14cbcSMatt Macy "spare")); 176eda14cbcSMatt Macy case EZFS_INVALCONFIG: 177eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 178eda14cbcSMatt Macy case EZFS_RECURSIVE: 179eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 180eda14cbcSMatt Macy case EZFS_NOHISTORY: 181eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no history available")); 182eda14cbcSMatt Macy case EZFS_POOLPROPS: 183eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "failed to retrieve " 184eda14cbcSMatt Macy "pool properties")); 185eda14cbcSMatt Macy case EZFS_POOL_NOTSUP: 186eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not supported " 187eda14cbcSMatt Macy "on this type of pool")); 188eda14cbcSMatt Macy case EZFS_POOL_INVALARG: 189eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid argument for " 190eda14cbcSMatt Macy "this pool operation")); 191eda14cbcSMatt Macy case EZFS_NAMETOOLONG: 192eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 193eda14cbcSMatt Macy case EZFS_OPENFAILED: 194eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "open failed")); 195eda14cbcSMatt Macy case EZFS_NOCAP: 196eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, 197eda14cbcSMatt Macy "disk capacity information could not be retrieved")); 198eda14cbcSMatt Macy case EZFS_LABELFAILED: 199eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "write of label failed")); 200eda14cbcSMatt Macy case EZFS_BADWHO: 201eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid user/group")); 202eda14cbcSMatt Macy case EZFS_BADPERM: 203eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid permission")); 204eda14cbcSMatt Macy case EZFS_BADPERMSET: 205eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 206eda14cbcSMatt Macy case EZFS_NODELEGATION: 207eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "delegated administration is " 208eda14cbcSMatt Macy "disabled on pool")); 209eda14cbcSMatt Macy case EZFS_BADCACHE: 210eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 211eda14cbcSMatt Macy case EZFS_ISL2CACHE: 212eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 213eda14cbcSMatt Macy case EZFS_VDEVNOTSUP: 214eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "vdev specification is not " 215eda14cbcSMatt Macy "supported")); 216eda14cbcSMatt Macy case EZFS_NOTSUP: 217eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not supported " 218eda14cbcSMatt Macy "on this dataset")); 219eda14cbcSMatt Macy case EZFS_IOC_NOTSUPPORTED: 220eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation not supported by " 221eda14cbcSMatt Macy "zfs kernel module")); 222eda14cbcSMatt Macy case EZFS_ACTIVE_SPARE: 223eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 224eda14cbcSMatt Macy "device")); 225eda14cbcSMatt Macy case EZFS_UNPLAYED_LOGS: 226eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 227eda14cbcSMatt Macy "logs")); 228eda14cbcSMatt Macy case EZFS_REFTAG_RELE: 229eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 230eda14cbcSMatt Macy case EZFS_REFTAG_HOLD: 231eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "tag already exists on this " 232eda14cbcSMatt Macy "dataset")); 233eda14cbcSMatt Macy case EZFS_TAGTOOLONG: 234eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "tag too long")); 235eda14cbcSMatt Macy case EZFS_PIPEFAILED: 236eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pipe create failed")); 237eda14cbcSMatt Macy case EZFS_THREADCREATEFAILED: 238eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "thread create failed")); 239eda14cbcSMatt Macy case EZFS_POSTSPLIT_ONLINE: 240eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "disk was split from this pool " 241eda14cbcSMatt Macy "into a new one")); 242eda14cbcSMatt Macy case EZFS_SCRUB_PAUSED: 243eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "scrub is paused; " 244eda14cbcSMatt Macy "use 'zpool scrub' to resume")); 245eda14cbcSMatt Macy case EZFS_SCRUBBING: 246eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently scrubbing; " 247eda14cbcSMatt Macy "use 'zpool scrub -s' to cancel current scrub")); 248eda14cbcSMatt Macy case EZFS_NO_SCRUB: 249eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "there is no active scrub")); 250eda14cbcSMatt Macy case EZFS_DIFF: 251eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unable to generate diffs")); 252eda14cbcSMatt Macy case EZFS_DIFFDATA: 253eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid diff data")); 254eda14cbcSMatt Macy case EZFS_POOLREADONLY: 255eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool is read-only")); 256eda14cbcSMatt Macy case EZFS_NO_PENDING: 257eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "operation is not " 258eda14cbcSMatt Macy "in progress")); 259eda14cbcSMatt Macy case EZFS_CHECKPOINT_EXISTS: 260eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "checkpoint exists")); 261eda14cbcSMatt Macy case EZFS_DISCARDING_CHECKPOINT: 262eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently discarding " 263eda14cbcSMatt Macy "checkpoint")); 264eda14cbcSMatt Macy case EZFS_NO_CHECKPOINT: 265eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "checkpoint does not exist")); 266eda14cbcSMatt Macy case EZFS_DEVRM_IN_PROGRESS: 267eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device removal in progress")); 268eda14cbcSMatt Macy case EZFS_VDEV_TOO_BIG: 269eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "device exceeds supported size")); 270eda14cbcSMatt Macy case EZFS_ACTIVE_POOL: 271eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool is imported on a " 272eda14cbcSMatt Macy "different host")); 273eda14cbcSMatt Macy case EZFS_CRYPTOFAILED: 274eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "encryption failure")); 275eda14cbcSMatt Macy case EZFS_TOOMANY: 276eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "argument list too long")); 277eda14cbcSMatt Macy case EZFS_INITIALIZING: 278eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently initializing")); 279eda14cbcSMatt Macy case EZFS_NO_INITIALIZE: 280eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "there is no active " 281eda14cbcSMatt Macy "initialization")); 282eda14cbcSMatt Macy case EZFS_WRONG_PARENT: 283eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "invalid parent dataset")); 284eda14cbcSMatt Macy case EZFS_TRIMMING: 285eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently trimming")); 286eda14cbcSMatt Macy case EZFS_NO_TRIM: 287eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "there is no active trim")); 288eda14cbcSMatt Macy case EZFS_TRIM_NOTSUP: 289eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "trim operations are not " 290eda14cbcSMatt Macy "supported by this device")); 291eda14cbcSMatt Macy case EZFS_NO_RESILVER_DEFER: 292eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "this action requires the " 293eda14cbcSMatt Macy "resilver_defer feature")); 294eda14cbcSMatt Macy case EZFS_EXPORT_IN_PROGRESS: 295eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "pool export in progress")); 296eda14cbcSMatt Macy case EZFS_REBUILDING: 297eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "currently sequentially " 298eda14cbcSMatt Macy "resilvering")); 299681ce946SMartin Matuska case EZFS_VDEV_NOTSUP: 300681ce946SMartin Matuska return (dgettext(TEXT_DOMAIN, "operation not supported " 301681ce946SMartin Matuska "on this type of vdev")); 302*1f1e2261SMartin Matuska case EZFS_NOT_USER_NAMESPACE: 303*1f1e2261SMartin Matuska return (dgettext(TEXT_DOMAIN, "the provided file " 304*1f1e2261SMartin Matuska "was not a user namespace file")); 305eda14cbcSMatt Macy case EZFS_UNKNOWN: 306eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "unknown error")); 307eda14cbcSMatt Macy default: 308eda14cbcSMatt Macy assert(hdl->libzfs_error == 0); 309eda14cbcSMatt Macy return (dgettext(TEXT_DOMAIN, "no error")); 310eda14cbcSMatt Macy } 311eda14cbcSMatt Macy } 312eda14cbcSMatt Macy 313eda14cbcSMatt Macy void 314eda14cbcSMatt Macy zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 315eda14cbcSMatt Macy { 316eda14cbcSMatt Macy va_list ap; 317eda14cbcSMatt Macy 318eda14cbcSMatt Macy va_start(ap, fmt); 319eda14cbcSMatt Macy 320eda14cbcSMatt Macy (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 321eda14cbcSMatt Macy fmt, ap); 322eda14cbcSMatt Macy hdl->libzfs_desc_active = 1; 323eda14cbcSMatt Macy 324eda14cbcSMatt Macy va_end(ap); 325eda14cbcSMatt Macy } 326eda14cbcSMatt Macy 327eda14cbcSMatt Macy static void 328eda14cbcSMatt Macy zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 329eda14cbcSMatt Macy { 330eda14cbcSMatt Macy (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 331eda14cbcSMatt Macy fmt, ap); 332eda14cbcSMatt Macy hdl->libzfs_error = error; 333eda14cbcSMatt Macy 334eda14cbcSMatt Macy if (hdl->libzfs_desc_active) 335eda14cbcSMatt Macy hdl->libzfs_desc_active = 0; 336eda14cbcSMatt Macy else 337eda14cbcSMatt Macy hdl->libzfs_desc[0] = '\0'; 338eda14cbcSMatt Macy 339eda14cbcSMatt Macy if (hdl->libzfs_printerr) { 340eda14cbcSMatt Macy if (error == EZFS_UNKNOWN) { 341eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 342eda14cbcSMatt Macy "error: %s: %s\n"), hdl->libzfs_action, 343eda14cbcSMatt Macy libzfs_error_description(hdl)); 344eda14cbcSMatt Macy abort(); 345eda14cbcSMatt Macy } 346eda14cbcSMatt Macy 347eda14cbcSMatt Macy (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 348eda14cbcSMatt Macy libzfs_error_description(hdl)); 349eda14cbcSMatt Macy if (error == EZFS_NOMEM) 350eda14cbcSMatt Macy exit(1); 351eda14cbcSMatt Macy } 352eda14cbcSMatt Macy } 353eda14cbcSMatt Macy 354eda14cbcSMatt Macy int 355eda14cbcSMatt Macy zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 356eda14cbcSMatt Macy { 357eda14cbcSMatt Macy return (zfs_error_fmt(hdl, error, "%s", msg)); 358eda14cbcSMatt Macy } 359eda14cbcSMatt Macy 360eda14cbcSMatt Macy int 361eda14cbcSMatt Macy zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 362eda14cbcSMatt Macy { 363eda14cbcSMatt Macy va_list ap; 364eda14cbcSMatt Macy 365eda14cbcSMatt Macy va_start(ap, fmt); 366eda14cbcSMatt Macy 367eda14cbcSMatt Macy zfs_verror(hdl, error, fmt, ap); 368eda14cbcSMatt Macy 369eda14cbcSMatt Macy va_end(ap); 370eda14cbcSMatt Macy 371eda14cbcSMatt Macy return (-1); 372eda14cbcSMatt Macy } 373eda14cbcSMatt Macy 374eda14cbcSMatt Macy static int 375eda14cbcSMatt Macy zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 376eda14cbcSMatt Macy va_list ap) 377eda14cbcSMatt Macy { 378eda14cbcSMatt Macy switch (error) { 379eda14cbcSMatt Macy case EPERM: 380eda14cbcSMatt Macy case EACCES: 381eda14cbcSMatt Macy zfs_verror(hdl, EZFS_PERM, fmt, ap); 382eda14cbcSMatt Macy return (-1); 383eda14cbcSMatt Macy 384eda14cbcSMatt Macy case ECANCELED: 385eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 386eda14cbcSMatt Macy return (-1); 387eda14cbcSMatt Macy 388eda14cbcSMatt Macy case EIO: 389eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IO, fmt, ap); 390eda14cbcSMatt Macy return (-1); 391eda14cbcSMatt Macy 392eda14cbcSMatt Macy case EFAULT: 393eda14cbcSMatt Macy zfs_verror(hdl, EZFS_FAULT, fmt, ap); 394eda14cbcSMatt Macy return (-1); 395eda14cbcSMatt Macy 396eda14cbcSMatt Macy case EINTR: 397eda14cbcSMatt Macy zfs_verror(hdl, EZFS_INTR, fmt, ap); 398eda14cbcSMatt Macy return (-1); 399eda14cbcSMatt Macy } 400eda14cbcSMatt Macy 401eda14cbcSMatt Macy return (0); 402eda14cbcSMatt Macy } 403eda14cbcSMatt Macy 404eda14cbcSMatt Macy int 405eda14cbcSMatt Macy zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 406eda14cbcSMatt Macy { 407eda14cbcSMatt Macy return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 408eda14cbcSMatt Macy } 409eda14cbcSMatt Macy 410eda14cbcSMatt Macy int 411eda14cbcSMatt Macy zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 412eda14cbcSMatt Macy { 413eda14cbcSMatt Macy va_list ap; 414eda14cbcSMatt Macy 415eda14cbcSMatt Macy va_start(ap, fmt); 416eda14cbcSMatt Macy 417eda14cbcSMatt Macy if (zfs_common_error(hdl, error, fmt, ap) != 0) { 418eda14cbcSMatt Macy va_end(ap); 419eda14cbcSMatt Macy return (-1); 420eda14cbcSMatt Macy } 421eda14cbcSMatt Macy 422eda14cbcSMatt Macy switch (error) { 423eda14cbcSMatt Macy case ENXIO: 424eda14cbcSMatt Macy case ENODEV: 425eda14cbcSMatt Macy case EPIPE: 426eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IO, fmt, ap); 427eda14cbcSMatt Macy break; 428eda14cbcSMatt Macy 429eda14cbcSMatt Macy case ENOENT: 430eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 431eda14cbcSMatt Macy "dataset does not exist")); 432eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOENT, fmt, ap); 433eda14cbcSMatt Macy break; 434eda14cbcSMatt Macy 435eda14cbcSMatt Macy case ENOSPC: 436eda14cbcSMatt Macy case EDQUOT: 437eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 438eda14cbcSMatt Macy break; 439eda14cbcSMatt Macy 440eda14cbcSMatt Macy case EEXIST: 441eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 442eda14cbcSMatt Macy "dataset already exists")); 443eda14cbcSMatt Macy zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 444eda14cbcSMatt Macy break; 445eda14cbcSMatt Macy 446eda14cbcSMatt Macy case EBUSY: 447eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 448eda14cbcSMatt Macy "dataset is busy")); 449eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BUSY, fmt, ap); 450eda14cbcSMatt Macy break; 451eda14cbcSMatt Macy case EROFS: 452eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 453eda14cbcSMatt Macy break; 454eda14cbcSMatt Macy case ENAMETOOLONG: 455eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 456eda14cbcSMatt Macy break; 457eda14cbcSMatt Macy case ENOTSUP: 458eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 459eda14cbcSMatt Macy break; 460eda14cbcSMatt Macy case EAGAIN: 461eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 462eda14cbcSMatt Macy "pool I/O is currently suspended")); 463eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 464eda14cbcSMatt Macy break; 465eda14cbcSMatt Macy case EREMOTEIO: 466eda14cbcSMatt Macy zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 467eda14cbcSMatt Macy break; 468eda14cbcSMatt Macy case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE: 469eda14cbcSMatt Macy case ZFS_ERR_IOC_CMD_UNAVAIL: 470eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 471eda14cbcSMatt Macy "module does not support this operation. A reboot may " 472eda14cbcSMatt Macy "be required to enable this operation.")); 473eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 474eda14cbcSMatt Macy break; 475eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_UNAVAIL: 476eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 477eda14cbcSMatt Macy "module does not support an option for this operation. " 478eda14cbcSMatt Macy "A reboot may be required to enable this option.")); 479eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 480eda14cbcSMatt Macy break; 481eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_REQUIRED: 482eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_BADTYPE: 483eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 484eda14cbcSMatt Macy break; 485eda14cbcSMatt Macy case ZFS_ERR_WRONG_PARENT: 486eda14cbcSMatt Macy zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap); 487eda14cbcSMatt Macy break; 488eda14cbcSMatt Macy case ZFS_ERR_BADPROP: 489eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 490eda14cbcSMatt Macy break; 491*1f1e2261SMartin Matuska case ZFS_ERR_NOT_USER_NAMESPACE: 492*1f1e2261SMartin Matuska zfs_verror(hdl, EZFS_NOT_USER_NAMESPACE, fmt, ap); 493*1f1e2261SMartin Matuska break; 494eda14cbcSMatt Macy default: 49516038816SMartin Matuska zfs_error_aux(hdl, "%s", strerror(error)); 496eda14cbcSMatt Macy zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 497eda14cbcSMatt Macy break; 498eda14cbcSMatt Macy } 499eda14cbcSMatt Macy 500eda14cbcSMatt Macy va_end(ap); 501eda14cbcSMatt Macy return (-1); 502eda14cbcSMatt Macy } 503eda14cbcSMatt Macy 504eda14cbcSMatt Macy void 505eda14cbcSMatt Macy zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 506eda14cbcSMatt Macy char *errbuf) 507eda14cbcSMatt Macy { 508eda14cbcSMatt Macy switch (err) { 509eda14cbcSMatt Macy 510eda14cbcSMatt Macy case ENOSPC: 511eda14cbcSMatt Macy /* 512eda14cbcSMatt Macy * For quotas and reservations, ENOSPC indicates 513eda14cbcSMatt Macy * something different; setting a quota or reservation 514eda14cbcSMatt Macy * doesn't use any disk space. 515eda14cbcSMatt Macy */ 516eda14cbcSMatt Macy switch (prop) { 517eda14cbcSMatt Macy case ZFS_PROP_QUOTA: 518eda14cbcSMatt Macy case ZFS_PROP_REFQUOTA: 519eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 520eda14cbcSMatt Macy "size is less than current used or " 521eda14cbcSMatt Macy "reserved space")); 522eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 523eda14cbcSMatt Macy break; 524eda14cbcSMatt Macy 525eda14cbcSMatt Macy case ZFS_PROP_RESERVATION: 526eda14cbcSMatt Macy case ZFS_PROP_REFRESERVATION: 527eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 528eda14cbcSMatt Macy "size is greater than available space")); 529eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 530eda14cbcSMatt Macy break; 531eda14cbcSMatt Macy 532eda14cbcSMatt Macy default: 533eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf); 534eda14cbcSMatt Macy break; 535eda14cbcSMatt Macy } 536eda14cbcSMatt Macy break; 537eda14cbcSMatt Macy 538eda14cbcSMatt Macy case EBUSY: 539eda14cbcSMatt Macy (void) zfs_standard_error(hdl, EBUSY, errbuf); 540eda14cbcSMatt Macy break; 541eda14cbcSMatt Macy 542eda14cbcSMatt Macy case EROFS: 543eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 544eda14cbcSMatt Macy break; 545eda14cbcSMatt Macy 546eda14cbcSMatt Macy case E2BIG: 547eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 548eda14cbcSMatt Macy "property value too long")); 549eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 550eda14cbcSMatt Macy break; 551eda14cbcSMatt Macy 552eda14cbcSMatt Macy case ENOTSUP: 553eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 554eda14cbcSMatt Macy "pool and or dataset must be upgraded to set this " 555eda14cbcSMatt Macy "property or value")); 556eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 557eda14cbcSMatt Macy break; 558eda14cbcSMatt Macy 559eda14cbcSMatt Macy case ERANGE: 560eda14cbcSMatt Macy if (prop == ZFS_PROP_COMPRESSION || 561eda14cbcSMatt Macy prop == ZFS_PROP_DNODESIZE || 562eda14cbcSMatt Macy prop == ZFS_PROP_RECORDSIZE) { 563eda14cbcSMatt Macy (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 564eda14cbcSMatt Macy "property setting is not allowed on " 565eda14cbcSMatt Macy "bootable datasets")); 566eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 567eda14cbcSMatt Macy } else if (prop == ZFS_PROP_CHECKSUM || 568eda14cbcSMatt Macy prop == ZFS_PROP_DEDUP) { 569eda14cbcSMatt Macy (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 570eda14cbcSMatt Macy "property setting is not allowed on " 571eda14cbcSMatt Macy "root pools")); 572eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 573eda14cbcSMatt Macy } else { 574eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf); 575eda14cbcSMatt Macy } 576eda14cbcSMatt Macy break; 577eda14cbcSMatt Macy 578eda14cbcSMatt Macy case EINVAL: 579eda14cbcSMatt Macy if (prop == ZPROP_INVAL) { 580eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 581eda14cbcSMatt Macy } else { 582eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf); 583eda14cbcSMatt Macy } 584eda14cbcSMatt Macy break; 585eda14cbcSMatt Macy 586eda14cbcSMatt Macy case ZFS_ERR_BADPROP: 587eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 588eda14cbcSMatt Macy break; 589eda14cbcSMatt Macy 590eda14cbcSMatt Macy case EACCES: 591eda14cbcSMatt Macy if (prop == ZFS_PROP_KEYLOCATION) { 592eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 593eda14cbcSMatt Macy "keylocation may only be set on encryption roots")); 594eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 595eda14cbcSMatt Macy } else { 596eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf); 597eda14cbcSMatt Macy } 598eda14cbcSMatt Macy break; 599eda14cbcSMatt Macy 600eda14cbcSMatt Macy case EOVERFLOW: 601eda14cbcSMatt Macy /* 602eda14cbcSMatt Macy * This platform can't address a volume this big. 603eda14cbcSMatt Macy */ 604eda14cbcSMatt Macy #ifdef _ILP32 605eda14cbcSMatt Macy if (prop == ZFS_PROP_VOLSIZE) { 606eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 607eda14cbcSMatt Macy break; 608eda14cbcSMatt Macy } 609c03c5b1cSMartin Matuska zfs_fallthrough; 610eda14cbcSMatt Macy #endif 611eda14cbcSMatt Macy default: 612eda14cbcSMatt Macy (void) zfs_standard_error(hdl, err, errbuf); 613eda14cbcSMatt Macy } 614eda14cbcSMatt Macy } 615eda14cbcSMatt Macy 616eda14cbcSMatt Macy int 617eda14cbcSMatt Macy zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 618eda14cbcSMatt Macy { 619eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 620eda14cbcSMatt Macy } 621eda14cbcSMatt Macy 622eda14cbcSMatt Macy int 623eda14cbcSMatt Macy zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 624eda14cbcSMatt Macy { 625eda14cbcSMatt Macy va_list ap; 626eda14cbcSMatt Macy 627eda14cbcSMatt Macy va_start(ap, fmt); 628eda14cbcSMatt Macy 629eda14cbcSMatt Macy if (zfs_common_error(hdl, error, fmt, ap) != 0) { 630eda14cbcSMatt Macy va_end(ap); 631eda14cbcSMatt Macy return (-1); 632eda14cbcSMatt Macy } 633eda14cbcSMatt Macy 634eda14cbcSMatt Macy switch (error) { 635eda14cbcSMatt Macy case ENODEV: 636eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 637eda14cbcSMatt Macy break; 638eda14cbcSMatt Macy 639eda14cbcSMatt Macy case ENOENT: 640eda14cbcSMatt Macy zfs_error_aux(hdl, 641eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "no such pool or dataset")); 642eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOENT, fmt, ap); 643eda14cbcSMatt Macy break; 644eda14cbcSMatt Macy 645eda14cbcSMatt Macy case EEXIST: 646eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 647eda14cbcSMatt Macy "pool already exists")); 648eda14cbcSMatt Macy zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 649eda14cbcSMatt Macy break; 650eda14cbcSMatt Macy 651eda14cbcSMatt Macy case EBUSY: 652eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 653eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BUSY, fmt, ap); 654eda14cbcSMatt Macy break; 655eda14cbcSMatt Macy 656eda14cbcSMatt Macy /* There is no pending operation to cancel */ 657eda14cbcSMatt Macy case ENOTACTIVE: 658eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap); 659eda14cbcSMatt Macy break; 660eda14cbcSMatt Macy 661eda14cbcSMatt Macy case ENXIO: 662eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 663eda14cbcSMatt Macy "one or more devices is currently unavailable")); 664eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 665eda14cbcSMatt Macy break; 666eda14cbcSMatt Macy 667eda14cbcSMatt Macy case ENAMETOOLONG: 668eda14cbcSMatt Macy zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 669eda14cbcSMatt Macy break; 670eda14cbcSMatt Macy 671eda14cbcSMatt Macy case ENOTSUP: 672eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 673eda14cbcSMatt Macy break; 674eda14cbcSMatt Macy 675eda14cbcSMatt Macy case EINVAL: 676eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 677eda14cbcSMatt Macy break; 678eda14cbcSMatt Macy 679eda14cbcSMatt Macy case ENOSPC: 680eda14cbcSMatt Macy case EDQUOT: 681eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 682eda14cbcSMatt Macy return (-1); 683eda14cbcSMatt Macy 684eda14cbcSMatt Macy case EAGAIN: 685eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 686eda14cbcSMatt Macy "pool I/O is currently suspended")); 687eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 688eda14cbcSMatt Macy break; 689eda14cbcSMatt Macy 690eda14cbcSMatt Macy case EROFS: 691eda14cbcSMatt Macy zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 692eda14cbcSMatt Macy break; 693eda14cbcSMatt Macy case EDOM: 694eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 695eda14cbcSMatt Macy "block size out of range or does not match")); 696eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 697eda14cbcSMatt Macy break; 698eda14cbcSMatt Macy case EREMOTEIO: 699eda14cbcSMatt Macy zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 700eda14cbcSMatt Macy break; 701eda14cbcSMatt Macy case ZFS_ERR_CHECKPOINT_EXISTS: 702eda14cbcSMatt Macy zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap); 703eda14cbcSMatt Macy break; 704eda14cbcSMatt Macy case ZFS_ERR_DISCARDING_CHECKPOINT: 705eda14cbcSMatt Macy zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap); 706eda14cbcSMatt Macy break; 707eda14cbcSMatt Macy case ZFS_ERR_NO_CHECKPOINT: 708eda14cbcSMatt Macy zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap); 709eda14cbcSMatt Macy break; 710eda14cbcSMatt Macy case ZFS_ERR_DEVRM_IN_PROGRESS: 711eda14cbcSMatt Macy zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap); 712eda14cbcSMatt Macy break; 713eda14cbcSMatt Macy case ZFS_ERR_VDEV_TOO_BIG: 714eda14cbcSMatt Macy zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap); 715eda14cbcSMatt Macy break; 716eda14cbcSMatt Macy case ZFS_ERR_EXPORT_IN_PROGRESS: 717eda14cbcSMatt Macy zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap); 718eda14cbcSMatt Macy break; 719eda14cbcSMatt Macy case ZFS_ERR_RESILVER_IN_PROGRESS: 720eda14cbcSMatt Macy zfs_verror(hdl, EZFS_RESILVERING, fmt, ap); 721eda14cbcSMatt Macy break; 722eda14cbcSMatt Macy case ZFS_ERR_REBUILD_IN_PROGRESS: 723eda14cbcSMatt Macy zfs_verror(hdl, EZFS_REBUILDING, fmt, ap); 724eda14cbcSMatt Macy break; 725eda14cbcSMatt Macy case ZFS_ERR_BADPROP: 726eda14cbcSMatt Macy zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 727eda14cbcSMatt Macy break; 728681ce946SMartin Matuska case ZFS_ERR_VDEV_NOTSUP: 729681ce946SMartin Matuska zfs_verror(hdl, EZFS_VDEV_NOTSUP, fmt, ap); 730681ce946SMartin Matuska break; 731eda14cbcSMatt Macy case ZFS_ERR_IOC_CMD_UNAVAIL: 732eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 733eda14cbcSMatt Macy "module does not support this operation. A reboot may " 734eda14cbcSMatt Macy "be required to enable this operation.")); 735eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 736eda14cbcSMatt Macy break; 737eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_UNAVAIL: 738eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 739eda14cbcSMatt Macy "module does not support an option for this operation. " 740eda14cbcSMatt Macy "A reboot may be required to enable this option.")); 741eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 742eda14cbcSMatt Macy break; 743eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_REQUIRED: 744eda14cbcSMatt Macy case ZFS_ERR_IOC_ARG_BADTYPE: 745eda14cbcSMatt Macy zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 746eda14cbcSMatt Macy break; 747eda14cbcSMatt Macy default: 74816038816SMartin Matuska zfs_error_aux(hdl, "%s", strerror(error)); 749eda14cbcSMatt Macy zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 750eda14cbcSMatt Macy } 751eda14cbcSMatt Macy 752eda14cbcSMatt Macy va_end(ap); 753eda14cbcSMatt Macy return (-1); 754eda14cbcSMatt Macy } 755eda14cbcSMatt Macy 756eda14cbcSMatt Macy /* 757eda14cbcSMatt Macy * Display an out of memory error message and abort the current program. 758eda14cbcSMatt Macy */ 759eda14cbcSMatt Macy int 760eda14cbcSMatt Macy no_memory(libzfs_handle_t *hdl) 761eda14cbcSMatt Macy { 762eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 763eda14cbcSMatt Macy } 764eda14cbcSMatt Macy 765eda14cbcSMatt Macy /* 766eda14cbcSMatt Macy * A safe form of malloc() which will die if the allocation fails. 767eda14cbcSMatt Macy */ 768eda14cbcSMatt Macy void * 769eda14cbcSMatt Macy zfs_alloc(libzfs_handle_t *hdl, size_t size) 770eda14cbcSMatt Macy { 771eda14cbcSMatt Macy void *data; 772eda14cbcSMatt Macy 773eda14cbcSMatt Macy if ((data = calloc(1, size)) == NULL) 774eda14cbcSMatt Macy (void) no_memory(hdl); 775eda14cbcSMatt Macy 776eda14cbcSMatt Macy return (data); 777eda14cbcSMatt Macy } 778eda14cbcSMatt Macy 779eda14cbcSMatt Macy /* 780eda14cbcSMatt Macy * A safe form of asprintf() which will die if the allocation fails. 781eda14cbcSMatt Macy */ 782eda14cbcSMatt Macy char * 783eda14cbcSMatt Macy zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) 784eda14cbcSMatt Macy { 785eda14cbcSMatt Macy va_list ap; 786eda14cbcSMatt Macy char *ret; 787eda14cbcSMatt Macy int err; 788eda14cbcSMatt Macy 789eda14cbcSMatt Macy va_start(ap, fmt); 790eda14cbcSMatt Macy 791eda14cbcSMatt Macy err = vasprintf(&ret, fmt, ap); 792eda14cbcSMatt Macy 793eda14cbcSMatt Macy va_end(ap); 794eda14cbcSMatt Macy 79516038816SMartin Matuska if (err < 0) { 796eda14cbcSMatt Macy (void) no_memory(hdl); 79716038816SMartin Matuska ret = NULL; 79816038816SMartin Matuska } 799eda14cbcSMatt Macy 800eda14cbcSMatt Macy return (ret); 801eda14cbcSMatt Macy } 802eda14cbcSMatt Macy 803eda14cbcSMatt Macy /* 804eda14cbcSMatt Macy * A safe form of realloc(), which also zeroes newly allocated space. 805eda14cbcSMatt Macy */ 806eda14cbcSMatt Macy void * 807eda14cbcSMatt Macy zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 808eda14cbcSMatt Macy { 809eda14cbcSMatt Macy void *ret; 810eda14cbcSMatt Macy 811eda14cbcSMatt Macy if ((ret = realloc(ptr, newsize)) == NULL) { 812eda14cbcSMatt Macy (void) no_memory(hdl); 813eda14cbcSMatt Macy return (NULL); 814eda14cbcSMatt Macy } 815eda14cbcSMatt Macy 816da5137abSMartin Matuska memset((char *)ret + oldsize, 0, newsize - oldsize); 817eda14cbcSMatt Macy return (ret); 818eda14cbcSMatt Macy } 819eda14cbcSMatt Macy 820eda14cbcSMatt Macy /* 821eda14cbcSMatt Macy * A safe form of strdup() which will die if the allocation fails. 822eda14cbcSMatt Macy */ 823eda14cbcSMatt Macy char * 824eda14cbcSMatt Macy zfs_strdup(libzfs_handle_t *hdl, const char *str) 825eda14cbcSMatt Macy { 826eda14cbcSMatt Macy char *ret; 827eda14cbcSMatt Macy 828eda14cbcSMatt Macy if ((ret = strdup(str)) == NULL) 829eda14cbcSMatt Macy (void) no_memory(hdl); 830eda14cbcSMatt Macy 831eda14cbcSMatt Macy return (ret); 832eda14cbcSMatt Macy } 833eda14cbcSMatt Macy 834eda14cbcSMatt Macy void 835eda14cbcSMatt Macy libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 836eda14cbcSMatt Macy { 837eda14cbcSMatt Macy hdl->libzfs_printerr = printerr; 838eda14cbcSMatt Macy } 839eda14cbcSMatt Macy 840eda14cbcSMatt Macy /* 841eda14cbcSMatt Macy * Read lines from an open file descriptor and store them in an array of 842eda14cbcSMatt Macy * strings until EOF. lines[] will be allocated and populated with all the 843eda14cbcSMatt Macy * lines read. All newlines are replaced with NULL terminators for 844eda14cbcSMatt Macy * convenience. lines[] must be freed after use with libzfs_free_str_array(). 845eda14cbcSMatt Macy * 846eda14cbcSMatt Macy * Returns the number of lines read. 847eda14cbcSMatt Macy */ 848eda14cbcSMatt Macy static int 849eda14cbcSMatt Macy libzfs_read_stdout_from_fd(int fd, char **lines[]) 850eda14cbcSMatt Macy { 851eda14cbcSMatt Macy 852eda14cbcSMatt Macy FILE *fp; 853eda14cbcSMatt Macy int lines_cnt = 0; 854eda14cbcSMatt Macy size_t len = 0; 855eda14cbcSMatt Macy char *line = NULL; 856eda14cbcSMatt Macy char **tmp_lines = NULL, **tmp; 857eda14cbcSMatt Macy 858eda14cbcSMatt Macy fp = fdopen(fd, "r"); 85916038816SMartin Matuska if (fp == NULL) { 86016038816SMartin Matuska close(fd); 861eda14cbcSMatt Macy return (0); 86216038816SMartin Matuska } 86316038816SMartin Matuska while (getline(&line, &len, fp) != -1) { 864eda14cbcSMatt Macy tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1)); 865eda14cbcSMatt Macy if (tmp == NULL) { 866eda14cbcSMatt Macy /* Return the lines we were able to process */ 867eda14cbcSMatt Macy break; 868eda14cbcSMatt Macy } 869eda14cbcSMatt Macy tmp_lines = tmp; 870eda14cbcSMatt Macy 87116038816SMartin Matuska /* Remove newline if not EOF */ 87216038816SMartin Matuska if (line[strlen(line) - 1] == '\n') 87316038816SMartin Matuska line[strlen(line) - 1] = '\0'; 87416038816SMartin Matuska 87516038816SMartin Matuska tmp_lines[lines_cnt] = strdup(line); 87616038816SMartin Matuska if (tmp_lines[lines_cnt] == NULL) 87716038816SMartin Matuska break; 87816038816SMartin Matuska ++lines_cnt; 879eda14cbcSMatt Macy } 88016038816SMartin Matuska free(line); 881eda14cbcSMatt Macy fclose(fp); 882eda14cbcSMatt Macy *lines = tmp_lines; 883eda14cbcSMatt Macy return (lines_cnt); 884eda14cbcSMatt Macy } 885eda14cbcSMatt Macy 886eda14cbcSMatt Macy static int 887eda14cbcSMatt Macy libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags, 888eda14cbcSMatt Macy char **lines[], int *lines_cnt) 889eda14cbcSMatt Macy { 890eda14cbcSMatt Macy pid_t pid; 891eda14cbcSMatt Macy int error, devnull_fd; 892eda14cbcSMatt Macy int link[2]; 893eda14cbcSMatt Macy 894eda14cbcSMatt Macy /* 895eda14cbcSMatt Macy * Setup a pipe between our child and parent process if we're 896eda14cbcSMatt Macy * reading stdout. 897eda14cbcSMatt Macy */ 89816038816SMartin Matuska if (lines != NULL && pipe2(link, O_NONBLOCK | O_CLOEXEC) == -1) 899eda14cbcSMatt Macy return (-EPIPE); 900eda14cbcSMatt Macy 90116038816SMartin Matuska pid = fork(); 902eda14cbcSMatt Macy if (pid == 0) { 903eda14cbcSMatt Macy /* Child process */ 90416038816SMartin Matuska devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC); 905eda14cbcSMatt Macy 906eda14cbcSMatt Macy if (devnull_fd < 0) 907eda14cbcSMatt Macy _exit(-1); 908eda14cbcSMatt Macy 909eda14cbcSMatt Macy if (!(flags & STDOUT_VERBOSE) && (lines == NULL)) 910eda14cbcSMatt Macy (void) dup2(devnull_fd, STDOUT_FILENO); 911eda14cbcSMatt Macy else if (lines != NULL) { 912eda14cbcSMatt Macy /* Save the output to lines[] */ 913eda14cbcSMatt Macy dup2(link[1], STDOUT_FILENO); 914eda14cbcSMatt Macy } 915eda14cbcSMatt Macy 916eda14cbcSMatt Macy if (!(flags & STDERR_VERBOSE)) 917eda14cbcSMatt Macy (void) dup2(devnull_fd, STDERR_FILENO); 918eda14cbcSMatt Macy 919eda14cbcSMatt Macy if (flags & NO_DEFAULT_PATH) { 920eda14cbcSMatt Macy if (env == NULL) 921eda14cbcSMatt Macy execv(path, argv); 922eda14cbcSMatt Macy else 923eda14cbcSMatt Macy execve(path, argv, env); 924eda14cbcSMatt Macy } else { 925eda14cbcSMatt Macy if (env == NULL) 926eda14cbcSMatt Macy execvp(path, argv); 927eda14cbcSMatt Macy else 928eda14cbcSMatt Macy execvpe(path, argv, env); 929eda14cbcSMatt Macy } 930eda14cbcSMatt Macy 931eda14cbcSMatt Macy _exit(-1); 932eda14cbcSMatt Macy } else if (pid > 0) { 933eda14cbcSMatt Macy /* Parent process */ 934eda14cbcSMatt Macy int status; 935eda14cbcSMatt Macy 936eda14cbcSMatt Macy while ((error = waitpid(pid, &status, 0)) == -1 && 93716038816SMartin Matuska errno == EINTR) 93816038816SMartin Matuska ; 939eda14cbcSMatt Macy if (error < 0 || !WIFEXITED(status)) 940eda14cbcSMatt Macy return (-1); 941eda14cbcSMatt Macy 942eda14cbcSMatt Macy if (lines != NULL) { 943eda14cbcSMatt Macy close(link[1]); 944eda14cbcSMatt Macy *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines); 945eda14cbcSMatt Macy } 946eda14cbcSMatt Macy return (WEXITSTATUS(status)); 947eda14cbcSMatt Macy } 948eda14cbcSMatt Macy 949eda14cbcSMatt Macy return (-1); 950eda14cbcSMatt Macy } 951eda14cbcSMatt Macy 952eda14cbcSMatt Macy int 953eda14cbcSMatt Macy libzfs_run_process(const char *path, char *argv[], int flags) 954eda14cbcSMatt Macy { 955eda14cbcSMatt Macy return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL)); 956eda14cbcSMatt Macy } 957eda14cbcSMatt Macy 958eda14cbcSMatt Macy /* 959eda14cbcSMatt Macy * Run a command and store its stdout lines in an array of strings (lines[]). 960eda14cbcSMatt Macy * lines[] is allocated and populated for you, and the number of lines is set in 961eda14cbcSMatt Macy * lines_cnt. lines[] must be freed after use with libzfs_free_str_array(). 962eda14cbcSMatt Macy * All newlines (\n) in lines[] are terminated for convenience. 963eda14cbcSMatt Macy */ 964eda14cbcSMatt Macy int 965eda14cbcSMatt Macy libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[], 966eda14cbcSMatt Macy char **lines[], int *lines_cnt) 967eda14cbcSMatt Macy { 968eda14cbcSMatt Macy return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt)); 969eda14cbcSMatt Macy } 970eda14cbcSMatt Macy 971eda14cbcSMatt Macy /* 972eda14cbcSMatt Macy * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This 973eda14cbcSMatt Macy * means that *path needs to be the full path to the executable. 974eda14cbcSMatt Macy */ 975eda14cbcSMatt Macy int 976eda14cbcSMatt Macy libzfs_run_process_get_stdout_nopath(const char *path, char *argv[], 977eda14cbcSMatt Macy char *env[], char **lines[], int *lines_cnt) 978eda14cbcSMatt Macy { 979eda14cbcSMatt Macy return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH, 980eda14cbcSMatt Macy lines, lines_cnt)); 981eda14cbcSMatt Macy } 982eda14cbcSMatt Macy 983eda14cbcSMatt Macy /* 984eda14cbcSMatt Macy * Free an array of strings. Free both the strings contained in the array and 985eda14cbcSMatt Macy * the array itself. 986eda14cbcSMatt Macy */ 987eda14cbcSMatt Macy void 988eda14cbcSMatt Macy libzfs_free_str_array(char **strs, int count) 989eda14cbcSMatt Macy { 990eda14cbcSMatt Macy while (--count >= 0) 991eda14cbcSMatt Macy free(strs[count]); 992eda14cbcSMatt Macy 993eda14cbcSMatt Macy free(strs); 994eda14cbcSMatt Macy } 995eda14cbcSMatt Macy 996eda14cbcSMatt Macy /* 997eda14cbcSMatt Macy * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or 998eda14cbcSMatt Macy * a non-zero number. 999eda14cbcSMatt Macy * 1000eda14cbcSMatt Macy * Returns 0 otherwise. 1001eda14cbcSMatt Macy */ 1002eda14cbcSMatt Macy int 1003eda14cbcSMatt Macy libzfs_envvar_is_set(char *envvar) 1004eda14cbcSMatt Macy { 1005eda14cbcSMatt Macy char *env = getenv(envvar); 1006eda14cbcSMatt Macy if (env && (strtoul(env, NULL, 0) > 0 || 1007eda14cbcSMatt Macy (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) || 1008eda14cbcSMatt Macy (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2))) 1009eda14cbcSMatt Macy return (1); 1010eda14cbcSMatt Macy 1011eda14cbcSMatt Macy return (0); 1012eda14cbcSMatt Macy } 1013eda14cbcSMatt Macy 1014eda14cbcSMatt Macy libzfs_handle_t * 1015eda14cbcSMatt Macy libzfs_init(void) 1016eda14cbcSMatt Macy { 1017eda14cbcSMatt Macy libzfs_handle_t *hdl; 1018eda14cbcSMatt Macy int error; 1019ac0bf12eSMatt Macy char *env; 1020eda14cbcSMatt Macy 1021180f8225SMatt Macy if ((error = libzfs_load_module()) != 0) { 1022eda14cbcSMatt Macy errno = error; 1023eda14cbcSMatt Macy return (NULL); 1024eda14cbcSMatt Macy } 1025eda14cbcSMatt Macy 1026eda14cbcSMatt Macy if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { 1027eda14cbcSMatt Macy return (NULL); 1028eda14cbcSMatt Macy } 1029eda14cbcSMatt Macy 1030eda14cbcSMatt Macy if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) { 1031eda14cbcSMatt Macy free(hdl); 1032eda14cbcSMatt Macy return (NULL); 1033eda14cbcSMatt Macy } 1034eda14cbcSMatt Macy 103516038816SMartin Matuska if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) { 1036eda14cbcSMatt Macy free(hdl); 1037eda14cbcSMatt Macy return (NULL); 1038eda14cbcSMatt Macy } 1039eda14cbcSMatt Macy 1040eda14cbcSMatt Macy if (libzfs_core_init() != 0) { 1041eda14cbcSMatt Macy (void) close(hdl->libzfs_fd); 1042eda14cbcSMatt Macy free(hdl); 1043eda14cbcSMatt Macy return (NULL); 1044eda14cbcSMatt Macy } 1045eda14cbcSMatt Macy 1046eda14cbcSMatt Macy zfs_prop_init(); 1047eda14cbcSMatt Macy zpool_prop_init(); 1048eda14cbcSMatt Macy zpool_feature_init(); 1049681ce946SMartin Matuska vdev_prop_init(); 1050eda14cbcSMatt Macy libzfs_mnttab_init(hdl); 1051eda14cbcSMatt Macy fletcher_4_init(); 1052eda14cbcSMatt Macy 1053eda14cbcSMatt Macy if (getenv("ZFS_PROP_DEBUG") != NULL) { 1054eda14cbcSMatt Macy hdl->libzfs_prop_debug = B_TRUE; 1055eda14cbcSMatt Macy } 1056ac0bf12eSMatt Macy if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) { 1057ac0bf12eSMatt Macy if ((error = zfs_nicestrtonum(hdl, env, 1058ac0bf12eSMatt Macy &hdl->libzfs_max_nvlist))) { 1059ac0bf12eSMatt Macy errno = error; 10602c48331dSMatt Macy (void) close(hdl->libzfs_fd); 10612c48331dSMatt Macy free(hdl); 1062ac0bf12eSMatt Macy return (NULL); 1063ac0bf12eSMatt Macy } 1064ac0bf12eSMatt Macy } else { 1065ac0bf12eSMatt Macy hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4); 1066ac0bf12eSMatt Macy } 1067eda14cbcSMatt Macy 1068eda14cbcSMatt Macy /* 1069eda14cbcSMatt Macy * For testing, remove some settable properties and features 1070eda14cbcSMatt Macy */ 1071eda14cbcSMatt Macy if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) { 1072eda14cbcSMatt Macy zprop_desc_t *proptbl; 1073eda14cbcSMatt Macy 1074eda14cbcSMatt Macy proptbl = zpool_prop_get_table(); 1075eda14cbcSMatt Macy proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE; 1076eda14cbcSMatt Macy 1077eda14cbcSMatt Macy proptbl = zfs_prop_get_table(); 1078eda14cbcSMatt Macy proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE; 1079eda14cbcSMatt Macy 1080eda14cbcSMatt Macy zfeature_info_t *ftbl = spa_feature_table; 1081eda14cbcSMatt Macy ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE; 1082eda14cbcSMatt Macy } 1083eda14cbcSMatt Macy 1084eda14cbcSMatt Macy return (hdl); 1085eda14cbcSMatt Macy } 1086eda14cbcSMatt Macy 1087eda14cbcSMatt Macy void 1088eda14cbcSMatt Macy libzfs_fini(libzfs_handle_t *hdl) 1089eda14cbcSMatt Macy { 1090eda14cbcSMatt Macy (void) close(hdl->libzfs_fd); 1091eda14cbcSMatt Macy zpool_free_handles(hdl); 1092eda14cbcSMatt Macy namespace_clear(hdl); 1093eda14cbcSMatt Macy libzfs_mnttab_fini(hdl); 1094eda14cbcSMatt Macy libzfs_core_fini(); 1095eda14cbcSMatt Macy regfree(&hdl->libzfs_urire); 1096eda14cbcSMatt Macy fletcher_4_fini(); 109716038816SMartin Matuska #if LIBFETCH_DYNAMIC 109816038816SMartin Matuska if (hdl->libfetch != (void *)-1 && hdl->libfetch != NULL) 109916038816SMartin Matuska (void) dlclose(hdl->libfetch); 110016038816SMartin Matuska free(hdl->libfetch_load_error); 110116038816SMartin Matuska #endif 1102eda14cbcSMatt Macy free(hdl); 1103eda14cbcSMatt Macy } 1104eda14cbcSMatt Macy 1105eda14cbcSMatt Macy libzfs_handle_t * 1106eda14cbcSMatt Macy zpool_get_handle(zpool_handle_t *zhp) 1107eda14cbcSMatt Macy { 1108eda14cbcSMatt Macy return (zhp->zpool_hdl); 1109eda14cbcSMatt Macy } 1110eda14cbcSMatt Macy 1111eda14cbcSMatt Macy libzfs_handle_t * 1112eda14cbcSMatt Macy zfs_get_handle(zfs_handle_t *zhp) 1113eda14cbcSMatt Macy { 1114eda14cbcSMatt Macy return (zhp->zfs_hdl); 1115eda14cbcSMatt Macy } 1116eda14cbcSMatt Macy 1117eda14cbcSMatt Macy zpool_handle_t * 1118eda14cbcSMatt Macy zfs_get_pool_handle(const zfs_handle_t *zhp) 1119eda14cbcSMatt Macy { 1120eda14cbcSMatt Macy return (zhp->zpool_hdl); 1121eda14cbcSMatt Macy } 1122eda14cbcSMatt Macy 1123eda14cbcSMatt Macy /* 1124eda14cbcSMatt Macy * Given a name, determine whether or not it's a valid path 1125eda14cbcSMatt Macy * (starts with '/' or "./"). If so, walk the mnttab trying 1126eda14cbcSMatt Macy * to match the device number. If not, treat the path as an 1127eda14cbcSMatt Macy * fs/vol/snap/bkmark name. 1128eda14cbcSMatt Macy */ 1129eda14cbcSMatt Macy zfs_handle_t * 1130eda14cbcSMatt Macy zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype) 1131eda14cbcSMatt Macy { 1132eda14cbcSMatt Macy struct stat64 statbuf; 1133eda14cbcSMatt Macy struct extmnttab entry; 1134eda14cbcSMatt Macy 1135eda14cbcSMatt Macy if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 1136eda14cbcSMatt Macy /* 1137eda14cbcSMatt Macy * It's not a valid path, assume it's a name of type 'argtype'. 1138eda14cbcSMatt Macy */ 1139eda14cbcSMatt Macy return (zfs_open(hdl, path, argtype)); 1140eda14cbcSMatt Macy } 1141eda14cbcSMatt Macy 1142eda14cbcSMatt Macy if (getextmntent(path, &entry, &statbuf) != 0) 1143eda14cbcSMatt Macy return (NULL); 1144eda14cbcSMatt Macy 1145eda14cbcSMatt Macy if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 1146eda14cbcSMatt Macy (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 1147eda14cbcSMatt Macy path); 1148eda14cbcSMatt Macy return (NULL); 1149eda14cbcSMatt Macy } 1150eda14cbcSMatt Macy 1151eda14cbcSMatt Macy return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 1152eda14cbcSMatt Macy } 1153eda14cbcSMatt Macy 1154eda14cbcSMatt Macy /* 1155eda14cbcSMatt Macy * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 1156eda14cbcSMatt Macy * an ioctl(). 1157eda14cbcSMatt Macy */ 1158716fd348SMartin Matuska void 1159eda14cbcSMatt Macy zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 1160eda14cbcSMatt Macy { 1161eda14cbcSMatt Macy if (len == 0) 1162eda14cbcSMatt Macy len = 256 * 1024; 1163eda14cbcSMatt Macy zc->zc_nvlist_dst_size = len; 1164eda14cbcSMatt Macy zc->zc_nvlist_dst = 1165eda14cbcSMatt Macy (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 1166eda14cbcSMatt Macy } 1167eda14cbcSMatt Macy 1168eda14cbcSMatt Macy /* 1169eda14cbcSMatt Macy * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 1170eda14cbcSMatt Macy * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 1171eda14cbcSMatt Macy * filled in by the kernel to indicate the actual required size. 1172eda14cbcSMatt Macy */ 1173716fd348SMartin Matuska void 1174eda14cbcSMatt Macy zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 1175eda14cbcSMatt Macy { 1176eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_dst); 1177eda14cbcSMatt Macy zc->zc_nvlist_dst = 1178eda14cbcSMatt Macy (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 1179eda14cbcSMatt Macy } 1180eda14cbcSMatt Macy 1181eda14cbcSMatt Macy /* 1182eda14cbcSMatt Macy * Called to free the src and dst nvlists stored in the command structure. 1183eda14cbcSMatt Macy */ 1184eda14cbcSMatt Macy void 1185eda14cbcSMatt Macy zcmd_free_nvlists(zfs_cmd_t *zc) 1186eda14cbcSMatt Macy { 1187eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_conf); 1188eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_src); 1189eda14cbcSMatt Macy free((void *)(uintptr_t)zc->zc_nvlist_dst); 1190eda14cbcSMatt Macy zc->zc_nvlist_conf = 0; 1191eda14cbcSMatt Macy zc->zc_nvlist_src = 0; 1192eda14cbcSMatt Macy zc->zc_nvlist_dst = 0; 1193eda14cbcSMatt Macy } 1194eda14cbcSMatt Macy 1195716fd348SMartin Matuska static void 1196eda14cbcSMatt Macy zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 1197eda14cbcSMatt Macy nvlist_t *nvl) 1198eda14cbcSMatt Macy { 1199eda14cbcSMatt Macy char *packed; 1200eda14cbcSMatt Macy 1201da5137abSMartin Matuska size_t len = fnvlist_size(nvl); 1202716fd348SMartin Matuska packed = zfs_alloc(hdl, len); 1203eda14cbcSMatt Macy 1204eda14cbcSMatt Macy verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 1205eda14cbcSMatt Macy 1206eda14cbcSMatt Macy *outnv = (uint64_t)(uintptr_t)packed; 1207eda14cbcSMatt Macy *outlen = len; 1208eda14cbcSMatt Macy } 1209eda14cbcSMatt Macy 1210716fd348SMartin Matuska void 1211eda14cbcSMatt Macy zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 1212eda14cbcSMatt Macy { 1213716fd348SMartin Matuska zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 1214716fd348SMartin Matuska &zc->zc_nvlist_conf_size, nvl); 1215eda14cbcSMatt Macy } 1216eda14cbcSMatt Macy 1217716fd348SMartin Matuska void 1218eda14cbcSMatt Macy zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 1219eda14cbcSMatt Macy { 1220716fd348SMartin Matuska zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 1221716fd348SMartin Matuska &zc->zc_nvlist_src_size, nvl); 1222eda14cbcSMatt Macy } 1223eda14cbcSMatt Macy 1224eda14cbcSMatt Macy /* 1225eda14cbcSMatt Macy * Unpacks an nvlist from the ZFS ioctl command structure. 1226eda14cbcSMatt Macy */ 1227eda14cbcSMatt Macy int 1228eda14cbcSMatt Macy zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 1229eda14cbcSMatt Macy { 1230eda14cbcSMatt Macy if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 1231eda14cbcSMatt Macy zc->zc_nvlist_dst_size, nvlp, 0) != 0) 1232eda14cbcSMatt Macy return (no_memory(hdl)); 1233eda14cbcSMatt Macy 1234eda14cbcSMatt Macy return (0); 1235eda14cbcSMatt Macy } 1236eda14cbcSMatt Macy 1237eda14cbcSMatt Macy /* 1238eda14cbcSMatt Macy * ================================================================ 1239eda14cbcSMatt Macy * API shared by zfs and zpool property management 1240eda14cbcSMatt Macy * ================================================================ 1241eda14cbcSMatt Macy */ 1242eda14cbcSMatt Macy 1243eda14cbcSMatt Macy static void 1244eda14cbcSMatt Macy zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 1245eda14cbcSMatt Macy { 1246eda14cbcSMatt Macy zprop_list_t *pl = cbp->cb_proplist; 1247eda14cbcSMatt Macy int i; 1248eda14cbcSMatt Macy char *title; 1249eda14cbcSMatt Macy size_t len; 1250eda14cbcSMatt Macy 1251eda14cbcSMatt Macy cbp->cb_first = B_FALSE; 1252eda14cbcSMatt Macy if (cbp->cb_scripted) 1253eda14cbcSMatt Macy return; 1254eda14cbcSMatt Macy 1255eda14cbcSMatt Macy /* 1256eda14cbcSMatt Macy * Start with the length of the column headers. 1257eda14cbcSMatt Macy */ 1258eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 1259eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 1260eda14cbcSMatt Macy "PROPERTY")); 1261eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 1262eda14cbcSMatt Macy "VALUE")); 1263eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, 1264eda14cbcSMatt Macy "RECEIVED")); 1265eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 1266eda14cbcSMatt Macy "SOURCE")); 1267eda14cbcSMatt Macy 1268eda14cbcSMatt Macy /* first property is always NAME */ 1269eda14cbcSMatt Macy assert(cbp->cb_proplist->pl_prop == 1270681ce946SMartin Matuska ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1271681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME))); 1272eda14cbcSMatt Macy 1273eda14cbcSMatt Macy /* 1274eda14cbcSMatt Macy * Go through and calculate the widths for each column. For the 1275eda14cbcSMatt Macy * 'source' column, we kludge it up by taking the worst-case scenario of 1276eda14cbcSMatt Macy * inheriting from the longest name. This is acceptable because in the 1277eda14cbcSMatt Macy * majority of cases 'SOURCE' is the last column displayed, and we don't 1278eda14cbcSMatt Macy * use the width anyway. Note that the 'VALUE' column can be oversized, 1279eda14cbcSMatt Macy * if the name of the property is much longer than any values we find. 1280eda14cbcSMatt Macy */ 1281eda14cbcSMatt Macy for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 1282eda14cbcSMatt Macy /* 1283eda14cbcSMatt Macy * 'PROPERTY' column 1284eda14cbcSMatt Macy */ 1285*1f1e2261SMartin Matuska if (pl->pl_prop != ZPROP_USERPROP) { 1286eda14cbcSMatt Macy const char *propname = (type == ZFS_TYPE_POOL) ? 1287eda14cbcSMatt Macy zpool_prop_to_name(pl->pl_prop) : 1288681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ? 1289681ce946SMartin Matuska vdev_prop_to_name(pl->pl_prop) : 1290681ce946SMartin Matuska zfs_prop_to_name(pl->pl_prop)); 1291eda14cbcSMatt Macy 1292681ce946SMartin Matuska assert(propname != NULL); 1293eda14cbcSMatt Macy len = strlen(propname); 1294eda14cbcSMatt Macy if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 1295eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_PROPERTY] = len; 1296eda14cbcSMatt Macy } else { 1297681ce946SMartin Matuska assert(pl->pl_user_prop != NULL); 1298eda14cbcSMatt Macy len = strlen(pl->pl_user_prop); 1299eda14cbcSMatt Macy if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 1300eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_PROPERTY] = len; 1301eda14cbcSMatt Macy } 1302eda14cbcSMatt Macy 1303eda14cbcSMatt Macy /* 1304eda14cbcSMatt Macy * 'VALUE' column. The first property is always the 'name' 1305eda14cbcSMatt Macy * property that was tacked on either by /sbin/zfs's 1306eda14cbcSMatt Macy * zfs_do_get() or when calling zprop_expand_list(), so we 1307eda14cbcSMatt Macy * ignore its width. If the user specified the name property 1308eda14cbcSMatt Macy * to display, then it will be later in the list in any case. 1309eda14cbcSMatt Macy */ 1310eda14cbcSMatt Macy if (pl != cbp->cb_proplist && 1311eda14cbcSMatt Macy pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 1312eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 1313eda14cbcSMatt Macy 1314eda14cbcSMatt Macy /* 'RECEIVED' column. */ 1315eda14cbcSMatt Macy if (pl != cbp->cb_proplist && 1316eda14cbcSMatt Macy pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) 1317eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; 1318eda14cbcSMatt Macy 1319eda14cbcSMatt Macy /* 1320eda14cbcSMatt Macy * 'NAME' and 'SOURCE' columns 1321eda14cbcSMatt Macy */ 1322681ce946SMartin Matuska if (pl->pl_prop == ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1323681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : 1324681ce946SMartin Matuska ZFS_PROP_NAME)) && pl->pl_width > 1325681ce946SMartin Matuska cbp->cb_colwidths[GET_COL_NAME]) { 1326eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 1327eda14cbcSMatt Macy cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 1328eda14cbcSMatt Macy strlen(dgettext(TEXT_DOMAIN, "inherited from")); 1329eda14cbcSMatt Macy } 1330eda14cbcSMatt Macy } 1331eda14cbcSMatt Macy 1332eda14cbcSMatt Macy /* 1333eda14cbcSMatt Macy * Now go through and print the headers. 1334eda14cbcSMatt Macy */ 1335eda14cbcSMatt Macy for (i = 0; i < ZFS_GET_NCOLS; i++) { 1336eda14cbcSMatt Macy switch (cbp->cb_columns[i]) { 1337eda14cbcSMatt Macy case GET_COL_NAME: 1338eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "NAME"); 1339eda14cbcSMatt Macy break; 1340eda14cbcSMatt Macy case GET_COL_PROPERTY: 1341eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "PROPERTY"); 1342eda14cbcSMatt Macy break; 1343eda14cbcSMatt Macy case GET_COL_VALUE: 1344eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "VALUE"); 1345eda14cbcSMatt Macy break; 1346eda14cbcSMatt Macy case GET_COL_RECVD: 1347eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "RECEIVED"); 1348eda14cbcSMatt Macy break; 1349eda14cbcSMatt Macy case GET_COL_SOURCE: 1350eda14cbcSMatt Macy title = dgettext(TEXT_DOMAIN, "SOURCE"); 1351eda14cbcSMatt Macy break; 1352eda14cbcSMatt Macy default: 1353eda14cbcSMatt Macy title = NULL; 1354eda14cbcSMatt Macy } 1355eda14cbcSMatt Macy 1356eda14cbcSMatt Macy if (title != NULL) { 1357eda14cbcSMatt Macy if (i == (ZFS_GET_NCOLS - 1) || 1358eda14cbcSMatt Macy cbp->cb_columns[i + 1] == GET_COL_NONE) 1359eda14cbcSMatt Macy (void) printf("%s", title); 1360eda14cbcSMatt Macy else 1361eda14cbcSMatt Macy (void) printf("%-*s ", 1362eda14cbcSMatt Macy cbp->cb_colwidths[cbp->cb_columns[i]], 1363eda14cbcSMatt Macy title); 1364eda14cbcSMatt Macy } 1365eda14cbcSMatt Macy } 1366eda14cbcSMatt Macy (void) printf("\n"); 1367eda14cbcSMatt Macy } 1368eda14cbcSMatt Macy 1369eda14cbcSMatt Macy /* 1370eda14cbcSMatt Macy * Display a single line of output, according to the settings in the callback 1371eda14cbcSMatt Macy * structure. 1372eda14cbcSMatt Macy */ 1373eda14cbcSMatt Macy void 1374eda14cbcSMatt Macy zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 1375eda14cbcSMatt Macy const char *propname, const char *value, zprop_source_t sourcetype, 1376eda14cbcSMatt Macy const char *source, const char *recvd_value) 1377eda14cbcSMatt Macy { 1378eda14cbcSMatt Macy int i; 1379eda14cbcSMatt Macy const char *str = NULL; 1380eda14cbcSMatt Macy char buf[128]; 1381eda14cbcSMatt Macy 1382eda14cbcSMatt Macy /* 1383eda14cbcSMatt Macy * Ignore those source types that the user has chosen to ignore. 1384eda14cbcSMatt Macy */ 1385eda14cbcSMatt Macy if ((sourcetype & cbp->cb_sources) == 0) 1386eda14cbcSMatt Macy return; 1387eda14cbcSMatt Macy 1388eda14cbcSMatt Macy if (cbp->cb_first) 1389eda14cbcSMatt Macy zprop_print_headers(cbp, cbp->cb_type); 1390eda14cbcSMatt Macy 1391eda14cbcSMatt Macy for (i = 0; i < ZFS_GET_NCOLS; i++) { 1392eda14cbcSMatt Macy switch (cbp->cb_columns[i]) { 1393eda14cbcSMatt Macy case GET_COL_NAME: 1394eda14cbcSMatt Macy str = name; 1395eda14cbcSMatt Macy break; 1396eda14cbcSMatt Macy 1397eda14cbcSMatt Macy case GET_COL_PROPERTY: 1398eda14cbcSMatt Macy str = propname; 1399eda14cbcSMatt Macy break; 1400eda14cbcSMatt Macy 1401eda14cbcSMatt Macy case GET_COL_VALUE: 1402eda14cbcSMatt Macy str = value; 1403eda14cbcSMatt Macy break; 1404eda14cbcSMatt Macy 1405eda14cbcSMatt Macy case GET_COL_SOURCE: 1406eda14cbcSMatt Macy switch (sourcetype) { 1407eda14cbcSMatt Macy case ZPROP_SRC_NONE: 1408eda14cbcSMatt Macy str = "-"; 1409eda14cbcSMatt Macy break; 1410eda14cbcSMatt Macy 1411eda14cbcSMatt Macy case ZPROP_SRC_DEFAULT: 1412eda14cbcSMatt Macy str = "default"; 1413eda14cbcSMatt Macy break; 1414eda14cbcSMatt Macy 1415eda14cbcSMatt Macy case ZPROP_SRC_LOCAL: 1416eda14cbcSMatt Macy str = "local"; 1417eda14cbcSMatt Macy break; 1418eda14cbcSMatt Macy 1419eda14cbcSMatt Macy case ZPROP_SRC_TEMPORARY: 1420eda14cbcSMatt Macy str = "temporary"; 1421eda14cbcSMatt Macy break; 1422eda14cbcSMatt Macy 1423eda14cbcSMatt Macy case ZPROP_SRC_INHERITED: 1424eda14cbcSMatt Macy (void) snprintf(buf, sizeof (buf), 1425eda14cbcSMatt Macy "inherited from %s", source); 1426eda14cbcSMatt Macy str = buf; 1427eda14cbcSMatt Macy break; 1428eda14cbcSMatt Macy case ZPROP_SRC_RECEIVED: 1429eda14cbcSMatt Macy str = "received"; 1430eda14cbcSMatt Macy break; 1431eda14cbcSMatt Macy 1432eda14cbcSMatt Macy default: 1433eda14cbcSMatt Macy str = NULL; 1434eda14cbcSMatt Macy assert(!"unhandled zprop_source_t"); 1435eda14cbcSMatt Macy } 1436eda14cbcSMatt Macy break; 1437eda14cbcSMatt Macy 1438eda14cbcSMatt Macy case GET_COL_RECVD: 1439eda14cbcSMatt Macy str = (recvd_value == NULL ? "-" : recvd_value); 1440eda14cbcSMatt Macy break; 1441eda14cbcSMatt Macy 1442eda14cbcSMatt Macy default: 1443eda14cbcSMatt Macy continue; 1444eda14cbcSMatt Macy } 1445eda14cbcSMatt Macy 1446eda14cbcSMatt Macy if (i == (ZFS_GET_NCOLS - 1) || 1447eda14cbcSMatt Macy cbp->cb_columns[i + 1] == GET_COL_NONE) 1448eda14cbcSMatt Macy (void) printf("%s", str); 1449eda14cbcSMatt Macy else if (cbp->cb_scripted) 1450eda14cbcSMatt Macy (void) printf("%s\t", str); 1451eda14cbcSMatt Macy else 1452eda14cbcSMatt Macy (void) printf("%-*s ", 1453eda14cbcSMatt Macy cbp->cb_colwidths[cbp->cb_columns[i]], 1454eda14cbcSMatt Macy str); 1455eda14cbcSMatt Macy } 1456eda14cbcSMatt Macy 1457eda14cbcSMatt Macy (void) printf("\n"); 1458eda14cbcSMatt Macy } 1459eda14cbcSMatt Macy 1460eda14cbcSMatt Macy /* 1461eda14cbcSMatt Macy * Given a numeric suffix, convert the value into a number of bits that the 1462eda14cbcSMatt Macy * resulting value must be shifted. 1463eda14cbcSMatt Macy */ 1464eda14cbcSMatt Macy static int 1465eda14cbcSMatt Macy str2shift(libzfs_handle_t *hdl, const char *buf) 1466eda14cbcSMatt Macy { 1467eda14cbcSMatt Macy const char *ends = "BKMGTPEZ"; 1468eda14cbcSMatt Macy int i; 1469eda14cbcSMatt Macy 1470eda14cbcSMatt Macy if (buf[0] == '\0') 1471eda14cbcSMatt Macy return (0); 1472eda14cbcSMatt Macy for (i = 0; i < strlen(ends); i++) { 1473eda14cbcSMatt Macy if (toupper(buf[0]) == ends[i]) 1474eda14cbcSMatt Macy break; 1475eda14cbcSMatt Macy } 1476eda14cbcSMatt Macy if (i == strlen(ends)) { 1477eda14cbcSMatt Macy if (hdl) 1478eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1479eda14cbcSMatt Macy "invalid numeric suffix '%s'"), buf); 1480eda14cbcSMatt Macy return (-1); 1481eda14cbcSMatt Macy } 1482eda14cbcSMatt Macy 1483eda14cbcSMatt Macy /* 1484eda14cbcSMatt Macy * Allow 'G' = 'GB' = 'GiB', case-insensitively. 1485eda14cbcSMatt Macy * However, 'BB' and 'BiB' are disallowed. 1486eda14cbcSMatt Macy */ 1487eda14cbcSMatt Macy if (buf[1] == '\0' || 1488eda14cbcSMatt Macy (toupper(buf[0]) != 'B' && 1489eda14cbcSMatt Macy ((toupper(buf[1]) == 'B' && buf[2] == '\0') || 1490eda14cbcSMatt Macy (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' && 1491eda14cbcSMatt Macy buf[3] == '\0')))) 1492eda14cbcSMatt Macy return (10 * i); 1493eda14cbcSMatt Macy 1494eda14cbcSMatt Macy if (hdl) 1495eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1496eda14cbcSMatt Macy "invalid numeric suffix '%s'"), buf); 1497eda14cbcSMatt Macy return (-1); 1498eda14cbcSMatt Macy } 1499eda14cbcSMatt Macy 1500eda14cbcSMatt Macy /* 1501eda14cbcSMatt Macy * Convert a string of the form '100G' into a real number. Used when setting 1502eda14cbcSMatt Macy * properties or creating a volume. 'buf' is used to place an extended error 1503eda14cbcSMatt Macy * message for the caller to use. 1504eda14cbcSMatt Macy */ 1505eda14cbcSMatt Macy int 1506eda14cbcSMatt Macy zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1507eda14cbcSMatt Macy { 1508eda14cbcSMatt Macy char *end; 1509eda14cbcSMatt Macy int shift; 1510eda14cbcSMatt Macy 1511eda14cbcSMatt Macy *num = 0; 1512eda14cbcSMatt Macy 1513eda14cbcSMatt Macy /* Check to see if this looks like a number. */ 1514eda14cbcSMatt Macy if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1515eda14cbcSMatt Macy if (hdl) 1516eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1517eda14cbcSMatt Macy "bad numeric value '%s'"), value); 1518eda14cbcSMatt Macy return (-1); 1519eda14cbcSMatt Macy } 1520eda14cbcSMatt Macy 1521eda14cbcSMatt Macy /* Rely on strtoull() to process the numeric portion. */ 1522eda14cbcSMatt Macy errno = 0; 1523eda14cbcSMatt Macy *num = strtoull(value, &end, 10); 1524eda14cbcSMatt Macy 1525eda14cbcSMatt Macy /* 1526eda14cbcSMatt Macy * Check for ERANGE, which indicates that the value is too large to fit 1527eda14cbcSMatt Macy * in a 64-bit value. 1528eda14cbcSMatt Macy */ 1529eda14cbcSMatt Macy if (errno == ERANGE) { 1530eda14cbcSMatt Macy if (hdl) 1531eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1532eda14cbcSMatt Macy "numeric value is too large")); 1533eda14cbcSMatt Macy return (-1); 1534eda14cbcSMatt Macy } 1535eda14cbcSMatt Macy 1536eda14cbcSMatt Macy /* 1537eda14cbcSMatt Macy * If we have a decimal value, then do the computation with floating 1538eda14cbcSMatt Macy * point arithmetic. Otherwise, use standard arithmetic. 1539eda14cbcSMatt Macy */ 1540eda14cbcSMatt Macy if (*end == '.') { 1541eda14cbcSMatt Macy double fval = strtod(value, &end); 1542eda14cbcSMatt Macy 1543eda14cbcSMatt Macy if ((shift = str2shift(hdl, end)) == -1) 1544eda14cbcSMatt Macy return (-1); 1545eda14cbcSMatt Macy 1546eda14cbcSMatt Macy fval *= pow(2, shift); 1547eda14cbcSMatt Macy 1548eda14cbcSMatt Macy /* 1549eda14cbcSMatt Macy * UINT64_MAX is not exactly representable as a double. 1550eda14cbcSMatt Macy * The closest representation is UINT64_MAX + 1, so we 1551eda14cbcSMatt Macy * use a >= comparison instead of > for the bounds check. 1552eda14cbcSMatt Macy */ 1553eda14cbcSMatt Macy if (fval >= (double)UINT64_MAX) { 1554eda14cbcSMatt Macy if (hdl) 1555eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1556eda14cbcSMatt Macy "numeric value is too large")); 1557eda14cbcSMatt Macy return (-1); 1558eda14cbcSMatt Macy } 1559eda14cbcSMatt Macy 1560eda14cbcSMatt Macy *num = (uint64_t)fval; 1561eda14cbcSMatt Macy } else { 1562eda14cbcSMatt Macy if ((shift = str2shift(hdl, end)) == -1) 1563eda14cbcSMatt Macy return (-1); 1564eda14cbcSMatt Macy 1565eda14cbcSMatt Macy /* Check for overflow */ 1566eda14cbcSMatt Macy if (shift >= 64 || (*num << shift) >> shift != *num) { 1567eda14cbcSMatt Macy if (hdl) 1568eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1569eda14cbcSMatt Macy "numeric value is too large")); 1570eda14cbcSMatt Macy return (-1); 1571eda14cbcSMatt Macy } 1572eda14cbcSMatt Macy 1573eda14cbcSMatt Macy *num <<= shift; 1574eda14cbcSMatt Macy } 1575eda14cbcSMatt Macy 1576eda14cbcSMatt Macy return (0); 1577eda14cbcSMatt Macy } 1578eda14cbcSMatt Macy 1579eda14cbcSMatt Macy /* 1580eda14cbcSMatt Macy * Given a propname=value nvpair to set, parse any numeric properties 1581eda14cbcSMatt Macy * (index, boolean, etc) if they are specified as strings and add the 1582eda14cbcSMatt Macy * resulting nvpair to the returned nvlist. 1583eda14cbcSMatt Macy * 1584eda14cbcSMatt Macy * At the DSL layer, all properties are either 64-bit numbers or strings. 1585eda14cbcSMatt Macy * We want the user to be able to ignore this fact and specify properties 1586eda14cbcSMatt Macy * as native values (numbers, for example) or as strings (to simplify 1587eda14cbcSMatt Macy * command line utilities). This also handles converting index types 1588eda14cbcSMatt Macy * (compression, checksum, etc) from strings to their on-disk index. 1589eda14cbcSMatt Macy */ 1590eda14cbcSMatt Macy int 1591eda14cbcSMatt Macy zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1592eda14cbcSMatt Macy zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1593eda14cbcSMatt Macy const char *errbuf) 1594eda14cbcSMatt Macy { 1595eda14cbcSMatt Macy data_type_t datatype = nvpair_type(elem); 1596eda14cbcSMatt Macy zprop_type_t proptype; 1597eda14cbcSMatt Macy const char *propname; 1598eda14cbcSMatt Macy char *value; 1599eda14cbcSMatt Macy boolean_t isnone = B_FALSE; 1600eda14cbcSMatt Macy boolean_t isauto = B_FALSE; 1601eda14cbcSMatt Macy int err = 0; 1602eda14cbcSMatt Macy 1603eda14cbcSMatt Macy if (type == ZFS_TYPE_POOL) { 1604eda14cbcSMatt Macy proptype = zpool_prop_get_type(prop); 1605eda14cbcSMatt Macy propname = zpool_prop_to_name(prop); 1606681ce946SMartin Matuska } else if (type == ZFS_TYPE_VDEV) { 1607681ce946SMartin Matuska proptype = vdev_prop_get_type(prop); 1608681ce946SMartin Matuska propname = vdev_prop_to_name(prop); 1609eda14cbcSMatt Macy } else { 1610eda14cbcSMatt Macy proptype = zfs_prop_get_type(prop); 1611eda14cbcSMatt Macy propname = zfs_prop_to_name(prop); 1612eda14cbcSMatt Macy } 1613eda14cbcSMatt Macy 1614eda14cbcSMatt Macy /* 1615eda14cbcSMatt Macy * Convert any properties to the internal DSL value types. 1616eda14cbcSMatt Macy */ 1617eda14cbcSMatt Macy *svalp = NULL; 1618eda14cbcSMatt Macy *ivalp = 0; 1619eda14cbcSMatt Macy 1620eda14cbcSMatt Macy switch (proptype) { 1621eda14cbcSMatt Macy case PROP_TYPE_STRING: 1622eda14cbcSMatt Macy if (datatype != DATA_TYPE_STRING) { 1623eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1624eda14cbcSMatt Macy "'%s' must be a string"), nvpair_name(elem)); 1625eda14cbcSMatt Macy goto error; 1626eda14cbcSMatt Macy } 1627eda14cbcSMatt Macy err = nvpair_value_string(elem, svalp); 1628eda14cbcSMatt Macy if (err != 0) { 1629eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1630eda14cbcSMatt Macy "'%s' is invalid"), nvpair_name(elem)); 1631eda14cbcSMatt Macy goto error; 1632eda14cbcSMatt Macy } 1633eda14cbcSMatt Macy if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1634eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1635eda14cbcSMatt Macy "'%s' is too long"), nvpair_name(elem)); 1636eda14cbcSMatt Macy goto error; 1637eda14cbcSMatt Macy } 1638eda14cbcSMatt Macy break; 1639eda14cbcSMatt Macy 1640eda14cbcSMatt Macy case PROP_TYPE_NUMBER: 1641eda14cbcSMatt Macy if (datatype == DATA_TYPE_STRING) { 1642eda14cbcSMatt Macy (void) nvpair_value_string(elem, &value); 1643eda14cbcSMatt Macy if (strcmp(value, "none") == 0) { 1644eda14cbcSMatt Macy isnone = B_TRUE; 1645eda14cbcSMatt Macy } else if (strcmp(value, "auto") == 0) { 1646eda14cbcSMatt Macy isauto = B_TRUE; 1647eda14cbcSMatt Macy } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) { 1648eda14cbcSMatt Macy goto error; 1649eda14cbcSMatt Macy } 1650eda14cbcSMatt Macy } else if (datatype == DATA_TYPE_UINT64) { 1651eda14cbcSMatt Macy (void) nvpair_value_uint64(elem, ivalp); 1652eda14cbcSMatt Macy } else { 1653eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1654eda14cbcSMatt Macy "'%s' must be a number"), nvpair_name(elem)); 1655eda14cbcSMatt Macy goto error; 1656eda14cbcSMatt Macy } 1657eda14cbcSMatt Macy 1658eda14cbcSMatt Macy /* 1659eda14cbcSMatt Macy * Quota special: force 'none' and don't allow 0. 1660eda14cbcSMatt Macy */ 1661eda14cbcSMatt Macy if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1662eda14cbcSMatt Macy (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1663eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1664eda14cbcSMatt Macy "use 'none' to disable quota/refquota")); 1665eda14cbcSMatt Macy goto error; 1666eda14cbcSMatt Macy } 1667eda14cbcSMatt Macy 1668eda14cbcSMatt Macy /* 1669eda14cbcSMatt Macy * Special handling for "*_limit=none". In this case it's not 1670eda14cbcSMatt Macy * 0 but UINT64_MAX. 1671eda14cbcSMatt Macy */ 1672eda14cbcSMatt Macy if ((type & ZFS_TYPE_DATASET) && isnone && 1673eda14cbcSMatt Macy (prop == ZFS_PROP_FILESYSTEM_LIMIT || 1674eda14cbcSMatt Macy prop == ZFS_PROP_SNAPSHOT_LIMIT)) { 1675eda14cbcSMatt Macy *ivalp = UINT64_MAX; 1676eda14cbcSMatt Macy } 1677eda14cbcSMatt Macy 1678eda14cbcSMatt Macy /* 1679eda14cbcSMatt Macy * Special handling for setting 'refreservation' to 'auto'. Use 1680eda14cbcSMatt Macy * UINT64_MAX to tell the caller to use zfs_fix_auto_resv(). 1681eda14cbcSMatt Macy * 'auto' is only allowed on volumes. 1682eda14cbcSMatt Macy */ 1683eda14cbcSMatt Macy if (isauto) { 1684eda14cbcSMatt Macy switch (prop) { 1685eda14cbcSMatt Macy case ZFS_PROP_REFRESERVATION: 1686eda14cbcSMatt Macy if ((type & ZFS_TYPE_VOLUME) == 0) { 1687eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1688eda14cbcSMatt Macy "'%s=auto' only allowed on " 1689eda14cbcSMatt Macy "volumes"), nvpair_name(elem)); 1690eda14cbcSMatt Macy goto error; 1691eda14cbcSMatt Macy } 1692eda14cbcSMatt Macy *ivalp = UINT64_MAX; 1693eda14cbcSMatt Macy break; 1694eda14cbcSMatt Macy default: 1695eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1696eda14cbcSMatt Macy "'auto' is invalid value for '%s'"), 1697eda14cbcSMatt Macy nvpair_name(elem)); 1698eda14cbcSMatt Macy goto error; 1699eda14cbcSMatt Macy } 1700eda14cbcSMatt Macy } 1701eda14cbcSMatt Macy 1702eda14cbcSMatt Macy break; 1703eda14cbcSMatt Macy 1704eda14cbcSMatt Macy case PROP_TYPE_INDEX: 1705eda14cbcSMatt Macy if (datatype != DATA_TYPE_STRING) { 1706eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1707eda14cbcSMatt Macy "'%s' must be a string"), nvpair_name(elem)); 1708eda14cbcSMatt Macy goto error; 1709eda14cbcSMatt Macy } 1710eda14cbcSMatt Macy 1711eda14cbcSMatt Macy (void) nvpair_value_string(elem, &value); 1712eda14cbcSMatt Macy 1713eda14cbcSMatt Macy if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1714eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1715eda14cbcSMatt Macy "'%s' must be one of '%s'"), propname, 1716eda14cbcSMatt Macy zprop_values(prop, type)); 1717eda14cbcSMatt Macy goto error; 1718eda14cbcSMatt Macy } 1719eda14cbcSMatt Macy break; 1720eda14cbcSMatt Macy 1721eda14cbcSMatt Macy default: 1722eda14cbcSMatt Macy abort(); 1723eda14cbcSMatt Macy } 1724eda14cbcSMatt Macy 1725eda14cbcSMatt Macy /* 1726eda14cbcSMatt Macy * Add the result to our return set of properties. 1727eda14cbcSMatt Macy */ 1728eda14cbcSMatt Macy if (*svalp != NULL) { 1729eda14cbcSMatt Macy if (nvlist_add_string(ret, propname, *svalp) != 0) { 1730eda14cbcSMatt Macy (void) no_memory(hdl); 1731eda14cbcSMatt Macy return (-1); 1732eda14cbcSMatt Macy } 1733eda14cbcSMatt Macy } else { 1734eda14cbcSMatt Macy if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1735eda14cbcSMatt Macy (void) no_memory(hdl); 1736eda14cbcSMatt Macy return (-1); 1737eda14cbcSMatt Macy } 1738eda14cbcSMatt Macy } 1739eda14cbcSMatt Macy 1740eda14cbcSMatt Macy return (0); 1741eda14cbcSMatt Macy error: 1742eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1743eda14cbcSMatt Macy return (-1); 1744eda14cbcSMatt Macy } 1745eda14cbcSMatt Macy 1746eda14cbcSMatt Macy static int 1747da5137abSMartin Matuska addlist(libzfs_handle_t *hdl, const char *propname, zprop_list_t **listp, 1748eda14cbcSMatt Macy zfs_type_t type) 1749eda14cbcSMatt Macy { 1750da5137abSMartin Matuska int prop = zprop_name_to_prop(propname, type); 1751eda14cbcSMatt Macy if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE)) 1752eda14cbcSMatt Macy prop = ZPROP_INVAL; 1753eda14cbcSMatt Macy 1754eda14cbcSMatt Macy /* 1755681ce946SMartin Matuska * Return failure if no property table entry was found and this isn't 1756681ce946SMartin Matuska * a user-defined property. 1757eda14cbcSMatt Macy */ 1758*1f1e2261SMartin Matuska if (prop == ZPROP_USERPROP && ((type == ZFS_TYPE_POOL && 1759eda14cbcSMatt Macy !zpool_prop_feature(propname) && 1760eda14cbcSMatt Macy !zpool_prop_unsupported(propname)) || 1761681ce946SMartin Matuska ((type == ZFS_TYPE_DATASET) && !zfs_prop_user(propname) && 1762681ce946SMartin Matuska !zfs_prop_userquota(propname) && !zfs_prop_written(propname)) || 1763681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) && !vdev_prop_user(propname)))) { 1764eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1765eda14cbcSMatt Macy "invalid property '%s'"), propname); 1766eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP, 1767eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "bad property list"))); 1768eda14cbcSMatt Macy } 1769eda14cbcSMatt Macy 1770da5137abSMartin Matuska zprop_list_t *entry = zfs_alloc(hdl, sizeof (*entry)); 1771da5137abSMartin Matuska 1772eda14cbcSMatt Macy entry->pl_prop = prop; 1773*1f1e2261SMartin Matuska if (prop == ZPROP_USERPROP) { 1774c03c5b1cSMartin Matuska entry->pl_user_prop = zfs_strdup(hdl, propname); 1775eda14cbcSMatt Macy entry->pl_width = strlen(propname); 1776eda14cbcSMatt Macy } else { 1777eda14cbcSMatt Macy entry->pl_width = zprop_width(prop, &entry->pl_fixed, 1778eda14cbcSMatt Macy type); 1779eda14cbcSMatt Macy } 1780eda14cbcSMatt Macy 1781eda14cbcSMatt Macy *listp = entry; 1782eda14cbcSMatt Macy 1783eda14cbcSMatt Macy return (0); 1784eda14cbcSMatt Macy } 1785eda14cbcSMatt Macy 1786eda14cbcSMatt Macy /* 1787eda14cbcSMatt Macy * Given a comma-separated list of properties, construct a property list 1788eda14cbcSMatt Macy * containing both user-defined and native properties. This function will 1789eda14cbcSMatt Macy * return a NULL list if 'all' is specified, which can later be expanded 1790eda14cbcSMatt Macy * by zprop_expand_list(). 1791eda14cbcSMatt Macy */ 1792eda14cbcSMatt Macy int 1793eda14cbcSMatt Macy zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1794eda14cbcSMatt Macy zfs_type_t type) 1795eda14cbcSMatt Macy { 1796eda14cbcSMatt Macy *listp = NULL; 1797eda14cbcSMatt Macy 1798eda14cbcSMatt Macy /* 1799eda14cbcSMatt Macy * If 'all' is specified, return a NULL list. 1800eda14cbcSMatt Macy */ 1801eda14cbcSMatt Macy if (strcmp(props, "all") == 0) 1802eda14cbcSMatt Macy return (0); 1803eda14cbcSMatt Macy 1804eda14cbcSMatt Macy /* 1805eda14cbcSMatt Macy * If no props were specified, return an error. 1806eda14cbcSMatt Macy */ 1807eda14cbcSMatt Macy if (props[0] == '\0') { 1808eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1809eda14cbcSMatt Macy "no properties specified")); 1810eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1811eda14cbcSMatt Macy "bad property list"))); 1812eda14cbcSMatt Macy } 1813eda14cbcSMatt Macy 1814da5137abSMartin Matuska for (char *p; (p = strsep(&props, ",")); ) 1815da5137abSMartin Matuska if (strcmp(p, "space") == 0) { 1816da5137abSMartin Matuska static const char *const spaceprops[] = { 1817eda14cbcSMatt Macy "name", "avail", "used", "usedbysnapshots", 1818eda14cbcSMatt Macy "usedbydataset", "usedbyrefreservation", 1819da5137abSMartin Matuska "usedbychildren" 1820eda14cbcSMatt Macy }; 1821eda14cbcSMatt Macy 1822da5137abSMartin Matuska for (int i = 0; i < ARRAY_SIZE(spaceprops); i++) { 1823eda14cbcSMatt Macy if (addlist(hdl, spaceprops[i], listp, type)) 1824eda14cbcSMatt Macy return (-1); 1825eda14cbcSMatt Macy listp = &(*listp)->pl_next; 1826eda14cbcSMatt Macy } 1827eda14cbcSMatt Macy } else { 1828da5137abSMartin Matuska if (addlist(hdl, p, listp, type)) 1829eda14cbcSMatt Macy return (-1); 1830eda14cbcSMatt Macy listp = &(*listp)->pl_next; 1831eda14cbcSMatt Macy } 1832eda14cbcSMatt Macy 1833eda14cbcSMatt Macy return (0); 1834eda14cbcSMatt Macy } 1835eda14cbcSMatt Macy 1836eda14cbcSMatt Macy void 1837eda14cbcSMatt Macy zprop_free_list(zprop_list_t *pl) 1838eda14cbcSMatt Macy { 1839eda14cbcSMatt Macy zprop_list_t *next; 1840eda14cbcSMatt Macy 1841eda14cbcSMatt Macy while (pl != NULL) { 1842eda14cbcSMatt Macy next = pl->pl_next; 1843eda14cbcSMatt Macy free(pl->pl_user_prop); 1844eda14cbcSMatt Macy free(pl); 1845eda14cbcSMatt Macy pl = next; 1846eda14cbcSMatt Macy } 1847eda14cbcSMatt Macy } 1848eda14cbcSMatt Macy 1849eda14cbcSMatt Macy typedef struct expand_data { 1850eda14cbcSMatt Macy zprop_list_t **last; 1851eda14cbcSMatt Macy libzfs_handle_t *hdl; 1852eda14cbcSMatt Macy zfs_type_t type; 1853eda14cbcSMatt Macy } expand_data_t; 1854eda14cbcSMatt Macy 1855eda14cbcSMatt Macy static int 1856eda14cbcSMatt Macy zprop_expand_list_cb(int prop, void *cb) 1857eda14cbcSMatt Macy { 1858eda14cbcSMatt Macy zprop_list_t *entry; 1859eda14cbcSMatt Macy expand_data_t *edp = cb; 1860eda14cbcSMatt Macy 1861716fd348SMartin Matuska entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t)); 1862eda14cbcSMatt Macy 1863eda14cbcSMatt Macy entry->pl_prop = prop; 1864eda14cbcSMatt Macy entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1865eda14cbcSMatt Macy entry->pl_all = B_TRUE; 1866eda14cbcSMatt Macy 1867eda14cbcSMatt Macy *(edp->last) = entry; 1868eda14cbcSMatt Macy edp->last = &entry->pl_next; 1869eda14cbcSMatt Macy 1870eda14cbcSMatt Macy return (ZPROP_CONT); 1871eda14cbcSMatt Macy } 1872eda14cbcSMatt Macy 1873eda14cbcSMatt Macy int 1874eda14cbcSMatt Macy zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1875eda14cbcSMatt Macy { 1876eda14cbcSMatt Macy zprop_list_t *entry; 1877eda14cbcSMatt Macy zprop_list_t **last; 1878eda14cbcSMatt Macy expand_data_t exp; 1879eda14cbcSMatt Macy 1880eda14cbcSMatt Macy if (*plp == NULL) { 1881eda14cbcSMatt Macy /* 1882eda14cbcSMatt Macy * If this is the very first time we've been called for an 'all' 1883eda14cbcSMatt Macy * specification, expand the list to include all native 1884eda14cbcSMatt Macy * properties. 1885eda14cbcSMatt Macy */ 1886eda14cbcSMatt Macy last = plp; 1887eda14cbcSMatt Macy 1888eda14cbcSMatt Macy exp.last = last; 1889eda14cbcSMatt Macy exp.hdl = hdl; 1890eda14cbcSMatt Macy exp.type = type; 1891eda14cbcSMatt Macy 1892eda14cbcSMatt Macy if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1893eda14cbcSMatt Macy B_FALSE, type) == ZPROP_INVAL) 1894eda14cbcSMatt Macy return (-1); 1895eda14cbcSMatt Macy 1896eda14cbcSMatt Macy /* 1897eda14cbcSMatt Macy * Add 'name' to the beginning of the list, which is handled 1898eda14cbcSMatt Macy * specially. 1899eda14cbcSMatt Macy */ 1900c03c5b1cSMartin Matuska entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 1901681ce946SMartin Matuska entry->pl_prop = ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1902681ce946SMartin Matuska ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME)); 1903eda14cbcSMatt Macy entry->pl_width = zprop_width(entry->pl_prop, 1904eda14cbcSMatt Macy &entry->pl_fixed, type); 1905eda14cbcSMatt Macy entry->pl_all = B_TRUE; 1906eda14cbcSMatt Macy entry->pl_next = *plp; 1907eda14cbcSMatt Macy *plp = entry; 1908eda14cbcSMatt Macy } 1909eda14cbcSMatt Macy return (0); 1910eda14cbcSMatt Macy } 1911eda14cbcSMatt Macy 1912eda14cbcSMatt Macy int 1913eda14cbcSMatt Macy zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1914eda14cbcSMatt Macy zfs_type_t type) 1915eda14cbcSMatt Macy { 1916eda14cbcSMatt Macy return (zprop_iter_common(func, cb, show_all, ordered, type)); 1917eda14cbcSMatt Macy } 1918eda14cbcSMatt Macy 1919e3aa18adSMartin Matuska const char * 1920e3aa18adSMartin Matuska zfs_version_userland(void) 1921eda14cbcSMatt Macy { 1922e3aa18adSMartin Matuska return (ZFS_META_ALIAS); 1923eda14cbcSMatt Macy } 1924eda14cbcSMatt Macy 1925eda14cbcSMatt Macy /* 1926eda14cbcSMatt Macy * Prints both zfs userland and kernel versions 1927e3aa18adSMartin Matuska * Returns 0 on success, and -1 on error 1928eda14cbcSMatt Macy */ 1929eda14cbcSMatt Macy int 1930eda14cbcSMatt Macy zfs_version_print(void) 1931eda14cbcSMatt Macy { 1932e3aa18adSMartin Matuska (void) puts(ZFS_META_ALIAS); 1933eda14cbcSMatt Macy 1934e3aa18adSMartin Matuska char *kver = zfs_version_kernel(); 1935e3aa18adSMartin Matuska if (kver == NULL) { 1936eda14cbcSMatt Macy fprintf(stderr, "zfs_version_kernel() failed: %s\n", 1937eda14cbcSMatt Macy strerror(errno)); 1938eda14cbcSMatt Macy return (-1); 1939eda14cbcSMatt Macy } 1940eda14cbcSMatt Macy 1941e3aa18adSMartin Matuska (void) printf("zfs-kmod-%s\n", kver); 1942e3aa18adSMartin Matuska free(kver); 1943eda14cbcSMatt Macy return (0); 1944eda14cbcSMatt Macy } 1945eda14cbcSMatt Macy 1946eda14cbcSMatt Macy /* 1947eda14cbcSMatt Macy * Return 1 if the user requested ANSI color output, and our terminal supports 1948eda14cbcSMatt Macy * it. Return 0 for no color. 1949eda14cbcSMatt Macy */ 1950eda14cbcSMatt Macy static int 1951eda14cbcSMatt Macy use_color(void) 1952eda14cbcSMatt Macy { 1953eda14cbcSMatt Macy static int use_color = -1; 1954eda14cbcSMatt Macy char *term; 1955eda14cbcSMatt Macy 1956eda14cbcSMatt Macy /* 1957eda14cbcSMatt Macy * Optimization: 1958eda14cbcSMatt Macy * 1959eda14cbcSMatt Macy * For each zpool invocation, we do a single check to see if we should 1960eda14cbcSMatt Macy * be using color or not, and cache that value for the lifetime of the 1961eda14cbcSMatt Macy * the zpool command. That makes it cheap to call use_color() when 1962eda14cbcSMatt Macy * we're printing with color. We assume that the settings are not going 1963eda14cbcSMatt Macy * to change during the invocation of a zpool command (the user isn't 1964eda14cbcSMatt Macy * going to change the ZFS_COLOR value while zpool is running, for 1965eda14cbcSMatt Macy * example). 1966eda14cbcSMatt Macy */ 1967eda14cbcSMatt Macy if (use_color != -1) { 1968eda14cbcSMatt Macy /* 1969eda14cbcSMatt Macy * We've already figured out if we should be using color or 1970eda14cbcSMatt Macy * not. Return the cached value. 1971eda14cbcSMatt Macy */ 1972eda14cbcSMatt Macy return (use_color); 1973eda14cbcSMatt Macy } 1974eda14cbcSMatt Macy 1975eda14cbcSMatt Macy term = getenv("TERM"); 1976eda14cbcSMatt Macy /* 1977eda14cbcSMatt Macy * The user sets the ZFS_COLOR env var set to enable zpool ANSI color 1978eda14cbcSMatt Macy * output. However if NO_COLOR is set (https://no-color.org/) then 1979eda14cbcSMatt Macy * don't use it. Also, don't use color if terminal doesn't support 1980eda14cbcSMatt Macy * it. 1981eda14cbcSMatt Macy */ 1982eda14cbcSMatt Macy if (libzfs_envvar_is_set("ZFS_COLOR") && 1983eda14cbcSMatt Macy !libzfs_envvar_is_set("NO_COLOR") && 1984eda14cbcSMatt Macy isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 && 1985eda14cbcSMatt Macy strcmp("unknown", term) != 0) { 1986eda14cbcSMatt Macy /* Color supported */ 1987eda14cbcSMatt Macy use_color = 1; 1988eda14cbcSMatt Macy } else { 1989eda14cbcSMatt Macy use_color = 0; 1990eda14cbcSMatt Macy } 1991eda14cbcSMatt Macy 1992eda14cbcSMatt Macy return (use_color); 1993eda14cbcSMatt Macy } 1994eda14cbcSMatt Macy 1995eda14cbcSMatt Macy /* 1996eda14cbcSMatt Macy * color_start() and color_end() are used for when you want to colorize a block 1997eda14cbcSMatt Macy * of text. For example: 1998eda14cbcSMatt Macy * 1999eda14cbcSMatt Macy * color_start(ANSI_RED_FG) 2000eda14cbcSMatt Macy * printf("hello"); 2001eda14cbcSMatt Macy * printf("world"); 2002eda14cbcSMatt Macy * color_end(); 2003eda14cbcSMatt Macy */ 2004eda14cbcSMatt Macy void 2005eda14cbcSMatt Macy color_start(char *color) 2006eda14cbcSMatt Macy { 2007eda14cbcSMatt Macy if (use_color()) 2008eda14cbcSMatt Macy printf("%s", color); 2009eda14cbcSMatt Macy } 2010eda14cbcSMatt Macy 2011eda14cbcSMatt Macy void 2012eda14cbcSMatt Macy color_end(void) 2013eda14cbcSMatt Macy { 2014eda14cbcSMatt Macy if (use_color()) 2015eda14cbcSMatt Macy printf(ANSI_RESET); 2016eda14cbcSMatt Macy } 2017eda14cbcSMatt Macy 2018eda14cbcSMatt Macy /* printf() with a color. If color is NULL, then do a normal printf. */ 2019eda14cbcSMatt Macy int 2020eda14cbcSMatt Macy printf_color(char *color, char *format, ...) 2021eda14cbcSMatt Macy { 2022eda14cbcSMatt Macy va_list aptr; 2023eda14cbcSMatt Macy int rc; 2024eda14cbcSMatt Macy 2025eda14cbcSMatt Macy if (color) 2026eda14cbcSMatt Macy color_start(color); 2027eda14cbcSMatt Macy 2028eda14cbcSMatt Macy va_start(aptr, format); 2029eda14cbcSMatt Macy rc = vprintf(format, aptr); 2030eda14cbcSMatt Macy va_end(aptr); 2031eda14cbcSMatt Macy 2032eda14cbcSMatt Macy if (color) 2033eda14cbcSMatt Macy color_end(); 2034eda14cbcSMatt Macy 2035eda14cbcSMatt Macy return (rc); 2036eda14cbcSMatt Macy } 2037