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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <assert.h> 30 #include <ctype.h> 31 #include <errno.h> 32 #include <libdevinfo.h> 33 #include <libintl.h> 34 #include <math.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <strings.h> 38 #include <unistd.h> 39 #include <zone.h> 40 #include <sys/mntent.h> 41 #include <sys/mnttab.h> 42 43 #include <sys/spa.h> 44 #include <sys/zio.h> 45 #include <libzfs.h> 46 47 #include "zfs_namecheck.h" 48 #include "zfs_prop.h" 49 #include "libzfs_impl.h" 50 51 /* 52 * Given a single type (not a mask of types), return the type in a human 53 * readable form. 54 */ 55 const char * 56 zfs_type_to_name(zfs_type_t type) 57 { 58 switch (type) { 59 case ZFS_TYPE_FILESYSTEM: 60 return (dgettext(TEXT_DOMAIN, "filesystem")); 61 case ZFS_TYPE_SNAPSHOT: 62 return (dgettext(TEXT_DOMAIN, "snapshot")); 63 case ZFS_TYPE_VOLUME: 64 return (dgettext(TEXT_DOMAIN, "volume")); 65 } 66 67 zfs_baderror(type); 68 return (NULL); 69 } 70 71 /* 72 * Given a path and mask of ZFS types, return a string describing this dataset. 73 * This is used when we fail to open a dataset and we cannot get an exact type. 74 * We guess what the type would have been based on the path and the mask of 75 * acceptable types. 76 */ 77 static const char * 78 path_to_str(const char *path, int types) 79 { 80 /* 81 * When given a single type, always report the exact type. 82 */ 83 if (types == ZFS_TYPE_SNAPSHOT) 84 return (dgettext(TEXT_DOMAIN, "snapshot")); 85 if (types == ZFS_TYPE_FILESYSTEM) 86 return (dgettext(TEXT_DOMAIN, "filesystem")); 87 if (types == ZFS_TYPE_VOLUME) 88 return (dgettext(TEXT_DOMAIN, "volume")); 89 90 /* 91 * The user is requesting more than one type of dataset. If this is the 92 * case, consult the path itself. If we're looking for a snapshot, and 93 * a '@' is found, then report it as "snapshot". Otherwise, remove the 94 * snapshot attribute and try again. 95 */ 96 if (types & ZFS_TYPE_SNAPSHOT) { 97 if (strchr(path, '@') != NULL) 98 return (dgettext(TEXT_DOMAIN, "snapshot")); 99 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 100 } 101 102 103 /* 104 * The user has requested either filesystems or volumes. 105 * We have no way of knowing a priori what type this would be, so always 106 * report it as "filesystem" or "volume", our two primitive types. 107 */ 108 if (types & ZFS_TYPE_FILESYSTEM) 109 return (dgettext(TEXT_DOMAIN, "filesystem")); 110 111 assert(types & ZFS_TYPE_VOLUME); 112 return (dgettext(TEXT_DOMAIN, "volume")); 113 } 114 115 /* 116 * Validate a ZFS path. This is used even before trying to open the dataset, to 117 * provide a more meaningful error message. We place a more useful message in 118 * 'buf' detailing exactly why the name was not valid. 119 */ 120 static int 121 zfs_validate_name(const char *path, int type, char *buf, size_t buflen) 122 { 123 namecheck_err_t why; 124 char what; 125 126 if (dataset_namecheck(path, &why, &what) != 0) { 127 if (buf != NULL) { 128 switch (why) { 129 case NAME_ERR_TOOLONG: 130 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 131 "name is too long"), buflen); 132 break; 133 134 case NAME_ERR_LEADING_SLASH: 135 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 136 "leading slash"), buflen); 137 break; 138 139 case NAME_ERR_EMPTY_COMPONENT: 140 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 141 "empty component"), buflen); 142 break; 143 144 case NAME_ERR_TRAILING_SLASH: 145 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 146 "trailing slash"), buflen); 147 break; 148 149 case NAME_ERR_INVALCHAR: 150 (void) snprintf(buf, buflen, 151 dgettext(TEXT_DOMAIN, "invalid character " 152 "'%c'"), what); 153 break; 154 155 case NAME_ERR_MULTIPLE_AT: 156 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 157 "multiple '@' delimiters"), buflen); 158 break; 159 } 160 } 161 162 return (0); 163 } 164 165 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 166 if (buf != NULL) 167 (void) strlcpy(buf, 168 dgettext(TEXT_DOMAIN, 169 "snapshot delimiter '@'"), buflen); 170 return (0); 171 } 172 173 return (1); 174 } 175 176 int 177 zfs_name_valid(const char *name, zfs_type_t type) 178 { 179 return (zfs_validate_name(name, type, NULL, NULL)); 180 } 181 182 /* 183 * Utility function to gather stats (objset and zpl) for the given object. 184 */ 185 static int 186 get_stats(zfs_handle_t *zhp) 187 { 188 zfs_cmd_t zc = { 0 }; 189 190 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 191 192 /* 193 * get the generic DMU stats and per-type (zfs, zvol) stats 194 */ 195 if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) 196 return (-1); 197 198 bcopy(&zc.zc_objset_stats, &zhp->zfs_dmustats, 199 sizeof (zc.zc_objset_stats)); 200 201 bcopy(&zc.zc_zfs_stats, &zhp->zfs_zplstats, sizeof (zc.zc_zfs_stats)); 202 203 zhp->zfs_volsize = zc.zc_volsize; 204 zhp->zfs_volblocksize = zc.zc_volblocksize; 205 206 return (0); 207 } 208 209 /* 210 * Refresh the properties currently stored in the handle. 211 */ 212 void 213 zfs_refresh_properties(zfs_handle_t *zhp) 214 { 215 (void) get_stats(zhp); 216 } 217 218 /* 219 * Makes a handle from the given dataset name. Used by zfs_open() and 220 * zfs_iter_* to create child handles on the fly. 221 */ 222 zfs_handle_t * 223 make_dataset_handle(const char *path) 224 { 225 zfs_handle_t *zhp = zfs_malloc(sizeof (zfs_handle_t)); 226 227 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 228 229 if (get_stats(zhp) != 0) { 230 free(zhp); 231 return (NULL); 232 } 233 234 /* 235 * We've managed to open the dataset and gather statistics. Determine 236 * the high-level type. 237 */ 238 if (zhp->zfs_dmustats.dds_is_snapshot) 239 zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 240 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 241 zhp->zfs_type = ZFS_TYPE_VOLUME; 242 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 243 zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 244 else 245 /* we should never see any other dataset types */ 246 zfs_baderror(zhp->zfs_dmustats.dds_type); 247 248 return (zhp); 249 } 250 251 /* 252 * Opens the given snapshot, filesystem, or volume. The 'types' 253 * argument is a mask of acceptable types. The function will print an 254 * appropriate error message and return NULL if it can't be opened. 255 */ 256 zfs_handle_t * 257 zfs_open(const char *path, int types) 258 { 259 zfs_handle_t *zhp; 260 261 /* 262 * Validate the name before we even try to open it. We don't care about 263 * the verbose invalid messages here; just report a generic error. 264 */ 265 if (!zfs_validate_name(path, types, NULL, 0)) { 266 zfs_error(dgettext(TEXT_DOMAIN, 267 "cannot open '%s': invalid %s name"), path, 268 path_to_str(path, types)); 269 return (NULL); 270 } 271 272 /* 273 * Try to get stats for the dataset, which will tell us if it exists. 274 */ 275 errno = 0; 276 if ((zhp = make_dataset_handle(path)) == NULL) { 277 switch (errno) { 278 case ENOENT: 279 /* 280 * The dataset doesn't exist. 281 */ 282 zfs_error(dgettext(TEXT_DOMAIN, 283 "cannot open '%s': no such %s"), path, 284 path_to_str(path, types)); 285 break; 286 287 case EBUSY: 288 /* 289 * We were able to open the dataset but couldn't 290 * get the stats. 291 */ 292 zfs_error(dgettext(TEXT_DOMAIN, 293 "cannot open '%s': %s is busy"), path, 294 path_to_str(path, types)); 295 break; 296 297 default: 298 zfs_baderror(errno); 299 300 } 301 return (NULL); 302 } 303 304 if (!(types & zhp->zfs_type)) { 305 zfs_error(dgettext(TEXT_DOMAIN, "cannot open '%s': operation " 306 "not supported for %ss"), path, 307 zfs_type_to_name(zhp->zfs_type)); 308 free(zhp); 309 return (NULL); 310 } 311 312 return (zhp); 313 } 314 315 /* 316 * Release a ZFS handle. Nothing to do but free the associated memory. 317 */ 318 void 319 zfs_close(zfs_handle_t *zhp) 320 { 321 if (zhp->zfs_mntopts) 322 free(zhp->zfs_mntopts); 323 free(zhp); 324 } 325 326 struct { 327 const char *name; 328 uint64_t value; 329 } checksum_table[] = { 330 { "on", ZIO_CHECKSUM_ON }, 331 { "off", ZIO_CHECKSUM_OFF }, 332 { "fletcher2", ZIO_CHECKSUM_FLETCHER_2 }, 333 { "fletcher4", ZIO_CHECKSUM_FLETCHER_4 }, 334 { "sha256", ZIO_CHECKSUM_SHA256 }, 335 { NULL } 336 }; 337 338 struct { 339 const char *name; 340 uint64_t value; 341 } compress_table[] = { 342 { "on", ZIO_COMPRESS_ON }, 343 { "off", ZIO_COMPRESS_OFF }, 344 { "lzjb", ZIO_COMPRESS_LZJB }, 345 { NULL } 346 }; 347 348 struct { 349 const char *name; 350 uint64_t value; 351 } snapdir_table[] = { 352 { "hidden", ZFS_SNAPDIR_HIDDEN }, 353 { "visible", ZFS_SNAPDIR_VISIBLE }, 354 { NULL } 355 }; 356 357 struct { 358 const char *name; 359 uint64_t value; 360 } acl_mode_table[] = { 361 { "discard", DISCARD }, 362 { "groupmask", GROUPMASK }, 363 { "passthrough", PASSTHROUGH }, 364 { NULL } 365 }; 366 367 struct { 368 const char *name; 369 uint64_t value; 370 } acl_inherit_table[] = { 371 { "discard", DISCARD }, 372 { "noallow", NOALLOW }, 373 { "secure", SECURE }, 374 { "passthrough", PASSTHROUGH }, 375 { NULL } 376 }; 377 378 379 /* 380 * Given a numeric suffix, convert the value into a number of bits that the 381 * resulting value must be shifted. 382 */ 383 static int 384 str2shift(const char *buf, char *reason, size_t len) 385 { 386 const char *ends = "BKMGTPEZ"; 387 int i; 388 389 if (buf[0] == '\0') 390 return (0); 391 for (i = 0; i < strlen(ends); i++) { 392 if (toupper(buf[0]) == ends[i]) 393 break; 394 } 395 if (i == strlen(ends)) { 396 (void) snprintf(reason, len, dgettext(TEXT_DOMAIN, "invalid " 397 "numeric suffix '%s'"), buf); 398 return (-1); 399 } 400 401 /* 402 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 403 * allow 'BB' - that's just weird. 404 */ 405 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 406 toupper(buf[0]) != 'B')) { 407 return (10*i); 408 } 409 410 (void) snprintf(reason, len, dgettext(TEXT_DOMAIN, "invalid numeric " 411 "suffix '%s'"), buf); 412 return (-1); 413 } 414 415 /* 416 * Convert a string of the form '100G' into a real number. Used when setting 417 * properties or creating a volume. 'buf' is used to place an extended error 418 * message for the caller to use. 419 */ 420 static int 421 nicestrtonum(const char *value, uint64_t *num, char *buf, size_t buflen) 422 { 423 char *end; 424 int shift; 425 426 *num = 0; 427 428 /* Check to see if this looks like a number. */ 429 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 430 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 431 "must be a numeric value"), buflen); 432 return (-1); 433 } 434 435 /* Rely on stroll() to process the numeric portion. */ 436 errno = 0; 437 *num = strtoll(value, &end, 10); 438 439 /* 440 * Check for ERANGE, which indicates that the value is too large to fit 441 * in a 64-bit value. 442 */ 443 if (errno == ERANGE) { 444 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 445 "value is too large"), buflen); 446 return (-1); 447 } 448 449 /* 450 * If we have a decimal value, then do the computation with floating 451 * point arithmetic. Otherwise, use standard arithmetic. 452 */ 453 if (*end == '.') { 454 double fval = strtod(value, &end); 455 456 if ((shift = str2shift(end, buf, buflen)) == -1) 457 return (-1); 458 459 fval *= pow(2, shift); 460 461 if (fval > UINT64_MAX) { 462 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 463 "value is too large"), buflen); 464 return (-1); 465 } 466 467 *num = (uint64_t)fval; 468 } else { 469 if ((shift = str2shift(end, buf, buflen)) == -1) 470 return (-1); 471 472 /* Check for overflow */ 473 if (shift >= 64 || (*num << shift) >> shift != *num) { 474 (void) strlcpy(buf, dgettext(TEXT_DOMAIN, 475 "value is too large"), buflen); 476 return (-1); 477 } 478 479 *num <<= shift; 480 } 481 482 return (0); 483 } 484 485 int 486 zfs_nicestrtonum(const char *str, uint64_t *val) 487 { 488 char buf[1]; 489 490 return (nicestrtonum(str, val, buf, sizeof (buf))); 491 } 492 493 /* 494 * Given a property type and value, verify that the value is appropriate. Used 495 * by zfs_prop_set() and some libzfs consumers. 496 */ 497 int 498 zfs_prop_validate(zfs_prop_t prop, const char *value, uint64_t *intval) 499 { 500 const char *propname = zfs_prop_to_name(prop); 501 uint64_t number; 502 char reason[64]; 503 int i; 504 505 /* 506 * Check to see if this a read-only property. 507 */ 508 if (zfs_prop_readonly(prop)) { 509 zfs_error(dgettext(TEXT_DOMAIN, 510 "cannot set %s property: read-only property"), propname); 511 return (-1); 512 } 513 514 /* See if the property value is too long */ 515 if (strlen(value) >= ZFS_MAXPROPLEN) { 516 zfs_error(dgettext(TEXT_DOMAIN, 517 "bad %s value '%s': value is too long"), propname, 518 value); 519 return (-1); 520 } 521 522 /* Perform basic checking based on property type */ 523 switch (zfs_prop_get_type(prop)) { 524 case prop_type_boolean: 525 if (strcmp(value, "on") == 0) { 526 number = 1; 527 } else if (strcmp(value, "off") == 0) { 528 number = 0; 529 } else { 530 zfs_error(dgettext(TEXT_DOMAIN, 531 "bad %s value '%s': must be 'on' or 'off'"), 532 propname, value); 533 return (-1); 534 } 535 break; 536 537 case prop_type_number: 538 /* treat 'none' as 0 */ 539 if (strcmp(value, "none") == 0) { 540 number = 0; 541 break; 542 } 543 544 if (nicestrtonum(value, &number, reason, 545 sizeof (reason)) != 0) { 546 zfs_error(dgettext(TEXT_DOMAIN, 547 "bad %s value '%s': %s"), propname, value, 548 reason); 549 return (-1); 550 } 551 552 /* don't allow 0 for quota, use 'none' instead */ 553 if (prop == ZFS_PROP_QUOTA && number == 0 && 554 strcmp(value, "none") != 0) { 555 zfs_error(dgettext(TEXT_DOMAIN, 556 "bad %s value '%s': use '%s=none' to disable"), 557 propname, value, propname); 558 return (-1); 559 } 560 561 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 562 if (prop == ZFS_PROP_RECORDSIZE || 563 prop == ZFS_PROP_VOLBLOCKSIZE) { 564 if (number < SPA_MINBLOCKSIZE || 565 number > SPA_MAXBLOCKSIZE || !ISP2(number)) { 566 zfs_error(dgettext(TEXT_DOMAIN, 567 "bad %s value '%s': " 568 "must be power of 2 from %u to %uk"), 569 propname, value, 570 (uint_t)SPA_MINBLOCKSIZE, 571 (uint_t)SPA_MAXBLOCKSIZE >> 10); 572 return (-1); 573 } 574 } 575 576 break; 577 578 case prop_type_string: 579 case prop_type_index: 580 /* 581 * The two writable string values, 'mountpoint' and 582 * 'checksum' need special consideration. The 'index' types are 583 * specified as strings by the user, but passed to the kernel as 584 * integers. 585 */ 586 switch (prop) { 587 case ZFS_PROP_MOUNTPOINT: 588 if (strcmp(value, ZFS_MOUNTPOINT_NONE) == 0 || 589 strcmp(value, ZFS_MOUNTPOINT_LEGACY) == 0) 590 break; 591 592 if (value[0] != '/') { 593 zfs_error(dgettext(TEXT_DOMAIN, 594 "bad %s value '%s': must be an absolute " 595 "path, 'none', or 'legacy'"), 596 propname, value); 597 return (-1); 598 } 599 break; 600 601 case ZFS_PROP_CHECKSUM: 602 for (i = 0; checksum_table[i].name != NULL; i++) { 603 if (strcmp(value, checksum_table[i].name) 604 == 0) { 605 number = checksum_table[i].value; 606 break; 607 } 608 } 609 610 if (checksum_table[i].name == NULL) { 611 zfs_error(dgettext(TEXT_DOMAIN, 612 "bad %s value '%s': must be 'on', 'off', " 613 "'fletcher2', 'fletcher4', or 'sha256'"), 614 propname, value); 615 return (-1); 616 } 617 break; 618 619 case ZFS_PROP_COMPRESSION: 620 for (i = 0; compress_table[i].name != NULL; i++) { 621 if (strcmp(value, compress_table[i].name) 622 == 0) { 623 number = compress_table[i].value; 624 break; 625 } 626 } 627 628 if (compress_table[i].name == NULL) { 629 zfs_error(dgettext(TEXT_DOMAIN, 630 "bad %s value '%s': must be 'on', 'off', " 631 "or 'lzjb'"), 632 propname, value); 633 return (-1); 634 } 635 break; 636 637 case ZFS_PROP_SNAPDIR: 638 for (i = 0; snapdir_table[i].name != NULL; i++) { 639 if (strcmp(value, snapdir_table[i].name) == 0) { 640 number = snapdir_table[i].value; 641 break; 642 } 643 } 644 645 if (snapdir_table[i].name == NULL) { 646 zfs_error(dgettext(TEXT_DOMAIN, 647 "bad %s value '%s': must be 'hidden' " 648 "or 'visible'"), 649 propname, value); 650 return (-1); 651 } 652 break; 653 654 case ZFS_PROP_ACLMODE: 655 for (i = 0; acl_mode_table[i].name != NULL; i++) { 656 if (strcmp(value, acl_mode_table[i].name) 657 == 0) { 658 number = acl_mode_table[i].value; 659 break; 660 } 661 } 662 663 if (acl_mode_table[i].name == NULL) { 664 zfs_error(dgettext(TEXT_DOMAIN, 665 "bad %s value '%s': must be 'discard', " 666 "'groupmask' or 'passthrough'"), 667 propname, value); 668 return (-1); 669 } 670 break; 671 672 case ZFS_PROP_ACLINHERIT: 673 for (i = 0; acl_inherit_table[i].name != NULL; i++) { 674 if (strcmp(value, acl_inherit_table[i].name) 675 == 0) { 676 number = acl_inherit_table[i].value; 677 break; 678 } 679 } 680 681 if (acl_inherit_table[i].name == NULL) { 682 zfs_error(dgettext(TEXT_DOMAIN, 683 "bad %s value '%s': must be 'discard', " 684 "'noallow', 'secure' or 'passthrough'"), 685 propname, value); 686 return (-1); 687 } 688 break; 689 690 case ZFS_PROP_SHARENFS: 691 /* 692 * Nothing to do for 'sharenfs', this gets passed on to 693 * share(1M) verbatim. 694 */ 695 break; 696 } 697 } 698 699 if (intval != NULL) 700 *intval = number; 701 702 return (0); 703 } 704 705 /* 706 * Given a property name and value, set the property for the given dataset. 707 */ 708 int 709 zfs_prop_set(zfs_handle_t *zhp, zfs_prop_t prop, const char *propval) 710 { 711 const char *propname = zfs_prop_to_name(prop); 712 uint64_t number; 713 zfs_cmd_t zc = { 0 }; 714 int ret; 715 prop_changelist_t *cl; 716 717 if (zfs_prop_validate(prop, propval, &number) != 0) 718 return (-1); 719 720 /* 721 * Check to see if the value applies to this type 722 */ 723 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 724 zfs_error(dgettext(TEXT_DOMAIN, 725 "cannot set %s for '%s': property does not apply to %ss"), 726 propname, zhp->zfs_name, zfs_type_to_name(zhp->zfs_type)); 727 return (-1); 728 } 729 730 /* 731 * For the mountpoint and sharenfs properties, check if it can be set 732 * in a global/non-global zone based on the zoned property value: 733 * 734 * global zone non-global zone 735 * ----------------------------------------------------- 736 * zoned=on mountpoint (no) mountpoint (yes) 737 * sharenfs (no) sharenfs (no) 738 * 739 * zoned=off mountpoint (yes) N/A 740 * sharenfs (yes) 741 */ 742 if (prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS) { 743 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 744 if (getzoneid() == GLOBAL_ZONEID) { 745 zfs_error(dgettext(TEXT_DOMAIN, 746 "cannot set %s for '%s': " 747 "dataset is used in a non-global zone"), 748 propname, zhp->zfs_name); 749 return (-1); 750 } else if (prop == ZFS_PROP_SHARENFS) { 751 zfs_error(dgettext(TEXT_DOMAIN, 752 "cannot set %s for '%s': filesystems " 753 "cannot be shared in a non-global zone"), 754 propname, zhp->zfs_name); 755 return (-1); 756 } 757 } else if (getzoneid() != GLOBAL_ZONEID) { 758 /* 759 * If zoned property is 'off', this must be in 760 * a globle zone. If not, something is wrong. 761 */ 762 zfs_error(dgettext(TEXT_DOMAIN, 763 "cannot set %s for '%s': dataset is " 764 "used in a non-global zone, but 'zoned' " 765 "property is not set"), 766 propname, zhp->zfs_name); 767 return (-1); 768 } 769 } 770 771 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 772 return (-1); 773 774 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 775 zfs_error(dgettext(TEXT_DOMAIN, "cannot set %s for '%s', " 776 "child dataset with inherited mountpoint is used " 777 "in a non-global zone"), 778 propname, zhp->zfs_name); 779 ret = -1; 780 goto error; 781 } 782 783 if ((ret = changelist_prefix(cl)) != 0) 784 goto error; 785 786 /* 787 * Execute the corresponding ioctl() to set this property. 788 */ 789 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 790 791 switch (prop) { 792 case ZFS_PROP_QUOTA: 793 zc.zc_cookie = number; 794 ret = ioctl(zfs_fd, ZFS_IOC_SET_QUOTA, &zc); 795 break; 796 case ZFS_PROP_RESERVATION: 797 zc.zc_cookie = number; 798 ret = ioctl(zfs_fd, ZFS_IOC_SET_RESERVATION, &zc); 799 break; 800 case ZFS_PROP_MOUNTPOINT: 801 case ZFS_PROP_SHARENFS: 802 /* 803 * These properties are passed down as real strings. 804 */ 805 (void) strlcpy(zc.zc_prop_name, propname, 806 sizeof (zc.zc_prop_name)); 807 (void) strlcpy(zc.zc_prop_value, propval, 808 sizeof (zc.zc_prop_value)); 809 zc.zc_intsz = 1; 810 zc.zc_numints = strlen(propval) + 1; 811 ret = ioctl(zfs_fd, ZFS_IOC_SET_PROP, &zc); 812 break; 813 case ZFS_PROP_VOLSIZE: 814 zc.zc_volsize = number; 815 ret = ioctl(zfs_fd, ZFS_IOC_SET_VOLSIZE, &zc); 816 break; 817 case ZFS_PROP_VOLBLOCKSIZE: 818 zc.zc_volblocksize = number; 819 ret = ioctl(zfs_fd, ZFS_IOC_SET_VOLBLOCKSIZE, &zc); 820 break; 821 default: 822 (void) strlcpy(zc.zc_prop_name, propname, 823 sizeof (zc.zc_prop_name)); 824 /* LINTED - alignment */ 825 *(uint64_t *)zc.zc_prop_value = number; 826 zc.zc_intsz = 8; 827 zc.zc_numints = 1; 828 ret = ioctl(zfs_fd, ZFS_IOC_SET_PROP, &zc); 829 break; 830 } 831 832 if (ret != 0) { 833 switch (errno) { 834 835 case EPERM: 836 zfs_error(dgettext(TEXT_DOMAIN, 837 "cannot set %s for '%s': permission " 838 "denied"), propname, zhp->zfs_name); 839 break; 840 841 case ENOENT: 842 zfs_error(dgettext(TEXT_DOMAIN, 843 "cannot open '%s': no such %s"), zhp->zfs_name, 844 zfs_type_to_name(zhp->zfs_type)); 845 break; 846 847 case ENOSPC: 848 /* 849 * For quotas and reservations, ENOSPC indicates 850 * something different; setting a quota or reservation 851 * doesn't use any disk space. 852 */ 853 switch (prop) { 854 case ZFS_PROP_QUOTA: 855 zfs_error(dgettext(TEXT_DOMAIN, "cannot set %s " 856 "for '%s': size is less than current " 857 "used or reserved space"), propname, 858 zhp->zfs_name); 859 break; 860 861 case ZFS_PROP_RESERVATION: 862 zfs_error(dgettext(TEXT_DOMAIN, "cannot set %s " 863 "for '%s': size is greater than available " 864 "space"), propname, zhp->zfs_name); 865 break; 866 867 default: 868 zfs_error(dgettext(TEXT_DOMAIN, 869 "cannot set %s for '%s': out of space"), 870 propname, zhp->zfs_name); 871 break; 872 } 873 break; 874 875 case EBUSY: 876 if (prop == ZFS_PROP_VOLBLOCKSIZE) { 877 zfs_error(dgettext(TEXT_DOMAIN, 878 "cannot set %s for '%s': " 879 "volume already contains data"), 880 propname, zhp->zfs_name); 881 } else { 882 zfs_baderror(errno); 883 } 884 break; 885 886 case EROFS: 887 zfs_error(dgettext(TEXT_DOMAIN, "cannot set %s for " 888 "'%s': read only %s"), propname, zhp->zfs_name, 889 zfs_type_to_name(zhp->zfs_type)); 890 break; 891 892 case EOVERFLOW: 893 /* 894 * This platform can't address a volume this big. 895 */ 896 #ifdef _ILP32 897 if (prop == ZFS_PROP_VOLSIZE) { 898 zfs_error(dgettext(TEXT_DOMAIN, 899 "cannot set %s for '%s': " 900 "max volume size is 1TB on 32-bit systems"), 901 propname, zhp->zfs_name); 902 break; 903 } 904 #endif 905 zfs_baderror(errno); 906 default: 907 zfs_baderror(errno); 908 } 909 } else { 910 /* 911 * Refresh the statistics so the new property value 912 * is reflected. 913 */ 914 if ((ret = changelist_postfix(cl)) != 0) 915 goto error; 916 917 (void) get_stats(zhp); 918 } 919 920 error: 921 changelist_free(cl); 922 return (ret); 923 } 924 925 /* 926 * Given a property, inherit the value from the parent dataset. 927 */ 928 int 929 zfs_prop_inherit(zfs_handle_t *zhp, zfs_prop_t prop) 930 { 931 const char *propname = zfs_prop_to_name(prop); 932 zfs_cmd_t zc = { 0 }; 933 int ret; 934 prop_changelist_t *cl; 935 936 /* 937 * Verify that this property is inheritable. 938 */ 939 if (zfs_prop_readonly(prop)) { 940 zfs_error(dgettext(TEXT_DOMAIN, 941 "cannot inherit %s for '%s': property is read-only"), 942 propname, zhp->zfs_name); 943 return (-1); 944 } 945 946 if (!zfs_prop_inheritable(prop)) { 947 zfs_error(dgettext(TEXT_DOMAIN, 948 "cannot inherit %s for '%s': property is not inheritable"), 949 propname, zhp->zfs_name); 950 return (-1); 951 } 952 953 /* 954 * Check to see if the value applies to this type 955 */ 956 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 957 zfs_error(dgettext(TEXT_DOMAIN, 958 "cannot inherit %s for '%s': property does " 959 "not apply to %ss"), propname, zhp->zfs_name, 960 zfs_type_to_name(zhp->zfs_type)); 961 return (-1); 962 } 963 964 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 965 (void) strlcpy(zc.zc_prop_name, propname, sizeof (zc.zc_prop_name)); 966 967 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 968 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 969 zfs_error(dgettext(TEXT_DOMAIN, "cannot inherit %s for '%s', " 970 "dataset is used in a non-global zone"), propname, 971 zhp->zfs_name); 972 return (-1); 973 } 974 975 /* 976 * Determine datasets which will be affected by this change, if any. 977 */ 978 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 979 return (-1); 980 981 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 982 zfs_error(dgettext(TEXT_DOMAIN, "cannot inherit %s for '%s', " 983 "child dataset with inherited mountpoint is " 984 "used in a non-global zone"), 985 propname, zhp->zfs_name); 986 ret = -1; 987 goto error; 988 } 989 990 if ((ret = changelist_prefix(cl)) != 0) 991 goto error; 992 993 zc.zc_numints = 0; 994 995 if ((ret = ioctl(zfs_fd, ZFS_IOC_SET_PROP, &zc)) != 0) { 996 switch (errno) { 997 case EPERM: 998 zfs_error(dgettext(TEXT_DOMAIN, 999 "cannot inherit %s for '%s': permission " 1000 "denied"), propname, zhp->zfs_name); 1001 break; 1002 case ENOENT: 1003 zfs_error(dgettext(TEXT_DOMAIN, 1004 "cannot open '%s': no such %s"), zhp->zfs_name, 1005 zfs_type_to_name(zhp->zfs_type)); 1006 break; 1007 case ENOSPC: 1008 zfs_error(dgettext(TEXT_DOMAIN, 1009 "cannot inherit %s for '%s': " 1010 "out of space"), propname, zhp->zfs_name); 1011 break; 1012 default: 1013 zfs_baderror(errno); 1014 } 1015 1016 } else { 1017 1018 if ((ret = changelist_postfix(cl)) != 0) 1019 goto error; 1020 1021 /* 1022 * Refresh the statistics so the new property is reflected. 1023 */ 1024 (void) get_stats(zhp); 1025 } 1026 1027 1028 error: 1029 changelist_free(cl); 1030 return (ret); 1031 } 1032 1033 static void 1034 nicebool(int value, char *buf, size_t buflen) 1035 { 1036 if (value) 1037 (void) strlcpy(buf, "on", buflen); 1038 else 1039 (void) strlcpy(buf, "off", buflen); 1040 } 1041 1042 /* 1043 * Internal function for getting a numeric property. Both zfs_prop_get() and 1044 * zfs_prop_get_int() are built using this interface. 1045 * 1046 * Certain properties can be overridden using 'mount -o'. In this case, scan 1047 * the contents of the /etc/mnttab entry, searching for the appropriate options. 1048 * If they differ from the on-disk values, report the current values and mark 1049 * the source "temporary". 1050 */ 1051 static uint64_t 1052 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, 1053 char **source) 1054 { 1055 uint64_t val; 1056 struct mnttab mnt; 1057 1058 *source = NULL; 1059 1060 if (zhp->zfs_mntopts == NULL) 1061 mnt.mnt_mntopts = ""; 1062 else 1063 mnt.mnt_mntopts = zhp->zfs_mntopts; 1064 1065 switch (prop) { 1066 case ZFS_PROP_ATIME: 1067 *source = zhp->zfs_zplstats.zs_atime_setpoint; 1068 val = zhp->zfs_zplstats.zs_devices; 1069 1070 if (hasmntopt(&mnt, MNTOPT_ATIME) && !val) { 1071 val = TRUE; 1072 if (src) 1073 *src = ZFS_SRC_TEMPORARY; 1074 } else if (hasmntopt(&mnt, MNTOPT_NOATIME) && val) { 1075 val = FALSE; 1076 if (src) 1077 *src = ZFS_SRC_TEMPORARY; 1078 } 1079 return (zhp->zfs_zplstats.zs_atime); 1080 1081 case ZFS_PROP_AVAILABLE: 1082 return (zhp->zfs_dmustats.dds_available); 1083 1084 case ZFS_PROP_DEVICES: 1085 *source = zhp->zfs_zplstats.zs_devices_setpoint; 1086 val = zhp->zfs_zplstats.zs_devices; 1087 1088 if (hasmntopt(&mnt, MNTOPT_DEVICES) && !val) { 1089 val = TRUE; 1090 if (src) 1091 *src = ZFS_SRC_TEMPORARY; 1092 } else if (hasmntopt(&mnt, MNTOPT_NODEVICES) && val) { 1093 val = FALSE; 1094 if (src) 1095 *src = ZFS_SRC_TEMPORARY; 1096 } 1097 return (val); 1098 1099 case ZFS_PROP_EXEC: 1100 *source = zhp->zfs_zplstats.zs_exec_setpoint; 1101 val = zhp->zfs_zplstats.zs_exec; 1102 1103 if (hasmntopt(&mnt, MNTOPT_EXEC) && !val) { 1104 val = TRUE; 1105 if (src) 1106 *src = ZFS_SRC_TEMPORARY; 1107 } else if (hasmntopt(&mnt, MNTOPT_NOEXEC) && val) { 1108 val = FALSE; 1109 if (src) 1110 *src = ZFS_SRC_TEMPORARY; 1111 } 1112 return (val); 1113 1114 case ZFS_PROP_RECORDSIZE: 1115 *source = zhp->zfs_zplstats.zs_recordsize_setpoint; 1116 return (zhp->zfs_zplstats.zs_recordsize); 1117 1118 case ZFS_PROP_COMPRESSION: 1119 *source = zhp->zfs_dmustats.dds_compression_setpoint; 1120 return (zhp->zfs_dmustats.dds_compression); 1121 1122 case ZFS_PROP_READONLY: 1123 *source = zhp->zfs_zplstats.zs_readonly_setpoint; 1124 val = zhp->zfs_zplstats.zs_readonly; 1125 1126 if (hasmntopt(&mnt, MNTOPT_RO) && !val) { 1127 val = TRUE; 1128 if (src) 1129 *src = ZFS_SRC_TEMPORARY; 1130 } else if (hasmntopt(&mnt, MNTOPT_RW) && val) { 1131 val = FALSE; 1132 if (src) 1133 *src = ZFS_SRC_TEMPORARY; 1134 } 1135 return (val); 1136 1137 case ZFS_PROP_QUOTA: 1138 if (zhp->zfs_dmustats.dds_quota == 0) 1139 *source = ""; /* default */ 1140 else 1141 *source = zhp->zfs_name; 1142 return (zhp->zfs_dmustats.dds_quota); 1143 1144 case ZFS_PROP_RESERVATION: 1145 if (zhp->zfs_dmustats.dds_reserved == 0) 1146 *source = ""; /* default */ 1147 else 1148 *source = zhp->zfs_name; 1149 return (zhp->zfs_dmustats.dds_reserved); 1150 1151 case ZFS_PROP_COMPRESSRATIO: 1152 /* 1153 * Using physical space and logical space, calculate the 1154 * compression ratio. We return the number as a multiple of 1155 * 100, so '2.5x' would be returned as 250. 1156 */ 1157 if (zhp->zfs_dmustats.dds_compressed_bytes == 0) 1158 return (100ULL); 1159 else 1160 return (zhp->zfs_dmustats.dds_uncompressed_bytes * 100 / 1161 zhp->zfs_dmustats.dds_compressed_bytes); 1162 1163 case ZFS_PROP_REFERENCED: 1164 /* 1165 * 'referenced' refers to the amount of physical space 1166 * referenced (possibly shared) by this object. 1167 */ 1168 return (zhp->zfs_dmustats.dds_space_refd); 1169 1170 case ZFS_PROP_SETUID: 1171 *source = zhp->zfs_zplstats.zs_setuid_setpoint; 1172 val = zhp->zfs_zplstats.zs_setuid; 1173 1174 if (hasmntopt(&mnt, MNTOPT_SETUID) && !val) { 1175 val = TRUE; 1176 if (src) 1177 *src = ZFS_SRC_TEMPORARY; 1178 } else if (hasmntopt(&mnt, MNTOPT_NOSETUID) && val) { 1179 val = FALSE; 1180 if (src) 1181 *src = ZFS_SRC_TEMPORARY; 1182 } 1183 return (val); 1184 1185 case ZFS_PROP_VOLSIZE: 1186 return (zhp->zfs_volsize); 1187 1188 case ZFS_PROP_VOLBLOCKSIZE: 1189 return (zhp->zfs_volblocksize); 1190 1191 case ZFS_PROP_ZONED: 1192 *source = zhp->zfs_dmustats.dds_zoned_setpoint; 1193 return (zhp->zfs_dmustats.dds_zoned); 1194 1195 case ZFS_PROP_USED: 1196 return (zhp->zfs_dmustats.dds_space_used); 1197 1198 case ZFS_PROP_CREATETXG: 1199 return (zhp->zfs_dmustats.dds_creation_txg); 1200 1201 case ZFS_PROP_MOUNTED: 1202 /* 1203 * Unlike other properties, we defer calculation of 'MOUNTED' 1204 * until actually requested. This is because the getmntany() 1205 * call can be extremely expensive on systems with a large 1206 * number of filesystems, and the property isn't needed in 1207 * normal use cases. 1208 */ 1209 if (zhp->zfs_mntopts == NULL) { 1210 struct mnttab search = { 0 }, entry; 1211 1212 search.mnt_special = (char *)zhp->zfs_name; 1213 rewind(mnttab_file); 1214 1215 if (getmntany(mnttab_file, &entry, &search) == 0) 1216 zhp->zfs_mntopts = 1217 zfs_strdup(entry.mnt_mntopts); 1218 } 1219 return (zhp->zfs_mntopts != NULL); 1220 1221 default: 1222 zfs_baderror(EINVAL); 1223 } 1224 1225 return (0); 1226 } 1227 1228 /* 1229 * Calculate the source type, given the raw source string. 1230 */ 1231 static void 1232 get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, 1233 char *statbuf, size_t statlen) 1234 { 1235 if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) 1236 return; 1237 1238 if (source == NULL) { 1239 *srctype = ZFS_SRC_NONE; 1240 } else if (source[0] == '\0') { 1241 *srctype = ZFS_SRC_DEFAULT; 1242 } else { 1243 if (strcmp(source, zhp->zfs_name) == 0) { 1244 *srctype = ZFS_SRC_LOCAL; 1245 } else { 1246 (void) strlcpy(statbuf, source, statlen); 1247 *srctype = ZFS_SRC_INHERITED; 1248 } 1249 } 1250 1251 } 1252 1253 /* 1254 * Retrieve a property from the given object. If 'literal' is specified, then 1255 * numbers are left as exact values. Otherwise, numbers are converted to a 1256 * human-readable form. 1257 * 1258 * Returns 0 on success, or -1 on error. 1259 */ 1260 int 1261 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 1262 zfs_source_t *src, char *statbuf, size_t statlen, int literal) 1263 { 1264 char *source = NULL; 1265 uint64_t val; 1266 char *str; 1267 int i; 1268 const char *root; 1269 1270 /* 1271 * Check to see if this property applies to our object 1272 */ 1273 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1274 return (-1); 1275 1276 if (src) 1277 *src = ZFS_SRC_NONE; 1278 1279 switch (prop) { 1280 case ZFS_PROP_ATIME: 1281 case ZFS_PROP_READONLY: 1282 case ZFS_PROP_SETUID: 1283 case ZFS_PROP_ZONED: 1284 case ZFS_PROP_DEVICES: 1285 case ZFS_PROP_EXEC: 1286 /* 1287 * Basic boolean values are built on top of 1288 * get_numeric_property(). 1289 */ 1290 nicebool(get_numeric_property(zhp, prop, src, &source), 1291 propbuf, proplen); 1292 1293 break; 1294 1295 case ZFS_PROP_AVAILABLE: 1296 case ZFS_PROP_RECORDSIZE: 1297 case ZFS_PROP_CREATETXG: 1298 case ZFS_PROP_REFERENCED: 1299 case ZFS_PROP_USED: 1300 case ZFS_PROP_VOLSIZE: 1301 case ZFS_PROP_VOLBLOCKSIZE: 1302 /* 1303 * Basic numeric values are built on top of 1304 * get_numeric_property(). 1305 */ 1306 val = get_numeric_property(zhp, prop, src, &source); 1307 if (literal) 1308 (void) snprintf(propbuf, proplen, "%llu", val); 1309 else 1310 zfs_nicenum(val, propbuf, proplen); 1311 break; 1312 1313 case ZFS_PROP_COMPRESSION: 1314 for (i = 0; compress_table[i].name != NULL; i++) { 1315 if (compress_table[i].value == 1316 zhp->zfs_dmustats.dds_compression) 1317 break; 1318 } 1319 assert(compress_table[i].name != NULL); 1320 (void) strlcpy(propbuf, compress_table[i].name, proplen); 1321 source = zhp->zfs_dmustats.dds_compression_setpoint; 1322 break; 1323 1324 case ZFS_PROP_CHECKSUM: 1325 for (i = 0; checksum_table[i].name != NULL; i++) { 1326 if (checksum_table[i].value == 1327 zhp->zfs_dmustats.dds_checksum) 1328 break; 1329 } 1330 assert(checksum_table[i].name != NULL); 1331 (void) strlcpy(propbuf, checksum_table[i].name, proplen); 1332 source = zhp->zfs_dmustats.dds_checksum_setpoint; 1333 break; 1334 1335 case ZFS_PROP_SNAPDIR: 1336 for (i = 0; snapdir_table[i].name != NULL; i++) { 1337 if (snapdir_table[i].value == 1338 zhp->zfs_zplstats.zs_snapdir) 1339 break; 1340 } 1341 assert(snapdir_table[i].name != NULL); 1342 (void) strlcpy(propbuf, snapdir_table[i].name, proplen); 1343 source = zhp->zfs_zplstats.zs_snapdir_setpoint; 1344 break; 1345 1346 case ZFS_PROP_ACLMODE: 1347 for (i = 0; acl_mode_table[i].name != NULL; i++) { 1348 if (acl_mode_table[i].value == 1349 zhp->zfs_zplstats.zs_acl_mode) 1350 break; 1351 } 1352 assert(acl_mode_table[i].name != NULL); 1353 (void) strlcpy(propbuf, acl_mode_table[i].name, proplen); 1354 source = zhp->zfs_zplstats.zs_acl_mode_setpoint; 1355 break; 1356 1357 case ZFS_PROP_ACLINHERIT: 1358 for (i = 0; acl_inherit_table[i].name != NULL; i++) { 1359 if (acl_inherit_table[i].value == 1360 zhp->zfs_zplstats.zs_acl_inherit) 1361 break; 1362 } 1363 assert(acl_inherit_table[i].name != NULL); 1364 (void) strlcpy(propbuf, acl_inherit_table[i].name, proplen); 1365 source = zhp->zfs_zplstats.zs_acl_inherit_setpoint; 1366 break; 1367 1368 case ZFS_PROP_CREATION: 1369 /* 1370 * 'creation' is a time_t stored in the statistics. We convert 1371 * this into a string unless 'literal' is specified. 1372 */ 1373 { 1374 time_t time = (time_t) 1375 zhp->zfs_dmustats.dds_creation_time; 1376 struct tm t; 1377 1378 if (literal || 1379 localtime_r(&time, &t) == NULL || 1380 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 1381 &t) == 0) 1382 (void) snprintf(propbuf, proplen, "%llu", 1383 zhp->zfs_dmustats.dds_creation_time); 1384 } 1385 break; 1386 1387 case ZFS_PROP_MOUNTPOINT: 1388 /* 1389 * Getting the precise mountpoint can be tricky. 1390 * 1391 * - for 'none' or 'legacy', return those values. 1392 * - for default mountpoints, construct it as /zfs/<dataset> 1393 * - for inherited mountpoints, we want to take everything 1394 * after our ancestor and append it to the inherited value. 1395 * 1396 * If the pool has an alternate root, we want to prepend that 1397 * root to any values we return. 1398 */ 1399 root = zhp->zfs_dmustats.dds_altroot; 1400 1401 if (zhp->zfs_zplstats.zs_mountpoint[0] == '\0') { 1402 (void) snprintf(propbuf, proplen, "%s/zfs/%s", 1403 root, zhp->zfs_name); 1404 } else if (zhp->zfs_zplstats.zs_mountpoint[0] == '/') { 1405 const char *relpath = zhp->zfs_name + 1406 strlen(zhp->zfs_zplstats.zs_mountpoint_setpoint); 1407 const char *mntpoint = zhp->zfs_zplstats.zs_mountpoint; 1408 1409 if (relpath[0] == '/') 1410 relpath++; 1411 if (mntpoint[1] == '\0') 1412 mntpoint++; 1413 1414 if (relpath[0] == '\0') 1415 (void) snprintf(propbuf, proplen, "%s%s", 1416 root, mntpoint); 1417 else 1418 (void) snprintf(propbuf, proplen, "%s%s%s%s", 1419 root, mntpoint, 1420 relpath[0] == '@' ? "" : "/", 1421 relpath); 1422 } else { 1423 /* 'legacy' or 'none' */ 1424 (void) strlcpy(propbuf, zhp->zfs_zplstats.zs_mountpoint, 1425 proplen); 1426 } 1427 1428 source = zhp->zfs_zplstats.zs_mountpoint_setpoint; 1429 break; 1430 1431 case ZFS_PROP_SHARENFS: 1432 (void) strlcpy(propbuf, zhp->zfs_zplstats.zs_sharenfs, proplen); 1433 source = zhp->zfs_zplstats.zs_sharenfs_setpoint; 1434 break; 1435 1436 case ZFS_PROP_ORIGIN: 1437 (void) strlcpy(propbuf, zhp->zfs_dmustats.dds_clone_of, 1438 proplen); 1439 /* 1440 * If there is no parent at all, return failure to indicate that 1441 * it doesn't apply to this dataset. 1442 */ 1443 if (propbuf[0] == '\0') 1444 return (-1); 1445 break; 1446 1447 case ZFS_PROP_QUOTA: 1448 case ZFS_PROP_RESERVATION: 1449 val = get_numeric_property(zhp, prop, src, &source); 1450 1451 /* 1452 * If quota or reservation is 0, we translate this into 'none' 1453 * (unless literal is set), and indicate that it's the default 1454 * value. Otherwise, we print the number nicely and indicate 1455 * that its set locally. 1456 */ 1457 if (val == 0) { 1458 if (literal) 1459 (void) strlcpy(propbuf, "0", proplen); 1460 else 1461 (void) strlcpy(propbuf, "none", proplen); 1462 } else { 1463 if (literal) 1464 (void) snprintf(propbuf, proplen, "%llu", val); 1465 else 1466 zfs_nicenum(val, propbuf, proplen); 1467 } 1468 break; 1469 1470 case ZFS_PROP_COMPRESSRATIO: 1471 val = get_numeric_property(zhp, prop, src, &source); 1472 (void) snprintf(propbuf, proplen, "%lld.%02lldx", val / 100, 1473 val % 100); 1474 break; 1475 1476 case ZFS_PROP_TYPE: 1477 switch (zhp->zfs_type) { 1478 case ZFS_TYPE_FILESYSTEM: 1479 str = "filesystem"; 1480 break; 1481 case ZFS_TYPE_VOLUME: 1482 str = "volume"; 1483 break; 1484 case ZFS_TYPE_SNAPSHOT: 1485 str = "snapshot"; 1486 break; 1487 default: 1488 zfs_baderror(zhp->zfs_type); 1489 } 1490 (void) snprintf(propbuf, proplen, "%s", str); 1491 break; 1492 1493 case ZFS_PROP_MOUNTED: 1494 /* 1495 * The 'mounted' property is a pseudo-property that described 1496 * whether the filesystem is currently mounted. Even though 1497 * it's a boolean value, the typical values of "on" and "off" 1498 * don't make sense, so we translate to "yes" and "no". 1499 */ 1500 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, src, &source)) 1501 (void) strlcpy(propbuf, "yes", proplen); 1502 else 1503 (void) strlcpy(propbuf, "no", proplen); 1504 break; 1505 1506 case ZFS_PROP_NAME: 1507 /* 1508 * The 'name' property is a pseudo-property derived from the 1509 * dataset name. It is presented as a real property to simplify 1510 * consumers. 1511 */ 1512 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 1513 break; 1514 1515 default: 1516 zfs_baderror(EINVAL); 1517 } 1518 1519 get_source(zhp, src, source, statbuf, statlen); 1520 1521 return (0); 1522 } 1523 1524 /* 1525 * Utility function to get the given numeric property. Does no validation that 1526 * the given property is the appropriate type; should only be used with 1527 * hard-coded property types. 1528 */ 1529 uint64_t 1530 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 1531 { 1532 char *source; 1533 zfs_source_t sourcetype = ZFS_SRC_NONE; 1534 1535 return (get_numeric_property(zhp, prop, &sourcetype, &source)); 1536 } 1537 1538 /* 1539 * Similar to zfs_prop_get(), but returns the value as an integer. 1540 */ 1541 int 1542 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 1543 zfs_source_t *src, char *statbuf, size_t statlen) 1544 { 1545 char *source; 1546 1547 /* 1548 * Check to see if this property applies to our object 1549 */ 1550 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1551 return (-1); 1552 1553 if (src) 1554 *src = ZFS_SRC_NONE; 1555 1556 *value = get_numeric_property(zhp, prop, src, &source); 1557 1558 get_source(zhp, src, source, statbuf, statlen); 1559 1560 return (0); 1561 } 1562 1563 /* 1564 * Returns the name of the given zfs handle. 1565 */ 1566 const char * 1567 zfs_get_name(const zfs_handle_t *zhp) 1568 { 1569 return (zhp->zfs_name); 1570 } 1571 1572 /* 1573 * Returns the type of the given zfs handle. 1574 */ 1575 zfs_type_t 1576 zfs_get_type(const zfs_handle_t *zhp) 1577 { 1578 return (zhp->zfs_type); 1579 } 1580 1581 /* 1582 * Iterate over all children, datasets and snapshots. 1583 */ 1584 int 1585 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 1586 { 1587 zfs_cmd_t zc = { 0 }; 1588 zfs_handle_t *nzhp; 1589 int ret; 1590 1591 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1592 ioctl(zfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 1593 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 1594 /* 1595 * Ignore private dataset names. 1596 */ 1597 if (dataset_name_hidden(zc.zc_name)) 1598 continue; 1599 1600 /* 1601 * Silently ignore errors, as the only plausible explanation is 1602 * that the pool has since been removed. 1603 */ 1604 if ((nzhp = make_dataset_handle(zc.zc_name)) == NULL) 1605 continue; 1606 1607 if ((ret = func(nzhp, data)) != 0) 1608 return (ret); 1609 } 1610 1611 /* 1612 * An errno value of ESRCH indicates normal completion. If ENOENT is 1613 * returned, then the underlying dataset has been removed since we 1614 * obtained the handle. 1615 */ 1616 if (errno != ESRCH && errno != ENOENT) 1617 zfs_baderror(errno); 1618 1619 bzero(&zc, sizeof (zc)); 1620 1621 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1622 ioctl(zfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, &zc) == 0; 1623 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 1624 1625 if ((nzhp = make_dataset_handle(zc.zc_name)) == NULL) 1626 continue; 1627 1628 if ((ret = func(nzhp, data)) != 0) 1629 return (ret); 1630 } 1631 1632 /* 1633 * An errno value of ESRCH indicates normal completion. If ENOENT is 1634 * returned, then the underlying dataset has been removed since we 1635 * obtained the handle. Silently ignore this case, and return success. 1636 */ 1637 if (errno != ESRCH && errno != ENOENT) 1638 zfs_baderror(errno); 1639 1640 return (0); 1641 } 1642 1643 /* 1644 * Given a complete name, return just the portion that refers to the parent. 1645 * Can return NULL if this is a pool. 1646 */ 1647 static int 1648 parent_name(const char *path, char *buf, size_t buflen) 1649 { 1650 char *loc; 1651 1652 if ((loc = strrchr(path, '/')) == NULL) 1653 return (-1); 1654 1655 (void) strncpy(buf, path, MIN(buflen, loc - path)); 1656 buf[loc - path] = '\0'; 1657 1658 return (0); 1659 } 1660 1661 /* 1662 * Checks to make sure that the given path has a parent, and that it exists. 1663 */ 1664 static int 1665 check_parents(const char *path, zfs_type_t type) 1666 { 1667 zfs_cmd_t zc = { 0 }; 1668 char parent[ZFS_MAXNAMELEN]; 1669 char *slash; 1670 1671 /* get parent, and check to see if this is just a pool */ 1672 if (parent_name(path, parent, sizeof (parent)) != 0) { 1673 zfs_error(dgettext(TEXT_DOMAIN, 1674 "cannot create '%s': missing dataset name"), 1675 path, zfs_type_to_name(type)); 1676 zfs_error(dgettext(TEXT_DOMAIN, 1677 "use 'zpool create' to create a storage pool")); 1678 return (-1); 1679 } 1680 1681 /* check to see if the pool exists */ 1682 if ((slash = strchr(parent, '/')) == NULL) 1683 slash = parent + strlen(parent); 1684 (void) strncpy(zc.zc_name, parent, slash - parent); 1685 zc.zc_name[slash - parent] = '\0'; 1686 if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 1687 errno == ENOENT) { 1688 zfs_error(dgettext(TEXT_DOMAIN, 1689 "cannot create '%s': no such pool '%s'"), path, zc.zc_name); 1690 return (-1); 1691 } 1692 1693 /* check to see if the parent dataset exists */ 1694 (void) strlcpy(zc.zc_name, parent, sizeof (zc.zc_name)); 1695 if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 1696 switch (errno) { 1697 case ENOENT: 1698 zfs_error(dgettext(TEXT_DOMAIN, 1699 "cannot create '%s': parent does not exist"), path); 1700 return (-1); 1701 1702 default: 1703 zfs_baderror(errno); 1704 } 1705 } 1706 1707 /* we are in a non-global zone, but parent is in the global zone */ 1708 if (getzoneid() != GLOBAL_ZONEID && !zc.zc_objset_stats.dds_zoned) { 1709 zfs_error(dgettext(TEXT_DOMAIN, 1710 "cannot create '%s': permission denied"), path); 1711 return (-1); 1712 } 1713 1714 /* make sure parent is a filesystem */ 1715 if (zc.zc_objset_stats.dds_type != DMU_OST_ZFS) { 1716 zfs_error(dgettext(TEXT_DOMAIN, 1717 "cannot create '%s': parent is not a filesystem"), 1718 path); 1719 return (-1); 1720 } 1721 1722 return (0); 1723 } 1724 1725 /* 1726 * Create a new filesystem or volume. 'sizestr' and 'blocksizestr' are used 1727 * only for volumes, and indicate the size and blocksize of the volume. 1728 */ 1729 int 1730 zfs_create(const char *path, zfs_type_t type, 1731 const char *sizestr, const char *blocksizestr) 1732 { 1733 char reason[64]; 1734 zfs_cmd_t zc = { 0 }; 1735 int ret; 1736 uint64_t size = 0; 1737 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 1738 1739 /* convert sizestr into integer size */ 1740 if (sizestr != NULL && nicestrtonum(sizestr, &size, 1741 reason, sizeof (reason)) != 0) { 1742 zfs_error(dgettext(TEXT_DOMAIN, 1743 "bad volume size '%s': %s"), sizestr, reason); 1744 return (-1); 1745 } 1746 1747 /* convert blocksizestr into integer blocksize */ 1748 if (blocksizestr != NULL && nicestrtonum(blocksizestr, &blocksize, 1749 reason, sizeof (reason)) != 0) { 1750 zfs_error(dgettext(TEXT_DOMAIN, 1751 "bad volume blocksize '%s': %s"), blocksizestr, reason); 1752 return (-1); 1753 } 1754 1755 /* validate the path, taking care to note the extended error message */ 1756 if (!zfs_validate_name(path, type, reason, sizeof (reason))) { 1757 zfs_error(dgettext(TEXT_DOMAIN, 1758 "cannot create '%s': %s in %s name"), path, reason, 1759 zfs_type_to_name(type)); 1760 if (strstr(reason, "snapshot") != NULL) 1761 zfs_error(dgettext(TEXT_DOMAIN, 1762 "use 'zfs snapshot' to create a snapshot")); 1763 return (-1); 1764 } 1765 1766 /* validate parents exist */ 1767 if (check_parents(path, type) != 0) 1768 return (-1); 1769 1770 /* 1771 * The failure modes when creating a dataset of a different type over 1772 * one that already exists is a little strange. In particular, if you 1773 * try to create a dataset on top of an existing dataset, the ioctl() 1774 * will return ENOENT, not EEXIST. To prevent this from happening, we 1775 * first try to see if the dataset exists. 1776 */ 1777 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 1778 if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0) { 1779 zfs_error(dgettext(TEXT_DOMAIN, 1780 "cannot create '%s': dataset exists"), path); 1781 return (-1); 1782 } 1783 1784 if (type == ZFS_TYPE_VOLUME) 1785 zc.zc_objset_type = DMU_OST_ZVOL; 1786 else 1787 zc.zc_objset_type = DMU_OST_ZFS; 1788 1789 if (type == ZFS_TYPE_VOLUME) { 1790 /* 1791 * If we are creating a volume, the size and block size must 1792 * satisfy a few restraints. First, the blocksize must be a 1793 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 1794 * volsize must be a multiple of the block size, and cannot be 1795 * zero. 1796 */ 1797 if (size == 0) { 1798 zfs_error(dgettext(TEXT_DOMAIN, 1799 "bad volume size '%s': cannot be zero"), sizestr); 1800 return (-1); 1801 } 1802 1803 if (blocksize < SPA_MINBLOCKSIZE || 1804 blocksize > SPA_MAXBLOCKSIZE || !ISP2(blocksize)) { 1805 zfs_error(dgettext(TEXT_DOMAIN, 1806 "bad volume block size '%s': " 1807 "must be power of 2 from %u to %uk"), 1808 blocksizestr, 1809 (uint_t)SPA_MINBLOCKSIZE, 1810 (uint_t)SPA_MAXBLOCKSIZE >> 10); 1811 return (-1); 1812 } 1813 1814 if (size % blocksize != 0) { 1815 char buf[64]; 1816 zfs_nicenum(blocksize, buf, sizeof (buf)); 1817 zfs_error(dgettext(TEXT_DOMAIN, 1818 "bad volume size '%s': " 1819 "must be multiple of volume block size (%s)"), 1820 sizestr, buf); 1821 return (-1); 1822 } 1823 1824 zc.zc_volsize = size; 1825 zc.zc_volblocksize = blocksize; 1826 } 1827 1828 /* create the dataset */ 1829 ret = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc); 1830 1831 if (ret == 0 && type == ZFS_TYPE_VOLUME) 1832 ret = zvol_create_link(path); 1833 1834 /* check for failure */ 1835 if (ret != 0) { 1836 char parent[ZFS_MAXNAMELEN]; 1837 (void) parent_name(path, parent, sizeof (parent)); 1838 1839 switch (errno) { 1840 case ENOENT: 1841 /* 1842 * The parent dataset has been deleted since our 1843 * previous check. 1844 */ 1845 zfs_error(dgettext(TEXT_DOMAIN, 1846 "cannot create '%s': no such parent '%s'"), 1847 path, parent); 1848 break; 1849 1850 case EPERM: 1851 /* 1852 * The user doesn't have permission to create a new 1853 * dataset here. 1854 */ 1855 zfs_error(dgettext(TEXT_DOMAIN, 1856 "cannot create '%s': permission denied"), path); 1857 break; 1858 1859 case EDQUOT: 1860 case ENOSPC: 1861 /* 1862 * The parent dataset does not have enough free space 1863 * to create a new dataset. 1864 */ 1865 zfs_error(dgettext(TEXT_DOMAIN, 1866 "cannot create '%s': not enough space in '%s'"), 1867 path, parent); 1868 break; 1869 1870 case EEXIST: 1871 /* 1872 * The target dataset already exists. We should have 1873 * caught this above, but there may be some unexplained 1874 * race condition. 1875 */ 1876 zfs_error(dgettext(TEXT_DOMAIN, 1877 "cannot create '%s': dataset exists"), path); 1878 break; 1879 1880 case EINVAL: 1881 /* 1882 * The target dataset does not support children. 1883 */ 1884 zfs_error(dgettext(TEXT_DOMAIN, 1885 "cannot create '%s': children unsupported in '%s'"), 1886 path, parent); 1887 break; 1888 1889 case EDOM: 1890 zfs_error(dgettext(TEXT_DOMAIN, "bad %s value '%s': " 1891 "must be power of 2 from %u to %uk"), 1892 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1893 blocksizestr ? blocksizestr : "<unknown>", 1894 (uint_t)SPA_MINBLOCKSIZE, 1895 (uint_t)SPA_MAXBLOCKSIZE >> 10); 1896 break; 1897 #ifdef _ILP32 1898 case EOVERFLOW: 1899 /* 1900 * This platform can't address a volume this big. 1901 */ 1902 if (type == ZFS_TYPE_VOLUME) { 1903 zfs_error(dgettext(TEXT_DOMAIN, 1904 "cannot create '%s': " 1905 "max volume size is 1TB on 32-bit systems"), 1906 path); 1907 break; 1908 } 1909 #endif 1910 1911 default: 1912 zfs_baderror(errno); 1913 } 1914 1915 return (-1); 1916 } 1917 1918 return (0); 1919 } 1920 1921 /* 1922 * Destroys the given dataset. The caller must make sure that the filesystem 1923 * isn't mounted, and that there are no active dependents. 1924 */ 1925 int 1926 zfs_destroy(zfs_handle_t *zhp) 1927 { 1928 zfs_cmd_t zc = { 0 }; 1929 int ret; 1930 1931 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1932 1933 /* 1934 * We use the check for 'zfs_volblocksize' instead of ZFS_TYPE_VOLUME 1935 * so that we do the right thing for snapshots of volumes. 1936 */ 1937 if (zhp->zfs_volblocksize != 0) { 1938 if (zvol_remove_link(zhp->zfs_name) != 0) 1939 return (-1); 1940 1941 zc.zc_objset_type = DMU_OST_ZVOL; 1942 } else { 1943 zc.zc_objset_type = DMU_OST_ZFS; 1944 } 1945 1946 ret = ioctl(zfs_fd, ZFS_IOC_DESTROY, &zc); 1947 1948 if (ret != 0) { 1949 switch (errno) { 1950 1951 case EPERM: 1952 /* 1953 * We don't have permission to destroy this dataset. 1954 */ 1955 zfs_error(dgettext(TEXT_DOMAIN, 1956 "cannot destroy '%s': permission denied"), 1957 zhp->zfs_name); 1958 break; 1959 1960 case ENOENT: 1961 /* 1962 * We've hit a race condition where the dataset has been 1963 * destroyed since we opened it. 1964 */ 1965 zfs_error(dgettext(TEXT_DOMAIN, 1966 "cannot destroy '%s': no such %s"), 1967 zhp->zfs_name, zfs_type_to_name(zhp->zfs_type)); 1968 break; 1969 1970 case EBUSY: 1971 /* 1972 * Even if we destroy all children, there is a chance we 1973 * can hit this case if: 1974 * 1975 * - A child dataset has since been created 1976 * - A filesystem is mounted 1977 * 1978 * This error message is awful, but hopefully we've 1979 * already caught the common cases (and aborted more 1980 * appropriately) before calling this function. There's 1981 * nothing else we can do at this point. 1982 */ 1983 zfs_error(dgettext(TEXT_DOMAIN, 1984 "cannot destroy '%s': %s is busy"), 1985 zhp->zfs_name, zfs_type_to_name(zhp->zfs_type)); 1986 break; 1987 1988 default: 1989 zfs_baderror(errno); 1990 } 1991 1992 return (-1); 1993 } 1994 1995 remove_mountpoint(zhp); 1996 1997 return (0); 1998 } 1999 2000 /* 2001 * Clones the given dataset. The target must be of the same type as the source. 2002 */ 2003 int 2004 zfs_clone(zfs_handle_t *zhp, const char *target) 2005 { 2006 char reason[64]; 2007 zfs_cmd_t zc = { 0 }; 2008 char parent[ZFS_MAXNAMELEN]; 2009 int ret; 2010 2011 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2012 2013 /* validate the target name */ 2014 if (!zfs_validate_name(target, ZFS_TYPE_FILESYSTEM, reason, 2015 sizeof (reason))) { 2016 zfs_error(dgettext(TEXT_DOMAIN, 2017 "cannot create '%s': %s in filesystem name"), target, 2018 reason, zfs_type_to_name(ZFS_TYPE_FILESYSTEM)); 2019 return (-1); 2020 } 2021 2022 /* validate parents exist */ 2023 if (check_parents(target, zhp->zfs_type) != 0) 2024 return (-1); 2025 2026 (void) parent_name(target, parent, sizeof (parent)); 2027 2028 /* do the clone */ 2029 if (zhp->zfs_volblocksize != 0) 2030 zc.zc_objset_type = DMU_OST_ZVOL; 2031 else 2032 zc.zc_objset_type = DMU_OST_ZFS; 2033 2034 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 2035 (void) strlcpy(zc.zc_filename, zhp->zfs_name, sizeof (zc.zc_filename)); 2036 ret = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc); 2037 2038 if (ret != 0) { 2039 switch (errno) { 2040 case EPERM: 2041 /* 2042 * The user doesn't have permission to create the clone. 2043 */ 2044 zfs_error(dgettext(TEXT_DOMAIN, 2045 "cannot create '%s': permission denied"), 2046 target); 2047 break; 2048 2049 case ENOENT: 2050 /* 2051 * The parent doesn't exist. We should have caught this 2052 * above, but there may a race condition that has since 2053 * destroyed the parent. 2054 * 2055 * At this point, we don't know whether it's the source 2056 * that doesn't exist anymore, or whether the target 2057 * dataset doesn't exist. 2058 */ 2059 zfs_error(dgettext(TEXT_DOMAIN, 2060 "cannot create '%s': no such parent '%s'"), 2061 target, parent); 2062 break; 2063 2064 case EDQUOT: 2065 case ENOSPC: 2066 /* 2067 * There is not enough space in the target dataset 2068 */ 2069 zfs_error(dgettext(TEXT_DOMAIN, 2070 "cannot create '%s': not enough space in '%s'"), 2071 target, parent); 2072 break; 2073 2074 case EEXIST: 2075 /* 2076 * The target already exists. 2077 */ 2078 zfs_error(dgettext(TEXT_DOMAIN, 2079 "cannot create '%s': dataset exists"), target); 2080 break; 2081 2082 case EXDEV: 2083 /* 2084 * The source and target pools differ. 2085 */ 2086 zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': " 2087 "source and target pools differ"), target); 2088 break; 2089 2090 default: 2091 zfs_baderror(errno); 2092 } 2093 } else if (zhp->zfs_volblocksize != 0) { 2094 ret = zvol_create_link(target); 2095 } 2096 2097 return (ret); 2098 } 2099 2100 /* 2101 * Takes a snapshot of the given dataset 2102 */ 2103 int 2104 zfs_snapshot(const char *path) 2105 { 2106 char reason[64]; 2107 const char *delim; 2108 char *parent; 2109 zfs_handle_t *zhp; 2110 zfs_cmd_t zc = { 0 }; 2111 int ret; 2112 2113 /* validate the snapshot name */ 2114 if (!zfs_validate_name(path, ZFS_TYPE_SNAPSHOT, reason, 2115 sizeof (reason))) { 2116 zfs_error(dgettext(TEXT_DOMAIN, 2117 "cannot snapshot '%s': %s in snapshot name"), path, 2118 reason); 2119 return (-1); 2120 } 2121 2122 /* make sure we have a snapshot */ 2123 if ((delim = strchr(path, '@')) == NULL) { 2124 zfs_error(dgettext(TEXT_DOMAIN, 2125 "cannot snapshot '%s': missing '@' delim in snapshot " 2126 "name"), path); 2127 zfs_error(dgettext(TEXT_DOMAIN, 2128 "use 'zfs create' to create a filesystem")); 2129 return (-1); 2130 } 2131 2132 /* make sure the parent exists and is of the appropriate type */ 2133 parent = zfs_malloc(delim - path + 1); 2134 (void) strncpy(parent, path, delim - path); 2135 parent[delim - path] = '\0'; 2136 2137 if ((zhp = zfs_open(parent, ZFS_TYPE_FILESYSTEM | 2138 ZFS_TYPE_VOLUME)) == NULL) { 2139 free(parent); 2140 return (-1); 2141 } 2142 2143 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 2144 2145 if (zhp->zfs_type == ZFS_TYPE_VOLUME) 2146 zc.zc_objset_type = DMU_OST_ZVOL; 2147 else 2148 zc.zc_objset_type = DMU_OST_ZFS; 2149 2150 ret = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc); 2151 2152 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 2153 ret = zvol_create_link(path); 2154 if (ret != 0) 2155 (void) ioctl(zfs_fd, ZFS_IOC_DESTROY, &zc); 2156 } 2157 2158 if (ret != 0) { 2159 switch (errno) { 2160 case EPERM: 2161 /* 2162 * User doesn't have permission to create a snapshot 2163 */ 2164 zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': " 2165 "permission denied"), path); 2166 break; 2167 2168 case EDQUOT: 2169 case ENOSPC: 2170 /* 2171 * Out of space in parent. 2172 */ 2173 zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': " 2174 "not enough space in '%s'"), path, parent); 2175 break; 2176 2177 case EEXIST: 2178 /* 2179 * Snapshot already exists. 2180 */ 2181 zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': " 2182 "snapshot exists"), path); 2183 break; 2184 2185 case ENOENT: 2186 /* 2187 * Shouldn't happen because we verified the parent 2188 * above. But there may be a race condition where it 2189 * has since been removed. 2190 */ 2191 zfs_error(dgettext(TEXT_DOMAIN, "cannot open '%s': " 2192 "no such %s"), parent, 2193 zfs_type_to_name(zhp->zfs_type)); 2194 break; 2195 2196 default: 2197 zfs_baderror(errno); 2198 } 2199 } 2200 2201 free(parent); 2202 zfs_close(zhp); 2203 2204 return (ret); 2205 } 2206 2207 /* 2208 * Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL. 2209 */ 2210 int 2211 zfs_backup(zfs_handle_t *zhp_to, zfs_handle_t *zhp_from) 2212 { 2213 zfs_cmd_t zc = { 0 }; 2214 int ret; 2215 2216 /* do the ioctl() */ 2217 (void) strlcpy(zc.zc_name, zhp_to->zfs_name, sizeof (zc.zc_name)); 2218 if (zhp_from) { 2219 (void) strlcpy(zc.zc_prop_value, zhp_from->zfs_name, 2220 sizeof (zc.zc_name)); 2221 } else { 2222 zc.zc_prop_value[0] = '\0'; 2223 } 2224 zc.zc_cookie = STDOUT_FILENO; 2225 2226 ret = ioctl(zfs_fd, ZFS_IOC_SENDBACKUP, &zc); 2227 if (ret != 0) { 2228 switch (errno) { 2229 case EPERM: 2230 /* 2231 * User doesn't have permission to do a backup 2232 */ 2233 zfs_error(dgettext(TEXT_DOMAIN, "cannot backup '%s': " 2234 "permission denied"), zhp_to->zfs_name); 2235 break; 2236 2237 case EXDEV: 2238 zfs_error(dgettext(TEXT_DOMAIN, 2239 "cannot do incremental backup from %s:\n" 2240 "it is not an earlier snapshot from the " 2241 "same fs as %s"), 2242 zhp_from->zfs_name, zhp_to->zfs_name); 2243 break; 2244 2245 case ENOENT: 2246 /* 2247 * Shouldn't happen because we verified the parent 2248 * above. But there may be a race condition where it 2249 * has since been removed. 2250 */ 2251 zfs_error(dgettext(TEXT_DOMAIN, "cannot open: " 2252 "no such snapshot")); 2253 break; 2254 2255 case EDQUOT: 2256 case EFBIG: 2257 case EIO: 2258 case ENOLINK: 2259 case ENOSPC: 2260 case ENOSTR: 2261 case ENXIO: 2262 case EPIPE: 2263 case ERANGE: 2264 case EFAULT: 2265 case EROFS: 2266 zfs_error(dgettext(TEXT_DOMAIN, 2267 "cannot write backup stream: %s"), 2268 strerror(errno)); 2269 break; 2270 2271 case EINTR: 2272 zfs_error(dgettext(TEXT_DOMAIN, 2273 "backup failed: signal recieved")); 2274 break; 2275 2276 default: 2277 zfs_baderror(errno); 2278 } 2279 } 2280 2281 return (ret); 2282 } 2283 2284 /* 2285 * Restores a backup of tosnap from stdin. 2286 */ 2287 int 2288 zfs_restore(const char *tosnap, int isprefix, int verbose, int dryrun) 2289 { 2290 zfs_cmd_t zc = { 0 }; 2291 time_t begin_time; 2292 int ioctl_err, err, bytes, size; 2293 char *cp; 2294 dmu_replay_record_t drr; 2295 struct drr_begin *drrb = &zc.zc_begin_record; 2296 2297 begin_time = time(NULL); 2298 2299 /* trim off snapname, if any */ 2300 (void) strcpy(zc.zc_name, tosnap); 2301 cp = strchr(zc.zc_name, '@'); 2302 if (cp) 2303 *cp = '\0'; 2304 2305 /* read in the BEGIN record */ 2306 cp = (char *)&drr; 2307 bytes = 0; 2308 do { 2309 size = read(STDIN_FILENO, cp, sizeof (drr) - bytes); 2310 cp += size; 2311 bytes += size; 2312 } while (size > 0); 2313 2314 if (size < 0 || bytes != sizeof (drr)) { 2315 zfs_error(dgettext(TEXT_DOMAIN, 2316 "cannot restore: invalid backup stream " 2317 "(couldn't read first record)")); 2318 return (-1); 2319 } 2320 2321 zc.zc_begin_record = drr.drr_u.drr_begin; 2322 2323 if (drrb->drr_magic != DMU_BACKUP_MAGIC && 2324 drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 2325 zfs_error(dgettext(TEXT_DOMAIN, 2326 "cannot restore: invalid backup stream " 2327 "(invalid magic number)")); 2328 return (-1); 2329 } 2330 2331 if (drrb->drr_version != DMU_BACKUP_VERSION && 2332 drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 2333 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 2334 drrb->drr_version = BSWAP_64(drrb->drr_version); 2335 zfs_error(dgettext(TEXT_DOMAIN, 2336 "cannot restore: only backup version 0x%llx is supported, " 2337 "stream is version %llx."), 2338 DMU_BACKUP_VERSION, drrb->drr_version); 2339 return (-1); 2340 } 2341 2342 /* 2343 * Determine name of destination snapshot. 2344 */ 2345 (void) strcpy(drrb->drr_toname, tosnap); 2346 if (isprefix) { 2347 if (strchr(tosnap, '@') != NULL) { 2348 zfs_error(dgettext(TEXT_DOMAIN, 2349 "cannot restore: " 2350 "argument to -d must be a filesystem")); 2351 return (-1); 2352 } 2353 2354 cp = strchr(drr.drr_u.drr_begin.drr_toname, '/'); 2355 if (cp == NULL) 2356 cp = drr.drr_u.drr_begin.drr_toname; 2357 else 2358 cp++; 2359 2360 (void) strcat(drrb->drr_toname, "/"); 2361 (void) strcat(drrb->drr_toname, cp); 2362 } else if (strchr(tosnap, '@') == NULL) { 2363 /* 2364 * they specified just a filesystem; tack on the 2365 * snapname from the backup. 2366 */ 2367 cp = strchr(drr.drr_u.drr_begin.drr_toname, '@'); 2368 if (cp == NULL || strlen(tosnap) + strlen(cp) >= MAXNAMELEN) { 2369 zfs_error(dgettext(TEXT_DOMAIN, 2370 "cannot restore: invalid backup stream " 2371 "(invalid snapshot name)")); 2372 return (-1); 2373 } 2374 (void) strcat(drrb->drr_toname, cp); 2375 } 2376 2377 if (drrb->drr_fromguid) { 2378 zfs_handle_t *h; 2379 /* incremental backup stream */ 2380 2381 /* do the ioctl to the containing fs */ 2382 (void) strcpy(zc.zc_name, drrb->drr_toname); 2383 cp = strchr(zc.zc_name, '@'); 2384 *cp = '\0'; 2385 2386 /* make sure destination fs exists */ 2387 h = zfs_open(zc.zc_name, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 2388 if (h == NULL) { 2389 zfs_error(dgettext(TEXT_DOMAIN, 2390 "cannot restore incrememtal backup: destination\n" 2391 "filesystem %s does not exist"), 2392 zc.zc_name); 2393 return (-1); 2394 } 2395 if (!dryrun) { 2396 /* unmount destination fs or remove device link. */ 2397 if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 2398 (void) zfs_unmount(h, NULL, 0); 2399 } else { 2400 (void) zvol_remove_link(h->zfs_name); 2401 } 2402 } 2403 zfs_close(h); 2404 } else { 2405 /* full backup stream */ 2406 2407 (void) strcpy(zc.zc_name, drrb->drr_toname); 2408 2409 /* make sure they aren't trying to restore into the root */ 2410 if (strchr(zc.zc_name, '/') == NULL) { 2411 cp = strchr(zc.zc_name, '@'); 2412 if (cp) 2413 *cp = '\0'; 2414 zfs_error(dgettext(TEXT_DOMAIN, 2415 "cannot restore: destination fs %s already exists"), 2416 zc.zc_name); 2417 return (-1); 2418 } 2419 2420 if (isprefix) { 2421 zfs_handle_t *h; 2422 2423 /* make sure prefix exists */ 2424 h = zfs_open(tosnap, ZFS_TYPE_FILESYSTEM | 2425 ZFS_TYPE_VOLUME); 2426 if (h == NULL) { 2427 zfs_error(dgettext(TEXT_DOMAIN, 2428 "cannot restore: " 2429 "filesystem %s does not exist"), 2430 tosnap); 2431 return (-1); 2432 } 2433 2434 /* create any necessary ancestors up to prefix */ 2435 zc.zc_objset_type = DMU_OST_ZFS; 2436 /* 2437 * zc.zc_name is now the full name of the snap 2438 * we're restoring into 2439 */ 2440 cp = zc.zc_name + strlen(tosnap) + 1; 2441 while (cp = strchr(cp, '/')) { 2442 *cp = '\0'; 2443 err = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc); 2444 if (err && errno != ENOENT && errno != EEXIST) { 2445 zfs_error(dgettext(TEXT_DOMAIN, 2446 "cannot restore: " 2447 "couldn't create ancestor %s"), 2448 zc.zc_name); 2449 return (-1); 2450 } 2451 *cp = '/'; 2452 cp++; 2453 } 2454 } 2455 2456 /* Make sure destination fs does not exist */ 2457 cp = strchr(zc.zc_name, '@'); 2458 *cp = '\0'; 2459 if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0) { 2460 zfs_error(dgettext(TEXT_DOMAIN, 2461 "cannot restore full backup: " 2462 "destination filesystem %s already exists"), 2463 zc.zc_name); 2464 return (-1); 2465 } 2466 2467 /* Do the recvbackup ioctl to the fs's parent. */ 2468 cp = strrchr(zc.zc_name, '/'); 2469 *cp = '\0'; 2470 } 2471 2472 (void) strcpy(zc.zc_prop_value, tosnap); 2473 zc.zc_cookie = STDIN_FILENO; 2474 zc.zc_intsz = isprefix; 2475 if (verbose) { 2476 (void) printf("%s %s backup of %s into %s\n", 2477 dryrun ? "would restore" : "restoring", 2478 drrb->drr_fromguid ? "incremental" : "full", 2479 drr.drr_u.drr_begin.drr_toname, 2480 zc.zc_begin_record.drr_toname); 2481 (void) fflush(stdout); 2482 } 2483 if (dryrun) 2484 return (0); 2485 err = ioctl_err = ioctl(zfs_fd, ZFS_IOC_RECVBACKUP, &zc); 2486 if (ioctl_err != 0) { 2487 switch (errno) { 2488 case ENODEV: 2489 zfs_error(dgettext(TEXT_DOMAIN, 2490 "cannot restore: " 2491 "most recent snapshot does not " 2492 "match incremental backup source")); 2493 break; 2494 case ETXTBSY: 2495 zfs_error(dgettext(TEXT_DOMAIN, 2496 "cannot restore: " 2497 "destination has been modified since " 2498 "most recent snapshot --\n" 2499 "use 'zfs rollback' to discard changes")); 2500 break; 2501 case EEXIST: 2502 if (drrb->drr_fromguid == 0) { 2503 /* it's the containing fs that exists */ 2504 cp = strchr(drrb->drr_toname, '@'); 2505 *cp = '\0'; 2506 } 2507 zfs_error(dgettext(TEXT_DOMAIN, 2508 "cannot restore to %s: destination already exists"), 2509 drrb->drr_toname); 2510 break; 2511 case ENOENT: 2512 zfs_error(dgettext(TEXT_DOMAIN, 2513 "cannot restore: destination does not exist")); 2514 break; 2515 case EBUSY: 2516 zfs_error(dgettext(TEXT_DOMAIN, 2517 "cannot restore: destination is in use")); 2518 break; 2519 case ENOSPC: 2520 zfs_error(dgettext(TEXT_DOMAIN, 2521 "cannot restore: out of space")); 2522 break; 2523 case EDQUOT: 2524 zfs_error(dgettext(TEXT_DOMAIN, 2525 "cannot restore: quota exceeded")); 2526 break; 2527 case EINTR: 2528 zfs_error(dgettext(TEXT_DOMAIN, 2529 "restore failed: signal recieved")); 2530 break; 2531 case EINVAL: 2532 zfs_error(dgettext(TEXT_DOMAIN, 2533 "cannot restore: invalid backup stream")); 2534 break; 2535 case EPERM: 2536 zfs_error(dgettext(TEXT_DOMAIN, 2537 "cannot restore: permission denied")); 2538 break; 2539 default: 2540 zfs_baderror(errno); 2541 } 2542 } 2543 2544 /* 2545 * Mount or recreate the /dev links for the target filesystem 2546 * (if created, or if we tore them down to do an incremental 2547 * restore), and the /dev links for the new snapshot (if 2548 * created). 2549 */ 2550 cp = strchr(drrb->drr_toname, '@'); 2551 if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 2552 zfs_handle_t *h; 2553 2554 *cp = '\0'; 2555 h = zfs_open(drrb->drr_toname, 2556 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 2557 *cp = '@'; 2558 if (h) { 2559 if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 2560 err = zfs_mount(h, NULL, 0); 2561 } else { 2562 err = zvol_create_link(h->zfs_name); 2563 if (err == 0 && ioctl_err == 0) { 2564 err = 2565 zvol_create_link(drrb->drr_toname); 2566 } 2567 } 2568 zfs_close(h); 2569 } 2570 } 2571 2572 if (err || ioctl_err) 2573 return (-1); 2574 2575 if (verbose) { 2576 char buf1[64]; 2577 char buf2[64]; 2578 uint64_t bytes = zc.zc_cookie; 2579 time_t delta = time(NULL) - begin_time; 2580 if (delta == 0) 2581 delta = 1; 2582 zfs_nicenum(bytes, buf1, sizeof (buf1)); 2583 zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 2584 2585 (void) printf("restored %sb backup in %lu seconds (%sb/sec)\n", 2586 buf1, delta, buf2); 2587 } 2588 return (0); 2589 } 2590 2591 /* 2592 * Rollback the given dataset to the previous snapshot. It is up to the caller 2593 * to verify that there is a previous snapshot available. 2594 */ 2595 int 2596 zfs_rollback(zfs_handle_t *zhp) 2597 { 2598 int ret; 2599 zfs_cmd_t zc = { 0 }; 2600 2601 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 2602 zhp->zfs_type == ZFS_TYPE_VOLUME); 2603 2604 if (zhp->zfs_type == ZFS_TYPE_VOLUME && 2605 zvol_remove_link(zhp->zfs_name) != 0) 2606 return (-1); 2607 2608 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2609 2610 if (zhp->zfs_volblocksize != 0) 2611 zc.zc_objset_type = DMU_OST_ZVOL; 2612 else 2613 zc.zc_objset_type = DMU_OST_ZFS; 2614 2615 /* 2616 * We rely on the consumer to verify that there are no newer snapshots 2617 * for the given dataset. Given these constraints, we can simply pass 2618 * the name on to the ioctl() call. There is still an unlikely race 2619 * condition where the user has taken a snapshot since we verified that 2620 * this was the most recent. 2621 */ 2622 if ((ret = ioctl(zfs_fd, ZFS_IOC_ROLLBACK, &zc)) != 0) { 2623 switch (errno) { 2624 case EPERM: 2625 /* 2626 * The user doesn't have permission to rollback the 2627 * given dataset. 2628 */ 2629 zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': " 2630 "permission denied"), zhp->zfs_name); 2631 break; 2632 2633 case EDQUOT: 2634 case ENOSPC: 2635 /* 2636 * The parent dataset doesn't have enough space to 2637 * rollback to the last snapshot. 2638 */ 2639 { 2640 char parent[ZFS_MAXNAMELEN]; 2641 (void) parent_name(zhp->zfs_name, parent, 2642 sizeof (parent)); 2643 zfs_error(dgettext(TEXT_DOMAIN, "cannot " 2644 "rollback '%s': out of space"), parent); 2645 } 2646 break; 2647 2648 case ENOENT: 2649 /* 2650 * The dataset doesn't exist. This shouldn't happen 2651 * except in race conditions. 2652 */ 2653 zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': " 2654 "no such %s"), zhp->zfs_name, 2655 zfs_type_to_name(zhp->zfs_type)); 2656 break; 2657 2658 case EBUSY: 2659 /* 2660 * The filesystem is busy. This should have been caught 2661 * by the caller before getting here, but there may be 2662 * an unexpected problem. 2663 */ 2664 zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': " 2665 "%s is busy"), zhp->zfs_name, 2666 zfs_type_to_name(zhp->zfs_type)); 2667 break; 2668 2669 default: 2670 zfs_baderror(errno); 2671 } 2672 } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 2673 ret = zvol_create_link(zhp->zfs_name); 2674 } 2675 2676 return (ret); 2677 } 2678 2679 /* 2680 * Iterate over all dependents for a given dataset. This includes both 2681 * hierarchical dependents (children) and data dependents (snapshots and 2682 * clones). The bulk of the processing occurs in get_dependents() in 2683 * libzfs_graph.c. 2684 */ 2685 int 2686 zfs_iter_dependents(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2687 { 2688 char **dependents; 2689 size_t count; 2690 int i; 2691 zfs_handle_t *child; 2692 int ret = 0; 2693 2694 dependents = get_dependents(zhp->zfs_name, &count); 2695 for (i = 0; i < count; i++) { 2696 if ((child = make_dataset_handle(dependents[i])) == NULL) 2697 continue; 2698 2699 if ((ret = func(child, data)) != 0) 2700 break; 2701 } 2702 2703 for (i = 0; i < count; i++) 2704 free(dependents[i]); 2705 free(dependents); 2706 2707 return (ret); 2708 } 2709 2710 /* 2711 * Renames the given dataset. 2712 */ 2713 int 2714 zfs_rename(zfs_handle_t *zhp, const char *target) 2715 { 2716 int ret; 2717 zfs_cmd_t zc = { 0 }; 2718 char reason[64]; 2719 char *delim; 2720 prop_changelist_t *cl; 2721 char parent[ZFS_MAXNAMELEN]; 2722 2723 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2724 (void) strlcpy(zc.zc_prop_value, target, sizeof (zc.zc_prop_value)); 2725 2726 /* if we have the same exact name, just return success */ 2727 if (strcmp(zhp->zfs_name, target) == 0) 2728 return (0); 2729 2730 /* 2731 * Make sure the target name is valid 2732 */ 2733 if (!zfs_validate_name(target, zhp->zfs_type, reason, 2734 sizeof (reason))) { 2735 zfs_error(dgettext(TEXT_DOMAIN, 2736 "cannot create '%s': %s in %s name"), target, reason, 2737 zfs_type_to_name(zhp->zfs_type)); 2738 return (-1); 2739 } 2740 2741 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 2742 if ((delim = strchr(target, '@')) == NULL) { 2743 zfs_error(dgettext(TEXT_DOMAIN, 2744 "cannot rename to '%s': not a snapshot"), target); 2745 return (-1); 2746 } 2747 2748 /* 2749 * Make sure we're renaming within the same dataset. 2750 */ 2751 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 2752 zhp->zfs_name[delim - target] != '@') { 2753 zfs_error(dgettext(TEXT_DOMAIN, 2754 "cannot rename to '%s': snapshots must be part " 2755 "of same dataset"), target); 2756 return (-1); 2757 } 2758 2759 (void) strncpy(parent, target, delim - target); 2760 parent[delim - target] = '\0'; 2761 } else { 2762 /* validate parents */ 2763 if (check_parents(target, zhp->zfs_type) != 0) 2764 return (-1); 2765 2766 (void) parent_name(target, parent, sizeof (parent)); 2767 2768 /* make sure we're in the same pool */ 2769 verify((delim = strchr(target, '/')) != NULL); 2770 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 2771 zhp->zfs_name[delim - target] != '/') { 2772 zfs_error(dgettext(TEXT_DOMAIN, 2773 "cannot rename to '%s': " 2774 "datasets must be within same pool"), target); 2775 return (-1); 2776 } 2777 } 2778 2779 if (getzoneid() == GLOBAL_ZONEID && 2780 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 2781 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename %s, " 2782 "dataset is used in a non-global zone"), zhp->zfs_name); 2783 return (-1); 2784 } 2785 2786 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 2787 return (1); 2788 2789 if (changelist_haszonedchild(cl)) { 2790 zfs_error(dgettext(TEXT_DOMAIN, 2791 "cannot rename '%s': child dataset with inherited " 2792 "mountpoint is used in a non-global zone"), zhp->zfs_name); 2793 ret = -1; 2794 goto error; 2795 } 2796 2797 if ((ret = changelist_prefix(cl)) != 0) 2798 goto error; 2799 2800 if (zhp->zfs_volblocksize != 0) 2801 zc.zc_objset_type = DMU_OST_ZVOL; 2802 else 2803 zc.zc_objset_type = DMU_OST_ZFS; 2804 2805 if ((ret = ioctl(zfs_fd, ZFS_IOC_RENAME, &zc)) != 0) { 2806 switch (errno) { 2807 case EPERM: 2808 /* 2809 * The user doesn't have permission to rename the 2810 * given dataset. 2811 */ 2812 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s': " 2813 "permission denied"), zhp->zfs_name); 2814 break; 2815 2816 case EDQUOT: 2817 case ENOSPC: 2818 /* 2819 * Not enough space in the parent dataset. 2820 */ 2821 zfs_error(dgettext(TEXT_DOMAIN, "cannot " 2822 "rename '%s': not enough space in '%s'"), 2823 zhp->zfs_name, parent); 2824 break; 2825 2826 case ENOENT: 2827 /* 2828 * The destination doesn't exist. 2829 */ 2830 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s' " 2831 "to '%s': destination doesn't exist"), 2832 zhp->zfs_name, target); 2833 break; 2834 2835 case EEXIST: 2836 /* 2837 * The destination already exists. 2838 */ 2839 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s' " 2840 "to '%s': destination already exists"), 2841 zhp->zfs_name, target); 2842 break; 2843 2844 case EBUSY: 2845 /* 2846 * The filesystem is busy. This should have been caught 2847 * by the caller before getting here, but there may be 2848 * an unexpected problem. 2849 */ 2850 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s': " 2851 "%s is busy"), zhp->zfs_name, 2852 zfs_type_to_name(zhp->zfs_type)); 2853 break; 2854 2855 default: 2856 zfs_baderror(errno); 2857 } 2858 2859 /* 2860 * On failure, we still want to remount any filesystems that 2861 * were previously mounted, so we don't alter the system state. 2862 */ 2863 (void) changelist_postfix(cl); 2864 } else { 2865 changelist_rename(cl, zfs_get_name(zhp), target); 2866 2867 ret = changelist_postfix(cl); 2868 } 2869 2870 error: 2871 changelist_free(cl); 2872 return (ret); 2873 } 2874 2875 /* 2876 * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 2877 * poke devfsadm to create the /dev link, and then wait for the link to appear. 2878 */ 2879 int 2880 zvol_create_link(const char *dataset) 2881 { 2882 zfs_cmd_t zc = { 0 }; 2883 di_devlink_handle_t hdl; 2884 2885 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 2886 2887 /* 2888 * Issue the appropriate ioctl. 2889 */ 2890 if (ioctl(zfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 2891 switch (errno) { 2892 case EPERM: 2893 zfs_error(dgettext(TEXT_DOMAIN, "cannot create " 2894 "device links for '%s': permission denied"), 2895 dataset); 2896 break; 2897 2898 case EEXIST: 2899 /* 2900 * Silently ignore the case where the link already 2901 * exists. This allows 'zfs volinit' to be run multiple 2902 * times without errors. 2903 */ 2904 return (0); 2905 2906 default: 2907 zfs_baderror(errno); 2908 } 2909 2910 return (-1); 2911 } 2912 2913 /* 2914 * Call devfsadm and wait for the links to magically appear. 2915 */ 2916 if ((hdl = di_devlink_init(ZFS_DRIVER, DI_MAKE_LINK)) == NULL) { 2917 zfs_error(dgettext(TEXT_DOMAIN, 2918 "cannot create device links for '%s'"), dataset); 2919 (void) ioctl(zfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 2920 return (-1); 2921 } else { 2922 (void) di_devlink_fini(&hdl); 2923 } 2924 2925 return (0); 2926 } 2927 2928 /* 2929 * Remove a minor node for the given zvol and the associated /dev links. 2930 */ 2931 int 2932 zvol_remove_link(const char *dataset) 2933 { 2934 zfs_cmd_t zc = { 0 }; 2935 2936 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 2937 2938 if (ioctl(zfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 2939 switch (errno) { 2940 case EPERM: 2941 zfs_error(dgettext(TEXT_DOMAIN, "cannot remove " 2942 "device links for '%s': permission denied"), 2943 dataset); 2944 break; 2945 2946 case EBUSY: 2947 zfs_error(dgettext(TEXT_DOMAIN, "cannot remove " 2948 "device links for '%s': volume is in use"), 2949 dataset); 2950 break; 2951 2952 case ENXIO: 2953 /* 2954 * Silently ignore the case where the link no longer 2955 * exists, so that 'zfs volfini' can be run multiple 2956 * times without errors. 2957 */ 2958 return (0); 2959 2960 default: 2961 zfs_baderror(errno); 2962 } 2963 2964 return (-1); 2965 } 2966 2967 return (0); 2968 } 2969