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