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