]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zfs_ioctl.c
cstyle: Resolve C style issues
[mirror_zfs.git] / module / zfs / zfs_ioctl.c
index d032648b54ebdb1850b4cb3cc5e4a7dc53aa12a4..bf212dee82eb94ddc5f9516f704b57bace63e533 100644 (file)
  *
  * CDDL HEADER END
  */
+
+/*
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Portions Copyright 2011 Martin Matuska
+ * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
+ * Copyright (c) 2012, Joyent, Inc. All rights reserved.
+ * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright (c) 2012, Joyent, Inc. All rights reserved.
+ * Copyright (c) 201i3 by Delphix. All rights reserved.
+ * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
+ * Copyright (c) 2013 Steven Hartland. All rights reserved.
+ */
+
 /*
- * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
- * Use is subject to license terms.
+ * ZFS ioctls.
+ *
+ * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
+ * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
+ *
+ * There are two ways that we handle ioctls: the legacy way where almost
+ * all of the logic is in the ioctl callback, and the new way where most
+ * of the marshalling is handled in the common entry point, zfsdev_ioctl().
+ *
+ * Non-legacy ioctls should be registered by calling
+ * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
+ * from userland by lzc_ioctl().
+ *
+ * The registration arguments are as follows:
+ *
+ * const char *name
+ *   The name of the ioctl.  This is used for history logging.  If the
+ *   ioctl returns successfully (the callback returns 0), and allow_log
+ *   is true, then a history log entry will be recorded with the input &
+ *   output nvlists.  The log entry can be printed with "zpool history -i".
+ *
+ * zfs_ioc_t ioc
+ *   The ioctl request number, which userland will pass to ioctl(2).
+ *   The ioctl numbers can change from release to release, because
+ *   the caller (libzfs) must be matched to the kernel.
+ *
+ * zfs_secpolicy_func_t *secpolicy
+ *   This function will be called before the zfs_ioc_func_t, to
+ *   determine if this operation is permitted.  It should return EPERM
+ *   on failure, and 0 on success.  Checks include determining if the
+ *   dataset is visible in this zone, and if the user has either all
+ *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
+ *   to do this operation on this dataset with "zfs allow".
+ *
+ * zfs_ioc_namecheck_t namecheck
+ *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
+ *   name, a dataset name, or nothing.  If the name is not well-formed,
+ *   the ioctl will fail and the callback will not be called.
+ *   Therefore, the callback can assume that the name is well-formed
+ *   (e.g. is null-terminated, doesn't have more than one '@' character,
+ *   doesn't have invalid characters).
+ *
+ * zfs_ioc_poolcheck_t pool_check
+ *   This specifies requirements on the pool state.  If the pool does
+ *   not meet them (is suspended or is readonly), the ioctl will fail
+ *   and the callback will not be called.  If any checks are specified
+ *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
+ *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
+ *   POOL_CHECK_READONLY).
+ *
+ * boolean_t smush_outnvlist
+ *   If smush_outnvlist is true, then the output is presumed to be a
+ *   list of errors, and it will be "smushed" down to fit into the
+ *   caller's buffer, by removing some entries and replacing them with a
+ *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
+ *   nvlist_smush() for details.  If smush_outnvlist is false, and the
+ *   outnvlist does not fit into the userland-provided buffer, then the
+ *   ioctl will fail with ENOMEM.
+ *
+ * zfs_ioc_func_t *func
+ *   The callback function that will perform the operation.
+ *
+ *   The callback should return 0 on success, or an error number on
+ *   failure.  If the function fails, the userland ioctl will return -1,
+ *   and errno will be set to the callback's return value.  The callback
+ *   will be called with the following arguments:
+ *
+ *   const char *name
+ *     The name of the pool or dataset to operate on, from
+ *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
+ *     expected type (pool, dataset, or none).
+ *
+ *   nvlist_t *innvl
+ *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
+ *     NULL if no input nvlist was provided.  Changes to this nvlist are
+ *     ignored.  If the input nvlist could not be deserialized, the
+ *     ioctl will fail and the callback will not be called.
+ *
+ *   nvlist_t *outnvl
+ *     The output nvlist, initially empty.  The callback can fill it in,
+ *     and it will be returned to userland by serializing it into
+ *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
+ *     fails (e.g. because the caller didn't supply a large enough
+ *     buffer), then the overall ioctl will fail.  See the
+ *     'smush_nvlist' argument above for additional behaviors.
+ *
+ *     There are two typical uses of the output nvlist:
+ *       - To return state, e.g. property values.  In this case,
+ *         smush_outnvlist should be false.  If the buffer was not large
+ *         enough, the caller will reallocate a larger buffer and try
+ *         the ioctl again.
+ *
+ *       - To return multiple errors from an ioctl which makes on-disk
+ *         changes.  In this case, smush_outnvlist should be true.
+ *         Ioctls which make on-disk modifications should generally not
+ *         use the outnvl if they succeed, because the caller can not
+ *         distinguish between the operation failing, and
+ *         deserialization failing.
  */
 
 #include <sys/types.h>
 #include <sys/cmn_err.h>
 #include <sys/stat.h>
 #include <sys/zfs_ioctl.h>
+#include <sys/zfs_vfsops.h>
 #include <sys/zfs_znode.h>
 #include <sys/zap.h>
 #include <sys/spa.h>
 #include <sys/spa_impl.h>
 #include <sys/vdev.h>
-#include <sys/vdev_impl.h>
+#include <sys/priv_impl.h>
 #include <sys/dmu.h>
 #include <sys/dsl_dir.h>
 #include <sys/dsl_dataset.h>
 #include <sys/dsl_prop.h>
 #include <sys/dsl_deleg.h>
 #include <sys/dmu_objset.h>
+#include <sys/dmu_impl.h>
+#include <sys/dmu_tx.h>
 #include <sys/ddi.h>
 #include <sys/sunddi.h>
 #include <sys/sunldi.h>
 #include <sys/fs/zfs.h>
 #include <sys/zfs_ctldir.h>
 #include <sys/zfs_dir.h>
+#include <sys/zfs_onexit.h>
 #include <sys/zvol.h>
+#include <sys/dsl_scan.h>
 #include <sharefs/share.h>
-#include <sys/dmu_objset.h>
+#include <sys/fm/util.h>
+
+#include <sys/dmu_send.h>
+#include <sys/dsl_destroy.h>
+#include <sys/dsl_userhold.h>
+#include <sys/zfeature.h>
+
+#include <linux/miscdevice.h>
 
 #include "zfs_namecheck.h"
 #include "zfs_prop.h"
 #include "zfs_deleg.h"
+#include "zfs_comutil.h"
 
-extern struct modlfs zfs_modlfs;
+kmutex_t zfsdev_state_lock;
+list_t zfsdev_state_list;
 
 extern void zfs_init(void);
 extern void zfs_fini(void);
 
-ldi_ident_t zfs_li = NULL;
-dev_info_t *zfs_dip;
+uint_t zfs_fsyncer_key;
+extern uint_t rrw_tsd_key;
+static uint_t zfs_allow_log_key;
+
+typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
+typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
+typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
+
+typedef enum {
+       NO_NAME,
+       POOL_NAME,
+       DATASET_NAME
+} zfs_ioc_namecheck_t;
 
-typedef int zfs_ioc_func_t(zfs_cmd_t *);
-typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
+typedef enum {
+       POOL_CHECK_NONE         = 1 << 0,
+       POOL_CHECK_SUSPENDED    = 1 << 1,
+       POOL_CHECK_READONLY     = 1 << 2,
+} zfs_ioc_poolcheck_t;
 
 typedef struct zfs_ioc_vec {
+       zfs_ioc_legacy_func_t   *zvec_legacy_func;
        zfs_ioc_func_t          *zvec_func;
        zfs_secpolicy_func_t    *zvec_secpolicy;
-       enum {
-               NO_NAME,
-               POOL_NAME,
-               DATASET_NAME
-       } zvec_namecheck;
-       boolean_t               zvec_his_log;
+       zfs_ioc_namecheck_t     zvec_namecheck;
+       boolean_t               zvec_allow_log;
+       zfs_ioc_poolcheck_t     zvec_pool_check;
+       boolean_t               zvec_smush_outnvlist;
+       const char              *zvec_name;
 } zfs_ioc_vec_t;
 
-static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops);
+/* This array is indexed by zfs_userquota_prop_t */
+static const char *userquota_perms[] = {
+       ZFS_DELEG_PERM_USERUSED,
+       ZFS_DELEG_PERM_USERQUOTA,
+       ZFS_DELEG_PERM_GROUPUSED,
+       ZFS_DELEG_PERM_GROUPQUOTA,
+};
+
+static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
+static int zfs_check_settable(const char *name, nvpair_t *property,
+    cred_t *cr);
+static int zfs_check_clearable(char *dataset, nvlist_t *props,
+    nvlist_t **errors);
 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
     boolean_t *);
-int zfs_set_prop_nvlist(const char *, nvlist_t *);
-
-/* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
-void
-__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
-{
-       const char *newfile;
-       char buf[256];
-       va_list adx;
-
-       /*
-        * Get rid of annoying "../common/" prefix to filename.
-        */
-       newfile = strrchr(file, '/');
-       if (newfile != NULL) {
-               newfile = newfile + 1; /* Get rid of leading / */
-       } else {
-               newfile = file;
-       }
+int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
+static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
 
-       va_start(adx, fmt);
-       (void) vsnprintf(buf, sizeof (buf), fmt, adx);
-       va_end(adx);
-
-       /*
-        * To get this data, use the zfs-dprintf probe as so:
-        * dtrace -q -n 'zfs-dprintf \
-        *      /stringof(arg0) == "dbuf.c"/ \
-        *      {printf("%s: %s", stringof(arg1), stringof(arg3))}'
-        * arg0 = file name
-        * arg1 = function name
-        * arg2 = line number
-        * arg3 = message
-        */
-       DTRACE_PROBE4(zfs__dprintf,
-           char *, newfile, char *, func, int, line, char *, buf);
-}
+static int zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature);
 
 static void
 history_str_free(char *buf)
@@ -142,10 +258,10 @@ history_str_get(zfs_cmd_t *zc)
 {
        char *buf;
 
-       if (zc->zc_history == NULL)
+       if (zc->zc_history == 0)
                return (NULL);
 
-       buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
+       buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP | KM_NODEBUG);
        if (copyinstr((void *)(uintptr_t)zc->zc_history,
            buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
                history_str_free(buf);
@@ -163,28 +279,19 @@ history_str_get(zfs_cmd_t *zc)
 static boolean_t
 zfs_is_bootfs(const char *name)
 {
-       spa_t *spa;
-       boolean_t ret = B_FALSE;
-
-       if (spa_open(name, &spa, FTAG) == 0) {
-               if (spa->spa_bootfs) {
-                       objset_t *os;
+       objset_t *os;
 
-                       if (dmu_objset_open(name, DMU_OST_ZFS,
-                           DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
-                               ret = (dmu_objset_id(os) == spa->spa_bootfs);
-                               dmu_objset_close(os);
-                       }
-               }
-               spa_close(spa, FTAG);
+       if (dmu_objset_hold(name, FTAG, &os) == 0) {
+               boolean_t ret;
+               ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
+               dmu_objset_rele(os, FTAG);
+               return (ret);
        }
-       return (ret);
+       return (B_FALSE);
 }
 
 /*
- * zfs_earlier_version
- *
- *     Return non-zero if the spa version is less than requested version.
+ * Return non-zero if the spa version is less than requested version.
  */
 static int
 zfs_earlier_version(const char *name, int version)
@@ -202,8 +309,6 @@ zfs_earlier_version(const char *name, int version)
 }
 
 /*
- * zpl_earlier_version
- *
  * Return TRUE if the ZPL version is less than requested version.
  */
 static boolean_t
@@ -212,13 +317,17 @@ zpl_earlier_version(const char *name, int version)
        objset_t *os;
        boolean_t rc = B_TRUE;
 
-       if (dmu_objset_open(name, DMU_OST_ANY,
-           DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
+       if (dmu_objset_hold(name, FTAG, &os) == 0) {
                uint64_t zplversion;
 
+               if (dmu_objset_type(os) != DMU_OST_ZFS) {
+                       dmu_objset_rele(os, FTAG);
+                       return (B_TRUE);
+               }
+               /* XXX reading from non-owned objset */
                if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
                        rc = zplversion < version;
-               dmu_objset_close(os);
+               dmu_objset_rele(os, FTAG);
        }
        return (rc);
 }
@@ -234,7 +343,7 @@ zfs_log_history(zfs_cmd_t *zc)
 
        if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
                if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
-                       (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
+                       (void) spa_history_log(spa, buf);
                spa_close(spa, FTAG);
        }
        history_str_free(buf);
@@ -246,7 +355,7 @@ zfs_log_history(zfs_cmd_t *zc)
  */
 /* ARGSUSED */
 static int
-zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        return (0);
 }
@@ -257,19 +366,18 @@ zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
  */
 /* ARGSUSED */
 static int
-zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        if (INGLOBALZONE(curproc) ||
            zone_dataset_visible(zc->zc_name, NULL))
                return (0);
 
-       return (ENOENT);
+       return (SET_ERROR(ENOENT));
 }
 
 static int
-zfs_dozonecheck(const char *dataset, cred_t *cr)
+zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
 {
-       uint64_t zoned;
        int writable = 1;
 
        /*
@@ -278,10 +386,7 @@ zfs_dozonecheck(const char *dataset, cred_t *cr)
         */
        if (!INGLOBALZONE(curproc) &&
            !zone_dataset_visible(dataset, &writable))
-               return (ENOENT);
-
-       if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
-               return (ENOENT);
+               return (SET_ERROR(ENOENT));
 
        if (INGLOBALZONE(curproc)) {
                /*
@@ -289,48 +394,201 @@ zfs_dozonecheck(const char *dataset, cred_t *cr)
                 * global zone.
                 */
                if (secpolicy_zfs(cr) && zoned)
-                       return (EPERM);
+                       return (SET_ERROR(EPERM));
        } else {
                /*
                 * If we are in a local zone, the 'zoned' property must be set.
                 */
                if (!zoned)
-                       return (EPERM);
+                       return (SET_ERROR(EPERM));
 
                /* must be writable by this zone */
                if (!writable)
-                       return (EPERM);
+                       return (SET_ERROR(EPERM));
        }
        return (0);
 }
 
-int
-zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
+static int
+zfs_dozonecheck(const char *dataset, cred_t *cr)
+{
+       uint64_t zoned;
+
+       if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
+               return (SET_ERROR(ENOENT));
+
+       return (zfs_dozonecheck_impl(dataset, zoned, cr));
+}
+
+static int
+zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
+{
+       uint64_t zoned;
+
+       if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
+               return (SET_ERROR(ENOENT));
+
+       return (zfs_dozonecheck_impl(dataset, zoned, cr));
+}
+
+static int
+zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
+    const char *perm, cred_t *cr)
 {
        int error;
 
-       error = zfs_dozonecheck(name, cr);
+       error = zfs_dozonecheck_ds(name, ds, cr);
        if (error == 0) {
                error = secpolicy_zfs(cr);
-               if (error)
-                       error = dsl_deleg_access(name, perm, cr);
+               if (error != 0)
+                       error = dsl_deleg_access_impl(ds, perm, cr);
+       }
+       return (error);
+}
+
+static int
+zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
+{
+       int error;
+       dsl_dataset_t *ds;
+       dsl_pool_t *dp;
+
+       error = dsl_pool_hold(name, FTAG, &dp);
+       if (error != 0)
+               return (error);
+
+       error = dsl_dataset_hold(dp, name, FTAG, &ds);
+       if (error != 0) {
+               dsl_pool_rele(dp, FTAG);
+               return (error);
        }
+
+       error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
+
+       dsl_dataset_rele(ds, FTAG);
+       dsl_pool_rele(dp, FTAG);
        return (error);
 }
 
+/*
+ * Policy for setting the security label property.
+ *
+ * Returns 0 for success, non-zero for access and other errors.
+ */
+static int
+zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
+{
+#ifdef HAVE_MLSLABEL
+       char            ds_hexsl[MAXNAMELEN];
+       bslabel_t       ds_sl, new_sl;
+       boolean_t       new_default = FALSE;
+       uint64_t        zoned;
+       int             needed_priv = -1;
+       int             error;
+
+       /* First get the existing dataset label. */
+       error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
+           1, sizeof (ds_hexsl), &ds_hexsl, NULL);
+       if (error != 0)
+               return (SET_ERROR(EPERM));
+
+       if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
+               new_default = TRUE;
+
+       /* The label must be translatable */
+       if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
+               return (SET_ERROR(EINVAL));
+
+       /*
+        * In a non-global zone, disallow attempts to set a label that
+        * doesn't match that of the zone; otherwise no other checks
+        * are needed.
+        */
+       if (!INGLOBALZONE(curproc)) {
+               if (new_default || !blequal(&new_sl, CR_SL(CRED())))
+                       return (SET_ERROR(EPERM));
+               return (0);
+       }
+
+       /*
+        * For global-zone datasets (i.e., those whose zoned property is
+        * "off", verify that the specified new label is valid for the
+        * global zone.
+        */
+       if (dsl_prop_get_integer(name,
+           zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
+               return (SET_ERROR(EPERM));
+       if (!zoned) {
+               if (zfs_check_global_label(name, strval) != 0)
+                       return (SET_ERROR(EPERM));
+       }
+
+       /*
+        * If the existing dataset label is nondefault, check if the
+        * dataset is mounted (label cannot be changed while mounted).
+        * Get the zfs_sb_t; if there isn't one, then the dataset isn't
+        * mounted (or isn't a dataset, doesn't exist, ...).
+        */
+       if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
+               objset_t *os;
+               static char *setsl_tag = "setsl_tag";
+
+               /*
+                * 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,
+                   setsl_tag, &os);
+               if (error != 0)
+                       return (SET_ERROR(EPERM));
+
+               dmu_objset_disown(os, setsl_tag);
+
+               if (new_default) {
+                       needed_priv = PRIV_FILE_DOWNGRADE_SL;
+                       goto out_check;
+               }
+
+               if (hexstr_to_label(strval, &new_sl) != 0)
+                       return (SET_ERROR(EPERM));
+
+               if (blstrictdom(&ds_sl, &new_sl))
+                       needed_priv = PRIV_FILE_DOWNGRADE_SL;
+               else if (blstrictdom(&new_sl, &ds_sl))
+                       needed_priv = PRIV_FILE_UPGRADE_SL;
+       } else {
+               /* dataset currently has a default label */
+               if (!new_default)
+                       needed_priv = PRIV_FILE_UPGRADE_SL;
+       }
+
+out_check:
+       if (needed_priv != -1)
+               return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
+       return (0);
+#else
+       return (ENOTSUP);
+#endif /* HAVE_MLSLABEL */
+}
+
 static int
-zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
+zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
+    cred_t *cr)
 {
+       char *strval;
+
        /*
         * Check permissions for special properties.
         */
        switch (prop) {
+       default:
+               break;
        case ZFS_PROP_ZONED:
                /*
                 * Disallow setting of 'zoned' from within a local zone.
                 */
                if (!INGLOBALZONE(curproc))
-                       return (EPERM);
+                       return (SET_ERROR(EPERM));
                break;
 
        case ZFS_PROP_QUOTA:
@@ -342,25 +600,39 @@ zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
                         * quota on things *under* (ie. contained by)
                         * the thing they own.
                         */
-                       if (dsl_prop_get_integer(name, "zoned", &zoned,
+                       if (dsl_prop_get_integer(dsname, "zoned", &zoned,
                            setpoint))
-                               return (EPERM);
-                       if (!zoned || strlen(name) <= strlen(setpoint))
-                               return (EPERM);
+                               return (SET_ERROR(EPERM));
+                       if (!zoned || strlen(dsname) <= strlen(setpoint))
+                               return (SET_ERROR(EPERM));
+               }
+               break;
+
+       case ZFS_PROP_MLSLABEL:
+               if (!is_system_labeled())
+                       return (SET_ERROR(EPERM));
+
+               if (nvpair_value_string(propval, &strval) == 0) {
+                       int err;
+
+                       err = zfs_set_slabel_policy(dsname, strval, CRED());
+                       if (err != 0)
+                               return (err);
                }
                break;
        }
 
-       return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
+       return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
 }
 
-int
-zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
+/* ARGSUSED */
+static int
+zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        int error;
 
        error = zfs_dozonecheck(zc->zc_name, cr);
-       if (error)
+       if (error != 0)
                return (error);
 
        /*
@@ -370,54 +642,117 @@ zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
        return (0);
 }
 
-int
-zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
+/* ARGSUSED */
+static int
+zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       return (zfs_secpolicy_write_perms(zc->zc_name,
+           ZFS_DELEG_PERM_ROLLBACK, cr));
+}
+
+/* ARGSUSED */
+static int
+zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
+       dsl_pool_t *dp;
+       dsl_dataset_t *ds;
+       char *cp;
        int error;
-       error = zfs_secpolicy_write_perms(zc->zc_name,
-           ZFS_DELEG_PERM_ROLLBACK, cr);
-       if (error == 0)
-               error = zfs_secpolicy_write_perms(zc->zc_name,
-                   ZFS_DELEG_PERM_MOUNT, cr);
+
+       /*
+        * Generate the current snapshot name from the given objsetid, then
+        * use that name for the secpolicy/zone checks.
+        */
+       cp = strchr(zc->zc_name, '@');
+       if (cp == NULL)
+               return (SET_ERROR(EINVAL));
+       error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
+       if (error != 0)
+               return (error);
+
+       error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
+       if (error != 0) {
+               dsl_pool_rele(dp, FTAG);
+               return (error);
+       }
+
+       dsl_dataset_name(ds, zc->zc_name);
+
+       error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
+           ZFS_DELEG_PERM_SEND, cr);
+       dsl_dataset_rele(ds, FTAG);
+       dsl_pool_rele(dp, FTAG);
+
        return (error);
 }
 
-int
-zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
+/* ARGSUSED */
+static int
+zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        return (zfs_secpolicy_write_perms(zc->zc_name,
            ZFS_DELEG_PERM_SEND, cr));
 }
 
+#ifdef HAVE_SMB_SHARE
+/* ARGSUSED */
+static int
+zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       vnode_t *vp;
+       int error;
+
+       if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
+           NO_FOLLOW, NULL, &vp)) != 0)
+               return (error);
+
+       /* Now make sure mntpnt and dataset are ZFS */
+
+       if (vp->v_vfsp->vfs_fstype != zfsfstype ||
+           (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
+           zc->zc_name) != 0)) {
+               VN_RELE(vp);
+               return (SET_ERROR(EPERM));
+       }
+
+       VN_RELE(vp);
+       return (dsl_deleg_access(zc->zc_name,
+           ZFS_DELEG_PERM_SHARE, cr));
+}
+#endif /* HAVE_SMB_SHARE */
+
 int
-zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
+#ifdef HAVE_SMB_SHARE
        if (!INGLOBALZONE(curproc))
-               return (EPERM);
+               return (SET_ERROR(EPERM));
 
        if (secpolicy_nfs(cr) == 0) {
                return (0);
        } else {
-               vnode_t *vp;
-               int error;
-
-               if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
-                   NO_FOLLOW, NULL, &vp)) != 0)
-                       return (error);
-
-               /* Now make sure mntpnt and dataset are ZFS */
+               return (zfs_secpolicy_deleg_share(zc, innvl, cr));
+       }
+#else
+       return (SET_ERROR(ENOTSUP));
+#endif /* HAVE_SMB_SHARE */
+}
 
-               if (vp->v_vfsp->vfs_fstype != zfsfstype ||
-                   (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
-                   zc->zc_name) != 0)) {
-                       VN_RELE(vp);
-                       return (EPERM);
-               }
+int
+zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+#ifdef HAVE_SMB_SHARE
+       if (!INGLOBALZONE(curproc))
+               return (SET_ERROR(EPERM));
 
-               VN_RELE(vp);
-               return (dsl_deleg_access(zc->zc_name,
-                   ZFS_DELEG_PERM_SHARE, cr));
+       if (secpolicy_smb(cr) == 0) {
+               return (0);
+       } else {
+               return (zfs_secpolicy_deleg_share(zc, innvl, cr));
        }
+#else
+       return (SET_ERROR(ENOTSUP));
+#endif /* HAVE_SMB_SHARE */
 }
 
 static int
@@ -435,7 +770,7 @@ zfs_get_parent(const char *datasetname, char *parent, int parentsize)
        } else {
                cp = strrchr(parent, '/');
                if (cp == NULL)
-                       return (ENOENT);
+                       return (SET_ERROR(ENOENT));
                cp[0] = '\0';
        }
 
@@ -454,26 +789,67 @@ zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
        return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
 }
 
+/* ARGSUSED */
 static int
-zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
 }
 
 /*
- * Must have sys_config privilege to check the iscsi permission
+ * Destroying snapshots with delegated permissions requires
+ * descendant mount and destroy permissions.
  */
 /* ARGSUSED */
 static int
-zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
-       return (secpolicy_zfs(cr));
+       nvlist_t *snaps;
+       nvpair_t *pair, *nextpair;
+       int error = 0;
+
+       if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
+               return (SET_ERROR(EINVAL));
+       for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
+           pair = nextpair) {
+               dsl_pool_t *dp;
+               dsl_dataset_t *ds;
+
+               error = dsl_pool_hold(nvpair_name(pair), FTAG, &dp);
+               if (error != 0)
+                       break;
+               nextpair = nvlist_next_nvpair(snaps, pair);
+               error = dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds);
+               if (error == 0)
+                       dsl_dataset_rele(ds, FTAG);
+               dsl_pool_rele(dp, FTAG);
+
+               if (error == 0) {
+                       error = zfs_secpolicy_destroy_perms(nvpair_name(pair),
+                           cr);
+               } else if (error == ENOENT) {
+                       /*
+                        * Ignore any snapshots that don't exist (we consider
+                        * them "already destroyed").  Remove the name from the
+                        * nvl here in case the snapshot is created between
+                        * now and when we try to destroy it (in which case
+                        * we don't want to destroy it since we haven't
+                        * checked for permission).
+                        */
+                       fnvlist_remove_nvpair(snaps, pair);
+                       error = 0;
+               }
+               if (error != 0)
+                       break;
+       }
+
+       return (error);
 }
 
 int
 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
 {
-       char    parentname[MAXNAMELEN];
+       char    parentname[MAXNAMELEN];
        int     error;
 
        if ((error = zfs_secpolicy_write_perms(from,
@@ -499,56 +875,64 @@ zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
        return (error);
 }
 
+/* ARGSUSED */
 static int
-zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
 }
 
+/* ARGSUSED */
 static int
-zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
-       char    parentname[MAXNAMELEN];
-       objset_t *clone;
+       dsl_pool_t *dp;
+       dsl_dataset_t *clone;
        int error;
 
        error = zfs_secpolicy_write_perms(zc->zc_name,
            ZFS_DELEG_PERM_PROMOTE, cr);
-       if (error)
+       if (error != 0)
+               return (error);
+
+       error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
+       if (error != 0)
                return (error);
 
-       error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
-           DS_MODE_USER | DS_MODE_READONLY, &clone);
+       error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
 
        if (error == 0) {
-               dsl_dataset_t *pclone = NULL;
+               char parentname[MAXNAMELEN];
+               dsl_dataset_t *origin = NULL;
                dsl_dir_t *dd;
-               dd = clone->os->os_dsl_dataset->ds_dir;
+               dd = clone->ds_dir;
 
-               rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
                error = dsl_dataset_hold_obj(dd->dd_pool,
-                   dd->dd_phys->dd_origin_obj, FTAG, &pclone);
-               rw_exit(&dd->dd_pool->dp_config_rwlock);
-               if (error) {
-                       dmu_objset_close(clone);
+                   dd->dd_phys->dd_origin_obj, FTAG, &origin);
+               if (error != 0) {
+                       dsl_dataset_rele(clone, FTAG);
+                       dsl_pool_rele(dp, FTAG);
                        return (error);
                }
 
-               error = zfs_secpolicy_write_perms(zc->zc_name,
+               error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
                    ZFS_DELEG_PERM_MOUNT, cr);
 
-               dsl_dataset_name(pclone, parentname);
-               dmu_objset_close(clone);
-               dsl_dataset_rele(pclone, FTAG);
-               if (error == 0)
-                       error = zfs_secpolicy_write_perms(parentname,
+               dsl_dataset_name(origin, parentname);
+               if (error == 0) {
+                       error = zfs_secpolicy_write_perms_ds(parentname, origin,
                            ZFS_DELEG_PERM_PROMOTE, cr);
+               }
+               dsl_dataset_rele(clone, FTAG);
+               dsl_dataset_rele(origin, FTAG);
        }
+       dsl_pool_rele(dp, FTAG);
        return (error);
 }
 
+/* ARGSUSED */
 static int
-zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        int error;
 
@@ -567,61 +951,76 @@ zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
 int
 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
 {
-       int error;
+       return (zfs_secpolicy_write_perms(name,
+           ZFS_DELEG_PERM_SNAPSHOT, cr));
+}
 
-       if ((error = zfs_secpolicy_write_perms(name,
-           ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
-               return (error);
+/*
+ * Check for permission to create each snapshot in the nvlist.
+ */
+/* ARGSUSED */
+static int
+zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       nvlist_t *snaps;
+       int error = 0;
+       nvpair_t *pair;
 
-       error = zfs_secpolicy_write_perms(name,
-           ZFS_DELEG_PERM_MOUNT, cr);
+       if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
+               return (SET_ERROR(EINVAL));
+       for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
+           pair = nvlist_next_nvpair(snaps, pair)) {
+               char *name = nvpair_name(pair);
+               char *atp = strchr(name, '@');
 
+               if (atp == NULL) {
+                       error = SET_ERROR(EINVAL);
+                       break;
+               }
+               *atp = '\0';
+               error = zfs_secpolicy_snapshot_perms(name, cr);
+               *atp = '@';
+               if (error != 0)
+                       break;
+       }
        return (error);
 }
 
+/* ARGSUSED */
 static int
-zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
-
-       return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
+       /*
+        * Even root must have a proper TSD so that we know what pool
+        * to log to.
+        */
+       if (tsd_get(zfs_allow_log_key) == NULL)
+               return (SET_ERROR(EPERM));
+       return (0);
 }
 
 static int
-zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
-       char    parentname[MAXNAMELEN];
-       int     error;
+       char    parentname[MAXNAMELEN];
+       int     error;
+       char    *origin;
 
        if ((error = zfs_get_parent(zc->zc_name, parentname,
            sizeof (parentname))) != 0)
                return (error);
 
-       if (zc->zc_value[0] != '\0') {
-               if ((error = zfs_secpolicy_write_perms(zc->zc_value,
-                   ZFS_DELEG_PERM_CLONE, cr)) != 0)
-                       return (error);
-       }
+       if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
+           (error = zfs_secpolicy_write_perms(origin,
+           ZFS_DELEG_PERM_CLONE, cr)) != 0)
+               return (error);
 
        if ((error = zfs_secpolicy_write_perms(parentname,
            ZFS_DELEG_PERM_CREATE, cr)) != 0)
                return (error);
 
-       error = zfs_secpolicy_write_perms(parentname,
-           ZFS_DELEG_PERM_MOUNT, cr);
-
-       return (error);
-}
-
-static int
-zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
-{
-       int error;
-
-       error = secpolicy_fs_unmount(cr, NULL);
-       if (error) {
-               error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
-       }
-       return (error);
+       return (zfs_secpolicy_write_perms(parentname,
+           ZFS_DELEG_PERM_MOUNT, cr));
 }
 
 /*
@@ -630,62 +1029,187 @@ zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
  */
 /* ARGSUSED */
 static int
-zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        if (secpolicy_sys_config(cr, B_FALSE) != 0)
-               return (EPERM);
+               return (SET_ERROR(EPERM));
 
        return (0);
 }
 
 /*
- * Just like zfs_secpolicy_config, except that we will check for
- * mount permission on the dataset for permission to create/remove
- * the minor nodes.
+ * Policy for object to name lookups.
  */
+/* ARGSUSED */
 static int
-zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
-       if (secpolicy_sys_config(cr, B_FALSE) != 0) {
-               return (dsl_deleg_access(zc->zc_name,
-                   ZFS_DELEG_PERM_MOUNT, cr));
-       }
+       int error;
 
-       return (0);
-}
+       if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
+               return (0);
+
+       error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
+       return (error);
+}
 
 /*
  * Policy for fault injection.  Requires all privileges.
  */
 /* ARGSUSED */
 static int
-zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        return (secpolicy_zinject(cr));
 }
 
+/* ARGSUSED */
 static int
-zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
+zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 {
        zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
 
        if (prop == ZPROP_INVAL) {
                if (!zfs_prop_user(zc->zc_value))
-                       return (EINVAL);
+                       return (SET_ERROR(EINVAL));
                return (zfs_secpolicy_write_perms(zc->zc_name,
                    ZFS_DELEG_PERM_USERPROP, cr));
        } else {
-               if (!zfs_prop_inheritable(prop))
-                       return (EINVAL);
-               return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
+               return (zfs_secpolicy_setprop(zc->zc_name, prop,
+                   NULL, cr));
+       }
+}
+
+static int
+zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       int err = zfs_secpolicy_read(zc, innvl, cr);
+       if (err)
+               return (err);
+
+       if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
+               return (SET_ERROR(EINVAL));
+
+       if (zc->zc_value[0] == 0) {
+               /*
+                * They are asking about a posix uid/gid.  If it's
+                * themself, allow it.
+                */
+               if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
+                   zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
+                       if (zc->zc_guid == crgetuid(cr))
+                               return (0);
+               } else {
+                       if (groupmember(zc->zc_guid, cr))
+                               return (0);
+               }
+       }
+
+       return (zfs_secpolicy_write_perms(zc->zc_name,
+           userquota_perms[zc->zc_objset_type], cr));
+}
+
+static int
+zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       int err = zfs_secpolicy_read(zc, innvl, cr);
+       if (err)
+               return (err);
+
+       if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
+               return (SET_ERROR(EINVAL));
+
+       return (zfs_secpolicy_write_perms(zc->zc_name,
+           userquota_perms[zc->zc_objset_type], cr));
+}
+
+/* ARGSUSED */
+static int
+zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
+           NULL, cr));
+}
+
+/* ARGSUSED */
+static int
+zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       nvpair_t *pair;
+       nvlist_t *holds;
+       int error;
+
+       error = nvlist_lookup_nvlist(innvl, "holds", &holds);
+       if (error != 0)
+               return (SET_ERROR(EINVAL));
+
+       for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
+           pair = nvlist_next_nvpair(holds, pair)) {
+               char fsname[MAXNAMELEN];
+               error = dmu_fsname(nvpair_name(pair), fsname);
+               if (error != 0)
+                       return (error);
+               error = zfs_secpolicy_write_perms(fsname,
+                   ZFS_DELEG_PERM_HOLD, cr);
+               if (error != 0)
+                       return (error);
+       }
+       return (0);
+}
+
+/* ARGSUSED */
+static int
+zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       nvpair_t *pair;
+       int error;
+
+       for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
+           pair = nvlist_next_nvpair(innvl, pair)) {
+               char fsname[MAXNAMELEN];
+               error = dmu_fsname(nvpair_name(pair), fsname);
+               if (error != 0)
+                       return (error);
+               error = zfs_secpolicy_write_perms(fsname,
+                   ZFS_DELEG_PERM_RELEASE, cr);
+               if (error != 0)
+                       return (error);
        }
+       return (0);
+}
+
+/*
+ * Policy for allowing temporary snapshots to be taken or released
+ */
+static int
+zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
+{
+       /*
+        * A temporary snapshot is the same as a snapshot,
+        * hold, destroy and release all rolled into one.
+        * Delegated diff alone is sufficient that we allow this.
+        */
+       int error;
+
+       if ((error = zfs_secpolicy_write_perms(zc->zc_name,
+           ZFS_DELEG_PERM_DIFF, cr)) == 0)
+               return (0);
+
+       error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
+       if (error == 0)
+               error = zfs_secpolicy_hold(zc, innvl, cr);
+       if (error == 0)
+               error = zfs_secpolicy_release(zc, innvl, cr);
+       if (error == 0)
+               error = zfs_secpolicy_destroy(zc, innvl, cr);
+       return (error);
 }
 
 /*
  * Returns the nvlist as specified by the user in the zfs_cmd_t.
  */
 static int
-get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
+get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
 {
        char *packed;
        int error;
@@ -695,11 +1219,12 @@ get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
         * Read in and unpack the user-supplied nvlist.
         */
        if (size == 0)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
-       packed = kmem_alloc(size, KM_SLEEP);
+       packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
 
-       if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
+       if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
+           iflag)) != 0) {
                kmem_free(packed, size);
                return (error);
        }
@@ -715,30 +1240,137 @@ get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
        return (0);
 }
 
+/*
+ * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
+ * Entries will be removed from the end of the nvlist, and one int32 entry
+ * named "N_MORE_ERRORS" will be added indicating how many entries were
+ * removed.
+ */
+static int
+nvlist_smush(nvlist_t *errors, size_t max)
+{
+       size_t size;
+
+       size = fnvlist_size(errors);
+
+       if (size > max) {
+               nvpair_t *more_errors;
+               int n = 0;
+
+               if (max < 1024)
+                       return (SET_ERROR(ENOMEM));
+
+               fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
+               more_errors = nvlist_prev_nvpair(errors, NULL);
+
+               do {
+                       nvpair_t *pair = nvlist_prev_nvpair(errors,
+                           more_errors);
+                       fnvlist_remove_nvpair(errors, pair);
+                       n++;
+                       size = fnvlist_size(errors);
+               } while (size > max);
+
+               fnvlist_remove_nvpair(errors, more_errors);
+               fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
+               ASSERT3U(fnvlist_size(errors), <=, max);
+       }
+
+       return (0);
+}
+
 static int
 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
 {
        char *packed = NULL;
+       int error = 0;
        size_t size;
-       int error;
 
-       VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
+       size = fnvlist_size(nvl);
 
        if (size > zc->zc_nvlist_dst_size) {
-               error = ENOMEM;
+               error = SET_ERROR(ENOMEM);
        } else {
-               packed = kmem_alloc(size, KM_SLEEP);
-               VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
-                   KM_SLEEP) == 0);
-               error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
-                   size);
-               kmem_free(packed, size);
+               packed = fnvlist_pack(nvl, &size);
+               if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
+                   size, zc->zc_iflags) != 0)
+                       error = SET_ERROR(EFAULT);
+               fnvlist_pack_free(packed, size);
        }
 
        zc->zc_nvlist_dst_size = size;
+       zc->zc_nvlist_dst_filled = B_TRUE;
+       return (error);
+}
+
+static int
+get_zfs_sb(const char *dsname, zfs_sb_t **zsbp)
+{
+       objset_t *os;
+       int error;
+
+       error = dmu_objset_hold(dsname, FTAG, &os);
+       if (error != 0)
+               return (error);
+       if (dmu_objset_type(os) != DMU_OST_ZFS) {
+               dmu_objset_rele(os, FTAG);
+               return (SET_ERROR(EINVAL));
+       }
+
+       mutex_enter(&os->os_user_ptr_lock);
+       *zsbp = dmu_objset_get_user(os);
+       if (*zsbp && (*zsbp)->z_sb) {
+               atomic_inc(&((*zsbp)->z_sb->s_active));
+       } else {
+               error = SET_ERROR(ESRCH);
+       }
+       mutex_exit(&os->os_user_ptr_lock);
+       dmu_objset_rele(os, FTAG);
+       return (error);
+}
+
+/*
+ * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
+ * case its z_sb will be NULL, and it will be opened as the owner.
+ * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
+ * which prevents all inode ops from running.
+ */
+static int
+zfs_sb_hold(const char *name, void *tag, zfs_sb_t **zsbp, boolean_t writer)
+{
+       int error = 0;
+
+       if (get_zfs_sb(name, zsbp) != 0)
+               error = zfs_sb_create(name, zsbp);
+       if (error == 0) {
+               rrw_enter(&(*zsbp)->z_teardown_lock, (writer) ? RW_WRITER :
+                   RW_READER, tag);
+               if ((*zsbp)->z_unmounted) {
+                       /*
+                        * XXX we could probably try again, since the unmounting
+                        * thread should be just about to disassociate the
+                        * objset from the zsb.
+                        */
+                       rrw_exit(&(*zsbp)->z_teardown_lock, tag);
+                       return (SET_ERROR(EBUSY));
+               }
+       }
        return (error);
 }
 
+static void
+zfs_sb_rele(zfs_sb_t *zsb, void *tag)
+{
+       rrw_exit(&zsb->z_teardown_lock, tag);
+
+       if (zsb->z_sb) {
+               deactivate_super(zsb->z_sb);
+       } else {
+               dmu_objset_disown(zsb->z_os, zsb);
+               zfs_sb_free(zsb);
+       }
+}
+
 static int
 zfs_ioc_pool_create(zfs_cmd_t *zc)
 {
@@ -746,14 +1378,14 @@ zfs_ioc_pool_create(zfs_cmd_t *zc)
        nvlist_t *config, *props = NULL;
        nvlist_t *rootprops = NULL;
        nvlist_t *zplprops = NULL;
-       char *buf;
 
-       if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
-           &config))
+       if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
+           zc->zc_iflags, &config)))
                return (error);
 
        if (zc->zc_nvlist_src_size != 0 && (error =
-           get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
+           get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
+           zc->zc_iflags, &props))) {
                nvlist_free(config);
                return (error);
        }
@@ -764,8 +1396,8 @@ zfs_ioc_pool_create(zfs_cmd_t *zc)
 
                (void) nvlist_lookup_uint64(props,
                    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
-               if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
-                       error = EINVAL;
+               if (!SPA_VERSION_IS_SUPPORTED(version)) {
+                       error = SET_ERROR(EINVAL);
                        goto pool_props_bad;
                }
                (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
@@ -781,24 +1413,19 @@ zfs_ioc_pool_create(zfs_cmd_t *zc)
                VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
                error = zfs_fill_zplprops_root(version, rootprops,
                    zplprops, NULL);
-               if (error)
+               if (error != 0)
                        goto pool_props_bad;
        }
 
-       buf = history_str_get(zc);
-
-       error = spa_create(zc->zc_name, config, props, buf, zplprops);
+       error = spa_create(zc->zc_name, config, props, zplprops);
 
        /*
         * Set the remaining root properties
         */
-       if (!error &&
-           (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
+       if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
+           ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
                (void) spa_destroy(zc->zc_name);
 
-       if (buf != NULL)
-               history_str_free(buf);
-
 pool_props_bad:
        nvlist_free(rootprops);
        nvlist_free(zplprops);
@@ -814,34 +1441,41 @@ zfs_ioc_pool_destroy(zfs_cmd_t *zc)
        int error;
        zfs_log_history(zc);
        error = spa_destroy(zc->zc_name);
+       if (error == 0)
+               zvol_remove_minors(zc->zc_name);
        return (error);
 }
 
 static int
 zfs_ioc_pool_import(zfs_cmd_t *zc)
 {
-       int error;
        nvlist_t *config, *props = NULL;
        uint64_t guid;
+       int error;
 
        if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
-           &config)) != 0)
+           zc->zc_iflags, &config)) != 0)
                return (error);
 
        if (zc->zc_nvlist_src_size != 0 && (error =
-           get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
+           get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
+           zc->zc_iflags, &props))) {
                nvlist_free(config);
                return (error);
        }
 
        if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
            guid != zc->zc_guid)
-               error = EINVAL;
-       else if (zc->zc_cookie)
-               error = spa_import_faulted(zc->zc_name, config,
-                   props);
+               error = SET_ERROR(EINVAL);
        else
-               error = spa_import(zc->zc_name, config, props);
+               error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
+
+       if (zc->zc_nvlist_dst != 0) {
+               int err;
+
+               if ((err = put_nvlist(zc, config)) != 0)
+                       error = err;
+       }
 
        nvlist_free(config);
 
@@ -860,6 +1494,8 @@ zfs_ioc_pool_export(zfs_cmd_t *zc)
 
        zfs_log_history(zc);
        error = spa_export(zc->zc_name, NULL, force, hardforce);
+       if (error == 0)
+               zvol_remove_minors(zc->zc_name);
        return (error);
 }
 
@@ -870,7 +1506,7 @@ zfs_ioc_pool_configs(zfs_cmd_t *zc)
        int error;
 
        if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
-               return (EEXIST);
+               return (SET_ERROR(EEXIST));
 
        error = put_nvlist(zc, configs);
 
@@ -879,6 +1515,15 @@ zfs_ioc_pool_configs(zfs_cmd_t *zc)
        return (error);
 }
 
+/*
+ * inputs:
+ * zc_name             name of the pool
+ *
+ * outputs:
+ * zc_cookie           real errno
+ * zc_nvlist_dst       config nvlist
+ * zc_nvlist_dst_size  size of config nvlist
+ */
 static int
 zfs_ioc_pool_stats(zfs_cmd_t *zc)
 {
@@ -917,7 +1562,7 @@ zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
        int error;
 
        if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
-           &tryconfig)) != 0)
+           zc->zc_iflags, &tryconfig)) != 0)
                return (error);
 
        config = spa_tryimport(tryconfig);
@@ -925,7 +1570,7 @@ zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
        nvlist_free(tryconfig);
 
        if (config == NULL)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
        error = put_nvlist(zc, config);
        nvlist_free(config);
@@ -933,8 +1578,13 @@ zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
        return (error);
 }
 
+/*
+ * inputs:
+ * zc_name              name of the pool
+ * zc_cookie            scan func (pool_scan_func_t)
+ */
 static int
-zfs_ioc_pool_scrub(zfs_cmd_t *zc)
+zfs_ioc_pool_scan(zfs_cmd_t *zc)
 {
        spa_t *spa;
        int error;
@@ -942,7 +1592,10 @@ zfs_ioc_pool_scrub(zfs_cmd_t *zc)
        if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
                return (error);
 
-       error = spa_scrub(spa, zc->zc_cookie);
+       if (zc->zc_cookie == POOL_SCAN_NONE)
+               error = spa_scan_stop(spa);
+       else
+               error = spa_scan(spa, zc->zc_cookie);
 
        spa_close(spa, FTAG);
 
@@ -972,9 +1625,10 @@ zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
        if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
                return (error);
 
-       if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
+       if (zc->zc_cookie < spa_version(spa) ||
+           !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
                spa_close(spa, FTAG);
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
        }
 
        spa_upgrade(spa, zc->zc_cookie);
@@ -992,52 +1646,102 @@ zfs_ioc_pool_get_history(zfs_cmd_t *zc)
        int error;
 
        if ((size = zc->zc_history_len) == 0)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
        if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
                return (error);
 
        if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
                spa_close(spa, FTAG);
-               return (ENOTSUP);
+               return (SET_ERROR(ENOTSUP));
        }
 
-       hist_buf = kmem_alloc(size, KM_SLEEP);
+       hist_buf = vmem_alloc(size, KM_SLEEP);
        if ((error = spa_history_get(spa, &zc->zc_history_offset,
            &zc->zc_history_len, hist_buf)) == 0) {
-               error = xcopyout(hist_buf,
-                   (char *)(uintptr_t)zc->zc_history,
-                   zc->zc_history_len);
+               error = ddi_copyout(hist_buf,
+                   (void *)(uintptr_t)zc->zc_history,
+                   zc->zc_history_len, zc->zc_iflags);
        }
 
        spa_close(spa, FTAG);
-       kmem_free(hist_buf, size);
+       vmem_free(hist_buf, size);
+       return (error);
+}
+
+static int
+zfs_ioc_pool_reguid(zfs_cmd_t *zc)
+{
+       spa_t *spa;
+       int error;
+
+       error = spa_open(zc->zc_name, &spa, FTAG);
+       if (error == 0) {
+               error = spa_change_guid(spa);
+               spa_close(spa, FTAG);
+       }
        return (error);
 }
 
 static int
 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
 {
+       return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
+}
+
+/*
+ * inputs:
+ * zc_name             name of filesystem
+ * zc_obj              object to find
+ *
+ * outputs:
+ * zc_value            name of object
+ */
+static int
+zfs_ioc_obj_to_path(zfs_cmd_t *zc)
+{
+       objset_t *os;
        int error;
 
-       if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
+       /* XXX reading from objset not owned */
+       if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
                return (error);
+       if (dmu_objset_type(os) != DMU_OST_ZFS) {
+               dmu_objset_rele(os, 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);
 
-       return (0);
+       return (error);
 }
 
+/*
+ * inputs:
+ * zc_name             name of filesystem
+ * zc_obj              object to find
+ *
+ * outputs:
+ * zc_stat             stats on object
+ * zc_value            path to object
+ */
 static int
-zfs_ioc_obj_to_path(zfs_cmd_t *zc)
+zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
 {
-       objset_t *osp;
+       objset_t *os;
        int error;
 
-       if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
-           DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
+       /* XXX reading from objset not owned */
+       if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
                return (error);
-       error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
+       if (dmu_objset_type(os) != DMU_OST_ZFS) {
+               dmu_objset_rele(os, 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_close(osp);
+       dmu_objset_rele(os, FTAG);
 
        return (error);
 }
@@ -1047,36 +1751,14 @@ zfs_ioc_vdev_add(zfs_cmd_t *zc)
 {
        spa_t *spa;
        int error;
-       nvlist_t *config, **l2cache, **spares;
-       uint_t nl2cache = 0, nspares = 0;
+       nvlist_t *config;
 
        error = spa_open(zc->zc_name, &spa, FTAG);
        if (error != 0)
                return (error);
 
        error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
-           &config);
-       (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
-           &l2cache, &nl2cache);
-
-       (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
-           &spares, &nspares);
-
-       /*
-        * A root pool with concatenated devices is not supported.
-        * Thus, can not add a device to a root pool.
-        *
-        * Intent log device can not be added to a rootpool because
-        * during mountroot, zil is replayed, a seperated log device
-        * can not be accessed during the mountroot time.
-        *
-        * l2cache and spare devices are ok to be added to a rootpool.
-        */
-       if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
-               spa_close(spa, FTAG);
-               return (EDOM);
-       }
-
+           zc->zc_iflags, &config);
        if (error == 0) {
                error = spa_vdev_add(spa, config);
                nvlist_free(config);
@@ -1085,6 +1767,12 @@ zfs_ioc_vdev_add(zfs_cmd_t *zc)
        return (error);
 }
 
+/*
+ * inputs:
+ * zc_name             name of the pool
+ * zc_nvlist_conf      nvlist of devices to remove
+ * zc_cookie           to stop the remove?
+ */
 static int
 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
 {
@@ -1118,15 +1806,23 @@ zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
                break;
 
        case VDEV_STATE_FAULTED:
-               error = vdev_fault(spa, zc->zc_guid);
+               if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
+                   zc->zc_obj != VDEV_AUX_EXTERNAL)
+                       zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
+
+               error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
                break;
 
        case VDEV_STATE_DEGRADED:
-               error = vdev_degrade(spa, zc->zc_guid);
+               if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
+                   zc->zc_obj != VDEV_AUX_EXTERNAL)
+                       zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
+
+               error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
                break;
 
        default:
-               error = EINVAL;
+               error = SET_ERROR(EINVAL);
        }
        zc->zc_cookie = newstate;
        spa_close(spa, FTAG);
@@ -1145,7 +1841,7 @@ zfs_ioc_vdev_attach(zfs_cmd_t *zc)
                return (error);
 
        if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
-           &config)) == 0) {
+           zc->zc_iflags, &config)) == 0) {
                error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
                nvlist_free(config);
        }
@@ -1169,6 +1865,41 @@ zfs_ioc_vdev_detach(zfs_cmd_t *zc)
        return (error);
 }
 
+static int
+zfs_ioc_vdev_split(zfs_cmd_t *zc)
+{
+       spa_t *spa;
+       nvlist_t *config, *props = NULL;
+       int error;
+       boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
+
+       if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
+               return (error);
+
+       if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
+           zc->zc_iflags, &config))) {
+               spa_close(spa, FTAG);
+               return (error);
+       }
+
+       if (zc->zc_nvlist_src_size != 0 && (error =
+           get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
+           zc->zc_iflags, &props))) {
+               spa_close(spa, FTAG);
+               nvlist_free(config);
+               return (error);
+       }
+
+       error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
+
+       spa_close(spa, FTAG);
+
+       nvlist_free(config);
+       nvlist_free(props);
+
+       return (error);
+}
+
 static int
 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
 {
@@ -1186,47 +1917,114 @@ zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
        return (error);
 }
 
-/*
- * inputs:
- * zc_name             name of filesystem
- * zc_nvlist_dst_size  size of buffer for property nvlist
- *
- * outputs:
- * zc_objset_stats     stats
- * zc_nvlist_dst       property nvlist
- * zc_nvlist_dst_size  size of property nvlist
- */
 static int
-zfs_ioc_objset_stats(zfs_cmd_t *zc)
+zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
 {
-       objset_t *os = NULL;
+       spa_t *spa;
+       char *fru = zc->zc_value;
+       uint64_t guid = zc->zc_guid;
        int error;
-       nvlist_t *nv;
 
-       if (error = dmu_objset_open(zc->zc_name,
-           DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
+       error = spa_open(zc->zc_name, &spa, FTAG);
+       if (error != 0)
                return (error);
 
+       error = spa_vdev_setfru(spa, guid, fru);
+       spa_close(spa, FTAG);
+       return (error);
+}
+
+static int
+zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
+{
+       int error = 0;
+       nvlist_t *nv;
+
        dmu_objset_fast_stat(os, &zc->zc_objset_stats);
 
        if (zc->zc_nvlist_dst != 0 &&
-           (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
+           (error = dsl_prop_get_all(os, &nv)) == 0) {
                dmu_objset_stats(os, nv);
                /*
                 * NB: zvol_get_stats() will read the objset contents,
                 * which we aren't supposed to do with a
                 * DS_MODE_USER hold, because it could be
                 * inconsistent.  So this is a bit of a workaround...
+                * XXX reading with out owning
                 */
-               if (!zc->zc_objset_stats.dds_inconsistent) {
-                       if (dmu_objset_type(os) == DMU_OST_ZVOL)
-                               VERIFY(zvol_get_stats(os, nv) == 0);
+               if (!zc->zc_objset_stats.dds_inconsistent &&
+                   dmu_objset_type(os) == DMU_OST_ZVOL) {
+                       error = zvol_get_stats(os, nv);
+                       if (error == EIO)
+                               return (error);
+                       VERIFY0(error);
                }
+               if (error == 0)
+                       error = put_nvlist(zc, nv);
+               nvlist_free(nv);
+       }
+
+       return (error);
+}
+
+/*
+ * inputs:
+ * zc_name             name of filesystem
+ * zc_nvlist_dst_size  size of buffer for property nvlist
+ *
+ * outputs:
+ * zc_objset_stats     stats
+ * zc_nvlist_dst       property nvlist
+ * zc_nvlist_dst_size  size of property nvlist
+ */
+static int
+zfs_ioc_objset_stats(zfs_cmd_t *zc)
+{
+       objset_t *os;
+       int error;
+
+       error = dmu_objset_hold(zc->zc_name, FTAG, &os);
+       if (error == 0) {
+               error = zfs_ioc_objset_stats_impl(zc, os);
+               dmu_objset_rele(os, FTAG);
+       }
+
+       return (error);
+}
+
+/*
+ * inputs:
+ * zc_name             name of filesystem
+ * zc_nvlist_dst_size  size of buffer for property nvlist
+ *
+ * outputs:
+ * zc_nvlist_dst       received property nvlist
+ * zc_nvlist_dst_size  size of received property nvlist
+ *
+ * Gets received properties (distinct from local properties on or after
+ * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
+ * local property values.
+ */
+static int
+zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
+{
+       int error = 0;
+       nvlist_t *nv;
+
+       /*
+        * Without this check, we would return local property values if the
+        * caller has not already received properties on or after
+        * SPA_VERSION_RECVD_PROPS.
+        */
+       if (!dsl_prop_get_hasrecvd(zc->zc_name))
+               return (SET_ERROR(ENOTSUP));
+
+       if (zc->zc_nvlist_dst != 0 &&
+           (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
                error = put_nvlist(zc, nv);
                nvlist_free(nv);
        }
 
-       dmu_objset_close(os);
        return (error);
 }
 
@@ -1261,8 +2059,8 @@ zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
        objset_t *os;
        int err;
 
-       if (err = dmu_objset_open(zc->zc_name,
-           DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
+       /* XXX reading without owning */
+       if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
                return (err);
 
        dmu_objset_fast_stat(os, &zc->zc_objset_stats);
@@ -1272,7 +2070,7 @@ zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
         * which we aren't supposed to do with a DS_MODE_USER
         * hold, because it could be inconsistent.
         */
-       if (zc->zc_nvlist_dst != NULL &&
+       if (zc->zc_nvlist_dst != 0 &&
            !zc->zc_objset_stats.dds_inconsistent &&
            dmu_objset_type(os) == DMU_OST_ZFS) {
                nvlist_t *nv;
@@ -1285,12 +2083,29 @@ zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
                        err = put_nvlist(zc, nv);
                nvlist_free(nv);
        } else {
-               err = ENOENT;
+               err = SET_ERROR(ENOENT);
        }
-       dmu_objset_close(os);
+       dmu_objset_rele(os, FTAG);
        return (err);
 }
 
+boolean_t
+dataset_name_hidden(const char *name)
+{
+       /*
+        * Skip over datasets that are not visible in this zone,
+        * internal datasets (which have a $ in their name), and
+        * temporary datasets (which have a % in their name).
+        */
+       if (strchr(name, '$') != NULL)
+               return (B_TRUE);
+       if (strchr(name, '%') != NULL)
+               return (B_TRUE);
+       if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
+               return (B_TRUE);
+       return (B_FALSE);
+}
+
 /*
  * inputs:
  * zc_name             name of filesystem
@@ -1299,6 +2114,7 @@ zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
  *
  * outputs:
  * zc_name             name of next filesystem
+ * zc_cookie           zap cursor
  * zc_objset_stats     stats
  * zc_nvlist_dst       property nvlist
  * zc_nvlist_dst_size  size of property nvlist
@@ -1309,11 +2125,12 @@ zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
        objset_t *os;
        int error;
        char *p;
+       size_t orig_len = strlen(zc->zc_name);
 
-       if (error = dmu_objset_open(zc->zc_name,
-           DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
+top:
+       if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
                if (error == ENOENT)
-                       error = ESRCH;
+                       error = SET_ERROR(ESRCH);
                return (error);
        }
 
@@ -1322,31 +2139,27 @@ zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
                (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
        p = zc->zc_name + strlen(zc->zc_name);
 
-       if (zc->zc_cookie == 0) {
-               uint64_t cookie = 0;
-               int len = sizeof (zc->zc_name) - (p - zc->zc_name);
-
-               while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
-                       dmu_objset_prefetch(p, NULL);
-       }
-
        do {
                error = dmu_dir_list_next(os,
                    sizeof (zc->zc_name) - (p - zc->zc_name), p,
                    NULL, &zc->zc_cookie);
                if (error == ENOENT)
-                       error = ESRCH;
-       } while (error == 0 && !INGLOBALZONE(curproc) &&
-           !zone_dataset_visible(zc->zc_name, NULL));
-       dmu_objset_close(os);
+                       error = SET_ERROR(ESRCH);
+       } while (error == 0 && dataset_name_hidden(zc->zc_name));
+       dmu_objset_rele(os, FTAG);
 
        /*
-        * If it's a hidden dataset (ie. with a '$' in its name), don't
-        * try to get stats for it.  Userland will skip over it.
+        * If it's an internal dataset (ie. with a '$' in its name),
+        * don't try to get stats for it, otherwise we'll return ENOENT.
         */
-       if (error == 0 && strchr(zc->zc_name, '$') == NULL)
+       if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
                error = zfs_ioc_objset_stats(zc); /* fill in the stats */
-
+               if (error == ENOENT) {
+                       /* We lost a race with destroy, get the next one. */
+                       zc->zc_name[orig_len] = '\0';
+                       goto top;
+               }
+       }
        return (error);
 }
 
@@ -1368,265 +2181,478 @@ zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
        objset_t *os;
        int error;
 
-       error = dmu_objset_open(zc->zc_name,
-           DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
-       if (error)
+       error = dmu_objset_hold(zc->zc_name, FTAG, &os);
+       if (error != 0) {
                return (error == ENOENT ? ESRCH : error);
+       }
 
-       if (zc->zc_cookie == 0)
-               dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
-                   NULL, DS_FIND_SNAPSHOTS);
        /*
         * A dataset name of maximum length cannot have any snapshots,
         * so exit immediately.
         */
        if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
-               dmu_objset_close(os);
-               return (ESRCH);
+               dmu_objset_rele(os, FTAG);
+               return (SET_ERROR(ESRCH));
        }
 
        error = dmu_snapshot_list_next(os,
            sizeof (zc->zc_name) - strlen(zc->zc_name),
-           zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
-       dmu_objset_close(os);
-       if (error == 0)
-               error = zfs_ioc_objset_stats(zc); /* fill in the stats */
-       else if (error == ENOENT)
-               error = ESRCH;
+           zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
+           NULL);
+
+       if (error == 0 && !zc->zc_simple) {
+               dsl_dataset_t *ds;
+               dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
 
+               error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
+               if (error == 0) {
+                       objset_t *ossnap;
+
+                       error = dmu_objset_from_ds(ds, &ossnap);
+                       if (error == 0)
+                               error = zfs_ioc_objset_stats_impl(zc, ossnap);
+                       dsl_dataset_rele(ds, FTAG);
+               }
+       } else if (error == ENOENT) {
+               error = SET_ERROR(ESRCH);
+       }
+
+       dmu_objset_rele(os, FTAG);
        /* if we failed, undo the @ that we tacked on to zc_name */
-       if (error)
+       if (error != 0)
                *strchr(zc->zc_name, '@') = '\0';
        return (error);
 }
 
-int
-zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
+static int
+zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
 {
-       nvpair_t *elem;
-       int error;
-       uint64_t intval;
-       char *strval;
+       const char *propname = nvpair_name(pair);
+       uint64_t *valary;
+       unsigned int vallen;
+       const char *domain;
+       char *dash;
+       zfs_userquota_prop_t type;
+       uint64_t rid;
+       uint64_t quota;
+       zfs_sb_t *zsb;
+       int err;
+
+       if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
+               nvlist_t *attrs;
+               VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
+               if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
+                   &pair) != 0)
+                       return (SET_ERROR(EINVAL));
+       }
 
        /*
-        * First validate permission to set all of the properties
+        * A correctly constructed propname is encoded as
+        * userquota@<rid>-<domain>.
         */
-       elem = NULL;
-       while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
-               const char *propname = nvpair_name(elem);
-               zfs_prop_t prop = zfs_name_to_prop(propname);
+       if ((dash = strchr(propname, '-')) == NULL ||
+           nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
+           vallen != 3)
+               return (SET_ERROR(EINVAL));
+
+       domain = dash + 1;
+       type = valary[0];
+       rid = valary[1];
+       quota = valary[2];
+
+       err = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
+       if (err == 0) {
+               err = zfs_set_userquota(zsb, type, domain, rid, quota);
+               zfs_sb_rele(zsb, FTAG);
+       }
 
-               if (prop == ZPROP_INVAL) {
-                       /*
-                        * If this is a user-defined property, it must be a
-                        * string, and there is no further validation to do.
-                        */
-                       if (!zfs_prop_user(propname) ||
-                           nvpair_type(elem) != DATA_TYPE_STRING)
-                               return (EINVAL);
+       return (err);
+}
 
-                       if (error = zfs_secpolicy_write_perms(name,
-                           ZFS_DELEG_PERM_USERPROP, CRED()))
-                               return (error);
-                       continue;
-               }
+/*
+ * If the named property is one that has a special function to set its value,
+ * return 0 on success and a positive error code on failure; otherwise if it is
+ * not one of the special properties handled by this function, return -1.
+ *
+ * XXX: It would be better for callers of the property interface if we handled
+ * these special cases in dsl_prop.c (in the dsl layer).
+ */
+static int
+zfs_prop_set_special(const char *dsname, zprop_source_t source,
+    nvpair_t *pair)
+{
+       const char *propname = nvpair_name(pair);
+       zfs_prop_t prop = zfs_name_to_prop(propname);
+       uint64_t intval;
+       int err;
 
-               if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
-                       return (error);
+       if (prop == ZPROP_INVAL) {
+               if (zfs_prop_userquota(propname))
+                       return (zfs_prop_set_userquota(dsname, pair));
+               return (-1);
+       }
 
-               /*
-                * Check that this value is valid for this pool version
-                */
-               switch (prop) {
-               case ZFS_PROP_COMPRESSION:
-                       /*
-                        * If the user specified gzip compression, make sure
-                        * the SPA supports it. We ignore any errors here since
-                        * we'll catch them later.
-                        */
-                       if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
-                           nvpair_value_uint64(elem, &intval) == 0) {
-                               if (intval >= ZIO_COMPRESS_GZIP_1 &&
-                                   intval <= ZIO_COMPRESS_GZIP_9 &&
-                                   zfs_earlier_version(name,
-                                   SPA_VERSION_GZIP_COMPRESSION))
-                                       return (ENOTSUP);
+       if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
+               nvlist_t *attrs;
+               VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
+               VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
+                   &pair) == 0);
+       }
 
-                               /*
-                                * If this is a bootable dataset then
-                                * verify that the compression algorithm
-                                * is supported for booting. We must return
-                                * something other than ENOTSUP since it
-                                * implies a downrev pool version.
-                                */
-                               if (zfs_is_bootfs(name) &&
-                                   !BOOTFS_COMPRESS_VALID(intval))
-                                       return (ERANGE);
-                       }
-                       break;
+       if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
+               return (-1);
 
-               case ZFS_PROP_COPIES:
-                       if (zfs_earlier_version(name,
-                           SPA_VERSION_DITTO_BLOCKS))
-                               return (ENOTSUP);
-                       break;
+       VERIFY(0 == nvpair_value_uint64(pair, &intval));
+
+       switch (prop) {
+       case ZFS_PROP_QUOTA:
+               err = dsl_dir_set_quota(dsname, source, intval);
+               break;
+       case ZFS_PROP_REFQUOTA:
+               err = dsl_dataset_set_refquota(dsname, source, intval);
+               break;
+       case ZFS_PROP_RESERVATION:
+               err = dsl_dir_set_reservation(dsname, source, intval);
+               break;
+       case ZFS_PROP_REFRESERVATION:
+               err = dsl_dataset_set_refreservation(dsname, source, intval);
+               break;
+       case ZFS_PROP_VOLSIZE:
+               err = zvol_set_volsize(dsname, intval);
+               break;
+       case ZFS_PROP_SNAPDEV:
+               err = zvol_set_snapdev(dsname, intval);
+               break;
+       case ZFS_PROP_VERSION:
+       {
+               zfs_sb_t *zsb;
 
-               case ZFS_PROP_SHARESMB:
-                       if (zpl_earlier_version(name, ZPL_VERSION_FUID))
-                               return (ENOTSUP);
+               if ((err = zfs_sb_hold(dsname, FTAG, &zsb, B_TRUE)) != 0)
                        break;
 
-               case ZFS_PROP_ACLINHERIT:
-                       if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
-                           nvpair_value_uint64(elem, &intval) == 0)
-                               if (intval == ZFS_ACL_PASSTHROUGH_X &&
-                                   zfs_earlier_version(name,
-                                   SPA_VERSION_PASSTHROUGH_X))
-                                       return (ENOTSUP);
-               }
-       }
+               err = zfs_set_version(zsb, intval);
+               zfs_sb_rele(zsb, FTAG);
 
-       elem = NULL;
-       while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
-               const char *propname = nvpair_name(elem);
-               zfs_prop_t prop = zfs_name_to_prop(propname);
+               if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
+                       zfs_cmd_t *zc;
 
-               if (prop == ZPROP_INVAL) {
-                       VERIFY(nvpair_value_string(elem, &strval) == 0);
-                       error = dsl_prop_set(name, propname, 1,
-                           strlen(strval) + 1, strval);
-                       if (error == 0)
-                               continue;
-                       else
-                               return (error);
+                       zc = kmem_zalloc(sizeof (zfs_cmd_t),
+                           KM_SLEEP | KM_NODEBUG);
+                       (void) strcpy(zc->zc_name, dsname);
+                       (void) zfs_ioc_userspace_upgrade(zc);
+                       kmem_free(zc, sizeof (zfs_cmd_t));
                }
+               break;
+       }
+       case ZFS_PROP_COMPRESSION:
+       {
+               if (intval == ZIO_COMPRESS_LZ4) {
+                       zfeature_info_t *feature =
+                           &spa_feature_table[SPA_FEATURE_LZ4_COMPRESS];
+                       spa_t *spa;
 
-               switch (prop) {
-               case ZFS_PROP_QUOTA:
-                       if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
-                           (error = dsl_dir_set_quota(name, intval)) != 0)
-                               return (error);
-                       break;
+                       if ((err = spa_open(dsname, &spa, FTAG)) != 0)
+                               return (err);
 
-               case ZFS_PROP_REFQUOTA:
-                       if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
-                           (error = dsl_dataset_set_quota(name, intval)) != 0)
-                               return (error);
-                       break;
+                       /*
+                        * Setting the LZ4 compression algorithm activates
+                        * the feature.
+                        */
+                       if (!spa_feature_is_active(spa, feature)) {
+                               if ((err = zfs_prop_activate_feature(spa,
+                                   feature)) != 0) {
+                                       spa_close(spa, FTAG);
+                                       return (err);
+                               }
+                       }
 
-               case ZFS_PROP_RESERVATION:
-                       if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
-                           (error = dsl_dir_set_reservation(name,
-                           intval)) != 0)
-                               return (error);
-                       break;
+                       spa_close(spa, FTAG);
+               }
+               /*
+                * We still want the default set action to be performed in the
+                * caller, we only performed zfeature settings here.
+                */
+               err = -1;
+               break;
+       }
 
-               case ZFS_PROP_REFRESERVATION:
-                       if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
-                           (error = dsl_dataset_set_reservation(name,
-                           intval)) != 0)
-                               return (error);
-                       break;
+       default:
+               err = -1;
+       }
 
-               case ZFS_PROP_VOLSIZE:
-                       if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
-                           (error = zvol_set_volsize(name,
-                           ddi_driver_major(zfs_dip), intval)) != 0)
-                               return (error);
-                       break;
+       return (err);
+}
 
-               case ZFS_PROP_VOLBLOCKSIZE:
-                       if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
-                           (error = zvol_set_volblocksize(name, intval)) != 0)
-                               return (error);
-                       break;
+/*
+ * This function is best effort. If it fails to set any of the given properties,
+ * it continues to set as many as it can and returns the last error
+ * encountered. If the caller provides a non-NULL errlist, it will be filled in
+ * with the list of names of all the properties that failed along with the
+ * corresponding error numbers.
+ *
+ * If every property is set successfully, zero is returned and errlist is not
+ * modified.
+ */
+int
+zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
+    nvlist_t *errlist)
+{
+       nvpair_t *pair;
+       nvpair_t *propval;
+       int rv = 0;
+       uint64_t intval;
+       char *strval;
 
-               case ZFS_PROP_VERSION:
-                       if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
-                           (error = zfs_set_version(name, intval)) != 0)
-                               return (error);
-                       break;
+       nvlist_t *genericnvl = fnvlist_alloc();
+       nvlist_t *retrynvl = fnvlist_alloc();
+retry:
+       pair = NULL;
+       while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
+               const char *propname = nvpair_name(pair);
+               zfs_prop_t prop = zfs_name_to_prop(propname);
+               int err = 0;
+
+               /* decode the property value */
+               propval = pair;
+               if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
+                       nvlist_t *attrs;
+                       attrs = fnvpair_value_nvlist(pair);
+                       if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
+                           &propval) != 0)
+                               err = SET_ERROR(EINVAL);
+               }
 
-               default:
-                       if (nvpair_type(elem) == DATA_TYPE_STRING) {
-                               if (zfs_prop_get_type(prop) !=
-                                   PROP_TYPE_STRING)
-                                       return (EINVAL);
-                               VERIFY(nvpair_value_string(elem, &strval) == 0);
-                               if ((error = dsl_prop_set(name,
-                                   nvpair_name(elem), 1, strlen(strval) + 1,
-                                   strval)) != 0)
-                                       return (error);
-                       } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
+               /* Validate value type */
+               if (err == 0 && prop == ZPROP_INVAL) {
+                       if (zfs_prop_user(propname)) {
+                               if (nvpair_type(propval) != DATA_TYPE_STRING)
+                                       err = SET_ERROR(EINVAL);
+                       } else if (zfs_prop_userquota(propname)) {
+                               if (nvpair_type(propval) !=
+                                   DATA_TYPE_UINT64_ARRAY)
+                                       err = SET_ERROR(EINVAL);
+                       } else {
+                               err = SET_ERROR(EINVAL);
+                       }
+               } else if (err == 0) {
+                       if (nvpair_type(propval) == DATA_TYPE_STRING) {
+                               if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
+                                       err = SET_ERROR(EINVAL);
+                       } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
                                const char *unused;
 
-                               VERIFY(nvpair_value_uint64(elem, &intval) == 0);
+                               intval = fnvpair_value_uint64(propval);
 
                                switch (zfs_prop_get_type(prop)) {
                                case PROP_TYPE_NUMBER:
                                        break;
                                case PROP_TYPE_STRING:
-                                       return (EINVAL);
+                                       err = SET_ERROR(EINVAL);
+                                       break;
                                case PROP_TYPE_INDEX:
                                        if (zfs_prop_index_to_string(prop,
                                            intval, &unused) != 0)
-                                               return (EINVAL);
+                                               err = SET_ERROR(EINVAL);
                                        break;
                                default:
                                        cmn_err(CE_PANIC,
                                            "unknown property type");
-                                       break;
                                }
+                       } else {
+                               err = SET_ERROR(EINVAL);
+                       }
+               }
+
+               /* Validate permissions */
+               if (err == 0)
+                       err = zfs_check_settable(dsname, pair, CRED());
+
+               if (err == 0) {
+                       err = zfs_prop_set_special(dsname, source, pair);
+                       if (err == -1) {
+                               /*
+                                * For better performance we build up a list of
+                                * properties to set in a single transaction.
+                                */
+                               err = nvlist_add_nvpair(genericnvl, pair);
+                       } else if (err != 0 && nvl != retrynvl) {
+                               /*
+                                * This may be a spurious error caused by
+                                * receiving quota and reservation out of order.
+                                * Try again in a second pass.
+                                */
+                               err = nvlist_add_nvpair(retrynvl, pair);
+                       }
+               }
+
+               if (err != 0) {
+                       if (errlist != NULL)
+                               fnvlist_add_int32(errlist, propname, err);
+                       rv = err;
+               }
+       }
+
+       if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
+               nvl = retrynvl;
+               goto retry;
+       }
 
-                               if ((error = dsl_prop_set(name, propname,
-                                   8, 1, &intval)) != 0)
-                                       return (error);
+       if (!nvlist_empty(genericnvl) &&
+           dsl_props_set(dsname, source, genericnvl) != 0) {
+               /*
+                * If this fails, we still want to set as many properties as we
+                * can, so try setting them individually.
+                */
+               pair = NULL;
+               while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
+                       const char *propname = nvpair_name(pair);
+                       int err = 0;
+
+                       propval = pair;
+                       if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
+                               nvlist_t *attrs;
+                               attrs = fnvpair_value_nvlist(pair);
+                               propval = fnvlist_lookup_nvpair(attrs,
+                                   ZPROP_VALUE);
+                       }
+
+                       if (nvpair_type(propval) == DATA_TYPE_STRING) {
+                               strval = fnvpair_value_string(propval);
+                               err = dsl_prop_set_string(dsname, propname,
+                                   source, strval);
                        } else {
-                               return (EINVAL);
+                               intval = fnvpair_value_uint64(propval);
+                               err = dsl_prop_set_int(dsname, propname, source,
+                                   intval);
+                       }
+
+                       if (err != 0) {
+                               if (errlist != NULL) {
+                                       fnvlist_add_int32(errlist, propname,
+                                           err);
+                               }
+                               rv = err;
                        }
-                       break;
                }
        }
+       nvlist_free(genericnvl);
+       nvlist_free(retrynvl);
 
+       return (rv);
+}
+
+/*
+ * Check that all the properties are valid user properties.
+ */
+static int
+zfs_check_userprops(const char *fsname, nvlist_t *nvl)
+{
+       nvpair_t *pair = NULL;
+       int error = 0;
+
+       while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
+               const char *propname = nvpair_name(pair);
+               char *valstr;
+
+               if (!zfs_prop_user(propname) ||
+                   nvpair_type(pair) != DATA_TYPE_STRING)
+                       return (SET_ERROR(EINVAL));
+
+               if ((error = zfs_secpolicy_write_perms(fsname,
+                   ZFS_DELEG_PERM_USERPROP, CRED())))
+                       return (error);
+
+               if (strlen(propname) >= ZAP_MAXNAMELEN)
+                       return (SET_ERROR(ENAMETOOLONG));
+
+               VERIFY(nvpair_value_string(pair, &valstr) == 0);
+               if (strlen(valstr) >= ZAP_MAXVALUELEN)
+                       return (SET_ERROR(E2BIG));
+       }
        return (0);
 }
 
+static void
+props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
+{
+       nvpair_t *pair;
+
+       VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
+
+       pair = NULL;
+       while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
+               if (nvlist_exists(skipped, nvpair_name(pair)))
+                       continue;
+
+               VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
+       }
+}
+
+static int
+clear_received_props(const char *dsname, nvlist_t *props,
+    nvlist_t *skipped)
+{
+       int err = 0;
+       nvlist_t *cleared_props = NULL;
+       props_skip(props, skipped, &cleared_props);
+       if (!nvlist_empty(cleared_props)) {
+               /*
+                * Acts on local properties until the dataset has received
+                * properties at least once on or after SPA_VERSION_RECVD_PROPS.
+                */
+               zprop_source_t flags = (ZPROP_SRC_NONE |
+                   (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
+               err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
+       }
+       nvlist_free(cleared_props);
+       return (err);
+}
+
 /*
  * inputs:
  * zc_name             name of filesystem
- * zc_value            name of property to inherit
+ * zc_value            name of property to set
  * zc_nvlist_src{_size}        nvlist of properties to apply
- * zc_cookie           clear existing local props?
+ * zc_cookie           received properties flag
  *
- * outputs:            none
+ * outputs:
+ * zc_nvlist_dst{_size} error for each unapplied received property
  */
 static int
 zfs_ioc_set_prop(zfs_cmd_t *zc)
 {
        nvlist_t *nvl;
+       boolean_t received = zc->zc_cookie;
+       zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
+           ZPROP_SRC_LOCAL);
+       nvlist_t *errors;
        int error;
 
        if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
-           &nvl)) != 0)
+           zc->zc_iflags, &nvl)) != 0)
                return (error);
 
-       if (zc->zc_cookie) {
+       if (received) {
                nvlist_t *origprops;
-               objset_t *os;
 
-               if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
-                   DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
-                       if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
-                               clear_props(zc->zc_name, origprops, nvl);
-                               nvlist_free(origprops);
-                       }
-                       dmu_objset_close(os);
+               if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
+                       (void) clear_received_props(zc->zc_name,
+                           origprops, nvl);
+                       nvlist_free(origprops);
                }
 
+               error = dsl_prop_set_hasrecvd(zc->zc_name);
        }
 
-       error = zfs_set_prop_nvlist(zc->zc_name, nvl);
+       errors = fnvlist_alloc();
+       if (error == 0)
+               error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
+
+       if (zc->zc_nvlist_dst != 0 && errors != NULL) {
+               (void) put_nvlist(zc, errors);
+       }
 
+       nvlist_free(errors);
        nvlist_free(nvl);
        return (error);
 }
@@ -1635,14 +2661,75 @@ zfs_ioc_set_prop(zfs_cmd_t *zc)
  * inputs:
  * zc_name             name of filesystem
  * zc_value            name of property to inherit
+ * zc_cookie           revert to received value if TRUE
  *
  * outputs:            none
  */
 static int
 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
 {
-       /* the property name has been validated by zfs_secpolicy_inherit() */
-       return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
+       const char *propname = zc->zc_value;
+       zfs_prop_t prop = zfs_name_to_prop(propname);
+       boolean_t received = zc->zc_cookie;
+       zprop_source_t source = (received
+           ? ZPROP_SRC_NONE            /* revert to received value, if any */
+           : ZPROP_SRC_INHERITED);     /* explicitly inherit */
+
+       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);
+               err = zfs_prop_set_special(zc->zc_name, source, pair);
+               nvlist_free(dummy);
+               if (err != -1)
+                       return (err); /* special property already handled */
+       } else {
+               /*
+                * Only check this in the non-received case. We want to allow
+                * 'inherit -S' to revert non-inheritable properties like quota
+                * and reservation to the received or default values even though
+                * they are not considered inheritable.
+                */
+               if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
+                       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));
 }
 
 static int
@@ -1651,28 +2738,30 @@ zfs_ioc_pool_set_props(zfs_cmd_t *zc)
        nvlist_t *props;
        spa_t *spa;
        int error;
-       nvpair_t *elem;
+       nvpair_t *pair;
 
        if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
-           &props)))
+           zc->zc_iflags, &props)))
                return (error);
 
        /*
         * If the only property is the configfile, then just do a spa_lookup()
         * to handle the faulted case.
         */
-       elem = nvlist_next_nvpair(props, NULL);
-       if (elem != NULL && strcmp(nvpair_name(elem),
+       pair = nvlist_next_nvpair(props, NULL);
+       if (pair != NULL && strcmp(nvpair_name(pair),
            zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
-           nvlist_next_nvpair(props, elem) == NULL) {
+           nvlist_next_nvpair(props, pair) == NULL) {
                mutex_enter(&spa_namespace_lock);
                if ((spa = spa_lookup(zc->zc_name)) != NULL) {
                        spa_configfile_set(spa, props, B_FALSE);
                        spa_config_sync(spa, B_FALSE, B_TRUE);
                }
                mutex_exit(&spa_namespace_lock);
-               if (spa != NULL)
+               if (spa != NULL) {
+                       nvlist_free(props);
                        return (0);
+               }
        }
 
        if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
@@ -1710,62 +2799,15 @@ zfs_ioc_pool_get_props(zfs_cmd_t *zc)
                spa_close(spa, FTAG);
        }
 
-       if (error == 0 && zc->zc_nvlist_dst != NULL)
+       if (error == 0 && zc->zc_nvlist_dst != 0)
                error = put_nvlist(zc, nvp);
        else
-               error = EFAULT;
+               error = SET_ERROR(EFAULT);
 
        nvlist_free(nvp);
        return (error);
 }
 
-static int
-zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
-{
-       nvlist_t *nvp;
-       int error;
-       uint32_t uid;
-       uint32_t gid;
-       uint32_t *groups;
-       uint_t group_cnt;
-       cred_t  *usercred;
-
-       if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
-           &nvp)) != 0) {
-               return (error);
-       }
-
-       if ((error = nvlist_lookup_uint32(nvp,
-           ZFS_DELEG_PERM_UID, &uid)) != 0) {
-               nvlist_free(nvp);
-               return (EPERM);
-       }
-
-       if ((error = nvlist_lookup_uint32(nvp,
-           ZFS_DELEG_PERM_GID, &gid)) != 0) {
-               nvlist_free(nvp);
-               return (EPERM);
-       }
-
-       if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
-           &groups, &group_cnt)) != 0) {
-               nvlist_free(nvp);
-               return (EPERM);
-       }
-       usercred = cralloc();
-       if ((crsetugid(usercred, uid, gid) != 0) ||
-           (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
-               nvlist_free(nvp);
-               crfree(usercred);
-               return (EPERM);
-       }
-       nvlist_free(nvp);
-       error = dsl_deleg_access(zc->zc_name,
-           zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
-       crfree(usercred);
-       return (error);
-}
-
 /*
  * inputs:
  * zc_name             name of filesystem
@@ -1781,7 +2823,7 @@ zfs_ioc_set_fsacl(zfs_cmd_t *zc)
        nvlist_t *fsaclnv = NULL;
 
        if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
-           &fsaclnv)) != 0)
+           zc->zc_iflags, &fsaclnv)) != 0)
                return (error);
 
        /*
@@ -1789,7 +2831,7 @@ zfs_ioc_set_fsacl(zfs_cmd_t *zc)
         */
        if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
                nvlist_free(fsaclnv);
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
        }
 
        /*
@@ -1799,7 +2841,7 @@ zfs_ioc_set_fsacl(zfs_cmd_t *zc)
         */
 
        error = secpolicy_zfs(CRED());
-       if (error) {
+       if (error != 0) {
                if (zc->zc_perm_action == B_FALSE) {
                        error = dsl_deleg_can_allow(zc->zc_name,
                            fsaclnv, CRED());
@@ -1837,55 +2879,6 @@ zfs_ioc_get_fsacl(zfs_cmd_t *zc)
        return (error);
 }
 
-/*
- * inputs:
- * zc_name             name of volume
- *
- * outputs:            none
- */
-static int
-zfs_ioc_create_minor(zfs_cmd_t *zc)
-{
-       return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
-}
-
-/*
- * inputs:
- * zc_name             name of volume
- *
- * outputs:            none
- */
-static int
-zfs_ioc_remove_minor(zfs_cmd_t *zc)
-{
-       return (zvol_remove_minor(zc->zc_name));
-}
-
-/*
- * Search the vfs list for a specified resource.  Returns a pointer to it
- * or NULL if no suitable entry is found. The caller of this routine
- * is responsible for releasing the returned vfs pointer.
- */
-static vfs_t *
-zfs_get_vfs(const char *resource)
-{
-       struct vfs *vfsp;
-       struct vfs *vfs_found = NULL;
-
-       vfs_list_read_lock();
-       vfsp = rootvfs;
-       do {
-               if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
-                       VFS_HOLD(vfsp);
-                       vfs_found = vfsp;
-                       break;
-               }
-               vfsp = vfsp->vfs_next;
-       } while (vfsp != rootvfs);
-       vfs_list_unlock();
-       return (vfs_found);
-}
-
 /* ARGSUSED */
 static void
 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
@@ -1899,10 +2892,10 @@ zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
 
 /*
  * inputs:
- * createprops         list of properties requested by creator
- * default_zplver      zpl version to use if unspecified in createprops
- * fuids_ok            fuids allowed in this version of the spa?
  * os                  parent objset pointer (NULL if root fs)
+ * fuids_ok            fuids allowed in this version of the spa?
+ * sa_ok               SAs allowed in this version of the spa?
+ * createprops         list of properties requested by creator
  *
  * outputs:
  * zplprops    values for the zplprops we attach to the master node object
@@ -1918,14 +2911,14 @@ zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
  * processing.
  */
 static int
-zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
-    boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
-    boolean_t *is_ci)
+zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
+    boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
+    nvlist_t *zplprops, boolean_t *is_ci)
 {
-       uint64_t zplver = default_zplver;
        uint64_t sense = ZFS_PROP_UNDEFINED;
        uint64_t norm = ZFS_PROP_UNDEFINED;
        uint64_t u8 = ZFS_PROP_UNDEFINED;
+       int error;
 
        ASSERT(zplprops != NULL);
 
@@ -1957,10 +2950,11 @@ zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
         */
        if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
            (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
+           (zplver >= ZPL_VERSION_SA && !sa_ok) ||
            (zplver < ZPL_VERSION_NORMALIZATION &&
            (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
            sense != ZFS_PROP_UNDEFINED)))
-               return (ENOTSUP);
+               return (SET_ERROR(ENOTSUP));
 
        /*
         * Put the version in the zplprops
@@ -1968,8 +2962,9 @@ zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
 
-       if (norm == ZFS_PROP_UNDEFINED)
-               VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
+       if (norm == ZFS_PROP_UNDEFINED &&
+           (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
+               return (error);
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
 
@@ -1978,13 +2973,15 @@ zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
         */
        if (norm)
                u8 = 1;
-       if (u8 == ZFS_PROP_UNDEFINED)
-               VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
+       if (u8 == ZFS_PROP_UNDEFINED &&
+           (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
+               return (error);
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
 
-       if (sense == ZFS_PROP_UNDEFINED)
-               VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
+       if (sense == ZFS_PROP_UNDEFINED &&
+           (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
+               return (error);
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
 
@@ -1998,11 +2995,13 @@ static int
 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
     nvlist_t *zplprops, boolean_t *is_ci)
 {
-       boolean_t fuids_ok = B_TRUE;
+       boolean_t fuids_ok, sa_ok;
        uint64_t zplver = ZPL_VERSION;
        objset_t *os = NULL;
        char parentname[MAXNAMELEN];
        char *cp;
+       spa_t *spa;
+       uint64_t spa_vers;
        int error;
 
        (void) strlcpy(parentname, dataset, sizeof (parentname));
@@ -2010,21 +3009,25 @@ zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
        ASSERT(cp != NULL);
        cp[0] = '\0';
 
-       if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
-               zplver = ZPL_VERSION_FUID - 1;
-               fuids_ok = B_FALSE;
-       }
+       if ((error = spa_open(dataset, &spa, FTAG)) != 0)
+               return (error);
+
+       spa_vers = spa_version(spa);
+       spa_close(spa, FTAG);
+
+       zplver = zfs_zpl_version_map(spa_vers);
+       fuids_ok = (zplver >= ZPL_VERSION_FUID);
+       sa_ok = (zplver >= ZPL_VERSION_SA);
 
        /*
         * Open parent object set so we can inherit zplprop values.
         */
-       if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
-           DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
+       if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
                return (error);
 
-       error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
+       error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
            zplprops, is_ci);
-       dmu_objset_close(os);
+       dmu_objset_rele(os, FTAG);
        return (error);
 }
 
@@ -2032,41 +3035,45 @@ static int
 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
     nvlist_t *zplprops, boolean_t *is_ci)
 {
-       boolean_t fuids_ok = B_TRUE;
+       boolean_t fuids_ok;
+       boolean_t sa_ok;
        uint64_t zplver = ZPL_VERSION;
        int error;
 
-       if (spa_vers < SPA_VERSION_FUID) {
-               zplver = ZPL_VERSION_FUID - 1;
-               fuids_ok = B_FALSE;
-       }
+       zplver = zfs_zpl_version_map(spa_vers);
+       fuids_ok = (zplver >= ZPL_VERSION_FUID);
+       sa_ok = (zplver >= ZPL_VERSION_SA);
 
-       error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
-           zplprops, is_ci);
+       error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
+           createprops, zplprops, is_ci);
        return (error);
 }
 
 /*
- * inputs:
- * zc_objset_type      type of objset to create (fs vs zvol)
- * zc_name             name of new objset
- * zc_value            name of snapshot to clone from (may be empty)
- * zc_nvlist_src{_size}        nvlist of properties to apply
+ * innvl: {
+ *     "type" -> dmu_objset_type_t (int32)
+ *     (optional) "props" -> { prop -> value }
+ * }
  *
- * outputs: none
+ * outnvl: propname -> error code (int32)
  */
 static int
-zfs_ioc_create(zfs_cmd_t *zc)
+zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
 {
-       objset_t *clone;
        int error = 0;
-       zfs_creat_t zct;
+       zfs_creat_t zct = { 0 };
        nvlist_t *nvprops = NULL;
        void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
-       dmu_objset_type_t type = zc->zc_objset_type;
+       int32_t type32;
+       dmu_objset_type_t type;
+       boolean_t is_insensitive = B_FALSE;
 
-       switch (type) {
+       if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
+               return (SET_ERROR(EINVAL));
+       type = type32;
+       (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
 
+       switch (type) {
        case DMU_OST_ZFS:
                cbfunc = zfs_create_cb;
                break;
@@ -2079,316 +3086,421 @@ zfs_ioc_create(zfs_cmd_t *zc)
                cbfunc = NULL;
                break;
        }
-       if (strchr(zc->zc_name, '@') ||
-           strchr(zc->zc_name, '%'))
-               return (EINVAL);
+       if (strchr(fsname, '@') ||
+           strchr(fsname, '%'))
+               return (SET_ERROR(EINVAL));
 
-       if (zc->zc_nvlist_src != NULL &&
-           (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
-           &nvprops)) != 0)
-               return (error);
-
-       zct.zct_zplprops = NULL;
        zct.zct_props = nvprops;
 
-       if (zc->zc_value[0] != '\0') {
-               /*
-                * We're creating a clone of an existing snapshot.
-                */
-               zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
-               if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
-                       nvlist_free(nvprops);
-                       return (EINVAL);
-               }
+       if (cbfunc == NULL)
+               return (SET_ERROR(EINVAL));
+
+       if (type == DMU_OST_ZVOL) {
+               uint64_t volsize, volblocksize;
+
+               if (nvprops == NULL)
+                       return (SET_ERROR(EINVAL));
+               if (nvlist_lookup_uint64(nvprops,
+                   zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
+                       return (SET_ERROR(EINVAL));
 
-               error = dmu_objset_open(zc->zc_value, type,
-                   DS_MODE_USER | DS_MODE_READONLY, &clone);
-               if (error) {
-                       nvlist_free(nvprops);
+               if ((error = nvlist_lookup_uint64(nvprops,
+                   zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
+                   &volblocksize)) != 0 && error != ENOENT)
+                       return (SET_ERROR(EINVAL));
+
+               if (error != 0)
+                       volblocksize = zfs_prop_default_numeric(
+                           ZFS_PROP_VOLBLOCKSIZE);
+
+               if ((error = zvol_check_volblocksize(
+                   volblocksize)) != 0 ||
+                   (error = zvol_check_volsize(volsize,
+                   volblocksize)) != 0)
                        return (error);
-               }
+       } else if (type == DMU_OST_ZFS) {
+               int error;
 
-               error = dmu_objset_create(zc->zc_name, type, clone, 0,
-                   NULL, NULL);
-               if (error) {
-                       dmu_objset_close(clone);
-                       nvlist_free(nvprops);
+               /*
+                * We have to have normalization and
+                * case-folding flags correct when we do the
+                * file system creation, so go figure them out
+                * now.
+                */
+               VERIFY(nvlist_alloc(&zct.zct_zplprops,
+                   NV_UNIQUE_NAME, KM_SLEEP) == 0);
+               error = zfs_fill_zplprops(fsname, nvprops,
+                   zct.zct_zplprops, &is_insensitive);
+               if (error != 0) {
+                       nvlist_free(zct.zct_zplprops);
                        return (error);
                }
-               dmu_objset_close(clone);
-       } else {
-               boolean_t is_insensitive = B_FALSE;
+       }
 
-               if (cbfunc == NULL) {
-                       nvlist_free(nvprops);
-                       return (EINVAL);
-               }
+       error = dmu_objset_create(fsname, type,
+           is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
+       nvlist_free(zct.zct_zplprops);
+
+       /*
+        * It would be nice to do this atomically.
+        */
+       if (error == 0) {
+               error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
+                   nvprops, outnvl);
+               if (error != 0)
+                       (void) dsl_destroy_head(fsname);
+       }
 
-               if (type == DMU_OST_ZVOL) {
-                       uint64_t volsize, volblocksize;
+#ifdef _KERNEL
+       if (error == 0 && type == DMU_OST_ZVOL)
+               zvol_create_minors(fsname);
+#endif
 
-                       if (nvprops == NULL ||
-                           nvlist_lookup_uint64(nvprops,
-                           zfs_prop_to_name(ZFS_PROP_VOLSIZE),
-                           &volsize) != 0) {
-                               nvlist_free(nvprops);
-                               return (EINVAL);
-                       }
+       return (error);
+}
 
-                       if ((error = nvlist_lookup_uint64(nvprops,
-                           zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
-                           &volblocksize)) != 0 && error != ENOENT) {
-                               nvlist_free(nvprops);
-                               return (EINVAL);
-                       }
+/*
+ * innvl: {
+ *     "origin" -> name of origin snapshot
+ *     (optional) "props" -> { prop -> value }
+ * }
+ *
+ * outputs:
+ * outnvl: propname -> error code (int32)
+ */
+static int
+zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
+{
+       int error = 0;
+       nvlist_t *nvprops = NULL;
+       char *origin_name;
 
-                       if (error != 0)
-                               volblocksize = zfs_prop_default_numeric(
-                                   ZFS_PROP_VOLBLOCKSIZE);
+       if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
+               return (SET_ERROR(EINVAL));
+       (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
 
-                       if ((error = zvol_check_volblocksize(
-                           volblocksize)) != 0 ||
-                           (error = zvol_check_volsize(volsize,
-                           volblocksize)) != 0) {
-                               nvlist_free(nvprops);
-                               return (error);
-                       }
-               } else if (type == DMU_OST_ZFS) {
-                       int error;
+       if (strchr(fsname, '@') ||
+           strchr(fsname, '%'))
+               return (SET_ERROR(EINVAL));
 
-                       /*
-                        * We have to have normalization and
-                        * case-folding flags correct when we do the
-                        * file system creation, so go figure them out
-                        * now.
-                        */
-                       VERIFY(nvlist_alloc(&zct.zct_zplprops,
-                           NV_UNIQUE_NAME, KM_SLEEP) == 0);
-                       error = zfs_fill_zplprops(zc->zc_name, nvprops,
-                           zct.zct_zplprops, &is_insensitive);
-                       if (error != 0) {
-                               nvlist_free(nvprops);
-                               nvlist_free(zct.zct_zplprops);
-                               return (error);
-                       }
-               }
-               error = dmu_objset_create(zc->zc_name, type, NULL,
-                   is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
-               nvlist_free(zct.zct_zplprops);
-       }
+       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.
         */
        if (error == 0) {
-               if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
-                       (void) dmu_objset_destroy(zc->zc_name);
+               error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
+                   nvprops, outnvl);
+               if (error != 0)
+                       (void) dsl_destroy_head(fsname);
        }
-       nvlist_free(nvprops);
+
+#ifdef _KERNEL
+       if (error == 0)
+               zvol_create_minors(fsname);
+#endif
+
        return (error);
 }
 
-struct snap_prop_arg {
-       nvlist_t *nvprops;
-       const char *snapname;
-};
-
+/*
+ * innvl: {
+ *     "snaps" -> { snapshot1, snapshot2 }
+ *     (optional) "props" -> { prop -> value (string) }
+ * }
+ *
+ * outnvl: snapshot -> error code (int32)
+ */
 static int
-set_snap_props(char *name, void *arg)
+zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
 {
-       struct snap_prop_arg *snpa = arg;
-       int len = strlen(name) + strlen(snpa->snapname) + 2;
-       char *buf = kmem_alloc(len, KM_SLEEP);
-       int err;
+       nvlist_t *snaps;
+       nvlist_t *props = NULL;
+       int error, poollen;
+       nvpair_t *pair, *pair2;
 
-       (void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
-       err = zfs_set_prop_nvlist(buf, snpa->nvprops);
-       if (err)
-               (void) dmu_objset_destroy(buf);
-       kmem_free(buf, len);
-       return (err);
+       (void) nvlist_lookup_nvlist(innvl, "props", &props);
+       if ((error = zfs_check_userprops(poolname, props)) != 0)
+               return (error);
+
+       if (!nvlist_empty(props) &&
+           zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
+               return (SET_ERROR(ENOTSUP));
+
+       if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
+               return (SET_ERROR(EINVAL));
+       poollen = strlen(poolname);
+       for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
+           pair = nvlist_next_nvpair(snaps, pair)) {
+               const char *name = nvpair_name(pair);
+               const char *cp = strchr(name, '@');
+
+               /*
+                * The snap name must contain an @, and the part after it must
+                * contain only valid characters.
+                */
+               if (cp == NULL || snapshot_namecheck(cp + 1, NULL, NULL) != 0)
+                       return (SET_ERROR(EINVAL));
+
+               /*
+                * The snap must be in the specified pool.
+                */
+               if (strncmp(name, poolname, poollen) != 0 ||
+                   (name[poollen] != '/' && name[poollen] != '@'))
+                       return (SET_ERROR(EXDEV));
+
+               /* This must be the only snap of this fs. */
+               for (pair2 = nvlist_next_nvpair(snaps, pair);
+                   pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
+                       if (strncmp(name, nvpair_name(pair2), cp - name + 1)
+                           == 0) {
+                               return (SET_ERROR(EXDEV));
+                       }
+               }
+       }
+
+       error = dsl_dataset_snapshot(snaps, props, outnvl);
+
+#ifdef _KERNEL
+       if (error == 0)
+               zvol_create_minors(poolname);
+#endif
+
+       return (error);
 }
 
 /*
- * inputs:
- * zc_name     name of filesystem
- * zc_value    short name of snapshot
- * zc_cookie   recursive flag
- *
- * outputs:    none
+ * innvl: "message" -> string
  */
+/* ARGSUSED */
 static int
-zfs_ioc_snapshot(zfs_cmd_t *zc)
+zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
 {
-       nvlist_t *nvprops = NULL;
+       char *message;
+       spa_t *spa;
        int error;
-       boolean_t recursive = zc->zc_cookie;
+       char *poolname;
 
-       if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
-               return (EINVAL);
-
-       if (zc->zc_nvlist_src != NULL &&
-           (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
-           &nvprops)) != 0)
+       /*
+        * The poolname in the ioctl is not set, we get it from the TSD,
+        * which was set at the end of the last successful ioctl that allows
+        * logging.  The secpolicy func already checked that it is set.
+        * Only one log ioctl is allowed after each successful ioctl, so
+        * we clear the TSD here.
+        */
+       poolname = tsd_get(zfs_allow_log_key);
+       (void) tsd_set(zfs_allow_log_key, NULL);
+       error = spa_open(poolname, &spa, FTAG);
+       strfree(poolname);
+       if (error != 0)
                return (error);
 
-       error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
+       if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
+               spa_close(spa, FTAG);
+               return (SET_ERROR(EINVAL));
+       }
 
-       /*
-        * It would be nice to do this atomically.
-        */
-       if (error == 0) {
-               struct snap_prop_arg snpa;
-               snpa.nvprops = nvprops;
-               snpa.snapname = zc->zc_value;
-               if (recursive) {
-                       error = dmu_objset_find(zc->zc_name,
-                           set_snap_props, &snpa, DS_FIND_CHILDREN);
-                       if (error) {
-                               (void) dmu_snapshots_destroy(zc->zc_name,
-                                   zc->zc_value);
-                       }
-               } else {
-                       error = set_snap_props(zc->zc_name, &snpa);
-               }
+       if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
+               spa_close(spa, FTAG);
+               return (SET_ERROR(ENOTSUP));
        }
-       nvlist_free(nvprops);
+
+       error = spa_history_log(spa, message);
+       spa_close(spa, FTAG);
        return (error);
 }
 
+/*
+ * The dp_config_rwlock must not be held when calling this, because the
+ * unmount may need to write out data.
+ *
+ * This function is best-effort.  Callers must deal gracefully if it
+ * remains mounted (or is remounted after this call).
+ *
+ * XXX: This function should detect a failure to unmount a snapdir of a dataset
+ * and return the appropriate error code when it is mounted. Its Illumos and
+ * FreeBSD counterparts do this. We do not do this on Linux because there is no
+ * clear way to access the mount information that FreeBSD and Illumos use to
+ * distinguish between things with mounted snapshot directories, and things
+ * without mounted snapshot directories, which include zvols. Returning a
+ * failure for the latter causes `zfs destroy` to fail on zvol snapshots.
+ */
 int
-zfs_unmount_snap(char *name, void *arg)
+zfs_unmount_snap(const char *snapname)
 {
-       vfs_t *vfsp = NULL;
+       zfs_sb_t *zsb = NULL;
+       char *dsname;
+       char *fullname;
+       char *ptr;
+
+       if ((ptr = strchr(snapname, '@')) == NULL)
+               return (0);
 
-       if (arg) {
-               char *snapname = arg;
-               int len = strlen(name) + strlen(snapname) + 2;
-               char *buf = kmem_alloc(len, KM_SLEEP);
+       dsname = kmem_alloc(ptr - snapname + 1, KM_SLEEP);
+       strlcpy(dsname, snapname, ptr - snapname + 1);
+       fullname = strdup(snapname);
 
-               (void) strcpy(buf, name);
-               (void) strcat(buf, "@");
-               (void) strcat(buf, snapname);
-               vfsp = zfs_get_vfs(buf);
-               kmem_free(buf, len);
-       } else if (strchr(name, '@')) {
-               vfsp = zfs_get_vfs(name);
+       if (zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE) == 0) {
+               ASSERT(!dsl_pool_config_held(dmu_objset_pool(zsb->z_os)));
+               (void) zfsctl_unmount_snapshot(zsb, fullname, MNT_FORCE);
+               zfs_sb_rele(zsb, FTAG);
        }
 
-       if (vfsp) {
-               /*
-                * Always force the unmount for snapshots.
-                */
-               int flag = MS_FORCE;
-               int err;
+       kmem_free(dsname, ptr - snapname + 1);
+       strfree(fullname);
 
-               if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
-                       VFS_RELE(vfsp);
-                       return (err);
-               }
-               VFS_RELE(vfsp);
-               if ((err = dounmount(vfsp, flag, kcred)) != 0)
-                       return (err);
-       }
        return (0);
 }
 
+/* ARGSUSED */
+static int
+zfs_unmount_snap_cb(const char *snapname, void *arg)
+{
+       return (zfs_unmount_snap(snapname));
+}
+
 /*
- * inputs:
- * zc_name     name of filesystem
- * zc_value    short name of snapshot
+ * When a clone is destroyed, its origin may also need to be destroyed,
+ * in which case it must be unmounted.  This routine will do that unmount
+ * if necessary.
+ */
+void
+zfs_destroy_unmount_origin(const char *fsname)
+{
+       int error;
+       objset_t *os;
+       dsl_dataset_t *ds;
+
+       error = dmu_objset_hold(fsname, FTAG, &os);
+       if (error != 0)
+               return;
+       ds = dmu_objset_ds(os);
+       if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
+               char originname[MAXNAMELEN];
+               dsl_dataset_name(ds->ds_prev, originname);
+               dmu_objset_rele(os, FTAG);
+               (void) zfs_unmount_snap(originname);
+       } else {
+               dmu_objset_rele(os, FTAG);
+       }
+}
+
+/*
+ * innvl: {
+ *     "snaps" -> { snapshot1, snapshot2 }
+ *     (optional boolean) "defer"
+ * }
  *
- * outputs:    none
+ * outnvl: snapshot -> error code (int32)
  */
 static int
-zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
+zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
 {
-       int err;
+       int error, poollen;
+       nvlist_t *snaps;
+       nvpair_t *pair;
+       boolean_t defer;
 
-       if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
-               return (EINVAL);
-       err = dmu_objset_find(zc->zc_name,
-           zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
-       if (err)
-               return (err);
-       return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
+       if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
+               return (SET_ERROR(EINVAL));
+       defer = nvlist_exists(innvl, "defer");
+
+       poollen = strlen(poolname);
+       for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
+           pair = nvlist_next_nvpair(snaps, pair)) {
+               const char *name = nvpair_name(pair);
+
+               /*
+                * The snap must be in the specified pool.
+                */
+               if (strncmp(name, poolname, poollen) != 0 ||
+                   (name[poollen] != '/' && name[poollen] != '@'))
+                       return (SET_ERROR(EXDEV));
+
+               error = zfs_unmount_snap(name);
+               if (error != 0)
+                       return (error);
+               (void) zvol_remove_minor(name);
+       }
+
+       return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
 }
 
 /*
  * inputs:
  * zc_name             name of dataset to destroy
  * zc_objset_type      type of objset
+ * zc_defer_destroy    mark for deferred destroy
  *
  * outputs:            none
  */
 static int
 zfs_ioc_destroy(zfs_cmd_t *zc)
 {
-       if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
-               int err = zfs_unmount_snap(zc->zc_name, NULL);
-               if (err)
+       int err;
+
+       if (zc->zc_objset_type == DMU_OST_ZFS) {
+               err = zfs_unmount_snap(zc->zc_name);
+               if (err != 0)
                        return (err);
        }
 
-       return (dmu_objset_destroy(zc->zc_name));
+       if (strchr(zc->zc_name, '@'))
+               err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
+       else
+               err = dsl_destroy_head(zc->zc_name);
+       if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
+               (void) zvol_remove_minor(zc->zc_name);
+       return (err);
 }
 
 /*
- * inputs:
- * zc_name     name of dataset to rollback (to most recent snapshot)
+ * fsname is name of dataset to rollback (to most recent snapshot)
  *
- * outputs:    none
+ * innvl is not used.
+ *
+ * outnvl: "target" -> name of most recent snapshot
+ * }
  */
+/* ARGSUSED */
 static int
-zfs_ioc_rollback(zfs_cmd_t *zc)
+zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
 {
-       objset_t *os;
+       zfs_sb_t *zsb;
        int error;
-       zfsvfs_t *zfsvfs = NULL;
-
-       /*
-        * Get the zfsvfs for the receiving objset. There
-        * won't be one if we're operating on a zvol, if the
-        * objset doesn't exist yet, or is not mounted.
-        */
-       error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
-       if (error)
-               return (error);
-
-       if (dmu_objset_type(os) == DMU_OST_ZFS) {
-               mutex_enter(&os->os->os_user_ptr_lock);
-               zfsvfs = dmu_objset_get_user(os);
-               if (zfsvfs != NULL)
-                       VFS_HOLD(zfsvfs->z_vfs);
-               mutex_exit(&os->os->os_user_ptr_lock);
-       }
-
-       if (zfsvfs != NULL) {
-               char *osname;
-               int mode;
 
-               osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
-               error = zfs_suspend_fs(zfsvfs, osname, &mode);
+       if (get_zfs_sb(fsname, &zsb) == 0) {
+               error = zfs_suspend_fs(zsb);
                if (error == 0) {
                        int resume_err;
 
-                       ASSERT(strcmp(osname, zc->zc_name) == 0);
-                       error = dmu_objset_rollback(os);
-                       resume_err = zfs_resume_fs(zfsvfs, osname, mode);
+                       error = dsl_dataset_rollback(fsname, zsb, outnvl);
+                       resume_err = zfs_resume_fs(zsb, fsname);
                        error = error ? error : resume_err;
-               } else {
-                       dmu_objset_close(os);
                }
-               kmem_free(osname, MAXNAMELEN);
-               VFS_RELE(zfsvfs->z_vfs);
+               deactivate_super(zsb->z_sb);
        } else {
-               error = dmu_objset_rollback(os);
+               error = dsl_dataset_rollback(fsname, NULL, outnvl);
        }
-       /* Note, the dmu_objset_rollback() releases the objset for us. */
-
        return (error);
 }
 
+static int
+recursive_unmount(const char *fsname, void *arg)
+{
+       const char *snapname = arg;
+       char *fullname;
+
+       fullname = kmem_asprintf("%s@%s", fsname, snapname);
+       zfs_unmount_snap(fullname);
+       strfree(fullname);
+       return (zfs_unmount_snap(fullname));
+}
+
 /*
  * inputs:
  * zc_name     old name of dataset
@@ -2401,48 +3513,364 @@ static int
 zfs_ioc_rename(zfs_cmd_t *zc)
 {
        boolean_t recursive = zc->zc_cookie & 1;
+       char *at;
 
        zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
        if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
            strchr(zc->zc_value, '%'))
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
-       /*
-        * Unmount snapshot unless we're doing a recursive rename,
-        * in which case the dataset code figures out which snapshots
-        * to unmount.
-        */
-       if (!recursive && strchr(zc->zc_name, '@') != NULL &&
-           zc->zc_objset_type == DMU_OST_ZFS) {
-               int err = zfs_unmount_snap(zc->zc_name, NULL);
-               if (err)
-                       return (err);
+       at = strchr(zc->zc_name, '@');
+       if (at != NULL) {
+               /* snaps must be in same fs */
+               int error;
+
+               if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
+                       return (SET_ERROR(EXDEV));
+               *at = '\0';
+               if (zc->zc_objset_type == DMU_OST_ZFS) {
+                       error = dmu_objset_find(zc->zc_name,
+                           recursive_unmount, at + 1,
+                           recursive ? DS_FIND_CHILDREN : 0);
+                       if (error != 0) {
+                               *at = '@';
+                               return (error);
+                       }
+               }
+               error = dsl_dataset_rename_snapshot(zc->zc_name,
+                   at + 1, strchr(zc->zc_value, '@') + 1, recursive);
+               *at = '@';
+
+               return (error);
+       } else {
+               return (dsl_dir_rename(zc->zc_name, zc->zc_value));
        }
-       return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
 }
 
-static void
-clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
+static int
+zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
 {
-       zfs_cmd_t *zc;
-       nvpair_t *prop;
+       const char *propname = nvpair_name(pair);
+       boolean_t issnap = (strchr(dsname, '@') != NULL);
+       zfs_prop_t prop = zfs_name_to_prop(propname);
+       uint64_t intval;
+       int err;
 
-       if (props == NULL)
-               return;
-       zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
+       if (prop == ZPROP_INVAL) {
+               if (zfs_prop_user(propname)) {
+                       if ((err = zfs_secpolicy_write_perms(dsname,
+                           ZFS_DELEG_PERM_USERPROP, cr)))
+                               return (err);
+                       return (0);
+               }
+
+               if (!issnap && zfs_prop_userquota(propname)) {
+                       const char *perm = NULL;
+                       const char *uq_prefix =
+                           zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
+                       const char *gq_prefix =
+                           zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
+
+                       if (strncmp(propname, uq_prefix,
+                           strlen(uq_prefix)) == 0) {
+                               perm = ZFS_DELEG_PERM_USERQUOTA;
+                       } else if (strncmp(propname, gq_prefix,
+                           strlen(gq_prefix)) == 0) {
+                               perm = ZFS_DELEG_PERM_GROUPQUOTA;
+                       } else {
+                               /* USERUSED and GROUPUSED are read-only */
+                               return (SET_ERROR(EINVAL));
+                       }
+
+                       if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
+                               return (err);
+                       return (0);
+               }
+
+               return (SET_ERROR(EINVAL));
+       }
+
+       if (issnap)
+               return (SET_ERROR(EINVAL));
+
+       if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
+               /*
+                * dsl_prop_get_all_impl() returns properties in this
+                * format.
+                */
+               nvlist_t *attrs;
+               VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
+               VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
+                   &pair) == 0);
+       }
+
+       /*
+        * Check that this value is valid for this pool version
+        */
+       switch (prop) {
+       case ZFS_PROP_COMPRESSION:
+               /*
+                * If the user specified gzip compression, make sure
+                * the SPA supports it. We ignore any errors here since
+                * we'll catch them later.
+                */
+               if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
+                   nvpair_value_uint64(pair, &intval) == 0) {
+                       if (intval >= ZIO_COMPRESS_GZIP_1 &&
+                           intval <= ZIO_COMPRESS_GZIP_9 &&
+                           zfs_earlier_version(dsname,
+                           SPA_VERSION_GZIP_COMPRESSION)) {
+                               return (SET_ERROR(ENOTSUP));
+                       }
+
+                       if (intval == ZIO_COMPRESS_ZLE &&
+                           zfs_earlier_version(dsname,
+                           SPA_VERSION_ZLE_COMPRESSION))
+                               return (SET_ERROR(ENOTSUP));
+
+                       if (intval == ZIO_COMPRESS_LZ4) {
+                               zfeature_info_t *feature =
+                                   &spa_feature_table[
+                                   SPA_FEATURE_LZ4_COMPRESS];
+                               spa_t *spa;
+
+                               if ((err = spa_open(dsname, &spa, FTAG)) != 0)
+                                       return (err);
+
+                               if (!spa_feature_is_enabled(spa, feature)) {
+                                       spa_close(spa, FTAG);
+                                       return (SET_ERROR(ENOTSUP));
+                               }
+                               spa_close(spa, FTAG);
+                       }
+
+                       /*
+                        * If this is a bootable dataset then
+                        * verify that the compression algorithm
+                        * is supported for booting. We must return
+                        * something other than ENOTSUP since it
+                        * implies a downrev pool version.
+                        */
+                       if (zfs_is_bootfs(dsname) &&
+                           !BOOTFS_COMPRESS_VALID(intval)) {
+                               return (SET_ERROR(ERANGE));
+                       }
+               }
+               break;
+
+       case ZFS_PROP_COPIES:
+               if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
+                       return (SET_ERROR(ENOTSUP));
+               break;
+
+       case ZFS_PROP_DEDUP:
+               if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
+                       return (SET_ERROR(ENOTSUP));
+               break;
+
+       case ZFS_PROP_SHARESMB:
+               if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
+                       return (SET_ERROR(ENOTSUP));
+               break;
+
+       case ZFS_PROP_ACLINHERIT:
+               if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
+                   nvpair_value_uint64(pair, &intval) == 0) {
+                       if (intval == ZFS_ACL_PASSTHROUGH_X &&
+                           zfs_earlier_version(dsname,
+                           SPA_VERSION_PASSTHROUGH_X))
+                               return (SET_ERROR(ENOTSUP));
+               }
+               break;
+       default:
+               break;
+       }
+
+       return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
+}
+
+/*
+ * Checks for a race condition to make sure we don't increment a feature flag
+ * multiple times.
+ */
+static int
+zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
+{
+       spa_t *spa = dmu_tx_pool(tx)->dp_spa;
+       zfeature_info_t *feature = arg;
+
+       if (!spa_feature_is_active(spa, feature))
+               return (0);
+       else
+               return (SET_ERROR(EBUSY));
+}
+
+/*
+ * The callback invoked on feature activation in the sync task caused by
+ * zfs_prop_activate_feature.
+ */
+static void
+zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
+{
+       spa_t *spa = dmu_tx_pool(tx)->dp_spa;
+       zfeature_info_t *feature = arg;
+
+       spa_feature_incr(spa, feature, tx);
+}
+
+/*
+ * Activates a feature on a pool in response to a property setting. This
+ * creates a new sync task which modifies the pool to reflect the feature
+ * as being active.
+ */
+static int
+zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature)
+{
+       int err;
+
+       /* EBUSY here indicates that the feature is already active */
+       err = dsl_sync_task(spa_name(spa),
+           zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
+           feature, 2);
+
+       if (err != 0 && err != EBUSY)
+               return (err);
+       else
+               return (0);
+}
+
+/*
+ * Removes properties from the given props list that fail permission checks
+ * needed to clear them and to restore them in case of a receive error. For each
+ * property, make sure we have both set and inherit permissions.
+ *
+ * Returns the first error encountered if any permission checks fail. If the
+ * caller provides a non-NULL errlist, it also gives the complete list of names
+ * of all the properties that failed a permission check along with the
+ * corresponding error numbers. The caller is responsible for freeing the
+ * returned errlist.
+ *
+ * If every property checks out successfully, zero is returned and the list
+ * pointed at by errlist is NULL.
+ */
+static int
+zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
+{
+       zfs_cmd_t *zc;
+       nvpair_t *pair, *next_pair;
+       nvlist_t *errors;
+       int err, rv = 0;
+
+       if (props == NULL)
+               return (0);
+
+       VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
+
+       zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
        (void) strcpy(zc->zc_name, dataset);
-       for (prop = nvlist_next_nvpair(props, NULL); prop;
-           prop = nvlist_next_nvpair(props, prop)) {
-               if (newprops != NULL &&
-                   nvlist_exists(newprops, nvpair_name(prop)))
-                       continue;
-               (void) strcpy(zc->zc_value, nvpair_name(prop));
-               if (zfs_secpolicy_inherit(zc, CRED()) == 0)
-                       (void) zfs_ioc_inherit_prop(zc);
+       pair = nvlist_next_nvpair(props, NULL);
+       while (pair != NULL) {
+               next_pair = nvlist_next_nvpair(props, pair);
+
+               (void) strcpy(zc->zc_value, nvpair_name(pair));
+               if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
+                   (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
+                       VERIFY(nvlist_remove_nvpair(props, pair) == 0);
+                       VERIFY(nvlist_add_int32(errors,
+                           zc->zc_value, err) == 0);
+               }
+               pair = next_pair;
        }
        kmem_free(zc, sizeof (zfs_cmd_t));
+
+       if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
+               nvlist_free(errors);
+               errors = NULL;
+       } else {
+               VERIFY(nvpair_value_int32(pair, &rv) == 0);
+       }
+
+       if (errlist == NULL)
+               nvlist_free(errors);
+       else
+               *errlist = errors;
+
+       return (rv);
+}
+
+static boolean_t
+propval_equals(nvpair_t *p1, nvpair_t *p2)
+{
+       if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
+               /* dsl_prop_get_all_impl() format */
+               nvlist_t *attrs;
+               VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
+               VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
+                   &p1) == 0);
+       }
+
+       if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
+               nvlist_t *attrs;
+               VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
+               VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
+                   &p2) == 0);
+       }
+
+       if (nvpair_type(p1) != nvpair_type(p2))
+               return (B_FALSE);
+
+       if (nvpair_type(p1) == DATA_TYPE_STRING) {
+               char *valstr1, *valstr2;
+
+               VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
+               VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
+               return (strcmp(valstr1, valstr2) == 0);
+       } else {
+               uint64_t intval1, intval2;
+
+               VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
+               VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
+               return (intval1 == intval2);
+       }
 }
 
+/*
+ * Remove properties from props if they are not going to change (as determined
+ * by comparison with origprops). Remove them from origprops as well, since we
+ * do not need to clear or restore properties that won't change.
+ */
+static void
+props_reduce(nvlist_t *props, nvlist_t *origprops)
+{
+       nvpair_t *pair, *next_pair;
+
+       if (origprops == NULL)
+               return; /* all props need to be received */
+
+       pair = nvlist_next_nvpair(props, NULL);
+       while (pair != NULL) {
+               const char *propname = nvpair_name(pair);
+               nvpair_t *match;
+
+               next_pair = nvlist_next_nvpair(props, pair);
+
+               if ((nvlist_lookup_nvpair(origprops, propname,
+                   &match) != 0) || !propval_equals(pair, match))
+                       goto next; /* need to set received value */
+
+               /* don't clear the existing received value */
+               (void) nvlist_remove_nvpair(origprops, match);
+               /* don't bother receiving the property */
+               (void) nvlist_remove_nvpair(props, pair);
+next:
+               pair = next_pair;
+       }
+}
+
+#ifdef DEBUG
+static boolean_t zfs_ioc_recv_inject_err;
+#endif
+
 /*
  * inputs:
  * zc_name             name of containing filesystem
@@ -2452,209 +3880,351 @@ clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
  * zc_cookie           file descriptor to recv from
  * zc_begin_record     the BEGIN record of the stream (not byteswapped)
  * zc_guid             force flag
+ * zc_cleanup_fd       cleanup-on-exit file descriptor
+ * zc_action_handle    handle for this guid/ds mapping (or zero on first call)
  *
  * outputs:
  * zc_cookie           number of bytes read
+ * zc_nvlist_dst{_size} error for each unapplied received property
+ * zc_obj              zprop_errflags_t
+ * zc_action_handle    handle for this guid/ds mapping
  */
 static int
 zfs_ioc_recv(zfs_cmd_t *zc)
 {
        file_t *fp;
-       objset_t *os;
        dmu_recv_cookie_t drc;
-       zfsvfs_t *zfsvfs = NULL;
        boolean_t force = (boolean_t)zc->zc_guid;
-       int error, fd;
+       int fd;
+       int error = 0;
+       int props_error = 0;
+       nvlist_t *errors;
        offset_t off;
-       nvlist_t *props = NULL;
-       nvlist_t *origprops = NULL;
-       objset_t *origin = NULL;
+       nvlist_t *props = NULL; /* sent properties */
+       nvlist_t *origprops = NULL; /* existing properties */
+       char *origin = NULL;
        char *tosnap;
        char tofs[ZFS_MAXNAMELEN];
+       boolean_t first_recvd_props = B_FALSE;
 
        if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
            strchr(zc->zc_value, '@') == NULL ||
            strchr(zc->zc_value, '%'))
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
        (void) strcpy(tofs, zc->zc_value);
        tosnap = strchr(tofs, '@');
-       *tosnap = '\0';
-       tosnap++;
+       *tosnap++ = '\0';
 
-       if (zc->zc_nvlist_src != NULL &&
+       if (zc->zc_nvlist_src != 0 &&
            (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
-           &props)) != 0)
+           zc->zc_iflags, &props)) != 0)
                return (error);
 
        fd = zc->zc_cookie;
        fp = getf(fd);
        if (fp == NULL) {
                nvlist_free(props);
-               return (EBADF);
+               return (SET_ERROR(EBADF));
        }
 
-       if (dmu_objset_open(tofs, DMU_OST_ANY,
-           DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
-               /*
-                * Try to get the zfsvfs for the receiving objset.
-                * There won't be one if we're operating on a zvol,
-                * if the objset doesn't exist yet, or is not mounted.
-                */
-               mutex_enter(&os->os->os_user_ptr_lock);
-               if (zfsvfs = dmu_objset_get_user(os)) {
-                       if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
-                               mutex_exit(&os->os->os_user_ptr_lock);
-                               dmu_objset_close(os);
-                               zfsvfs = NULL;
-                               error = EBUSY;
-                               goto out;
-                       }
-                       VFS_HOLD(zfsvfs->z_vfs);
-               }
-               mutex_exit(&os->os->os_user_ptr_lock);
+       VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
+
+       if (zc->zc_string[0])
+               origin = zc->zc_string;
+
+       error = dmu_recv_begin(tofs, tosnap,
+           &zc->zc_begin_record, force, origin, &drc);
+       if (error != 0)
+               goto out;
+
+       /*
+        * Set properties before we receive the stream so that they are applied
+        * to the new data. Note that we must call dmu_recv_stream() if
+        * dmu_recv_begin() succeeds.
+        */
+       if (props != NULL && !drc.drc_newfs) {
+               if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
+                   SPA_VERSION_RECVD_PROPS &&
+                   !dsl_prop_get_hasrecvd(tofs))
+                       first_recvd_props = B_TRUE;
 
                /*
-                * If new properties are supplied, they are to completely
-                * replace the existing ones, so stash away the existing ones.
+                * If new received properties are supplied, they are to
+                * completely replace the existing received properties, so stash
+                * away the existing ones.
                 */
-               if (props)
-                       (void) dsl_prop_get_all(os, &origprops, TRUE);
-
-               dmu_objset_close(os);
+               if (dsl_prop_get_received(tofs, &origprops) == 0) {
+                       nvlist_t *errlist = NULL;
+                       /*
+                        * Don't bother writing a property if its value won't
+                        * change (and avoid the unnecessary security checks).
+                        *
+                        * The first receive after SPA_VERSION_RECVD_PROPS is a
+                        * special case where we blow away all local properties
+                        * regardless.
+                        */
+                       if (!first_recvd_props)
+                               props_reduce(props, origprops);
+                       if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
+                               (void) nvlist_merge(errors, errlist, 0);
+                       nvlist_free(errlist);
+
+                       if (clear_received_props(tofs, origprops,
+                           first_recvd_props ? NULL : props) != 0)
+                               zc->zc_obj |= ZPROP_ERR_NOCLEAR;
+               } else {
+                       zc->zc_obj |= ZPROP_ERR_NOCLEAR;
+               }
        }
 
-       if (zc->zc_string[0]) {
-               error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
-                   DS_MODE_USER | DS_MODE_READONLY, &origin);
-               if (error)
-                       goto out;
-       }
+       if (props != NULL) {
+               props_error = dsl_prop_set_hasrecvd(tofs);
 
-       error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
-           force, origin, zfsvfs != NULL, &drc);
-       if (origin)
-               dmu_objset_close(origin);
-       if (error)
-               goto out;
+               if (props_error == 0) {
+                       (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
+                           props, errors);
+               }
+       }
 
-       /*
-        * Reset properties.  We do this before we receive the stream
-        * so that the properties are applied to the new data.
-        */
-       if (props) {
-               clear_props(tofs, origprops, props);
+       if (zc->zc_nvlist_dst_size != 0 &&
+           (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
+           put_nvlist(zc, errors) != 0)) {
                /*
-                * XXX - Note, this is all-or-nothing; should be best-effort.
+                * Caller made zc->zc_nvlist_dst less than the minimum expected
+                * size or supplied an invalid address.
                 */
-               (void) zfs_set_prop_nvlist(tofs, props);
+               props_error = SET_ERROR(EINVAL);
        }
 
        off = fp->f_offset;
-       error = dmu_recv_stream(&drc, fp->f_vnode, &off);
+       error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
+           &zc->zc_action_handle);
 
-       if (error == 0 && zfsvfs) {
-               char *osname;
-               int mode;
+       if (error == 0) {
+               zfs_sb_t *zsb = NULL;
 
-               /* online recv */
-               osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
-               error = zfs_suspend_fs(zfsvfs, osname, &mode);
-               if (error == 0) {
-                       int resume_err;
+               if (get_zfs_sb(tofs, &zsb) == 0) {
+                       /* online recv */
+                       int end_err;
 
-                       error = dmu_recv_end(&drc);
-                       resume_err = zfs_resume_fs(zfsvfs, osname, mode);
-                       error = error ? error : resume_err;
+                       error = zfs_suspend_fs(zsb);
+                       /*
+                        * If the suspend fails, then the recv_end will
+                        * likely also fail, and clean up after itself.
+                        */
+                       end_err = dmu_recv_end(&drc, zsb);
+                       if (error == 0)
+                               error = zfs_resume_fs(zsb, tofs);
+                       error = error ? error : end_err;
+                       deactivate_super(zsb->z_sb);
                } else {
-                       dmu_recv_abort_cleanup(&drc);
+                       error = dmu_recv_end(&drc, NULL);
                }
-               kmem_free(osname, MAXNAMELEN);
-       } else if (error == 0) {
-               error = dmu_recv_end(&drc);
        }
 
        zc->zc_cookie = off - fp->f_offset;
        if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
                fp->f_offset = off;
 
+#ifdef DEBUG
+       if (zfs_ioc_recv_inject_err) {
+               zfs_ioc_recv_inject_err = B_FALSE;
+               error = 1;
+       }
+#endif
+
+#ifdef _KERNEL
+       if (error == 0)
+               zvol_create_minors(tofs);
+#endif
+
        /*
         * On error, restore the original props.
         */
-       if (error && props) {
-               clear_props(tofs, props, NULL);
-               (void) zfs_set_prop_nvlist(tofs, origprops);
+       if (error != 0 && props != NULL && !drc.drc_newfs) {
+               if (clear_received_props(tofs, props, NULL) != 0) {
+                       /*
+                        * We failed to clear the received properties.
+                        * Since we may have left a $recvd value on the
+                        * system, we can't clear the $hasrecvd flag.
+                        */
+                       zc->zc_obj |= ZPROP_ERR_NORESTORE;
+               } else if (first_recvd_props) {
+                       dsl_prop_unset_hasrecvd(tofs);
+               }
+
+               if (origprops == NULL && !drc.drc_newfs) {
+                       /* We failed to stash the original properties. */
+                       zc->zc_obj |= ZPROP_ERR_NORESTORE;
+               }
+
+               /*
+                * dsl_props_set() will not convert RECEIVED to LOCAL on or
+                * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
+                * explictly if we're restoring local properties cleared in the
+                * first new-style receive.
+                */
+               if (origprops != NULL &&
+                   zfs_set_prop_nvlist(tofs, (first_recvd_props ?
+                   ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
+                   origprops, NULL) != 0) {
+                       /*
+                        * We stashed the original properties but failed to
+                        * restore them.
+                        */
+                       zc->zc_obj |= ZPROP_ERR_NORESTORE;
+               }
        }
 out:
-       if (zfsvfs) {
-               mutex_exit(&zfsvfs->z_online_recv_lock);
-               VFS_RELE(zfsvfs->z_vfs);
-       }
        nvlist_free(props);
        nvlist_free(origprops);
+       nvlist_free(errors);
        releasef(fd);
+
+       if (error == 0)
+               error = props_error;
+
        return (error);
 }
 
 /*
  * inputs:
  * zc_name     name of snapshot to send
- * zc_value    short name of incremental fromsnap (may be empty)
  * zc_cookie   file descriptor to send stream to
- * zc_obj      fromorigin flag (mutually exclusive with zc_value)
+ * zc_obj      fromorigin flag (mutually exclusive with zc_fromobj)
+ * zc_sendobj  objsetid of snapshot to send
+ * zc_fromobj  objsetid of incremental fromsnap (may be zero)
+ * zc_guid     if set, estimate size of stream only.  zc_cookie is ignored.
+ *             output size in zc_objset_type.
  *
  * outputs: none
  */
 static int
 zfs_ioc_send(zfs_cmd_t *zc)
 {
-       objset_t *fromsnap = NULL;
-       objset_t *tosnap;
-       file_t *fp;
        int error;
        offset_t off;
+       boolean_t estimate = (zc->zc_guid != 0);
+
+       if (zc->zc_obj != 0) {
+               dsl_pool_t *dp;
+               dsl_dataset_t *tosnap;
 
-       error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
-           DS_MODE_USER | DS_MODE_READONLY, &tosnap);
-       if (error)
-               return (error);
-
-       if (zc->zc_value[0] != '\0') {
-               char *buf;
-               char *cp;
-
-               buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
-               (void) strncpy(buf, zc->zc_name, MAXPATHLEN);
-               cp = strchr(buf, '@');
-               if (cp)
-                       *(cp+1) = 0;
-               (void) strncat(buf, zc->zc_value, MAXPATHLEN);
-               error = dmu_objset_open(buf, DMU_OST_ANY,
-                   DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
-               kmem_free(buf, MAXPATHLEN);
-               if (error) {
-                       dmu_objset_close(tosnap);
+               error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
+               if (error != 0)
+                       return (error);
+
+               error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
+               if (error != 0) {
+                       dsl_pool_rele(dp, FTAG);
                        return (error);
                }
+
+               if (dsl_dir_is_clone(tosnap->ds_dir))
+                       zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj;
+               dsl_dataset_rele(tosnap, FTAG);
+               dsl_pool_rele(dp, FTAG);
        }
 
-       fp = getf(zc->zc_cookie);
-       if (fp == NULL) {
-               dmu_objset_close(tosnap);
-               if (fromsnap)
-                       dmu_objset_close(fromsnap);
-               return (EBADF);
+       if (estimate) {
+               dsl_pool_t *dp;
+               dsl_dataset_t *tosnap;
+               dsl_dataset_t *fromsnap = NULL;
+
+               error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
+               if (error != 0)
+                       return (error);
+
+               error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
+               if (error != 0) {
+                       dsl_pool_rele(dp, FTAG);
+                       return (error);
+               }
+
+               if (zc->zc_fromobj != 0) {
+                       error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
+                           FTAG, &fromsnap);
+                       if (error != 0) {
+                               dsl_dataset_rele(tosnap, FTAG);
+                               dsl_pool_rele(dp, FTAG);
+                               return (error);
+                       }
+               }
+
+               error = dmu_send_estimate(tosnap, fromsnap,
+                   &zc->zc_objset_type);
+
+               if (fromsnap != NULL)
+                       dsl_dataset_rele(fromsnap, FTAG);
+               dsl_dataset_rele(tosnap, FTAG);
+               dsl_pool_rele(dp, FTAG);
+       } else {
+               file_t *fp = getf(zc->zc_cookie);
+               if (fp == NULL)
+                       return (SET_ERROR(EBADF));
+
+               off = fp->f_offset;
+               error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
+                   zc->zc_fromobj, zc->zc_cookie, fp->f_vnode, &off);
+
+               if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
+                       fp->f_offset = off;
+               releasef(zc->zc_cookie);
        }
+       return (error);
+}
 
-       off = fp->f_offset;
-       error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
+/*
+ * inputs:
+ * zc_name     name of snapshot on which to report progress
+ * zc_cookie   file descriptor of send stream
+ *
+ * outputs:
+ * zc_cookie   number of bytes written in send stream thus far
+ */
+static int
+zfs_ioc_send_progress(zfs_cmd_t *zc)
+{
+       dsl_pool_t *dp;
+       dsl_dataset_t *ds;
+       dmu_sendarg_t *dsp = NULL;
+       int error;
 
-       if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
-               fp->f_offset = off;
-       releasef(zc->zc_cookie);
-       if (fromsnap)
-               dmu_objset_close(fromsnap);
-       dmu_objset_close(tosnap);
+       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);
+       }
+
+       mutex_enter(&ds->ds_sendstream_lock);
+
+       /*
+        * Iterate over all the send streams currently active on this dataset.
+        * If there's one which matches the specified file descriptor _and_ the
+        * stream was started by the current process, return the progress of
+        * that stream.
+        */
+
+       for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
+           dsp = list_next(&ds->ds_sendstreams, dsp)) {
+               if (dsp->dsa_outfd == zc->zc_cookie &&
+                   dsp->dsa_proc->group_leader == curproc->group_leader)
+                       break;
+       }
+
+       if (dsp != NULL)
+               zc->zc_cookie = *(dsp->dsa_off);
+       else
+               error = SET_ERROR(ENOENT);
+
+       mutex_exit(&ds->ds_sendstream_lock);
+       dsl_dataset_rele(ds, FTAG);
+       dsl_pool_rele(dp, FTAG);
        return (error);
 }
 
@@ -2728,18 +4298,43 @@ zfs_ioc_clear(zfs_cmd_t *zc)
        spa = spa_lookup(zc->zc_name);
        if (spa == NULL) {
                mutex_exit(&spa_namespace_lock);
-               return (EIO);
+               return (SET_ERROR(EIO));
        }
-       if (spa->spa_log_state == SPA_LOG_MISSING) {
+       if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
                /* we need to let spa_open/spa_load clear the chains */
-               spa->spa_log_state = SPA_LOG_CLEAR;
+               spa_set_log_state(spa, SPA_LOG_CLEAR);
        }
+       spa->spa_last_open_failed = 0;
        mutex_exit(&spa_namespace_lock);
 
-       if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
+       if (zc->zc_cookie & ZPOOL_NO_REWIND) {
+               error = spa_open(zc->zc_name, &spa, FTAG);
+       } else {
+               nvlist_t *policy;
+               nvlist_t *config = NULL;
+
+               if (zc->zc_nvlist_src == 0)
+                       return (SET_ERROR(EINVAL));
+
+               if ((error = get_nvlist(zc->zc_nvlist_src,
+                   zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
+                       error = spa_open_rewind(zc->zc_name, &spa, FTAG,
+                           policy, &config);
+                       if (config != NULL) {
+                               int err;
+
+                               if ((err = put_nvlist(zc, config)) != 0)
+                                       error = err;
+                               nvlist_free(config);
+                       }
+                       nvlist_free(policy);
+               }
+       }
+
+       if (error != 0)
                return (error);
 
-       spa_vdev_state_enter(spa);
+       spa_vdev_state_enter(spa, SCL_NONE);
 
        if (zc->zc_guid == 0) {
                vd = NULL;
@@ -2748,7 +4343,7 @@ zfs_ioc_clear(zfs_cmd_t *zc)
                if (vd == NULL) {
                        (void) spa_vdev_state_exit(spa, NULL, ENODEV);
                        spa_close(spa, FTAG);
-                       return (ENODEV);
+                       return (SET_ERROR(ENODEV));
                }
        }
 
@@ -2759,400 +4354,1420 @@ zfs_ioc_clear(zfs_cmd_t *zc)
        /*
         * Resume any suspended I/Os.
         */
-       zio_resume(spa);
+       if (zio_resume(spa) != 0)
+               error = SET_ERROR(EIO);
 
        spa_close(spa, FTAG);
 
-       return (0);
+       return (error);
 }
 
-/*
- * inputs:
- * zc_name     name of filesystem
- * zc_value    name of origin snapshot
- *
- * outputs:    none
- */
 static int
-zfs_ioc_promote(zfs_cmd_t *zc)
+zfs_ioc_pool_reopen(zfs_cmd_t *zc)
 {
-       char *cp;
+       spa_t *spa;
+       int error;
 
-       /*
-        * We don't need to unmount *all* the origin fs's snapshots, but
-        * it's easier.
+       error = spa_open(zc->zc_name, &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.
+        */
+       spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
+       vdev_reopen(spa->spa_root_vdev);
+       spa->spa_scrub_reopen = B_FALSE;
+
+       (void) spa_vdev_state_exit(spa, NULL, 0);
+       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
+ */
+static int
+zfs_ioc_promote(zfs_cmd_t *zc)
+{
+       char *cp;
+
+       /*
+        * We don't need to unmount *all* the origin fs's snapshots, but
+        * it's easier.
         */
        cp = strchr(zc->zc_value, '@');
        if (cp)
                *cp = '\0';
        (void) dmu_objset_find(zc->zc_value,
-           zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
-       return (dsl_dataset_promote(zc->zc_name));
+           zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
+       return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
+}
+
+/*
+ * Retrieve a single {user|group}{used|quota}@... property.
+ *
+ * inputs:
+ * zc_name     name of filesystem
+ * zc_objset_type zfs_userquota_prop_t
+ * zc_value    domain name (eg. "S-1-234-567-89")
+ * zc_guid     RID/UID/GID
+ *
+ * outputs:
+ * zc_cookie   property value
+ */
+static int
+zfs_ioc_userspace_one(zfs_cmd_t *zc)
+{
+       zfs_sb_t *zsb;
+       int error;
+
+       if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
+               return (SET_ERROR(EINVAL));
+
+       error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
+       if (error != 0)
+               return (error);
+
+       error = zfs_userspace_one(zsb,
+           zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
+       zfs_sb_rele(zsb, FTAG);
+
+       return (error);
+}
+
+/*
+ * inputs:
+ * zc_name             name of filesystem
+ * zc_cookie           zap cursor
+ * zc_objset_type      zfs_userquota_prop_t
+ * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
+ *
+ * outputs:
+ * zc_nvlist_dst[_size]        data buffer (array of zfs_useracct_t)
+ * zc_cookie   zap cursor
+ */
+static int
+zfs_ioc_userspace_many(zfs_cmd_t *zc)
+{
+       zfs_sb_t *zsb;
+       int bufsize = zc->zc_nvlist_dst_size;
+       int error;
+       void *buf;
+
+       if (bufsize <= 0)
+               return (SET_ERROR(ENOMEM));
+
+       error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
+       if (error != 0)
+               return (error);
+
+       buf = vmem_alloc(bufsize, KM_SLEEP);
+
+       error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
+           buf, &zc->zc_nvlist_dst_size);
+
+       if (error == 0) {
+               error = xcopyout(buf,
+                   (void *)(uintptr_t)zc->zc_nvlist_dst,
+                   zc->zc_nvlist_dst_size);
+       }
+       vmem_free(buf, bufsize);
+       zfs_sb_rele(zsb, FTAG);
+
+       return (error);
+}
+
+/*
+ * inputs:
+ * zc_name             name of filesystem
+ *
+ * outputs:
+ * none
+ */
+static int
+zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
+{
+       objset_t *os;
+       int error = 0;
+       zfs_sb_t *zsb;
+
+       if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
+               if (!dmu_objset_userused_enabled(zsb->z_os)) {
+                       /*
+                        * If userused is not enabled, it may be because the
+                        * objset needs to be closed & reopened (to grow the
+                        * objset_phys_t).  Suspend/resume the fs will do that.
+                        */
+                       error = zfs_suspend_fs(zsb);
+                       if (error == 0) {
+                               dmu_objset_refresh_ownership(zsb->z_os,
+                                   zsb);
+                               error = zfs_resume_fs(zsb, zc->zc_name);
+                       }
+               }
+               if (error == 0)
+                       error = dmu_objset_userspace_upgrade(zsb->z_os);
+               deactivate_super(zsb->z_sb);
+       } else {
+               /* XXX kind of reading contents without owning */
+               error = dmu_objset_hold(zc->zc_name, FTAG, &os);
+               if (error != 0)
+                       return (error);
+
+               error = dmu_objset_userspace_upgrade(os);
+               dmu_objset_rele(os, FTAG);
+       }
+
+       return (error);
+}
+
+static int
+zfs_ioc_share(zfs_cmd_t *zc)
+{
+       return (SET_ERROR(ENOSYS));
+}
+
+ace_t full_access[] = {
+       {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
+};
+
+/*
+ * inputs:
+ * zc_name             name of containing filesystem
+ * zc_obj              object # beyond which we want next in-use object #
+ *
+ * outputs:
+ * zc_obj              next in-use object #
+ */
+static int
+zfs_ioc_next_obj(zfs_cmd_t *zc)
+{
+       objset_t *os = NULL;
+       int error;
+
+       error = dmu_objset_hold(zc->zc_name, FTAG, &os);
+       if (error != 0)
+               return (error);
+
+       error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
+           os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
+
+       dmu_objset_rele(os, FTAG);
+       return (error);
+}
+
+/*
+ * inputs:
+ * zc_name             name of filesystem
+ * zc_value            prefix name for snapshot
+ * zc_cleanup_fd       cleanup-on-exit file descriptor for calling process
+ *
+ * outputs:
+ * zc_value            short name of new snapshot
+ */
+static int
+zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
+{
+       char *snap_name;
+       char *hold_name;
+       int error;
+       minor_t minor;
+
+       error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
+       if (error != 0)
+               return (error);
+
+       snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
+           (u_longlong_t)ddi_get_lbolt64());
+       hold_name = kmem_asprintf("%%%s", zc->zc_value);
+
+       error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
+           hold_name);
+       if (error == 0)
+               (void) strcpy(zc->zc_value, snap_name);
+       strfree(snap_name);
+       strfree(hold_name);
+       zfs_onexit_fd_rele(zc->zc_cleanup_fd);
+       return (error);
+}
+
+/*
+ * inputs:
+ * zc_name             name of "to" snapshot
+ * zc_value            name of "from" snapshot
+ * zc_cookie           file descriptor to write diff data on
+ *
+ * outputs:
+ * dmu_diff_record_t's to the file descriptor
+ */
+static int
+zfs_ioc_diff(zfs_cmd_t *zc)
+{
+       file_t *fp;
+       offset_t off;
+       int error;
+
+       fp = getf(zc->zc_cookie);
+       if (fp == NULL)
+               return (SET_ERROR(EBADF));
+
+       off = fp->f_offset;
+
+       error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
+
+       if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
+               fp->f_offset = off;
+       releasef(zc->zc_cookie);
+
+       return (error);
+}
+
+/*
+ * Remove all ACL files in shares dir
+ */
+#ifdef HAVE_SMB_SHARE
+static int
+zfs_smb_acl_purge(znode_t *dzp)
+{
+       zap_cursor_t    zc;
+       zap_attribute_t zap;
+       zfs_sb_t *zsb = ZTOZSB(dzp);
+       int error;
+
+       for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
+           (error = zap_cursor_retrieve(&zc, &zap)) == 0;
+           zap_cursor_advance(&zc)) {
+               if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
+                   NULL, 0)) != 0)
+                       break;
+       }
+       zap_cursor_fini(&zc);
+       return (error);
+}
+#endif /* HAVE_SMB_SHARE */
+
+static int
+zfs_ioc_smb_acl(zfs_cmd_t *zc)
+{
+#ifdef HAVE_SMB_SHARE
+       vnode_t *vp;
+       znode_t *dzp;
+       vnode_t *resourcevp = NULL;
+       znode_t *sharedir;
+       zfs_sb_t *zsb;
+       nvlist_t *nvlist;
+       char *src, *target;
+       vattr_t vattr;
+       vsecattr_t vsec;
+       int error = 0;
+
+       if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
+           NO_FOLLOW, NULL, &vp)) != 0)
+               return (error);
+
+       /* Now make sure mntpnt and dataset are ZFS */
+
+       if (vp->v_vfsp->vfs_fstype != zfsfstype ||
+           (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
+           zc->zc_name) != 0)) {
+               VN_RELE(vp);
+               return (SET_ERROR(EINVAL));
+       }
+
+       dzp = VTOZ(vp);
+       zsb = ZTOZSB(dzp);
+       ZFS_ENTER(zsb);
+
+       /*
+        * Create share dir if its missing.
+        */
+       mutex_enter(&zsb->z_lock);
+       if (zsb->z_shares_dir == 0) {
+               dmu_tx_t *tx;
+
+               tx = dmu_tx_create(zsb->z_os);
+               dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
+                   ZFS_SHARES_DIR);
+               dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
+               error = dmu_tx_assign(tx, TXG_WAIT);
+               if (error != 0) {
+                       dmu_tx_abort(tx);
+               } else {
+                       error = zfs_create_share_dir(zsb, tx);
+                       dmu_tx_commit(tx);
+               }
+               if (error != 0) {
+                       mutex_exit(&zsb->z_lock);
+                       VN_RELE(vp);
+                       ZFS_EXIT(zsb);
+                       return (error);
+               }
+       }
+       mutex_exit(&zsb->z_lock);
+
+       ASSERT(zsb->z_shares_dir);
+       if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
+               VN_RELE(vp);
+               ZFS_EXIT(zsb);
+               return (error);
+       }
+
+       switch (zc->zc_cookie) {
+       case ZFS_SMB_ACL_ADD:
+               vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
+               vattr.va_mode = S_IFREG|0777;
+               vattr.va_uid = 0;
+               vattr.va_gid = 0;
+
+               vsec.vsa_mask = VSA_ACE;
+               vsec.vsa_aclentp = &full_access;
+               vsec.vsa_aclentsz = sizeof (full_access);
+               vsec.vsa_aclcnt = 1;
+
+               error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
+                   &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
+               if (resourcevp)
+                       VN_RELE(resourcevp);
+               break;
+
+       case ZFS_SMB_ACL_REMOVE:
+               error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
+                   NULL, 0);
+               break;
+
+       case ZFS_SMB_ACL_RENAME:
+               if ((error = get_nvlist(zc->zc_nvlist_src,
+                   zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
+                       VN_RELE(vp);
+                       ZFS_EXIT(zsb);
+                       return (error);
+               }
+               if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
+                   nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
+                   &target)) {
+                       VN_RELE(vp);
+                       VN_RELE(ZTOV(sharedir));
+                       ZFS_EXIT(zsb);
+                       nvlist_free(nvlist);
+                       return (error);
+               }
+               error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
+                   kcred, NULL, 0);
+               nvlist_free(nvlist);
+               break;
+
+       case ZFS_SMB_ACL_PURGE:
+               error = zfs_smb_acl_purge(sharedir);
+               break;
+
+       default:
+               error = SET_ERROR(EINVAL);
+               break;
+       }
+
+       VN_RELE(vp);
+       VN_RELE(ZTOV(sharedir));
+
+       ZFS_EXIT(zsb);
+
+       return (error);
+#else
+       return (SET_ERROR(ENOTSUP));
+#endif /* HAVE_SMB_SHARE */
+}
+
+/*
+ * innvl: {
+ *     "holds" -> { snapname -> holdname (string), ... }
+ *     (optional) "cleanup_fd" -> fd (int32)
+ * }
+ *
+ * outnvl: {
+ *     snapname -> error value (int32)
+ *     ...
+ * }
+ */
+/* ARGSUSED */
+static int
+zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
+{
+       nvlist_t *holds;
+       int cleanup_fd = -1;
+       int error;
+       minor_t minor = 0;
+
+       error = nvlist_lookup_nvlist(args, "holds", &holds);
+       if (error != 0)
+               return (SET_ERROR(EINVAL));
+
+       if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
+               error = zfs_onexit_fd_hold(cleanup_fd, &minor);
+               if (error != 0)
+                       return (error);
+       }
+
+       error = dsl_dataset_user_hold(holds, minor, errlist);
+       if (minor != 0)
+               zfs_onexit_fd_rele(cleanup_fd);
+       return (error);
+}
+
+/*
+ * innvl is not used.
+ *
+ * outnvl: {
+ *    holdname -> time added (uint64 seconds since epoch)
+ *    ...
+ * }
+ */
+/* ARGSUSED */
+static int
+zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
+{
+       return (dsl_dataset_get_holds(snapname, outnvl));
+}
+
+/*
+ * innvl: {
+ *     snapname -> { holdname, ... }
+ *     ...
+ * }
+ *
+ * outnvl: {
+ *     snapname -> error value (int32)
+ *     ...
+ * }
+ */
+/* ARGSUSED */
+static int
+zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
+{
+       return (dsl_dataset_user_release(holds, errlist));
+}
+
+/*
+ * inputs:
+ * zc_guid             flags (ZEVENT_NONBLOCK)
+ *
+ * outputs:
+ * zc_nvlist_dst       next nvlist event
+ * zc_cookie           dropped events since last get
+ * zc_cleanup_fd       cleanup-on-exit file descriptor
+ */
+static int
+zfs_ioc_events_next(zfs_cmd_t *zc)
+{
+       zfs_zevent_t *ze;
+       nvlist_t *event = NULL;
+       minor_t minor;
+       uint64_t dropped = 0;
+       int error;
+
+       error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
+       if (error != 0)
+               return (error);
+
+       do {
+               error = zfs_zevent_next(ze, &event,
+                       &zc->zc_nvlist_dst_size, &dropped);
+               if (event != NULL) {
+                       zc->zc_cookie = dropped;
+                       error = put_nvlist(zc, event);
+                       nvlist_free(event);
+               }
+
+               if (zc->zc_guid & ZEVENT_NONBLOCK)
+                       break;
+
+               if ((error == 0) || (error != ENOENT))
+                       break;
+
+               error = zfs_zevent_wait(ze);
+               if (error != 0)
+                       break;
+       } while (1);
+
+       zfs_zevent_fd_rele(zc->zc_cleanup_fd);
+
+       return (error);
+}
+
+/*
+ * outputs:
+ * zc_cookie           cleared events count
+ */
+static int
+zfs_ioc_events_clear(zfs_cmd_t *zc)
+{
+       int count;
+
+       zfs_zevent_drain_all(&count);
+       zc->zc_cookie = count;
+
+       return (0);
+}
+
+/*
+ * inputs:
+ * zc_name             name of new filesystem or snapshot
+ * zc_value            full name of old snapshot
+ *
+ * outputs:
+ * zc_cookie           space in bytes
+ * zc_objset_type      compressed space in bytes
+ * zc_perm_action      uncompressed space in bytes
+ */
+static int
+zfs_ioc_space_written(zfs_cmd_t *zc)
+{
+       int error;
+       dsl_pool_t *dp;
+       dsl_dataset_t *new, *old;
+
+       error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
+       if (error != 0)
+               return (error);
+       error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
+       if (error != 0) {
+               dsl_pool_rele(dp, FTAG);
+               return (error);
+       }
+       error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
+       if (error != 0) {
+               dsl_dataset_rele(new, FTAG);
+               dsl_pool_rele(dp, FTAG);
+               return (error);
+       }
+
+       error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
+           &zc->zc_objset_type, &zc->zc_perm_action);
+       dsl_dataset_rele(old, FTAG);
+       dsl_dataset_rele(new, FTAG);
+       dsl_pool_rele(dp, FTAG);
+       return (error);
+}
+
+/*
+ * innvl: {
+ *     "firstsnap" -> snapshot name
+ * }
+ *
+ * outnvl: {
+ *     "used" -> space in bytes
+ *     "compressed" -> compressed space in bytes
+ *     "uncompressed" -> uncompressed space in bytes
+ * }
+ */
+static int
+zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
+{
+       int error;
+       dsl_pool_t *dp;
+       dsl_dataset_t *new, *old;
+       char *firstsnap;
+       uint64_t used, comp, uncomp;
+
+       if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
+               return (SET_ERROR(EINVAL));
+
+       error = dsl_pool_hold(lastsnap, FTAG, &dp);
+       if (error != 0)
+               return (error);
+
+       error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
+       if (error != 0) {
+               dsl_pool_rele(dp, FTAG);
+               return (error);
+       }
+       error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
+       if (error != 0) {
+               dsl_dataset_rele(new, FTAG);
+               dsl_pool_rele(dp, FTAG);
+               return (error);
+       }
+
+       error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
+       dsl_dataset_rele(old, FTAG);
+       dsl_dataset_rele(new, FTAG);
+       dsl_pool_rele(dp, FTAG);
+       fnvlist_add_uint64(outnvl, "used", used);
+       fnvlist_add_uint64(outnvl, "compressed", comp);
+       fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
+       return (error);
 }
 
 /*
- * We don't want to have a hard dependency
- * against some special symbols in sharefs
- * nfs, and smbsrv.  Determine them if needed when
- * the first file system is shared.
- * Neither sharefs, nfs or smbsrv are unloadable modules.
+ * innvl: {
+ *     "fd" -> file descriptor to write stream to (int32)
+ *     (optional) "fromsnap" -> full snap name to send an incremental from
+ * }
+ *
+ * outnvl is unused
  */
-int (*znfsexport_fs)(void *arg);
-int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
-int (*zsmbexport_fs)(void *arg, boolean_t add_share);
-
-int zfs_nfsshare_inited;
-int zfs_smbshare_inited;
-
-ddi_modhandle_t nfs_mod;
-ddi_modhandle_t sharefs_mod;
-ddi_modhandle_t smbsrv_mod;
-kmutex_t zfs_share_lock;
-
+/* ARGSUSED */
 static int
-zfs_init_sharefs()
+zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
 {
        int error;
+       offset_t off;
+       char *fromname = NULL;
+       int fd;
+       file_t *fp;
 
-       ASSERT(MUTEX_HELD(&zfs_share_lock));
-       /* Both NFS and SMB shares also require sharetab support. */
-       if (sharefs_mod == NULL && ((sharefs_mod =
-           ddi_modopen("fs/sharefs",
-           KRTLD_MODE_FIRST, &error)) == NULL)) {
-               return (ENOSYS);
-       }
-       if (zshare_fs == NULL && ((zshare_fs =
-           (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
-           ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
-               return (ENOSYS);
-       }
-       return (0);
+       error = nvlist_lookup_int32(innvl, "fd", &fd);
+       if (error != 0)
+               return (SET_ERROR(EINVAL));
+
+       (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
+
+       if ((fp = getf(fd)) == NULL)
+               return (SET_ERROR(EBADF));
+
+       off = fp->f_offset;
+       error = dmu_send(snapname, fromname, fd, fp->f_vnode, &off);
+
+       if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
+               fp->f_offset = off;
+
+       releasef(fd);
+       return (error);
 }
 
+/*
+ * Determine approximately how large a zfs send stream will be -- the number
+ * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
+ *
+ * innvl: {
+ *     (optional) "fromsnap" -> full snap name to send an incremental from
+ * }
+ *
+ * outnvl: {
+ *     "space" -> bytes of space (uint64)
+ * }
+ */
 static int
-zfs_ioc_share(zfs_cmd_t *zc)
+zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
 {
+       dsl_pool_t *dp;
+       dsl_dataset_t *fromsnap = NULL;
+       dsl_dataset_t *tosnap;
        int error;
-       int opcode;
+       char *fromname;
+       uint64_t space;
 
-       switch (zc->zc_share.z_sharetype) {
-       case ZFS_SHARE_NFS:
-       case ZFS_UNSHARE_NFS:
-               if (zfs_nfsshare_inited == 0) {
-                       mutex_enter(&zfs_share_lock);
-                       if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
-                           KRTLD_MODE_FIRST, &error)) == NULL)) {
-                               mutex_exit(&zfs_share_lock);
-                               return (ENOSYS);
-                       }
-                       if (znfsexport_fs == NULL &&
-                           ((znfsexport_fs = (int (*)(void *))
-                           ddi_modsym(nfs_mod,
-                           "nfs_export", &error)) == NULL)) {
-                               mutex_exit(&zfs_share_lock);
-                               return (ENOSYS);
-                       }
-                       error = zfs_init_sharefs();
-                       if (error) {
-                               mutex_exit(&zfs_share_lock);
-                               return (ENOSYS);
-                       }
-                       zfs_nfsshare_inited = 1;
-                       mutex_exit(&zfs_share_lock);
-               }
-               break;
-       case ZFS_SHARE_SMB:
-       case ZFS_UNSHARE_SMB:
-               if (zfs_smbshare_inited == 0) {
-                       mutex_enter(&zfs_share_lock);
-                       if (smbsrv_mod == NULL && ((smbsrv_mod =
-                           ddi_modopen("drv/smbsrv",
-                           KRTLD_MODE_FIRST, &error)) == NULL)) {
-                               mutex_exit(&zfs_share_lock);
-                               return (ENOSYS);
-                       }
-                       if (zsmbexport_fs == NULL && ((zsmbexport_fs =
-                           (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
-                           "smb_server_share", &error)) == NULL)) {
-                               mutex_exit(&zfs_share_lock);
-                               return (ENOSYS);
-                       }
-                       error = zfs_init_sharefs();
-                       if (error) {
-                               mutex_exit(&zfs_share_lock);
-                               return (ENOSYS);
-                       }
-                       zfs_smbshare_inited = 1;
-                       mutex_exit(&zfs_share_lock);
-               }
-               break;
-       default:
-               return (EINVAL);
+       error = dsl_pool_hold(snapname, FTAG, &dp);
+       if (error != 0)
+               return (error);
+
+       error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
+       if (error != 0) {
+               dsl_pool_rele(dp, FTAG);
+               return (error);
        }
 
-       switch (zc->zc_share.z_sharetype) {
-       case ZFS_SHARE_NFS:
-       case ZFS_UNSHARE_NFS:
-               if (error =
-                   znfsexport_fs((void *)
-                   (uintptr_t)zc->zc_share.z_exportdata))
-                       return (error);
-               break;
-       case ZFS_SHARE_SMB:
-       case ZFS_UNSHARE_SMB:
-               if (error = zsmbexport_fs((void *)
-                   (uintptr_t)zc->zc_share.z_exportdata,
-                   zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
-                   B_TRUE : B_FALSE)) {
+       error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
+       if (error == 0) {
+               error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
+               if (error != 0) {
+                       dsl_dataset_rele(tosnap, FTAG);
+                       dsl_pool_rele(dp, FTAG);
                        return (error);
                }
-               break;
        }
 
-       opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
-           zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
-           SHAREFS_ADD : SHAREFS_REMOVE;
+       error = dmu_send_estimate(tosnap, fromsnap, &space);
+       fnvlist_add_uint64(outnvl, "space", space);
 
-       /*
-        * Add or remove share from sharetab
-        */
-       error = zshare_fs(opcode,
-           (void *)(uintptr_t)zc->zc_share.z_sharedata,
-           zc->zc_share.z_sharemax);
-
-       return (error);
-
-}
-
-/*
- * pool create, destroy, and export don't log the history as part of
- * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
- * do the logging of those commands.
- */
-static zfs_ioc_vec_t zfs_ioc_vec[] = {
-       { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
-       { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE },
-       { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
-       { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE },
-       { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
-       { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
-       { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
-       { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
-       { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE },
-       { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
-       { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
-       { zfs_ioc_dataset_list_next, zfs_secpolicy_read,
-           DATASET_NAME, B_FALSE },
-       { zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
-           DATASET_NAME, B_FALSE },
-       { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
-       { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
-       { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
-       { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
-       { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
-       { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
-       { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE },
-       { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
-       { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
-       { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
-       { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
-       { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
-       { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
-       { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
-       { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
-       { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
-       { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
-       { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
-       { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE },
-       { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
-       { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
-       { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
-       { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
-           DATASET_NAME, B_FALSE },
-       { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
-       { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
-};
+       if (fromsnap != NULL)
+               dsl_dataset_rele(fromsnap, FTAG);
+       dsl_dataset_rele(tosnap, FTAG);
+       dsl_pool_rele(dp, FTAG);
+       return (error);
+}
 
-static int
-zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
+
+static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
+
+static void
+zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
+    zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
+    boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
 {
-       zfs_cmd_t *zc;
-       uint_t vec;
-       int error, rc;
+       zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
+
+       ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
+       ASSERT3U(ioc, <, ZFS_IOC_LAST);
+       ASSERT3P(vec->zvec_legacy_func, ==, NULL);
+       ASSERT3P(vec->zvec_func, ==, NULL);
+
+       vec->zvec_legacy_func = func;
+       vec->zvec_secpolicy = secpolicy;
+       vec->zvec_namecheck = namecheck;
+       vec->zvec_allow_log = log_history;
+       vec->zvec_pool_check = pool_check;
+}
+
+/*
+ * See the block comment at the beginning of this file for details on
+ * each argument to this function.
+ */
+static void
+zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
+    zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
+    zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
+    boolean_t allow_log)
+{
+       zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
+
+       ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
+       ASSERT3U(ioc, <, ZFS_IOC_LAST);
+       ASSERT3P(vec->zvec_legacy_func, ==, NULL);
+       ASSERT3P(vec->zvec_func, ==, NULL);
+
+       /* if we are logging, the name must be valid */
+       ASSERT(!allow_log || namecheck != NO_NAME);
+
+       vec->zvec_name = name;
+       vec->zvec_func = func;
+       vec->zvec_secpolicy = secpolicy;
+       vec->zvec_namecheck = namecheck;
+       vec->zvec_pool_check = pool_check;
+       vec->zvec_smush_outnvlist = smush_outnvlist;
+       vec->zvec_allow_log = allow_log;
+}
+
+static void
+zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
+    zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
+    zfs_ioc_poolcheck_t pool_check)
+{
+       zfs_ioctl_register_legacy(ioc, func, secpolicy,
+           POOL_NAME, log_history, pool_check);
+}
+
+static void
+zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
+    zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
+{
+       zfs_ioctl_register_legacy(ioc, func, secpolicy,
+           DATASET_NAME, B_FALSE, pool_check);
+}
 
-       if (getminor(dev) != 0)
-               return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
+static void
+zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
+{
+       zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
+           POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
+}
 
-       vec = cmd - ZFS_IOC;
-       ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
+static void
+zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
+    zfs_secpolicy_func_t *secpolicy)
+{
+       zfs_ioctl_register_legacy(ioc, func, secpolicy,
+           NO_NAME, B_FALSE, POOL_CHECK_NONE);
+}
 
-       if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
-               return (EINVAL);
+static void
+zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
+    zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
+{
+       zfs_ioctl_register_legacy(ioc, func, secpolicy,
+           DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
+}
 
-       zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
+static void
+zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
+{
+       zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
+           zfs_secpolicy_read);
+}
 
-       error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
+static void
+zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
+       zfs_secpolicy_func_t *secpolicy)
+{
+       zfs_ioctl_register_legacy(ioc, func, secpolicy,
+           DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
+}
 
-       if (error == 0)
-               error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
+static void
+zfs_ioctl_init(void)
+{
+       zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
+           zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
+
+       zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
+           zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
+
+       zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
+           zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
+           POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
+
+       zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
+           zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
+           POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
+
+       zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
+           zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
+           POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
+
+       zfs_ioctl_register("create", ZFS_IOC_CREATE,
+           zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
+
+       zfs_ioctl_register("clone", ZFS_IOC_CLONE,
+           zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
+
+       zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
+           zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
+
+       zfs_ioctl_register("hold", ZFS_IOC_HOLD,
+           zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
+       zfs_ioctl_register("release", ZFS_IOC_RELEASE,
+           zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
+
+       zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
+           zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
+           POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
+
+       zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
+           zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
+
+       /* IOCTLS that use the legacy function signature */
+
+       zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
+           zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
+
+       zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
+           zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
+           zfs_ioc_pool_scan);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
+           zfs_ioc_pool_upgrade);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
+           zfs_ioc_vdev_add);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
+           zfs_ioc_vdev_remove);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
+           zfs_ioc_vdev_set_state);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
+           zfs_ioc_vdev_attach);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
+           zfs_ioc_vdev_detach);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
+           zfs_ioc_vdev_setpath);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
+           zfs_ioc_vdev_setfru);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
+           zfs_ioc_pool_set_props);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
+           zfs_ioc_vdev_split);
+       zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
+           zfs_ioc_pool_reguid);
+
+       zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
+           zfs_ioc_pool_configs, zfs_secpolicy_none);
+       zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
+           zfs_ioc_pool_tryimport, zfs_secpolicy_config);
+       zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
+           zfs_ioc_inject_fault, zfs_secpolicy_inject);
+       zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
+           zfs_ioc_clear_fault, zfs_secpolicy_inject);
+       zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
+           zfs_ioc_inject_list_next, zfs_secpolicy_inject);
 
        /*
-        * Ensure that all pool/dataset names are valid before we pass down to
-        * the lower layers.
+        * pool destroy, and export don't log the history as part of
+        * zfsdev_ioctl, but rather zfs_ioc_pool_export
+        * does the logging of those commands.
         */
-       if (error == 0) {
-               zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
-               switch (zfs_ioc_vec[vec].zvec_namecheck) {
-               case POOL_NAME:
-                       if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
-                               error = EINVAL;
-                       break;
+       zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
+           zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
+       zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
+           zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
+
+       zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
+           zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
+       zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
+           zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
+
+       zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
+           zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
+       zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
+           zfs_ioc_dsobj_to_dsname,
+           zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
+       zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
+           zfs_ioc_pool_get_history,
+           zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
+
+       zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
+           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_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
+           zfs_ioc_space_written);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
+           zfs_ioc_objset_recvd_props);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
+           zfs_ioc_next_obj);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
+           zfs_ioc_get_fsacl);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
+           zfs_ioc_objset_stats);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
+           zfs_ioc_objset_zplprops);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
+           zfs_ioc_dataset_list_next);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
+           zfs_ioc_snapshot_list_next);
+       zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
+           zfs_ioc_send_progress);
+
+       zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
+           zfs_ioc_diff, zfs_secpolicy_diff);
+       zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
+           zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
+       zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
+           zfs_ioc_obj_to_path, zfs_secpolicy_diff);
+       zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
+           zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
+       zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
+           zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
+       zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
+           zfs_ioc_send, zfs_secpolicy_send);
+
+       zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
+           zfs_secpolicy_none);
+       zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
+           zfs_secpolicy_destroy);
+       zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
+           zfs_secpolicy_rename);
+       zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
+           zfs_secpolicy_recv);
+       zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
+           zfs_secpolicy_promote);
+       zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
+           zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
+       zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
+           zfs_secpolicy_set_fsacl);
+
+       zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
+           zfs_secpolicy_share, POOL_CHECK_NONE);
+       zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
+           zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
+       zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
+           zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
+       zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
+           zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
+           POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
 
-               case DATASET_NAME:
-                       if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
-                               error = EINVAL;
-                       break;
+       /*
+        * ZoL functions
+        */
+       zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
+           zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
+       zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
+           zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
+}
 
-               case NO_NAME:
-                       break;
+int
+pool_status_check(const char *name, zfs_ioc_namecheck_t type,
+    zfs_ioc_poolcheck_t check)
+{
+       spa_t *spa;
+       int error;
+
+       ASSERT(type == POOL_NAME || type == DATASET_NAME);
+
+       if (check & POOL_CHECK_NONE)
+               return (0);
+
+       error = spa_open(name, &spa, FTAG);
+       if (error == 0) {
+               if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
+                       error = SET_ERROR(EAGAIN);
+               else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
+                       error = SET_ERROR(EROFS);
+               spa_close(spa, FTAG);
+       }
+       return (error);
+}
+
+static void *
+zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
+{
+       zfsdev_state_t *zs;
+
+       ASSERT(MUTEX_HELD(&zfsdev_state_lock));
+
+       for (zs = list_head(&zfsdev_state_list); zs != NULL;
+           zs = list_next(&zfsdev_state_list, zs)) {
+               if (zs->zs_minor == minor) {
+                       switch (which) {
+                       case ZST_ONEXIT:
+                               return (zs->zs_onexit);
+                       case ZST_ZEVENT:
+                               return (zs->zs_zevent);
+                       case ZST_ALL:
+                               return (zs);
+                       }
                }
        }
 
-       if (error == 0)
-               error = zfs_ioc_vec[vec].zvec_func(zc);
+       return (NULL);
+}
 
-       rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
-       if (error == 0) {
-               error = rc;
-               if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
-                       zfs_log_history(zc);
+void *
+zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
+{
+       void *ptr;
+
+       mutex_enter(&zfsdev_state_lock);
+       ptr = zfsdev_get_state_impl(minor, which);
+       mutex_exit(&zfsdev_state_lock);
+
+       return (ptr);
+}
+
+minor_t
+zfsdev_getminor(struct file *filp)
+{
+       ASSERT(filp != NULL);
+       ASSERT(filp->private_data != NULL);
+
+       return (((zfsdev_state_t *)filp->private_data)->zs_minor);
+}
+
+/*
+ * Find a free minor number.  The zfsdev_state_list is expected to
+ * be short since it is only a list of currently open file handles.
+ */
+minor_t
+zfsdev_minor_alloc(void)
+{
+       static minor_t last_minor = 0;
+       minor_t m;
+
+       ASSERT(MUTEX_HELD(&zfsdev_state_lock));
+
+       for (m = last_minor + 1; m != last_minor; m++) {
+               if (m > ZFSDEV_MAX_MINOR)
+                       m = 1;
+               if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
+                       last_minor = m;
+                       return (m);
+               }
        }
 
-       kmem_free(zc, sizeof (zfs_cmd_t));
-       return (error);
+       return (0);
 }
 
 static int
-zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
+zfsdev_state_init(struct file *filp)
 {
-       if (cmd != DDI_ATTACH)
-               return (DDI_FAILURE);
+       zfsdev_state_t *zs;
+       minor_t minor;
+
+       ASSERT(MUTEX_HELD(&zfsdev_state_lock));
+
+       minor = zfsdev_minor_alloc();
+       if (minor == 0)
+               return (SET_ERROR(ENXIO));
+
+       zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
 
-       if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
-           DDI_PSEUDO, 0) == DDI_FAILURE)
-               return (DDI_FAILURE);
+       zs->zs_file = filp;
+       zs->zs_minor = minor;
+       filp->private_data = zs;
 
-       zfs_dip = dip;
+       zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
+       zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
 
-       ddi_report_dev(dip);
+       list_insert_tail(&zfsdev_state_list, zs);
 
-       return (DDI_SUCCESS);
+       return (0);
 }
 
 static int
-zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
+zfsdev_state_destroy(struct file *filp)
 {
-       if (spa_busy() || zfs_busy() || zvol_busy())
-               return (DDI_FAILURE);
+       zfsdev_state_t *zs;
+
+       ASSERT(MUTEX_HELD(&zfsdev_state_lock));
+       ASSERT(filp->private_data != NULL);
+
+       zs = filp->private_data;
+       zfs_onexit_destroy(zs->zs_onexit);
+       zfs_zevent_destroy(zs->zs_zevent);
+
+       list_remove(&zfsdev_state_list, zs);
+       kmem_free(zs, sizeof (zfsdev_state_t));
 
-       if (cmd != DDI_DETACH)
-               return (DDI_FAILURE);
+       return (0);
+}
 
-       zfs_dip = NULL;
+static int
+zfsdev_open(struct inode *ino, struct file *filp)
+{
+       int error;
 
-       ddi_prop_remove_all(dip);
-       ddi_remove_minor_node(dip, NULL);
+       mutex_enter(&zfsdev_state_lock);
+       error = zfsdev_state_init(filp);
+       mutex_exit(&zfsdev_state_lock);
 
-       return (DDI_SUCCESS);
+       return (-error);
 }
 
-/*ARGSUSED*/
 static int
-zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
+zfsdev_release(struct inode *ino, struct file *filp)
+{
+       int error;
+
+       mutex_enter(&zfsdev_state_lock);
+       error = zfsdev_state_destroy(filp);
+       mutex_exit(&zfsdev_state_lock);
+
+       return (-error);
+}
+
+static long
+zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
 {
-       switch (infocmd) {
-       case DDI_INFO_DEVT2DEVINFO:
-               *result = zfs_dip;
-               return (DDI_SUCCESS);
+       zfs_cmd_t *zc;
+       uint_t vecnum;
+       int error, rc, len, flag = 0;
+       const zfs_ioc_vec_t *vec;
+       char *saved_poolname;
+       nvlist_t *innvl = NULL;
+
+       vecnum = cmd - ZFS_IOC_FIRST;
+       if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
+               return (-SET_ERROR(EINVAL));
+       vec = &zfs_ioc_vec[vecnum];
+
+       /*
+        * The registered ioctl list may be sparse, verify that either
+        * a normal or legacy handler are registered.
+        */
+       if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
+               return (-SET_ERROR(EINVAL));
+
+       zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
+       saved_poolname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
+
+       error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
+       if (error != 0) {
+               error = SET_ERROR(EFAULT);
+               goto out;
+       }
+
+       zc->zc_iflags = flag & FKIOCTL;
+       if (zc->zc_nvlist_src_size != 0) {
+               error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
+                   zc->zc_iflags, &innvl);
+               if (error != 0)
+                       goto out;
+       }
+
+       /*
+        * Ensure that all pool/dataset names are valid before we pass down to
+        * the lower layers.
+        */
+       zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
+       switch (vec->zvec_namecheck) {
+       case POOL_NAME:
+               if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
+                       error = SET_ERROR(EINVAL);
+               else
+                       error = pool_status_check(zc->zc_name,
+                           vec->zvec_namecheck, vec->zvec_pool_check);
+               break;
+
+       case DATASET_NAME:
+               if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
+                       error = SET_ERROR(EINVAL);
+               else
+                       error = pool_status_check(zc->zc_name,
+                           vec->zvec_namecheck, vec->zvec_pool_check);
+               break;
+
+       case NO_NAME:
+               break;
+       }
+
+
+       if (error == 0 && !(flag & FKIOCTL))
+               error = vec->zvec_secpolicy(zc, innvl, CRED());
+
+       if (error != 0)
+               goto out;
+
+       /* legacy ioctls can modify zc_name */
+       (void) strlcpy(saved_poolname, zc->zc_name, sizeof (saved_poolname));
+       len = strcspn(saved_poolname, "/@") + 1;
+       saved_poolname[len] = '\0';
+
+       if (vec->zvec_func != NULL) {
+               nvlist_t *outnvl;
+               int puterror = 0;
+               spa_t *spa;
+               nvlist_t *lognv = NULL;
+
+               ASSERT(vec->zvec_legacy_func == NULL);
+
+               /*
+                * Add the innvl to the lognv before calling the func,
+                * in case the func changes the innvl.
+                */
+               if (vec->zvec_allow_log) {
+                       lognv = fnvlist_alloc();
+                       fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
+                           vec->zvec_name);
+                       if (!nvlist_empty(innvl)) {
+                               fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
+                                   innvl);
+                       }
+               }
+
+               VERIFY0(nvlist_alloc(&outnvl, NV_UNIQUE_NAME, KM_PUSHPAGE));
+               error = vec->zvec_func(zc->zc_name, innvl, outnvl);
 
-       case DDI_INFO_DEVT2INSTANCE:
-               *result = (void *)0;
-               return (DDI_SUCCESS);
+               if (error == 0 && 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);
+                       }
+                       (void) spa_history_log_nvl(spa, lognv);
+                       spa_close(spa, FTAG);
+               }
+               fnvlist_free(lognv);
+
+               if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
+                       int smusherror = 0;
+                       if (vec->zvec_smush_outnvlist) {
+                               smusherror = nvlist_smush(outnvl,
+                                   zc->zc_nvlist_dst_size);
+                       }
+                       if (smusherror == 0)
+                               puterror = put_nvlist(zc, outnvl);
+               }
+
+               if (puterror != 0)
+                       error = puterror;
+
+               nvlist_free(outnvl);
+       } else {
+               error = vec->zvec_legacy_func(zc);
+       }
+
+out:
+       nvlist_free(innvl);
+       rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
+       if (error == 0 && rc != 0)
+               error = SET_ERROR(EFAULT);
+       if (error == 0 && vec->zvec_allow_log) {
+               char *s = tsd_get(zfs_allow_log_key);
+               if (s != NULL)
+                       strfree(s);
+               (void) tsd_set(zfs_allow_log_key, strdup(saved_poolname));
        }
 
-       return (DDI_FAILURE);
+       kmem_free(saved_poolname, MAXNAMELEN);
+       kmem_free(zc, sizeof (zfs_cmd_t));
+       return (-error);
 }
 
-/*
- * OK, so this is a little weird.
- *
- * /dev/zfs is the control node, i.e. minor 0.
- * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
- *
- * /dev/zfs has basically nothing to do except serve up ioctls,
- * so most of the standard driver entry points are in zvol.c.
- */
-static struct cb_ops zfs_cb_ops = {
-       zvol_open,      /* open */
-       zvol_close,     /* close */
-       zvol_strategy,  /* strategy */
-       nodev,          /* print */
-       zvol_dump,      /* dump */
-       zvol_read,      /* read */
-       zvol_write,     /* write */
-       zfsdev_ioctl,   /* ioctl */
-       nodev,          /* devmap */
-       nodev,          /* mmap */
-       nodev,          /* segmap */
-       nochpoll,       /* poll */
-       ddi_prop_op,    /* prop_op */
-       NULL,           /* streamtab */
-       D_NEW | D_MP | D_64BIT,         /* Driver compatibility flag */
-       CB_REV,         /* version */
-       nodev,          /* async read */
-       nodev,          /* async write */
+#ifdef CONFIG_COMPAT
+static long
+zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
+{
+       return (zfsdev_ioctl(filp, cmd, arg));
+}
+#else
+#define        zfsdev_compat_ioctl     NULL
+#endif
+
+static const struct file_operations zfsdev_fops = {
+       .open           = zfsdev_open,
+       .release        = zfsdev_release,
+       .unlocked_ioctl = zfsdev_ioctl,
+       .compat_ioctl   = zfsdev_compat_ioctl,
+       .owner          = THIS_MODULE,
 };
 
-static struct dev_ops zfs_dev_ops = {
-       DEVO_REV,       /* version */
-       0,              /* refcnt */
-       zfs_info,       /* info */
-       nulldev,        /* identify */
-       nulldev,        /* probe */
-       zfs_attach,     /* attach */
-       zfs_detach,     /* detach */
-       nodev,          /* reset */
-       &zfs_cb_ops,    /* driver operations */
-       NULL,           /* no bus operations */
-       NULL,           /* power */
-       ddi_quiesce_not_needed, /* quiesce */
+static struct miscdevice zfs_misc = {
+       .minor          = MISC_DYNAMIC_MINOR,
+       .name           = ZFS_DRIVER,
+       .fops           = &zfsdev_fops,
 };
 
-static struct modldrv zfs_modldrv = {
-       &mod_driverops,
-       "ZFS storage pool",
-       &zfs_dev_ops
-};
+static int
+zfs_attach(void)
+{
+       int error;
 
-static struct modlinkage modlinkage = {
-       MODREV_1,
-       (void *)&zfs_modlfs,
-       (void *)&zfs_modldrv,
-       NULL
-};
+       mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
+       list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
+           offsetof(zfsdev_state_t, zs_next));
 
+       error = misc_register(&zfs_misc);
+       if (error != 0) {
+               printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
+               return (error);
+       }
 
-uint_t zfs_fsyncer_key;
-extern uint_t rrw_tsd_key;
+       return (0);
+}
+
+static void
+zfs_detach(void)
+{
+       int error;
+
+       error = misc_deregister(&zfs_misc);
+       if (error != 0)
+               printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
+
+       mutex_destroy(&zfsdev_state_lock);
+       list_destroy(&zfsdev_state_list);
+}
+
+static void
+zfs_allow_log_destroy(void *arg)
+{
+       char *poolname = arg;
+       strfree(poolname);
+}
+
+#ifdef DEBUG
+#define        ZFS_DEBUG_STR   " (DEBUG mode)"
+#else
+#define        ZFS_DEBUG_STR   ""
+#endif
 
 int
 _init(void)
@@ -3161,56 +5776,65 @@ _init(void)
 
        spa_init(FREAD | FWRITE);
        zfs_init();
-       zvol_init();
 
-       if ((error = mod_install(&modlinkage)) != 0) {
-               zvol_fini();
-               zfs_fini();
-               spa_fini();
-               return (error);
-       }
+       if ((error = zvol_init()) != 0)
+               goto out1;
+
+       zfs_ioctl_init();
+
+       if ((error = zfs_attach()) != 0)
+               goto out2;
 
        tsd_create(&zfs_fsyncer_key, NULL);
-       tsd_create(&rrw_tsd_key, NULL);
+       tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
+       tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
 
-       error = ldi_ident_from_mod(&modlinkage, &zfs_li);
-       ASSERT(error == 0);
-       mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
+       printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
+           "ZFS pool version %s, ZFS filesystem version %s\n",
+           ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
+           SPA_VERSION_STRING, ZPL_VERSION_STRING);
+#ifndef CONFIG_FS_POSIX_ACL
+       printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
+#endif /* CONFIG_FS_POSIX_ACL */
 
        return (0);
+
+out2:
+       (void) zvol_fini();
+out1:
+       zfs_fini();
+       spa_fini();
+       printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
+           ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
+           ZFS_DEBUG_STR, error);
+
+       return (error);
 }
 
 int
 _fini(void)
 {
-       int error;
-
-       if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
-               return (EBUSY);
-
-       if ((error = mod_remove(&modlinkage)) != 0)
-               return (error);
-
+       zfs_detach();
        zvol_fini();
        zfs_fini();
        spa_fini();
-       if (zfs_nfsshare_inited)
-               (void) ddi_modclose(nfs_mod);
-       if (zfs_smbshare_inited)
-               (void) ddi_modclose(smbsrv_mod);
-       if (zfs_nfsshare_inited || zfs_smbshare_inited)
-               (void) ddi_modclose(sharefs_mod);
 
        tsd_destroy(&zfs_fsyncer_key);
-       ldi_ident_release(zfs_li);
-       zfs_li = NULL;
-       mutex_destroy(&zfs_share_lock);
+       tsd_destroy(&rrw_tsd_key);
+       tsd_destroy(&zfs_allow_log_key);
 
-       return (error);
-}
+       printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
+           ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
 
-int
-_info(struct modinfo *modinfop)
-{
-       return (mod_info(&modlinkage, modinfop));
+       return (0);
 }
+
+#ifdef HAVE_SPL
+spl_module_init(_init);
+spl_module_exit(_fini);
+
+MODULE_DESCRIPTION("ZFS");
+MODULE_AUTHOR(ZFS_META_AUTHOR);
+MODULE_LICENSE(ZFS_META_LICENSE);
+MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
+#endif /* HAVE_SPL */