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