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