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