1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 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 <stddef.h> 40 #include <zone.h> 41 #include <fcntl.h> 42 #include <sys/mntent.h> 43 #include <sys/mnttab.h> 44 #include <sys/mount.h> 45 #include <sys/avl.h> 46 #include <priv.h> 47 #include <pwd.h> 48 #include <grp.h> 49 #include <stddef.h> 50 #include <ucred.h> 51 52 #include <sys/spa.h> 53 #include <sys/zap.h> 54 #include <sys/zfs_i18n.h> 55 #include <libzfs.h> 56 57 #include "zfs_namecheck.h" 58 #include "zfs_prop.h" 59 #include "libzfs_impl.h" 60 #include "zfs_deleg.h" 61 62 static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 63 64 /* 65 * Given a single type (not a mask of types), return the type in a human 66 * readable form. 67 */ 68 const char * 69 zfs_type_to_name(zfs_type_t type) 70 { 71 switch (type) { 72 case ZFS_TYPE_FILESYSTEM: 73 return (dgettext(TEXT_DOMAIN, "filesystem")); 74 case ZFS_TYPE_SNAPSHOT: 75 return (dgettext(TEXT_DOMAIN, "snapshot")); 76 case ZFS_TYPE_VOLUME: 77 return (dgettext(TEXT_DOMAIN, "volume")); 78 } 79 80 return (NULL); 81 } 82 83 /* 84 * Given a path and mask of ZFS types, return a string describing this dataset. 85 * This is used when we fail to open a dataset and we cannot get an exact type. 86 * We guess what the type would have been based on the path and the mask of 87 * acceptable types. 88 */ 89 static const char * 90 path_to_str(const char *path, int types) 91 { 92 /* 93 * When given a single type, always report the exact type. 94 */ 95 if (types == ZFS_TYPE_SNAPSHOT) 96 return (dgettext(TEXT_DOMAIN, "snapshot")); 97 if (types == ZFS_TYPE_FILESYSTEM) 98 return (dgettext(TEXT_DOMAIN, "filesystem")); 99 if (types == ZFS_TYPE_VOLUME) 100 return (dgettext(TEXT_DOMAIN, "volume")); 101 102 /* 103 * The user is requesting more than one type of dataset. If this is the 104 * case, consult the path itself. If we're looking for a snapshot, and 105 * a '@' is found, then report it as "snapshot". Otherwise, remove the 106 * snapshot attribute and try again. 107 */ 108 if (types & ZFS_TYPE_SNAPSHOT) { 109 if (strchr(path, '@') != NULL) 110 return (dgettext(TEXT_DOMAIN, "snapshot")); 111 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112 } 113 114 115 /* 116 * The user has requested either filesystems or volumes. 117 * We have no way of knowing a priori what type this would be, so always 118 * report it as "filesystem" or "volume", our two primitive types. 119 */ 120 if (types & ZFS_TYPE_FILESYSTEM) 121 return (dgettext(TEXT_DOMAIN, "filesystem")); 122 123 assert(types & ZFS_TYPE_VOLUME); 124 return (dgettext(TEXT_DOMAIN, "volume")); 125 } 126 127 /* 128 * Validate a ZFS path. This is used even before trying to open the dataset, to 129 * provide a more meaningful error message. We place a more useful message in 130 * 'buf' detailing exactly why the name was not valid. 131 */ 132 static int 133 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 134 boolean_t modifying) 135 { 136 namecheck_err_t why; 137 char what; 138 139 if (dataset_namecheck(path, &why, &what) != 0) { 140 if (hdl != NULL) { 141 switch (why) { 142 case NAME_ERR_TOOLONG: 143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 144 "name is too long")); 145 break; 146 147 case NAME_ERR_LEADING_SLASH: 148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 149 "leading slash in name")); 150 break; 151 152 case NAME_ERR_EMPTY_COMPONENT: 153 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 154 "empty component in name")); 155 break; 156 157 case NAME_ERR_TRAILING_SLASH: 158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 159 "trailing slash in name")); 160 break; 161 162 case NAME_ERR_INVALCHAR: 163 zfs_error_aux(hdl, 164 dgettext(TEXT_DOMAIN, "invalid character " 165 "'%c' in name"), what); 166 break; 167 168 case NAME_ERR_MULTIPLE_AT: 169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 170 "multiple '@' delimiters in name")); 171 break; 172 173 case NAME_ERR_NOLETTER: 174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 175 "pool doesn't begin with a letter")); 176 break; 177 178 case NAME_ERR_RESERVED: 179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 180 "name is reserved")); 181 break; 182 183 case NAME_ERR_DISKLIKE: 184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 185 "reserved disk name")); 186 break; 187 } 188 } 189 190 return (0); 191 } 192 193 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 194 if (hdl != NULL) 195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 196 "snapshot delimiter '@' in filesystem name")); 197 return (0); 198 } 199 200 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 201 if (hdl != NULL) 202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 203 "missing '@' delimiter in snapshot name")); 204 return (0); 205 } 206 207 if (modifying && strchr(path, '%') != NULL) { 208 if (hdl != NULL) 209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 210 "invalid character %c in name"), '%'); 211 return (0); 212 } 213 214 return (-1); 215 } 216 217 int 218 zfs_name_valid(const char *name, zfs_type_t type) 219 { 220 return (zfs_validate_name(NULL, name, type, B_FALSE)); 221 } 222 223 /* 224 * This function takes the raw DSL properties, and filters out the user-defined 225 * properties into a separate nvlist. 226 */ 227 static nvlist_t * 228 process_user_props(zfs_handle_t *zhp, nvlist_t *props) 229 { 230 libzfs_handle_t *hdl = zhp->zfs_hdl; 231 nvpair_t *elem; 232 nvlist_t *propval; 233 nvlist_t *nvl; 234 235 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 236 (void) no_memory(hdl); 237 return (NULL); 238 } 239 240 elem = NULL; 241 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 242 if (!zfs_prop_user(nvpair_name(elem))) 243 continue; 244 245 verify(nvpair_value_nvlist(elem, &propval) == 0); 246 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 247 nvlist_free(nvl); 248 (void) no_memory(hdl); 249 return (NULL); 250 } 251 } 252 253 return (nvl); 254 } 255 256 /* 257 * Utility function to gather stats (objset and zpl) for the given object. 258 */ 259 static int 260 get_stats(zfs_handle_t *zhp) 261 { 262 zfs_cmd_t zc = { 0 }; 263 libzfs_handle_t *hdl = zhp->zfs_hdl; 264 nvlist_t *allprops, *userprops; 265 266 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 267 268 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 269 return (-1); 270 271 while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 272 if (errno == ENOMEM) { 273 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 274 zcmd_free_nvlists(&zc); 275 return (-1); 276 } 277 } else { 278 zcmd_free_nvlists(&zc); 279 return (-1); 280 } 281 } 282 283 zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 284 285 (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 286 287 if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 288 zcmd_free_nvlists(&zc); 289 return (-1); 290 } 291 292 zcmd_free_nvlists(&zc); 293 294 if ((userprops = process_user_props(zhp, allprops)) == NULL) { 295 nvlist_free(allprops); 296 return (-1); 297 } 298 299 nvlist_free(zhp->zfs_props); 300 nvlist_free(zhp->zfs_user_props); 301 302 zhp->zfs_props = allprops; 303 zhp->zfs_user_props = userprops; 304 305 return (0); 306 } 307 308 /* 309 * Refresh the properties currently stored in the handle. 310 */ 311 void 312 zfs_refresh_properties(zfs_handle_t *zhp) 313 { 314 (void) get_stats(zhp); 315 } 316 317 /* 318 * Makes a handle from the given dataset name. Used by zfs_open() and 319 * zfs_iter_* to create child handles on the fly. 320 */ 321 zfs_handle_t * 322 make_dataset_handle(libzfs_handle_t *hdl, const char *path) 323 { 324 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 325 char *logstr; 326 327 if (zhp == NULL) 328 return (NULL); 329 330 zhp->zfs_hdl = hdl; 331 332 /* 333 * Preserve history log string. 334 * any changes performed here will be 335 * logged as an internal event. 336 */ 337 logstr = zhp->zfs_hdl->libzfs_log_str; 338 zhp->zfs_hdl->libzfs_log_str = NULL; 339 top: 340 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 341 342 if (get_stats(zhp) != 0) { 343 zhp->zfs_hdl->libzfs_log_str = logstr; 344 free(zhp); 345 return (NULL); 346 } 347 348 if (zhp->zfs_dmustats.dds_inconsistent) { 349 zfs_cmd_t zc = { 0 }; 350 351 /* 352 * If it is dds_inconsistent, then we've caught it in 353 * the middle of a 'zfs receive' or 'zfs destroy', and 354 * it is inconsistent from the ZPL's point of view, so 355 * can't be mounted. However, it could also be that we 356 * have crashed in the middle of one of those 357 * operations, in which case we need to get rid of the 358 * inconsistent state. We do that by either rolling 359 * back to the previous snapshot (which will fail if 360 * there is none), or destroying the filesystem. Note 361 * that if we are still in the middle of an active 362 * 'receive' or 'destroy', then the rollback and destroy 363 * will fail with EBUSY and we will drive on as usual. 364 */ 365 366 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 367 368 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 369 (void) zvol_remove_link(hdl, zhp->zfs_name); 370 zc.zc_objset_type = DMU_OST_ZVOL; 371 } else { 372 zc.zc_objset_type = DMU_OST_ZFS; 373 } 374 375 /* 376 * If we can successfully destroy it, pretend that it 377 * never existed. 378 */ 379 if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 380 zhp->zfs_hdl->libzfs_log_str = logstr; 381 free(zhp); 382 errno = ENOENT; 383 return (NULL); 384 } 385 /* If we can successfully roll it back, reget the stats */ 386 if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 387 goto top; 388 } 389 390 /* 391 * We've managed to open the dataset and gather statistics. Determine 392 * the high-level type. 393 */ 394 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 395 zhp->zfs_head_type = ZFS_TYPE_VOLUME; 396 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 397 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 398 else 399 abort(); 400 401 if (zhp->zfs_dmustats.dds_is_snapshot) 402 zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 403 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 404 zhp->zfs_type = ZFS_TYPE_VOLUME; 405 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 406 zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 407 else 408 abort(); /* we should never see any other types */ 409 410 zhp->zfs_hdl->libzfs_log_str = logstr; 411 return (zhp); 412 } 413 414 /* 415 * Opens the given snapshot, filesystem, or volume. The 'types' 416 * argument is a mask of acceptable types. The function will print an 417 * appropriate error message and return NULL if it can't be opened. 418 */ 419 zfs_handle_t * 420 zfs_open(libzfs_handle_t *hdl, const char *path, int types) 421 { 422 zfs_handle_t *zhp; 423 char errbuf[1024]; 424 425 (void) snprintf(errbuf, sizeof (errbuf), 426 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 427 428 /* 429 * Validate the name before we even try to open it. 430 */ 431 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 433 "invalid dataset name")); 434 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 435 return (NULL); 436 } 437 438 /* 439 * Try to get stats for the dataset, which will tell us if it exists. 440 */ 441 errno = 0; 442 if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 443 (void) zfs_standard_error(hdl, errno, errbuf); 444 return (NULL); 445 } 446 447 if (!(types & zhp->zfs_type)) { 448 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 449 zfs_close(zhp); 450 return (NULL); 451 } 452 453 return (zhp); 454 } 455 456 /* 457 * Release a ZFS handle. Nothing to do but free the associated memory. 458 */ 459 void 460 zfs_close(zfs_handle_t *zhp) 461 { 462 if (zhp->zfs_mntopts) 463 free(zhp->zfs_mntopts); 464 nvlist_free(zhp->zfs_props); 465 nvlist_free(zhp->zfs_user_props); 466 free(zhp); 467 } 468 469 /* 470 * Given an nvlist of properties to set, validates that they are correct, and 471 * parses any numeric properties (index, boolean, etc) if they are specified as 472 * strings. 473 */ 474 static nvlist_t * 475 zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 476 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 477 { 478 nvpair_t *elem; 479 uint64_t intval; 480 char *strval; 481 zfs_prop_t prop; 482 nvlist_t *ret; 483 int chosen_normal = -1; 484 int chosen_utf = -1; 485 486 if (type == ZFS_TYPE_SNAPSHOT) { 487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 488 "snapshot properties cannot be modified")); 489 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 490 return (NULL); 491 } 492 493 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 494 (void) no_memory(hdl); 495 return (NULL); 496 } 497 498 elem = NULL; 499 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 500 const char *propname = nvpair_name(elem); 501 502 /* 503 * Make sure this property is valid and applies to this type. 504 */ 505 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 506 if (!zfs_prop_user(propname)) { 507 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 508 "invalid property '%s'"), propname); 509 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 510 goto error; 511 } 512 513 /* 514 * If this is a user property, make sure it's a 515 * string, and that it's less than ZAP_MAXNAMELEN. 516 */ 517 if (nvpair_type(elem) != DATA_TYPE_STRING) { 518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 519 "'%s' must be a string"), propname); 520 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 521 goto error; 522 } 523 524 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 526 "property name '%s' is too long"), 527 propname); 528 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 529 goto error; 530 } 531 532 (void) nvpair_value_string(elem, &strval); 533 if (nvlist_add_string(ret, propname, strval) != 0) { 534 (void) no_memory(hdl); 535 goto error; 536 } 537 continue; 538 } 539 540 if (!zfs_prop_valid_for_type(prop, type)) { 541 zfs_error_aux(hdl, 542 dgettext(TEXT_DOMAIN, "'%s' does not " 543 "apply to datasets of this type"), propname); 544 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 545 goto error; 546 } 547 548 if (zfs_prop_readonly(prop) && 549 (!zfs_prop_setonce(prop) || zhp != NULL)) { 550 zfs_error_aux(hdl, 551 dgettext(TEXT_DOMAIN, "'%s' is readonly"), 552 propname); 553 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 554 goto error; 555 } 556 557 if (zprop_parse_value(hdl, elem, prop, type, ret, 558 &strval, &intval, errbuf) != 0) 559 goto error; 560 561 /* 562 * Perform some additional checks for specific properties. 563 */ 564 switch (prop) { 565 case ZFS_PROP_VERSION: 566 { 567 int version; 568 569 if (zhp == NULL) 570 break; 571 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 572 if (intval < version) { 573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 574 "Can not downgrade; already at version %u"), 575 version); 576 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 577 goto error; 578 } 579 break; 580 } 581 582 case ZFS_PROP_RECORDSIZE: 583 case ZFS_PROP_VOLBLOCKSIZE: 584 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 585 if (intval < SPA_MINBLOCKSIZE || 586 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 588 "'%s' must be power of 2 from %u " 589 "to %uk"), propname, 590 (uint_t)SPA_MINBLOCKSIZE, 591 (uint_t)SPA_MAXBLOCKSIZE >> 10); 592 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 593 goto error; 594 } 595 break; 596 597 case ZFS_PROP_SHAREISCSI: 598 if (strcmp(strval, "off") != 0 && 599 strcmp(strval, "on") != 0 && 600 strcmp(strval, "type=disk") != 0) { 601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 602 "'%s' must be 'on', 'off', or 'type=disk'"), 603 propname); 604 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 605 goto error; 606 } 607 608 break; 609 610 case ZFS_PROP_MOUNTPOINT: 611 { 612 namecheck_err_t why; 613 614 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 615 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 616 break; 617 618 if (mountpoint_namecheck(strval, &why)) { 619 switch (why) { 620 case NAME_ERR_LEADING_SLASH: 621 zfs_error_aux(hdl, 622 dgettext(TEXT_DOMAIN, 623 "'%s' must be an absolute path, " 624 "'none', or 'legacy'"), propname); 625 break; 626 case NAME_ERR_TOOLONG: 627 zfs_error_aux(hdl, 628 dgettext(TEXT_DOMAIN, 629 "component of '%s' is too long"), 630 propname); 631 break; 632 } 633 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 634 goto error; 635 } 636 } 637 638 /*FALLTHRU*/ 639 640 case ZFS_PROP_SHARESMB: 641 case ZFS_PROP_SHARENFS: 642 /* 643 * For the mountpoint and sharenfs or sharesmb 644 * properties, check if it can be set in a 645 * global/non-global zone based on 646 * the zoned property value: 647 * 648 * global zone non-global zone 649 * -------------------------------------------------- 650 * zoned=on mountpoint (no) mountpoint (yes) 651 * sharenfs (no) sharenfs (no) 652 * sharesmb (no) sharesmb (no) 653 * 654 * zoned=off mountpoint (yes) N/A 655 * sharenfs (yes) 656 * sharesmb (yes) 657 */ 658 if (zoned) { 659 if (getzoneid() == GLOBAL_ZONEID) { 660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 661 "'%s' cannot be set on " 662 "dataset in a non-global zone"), 663 propname); 664 (void) zfs_error(hdl, EZFS_ZONED, 665 errbuf); 666 goto error; 667 } else if (prop == ZFS_PROP_SHARENFS || 668 prop == ZFS_PROP_SHARESMB) { 669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 670 "'%s' cannot be set in " 671 "a non-global zone"), propname); 672 (void) zfs_error(hdl, EZFS_ZONED, 673 errbuf); 674 goto error; 675 } 676 } else if (getzoneid() != GLOBAL_ZONEID) { 677 /* 678 * If zoned property is 'off', this must be in 679 * a globle zone. If not, something is wrong. 680 */ 681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 682 "'%s' cannot be set while dataset " 683 "'zoned' property is set"), propname); 684 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 685 goto error; 686 } 687 688 /* 689 * At this point, it is legitimate to set the 690 * property. Now we want to make sure that the 691 * property value is valid if it is sharenfs. 692 */ 693 if ((prop == ZFS_PROP_SHARENFS || 694 prop == ZFS_PROP_SHARESMB) && 695 strcmp(strval, "on") != 0 && 696 strcmp(strval, "off") != 0) { 697 zfs_share_proto_t proto; 698 699 if (prop == ZFS_PROP_SHARESMB) 700 proto = PROTO_SMB; 701 else 702 proto = PROTO_NFS; 703 704 /* 705 * Must be an valid sharing protocol 706 * option string so init the libshare 707 * in order to enable the parser and 708 * then parse the options. We use the 709 * control API since we don't care about 710 * the current configuration and don't 711 * want the overhead of loading it 712 * until we actually do something. 713 */ 714 715 if (zfs_init_libshare(hdl, 716 SA_INIT_CONTROL_API) != SA_OK) { 717 /* 718 * An error occurred so we can't do 719 * anything 720 */ 721 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 722 "'%s' cannot be set: problem " 723 "in share initialization"), 724 propname); 725 (void) zfs_error(hdl, EZFS_BADPROP, 726 errbuf); 727 goto error; 728 } 729 730 if (zfs_parse_options(strval, proto) != SA_OK) { 731 /* 732 * There was an error in parsing so 733 * deal with it by issuing an error 734 * message and leaving after 735 * uninitializing the the libshare 736 * interface. 737 */ 738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 739 "'%s' cannot be set to invalid " 740 "options"), propname); 741 (void) zfs_error(hdl, EZFS_BADPROP, 742 errbuf); 743 zfs_uninit_libshare(hdl); 744 goto error; 745 } 746 zfs_uninit_libshare(hdl); 747 } 748 749 break; 750 case ZFS_PROP_UTF8ONLY: 751 chosen_utf = (int)intval; 752 break; 753 case ZFS_PROP_NORMALIZE: 754 chosen_normal = (int)intval; 755 break; 756 } 757 758 /* 759 * For changes to existing volumes, we have some additional 760 * checks to enforce. 761 */ 762 if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 763 uint64_t volsize = zfs_prop_get_int(zhp, 764 ZFS_PROP_VOLSIZE); 765 uint64_t blocksize = zfs_prop_get_int(zhp, 766 ZFS_PROP_VOLBLOCKSIZE); 767 char buf[64]; 768 769 switch (prop) { 770 case ZFS_PROP_RESERVATION: 771 if (intval > volsize) { 772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 773 "'%s' is greater than current " 774 "volume size"), propname); 775 (void) zfs_error(hdl, EZFS_BADPROP, 776 errbuf); 777 goto error; 778 } 779 break; 780 781 case ZFS_PROP_VOLSIZE: 782 if (intval % blocksize != 0) { 783 zfs_nicenum(blocksize, buf, 784 sizeof (buf)); 785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 786 "'%s' must be a multiple of " 787 "volume block size (%s)"), 788 propname, buf); 789 (void) zfs_error(hdl, EZFS_BADPROP, 790 errbuf); 791 goto error; 792 } 793 794 if (intval == 0) { 795 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 796 "'%s' cannot be zero"), 797 propname); 798 (void) zfs_error(hdl, EZFS_BADPROP, 799 errbuf); 800 goto error; 801 } 802 break; 803 } 804 } 805 } 806 807 /* 808 * If normalization was chosen, but no UTF8 choice was made, 809 * enforce rejection of non-UTF8 names. 810 * 811 * If normalization was chosen, but rejecting non-UTF8 names 812 * was explicitly not chosen, it is an error. 813 */ 814 if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf < 0) { 815 if (nvlist_add_uint64(ret, 816 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 817 (void) no_memory(hdl); 818 goto error; 819 } 820 } else if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf == 0) { 821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 822 "'%s' must be set 'on' if normalization chosen"), 823 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 824 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 825 goto error; 826 } 827 828 /* 829 * If this is an existing volume, and someone is setting the volsize, 830 * make sure that it matches the reservation, or add it if necessary. 831 */ 832 if (zhp != NULL && type == ZFS_TYPE_VOLUME && 833 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 834 &intval) == 0) { 835 uint64_t old_volsize = zfs_prop_get_int(zhp, 836 ZFS_PROP_VOLSIZE); 837 uint64_t old_reservation = zfs_prop_get_int(zhp, 838 ZFS_PROP_RESERVATION); 839 uint64_t new_reservation; 840 841 if (old_volsize == old_reservation && 842 nvlist_lookup_uint64(ret, 843 zfs_prop_to_name(ZFS_PROP_RESERVATION), 844 &new_reservation) != 0) { 845 if (nvlist_add_uint64(ret, 846 zfs_prop_to_name(ZFS_PROP_RESERVATION), 847 intval) != 0) { 848 (void) no_memory(hdl); 849 goto error; 850 } 851 } 852 } 853 854 return (ret); 855 856 error: 857 nvlist_free(ret); 858 return (NULL); 859 } 860 861 static int 862 zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 863 uint64_t *ret_who) 864 { 865 struct passwd *pwd; 866 struct group *grp; 867 uid_t id; 868 869 if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 870 *who_type == ZFS_DELEG_NAMED_SET) { 871 *ret_who = -1; 872 return (0); 873 } 874 if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 875 return (EZFS_BADWHO); 876 877 if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 878 strcmp(who, "everyone") == 0) { 879 *ret_who = -1; 880 *who_type = ZFS_DELEG_EVERYONE; 881 return (0); 882 } 883 884 pwd = getpwnam(who); 885 grp = getgrnam(who); 886 887 if ((*who_type == ZFS_DELEG_USER) && pwd) { 888 *ret_who = pwd->pw_uid; 889 } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 890 *ret_who = grp->gr_gid; 891 } else if (pwd) { 892 *ret_who = pwd->pw_uid; 893 *who_type = ZFS_DELEG_USER; 894 } else if (grp) { 895 *ret_who = grp->gr_gid; 896 *who_type = ZFS_DELEG_GROUP; 897 } else { 898 char *end; 899 900 id = strtol(who, &end, 10); 901 if (errno != 0 || *end != '\0') { 902 return (EZFS_BADWHO); 903 } else { 904 *ret_who = id; 905 if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 906 *who_type = ZFS_DELEG_USER; 907 } 908 } 909 910 return (0); 911 } 912 913 static void 914 zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 915 { 916 if (perms_nvp != NULL) { 917 verify(nvlist_add_nvlist(who_nvp, 918 name, perms_nvp) == 0); 919 } else { 920 verify(nvlist_add_boolean(who_nvp, name) == 0); 921 } 922 } 923 924 static void 925 helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 926 zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 927 nvlist_t *sets_nvp) 928 { 929 boolean_t do_perms, do_sets; 930 char name[ZFS_MAX_DELEG_NAME]; 931 932 do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 933 do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 934 935 if (!do_perms && !do_sets) 936 do_perms = do_sets = B_TRUE; 937 938 if (do_perms) { 939 zfs_deleg_whokey(name, who_type, inherit, 940 (who_type == ZFS_DELEG_NAMED_SET) ? 941 whostr : (void *)&whoid); 942 zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 943 } 944 if (do_sets) { 945 zfs_deleg_whokey(name, toupper(who_type), inherit, 946 (who_type == ZFS_DELEG_NAMED_SET) ? 947 whostr : (void *)&whoid); 948 zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 949 } 950 } 951 952 static void 953 zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 954 nvlist_t *perms_nvp, nvlist_t *sets_nvp, 955 zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 956 { 957 if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 958 helper(who_type, whoid, whostr, 0, 959 who_nvp, perms_nvp, sets_nvp); 960 } else { 961 if (inherit & ZFS_DELEG_PERM_LOCAL) { 962 helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 963 who_nvp, perms_nvp, sets_nvp); 964 } 965 if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 966 helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 967 who_nvp, perms_nvp, sets_nvp); 968 } 969 } 970 } 971 972 /* 973 * Construct nvlist to pass down to kernel for setting/removing permissions. 974 * 975 * The nvlist is constructed as a series of nvpairs with an optional embedded 976 * nvlist of permissions to remove or set. The topmost nvpairs are the actual 977 * base attribute named stored in the dsl. 978 * Arguments: 979 * 980 * whostr: is a comma separated list of users, groups, or a single set name. 981 * whostr may be null for everyone or create perms. 982 * who_type: is the type of entry in whostr. Typically this will be 983 * ZFS_DELEG_WHO_UNKNOWN. 984 * perms: common separated list of permissions. May be null if user 985 * is requested to remove permissions by who. 986 * inherit: Specifies the inheritance of the permissions. Will be either 987 * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 988 * nvp The constructed nvlist to pass to zfs_perm_set(). 989 * The output nvp will look something like this. 990 * ul$1234 -> {create ; destroy } 991 * Ul$1234 -> { @myset } 992 * s-$@myset - { snapshot; checksum; compression } 993 */ 994 int 995 zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 996 zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 997 { 998 nvlist_t *who_nvp; 999 nvlist_t *perms_nvp = NULL; 1000 nvlist_t *sets_nvp = NULL; 1001 char errbuf[1024]; 1002 char *who_tok, *perm; 1003 int error; 1004 1005 *nvp = NULL; 1006 1007 if (perms) { 1008 if ((error = nvlist_alloc(&perms_nvp, 1009 NV_UNIQUE_NAME, 0)) != 0) { 1010 return (1); 1011 } 1012 if ((error = nvlist_alloc(&sets_nvp, 1013 NV_UNIQUE_NAME, 0)) != 0) { 1014 nvlist_free(perms_nvp); 1015 return (1); 1016 } 1017 } 1018 1019 if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 1020 if (perms_nvp) 1021 nvlist_free(perms_nvp); 1022 if (sets_nvp) 1023 nvlist_free(sets_nvp); 1024 return (1); 1025 } 1026 1027 if (who_type == ZFS_DELEG_NAMED_SET) { 1028 namecheck_err_t why; 1029 char what; 1030 1031 if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 1032 nvlist_free(who_nvp); 1033 if (perms_nvp) 1034 nvlist_free(perms_nvp); 1035 if (sets_nvp) 1036 nvlist_free(sets_nvp); 1037 1038 switch (why) { 1039 case NAME_ERR_NO_AT: 1040 zfs_error_aux(zhp->zfs_hdl, 1041 dgettext(TEXT_DOMAIN, 1042 "set definition must begin with an '@' " 1043 "character")); 1044 } 1045 return (zfs_error(zhp->zfs_hdl, 1046 EZFS_BADPERMSET, whostr)); 1047 } 1048 } 1049 1050 /* 1051 * Build up nvlist(s) of permissions. Two nvlists are maintained. 1052 * The first nvlist perms_nvp will have normal permissions and the 1053 * other sets_nvp will have only permssion set names in it. 1054 */ 1055 for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 1056 const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 1057 1058 if (perm_canonical) { 1059 verify(nvlist_add_boolean(perms_nvp, 1060 perm_canonical) == 0); 1061 } else if (perm[0] == '@') { 1062 verify(nvlist_add_boolean(sets_nvp, perm) == 0); 1063 } else { 1064 nvlist_free(who_nvp); 1065 nvlist_free(perms_nvp); 1066 nvlist_free(sets_nvp); 1067 return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 1068 } 1069 } 1070 1071 if (whostr && who_type != ZFS_DELEG_CREATE) { 1072 who_tok = strtok(whostr, ","); 1073 if (who_tok == NULL) { 1074 nvlist_free(who_nvp); 1075 if (perms_nvp) 1076 nvlist_free(perms_nvp); 1077 if (sets_nvp) 1078 nvlist_free(sets_nvp); 1079 (void) snprintf(errbuf, sizeof (errbuf), 1080 dgettext(TEXT_DOMAIN, "Who string is NULL"), 1081 whostr); 1082 return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1083 } 1084 } 1085 1086 /* 1087 * Now create the nvlist(s) 1088 */ 1089 do { 1090 uint64_t who_id; 1091 1092 error = zfs_get_perm_who(who_tok, &who_type, 1093 &who_id); 1094 if (error) { 1095 nvlist_free(who_nvp); 1096 if (perms_nvp) 1097 nvlist_free(perms_nvp); 1098 if (sets_nvp) 1099 nvlist_free(sets_nvp); 1100 (void) snprintf(errbuf, sizeof (errbuf), 1101 dgettext(TEXT_DOMAIN, 1102 "Unable to determine uid/gid for " 1103 "%s "), who_tok); 1104 return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1105 } 1106 1107 /* 1108 * add entries for both local and descendent when required 1109 */ 1110 zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 1111 perms_nvp, sets_nvp, who_type, inherit); 1112 1113 } while (who_tok = strtok(NULL, ",")); 1114 *nvp = who_nvp; 1115 return (0); 1116 } 1117 1118 static int 1119 zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 1120 { 1121 zfs_cmd_t zc = { 0 }; 1122 int error; 1123 char errbuf[1024]; 1124 1125 (void) snprintf(errbuf, sizeof (errbuf), 1126 dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 1127 zhp->zfs_name); 1128 1129 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 1130 return (-1); 1131 1132 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1133 zc.zc_perm_action = unset; 1134 1135 error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 1136 if (error && errno == ENOTSUP) { 1137 (void) snprintf(errbuf, sizeof (errbuf), 1138 gettext("Pool must be upgraded to use 'allow/unallow'")); 1139 zcmd_free_nvlists(&zc); 1140 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 1141 } else if (error) { 1142 return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 1143 } 1144 zcmd_free_nvlists(&zc); 1145 1146 return (error); 1147 } 1148 1149 int 1150 zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 1151 { 1152 return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 1153 } 1154 1155 int 1156 zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 1157 { 1158 return (zfs_perm_set_common(zhp, perms, B_TRUE)); 1159 } 1160 1161 static int 1162 perm_compare(const void *arg1, const void *arg2) 1163 { 1164 const zfs_perm_node_t *node1 = arg1; 1165 const zfs_perm_node_t *node2 = arg2; 1166 int ret; 1167 1168 ret = strcmp(node1->z_pname, node2->z_pname); 1169 1170 if (ret > 0) 1171 return (1); 1172 if (ret < 0) 1173 return (-1); 1174 else 1175 return (0); 1176 } 1177 1178 static void 1179 zfs_destroy_perm_tree(avl_tree_t *tree) 1180 { 1181 zfs_perm_node_t *permnode; 1182 void *cookie = NULL; 1183 1184 while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 1185 free(permnode); 1186 avl_destroy(tree); 1187 } 1188 1189 static void 1190 zfs_destroy_tree(avl_tree_t *tree) 1191 { 1192 zfs_allow_node_t *allownode; 1193 void *cookie = NULL; 1194 1195 while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 1196 zfs_destroy_perm_tree(&allownode->z_localdescend); 1197 zfs_destroy_perm_tree(&allownode->z_local); 1198 zfs_destroy_perm_tree(&allownode->z_descend); 1199 free(allownode); 1200 } 1201 avl_destroy(tree); 1202 } 1203 1204 void 1205 zfs_free_allows(zfs_allow_t *allow) 1206 { 1207 zfs_allow_t *allownext; 1208 zfs_allow_t *freeallow; 1209 1210 allownext = allow; 1211 while (allownext) { 1212 zfs_destroy_tree(&allownext->z_sets); 1213 zfs_destroy_tree(&allownext->z_crperms); 1214 zfs_destroy_tree(&allownext->z_user); 1215 zfs_destroy_tree(&allownext->z_group); 1216 zfs_destroy_tree(&allownext->z_everyone); 1217 freeallow = allownext; 1218 allownext = allownext->z_next; 1219 free(freeallow); 1220 } 1221 } 1222 1223 static zfs_allow_t * 1224 zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 1225 { 1226 zfs_allow_t *ptree; 1227 1228 if ((ptree = zfs_alloc(zhp->zfs_hdl, 1229 sizeof (zfs_allow_t))) == NULL) { 1230 return (NULL); 1231 } 1232 1233 (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 1234 avl_create(&ptree->z_sets, 1235 perm_compare, sizeof (zfs_allow_node_t), 1236 offsetof(zfs_allow_node_t, z_node)); 1237 avl_create(&ptree->z_crperms, 1238 perm_compare, sizeof (zfs_allow_node_t), 1239 offsetof(zfs_allow_node_t, z_node)); 1240 avl_create(&ptree->z_user, 1241 perm_compare, sizeof (zfs_allow_node_t), 1242 offsetof(zfs_allow_node_t, z_node)); 1243 avl_create(&ptree->z_group, 1244 perm_compare, sizeof (zfs_allow_node_t), 1245 offsetof(zfs_allow_node_t, z_node)); 1246 avl_create(&ptree->z_everyone, 1247 perm_compare, sizeof (zfs_allow_node_t), 1248 offsetof(zfs_allow_node_t, z_node)); 1249 1250 if (prev) 1251 prev->z_next = ptree; 1252 ptree->z_next = NULL; 1253 return (ptree); 1254 } 1255 1256 /* 1257 * Add permissions to the appropriate AVL permission tree. 1258 * The appropriate tree may not be the requested tree. 1259 * For example if ld indicates a local permission, but 1260 * same permission also exists as a descendent permission 1261 * then the permission will be removed from the descendent 1262 * tree and add the the local+descendent tree. 1263 */ 1264 static int 1265 zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 1266 char *perm, char ld) 1267 { 1268 zfs_perm_node_t pnode, *permnode, *permnode2; 1269 zfs_perm_node_t *newnode; 1270 avl_index_t where, where2; 1271 avl_tree_t *tree, *altree; 1272 1273 (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 1274 1275 if (ld == ZFS_DELEG_NA) { 1276 tree = &allownode->z_localdescend; 1277 altree = &allownode->z_descend; 1278 } else if (ld == ZFS_DELEG_LOCAL) { 1279 tree = &allownode->z_local; 1280 altree = &allownode->z_descend; 1281 } else { 1282 tree = &allownode->z_descend; 1283 altree = &allownode->z_local; 1284 } 1285 permnode = avl_find(tree, &pnode, &where); 1286 permnode2 = avl_find(altree, &pnode, &where2); 1287 1288 if (permnode2) { 1289 avl_remove(altree, permnode2); 1290 free(permnode2); 1291 if (permnode == NULL) { 1292 tree = &allownode->z_localdescend; 1293 } 1294 } 1295 1296 /* 1297 * Now insert new permission in either requested location 1298 * local/descendent or into ld when perm will exist in both. 1299 */ 1300 if (permnode == NULL) { 1301 if ((newnode = zfs_alloc(zhp->zfs_hdl, 1302 sizeof (zfs_perm_node_t))) == NULL) { 1303 return (-1); 1304 } 1305 *newnode = pnode; 1306 avl_add(tree, newnode); 1307 } 1308 return (0); 1309 } 1310 1311 /* 1312 * Uggh, this is going to be a bit complicated. 1313 * we have an nvlist coming out of the kernel that 1314 * will indicate where the permission is set and then 1315 * it will contain allow of the various "who's", and what 1316 * their permissions are. To further complicate this 1317 * we will then have to coalesce the local,descendent 1318 * and local+descendent permissions where appropriate. 1319 * The kernel only knows about a permission as being local 1320 * or descendent, but not both. 1321 * 1322 * In order to make this easier for zfs_main to deal with 1323 * a series of AVL trees will be used to maintain 1324 * all of this, primarily for sorting purposes as well 1325 * as the ability to quickly locate a specific entry. 1326 * 1327 * What we end up with are tree's for sets, create perms, 1328 * user, groups and everyone. With each of those trees 1329 * we have subtrees for local, descendent and local+descendent 1330 * permissions. 1331 */ 1332 int 1333 zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 1334 { 1335 zfs_cmd_t zc = { 0 }; 1336 int error; 1337 nvlist_t *nvlist; 1338 nvlist_t *permnv, *sourcenv; 1339 nvpair_t *who_pair, *source_pair; 1340 nvpair_t *perm_pair; 1341 char errbuf[1024]; 1342 zfs_allow_t *zallowp, *newallowp; 1343 char ld; 1344 char *nvpname; 1345 uid_t uid; 1346 gid_t gid; 1347 avl_tree_t *tree; 1348 avl_index_t where; 1349 1350 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1351 1352 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 1353 return (-1); 1354 1355 while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 1356 if (errno == ENOMEM) { 1357 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 1358 zcmd_free_nvlists(&zc); 1359 return (-1); 1360 } 1361 } else if (errno == ENOTSUP) { 1362 zcmd_free_nvlists(&zc); 1363 (void) snprintf(errbuf, sizeof (errbuf), 1364 gettext("Pool must be upgraded to use 'allow'")); 1365 return (zfs_error(zhp->zfs_hdl, 1366 EZFS_BADVERSION, errbuf)); 1367 } else { 1368 zcmd_free_nvlists(&zc); 1369 return (-1); 1370 } 1371 } 1372 1373 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 1374 zcmd_free_nvlists(&zc); 1375 return (-1); 1376 } 1377 1378 zcmd_free_nvlists(&zc); 1379 1380 source_pair = nvlist_next_nvpair(nvlist, NULL); 1381 1382 if (source_pair == NULL) { 1383 *zfs_perms = NULL; 1384 return (0); 1385 } 1386 1387 *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 1388 if (*zfs_perms == NULL) { 1389 return (0); 1390 } 1391 1392 zallowp = *zfs_perms; 1393 1394 for (;;) { 1395 struct passwd *pwd; 1396 struct group *grp; 1397 zfs_allow_node_t *allownode; 1398 zfs_allow_node_t findallownode; 1399 zfs_allow_node_t *newallownode; 1400 1401 (void) strlcpy(zallowp->z_setpoint, 1402 nvpair_name(source_pair), 1403 sizeof (zallowp->z_setpoint)); 1404 1405 if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 1406 goto abort; 1407 1408 /* 1409 * Make sure nvlist is composed correctly 1410 */ 1411 if (zfs_deleg_verify_nvlist(sourcenv)) { 1412 goto abort; 1413 } 1414 1415 who_pair = nvlist_next_nvpair(sourcenv, NULL); 1416 if (who_pair == NULL) { 1417 goto abort; 1418 } 1419 1420 do { 1421 error = nvpair_value_nvlist(who_pair, &permnv); 1422 if (error) { 1423 goto abort; 1424 } 1425 1426 /* 1427 * First build up the key to use 1428 * for looking up in the various 1429 * who trees. 1430 */ 1431 ld = nvpair_name(who_pair)[1]; 1432 nvpname = nvpair_name(who_pair); 1433 switch (nvpair_name(who_pair)[0]) { 1434 case ZFS_DELEG_USER: 1435 case ZFS_DELEG_USER_SETS: 1436 tree = &zallowp->z_user; 1437 uid = atol(&nvpname[3]); 1438 pwd = getpwuid(uid); 1439 (void) snprintf(findallownode.z_key, 1440 sizeof (findallownode.z_key), "user %s", 1441 (pwd) ? pwd->pw_name : 1442 &nvpair_name(who_pair)[3]); 1443 break; 1444 case ZFS_DELEG_GROUP: 1445 case ZFS_DELEG_GROUP_SETS: 1446 tree = &zallowp->z_group; 1447 gid = atol(&nvpname[3]); 1448 grp = getgrgid(gid); 1449 (void) snprintf(findallownode.z_key, 1450 sizeof (findallownode.z_key), "group %s", 1451 (grp) ? grp->gr_name : 1452 &nvpair_name(who_pair)[3]); 1453 break; 1454 case ZFS_DELEG_CREATE: 1455 case ZFS_DELEG_CREATE_SETS: 1456 tree = &zallowp->z_crperms; 1457 (void) strlcpy(findallownode.z_key, "", 1458 sizeof (findallownode.z_key)); 1459 break; 1460 case ZFS_DELEG_EVERYONE: 1461 case ZFS_DELEG_EVERYONE_SETS: 1462 (void) snprintf(findallownode.z_key, 1463 sizeof (findallownode.z_key), "everyone"); 1464 tree = &zallowp->z_everyone; 1465 break; 1466 case ZFS_DELEG_NAMED_SET: 1467 case ZFS_DELEG_NAMED_SET_SETS: 1468 (void) snprintf(findallownode.z_key, 1469 sizeof (findallownode.z_key), "%s", 1470 &nvpair_name(who_pair)[3]); 1471 tree = &zallowp->z_sets; 1472 break; 1473 } 1474 1475 /* 1476 * Place who in tree 1477 */ 1478 allownode = avl_find(tree, &findallownode, &where); 1479 if (allownode == NULL) { 1480 if ((newallownode = zfs_alloc(zhp->zfs_hdl, 1481 sizeof (zfs_allow_node_t))) == NULL) { 1482 goto abort; 1483 } 1484 avl_create(&newallownode->z_localdescend, 1485 perm_compare, 1486 sizeof (zfs_perm_node_t), 1487 offsetof(zfs_perm_node_t, z_node)); 1488 avl_create(&newallownode->z_local, 1489 perm_compare, 1490 sizeof (zfs_perm_node_t), 1491 offsetof(zfs_perm_node_t, z_node)); 1492 avl_create(&newallownode->z_descend, 1493 perm_compare, 1494 sizeof (zfs_perm_node_t), 1495 offsetof(zfs_perm_node_t, z_node)); 1496 (void) strlcpy(newallownode->z_key, 1497 findallownode.z_key, 1498 sizeof (findallownode.z_key)); 1499 avl_insert(tree, newallownode, where); 1500 allownode = newallownode; 1501 } 1502 1503 /* 1504 * Now iterate over the permissions and 1505 * place them in the appropriate local, 1506 * descendent or local+descendent tree. 1507 * 1508 * The permissions are added to the tree 1509 * via zfs_coalesce_perm(). 1510 */ 1511 perm_pair = nvlist_next_nvpair(permnv, NULL); 1512 if (perm_pair == NULL) 1513 goto abort; 1514 do { 1515 if (zfs_coalesce_perm(zhp, allownode, 1516 nvpair_name(perm_pair), ld) != 0) 1517 goto abort; 1518 } while (perm_pair = nvlist_next_nvpair(permnv, 1519 perm_pair)); 1520 } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 1521 1522 source_pair = nvlist_next_nvpair(nvlist, source_pair); 1523 if (source_pair == NULL) 1524 break; 1525 1526 /* 1527 * allocate another node from the link list of 1528 * zfs_allow_t structures 1529 */ 1530 newallowp = zfs_alloc_perm_tree(zhp, zallowp, 1531 nvpair_name(source_pair)); 1532 if (newallowp == NULL) { 1533 goto abort; 1534 } 1535 zallowp = newallowp; 1536 } 1537 nvlist_free(nvlist); 1538 return (0); 1539 abort: 1540 zfs_free_allows(*zfs_perms); 1541 nvlist_free(nvlist); 1542 return (-1); 1543 } 1544 1545 /* 1546 * Given a property name and value, set the property for the given dataset. 1547 */ 1548 int 1549 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1550 { 1551 zfs_cmd_t zc = { 0 }; 1552 int ret = -1; 1553 prop_changelist_t *cl = NULL; 1554 char errbuf[1024]; 1555 libzfs_handle_t *hdl = zhp->zfs_hdl; 1556 nvlist_t *nvl = NULL, *realprops; 1557 zfs_prop_t prop; 1558 1559 (void) snprintf(errbuf, sizeof (errbuf), 1560 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1561 zhp->zfs_name); 1562 1563 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1564 nvlist_add_string(nvl, propname, propval) != 0) { 1565 (void) no_memory(hdl); 1566 goto error; 1567 } 1568 1569 if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 1570 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 1571 goto error; 1572 1573 nvlist_free(nvl); 1574 nvl = realprops; 1575 1576 prop = zfs_name_to_prop(propname); 1577 1578 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1579 goto error; 1580 1581 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1583 "child dataset with inherited mountpoint is used " 1584 "in a non-global zone")); 1585 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1586 goto error; 1587 } 1588 1589 if ((ret = changelist_prefix(cl)) != 0) 1590 goto error; 1591 1592 /* 1593 * Execute the corresponding ioctl() to set this property. 1594 */ 1595 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1596 1597 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 1598 goto error; 1599 1600 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1601 1602 if (ret != 0) { 1603 switch (errno) { 1604 1605 case ENOSPC: 1606 /* 1607 * For quotas and reservations, ENOSPC indicates 1608 * something different; setting a quota or reservation 1609 * doesn't use any disk space. 1610 */ 1611 switch (prop) { 1612 case ZFS_PROP_QUOTA: 1613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1614 "size is less than current used or " 1615 "reserved space")); 1616 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1617 break; 1618 1619 case ZFS_PROP_RESERVATION: 1620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1621 "size is greater than available space")); 1622 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1623 break; 1624 1625 default: 1626 (void) zfs_standard_error(hdl, errno, errbuf); 1627 break; 1628 } 1629 break; 1630 1631 case EBUSY: 1632 if (prop == ZFS_PROP_VOLBLOCKSIZE) 1633 (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 1634 else 1635 (void) zfs_standard_error(hdl, EBUSY, errbuf); 1636 break; 1637 1638 case EROFS: 1639 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 1640 break; 1641 1642 case ENOTSUP: 1643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1644 "pool must be upgraded to set this " 1645 "property or value")); 1646 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 1647 break; 1648 1649 case EOVERFLOW: 1650 /* 1651 * This platform can't address a volume this big. 1652 */ 1653 #ifdef _ILP32 1654 if (prop == ZFS_PROP_VOLSIZE) { 1655 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1656 break; 1657 } 1658 #endif 1659 /* FALLTHROUGH */ 1660 default: 1661 (void) zfs_standard_error(hdl, errno, errbuf); 1662 } 1663 } else { 1664 /* 1665 * Refresh the statistics so the new property value 1666 * is reflected. 1667 */ 1668 if ((ret = changelist_postfix(cl)) == 0) 1669 (void) get_stats(zhp); 1670 } 1671 1672 error: 1673 nvlist_free(nvl); 1674 zcmd_free_nvlists(&zc); 1675 if (cl) 1676 changelist_free(cl); 1677 return (ret); 1678 } 1679 1680 /* 1681 * Given a property, inherit the value from the parent dataset. 1682 */ 1683 int 1684 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1685 { 1686 zfs_cmd_t zc = { 0 }; 1687 int ret; 1688 prop_changelist_t *cl; 1689 libzfs_handle_t *hdl = zhp->zfs_hdl; 1690 char errbuf[1024]; 1691 zfs_prop_t prop; 1692 1693 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1694 "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1695 1696 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1697 /* 1698 * For user properties, the amount of work we have to do is very 1699 * small, so just do it here. 1700 */ 1701 if (!zfs_prop_user(propname)) { 1702 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1703 "invalid property")); 1704 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1705 } 1706 1707 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1708 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1709 1710 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1711 return (zfs_standard_error(hdl, errno, errbuf)); 1712 1713 return (0); 1714 } 1715 1716 /* 1717 * Verify that this property is inheritable. 1718 */ 1719 if (zfs_prop_readonly(prop)) 1720 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1721 1722 if (!zfs_prop_inheritable(prop)) 1723 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1724 1725 /* 1726 * Check to see if the value applies to this type 1727 */ 1728 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1729 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1730 1731 /* 1732 * Normalize the name, to get rid of shorthand abbrevations. 1733 */ 1734 propname = zfs_prop_to_name(prop); 1735 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1736 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1737 1738 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1739 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 1740 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1741 "dataset is used in a non-global zone")); 1742 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1743 } 1744 1745 /* 1746 * Determine datasets which will be affected by this change, if any. 1747 */ 1748 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1749 return (-1); 1750 1751 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1753 "child dataset with inherited mountpoint is used " 1754 "in a non-global zone")); 1755 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1756 goto error; 1757 } 1758 1759 if ((ret = changelist_prefix(cl)) != 0) 1760 goto error; 1761 1762 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 1763 return (zfs_standard_error(hdl, errno, errbuf)); 1764 } else { 1765 1766 if ((ret = changelist_postfix(cl)) != 0) 1767 goto error; 1768 1769 /* 1770 * Refresh the statistics so the new property is reflected. 1771 */ 1772 (void) get_stats(zhp); 1773 } 1774 1775 error: 1776 changelist_free(cl); 1777 return (ret); 1778 } 1779 1780 /* 1781 * True DSL properties are stored in an nvlist. The following two functions 1782 * extract them appropriately. 1783 */ 1784 static uint64_t 1785 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 1786 { 1787 nvlist_t *nv; 1788 uint64_t value; 1789 1790 *source = NULL; 1791 if (nvlist_lookup_nvlist(zhp->zfs_props, 1792 zfs_prop_to_name(prop), &nv) == 0) { 1793 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1794 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 1795 } else { 1796 value = zfs_prop_default_numeric(prop); 1797 *source = ""; 1798 } 1799 1800 return (value); 1801 } 1802 1803 static char * 1804 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 1805 { 1806 nvlist_t *nv; 1807 char *value; 1808 1809 *source = NULL; 1810 if (nvlist_lookup_nvlist(zhp->zfs_props, 1811 zfs_prop_to_name(prop), &nv) == 0) { 1812 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1813 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 1814 } else { 1815 if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 1816 value = ""; 1817 *source = ""; 1818 } 1819 1820 return (value); 1821 } 1822 1823 /* 1824 * Internal function for getting a numeric property. Both zfs_prop_get() and 1825 * zfs_prop_get_int() are built using this interface. 1826 * 1827 * Certain properties can be overridden using 'mount -o'. In this case, scan 1828 * the contents of the /etc/mnttab entry, searching for the appropriate options. 1829 * If they differ from the on-disk values, report the current values and mark 1830 * the source "temporary". 1831 */ 1832 static int 1833 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 1834 char **source, uint64_t *val) 1835 { 1836 zfs_cmd_t zc = { 0 }; 1837 struct mnttab mnt; 1838 char *mntopt_on = NULL; 1839 char *mntopt_off = NULL; 1840 1841 *source = NULL; 1842 1843 switch (prop) { 1844 case ZFS_PROP_ATIME: 1845 mntopt_on = MNTOPT_ATIME; 1846 mntopt_off = MNTOPT_NOATIME; 1847 break; 1848 1849 case ZFS_PROP_DEVICES: 1850 mntopt_on = MNTOPT_DEVICES; 1851 mntopt_off = MNTOPT_NODEVICES; 1852 break; 1853 1854 case ZFS_PROP_EXEC: 1855 mntopt_on = MNTOPT_EXEC; 1856 mntopt_off = MNTOPT_NOEXEC; 1857 break; 1858 1859 case ZFS_PROP_READONLY: 1860 mntopt_on = MNTOPT_RO; 1861 mntopt_off = MNTOPT_RW; 1862 break; 1863 1864 case ZFS_PROP_SETUID: 1865 mntopt_on = MNTOPT_SETUID; 1866 mntopt_off = MNTOPT_NOSETUID; 1867 break; 1868 1869 case ZFS_PROP_XATTR: 1870 mntopt_on = MNTOPT_XATTR; 1871 mntopt_off = MNTOPT_NOXATTR; 1872 break; 1873 1874 case ZFS_PROP_NBMAND: 1875 mntopt_on = MNTOPT_NBMAND; 1876 mntopt_off = MNTOPT_NONBMAND; 1877 break; 1878 } 1879 1880 /* 1881 * Because looking up the mount options is potentially expensive 1882 * (iterating over all of /etc/mnttab), we defer its calculation until 1883 * we're looking up a property which requires its presence. 1884 */ 1885 if (!zhp->zfs_mntcheck && 1886 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 1887 struct mnttab entry, search = { 0 }; 1888 FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 1889 1890 search.mnt_special = (char *)zhp->zfs_name; 1891 search.mnt_fstype = MNTTYPE_ZFS; 1892 rewind(mnttab); 1893 1894 if (getmntany(mnttab, &entry, &search) == 0) { 1895 zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 1896 entry.mnt_mntopts); 1897 if (zhp->zfs_mntopts == NULL) 1898 return (-1); 1899 } 1900 1901 zhp->zfs_mntcheck = B_TRUE; 1902 } 1903 1904 if (zhp->zfs_mntopts == NULL) 1905 mnt.mnt_mntopts = ""; 1906 else 1907 mnt.mnt_mntopts = zhp->zfs_mntopts; 1908 1909 switch (prop) { 1910 case ZFS_PROP_ATIME: 1911 case ZFS_PROP_DEVICES: 1912 case ZFS_PROP_EXEC: 1913 case ZFS_PROP_READONLY: 1914 case ZFS_PROP_SETUID: 1915 case ZFS_PROP_XATTR: 1916 case ZFS_PROP_NBMAND: 1917 *val = getprop_uint64(zhp, prop, source); 1918 1919 if (hasmntopt(&mnt, mntopt_on) && !*val) { 1920 *val = B_TRUE; 1921 if (src) 1922 *src = ZPROP_SRC_TEMPORARY; 1923 } else if (hasmntopt(&mnt, mntopt_off) && *val) { 1924 *val = B_FALSE; 1925 if (src) 1926 *src = ZPROP_SRC_TEMPORARY; 1927 } 1928 break; 1929 1930 case ZFS_PROP_CANMOUNT: 1931 *val = getprop_uint64(zhp, prop, source); 1932 if (*val == 0) 1933 *source = zhp->zfs_name; 1934 else 1935 *source = ""; /* default */ 1936 break; 1937 1938 case ZFS_PROP_QUOTA: 1939 case ZFS_PROP_RESERVATION: 1940 *val = getprop_uint64(zhp, prop, source); 1941 if (*val == 0) 1942 *source = ""; /* default */ 1943 else 1944 *source = zhp->zfs_name; 1945 break; 1946 1947 case ZFS_PROP_MOUNTED: 1948 *val = (zhp->zfs_mntopts != NULL); 1949 break; 1950 1951 case ZFS_PROP_NUMCLONES: 1952 *val = zhp->zfs_dmustats.dds_num_clones; 1953 break; 1954 1955 case ZFS_PROP_VERSION: 1956 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1957 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) || 1958 (zc.zc_cookie == 0)) { 1959 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 1960 "unable to get version property")); 1961 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 1962 dgettext(TEXT_DOMAIN, "internal error"))); 1963 } 1964 *val = zc.zc_cookie; 1965 break; 1966 1967 default: 1968 switch (zfs_prop_get_type(prop)) { 1969 case PROP_TYPE_NUMBER: 1970 case PROP_TYPE_INDEX: 1971 *val = getprop_uint64(zhp, prop, source); 1972 break; 1973 1974 case PROP_TYPE_STRING: 1975 default: 1976 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 1977 "cannot get non-numeric property")); 1978 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 1979 dgettext(TEXT_DOMAIN, "internal error"))); 1980 } 1981 } 1982 1983 return (0); 1984 } 1985 1986 /* 1987 * Calculate the source type, given the raw source string. 1988 */ 1989 static void 1990 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1991 char *statbuf, size_t statlen) 1992 { 1993 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1994 return; 1995 1996 if (source == NULL) { 1997 *srctype = ZPROP_SRC_NONE; 1998 } else if (source[0] == '\0') { 1999 *srctype = ZPROP_SRC_DEFAULT; 2000 } else { 2001 if (strcmp(source, zhp->zfs_name) == 0) { 2002 *srctype = ZPROP_SRC_LOCAL; 2003 } else { 2004 (void) strlcpy(statbuf, source, statlen); 2005 *srctype = ZPROP_SRC_INHERITED; 2006 } 2007 } 2008 2009 } 2010 2011 /* 2012 * Retrieve a property from the given object. If 'literal' is specified, then 2013 * numbers are left as exact values. Otherwise, numbers are converted to a 2014 * human-readable form. 2015 * 2016 * Returns 0 on success, or -1 on error. 2017 */ 2018 int 2019 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2020 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2021 { 2022 char *source = NULL; 2023 uint64_t val; 2024 char *str; 2025 const char *root; 2026 const char *strval; 2027 2028 /* 2029 * Check to see if this property applies to our object 2030 */ 2031 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2032 return (-1); 2033 2034 if (src) 2035 *src = ZPROP_SRC_NONE; 2036 2037 switch (prop) { 2038 case ZFS_PROP_CREATION: 2039 /* 2040 * 'creation' is a time_t stored in the statistics. We convert 2041 * this into a string unless 'literal' is specified. 2042 */ 2043 { 2044 val = getprop_uint64(zhp, prop, &source); 2045 time_t time = (time_t)val; 2046 struct tm t; 2047 2048 if (literal || 2049 localtime_r(&time, &t) == NULL || 2050 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2051 &t) == 0) 2052 (void) snprintf(propbuf, proplen, "%llu", val); 2053 } 2054 break; 2055 2056 case ZFS_PROP_MOUNTPOINT: 2057 /* 2058 * Getting the precise mountpoint can be tricky. 2059 * 2060 * - for 'none' or 'legacy', return those values. 2061 * - for default mountpoints, construct it as /zfs/<dataset> 2062 * - for inherited mountpoints, we want to take everything 2063 * after our ancestor and append it to the inherited value. 2064 * 2065 * If the pool has an alternate root, we want to prepend that 2066 * root to any values we return. 2067 */ 2068 root = zhp->zfs_root; 2069 str = getprop_string(zhp, prop, &source); 2070 2071 if (str[0] == '\0') { 2072 (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2073 root, zhp->zfs_name); 2074 } else if (str[0] == '/') { 2075 const char *relpath = zhp->zfs_name + strlen(source); 2076 2077 if (relpath[0] == '/') 2078 relpath++; 2079 if (str[1] == '\0') 2080 str++; 2081 2082 if (relpath[0] == '\0') 2083 (void) snprintf(propbuf, proplen, "%s%s", 2084 root, str); 2085 else 2086 (void) snprintf(propbuf, proplen, "%s%s%s%s", 2087 root, str, relpath[0] == '@' ? "" : "/", 2088 relpath); 2089 } else { 2090 /* 'legacy' or 'none' */ 2091 (void) strlcpy(propbuf, str, proplen); 2092 } 2093 2094 break; 2095 2096 case ZFS_PROP_ORIGIN: 2097 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2098 proplen); 2099 /* 2100 * If there is no parent at all, return failure to indicate that 2101 * it doesn't apply to this dataset. 2102 */ 2103 if (propbuf[0] == '\0') 2104 return (-1); 2105 break; 2106 2107 case ZFS_PROP_QUOTA: 2108 case ZFS_PROP_RESERVATION: 2109 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2110 return (-1); 2111 2112 /* 2113 * If quota or reservation is 0, we translate this into 'none' 2114 * (unless literal is set), and indicate that it's the default 2115 * value. Otherwise, we print the number nicely and indicate 2116 * that its set locally. 2117 */ 2118 if (val == 0) { 2119 if (literal) 2120 (void) strlcpy(propbuf, "0", proplen); 2121 else 2122 (void) strlcpy(propbuf, "none", proplen); 2123 } else { 2124 if (literal) 2125 (void) snprintf(propbuf, proplen, "%llu", 2126 (u_longlong_t)val); 2127 else 2128 zfs_nicenum(val, propbuf, proplen); 2129 } 2130 break; 2131 2132 case ZFS_PROP_COMPRESSRATIO: 2133 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2134 return (-1); 2135 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 2136 val / 100, (longlong_t)val % 100); 2137 break; 2138 2139 case ZFS_PROP_TYPE: 2140 switch (zhp->zfs_type) { 2141 case ZFS_TYPE_FILESYSTEM: 2142 str = "filesystem"; 2143 break; 2144 case ZFS_TYPE_VOLUME: 2145 str = "volume"; 2146 break; 2147 case ZFS_TYPE_SNAPSHOT: 2148 str = "snapshot"; 2149 break; 2150 default: 2151 abort(); 2152 } 2153 (void) snprintf(propbuf, proplen, "%s", str); 2154 break; 2155 2156 case ZFS_PROP_MOUNTED: 2157 /* 2158 * The 'mounted' property is a pseudo-property that described 2159 * whether the filesystem is currently mounted. Even though 2160 * it's a boolean value, the typical values of "on" and "off" 2161 * don't make sense, so we translate to "yes" and "no". 2162 */ 2163 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 2164 src, &source, &val) != 0) 2165 return (-1); 2166 if (val) 2167 (void) strlcpy(propbuf, "yes", proplen); 2168 else 2169 (void) strlcpy(propbuf, "no", proplen); 2170 break; 2171 2172 case ZFS_PROP_NAME: 2173 /* 2174 * The 'name' property is a pseudo-property derived from the 2175 * dataset name. It is presented as a real property to simplify 2176 * consumers. 2177 */ 2178 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2179 break; 2180 2181 default: 2182 switch (zfs_prop_get_type(prop)) { 2183 case PROP_TYPE_NUMBER: 2184 if (get_numeric_property(zhp, prop, src, 2185 &source, &val) != 0) 2186 return (-1); 2187 if (literal) 2188 (void) snprintf(propbuf, proplen, "%llu", 2189 (u_longlong_t)val); 2190 else 2191 zfs_nicenum(val, propbuf, proplen); 2192 break; 2193 2194 case PROP_TYPE_STRING: 2195 (void) strlcpy(propbuf, 2196 getprop_string(zhp, prop, &source), proplen); 2197 break; 2198 2199 case PROP_TYPE_INDEX: 2200 if (get_numeric_property(zhp, prop, src, 2201 &source, &val) != 0) 2202 return (-1); 2203 if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2204 return (-1); 2205 (void) strlcpy(propbuf, strval, proplen); 2206 break; 2207 2208 default: 2209 abort(); 2210 } 2211 } 2212 2213 get_source(zhp, src, source, statbuf, statlen); 2214 2215 return (0); 2216 } 2217 2218 /* 2219 * Utility function to get the given numeric property. Does no validation that 2220 * the given property is the appropriate type; should only be used with 2221 * hard-coded property types. 2222 */ 2223 uint64_t 2224 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2225 { 2226 char *source; 2227 uint64_t val; 2228 2229 (void) get_numeric_property(zhp, prop, NULL, &source, &val); 2230 2231 return (val); 2232 } 2233 2234 /* 2235 * Similar to zfs_prop_get(), but returns the value as an integer. 2236 */ 2237 int 2238 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2239 zprop_source_t *src, char *statbuf, size_t statlen) 2240 { 2241 char *source; 2242 2243 /* 2244 * Check to see if this property applies to our object 2245 */ 2246 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2247 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 2248 dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 2249 zfs_prop_to_name(prop))); 2250 } 2251 2252 if (src) 2253 *src = ZPROP_SRC_NONE; 2254 2255 if (get_numeric_property(zhp, prop, src, &source, value) != 0) 2256 return (-1); 2257 2258 get_source(zhp, src, source, statbuf, statlen); 2259 2260 return (0); 2261 } 2262 2263 /* 2264 * Returns the name of the given zfs handle. 2265 */ 2266 const char * 2267 zfs_get_name(const zfs_handle_t *zhp) 2268 { 2269 return (zhp->zfs_name); 2270 } 2271 2272 /* 2273 * Returns the type of the given zfs handle. 2274 */ 2275 zfs_type_t 2276 zfs_get_type(const zfs_handle_t *zhp) 2277 { 2278 return (zhp->zfs_type); 2279 } 2280 2281 /* 2282 * Iterate over all child filesystems 2283 */ 2284 int 2285 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2286 { 2287 zfs_cmd_t zc = { 0 }; 2288 zfs_handle_t *nzhp; 2289 int ret; 2290 2291 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 2292 return (0); 2293 2294 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2295 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2296 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2297 /* 2298 * Ignore private dataset names. 2299 */ 2300 if (dataset_name_hidden(zc.zc_name)) 2301 continue; 2302 2303 /* 2304 * Silently ignore errors, as the only plausible explanation is 2305 * that the pool has since been removed. 2306 */ 2307 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2308 zc.zc_name)) == NULL) 2309 continue; 2310 2311 if ((ret = func(nzhp, data)) != 0) 2312 return (ret); 2313 } 2314 2315 /* 2316 * An errno value of ESRCH indicates normal completion. If ENOENT is 2317 * returned, then the underlying dataset has been removed since we 2318 * obtained the handle. 2319 */ 2320 if (errno != ESRCH && errno != ENOENT) 2321 return (zfs_standard_error(zhp->zfs_hdl, errno, 2322 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2323 2324 return (0); 2325 } 2326 2327 /* 2328 * Iterate over all snapshots 2329 */ 2330 int 2331 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2332 { 2333 zfs_cmd_t zc = { 0 }; 2334 zfs_handle_t *nzhp; 2335 int ret; 2336 2337 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 2338 return (0); 2339 2340 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2341 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 2342 &zc) == 0; 2343 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2344 2345 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2346 zc.zc_name)) == NULL) 2347 continue; 2348 2349 if ((ret = func(nzhp, data)) != 0) 2350 return (ret); 2351 } 2352 2353 /* 2354 * An errno value of ESRCH indicates normal completion. If ENOENT is 2355 * returned, then the underlying dataset has been removed since we 2356 * obtained the handle. Silently ignore this case, and return success. 2357 */ 2358 if (errno != ESRCH && errno != ENOENT) 2359 return (zfs_standard_error(zhp->zfs_hdl, errno, 2360 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2361 2362 return (0); 2363 } 2364 2365 /* 2366 * Iterate over all children, snapshots and filesystems 2367 */ 2368 int 2369 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2370 { 2371 int ret; 2372 2373 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 2374 return (ret); 2375 2376 return (zfs_iter_snapshots(zhp, func, data)); 2377 } 2378 2379 /* 2380 * Given a complete name, return just the portion that refers to the parent. 2381 * Can return NULL if this is a pool. 2382 */ 2383 static int 2384 parent_name(const char *path, char *buf, size_t buflen) 2385 { 2386 char *loc; 2387 2388 if ((loc = strrchr(path, '/')) == NULL) 2389 return (-1); 2390 2391 (void) strncpy(buf, path, MIN(buflen, loc - path)); 2392 buf[loc - path] = '\0'; 2393 2394 return (0); 2395 } 2396 2397 /* 2398 * If accept_ancestor is false, then check to make sure that the given path has 2399 * a parent, and that it exists. If accept_ancestor is true, then find the 2400 * closest existing ancestor for the given path. In prefixlen return the 2401 * length of already existing prefix of the given path. We also fetch the 2402 * 'zoned' property, which is used to validate property settings when creating 2403 * new datasets. 2404 */ 2405 static int 2406 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 2407 boolean_t accept_ancestor, int *prefixlen) 2408 { 2409 zfs_cmd_t zc = { 0 }; 2410 char parent[ZFS_MAXNAMELEN]; 2411 char *slash; 2412 zfs_handle_t *zhp; 2413 char errbuf[1024]; 2414 2415 (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 2416 path); 2417 2418 /* get parent, and check to see if this is just a pool */ 2419 if (parent_name(path, parent, sizeof (parent)) != 0) { 2420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2421 "missing dataset name")); 2422 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2423 } 2424 2425 /* check to see if the pool exists */ 2426 if ((slash = strchr(parent, '/')) == NULL) 2427 slash = parent + strlen(parent); 2428 (void) strncpy(zc.zc_name, parent, slash - parent); 2429 zc.zc_name[slash - parent] = '\0'; 2430 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2431 errno == ENOENT) { 2432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2433 "no such pool '%s'"), zc.zc_name); 2434 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2435 } 2436 2437 /* check to see if the parent dataset exists */ 2438 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 2439 if (errno == ENOENT && accept_ancestor) { 2440 /* 2441 * Go deeper to find an ancestor, give up on top level. 2442 */ 2443 if (parent_name(parent, parent, sizeof (parent)) != 0) { 2444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2445 "no such pool '%s'"), zc.zc_name); 2446 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2447 } 2448 } else if (errno == ENOENT) { 2449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2450 "parent does not exist")); 2451 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2452 } else 2453 return (zfs_standard_error(hdl, errno, errbuf)); 2454 } 2455 2456 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2457 /* we are in a non-global zone, but parent is in the global zone */ 2458 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 2459 (void) zfs_standard_error(hdl, EPERM, errbuf); 2460 zfs_close(zhp); 2461 return (-1); 2462 } 2463 2464 /* make sure parent is a filesystem */ 2465 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 2466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2467 "parent is not a filesystem")); 2468 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 2469 zfs_close(zhp); 2470 return (-1); 2471 } 2472 2473 zfs_close(zhp); 2474 if (prefixlen != NULL) 2475 *prefixlen = strlen(parent); 2476 return (0); 2477 } 2478 2479 /* 2480 * Finds whether the dataset of the given type(s) exists. 2481 */ 2482 boolean_t 2483 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 2484 { 2485 zfs_handle_t *zhp; 2486 2487 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 2488 return (B_FALSE); 2489 2490 /* 2491 * Try to get stats for the dataset, which will tell us if it exists. 2492 */ 2493 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 2494 int ds_type = zhp->zfs_type; 2495 2496 zfs_close(zhp); 2497 if (types & ds_type) 2498 return (B_TRUE); 2499 } 2500 return (B_FALSE); 2501 } 2502 2503 /* 2504 * Given a path to 'target', create all the ancestors between 2505 * the prefixlen portion of the path, and the target itself. 2506 * Fail if the initial prefixlen-ancestor does not already exist. 2507 */ 2508 int 2509 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 2510 { 2511 zfs_handle_t *h; 2512 char *cp; 2513 const char *opname; 2514 2515 /* make sure prefix exists */ 2516 cp = target + prefixlen; 2517 if (*cp != '/') { 2518 assert(strchr(cp, '/') == NULL); 2519 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2520 } else { 2521 *cp = '\0'; 2522 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2523 *cp = '/'; 2524 } 2525 if (h == NULL) 2526 return (-1); 2527 zfs_close(h); 2528 2529 /* 2530 * Attempt to create, mount, and share any ancestor filesystems, 2531 * up to the prefixlen-long one. 2532 */ 2533 for (cp = target + prefixlen + 1; 2534 cp = strchr(cp, '/'); *cp = '/', cp++) { 2535 char *logstr; 2536 2537 *cp = '\0'; 2538 2539 h = make_dataset_handle(hdl, target); 2540 if (h) { 2541 /* it already exists, nothing to do here */ 2542 zfs_close(h); 2543 continue; 2544 } 2545 2546 logstr = hdl->libzfs_log_str; 2547 hdl->libzfs_log_str = NULL; 2548 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 2549 NULL) != 0) { 2550 hdl->libzfs_log_str = logstr; 2551 opname = dgettext(TEXT_DOMAIN, "create"); 2552 goto ancestorerr; 2553 } 2554 2555 hdl->libzfs_log_str = logstr; 2556 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2557 if (h == NULL) { 2558 opname = dgettext(TEXT_DOMAIN, "open"); 2559 goto ancestorerr; 2560 } 2561 2562 if (zfs_mount(h, NULL, 0) != 0) { 2563 opname = dgettext(TEXT_DOMAIN, "mount"); 2564 goto ancestorerr; 2565 } 2566 2567 if (zfs_share(h) != 0) { 2568 opname = dgettext(TEXT_DOMAIN, "share"); 2569 goto ancestorerr; 2570 } 2571 2572 zfs_close(h); 2573 } 2574 2575 return (0); 2576 2577 ancestorerr: 2578 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2579 "failed to %s ancestor '%s'"), opname, target); 2580 return (-1); 2581 } 2582 2583 /* 2584 * Creates non-existing ancestors of the given path. 2585 */ 2586 int 2587 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 2588 { 2589 int prefix; 2590 uint64_t zoned; 2591 char *path_copy; 2592 int rc; 2593 2594 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 2595 return (-1); 2596 2597 if ((path_copy = strdup(path)) != NULL) { 2598 rc = create_parents(hdl, path_copy, prefix); 2599 free(path_copy); 2600 } 2601 if (path_copy == NULL || rc != 0) 2602 return (-1); 2603 2604 return (0); 2605 } 2606 2607 /* 2608 * Create a new filesystem or volume. 2609 */ 2610 int 2611 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 2612 nvlist_t *props) 2613 { 2614 zfs_cmd_t zc = { 0 }; 2615 int ret; 2616 uint64_t size = 0; 2617 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 2618 char errbuf[1024]; 2619 uint64_t zoned; 2620 2621 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2622 "cannot create '%s'"), path); 2623 2624 /* validate the path, taking care to note the extended error message */ 2625 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 2626 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2627 2628 /* validate parents exist */ 2629 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2630 return (-1); 2631 2632 /* 2633 * The failure modes when creating a dataset of a different type over 2634 * one that already exists is a little strange. In particular, if you 2635 * try to create a dataset on top of an existing dataset, the ioctl() 2636 * will return ENOENT, not EEXIST. To prevent this from happening, we 2637 * first try to see if the dataset exists. 2638 */ 2639 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 2640 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 2641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2642 "dataset already exists")); 2643 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2644 } 2645 2646 if (type == ZFS_TYPE_VOLUME) 2647 zc.zc_objset_type = DMU_OST_ZVOL; 2648 else 2649 zc.zc_objset_type = DMU_OST_ZFS; 2650 2651 if (props && (props = zfs_validate_properties(hdl, type, props, 2652 zoned, NULL, errbuf)) == 0) 2653 return (-1); 2654 2655 if (type == ZFS_TYPE_VOLUME) { 2656 /* 2657 * If we are creating a volume, the size and block size must 2658 * satisfy a few restraints. First, the blocksize must be a 2659 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 2660 * volsize must be a multiple of the block size, and cannot be 2661 * zero. 2662 */ 2663 if (props == NULL || nvlist_lookup_uint64(props, 2664 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 2665 nvlist_free(props); 2666 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2667 "missing volume size")); 2668 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2669 } 2670 2671 if ((ret = nvlist_lookup_uint64(props, 2672 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2673 &blocksize)) != 0) { 2674 if (ret == ENOENT) { 2675 blocksize = zfs_prop_default_numeric( 2676 ZFS_PROP_VOLBLOCKSIZE); 2677 } else { 2678 nvlist_free(props); 2679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2680 "missing volume block size")); 2681 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2682 } 2683 } 2684 2685 if (size == 0) { 2686 nvlist_free(props); 2687 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2688 "volume size cannot be zero")); 2689 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2690 } 2691 2692 if (size % blocksize != 0) { 2693 nvlist_free(props); 2694 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2695 "volume size must be a multiple of volume block " 2696 "size")); 2697 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2698 } 2699 } 2700 2701 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 2702 return (-1); 2703 nvlist_free(props); 2704 2705 /* create the dataset */ 2706 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2707 2708 if (ret == 0 && type == ZFS_TYPE_VOLUME) { 2709 ret = zvol_create_link(hdl, path); 2710 if (ret) { 2711 (void) zfs_standard_error(hdl, errno, 2712 dgettext(TEXT_DOMAIN, 2713 "Volume successfully created, but device links " 2714 "were not created")); 2715 zcmd_free_nvlists(&zc); 2716 return (-1); 2717 } 2718 } 2719 2720 zcmd_free_nvlists(&zc); 2721 2722 /* check for failure */ 2723 if (ret != 0) { 2724 char parent[ZFS_MAXNAMELEN]; 2725 (void) parent_name(path, parent, sizeof (parent)); 2726 2727 switch (errno) { 2728 case ENOENT: 2729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2730 "no such parent '%s'"), parent); 2731 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2732 2733 case EINVAL: 2734 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2735 "parent '%s' is not a filesystem"), parent); 2736 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2737 2738 case EDOM: 2739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2740 "volume block size must be power of 2 from " 2741 "%u to %uk"), 2742 (uint_t)SPA_MINBLOCKSIZE, 2743 (uint_t)SPA_MAXBLOCKSIZE >> 10); 2744 2745 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2746 2747 case ENOTSUP: 2748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2749 "pool must be upgraded to set this " 2750 "property or value")); 2751 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 2752 2753 #ifdef _ILP32 2754 case EOVERFLOW: 2755 /* 2756 * This platform can't address a volume this big. 2757 */ 2758 if (type == ZFS_TYPE_VOLUME) 2759 return (zfs_error(hdl, EZFS_VOLTOOBIG, 2760 errbuf)); 2761 #endif 2762 /* FALLTHROUGH */ 2763 default: 2764 return (zfs_standard_error(hdl, errno, errbuf)); 2765 } 2766 } 2767 2768 return (0); 2769 } 2770 2771 /* 2772 * Destroys the given dataset. The caller must make sure that the filesystem 2773 * isn't mounted, and that there are no active dependents. 2774 */ 2775 int 2776 zfs_destroy(zfs_handle_t *zhp) 2777 { 2778 zfs_cmd_t zc = { 0 }; 2779 2780 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2781 2782 if (ZFS_IS_VOLUME(zhp)) { 2783 /* 2784 * If user doesn't have permissions to unshare volume, then 2785 * abort the request. This would only happen for a 2786 * non-privileged user. 2787 */ 2788 if (zfs_unshare_iscsi(zhp) != 0) { 2789 return (-1); 2790 } 2791 2792 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2793 return (-1); 2794 2795 zc.zc_objset_type = DMU_OST_ZVOL; 2796 } else { 2797 zc.zc_objset_type = DMU_OST_ZFS; 2798 } 2799 2800 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 2801 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 2802 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 2803 zhp->zfs_name)); 2804 } 2805 2806 remove_mountpoint(zhp); 2807 2808 return (0); 2809 } 2810 2811 struct destroydata { 2812 char *snapname; 2813 boolean_t gotone; 2814 boolean_t closezhp; 2815 }; 2816 2817 static int 2818 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 2819 { 2820 struct destroydata *dd = arg; 2821 zfs_handle_t *szhp; 2822 char name[ZFS_MAXNAMELEN]; 2823 boolean_t closezhp = dd->closezhp; 2824 int rv; 2825 2826 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 2827 (void) strlcat(name, "@", sizeof (name)); 2828 (void) strlcat(name, dd->snapname, sizeof (name)); 2829 2830 szhp = make_dataset_handle(zhp->zfs_hdl, name); 2831 if (szhp) { 2832 dd->gotone = B_TRUE; 2833 zfs_close(szhp); 2834 } 2835 2836 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 2837 (void) zvol_remove_link(zhp->zfs_hdl, name); 2838 /* 2839 * NB: this is simply a best-effort. We don't want to 2840 * return an error, because then we wouldn't visit all 2841 * the volumes. 2842 */ 2843 } 2844 2845 dd->closezhp = B_TRUE; 2846 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 2847 if (closezhp) 2848 zfs_close(zhp); 2849 return (rv); 2850 } 2851 2852 /* 2853 * Destroys all snapshots with the given name in zhp & descendants. 2854 */ 2855 int 2856 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 2857 { 2858 zfs_cmd_t zc = { 0 }; 2859 int ret; 2860 struct destroydata dd = { 0 }; 2861 2862 dd.snapname = snapname; 2863 (void) zfs_remove_link_cb(zhp, &dd); 2864 2865 if (!dd.gotone) { 2866 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 2867 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 2868 zhp->zfs_name, snapname)); 2869 } 2870 2871 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2872 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 2873 2874 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 2875 if (ret != 0) { 2876 char errbuf[1024]; 2877 2878 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2879 "cannot destroy '%s@%s'"), zc.zc_name, snapname); 2880 2881 switch (errno) { 2882 case EEXIST: 2883 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2884 "snapshot is cloned")); 2885 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 2886 2887 default: 2888 return (zfs_standard_error(zhp->zfs_hdl, errno, 2889 errbuf)); 2890 } 2891 } 2892 2893 return (0); 2894 } 2895 2896 /* 2897 * Clones the given dataset. The target must be of the same type as the source. 2898 */ 2899 int 2900 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2901 { 2902 zfs_cmd_t zc = { 0 }; 2903 char parent[ZFS_MAXNAMELEN]; 2904 int ret; 2905 char errbuf[1024]; 2906 libzfs_handle_t *hdl = zhp->zfs_hdl; 2907 zfs_type_t type; 2908 uint64_t zoned; 2909 2910 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2911 2912 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2913 "cannot create '%s'"), target); 2914 2915 /* validate the target name */ 2916 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 2917 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2918 2919 /* validate parents exist */ 2920 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2921 return (-1); 2922 2923 (void) parent_name(target, parent, sizeof (parent)); 2924 2925 /* do the clone */ 2926 if (ZFS_IS_VOLUME(zhp)) { 2927 zc.zc_objset_type = DMU_OST_ZVOL; 2928 type = ZFS_TYPE_VOLUME; 2929 } else { 2930 zc.zc_objset_type = DMU_OST_ZFS; 2931 type = ZFS_TYPE_FILESYSTEM; 2932 } 2933 2934 if (props) { 2935 if ((props = zfs_validate_properties(hdl, type, props, 2936 zoned, zhp, errbuf)) == NULL) 2937 return (-1); 2938 2939 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 2940 nvlist_free(props); 2941 return (-1); 2942 } 2943 2944 nvlist_free(props); 2945 } 2946 2947 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 2948 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 2949 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2950 2951 zcmd_free_nvlists(&zc); 2952 2953 if (ret != 0) { 2954 switch (errno) { 2955 2956 case ENOENT: 2957 /* 2958 * The parent doesn't exist. We should have caught this 2959 * above, but there may a race condition that has since 2960 * destroyed the parent. 2961 * 2962 * At this point, we don't know whether it's the source 2963 * that doesn't exist anymore, or whether the target 2964 * dataset doesn't exist. 2965 */ 2966 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2967 "no such parent '%s'"), parent); 2968 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 2969 2970 case EXDEV: 2971 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2972 "source and target pools differ")); 2973 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 2974 errbuf)); 2975 2976 default: 2977 return (zfs_standard_error(zhp->zfs_hdl, errno, 2978 errbuf)); 2979 } 2980 } else if (ZFS_IS_VOLUME(zhp)) { 2981 ret = zvol_create_link(zhp->zfs_hdl, target); 2982 } 2983 2984 return (ret); 2985 } 2986 2987 typedef struct promote_data { 2988 char cb_mountpoint[MAXPATHLEN]; 2989 const char *cb_target; 2990 const char *cb_errbuf; 2991 uint64_t cb_pivot_txg; 2992 } promote_data_t; 2993 2994 static int 2995 promote_snap_cb(zfs_handle_t *zhp, void *data) 2996 { 2997 promote_data_t *pd = data; 2998 zfs_handle_t *szhp; 2999 char snapname[MAXPATHLEN]; 3000 int rv = 0; 3001 3002 /* We don't care about snapshots after the pivot point */ 3003 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 3004 zfs_close(zhp); 3005 return (0); 3006 } 3007 3008 /* Remove the device link if it's a zvol. */ 3009 if (ZFS_IS_VOLUME(zhp)) 3010 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 3011 3012 /* Check for conflicting names */ 3013 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 3014 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 3015 szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 3016 if (szhp != NULL) { 3017 zfs_close(szhp); 3018 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3019 "snapshot name '%s' from origin \n" 3020 "conflicts with '%s' from target"), 3021 zhp->zfs_name, snapname); 3022 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 3023 } 3024 zfs_close(zhp); 3025 return (rv); 3026 } 3027 3028 static int 3029 promote_snap_done_cb(zfs_handle_t *zhp, void *data) 3030 { 3031 promote_data_t *pd = data; 3032 3033 /* We don't care about snapshots after the pivot point */ 3034 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 3035 /* Create the device link if it's a zvol. */ 3036 if (ZFS_IS_VOLUME(zhp)) 3037 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3038 } 3039 3040 zfs_close(zhp); 3041 return (0); 3042 } 3043 3044 /* 3045 * Promotes the given clone fs to be the clone parent. 3046 */ 3047 int 3048 zfs_promote(zfs_handle_t *zhp) 3049 { 3050 libzfs_handle_t *hdl = zhp->zfs_hdl; 3051 zfs_cmd_t zc = { 0 }; 3052 char parent[MAXPATHLEN]; 3053 char *cp; 3054 int ret; 3055 zfs_handle_t *pzhp; 3056 promote_data_t pd; 3057 char errbuf[1024]; 3058 3059 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3060 "cannot promote '%s'"), zhp->zfs_name); 3061 3062 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3064 "snapshots can not be promoted")); 3065 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3066 } 3067 3068 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 3069 if (parent[0] == '\0') { 3070 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3071 "not a cloned filesystem")); 3072 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3073 } 3074 cp = strchr(parent, '@'); 3075 *cp = '\0'; 3076 3077 /* Walk the snapshots we will be moving */ 3078 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 3079 if (pzhp == NULL) 3080 return (-1); 3081 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 3082 zfs_close(pzhp); 3083 pd.cb_target = zhp->zfs_name; 3084 pd.cb_errbuf = errbuf; 3085 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 3086 if (pzhp == NULL) 3087 return (-1); 3088 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 3089 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 3090 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 3091 if (ret != 0) { 3092 zfs_close(pzhp); 3093 return (-1); 3094 } 3095 3096 /* issue the ioctl */ 3097 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 3098 sizeof (zc.zc_value)); 3099 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3100 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 3101 3102 if (ret != 0) { 3103 int save_errno = errno; 3104 3105 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 3106 zfs_close(pzhp); 3107 3108 switch (save_errno) { 3109 case EEXIST: 3110 /* 3111 * There is a conflicting snapshot name. We 3112 * should have caught this above, but they could 3113 * have renamed something in the mean time. 3114 */ 3115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3116 "conflicting snapshot name from parent '%s'"), 3117 parent); 3118 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3119 3120 default: 3121 return (zfs_standard_error(hdl, save_errno, errbuf)); 3122 } 3123 } else { 3124 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3125 } 3126 3127 zfs_close(pzhp); 3128 return (ret); 3129 } 3130 3131 struct createdata { 3132 const char *cd_snapname; 3133 int cd_ifexists; 3134 }; 3135 3136 static int 3137 zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 3138 { 3139 struct createdata *cd = arg; 3140 int ret; 3141 3142 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3143 char name[MAXPATHLEN]; 3144 3145 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 3146 (void) strlcat(name, "@", sizeof (name)); 3147 (void) strlcat(name, cd->cd_snapname, sizeof (name)); 3148 (void) zvol_create_link_common(zhp->zfs_hdl, name, 3149 cd->cd_ifexists); 3150 /* 3151 * NB: this is simply a best-effort. We don't want to 3152 * return an error, because then we wouldn't visit all 3153 * the volumes. 3154 */ 3155 } 3156 3157 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 3158 3159 zfs_close(zhp); 3160 3161 return (ret); 3162 } 3163 3164 /* 3165 * Takes a snapshot of the given dataset. 3166 */ 3167 int 3168 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3169 { 3170 const char *delim; 3171 char *parent; 3172 zfs_handle_t *zhp; 3173 zfs_cmd_t zc = { 0 }; 3174 int ret; 3175 char errbuf[1024]; 3176 3177 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3178 "cannot snapshot '%s'"), path); 3179 3180 /* validate the target name */ 3181 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 3182 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3183 3184 /* make sure the parent exists and is of the appropriate type */ 3185 delim = strchr(path, '@'); 3186 if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 3187 return (-1); 3188 (void) strncpy(parent, path, delim - path); 3189 parent[delim - path] = '\0'; 3190 3191 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3192 ZFS_TYPE_VOLUME)) == NULL) { 3193 free(parent); 3194 return (-1); 3195 } 3196 3197 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3198 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 3199 if (ZFS_IS_VOLUME(zhp)) 3200 zc.zc_objset_type = DMU_OST_ZVOL; 3201 else 3202 zc.zc_objset_type = DMU_OST_ZFS; 3203 zc.zc_cookie = recursive; 3204 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 3205 3206 /* 3207 * if it was recursive, the one that actually failed will be in 3208 * zc.zc_name. 3209 */ 3210 if (ret != 0) 3211 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3212 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 3213 3214 if (ret == 0 && recursive) { 3215 struct createdata cd; 3216 3217 cd.cd_snapname = delim + 1; 3218 cd.cd_ifexists = B_FALSE; 3219 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 3220 } 3221 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 3222 ret = zvol_create_link(zhp->zfs_hdl, path); 3223 if (ret != 0) { 3224 (void) zfs_standard_error(hdl, errno, 3225 dgettext(TEXT_DOMAIN, 3226 "Volume successfully snapshotted, but device links " 3227 "were not created")); 3228 free(parent); 3229 zfs_close(zhp); 3230 return (-1); 3231 } 3232 } 3233 3234 if (ret != 0) 3235 (void) zfs_standard_error(hdl, errno, errbuf); 3236 3237 free(parent); 3238 zfs_close(zhp); 3239 3240 return (ret); 3241 } 3242 3243 /* 3244 * Destroy any more recent snapshots. We invoke this callback on any dependents 3245 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3246 * is a dependent and we should just destroy it without checking the transaction 3247 * group. 3248 */ 3249 typedef struct rollback_data { 3250 const char *cb_target; /* the snapshot */ 3251 uint64_t cb_create; /* creation time reference */ 3252 prop_changelist_t *cb_clp; /* changelist pointer */ 3253 int cb_error; 3254 boolean_t cb_dependent; 3255 } rollback_data_t; 3256 3257 static int 3258 rollback_destroy(zfs_handle_t *zhp, void *data) 3259 { 3260 rollback_data_t *cbp = data; 3261 3262 if (!cbp->cb_dependent) { 3263 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 3264 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 3265 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 3266 cbp->cb_create) { 3267 char *logstr; 3268 3269 cbp->cb_dependent = B_TRUE; 3270 if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 3271 cbp) != 0) 3272 cbp->cb_error = 1; 3273 cbp->cb_dependent = B_FALSE; 3274 3275 logstr = zhp->zfs_hdl->libzfs_log_str; 3276 zhp->zfs_hdl->libzfs_log_str = NULL; 3277 if (zfs_destroy(zhp) != 0) 3278 cbp->cb_error = 1; 3279 else 3280 changelist_remove(cbp->cb_clp, zhp->zfs_name); 3281 zhp->zfs_hdl->libzfs_log_str = logstr; 3282 } 3283 } else { 3284 if (zfs_destroy(zhp) != 0) 3285 cbp->cb_error = 1; 3286 else 3287 changelist_remove(cbp->cb_clp, zhp->zfs_name); 3288 } 3289 3290 zfs_close(zhp); 3291 return (0); 3292 } 3293 3294 /* 3295 * Rollback the dataset to its latest snapshot. 3296 */ 3297 static int 3298 do_rollback(zfs_handle_t *zhp) 3299 { 3300 int ret; 3301 zfs_cmd_t zc = { 0 }; 3302 3303 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3304 zhp->zfs_type == ZFS_TYPE_VOLUME); 3305 3306 if (zhp->zfs_type == ZFS_TYPE_VOLUME && 3307 zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3308 return (-1); 3309 3310 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3311 3312 if (ZFS_IS_VOLUME(zhp)) 3313 zc.zc_objset_type = DMU_OST_ZVOL; 3314 else 3315 zc.zc_objset_type = DMU_OST_ZFS; 3316 3317 /* 3318 * We rely on the consumer to verify that there are no newer snapshots 3319 * for the given dataset. Given these constraints, we can simply pass 3320 * the name on to the ioctl() call. There is still an unlikely race 3321 * condition where the user has taken a snapshot since we verified that 3322 * this was the most recent. 3323 */ 3324 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 3325 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3326 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 3327 zhp->zfs_name); 3328 } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3329 ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3330 } 3331 3332 return (ret); 3333 } 3334 3335 /* 3336 * Given a dataset, rollback to a specific snapshot, discarding any 3337 * data changes since then and making it the active dataset. 3338 * 3339 * Any snapshots more recent than the target are destroyed, along with 3340 * their dependents. 3341 */ 3342 int 3343 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 3344 { 3345 int ret; 3346 rollback_data_t cb = { 0 }; 3347 prop_changelist_t *clp; 3348 3349 /* 3350 * Unmount all dependendents of the dataset and the dataset itself. 3351 * The list we need to gather is the same as for doing rename 3352 */ 3353 clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 3354 if (clp == NULL) 3355 return (-1); 3356 3357 if ((ret = changelist_prefix(clp)) != 0) 3358 goto out; 3359 3360 /* 3361 * Destroy all recent snapshots and its dependends. 3362 */ 3363 cb.cb_target = snap->zfs_name; 3364 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3365 cb.cb_clp = clp; 3366 (void) zfs_iter_children(zhp, rollback_destroy, &cb); 3367 3368 if ((ret = cb.cb_error) != 0) { 3369 (void) changelist_postfix(clp); 3370 goto out; 3371 } 3372 3373 /* 3374 * Now that we have verified that the snapshot is the latest, 3375 * rollback to the given snapshot. 3376 */ 3377 ret = do_rollback(zhp); 3378 3379 if (ret != 0) { 3380 (void) changelist_postfix(clp); 3381 goto out; 3382 } 3383 3384 /* 3385 * We only want to re-mount the filesystem if it was mounted in the 3386 * first place. 3387 */ 3388 ret = changelist_postfix(clp); 3389 3390 out: 3391 changelist_free(clp); 3392 return (ret); 3393 } 3394 3395 /* 3396 * Iterate over all dependents for a given dataset. This includes both 3397 * hierarchical dependents (children) and data dependents (snapshots and 3398 * clones). The bulk of the processing occurs in get_dependents() in 3399 * libzfs_graph.c. 3400 */ 3401 int 3402 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 3403 zfs_iter_f func, void *data) 3404 { 3405 char **dependents; 3406 size_t count; 3407 int i; 3408 zfs_handle_t *child; 3409 int ret = 0; 3410 3411 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 3412 &dependents, &count) != 0) 3413 return (-1); 3414 3415 for (i = 0; i < count; i++) { 3416 if ((child = make_dataset_handle(zhp->zfs_hdl, 3417 dependents[i])) == NULL) 3418 continue; 3419 3420 if ((ret = func(child, data)) != 0) 3421 break; 3422 } 3423 3424 for (i = 0; i < count; i++) 3425 free(dependents[i]); 3426 free(dependents); 3427 3428 return (ret); 3429 } 3430 3431 /* 3432 * Renames the given dataset. 3433 */ 3434 int 3435 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3436 { 3437 int ret; 3438 zfs_cmd_t zc = { 0 }; 3439 char *delim; 3440 prop_changelist_t *cl = NULL; 3441 zfs_handle_t *zhrp = NULL; 3442 char *parentname = NULL; 3443 char parent[ZFS_MAXNAMELEN]; 3444 libzfs_handle_t *hdl = zhp->zfs_hdl; 3445 char errbuf[1024]; 3446 3447 /* if we have the same exact name, just return success */ 3448 if (strcmp(zhp->zfs_name, target) == 0) 3449 return (0); 3450 3451 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3452 "cannot rename to '%s'"), target); 3453 3454 /* 3455 * Make sure the target name is valid 3456 */ 3457 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3458 if ((strchr(target, '@') == NULL) || 3459 *target == '@') { 3460 /* 3461 * Snapshot target name is abbreviated, 3462 * reconstruct full dataset name 3463 */ 3464 (void) strlcpy(parent, zhp->zfs_name, 3465 sizeof (parent)); 3466 delim = strchr(parent, '@'); 3467 if (strchr(target, '@') == NULL) 3468 *(++delim) = '\0'; 3469 else 3470 *delim = '\0'; 3471 (void) strlcat(parent, target, sizeof (parent)); 3472 target = parent; 3473 } else { 3474 /* 3475 * Make sure we're renaming within the same dataset. 3476 */ 3477 delim = strchr(target, '@'); 3478 if (strncmp(zhp->zfs_name, target, delim - target) 3479 != 0 || zhp->zfs_name[delim - target] != '@') { 3480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3481 "snapshots must be part of same " 3482 "dataset")); 3483 return (zfs_error(hdl, EZFS_CROSSTARGET, 3484 errbuf)); 3485 } 3486 } 3487 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 3488 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3489 } else { 3490 if (recursive) { 3491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3492 "recursive rename must be a snapshot")); 3493 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3494 } 3495 3496 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 3497 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3498 uint64_t unused; 3499 3500 /* validate parents */ 3501 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3502 return (-1); 3503 3504 (void) parent_name(target, parent, sizeof (parent)); 3505 3506 /* make sure we're in the same pool */ 3507 verify((delim = strchr(target, '/')) != NULL); 3508 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3509 zhp->zfs_name[delim - target] != '/') { 3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3511 "datasets must be within same pool")); 3512 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3513 } 3514 3515 /* new name cannot be a child of the current dataset name */ 3516 if (strncmp(parent, zhp->zfs_name, 3517 strlen(zhp->zfs_name)) == 0) { 3518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3519 "New dataset name cannot be a descendent of " 3520 "current dataset name")); 3521 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3522 } 3523 } 3524 3525 (void) snprintf(errbuf, sizeof (errbuf), 3526 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 3527 3528 if (getzoneid() == GLOBAL_ZONEID && 3529 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 3530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3531 "dataset is used in a non-global zone")); 3532 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3533 } 3534 3535 if (recursive) { 3536 struct destroydata dd; 3537 3538 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 3539 if (parentname == NULL) { 3540 ret = -1; 3541 goto error; 3542 } 3543 delim = strchr(parentname, '@'); 3544 *delim = '\0'; 3545 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 3546 if (zhrp == NULL) { 3547 ret = -1; 3548 goto error; 3549 } 3550 3551 dd.snapname = delim + 1; 3552 dd.gotone = B_FALSE; 3553 dd.closezhp = B_TRUE; 3554 3555 /* We remove any zvol links prior to renaming them */ 3556 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 3557 if (ret) { 3558 goto error; 3559 } 3560 } else { 3561 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 3562 return (-1); 3563 3564 if (changelist_haszonedchild(cl)) { 3565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3566 "child dataset with inherited mountpoint is used " 3567 "in a non-global zone")); 3568 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 3569 goto error; 3570 } 3571 3572 if ((ret = changelist_prefix(cl)) != 0) 3573 goto error; 3574 } 3575 3576 if (ZFS_IS_VOLUME(zhp)) 3577 zc.zc_objset_type = DMU_OST_ZVOL; 3578 else 3579 zc.zc_objset_type = DMU_OST_ZFS; 3580 3581 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3582 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 3583 3584 zc.zc_cookie = recursive; 3585 3586 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 3587 /* 3588 * if it was recursive, the one that actually failed will 3589 * be in zc.zc_name 3590 */ 3591 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3592 "cannot rename '%s'"), zc.zc_name); 3593 3594 if (recursive && errno == EEXIST) { 3595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3596 "a child dataset already has a snapshot " 3597 "with the new name")); 3598 (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 3599 } else { 3600 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 3601 } 3602 3603 /* 3604 * On failure, we still want to remount any filesystems that 3605 * were previously mounted, so we don't alter the system state. 3606 */ 3607 if (recursive) { 3608 struct createdata cd; 3609 3610 /* only create links for datasets that had existed */ 3611 cd.cd_snapname = delim + 1; 3612 cd.cd_ifexists = B_TRUE; 3613 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 3614 &cd); 3615 } else { 3616 (void) changelist_postfix(cl); 3617 } 3618 } else { 3619 if (recursive) { 3620 struct createdata cd; 3621 3622 /* only create links for datasets that had existed */ 3623 cd.cd_snapname = strchr(target, '@') + 1; 3624 cd.cd_ifexists = B_TRUE; 3625 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 3626 &cd); 3627 } else { 3628 changelist_rename(cl, zfs_get_name(zhp), target); 3629 ret = changelist_postfix(cl); 3630 } 3631 } 3632 3633 error: 3634 if (parentname) { 3635 free(parentname); 3636 } 3637 if (zhrp) { 3638 zfs_close(zhrp); 3639 } 3640 if (cl) { 3641 changelist_free(cl); 3642 } 3643 return (ret); 3644 } 3645 3646 /* 3647 * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3648 * poke devfsadm to create the /dev link, and then wait for the link to appear. 3649 */ 3650 int 3651 zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3652 { 3653 return (zvol_create_link_common(hdl, dataset, B_FALSE)); 3654 } 3655 3656 static int 3657 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 3658 { 3659 zfs_cmd_t zc = { 0 }; 3660 di_devlink_handle_t dhdl; 3661 priv_set_t *priv_effective; 3662 int privileged; 3663 3664 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3665 3666 /* 3667 * Issue the appropriate ioctl. 3668 */ 3669 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3670 switch (errno) { 3671 case EEXIST: 3672 /* 3673 * Silently ignore the case where the link already 3674 * exists. This allows 'zfs volinit' to be run multiple 3675 * times without errors. 3676 */ 3677 return (0); 3678 3679 case ENOENT: 3680 /* 3681 * Dataset does not exist in the kernel. If we 3682 * don't care (see zfs_rename), then ignore the 3683 * error quietly. 3684 */ 3685 if (ifexists) { 3686 return (0); 3687 } 3688 3689 /* FALLTHROUGH */ 3690 3691 default: 3692 return (zfs_standard_error_fmt(hdl, errno, 3693 dgettext(TEXT_DOMAIN, "cannot create device links " 3694 "for '%s'"), dataset)); 3695 } 3696 } 3697 3698 /* 3699 * If privileged call devfsadm and wait for the links to 3700 * magically appear. 3701 * Otherwise, print out an informational message. 3702 */ 3703 3704 priv_effective = priv_allocset(); 3705 (void) getppriv(PRIV_EFFECTIVE, priv_effective); 3706 privileged = (priv_isfullset(priv_effective) == B_TRUE); 3707 priv_freeset(priv_effective); 3708 3709 if (privileged) { 3710 if ((dhdl = di_devlink_init(ZFS_DRIVER, 3711 DI_MAKE_LINK)) == NULL) { 3712 zfs_error_aux(hdl, strerror(errno)); 3713 (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 3714 dgettext(TEXT_DOMAIN, "cannot create device links " 3715 "for '%s'"), dataset); 3716 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 3717 return (-1); 3718 } else { 3719 (void) di_devlink_fini(&dhdl); 3720 } 3721 } else { 3722 char pathname[MAXPATHLEN]; 3723 struct stat64 statbuf; 3724 int i; 3725 3726 #define MAX_WAIT 10 3727 3728 /* 3729 * This is the poor mans way of waiting for the link 3730 * to show up. If after 10 seconds we still don't 3731 * have it, then print out a message. 3732 */ 3733 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 3734 dataset); 3735 3736 for (i = 0; i != MAX_WAIT; i++) { 3737 if (stat64(pathname, &statbuf) == 0) 3738 break; 3739 (void) sleep(1); 3740 } 3741 if (i == MAX_WAIT) 3742 (void) printf(gettext("%s may not be immediately " 3743 "available\n"), pathname); 3744 } 3745 3746 return (0); 3747 } 3748 3749 /* 3750 * Remove a minor node for the given zvol and the associated /dev links. 3751 */ 3752 int 3753 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3754 { 3755 zfs_cmd_t zc = { 0 }; 3756 3757 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3758 3759 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3760 switch (errno) { 3761 case ENXIO: 3762 /* 3763 * Silently ignore the case where the link no longer 3764 * exists, so that 'zfs volfini' can be run multiple 3765 * times without errors. 3766 */ 3767 return (0); 3768 3769 default: 3770 return (zfs_standard_error_fmt(hdl, errno, 3771 dgettext(TEXT_DOMAIN, "cannot remove device " 3772 "links for '%s'"), dataset)); 3773 } 3774 } 3775 3776 return (0); 3777 } 3778 3779 nvlist_t * 3780 zfs_get_user_props(zfs_handle_t *zhp) 3781 { 3782 return (zhp->zfs_user_props); 3783 } 3784 3785 /* 3786 * This function is used by 'zfs list' to determine the exact set of columns to 3787 * display, and their maximum widths. This does two main things: 3788 * 3789 * - If this is a list of all properties, then expand the list to include 3790 * all native properties, and set a flag so that for each dataset we look 3791 * for new unique user properties and add them to the list. 3792 * 3793 * - For non fixed-width properties, keep track of the maximum width seen 3794 * so that we can size the column appropriately. 3795 */ 3796 int 3797 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 3798 { 3799 libzfs_handle_t *hdl = zhp->zfs_hdl; 3800 zprop_list_t *entry; 3801 zprop_list_t **last, **start; 3802 nvlist_t *userprops, *propval; 3803 nvpair_t *elem; 3804 char *strval; 3805 char buf[ZFS_MAXPROPLEN]; 3806 3807 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 3808 return (-1); 3809 3810 userprops = zfs_get_user_props(zhp); 3811 3812 entry = *plp; 3813 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 3814 /* 3815 * Go through and add any user properties as necessary. We 3816 * start by incrementing our list pointer to the first 3817 * non-native property. 3818 */ 3819 start = plp; 3820 while (*start != NULL) { 3821 if ((*start)->pl_prop == ZPROP_INVAL) 3822 break; 3823 start = &(*start)->pl_next; 3824 } 3825 3826 elem = NULL; 3827 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 3828 /* 3829 * See if we've already found this property in our list. 3830 */ 3831 for (last = start; *last != NULL; 3832 last = &(*last)->pl_next) { 3833 if (strcmp((*last)->pl_user_prop, 3834 nvpair_name(elem)) == 0) 3835 break; 3836 } 3837 3838 if (*last == NULL) { 3839 if ((entry = zfs_alloc(hdl, 3840 sizeof (zprop_list_t))) == NULL || 3841 ((entry->pl_user_prop = zfs_strdup(hdl, 3842 nvpair_name(elem)))) == NULL) { 3843 free(entry); 3844 return (-1); 3845 } 3846 3847 entry->pl_prop = ZPROP_INVAL; 3848 entry->pl_width = strlen(nvpair_name(elem)); 3849 entry->pl_all = B_TRUE; 3850 *last = entry; 3851 } 3852 } 3853 } 3854 3855 /* 3856 * Now go through and check the width of any non-fixed columns 3857 */ 3858 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 3859 if (entry->pl_fixed) 3860 continue; 3861 3862 if (entry->pl_prop != ZPROP_INVAL) { 3863 if (zfs_prop_get(zhp, entry->pl_prop, 3864 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 3865 if (strlen(buf) > entry->pl_width) 3866 entry->pl_width = strlen(buf); 3867 } 3868 } else if (nvlist_lookup_nvlist(userprops, 3869 entry->pl_user_prop, &propval) == 0) { 3870 verify(nvlist_lookup_string(propval, 3871 ZPROP_VALUE, &strval) == 0); 3872 if (strlen(strval) > entry->pl_width) 3873 entry->pl_width = strlen(strval); 3874 } 3875 } 3876 3877 return (0); 3878 } 3879 3880 int 3881 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 3882 { 3883 zfs_cmd_t zc = { 0 }; 3884 nvlist_t *nvp; 3885 gid_t gid; 3886 uid_t uid; 3887 const gid_t *groups; 3888 int group_cnt; 3889 int error; 3890 3891 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 3892 return (no_memory(hdl)); 3893 3894 uid = ucred_geteuid(cred); 3895 gid = ucred_getegid(cred); 3896 group_cnt = ucred_getgroups(cred, &groups); 3897 3898 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 3899 return (1); 3900 3901 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 3902 nvlist_free(nvp); 3903 return (1); 3904 } 3905 3906 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 3907 nvlist_free(nvp); 3908 return (1); 3909 } 3910 3911 if (nvlist_add_uint32_array(nvp, 3912 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 3913 nvlist_free(nvp); 3914 return (1); 3915 } 3916 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3917 3918 if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 3919 return (-1); 3920 3921 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 3922 nvlist_free(nvp); 3923 return (error); 3924 } 3925 3926 int 3927 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 3928 void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 3929 { 3930 zfs_cmd_t zc = { 0 }; 3931 int error; 3932 3933 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3934 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 3935 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 3936 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 3937 zc.zc_share.z_sharetype = operation; 3938 zc.zc_share.z_sharemax = sharemax; 3939 3940 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 3941 return (error); 3942 } 3943