Enable zfs_znode compilation
[zfs.git] / module / zfs / zfs_znode.c
index f99e72f..72a077d 100644 (file)
@@ -19,8 +19,7 @@
  * CDDL HEADER END
  */
 /*
- * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
- * Use is subject to license terms.
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  */
 
 /* Portions Copyright 2007 Jeremy Teo */
@@ -52,6 +51,7 @@
 #include <sys/zfs_ioctl.h>
 #include <sys/zfs_rlock.h>
 #include <sys/zfs_fuid.h>
+#include <sys/dnode.h>
 #include <sys/fs/zfs.h>
 #include <sys/kidmap.h>
 #endif /* _KERNEL */
 #include <sys/stat.h>
 #include <sys/zap.h>
 #include <sys/zfs_znode.h>
+#include <sys/sa.h>
+#include <sys/zfs_sa.h>
+#include <sys/zfs_stat.h>
 
 #include "zfs_prop.h"
+#include "zfs_comutil.h"
 
 /*
  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
@@ -78,9 +82,6 @@
 #define        ZNODE_STAT_ADD(stat)                    /* nothing */
 #endif /* ZNODE_STATS */
 
-#define        POINTER_IS_VALID(p)     (!((uintptr_t)(p) & 0x3))
-#define        POINTER_INVALIDATE(pp)  (*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
-
 /*
  * Functions needed for userland (ie: libzpool) are not put under
  * #ifdef_KERNEL; the rest of the functions have dependencies
@@ -96,17 +97,6 @@ krwlock_t zfsvfs_lock;
 static kmem_cache_t *znode_cache = NULL;
 
 /*ARGSUSED*/
-static void
-znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
-{
-       /*
-        * We should never drop all dbuf refs without first clearing
-        * the eviction callback.
-        */
-       panic("evicting znode %p\n", user_ptr);
-}
-
-/*ARGSUSED*/
 static int
 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
 {
@@ -131,9 +121,9 @@ zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
        avl_create(&zp->z_range_avl, zfs_range_compare,
            sizeof (rl_t), offsetof(rl_t, r_node));
 
-       zp->z_dbuf = NULL;
        zp->z_dirlocks = NULL;
        zp->z_acl_cached = NULL;
+       zp->z_moved = 0;
        return (0);
 }
 
@@ -154,187 +144,10 @@ zfs_znode_cache_destructor(void *buf, void *arg)
        avl_destroy(&zp->z_range_avl);
        mutex_destroy(&zp->z_range_lock);
 
-       ASSERT(zp->z_dbuf == NULL);
        ASSERT(zp->z_dirlocks == NULL);
        ASSERT(zp->z_acl_cached == NULL);
 }
 
-#ifdef ZNODE_STATS
-static struct {
-       uint64_t zms_zfsvfs_invalid;
-       uint64_t zms_zfsvfs_recheck1;
-       uint64_t zms_zfsvfs_unmounted;
-       uint64_t zms_zfsvfs_recheck2;
-       uint64_t zms_obj_held;
-       uint64_t zms_vnode_locked;
-       uint64_t zms_not_only_dnlc;
-} znode_move_stats;
-#endif /* ZNODE_STATS */
-
-static void
-zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
-{
-       vnode_t *vp;
-
-       /* Copy fields. */
-       nzp->z_zfsvfs = ozp->z_zfsvfs;
-
-       /* Swap vnodes. */
-       vp = nzp->z_vnode;
-       nzp->z_vnode = ozp->z_vnode;
-       ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
-       ZTOV(ozp)->v_data = ozp;
-       ZTOV(nzp)->v_data = nzp;
-
-       nzp->z_id = ozp->z_id;
-       ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
-       ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
-       nzp->z_unlinked = ozp->z_unlinked;
-       nzp->z_atime_dirty = ozp->z_atime_dirty;
-       nzp->z_zn_prefetch = ozp->z_zn_prefetch;
-       nzp->z_blksz = ozp->z_blksz;
-       nzp->z_seq = ozp->z_seq;
-       nzp->z_mapcnt = ozp->z_mapcnt;
-       nzp->z_last_itx = ozp->z_last_itx;
-       nzp->z_gen = ozp->z_gen;
-       nzp->z_sync_cnt = ozp->z_sync_cnt;
-       nzp->z_phys = ozp->z_phys;
-       nzp->z_dbuf = ozp->z_dbuf;
-
-       /*
-        * Release any cached ACL, since it *may* have
-        * zfs_acl_node_t's that directly references an
-        * embedded ACL in the zp_acl of the old znode_phys_t
-        *
-        * It will be recached the next time the ACL is needed.
-        */
-       if (ozp->z_acl_cached) {
-               zfs_acl_free(ozp->z_acl_cached);
-               ozp->z_acl_cached = NULL;
-       }
-
-       /* Update back pointers. */
-       (void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
-           znode_evict_error);
-
-       /*
-        * Invalidate the original znode by clearing fields that provide a
-        * pointer back to the znode. Set the low bit of the vfs pointer to
-        * ensure that zfs_znode_move() recognizes the znode as invalid in any
-        * subsequent callback.
-        */
-       ozp->z_dbuf = NULL;
-       POINTER_INVALIDATE(&ozp->z_zfsvfs);
-}
-
-/*ARGSUSED*/
-static kmem_cbrc_t
-zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
-{
-       znode_t *ozp = buf, *nzp = newbuf;
-       zfsvfs_t *zfsvfs;
-       vnode_t *vp;
-
-       /*
-        * The znode is on the file system's list of known znodes if the vfs
-        * pointer is valid. We set the low bit of the vfs pointer when freeing
-        * the znode to invalidate it, and the memory patterns written by kmem
-        * (baddcafe and deadbeef) set at least one of the two low bits. A newly
-        * created znode sets the vfs pointer last of all to indicate that the
-        * znode is known and in a valid state to be moved by this function.
-        */
-       zfsvfs = ozp->z_zfsvfs;
-       if (!POINTER_IS_VALID(zfsvfs)) {
-               ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
-               return (KMEM_CBRC_DONT_KNOW);
-       }
-
-       /*
-        * Close a small window in which it's possible that the filesystem could
-        * be unmounted and freed, and zfsvfs, though valid in the previous
-        * statement, could point to unrelated memory by the time we try to
-        * prevent the filesystem from being unmounted.
-        */
-       rw_enter(&zfsvfs_lock, RW_WRITER);
-       if (zfsvfs != ozp->z_zfsvfs) {
-               rw_exit(&zfsvfs_lock);
-               ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
-               return (KMEM_CBRC_DONT_KNOW);
-       }
-
-       /*
-        * If the znode is still valid, then so is the file system. We know that
-        * no valid file system can be freed while we hold zfsvfs_lock, so we
-        * can safely ensure that the filesystem is not and will not be
-        * unmounted. The next statement is equivalent to ZFS_ENTER().
-        */
-       rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
-       if (zfsvfs->z_unmounted) {
-               ZFS_EXIT(zfsvfs);
-               rw_exit(&zfsvfs_lock);
-               ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
-               return (KMEM_CBRC_DONT_KNOW);
-       }
-       rw_exit(&zfsvfs_lock);
-
-       mutex_enter(&zfsvfs->z_znodes_lock);
-       /*
-        * Recheck the vfs pointer in case the znode was removed just before
-        * acquiring the lock.
-        */
-       if (zfsvfs != ozp->z_zfsvfs) {
-               mutex_exit(&zfsvfs->z_znodes_lock);
-               ZFS_EXIT(zfsvfs);
-               ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
-               return (KMEM_CBRC_DONT_KNOW);
-       }
-
-       /*
-        * At this point we know that as long as we hold z_znodes_lock, the
-        * znode cannot be freed and fields within the znode can be safely
-        * accessed. Now, prevent a race with zfs_zget().
-        */
-       if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
-               mutex_exit(&zfsvfs->z_znodes_lock);
-               ZFS_EXIT(zfsvfs);
-               ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
-               return (KMEM_CBRC_LATER);
-       }
-
-       vp = ZTOV(ozp);
-       if (mutex_tryenter(&vp->v_lock) == 0) {
-               ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
-               mutex_exit(&zfsvfs->z_znodes_lock);
-               ZFS_EXIT(zfsvfs);
-               ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
-               return (KMEM_CBRC_LATER);
-       }
-
-       /* Only move znodes that are referenced _only_ by the DNLC. */
-       if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
-               mutex_exit(&vp->v_lock);
-               ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
-               mutex_exit(&zfsvfs->z_znodes_lock);
-               ZFS_EXIT(zfsvfs);
-               ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
-               return (KMEM_CBRC_LATER);
-       }
-
-       /*
-        * The znode is known and in a valid state to move. We're holding the
-        * locks needed to execute the critical section.
-        */
-       zfs_znode_move_impl(ozp, nzp);
-       mutex_exit(&vp->v_lock);
-       ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
-
-       list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
-       mutex_exit(&zfsvfs->z_znodes_lock);
-       ZFS_EXIT(zfsvfs);
-
-       return (KMEM_CBRC_YES);
-}
-
 void
 zfs_znode_init(void)
 {
@@ -346,7 +159,6 @@ zfs_znode_init(void)
        znode_cache = kmem_cache_create("zfs_znode_cache",
            sizeof (znode_t), 0, zfs_znode_cache_constructor,
            zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
-       kmem_cache_set_move(znode_cache, zfs_znode_move);
 }
 
 void
@@ -355,7 +167,9 @@ zfs_znode_fini(void)
        /*
         * Cleanup vfs & vnode ops
         */
+#ifdef HAVE_ZPL
        zfs_remove_op_tables();
+#endif /* HAVE_ZPL */
 
        /*
         * Cleanup zcache
@@ -366,6 +180,7 @@ zfs_znode_fini(void)
        rw_destroy(&zfsvfs_lock);
 }
 
+#ifdef HAVE_ZPL
 struct vnodeops *zfs_dvnodeops;
 struct vnodeops *zfs_fvnodeops;
 struct vnodeops *zfs_symvnodeops;
@@ -461,6 +276,7 @@ zfs_create_op_tables()
 int
 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
 {
+#ifdef HAVE_SHARE
        zfs_acl_ids_t acl_ids;
        vattr_t vattr;
        znode_t *sharezp;
@@ -475,9 +291,12 @@ zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
        vattr.va_gid = crgetgid(kcred);
 
        sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
+       ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
+       sharezp->z_moved = 0;
        sharezp->z_unlinked = 0;
        sharezp->z_atime_dirty = 0;
        sharezp->z_zfsvfs = zfsvfs;
+       sharezp->z_is_sa = zfsvfs->z_use_sa;
 
        vp = ZTOV(sharezp);
        vn_reinit(vp);
@@ -485,8 +304,7 @@ zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
 
        VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
            kcred, NULL, &acl_ids));
-       zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE,
-           &zp, 0, &acl_ids);
+       zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
        ASSERT3P(zp, ==, sharezp);
        ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
        POINTER_INVALIDATE(&sharezp->z_zfsvfs);
@@ -496,11 +314,13 @@ zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
 
        zfs_acl_ids_free(&acl_ids);
        ZTOV(sharezp)->v_count = 0;
-       dmu_buf_rele(sharezp->z_dbuf, NULL);
-       sharezp->z_dbuf = NULL;
+       sa_handle_destroy(sharezp->z_sa_hdl);
        kmem_cache_free(znode_cache, sharezp);
 
        return (error);
+#else
+       return (0);
+#endif /* HAVE_SHARE */
 }
 
 /*
@@ -517,6 +337,8 @@ zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
 #define        MAXMIN64        0xffffffffUL
 #endif
 
+#endif /* HAVE_ZPL */
+
 /*
  * Create special expldev for ZFS private use.
  * Can't use standard expldev since it doesn't do
@@ -561,25 +383,25 @@ zfs_cmpldev(uint64_t dev)
 }
 
 static void
-zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
+zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
+    dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
 {
-       znode_t         *nzp;
-
        ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
        ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
 
        mutex_enter(&zp->z_lock);
 
-       ASSERT(zp->z_dbuf == NULL);
-       zp->z_dbuf = db;
-       nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
+       ASSERT(zp->z_sa_hdl == NULL);
+       ASSERT(zp->z_acl_cached == NULL);
+       if (sa_hdl == NULL) {
+               VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
+                   SA_HDL_SHARED, &zp->z_sa_hdl));
+       } else {
+               zp->z_sa_hdl = sa_hdl;
+               sa_set_userp(sa_hdl, zp);
+       }
 
-       /*
-        * there should be no
-        * concurrent zgets on this object.
-        */
-       if (nzp != NULL)
-               panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db);
+       zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
 
        /*
         * Slap on VROOT if we are the root znode
@@ -594,14 +416,12 @@ zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
 void
 zfs_znode_dmu_fini(znode_t *zp)
 {
-       dmu_buf_t *db = zp->z_dbuf;
        ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
            zp->z_unlinked ||
            RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
-       ASSERT(zp->z_dbuf != NULL);
-       zp->z_dbuf = NULL;
-       VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
-       dmu_buf_rele(db, NULL);
+
+       sa_handle_destroy(zp->z_sa_hdl);
+       zp->z_sa_hdl = NULL;
 }
 
 /*
@@ -612,44 +432,74 @@ zfs_znode_dmu_fini(znode_t *zp)
  * return the znode
  */
 static znode_t *
-zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
+zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
+    dmu_object_type_t obj_type, sa_handle_t *hdl)
 {
        znode_t *zp;
+#ifdef HAVE_ZPL
        vnode_t *vp;
+       uint64_t mode;
+       uint64_t parent;
+       sa_bulk_attr_t bulk[9];
+       int count = 0;
+#endif /* HAVE_ZPL */
 
        zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
 
        ASSERT(zp->z_dirlocks == NULL);
-       ASSERT(zp->z_dbuf == NULL);
        ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
+       zp->z_moved = 0;
 
        /*
         * Defer setting z_zfsvfs until the znode is ready to be a candidate for
         * the zfs_znode_move() callback.
         */
-       zp->z_phys = NULL;
+       zp->z_sa_hdl = NULL;
        zp->z_unlinked = 0;
        zp->z_atime_dirty = 0;
        zp->z_mapcnt = 0;
-       zp->z_last_itx = 0;
        zp->z_id = db->db_object;
        zp->z_blksz = blksz;
        zp->z_seq = 0x7A4653;
        zp->z_sync_cnt = 0;
 
+#ifdef HAVE_ZPL
        vp = ZTOV(zp);
        vn_reinit(vp);
 
-       zfs_znode_dmu_init(zfsvfs, zp, db);
-
-       zp->z_gen = zp->z_phys->zp_gen;
+       zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
+
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
+           &zp->z_size, 8);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
+           &zp->z_links, 8);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
+           &zp->z_pflags, 8);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
+           &zp->z_atime, 16);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
+           &zp->z_uid, 8);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
+           &zp->z_gid, 8);
+
+       if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
+               if (hdl == NULL)
+                       sa_handle_destroy(zp->z_sa_hdl);
+               kmem_cache_free(znode_cache, zp);
+               return (NULL);
+       }
 
+       zp->z_mode = mode;
        vp->v_vfsp = zfsvfs->z_parent->z_vfs;
-       vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
+
+       vp->v_type = IFTOVT((mode_t)mode);
 
        switch (vp->v_type) {
        case VDIR:
-               if (zp->z_phys->zp_flags & ZFS_XATTR) {
+               if (zp->z_pflags & ZFS_XATTR) {
                        vn_setops(vp, zfs_xdvnodeops);
                        vp->v_flag |= V_XATTRDIR;
                } else {
@@ -659,7 +509,13 @@ zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
                break;
        case VBLK:
        case VCHR:
-               vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
+               {
+                       uint64_t rdev;
+                       VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
+                           &rdev, sizeof (rdev)) == 0);
+
+                       vp->v_rdev = zfs_cmpldev(rdev);
+               }
                /*FALLTHROUGH*/
        case VFIFO:
        case VSOCK:
@@ -668,10 +524,12 @@ zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
                break;
        case VREG:
                vp->v_flag |= VMODSORT;
-               if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir)
+               if (parent == zfsvfs->z_shares_dir) {
+                       ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
                        vn_setops(vp, zfs_sharevnodeops);
-               else
+               } else {
                        vn_setops(vp, zfs_fvnodeops);
+               }
                break;
        case VLNK:
                vn_setops(vp, zfs_symvnodeops);
@@ -680,7 +538,7 @@ zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
                vn_setops(vp, zfs_evnodeops);
                break;
        }
-
+#endif /* HAVE_ZPL */
        mutex_enter(&zfsvfs->z_znodes_lock);
        list_insert_tail(&zfsvfs->z_all_znodes, zp);
        membar_producer();
@@ -695,6 +553,9 @@ zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
        return (zp);
 }
 
+static uint64_t empty_xattr;
+static uint64_t pad[4];
+static zfs_acl_phys_t acl_phys;
 /*
  * Create a new DMU object to hold a zfs znode.
  *
@@ -705,7 +566,6 @@ zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
  *             flag    - flags:
  *                       IS_ROOT_NODE  - new object will be root
  *                       IS_XATTR      - new object is an attribute
- *                       IS_REPLAY     - intent log replay
  *             bonuslen - length of bonus buffer
  *             setaclp  - File/Dir initial ACL
  *             fuidp    - Tracks fuid allocation.
@@ -715,20 +575,28 @@ zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
  */
 void
 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
-    uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_ids_t *acl_ids)
+    uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
 {
-       dmu_buf_t       *db;
-       znode_phys_t    *pzp;
+       uint64_t        crtime[2], atime[2], mtime[2], ctime[2];
+       uint64_t        mode, size, links, parent, pflags;
+       uint64_t        dzp_pflags = 0;
+       uint64_t        rdev = 0;
        zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
+       dmu_buf_t       *db;
        timestruc_t     now;
        uint64_t        gen, obj;
        int             err;
+       int             bonuslen;
+       sa_handle_t     *sa_hdl;
+       dmu_object_type_t obj_type;
+       sa_bulk_attr_t  *sa_attrs;
+       int             cnt = 0;
+       zfs_acl_locator_cb_t locate = { 0 };
 
        ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
 
        if (zfsvfs->z_replay) {
                obj = vap->va_nodeid;
-               flag |= IS_REPLAY;
                now = vap->va_ctime;            /* see zfs_replay_create() */
                gen = vap->va_nblocks;          /* ditto */
        } else {
@@ -737,180 +605,320 @@ zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
                gen = dmu_tx_get_txg(tx);
        }
 
+       obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
+       bonuslen = (obj_type == DMU_OT_SA) ?
+           DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
+
        /*
         * Create a new DMU object.
         */
        /*
         * There's currently no mechanism for pre-reading the blocks that will
-        * be to needed allocate a new object, so we accept the small chance
+        * be needed to allocate a new object, so we accept the small chance
         * that there will be an i/o error and we will fail one of the
         * assertions below.
         */
        if (vap->va_type == VDIR) {
-               if (flag & IS_REPLAY) {
+               if (zfsvfs->z_replay) {
                        err = zap_create_claim_norm(zfsvfs->z_os, obj,
                            zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
-                           DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
+                           obj_type, bonuslen, tx);
                        ASSERT3U(err, ==, 0);
                } else {
                        obj = zap_create_norm(zfsvfs->z_os,
                            zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
-                           DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
+                           obj_type, bonuslen, tx);
                }
        } else {
-               if (flag & IS_REPLAY) {
+               if (zfsvfs->z_replay) {
                        err = dmu_object_claim(zfsvfs->z_os, obj,
                            DMU_OT_PLAIN_FILE_CONTENTS, 0,
-                           DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
+                           obj_type, bonuslen, tx);
                        ASSERT3U(err, ==, 0);
                } else {
                        obj = dmu_object_alloc(zfsvfs->z_os,
                            DMU_OT_PLAIN_FILE_CONTENTS, 0,
-                           DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
+                           obj_type, bonuslen, tx);
                }
        }
-       VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
-       dmu_buf_will_dirty(db, tx);
 
-       /*
-        * Initialize the znode physical data to zero.
-        */
-       ASSERT(db->db_size >= sizeof (znode_phys_t));
-       bzero(db->db_data, db->db_size);
-       pzp = db->db_data;
+       ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
+       VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
 
        /*
         * If this is the root, fix up the half-initialized parent pointer
         * to reference the just-allocated physical data area.
         */
        if (flag & IS_ROOT_NODE) {
-               dzp->z_dbuf = db;
-               dzp->z_phys = pzp;
                dzp->z_id = obj;
+       } else {
+               dzp_pflags = dzp->z_pflags;
        }
 
        /*
         * If parent is an xattr, so am I.
         */
-       if (dzp->z_phys->zp_flags & ZFS_XATTR)
+       if (dzp_pflags & ZFS_XATTR) {
                flag |= IS_XATTR;
-
-       if (vap->va_type == VBLK || vap->va_type == VCHR) {
-               pzp->zp_rdev = zfs_expldev(vap->va_rdev);
        }
 
        if (zfsvfs->z_use_fuids)
-               pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
+               pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
+       else
+               pflags = 0;
 
        if (vap->va_type == VDIR) {
-               pzp->zp_size = 2;               /* contents ("." and "..") */
-               pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
+               size = 2;               /* contents ("." and "..") */
+               links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
+       } else {
+               size = links = 0;
+       }
+
+       if (vap->va_type == VBLK || vap->va_type == VCHR) {
+               rdev = zfs_expldev(vap->va_rdev);
        }
 
-       pzp->zp_parent = dzp->z_id;
+       parent = dzp->z_id;
+       mode = acl_ids->z_mode;
        if (flag & IS_XATTR)
-               pzp->zp_flags |= ZFS_XATTR;
+               pflags |= ZFS_XATTR;
 
-       pzp->zp_gen = gen;
+       /*
+        * No execs denied will be deterimed when zfs_mode_compute() is called.
+        */
+       pflags |= acl_ids->z_aclp->z_hints &
+           (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
+           ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
 
-       ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
-       ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
+       ZFS_TIME_ENCODE(&now, crtime);
+       ZFS_TIME_ENCODE(&now, ctime);
 
        if (vap->va_mask & AT_ATIME) {
-               ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
+               ZFS_TIME_ENCODE(&vap->va_atime, atime);
        } else {
-               ZFS_TIME_ENCODE(&now, pzp->zp_atime);
+               ZFS_TIME_ENCODE(&now, atime);
        }
 
        if (vap->va_mask & AT_MTIME) {
-               ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
+               ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
        } else {
-               ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
+               ZFS_TIME_ENCODE(&now, mtime);
        }
 
-       pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
+       /* Now add in all of the "SA" attributes */
+       VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
+           &sa_hdl));
+
+       /*
+        * Setup the array of attributes to be replaced/set on the new file
+        *
+        * order for  DMU_OT_ZNODE is critical since it needs to be constructed
+        * in the old znode_phys_t format.  Don't change this ordering
+        */
+       sa_attrs = kmem_alloc(sizeof(sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
+
+       if (obj_type == DMU_OT_ZNODE) {
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
+                   NULL, &atime, 16);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
+                   NULL, &mtime, 16);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
+                   NULL, &ctime, 16);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
+                   NULL, &crtime, 16);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
+                   NULL, &gen, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
+                   NULL, &mode, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
+                   NULL, &size, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
+                   NULL, &parent, 8);
+       } else {
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
+                   NULL, &mode, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
+                   NULL, &size, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
+                   NULL, &gen, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
+                   &acl_ids->z_fuid, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
+                   &acl_ids->z_fgid, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
+                   NULL, &parent, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
+                   NULL, &pflags, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
+                   NULL, &atime, 16);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
+                   NULL, &mtime, 16);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
+                   NULL, &ctime, 16);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
+                   NULL, &crtime, 16);
+       }
+
+       SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
+
+       if (obj_type == DMU_OT_ZNODE) {
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
+                   &empty_xattr, 8);
+       }
+       if (obj_type == DMU_OT_ZNODE ||
+           (vap->va_type == VBLK || vap->va_type == VCHR)) {
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
+                   NULL, &rdev, 8);
+
+       }
+       if (obj_type == DMU_OT_ZNODE) {
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
+                   NULL, &pflags, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
+                   &acl_ids->z_fuid, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
+                   &acl_ids->z_fgid, 8);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
+                   sizeof (uint64_t) * 4);
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
+                   &acl_phys, sizeof (zfs_acl_phys_t));
+       } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
+                   &acl_ids->z_aclp->z_acl_count, 8);
+               locate.cb_aclp = acl_ids->z_aclp;
+               SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
+                   zfs_acl_data_locator, &locate,
+                   acl_ids->z_aclp->z_acl_bytes);
+               mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
+                   acl_ids->z_fuid, acl_ids->z_fgid);
+       }
+
+       VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
+
        if (!(flag & IS_ROOT_NODE)) {
-               ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
-               *zpp = zfs_znode_alloc(zfsvfs, db, 0);
-               ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
+               *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
+               ASSERT(*zpp != NULL);
        } else {
                /*
                 * If we are creating the root node, the "parent" we
                 * passed in is the znode for the root.
                 */
                *zpp = dzp;
+
+               (*zpp)->z_sa_hdl = sa_hdl;
        }
-       pzp->zp_uid = acl_ids->z_fuid;
-       pzp->zp_gid = acl_ids->z_fgid;
-       pzp->zp_mode = acl_ids->z_mode;
-       VERIFY(0 == zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
+
+       (*zpp)->z_pflags = pflags;
+       (*zpp)->z_mode = mode;
+
        if (vap->va_mask & AT_XVATTR)
-               zfs_xvattr_set(*zpp, (xvattr_t *)vap);
+               zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
+
+       if (obj_type == DMU_OT_ZNODE ||
+           acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
+               err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
+               ASSERT3S(err, ==, 0);
+       }
+       kmem_free(sa_attrs, sizeof(sa_bulk_attr_t) * ZPL_END);
+       ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
 }
 
+/*
+ * zfs_xvattr_set only updates the in-core attributes
+ * it is assumed the caller will be doing an sa_bulk_update
+ * to push the changes out
+ */
 void
-zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
+zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
 {
+#ifdef HAVE_XVATTR
        xoptattr_t *xoap;
 
        xoap = xva_getxoptattr(xvap);
        ASSERT(xoap);
 
        if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
-               ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
+               uint64_t times[2];
+               ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
+               (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
+                   &times, sizeof (times), tx);
                XVA_SET_RTN(xvap, XAT_CREATETIME);
        }
        if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
-               ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
+               ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_READONLY);
        }
        if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
-               ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
+               ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_HIDDEN);
        }
        if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
-               ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
+               ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_SYSTEM);
        }
        if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
-               ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
+               ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_ARCHIVE);
        }
        if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
-               ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
+               ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_IMMUTABLE);
        }
        if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
-               ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
+               ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_NOUNLINK);
        }
        if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
-               ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
+               ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_APPENDONLY);
        }
        if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
-               ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
+               ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_NODUMP);
        }
        if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
-               ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
+               ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_OPAQUE);
        }
        if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
                ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
-                   xoap->xoa_av_quarantined);
+                   xoap->xoa_av_quarantined, zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
        }
        if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
-               ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
+               ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
+                   zp->z_pflags, tx);
                XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
        }
        if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
-               (void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
-                   sizeof (xoap->xoa_av_scanstamp));
-               zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
+               zfs_sa_set_scanstamp(zp, xvap, tx);
                XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
        }
+       if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
+               ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
+                   zp->z_pflags, tx);
+               XVA_SET_RTN(xvap, XAT_REPARSE);
+       }
+       if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
+               ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
+                   zp->z_pflags, tx);
+               XVA_SET_RTN(xvap, XAT_OFFLINE);
+       }
+       if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
+               ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
+                   zp->z_pflags, tx);
+               XVA_SET_RTN(xvap, XAT_SPARSE);
+       }
+#endif /* HAVE_XVATTR */
 }
 
 int
@@ -920,35 +928,42 @@ zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
        dmu_buf_t       *db;
        znode_t         *zp;
        int err;
+       sa_handle_t     *hdl;
 
        *zpp = NULL;
 
        ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
 
-       err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
+       err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
        if (err) {
                ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
                return (err);
        }
 
        dmu_object_info_from_db(db, &doi);
-       if (doi.doi_bonus_type != DMU_OT_ZNODE ||
-           doi.doi_bonus_size < sizeof (znode_phys_t)) {
-               dmu_buf_rele(db, NULL);
+       if (doi.doi_bonus_type != DMU_OT_SA &&
+           (doi.doi_bonus_type != DMU_OT_ZNODE ||
+           (doi.doi_bonus_type == DMU_OT_ZNODE &&
+           doi.doi_bonus_size < sizeof (znode_phys_t)))) {
+               sa_buf_rele(db, NULL);
                ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
                return (EINVAL);
        }
 
-       zp = dmu_buf_get_user(db);
-       if (zp != NULL) {
-               mutex_enter(&zp->z_lock);
+       hdl = dmu_buf_get_user(db);
+       if (hdl != NULL) {
+               zp  = sa_get_userdata(hdl);
+
 
                /*
-                * Since we do immediate eviction of the z_dbuf, we
-                * should never find a dbuf with a znode that doesn't
-                * know about the dbuf.
+                * Since "SA" does immediate eviction we
+                * should never find a sa handle that doesn't
+                * know about the znode.
                 */
-               ASSERT3P(zp->z_dbuf, ==, db);
+
+               ASSERT3P(zp, !=, NULL);
+
+               mutex_enter(&zp->z_lock);
                ASSERT3U(zp->z_id, ==, obj_num);
                if (zp->z_unlinked) {
                        err = ENOENT;
@@ -957,7 +972,7 @@ zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
                        *zpp = zp;
                        err = 0;
                }
-               dmu_buf_rele(db, NULL);
+               sa_buf_rele(db, NULL);
                mutex_exit(&zp->z_lock);
                ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
                return (err);
@@ -965,11 +980,24 @@ zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
 
        /*
         * Not found create new znode/vnode
+        * but only if file exists.
+        *
+        * There is a small window where zfs_vget() could
+        * find this object while a file create is still in
+        * progress.  This is checked for in zfs_znode_alloc()
+        *
+        * if zfs_znode_alloc() fails it will drop the hold on the
+        * bonus buffer.
         */
-       zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
+       zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
+           doi.doi_bonus_type, NULL);
+       if (zp == NULL) {
+               err = ENOENT;
+       } else {
+               *zpp = zp;
+       }
        ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
-       *zpp = zp;
-       return (0);
+       return (err);
 }
 
 int
@@ -979,32 +1007,73 @@ zfs_rezget(znode_t *zp)
        dmu_object_info_t doi;
        dmu_buf_t *db;
        uint64_t obj_num = zp->z_id;
+       uint64_t mode;
+       sa_bulk_attr_t bulk[8];
        int err;
+       int count = 0;
+       uint64_t gen;
 
        ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
 
-       err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
+       mutex_enter(&zp->z_acl_lock);
+       if (zp->z_acl_cached) {
+               zfs_acl_free(zp->z_acl_cached);
+               zp->z_acl_cached = NULL;
+       }
+
+       mutex_exit(&zp->z_acl_lock);
+       ASSERT(zp->z_sa_hdl == NULL);
+       err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
        if (err) {
                ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
                return (err);
        }
 
        dmu_object_info_from_db(db, &doi);
-       if (doi.doi_bonus_type != DMU_OT_ZNODE ||
-           doi.doi_bonus_size < sizeof (znode_phys_t)) {
-               dmu_buf_rele(db, NULL);
+       if (doi.doi_bonus_type != DMU_OT_SA &&
+           (doi.doi_bonus_type != DMU_OT_ZNODE ||
+           (doi.doi_bonus_type == DMU_OT_ZNODE &&
+           doi.doi_bonus_size < sizeof (znode_phys_t)))) {
+               sa_buf_rele(db, NULL);
                ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
                return (EINVAL);
        }
 
-       if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
-               dmu_buf_rele(db, NULL);
+       zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
+
+       /* reload cached values */
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
+           &gen, sizeof (gen));
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
+           &zp->z_size, sizeof (zp->z_size));
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
+           &zp->z_links, sizeof (zp->z_links));
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
+           &zp->z_pflags, sizeof (zp->z_pflags));
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
+           &zp->z_atime, sizeof (zp->z_atime));
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
+           &zp->z_uid, sizeof (zp->z_uid));
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
+           &zp->z_gid, sizeof (zp->z_gid));
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
+           &mode, sizeof (mode));
+
+       if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
+               zfs_znode_dmu_fini(zp);
                ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
                return (EIO);
        }
 
-       zfs_znode_dmu_init(zfsvfs, zp, db);
-       zp->z_unlinked = (zp->z_phys->zp_links == 0);
+       zp->z_mode = mode;
+
+       if (gen != zp->z_gen) {
+               zfs_znode_dmu_fini(zp);
+               ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
+               return (EIO);
+       }
+
+       zp->z_unlinked = (zp->z_links == 0);
        zp->z_blksz = doi.doi_data_block_size;
 
        ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
@@ -1018,11 +1087,13 @@ zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
        zfsvfs_t *zfsvfs = zp->z_zfsvfs;
        objset_t *os = zfsvfs->z_os;
        uint64_t obj = zp->z_id;
-       uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
+       uint64_t acl_obj = zfs_external_acl(zp);
 
        ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
-       if (acl_obj)
+       if (acl_obj) {
+               VERIFY(!zp->z_is_sa);
                VERIFY(0 == dmu_object_free(os, acl_obj, tx));
+       }
        VERIFY(0 == dmu_object_free(os, obj, tx));
        zfs_znode_dmu_fini(zp);
        ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
@@ -1036,7 +1107,7 @@ zfs_zinactive(znode_t *zp)
        zfsvfs_t *zfsvfs = zp->z_zfsvfs;
        uint64_t z_id = zp->z_id;
 
-       ASSERT(zp->z_dbuf && zp->z_phys);
+       ASSERT(zp->z_sa_hdl);
 
        /*
         * Don't allow a zfs_zget() while were trying to release this znode
@@ -1075,6 +1146,7 @@ zfs_zinactive(znode_t *zp)
                zfs_rmnode(zp);
                return;
        }
+
        mutex_exit(&zp->z_lock);
        zfs_znode_dmu_fini(zp);
        ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
@@ -1106,59 +1178,40 @@ zfs_znode_free(znode_t *zp)
 }
 
 void
-zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
+zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
+    uint64_t ctime[2], boolean_t have_tx)
 {
        timestruc_t     now;
 
-       ASSERT(MUTEX_HELD(&zp->z_lock));
-
        gethrestime(&now);
 
-       if (tx) {
-               dmu_buf_will_dirty(zp->z_dbuf, tx);
+       if (have_tx) {  /* will sa_bulk_update happen really soon? */
                zp->z_atime_dirty = 0;
                zp->z_seq++;
        } else {
                zp->z_atime_dirty = 1;
        }
 
-       if (flag & AT_ATIME)
-               ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
+       if (flag & AT_ATIME) {
+               ZFS_TIME_ENCODE(&now, zp->z_atime);
+       }
 
        if (flag & AT_MTIME) {
-               ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
-               if (zp->z_zfsvfs->z_use_fuids)
-                       zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
+               ZFS_TIME_ENCODE(&now, mtime);
+               if (zp->z_zfsvfs->z_use_fuids) {
+                       zp->z_pflags |= (ZFS_ARCHIVE |
+                           ZFS_AV_MODIFIED);
+               }
        }
 
        if (flag & AT_CTIME) {
-               ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
+               ZFS_TIME_ENCODE(&now, ctime);
                if (zp->z_zfsvfs->z_use_fuids)
-                       zp->z_phys->zp_flags |= ZFS_ARCHIVE;
+                       zp->z_pflags |= ZFS_ARCHIVE;
        }
 }
 
 /*
- * Update the requested znode timestamps with the current time.
- * If we are in a transaction, then go ahead and mark the znode
- * dirty in the transaction so the timestamps will go to disk.
- * Otherwise, we will get pushed next time the znode is updated
- * in a transaction, or when this znode eventually goes inactive.
- *
- * Why is this OK?
- *  1 - Only the ACCESS time is ever updated outside of a transaction.
- *  2 - Multiple consecutive updates will be collapsed into a single
- *     znode update by the transaction grouping semantics of the DMU.
- */
-void
-zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
-{
-       mutex_enter(&zp->z_lock);
-       zfs_time_stamper_locked(zp, flag, tx);
-       mutex_exit(&zp->z_lock);
-}
-
-/*
  * Grow the block size for a file.
  *
  *     IN:     zp      - znode of file to free data in.
@@ -1180,19 +1233,21 @@ zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
         * we will not grow.  If there is more than one block in a file,
         * the blocksize cannot change.
         */
-       if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
+       if (zp->z_blksz && zp->z_size > zp->z_blksz)
                return;
 
        error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
            size, 0, tx);
+
        if (error == ENOTSUP)
                return;
        ASSERT3U(error, ==, 0);
 
        /* What blocksize did we actually get? */
-       dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
+       dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
 }
 
+#ifdef HAVE_ZPL
 /*
  * This is a dummy interface used when pvn_vplist_dirty() should *not*
  * be calling back into the fs for a putpage().  E.g.: when truncating
@@ -1206,6 +1261,7 @@ zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
        ASSERT(0);
        return (0);
 }
+#endif /* HAVE_ZPL */
 
 /*
  * Increase the file length
@@ -1233,13 +1289,14 @@ zfs_extend(znode_t *zp, uint64_t end)
        /*
         * Nothing to do if file already at desired length.
         */
-       if (end <= zp->z_phys->zp_size) {
+       if (end <= zp->z_size) {
                zfs_range_unlock(rl);
                return (0);
        }
 top:
        tx = dmu_tx_create(zfsvfs->z_os);
-       dmu_tx_hold_bonus(tx, zp->z_id);
+       dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
+       zfs_sa_upgrade_txholds(tx, zp);
        if (end > zp->z_blksz &&
            (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
                /*
@@ -1267,12 +1324,14 @@ top:
                zfs_range_unlock(rl);
                return (error);
        }
-       dmu_buf_will_dirty(zp->z_dbuf, tx);
 
        if (newblksz)
                zfs_grow_blocksize(zp, newblksz, tx);
 
-       zp->z_phys->zp_size = end;
+       zp->z_size = end;
+
+       VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
+           &zp->z_size, sizeof (zp->z_size), tx));
 
        zfs_range_unlock(rl);
 
@@ -1306,13 +1365,13 @@ zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
        /*
         * Nothing to do if file already at desired length.
         */
-       if (off >= zp->z_phys->zp_size) {
+       if (off >= zp->z_size) {
                zfs_range_unlock(rl);
                return (0);
        }
 
-       if (off + len > zp->z_phys->zp_size)
-               len = zp->z_phys->zp_size - off;
+       if (off + len > zp->z_size)
+               len = zp->z_size - off;
 
        error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
 
@@ -1334,10 +1393,14 @@ static int
 zfs_trunc(znode_t *zp, uint64_t end)
 {
        zfsvfs_t *zfsvfs = zp->z_zfsvfs;
+#ifdef HAVE_ZPL
        vnode_t *vp = ZTOV(zp);
+#endif /* HAVE_ZPL */
        dmu_tx_t *tx;
        rl_t *rl;
        int error;
+       sa_bulk_attr_t bulk[2];
+       int count = 0;
 
        /*
         * We will change zp_size, lock the whole file.
@@ -1347,7 +1410,7 @@ zfs_trunc(znode_t *zp, uint64_t end)
        /*
         * Nothing to do if file already at desired length.
         */
-       if (end >= zp->z_phys->zp_size) {
+       if (end >= zp->z_size) {
                zfs_range_unlock(rl);
                return (0);
        }
@@ -1359,7 +1422,8 @@ zfs_trunc(znode_t *zp, uint64_t end)
        }
 top:
        tx = dmu_tx_create(zfsvfs->z_os);
-       dmu_tx_hold_bonus(tx, zp->z_id);
+       dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
+       zfs_sa_upgrade_txholds(tx, zp);
        error = dmu_tx_assign(tx, TXG_NOWAIT);
        if (error) {
                if (error == ERESTART) {
@@ -1371,12 +1435,21 @@ top:
                zfs_range_unlock(rl);
                return (error);
        }
-       dmu_buf_will_dirty(zp->z_dbuf, tx);
 
-       zp->z_phys->zp_size = end;
+       zp->z_size = end;
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
+           NULL, &zp->z_size, sizeof (zp->z_size));
+
+       if (end == 0) {
+               zp->z_pflags &= ~ZFS_SPARSE;
+               SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
+                   NULL, &zp->z_pflags, 8);
+       }
+       VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
 
        dmu_tx_commit(tx);
 
+#ifdef HAVE_ZPL
        /*
         * Clear any mapped pages in the truncated region.  This has to
         * happen outside of the transaction to avoid the possibility of
@@ -1400,6 +1473,7 @@ top:
                    B_INVAL | B_TRUNC, NULL);
                ASSERT(error == 0);
        }
+#endif /* HAVE_ZPL */
 
        zfs_range_unlock(rl);
 
@@ -1421,13 +1495,23 @@ top:
 int
 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
 {
+#ifdef HAVE_ZPL
        vnode_t *vp = ZTOV(zp);
+#endif /* HAVE_ZPL */
        dmu_tx_t *tx;
        zfsvfs_t *zfsvfs = zp->z_zfsvfs;
        zilog_t *zilog = zfsvfs->z_log;
+       uint64_t mode;
+       uint64_t mtime[2], ctime[2];
+       sa_bulk_attr_t bulk[3];
+       int count = 0;
        int error;
 
-       if (off > zp->z_phys->zp_size) {
+       if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
+           sizeof (mode))) != 0)
+               return (error);
+
+       if (off > zp->z_size) {
                error =  zfs_extend(zp, off+len);
                if (error == 0 && log)
                        goto log;
@@ -1435,27 +1519,31 @@ zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
                        return (error);
        }
 
+#ifdef HAVE_ZPL
        /*
         * Check for any locks in the region to be freed.
         */
-       if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
-               uint64_t length = (len ? len : zp->z_phys->zp_size - off);
+
+       if (MANDLOCK(vp, (mode_t)mode)) {
+               uint64_t length = (len ? len : zp->z_size - off);
                if (error = chklock(vp, FWRITE, off, length, flag, NULL))
                        return (error);
        }
+#endif /* HAVE_ZPL */
 
        if (len == 0) {
                error = zfs_trunc(zp, off);
        } else {
                if ((error = zfs_free_range(zp, off, len)) == 0 &&
-                   off + len > zp->z_phys->zp_size)
+                   off + len > zp->z_size)
                        error = zfs_extend(zp, off+len);
        }
        if (error || !log)
                return (error);
 log:
        tx = dmu_tx_create(zfsvfs->z_os);
-       dmu_tx_hold_bonus(tx, zp->z_id);
+       dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
+       zfs_sa_upgrade_txholds(tx, zp);
        error = dmu_tx_assign(tx, TXG_NOWAIT);
        if (error) {
                if (error == ERESTART) {
@@ -1467,7 +1555,14 @@ log:
                return (error);
        }
 
-       zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
+       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
+           NULL, &zp->z_pflags, 8);
+       zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
+       error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
+       ASSERT(error == 0);
+
        zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
 
        dmu_tx_commit(tx);
@@ -1477,17 +1572,24 @@ log:
 void
 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
 {
-       zfsvfs_t        zfsvfs;
-       uint64_t        moid, obj, version;
+       uint64_t        moid, obj, sa_obj, version;
        uint64_t        sense = ZFS_CASE_SENSITIVE;
        uint64_t        norm = 0;
        nvpair_t        *elem;
        int             error;
+#ifdef HAVE_ZPL
+       zfsvfs_t        zfsvfs;
+       int             i;
        znode_t         *rootzp = NULL;
        vnode_t         *vp;
        vattr_t         vattr;
        znode_t         *zp;
        zfs_acl_ids_t   acl_ids;
+#else
+       timestruc_t     now;
+       dmu_buf_t       *db;
+       znode_phys_t    *pzp;
+#endif /* HAVE_ZPL */
 
        /*
         * First attempt to create master node.
@@ -1504,12 +1606,7 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
        /*
         * Set starting attributes.
         */
-       if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_USERSPACE)
-               version = ZPL_VERSION;
-       else if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
-               version = ZPL_VERSION_USERSPACE - 1;
-       else
-               version = ZPL_VERSION_FUID - 1;
+       version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
        elem = NULL;
        while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
                /* For the moment we expect all zpl props to be uint64_ts */
@@ -1535,6 +1632,18 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
        error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
 
        /*
+        * Create zap object used for SA attribute registration
+        */
+
+       if (version >= ZPL_VERSION_SA) {
+               sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
+                   DMU_OT_NONE, 0, tx);
+               error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
+               ASSERT(error == 0);
+       } else {
+               sa_obj = 0;
+       }
+       /*
         * Create a delete queue.
         */
        obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
@@ -1542,6 +1651,7 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
        error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
        ASSERT(error == 0);
 
+#ifdef HAVE_ZPL
        /*
         * Create root znode.  Create minimal znode/vnode/zfsvfs
         * to allow zfs_mknode to work.
@@ -1553,8 +1663,11 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
        vattr.va_gid = crgetgid(cr);
 
        rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
+       ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
+       rootzp->z_moved = 0;
        rootzp->z_unlinked = 0;
        rootzp->z_atime_dirty = 0;
+       rootzp->z_is_sa = USE_SA(version, os);
 
        vp = ZTOV(rootzp);
        vn_reinit(vp);
@@ -1566,7 +1679,14 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
        zfsvfs.z_parent = &zfsvfs;
        zfsvfs.z_version = version;
        zfsvfs.z_use_fuids = USE_FUIDS(version, os);
+       zfsvfs.z_use_sa = USE_SA(version, os);
        zfsvfs.z_norm = norm;
+
+       error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
+           &zfsvfs.z_attr_table);
+
+       ASSERT(error == 0);
+
        /*
         * Fold case on file systems that are always or sometimes case
         * insensitive.
@@ -1574,15 +1694,19 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
        if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
                zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
 
+       /* XXX - This must be destroyed but I'm not quite sure yet so
+        * I'm just annotating that fact when it's an issue.  -Brian */
        mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
        list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
            offsetof(znode_t, z_link_node));
 
-       ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
+       for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
+               mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
+
        rootzp->z_zfsvfs = &zfsvfs;
        VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
            cr, NULL, &acl_ids));
-       zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, &acl_ids);
+       zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
        ASSERT3P(zp, ==, rootzp);
        ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
        error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
@@ -1591,58 +1715,170 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
        POINTER_INVALIDATE(&rootzp->z_zfsvfs);
 
        ZTOV(rootzp)->v_count = 0;
-       dmu_buf_rele(rootzp->z_dbuf, NULL);
-       rootzp->z_dbuf = NULL;
+       sa_handle_destroy(rootzp->z_sa_hdl);
        kmem_cache_free(znode_cache, rootzp);
+       error = zfs_create_share_dir(&zfsvfs, tx);
 
+       for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
+               mutex_destroy(&zfsvfs.z_hold_mtx[i]);
+#else
        /*
-        * Create shares directory
+        * Create root znode with code free of VFS dependencies
         */
+       obj = zap_create_norm(os, norm, DMU_OT_DIRECTORY_CONTENTS,
+                             DMU_OT_ZNODE, sizeof (znode_phys_t), tx);
 
-       error = zfs_create_share_dir(&zfsvfs, tx);
+       VERIFY(0 == dmu_bonus_hold(os, obj, FTAG, &db));
+       dmu_buf_will_dirty(db, tx);
+
+       /*
+        * Initialize the znode physical data to zero.
+        */
+       ASSERT(db->db_size >= sizeof (znode_phys_t));
+       bzero(db->db_data, db->db_size);
+       pzp = db->db_data;
+
+       if (USE_FUIDS(version, os))
+               pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
+
+       pzp->zp_size = 2; /* "." and ".." */
+       pzp->zp_links = 2;
+       pzp->zp_parent = obj;
+       pzp->zp_gen = dmu_tx_get_txg(tx);
+       pzp->zp_mode = S_IFDIR | 0755;
+       pzp->zp_flags = ZFS_ACL_TRIVIAL;
+
+       gethrestime(&now);
 
+       ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
+       ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
+       ZFS_TIME_ENCODE(&now, pzp->zp_atime);
+       ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
+
+       error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &obj, tx);
        ASSERT(error == 0);
+
+       dmu_buf_rele(db, FTAG);
+#endif /* HAVE_ZPL */
 }
 
 #endif /* _KERNEL */
+
+static int
+zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
+{
+       uint64_t sa_obj = 0;
+       int error;
+
+       error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
+       if (error != 0 && error != ENOENT)
+               return (error);
+
+       error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
+       return (error);
+}
+
+static int
+zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
+    dmu_buf_t **db)
+{
+       dmu_object_info_t doi;
+       int error;
+
+       if ((error = sa_buf_hold(osp, obj, FTAG, db)) != 0)
+               return (error);
+
+       dmu_object_info_from_db(*db, &doi);
+       if ((doi.doi_bonus_type != DMU_OT_SA &&
+           doi.doi_bonus_type != DMU_OT_ZNODE) ||
+           (doi.doi_bonus_type == DMU_OT_ZNODE &&
+           doi.doi_bonus_size < sizeof (znode_phys_t))) {
+               sa_buf_rele(*db, FTAG);
+               return (ENOTSUP);
+       }
+
+       error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
+       if (error != 0) {
+               sa_buf_rele(*db, FTAG);
+               return (error);
+       }
+
+       return (0);
+}
+
+void
+zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db)
+{
+       sa_handle_destroy(hdl);
+       sa_buf_rele(db, FTAG);
+}
+
 /*
  * Given an object number, return its parent object number and whether
  * or not the object is an extended attribute directory.
  */
 static int
-zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
+zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
+    int *is_xattrdir)
 {
-       dmu_buf_t *db;
-       dmu_object_info_t doi;
-       znode_phys_t *zp;
+       uint64_t parent;
+       uint64_t pflags;
+       uint64_t mode;
+       sa_bulk_attr_t bulk[3];
+       int count = 0;
        int error;
 
-       if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
-               return (error);
+       SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
+           &parent, sizeof (parent));
+       SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
+           &pflags, sizeof (pflags));
+       SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
+           &mode, sizeof (mode));
 
-       dmu_object_info_from_db(db, &doi);
-       if (doi.doi_bonus_type != DMU_OT_ZNODE ||
-           doi.doi_bonus_size < sizeof (znode_phys_t)) {
-               dmu_buf_rele(db, FTAG);
-               return (EINVAL);
-       }
+       if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
+               return (error);
 
-       zp = db->db_data;
-       *pobjp = zp->zp_parent;
-       *is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
-           S_ISDIR(zp->zp_mode);
-       dmu_buf_rele(db, FTAG);
+       *pobjp = parent;
+       *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
 
        return (0);
 }
 
-int
-zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
+/*
+ * Given an object number, return some zpl level statistics
+ */
+static int
+zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
+    zfs_stat_t *sb)
 {
+       sa_bulk_attr_t bulk[4];
+       int count = 0;
+
+       SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
+           &sb->zs_mode, sizeof (sb->zs_mode));
+       SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
+           &sb->zs_gen, sizeof (sb->zs_gen));
+       SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
+           &sb->zs_links, sizeof (sb->zs_links));
+       SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
+           &sb->zs_ctime, sizeof (sb->zs_ctime));
+
+       return (sa_bulk_lookup(hdl, bulk, count));
+}
+
+static int
+zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
+    sa_attr_type_t *sa_table, char *buf, int len)
+{
+       sa_handle_t *sa_hdl;
+       sa_handle_t *prevhdl = NULL;
+       dmu_buf_t *prevdb = NULL;
+       dmu_buf_t *sa_db = NULL;
        char *path = buf + len - 1;
        int error;
 
        *path = '\0';
+       sa_hdl = hdl;
 
        for (;;) {
                uint64_t pobj;
@@ -1650,7 +1886,10 @@ zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
                size_t complen;
                int is_xattrdir;
 
-               if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
+               if (prevdb)
+                       zfs_release_sa_handle(prevhdl, prevdb);
+
+               if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
                    &is_xattrdir)) != 0)
                        break;
 
@@ -1675,9 +1914,85 @@ zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
                ASSERT(path >= buf);
                bcopy(component, path, complen);
                obj = pobj;
+
+               if (sa_hdl != hdl) {
+                       prevhdl = sa_hdl;
+                       prevdb = sa_db;
+               }
+               error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db);
+               if (error != 0) {
+                       sa_hdl = prevhdl;
+                       sa_db = prevdb;
+                       break;
+               }
+       }
+
+       if (sa_hdl != NULL && sa_hdl != hdl) {
+               ASSERT(sa_db != NULL);
+               zfs_release_sa_handle(sa_hdl, sa_db);
        }
 
        if (error == 0)
                (void) memmove(buf, path, buf + len - path);
+
+       return (error);
+}
+
+int
+zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
+{
+       sa_attr_type_t *sa_table;
+       sa_handle_t *hdl;
+       dmu_buf_t *db;
+       int error;
+
+       error = zfs_sa_setup(osp, &sa_table);
+       if (error != 0)
+               return (error);
+
+       error = zfs_grab_sa_handle(osp, obj, &hdl, &db);
+       if (error != 0)
+               return (error);
+
+       error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
+
+       zfs_release_sa_handle(hdl, db);
+       return (error);
+}
+
+int
+zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
+    char *buf, int len)
+{
+       char *path = buf + len - 1;
+       sa_attr_type_t *sa_table;
+       sa_handle_t *hdl;
+       dmu_buf_t *db;
+       int error;
+
+       *path = '\0';
+
+       error = zfs_sa_setup(osp, &sa_table);
+       if (error != 0)
+               return (error);
+
+       error = zfs_grab_sa_handle(osp, obj, &hdl, &db);
+       if (error != 0)
+               return (error);
+
+       error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
+       if (error != 0) {
+               zfs_release_sa_handle(hdl, db);
+               return (error);
+       }
+
+       error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
+
+       zfs_release_sa_handle(hdl, db);
        return (error);
 }
+
+#if defined(_KERNEL) && defined(HAVE_SPL)
+EXPORT_SYMBOL(zfs_create_fs);
+EXPORT_SYMBOL(zfs_obj_to_path);
+#endif