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