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