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