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