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_UNKNOWN: 300 return (dgettext(TEXT_DOMAIN, "unknown error")); 301 default: 302 assert(hdl->libzfs_error == 0); 303 return (dgettext(TEXT_DOMAIN, "no error")); 304 } 305 } 306 307 void 308 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 309 { 310 va_list ap; 311 312 va_start(ap, fmt); 313 314 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 315 fmt, ap); 316 hdl->libzfs_desc_active = 1; 317 318 va_end(ap); 319 } 320 321 static void 322 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 323 { 324 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 325 fmt, ap); 326 hdl->libzfs_error = error; 327 328 if (hdl->libzfs_desc_active) 329 hdl->libzfs_desc_active = 0; 330 else 331 hdl->libzfs_desc[0] = '\0'; 332 333 if (hdl->libzfs_printerr) { 334 if (error == EZFS_UNKNOWN) { 335 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 336 "error: %s: %s\n"), hdl->libzfs_action, 337 libzfs_error_description(hdl)); 338 abort(); 339 } 340 341 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 342 libzfs_error_description(hdl)); 343 if (error == EZFS_NOMEM) 344 exit(1); 345 } 346 } 347 348 int 349 zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 350 { 351 return (zfs_error_fmt(hdl, error, "%s", msg)); 352 } 353 354 int 355 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 356 { 357 va_list ap; 358 359 va_start(ap, fmt); 360 361 zfs_verror(hdl, error, fmt, ap); 362 363 va_end(ap); 364 365 return (-1); 366 } 367 368 static int 369 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 370 va_list ap) 371 { 372 switch (error) { 373 case EPERM: 374 case EACCES: 375 zfs_verror(hdl, EZFS_PERM, fmt, ap); 376 return (-1); 377 378 case ECANCELED: 379 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 380 return (-1); 381 382 case EIO: 383 zfs_verror(hdl, EZFS_IO, fmt, ap); 384 return (-1); 385 386 case EFAULT: 387 zfs_verror(hdl, EZFS_FAULT, fmt, ap); 388 return (-1); 389 390 case EINTR: 391 zfs_verror(hdl, EZFS_INTR, fmt, ap); 392 return (-1); 393 } 394 395 return (0); 396 } 397 398 int 399 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 400 { 401 return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 402 } 403 404 int 405 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 406 { 407 va_list ap; 408 409 va_start(ap, fmt); 410 411 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 412 va_end(ap); 413 return (-1); 414 } 415 416 switch (error) { 417 case ENXIO: 418 case ENODEV: 419 case EPIPE: 420 zfs_verror(hdl, EZFS_IO, fmt, ap); 421 break; 422 423 case ENOENT: 424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 425 "dataset does not exist")); 426 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 427 break; 428 429 case ENOSPC: 430 case EDQUOT: 431 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 432 break; 433 434 case EEXIST: 435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 436 "dataset already exists")); 437 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 438 break; 439 440 case EBUSY: 441 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 442 "dataset is busy")); 443 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 444 break; 445 case EROFS: 446 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 447 break; 448 case ENAMETOOLONG: 449 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 450 break; 451 case ENOTSUP: 452 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 453 break; 454 case EAGAIN: 455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 456 "pool I/O is currently suspended")); 457 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 458 break; 459 case EREMOTEIO: 460 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 461 break; 462 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE: 463 case ZFS_ERR_IOC_CMD_UNAVAIL: 464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 465 "module does not support this operation. A reboot may " 466 "be required to enable this operation.")); 467 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 468 break; 469 case ZFS_ERR_IOC_ARG_UNAVAIL: 470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 471 "module does not support an option for this operation. " 472 "A reboot may be required to enable this option.")); 473 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 474 break; 475 case ZFS_ERR_IOC_ARG_REQUIRED: 476 case ZFS_ERR_IOC_ARG_BADTYPE: 477 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 478 break; 479 case ZFS_ERR_WRONG_PARENT: 480 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap); 481 break; 482 case ZFS_ERR_BADPROP: 483 zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 484 break; 485 default: 486 zfs_error_aux(hdl, "%s", strerror(error)); 487 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 488 break; 489 } 490 491 va_end(ap); 492 return (-1); 493 } 494 495 void 496 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 497 char *errbuf) 498 { 499 switch (err) { 500 501 case ENOSPC: 502 /* 503 * For quotas and reservations, ENOSPC indicates 504 * something different; setting a quota or reservation 505 * doesn't use any disk space. 506 */ 507 switch (prop) { 508 case ZFS_PROP_QUOTA: 509 case ZFS_PROP_REFQUOTA: 510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 511 "size is less than current used or " 512 "reserved space")); 513 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 514 break; 515 516 case ZFS_PROP_RESERVATION: 517 case ZFS_PROP_REFRESERVATION: 518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 519 "size is greater than available space")); 520 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 521 break; 522 523 default: 524 (void) zfs_standard_error(hdl, err, errbuf); 525 break; 526 } 527 break; 528 529 case EBUSY: 530 (void) zfs_standard_error(hdl, EBUSY, errbuf); 531 break; 532 533 case EROFS: 534 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 535 break; 536 537 case E2BIG: 538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 539 "property value too long")); 540 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 541 break; 542 543 case ENOTSUP: 544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 545 "pool and or dataset must be upgraded to set this " 546 "property or value")); 547 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 548 break; 549 550 case ERANGE: 551 if (prop == ZFS_PROP_COMPRESSION || 552 prop == ZFS_PROP_DNODESIZE || 553 prop == ZFS_PROP_RECORDSIZE) { 554 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 555 "property setting is not allowed on " 556 "bootable datasets")); 557 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 558 } else if (prop == ZFS_PROP_CHECKSUM || 559 prop == ZFS_PROP_DEDUP) { 560 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 561 "property setting is not allowed on " 562 "root pools")); 563 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 564 } else { 565 (void) zfs_standard_error(hdl, err, errbuf); 566 } 567 break; 568 569 case EINVAL: 570 if (prop == ZPROP_INVAL) { 571 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 572 } else { 573 (void) zfs_standard_error(hdl, err, errbuf); 574 } 575 break; 576 577 case ZFS_ERR_BADPROP: 578 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 579 break; 580 581 case EACCES: 582 if (prop == ZFS_PROP_KEYLOCATION) { 583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 584 "keylocation may only be set on encryption roots")); 585 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 586 } else { 587 (void) zfs_standard_error(hdl, err, errbuf); 588 } 589 break; 590 591 case EOVERFLOW: 592 /* 593 * This platform can't address a volume this big. 594 */ 595 #ifdef _ILP32 596 if (prop == ZFS_PROP_VOLSIZE) { 597 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 598 break; 599 } 600 #endif 601 fallthrough; 602 default: 603 (void) zfs_standard_error(hdl, err, errbuf); 604 } 605 } 606 607 int 608 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 609 { 610 return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 611 } 612 613 int 614 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 615 { 616 va_list ap; 617 618 va_start(ap, fmt); 619 620 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 621 va_end(ap); 622 return (-1); 623 } 624 625 switch (error) { 626 case ENODEV: 627 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 628 break; 629 630 case ENOENT: 631 zfs_error_aux(hdl, 632 dgettext(TEXT_DOMAIN, "no such pool or dataset")); 633 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 634 break; 635 636 case EEXIST: 637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 638 "pool already exists")); 639 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 640 break; 641 642 case EBUSY: 643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 644 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 645 break; 646 647 /* There is no pending operation to cancel */ 648 case ENOTACTIVE: 649 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap); 650 break; 651 652 case ENXIO: 653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 654 "one or more devices is currently unavailable")); 655 zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 656 break; 657 658 case ENAMETOOLONG: 659 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 660 break; 661 662 case ENOTSUP: 663 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 664 break; 665 666 case EINVAL: 667 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 668 break; 669 670 case ENOSPC: 671 case EDQUOT: 672 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 673 return (-1); 674 675 case EAGAIN: 676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 677 "pool I/O is currently suspended")); 678 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 679 break; 680 681 case EROFS: 682 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 683 break; 684 case EDOM: 685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 686 "block size out of range or does not match")); 687 zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 688 break; 689 case EREMOTEIO: 690 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 691 break; 692 case ZFS_ERR_CHECKPOINT_EXISTS: 693 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap); 694 break; 695 case ZFS_ERR_DISCARDING_CHECKPOINT: 696 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap); 697 break; 698 case ZFS_ERR_NO_CHECKPOINT: 699 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap); 700 break; 701 case ZFS_ERR_DEVRM_IN_PROGRESS: 702 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap); 703 break; 704 case ZFS_ERR_VDEV_TOO_BIG: 705 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap); 706 break; 707 case ZFS_ERR_EXPORT_IN_PROGRESS: 708 zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap); 709 break; 710 case ZFS_ERR_RESILVER_IN_PROGRESS: 711 zfs_verror(hdl, EZFS_RESILVERING, fmt, ap); 712 break; 713 case ZFS_ERR_REBUILD_IN_PROGRESS: 714 zfs_verror(hdl, EZFS_REBUILDING, fmt, ap); 715 break; 716 case ZFS_ERR_BADPROP: 717 zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 718 break; 719 case ZFS_ERR_IOC_CMD_UNAVAIL: 720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 721 "module does not support this operation. A reboot may " 722 "be required to enable this operation.")); 723 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 724 break; 725 case ZFS_ERR_IOC_ARG_UNAVAIL: 726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 727 "module does not support an option for this operation. " 728 "A reboot may be required to enable this option.")); 729 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 730 break; 731 case ZFS_ERR_IOC_ARG_REQUIRED: 732 case ZFS_ERR_IOC_ARG_BADTYPE: 733 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 734 break; 735 default: 736 zfs_error_aux(hdl, "%s", strerror(error)); 737 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 738 } 739 740 va_end(ap); 741 return (-1); 742 } 743 744 /* 745 * Display an out of memory error message and abort the current program. 746 */ 747 int 748 no_memory(libzfs_handle_t *hdl) 749 { 750 return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 751 } 752 753 /* 754 * A safe form of malloc() which will die if the allocation fails. 755 */ 756 void * 757 zfs_alloc(libzfs_handle_t *hdl, size_t size) 758 { 759 void *data; 760 761 if ((data = calloc(1, size)) == NULL) 762 (void) no_memory(hdl); 763 764 return (data); 765 } 766 767 /* 768 * A safe form of asprintf() which will die if the allocation fails. 769 */ 770 char * 771 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) 772 { 773 va_list ap; 774 char *ret; 775 int err; 776 777 va_start(ap, fmt); 778 779 err = vasprintf(&ret, fmt, ap); 780 781 va_end(ap); 782 783 if (err < 0) { 784 (void) no_memory(hdl); 785 ret = NULL; 786 } 787 788 return (ret); 789 } 790 791 /* 792 * A safe form of realloc(), which also zeroes newly allocated space. 793 */ 794 void * 795 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 796 { 797 void *ret; 798 799 if ((ret = realloc(ptr, newsize)) == NULL) { 800 (void) no_memory(hdl); 801 return (NULL); 802 } 803 804 bzero((char *)ret + oldsize, (newsize - oldsize)); 805 return (ret); 806 } 807 808 /* 809 * A safe form of strdup() which will die if the allocation fails. 810 */ 811 char * 812 zfs_strdup(libzfs_handle_t *hdl, const char *str) 813 { 814 char *ret; 815 816 if ((ret = strdup(str)) == NULL) 817 (void) no_memory(hdl); 818 819 return (ret); 820 } 821 822 void 823 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 824 { 825 hdl->libzfs_printerr = printerr; 826 } 827 828 /* 829 * Read lines from an open file descriptor and store them in an array of 830 * strings until EOF. lines[] will be allocated and populated with all the 831 * lines read. All newlines are replaced with NULL terminators for 832 * convenience. lines[] must be freed after use with libzfs_free_str_array(). 833 * 834 * Returns the number of lines read. 835 */ 836 static int 837 libzfs_read_stdout_from_fd(int fd, char **lines[]) 838 { 839 840 FILE *fp; 841 int lines_cnt = 0; 842 size_t len = 0; 843 char *line = NULL; 844 char **tmp_lines = NULL, **tmp; 845 846 fp = fdopen(fd, "r"); 847 if (fp == NULL) { 848 close(fd); 849 return (0); 850 } 851 while (getline(&line, &len, fp) != -1) { 852 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1)); 853 if (tmp == NULL) { 854 /* Return the lines we were able to process */ 855 break; 856 } 857 tmp_lines = tmp; 858 859 /* Remove newline if not EOF */ 860 if (line[strlen(line) - 1] == '\n') 861 line[strlen(line) - 1] = '\0'; 862 863 tmp_lines[lines_cnt] = strdup(line); 864 if (tmp_lines[lines_cnt] == NULL) 865 break; 866 ++lines_cnt; 867 } 868 free(line); 869 fclose(fp); 870 *lines = tmp_lines; 871 return (lines_cnt); 872 } 873 874 static int 875 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags, 876 char **lines[], int *lines_cnt) 877 { 878 pid_t pid; 879 int error, devnull_fd; 880 int link[2]; 881 882 /* 883 * Setup a pipe between our child and parent process if we're 884 * reading stdout. 885 */ 886 if (lines != NULL && pipe2(link, O_NONBLOCK | O_CLOEXEC) == -1) 887 return (-EPIPE); 888 889 pid = fork(); 890 if (pid == 0) { 891 /* Child process */ 892 devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC); 893 894 if (devnull_fd < 0) 895 _exit(-1); 896 897 if (!(flags & STDOUT_VERBOSE) && (lines == NULL)) 898 (void) dup2(devnull_fd, STDOUT_FILENO); 899 else if (lines != NULL) { 900 /* Save the output to lines[] */ 901 dup2(link[1], STDOUT_FILENO); 902 } 903 904 if (!(flags & STDERR_VERBOSE)) 905 (void) dup2(devnull_fd, STDERR_FILENO); 906 907 if (flags & NO_DEFAULT_PATH) { 908 if (env == NULL) 909 execv(path, argv); 910 else 911 execve(path, argv, env); 912 } else { 913 if (env == NULL) 914 execvp(path, argv); 915 else 916 execvpe(path, argv, env); 917 } 918 919 _exit(-1); 920 } else if (pid > 0) { 921 /* Parent process */ 922 int status; 923 924 while ((error = waitpid(pid, &status, 0)) == -1 && 925 errno == EINTR) 926 ; 927 if (error < 0 || !WIFEXITED(status)) 928 return (-1); 929 930 if (lines != NULL) { 931 close(link[1]); 932 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines); 933 } 934 return (WEXITSTATUS(status)); 935 } 936 937 return (-1); 938 } 939 940 int 941 libzfs_run_process(const char *path, char *argv[], int flags) 942 { 943 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL)); 944 } 945 946 /* 947 * Run a command and store its stdout lines in an array of strings (lines[]). 948 * lines[] is allocated and populated for you, and the number of lines is set in 949 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array(). 950 * All newlines (\n) in lines[] are terminated for convenience. 951 */ 952 int 953 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[], 954 char **lines[], int *lines_cnt) 955 { 956 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt)); 957 } 958 959 /* 960 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This 961 * means that *path needs to be the full path to the executable. 962 */ 963 int 964 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[], 965 char *env[], char **lines[], int *lines_cnt) 966 { 967 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH, 968 lines, lines_cnt)); 969 } 970 971 /* 972 * Free an array of strings. Free both the strings contained in the array and 973 * the array itself. 974 */ 975 void 976 libzfs_free_str_array(char **strs, int count) 977 { 978 while (--count >= 0) 979 free(strs[count]); 980 981 free(strs); 982 } 983 984 /* 985 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or 986 * a non-zero number. 987 * 988 * Returns 0 otherwise. 989 */ 990 int 991 libzfs_envvar_is_set(char *envvar) 992 { 993 char *env = getenv(envvar); 994 if (env && (strtoul(env, NULL, 0) > 0 || 995 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) || 996 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2))) 997 return (1); 998 999 return (0); 1000 } 1001 1002 libzfs_handle_t * 1003 libzfs_init(void) 1004 { 1005 libzfs_handle_t *hdl; 1006 int error; 1007 char *env; 1008 1009 if ((error = libzfs_load_module()) != 0) { 1010 errno = error; 1011 return (NULL); 1012 } 1013 1014 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { 1015 return (NULL); 1016 } 1017 1018 if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) { 1019 free(hdl); 1020 return (NULL); 1021 } 1022 1023 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) { 1024 free(hdl); 1025 return (NULL); 1026 } 1027 1028 if (libzfs_core_init() != 0) { 1029 (void) close(hdl->libzfs_fd); 1030 free(hdl); 1031 return (NULL); 1032 } 1033 1034 zfs_prop_init(); 1035 zpool_prop_init(); 1036 zpool_feature_init(); 1037 libzfs_mnttab_init(hdl); 1038 fletcher_4_init(); 1039 1040 if (getenv("ZFS_PROP_DEBUG") != NULL) { 1041 hdl->libzfs_prop_debug = B_TRUE; 1042 } 1043 if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) { 1044 if ((error = zfs_nicestrtonum(hdl, env, 1045 &hdl->libzfs_max_nvlist))) { 1046 errno = error; 1047 (void) close(hdl->libzfs_fd); 1048 free(hdl); 1049 return (NULL); 1050 } 1051 } else { 1052 hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4); 1053 } 1054 1055 /* 1056 * For testing, remove some settable properties and features 1057 */ 1058 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) { 1059 zprop_desc_t *proptbl; 1060 1061 proptbl = zpool_prop_get_table(); 1062 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE; 1063 1064 proptbl = zfs_prop_get_table(); 1065 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE; 1066 1067 zfeature_info_t *ftbl = spa_feature_table; 1068 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE; 1069 } 1070 1071 return (hdl); 1072 } 1073 1074 void 1075 libzfs_fini(libzfs_handle_t *hdl) 1076 { 1077 (void) close(hdl->libzfs_fd); 1078 zpool_free_handles(hdl); 1079 namespace_clear(hdl); 1080 libzfs_mnttab_fini(hdl); 1081 libzfs_core_fini(); 1082 regfree(&hdl->libzfs_urire); 1083 fletcher_4_fini(); 1084 #if LIBFETCH_DYNAMIC 1085 if (hdl->libfetch != (void *)-1 && hdl->libfetch != NULL) 1086 (void) dlclose(hdl->libfetch); 1087 free(hdl->libfetch_load_error); 1088 #endif 1089 free(hdl); 1090 } 1091 1092 libzfs_handle_t * 1093 zpool_get_handle(zpool_handle_t *zhp) 1094 { 1095 return (zhp->zpool_hdl); 1096 } 1097 1098 libzfs_handle_t * 1099 zfs_get_handle(zfs_handle_t *zhp) 1100 { 1101 return (zhp->zfs_hdl); 1102 } 1103 1104 zpool_handle_t * 1105 zfs_get_pool_handle(const zfs_handle_t *zhp) 1106 { 1107 return (zhp->zpool_hdl); 1108 } 1109 1110 /* 1111 * Given a name, determine whether or not it's a valid path 1112 * (starts with '/' or "./"). If so, walk the mnttab trying 1113 * to match the device number. If not, treat the path as an 1114 * fs/vol/snap/bkmark name. 1115 */ 1116 zfs_handle_t * 1117 zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype) 1118 { 1119 struct stat64 statbuf; 1120 struct extmnttab entry; 1121 1122 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 1123 /* 1124 * It's not a valid path, assume it's a name of type 'argtype'. 1125 */ 1126 return (zfs_open(hdl, path, argtype)); 1127 } 1128 1129 if (getextmntent(path, &entry, &statbuf) != 0) 1130 return (NULL); 1131 1132 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 1133 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 1134 path); 1135 return (NULL); 1136 } 1137 1138 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 1139 } 1140 1141 /* 1142 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 1143 * an ioctl(). 1144 */ 1145 int 1146 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 1147 { 1148 if (len == 0) 1149 len = 256 * 1024; 1150 zc->zc_nvlist_dst_size = len; 1151 zc->zc_nvlist_dst = 1152 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 1153 if (zc->zc_nvlist_dst == 0) 1154 return (-1); 1155 1156 return (0); 1157 } 1158 1159 /* 1160 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 1161 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 1162 * filled in by the kernel to indicate the actual required size. 1163 */ 1164 int 1165 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 1166 { 1167 free((void *)(uintptr_t)zc->zc_nvlist_dst); 1168 zc->zc_nvlist_dst = 1169 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 1170 if (zc->zc_nvlist_dst == 0) 1171 return (-1); 1172 1173 return (0); 1174 } 1175 1176 /* 1177 * Called to free the src and dst nvlists stored in the command structure. 1178 */ 1179 void 1180 zcmd_free_nvlists(zfs_cmd_t *zc) 1181 { 1182 free((void *)(uintptr_t)zc->zc_nvlist_conf); 1183 free((void *)(uintptr_t)zc->zc_nvlist_src); 1184 free((void *)(uintptr_t)zc->zc_nvlist_dst); 1185 zc->zc_nvlist_conf = 0; 1186 zc->zc_nvlist_src = 0; 1187 zc->zc_nvlist_dst = 0; 1188 } 1189 1190 static int 1191 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 1192 nvlist_t *nvl) 1193 { 1194 char *packed; 1195 size_t len; 1196 1197 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 1198 1199 if ((packed = zfs_alloc(hdl, len)) == NULL) 1200 return (-1); 1201 1202 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 1203 1204 *outnv = (uint64_t)(uintptr_t)packed; 1205 *outlen = len; 1206 1207 return (0); 1208 } 1209 1210 int 1211 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 1212 { 1213 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 1214 &zc->zc_nvlist_conf_size, nvl)); 1215 } 1216 1217 int 1218 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 1219 { 1220 return (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 : ZFS_PROP_NAME)); 1271 1272 /* 1273 * Go through and calculate the widths for each column. For the 1274 * 'source' column, we kludge it up by taking the worst-case scenario of 1275 * inheriting from the longest name. This is acceptable because in the 1276 * majority of cases 'SOURCE' is the last column displayed, and we don't 1277 * use the width anyway. Note that the 'VALUE' column can be oversized, 1278 * if the name of the property is much longer than any values we find. 1279 */ 1280 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 1281 /* 1282 * 'PROPERTY' column 1283 */ 1284 if (pl->pl_prop != ZPROP_INVAL) { 1285 const char *propname = (type == ZFS_TYPE_POOL) ? 1286 zpool_prop_to_name(pl->pl_prop) : 1287 zfs_prop_to_name(pl->pl_prop); 1288 1289 len = strlen(propname); 1290 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 1291 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 1292 } else { 1293 len = strlen(pl->pl_user_prop); 1294 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 1295 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 1296 } 1297 1298 /* 1299 * 'VALUE' column. The first property is always the 'name' 1300 * property that was tacked on either by /sbin/zfs's 1301 * zfs_do_get() or when calling zprop_expand_list(), so we 1302 * ignore its width. If the user specified the name property 1303 * to display, then it will be later in the list in any case. 1304 */ 1305 if (pl != cbp->cb_proplist && 1306 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 1307 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 1308 1309 /* 'RECEIVED' column. */ 1310 if (pl != cbp->cb_proplist && 1311 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) 1312 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; 1313 1314 /* 1315 * 'NAME' and 'SOURCE' columns 1316 */ 1317 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 1318 ZFS_PROP_NAME) && 1319 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 1320 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 1321 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 1322 strlen(dgettext(TEXT_DOMAIN, "inherited from")); 1323 } 1324 } 1325 1326 /* 1327 * Now go through and print the headers. 1328 */ 1329 for (i = 0; i < ZFS_GET_NCOLS; i++) { 1330 switch (cbp->cb_columns[i]) { 1331 case GET_COL_NAME: 1332 title = dgettext(TEXT_DOMAIN, "NAME"); 1333 break; 1334 case GET_COL_PROPERTY: 1335 title = dgettext(TEXT_DOMAIN, "PROPERTY"); 1336 break; 1337 case GET_COL_VALUE: 1338 title = dgettext(TEXT_DOMAIN, "VALUE"); 1339 break; 1340 case GET_COL_RECVD: 1341 title = dgettext(TEXT_DOMAIN, "RECEIVED"); 1342 break; 1343 case GET_COL_SOURCE: 1344 title = dgettext(TEXT_DOMAIN, "SOURCE"); 1345 break; 1346 default: 1347 title = NULL; 1348 } 1349 1350 if (title != NULL) { 1351 if (i == (ZFS_GET_NCOLS - 1) || 1352 cbp->cb_columns[i + 1] == GET_COL_NONE) 1353 (void) printf("%s", title); 1354 else 1355 (void) printf("%-*s ", 1356 cbp->cb_colwidths[cbp->cb_columns[i]], 1357 title); 1358 } 1359 } 1360 (void) printf("\n"); 1361 } 1362 1363 /* 1364 * Display a single line of output, according to the settings in the callback 1365 * structure. 1366 */ 1367 void 1368 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 1369 const char *propname, const char *value, zprop_source_t sourcetype, 1370 const char *source, const char *recvd_value) 1371 { 1372 int i; 1373 const char *str = NULL; 1374 char buf[128]; 1375 1376 /* 1377 * Ignore those source types that the user has chosen to ignore. 1378 */ 1379 if ((sourcetype & cbp->cb_sources) == 0) 1380 return; 1381 1382 if (cbp->cb_first) 1383 zprop_print_headers(cbp, cbp->cb_type); 1384 1385 for (i = 0; i < ZFS_GET_NCOLS; i++) { 1386 switch (cbp->cb_columns[i]) { 1387 case GET_COL_NAME: 1388 str = name; 1389 break; 1390 1391 case GET_COL_PROPERTY: 1392 str = propname; 1393 break; 1394 1395 case GET_COL_VALUE: 1396 str = value; 1397 break; 1398 1399 case GET_COL_SOURCE: 1400 switch (sourcetype) { 1401 case ZPROP_SRC_NONE: 1402 str = "-"; 1403 break; 1404 1405 case ZPROP_SRC_DEFAULT: 1406 str = "default"; 1407 break; 1408 1409 case ZPROP_SRC_LOCAL: 1410 str = "local"; 1411 break; 1412 1413 case ZPROP_SRC_TEMPORARY: 1414 str = "temporary"; 1415 break; 1416 1417 case ZPROP_SRC_INHERITED: 1418 (void) snprintf(buf, sizeof (buf), 1419 "inherited from %s", source); 1420 str = buf; 1421 break; 1422 case ZPROP_SRC_RECEIVED: 1423 str = "received"; 1424 break; 1425 1426 default: 1427 str = NULL; 1428 assert(!"unhandled zprop_source_t"); 1429 } 1430 break; 1431 1432 case GET_COL_RECVD: 1433 str = (recvd_value == NULL ? "-" : recvd_value); 1434 break; 1435 1436 default: 1437 continue; 1438 } 1439 1440 if (i == (ZFS_GET_NCOLS - 1) || 1441 cbp->cb_columns[i + 1] == GET_COL_NONE) 1442 (void) printf("%s", str); 1443 else if (cbp->cb_scripted) 1444 (void) printf("%s\t", str); 1445 else 1446 (void) printf("%-*s ", 1447 cbp->cb_colwidths[cbp->cb_columns[i]], 1448 str); 1449 } 1450 1451 (void) printf("\n"); 1452 } 1453 1454 /* 1455 * Given a numeric suffix, convert the value into a number of bits that the 1456 * resulting value must be shifted. 1457 */ 1458 static int 1459 str2shift(libzfs_handle_t *hdl, const char *buf) 1460 { 1461 const char *ends = "BKMGTPEZ"; 1462 int i; 1463 1464 if (buf[0] == '\0') 1465 return (0); 1466 for (i = 0; i < strlen(ends); i++) { 1467 if (toupper(buf[0]) == ends[i]) 1468 break; 1469 } 1470 if (i == strlen(ends)) { 1471 if (hdl) 1472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1473 "invalid numeric suffix '%s'"), buf); 1474 return (-1); 1475 } 1476 1477 /* 1478 * Allow 'G' = 'GB' = 'GiB', case-insensitively. 1479 * However, 'BB' and 'BiB' are disallowed. 1480 */ 1481 if (buf[1] == '\0' || 1482 (toupper(buf[0]) != 'B' && 1483 ((toupper(buf[1]) == 'B' && buf[2] == '\0') || 1484 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' && 1485 buf[3] == '\0')))) 1486 return (10 * i); 1487 1488 if (hdl) 1489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1490 "invalid numeric suffix '%s'"), buf); 1491 return (-1); 1492 } 1493 1494 /* 1495 * Convert a string of the form '100G' into a real number. Used when setting 1496 * properties or creating a volume. 'buf' is used to place an extended error 1497 * message for the caller to use. 1498 */ 1499 int 1500 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1501 { 1502 char *end; 1503 int shift; 1504 1505 *num = 0; 1506 1507 /* Check to see if this looks like a number. */ 1508 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1509 if (hdl) 1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1511 "bad numeric value '%s'"), value); 1512 return (-1); 1513 } 1514 1515 /* Rely on strtoull() to process the numeric portion. */ 1516 errno = 0; 1517 *num = strtoull(value, &end, 10); 1518 1519 /* 1520 * Check for ERANGE, which indicates that the value is too large to fit 1521 * in a 64-bit value. 1522 */ 1523 if (errno == ERANGE) { 1524 if (hdl) 1525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1526 "numeric value is too large")); 1527 return (-1); 1528 } 1529 1530 /* 1531 * If we have a decimal value, then do the computation with floating 1532 * point arithmetic. Otherwise, use standard arithmetic. 1533 */ 1534 if (*end == '.') { 1535 double fval = strtod(value, &end); 1536 1537 if ((shift = str2shift(hdl, end)) == -1) 1538 return (-1); 1539 1540 fval *= pow(2, shift); 1541 1542 /* 1543 * UINT64_MAX is not exactly representable as a double. 1544 * The closest representation is UINT64_MAX + 1, so we 1545 * use a >= comparison instead of > for the bounds check. 1546 */ 1547 if (fval >= (double)UINT64_MAX) { 1548 if (hdl) 1549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1550 "numeric value is too large")); 1551 return (-1); 1552 } 1553 1554 *num = (uint64_t)fval; 1555 } else { 1556 if ((shift = str2shift(hdl, end)) == -1) 1557 return (-1); 1558 1559 /* Check for overflow */ 1560 if (shift >= 64 || (*num << shift) >> shift != *num) { 1561 if (hdl) 1562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1563 "numeric value is too large")); 1564 return (-1); 1565 } 1566 1567 *num <<= shift; 1568 } 1569 1570 return (0); 1571 } 1572 1573 /* 1574 * Given a propname=value nvpair to set, parse any numeric properties 1575 * (index, boolean, etc) if they are specified as strings and add the 1576 * resulting nvpair to the returned nvlist. 1577 * 1578 * At the DSL layer, all properties are either 64-bit numbers or strings. 1579 * We want the user to be able to ignore this fact and specify properties 1580 * as native values (numbers, for example) or as strings (to simplify 1581 * command line utilities). This also handles converting index types 1582 * (compression, checksum, etc) from strings to their on-disk index. 1583 */ 1584 int 1585 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1586 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1587 const char *errbuf) 1588 { 1589 data_type_t datatype = nvpair_type(elem); 1590 zprop_type_t proptype; 1591 const char *propname; 1592 char *value; 1593 boolean_t isnone = B_FALSE; 1594 boolean_t isauto = B_FALSE; 1595 int err = 0; 1596 1597 if (type == ZFS_TYPE_POOL) { 1598 proptype = zpool_prop_get_type(prop); 1599 propname = zpool_prop_to_name(prop); 1600 } else { 1601 proptype = zfs_prop_get_type(prop); 1602 propname = zfs_prop_to_name(prop); 1603 } 1604 1605 /* 1606 * Convert any properties to the internal DSL value types. 1607 */ 1608 *svalp = NULL; 1609 *ivalp = 0; 1610 1611 switch (proptype) { 1612 case PROP_TYPE_STRING: 1613 if (datatype != DATA_TYPE_STRING) { 1614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1615 "'%s' must be a string"), nvpair_name(elem)); 1616 goto error; 1617 } 1618 err = nvpair_value_string(elem, svalp); 1619 if (err != 0) { 1620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1621 "'%s' is invalid"), nvpair_name(elem)); 1622 goto error; 1623 } 1624 if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1626 "'%s' is too long"), nvpair_name(elem)); 1627 goto error; 1628 } 1629 break; 1630 1631 case PROP_TYPE_NUMBER: 1632 if (datatype == DATA_TYPE_STRING) { 1633 (void) nvpair_value_string(elem, &value); 1634 if (strcmp(value, "none") == 0) { 1635 isnone = B_TRUE; 1636 } else if (strcmp(value, "auto") == 0) { 1637 isauto = B_TRUE; 1638 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) { 1639 goto error; 1640 } 1641 } else if (datatype == DATA_TYPE_UINT64) { 1642 (void) nvpair_value_uint64(elem, ivalp); 1643 } else { 1644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1645 "'%s' must be a number"), nvpair_name(elem)); 1646 goto error; 1647 } 1648 1649 /* 1650 * Quota special: force 'none' and don't allow 0. 1651 */ 1652 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1653 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1655 "use 'none' to disable quota/refquota")); 1656 goto error; 1657 } 1658 1659 /* 1660 * Special handling for "*_limit=none". In this case it's not 1661 * 0 but UINT64_MAX. 1662 */ 1663 if ((type & ZFS_TYPE_DATASET) && isnone && 1664 (prop == ZFS_PROP_FILESYSTEM_LIMIT || 1665 prop == ZFS_PROP_SNAPSHOT_LIMIT)) { 1666 *ivalp = UINT64_MAX; 1667 } 1668 1669 /* 1670 * Special handling for setting 'refreservation' to 'auto'. Use 1671 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv(). 1672 * 'auto' is only allowed on volumes. 1673 */ 1674 if (isauto) { 1675 switch (prop) { 1676 case ZFS_PROP_REFRESERVATION: 1677 if ((type & ZFS_TYPE_VOLUME) == 0) { 1678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1679 "'%s=auto' only allowed on " 1680 "volumes"), nvpair_name(elem)); 1681 goto error; 1682 } 1683 *ivalp = UINT64_MAX; 1684 break; 1685 default: 1686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1687 "'auto' is invalid value for '%s'"), 1688 nvpair_name(elem)); 1689 goto error; 1690 } 1691 } 1692 1693 break; 1694 1695 case PROP_TYPE_INDEX: 1696 if (datatype != DATA_TYPE_STRING) { 1697 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1698 "'%s' must be a string"), nvpair_name(elem)); 1699 goto error; 1700 } 1701 1702 (void) nvpair_value_string(elem, &value); 1703 1704 if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1705 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1706 "'%s' must be one of '%s'"), propname, 1707 zprop_values(prop, type)); 1708 goto error; 1709 } 1710 break; 1711 1712 default: 1713 abort(); 1714 } 1715 1716 /* 1717 * Add the result to our return set of properties. 1718 */ 1719 if (*svalp != NULL) { 1720 if (nvlist_add_string(ret, propname, *svalp) != 0) { 1721 (void) no_memory(hdl); 1722 return (-1); 1723 } 1724 } else { 1725 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1726 (void) no_memory(hdl); 1727 return (-1); 1728 } 1729 } 1730 1731 return (0); 1732 error: 1733 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1734 return (-1); 1735 } 1736 1737 static int 1738 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 1739 zfs_type_t type) 1740 { 1741 int prop; 1742 zprop_list_t *entry; 1743 1744 prop = zprop_name_to_prop(propname, type); 1745 1746 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE)) 1747 prop = ZPROP_INVAL; 1748 1749 /* 1750 * When no property table entry can be found, return failure if 1751 * this is a pool property or if this isn't a user-defined 1752 * dataset property, 1753 */ 1754 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL && 1755 !zpool_prop_feature(propname) && 1756 !zpool_prop_unsupported(propname)) || 1757 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) && 1758 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) { 1759 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1760 "invalid property '%s'"), propname); 1761 return (zfs_error(hdl, EZFS_BADPROP, 1762 dgettext(TEXT_DOMAIN, "bad property list"))); 1763 } 1764 1765 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1766 return (-1); 1767 1768 entry->pl_prop = prop; 1769 if (prop == ZPROP_INVAL) { 1770 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == 1771 NULL) { 1772 free(entry); 1773 return (-1); 1774 } 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 /* 1815 * It would be nice to use getsubopt() here, but the inclusion of column 1816 * aliases makes this more effort than it's worth. 1817 */ 1818 while (*props != '\0') { 1819 size_t len; 1820 char *p; 1821 char c; 1822 1823 if ((p = strchr(props, ',')) == NULL) { 1824 len = strlen(props); 1825 p = props + len; 1826 } else { 1827 len = p - props; 1828 } 1829 1830 /* 1831 * Check for empty options. 1832 */ 1833 if (len == 0) { 1834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1835 "empty property name")); 1836 return (zfs_error(hdl, EZFS_BADPROP, 1837 dgettext(TEXT_DOMAIN, "bad property list"))); 1838 } 1839 1840 /* 1841 * Check all regular property names. 1842 */ 1843 c = props[len]; 1844 props[len] = '\0'; 1845 1846 if (strcmp(props, "space") == 0) { 1847 static char *spaceprops[] = { 1848 "name", "avail", "used", "usedbysnapshots", 1849 "usedbydataset", "usedbyrefreservation", 1850 "usedbychildren", NULL 1851 }; 1852 int i; 1853 1854 for (i = 0; spaceprops[i]; i++) { 1855 if (addlist(hdl, spaceprops[i], listp, type)) 1856 return (-1); 1857 listp = &(*listp)->pl_next; 1858 } 1859 } else { 1860 if (addlist(hdl, props, listp, type)) 1861 return (-1); 1862 listp = &(*listp)->pl_next; 1863 } 1864 1865 props = p; 1866 if (c == ',') 1867 props++; 1868 } 1869 1870 return (0); 1871 } 1872 1873 void 1874 zprop_free_list(zprop_list_t *pl) 1875 { 1876 zprop_list_t *next; 1877 1878 while (pl != NULL) { 1879 next = pl->pl_next; 1880 free(pl->pl_user_prop); 1881 free(pl); 1882 pl = next; 1883 } 1884 } 1885 1886 typedef struct expand_data { 1887 zprop_list_t **last; 1888 libzfs_handle_t *hdl; 1889 zfs_type_t type; 1890 } expand_data_t; 1891 1892 static int 1893 zprop_expand_list_cb(int prop, void *cb) 1894 { 1895 zprop_list_t *entry; 1896 expand_data_t *edp = cb; 1897 1898 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1899 return (ZPROP_INVAL); 1900 1901 entry->pl_prop = prop; 1902 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1903 entry->pl_all = B_TRUE; 1904 1905 *(edp->last) = entry; 1906 edp->last = &entry->pl_next; 1907 1908 return (ZPROP_CONT); 1909 } 1910 1911 int 1912 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1913 { 1914 zprop_list_t *entry; 1915 zprop_list_t **last; 1916 expand_data_t exp; 1917 1918 if (*plp == NULL) { 1919 /* 1920 * If this is the very first time we've been called for an 'all' 1921 * specification, expand the list to include all native 1922 * properties. 1923 */ 1924 last = plp; 1925 1926 exp.last = last; 1927 exp.hdl = hdl; 1928 exp.type = type; 1929 1930 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1931 B_FALSE, type) == ZPROP_INVAL) 1932 return (-1); 1933 1934 /* 1935 * Add 'name' to the beginning of the list, which is handled 1936 * specially. 1937 */ 1938 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1939 return (-1); 1940 1941 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1942 ZFS_PROP_NAME; 1943 entry->pl_width = zprop_width(entry->pl_prop, 1944 &entry->pl_fixed, type); 1945 entry->pl_all = B_TRUE; 1946 entry->pl_next = *plp; 1947 *plp = entry; 1948 } 1949 return (0); 1950 } 1951 1952 int 1953 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1954 zfs_type_t type) 1955 { 1956 return (zprop_iter_common(func, cb, show_all, ordered, type)); 1957 } 1958 1959 /* 1960 * Fill given version buffer with zfs userland version 1961 */ 1962 void 1963 zfs_version_userland(char *version, int len) 1964 { 1965 (void) strlcpy(version, ZFS_META_ALIAS, len); 1966 } 1967 1968 /* 1969 * Prints both zfs userland and kernel versions 1970 * Returns 0 on success, and -1 on error (with errno set) 1971 */ 1972 int 1973 zfs_version_print(void) 1974 { 1975 char zver_userland[128]; 1976 char zver_kernel[128]; 1977 1978 zfs_version_userland(zver_userland, sizeof (zver_userland)); 1979 1980 (void) printf("%s\n", zver_userland); 1981 1982 if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) { 1983 fprintf(stderr, "zfs_version_kernel() failed: %s\n", 1984 strerror(errno)); 1985 return (-1); 1986 } 1987 1988 (void) printf("zfs-kmod-%s\n", zver_kernel); 1989 1990 return (0); 1991 } 1992 1993 /* 1994 * Return 1 if the user requested ANSI color output, and our terminal supports 1995 * it. Return 0 for no color. 1996 */ 1997 static int 1998 use_color(void) 1999 { 2000 static int use_color = -1; 2001 char *term; 2002 2003 /* 2004 * Optimization: 2005 * 2006 * For each zpool invocation, we do a single check to see if we should 2007 * be using color or not, and cache that value for the lifetime of the 2008 * the zpool command. That makes it cheap to call use_color() when 2009 * we're printing with color. We assume that the settings are not going 2010 * to change during the invocation of a zpool command (the user isn't 2011 * going to change the ZFS_COLOR value while zpool is running, for 2012 * example). 2013 */ 2014 if (use_color != -1) { 2015 /* 2016 * We've already figured out if we should be using color or 2017 * not. Return the cached value. 2018 */ 2019 return (use_color); 2020 } 2021 2022 term = getenv("TERM"); 2023 /* 2024 * The user sets the ZFS_COLOR env var set to enable zpool ANSI color 2025 * output. However if NO_COLOR is set (https://no-color.org/) then 2026 * don't use it. Also, don't use color if terminal doesn't support 2027 * it. 2028 */ 2029 if (libzfs_envvar_is_set("ZFS_COLOR") && 2030 !libzfs_envvar_is_set("NO_COLOR") && 2031 isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 && 2032 strcmp("unknown", term) != 0) { 2033 /* Color supported */ 2034 use_color = 1; 2035 } else { 2036 use_color = 0; 2037 } 2038 2039 return (use_color); 2040 } 2041 2042 /* 2043 * color_start() and color_end() are used for when you want to colorize a block 2044 * of text. For example: 2045 * 2046 * color_start(ANSI_RED_FG) 2047 * printf("hello"); 2048 * printf("world"); 2049 * color_end(); 2050 */ 2051 void 2052 color_start(char *color) 2053 { 2054 if (use_color()) 2055 printf("%s", color); 2056 } 2057 2058 void 2059 color_end(void) 2060 { 2061 if (use_color()) 2062 printf(ANSI_RESET); 2063 } 2064 2065 /* printf() with a color. If color is NULL, then do a normal printf. */ 2066 int 2067 printf_color(char *color, char *format, ...) 2068 { 2069 va_list aptr; 2070 int rc; 2071 2072 if (color) 2073 color_start(color); 2074 2075 va_start(aptr, format); 2076 rc = vprintf(format, aptr); 2077 va_end(aptr); 2078 2079 if (color) 2080 color_end(); 2081 2082 return (rc); 2083 } 2084