209 210typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *); 211typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *); 212typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *); 213 214typedef enum { 215 NO_NAME, 216 POOL_NAME, 217 DATASET_NAME 218} zfs_ioc_namecheck_t; 219 220typedef enum { 221 POOL_CHECK_NONE = 1 << 0, 222 POOL_CHECK_SUSPENDED = 1 << 1, 223 POOL_CHECK_READONLY = 1 << 2, 224} zfs_ioc_poolcheck_t; 225 226typedef struct zfs_ioc_vec { 227 zfs_ioc_legacy_func_t *zvec_legacy_func; 228 zfs_ioc_func_t *zvec_func; 229 zfs_secpolicy_func_t *zvec_secpolicy; 230 zfs_ioc_namecheck_t zvec_namecheck; 231 boolean_t zvec_allow_log; 232 zfs_ioc_poolcheck_t zvec_pool_check; 233 boolean_t zvec_smush_outnvlist; 234 const char *zvec_name; 235} zfs_ioc_vec_t; 236 237/* This array is indexed by zfs_userquota_prop_t */ 238static const char *userquota_perms[] = { 239 ZFS_DELEG_PERM_USERUSED, 240 ZFS_DELEG_PERM_USERQUOTA, 241 ZFS_DELEG_PERM_GROUPUSED, 242 ZFS_DELEG_PERM_GROUPQUOTA, 243}; 244 245static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 246static int zfs_check_settable(const char *name, nvpair_t *property, 247 cred_t *cr); 248static int zfs_check_clearable(char *dataset, nvlist_t *props, 249 nvlist_t **errors); 250static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 251 boolean_t *); 252int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *); 253static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp); 254 255static void zfsdev_close(void *data); 256 257static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature); 258 259/* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 260void 261__dprintf(const char *file, const char *func, int line, const char *fmt, ...) 262{ 263 const char *newfile; 264 char buf[512]; 265 va_list adx; 266 267 /* 268 * Get rid of annoying "../common/" prefix to filename. 269 */ 270 newfile = strrchr(file, '/'); 271 if (newfile != NULL) { 272 newfile = newfile + 1; /* Get rid of leading / */ 273 } else { 274 newfile = file; 275 } 276 277 va_start(adx, fmt); 278 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 279 va_end(adx); 280 281 /* 282 * To get this data, use the zfs-dprintf probe as so: 283 * dtrace -q -n 'zfs-dprintf \ 284 * /stringof(arg0) == "dbuf.c"/ \ 285 * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 286 * arg0 = file name 287 * arg1 = function name 288 * arg2 = line number 289 * arg3 = message 290 */ 291 DTRACE_PROBE4(zfs__dprintf, 292 char *, newfile, char *, func, int, line, char *, buf); 293} 294 295static void 296history_str_free(char *buf) 297{ 298 kmem_free(buf, HIS_MAX_RECORD_LEN); 299} 300 301static char * 302history_str_get(zfs_cmd_t *zc) 303{ 304 char *buf; 305 306 if (zc->zc_history == 0) 307 return (NULL); 308 309 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 310 if (copyinstr((void *)(uintptr_t)zc->zc_history, 311 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 312 history_str_free(buf); 313 return (NULL); 314 } 315 316 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 317 318 return (buf); 319} 320 321/* 322 * Check to see if the named dataset is currently defined as bootable 323 */ 324static boolean_t 325zfs_is_bootfs(const char *name) 326{ 327 objset_t *os; 328 329 if (dmu_objset_hold(name, FTAG, &os) == 0) { 330 boolean_t ret; 331 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 332 dmu_objset_rele(os, FTAG); 333 return (ret); 334 } 335 return (B_FALSE); 336} 337 338/* 339 * Return non-zero if the spa version is less than requested version. 340 */ 341static int 342zfs_earlier_version(const char *name, int version) 343{ 344 spa_t *spa; 345 346 if (spa_open(name, &spa, FTAG) == 0) { 347 if (spa_version(spa) < version) { 348 spa_close(spa, FTAG); 349 return (1); 350 } 351 spa_close(spa, FTAG); 352 } 353 return (0); 354} 355 356/* 357 * Return TRUE if the ZPL version is less than requested version. 358 */ 359static boolean_t 360zpl_earlier_version(const char *name, int version) 361{ 362 objset_t *os; 363 boolean_t rc = B_TRUE; 364 365 if (dmu_objset_hold(name, FTAG, &os) == 0) { 366 uint64_t zplversion; 367 368 if (dmu_objset_type(os) != DMU_OST_ZFS) { 369 dmu_objset_rele(os, FTAG); 370 return (B_TRUE); 371 } 372 /* XXX reading from non-owned objset */ 373 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 374 rc = zplversion < version; 375 dmu_objset_rele(os, FTAG); 376 } 377 return (rc); 378} 379 380static void 381zfs_log_history(zfs_cmd_t *zc) 382{ 383 spa_t *spa; 384 char *buf; 385 386 if ((buf = history_str_get(zc)) == NULL) 387 return; 388 389 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 390 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 391 (void) spa_history_log(spa, buf); 392 spa_close(spa, FTAG); 393 } 394 history_str_free(buf); 395} 396 397/* 398 * Policy for top-level read operations (list pools). Requires no privileges, 399 * and can be used in the local zone, as there is no associated dataset. 400 */ 401/* ARGSUSED */ 402static int 403zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 404{ 405 return (0); 406} 407 408/* 409 * Policy for dataset read operations (list children, get statistics). Requires 410 * no privileges, but must be visible in the local zone. 411 */ 412/* ARGSUSED */ 413static int 414zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 415{ 416 if (INGLOBALZONE(curthread) || 417 zone_dataset_visible(zc->zc_name, NULL)) 418 return (0); 419 420 return (SET_ERROR(ENOENT)); 421} 422 423static int 424zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) 425{ 426 int writable = 1; 427 428 /* 429 * The dataset must be visible by this zone -- check this first 430 * so they don't see EPERM on something they shouldn't know about. 431 */ 432 if (!INGLOBALZONE(curthread) && 433 !zone_dataset_visible(dataset, &writable)) 434 return (SET_ERROR(ENOENT)); 435 436 if (INGLOBALZONE(curthread)) { 437 /* 438 * If the fs is zoned, only root can access it from the 439 * global zone. 440 */ 441 if (secpolicy_zfs(cr) && zoned) 442 return (SET_ERROR(EPERM)); 443 } else { 444 /* 445 * If we are in a local zone, the 'zoned' property must be set. 446 */ 447 if (!zoned) 448 return (SET_ERROR(EPERM)); 449 450 /* must be writable by this zone */ 451 if (!writable) 452 return (SET_ERROR(EPERM)); 453 } 454 return (0); 455} 456 457static int 458zfs_dozonecheck(const char *dataset, cred_t *cr) 459{ 460 uint64_t zoned; 461 462 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL)) 463 return (SET_ERROR(ENOENT)); 464 465 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 466} 467 468static int 469zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) 470{ 471 uint64_t zoned; 472 473 if (dsl_prop_get_int_ds(ds, "jailed", &zoned)) 474 return (SET_ERROR(ENOENT)); 475 476 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 477} 478 479static int 480zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, 481 const char *perm, cred_t *cr) 482{ 483 int error; 484 485 error = zfs_dozonecheck_ds(name, ds, cr); 486 if (error == 0) { 487 error = secpolicy_zfs(cr); 488 if (error != 0) 489 error = dsl_deleg_access_impl(ds, perm, cr); 490 } 491 return (error); 492} 493 494static int 495zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 496{ 497 int error; 498 dsl_dataset_t *ds; 499 dsl_pool_t *dp; 500 501 error = dsl_pool_hold(name, FTAG, &dp); 502 if (error != 0) 503 return (error); 504 505 error = dsl_dataset_hold(dp, name, FTAG, &ds); 506 if (error != 0) { 507 dsl_pool_rele(dp, FTAG); 508 return (error); 509 } 510 511 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr); 512 513 dsl_dataset_rele(ds, FTAG); 514 dsl_pool_rele(dp, FTAG); 515 return (error); 516} 517 518#ifdef SECLABEL 519/* 520 * Policy for setting the security label property. 521 * 522 * Returns 0 for success, non-zero for access and other errors. 523 */ 524static int 525zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 526{ 527 char ds_hexsl[MAXNAMELEN]; 528 bslabel_t ds_sl, new_sl; 529 boolean_t new_default = FALSE; 530 uint64_t zoned; 531 int needed_priv = -1; 532 int error; 533 534 /* First get the existing dataset label. */ 535 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 536 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 537 if (error != 0) 538 return (SET_ERROR(EPERM)); 539 540 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 541 new_default = TRUE; 542 543 /* The label must be translatable */ 544 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 545 return (SET_ERROR(EINVAL)); 546 547 /* 548 * In a non-global zone, disallow attempts to set a label that 549 * doesn't match that of the zone; otherwise no other checks 550 * are needed. 551 */ 552 if (!INGLOBALZONE(curproc)) { 553 if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 554 return (SET_ERROR(EPERM)); 555 return (0); 556 } 557 558 /* 559 * For global-zone datasets (i.e., those whose zoned property is 560 * "off", verify that the specified new label is valid for the 561 * global zone. 562 */ 563 if (dsl_prop_get_integer(name, 564 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 565 return (SET_ERROR(EPERM)); 566 if (!zoned) { 567 if (zfs_check_global_label(name, strval) != 0) 568 return (SET_ERROR(EPERM)); 569 } 570 571 /* 572 * If the existing dataset label is nondefault, check if the 573 * dataset is mounted (label cannot be changed while mounted). 574 * Get the zfsvfs; if there isn't one, then the dataset isn't 575 * mounted (or isn't a dataset, doesn't exist, ...). 576 */ 577 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 578 objset_t *os; 579 static char *setsl_tag = "setsl_tag"; 580 581 /* 582 * Try to own the dataset; abort if there is any error, 583 * (e.g., already mounted, in use, or other error). 584 */ 585 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 586 setsl_tag, &os); 587 if (error != 0) 588 return (SET_ERROR(EPERM)); 589 590 dmu_objset_disown(os, setsl_tag); 591 592 if (new_default) { 593 needed_priv = PRIV_FILE_DOWNGRADE_SL; 594 goto out_check; 595 } 596 597 if (hexstr_to_label(strval, &new_sl) != 0) 598 return (SET_ERROR(EPERM)); 599 600 if (blstrictdom(&ds_sl, &new_sl)) 601 needed_priv = PRIV_FILE_DOWNGRADE_SL; 602 else if (blstrictdom(&new_sl, &ds_sl)) 603 needed_priv = PRIV_FILE_UPGRADE_SL; 604 } else { 605 /* dataset currently has a default label */ 606 if (!new_default) 607 needed_priv = PRIV_FILE_UPGRADE_SL; 608 } 609 610out_check: 611 if (needed_priv != -1) 612 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 613 return (0); 614} 615#endif /* SECLABEL */ 616 617static int 618zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 619 cred_t *cr) 620{ 621 char *strval; 622 623 /* 624 * Check permissions for special properties. 625 */ 626 switch (prop) { 627 case ZFS_PROP_ZONED: 628 /* 629 * Disallow setting of 'zoned' from within a local zone. 630 */ 631 if (!INGLOBALZONE(curthread)) 632 return (SET_ERROR(EPERM)); 633 break; 634 635 case ZFS_PROP_QUOTA: 636 case ZFS_PROP_FILESYSTEM_LIMIT: 637 case ZFS_PROP_SNAPSHOT_LIMIT: 638 if (!INGLOBALZONE(curthread)) { 639 uint64_t zoned; 640 char setpoint[MAXNAMELEN]; 641 /* 642 * Unprivileged users are allowed to modify the 643 * limit on things *under* (ie. contained by) 644 * the thing they own. 645 */ 646 if (dsl_prop_get_integer(dsname, "jailed", &zoned, 647 setpoint)) 648 return (SET_ERROR(EPERM)); 649 if (!zoned || strlen(dsname) <= strlen(setpoint)) 650 return (SET_ERROR(EPERM)); 651 } 652 break; 653 654 case ZFS_PROP_MLSLABEL: 655#ifdef SECLABEL 656 if (!is_system_labeled()) 657 return (SET_ERROR(EPERM)); 658 659 if (nvpair_value_string(propval, &strval) == 0) { 660 int err; 661 662 err = zfs_set_slabel_policy(dsname, strval, CRED()); 663 if (err != 0) 664 return (err); 665 } 666#else 667 return (EOPNOTSUPP); 668#endif 669 break; 670 } 671 672 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 673} 674 675/* ARGSUSED */ 676static int 677zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 678{ 679 int error; 680 681 error = zfs_dozonecheck(zc->zc_name, cr); 682 if (error != 0) 683 return (error); 684 685 /* 686 * permission to set permissions will be evaluated later in 687 * dsl_deleg_can_allow() 688 */ 689 return (0); 690} 691 692/* ARGSUSED */ 693static int 694zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 695{ 696 return (zfs_secpolicy_write_perms(zc->zc_name, 697 ZFS_DELEG_PERM_ROLLBACK, cr)); 698} 699 700/* ARGSUSED */ 701static int 702zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 703{ 704 dsl_pool_t *dp; 705 dsl_dataset_t *ds; 706 char *cp; 707 int error; 708 709 /* 710 * Generate the current snapshot name from the given objsetid, then 711 * use that name for the secpolicy/zone checks. 712 */ 713 cp = strchr(zc->zc_name, '@'); 714 if (cp == NULL) 715 return (SET_ERROR(EINVAL)); 716 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 717 if (error != 0) 718 return (error); 719 720 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 721 if (error != 0) { 722 dsl_pool_rele(dp, FTAG); 723 return (error); 724 } 725 726 dsl_dataset_name(ds, zc->zc_name); 727 728 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, 729 ZFS_DELEG_PERM_SEND, cr); 730 dsl_dataset_rele(ds, FTAG); 731 dsl_pool_rele(dp, FTAG); 732 733 return (error); 734} 735 736/* ARGSUSED */ 737static int 738zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 739{ 740 return (zfs_secpolicy_write_perms(zc->zc_name, 741 ZFS_DELEG_PERM_SEND, cr)); 742} 743 744/* ARGSUSED */ 745static int 746zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 747{ 748 vnode_t *vp; 749 int error; 750 751 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 752 NO_FOLLOW, NULL, &vp)) != 0) 753 return (error); 754 755 /* Now make sure mntpnt and dataset are ZFS */ 756 757 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 || 758 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 759 zc->zc_name) != 0)) { 760 VN_RELE(vp); 761 return (SET_ERROR(EPERM)); 762 } 763 764 VN_RELE(vp); 765 return (dsl_deleg_access(zc->zc_name, 766 ZFS_DELEG_PERM_SHARE, cr)); 767} 768 769int 770zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 771{ 772 if (!INGLOBALZONE(curthread)) 773 return (SET_ERROR(EPERM)); 774 775 if (secpolicy_nfs(cr) == 0) { 776 return (0); 777 } else { 778 return (zfs_secpolicy_deleg_share(zc, innvl, cr)); 779 } 780} 781 782int 783zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 784{ 785 if (!INGLOBALZONE(curthread)) 786 return (SET_ERROR(EPERM)); 787 788 if (secpolicy_smb(cr) == 0) { 789 return (0); 790 } else { 791 return (zfs_secpolicy_deleg_share(zc, innvl, cr)); 792 } 793} 794 795static int 796zfs_get_parent(const char *datasetname, char *parent, int parentsize) 797{ 798 char *cp; 799 800 /* 801 * Remove the @bla or /bla from the end of the name to get the parent. 802 */ 803 (void) strncpy(parent, datasetname, parentsize); 804 cp = strrchr(parent, '@'); 805 if (cp != NULL) { 806 cp[0] = '\0'; 807 } else { 808 cp = strrchr(parent, '/'); 809 if (cp == NULL) 810 return (SET_ERROR(ENOENT)); 811 cp[0] = '\0'; 812 } 813 814 return (0); 815} 816 817int 818zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 819{ 820 int error; 821 822 if ((error = zfs_secpolicy_write_perms(name, 823 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 824 return (error); 825 826 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 827} 828 829/* ARGSUSED */ 830static int 831zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 832{ 833 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 834} 835 836/* 837 * Destroying snapshots with delegated permissions requires 838 * descendant mount and destroy permissions. 839 */ 840/* ARGSUSED */ 841static int 842zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 843{ 844 nvlist_t *snaps; 845 nvpair_t *pair, *nextpair; 846 int error = 0; 847 848 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 849 return (SET_ERROR(EINVAL)); 850 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 851 pair = nextpair) { 852 nextpair = nvlist_next_nvpair(snaps, pair); 853 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr); 854 if (error == ENOENT) { 855 /* 856 * Ignore any snapshots that don't exist (we consider 857 * them "already destroyed"). Remove the name from the 858 * nvl here in case the snapshot is created between 859 * now and when we try to destroy it (in which case 860 * we don't want to destroy it since we haven't 861 * checked for permission). 862 */ 863 fnvlist_remove_nvpair(snaps, pair); 864 error = 0; 865 } 866 if (error != 0) 867 break; 868 } 869 870 return (error); 871} 872 873int 874zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 875{ 876 char parentname[MAXNAMELEN]; 877 int error; 878 879 if ((error = zfs_secpolicy_write_perms(from, 880 ZFS_DELEG_PERM_RENAME, cr)) != 0) 881 return (error); 882 883 if ((error = zfs_secpolicy_write_perms(from, 884 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 885 return (error); 886 887 if ((error = zfs_get_parent(to, parentname, 888 sizeof (parentname))) != 0) 889 return (error); 890 891 if ((error = zfs_secpolicy_write_perms(parentname, 892 ZFS_DELEG_PERM_CREATE, cr)) != 0) 893 return (error); 894 895 if ((error = zfs_secpolicy_write_perms(parentname, 896 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 897 return (error); 898 899 return (error); 900} 901 902/* ARGSUSED */ 903static int 904zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 905{ 906 char *at = NULL; 907 int error; 908 909 if ((zc->zc_cookie & 1) != 0) { 910 /* 911 * This is recursive rename, so the starting snapshot might 912 * not exist. Check file system or volume permission instead. 913 */ 914 at = strchr(zc->zc_name, '@'); 915 if (at == NULL) 916 return (EINVAL); 917 *at = '\0'; 918 } 919 920 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr); 921 922 if (at != NULL) 923 *at = '@'; 924 925 return (error); 926} 927 928/* ARGSUSED */ 929static int 930zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 931{ 932 dsl_pool_t *dp; 933 dsl_dataset_t *clone; 934 int error; 935 936 error = zfs_secpolicy_write_perms(zc->zc_name, 937 ZFS_DELEG_PERM_PROMOTE, cr); 938 if (error != 0) 939 return (error); 940 941 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 942 if (error != 0) 943 return (error); 944 945 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone); 946 947 if (error == 0) { 948 char parentname[MAXNAMELEN]; 949 dsl_dataset_t *origin = NULL; 950 dsl_dir_t *dd; 951 dd = clone->ds_dir; 952 953 error = dsl_dataset_hold_obj(dd->dd_pool, 954 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin); 955 if (error != 0) { 956 dsl_dataset_rele(clone, FTAG); 957 dsl_pool_rele(dp, FTAG); 958 return (error); 959 } 960 961 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone, 962 ZFS_DELEG_PERM_MOUNT, cr); 963 964 dsl_dataset_name(origin, parentname); 965 if (error == 0) { 966 error = zfs_secpolicy_write_perms_ds(parentname, origin, 967 ZFS_DELEG_PERM_PROMOTE, cr); 968 } 969 dsl_dataset_rele(clone, FTAG); 970 dsl_dataset_rele(origin, FTAG); 971 } 972 dsl_pool_rele(dp, FTAG); 973 return (error); 974} 975 976/* ARGSUSED */ 977static int 978zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 979{ 980 int error; 981 982 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 983 ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 984 return (error); 985 986 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 987 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 988 return (error); 989 990 return (zfs_secpolicy_write_perms(zc->zc_name, 991 ZFS_DELEG_PERM_CREATE, cr)); 992} 993 994int 995zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 996{ 997 return (zfs_secpolicy_write_perms(name, 998 ZFS_DELEG_PERM_SNAPSHOT, cr)); 999} 1000 1001/* 1002 * Check for permission to create each snapshot in the nvlist. 1003 */ 1004/* ARGSUSED */ 1005static int 1006zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1007{ 1008 nvlist_t *snaps; 1009 int error; 1010 nvpair_t *pair; 1011 1012 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 1013 return (SET_ERROR(EINVAL)); 1014 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 1015 pair = nvlist_next_nvpair(snaps, pair)) { 1016 char *name = nvpair_name(pair); 1017 char *atp = strchr(name, '@'); 1018 1019 if (atp == NULL) { 1020 error = SET_ERROR(EINVAL); 1021 break; 1022 } 1023 *atp = '\0'; 1024 error = zfs_secpolicy_snapshot_perms(name, cr); 1025 *atp = '@'; 1026 if (error != 0) 1027 break; 1028 } 1029 return (error); 1030} 1031 1032/* 1033 * Check for permission to create each snapshot in the nvlist. 1034 */ 1035/* ARGSUSED */ 1036static int 1037zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1038{ 1039 int error = 0; 1040 1041 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 1042 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 1043 char *name = nvpair_name(pair); 1044 char *hashp = strchr(name, '#'); 1045 1046 if (hashp == NULL) { 1047 error = SET_ERROR(EINVAL); 1048 break; 1049 } 1050 *hashp = '\0'; 1051 error = zfs_secpolicy_write_perms(name, 1052 ZFS_DELEG_PERM_BOOKMARK, cr); 1053 *hashp = '#'; 1054 if (error != 0) 1055 break; 1056 } 1057 return (error); 1058} 1059 1060/* ARGSUSED */ 1061static int 1062zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1063{ 1064 nvpair_t *pair, *nextpair; 1065 int error = 0; 1066 1067 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; 1068 pair = nextpair) { 1069 char *name = nvpair_name(pair); 1070 char *hashp = strchr(name, '#'); 1071 nextpair = nvlist_next_nvpair(innvl, pair); 1072 1073 if (hashp == NULL) { 1074 error = SET_ERROR(EINVAL); 1075 break; 1076 } 1077 1078 *hashp = '\0'; 1079 error = zfs_secpolicy_write_perms(name, 1080 ZFS_DELEG_PERM_DESTROY, cr); 1081 *hashp = '#'; 1082 if (error == ENOENT) { 1083 /* 1084 * Ignore any filesystems that don't exist (we consider 1085 * their bookmarks "already destroyed"). Remove 1086 * the name from the nvl here in case the filesystem 1087 * is created between now and when we try to destroy 1088 * the bookmark (in which case we don't want to 1089 * destroy it since we haven't checked for permission). 1090 */ 1091 fnvlist_remove_nvpair(innvl, pair); 1092 error = 0; 1093 } 1094 if (error != 0) 1095 break; 1096 } 1097 1098 return (error); 1099} 1100 1101/* ARGSUSED */ 1102static int 1103zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1104{ 1105 /* 1106 * Even root must have a proper TSD so that we know what pool 1107 * to log to. 1108 */ 1109 if (tsd_get(zfs_allow_log_key) == NULL) 1110 return (SET_ERROR(EPERM)); 1111 return (0); 1112} 1113 1114static int 1115zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1116{ 1117 char parentname[MAXNAMELEN]; 1118 int error; 1119 char *origin; 1120 1121 if ((error = zfs_get_parent(zc->zc_name, parentname, 1122 sizeof (parentname))) != 0) 1123 return (error); 1124 1125 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 && 1126 (error = zfs_secpolicy_write_perms(origin, 1127 ZFS_DELEG_PERM_CLONE, cr)) != 0) 1128 return (error); 1129 1130 if ((error = zfs_secpolicy_write_perms(parentname, 1131 ZFS_DELEG_PERM_CREATE, cr)) != 0) 1132 return (error); 1133 1134 return (zfs_secpolicy_write_perms(parentname, 1135 ZFS_DELEG_PERM_MOUNT, cr)); 1136} 1137 1138/* 1139 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 1140 * SYS_CONFIG privilege, which is not available in a local zone. 1141 */ 1142/* ARGSUSED */ 1143static int 1144zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1145{ 1146 if (secpolicy_sys_config(cr, B_FALSE) != 0) 1147 return (SET_ERROR(EPERM)); 1148 1149 return (0); 1150} 1151 1152/* 1153 * Policy for object to name lookups. 1154 */ 1155/* ARGSUSED */ 1156static int 1157zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1158{ 1159 int error; 1160 1161 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0) 1162 return (0); 1163 1164 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); 1165 return (error); 1166} 1167 1168/* 1169 * Policy for fault injection. Requires all privileges. 1170 */ 1171/* ARGSUSED */ 1172static int 1173zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1174{ 1175 return (secpolicy_zinject(cr)); 1176} 1177 1178/* ARGSUSED */ 1179static int 1180zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1181{ 1182 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 1183 1184 if (prop == ZPROP_INVAL) { 1185 if (!zfs_prop_user(zc->zc_value)) 1186 return (SET_ERROR(EINVAL)); 1187 return (zfs_secpolicy_write_perms(zc->zc_name, 1188 ZFS_DELEG_PERM_USERPROP, cr)); 1189 } else { 1190 return (zfs_secpolicy_setprop(zc->zc_name, prop, 1191 NULL, cr)); 1192 } 1193} 1194 1195static int 1196zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1197{ 1198 int err = zfs_secpolicy_read(zc, innvl, cr); 1199 if (err) 1200 return (err); 1201 1202 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1203 return (SET_ERROR(EINVAL)); 1204 1205 if (zc->zc_value[0] == 0) { 1206 /* 1207 * They are asking about a posix uid/gid. If it's 1208 * themself, allow it. 1209 */ 1210 if (zc->zc_objset_type == ZFS_PROP_USERUSED || 1211 zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 1212 if (zc->zc_guid == crgetuid(cr)) 1213 return (0); 1214 } else { 1215 if (groupmember(zc->zc_guid, cr)) 1216 return (0); 1217 } 1218 } 1219 1220 return (zfs_secpolicy_write_perms(zc->zc_name, 1221 userquota_perms[zc->zc_objset_type], cr)); 1222} 1223 1224static int 1225zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1226{ 1227 int err = zfs_secpolicy_read(zc, innvl, cr); 1228 if (err) 1229 return (err); 1230 1231 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1232 return (SET_ERROR(EINVAL)); 1233 1234 return (zfs_secpolicy_write_perms(zc->zc_name, 1235 userquota_perms[zc->zc_objset_type], cr)); 1236} 1237 1238/* ARGSUSED */ 1239static int 1240zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1241{ 1242 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 1243 NULL, cr)); 1244} 1245 1246/* ARGSUSED */ 1247static int 1248zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1249{ 1250 nvpair_t *pair; 1251 nvlist_t *holds; 1252 int error; 1253 1254 error = nvlist_lookup_nvlist(innvl, "holds", &holds); 1255 if (error != 0) 1256 return (SET_ERROR(EINVAL)); 1257 1258 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 1259 pair = nvlist_next_nvpair(holds, pair)) { 1260 char fsname[MAXNAMELEN]; 1261 error = dmu_fsname(nvpair_name(pair), fsname); 1262 if (error != 0) 1263 return (error); 1264 error = zfs_secpolicy_write_perms(fsname, 1265 ZFS_DELEG_PERM_HOLD, cr); 1266 if (error != 0) 1267 return (error); 1268 } 1269 return (0); 1270} 1271 1272/* ARGSUSED */ 1273static int 1274zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1275{ 1276 nvpair_t *pair; 1277 int error; 1278 1279 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; 1280 pair = nvlist_next_nvpair(innvl, pair)) { 1281 char fsname[MAXNAMELEN]; 1282 error = dmu_fsname(nvpair_name(pair), fsname); 1283 if (error != 0) 1284 return (error); 1285 error = zfs_secpolicy_write_perms(fsname, 1286 ZFS_DELEG_PERM_RELEASE, cr); 1287 if (error != 0) 1288 return (error); 1289 } 1290 return (0); 1291} 1292 1293/* 1294 * Policy for allowing temporary snapshots to be taken or released 1295 */ 1296static int 1297zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1298{ 1299 /* 1300 * A temporary snapshot is the same as a snapshot, 1301 * hold, destroy and release all rolled into one. 1302 * Delegated diff alone is sufficient that we allow this. 1303 */ 1304 int error; 1305 1306 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 1307 ZFS_DELEG_PERM_DIFF, cr)) == 0) 1308 return (0); 1309 1310 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr); 1311 if (error == 0) 1312 error = zfs_secpolicy_hold(zc, innvl, cr); 1313 if (error == 0) 1314 error = zfs_secpolicy_release(zc, innvl, cr); 1315 if (error == 0) 1316 error = zfs_secpolicy_destroy(zc, innvl, cr); 1317 return (error); 1318} 1319 1320/* 1321 * Returns the nvlist as specified by the user in the zfs_cmd_t. 1322 */ 1323static int 1324get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 1325{ 1326 char *packed; 1327 int error; 1328 nvlist_t *list = NULL; 1329 1330 /* 1331 * Read in and unpack the user-supplied nvlist. 1332 */ 1333 if (size == 0) 1334 return (SET_ERROR(EINVAL)); 1335 1336 packed = kmem_alloc(size, KM_SLEEP); 1337 1338 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 1339 iflag)) != 0) { 1340 kmem_free(packed, size); 1341 return (error); 1342 } 1343 1344 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 1345 kmem_free(packed, size); 1346 return (error); 1347 } 1348 1349 kmem_free(packed, size); 1350 1351 *nvp = list; 1352 return (0); 1353} 1354 1355/* 1356 * Reduce the size of this nvlist until it can be serialized in 'max' bytes. 1357 * Entries will be removed from the end of the nvlist, and one int32 entry 1358 * named "N_MORE_ERRORS" will be added indicating how many entries were 1359 * removed. 1360 */ 1361static int 1362nvlist_smush(nvlist_t *errors, size_t max) 1363{ 1364 size_t size; 1365 1366 size = fnvlist_size(errors); 1367 1368 if (size > max) { 1369 nvpair_t *more_errors; 1370 int n = 0; 1371 1372 if (max < 1024) 1373 return (SET_ERROR(ENOMEM)); 1374 1375 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0); 1376 more_errors = nvlist_prev_nvpair(errors, NULL); 1377 1378 do { 1379 nvpair_t *pair = nvlist_prev_nvpair(errors, 1380 more_errors); 1381 fnvlist_remove_nvpair(errors, pair); 1382 n++; 1383 size = fnvlist_size(errors); 1384 } while (size > max); 1385 1386 fnvlist_remove_nvpair(errors, more_errors); 1387 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n); 1388 ASSERT3U(fnvlist_size(errors), <=, max); 1389 } 1390 1391 return (0); 1392} 1393 1394static int 1395put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 1396{ 1397 char *packed = NULL; 1398 int error = 0; 1399 size_t size; 1400 1401 size = fnvlist_size(nvl); 1402 1403 if (size > zc->zc_nvlist_dst_size) { 1404 /* 1405 * Solaris returns ENOMEM here, because even if an error is 1406 * returned from an ioctl(2), new zc_nvlist_dst_size will be 1407 * passed to the userland. This is not the case for FreeBSD. 1408 * We need to return 0, so the kernel will copy the 1409 * zc_nvlist_dst_size back and the userland can discover that a 1410 * bigger buffer is needed. 1411 */ 1412 error = 0; 1413 } else { 1414 packed = fnvlist_pack(nvl, &size); 1415 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 1416 size, zc->zc_iflags) != 0) 1417 error = SET_ERROR(EFAULT); 1418 fnvlist_pack_free(packed, size); 1419 } 1420 1421 zc->zc_nvlist_dst_size = size; 1422 zc->zc_nvlist_dst_filled = B_TRUE; 1423 return (error); 1424} 1425 1426static int 1427getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 1428{ 1429 objset_t *os; 1430 int error; 1431 1432 error = dmu_objset_hold(dsname, FTAG, &os); 1433 if (error != 0) 1434 return (error); 1435 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1436 dmu_objset_rele(os, FTAG); 1437 return (SET_ERROR(EINVAL)); 1438 } 1439 1440 mutex_enter(&os->os_user_ptr_lock); 1441 *zfvp = dmu_objset_get_user(os); 1442 if (*zfvp) { 1443 VFS_HOLD((*zfvp)->z_vfs); 1444 } else { 1445 error = SET_ERROR(ESRCH); 1446 } 1447 mutex_exit(&os->os_user_ptr_lock); 1448 dmu_objset_rele(os, FTAG); 1449 return (error); 1450} 1451 1452/* 1453 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 1454 * case its z_vfs will be NULL, and it will be opened as the owner. 1455 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER, 1456 * which prevents all vnode ops from running. 1457 */ 1458static int 1459zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer) 1460{ 1461 int error = 0; 1462 1463 if (getzfsvfs(name, zfvp) != 0) 1464 error = zfsvfs_create(name, zfvp); 1465 if (error == 0) { 1466 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER : 1467 RW_READER, tag); 1468 if ((*zfvp)->z_unmounted) { 1469 /* 1470 * XXX we could probably try again, since the unmounting 1471 * thread should be just about to disassociate the 1472 * objset from the zfsvfs. 1473 */ 1474 rrm_exit(&(*zfvp)->z_teardown_lock, tag); 1475 return (SET_ERROR(EBUSY)); 1476 } 1477 } 1478 return (error); 1479} 1480 1481static void 1482zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 1483{ 1484 rrm_exit(&zfsvfs->z_teardown_lock, tag); 1485 1486 if (zfsvfs->z_vfs) { 1487 VFS_RELE(zfsvfs->z_vfs); 1488 } else { 1489 dmu_objset_disown(zfsvfs->z_os, zfsvfs); 1490 zfsvfs_free(zfsvfs); 1491 } 1492} 1493 1494static int 1495zfs_ioc_pool_create(zfs_cmd_t *zc) 1496{ 1497 int error; 1498 nvlist_t *config, *props = NULL; 1499 nvlist_t *rootprops = NULL; 1500 nvlist_t *zplprops = NULL; 1501 1502 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1503 zc->zc_iflags, &config)) 1504 return (error); 1505 1506 if (zc->zc_nvlist_src_size != 0 && (error = 1507 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1508 zc->zc_iflags, &props))) { 1509 nvlist_free(config); 1510 return (error); 1511 } 1512 1513 if (props) { 1514 nvlist_t *nvl = NULL; 1515 uint64_t version = SPA_VERSION; 1516 1517 (void) nvlist_lookup_uint64(props, 1518 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 1519 if (!SPA_VERSION_IS_SUPPORTED(version)) { 1520 error = SET_ERROR(EINVAL); 1521 goto pool_props_bad; 1522 } 1523 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 1524 if (nvl) { 1525 error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 1526 if (error != 0) { 1527 nvlist_free(config); 1528 nvlist_free(props); 1529 return (error); 1530 } 1531 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 1532 } 1533 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1534 error = zfs_fill_zplprops_root(version, rootprops, 1535 zplprops, NULL); 1536 if (error != 0) 1537 goto pool_props_bad; 1538 } 1539 1540 error = spa_create(zc->zc_name, config, props, zplprops); 1541 1542 /* 1543 * Set the remaining root properties 1544 */ 1545 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 1546 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 1547 (void) spa_destroy(zc->zc_name); 1548 1549pool_props_bad: 1550 nvlist_free(rootprops); 1551 nvlist_free(zplprops); 1552 nvlist_free(config); 1553 nvlist_free(props); 1554 1555 return (error); 1556} 1557 1558static int 1559zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1560{ 1561 int error; 1562 zfs_log_history(zc); 1563 error = spa_destroy(zc->zc_name); 1564 if (error == 0) 1565 zvol_remove_minors(zc->zc_name); 1566 return (error); 1567} 1568 1569static int 1570zfs_ioc_pool_import(zfs_cmd_t *zc) 1571{ 1572 nvlist_t *config, *props = NULL; 1573 uint64_t guid; 1574 int error; 1575 1576 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1577 zc->zc_iflags, &config)) != 0) 1578 return (error); 1579 1580 if (zc->zc_nvlist_src_size != 0 && (error = 1581 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1582 zc->zc_iflags, &props))) { 1583 nvlist_free(config); 1584 return (error); 1585 } 1586 1587 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1588 guid != zc->zc_guid) 1589 error = SET_ERROR(EINVAL); 1590 else 1591 error = spa_import(zc->zc_name, config, props, zc->zc_cookie); 1592 1593 if (zc->zc_nvlist_dst != 0) { 1594 int err; 1595 1596 if ((err = put_nvlist(zc, config)) != 0) 1597 error = err; 1598 } 1599 1600 nvlist_free(config); 1601 1602 if (props) 1603 nvlist_free(props); 1604 1605 return (error); 1606} 1607 1608static int 1609zfs_ioc_pool_export(zfs_cmd_t *zc) 1610{ 1611 int error; 1612 boolean_t force = (boolean_t)zc->zc_cookie; 1613 boolean_t hardforce = (boolean_t)zc->zc_guid; 1614 1615 zfs_log_history(zc); 1616 error = spa_export(zc->zc_name, NULL, force, hardforce); 1617 if (error == 0) 1618 zvol_remove_minors(zc->zc_name); 1619 return (error); 1620} 1621 1622static int 1623zfs_ioc_pool_configs(zfs_cmd_t *zc) 1624{ 1625 nvlist_t *configs; 1626 int error; 1627 1628 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1629 return (SET_ERROR(EEXIST)); 1630 1631 error = put_nvlist(zc, configs); 1632 1633 nvlist_free(configs); 1634 1635 return (error); 1636} 1637 1638/* 1639 * inputs: 1640 * zc_name name of the pool 1641 * 1642 * outputs: 1643 * zc_cookie real errno 1644 * zc_nvlist_dst config nvlist 1645 * zc_nvlist_dst_size size of config nvlist 1646 */ 1647static int 1648zfs_ioc_pool_stats(zfs_cmd_t *zc) 1649{ 1650 nvlist_t *config; 1651 int error; 1652 int ret = 0; 1653 1654 error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1655 sizeof (zc->zc_value)); 1656 1657 if (config != NULL) { 1658 ret = put_nvlist(zc, config); 1659 nvlist_free(config); 1660 1661 /* 1662 * The config may be present even if 'error' is non-zero. 1663 * In this case we return success, and preserve the real errno 1664 * in 'zc_cookie'. 1665 */ 1666 zc->zc_cookie = error; 1667 } else { 1668 ret = error; 1669 } 1670 1671 return (ret); 1672} 1673 1674/* 1675 * Try to import the given pool, returning pool stats as appropriate so that 1676 * user land knows which devices are available and overall pool health. 1677 */ 1678static int 1679zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1680{ 1681 nvlist_t *tryconfig, *config; 1682 int error; 1683 1684 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1685 zc->zc_iflags, &tryconfig)) != 0) 1686 return (error); 1687 1688 config = spa_tryimport(tryconfig); 1689 1690 nvlist_free(tryconfig); 1691 1692 if (config == NULL) 1693 return (SET_ERROR(EINVAL)); 1694 1695 error = put_nvlist(zc, config); 1696 nvlist_free(config); 1697 1698 return (error); 1699} 1700 1701/* 1702 * inputs: 1703 * zc_name name of the pool 1704 * zc_cookie scan func (pool_scan_func_t) 1705 */ 1706static int 1707zfs_ioc_pool_scan(zfs_cmd_t *zc) 1708{ 1709 spa_t *spa; 1710 int error; 1711 1712 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1713 return (error); 1714 1715 if (zc->zc_cookie == POOL_SCAN_NONE) 1716 error = spa_scan_stop(spa); 1717 else 1718 error = spa_scan(spa, zc->zc_cookie); 1719 1720 spa_close(spa, FTAG); 1721 1722 return (error); 1723} 1724 1725static int 1726zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1727{ 1728 spa_t *spa; 1729 int error; 1730 1731 error = spa_open(zc->zc_name, &spa, FTAG); 1732 if (error == 0) { 1733 spa_freeze(spa); 1734 spa_close(spa, FTAG); 1735 } 1736 return (error); 1737} 1738 1739static int 1740zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1741{ 1742 spa_t *spa; 1743 int error; 1744 1745 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1746 return (error); 1747 1748 if (zc->zc_cookie < spa_version(spa) || 1749 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) { 1750 spa_close(spa, FTAG); 1751 return (SET_ERROR(EINVAL)); 1752 } 1753 1754 spa_upgrade(spa, zc->zc_cookie); 1755 spa_close(spa, FTAG); 1756 1757 return (error); 1758} 1759 1760static int 1761zfs_ioc_pool_get_history(zfs_cmd_t *zc) 1762{ 1763 spa_t *spa; 1764 char *hist_buf; 1765 uint64_t size; 1766 int error; 1767 1768 if ((size = zc->zc_history_len) == 0) 1769 return (SET_ERROR(EINVAL)); 1770 1771 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1772 return (error); 1773 1774 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1775 spa_close(spa, FTAG); 1776 return (SET_ERROR(ENOTSUP)); 1777 } 1778 1779 hist_buf = kmem_alloc(size, KM_SLEEP); 1780 if ((error = spa_history_get(spa, &zc->zc_history_offset, 1781 &zc->zc_history_len, hist_buf)) == 0) { 1782 error = ddi_copyout(hist_buf, 1783 (void *)(uintptr_t)zc->zc_history, 1784 zc->zc_history_len, zc->zc_iflags); 1785 } 1786 1787 spa_close(spa, FTAG); 1788 kmem_free(hist_buf, size); 1789 return (error); 1790} 1791 1792static int 1793zfs_ioc_pool_reguid(zfs_cmd_t *zc) 1794{ 1795 spa_t *spa; 1796 int error; 1797 1798 error = spa_open(zc->zc_name, &spa, FTAG); 1799 if (error == 0) { 1800 error = spa_change_guid(spa); 1801 spa_close(spa, FTAG); 1802 } 1803 return (error); 1804} 1805 1806static int 1807zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 1808{ 1809 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)); 1810} 1811 1812/* 1813 * inputs: 1814 * zc_name name of filesystem 1815 * zc_obj object to find 1816 * 1817 * outputs: 1818 * zc_value name of object 1819 */ 1820static int 1821zfs_ioc_obj_to_path(zfs_cmd_t *zc) 1822{ 1823 objset_t *os; 1824 int error; 1825 1826 /* XXX reading from objset not owned */ 1827 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 1828 return (error); 1829 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1830 dmu_objset_rele(os, FTAG); 1831 return (SET_ERROR(EINVAL)); 1832 } 1833 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 1834 sizeof (zc->zc_value)); 1835 dmu_objset_rele(os, FTAG); 1836 1837 return (error); 1838} 1839 1840/* 1841 * inputs: 1842 * zc_name name of filesystem 1843 * zc_obj object to find 1844 * 1845 * outputs: 1846 * zc_stat stats on object 1847 * zc_value path to object 1848 */ 1849static int 1850zfs_ioc_obj_to_stats(zfs_cmd_t *zc) 1851{ 1852 objset_t *os; 1853 int error; 1854 1855 /* XXX reading from objset not owned */ 1856 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 1857 return (error); 1858 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1859 dmu_objset_rele(os, FTAG); 1860 return (SET_ERROR(EINVAL)); 1861 } 1862 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, 1863 sizeof (zc->zc_value)); 1864 dmu_objset_rele(os, FTAG); 1865 1866 return (error); 1867} 1868 1869static int 1870zfs_ioc_vdev_add(zfs_cmd_t *zc) 1871{ 1872 spa_t *spa; 1873 int error; 1874 nvlist_t *config, **l2cache, **spares; 1875 uint_t nl2cache = 0, nspares = 0; 1876 1877 error = spa_open(zc->zc_name, &spa, FTAG); 1878 if (error != 0) 1879 return (error); 1880 1881 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1882 zc->zc_iflags, &config); 1883 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 1884 &l2cache, &nl2cache); 1885 1886 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 1887 &spares, &nspares); 1888 1889#ifdef illumos 1890 /* 1891 * A root pool with concatenated devices is not supported. 1892 * Thus, can not add a device to a root pool. 1893 * 1894 * Intent log device can not be added to a rootpool because 1895 * during mountroot, zil is replayed, a seperated log device 1896 * can not be accessed during the mountroot time. 1897 * 1898 * l2cache and spare devices are ok to be added to a rootpool. 1899 */ 1900 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 1901 nvlist_free(config); 1902 spa_close(spa, FTAG); 1903 return (SET_ERROR(EDOM)); 1904 } 1905#endif /* illumos */ 1906 1907 if (error == 0) { 1908 error = spa_vdev_add(spa, config); 1909 nvlist_free(config); 1910 } 1911 spa_close(spa, FTAG); 1912 return (error); 1913} 1914 1915/* 1916 * inputs: 1917 * zc_name name of the pool 1918 * zc_nvlist_conf nvlist of devices to remove 1919 * zc_cookie to stop the remove? 1920 */ 1921static int 1922zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1923{ 1924 spa_t *spa; 1925 int error; 1926 1927 error = spa_open(zc->zc_name, &spa, FTAG); 1928 if (error != 0) 1929 return (error); 1930 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 1931 spa_close(spa, FTAG); 1932 return (error); 1933} 1934 1935static int 1936zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1937{ 1938 spa_t *spa; 1939 int error; 1940 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1941 1942 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1943 return (error); 1944 switch (zc->zc_cookie) { 1945 case VDEV_STATE_ONLINE: 1946 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 1947 break; 1948 1949 case VDEV_STATE_OFFLINE: 1950 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 1951 break; 1952 1953 case VDEV_STATE_FAULTED: 1954 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1955 zc->zc_obj != VDEV_AUX_EXTERNAL) 1956 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1957 1958 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 1959 break; 1960 1961 case VDEV_STATE_DEGRADED: 1962 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1963 zc->zc_obj != VDEV_AUX_EXTERNAL) 1964 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1965 1966 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 1967 break; 1968 1969 default: 1970 error = SET_ERROR(EINVAL); 1971 } 1972 zc->zc_cookie = newstate; 1973 spa_close(spa, FTAG); 1974 return (error); 1975} 1976 1977static int 1978zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1979{ 1980 spa_t *spa; 1981 int replacing = zc->zc_cookie; 1982 nvlist_t *config; 1983 int error; 1984 1985 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1986 return (error); 1987 1988 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1989 zc->zc_iflags, &config)) == 0) { 1990 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1991 nvlist_free(config); 1992 } 1993 1994 spa_close(spa, FTAG); 1995 return (error); 1996} 1997 1998static int 1999zfs_ioc_vdev_detach(zfs_cmd_t *zc) 2000{ 2001 spa_t *spa; 2002 int error; 2003 2004 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2005 return (error); 2006 2007 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 2008 2009 spa_close(spa, FTAG); 2010 return (error); 2011} 2012 2013static int 2014zfs_ioc_vdev_split(zfs_cmd_t *zc) 2015{ 2016 spa_t *spa; 2017 nvlist_t *config, *props = NULL; 2018 int error; 2019 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 2020 2021 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2022 return (error); 2023 2024 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 2025 zc->zc_iflags, &config)) { 2026 spa_close(spa, FTAG); 2027 return (error); 2028 } 2029 2030 if (zc->zc_nvlist_src_size != 0 && (error = 2031 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2032 zc->zc_iflags, &props))) { 2033 spa_close(spa, FTAG); 2034 nvlist_free(config); 2035 return (error); 2036 } 2037 2038 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 2039 2040 spa_close(spa, FTAG); 2041 2042 nvlist_free(config); 2043 nvlist_free(props); 2044 2045 return (error); 2046} 2047 2048static int 2049zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 2050{ 2051 spa_t *spa; 2052 char *path = zc->zc_value; 2053 uint64_t guid = zc->zc_guid; 2054 int error; 2055 2056 error = spa_open(zc->zc_name, &spa, FTAG); 2057 if (error != 0) 2058 return (error); 2059 2060 error = spa_vdev_setpath(spa, guid, path); 2061 spa_close(spa, FTAG); 2062 return (error); 2063} 2064 2065static int 2066zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 2067{ 2068 spa_t *spa; 2069 char *fru = zc->zc_value; 2070 uint64_t guid = zc->zc_guid; 2071 int error; 2072 2073 error = spa_open(zc->zc_name, &spa, FTAG); 2074 if (error != 0) 2075 return (error); 2076 2077 error = spa_vdev_setfru(spa, guid, fru); 2078 spa_close(spa, FTAG); 2079 return (error); 2080} 2081 2082static int 2083zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) 2084{ 2085 int error = 0; 2086 nvlist_t *nv; 2087 2088 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 2089 2090 if (zc->zc_nvlist_dst != 0 && 2091 (error = dsl_prop_get_all(os, &nv)) == 0) { 2092 dmu_objset_stats(os, nv); 2093 /* 2094 * NB: zvol_get_stats() will read the objset contents, 2095 * which we aren't supposed to do with a 2096 * DS_MODE_USER hold, because it could be 2097 * inconsistent. So this is a bit of a workaround... 2098 * XXX reading with out owning 2099 */ 2100 if (!zc->zc_objset_stats.dds_inconsistent && 2101 dmu_objset_type(os) == DMU_OST_ZVOL) { 2102 error = zvol_get_stats(os, nv); 2103 if (error == EIO) 2104 return (error); 2105 VERIFY0(error); 2106 } 2107 error = put_nvlist(zc, nv); 2108 nvlist_free(nv); 2109 } 2110 2111 return (error); 2112} 2113 2114/* 2115 * inputs: 2116 * zc_name name of filesystem 2117 * zc_nvlist_dst_size size of buffer for property nvlist 2118 * 2119 * outputs: 2120 * zc_objset_stats stats 2121 * zc_nvlist_dst property nvlist 2122 * zc_nvlist_dst_size size of property nvlist 2123 */ 2124static int 2125zfs_ioc_objset_stats(zfs_cmd_t *zc) 2126{ 2127 objset_t *os; 2128 int error; 2129 2130 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2131 if (error == 0) { 2132 error = zfs_ioc_objset_stats_impl(zc, os); 2133 dmu_objset_rele(os, FTAG); 2134 } 2135 2136 if (error == ENOMEM) 2137 error = 0; 2138 return (error); 2139} 2140 2141/* 2142 * inputs: 2143 * zc_name name of filesystem 2144 * zc_nvlist_dst_size size of buffer for property nvlist 2145 * 2146 * outputs: 2147 * zc_nvlist_dst received property nvlist 2148 * zc_nvlist_dst_size size of received property nvlist 2149 * 2150 * Gets received properties (distinct from local properties on or after 2151 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 2152 * local property values. 2153 */ 2154static int 2155zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 2156{ 2157 int error = 0; 2158 nvlist_t *nv; 2159 2160 /* 2161 * Without this check, we would return local property values if the 2162 * caller has not already received properties on or after 2163 * SPA_VERSION_RECVD_PROPS. 2164 */ 2165 if (!dsl_prop_get_hasrecvd(zc->zc_name)) 2166 return (SET_ERROR(ENOTSUP)); 2167 2168 if (zc->zc_nvlist_dst != 0 && 2169 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) { 2170 error = put_nvlist(zc, nv); 2171 nvlist_free(nv); 2172 } 2173 2174 return (error); 2175} 2176 2177static int 2178nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 2179{ 2180 uint64_t value; 2181 int error; 2182 2183 /* 2184 * zfs_get_zplprop() will either find a value or give us 2185 * the default value (if there is one). 2186 */ 2187 if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 2188 return (error); 2189 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 2190 return (0); 2191} 2192 2193/* 2194 * inputs: 2195 * zc_name name of filesystem 2196 * zc_nvlist_dst_size size of buffer for zpl property nvlist 2197 * 2198 * outputs: 2199 * zc_nvlist_dst zpl property nvlist 2200 * zc_nvlist_dst_size size of zpl property nvlist 2201 */ 2202static int 2203zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 2204{ 2205 objset_t *os; 2206 int err; 2207 2208 /* XXX reading without owning */ 2209 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 2210 return (err); 2211 2212 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 2213 2214 /* 2215 * NB: nvl_add_zplprop() will read the objset contents, 2216 * which we aren't supposed to do with a DS_MODE_USER 2217 * hold, because it could be inconsistent. 2218 */ 2219 if (zc->zc_nvlist_dst != 0 && 2220 !zc->zc_objset_stats.dds_inconsistent && 2221 dmu_objset_type(os) == DMU_OST_ZFS) { 2222 nvlist_t *nv; 2223 2224 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2225 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 2226 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 2227 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 2228 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 2229 err = put_nvlist(zc, nv); 2230 nvlist_free(nv); 2231 } else { 2232 err = SET_ERROR(ENOENT); 2233 } 2234 dmu_objset_rele(os, FTAG); 2235 return (err); 2236} 2237 2238boolean_t 2239dataset_name_hidden(const char *name) 2240{ 2241 /* 2242 * Skip over datasets that are not visible in this zone, 2243 * internal datasets (which have a $ in their name), and 2244 * temporary datasets (which have a % in their name). 2245 */ 2246 if (strchr(name, '$') != NULL) 2247 return (B_TRUE); 2248 if (strchr(name, '%') != NULL) 2249 return (B_TRUE); 2250 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL)) 2251 return (B_TRUE); 2252 return (B_FALSE); 2253} 2254 2255/* 2256 * inputs: 2257 * zc_name name of filesystem 2258 * zc_cookie zap cursor 2259 * zc_nvlist_dst_size size of buffer for property nvlist 2260 * 2261 * outputs: 2262 * zc_name name of next filesystem 2263 * zc_cookie zap cursor 2264 * zc_objset_stats stats 2265 * zc_nvlist_dst property nvlist 2266 * zc_nvlist_dst_size size of property nvlist 2267 */ 2268static int 2269zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 2270{ 2271 objset_t *os; 2272 int error; 2273 char *p; 2274 size_t orig_len = strlen(zc->zc_name); 2275 2276top: 2277 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 2278 if (error == ENOENT) 2279 error = SET_ERROR(ESRCH); 2280 return (error); 2281 } 2282 2283 p = strrchr(zc->zc_name, '/'); 2284 if (p == NULL || p[1] != '\0') 2285 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 2286 p = zc->zc_name + strlen(zc->zc_name); 2287 2288 do { 2289 error = dmu_dir_list_next(os, 2290 sizeof (zc->zc_name) - (p - zc->zc_name), p, 2291 NULL, &zc->zc_cookie); 2292 if (error == ENOENT) 2293 error = SET_ERROR(ESRCH); 2294 } while (error == 0 && dataset_name_hidden(zc->zc_name)); 2295 dmu_objset_rele(os, FTAG); 2296 2297 /* 2298 * If it's an internal dataset (ie. with a '$' in its name), 2299 * don't try to get stats for it, otherwise we'll return ENOENT. 2300 */ 2301 if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 2302 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 2303 if (error == ENOENT) { 2304 /* We lost a race with destroy, get the next one. */ 2305 zc->zc_name[orig_len] = '\0'; 2306 goto top; 2307 } 2308 } 2309 return (error); 2310} 2311 2312/* 2313 * inputs: 2314 * zc_name name of filesystem 2315 * zc_cookie zap cursor 2316 * zc_nvlist_dst_size size of buffer for property nvlist 2317 * zc_simple when set, only name is requested 2318 * 2319 * outputs: 2320 * zc_name name of next snapshot 2321 * zc_objset_stats stats 2322 * zc_nvlist_dst property nvlist 2323 * zc_nvlist_dst_size size of property nvlist 2324 */ 2325static int 2326zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 2327{ 2328 objset_t *os; 2329 int error; 2330 2331 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2332 if (error != 0) { 2333 return (error == ENOENT ? ESRCH : error); 2334 } 2335 2336 /* 2337 * A dataset name of maximum length cannot have any snapshots, 2338 * so exit immediately. 2339 */ 2340 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 2341 dmu_objset_rele(os, FTAG); 2342 return (SET_ERROR(ESRCH)); 2343 } 2344 2345 error = dmu_snapshot_list_next(os, 2346 sizeof (zc->zc_name) - strlen(zc->zc_name), 2347 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie, 2348 NULL); 2349 2350 if (error == 0 && !zc->zc_simple) { 2351 dsl_dataset_t *ds; 2352 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 2353 2354 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds); 2355 if (error == 0) { 2356 objset_t *ossnap; 2357 2358 error = dmu_objset_from_ds(ds, &ossnap); 2359 if (error == 0) 2360 error = zfs_ioc_objset_stats_impl(zc, ossnap); 2361 dsl_dataset_rele(ds, FTAG); 2362 } 2363 } else if (error == ENOENT) { 2364 error = SET_ERROR(ESRCH); 2365 } 2366 2367 dmu_objset_rele(os, FTAG); 2368 /* if we failed, undo the @ that we tacked on to zc_name */ 2369 if (error != 0) 2370 *strchr(zc->zc_name, '@') = '\0'; 2371 return (error); 2372} 2373 2374static int 2375zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 2376{ 2377 const char *propname = nvpair_name(pair); 2378 uint64_t *valary; 2379 unsigned int vallen; 2380 const char *domain; 2381 char *dash; 2382 zfs_userquota_prop_t type; 2383 uint64_t rid; 2384 uint64_t quota; 2385 zfsvfs_t *zfsvfs; 2386 int err; 2387 2388 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2389 nvlist_t *attrs; 2390 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2391 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2392 &pair) != 0) 2393 return (SET_ERROR(EINVAL)); 2394 } 2395 2396 /* 2397 * A correctly constructed propname is encoded as 2398 * userquota@<rid>-<domain>. 2399 */ 2400 if ((dash = strchr(propname, '-')) == NULL || 2401 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 2402 vallen != 3) 2403 return (SET_ERROR(EINVAL)); 2404 2405 domain = dash + 1; 2406 type = valary[0]; 2407 rid = valary[1]; 2408 quota = valary[2]; 2409 2410 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); 2411 if (err == 0) { 2412 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 2413 zfsvfs_rele(zfsvfs, FTAG); 2414 } 2415 2416 return (err); 2417} 2418 2419/* 2420 * If the named property is one that has a special function to set its value, 2421 * return 0 on success and a positive error code on failure; otherwise if it is 2422 * not one of the special properties handled by this function, return -1. 2423 * 2424 * XXX: It would be better for callers of the property interface if we handled 2425 * these special cases in dsl_prop.c (in the dsl layer). 2426 */ 2427static int 2428zfs_prop_set_special(const char *dsname, zprop_source_t source, 2429 nvpair_t *pair) 2430{ 2431 const char *propname = nvpair_name(pair); 2432 zfs_prop_t prop = zfs_name_to_prop(propname); 2433 uint64_t intval; 2434 int err = -1; 2435 2436 if (prop == ZPROP_INVAL) { 2437 if (zfs_prop_userquota(propname)) 2438 return (zfs_prop_set_userquota(dsname, pair)); 2439 return (-1); 2440 } 2441 2442 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2443 nvlist_t *attrs; 2444 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2445 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2446 &pair) == 0); 2447 } 2448 2449 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 2450 return (-1); 2451 2452 VERIFY(0 == nvpair_value_uint64(pair, &intval)); 2453 2454 switch (prop) { 2455 case ZFS_PROP_QUOTA: 2456 err = dsl_dir_set_quota(dsname, source, intval); 2457 break; 2458 case ZFS_PROP_REFQUOTA: 2459 err = dsl_dataset_set_refquota(dsname, source, intval); 2460 break; 2461 case ZFS_PROP_FILESYSTEM_LIMIT: 2462 case ZFS_PROP_SNAPSHOT_LIMIT: 2463 if (intval == UINT64_MAX) { 2464 /* clearing the limit, just do it */ 2465 err = 0; 2466 } else { 2467 err = dsl_dir_activate_fs_ss_limit(dsname); 2468 } 2469 /* 2470 * Set err to -1 to force the zfs_set_prop_nvlist code down the 2471 * default path to set the value in the nvlist. 2472 */ 2473 if (err == 0) 2474 err = -1; 2475 break; 2476 case ZFS_PROP_RESERVATION: 2477 err = dsl_dir_set_reservation(dsname, source, intval); 2478 break; 2479 case ZFS_PROP_REFRESERVATION: 2480 err = dsl_dataset_set_refreservation(dsname, source, intval); 2481 break; 2482 case ZFS_PROP_VOLSIZE: 2483 err = zvol_set_volsize(dsname, intval); 2484 break; 2485 case ZFS_PROP_VERSION: 2486 { 2487 zfsvfs_t *zfsvfs; 2488 2489 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) 2490 break; 2491 2492 err = zfs_set_version(zfsvfs, intval); 2493 zfsvfs_rele(zfsvfs, FTAG); 2494 2495 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 2496 zfs_cmd_t *zc; 2497 2498 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2499 (void) strcpy(zc->zc_name, dsname); 2500 (void) zfs_ioc_userspace_upgrade(zc); 2501 kmem_free(zc, sizeof (zfs_cmd_t)); 2502 } 2503 break; 2504 } 2505 default: 2506 err = -1; 2507 } 2508 2509 return (err); 2510} 2511 2512/* 2513 * This function is best effort. If it fails to set any of the given properties, 2514 * it continues to set as many as it can and returns the last error 2515 * encountered. If the caller provides a non-NULL errlist, it will be filled in 2516 * with the list of names of all the properties that failed along with the 2517 * corresponding error numbers. 2518 * 2519 * If every property is set successfully, zero is returned and errlist is not 2520 * modified. 2521 */ 2522int 2523zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 2524 nvlist_t *errlist) 2525{ 2526 nvpair_t *pair; 2527 nvpair_t *propval; 2528 int rv = 0; 2529 uint64_t intval; 2530 char *strval; 2531 nvlist_t *genericnvl = fnvlist_alloc(); 2532 nvlist_t *retrynvl = fnvlist_alloc(); 2533 2534retry: 2535 pair = NULL; 2536 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2537 const char *propname = nvpair_name(pair); 2538 zfs_prop_t prop = zfs_name_to_prop(propname); 2539 int err = 0; 2540 2541 /* decode the property value */ 2542 propval = pair; 2543 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2544 nvlist_t *attrs; 2545 attrs = fnvpair_value_nvlist(pair); 2546 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2547 &propval) != 0) 2548 err = SET_ERROR(EINVAL); 2549 } 2550 2551 /* Validate value type */ 2552 if (err == 0 && prop == ZPROP_INVAL) { 2553 if (zfs_prop_user(propname)) { 2554 if (nvpair_type(propval) != DATA_TYPE_STRING) 2555 err = SET_ERROR(EINVAL); 2556 } else if (zfs_prop_userquota(propname)) { 2557 if (nvpair_type(propval) != 2558 DATA_TYPE_UINT64_ARRAY) 2559 err = SET_ERROR(EINVAL); 2560 } else { 2561 err = SET_ERROR(EINVAL); 2562 } 2563 } else if (err == 0) { 2564 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2565 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 2566 err = SET_ERROR(EINVAL); 2567 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 2568 const char *unused; 2569 2570 intval = fnvpair_value_uint64(propval); 2571 2572 switch (zfs_prop_get_type(prop)) { 2573 case PROP_TYPE_NUMBER: 2574 break; 2575 case PROP_TYPE_STRING: 2576 err = SET_ERROR(EINVAL); 2577 break; 2578 case PROP_TYPE_INDEX: 2579 if (zfs_prop_index_to_string(prop, 2580 intval, &unused) != 0) 2581 err = SET_ERROR(EINVAL); 2582 break; 2583 default: 2584 cmn_err(CE_PANIC, 2585 "unknown property type"); 2586 } 2587 } else { 2588 err = SET_ERROR(EINVAL); 2589 } 2590 } 2591 2592 /* Validate permissions */ 2593 if (err == 0) 2594 err = zfs_check_settable(dsname, pair, CRED()); 2595 2596 if (err == 0) { 2597 err = zfs_prop_set_special(dsname, source, pair); 2598 if (err == -1) { 2599 /* 2600 * For better performance we build up a list of 2601 * properties to set in a single transaction. 2602 */ 2603 err = nvlist_add_nvpair(genericnvl, pair); 2604 } else if (err != 0 && nvl != retrynvl) { 2605 /* 2606 * This may be a spurious error caused by 2607 * receiving quota and reservation out of order. 2608 * Try again in a second pass. 2609 */ 2610 err = nvlist_add_nvpair(retrynvl, pair); 2611 } 2612 } 2613 2614 if (err != 0) { 2615 if (errlist != NULL) 2616 fnvlist_add_int32(errlist, propname, err); 2617 rv = err; 2618 } 2619 } 2620 2621 if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 2622 nvl = retrynvl; 2623 goto retry; 2624 } 2625 2626 if (!nvlist_empty(genericnvl) && 2627 dsl_props_set(dsname, source, genericnvl) != 0) { 2628 /* 2629 * If this fails, we still want to set as many properties as we 2630 * can, so try setting them individually. 2631 */ 2632 pair = NULL; 2633 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 2634 const char *propname = nvpair_name(pair); 2635 int err = 0; 2636 2637 propval = pair; 2638 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2639 nvlist_t *attrs; 2640 attrs = fnvpair_value_nvlist(pair); 2641 propval = fnvlist_lookup_nvpair(attrs, 2642 ZPROP_VALUE); 2643 } 2644 2645 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2646 strval = fnvpair_value_string(propval); 2647 err = dsl_prop_set_string(dsname, propname, 2648 source, strval); 2649 } else { 2650 intval = fnvpair_value_uint64(propval); 2651 err = dsl_prop_set_int(dsname, propname, source, 2652 intval); 2653 } 2654 2655 if (err != 0) { 2656 if (errlist != NULL) { 2657 fnvlist_add_int32(errlist, propname, 2658 err); 2659 } 2660 rv = err; 2661 } 2662 } 2663 } 2664 nvlist_free(genericnvl); 2665 nvlist_free(retrynvl); 2666 2667 return (rv); 2668} 2669 2670/* 2671 * Check that all the properties are valid user properties. 2672 */ 2673static int 2674zfs_check_userprops(const char *fsname, nvlist_t *nvl) 2675{ 2676 nvpair_t *pair = NULL; 2677 int error = 0; 2678 2679 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2680 const char *propname = nvpair_name(pair); 2681 2682 if (!zfs_prop_user(propname) || 2683 nvpair_type(pair) != DATA_TYPE_STRING) 2684 return (SET_ERROR(EINVAL)); 2685 2686 if (error = zfs_secpolicy_write_perms(fsname, 2687 ZFS_DELEG_PERM_USERPROP, CRED())) 2688 return (error); 2689 2690 if (strlen(propname) >= ZAP_MAXNAMELEN) 2691 return (SET_ERROR(ENAMETOOLONG)); 2692 2693 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN) 2694 return (E2BIG); 2695 } 2696 return (0); 2697} 2698 2699static void 2700props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 2701{ 2702 nvpair_t *pair; 2703 2704 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2705 2706 pair = NULL; 2707 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 2708 if (nvlist_exists(skipped, nvpair_name(pair))) 2709 continue; 2710 2711 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 2712 } 2713} 2714 2715static int 2716clear_received_props(const char *dsname, nvlist_t *props, 2717 nvlist_t *skipped) 2718{ 2719 int err = 0; 2720 nvlist_t *cleared_props = NULL; 2721 props_skip(props, skipped, &cleared_props); 2722 if (!nvlist_empty(cleared_props)) { 2723 /* 2724 * Acts on local properties until the dataset has received 2725 * properties at least once on or after SPA_VERSION_RECVD_PROPS. 2726 */ 2727 zprop_source_t flags = (ZPROP_SRC_NONE | 2728 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0)); 2729 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL); 2730 } 2731 nvlist_free(cleared_props); 2732 return (err); 2733} 2734 2735/* 2736 * inputs: 2737 * zc_name name of filesystem 2738 * zc_value name of property to set 2739 * zc_nvlist_src{_size} nvlist of properties to apply 2740 * zc_cookie received properties flag 2741 * 2742 * outputs: 2743 * zc_nvlist_dst{_size} error for each unapplied received property 2744 */ 2745static int 2746zfs_ioc_set_prop(zfs_cmd_t *zc) 2747{ 2748 nvlist_t *nvl; 2749 boolean_t received = zc->zc_cookie; 2750 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 2751 ZPROP_SRC_LOCAL); 2752 nvlist_t *errors; 2753 int error; 2754 2755 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2756 zc->zc_iflags, &nvl)) != 0) 2757 return (error); 2758 2759 if (received) { 2760 nvlist_t *origprops; 2761 2762 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) { 2763 (void) clear_received_props(zc->zc_name, 2764 origprops, nvl); 2765 nvlist_free(origprops); 2766 } 2767 2768 error = dsl_prop_set_hasrecvd(zc->zc_name); 2769 } 2770 2771 errors = fnvlist_alloc(); 2772 if (error == 0) 2773 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors); 2774 2775 if (zc->zc_nvlist_dst != 0 && errors != NULL) { 2776 (void) put_nvlist(zc, errors); 2777 } 2778 2779 nvlist_free(errors); 2780 nvlist_free(nvl); 2781 return (error); 2782} 2783 2784/* 2785 * inputs: 2786 * zc_name name of filesystem 2787 * zc_value name of property to inherit 2788 * zc_cookie revert to received value if TRUE 2789 * 2790 * outputs: none 2791 */ 2792static int 2793zfs_ioc_inherit_prop(zfs_cmd_t *zc) 2794{ 2795 const char *propname = zc->zc_value; 2796 zfs_prop_t prop = zfs_name_to_prop(propname); 2797 boolean_t received = zc->zc_cookie; 2798 zprop_source_t source = (received 2799 ? ZPROP_SRC_NONE /* revert to received value, if any */ 2800 : ZPROP_SRC_INHERITED); /* explicitly inherit */ 2801 2802 if (received) { 2803 nvlist_t *dummy; 2804 nvpair_t *pair; 2805 zprop_type_t type; 2806 int err; 2807 2808 /* 2809 * zfs_prop_set_special() expects properties in the form of an 2810 * nvpair with type info. 2811 */ 2812 if (prop == ZPROP_INVAL) { 2813 if (!zfs_prop_user(propname)) 2814 return (SET_ERROR(EINVAL)); 2815 2816 type = PROP_TYPE_STRING; 2817 } else if (prop == ZFS_PROP_VOLSIZE || 2818 prop == ZFS_PROP_VERSION) { 2819 return (SET_ERROR(EINVAL)); 2820 } else { 2821 type = zfs_prop_get_type(prop); 2822 } 2823 2824 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2825 2826 switch (type) { 2827 case PROP_TYPE_STRING: 2828 VERIFY(0 == nvlist_add_string(dummy, propname, "")); 2829 break; 2830 case PROP_TYPE_NUMBER: 2831 case PROP_TYPE_INDEX: 2832 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 2833 break; 2834 default: 2835 nvlist_free(dummy); 2836 return (SET_ERROR(EINVAL)); 2837 } 2838 2839 pair = nvlist_next_nvpair(dummy, NULL); 2840 err = zfs_prop_set_special(zc->zc_name, source, pair); 2841 nvlist_free(dummy); 2842 if (err != -1) 2843 return (err); /* special property already handled */ 2844 } else { 2845 /* 2846 * Only check this in the non-received case. We want to allow 2847 * 'inherit -S' to revert non-inheritable properties like quota 2848 * and reservation to the received or default values even though 2849 * they are not considered inheritable. 2850 */ 2851 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 2852 return (SET_ERROR(EINVAL)); 2853 } 2854 2855 /* property name has been validated by zfs_secpolicy_inherit_prop() */ 2856 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source)); 2857} 2858 2859static int 2860zfs_ioc_pool_set_props(zfs_cmd_t *zc) 2861{ 2862 nvlist_t *props; 2863 spa_t *spa; 2864 int error; 2865 nvpair_t *pair; 2866 2867 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2868 zc->zc_iflags, &props)) 2869 return (error); 2870 2871 /* 2872 * If the only property is the configfile, then just do a spa_lookup() 2873 * to handle the faulted case. 2874 */ 2875 pair = nvlist_next_nvpair(props, NULL); 2876 if (pair != NULL && strcmp(nvpair_name(pair), 2877 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 2878 nvlist_next_nvpair(props, pair) == NULL) { 2879 mutex_enter(&spa_namespace_lock); 2880 if ((spa = spa_lookup(zc->zc_name)) != NULL) { 2881 spa_configfile_set(spa, props, B_FALSE); 2882 spa_config_sync(spa, B_FALSE, B_TRUE); 2883 } 2884 mutex_exit(&spa_namespace_lock); 2885 if (spa != NULL) { 2886 nvlist_free(props); 2887 return (0); 2888 } 2889 } 2890 2891 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2892 nvlist_free(props); 2893 return (error); 2894 } 2895 2896 error = spa_prop_set(spa, props); 2897 2898 nvlist_free(props); 2899 spa_close(spa, FTAG); 2900 2901 return (error); 2902} 2903 2904static int 2905zfs_ioc_pool_get_props(zfs_cmd_t *zc) 2906{ 2907 spa_t *spa; 2908 int error; 2909 nvlist_t *nvp = NULL; 2910 2911 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2912 /* 2913 * If the pool is faulted, there may be properties we can still 2914 * get (such as altroot and cachefile), so attempt to get them 2915 * anyway. 2916 */ 2917 mutex_enter(&spa_namespace_lock); 2918 if ((spa = spa_lookup(zc->zc_name)) != NULL) 2919 error = spa_prop_get(spa, &nvp); 2920 mutex_exit(&spa_namespace_lock); 2921 } else { 2922 error = spa_prop_get(spa, &nvp); 2923 spa_close(spa, FTAG); 2924 } 2925 2926 if (error == 0 && zc->zc_nvlist_dst != 0) 2927 error = put_nvlist(zc, nvp); 2928 else 2929 error = SET_ERROR(EFAULT); 2930 2931 nvlist_free(nvp); 2932 return (error); 2933} 2934 2935/* 2936 * inputs: 2937 * zc_name name of filesystem 2938 * zc_nvlist_src{_size} nvlist of delegated permissions 2939 * zc_perm_action allow/unallow flag 2940 * 2941 * outputs: none 2942 */ 2943static int 2944zfs_ioc_set_fsacl(zfs_cmd_t *zc) 2945{ 2946 int error; 2947 nvlist_t *fsaclnv = NULL; 2948 2949 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2950 zc->zc_iflags, &fsaclnv)) != 0) 2951 return (error); 2952 2953 /* 2954 * Verify nvlist is constructed correctly 2955 */ 2956 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 2957 nvlist_free(fsaclnv); 2958 return (SET_ERROR(EINVAL)); 2959 } 2960 2961 /* 2962 * If we don't have PRIV_SYS_MOUNT, then validate 2963 * that user is allowed to hand out each permission in 2964 * the nvlist(s) 2965 */ 2966 2967 error = secpolicy_zfs(CRED()); 2968 if (error != 0) { 2969 if (zc->zc_perm_action == B_FALSE) { 2970 error = dsl_deleg_can_allow(zc->zc_name, 2971 fsaclnv, CRED()); 2972 } else { 2973 error = dsl_deleg_can_unallow(zc->zc_name, 2974 fsaclnv, CRED()); 2975 } 2976 } 2977 2978 if (error == 0) 2979 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 2980 2981 nvlist_free(fsaclnv); 2982 return (error); 2983} 2984 2985/* 2986 * inputs: 2987 * zc_name name of filesystem 2988 * 2989 * outputs: 2990 * zc_nvlist_src{_size} nvlist of delegated permissions 2991 */ 2992static int 2993zfs_ioc_get_fsacl(zfs_cmd_t *zc) 2994{ 2995 nvlist_t *nvp; 2996 int error; 2997 2998 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 2999 error = put_nvlist(zc, nvp); 3000 nvlist_free(nvp); 3001 } 3002 3003 return (error); 3004} 3005 3006/* 3007 * Search the vfs list for a specified resource. Returns a pointer to it 3008 * or NULL if no suitable entry is found. The caller of this routine 3009 * is responsible for releasing the returned vfs pointer. 3010 */ 3011static vfs_t * 3012zfs_get_vfs(const char *resource) 3013{ 3014 vfs_t *vfsp; 3015 3016 mtx_lock(&mountlist_mtx); 3017 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) { 3018 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 3019 VFS_HOLD(vfsp); 3020 break; 3021 } 3022 } 3023 mtx_unlock(&mountlist_mtx); 3024 return (vfsp); 3025} 3026 3027/* ARGSUSED */ 3028static void 3029zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 3030{ 3031 zfs_creat_t *zct = arg; 3032 3033 zfs_create_fs(os, cr, zct->zct_zplprops, tx); 3034} 3035 3036#define ZFS_PROP_UNDEFINED ((uint64_t)-1) 3037 3038/* 3039 * inputs: 3040 * os parent objset pointer (NULL if root fs) 3041 * fuids_ok fuids allowed in this version of the spa? 3042 * sa_ok SAs allowed in this version of the spa? 3043 * createprops list of properties requested by creator 3044 * 3045 * outputs: 3046 * zplprops values for the zplprops we attach to the master node object 3047 * is_ci true if requested file system will be purely case-insensitive 3048 * 3049 * Determine the settings for utf8only, normalization and 3050 * casesensitivity. Specific values may have been requested by the 3051 * creator and/or we can inherit values from the parent dataset. If 3052 * the file system is of too early a vintage, a creator can not 3053 * request settings for these properties, even if the requested 3054 * setting is the default value. We don't actually want to create dsl 3055 * properties for these, so remove them from the source nvlist after 3056 * processing. 3057 */ 3058static int 3059zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 3060 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 3061 nvlist_t *zplprops, boolean_t *is_ci) 3062{ 3063 uint64_t sense = ZFS_PROP_UNDEFINED; 3064 uint64_t norm = ZFS_PROP_UNDEFINED; 3065 uint64_t u8 = ZFS_PROP_UNDEFINED; 3066 3067 ASSERT(zplprops != NULL); 3068 3069 /* 3070 * Pull out creator prop choices, if any. 3071 */ 3072 if (createprops) { 3073 (void) nvlist_lookup_uint64(createprops, 3074 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 3075 (void) nvlist_lookup_uint64(createprops, 3076 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 3077 (void) nvlist_remove_all(createprops, 3078 zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 3079 (void) nvlist_lookup_uint64(createprops, 3080 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 3081 (void) nvlist_remove_all(createprops, 3082 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 3083 (void) nvlist_lookup_uint64(createprops, 3084 zfs_prop_to_name(ZFS_PROP_CASE), &sense); 3085 (void) nvlist_remove_all(createprops, 3086 zfs_prop_to_name(ZFS_PROP_CASE)); 3087 } 3088 3089 /* 3090 * If the zpl version requested is whacky or the file system 3091 * or pool is version is too "young" to support normalization 3092 * and the creator tried to set a value for one of the props, 3093 * error out. 3094 */ 3095 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 3096 (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 3097 (zplver >= ZPL_VERSION_SA && !sa_ok) || 3098 (zplver < ZPL_VERSION_NORMALIZATION && 3099 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 3100 sense != ZFS_PROP_UNDEFINED))) 3101 return (SET_ERROR(ENOTSUP)); 3102 3103 /* 3104 * Put the version in the zplprops 3105 */ 3106 VERIFY(nvlist_add_uint64(zplprops, 3107 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 3108 3109 if (norm == ZFS_PROP_UNDEFINED) 3110 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 3111 VERIFY(nvlist_add_uint64(zplprops, 3112 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 3113 3114 /* 3115 * If we're normalizing, names must always be valid UTF-8 strings. 3116 */ 3117 if (norm) 3118 u8 = 1; 3119 if (u8 == ZFS_PROP_UNDEFINED) 3120 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 3121 VERIFY(nvlist_add_uint64(zplprops, 3122 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 3123 3124 if (sense == ZFS_PROP_UNDEFINED) 3125 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 3126 VERIFY(nvlist_add_uint64(zplprops, 3127 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 3128 3129 if (is_ci) 3130 *is_ci = (sense == ZFS_CASE_INSENSITIVE); 3131 3132 return (0); 3133} 3134 3135static int 3136zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 3137 nvlist_t *zplprops, boolean_t *is_ci) 3138{ 3139 boolean_t fuids_ok, sa_ok; 3140 uint64_t zplver = ZPL_VERSION; 3141 objset_t *os = NULL; 3142 char parentname[MAXNAMELEN]; 3143 char *cp; 3144 spa_t *spa; 3145 uint64_t spa_vers; 3146 int error; 3147 3148 (void) strlcpy(parentname, dataset, sizeof (parentname)); 3149 cp = strrchr(parentname, '/'); 3150 ASSERT(cp != NULL); 3151 cp[0] = '\0'; 3152 3153 if ((error = spa_open(dataset, &spa, FTAG)) != 0) 3154 return (error); 3155 3156 spa_vers = spa_version(spa); 3157 spa_close(spa, FTAG); 3158 3159 zplver = zfs_zpl_version_map(spa_vers); 3160 fuids_ok = (zplver >= ZPL_VERSION_FUID); 3161 sa_ok = (zplver >= ZPL_VERSION_SA); 3162 3163 /* 3164 * Open parent object set so we can inherit zplprop values. 3165 */ 3166 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 3167 return (error); 3168 3169 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 3170 zplprops, is_ci); 3171 dmu_objset_rele(os, FTAG); 3172 return (error); 3173} 3174 3175static int 3176zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 3177 nvlist_t *zplprops, boolean_t *is_ci) 3178{ 3179 boolean_t fuids_ok; 3180 boolean_t sa_ok; 3181 uint64_t zplver = ZPL_VERSION; 3182 int error; 3183 3184 zplver = zfs_zpl_version_map(spa_vers); 3185 fuids_ok = (zplver >= ZPL_VERSION_FUID); 3186 sa_ok = (zplver >= ZPL_VERSION_SA); 3187 3188 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 3189 createprops, zplprops, is_ci); 3190 return (error); 3191} 3192 3193/* 3194 * innvl: { 3195 * "type" -> dmu_objset_type_t (int32) 3196 * (optional) "props" -> { prop -> value } 3197 * } 3198 * 3199 * outnvl: propname -> error code (int32) 3200 */ 3201static int 3202zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3203{ 3204 int error = 0; 3205 zfs_creat_t zct = { 0 }; 3206 nvlist_t *nvprops = NULL; 3207 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 3208 int32_t type32; 3209 dmu_objset_type_t type; 3210 boolean_t is_insensitive = B_FALSE; 3211 3212 if (nvlist_lookup_int32(innvl, "type", &type32) != 0) 3213 return (SET_ERROR(EINVAL)); 3214 type = type32; 3215 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3216 3217 switch (type) { 3218 case DMU_OST_ZFS: 3219 cbfunc = zfs_create_cb; 3220 break; 3221 3222 case DMU_OST_ZVOL: 3223 cbfunc = zvol_create_cb; 3224 break; 3225 3226 default: 3227 cbfunc = NULL; 3228 break; 3229 } 3230 if (strchr(fsname, '@') || 3231 strchr(fsname, '%')) 3232 return (SET_ERROR(EINVAL)); 3233 3234 zct.zct_props = nvprops; 3235 3236 if (cbfunc == NULL) 3237 return (SET_ERROR(EINVAL)); 3238 3239 if (type == DMU_OST_ZVOL) { 3240 uint64_t volsize, volblocksize; 3241 3242 if (nvprops == NULL) 3243 return (SET_ERROR(EINVAL)); 3244 if (nvlist_lookup_uint64(nvprops, 3245 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0) 3246 return (SET_ERROR(EINVAL)); 3247 3248 if ((error = nvlist_lookup_uint64(nvprops, 3249 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3250 &volblocksize)) != 0 && error != ENOENT) 3251 return (SET_ERROR(EINVAL)); 3252 3253 if (error != 0) 3254 volblocksize = zfs_prop_default_numeric( 3255 ZFS_PROP_VOLBLOCKSIZE); 3256 3257 if ((error = zvol_check_volblocksize( 3258 volblocksize)) != 0 || 3259 (error = zvol_check_volsize(volsize, 3260 volblocksize)) != 0) 3261 return (error); 3262 } else if (type == DMU_OST_ZFS) { 3263 int error; 3264 3265 /* 3266 * We have to have normalization and 3267 * case-folding flags correct when we do the 3268 * file system creation, so go figure them out 3269 * now. 3270 */ 3271 VERIFY(nvlist_alloc(&zct.zct_zplprops, 3272 NV_UNIQUE_NAME, KM_SLEEP) == 0); 3273 error = zfs_fill_zplprops(fsname, nvprops, 3274 zct.zct_zplprops, &is_insensitive); 3275 if (error != 0) { 3276 nvlist_free(zct.zct_zplprops); 3277 return (error); 3278 } 3279 } 3280 3281 error = dmu_objset_create(fsname, type, 3282 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 3283 nvlist_free(zct.zct_zplprops); 3284 3285 /* 3286 * It would be nice to do this atomically. 3287 */ 3288 if (error == 0) { 3289 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3290 nvprops, outnvl); 3291 if (error != 0) 3292 (void) dsl_destroy_head(fsname); 3293 } 3294#ifdef __FreeBSD__ 3295 if (error == 0 && type == DMU_OST_ZVOL) 3296 zvol_create_minors(fsname); 3297#endif 3298 return (error); 3299} 3300 3301/* 3302 * innvl: { 3303 * "origin" -> name of origin snapshot 3304 * (optional) "props" -> { prop -> value } 3305 * } 3306 * 3307 * outnvl: propname -> error code (int32) 3308 */ 3309static int 3310zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3311{ 3312 int error = 0; 3313 nvlist_t *nvprops = NULL; 3314 char *origin_name; 3315 3316 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0) 3317 return (SET_ERROR(EINVAL)); 3318 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3319 3320 if (strchr(fsname, '@') || 3321 strchr(fsname, '%')) 3322 return (SET_ERROR(EINVAL)); 3323 3324 if (dataset_namecheck(origin_name, NULL, NULL) != 0) 3325 return (SET_ERROR(EINVAL)); 3326 error = dmu_objset_clone(fsname, origin_name); 3327 if (error != 0) 3328 return (error); 3329 3330 /* 3331 * It would be nice to do this atomically. 3332 */ 3333 if (error == 0) { 3334 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3335 nvprops, outnvl); 3336 if (error != 0) 3337 (void) dsl_destroy_head(fsname); 3338 } 3339#ifdef __FreeBSD__ 3340 if (error == 0) 3341 zvol_create_minors(fsname); 3342#endif 3343 return (error); 3344} 3345 3346/* 3347 * innvl: { 3348 * "snaps" -> { snapshot1, snapshot2 } 3349 * (optional) "props" -> { prop -> value (string) } 3350 * } 3351 * 3352 * outnvl: snapshot -> error code (int32) 3353 */ 3354static int 3355zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3356{ 3357 nvlist_t *snaps; 3358 nvlist_t *props = NULL; 3359 int error, poollen; 3360 nvpair_t *pair; 3361 3362 (void) nvlist_lookup_nvlist(innvl, "props", &props); 3363 if ((error = zfs_check_userprops(poolname, props)) != 0) 3364 return (error); 3365 3366 if (!nvlist_empty(props) && 3367 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS)) 3368 return (SET_ERROR(ENOTSUP)); 3369 3370 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3371 return (SET_ERROR(EINVAL)); 3372 poollen = strlen(poolname); 3373 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3374 pair = nvlist_next_nvpair(snaps, pair)) { 3375 const char *name = nvpair_name(pair); 3376 const char *cp = strchr(name, '@'); 3377 3378 /* 3379 * The snap name must contain an @, and the part after it must 3380 * contain only valid characters. 3381 */ 3382 if (cp == NULL || 3383 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3384 return (SET_ERROR(EINVAL)); 3385 3386 /* 3387 * The snap must be in the specified pool. 3388 */ 3389 if (strncmp(name, poolname, poollen) != 0 || 3390 (name[poollen] != '/' && name[poollen] != '@')) 3391 return (SET_ERROR(EXDEV)); 3392 3393 /* This must be the only snap of this fs. */ 3394 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair); 3395 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) { 3396 if (strncmp(name, nvpair_name(pair2), cp - name + 1) 3397 == 0) { 3398 return (SET_ERROR(EXDEV)); 3399 } 3400 } 3401 } 3402 3403 error = dsl_dataset_snapshot(snaps, props, outnvl); 3404 return (error); 3405} 3406 3407/* 3408 * innvl: "message" -> string 3409 */ 3410/* ARGSUSED */ 3411static int 3412zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl) 3413{ 3414 char *message; 3415 spa_t *spa; 3416 int error; 3417 char *poolname; 3418 3419 /* 3420 * The poolname in the ioctl is not set, we get it from the TSD, 3421 * which was set at the end of the last successful ioctl that allows 3422 * logging. The secpolicy func already checked that it is set. 3423 * Only one log ioctl is allowed after each successful ioctl, so 3424 * we clear the TSD here. 3425 */ 3426 poolname = tsd_get(zfs_allow_log_key); 3427 (void) tsd_set(zfs_allow_log_key, NULL); 3428 error = spa_open(poolname, &spa, FTAG); 3429 strfree(poolname); 3430 if (error != 0) 3431 return (error); 3432 3433 if (nvlist_lookup_string(innvl, "message", &message) != 0) { 3434 spa_close(spa, FTAG); 3435 return (SET_ERROR(EINVAL)); 3436 } 3437 3438 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 3439 spa_close(spa, FTAG); 3440 return (SET_ERROR(ENOTSUP)); 3441 } 3442 3443 error = spa_history_log(spa, message); 3444 spa_close(spa, FTAG); 3445 return (error); 3446} 3447 3448/* 3449 * The dp_config_rwlock must not be held when calling this, because the 3450 * unmount may need to write out data. 3451 * 3452 * This function is best-effort. Callers must deal gracefully if it 3453 * remains mounted (or is remounted after this call). 3454 * 3455 * Returns 0 if the argument is not a snapshot, or it is not currently a 3456 * filesystem, or we were able to unmount it. Returns error code otherwise. 3457 */ 3458int 3459zfs_unmount_snap(const char *snapname) 3460{ 3461 vfs_t *vfsp; 3462 zfsvfs_t *zfsvfs; 3463 int err; 3464 3465 if (strchr(snapname, '@') == NULL) 3466 return (0); 3467 3468 vfsp = zfs_get_vfs(snapname); 3469 if (vfsp == NULL) 3470 return (0); 3471 3472 zfsvfs = vfsp->vfs_data; 3473 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os))); 3474 3475 err = vn_vfswlock(vfsp->vfs_vnodecovered); 3476 VFS_RELE(vfsp); 3477 if (err != 0) 3478 return (SET_ERROR(err)); 3479 3480 /* 3481 * Always force the unmount for snapshots. 3482 */ 3483 3484#ifdef illumos 3485 (void) dounmount(vfsp, MS_FORCE, kcred); 3486#else 3487 vfs_ref(vfsp); 3488 (void) dounmount(vfsp, MS_FORCE, curthread); 3489#endif 3490 return (0); 3491} 3492 3493/* ARGSUSED */ 3494static int 3495zfs_unmount_snap_cb(const char *snapname, void *arg) 3496{ 3497 return (zfs_unmount_snap(snapname)); 3498} 3499 3500/* 3501 * When a clone is destroyed, its origin may also need to be destroyed, 3502 * in which case it must be unmounted. This routine will do that unmount 3503 * if necessary. 3504 */ 3505void 3506zfs_destroy_unmount_origin(const char *fsname) 3507{ 3508 int error; 3509 objset_t *os; 3510 dsl_dataset_t *ds; 3511 3512 error = dmu_objset_hold(fsname, FTAG, &os); 3513 if (error != 0) 3514 return; 3515 ds = dmu_objset_ds(os); 3516 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) { 3517 char originname[MAXNAMELEN]; 3518 dsl_dataset_name(ds->ds_prev, originname); 3519 dmu_objset_rele(os, FTAG); 3520 (void) zfs_unmount_snap(originname); 3521 } else { 3522 dmu_objset_rele(os, FTAG); 3523 } 3524} 3525 3526/* 3527 * innvl: { 3528 * "snaps" -> { snapshot1, snapshot2 } 3529 * (optional boolean) "defer" 3530 * } 3531 * 3532 * outnvl: snapshot -> error code (int32) 3533 * 3534 */ 3535/* ARGSUSED */ 3536static int 3537zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3538{ 3539 int error, poollen; 3540 nvlist_t *snaps; 3541 nvpair_t *pair; 3542 boolean_t defer; 3543 3544 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3545 return (SET_ERROR(EINVAL)); 3546 defer = nvlist_exists(innvl, "defer"); 3547 3548 poollen = strlen(poolname); 3549 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3550 pair = nvlist_next_nvpair(snaps, pair)) { 3551 const char *name = nvpair_name(pair); 3552 3553 /* 3554 * The snap must be in the specified pool to prevent the 3555 * invalid removal of zvol minors below. 3556 */ 3557 if (strncmp(name, poolname, poollen) != 0 || 3558 (name[poollen] != '/' && name[poollen] != '@')) 3559 return (SET_ERROR(EXDEV)); 3560 3561 error = zfs_unmount_snap(name); 3562 if (error != 0) 3563 return (error); 3564#if defined(__FreeBSD__) 3565 zvol_remove_minors(name); 3566#endif 3567 } 3568 3569 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl)); 3570} 3571 3572/* 3573 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>. 3574 * All bookmarks must be in the same pool. 3575 * 3576 * innvl: { 3577 * bookmark1 -> snapshot1, bookmark2 -> snapshot2 3578 * } 3579 * 3580 * outnvl: bookmark -> error code (int32) 3581 * 3582 */ 3583/* ARGSUSED */ 3584static int 3585zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3586{ 3587 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 3588 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 3589 char *snap_name; 3590 3591 /* 3592 * Verify the snapshot argument. 3593 */ 3594 if (nvpair_value_string(pair, &snap_name) != 0) 3595 return (SET_ERROR(EINVAL)); 3596 3597 3598 /* Verify that the keys (bookmarks) are unique */ 3599 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair); 3600 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) { 3601 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0) 3602 return (SET_ERROR(EINVAL)); 3603 } 3604 } 3605 3606 return (dsl_bookmark_create(innvl, outnvl)); 3607} 3608 3609/* 3610 * innvl: { 3611 * property 1, property 2, ... 3612 * } 3613 * 3614 * outnvl: { 3615 * bookmark name 1 -> { property 1, property 2, ... }, 3616 * bookmark name 2 -> { property 1, property 2, ... } 3617 * } 3618 * 3619 */ 3620static int 3621zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3622{ 3623 return (dsl_get_bookmarks(fsname, innvl, outnvl)); 3624} 3625 3626/* 3627 * innvl: { 3628 * bookmark name 1, bookmark name 2 3629 * } 3630 * 3631 * outnvl: bookmark -> error code (int32) 3632 * 3633 */ 3634static int 3635zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl, 3636 nvlist_t *outnvl) 3637{ 3638 int error, poollen; 3639 3640 poollen = strlen(poolname); 3641 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 3642 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 3643 const char *name = nvpair_name(pair); 3644 const char *cp = strchr(name, '#'); 3645 3646 /* 3647 * The bookmark name must contain an #, and the part after it 3648 * must contain only valid characters. 3649 */ 3650 if (cp == NULL || 3651 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3652 return (SET_ERROR(EINVAL)); 3653 3654 /* 3655 * The bookmark must be in the specified pool. 3656 */ 3657 if (strncmp(name, poolname, poollen) != 0 || 3658 (name[poollen] != '/' && name[poollen] != '#')) 3659 return (SET_ERROR(EXDEV)); 3660 } 3661 3662 error = dsl_bookmark_destroy(innvl, outnvl); 3663 return (error); 3664} 3665 3666/* 3667 * inputs: 3668 * zc_name name of dataset to destroy 3669 * zc_objset_type type of objset 3670 * zc_defer_destroy mark for deferred destroy 3671 * 3672 * outputs: none 3673 */ 3674static int 3675zfs_ioc_destroy(zfs_cmd_t *zc) 3676{ 3677 int err; 3678 3679 if (zc->zc_objset_type == DMU_OST_ZFS) { 3680 err = zfs_unmount_snap(zc->zc_name); 3681 if (err != 0) 3682 return (err); 3683 } 3684 3685 if (strchr(zc->zc_name, '@')) 3686 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy); 3687 else 3688 err = dsl_destroy_head(zc->zc_name); 3689 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 3690#ifdef __FreeBSD__ 3691 zvol_remove_minors(zc->zc_name); 3692#else 3693 (void) zvol_remove_minor(zc->zc_name); 3694#endif 3695 return (err); 3696} 3697 3698/* 3699 * fsname is name of dataset to rollback (to most recent snapshot) 3700 * 3701 * innvl is not used. 3702 * 3703 * outnvl: "target" -> name of most recent snapshot 3704 * } 3705 */ 3706/* ARGSUSED */ 3707static int 3708zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl) 3709{ 3710 zfsvfs_t *zfsvfs; 3711 int error; 3712 3713 if (getzfsvfs(fsname, &zfsvfs) == 0) { 3714 error = zfs_suspend_fs(zfsvfs); 3715 if (error == 0) { 3716 int resume_err; 3717 3718 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl); 3719 resume_err = zfs_resume_fs(zfsvfs, fsname); 3720 error = error ? error : resume_err; 3721 } 3722 VFS_RELE(zfsvfs->z_vfs); 3723 } else { 3724 error = dsl_dataset_rollback(fsname, NULL, outnvl); 3725 } 3726 return (error); 3727} 3728 3729static int 3730recursive_unmount(const char *fsname, void *arg) 3731{ 3732 const char *snapname = arg; 3733 char fullname[MAXNAMELEN]; 3734 3735 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname); 3736 return (zfs_unmount_snap(fullname)); 3737} 3738 3739/* 3740 * inputs: 3741 * zc_name old name of dataset 3742 * zc_value new name of dataset 3743 * zc_cookie recursive flag (only valid for snapshots) 3744 * 3745 * outputs: none 3746 */ 3747static int 3748zfs_ioc_rename(zfs_cmd_t *zc) 3749{ 3750 boolean_t recursive = zc->zc_cookie & 1; 3751 char *at; 3752 boolean_t allow_mounted = B_TRUE; 3753 3754#ifdef __FreeBSD__ 3755 allow_mounted = (zc->zc_cookie & 2) != 0; 3756#endif 3757 3758 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 3759 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3760 strchr(zc->zc_value, '%')) 3761 return (SET_ERROR(EINVAL)); 3762 3763 at = strchr(zc->zc_name, '@'); 3764 if (at != NULL) { 3765 /* snaps must be in same fs */ 3766 int error; 3767 3768 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1)) 3769 return (SET_ERROR(EXDEV)); 3770 *at = '\0'; 3771 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) { 3772 error = dmu_objset_find(zc->zc_name, 3773 recursive_unmount, at + 1, 3774 recursive ? DS_FIND_CHILDREN : 0); 3775 if (error != 0) { 3776 *at = '@'; 3777 return (error); 3778 } 3779 } 3780 error = dsl_dataset_rename_snapshot(zc->zc_name, 3781 at + 1, strchr(zc->zc_value, '@') + 1, recursive); 3782 *at = '@'; 3783 3784 return (error); 3785 } else { 3786#ifdef illumos 3787 if (zc->zc_objset_type == DMU_OST_ZVOL) 3788 (void) zvol_remove_minor(zc->zc_name); 3789#endif 3790 return (dsl_dir_rename(zc->zc_name, zc->zc_value)); 3791 } 3792} 3793 3794static int 3795zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 3796{ 3797 const char *propname = nvpair_name(pair); 3798 boolean_t issnap = (strchr(dsname, '@') != NULL); 3799 zfs_prop_t prop = zfs_name_to_prop(propname); 3800 uint64_t intval; 3801 int err; 3802 3803 if (prop == ZPROP_INVAL) { 3804 if (zfs_prop_user(propname)) { 3805 if (err = zfs_secpolicy_write_perms(dsname, 3806 ZFS_DELEG_PERM_USERPROP, cr)) 3807 return (err); 3808 return (0); 3809 } 3810 3811 if (!issnap && zfs_prop_userquota(propname)) { 3812 const char *perm = NULL; 3813 const char *uq_prefix = 3814 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 3815 const char *gq_prefix = 3816 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 3817 3818 if (strncmp(propname, uq_prefix, 3819 strlen(uq_prefix)) == 0) { 3820 perm = ZFS_DELEG_PERM_USERQUOTA; 3821 } else if (strncmp(propname, gq_prefix, 3822 strlen(gq_prefix)) == 0) { 3823 perm = ZFS_DELEG_PERM_GROUPQUOTA; 3824 } else { 3825 /* USERUSED and GROUPUSED are read-only */ 3826 return (SET_ERROR(EINVAL)); 3827 } 3828 3829 if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 3830 return (err); 3831 return (0); 3832 } 3833 3834 return (SET_ERROR(EINVAL)); 3835 } 3836 3837 if (issnap) 3838 return (SET_ERROR(EINVAL)); 3839 3840 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 3841 /* 3842 * dsl_prop_get_all_impl() returns properties in this 3843 * format. 3844 */ 3845 nvlist_t *attrs; 3846 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 3847 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 3848 &pair) == 0); 3849 } 3850 3851 /* 3852 * Check that this value is valid for this pool version 3853 */ 3854 switch (prop) { 3855 case ZFS_PROP_COMPRESSION: 3856 /* 3857 * If the user specified gzip compression, make sure 3858 * the SPA supports it. We ignore any errors here since 3859 * we'll catch them later. 3860 */ 3861 if (nvpair_value_uint64(pair, &intval) == 0) { 3862 if (intval >= ZIO_COMPRESS_GZIP_1 && 3863 intval <= ZIO_COMPRESS_GZIP_9 && 3864 zfs_earlier_version(dsname, 3865 SPA_VERSION_GZIP_COMPRESSION)) { 3866 return (SET_ERROR(ENOTSUP)); 3867 } 3868 3869 if (intval == ZIO_COMPRESS_ZLE && 3870 zfs_earlier_version(dsname, 3871 SPA_VERSION_ZLE_COMPRESSION)) 3872 return (SET_ERROR(ENOTSUP)); 3873 3874 if (intval == ZIO_COMPRESS_LZ4) { 3875 spa_t *spa; 3876 3877 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3878 return (err); 3879 3880 if (!spa_feature_is_enabled(spa, 3881 SPA_FEATURE_LZ4_COMPRESS)) { 3882 spa_close(spa, FTAG); 3883 return (SET_ERROR(ENOTSUP)); 3884 } 3885 spa_close(spa, FTAG); 3886 } 3887 3888 /* 3889 * If this is a bootable dataset then 3890 * verify that the compression algorithm 3891 * is supported for booting. We must return 3892 * something other than ENOTSUP since it 3893 * implies a downrev pool version. 3894 */ 3895 if (zfs_is_bootfs(dsname) && 3896 !BOOTFS_COMPRESS_VALID(intval)) { 3897 return (SET_ERROR(ERANGE)); 3898 } 3899 } 3900 break; 3901 3902 case ZFS_PROP_COPIES: 3903 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 3904 return (SET_ERROR(ENOTSUP)); 3905 break; 3906 3907 case ZFS_PROP_RECORDSIZE: 3908 /* Record sizes above 128k need the feature to be enabled */ 3909 if (nvpair_value_uint64(pair, &intval) == 0 && 3910 intval > SPA_OLD_MAXBLOCKSIZE) { 3911 spa_t *spa; 3912 3913 /* 3914 * If this is a bootable dataset then 3915 * the we don't allow large (>128K) blocks, 3916 * because GRUB doesn't support them. 3917 */ 3918 if (zfs_is_bootfs(dsname) && 3919 intval > SPA_OLD_MAXBLOCKSIZE) { 3920 return (SET_ERROR(ERANGE)); 3921 } 3922 3923 /* 3924 * We don't allow setting the property above 1MB, 3925 * unless the tunable has been changed. 3926 */ 3927 if (intval > zfs_max_recordsize || 3928 intval > SPA_MAXBLOCKSIZE) 3929 return (SET_ERROR(ERANGE)); 3930 3931 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3932 return (err); 3933 3934 if (!spa_feature_is_enabled(spa, 3935 SPA_FEATURE_LARGE_BLOCKS)) { 3936 spa_close(spa, FTAG); 3937 return (SET_ERROR(ENOTSUP)); 3938 } 3939 spa_close(spa, FTAG); 3940 } 3941 break; 3942 3943 case ZFS_PROP_SHARESMB: 3944 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 3945 return (SET_ERROR(ENOTSUP)); 3946 break; 3947 3948 case ZFS_PROP_ACLINHERIT: 3949 if (nvpair_type(pair) == DATA_TYPE_UINT64 && 3950 nvpair_value_uint64(pair, &intval) == 0) { 3951 if (intval == ZFS_ACL_PASSTHROUGH_X && 3952 zfs_earlier_version(dsname, 3953 SPA_VERSION_PASSTHROUGH_X)) 3954 return (SET_ERROR(ENOTSUP)); 3955 } 3956 break; 3957 3958 case ZFS_PROP_CHECKSUM: 3959 case ZFS_PROP_DEDUP: 3960 { 3961 spa_feature_t feature; 3962 spa_t *spa; 3963 3964 /* dedup feature version checks */ 3965 if (prop == ZFS_PROP_DEDUP && 3966 zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 3967 return (SET_ERROR(ENOTSUP)); 3968 3969 if (nvpair_value_uint64(pair, &intval) != 0) 3970 return (SET_ERROR(EINVAL)); 3971 3972 /* check prop value is enabled in features */ 3973 feature = zio_checksum_to_feature(intval); 3974 if (feature == SPA_FEATURE_NONE) 3975 break; 3976 3977 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3978 return (err); 3979 /* 3980 * Salted checksums are not supported on root pools. 3981 */ 3982 if (spa_bootfs(spa) != 0 && 3983 intval < ZIO_CHECKSUM_FUNCTIONS && 3984 (zio_checksum_table[intval].ci_flags & 3985 ZCHECKSUM_FLAG_SALTED)) { 3986 spa_close(spa, FTAG); 3987 return (SET_ERROR(ERANGE)); 3988 } 3989 if (!spa_feature_is_enabled(spa, feature)) { 3990 spa_close(spa, FTAG); 3991 return (SET_ERROR(ENOTSUP)); 3992 } 3993 spa_close(spa, FTAG); 3994 break; 3995 } 3996 } 3997 3998 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 3999} 4000 4001/* 4002 * Checks for a race condition to make sure we don't increment a feature flag 4003 * multiple times. 4004 */ 4005static int 4006zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx) 4007{ 4008 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 4009 spa_feature_t *featurep = arg; 4010 4011 if (!spa_feature_is_active(spa, *featurep)) 4012 return (0); 4013 else 4014 return (SET_ERROR(EBUSY)); 4015} 4016 4017/* 4018 * The callback invoked on feature activation in the sync task caused by 4019 * zfs_prop_activate_feature. 4020 */ 4021static void 4022zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx) 4023{ 4024 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 4025 spa_feature_t *featurep = arg; 4026 4027 spa_feature_incr(spa, *featurep, tx); 4028} 4029 4030/* 4031 * Activates a feature on a pool in response to a property setting. This 4032 * creates a new sync task which modifies the pool to reflect the feature 4033 * as being active. 4034 */ 4035static int 4036zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature) 4037{ 4038 int err; 4039 4040 /* EBUSY here indicates that the feature is already active */ 4041 err = dsl_sync_task(spa_name(spa), 4042 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync, 4043 &feature, 2, ZFS_SPACE_CHECK_RESERVED); 4044 4045 if (err != 0 && err != EBUSY) 4046 return (err); 4047 else 4048 return (0); 4049} 4050 4051/* 4052 * Removes properties from the given props list that fail permission checks 4053 * needed to clear them and to restore them in case of a receive error. For each 4054 * property, make sure we have both set and inherit permissions. 4055 * 4056 * Returns the first error encountered if any permission checks fail. If the 4057 * caller provides a non-NULL errlist, it also gives the complete list of names 4058 * of all the properties that failed a permission check along with the 4059 * corresponding error numbers. The caller is responsible for freeing the 4060 * returned errlist. 4061 * 4062 * If every property checks out successfully, zero is returned and the list 4063 * pointed at by errlist is NULL. 4064 */ 4065static int 4066zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 4067{ 4068 zfs_cmd_t *zc; 4069 nvpair_t *pair, *next_pair; 4070 nvlist_t *errors; 4071 int err, rv = 0; 4072 4073 if (props == NULL) 4074 return (0); 4075 4076 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4077 4078 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 4079 (void) strcpy(zc->zc_name, dataset); 4080 pair = nvlist_next_nvpair(props, NULL); 4081 while (pair != NULL) { 4082 next_pair = nvlist_next_nvpair(props, pair); 4083 4084 (void) strcpy(zc->zc_value, nvpair_name(pair)); 4085 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 4086 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) { 4087 VERIFY(nvlist_remove_nvpair(props, pair) == 0); 4088 VERIFY(nvlist_add_int32(errors, 4089 zc->zc_value, err) == 0); 4090 } 4091 pair = next_pair; 4092 } 4093 kmem_free(zc, sizeof (zfs_cmd_t)); 4094 4095 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 4096 nvlist_free(errors); 4097 errors = NULL; 4098 } else { 4099 VERIFY(nvpair_value_int32(pair, &rv) == 0); 4100 } 4101 4102 if (errlist == NULL) 4103 nvlist_free(errors); 4104 else 4105 *errlist = errors; 4106 4107 return (rv); 4108} 4109 4110static boolean_t 4111propval_equals(nvpair_t *p1, nvpair_t *p2) 4112{ 4113 if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 4114 /* dsl_prop_get_all_impl() format */ 4115 nvlist_t *attrs; 4116 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 4117 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4118 &p1) == 0); 4119 } 4120 4121 if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 4122 nvlist_t *attrs; 4123 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 4124 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4125 &p2) == 0); 4126 } 4127 4128 if (nvpair_type(p1) != nvpair_type(p2)) 4129 return (B_FALSE); 4130 4131 if (nvpair_type(p1) == DATA_TYPE_STRING) { 4132 char *valstr1, *valstr2; 4133 4134 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 4135 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 4136 return (strcmp(valstr1, valstr2) == 0); 4137 } else { 4138 uint64_t intval1, intval2; 4139 4140 VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 4141 VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 4142 return (intval1 == intval2); 4143 } 4144} 4145 4146/* 4147 * Remove properties from props if they are not going to change (as determined 4148 * by comparison with origprops). Remove them from origprops as well, since we 4149 * do not need to clear or restore properties that won't change. 4150 */ 4151static void 4152props_reduce(nvlist_t *props, nvlist_t *origprops) 4153{ 4154 nvpair_t *pair, *next_pair; 4155 4156 if (origprops == NULL) 4157 return; /* all props need to be received */ 4158 4159 pair = nvlist_next_nvpair(props, NULL); 4160 while (pair != NULL) { 4161 const char *propname = nvpair_name(pair); 4162 nvpair_t *match; 4163 4164 next_pair = nvlist_next_nvpair(props, pair); 4165 4166 if ((nvlist_lookup_nvpair(origprops, propname, 4167 &match) != 0) || !propval_equals(pair, match)) 4168 goto next; /* need to set received value */ 4169 4170 /* don't clear the existing received value */ 4171 (void) nvlist_remove_nvpair(origprops, match); 4172 /* don't bother receiving the property */ 4173 (void) nvlist_remove_nvpair(props, pair); 4174next: 4175 pair = next_pair; 4176 } 4177} 4178 4179#ifdef DEBUG 4180static boolean_t zfs_ioc_recv_inject_err; 4181#endif 4182 4183/* 4184 * inputs: 4185 * zc_name name of containing filesystem 4186 * zc_nvlist_src{_size} nvlist of properties to apply 4187 * zc_value name of snapshot to create 4188 * zc_string name of clone origin (if DRR_FLAG_CLONE) 4189 * zc_cookie file descriptor to recv from 4190 * zc_begin_record the BEGIN record of the stream (not byteswapped) 4191 * zc_guid force flag 4192 * zc_cleanup_fd cleanup-on-exit file descriptor 4193 * zc_action_handle handle for this guid/ds mapping (or zero on first call) 4194 * zc_resumable if data is incomplete assume sender will resume 4195 * 4196 * outputs: 4197 * zc_cookie number of bytes read 4198 * zc_nvlist_dst{_size} error for each unapplied received property 4199 * zc_obj zprop_errflags_t 4200 * zc_action_handle handle for this guid/ds mapping 4201 */ 4202static int 4203zfs_ioc_recv(zfs_cmd_t *zc) 4204{ 4205 file_t *fp; 4206 dmu_recv_cookie_t drc; 4207 boolean_t force = (boolean_t)zc->zc_guid; 4208 int fd; 4209 int error = 0; 4210 int props_error = 0; 4211 nvlist_t *errors; 4212 offset_t off; 4213 nvlist_t *props = NULL; /* sent properties */ 4214 nvlist_t *origprops = NULL; /* existing properties */ 4215 char *origin = NULL; 4216 char *tosnap; 4217 char tofs[ZFS_MAXNAMELEN]; 4218 cap_rights_t rights; 4219 boolean_t first_recvd_props = B_FALSE; 4220 4221 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 4222 strchr(zc->zc_value, '@') == NULL || 4223 strchr(zc->zc_value, '%')) 4224 return (SET_ERROR(EINVAL)); 4225 4226 (void) strcpy(tofs, zc->zc_value); 4227 tosnap = strchr(tofs, '@'); 4228 *tosnap++ = '\0'; 4229 4230 if (zc->zc_nvlist_src != 0 && 4231 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 4232 zc->zc_iflags, &props)) != 0) 4233 return (error); 4234 4235 fd = zc->zc_cookie; 4236#ifdef illumos 4237 fp = getf(fd); 4238#else 4239 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp); 4240#endif 4241 if (fp == NULL) { 4242 nvlist_free(props); 4243 return (SET_ERROR(EBADF)); 4244 } 4245 4246 errors = fnvlist_alloc(); 4247 4248 if (zc->zc_string[0]) 4249 origin = zc->zc_string; 4250 4251 error = dmu_recv_begin(tofs, tosnap, 4252 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc); 4253 if (error != 0) 4254 goto out; 4255 4256 /* 4257 * Set properties before we receive the stream so that they are applied 4258 * to the new data. Note that we must call dmu_recv_stream() if 4259 * dmu_recv_begin() succeeds. 4260 */ 4261 if (props != NULL && !drc.drc_newfs) { 4262 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >= 4263 SPA_VERSION_RECVD_PROPS && 4264 !dsl_prop_get_hasrecvd(tofs)) 4265 first_recvd_props = B_TRUE; 4266 4267 /* 4268 * If new received properties are supplied, they are to 4269 * completely replace the existing received properties, so stash 4270 * away the existing ones. 4271 */ 4272 if (dsl_prop_get_received(tofs, &origprops) == 0) { 4273 nvlist_t *errlist = NULL; 4274 /* 4275 * Don't bother writing a property if its value won't 4276 * change (and avoid the unnecessary security checks). 4277 * 4278 * The first receive after SPA_VERSION_RECVD_PROPS is a 4279 * special case where we blow away all local properties 4280 * regardless. 4281 */ 4282 if (!first_recvd_props) 4283 props_reduce(props, origprops); 4284 if (zfs_check_clearable(tofs, origprops, &errlist) != 0) 4285 (void) nvlist_merge(errors, errlist, 0); 4286 nvlist_free(errlist); 4287 4288 if (clear_received_props(tofs, origprops, 4289 first_recvd_props ? NULL : props) != 0) 4290 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 4291 } else { 4292 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 4293 } 4294 } 4295 4296 if (props != NULL) { 4297 props_error = dsl_prop_set_hasrecvd(tofs); 4298 4299 if (props_error == 0) { 4300 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 4301 props, errors); 4302 } 4303 } 4304 4305 if (zc->zc_nvlist_dst_size != 0 && 4306 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 || 4307 put_nvlist(zc, errors) != 0)) { 4308 /* 4309 * Caller made zc->zc_nvlist_dst less than the minimum expected 4310 * size or supplied an invalid address. 4311 */ 4312 props_error = SET_ERROR(EINVAL); 4313 } 4314 4315 off = fp->f_offset; 4316 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd, 4317 &zc->zc_action_handle); 4318 4319 if (error == 0) { 4320 zfsvfs_t *zfsvfs = NULL; 4321 4322 if (getzfsvfs(tofs, &zfsvfs) == 0) { 4323 /* online recv */ 4324 int end_err; 4325 4326 error = zfs_suspend_fs(zfsvfs); 4327 /* 4328 * If the suspend fails, then the recv_end will 4329 * likely also fail, and clean up after itself. 4330 */ 4331 end_err = dmu_recv_end(&drc, zfsvfs); 4332 if (error == 0) 4333 error = zfs_resume_fs(zfsvfs, tofs); 4334 error = error ? error : end_err; 4335 VFS_RELE(zfsvfs->z_vfs); 4336 } else { 4337 error = dmu_recv_end(&drc, NULL); 4338 } 4339 } 4340 4341 zc->zc_cookie = off - fp->f_offset; 4342 if (off >= 0 && off <= MAXOFFSET_T) 4343 fp->f_offset = off; 4344 4345#ifdef DEBUG 4346 if (zfs_ioc_recv_inject_err) { 4347 zfs_ioc_recv_inject_err = B_FALSE; 4348 error = 1; 4349 } 4350#endif 4351 4352#ifdef __FreeBSD__ 4353 if (error == 0) 4354 zvol_create_minors(tofs); 4355#endif 4356 4357 /* 4358 * On error, restore the original props. 4359 */ 4360 if (error != 0 && props != NULL && !drc.drc_newfs) { 4361 if (clear_received_props(tofs, props, NULL) != 0) { 4362 /* 4363 * We failed to clear the received properties. 4364 * Since we may have left a $recvd value on the 4365 * system, we can't clear the $hasrecvd flag. 4366 */ 4367 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4368 } else if (first_recvd_props) { 4369 dsl_prop_unset_hasrecvd(tofs); 4370 } 4371 4372 if (origprops == NULL && !drc.drc_newfs) { 4373 /* We failed to stash the original properties. */ 4374 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4375 } 4376 4377 /* 4378 * dsl_props_set() will not convert RECEIVED to LOCAL on or 4379 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 4380 * explictly if we're restoring local properties cleared in the 4381 * first new-style receive. 4382 */ 4383 if (origprops != NULL && 4384 zfs_set_prop_nvlist(tofs, (first_recvd_props ? 4385 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 4386 origprops, NULL) != 0) { 4387 /* 4388 * We stashed the original properties but failed to 4389 * restore them. 4390 */ 4391 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4392 } 4393 } 4394out: 4395 nvlist_free(props); 4396 nvlist_free(origprops); 4397 nvlist_free(errors); 4398 releasef(fd); 4399 4400 if (error == 0) 4401 error = props_error; 4402 4403 return (error); 4404} 4405 4406/* 4407 * inputs: 4408 * zc_name name of snapshot to send 4409 * zc_cookie file descriptor to send stream to 4410 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 4411 * zc_sendobj objsetid of snapshot to send 4412 * zc_fromobj objsetid of incremental fromsnap (may be zero) 4413 * zc_guid if set, estimate size of stream only. zc_cookie is ignored. 4414 * output size in zc_objset_type. 4415 * zc_flags lzc_send_flags 4416 * 4417 * outputs: 4418 * zc_objset_type estimated size, if zc_guid is set 4419 */ 4420static int 4421zfs_ioc_send(zfs_cmd_t *zc) 4422{ 4423 int error; 4424 offset_t off; 4425 boolean_t estimate = (zc->zc_guid != 0); 4426 boolean_t embedok = (zc->zc_flags & 0x1); 4427 boolean_t large_block_ok = (zc->zc_flags & 0x2); 4428 4429 if (zc->zc_obj != 0) { 4430 dsl_pool_t *dp; 4431 dsl_dataset_t *tosnap; 4432 4433 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4434 if (error != 0) 4435 return (error); 4436 4437 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 4438 if (error != 0) { 4439 dsl_pool_rele(dp, FTAG); 4440 return (error); 4441 } 4442 4443 if (dsl_dir_is_clone(tosnap->ds_dir)) 4444 zc->zc_fromobj = 4445 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj; 4446 dsl_dataset_rele(tosnap, FTAG); 4447 dsl_pool_rele(dp, FTAG); 4448 } 4449 4450 if (estimate) { 4451 dsl_pool_t *dp; 4452 dsl_dataset_t *tosnap; 4453 dsl_dataset_t *fromsnap = NULL; 4454 4455 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4456 if (error != 0) 4457 return (error); 4458 4459 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 4460 if (error != 0) { 4461 dsl_pool_rele(dp, FTAG); 4462 return (error); 4463 } 4464 4465 if (zc->zc_fromobj != 0) { 4466 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, 4467 FTAG, &fromsnap); 4468 if (error != 0) { 4469 dsl_dataset_rele(tosnap, FTAG); 4470 dsl_pool_rele(dp, FTAG); 4471 return (error); 4472 } 4473 } 4474 4475 error = dmu_send_estimate(tosnap, fromsnap, 4476 &zc->zc_objset_type); 4477 4478 if (fromsnap != NULL) 4479 dsl_dataset_rele(fromsnap, FTAG); 4480 dsl_dataset_rele(tosnap, FTAG); 4481 dsl_pool_rele(dp, FTAG); 4482 } else { 4483 file_t *fp; 4484 cap_rights_t rights; 4485 4486#ifdef illumos 4487 fp = getf(zc->zc_cookie); 4488#else 4489 fget_write(curthread, zc->zc_cookie, 4490 cap_rights_init(&rights, CAP_WRITE), &fp); 4491#endif 4492 if (fp == NULL) 4493 return (SET_ERROR(EBADF)); 4494 4495 off = fp->f_offset; 4496 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj, 4497 zc->zc_fromobj, embedok, large_block_ok, 4498#ifdef illumos 4499 zc->zc_cookie, fp->f_vnode, &off); 4500#else 4501 zc->zc_cookie, fp, &off); 4502#endif 4503 4504 if (off >= 0 && off <= MAXOFFSET_T) 4505 fp->f_offset = off; 4506 releasef(zc->zc_cookie); 4507 } 4508 return (error); 4509} 4510 4511/* 4512 * inputs: 4513 * zc_name name of snapshot on which to report progress 4514 * zc_cookie file descriptor of send stream 4515 * 4516 * outputs: 4517 * zc_cookie number of bytes written in send stream thus far 4518 */ 4519static int 4520zfs_ioc_send_progress(zfs_cmd_t *zc) 4521{ 4522 dsl_pool_t *dp; 4523 dsl_dataset_t *ds; 4524 dmu_sendarg_t *dsp = NULL; 4525 int error; 4526 4527 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4528 if (error != 0) 4529 return (error); 4530 4531 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); 4532 if (error != 0) { 4533 dsl_pool_rele(dp, FTAG); 4534 return (error); 4535 } 4536 4537 mutex_enter(&ds->ds_sendstream_lock); 4538 4539 /* 4540 * Iterate over all the send streams currently active on this dataset. 4541 * If there's one which matches the specified file descriptor _and_ the 4542 * stream was started by the current process, return the progress of 4543 * that stream. 4544 */ 4545 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; 4546 dsp = list_next(&ds->ds_sendstreams, dsp)) { 4547 if (dsp->dsa_outfd == zc->zc_cookie && 4548 dsp->dsa_proc == curproc) 4549 break; 4550 } 4551 4552 if (dsp != NULL) 4553 zc->zc_cookie = *(dsp->dsa_off); 4554 else 4555 error = SET_ERROR(ENOENT); 4556 4557 mutex_exit(&ds->ds_sendstream_lock); 4558 dsl_dataset_rele(ds, FTAG); 4559 dsl_pool_rele(dp, FTAG); 4560 return (error); 4561} 4562 4563static int 4564zfs_ioc_inject_fault(zfs_cmd_t *zc) 4565{ 4566 int id, error; 4567 4568 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 4569 &zc->zc_inject_record); 4570 4571 if (error == 0) 4572 zc->zc_guid = (uint64_t)id; 4573 4574 return (error); 4575} 4576 4577static int 4578zfs_ioc_clear_fault(zfs_cmd_t *zc) 4579{ 4580 return (zio_clear_fault((int)zc->zc_guid)); 4581} 4582 4583static int 4584zfs_ioc_inject_list_next(zfs_cmd_t *zc) 4585{ 4586 int id = (int)zc->zc_guid; 4587 int error; 4588 4589 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 4590 &zc->zc_inject_record); 4591 4592 zc->zc_guid = id; 4593 4594 return (error); 4595} 4596 4597static int 4598zfs_ioc_error_log(zfs_cmd_t *zc) 4599{ 4600 spa_t *spa; 4601 int error; 4602 size_t count = (size_t)zc->zc_nvlist_dst_size; 4603 4604 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 4605 return (error); 4606 4607 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 4608 &count); 4609 if (error == 0) 4610 zc->zc_nvlist_dst_size = count; 4611 else 4612 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 4613 4614 spa_close(spa, FTAG); 4615 4616 return (error); 4617} 4618 4619static int 4620zfs_ioc_clear(zfs_cmd_t *zc) 4621{ 4622 spa_t *spa; 4623 vdev_t *vd; 4624 int error; 4625 4626 /* 4627 * On zpool clear we also fix up missing slogs 4628 */ 4629 mutex_enter(&spa_namespace_lock); 4630 spa = spa_lookup(zc->zc_name); 4631 if (spa == NULL) { 4632 mutex_exit(&spa_namespace_lock); 4633 return (SET_ERROR(EIO)); 4634 } 4635 if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 4636 /* we need to let spa_open/spa_load clear the chains */ 4637 spa_set_log_state(spa, SPA_LOG_CLEAR); 4638 } 4639 spa->spa_last_open_failed = 0; 4640 mutex_exit(&spa_namespace_lock); 4641 4642 if (zc->zc_cookie & ZPOOL_NO_REWIND) { 4643 error = spa_open(zc->zc_name, &spa, FTAG); 4644 } else { 4645 nvlist_t *policy; 4646 nvlist_t *config = NULL; 4647 4648 if (zc->zc_nvlist_src == 0) 4649 return (SET_ERROR(EINVAL)); 4650 4651 if ((error = get_nvlist(zc->zc_nvlist_src, 4652 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 4653 error = spa_open_rewind(zc->zc_name, &spa, FTAG, 4654 policy, &config); 4655 if (config != NULL) { 4656 int err; 4657 4658 if ((err = put_nvlist(zc, config)) != 0) 4659 error = err; 4660 nvlist_free(config); 4661 } 4662 nvlist_free(policy); 4663 } 4664 } 4665 4666 if (error != 0) 4667 return (error); 4668 4669 spa_vdev_state_enter(spa, SCL_NONE); 4670 4671 if (zc->zc_guid == 0) { 4672 vd = NULL; 4673 } else { 4674 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 4675 if (vd == NULL) { 4676 (void) spa_vdev_state_exit(spa, NULL, ENODEV); 4677 spa_close(spa, FTAG); 4678 return (SET_ERROR(ENODEV)); 4679 } 4680 } 4681 4682 vdev_clear(spa, vd); 4683 4684 (void) spa_vdev_state_exit(spa, NULL, 0); 4685 4686 /* 4687 * Resume any suspended I/Os. 4688 */ 4689 if (zio_resume(spa) != 0) 4690 error = SET_ERROR(EIO); 4691 4692 spa_close(spa, FTAG); 4693 4694 return (error); 4695} 4696 4697static int 4698zfs_ioc_pool_reopen(zfs_cmd_t *zc) 4699{ 4700 spa_t *spa; 4701 int error; 4702 4703 error = spa_open(zc->zc_name, &spa, FTAG); 4704 if (error != 0) 4705 return (error); 4706 4707 spa_vdev_state_enter(spa, SCL_NONE); 4708 4709 /* 4710 * If a resilver is already in progress then set the 4711 * spa_scrub_reopen flag to B_TRUE so that we don't restart 4712 * the scan as a side effect of the reopen. Otherwise, let 4713 * vdev_open() decided if a resilver is required. 4714 */ 4715 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool); 4716 vdev_reopen(spa->spa_root_vdev); 4717 spa->spa_scrub_reopen = B_FALSE; 4718 4719 (void) spa_vdev_state_exit(spa, NULL, 0); 4720 spa_close(spa, FTAG); 4721 return (0); 4722} 4723/* 4724 * inputs: 4725 * zc_name name of filesystem 4726 * zc_value name of origin snapshot 4727 * 4728 * outputs: 4729 * zc_string name of conflicting snapshot, if there is one 4730 */ 4731static int 4732zfs_ioc_promote(zfs_cmd_t *zc) 4733{ 4734 char *cp; 4735 4736 /* 4737 * We don't need to unmount *all* the origin fs's snapshots, but 4738 * it's easier. 4739 */ 4740 cp = strchr(zc->zc_value, '@'); 4741 if (cp) 4742 *cp = '\0'; 4743 (void) dmu_objset_find(zc->zc_value, 4744 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS); 4745 return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 4746} 4747 4748/* 4749 * Retrieve a single {user|group}{used|quota}@... property. 4750 * 4751 * inputs: 4752 * zc_name name of filesystem 4753 * zc_objset_type zfs_userquota_prop_t 4754 * zc_value domain name (eg. "S-1-234-567-89") 4755 * zc_guid RID/UID/GID 4756 * 4757 * outputs: 4758 * zc_cookie property value 4759 */ 4760static int 4761zfs_ioc_userspace_one(zfs_cmd_t *zc) 4762{ 4763 zfsvfs_t *zfsvfs; 4764 int error; 4765 4766 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 4767 return (SET_ERROR(EINVAL)); 4768 4769 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4770 if (error != 0) 4771 return (error); 4772 4773 error = zfs_userspace_one(zfsvfs, 4774 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 4775 zfsvfs_rele(zfsvfs, FTAG); 4776 4777 return (error); 4778} 4779 4780/* 4781 * inputs: 4782 * zc_name name of filesystem 4783 * zc_cookie zap cursor 4784 * zc_objset_type zfs_userquota_prop_t 4785 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 4786 * 4787 * outputs: 4788 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 4789 * zc_cookie zap cursor 4790 */ 4791static int 4792zfs_ioc_userspace_many(zfs_cmd_t *zc) 4793{ 4794 zfsvfs_t *zfsvfs; 4795 int bufsize = zc->zc_nvlist_dst_size; 4796 4797 if (bufsize <= 0) 4798 return (SET_ERROR(ENOMEM)); 4799 4800 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4801 if (error != 0) 4802 return (error); 4803 4804 void *buf = kmem_alloc(bufsize, KM_SLEEP); 4805 4806 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 4807 buf, &zc->zc_nvlist_dst_size); 4808 4809 if (error == 0) { 4810 error = ddi_copyout(buf, 4811 (void *)(uintptr_t)zc->zc_nvlist_dst, 4812 zc->zc_nvlist_dst_size, zc->zc_iflags); 4813 } 4814 kmem_free(buf, bufsize); 4815 zfsvfs_rele(zfsvfs, FTAG); 4816 4817 return (error); 4818} 4819 4820/* 4821 * inputs: 4822 * zc_name name of filesystem 4823 * 4824 * outputs: 4825 * none 4826 */ 4827static int 4828zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 4829{ 4830 objset_t *os; 4831 int error = 0; 4832 zfsvfs_t *zfsvfs; 4833 4834 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 4835 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 4836 /* 4837 * If userused is not enabled, it may be because the 4838 * objset needs to be closed & reopened (to grow the 4839 * objset_phys_t). Suspend/resume the fs will do that. 4840 */ 4841 error = zfs_suspend_fs(zfsvfs); 4842 if (error == 0) { 4843 dmu_objset_refresh_ownership(zfsvfs->z_os, 4844 zfsvfs); 4845 error = zfs_resume_fs(zfsvfs, zc->zc_name); 4846 } 4847 } 4848 if (error == 0) 4849 error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 4850 VFS_RELE(zfsvfs->z_vfs); 4851 } else { 4852 /* XXX kind of reading contents without owning */ 4853 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 4854 if (error != 0) 4855 return (error); 4856 4857 error = dmu_objset_userspace_upgrade(os); 4858 dmu_objset_rele(os, FTAG); 4859 } 4860 4861 return (error); 4862} 4863 4864#ifdef illumos 4865/* 4866 * We don't want to have a hard dependency 4867 * against some special symbols in sharefs 4868 * nfs, and smbsrv. Determine them if needed when 4869 * the first file system is shared. 4870 * Neither sharefs, nfs or smbsrv are unloadable modules. 4871 */ 4872int (*znfsexport_fs)(void *arg); 4873int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 4874int (*zsmbexport_fs)(void *arg, boolean_t add_share); 4875 4876int zfs_nfsshare_inited; 4877int zfs_smbshare_inited; 4878 4879ddi_modhandle_t nfs_mod; 4880ddi_modhandle_t sharefs_mod; 4881ddi_modhandle_t smbsrv_mod; 4882#endif /* illumos */ 4883kmutex_t zfs_share_lock; 4884 4885#ifdef illumos 4886static int 4887zfs_init_sharefs() 4888{ 4889 int error; 4890 4891 ASSERT(MUTEX_HELD(&zfs_share_lock)); 4892 /* Both NFS and SMB shares also require sharetab support. */ 4893 if (sharefs_mod == NULL && ((sharefs_mod = 4894 ddi_modopen("fs/sharefs", 4895 KRTLD_MODE_FIRST, &error)) == NULL)) { 4896 return (SET_ERROR(ENOSYS)); 4897 } 4898 if (zshare_fs == NULL && ((zshare_fs = 4899 (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 4900 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 4901 return (SET_ERROR(ENOSYS)); 4902 } 4903 return (0); 4904} 4905#endif /* illumos */ 4906 4907static int 4908zfs_ioc_share(zfs_cmd_t *zc) 4909{ 4910#ifdef illumos 4911 int error; 4912 int opcode; 4913 4914 switch (zc->zc_share.z_sharetype) { 4915 case ZFS_SHARE_NFS: 4916 case ZFS_UNSHARE_NFS: 4917 if (zfs_nfsshare_inited == 0) { 4918 mutex_enter(&zfs_share_lock); 4919 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 4920 KRTLD_MODE_FIRST, &error)) == NULL)) { 4921 mutex_exit(&zfs_share_lock); 4922 return (SET_ERROR(ENOSYS)); 4923 } 4924 if (znfsexport_fs == NULL && 4925 ((znfsexport_fs = (int (*)(void *)) 4926 ddi_modsym(nfs_mod, 4927 "nfs_export", &error)) == NULL)) { 4928 mutex_exit(&zfs_share_lock); 4929 return (SET_ERROR(ENOSYS)); 4930 } 4931 error = zfs_init_sharefs(); 4932 if (error != 0) { 4933 mutex_exit(&zfs_share_lock); 4934 return (SET_ERROR(ENOSYS)); 4935 } 4936 zfs_nfsshare_inited = 1; 4937 mutex_exit(&zfs_share_lock); 4938 } 4939 break; 4940 case ZFS_SHARE_SMB: 4941 case ZFS_UNSHARE_SMB: 4942 if (zfs_smbshare_inited == 0) { 4943 mutex_enter(&zfs_share_lock); 4944 if (smbsrv_mod == NULL && ((smbsrv_mod = 4945 ddi_modopen("drv/smbsrv", 4946 KRTLD_MODE_FIRST, &error)) == NULL)) { 4947 mutex_exit(&zfs_share_lock); 4948 return (SET_ERROR(ENOSYS)); 4949 } 4950 if (zsmbexport_fs == NULL && ((zsmbexport_fs = 4951 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 4952 "smb_server_share", &error)) == NULL)) { 4953 mutex_exit(&zfs_share_lock); 4954 return (SET_ERROR(ENOSYS)); 4955 } 4956 error = zfs_init_sharefs(); 4957 if (error != 0) { 4958 mutex_exit(&zfs_share_lock); 4959 return (SET_ERROR(ENOSYS)); 4960 } 4961 zfs_smbshare_inited = 1; 4962 mutex_exit(&zfs_share_lock); 4963 } 4964 break; 4965 default: 4966 return (SET_ERROR(EINVAL)); 4967 } 4968 4969 switch (zc->zc_share.z_sharetype) { 4970 case ZFS_SHARE_NFS: 4971 case ZFS_UNSHARE_NFS: 4972 if (error = 4973 znfsexport_fs((void *) 4974 (uintptr_t)zc->zc_share.z_exportdata)) 4975 return (error); 4976 break; 4977 case ZFS_SHARE_SMB: 4978 case ZFS_UNSHARE_SMB: 4979 if (error = zsmbexport_fs((void *) 4980 (uintptr_t)zc->zc_share.z_exportdata, 4981 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 4982 B_TRUE: B_FALSE)) { 4983 return (error); 4984 } 4985 break; 4986 } 4987 4988 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 4989 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 4990 SHAREFS_ADD : SHAREFS_REMOVE; 4991 4992 /* 4993 * Add or remove share from sharetab 4994 */ 4995 error = zshare_fs(opcode, 4996 (void *)(uintptr_t)zc->zc_share.z_sharedata, 4997 zc->zc_share.z_sharemax); 4998 4999 return (error); 5000 5001#else /* !illumos */ 5002 return (ENOSYS); 5003#endif /* illumos */ 5004} 5005 5006ace_t full_access[] = { 5007 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 5008}; 5009 5010/* 5011 * inputs: 5012 * zc_name name of containing filesystem 5013 * zc_obj object # beyond which we want next in-use object # 5014 * 5015 * outputs: 5016 * zc_obj next in-use object # 5017 */ 5018static int 5019zfs_ioc_next_obj(zfs_cmd_t *zc) 5020{ 5021 objset_t *os = NULL; 5022 int error; 5023 5024 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 5025 if (error != 0) 5026 return (error); 5027 5028 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 5029 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg); 5030 5031 dmu_objset_rele(os, FTAG); 5032 return (error); 5033} 5034 5035/* 5036 * inputs: 5037 * zc_name name of filesystem 5038 * zc_value prefix name for snapshot 5039 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 5040 * 5041 * outputs: 5042 * zc_value short name of new snapshot 5043 */ 5044static int 5045zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 5046{ 5047 char *snap_name; 5048 char *hold_name; 5049 int error; 5050 minor_t minor; 5051 5052 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 5053 if (error != 0) 5054 return (error); 5055 5056 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 5057 (u_longlong_t)ddi_get_lbolt64()); 5058 hold_name = kmem_asprintf("%%%s", zc->zc_value); 5059 5060 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor, 5061 hold_name); 5062 if (error == 0) 5063 (void) strcpy(zc->zc_value, snap_name); 5064 strfree(snap_name); 5065 strfree(hold_name); 5066 zfs_onexit_fd_rele(zc->zc_cleanup_fd); 5067 return (error); 5068} 5069 5070/* 5071 * inputs: 5072 * zc_name name of "to" snapshot 5073 * zc_value name of "from" snapshot 5074 * zc_cookie file descriptor to write diff data on 5075 * 5076 * outputs: 5077 * dmu_diff_record_t's to the file descriptor 5078 */ 5079static int 5080zfs_ioc_diff(zfs_cmd_t *zc) 5081{ 5082 file_t *fp; 5083 cap_rights_t rights; 5084 offset_t off; 5085 int error; 5086 5087#ifdef illumos 5088 fp = getf(zc->zc_cookie); 5089#else 5090 fget_write(curthread, zc->zc_cookie, 5091 cap_rights_init(&rights, CAP_WRITE), &fp); 5092#endif 5093 if (fp == NULL) 5094 return (SET_ERROR(EBADF)); 5095 5096 off = fp->f_offset; 5097 5098#ifdef illumos 5099 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off); 5100#else 5101 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off); 5102#endif 5103 5104 if (off >= 0 && off <= MAXOFFSET_T) 5105 fp->f_offset = off; 5106 releasef(zc->zc_cookie); 5107 5108 return (error); 5109} 5110 5111#ifdef illumos 5112/* 5113 * Remove all ACL files in shares dir 5114 */ 5115static int 5116zfs_smb_acl_purge(znode_t *dzp) 5117{ 5118 zap_cursor_t zc; 5119 zap_attribute_t zap; 5120 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 5121 int error; 5122 5123 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 5124 (error = zap_cursor_retrieve(&zc, &zap)) == 0; 5125 zap_cursor_advance(&zc)) { 5126 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 5127 NULL, 0)) != 0) 5128 break; 5129 } 5130 zap_cursor_fini(&zc); 5131 return (error); 5132} 5133#endif /* illumos */ 5134 5135static int 5136zfs_ioc_smb_acl(zfs_cmd_t *zc) 5137{ 5138#ifdef illumos 5139 vnode_t *vp; 5140 znode_t *dzp; 5141 vnode_t *resourcevp = NULL; 5142 znode_t *sharedir; 5143 zfsvfs_t *zfsvfs; 5144 nvlist_t *nvlist; 5145 char *src, *target; 5146 vattr_t vattr; 5147 vsecattr_t vsec; 5148 int error = 0; 5149 5150 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 5151 NO_FOLLOW, NULL, &vp)) != 0) 5152 return (error); 5153 5154 /* Now make sure mntpnt and dataset are ZFS */ 5155 5156 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 || 5157 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 5158 zc->zc_name) != 0)) { 5159 VN_RELE(vp); 5160 return (SET_ERROR(EINVAL)); 5161 } 5162 5163 dzp = VTOZ(vp); 5164 zfsvfs = dzp->z_zfsvfs; 5165 ZFS_ENTER(zfsvfs); 5166 5167 /* 5168 * Create share dir if its missing. 5169 */ 5170 mutex_enter(&zfsvfs->z_lock); 5171 if (zfsvfs->z_shares_dir == 0) { 5172 dmu_tx_t *tx; 5173 5174 tx = dmu_tx_create(zfsvfs->z_os); 5175 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 5176 ZFS_SHARES_DIR); 5177 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 5178 error = dmu_tx_assign(tx, TXG_WAIT); 5179 if (error != 0) { 5180 dmu_tx_abort(tx); 5181 } else { 5182 error = zfs_create_share_dir(zfsvfs, tx); 5183 dmu_tx_commit(tx); 5184 } 5185 if (error != 0) { 5186 mutex_exit(&zfsvfs->z_lock); 5187 VN_RELE(vp); 5188 ZFS_EXIT(zfsvfs); 5189 return (error); 5190 } 5191 } 5192 mutex_exit(&zfsvfs->z_lock); 5193 5194 ASSERT(zfsvfs->z_shares_dir); 5195 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 5196 VN_RELE(vp); 5197 ZFS_EXIT(zfsvfs); 5198 return (error); 5199 } 5200 5201 switch (zc->zc_cookie) { 5202 case ZFS_SMB_ACL_ADD: 5203 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 5204 vattr.va_type = VREG; 5205 vattr.va_mode = S_IFREG|0777; 5206 vattr.va_uid = 0; 5207 vattr.va_gid = 0; 5208 5209 vsec.vsa_mask = VSA_ACE; 5210 vsec.vsa_aclentp = &full_access; 5211 vsec.vsa_aclentsz = sizeof (full_access); 5212 vsec.vsa_aclcnt = 1; 5213 5214 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 5215 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 5216 if (resourcevp) 5217 VN_RELE(resourcevp); 5218 break; 5219 5220 case ZFS_SMB_ACL_REMOVE: 5221 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 5222 NULL, 0); 5223 break; 5224 5225 case ZFS_SMB_ACL_RENAME: 5226 if ((error = get_nvlist(zc->zc_nvlist_src, 5227 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 5228 VN_RELE(vp); 5229 VN_RELE(ZTOV(sharedir)); 5230 ZFS_EXIT(zfsvfs); 5231 return (error); 5232 } 5233 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 5234 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 5235 &target)) { 5236 VN_RELE(vp); 5237 VN_RELE(ZTOV(sharedir)); 5238 ZFS_EXIT(zfsvfs); 5239 nvlist_free(nvlist); 5240 return (error); 5241 } 5242 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 5243 kcred, NULL, 0); 5244 nvlist_free(nvlist); 5245 break; 5246 5247 case ZFS_SMB_ACL_PURGE: 5248 error = zfs_smb_acl_purge(sharedir); 5249 break; 5250 5251 default: 5252 error = SET_ERROR(EINVAL); 5253 break; 5254 } 5255 5256 VN_RELE(vp); 5257 VN_RELE(ZTOV(sharedir)); 5258 5259 ZFS_EXIT(zfsvfs); 5260 5261 return (error); 5262#else /* !illumos */ 5263 return (EOPNOTSUPP); 5264#endif /* illumos */ 5265} 5266 5267/* 5268 * innvl: { 5269 * "holds" -> { snapname -> holdname (string), ... } 5270 * (optional) "cleanup_fd" -> fd (int32) 5271 * } 5272 * 5273 * outnvl: { 5274 * snapname -> error value (int32) 5275 * ... 5276 * } 5277 */ 5278/* ARGSUSED */ 5279static int 5280zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist) 5281{ 5282 nvpair_t *pair; 5283 nvlist_t *holds; 5284 int cleanup_fd = -1; 5285 int error; 5286 minor_t minor = 0; 5287 5288 error = nvlist_lookup_nvlist(args, "holds", &holds); 5289 if (error != 0) 5290 return (SET_ERROR(EINVAL)); 5291 5292 /* make sure the user didn't pass us any invalid (empty) tags */ 5293 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 5294 pair = nvlist_next_nvpair(holds, pair)) { 5295 char *htag; 5296 5297 error = nvpair_value_string(pair, &htag); 5298 if (error != 0) 5299 return (SET_ERROR(error)); 5300 5301 if (strlen(htag) == 0) 5302 return (SET_ERROR(EINVAL)); 5303 } 5304 5305 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) { 5306 error = zfs_onexit_fd_hold(cleanup_fd, &minor); 5307 if (error != 0) 5308 return (error); 5309 } 5310 5311 error = dsl_dataset_user_hold(holds, minor, errlist); 5312 if (minor != 0) 5313 zfs_onexit_fd_rele(cleanup_fd); 5314 return (error); 5315} 5316 5317/* 5318 * innvl is not used. 5319 * 5320 * outnvl: { 5321 * holdname -> time added (uint64 seconds since epoch) 5322 * ... 5323 * } 5324 */ 5325/* ARGSUSED */ 5326static int 5327zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl) 5328{ 5329 return (dsl_dataset_get_holds(snapname, outnvl)); 5330} 5331 5332/* 5333 * innvl: { 5334 * snapname -> { holdname, ... } 5335 * ... 5336 * } 5337 * 5338 * outnvl: { 5339 * snapname -> error value (int32) 5340 * ... 5341 * } 5342 */ 5343/* ARGSUSED */ 5344static int 5345zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist) 5346{ 5347 return (dsl_dataset_user_release(holds, errlist)); 5348} 5349 5350/* 5351 * inputs: 5352 * zc_name name of new filesystem or snapshot 5353 * zc_value full name of old snapshot 5354 * 5355 * outputs: 5356 * zc_cookie space in bytes 5357 * zc_objset_type compressed space in bytes 5358 * zc_perm_action uncompressed space in bytes 5359 */ 5360static int 5361zfs_ioc_space_written(zfs_cmd_t *zc) 5362{ 5363 int error; 5364 dsl_pool_t *dp; 5365 dsl_dataset_t *new, *old; 5366 5367 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 5368 if (error != 0) 5369 return (error); 5370 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new); 5371 if (error != 0) { 5372 dsl_pool_rele(dp, FTAG); 5373 return (error); 5374 } 5375 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old); 5376 if (error != 0) { 5377 dsl_dataset_rele(new, FTAG); 5378 dsl_pool_rele(dp, FTAG); 5379 return (error); 5380 } 5381 5382 error = dsl_dataset_space_written(old, new, &zc->zc_cookie, 5383 &zc->zc_objset_type, &zc->zc_perm_action); 5384 dsl_dataset_rele(old, FTAG); 5385 dsl_dataset_rele(new, FTAG); 5386 dsl_pool_rele(dp, FTAG); 5387 return (error); 5388} 5389 5390/* 5391 * innvl: { 5392 * "firstsnap" -> snapshot name 5393 * } 5394 * 5395 * outnvl: { 5396 * "used" -> space in bytes 5397 * "compressed" -> compressed space in bytes 5398 * "uncompressed" -> uncompressed space in bytes 5399 * } 5400 */ 5401static int 5402zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl) 5403{ 5404 int error; 5405 dsl_pool_t *dp; 5406 dsl_dataset_t *new, *old; 5407 char *firstsnap; 5408 uint64_t used, comp, uncomp; 5409 5410 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0) 5411 return (SET_ERROR(EINVAL)); 5412 5413 error = dsl_pool_hold(lastsnap, FTAG, &dp); 5414 if (error != 0) 5415 return (error); 5416 5417 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new); 5418 if (error == 0 && !new->ds_is_snapshot) { 5419 dsl_dataset_rele(new, FTAG); 5420 error = SET_ERROR(EINVAL); 5421 } 5422 if (error != 0) { 5423 dsl_pool_rele(dp, FTAG); 5424 return (error); 5425 } 5426 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old); 5427 if (error == 0 && !old->ds_is_snapshot) { 5428 dsl_dataset_rele(old, FTAG); 5429 error = SET_ERROR(EINVAL); 5430 } 5431 if (error != 0) { 5432 dsl_dataset_rele(new, FTAG); 5433 dsl_pool_rele(dp, FTAG); 5434 return (error); 5435 } 5436 5437 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp); 5438 dsl_dataset_rele(old, FTAG); 5439 dsl_dataset_rele(new, FTAG); 5440 dsl_pool_rele(dp, FTAG); 5441 fnvlist_add_uint64(outnvl, "used", used); 5442 fnvlist_add_uint64(outnvl, "compressed", comp); 5443 fnvlist_add_uint64(outnvl, "uncompressed", uncomp); 5444 return (error); 5445} 5446 5447static int 5448zfs_ioc_jail(zfs_cmd_t *zc) 5449{ 5450 5451 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name, 5452 (int)zc->zc_jailid)); 5453} 5454 5455static int 5456zfs_ioc_unjail(zfs_cmd_t *zc) 5457{ 5458 5459 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name, 5460 (int)zc->zc_jailid)); 5461} 5462 5463/* 5464 * innvl: { 5465 * "fd" -> file descriptor to write stream to (int32) 5466 * (optional) "fromsnap" -> full snap name to send an incremental from 5467 * (optional) "largeblockok" -> (value ignored) 5468 * indicates that blocks > 128KB are permitted 5469 * (optional) "embedok" -> (value ignored) 5470 * presence indicates DRR_WRITE_EMBEDDED records are permitted 5471 * (optional) "resume_object" and "resume_offset" -> (uint64) 5472 * if present, resume send stream from specified object and offset. 5473 * } 5474 * 5475 * outnvl is unused 5476 */ 5477/* ARGSUSED */ 5478static int 5479zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 5480{ 5481 cap_rights_t rights; 5482 file_t *fp; 5483 int error; 5484 offset_t off; 5485 char *fromname = NULL; 5486 int fd; 5487 boolean_t largeblockok; 5488 boolean_t embedok; 5489 uint64_t resumeobj = 0; 5490 uint64_t resumeoff = 0; 5491 5492 error = nvlist_lookup_int32(innvl, "fd", &fd); 5493 if (error != 0) 5494 return (SET_ERROR(EINVAL)); 5495 5496 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname); 5497 5498 largeblockok = nvlist_exists(innvl, "largeblockok"); 5499 embedok = nvlist_exists(innvl, "embedok"); 5500 5501 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj); 5502 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff); 5503 5504#ifdef illumos 5505 file_t *fp = getf(fd); 5506#else 5507 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp); 5508#endif 5509 if (fp == NULL) 5510 return (SET_ERROR(EBADF)); 5511 5512 off = fp->f_offset; 5513 error = dmu_send(snapname, fromname, embedok, largeblockok, fd, 5514#ifdef illumos 5515 resumeobj, resumeoff, fp->f_vnode, &off); 5516#else 5517 resumeobj, resumeoff, fp, &off); 5518#endif 5519 5520#ifdef illumos 5521 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 5522 fp->f_offset = off; 5523#else 5524 fp->f_offset = off; 5525#endif 5526 5527 releasef(fd); 5528 return (error); 5529} 5530 5531/* 5532 * Determine approximately how large a zfs send stream will be -- the number 5533 * of bytes that will be written to the fd supplied to zfs_ioc_send_new(). 5534 * 5535 * innvl: { 5536 * (optional) "from" -> full snap or bookmark name to send an incremental 5537 * from 5538 * } 5539 * 5540 * outnvl: { 5541 * "space" -> bytes of space (uint64) 5542 * } 5543 */ 5544static int 5545zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 5546{ 5547 dsl_pool_t *dp; 5548 dsl_dataset_t *tosnap; 5549 int error; 5550 char *fromname; 5551 uint64_t space; 5552 5553 error = dsl_pool_hold(snapname, FTAG, &dp); 5554 if (error != 0) 5555 return (error); 5556 5557 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap); 5558 if (error != 0) { 5559 dsl_pool_rele(dp, FTAG); 5560 return (error); 5561 } 5562 5563 error = nvlist_lookup_string(innvl, "from", &fromname); 5564 if (error == 0) { 5565 if (strchr(fromname, '@') != NULL) { 5566 /* 5567 * If from is a snapshot, hold it and use the more 5568 * efficient dmu_send_estimate to estimate send space 5569 * size using deadlists. 5570 */ 5571 dsl_dataset_t *fromsnap; 5572 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap); 5573 if (error != 0) 5574 goto out; 5575 error = dmu_send_estimate(tosnap, fromsnap, &space); 5576 dsl_dataset_rele(fromsnap, FTAG); 5577 } else if (strchr(fromname, '#') != NULL) { 5578 /* 5579 * If from is a bookmark, fetch the creation TXG of the 5580 * snapshot it was created from and use that to find 5581 * blocks that were born after it. 5582 */ 5583 zfs_bookmark_phys_t frombm; 5584 5585 error = dsl_bookmark_lookup(dp, fromname, tosnap, 5586 &frombm); 5587 if (error != 0) 5588 goto out; 5589 error = dmu_send_estimate_from_txg(tosnap, 5590 frombm.zbm_creation_txg, &space); 5591 } else { 5592 /* 5593 * from is not properly formatted as a snapshot or 5594 * bookmark 5595 */ 5596 error = SET_ERROR(EINVAL); 5597 goto out; 5598 } 5599 } else { 5600 // If estimating the size of a full send, use dmu_send_estimate 5601 error = dmu_send_estimate(tosnap, NULL, &space); 5602 } 5603 5604 fnvlist_add_uint64(outnvl, "space", space); 5605 5606out: 5607 dsl_dataset_rele(tosnap, FTAG); 5608 dsl_pool_rele(dp, FTAG); 5609 return (error); 5610} 5611 5612static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST]; 5613 5614static void 5615zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5616 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 5617 boolean_t log_history, zfs_ioc_poolcheck_t pool_check) 5618{ 5619 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 5620 5621 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 5622 ASSERT3U(ioc, <, ZFS_IOC_LAST); 5623 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 5624 ASSERT3P(vec->zvec_func, ==, NULL); 5625 5626 vec->zvec_legacy_func = func; 5627 vec->zvec_secpolicy = secpolicy; 5628 vec->zvec_namecheck = namecheck; 5629 vec->zvec_allow_log = log_history; 5630 vec->zvec_pool_check = pool_check; 5631} 5632 5633/* 5634 * See the block comment at the beginning of this file for details on 5635 * each argument to this function. 5636 */ 5637static void 5638zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func, 5639 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 5640 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist, 5641 boolean_t allow_log) 5642{ 5643 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 5644 5645 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 5646 ASSERT3U(ioc, <, ZFS_IOC_LAST); 5647 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 5648 ASSERT3P(vec->zvec_func, ==, NULL); 5649 5650 /* if we are logging, the name must be valid */ 5651 ASSERT(!allow_log || namecheck != NO_NAME); 5652 5653 vec->zvec_name = name; 5654 vec->zvec_func = func; 5655 vec->zvec_secpolicy = secpolicy; 5656 vec->zvec_namecheck = namecheck; 5657 vec->zvec_pool_check = pool_check; 5658 vec->zvec_smush_outnvlist = smush_outnvlist; 5659 vec->zvec_allow_log = allow_log; 5660} 5661 5662static void 5663zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5664 zfs_secpolicy_func_t *secpolicy, boolean_t log_history, 5665 zfs_ioc_poolcheck_t pool_check) 5666{ 5667 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5668 POOL_NAME, log_history, pool_check); 5669} 5670 5671static void 5672zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5673 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check) 5674{ 5675 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5676 DATASET_NAME, B_FALSE, pool_check); 5677} 5678 5679static void 5680zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 5681{ 5682 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config, 5683 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5684} 5685 5686static void 5687zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5688 zfs_secpolicy_func_t *secpolicy) 5689{ 5690 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5691 NO_NAME, B_FALSE, POOL_CHECK_NONE); 5692} 5693 5694static void 5695zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc, 5696 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy) 5697{ 5698 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5699 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED); 5700} 5701 5702static void 5703zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 5704{ 5705 zfs_ioctl_register_dataset_read_secpolicy(ioc, func, 5706 zfs_secpolicy_read); 5707} 5708 5709static void 5710zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5711 zfs_secpolicy_func_t *secpolicy) 5712{ 5713 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5714 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5715} 5716 5717static void 5718zfs_ioctl_init(void) 5719{ 5720 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT, 5721 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME, 5722 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5723 5724 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY, 5725 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME, 5726 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE); 5727 5728 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS, 5729 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, 5730 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5731 5732 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW, 5733 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME, 5734 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5735 5736 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE, 5737 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME, 5738 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5739 5740 zfs_ioctl_register("create", ZFS_IOC_CREATE, 5741 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME, 5742 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5743 5744 zfs_ioctl_register("clone", ZFS_IOC_CLONE, 5745 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME, 5746 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5747 5748 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS, 5749 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME, 5750 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5751 5752 zfs_ioctl_register("hold", ZFS_IOC_HOLD, 5753 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME, 5754 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5755 zfs_ioctl_register("release", ZFS_IOC_RELEASE, 5756 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME, 5757 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5758 5759 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS, 5760 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, 5761 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5762 5763 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK, 5764 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, 5765 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE); 5766 5767 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK, 5768 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME, 5769 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5770 5771 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS, 5772 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME, 5773 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5774 5775 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS, 5776 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks, 5777 POOL_NAME, 5778 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5779 5780 /* IOCTLS that use the legacy function signature */ 5781 5782 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, 5783 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY); 5784 5785 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create, 5786 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5787 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN, 5788 zfs_ioc_pool_scan); 5789 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE, 5790 zfs_ioc_pool_upgrade); 5791 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD, 5792 zfs_ioc_vdev_add); 5793 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE, 5794 zfs_ioc_vdev_remove); 5795 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE, 5796 zfs_ioc_vdev_set_state); 5797 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH, 5798 zfs_ioc_vdev_attach); 5799 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH, 5800 zfs_ioc_vdev_detach); 5801 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH, 5802 zfs_ioc_vdev_setpath); 5803 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU, 5804 zfs_ioc_vdev_setfru); 5805 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS, 5806 zfs_ioc_pool_set_props); 5807 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT, 5808 zfs_ioc_vdev_split); 5809 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID, 5810 zfs_ioc_pool_reguid); 5811 5812 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS, 5813 zfs_ioc_pool_configs, zfs_secpolicy_none); 5814 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT, 5815 zfs_ioc_pool_tryimport, zfs_secpolicy_config); 5816 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT, 5817 zfs_ioc_inject_fault, zfs_secpolicy_inject); 5818 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT, 5819 zfs_ioc_clear_fault, zfs_secpolicy_inject); 5820 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT, 5821 zfs_ioc_inject_list_next, zfs_secpolicy_inject); 5822 5823 /* 5824 * pool destroy, and export don't log the history as part of 5825 * zfsdev_ioctl, but rather zfs_ioc_pool_export 5826 * does the logging of those commands. 5827 */ 5828 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy, 5829 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 5830 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export, 5831 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 5832 5833 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats, 5834 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 5835 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props, 5836 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 5837 5838 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log, 5839 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE); 5840 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME, 5841 zfs_ioc_dsobj_to_dsname, 5842 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE); 5843 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY, 5844 zfs_ioc_pool_get_history, 5845 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); 5846 5847 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import, 5848 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5849 5850 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear, 5851 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5852 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen, 5853 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED); 5854 5855 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN, 5856 zfs_ioc_space_written); 5857 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS, 5858 zfs_ioc_objset_recvd_props); 5859 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ, 5860 zfs_ioc_next_obj); 5861 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL, 5862 zfs_ioc_get_fsacl); 5863 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS, 5864 zfs_ioc_objset_stats); 5865 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS, 5866 zfs_ioc_objset_zplprops); 5867 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT, 5868 zfs_ioc_dataset_list_next); 5869 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT, 5870 zfs_ioc_snapshot_list_next); 5871 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS, 5872 zfs_ioc_send_progress); 5873 5874 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF, 5875 zfs_ioc_diff, zfs_secpolicy_diff); 5876 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS, 5877 zfs_ioc_obj_to_stats, zfs_secpolicy_diff); 5878 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH, 5879 zfs_ioc_obj_to_path, zfs_secpolicy_diff); 5880 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE, 5881 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one); 5882 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY, 5883 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many); 5884 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND, 5885 zfs_ioc_send, zfs_secpolicy_send); 5886 5887 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop, 5888 zfs_secpolicy_none); 5889 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy, 5890 zfs_secpolicy_destroy); 5891 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename, 5892 zfs_secpolicy_rename); 5893 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, 5894 zfs_secpolicy_recv); 5895 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote, 5896 zfs_secpolicy_promote); 5897 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP, 5898 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop); 5899 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl, 5900 zfs_secpolicy_set_fsacl); 5901 5902 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share, 5903 zfs_secpolicy_share, POOL_CHECK_NONE); 5904 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl, 5905 zfs_secpolicy_smb_acl, POOL_CHECK_NONE); 5906 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE, 5907 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 5908 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5909 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT, 5910 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, 5911 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5912 5913#ifdef __FreeBSD__ 5914 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail, 5915 zfs_secpolicy_config, POOL_CHECK_NONE); 5916 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail, 5917 zfs_secpolicy_config, POOL_CHECK_NONE); 5918#endif 5919} 5920 5921int 5922pool_status_check(const char *name, zfs_ioc_namecheck_t type, 5923 zfs_ioc_poolcheck_t check) 5924{ 5925 spa_t *spa; 5926 int error; 5927 5928 ASSERT(type == POOL_NAME || type == DATASET_NAME); 5929 5930 if (check & POOL_CHECK_NONE) 5931 return (0); 5932 5933 error = spa_open(name, &spa, FTAG); 5934 if (error == 0) { 5935 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) 5936 error = SET_ERROR(EAGAIN); 5937 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) 5938 error = SET_ERROR(EROFS); 5939 spa_close(spa, FTAG); 5940 } 5941 return (error); 5942} 5943 5944/* 5945 * Find a free minor number. 5946 */ 5947minor_t 5948zfsdev_minor_alloc(void) 5949{ 5950 static minor_t last_minor; 5951 minor_t m; 5952 5953 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5954 5955 for (m = last_minor + 1; m != last_minor; m++) { 5956 if (m > ZFSDEV_MAX_MINOR) 5957 m = 1; 5958 if (ddi_get_soft_state(zfsdev_state, m) == NULL) { 5959 last_minor = m; 5960 return (m); 5961 } 5962 } 5963 5964 return (0); 5965} 5966 5967static int 5968zfs_ctldev_init(struct cdev *devp) 5969{ 5970 minor_t minor; 5971 zfs_soft_state_t *zs; 5972 5973 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5974 5975 minor = zfsdev_minor_alloc(); 5976 if (minor == 0) 5977 return (SET_ERROR(ENXIO)); 5978 5979 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) 5980 return (SET_ERROR(EAGAIN)); 5981 5982 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close); 5983 5984 zs = ddi_get_soft_state(zfsdev_state, minor); 5985 zs->zss_type = ZSST_CTLDEV; 5986 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data); 5987 5988 return (0); 5989} 5990 5991static void 5992zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor) 5993{ 5994 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5995 5996 zfs_onexit_destroy(zo); 5997 ddi_soft_state_free(zfsdev_state, minor); 5998} 5999 6000void * 6001zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which) 6002{ 6003 zfs_soft_state_t *zp; 6004 6005 zp = ddi_get_soft_state(zfsdev_state, minor); 6006 if (zp == NULL || zp->zss_type != which) 6007 return (NULL); 6008 6009 return (zp->zss_data); 6010} 6011 6012static int 6013zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td) 6014{ 6015 int error = 0; 6016 6017#ifdef illumos 6018 if (getminor(*devp) != 0) 6019 return (zvol_open(devp, flag, otyp, cr)); 6020#endif 6021 6022 /* This is the control device. Allocate a new minor if requested. */ 6023 if (flag & FEXCL) { 6024 mutex_enter(&spa_namespace_lock); 6025 error = zfs_ctldev_init(devp); 6026 mutex_exit(&spa_namespace_lock); 6027 } 6028 6029 return (error); 6030} 6031 6032static void 6033zfsdev_close(void *data) 6034{ 6035 zfs_onexit_t *zo; 6036 minor_t minor = (minor_t)(uintptr_t)data; 6037 6038 if (minor == 0) 6039 return; 6040 6041 mutex_enter(&spa_namespace_lock); 6042 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV); 6043 if (zo == NULL) { 6044 mutex_exit(&spa_namespace_lock); 6045 return; 6046 } 6047 zfs_ctldev_destroy(zo, minor); 6048 mutex_exit(&spa_namespace_lock); 6049} 6050 6051static int 6052zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag, 6053 struct thread *td) 6054{ 6055 zfs_cmd_t *zc; 6056 uint_t vecnum; 6057 int error, rc, len; 6058#ifdef illumos 6059 minor_t minor = getminor(dev); 6060#else 6061 zfs_iocparm_t *zc_iocparm; 6062 int cflag, cmd, oldvecnum; 6063 boolean_t newioc, compat; 6064 void *compat_zc = NULL; 6065 cred_t *cr = td->td_ucred; 6066#endif 6067 const zfs_ioc_vec_t *vec; 6068 char *saved_poolname = NULL; 6069 nvlist_t *innvl = NULL; 6070 6071 cflag = ZFS_CMD_COMPAT_NONE; 6072 compat = B_FALSE; 6073 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */ 6074 6075 len = IOCPARM_LEN(zcmd); 6076 vecnum = cmd = zcmd & 0xff; 6077 6078 /* 6079 * Check if we are talking to supported older binaries 6080 * and translate zfs_cmd if necessary 6081 */ 6082 if (len != sizeof(zfs_iocparm_t)) { 6083 newioc = B_FALSE; 6084 compat = B_TRUE; 6085 6086 vecnum = cmd; 6087 6088 switch (len) { 6089 case sizeof(zfs_cmd_zcmd_t): 6090 cflag = ZFS_CMD_COMPAT_LZC; 6091 break; 6092 case sizeof(zfs_cmd_deadman_t): 6093 cflag = ZFS_CMD_COMPAT_DEADMAN; 6094 break; 6095 case sizeof(zfs_cmd_v28_t): 6096 cflag = ZFS_CMD_COMPAT_V28; 6097 break; 6098 case sizeof(zfs_cmd_v15_t): 6099 cflag = ZFS_CMD_COMPAT_V15; 6100 vecnum = zfs_ioctl_v15_to_v28[cmd]; 6101 6102 /* 6103 * Return without further handling 6104 * if the command is blacklisted. 6105 */ 6106 if (vecnum == ZFS_IOC_COMPAT_PASS) 6107 return (0); 6108 else if (vecnum == ZFS_IOC_COMPAT_FAIL) 6109 return (ENOTSUP); 6110 break; 6111 default: 6112 return (EINVAL); 6113 } 6114 } 6115 6116#ifdef illumos 6117 vecnum = cmd - ZFS_IOC_FIRST; 6118 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 6119#endif 6120 6121 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 6122 return (SET_ERROR(EINVAL)); 6123 vec = &zfs_ioc_vec[vecnum]; 6124 6125 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP); 6126 6127#ifdef illumos 6128 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 6129 if (error != 0) { 6130 error = SET_ERROR(EFAULT); 6131 goto out; 6132 } 6133#else /* !illumos */ 6134 bzero(zc, sizeof(zfs_cmd_t)); 6135 6136 if (newioc) { 6137 zc_iocparm = (void *)arg; 6138 6139 switch (zc_iocparm->zfs_ioctl_version) { 6140 case ZFS_IOCVER_CURRENT: 6141 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) { 6142 error = SET_ERROR(EINVAL); 6143 goto out; 6144 } 6145 break; 6146 case ZFS_IOCVER_EDBP: 6147 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) { 6148 error = SET_ERROR(EFAULT); 6149 goto out; 6150 } 6151 compat = B_TRUE; 6152 cflag = ZFS_CMD_COMPAT_EDBP; 6153 break; 6154 case ZFS_IOCVER_ZCMD: 6155 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) || 6156 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) { 6157 error = SET_ERROR(EFAULT); 6158 goto out; 6159 } 6160 compat = B_TRUE; 6161 cflag = ZFS_CMD_COMPAT_ZCMD; 6162 break; 6163 default: 6164 error = SET_ERROR(EINVAL); 6165 goto out; 6166 /* NOTREACHED */ 6167 } 6168 6169 if (compat) { 6170 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size); 6171 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP); 6172 bzero(compat_zc, sizeof(zfs_cmd_t)); 6173 6174 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd, 6175 compat_zc, zc_iocparm->zfs_cmd_size, flag); 6176 if (error != 0) { 6177 error = SET_ERROR(EFAULT); 6178 goto out; 6179 } 6180 } else { 6181 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd, 6182 zc, zc_iocparm->zfs_cmd_size, flag); 6183 if (error != 0) { 6184 error = SET_ERROR(EFAULT); 6185 goto out; 6186 } 6187 } 6188 } 6189 6190 if (compat) { 6191 if (newioc) { 6192 ASSERT(compat_zc != NULL); 6193 zfs_cmd_compat_get(zc, compat_zc, cflag); 6194 } else { 6195 ASSERT(compat_zc == NULL); 6196 zfs_cmd_compat_get(zc, arg, cflag); 6197 } 6198 oldvecnum = vecnum; 6199 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag); 6200 if (error != 0) 6201 goto out; 6202 if (oldvecnum != vecnum) 6203 vec = &zfs_ioc_vec[vecnum]; 6204 } 6205#endif /* !illumos */ 6206 6207 zc->zc_iflags = flag & FKIOCTL; 6208 if (zc->zc_nvlist_src_size != 0) { 6209 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 6210 zc->zc_iflags, &innvl); 6211 if (error != 0) 6212 goto out; 6213 } 6214 6215 /* rewrite innvl for backwards compatibility */ 6216 if (compat) 6217 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag); 6218 6219 /* 6220 * Ensure that all pool/dataset names are valid before we pass down to 6221 * the lower layers. 6222 */ 6223 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 6224 switch (vec->zvec_namecheck) { 6225 case POOL_NAME: 6226 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 6227 error = SET_ERROR(EINVAL); 6228 else 6229 error = pool_status_check(zc->zc_name, 6230 vec->zvec_namecheck, vec->zvec_pool_check); 6231 break; 6232 6233 case DATASET_NAME: 6234 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 6235 error = SET_ERROR(EINVAL); 6236 else 6237 error = pool_status_check(zc->zc_name, 6238 vec->zvec_namecheck, vec->zvec_pool_check); 6239 break; 6240 6241 case NO_NAME: 6242 break; 6243 } 6244 6245 if (error == 0 && !(flag & FKIOCTL)) 6246 error = vec->zvec_secpolicy(zc, innvl, cr); 6247 6248 if (error != 0) 6249 goto out; 6250 6251 /* legacy ioctls can modify zc_name */ 6252 len = strcspn(zc->zc_name, "/@#") + 1; 6253 saved_poolname = kmem_alloc(len, KM_SLEEP); 6254 (void) strlcpy(saved_poolname, zc->zc_name, len); 6255 6256 if (vec->zvec_func != NULL) { 6257 nvlist_t *outnvl; 6258 int puterror = 0; 6259 spa_t *spa; 6260 nvlist_t *lognv = NULL; 6261 6262 ASSERT(vec->zvec_legacy_func == NULL); 6263 6264 /* 6265 * Add the innvl to the lognv before calling the func, 6266 * in case the func changes the innvl. 6267 */ 6268 if (vec->zvec_allow_log) { 6269 lognv = fnvlist_alloc(); 6270 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL, 6271 vec->zvec_name); 6272 if (!nvlist_empty(innvl)) { 6273 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL, 6274 innvl); 6275 } 6276 } 6277 6278 outnvl = fnvlist_alloc(); 6279 error = vec->zvec_func(zc->zc_name, innvl, outnvl); 6280 6281 if (error == 0 && vec->zvec_allow_log && 6282 spa_open(zc->zc_name, &spa, FTAG) == 0) { 6283 if (!nvlist_empty(outnvl)) { 6284 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL, 6285 outnvl); 6286 } 6287 (void) spa_history_log_nvl(spa, lognv); 6288 spa_close(spa, FTAG); 6289 } 6290 fnvlist_free(lognv); 6291 6292 /* rewrite outnvl for backwards compatibility */ 6293 if (compat) 6294 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum, 6295 cflag); 6296 6297 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) { 6298 int smusherror = 0; 6299 if (vec->zvec_smush_outnvlist) { 6300 smusherror = nvlist_smush(outnvl, 6301 zc->zc_nvlist_dst_size); 6302 } 6303 if (smusherror == 0) 6304 puterror = put_nvlist(zc, outnvl); 6305 } 6306 6307 if (puterror != 0) 6308 error = puterror; 6309 6310 nvlist_free(outnvl); 6311 } else { 6312 error = vec->zvec_legacy_func(zc); 6313 } 6314 6315out: 6316 nvlist_free(innvl); 6317 6318#ifdef illumos 6319 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 6320 if (error == 0 && rc != 0) 6321 error = SET_ERROR(EFAULT); 6322#else 6323 if (compat) { 6324 zfs_ioctl_compat_post(zc, cmd, cflag); 6325 if (newioc) { 6326 ASSERT(compat_zc != NULL); 6327 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size); 6328 6329 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag); 6330 rc = ddi_copyout(compat_zc, 6331 (void *)(uintptr_t)zc_iocparm->zfs_cmd, 6332 zc_iocparm->zfs_cmd_size, flag); 6333 if (error == 0 && rc != 0) 6334 error = SET_ERROR(EFAULT); 6335 kmem_free(compat_zc, sizeof (zfs_cmd_t)); 6336 } else { 6337 zfs_cmd_compat_put(zc, arg, vecnum, cflag); 6338 } 6339 } else { 6340 ASSERT(newioc); 6341 6342 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd, 6343 sizeof (zfs_cmd_t), flag); 6344 if (error == 0 && rc != 0) 6345 error = SET_ERROR(EFAULT); 6346 } 6347#endif 6348 if (error == 0 && vec->zvec_allow_log) { 6349 char *s = tsd_get(zfs_allow_log_key); 6350 if (s != NULL) 6351 strfree(s); 6352 (void) tsd_set(zfs_allow_log_key, saved_poolname); 6353 } else { 6354 if (saved_poolname != NULL) 6355 strfree(saved_poolname); 6356 } 6357 6358 kmem_free(zc, sizeof (zfs_cmd_t)); 6359 return (error); 6360} 6361 6362#ifdef illumos 6363static int 6364zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 6365{ 6366 if (cmd != DDI_ATTACH) 6367 return (DDI_FAILURE); 6368 6369 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 6370 DDI_PSEUDO, 0) == DDI_FAILURE) 6371 return (DDI_FAILURE); 6372 6373 zfs_dip = dip; 6374 6375 ddi_report_dev(dip); 6376 6377 return (DDI_SUCCESS); 6378} 6379 6380static int 6381zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 6382{ 6383 if (spa_busy() || zfs_busy() || zvol_busy()) 6384 return (DDI_FAILURE); 6385 6386 if (cmd != DDI_DETACH) 6387 return (DDI_FAILURE); 6388 6389 zfs_dip = NULL; 6390 6391 ddi_prop_remove_all(dip); 6392 ddi_remove_minor_node(dip, NULL); 6393 6394 return (DDI_SUCCESS); 6395} 6396 6397/*ARGSUSED*/ 6398static int 6399zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 6400{ 6401 switch (infocmd) { 6402 case DDI_INFO_DEVT2DEVINFO: 6403 *result = zfs_dip; 6404 return (DDI_SUCCESS); 6405 6406 case DDI_INFO_DEVT2INSTANCE: 6407 *result = (void *)0; 6408 return (DDI_SUCCESS); 6409 } 6410 6411 return (DDI_FAILURE); 6412} 6413#endif /* illumos */ 6414 6415/* 6416 * OK, so this is a little weird. 6417 * 6418 * /dev/zfs is the control node, i.e. minor 0. 6419 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 6420 * 6421 * /dev/zfs has basically nothing to do except serve up ioctls, 6422 * so most of the standard driver entry points are in zvol.c. 6423 */ 6424#ifdef illumos 6425static struct cb_ops zfs_cb_ops = { 6426 zfsdev_open, /* open */ 6427 zfsdev_close, /* close */ 6428 zvol_strategy, /* strategy */ 6429 nodev, /* print */ 6430 zvol_dump, /* dump */ 6431 zvol_read, /* read */ 6432 zvol_write, /* write */ 6433 zfsdev_ioctl, /* ioctl */ 6434 nodev, /* devmap */ 6435 nodev, /* mmap */ 6436 nodev, /* segmap */ 6437 nochpoll, /* poll */ 6438 ddi_prop_op, /* prop_op */ 6439 NULL, /* streamtab */ 6440 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 6441 CB_REV, /* version */ 6442 nodev, /* async read */ 6443 nodev, /* async write */ 6444}; 6445 6446static struct dev_ops zfs_dev_ops = { 6447 DEVO_REV, /* version */ 6448 0, /* refcnt */ 6449 zfs_info, /* info */ 6450 nulldev, /* identify */ 6451 nulldev, /* probe */ 6452 zfs_attach, /* attach */ 6453 zfs_detach, /* detach */ 6454 nodev, /* reset */ 6455 &zfs_cb_ops, /* driver operations */ 6456 NULL, /* no bus operations */ 6457 NULL, /* power */ 6458 ddi_quiesce_not_needed, /* quiesce */ 6459}; 6460 6461static struct modldrv zfs_modldrv = { 6462 &mod_driverops, 6463 "ZFS storage pool", 6464 &zfs_dev_ops 6465}; 6466 6467static struct modlinkage modlinkage = { 6468 MODREV_1, 6469 (void *)&zfs_modlfs, 6470 (void *)&zfs_modldrv, 6471 NULL 6472}; 6473#endif /* illumos */ 6474 6475static struct cdevsw zfs_cdevsw = { 6476 .d_version = D_VERSION, 6477 .d_open = zfsdev_open, 6478 .d_ioctl = zfsdev_ioctl, 6479 .d_name = ZFS_DEV_NAME 6480}; 6481 6482static void 6483zfs_allow_log_destroy(void *arg) 6484{ 6485 char *poolname = arg; 6486 strfree(poolname); 6487} 6488 6489static void 6490zfsdev_init(void) 6491{ 6492 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666, 6493 ZFS_DEV_NAME); 6494} 6495 6496static void 6497zfsdev_fini(void) 6498{ 6499 if (zfsdev != NULL) 6500 destroy_dev(zfsdev); 6501} 6502 6503static struct root_hold_token *zfs_root_token; 6504struct proc *zfsproc; 6505 6506#ifdef illumos 6507int 6508_init(void) 6509{ 6510 int error; 6511 6512 spa_init(FREAD | FWRITE); 6513 zfs_init(); 6514 zvol_init(); 6515 zfs_ioctl_init(); 6516 6517 if ((error = mod_install(&modlinkage)) != 0) { 6518 zvol_fini(); 6519 zfs_fini(); 6520 spa_fini(); 6521 return (error); 6522 } 6523 6524 tsd_create(&zfs_fsyncer_key, NULL); 6525 tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 6526 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy); 6527 6528 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 6529 ASSERT(error == 0); 6530 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 6531 6532 return (0); 6533} 6534 6535int 6536_fini(void) 6537{ 6538 int error; 6539 6540 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 6541 return (SET_ERROR(EBUSY)); 6542 6543 if ((error = mod_remove(&modlinkage)) != 0) 6544 return (error); 6545 6546 zvol_fini(); 6547 zfs_fini(); 6548 spa_fini(); 6549 if (zfs_nfsshare_inited) 6550 (void) ddi_modclose(nfs_mod); 6551 if (zfs_smbshare_inited) 6552 (void) ddi_modclose(smbsrv_mod); 6553 if (zfs_nfsshare_inited || zfs_smbshare_inited) 6554 (void) ddi_modclose(sharefs_mod); 6555 6556 tsd_destroy(&zfs_fsyncer_key); 6557 ldi_ident_release(zfs_li); 6558 zfs_li = NULL; 6559 mutex_destroy(&zfs_share_lock); 6560 6561 return (error); 6562} 6563 6564int 6565_info(struct modinfo *modinfop) 6566{ 6567 return (mod_info(&modlinkage, modinfop)); 6568} 6569#endif /* illumos */ 6570 6571static int zfs__init(void); 6572static int zfs__fini(void); 6573static void zfs_shutdown(void *, int); 6574 6575static eventhandler_tag zfs_shutdown_event_tag; 6576 6577#ifdef __FreeBSD__ 6578#define ZFS_MIN_KSTACK_PAGES 4 6579#endif 6580 6581int 6582zfs__init(void) 6583{ 6584 6585#ifdef __FreeBSD__ 6586#if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES 6587 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack " 6588 "overflow panic!\nPlease consider adding " 6589 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES, 6590 ZFS_MIN_KSTACK_PAGES); 6591#endif 6592#endif 6593 zfs_root_token = root_mount_hold("ZFS"); 6594 6595 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 6596 6597 spa_init(FREAD | FWRITE); 6598 zfs_init(); 6599 zvol_init(); 6600 zfs_ioctl_init(); 6601 6602 tsd_create(&zfs_fsyncer_key, NULL); 6603 tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 6604 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
| 210 211typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *); 212typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *); 213typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *); 214 215typedef enum { 216 NO_NAME, 217 POOL_NAME, 218 DATASET_NAME 219} zfs_ioc_namecheck_t; 220 221typedef enum { 222 POOL_CHECK_NONE = 1 << 0, 223 POOL_CHECK_SUSPENDED = 1 << 1, 224 POOL_CHECK_READONLY = 1 << 2, 225} zfs_ioc_poolcheck_t; 226 227typedef struct zfs_ioc_vec { 228 zfs_ioc_legacy_func_t *zvec_legacy_func; 229 zfs_ioc_func_t *zvec_func; 230 zfs_secpolicy_func_t *zvec_secpolicy; 231 zfs_ioc_namecheck_t zvec_namecheck; 232 boolean_t zvec_allow_log; 233 zfs_ioc_poolcheck_t zvec_pool_check; 234 boolean_t zvec_smush_outnvlist; 235 const char *zvec_name; 236} zfs_ioc_vec_t; 237 238/* This array is indexed by zfs_userquota_prop_t */ 239static const char *userquota_perms[] = { 240 ZFS_DELEG_PERM_USERUSED, 241 ZFS_DELEG_PERM_USERQUOTA, 242 ZFS_DELEG_PERM_GROUPUSED, 243 ZFS_DELEG_PERM_GROUPQUOTA, 244}; 245 246static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 247static int zfs_check_settable(const char *name, nvpair_t *property, 248 cred_t *cr); 249static int zfs_check_clearable(char *dataset, nvlist_t *props, 250 nvlist_t **errors); 251static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 252 boolean_t *); 253int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *); 254static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp); 255 256static void zfsdev_close(void *data); 257 258static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature); 259 260/* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 261void 262__dprintf(const char *file, const char *func, int line, const char *fmt, ...) 263{ 264 const char *newfile; 265 char buf[512]; 266 va_list adx; 267 268 /* 269 * Get rid of annoying "../common/" prefix to filename. 270 */ 271 newfile = strrchr(file, '/'); 272 if (newfile != NULL) { 273 newfile = newfile + 1; /* Get rid of leading / */ 274 } else { 275 newfile = file; 276 } 277 278 va_start(adx, fmt); 279 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 280 va_end(adx); 281 282 /* 283 * To get this data, use the zfs-dprintf probe as so: 284 * dtrace -q -n 'zfs-dprintf \ 285 * /stringof(arg0) == "dbuf.c"/ \ 286 * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 287 * arg0 = file name 288 * arg1 = function name 289 * arg2 = line number 290 * arg3 = message 291 */ 292 DTRACE_PROBE4(zfs__dprintf, 293 char *, newfile, char *, func, int, line, char *, buf); 294} 295 296static void 297history_str_free(char *buf) 298{ 299 kmem_free(buf, HIS_MAX_RECORD_LEN); 300} 301 302static char * 303history_str_get(zfs_cmd_t *zc) 304{ 305 char *buf; 306 307 if (zc->zc_history == 0) 308 return (NULL); 309 310 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 311 if (copyinstr((void *)(uintptr_t)zc->zc_history, 312 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 313 history_str_free(buf); 314 return (NULL); 315 } 316 317 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 318 319 return (buf); 320} 321 322/* 323 * Check to see if the named dataset is currently defined as bootable 324 */ 325static boolean_t 326zfs_is_bootfs(const char *name) 327{ 328 objset_t *os; 329 330 if (dmu_objset_hold(name, FTAG, &os) == 0) { 331 boolean_t ret; 332 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 333 dmu_objset_rele(os, FTAG); 334 return (ret); 335 } 336 return (B_FALSE); 337} 338 339/* 340 * Return non-zero if the spa version is less than requested version. 341 */ 342static int 343zfs_earlier_version(const char *name, int version) 344{ 345 spa_t *spa; 346 347 if (spa_open(name, &spa, FTAG) == 0) { 348 if (spa_version(spa) < version) { 349 spa_close(spa, FTAG); 350 return (1); 351 } 352 spa_close(spa, FTAG); 353 } 354 return (0); 355} 356 357/* 358 * Return TRUE if the ZPL version is less than requested version. 359 */ 360static boolean_t 361zpl_earlier_version(const char *name, int version) 362{ 363 objset_t *os; 364 boolean_t rc = B_TRUE; 365 366 if (dmu_objset_hold(name, FTAG, &os) == 0) { 367 uint64_t zplversion; 368 369 if (dmu_objset_type(os) != DMU_OST_ZFS) { 370 dmu_objset_rele(os, FTAG); 371 return (B_TRUE); 372 } 373 /* XXX reading from non-owned objset */ 374 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 375 rc = zplversion < version; 376 dmu_objset_rele(os, FTAG); 377 } 378 return (rc); 379} 380 381static void 382zfs_log_history(zfs_cmd_t *zc) 383{ 384 spa_t *spa; 385 char *buf; 386 387 if ((buf = history_str_get(zc)) == NULL) 388 return; 389 390 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 391 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 392 (void) spa_history_log(spa, buf); 393 spa_close(spa, FTAG); 394 } 395 history_str_free(buf); 396} 397 398/* 399 * Policy for top-level read operations (list pools). Requires no privileges, 400 * and can be used in the local zone, as there is no associated dataset. 401 */ 402/* ARGSUSED */ 403static int 404zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 405{ 406 return (0); 407} 408 409/* 410 * Policy for dataset read operations (list children, get statistics). Requires 411 * no privileges, but must be visible in the local zone. 412 */ 413/* ARGSUSED */ 414static int 415zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 416{ 417 if (INGLOBALZONE(curthread) || 418 zone_dataset_visible(zc->zc_name, NULL)) 419 return (0); 420 421 return (SET_ERROR(ENOENT)); 422} 423 424static int 425zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) 426{ 427 int writable = 1; 428 429 /* 430 * The dataset must be visible by this zone -- check this first 431 * so they don't see EPERM on something they shouldn't know about. 432 */ 433 if (!INGLOBALZONE(curthread) && 434 !zone_dataset_visible(dataset, &writable)) 435 return (SET_ERROR(ENOENT)); 436 437 if (INGLOBALZONE(curthread)) { 438 /* 439 * If the fs is zoned, only root can access it from the 440 * global zone. 441 */ 442 if (secpolicy_zfs(cr) && zoned) 443 return (SET_ERROR(EPERM)); 444 } else { 445 /* 446 * If we are in a local zone, the 'zoned' property must be set. 447 */ 448 if (!zoned) 449 return (SET_ERROR(EPERM)); 450 451 /* must be writable by this zone */ 452 if (!writable) 453 return (SET_ERROR(EPERM)); 454 } 455 return (0); 456} 457 458static int 459zfs_dozonecheck(const char *dataset, cred_t *cr) 460{ 461 uint64_t zoned; 462 463 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL)) 464 return (SET_ERROR(ENOENT)); 465 466 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 467} 468 469static int 470zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) 471{ 472 uint64_t zoned; 473 474 if (dsl_prop_get_int_ds(ds, "jailed", &zoned)) 475 return (SET_ERROR(ENOENT)); 476 477 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 478} 479 480static int 481zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, 482 const char *perm, cred_t *cr) 483{ 484 int error; 485 486 error = zfs_dozonecheck_ds(name, ds, cr); 487 if (error == 0) { 488 error = secpolicy_zfs(cr); 489 if (error != 0) 490 error = dsl_deleg_access_impl(ds, perm, cr); 491 } 492 return (error); 493} 494 495static int 496zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 497{ 498 int error; 499 dsl_dataset_t *ds; 500 dsl_pool_t *dp; 501 502 error = dsl_pool_hold(name, FTAG, &dp); 503 if (error != 0) 504 return (error); 505 506 error = dsl_dataset_hold(dp, name, FTAG, &ds); 507 if (error != 0) { 508 dsl_pool_rele(dp, FTAG); 509 return (error); 510 } 511 512 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr); 513 514 dsl_dataset_rele(ds, FTAG); 515 dsl_pool_rele(dp, FTAG); 516 return (error); 517} 518 519#ifdef SECLABEL 520/* 521 * Policy for setting the security label property. 522 * 523 * Returns 0 for success, non-zero for access and other errors. 524 */ 525static int 526zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 527{ 528 char ds_hexsl[MAXNAMELEN]; 529 bslabel_t ds_sl, new_sl; 530 boolean_t new_default = FALSE; 531 uint64_t zoned; 532 int needed_priv = -1; 533 int error; 534 535 /* First get the existing dataset label. */ 536 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 537 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 538 if (error != 0) 539 return (SET_ERROR(EPERM)); 540 541 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 542 new_default = TRUE; 543 544 /* The label must be translatable */ 545 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 546 return (SET_ERROR(EINVAL)); 547 548 /* 549 * In a non-global zone, disallow attempts to set a label that 550 * doesn't match that of the zone; otherwise no other checks 551 * are needed. 552 */ 553 if (!INGLOBALZONE(curproc)) { 554 if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 555 return (SET_ERROR(EPERM)); 556 return (0); 557 } 558 559 /* 560 * For global-zone datasets (i.e., those whose zoned property is 561 * "off", verify that the specified new label is valid for the 562 * global zone. 563 */ 564 if (dsl_prop_get_integer(name, 565 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 566 return (SET_ERROR(EPERM)); 567 if (!zoned) { 568 if (zfs_check_global_label(name, strval) != 0) 569 return (SET_ERROR(EPERM)); 570 } 571 572 /* 573 * If the existing dataset label is nondefault, check if the 574 * dataset is mounted (label cannot be changed while mounted). 575 * Get the zfsvfs; if there isn't one, then the dataset isn't 576 * mounted (or isn't a dataset, doesn't exist, ...). 577 */ 578 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 579 objset_t *os; 580 static char *setsl_tag = "setsl_tag"; 581 582 /* 583 * Try to own the dataset; abort if there is any error, 584 * (e.g., already mounted, in use, or other error). 585 */ 586 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 587 setsl_tag, &os); 588 if (error != 0) 589 return (SET_ERROR(EPERM)); 590 591 dmu_objset_disown(os, setsl_tag); 592 593 if (new_default) { 594 needed_priv = PRIV_FILE_DOWNGRADE_SL; 595 goto out_check; 596 } 597 598 if (hexstr_to_label(strval, &new_sl) != 0) 599 return (SET_ERROR(EPERM)); 600 601 if (blstrictdom(&ds_sl, &new_sl)) 602 needed_priv = PRIV_FILE_DOWNGRADE_SL; 603 else if (blstrictdom(&new_sl, &ds_sl)) 604 needed_priv = PRIV_FILE_UPGRADE_SL; 605 } else { 606 /* dataset currently has a default label */ 607 if (!new_default) 608 needed_priv = PRIV_FILE_UPGRADE_SL; 609 } 610 611out_check: 612 if (needed_priv != -1) 613 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 614 return (0); 615} 616#endif /* SECLABEL */ 617 618static int 619zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 620 cred_t *cr) 621{ 622 char *strval; 623 624 /* 625 * Check permissions for special properties. 626 */ 627 switch (prop) { 628 case ZFS_PROP_ZONED: 629 /* 630 * Disallow setting of 'zoned' from within a local zone. 631 */ 632 if (!INGLOBALZONE(curthread)) 633 return (SET_ERROR(EPERM)); 634 break; 635 636 case ZFS_PROP_QUOTA: 637 case ZFS_PROP_FILESYSTEM_LIMIT: 638 case ZFS_PROP_SNAPSHOT_LIMIT: 639 if (!INGLOBALZONE(curthread)) { 640 uint64_t zoned; 641 char setpoint[MAXNAMELEN]; 642 /* 643 * Unprivileged users are allowed to modify the 644 * limit on things *under* (ie. contained by) 645 * the thing they own. 646 */ 647 if (dsl_prop_get_integer(dsname, "jailed", &zoned, 648 setpoint)) 649 return (SET_ERROR(EPERM)); 650 if (!zoned || strlen(dsname) <= strlen(setpoint)) 651 return (SET_ERROR(EPERM)); 652 } 653 break; 654 655 case ZFS_PROP_MLSLABEL: 656#ifdef SECLABEL 657 if (!is_system_labeled()) 658 return (SET_ERROR(EPERM)); 659 660 if (nvpair_value_string(propval, &strval) == 0) { 661 int err; 662 663 err = zfs_set_slabel_policy(dsname, strval, CRED()); 664 if (err != 0) 665 return (err); 666 } 667#else 668 return (EOPNOTSUPP); 669#endif 670 break; 671 } 672 673 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 674} 675 676/* ARGSUSED */ 677static int 678zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 679{ 680 int error; 681 682 error = zfs_dozonecheck(zc->zc_name, cr); 683 if (error != 0) 684 return (error); 685 686 /* 687 * permission to set permissions will be evaluated later in 688 * dsl_deleg_can_allow() 689 */ 690 return (0); 691} 692 693/* ARGSUSED */ 694static int 695zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 696{ 697 return (zfs_secpolicy_write_perms(zc->zc_name, 698 ZFS_DELEG_PERM_ROLLBACK, cr)); 699} 700 701/* ARGSUSED */ 702static int 703zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 704{ 705 dsl_pool_t *dp; 706 dsl_dataset_t *ds; 707 char *cp; 708 int error; 709 710 /* 711 * Generate the current snapshot name from the given objsetid, then 712 * use that name for the secpolicy/zone checks. 713 */ 714 cp = strchr(zc->zc_name, '@'); 715 if (cp == NULL) 716 return (SET_ERROR(EINVAL)); 717 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 718 if (error != 0) 719 return (error); 720 721 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 722 if (error != 0) { 723 dsl_pool_rele(dp, FTAG); 724 return (error); 725 } 726 727 dsl_dataset_name(ds, zc->zc_name); 728 729 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, 730 ZFS_DELEG_PERM_SEND, cr); 731 dsl_dataset_rele(ds, FTAG); 732 dsl_pool_rele(dp, FTAG); 733 734 return (error); 735} 736 737/* ARGSUSED */ 738static int 739zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 740{ 741 return (zfs_secpolicy_write_perms(zc->zc_name, 742 ZFS_DELEG_PERM_SEND, cr)); 743} 744 745/* ARGSUSED */ 746static int 747zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 748{ 749 vnode_t *vp; 750 int error; 751 752 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 753 NO_FOLLOW, NULL, &vp)) != 0) 754 return (error); 755 756 /* Now make sure mntpnt and dataset are ZFS */ 757 758 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 || 759 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 760 zc->zc_name) != 0)) { 761 VN_RELE(vp); 762 return (SET_ERROR(EPERM)); 763 } 764 765 VN_RELE(vp); 766 return (dsl_deleg_access(zc->zc_name, 767 ZFS_DELEG_PERM_SHARE, cr)); 768} 769 770int 771zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 772{ 773 if (!INGLOBALZONE(curthread)) 774 return (SET_ERROR(EPERM)); 775 776 if (secpolicy_nfs(cr) == 0) { 777 return (0); 778 } else { 779 return (zfs_secpolicy_deleg_share(zc, innvl, cr)); 780 } 781} 782 783int 784zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 785{ 786 if (!INGLOBALZONE(curthread)) 787 return (SET_ERROR(EPERM)); 788 789 if (secpolicy_smb(cr) == 0) { 790 return (0); 791 } else { 792 return (zfs_secpolicy_deleg_share(zc, innvl, cr)); 793 } 794} 795 796static int 797zfs_get_parent(const char *datasetname, char *parent, int parentsize) 798{ 799 char *cp; 800 801 /* 802 * Remove the @bla or /bla from the end of the name to get the parent. 803 */ 804 (void) strncpy(parent, datasetname, parentsize); 805 cp = strrchr(parent, '@'); 806 if (cp != NULL) { 807 cp[0] = '\0'; 808 } else { 809 cp = strrchr(parent, '/'); 810 if (cp == NULL) 811 return (SET_ERROR(ENOENT)); 812 cp[0] = '\0'; 813 } 814 815 return (0); 816} 817 818int 819zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 820{ 821 int error; 822 823 if ((error = zfs_secpolicy_write_perms(name, 824 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 825 return (error); 826 827 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 828} 829 830/* ARGSUSED */ 831static int 832zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 833{ 834 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 835} 836 837/* 838 * Destroying snapshots with delegated permissions requires 839 * descendant mount and destroy permissions. 840 */ 841/* ARGSUSED */ 842static int 843zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 844{ 845 nvlist_t *snaps; 846 nvpair_t *pair, *nextpair; 847 int error = 0; 848 849 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 850 return (SET_ERROR(EINVAL)); 851 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 852 pair = nextpair) { 853 nextpair = nvlist_next_nvpair(snaps, pair); 854 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr); 855 if (error == ENOENT) { 856 /* 857 * Ignore any snapshots that don't exist (we consider 858 * them "already destroyed"). Remove the name from the 859 * nvl here in case the snapshot is created between 860 * now and when we try to destroy it (in which case 861 * we don't want to destroy it since we haven't 862 * checked for permission). 863 */ 864 fnvlist_remove_nvpair(snaps, pair); 865 error = 0; 866 } 867 if (error != 0) 868 break; 869 } 870 871 return (error); 872} 873 874int 875zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 876{ 877 char parentname[MAXNAMELEN]; 878 int error; 879 880 if ((error = zfs_secpolicy_write_perms(from, 881 ZFS_DELEG_PERM_RENAME, cr)) != 0) 882 return (error); 883 884 if ((error = zfs_secpolicy_write_perms(from, 885 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 886 return (error); 887 888 if ((error = zfs_get_parent(to, parentname, 889 sizeof (parentname))) != 0) 890 return (error); 891 892 if ((error = zfs_secpolicy_write_perms(parentname, 893 ZFS_DELEG_PERM_CREATE, cr)) != 0) 894 return (error); 895 896 if ((error = zfs_secpolicy_write_perms(parentname, 897 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 898 return (error); 899 900 return (error); 901} 902 903/* ARGSUSED */ 904static int 905zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 906{ 907 char *at = NULL; 908 int error; 909 910 if ((zc->zc_cookie & 1) != 0) { 911 /* 912 * This is recursive rename, so the starting snapshot might 913 * not exist. Check file system or volume permission instead. 914 */ 915 at = strchr(zc->zc_name, '@'); 916 if (at == NULL) 917 return (EINVAL); 918 *at = '\0'; 919 } 920 921 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr); 922 923 if (at != NULL) 924 *at = '@'; 925 926 return (error); 927} 928 929/* ARGSUSED */ 930static int 931zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 932{ 933 dsl_pool_t *dp; 934 dsl_dataset_t *clone; 935 int error; 936 937 error = zfs_secpolicy_write_perms(zc->zc_name, 938 ZFS_DELEG_PERM_PROMOTE, cr); 939 if (error != 0) 940 return (error); 941 942 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 943 if (error != 0) 944 return (error); 945 946 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone); 947 948 if (error == 0) { 949 char parentname[MAXNAMELEN]; 950 dsl_dataset_t *origin = NULL; 951 dsl_dir_t *dd; 952 dd = clone->ds_dir; 953 954 error = dsl_dataset_hold_obj(dd->dd_pool, 955 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin); 956 if (error != 0) { 957 dsl_dataset_rele(clone, FTAG); 958 dsl_pool_rele(dp, FTAG); 959 return (error); 960 } 961 962 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone, 963 ZFS_DELEG_PERM_MOUNT, cr); 964 965 dsl_dataset_name(origin, parentname); 966 if (error == 0) { 967 error = zfs_secpolicy_write_perms_ds(parentname, origin, 968 ZFS_DELEG_PERM_PROMOTE, cr); 969 } 970 dsl_dataset_rele(clone, FTAG); 971 dsl_dataset_rele(origin, FTAG); 972 } 973 dsl_pool_rele(dp, FTAG); 974 return (error); 975} 976 977/* ARGSUSED */ 978static int 979zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 980{ 981 int error; 982 983 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 984 ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 985 return (error); 986 987 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 988 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 989 return (error); 990 991 return (zfs_secpolicy_write_perms(zc->zc_name, 992 ZFS_DELEG_PERM_CREATE, cr)); 993} 994 995int 996zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 997{ 998 return (zfs_secpolicy_write_perms(name, 999 ZFS_DELEG_PERM_SNAPSHOT, cr)); 1000} 1001 1002/* 1003 * Check for permission to create each snapshot in the nvlist. 1004 */ 1005/* ARGSUSED */ 1006static int 1007zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1008{ 1009 nvlist_t *snaps; 1010 int error; 1011 nvpair_t *pair; 1012 1013 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 1014 return (SET_ERROR(EINVAL)); 1015 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 1016 pair = nvlist_next_nvpair(snaps, pair)) { 1017 char *name = nvpair_name(pair); 1018 char *atp = strchr(name, '@'); 1019 1020 if (atp == NULL) { 1021 error = SET_ERROR(EINVAL); 1022 break; 1023 } 1024 *atp = '\0'; 1025 error = zfs_secpolicy_snapshot_perms(name, cr); 1026 *atp = '@'; 1027 if (error != 0) 1028 break; 1029 } 1030 return (error); 1031} 1032 1033/* 1034 * Check for permission to create each snapshot in the nvlist. 1035 */ 1036/* ARGSUSED */ 1037static int 1038zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1039{ 1040 int error = 0; 1041 1042 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 1043 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 1044 char *name = nvpair_name(pair); 1045 char *hashp = strchr(name, '#'); 1046 1047 if (hashp == NULL) { 1048 error = SET_ERROR(EINVAL); 1049 break; 1050 } 1051 *hashp = '\0'; 1052 error = zfs_secpolicy_write_perms(name, 1053 ZFS_DELEG_PERM_BOOKMARK, cr); 1054 *hashp = '#'; 1055 if (error != 0) 1056 break; 1057 } 1058 return (error); 1059} 1060 1061/* ARGSUSED */ 1062static int 1063zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1064{ 1065 nvpair_t *pair, *nextpair; 1066 int error = 0; 1067 1068 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; 1069 pair = nextpair) { 1070 char *name = nvpair_name(pair); 1071 char *hashp = strchr(name, '#'); 1072 nextpair = nvlist_next_nvpair(innvl, pair); 1073 1074 if (hashp == NULL) { 1075 error = SET_ERROR(EINVAL); 1076 break; 1077 } 1078 1079 *hashp = '\0'; 1080 error = zfs_secpolicy_write_perms(name, 1081 ZFS_DELEG_PERM_DESTROY, cr); 1082 *hashp = '#'; 1083 if (error == ENOENT) { 1084 /* 1085 * Ignore any filesystems that don't exist (we consider 1086 * their bookmarks "already destroyed"). Remove 1087 * the name from the nvl here in case the filesystem 1088 * is created between now and when we try to destroy 1089 * the bookmark (in which case we don't want to 1090 * destroy it since we haven't checked for permission). 1091 */ 1092 fnvlist_remove_nvpair(innvl, pair); 1093 error = 0; 1094 } 1095 if (error != 0) 1096 break; 1097 } 1098 1099 return (error); 1100} 1101 1102/* ARGSUSED */ 1103static int 1104zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1105{ 1106 /* 1107 * Even root must have a proper TSD so that we know what pool 1108 * to log to. 1109 */ 1110 if (tsd_get(zfs_allow_log_key) == NULL) 1111 return (SET_ERROR(EPERM)); 1112 return (0); 1113} 1114 1115static int 1116zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1117{ 1118 char parentname[MAXNAMELEN]; 1119 int error; 1120 char *origin; 1121 1122 if ((error = zfs_get_parent(zc->zc_name, parentname, 1123 sizeof (parentname))) != 0) 1124 return (error); 1125 1126 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 && 1127 (error = zfs_secpolicy_write_perms(origin, 1128 ZFS_DELEG_PERM_CLONE, cr)) != 0) 1129 return (error); 1130 1131 if ((error = zfs_secpolicy_write_perms(parentname, 1132 ZFS_DELEG_PERM_CREATE, cr)) != 0) 1133 return (error); 1134 1135 return (zfs_secpolicy_write_perms(parentname, 1136 ZFS_DELEG_PERM_MOUNT, cr)); 1137} 1138 1139/* 1140 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 1141 * SYS_CONFIG privilege, which is not available in a local zone. 1142 */ 1143/* ARGSUSED */ 1144static int 1145zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1146{ 1147 if (secpolicy_sys_config(cr, B_FALSE) != 0) 1148 return (SET_ERROR(EPERM)); 1149 1150 return (0); 1151} 1152 1153/* 1154 * Policy for object to name lookups. 1155 */ 1156/* ARGSUSED */ 1157static int 1158zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1159{ 1160 int error; 1161 1162 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0) 1163 return (0); 1164 1165 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); 1166 return (error); 1167} 1168 1169/* 1170 * Policy for fault injection. Requires all privileges. 1171 */ 1172/* ARGSUSED */ 1173static int 1174zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1175{ 1176 return (secpolicy_zinject(cr)); 1177} 1178 1179/* ARGSUSED */ 1180static int 1181zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1182{ 1183 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 1184 1185 if (prop == ZPROP_INVAL) { 1186 if (!zfs_prop_user(zc->zc_value)) 1187 return (SET_ERROR(EINVAL)); 1188 return (zfs_secpolicy_write_perms(zc->zc_name, 1189 ZFS_DELEG_PERM_USERPROP, cr)); 1190 } else { 1191 return (zfs_secpolicy_setprop(zc->zc_name, prop, 1192 NULL, cr)); 1193 } 1194} 1195 1196static int 1197zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1198{ 1199 int err = zfs_secpolicy_read(zc, innvl, cr); 1200 if (err) 1201 return (err); 1202 1203 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1204 return (SET_ERROR(EINVAL)); 1205 1206 if (zc->zc_value[0] == 0) { 1207 /* 1208 * They are asking about a posix uid/gid. If it's 1209 * themself, allow it. 1210 */ 1211 if (zc->zc_objset_type == ZFS_PROP_USERUSED || 1212 zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 1213 if (zc->zc_guid == crgetuid(cr)) 1214 return (0); 1215 } else { 1216 if (groupmember(zc->zc_guid, cr)) 1217 return (0); 1218 } 1219 } 1220 1221 return (zfs_secpolicy_write_perms(zc->zc_name, 1222 userquota_perms[zc->zc_objset_type], cr)); 1223} 1224 1225static int 1226zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1227{ 1228 int err = zfs_secpolicy_read(zc, innvl, cr); 1229 if (err) 1230 return (err); 1231 1232 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1233 return (SET_ERROR(EINVAL)); 1234 1235 return (zfs_secpolicy_write_perms(zc->zc_name, 1236 userquota_perms[zc->zc_objset_type], cr)); 1237} 1238 1239/* ARGSUSED */ 1240static int 1241zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1242{ 1243 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 1244 NULL, cr)); 1245} 1246 1247/* ARGSUSED */ 1248static int 1249zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1250{ 1251 nvpair_t *pair; 1252 nvlist_t *holds; 1253 int error; 1254 1255 error = nvlist_lookup_nvlist(innvl, "holds", &holds); 1256 if (error != 0) 1257 return (SET_ERROR(EINVAL)); 1258 1259 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 1260 pair = nvlist_next_nvpair(holds, pair)) { 1261 char fsname[MAXNAMELEN]; 1262 error = dmu_fsname(nvpair_name(pair), fsname); 1263 if (error != 0) 1264 return (error); 1265 error = zfs_secpolicy_write_perms(fsname, 1266 ZFS_DELEG_PERM_HOLD, cr); 1267 if (error != 0) 1268 return (error); 1269 } 1270 return (0); 1271} 1272 1273/* ARGSUSED */ 1274static int 1275zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1276{ 1277 nvpair_t *pair; 1278 int error; 1279 1280 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; 1281 pair = nvlist_next_nvpair(innvl, pair)) { 1282 char fsname[MAXNAMELEN]; 1283 error = dmu_fsname(nvpair_name(pair), fsname); 1284 if (error != 0) 1285 return (error); 1286 error = zfs_secpolicy_write_perms(fsname, 1287 ZFS_DELEG_PERM_RELEASE, cr); 1288 if (error != 0) 1289 return (error); 1290 } 1291 return (0); 1292} 1293 1294/* 1295 * Policy for allowing temporary snapshots to be taken or released 1296 */ 1297static int 1298zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1299{ 1300 /* 1301 * A temporary snapshot is the same as a snapshot, 1302 * hold, destroy and release all rolled into one. 1303 * Delegated diff alone is sufficient that we allow this. 1304 */ 1305 int error; 1306 1307 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 1308 ZFS_DELEG_PERM_DIFF, cr)) == 0) 1309 return (0); 1310 1311 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr); 1312 if (error == 0) 1313 error = zfs_secpolicy_hold(zc, innvl, cr); 1314 if (error == 0) 1315 error = zfs_secpolicy_release(zc, innvl, cr); 1316 if (error == 0) 1317 error = zfs_secpolicy_destroy(zc, innvl, cr); 1318 return (error); 1319} 1320 1321/* 1322 * Returns the nvlist as specified by the user in the zfs_cmd_t. 1323 */ 1324static int 1325get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 1326{ 1327 char *packed; 1328 int error; 1329 nvlist_t *list = NULL; 1330 1331 /* 1332 * Read in and unpack the user-supplied nvlist. 1333 */ 1334 if (size == 0) 1335 return (SET_ERROR(EINVAL)); 1336 1337 packed = kmem_alloc(size, KM_SLEEP); 1338 1339 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 1340 iflag)) != 0) { 1341 kmem_free(packed, size); 1342 return (error); 1343 } 1344 1345 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 1346 kmem_free(packed, size); 1347 return (error); 1348 } 1349 1350 kmem_free(packed, size); 1351 1352 *nvp = list; 1353 return (0); 1354} 1355 1356/* 1357 * Reduce the size of this nvlist until it can be serialized in 'max' bytes. 1358 * Entries will be removed from the end of the nvlist, and one int32 entry 1359 * named "N_MORE_ERRORS" will be added indicating how many entries were 1360 * removed. 1361 */ 1362static int 1363nvlist_smush(nvlist_t *errors, size_t max) 1364{ 1365 size_t size; 1366 1367 size = fnvlist_size(errors); 1368 1369 if (size > max) { 1370 nvpair_t *more_errors; 1371 int n = 0; 1372 1373 if (max < 1024) 1374 return (SET_ERROR(ENOMEM)); 1375 1376 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0); 1377 more_errors = nvlist_prev_nvpair(errors, NULL); 1378 1379 do { 1380 nvpair_t *pair = nvlist_prev_nvpair(errors, 1381 more_errors); 1382 fnvlist_remove_nvpair(errors, pair); 1383 n++; 1384 size = fnvlist_size(errors); 1385 } while (size > max); 1386 1387 fnvlist_remove_nvpair(errors, more_errors); 1388 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n); 1389 ASSERT3U(fnvlist_size(errors), <=, max); 1390 } 1391 1392 return (0); 1393} 1394 1395static int 1396put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 1397{ 1398 char *packed = NULL; 1399 int error = 0; 1400 size_t size; 1401 1402 size = fnvlist_size(nvl); 1403 1404 if (size > zc->zc_nvlist_dst_size) { 1405 /* 1406 * Solaris returns ENOMEM here, because even if an error is 1407 * returned from an ioctl(2), new zc_nvlist_dst_size will be 1408 * passed to the userland. This is not the case for FreeBSD. 1409 * We need to return 0, so the kernel will copy the 1410 * zc_nvlist_dst_size back and the userland can discover that a 1411 * bigger buffer is needed. 1412 */ 1413 error = 0; 1414 } else { 1415 packed = fnvlist_pack(nvl, &size); 1416 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 1417 size, zc->zc_iflags) != 0) 1418 error = SET_ERROR(EFAULT); 1419 fnvlist_pack_free(packed, size); 1420 } 1421 1422 zc->zc_nvlist_dst_size = size; 1423 zc->zc_nvlist_dst_filled = B_TRUE; 1424 return (error); 1425} 1426 1427static int 1428getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 1429{ 1430 objset_t *os; 1431 int error; 1432 1433 error = dmu_objset_hold(dsname, FTAG, &os); 1434 if (error != 0) 1435 return (error); 1436 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1437 dmu_objset_rele(os, FTAG); 1438 return (SET_ERROR(EINVAL)); 1439 } 1440 1441 mutex_enter(&os->os_user_ptr_lock); 1442 *zfvp = dmu_objset_get_user(os); 1443 if (*zfvp) { 1444 VFS_HOLD((*zfvp)->z_vfs); 1445 } else { 1446 error = SET_ERROR(ESRCH); 1447 } 1448 mutex_exit(&os->os_user_ptr_lock); 1449 dmu_objset_rele(os, FTAG); 1450 return (error); 1451} 1452 1453/* 1454 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 1455 * case its z_vfs will be NULL, and it will be opened as the owner. 1456 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER, 1457 * which prevents all vnode ops from running. 1458 */ 1459static int 1460zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer) 1461{ 1462 int error = 0; 1463 1464 if (getzfsvfs(name, zfvp) != 0) 1465 error = zfsvfs_create(name, zfvp); 1466 if (error == 0) { 1467 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER : 1468 RW_READER, tag); 1469 if ((*zfvp)->z_unmounted) { 1470 /* 1471 * XXX we could probably try again, since the unmounting 1472 * thread should be just about to disassociate the 1473 * objset from the zfsvfs. 1474 */ 1475 rrm_exit(&(*zfvp)->z_teardown_lock, tag); 1476 return (SET_ERROR(EBUSY)); 1477 } 1478 } 1479 return (error); 1480} 1481 1482static void 1483zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 1484{ 1485 rrm_exit(&zfsvfs->z_teardown_lock, tag); 1486 1487 if (zfsvfs->z_vfs) { 1488 VFS_RELE(zfsvfs->z_vfs); 1489 } else { 1490 dmu_objset_disown(zfsvfs->z_os, zfsvfs); 1491 zfsvfs_free(zfsvfs); 1492 } 1493} 1494 1495static int 1496zfs_ioc_pool_create(zfs_cmd_t *zc) 1497{ 1498 int error; 1499 nvlist_t *config, *props = NULL; 1500 nvlist_t *rootprops = NULL; 1501 nvlist_t *zplprops = NULL; 1502 1503 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1504 zc->zc_iflags, &config)) 1505 return (error); 1506 1507 if (zc->zc_nvlist_src_size != 0 && (error = 1508 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1509 zc->zc_iflags, &props))) { 1510 nvlist_free(config); 1511 return (error); 1512 } 1513 1514 if (props) { 1515 nvlist_t *nvl = NULL; 1516 uint64_t version = SPA_VERSION; 1517 1518 (void) nvlist_lookup_uint64(props, 1519 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 1520 if (!SPA_VERSION_IS_SUPPORTED(version)) { 1521 error = SET_ERROR(EINVAL); 1522 goto pool_props_bad; 1523 } 1524 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 1525 if (nvl) { 1526 error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 1527 if (error != 0) { 1528 nvlist_free(config); 1529 nvlist_free(props); 1530 return (error); 1531 } 1532 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 1533 } 1534 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1535 error = zfs_fill_zplprops_root(version, rootprops, 1536 zplprops, NULL); 1537 if (error != 0) 1538 goto pool_props_bad; 1539 } 1540 1541 error = spa_create(zc->zc_name, config, props, zplprops); 1542 1543 /* 1544 * Set the remaining root properties 1545 */ 1546 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 1547 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 1548 (void) spa_destroy(zc->zc_name); 1549 1550pool_props_bad: 1551 nvlist_free(rootprops); 1552 nvlist_free(zplprops); 1553 nvlist_free(config); 1554 nvlist_free(props); 1555 1556 return (error); 1557} 1558 1559static int 1560zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1561{ 1562 int error; 1563 zfs_log_history(zc); 1564 error = spa_destroy(zc->zc_name); 1565 if (error == 0) 1566 zvol_remove_minors(zc->zc_name); 1567 return (error); 1568} 1569 1570static int 1571zfs_ioc_pool_import(zfs_cmd_t *zc) 1572{ 1573 nvlist_t *config, *props = NULL; 1574 uint64_t guid; 1575 int error; 1576 1577 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1578 zc->zc_iflags, &config)) != 0) 1579 return (error); 1580 1581 if (zc->zc_nvlist_src_size != 0 && (error = 1582 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1583 zc->zc_iflags, &props))) { 1584 nvlist_free(config); 1585 return (error); 1586 } 1587 1588 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1589 guid != zc->zc_guid) 1590 error = SET_ERROR(EINVAL); 1591 else 1592 error = spa_import(zc->zc_name, config, props, zc->zc_cookie); 1593 1594 if (zc->zc_nvlist_dst != 0) { 1595 int err; 1596 1597 if ((err = put_nvlist(zc, config)) != 0) 1598 error = err; 1599 } 1600 1601 nvlist_free(config); 1602 1603 if (props) 1604 nvlist_free(props); 1605 1606 return (error); 1607} 1608 1609static int 1610zfs_ioc_pool_export(zfs_cmd_t *zc) 1611{ 1612 int error; 1613 boolean_t force = (boolean_t)zc->zc_cookie; 1614 boolean_t hardforce = (boolean_t)zc->zc_guid; 1615 1616 zfs_log_history(zc); 1617 error = spa_export(zc->zc_name, NULL, force, hardforce); 1618 if (error == 0) 1619 zvol_remove_minors(zc->zc_name); 1620 return (error); 1621} 1622 1623static int 1624zfs_ioc_pool_configs(zfs_cmd_t *zc) 1625{ 1626 nvlist_t *configs; 1627 int error; 1628 1629 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1630 return (SET_ERROR(EEXIST)); 1631 1632 error = put_nvlist(zc, configs); 1633 1634 nvlist_free(configs); 1635 1636 return (error); 1637} 1638 1639/* 1640 * inputs: 1641 * zc_name name of the pool 1642 * 1643 * outputs: 1644 * zc_cookie real errno 1645 * zc_nvlist_dst config nvlist 1646 * zc_nvlist_dst_size size of config nvlist 1647 */ 1648static int 1649zfs_ioc_pool_stats(zfs_cmd_t *zc) 1650{ 1651 nvlist_t *config; 1652 int error; 1653 int ret = 0; 1654 1655 error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1656 sizeof (zc->zc_value)); 1657 1658 if (config != NULL) { 1659 ret = put_nvlist(zc, config); 1660 nvlist_free(config); 1661 1662 /* 1663 * The config may be present even if 'error' is non-zero. 1664 * In this case we return success, and preserve the real errno 1665 * in 'zc_cookie'. 1666 */ 1667 zc->zc_cookie = error; 1668 } else { 1669 ret = error; 1670 } 1671 1672 return (ret); 1673} 1674 1675/* 1676 * Try to import the given pool, returning pool stats as appropriate so that 1677 * user land knows which devices are available and overall pool health. 1678 */ 1679static int 1680zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1681{ 1682 nvlist_t *tryconfig, *config; 1683 int error; 1684 1685 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1686 zc->zc_iflags, &tryconfig)) != 0) 1687 return (error); 1688 1689 config = spa_tryimport(tryconfig); 1690 1691 nvlist_free(tryconfig); 1692 1693 if (config == NULL) 1694 return (SET_ERROR(EINVAL)); 1695 1696 error = put_nvlist(zc, config); 1697 nvlist_free(config); 1698 1699 return (error); 1700} 1701 1702/* 1703 * inputs: 1704 * zc_name name of the pool 1705 * zc_cookie scan func (pool_scan_func_t) 1706 */ 1707static int 1708zfs_ioc_pool_scan(zfs_cmd_t *zc) 1709{ 1710 spa_t *spa; 1711 int error; 1712 1713 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1714 return (error); 1715 1716 if (zc->zc_cookie == POOL_SCAN_NONE) 1717 error = spa_scan_stop(spa); 1718 else 1719 error = spa_scan(spa, zc->zc_cookie); 1720 1721 spa_close(spa, FTAG); 1722 1723 return (error); 1724} 1725 1726static int 1727zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1728{ 1729 spa_t *spa; 1730 int error; 1731 1732 error = spa_open(zc->zc_name, &spa, FTAG); 1733 if (error == 0) { 1734 spa_freeze(spa); 1735 spa_close(spa, FTAG); 1736 } 1737 return (error); 1738} 1739 1740static int 1741zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1742{ 1743 spa_t *spa; 1744 int error; 1745 1746 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1747 return (error); 1748 1749 if (zc->zc_cookie < spa_version(spa) || 1750 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) { 1751 spa_close(spa, FTAG); 1752 return (SET_ERROR(EINVAL)); 1753 } 1754 1755 spa_upgrade(spa, zc->zc_cookie); 1756 spa_close(spa, FTAG); 1757 1758 return (error); 1759} 1760 1761static int 1762zfs_ioc_pool_get_history(zfs_cmd_t *zc) 1763{ 1764 spa_t *spa; 1765 char *hist_buf; 1766 uint64_t size; 1767 int error; 1768 1769 if ((size = zc->zc_history_len) == 0) 1770 return (SET_ERROR(EINVAL)); 1771 1772 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1773 return (error); 1774 1775 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1776 spa_close(spa, FTAG); 1777 return (SET_ERROR(ENOTSUP)); 1778 } 1779 1780 hist_buf = kmem_alloc(size, KM_SLEEP); 1781 if ((error = spa_history_get(spa, &zc->zc_history_offset, 1782 &zc->zc_history_len, hist_buf)) == 0) { 1783 error = ddi_copyout(hist_buf, 1784 (void *)(uintptr_t)zc->zc_history, 1785 zc->zc_history_len, zc->zc_iflags); 1786 } 1787 1788 spa_close(spa, FTAG); 1789 kmem_free(hist_buf, size); 1790 return (error); 1791} 1792 1793static int 1794zfs_ioc_pool_reguid(zfs_cmd_t *zc) 1795{ 1796 spa_t *spa; 1797 int error; 1798 1799 error = spa_open(zc->zc_name, &spa, FTAG); 1800 if (error == 0) { 1801 error = spa_change_guid(spa); 1802 spa_close(spa, FTAG); 1803 } 1804 return (error); 1805} 1806 1807static int 1808zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 1809{ 1810 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)); 1811} 1812 1813/* 1814 * inputs: 1815 * zc_name name of filesystem 1816 * zc_obj object to find 1817 * 1818 * outputs: 1819 * zc_value name of object 1820 */ 1821static int 1822zfs_ioc_obj_to_path(zfs_cmd_t *zc) 1823{ 1824 objset_t *os; 1825 int error; 1826 1827 /* XXX reading from objset not owned */ 1828 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 1829 return (error); 1830 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1831 dmu_objset_rele(os, FTAG); 1832 return (SET_ERROR(EINVAL)); 1833 } 1834 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 1835 sizeof (zc->zc_value)); 1836 dmu_objset_rele(os, FTAG); 1837 1838 return (error); 1839} 1840 1841/* 1842 * inputs: 1843 * zc_name name of filesystem 1844 * zc_obj object to find 1845 * 1846 * outputs: 1847 * zc_stat stats on object 1848 * zc_value path to object 1849 */ 1850static int 1851zfs_ioc_obj_to_stats(zfs_cmd_t *zc) 1852{ 1853 objset_t *os; 1854 int error; 1855 1856 /* XXX reading from objset not owned */ 1857 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 1858 return (error); 1859 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1860 dmu_objset_rele(os, FTAG); 1861 return (SET_ERROR(EINVAL)); 1862 } 1863 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, 1864 sizeof (zc->zc_value)); 1865 dmu_objset_rele(os, FTAG); 1866 1867 return (error); 1868} 1869 1870static int 1871zfs_ioc_vdev_add(zfs_cmd_t *zc) 1872{ 1873 spa_t *spa; 1874 int error; 1875 nvlist_t *config, **l2cache, **spares; 1876 uint_t nl2cache = 0, nspares = 0; 1877 1878 error = spa_open(zc->zc_name, &spa, FTAG); 1879 if (error != 0) 1880 return (error); 1881 1882 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1883 zc->zc_iflags, &config); 1884 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 1885 &l2cache, &nl2cache); 1886 1887 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 1888 &spares, &nspares); 1889 1890#ifdef illumos 1891 /* 1892 * A root pool with concatenated devices is not supported. 1893 * Thus, can not add a device to a root pool. 1894 * 1895 * Intent log device can not be added to a rootpool because 1896 * during mountroot, zil is replayed, a seperated log device 1897 * can not be accessed during the mountroot time. 1898 * 1899 * l2cache and spare devices are ok to be added to a rootpool. 1900 */ 1901 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 1902 nvlist_free(config); 1903 spa_close(spa, FTAG); 1904 return (SET_ERROR(EDOM)); 1905 } 1906#endif /* illumos */ 1907 1908 if (error == 0) { 1909 error = spa_vdev_add(spa, config); 1910 nvlist_free(config); 1911 } 1912 spa_close(spa, FTAG); 1913 return (error); 1914} 1915 1916/* 1917 * inputs: 1918 * zc_name name of the pool 1919 * zc_nvlist_conf nvlist of devices to remove 1920 * zc_cookie to stop the remove? 1921 */ 1922static int 1923zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1924{ 1925 spa_t *spa; 1926 int error; 1927 1928 error = spa_open(zc->zc_name, &spa, FTAG); 1929 if (error != 0) 1930 return (error); 1931 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 1932 spa_close(spa, FTAG); 1933 return (error); 1934} 1935 1936static int 1937zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1938{ 1939 spa_t *spa; 1940 int error; 1941 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1942 1943 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1944 return (error); 1945 switch (zc->zc_cookie) { 1946 case VDEV_STATE_ONLINE: 1947 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 1948 break; 1949 1950 case VDEV_STATE_OFFLINE: 1951 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 1952 break; 1953 1954 case VDEV_STATE_FAULTED: 1955 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1956 zc->zc_obj != VDEV_AUX_EXTERNAL) 1957 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1958 1959 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 1960 break; 1961 1962 case VDEV_STATE_DEGRADED: 1963 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1964 zc->zc_obj != VDEV_AUX_EXTERNAL) 1965 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1966 1967 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 1968 break; 1969 1970 default: 1971 error = SET_ERROR(EINVAL); 1972 } 1973 zc->zc_cookie = newstate; 1974 spa_close(spa, FTAG); 1975 return (error); 1976} 1977 1978static int 1979zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1980{ 1981 spa_t *spa; 1982 int replacing = zc->zc_cookie; 1983 nvlist_t *config; 1984 int error; 1985 1986 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1987 return (error); 1988 1989 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1990 zc->zc_iflags, &config)) == 0) { 1991 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1992 nvlist_free(config); 1993 } 1994 1995 spa_close(spa, FTAG); 1996 return (error); 1997} 1998 1999static int 2000zfs_ioc_vdev_detach(zfs_cmd_t *zc) 2001{ 2002 spa_t *spa; 2003 int error; 2004 2005 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2006 return (error); 2007 2008 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 2009 2010 spa_close(spa, FTAG); 2011 return (error); 2012} 2013 2014static int 2015zfs_ioc_vdev_split(zfs_cmd_t *zc) 2016{ 2017 spa_t *spa; 2018 nvlist_t *config, *props = NULL; 2019 int error; 2020 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 2021 2022 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2023 return (error); 2024 2025 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 2026 zc->zc_iflags, &config)) { 2027 spa_close(spa, FTAG); 2028 return (error); 2029 } 2030 2031 if (zc->zc_nvlist_src_size != 0 && (error = 2032 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2033 zc->zc_iflags, &props))) { 2034 spa_close(spa, FTAG); 2035 nvlist_free(config); 2036 return (error); 2037 } 2038 2039 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 2040 2041 spa_close(spa, FTAG); 2042 2043 nvlist_free(config); 2044 nvlist_free(props); 2045 2046 return (error); 2047} 2048 2049static int 2050zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 2051{ 2052 spa_t *spa; 2053 char *path = zc->zc_value; 2054 uint64_t guid = zc->zc_guid; 2055 int error; 2056 2057 error = spa_open(zc->zc_name, &spa, FTAG); 2058 if (error != 0) 2059 return (error); 2060 2061 error = spa_vdev_setpath(spa, guid, path); 2062 spa_close(spa, FTAG); 2063 return (error); 2064} 2065 2066static int 2067zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 2068{ 2069 spa_t *spa; 2070 char *fru = zc->zc_value; 2071 uint64_t guid = zc->zc_guid; 2072 int error; 2073 2074 error = spa_open(zc->zc_name, &spa, FTAG); 2075 if (error != 0) 2076 return (error); 2077 2078 error = spa_vdev_setfru(spa, guid, fru); 2079 spa_close(spa, FTAG); 2080 return (error); 2081} 2082 2083static int 2084zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) 2085{ 2086 int error = 0; 2087 nvlist_t *nv; 2088 2089 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 2090 2091 if (zc->zc_nvlist_dst != 0 && 2092 (error = dsl_prop_get_all(os, &nv)) == 0) { 2093 dmu_objset_stats(os, nv); 2094 /* 2095 * NB: zvol_get_stats() will read the objset contents, 2096 * which we aren't supposed to do with a 2097 * DS_MODE_USER hold, because it could be 2098 * inconsistent. So this is a bit of a workaround... 2099 * XXX reading with out owning 2100 */ 2101 if (!zc->zc_objset_stats.dds_inconsistent && 2102 dmu_objset_type(os) == DMU_OST_ZVOL) { 2103 error = zvol_get_stats(os, nv); 2104 if (error == EIO) 2105 return (error); 2106 VERIFY0(error); 2107 } 2108 error = put_nvlist(zc, nv); 2109 nvlist_free(nv); 2110 } 2111 2112 return (error); 2113} 2114 2115/* 2116 * inputs: 2117 * zc_name name of filesystem 2118 * zc_nvlist_dst_size size of buffer for property nvlist 2119 * 2120 * outputs: 2121 * zc_objset_stats stats 2122 * zc_nvlist_dst property nvlist 2123 * zc_nvlist_dst_size size of property nvlist 2124 */ 2125static int 2126zfs_ioc_objset_stats(zfs_cmd_t *zc) 2127{ 2128 objset_t *os; 2129 int error; 2130 2131 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2132 if (error == 0) { 2133 error = zfs_ioc_objset_stats_impl(zc, os); 2134 dmu_objset_rele(os, FTAG); 2135 } 2136 2137 if (error == ENOMEM) 2138 error = 0; 2139 return (error); 2140} 2141 2142/* 2143 * inputs: 2144 * zc_name name of filesystem 2145 * zc_nvlist_dst_size size of buffer for property nvlist 2146 * 2147 * outputs: 2148 * zc_nvlist_dst received property nvlist 2149 * zc_nvlist_dst_size size of received property nvlist 2150 * 2151 * Gets received properties (distinct from local properties on or after 2152 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 2153 * local property values. 2154 */ 2155static int 2156zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 2157{ 2158 int error = 0; 2159 nvlist_t *nv; 2160 2161 /* 2162 * Without this check, we would return local property values if the 2163 * caller has not already received properties on or after 2164 * SPA_VERSION_RECVD_PROPS. 2165 */ 2166 if (!dsl_prop_get_hasrecvd(zc->zc_name)) 2167 return (SET_ERROR(ENOTSUP)); 2168 2169 if (zc->zc_nvlist_dst != 0 && 2170 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) { 2171 error = put_nvlist(zc, nv); 2172 nvlist_free(nv); 2173 } 2174 2175 return (error); 2176} 2177 2178static int 2179nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 2180{ 2181 uint64_t value; 2182 int error; 2183 2184 /* 2185 * zfs_get_zplprop() will either find a value or give us 2186 * the default value (if there is one). 2187 */ 2188 if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 2189 return (error); 2190 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 2191 return (0); 2192} 2193 2194/* 2195 * inputs: 2196 * zc_name name of filesystem 2197 * zc_nvlist_dst_size size of buffer for zpl property nvlist 2198 * 2199 * outputs: 2200 * zc_nvlist_dst zpl property nvlist 2201 * zc_nvlist_dst_size size of zpl property nvlist 2202 */ 2203static int 2204zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 2205{ 2206 objset_t *os; 2207 int err; 2208 2209 /* XXX reading without owning */ 2210 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 2211 return (err); 2212 2213 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 2214 2215 /* 2216 * NB: nvl_add_zplprop() will read the objset contents, 2217 * which we aren't supposed to do with a DS_MODE_USER 2218 * hold, because it could be inconsistent. 2219 */ 2220 if (zc->zc_nvlist_dst != 0 && 2221 !zc->zc_objset_stats.dds_inconsistent && 2222 dmu_objset_type(os) == DMU_OST_ZFS) { 2223 nvlist_t *nv; 2224 2225 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2226 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 2227 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 2228 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 2229 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 2230 err = put_nvlist(zc, nv); 2231 nvlist_free(nv); 2232 } else { 2233 err = SET_ERROR(ENOENT); 2234 } 2235 dmu_objset_rele(os, FTAG); 2236 return (err); 2237} 2238 2239boolean_t 2240dataset_name_hidden(const char *name) 2241{ 2242 /* 2243 * Skip over datasets that are not visible in this zone, 2244 * internal datasets (which have a $ in their name), and 2245 * temporary datasets (which have a % in their name). 2246 */ 2247 if (strchr(name, '$') != NULL) 2248 return (B_TRUE); 2249 if (strchr(name, '%') != NULL) 2250 return (B_TRUE); 2251 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL)) 2252 return (B_TRUE); 2253 return (B_FALSE); 2254} 2255 2256/* 2257 * inputs: 2258 * zc_name name of filesystem 2259 * zc_cookie zap cursor 2260 * zc_nvlist_dst_size size of buffer for property nvlist 2261 * 2262 * outputs: 2263 * zc_name name of next filesystem 2264 * zc_cookie zap cursor 2265 * zc_objset_stats stats 2266 * zc_nvlist_dst property nvlist 2267 * zc_nvlist_dst_size size of property nvlist 2268 */ 2269static int 2270zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 2271{ 2272 objset_t *os; 2273 int error; 2274 char *p; 2275 size_t orig_len = strlen(zc->zc_name); 2276 2277top: 2278 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 2279 if (error == ENOENT) 2280 error = SET_ERROR(ESRCH); 2281 return (error); 2282 } 2283 2284 p = strrchr(zc->zc_name, '/'); 2285 if (p == NULL || p[1] != '\0') 2286 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 2287 p = zc->zc_name + strlen(zc->zc_name); 2288 2289 do { 2290 error = dmu_dir_list_next(os, 2291 sizeof (zc->zc_name) - (p - zc->zc_name), p, 2292 NULL, &zc->zc_cookie); 2293 if (error == ENOENT) 2294 error = SET_ERROR(ESRCH); 2295 } while (error == 0 && dataset_name_hidden(zc->zc_name)); 2296 dmu_objset_rele(os, FTAG); 2297 2298 /* 2299 * If it's an internal dataset (ie. with a '$' in its name), 2300 * don't try to get stats for it, otherwise we'll return ENOENT. 2301 */ 2302 if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 2303 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 2304 if (error == ENOENT) { 2305 /* We lost a race with destroy, get the next one. */ 2306 zc->zc_name[orig_len] = '\0'; 2307 goto top; 2308 } 2309 } 2310 return (error); 2311} 2312 2313/* 2314 * inputs: 2315 * zc_name name of filesystem 2316 * zc_cookie zap cursor 2317 * zc_nvlist_dst_size size of buffer for property nvlist 2318 * zc_simple when set, only name is requested 2319 * 2320 * outputs: 2321 * zc_name name of next snapshot 2322 * zc_objset_stats stats 2323 * zc_nvlist_dst property nvlist 2324 * zc_nvlist_dst_size size of property nvlist 2325 */ 2326static int 2327zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 2328{ 2329 objset_t *os; 2330 int error; 2331 2332 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2333 if (error != 0) { 2334 return (error == ENOENT ? ESRCH : error); 2335 } 2336 2337 /* 2338 * A dataset name of maximum length cannot have any snapshots, 2339 * so exit immediately. 2340 */ 2341 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 2342 dmu_objset_rele(os, FTAG); 2343 return (SET_ERROR(ESRCH)); 2344 } 2345 2346 error = dmu_snapshot_list_next(os, 2347 sizeof (zc->zc_name) - strlen(zc->zc_name), 2348 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie, 2349 NULL); 2350 2351 if (error == 0 && !zc->zc_simple) { 2352 dsl_dataset_t *ds; 2353 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 2354 2355 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds); 2356 if (error == 0) { 2357 objset_t *ossnap; 2358 2359 error = dmu_objset_from_ds(ds, &ossnap); 2360 if (error == 0) 2361 error = zfs_ioc_objset_stats_impl(zc, ossnap); 2362 dsl_dataset_rele(ds, FTAG); 2363 } 2364 } else if (error == ENOENT) { 2365 error = SET_ERROR(ESRCH); 2366 } 2367 2368 dmu_objset_rele(os, FTAG); 2369 /* if we failed, undo the @ that we tacked on to zc_name */ 2370 if (error != 0) 2371 *strchr(zc->zc_name, '@') = '\0'; 2372 return (error); 2373} 2374 2375static int 2376zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 2377{ 2378 const char *propname = nvpair_name(pair); 2379 uint64_t *valary; 2380 unsigned int vallen; 2381 const char *domain; 2382 char *dash; 2383 zfs_userquota_prop_t type; 2384 uint64_t rid; 2385 uint64_t quota; 2386 zfsvfs_t *zfsvfs; 2387 int err; 2388 2389 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2390 nvlist_t *attrs; 2391 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2392 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2393 &pair) != 0) 2394 return (SET_ERROR(EINVAL)); 2395 } 2396 2397 /* 2398 * A correctly constructed propname is encoded as 2399 * userquota@<rid>-<domain>. 2400 */ 2401 if ((dash = strchr(propname, '-')) == NULL || 2402 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 2403 vallen != 3) 2404 return (SET_ERROR(EINVAL)); 2405 2406 domain = dash + 1; 2407 type = valary[0]; 2408 rid = valary[1]; 2409 quota = valary[2]; 2410 2411 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); 2412 if (err == 0) { 2413 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 2414 zfsvfs_rele(zfsvfs, FTAG); 2415 } 2416 2417 return (err); 2418} 2419 2420/* 2421 * If the named property is one that has a special function to set its value, 2422 * return 0 on success and a positive error code on failure; otherwise if it is 2423 * not one of the special properties handled by this function, return -1. 2424 * 2425 * XXX: It would be better for callers of the property interface if we handled 2426 * these special cases in dsl_prop.c (in the dsl layer). 2427 */ 2428static int 2429zfs_prop_set_special(const char *dsname, zprop_source_t source, 2430 nvpair_t *pair) 2431{ 2432 const char *propname = nvpair_name(pair); 2433 zfs_prop_t prop = zfs_name_to_prop(propname); 2434 uint64_t intval; 2435 int err = -1; 2436 2437 if (prop == ZPROP_INVAL) { 2438 if (zfs_prop_userquota(propname)) 2439 return (zfs_prop_set_userquota(dsname, pair)); 2440 return (-1); 2441 } 2442 2443 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2444 nvlist_t *attrs; 2445 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2446 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2447 &pair) == 0); 2448 } 2449 2450 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 2451 return (-1); 2452 2453 VERIFY(0 == nvpair_value_uint64(pair, &intval)); 2454 2455 switch (prop) { 2456 case ZFS_PROP_QUOTA: 2457 err = dsl_dir_set_quota(dsname, source, intval); 2458 break; 2459 case ZFS_PROP_REFQUOTA: 2460 err = dsl_dataset_set_refquota(dsname, source, intval); 2461 break; 2462 case ZFS_PROP_FILESYSTEM_LIMIT: 2463 case ZFS_PROP_SNAPSHOT_LIMIT: 2464 if (intval == UINT64_MAX) { 2465 /* clearing the limit, just do it */ 2466 err = 0; 2467 } else { 2468 err = dsl_dir_activate_fs_ss_limit(dsname); 2469 } 2470 /* 2471 * Set err to -1 to force the zfs_set_prop_nvlist code down the 2472 * default path to set the value in the nvlist. 2473 */ 2474 if (err == 0) 2475 err = -1; 2476 break; 2477 case ZFS_PROP_RESERVATION: 2478 err = dsl_dir_set_reservation(dsname, source, intval); 2479 break; 2480 case ZFS_PROP_REFRESERVATION: 2481 err = dsl_dataset_set_refreservation(dsname, source, intval); 2482 break; 2483 case ZFS_PROP_VOLSIZE: 2484 err = zvol_set_volsize(dsname, intval); 2485 break; 2486 case ZFS_PROP_VERSION: 2487 { 2488 zfsvfs_t *zfsvfs; 2489 2490 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) 2491 break; 2492 2493 err = zfs_set_version(zfsvfs, intval); 2494 zfsvfs_rele(zfsvfs, FTAG); 2495 2496 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 2497 zfs_cmd_t *zc; 2498 2499 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2500 (void) strcpy(zc->zc_name, dsname); 2501 (void) zfs_ioc_userspace_upgrade(zc); 2502 kmem_free(zc, sizeof (zfs_cmd_t)); 2503 } 2504 break; 2505 } 2506 default: 2507 err = -1; 2508 } 2509 2510 return (err); 2511} 2512 2513/* 2514 * This function is best effort. If it fails to set any of the given properties, 2515 * it continues to set as many as it can and returns the last error 2516 * encountered. If the caller provides a non-NULL errlist, it will be filled in 2517 * with the list of names of all the properties that failed along with the 2518 * corresponding error numbers. 2519 * 2520 * If every property is set successfully, zero is returned and errlist is not 2521 * modified. 2522 */ 2523int 2524zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 2525 nvlist_t *errlist) 2526{ 2527 nvpair_t *pair; 2528 nvpair_t *propval; 2529 int rv = 0; 2530 uint64_t intval; 2531 char *strval; 2532 nvlist_t *genericnvl = fnvlist_alloc(); 2533 nvlist_t *retrynvl = fnvlist_alloc(); 2534 2535retry: 2536 pair = NULL; 2537 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2538 const char *propname = nvpair_name(pair); 2539 zfs_prop_t prop = zfs_name_to_prop(propname); 2540 int err = 0; 2541 2542 /* decode the property value */ 2543 propval = pair; 2544 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2545 nvlist_t *attrs; 2546 attrs = fnvpair_value_nvlist(pair); 2547 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2548 &propval) != 0) 2549 err = SET_ERROR(EINVAL); 2550 } 2551 2552 /* Validate value type */ 2553 if (err == 0 && prop == ZPROP_INVAL) { 2554 if (zfs_prop_user(propname)) { 2555 if (nvpair_type(propval) != DATA_TYPE_STRING) 2556 err = SET_ERROR(EINVAL); 2557 } else if (zfs_prop_userquota(propname)) { 2558 if (nvpair_type(propval) != 2559 DATA_TYPE_UINT64_ARRAY) 2560 err = SET_ERROR(EINVAL); 2561 } else { 2562 err = SET_ERROR(EINVAL); 2563 } 2564 } else if (err == 0) { 2565 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2566 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 2567 err = SET_ERROR(EINVAL); 2568 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 2569 const char *unused; 2570 2571 intval = fnvpair_value_uint64(propval); 2572 2573 switch (zfs_prop_get_type(prop)) { 2574 case PROP_TYPE_NUMBER: 2575 break; 2576 case PROP_TYPE_STRING: 2577 err = SET_ERROR(EINVAL); 2578 break; 2579 case PROP_TYPE_INDEX: 2580 if (zfs_prop_index_to_string(prop, 2581 intval, &unused) != 0) 2582 err = SET_ERROR(EINVAL); 2583 break; 2584 default: 2585 cmn_err(CE_PANIC, 2586 "unknown property type"); 2587 } 2588 } else { 2589 err = SET_ERROR(EINVAL); 2590 } 2591 } 2592 2593 /* Validate permissions */ 2594 if (err == 0) 2595 err = zfs_check_settable(dsname, pair, CRED()); 2596 2597 if (err == 0) { 2598 err = zfs_prop_set_special(dsname, source, pair); 2599 if (err == -1) { 2600 /* 2601 * For better performance we build up a list of 2602 * properties to set in a single transaction. 2603 */ 2604 err = nvlist_add_nvpair(genericnvl, pair); 2605 } else if (err != 0 && nvl != retrynvl) { 2606 /* 2607 * This may be a spurious error caused by 2608 * receiving quota and reservation out of order. 2609 * Try again in a second pass. 2610 */ 2611 err = nvlist_add_nvpair(retrynvl, pair); 2612 } 2613 } 2614 2615 if (err != 0) { 2616 if (errlist != NULL) 2617 fnvlist_add_int32(errlist, propname, err); 2618 rv = err; 2619 } 2620 } 2621 2622 if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 2623 nvl = retrynvl; 2624 goto retry; 2625 } 2626 2627 if (!nvlist_empty(genericnvl) && 2628 dsl_props_set(dsname, source, genericnvl) != 0) { 2629 /* 2630 * If this fails, we still want to set as many properties as we 2631 * can, so try setting them individually. 2632 */ 2633 pair = NULL; 2634 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 2635 const char *propname = nvpair_name(pair); 2636 int err = 0; 2637 2638 propval = pair; 2639 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2640 nvlist_t *attrs; 2641 attrs = fnvpair_value_nvlist(pair); 2642 propval = fnvlist_lookup_nvpair(attrs, 2643 ZPROP_VALUE); 2644 } 2645 2646 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2647 strval = fnvpair_value_string(propval); 2648 err = dsl_prop_set_string(dsname, propname, 2649 source, strval); 2650 } else { 2651 intval = fnvpair_value_uint64(propval); 2652 err = dsl_prop_set_int(dsname, propname, source, 2653 intval); 2654 } 2655 2656 if (err != 0) { 2657 if (errlist != NULL) { 2658 fnvlist_add_int32(errlist, propname, 2659 err); 2660 } 2661 rv = err; 2662 } 2663 } 2664 } 2665 nvlist_free(genericnvl); 2666 nvlist_free(retrynvl); 2667 2668 return (rv); 2669} 2670 2671/* 2672 * Check that all the properties are valid user properties. 2673 */ 2674static int 2675zfs_check_userprops(const char *fsname, nvlist_t *nvl) 2676{ 2677 nvpair_t *pair = NULL; 2678 int error = 0; 2679 2680 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2681 const char *propname = nvpair_name(pair); 2682 2683 if (!zfs_prop_user(propname) || 2684 nvpair_type(pair) != DATA_TYPE_STRING) 2685 return (SET_ERROR(EINVAL)); 2686 2687 if (error = zfs_secpolicy_write_perms(fsname, 2688 ZFS_DELEG_PERM_USERPROP, CRED())) 2689 return (error); 2690 2691 if (strlen(propname) >= ZAP_MAXNAMELEN) 2692 return (SET_ERROR(ENAMETOOLONG)); 2693 2694 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN) 2695 return (E2BIG); 2696 } 2697 return (0); 2698} 2699 2700static void 2701props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 2702{ 2703 nvpair_t *pair; 2704 2705 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2706 2707 pair = NULL; 2708 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 2709 if (nvlist_exists(skipped, nvpair_name(pair))) 2710 continue; 2711 2712 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 2713 } 2714} 2715 2716static int 2717clear_received_props(const char *dsname, nvlist_t *props, 2718 nvlist_t *skipped) 2719{ 2720 int err = 0; 2721 nvlist_t *cleared_props = NULL; 2722 props_skip(props, skipped, &cleared_props); 2723 if (!nvlist_empty(cleared_props)) { 2724 /* 2725 * Acts on local properties until the dataset has received 2726 * properties at least once on or after SPA_VERSION_RECVD_PROPS. 2727 */ 2728 zprop_source_t flags = (ZPROP_SRC_NONE | 2729 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0)); 2730 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL); 2731 } 2732 nvlist_free(cleared_props); 2733 return (err); 2734} 2735 2736/* 2737 * inputs: 2738 * zc_name name of filesystem 2739 * zc_value name of property to set 2740 * zc_nvlist_src{_size} nvlist of properties to apply 2741 * zc_cookie received properties flag 2742 * 2743 * outputs: 2744 * zc_nvlist_dst{_size} error for each unapplied received property 2745 */ 2746static int 2747zfs_ioc_set_prop(zfs_cmd_t *zc) 2748{ 2749 nvlist_t *nvl; 2750 boolean_t received = zc->zc_cookie; 2751 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 2752 ZPROP_SRC_LOCAL); 2753 nvlist_t *errors; 2754 int error; 2755 2756 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2757 zc->zc_iflags, &nvl)) != 0) 2758 return (error); 2759 2760 if (received) { 2761 nvlist_t *origprops; 2762 2763 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) { 2764 (void) clear_received_props(zc->zc_name, 2765 origprops, nvl); 2766 nvlist_free(origprops); 2767 } 2768 2769 error = dsl_prop_set_hasrecvd(zc->zc_name); 2770 } 2771 2772 errors = fnvlist_alloc(); 2773 if (error == 0) 2774 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors); 2775 2776 if (zc->zc_nvlist_dst != 0 && errors != NULL) { 2777 (void) put_nvlist(zc, errors); 2778 } 2779 2780 nvlist_free(errors); 2781 nvlist_free(nvl); 2782 return (error); 2783} 2784 2785/* 2786 * inputs: 2787 * zc_name name of filesystem 2788 * zc_value name of property to inherit 2789 * zc_cookie revert to received value if TRUE 2790 * 2791 * outputs: none 2792 */ 2793static int 2794zfs_ioc_inherit_prop(zfs_cmd_t *zc) 2795{ 2796 const char *propname = zc->zc_value; 2797 zfs_prop_t prop = zfs_name_to_prop(propname); 2798 boolean_t received = zc->zc_cookie; 2799 zprop_source_t source = (received 2800 ? ZPROP_SRC_NONE /* revert to received value, if any */ 2801 : ZPROP_SRC_INHERITED); /* explicitly inherit */ 2802 2803 if (received) { 2804 nvlist_t *dummy; 2805 nvpair_t *pair; 2806 zprop_type_t type; 2807 int err; 2808 2809 /* 2810 * zfs_prop_set_special() expects properties in the form of an 2811 * nvpair with type info. 2812 */ 2813 if (prop == ZPROP_INVAL) { 2814 if (!zfs_prop_user(propname)) 2815 return (SET_ERROR(EINVAL)); 2816 2817 type = PROP_TYPE_STRING; 2818 } else if (prop == ZFS_PROP_VOLSIZE || 2819 prop == ZFS_PROP_VERSION) { 2820 return (SET_ERROR(EINVAL)); 2821 } else { 2822 type = zfs_prop_get_type(prop); 2823 } 2824 2825 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2826 2827 switch (type) { 2828 case PROP_TYPE_STRING: 2829 VERIFY(0 == nvlist_add_string(dummy, propname, "")); 2830 break; 2831 case PROP_TYPE_NUMBER: 2832 case PROP_TYPE_INDEX: 2833 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 2834 break; 2835 default: 2836 nvlist_free(dummy); 2837 return (SET_ERROR(EINVAL)); 2838 } 2839 2840 pair = nvlist_next_nvpair(dummy, NULL); 2841 err = zfs_prop_set_special(zc->zc_name, source, pair); 2842 nvlist_free(dummy); 2843 if (err != -1) 2844 return (err); /* special property already handled */ 2845 } else { 2846 /* 2847 * Only check this in the non-received case. We want to allow 2848 * 'inherit -S' to revert non-inheritable properties like quota 2849 * and reservation to the received or default values even though 2850 * they are not considered inheritable. 2851 */ 2852 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 2853 return (SET_ERROR(EINVAL)); 2854 } 2855 2856 /* property name has been validated by zfs_secpolicy_inherit_prop() */ 2857 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source)); 2858} 2859 2860static int 2861zfs_ioc_pool_set_props(zfs_cmd_t *zc) 2862{ 2863 nvlist_t *props; 2864 spa_t *spa; 2865 int error; 2866 nvpair_t *pair; 2867 2868 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2869 zc->zc_iflags, &props)) 2870 return (error); 2871 2872 /* 2873 * If the only property is the configfile, then just do a spa_lookup() 2874 * to handle the faulted case. 2875 */ 2876 pair = nvlist_next_nvpair(props, NULL); 2877 if (pair != NULL && strcmp(nvpair_name(pair), 2878 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 2879 nvlist_next_nvpair(props, pair) == NULL) { 2880 mutex_enter(&spa_namespace_lock); 2881 if ((spa = spa_lookup(zc->zc_name)) != NULL) { 2882 spa_configfile_set(spa, props, B_FALSE); 2883 spa_config_sync(spa, B_FALSE, B_TRUE); 2884 } 2885 mutex_exit(&spa_namespace_lock); 2886 if (spa != NULL) { 2887 nvlist_free(props); 2888 return (0); 2889 } 2890 } 2891 2892 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2893 nvlist_free(props); 2894 return (error); 2895 } 2896 2897 error = spa_prop_set(spa, props); 2898 2899 nvlist_free(props); 2900 spa_close(spa, FTAG); 2901 2902 return (error); 2903} 2904 2905static int 2906zfs_ioc_pool_get_props(zfs_cmd_t *zc) 2907{ 2908 spa_t *spa; 2909 int error; 2910 nvlist_t *nvp = NULL; 2911 2912 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2913 /* 2914 * If the pool is faulted, there may be properties we can still 2915 * get (such as altroot and cachefile), so attempt to get them 2916 * anyway. 2917 */ 2918 mutex_enter(&spa_namespace_lock); 2919 if ((spa = spa_lookup(zc->zc_name)) != NULL) 2920 error = spa_prop_get(spa, &nvp); 2921 mutex_exit(&spa_namespace_lock); 2922 } else { 2923 error = spa_prop_get(spa, &nvp); 2924 spa_close(spa, FTAG); 2925 } 2926 2927 if (error == 0 && zc->zc_nvlist_dst != 0) 2928 error = put_nvlist(zc, nvp); 2929 else 2930 error = SET_ERROR(EFAULT); 2931 2932 nvlist_free(nvp); 2933 return (error); 2934} 2935 2936/* 2937 * inputs: 2938 * zc_name name of filesystem 2939 * zc_nvlist_src{_size} nvlist of delegated permissions 2940 * zc_perm_action allow/unallow flag 2941 * 2942 * outputs: none 2943 */ 2944static int 2945zfs_ioc_set_fsacl(zfs_cmd_t *zc) 2946{ 2947 int error; 2948 nvlist_t *fsaclnv = NULL; 2949 2950 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2951 zc->zc_iflags, &fsaclnv)) != 0) 2952 return (error); 2953 2954 /* 2955 * Verify nvlist is constructed correctly 2956 */ 2957 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 2958 nvlist_free(fsaclnv); 2959 return (SET_ERROR(EINVAL)); 2960 } 2961 2962 /* 2963 * If we don't have PRIV_SYS_MOUNT, then validate 2964 * that user is allowed to hand out each permission in 2965 * the nvlist(s) 2966 */ 2967 2968 error = secpolicy_zfs(CRED()); 2969 if (error != 0) { 2970 if (zc->zc_perm_action == B_FALSE) { 2971 error = dsl_deleg_can_allow(zc->zc_name, 2972 fsaclnv, CRED()); 2973 } else { 2974 error = dsl_deleg_can_unallow(zc->zc_name, 2975 fsaclnv, CRED()); 2976 } 2977 } 2978 2979 if (error == 0) 2980 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 2981 2982 nvlist_free(fsaclnv); 2983 return (error); 2984} 2985 2986/* 2987 * inputs: 2988 * zc_name name of filesystem 2989 * 2990 * outputs: 2991 * zc_nvlist_src{_size} nvlist of delegated permissions 2992 */ 2993static int 2994zfs_ioc_get_fsacl(zfs_cmd_t *zc) 2995{ 2996 nvlist_t *nvp; 2997 int error; 2998 2999 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 3000 error = put_nvlist(zc, nvp); 3001 nvlist_free(nvp); 3002 } 3003 3004 return (error); 3005} 3006 3007/* 3008 * Search the vfs list for a specified resource. Returns a pointer to it 3009 * or NULL if no suitable entry is found. The caller of this routine 3010 * is responsible for releasing the returned vfs pointer. 3011 */ 3012static vfs_t * 3013zfs_get_vfs(const char *resource) 3014{ 3015 vfs_t *vfsp; 3016 3017 mtx_lock(&mountlist_mtx); 3018 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) { 3019 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 3020 VFS_HOLD(vfsp); 3021 break; 3022 } 3023 } 3024 mtx_unlock(&mountlist_mtx); 3025 return (vfsp); 3026} 3027 3028/* ARGSUSED */ 3029static void 3030zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 3031{ 3032 zfs_creat_t *zct = arg; 3033 3034 zfs_create_fs(os, cr, zct->zct_zplprops, tx); 3035} 3036 3037#define ZFS_PROP_UNDEFINED ((uint64_t)-1) 3038 3039/* 3040 * inputs: 3041 * os parent objset pointer (NULL if root fs) 3042 * fuids_ok fuids allowed in this version of the spa? 3043 * sa_ok SAs allowed in this version of the spa? 3044 * createprops list of properties requested by creator 3045 * 3046 * outputs: 3047 * zplprops values for the zplprops we attach to the master node object 3048 * is_ci true if requested file system will be purely case-insensitive 3049 * 3050 * Determine the settings for utf8only, normalization and 3051 * casesensitivity. Specific values may have been requested by the 3052 * creator and/or we can inherit values from the parent dataset. If 3053 * the file system is of too early a vintage, a creator can not 3054 * request settings for these properties, even if the requested 3055 * setting is the default value. We don't actually want to create dsl 3056 * properties for these, so remove them from the source nvlist after 3057 * processing. 3058 */ 3059static int 3060zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 3061 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 3062 nvlist_t *zplprops, boolean_t *is_ci) 3063{ 3064 uint64_t sense = ZFS_PROP_UNDEFINED; 3065 uint64_t norm = ZFS_PROP_UNDEFINED; 3066 uint64_t u8 = ZFS_PROP_UNDEFINED; 3067 3068 ASSERT(zplprops != NULL); 3069 3070 /* 3071 * Pull out creator prop choices, if any. 3072 */ 3073 if (createprops) { 3074 (void) nvlist_lookup_uint64(createprops, 3075 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 3076 (void) nvlist_lookup_uint64(createprops, 3077 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 3078 (void) nvlist_remove_all(createprops, 3079 zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 3080 (void) nvlist_lookup_uint64(createprops, 3081 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 3082 (void) nvlist_remove_all(createprops, 3083 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 3084 (void) nvlist_lookup_uint64(createprops, 3085 zfs_prop_to_name(ZFS_PROP_CASE), &sense); 3086 (void) nvlist_remove_all(createprops, 3087 zfs_prop_to_name(ZFS_PROP_CASE)); 3088 } 3089 3090 /* 3091 * If the zpl version requested is whacky or the file system 3092 * or pool is version is too "young" to support normalization 3093 * and the creator tried to set a value for one of the props, 3094 * error out. 3095 */ 3096 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 3097 (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 3098 (zplver >= ZPL_VERSION_SA && !sa_ok) || 3099 (zplver < ZPL_VERSION_NORMALIZATION && 3100 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 3101 sense != ZFS_PROP_UNDEFINED))) 3102 return (SET_ERROR(ENOTSUP)); 3103 3104 /* 3105 * Put the version in the zplprops 3106 */ 3107 VERIFY(nvlist_add_uint64(zplprops, 3108 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 3109 3110 if (norm == ZFS_PROP_UNDEFINED) 3111 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 3112 VERIFY(nvlist_add_uint64(zplprops, 3113 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 3114 3115 /* 3116 * If we're normalizing, names must always be valid UTF-8 strings. 3117 */ 3118 if (norm) 3119 u8 = 1; 3120 if (u8 == ZFS_PROP_UNDEFINED) 3121 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 3122 VERIFY(nvlist_add_uint64(zplprops, 3123 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 3124 3125 if (sense == ZFS_PROP_UNDEFINED) 3126 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 3127 VERIFY(nvlist_add_uint64(zplprops, 3128 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 3129 3130 if (is_ci) 3131 *is_ci = (sense == ZFS_CASE_INSENSITIVE); 3132 3133 return (0); 3134} 3135 3136static int 3137zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 3138 nvlist_t *zplprops, boolean_t *is_ci) 3139{ 3140 boolean_t fuids_ok, sa_ok; 3141 uint64_t zplver = ZPL_VERSION; 3142 objset_t *os = NULL; 3143 char parentname[MAXNAMELEN]; 3144 char *cp; 3145 spa_t *spa; 3146 uint64_t spa_vers; 3147 int error; 3148 3149 (void) strlcpy(parentname, dataset, sizeof (parentname)); 3150 cp = strrchr(parentname, '/'); 3151 ASSERT(cp != NULL); 3152 cp[0] = '\0'; 3153 3154 if ((error = spa_open(dataset, &spa, FTAG)) != 0) 3155 return (error); 3156 3157 spa_vers = spa_version(spa); 3158 spa_close(spa, FTAG); 3159 3160 zplver = zfs_zpl_version_map(spa_vers); 3161 fuids_ok = (zplver >= ZPL_VERSION_FUID); 3162 sa_ok = (zplver >= ZPL_VERSION_SA); 3163 3164 /* 3165 * Open parent object set so we can inherit zplprop values. 3166 */ 3167 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 3168 return (error); 3169 3170 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 3171 zplprops, is_ci); 3172 dmu_objset_rele(os, FTAG); 3173 return (error); 3174} 3175 3176static int 3177zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 3178 nvlist_t *zplprops, boolean_t *is_ci) 3179{ 3180 boolean_t fuids_ok; 3181 boolean_t sa_ok; 3182 uint64_t zplver = ZPL_VERSION; 3183 int error; 3184 3185 zplver = zfs_zpl_version_map(spa_vers); 3186 fuids_ok = (zplver >= ZPL_VERSION_FUID); 3187 sa_ok = (zplver >= ZPL_VERSION_SA); 3188 3189 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 3190 createprops, zplprops, is_ci); 3191 return (error); 3192} 3193 3194/* 3195 * innvl: { 3196 * "type" -> dmu_objset_type_t (int32) 3197 * (optional) "props" -> { prop -> value } 3198 * } 3199 * 3200 * outnvl: propname -> error code (int32) 3201 */ 3202static int 3203zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3204{ 3205 int error = 0; 3206 zfs_creat_t zct = { 0 }; 3207 nvlist_t *nvprops = NULL; 3208 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 3209 int32_t type32; 3210 dmu_objset_type_t type; 3211 boolean_t is_insensitive = B_FALSE; 3212 3213 if (nvlist_lookup_int32(innvl, "type", &type32) != 0) 3214 return (SET_ERROR(EINVAL)); 3215 type = type32; 3216 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3217 3218 switch (type) { 3219 case DMU_OST_ZFS: 3220 cbfunc = zfs_create_cb; 3221 break; 3222 3223 case DMU_OST_ZVOL: 3224 cbfunc = zvol_create_cb; 3225 break; 3226 3227 default: 3228 cbfunc = NULL; 3229 break; 3230 } 3231 if (strchr(fsname, '@') || 3232 strchr(fsname, '%')) 3233 return (SET_ERROR(EINVAL)); 3234 3235 zct.zct_props = nvprops; 3236 3237 if (cbfunc == NULL) 3238 return (SET_ERROR(EINVAL)); 3239 3240 if (type == DMU_OST_ZVOL) { 3241 uint64_t volsize, volblocksize; 3242 3243 if (nvprops == NULL) 3244 return (SET_ERROR(EINVAL)); 3245 if (nvlist_lookup_uint64(nvprops, 3246 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0) 3247 return (SET_ERROR(EINVAL)); 3248 3249 if ((error = nvlist_lookup_uint64(nvprops, 3250 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3251 &volblocksize)) != 0 && error != ENOENT) 3252 return (SET_ERROR(EINVAL)); 3253 3254 if (error != 0) 3255 volblocksize = zfs_prop_default_numeric( 3256 ZFS_PROP_VOLBLOCKSIZE); 3257 3258 if ((error = zvol_check_volblocksize( 3259 volblocksize)) != 0 || 3260 (error = zvol_check_volsize(volsize, 3261 volblocksize)) != 0) 3262 return (error); 3263 } else if (type == DMU_OST_ZFS) { 3264 int error; 3265 3266 /* 3267 * We have to have normalization and 3268 * case-folding flags correct when we do the 3269 * file system creation, so go figure them out 3270 * now. 3271 */ 3272 VERIFY(nvlist_alloc(&zct.zct_zplprops, 3273 NV_UNIQUE_NAME, KM_SLEEP) == 0); 3274 error = zfs_fill_zplprops(fsname, nvprops, 3275 zct.zct_zplprops, &is_insensitive); 3276 if (error != 0) { 3277 nvlist_free(zct.zct_zplprops); 3278 return (error); 3279 } 3280 } 3281 3282 error = dmu_objset_create(fsname, type, 3283 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 3284 nvlist_free(zct.zct_zplprops); 3285 3286 /* 3287 * It would be nice to do this atomically. 3288 */ 3289 if (error == 0) { 3290 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3291 nvprops, outnvl); 3292 if (error != 0) 3293 (void) dsl_destroy_head(fsname); 3294 } 3295#ifdef __FreeBSD__ 3296 if (error == 0 && type == DMU_OST_ZVOL) 3297 zvol_create_minors(fsname); 3298#endif 3299 return (error); 3300} 3301 3302/* 3303 * innvl: { 3304 * "origin" -> name of origin snapshot 3305 * (optional) "props" -> { prop -> value } 3306 * } 3307 * 3308 * outnvl: propname -> error code (int32) 3309 */ 3310static int 3311zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3312{ 3313 int error = 0; 3314 nvlist_t *nvprops = NULL; 3315 char *origin_name; 3316 3317 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0) 3318 return (SET_ERROR(EINVAL)); 3319 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3320 3321 if (strchr(fsname, '@') || 3322 strchr(fsname, '%')) 3323 return (SET_ERROR(EINVAL)); 3324 3325 if (dataset_namecheck(origin_name, NULL, NULL) != 0) 3326 return (SET_ERROR(EINVAL)); 3327 error = dmu_objset_clone(fsname, origin_name); 3328 if (error != 0) 3329 return (error); 3330 3331 /* 3332 * It would be nice to do this atomically. 3333 */ 3334 if (error == 0) { 3335 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3336 nvprops, outnvl); 3337 if (error != 0) 3338 (void) dsl_destroy_head(fsname); 3339 } 3340#ifdef __FreeBSD__ 3341 if (error == 0) 3342 zvol_create_minors(fsname); 3343#endif 3344 return (error); 3345} 3346 3347/* 3348 * innvl: { 3349 * "snaps" -> { snapshot1, snapshot2 } 3350 * (optional) "props" -> { prop -> value (string) } 3351 * } 3352 * 3353 * outnvl: snapshot -> error code (int32) 3354 */ 3355static int 3356zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3357{ 3358 nvlist_t *snaps; 3359 nvlist_t *props = NULL; 3360 int error, poollen; 3361 nvpair_t *pair; 3362 3363 (void) nvlist_lookup_nvlist(innvl, "props", &props); 3364 if ((error = zfs_check_userprops(poolname, props)) != 0) 3365 return (error); 3366 3367 if (!nvlist_empty(props) && 3368 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS)) 3369 return (SET_ERROR(ENOTSUP)); 3370 3371 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3372 return (SET_ERROR(EINVAL)); 3373 poollen = strlen(poolname); 3374 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3375 pair = nvlist_next_nvpair(snaps, pair)) { 3376 const char *name = nvpair_name(pair); 3377 const char *cp = strchr(name, '@'); 3378 3379 /* 3380 * The snap name must contain an @, and the part after it must 3381 * contain only valid characters. 3382 */ 3383 if (cp == NULL || 3384 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3385 return (SET_ERROR(EINVAL)); 3386 3387 /* 3388 * The snap must be in the specified pool. 3389 */ 3390 if (strncmp(name, poolname, poollen) != 0 || 3391 (name[poollen] != '/' && name[poollen] != '@')) 3392 return (SET_ERROR(EXDEV)); 3393 3394 /* This must be the only snap of this fs. */ 3395 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair); 3396 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) { 3397 if (strncmp(name, nvpair_name(pair2), cp - name + 1) 3398 == 0) { 3399 return (SET_ERROR(EXDEV)); 3400 } 3401 } 3402 } 3403 3404 error = dsl_dataset_snapshot(snaps, props, outnvl); 3405 return (error); 3406} 3407 3408/* 3409 * innvl: "message" -> string 3410 */ 3411/* ARGSUSED */ 3412static int 3413zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl) 3414{ 3415 char *message; 3416 spa_t *spa; 3417 int error; 3418 char *poolname; 3419 3420 /* 3421 * The poolname in the ioctl is not set, we get it from the TSD, 3422 * which was set at the end of the last successful ioctl that allows 3423 * logging. The secpolicy func already checked that it is set. 3424 * Only one log ioctl is allowed after each successful ioctl, so 3425 * we clear the TSD here. 3426 */ 3427 poolname = tsd_get(zfs_allow_log_key); 3428 (void) tsd_set(zfs_allow_log_key, NULL); 3429 error = spa_open(poolname, &spa, FTAG); 3430 strfree(poolname); 3431 if (error != 0) 3432 return (error); 3433 3434 if (nvlist_lookup_string(innvl, "message", &message) != 0) { 3435 spa_close(spa, FTAG); 3436 return (SET_ERROR(EINVAL)); 3437 } 3438 3439 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 3440 spa_close(spa, FTAG); 3441 return (SET_ERROR(ENOTSUP)); 3442 } 3443 3444 error = spa_history_log(spa, message); 3445 spa_close(spa, FTAG); 3446 return (error); 3447} 3448 3449/* 3450 * The dp_config_rwlock must not be held when calling this, because the 3451 * unmount may need to write out data. 3452 * 3453 * This function is best-effort. Callers must deal gracefully if it 3454 * remains mounted (or is remounted after this call). 3455 * 3456 * Returns 0 if the argument is not a snapshot, or it is not currently a 3457 * filesystem, or we were able to unmount it. Returns error code otherwise. 3458 */ 3459int 3460zfs_unmount_snap(const char *snapname) 3461{ 3462 vfs_t *vfsp; 3463 zfsvfs_t *zfsvfs; 3464 int err; 3465 3466 if (strchr(snapname, '@') == NULL) 3467 return (0); 3468 3469 vfsp = zfs_get_vfs(snapname); 3470 if (vfsp == NULL) 3471 return (0); 3472 3473 zfsvfs = vfsp->vfs_data; 3474 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os))); 3475 3476 err = vn_vfswlock(vfsp->vfs_vnodecovered); 3477 VFS_RELE(vfsp); 3478 if (err != 0) 3479 return (SET_ERROR(err)); 3480 3481 /* 3482 * Always force the unmount for snapshots. 3483 */ 3484 3485#ifdef illumos 3486 (void) dounmount(vfsp, MS_FORCE, kcred); 3487#else 3488 vfs_ref(vfsp); 3489 (void) dounmount(vfsp, MS_FORCE, curthread); 3490#endif 3491 return (0); 3492} 3493 3494/* ARGSUSED */ 3495static int 3496zfs_unmount_snap_cb(const char *snapname, void *arg) 3497{ 3498 return (zfs_unmount_snap(snapname)); 3499} 3500 3501/* 3502 * When a clone is destroyed, its origin may also need to be destroyed, 3503 * in which case it must be unmounted. This routine will do that unmount 3504 * if necessary. 3505 */ 3506void 3507zfs_destroy_unmount_origin(const char *fsname) 3508{ 3509 int error; 3510 objset_t *os; 3511 dsl_dataset_t *ds; 3512 3513 error = dmu_objset_hold(fsname, FTAG, &os); 3514 if (error != 0) 3515 return; 3516 ds = dmu_objset_ds(os); 3517 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) { 3518 char originname[MAXNAMELEN]; 3519 dsl_dataset_name(ds->ds_prev, originname); 3520 dmu_objset_rele(os, FTAG); 3521 (void) zfs_unmount_snap(originname); 3522 } else { 3523 dmu_objset_rele(os, FTAG); 3524 } 3525} 3526 3527/* 3528 * innvl: { 3529 * "snaps" -> { snapshot1, snapshot2 } 3530 * (optional boolean) "defer" 3531 * } 3532 * 3533 * outnvl: snapshot -> error code (int32) 3534 * 3535 */ 3536/* ARGSUSED */ 3537static int 3538zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3539{ 3540 int error, poollen; 3541 nvlist_t *snaps; 3542 nvpair_t *pair; 3543 boolean_t defer; 3544 3545 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3546 return (SET_ERROR(EINVAL)); 3547 defer = nvlist_exists(innvl, "defer"); 3548 3549 poollen = strlen(poolname); 3550 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3551 pair = nvlist_next_nvpair(snaps, pair)) { 3552 const char *name = nvpair_name(pair); 3553 3554 /* 3555 * The snap must be in the specified pool to prevent the 3556 * invalid removal of zvol minors below. 3557 */ 3558 if (strncmp(name, poolname, poollen) != 0 || 3559 (name[poollen] != '/' && name[poollen] != '@')) 3560 return (SET_ERROR(EXDEV)); 3561 3562 error = zfs_unmount_snap(name); 3563 if (error != 0) 3564 return (error); 3565#if defined(__FreeBSD__) 3566 zvol_remove_minors(name); 3567#endif 3568 } 3569 3570 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl)); 3571} 3572 3573/* 3574 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>. 3575 * All bookmarks must be in the same pool. 3576 * 3577 * innvl: { 3578 * bookmark1 -> snapshot1, bookmark2 -> snapshot2 3579 * } 3580 * 3581 * outnvl: bookmark -> error code (int32) 3582 * 3583 */ 3584/* ARGSUSED */ 3585static int 3586zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3587{ 3588 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 3589 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 3590 char *snap_name; 3591 3592 /* 3593 * Verify the snapshot argument. 3594 */ 3595 if (nvpair_value_string(pair, &snap_name) != 0) 3596 return (SET_ERROR(EINVAL)); 3597 3598 3599 /* Verify that the keys (bookmarks) are unique */ 3600 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair); 3601 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) { 3602 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0) 3603 return (SET_ERROR(EINVAL)); 3604 } 3605 } 3606 3607 return (dsl_bookmark_create(innvl, outnvl)); 3608} 3609 3610/* 3611 * innvl: { 3612 * property 1, property 2, ... 3613 * } 3614 * 3615 * outnvl: { 3616 * bookmark name 1 -> { property 1, property 2, ... }, 3617 * bookmark name 2 -> { property 1, property 2, ... } 3618 * } 3619 * 3620 */ 3621static int 3622zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3623{ 3624 return (dsl_get_bookmarks(fsname, innvl, outnvl)); 3625} 3626 3627/* 3628 * innvl: { 3629 * bookmark name 1, bookmark name 2 3630 * } 3631 * 3632 * outnvl: bookmark -> error code (int32) 3633 * 3634 */ 3635static int 3636zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl, 3637 nvlist_t *outnvl) 3638{ 3639 int error, poollen; 3640 3641 poollen = strlen(poolname); 3642 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 3643 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 3644 const char *name = nvpair_name(pair); 3645 const char *cp = strchr(name, '#'); 3646 3647 /* 3648 * The bookmark name must contain an #, and the part after it 3649 * must contain only valid characters. 3650 */ 3651 if (cp == NULL || 3652 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3653 return (SET_ERROR(EINVAL)); 3654 3655 /* 3656 * The bookmark must be in the specified pool. 3657 */ 3658 if (strncmp(name, poolname, poollen) != 0 || 3659 (name[poollen] != '/' && name[poollen] != '#')) 3660 return (SET_ERROR(EXDEV)); 3661 } 3662 3663 error = dsl_bookmark_destroy(innvl, outnvl); 3664 return (error); 3665} 3666 3667/* 3668 * inputs: 3669 * zc_name name of dataset to destroy 3670 * zc_objset_type type of objset 3671 * zc_defer_destroy mark for deferred destroy 3672 * 3673 * outputs: none 3674 */ 3675static int 3676zfs_ioc_destroy(zfs_cmd_t *zc) 3677{ 3678 int err; 3679 3680 if (zc->zc_objset_type == DMU_OST_ZFS) { 3681 err = zfs_unmount_snap(zc->zc_name); 3682 if (err != 0) 3683 return (err); 3684 } 3685 3686 if (strchr(zc->zc_name, '@')) 3687 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy); 3688 else 3689 err = dsl_destroy_head(zc->zc_name); 3690 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 3691#ifdef __FreeBSD__ 3692 zvol_remove_minors(zc->zc_name); 3693#else 3694 (void) zvol_remove_minor(zc->zc_name); 3695#endif 3696 return (err); 3697} 3698 3699/* 3700 * fsname is name of dataset to rollback (to most recent snapshot) 3701 * 3702 * innvl is not used. 3703 * 3704 * outnvl: "target" -> name of most recent snapshot 3705 * } 3706 */ 3707/* ARGSUSED */ 3708static int 3709zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl) 3710{ 3711 zfsvfs_t *zfsvfs; 3712 int error; 3713 3714 if (getzfsvfs(fsname, &zfsvfs) == 0) { 3715 error = zfs_suspend_fs(zfsvfs); 3716 if (error == 0) { 3717 int resume_err; 3718 3719 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl); 3720 resume_err = zfs_resume_fs(zfsvfs, fsname); 3721 error = error ? error : resume_err; 3722 } 3723 VFS_RELE(zfsvfs->z_vfs); 3724 } else { 3725 error = dsl_dataset_rollback(fsname, NULL, outnvl); 3726 } 3727 return (error); 3728} 3729 3730static int 3731recursive_unmount(const char *fsname, void *arg) 3732{ 3733 const char *snapname = arg; 3734 char fullname[MAXNAMELEN]; 3735 3736 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname); 3737 return (zfs_unmount_snap(fullname)); 3738} 3739 3740/* 3741 * inputs: 3742 * zc_name old name of dataset 3743 * zc_value new name of dataset 3744 * zc_cookie recursive flag (only valid for snapshots) 3745 * 3746 * outputs: none 3747 */ 3748static int 3749zfs_ioc_rename(zfs_cmd_t *zc) 3750{ 3751 boolean_t recursive = zc->zc_cookie & 1; 3752 char *at; 3753 boolean_t allow_mounted = B_TRUE; 3754 3755#ifdef __FreeBSD__ 3756 allow_mounted = (zc->zc_cookie & 2) != 0; 3757#endif 3758 3759 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 3760 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3761 strchr(zc->zc_value, '%')) 3762 return (SET_ERROR(EINVAL)); 3763 3764 at = strchr(zc->zc_name, '@'); 3765 if (at != NULL) { 3766 /* snaps must be in same fs */ 3767 int error; 3768 3769 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1)) 3770 return (SET_ERROR(EXDEV)); 3771 *at = '\0'; 3772 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) { 3773 error = dmu_objset_find(zc->zc_name, 3774 recursive_unmount, at + 1, 3775 recursive ? DS_FIND_CHILDREN : 0); 3776 if (error != 0) { 3777 *at = '@'; 3778 return (error); 3779 } 3780 } 3781 error = dsl_dataset_rename_snapshot(zc->zc_name, 3782 at + 1, strchr(zc->zc_value, '@') + 1, recursive); 3783 *at = '@'; 3784 3785 return (error); 3786 } else { 3787#ifdef illumos 3788 if (zc->zc_objset_type == DMU_OST_ZVOL) 3789 (void) zvol_remove_minor(zc->zc_name); 3790#endif 3791 return (dsl_dir_rename(zc->zc_name, zc->zc_value)); 3792 } 3793} 3794 3795static int 3796zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 3797{ 3798 const char *propname = nvpair_name(pair); 3799 boolean_t issnap = (strchr(dsname, '@') != NULL); 3800 zfs_prop_t prop = zfs_name_to_prop(propname); 3801 uint64_t intval; 3802 int err; 3803 3804 if (prop == ZPROP_INVAL) { 3805 if (zfs_prop_user(propname)) { 3806 if (err = zfs_secpolicy_write_perms(dsname, 3807 ZFS_DELEG_PERM_USERPROP, cr)) 3808 return (err); 3809 return (0); 3810 } 3811 3812 if (!issnap && zfs_prop_userquota(propname)) { 3813 const char *perm = NULL; 3814 const char *uq_prefix = 3815 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 3816 const char *gq_prefix = 3817 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 3818 3819 if (strncmp(propname, uq_prefix, 3820 strlen(uq_prefix)) == 0) { 3821 perm = ZFS_DELEG_PERM_USERQUOTA; 3822 } else if (strncmp(propname, gq_prefix, 3823 strlen(gq_prefix)) == 0) { 3824 perm = ZFS_DELEG_PERM_GROUPQUOTA; 3825 } else { 3826 /* USERUSED and GROUPUSED are read-only */ 3827 return (SET_ERROR(EINVAL)); 3828 } 3829 3830 if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 3831 return (err); 3832 return (0); 3833 } 3834 3835 return (SET_ERROR(EINVAL)); 3836 } 3837 3838 if (issnap) 3839 return (SET_ERROR(EINVAL)); 3840 3841 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 3842 /* 3843 * dsl_prop_get_all_impl() returns properties in this 3844 * format. 3845 */ 3846 nvlist_t *attrs; 3847 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 3848 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 3849 &pair) == 0); 3850 } 3851 3852 /* 3853 * Check that this value is valid for this pool version 3854 */ 3855 switch (prop) { 3856 case ZFS_PROP_COMPRESSION: 3857 /* 3858 * If the user specified gzip compression, make sure 3859 * the SPA supports it. We ignore any errors here since 3860 * we'll catch them later. 3861 */ 3862 if (nvpair_value_uint64(pair, &intval) == 0) { 3863 if (intval >= ZIO_COMPRESS_GZIP_1 && 3864 intval <= ZIO_COMPRESS_GZIP_9 && 3865 zfs_earlier_version(dsname, 3866 SPA_VERSION_GZIP_COMPRESSION)) { 3867 return (SET_ERROR(ENOTSUP)); 3868 } 3869 3870 if (intval == ZIO_COMPRESS_ZLE && 3871 zfs_earlier_version(dsname, 3872 SPA_VERSION_ZLE_COMPRESSION)) 3873 return (SET_ERROR(ENOTSUP)); 3874 3875 if (intval == ZIO_COMPRESS_LZ4) { 3876 spa_t *spa; 3877 3878 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3879 return (err); 3880 3881 if (!spa_feature_is_enabled(spa, 3882 SPA_FEATURE_LZ4_COMPRESS)) { 3883 spa_close(spa, FTAG); 3884 return (SET_ERROR(ENOTSUP)); 3885 } 3886 spa_close(spa, FTAG); 3887 } 3888 3889 /* 3890 * If this is a bootable dataset then 3891 * verify that the compression algorithm 3892 * is supported for booting. We must return 3893 * something other than ENOTSUP since it 3894 * implies a downrev pool version. 3895 */ 3896 if (zfs_is_bootfs(dsname) && 3897 !BOOTFS_COMPRESS_VALID(intval)) { 3898 return (SET_ERROR(ERANGE)); 3899 } 3900 } 3901 break; 3902 3903 case ZFS_PROP_COPIES: 3904 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 3905 return (SET_ERROR(ENOTSUP)); 3906 break; 3907 3908 case ZFS_PROP_RECORDSIZE: 3909 /* Record sizes above 128k need the feature to be enabled */ 3910 if (nvpair_value_uint64(pair, &intval) == 0 && 3911 intval > SPA_OLD_MAXBLOCKSIZE) { 3912 spa_t *spa; 3913 3914 /* 3915 * If this is a bootable dataset then 3916 * the we don't allow large (>128K) blocks, 3917 * because GRUB doesn't support them. 3918 */ 3919 if (zfs_is_bootfs(dsname) && 3920 intval > SPA_OLD_MAXBLOCKSIZE) { 3921 return (SET_ERROR(ERANGE)); 3922 } 3923 3924 /* 3925 * We don't allow setting the property above 1MB, 3926 * unless the tunable has been changed. 3927 */ 3928 if (intval > zfs_max_recordsize || 3929 intval > SPA_MAXBLOCKSIZE) 3930 return (SET_ERROR(ERANGE)); 3931 3932 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3933 return (err); 3934 3935 if (!spa_feature_is_enabled(spa, 3936 SPA_FEATURE_LARGE_BLOCKS)) { 3937 spa_close(spa, FTAG); 3938 return (SET_ERROR(ENOTSUP)); 3939 } 3940 spa_close(spa, FTAG); 3941 } 3942 break; 3943 3944 case ZFS_PROP_SHARESMB: 3945 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 3946 return (SET_ERROR(ENOTSUP)); 3947 break; 3948 3949 case ZFS_PROP_ACLINHERIT: 3950 if (nvpair_type(pair) == DATA_TYPE_UINT64 && 3951 nvpair_value_uint64(pair, &intval) == 0) { 3952 if (intval == ZFS_ACL_PASSTHROUGH_X && 3953 zfs_earlier_version(dsname, 3954 SPA_VERSION_PASSTHROUGH_X)) 3955 return (SET_ERROR(ENOTSUP)); 3956 } 3957 break; 3958 3959 case ZFS_PROP_CHECKSUM: 3960 case ZFS_PROP_DEDUP: 3961 { 3962 spa_feature_t feature; 3963 spa_t *spa; 3964 3965 /* dedup feature version checks */ 3966 if (prop == ZFS_PROP_DEDUP && 3967 zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 3968 return (SET_ERROR(ENOTSUP)); 3969 3970 if (nvpair_value_uint64(pair, &intval) != 0) 3971 return (SET_ERROR(EINVAL)); 3972 3973 /* check prop value is enabled in features */ 3974 feature = zio_checksum_to_feature(intval); 3975 if (feature == SPA_FEATURE_NONE) 3976 break; 3977 3978 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3979 return (err); 3980 /* 3981 * Salted checksums are not supported on root pools. 3982 */ 3983 if (spa_bootfs(spa) != 0 && 3984 intval < ZIO_CHECKSUM_FUNCTIONS && 3985 (zio_checksum_table[intval].ci_flags & 3986 ZCHECKSUM_FLAG_SALTED)) { 3987 spa_close(spa, FTAG); 3988 return (SET_ERROR(ERANGE)); 3989 } 3990 if (!spa_feature_is_enabled(spa, feature)) { 3991 spa_close(spa, FTAG); 3992 return (SET_ERROR(ENOTSUP)); 3993 } 3994 spa_close(spa, FTAG); 3995 break; 3996 } 3997 } 3998 3999 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 4000} 4001 4002/* 4003 * Checks for a race condition to make sure we don't increment a feature flag 4004 * multiple times. 4005 */ 4006static int 4007zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx) 4008{ 4009 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 4010 spa_feature_t *featurep = arg; 4011 4012 if (!spa_feature_is_active(spa, *featurep)) 4013 return (0); 4014 else 4015 return (SET_ERROR(EBUSY)); 4016} 4017 4018/* 4019 * The callback invoked on feature activation in the sync task caused by 4020 * zfs_prop_activate_feature. 4021 */ 4022static void 4023zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx) 4024{ 4025 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 4026 spa_feature_t *featurep = arg; 4027 4028 spa_feature_incr(spa, *featurep, tx); 4029} 4030 4031/* 4032 * Activates a feature on a pool in response to a property setting. This 4033 * creates a new sync task which modifies the pool to reflect the feature 4034 * as being active. 4035 */ 4036static int 4037zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature) 4038{ 4039 int err; 4040 4041 /* EBUSY here indicates that the feature is already active */ 4042 err = dsl_sync_task(spa_name(spa), 4043 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync, 4044 &feature, 2, ZFS_SPACE_CHECK_RESERVED); 4045 4046 if (err != 0 && err != EBUSY) 4047 return (err); 4048 else 4049 return (0); 4050} 4051 4052/* 4053 * Removes properties from the given props list that fail permission checks 4054 * needed to clear them and to restore them in case of a receive error. For each 4055 * property, make sure we have both set and inherit permissions. 4056 * 4057 * Returns the first error encountered if any permission checks fail. If the 4058 * caller provides a non-NULL errlist, it also gives the complete list of names 4059 * of all the properties that failed a permission check along with the 4060 * corresponding error numbers. The caller is responsible for freeing the 4061 * returned errlist. 4062 * 4063 * If every property checks out successfully, zero is returned and the list 4064 * pointed at by errlist is NULL. 4065 */ 4066static int 4067zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 4068{ 4069 zfs_cmd_t *zc; 4070 nvpair_t *pair, *next_pair; 4071 nvlist_t *errors; 4072 int err, rv = 0; 4073 4074 if (props == NULL) 4075 return (0); 4076 4077 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4078 4079 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 4080 (void) strcpy(zc->zc_name, dataset); 4081 pair = nvlist_next_nvpair(props, NULL); 4082 while (pair != NULL) { 4083 next_pair = nvlist_next_nvpair(props, pair); 4084 4085 (void) strcpy(zc->zc_value, nvpair_name(pair)); 4086 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 4087 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) { 4088 VERIFY(nvlist_remove_nvpair(props, pair) == 0); 4089 VERIFY(nvlist_add_int32(errors, 4090 zc->zc_value, err) == 0); 4091 } 4092 pair = next_pair; 4093 } 4094 kmem_free(zc, sizeof (zfs_cmd_t)); 4095 4096 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 4097 nvlist_free(errors); 4098 errors = NULL; 4099 } else { 4100 VERIFY(nvpair_value_int32(pair, &rv) == 0); 4101 } 4102 4103 if (errlist == NULL) 4104 nvlist_free(errors); 4105 else 4106 *errlist = errors; 4107 4108 return (rv); 4109} 4110 4111static boolean_t 4112propval_equals(nvpair_t *p1, nvpair_t *p2) 4113{ 4114 if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 4115 /* dsl_prop_get_all_impl() format */ 4116 nvlist_t *attrs; 4117 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 4118 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4119 &p1) == 0); 4120 } 4121 4122 if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 4123 nvlist_t *attrs; 4124 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 4125 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4126 &p2) == 0); 4127 } 4128 4129 if (nvpair_type(p1) != nvpair_type(p2)) 4130 return (B_FALSE); 4131 4132 if (nvpair_type(p1) == DATA_TYPE_STRING) { 4133 char *valstr1, *valstr2; 4134 4135 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 4136 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 4137 return (strcmp(valstr1, valstr2) == 0); 4138 } else { 4139 uint64_t intval1, intval2; 4140 4141 VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 4142 VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 4143 return (intval1 == intval2); 4144 } 4145} 4146 4147/* 4148 * Remove properties from props if they are not going to change (as determined 4149 * by comparison with origprops). Remove them from origprops as well, since we 4150 * do not need to clear or restore properties that won't change. 4151 */ 4152static void 4153props_reduce(nvlist_t *props, nvlist_t *origprops) 4154{ 4155 nvpair_t *pair, *next_pair; 4156 4157 if (origprops == NULL) 4158 return; /* all props need to be received */ 4159 4160 pair = nvlist_next_nvpair(props, NULL); 4161 while (pair != NULL) { 4162 const char *propname = nvpair_name(pair); 4163 nvpair_t *match; 4164 4165 next_pair = nvlist_next_nvpair(props, pair); 4166 4167 if ((nvlist_lookup_nvpair(origprops, propname, 4168 &match) != 0) || !propval_equals(pair, match)) 4169 goto next; /* need to set received value */ 4170 4171 /* don't clear the existing received value */ 4172 (void) nvlist_remove_nvpair(origprops, match); 4173 /* don't bother receiving the property */ 4174 (void) nvlist_remove_nvpair(props, pair); 4175next: 4176 pair = next_pair; 4177 } 4178} 4179 4180#ifdef DEBUG 4181static boolean_t zfs_ioc_recv_inject_err; 4182#endif 4183 4184/* 4185 * inputs: 4186 * zc_name name of containing filesystem 4187 * zc_nvlist_src{_size} nvlist of properties to apply 4188 * zc_value name of snapshot to create 4189 * zc_string name of clone origin (if DRR_FLAG_CLONE) 4190 * zc_cookie file descriptor to recv from 4191 * zc_begin_record the BEGIN record of the stream (not byteswapped) 4192 * zc_guid force flag 4193 * zc_cleanup_fd cleanup-on-exit file descriptor 4194 * zc_action_handle handle for this guid/ds mapping (or zero on first call) 4195 * zc_resumable if data is incomplete assume sender will resume 4196 * 4197 * outputs: 4198 * zc_cookie number of bytes read 4199 * zc_nvlist_dst{_size} error for each unapplied received property 4200 * zc_obj zprop_errflags_t 4201 * zc_action_handle handle for this guid/ds mapping 4202 */ 4203static int 4204zfs_ioc_recv(zfs_cmd_t *zc) 4205{ 4206 file_t *fp; 4207 dmu_recv_cookie_t drc; 4208 boolean_t force = (boolean_t)zc->zc_guid; 4209 int fd; 4210 int error = 0; 4211 int props_error = 0; 4212 nvlist_t *errors; 4213 offset_t off; 4214 nvlist_t *props = NULL; /* sent properties */ 4215 nvlist_t *origprops = NULL; /* existing properties */ 4216 char *origin = NULL; 4217 char *tosnap; 4218 char tofs[ZFS_MAXNAMELEN]; 4219 cap_rights_t rights; 4220 boolean_t first_recvd_props = B_FALSE; 4221 4222 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 4223 strchr(zc->zc_value, '@') == NULL || 4224 strchr(zc->zc_value, '%')) 4225 return (SET_ERROR(EINVAL)); 4226 4227 (void) strcpy(tofs, zc->zc_value); 4228 tosnap = strchr(tofs, '@'); 4229 *tosnap++ = '\0'; 4230 4231 if (zc->zc_nvlist_src != 0 && 4232 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 4233 zc->zc_iflags, &props)) != 0) 4234 return (error); 4235 4236 fd = zc->zc_cookie; 4237#ifdef illumos 4238 fp = getf(fd); 4239#else 4240 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp); 4241#endif 4242 if (fp == NULL) { 4243 nvlist_free(props); 4244 return (SET_ERROR(EBADF)); 4245 } 4246 4247 errors = fnvlist_alloc(); 4248 4249 if (zc->zc_string[0]) 4250 origin = zc->zc_string; 4251 4252 error = dmu_recv_begin(tofs, tosnap, 4253 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc); 4254 if (error != 0) 4255 goto out; 4256 4257 /* 4258 * Set properties before we receive the stream so that they are applied 4259 * to the new data. Note that we must call dmu_recv_stream() if 4260 * dmu_recv_begin() succeeds. 4261 */ 4262 if (props != NULL && !drc.drc_newfs) { 4263 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >= 4264 SPA_VERSION_RECVD_PROPS && 4265 !dsl_prop_get_hasrecvd(tofs)) 4266 first_recvd_props = B_TRUE; 4267 4268 /* 4269 * If new received properties are supplied, they are to 4270 * completely replace the existing received properties, so stash 4271 * away the existing ones. 4272 */ 4273 if (dsl_prop_get_received(tofs, &origprops) == 0) { 4274 nvlist_t *errlist = NULL; 4275 /* 4276 * Don't bother writing a property if its value won't 4277 * change (and avoid the unnecessary security checks). 4278 * 4279 * The first receive after SPA_VERSION_RECVD_PROPS is a 4280 * special case where we blow away all local properties 4281 * regardless. 4282 */ 4283 if (!first_recvd_props) 4284 props_reduce(props, origprops); 4285 if (zfs_check_clearable(tofs, origprops, &errlist) != 0) 4286 (void) nvlist_merge(errors, errlist, 0); 4287 nvlist_free(errlist); 4288 4289 if (clear_received_props(tofs, origprops, 4290 first_recvd_props ? NULL : props) != 0) 4291 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 4292 } else { 4293 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 4294 } 4295 } 4296 4297 if (props != NULL) { 4298 props_error = dsl_prop_set_hasrecvd(tofs); 4299 4300 if (props_error == 0) { 4301 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 4302 props, errors); 4303 } 4304 } 4305 4306 if (zc->zc_nvlist_dst_size != 0 && 4307 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 || 4308 put_nvlist(zc, errors) != 0)) { 4309 /* 4310 * Caller made zc->zc_nvlist_dst less than the minimum expected 4311 * size or supplied an invalid address. 4312 */ 4313 props_error = SET_ERROR(EINVAL); 4314 } 4315 4316 off = fp->f_offset; 4317 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd, 4318 &zc->zc_action_handle); 4319 4320 if (error == 0) { 4321 zfsvfs_t *zfsvfs = NULL; 4322 4323 if (getzfsvfs(tofs, &zfsvfs) == 0) { 4324 /* online recv */ 4325 int end_err; 4326 4327 error = zfs_suspend_fs(zfsvfs); 4328 /* 4329 * If the suspend fails, then the recv_end will 4330 * likely also fail, and clean up after itself. 4331 */ 4332 end_err = dmu_recv_end(&drc, zfsvfs); 4333 if (error == 0) 4334 error = zfs_resume_fs(zfsvfs, tofs); 4335 error = error ? error : end_err; 4336 VFS_RELE(zfsvfs->z_vfs); 4337 } else { 4338 error = dmu_recv_end(&drc, NULL); 4339 } 4340 } 4341 4342 zc->zc_cookie = off - fp->f_offset; 4343 if (off >= 0 && off <= MAXOFFSET_T) 4344 fp->f_offset = off; 4345 4346#ifdef DEBUG 4347 if (zfs_ioc_recv_inject_err) { 4348 zfs_ioc_recv_inject_err = B_FALSE; 4349 error = 1; 4350 } 4351#endif 4352 4353#ifdef __FreeBSD__ 4354 if (error == 0) 4355 zvol_create_minors(tofs); 4356#endif 4357 4358 /* 4359 * On error, restore the original props. 4360 */ 4361 if (error != 0 && props != NULL && !drc.drc_newfs) { 4362 if (clear_received_props(tofs, props, NULL) != 0) { 4363 /* 4364 * We failed to clear the received properties. 4365 * Since we may have left a $recvd value on the 4366 * system, we can't clear the $hasrecvd flag. 4367 */ 4368 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4369 } else if (first_recvd_props) { 4370 dsl_prop_unset_hasrecvd(tofs); 4371 } 4372 4373 if (origprops == NULL && !drc.drc_newfs) { 4374 /* We failed to stash the original properties. */ 4375 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4376 } 4377 4378 /* 4379 * dsl_props_set() will not convert RECEIVED to LOCAL on or 4380 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 4381 * explictly if we're restoring local properties cleared in the 4382 * first new-style receive. 4383 */ 4384 if (origprops != NULL && 4385 zfs_set_prop_nvlist(tofs, (first_recvd_props ? 4386 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 4387 origprops, NULL) != 0) { 4388 /* 4389 * We stashed the original properties but failed to 4390 * restore them. 4391 */ 4392 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4393 } 4394 } 4395out: 4396 nvlist_free(props); 4397 nvlist_free(origprops); 4398 nvlist_free(errors); 4399 releasef(fd); 4400 4401 if (error == 0) 4402 error = props_error; 4403 4404 return (error); 4405} 4406 4407/* 4408 * inputs: 4409 * zc_name name of snapshot to send 4410 * zc_cookie file descriptor to send stream to 4411 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 4412 * zc_sendobj objsetid of snapshot to send 4413 * zc_fromobj objsetid of incremental fromsnap (may be zero) 4414 * zc_guid if set, estimate size of stream only. zc_cookie is ignored. 4415 * output size in zc_objset_type. 4416 * zc_flags lzc_send_flags 4417 * 4418 * outputs: 4419 * zc_objset_type estimated size, if zc_guid is set 4420 */ 4421static int 4422zfs_ioc_send(zfs_cmd_t *zc) 4423{ 4424 int error; 4425 offset_t off; 4426 boolean_t estimate = (zc->zc_guid != 0); 4427 boolean_t embedok = (zc->zc_flags & 0x1); 4428 boolean_t large_block_ok = (zc->zc_flags & 0x2); 4429 4430 if (zc->zc_obj != 0) { 4431 dsl_pool_t *dp; 4432 dsl_dataset_t *tosnap; 4433 4434 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4435 if (error != 0) 4436 return (error); 4437 4438 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 4439 if (error != 0) { 4440 dsl_pool_rele(dp, FTAG); 4441 return (error); 4442 } 4443 4444 if (dsl_dir_is_clone(tosnap->ds_dir)) 4445 zc->zc_fromobj = 4446 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj; 4447 dsl_dataset_rele(tosnap, FTAG); 4448 dsl_pool_rele(dp, FTAG); 4449 } 4450 4451 if (estimate) { 4452 dsl_pool_t *dp; 4453 dsl_dataset_t *tosnap; 4454 dsl_dataset_t *fromsnap = NULL; 4455 4456 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4457 if (error != 0) 4458 return (error); 4459 4460 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 4461 if (error != 0) { 4462 dsl_pool_rele(dp, FTAG); 4463 return (error); 4464 } 4465 4466 if (zc->zc_fromobj != 0) { 4467 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, 4468 FTAG, &fromsnap); 4469 if (error != 0) { 4470 dsl_dataset_rele(tosnap, FTAG); 4471 dsl_pool_rele(dp, FTAG); 4472 return (error); 4473 } 4474 } 4475 4476 error = dmu_send_estimate(tosnap, fromsnap, 4477 &zc->zc_objset_type); 4478 4479 if (fromsnap != NULL) 4480 dsl_dataset_rele(fromsnap, FTAG); 4481 dsl_dataset_rele(tosnap, FTAG); 4482 dsl_pool_rele(dp, FTAG); 4483 } else { 4484 file_t *fp; 4485 cap_rights_t rights; 4486 4487#ifdef illumos 4488 fp = getf(zc->zc_cookie); 4489#else 4490 fget_write(curthread, zc->zc_cookie, 4491 cap_rights_init(&rights, CAP_WRITE), &fp); 4492#endif 4493 if (fp == NULL) 4494 return (SET_ERROR(EBADF)); 4495 4496 off = fp->f_offset; 4497 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj, 4498 zc->zc_fromobj, embedok, large_block_ok, 4499#ifdef illumos 4500 zc->zc_cookie, fp->f_vnode, &off); 4501#else 4502 zc->zc_cookie, fp, &off); 4503#endif 4504 4505 if (off >= 0 && off <= MAXOFFSET_T) 4506 fp->f_offset = off; 4507 releasef(zc->zc_cookie); 4508 } 4509 return (error); 4510} 4511 4512/* 4513 * inputs: 4514 * zc_name name of snapshot on which to report progress 4515 * zc_cookie file descriptor of send stream 4516 * 4517 * outputs: 4518 * zc_cookie number of bytes written in send stream thus far 4519 */ 4520static int 4521zfs_ioc_send_progress(zfs_cmd_t *zc) 4522{ 4523 dsl_pool_t *dp; 4524 dsl_dataset_t *ds; 4525 dmu_sendarg_t *dsp = NULL; 4526 int error; 4527 4528 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4529 if (error != 0) 4530 return (error); 4531 4532 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); 4533 if (error != 0) { 4534 dsl_pool_rele(dp, FTAG); 4535 return (error); 4536 } 4537 4538 mutex_enter(&ds->ds_sendstream_lock); 4539 4540 /* 4541 * Iterate over all the send streams currently active on this dataset. 4542 * If there's one which matches the specified file descriptor _and_ the 4543 * stream was started by the current process, return the progress of 4544 * that stream. 4545 */ 4546 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; 4547 dsp = list_next(&ds->ds_sendstreams, dsp)) { 4548 if (dsp->dsa_outfd == zc->zc_cookie && 4549 dsp->dsa_proc == curproc) 4550 break; 4551 } 4552 4553 if (dsp != NULL) 4554 zc->zc_cookie = *(dsp->dsa_off); 4555 else 4556 error = SET_ERROR(ENOENT); 4557 4558 mutex_exit(&ds->ds_sendstream_lock); 4559 dsl_dataset_rele(ds, FTAG); 4560 dsl_pool_rele(dp, FTAG); 4561 return (error); 4562} 4563 4564static int 4565zfs_ioc_inject_fault(zfs_cmd_t *zc) 4566{ 4567 int id, error; 4568 4569 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 4570 &zc->zc_inject_record); 4571 4572 if (error == 0) 4573 zc->zc_guid = (uint64_t)id; 4574 4575 return (error); 4576} 4577 4578static int 4579zfs_ioc_clear_fault(zfs_cmd_t *zc) 4580{ 4581 return (zio_clear_fault((int)zc->zc_guid)); 4582} 4583 4584static int 4585zfs_ioc_inject_list_next(zfs_cmd_t *zc) 4586{ 4587 int id = (int)zc->zc_guid; 4588 int error; 4589 4590 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 4591 &zc->zc_inject_record); 4592 4593 zc->zc_guid = id; 4594 4595 return (error); 4596} 4597 4598static int 4599zfs_ioc_error_log(zfs_cmd_t *zc) 4600{ 4601 spa_t *spa; 4602 int error; 4603 size_t count = (size_t)zc->zc_nvlist_dst_size; 4604 4605 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 4606 return (error); 4607 4608 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 4609 &count); 4610 if (error == 0) 4611 zc->zc_nvlist_dst_size = count; 4612 else 4613 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 4614 4615 spa_close(spa, FTAG); 4616 4617 return (error); 4618} 4619 4620static int 4621zfs_ioc_clear(zfs_cmd_t *zc) 4622{ 4623 spa_t *spa; 4624 vdev_t *vd; 4625 int error; 4626 4627 /* 4628 * On zpool clear we also fix up missing slogs 4629 */ 4630 mutex_enter(&spa_namespace_lock); 4631 spa = spa_lookup(zc->zc_name); 4632 if (spa == NULL) { 4633 mutex_exit(&spa_namespace_lock); 4634 return (SET_ERROR(EIO)); 4635 } 4636 if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 4637 /* we need to let spa_open/spa_load clear the chains */ 4638 spa_set_log_state(spa, SPA_LOG_CLEAR); 4639 } 4640 spa->spa_last_open_failed = 0; 4641 mutex_exit(&spa_namespace_lock); 4642 4643 if (zc->zc_cookie & ZPOOL_NO_REWIND) { 4644 error = spa_open(zc->zc_name, &spa, FTAG); 4645 } else { 4646 nvlist_t *policy; 4647 nvlist_t *config = NULL; 4648 4649 if (zc->zc_nvlist_src == 0) 4650 return (SET_ERROR(EINVAL)); 4651 4652 if ((error = get_nvlist(zc->zc_nvlist_src, 4653 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 4654 error = spa_open_rewind(zc->zc_name, &spa, FTAG, 4655 policy, &config); 4656 if (config != NULL) { 4657 int err; 4658 4659 if ((err = put_nvlist(zc, config)) != 0) 4660 error = err; 4661 nvlist_free(config); 4662 } 4663 nvlist_free(policy); 4664 } 4665 } 4666 4667 if (error != 0) 4668 return (error); 4669 4670 spa_vdev_state_enter(spa, SCL_NONE); 4671 4672 if (zc->zc_guid == 0) { 4673 vd = NULL; 4674 } else { 4675 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 4676 if (vd == NULL) { 4677 (void) spa_vdev_state_exit(spa, NULL, ENODEV); 4678 spa_close(spa, FTAG); 4679 return (SET_ERROR(ENODEV)); 4680 } 4681 } 4682 4683 vdev_clear(spa, vd); 4684 4685 (void) spa_vdev_state_exit(spa, NULL, 0); 4686 4687 /* 4688 * Resume any suspended I/Os. 4689 */ 4690 if (zio_resume(spa) != 0) 4691 error = SET_ERROR(EIO); 4692 4693 spa_close(spa, FTAG); 4694 4695 return (error); 4696} 4697 4698static int 4699zfs_ioc_pool_reopen(zfs_cmd_t *zc) 4700{ 4701 spa_t *spa; 4702 int error; 4703 4704 error = spa_open(zc->zc_name, &spa, FTAG); 4705 if (error != 0) 4706 return (error); 4707 4708 spa_vdev_state_enter(spa, SCL_NONE); 4709 4710 /* 4711 * If a resilver is already in progress then set the 4712 * spa_scrub_reopen flag to B_TRUE so that we don't restart 4713 * the scan as a side effect of the reopen. Otherwise, let 4714 * vdev_open() decided if a resilver is required. 4715 */ 4716 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool); 4717 vdev_reopen(spa->spa_root_vdev); 4718 spa->spa_scrub_reopen = B_FALSE; 4719 4720 (void) spa_vdev_state_exit(spa, NULL, 0); 4721 spa_close(spa, FTAG); 4722 return (0); 4723} 4724/* 4725 * inputs: 4726 * zc_name name of filesystem 4727 * zc_value name of origin snapshot 4728 * 4729 * outputs: 4730 * zc_string name of conflicting snapshot, if there is one 4731 */ 4732static int 4733zfs_ioc_promote(zfs_cmd_t *zc) 4734{ 4735 char *cp; 4736 4737 /* 4738 * We don't need to unmount *all* the origin fs's snapshots, but 4739 * it's easier. 4740 */ 4741 cp = strchr(zc->zc_value, '@'); 4742 if (cp) 4743 *cp = '\0'; 4744 (void) dmu_objset_find(zc->zc_value, 4745 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS); 4746 return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 4747} 4748 4749/* 4750 * Retrieve a single {user|group}{used|quota}@... property. 4751 * 4752 * inputs: 4753 * zc_name name of filesystem 4754 * zc_objset_type zfs_userquota_prop_t 4755 * zc_value domain name (eg. "S-1-234-567-89") 4756 * zc_guid RID/UID/GID 4757 * 4758 * outputs: 4759 * zc_cookie property value 4760 */ 4761static int 4762zfs_ioc_userspace_one(zfs_cmd_t *zc) 4763{ 4764 zfsvfs_t *zfsvfs; 4765 int error; 4766 4767 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 4768 return (SET_ERROR(EINVAL)); 4769 4770 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4771 if (error != 0) 4772 return (error); 4773 4774 error = zfs_userspace_one(zfsvfs, 4775 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 4776 zfsvfs_rele(zfsvfs, FTAG); 4777 4778 return (error); 4779} 4780 4781/* 4782 * inputs: 4783 * zc_name name of filesystem 4784 * zc_cookie zap cursor 4785 * zc_objset_type zfs_userquota_prop_t 4786 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 4787 * 4788 * outputs: 4789 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 4790 * zc_cookie zap cursor 4791 */ 4792static int 4793zfs_ioc_userspace_many(zfs_cmd_t *zc) 4794{ 4795 zfsvfs_t *zfsvfs; 4796 int bufsize = zc->zc_nvlist_dst_size; 4797 4798 if (bufsize <= 0) 4799 return (SET_ERROR(ENOMEM)); 4800 4801 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4802 if (error != 0) 4803 return (error); 4804 4805 void *buf = kmem_alloc(bufsize, KM_SLEEP); 4806 4807 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 4808 buf, &zc->zc_nvlist_dst_size); 4809 4810 if (error == 0) { 4811 error = ddi_copyout(buf, 4812 (void *)(uintptr_t)zc->zc_nvlist_dst, 4813 zc->zc_nvlist_dst_size, zc->zc_iflags); 4814 } 4815 kmem_free(buf, bufsize); 4816 zfsvfs_rele(zfsvfs, FTAG); 4817 4818 return (error); 4819} 4820 4821/* 4822 * inputs: 4823 * zc_name name of filesystem 4824 * 4825 * outputs: 4826 * none 4827 */ 4828static int 4829zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 4830{ 4831 objset_t *os; 4832 int error = 0; 4833 zfsvfs_t *zfsvfs; 4834 4835 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 4836 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 4837 /* 4838 * If userused is not enabled, it may be because the 4839 * objset needs to be closed & reopened (to grow the 4840 * objset_phys_t). Suspend/resume the fs will do that. 4841 */ 4842 error = zfs_suspend_fs(zfsvfs); 4843 if (error == 0) { 4844 dmu_objset_refresh_ownership(zfsvfs->z_os, 4845 zfsvfs); 4846 error = zfs_resume_fs(zfsvfs, zc->zc_name); 4847 } 4848 } 4849 if (error == 0) 4850 error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 4851 VFS_RELE(zfsvfs->z_vfs); 4852 } else { 4853 /* XXX kind of reading contents without owning */ 4854 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 4855 if (error != 0) 4856 return (error); 4857 4858 error = dmu_objset_userspace_upgrade(os); 4859 dmu_objset_rele(os, FTAG); 4860 } 4861 4862 return (error); 4863} 4864 4865#ifdef illumos 4866/* 4867 * We don't want to have a hard dependency 4868 * against some special symbols in sharefs 4869 * nfs, and smbsrv. Determine them if needed when 4870 * the first file system is shared. 4871 * Neither sharefs, nfs or smbsrv are unloadable modules. 4872 */ 4873int (*znfsexport_fs)(void *arg); 4874int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 4875int (*zsmbexport_fs)(void *arg, boolean_t add_share); 4876 4877int zfs_nfsshare_inited; 4878int zfs_smbshare_inited; 4879 4880ddi_modhandle_t nfs_mod; 4881ddi_modhandle_t sharefs_mod; 4882ddi_modhandle_t smbsrv_mod; 4883#endif /* illumos */ 4884kmutex_t zfs_share_lock; 4885 4886#ifdef illumos 4887static int 4888zfs_init_sharefs() 4889{ 4890 int error; 4891 4892 ASSERT(MUTEX_HELD(&zfs_share_lock)); 4893 /* Both NFS and SMB shares also require sharetab support. */ 4894 if (sharefs_mod == NULL && ((sharefs_mod = 4895 ddi_modopen("fs/sharefs", 4896 KRTLD_MODE_FIRST, &error)) == NULL)) { 4897 return (SET_ERROR(ENOSYS)); 4898 } 4899 if (zshare_fs == NULL && ((zshare_fs = 4900 (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 4901 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 4902 return (SET_ERROR(ENOSYS)); 4903 } 4904 return (0); 4905} 4906#endif /* illumos */ 4907 4908static int 4909zfs_ioc_share(zfs_cmd_t *zc) 4910{ 4911#ifdef illumos 4912 int error; 4913 int opcode; 4914 4915 switch (zc->zc_share.z_sharetype) { 4916 case ZFS_SHARE_NFS: 4917 case ZFS_UNSHARE_NFS: 4918 if (zfs_nfsshare_inited == 0) { 4919 mutex_enter(&zfs_share_lock); 4920 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 4921 KRTLD_MODE_FIRST, &error)) == NULL)) { 4922 mutex_exit(&zfs_share_lock); 4923 return (SET_ERROR(ENOSYS)); 4924 } 4925 if (znfsexport_fs == NULL && 4926 ((znfsexport_fs = (int (*)(void *)) 4927 ddi_modsym(nfs_mod, 4928 "nfs_export", &error)) == NULL)) { 4929 mutex_exit(&zfs_share_lock); 4930 return (SET_ERROR(ENOSYS)); 4931 } 4932 error = zfs_init_sharefs(); 4933 if (error != 0) { 4934 mutex_exit(&zfs_share_lock); 4935 return (SET_ERROR(ENOSYS)); 4936 } 4937 zfs_nfsshare_inited = 1; 4938 mutex_exit(&zfs_share_lock); 4939 } 4940 break; 4941 case ZFS_SHARE_SMB: 4942 case ZFS_UNSHARE_SMB: 4943 if (zfs_smbshare_inited == 0) { 4944 mutex_enter(&zfs_share_lock); 4945 if (smbsrv_mod == NULL && ((smbsrv_mod = 4946 ddi_modopen("drv/smbsrv", 4947 KRTLD_MODE_FIRST, &error)) == NULL)) { 4948 mutex_exit(&zfs_share_lock); 4949 return (SET_ERROR(ENOSYS)); 4950 } 4951 if (zsmbexport_fs == NULL && ((zsmbexport_fs = 4952 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 4953 "smb_server_share", &error)) == NULL)) { 4954 mutex_exit(&zfs_share_lock); 4955 return (SET_ERROR(ENOSYS)); 4956 } 4957 error = zfs_init_sharefs(); 4958 if (error != 0) { 4959 mutex_exit(&zfs_share_lock); 4960 return (SET_ERROR(ENOSYS)); 4961 } 4962 zfs_smbshare_inited = 1; 4963 mutex_exit(&zfs_share_lock); 4964 } 4965 break; 4966 default: 4967 return (SET_ERROR(EINVAL)); 4968 } 4969 4970 switch (zc->zc_share.z_sharetype) { 4971 case ZFS_SHARE_NFS: 4972 case ZFS_UNSHARE_NFS: 4973 if (error = 4974 znfsexport_fs((void *) 4975 (uintptr_t)zc->zc_share.z_exportdata)) 4976 return (error); 4977 break; 4978 case ZFS_SHARE_SMB: 4979 case ZFS_UNSHARE_SMB: 4980 if (error = zsmbexport_fs((void *) 4981 (uintptr_t)zc->zc_share.z_exportdata, 4982 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 4983 B_TRUE: B_FALSE)) { 4984 return (error); 4985 } 4986 break; 4987 } 4988 4989 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 4990 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 4991 SHAREFS_ADD : SHAREFS_REMOVE; 4992 4993 /* 4994 * Add or remove share from sharetab 4995 */ 4996 error = zshare_fs(opcode, 4997 (void *)(uintptr_t)zc->zc_share.z_sharedata, 4998 zc->zc_share.z_sharemax); 4999 5000 return (error); 5001 5002#else /* !illumos */ 5003 return (ENOSYS); 5004#endif /* illumos */ 5005} 5006 5007ace_t full_access[] = { 5008 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 5009}; 5010 5011/* 5012 * inputs: 5013 * zc_name name of containing filesystem 5014 * zc_obj object # beyond which we want next in-use object # 5015 * 5016 * outputs: 5017 * zc_obj next in-use object # 5018 */ 5019static int 5020zfs_ioc_next_obj(zfs_cmd_t *zc) 5021{ 5022 objset_t *os = NULL; 5023 int error; 5024 5025 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 5026 if (error != 0) 5027 return (error); 5028 5029 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 5030 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg); 5031 5032 dmu_objset_rele(os, FTAG); 5033 return (error); 5034} 5035 5036/* 5037 * inputs: 5038 * zc_name name of filesystem 5039 * zc_value prefix name for snapshot 5040 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 5041 * 5042 * outputs: 5043 * zc_value short name of new snapshot 5044 */ 5045static int 5046zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 5047{ 5048 char *snap_name; 5049 char *hold_name; 5050 int error; 5051 minor_t minor; 5052 5053 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 5054 if (error != 0) 5055 return (error); 5056 5057 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 5058 (u_longlong_t)ddi_get_lbolt64()); 5059 hold_name = kmem_asprintf("%%%s", zc->zc_value); 5060 5061 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor, 5062 hold_name); 5063 if (error == 0) 5064 (void) strcpy(zc->zc_value, snap_name); 5065 strfree(snap_name); 5066 strfree(hold_name); 5067 zfs_onexit_fd_rele(zc->zc_cleanup_fd); 5068 return (error); 5069} 5070 5071/* 5072 * inputs: 5073 * zc_name name of "to" snapshot 5074 * zc_value name of "from" snapshot 5075 * zc_cookie file descriptor to write diff data on 5076 * 5077 * outputs: 5078 * dmu_diff_record_t's to the file descriptor 5079 */ 5080static int 5081zfs_ioc_diff(zfs_cmd_t *zc) 5082{ 5083 file_t *fp; 5084 cap_rights_t rights; 5085 offset_t off; 5086 int error; 5087 5088#ifdef illumos 5089 fp = getf(zc->zc_cookie); 5090#else 5091 fget_write(curthread, zc->zc_cookie, 5092 cap_rights_init(&rights, CAP_WRITE), &fp); 5093#endif 5094 if (fp == NULL) 5095 return (SET_ERROR(EBADF)); 5096 5097 off = fp->f_offset; 5098 5099#ifdef illumos 5100 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off); 5101#else 5102 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off); 5103#endif 5104 5105 if (off >= 0 && off <= MAXOFFSET_T) 5106 fp->f_offset = off; 5107 releasef(zc->zc_cookie); 5108 5109 return (error); 5110} 5111 5112#ifdef illumos 5113/* 5114 * Remove all ACL files in shares dir 5115 */ 5116static int 5117zfs_smb_acl_purge(znode_t *dzp) 5118{ 5119 zap_cursor_t zc; 5120 zap_attribute_t zap; 5121 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 5122 int error; 5123 5124 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 5125 (error = zap_cursor_retrieve(&zc, &zap)) == 0; 5126 zap_cursor_advance(&zc)) { 5127 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 5128 NULL, 0)) != 0) 5129 break; 5130 } 5131 zap_cursor_fini(&zc); 5132 return (error); 5133} 5134#endif /* illumos */ 5135 5136static int 5137zfs_ioc_smb_acl(zfs_cmd_t *zc) 5138{ 5139#ifdef illumos 5140 vnode_t *vp; 5141 znode_t *dzp; 5142 vnode_t *resourcevp = NULL; 5143 znode_t *sharedir; 5144 zfsvfs_t *zfsvfs; 5145 nvlist_t *nvlist; 5146 char *src, *target; 5147 vattr_t vattr; 5148 vsecattr_t vsec; 5149 int error = 0; 5150 5151 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 5152 NO_FOLLOW, NULL, &vp)) != 0) 5153 return (error); 5154 5155 /* Now make sure mntpnt and dataset are ZFS */ 5156 5157 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 || 5158 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 5159 zc->zc_name) != 0)) { 5160 VN_RELE(vp); 5161 return (SET_ERROR(EINVAL)); 5162 } 5163 5164 dzp = VTOZ(vp); 5165 zfsvfs = dzp->z_zfsvfs; 5166 ZFS_ENTER(zfsvfs); 5167 5168 /* 5169 * Create share dir if its missing. 5170 */ 5171 mutex_enter(&zfsvfs->z_lock); 5172 if (zfsvfs->z_shares_dir == 0) { 5173 dmu_tx_t *tx; 5174 5175 tx = dmu_tx_create(zfsvfs->z_os); 5176 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 5177 ZFS_SHARES_DIR); 5178 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 5179 error = dmu_tx_assign(tx, TXG_WAIT); 5180 if (error != 0) { 5181 dmu_tx_abort(tx); 5182 } else { 5183 error = zfs_create_share_dir(zfsvfs, tx); 5184 dmu_tx_commit(tx); 5185 } 5186 if (error != 0) { 5187 mutex_exit(&zfsvfs->z_lock); 5188 VN_RELE(vp); 5189 ZFS_EXIT(zfsvfs); 5190 return (error); 5191 } 5192 } 5193 mutex_exit(&zfsvfs->z_lock); 5194 5195 ASSERT(zfsvfs->z_shares_dir); 5196 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 5197 VN_RELE(vp); 5198 ZFS_EXIT(zfsvfs); 5199 return (error); 5200 } 5201 5202 switch (zc->zc_cookie) { 5203 case ZFS_SMB_ACL_ADD: 5204 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 5205 vattr.va_type = VREG; 5206 vattr.va_mode = S_IFREG|0777; 5207 vattr.va_uid = 0; 5208 vattr.va_gid = 0; 5209 5210 vsec.vsa_mask = VSA_ACE; 5211 vsec.vsa_aclentp = &full_access; 5212 vsec.vsa_aclentsz = sizeof (full_access); 5213 vsec.vsa_aclcnt = 1; 5214 5215 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 5216 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 5217 if (resourcevp) 5218 VN_RELE(resourcevp); 5219 break; 5220 5221 case ZFS_SMB_ACL_REMOVE: 5222 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 5223 NULL, 0); 5224 break; 5225 5226 case ZFS_SMB_ACL_RENAME: 5227 if ((error = get_nvlist(zc->zc_nvlist_src, 5228 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 5229 VN_RELE(vp); 5230 VN_RELE(ZTOV(sharedir)); 5231 ZFS_EXIT(zfsvfs); 5232 return (error); 5233 } 5234 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 5235 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 5236 &target)) { 5237 VN_RELE(vp); 5238 VN_RELE(ZTOV(sharedir)); 5239 ZFS_EXIT(zfsvfs); 5240 nvlist_free(nvlist); 5241 return (error); 5242 } 5243 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 5244 kcred, NULL, 0); 5245 nvlist_free(nvlist); 5246 break; 5247 5248 case ZFS_SMB_ACL_PURGE: 5249 error = zfs_smb_acl_purge(sharedir); 5250 break; 5251 5252 default: 5253 error = SET_ERROR(EINVAL); 5254 break; 5255 } 5256 5257 VN_RELE(vp); 5258 VN_RELE(ZTOV(sharedir)); 5259 5260 ZFS_EXIT(zfsvfs); 5261 5262 return (error); 5263#else /* !illumos */ 5264 return (EOPNOTSUPP); 5265#endif /* illumos */ 5266} 5267 5268/* 5269 * innvl: { 5270 * "holds" -> { snapname -> holdname (string), ... } 5271 * (optional) "cleanup_fd" -> fd (int32) 5272 * } 5273 * 5274 * outnvl: { 5275 * snapname -> error value (int32) 5276 * ... 5277 * } 5278 */ 5279/* ARGSUSED */ 5280static int 5281zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist) 5282{ 5283 nvpair_t *pair; 5284 nvlist_t *holds; 5285 int cleanup_fd = -1; 5286 int error; 5287 minor_t minor = 0; 5288 5289 error = nvlist_lookup_nvlist(args, "holds", &holds); 5290 if (error != 0) 5291 return (SET_ERROR(EINVAL)); 5292 5293 /* make sure the user didn't pass us any invalid (empty) tags */ 5294 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 5295 pair = nvlist_next_nvpair(holds, pair)) { 5296 char *htag; 5297 5298 error = nvpair_value_string(pair, &htag); 5299 if (error != 0) 5300 return (SET_ERROR(error)); 5301 5302 if (strlen(htag) == 0) 5303 return (SET_ERROR(EINVAL)); 5304 } 5305 5306 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) { 5307 error = zfs_onexit_fd_hold(cleanup_fd, &minor); 5308 if (error != 0) 5309 return (error); 5310 } 5311 5312 error = dsl_dataset_user_hold(holds, minor, errlist); 5313 if (minor != 0) 5314 zfs_onexit_fd_rele(cleanup_fd); 5315 return (error); 5316} 5317 5318/* 5319 * innvl is not used. 5320 * 5321 * outnvl: { 5322 * holdname -> time added (uint64 seconds since epoch) 5323 * ... 5324 * } 5325 */ 5326/* ARGSUSED */ 5327static int 5328zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl) 5329{ 5330 return (dsl_dataset_get_holds(snapname, outnvl)); 5331} 5332 5333/* 5334 * innvl: { 5335 * snapname -> { holdname, ... } 5336 * ... 5337 * } 5338 * 5339 * outnvl: { 5340 * snapname -> error value (int32) 5341 * ... 5342 * } 5343 */ 5344/* ARGSUSED */ 5345static int 5346zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist) 5347{ 5348 return (dsl_dataset_user_release(holds, errlist)); 5349} 5350 5351/* 5352 * inputs: 5353 * zc_name name of new filesystem or snapshot 5354 * zc_value full name of old snapshot 5355 * 5356 * outputs: 5357 * zc_cookie space in bytes 5358 * zc_objset_type compressed space in bytes 5359 * zc_perm_action uncompressed space in bytes 5360 */ 5361static int 5362zfs_ioc_space_written(zfs_cmd_t *zc) 5363{ 5364 int error; 5365 dsl_pool_t *dp; 5366 dsl_dataset_t *new, *old; 5367 5368 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 5369 if (error != 0) 5370 return (error); 5371 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new); 5372 if (error != 0) { 5373 dsl_pool_rele(dp, FTAG); 5374 return (error); 5375 } 5376 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old); 5377 if (error != 0) { 5378 dsl_dataset_rele(new, FTAG); 5379 dsl_pool_rele(dp, FTAG); 5380 return (error); 5381 } 5382 5383 error = dsl_dataset_space_written(old, new, &zc->zc_cookie, 5384 &zc->zc_objset_type, &zc->zc_perm_action); 5385 dsl_dataset_rele(old, FTAG); 5386 dsl_dataset_rele(new, FTAG); 5387 dsl_pool_rele(dp, FTAG); 5388 return (error); 5389} 5390 5391/* 5392 * innvl: { 5393 * "firstsnap" -> snapshot name 5394 * } 5395 * 5396 * outnvl: { 5397 * "used" -> space in bytes 5398 * "compressed" -> compressed space in bytes 5399 * "uncompressed" -> uncompressed space in bytes 5400 * } 5401 */ 5402static int 5403zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl) 5404{ 5405 int error; 5406 dsl_pool_t *dp; 5407 dsl_dataset_t *new, *old; 5408 char *firstsnap; 5409 uint64_t used, comp, uncomp; 5410 5411 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0) 5412 return (SET_ERROR(EINVAL)); 5413 5414 error = dsl_pool_hold(lastsnap, FTAG, &dp); 5415 if (error != 0) 5416 return (error); 5417 5418 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new); 5419 if (error == 0 && !new->ds_is_snapshot) { 5420 dsl_dataset_rele(new, FTAG); 5421 error = SET_ERROR(EINVAL); 5422 } 5423 if (error != 0) { 5424 dsl_pool_rele(dp, FTAG); 5425 return (error); 5426 } 5427 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old); 5428 if (error == 0 && !old->ds_is_snapshot) { 5429 dsl_dataset_rele(old, FTAG); 5430 error = SET_ERROR(EINVAL); 5431 } 5432 if (error != 0) { 5433 dsl_dataset_rele(new, FTAG); 5434 dsl_pool_rele(dp, FTAG); 5435 return (error); 5436 } 5437 5438 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp); 5439 dsl_dataset_rele(old, FTAG); 5440 dsl_dataset_rele(new, FTAG); 5441 dsl_pool_rele(dp, FTAG); 5442 fnvlist_add_uint64(outnvl, "used", used); 5443 fnvlist_add_uint64(outnvl, "compressed", comp); 5444 fnvlist_add_uint64(outnvl, "uncompressed", uncomp); 5445 return (error); 5446} 5447 5448static int 5449zfs_ioc_jail(zfs_cmd_t *zc) 5450{ 5451 5452 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name, 5453 (int)zc->zc_jailid)); 5454} 5455 5456static int 5457zfs_ioc_unjail(zfs_cmd_t *zc) 5458{ 5459 5460 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name, 5461 (int)zc->zc_jailid)); 5462} 5463 5464/* 5465 * innvl: { 5466 * "fd" -> file descriptor to write stream to (int32) 5467 * (optional) "fromsnap" -> full snap name to send an incremental from 5468 * (optional) "largeblockok" -> (value ignored) 5469 * indicates that blocks > 128KB are permitted 5470 * (optional) "embedok" -> (value ignored) 5471 * presence indicates DRR_WRITE_EMBEDDED records are permitted 5472 * (optional) "resume_object" and "resume_offset" -> (uint64) 5473 * if present, resume send stream from specified object and offset. 5474 * } 5475 * 5476 * outnvl is unused 5477 */ 5478/* ARGSUSED */ 5479static int 5480zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 5481{ 5482 cap_rights_t rights; 5483 file_t *fp; 5484 int error; 5485 offset_t off; 5486 char *fromname = NULL; 5487 int fd; 5488 boolean_t largeblockok; 5489 boolean_t embedok; 5490 uint64_t resumeobj = 0; 5491 uint64_t resumeoff = 0; 5492 5493 error = nvlist_lookup_int32(innvl, "fd", &fd); 5494 if (error != 0) 5495 return (SET_ERROR(EINVAL)); 5496 5497 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname); 5498 5499 largeblockok = nvlist_exists(innvl, "largeblockok"); 5500 embedok = nvlist_exists(innvl, "embedok"); 5501 5502 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj); 5503 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff); 5504 5505#ifdef illumos 5506 file_t *fp = getf(fd); 5507#else 5508 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp); 5509#endif 5510 if (fp == NULL) 5511 return (SET_ERROR(EBADF)); 5512 5513 off = fp->f_offset; 5514 error = dmu_send(snapname, fromname, embedok, largeblockok, fd, 5515#ifdef illumos 5516 resumeobj, resumeoff, fp->f_vnode, &off); 5517#else 5518 resumeobj, resumeoff, fp, &off); 5519#endif 5520 5521#ifdef illumos 5522 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 5523 fp->f_offset = off; 5524#else 5525 fp->f_offset = off; 5526#endif 5527 5528 releasef(fd); 5529 return (error); 5530} 5531 5532/* 5533 * Determine approximately how large a zfs send stream will be -- the number 5534 * of bytes that will be written to the fd supplied to zfs_ioc_send_new(). 5535 * 5536 * innvl: { 5537 * (optional) "from" -> full snap or bookmark name to send an incremental 5538 * from 5539 * } 5540 * 5541 * outnvl: { 5542 * "space" -> bytes of space (uint64) 5543 * } 5544 */ 5545static int 5546zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 5547{ 5548 dsl_pool_t *dp; 5549 dsl_dataset_t *tosnap; 5550 int error; 5551 char *fromname; 5552 uint64_t space; 5553 5554 error = dsl_pool_hold(snapname, FTAG, &dp); 5555 if (error != 0) 5556 return (error); 5557 5558 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap); 5559 if (error != 0) { 5560 dsl_pool_rele(dp, FTAG); 5561 return (error); 5562 } 5563 5564 error = nvlist_lookup_string(innvl, "from", &fromname); 5565 if (error == 0) { 5566 if (strchr(fromname, '@') != NULL) { 5567 /* 5568 * If from is a snapshot, hold it and use the more 5569 * efficient dmu_send_estimate to estimate send space 5570 * size using deadlists. 5571 */ 5572 dsl_dataset_t *fromsnap; 5573 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap); 5574 if (error != 0) 5575 goto out; 5576 error = dmu_send_estimate(tosnap, fromsnap, &space); 5577 dsl_dataset_rele(fromsnap, FTAG); 5578 } else if (strchr(fromname, '#') != NULL) { 5579 /* 5580 * If from is a bookmark, fetch the creation TXG of the 5581 * snapshot it was created from and use that to find 5582 * blocks that were born after it. 5583 */ 5584 zfs_bookmark_phys_t frombm; 5585 5586 error = dsl_bookmark_lookup(dp, fromname, tosnap, 5587 &frombm); 5588 if (error != 0) 5589 goto out; 5590 error = dmu_send_estimate_from_txg(tosnap, 5591 frombm.zbm_creation_txg, &space); 5592 } else { 5593 /* 5594 * from is not properly formatted as a snapshot or 5595 * bookmark 5596 */ 5597 error = SET_ERROR(EINVAL); 5598 goto out; 5599 } 5600 } else { 5601 // If estimating the size of a full send, use dmu_send_estimate 5602 error = dmu_send_estimate(tosnap, NULL, &space); 5603 } 5604 5605 fnvlist_add_uint64(outnvl, "space", space); 5606 5607out: 5608 dsl_dataset_rele(tosnap, FTAG); 5609 dsl_pool_rele(dp, FTAG); 5610 return (error); 5611} 5612 5613static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST]; 5614 5615static void 5616zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5617 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 5618 boolean_t log_history, zfs_ioc_poolcheck_t pool_check) 5619{ 5620 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 5621 5622 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 5623 ASSERT3U(ioc, <, ZFS_IOC_LAST); 5624 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 5625 ASSERT3P(vec->zvec_func, ==, NULL); 5626 5627 vec->zvec_legacy_func = func; 5628 vec->zvec_secpolicy = secpolicy; 5629 vec->zvec_namecheck = namecheck; 5630 vec->zvec_allow_log = log_history; 5631 vec->zvec_pool_check = pool_check; 5632} 5633 5634/* 5635 * See the block comment at the beginning of this file for details on 5636 * each argument to this function. 5637 */ 5638static void 5639zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func, 5640 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 5641 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist, 5642 boolean_t allow_log) 5643{ 5644 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 5645 5646 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 5647 ASSERT3U(ioc, <, ZFS_IOC_LAST); 5648 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 5649 ASSERT3P(vec->zvec_func, ==, NULL); 5650 5651 /* if we are logging, the name must be valid */ 5652 ASSERT(!allow_log || namecheck != NO_NAME); 5653 5654 vec->zvec_name = name; 5655 vec->zvec_func = func; 5656 vec->zvec_secpolicy = secpolicy; 5657 vec->zvec_namecheck = namecheck; 5658 vec->zvec_pool_check = pool_check; 5659 vec->zvec_smush_outnvlist = smush_outnvlist; 5660 vec->zvec_allow_log = allow_log; 5661} 5662 5663static void 5664zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5665 zfs_secpolicy_func_t *secpolicy, boolean_t log_history, 5666 zfs_ioc_poolcheck_t pool_check) 5667{ 5668 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5669 POOL_NAME, log_history, pool_check); 5670} 5671 5672static void 5673zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5674 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check) 5675{ 5676 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5677 DATASET_NAME, B_FALSE, pool_check); 5678} 5679 5680static void 5681zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 5682{ 5683 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config, 5684 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5685} 5686 5687static void 5688zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5689 zfs_secpolicy_func_t *secpolicy) 5690{ 5691 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5692 NO_NAME, B_FALSE, POOL_CHECK_NONE); 5693} 5694 5695static void 5696zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc, 5697 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy) 5698{ 5699 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5700 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED); 5701} 5702 5703static void 5704zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 5705{ 5706 zfs_ioctl_register_dataset_read_secpolicy(ioc, func, 5707 zfs_secpolicy_read); 5708} 5709 5710static void 5711zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5712 zfs_secpolicy_func_t *secpolicy) 5713{ 5714 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5715 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5716} 5717 5718static void 5719zfs_ioctl_init(void) 5720{ 5721 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT, 5722 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME, 5723 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5724 5725 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY, 5726 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME, 5727 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE); 5728 5729 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS, 5730 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, 5731 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5732 5733 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW, 5734 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME, 5735 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5736 5737 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE, 5738 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME, 5739 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5740 5741 zfs_ioctl_register("create", ZFS_IOC_CREATE, 5742 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME, 5743 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5744 5745 zfs_ioctl_register("clone", ZFS_IOC_CLONE, 5746 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME, 5747 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5748 5749 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS, 5750 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME, 5751 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5752 5753 zfs_ioctl_register("hold", ZFS_IOC_HOLD, 5754 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME, 5755 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5756 zfs_ioctl_register("release", ZFS_IOC_RELEASE, 5757 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME, 5758 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5759 5760 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS, 5761 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, 5762 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5763 5764 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK, 5765 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, 5766 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE); 5767 5768 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK, 5769 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME, 5770 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5771 5772 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS, 5773 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME, 5774 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5775 5776 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS, 5777 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks, 5778 POOL_NAME, 5779 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5780 5781 /* IOCTLS that use the legacy function signature */ 5782 5783 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, 5784 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY); 5785 5786 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create, 5787 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5788 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN, 5789 zfs_ioc_pool_scan); 5790 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE, 5791 zfs_ioc_pool_upgrade); 5792 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD, 5793 zfs_ioc_vdev_add); 5794 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE, 5795 zfs_ioc_vdev_remove); 5796 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE, 5797 zfs_ioc_vdev_set_state); 5798 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH, 5799 zfs_ioc_vdev_attach); 5800 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH, 5801 zfs_ioc_vdev_detach); 5802 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH, 5803 zfs_ioc_vdev_setpath); 5804 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU, 5805 zfs_ioc_vdev_setfru); 5806 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS, 5807 zfs_ioc_pool_set_props); 5808 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT, 5809 zfs_ioc_vdev_split); 5810 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID, 5811 zfs_ioc_pool_reguid); 5812 5813 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS, 5814 zfs_ioc_pool_configs, zfs_secpolicy_none); 5815 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT, 5816 zfs_ioc_pool_tryimport, zfs_secpolicy_config); 5817 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT, 5818 zfs_ioc_inject_fault, zfs_secpolicy_inject); 5819 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT, 5820 zfs_ioc_clear_fault, zfs_secpolicy_inject); 5821 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT, 5822 zfs_ioc_inject_list_next, zfs_secpolicy_inject); 5823 5824 /* 5825 * pool destroy, and export don't log the history as part of 5826 * zfsdev_ioctl, but rather zfs_ioc_pool_export 5827 * does the logging of those commands. 5828 */ 5829 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy, 5830 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 5831 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export, 5832 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 5833 5834 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats, 5835 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 5836 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props, 5837 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 5838 5839 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log, 5840 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE); 5841 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME, 5842 zfs_ioc_dsobj_to_dsname, 5843 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE); 5844 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY, 5845 zfs_ioc_pool_get_history, 5846 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); 5847 5848 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import, 5849 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5850 5851 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear, 5852 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5853 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen, 5854 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED); 5855 5856 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN, 5857 zfs_ioc_space_written); 5858 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS, 5859 zfs_ioc_objset_recvd_props); 5860 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ, 5861 zfs_ioc_next_obj); 5862 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL, 5863 zfs_ioc_get_fsacl); 5864 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS, 5865 zfs_ioc_objset_stats); 5866 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS, 5867 zfs_ioc_objset_zplprops); 5868 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT, 5869 zfs_ioc_dataset_list_next); 5870 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT, 5871 zfs_ioc_snapshot_list_next); 5872 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS, 5873 zfs_ioc_send_progress); 5874 5875 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF, 5876 zfs_ioc_diff, zfs_secpolicy_diff); 5877 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS, 5878 zfs_ioc_obj_to_stats, zfs_secpolicy_diff); 5879 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH, 5880 zfs_ioc_obj_to_path, zfs_secpolicy_diff); 5881 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE, 5882 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one); 5883 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY, 5884 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many); 5885 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND, 5886 zfs_ioc_send, zfs_secpolicy_send); 5887 5888 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop, 5889 zfs_secpolicy_none); 5890 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy, 5891 zfs_secpolicy_destroy); 5892 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename, 5893 zfs_secpolicy_rename); 5894 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, 5895 zfs_secpolicy_recv); 5896 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote, 5897 zfs_secpolicy_promote); 5898 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP, 5899 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop); 5900 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl, 5901 zfs_secpolicy_set_fsacl); 5902 5903 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share, 5904 zfs_secpolicy_share, POOL_CHECK_NONE); 5905 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl, 5906 zfs_secpolicy_smb_acl, POOL_CHECK_NONE); 5907 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE, 5908 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 5909 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5910 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT, 5911 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, 5912 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5913 5914#ifdef __FreeBSD__ 5915 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail, 5916 zfs_secpolicy_config, POOL_CHECK_NONE); 5917 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail, 5918 zfs_secpolicy_config, POOL_CHECK_NONE); 5919#endif 5920} 5921 5922int 5923pool_status_check(const char *name, zfs_ioc_namecheck_t type, 5924 zfs_ioc_poolcheck_t check) 5925{ 5926 spa_t *spa; 5927 int error; 5928 5929 ASSERT(type == POOL_NAME || type == DATASET_NAME); 5930 5931 if (check & POOL_CHECK_NONE) 5932 return (0); 5933 5934 error = spa_open(name, &spa, FTAG); 5935 if (error == 0) { 5936 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) 5937 error = SET_ERROR(EAGAIN); 5938 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) 5939 error = SET_ERROR(EROFS); 5940 spa_close(spa, FTAG); 5941 } 5942 return (error); 5943} 5944 5945/* 5946 * Find a free minor number. 5947 */ 5948minor_t 5949zfsdev_minor_alloc(void) 5950{ 5951 static minor_t last_minor; 5952 minor_t m; 5953 5954 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5955 5956 for (m = last_minor + 1; m != last_minor; m++) { 5957 if (m > ZFSDEV_MAX_MINOR) 5958 m = 1; 5959 if (ddi_get_soft_state(zfsdev_state, m) == NULL) { 5960 last_minor = m; 5961 return (m); 5962 } 5963 } 5964 5965 return (0); 5966} 5967 5968static int 5969zfs_ctldev_init(struct cdev *devp) 5970{ 5971 minor_t minor; 5972 zfs_soft_state_t *zs; 5973 5974 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5975 5976 minor = zfsdev_minor_alloc(); 5977 if (minor == 0) 5978 return (SET_ERROR(ENXIO)); 5979 5980 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) 5981 return (SET_ERROR(EAGAIN)); 5982 5983 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close); 5984 5985 zs = ddi_get_soft_state(zfsdev_state, minor); 5986 zs->zss_type = ZSST_CTLDEV; 5987 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data); 5988 5989 return (0); 5990} 5991 5992static void 5993zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor) 5994{ 5995 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5996 5997 zfs_onexit_destroy(zo); 5998 ddi_soft_state_free(zfsdev_state, minor); 5999} 6000 6001void * 6002zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which) 6003{ 6004 zfs_soft_state_t *zp; 6005 6006 zp = ddi_get_soft_state(zfsdev_state, minor); 6007 if (zp == NULL || zp->zss_type != which) 6008 return (NULL); 6009 6010 return (zp->zss_data); 6011} 6012 6013static int 6014zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td) 6015{ 6016 int error = 0; 6017 6018#ifdef illumos 6019 if (getminor(*devp) != 0) 6020 return (zvol_open(devp, flag, otyp, cr)); 6021#endif 6022 6023 /* This is the control device. Allocate a new minor if requested. */ 6024 if (flag & FEXCL) { 6025 mutex_enter(&spa_namespace_lock); 6026 error = zfs_ctldev_init(devp); 6027 mutex_exit(&spa_namespace_lock); 6028 } 6029 6030 return (error); 6031} 6032 6033static void 6034zfsdev_close(void *data) 6035{ 6036 zfs_onexit_t *zo; 6037 minor_t minor = (minor_t)(uintptr_t)data; 6038 6039 if (minor == 0) 6040 return; 6041 6042 mutex_enter(&spa_namespace_lock); 6043 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV); 6044 if (zo == NULL) { 6045 mutex_exit(&spa_namespace_lock); 6046 return; 6047 } 6048 zfs_ctldev_destroy(zo, minor); 6049 mutex_exit(&spa_namespace_lock); 6050} 6051 6052static int 6053zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag, 6054 struct thread *td) 6055{ 6056 zfs_cmd_t *zc; 6057 uint_t vecnum; 6058 int error, rc, len; 6059#ifdef illumos 6060 minor_t minor = getminor(dev); 6061#else 6062 zfs_iocparm_t *zc_iocparm; 6063 int cflag, cmd, oldvecnum; 6064 boolean_t newioc, compat; 6065 void *compat_zc = NULL; 6066 cred_t *cr = td->td_ucred; 6067#endif 6068 const zfs_ioc_vec_t *vec; 6069 char *saved_poolname = NULL; 6070 nvlist_t *innvl = NULL; 6071 6072 cflag = ZFS_CMD_COMPAT_NONE; 6073 compat = B_FALSE; 6074 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */ 6075 6076 len = IOCPARM_LEN(zcmd); 6077 vecnum = cmd = zcmd & 0xff; 6078 6079 /* 6080 * Check if we are talking to supported older binaries 6081 * and translate zfs_cmd if necessary 6082 */ 6083 if (len != sizeof(zfs_iocparm_t)) { 6084 newioc = B_FALSE; 6085 compat = B_TRUE; 6086 6087 vecnum = cmd; 6088 6089 switch (len) { 6090 case sizeof(zfs_cmd_zcmd_t): 6091 cflag = ZFS_CMD_COMPAT_LZC; 6092 break; 6093 case sizeof(zfs_cmd_deadman_t): 6094 cflag = ZFS_CMD_COMPAT_DEADMAN; 6095 break; 6096 case sizeof(zfs_cmd_v28_t): 6097 cflag = ZFS_CMD_COMPAT_V28; 6098 break; 6099 case sizeof(zfs_cmd_v15_t): 6100 cflag = ZFS_CMD_COMPAT_V15; 6101 vecnum = zfs_ioctl_v15_to_v28[cmd]; 6102 6103 /* 6104 * Return without further handling 6105 * if the command is blacklisted. 6106 */ 6107 if (vecnum == ZFS_IOC_COMPAT_PASS) 6108 return (0); 6109 else if (vecnum == ZFS_IOC_COMPAT_FAIL) 6110 return (ENOTSUP); 6111 break; 6112 default: 6113 return (EINVAL); 6114 } 6115 } 6116 6117#ifdef illumos 6118 vecnum = cmd - ZFS_IOC_FIRST; 6119 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 6120#endif 6121 6122 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 6123 return (SET_ERROR(EINVAL)); 6124 vec = &zfs_ioc_vec[vecnum]; 6125 6126 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP); 6127 6128#ifdef illumos 6129 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 6130 if (error != 0) { 6131 error = SET_ERROR(EFAULT); 6132 goto out; 6133 } 6134#else /* !illumos */ 6135 bzero(zc, sizeof(zfs_cmd_t)); 6136 6137 if (newioc) { 6138 zc_iocparm = (void *)arg; 6139 6140 switch (zc_iocparm->zfs_ioctl_version) { 6141 case ZFS_IOCVER_CURRENT: 6142 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) { 6143 error = SET_ERROR(EINVAL); 6144 goto out; 6145 } 6146 break; 6147 case ZFS_IOCVER_EDBP: 6148 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) { 6149 error = SET_ERROR(EFAULT); 6150 goto out; 6151 } 6152 compat = B_TRUE; 6153 cflag = ZFS_CMD_COMPAT_EDBP; 6154 break; 6155 case ZFS_IOCVER_ZCMD: 6156 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) || 6157 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) { 6158 error = SET_ERROR(EFAULT); 6159 goto out; 6160 } 6161 compat = B_TRUE; 6162 cflag = ZFS_CMD_COMPAT_ZCMD; 6163 break; 6164 default: 6165 error = SET_ERROR(EINVAL); 6166 goto out; 6167 /* NOTREACHED */ 6168 } 6169 6170 if (compat) { 6171 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size); 6172 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP); 6173 bzero(compat_zc, sizeof(zfs_cmd_t)); 6174 6175 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd, 6176 compat_zc, zc_iocparm->zfs_cmd_size, flag); 6177 if (error != 0) { 6178 error = SET_ERROR(EFAULT); 6179 goto out; 6180 } 6181 } else { 6182 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd, 6183 zc, zc_iocparm->zfs_cmd_size, flag); 6184 if (error != 0) { 6185 error = SET_ERROR(EFAULT); 6186 goto out; 6187 } 6188 } 6189 } 6190 6191 if (compat) { 6192 if (newioc) { 6193 ASSERT(compat_zc != NULL); 6194 zfs_cmd_compat_get(zc, compat_zc, cflag); 6195 } else { 6196 ASSERT(compat_zc == NULL); 6197 zfs_cmd_compat_get(zc, arg, cflag); 6198 } 6199 oldvecnum = vecnum; 6200 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag); 6201 if (error != 0) 6202 goto out; 6203 if (oldvecnum != vecnum) 6204 vec = &zfs_ioc_vec[vecnum]; 6205 } 6206#endif /* !illumos */ 6207 6208 zc->zc_iflags = flag & FKIOCTL; 6209 if (zc->zc_nvlist_src_size != 0) { 6210 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 6211 zc->zc_iflags, &innvl); 6212 if (error != 0) 6213 goto out; 6214 } 6215 6216 /* rewrite innvl for backwards compatibility */ 6217 if (compat) 6218 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag); 6219 6220 /* 6221 * Ensure that all pool/dataset names are valid before we pass down to 6222 * the lower layers. 6223 */ 6224 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 6225 switch (vec->zvec_namecheck) { 6226 case POOL_NAME: 6227 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 6228 error = SET_ERROR(EINVAL); 6229 else 6230 error = pool_status_check(zc->zc_name, 6231 vec->zvec_namecheck, vec->zvec_pool_check); 6232 break; 6233 6234 case DATASET_NAME: 6235 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 6236 error = SET_ERROR(EINVAL); 6237 else 6238 error = pool_status_check(zc->zc_name, 6239 vec->zvec_namecheck, vec->zvec_pool_check); 6240 break; 6241 6242 case NO_NAME: 6243 break; 6244 } 6245 6246 if (error == 0 && !(flag & FKIOCTL)) 6247 error = vec->zvec_secpolicy(zc, innvl, cr); 6248 6249 if (error != 0) 6250 goto out; 6251 6252 /* legacy ioctls can modify zc_name */ 6253 len = strcspn(zc->zc_name, "/@#") + 1; 6254 saved_poolname = kmem_alloc(len, KM_SLEEP); 6255 (void) strlcpy(saved_poolname, zc->zc_name, len); 6256 6257 if (vec->zvec_func != NULL) { 6258 nvlist_t *outnvl; 6259 int puterror = 0; 6260 spa_t *spa; 6261 nvlist_t *lognv = NULL; 6262 6263 ASSERT(vec->zvec_legacy_func == NULL); 6264 6265 /* 6266 * Add the innvl to the lognv before calling the func, 6267 * in case the func changes the innvl. 6268 */ 6269 if (vec->zvec_allow_log) { 6270 lognv = fnvlist_alloc(); 6271 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL, 6272 vec->zvec_name); 6273 if (!nvlist_empty(innvl)) { 6274 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL, 6275 innvl); 6276 } 6277 } 6278 6279 outnvl = fnvlist_alloc(); 6280 error = vec->zvec_func(zc->zc_name, innvl, outnvl); 6281 6282 if (error == 0 && vec->zvec_allow_log && 6283 spa_open(zc->zc_name, &spa, FTAG) == 0) { 6284 if (!nvlist_empty(outnvl)) { 6285 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL, 6286 outnvl); 6287 } 6288 (void) spa_history_log_nvl(spa, lognv); 6289 spa_close(spa, FTAG); 6290 } 6291 fnvlist_free(lognv); 6292 6293 /* rewrite outnvl for backwards compatibility */ 6294 if (compat) 6295 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum, 6296 cflag); 6297 6298 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) { 6299 int smusherror = 0; 6300 if (vec->zvec_smush_outnvlist) { 6301 smusherror = nvlist_smush(outnvl, 6302 zc->zc_nvlist_dst_size); 6303 } 6304 if (smusherror == 0) 6305 puterror = put_nvlist(zc, outnvl); 6306 } 6307 6308 if (puterror != 0) 6309 error = puterror; 6310 6311 nvlist_free(outnvl); 6312 } else { 6313 error = vec->zvec_legacy_func(zc); 6314 } 6315 6316out: 6317 nvlist_free(innvl); 6318 6319#ifdef illumos 6320 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 6321 if (error == 0 && rc != 0) 6322 error = SET_ERROR(EFAULT); 6323#else 6324 if (compat) { 6325 zfs_ioctl_compat_post(zc, cmd, cflag); 6326 if (newioc) { 6327 ASSERT(compat_zc != NULL); 6328 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size); 6329 6330 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag); 6331 rc = ddi_copyout(compat_zc, 6332 (void *)(uintptr_t)zc_iocparm->zfs_cmd, 6333 zc_iocparm->zfs_cmd_size, flag); 6334 if (error == 0 && rc != 0) 6335 error = SET_ERROR(EFAULT); 6336 kmem_free(compat_zc, sizeof (zfs_cmd_t)); 6337 } else { 6338 zfs_cmd_compat_put(zc, arg, vecnum, cflag); 6339 } 6340 } else { 6341 ASSERT(newioc); 6342 6343 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd, 6344 sizeof (zfs_cmd_t), flag); 6345 if (error == 0 && rc != 0) 6346 error = SET_ERROR(EFAULT); 6347 } 6348#endif 6349 if (error == 0 && vec->zvec_allow_log) { 6350 char *s = tsd_get(zfs_allow_log_key); 6351 if (s != NULL) 6352 strfree(s); 6353 (void) tsd_set(zfs_allow_log_key, saved_poolname); 6354 } else { 6355 if (saved_poolname != NULL) 6356 strfree(saved_poolname); 6357 } 6358 6359 kmem_free(zc, sizeof (zfs_cmd_t)); 6360 return (error); 6361} 6362 6363#ifdef illumos 6364static int 6365zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 6366{ 6367 if (cmd != DDI_ATTACH) 6368 return (DDI_FAILURE); 6369 6370 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 6371 DDI_PSEUDO, 0) == DDI_FAILURE) 6372 return (DDI_FAILURE); 6373 6374 zfs_dip = dip; 6375 6376 ddi_report_dev(dip); 6377 6378 return (DDI_SUCCESS); 6379} 6380 6381static int 6382zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 6383{ 6384 if (spa_busy() || zfs_busy() || zvol_busy()) 6385 return (DDI_FAILURE); 6386 6387 if (cmd != DDI_DETACH) 6388 return (DDI_FAILURE); 6389 6390 zfs_dip = NULL; 6391 6392 ddi_prop_remove_all(dip); 6393 ddi_remove_minor_node(dip, NULL); 6394 6395 return (DDI_SUCCESS); 6396} 6397 6398/*ARGSUSED*/ 6399static int 6400zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 6401{ 6402 switch (infocmd) { 6403 case DDI_INFO_DEVT2DEVINFO: 6404 *result = zfs_dip; 6405 return (DDI_SUCCESS); 6406 6407 case DDI_INFO_DEVT2INSTANCE: 6408 *result = (void *)0; 6409 return (DDI_SUCCESS); 6410 } 6411 6412 return (DDI_FAILURE); 6413} 6414#endif /* illumos */ 6415 6416/* 6417 * OK, so this is a little weird. 6418 * 6419 * /dev/zfs is the control node, i.e. minor 0. 6420 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 6421 * 6422 * /dev/zfs has basically nothing to do except serve up ioctls, 6423 * so most of the standard driver entry points are in zvol.c. 6424 */ 6425#ifdef illumos 6426static struct cb_ops zfs_cb_ops = { 6427 zfsdev_open, /* open */ 6428 zfsdev_close, /* close */ 6429 zvol_strategy, /* strategy */ 6430 nodev, /* print */ 6431 zvol_dump, /* dump */ 6432 zvol_read, /* read */ 6433 zvol_write, /* write */ 6434 zfsdev_ioctl, /* ioctl */ 6435 nodev, /* devmap */ 6436 nodev, /* mmap */ 6437 nodev, /* segmap */ 6438 nochpoll, /* poll */ 6439 ddi_prop_op, /* prop_op */ 6440 NULL, /* streamtab */ 6441 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 6442 CB_REV, /* version */ 6443 nodev, /* async read */ 6444 nodev, /* async write */ 6445}; 6446 6447static struct dev_ops zfs_dev_ops = { 6448 DEVO_REV, /* version */ 6449 0, /* refcnt */ 6450 zfs_info, /* info */ 6451 nulldev, /* identify */ 6452 nulldev, /* probe */ 6453 zfs_attach, /* attach */ 6454 zfs_detach, /* detach */ 6455 nodev, /* reset */ 6456 &zfs_cb_ops, /* driver operations */ 6457 NULL, /* no bus operations */ 6458 NULL, /* power */ 6459 ddi_quiesce_not_needed, /* quiesce */ 6460}; 6461 6462static struct modldrv zfs_modldrv = { 6463 &mod_driverops, 6464 "ZFS storage pool", 6465 &zfs_dev_ops 6466}; 6467 6468static struct modlinkage modlinkage = { 6469 MODREV_1, 6470 (void *)&zfs_modlfs, 6471 (void *)&zfs_modldrv, 6472 NULL 6473}; 6474#endif /* illumos */ 6475 6476static struct cdevsw zfs_cdevsw = { 6477 .d_version = D_VERSION, 6478 .d_open = zfsdev_open, 6479 .d_ioctl = zfsdev_ioctl, 6480 .d_name = ZFS_DEV_NAME 6481}; 6482 6483static void 6484zfs_allow_log_destroy(void *arg) 6485{ 6486 char *poolname = arg; 6487 strfree(poolname); 6488} 6489 6490static void 6491zfsdev_init(void) 6492{ 6493 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666, 6494 ZFS_DEV_NAME); 6495} 6496 6497static void 6498zfsdev_fini(void) 6499{ 6500 if (zfsdev != NULL) 6501 destroy_dev(zfsdev); 6502} 6503 6504static struct root_hold_token *zfs_root_token; 6505struct proc *zfsproc; 6506 6507#ifdef illumos 6508int 6509_init(void) 6510{ 6511 int error; 6512 6513 spa_init(FREAD | FWRITE); 6514 zfs_init(); 6515 zvol_init(); 6516 zfs_ioctl_init(); 6517 6518 if ((error = mod_install(&modlinkage)) != 0) { 6519 zvol_fini(); 6520 zfs_fini(); 6521 spa_fini(); 6522 return (error); 6523 } 6524 6525 tsd_create(&zfs_fsyncer_key, NULL); 6526 tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 6527 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy); 6528 6529 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 6530 ASSERT(error == 0); 6531 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 6532 6533 return (0); 6534} 6535 6536int 6537_fini(void) 6538{ 6539 int error; 6540 6541 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 6542 return (SET_ERROR(EBUSY)); 6543 6544 if ((error = mod_remove(&modlinkage)) != 0) 6545 return (error); 6546 6547 zvol_fini(); 6548 zfs_fini(); 6549 spa_fini(); 6550 if (zfs_nfsshare_inited) 6551 (void) ddi_modclose(nfs_mod); 6552 if (zfs_smbshare_inited) 6553 (void) ddi_modclose(smbsrv_mod); 6554 if (zfs_nfsshare_inited || zfs_smbshare_inited) 6555 (void) ddi_modclose(sharefs_mod); 6556 6557 tsd_destroy(&zfs_fsyncer_key); 6558 ldi_ident_release(zfs_li); 6559 zfs_li = NULL; 6560 mutex_destroy(&zfs_share_lock); 6561 6562 return (error); 6563} 6564 6565int 6566_info(struct modinfo *modinfop) 6567{ 6568 return (mod_info(&modlinkage, modinfop)); 6569} 6570#endif /* illumos */ 6571 6572static int zfs__init(void); 6573static int zfs__fini(void); 6574static void zfs_shutdown(void *, int); 6575 6576static eventhandler_tag zfs_shutdown_event_tag; 6577 6578#ifdef __FreeBSD__ 6579#define ZFS_MIN_KSTACK_PAGES 4 6580#endif 6581 6582int 6583zfs__init(void) 6584{ 6585 6586#ifdef __FreeBSD__ 6587#if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES 6588 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack " 6589 "overflow panic!\nPlease consider adding " 6590 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES, 6591 ZFS_MIN_KSTACK_PAGES); 6592#endif 6593#endif 6594 zfs_root_token = root_mount_hold("ZFS"); 6595 6596 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 6597 6598 spa_init(FREAD | FWRITE); 6599 zfs_init(); 6600 zvol_init(); 6601 zfs_ioctl_init(); 6602 6603 tsd_create(&zfs_fsyncer_key, NULL); 6604 tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 6605 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
|