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