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