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