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 https://opensource.org/licenses/CDDL-1.0. 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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright 2019 Joyent, Inc. 25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. 27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. 28 * Copyright (c) 2013 Martin Matuska. All rights reserved. 29 * Copyright (c) 2013 Steven Hartland. All rights reserved. 30 * Copyright 2017 Nexenta Systems, Inc. 31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 32 * Copyright 2017-2018 RackTop Systems. 33 * Copyright (c) 2019 Datto Inc. 34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com> 35 * Copyright (c) 2021 Matt Fiddaman 36 */ 37 38 #include <ctype.h> 39 #include <errno.h> 40 #include <libintl.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <strings.h> 44 #include <unistd.h> 45 #include <stddef.h> 46 #include <zone.h> 47 #include <fcntl.h> 48 #include <sys/mntent.h> 49 #include <sys/mount.h> 50 #include <pwd.h> 51 #include <grp.h> 52 #ifdef HAVE_IDMAP 53 #include <idmap.h> 54 #include <aclutils.h> 55 #include <directory.h> 56 #endif /* HAVE_IDMAP */ 57 58 #include <sys/dnode.h> 59 #include <sys/spa.h> 60 #include <sys/zap.h> 61 #include <sys/dsl_crypt.h> 62 #include <libzfs.h> 63 #include <libzutil.h> 64 65 #include "zfs_namecheck.h" 66 #include "zfs_prop.h" 67 #include "libzfs_impl.h" 68 #include "zfs_deleg.h" 69 70 static int userquota_propname_decode(const char *propname, boolean_t zoned, 71 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 72 73 /* 74 * Given a single type (not a mask of types), return the type in a human 75 * readable form. 76 */ 77 const char * 78 zfs_type_to_name(zfs_type_t type) 79 { 80 switch (type) { 81 case ZFS_TYPE_FILESYSTEM: 82 return (dgettext(TEXT_DOMAIN, "filesystem")); 83 case ZFS_TYPE_SNAPSHOT: 84 return (dgettext(TEXT_DOMAIN, "snapshot")); 85 case ZFS_TYPE_VOLUME: 86 return (dgettext(TEXT_DOMAIN, "volume")); 87 case ZFS_TYPE_POOL: 88 return (dgettext(TEXT_DOMAIN, "pool")); 89 case ZFS_TYPE_BOOKMARK: 90 return (dgettext(TEXT_DOMAIN, "bookmark")); 91 default: 92 assert(!"unhandled zfs_type_t"); 93 } 94 95 return (NULL); 96 } 97 98 /* 99 * Validate a ZFS path. This is used even before trying to open the dataset, to 100 * provide a more meaningful error message. We call zfs_error_aux() to 101 * explain exactly why the name was not valid. 102 */ 103 int 104 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 105 boolean_t modifying) 106 { 107 namecheck_err_t why; 108 char what; 109 110 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 111 if (hdl != NULL) 112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 113 "snapshot delimiter '@' is not expected here")); 114 return (0); 115 } 116 117 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 118 if (hdl != NULL) 119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 120 "missing '@' delimiter in snapshot name")); 121 return (0); 122 } 123 124 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) { 125 if (hdl != NULL) 126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 127 "bookmark delimiter '#' is not expected here")); 128 return (0); 129 } 130 131 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) { 132 if (hdl != NULL) 133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 134 "missing '#' delimiter in bookmark name")); 135 return (0); 136 } 137 138 if (modifying && strchr(path, '%') != NULL) { 139 if (hdl != NULL) 140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 141 "invalid character %c in name"), '%'); 142 return (0); 143 } 144 145 if (entity_namecheck(path, &why, &what) != 0) { 146 if (hdl != NULL) { 147 switch (why) { 148 case NAME_ERR_TOOLONG: 149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 150 "name is too long")); 151 break; 152 153 case NAME_ERR_LEADING_SLASH: 154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 155 "leading slash in name")); 156 break; 157 158 case NAME_ERR_EMPTY_COMPONENT: 159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 160 "empty component or misplaced '@'" 161 " or '#' delimiter in name")); 162 break; 163 164 case NAME_ERR_TRAILING_SLASH: 165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 166 "trailing slash in name")); 167 break; 168 169 case NAME_ERR_INVALCHAR: 170 zfs_error_aux(hdl, 171 dgettext(TEXT_DOMAIN, "invalid character " 172 "'%c' in name"), what); 173 break; 174 175 case NAME_ERR_MULTIPLE_DELIMITERS: 176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 177 "multiple '@' and/or '#' delimiters in " 178 "name")); 179 break; 180 181 case NAME_ERR_NOLETTER: 182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 183 "pool doesn't begin with a letter")); 184 break; 185 186 case NAME_ERR_RESERVED: 187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 188 "name is reserved")); 189 break; 190 191 case NAME_ERR_DISKLIKE: 192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 193 "reserved disk name")); 194 break; 195 196 case NAME_ERR_SELF_REF: 197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 198 "self reference, '.' is found in name")); 199 break; 200 201 case NAME_ERR_PARENT_REF: 202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 203 "parent reference, '..' is found in name")); 204 break; 205 206 default: 207 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 208 "(%d) not defined"), why); 209 break; 210 } 211 } 212 213 return (0); 214 } 215 216 return (-1); 217 } 218 219 int 220 zfs_name_valid(const char *name, zfs_type_t type) 221 { 222 if (type == ZFS_TYPE_POOL) 223 return (zpool_name_valid(NULL, B_FALSE, name)); 224 return (zfs_validate_name(NULL, name, type, B_FALSE)); 225 } 226 227 /* 228 * This function takes the raw DSL properties, and filters out the user-defined 229 * properties into a separate nvlist. 230 */ 231 static nvlist_t * 232 process_user_props(zfs_handle_t *zhp, nvlist_t *props) 233 { 234 libzfs_handle_t *hdl = zhp->zfs_hdl; 235 nvpair_t *elem; 236 nvlist_t *nvl; 237 238 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 239 (void) no_memory(hdl); 240 return (NULL); 241 } 242 243 elem = NULL; 244 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 245 if (!zfs_prop_user(nvpair_name(elem))) 246 continue; 247 248 nvlist_t *propval = fnvpair_value_nvlist(elem); 249 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 250 nvlist_free(nvl); 251 (void) no_memory(hdl); 252 return (NULL); 253 } 254 } 255 256 return (nvl); 257 } 258 259 static zpool_handle_t * 260 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 261 { 262 libzfs_handle_t *hdl = zhp->zfs_hdl; 263 zpool_handle_t *zph; 264 265 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 266 if (hdl->libzfs_pool_handles != NULL) 267 zph->zpool_next = hdl->libzfs_pool_handles; 268 hdl->libzfs_pool_handles = zph; 269 } 270 return (zph); 271 } 272 273 static zpool_handle_t * 274 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 275 { 276 libzfs_handle_t *hdl = zhp->zfs_hdl; 277 zpool_handle_t *zph = hdl->libzfs_pool_handles; 278 279 while ((zph != NULL) && 280 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 281 zph = zph->zpool_next; 282 return (zph); 283 } 284 285 /* 286 * Returns a handle to the pool that contains the provided dataset. 287 * If a handle to that pool already exists then that handle is returned. 288 * Otherwise, a new handle is created and added to the list of handles. 289 */ 290 static zpool_handle_t * 291 zpool_handle(zfs_handle_t *zhp) 292 { 293 char *pool_name; 294 int len; 295 zpool_handle_t *zph; 296 297 len = strcspn(zhp->zfs_name, "/@#") + 1; 298 pool_name = zfs_alloc(zhp->zfs_hdl, len); 299 (void) strlcpy(pool_name, zhp->zfs_name, len); 300 301 zph = zpool_find_handle(zhp, pool_name, len); 302 if (zph == NULL) 303 zph = zpool_add_handle(zhp, pool_name); 304 305 free(pool_name); 306 return (zph); 307 } 308 309 void 310 zpool_free_handles(libzfs_handle_t *hdl) 311 { 312 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 313 314 while (zph != NULL) { 315 next = zph->zpool_next; 316 zpool_close(zph); 317 zph = next; 318 } 319 hdl->libzfs_pool_handles = NULL; 320 } 321 322 /* 323 * Utility function to gather stats (objset and zpl) for the given object. 324 */ 325 static int 326 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 327 { 328 libzfs_handle_t *hdl = zhp->zfs_hdl; 329 330 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 331 332 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, zc) != 0) { 333 if (errno == ENOMEM) 334 zcmd_expand_dst_nvlist(hdl, zc); 335 else 336 return (-1); 337 } 338 return (0); 339 } 340 341 /* 342 * Utility function to get the received properties of the given object. 343 */ 344 static int 345 get_recvd_props_ioctl(zfs_handle_t *zhp) 346 { 347 libzfs_handle_t *hdl = zhp->zfs_hdl; 348 nvlist_t *recvdprops; 349 zfs_cmd_t zc = {"\0"}; 350 int err; 351 352 zcmd_alloc_dst_nvlist(hdl, &zc, 0); 353 354 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 355 356 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 357 if (errno == ENOMEM) 358 zcmd_expand_dst_nvlist(hdl, &zc); 359 else { 360 zcmd_free_nvlists(&zc); 361 return (-1); 362 } 363 } 364 365 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 366 zcmd_free_nvlists(&zc); 367 if (err != 0) 368 return (-1); 369 370 nvlist_free(zhp->zfs_recvd_props); 371 zhp->zfs_recvd_props = recvdprops; 372 373 return (0); 374 } 375 376 static int 377 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 378 { 379 nvlist_t *allprops, *userprops; 380 381 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 382 383 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 384 return (-1); 385 } 386 387 /* 388 * XXX Why do we store the user props separately, in addition to 389 * storing them in zfs_props? 390 */ 391 if ((userprops = process_user_props(zhp, allprops)) == NULL) { 392 nvlist_free(allprops); 393 return (-1); 394 } 395 396 nvlist_free(zhp->zfs_props); 397 nvlist_free(zhp->zfs_user_props); 398 399 zhp->zfs_props = allprops; 400 zhp->zfs_user_props = userprops; 401 402 return (0); 403 } 404 405 static int 406 get_stats(zfs_handle_t *zhp) 407 { 408 int rc = 0; 409 zfs_cmd_t zc = {"\0"}; 410 411 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0); 412 413 if (get_stats_ioctl(zhp, &zc) != 0) 414 rc = -1; 415 else if (put_stats_zhdl(zhp, &zc) != 0) 416 rc = -1; 417 zcmd_free_nvlists(&zc); 418 return (rc); 419 } 420 421 /* 422 * Refresh the properties currently stored in the handle. 423 */ 424 void 425 zfs_refresh_properties(zfs_handle_t *zhp) 426 { 427 (void) get_stats(zhp); 428 } 429 430 /* 431 * Makes a handle from the given dataset name. Used by zfs_open() and 432 * zfs_iter_* to create child handles on the fly. 433 */ 434 static int 435 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 436 { 437 if (put_stats_zhdl(zhp, zc) != 0) 438 return (-1); 439 440 /* 441 * We've managed to open the dataset and gather statistics. Determine 442 * the high-level type. 443 */ 444 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 445 zhp->zfs_head_type = ZFS_TYPE_VOLUME; 446 } else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) { 447 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 448 } else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER) { 449 errno = EINVAL; 450 return (-1); 451 } else if (zhp->zfs_dmustats.dds_inconsistent) { 452 errno = EBUSY; 453 return (-1); 454 } else { 455 abort(); 456 } 457 458 if (zhp->zfs_dmustats.dds_is_snapshot) 459 zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 460 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 461 zhp->zfs_type = ZFS_TYPE_VOLUME; 462 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 463 zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 464 else 465 abort(); /* we should never see any other types */ 466 467 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 468 return (-1); 469 470 return (0); 471 } 472 473 zfs_handle_t * 474 make_dataset_handle(libzfs_handle_t *hdl, const char *path) 475 { 476 zfs_cmd_t zc = {"\0"}; 477 478 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); 479 480 if (zhp == NULL) 481 return (NULL); 482 483 zhp->zfs_hdl = hdl; 484 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 485 zcmd_alloc_dst_nvlist(hdl, &zc, 0); 486 487 if (get_stats_ioctl(zhp, &zc) == -1) { 488 zcmd_free_nvlists(&zc); 489 free(zhp); 490 return (NULL); 491 } 492 if (make_dataset_handle_common(zhp, &zc) == -1) { 493 free(zhp); 494 zhp = NULL; 495 } 496 zcmd_free_nvlists(&zc); 497 return (zhp); 498 } 499 500 zfs_handle_t * 501 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 502 { 503 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); 504 505 if (zhp == NULL) 506 return (NULL); 507 508 zhp->zfs_hdl = hdl; 509 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 510 if (make_dataset_handle_common(zhp, zc) == -1) { 511 free(zhp); 512 return (NULL); 513 } 514 return (zhp); 515 } 516 517 zfs_handle_t * 518 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc) 519 { 520 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); 521 522 if (zhp == NULL) 523 return (NULL); 524 525 zhp->zfs_hdl = pzhp->zfs_hdl; 526 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 527 zhp->zfs_head_type = pzhp->zfs_type; 528 zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 529 zhp->zpool_hdl = zpool_handle(zhp); 530 zhp->zfs_dmustats = zc->zc_objset_stats; 531 532 return (zhp); 533 } 534 535 zfs_handle_t * 536 zfs_handle_dup(zfs_handle_t *zhp_orig) 537 { 538 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); 539 540 if (zhp == NULL) 541 return (NULL); 542 543 zhp->zfs_hdl = zhp_orig->zfs_hdl; 544 zhp->zpool_hdl = zhp_orig->zpool_hdl; 545 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 546 sizeof (zhp->zfs_name)); 547 zhp->zfs_type = zhp_orig->zfs_type; 548 zhp->zfs_head_type = zhp_orig->zfs_head_type; 549 zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 550 if (zhp_orig->zfs_props != NULL) { 551 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { 552 (void) no_memory(zhp->zfs_hdl); 553 zfs_close(zhp); 554 return (NULL); 555 } 556 } 557 if (zhp_orig->zfs_user_props != NULL) { 558 if (nvlist_dup(zhp_orig->zfs_user_props, 559 &zhp->zfs_user_props, 0) != 0) { 560 (void) no_memory(zhp->zfs_hdl); 561 zfs_close(zhp); 562 return (NULL); 563 } 564 } 565 if (zhp_orig->zfs_recvd_props != NULL) { 566 if (nvlist_dup(zhp_orig->zfs_recvd_props, 567 &zhp->zfs_recvd_props, 0)) { 568 (void) no_memory(zhp->zfs_hdl); 569 zfs_close(zhp); 570 return (NULL); 571 } 572 } 573 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 574 if (zhp_orig->zfs_mntopts != NULL) { 575 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 576 zhp_orig->zfs_mntopts); 577 } 578 zhp->zfs_props_table = zhp_orig->zfs_props_table; 579 return (zhp); 580 } 581 582 boolean_t 583 zfs_bookmark_exists(const char *path) 584 { 585 nvlist_t *bmarks; 586 nvlist_t *props; 587 char fsname[ZFS_MAX_DATASET_NAME_LEN]; 588 char *bmark_name; 589 char *pound; 590 int err; 591 boolean_t rv; 592 593 (void) strlcpy(fsname, path, sizeof (fsname)); 594 pound = strchr(fsname, '#'); 595 if (pound == NULL) 596 return (B_FALSE); 597 598 *pound = '\0'; 599 bmark_name = pound + 1; 600 props = fnvlist_alloc(); 601 err = lzc_get_bookmarks(fsname, props, &bmarks); 602 nvlist_free(props); 603 if (err != 0) { 604 nvlist_free(bmarks); 605 return (B_FALSE); 606 } 607 608 rv = nvlist_exists(bmarks, bmark_name); 609 nvlist_free(bmarks); 610 return (rv); 611 } 612 613 zfs_handle_t * 614 make_bookmark_handle(zfs_handle_t *parent, const char *path, 615 nvlist_t *bmark_props) 616 { 617 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); 618 619 if (zhp == NULL) 620 return (NULL); 621 622 /* Fill in the name. */ 623 zhp->zfs_hdl = parent->zfs_hdl; 624 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 625 626 /* Set the property lists. */ 627 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { 628 free(zhp); 629 return (NULL); 630 } 631 632 /* Set the types. */ 633 zhp->zfs_head_type = parent->zfs_head_type; 634 zhp->zfs_type = ZFS_TYPE_BOOKMARK; 635 636 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { 637 nvlist_free(zhp->zfs_props); 638 free(zhp); 639 return (NULL); 640 } 641 642 return (zhp); 643 } 644 645 struct zfs_open_bookmarks_cb_data { 646 const char *path; 647 zfs_handle_t *zhp; 648 }; 649 650 static int 651 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data) 652 { 653 struct zfs_open_bookmarks_cb_data *dp = data; 654 655 /* 656 * Is it the one we are looking for? 657 */ 658 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) { 659 /* 660 * We found it. Save it and let the caller know we are done. 661 */ 662 dp->zhp = zhp; 663 return (EEXIST); 664 } 665 666 /* 667 * Not found. Close the handle and ask for another one. 668 */ 669 zfs_close(zhp); 670 return (0); 671 } 672 673 /* 674 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types' 675 * argument is a mask of acceptable types. The function will print an 676 * appropriate error message and return NULL if it can't be opened. 677 */ 678 zfs_handle_t * 679 zfs_open(libzfs_handle_t *hdl, const char *path, int types) 680 { 681 zfs_handle_t *zhp; 682 char errbuf[ERRBUFLEN]; 683 char *bookp; 684 685 (void) snprintf(errbuf, sizeof (errbuf), 686 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 687 688 /* 689 * Validate the name before we even try to open it. 690 */ 691 if (!zfs_validate_name(hdl, path, types, B_FALSE)) { 692 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 693 return (NULL); 694 } 695 696 /* 697 * Bookmarks needs to be handled separately. 698 */ 699 bookp = strchr(path, '#'); 700 if (bookp == NULL) { 701 /* 702 * Try to get stats for the dataset, which will tell us if it 703 * exists. 704 */ 705 errno = 0; 706 if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 707 (void) zfs_standard_error(hdl, errno, errbuf); 708 return (NULL); 709 } 710 } else { 711 char dsname[ZFS_MAX_DATASET_NAME_LEN]; 712 zfs_handle_t *pzhp; 713 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL}; 714 715 /* 716 * We need to cut out '#' and everything after '#' 717 * to get the parent dataset name only. 718 */ 719 assert(bookp - path < sizeof (dsname)); 720 (void) strncpy(dsname, path, bookp - path); 721 dsname[bookp - path] = '\0'; 722 723 /* 724 * Create handle for the parent dataset. 725 */ 726 errno = 0; 727 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) { 728 (void) zfs_standard_error(hdl, errno, errbuf); 729 return (NULL); 730 } 731 732 /* 733 * Iterate bookmarks to find the right one. 734 */ 735 errno = 0; 736 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb, 737 &cb_data) == 0) && (cb_data.zhp == NULL)) { 738 (void) zfs_error(hdl, EZFS_NOENT, errbuf); 739 zfs_close(pzhp); 740 return (NULL); 741 } 742 if (cb_data.zhp == NULL) { 743 (void) zfs_standard_error(hdl, errno, errbuf); 744 zfs_close(pzhp); 745 return (NULL); 746 } 747 zhp = cb_data.zhp; 748 749 /* 750 * Cleanup. 751 */ 752 zfs_close(pzhp); 753 } 754 755 if (!(types & zhp->zfs_type)) { 756 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 757 zfs_close(zhp); 758 return (NULL); 759 } 760 761 return (zhp); 762 } 763 764 /* 765 * Release a ZFS handle. Nothing to do but free the associated memory. 766 */ 767 void 768 zfs_close(zfs_handle_t *zhp) 769 { 770 if (zhp->zfs_mntopts) 771 free(zhp->zfs_mntopts); 772 nvlist_free(zhp->zfs_props); 773 nvlist_free(zhp->zfs_user_props); 774 nvlist_free(zhp->zfs_recvd_props); 775 free(zhp); 776 } 777 778 typedef struct mnttab_node { 779 struct mnttab mtn_mt; 780 avl_node_t mtn_node; 781 } mnttab_node_t; 782 783 static int 784 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 785 { 786 const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1; 787 const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2; 788 int rv; 789 790 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 791 792 return (TREE_ISIGN(rv)); 793 } 794 795 void 796 libzfs_mnttab_init(libzfs_handle_t *hdl) 797 { 798 pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL); 799 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 800 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 801 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 802 } 803 804 static int 805 libzfs_mnttab_update(libzfs_handle_t *hdl) 806 { 807 FILE *mnttab; 808 struct mnttab entry; 809 810 if ((mnttab = fopen(MNTTAB, "re")) == NULL) 811 return (ENOENT); 812 813 while (getmntent(mnttab, &entry) == 0) { 814 mnttab_node_t *mtn; 815 avl_index_t where; 816 817 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 818 continue; 819 820 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 821 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 822 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 823 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 824 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 825 826 /* Exclude duplicate mounts */ 827 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) { 828 free(mtn->mtn_mt.mnt_special); 829 free(mtn->mtn_mt.mnt_mountp); 830 free(mtn->mtn_mt.mnt_fstype); 831 free(mtn->mtn_mt.mnt_mntopts); 832 free(mtn); 833 continue; 834 } 835 836 avl_add(&hdl->libzfs_mnttab_cache, mtn); 837 } 838 839 (void) fclose(mnttab); 840 return (0); 841 } 842 843 void 844 libzfs_mnttab_fini(libzfs_handle_t *hdl) 845 { 846 void *cookie = NULL; 847 mnttab_node_t *mtn; 848 849 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) 850 != NULL) { 851 free(mtn->mtn_mt.mnt_special); 852 free(mtn->mtn_mt.mnt_mountp); 853 free(mtn->mtn_mt.mnt_fstype); 854 free(mtn->mtn_mt.mnt_mntopts); 855 free(mtn); 856 } 857 avl_destroy(&hdl->libzfs_mnttab_cache); 858 (void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock); 859 } 860 861 void 862 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 863 { 864 hdl->libzfs_mnttab_enable = enable; 865 } 866 867 int 868 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 869 struct mnttab *entry) 870 { 871 FILE *mnttab; 872 mnttab_node_t find; 873 mnttab_node_t *mtn; 874 int ret = ENOENT; 875 876 if (!hdl->libzfs_mnttab_enable) { 877 struct mnttab srch = { 0 }; 878 879 if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 880 libzfs_mnttab_fini(hdl); 881 882 if ((mnttab = fopen(MNTTAB, "re")) == NULL) 883 return (ENOENT); 884 885 srch.mnt_special = (char *)fsname; 886 srch.mnt_fstype = (char *)MNTTYPE_ZFS; 887 ret = getmntany(mnttab, entry, &srch) ? ENOENT : 0; 888 (void) fclose(mnttab); 889 return (ret); 890 } 891 892 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock); 893 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) { 894 int error; 895 896 if ((error = libzfs_mnttab_update(hdl)) != 0) { 897 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); 898 return (error); 899 } 900 } 901 902 find.mtn_mt.mnt_special = (char *)fsname; 903 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 904 if (mtn) { 905 *entry = mtn->mtn_mt; 906 ret = 0; 907 } 908 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); 909 return (ret); 910 } 911 912 void 913 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 914 const char *mountp, const char *mntopts) 915 { 916 mnttab_node_t *mtn; 917 918 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock); 919 if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) { 920 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 921 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 922 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 923 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 924 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 925 /* 926 * Another thread may have already added this entry 927 * via libzfs_mnttab_update. If so we should skip it. 928 */ 929 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, NULL) != NULL) { 930 free(mtn->mtn_mt.mnt_special); 931 free(mtn->mtn_mt.mnt_mountp); 932 free(mtn->mtn_mt.mnt_fstype); 933 free(mtn->mtn_mt.mnt_mntopts); 934 free(mtn); 935 } else { 936 avl_add(&hdl->libzfs_mnttab_cache, mtn); 937 } 938 } 939 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); 940 } 941 942 void 943 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 944 { 945 mnttab_node_t find; 946 mnttab_node_t *ret; 947 948 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock); 949 find.mtn_mt.mnt_special = (char *)fsname; 950 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) 951 != NULL) { 952 avl_remove(&hdl->libzfs_mnttab_cache, ret); 953 free(ret->mtn_mt.mnt_special); 954 free(ret->mtn_mt.mnt_mountp); 955 free(ret->mtn_mt.mnt_fstype); 956 free(ret->mtn_mt.mnt_mntopts); 957 free(ret); 958 } 959 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); 960 } 961 962 int 963 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 964 { 965 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 966 967 if (zpool_handle == NULL) 968 return (-1); 969 970 *spa_version = zpool_get_prop_int(zpool_handle, 971 ZPOOL_PROP_VERSION, NULL); 972 return (0); 973 } 974 975 /* 976 * The choice of reservation property depends on the SPA version. 977 */ 978 static int 979 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 980 { 981 int spa_version; 982 983 if (zfs_spa_version(zhp, &spa_version) < 0) 984 return (-1); 985 986 if (spa_version >= SPA_VERSION_REFRESERVATION) 987 *resv_prop = ZFS_PROP_REFRESERVATION; 988 else 989 *resv_prop = ZFS_PROP_RESERVATION; 990 991 return (0); 992 } 993 994 /* 995 * Given an nvlist of properties to set, validates that they are correct, and 996 * parses any numeric properties (index, boolean, etc) if they are specified as 997 * strings. 998 */ 999 nvlist_t * 1000 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 1001 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl, 1002 boolean_t key_params_ok, const char *errbuf) 1003 { 1004 nvpair_t *elem; 1005 uint64_t intval; 1006 char *strval; 1007 zfs_prop_t prop; 1008 nvlist_t *ret; 1009 int chosen_normal = -1; 1010 int chosen_utf = -1; 1011 1012 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 1013 (void) no_memory(hdl); 1014 return (NULL); 1015 } 1016 1017 /* 1018 * Make sure this property is valid and applies to this type. 1019 */ 1020 1021 elem = NULL; 1022 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1023 const char *propname = nvpair_name(elem); 1024 1025 prop = zfs_name_to_prop(propname); 1026 if (prop == ZPROP_USERPROP && zfs_prop_user(propname)) { 1027 /* 1028 * This is a user property: make sure it's a 1029 * string, and that it's less than ZAP_MAXNAMELEN. 1030 */ 1031 if (nvpair_type(elem) != DATA_TYPE_STRING) { 1032 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1033 "'%s' must be a string"), propname); 1034 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1035 goto error; 1036 } 1037 1038 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 1039 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1040 "property name '%s' is too long"), 1041 propname); 1042 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1043 goto error; 1044 } 1045 1046 (void) nvpair_value_string(elem, &strval); 1047 if (nvlist_add_string(ret, propname, strval) != 0) { 1048 (void) no_memory(hdl); 1049 goto error; 1050 } 1051 continue; 1052 } 1053 1054 /* 1055 * Currently, only user properties can be modified on 1056 * snapshots. 1057 */ 1058 if (type == ZFS_TYPE_SNAPSHOT) { 1059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1060 "this property can not be modified for snapshots")); 1061 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1062 goto error; 1063 } 1064 1065 if (prop == ZPROP_USERPROP && zfs_prop_userquota(propname)) { 1066 zfs_userquota_prop_t uqtype; 1067 char *newpropname = NULL; 1068 char domain[128]; 1069 uint64_t rid; 1070 uint64_t valary[3]; 1071 int rc; 1072 1073 if (userquota_propname_decode(propname, zoned, 1074 &uqtype, domain, sizeof (domain), &rid) != 0) { 1075 zfs_error_aux(hdl, 1076 dgettext(TEXT_DOMAIN, 1077 "'%s' has an invalid user/group name"), 1078 propname); 1079 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1080 goto error; 1081 } 1082 1083 if (uqtype != ZFS_PROP_USERQUOTA && 1084 uqtype != ZFS_PROP_GROUPQUOTA && 1085 uqtype != ZFS_PROP_USEROBJQUOTA && 1086 uqtype != ZFS_PROP_GROUPOBJQUOTA && 1087 uqtype != ZFS_PROP_PROJECTQUOTA && 1088 uqtype != ZFS_PROP_PROJECTOBJQUOTA) { 1089 zfs_error_aux(hdl, 1090 dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1091 propname); 1092 (void) zfs_error(hdl, EZFS_PROPREADONLY, 1093 errbuf); 1094 goto error; 1095 } 1096 1097 if (nvpair_type(elem) == DATA_TYPE_STRING) { 1098 (void) nvpair_value_string(elem, &strval); 1099 if (strcmp(strval, "none") == 0) { 1100 intval = 0; 1101 } else if (zfs_nicestrtonum(hdl, 1102 strval, &intval) != 0) { 1103 (void) zfs_error(hdl, 1104 EZFS_BADPROP, errbuf); 1105 goto error; 1106 } 1107 } else if (nvpair_type(elem) == 1108 DATA_TYPE_UINT64) { 1109 (void) nvpair_value_uint64(elem, &intval); 1110 if (intval == 0) { 1111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1112 "use 'none' to disable " 1113 "{user|group|project}quota")); 1114 goto error; 1115 } 1116 } else { 1117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1118 "'%s' must be a number"), propname); 1119 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1120 goto error; 1121 } 1122 1123 /* 1124 * Encode the prop name as 1125 * userquota@<hex-rid>-domain, to make it easy 1126 * for the kernel to decode. 1127 */ 1128 rc = asprintf(&newpropname, "%s%llx-%s", 1129 zfs_userquota_prop_prefixes[uqtype], 1130 (longlong_t)rid, domain); 1131 if (rc == -1 || newpropname == NULL) { 1132 (void) no_memory(hdl); 1133 goto error; 1134 } 1135 1136 valary[0] = uqtype; 1137 valary[1] = rid; 1138 valary[2] = intval; 1139 if (nvlist_add_uint64_array(ret, newpropname, 1140 valary, 3) != 0) { 1141 free(newpropname); 1142 (void) no_memory(hdl); 1143 goto error; 1144 } 1145 free(newpropname); 1146 continue; 1147 } else if (prop == ZPROP_USERPROP && 1148 zfs_prop_written(propname)) { 1149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1150 "'%s' is readonly"), 1151 propname); 1152 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1153 goto error; 1154 } 1155 1156 if (prop == ZPROP_INVAL) { 1157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1158 "invalid property '%s'"), propname); 1159 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1160 goto error; 1161 } 1162 1163 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) { 1164 zfs_error_aux(hdl, 1165 dgettext(TEXT_DOMAIN, "'%s' does not " 1166 "apply to datasets of this type"), propname); 1167 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1168 goto error; 1169 } 1170 1171 if (zfs_prop_readonly(prop) && 1172 !(zfs_prop_setonce(prop) && zhp == NULL) && 1173 !(zfs_prop_encryption_key_param(prop) && key_params_ok)) { 1174 zfs_error_aux(hdl, 1175 dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1176 propname); 1177 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1178 goto error; 1179 } 1180 1181 if (zprop_parse_value(hdl, elem, prop, type, ret, 1182 &strval, &intval, errbuf) != 0) 1183 goto error; 1184 1185 /* 1186 * Perform some additional checks for specific properties. 1187 */ 1188 switch (prop) { 1189 case ZFS_PROP_VERSION: 1190 { 1191 int version; 1192 1193 if (zhp == NULL) 1194 break; 1195 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1196 if (intval < version) { 1197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1198 "Can not downgrade; already at version %u"), 1199 version); 1200 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1201 goto error; 1202 } 1203 break; 1204 } 1205 1206 case ZFS_PROP_VOLBLOCKSIZE: 1207 case ZFS_PROP_RECORDSIZE: 1208 { 1209 int maxbs = SPA_MAXBLOCKSIZE; 1210 char buf[64]; 1211 1212 if (zpool_hdl != NULL) { 1213 maxbs = zpool_get_prop_int(zpool_hdl, 1214 ZPOOL_PROP_MAXBLOCKSIZE, NULL); 1215 } 1216 /* 1217 * The value must be a power of two between 1218 * SPA_MINBLOCKSIZE and maxbs. 1219 */ 1220 if (intval < SPA_MINBLOCKSIZE || 1221 intval > maxbs || !ISP2(intval)) { 1222 zfs_nicebytes(maxbs, buf, sizeof (buf)); 1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1224 "'%s' must be power of 2 from 512B " 1225 "to %s"), propname, buf); 1226 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1227 goto error; 1228 } 1229 break; 1230 } 1231 1232 case ZFS_PROP_SPECIAL_SMALL_BLOCKS: 1233 { 1234 int maxbs = SPA_OLD_MAXBLOCKSIZE; 1235 char buf[64]; 1236 1237 if (zpool_hdl != NULL) { 1238 char state[64] = ""; 1239 1240 maxbs = zpool_get_prop_int(zpool_hdl, 1241 ZPOOL_PROP_MAXBLOCKSIZE, NULL); 1242 1243 /* 1244 * Issue a warning but do not fail so that 1245 * tests for settable properties succeed. 1246 */ 1247 if (zpool_prop_get_feature(zpool_hdl, 1248 "feature@allocation_classes", state, 1249 sizeof (state)) != 0 || 1250 strcmp(state, ZFS_FEATURE_ACTIVE) != 0) { 1251 (void) fprintf(stderr, gettext( 1252 "%s: property requires a special " 1253 "device in the pool\n"), propname); 1254 } 1255 } 1256 if (intval != 0 && 1257 (intval < SPA_MINBLOCKSIZE || 1258 intval > maxbs || !ISP2(intval))) { 1259 zfs_nicebytes(maxbs, buf, sizeof (buf)); 1260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1261 "invalid '%s=%llu' property: must be zero " 1262 "or a power of 2 from 512B to %s"), 1263 propname, (unsigned long long)intval, buf); 1264 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1265 goto error; 1266 } 1267 break; 1268 } 1269 1270 case ZFS_PROP_MLSLABEL: 1271 { 1272 #ifdef HAVE_MLSLABEL 1273 /* 1274 * Verify the mlslabel string and convert to 1275 * internal hex label string. 1276 */ 1277 1278 m_label_t *new_sl; 1279 char *hex = NULL; /* internal label string */ 1280 1281 /* Default value is already OK. */ 1282 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 1283 break; 1284 1285 /* Verify the label can be converted to binary form */ 1286 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 1287 (str_to_label(strval, &new_sl, MAC_LABEL, 1288 L_NO_CORRECTION, NULL) == -1)) { 1289 goto badlabel; 1290 } 1291 1292 /* Now translate to hex internal label string */ 1293 if (label_to_str(new_sl, &hex, M_INTERNAL, 1294 DEF_NAMES) != 0) { 1295 if (hex) 1296 free(hex); 1297 goto badlabel; 1298 } 1299 m_label_free(new_sl); 1300 1301 /* If string is already in internal form, we're done. */ 1302 if (strcmp(strval, hex) == 0) { 1303 free(hex); 1304 break; 1305 } 1306 1307 /* Replace the label string with the internal form. */ 1308 (void) nvlist_remove(ret, zfs_prop_to_name(prop), 1309 DATA_TYPE_STRING); 1310 fnvlist_add_string(ret, zfs_prop_to_name(prop), hex); 1311 free(hex); 1312 1313 break; 1314 1315 badlabel: 1316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1317 "invalid mlslabel '%s'"), strval); 1318 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1319 m_label_free(new_sl); /* OK if null */ 1320 goto error; 1321 #else 1322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1323 "mlslabels are unsupported")); 1324 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1325 goto error; 1326 #endif /* HAVE_MLSLABEL */ 1327 } 1328 1329 case ZFS_PROP_MOUNTPOINT: 1330 { 1331 namecheck_err_t why; 1332 1333 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1334 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1335 break; 1336 1337 if (mountpoint_namecheck(strval, &why)) { 1338 switch (why) { 1339 case NAME_ERR_LEADING_SLASH: 1340 zfs_error_aux(hdl, 1341 dgettext(TEXT_DOMAIN, 1342 "'%s' must be an absolute path, " 1343 "'none', or 'legacy'"), propname); 1344 break; 1345 case NAME_ERR_TOOLONG: 1346 zfs_error_aux(hdl, 1347 dgettext(TEXT_DOMAIN, 1348 "component of '%s' is too long"), 1349 propname); 1350 break; 1351 1352 default: 1353 zfs_error_aux(hdl, 1354 dgettext(TEXT_DOMAIN, 1355 "(%d) not defined"), 1356 why); 1357 break; 1358 } 1359 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1360 goto error; 1361 } 1362 zfs_fallthrough; 1363 } 1364 1365 case ZFS_PROP_SHARESMB: 1366 case ZFS_PROP_SHARENFS: 1367 /* 1368 * For the mountpoint and sharenfs or sharesmb 1369 * properties, check if it can be set in a 1370 * global/non-global zone based on 1371 * the zoned property value: 1372 * 1373 * global zone non-global zone 1374 * -------------------------------------------------- 1375 * zoned=on mountpoint (no) mountpoint (yes) 1376 * sharenfs (no) sharenfs (no) 1377 * sharesmb (no) sharesmb (no) 1378 * 1379 * zoned=off mountpoint (yes) N/A 1380 * sharenfs (yes) 1381 * sharesmb (yes) 1382 */ 1383 if (zoned) { 1384 if (getzoneid() == GLOBAL_ZONEID) { 1385 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1386 "'%s' cannot be set on " 1387 "dataset in a non-global zone"), 1388 propname); 1389 (void) zfs_error(hdl, EZFS_ZONED, 1390 errbuf); 1391 goto error; 1392 } else if (prop == ZFS_PROP_SHARENFS || 1393 prop == ZFS_PROP_SHARESMB) { 1394 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1395 "'%s' cannot be set in " 1396 "a non-global zone"), propname); 1397 (void) zfs_error(hdl, EZFS_ZONED, 1398 errbuf); 1399 goto error; 1400 } 1401 } else if (getzoneid() != GLOBAL_ZONEID) { 1402 /* 1403 * If zoned property is 'off', this must be in 1404 * a global zone. If not, something is wrong. 1405 */ 1406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1407 "'%s' cannot be set while dataset " 1408 "'zoned' property is set"), propname); 1409 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1410 goto error; 1411 } 1412 1413 /* 1414 * At this point, it is legitimate to set the 1415 * property. Now we want to make sure that the 1416 * property value is valid if it is sharenfs. 1417 */ 1418 if ((prop == ZFS_PROP_SHARENFS || 1419 prop == ZFS_PROP_SHARESMB) && 1420 strcmp(strval, "on") != 0 && 1421 strcmp(strval, "off") != 0) { 1422 enum sa_protocol proto; 1423 1424 if (prop == ZFS_PROP_SHARESMB) 1425 proto = SA_PROTOCOL_SMB; 1426 else 1427 proto = SA_PROTOCOL_NFS; 1428 1429 if (sa_validate_shareopts(strval, proto) != 1430 SA_OK) { 1431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1432 "'%s' cannot be set to invalid " 1433 "options"), propname); 1434 (void) zfs_error(hdl, EZFS_BADPROP, 1435 errbuf); 1436 goto error; 1437 } 1438 } 1439 1440 break; 1441 1442 case ZFS_PROP_KEYLOCATION: 1443 if (!zfs_prop_valid_keylocation(strval, B_FALSE)) { 1444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1445 "invalid keylocation")); 1446 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1447 goto error; 1448 } 1449 1450 if (zhp != NULL) { 1451 uint64_t crypt = 1452 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION); 1453 1454 if (crypt == ZIO_CRYPT_OFF && 1455 strcmp(strval, "none") != 0) { 1456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1457 "keylocation must be 'none' " 1458 "for unencrypted datasets")); 1459 (void) zfs_error(hdl, EZFS_BADPROP, 1460 errbuf); 1461 goto error; 1462 } else if (crypt != ZIO_CRYPT_OFF && 1463 strcmp(strval, "none") == 0) { 1464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1465 "keylocation must not be 'none' " 1466 "for encrypted datasets")); 1467 (void) zfs_error(hdl, EZFS_BADPROP, 1468 errbuf); 1469 goto error; 1470 } 1471 } 1472 break; 1473 1474 case ZFS_PROP_PBKDF2_ITERS: 1475 if (intval < MIN_PBKDF2_ITERATIONS) { 1476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1477 "minimum pbkdf2 iterations is %u"), 1478 MIN_PBKDF2_ITERATIONS); 1479 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1480 goto error; 1481 } 1482 break; 1483 1484 case ZFS_PROP_UTF8ONLY: 1485 chosen_utf = (int)intval; 1486 break; 1487 1488 case ZFS_PROP_NORMALIZE: 1489 chosen_normal = (int)intval; 1490 break; 1491 1492 default: 1493 break; 1494 } 1495 1496 /* 1497 * For changes to existing volumes, we have some additional 1498 * checks to enforce. 1499 */ 1500 if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1501 uint64_t blocksize = zfs_prop_get_int(zhp, 1502 ZFS_PROP_VOLBLOCKSIZE); 1503 char buf[64]; 1504 1505 switch (prop) { 1506 case ZFS_PROP_VOLSIZE: 1507 if (intval % blocksize != 0) { 1508 zfs_nicebytes(blocksize, buf, 1509 sizeof (buf)); 1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1511 "'%s' must be a multiple of " 1512 "volume block size (%s)"), 1513 propname, buf); 1514 (void) zfs_error(hdl, EZFS_BADPROP, 1515 errbuf); 1516 goto error; 1517 } 1518 1519 if (intval == 0) { 1520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1521 "'%s' cannot be zero"), 1522 propname); 1523 (void) zfs_error(hdl, EZFS_BADPROP, 1524 errbuf); 1525 goto error; 1526 } 1527 break; 1528 1529 default: 1530 break; 1531 } 1532 } 1533 1534 /* check encryption properties */ 1535 if (zhp != NULL) { 1536 int64_t crypt = zfs_prop_get_int(zhp, 1537 ZFS_PROP_ENCRYPTION); 1538 1539 switch (prop) { 1540 case ZFS_PROP_COPIES: 1541 if (crypt != ZIO_CRYPT_OFF && intval > 2) { 1542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1543 "encrypted datasets cannot have " 1544 "3 copies")); 1545 (void) zfs_error(hdl, EZFS_BADPROP, 1546 errbuf); 1547 goto error; 1548 } 1549 break; 1550 default: 1551 break; 1552 } 1553 } 1554 } 1555 1556 /* 1557 * If normalization was chosen, but no UTF8 choice was made, 1558 * enforce rejection of non-UTF8 names. 1559 * 1560 * If normalization was chosen, but rejecting non-UTF8 names 1561 * was explicitly not chosen, it is an error. 1562 * 1563 * If utf8only was turned off, but the parent has normalization, 1564 * turn off normalization. 1565 */ 1566 if (chosen_normal > 0 && chosen_utf < 0) { 1567 if (nvlist_add_uint64(ret, 1568 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1569 (void) no_memory(hdl); 1570 goto error; 1571 } 1572 } else if (chosen_normal > 0 && chosen_utf == 0) { 1573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1574 "'%s' must be set 'on' if normalization chosen"), 1575 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1576 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1577 goto error; 1578 } else if (chosen_normal < 0 && chosen_utf == 0) { 1579 if (nvlist_add_uint64(ret, 1580 zfs_prop_to_name(ZFS_PROP_NORMALIZE), 0) != 0) { 1581 (void) no_memory(hdl); 1582 goto error; 1583 } 1584 } 1585 return (ret); 1586 1587 error: 1588 nvlist_free(ret); 1589 return (NULL); 1590 } 1591 1592 static int 1593 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 1594 { 1595 uint64_t old_volsize; 1596 uint64_t new_volsize; 1597 uint64_t old_reservation; 1598 uint64_t new_reservation; 1599 zfs_prop_t resv_prop; 1600 nvlist_t *props; 1601 zpool_handle_t *zph = zpool_handle(zhp); 1602 1603 /* 1604 * If this is an existing volume, and someone is setting the volsize, 1605 * make sure that it matches the reservation, or add it if necessary. 1606 */ 1607 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 1608 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 1609 return (-1); 1610 old_reservation = zfs_prop_get_int(zhp, resv_prop); 1611 1612 props = fnvlist_alloc(); 1613 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1614 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1615 1616 if ((zvol_volsize_to_reservation(zph, old_volsize, props) != 1617 old_reservation) || nvlist_exists(nvl, 1618 zfs_prop_to_name(resv_prop))) { 1619 fnvlist_free(props); 1620 return (0); 1621 } 1622 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1623 &new_volsize) != 0) { 1624 fnvlist_free(props); 1625 return (-1); 1626 } 1627 new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props); 1628 fnvlist_free(props); 1629 1630 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 1631 new_reservation) != 0) { 1632 (void) no_memory(zhp->zfs_hdl); 1633 return (-1); 1634 } 1635 return (1); 1636 } 1637 1638 /* 1639 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after 1640 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value. 1641 * Return codes must match zfs_add_synthetic_resv(). 1642 */ 1643 static int 1644 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl) 1645 { 1646 uint64_t volsize; 1647 uint64_t resvsize; 1648 zfs_prop_t prop; 1649 nvlist_t *props; 1650 1651 if (!ZFS_IS_VOLUME(zhp)) { 1652 return (0); 1653 } 1654 1655 if (zfs_which_resv_prop(zhp, &prop) != 0) { 1656 return (-1); 1657 } 1658 1659 if (prop != ZFS_PROP_REFRESERVATION) { 1660 return (0); 1661 } 1662 1663 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) { 1664 /* No value being set, so it can't be "auto" */ 1665 return (0); 1666 } 1667 if (resvsize != UINT64_MAX) { 1668 /* Being set to a value other than "auto" */ 1669 return (0); 1670 } 1671 1672 props = fnvlist_alloc(); 1673 1674 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1675 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1676 1677 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1678 &volsize) != 0) { 1679 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 1680 } 1681 1682 resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize, 1683 props); 1684 fnvlist_free(props); 1685 1686 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop)); 1687 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) { 1688 (void) no_memory(zhp->zfs_hdl); 1689 return (-1); 1690 } 1691 return (1); 1692 } 1693 1694 static boolean_t 1695 zfs_is_namespace_prop(zfs_prop_t prop) 1696 { 1697 switch (prop) { 1698 1699 case ZFS_PROP_ATIME: 1700 case ZFS_PROP_RELATIME: 1701 case ZFS_PROP_DEVICES: 1702 case ZFS_PROP_EXEC: 1703 case ZFS_PROP_SETUID: 1704 case ZFS_PROP_READONLY: 1705 case ZFS_PROP_XATTR: 1706 case ZFS_PROP_NBMAND: 1707 return (B_TRUE); 1708 1709 default: 1710 return (B_FALSE); 1711 } 1712 } 1713 1714 /* 1715 * Given a property name and value, set the property for the given dataset. 1716 */ 1717 int 1718 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1719 { 1720 int ret = -1; 1721 char errbuf[ERRBUFLEN]; 1722 libzfs_handle_t *hdl = zhp->zfs_hdl; 1723 nvlist_t *nvl = NULL; 1724 1725 (void) snprintf(errbuf, sizeof (errbuf), 1726 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1727 zhp->zfs_name); 1728 1729 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1730 nvlist_add_string(nvl, propname, propval) != 0) { 1731 (void) no_memory(hdl); 1732 goto error; 1733 } 1734 1735 ret = zfs_prop_set_list(zhp, nvl); 1736 1737 error: 1738 nvlist_free(nvl); 1739 return (ret); 1740 } 1741 1742 1743 1744 /* 1745 * Given an nvlist of property names and values, set the properties for the 1746 * given dataset. 1747 */ 1748 int 1749 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props) 1750 { 1751 zfs_cmd_t zc = {"\0"}; 1752 int ret = -1; 1753 prop_changelist_t **cls = NULL; 1754 int cl_idx; 1755 char errbuf[ERRBUFLEN]; 1756 libzfs_handle_t *hdl = zhp->zfs_hdl; 1757 nvlist_t *nvl; 1758 int nvl_len = 0; 1759 int added_resv = 0; 1760 zfs_prop_t prop = 0; 1761 nvpair_t *elem; 1762 1763 (void) snprintf(errbuf, sizeof (errbuf), 1764 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1765 zhp->zfs_name); 1766 1767 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, 1768 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, 1769 B_FALSE, errbuf)) == NULL) 1770 goto error; 1771 1772 /* 1773 * We have to check for any extra properties which need to be added 1774 * before computing the length of the nvlist. 1775 */ 1776 for (elem = nvlist_next_nvpair(nvl, NULL); 1777 elem != NULL; 1778 elem = nvlist_next_nvpair(nvl, elem)) { 1779 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && 1780 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { 1781 goto error; 1782 } 1783 } 1784 1785 if (added_resv != 1 && 1786 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) { 1787 goto error; 1788 } 1789 1790 /* 1791 * Check how many properties we're setting and allocate an array to 1792 * store changelist pointers for postfix(). 1793 */ 1794 for (elem = nvlist_next_nvpair(nvl, NULL); 1795 elem != NULL; 1796 elem = nvlist_next_nvpair(nvl, elem)) 1797 nvl_len++; 1798 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) 1799 goto error; 1800 1801 cl_idx = 0; 1802 for (elem = nvlist_next_nvpair(nvl, NULL); 1803 elem != NULL; 1804 elem = nvlist_next_nvpair(nvl, elem)) { 1805 1806 prop = zfs_name_to_prop(nvpair_name(elem)); 1807 1808 assert(cl_idx < nvl_len); 1809 /* 1810 * We don't want to unmount & remount the dataset when changing 1811 * its canmount property to 'on' or 'noauto'. We only use 1812 * the changelist logic to unmount when setting canmount=off. 1813 */ 1814 if (prop != ZFS_PROP_CANMOUNT || 1815 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF && 1816 zfs_is_mounted(zhp, NULL))) { 1817 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); 1818 if (cls[cl_idx] == NULL) 1819 goto error; 1820 } 1821 1822 if (prop == ZFS_PROP_MOUNTPOINT && 1823 changelist_haszonedchild(cls[cl_idx])) { 1824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1825 "child dataset with inherited mountpoint is used " 1826 "in a non-global zone")); 1827 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1828 goto error; 1829 } 1830 1831 if (cls[cl_idx] != NULL && 1832 (ret = changelist_prefix(cls[cl_idx])) != 0) 1833 goto error; 1834 1835 cl_idx++; 1836 } 1837 assert(cl_idx == nvl_len); 1838 1839 /* 1840 * Execute the corresponding ioctl() to set this list of properties. 1841 */ 1842 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1843 1844 zcmd_write_src_nvlist(hdl, &zc, nvl); 1845 zcmd_alloc_dst_nvlist(hdl, &zc, 0); 1846 1847 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1848 1849 if (ret != 0) { 1850 if (zc.zc_nvlist_dst_filled == B_FALSE) { 1851 (void) zfs_standard_error(hdl, errno, errbuf); 1852 goto error; 1853 } 1854 1855 /* Get the list of unset properties back and report them. */ 1856 nvlist_t *errorprops = NULL; 1857 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) 1858 goto error; 1859 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL); 1860 elem != NULL; 1861 elem = nvlist_next_nvpair(errorprops, elem)) { 1862 prop = zfs_name_to_prop(nvpair_name(elem)); 1863 zfs_setprop_error(hdl, prop, errno, errbuf); 1864 } 1865 nvlist_free(errorprops); 1866 1867 if (added_resv && errno == ENOSPC) { 1868 /* clean up the volsize property we tried to set */ 1869 uint64_t old_volsize = zfs_prop_get_int(zhp, 1870 ZFS_PROP_VOLSIZE); 1871 nvlist_free(nvl); 1872 nvl = NULL; 1873 zcmd_free_nvlists(&zc); 1874 1875 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 1876 goto error; 1877 if (nvlist_add_uint64(nvl, 1878 zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1879 old_volsize) != 0) 1880 goto error; 1881 zcmd_write_src_nvlist(hdl, &zc, nvl); 1882 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1883 } 1884 } else { 1885 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 1886 if (cls[cl_idx] != NULL) { 1887 int clp_err = changelist_postfix(cls[cl_idx]); 1888 if (clp_err != 0) 1889 ret = clp_err; 1890 } 1891 } 1892 1893 if (ret == 0) { 1894 /* 1895 * Refresh the statistics so the new property 1896 * value is reflected. 1897 */ 1898 (void) get_stats(zhp); 1899 1900 /* 1901 * Remount the filesystem to propagate the change 1902 * if one of the options handled by the generic 1903 * Linux namespace layer has been modified. 1904 */ 1905 if (zfs_is_namespace_prop(prop) && 1906 zfs_is_mounted(zhp, NULL)) 1907 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0); 1908 } 1909 } 1910 1911 error: 1912 nvlist_free(nvl); 1913 zcmd_free_nvlists(&zc); 1914 if (cls != NULL) { 1915 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 1916 if (cls[cl_idx] != NULL) 1917 changelist_free(cls[cl_idx]); 1918 } 1919 free(cls); 1920 } 1921 return (ret); 1922 } 1923 1924 /* 1925 * Given a property, inherit the value from the parent dataset, or if received 1926 * is TRUE, revert to the received value, if any. 1927 */ 1928 int 1929 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1930 { 1931 zfs_cmd_t zc = {"\0"}; 1932 int ret; 1933 prop_changelist_t *cl; 1934 libzfs_handle_t *hdl = zhp->zfs_hdl; 1935 char errbuf[ERRBUFLEN]; 1936 zfs_prop_t prop; 1937 1938 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1939 "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1940 1941 zc.zc_cookie = received; 1942 if ((prop = zfs_name_to_prop(propname)) == ZPROP_USERPROP) { 1943 /* 1944 * For user properties, the amount of work we have to do is very 1945 * small, so just do it here. 1946 */ 1947 if (!zfs_prop_user(propname)) { 1948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1949 "invalid property")); 1950 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1951 } 1952 1953 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1954 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1955 1956 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1957 return (zfs_standard_error(hdl, errno, errbuf)); 1958 1959 (void) get_stats(zhp); 1960 return (0); 1961 } 1962 1963 /* 1964 * Verify that this property is inheritable. 1965 */ 1966 if (zfs_prop_readonly(prop)) 1967 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1968 1969 if (!zfs_prop_inheritable(prop) && !received) 1970 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1971 1972 /* 1973 * Check to see if the value applies to this type 1974 */ 1975 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) 1976 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1977 1978 /* 1979 * Normalize the name, to get rid of shorthand abbreviations. 1980 */ 1981 propname = zfs_prop_to_name(prop); 1982 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1983 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1984 1985 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1986 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 1987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1988 "dataset is used in a non-global zone")); 1989 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1990 } 1991 1992 /* 1993 * Determine datasets which will be affected by this change, if any. 1994 */ 1995 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1996 return (-1); 1997 1998 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2000 "child dataset with inherited mountpoint is used " 2001 "in a non-global zone")); 2002 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2003 goto error; 2004 } 2005 2006 if ((ret = changelist_prefix(cl)) != 0) 2007 goto error; 2008 2009 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 2010 return (zfs_standard_error(hdl, errno, errbuf)); 2011 } else { 2012 2013 if ((ret = changelist_postfix(cl)) != 0) 2014 goto error; 2015 2016 /* 2017 * Refresh the statistics so the new property is reflected. 2018 */ 2019 (void) get_stats(zhp); 2020 2021 /* 2022 * Remount the filesystem to propagate the change 2023 * if one of the options handled by the generic 2024 * Linux namespace layer has been modified. 2025 */ 2026 if (zfs_is_namespace_prop(prop) && 2027 zfs_is_mounted(zhp, NULL)) 2028 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0); 2029 } 2030 2031 error: 2032 changelist_free(cl); 2033 return (ret); 2034 } 2035 2036 /* 2037 * True DSL properties are stored in an nvlist. The following two functions 2038 * extract them appropriately. 2039 */ 2040 uint64_t 2041 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 2042 { 2043 nvlist_t *nv; 2044 uint64_t value; 2045 2046 *source = NULL; 2047 if (nvlist_lookup_nvlist(zhp->zfs_props, 2048 zfs_prop_to_name(prop), &nv) == 0) { 2049 value = fnvlist_lookup_uint64(nv, ZPROP_VALUE); 2050 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 2051 } else { 2052 verify(!zhp->zfs_props_table || 2053 zhp->zfs_props_table[prop] == B_TRUE); 2054 value = zfs_prop_default_numeric(prop); 2055 *source = (char *)""; 2056 } 2057 2058 return (value); 2059 } 2060 2061 static const char * 2062 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 2063 { 2064 nvlist_t *nv; 2065 const char *value; 2066 2067 *source = NULL; 2068 if (nvlist_lookup_nvlist(zhp->zfs_props, 2069 zfs_prop_to_name(prop), &nv) == 0) { 2070 value = fnvlist_lookup_string(nv, ZPROP_VALUE); 2071 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 2072 } else { 2073 verify(!zhp->zfs_props_table || 2074 zhp->zfs_props_table[prop] == B_TRUE); 2075 value = zfs_prop_default_string(prop); 2076 *source = (char *)""; 2077 } 2078 2079 return (value); 2080 } 2081 2082 static boolean_t 2083 zfs_is_recvd_props_mode(zfs_handle_t *zhp) 2084 { 2085 return (zhp->zfs_props == zhp->zfs_recvd_props); 2086 } 2087 2088 static void 2089 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 2090 { 2091 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 2092 zhp->zfs_props = zhp->zfs_recvd_props; 2093 } 2094 2095 static void 2096 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 2097 { 2098 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 2099 *cookie = 0; 2100 } 2101 2102 /* 2103 * Internal function for getting a numeric property. Both zfs_prop_get() and 2104 * zfs_prop_get_int() are built using this interface. 2105 * 2106 * Certain properties can be overridden using 'mount -o'. In this case, scan 2107 * the contents of the /proc/self/mounts entry, searching for the 2108 * appropriate options. If they differ from the on-disk values, report the 2109 * current values and mark the source "temporary". 2110 */ 2111 static int 2112 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 2113 char **source, uint64_t *val) 2114 { 2115 zfs_cmd_t zc = {"\0"}; 2116 nvlist_t *zplprops = NULL; 2117 struct mnttab mnt; 2118 const char *mntopt_on = NULL; 2119 const char *mntopt_off = NULL; 2120 boolean_t received = zfs_is_recvd_props_mode(zhp); 2121 2122 *source = NULL; 2123 2124 /* 2125 * If the property is being fetched for a snapshot, check whether 2126 * the property is valid for the snapshot's head dataset type. 2127 */ 2128 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT && 2129 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) { 2130 *val = zfs_prop_default_numeric(prop); 2131 return (-1); 2132 } 2133 2134 switch (prop) { 2135 case ZFS_PROP_ATIME: 2136 mntopt_on = MNTOPT_ATIME; 2137 mntopt_off = MNTOPT_NOATIME; 2138 break; 2139 2140 case ZFS_PROP_RELATIME: 2141 mntopt_on = MNTOPT_RELATIME; 2142 mntopt_off = MNTOPT_NORELATIME; 2143 break; 2144 2145 case ZFS_PROP_DEVICES: 2146 mntopt_on = MNTOPT_DEVICES; 2147 mntopt_off = MNTOPT_NODEVICES; 2148 break; 2149 2150 case ZFS_PROP_EXEC: 2151 mntopt_on = MNTOPT_EXEC; 2152 mntopt_off = MNTOPT_NOEXEC; 2153 break; 2154 2155 case ZFS_PROP_READONLY: 2156 mntopt_on = MNTOPT_RO; 2157 mntopt_off = MNTOPT_RW; 2158 break; 2159 2160 case ZFS_PROP_SETUID: 2161 mntopt_on = MNTOPT_SETUID; 2162 mntopt_off = MNTOPT_NOSETUID; 2163 break; 2164 2165 case ZFS_PROP_XATTR: 2166 mntopt_on = MNTOPT_XATTR; 2167 mntopt_off = MNTOPT_NOXATTR; 2168 break; 2169 2170 case ZFS_PROP_NBMAND: 2171 mntopt_on = MNTOPT_NBMAND; 2172 mntopt_off = MNTOPT_NONBMAND; 2173 break; 2174 2175 default: 2176 break; 2177 } 2178 2179 /* 2180 * Because looking up the mount options is potentially expensive 2181 * (iterating over all of /proc/self/mounts), we defer its 2182 * calculation until we're looking up a property which requires 2183 * its presence. 2184 */ 2185 if (!zhp->zfs_mntcheck && 2186 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2187 libzfs_handle_t *hdl = zhp->zfs_hdl; 2188 struct mnttab entry; 2189 2190 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) 2191 zhp->zfs_mntopts = zfs_strdup(hdl, 2192 entry.mnt_mntopts); 2193 2194 zhp->zfs_mntcheck = B_TRUE; 2195 } 2196 2197 if (zhp->zfs_mntopts == NULL) 2198 mnt.mnt_mntopts = (char *)""; 2199 else 2200 mnt.mnt_mntopts = zhp->zfs_mntopts; 2201 2202 switch (prop) { 2203 case ZFS_PROP_ATIME: 2204 case ZFS_PROP_RELATIME: 2205 case ZFS_PROP_DEVICES: 2206 case ZFS_PROP_EXEC: 2207 case ZFS_PROP_READONLY: 2208 case ZFS_PROP_SETUID: 2209 #ifndef __FreeBSD__ 2210 case ZFS_PROP_XATTR: 2211 #endif 2212 case ZFS_PROP_NBMAND: 2213 *val = getprop_uint64(zhp, prop, source); 2214 2215 if (received) 2216 break; 2217 2218 if (hasmntopt(&mnt, mntopt_on) && !*val) { 2219 *val = B_TRUE; 2220 if (src) 2221 *src = ZPROP_SRC_TEMPORARY; 2222 } else if (hasmntopt(&mnt, mntopt_off) && *val) { 2223 *val = B_FALSE; 2224 if (src) 2225 *src = ZPROP_SRC_TEMPORARY; 2226 } 2227 break; 2228 2229 case ZFS_PROP_CANMOUNT: 2230 case ZFS_PROP_VOLSIZE: 2231 case ZFS_PROP_QUOTA: 2232 case ZFS_PROP_REFQUOTA: 2233 case ZFS_PROP_RESERVATION: 2234 case ZFS_PROP_REFRESERVATION: 2235 case ZFS_PROP_FILESYSTEM_LIMIT: 2236 case ZFS_PROP_SNAPSHOT_LIMIT: 2237 case ZFS_PROP_FILESYSTEM_COUNT: 2238 case ZFS_PROP_SNAPSHOT_COUNT: 2239 *val = getprop_uint64(zhp, prop, source); 2240 2241 if (*source == NULL) { 2242 /* not default, must be local */ 2243 *source = zhp->zfs_name; 2244 } 2245 break; 2246 2247 case ZFS_PROP_MOUNTED: 2248 *val = (zhp->zfs_mntopts != NULL); 2249 break; 2250 2251 case ZFS_PROP_NUMCLONES: 2252 *val = zhp->zfs_dmustats.dds_num_clones; 2253 break; 2254 2255 case ZFS_PROP_VERSION: 2256 case ZFS_PROP_NORMALIZE: 2257 case ZFS_PROP_UTF8ONLY: 2258 case ZFS_PROP_CASE: 2259 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0); 2260 2261 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2262 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2263 zcmd_free_nvlists(&zc); 2264 if (prop == ZFS_PROP_VERSION && 2265 zhp->zfs_type == ZFS_TYPE_VOLUME) 2266 *val = zfs_prop_default_numeric(prop); 2267 return (-1); 2268 } 2269 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2270 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2271 val) != 0) { 2272 zcmd_free_nvlists(&zc); 2273 return (-1); 2274 } 2275 nvlist_free(zplprops); 2276 zcmd_free_nvlists(&zc); 2277 break; 2278 2279 case ZFS_PROP_INCONSISTENT: 2280 *val = zhp->zfs_dmustats.dds_inconsistent; 2281 break; 2282 2283 case ZFS_PROP_REDACTED: 2284 *val = zhp->zfs_dmustats.dds_redacted; 2285 break; 2286 2287 case ZFS_PROP_CREATETXG: 2288 /* 2289 * We can directly read createtxg property from zfs 2290 * handle for Filesystem, Snapshot and ZVOL types. 2291 */ 2292 if ((zhp->zfs_type == ZFS_TYPE_FILESYSTEM) || 2293 (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) || 2294 (zhp->zfs_type == ZFS_TYPE_VOLUME)) { 2295 *val = zhp->zfs_dmustats.dds_creation_txg; 2296 break; 2297 } 2298 zfs_fallthrough; 2299 2300 default: 2301 switch (zfs_prop_get_type(prop)) { 2302 case PROP_TYPE_NUMBER: 2303 case PROP_TYPE_INDEX: 2304 *val = getprop_uint64(zhp, prop, source); 2305 /* 2306 * If we tried to use a default value for a 2307 * readonly property, it means that it was not 2308 * present. Note this only applies to "truly" 2309 * readonly properties, not set-once properties 2310 * like volblocksize. 2311 */ 2312 if (zfs_prop_readonly(prop) && 2313 !zfs_prop_setonce(prop) && 2314 *source != NULL && (*source)[0] == '\0') { 2315 *source = NULL; 2316 return (-1); 2317 } 2318 break; 2319 2320 case PROP_TYPE_STRING: 2321 default: 2322 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2323 "cannot get non-numeric property")); 2324 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2325 dgettext(TEXT_DOMAIN, "internal error"))); 2326 } 2327 } 2328 2329 return (0); 2330 } 2331 2332 /* 2333 * Calculate the source type, given the raw source string. 2334 */ 2335 static void 2336 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2337 char *statbuf, size_t statlen) 2338 { 2339 if (statbuf == NULL || 2340 srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) { 2341 return; 2342 } 2343 2344 if (source == NULL) { 2345 *srctype = ZPROP_SRC_NONE; 2346 } else if (source[0] == '\0') { 2347 *srctype = ZPROP_SRC_DEFAULT; 2348 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 2349 *srctype = ZPROP_SRC_RECEIVED; 2350 } else { 2351 if (strcmp(source, zhp->zfs_name) == 0) { 2352 *srctype = ZPROP_SRC_LOCAL; 2353 } else { 2354 (void) strlcpy(statbuf, source, statlen); 2355 *srctype = ZPROP_SRC_INHERITED; 2356 } 2357 } 2358 2359 } 2360 2361 int 2362 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 2363 size_t proplen, boolean_t literal) 2364 { 2365 zfs_prop_t prop; 2366 int err = 0; 2367 2368 if (zhp->zfs_recvd_props == NULL) 2369 if (get_recvd_props_ioctl(zhp) != 0) 2370 return (-1); 2371 2372 prop = zfs_name_to_prop(propname); 2373 2374 if (prop != ZPROP_USERPROP) { 2375 uint64_t cookie; 2376 if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 2377 return (-1); 2378 zfs_set_recvd_props_mode(zhp, &cookie); 2379 err = zfs_prop_get(zhp, prop, propbuf, proplen, 2380 NULL, NULL, 0, literal); 2381 zfs_unset_recvd_props_mode(zhp, &cookie); 2382 } else { 2383 nvlist_t *propval; 2384 char *recvdval; 2385 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 2386 propname, &propval) != 0) 2387 return (-1); 2388 recvdval = fnvlist_lookup_string(propval, ZPROP_VALUE); 2389 (void) strlcpy(propbuf, recvdval, proplen); 2390 } 2391 2392 return (err == 0 ? 0 : -1); 2393 } 2394 2395 static int 2396 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 2397 { 2398 nvlist_t *value; 2399 nvpair_t *pair; 2400 2401 value = zfs_get_clones_nvl(zhp); 2402 if (value == NULL || nvlist_empty(value)) 2403 return (-1); 2404 2405 propbuf[0] = '\0'; 2406 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 2407 pair = nvlist_next_nvpair(value, pair)) { 2408 if (propbuf[0] != '\0') 2409 (void) strlcat(propbuf, ",", proplen); 2410 (void) strlcat(propbuf, nvpair_name(pair), proplen); 2411 } 2412 2413 return (0); 2414 } 2415 2416 struct get_clones_arg { 2417 uint64_t numclones; 2418 nvlist_t *value; 2419 const char *origin; 2420 char buf[ZFS_MAX_DATASET_NAME_LEN]; 2421 }; 2422 2423 static int 2424 get_clones_cb(zfs_handle_t *zhp, void *arg) 2425 { 2426 struct get_clones_arg *gca = arg; 2427 2428 if (gca->numclones == 0) { 2429 zfs_close(zhp); 2430 return (0); 2431 } 2432 2433 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 2434 NULL, NULL, 0, B_TRUE) != 0) 2435 goto out; 2436 if (strcmp(gca->buf, gca->origin) == 0) { 2437 fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 2438 gca->numclones--; 2439 } 2440 2441 out: 2442 (void) zfs_iter_children(zhp, get_clones_cb, gca); 2443 zfs_close(zhp); 2444 return (0); 2445 } 2446 2447 nvlist_t * 2448 zfs_get_clones_nvl(zfs_handle_t *zhp) 2449 { 2450 nvlist_t *nv, *value; 2451 2452 if (nvlist_lookup_nvlist(zhp->zfs_props, 2453 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 2454 struct get_clones_arg gca; 2455 2456 /* 2457 * if this is a snapshot, then the kernel wasn't able 2458 * to get the clones. Do it by slowly iterating. 2459 */ 2460 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 2461 return (NULL); 2462 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 2463 return (NULL); 2464 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 2465 nvlist_free(nv); 2466 return (NULL); 2467 } 2468 2469 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 2470 gca.value = value; 2471 gca.origin = zhp->zfs_name; 2472 2473 if (gca.numclones != 0) { 2474 zfs_handle_t *root; 2475 char pool[ZFS_MAX_DATASET_NAME_LEN]; 2476 char *cp = pool; 2477 2478 /* get the pool name */ 2479 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 2480 (void) strsep(&cp, "/@"); 2481 root = zfs_open(zhp->zfs_hdl, pool, 2482 ZFS_TYPE_FILESYSTEM); 2483 if (root == NULL) { 2484 nvlist_free(nv); 2485 nvlist_free(value); 2486 return (NULL); 2487 } 2488 2489 (void) get_clones_cb(root, &gca); 2490 } 2491 2492 if (gca.numclones != 0 || 2493 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 2494 nvlist_add_nvlist(zhp->zfs_props, 2495 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 2496 nvlist_free(nv); 2497 nvlist_free(value); 2498 return (NULL); 2499 } 2500 nvlist_free(nv); 2501 nvlist_free(value); 2502 nv = fnvlist_lookup_nvlist(zhp->zfs_props, 2503 zfs_prop_to_name(ZFS_PROP_CLONES)); 2504 } 2505 2506 return (fnvlist_lookup_nvlist(nv, ZPROP_VALUE)); 2507 } 2508 2509 static int 2510 get_rsnaps_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 2511 { 2512 nvlist_t *value; 2513 uint64_t *snaps; 2514 uint_t nsnaps; 2515 2516 if (nvlist_lookup_nvlist(zhp->zfs_props, 2517 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &value) != 0) 2518 return (-1); 2519 if (nvlist_lookup_uint64_array(value, ZPROP_VALUE, &snaps, 2520 &nsnaps) != 0) 2521 return (-1); 2522 if (nsnaps == 0) { 2523 /* There's no redaction snapshots; pass a special value back */ 2524 (void) snprintf(propbuf, proplen, "none"); 2525 return (0); 2526 } 2527 propbuf[0] = '\0'; 2528 for (int i = 0; i < nsnaps; i++) { 2529 char buf[128]; 2530 if (propbuf[0] != '\0') 2531 (void) strlcat(propbuf, ",", proplen); 2532 (void) snprintf(buf, sizeof (buf), "%llu", 2533 (u_longlong_t)snaps[i]); 2534 (void) strlcat(propbuf, buf, proplen); 2535 } 2536 2537 return (0); 2538 } 2539 2540 /* 2541 * Accepts a property and value and checks that the value 2542 * matches the one found by the channel program. If they are 2543 * not equal, print both of them. 2544 */ 2545 static void 2546 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval, 2547 const char *strval) 2548 { 2549 if (!zhp->zfs_hdl->libzfs_prop_debug) 2550 return; 2551 int error; 2552 char *poolname = zhp->zpool_hdl->zpool_name; 2553 const char *prop_name = zfs_prop_to_name(prop); 2554 const char *program = 2555 "args = ...\n" 2556 "ds = args['dataset']\n" 2557 "prop = args['property']\n" 2558 "value, setpoint = zfs.get_prop(ds, prop)\n" 2559 "return {value=value, setpoint=setpoint}\n"; 2560 nvlist_t *outnvl; 2561 nvlist_t *retnvl; 2562 nvlist_t *argnvl = fnvlist_alloc(); 2563 2564 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name); 2565 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop)); 2566 2567 error = lzc_channel_program_nosync(poolname, program, 2568 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl); 2569 2570 if (error == 0) { 2571 retnvl = fnvlist_lookup_nvlist(outnvl, "return"); 2572 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) { 2573 int64_t ans; 2574 error = nvlist_lookup_int64(retnvl, "value", &ans); 2575 if (error != 0) { 2576 (void) fprintf(stderr, "%s: zcp check error: " 2577 "%u\n", prop_name, error); 2578 return; 2579 } 2580 if (ans != intval) { 2581 (void) fprintf(stderr, "%s: zfs found %llu, " 2582 "but zcp found %llu\n", prop_name, 2583 (u_longlong_t)intval, (u_longlong_t)ans); 2584 } 2585 } else { 2586 char *str_ans; 2587 error = nvlist_lookup_string(retnvl, "value", &str_ans); 2588 if (error != 0) { 2589 (void) fprintf(stderr, "%s: zcp check error: " 2590 "%u\n", prop_name, error); 2591 return; 2592 } 2593 if (strcmp(strval, str_ans) != 0) { 2594 (void) fprintf(stderr, 2595 "%s: zfs found '%s', but zcp found '%s'\n", 2596 prop_name, strval, str_ans); 2597 } 2598 } 2599 } else { 2600 (void) fprintf(stderr, "%s: zcp check failed, channel program " 2601 "error: %u\n", prop_name, error); 2602 } 2603 nvlist_free(argnvl); 2604 nvlist_free(outnvl); 2605 } 2606 2607 /* 2608 * Retrieve a property from the given object. If 'literal' is specified, then 2609 * numbers are left as exact values. Otherwise, numbers are converted to a 2610 * human-readable form. 2611 * 2612 * Returns 0 on success, or -1 on error. 2613 */ 2614 int 2615 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2616 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2617 { 2618 char *source = NULL; 2619 uint64_t val; 2620 const char *str; 2621 const char *strval; 2622 boolean_t received = zfs_is_recvd_props_mode(zhp); 2623 2624 /* 2625 * Check to see if this property applies to our object 2626 */ 2627 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) 2628 return (-1); 2629 2630 if (received && zfs_prop_readonly(prop)) 2631 return (-1); 2632 2633 if (src) 2634 *src = ZPROP_SRC_NONE; 2635 2636 switch (prop) { 2637 case ZFS_PROP_CREATION: 2638 /* 2639 * 'creation' is a time_t stored in the statistics. We convert 2640 * this into a string unless 'literal' is specified. 2641 */ 2642 { 2643 val = getprop_uint64(zhp, prop, &source); 2644 time_t time = (time_t)val; 2645 struct tm t; 2646 2647 if (literal || 2648 localtime_r(&time, &t) == NULL || 2649 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2650 &t) == 0) 2651 (void) snprintf(propbuf, proplen, "%llu", 2652 (u_longlong_t)val); 2653 } 2654 zcp_check(zhp, prop, val, NULL); 2655 break; 2656 2657 case ZFS_PROP_MOUNTPOINT: 2658 /* 2659 * Getting the precise mountpoint can be tricky. 2660 * 2661 * - for 'none' or 'legacy', return those values. 2662 * - for inherited mountpoints, we want to take everything 2663 * after our ancestor and append it to the inherited value. 2664 * 2665 * If the pool has an alternate root, we want to prepend that 2666 * root to any values we return. 2667 */ 2668 2669 str = getprop_string(zhp, prop, &source); 2670 2671 if (str[0] == '/') { 2672 char buf[MAXPATHLEN]; 2673 char *root = buf; 2674 const char *relpath; 2675 2676 /* 2677 * If we inherit the mountpoint, even from a dataset 2678 * with a received value, the source will be the path of 2679 * the dataset we inherit from. If source is 2680 * ZPROP_SOURCE_VAL_RECVD, the received value is not 2681 * inherited. 2682 */ 2683 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2684 relpath = ""; 2685 } else { 2686 relpath = zhp->zfs_name + strlen(source); 2687 if (relpath[0] == '/') 2688 relpath++; 2689 } 2690 2691 if ((zpool_get_prop(zhp->zpool_hdl, 2692 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2693 B_FALSE)) || (strcmp(root, "-") == 0)) 2694 root[0] = '\0'; 2695 /* 2696 * Special case an alternate root of '/'. This will 2697 * avoid having multiple leading slashes in the 2698 * mountpoint path. 2699 */ 2700 if (strcmp(root, "/") == 0) 2701 root++; 2702 2703 /* 2704 * If the mountpoint is '/' then skip over this 2705 * if we are obtaining either an alternate root or 2706 * an inherited mountpoint. 2707 */ 2708 if (str[1] == '\0' && (root[0] != '\0' || 2709 relpath[0] != '\0')) 2710 str++; 2711 2712 if (relpath[0] == '\0') 2713 (void) snprintf(propbuf, proplen, "%s%s", 2714 root, str); 2715 else 2716 (void) snprintf(propbuf, proplen, "%s%s%s%s", 2717 root, str, relpath[0] == '@' ? "" : "/", 2718 relpath); 2719 } else { 2720 /* 'legacy' or 'none' */ 2721 (void) strlcpy(propbuf, str, proplen); 2722 } 2723 zcp_check(zhp, prop, 0, propbuf); 2724 break; 2725 2726 case ZFS_PROP_ORIGIN: 2727 str = getprop_string(zhp, prop, &source); 2728 if (str == NULL) 2729 return (-1); 2730 (void) strlcpy(propbuf, str, proplen); 2731 zcp_check(zhp, prop, 0, str); 2732 break; 2733 2734 case ZFS_PROP_REDACT_SNAPS: 2735 if (get_rsnaps_string(zhp, propbuf, proplen) != 0) 2736 return (-1); 2737 break; 2738 2739 case ZFS_PROP_CLONES: 2740 if (get_clones_string(zhp, propbuf, proplen) != 0) 2741 return (-1); 2742 break; 2743 2744 case ZFS_PROP_QUOTA: 2745 case ZFS_PROP_REFQUOTA: 2746 case ZFS_PROP_RESERVATION: 2747 case ZFS_PROP_REFRESERVATION: 2748 2749 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2750 return (-1); 2751 /* 2752 * If quota or reservation is 0, we translate this into 'none' 2753 * (unless literal is set), and indicate that it's the default 2754 * value. Otherwise, we print the number nicely and indicate 2755 * that its set locally. 2756 */ 2757 if (val == 0) { 2758 if (literal) 2759 (void) strlcpy(propbuf, "0", proplen); 2760 else 2761 (void) strlcpy(propbuf, "none", proplen); 2762 } else { 2763 if (literal) 2764 (void) snprintf(propbuf, proplen, "%llu", 2765 (u_longlong_t)val); 2766 else 2767 zfs_nicebytes(val, propbuf, proplen); 2768 } 2769 zcp_check(zhp, prop, val, NULL); 2770 break; 2771 2772 case ZFS_PROP_FILESYSTEM_LIMIT: 2773 case ZFS_PROP_SNAPSHOT_LIMIT: 2774 case ZFS_PROP_FILESYSTEM_COUNT: 2775 case ZFS_PROP_SNAPSHOT_COUNT: 2776 2777 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2778 return (-1); 2779 2780 /* 2781 * If limit is UINT64_MAX, we translate this into 'none', and 2782 * indicate that it's the default value. Otherwise, we print 2783 * the number nicely and indicate that it's set locally. 2784 */ 2785 if (val == UINT64_MAX) { 2786 (void) strlcpy(propbuf, "none", proplen); 2787 } else if (literal) { 2788 (void) snprintf(propbuf, proplen, "%llu", 2789 (u_longlong_t)val); 2790 } else { 2791 zfs_nicenum(val, propbuf, proplen); 2792 } 2793 2794 zcp_check(zhp, prop, val, NULL); 2795 break; 2796 2797 case ZFS_PROP_REFRATIO: 2798 case ZFS_PROP_COMPRESSRATIO: 2799 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2800 return (-1); 2801 if (literal) 2802 (void) snprintf(propbuf, proplen, "%llu.%02llu", 2803 (u_longlong_t)(val / 100), 2804 (u_longlong_t)(val % 100)); 2805 else 2806 (void) snprintf(propbuf, proplen, "%llu.%02llux", 2807 (u_longlong_t)(val / 100), 2808 (u_longlong_t)(val % 100)); 2809 zcp_check(zhp, prop, val, NULL); 2810 break; 2811 2812 case ZFS_PROP_TYPE: 2813 switch (zhp->zfs_type) { 2814 case ZFS_TYPE_FILESYSTEM: 2815 str = "filesystem"; 2816 break; 2817 case ZFS_TYPE_VOLUME: 2818 str = "volume"; 2819 break; 2820 case ZFS_TYPE_SNAPSHOT: 2821 str = "snapshot"; 2822 break; 2823 case ZFS_TYPE_BOOKMARK: 2824 str = "bookmark"; 2825 break; 2826 default: 2827 abort(); 2828 } 2829 (void) snprintf(propbuf, proplen, "%s", str); 2830 zcp_check(zhp, prop, 0, propbuf); 2831 break; 2832 2833 case ZFS_PROP_MOUNTED: 2834 /* 2835 * The 'mounted' property is a pseudo-property that described 2836 * whether the filesystem is currently mounted. Even though 2837 * it's a boolean value, the typical values of "on" and "off" 2838 * don't make sense, so we translate to "yes" and "no". 2839 */ 2840 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 2841 src, &source, &val) != 0) 2842 return (-1); 2843 if (val) 2844 (void) strlcpy(propbuf, "yes", proplen); 2845 else 2846 (void) strlcpy(propbuf, "no", proplen); 2847 break; 2848 2849 case ZFS_PROP_NAME: 2850 /* 2851 * The 'name' property is a pseudo-property derived from the 2852 * dataset name. It is presented as a real property to simplify 2853 * consumers. 2854 */ 2855 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2856 zcp_check(zhp, prop, 0, propbuf); 2857 break; 2858 2859 case ZFS_PROP_MLSLABEL: 2860 { 2861 #ifdef HAVE_MLSLABEL 2862 m_label_t *new_sl = NULL; 2863 char *ascii = NULL; /* human readable label */ 2864 2865 (void) strlcpy(propbuf, 2866 getprop_string(zhp, prop, &source), proplen); 2867 2868 if (literal || (strcasecmp(propbuf, 2869 ZFS_MLSLABEL_DEFAULT) == 0)) 2870 break; 2871 2872 /* 2873 * Try to translate the internal hex string to 2874 * human-readable output. If there are any 2875 * problems just use the hex string. 2876 */ 2877 2878 if (str_to_label(propbuf, &new_sl, MAC_LABEL, 2879 L_NO_CORRECTION, NULL) == -1) { 2880 m_label_free(new_sl); 2881 break; 2882 } 2883 2884 if (label_to_str(new_sl, &ascii, M_LABEL, 2885 DEF_NAMES) != 0) { 2886 if (ascii) 2887 free(ascii); 2888 m_label_free(new_sl); 2889 break; 2890 } 2891 m_label_free(new_sl); 2892 2893 (void) strlcpy(propbuf, ascii, proplen); 2894 free(ascii); 2895 #else 2896 (void) strlcpy(propbuf, 2897 getprop_string(zhp, prop, &source), proplen); 2898 #endif /* HAVE_MLSLABEL */ 2899 } 2900 break; 2901 2902 case ZFS_PROP_GUID: 2903 case ZFS_PROP_KEY_GUID: 2904 case ZFS_PROP_IVSET_GUID: 2905 case ZFS_PROP_CREATETXG: 2906 case ZFS_PROP_OBJSETID: 2907 case ZFS_PROP_PBKDF2_ITERS: 2908 /* 2909 * These properties are stored as numbers, but they are 2910 * identifiers or counters. 2911 * We don't want them to be pretty printed, because pretty 2912 * printing truncates their values making them useless. 2913 */ 2914 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2915 return (-1); 2916 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2917 zcp_check(zhp, prop, val, NULL); 2918 break; 2919 2920 case ZFS_PROP_REFERENCED: 2921 case ZFS_PROP_AVAILABLE: 2922 case ZFS_PROP_USED: 2923 case ZFS_PROP_USEDSNAP: 2924 case ZFS_PROP_USEDDS: 2925 case ZFS_PROP_USEDREFRESERV: 2926 case ZFS_PROP_USEDCHILD: 2927 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2928 return (-1); 2929 if (literal) { 2930 (void) snprintf(propbuf, proplen, "%llu", 2931 (u_longlong_t)val); 2932 } else { 2933 zfs_nicebytes(val, propbuf, proplen); 2934 } 2935 zcp_check(zhp, prop, val, NULL); 2936 break; 2937 2938 case ZFS_PROP_SNAPSHOTS_CHANGED: 2939 { 2940 if ((get_numeric_property(zhp, prop, src, &source, 2941 &val) != 0) || val == 0) { 2942 return (-1); 2943 } 2944 2945 time_t time = (time_t)val; 2946 struct tm t; 2947 2948 if (literal || 2949 localtime_r(&time, &t) == NULL || 2950 strftime(propbuf, proplen, "%a %b %e %k:%M:%S %Y", 2951 &t) == 0) 2952 (void) snprintf(propbuf, proplen, "%llu", 2953 (u_longlong_t)val); 2954 } 2955 zcp_check(zhp, prop, val, NULL); 2956 break; 2957 2958 default: 2959 switch (zfs_prop_get_type(prop)) { 2960 case PROP_TYPE_NUMBER: 2961 if (get_numeric_property(zhp, prop, src, 2962 &source, &val) != 0) { 2963 return (-1); 2964 } 2965 2966 if (literal) { 2967 (void) snprintf(propbuf, proplen, "%llu", 2968 (u_longlong_t)val); 2969 } else { 2970 zfs_nicenum(val, propbuf, proplen); 2971 } 2972 zcp_check(zhp, prop, val, NULL); 2973 break; 2974 2975 case PROP_TYPE_STRING: 2976 str = getprop_string(zhp, prop, &source); 2977 if (str == NULL) 2978 return (-1); 2979 2980 (void) strlcpy(propbuf, str, proplen); 2981 zcp_check(zhp, prop, 0, str); 2982 break; 2983 2984 case PROP_TYPE_INDEX: 2985 if (get_numeric_property(zhp, prop, src, 2986 &source, &val) != 0) 2987 return (-1); 2988 if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2989 return (-1); 2990 2991 (void) strlcpy(propbuf, strval, proplen); 2992 zcp_check(zhp, prop, 0, strval); 2993 break; 2994 2995 default: 2996 abort(); 2997 } 2998 } 2999 3000 get_source(zhp, src, source, statbuf, statlen); 3001 3002 return (0); 3003 } 3004 3005 /* 3006 * Utility function to get the given numeric property. Does no validation that 3007 * the given property is the appropriate type; should only be used with 3008 * hard-coded property types. 3009 */ 3010 uint64_t 3011 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 3012 { 3013 char *source; 3014 uint64_t val = 0; 3015 3016 (void) get_numeric_property(zhp, prop, NULL, &source, &val); 3017 3018 return (val); 3019 } 3020 3021 static int 3022 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 3023 { 3024 char buf[64]; 3025 3026 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 3027 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 3028 } 3029 3030 /* 3031 * Similar to zfs_prop_get(), but returns the value as an integer. 3032 */ 3033 int 3034 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 3035 zprop_source_t *src, char *statbuf, size_t statlen) 3036 { 3037 char *source; 3038 3039 /* 3040 * Check to see if this property applies to our object 3041 */ 3042 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) { 3043 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 3044 dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 3045 zfs_prop_to_name(prop))); 3046 } 3047 3048 if (src) 3049 *src = ZPROP_SRC_NONE; 3050 3051 if (get_numeric_property(zhp, prop, src, &source, value) != 0) 3052 return (-1); 3053 3054 get_source(zhp, src, source, statbuf, statlen); 3055 3056 return (0); 3057 } 3058 3059 #ifdef HAVE_IDMAP 3060 static int 3061 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 3062 char **domainp, idmap_rid_t *ridp) 3063 { 3064 idmap_get_handle_t *get_hdl = NULL; 3065 idmap_stat status; 3066 int err = EINVAL; 3067 3068 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 3069 goto out; 3070 3071 if (isuser) { 3072 err = idmap_get_sidbyuid(get_hdl, id, 3073 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 3074 } else { 3075 err = idmap_get_sidbygid(get_hdl, id, 3076 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 3077 } 3078 if (err == IDMAP_SUCCESS && 3079 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 3080 status == IDMAP_SUCCESS) 3081 err = 0; 3082 else 3083 err = EINVAL; 3084 out: 3085 if (get_hdl) 3086 idmap_get_destroy(get_hdl); 3087 return (err); 3088 } 3089 #endif /* HAVE_IDMAP */ 3090 3091 /* 3092 * convert the propname into parameters needed by kernel 3093 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 3094 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 3095 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234 3096 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234 3097 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123 3098 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789 3099 */ 3100 static int 3101 userquota_propname_decode(const char *propname, boolean_t zoned, 3102 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 3103 { 3104 zfs_userquota_prop_t type; 3105 char *cp; 3106 boolean_t isuser; 3107 boolean_t isgroup; 3108 boolean_t isproject; 3109 struct passwd *pw; 3110 struct group *gr; 3111 3112 domain[0] = '\0'; 3113 3114 /* Figure out the property type ({user|group|project}{quota|space}) */ 3115 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 3116 if (strncmp(propname, zfs_userquota_prop_prefixes[type], 3117 strlen(zfs_userquota_prop_prefixes[type])) == 0) 3118 break; 3119 } 3120 if (type == ZFS_NUM_USERQUOTA_PROPS) 3121 return (EINVAL); 3122 *typep = type; 3123 3124 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED || 3125 type == ZFS_PROP_USEROBJQUOTA || 3126 type == ZFS_PROP_USEROBJUSED); 3127 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED || 3128 type == ZFS_PROP_GROUPOBJQUOTA || 3129 type == ZFS_PROP_GROUPOBJUSED); 3130 isproject = (type == ZFS_PROP_PROJECTQUOTA || 3131 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA || 3132 type == ZFS_PROP_PROJECTOBJUSED); 3133 3134 cp = strchr(propname, '@') + 1; 3135 3136 if (isuser && (pw = getpwnam(cp)) != NULL) { 3137 if (zoned && getzoneid() == GLOBAL_ZONEID) 3138 return (ENOENT); 3139 *ridp = pw->pw_uid; 3140 } else if (isgroup && (gr = getgrnam(cp)) != NULL) { 3141 if (zoned && getzoneid() == GLOBAL_ZONEID) 3142 return (ENOENT); 3143 *ridp = gr->gr_gid; 3144 } else if (!isproject && strchr(cp, '@')) { 3145 #ifdef HAVE_IDMAP 3146 /* 3147 * It's a SID name (eg "user@domain") that needs to be 3148 * turned into S-1-domainID-RID. 3149 */ 3150 directory_error_t e; 3151 char *numericsid = NULL; 3152 char *end; 3153 3154 if (zoned && getzoneid() == GLOBAL_ZONEID) 3155 return (ENOENT); 3156 if (isuser) { 3157 e = directory_sid_from_user_name(NULL, 3158 cp, &numericsid); 3159 } else { 3160 e = directory_sid_from_group_name(NULL, 3161 cp, &numericsid); 3162 } 3163 if (e != NULL) { 3164 directory_error_free(e); 3165 return (ENOENT); 3166 } 3167 if (numericsid == NULL) 3168 return (ENOENT); 3169 cp = numericsid; 3170 (void) strlcpy(domain, cp, domainlen); 3171 cp = strrchr(domain, '-'); 3172 *cp = '\0'; 3173 cp++; 3174 3175 errno = 0; 3176 *ridp = strtoull(cp, &end, 10); 3177 free(numericsid); 3178 3179 if (errno != 0 || *end != '\0') 3180 return (EINVAL); 3181 #else 3182 (void) domainlen; 3183 return (ENOSYS); 3184 #endif /* HAVE_IDMAP */ 3185 } else { 3186 /* It's a user/group/project ID (eg "12345"). */ 3187 uid_t id; 3188 char *end; 3189 id = strtoul(cp, &end, 10); 3190 if (*end != '\0') 3191 return (EINVAL); 3192 if (id > MAXUID && !isproject) { 3193 #ifdef HAVE_IDMAP 3194 /* It's an ephemeral ID. */ 3195 idmap_rid_t rid; 3196 char *mapdomain; 3197 3198 if (idmap_id_to_numeric_domain_rid(id, isuser, 3199 &mapdomain, &rid) != 0) 3200 return (ENOENT); 3201 (void) strlcpy(domain, mapdomain, domainlen); 3202 *ridp = rid; 3203 #else 3204 return (ENOSYS); 3205 #endif /* HAVE_IDMAP */ 3206 } else { 3207 *ridp = id; 3208 } 3209 } 3210 3211 return (0); 3212 } 3213 3214 static int 3215 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 3216 uint64_t *propvalue, zfs_userquota_prop_t *typep) 3217 { 3218 int err; 3219 zfs_cmd_t zc = {"\0"}; 3220 3221 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3222 3223 err = userquota_propname_decode(propname, 3224 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 3225 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 3226 zc.zc_objset_type = *typep; 3227 if (err) 3228 return (err); 3229 3230 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_USERSPACE_ONE, &zc); 3231 if (err) 3232 return (err); 3233 3234 *propvalue = zc.zc_cookie; 3235 return (0); 3236 } 3237 3238 int 3239 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 3240 uint64_t *propvalue) 3241 { 3242 zfs_userquota_prop_t type; 3243 3244 return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 3245 &type)); 3246 } 3247 3248 int 3249 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 3250 char *propbuf, int proplen, boolean_t literal) 3251 { 3252 int err; 3253 uint64_t propvalue; 3254 zfs_userquota_prop_t type; 3255 3256 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 3257 &type); 3258 3259 if (err) 3260 return (err); 3261 3262 if (literal) { 3263 (void) snprintf(propbuf, proplen, "%llu", 3264 (u_longlong_t)propvalue); 3265 } else if (propvalue == 0 && 3266 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || 3267 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA || 3268 type == ZFS_PROP_PROJECTQUOTA || 3269 type == ZFS_PROP_PROJECTOBJQUOTA)) { 3270 (void) strlcpy(propbuf, "none", proplen); 3271 } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || 3272 type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED || 3273 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) { 3274 zfs_nicebytes(propvalue, propbuf, proplen); 3275 } else { 3276 zfs_nicenum(propvalue, propbuf, proplen); 3277 } 3278 return (0); 3279 } 3280 3281 /* 3282 * propname must start with "written@" or "written#". 3283 */ 3284 int 3285 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 3286 uint64_t *propvalue) 3287 { 3288 int err; 3289 zfs_cmd_t zc = {"\0"}; 3290 const char *snapname; 3291 3292 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3293 3294 assert(zfs_prop_written(propname)); 3295 snapname = propname + strlen("written@"); 3296 if (strchr(snapname, '@') != NULL || strchr(snapname, '#') != NULL) { 3297 /* full snapshot or bookmark name specified */ 3298 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 3299 } else { 3300 /* snapname is the short name, append it to zhp's fsname */ 3301 char *cp; 3302 3303 (void) strlcpy(zc.zc_value, zhp->zfs_name, 3304 sizeof (zc.zc_value)); 3305 cp = strchr(zc.zc_value, '@'); 3306 if (cp != NULL) 3307 *cp = '\0'; 3308 (void) strlcat(zc.zc_value, snapname - 1, sizeof (zc.zc_value)); 3309 } 3310 3311 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SPACE_WRITTEN, &zc); 3312 if (err) 3313 return (err); 3314 3315 *propvalue = zc.zc_cookie; 3316 return (0); 3317 } 3318 3319 int 3320 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 3321 char *propbuf, int proplen, boolean_t literal) 3322 { 3323 int err; 3324 uint64_t propvalue; 3325 3326 err = zfs_prop_get_written_int(zhp, propname, &propvalue); 3327 3328 if (err) 3329 return (err); 3330 3331 if (literal) { 3332 (void) snprintf(propbuf, proplen, "%llu", 3333 (u_longlong_t)propvalue); 3334 } else { 3335 zfs_nicebytes(propvalue, propbuf, proplen); 3336 } 3337 3338 return (0); 3339 } 3340 3341 /* 3342 * Returns the name of the given zfs handle. 3343 */ 3344 const char * 3345 zfs_get_name(const zfs_handle_t *zhp) 3346 { 3347 return (zhp->zfs_name); 3348 } 3349 3350 /* 3351 * Returns the name of the parent pool for the given zfs handle. 3352 */ 3353 const char * 3354 zfs_get_pool_name(const zfs_handle_t *zhp) 3355 { 3356 return (zhp->zpool_hdl->zpool_name); 3357 } 3358 3359 /* 3360 * Returns the type of the given zfs handle. 3361 */ 3362 zfs_type_t 3363 zfs_get_type(const zfs_handle_t *zhp) 3364 { 3365 return (zhp->zfs_type); 3366 } 3367 3368 /* 3369 * Returns the type of the given zfs handle, 3370 * or, if a snapshot, the type of the snapshotted dataset. 3371 */ 3372 zfs_type_t 3373 zfs_get_underlying_type(const zfs_handle_t *zhp) 3374 { 3375 return (zhp->zfs_head_type); 3376 } 3377 3378 /* 3379 * Is one dataset name a child dataset of another? 3380 * 3381 * Needs to handle these cases: 3382 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 3383 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 3384 * Descendant? No. No. No. Yes. 3385 */ 3386 static boolean_t 3387 is_descendant(const char *ds1, const char *ds2) 3388 { 3389 size_t d1len = strlen(ds1); 3390 3391 /* ds2 can't be a descendant if it's smaller */ 3392 if (strlen(ds2) < d1len) 3393 return (B_FALSE); 3394 3395 /* otherwise, compare strings and verify that there's a '/' char */ 3396 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 3397 } 3398 3399 /* 3400 * Given a complete name, return just the portion that refers to the parent. 3401 * Will return -1 if there is no parent (path is just the name of the 3402 * pool). 3403 */ 3404 static int 3405 parent_name(const char *path, char *buf, size_t buflen) 3406 { 3407 char *slashp; 3408 3409 (void) strlcpy(buf, path, buflen); 3410 3411 if ((slashp = strrchr(buf, '/')) == NULL) 3412 return (-1); 3413 *slashp = '\0'; 3414 3415 return (0); 3416 } 3417 3418 int 3419 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen) 3420 { 3421 return (parent_name(zfs_get_name(zhp), buf, buflen)); 3422 } 3423 3424 /* 3425 * If accept_ancestor is false, then check to make sure that the given path has 3426 * a parent, and that it exists. If accept_ancestor is true, then find the 3427 * closest existing ancestor for the given path. In prefixlen return the 3428 * length of already existing prefix of the given path. We also fetch the 3429 * 'zoned' property, which is used to validate property settings when creating 3430 * new datasets. 3431 */ 3432 static int 3433 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 3434 boolean_t accept_ancestor, int *prefixlen) 3435 { 3436 zfs_cmd_t zc = {"\0"}; 3437 char parent[ZFS_MAX_DATASET_NAME_LEN]; 3438 char *slash; 3439 zfs_handle_t *zhp; 3440 char errbuf[ERRBUFLEN]; 3441 uint64_t is_zoned; 3442 3443 (void) snprintf(errbuf, sizeof (errbuf), 3444 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 3445 3446 /* get parent, and check to see if this is just a pool */ 3447 if (parent_name(path, parent, sizeof (parent)) != 0) { 3448 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3449 "missing dataset name")); 3450 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3451 } 3452 3453 /* check to see if the pool exists */ 3454 if ((slash = strchr(parent, '/')) == NULL) 3455 slash = parent + strlen(parent); 3456 (void) strncpy(zc.zc_name, parent, slash - parent); 3457 zc.zc_name[slash - parent] = '\0'; 3458 if (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 3459 errno == ENOENT) { 3460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3461 "no such pool '%s'"), zc.zc_name); 3462 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3463 } 3464 3465 /* check to see if the parent dataset exists */ 3466 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 3467 if (errno == ENOENT && accept_ancestor) { 3468 /* 3469 * Go deeper to find an ancestor, give up on top level. 3470 */ 3471 if (parent_name(parent, parent, sizeof (parent)) != 0) { 3472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3473 "no such pool '%s'"), zc.zc_name); 3474 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3475 } 3476 } else if (errno == ENOENT) { 3477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3478 "parent does not exist")); 3479 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3480 } else 3481 return (zfs_standard_error(hdl, errno, errbuf)); 3482 } 3483 3484 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 3485 if (zoned != NULL) 3486 *zoned = is_zoned; 3487 3488 /* we are in a non-global zone, but parent is in the global zone */ 3489 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 3490 (void) zfs_standard_error(hdl, EPERM, errbuf); 3491 zfs_close(zhp); 3492 return (-1); 3493 } 3494 3495 /* make sure parent is a filesystem */ 3496 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 3497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3498 "parent is not a filesystem")); 3499 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 3500 zfs_close(zhp); 3501 return (-1); 3502 } 3503 3504 zfs_close(zhp); 3505 if (prefixlen != NULL) 3506 *prefixlen = strlen(parent); 3507 return (0); 3508 } 3509 3510 /* 3511 * Finds whether the dataset of the given type(s) exists. 3512 */ 3513 boolean_t 3514 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 3515 { 3516 zfs_handle_t *zhp; 3517 3518 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 3519 return (B_FALSE); 3520 3521 /* 3522 * Try to get stats for the dataset, which will tell us if it exists. 3523 */ 3524 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 3525 int ds_type = zhp->zfs_type; 3526 3527 zfs_close(zhp); 3528 if (types & ds_type) 3529 return (B_TRUE); 3530 } 3531 return (B_FALSE); 3532 } 3533 3534 /* 3535 * Given a path to 'target', create all the ancestors between 3536 * the prefixlen portion of the path, and the target itself. 3537 * Fail if the initial prefixlen-ancestor does not already exist. 3538 */ 3539 int 3540 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 3541 { 3542 zfs_handle_t *h; 3543 char *cp; 3544 const char *opname; 3545 3546 /* make sure prefix exists */ 3547 cp = target + prefixlen; 3548 if (*cp != '/') { 3549 assert(strchr(cp, '/') == NULL); 3550 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3551 } else { 3552 *cp = '\0'; 3553 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3554 *cp = '/'; 3555 } 3556 if (h == NULL) 3557 return (-1); 3558 zfs_close(h); 3559 3560 /* 3561 * Attempt to create, mount, and share any ancestor filesystems, 3562 * up to the prefixlen-long one. 3563 */ 3564 for (cp = target + prefixlen + 1; 3565 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) { 3566 3567 *cp = '\0'; 3568 3569 h = make_dataset_handle(hdl, target); 3570 if (h) { 3571 /* it already exists, nothing to do here */ 3572 zfs_close(h); 3573 continue; 3574 } 3575 3576 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 3577 NULL) != 0) { 3578 opname = dgettext(TEXT_DOMAIN, "create"); 3579 goto ancestorerr; 3580 } 3581 3582 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3583 if (h == NULL) { 3584 opname = dgettext(TEXT_DOMAIN, "open"); 3585 goto ancestorerr; 3586 } 3587 3588 if (zfs_mount(h, NULL, 0) != 0) { 3589 opname = dgettext(TEXT_DOMAIN, "mount"); 3590 goto ancestorerr; 3591 } 3592 3593 if (zfs_share(h, NULL) != 0) { 3594 opname = dgettext(TEXT_DOMAIN, "share"); 3595 goto ancestorerr; 3596 } 3597 3598 zfs_close(h); 3599 } 3600 zfs_commit_shares(NULL); 3601 3602 return (0); 3603 3604 ancestorerr: 3605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3606 "failed to %s ancestor '%s'"), opname, target); 3607 return (-1); 3608 } 3609 3610 /* 3611 * Creates non-existing ancestors of the given path. 3612 */ 3613 int 3614 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 3615 { 3616 int prefix; 3617 char *path_copy; 3618 char errbuf[ERRBUFLEN]; 3619 int rc = 0; 3620 3621 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3622 "cannot create '%s'"), path); 3623 3624 /* 3625 * Check that we are not passing the nesting limit 3626 * before we start creating any ancestors. 3627 */ 3628 if (dataset_nestcheck(path) != 0) { 3629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3630 "maximum name nesting depth exceeded")); 3631 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3632 } 3633 3634 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 3635 return (-1); 3636 3637 if ((path_copy = strdup(path)) != NULL) { 3638 rc = create_parents(hdl, path_copy, prefix); 3639 free(path_copy); 3640 } 3641 if (path_copy == NULL || rc != 0) 3642 return (-1); 3643 3644 return (0); 3645 } 3646 3647 /* 3648 * Create a new filesystem or volume. 3649 */ 3650 int 3651 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3652 nvlist_t *props) 3653 { 3654 int ret; 3655 uint64_t size = 0; 3656 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 3657 uint64_t zoned; 3658 enum lzc_dataset_type ost; 3659 zpool_handle_t *zpool_handle; 3660 uint8_t *wkeydata = NULL; 3661 uint_t wkeylen = 0; 3662 char errbuf[ERRBUFLEN]; 3663 char parent[ZFS_MAX_DATASET_NAME_LEN]; 3664 3665 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3666 "cannot create '%s'"), path); 3667 3668 /* validate the path, taking care to note the extended error message */ 3669 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 3670 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3671 3672 if (dataset_nestcheck(path) != 0) { 3673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3674 "maximum name nesting depth exceeded")); 3675 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3676 } 3677 3678 /* validate parents exist */ 3679 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3680 return (-1); 3681 3682 /* 3683 * The failure modes when creating a dataset of a different type over 3684 * one that already exists is a little strange. In particular, if you 3685 * try to create a dataset on top of an existing dataset, the ioctl() 3686 * will return ENOENT, not EEXIST. To prevent this from happening, we 3687 * first try to see if the dataset exists. 3688 */ 3689 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 3690 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3691 "dataset already exists")); 3692 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3693 } 3694 3695 if (type == ZFS_TYPE_VOLUME) 3696 ost = LZC_DATSET_TYPE_ZVOL; 3697 else 3698 ost = LZC_DATSET_TYPE_ZFS; 3699 3700 /* open zpool handle for prop validation */ 3701 char pool_path[ZFS_MAX_DATASET_NAME_LEN]; 3702 (void) strlcpy(pool_path, path, sizeof (pool_path)); 3703 3704 /* truncate pool_path at first slash */ 3705 char *p = strchr(pool_path, '/'); 3706 if (p != NULL) 3707 *p = '\0'; 3708 3709 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL) 3710 return (-1); 3711 3712 if (props && (props = zfs_valid_proplist(hdl, type, props, 3713 zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) { 3714 zpool_close(zpool_handle); 3715 return (-1); 3716 } 3717 zpool_close(zpool_handle); 3718 3719 if (type == ZFS_TYPE_VOLUME) { 3720 /* 3721 * If we are creating a volume, the size and block size must 3722 * satisfy a few restraints. First, the blocksize must be a 3723 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 3724 * volsize must be a multiple of the block size, and cannot be 3725 * zero. 3726 */ 3727 if (props == NULL || nvlist_lookup_uint64(props, 3728 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3729 nvlist_free(props); 3730 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3731 "missing volume size")); 3732 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3733 } 3734 3735 if ((ret = nvlist_lookup_uint64(props, 3736 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3737 &blocksize)) != 0) { 3738 if (ret == ENOENT) { 3739 blocksize = zfs_prop_default_numeric( 3740 ZFS_PROP_VOLBLOCKSIZE); 3741 } else { 3742 nvlist_free(props); 3743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3744 "missing volume block size")); 3745 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3746 } 3747 } 3748 3749 if (size == 0) { 3750 nvlist_free(props); 3751 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3752 "volume size cannot be zero")); 3753 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3754 } 3755 3756 if (size % blocksize != 0) { 3757 nvlist_free(props); 3758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3759 "volume size must be a multiple of volume block " 3760 "size")); 3761 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3762 } 3763 } 3764 3765 (void) parent_name(path, parent, sizeof (parent)); 3766 if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE, 3767 &wkeydata, &wkeylen) != 0) { 3768 nvlist_free(props); 3769 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 3770 } 3771 3772 /* create the dataset */ 3773 ret = lzc_create(path, ost, props, wkeydata, wkeylen); 3774 nvlist_free(props); 3775 if (wkeydata != NULL) 3776 free(wkeydata); 3777 3778 /* check for failure */ 3779 if (ret != 0) { 3780 switch (errno) { 3781 case ENOENT: 3782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3783 "no such parent '%s'"), parent); 3784 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3785 3786 case ENOTSUP: 3787 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3788 "pool must be upgraded to set this " 3789 "property or value")); 3790 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3791 3792 case EACCES: 3793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3794 "encryption root's key is not loaded " 3795 "or provided")); 3796 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 3797 3798 case ERANGE: 3799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3800 "invalid property value(s) specified")); 3801 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3802 #ifdef _ILP32 3803 case EOVERFLOW: 3804 /* 3805 * This platform can't address a volume this big. 3806 */ 3807 if (type == ZFS_TYPE_VOLUME) 3808 return (zfs_error(hdl, EZFS_VOLTOOBIG, 3809 errbuf)); 3810 zfs_fallthrough; 3811 #endif 3812 default: 3813 return (zfs_standard_error(hdl, errno, errbuf)); 3814 } 3815 } 3816 3817 return (0); 3818 } 3819 3820 /* 3821 * Destroys the given dataset. The caller must make sure that the filesystem 3822 * isn't mounted, and that there are no active dependents. If the file system 3823 * does not exist this function does nothing. 3824 */ 3825 int 3826 zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3827 { 3828 int error; 3829 3830 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer) 3831 return (EINVAL); 3832 3833 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 3834 nvlist_t *nv = fnvlist_alloc(); 3835 fnvlist_add_boolean(nv, zhp->zfs_name); 3836 error = lzc_destroy_bookmarks(nv, NULL); 3837 fnvlist_free(nv); 3838 if (error != 0) { 3839 return (zfs_standard_error_fmt(zhp->zfs_hdl, error, 3840 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 3841 zhp->zfs_name)); 3842 } 3843 return (0); 3844 } 3845 3846 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3847 nvlist_t *nv = fnvlist_alloc(); 3848 fnvlist_add_boolean(nv, zhp->zfs_name); 3849 error = lzc_destroy_snaps(nv, defer, NULL); 3850 fnvlist_free(nv); 3851 } else { 3852 error = lzc_destroy(zhp->zfs_name); 3853 } 3854 3855 if (error != 0 && error != ENOENT) { 3856 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3857 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 3858 zhp->zfs_name)); 3859 } 3860 3861 remove_mountpoint(zhp); 3862 3863 return (0); 3864 } 3865 3866 struct destroydata { 3867 nvlist_t *nvl; 3868 const char *snapname; 3869 }; 3870 3871 static int 3872 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 3873 { 3874 struct destroydata *dd = arg; 3875 char name[ZFS_MAX_DATASET_NAME_LEN]; 3876 int rv = 0; 3877 3878 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, 3879 dd->snapname) >= sizeof (name)) 3880 return (EINVAL); 3881 3882 if (lzc_exists(name)) 3883 fnvlist_add_boolean(dd->nvl, name); 3884 3885 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 3886 zfs_close(zhp); 3887 return (rv); 3888 } 3889 3890 /* 3891 * Destroys all snapshots with the given name in zhp & descendants. 3892 */ 3893 int 3894 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 3895 { 3896 int ret; 3897 struct destroydata dd = { 0 }; 3898 3899 dd.snapname = snapname; 3900 dd.nvl = fnvlist_alloc(); 3901 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 3902 3903 if (nvlist_empty(dd.nvl)) { 3904 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 3905 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 3906 zhp->zfs_name, snapname); 3907 } else { 3908 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 3909 } 3910 fnvlist_free(dd.nvl); 3911 return (ret); 3912 } 3913 3914 /* 3915 * Destroys all the snapshots named in the nvlist. 3916 */ 3917 int 3918 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 3919 { 3920 nvlist_t *errlist = NULL; 3921 nvpair_t *pair; 3922 3923 int ret = zfs_destroy_snaps_nvl_os(hdl, snaps); 3924 if (ret != 0) 3925 return (ret); 3926 3927 ret = lzc_destroy_snaps(snaps, defer, &errlist); 3928 3929 if (ret == 0) { 3930 nvlist_free(errlist); 3931 return (0); 3932 } 3933 3934 if (nvlist_empty(errlist)) { 3935 char errbuf[ERRBUFLEN]; 3936 (void) snprintf(errbuf, sizeof (errbuf), 3937 dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 3938 3939 ret = zfs_standard_error(hdl, ret, errbuf); 3940 } 3941 for (pair = nvlist_next_nvpair(errlist, NULL); 3942 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 3943 char errbuf[ERRBUFLEN]; 3944 (void) snprintf(errbuf, sizeof (errbuf), 3945 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 3946 nvpair_name(pair)); 3947 3948 switch (fnvpair_value_int32(pair)) { 3949 case EEXIST: 3950 zfs_error_aux(hdl, 3951 dgettext(TEXT_DOMAIN, "snapshot is cloned")); 3952 ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 3953 break; 3954 default: 3955 ret = zfs_standard_error(hdl, errno, errbuf); 3956 break; 3957 } 3958 } 3959 3960 nvlist_free(errlist); 3961 return (ret); 3962 } 3963 3964 /* 3965 * Clones the given dataset. The target must be of the same type as the source. 3966 */ 3967 int 3968 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3969 { 3970 char parent[ZFS_MAX_DATASET_NAME_LEN]; 3971 int ret; 3972 char errbuf[ERRBUFLEN]; 3973 libzfs_handle_t *hdl = zhp->zfs_hdl; 3974 uint64_t zoned; 3975 3976 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3977 3978 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3979 "cannot create '%s'"), target); 3980 3981 /* validate the target/clone name */ 3982 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 3983 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3984 3985 /* validate parents exist */ 3986 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3987 return (-1); 3988 3989 (void) parent_name(target, parent, sizeof (parent)); 3990 3991 /* do the clone */ 3992 3993 if (props) { 3994 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 3995 3996 if (ZFS_IS_VOLUME(zhp)) 3997 type = ZFS_TYPE_VOLUME; 3998 if ((props = zfs_valid_proplist(hdl, type, props, zoned, 3999 zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL) 4000 return (-1); 4001 if (zfs_fix_auto_resv(zhp, props) == -1) { 4002 nvlist_free(props); 4003 return (-1); 4004 } 4005 } 4006 4007 if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) { 4008 nvlist_free(props); 4009 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 4010 } 4011 4012 ret = lzc_clone(target, zhp->zfs_name, props); 4013 nvlist_free(props); 4014 4015 if (ret != 0) { 4016 switch (errno) { 4017 4018 case ENOENT: 4019 /* 4020 * The parent doesn't exist. We should have caught this 4021 * above, but there may a race condition that has since 4022 * destroyed the parent. 4023 * 4024 * At this point, we don't know whether it's the source 4025 * that doesn't exist anymore, or whether the target 4026 * dataset doesn't exist. 4027 */ 4028 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4029 "no such parent '%s'"), parent); 4030 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 4031 4032 case EXDEV: 4033 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4034 "source and target pools differ")); 4035 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 4036 errbuf)); 4037 4038 default: 4039 return (zfs_standard_error(zhp->zfs_hdl, errno, 4040 errbuf)); 4041 } 4042 } 4043 4044 return (ret); 4045 } 4046 4047 /* 4048 * Promotes the given clone fs to be the clone parent. 4049 */ 4050 int 4051 zfs_promote(zfs_handle_t *zhp) 4052 { 4053 libzfs_handle_t *hdl = zhp->zfs_hdl; 4054 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 4055 int ret; 4056 char errbuf[ERRBUFLEN]; 4057 4058 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4059 "cannot promote '%s'"), zhp->zfs_name); 4060 4061 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 4062 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4063 "snapshots can not be promoted")); 4064 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4065 } 4066 4067 if (zhp->zfs_dmustats.dds_origin[0] == '\0') { 4068 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4069 "not a cloned filesystem")); 4070 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4071 } 4072 4073 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 4074 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4075 4076 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname)); 4077 4078 if (ret != 0) { 4079 switch (ret) { 4080 case EACCES: 4081 /* 4082 * Promoting encrypted dataset outside its 4083 * encryption root. 4084 */ 4085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4086 "cannot promote dataset outside its " 4087 "encryption root")); 4088 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 4089 4090 case EEXIST: 4091 /* There is a conflicting snapshot name. */ 4092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4093 "conflicting snapshot '%s' from parent '%s'"), 4094 snapname, zhp->zfs_dmustats.dds_origin); 4095 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 4096 4097 default: 4098 return (zfs_standard_error(hdl, ret, errbuf)); 4099 } 4100 } 4101 return (ret); 4102 } 4103 4104 typedef struct snapdata { 4105 nvlist_t *sd_nvl; 4106 const char *sd_snapname; 4107 } snapdata_t; 4108 4109 static int 4110 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 4111 { 4112 snapdata_t *sd = arg; 4113 char name[ZFS_MAX_DATASET_NAME_LEN]; 4114 int rv = 0; 4115 4116 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 4117 if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp), 4118 sd->sd_snapname) >= sizeof (name)) 4119 return (EINVAL); 4120 4121 fnvlist_add_boolean(sd->sd_nvl, name); 4122 4123 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 4124 } 4125 zfs_close(zhp); 4126 4127 return (rv); 4128 } 4129 4130 /* 4131 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 4132 * created. 4133 */ 4134 int 4135 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 4136 { 4137 int ret; 4138 char errbuf[ERRBUFLEN]; 4139 nvpair_t *elem; 4140 nvlist_t *errors; 4141 zpool_handle_t *zpool_hdl; 4142 char pool[ZFS_MAX_DATASET_NAME_LEN]; 4143 4144 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4145 "cannot create snapshots ")); 4146 4147 elem = NULL; 4148 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 4149 const char *snapname = nvpair_name(elem); 4150 4151 /* validate the target name */ 4152 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 4153 B_TRUE)) { 4154 (void) snprintf(errbuf, sizeof (errbuf), 4155 dgettext(TEXT_DOMAIN, 4156 "cannot create snapshot '%s'"), snapname); 4157 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4158 } 4159 } 4160 4161 /* 4162 * get pool handle for prop validation. assumes all snaps are in the 4163 * same pool, as does lzc_snapshot (below). 4164 */ 4165 elem = nvlist_next_nvpair(snaps, NULL); 4166 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); 4167 pool[strcspn(pool, "/@")] = '\0'; 4168 zpool_hdl = zpool_open(hdl, pool); 4169 if (zpool_hdl == NULL) 4170 return (-1); 4171 4172 if (props != NULL && 4173 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 4174 props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) { 4175 zpool_close(zpool_hdl); 4176 return (-1); 4177 } 4178 zpool_close(zpool_hdl); 4179 4180 ret = lzc_snapshot(snaps, props, &errors); 4181 4182 if (ret != 0) { 4183 boolean_t printed = B_FALSE; 4184 for (elem = nvlist_next_nvpair(errors, NULL); 4185 elem != NULL; 4186 elem = nvlist_next_nvpair(errors, elem)) { 4187 (void) snprintf(errbuf, sizeof (errbuf), 4188 dgettext(TEXT_DOMAIN, 4189 "cannot create snapshot '%s'"), nvpair_name(elem)); 4190 (void) zfs_standard_error(hdl, 4191 fnvpair_value_int32(elem), errbuf); 4192 printed = B_TRUE; 4193 } 4194 if (!printed) { 4195 switch (ret) { 4196 case EXDEV: 4197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4198 "multiple snapshots of same " 4199 "fs not allowed")); 4200 (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4201 4202 break; 4203 default: 4204 (void) zfs_standard_error(hdl, ret, errbuf); 4205 } 4206 } 4207 } 4208 4209 nvlist_free(props); 4210 nvlist_free(errors); 4211 return (ret); 4212 } 4213 4214 int 4215 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 4216 nvlist_t *props) 4217 { 4218 int ret; 4219 snapdata_t sd = { 0 }; 4220 char fsname[ZFS_MAX_DATASET_NAME_LEN]; 4221 char *cp; 4222 zfs_handle_t *zhp; 4223 char errbuf[ERRBUFLEN]; 4224 4225 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4226 "cannot snapshot %s"), path); 4227 4228 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 4229 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4230 4231 (void) strlcpy(fsname, path, sizeof (fsname)); 4232 cp = strchr(fsname, '@'); 4233 *cp = '\0'; 4234 sd.sd_snapname = cp + 1; 4235 4236 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 4237 ZFS_TYPE_VOLUME)) == NULL) { 4238 return (-1); 4239 } 4240 4241 sd.sd_nvl = fnvlist_alloc(); 4242 if (recursive) { 4243 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 4244 } else { 4245 fnvlist_add_boolean(sd.sd_nvl, path); 4246 } 4247 4248 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 4249 fnvlist_free(sd.sd_nvl); 4250 zfs_close(zhp); 4251 return (ret); 4252 } 4253 4254 /* 4255 * Destroy any more recent snapshots. We invoke this callback on any dependents 4256 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 4257 * is a dependent and we should just destroy it without checking the transaction 4258 * group. 4259 */ 4260 typedef struct rollback_data { 4261 const char *cb_target; /* the snapshot */ 4262 uint64_t cb_create; /* creation time reference */ 4263 boolean_t cb_error; 4264 boolean_t cb_force; 4265 } rollback_data_t; 4266 4267 static int 4268 rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 4269 { 4270 rollback_data_t *cbp = data; 4271 prop_changelist_t *clp; 4272 4273 /* We must destroy this clone; first unmount it */ 4274 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 4275 cbp->cb_force ? MS_FORCE: 0); 4276 if (clp == NULL || changelist_prefix(clp) != 0) { 4277 cbp->cb_error = B_TRUE; 4278 zfs_close(zhp); 4279 return (0); 4280 } 4281 if (zfs_destroy(zhp, B_FALSE) != 0) 4282 cbp->cb_error = B_TRUE; 4283 else 4284 changelist_remove(clp, zhp->zfs_name); 4285 (void) changelist_postfix(clp); 4286 changelist_free(clp); 4287 4288 zfs_close(zhp); 4289 return (0); 4290 } 4291 4292 static int 4293 rollback_destroy(zfs_handle_t *zhp, void *data) 4294 { 4295 rollback_data_t *cbp = data; 4296 4297 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 4298 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 4299 rollback_destroy_dependent, cbp); 4300 4301 cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 4302 } 4303 4304 zfs_close(zhp); 4305 return (0); 4306 } 4307 4308 /* 4309 * Given a dataset, rollback to a specific snapshot, discarding any 4310 * data changes since then and making it the active dataset. 4311 * 4312 * Any snapshots and bookmarks more recent than the target are 4313 * destroyed, along with their dependents (i.e. clones). 4314 */ 4315 int 4316 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 4317 { 4318 rollback_data_t cb = { 0 }; 4319 int err; 4320 boolean_t restore_resv = 0; 4321 uint64_t old_volsize = 0, new_volsize; 4322 zfs_prop_t resv_prop = { 0 }; 4323 uint64_t min_txg = 0; 4324 4325 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 4326 zhp->zfs_type == ZFS_TYPE_VOLUME); 4327 4328 /* 4329 * Destroy all recent snapshots and their dependents. 4330 */ 4331 cb.cb_force = force; 4332 cb.cb_target = snap->zfs_name; 4333 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 4334 4335 if (cb.cb_create > 0) 4336 min_txg = cb.cb_create; 4337 4338 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb, 4339 min_txg, 0); 4340 4341 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); 4342 4343 if (cb.cb_error) 4344 return (-1); 4345 4346 /* 4347 * Now that we have verified that the snapshot is the latest, 4348 * rollback to the given snapshot. 4349 */ 4350 4351 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 4352 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 4353 return (-1); 4354 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 4355 restore_resv = 4356 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 4357 } 4358 4359 /* 4360 * Pass both the filesystem and the wanted snapshot names, 4361 * we would get an error back if the snapshot is destroyed or 4362 * a new snapshot is created before this request is processed. 4363 */ 4364 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name); 4365 if (err != 0) { 4366 char errbuf[ERRBUFLEN]; 4367 4368 (void) snprintf(errbuf, sizeof (errbuf), 4369 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 4370 zhp->zfs_name); 4371 switch (err) { 4372 case EEXIST: 4373 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4374 "there is a snapshot or bookmark more recent " 4375 "than '%s'"), snap->zfs_name); 4376 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf); 4377 break; 4378 case ESRCH: 4379 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4380 "'%s' is not found among snapshots of '%s'"), 4381 snap->zfs_name, zhp->zfs_name); 4382 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf); 4383 break; 4384 case EINVAL: 4385 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf); 4386 break; 4387 default: 4388 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf); 4389 } 4390 return (err); 4391 } 4392 4393 /* 4394 * For volumes, if the pre-rollback volsize matched the pre- 4395 * rollback reservation and the volsize has changed then set 4396 * the reservation property to the post-rollback volsize. 4397 * Make a new handle since the rollback closed the dataset. 4398 */ 4399 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 4400 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 4401 if (restore_resv) { 4402 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 4403 if (old_volsize != new_volsize) 4404 err = zfs_prop_set_int(zhp, resv_prop, 4405 new_volsize); 4406 } 4407 zfs_close(zhp); 4408 } 4409 return (err); 4410 } 4411 4412 /* 4413 * Renames the given dataset. 4414 */ 4415 int 4416 zfs_rename(zfs_handle_t *zhp, const char *target, renameflags_t flags) 4417 { 4418 int ret = 0; 4419 zfs_cmd_t zc = {"\0"}; 4420 char *delim; 4421 prop_changelist_t *cl = NULL; 4422 char parent[ZFS_MAX_DATASET_NAME_LEN]; 4423 char property[ZFS_MAXPROPLEN]; 4424 libzfs_handle_t *hdl = zhp->zfs_hdl; 4425 char errbuf[ERRBUFLEN]; 4426 4427 /* if we have the same exact name, just return success */ 4428 if (strcmp(zhp->zfs_name, target) == 0) 4429 return (0); 4430 4431 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4432 "cannot rename to '%s'"), target); 4433 4434 /* make sure source name is valid */ 4435 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 4436 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4437 4438 /* 4439 * Make sure the target name is valid 4440 */ 4441 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 4442 if ((strchr(target, '@') == NULL) || 4443 *target == '@') { 4444 /* 4445 * Snapshot target name is abbreviated, 4446 * reconstruct full dataset name 4447 */ 4448 (void) strlcpy(parent, zhp->zfs_name, 4449 sizeof (parent)); 4450 delim = strchr(parent, '@'); 4451 if (strchr(target, '@') == NULL) 4452 *(++delim) = '\0'; 4453 else 4454 *delim = '\0'; 4455 (void) strlcat(parent, target, sizeof (parent)); 4456 target = parent; 4457 } else { 4458 /* 4459 * Make sure we're renaming within the same dataset. 4460 */ 4461 delim = strchr(target, '@'); 4462 if (strncmp(zhp->zfs_name, target, delim - target) 4463 != 0 || zhp->zfs_name[delim - target] != '@') { 4464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4465 "snapshots must be part of same " 4466 "dataset")); 4467 return (zfs_error(hdl, EZFS_CROSSTARGET, 4468 errbuf)); 4469 } 4470 } 4471 4472 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 4473 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4474 } else { 4475 if (flags.recursive) { 4476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4477 "recursive rename must be a snapshot")); 4478 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4479 } 4480 4481 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 4482 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4483 4484 /* validate parents */ 4485 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 4486 return (-1); 4487 4488 /* make sure we're in the same pool */ 4489 verify((delim = strchr(target, '/')) != NULL); 4490 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4491 zhp->zfs_name[delim - target] != '/') { 4492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4493 "datasets must be within same pool")); 4494 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4495 } 4496 4497 /* new name cannot be a child of the current dataset name */ 4498 if (is_descendant(zhp->zfs_name, target)) { 4499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4500 "New dataset name cannot be a descendant of " 4501 "current dataset name")); 4502 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4503 } 4504 } 4505 4506 (void) snprintf(errbuf, sizeof (errbuf), 4507 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 4508 4509 if (getzoneid() == GLOBAL_ZONEID && 4510 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 4511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4512 "dataset is used in a non-global zone")); 4513 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4514 } 4515 4516 /* 4517 * Avoid unmounting file systems with mountpoint property set to 4518 * 'legacy' or 'none' even if -u option is not given. 4519 */ 4520 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && 4521 !flags.recursive && !flags.nounmount && 4522 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property, 4523 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 && 4524 (strcmp(property, "legacy") == 0 || 4525 strcmp(property, "none") == 0)) { 4526 flags.nounmount = B_TRUE; 4527 } 4528 if (flags.recursive) { 4529 char *parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 4530 delim = strchr(parentname, '@'); 4531 *delim = '\0'; 4532 zfs_handle_t *zhrp = zfs_open(zhp->zfs_hdl, parentname, 4533 ZFS_TYPE_DATASET); 4534 free(parentname); 4535 if (zhrp == NULL) { 4536 ret = -1; 4537 goto error; 4538 } 4539 zfs_close(zhrp); 4540 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 4541 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 4542 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 4543 CL_GATHER_ITER_MOUNTED, 4544 flags.forceunmount ? MS_FORCE : 0)) == NULL) 4545 return (-1); 4546 4547 if (changelist_haszonedchild(cl)) { 4548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4549 "child dataset with inherited mountpoint is used " 4550 "in a non-global zone")); 4551 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 4552 ret = -1; 4553 goto error; 4554 } 4555 4556 if ((ret = changelist_prefix(cl)) != 0) 4557 goto error; 4558 } 4559 4560 if (ZFS_IS_VOLUME(zhp)) 4561 zc.zc_objset_type = DMU_OST_ZVOL; 4562 else 4563 zc.zc_objset_type = DMU_OST_ZFS; 4564 4565 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4566 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 4567 4568 zc.zc_cookie = !!flags.recursive; 4569 zc.zc_cookie |= (!!flags.nounmount) << 1; 4570 4571 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 4572 /* 4573 * if it was recursive, the one that actually failed will 4574 * be in zc.zc_name 4575 */ 4576 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4577 "cannot rename '%s'"), zc.zc_name); 4578 4579 if (flags.recursive && errno == EEXIST) { 4580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4581 "a child dataset already has a snapshot " 4582 "with the new name")); 4583 (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4584 } else if (errno == EACCES) { 4585 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4586 "cannot move encrypted child outside of " 4587 "its encryption root")); 4588 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf); 4589 } else { 4590 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4591 } 4592 4593 /* 4594 * On failure, we still want to remount any filesystems that 4595 * were previously mounted, so we don't alter the system state. 4596 */ 4597 if (cl != NULL) 4598 (void) changelist_postfix(cl); 4599 } else { 4600 if (cl != NULL) { 4601 changelist_rename(cl, zfs_get_name(zhp), target); 4602 ret = changelist_postfix(cl); 4603 } 4604 } 4605 4606 error: 4607 if (cl != NULL) { 4608 changelist_free(cl); 4609 } 4610 return (ret); 4611 } 4612 4613 nvlist_t * 4614 zfs_get_all_props(zfs_handle_t *zhp) 4615 { 4616 return (zhp->zfs_props); 4617 } 4618 4619 nvlist_t * 4620 zfs_get_recvd_props(zfs_handle_t *zhp) 4621 { 4622 if (zhp->zfs_recvd_props == NULL) 4623 if (get_recvd_props_ioctl(zhp) != 0) 4624 return (NULL); 4625 return (zhp->zfs_recvd_props); 4626 } 4627 4628 nvlist_t * 4629 zfs_get_user_props(zfs_handle_t *zhp) 4630 { 4631 return (zhp->zfs_user_props); 4632 } 4633 4634 /* 4635 * This function is used by 'zfs list' to determine the exact set of columns to 4636 * display, and their maximum widths. This does two main things: 4637 * 4638 * - If this is a list of all properties, then expand the list to include 4639 * all native properties, and set a flag so that for each dataset we look 4640 * for new unique user properties and add them to the list. 4641 * 4642 * - For non fixed-width properties, keep track of the maximum width seen 4643 * so that we can size the column appropriately. If the user has 4644 * requested received property values, we also need to compute the width 4645 * of the RECEIVED column. 4646 */ 4647 int 4648 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 4649 boolean_t literal) 4650 { 4651 libzfs_handle_t *hdl = zhp->zfs_hdl; 4652 zprop_list_t *entry; 4653 zprop_list_t **last, **start; 4654 nvlist_t *userprops, *propval; 4655 nvpair_t *elem; 4656 char *strval; 4657 char buf[ZFS_MAXPROPLEN]; 4658 4659 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 4660 return (-1); 4661 4662 userprops = zfs_get_user_props(zhp); 4663 4664 entry = *plp; 4665 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4666 /* 4667 * Go through and add any user properties as necessary. We 4668 * start by incrementing our list pointer to the first 4669 * non-native property. 4670 */ 4671 start = plp; 4672 while (*start != NULL) { 4673 if ((*start)->pl_prop == ZPROP_USERPROP) 4674 break; 4675 start = &(*start)->pl_next; 4676 } 4677 4678 elem = NULL; 4679 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4680 /* 4681 * See if we've already found this property in our list. 4682 */ 4683 for (last = start; *last != NULL; 4684 last = &(*last)->pl_next) { 4685 if (strcmp((*last)->pl_user_prop, 4686 nvpair_name(elem)) == 0) 4687 break; 4688 } 4689 4690 if (*last == NULL) { 4691 entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 4692 entry->pl_user_prop = 4693 zfs_strdup(hdl, nvpair_name(elem)); 4694 entry->pl_prop = ZPROP_USERPROP; 4695 entry->pl_width = strlen(nvpair_name(elem)); 4696 entry->pl_all = B_TRUE; 4697 *last = entry; 4698 } 4699 } 4700 } 4701 4702 /* 4703 * Now go through and check the width of any non-fixed columns 4704 */ 4705 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 4706 if (entry->pl_fixed && !literal) 4707 continue; 4708 4709 if (entry->pl_prop != ZPROP_USERPROP) { 4710 if (zfs_prop_get(zhp, entry->pl_prop, 4711 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 4712 if (strlen(buf) > entry->pl_width) 4713 entry->pl_width = strlen(buf); 4714 } 4715 if (received && zfs_prop_get_recvd(zhp, 4716 zfs_prop_to_name(entry->pl_prop), 4717 buf, sizeof (buf), literal) == 0) 4718 if (strlen(buf) > entry->pl_recvd_width) 4719 entry->pl_recvd_width = strlen(buf); 4720 } else { 4721 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 4722 &propval) == 0) { 4723 strval = fnvlist_lookup_string(propval, 4724 ZPROP_VALUE); 4725 if (strlen(strval) > entry->pl_width) 4726 entry->pl_width = strlen(strval); 4727 } 4728 if (received && zfs_prop_get_recvd(zhp, 4729 entry->pl_user_prop, 4730 buf, sizeof (buf), literal) == 0) 4731 if (strlen(buf) > entry->pl_recvd_width) 4732 entry->pl_recvd_width = strlen(buf); 4733 } 4734 } 4735 4736 return (0); 4737 } 4738 4739 void 4740 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 4741 { 4742 nvpair_t *curr; 4743 nvpair_t *next; 4744 4745 /* 4746 * Keep a reference to the props-table against which we prune the 4747 * properties. 4748 */ 4749 zhp->zfs_props_table = props; 4750 4751 curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 4752 4753 while (curr) { 4754 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 4755 next = nvlist_next_nvpair(zhp->zfs_props, curr); 4756 4757 /* 4758 * User properties will result in ZPROP_USERPROP (an alias 4759 * for ZPROP_INVAL), and since we 4760 * only know how to prune standard ZFS properties, we always 4761 * leave these in the list. This can also happen if we 4762 * encounter an unknown DSL property (when running older 4763 * software, for example). 4764 */ 4765 if (zfs_prop != ZPROP_USERPROP && props[zfs_prop] == B_FALSE) 4766 (void) nvlist_remove(zhp->zfs_props, 4767 nvpair_name(curr), nvpair_type(curr)); 4768 curr = next; 4769 } 4770 } 4771 4772 static int 4773 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4774 zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4775 { 4776 zfs_cmd_t zc = {"\0"}; 4777 nvlist_t *nvlist = NULL; 4778 int error; 4779 4780 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4781 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4782 zc.zc_cookie = (uint64_t)cmd; 4783 4784 if (cmd == ZFS_SMB_ACL_RENAME) { 4785 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4786 (void) no_memory(hdl); 4787 return (0); 4788 } 4789 } 4790 4791 switch (cmd) { 4792 case ZFS_SMB_ACL_ADD: 4793 case ZFS_SMB_ACL_REMOVE: 4794 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4795 break; 4796 case ZFS_SMB_ACL_RENAME: 4797 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4798 resource1) != 0) { 4799 (void) no_memory(hdl); 4800 return (-1); 4801 } 4802 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4803 resource2) != 0) { 4804 (void) no_memory(hdl); 4805 return (-1); 4806 } 4807 zcmd_write_src_nvlist(hdl, &zc, nvlist); 4808 break; 4809 case ZFS_SMB_ACL_PURGE: 4810 break; 4811 default: 4812 return (-1); 4813 } 4814 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4815 nvlist_free(nvlist); 4816 return (error); 4817 } 4818 4819 int 4820 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4821 char *path, char *resource) 4822 { 4823 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4824 resource, NULL)); 4825 } 4826 4827 int 4828 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4829 char *path, char *resource) 4830 { 4831 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4832 resource, NULL)); 4833 } 4834 4835 int 4836 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4837 { 4838 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4839 NULL, NULL)); 4840 } 4841 4842 int 4843 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4844 char *oldname, char *newname) 4845 { 4846 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4847 oldname, newname)); 4848 } 4849 4850 int 4851 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 4852 zfs_userspace_cb_t func, void *arg) 4853 { 4854 zfs_cmd_t zc = {"\0"}; 4855 zfs_useracct_t buf[100]; 4856 libzfs_handle_t *hdl = zhp->zfs_hdl; 4857 int ret; 4858 4859 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4860 4861 zc.zc_objset_type = type; 4862 zc.zc_nvlist_dst = (uintptr_t)buf; 4863 4864 for (;;) { 4865 zfs_useracct_t *zua = buf; 4866 4867 zc.zc_nvlist_dst_size = sizeof (buf); 4868 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 4869 if ((errno == ENOTSUP && 4870 (type == ZFS_PROP_USEROBJUSED || 4871 type == ZFS_PROP_GROUPOBJUSED || 4872 type == ZFS_PROP_USEROBJQUOTA || 4873 type == ZFS_PROP_GROUPOBJQUOTA || 4874 type == ZFS_PROP_PROJECTOBJUSED || 4875 type == ZFS_PROP_PROJECTOBJQUOTA || 4876 type == ZFS_PROP_PROJECTUSED || 4877 type == ZFS_PROP_PROJECTQUOTA))) 4878 break; 4879 4880 return (zfs_standard_error_fmt(hdl, errno, 4881 dgettext(TEXT_DOMAIN, 4882 "cannot get used/quota for %s"), zc.zc_name)); 4883 } 4884 if (zc.zc_nvlist_dst_size == 0) 4885 break; 4886 4887 while (zc.zc_nvlist_dst_size > 0) { 4888 if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 4889 zua->zu_space)) != 0) 4890 return (ret); 4891 zua++; 4892 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 4893 } 4894 } 4895 4896 return (0); 4897 } 4898 4899 struct holdarg { 4900 nvlist_t *nvl; 4901 const char *snapname; 4902 const char *tag; 4903 boolean_t recursive; 4904 int error; 4905 }; 4906 4907 static int 4908 zfs_hold_one(zfs_handle_t *zhp, void *arg) 4909 { 4910 struct holdarg *ha = arg; 4911 char name[ZFS_MAX_DATASET_NAME_LEN]; 4912 int rv = 0; 4913 4914 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, 4915 ha->snapname) >= sizeof (name)) 4916 return (EINVAL); 4917 4918 if (lzc_exists(name)) 4919 fnvlist_add_string(ha->nvl, name, ha->tag); 4920 4921 if (ha->recursive) 4922 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); 4923 zfs_close(zhp); 4924 return (rv); 4925 } 4926 4927 int 4928 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4929 boolean_t recursive, int cleanup_fd) 4930 { 4931 int ret; 4932 struct holdarg ha; 4933 4934 ha.nvl = fnvlist_alloc(); 4935 ha.snapname = snapname; 4936 ha.tag = tag; 4937 ha.recursive = recursive; 4938 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4939 4940 if (nvlist_empty(ha.nvl)) { 4941 char errbuf[ERRBUFLEN]; 4942 4943 fnvlist_free(ha.nvl); 4944 ret = ENOENT; 4945 (void) snprintf(errbuf, sizeof (errbuf), 4946 dgettext(TEXT_DOMAIN, 4947 "cannot hold snapshot '%s@%s'"), 4948 zhp->zfs_name, snapname); 4949 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 4950 return (ret); 4951 } 4952 4953 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 4954 fnvlist_free(ha.nvl); 4955 4956 return (ret); 4957 } 4958 4959 int 4960 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 4961 { 4962 int ret; 4963 nvlist_t *errors; 4964 libzfs_handle_t *hdl = zhp->zfs_hdl; 4965 char errbuf[ERRBUFLEN]; 4966 nvpair_t *elem; 4967 4968 errors = NULL; 4969 ret = lzc_hold(holds, cleanup_fd, &errors); 4970 4971 if (ret == 0) { 4972 /* There may be errors even in the success case. */ 4973 fnvlist_free(errors); 4974 return (0); 4975 } 4976 4977 if (nvlist_empty(errors)) { 4978 /* no hold-specific errors */ 4979 (void) snprintf(errbuf, sizeof (errbuf), 4980 dgettext(TEXT_DOMAIN, "cannot hold")); 4981 switch (ret) { 4982 case ENOTSUP: 4983 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4984 "pool must be upgraded")); 4985 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4986 break; 4987 case EINVAL: 4988 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4989 break; 4990 default: 4991 (void) zfs_standard_error(hdl, ret, errbuf); 4992 } 4993 } 4994 4995 for (elem = nvlist_next_nvpair(errors, NULL); 4996 elem != NULL; 4997 elem = nvlist_next_nvpair(errors, elem)) { 4998 (void) snprintf(errbuf, sizeof (errbuf), 4999 dgettext(TEXT_DOMAIN, 5000 "cannot hold snapshot '%s'"), nvpair_name(elem)); 5001 switch (fnvpair_value_int32(elem)) { 5002 case E2BIG: 5003 /* 5004 * Temporary tags wind up having the ds object id 5005 * prepended. So even if we passed the length check 5006 * above, it's still possible for the tag to wind 5007 * up being slightly too long. 5008 */ 5009 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 5010 break; 5011 case EINVAL: 5012 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5013 break; 5014 case EEXIST: 5015 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 5016 break; 5017 default: 5018 (void) zfs_standard_error(hdl, 5019 fnvpair_value_int32(elem), errbuf); 5020 } 5021 } 5022 5023 fnvlist_free(errors); 5024 return (ret); 5025 } 5026 5027 static int 5028 zfs_release_one(zfs_handle_t *zhp, void *arg) 5029 { 5030 struct holdarg *ha = arg; 5031 char name[ZFS_MAX_DATASET_NAME_LEN]; 5032 int rv = 0; 5033 nvlist_t *existing_holds; 5034 5035 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, 5036 ha->snapname) >= sizeof (name)) { 5037 ha->error = EINVAL; 5038 rv = EINVAL; 5039 } 5040 5041 if (lzc_get_holds(name, &existing_holds) != 0) { 5042 ha->error = ENOENT; 5043 } else if (!nvlist_exists(existing_holds, ha->tag)) { 5044 ha->error = ESRCH; 5045 } else { 5046 nvlist_t *torelease = fnvlist_alloc(); 5047 fnvlist_add_boolean(torelease, ha->tag); 5048 fnvlist_add_nvlist(ha->nvl, name, torelease); 5049 fnvlist_free(torelease); 5050 } 5051 5052 if (ha->recursive) 5053 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); 5054 zfs_close(zhp); 5055 return (rv); 5056 } 5057 5058 int 5059 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 5060 boolean_t recursive) 5061 { 5062 int ret; 5063 struct holdarg ha; 5064 nvlist_t *errors = NULL; 5065 nvpair_t *elem; 5066 libzfs_handle_t *hdl = zhp->zfs_hdl; 5067 char errbuf[ERRBUFLEN]; 5068 5069 ha.nvl = fnvlist_alloc(); 5070 ha.snapname = snapname; 5071 ha.tag = tag; 5072 ha.recursive = recursive; 5073 ha.error = 0; 5074 (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 5075 5076 if (nvlist_empty(ha.nvl)) { 5077 fnvlist_free(ha.nvl); 5078 ret = ha.error; 5079 (void) snprintf(errbuf, sizeof (errbuf), 5080 dgettext(TEXT_DOMAIN, 5081 "cannot release hold from snapshot '%s@%s'"), 5082 zhp->zfs_name, snapname); 5083 if (ret == ESRCH) { 5084 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 5085 } else { 5086 (void) zfs_standard_error(hdl, ret, errbuf); 5087 } 5088 return (ret); 5089 } 5090 5091 ret = lzc_release(ha.nvl, &errors); 5092 fnvlist_free(ha.nvl); 5093 5094 if (ret == 0) { 5095 /* There may be errors even in the success case. */ 5096 fnvlist_free(errors); 5097 return (0); 5098 } 5099 5100 if (nvlist_empty(errors)) { 5101 /* no hold-specific errors */ 5102 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 5103 "cannot release")); 5104 switch (errno) { 5105 case ENOTSUP: 5106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5107 "pool must be upgraded")); 5108 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 5109 break; 5110 default: 5111 (void) zfs_standard_error(hdl, errno, errbuf); 5112 } 5113 } 5114 5115 for (elem = nvlist_next_nvpair(errors, NULL); 5116 elem != NULL; 5117 elem = nvlist_next_nvpair(errors, elem)) { 5118 (void) snprintf(errbuf, sizeof (errbuf), 5119 dgettext(TEXT_DOMAIN, 5120 "cannot release hold from snapshot '%s'"), 5121 nvpair_name(elem)); 5122 switch (fnvpair_value_int32(elem)) { 5123 case ESRCH: 5124 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 5125 break; 5126 case EINVAL: 5127 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5128 break; 5129 default: 5130 (void) zfs_standard_error(hdl, 5131 fnvpair_value_int32(elem), errbuf); 5132 } 5133 } 5134 5135 fnvlist_free(errors); 5136 return (ret); 5137 } 5138 5139 int 5140 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 5141 { 5142 zfs_cmd_t zc = {"\0"}; 5143 libzfs_handle_t *hdl = zhp->zfs_hdl; 5144 int nvsz = 2048; 5145 void *nvbuf; 5146 int err = 0; 5147 char errbuf[ERRBUFLEN]; 5148 5149 assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 5150 zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 5151 5152 tryagain: 5153 5154 nvbuf = malloc(nvsz); 5155 if (nvbuf == NULL) { 5156 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 5157 goto out; 5158 } 5159 5160 zc.zc_nvlist_dst_size = nvsz; 5161 zc.zc_nvlist_dst = (uintptr_t)nvbuf; 5162 5163 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 5164 5165 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) { 5166 (void) snprintf(errbuf, sizeof (errbuf), 5167 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 5168 zc.zc_name); 5169 switch (errno) { 5170 case ENOMEM: 5171 free(nvbuf); 5172 nvsz = zc.zc_nvlist_dst_size; 5173 goto tryagain; 5174 5175 case ENOTSUP: 5176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5177 "pool must be upgraded")); 5178 err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 5179 break; 5180 case EINVAL: 5181 err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 5182 break; 5183 case ENOENT: 5184 err = zfs_error(hdl, EZFS_NOENT, errbuf); 5185 break; 5186 default: 5187 err = zfs_standard_error(hdl, errno, errbuf); 5188 break; 5189 } 5190 } else { 5191 /* success */ 5192 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 5193 if (rc) { 5194 err = zfs_standard_error_fmt(hdl, rc, dgettext( 5195 TEXT_DOMAIN, "cannot get permissions on '%s'"), 5196 zc.zc_name); 5197 } 5198 } 5199 5200 free(nvbuf); 5201 out: 5202 return (err); 5203 } 5204 5205 int 5206 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 5207 { 5208 zfs_cmd_t zc = {"\0"}; 5209 libzfs_handle_t *hdl = zhp->zfs_hdl; 5210 char *nvbuf; 5211 char errbuf[ERRBUFLEN]; 5212 size_t nvsz; 5213 int err; 5214 5215 assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 5216 zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 5217 5218 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 5219 assert(err == 0); 5220 5221 nvbuf = malloc(nvsz); 5222 5223 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 5224 assert(err == 0); 5225 5226 zc.zc_nvlist_src_size = nvsz; 5227 zc.zc_nvlist_src = (uintptr_t)nvbuf; 5228 zc.zc_perm_action = un; 5229 5230 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 5231 5232 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 5233 (void) snprintf(errbuf, sizeof (errbuf), 5234 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 5235 zc.zc_name); 5236 switch (errno) { 5237 case ENOTSUP: 5238 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5239 "pool must be upgraded")); 5240 err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 5241 break; 5242 case EINVAL: 5243 err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 5244 break; 5245 case ENOENT: 5246 err = zfs_error(hdl, EZFS_NOENT, errbuf); 5247 break; 5248 default: 5249 err = zfs_standard_error(hdl, errno, errbuf); 5250 break; 5251 } 5252 } 5253 5254 free(nvbuf); 5255 5256 return (err); 5257 } 5258 5259 int 5260 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 5261 { 5262 int err; 5263 char errbuf[ERRBUFLEN]; 5264 5265 err = lzc_get_holds(zhp->zfs_name, nvl); 5266 5267 if (err != 0) { 5268 libzfs_handle_t *hdl = zhp->zfs_hdl; 5269 5270 (void) snprintf(errbuf, sizeof (errbuf), 5271 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 5272 zhp->zfs_name); 5273 switch (err) { 5274 case ENOTSUP: 5275 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5276 "pool must be upgraded")); 5277 err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 5278 break; 5279 case EINVAL: 5280 err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 5281 break; 5282 case ENOENT: 5283 err = zfs_error(hdl, EZFS_NOENT, errbuf); 5284 break; 5285 default: 5286 err = zfs_standard_error(hdl, errno, errbuf); 5287 break; 5288 } 5289 } 5290 5291 return (err); 5292 } 5293 5294 /* 5295 * The theory of raidz space accounting 5296 * 5297 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block 5298 * will "reference" 128KB, even though it allocates more than that, to store the 5299 * parity information (and perhaps skip sectors). This concept of the 5300 * "referenced" (and other DMU space accounting) being lower than the allocated 5301 * space by a constant factor is called "raidz deflation." 5302 * 5303 * As mentioned above, the constant factor for raidz deflation assumes a 128KB 5304 * block size. However, zvols typically have a much smaller block size (default 5305 * 8KB). These smaller blocks may require proportionally much more parity 5306 * information (and perhaps skip sectors). In this case, the change to the 5307 * "referenced" property may be much more than the logical block size. 5308 * 5309 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written 5310 * as follows. 5311 * 5312 * +-------+-------+-------+-------+-------+ 5313 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5314 * +-------+-------+-------+-------+-------+ 5315 * | P0 | D0 | D8 | D16 | D24 | 5316 * | P1 | D1 | D9 | D17 | D25 | 5317 * | P2 | D2 | D10 | D18 | D26 | 5318 * | P3 | D3 | D11 | D19 | D27 | 5319 * | P4 | D4 | D12 | D20 | D28 | 5320 * | P5 | D5 | D13 | D21 | D29 | 5321 * | P6 | D6 | D14 | D22 | D30 | 5322 * | P7 | D7 | D15 | D23 | D31 | 5323 * +-------+-------+-------+-------+-------+ 5324 * 5325 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data 5326 * sectors. The dataset's referenced will increase by 128k and the pool's 5327 * allocated and free properties will be adjusted by 160k. 5328 * 5329 * A 4k block written to the same raidz vdev will require two 4k sectors. The 5330 * blank cells represent unallocated space. 5331 * 5332 * +-------+-------+-------+-------+-------+ 5333 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5334 * +-------+-------+-------+-------+-------+ 5335 * | P0 | D0 | | | | 5336 * +-------+-------+-------+-------+-------+ 5337 * 5338 * Above, notice that the 4k block required one sector for parity and another 5339 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated 5340 * and free properties will be adjusted by 8k. The dataset will not be charged 5341 * 8k. Rather, it will be charged a value that is scaled according to the 5342 * overhead of the 128k block on the same vdev. This 8k allocation will be 5343 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as 5344 * calculated in the 128k block example above. 5345 * 5346 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That 5347 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2 5348 * allocations are a multiple of 3 sectors, and raidz3 allocations are a 5349 * multiple of of 4 sectors. When a block does not fill the required number of 5350 * sectors, skip blocks (sectors) are used. 5351 * 5352 * An 8k block being written to a raidz vdev may be written as follows: 5353 * 5354 * +-------+-------+-------+-------+-------+ 5355 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5356 * +-------+-------+-------+-------+-------+ 5357 * | P0 | D0 | D1 | S0 | | 5358 * +-------+-------+-------+-------+-------+ 5359 * 5360 * In order to maintain the nparity+1 allocation size, a skip block (S0) was 5361 * added. For this 8k block, the pool's allocated and free properties are 5362 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k / 5363 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in 5364 * the 128k block example above. 5365 * 5366 * The situation is slightly different for dRAID since the minimum allocation 5367 * size is the full group width. The same 8K block above would be written as 5368 * follows in a dRAID group: 5369 * 5370 * +-------+-------+-------+-------+-------+ 5371 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5372 * +-------+-------+-------+-------+-------+ 5373 * | P0 | D0 | D1 | S0 | S1 | 5374 * +-------+-------+-------+-------+-------+ 5375 * 5376 * Compression may lead to a variety of block sizes being written for the same 5377 * volume or file. There is no clear way to reserve just the amount of space 5378 * that will be required, so the worst case (no compression) is assumed. 5379 * Note that metadata blocks will typically be compressed, so the reservation 5380 * size returned by zvol_volsize_to_reservation() will generally be slightly 5381 * larger than the maximum that the volume can reference. 5382 */ 5383 5384 /* 5385 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the 5386 * amount of space (in bytes) that will be allocated for the specified block 5387 * size. Note that the "referenced" space accounted will be less than this, but 5388 * not necessarily equal to "blksize", due to RAIDZ deflation. 5389 */ 5390 static uint64_t 5391 vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift, 5392 uint64_t blksize) 5393 { 5394 uint64_t asize, ndata; 5395 5396 ASSERT3U(ndisks, >, nparity); 5397 ndata = ndisks - nparity; 5398 asize = ((blksize - 1) >> ashift) + 1; 5399 asize += nparity * ((asize + ndata - 1) / ndata); 5400 asize = roundup(asize, nparity + 1) << ashift; 5401 5402 return (asize); 5403 } 5404 5405 /* 5406 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the 5407 * amount of space (in bytes) that will be allocated for the specified block 5408 * size. 5409 */ 5410 static uint64_t 5411 vdev_draid_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift, 5412 uint64_t blksize) 5413 { 5414 ASSERT3U(ndisks, >, nparity); 5415 uint64_t ndata = ndisks - nparity; 5416 uint64_t rows = ((blksize - 1) / (ndata << ashift)) + 1; 5417 uint64_t asize = (rows * ndisks) << ashift; 5418 5419 return (asize); 5420 } 5421 5422 /* 5423 * Determine how much space will be allocated if it lands on the most space- 5424 * inefficient top-level vdev. Returns the size in bytes required to store one 5425 * copy of the volume data. See theory comment above. 5426 */ 5427 static uint64_t 5428 volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize) 5429 { 5430 nvlist_t *config, *tree, **vdevs; 5431 uint_t nvdevs; 5432 uint64_t ret = 0; 5433 5434 config = zpool_get_config(zhp, NULL); 5435 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 || 5436 nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, 5437 &vdevs, &nvdevs) != 0) { 5438 return (nblocks * blksize); 5439 } 5440 5441 for (int v = 0; v < nvdevs; v++) { 5442 char *type; 5443 uint64_t nparity, ashift, asize, tsize; 5444 uint64_t volsize; 5445 5446 if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE, 5447 &type) != 0) 5448 continue; 5449 5450 if (strcmp(type, VDEV_TYPE_RAIDZ) != 0 && 5451 strcmp(type, VDEV_TYPE_DRAID) != 0) 5452 continue; 5453 5454 if (nvlist_lookup_uint64(vdevs[v], 5455 ZPOOL_CONFIG_NPARITY, &nparity) != 0) 5456 continue; 5457 5458 if (nvlist_lookup_uint64(vdevs[v], 5459 ZPOOL_CONFIG_ASHIFT, &ashift) != 0) 5460 continue; 5461 5462 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) { 5463 nvlist_t **disks; 5464 uint_t ndisks; 5465 5466 if (nvlist_lookup_nvlist_array(vdevs[v], 5467 ZPOOL_CONFIG_CHILDREN, &disks, &ndisks) != 0) 5468 continue; 5469 5470 /* allocation size for the "typical" 128k block */ 5471 tsize = vdev_raidz_asize(ndisks, nparity, ashift, 5472 SPA_OLD_MAXBLOCKSIZE); 5473 5474 /* allocation size for the blksize block */ 5475 asize = vdev_raidz_asize(ndisks, nparity, ashift, 5476 blksize); 5477 } else { 5478 uint64_t ndata; 5479 5480 if (nvlist_lookup_uint64(vdevs[v], 5481 ZPOOL_CONFIG_DRAID_NDATA, &ndata) != 0) 5482 continue; 5483 5484 /* allocation size for the "typical" 128k block */ 5485 tsize = vdev_draid_asize(ndata + nparity, nparity, 5486 ashift, SPA_OLD_MAXBLOCKSIZE); 5487 5488 /* allocation size for the blksize block */ 5489 asize = vdev_draid_asize(ndata + nparity, nparity, 5490 ashift, blksize); 5491 } 5492 5493 /* 5494 * Scale this size down as a ratio of 128k / tsize. 5495 * See theory statement above. 5496 */ 5497 volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize; 5498 if (volsize > ret) { 5499 ret = volsize; 5500 } 5501 } 5502 5503 if (ret == 0) { 5504 ret = nblocks * blksize; 5505 } 5506 5507 return (ret); 5508 } 5509 5510 /* 5511 * Convert the zvol's volume size to an appropriate reservation. See theory 5512 * comment above. 5513 * 5514 * Note: If this routine is updated, it is necessary to update the ZFS test 5515 * suite's shell version in reservation.shlib. 5516 */ 5517 uint64_t 5518 zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize, 5519 nvlist_t *props) 5520 { 5521 uint64_t numdb; 5522 uint64_t nblocks, volblocksize; 5523 int ncopies; 5524 char *strval; 5525 5526 if (nvlist_lookup_string(props, 5527 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 5528 ncopies = atoi(strval); 5529 else 5530 ncopies = 1; 5531 if (nvlist_lookup_uint64(props, 5532 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 5533 &volblocksize) != 0) 5534 volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 5535 5536 nblocks = volsize / volblocksize; 5537 /* 5538 * Metadata defaults to using 128k blocks, not volblocksize blocks. For 5539 * this reason, only the data blocks are scaled based on vdev config. 5540 */ 5541 volsize = volsize_from_vdevs(zph, nblocks, volblocksize); 5542 5543 /* start with metadnode L0-L6 */ 5544 numdb = 7; 5545 /* calculate number of indirects */ 5546 while (nblocks > 1) { 5547 nblocks += DNODES_PER_LEVEL - 1; 5548 nblocks /= DNODES_PER_LEVEL; 5549 numdb += nblocks; 5550 } 5551 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 5552 volsize *= ncopies; 5553 /* 5554 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 5555 * compressed, but in practice they compress down to about 5556 * 1100 bytes 5557 */ 5558 numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 5559 volsize += numdb; 5560 return (volsize); 5561 } 5562 5563 /* 5564 * Wait for the given activity and return the status of the wait (whether or not 5565 * any waiting was done) in the 'waited' parameter. Non-existent fses are 5566 * reported via the 'missing' parameter, rather than by printing an error 5567 * message. This is convenient when this function is called in a loop over a 5568 * long period of time (as it is, for example, by zfs's wait cmd). In that 5569 * scenario, a fs being exported or destroyed should be considered a normal 5570 * event, so we don't want to print an error when we find that the fs doesn't 5571 * exist. 5572 */ 5573 int 5574 zfs_wait_status(zfs_handle_t *zhp, zfs_wait_activity_t activity, 5575 boolean_t *missing, boolean_t *waited) 5576 { 5577 int error = lzc_wait_fs(zhp->zfs_name, activity, waited); 5578 *missing = (error == ENOENT); 5579 if (*missing) 5580 return (0); 5581 5582 if (error != 0) { 5583 (void) zfs_standard_error_fmt(zhp->zfs_hdl, error, 5584 dgettext(TEXT_DOMAIN, "error waiting in fs '%s'"), 5585 zhp->zfs_name); 5586 } 5587 5588 return (error); 5589 } 5590