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 2019 Joyent, Inc. 25 * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 27 * Copyright (c) 2017 Datto Inc. 28 */ 29 30 /* 31 * Internal utility routines for the ZFS library. 32 */ 33 34 #include <errno.h> 35 #include <fcntl.h> 36 #include <libintl.h> 37 #include <stdarg.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <strings.h> 41 #include <unistd.h> 42 #include <ctype.h> 43 #include <math.h> 44 #include <sys/filio.h> 45 #include <sys/mnttab.h> 46 #include <sys/mntent.h> 47 #include <sys/types.h> 48 #include <libcmdutils.h> 49 50 #include <libzfs.h> 51 #include <libzfs_core.h> 52 53 #include "libzfs_impl.h" 54 #include "zfs_prop.h" 55 #include "zfs_comutil.h" 56 #include "zfeature_common.h" 57 58 int 59 libzfs_errno(libzfs_handle_t *hdl) 60 { 61 return (hdl->libzfs_error); 62 } 63 64 const char * 65 libzfs_error_action(libzfs_handle_t *hdl) 66 { 67 return (hdl->libzfs_action); 68 } 69 70 const char * 71 libzfs_error_description(libzfs_handle_t *hdl) 72 { 73 if (hdl->libzfs_desc[0] != '\0') 74 return (hdl->libzfs_desc); 75 76 switch (hdl->libzfs_error) { 77 case EZFS_NOMEM: 78 return (dgettext(TEXT_DOMAIN, "out of memory")); 79 case EZFS_BADPROP: 80 return (dgettext(TEXT_DOMAIN, "invalid property value")); 81 case EZFS_PROPREADONLY: 82 return (dgettext(TEXT_DOMAIN, "read-only property")); 83 case EZFS_PROPTYPE: 84 return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 85 "datasets of this type")); 86 case EZFS_PROPNONINHERIT: 87 return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 88 case EZFS_PROPSPACE: 89 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 90 case EZFS_BADTYPE: 91 return (dgettext(TEXT_DOMAIN, "operation not applicable to " 92 "datasets of this type")); 93 case EZFS_BUSY: 94 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 95 case EZFS_EXISTS: 96 return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 97 case EZFS_NOENT: 98 return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 99 case EZFS_BADSTREAM: 100 return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 101 case EZFS_DSREADONLY: 102 return (dgettext(TEXT_DOMAIN, "dataset is read-only")); 103 case EZFS_VOLTOOBIG: 104 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 105 "this system")); 106 case EZFS_INVALIDNAME: 107 return (dgettext(TEXT_DOMAIN, "invalid name")); 108 case EZFS_BADRESTORE: 109 return (dgettext(TEXT_DOMAIN, "unable to restore to " 110 "destination")); 111 case EZFS_BADBACKUP: 112 return (dgettext(TEXT_DOMAIN, "backup failed")); 113 case EZFS_BADTARGET: 114 return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 115 case EZFS_NODEVICE: 116 return (dgettext(TEXT_DOMAIN, "no such device in pool")); 117 case EZFS_BADDEV: 118 return (dgettext(TEXT_DOMAIN, "invalid device")); 119 case EZFS_NOREPLICAS: 120 return (dgettext(TEXT_DOMAIN, "no valid replicas")); 121 case EZFS_RESILVERING: 122 return (dgettext(TEXT_DOMAIN, "currently resilvering")); 123 case EZFS_BADVERSION: 124 return (dgettext(TEXT_DOMAIN, "unsupported version or " 125 "feature")); 126 case EZFS_POOLUNAVAIL: 127 return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 128 case EZFS_DEVOVERFLOW: 129 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 130 case EZFS_BADPATH: 131 return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 132 case EZFS_CROSSTARGET: 133 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 134 "pools")); 135 case EZFS_ZONED: 136 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 137 case EZFS_MOUNTFAILED: 138 return (dgettext(TEXT_DOMAIN, "mount failed")); 139 case EZFS_UMOUNTFAILED: 140 return (dgettext(TEXT_DOMAIN, "umount failed")); 141 case EZFS_UNSHARENFSFAILED: 142 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 143 case EZFS_SHARENFSFAILED: 144 return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 145 case EZFS_UNSHARESMBFAILED: 146 return (dgettext(TEXT_DOMAIN, "smb remove share failed")); 147 case EZFS_SHARESMBFAILED: 148 return (dgettext(TEXT_DOMAIN, "smb add share failed")); 149 case EZFS_PERM: 150 return (dgettext(TEXT_DOMAIN, "permission denied")); 151 case EZFS_NOSPC: 152 return (dgettext(TEXT_DOMAIN, "out of space")); 153 case EZFS_FAULT: 154 return (dgettext(TEXT_DOMAIN, "bad address")); 155 case EZFS_IO: 156 return (dgettext(TEXT_DOMAIN, "I/O error")); 157 case EZFS_INTR: 158 return (dgettext(TEXT_DOMAIN, "signal received")); 159 case EZFS_ISSPARE: 160 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 161 "spare")); 162 case EZFS_INVALCONFIG: 163 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 164 case EZFS_RECURSIVE: 165 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 166 case EZFS_NOHISTORY: 167 return (dgettext(TEXT_DOMAIN, "no history available")); 168 case EZFS_POOLPROPS: 169 return (dgettext(TEXT_DOMAIN, "failed to retrieve " 170 "pool properties")); 171 case EZFS_POOL_NOTSUP: 172 return (dgettext(TEXT_DOMAIN, "operation not supported " 173 "on this type of pool")); 174 case EZFS_POOL_INVALARG: 175 return (dgettext(TEXT_DOMAIN, "invalid argument for " 176 "this pool operation")); 177 case EZFS_NAMETOOLONG: 178 return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 179 case EZFS_OPENFAILED: 180 return (dgettext(TEXT_DOMAIN, "open failed")); 181 case EZFS_NOCAP: 182 return (dgettext(TEXT_DOMAIN, 183 "disk capacity information could not be retrieved")); 184 case EZFS_LABELFAILED: 185 return (dgettext(TEXT_DOMAIN, "write of label failed")); 186 case EZFS_BADWHO: 187 return (dgettext(TEXT_DOMAIN, "invalid user/group")); 188 case EZFS_BADPERM: 189 return (dgettext(TEXT_DOMAIN, "invalid permission")); 190 case EZFS_BADPERMSET: 191 return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 192 case EZFS_NODELEGATION: 193 return (dgettext(TEXT_DOMAIN, "delegated administration is " 194 "disabled on pool")); 195 case EZFS_BADCACHE: 196 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 197 case EZFS_ISL2CACHE: 198 return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 199 case EZFS_VDEVNOTSUP: 200 return (dgettext(TEXT_DOMAIN, "vdev specification is not " 201 "supported")); 202 case EZFS_NOTSUP: 203 return (dgettext(TEXT_DOMAIN, "operation not supported " 204 "on this dataset")); 205 case EZFS_ACTIVE_SPARE: 206 return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 207 "device")); 208 case EZFS_UNPLAYED_LOGS: 209 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 210 "logs")); 211 case EZFS_REFTAG_RELE: 212 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 213 case EZFS_REFTAG_HOLD: 214 return (dgettext(TEXT_DOMAIN, "tag already exists on this " 215 "dataset")); 216 case EZFS_TAGTOOLONG: 217 return (dgettext(TEXT_DOMAIN, "tag too long")); 218 case EZFS_PIPEFAILED: 219 return (dgettext(TEXT_DOMAIN, "pipe create failed")); 220 case EZFS_THREADCREATEFAILED: 221 return (dgettext(TEXT_DOMAIN, "thread create failed")); 222 case EZFS_POSTSPLIT_ONLINE: 223 return (dgettext(TEXT_DOMAIN, "disk was split from this pool " 224 "into a new one")); 225 case EZFS_SCRUB_PAUSED: 226 return (dgettext(TEXT_DOMAIN, "scrub is paused; " 227 "use 'zpool scrub' to resume")); 228 case EZFS_SCRUBBING: 229 return (dgettext(TEXT_DOMAIN, "currently scrubbing; " 230 "use 'zpool scrub -s' to cancel current scrub")); 231 case EZFS_NO_SCRUB: 232 return (dgettext(TEXT_DOMAIN, "there is no active scrub")); 233 case EZFS_DIFF: 234 return (dgettext(TEXT_DOMAIN, "unable to generate diffs")); 235 case EZFS_DIFFDATA: 236 return (dgettext(TEXT_DOMAIN, "invalid diff data")); 237 case EZFS_POOLREADONLY: 238 return (dgettext(TEXT_DOMAIN, "pool is read-only")); 239 case EZFS_NO_PENDING: 240 return (dgettext(TEXT_DOMAIN, "operation is not " 241 "in progress")); 242 case EZFS_CHECKPOINT_EXISTS: 243 return (dgettext(TEXT_DOMAIN, "checkpoint exists")); 244 case EZFS_DISCARDING_CHECKPOINT: 245 return (dgettext(TEXT_DOMAIN, "currently discarding " 246 "checkpoint")); 247 case EZFS_NO_CHECKPOINT: 248 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist")); 249 case EZFS_DEVRM_IN_PROGRESS: 250 return (dgettext(TEXT_DOMAIN, "device removal in progress")); 251 case EZFS_VDEV_TOO_BIG: 252 return (dgettext(TEXT_DOMAIN, "device exceeds supported size")); 253 case EZFS_ACTIVE_POOL: 254 return (dgettext(TEXT_DOMAIN, "pool is imported on a " 255 "different host")); 256 case EZFS_TOOMANY: 257 return (dgettext(TEXT_DOMAIN, "argument list too long")); 258 case EZFS_INITIALIZING: 259 return (dgettext(TEXT_DOMAIN, "currently initializing")); 260 case EZFS_NO_INITIALIZE: 261 return (dgettext(TEXT_DOMAIN, "there is no active " 262 "initialization")); 263 case EZFS_NO_RESILVER_DEFER: 264 return (dgettext(TEXT_DOMAIN, "this action requires the " 265 "resilver_defer feature")); 266 case EZFS_CRYPTOFAILED: 267 return (dgettext(TEXT_DOMAIN, "encryption failure")); 268 case EZFS_UNKNOWN: 269 return (dgettext(TEXT_DOMAIN, "unknown error")); 270 default: 271 assert(hdl->libzfs_error == 0); 272 return (dgettext(TEXT_DOMAIN, "no error")); 273 } 274 } 275 276 /*PRINTFLIKE2*/ 277 void 278 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 279 { 280 va_list ap; 281 282 va_start(ap, fmt); 283 284 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 285 fmt, ap); 286 hdl->libzfs_desc_active = 1; 287 288 va_end(ap); 289 } 290 291 static void 292 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 293 { 294 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 295 fmt, ap); 296 hdl->libzfs_error = error; 297 298 if (hdl->libzfs_desc_active) 299 hdl->libzfs_desc_active = 0; 300 else 301 hdl->libzfs_desc[0] = '\0'; 302 303 if (hdl->libzfs_printerr) { 304 if (error == EZFS_UNKNOWN) { 305 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 306 "error: %s\n"), libzfs_error_description(hdl)); 307 abort(); 308 } 309 310 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 311 libzfs_error_description(hdl)); 312 if (error == EZFS_NOMEM) 313 exit(1); 314 } 315 } 316 317 int 318 zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 319 { 320 return (zfs_error_fmt(hdl, error, "%s", msg)); 321 } 322 323 /*PRINTFLIKE3*/ 324 int 325 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 326 { 327 va_list ap; 328 329 va_start(ap, fmt); 330 331 zfs_verror(hdl, error, fmt, ap); 332 333 va_end(ap); 334 335 return (-1); 336 } 337 338 static int 339 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 340 va_list ap) 341 { 342 switch (error) { 343 case EPERM: 344 case EACCES: 345 zfs_verror(hdl, EZFS_PERM, fmt, ap); 346 return (-1); 347 348 case ECANCELED: 349 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 350 return (-1); 351 352 case EIO: 353 zfs_verror(hdl, EZFS_IO, fmt, ap); 354 return (-1); 355 356 case EFAULT: 357 zfs_verror(hdl, EZFS_FAULT, fmt, ap); 358 return (-1); 359 360 case EINTR: 361 zfs_verror(hdl, EZFS_INTR, fmt, ap); 362 return (-1); 363 } 364 365 return (0); 366 } 367 368 int 369 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 370 { 371 return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 372 } 373 374 /*PRINTFLIKE3*/ 375 int 376 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 377 { 378 va_list ap; 379 380 va_start(ap, fmt); 381 382 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 383 va_end(ap); 384 return (-1); 385 } 386 387 switch (error) { 388 case ENXIO: 389 case ENODEV: 390 case EPIPE: 391 zfs_verror(hdl, EZFS_IO, fmt, ap); 392 break; 393 394 case ENOENT: 395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 396 "dataset does not exist")); 397 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 398 break; 399 400 case ENOSPC: 401 case EDQUOT: 402 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 403 return (-1); 404 405 case EEXIST: 406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 407 "dataset already exists")); 408 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 409 break; 410 411 case EBUSY: 412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 413 "dataset is busy")); 414 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 415 break; 416 case EROFS: 417 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 418 break; 419 case ENAMETOOLONG: 420 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 421 break; 422 case ENOTSUP: 423 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 424 break; 425 case EAGAIN: 426 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 427 "pool I/O is currently suspended")); 428 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 429 break; 430 case EREMOTEIO: 431 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 432 break; 433 default: 434 zfs_error_aux(hdl, strerror(error)); 435 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 436 break; 437 } 438 439 va_end(ap); 440 return (-1); 441 } 442 443 int 444 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 445 { 446 return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 447 } 448 449 /*PRINTFLIKE3*/ 450 int 451 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 452 { 453 va_list ap; 454 455 va_start(ap, fmt); 456 457 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 458 va_end(ap); 459 return (-1); 460 } 461 462 switch (error) { 463 case ENODEV: 464 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 465 break; 466 467 case ENOENT: 468 zfs_error_aux(hdl, 469 dgettext(TEXT_DOMAIN, "no such pool or dataset")); 470 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 471 break; 472 473 case EEXIST: 474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 475 "pool already exists")); 476 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 477 break; 478 479 case EBUSY: 480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 481 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 482 break; 483 484 case ENXIO: 485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 486 "one or more devices is currently unavailable")); 487 zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 488 break; 489 490 case ENAMETOOLONG: 491 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 492 break; 493 494 case ENOTSUP: 495 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 496 break; 497 498 case EINVAL: 499 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 500 break; 501 502 case ENOSPC: 503 case EDQUOT: 504 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 505 return (-1); 506 507 case EAGAIN: 508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 509 "pool I/O is currently suspended")); 510 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 511 break; 512 513 case EROFS: 514 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 515 break; 516 /* There is no pending operation to cancel */ 517 case ENOTACTIVE: 518 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap); 519 break; 520 case EREMOTEIO: 521 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 522 break; 523 case ZFS_ERR_CHECKPOINT_EXISTS: 524 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap); 525 break; 526 case ZFS_ERR_DISCARDING_CHECKPOINT: 527 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap); 528 break; 529 case ZFS_ERR_NO_CHECKPOINT: 530 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap); 531 break; 532 case ZFS_ERR_DEVRM_IN_PROGRESS: 533 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap); 534 break; 535 case ZFS_ERR_VDEV_TOO_BIG: 536 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap); 537 break; 538 default: 539 zfs_error_aux(hdl, strerror(error)); 540 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 541 } 542 543 va_end(ap); 544 return (-1); 545 } 546 547 /* 548 * Display an out of memory error message and abort the current program. 549 */ 550 int 551 no_memory(libzfs_handle_t *hdl) 552 { 553 return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 554 } 555 556 /* 557 * A safe form of malloc() which will die if the allocation fails. 558 */ 559 void * 560 zfs_alloc(libzfs_handle_t *hdl, size_t size) 561 { 562 void *data; 563 564 if ((data = calloc(1, size)) == NULL) 565 (void) no_memory(hdl); 566 567 return (data); 568 } 569 570 /* 571 * A safe form of asprintf() which will die if the allocation fails. 572 */ 573 /*PRINTFLIKE2*/ 574 char * 575 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) 576 { 577 va_list ap; 578 char *ret; 579 int err; 580 581 va_start(ap, fmt); 582 583 err = vasprintf(&ret, fmt, ap); 584 585 va_end(ap); 586 587 if (err < 0) 588 (void) no_memory(hdl); 589 590 return (ret); 591 } 592 593 /* 594 * A safe form of realloc(), which also zeroes newly allocated space. 595 */ 596 void * 597 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 598 { 599 void *ret; 600 601 if ((ret = realloc(ptr, newsize)) == NULL) { 602 (void) no_memory(hdl); 603 return (NULL); 604 } 605 606 bzero((char *)ret + oldsize, (newsize - oldsize)); 607 return (ret); 608 } 609 610 /* 611 * A safe form of strdup() which will die if the allocation fails. 612 */ 613 char * 614 zfs_strdup(libzfs_handle_t *hdl, const char *str) 615 { 616 char *ret; 617 618 if ((ret = strdup(str)) == NULL) 619 (void) no_memory(hdl); 620 621 return (ret); 622 } 623 624 /* 625 * Convert a number to an appropriately human-readable output. 626 */ 627 void 628 zfs_nicenum(uint64_t num, char *buf, size_t buflen) 629 { 630 nicenum(num, buf, buflen); 631 } 632 633 void 634 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 635 { 636 hdl->libzfs_printerr = printerr; 637 } 638 639 libzfs_handle_t * 640 libzfs_init(void) 641 { 642 libzfs_handle_t *hdl; 643 644 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { 645 return (NULL); 646 } 647 648 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 649 free(hdl); 650 return (NULL); 651 } 652 653 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "rF")) == NULL) { 654 (void) close(hdl->libzfs_fd); 655 free(hdl); 656 return (NULL); 657 } 658 659 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "rF"); 660 661 if (libzfs_core_init() != 0) { 662 (void) close(hdl->libzfs_fd); 663 (void) fclose(hdl->libzfs_mnttab); 664 (void) fclose(hdl->libzfs_sharetab); 665 free(hdl); 666 return (NULL); 667 } 668 669 zfs_prop_init(); 670 zpool_prop_init(); 671 zpool_feature_init(); 672 libzfs_mnttab_init(hdl); 673 674 if (getenv("ZFS_PROP_DEBUG") != NULL) { 675 hdl->libzfs_prop_debug = B_TRUE; 676 } 677 678 return (hdl); 679 } 680 681 void 682 libzfs_fini(libzfs_handle_t *hdl) 683 { 684 (void) close(hdl->libzfs_fd); 685 if (hdl->libzfs_mnttab) 686 (void) fclose(hdl->libzfs_mnttab); 687 if (hdl->libzfs_sharetab) 688 (void) fclose(hdl->libzfs_sharetab); 689 zfs_uninit_libshare(hdl); 690 zpool_free_handles(hdl); 691 libzfs_fru_clear(hdl, B_TRUE); 692 namespace_clear(hdl); 693 libzfs_mnttab_fini(hdl); 694 libzfs_core_fini(); 695 free(hdl); 696 } 697 698 libzfs_handle_t * 699 zpool_get_handle(zpool_handle_t *zhp) 700 { 701 return (zhp->zpool_hdl); 702 } 703 704 libzfs_handle_t * 705 zfs_get_handle(zfs_handle_t *zhp) 706 { 707 return (zhp->zfs_hdl); 708 } 709 710 zpool_handle_t * 711 zfs_get_pool_handle(const zfs_handle_t *zhp) 712 { 713 return (zhp->zpool_hdl); 714 } 715 716 /* 717 * Given a name, determine whether or not it's a valid path 718 * (starts with '/' or "./"). If so, walk the mnttab trying 719 * to match the device number. If not, treat the path as an 720 * fs/vol/snap/bkmark name. 721 */ 722 zfs_handle_t * 723 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 724 { 725 struct stat64 statbuf; 726 struct extmnttab entry; 727 int ret; 728 729 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 730 /* 731 * It's not a valid path, assume it's a name of type 'argtype'. 732 */ 733 return (zfs_open(hdl, path, argtype)); 734 } 735 736 if (stat64(path, &statbuf) != 0) { 737 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 738 return (NULL); 739 } 740 741 rewind(hdl->libzfs_mnttab); 742 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 743 if (makedevice(entry.mnt_major, entry.mnt_minor) == 744 statbuf.st_dev) { 745 break; 746 } 747 } 748 if (ret != 0) { 749 return (NULL); 750 } 751 752 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 753 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 754 path); 755 return (NULL); 756 } 757 758 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 759 } 760 761 /* 762 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 763 * an ioctl(). 764 */ 765 int 766 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 767 { 768 if (len == 0) 769 len = 16 * 1024; 770 zc->zc_nvlist_dst_size = len; 771 zc->zc_nvlist_dst = 772 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 773 if (zc->zc_nvlist_dst == 0) 774 return (-1); 775 776 return (0); 777 } 778 779 /* 780 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 781 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 782 * filled in by the kernel to indicate the actual required size. 783 */ 784 int 785 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 786 { 787 free((void *)(uintptr_t)zc->zc_nvlist_dst); 788 zc->zc_nvlist_dst = 789 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 790 if (zc->zc_nvlist_dst == 0) 791 return (-1); 792 793 return (0); 794 } 795 796 /* 797 * Called to free the src and dst nvlists stored in the command structure. 798 */ 799 void 800 zcmd_free_nvlists(zfs_cmd_t *zc) 801 { 802 free((void *)(uintptr_t)zc->zc_nvlist_conf); 803 free((void *)(uintptr_t)zc->zc_nvlist_src); 804 free((void *)(uintptr_t)zc->zc_nvlist_dst); 805 zc->zc_nvlist_conf = 0; 806 zc->zc_nvlist_src = 0; 807 zc->zc_nvlist_dst = 0; 808 } 809 810 static int 811 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 812 nvlist_t *nvl) 813 { 814 char *packed; 815 size_t len; 816 817 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 818 819 if ((packed = zfs_alloc(hdl, len)) == NULL) 820 return (-1); 821 822 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 823 824 *outnv = (uint64_t)(uintptr_t)packed; 825 *outlen = len; 826 827 return (0); 828 } 829 830 int 831 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 832 { 833 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 834 &zc->zc_nvlist_conf_size, nvl)); 835 } 836 837 int 838 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 839 { 840 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 841 &zc->zc_nvlist_src_size, nvl)); 842 } 843 844 /* 845 * Unpacks an nvlist from the ZFS ioctl command structure. 846 */ 847 int 848 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 849 { 850 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 851 zc->zc_nvlist_dst_size, nvlp, 0) != 0) 852 return (no_memory(hdl)); 853 854 return (0); 855 } 856 857 int 858 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 859 { 860 return (ioctl(hdl->libzfs_fd, request, zc)); 861 } 862 863 /* 864 * ================================================================ 865 * API shared by zfs and zpool property management 866 * ================================================================ 867 */ 868 869 static void 870 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 871 { 872 zprop_list_t *pl = cbp->cb_proplist; 873 int i; 874 char *title; 875 size_t len; 876 877 cbp->cb_first = B_FALSE; 878 if (cbp->cb_scripted) 879 return; 880 881 /* 882 * Start with the length of the column headers. 883 */ 884 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 885 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 886 "PROPERTY")); 887 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 888 "VALUE")); 889 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, 890 "RECEIVED")); 891 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 892 "SOURCE")); 893 894 /* first property is always NAME */ 895 assert(cbp->cb_proplist->pl_prop == 896 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 897 898 /* 899 * Go through and calculate the widths for each column. For the 900 * 'source' column, we kludge it up by taking the worst-case scenario of 901 * inheriting from the longest name. This is acceptable because in the 902 * majority of cases 'SOURCE' is the last column displayed, and we don't 903 * use the width anyway. Note that the 'VALUE' column can be oversized, 904 * if the name of the property is much longer than any values we find. 905 */ 906 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 907 /* 908 * 'PROPERTY' column 909 */ 910 if (pl->pl_prop != ZPROP_INVAL) { 911 const char *propname = (type == ZFS_TYPE_POOL) ? 912 zpool_prop_to_name(pl->pl_prop) : 913 zfs_prop_to_name(pl->pl_prop); 914 915 len = strlen(propname); 916 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 917 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 918 } else { 919 len = strlen(pl->pl_user_prop); 920 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 921 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 922 } 923 924 /* 925 * 'VALUE' column. The first property is always the 'name' 926 * property that was tacked on either by /sbin/zfs's 927 * zfs_do_get() or when calling zprop_expand_list(), so we 928 * ignore its width. If the user specified the name property 929 * to display, then it will be later in the list in any case. 930 */ 931 if (pl != cbp->cb_proplist && 932 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 933 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 934 935 /* 'RECEIVED' column. */ 936 if (pl != cbp->cb_proplist && 937 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) 938 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; 939 940 /* 941 * 'NAME' and 'SOURCE' columns 942 */ 943 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 944 ZFS_PROP_NAME) && 945 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 946 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 947 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 948 strlen(dgettext(TEXT_DOMAIN, "inherited from")); 949 } 950 } 951 952 /* 953 * Now go through and print the headers. 954 */ 955 for (i = 0; i < ZFS_GET_NCOLS; i++) { 956 switch (cbp->cb_columns[i]) { 957 case GET_COL_NAME: 958 title = dgettext(TEXT_DOMAIN, "NAME"); 959 break; 960 case GET_COL_PROPERTY: 961 title = dgettext(TEXT_DOMAIN, "PROPERTY"); 962 break; 963 case GET_COL_VALUE: 964 title = dgettext(TEXT_DOMAIN, "VALUE"); 965 break; 966 case GET_COL_RECVD: 967 title = dgettext(TEXT_DOMAIN, "RECEIVED"); 968 break; 969 case GET_COL_SOURCE: 970 title = dgettext(TEXT_DOMAIN, "SOURCE"); 971 break; 972 default: 973 title = NULL; 974 } 975 976 if (title != NULL) { 977 if (i == (ZFS_GET_NCOLS - 1) || 978 cbp->cb_columns[i + 1] == GET_COL_NONE) 979 (void) printf("%s", title); 980 else 981 (void) printf("%-*s ", 982 cbp->cb_colwidths[cbp->cb_columns[i]], 983 title); 984 } 985 } 986 (void) printf("\n"); 987 } 988 989 /* 990 * Display a single line of output, according to the settings in the callback 991 * structure. 992 */ 993 void 994 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 995 const char *propname, const char *value, zprop_source_t sourcetype, 996 const char *source, const char *recvd_value) 997 { 998 int i; 999 const char *str = NULL; 1000 char buf[128]; 1001 1002 /* 1003 * Ignore those source types that the user has chosen to ignore. 1004 */ 1005 if ((sourcetype & cbp->cb_sources) == 0) 1006 return; 1007 1008 if (cbp->cb_first) 1009 zprop_print_headers(cbp, cbp->cb_type); 1010 1011 for (i = 0; i < ZFS_GET_NCOLS; i++) { 1012 switch (cbp->cb_columns[i]) { 1013 case GET_COL_NAME: 1014 str = name; 1015 break; 1016 1017 case GET_COL_PROPERTY: 1018 str = propname; 1019 break; 1020 1021 case GET_COL_VALUE: 1022 str = value; 1023 break; 1024 1025 case GET_COL_SOURCE: 1026 switch (sourcetype) { 1027 case ZPROP_SRC_NONE: 1028 str = "-"; 1029 break; 1030 1031 case ZPROP_SRC_DEFAULT: 1032 str = "default"; 1033 break; 1034 1035 case ZPROP_SRC_LOCAL: 1036 str = "local"; 1037 break; 1038 1039 case ZPROP_SRC_TEMPORARY: 1040 str = "temporary"; 1041 break; 1042 1043 case ZPROP_SRC_INHERITED: 1044 (void) snprintf(buf, sizeof (buf), 1045 "inherited from %s", source); 1046 str = buf; 1047 break; 1048 case ZPROP_SRC_RECEIVED: 1049 str = "received"; 1050 break; 1051 1052 default: 1053 str = NULL; 1054 assert(!"unhandled zprop_source_t"); 1055 } 1056 break; 1057 1058 case GET_COL_RECVD: 1059 str = (recvd_value == NULL ? "-" : recvd_value); 1060 break; 1061 1062 default: 1063 continue; 1064 } 1065 1066 if (cbp->cb_columns[i + 1] == GET_COL_NONE) 1067 (void) printf("%s", str); 1068 else if (cbp->cb_scripted) 1069 (void) printf("%s\t", str); 1070 else 1071 (void) printf("%-*s ", 1072 cbp->cb_colwidths[cbp->cb_columns[i]], 1073 str); 1074 } 1075 1076 (void) printf("\n"); 1077 } 1078 1079 /* 1080 * Given a numeric suffix, convert the value into a number of bits that the 1081 * resulting value must be shifted. 1082 */ 1083 static int 1084 str2shift(libzfs_handle_t *hdl, const char *buf) 1085 { 1086 const char *ends = "BKMGTPEZ"; 1087 int i; 1088 1089 if (buf[0] == '\0') 1090 return (0); 1091 for (i = 0; i < strlen(ends); i++) { 1092 if (toupper(buf[0]) == ends[i]) 1093 break; 1094 } 1095 if (i == strlen(ends)) { 1096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1097 "invalid numeric suffix '%s'"), buf); 1098 return (-1); 1099 } 1100 1101 /* 1102 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 1103 * allow 'BB' - that's just weird. 1104 */ 1105 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 1106 toupper(buf[0]) != 'B')) 1107 return (10*i); 1108 1109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1110 "invalid numeric suffix '%s'"), buf); 1111 return (-1); 1112 } 1113 1114 /* 1115 * Convert a string of the form '100G' into a real number. Used when setting 1116 * properties or creating a volume. 'buf' is used to place an extended error 1117 * message for the caller to use. 1118 */ 1119 int 1120 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1121 { 1122 char *end; 1123 int shift; 1124 1125 *num = 0; 1126 1127 /* Check to see if this looks like a number. */ 1128 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1129 if (hdl) 1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1131 "bad numeric value '%s'"), value); 1132 return (-1); 1133 } 1134 1135 /* Rely on strtoull() to process the numeric portion. */ 1136 errno = 0; 1137 *num = strtoull(value, &end, 10); 1138 1139 /* 1140 * Check for ERANGE, which indicates that the value is too large to fit 1141 * in a 64-bit value. 1142 */ 1143 if (errno == ERANGE) { 1144 if (hdl) 1145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1146 "numeric value is too large")); 1147 return (-1); 1148 } 1149 1150 /* 1151 * If we have a decimal value, then do the computation with floating 1152 * point arithmetic. Otherwise, use standard arithmetic. 1153 */ 1154 if (*end == '.') { 1155 double fval = strtod(value, &end); 1156 1157 if ((shift = str2shift(hdl, end)) == -1) 1158 return (-1); 1159 1160 fval *= pow(2, shift); 1161 1162 if (fval > UINT64_MAX) { 1163 if (hdl) 1164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1165 "numeric value is too large")); 1166 return (-1); 1167 } 1168 1169 *num = (uint64_t)fval; 1170 } else { 1171 if ((shift = str2shift(hdl, end)) == -1) 1172 return (-1); 1173 1174 /* Check for overflow */ 1175 if (shift >= 64 || (*num << shift) >> shift != *num) { 1176 if (hdl) 1177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1178 "numeric value is too large")); 1179 return (-1); 1180 } 1181 1182 *num <<= shift; 1183 } 1184 1185 return (0); 1186 } 1187 1188 /* 1189 * Given a propname=value nvpair to set, parse any numeric properties 1190 * (index, boolean, etc) if they are specified as strings and add the 1191 * resulting nvpair to the returned nvlist. 1192 * 1193 * At the DSL layer, all properties are either 64-bit numbers or strings. 1194 * We want the user to be able to ignore this fact and specify properties 1195 * as native values (numbers, for example) or as strings (to simplify 1196 * command line utilities). This also handles converting index types 1197 * (compression, checksum, etc) from strings to their on-disk index. 1198 */ 1199 int 1200 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1201 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1202 const char *errbuf) 1203 { 1204 data_type_t datatype = nvpair_type(elem); 1205 zprop_type_t proptype; 1206 const char *propname; 1207 char *value; 1208 boolean_t isnone = B_FALSE; 1209 boolean_t isauto = B_FALSE; 1210 1211 if (type == ZFS_TYPE_POOL) { 1212 proptype = zpool_prop_get_type(prop); 1213 propname = zpool_prop_to_name(prop); 1214 } else { 1215 proptype = zfs_prop_get_type(prop); 1216 propname = zfs_prop_to_name(prop); 1217 } 1218 1219 /* 1220 * Convert any properties to the internal DSL value types. 1221 */ 1222 *svalp = NULL; 1223 *ivalp = 0; 1224 1225 switch (proptype) { 1226 case PROP_TYPE_STRING: 1227 if (datatype != DATA_TYPE_STRING) { 1228 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1229 "'%s' must be a string"), nvpair_name(elem)); 1230 goto error; 1231 } 1232 (void) nvpair_value_string(elem, svalp); 1233 if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1234 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1235 "'%s' is too long"), nvpair_name(elem)); 1236 goto error; 1237 } 1238 break; 1239 1240 case PROP_TYPE_NUMBER: 1241 if (datatype == DATA_TYPE_STRING) { 1242 (void) nvpair_value_string(elem, &value); 1243 if (strcmp(value, "none") == 0) { 1244 isnone = B_TRUE; 1245 } else if (strcmp(value, "auto") == 0) { 1246 isauto = B_TRUE; 1247 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) { 1248 goto error; 1249 } 1250 } else if (datatype == DATA_TYPE_UINT64) { 1251 (void) nvpair_value_uint64(elem, ivalp); 1252 } else { 1253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1254 "'%s' must be a number"), nvpair_name(elem)); 1255 goto error; 1256 } 1257 1258 /* 1259 * Quota special: force 'none' and don't allow 0. 1260 */ 1261 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1262 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1263 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1264 "use 'none' to disable quota/refquota")); 1265 goto error; 1266 } 1267 1268 /* 1269 * Special handling for "*_limit=none". In this case it's not 1270 * 0 but UINT64_MAX. 1271 */ 1272 if ((type & ZFS_TYPE_DATASET) && isnone && 1273 (prop == ZFS_PROP_FILESYSTEM_LIMIT || 1274 prop == ZFS_PROP_SNAPSHOT_LIMIT)) { 1275 *ivalp = UINT64_MAX; 1276 } 1277 1278 /* 1279 * Special handling for setting 'refreservation' to 'auto'. Use 1280 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv(). 1281 * 'auto' is only allowed on volumes. 1282 */ 1283 if (isauto) { 1284 switch (prop) { 1285 case ZFS_PROP_REFRESERVATION: 1286 if ((type & ZFS_TYPE_VOLUME) == 0) { 1287 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1288 "'%s=auto' only allowed on " 1289 "volumes"), nvpair_name(elem)); 1290 goto error; 1291 } 1292 *ivalp = UINT64_MAX; 1293 break; 1294 default: 1295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1296 "'auto' is invalid value for '%s'"), 1297 nvpair_name(elem)); 1298 goto error; 1299 } 1300 } 1301 1302 break; 1303 1304 case PROP_TYPE_INDEX: 1305 if (datatype != DATA_TYPE_STRING) { 1306 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1307 "'%s' must be a string"), nvpair_name(elem)); 1308 goto error; 1309 } 1310 1311 (void) nvpair_value_string(elem, &value); 1312 1313 if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1314 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1315 "'%s' must be one of '%s'"), propname, 1316 zprop_values(prop, type)); 1317 goto error; 1318 } 1319 break; 1320 1321 default: 1322 abort(); 1323 } 1324 1325 /* 1326 * Add the result to our return set of properties. 1327 */ 1328 if (*svalp != NULL) { 1329 if (nvlist_add_string(ret, propname, *svalp) != 0) { 1330 (void) no_memory(hdl); 1331 return (-1); 1332 } 1333 } else { 1334 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1335 (void) no_memory(hdl); 1336 return (-1); 1337 } 1338 } 1339 1340 return (0); 1341 error: 1342 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1343 return (-1); 1344 } 1345 1346 static int 1347 addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 1348 zfs_type_t type) 1349 { 1350 int prop; 1351 zprop_list_t *entry; 1352 1353 prop = zprop_name_to_prop(propname, type); 1354 1355 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 1356 prop = ZPROP_INVAL; 1357 1358 /* 1359 * When no property table entry can be found, return failure if 1360 * this is a pool property or if this isn't a user-defined 1361 * dataset property, 1362 */ 1363 if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL && 1364 !zpool_prop_feature(propname) && 1365 !zpool_prop_unsupported(propname)) || 1366 (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) && 1367 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) { 1368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1369 "invalid property '%s'"), propname); 1370 return (zfs_error(hdl, EZFS_BADPROP, 1371 dgettext(TEXT_DOMAIN, "bad property list"))); 1372 } 1373 1374 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1375 return (-1); 1376 1377 entry->pl_prop = prop; 1378 if (prop == ZPROP_INVAL) { 1379 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == 1380 NULL) { 1381 free(entry); 1382 return (-1); 1383 } 1384 entry->pl_width = strlen(propname); 1385 } else { 1386 entry->pl_width = zprop_width(prop, &entry->pl_fixed, 1387 type); 1388 } 1389 1390 *listp = entry; 1391 1392 return (0); 1393 } 1394 1395 /* 1396 * Given a comma-separated list of properties, construct a property list 1397 * containing both user-defined and native properties. This function will 1398 * return a NULL list if 'all' is specified, which can later be expanded 1399 * by zprop_expand_list(). 1400 */ 1401 int 1402 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1403 zfs_type_t type) 1404 { 1405 *listp = NULL; 1406 1407 /* 1408 * If 'all' is specified, return a NULL list. 1409 */ 1410 if (strcmp(props, "all") == 0) 1411 return (0); 1412 1413 /* 1414 * If no props were specified, return an error. 1415 */ 1416 if (props[0] == '\0') { 1417 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1418 "no properties specified")); 1419 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1420 "bad property list"))); 1421 } 1422 1423 /* 1424 * It would be nice to use getsubopt() here, but the inclusion of column 1425 * aliases makes this more effort than it's worth. 1426 */ 1427 while (*props != '\0') { 1428 size_t len; 1429 char *p; 1430 char c; 1431 1432 if ((p = strchr(props, ',')) == NULL) { 1433 len = strlen(props); 1434 p = props + len; 1435 } else { 1436 len = p - props; 1437 } 1438 1439 /* 1440 * Check for empty options. 1441 */ 1442 if (len == 0) { 1443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1444 "empty property name")); 1445 return (zfs_error(hdl, EZFS_BADPROP, 1446 dgettext(TEXT_DOMAIN, "bad property list"))); 1447 } 1448 1449 /* 1450 * Check all regular property names. 1451 */ 1452 c = props[len]; 1453 props[len] = '\0'; 1454 1455 if (strcmp(props, "space") == 0) { 1456 static char *spaceprops[] = { 1457 "name", "avail", "used", "usedbysnapshots", 1458 "usedbydataset", "usedbyrefreservation", 1459 "usedbychildren", NULL 1460 }; 1461 int i; 1462 1463 for (i = 0; spaceprops[i]; i++) { 1464 if (addlist(hdl, spaceprops[i], listp, type)) 1465 return (-1); 1466 listp = &(*listp)->pl_next; 1467 } 1468 } else { 1469 if (addlist(hdl, props, listp, type)) 1470 return (-1); 1471 listp = &(*listp)->pl_next; 1472 } 1473 1474 props = p; 1475 if (c == ',') 1476 props++; 1477 } 1478 1479 return (0); 1480 } 1481 1482 void 1483 zprop_free_list(zprop_list_t *pl) 1484 { 1485 zprop_list_t *next; 1486 1487 while (pl != NULL) { 1488 next = pl->pl_next; 1489 free(pl->pl_user_prop); 1490 free(pl); 1491 pl = next; 1492 } 1493 } 1494 1495 typedef struct expand_data { 1496 zprop_list_t **last; 1497 libzfs_handle_t *hdl; 1498 zfs_type_t type; 1499 } expand_data_t; 1500 1501 int 1502 zprop_expand_list_cb(int prop, void *cb) 1503 { 1504 zprop_list_t *entry; 1505 expand_data_t *edp = cb; 1506 1507 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1508 return (ZPROP_INVAL); 1509 1510 entry->pl_prop = prop; 1511 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1512 entry->pl_all = B_TRUE; 1513 1514 *(edp->last) = entry; 1515 edp->last = &entry->pl_next; 1516 1517 return (ZPROP_CONT); 1518 } 1519 1520 int 1521 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1522 { 1523 zprop_list_t *entry; 1524 zprop_list_t **last; 1525 expand_data_t exp; 1526 1527 if (*plp == NULL) { 1528 /* 1529 * If this is the very first time we've been called for an 'all' 1530 * specification, expand the list to include all native 1531 * properties. 1532 */ 1533 last = plp; 1534 1535 exp.last = last; 1536 exp.hdl = hdl; 1537 exp.type = type; 1538 1539 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1540 B_FALSE, type) == ZPROP_INVAL) 1541 return (-1); 1542 1543 /* 1544 * Add 'name' to the beginning of the list, which is handled 1545 * specially. 1546 */ 1547 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1548 return (-1); 1549 1550 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1551 ZFS_PROP_NAME; 1552 entry->pl_width = zprop_width(entry->pl_prop, 1553 &entry->pl_fixed, type); 1554 entry->pl_all = B_TRUE; 1555 entry->pl_next = *plp; 1556 *plp = entry; 1557 } 1558 return (0); 1559 } 1560 1561 int 1562 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1563 zfs_type_t type) 1564 { 1565 return (zprop_iter_common(func, cb, show_all, ordered, type)); 1566 } 1567 1568 /* 1569 * zfs_get_hole_count retrieves the number of holes (blocks which are 1570 * zero-filled) in the specified file using the _FIO_COUNT_FILLED ioctl. It 1571 * also optionally fetches the block size when bs is non-NULL. With hole count 1572 * and block size the full space consumed by the holes of a file can be 1573 * calculated. 1574 * 1575 * On success, zero is returned, the count argument is set to the 1576 * number of holes, and the bs argument is set to the block size (if it is 1577 * not NULL). On error, a non-zero errno is returned and the values in count 1578 * and bs are undefined. 1579 */ 1580 int 1581 zfs_get_hole_count(const char *path, uint64_t *count, uint64_t *bs) 1582 { 1583 int fd, err; 1584 struct stat64 ss; 1585 uint64_t fill; 1586 1587 fd = open(path, O_RDONLY | O_LARGEFILE); 1588 if (fd == -1) 1589 return (errno); 1590 1591 if (ioctl(fd, _FIO_COUNT_FILLED, &fill) == -1) { 1592 err = errno; 1593 (void) close(fd); 1594 return (err); 1595 } 1596 1597 if (fstat64(fd, &ss) == -1) { 1598 err = errno; 1599 (void) close(fd); 1600 return (err); 1601 } 1602 1603 *count = (ss.st_size + ss.st_blksize - 1) / ss.st_blksize - fill; 1604 VERIFY3S(*count, >=, 0); 1605 if (bs != NULL) { 1606 *bs = ss.st_blksize; 1607 } 1608 1609 if (close(fd) == -1) { 1610 return (errno); 1611 } 1612 return (0); 1613 } 1614 1615 ulong_t 1616 get_system_hostid(void) 1617 { 1618 char *env; 1619 1620 /* 1621 * Allow the hostid to be subverted for testing. 1622 */ 1623 env = getenv("ZFS_HOSTID"); 1624 if (env) { 1625 ulong_t hostid = strtoull(env, NULL, 16); 1626 return (hostid & 0xFFFFFFFF); 1627 } 1628 1629 return (gethostid()); 1630 } 1631