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 if (h == NULL) { 2499 zfs_error(dgettext(TEXT_DOMAIN, 2500 "cannot restore: " 2501 "%s is an invalid destination"), 2502 tosnap); 2503 return (-1); 2504 } 2505 zfs_close(h); 2506 2507 /* create any necessary ancestors up to prefix */ 2508 zc.zc_objset_type = DMU_OST_ZFS; 2509 2510 /* 2511 * zc.zc_name is now the full name of the snap 2512 * we're restoring into. Attempt to create, 2513 * mount, and share any ancestor filesystems, up 2514 * to the one that was named. 2515 */ 2516 for (cp = zc.zc_name + strlen(tosnap) + 1; 2517 cp = strchr(cp, '/'); *cp = '/', cp++) { 2518 const char *opname; 2519 *cp = '\0'; 2520 2521 opname = "create"; 2522 if (zfs_create(zc.zc_name, ZFS_TYPE_FILESYSTEM, 2523 NULL, NULL) != 0) { 2524 if (errno == EEXIST) 2525 continue; 2526 goto ancestorerr; 2527 } 2528 2529 opname = "open"; 2530 h = zfs_open(zc.zc_name, ZFS_TYPE_FILESYSTEM); 2531 if (h == NULL) 2532 goto ancestorerr; 2533 2534 opname = "mount"; 2535 if (zfs_mount(h, NULL, 0) != 0) 2536 goto ancestorerr; 2537 2538 opname = "share"; 2539 if (zfs_share(h) != 0) 2540 goto ancestorerr; 2541 2542 zfs_close(h); 2543 2544 continue; 2545 ancestorerr: 2546 zfs_error(dgettext(TEXT_DOMAIN, 2547 "cannot restore: couldn't %s ancestor %s"), 2548 opname, zc.zc_name); 2549 return (-1); 2550 } 2551 } 2552 2553 /* Make sure destination fs does not exist */ 2554 cp = strchr(zc.zc_name, '@'); 2555 *cp = '\0'; 2556 if (zfs_ioctl(ZFS_IOC_OBJSET_STATS, &zc) == 0) { 2557 zfs_error(dgettext(TEXT_DOMAIN, 2558 "cannot restore full backup: " 2559 "destination filesystem %s already exists"), 2560 zc.zc_name); 2561 return (-1); 2562 } 2563 2564 /* Do the recvbackup ioctl to the fs's parent. */ 2565 cp = strrchr(zc.zc_name, '/'); 2566 *cp = '\0'; 2567 } 2568 2569 (void) strcpy(zc.zc_prop_value, tosnap); 2570 zc.zc_cookie = STDIN_FILENO; 2571 zc.zc_intsz = isprefix; 2572 if (verbose) { 2573 (void) printf("%s %s backup of %s into %s\n", 2574 dryrun ? "would restore" : "restoring", 2575 drrb->drr_fromguid ? "incremental" : "full", 2576 drr.drr_u.drr_begin.drr_toname, 2577 zc.zc_filename); 2578 (void) fflush(stdout); 2579 } 2580 if (dryrun) 2581 return (0); 2582 err = ioctl_err = zfs_ioctl(ZFS_IOC_RECVBACKUP, &zc); 2583 if (ioctl_err != 0) { 2584 switch (errno) { 2585 case ENODEV: 2586 zfs_error(dgettext(TEXT_DOMAIN, 2587 "cannot restore: " 2588 "most recent snapshot does not " 2589 "match incremental backup source")); 2590 break; 2591 case ETXTBSY: 2592 zfs_error(dgettext(TEXT_DOMAIN, 2593 "cannot restore: " 2594 "destination has been modified since " 2595 "most recent snapshot --\n" 2596 "use 'zfs rollback' to discard changes")); 2597 break; 2598 case EEXIST: 2599 if (drrb->drr_fromguid == 0) { 2600 /* it's the containing fs that exists */ 2601 cp = strchr(zc.zc_filename, '@'); 2602 *cp = '\0'; 2603 } 2604 zfs_error(dgettext(TEXT_DOMAIN, 2605 "cannot restore to %s: destination already exists"), 2606 zc.zc_filename); 2607 break; 2608 case ENOENT: 2609 zfs_error(dgettext(TEXT_DOMAIN, 2610 "cannot restore: destination does not exist")); 2611 break; 2612 case EBUSY: 2613 zfs_error(dgettext(TEXT_DOMAIN, 2614 "cannot restore: destination is in use")); 2615 break; 2616 case ENOSPC: 2617 zfs_error(dgettext(TEXT_DOMAIN, 2618 "cannot restore: out of space")); 2619 break; 2620 case EDQUOT: 2621 zfs_error(dgettext(TEXT_DOMAIN, 2622 "cannot restore: quota exceeded")); 2623 break; 2624 case EINTR: 2625 zfs_error(dgettext(TEXT_DOMAIN, 2626 "restore failed: signal recieved")); 2627 break; 2628 case EINVAL: 2629 zfs_error(dgettext(TEXT_DOMAIN, 2630 "cannot restore: invalid backup stream")); 2631 break; 2632 case ECKSUM: 2633 zfs_error(dgettext(TEXT_DOMAIN, 2634 "cannot restore: invalid backup stream " 2635 "(checksum mismatch)")); 2636 break; 2637 case EPERM: 2638 zfs_error(dgettext(TEXT_DOMAIN, 2639 "cannot restore: permission denied")); 2640 break; 2641 default: 2642 zfs_baderror(errno); 2643 } 2644 } 2645 2646 /* 2647 * Mount or recreate the /dev links for the target filesystem 2648 * (if created, or if we tore them down to do an incremental 2649 * restore), and the /dev links for the new snapshot (if 2650 * created). 2651 */ 2652 cp = strchr(zc.zc_filename, '@'); 2653 if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 2654 zfs_handle_t *h; 2655 2656 *cp = '\0'; 2657 h = zfs_open(zc.zc_filename, 2658 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 2659 *cp = '@'; 2660 if (h) { 2661 if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 2662 err = zfs_mount(h, NULL, 0); 2663 } else { 2664 err = zvol_create_link(h->zfs_name); 2665 if (err == 0 && ioctl_err == 0) 2666 err = zvol_create_link(zc.zc_filename); 2667 } 2668 zfs_close(h); 2669 } 2670 } 2671 2672 if (err || ioctl_err) 2673 return (-1); 2674 2675 if (verbose) { 2676 char buf1[64]; 2677 char buf2[64]; 2678 uint64_t bytes = zc.zc_cookie; 2679 time_t delta = time(NULL) - begin_time; 2680 if (delta == 0) 2681 delta = 1; 2682 zfs_nicenum(bytes, buf1, sizeof (buf1)); 2683 zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 2684 2685 (void) printf("restored %sb backup in %lu seconds (%sb/sec)\n", 2686 buf1, delta, buf2); 2687 } 2688 return (0); 2689 } 2690 2691 /* 2692 * Destroy any more recent snapshots. We invoke this callback on any dependents 2693 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 2694 * is a dependent and we should just destroy it without checking the transaction 2695 * group. 2696 */ 2697 typedef struct rollback_data { 2698 const char *cb_target; /* the snapshot */ 2699 uint64_t cb_create; /* creation time reference */ 2700 prop_changelist_t *cb_clp; /* changelist pointer */ 2701 int cb_error; 2702 int cb_dependent; 2703 } rollback_data_t; 2704 2705 static int 2706 rollback_destroy(zfs_handle_t *zhp, void *data) 2707 { 2708 rollback_data_t *cbp = data; 2709 2710 if (!cbp->cb_dependent) { 2711 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 2712 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 2713 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 2714 cbp->cb_create) { 2715 2716 cbp->cb_dependent = TRUE; 2717 (void) zfs_iter_dependents(zhp, rollback_destroy, cbp); 2718 cbp->cb_dependent = FALSE; 2719 2720 if (zfs_destroy(zhp) != 0) 2721 cbp->cb_error = 1; 2722 else 2723 changelist_remove(zhp, cbp->cb_clp); 2724 } 2725 } else { 2726 if (zfs_destroy(zhp) != 0) 2727 cbp->cb_error = 1; 2728 else 2729 changelist_remove(zhp, cbp->cb_clp); 2730 } 2731 2732 zfs_close(zhp); 2733 return (0); 2734 } 2735 2736 /* 2737 * Rollback the dataset to its latest snapshot. 2738 */ 2739 static int 2740 do_rollback(zfs_handle_t *zhp) 2741 { 2742 int ret; 2743 zfs_cmd_t zc = { 0 }; 2744 2745 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 2746 zhp->zfs_type == ZFS_TYPE_VOLUME); 2747 2748 if (zhp->zfs_type == ZFS_TYPE_VOLUME && 2749 zvol_remove_link(zhp->zfs_name) != 0) 2750 return (-1); 2751 2752 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2753 2754 if (zhp->zfs_volblocksize != 0) 2755 zc.zc_objset_type = DMU_OST_ZVOL; 2756 else 2757 zc.zc_objset_type = DMU_OST_ZFS; 2758 2759 /* 2760 * We rely on the consumer to verify that there are no newer snapshots 2761 * for the given dataset. Given these constraints, we can simply pass 2762 * the name on to the ioctl() call. There is still an unlikely race 2763 * condition where the user has taken a snapshot since we verified that 2764 * this was the most recent. 2765 */ 2766 if ((ret = zfs_ioctl(ZFS_IOC_ROLLBACK, &zc)) != 0) { 2767 switch (errno) { 2768 case EPERM: 2769 /* 2770 * The user doesn't have permission to rollback the 2771 * given dataset. 2772 */ 2773 zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': " 2774 "permission denied"), zhp->zfs_name); 2775 break; 2776 2777 case EDQUOT: 2778 case ENOSPC: 2779 /* 2780 * The parent dataset doesn't have enough space to 2781 * rollback to the last snapshot. 2782 */ 2783 { 2784 char parent[ZFS_MAXNAMELEN]; 2785 (void) parent_name(zhp->zfs_name, parent, 2786 sizeof (parent)); 2787 zfs_error(dgettext(TEXT_DOMAIN, "cannot " 2788 "rollback '%s': out of space"), parent); 2789 } 2790 break; 2791 2792 case ENOENT: 2793 /* 2794 * The dataset doesn't exist. This shouldn't happen 2795 * except in race conditions. 2796 */ 2797 zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': " 2798 "no such %s"), zhp->zfs_name, 2799 zfs_type_to_name(zhp->zfs_type)); 2800 break; 2801 2802 case EBUSY: 2803 /* 2804 * The filesystem is busy. This should have been caught 2805 * by the caller before getting here, but there may be 2806 * an unexpected problem. 2807 */ 2808 zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': " 2809 "%s is busy"), zhp->zfs_name, 2810 zfs_type_to_name(zhp->zfs_type)); 2811 break; 2812 2813 default: 2814 zfs_baderror(errno); 2815 } 2816 } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 2817 ret = zvol_create_link(zhp->zfs_name); 2818 } 2819 2820 return (ret); 2821 } 2822 2823 /* 2824 * Given a dataset, rollback to a specific snapshot, discarding any 2825 * data changes since then and making it the active dataset. 2826 * 2827 * Any snapshots more recent than the target are destroyed, along with 2828 * their dependents. 2829 */ 2830 int 2831 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 2832 { 2833 int ret; 2834 rollback_data_t cb = { 0 }; 2835 prop_changelist_t *clp; 2836 2837 /* 2838 * Unmount all dependendents of the dataset and the dataset itself. 2839 * The list we need to gather is the same as for doing rename 2840 */ 2841 clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 2842 if (clp == NULL) 2843 return (-1); 2844 2845 if ((ret = changelist_prefix(clp)) != 0) 2846 goto out; 2847 2848 /* 2849 * Destroy all recent snapshots and its dependends. 2850 */ 2851 cb.cb_target = snap->zfs_name; 2852 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 2853 cb.cb_clp = clp; 2854 (void) zfs_iter_children(zhp, rollback_destroy, &cb); 2855 2856 if ((ret = cb.cb_error) != 0) { 2857 (void) changelist_postfix(clp); 2858 goto out; 2859 } 2860 2861 /* 2862 * Now that we have verified that the snapshot is the latest, 2863 * rollback to the given snapshot. 2864 */ 2865 ret = do_rollback(zhp); 2866 2867 if (ret != 0) { 2868 (void) changelist_postfix(clp); 2869 goto out; 2870 } 2871 2872 /* 2873 * We only want to re-mount the filesystem if it was mounted in the 2874 * first place. 2875 */ 2876 ret = changelist_postfix(clp); 2877 2878 out: 2879 changelist_free(clp); 2880 return (ret); 2881 } 2882 2883 /* 2884 * Iterate over all dependents for a given dataset. This includes both 2885 * hierarchical dependents (children) and data dependents (snapshots and 2886 * clones). The bulk of the processing occurs in get_dependents() in 2887 * libzfs_graph.c. 2888 */ 2889 int 2890 zfs_iter_dependents(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2891 { 2892 char **dependents; 2893 size_t count; 2894 int i; 2895 zfs_handle_t *child; 2896 int ret = 0; 2897 2898 dependents = get_dependents(zhp->zfs_name, &count); 2899 for (i = 0; i < count; i++) { 2900 if ((child = make_dataset_handle(dependents[i])) == NULL) 2901 continue; 2902 2903 if ((ret = func(child, data)) != 0) 2904 break; 2905 } 2906 2907 for (i = 0; i < count; i++) 2908 free(dependents[i]); 2909 free(dependents); 2910 2911 return (ret); 2912 } 2913 2914 /* 2915 * Renames the given dataset. 2916 */ 2917 int 2918 zfs_rename(zfs_handle_t *zhp, const char *target) 2919 { 2920 int ret; 2921 zfs_cmd_t zc = { 0 }; 2922 char reason[64]; 2923 char *delim; 2924 prop_changelist_t *cl; 2925 char parent[ZFS_MAXNAMELEN]; 2926 2927 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2928 (void) strlcpy(zc.zc_prop_value, target, sizeof (zc.zc_prop_value)); 2929 2930 /* if we have the same exact name, just return success */ 2931 if (strcmp(zhp->zfs_name, target) == 0) 2932 return (0); 2933 2934 /* 2935 * Make sure the target name is valid 2936 */ 2937 if (!zfs_validate_name(target, zhp->zfs_type, reason, 2938 sizeof (reason))) { 2939 zfs_error(dgettext(TEXT_DOMAIN, 2940 "cannot create '%s': %s in %s name"), target, reason, 2941 zfs_type_to_name(zhp->zfs_type)); 2942 return (-1); 2943 } 2944 2945 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 2946 if ((delim = strchr(target, '@')) == NULL) { 2947 zfs_error(dgettext(TEXT_DOMAIN, 2948 "cannot rename to '%s': not a snapshot"), target); 2949 return (-1); 2950 } 2951 2952 /* 2953 * Make sure we're renaming within the same dataset. 2954 */ 2955 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 2956 zhp->zfs_name[delim - target] != '@') { 2957 zfs_error(dgettext(TEXT_DOMAIN, 2958 "cannot rename to '%s': snapshots must be part " 2959 "of same dataset"), target); 2960 return (-1); 2961 } 2962 2963 (void) strncpy(parent, target, delim - target); 2964 parent[delim - target] = '\0'; 2965 } else { 2966 /* validate parents */ 2967 if (check_parents(target, zhp->zfs_type) != 0) 2968 return (-1); 2969 2970 (void) parent_name(target, parent, sizeof (parent)); 2971 2972 /* make sure we're in the same pool */ 2973 verify((delim = strchr(target, '/')) != NULL); 2974 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 2975 zhp->zfs_name[delim - target] != '/') { 2976 zfs_error(dgettext(TEXT_DOMAIN, 2977 "cannot rename to '%s': " 2978 "datasets must be within same pool"), target); 2979 return (-1); 2980 } 2981 } 2982 2983 if (getzoneid() == GLOBAL_ZONEID && 2984 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 2985 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename %s, " 2986 "dataset is used in a non-global zone"), zhp->zfs_name); 2987 return (-1); 2988 } 2989 2990 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 2991 return (1); 2992 2993 if (changelist_haszonedchild(cl)) { 2994 zfs_error(dgettext(TEXT_DOMAIN, 2995 "cannot rename '%s': child dataset with inherited " 2996 "mountpoint is used in a non-global zone"), zhp->zfs_name); 2997 ret = -1; 2998 goto error; 2999 } 3000 3001 if ((ret = changelist_prefix(cl)) != 0) 3002 goto error; 3003 3004 if (zhp->zfs_volblocksize != 0) 3005 zc.zc_objset_type = DMU_OST_ZVOL; 3006 else 3007 zc.zc_objset_type = DMU_OST_ZFS; 3008 3009 if ((ret = zfs_ioctl(ZFS_IOC_RENAME, &zc)) != 0) { 3010 switch (errno) { 3011 case EPERM: 3012 /* 3013 * The user doesn't have permission to rename the 3014 * given dataset. 3015 */ 3016 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s': " 3017 "permission denied"), zhp->zfs_name); 3018 break; 3019 3020 case EDQUOT: 3021 case ENOSPC: 3022 /* 3023 * Not enough space in the parent dataset. 3024 */ 3025 zfs_error(dgettext(TEXT_DOMAIN, "cannot " 3026 "rename '%s': not enough space in '%s'"), 3027 zhp->zfs_name, parent); 3028 break; 3029 3030 case ENOENT: 3031 /* 3032 * The destination doesn't exist. 3033 */ 3034 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s' " 3035 "to '%s': destination doesn't exist"), 3036 zhp->zfs_name, target); 3037 break; 3038 3039 case EEXIST: 3040 /* 3041 * The destination already exists. 3042 */ 3043 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s' " 3044 "to '%s': destination already exists"), 3045 zhp->zfs_name, target); 3046 break; 3047 3048 case EBUSY: 3049 /* 3050 * The filesystem is busy. This should have been caught 3051 * by the caller before getting here, but there may be 3052 * an unexpected problem. 3053 */ 3054 zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s': " 3055 "%s is busy"), zhp->zfs_name, 3056 zfs_type_to_name(zhp->zfs_type)); 3057 break; 3058 3059 default: 3060 zfs_baderror(errno); 3061 } 3062 3063 /* 3064 * On failure, we still want to remount any filesystems that 3065 * were previously mounted, so we don't alter the system state. 3066 */ 3067 (void) changelist_postfix(cl); 3068 } else { 3069 changelist_rename(cl, zfs_get_name(zhp), target); 3070 3071 ret = changelist_postfix(cl); 3072 } 3073 3074 error: 3075 changelist_free(cl); 3076 return (ret); 3077 } 3078 3079 /* 3080 * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3081 * poke devfsadm to create the /dev link, and then wait for the link to appear. 3082 */ 3083 int 3084 zvol_create_link(const char *dataset) 3085 { 3086 zfs_cmd_t zc = { 0 }; 3087 di_devlink_handle_t hdl; 3088 3089 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3090 3091 /* 3092 * Issue the appropriate ioctl. 3093 */ 3094 if (zfs_ioctl(ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3095 switch (errno) { 3096 case EPERM: 3097 zfs_error(dgettext(TEXT_DOMAIN, "cannot create " 3098 "device links for '%s': permission denied"), 3099 dataset); 3100 break; 3101 3102 case EEXIST: 3103 /* 3104 * Silently ignore the case where the link already 3105 * exists. This allows 'zfs volinit' to be run multiple 3106 * times without errors. 3107 */ 3108 return (0); 3109 3110 default: 3111 zfs_baderror(errno); 3112 } 3113 3114 return (-1); 3115 } 3116 3117 /* 3118 * Call devfsadm and wait for the links to magically appear. 3119 */ 3120 if ((hdl = di_devlink_init(ZFS_DRIVER, DI_MAKE_LINK)) == NULL) { 3121 zfs_error(dgettext(TEXT_DOMAIN, 3122 "cannot create device links for '%s'"), dataset); 3123 (void) zfs_ioctl(ZFS_IOC_REMOVE_MINOR, &zc); 3124 return (-1); 3125 } else { 3126 (void) di_devlink_fini(&hdl); 3127 } 3128 3129 return (0); 3130 } 3131 3132 /* 3133 * Remove a minor node for the given zvol and the associated /dev links. 3134 */ 3135 int 3136 zvol_remove_link(const char *dataset) 3137 { 3138 zfs_cmd_t zc = { 0 }; 3139 3140 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3141 3142 if (zfs_ioctl(ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3143 switch (errno) { 3144 case EPERM: 3145 zfs_error(dgettext(TEXT_DOMAIN, "cannot remove " 3146 "device links for '%s': permission denied"), 3147 dataset); 3148 break; 3149 3150 case EBUSY: 3151 zfs_error(dgettext(TEXT_DOMAIN, "cannot remove " 3152 "device links for '%s': volume is in use"), 3153 dataset); 3154 break; 3155 3156 case ENXIO: 3157 /* 3158 * Silently ignore the case where the link no longer 3159 * exists, so that 'zfs volfini' can be run multiple 3160 * times without errors. 3161 */ 3162 return (0); 3163 3164 default: 3165 zfs_baderror(errno); 3166 } 3167 3168 return (-1); 3169 } 3170 3171 return (0); 3172 } 3173