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