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