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_MOUNTPOINT: 915 { 916 namecheck_err_t why; 917 918 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 919 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 920 break; 921 922 if (mountpoint_namecheck(strval, &why)) { 923 switch (why) { 924 case NAME_ERR_LEADING_SLASH: 925 zfs_error_aux(hdl, 926 dgettext(TEXT_DOMAIN, 927 "'%s' must be an absolute path, " 928 "'none', or 'legacy'"), propname); 929 break; 930 case NAME_ERR_TOOLONG: 931 zfs_error_aux(hdl, 932 dgettext(TEXT_DOMAIN, 933 "component of '%s' is too long"), 934 propname); 935 break; 936 } 937 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 938 goto error; 939 } 940 } 941 942 /*FALLTHRU*/ 943 944 case ZFS_PROP_SHARESMB: 945 case ZFS_PROP_SHARENFS: 946 /* 947 * For the mountpoint and sharenfs or sharesmb 948 * properties, check if it can be set in a 949 * global/non-global zone based on 950 * the zoned property value: 951 * 952 * global zone non-global zone 953 * -------------------------------------------------- 954 * zoned=on mountpoint (no) mountpoint (yes) 955 * sharenfs (no) sharenfs (no) 956 * sharesmb (no) sharesmb (no) 957 * 958 * zoned=off mountpoint (yes) N/A 959 * sharenfs (yes) 960 * sharesmb (yes) 961 */ 962 if (zoned) { 963 if (getzoneid() == GLOBAL_ZONEID) { 964 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 965 "'%s' cannot be set on " 966 "dataset in a non-global zone"), 967 propname); 968 (void) zfs_error(hdl, EZFS_ZONED, 969 errbuf); 970 goto error; 971 } else if (prop == ZFS_PROP_SHARENFS || 972 prop == ZFS_PROP_SHARESMB) { 973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 974 "'%s' cannot be set in " 975 "a non-global zone"), propname); 976 (void) zfs_error(hdl, EZFS_ZONED, 977 errbuf); 978 goto error; 979 } 980 } else if (getzoneid() != GLOBAL_ZONEID) { 981 /* 982 * If zoned property is 'off', this must be in 983 * a global zone. If not, something is wrong. 984 */ 985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 986 "'%s' cannot be set while dataset " 987 "'zoned' property is set"), propname); 988 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 989 goto error; 990 } 991 992 /* 993 * At this point, it is legitimate to set the 994 * property. Now we want to make sure that the 995 * property value is valid if it is sharenfs. 996 */ 997 if ((prop == ZFS_PROP_SHARENFS || 998 prop == ZFS_PROP_SHARESMB) && 999 strcmp(strval, "on") != 0 && 1000 strcmp(strval, "off") != 0) { 1001 zfs_share_proto_t proto; 1002 1003 if (prop == ZFS_PROP_SHARESMB) 1004 proto = PROTO_SMB; 1005 else 1006 proto = PROTO_NFS; 1007 1008 /* 1009 * Must be an valid sharing protocol 1010 * option string so init the libshare 1011 * in order to enable the parser and 1012 * then parse the options. We use the 1013 * control API since we don't care about 1014 * the current configuration and don't 1015 * want the overhead of loading it 1016 * until we actually do something. 1017 */ 1018 1019 if (zfs_init_libshare(hdl, 1020 SA_INIT_CONTROL_API) != SA_OK) { 1021 /* 1022 * An error occurred so we can't do 1023 * anything 1024 */ 1025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1026 "'%s' cannot be set: problem " 1027 "in share initialization"), 1028 propname); 1029 (void) zfs_error(hdl, EZFS_BADPROP, 1030 errbuf); 1031 goto error; 1032 } 1033 1034 if (zfs_parse_options(strval, proto) != SA_OK) { 1035 /* 1036 * There was an error in parsing so 1037 * deal with it by issuing an error 1038 * message and leaving after 1039 * uninitializing the the libshare 1040 * interface. 1041 */ 1042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1043 "'%s' cannot be set to invalid " 1044 "options"), propname); 1045 (void) zfs_error(hdl, EZFS_BADPROP, 1046 errbuf); 1047 zfs_uninit_libshare(hdl); 1048 goto error; 1049 } 1050 zfs_uninit_libshare(hdl); 1051 } 1052 1053 break; 1054 case ZFS_PROP_UTF8ONLY: 1055 chosen_utf = (int)intval; 1056 break; 1057 case ZFS_PROP_NORMALIZE: 1058 chosen_normal = (int)intval; 1059 break; 1060 } 1061 1062 /* 1063 * For changes to existing volumes, we have some additional 1064 * checks to enforce. 1065 */ 1066 if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1067 uint64_t volsize = zfs_prop_get_int(zhp, 1068 ZFS_PROP_VOLSIZE); 1069 uint64_t blocksize = zfs_prop_get_int(zhp, 1070 ZFS_PROP_VOLBLOCKSIZE); 1071 char buf[64]; 1072 1073 switch (prop) { 1074 case ZFS_PROP_RESERVATION: 1075 case ZFS_PROP_REFRESERVATION: 1076 if (intval > volsize) { 1077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1078 "'%s' is greater than current " 1079 "volume size"), propname); 1080 (void) zfs_error(hdl, EZFS_BADPROP, 1081 errbuf); 1082 goto error; 1083 } 1084 break; 1085 1086 case ZFS_PROP_VOLSIZE: 1087 if (intval % blocksize != 0) { 1088 zfs_nicenum(blocksize, buf, 1089 sizeof (buf)); 1090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1091 "'%s' must be a multiple of " 1092 "volume block size (%s)"), 1093 propname, buf); 1094 (void) zfs_error(hdl, EZFS_BADPROP, 1095 errbuf); 1096 goto error; 1097 } 1098 1099 if (intval == 0) { 1100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1101 "'%s' cannot be zero"), 1102 propname); 1103 (void) zfs_error(hdl, EZFS_BADPROP, 1104 errbuf); 1105 goto error; 1106 } 1107 break; 1108 } 1109 } 1110 } 1111 1112 /* 1113 * If normalization was chosen, but no UTF8 choice was made, 1114 * enforce rejection of non-UTF8 names. 1115 * 1116 * If normalization was chosen, but rejecting non-UTF8 names 1117 * was explicitly not chosen, it is an error. 1118 */ 1119 if (chosen_normal > 0 && chosen_utf < 0) { 1120 if (nvlist_add_uint64(ret, 1121 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1122 (void) no_memory(hdl); 1123 goto error; 1124 } 1125 } else if (chosen_normal > 0 && chosen_utf == 0) { 1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1127 "'%s' must be set 'on' if normalization chosen"), 1128 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1129 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1130 goto error; 1131 } 1132 1133 /* 1134 * If this is an existing volume, and someone is setting the volsize, 1135 * make sure that it matches the reservation, or add it if necessary. 1136 */ 1137 if (zhp != NULL && type == ZFS_TYPE_VOLUME && 1138 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1139 &intval) == 0) { 1140 uint64_t old_volsize = zfs_prop_get_int(zhp, 1141 ZFS_PROP_VOLSIZE); 1142 uint64_t old_reservation; 1143 uint64_t new_reservation; 1144 zfs_prop_t resv_prop; 1145 1146 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 1147 goto error; 1148 old_reservation = zfs_prop_get_int(zhp, resv_prop); 1149 1150 if (old_volsize == old_reservation && 1151 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 1152 &new_reservation) != 0) { 1153 if (nvlist_add_uint64(ret, 1154 zfs_prop_to_name(resv_prop), intval) != 0) { 1155 (void) no_memory(hdl); 1156 goto error; 1157 } 1158 } 1159 } 1160 return (ret); 1161 1162 error: 1163 nvlist_free(ret); 1164 return (NULL); 1165 } 1166 1167 /* 1168 * Given a property name and value, set the property for the given dataset. 1169 */ 1170 int 1171 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1172 { 1173 zfs_cmd_t zc = { 0 }; 1174 int ret = -1; 1175 prop_changelist_t *cl = NULL; 1176 char errbuf[1024]; 1177 libzfs_handle_t *hdl = zhp->zfs_hdl; 1178 nvlist_t *nvl = NULL, *realprops; 1179 zfs_prop_t prop; 1180 boolean_t do_prefix; 1181 uint64_t idx; 1182 1183 (void) snprintf(errbuf, sizeof (errbuf), 1184 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1185 zhp->zfs_name); 1186 1187 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1188 nvlist_add_string(nvl, propname, propval) != 0) { 1189 (void) no_memory(hdl); 1190 goto error; 1191 } 1192 1193 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 1194 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 1195 goto error; 1196 1197 nvlist_free(nvl); 1198 nvl = realprops; 1199 1200 prop = zfs_name_to_prop(propname); 1201 1202 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1203 goto error; 1204 1205 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1207 "child dataset with inherited mountpoint is used " 1208 "in a non-global zone")); 1209 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1210 goto error; 1211 } 1212 1213 /* 1214 * If the dataset's canmount property is being set to noauto, 1215 * then we want to prevent unmounting & remounting it. 1216 */ 1217 do_prefix = !((prop == ZFS_PROP_CANMOUNT) && 1218 (zprop_string_to_index(prop, propval, &idx, 1219 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO)); 1220 1221 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 1222 goto error; 1223 1224 /* 1225 * Execute the corresponding ioctl() to set this property. 1226 */ 1227 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1228 1229 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 1230 goto error; 1231 1232 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1233 1234 if (ret != 0) { 1235 switch (errno) { 1236 1237 case ENOSPC: 1238 /* 1239 * For quotas and reservations, ENOSPC indicates 1240 * something different; setting a quota or reservation 1241 * doesn't use any disk space. 1242 */ 1243 switch (prop) { 1244 case ZFS_PROP_QUOTA: 1245 case ZFS_PROP_REFQUOTA: 1246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1247 "size is less than current used or " 1248 "reserved space")); 1249 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1250 break; 1251 1252 case ZFS_PROP_RESERVATION: 1253 case ZFS_PROP_REFRESERVATION: 1254 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1255 "size is greater than available space")); 1256 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1257 break; 1258 1259 default: 1260 (void) zfs_standard_error(hdl, errno, errbuf); 1261 break; 1262 } 1263 break; 1264 1265 case EBUSY: 1266 (void) zfs_standard_error(hdl, EBUSY, errbuf); 1267 break; 1268 1269 case EROFS: 1270 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 1271 break; 1272 1273 case ENOTSUP: 1274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1275 "pool and or dataset must be upgraded to set this " 1276 "property or value")); 1277 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 1278 break; 1279 1280 case ERANGE: 1281 if (prop == ZFS_PROP_COMPRESSION) { 1282 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1283 "property setting is not allowed on " 1284 "bootable datasets")); 1285 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 1286 } else { 1287 (void) zfs_standard_error(hdl, errno, errbuf); 1288 } 1289 break; 1290 1291 case EOVERFLOW: 1292 /* 1293 * This platform can't address a volume this big. 1294 */ 1295 #ifdef _ILP32 1296 if (prop == ZFS_PROP_VOLSIZE) { 1297 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1298 break; 1299 } 1300 #endif 1301 /* FALLTHROUGH */ 1302 default: 1303 (void) zfs_standard_error(hdl, errno, errbuf); 1304 } 1305 } else { 1306 if (do_prefix) 1307 ret = changelist_postfix(cl); 1308 1309 /* 1310 * Refresh the statistics so the new property value 1311 * is reflected. 1312 */ 1313 if (ret == 0) 1314 (void) get_stats(zhp); 1315 } 1316 1317 error: 1318 nvlist_free(nvl); 1319 zcmd_free_nvlists(&zc); 1320 if (cl) 1321 changelist_free(cl); 1322 return (ret); 1323 } 1324 1325 /* 1326 * Given a property, inherit the value from the parent dataset. 1327 */ 1328 int 1329 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1330 { 1331 zfs_cmd_t zc = { 0 }; 1332 int ret; 1333 prop_changelist_t *cl; 1334 libzfs_handle_t *hdl = zhp->zfs_hdl; 1335 char errbuf[1024]; 1336 zfs_prop_t prop; 1337 1338 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1339 "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1340 1341 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1342 /* 1343 * For user properties, the amount of work we have to do is very 1344 * small, so just do it here. 1345 */ 1346 if (!zfs_prop_user(propname)) { 1347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1348 "invalid property")); 1349 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1350 } 1351 1352 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1353 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1354 1355 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1356 return (zfs_standard_error(hdl, errno, errbuf)); 1357 1358 return (0); 1359 } 1360 1361 /* 1362 * Verify that this property is inheritable. 1363 */ 1364 if (zfs_prop_readonly(prop)) 1365 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1366 1367 if (!zfs_prop_inheritable(prop)) 1368 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1369 1370 /* 1371 * Check to see if the value applies to this type 1372 */ 1373 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1374 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1375 1376 /* 1377 * Normalize the name, to get rid of shorthand abbrevations. 1378 */ 1379 propname = zfs_prop_to_name(prop); 1380 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1381 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1382 1383 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1384 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 1385 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1386 "dataset is used in a non-global zone")); 1387 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1388 } 1389 1390 /* 1391 * Determine datasets which will be affected by this change, if any. 1392 */ 1393 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1394 return (-1); 1395 1396 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1398 "child dataset with inherited mountpoint is used " 1399 "in a non-global zone")); 1400 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1401 goto error; 1402 } 1403 1404 if ((ret = changelist_prefix(cl)) != 0) 1405 goto error; 1406 1407 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 1408 return (zfs_standard_error(hdl, errno, errbuf)); 1409 } else { 1410 1411 if ((ret = changelist_postfix(cl)) != 0) 1412 goto error; 1413 1414 /* 1415 * Refresh the statistics so the new property is reflected. 1416 */ 1417 (void) get_stats(zhp); 1418 } 1419 1420 error: 1421 changelist_free(cl); 1422 return (ret); 1423 } 1424 1425 /* 1426 * True DSL properties are stored in an nvlist. The following two functions 1427 * extract them appropriately. 1428 */ 1429 static uint64_t 1430 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 1431 { 1432 nvlist_t *nv; 1433 uint64_t value; 1434 1435 *source = NULL; 1436 if (nvlist_lookup_nvlist(zhp->zfs_props, 1437 zfs_prop_to_name(prop), &nv) == 0) { 1438 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1439 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 1440 } else { 1441 verify(!zhp->zfs_props_table || 1442 zhp->zfs_props_table[prop] == B_TRUE); 1443 value = zfs_prop_default_numeric(prop); 1444 *source = ""; 1445 } 1446 1447 return (value); 1448 } 1449 1450 static char * 1451 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 1452 { 1453 nvlist_t *nv; 1454 char *value; 1455 1456 *source = NULL; 1457 if (nvlist_lookup_nvlist(zhp->zfs_props, 1458 zfs_prop_to_name(prop), &nv) == 0) { 1459 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1460 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 1461 } else { 1462 verify(!zhp->zfs_props_table || 1463 zhp->zfs_props_table[prop] == B_TRUE); 1464 if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 1465 value = ""; 1466 *source = ""; 1467 } 1468 1469 return (value); 1470 } 1471 1472 /* 1473 * Internal function for getting a numeric property. Both zfs_prop_get() and 1474 * zfs_prop_get_int() are built using this interface. 1475 * 1476 * Certain properties can be overridden using 'mount -o'. In this case, scan 1477 * the contents of the /etc/mnttab entry, searching for the appropriate options. 1478 * If they differ from the on-disk values, report the current values and mark 1479 * the source "temporary". 1480 */ 1481 static int 1482 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 1483 char **source, uint64_t *val) 1484 { 1485 zfs_cmd_t zc = { 0 }; 1486 nvlist_t *zplprops = NULL; 1487 struct mnttab mnt; 1488 char *mntopt_on = NULL; 1489 char *mntopt_off = NULL; 1490 1491 *source = NULL; 1492 1493 switch (prop) { 1494 case ZFS_PROP_ATIME: 1495 mntopt_on = MNTOPT_ATIME; 1496 mntopt_off = MNTOPT_NOATIME; 1497 break; 1498 1499 case ZFS_PROP_DEVICES: 1500 mntopt_on = MNTOPT_DEVICES; 1501 mntopt_off = MNTOPT_NODEVICES; 1502 break; 1503 1504 case ZFS_PROP_EXEC: 1505 mntopt_on = MNTOPT_EXEC; 1506 mntopt_off = MNTOPT_NOEXEC; 1507 break; 1508 1509 case ZFS_PROP_READONLY: 1510 mntopt_on = MNTOPT_RO; 1511 mntopt_off = MNTOPT_RW; 1512 break; 1513 1514 case ZFS_PROP_SETUID: 1515 mntopt_on = MNTOPT_SETUID; 1516 mntopt_off = MNTOPT_NOSETUID; 1517 break; 1518 1519 case ZFS_PROP_XATTR: 1520 mntopt_on = MNTOPT_XATTR; 1521 mntopt_off = MNTOPT_NOXATTR; 1522 break; 1523 1524 case ZFS_PROP_NBMAND: 1525 mntopt_on = MNTOPT_NBMAND; 1526 mntopt_off = MNTOPT_NONBMAND; 1527 break; 1528 } 1529 1530 /* 1531 * Because looking up the mount options is potentially expensive 1532 * (iterating over all of /etc/mnttab), we defer its calculation until 1533 * we're looking up a property which requires its presence. 1534 */ 1535 if (!zhp->zfs_mntcheck && 1536 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 1537 libzfs_handle_t *hdl = zhp->zfs_hdl; 1538 struct mnttab entry; 1539 1540 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 1541 zhp->zfs_mntopts = zfs_strdup(hdl, 1542 entry.mnt_mntopts); 1543 if (zhp->zfs_mntopts == NULL) 1544 return (-1); 1545 } 1546 1547 zhp->zfs_mntcheck = B_TRUE; 1548 } 1549 1550 if (zhp->zfs_mntopts == NULL) 1551 mnt.mnt_mntopts = ""; 1552 else 1553 mnt.mnt_mntopts = zhp->zfs_mntopts; 1554 1555 switch (prop) { 1556 case ZFS_PROP_ATIME: 1557 case ZFS_PROP_DEVICES: 1558 case ZFS_PROP_EXEC: 1559 case ZFS_PROP_READONLY: 1560 case ZFS_PROP_SETUID: 1561 case ZFS_PROP_XATTR: 1562 case ZFS_PROP_NBMAND: 1563 *val = getprop_uint64(zhp, prop, source); 1564 1565 if (hasmntopt(&mnt, mntopt_on) && !*val) { 1566 *val = B_TRUE; 1567 if (src) 1568 *src = ZPROP_SRC_TEMPORARY; 1569 } else if (hasmntopt(&mnt, mntopt_off) && *val) { 1570 *val = B_FALSE; 1571 if (src) 1572 *src = ZPROP_SRC_TEMPORARY; 1573 } 1574 break; 1575 1576 case ZFS_PROP_CANMOUNT: 1577 *val = getprop_uint64(zhp, prop, source); 1578 if (*val != ZFS_CANMOUNT_ON) 1579 *source = zhp->zfs_name; 1580 else 1581 *source = ""; /* default */ 1582 break; 1583 1584 case ZFS_PROP_QUOTA: 1585 case ZFS_PROP_REFQUOTA: 1586 case ZFS_PROP_RESERVATION: 1587 case ZFS_PROP_REFRESERVATION: 1588 *val = getprop_uint64(zhp, prop, source); 1589 if (*val == 0) 1590 *source = ""; /* default */ 1591 else 1592 *source = zhp->zfs_name; 1593 break; 1594 1595 case ZFS_PROP_MOUNTED: 1596 *val = (zhp->zfs_mntopts != NULL); 1597 break; 1598 1599 case ZFS_PROP_NUMCLONES: 1600 *val = zhp->zfs_dmustats.dds_num_clones; 1601 break; 1602 1603 case ZFS_PROP_VERSION: 1604 case ZFS_PROP_NORMALIZE: 1605 case ZFS_PROP_UTF8ONLY: 1606 case ZFS_PROP_CASE: 1607 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 1608 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 1609 return (-1); 1610 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1611 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 1612 zcmd_free_nvlists(&zc); 1613 return (-1); 1614 } 1615 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 1616 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 1617 val) != 0) { 1618 zcmd_free_nvlists(&zc); 1619 return (-1); 1620 } 1621 if (zplprops) 1622 nvlist_free(zplprops); 1623 zcmd_free_nvlists(&zc); 1624 break; 1625 1626 default: 1627 switch (zfs_prop_get_type(prop)) { 1628 case PROP_TYPE_NUMBER: 1629 case PROP_TYPE_INDEX: 1630 *val = getprop_uint64(zhp, prop, source); 1631 /* 1632 * If we tried to use a default value for a 1633 * readonly property, it means that it was not 1634 * present; return an error. 1635 */ 1636 if (zfs_prop_readonly(prop) && 1637 *source && (*source)[0] == '\0') { 1638 return (-1); 1639 } 1640 break; 1641 1642 case PROP_TYPE_STRING: 1643 default: 1644 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 1645 "cannot get non-numeric property")); 1646 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 1647 dgettext(TEXT_DOMAIN, "internal error"))); 1648 } 1649 } 1650 1651 return (0); 1652 } 1653 1654 /* 1655 * Calculate the source type, given the raw source string. 1656 */ 1657 static void 1658 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1659 char *statbuf, size_t statlen) 1660 { 1661 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1662 return; 1663 1664 if (source == NULL) { 1665 *srctype = ZPROP_SRC_NONE; 1666 } else if (source[0] == '\0') { 1667 *srctype = ZPROP_SRC_DEFAULT; 1668 } else { 1669 if (strcmp(source, zhp->zfs_name) == 0) { 1670 *srctype = ZPROP_SRC_LOCAL; 1671 } else { 1672 (void) strlcpy(statbuf, source, statlen); 1673 *srctype = ZPROP_SRC_INHERITED; 1674 } 1675 } 1676 1677 } 1678 1679 /* 1680 * Retrieve a property from the given object. If 'literal' is specified, then 1681 * numbers are left as exact values. Otherwise, numbers are converted to a 1682 * human-readable form. 1683 * 1684 * Returns 0 on success, or -1 on error. 1685 */ 1686 int 1687 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 1688 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 1689 { 1690 char *source = NULL; 1691 uint64_t val; 1692 char *str; 1693 const char *strval; 1694 1695 /* 1696 * Check to see if this property applies to our object 1697 */ 1698 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1699 return (-1); 1700 1701 if (src) 1702 *src = ZPROP_SRC_NONE; 1703 1704 switch (prop) { 1705 case ZFS_PROP_CREATION: 1706 /* 1707 * 'creation' is a time_t stored in the statistics. We convert 1708 * this into a string unless 'literal' is specified. 1709 */ 1710 { 1711 val = getprop_uint64(zhp, prop, &source); 1712 time_t time = (time_t)val; 1713 struct tm t; 1714 1715 if (literal || 1716 localtime_r(&time, &t) == NULL || 1717 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 1718 &t) == 0) 1719 (void) snprintf(propbuf, proplen, "%llu", val); 1720 } 1721 break; 1722 1723 case ZFS_PROP_MOUNTPOINT: 1724 /* 1725 * Getting the precise mountpoint can be tricky. 1726 * 1727 * - for 'none' or 'legacy', return those values. 1728 * - for inherited mountpoints, we want to take everything 1729 * after our ancestor and append it to the inherited value. 1730 * 1731 * If the pool has an alternate root, we want to prepend that 1732 * root to any values we return. 1733 */ 1734 1735 str = getprop_string(zhp, prop, &source); 1736 1737 if (str[0] == '/') { 1738 char buf[MAXPATHLEN]; 1739 char *root = buf; 1740 const char *relpath = zhp->zfs_name + strlen(source); 1741 1742 if (relpath[0] == '/') 1743 relpath++; 1744 1745 if ((zpool_get_prop(zhp->zpool_hdl, 1746 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) || 1747 (strcmp(root, "-") == 0)) 1748 root[0] = '\0'; 1749 /* 1750 * Special case an alternate root of '/'. This will 1751 * avoid having multiple leading slashes in the 1752 * mountpoint path. 1753 */ 1754 if (strcmp(root, "/") == 0) 1755 root++; 1756 1757 /* 1758 * If the mountpoint is '/' then skip over this 1759 * if we are obtaining either an alternate root or 1760 * an inherited mountpoint. 1761 */ 1762 if (str[1] == '\0' && (root[0] != '\0' || 1763 relpath[0] != '\0')) 1764 str++; 1765 1766 if (relpath[0] == '\0') 1767 (void) snprintf(propbuf, proplen, "%s%s", 1768 root, str); 1769 else 1770 (void) snprintf(propbuf, proplen, "%s%s%s%s", 1771 root, str, relpath[0] == '@' ? "" : "/", 1772 relpath); 1773 } else { 1774 /* 'legacy' or 'none' */ 1775 (void) strlcpy(propbuf, str, proplen); 1776 } 1777 1778 break; 1779 1780 case ZFS_PROP_ORIGIN: 1781 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 1782 proplen); 1783 /* 1784 * If there is no parent at all, return failure to indicate that 1785 * it doesn't apply to this dataset. 1786 */ 1787 if (propbuf[0] == '\0') 1788 return (-1); 1789 break; 1790 1791 case ZFS_PROP_QUOTA: 1792 case ZFS_PROP_REFQUOTA: 1793 case ZFS_PROP_RESERVATION: 1794 case ZFS_PROP_REFRESERVATION: 1795 1796 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 1797 return (-1); 1798 1799 /* 1800 * If quota or reservation is 0, we translate this into 'none' 1801 * (unless literal is set), and indicate that it's the default 1802 * value. Otherwise, we print the number nicely and indicate 1803 * that its set locally. 1804 */ 1805 if (val == 0) { 1806 if (literal) 1807 (void) strlcpy(propbuf, "0", proplen); 1808 else 1809 (void) strlcpy(propbuf, "none", proplen); 1810 } else { 1811 if (literal) 1812 (void) snprintf(propbuf, proplen, "%llu", 1813 (u_longlong_t)val); 1814 else 1815 zfs_nicenum(val, propbuf, proplen); 1816 } 1817 break; 1818 1819 case ZFS_PROP_COMPRESSRATIO: 1820 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 1821 return (-1); 1822 (void) snprintf(propbuf, proplen, "%llu.%02llux", 1823 (u_longlong_t)(val / 100), 1824 (u_longlong_t)(val % 100)); 1825 break; 1826 1827 case ZFS_PROP_TYPE: 1828 switch (zhp->zfs_type) { 1829 case ZFS_TYPE_FILESYSTEM: 1830 str = "filesystem"; 1831 break; 1832 case ZFS_TYPE_VOLUME: 1833 str = "volume"; 1834 break; 1835 case ZFS_TYPE_SNAPSHOT: 1836 str = "snapshot"; 1837 break; 1838 default: 1839 abort(); 1840 } 1841 (void) snprintf(propbuf, proplen, "%s", str); 1842 break; 1843 1844 case ZFS_PROP_MOUNTED: 1845 /* 1846 * The 'mounted' property is a pseudo-property that described 1847 * whether the filesystem is currently mounted. Even though 1848 * it's a boolean value, the typical values of "on" and "off" 1849 * don't make sense, so we translate to "yes" and "no". 1850 */ 1851 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 1852 src, &source, &val) != 0) 1853 return (-1); 1854 if (val) 1855 (void) strlcpy(propbuf, "yes", proplen); 1856 else 1857 (void) strlcpy(propbuf, "no", proplen); 1858 break; 1859 1860 case ZFS_PROP_NAME: 1861 /* 1862 * The 'name' property is a pseudo-property derived from the 1863 * dataset name. It is presented as a real property to simplify 1864 * consumers. 1865 */ 1866 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 1867 break; 1868 1869 default: 1870 switch (zfs_prop_get_type(prop)) { 1871 case PROP_TYPE_NUMBER: 1872 if (get_numeric_property(zhp, prop, src, 1873 &source, &val) != 0) 1874 return (-1); 1875 if (literal) 1876 (void) snprintf(propbuf, proplen, "%llu", 1877 (u_longlong_t)val); 1878 else 1879 zfs_nicenum(val, propbuf, proplen); 1880 break; 1881 1882 case PROP_TYPE_STRING: 1883 (void) strlcpy(propbuf, 1884 getprop_string(zhp, prop, &source), proplen); 1885 break; 1886 1887 case PROP_TYPE_INDEX: 1888 if (get_numeric_property(zhp, prop, src, 1889 &source, &val) != 0) 1890 return (-1); 1891 if (zfs_prop_index_to_string(prop, val, &strval) != 0) 1892 return (-1); 1893 (void) strlcpy(propbuf, strval, proplen); 1894 break; 1895 1896 default: 1897 abort(); 1898 } 1899 } 1900 1901 get_source(zhp, src, source, statbuf, statlen); 1902 1903 return (0); 1904 } 1905 1906 /* 1907 * Utility function to get the given numeric property. Does no validation that 1908 * the given property is the appropriate type; should only be used with 1909 * hard-coded property types. 1910 */ 1911 uint64_t 1912 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 1913 { 1914 char *source; 1915 uint64_t val; 1916 1917 (void) get_numeric_property(zhp, prop, NULL, &source, &val); 1918 1919 return (val); 1920 } 1921 1922 int 1923 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 1924 { 1925 char buf[64]; 1926 1927 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 1928 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 1929 } 1930 1931 /* 1932 * Similar to zfs_prop_get(), but returns the value as an integer. 1933 */ 1934 int 1935 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 1936 zprop_source_t *src, char *statbuf, size_t statlen) 1937 { 1938 char *source; 1939 1940 /* 1941 * Check to see if this property applies to our object 1942 */ 1943 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 1944 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 1945 dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 1946 zfs_prop_to_name(prop))); 1947 } 1948 1949 if (src) 1950 *src = ZPROP_SRC_NONE; 1951 1952 if (get_numeric_property(zhp, prop, src, &source, value) != 0) 1953 return (-1); 1954 1955 get_source(zhp, src, source, statbuf, statlen); 1956 1957 return (0); 1958 } 1959 1960 static int 1961 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 1962 char **domainp, idmap_rid_t *ridp) 1963 { 1964 idmap_handle_t *idmap_hdl = NULL; 1965 idmap_get_handle_t *get_hdl = NULL; 1966 idmap_stat status; 1967 int err = EINVAL; 1968 1969 if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS) 1970 goto out; 1971 if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS) 1972 goto out; 1973 1974 if (isuser) { 1975 err = idmap_get_sidbyuid(get_hdl, id, 1976 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 1977 } else { 1978 err = idmap_get_sidbygid(get_hdl, id, 1979 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 1980 } 1981 if (err == IDMAP_SUCCESS && 1982 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 1983 status == IDMAP_SUCCESS) 1984 err = 0; 1985 else 1986 err = EINVAL; 1987 out: 1988 if (get_hdl) 1989 idmap_get_destroy(get_hdl); 1990 if (idmap_hdl) 1991 (void) idmap_fini(idmap_hdl); 1992 return (err); 1993 } 1994 1995 /* 1996 * convert the propname into parameters needed by kernel 1997 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 1998 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 1999 */ 2000 static int 2001 userquota_propname_decode(const char *propname, boolean_t zoned, 2002 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 2003 { 2004 zfs_userquota_prop_t type; 2005 char *cp, *end; 2006 char *numericsid = NULL; 2007 boolean_t isuser; 2008 2009 domain[0] = '\0'; 2010 2011 /* Figure out the property type ({user|group}{quota|space}) */ 2012 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 2013 if (strncmp(propname, zfs_userquota_prop_prefixes[type], 2014 strlen(zfs_userquota_prop_prefixes[type])) == 0) 2015 break; 2016 } 2017 if (type == ZFS_NUM_USERQUOTA_PROPS) 2018 return (EINVAL); 2019 *typep = type; 2020 2021 isuser = (type == ZFS_PROP_USERQUOTA || 2022 type == ZFS_PROP_USERUSED); 2023 2024 cp = strchr(propname, '@') + 1; 2025 2026 if (strchr(cp, '@')) { 2027 /* 2028 * It's a SID name (eg "user@domain") that needs to be 2029 * turned into S-1-domainID-RID. 2030 */ 2031 directory_error_t e; 2032 if (zoned && getzoneid() == GLOBAL_ZONEID) 2033 return (ENOENT); 2034 if (isuser) { 2035 e = directory_sid_from_user_name(NULL, 2036 cp, &numericsid); 2037 } else { 2038 e = directory_sid_from_group_name(NULL, 2039 cp, &numericsid); 2040 } 2041 if (e != NULL) { 2042 directory_error_free(e); 2043 return (ENOENT); 2044 } 2045 if (numericsid == NULL) 2046 return (ENOENT); 2047 cp = numericsid; 2048 /* will be further decoded below */ 2049 } 2050 2051 if (strncmp(cp, "S-1-", 4) == 0) { 2052 /* It's a numeric SID (eg "S-1-234-567-89") */ 2053 (void) strlcpy(domain, cp, domainlen); 2054 cp = strrchr(domain, '-'); 2055 *cp = '\0'; 2056 cp++; 2057 2058 errno = 0; 2059 *ridp = strtoull(cp, &end, 10); 2060 if (numericsid) { 2061 free(numericsid); 2062 numericsid = NULL; 2063 } 2064 if (errno != 0 || *end != '\0') 2065 return (EINVAL); 2066 } else if (!isdigit(*cp)) { 2067 /* 2068 * It's a user/group name (eg "user") that needs to be 2069 * turned into a uid/gid 2070 */ 2071 if (zoned && getzoneid() == GLOBAL_ZONEID) 2072 return (ENOENT); 2073 if (isuser) { 2074 struct passwd *pw; 2075 pw = getpwnam(cp); 2076 if (pw == NULL) 2077 return (ENOENT); 2078 *ridp = pw->pw_uid; 2079 } else { 2080 struct group *gr; 2081 gr = getgrnam(cp); 2082 if (gr == NULL) 2083 return (ENOENT); 2084 *ridp = gr->gr_gid; 2085 } 2086 } else { 2087 /* It's a user/group ID (eg "12345"). */ 2088 uid_t id = strtoul(cp, &end, 10); 2089 idmap_rid_t rid; 2090 char *mapdomain; 2091 2092 if (*end != '\0') 2093 return (EINVAL); 2094 if (id > MAXUID) { 2095 /* It's an ephemeral ID. */ 2096 if (idmap_id_to_numeric_domain_rid(id, isuser, 2097 &mapdomain, &rid) != 0) 2098 return (ENOENT); 2099 (void) strlcpy(domain, mapdomain, domainlen); 2100 *ridp = rid; 2101 } else { 2102 *ridp = id; 2103 } 2104 } 2105 2106 ASSERT3P(numericsid, ==, NULL); 2107 return (0); 2108 } 2109 2110 static int 2111 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 2112 uint64_t *propvalue, zfs_userquota_prop_t *typep) 2113 { 2114 int err; 2115 zfs_cmd_t zc = { 0 }; 2116 2117 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2118 2119 err = userquota_propname_decode(propname, 2120 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 2121 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 2122 zc.zc_objset_type = *typep; 2123 if (err) 2124 return (err); 2125 2126 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 2127 if (err) 2128 return (err); 2129 2130 *propvalue = zc.zc_cookie; 2131 return (0); 2132 } 2133 2134 int 2135 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 2136 uint64_t *propvalue) 2137 { 2138 zfs_userquota_prop_t type; 2139 2140 return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 2141 &type)); 2142 } 2143 2144 int 2145 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 2146 char *propbuf, int proplen, boolean_t literal) 2147 { 2148 int err; 2149 uint64_t propvalue; 2150 zfs_userquota_prop_t type; 2151 2152 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 2153 &type); 2154 2155 if (err) 2156 return (err); 2157 2158 if (literal) { 2159 (void) snprintf(propbuf, proplen, "%llu", propvalue); 2160 } else if (propvalue == 0 && 2161 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 2162 (void) strlcpy(propbuf, "none", proplen); 2163 } else { 2164 zfs_nicenum(propvalue, propbuf, proplen); 2165 } 2166 return (0); 2167 } 2168 2169 /* 2170 * Returns the name of the given zfs handle. 2171 */ 2172 const char * 2173 zfs_get_name(const zfs_handle_t *zhp) 2174 { 2175 return (zhp->zfs_name); 2176 } 2177 2178 /* 2179 * Returns the type of the given zfs handle. 2180 */ 2181 zfs_type_t 2182 zfs_get_type(const zfs_handle_t *zhp) 2183 { 2184 return (zhp->zfs_type); 2185 } 2186 2187 static int 2188 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc) 2189 { 2190 int rc; 2191 uint64_t orig_cookie; 2192 2193 orig_cookie = zc->zc_cookie; 2194 top: 2195 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 2196 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc); 2197 2198 if (rc == -1) { 2199 switch (errno) { 2200 case ENOMEM: 2201 /* expand nvlist memory and try again */ 2202 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { 2203 zcmd_free_nvlists(zc); 2204 return (-1); 2205 } 2206 zc->zc_cookie = orig_cookie; 2207 goto top; 2208 /* 2209 * An errno value of ESRCH indicates normal completion. 2210 * If ENOENT is returned, then the underlying dataset 2211 * has been removed since we obtained the handle. 2212 */ 2213 case ESRCH: 2214 case ENOENT: 2215 rc = 1; 2216 break; 2217 default: 2218 rc = zfs_standard_error(zhp->zfs_hdl, errno, 2219 dgettext(TEXT_DOMAIN, 2220 "cannot iterate filesystems")); 2221 break; 2222 } 2223 } 2224 return (rc); 2225 } 2226 2227 /* 2228 * Iterate over all child filesystems 2229 */ 2230 int 2231 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2232 { 2233 zfs_cmd_t zc = { 0 }; 2234 zfs_handle_t *nzhp; 2235 int ret; 2236 2237 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 2238 return (0); 2239 2240 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 2241 return (-1); 2242 2243 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, 2244 &zc)) == 0) { 2245 /* 2246 * Silently ignore errors, as the only plausible explanation is 2247 * that the pool has since been removed. 2248 */ 2249 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 2250 &zc)) == NULL) { 2251 continue; 2252 } 2253 2254 if ((ret = func(nzhp, data)) != 0) { 2255 zcmd_free_nvlists(&zc); 2256 return (ret); 2257 } 2258 } 2259 zcmd_free_nvlists(&zc); 2260 return ((ret < 0) ? ret : 0); 2261 } 2262 2263 /* 2264 * Iterate over all snapshots 2265 */ 2266 int 2267 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2268 { 2269 zfs_cmd_t zc = { 0 }; 2270 zfs_handle_t *nzhp; 2271 int ret; 2272 2273 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 2274 return (0); 2275 2276 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 2277 return (-1); 2278 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 2279 &zc)) == 0) { 2280 2281 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 2282 &zc)) == NULL) { 2283 continue; 2284 } 2285 2286 if ((ret = func(nzhp, data)) != 0) { 2287 zcmd_free_nvlists(&zc); 2288 return (ret); 2289 } 2290 } 2291 zcmd_free_nvlists(&zc); 2292 return ((ret < 0) ? ret : 0); 2293 } 2294 2295 /* 2296 * Iterate over all children, snapshots and filesystems 2297 */ 2298 int 2299 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2300 { 2301 int ret; 2302 2303 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 2304 return (ret); 2305 2306 return (zfs_iter_snapshots(zhp, func, data)); 2307 } 2308 2309 /* 2310 * Given a complete name, return just the portion that refers to the parent. 2311 * Can return NULL if this is a pool. 2312 */ 2313 static int 2314 parent_name(const char *path, char *buf, size_t buflen) 2315 { 2316 char *loc; 2317 2318 if ((loc = strrchr(path, '/')) == NULL) 2319 return (-1); 2320 2321 (void) strncpy(buf, path, MIN(buflen, loc - path)); 2322 buf[loc - path] = '\0'; 2323 2324 return (0); 2325 } 2326 2327 /* 2328 * If accept_ancestor is false, then check to make sure that the given path has 2329 * a parent, and that it exists. If accept_ancestor is true, then find the 2330 * closest existing ancestor for the given path. In prefixlen return the 2331 * length of already existing prefix of the given path. We also fetch the 2332 * 'zoned' property, which is used to validate property settings when creating 2333 * new datasets. 2334 */ 2335 static int 2336 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 2337 boolean_t accept_ancestor, int *prefixlen) 2338 { 2339 zfs_cmd_t zc = { 0 }; 2340 char parent[ZFS_MAXNAMELEN]; 2341 char *slash; 2342 zfs_handle_t *zhp; 2343 char errbuf[1024]; 2344 2345 (void) snprintf(errbuf, sizeof (errbuf), 2346 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2347 2348 /* get parent, and check to see if this is just a pool */ 2349 if (parent_name(path, parent, sizeof (parent)) != 0) { 2350 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2351 "missing dataset name")); 2352 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2353 } 2354 2355 /* check to see if the pool exists */ 2356 if ((slash = strchr(parent, '/')) == NULL) 2357 slash = parent + strlen(parent); 2358 (void) strncpy(zc.zc_name, parent, slash - parent); 2359 zc.zc_name[slash - parent] = '\0'; 2360 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2361 errno == ENOENT) { 2362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2363 "no such pool '%s'"), zc.zc_name); 2364 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2365 } 2366 2367 /* check to see if the parent dataset exists */ 2368 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 2369 if (errno == ENOENT && accept_ancestor) { 2370 /* 2371 * Go deeper to find an ancestor, give up on top level. 2372 */ 2373 if (parent_name(parent, parent, sizeof (parent)) != 0) { 2374 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2375 "no such pool '%s'"), zc.zc_name); 2376 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2377 } 2378 } else if (errno == ENOENT) { 2379 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2380 "parent does not exist")); 2381 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2382 } else 2383 return (zfs_standard_error(hdl, errno, errbuf)); 2384 } 2385 2386 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2387 /* we are in a non-global zone, but parent is in the global zone */ 2388 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 2389 (void) zfs_standard_error(hdl, EPERM, errbuf); 2390 zfs_close(zhp); 2391 return (-1); 2392 } 2393 2394 /* make sure parent is a filesystem */ 2395 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 2396 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2397 "parent is not a filesystem")); 2398 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 2399 zfs_close(zhp); 2400 return (-1); 2401 } 2402 2403 zfs_close(zhp); 2404 if (prefixlen != NULL) 2405 *prefixlen = strlen(parent); 2406 return (0); 2407 } 2408 2409 /* 2410 * Finds whether the dataset of the given type(s) exists. 2411 */ 2412 boolean_t 2413 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 2414 { 2415 zfs_handle_t *zhp; 2416 2417 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 2418 return (B_FALSE); 2419 2420 /* 2421 * Try to get stats for the dataset, which will tell us if it exists. 2422 */ 2423 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 2424 int ds_type = zhp->zfs_type; 2425 2426 zfs_close(zhp); 2427 if (types & ds_type) 2428 return (B_TRUE); 2429 } 2430 return (B_FALSE); 2431 } 2432 2433 /* 2434 * Given a path to 'target', create all the ancestors between 2435 * the prefixlen portion of the path, and the target itself. 2436 * Fail if the initial prefixlen-ancestor does not already exist. 2437 */ 2438 int 2439 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 2440 { 2441 zfs_handle_t *h; 2442 char *cp; 2443 const char *opname; 2444 2445 /* make sure prefix exists */ 2446 cp = target + prefixlen; 2447 if (*cp != '/') { 2448 assert(strchr(cp, '/') == NULL); 2449 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2450 } else { 2451 *cp = '\0'; 2452 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2453 *cp = '/'; 2454 } 2455 if (h == NULL) 2456 return (-1); 2457 zfs_close(h); 2458 2459 /* 2460 * Attempt to create, mount, and share any ancestor filesystems, 2461 * up to the prefixlen-long one. 2462 */ 2463 for (cp = target + prefixlen + 1; 2464 cp = strchr(cp, '/'); *cp = '/', cp++) { 2465 char *logstr; 2466 2467 *cp = '\0'; 2468 2469 h = make_dataset_handle(hdl, target); 2470 if (h) { 2471 /* it already exists, nothing to do here */ 2472 zfs_close(h); 2473 continue; 2474 } 2475 2476 logstr = hdl->libzfs_log_str; 2477 hdl->libzfs_log_str = NULL; 2478 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 2479 NULL) != 0) { 2480 hdl->libzfs_log_str = logstr; 2481 opname = dgettext(TEXT_DOMAIN, "create"); 2482 goto ancestorerr; 2483 } 2484 2485 hdl->libzfs_log_str = logstr; 2486 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2487 if (h == NULL) { 2488 opname = dgettext(TEXT_DOMAIN, "open"); 2489 goto ancestorerr; 2490 } 2491 2492 if (zfs_mount(h, NULL, 0) != 0) { 2493 opname = dgettext(TEXT_DOMAIN, "mount"); 2494 goto ancestorerr; 2495 } 2496 2497 if (zfs_share(h) != 0) { 2498 opname = dgettext(TEXT_DOMAIN, "share"); 2499 goto ancestorerr; 2500 } 2501 2502 zfs_close(h); 2503 } 2504 2505 return (0); 2506 2507 ancestorerr: 2508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2509 "failed to %s ancestor '%s'"), opname, target); 2510 return (-1); 2511 } 2512 2513 /* 2514 * Creates non-existing ancestors of the given path. 2515 */ 2516 int 2517 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 2518 { 2519 int prefix; 2520 uint64_t zoned; 2521 char *path_copy; 2522 int rc; 2523 2524 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 2525 return (-1); 2526 2527 if ((path_copy = strdup(path)) != NULL) { 2528 rc = create_parents(hdl, path_copy, prefix); 2529 free(path_copy); 2530 } 2531 if (path_copy == NULL || rc != 0) 2532 return (-1); 2533 2534 return (0); 2535 } 2536 2537 /* 2538 * Create a new filesystem or volume. 2539 */ 2540 int 2541 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 2542 nvlist_t *props) 2543 { 2544 zfs_cmd_t zc = { 0 }; 2545 int ret; 2546 uint64_t size = 0; 2547 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 2548 char errbuf[1024]; 2549 uint64_t zoned; 2550 2551 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2552 "cannot create '%s'"), path); 2553 2554 /* validate the path, taking care to note the extended error message */ 2555 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 2556 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2557 2558 /* validate parents exist */ 2559 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2560 return (-1); 2561 2562 /* 2563 * The failure modes when creating a dataset of a different type over 2564 * one that already exists is a little strange. In particular, if you 2565 * try to create a dataset on top of an existing dataset, the ioctl() 2566 * will return ENOENT, not EEXIST. To prevent this from happening, we 2567 * first try to see if the dataset exists. 2568 */ 2569 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 2570 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 2571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2572 "dataset already exists")); 2573 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2574 } 2575 2576 if (type == ZFS_TYPE_VOLUME) 2577 zc.zc_objset_type = DMU_OST_ZVOL; 2578 else 2579 zc.zc_objset_type = DMU_OST_ZFS; 2580 2581 if (props && (props = zfs_valid_proplist(hdl, type, props, 2582 zoned, NULL, errbuf)) == 0) 2583 return (-1); 2584 2585 if (type == ZFS_TYPE_VOLUME) { 2586 /* 2587 * If we are creating a volume, the size and block size must 2588 * satisfy a few restraints. First, the blocksize must be a 2589 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 2590 * volsize must be a multiple of the block size, and cannot be 2591 * zero. 2592 */ 2593 if (props == NULL || nvlist_lookup_uint64(props, 2594 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 2595 nvlist_free(props); 2596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2597 "missing volume size")); 2598 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2599 } 2600 2601 if ((ret = nvlist_lookup_uint64(props, 2602 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2603 &blocksize)) != 0) { 2604 if (ret == ENOENT) { 2605 blocksize = zfs_prop_default_numeric( 2606 ZFS_PROP_VOLBLOCKSIZE); 2607 } else { 2608 nvlist_free(props); 2609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2610 "missing volume block size")); 2611 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2612 } 2613 } 2614 2615 if (size == 0) { 2616 nvlist_free(props); 2617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2618 "volume size cannot be zero")); 2619 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2620 } 2621 2622 if (size % blocksize != 0) { 2623 nvlist_free(props); 2624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2625 "volume size must be a multiple of volume block " 2626 "size")); 2627 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2628 } 2629 } 2630 2631 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 2632 return (-1); 2633 nvlist_free(props); 2634 2635 /* create the dataset */ 2636 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2637 2638 zcmd_free_nvlists(&zc); 2639 2640 /* check for failure */ 2641 if (ret != 0) { 2642 char parent[ZFS_MAXNAMELEN]; 2643 (void) parent_name(path, parent, sizeof (parent)); 2644 2645 switch (errno) { 2646 case ENOENT: 2647 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2648 "no such parent '%s'"), parent); 2649 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2650 2651 case EINVAL: 2652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2653 "parent '%s' is not a filesystem"), parent); 2654 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2655 2656 case EDOM: 2657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2658 "volume block size must be power of 2 from " 2659 "%u to %uk"), 2660 (uint_t)SPA_MINBLOCKSIZE, 2661 (uint_t)SPA_MAXBLOCKSIZE >> 10); 2662 2663 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2664 2665 case ENOTSUP: 2666 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2667 "pool must be upgraded to set this " 2668 "property or value")); 2669 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 2670 #ifdef _ILP32 2671 case EOVERFLOW: 2672 /* 2673 * This platform can't address a volume this big. 2674 */ 2675 if (type == ZFS_TYPE_VOLUME) 2676 return (zfs_error(hdl, EZFS_VOLTOOBIG, 2677 errbuf)); 2678 #endif 2679 /* FALLTHROUGH */ 2680 default: 2681 return (zfs_standard_error(hdl, errno, errbuf)); 2682 } 2683 } 2684 2685 return (0); 2686 } 2687 2688 /* 2689 * Destroys the given dataset. The caller must make sure that the filesystem 2690 * isn't mounted, and that there are no active dependents. 2691 */ 2692 int 2693 zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 2694 { 2695 zfs_cmd_t zc = { 0 }; 2696 2697 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2698 2699 if (ZFS_IS_VOLUME(zhp)) { 2700 /* 2701 * If user doesn't have permissions to unshare volume, then 2702 * abort the request. This would only happen for a 2703 * non-privileged user. 2704 */ 2705 if (zfs_unshare_iscsi(zhp) != 0) { 2706 return (-1); 2707 } 2708 2709 zc.zc_objset_type = DMU_OST_ZVOL; 2710 } else { 2711 zc.zc_objset_type = DMU_OST_ZFS; 2712 } 2713 2714 zc.zc_defer_destroy = defer; 2715 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 2716 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 2717 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 2718 zhp->zfs_name)); 2719 } 2720 2721 remove_mountpoint(zhp); 2722 2723 return (0); 2724 } 2725 2726 struct destroydata { 2727 char *snapname; 2728 boolean_t gotone; 2729 boolean_t closezhp; 2730 }; 2731 2732 static int 2733 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 2734 { 2735 struct destroydata *dd = arg; 2736 zfs_handle_t *szhp; 2737 char name[ZFS_MAXNAMELEN]; 2738 boolean_t closezhp = dd->closezhp; 2739 int rv = 0; 2740 2741 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 2742 (void) strlcat(name, "@", sizeof (name)); 2743 (void) strlcat(name, dd->snapname, sizeof (name)); 2744 2745 szhp = make_dataset_handle(zhp->zfs_hdl, name); 2746 if (szhp) { 2747 dd->gotone = B_TRUE; 2748 zfs_close(szhp); 2749 } 2750 2751 dd->closezhp = B_TRUE; 2752 if (!dd->gotone) 2753 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg); 2754 if (closezhp) 2755 zfs_close(zhp); 2756 return (rv); 2757 } 2758 2759 /* 2760 * Destroys all snapshots with the given name in zhp & descendants. 2761 */ 2762 int 2763 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 2764 { 2765 zfs_cmd_t zc = { 0 }; 2766 int ret; 2767 struct destroydata dd = { 0 }; 2768 2769 dd.snapname = snapname; 2770 (void) zfs_check_snap_cb(zhp, &dd); 2771 2772 if (!dd.gotone) { 2773 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 2774 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 2775 zhp->zfs_name, snapname)); 2776 } 2777 2778 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2779 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 2780 zc.zc_defer_destroy = defer; 2781 2782 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 2783 if (ret != 0) { 2784 char errbuf[1024]; 2785 2786 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2787 "cannot destroy '%s@%s'"), zc.zc_name, snapname); 2788 2789 switch (errno) { 2790 case EEXIST: 2791 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2792 "snapshot is cloned")); 2793 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 2794 2795 default: 2796 return (zfs_standard_error(zhp->zfs_hdl, errno, 2797 errbuf)); 2798 } 2799 } 2800 2801 return (0); 2802 } 2803 2804 /* 2805 * Clones the given dataset. The target must be of the same type as the source. 2806 */ 2807 int 2808 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2809 { 2810 zfs_cmd_t zc = { 0 }; 2811 char parent[ZFS_MAXNAMELEN]; 2812 int ret; 2813 char errbuf[1024]; 2814 libzfs_handle_t *hdl = zhp->zfs_hdl; 2815 zfs_type_t type; 2816 uint64_t zoned; 2817 2818 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2819 2820 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2821 "cannot create '%s'"), target); 2822 2823 /* validate the target name */ 2824 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 2825 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2826 2827 /* validate parents exist */ 2828 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2829 return (-1); 2830 2831 (void) parent_name(target, parent, sizeof (parent)); 2832 2833 /* do the clone */ 2834 if (ZFS_IS_VOLUME(zhp)) { 2835 zc.zc_objset_type = DMU_OST_ZVOL; 2836 type = ZFS_TYPE_VOLUME; 2837 } else { 2838 zc.zc_objset_type = DMU_OST_ZFS; 2839 type = ZFS_TYPE_FILESYSTEM; 2840 } 2841 2842 if (props) { 2843 if ((props = zfs_valid_proplist(hdl, type, props, zoned, 2844 zhp, errbuf)) == NULL) 2845 return (-1); 2846 2847 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 2848 nvlist_free(props); 2849 return (-1); 2850 } 2851 2852 nvlist_free(props); 2853 } 2854 2855 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 2856 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 2857 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2858 2859 zcmd_free_nvlists(&zc); 2860 2861 if (ret != 0) { 2862 switch (errno) { 2863 2864 case ENOENT: 2865 /* 2866 * The parent doesn't exist. We should have caught this 2867 * above, but there may a race condition that has since 2868 * destroyed the parent. 2869 * 2870 * At this point, we don't know whether it's the source 2871 * that doesn't exist anymore, or whether the target 2872 * dataset doesn't exist. 2873 */ 2874 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2875 "no such parent '%s'"), parent); 2876 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 2877 2878 case EXDEV: 2879 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2880 "source and target pools differ")); 2881 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 2882 errbuf)); 2883 2884 default: 2885 return (zfs_standard_error(zhp->zfs_hdl, errno, 2886 errbuf)); 2887 } 2888 } 2889 2890 return (ret); 2891 } 2892 2893 /* 2894 * Promotes the given clone fs to be the clone parent. 2895 */ 2896 int 2897 zfs_promote(zfs_handle_t *zhp) 2898 { 2899 libzfs_handle_t *hdl = zhp->zfs_hdl; 2900 zfs_cmd_t zc = { 0 }; 2901 char parent[MAXPATHLEN]; 2902 int ret; 2903 char errbuf[1024]; 2904 2905 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2906 "cannot promote '%s'"), zhp->zfs_name); 2907 2908 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 2909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2910 "snapshots can not be promoted")); 2911 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2912 } 2913 2914 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 2915 if (parent[0] == '\0') { 2916 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2917 "not a cloned filesystem")); 2918 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2919 } 2920 2921 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 2922 sizeof (zc.zc_value)); 2923 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2924 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 2925 2926 if (ret != 0) { 2927 int save_errno = errno; 2928 2929 switch (save_errno) { 2930 case EEXIST: 2931 /* There is a conflicting snapshot name. */ 2932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2933 "conflicting snapshot '%s' from parent '%s'"), 2934 zc.zc_string, parent); 2935 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2936 2937 default: 2938 return (zfs_standard_error(hdl, save_errno, errbuf)); 2939 } 2940 } 2941 return (ret); 2942 } 2943 2944 /* 2945 * Takes a snapshot of the given dataset. 2946 */ 2947 int 2948 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 2949 nvlist_t *props) 2950 { 2951 const char *delim; 2952 char parent[ZFS_MAXNAMELEN]; 2953 zfs_handle_t *zhp; 2954 zfs_cmd_t zc = { 0 }; 2955 int ret; 2956 char errbuf[1024]; 2957 2958 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2959 "cannot snapshot '%s'"), path); 2960 2961 /* validate the target name */ 2962 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 2963 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2964 2965 if (props) { 2966 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 2967 props, B_FALSE, NULL, errbuf)) == NULL) 2968 return (-1); 2969 2970 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 2971 nvlist_free(props); 2972 return (-1); 2973 } 2974 2975 nvlist_free(props); 2976 } 2977 2978 /* make sure the parent exists and is of the appropriate type */ 2979 delim = strchr(path, '@'); 2980 (void) strncpy(parent, path, delim - path); 2981 parent[delim - path] = '\0'; 2982 2983 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 2984 ZFS_TYPE_VOLUME)) == NULL) { 2985 zcmd_free_nvlists(&zc); 2986 return (-1); 2987 } 2988 2989 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2990 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 2991 if (ZFS_IS_VOLUME(zhp)) 2992 zc.zc_objset_type = DMU_OST_ZVOL; 2993 else 2994 zc.zc_objset_type = DMU_OST_ZFS; 2995 zc.zc_cookie = recursive; 2996 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 2997 2998 zcmd_free_nvlists(&zc); 2999 3000 /* 3001 * if it was recursive, the one that actually failed will be in 3002 * zc.zc_name. 3003 */ 3004 if (ret != 0) { 3005 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3006 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 3007 (void) zfs_standard_error(hdl, errno, errbuf); 3008 } 3009 3010 zfs_close(zhp); 3011 3012 return (ret); 3013 } 3014 3015 /* 3016 * Destroy any more recent snapshots. We invoke this callback on any dependents 3017 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3018 * is a dependent and we should just destroy it without checking the transaction 3019 * group. 3020 */ 3021 typedef struct rollback_data { 3022 const char *cb_target; /* the snapshot */ 3023 uint64_t cb_create; /* creation time reference */ 3024 boolean_t cb_error; 3025 boolean_t cb_dependent; 3026 boolean_t cb_force; 3027 } rollback_data_t; 3028 3029 static int 3030 rollback_destroy(zfs_handle_t *zhp, void *data) 3031 { 3032 rollback_data_t *cbp = data; 3033 3034 if (!cbp->cb_dependent) { 3035 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 3036 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 3037 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 3038 cbp->cb_create) { 3039 char *logstr; 3040 3041 cbp->cb_dependent = B_TRUE; 3042 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 3043 rollback_destroy, cbp); 3044 cbp->cb_dependent = B_FALSE; 3045 3046 logstr = zhp->zfs_hdl->libzfs_log_str; 3047 zhp->zfs_hdl->libzfs_log_str = NULL; 3048 cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 3049 zhp->zfs_hdl->libzfs_log_str = logstr; 3050 } 3051 } else { 3052 /* We must destroy this clone; first unmount it */ 3053 prop_changelist_t *clp; 3054 3055 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 3056 cbp->cb_force ? MS_FORCE: 0); 3057 if (clp == NULL || changelist_prefix(clp) != 0) { 3058 cbp->cb_error = B_TRUE; 3059 zfs_close(zhp); 3060 return (0); 3061 } 3062 if (zfs_destroy(zhp, B_FALSE) != 0) 3063 cbp->cb_error = B_TRUE; 3064 else 3065 changelist_remove(clp, zhp->zfs_name); 3066 (void) changelist_postfix(clp); 3067 changelist_free(clp); 3068 } 3069 3070 zfs_close(zhp); 3071 return (0); 3072 } 3073 3074 /* 3075 * Given a dataset, rollback to a specific snapshot, discarding any 3076 * data changes since then and making it the active dataset. 3077 * 3078 * Any snapshots more recent than the target are destroyed, along with 3079 * their dependents. 3080 */ 3081 int 3082 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3083 { 3084 rollback_data_t cb = { 0 }; 3085 int err; 3086 zfs_cmd_t zc = { 0 }; 3087 boolean_t restore_resv = 0; 3088 uint64_t old_volsize, new_volsize; 3089 zfs_prop_t resv_prop; 3090 3091 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3092 zhp->zfs_type == ZFS_TYPE_VOLUME); 3093 3094 /* 3095 * Destroy all recent snapshots and its dependends. 3096 */ 3097 cb.cb_force = force; 3098 cb.cb_target = snap->zfs_name; 3099 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3100 (void) zfs_iter_children(zhp, rollback_destroy, &cb); 3101 3102 if (cb.cb_error) 3103 return (-1); 3104 3105 /* 3106 * Now that we have verified that the snapshot is the latest, 3107 * rollback to the given snapshot. 3108 */ 3109 3110 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3111 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 3112 return (-1); 3113 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 3114 restore_resv = 3115 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 3116 } 3117 3118 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3119 3120 if (ZFS_IS_VOLUME(zhp)) 3121 zc.zc_objset_type = DMU_OST_ZVOL; 3122 else 3123 zc.zc_objset_type = DMU_OST_ZFS; 3124 3125 /* 3126 * We rely on zfs_iter_children() to verify that there are no 3127 * newer snapshots for the given dataset. Therefore, we can 3128 * simply pass the name on to the ioctl() call. There is still 3129 * an unlikely race condition where the user has taken a 3130 * snapshot since we verified that this was the most recent. 3131 * 3132 */ 3133 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 3134 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3135 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 3136 zhp->zfs_name); 3137 return (err); 3138 } 3139 3140 /* 3141 * For volumes, if the pre-rollback volsize matched the pre- 3142 * rollback reservation and the volsize has changed then set 3143 * the reservation property to the post-rollback volsize. 3144 * Make a new handle since the rollback closed the dataset. 3145 */ 3146 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 3147 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 3148 if (restore_resv) { 3149 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 3150 if (old_volsize != new_volsize) 3151 err = zfs_prop_set_int(zhp, resv_prop, 3152 new_volsize); 3153 } 3154 zfs_close(zhp); 3155 } 3156 return (err); 3157 } 3158 3159 /* 3160 * Iterate over all dependents for a given dataset. This includes both 3161 * hierarchical dependents (children) and data dependents (snapshots and 3162 * clones). The bulk of the processing occurs in get_dependents() in 3163 * libzfs_graph.c. 3164 */ 3165 int 3166 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 3167 zfs_iter_f func, void *data) 3168 { 3169 char **dependents; 3170 size_t count; 3171 int i; 3172 zfs_handle_t *child; 3173 int ret = 0; 3174 3175 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 3176 &dependents, &count) != 0) 3177 return (-1); 3178 3179 for (i = 0; i < count; i++) { 3180 if ((child = make_dataset_handle(zhp->zfs_hdl, 3181 dependents[i])) == NULL) 3182 continue; 3183 3184 if ((ret = func(child, data)) != 0) 3185 break; 3186 } 3187 3188 for (i = 0; i < count; i++) 3189 free(dependents[i]); 3190 free(dependents); 3191 3192 return (ret); 3193 } 3194 3195 /* 3196 * Renames the given dataset. 3197 */ 3198 int 3199 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3200 { 3201 int ret; 3202 zfs_cmd_t zc = { 0 }; 3203 char *delim; 3204 prop_changelist_t *cl = NULL; 3205 zfs_handle_t *zhrp = NULL; 3206 char *parentname = NULL; 3207 char parent[ZFS_MAXNAMELEN]; 3208 libzfs_handle_t *hdl = zhp->zfs_hdl; 3209 char errbuf[1024]; 3210 3211 /* if we have the same exact name, just return success */ 3212 if (strcmp(zhp->zfs_name, target) == 0) 3213 return (0); 3214 3215 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3216 "cannot rename to '%s'"), target); 3217 3218 /* 3219 * Make sure the target name is valid 3220 */ 3221 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3222 if ((strchr(target, '@') == NULL) || 3223 *target == '@') { 3224 /* 3225 * Snapshot target name is abbreviated, 3226 * reconstruct full dataset name 3227 */ 3228 (void) strlcpy(parent, zhp->zfs_name, 3229 sizeof (parent)); 3230 delim = strchr(parent, '@'); 3231 if (strchr(target, '@') == NULL) 3232 *(++delim) = '\0'; 3233 else 3234 *delim = '\0'; 3235 (void) strlcat(parent, target, sizeof (parent)); 3236 target = parent; 3237 } else { 3238 /* 3239 * Make sure we're renaming within the same dataset. 3240 */ 3241 delim = strchr(target, '@'); 3242 if (strncmp(zhp->zfs_name, target, delim - target) 3243 != 0 || zhp->zfs_name[delim - target] != '@') { 3244 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3245 "snapshots must be part of same " 3246 "dataset")); 3247 return (zfs_error(hdl, EZFS_CROSSTARGET, 3248 errbuf)); 3249 } 3250 } 3251 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 3252 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3253 } else { 3254 if (recursive) { 3255 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3256 "recursive rename must be a snapshot")); 3257 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3258 } 3259 3260 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 3261 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3262 uint64_t unused; 3263 3264 /* validate parents */ 3265 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3266 return (-1); 3267 3268 (void) parent_name(target, parent, sizeof (parent)); 3269 3270 /* make sure we're in the same pool */ 3271 verify((delim = strchr(target, '/')) != NULL); 3272 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3273 zhp->zfs_name[delim - target] != '/') { 3274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3275 "datasets must be within same pool")); 3276 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3277 } 3278 3279 /* new name cannot be a child of the current dataset name */ 3280 if (strncmp(parent, zhp->zfs_name, 3281 strlen(zhp->zfs_name)) == 0) { 3282 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3283 "New dataset name cannot be a descendent of " 3284 "current dataset name")); 3285 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3286 } 3287 } 3288 3289 (void) snprintf(errbuf, sizeof (errbuf), 3290 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 3291 3292 if (getzoneid() == GLOBAL_ZONEID && 3293 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 3294 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3295 "dataset is used in a non-global zone")); 3296 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3297 } 3298 3299 if (recursive) { 3300 3301 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 3302 if (parentname == NULL) { 3303 ret = -1; 3304 goto error; 3305 } 3306 delim = strchr(parentname, '@'); 3307 *delim = '\0'; 3308 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 3309 if (zhrp == NULL) { 3310 ret = -1; 3311 goto error; 3312 } 3313 3314 } else { 3315 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL) 3316 return (-1); 3317 3318 if (changelist_haszonedchild(cl)) { 3319 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3320 "child dataset with inherited mountpoint is used " 3321 "in a non-global zone")); 3322 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 3323 goto error; 3324 } 3325 3326 if ((ret = changelist_prefix(cl)) != 0) 3327 goto error; 3328 } 3329 3330 if (ZFS_IS_VOLUME(zhp)) 3331 zc.zc_objset_type = DMU_OST_ZVOL; 3332 else 3333 zc.zc_objset_type = DMU_OST_ZFS; 3334 3335 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3336 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 3337 3338 zc.zc_cookie = recursive; 3339 3340 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 3341 /* 3342 * if it was recursive, the one that actually failed will 3343 * be in zc.zc_name 3344 */ 3345 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3346 "cannot rename '%s'"), zc.zc_name); 3347 3348 if (recursive && errno == EEXIST) { 3349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3350 "a child dataset already has a snapshot " 3351 "with the new name")); 3352 (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 3353 } else { 3354 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 3355 } 3356 3357 /* 3358 * On failure, we still want to remount any filesystems that 3359 * were previously mounted, so we don't alter the system state. 3360 */ 3361 if (!recursive) 3362 (void) changelist_postfix(cl); 3363 } else { 3364 if (!recursive) { 3365 changelist_rename(cl, zfs_get_name(zhp), target); 3366 ret = changelist_postfix(cl); 3367 } 3368 } 3369 3370 error: 3371 if (parentname) { 3372 free(parentname); 3373 } 3374 if (zhrp) { 3375 zfs_close(zhrp); 3376 } 3377 if (cl) { 3378 changelist_free(cl); 3379 } 3380 return (ret); 3381 } 3382 3383 nvlist_t * 3384 zfs_get_user_props(zfs_handle_t *zhp) 3385 { 3386 return (zhp->zfs_user_props); 3387 } 3388 3389 /* 3390 * This function is used by 'zfs list' to determine the exact set of columns to 3391 * display, and their maximum widths. This does two main things: 3392 * 3393 * - If this is a list of all properties, then expand the list to include 3394 * all native properties, and set a flag so that for each dataset we look 3395 * for new unique user properties and add them to the list. 3396 * 3397 * - For non fixed-width properties, keep track of the maximum width seen 3398 * so that we can size the column appropriately. 3399 */ 3400 int 3401 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 3402 { 3403 libzfs_handle_t *hdl = zhp->zfs_hdl; 3404 zprop_list_t *entry; 3405 zprop_list_t **last, **start; 3406 nvlist_t *userprops, *propval; 3407 nvpair_t *elem; 3408 char *strval; 3409 char buf[ZFS_MAXPROPLEN]; 3410 3411 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 3412 return (-1); 3413 3414 userprops = zfs_get_user_props(zhp); 3415 3416 entry = *plp; 3417 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 3418 /* 3419 * Go through and add any user properties as necessary. We 3420 * start by incrementing our list pointer to the first 3421 * non-native property. 3422 */ 3423 start = plp; 3424 while (*start != NULL) { 3425 if ((*start)->pl_prop == ZPROP_INVAL) 3426 break; 3427 start = &(*start)->pl_next; 3428 } 3429 3430 elem = NULL; 3431 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 3432 /* 3433 * See if we've already found this property in our list. 3434 */ 3435 for (last = start; *last != NULL; 3436 last = &(*last)->pl_next) { 3437 if (strcmp((*last)->pl_user_prop, 3438 nvpair_name(elem)) == 0) 3439 break; 3440 } 3441 3442 if (*last == NULL) { 3443 if ((entry = zfs_alloc(hdl, 3444 sizeof (zprop_list_t))) == NULL || 3445 ((entry->pl_user_prop = zfs_strdup(hdl, 3446 nvpair_name(elem)))) == NULL) { 3447 free(entry); 3448 return (-1); 3449 } 3450 3451 entry->pl_prop = ZPROP_INVAL; 3452 entry->pl_width = strlen(nvpair_name(elem)); 3453 entry->pl_all = B_TRUE; 3454 *last = entry; 3455 } 3456 } 3457 } 3458 3459 /* 3460 * Now go through and check the width of any non-fixed columns 3461 */ 3462 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 3463 if (entry->pl_fixed) 3464 continue; 3465 3466 if (entry->pl_prop != ZPROP_INVAL) { 3467 if (zfs_prop_get(zhp, entry->pl_prop, 3468 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 3469 if (strlen(buf) > entry->pl_width) 3470 entry->pl_width = strlen(buf); 3471 } 3472 } else if (nvlist_lookup_nvlist(userprops, 3473 entry->pl_user_prop, &propval) == 0) { 3474 verify(nvlist_lookup_string(propval, 3475 ZPROP_VALUE, &strval) == 0); 3476 if (strlen(strval) > entry->pl_width) 3477 entry->pl_width = strlen(strval); 3478 } 3479 } 3480 3481 return (0); 3482 } 3483 3484 int 3485 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 3486 { 3487 zfs_cmd_t zc = { 0 }; 3488 nvlist_t *nvp; 3489 gid_t gid; 3490 uid_t uid; 3491 const gid_t *groups; 3492 int group_cnt; 3493 int error; 3494 3495 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 3496 return (no_memory(hdl)); 3497 3498 uid = ucred_geteuid(cred); 3499 gid = ucred_getegid(cred); 3500 group_cnt = ucred_getgroups(cred, &groups); 3501 3502 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 3503 return (1); 3504 3505 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 3506 nvlist_free(nvp); 3507 return (1); 3508 } 3509 3510 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 3511 nvlist_free(nvp); 3512 return (1); 3513 } 3514 3515 if (nvlist_add_uint32_array(nvp, 3516 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 3517 nvlist_free(nvp); 3518 return (1); 3519 } 3520 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3521 3522 if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 3523 return (-1); 3524 3525 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 3526 nvlist_free(nvp); 3527 return (error); 3528 } 3529 3530 int 3531 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 3532 char *resource, void *export, void *sharetab, 3533 int sharemax, zfs_share_op_t operation) 3534 { 3535 zfs_cmd_t zc = { 0 }; 3536 int error; 3537 3538 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3539 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 3540 if (resource) 3541 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 3542 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 3543 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 3544 zc.zc_share.z_sharetype = operation; 3545 zc.zc_share.z_sharemax = sharemax; 3546 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 3547 return (error); 3548 } 3549 3550 void 3551 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 3552 { 3553 nvpair_t *curr; 3554 3555 /* 3556 * Keep a reference to the props-table against which we prune the 3557 * properties. 3558 */ 3559 zhp->zfs_props_table = props; 3560 3561 curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 3562 3563 while (curr) { 3564 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 3565 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 3566 3567 /* 3568 * User properties will result in ZPROP_INVAL, and since we 3569 * only know how to prune standard ZFS properties, we always 3570 * leave these in the list. This can also happen if we 3571 * encounter an unknown DSL property (when running older 3572 * software, for example). 3573 */ 3574 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 3575 (void) nvlist_remove(zhp->zfs_props, 3576 nvpair_name(curr), nvpair_type(curr)); 3577 curr = next; 3578 } 3579 } 3580 3581 static int 3582 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 3583 zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 3584 { 3585 zfs_cmd_t zc = { 0 }; 3586 nvlist_t *nvlist = NULL; 3587 int error; 3588 3589 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3590 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 3591 zc.zc_cookie = (uint64_t)cmd; 3592 3593 if (cmd == ZFS_SMB_ACL_RENAME) { 3594 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 3595 (void) no_memory(hdl); 3596 return (NULL); 3597 } 3598 } 3599 3600 switch (cmd) { 3601 case ZFS_SMB_ACL_ADD: 3602 case ZFS_SMB_ACL_REMOVE: 3603 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 3604 break; 3605 case ZFS_SMB_ACL_RENAME: 3606 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 3607 resource1) != 0) { 3608 (void) no_memory(hdl); 3609 return (-1); 3610 } 3611 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 3612 resource2) != 0) { 3613 (void) no_memory(hdl); 3614 return (-1); 3615 } 3616 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 3617 nvlist_free(nvlist); 3618 return (-1); 3619 } 3620 break; 3621 case ZFS_SMB_ACL_PURGE: 3622 break; 3623 default: 3624 return (-1); 3625 } 3626 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 3627 if (nvlist) 3628 nvlist_free(nvlist); 3629 return (error); 3630 } 3631 3632 int 3633 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 3634 char *path, char *resource) 3635 { 3636 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 3637 resource, NULL)); 3638 } 3639 3640 int 3641 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 3642 char *path, char *resource) 3643 { 3644 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 3645 resource, NULL)); 3646 } 3647 3648 int 3649 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 3650 { 3651 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 3652 NULL, NULL)); 3653 } 3654 3655 int 3656 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 3657 char *oldname, char *newname) 3658 { 3659 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 3660 oldname, newname)); 3661 } 3662 3663 int 3664 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 3665 zfs_userspace_cb_t func, void *arg) 3666 { 3667 zfs_cmd_t zc = { 0 }; 3668 int error; 3669 zfs_useracct_t buf[100]; 3670 3671 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3672 3673 zc.zc_objset_type = type; 3674 zc.zc_nvlist_dst = (uintptr_t)buf; 3675 3676 /* CONSTCOND */ 3677 while (1) { 3678 zfs_useracct_t *zua = buf; 3679 3680 zc.zc_nvlist_dst_size = sizeof (buf); 3681 error = ioctl(zhp->zfs_hdl->libzfs_fd, 3682 ZFS_IOC_USERSPACE_MANY, &zc); 3683 if (error || zc.zc_nvlist_dst_size == 0) 3684 break; 3685 3686 while (zc.zc_nvlist_dst_size > 0) { 3687 error = func(arg, zua->zu_domain, zua->zu_rid, 3688 zua->zu_space); 3689 if (error != 0) 3690 return (error); 3691 zua++; 3692 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 3693 } 3694 } 3695 3696 return (error); 3697 } 3698 3699 int 3700 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 3701 boolean_t recursive, boolean_t temphold) 3702 { 3703 zfs_cmd_t zc = { 0 }; 3704 libzfs_handle_t *hdl = zhp->zfs_hdl; 3705 3706 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3707 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 3708 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string)) 3709 >= sizeof (zc.zc_string)) 3710 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag)); 3711 zc.zc_cookie = recursive; 3712 zc.zc_temphold = temphold; 3713 3714 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) { 3715 char errbuf[ZFS_MAXNAMELEN+32]; 3716 3717 /* 3718 * if it was recursive, the one that actually failed will be in 3719 * zc.zc_name. 3720 */ 3721 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3722 "cannot hold '%s@%s'"), zc.zc_name, snapname); 3723 switch (errno) { 3724 case E2BIG: 3725 /* 3726 * Temporary tags wind up having the ds object id 3727 * prepended. So even if we passed the length check 3728 * above, it's still possible for the tag to wind 3729 * up being slightly too long. 3730 */ 3731 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf)); 3732 case ENOTSUP: 3733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3734 "pool must be upgraded")); 3735 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3736 case EINVAL: 3737 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3738 case EEXIST: 3739 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf)); 3740 default: 3741 return (zfs_standard_error_fmt(hdl, errno, errbuf)); 3742 } 3743 } 3744 3745 return (0); 3746 } 3747 3748 struct hold_range_arg { 3749 zfs_handle_t *origin; 3750 const char *fromsnap; 3751 const char *tosnap; 3752 char lastsnapheld[ZFS_MAXNAMELEN]; 3753 const char *tag; 3754 boolean_t temphold; 3755 boolean_t seento; 3756 boolean_t seenfrom; 3757 boolean_t holding; 3758 }; 3759 3760 static int 3761 zfs_hold_range_one(zfs_handle_t *zhp, void *arg) 3762 { 3763 struct hold_range_arg *hra = arg; 3764 const char *thissnap; 3765 int error; 3766 3767 thissnap = strchr(zfs_get_name(zhp), '@') + 1; 3768 3769 if (hra->fromsnap && !hra->seenfrom && 3770 strcmp(hra->fromsnap, thissnap) == 0) 3771 hra->seenfrom = B_TRUE; 3772 3773 /* snap is older or newer than the desired range, ignore it */ 3774 if (hra->seento || !hra->seenfrom) { 3775 zfs_close(zhp); 3776 return (0); 3777 } 3778 3779 if (hra->holding) { 3780 error = zfs_hold(hra->origin, thissnap, hra->tag, B_FALSE, 3781 hra->temphold); 3782 if (error == 0) { 3783 (void) strlcpy(hra->lastsnapheld, zfs_get_name(zhp), 3784 sizeof (hra->lastsnapheld)); 3785 } 3786 } else { 3787 error = zfs_release(hra->origin, thissnap, hra->tag, B_FALSE); 3788 } 3789 3790 if (!hra->seento && strcmp(hra->tosnap, thissnap) == 0) 3791 hra->seento = B_TRUE; 3792 3793 zfs_close(zhp); 3794 return (error); 3795 } 3796 3797 /* 3798 * Add a user hold on the set of snapshots starting with fromsnap up to 3799 * and including tosnap. If we're unable to to acquire a particular hold, 3800 * undo any holds up to that point. 3801 */ 3802 int 3803 zfs_hold_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 3804 const char *tag, boolean_t temphold) 3805 { 3806 struct hold_range_arg arg = { 0 }; 3807 int error; 3808 3809 arg.origin = zhp; 3810 arg.fromsnap = fromsnap; 3811 arg.tosnap = tosnap; 3812 arg.tag = tag; 3813 arg.temphold = temphold; 3814 arg.holding = B_TRUE; 3815 3816 error = zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg); 3817 3818 /* 3819 * Make sure we either hold the entire range or none. 3820 */ 3821 if (error && arg.lastsnapheld[0] != '\0') { 3822 (void) zfs_release_range(zhp, fromsnap, 3823 (const char *)arg.lastsnapheld, tag); 3824 } 3825 return (error); 3826 } 3827 3828 int 3829 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 3830 boolean_t recursive) 3831 { 3832 zfs_cmd_t zc = { 0 }; 3833 libzfs_handle_t *hdl = zhp->zfs_hdl; 3834 3835 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3836 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 3837 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string)) 3838 >= sizeof (zc.zc_string)) 3839 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag)); 3840 zc.zc_cookie = recursive; 3841 3842 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) { 3843 char errbuf[ZFS_MAXNAMELEN+32]; 3844 3845 /* 3846 * if it was recursive, the one that actually failed will be in 3847 * zc.zc_name. 3848 */ 3849 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3850 "cannot release '%s@%s'"), zc.zc_name, snapname); 3851 switch (errno) { 3852 case ESRCH: 3853 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf)); 3854 case ENOTSUP: 3855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3856 "pool must be upgraded")); 3857 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3858 case EINVAL: 3859 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3860 default: 3861 return (zfs_standard_error_fmt(hdl, errno, errbuf)); 3862 } 3863 } 3864 3865 return (0); 3866 } 3867 3868 /* 3869 * Release a user hold from the set of snapshots starting with fromsnap 3870 * up to and including tosnap. 3871 */ 3872 int 3873 zfs_release_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 3874 const char *tag) 3875 { 3876 struct hold_range_arg arg = { 0 }; 3877 3878 arg.origin = zhp; 3879 arg.fromsnap = fromsnap; 3880 arg.tosnap = tosnap; 3881 arg.tag = tag; 3882 3883 return (zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg)); 3884 } 3885