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