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