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