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