]> granicus.if.org Git - zfs/blobdiff - module/zfs/zfs_ioctl.c
OpenZFS 8677 - Open-Context Channel Programs
[zfs] / module / zfs / zfs_ioctl.c
index 268e797146fc65b47748145232e7a6b5bd531707..cc7155a78ad192899a58c0ecfceda6c32480b49e 100644 (file)
  * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
- * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
+ * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  * Copyright (c) 2013 Steven Hartland. All rights reserved.
  * Copyright (c) 2014 Integros [integros.com]
  * Copyright 2016 Toomas Soome <tsoome@me.com>
  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
  * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
+ * Copyright (c) 2017 Datto Inc. All rights reserved.
+ * Copyright 2017 RackTop Systems.
+ * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
  */
 
 /*
 #include <sys/dsl_scan.h>
 #include <sharefs/share.h>
 #include <sys/fm/util.h>
+#include <sys/dsl_crypt.h>
 
 #include <sys/dmu_send.h>
 #include <sys/dsl_destroy.h>
 #include <sys/dsl_bookmark.h>
 #include <sys/dsl_userhold.h>
 #include <sys/zfeature.h>
+#include <sys/zcp.h>
 #include <sys/zio_checksum.h>
 
 #include <linux/miscdevice.h>
 #include "zfs_deleg.h"
 #include "zfs_comutil.h"
 
+#include <sys/lua/lua.h>
+#include <sys/lua/lauxlib.h>
+
 /*
  * Limit maximum nvlist size.  We don't want users passing in insane values
  * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
@@ -563,12 +571,12 @@ zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
                 * Try to own the dataset; abort if there is any error,
                 * (e.g., already mounted, in use, or other error).
                 */
-               error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
+               error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
                    setsl_tag, &os);
                if (error != 0)
                        return (SET_ERROR(EPERM));
 
-               dmu_objset_disown(os, setsl_tag);
+               dmu_objset_disown(os, B_TRUE, setsl_tag);
 
                if (new_default) {
                        needed_priv = PRIV_FILE_DOWNGRADE_SL;
@@ -593,7 +601,7 @@ out_check:
                return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
        return (0);
 #else
-       return (ENOTSUP);
+       return (SET_ERROR(ENOTSUP));
 #endif /* HAVE_MLSLABEL */
 }
 
@@ -1016,9 +1024,8 @@ static int
 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        int error = 0;
-       nvpair_t *pair;
 
-       for (pair = nvlist_next_nvpair(innvl, NULL);
+       for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
            pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
                char *name = nvpair_name(pair);
                char *hashp = strchr(name, '#');
@@ -1299,6 +1306,20 @@ zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
        return (error);
 }
 
+static int
+zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       return (zfs_secpolicy_write_perms(zc->zc_name,
+           ZFS_DELEG_PERM_LOAD_KEY, cr));
+}
+
+static int
+zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       return (zfs_secpolicy_write_perms(zc->zc_name,
+           ZFS_DELEG_PERM_CHANGE_KEY, cr));
+}
+
 /*
  * Returns the nvlist as specified by the user in the zfs_cmd_t.
  */
@@ -1397,17 +1418,11 @@ put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
        return (error);
 }
 
-static int
-getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
+int
+getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
 {
-       objset_t *os;
-       int error;
-
-       error = dmu_objset_hold(dsname, FTAG, &os);
-       if (error != 0)
-               return (error);
+       int error = 0;
        if (dmu_objset_type(os) != DMU_OST_ZFS) {
-               dmu_objset_rele(os, FTAG);
                return (SET_ERROR(EINVAL));
        }
 
@@ -1419,6 +1434,20 @@ getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
                error = SET_ERROR(ESRCH);
        }
        mutex_exit(&os->os_user_ptr_lock);
+       return (error);
+}
+
+int
+getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
+{
+       objset_t *os;
+       int error;
+
+       error = dmu_objset_hold(dsname, FTAG, &os);
+       if (error != 0)
+               return (error);
+
+       error = getzfsvfs_impl(os, zfvp);
        dmu_objset_rele(os, FTAG);
        return (error);
 }
@@ -1460,7 +1489,7 @@ zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
        if (zfsvfs->z_sb) {
                deactivate_super(zfsvfs->z_sb);
        } else {
-               dmu_objset_disown(zfsvfs->z_os, zfsvfs);
+               dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
                zfsvfs_free(zfsvfs);
        }
 }
@@ -1472,6 +1501,7 @@ zfs_ioc_pool_create(zfs_cmd_t *zc)
        nvlist_t *config, *props = NULL;
        nvlist_t *rootprops = NULL;
        nvlist_t *zplprops = NULL;
+       dsl_crypto_params_t *dcp = NULL;
 
        if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
            zc->zc_iflags, &config)))
@@ -1486,6 +1516,7 @@ zfs_ioc_pool_create(zfs_cmd_t *zc)
 
        if (props) {
                nvlist_t *nvl = NULL;
+               nvlist_t *hidden_args = NULL;
                uint64_t version = SPA_VERSION;
 
                (void) nvlist_lookup_uint64(props,
@@ -1504,6 +1535,18 @@ zfs_ioc_pool_create(zfs_cmd_t *zc)
                        }
                        (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
                }
+
+               (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
+                   &hidden_args);
+               error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
+                   rootprops, hidden_args, &dcp);
+               if (error != 0) {
+                       nvlist_free(config);
+                       nvlist_free(props);
+                       return (error);
+               }
+               (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
+
                VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
                error = zfs_fill_zplprops_root(version, rootprops,
                    zplprops, NULL);
@@ -1511,7 +1554,7 @@ zfs_ioc_pool_create(zfs_cmd_t *zc)
                        goto pool_props_bad;
        }
 
-       error = spa_create(zc->zc_name, config, props, zplprops);
+       error = spa_create(zc->zc_name, config, props, zplprops, dcp);
 
        /*
         * Set the remaining root properties
@@ -1525,6 +1568,7 @@ pool_props_bad:
        nvlist_free(zplprops);
        nvlist_free(config);
        nvlist_free(props);
+       dsl_crypto_params_free(dcp, !!error);
 
        return (error);
 }
@@ -1648,7 +1692,7 @@ zfs_ioc_pool_stats(zfs_cmd_t *zc)
 static int
 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
 {
-       nvlist_t *tryconfig, *config;
+       nvlist_t *tryconfig, *config = NULL;
        int error;
 
        if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
@@ -1672,6 +1716,7 @@ zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
  * inputs:
  * zc_name              name of the pool
  * zc_cookie            scan func (pool_scan_func_t)
+ * zc_flags             scrub pause/resume flag (pool_scrub_cmd_t)
  */
 static int
 zfs_ioc_pool_scan(zfs_cmd_t *zc)
@@ -1682,7 +1727,12 @@ zfs_ioc_pool_scan(zfs_cmd_t *zc)
        if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
                return (error);
 
-       if (zc->zc_cookie == POOL_SCAN_NONE)
+       if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
+               return (SET_ERROR(EINVAL));
+
+       if (zc->zc_flags == POOL_SCRUB_PAUSE)
+               error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
+       else if (zc->zc_cookie == POOL_SCAN_NONE)
                error = spa_scan_stop(spa);
        else
                error = spa_scan(spa, zc->zc_cookie);
@@ -1794,15 +1844,16 @@ zfs_ioc_obj_to_path(zfs_cmd_t *zc)
        int error;
 
        /* XXX reading from objset not owned */
-       if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
+       if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
+           FTAG, &os)) != 0)
                return (error);
        if (dmu_objset_type(os) != DMU_OST_ZFS) {
-               dmu_objset_rele(os, FTAG);
+               dmu_objset_rele_flags(os, B_TRUE, FTAG);
                return (SET_ERROR(EINVAL));
        }
        error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
            sizeof (zc->zc_value));
-       dmu_objset_rele(os, FTAG);
+       dmu_objset_rele_flags(os, B_TRUE, FTAG);
 
        return (error);
 }
@@ -1823,15 +1874,16 @@ zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
        int error;
 
        /* XXX reading from objset not owned */
-       if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
+       if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
+           FTAG, &os)) != 0)
                return (error);
        if (dmu_objset_type(os) != DMU_OST_ZFS) {
-               dmu_objset_rele(os, FTAG);
+               dmu_objset_rele_flags(os, B_TRUE, FTAG);
                return (SET_ERROR(EINVAL));
        }
        error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
            sizeof (zc->zc_value));
-       dmu_objset_rele(os, FTAG);
+       dmu_objset_rele_flags(os, B_TRUE, FTAG);
 
        return (error);
 }
@@ -2377,7 +2429,8 @@ zfs_prop_set_special(const char *dsname, zprop_source_t source,
 {
        const char *propname = nvpair_name(pair);
        zfs_prop_t prop = zfs_name_to_prop(propname);
-       uint64_t intval;
+       uint64_t intval = 0;
+       char *strval = NULL;
        int err = -1;
 
        if (prop == ZPROP_INVAL) {
@@ -2393,10 +2446,12 @@ zfs_prop_set_special(const char *dsname, zprop_source_t source,
                    &pair) == 0);
        }
 
-       if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
-               return (-1);
-
-       VERIFY(0 == nvpair_value_uint64(pair, &intval));
+       /* all special properties are numeric except for keylocation */
+       if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
+               strval = fnvpair_value_string(pair);
+       } else {
+               intval = fnvpair_value_uint64(pair);
+       }
 
        switch (prop) {
        case ZFS_PROP_QUOTA:
@@ -2413,6 +2468,16 @@ zfs_prop_set_special(const char *dsname, zprop_source_t source,
                } else {
                        err = dsl_dir_activate_fs_ss_limit(dsname);
                }
+               /*
+                * Set err to -1 to force the zfs_set_prop_nvlist code down the
+                * default path to set the value in the nvlist.
+                */
+               if (err == 0)
+                       err = -1;
+               break;
+       case ZFS_PROP_KEYLOCATION:
+               err = dsl_crypto_can_set_keylocation(dsname, strval);
+
                /*
                 * Set err to -1 to force the zfs_set_prop_nvlist code down the
                 * default path to set the value in the nvlist.
@@ -2432,6 +2497,9 @@ zfs_prop_set_special(const char *dsname, zprop_source_t source,
        case ZFS_PROP_SNAPDEV:
                err = zvol_set_snapdev(dsname, source, intval);
                break;
+       case ZFS_PROP_VOLMODE:
+               err = zvol_set_volmode(dsname, source, intval);
+               break;
        case ZFS_PROP_VERSION:
        {
                zfsvfs_t *zfsvfs;
@@ -2760,54 +2828,12 @@ zfs_ioc_inherit_prop(zfs_cmd_t *zc)
        zprop_source_t source = (received
            ? ZPROP_SRC_NONE            /* revert to received value, if any */
            : ZPROP_SRC_INHERITED);     /* explicitly inherit */
+       nvlist_t *dummy;
+       nvpair_t *pair;
+       zprop_type_t type;
+       int err;
 
-       if (received) {
-               nvlist_t *dummy;
-               nvpair_t *pair;
-               zprop_type_t type;
-               int err;
-
-               /*
-                * zfs_prop_set_special() expects properties in the form of an
-                * nvpair with type info.
-                */
-               if (prop == ZPROP_INVAL) {
-                       if (!zfs_prop_user(propname))
-                               return (SET_ERROR(EINVAL));
-
-                       type = PROP_TYPE_STRING;
-               } else if (prop == ZFS_PROP_VOLSIZE ||
-                   prop == ZFS_PROP_VERSION) {
-                       return (SET_ERROR(EINVAL));
-               } else {
-                       type = zfs_prop_get_type(prop);
-               }
-
-               VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
-
-               switch (type) {
-               case PROP_TYPE_STRING:
-                       VERIFY(0 == nvlist_add_string(dummy, propname, ""));
-                       break;
-               case PROP_TYPE_NUMBER:
-               case PROP_TYPE_INDEX:
-                       VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
-                       break;
-               default:
-                       nvlist_free(dummy);
-                       return (SET_ERROR(EINVAL));
-               }
-
-               pair = nvlist_next_nvpair(dummy, NULL);
-               if (pair == NULL) {
-                       nvlist_free(dummy);
-                       return (SET_ERROR(EINVAL));
-               }
-               err = zfs_prop_set_special(zc->zc_name, source, pair);
-               nvlist_free(dummy);
-               if (err != -1)
-                       return (err); /* special property already handled */
-       } else {
+       if (!received) {
                /*
                 * Only check this in the non-received case. We want to allow
                 * 'inherit -S' to revert non-inheritable properties like quota
@@ -2818,8 +2844,49 @@ zfs_ioc_inherit_prop(zfs_cmd_t *zc)
                        return (SET_ERROR(EINVAL));
        }
 
-       /* property name has been validated by zfs_secpolicy_inherit_prop() */
-       return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
+       if (prop == ZPROP_INVAL) {
+               if (!zfs_prop_user(propname))
+                       return (SET_ERROR(EINVAL));
+
+               type = PROP_TYPE_STRING;
+       } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
+               return (SET_ERROR(EINVAL));
+       } else {
+               type = zfs_prop_get_type(prop);
+       }
+
+       /*
+        * zfs_prop_set_special() expects properties in the form of an
+        * nvpair with type info.
+        */
+       dummy = fnvlist_alloc();
+
+       switch (type) {
+       case PROP_TYPE_STRING:
+               VERIFY(0 == nvlist_add_string(dummy, propname, ""));
+               break;
+       case PROP_TYPE_NUMBER:
+       case PROP_TYPE_INDEX:
+               VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
+               break;
+       default:
+               err = SET_ERROR(EINVAL);
+               goto errout;
+       }
+
+       pair = nvlist_next_nvpair(dummy, NULL);
+       if (pair == NULL) {
+               err = SET_ERROR(EINVAL);
+       } else {
+               err = zfs_prop_set_special(zc->zc_name, source, pair);
+               if (err == -1) /* property is not "special", needs handling */
+                       err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
+                           source);
+       }
+
+errout:
+       nvlist_free(dummy);
+       return (err);
 }
 
 static int
@@ -3146,6 +3213,8 @@ zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
  * innvl: {
  *     "type" -> dmu_objset_type_t (int32)
  *     (optional) "props" -> { prop -> value }
+ *     (optional) "hidden_args" -> { "wkeydata" -> value }
+ *         raw uint8_t array of encryption wrapping key data (32 bytes)
  * }
  *
  * outnvl: propname -> error code (int32)
@@ -3156,15 +3225,18 @@ zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
        int error = 0;
        zfs_creat_t zct = { 0 };
        nvlist_t *nvprops = NULL;
+       nvlist_t *hidden_args = NULL;
        void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
        int32_t type32;
        dmu_objset_type_t type;
        boolean_t is_insensitive = B_FALSE;
+       dsl_crypto_params_t *dcp = NULL;
 
        if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
                return (SET_ERROR(EINVAL));
        type = type32;
        (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
+       (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
 
        switch (type) {
        case DMU_OST_ZFS:
@@ -3230,9 +3302,18 @@ zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
                }
        }
 
+       error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
+           hidden_args, &dcp);
+       if (error != 0) {
+               nvlist_free(zct.zct_zplprops);
+               return (error);
+       }
+
        error = dmu_objset_create(fsname, type,
-           is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
+           is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
+
        nvlist_free(zct.zct_zplprops);
+       dsl_crypto_params_free(dcp, !!error);
 
        /*
         * It would be nice to do this atomically.
@@ -3267,6 +3348,8 @@ zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
  * innvl: {
  *     "origin" -> name of origin snapshot
  *     (optional) "props" -> { prop -> value }
+ *     (optional) "hidden_args" -> { "wkeydata" -> value }
+ *         raw uint8_t array of encryption wrapping key data (32 bytes)
  * }
  *
  * outputs:
@@ -3289,9 +3372,8 @@ zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
 
        if (dataset_namecheck(origin_name, NULL, NULL) != 0)
                return (SET_ERROR(EINVAL));
+
        error = dmu_objset_clone(fsname, origin_name);
-       if (error != 0)
-               return (error);
 
        /*
         * It would be nice to do this atomically.
@@ -3319,7 +3401,7 @@ zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
        nvlist_t *snaps;
        nvlist_t *props = NULL;
        int error, poollen;
-       nvpair_t *pair, *pair2;
+       nvpair_t *pair;
 
        (void) nvlist_lookup_nvlist(innvl, "props", &props);
        if ((error = zfs_check_userprops(poolname, props)) != 0)
@@ -3353,7 +3435,7 @@ zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
                        return (SET_ERROR(EXDEV));
 
                /* This must be the only snap of this fs. */
-               for (pair2 = nvlist_next_nvpair(snaps, pair);
+               for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
                    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
                        if (strncmp(name, nvpair_name(pair2), cp - name + 1)
                            == 0) {
@@ -3420,26 +3502,21 @@ zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
  * Returns 0 if the argument is not a snapshot, or it is not currently a
  * filesystem, or we were able to unmount it.  Returns error code otherwise.
  */
-int
+void
 zfs_unmount_snap(const char *snapname)
 {
-       int err;
-
        if (strchr(snapname, '@') == NULL)
-               return (0);
-
-       err = zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
-       if (err != 0 && err != ENOENT)
-               return (SET_ERROR(err));
+               return;
 
-       return (0);
+       (void) zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
 }
 
 /* ARGSUSED */
 static int
 zfs_unmount_snap_cb(const char *snapname, void *arg)
 {
-       return (zfs_unmount_snap(snapname));
+       zfs_unmount_snap(snapname);
+       return (0);
 }
 
 /*
@@ -3462,7 +3539,7 @@ zfs_destroy_unmount_origin(const char *fsname)
                char originname[ZFS_MAX_DATASET_NAME_LEN];
                dsl_dataset_name(ds->ds_prev, originname);
                dmu_objset_rele(os, FTAG);
-               (void) zfs_unmount_snap(originname);
+               zfs_unmount_snap(originname);
        } else {
                dmu_objset_rele(os, FTAG);
        }
@@ -3490,7 +3567,7 @@ zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
 
        for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
            pair = nvlist_next_nvpair(snaps, pair)) {
-               (void) zfs_unmount_snap(nvpair_name(pair));
+               zfs_unmount_snap(nvpair_name(pair));
        }
 
        return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
@@ -3511,9 +3588,7 @@ zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
 static int
 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
 {
-       nvpair_t *pair, *pair2;
-
-       for (pair = nvlist_next_nvpair(innvl, NULL);
+       for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
            pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
                char *snap_name;
 
@@ -3525,7 +3600,7 @@ zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
 
 
                /* Verify that the keys (bookmarks) are unique */
-               for (pair2 = nvlist_next_nvpair(innvl, pair);
+               for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
                    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
                        if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
                                return (SET_ERROR(EINVAL));
@@ -3565,10 +3640,9 @@ zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
     nvlist_t *outnvl)
 {
        int error, poollen;
-       nvpair_t *pair;
 
        poollen = strlen(poolname);
-       for (pair = nvlist_next_nvpair(innvl, NULL);
+       for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
            pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
                const char *name = nvpair_name(pair);
                const char *cp = strchr(name, '#');
@@ -3593,6 +3667,40 @@ zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
        return (error);
 }
 
+static int
+zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
+    nvlist_t *outnvl)
+{
+       char *program;
+       uint64_t instrlimit, memlimit;
+       boolean_t sync_flag;
+       nvpair_t *nvarg = NULL;
+
+       if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
+               return (EINVAL);
+       }
+       if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
+               sync_flag = B_TRUE;
+       }
+       if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
+               instrlimit = ZCP_DEFAULT_INSTRLIMIT;
+       }
+       if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
+               memlimit = ZCP_DEFAULT_MEMLIMIT;
+       }
+       if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
+               return (EINVAL);
+       }
+
+       if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
+               return (EINVAL);
+       if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
+               return (EINVAL);
+
+       return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
+           nvarg, outnvl));
+}
+
 /*
  * inputs:
  * zc_name             name of dataset to destroy
@@ -3606,11 +3714,8 @@ zfs_ioc_destroy(zfs_cmd_t *zc)
 {
        int err;
 
-       if (zc->zc_objset_type == DMU_OST_ZFS) {
-               err = zfs_unmount_snap(zc->zc_name);
-               if (err != 0)
-                       return (err);
-       }
+       if (zc->zc_objset_type == DMU_OST_ZFS)
+               zfs_unmount_snap(zc->zc_name);
 
        if (strchr(zc->zc_name, '@')) {
                err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
@@ -3627,8 +3732,10 @@ zfs_ioc_destroy(zfs_cmd_t *zc)
                         */
                        char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
 
-                       (void) snprintf(namebuf, sizeof (namebuf),
-                           "%s/%s", zc->zc_name, recv_clone_name);
+                       if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
+                           zc->zc_name, recv_clone_name) >=
+                           sizeof (namebuf))
+                               return (SET_ERROR(EINVAL));
 
                        /*
                         * Try to remove the hidden child (%recv) and after
@@ -3640,7 +3747,7 @@ zfs_ioc_destroy(zfs_cmd_t *zc)
                        if (err == 0)
                                err = dsl_destroy_head(zc->zc_name);
                        else if (err == ENOENT)
-                               err = EEXIST;
+                               err = SET_ERROR(EEXIST);
                }
        }
 
@@ -3650,19 +3757,30 @@ zfs_ioc_destroy(zfs_cmd_t *zc)
 /*
  * fsname is name of dataset to rollback (to most recent snapshot)
  *
- * innvl is not used.
+ * innvl may contain name of expected target snapshot
  *
  * outnvl: "target" -> name of most recent snapshot
  * }
  */
 /* ARGSUSED */
 static int
-zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
+zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
 {
        zfsvfs_t *zfsvfs;
        zvol_state_t *zv;
+       char *target = NULL;
        int error;
 
+       (void) nvlist_lookup_string(innvl, "target", &target);
+       if (target != NULL) {
+               int fslen = strlen(fsname);
+
+               if (strncmp(fsname, target, fslen) != 0)
+                       return (SET_ERROR(EINVAL));
+               if (target[fslen] != '@')
+                       return (SET_ERROR(EINVAL));
+       }
+
        if (getzfsvfs(fsname, &zfsvfs) == 0) {
                dsl_dataset_t *ds;
 
@@ -3671,16 +3789,18 @@ zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
                if (error == 0) {
                        int resume_err;
 
-                       error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
+                       error = dsl_dataset_rollback(fsname, target, zfsvfs,
+                           outnvl);
                        resume_err = zfs_resume_fs(zfsvfs, ds);
                        error = error ? error : resume_err;
                }
                deactivate_super(zfsvfs->z_sb);
        } else if ((zv = zvol_suspend(fsname)) != NULL) {
-               error = dsl_dataset_rollback(fsname, zvol_tag(zv), outnvl);
+               error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
+                   outnvl);
                zvol_resume(zv);
        } else {
-               error = dsl_dataset_rollback(fsname, NULL, outnvl);
+               error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
        }
        return (error);
 }
@@ -3690,13 +3810,12 @@ recursive_unmount(const char *fsname, void *arg)
 {
        const char *snapname = arg;
        char *fullname;
-       int error;
 
        fullname = kmem_asprintf("%s@%s", fsname, snapname);
-       error = zfs_unmount_snap(fullname);
+       zfs_unmount_snap(fullname);
        strfree(fullname);
 
-       return (error);
+       return (0);
 }
 
 /*
@@ -3713,9 +3832,12 @@ zfs_ioc_rename(zfs_cmd_t *zc)
        boolean_t recursive = zc->zc_cookie & 1;
        char *at;
 
+       /* "zfs rename" from and to ...%recv datasets should both fail */
+       zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
        zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
-       if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
-           strchr(zc->zc_value, '%'))
+       if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
+           dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
+           strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
                return (SET_ERROR(EINVAL));
 
        at = strchr(zc->zc_name, '@');
@@ -4132,7 +4254,11 @@ extract_delay_props(nvlist_t *props)
 {
        nvlist_t *delayprops;
        nvpair_t *nvp, *tmp;
-       static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
+       static const zfs_prop_t delayable[] = {
+               ZFS_PROP_REFQUOTA,
+               ZFS_PROP_KEYLOCATION,
+               0
+       };
        int i;
 
        VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
@@ -4666,6 +4792,9 @@ zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
  *
  * outputs:
  * zc_objset_type      estimated size, if zc_guid is set
+ *
+ * NOTE: This is no longer the preferred interface, any new functionality
+ *       should be added to zfs_ioc_send_new() instead.
  */
 static int
 zfs_ioc_send(zfs_cmd_t *zc)
@@ -4676,6 +4805,7 @@ zfs_ioc_send(zfs_cmd_t *zc)
        boolean_t embedok = (zc->zc_flags & 0x1);
        boolean_t large_block_ok = (zc->zc_flags & 0x2);
        boolean_t compressok = (zc->zc_flags & 0x4);
+       boolean_t rawok = (zc->zc_flags & 0x8);
 
        if (zc->zc_obj != 0) {
                dsl_pool_t *dp;
@@ -4707,7 +4837,8 @@ zfs_ioc_send(zfs_cmd_t *zc)
                if (error != 0)
                        return (error);
 
-               error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
+               error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
+                   FTAG, &tosnap);
                if (error != 0) {
                        dsl_pool_rele(dp, FTAG);
                        return (error);
@@ -4723,7 +4854,7 @@ zfs_ioc_send(zfs_cmd_t *zc)
                        }
                }
 
-               error = dmu_send_estimate(tosnap, fromsnap, compressok,
+               error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
                    &zc->zc_objset_type);
 
                if (fromsnap != NULL)
@@ -4737,7 +4868,7 @@ zfs_ioc_send(zfs_cmd_t *zc)
 
                off = fp->f_offset;
                error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
-                   zc->zc_fromobj, embedok, large_block_ok, compressok,
+                   zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
                    zc->zc_cookie, fp->f_vnode, &off);
 
                if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
@@ -4921,7 +5052,8 @@ zfs_ioc_clear(zfs_cmd_t *zc)
 
        vdev_clear(spa, vd);
 
-       (void) spa_vdev_state_exit(spa, spa->spa_root_vdev, 0);
+       (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
+           NULL : spa->spa_root_vdev, 0);
 
        /*
         * Resume any suspended I/Os.
@@ -4934,25 +5066,46 @@ zfs_ioc_clear(zfs_cmd_t *zc)
        return (error);
 }
 
+/*
+ * Reopen all the vdevs associated with the pool.
+ *
+ * innvl: {
+ *  "scrub_restart" -> when true and scrub is running, allow to restart
+ *              scrub as the side effect of the reopen (boolean).
+ * }
+ *
+ * outnvl is unused
+ */
+/* ARGSUSED */
 static int
-zfs_ioc_pool_reopen(zfs_cmd_t *zc)
+zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
 {
        spa_t *spa;
        int error;
+       boolean_t scrub_restart = B_TRUE;
 
-       error = spa_open(zc->zc_name, &spa, FTAG);
+       if (innvl) {
+               if (nvlist_lookup_boolean_value(innvl, "scrub_restart",
+                   &scrub_restart) != 0) {
+                       return (SET_ERROR(EINVAL));
+               }
+       }
+
+       error = spa_open(pool, &spa, FTAG);
        if (error != 0)
                return (error);
 
        spa_vdev_state_enter(spa, SCL_NONE);
 
        /*
-        * If a resilver is already in progress then set the
-        * spa_scrub_reopen flag to B_TRUE so that we don't restart
-        * the scan as a side effect of the reopen. Otherwise, let
-        * vdev_open() decided if a resilver is required.
+        * If the scrub_restart flag is B_FALSE and a scrub is already
+        * in progress then set spa_scrub_reopen flag to B_TRUE so that
+        * we don't restart the scrub as a side effect of the reopen.
+        * Otherwise, let vdev_open() decided if a resilver is required.
         */
-       spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
+
+       spa->spa_scrub_reopen = (!scrub_restart &&
+           dsl_scan_scrubbing(spa->spa_dsl_pool));
        vdev_reopen(spa->spa_root_vdev);
        spa->spa_scrub_reopen = B_FALSE;
 
@@ -4960,10 +5113,10 @@ zfs_ioc_pool_reopen(zfs_cmd_t *zc)
        spa_close(spa, FTAG);
        return (0);
 }
+
 /*
  * inputs:
  * zc_name     name of filesystem
- * zc_value    name of origin snapshot
  *
  * outputs:
  * zc_string   name of conflicting snapshot, if there is one
@@ -4971,16 +5124,54 @@ zfs_ioc_pool_reopen(zfs_cmd_t *zc)
 static int
 zfs_ioc_promote(zfs_cmd_t *zc)
 {
+       dsl_pool_t *dp;
+       dsl_dataset_t *ds, *ods;
+       char origin[ZFS_MAX_DATASET_NAME_LEN];
        char *cp;
+       int error;
+
+       zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
+       if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
+           strchr(zc->zc_name, '%'))
+               return (SET_ERROR(EINVAL));
+
+       error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
+       if (error != 0)
+               return (error);
+
+       error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
+       if (error != 0) {
+               dsl_pool_rele(dp, FTAG);
+               return (error);
+       }
+
+       if (!dsl_dir_is_clone(ds->ds_dir)) {
+               dsl_dataset_rele(ds, FTAG);
+               dsl_pool_rele(dp, FTAG);
+               return (SET_ERROR(EINVAL));
+       }
+
+       error = dsl_dataset_hold_obj(dp,
+           dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
+       if (error != 0) {
+               dsl_dataset_rele(ds, FTAG);
+               dsl_pool_rele(dp, FTAG);
+               return (error);
+       }
+
+       dsl_dataset_name(ods, origin);
+       dsl_dataset_rele(ods, FTAG);
+       dsl_dataset_rele(ds, FTAG);
+       dsl_pool_rele(dp, FTAG);
 
        /*
         * We don't need to unmount *all* the origin fs's snapshots, but
         * it's easier.
         */
-       cp = strchr(zc->zc_value, '@');
+       cp = strchr(origin, '@');
        if (cp)
                *cp = '\0';
-       (void) dmu_objset_find(zc->zc_value,
+       (void) dmu_objset_find(origin,
            zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
        return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
 }
@@ -5033,17 +5224,15 @@ zfs_ioc_userspace_many(zfs_cmd_t *zc)
 {
        zfsvfs_t *zfsvfs;
        int bufsize = zc->zc_nvlist_dst_size;
-       int error;
-       void *buf;
 
        if (bufsize <= 0)
                return (SET_ERROR(ENOMEM));
 
-       error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
+       int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
        if (error != 0)
                return (error);
 
-       buf = vmem_alloc(bufsize, KM_SLEEP);
+       void *buf = vmem_alloc(bufsize, KM_SLEEP);
 
        error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
            buf, &zc->zc_nvlist_dst_size);
@@ -5086,7 +5275,7 @@ zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
                        error = zfs_suspend_fs(zfsvfs);
                        if (error == 0) {
                                dmu_objset_refresh_ownership(zfsvfs->z_os,
-                                   zfsvfs);
+                                   B_TRUE, zfsvfs);
                                error = zfs_resume_fs(zfsvfs, ds);
                        }
                }
@@ -5095,12 +5284,12 @@ zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
                deactivate_super(zfsvfs->z_sb);
        } else {
                /* XXX kind of reading contents without owning */
-               error = dmu_objset_hold(zc->zc_name, FTAG, &os);
+               error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
                if (error != 0)
                        return (error);
 
                error = dmu_objset_userspace_upgrade(os);
-               dmu_objset_rele(os, FTAG);
+               dmu_objset_rele_flags(os, B_TRUE, FTAG);
        }
 
        return (error);
@@ -5119,13 +5308,10 @@ zfs_ioc_userobjspace_upgrade(zfs_cmd_t *zc)
        objset_t *os;
        int error;
 
-       error = dmu_objset_hold(zc->zc_name, FTAG, &os);
+       error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
        if (error != 0)
                return (error);
 
-       dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
-       dsl_pool_rele(dmu_objset_pool(os), FTAG);
-
        if (dmu_objset_userobjspace_upgradable(os)) {
                mutex_enter(&os->os_upgrade_lock);
                if (os->os_upgrade_id == 0) {
@@ -5138,12 +5324,15 @@ zfs_ioc_userobjspace_upgrade(zfs_cmd_t *zc)
                        mutex_exit(&os->os_upgrade_lock);
                }
 
+               dsl_pool_rele(dmu_objset_pool(os), FTAG);
+
                taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
                error = os->os_upgrade_status;
+       } else {
+               dsl_pool_rele(dmu_objset_pool(os), FTAG);
        }
 
-       dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
-       dsl_dataset_rele(dmu_objset_ds(os), FTAG);
+       dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
 
        return (error);
 }
@@ -5466,6 +5655,7 @@ zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
 static int
 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
 {
+       ASSERT3P(args, ==, NULL);
        return (dsl_dataset_get_holds(snapname, outnvl));
 }
 
@@ -5678,6 +5868,8 @@ zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
  *     (optional) "compressok" -> (value ignored)
  *         presence indicates compressed DRR_WRITE records are permitted
+ *     (optional) "rawok" -> (value ignored)
+ *         presence indicates raw encrypted records should be used.
  *     (optional) "resume_object" and "resume_offset" -> (uint64)
  *         if present, resume send stream from specified object and offset.
  * }
@@ -5696,6 +5888,7 @@ zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
        boolean_t largeblockok;
        boolean_t embedok;
        boolean_t compressok;
+       boolean_t rawok;
        uint64_t resumeobj = 0;
        uint64_t resumeoff = 0;
 
@@ -5708,6 +5901,7 @@ zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
        largeblockok = nvlist_exists(innvl, "largeblockok");
        embedok = nvlist_exists(innvl, "embedok");
        compressok = nvlist_exists(innvl, "compressok");
+       rawok = nvlist_exists(innvl, "rawok");
 
        (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
        (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
@@ -5717,7 +5911,7 @@ zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
 
        off = fp->f_offset;
        error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
-           fd, resumeobj, resumeoff, fp->f_vnode, &off);
+           rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
 
        if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
                fp->f_offset = off;
@@ -5739,6 +5933,8 @@ zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
  *     (optional) "compressok" -> (value ignored)
  *         presence indicates compressed DRR_WRITE records are permitted
+ *     (optional) "rawok" -> (value ignored)
+ *         presence indicates raw encrypted records should be used.
  * }
  *
  * outnvl: {
@@ -5752,11 +5948,8 @@ zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
        dsl_dataset_t *tosnap;
        int error;
        char *fromname;
-       /* LINTED E_FUNC_SET_NOT_USED */
-       boolean_t largeblockok;
-       /* LINTED E_FUNC_SET_NOT_USED */
-       boolean_t embedok;
        boolean_t compressok;
+       boolean_t rawok;
        uint64_t space;
 
        error = dsl_pool_hold(snapname, FTAG, &dp);
@@ -5769,9 +5962,8 @@ zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
                return (error);
        }
 
-       largeblockok = nvlist_exists(innvl, "largeblockok");
-       embedok = nvlist_exists(innvl, "embedok");
        compressok = nvlist_exists(innvl, "compressok");
+       rawok = nvlist_exists(innvl, "rawok");
 
        error = nvlist_lookup_string(innvl, "from", &fromname);
        if (error == 0) {
@@ -5785,8 +5977,8 @@ zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
                        error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
                        if (error != 0)
                                goto out;
-                       error = dmu_send_estimate(tosnap, fromsnap, compressok,
-                           &space);
+                       error = dmu_send_estimate(tosnap, fromsnap,
+                           compressok || rawok, &space);
                        dsl_dataset_rele(fromsnap, FTAG);
                } else if (strchr(fromname, '#') != NULL) {
                        /*
@@ -5801,7 +5993,8 @@ zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
                        if (error != 0)
                                goto out;
                        error = dmu_send_estimate_from_txg(tosnap,
-                           frombm.zbm_creation_txg, compressok, &space);
+                           frombm.zbm_creation_txg, compressok || rawok,
+                           &space);
                } else {
                        /*
                         * from is not properly formatted as a snapshot or
@@ -5811,8 +6004,11 @@ zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
                        goto out;
                }
        } else {
-               // If estimating the size of a full send, use dmu_send_estimate
-               error = dmu_send_estimate(tosnap, NULL, compressok, &space);
+               /*
+                * If estimating the size of a full send, use dmu_send_estimate.
+                */
+               error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
+                   &space);
        }
 
        fnvlist_add_uint64(outnvl, "space", space);
@@ -5823,6 +6019,168 @@ out:
        return (error);
 }
 
+/*
+ * Sync the currently open TXG to disk for the specified pool.
+ * This is somewhat similar to 'zfs_sync()'.
+ * For cases that do not result in error this ioctl will wait for
+ * the currently open TXG to commit before returning back to the caller.
+ *
+ * innvl: {
+ *  "force" -> when true, force uberblock update even if there is no dirty data.
+ *             In addition this will cause the vdev configuration to be written
+ *             out including updating the zpool cache file. (boolean_t)
+ * }
+ *
+ * onvl is unused
+ */
+/* ARGSUSED */
+static int
+zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
+{
+       int err;
+       boolean_t force = B_FALSE;
+       spa_t *spa;
+
+       if ((err = spa_open(pool, &spa, FTAG)) != 0)
+               return (err);
+
+       if (innvl) {
+               if (nvlist_lookup_boolean_value(innvl, "force", &force) != 0) {
+                       err = SET_ERROR(EINVAL);
+                       goto out;
+               }
+       }
+
+       if (force) {
+               spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
+               vdev_config_dirty(spa->spa_root_vdev);
+               spa_config_exit(spa, SCL_CONFIG, FTAG);
+       }
+       txg_wait_synced(spa_get_dsl(spa), 0);
+out:
+       spa_close(spa, FTAG);
+
+       return (err);
+}
+
+/*
+ * Load a user's wrapping key into the kernel.
+ * innvl: {
+ *     "hidden_args" -> { "wkeydata" -> value }
+ *         raw uint8_t array of encryption wrapping key data (32 bytes)
+ *     (optional) "noop" -> (value ignored)
+ *         presence indicated key should only be verified, not loaded
+ * }
+ */
+/* ARGSUSED */
+static int
+zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
+{
+       int ret;
+       dsl_crypto_params_t *dcp = NULL;
+       nvlist_t *hidden_args;
+       boolean_t noop = nvlist_exists(innvl, "noop");
+
+       if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
+               ret = SET_ERROR(EINVAL);
+               goto error;
+       }
+
+       ret = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
+       if (ret != 0) {
+               ret = SET_ERROR(EINVAL);
+               goto error;
+       }
+
+       ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
+           hidden_args, &dcp);
+       if (ret != 0)
+               goto error;
+
+       ret = spa_keystore_load_wkey(dsname, dcp, noop);
+       if (ret != 0)
+               goto error;
+
+       dsl_crypto_params_free(dcp, noop);
+
+       return (0);
+
+error:
+       dsl_crypto_params_free(dcp, B_TRUE);
+       return (ret);
+}
+
+/*
+ * Unload a user's wrapping key from the kernel.
+ * Both innvl and outnvl are unused.
+ */
+/* ARGSUSED */
+static int
+zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
+{
+       int ret = 0;
+
+       if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
+               ret = (SET_ERROR(EINVAL));
+               goto out;
+       }
+
+       ret = spa_keystore_unload_wkey(dsname);
+       if (ret != 0)
+               goto out;
+
+out:
+       return (ret);
+}
+
+/*
+ * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
+ * keylocation, pbkdf2salt, and  pbkdf2iters properties can also be specified
+ * here to change how the key is derived in userspace.
+ *
+ * innvl: {
+ *    "hidden_args" (optional) -> { "wkeydata" -> value }
+ *         raw uint8_t array of new encryption wrapping key data (32 bytes)
+ *    "props" (optional) -> { prop -> value }
+ * }
+ *
+ * outnvl is unused
+ */
+/* ARGSUSED */
+static int
+zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
+{
+       int ret;
+       uint64_t cmd = DCP_CMD_NONE;
+       dsl_crypto_params_t *dcp = NULL;
+       nvlist_t *args = NULL, *hidden_args = NULL;
+
+       if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
+               ret = (SET_ERROR(EINVAL));
+               goto error;
+       }
+
+       (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
+       (void) nvlist_lookup_nvlist(innvl, "props", &args);
+       (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
+
+       ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
+       if (ret != 0)
+               goto error;
+
+       ret = spa_keystore_change_key(dsname, dcp);
+       if (ret != 0)
+               goto error;
+
+       dsl_crypto_params_free(dcp, B_FALSE);
+
+       return (0);
+
+error:
+       dsl_crypto_params_free(dcp, B_TRUE);
+       return (ret);
+}
+
 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
 
 static void
@@ -5994,6 +6352,28 @@ zfs_ioctl_init(void)
        zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
            zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
            POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
+       zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
+           zfs_ioc_load_key, zfs_secpolicy_load_key,
+           DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
+       zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
+           zfs_ioc_unload_key, zfs_secpolicy_load_key,
+           DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
+       zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
+           zfs_ioc_change_key, zfs_secpolicy_change_key,
+           DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
+           B_TRUE, B_TRUE);
+
+       zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
+           zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
+       zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
+           zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
+           B_TRUE);
+
+       zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
+           zfs_ioc_channel_program, zfs_secpolicy_config,
+           POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
+           B_TRUE);
 
        /* IOCTLS that use the legacy function signature */
 
@@ -6066,9 +6446,7 @@ zfs_ioctl_init(void)
            zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
 
        zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
-           zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
-       zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
-           zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
+           zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
 
        zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
            zfs_ioc_space_written);
@@ -6204,7 +6582,7 @@ zfsdev_getminor(struct file *filp, minor_t *minorp)
 
        fpd = filp->private_data;
        if (fpd == NULL)
-               return (EBADF);
+               return (SET_ERROR(EBADF));
 
        mutex_enter(&zfsdev_state_lock);
 
@@ -6222,7 +6600,7 @@ zfsdev_getminor(struct file *filp, minor_t *minorp)
 
        mutex_exit(&zfsdev_state_lock);
 
-       return (EBADF);
+       return (SET_ERROR(EBADF));
 }
 
 /*
@@ -6467,12 +6845,23 @@ zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
                error = vec->zvec_func(zc->zc_name, innvl, outnvl);
                spl_fstrans_unmark(cookie);
 
-               if (error == 0 && vec->zvec_allow_log &&
+               /*
+                * Some commands can partially execute, modify state, and still
+                * return an error.  In these cases, attempt to record what
+                * was modified.
+                */
+               if ((error == 0 ||
+                   (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
+                   vec->zvec_allow_log &&
                    spa_open(zc->zc_name, &spa, FTAG) == 0) {
                        if (!nvlist_empty(outnvl)) {
                                fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
                                    outnvl);
                        }
+                       if (error != 0) {
+                               fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
+                                   error);
+                       }
                        (void) spa_history_log_nvl(spa, lognv);
                        spa_close(spa, FTAG);
                }