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