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