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