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