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