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