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