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