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