]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/dmu_objset.c
dbuf: Set dr_data when unoverriding after clone
[mirror_zfs.git] / module / zfs / dmu_objset.c
index c6b6eb745c0d27eda208bb1d0fe620e3043769c3..f098e1daa44bda5cc9e56b85854cbf784047a2e5 100644 (file)
@@ -6,7 +6,7 @@
  * You may not use this file except in compliance with the License.
  *
  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
- * or http://www.opensolaris.org/os/licensing.
+ * or https://opensource.org/licenses/CDDL-1.0.
  * See the License for the specific language governing permissions
  * and limitations under the License.
  *
  *
  * CDDL HEADER END
  */
+
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
- * Copyright (c) 2015 Nexenta Systems, Inc. All rights reserved.
  * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
+ * Copyright 2017 Nexenta Systems, Inc.
+ * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
+ * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
+ * Copyright (c) 2019, Klara Inc.
+ * Copyright (c) 2019, Allan Jude
+ * Copyright (c) 2022 Hewlett Packard Enterprise Development LP.
  */
 
 /* Portions Copyright 2010 Robert Milkowski */
 
-#include <sys/zfeature.h>
 #include <sys/cred.h>
 #include <sys/zfs_context.h>
 #include <sys/dmu_objset.h>
 #include <sys/zfs_onexit.h>
 #include <sys/dsl_destroy.h>
 #include <sys/vdev.h>
+#include <sys/zfeature.h>
 #include <sys/policy.h>
 #include <sys/spa_impl.h>
+#include <sys/dmu_recv.h>
+#include <sys/zfs_project.h>
+#include "zfs_namecheck.h"
+#include <sys/vdev_impl.h>
+#include <sys/arc.h>
 
 /*
  * Needed to close a window in dnode_move() that allows the objset to be freed
 krwlock_t os_lock;
 
 /*
- * Tunable to overwrite the maximum number of threads for the parallization
+ * Tunable to overwrite the maximum number of threads for the parallelization
  * of dmu_objset_find_dp, needed to speed up the import of pools with many
  * datasets.
  * Default is 4 times the number of leaf vdevs.
  */
-int dmu_find_threads = 0;
+static const int dmu_find_threads = 0;
 
 /*
  * Backfill lower metadnode objects after this many have been freed.
  * Backfilling negatively impacts object creation rates, so only do it
  * if there are enough holes to fill.
  */
-int dmu_rescan_dnode_threshold = 1 << DN_MAX_INDBLKSHIFT;
+static const int dmu_rescan_dnode_threshold = 1 << DN_MAX_INDBLKSHIFT;
+
+static const char *upgrade_tag = "upgrade_tag";
 
 static void dmu_objset_find_dp_cb(void *arg);
 
@@ -184,8 +197,10 @@ compression_changed_cb(void *arg, uint64_t newval)
         */
        ASSERT(newval != ZIO_COMPRESS_INHERIT);
 
-       os->os_compress = zio_compress_select(os->os_spa, newval,
-           ZIO_COMPRESS_ON);
+       os->os_compress = zio_compress_select(os->os_spa,
+           ZIO_COMPRESS_ALGO(newval), ZIO_COMPRESS_ON);
+       os->os_complevel = zio_complevel_select(os->os_spa, os->os_compress,
+           ZIO_COMPRESS_LEVEL(newval), ZIO_COMPLEVEL_DEFAULT);
 }
 
 static void
@@ -248,6 +263,19 @@ secondary_cache_changed_cb(void *arg, uint64_t newval)
        os->os_secondary_cache = newval;
 }
 
+static void
+prefetch_changed_cb(void *arg, uint64_t newval)
+{
+       objset_t *os = arg;
+
+       /*
+        * Inheritance should have been done by now.
+        */
+       ASSERT(newval == ZFS_PREFETCH_ALL || newval == ZFS_PREFETCH_NONE ||
+           newval == ZFS_PREFETCH_METADATA);
+       os->os_prefetch = newval;
+}
+
 static void
 sync_changed_cb(void *arg, uint64_t newval)
 {
@@ -273,7 +301,9 @@ redundant_metadata_changed_cb(void *arg, uint64_t newval)
         * Inheritance and range checking should have been done by now.
         */
        ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
-           newval == ZFS_REDUNDANT_METADATA_MOST);
+           newval == ZFS_REDUNDANT_METADATA_MOST ||
+           newval == ZFS_REDUNDANT_METADATA_SOME ||
+           newval == ZFS_REDUNDANT_METADATA_NONE);
 
        os->os_redundant_metadata = newval;
 }
@@ -306,6 +336,20 @@ dnodesize_changed_cb(void *arg, uint64_t newval)
        }
 }
 
+static void
+smallblk_changed_cb(void *arg, uint64_t newval)
+{
+       objset_t *os = arg;
+
+       /*
+        * Inheritance and range checking should have been done by now.
+        */
+       ASSERT(newval <= SPA_MAXBLOCKSIZE);
+       ASSERT(ISP2(newval));
+
+       os->os_zpl_special_smallblock = newval;
+}
+
 static void
 logbias_changed_cb(void *arg, uint64_t newval)
 {
@@ -331,17 +375,92 @@ dmu_objset_byteswap(void *buf, size_t size)
 {
        objset_phys_t *osp = buf;
 
-       ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
+       ASSERT(size == OBJSET_PHYS_SIZE_V1 || size == OBJSET_PHYS_SIZE_V2 ||
+           size == sizeof (objset_phys_t));
        dnode_byteswap(&osp->os_meta_dnode);
        byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
        osp->os_type = BSWAP_64(osp->os_type);
        osp->os_flags = BSWAP_64(osp->os_flags);
-       if (size == sizeof (objset_phys_t)) {
+       if (size >= OBJSET_PHYS_SIZE_V2) {
                dnode_byteswap(&osp->os_userused_dnode);
                dnode_byteswap(&osp->os_groupused_dnode);
+               if (size >= sizeof (objset_phys_t))
+                       dnode_byteswap(&osp->os_projectused_dnode);
+       }
+}
+
+/*
+ * The hash is a CRC-based hash of the objset_t pointer and the object number.
+ */
+static uint64_t
+dnode_hash(const objset_t *os, uint64_t obj)
+{
+       uintptr_t osv = (uintptr_t)os;
+       uint64_t crc = -1ULL;
+
+       ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
+       /*
+        * The low 6 bits of the pointer don't have much entropy, because
+        * the objset_t is larger than 2^6 bytes long.
+        */
+       crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
+       crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
+       crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
+       crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF];
+
+       crc ^= (osv>>14) ^ (obj>>24);
+
+       return (crc);
+}
+
+static unsigned int
+dnode_multilist_index_func(multilist_t *ml, void *obj)
+{
+       dnode_t *dn = obj;
+
+       /*
+        * The low order bits of the hash value are thought to be
+        * distributed evenly. Otherwise, in the case that the multilist
+        * has a power of two number of sublists, each sublists' usage
+        * would not be evenly distributed. In this context full 64bit
+        * division would be a waste of time, so limit it to 32 bits.
+        */
+       return ((unsigned int)dnode_hash(dn->dn_objset, dn->dn_object) %
+           multilist_get_num_sublists(ml));
+}
+
+static inline boolean_t
+dmu_os_is_l2cacheable(objset_t *os)
+{
+       if (os->os_secondary_cache == ZFS_CACHE_ALL ||
+           os->os_secondary_cache == ZFS_CACHE_METADATA) {
+               if (l2arc_exclude_special == 0)
+                       return (B_TRUE);
+
+               blkptr_t *bp = os->os_rootbp;
+               if (bp == NULL || BP_IS_HOLE(bp))
+                       return (B_FALSE);
+               uint64_t vdev = DVA_GET_VDEV(bp->blk_dva);
+               vdev_t *rvd = os->os_spa->spa_root_vdev;
+               vdev_t *vd = NULL;
+
+               if (vdev < rvd->vdev_children)
+                       vd = rvd->vdev_child[vdev];
+
+               if (vd == NULL)
+                       return (B_TRUE);
+
+               if (vd->vdev_alloc_bias != VDEV_BIAS_SPECIAL &&
+                   vd->vdev_alloc_bias != VDEV_BIAS_DEDUP)
+                       return (B_TRUE);
        }
+       return (B_FALSE);
 }
 
+/*
+ * Instantiates the objset_t in-memory structure corresponding to the
+ * objset_phys_t that's pointed to by the specified blkptr_t.
+ */
 int
 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
     objset_t **osp)
@@ -350,6 +469,23 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
        int i, err;
 
        ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
+       ASSERT(!BP_IS_REDACTED(bp));
+
+       /*
+        * We need the pool config lock to get properties.
+        */
+       ASSERT(ds == NULL || dsl_pool_config_held(ds->ds_dir->dd_pool));
+
+       /*
+        * The $ORIGIN dataset (if it exists) doesn't have an associated
+        * objset, so there's no reason to open it. The $ORIGIN dataset
+        * will not exist on pools older than SPA_VERSION_ORIGIN.
+        */
+       if (ds != NULL && spa_get_dsl(spa) != NULL &&
+           spa_get_dsl(spa)->dp_origin_snap != NULL) {
+               ASSERT3P(ds->ds_dir, !=,
+                   spa_get_dsl(spa)->dp_origin_snap->ds_dir);
+       }
 
        os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
        os->os_dsl_dataset = ds;
@@ -358,16 +494,24 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
        if (!BP_IS_HOLE(os->os_rootbp)) {
                arc_flags_t aflags = ARC_FLAG_WAIT;
                zbookmark_phys_t zb;
+               int size;
+               zio_flag_t zio_flags = ZIO_FLAG_CANFAIL;
                SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
                    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
 
-               if (DMU_OS_IS_L2CACHEABLE(os))
+               if (dmu_os_is_l2cacheable(os))
                        aflags |= ARC_FLAG_L2CACHE;
 
+               if (ds != NULL && ds->ds_dir->dd_crypto_obj != 0) {
+                       ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
+                       ASSERT(BP_IS_AUTHENTICATED(bp));
+                       zio_flags |= ZIO_FLAG_RAW;
+               }
+
                dprintf_bp(os->os_rootbp, "reading %s", "");
                err = arc_read(NULL, spa, os->os_rootbp,
                    arc_getbuf_func, &os->os_phys_buf,
-                   ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
+                   ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
                if (err != 0) {
                        kmem_free(os, sizeof (objset_t));
                        /* convert checksum errors into IO errors */
@@ -376,13 +520,20 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
                        return (err);
                }
 
+               if (spa_version(spa) < SPA_VERSION_USERSPACE)
+                       size = OBJSET_PHYS_SIZE_V1;
+               else if (!spa_feature_is_enabled(spa,
+                   SPA_FEATURE_PROJECT_QUOTA))
+                       size = OBJSET_PHYS_SIZE_V2;
+               else
+                       size = sizeof (objset_phys_t);
+
                /* Increase the blocksize if we are permitted. */
-               if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
-                   arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
+               if (arc_buf_size(os->os_phys_buf) < size) {
                        arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf,
-                           ARC_BUFC_METADATA, sizeof (objset_phys_t));
-                       bzero(buf->b_data, sizeof (objset_phys_t));
-                       bcopy(os->os_phys_buf->b_data, buf->b_data,
+                           ARC_BUFC_METADATA, size);
+                       memset(buf->b_data, 0, size);
+                       memcpy(buf->b_data, os->os_phys_buf->b_data,
                            arc_buf_size(os->os_phys_buf));
                        arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
                        os->os_phys_buf = buf;
@@ -392,12 +543,20 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
                os->os_flags = os->os_phys->os_flags;
        } else {
                int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
-                   sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
+                   sizeof (objset_phys_t) : OBJSET_PHYS_SIZE_V1;
                os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf,
                    ARC_BUFC_METADATA, size);
                os->os_phys = os->os_phys_buf->b_data;
-               bzero(os->os_phys, size);
+               memset(os->os_phys, 0, size);
        }
+       /*
+        * These properties will be filled in by the logic in zfs_get_zplprop()
+        * when they are queried for the first time.
+        */
+       os->os_version = OBJSET_PROP_UNINITIALIZED;
+       os->os_normalization = OBJSET_PROP_UNINITIALIZED;
+       os->os_utf8only = OBJSET_PROP_UNINITIALIZED;
+       os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED;
 
        /*
         * Note: the changed_cb will be called once before the register
@@ -406,17 +565,8 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
         * checksum/compression/copies.
         */
        if (ds != NULL) {
-               boolean_t needlock = B_FALSE;
+               os->os_encrypted = (ds->ds_dir->dd_crypto_obj != 0);
 
-               /*
-                * Note: it's valid to open the objset if the dataset is
-                * long-held, in which case the pool_config lock will not
-                * be held.
-                */
-               if (!dsl_pool_config_held(dmu_objset_pool(os))) {
-                       needlock = B_TRUE;
-                       dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
-               }
                err = dsl_prop_register(ds,
                    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
                    primary_cache_changed_cb, os);
@@ -425,6 +575,11 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
                            zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
                            secondary_cache_changed_cb, os);
                }
+               if (err == 0) {
+                       err = dsl_prop_register(ds,
+                           zfs_prop_to_name(ZFS_PROP_PREFETCH),
+                           prefetch_changed_cb, os);
+               }
                if (!ds->ds_is_snapshot) {
                        if (err == 0) {
                                err = dsl_prop_register(ds,
@@ -472,9 +627,13 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
                                    zfs_prop_to_name(ZFS_PROP_DNODESIZE),
                                    dnodesize_changed_cb, os);
                        }
+                       if (err == 0) {
+                               err = dsl_prop_register(ds,
+                                   zfs_prop_to_name(
+                                   ZFS_PROP_SPECIAL_SMALL_BLOCKS),
+                                   smallblk_changed_cb, os);
+                       }
                }
-               if (needlock)
-                       dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
                if (err != 0) {
                        arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
                        kmem_free(os, sizeof (objset_t));
@@ -484,6 +643,8 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
                /* It's the meta-objset. */
                os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
                os->os_compress = ZIO_COMPRESS_ON;
+               os->os_complevel = ZIO_COMPLEVEL_DEFAULT;
+               os->os_encrypted = B_FALSE;
                os->os_copies = spa_max_replication(spa);
                os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
                os->os_dedup_verify = B_FALSE;
@@ -492,6 +653,7 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
                os->os_primary_cache = ZFS_CACHE_ALL;
                os->os_secondary_cache = ZFS_CACHE_ALL;
                os->os_dnodesize = DNODE_MIN_SIZE;
+               os->os_prefetch = ZFS_PREFETCH_ALL;
        }
 
        if (ds == NULL || !ds->ds_is_snapshot)
@@ -499,10 +661,9 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
        os->os_zil = zil_alloc(os, &os->os_zil_header);
 
        for (i = 0; i < TXG_SIZE; i++) {
-               list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
-                   offsetof(dnode_t, dn_dirty_link[i]));
-               list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
-                   offsetof(dnode_t, dn_dirty_link[i]));
+               multilist_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
+                   offsetof(dnode_t, dn_dirty_link[i]),
+                   dnode_multilist_index_func);
        }
        list_create(&os->os_dnodes, sizeof (dnode_t),
            offsetof(dnode_t, dn_link));
@@ -512,16 +673,24 @@ dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
        list_link_init(&os->os_evicting_node);
 
        mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
+       mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
        mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
        mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
+       os->os_obj_next_percpu_len = boot_ncpus;
+       os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len *
+           sizeof (os->os_obj_next_percpu[0]), KM_SLEEP);
 
        dnode_special_open(os, &os->os_phys->os_meta_dnode,
            DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
-       if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
+       if (OBJSET_BUF_HAS_USERUSED(os->os_phys_buf)) {
                dnode_special_open(os, &os->os_phys->os_userused_dnode,
                    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
                dnode_special_open(os, &os->os_phys->os_groupused_dnode,
                    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
+               if (OBJSET_BUF_HAS_PROJECTUSED(os->os_phys_buf))
+                       dnode_special_open(os,
+                           &os->os_phys->os_projectused_dnode,
+                           DMU_PROJECTUSED_OBJECT, &os->os_projectused_dnode);
        }
 
        mutex_init(&os->os_upgrade_lock, NULL, MUTEX_DEFAULT, NULL);
@@ -536,17 +705,19 @@ dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
        int err = 0;
 
        /*
-        * We shouldn't be doing anything with dsl_dataset_t's unless the
-        * pool_config lock is held, or the dataset is long-held.
+        * We need the pool_config lock to manipulate the dsl_dataset_t.
+        * Even if the dataset is long-held, we need the pool_config lock
+        * to open the objset, as it needs to get properties.
         */
-       ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
-           dsl_dataset_long_held(ds));
+       ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
 
        mutex_enter(&ds->ds_opening_lock);
        if (ds->ds_objset == NULL) {
                objset_t *os;
+               rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
                err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
                    ds, dsl_dataset_get_blkptr(ds), &os);
+               rrw_exit(&ds->ds_bp_rwlock, FTAG);
 
                if (err == 0) {
                        mutex_enter(&ds->ds_lock);
@@ -565,16 +736,19 @@ dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
  * can be held at a time.
  */
 int
-dmu_objset_hold(const char *name, void *tag, objset_t **osp)
+dmu_objset_hold_flags(const char *name, boolean_t decrypt, const void *tag,
+    objset_t **osp)
 {
        dsl_pool_t *dp;
        dsl_dataset_t *ds;
        int err;
+       ds_hold_flags_t flags;
 
+       flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
        err = dsl_pool_hold(name, tag, &dp);
        if (err != 0)
                return (err);
-       err = dsl_dataset_hold(dp, name, tag, &ds);
+       err = dsl_dataset_hold_flags(dp, name, flags, tag, &ds);
        if (err != 0) {
                dsl_pool_rele(dp, tag);
                return (err);
@@ -589,23 +763,45 @@ dmu_objset_hold(const char *name, void *tag, objset_t **osp)
        return (err);
 }
 
+int
+dmu_objset_hold(const char *name, const void *tag, objset_t **osp)
+{
+       return (dmu_objset_hold_flags(name, B_FALSE, tag, osp));
+}
+
 static int
 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
-    boolean_t readonly, void *tag, objset_t **osp)
+    boolean_t readonly, boolean_t decrypt, const void *tag, objset_t **osp)
 {
-       int err;
+       (void) tag;
 
-       err = dmu_objset_from_ds(ds, osp);
+       int err = dmu_objset_from_ds(ds, osp);
        if (err != 0) {
-               dsl_dataset_disown(ds, tag);
+               return (err);
        } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
-               dsl_dataset_disown(ds, tag);
                return (SET_ERROR(EINVAL));
        } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
-               dsl_dataset_disown(ds, tag);
+               return (SET_ERROR(EROFS));
+       } else if (!readonly && decrypt &&
+           dsl_dir_incompatible_encryption_version(ds->ds_dir)) {
                return (SET_ERROR(EROFS));
        }
-       return (err);
+
+       /* if we are decrypting, we can now check MACs in os->os_phys_buf */
+       if (decrypt && arc_is_unauthenticated((*osp)->os_phys_buf)) {
+               zbookmark_phys_t zb;
+
+               SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
+                   ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
+               err = arc_untransform((*osp)->os_phys_buf, (*osp)->os_spa,
+                   &zb, B_FALSE);
+               if (err != 0)
+                       return (err);
+
+               ASSERT0(arc_is_unauthenticated((*osp)->os_phys_buf));
+       }
+
+       return (0);
 }
 
 /*
@@ -615,51 +811,88 @@ dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
  */
 int
 dmu_objset_own(const char *name, dmu_objset_type_t type,
-    boolean_t readonly, void *tag, objset_t **osp)
+    boolean_t readonly, boolean_t decrypt, const void *tag, objset_t **osp)
 {
        dsl_pool_t *dp;
        dsl_dataset_t *ds;
        int err;
+       ds_hold_flags_t flags;
 
+       flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
        err = dsl_pool_hold(name, FTAG, &dp);
        if (err != 0)
                return (err);
-       err = dsl_dataset_own(dp, name, tag, &ds);
+       err = dsl_dataset_own(dp, name, flags, tag, &ds);
        if (err != 0) {
                dsl_pool_rele(dp, FTAG);
                return (err);
        }
-       err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
-       dsl_pool_rele(dp, FTAG);
+       err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
+       if (err != 0) {
+               dsl_dataset_disown(ds, flags, tag);
+               dsl_pool_rele(dp, FTAG);
+               return (err);
+       }
 
-       if (err == 0 && dmu_objset_userobjspace_upgradable(*osp))
-               dmu_objset_userobjspace_upgrade(*osp);
+       /*
+        * User accounting requires the dataset to be decrypted and rw.
+        * We also don't begin user accounting during claiming to help
+        * speed up pool import times and to keep this txg reserved
+        * completely for recovery work.
+        */
+       if (!readonly && !dp->dp_spa->spa_claiming &&
+           (ds->ds_dir->dd_crypto_obj == 0 || decrypt)) {
+               if (dmu_objset_userobjspace_upgradable(*osp) ||
+                   dmu_objset_projectquota_upgradable(*osp)) {
+                       dmu_objset_id_quota_upgrade(*osp);
+               } else if (dmu_objset_userused_enabled(*osp)) {
+                       dmu_objset_userspace_upgrade(*osp);
+               }
+       }
 
-       return (err);
+       dsl_pool_rele(dp, FTAG);
+       return (0);
 }
 
 int
 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
-    boolean_t readonly, void *tag, objset_t **osp)
+    boolean_t readonly, boolean_t decrypt, const void *tag, objset_t **osp)
 {
        dsl_dataset_t *ds;
        int err;
+       ds_hold_flags_t flags;
 
-       err = dsl_dataset_own_obj(dp, obj, tag, &ds);
+       flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
+       err = dsl_dataset_own_obj(dp, obj, flags, tag, &ds);
        if (err != 0)
                return (err);
 
-       return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
+       err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
+       if (err != 0) {
+               dsl_dataset_disown(ds, flags, tag);
+               return (err);
+       }
+
+       return (0);
 }
 
 void
-dmu_objset_rele(objset_t *os, void *tag)
+dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, const void *tag)
 {
+       ds_hold_flags_t flags;
        dsl_pool_t *dp = dmu_objset_pool(os);
-       dsl_dataset_rele(os->os_dsl_dataset, tag);
+
+       flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
+       dsl_dataset_rele_flags(os->os_dsl_dataset, flags, tag);
        dsl_pool_rele(dp, tag);
 }
 
+void
+dmu_objset_rele(objset_t *os, const void *tag)
+{
+       dmu_objset_rele_flags(os, B_FALSE, tag);
+}
+
 /*
  * When we are called, os MUST refer to an objset associated with a dataset
  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
@@ -672,34 +905,37 @@ dmu_objset_rele(objset_t *os, void *tag)
  * same name so that it can be partially torn down and reconstructed.
  */
 void
-dmu_objset_refresh_ownership(objset_t *os, void *tag)
+dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds,
+    boolean_t decrypt, const void *tag)
 {
        dsl_pool_t *dp;
-       dsl_dataset_t *ds, *newds;
        char name[ZFS_MAX_DATASET_NAME_LEN];
+       ds_hold_flags_t flags;
 
-       ds = os->os_dsl_dataset;
+       flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
        VERIFY3P(ds, !=, NULL);
        VERIFY3P(ds->ds_owner, ==, tag);
        VERIFY(dsl_dataset_long_held(ds));
 
        dsl_dataset_name(ds, name);
-       dp = dmu_objset_pool(os);
+       dp = ds->ds_dir->dd_pool;
        dsl_pool_config_enter(dp, FTAG);
-       dmu_objset_disown(os, tag);
-       VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
-       VERIFY3P(newds, ==, os->os_dsl_dataset);
+       dsl_dataset_disown(ds, flags, tag);
+       VERIFY0(dsl_dataset_own(dp, name, flags, tag, newds));
        dsl_pool_config_exit(dp, FTAG);
 }
 
 void
-dmu_objset_disown(objset_t *os, void *tag)
+dmu_objset_disown(objset_t *os, boolean_t decrypt, const void *tag)
 {
+       ds_hold_flags_t flags;
+
+       flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
        /*
         * Stop upgrading thread
         */
        dmu_objset_upgrade_stop(os);
-       dsl_dataset_disown(os->os_dsl_dataset, tag);
+       dsl_dataset_disown(os->os_dsl_dataset, flags, tag);
 }
 
 void
@@ -737,6 +973,8 @@ dmu_objset_evict_dbufs(objset_t *os)
        kmem_free(dn_marker, sizeof (dnode_t));
 
        if (DMU_USERUSED_DNODE(os) != NULL) {
+               if (DMU_PROJECTUSED_DNODE(os) != NULL)
+                       dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os));
                dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
                dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
        }
@@ -759,11 +997,9 @@ dmu_objset_evict_dbufs(objset_t *os)
 void
 dmu_objset_evict(objset_t *os)
 {
-       int t;
-
        dsl_dataset_t *ds = os->os_dsl_dataset;
 
-       for (t = 0; t < TXG_SIZE; t++)
+       for (int t = 0; t < TXG_SIZE; t++)
                ASSERT(!dmu_objset_is_dirty(os, t));
 
        if (ds)
@@ -782,6 +1018,8 @@ dmu_objset_evict(objset_t *os)
        } else {
                mutex_exit(&os->os_lock);
        }
+
+
 }
 
 void
@@ -791,6 +1029,8 @@ dmu_objset_evict_done(objset_t *os)
 
        dnode_special_close(&os->os_meta_dnode);
        if (DMU_USERUSED_DNODE(os)) {
+               if (DMU_PROJECTUSED_DNODE(os))
+                       dnode_special_close(&os->os_projectused_dnode);
                dnode_special_close(&os->os_userused_dnode);
                dnode_special_close(&os->os_groupused_dnode);
        }
@@ -807,29 +1047,40 @@ dmu_objset_evict_done(objset_t *os)
        rw_enter(&os_lock, RW_READER);
        rw_exit(&os_lock);
 
+       kmem_free(os->os_obj_next_percpu,
+           os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0]));
+
        mutex_destroy(&os->os_lock);
+       mutex_destroy(&os->os_userused_lock);
        mutex_destroy(&os->os_obj_lock);
        mutex_destroy(&os->os_user_ptr_lock);
+       mutex_destroy(&os->os_upgrade_lock);
+       for (int i = 0; i < TXG_SIZE; i++)
+               multilist_destroy(&os->os_dirty_dnodes[i]);
        spa_evicting_os_deregister(os->os_spa, os);
        kmem_free(os, sizeof (objset_t));
 }
 
-timestruc_t
+inode_timespec_t
 dmu_objset_snap_cmtime(objset_t *os)
 {
        return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
 }
 
-/* called from dsl for meta-objset */
 objset_t *
-dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
-    dmu_objset_type_t type, dmu_tx_t *tx)
+dmu_objset_create_impl_dnstats(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
+    dmu_objset_type_t type, int levels, int blksz, int ibs, dmu_tx_t *tx)
 {
        objset_t *os;
        dnode_t *mdn;
 
        ASSERT(dmu_tx_is_syncing(tx));
 
+       if (blksz == 0)
+               blksz = DNODE_BLOCK_SIZE;
+       if (ibs == 0)
+               ibs = DN_MAX_INDBLKSHIFT;
+
        if (ds != NULL)
                VERIFY0(dmu_objset_from_ds(ds, &os));
        else
@@ -837,12 +1088,12 @@ dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
 
        mdn = DMU_META_DNODE(os);
 
-       dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT,
-           DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx);
+       dnode_allocate(mdn, DMU_OT_DNODE, blksz, ibs, DMU_OT_NONE, 0,
+           DNODE_MIN_SLOTS, tx);
 
        /*
         * We don't want to have to increase the meta-dnode's nlevels
-        * later, because then we could do it in quescing context while
+        * later, because then we could do it in quiescing context while
         * we are also accessing it in open context.
         *
         * This precaution is not necessary for the MOS (ds == NULL),
@@ -852,16 +1103,25 @@ dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
         * to convergence, so minimizing its dn_nlevels matters.
         */
        if (ds != NULL) {
-               int levels = 1;
-
-               /*
-                * Determine the number of levels necessary for the meta-dnode
-                * to contain DN_MAX_OBJECT dnodes.
-                */
-               while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
-                   (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
-                   DN_MAX_OBJECT * sizeof (dnode_phys_t))
-                       levels++;
+               if (levels == 0) {
+                       levels = 1;
+
+                       /*
+                        * Determine the number of levels necessary for the
+                        * meta-dnode to contain DN_MAX_OBJECT dnodes.  Note
+                        * that in order to ensure that we do not overflow
+                        * 64 bits, there has to be a nlevels that gives us a
+                        * number of blocks > DN_MAX_OBJECT but < 2^64.
+                        * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
+                        * (10) must be less than (64 - log2(DN_MAX_OBJECT))
+                        * (16).
+                        */
+                       while ((uint64_t)mdn->dn_nblkptr <<
+                           (mdn->dn_datablkshift - DNODE_SHIFT + (levels - 1) *
+                           (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
+                           DN_MAX_OBJECT)
+                               levels++;
+               }
 
                mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
                    mdn->dn_nlevels = levels;
@@ -871,14 +1131,28 @@ dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
        ASSERT(type != DMU_OST_ANY);
        ASSERT(type < DMU_OST_NUMTYPES);
        os->os_phys->os_type = type;
-       if (dmu_objset_userused_enabled(os)) {
+
+       /*
+        * Enable user accounting if it is enabled and this is not an
+        * encrypted receive.
+        */
+       if (dmu_objset_userused_enabled(os) &&
+           (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
                os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
                if (dmu_objset_userobjused_enabled(os)) {
-                       ds->ds_feature_activation_needed[
-                           SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
+                       ASSERT3P(ds, !=, NULL);
+                       ds->ds_feature_activation[
+                           SPA_FEATURE_USEROBJ_ACCOUNTING] = (void *)B_TRUE;
                        os->os_phys->os_flags |=
                            OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
                }
+               if (dmu_objset_projectquota_enabled(os)) {
+                       ASSERT3P(ds, !=, NULL);
+                       ds->ds_feature_activation[
+                           SPA_FEATURE_PROJECT_QUOTA] = (void *)B_TRUE;
+                       os->os_phys->os_flags |=
+                           OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
+               }
                os->os_flags = os->os_phys->os_flags;
        }
 
@@ -887,23 +1161,34 @@ dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
        return (os);
 }
 
+/* called from dsl for meta-objset */
+objset_t *
+dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
+    dmu_objset_type_t type, dmu_tx_t *tx)
+{
+       return (dmu_objset_create_impl_dnstats(spa, ds, bp, type, 0, 0, 0, tx));
+}
+
 typedef struct dmu_objset_create_arg {
        const char *doca_name;
        cred_t *doca_cred;
+       proc_t *doca_proc;
        void (*doca_userfunc)(objset_t *os, void *arg,
            cred_t *cr, dmu_tx_t *tx);
        void *doca_userarg;
        dmu_objset_type_t doca_type;
        uint64_t doca_flags;
+       dsl_crypto_params_t *doca_dcp;
 } dmu_objset_create_arg_t;
 
-/*ARGSUSED*/
 static int
 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
 {
        dmu_objset_create_arg_t *doca = arg;
        dsl_pool_t *dp = dmu_tx_pool(tx);
        dsl_dir_t *pdd;
+       dsl_dataset_t *parentds;
+       objset_t *parentos;
        const char *tail;
        int error;
 
@@ -913,6 +1198,9 @@ dmu_objset_create_check(void *arg, dmu_tx_t *tx)
        if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
                return (SET_ERROR(ENAMETOOLONG));
 
+       if (dataset_nestcheck(doca->doca_name) != 0)
+               return (SET_ERROR(ENAMETOOLONG));
+
        error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
        if (error != 0)
                return (error);
@@ -920,8 +1208,39 @@ dmu_objset_create_check(void *arg, dmu_tx_t *tx)
                dsl_dir_rele(pdd, FTAG);
                return (SET_ERROR(EEXIST));
        }
+
+       error = dmu_objset_create_crypt_check(pdd, doca->doca_dcp, NULL);
+       if (error != 0) {
+               dsl_dir_rele(pdd, FTAG);
+               return (error);
+       }
+
        error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
-           doca->doca_cred);
+           doca->doca_cred, doca->doca_proc);
+       if (error != 0) {
+               dsl_dir_rele(pdd, FTAG);
+               return (error);
+       }
+
+       /* can't create below anything but filesystems (eg. no ZVOLs) */
+       error = dsl_dataset_hold_obj(pdd->dd_pool,
+           dsl_dir_phys(pdd)->dd_head_dataset_obj, FTAG, &parentds);
+       if (error != 0) {
+               dsl_dir_rele(pdd, FTAG);
+               return (error);
+       }
+       error = dmu_objset_from_ds(parentds, &parentos);
+       if (error != 0) {
+               dsl_dataset_rele(parentds, FTAG);
+               dsl_dir_rele(pdd, FTAG);
+               return (error);
+       }
+       if (dmu_objset_type(parentos) != DMU_OST_ZFS) {
+               dsl_dataset_rele(parentds, FTAG);
+               dsl_dir_rele(pdd, FTAG);
+               return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
+       }
+       dsl_dataset_rele(parentds, FTAG);
        dsl_dir_rele(pdd, FTAG);
 
        return (error);
@@ -932,60 +1251,132 @@ dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
 {
        dmu_objset_create_arg_t *doca = arg;
        dsl_pool_t *dp = dmu_tx_pool(tx);
+       spa_t *spa = dp->dp_spa;
        dsl_dir_t *pdd;
        const char *tail;
        dsl_dataset_t *ds;
        uint64_t obj;
        blkptr_t *bp;
        objset_t *os;
+       zio_t *rzio;
 
        VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
 
        obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
-           doca->doca_cred, tx);
+           doca->doca_cred, doca->doca_dcp, tx);
 
-       VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
+       VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj,
+           DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
+       rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
        bp = dsl_dataset_get_blkptr(ds);
-       os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
-           ds, bp, doca->doca_type, tx);
+       os = dmu_objset_create_impl(spa, ds, bp, doca->doca_type, tx);
+       rrw_exit(&ds->ds_bp_rwlock, FTAG);
 
        if (doca->doca_userfunc != NULL) {
                doca->doca_userfunc(os, doca->doca_userarg,
                    doca->doca_cred, tx);
        }
 
-       spa_history_log_internal_ds(ds, "create", tx, "");
-       zvol_create_minors(dp->dp_spa, doca->doca_name, B_TRUE);
+       /*
+        * The doca_userfunc() may write out some data that needs to be
+        * encrypted if the dataset is encrypted (specifically the root
+        * directory).  This data must be written out before the encryption
+        * key mapping is removed by dsl_dataset_rele_flags().  Force the
+        * I/O to occur immediately by invoking the relevant sections of
+        * dsl_pool_sync().
+        */
+       if (os->os_encrypted) {
+               dsl_dataset_t *tmpds = NULL;
+               boolean_t need_sync_done = B_FALSE;
+
+               mutex_enter(&ds->ds_lock);
+               ds->ds_owner = FTAG;
+               mutex_exit(&ds->ds_lock);
+
+               rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
+               tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
+                   tx->tx_txg);
+               if (tmpds != NULL) {
+                       dsl_dataset_sync(ds, rzio, tx);
+                       need_sync_done = B_TRUE;
+               }
+               VERIFY0(zio_wait(rzio));
 
-       dsl_dataset_rele(ds, FTAG);
+               dmu_objset_sync_done(os, tx);
+               taskq_wait(dp->dp_sync_taskq);
+               if (txg_list_member(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
+                       ASSERT3P(ds->ds_key_mapping, !=, NULL);
+                       key_mapping_rele(spa, ds->ds_key_mapping, ds);
+               }
+
+               rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
+               tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
+                   tx->tx_txg);
+               if (tmpds != NULL) {
+                       dmu_buf_rele(ds->ds_dbuf, ds);
+                       dsl_dataset_sync(ds, rzio, tx);
+               }
+               VERIFY0(zio_wait(rzio));
+
+               if (need_sync_done) {
+                       ASSERT3P(ds->ds_key_mapping, !=, NULL);
+                       key_mapping_rele(spa, ds->ds_key_mapping, ds);
+                       dsl_dataset_sync_done(ds, tx);
+                       dmu_buf_rele(ds->ds_dbuf, ds);
+               }
+
+               mutex_enter(&ds->ds_lock);
+               ds->ds_owner = NULL;
+               mutex_exit(&ds->ds_lock);
+       }
+
+       spa_history_log_internal_ds(ds, "create", tx, " ");
+
+       dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
        dsl_dir_rele(pdd, FTAG);
 }
 
 int
 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
-    void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
+    dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg)
 {
        dmu_objset_create_arg_t doca;
+       dsl_crypto_params_t tmp_dcp = { 0 };
 
        doca.doca_name = name;
        doca.doca_cred = CRED();
+       doca.doca_proc = curproc;
        doca.doca_flags = flags;
        doca.doca_userfunc = func;
        doca.doca_userarg = arg;
        doca.doca_type = type;
 
-       return (dsl_sync_task(name,
+       /*
+        * Some callers (mostly for testing) do not provide a dcp on their
+        * own but various code inside the sync task will require it to be
+        * allocated. Rather than adding NULL checks throughout this code
+        * or adding dummy dcp's to all of the callers we simply create a
+        * dummy one here and use that. This zero dcp will have the same
+        * effect as asking for inheritance of all encryption params.
+        */
+       doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp;
+
+       int rv = dsl_sync_task(name,
            dmu_objset_create_check, dmu_objset_create_sync, &doca,
-           5, ZFS_SPACE_CHECK_NORMAL));
+           6, ZFS_SPACE_CHECK_NORMAL);
+
+       if (rv == 0)
+               zvol_create_minor(name);
+       return (rv);
 }
 
 typedef struct dmu_objset_clone_arg {
        const char *doca_clone;
        const char *doca_origin;
        cred_t *doca_cred;
+       proc_t *doca_proc;
 } dmu_objset_clone_arg_t;
 
-/*ARGSUSED*/
 static int
 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
 {
@@ -1011,23 +1402,27 @@ dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
        }
 
        error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
-           doca->doca_cred);
+           doca->doca_cred, doca->doca_proc);
        if (error != 0) {
                dsl_dir_rele(pdd, FTAG);
                return (SET_ERROR(EDQUOT));
        }
-       dsl_dir_rele(pdd, FTAG);
 
        error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
-       if (error != 0)
+       if (error != 0) {
+               dsl_dir_rele(pdd, FTAG);
                return (error);
+       }
 
        /* You can only clone snapshots, not the head datasets. */
        if (!origin->ds_is_snapshot) {
                dsl_dataset_rele(origin, FTAG);
+               dsl_dir_rele(pdd, FTAG);
                return (SET_ERROR(EINVAL));
        }
+
        dsl_dataset_rele(origin, FTAG);
+       dsl_dir_rele(pdd, FTAG);
 
        return (0);
 }
@@ -1047,13 +1442,12 @@ dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
        VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
 
        obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
-           doca->doca_cred, tx);
+           doca->doca_cred, NULL, tx);
 
        VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
        dsl_dataset_name(origin, namebuf);
        spa_history_log_internal_ds(ds, "clone", tx,
-           "origin=%s (%llu)", namebuf, origin->ds_object);
-       zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE);
+           "origin=%s (%llu)", namebuf, (u_longlong_t)origin->ds_object);
        dsl_dataset_rele(ds, FTAG);
        dsl_dataset_rele(origin, FTAG);
        dsl_dir_rele(pdd, FTAG);
@@ -1067,10 +1461,16 @@ dmu_objset_clone(const char *clone, const char *origin)
        doca.doca_clone = clone;
        doca.doca_origin = origin;
        doca.doca_cred = CRED();
+       doca.doca_proc = curproc;
 
-       return (dsl_sync_task(clone,
+       int rv = dsl_sync_task(clone,
            dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
-           5, ZFS_SPACE_CHECK_NORMAL));
+           6, ZFS_SPACE_CHECK_NORMAL);
+
+       if (rv == 0)
+               zvol_create_minor(clone);
+
+       return (rv);
 }
 
 int
@@ -1081,7 +1481,7 @@ dmu_objset_snapshot_one(const char *fsname, const char *snapname)
        nvlist_t *snaps = fnvlist_alloc();
 
        fnvlist_add_boolean(snaps, longsnap);
-       strfree(longsnap);
+       kmem_strfree(longsnap);
        err = dsl_dataset_snapshot(snaps, NULL, NULL);
        fnvlist_free(snaps);
        return (err);
@@ -1095,14 +1495,20 @@ dmu_objset_upgrade_task_cb(void *data)
        mutex_enter(&os->os_upgrade_lock);
        os->os_upgrade_status = EINTR;
        if (!os->os_upgrade_exit) {
+               int status;
+
                mutex_exit(&os->os_upgrade_lock);
 
-               os->os_upgrade_status = os->os_upgrade_cb(os);
+               status = os->os_upgrade_cb(os);
+
                mutex_enter(&os->os_upgrade_lock);
+
+               os->os_upgrade_status = status;
        }
        os->os_upgrade_exit = B_TRUE;
        os->os_upgrade_id = 0;
        mutex_exit(&os->os_upgrade_lock);
+       dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
 }
 
 static void
@@ -1111,6 +1517,9 @@ dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
        if (os->os_upgrade_id != 0)
                return;
 
+       ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
+       dsl_dataset_long_hold(dmu_objset_ds(os), upgrade_tag);
+
        mutex_enter(&os->os_upgrade_lock);
        if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) {
                os->os_upgrade_exit = B_FALSE;
@@ -1118,8 +1527,12 @@ dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
                os->os_upgrade_id = taskq_dispatch(
                    os->os_spa->spa_upgrade_taskq,
                    dmu_objset_upgrade_task_cb, os, TQ_SLEEP);
-               if (os->os_upgrade_id == 0)
+               if (os->os_upgrade_id == TASKQID_INVALID) {
+                       dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
                        os->os_upgrade_status = ENOMEM;
+               }
+       } else {
+               dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
        }
        mutex_exit(&os->os_upgrade_lock);
 }
@@ -1135,51 +1548,56 @@ dmu_objset_upgrade_stop(objset_t *os)
                os->os_upgrade_id = 0;
                mutex_exit(&os->os_upgrade_lock);
 
-               taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id);
+               if ((taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id)) == 0) {
+                       dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
+               }
+               txg_wait_synced(os->os_spa->spa_dsl_pool, 0);
        } else {
                mutex_exit(&os->os_upgrade_lock);
        }
 }
 
 static void
-dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
+dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
 {
        dnode_t *dn;
 
-       while ((dn = list_head(list))) {
+       while ((dn = multilist_sublist_head(list)) != NULL) {
                ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
                ASSERT(dn->dn_dbuf->db_data_pending);
                /*
                 * Initialize dn_zio outside dnode_sync() because the
-                * meta-dnode needs to set it ouside dnode_sync().
+                * meta-dnode needs to set it outside dnode_sync().
                 */
                dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
                ASSERT(dn->dn_zio);
 
                ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
-               list_remove(list, dn);
+               multilist_sublist_remove(list, dn);
 
-               if (newlist) {
-                       (void) dnode_add_ref(dn, newlist);
-                       list_insert_tail(newlist, dn);
-               }
+               /*
+                * See the comment above dnode_rele_task() for an explanation
+                * of why this dnode hold is always needed (even when not
+                * doing user accounting).
+                */
+               multilist_t *newlist = &dn->dn_objset->os_synced_dnodes;
+               (void) dnode_add_ref(dn, newlist);
+               multilist_insert(newlist, dn);
 
                dnode_sync(dn, tx);
        }
 }
 
-/* ARGSUSED */
 static void
 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
 {
-       int i;
-
+       (void) abuf;
        blkptr_t *bp = zio->io_bp;
        objset_t *os = arg;
        dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
+       uint64_t fill = 0;
 
        ASSERT(!BP_IS_EMBEDDED(bp));
-       ASSERT3P(bp, ==, os->os_rootbp);
        ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
        ASSERT0(BP_GET_LEVEL(bp));
 
@@ -1187,17 +1605,24 @@ dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
         * Update rootbp fill count: it should be the number of objects
         * allocated in the object set (not counting the "special"
         * objects that are stored in the objset_phys_t -- the meta
-        * dnode and user/group accounting objects).
+        * dnode and user/group/project accounting objects).
         */
-       bp->blk_fill = 0;
-       for (i = 0; i < dnp->dn_nblkptr; i++)
-               bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
+       for (int i = 0; i < dnp->dn_nblkptr; i++)
+               fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
+
+       BP_SET_FILL(bp, fill);
+
+       if (os->os_dsl_dataset != NULL)
+               rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
+       *os->os_rootbp = *bp;
+       if (os->os_dsl_dataset != NULL)
+               rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
 }
 
-/* ARGSUSED */
 static void
 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
 {
+       (void) abuf;
        blkptr_t *bp = zio->io_bp;
        blkptr_t *bp_orig = &zio->io_bp_orig;
        objset_t *os = arg;
@@ -1211,6 +1636,92 @@ dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
                (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
                dsl_dataset_block_born(ds, bp, tx);
        }
+       kmem_free(bp, sizeof (*bp));
+}
+
+typedef struct sync_objset_arg {
+       zio_t           *soa_zio;
+       objset_t        *soa_os;
+       dmu_tx_t        *soa_tx;
+       kmutex_t        soa_mutex;
+       int             soa_count;
+       taskq_ent_t     soa_tq_ent;
+} sync_objset_arg_t;
+
+typedef struct sync_dnodes_arg {
+       multilist_t     *sda_list;
+       int             sda_sublist_idx;
+       multilist_t     *sda_newlist;
+       sync_objset_arg_t *sda_soa;
+} sync_dnodes_arg_t;
+
+static void sync_meta_dnode_task(void *arg);
+
+static void
+sync_dnodes_task(void *arg)
+{
+       sync_dnodes_arg_t *sda = arg;
+       sync_objset_arg_t *soa = sda->sda_soa;
+       objset_t *os = soa->soa_os;
+
+       multilist_sublist_t *ms =
+           multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
+
+       dmu_objset_sync_dnodes(ms, soa->soa_tx);
+
+       multilist_sublist_unlock(ms);
+
+       kmem_free(sda, sizeof (*sda));
+
+       mutex_enter(&soa->soa_mutex);
+       ASSERT(soa->soa_count != 0);
+       if (--soa->soa_count != 0) {
+               mutex_exit(&soa->soa_mutex);
+               return;
+       }
+       mutex_exit(&soa->soa_mutex);
+
+       taskq_dispatch_ent(dmu_objset_pool(os)->dp_sync_taskq,
+           sync_meta_dnode_task, soa, TQ_FRONT, &soa->soa_tq_ent);
+}
+
+/*
+ * Issue the zio_nowait() for all dirty record zios on the meta dnode,
+ * then trigger the callback for the zil_sync. This runs once for each
+ * objset, only after any/all sublists in the objset have been synced.
+ */
+static void
+sync_meta_dnode_task(void *arg)
+{
+       sync_objset_arg_t *soa = arg;
+       objset_t *os = soa->soa_os;
+       dmu_tx_t *tx = soa->soa_tx;
+       int txgoff = tx->tx_txg & TXG_MASK;
+       dbuf_dirty_record_t *dr;
+
+       ASSERT0(soa->soa_count);
+
+       list_t *list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
+       while ((dr = list_remove_head(list)) != NULL) {
+               ASSERT0(dr->dr_dbuf->db_level);
+               zio_nowait(dr->dr_zio);
+       }
+
+       /* Enable dnode backfill if enough objects have been freed. */
+       if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
+               os->os_rescan_dnodes = B_TRUE;
+               os->os_freed_dnodes = 0;
+       }
+
+       /*
+        * Free intent log blocks up to this tx.
+        */
+       zil_sync(os->os_zil, tx);
+       os->os_phys->os_zil_header = os->os_zil_header;
+       zio_nowait(soa->soa_zio);
+
+       mutex_destroy(&soa->soa_mutex);
+       kmem_free(soa, sizeof (*soa));
 }
 
 /* called from dsl */
@@ -1221,11 +1732,12 @@ dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
        zbookmark_phys_t zb;
        zio_prop_t zp;
        zio_t *zio;
-       list_t *list;
-       list_t *newlist = NULL;
-       dbuf_dirty_record_t *dr;
+       int num_sublists;
+       multilist_t *ml;
+       blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
+       *blkptr_copy = *os->os_rootbp;
 
-       dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
+       dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", (u_longlong_t)tx->tx_txg);
 
        ASSERT(dmu_tx_is_syncing(tx));
        /* XXX the write_done callback should really give us the tx... */
@@ -1248,11 +1760,24 @@ dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
            ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
        arc_release(os->os_phys_buf, &os->os_phys_buf);
 
-       dmu_write_policy(os, NULL, 0, 0, ZIO_COMPRESS_INHERIT, &zp);
+       dmu_write_policy(os, NULL, 0, 0, &zp);
+
+       /*
+        * If we are either claiming the ZIL or doing a raw receive, write
+        * out the os_phys_buf raw. Neither of these actions will effect the
+        * MAC at this point.
+        */
+       if (os->os_raw_receive ||
+           os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
+               ASSERT(os->os_encrypted);
+               arc_convert_to_raw(os->os_phys_buf,
+                   os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER,
+                   DMU_OT_OBJSET, NULL, NULL, NULL);
+       }
 
        zio = arc_write(pio, os->os_spa, tx->tx_txg,
-           os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
-           &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
+           blkptr_copy, os->os_phys_buf, B_FALSE, dmu_os_is_l2cacheable(os),
+           &zp, dmu_objset_write_ready, NULL, dmu_objset_write_done,
            os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
 
        /*
@@ -1271,63 +1796,102 @@ dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
                dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
        }
 
+       if (DMU_PROJECTUSED_DNODE(os) &&
+           DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
+               DMU_PROJECTUSED_DNODE(os)->dn_zio = zio;
+               dnode_sync(DMU_PROJECTUSED_DNODE(os), tx);
+       }
+
        txgoff = tx->tx_txg & TXG_MASK;
 
-       if (dmu_objset_userused_enabled(os)) {
-               newlist = &os->os_synced_dnodes;
-               /*
-                * We must create the list here because it uses the
-                * dn_dirty_link[] of this txg.
-                */
-               list_create(newlist, sizeof (dnode_t),
+       /*
+        * We must create the list here because it uses the
+        * dn_dirty_link[] of this txg.  But it may already
+        * exist because we call dsl_dataset_sync() twice per txg.
+        */
+       if (os->os_synced_dnodes.ml_sublists == NULL) {
+               multilist_create(&os->os_synced_dnodes, sizeof (dnode_t),
+                   offsetof(dnode_t, dn_dirty_link[txgoff]),
+                   dnode_multilist_index_func);
+       } else {
+               ASSERT3U(os->os_synced_dnodes.ml_offset, ==,
                    offsetof(dnode_t, dn_dirty_link[txgoff]));
        }
 
-       dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
-       dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
-
-       list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
-       while ((dr = list_head(list))) {
-               ASSERT0(dr->dr_dbuf->db_level);
-               list_remove(list, dr);
-               if (dr->dr_zio)
-                       zio_nowait(dr->dr_zio);
+       /*
+        * zio_nowait(zio) is done after any/all sublist and meta dnode
+        * zios have been nowaited, and the zil_sync() has been performed.
+        * The soa is freed at the end of sync_meta_dnode_task.
+        */
+       sync_objset_arg_t *soa = kmem_alloc(sizeof (*soa), KM_SLEEP);
+       soa->soa_zio = zio;
+       soa->soa_os = os;
+       soa->soa_tx = tx;
+       taskq_init_ent(&soa->soa_tq_ent);
+       mutex_init(&soa->soa_mutex, NULL, MUTEX_DEFAULT, NULL);
+
+       ml = &os->os_dirty_dnodes[txgoff];
+       soa->soa_count = num_sublists = multilist_get_num_sublists(ml);
+
+       for (int i = 0; i < num_sublists; i++) {
+               if (multilist_sublist_is_empty_idx(ml, i))
+                       soa->soa_count--;
        }
 
-       /* Enable dnode backfill if enough objects have been freed. */
-       if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
-               os->os_rescan_dnodes = B_TRUE;
-               os->os_freed_dnodes = 0;
+       if (soa->soa_count == 0) {
+               taskq_dispatch_ent(dmu_objset_pool(os)->dp_sync_taskq,
+                   sync_meta_dnode_task, soa, TQ_FRONT, &soa->soa_tq_ent);
+       } else {
+               /*
+                * Sync sublists in parallel. The last to finish
+                * (i.e., when soa->soa_count reaches zero) must
+                *  dispatch sync_meta_dnode_task.
+                */
+               for (int i = 0; i < num_sublists; i++) {
+                       if (multilist_sublist_is_empty_idx(ml, i))
+                               continue;
+                       sync_dnodes_arg_t *sda =
+                           kmem_alloc(sizeof (*sda), KM_SLEEP);
+                       sda->sda_list = ml;
+                       sda->sda_sublist_idx = i;
+                       sda->sda_soa = soa;
+                       (void) taskq_dispatch(
+                           dmu_objset_pool(os)->dp_sync_taskq,
+                           sync_dnodes_task, sda, 0);
+                       /* sync_dnodes_task frees sda */
+               }
        }
-
-       /*
-        * Free intent log blocks up to this tx.
-        */
-       zil_sync(os->os_zil, tx);
-       os->os_phys->os_zil_header = os->os_zil_header;
-       zio_nowait(zio);
 }
 
 boolean_t
 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
 {
-       return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
-           !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
+       return (!multilist_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]));
 }
 
-static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
+static file_info_cb_t *file_cbs[DMU_OST_NUMTYPES];
 
 void
-dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
+dmu_objset_register_type(dmu_objset_type_t ost, file_info_cb_t *cb)
+{
+       file_cbs[ost] = cb;
+}
+
+int
+dmu_get_file_info(objset_t *os, dmu_object_type_t bonustype, const void *data,
+    zfs_file_info_t *zfi)
 {
-       used_cbs[ost] = cb;
+       file_info_cb_t *cb = file_cbs[os->os_phys->os_type];
+       if (cb == NULL)
+               return (EINVAL);
+       return (cb(bonustype, data, zfi));
 }
 
 boolean_t
 dmu_objset_userused_enabled(objset_t *os)
 {
        return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
-           used_cbs[os->os_phys->os_type] != NULL &&
+           file_cbs[os->os_phys->os_type] != NULL &&
            DMU_USERUSED_DNODE(os) != NULL);
 }
 
@@ -1338,87 +1902,219 @@ dmu_objset_userobjused_enabled(objset_t *os)
            spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
 }
 
+boolean_t
+dmu_objset_projectquota_enabled(objset_t *os)
+{
+       return (file_cbs[os->os_phys->os_type] != NULL &&
+           DMU_PROJECTUSED_DNODE(os) != NULL &&
+           spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA));
+}
+
+typedef struct userquota_node {
+       /* must be in the first filed, see userquota_update_cache() */
+       char            uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
+       int64_t         uqn_delta;
+       avl_node_t      uqn_node;
+} userquota_node_t;
+
+typedef struct userquota_cache {
+       avl_tree_t uqc_user_deltas;
+       avl_tree_t uqc_group_deltas;
+       avl_tree_t uqc_project_deltas;
+} userquota_cache_t;
+
+static int
+userquota_compare(const void *l, const void *r)
+{
+       const userquota_node_t *luqn = l;
+       const userquota_node_t *ruqn = r;
+       int rv;
+
+       /*
+        * NB: can only access uqn_id because userquota_update_cache() doesn't
+        * pass in an entire userquota_node_t.
+        */
+       rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
+
+       return (TREE_ISIGN(rv));
+}
+
 static void
-do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
-    uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
+do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
 {
-       if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
+       void *cookie;
+       userquota_node_t *uqn;
+
+       ASSERT(dmu_tx_is_syncing(tx));
+
+       cookie = NULL;
+       while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
+           &cookie)) != NULL) {
+               /*
+                * os_userused_lock protects against concurrent calls to
+                * zap_increment_int().  It's needed because zap_increment_int()
+                * is not thread-safe (i.e. not atomic).
+                */
+               mutex_enter(&os->os_userused_lock);
+               VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
+                   uqn->uqn_id, uqn->uqn_delta, tx));
+               mutex_exit(&os->os_userused_lock);
+               kmem_free(uqn, sizeof (*uqn));
+       }
+       avl_destroy(&cache->uqc_user_deltas);
+
+       cookie = NULL;
+       while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
+           &cookie)) != NULL) {
+               mutex_enter(&os->os_userused_lock);
+               VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
+                   uqn->uqn_id, uqn->uqn_delta, tx));
+               mutex_exit(&os->os_userused_lock);
+               kmem_free(uqn, sizeof (*uqn));
+       }
+       avl_destroy(&cache->uqc_group_deltas);
+
+       if (dmu_objset_projectquota_enabled(os)) {
+               cookie = NULL;
+               while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas,
+                   &cookie)) != NULL) {
+                       mutex_enter(&os->os_userused_lock);
+                       VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT,
+                           uqn->uqn_id, uqn->uqn_delta, tx));
+                       mutex_exit(&os->os_userused_lock);
+                       kmem_free(uqn, sizeof (*uqn));
+               }
+               avl_destroy(&cache->uqc_project_deltas);
+       }
+}
+
+static void
+userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
+{
+       userquota_node_t *uqn;
+       avl_index_t idx;
+
+       ASSERT(strlen(id) < sizeof (uqn->uqn_id));
+       /*
+        * Use id directly for searching because uqn_id is the first field of
+        * userquota_node_t and fields after uqn_id won't be accessed in
+        * avl_find().
+        */
+       uqn = avl_find(avl, (const void *)id, &idx);
+       if (uqn == NULL) {
+               uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
+               strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
+               avl_insert(avl, uqn, idx);
+       }
+       uqn->uqn_delta += delta;
+}
+
+static void
+do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used,
+    uint64_t flags, uint64_t user, uint64_t group, uint64_t project,
+    boolean_t subtract)
+{
+       if (flags & DNODE_FLAG_USERUSED_ACCOUNTED) {
                int64_t delta = DNODE_MIN_SIZE + used;
+               char name[20];
+
                if (subtract)
                        delta = -delta;
-               VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
-                   user, delta, tx));
-               VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
-                   group, delta, tx));
+
+               (void) snprintf(name, sizeof (name), "%llx", (longlong_t)user);
+               userquota_update_cache(&cache->uqc_user_deltas, name, delta);
+
+               (void) snprintf(name, sizeof (name), "%llx", (longlong_t)group);
+               userquota_update_cache(&cache->uqc_group_deltas, name, delta);
+
+               if (dmu_objset_projectquota_enabled(os)) {
+                       (void) snprintf(name, sizeof (name), "%llx",
+                           (longlong_t)project);
+                       userquota_update_cache(&cache->uqc_project_deltas,
+                           name, delta);
+               }
        }
 }
 
 static void
-do_userobjquota_update(objset_t *os, uint64_t flags, uint64_t user,
-    uint64_t group, boolean_t subtract, dmu_tx_t *tx)
+do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags,
+    uint64_t user, uint64_t group, uint64_t project, boolean_t subtract)
 {
        if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
                char name[20 + DMU_OBJACCT_PREFIX_LEN];
+               int delta = subtract ? -1 : 1;
 
                (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
                    (longlong_t)user);
-               VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT, name,
-                   subtract ? -1 : 1, tx));
+               userquota_update_cache(&cache->uqc_user_deltas, name, delta);
 
                (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
                    (longlong_t)group);
-               VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT, name,
-                   subtract ? -1 : 1, tx));
+               userquota_update_cache(&cache->uqc_group_deltas, name, delta);
+
+               if (dmu_objset_projectquota_enabled(os)) {
+                       (void) snprintf(name, sizeof (name),
+                           DMU_OBJACCT_PREFIX "%llx", (longlong_t)project);
+                       userquota_update_cache(&cache->uqc_project_deltas,
+                           name, delta);
+               }
        }
 }
 
-void
-dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
+typedef struct userquota_updates_arg {
+       objset_t *uua_os;
+       int uua_sublist_idx;
+       dmu_tx_t *uua_tx;
+} userquota_updates_arg_t;
+
+static void
+userquota_updates_task(void *arg)
 {
+       userquota_updates_arg_t *uua = arg;
+       objset_t *os = uua->uua_os;
+       dmu_tx_t *tx = uua->uua_tx;
        dnode_t *dn;
-       list_t *list = &os->os_synced_dnodes;
-
-       ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
-
-       while ((dn = list_head(list))) {
+       userquota_cache_t cache = { { 0 } };
+
+       multilist_sublist_t *list =
+           multilist_sublist_lock(&os->os_synced_dnodes, uua->uua_sublist_idx);
+
+       ASSERT(multilist_sublist_head(list) == NULL ||
+           dmu_objset_userused_enabled(os));
+       avl_create(&cache.uqc_user_deltas, userquota_compare,
+           sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
+       avl_create(&cache.uqc_group_deltas, userquota_compare,
+           sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
+       if (dmu_objset_projectquota_enabled(os))
+               avl_create(&cache.uqc_project_deltas, userquota_compare,
+                   sizeof (userquota_node_t), offsetof(userquota_node_t,
+                   uqn_node));
+
+       while ((dn = multilist_sublist_head(list)) != NULL) {
                int flags;
                ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
                ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
                    dn->dn_phys->dn_flags &
                    DNODE_FLAG_USERUSED_ACCOUNTED);
 
-               /* Allocate the user/groupused objects if necessary. */
-               if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
-                       VERIFY(0 == zap_create_claim(os,
-                           DMU_USERUSED_OBJECT,
-                           DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
-                       VERIFY(0 == zap_create_claim(os,
-                           DMU_GROUPUSED_OBJECT,
-                           DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
-               }
-
-               /*
-                * We intentionally modify the zap object even if the
-                * net delta is zero.  Otherwise
-                * the block of the zap obj could be shared between
-                * datasets but need to be different between them after
-                * a bprewrite.
-                */
-
                flags = dn->dn_id_flags;
                ASSERT(flags);
                if (flags & DN_ID_OLD_EXIST)  {
-                       do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
-                           dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
-                       do_userobjquota_update(os, dn->dn_oldflags,
-                           dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
+                       do_userquota_update(os, &cache, dn->dn_oldused,
+                           dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid,
+                           dn->dn_oldprojid, B_TRUE);
+                       do_userobjquota_update(os, &cache, dn->dn_oldflags,
+                           dn->dn_olduid, dn->dn_oldgid,
+                           dn->dn_oldprojid, B_TRUE);
                }
                if (flags & DN_ID_NEW_EXIST) {
-                       do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
-                           dn->dn_phys->dn_flags,  dn->dn_newuid,
-                           dn->dn_newgid, B_FALSE, tx);
-                       do_userobjquota_update(os, dn->dn_phys->dn_flags,
-                           dn->dn_newuid, dn->dn_newgid, B_FALSE, tx);
+                       do_userquota_update(os, &cache,
+                           DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
+                           dn->dn_newuid, dn->dn_newgid,
+                           dn->dn_newprojid, B_FALSE);
+                       do_userobjquota_update(os, &cache,
+                           dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid,
+                           dn->dn_newprojid, B_FALSE);
                }
 
                mutex_enter(&dn->dn_mtx);
@@ -1427,6 +2123,7 @@ dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
                if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
                        dn->dn_olduid = dn->dn_newuid;
                        dn->dn_oldgid = dn->dn_newgid;
+                       dn->dn_oldprojid = dn->dn_newprojid;
                        dn->dn_id_flags |= DN_ID_OLD_EXIST;
                        if (dn->dn_bonuslen == 0)
                                dn->dn_id_flags |= DN_ID_CHKED_SPILL;
@@ -1436,11 +2133,114 @@ dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
                dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
                mutex_exit(&dn->dn_mtx);
 
-               list_remove(list, dn);
-               dnode_rele(dn, list);
+               multilist_sublist_remove(list, dn);
+               dnode_rele(dn, &os->os_synced_dnodes);
+       }
+       do_userquota_cacheflush(os, &cache, tx);
+       multilist_sublist_unlock(list);
+       kmem_free(uua, sizeof (*uua));
+}
+
+/*
+ * Release dnode holds from dmu_objset_sync_dnodes().  When the dnode is being
+ * synced (i.e. we have issued the zio's for blocks in the dnode), it can't be
+ * evicted because the block containing the dnode can't be evicted until it is
+ * written out.  However, this hold is necessary to prevent the dnode_t from
+ * being moved (via dnode_move()) while it's still referenced by
+ * dbuf_dirty_record_t:dr_dnode.  And dr_dnode is needed for
+ * dirty_lightweight_leaf-type dirty records.
+ *
+ * If we are doing user-object accounting, the dnode_rele() happens from
+ * userquota_updates_task() instead.
+ */
+static void
+dnode_rele_task(void *arg)
+{
+       userquota_updates_arg_t *uua = arg;
+       objset_t *os = uua->uua_os;
+
+       multilist_sublist_t *list =
+           multilist_sublist_lock(&os->os_synced_dnodes, uua->uua_sublist_idx);
+
+       dnode_t *dn;
+       while ((dn = multilist_sublist_head(list)) != NULL) {
+               multilist_sublist_remove(list, dn);
+               dnode_rele(dn, &os->os_synced_dnodes);
+       }
+       multilist_sublist_unlock(list);
+       kmem_free(uua, sizeof (*uua));
+}
+
+/*
+ * Return TRUE if userquota updates are needed.
+ */
+static boolean_t
+dmu_objset_do_userquota_updates_prep(objset_t *os, dmu_tx_t *tx)
+{
+       if (!dmu_objset_userused_enabled(os))
+               return (B_FALSE);
+
+       /*
+        * If this is a raw receive just return and handle accounting
+        * later when we have the keys loaded. We also don't do user
+        * accounting during claiming since the datasets are not owned
+        * for the duration of claiming and this txg should only be
+        * used for recovery.
+        */
+       if (os->os_encrypted && dmu_objset_is_receiving(os))
+               return (B_FALSE);
+
+       if (tx->tx_txg <= os->os_spa->spa_claim_max_txg)
+               return (B_FALSE);
+
+       /* Allocate the user/group/project used objects if necessary. */
+       if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
+               VERIFY0(zap_create_claim(os,
+                   DMU_USERUSED_OBJECT,
+                   DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
+               VERIFY0(zap_create_claim(os,
+                   DMU_GROUPUSED_OBJECT,
+                   DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
        }
+
+       if (dmu_objset_projectquota_enabled(os) &&
+           DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
+               VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT,
+                   DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
+       }
+       return (B_TRUE);
 }
 
+/*
+ * Dispatch taskq tasks to dp_sync_taskq to update the user accounting, and
+ * also release the holds on the dnodes from dmu_objset_sync_dnodes().
+ * The caller must taskq_wait(dp_sync_taskq).
+ */
+void
+dmu_objset_sync_done(objset_t *os, dmu_tx_t *tx)
+{
+       boolean_t need_userquota = dmu_objset_do_userquota_updates_prep(os, tx);
+
+       int num_sublists = multilist_get_num_sublists(&os->os_synced_dnodes);
+       for (int i = 0; i < num_sublists; i++) {
+               userquota_updates_arg_t *uua =
+                   kmem_alloc(sizeof (*uua), KM_SLEEP);
+               uua->uua_os = os;
+               uua->uua_sublist_idx = i;
+               uua->uua_tx = tx;
+
+               /*
+                * If we don't need to update userquotas, use
+                * dnode_rele_task() to call dnode_rele()
+                */
+               (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
+                   need_userquota ? userquota_updates_task : dnode_rele_task,
+                   uua, 0);
+               /* callback frees uua */
+       }
+}
+
+
 /*
  * Returns a pointer to data to find uid/gid from
  *
@@ -1451,31 +2251,22 @@ dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
 static void *
 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
 {
-       dbuf_dirty_record_t *dr, **drp;
+       dbuf_dirty_record_t *dr;
        void *data;
 
        if (db->db_dirtycnt == 0)
                return (db->db.db_data);  /* Nothing is changing */
 
-       for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
-               if (dr->dr_txg == tx->tx_txg)
-                       break;
+       dr = dbuf_find_dirty_eq(db, tx->tx_txg);
 
        if (dr == NULL) {
                data = NULL;
        } else {
-               dnode_t *dn;
-
-               DB_DNODE_ENTER(dr->dr_dbuf);
-               dn = DB_DNODE(dr->dr_dbuf);
-
-               if (dn->dn_bonuslen == 0 &&
+               if (dr->dr_dnode->dn_bonuslen == 0 &&
                    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
                        data = dr->dt.dl.dr_data->b_data;
                else
                        data = dr->dt.dl.dr_data;
-
-               DB_DNODE_EXIT(dr->dr_dbuf);
        }
 
        return (data);
@@ -1487,8 +2278,6 @@ dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
        objset_t *os = dn->dn_objset;
        void *data = NULL;
        dmu_buf_impl_t *db = NULL;
-       uint64_t *user = NULL;
-       uint64_t *group = NULL;
        int flags = dn->dn_id_flags;
        int error;
        boolean_t have_spill = B_FALSE;
@@ -1496,6 +2285,18 @@ dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
        if (!dmu_objset_userused_enabled(dn->dn_objset))
                return;
 
+       /*
+        * Raw receives introduce a problem with user accounting. Raw
+        * receives cannot update the user accounting info because the
+        * user ids and the sizes are encrypted. To guarantee that we
+        * never end up with bad user accounting, we simply disable it
+        * during raw receives. We also disable this for normal receives
+        * so that an incremental raw receive may be done on top of an
+        * existing non-raw receive.
+        */
+       if (os->os_encrypted && dmu_objset_is_receiving(os))
+               return;
+
        if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
            DN_ID_CHKED_SPILL)))
                return;
@@ -1530,21 +2331,23 @@ dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
                return;
        }
 
-       if (before) {
-               ASSERT(data);
-               user = &dn->dn_olduid;
-               group = &dn->dn_oldgid;
-       } else if (data) {
-               user = &dn->dn_newuid;
-               group = &dn->dn_newgid;
-       }
-
        /*
         * Must always call the callback in case the object
         * type has changed and that type isn't an object type to track
         */
-       error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
-           user, group);
+       zfs_file_info_t zfi;
+       error = file_cbs[os->os_phys->os_type](dn->dn_bonustype, data, &zfi);
+
+       if (before) {
+               ASSERT(data);
+               dn->dn_olduid = zfi.zfi_user;
+               dn->dn_oldgid = zfi.zfi_group;
+               dn->dn_oldprojid = zfi.zfi_project;
+       } else if (data) {
+               dn->dn_newuid = zfi.zfi_user;
+               dn->dn_newgid = zfi.zfi_group;
+               dn->dn_newprojid = zfi.zfi_project;
+       }
 
        /*
         * Preserve existing uid/gid when the callback can't determine
@@ -1557,9 +2360,11 @@ dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
                if (flags & DN_ID_OLD_EXIST) {
                        dn->dn_newuid = dn->dn_olduid;
                        dn->dn_newgid = dn->dn_oldgid;
+                       dn->dn_newprojid = dn->dn_oldprojid;
                } else {
                        dn->dn_newuid = 0;
                        dn->dn_newgid = 0;
+                       dn->dn_newprojid = ZFS_DEFAULT_PROJID;
                }
                error = 0;
        }
@@ -1597,6 +2402,13 @@ dmu_objset_userobjspace_present(objset_t *os)
            OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
 }
 
+boolean_t
+dmu_objset_projectquota_present(objset_t *os)
+{
+       return (os->os_phys->os_flags &
+           OBJSET_FLAG_PROJECTQUOTA_COMPLETE);
+}
+
 static int
 dmu_objset_space_upgrade(objset_t *os)
 {
@@ -1633,6 +2445,7 @@ dmu_objset_space_upgrade(objset_t *os)
                dmu_tx_hold_bonus(tx, obj);
                objerr = dmu_tx_assign(tx, TXG_WAIT);
                if (objerr != 0) {
+                       dmu_buf_rele(db, FTAG);
                        dmu_tx_abort(tx);
                        continue;
                }
@@ -1643,8 +2456,8 @@ dmu_objset_space_upgrade(objset_t *os)
        return (0);
 }
 
-int
-dmu_objset_userspace_upgrade(objset_t *os)
+static int
+dmu_objset_userspace_upgrade_cb(objset_t *os)
 {
        int err = 0;
 
@@ -1664,34 +2477,73 @@ dmu_objset_userspace_upgrade(objset_t *os)
        return (0);
 }
 
+void
+dmu_objset_userspace_upgrade(objset_t *os)
+{
+       dmu_objset_upgrade(os, dmu_objset_userspace_upgrade_cb);
+}
+
 static int
-dmu_objset_userobjspace_upgrade_cb(objset_t *os)
+dmu_objset_id_quota_upgrade_cb(objset_t *os)
 {
        int err = 0;
 
-       if (dmu_objset_userobjspace_present(os))
+       if (dmu_objset_userobjspace_present(os) &&
+           dmu_objset_projectquota_present(os))
                return (0);
        if (dmu_objset_is_snapshot(os))
                return (SET_ERROR(EINVAL));
-       if (!dmu_objset_userobjused_enabled(os))
+       if (!dmu_objset_userused_enabled(os))
+               return (SET_ERROR(ENOTSUP));
+       if (!dmu_objset_projectquota_enabled(os) &&
+           dmu_objset_userobjspace_present(os))
                return (SET_ERROR(ENOTSUP));
 
-       dmu_objset_ds(os)->ds_feature_activation_needed[
-           SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
+       if (dmu_objset_userobjused_enabled(os))
+               dmu_objset_ds(os)->ds_feature_activation[
+                   SPA_FEATURE_USEROBJ_ACCOUNTING] = (void *)B_TRUE;
+       if (dmu_objset_projectquota_enabled(os))
+               dmu_objset_ds(os)->ds_feature_activation[
+                   SPA_FEATURE_PROJECT_QUOTA] = (void *)B_TRUE;
 
        err = dmu_objset_space_upgrade(os);
        if (err)
                return (err);
 
-       os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
+       os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
+       if (dmu_objset_userobjused_enabled(os))
+               os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
+       if (dmu_objset_projectquota_enabled(os))
+               os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
+
        txg_wait_synced(dmu_objset_pool(os), 0);
        return (0);
 }
 
 void
-dmu_objset_userobjspace_upgrade(objset_t *os)
+dmu_objset_id_quota_upgrade(objset_t *os)
+{
+       dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb);
+}
+
+boolean_t
+dmu_objset_userobjspace_upgradable(objset_t *os)
 {
-       dmu_objset_upgrade(os, dmu_objset_userobjspace_upgrade_cb);
+       return (dmu_objset_type(os) == DMU_OST_ZFS &&
+           !dmu_objset_is_snapshot(os) &&
+           dmu_objset_userobjused_enabled(os) &&
+           !dmu_objset_userobjspace_present(os) &&
+           spa_writeable(dmu_objset_spa(os)));
+}
+
+boolean_t
+dmu_objset_projectquota_upgradable(objset_t *os)
+{
+       return (dmu_objset_type(os) == DMU_OST_ZFS &&
+           !dmu_objset_is_snapshot(os) &&
+           dmu_objset_projectquota_enabled(os) &&
+           !dmu_objset_projectquota_present(os) &&
+           spa_writeable(dmu_objset_spa(os)));
 }
 
 void
@@ -1741,7 +2593,7 @@ dmu_objset_is_snapshot(objset_t *os)
 }
 
 int
-dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
+dmu_snapshot_realname(objset_t *os, const char *name, char *real, int maxlen,
     boolean_t *conflict)
 {
        dsl_dataset_t *ds = os->os_dsl_dataset;
@@ -1752,7 +2604,7 @@ dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
 
        return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
            dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
-           MT_FIRST, real, maxlen, conflict));
+           MT_NORMALIZE, real, maxlen, conflict));
 }
 
 int
@@ -1782,7 +2634,7 @@ dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
                return (SET_ERROR(ENAMETOOLONG));
        }
 
-       (void) strcpy(name, attr.za_name);
+       (void) strlcpy(name, attr.za_name, namelen);
        if (idp)
                *idp = attr.za_first_integer;
        if (case_conflict)
@@ -1827,7 +2679,7 @@ dmu_dir_list_next(objset_t *os, int namelen, char *name,
                return (SET_ERROR(ENAMETOOLONG));
        }
 
-       (void) strcpy(name, attr.za_name);
+       (void) strlcpy(name, attr.za_name, namelen);
        if (idp)
                *idp = attr.za_first_integer;
        zap_cursor_advance(&cursor);
@@ -1841,6 +2693,7 @@ typedef struct dmu_objset_find_ctx {
        taskq_t         *dc_tq;
        dsl_pool_t      *dc_dp;
        uint64_t        dc_ddobj;
+       char            *dc_ddname; /* last component of ddobj's name */
        int             (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
        void            *dc_arg;
        int             dc_flags;
@@ -1852,7 +2705,6 @@ static void
 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
 {
        dsl_pool_t *dp = dcp->dc_dp;
-       dmu_objset_find_ctx_t *child_dcp;
        dsl_dir_t *dd;
        dsl_dataset_t *ds;
        zap_cursor_t zc;
@@ -1864,7 +2716,12 @@ dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
        if (*dcp->dc_error != 0)
                goto out;
 
-       err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
+       /*
+        * Note: passing the name (dc_ddname) here is optional, but it
+        * improves performance because we don't need to call
+        * zap_value_search() to determine the name.
+        */
+       err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
        if (err != 0)
                goto out;
 
@@ -1889,9 +2746,11 @@ dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
                            sizeof (uint64_t));
                        ASSERT3U(attr->za_num_integers, ==, 1);
 
-                       child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
+                       dmu_objset_find_ctx_t *child_dcp =
+                           kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
                        *child_dcp = *dcp;
                        child_dcp->dc_ddobj = attr->za_first_integer;
+                       child_dcp->dc_ddname = spa_strdup(attr->za_name);
                        if (dcp->dc_tq != NULL)
                                (void) taskq_dispatch(dcp->dc_tq,
                                    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
@@ -1934,16 +2793,25 @@ dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
                }
        }
 
-       dsl_dir_rele(dd, FTAG);
        kmem_free(attr, sizeof (zap_attribute_t));
 
-       if (err != 0)
+       if (err != 0) {
+               dsl_dir_rele(dd, FTAG);
                goto out;
+       }
 
        /*
         * Apply to self.
         */
        err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
+
+       /*
+        * Note: we hold the dir while calling dsl_dataset_hold_obj() so
+        * that the dir will remain cached, and we won't have to re-instantiate
+        * it (which could be expensive due to finding its name via
+        * zap_value_search()).
+        */
+       dsl_dir_rele(dd, FTAG);
        if (err != 0)
                goto out;
        err = dcp->dc_func(dp, ds, dcp->dc_arg);
@@ -1958,6 +2826,8 @@ out:
                mutex_exit(dcp->dc_error_lock);
        }
 
+       if (dcp->dc_ddname != NULL)
+               spa_strfree(dcp->dc_ddname);
        kmem_free(dcp, sizeof (*dcp));
 }
 
@@ -1969,7 +2839,7 @@ dmu_objset_find_dp_cb(void *arg)
 
        /*
         * We need to get a pool_config_lock here, as there are several
-        * asssert(pool_config_held) down the stack. Getting a lock via
+        * assert(pool_config_held) down the stack. Getting a lock via
         * dsl_pool_config_enter is risky, as it might be stalled by a
         * pending writer. This would deadlock, as the write lock can
         * only be granted when our parent thread gives up the lock.
@@ -2002,6 +2872,7 @@ dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
        dcp->dc_tq = NULL;
        dcp->dc_dp = dp;
        dcp->dc_ddobj = ddobj;
+       dcp->dc_ddname = NULL;
        dcp->dc_func = func;
        dcp->dc_arg = arg;
        dcp->dc_flags = flags;
@@ -2115,7 +2986,7 @@ dmu_objset_find_impl(spa_t *spa, const char *name,
                        err = dmu_objset_find_impl(spa, child,
                            func, arg, flags);
                        dsl_pool_config_enter(dp, FTAG);
-                       strfree(child);
+                       kmem_strfree(child);
                        if (err != 0)
                                break;
                }
@@ -2153,7 +3024,7 @@ dmu_objset_find_impl(spa_t *spa, const char *name,
                                dsl_pool_config_exit(dp, FTAG);
                                err = func(child, arg);
                                dsl_pool_config_enter(dp, FTAG);
-                               strfree(child);
+                               kmem_strfree(child);
                                if (err != 0)
                                        break;
                        }
@@ -2176,7 +3047,7 @@ dmu_objset_find_impl(spa_t *spa, const char *name,
  * See comment above dmu_objset_find_impl().
  */
 int
-dmu_objset_find(char *name, int func(const char *, void *), void *arg,
+dmu_objset_find(const char *name, int func(const char *, void *), void *arg,
     int flags)
 {
        spa_t *spa;
@@ -2190,6 +3061,13 @@ dmu_objset_find(char *name, int func(const char *, void *), void *arg,
        return (error);
 }
 
+boolean_t
+dmu_objset_incompatible_encryption_version(objset_t *os)
+{
+       return (dsl_dir_incompatible_encryption_version(
+           os->os_dsl_dataset->ds_dir));
+}
+
 void
 dmu_objset_set_user(objset_t *os, void *user_ptr)
 {
@@ -2220,15 +3098,43 @@ dmu_fsname(const char *snapname, char *buf)
        return (0);
 }
 
-#if defined(_KERNEL) && defined(HAVE_SPL)
+/*
+ * Call when we think we're going to write/free space in open context
+ * to track the amount of dirty data in the open txg, which is also the
+ * amount of memory that can not be evicted until this txg syncs.
+ *
+ * Note that there are two conditions where this can be called from
+ * syncing context:
+ *
+ * [1] When we just created the dataset, in which case we go on with
+ *     updating any accounting of dirty data as usual.
+ * [2] When we are dirtying MOS data, in which case we only update the
+ *     pool's accounting of dirty data.
+ */
+void
+dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
+{
+       dsl_dataset_t *ds = os->os_dsl_dataset;
+       int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
+
+       if (ds != NULL) {
+               dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
+       }
+
+       dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
+}
+
+#if defined(_KERNEL)
 EXPORT_SYMBOL(dmu_objset_zil);
 EXPORT_SYMBOL(dmu_objset_pool);
 EXPORT_SYMBOL(dmu_objset_ds);
 EXPORT_SYMBOL(dmu_objset_type);
 EXPORT_SYMBOL(dmu_objset_name);
 EXPORT_SYMBOL(dmu_objset_hold);
+EXPORT_SYMBOL(dmu_objset_hold_flags);
 EXPORT_SYMBOL(dmu_objset_own);
 EXPORT_SYMBOL(dmu_objset_rele);
+EXPORT_SYMBOL(dmu_objset_rele_flags);
 EXPORT_SYMBOL(dmu_objset_disown);
 EXPORT_SYMBOL(dmu_objset_from_ds);
 EXPORT_SYMBOL(dmu_objset_create);
@@ -2246,16 +3152,21 @@ EXPORT_SYMBOL(dmu_objset_dnodesize);
 
 EXPORT_SYMBOL(dmu_objset_sync);
 EXPORT_SYMBOL(dmu_objset_is_dirty);
+EXPORT_SYMBOL(dmu_objset_create_impl_dnstats);
 EXPORT_SYMBOL(dmu_objset_create_impl);
 EXPORT_SYMBOL(dmu_objset_open_impl);
 EXPORT_SYMBOL(dmu_objset_evict);
 EXPORT_SYMBOL(dmu_objset_register_type);
-EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
+EXPORT_SYMBOL(dmu_objset_sync_done);
 EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
 EXPORT_SYMBOL(dmu_objset_userused_enabled);
 EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
 EXPORT_SYMBOL(dmu_objset_userspace_present);
 EXPORT_SYMBOL(dmu_objset_userobjused_enabled);
-EXPORT_SYMBOL(dmu_objset_userobjspace_upgrade);
+EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable);
 EXPORT_SYMBOL(dmu_objset_userobjspace_present);
+EXPORT_SYMBOL(dmu_objset_projectquota_enabled);
+EXPORT_SYMBOL(dmu_objset_projectquota_present);
+EXPORT_SYMBOL(dmu_objset_projectquota_upgradable);
+EXPORT_SYMBOL(dmu_objset_id_quota_upgrade);
 #endif