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