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 = 0; 1793 nvpair_t *elem; 1794 1795 (void) snprintf(errbuf, sizeof (errbuf), 1796 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1797 zhp->zfs_name); 1798 1799 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, 1800 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, 1801 B_FALSE, errbuf)) == NULL) 1802 goto error; 1803 1804 /* 1805 * We have to check for any extra properties which need to be added 1806 * before computing the length of the nvlist. 1807 */ 1808 for (elem = nvlist_next_nvpair(nvl, NULL); 1809 elem != NULL; 1810 elem = nvlist_next_nvpair(nvl, elem)) { 1811 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && 1812 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { 1813 goto error; 1814 } 1815 } 1816 1817 if (added_resv != 1 && 1818 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) { 1819 goto error; 1820 } 1821 1822 /* 1823 * Check how many properties we're setting and allocate an array to 1824 * store changelist pointers for postfix(). 1825 */ 1826 for (elem = nvlist_next_nvpair(nvl, NULL); 1827 elem != NULL; 1828 elem = nvlist_next_nvpair(nvl, elem)) 1829 nvl_len++; 1830 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) 1831 goto error; 1832 1833 cl_idx = 0; 1834 for (elem = nvlist_next_nvpair(nvl, NULL); 1835 elem != NULL; 1836 elem = nvlist_next_nvpair(nvl, elem)) { 1837 1838 prop = zfs_name_to_prop(nvpair_name(elem)); 1839 1840 assert(cl_idx < nvl_len); 1841 /* 1842 * We don't want to unmount & remount the dataset when changing 1843 * its canmount property to 'on' or 'noauto'. We only use 1844 * the changelist logic to unmount when setting canmount=off. 1845 */ 1846 if (prop != ZFS_PROP_CANMOUNT || 1847 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF && 1848 zfs_is_mounted(zhp, NULL))) { 1849 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); 1850 if (cls[cl_idx] == NULL) 1851 goto error; 1852 } 1853 1854 if (prop == ZFS_PROP_MOUNTPOINT && 1855 changelist_haszonedchild(cls[cl_idx])) { 1856 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1857 "child dataset with inherited mountpoint is used " 1858 "in a non-global zone")); 1859 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1860 goto error; 1861 } 1862 1863 if (cls[cl_idx] != NULL && 1864 (ret = changelist_prefix(cls[cl_idx])) != 0) 1865 goto error; 1866 1867 cl_idx++; 1868 } 1869 assert(cl_idx == nvl_len); 1870 1871 /* 1872 * Execute the corresponding ioctl() to set this list of properties. 1873 */ 1874 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1875 1876 zcmd_write_src_nvlist(hdl, &zc, nvl); 1877 zcmd_alloc_dst_nvlist(hdl, &zc, 0); 1878 1879 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1880 1881 if (ret != 0) { 1882 if (zc.zc_nvlist_dst_filled == B_FALSE) { 1883 (void) zfs_standard_error(hdl, errno, errbuf); 1884 goto error; 1885 } 1886 1887 /* Get the list of unset properties back and report them. */ 1888 nvlist_t *errorprops = NULL; 1889 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) 1890 goto error; 1891 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL); 1892 elem != NULL; 1893 elem = nvlist_next_nvpair(errorprops, elem)) { 1894 prop = zfs_name_to_prop(nvpair_name(elem)); 1895 zfs_setprop_error(hdl, prop, errno, errbuf); 1896 } 1897 nvlist_free(errorprops); 1898 1899 if (added_resv && errno == ENOSPC) { 1900 /* clean up the volsize property we tried to set */ 1901 uint64_t old_volsize = zfs_prop_get_int(zhp, 1902 ZFS_PROP_VOLSIZE); 1903 nvlist_free(nvl); 1904 nvl = NULL; 1905 zcmd_free_nvlists(&zc); 1906 1907 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 1908 goto error; 1909 if (nvlist_add_uint64(nvl, 1910 zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1911 old_volsize) != 0) 1912 goto error; 1913 zcmd_write_src_nvlist(hdl, &zc, nvl); 1914 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1915 } 1916 } else { 1917 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 1918 if (cls[cl_idx] != NULL) { 1919 int clp_err = changelist_postfix(cls[cl_idx]); 1920 if (clp_err != 0) 1921 ret = clp_err; 1922 } 1923 } 1924 1925 if (ret == 0) { 1926 /* 1927 * Refresh the statistics so the new property 1928 * value is reflected. 1929 */ 1930 (void) get_stats(zhp); 1931 1932 /* 1933 * Remount the filesystem to propagate the change 1934 * if one of the options handled by the generic 1935 * Linux namespace layer has been modified. 1936 */ 1937 if (zfs_is_namespace_prop(prop) && 1938 zfs_is_mounted(zhp, NULL)) 1939 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0); 1940 } 1941 } 1942 1943 error: 1944 nvlist_free(nvl); 1945 zcmd_free_nvlists(&zc); 1946 if (cls != NULL) { 1947 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 1948 if (cls[cl_idx] != NULL) 1949 changelist_free(cls[cl_idx]); 1950 } 1951 free(cls); 1952 } 1953 return (ret); 1954 } 1955 1956 /* 1957 * Given a property, inherit the value from the parent dataset, or if received 1958 * is TRUE, revert to the received value, if any. 1959 */ 1960 int 1961 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1962 { 1963 zfs_cmd_t zc = {"\0"}; 1964 int ret; 1965 prop_changelist_t *cl; 1966 libzfs_handle_t *hdl = zhp->zfs_hdl; 1967 char errbuf[ERRBUFLEN]; 1968 zfs_prop_t prop; 1969 1970 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1971 "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1972 1973 zc.zc_cookie = received; 1974 if ((prop = zfs_name_to_prop(propname)) == ZPROP_USERPROP) { 1975 /* 1976 * For user properties, the amount of work we have to do is very 1977 * small, so just do it here. 1978 */ 1979 if (!zfs_prop_user(propname)) { 1980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1981 "invalid property")); 1982 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1983 } 1984 1985 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1986 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1987 1988 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1989 return (zfs_standard_error(hdl, errno, errbuf)); 1990 1991 (void) get_stats(zhp); 1992 return (0); 1993 } 1994 1995 /* 1996 * Verify that this property is inheritable. 1997 */ 1998 if (zfs_prop_readonly(prop)) 1999 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 2000 2001 if (!zfs_prop_inheritable(prop) && !received) 2002 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2003 2004 /* 2005 * Check to see if the value applies to this type 2006 */ 2007 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) 2008 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2009 2010 /* 2011 * Normalize the name, to get rid of shorthand abbreviations. 2012 */ 2013 propname = zfs_prop_to_name(prop); 2014 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2015 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2016 2017 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2018 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 2019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2020 "dataset is used in a non-global zone")); 2021 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2022 } 2023 2024 /* 2025 * Determine datasets which will be affected by this change, if any. 2026 */ 2027 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 2028 return (-1); 2029 2030 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 2031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2032 "child dataset with inherited mountpoint is used " 2033 "in a non-global zone")); 2034 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2035 goto error; 2036 } 2037 2038 if ((ret = changelist_prefix(cl)) != 0) 2039 goto error; 2040 2041 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) { 2042 changelist_free(cl); 2043 return (zfs_standard_error(hdl, errno, errbuf)); 2044 } else { 2045 2046 if ((ret = changelist_postfix(cl)) != 0) 2047 goto error; 2048 2049 /* 2050 * Refresh the statistics so the new property is reflected. 2051 */ 2052 (void) get_stats(zhp); 2053 2054 /* 2055 * Remount the filesystem to propagate the change 2056 * if one of the options handled by the generic 2057 * Linux namespace layer has been modified. 2058 */ 2059 if (zfs_is_namespace_prop(prop) && 2060 zfs_is_mounted(zhp, NULL)) 2061 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0); 2062 } 2063 2064 error: 2065 changelist_free(cl); 2066 return (ret); 2067 } 2068 2069 /* 2070 * True DSL properties are stored in an nvlist. The following two functions 2071 * extract them appropriately. 2072 */ 2073 uint64_t 2074 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, const char **source) 2075 { 2076 nvlist_t *nv; 2077 uint64_t value; 2078 2079 *source = NULL; 2080 if (nvlist_lookup_nvlist(zhp->zfs_props, 2081 zfs_prop_to_name(prop), &nv) == 0) { 2082 value = fnvlist_lookup_uint64(nv, ZPROP_VALUE); 2083 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 2084 } else { 2085 verify(!zhp->zfs_props_table || 2086 zhp->zfs_props_table[prop] == B_TRUE); 2087 value = zfs_prop_default_numeric(prop); 2088 *source = ""; 2089 } 2090 2091 return (value); 2092 } 2093 2094 static const char * 2095 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, const char **source) 2096 { 2097 nvlist_t *nv; 2098 const char *value; 2099 2100 *source = NULL; 2101 if (nvlist_lookup_nvlist(zhp->zfs_props, 2102 zfs_prop_to_name(prop), &nv) == 0) { 2103 value = fnvlist_lookup_string(nv, ZPROP_VALUE); 2104 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 2105 } else { 2106 verify(!zhp->zfs_props_table || 2107 zhp->zfs_props_table[prop] == B_TRUE); 2108 value = zfs_prop_default_string(prop); 2109 *source = ""; 2110 } 2111 2112 return (value); 2113 } 2114 2115 static boolean_t 2116 zfs_is_recvd_props_mode(zfs_handle_t *zhp) 2117 { 2118 return (zhp->zfs_props != NULL && 2119 zhp->zfs_props == zhp->zfs_recvd_props); 2120 } 2121 2122 static void 2123 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uintptr_t *cookie) 2124 { 2125 *cookie = (uintptr_t)zhp->zfs_props; 2126 zhp->zfs_props = zhp->zfs_recvd_props; 2127 } 2128 2129 static void 2130 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uintptr_t *cookie) 2131 { 2132 zhp->zfs_props = (nvlist_t *)*cookie; 2133 *cookie = 0; 2134 } 2135 2136 /* 2137 * Internal function for getting a numeric property. Both zfs_prop_get() and 2138 * zfs_prop_get_int() are built using this interface. 2139 * 2140 * Certain properties can be overridden using 'mount -o'. In this case, scan 2141 * the contents of the /proc/self/mounts entry, searching for the 2142 * appropriate options. If they differ from the on-disk values, report the 2143 * current values and mark the source "temporary". 2144 */ 2145 static int 2146 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 2147 const char **source, uint64_t *val) 2148 { 2149 zfs_cmd_t zc = {"\0"}; 2150 nvlist_t *zplprops = NULL; 2151 struct mnttab mnt; 2152 const char *mntopt_on = NULL; 2153 const char *mntopt_off = NULL; 2154 boolean_t received = zfs_is_recvd_props_mode(zhp); 2155 2156 *source = NULL; 2157 2158 /* 2159 * If the property is being fetched for a snapshot, check whether 2160 * the property is valid for the snapshot's head dataset type. 2161 */ 2162 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT && 2163 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) { 2164 *val = zfs_prop_default_numeric(prop); 2165 return (-1); 2166 } 2167 2168 switch (prop) { 2169 case ZFS_PROP_ATIME: 2170 mntopt_on = MNTOPT_ATIME; 2171 mntopt_off = MNTOPT_NOATIME; 2172 break; 2173 2174 case ZFS_PROP_RELATIME: 2175 mntopt_on = MNTOPT_RELATIME; 2176 mntopt_off = MNTOPT_NORELATIME; 2177 break; 2178 2179 case ZFS_PROP_DEVICES: 2180 mntopt_on = MNTOPT_DEVICES; 2181 mntopt_off = MNTOPT_NODEVICES; 2182 break; 2183 2184 case ZFS_PROP_EXEC: 2185 mntopt_on = MNTOPT_EXEC; 2186 mntopt_off = MNTOPT_NOEXEC; 2187 break; 2188 2189 case ZFS_PROP_READONLY: 2190 mntopt_on = MNTOPT_RO; 2191 mntopt_off = MNTOPT_RW; 2192 break; 2193 2194 case ZFS_PROP_SETUID: 2195 mntopt_on = MNTOPT_SETUID; 2196 mntopt_off = MNTOPT_NOSETUID; 2197 break; 2198 2199 case ZFS_PROP_XATTR: 2200 mntopt_on = MNTOPT_XATTR; 2201 mntopt_off = MNTOPT_NOXATTR; 2202 break; 2203 2204 case ZFS_PROP_NBMAND: 2205 mntopt_on = MNTOPT_NBMAND; 2206 mntopt_off = MNTOPT_NONBMAND; 2207 break; 2208 2209 default: 2210 break; 2211 } 2212 2213 /* 2214 * Because looking up the mount options is potentially expensive 2215 * (iterating over all of /proc/self/mounts), we defer its 2216 * calculation until we're looking up a property which requires 2217 * its presence. 2218 */ 2219 if (!zhp->zfs_mntcheck && 2220 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2221 libzfs_handle_t *hdl = zhp->zfs_hdl; 2222 struct mnttab entry; 2223 2224 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) 2225 zhp->zfs_mntopts = zfs_strdup(hdl, 2226 entry.mnt_mntopts); 2227 2228 zhp->zfs_mntcheck = B_TRUE; 2229 } 2230 2231 if (zhp->zfs_mntopts == NULL) 2232 mnt.mnt_mntopts = (char *)""; 2233 else 2234 mnt.mnt_mntopts = zhp->zfs_mntopts; 2235 2236 switch (prop) { 2237 case ZFS_PROP_ATIME: 2238 case ZFS_PROP_RELATIME: 2239 case ZFS_PROP_DEVICES: 2240 case ZFS_PROP_EXEC: 2241 case ZFS_PROP_READONLY: 2242 case ZFS_PROP_SETUID: 2243 #ifndef __FreeBSD__ 2244 case ZFS_PROP_XATTR: 2245 #endif 2246 case ZFS_PROP_NBMAND: 2247 *val = getprop_uint64(zhp, prop, source); 2248 2249 if (received) 2250 break; 2251 2252 if (hasmntopt(&mnt, mntopt_on) && !*val) { 2253 *val = B_TRUE; 2254 if (src) 2255 *src = ZPROP_SRC_TEMPORARY; 2256 } else if (hasmntopt(&mnt, mntopt_off) && *val) { 2257 *val = B_FALSE; 2258 if (src) 2259 *src = ZPROP_SRC_TEMPORARY; 2260 } 2261 break; 2262 2263 case ZFS_PROP_CANMOUNT: 2264 case ZFS_PROP_VOLSIZE: 2265 case ZFS_PROP_QUOTA: 2266 case ZFS_PROP_REFQUOTA: 2267 case ZFS_PROP_RESERVATION: 2268 case ZFS_PROP_REFRESERVATION: 2269 case ZFS_PROP_FILESYSTEM_LIMIT: 2270 case ZFS_PROP_SNAPSHOT_LIMIT: 2271 case ZFS_PROP_FILESYSTEM_COUNT: 2272 case ZFS_PROP_SNAPSHOT_COUNT: 2273 *val = getprop_uint64(zhp, prop, source); 2274 2275 if (*source == NULL) { 2276 /* not default, must be local */ 2277 *source = zhp->zfs_name; 2278 } 2279 break; 2280 2281 case ZFS_PROP_MOUNTED: 2282 *val = (zhp->zfs_mntopts != NULL); 2283 break; 2284 2285 case ZFS_PROP_NUMCLONES: 2286 *val = zhp->zfs_dmustats.dds_num_clones; 2287 break; 2288 2289 case ZFS_PROP_VERSION: 2290 case ZFS_PROP_NORMALIZE: 2291 case ZFS_PROP_UTF8ONLY: 2292 case ZFS_PROP_CASE: 2293 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0); 2294 2295 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2296 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2297 zcmd_free_nvlists(&zc); 2298 if (prop == ZFS_PROP_VERSION && 2299 zhp->zfs_type == ZFS_TYPE_VOLUME) 2300 *val = zfs_prop_default_numeric(prop); 2301 return (-1); 2302 } 2303 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2304 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2305 val) != 0) { 2306 zcmd_free_nvlists(&zc); 2307 return (-1); 2308 } 2309 nvlist_free(zplprops); 2310 zcmd_free_nvlists(&zc); 2311 break; 2312 2313 case ZFS_PROP_INCONSISTENT: 2314 *val = zhp->zfs_dmustats.dds_inconsistent; 2315 break; 2316 2317 case ZFS_PROP_REDACTED: 2318 *val = zhp->zfs_dmustats.dds_redacted; 2319 break; 2320 2321 case ZFS_PROP_GUID: 2322 if (zhp->zfs_dmustats.dds_guid != 0) 2323 *val = zhp->zfs_dmustats.dds_guid; 2324 else 2325 *val = getprop_uint64(zhp, prop, source); 2326 break; 2327 2328 case ZFS_PROP_CREATETXG: 2329 /* 2330 * We can directly read createtxg property from zfs 2331 * handle for Filesystem, Snapshot and ZVOL types. 2332 */ 2333 if (((zhp->zfs_type == ZFS_TYPE_FILESYSTEM) || 2334 (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) || 2335 (zhp->zfs_type == ZFS_TYPE_VOLUME)) && 2336 (zhp->zfs_dmustats.dds_creation_txg != 0)) { 2337 *val = zhp->zfs_dmustats.dds_creation_txg; 2338 break; 2339 } else { 2340 *val = getprop_uint64(zhp, prop, source); 2341 } 2342 zfs_fallthrough; 2343 default: 2344 switch (zfs_prop_get_type(prop)) { 2345 case PROP_TYPE_NUMBER: 2346 case PROP_TYPE_INDEX: 2347 *val = getprop_uint64(zhp, prop, source); 2348 /* 2349 * If we tried to use a default value for a 2350 * readonly property, it means that it was not 2351 * present. Note this only applies to "truly" 2352 * readonly properties, not set-once properties 2353 * like volblocksize. 2354 */ 2355 if (zfs_prop_readonly(prop) && 2356 !zfs_prop_setonce(prop) && 2357 *source != NULL && (*source)[0] == '\0') { 2358 *source = NULL; 2359 return (-1); 2360 } 2361 break; 2362 2363 case PROP_TYPE_STRING: 2364 default: 2365 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2366 "cannot get non-numeric property")); 2367 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2368 dgettext(TEXT_DOMAIN, "internal error"))); 2369 } 2370 } 2371 2372 return (0); 2373 } 2374 2375 /* 2376 * Calculate the source type, given the raw source string. 2377 */ 2378 static void 2379 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, const char *source, 2380 char *statbuf, size_t statlen) 2381 { 2382 if (statbuf == NULL || 2383 srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) { 2384 return; 2385 } 2386 2387 if (source == NULL) { 2388 *srctype = ZPROP_SRC_NONE; 2389 } else if (source[0] == '\0') { 2390 *srctype = ZPROP_SRC_DEFAULT; 2391 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 2392 *srctype = ZPROP_SRC_RECEIVED; 2393 } else { 2394 if (strcmp(source, zhp->zfs_name) == 0) { 2395 *srctype = ZPROP_SRC_LOCAL; 2396 } else { 2397 (void) strlcpy(statbuf, source, statlen); 2398 *srctype = ZPROP_SRC_INHERITED; 2399 } 2400 } 2401 2402 } 2403 2404 int 2405 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 2406 size_t proplen, boolean_t literal) 2407 { 2408 zfs_prop_t prop; 2409 int err = 0; 2410 2411 if (zhp->zfs_recvd_props == NULL) 2412 if (get_recvd_props_ioctl(zhp) != 0) 2413 return (-1); 2414 2415 prop = zfs_name_to_prop(propname); 2416 2417 if (prop != ZPROP_USERPROP) { 2418 uintptr_t cookie; 2419 if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 2420 return (-1); 2421 zfs_set_recvd_props_mode(zhp, &cookie); 2422 err = zfs_prop_get(zhp, prop, propbuf, proplen, 2423 NULL, NULL, 0, literal); 2424 zfs_unset_recvd_props_mode(zhp, &cookie); 2425 } else { 2426 nvlist_t *propval; 2427 const char *recvdval; 2428 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 2429 propname, &propval) != 0) 2430 return (-1); 2431 recvdval = fnvlist_lookup_string(propval, ZPROP_VALUE); 2432 (void) strlcpy(propbuf, recvdval, proplen); 2433 } 2434 2435 return (err == 0 ? 0 : -1); 2436 } 2437 2438 static int 2439 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 2440 { 2441 nvlist_t *value; 2442 nvpair_t *pair; 2443 2444 value = zfs_get_clones_nvl(zhp); 2445 if (value == NULL || nvlist_empty(value)) 2446 return (-1); 2447 2448 propbuf[0] = '\0'; 2449 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 2450 pair = nvlist_next_nvpair(value, pair)) { 2451 if (propbuf[0] != '\0') 2452 (void) strlcat(propbuf, ",", proplen); 2453 (void) strlcat(propbuf, nvpair_name(pair), proplen); 2454 } 2455 2456 return (0); 2457 } 2458 2459 struct get_clones_arg { 2460 uint64_t numclones; 2461 nvlist_t *value; 2462 const char *origin; 2463 char buf[ZFS_MAX_DATASET_NAME_LEN]; 2464 }; 2465 2466 static int 2467 get_clones_cb(zfs_handle_t *zhp, void *arg) 2468 { 2469 struct get_clones_arg *gca = arg; 2470 2471 if (gca->numclones == 0) { 2472 zfs_close(zhp); 2473 return (0); 2474 } 2475 2476 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 2477 NULL, NULL, 0, B_TRUE) != 0) 2478 goto out; 2479 if (strcmp(gca->buf, gca->origin) == 0) { 2480 fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 2481 gca->numclones--; 2482 } 2483 2484 out: 2485 (void) zfs_iter_children_v2(zhp, 0, get_clones_cb, gca); 2486 zfs_close(zhp); 2487 return (0); 2488 } 2489 2490 nvlist_t * 2491 zfs_get_clones_nvl(zfs_handle_t *zhp) 2492 { 2493 nvlist_t *nv, *value; 2494 2495 if (nvlist_lookup_nvlist(zhp->zfs_props, 2496 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 2497 struct get_clones_arg gca; 2498 2499 /* 2500 * if this is a snapshot, then the kernel wasn't able 2501 * to get the clones. Do it by slowly iterating. 2502 */ 2503 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 2504 return (NULL); 2505 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 2506 return (NULL); 2507 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 2508 nvlist_free(nv); 2509 return (NULL); 2510 } 2511 2512 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 2513 gca.value = value; 2514 gca.origin = zhp->zfs_name; 2515 2516 if (gca.numclones != 0) { 2517 zfs_handle_t *root; 2518 char pool[ZFS_MAX_DATASET_NAME_LEN]; 2519 char *cp = pool; 2520 2521 /* get the pool name */ 2522 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 2523 (void) strsep(&cp, "/@"); 2524 root = zfs_open(zhp->zfs_hdl, pool, 2525 ZFS_TYPE_FILESYSTEM); 2526 if (root == NULL) { 2527 nvlist_free(nv); 2528 nvlist_free(value); 2529 return (NULL); 2530 } 2531 2532 (void) get_clones_cb(root, &gca); 2533 } 2534 2535 if (gca.numclones != 0 || 2536 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 2537 nvlist_add_nvlist(zhp->zfs_props, 2538 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 2539 nvlist_free(nv); 2540 nvlist_free(value); 2541 return (NULL); 2542 } 2543 nvlist_free(nv); 2544 nvlist_free(value); 2545 nv = fnvlist_lookup_nvlist(zhp->zfs_props, 2546 zfs_prop_to_name(ZFS_PROP_CLONES)); 2547 } 2548 2549 return (fnvlist_lookup_nvlist(nv, ZPROP_VALUE)); 2550 } 2551 2552 static int 2553 get_rsnaps_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 2554 { 2555 nvlist_t *value; 2556 uint64_t *snaps; 2557 uint_t nsnaps; 2558 2559 if (nvlist_lookup_nvlist(zhp->zfs_props, 2560 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &value) != 0) 2561 return (-1); 2562 if (nvlist_lookup_uint64_array(value, ZPROP_VALUE, &snaps, 2563 &nsnaps) != 0) 2564 return (-1); 2565 if (nsnaps == 0) { 2566 /* There's no redaction snapshots; pass a special value back */ 2567 (void) snprintf(propbuf, proplen, "none"); 2568 return (0); 2569 } 2570 propbuf[0] = '\0'; 2571 for (int i = 0; i < nsnaps; i++) { 2572 char buf[128]; 2573 if (propbuf[0] != '\0') 2574 (void) strlcat(propbuf, ",", proplen); 2575 (void) snprintf(buf, sizeof (buf), "%llu", 2576 (u_longlong_t)snaps[i]); 2577 (void) strlcat(propbuf, buf, proplen); 2578 } 2579 2580 return (0); 2581 } 2582 2583 /* 2584 * Accepts a property and value and checks that the value 2585 * matches the one found by the channel program. If they are 2586 * not equal, print both of them. 2587 */ 2588 static void 2589 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval, 2590 const char *strval) 2591 { 2592 if (!zhp->zfs_hdl->libzfs_prop_debug) 2593 return; 2594 int error; 2595 char *poolname = zhp->zpool_hdl->zpool_name; 2596 const char *prop_name = zfs_prop_to_name(prop); 2597 const char *program = 2598 "args = ...\n" 2599 "ds = args['dataset']\n" 2600 "prop = args['property']\n" 2601 "value, setpoint = zfs.get_prop(ds, prop)\n" 2602 "return {value=value, setpoint=setpoint}\n"; 2603 nvlist_t *outnvl; 2604 nvlist_t *retnvl; 2605 nvlist_t *argnvl = fnvlist_alloc(); 2606 2607 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name); 2608 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop)); 2609 2610 error = lzc_channel_program_nosync(poolname, program, 2611 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl); 2612 2613 if (error == 0) { 2614 retnvl = fnvlist_lookup_nvlist(outnvl, "return"); 2615 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) { 2616 int64_t ans; 2617 error = nvlist_lookup_int64(retnvl, "value", &ans); 2618 if (error != 0) { 2619 (void) fprintf(stderr, "%s: zcp check error: " 2620 "%u\n", prop_name, error); 2621 return; 2622 } 2623 if (ans != intval) { 2624 (void) fprintf(stderr, "%s: zfs found %llu, " 2625 "but zcp found %llu\n", prop_name, 2626 (u_longlong_t)intval, (u_longlong_t)ans); 2627 } 2628 } else { 2629 const char *str_ans; 2630 error = nvlist_lookup_string(retnvl, "value", &str_ans); 2631 if (error != 0) { 2632 (void) fprintf(stderr, "%s: zcp check error: " 2633 "%u\n", prop_name, error); 2634 return; 2635 } 2636 if (strcmp(strval, str_ans) != 0) { 2637 (void) fprintf(stderr, 2638 "%s: zfs found '%s', but zcp found '%s'\n", 2639 prop_name, strval, str_ans); 2640 } 2641 } 2642 } else { 2643 (void) fprintf(stderr, "%s: zcp check failed, channel program " 2644 "error: %u\n", prop_name, error); 2645 } 2646 nvlist_free(argnvl); 2647 nvlist_free(outnvl); 2648 } 2649 2650 /* 2651 * Retrieve a property from the given object. If 'literal' is specified, then 2652 * numbers are left as exact values. Otherwise, numbers are converted to a 2653 * human-readable form. 2654 * 2655 * Returns 0 on success, or -1 on error. 2656 */ 2657 int 2658 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2659 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2660 { 2661 const char *source = NULL; 2662 uint64_t val; 2663 const char *str; 2664 const char *strval; 2665 boolean_t received = zfs_is_recvd_props_mode(zhp); 2666 2667 /* 2668 * Check to see if this property applies to our object 2669 */ 2670 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) 2671 return (-1); 2672 2673 if (received && zfs_prop_readonly(prop)) 2674 return (-1); 2675 2676 if (src) 2677 *src = ZPROP_SRC_NONE; 2678 2679 switch (prop) { 2680 case ZFS_PROP_CREATION: 2681 /* 2682 * 'creation' is a time_t stored in the statistics. We convert 2683 * this into a string unless 'literal' is specified. 2684 */ 2685 { 2686 val = getprop_uint64(zhp, prop, &source); 2687 time_t time = (time_t)val; 2688 struct tm t; 2689 2690 if (literal || 2691 localtime_r(&time, &t) == NULL || 2692 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2693 &t) == 0) 2694 (void) snprintf(propbuf, proplen, "%llu", 2695 (u_longlong_t)val); 2696 } 2697 zcp_check(zhp, prop, val, NULL); 2698 break; 2699 2700 case ZFS_PROP_MOUNTPOINT: 2701 /* 2702 * Getting the precise mountpoint can be tricky. 2703 * 2704 * - for 'none' or 'legacy', return those values. 2705 * - for inherited mountpoints, we want to take everything 2706 * after our ancestor and append it to the inherited value. 2707 * 2708 * If the pool has an alternate root, we want to prepend that 2709 * root to any values we return. 2710 */ 2711 2712 str = getprop_string(zhp, prop, &source); 2713 2714 if (str[0] == '/') { 2715 char buf[MAXPATHLEN]; 2716 char *root = buf; 2717 const char *relpath; 2718 2719 /* 2720 * If we inherit the mountpoint, even from a dataset 2721 * with a received value, the source will be the path of 2722 * the dataset we inherit from. If source is 2723 * ZPROP_SOURCE_VAL_RECVD, the received value is not 2724 * inherited. 2725 */ 2726 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2727 relpath = ""; 2728 } else { 2729 relpath = zhp->zfs_name + strlen(source); 2730 if (relpath[0] == '/') 2731 relpath++; 2732 } 2733 2734 if ((zpool_get_prop(zhp->zpool_hdl, 2735 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2736 B_FALSE)) || (strcmp(root, "-") == 0)) 2737 root[0] = '\0'; 2738 /* 2739 * Special case an alternate root of '/'. This will 2740 * avoid having multiple leading slashes in the 2741 * mountpoint path. 2742 */ 2743 if (strcmp(root, "/") == 0) 2744 root++; 2745 2746 /* 2747 * If the mountpoint is '/' then skip over this 2748 * if we are obtaining either an alternate root or 2749 * an inherited mountpoint. 2750 */ 2751 if (str[1] == '\0' && (root[0] != '\0' || 2752 relpath[0] != '\0')) 2753 str++; 2754 2755 if (relpath[0] == '\0') 2756 (void) snprintf(propbuf, proplen, "%s%s", 2757 root, str); 2758 else 2759 (void) snprintf(propbuf, proplen, "%s%s%s%s", 2760 root, str, relpath[0] == '@' ? "" : "/", 2761 relpath); 2762 } else { 2763 /* 'legacy' or 'none' */ 2764 (void) strlcpy(propbuf, str, proplen); 2765 } 2766 zcp_check(zhp, prop, 0, propbuf); 2767 break; 2768 2769 case ZFS_PROP_ORIGIN: 2770 if (*zhp->zfs_dmustats.dds_origin != '\0') { 2771 str = (char *)&zhp->zfs_dmustats.dds_origin; 2772 } else { 2773 str = getprop_string(zhp, prop, &source); 2774 } 2775 if (str == NULL || *str == '\0') 2776 str = zfs_prop_default_string(prop); 2777 if (str == NULL) 2778 return (-1); 2779 (void) strlcpy(propbuf, str, proplen); 2780 zcp_check(zhp, prop, 0, str); 2781 break; 2782 2783 case ZFS_PROP_REDACT_SNAPS: 2784 if (get_rsnaps_string(zhp, propbuf, proplen) != 0) 2785 return (-1); 2786 break; 2787 2788 case ZFS_PROP_CLONES: 2789 if (get_clones_string(zhp, propbuf, proplen) != 0) 2790 return (-1); 2791 break; 2792 2793 case ZFS_PROP_QUOTA: 2794 case ZFS_PROP_REFQUOTA: 2795 case ZFS_PROP_RESERVATION: 2796 case ZFS_PROP_REFRESERVATION: 2797 2798 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2799 return (-1); 2800 /* 2801 * If quota or reservation is 0, we translate this into 'none' 2802 * (unless literal is set), and indicate that it's the default 2803 * value. Otherwise, we print the number nicely and indicate 2804 * that its set locally. 2805 */ 2806 if (val == 0) { 2807 if (literal) 2808 (void) strlcpy(propbuf, "0", proplen); 2809 else 2810 (void) strlcpy(propbuf, "none", proplen); 2811 } else { 2812 if (literal) 2813 (void) snprintf(propbuf, proplen, "%llu", 2814 (u_longlong_t)val); 2815 else 2816 zfs_nicebytes(val, propbuf, proplen); 2817 } 2818 zcp_check(zhp, prop, val, NULL); 2819 break; 2820 2821 case ZFS_PROP_FILESYSTEM_LIMIT: 2822 case ZFS_PROP_SNAPSHOT_LIMIT: 2823 case ZFS_PROP_FILESYSTEM_COUNT: 2824 case ZFS_PROP_SNAPSHOT_COUNT: 2825 2826 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2827 return (-1); 2828 2829 /* 2830 * If limit is UINT64_MAX, we translate this into 'none', and 2831 * indicate that it's the default value. Otherwise, we print 2832 * the number nicely and indicate that it's set locally. 2833 */ 2834 if (val == UINT64_MAX) { 2835 (void) strlcpy(propbuf, "none", proplen); 2836 } else if (literal) { 2837 (void) snprintf(propbuf, proplen, "%llu", 2838 (u_longlong_t)val); 2839 } else { 2840 zfs_nicenum(val, propbuf, proplen); 2841 } 2842 2843 zcp_check(zhp, prop, val, NULL); 2844 break; 2845 2846 case ZFS_PROP_REFRATIO: 2847 case ZFS_PROP_COMPRESSRATIO: 2848 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2849 return (-1); 2850 if (literal) 2851 (void) snprintf(propbuf, proplen, "%llu.%02llu", 2852 (u_longlong_t)(val / 100), 2853 (u_longlong_t)(val % 100)); 2854 else 2855 (void) snprintf(propbuf, proplen, "%llu.%02llux", 2856 (u_longlong_t)(val / 100), 2857 (u_longlong_t)(val % 100)); 2858 zcp_check(zhp, prop, val, NULL); 2859 break; 2860 2861 case ZFS_PROP_TYPE: 2862 switch (zhp->zfs_type) { 2863 case ZFS_TYPE_FILESYSTEM: 2864 str = "filesystem"; 2865 break; 2866 case ZFS_TYPE_VOLUME: 2867 str = "volume"; 2868 break; 2869 case ZFS_TYPE_SNAPSHOT: 2870 str = "snapshot"; 2871 break; 2872 case ZFS_TYPE_BOOKMARK: 2873 str = "bookmark"; 2874 break; 2875 default: 2876 abort(); 2877 } 2878 (void) snprintf(propbuf, proplen, "%s", str); 2879 zcp_check(zhp, prop, 0, propbuf); 2880 break; 2881 2882 case ZFS_PROP_MOUNTED: 2883 /* 2884 * The 'mounted' property is a pseudo-property that described 2885 * whether the filesystem is currently mounted. Even though 2886 * it's a boolean value, the typical values of "on" and "off" 2887 * don't make sense, so we translate to "yes" and "no". 2888 */ 2889 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 2890 src, &source, &val) != 0) 2891 return (-1); 2892 if (val) 2893 (void) strlcpy(propbuf, "yes", proplen); 2894 else 2895 (void) strlcpy(propbuf, "no", proplen); 2896 break; 2897 2898 case ZFS_PROP_NAME: 2899 /* 2900 * The 'name' property is a pseudo-property derived from the 2901 * dataset name. It is presented as a real property to simplify 2902 * consumers. 2903 */ 2904 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2905 zcp_check(zhp, prop, 0, propbuf); 2906 break; 2907 2908 case ZFS_PROP_MLSLABEL: 2909 { 2910 #ifdef HAVE_MLSLABEL 2911 m_label_t *new_sl = NULL; 2912 char *ascii = NULL; /* human readable label */ 2913 2914 (void) strlcpy(propbuf, 2915 getprop_string(zhp, prop, &source), proplen); 2916 2917 if (literal || (strcasecmp(propbuf, 2918 ZFS_MLSLABEL_DEFAULT) == 0)) 2919 break; 2920 2921 /* 2922 * Try to translate the internal hex string to 2923 * human-readable output. If there are any 2924 * problems just use the hex string. 2925 */ 2926 2927 if (str_to_label(propbuf, &new_sl, MAC_LABEL, 2928 L_NO_CORRECTION, NULL) == -1) { 2929 m_label_free(new_sl); 2930 break; 2931 } 2932 2933 if (label_to_str(new_sl, &ascii, M_LABEL, 2934 DEF_NAMES) != 0) { 2935 if (ascii) 2936 free(ascii); 2937 m_label_free(new_sl); 2938 break; 2939 } 2940 m_label_free(new_sl); 2941 2942 (void) strlcpy(propbuf, ascii, proplen); 2943 free(ascii); 2944 #else 2945 (void) strlcpy(propbuf, 2946 getprop_string(zhp, prop, &source), proplen); 2947 #endif /* HAVE_MLSLABEL */ 2948 } 2949 break; 2950 2951 case ZFS_PROP_GUID: 2952 case ZFS_PROP_KEY_GUID: 2953 case ZFS_PROP_IVSET_GUID: 2954 case ZFS_PROP_CREATETXG: 2955 case ZFS_PROP_OBJSETID: 2956 case ZFS_PROP_PBKDF2_ITERS: 2957 /* 2958 * These properties are stored as numbers, but they are 2959 * identifiers or counters. 2960 * We don't want them to be pretty printed, because pretty 2961 * printing truncates their values making them useless. 2962 */ 2963 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2964 return (-1); 2965 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2966 zcp_check(zhp, prop, val, NULL); 2967 break; 2968 2969 case ZFS_PROP_REFERENCED: 2970 case ZFS_PROP_AVAILABLE: 2971 case ZFS_PROP_USED: 2972 case ZFS_PROP_USEDSNAP: 2973 case ZFS_PROP_USEDDS: 2974 case ZFS_PROP_USEDREFRESERV: 2975 case ZFS_PROP_USEDCHILD: 2976 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2977 return (-1); 2978 if (literal) { 2979 (void) snprintf(propbuf, proplen, "%llu", 2980 (u_longlong_t)val); 2981 } else { 2982 zfs_nicebytes(val, propbuf, proplen); 2983 } 2984 zcp_check(zhp, prop, val, NULL); 2985 break; 2986 2987 case ZFS_PROP_SNAPSHOTS_CHANGED: 2988 { 2989 if ((get_numeric_property(zhp, prop, src, &source, 2990 &val) != 0) || val == 0) { 2991 return (-1); 2992 } 2993 2994 time_t time = (time_t)val; 2995 struct tm t; 2996 2997 if (literal || 2998 localtime_r(&time, &t) == NULL || 2999 strftime(propbuf, proplen, "%a %b %e %k:%M:%S %Y", 3000 &t) == 0) 3001 (void) snprintf(propbuf, proplen, "%llu", 3002 (u_longlong_t)val); 3003 } 3004 zcp_check(zhp, prop, val, NULL); 3005 break; 3006 3007 default: 3008 switch (zfs_prop_get_type(prop)) { 3009 case PROP_TYPE_NUMBER: 3010 if (get_numeric_property(zhp, prop, src, 3011 &source, &val) != 0) { 3012 return (-1); 3013 } 3014 3015 if (literal) { 3016 (void) snprintf(propbuf, proplen, "%llu", 3017 (u_longlong_t)val); 3018 } else { 3019 zfs_nicenum(val, propbuf, proplen); 3020 } 3021 zcp_check(zhp, prop, val, NULL); 3022 break; 3023 3024 case PROP_TYPE_STRING: 3025 str = getprop_string(zhp, prop, &source); 3026 if (str == NULL) 3027 return (-1); 3028 3029 (void) strlcpy(propbuf, str, proplen); 3030 zcp_check(zhp, prop, 0, str); 3031 break; 3032 3033 case PROP_TYPE_INDEX: 3034 if (get_numeric_property(zhp, prop, src, 3035 &source, &val) != 0) 3036 return (-1); 3037 if (zfs_prop_index_to_string(prop, val, &strval) != 0) 3038 return (-1); 3039 3040 (void) strlcpy(propbuf, strval, proplen); 3041 zcp_check(zhp, prop, 0, strval); 3042 break; 3043 3044 default: 3045 abort(); 3046 } 3047 } 3048 3049 get_source(zhp, src, source, statbuf, statlen); 3050 3051 return (0); 3052 } 3053 3054 /* 3055 * Utility function to get the given numeric property. Does no validation that 3056 * the given property is the appropriate type; should only be used with 3057 * hard-coded property types. 3058 */ 3059 uint64_t 3060 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 3061 { 3062 const char *source; 3063 uint64_t val = 0; 3064 3065 (void) get_numeric_property(zhp, prop, NULL, &source, &val); 3066 3067 return (val); 3068 } 3069 3070 static int 3071 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 3072 { 3073 char buf[64]; 3074 3075 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 3076 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 3077 } 3078 3079 /* 3080 * Similar to zfs_prop_get(), but returns the value as an integer. 3081 */ 3082 int 3083 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 3084 zprop_source_t *src, char *statbuf, size_t statlen) 3085 { 3086 const char *source; 3087 3088 /* 3089 * Check to see if this property applies to our object 3090 */ 3091 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) { 3092 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 3093 dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 3094 zfs_prop_to_name(prop))); 3095 } 3096 3097 if (src) 3098 *src = ZPROP_SRC_NONE; 3099 3100 if (get_numeric_property(zhp, prop, src, &source, value) != 0) 3101 return (-1); 3102 3103 get_source(zhp, src, source, statbuf, statlen); 3104 3105 return (0); 3106 } 3107 3108 #ifdef HAVE_IDMAP 3109 static int 3110 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 3111 char **domainp, idmap_rid_t *ridp) 3112 { 3113 idmap_get_handle_t *get_hdl = NULL; 3114 idmap_stat status; 3115 int err = EINVAL; 3116 3117 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 3118 goto out; 3119 3120 if (isuser) { 3121 err = idmap_get_sidbyuid(get_hdl, id, 3122 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 3123 } else { 3124 err = idmap_get_sidbygid(get_hdl, id, 3125 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 3126 } 3127 if (err == IDMAP_SUCCESS && 3128 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 3129 status == IDMAP_SUCCESS) 3130 err = 0; 3131 else 3132 err = EINVAL; 3133 out: 3134 if (get_hdl) 3135 idmap_get_destroy(get_hdl); 3136 return (err); 3137 } 3138 #endif /* HAVE_IDMAP */ 3139 3140 /* 3141 * convert the propname into parameters needed by kernel 3142 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 3143 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 3144 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234 3145 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234 3146 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123 3147 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789 3148 */ 3149 static int 3150 userquota_propname_decode(const char *propname, boolean_t zoned, 3151 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 3152 { 3153 zfs_userquota_prop_t type; 3154 char *cp; 3155 boolean_t isuser; 3156 boolean_t isgroup; 3157 boolean_t isproject; 3158 struct passwd *pw; 3159 struct group *gr; 3160 3161 domain[0] = '\0'; 3162 3163 /* Figure out the property type ({user|group|project}{quota|space}) */ 3164 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 3165 if (strncmp(propname, zfs_userquota_prop_prefixes[type], 3166 strlen(zfs_userquota_prop_prefixes[type])) == 0) 3167 break; 3168 } 3169 if (type == ZFS_NUM_USERQUOTA_PROPS) 3170 return (EINVAL); 3171 *typep = type; 3172 3173 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED || 3174 type == ZFS_PROP_USEROBJQUOTA || 3175 type == ZFS_PROP_USEROBJUSED); 3176 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED || 3177 type == ZFS_PROP_GROUPOBJQUOTA || 3178 type == ZFS_PROP_GROUPOBJUSED); 3179 isproject = (type == ZFS_PROP_PROJECTQUOTA || 3180 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA || 3181 type == ZFS_PROP_PROJECTOBJUSED); 3182 3183 cp = strchr(propname, '@') + 1; 3184 3185 if (isuser && (pw = getpwnam(cp)) != NULL) { 3186 if (zoned && getzoneid() == GLOBAL_ZONEID) 3187 return (ENOENT); 3188 *ridp = pw->pw_uid; 3189 } else if (isgroup && (gr = getgrnam(cp)) != NULL) { 3190 if (zoned && getzoneid() == GLOBAL_ZONEID) 3191 return (ENOENT); 3192 *ridp = gr->gr_gid; 3193 } else if (!isproject && strchr(cp, '@')) { 3194 #ifdef HAVE_IDMAP 3195 /* 3196 * It's a SID name (eg "user@domain") that needs to be 3197 * turned into S-1-domainID-RID. 3198 */ 3199 directory_error_t e; 3200 char *numericsid = NULL; 3201 char *end; 3202 3203 if (zoned && getzoneid() == GLOBAL_ZONEID) 3204 return (ENOENT); 3205 if (isuser) { 3206 e = directory_sid_from_user_name(NULL, 3207 cp, &numericsid); 3208 } else { 3209 e = directory_sid_from_group_name(NULL, 3210 cp, &numericsid); 3211 } 3212 if (e != NULL) { 3213 directory_error_free(e); 3214 return (ENOENT); 3215 } 3216 if (numericsid == NULL) 3217 return (ENOENT); 3218 cp = numericsid; 3219 (void) strlcpy(domain, cp, domainlen); 3220 cp = strrchr(domain, '-'); 3221 *cp = '\0'; 3222 cp++; 3223 3224 errno = 0; 3225 *ridp = strtoull(cp, &end, 10); 3226 free(numericsid); 3227 3228 if (errno != 0 || *end != '\0') 3229 return (EINVAL); 3230 #else 3231 (void) domainlen; 3232 return (ENOSYS); 3233 #endif /* HAVE_IDMAP */ 3234 } else { 3235 /* It's a user/group/project ID (eg "12345"). */ 3236 uid_t id; 3237 char *end; 3238 id = strtoul(cp, &end, 10); 3239 if (*end != '\0') 3240 return (EINVAL); 3241 if (id > MAXUID && !isproject) { 3242 #ifdef HAVE_IDMAP 3243 /* It's an ephemeral ID. */ 3244 idmap_rid_t rid; 3245 char *mapdomain; 3246 3247 if (idmap_id_to_numeric_domain_rid(id, isuser, 3248 &mapdomain, &rid) != 0) 3249 return (ENOENT); 3250 (void) strlcpy(domain, mapdomain, domainlen); 3251 *ridp = rid; 3252 #else 3253 return (ENOSYS); 3254 #endif /* HAVE_IDMAP */ 3255 } else { 3256 *ridp = id; 3257 } 3258 } 3259 3260 return (0); 3261 } 3262 3263 static int 3264 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 3265 uint64_t *propvalue, zfs_userquota_prop_t *typep) 3266 { 3267 int err; 3268 zfs_cmd_t zc = {"\0"}; 3269 3270 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3271 3272 err = userquota_propname_decode(propname, 3273 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 3274 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 3275 zc.zc_objset_type = *typep; 3276 if (err) 3277 return (err); 3278 3279 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_USERSPACE_ONE, &zc); 3280 if (err) 3281 return (err); 3282 3283 *propvalue = zc.zc_cookie; 3284 return (0); 3285 } 3286 3287 int 3288 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 3289 uint64_t *propvalue) 3290 { 3291 zfs_userquota_prop_t type; 3292 3293 return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 3294 &type)); 3295 } 3296 3297 int 3298 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 3299 char *propbuf, int proplen, boolean_t literal) 3300 { 3301 int err; 3302 uint64_t propvalue; 3303 zfs_userquota_prop_t type; 3304 3305 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 3306 &type); 3307 3308 if (err) 3309 return (err); 3310 3311 if (literal) { 3312 (void) snprintf(propbuf, proplen, "%llu", 3313 (u_longlong_t)propvalue); 3314 } else if (propvalue == 0 && 3315 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || 3316 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA || 3317 type == ZFS_PROP_PROJECTQUOTA || 3318 type == ZFS_PROP_PROJECTOBJQUOTA)) { 3319 (void) strlcpy(propbuf, "none", proplen); 3320 } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || 3321 type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED || 3322 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) { 3323 zfs_nicebytes(propvalue, propbuf, proplen); 3324 } else { 3325 zfs_nicenum(propvalue, propbuf, proplen); 3326 } 3327 return (0); 3328 } 3329 3330 /* 3331 * propname must start with "written@" or "written#". 3332 */ 3333 int 3334 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 3335 uint64_t *propvalue) 3336 { 3337 int err; 3338 zfs_cmd_t zc = {"\0"}; 3339 const char *snapname; 3340 3341 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3342 3343 assert(zfs_prop_written(propname)); 3344 snapname = propname + strlen("written@"); 3345 if (strchr(snapname, '@') != NULL || strchr(snapname, '#') != NULL) { 3346 /* full snapshot or bookmark name specified */ 3347 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 3348 } else { 3349 /* snapname is the short name, append it to zhp's fsname */ 3350 char *cp; 3351 3352 (void) strlcpy(zc.zc_value, zhp->zfs_name, 3353 sizeof (zc.zc_value)); 3354 cp = strchr(zc.zc_value, '@'); 3355 if (cp != NULL) 3356 *cp = '\0'; 3357 (void) strlcat(zc.zc_value, snapname - 1, sizeof (zc.zc_value)); 3358 } 3359 3360 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SPACE_WRITTEN, &zc); 3361 if (err) 3362 return (err); 3363 3364 *propvalue = zc.zc_cookie; 3365 return (0); 3366 } 3367 3368 int 3369 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 3370 char *propbuf, int proplen, boolean_t literal) 3371 { 3372 int err; 3373 uint64_t propvalue; 3374 3375 err = zfs_prop_get_written_int(zhp, propname, &propvalue); 3376 3377 if (err) 3378 return (err); 3379 3380 if (literal) { 3381 (void) snprintf(propbuf, proplen, "%llu", 3382 (u_longlong_t)propvalue); 3383 } else { 3384 zfs_nicebytes(propvalue, propbuf, proplen); 3385 } 3386 3387 return (0); 3388 } 3389 3390 /* 3391 * Returns the name of the given zfs handle. 3392 */ 3393 const char * 3394 zfs_get_name(const zfs_handle_t *zhp) 3395 { 3396 return (zhp->zfs_name); 3397 } 3398 3399 /* 3400 * Returns the name of the parent pool for the given zfs handle. 3401 */ 3402 const char * 3403 zfs_get_pool_name(const zfs_handle_t *zhp) 3404 { 3405 return (zhp->zpool_hdl->zpool_name); 3406 } 3407 3408 /* 3409 * Returns the type of the given zfs handle. 3410 */ 3411 zfs_type_t 3412 zfs_get_type(const zfs_handle_t *zhp) 3413 { 3414 return (zhp->zfs_type); 3415 } 3416 3417 /* 3418 * Returns the type of the given zfs handle, 3419 * or, if a snapshot, the type of the snapshotted dataset. 3420 */ 3421 zfs_type_t 3422 zfs_get_underlying_type(const zfs_handle_t *zhp) 3423 { 3424 return (zhp->zfs_head_type); 3425 } 3426 3427 /* 3428 * Is one dataset name a child dataset of another? 3429 * 3430 * Needs to handle these cases: 3431 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 3432 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 3433 * Descendant? No. No. No. Yes. 3434 */ 3435 static boolean_t 3436 is_descendant(const char *ds1, const char *ds2) 3437 { 3438 size_t d1len = strlen(ds1); 3439 3440 /* ds2 can't be a descendant if it's smaller */ 3441 if (strlen(ds2) < d1len) 3442 return (B_FALSE); 3443 3444 /* otherwise, compare strings and verify that there's a '/' char */ 3445 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 3446 } 3447 3448 /* 3449 * Given a complete name, return just the portion that refers to the parent. 3450 * Will return -1 if there is no parent (path is just the name of the 3451 * pool). 3452 */ 3453 static int 3454 parent_name(const char *path, char *buf, size_t buflen) 3455 { 3456 char *slashp; 3457 3458 (void) strlcpy(buf, path, buflen); 3459 3460 if ((slashp = strrchr(buf, '/')) == NULL) 3461 return (-1); 3462 *slashp = '\0'; 3463 3464 return (0); 3465 } 3466 3467 int 3468 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen) 3469 { 3470 return (parent_name(zfs_get_name(zhp), buf, buflen)); 3471 } 3472 3473 /* 3474 * If accept_ancestor is false, then check to make sure that the given path has 3475 * a parent, and that it exists. If accept_ancestor is true, then find the 3476 * closest existing ancestor for the given path. In prefixlen return the 3477 * length of already existing prefix of the given path. We also fetch the 3478 * 'zoned' property, which is used to validate property settings when creating 3479 * new datasets. 3480 */ 3481 static int 3482 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 3483 boolean_t accept_ancestor, int *prefixlen) 3484 { 3485 zfs_cmd_t zc = {"\0"}; 3486 char parent[ZFS_MAX_DATASET_NAME_LEN]; 3487 char *slash; 3488 zfs_handle_t *zhp; 3489 char errbuf[ERRBUFLEN]; 3490 uint64_t is_zoned; 3491 3492 (void) snprintf(errbuf, sizeof (errbuf), 3493 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 3494 3495 /* get parent, and check to see if this is just a pool */ 3496 if (parent_name(path, parent, sizeof (parent)) != 0) { 3497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3498 "missing dataset name")); 3499 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3500 } 3501 3502 /* check to see if the pool exists */ 3503 if ((slash = strchr(parent, '/')) == NULL) 3504 slash = parent + strlen(parent); 3505 (void) strlcpy(zc.zc_name, parent, 3506 MIN(sizeof (zc.zc_name), slash - parent + 1)); 3507 if (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 3508 errno == ENOENT) { 3509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3510 "no such pool '%s'"), zc.zc_name); 3511 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3512 } 3513 3514 /* check to see if the parent dataset exists */ 3515 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 3516 if (errno == ENOENT && accept_ancestor) { 3517 /* 3518 * Go deeper to find an ancestor, give up on top level. 3519 */ 3520 if (parent_name(parent, parent, sizeof (parent)) != 0) { 3521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3522 "no such pool '%s'"), zc.zc_name); 3523 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3524 } 3525 } else if (errno == ENOENT) { 3526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3527 "parent does not exist")); 3528 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3529 } else 3530 return (zfs_standard_error(hdl, errno, errbuf)); 3531 } 3532 3533 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 3534 if (zoned != NULL) 3535 *zoned = is_zoned; 3536 3537 /* we are in a non-global zone, but parent is in the global zone */ 3538 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 3539 (void) zfs_standard_error(hdl, EPERM, errbuf); 3540 zfs_close(zhp); 3541 return (-1); 3542 } 3543 3544 /* make sure parent is a filesystem */ 3545 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 3546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3547 "parent is not a filesystem")); 3548 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 3549 zfs_close(zhp); 3550 return (-1); 3551 } 3552 3553 zfs_close(zhp); 3554 if (prefixlen != NULL) 3555 *prefixlen = strlen(parent); 3556 return (0); 3557 } 3558 3559 /* 3560 * Finds whether the dataset of the given type(s) exists. 3561 */ 3562 boolean_t 3563 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 3564 { 3565 zfs_handle_t *zhp; 3566 3567 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 3568 return (B_FALSE); 3569 3570 /* 3571 * Try to get stats for the dataset, which will tell us if it exists. 3572 */ 3573 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 3574 int ds_type = zhp->zfs_type; 3575 3576 zfs_close(zhp); 3577 if (types & ds_type) 3578 return (B_TRUE); 3579 } 3580 return (B_FALSE); 3581 } 3582 3583 /* 3584 * Given a path to 'target', create all the ancestors between 3585 * the prefixlen portion of the path, and the target itself. 3586 * Fail if the initial prefixlen-ancestor does not already exist. 3587 */ 3588 int 3589 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 3590 { 3591 zfs_handle_t *h; 3592 char *cp; 3593 const char *opname; 3594 3595 /* make sure prefix exists */ 3596 cp = target + prefixlen; 3597 if (*cp != '/') { 3598 assert(strchr(cp, '/') == NULL); 3599 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3600 } else { 3601 *cp = '\0'; 3602 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3603 *cp = '/'; 3604 } 3605 if (h == NULL) 3606 return (-1); 3607 zfs_close(h); 3608 3609 /* 3610 * Attempt to create, mount, and share any ancestor filesystems, 3611 * up to the prefixlen-long one. 3612 */ 3613 for (cp = target + prefixlen + 1; 3614 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) { 3615 3616 *cp = '\0'; 3617 3618 h = make_dataset_handle(hdl, target); 3619 if (h) { 3620 /* it already exists, nothing to do here */ 3621 zfs_close(h); 3622 continue; 3623 } 3624 3625 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 3626 NULL) != 0) { 3627 opname = dgettext(TEXT_DOMAIN, "create"); 3628 goto ancestorerr; 3629 } 3630 3631 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3632 if (h == NULL) { 3633 opname = dgettext(TEXT_DOMAIN, "open"); 3634 goto ancestorerr; 3635 } 3636 3637 if (zfs_mount(h, NULL, 0) != 0) { 3638 opname = dgettext(TEXT_DOMAIN, "mount"); 3639 goto ancestorerr; 3640 } 3641 3642 if (zfs_share(h, NULL) != 0) { 3643 opname = dgettext(TEXT_DOMAIN, "share"); 3644 goto ancestorerr; 3645 } 3646 3647 zfs_close(h); 3648 } 3649 zfs_commit_shares(NULL); 3650 3651 return (0); 3652 3653 ancestorerr: 3654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3655 "failed to %s ancestor '%s'"), opname, target); 3656 return (-1); 3657 } 3658 3659 /* 3660 * Creates non-existing ancestors of the given path. 3661 */ 3662 int 3663 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 3664 { 3665 int prefix; 3666 char *path_copy; 3667 char errbuf[ERRBUFLEN]; 3668 int rc = 0; 3669 3670 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3671 "cannot create '%s'"), path); 3672 3673 /* 3674 * Check that we are not passing the nesting limit 3675 * before we start creating any ancestors. 3676 */ 3677 if (dataset_nestcheck(path) != 0) { 3678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3679 "maximum name nesting depth exceeded")); 3680 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3681 } 3682 3683 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 3684 return (-1); 3685 3686 if ((path_copy = strdup(path)) != NULL) { 3687 rc = create_parents(hdl, path_copy, prefix); 3688 free(path_copy); 3689 } 3690 if (path_copy == NULL || rc != 0) 3691 return (-1); 3692 3693 return (0); 3694 } 3695 3696 /* 3697 * Create a new filesystem or volume. 3698 */ 3699 int 3700 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3701 nvlist_t *props) 3702 { 3703 int ret; 3704 uint64_t size = 0; 3705 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 3706 uint64_t zoned; 3707 enum lzc_dataset_type ost; 3708 zpool_handle_t *zpool_handle; 3709 uint8_t *wkeydata = NULL; 3710 uint_t wkeylen = 0; 3711 char errbuf[ERRBUFLEN]; 3712 char parent[ZFS_MAX_DATASET_NAME_LEN]; 3713 3714 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3715 "cannot create '%s'"), path); 3716 3717 /* validate the path, taking care to note the extended error message */ 3718 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 3719 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3720 3721 if (dataset_nestcheck(path) != 0) { 3722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3723 "maximum name nesting depth exceeded")); 3724 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3725 } 3726 3727 /* validate parents exist */ 3728 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3729 return (-1); 3730 3731 /* 3732 * The failure modes when creating a dataset of a different type over 3733 * one that already exists is a little strange. In particular, if you 3734 * try to create a dataset on top of an existing dataset, the ioctl() 3735 * will return ENOENT, not EEXIST. To prevent this from happening, we 3736 * first try to see if the dataset exists. 3737 */ 3738 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 3739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3740 "dataset already exists")); 3741 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3742 } 3743 3744 if (type == ZFS_TYPE_VOLUME) 3745 ost = LZC_DATSET_TYPE_ZVOL; 3746 else 3747 ost = LZC_DATSET_TYPE_ZFS; 3748 3749 /* open zpool handle for prop validation */ 3750 char pool_path[ZFS_MAX_DATASET_NAME_LEN]; 3751 (void) strlcpy(pool_path, path, sizeof (pool_path)); 3752 3753 /* truncate pool_path at first slash */ 3754 char *p = strchr(pool_path, '/'); 3755 if (p != NULL) 3756 *p = '\0'; 3757 3758 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL) 3759 return (-1); 3760 3761 if (props && (props = zfs_valid_proplist(hdl, type, props, 3762 zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) { 3763 zpool_close(zpool_handle); 3764 return (-1); 3765 } 3766 zpool_close(zpool_handle); 3767 3768 if (type == ZFS_TYPE_VOLUME) { 3769 /* 3770 * If we are creating a volume, the size and block size must 3771 * satisfy a few restraints. First, the blocksize must be a 3772 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 3773 * volsize must be a multiple of the block size, and cannot be 3774 * zero. 3775 */ 3776 if (props == NULL || nvlist_lookup_uint64(props, 3777 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3778 nvlist_free(props); 3779 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3780 "missing volume size")); 3781 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3782 } 3783 3784 if ((ret = nvlist_lookup_uint64(props, 3785 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3786 &blocksize)) != 0) { 3787 if (ret == ENOENT) { 3788 blocksize = zfs_prop_default_numeric( 3789 ZFS_PROP_VOLBLOCKSIZE); 3790 } else { 3791 nvlist_free(props); 3792 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3793 "missing volume block size")); 3794 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3795 } 3796 } 3797 3798 if (size == 0) { 3799 nvlist_free(props); 3800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3801 "volume size cannot be zero")); 3802 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3803 } 3804 3805 if (size % blocksize != 0) { 3806 nvlist_free(props); 3807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3808 "volume size must be a multiple of volume block " 3809 "size")); 3810 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3811 } 3812 } 3813 3814 (void) parent_name(path, parent, sizeof (parent)); 3815 if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE, 3816 &wkeydata, &wkeylen) != 0) { 3817 nvlist_free(props); 3818 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 3819 } 3820 3821 /* create the dataset */ 3822 ret = lzc_create(path, ost, props, wkeydata, wkeylen); 3823 nvlist_free(props); 3824 if (wkeydata != NULL) 3825 free(wkeydata); 3826 3827 /* check for failure */ 3828 if (ret != 0) { 3829 switch (errno) { 3830 case ENOENT: 3831 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3832 "no such parent '%s'"), parent); 3833 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3834 3835 case ENOTSUP: 3836 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3837 "pool must be upgraded to set this " 3838 "property or value")); 3839 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3840 3841 case EACCES: 3842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3843 "encryption root's key is not loaded " 3844 "or provided")); 3845 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 3846 3847 case ERANGE: 3848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3849 "invalid property value(s) specified")); 3850 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3851 #ifdef _ILP32 3852 case EOVERFLOW: 3853 /* 3854 * This platform can't address a volume this big. 3855 */ 3856 if (type == ZFS_TYPE_VOLUME) 3857 return (zfs_error(hdl, EZFS_VOLTOOBIG, 3858 errbuf)); 3859 zfs_fallthrough; 3860 #endif 3861 default: 3862 return (zfs_standard_error(hdl, errno, errbuf)); 3863 } 3864 } 3865 3866 return (0); 3867 } 3868 3869 /* 3870 * Destroys the given dataset. The caller must make sure that the filesystem 3871 * isn't mounted, and that there are no active dependents. If the file system 3872 * does not exist this function does nothing. 3873 */ 3874 int 3875 zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3876 { 3877 int error; 3878 3879 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer) 3880 return (EINVAL); 3881 3882 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 3883 nvlist_t *nv = fnvlist_alloc(); 3884 fnvlist_add_boolean(nv, zhp->zfs_name); 3885 error = lzc_destroy_bookmarks(nv, NULL); 3886 fnvlist_free(nv); 3887 if (error != 0) { 3888 return (zfs_standard_error_fmt(zhp->zfs_hdl, error, 3889 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 3890 zhp->zfs_name)); 3891 } 3892 return (0); 3893 } 3894 3895 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3896 nvlist_t *nv = fnvlist_alloc(); 3897 fnvlist_add_boolean(nv, zhp->zfs_name); 3898 error = lzc_destroy_snaps(nv, defer, NULL); 3899 fnvlist_free(nv); 3900 } else { 3901 error = lzc_destroy(zhp->zfs_name); 3902 } 3903 3904 if (error != 0 && error != ENOENT) { 3905 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3906 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 3907 zhp->zfs_name)); 3908 } 3909 3910 remove_mountpoint(zhp); 3911 3912 return (0); 3913 } 3914 3915 struct destroydata { 3916 nvlist_t *nvl; 3917 const char *snapname; 3918 }; 3919 3920 static int 3921 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 3922 { 3923 struct destroydata *dd = arg; 3924 char name[ZFS_MAX_DATASET_NAME_LEN]; 3925 int rv = 0; 3926 3927 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, 3928 dd->snapname) >= sizeof (name)) 3929 return (EINVAL); 3930 3931 if (lzc_exists(name)) 3932 fnvlist_add_boolean(dd->nvl, name); 3933 3934 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_check_snap_cb, dd); 3935 zfs_close(zhp); 3936 return (rv); 3937 } 3938 3939 /* 3940 * Destroys all snapshots with the given name in zhp & descendants. 3941 */ 3942 int 3943 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 3944 { 3945 int ret; 3946 struct destroydata dd = { 0 }; 3947 3948 dd.snapname = snapname; 3949 dd.nvl = fnvlist_alloc(); 3950 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 3951 3952 if (nvlist_empty(dd.nvl)) { 3953 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 3954 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 3955 zhp->zfs_name, snapname); 3956 } else { 3957 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 3958 } 3959 fnvlist_free(dd.nvl); 3960 return (ret); 3961 } 3962 3963 /* 3964 * Destroys all the snapshots named in the nvlist. 3965 */ 3966 int 3967 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 3968 { 3969 nvlist_t *errlist = NULL; 3970 nvpair_t *pair; 3971 3972 int ret = zfs_destroy_snaps_nvl_os(hdl, snaps); 3973 if (ret != 0) 3974 return (ret); 3975 3976 ret = lzc_destroy_snaps(snaps, defer, &errlist); 3977 3978 if (ret == 0) { 3979 nvlist_free(errlist); 3980 return (0); 3981 } 3982 3983 if (nvlist_empty(errlist)) { 3984 char errbuf[ERRBUFLEN]; 3985 (void) snprintf(errbuf, sizeof (errbuf), 3986 dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 3987 3988 ret = zfs_standard_error(hdl, ret, errbuf); 3989 } 3990 for (pair = nvlist_next_nvpair(errlist, NULL); 3991 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 3992 char errbuf[ERRBUFLEN]; 3993 (void) snprintf(errbuf, sizeof (errbuf), 3994 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 3995 nvpair_name(pair)); 3996 3997 switch (fnvpair_value_int32(pair)) { 3998 case EEXIST: 3999 zfs_error_aux(hdl, 4000 dgettext(TEXT_DOMAIN, "snapshot is cloned")); 4001 ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 4002 break; 4003 default: 4004 ret = zfs_standard_error(hdl, errno, errbuf); 4005 break; 4006 } 4007 } 4008 4009 nvlist_free(errlist); 4010 return (ret); 4011 } 4012 4013 /* 4014 * Clones the given dataset. The target must be of the same type as the source. 4015 */ 4016 int 4017 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 4018 { 4019 char parent[ZFS_MAX_DATASET_NAME_LEN]; 4020 int ret; 4021 char errbuf[ERRBUFLEN]; 4022 libzfs_handle_t *hdl = zhp->zfs_hdl; 4023 uint64_t zoned; 4024 4025 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 4026 4027 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4028 "cannot create '%s'"), target); 4029 4030 /* validate the target/clone name */ 4031 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 4032 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4033 4034 /* validate parents exist */ 4035 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 4036 return (-1); 4037 4038 (void) parent_name(target, parent, sizeof (parent)); 4039 4040 /* do the clone */ 4041 4042 if (props) { 4043 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 4044 4045 if (ZFS_IS_VOLUME(zhp)) 4046 type = ZFS_TYPE_VOLUME; 4047 if ((props = zfs_valid_proplist(hdl, type, props, zoned, 4048 zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL) 4049 return (-1); 4050 if (zfs_fix_auto_resv(zhp, props) == -1) { 4051 nvlist_free(props); 4052 return (-1); 4053 } 4054 } 4055 4056 if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) { 4057 nvlist_free(props); 4058 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 4059 } 4060 4061 ret = lzc_clone(target, zhp->zfs_name, props); 4062 nvlist_free(props); 4063 4064 if (ret != 0) { 4065 switch (errno) { 4066 4067 case ENOENT: 4068 /* 4069 * The parent doesn't exist. We should have caught this 4070 * above, but there may a race condition that has since 4071 * destroyed the parent. 4072 * 4073 * At this point, we don't know whether it's the source 4074 * that doesn't exist anymore, or whether the target 4075 * dataset doesn't exist. 4076 */ 4077 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4078 "no such parent '%s'"), parent); 4079 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 4080 4081 case EXDEV: 4082 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4083 "source and target pools differ")); 4084 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 4085 errbuf)); 4086 4087 default: 4088 return (zfs_standard_error(zhp->zfs_hdl, errno, 4089 errbuf)); 4090 } 4091 } 4092 4093 return (ret); 4094 } 4095 4096 /* 4097 * Promotes the given clone fs to be the clone parent. 4098 */ 4099 int 4100 zfs_promote(zfs_handle_t *zhp) 4101 { 4102 libzfs_handle_t *hdl = zhp->zfs_hdl; 4103 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 4104 int ret; 4105 char errbuf[ERRBUFLEN]; 4106 4107 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4108 "cannot promote '%s'"), zhp->zfs_name); 4109 4110 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 4111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4112 "snapshots can not be promoted")); 4113 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4114 } 4115 4116 if (zhp->zfs_dmustats.dds_origin[0] == '\0') { 4117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4118 "not a cloned filesystem")); 4119 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4120 } 4121 4122 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 4123 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4124 4125 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname)); 4126 4127 if (ret != 0) { 4128 switch (ret) { 4129 case EACCES: 4130 /* 4131 * Promoting encrypted dataset outside its 4132 * encryption root. 4133 */ 4134 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4135 "cannot promote dataset outside its " 4136 "encryption root")); 4137 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 4138 4139 case EEXIST: 4140 /* There is a conflicting snapshot name. */ 4141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4142 "conflicting snapshot '%s' from parent '%s'"), 4143 snapname, zhp->zfs_dmustats.dds_origin); 4144 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 4145 4146 default: 4147 return (zfs_standard_error(hdl, ret, errbuf)); 4148 } 4149 } 4150 return (ret); 4151 } 4152 4153 typedef struct snapdata { 4154 nvlist_t *sd_nvl; 4155 const char *sd_snapname; 4156 } snapdata_t; 4157 4158 static int 4159 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 4160 { 4161 snapdata_t *sd = arg; 4162 char name[ZFS_MAX_DATASET_NAME_LEN]; 4163 int rv = 0; 4164 4165 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 4166 if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp), 4167 sd->sd_snapname) >= sizeof (name)) 4168 return (EINVAL); 4169 4170 fnvlist_add_boolean(sd->sd_nvl, name); 4171 4172 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_snapshot_cb, sd); 4173 } 4174 zfs_close(zhp); 4175 4176 return (rv); 4177 } 4178 4179 /* 4180 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 4181 * created. 4182 */ 4183 int 4184 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 4185 { 4186 int ret; 4187 char errbuf[ERRBUFLEN]; 4188 nvpair_t *elem; 4189 nvlist_t *errors; 4190 zpool_handle_t *zpool_hdl; 4191 char pool[ZFS_MAX_DATASET_NAME_LEN]; 4192 4193 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4194 "cannot create snapshots ")); 4195 4196 elem = NULL; 4197 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 4198 const char *snapname = nvpair_name(elem); 4199 4200 /* validate the target name */ 4201 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 4202 B_TRUE)) { 4203 (void) snprintf(errbuf, sizeof (errbuf), 4204 dgettext(TEXT_DOMAIN, 4205 "cannot create snapshot '%s'"), snapname); 4206 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4207 } 4208 } 4209 4210 /* 4211 * get pool handle for prop validation. assumes all snaps are in the 4212 * same pool, as does lzc_snapshot (below). 4213 */ 4214 elem = nvlist_next_nvpair(snaps, NULL); 4215 if (elem == NULL) 4216 return (-1); 4217 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); 4218 pool[strcspn(pool, "/@")] = '\0'; 4219 zpool_hdl = zpool_open(hdl, pool); 4220 if (zpool_hdl == NULL) 4221 return (-1); 4222 4223 if (props != NULL && 4224 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 4225 props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) { 4226 zpool_close(zpool_hdl); 4227 return (-1); 4228 } 4229 zpool_close(zpool_hdl); 4230 4231 ret = lzc_snapshot(snaps, props, &errors); 4232 4233 if (ret != 0) { 4234 boolean_t printed = B_FALSE; 4235 for (elem = nvlist_next_nvpair(errors, NULL); 4236 elem != NULL; 4237 elem = nvlist_next_nvpair(errors, elem)) { 4238 (void) snprintf(errbuf, sizeof (errbuf), 4239 dgettext(TEXT_DOMAIN, 4240 "cannot create snapshot '%s'"), nvpair_name(elem)); 4241 (void) zfs_standard_error(hdl, 4242 fnvpair_value_int32(elem), errbuf); 4243 printed = B_TRUE; 4244 } 4245 if (!printed) { 4246 switch (ret) { 4247 case EXDEV: 4248 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4249 "multiple snapshots of same " 4250 "fs not allowed")); 4251 (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4252 4253 break; 4254 default: 4255 (void) zfs_standard_error(hdl, ret, errbuf); 4256 } 4257 } 4258 } 4259 4260 nvlist_free(props); 4261 nvlist_free(errors); 4262 return (ret); 4263 } 4264 4265 int 4266 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 4267 nvlist_t *props) 4268 { 4269 int ret; 4270 snapdata_t sd = { 0 }; 4271 char fsname[ZFS_MAX_DATASET_NAME_LEN]; 4272 char *cp; 4273 zfs_handle_t *zhp; 4274 char errbuf[ERRBUFLEN]; 4275 4276 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4277 "cannot snapshot %s"), path); 4278 4279 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 4280 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4281 4282 (void) strlcpy(fsname, path, sizeof (fsname)); 4283 cp = strchr(fsname, '@'); 4284 *cp = '\0'; 4285 sd.sd_snapname = cp + 1; 4286 4287 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 4288 ZFS_TYPE_VOLUME)) == NULL) { 4289 return (-1); 4290 } 4291 4292 sd.sd_nvl = fnvlist_alloc(); 4293 if (recursive) { 4294 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 4295 } else { 4296 fnvlist_add_boolean(sd.sd_nvl, path); 4297 } 4298 4299 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 4300 fnvlist_free(sd.sd_nvl); 4301 zfs_close(zhp); 4302 return (ret); 4303 } 4304 4305 /* 4306 * Destroy any more recent snapshots. We invoke this callback on any dependents 4307 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 4308 * is a dependent and we should just destroy it without checking the transaction 4309 * group. 4310 */ 4311 typedef struct rollback_data { 4312 const char *cb_target; /* the snapshot */ 4313 uint64_t cb_create; /* creation time reference */ 4314 boolean_t cb_error; 4315 boolean_t cb_force; 4316 } rollback_data_t; 4317 4318 static int 4319 rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 4320 { 4321 rollback_data_t *cbp = data; 4322 prop_changelist_t *clp; 4323 4324 /* We must destroy this clone; first unmount it */ 4325 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 4326 cbp->cb_force ? MS_FORCE: 0); 4327 if (clp == NULL || changelist_prefix(clp) != 0) { 4328 cbp->cb_error = B_TRUE; 4329 zfs_close(zhp); 4330 return (0); 4331 } 4332 if (zfs_destroy(zhp, B_FALSE) != 0) 4333 cbp->cb_error = B_TRUE; 4334 else 4335 changelist_remove(clp, zhp->zfs_name); 4336 (void) changelist_postfix(clp); 4337 changelist_free(clp); 4338 4339 zfs_close(zhp); 4340 return (0); 4341 } 4342 4343 static int 4344 rollback_destroy(zfs_handle_t *zhp, void *data) 4345 { 4346 rollback_data_t *cbp = data; 4347 4348 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 4349 cbp->cb_error |= zfs_iter_dependents_v2(zhp, 0, B_FALSE, 4350 rollback_destroy_dependent, cbp); 4351 4352 cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 4353 } 4354 4355 zfs_close(zhp); 4356 return (0); 4357 } 4358 4359 /* 4360 * Given a dataset, rollback to a specific snapshot, discarding any 4361 * data changes since then and making it the active dataset. 4362 * 4363 * Any snapshots and bookmarks more recent than the target are 4364 * destroyed, along with their dependents (i.e. clones). 4365 */ 4366 int 4367 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 4368 { 4369 rollback_data_t cb = { 0 }; 4370 int err; 4371 boolean_t restore_resv = 0; 4372 uint64_t old_volsize = 0, new_volsize; 4373 zfs_prop_t resv_prop = { 0 }; 4374 uint64_t min_txg = 0; 4375 4376 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 4377 zhp->zfs_type == ZFS_TYPE_VOLUME); 4378 4379 /* 4380 * Destroy all recent snapshots and their dependents. 4381 */ 4382 cb.cb_force = force; 4383 cb.cb_target = snap->zfs_name; 4384 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 4385 4386 if (cb.cb_create > 0) 4387 min_txg = cb.cb_create; 4388 4389 (void) zfs_iter_snapshots_v2(zhp, 0, rollback_destroy, &cb, 4390 min_txg, 0); 4391 4392 (void) zfs_iter_bookmarks_v2(zhp, 0, rollback_destroy, &cb); 4393 4394 if (cb.cb_error) 4395 return (-1); 4396 4397 /* 4398 * Now that we have verified that the snapshot is the latest, 4399 * rollback to the given snapshot. 4400 */ 4401 4402 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 4403 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 4404 return (-1); 4405 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 4406 restore_resv = 4407 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 4408 } 4409 4410 /* 4411 * Pass both the filesystem and the wanted snapshot names, 4412 * we would get an error back if the snapshot is destroyed or 4413 * a new snapshot is created before this request is processed. 4414 */ 4415 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name); 4416 if (err != 0) { 4417 char errbuf[ERRBUFLEN]; 4418 4419 (void) snprintf(errbuf, sizeof (errbuf), 4420 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 4421 zhp->zfs_name); 4422 switch (err) { 4423 case EEXIST: 4424 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4425 "there is a snapshot or bookmark more recent " 4426 "than '%s'"), snap->zfs_name); 4427 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf); 4428 break; 4429 case ESRCH: 4430 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 4431 "'%s' is not found among snapshots of '%s'"), 4432 snap->zfs_name, zhp->zfs_name); 4433 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf); 4434 break; 4435 case EINVAL: 4436 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf); 4437 break; 4438 default: 4439 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf); 4440 } 4441 return (err); 4442 } 4443 4444 /* 4445 * For volumes, if the pre-rollback volsize matched the pre- 4446 * rollback reservation and the volsize has changed then set 4447 * the reservation property to the post-rollback volsize. 4448 * Make a new handle since the rollback closed the dataset. 4449 */ 4450 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 4451 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 4452 if (restore_resv) { 4453 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 4454 if (old_volsize != new_volsize) 4455 err = zfs_prop_set_int(zhp, resv_prop, 4456 new_volsize); 4457 } 4458 zfs_close(zhp); 4459 } 4460 return (err); 4461 } 4462 4463 /* 4464 * Renames the given dataset. 4465 */ 4466 int 4467 zfs_rename(zfs_handle_t *zhp, const char *target, renameflags_t flags) 4468 { 4469 int ret = 0; 4470 zfs_cmd_t zc = {"\0"}; 4471 char *delim; 4472 prop_changelist_t *cl = NULL; 4473 char parent[ZFS_MAX_DATASET_NAME_LEN]; 4474 char property[ZFS_MAXPROPLEN]; 4475 libzfs_handle_t *hdl = zhp->zfs_hdl; 4476 char errbuf[ERRBUFLEN]; 4477 4478 /* if we have the same exact name, just return success */ 4479 if (strcmp(zhp->zfs_name, target) == 0) 4480 return (0); 4481 4482 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4483 "cannot rename to '%s'"), target); 4484 4485 /* make sure source name is valid */ 4486 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 4487 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4488 4489 /* 4490 * Make sure the target name is valid 4491 */ 4492 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 4493 if ((strchr(target, '@') == NULL) || 4494 *target == '@') { 4495 /* 4496 * Snapshot target name is abbreviated, 4497 * reconstruct full dataset name 4498 */ 4499 (void) strlcpy(parent, zhp->zfs_name, 4500 sizeof (parent)); 4501 delim = strchr(parent, '@'); 4502 if (strchr(target, '@') == NULL) 4503 *(++delim) = '\0'; 4504 else 4505 *delim = '\0'; 4506 (void) strlcat(parent, target, sizeof (parent)); 4507 target = parent; 4508 } else { 4509 /* 4510 * Make sure we're renaming within the same dataset. 4511 */ 4512 delim = strchr(target, '@'); 4513 if (strncmp(zhp->zfs_name, target, delim - target) 4514 != 0 || zhp->zfs_name[delim - target] != '@') { 4515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4516 "snapshots must be part of same " 4517 "dataset")); 4518 return (zfs_error(hdl, EZFS_CROSSTARGET, 4519 errbuf)); 4520 } 4521 } 4522 4523 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 4524 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4525 } else { 4526 if (flags.recursive) { 4527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4528 "recursive rename must be a snapshot")); 4529 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4530 } 4531 4532 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 4533 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4534 4535 /* validate parents */ 4536 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 4537 return (-1); 4538 4539 /* make sure we're in the same pool */ 4540 verify((delim = strchr(target, '/')) != NULL); 4541 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4542 zhp->zfs_name[delim - target] != '/') { 4543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4544 "datasets must be within same pool")); 4545 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4546 } 4547 4548 /* new name cannot be a child of the current dataset name */ 4549 if (is_descendant(zhp->zfs_name, target)) { 4550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4551 "New dataset name cannot be a descendant of " 4552 "current dataset name")); 4553 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4554 } 4555 } 4556 4557 (void) snprintf(errbuf, sizeof (errbuf), 4558 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 4559 4560 if (getzoneid() == GLOBAL_ZONEID && 4561 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 4562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4563 "dataset is used in a non-global zone")); 4564 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4565 } 4566 4567 /* 4568 * Avoid unmounting file systems with mountpoint property set to 4569 * 'legacy' or 'none' even if -u option is not given. 4570 */ 4571 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && 4572 !flags.recursive && !flags.nounmount && 4573 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property, 4574 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 && 4575 (strcmp(property, "legacy") == 0 || 4576 strcmp(property, "none") == 0)) { 4577 flags.nounmount = B_TRUE; 4578 } 4579 if (flags.recursive) { 4580 char *parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 4581 delim = strchr(parentname, '@'); 4582 *delim = '\0'; 4583 zfs_handle_t *zhrp = zfs_open(zhp->zfs_hdl, parentname, 4584 ZFS_TYPE_DATASET); 4585 free(parentname); 4586 if (zhrp == NULL) { 4587 ret = -1; 4588 goto error; 4589 } 4590 zfs_close(zhrp); 4591 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 4592 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 4593 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 4594 CL_GATHER_ITER_MOUNTED, 4595 flags.forceunmount ? MS_FORCE : 0)) == NULL) 4596 return (-1); 4597 4598 if (changelist_haszonedchild(cl)) { 4599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4600 "child dataset with inherited mountpoint is used " 4601 "in a non-global zone")); 4602 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 4603 ret = -1; 4604 goto error; 4605 } 4606 4607 if ((ret = changelist_prefix(cl)) != 0) 4608 goto error; 4609 } 4610 4611 if (ZFS_IS_VOLUME(zhp)) 4612 zc.zc_objset_type = DMU_OST_ZVOL; 4613 else 4614 zc.zc_objset_type = DMU_OST_ZFS; 4615 4616 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4617 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 4618 4619 zc.zc_cookie = !!flags.recursive; 4620 zc.zc_cookie |= (!!flags.nounmount) << 1; 4621 4622 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 4623 /* 4624 * if it was recursive, the one that actually failed will 4625 * be in zc.zc_name 4626 */ 4627 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4628 "cannot rename '%s'"), zc.zc_name); 4629 4630 if (flags.recursive && errno == EEXIST) { 4631 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4632 "a child dataset already has a snapshot " 4633 "with the new name")); 4634 (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4635 } else if (errno == EACCES) { 4636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4637 "cannot move encrypted child outside of " 4638 "its encryption root")); 4639 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf); 4640 } else { 4641 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4642 } 4643 4644 /* 4645 * On failure, we still want to remount any filesystems that 4646 * were previously mounted, so we don't alter the system state. 4647 */ 4648 if (cl != NULL) 4649 (void) changelist_postfix(cl); 4650 } else { 4651 if (cl != NULL) { 4652 changelist_rename(cl, zfs_get_name(zhp), target); 4653 ret = changelist_postfix(cl); 4654 } 4655 } 4656 4657 error: 4658 if (cl != NULL) { 4659 changelist_free(cl); 4660 } 4661 return (ret); 4662 } 4663 4664 nvlist_t * 4665 zfs_get_all_props(zfs_handle_t *zhp) 4666 { 4667 return (zhp->zfs_props); 4668 } 4669 4670 nvlist_t * 4671 zfs_get_recvd_props(zfs_handle_t *zhp) 4672 { 4673 if (zhp->zfs_recvd_props == NULL) 4674 if (get_recvd_props_ioctl(zhp) != 0) 4675 return (NULL); 4676 return (zhp->zfs_recvd_props); 4677 } 4678 4679 nvlist_t * 4680 zfs_get_user_props(zfs_handle_t *zhp) 4681 { 4682 return (zhp->zfs_user_props); 4683 } 4684 4685 /* 4686 * This function is used by 'zfs list' to determine the exact set of columns to 4687 * display, and their maximum widths. This does two main things: 4688 * 4689 * - If this is a list of all properties, then expand the list to include 4690 * all native properties, and set a flag so that for each dataset we look 4691 * for new unique user properties and add them to the list. 4692 * 4693 * - For non fixed-width properties, keep track of the maximum width seen 4694 * so that we can size the column appropriately. If the user has 4695 * requested received property values, we also need to compute the width 4696 * of the RECEIVED column. 4697 */ 4698 int 4699 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 4700 boolean_t literal) 4701 { 4702 libzfs_handle_t *hdl = zhp->zfs_hdl; 4703 zprop_list_t *entry; 4704 zprop_list_t **last, **start; 4705 nvlist_t *userprops, *propval; 4706 nvpair_t *elem; 4707 const char *strval; 4708 char buf[ZFS_MAXPROPLEN]; 4709 4710 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 4711 return (-1); 4712 4713 userprops = zfs_get_user_props(zhp); 4714 4715 entry = *plp; 4716 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4717 /* 4718 * Go through and add any user properties as necessary. We 4719 * start by incrementing our list pointer to the first 4720 * non-native property. 4721 */ 4722 start = plp; 4723 while (*start != NULL) { 4724 if ((*start)->pl_prop == ZPROP_USERPROP) 4725 break; 4726 start = &(*start)->pl_next; 4727 } 4728 4729 elem = NULL; 4730 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4731 /* 4732 * See if we've already found this property in our list. 4733 */ 4734 for (last = start; *last != NULL; 4735 last = &(*last)->pl_next) { 4736 if (strcmp((*last)->pl_user_prop, 4737 nvpair_name(elem)) == 0) 4738 break; 4739 } 4740 4741 if (*last == NULL) { 4742 entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 4743 entry->pl_user_prop = 4744 zfs_strdup(hdl, nvpair_name(elem)); 4745 entry->pl_prop = ZPROP_USERPROP; 4746 entry->pl_width = strlen(nvpair_name(elem)); 4747 entry->pl_all = B_TRUE; 4748 *last = entry; 4749 } 4750 } 4751 } 4752 4753 /* 4754 * Now go through and check the width of any non-fixed columns 4755 */ 4756 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 4757 if (entry->pl_fixed && !literal) 4758 continue; 4759 4760 if (entry->pl_prop != ZPROP_USERPROP) { 4761 if (zfs_prop_get(zhp, entry->pl_prop, 4762 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 4763 if (strlen(buf) > entry->pl_width) 4764 entry->pl_width = strlen(buf); 4765 } 4766 if (received && zfs_prop_get_recvd(zhp, 4767 zfs_prop_to_name(entry->pl_prop), 4768 buf, sizeof (buf), literal) == 0) 4769 if (strlen(buf) > entry->pl_recvd_width) 4770 entry->pl_recvd_width = strlen(buf); 4771 } else { 4772 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 4773 &propval) == 0) { 4774 strval = fnvlist_lookup_string(propval, 4775 ZPROP_VALUE); 4776 if (strlen(strval) > entry->pl_width) 4777 entry->pl_width = strlen(strval); 4778 } 4779 if (received && zfs_prop_get_recvd(zhp, 4780 entry->pl_user_prop, 4781 buf, sizeof (buf), literal) == 0) 4782 if (strlen(buf) > entry->pl_recvd_width) 4783 entry->pl_recvd_width = strlen(buf); 4784 } 4785 } 4786 4787 return (0); 4788 } 4789 4790 void 4791 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 4792 { 4793 nvpair_t *curr; 4794 nvpair_t *next; 4795 4796 /* 4797 * Keep a reference to the props-table against which we prune the 4798 * properties. 4799 */ 4800 zhp->zfs_props_table = props; 4801 4802 curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 4803 4804 while (curr) { 4805 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 4806 next = nvlist_next_nvpair(zhp->zfs_props, curr); 4807 4808 /* 4809 * User properties will result in ZPROP_USERPROP (an alias 4810 * for ZPROP_INVAL), and since we 4811 * only know how to prune standard ZFS properties, we always 4812 * leave these in the list. This can also happen if we 4813 * encounter an unknown DSL property (when running older 4814 * software, for example). 4815 */ 4816 if (zfs_prop != ZPROP_USERPROP && props[zfs_prop] == B_FALSE) 4817 (void) nvlist_remove(zhp->zfs_props, 4818 nvpair_name(curr), nvpair_type(curr)); 4819 curr = next; 4820 } 4821 } 4822 4823 static int 4824 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4825 zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4826 { 4827 zfs_cmd_t zc = {"\0"}; 4828 nvlist_t *nvlist = NULL; 4829 int error; 4830 4831 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4832 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4833 zc.zc_cookie = (uint64_t)cmd; 4834 4835 if (cmd == ZFS_SMB_ACL_RENAME) { 4836 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4837 (void) no_memory(hdl); 4838 return (0); 4839 } 4840 } 4841 4842 switch (cmd) { 4843 case ZFS_SMB_ACL_ADD: 4844 case ZFS_SMB_ACL_REMOVE: 4845 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4846 break; 4847 case ZFS_SMB_ACL_RENAME: 4848 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4849 resource1) != 0) { 4850 (void) no_memory(hdl); 4851 return (-1); 4852 } 4853 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4854 resource2) != 0) { 4855 (void) no_memory(hdl); 4856 return (-1); 4857 } 4858 zcmd_write_src_nvlist(hdl, &zc, nvlist); 4859 break; 4860 case ZFS_SMB_ACL_PURGE: 4861 break; 4862 default: 4863 return (-1); 4864 } 4865 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4866 nvlist_free(nvlist); 4867 return (error); 4868 } 4869 4870 int 4871 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4872 char *path, char *resource) 4873 { 4874 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4875 resource, NULL)); 4876 } 4877 4878 int 4879 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4880 char *path, char *resource) 4881 { 4882 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4883 resource, NULL)); 4884 } 4885 4886 int 4887 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4888 { 4889 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4890 NULL, NULL)); 4891 } 4892 4893 int 4894 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4895 char *oldname, char *newname) 4896 { 4897 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4898 oldname, newname)); 4899 } 4900 4901 int 4902 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 4903 zfs_userspace_cb_t func, void *arg) 4904 { 4905 zfs_cmd_t zc = {"\0"}; 4906 zfs_useracct_t buf[100]; 4907 libzfs_handle_t *hdl = zhp->zfs_hdl; 4908 int ret; 4909 4910 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4911 4912 zc.zc_objset_type = type; 4913 zc.zc_nvlist_dst = (uintptr_t)buf; 4914 4915 for (;;) { 4916 zfs_useracct_t *zua = buf; 4917 4918 zc.zc_nvlist_dst_size = sizeof (buf); 4919 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 4920 if ((errno == ENOTSUP && 4921 (type == ZFS_PROP_USEROBJUSED || 4922 type == ZFS_PROP_GROUPOBJUSED || 4923 type == ZFS_PROP_USEROBJQUOTA || 4924 type == ZFS_PROP_GROUPOBJQUOTA || 4925 type == ZFS_PROP_PROJECTOBJUSED || 4926 type == ZFS_PROP_PROJECTOBJQUOTA || 4927 type == ZFS_PROP_PROJECTUSED || 4928 type == ZFS_PROP_PROJECTQUOTA))) 4929 break; 4930 4931 return (zfs_standard_error_fmt(hdl, errno, 4932 dgettext(TEXT_DOMAIN, 4933 "cannot get used/quota for %s"), zc.zc_name)); 4934 } 4935 if (zc.zc_nvlist_dst_size == 0) 4936 break; 4937 4938 while (zc.zc_nvlist_dst_size > 0) { 4939 if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 4940 zua->zu_space)) != 0) 4941 return (ret); 4942 zua++; 4943 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 4944 } 4945 } 4946 4947 return (0); 4948 } 4949 4950 struct holdarg { 4951 nvlist_t *nvl; 4952 const char *snapname; 4953 const char *tag; 4954 boolean_t recursive; 4955 int error; 4956 }; 4957 4958 static int 4959 zfs_hold_one(zfs_handle_t *zhp, void *arg) 4960 { 4961 struct holdarg *ha = arg; 4962 char name[ZFS_MAX_DATASET_NAME_LEN]; 4963 int rv = 0; 4964 4965 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, 4966 ha->snapname) >= sizeof (name)) 4967 return (EINVAL); 4968 4969 if (lzc_exists(name)) 4970 fnvlist_add_string(ha->nvl, name, ha->tag); 4971 4972 if (ha->recursive) 4973 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_hold_one, ha); 4974 zfs_close(zhp); 4975 return (rv); 4976 } 4977 4978 int 4979 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4980 boolean_t recursive, int cleanup_fd) 4981 { 4982 int ret; 4983 struct holdarg ha; 4984 4985 ha.nvl = fnvlist_alloc(); 4986 ha.snapname = snapname; 4987 ha.tag = tag; 4988 ha.recursive = recursive; 4989 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4990 4991 if (nvlist_empty(ha.nvl)) { 4992 char errbuf[ERRBUFLEN]; 4993 4994 fnvlist_free(ha.nvl); 4995 ret = ENOENT; 4996 (void) snprintf(errbuf, sizeof (errbuf), 4997 dgettext(TEXT_DOMAIN, 4998 "cannot hold snapshot '%s@%s'"), 4999 zhp->zfs_name, snapname); 5000 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 5001 return (ret); 5002 } 5003 5004 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 5005 fnvlist_free(ha.nvl); 5006 5007 return (ret); 5008 } 5009 5010 int 5011 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 5012 { 5013 int ret; 5014 nvlist_t *errors; 5015 libzfs_handle_t *hdl = zhp->zfs_hdl; 5016 char errbuf[ERRBUFLEN]; 5017 nvpair_t *elem; 5018 5019 errors = NULL; 5020 ret = lzc_hold(holds, cleanup_fd, &errors); 5021 5022 if (ret == 0) { 5023 /* There may be errors even in the success case. */ 5024 fnvlist_free(errors); 5025 return (0); 5026 } 5027 5028 if (nvlist_empty(errors)) { 5029 /* no hold-specific errors */ 5030 (void) snprintf(errbuf, sizeof (errbuf), 5031 dgettext(TEXT_DOMAIN, "cannot hold")); 5032 switch (ret) { 5033 case ENOTSUP: 5034 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5035 "pool must be upgraded")); 5036 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 5037 break; 5038 case EINVAL: 5039 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5040 break; 5041 default: 5042 (void) zfs_standard_error(hdl, ret, errbuf); 5043 } 5044 } 5045 5046 for (elem = nvlist_next_nvpair(errors, NULL); 5047 elem != NULL; 5048 elem = nvlist_next_nvpair(errors, elem)) { 5049 (void) snprintf(errbuf, sizeof (errbuf), 5050 dgettext(TEXT_DOMAIN, 5051 "cannot hold snapshot '%s'"), nvpair_name(elem)); 5052 switch (fnvpair_value_int32(elem)) { 5053 case E2BIG: 5054 /* 5055 * Temporary tags wind up having the ds object id 5056 * prepended. So even if we passed the length check 5057 * above, it's still possible for the tag to wind 5058 * up being slightly too long. 5059 */ 5060 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 5061 break; 5062 case EINVAL: 5063 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5064 break; 5065 case EEXIST: 5066 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 5067 break; 5068 default: 5069 (void) zfs_standard_error(hdl, 5070 fnvpair_value_int32(elem), errbuf); 5071 } 5072 } 5073 5074 fnvlist_free(errors); 5075 return (ret); 5076 } 5077 5078 static int 5079 zfs_release_one(zfs_handle_t *zhp, void *arg) 5080 { 5081 struct holdarg *ha = arg; 5082 char name[ZFS_MAX_DATASET_NAME_LEN]; 5083 int rv = 0; 5084 nvlist_t *existing_holds; 5085 5086 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, 5087 ha->snapname) >= sizeof (name)) { 5088 ha->error = EINVAL; 5089 rv = EINVAL; 5090 } 5091 5092 if (lzc_get_holds(name, &existing_holds) != 0) { 5093 ha->error = ENOENT; 5094 } else if (!nvlist_exists(existing_holds, ha->tag)) { 5095 ha->error = ESRCH; 5096 } else { 5097 nvlist_t *torelease = fnvlist_alloc(); 5098 fnvlist_add_boolean(torelease, ha->tag); 5099 fnvlist_add_nvlist(ha->nvl, name, torelease); 5100 fnvlist_free(torelease); 5101 } 5102 5103 if (ha->recursive) 5104 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_release_one, ha); 5105 zfs_close(zhp); 5106 return (rv); 5107 } 5108 5109 int 5110 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 5111 boolean_t recursive) 5112 { 5113 int ret; 5114 struct holdarg ha; 5115 nvlist_t *errors = NULL; 5116 nvpair_t *elem; 5117 libzfs_handle_t *hdl = zhp->zfs_hdl; 5118 char errbuf[ERRBUFLEN]; 5119 5120 ha.nvl = fnvlist_alloc(); 5121 ha.snapname = snapname; 5122 ha.tag = tag; 5123 ha.recursive = recursive; 5124 ha.error = 0; 5125 (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 5126 5127 if (nvlist_empty(ha.nvl)) { 5128 fnvlist_free(ha.nvl); 5129 ret = ha.error; 5130 (void) snprintf(errbuf, sizeof (errbuf), 5131 dgettext(TEXT_DOMAIN, 5132 "cannot release hold from snapshot '%s@%s'"), 5133 zhp->zfs_name, snapname); 5134 if (ret == ESRCH) { 5135 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 5136 } else { 5137 (void) zfs_standard_error(hdl, ret, errbuf); 5138 } 5139 return (ret); 5140 } 5141 5142 ret = lzc_release(ha.nvl, &errors); 5143 fnvlist_free(ha.nvl); 5144 5145 if (ret == 0) { 5146 /* There may be errors even in the success case. */ 5147 fnvlist_free(errors); 5148 return (0); 5149 } 5150 5151 if (nvlist_empty(errors)) { 5152 /* no hold-specific errors */ 5153 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 5154 "cannot release")); 5155 switch (errno) { 5156 case ENOTSUP: 5157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5158 "pool must be upgraded")); 5159 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 5160 break; 5161 default: 5162 (void) zfs_standard_error(hdl, errno, errbuf); 5163 } 5164 } 5165 5166 for (elem = nvlist_next_nvpair(errors, NULL); 5167 elem != NULL; 5168 elem = nvlist_next_nvpair(errors, elem)) { 5169 (void) snprintf(errbuf, sizeof (errbuf), 5170 dgettext(TEXT_DOMAIN, 5171 "cannot release hold from snapshot '%s'"), 5172 nvpair_name(elem)); 5173 switch (fnvpair_value_int32(elem)) { 5174 case ESRCH: 5175 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 5176 break; 5177 case EINVAL: 5178 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5179 break; 5180 default: 5181 (void) zfs_standard_error(hdl, 5182 fnvpair_value_int32(elem), errbuf); 5183 } 5184 } 5185 5186 fnvlist_free(errors); 5187 return (ret); 5188 } 5189 5190 int 5191 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 5192 { 5193 zfs_cmd_t zc = {"\0"}; 5194 libzfs_handle_t *hdl = zhp->zfs_hdl; 5195 int nvsz = 2048; 5196 void *nvbuf; 5197 int err = 0; 5198 char errbuf[ERRBUFLEN]; 5199 5200 assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 5201 zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 5202 5203 tryagain: 5204 5205 nvbuf = malloc(nvsz); 5206 if (nvbuf == NULL) { 5207 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 5208 goto out; 5209 } 5210 5211 zc.zc_nvlist_dst_size = nvsz; 5212 zc.zc_nvlist_dst = (uintptr_t)nvbuf; 5213 5214 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 5215 5216 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) { 5217 (void) snprintf(errbuf, sizeof (errbuf), 5218 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 5219 zc.zc_name); 5220 switch (errno) { 5221 case ENOMEM: 5222 free(nvbuf); 5223 nvsz = zc.zc_nvlist_dst_size; 5224 goto tryagain; 5225 5226 case ENOTSUP: 5227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5228 "pool must be upgraded")); 5229 err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 5230 break; 5231 case EINVAL: 5232 err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 5233 break; 5234 case ENOENT: 5235 err = zfs_error(hdl, EZFS_NOENT, errbuf); 5236 break; 5237 default: 5238 err = zfs_standard_error(hdl, errno, errbuf); 5239 break; 5240 } 5241 } else { 5242 /* success */ 5243 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 5244 if (rc) { 5245 err = zfs_standard_error_fmt(hdl, rc, dgettext( 5246 TEXT_DOMAIN, "cannot get permissions on '%s'"), 5247 zc.zc_name); 5248 } 5249 } 5250 5251 free(nvbuf); 5252 out: 5253 return (err); 5254 } 5255 5256 int 5257 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 5258 { 5259 zfs_cmd_t zc = {"\0"}; 5260 libzfs_handle_t *hdl = zhp->zfs_hdl; 5261 char *nvbuf; 5262 char errbuf[ERRBUFLEN]; 5263 size_t nvsz; 5264 int err; 5265 5266 assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 5267 zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 5268 5269 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 5270 assert(err == 0); 5271 5272 nvbuf = malloc(nvsz); 5273 5274 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 5275 assert(err == 0); 5276 5277 zc.zc_nvlist_src_size = nvsz; 5278 zc.zc_nvlist_src = (uintptr_t)nvbuf; 5279 zc.zc_perm_action = un; 5280 5281 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 5282 5283 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 5284 (void) snprintf(errbuf, sizeof (errbuf), 5285 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 5286 zc.zc_name); 5287 switch (errno) { 5288 case ENOTSUP: 5289 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5290 "pool must be upgraded")); 5291 err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 5292 break; 5293 case EINVAL: 5294 err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 5295 break; 5296 case ENOENT: 5297 err = zfs_error(hdl, EZFS_NOENT, errbuf); 5298 break; 5299 default: 5300 err = zfs_standard_error(hdl, errno, errbuf); 5301 break; 5302 } 5303 } 5304 5305 free(nvbuf); 5306 5307 return (err); 5308 } 5309 5310 int 5311 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 5312 { 5313 int err; 5314 char errbuf[ERRBUFLEN]; 5315 5316 err = lzc_get_holds(zhp->zfs_name, nvl); 5317 5318 if (err != 0) { 5319 libzfs_handle_t *hdl = zhp->zfs_hdl; 5320 5321 (void) snprintf(errbuf, sizeof (errbuf), 5322 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 5323 zhp->zfs_name); 5324 switch (err) { 5325 case ENOTSUP: 5326 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5327 "pool must be upgraded")); 5328 err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 5329 break; 5330 case EINVAL: 5331 err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 5332 break; 5333 case ENOENT: 5334 err = zfs_error(hdl, EZFS_NOENT, errbuf); 5335 break; 5336 default: 5337 err = zfs_standard_error(hdl, errno, errbuf); 5338 break; 5339 } 5340 } 5341 5342 return (err); 5343 } 5344 5345 /* 5346 * The theory of raidz space accounting 5347 * 5348 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block 5349 * will "reference" 128KB, even though it allocates more than that, to store the 5350 * parity information (and perhaps skip sectors). This concept of the 5351 * "referenced" (and other DMU space accounting) being lower than the allocated 5352 * space by a constant factor is called "raidz deflation." 5353 * 5354 * As mentioned above, the constant factor for raidz deflation assumes a 128KB 5355 * block size. However, zvols typically have a much smaller block size (default 5356 * 8KB). These smaller blocks may require proportionally much more parity 5357 * information (and perhaps skip sectors). In this case, the change to the 5358 * "referenced" property may be much more than the logical block size. 5359 * 5360 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written 5361 * as follows. 5362 * 5363 * +-------+-------+-------+-------+-------+ 5364 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5365 * +-------+-------+-------+-------+-------+ 5366 * | P0 | D0 | D8 | D16 | D24 | 5367 * | P1 | D1 | D9 | D17 | D25 | 5368 * | P2 | D2 | D10 | D18 | D26 | 5369 * | P3 | D3 | D11 | D19 | D27 | 5370 * | P4 | D4 | D12 | D20 | D28 | 5371 * | P5 | D5 | D13 | D21 | D29 | 5372 * | P6 | D6 | D14 | D22 | D30 | 5373 * | P7 | D7 | D15 | D23 | D31 | 5374 * +-------+-------+-------+-------+-------+ 5375 * 5376 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data 5377 * sectors. The dataset's referenced will increase by 128k and the pool's 5378 * allocated and free properties will be adjusted by 160k. 5379 * 5380 * A 4k block written to the same raidz vdev will require two 4k sectors. The 5381 * blank cells represent unallocated space. 5382 * 5383 * +-------+-------+-------+-------+-------+ 5384 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5385 * +-------+-------+-------+-------+-------+ 5386 * | P0 | D0 | | | | 5387 * +-------+-------+-------+-------+-------+ 5388 * 5389 * Above, notice that the 4k block required one sector for parity and another 5390 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated 5391 * and free properties will be adjusted by 8k. The dataset will not be charged 5392 * 8k. Rather, it will be charged a value that is scaled according to the 5393 * overhead of the 128k block on the same vdev. This 8k allocation will be 5394 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as 5395 * calculated in the 128k block example above. 5396 * 5397 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That 5398 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2 5399 * allocations are a multiple of 3 sectors, and raidz3 allocations are a 5400 * multiple of of 4 sectors. When a block does not fill the required number of 5401 * sectors, skip blocks (sectors) are used. 5402 * 5403 * An 8k block being written to a raidz vdev may be written as follows: 5404 * 5405 * +-------+-------+-------+-------+-------+ 5406 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5407 * +-------+-------+-------+-------+-------+ 5408 * | P0 | D0 | D1 | S0 | | 5409 * +-------+-------+-------+-------+-------+ 5410 * 5411 * In order to maintain the nparity+1 allocation size, a skip block (S0) was 5412 * added. For this 8k block, the pool's allocated and free properties are 5413 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k / 5414 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in 5415 * the 128k block example above. 5416 * 5417 * The situation is slightly different for dRAID since the minimum allocation 5418 * size is the full group width. The same 8K block above would be written as 5419 * follows in a dRAID group: 5420 * 5421 * +-------+-------+-------+-------+-------+ 5422 * | disk1 | disk2 | disk3 | disk4 | disk5 | 5423 * +-------+-------+-------+-------+-------+ 5424 * | P0 | D0 | D1 | S0 | S1 | 5425 * +-------+-------+-------+-------+-------+ 5426 * 5427 * Compression may lead to a variety of block sizes being written for the same 5428 * volume or file. There is no clear way to reserve just the amount of space 5429 * that will be required, so the worst case (no compression) is assumed. 5430 * Note that metadata blocks will typically be compressed, so the reservation 5431 * size returned by zvol_volsize_to_reservation() will generally be slightly 5432 * larger than the maximum that the volume can reference. 5433 */ 5434 5435 /* 5436 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the 5437 * amount of space (in bytes) that will be allocated for the specified block 5438 * size. Note that the "referenced" space accounted will be less than this, but 5439 * not necessarily equal to "blksize", due to RAIDZ deflation. 5440 */ 5441 static uint64_t 5442 vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift, 5443 uint64_t blksize) 5444 { 5445 uint64_t asize, ndata; 5446 5447 ASSERT3U(ndisks, >, nparity); 5448 ndata = ndisks - nparity; 5449 asize = ((blksize - 1) >> ashift) + 1; 5450 asize += nparity * ((asize + ndata - 1) / ndata); 5451 asize = roundup(asize, nparity + 1) << ashift; 5452 5453 return (asize); 5454 } 5455 5456 /* 5457 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the 5458 * amount of space (in bytes) that will be allocated for the specified block 5459 * size. 5460 */ 5461 static uint64_t 5462 vdev_draid_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift, 5463 uint64_t blksize) 5464 { 5465 ASSERT3U(ndisks, >, nparity); 5466 uint64_t ndata = ndisks - nparity; 5467 uint64_t rows = ((blksize - 1) / (ndata << ashift)) + 1; 5468 uint64_t asize = (rows * ndisks) << ashift; 5469 5470 return (asize); 5471 } 5472 5473 /* 5474 * Determine how much space will be allocated if it lands on the most space- 5475 * inefficient top-level vdev. Returns the size in bytes required to store one 5476 * copy of the volume data. See theory comment above. 5477 */ 5478 static uint64_t 5479 volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize) 5480 { 5481 nvlist_t *config, *tree, **vdevs; 5482 uint_t nvdevs; 5483 uint64_t ret = 0; 5484 5485 config = zpool_get_config(zhp, NULL); 5486 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 || 5487 nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, 5488 &vdevs, &nvdevs) != 0) { 5489 return (nblocks * blksize); 5490 } 5491 5492 for (int v = 0; v < nvdevs; v++) { 5493 const char *type; 5494 uint64_t nparity, ashift, asize, tsize; 5495 uint64_t volsize; 5496 5497 if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE, 5498 &type) != 0) 5499 continue; 5500 5501 if (strcmp(type, VDEV_TYPE_RAIDZ) != 0 && 5502 strcmp(type, VDEV_TYPE_DRAID) != 0) 5503 continue; 5504 5505 if (nvlist_lookup_uint64(vdevs[v], 5506 ZPOOL_CONFIG_NPARITY, &nparity) != 0) 5507 continue; 5508 5509 if (nvlist_lookup_uint64(vdevs[v], 5510 ZPOOL_CONFIG_ASHIFT, &ashift) != 0) 5511 continue; 5512 5513 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) { 5514 nvlist_t **disks; 5515 uint_t ndisks; 5516 5517 if (nvlist_lookup_nvlist_array(vdevs[v], 5518 ZPOOL_CONFIG_CHILDREN, &disks, &ndisks) != 0) 5519 continue; 5520 5521 /* allocation size for the "typical" 128k block */ 5522 tsize = vdev_raidz_asize(ndisks, nparity, ashift, 5523 SPA_OLD_MAXBLOCKSIZE); 5524 5525 /* allocation size for the blksize block */ 5526 asize = vdev_raidz_asize(ndisks, nparity, ashift, 5527 blksize); 5528 } else { 5529 uint64_t ndata; 5530 5531 if (nvlist_lookup_uint64(vdevs[v], 5532 ZPOOL_CONFIG_DRAID_NDATA, &ndata) != 0) 5533 continue; 5534 5535 /* allocation size for the "typical" 128k block */ 5536 tsize = vdev_draid_asize(ndata + nparity, nparity, 5537 ashift, SPA_OLD_MAXBLOCKSIZE); 5538 5539 /* allocation size for the blksize block */ 5540 asize = vdev_draid_asize(ndata + nparity, nparity, 5541 ashift, blksize); 5542 } 5543 5544 /* 5545 * Scale this size down as a ratio of 128k / tsize. 5546 * See theory statement above. 5547 */ 5548 volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize; 5549 if (volsize > ret) { 5550 ret = volsize; 5551 } 5552 } 5553 5554 if (ret == 0) { 5555 ret = nblocks * blksize; 5556 } 5557 5558 return (ret); 5559 } 5560 5561 /* 5562 * Convert the zvol's volume size to an appropriate reservation. See theory 5563 * comment above. 5564 * 5565 * Note: If this routine is updated, it is necessary to update the ZFS test 5566 * suite's shell version in reservation.shlib. 5567 */ 5568 uint64_t 5569 zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize, 5570 nvlist_t *props) 5571 { 5572 uint64_t numdb; 5573 uint64_t nblocks, volblocksize; 5574 int ncopies; 5575 const char *strval; 5576 5577 if (nvlist_lookup_string(props, 5578 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 5579 ncopies = atoi(strval); 5580 else 5581 ncopies = 1; 5582 if (nvlist_lookup_uint64(props, 5583 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 5584 &volblocksize) != 0) 5585 volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 5586 5587 nblocks = volsize / volblocksize; 5588 /* 5589 * Metadata defaults to using 128k blocks, not volblocksize blocks. For 5590 * this reason, only the data blocks are scaled based on vdev config. 5591 */ 5592 volsize = volsize_from_vdevs(zph, nblocks, volblocksize); 5593 5594 /* start with metadnode L0-L6 */ 5595 numdb = 7; 5596 /* calculate number of indirects */ 5597 while (nblocks > 1) { 5598 nblocks += DNODES_PER_LEVEL - 1; 5599 nblocks /= DNODES_PER_LEVEL; 5600 numdb += nblocks; 5601 } 5602 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 5603 volsize *= ncopies; 5604 /* 5605 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 5606 * compressed, but in practice they compress down to about 5607 * 1100 bytes 5608 */ 5609 numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 5610 volsize += numdb; 5611 return (volsize); 5612 } 5613 5614 /* 5615 * Wait for the given activity and return the status of the wait (whether or not 5616 * any waiting was done) in the 'waited' parameter. Non-existent fses are 5617 * reported via the 'missing' parameter, rather than by printing an error 5618 * message. This is convenient when this function is called in a loop over a 5619 * long period of time (as it is, for example, by zfs's wait cmd). In that 5620 * scenario, a fs being exported or destroyed should be considered a normal 5621 * event, so we don't want to print an error when we find that the fs doesn't 5622 * exist. 5623 */ 5624 int 5625 zfs_wait_status(zfs_handle_t *zhp, zfs_wait_activity_t activity, 5626 boolean_t *missing, boolean_t *waited) 5627 { 5628 int error = lzc_wait_fs(zhp->zfs_name, activity, waited); 5629 *missing = (error == ENOENT); 5630 if (*missing) 5631 return (0); 5632 5633 if (error != 0) { 5634 (void) zfs_standard_error_fmt(zhp->zfs_hdl, error, 5635 dgettext(TEXT_DOMAIN, "error waiting in fs '%s'"), 5636 zhp->zfs_name); 5637 } 5638 5639 return (error); 5640 } 5641