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