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