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