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