Whamcloud - gitweb
LU-14513 osd: release o_guard before quota acquisition
[fs/lustre-release.git] / lustre / osd-zfs / osd_object.c
index 2d0ff07..464e0d6 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2014, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/osd-zfs/osd_object.c
  *
@@ -42,7 +37,6 @@
 
 #define DEBUG_SUBSYSTEM S_OSD
 
-#include <lustre_ver.h>
 #include <libcfs/libcfs.h>
 #include <obd_support.h>
 #include <lustre_net.h>
 #include <sys/txg.h>
 
 char *osd_obj_tag = "osd_object";
+static int osd_object_sync_delay_us = -1;
 
-static struct dt_object_operations osd_obj_ops;
-static struct lu_object_operations osd_lu_obj_ops;
-extern struct dt_body_operations osd_body_ops;
-static struct dt_object_operations osd_obj_otable_it_ops;
-
-extern struct kmem_cache *osd_object_kmem;
+static const struct dt_object_operations osd_obj_ops;
+static const struct lu_object_operations osd_lu_obj_ops;
+static const struct dt_object_operations osd_obj_otable_it_ops;
 
 static void
 osd_object_sa_fini(struct osd_object *obj)
@@ -90,10 +82,9 @@ osd_object_sa_init(struct osd_object *obj, struct osd_device *o)
        int rc;
 
        LASSERT(obj->oo_sa_hdl == NULL);
-       LASSERT(obj->oo_db != NULL);
+       LASSERT(obj->oo_dn != NULL);
 
-       rc = -sa_handle_get(o->od_os, obj->oo_db->db_object, obj,
-                           SA_HDL_PRIVATE, &obj->oo_sa_hdl);
+       rc = osd_sa_handle_get(obj);
        if (rc)
                return rc;
 
@@ -112,37 +103,45 @@ osd_object_sa_init(struct osd_object *obj, struct osd_device *o)
 /*
  * Add object to list of dirty objects in tx handle.
  */
-static void
-osd_object_sa_dirty_add(struct osd_object *obj, struct osd_thandle *oh)
+void osd_object_sa_dirty_add(struct osd_object *obj, struct osd_thandle *oh)
 {
        if (!list_empty(&obj->oo_sa_linkage))
                return;
 
-       down(&oh->ot_sa_lock);
        write_lock(&obj->oo_attr_lock);
        if (likely(list_empty(&obj->oo_sa_linkage)))
                list_add(&obj->oo_sa_linkage, &oh->ot_sa_list);
        write_unlock(&obj->oo_attr_lock);
-       up(&oh->ot_sa_lock);
 }
 
 /*
  * Release spill block dbuf hold for all dirty SAs.
  */
-void osd_object_sa_dirty_rele(struct osd_thandle *oh)
+void osd_object_sa_dirty_rele(const struct lu_env *env, struct osd_thandle *oh)
 {
        struct osd_object *obj;
 
-       down(&oh->ot_sa_lock);
        while (!list_empty(&oh->ot_sa_list)) {
                obj = list_entry(oh->ot_sa_list.next,
-                                    struct osd_object, oo_sa_linkage);
-               sa_spill_rele(obj->oo_sa_hdl);
+                                struct osd_object, oo_sa_linkage);
                write_lock(&obj->oo_attr_lock);
                list_del_init(&obj->oo_sa_linkage);
                write_unlock(&obj->oo_attr_lock);
+               if (obj->oo_late_xattr) {
+                       /*
+                        * take oo_guard to protect oo_sa_xattr buffer
+                        * from concurrent update by osd_xattr_set()
+                        */
+                       LASSERT(oh->ot_assigned != 0);
+                       down_write(&obj->oo_guard);
+                       if (obj->oo_late_attr_set)
+                               __osd_sa_attr_init(env, obj, oh);
+                       else if (obj->oo_late_xattr)
+                               __osd_sa_xattr_update(env, obj, oh);
+                       up_write(&obj->oo_guard);
+               }
+               sa_spill_rele(obj->oo_sa_hdl);
        }
-       up(&oh->ot_sa_lock);
 }
 
 /*
@@ -183,47 +182,62 @@ osd_object_sa_bulk_update(struct osd_object *obj, sa_bulk_attr_t *attrs,
 /*
  * Retrieve the attributes of a DMU object
  */
-int __osd_object_attr_get(const struct lu_env *env, struct osd_device *o,
-                         struct osd_object *obj, struct lu_attr *la)
+static int __osd_object_attr_get(const struct lu_env *env, struct osd_device *o,
+                                struct osd_object *obj, struct lu_attr *la)
 {
-       struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
-       sa_handle_t     *sa_hdl;
-       sa_bulk_attr_t  *bulk;
-       int              cnt = 0;
+       struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
+       sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
+       struct lustre_mdt_attrs *lma;
+       struct lu_buf buf;
+       int cnt = 0;
        int              rc;
        ENTRY;
 
-       LASSERT(obj->oo_db != NULL);
+       LASSERT(obj->oo_dn != NULL);
 
-       rc = -sa_handle_get(o->od_os, obj->oo_db->db_object, NULL,
-                           SA_HDL_PRIVATE, &sa_hdl);
-       if (rc)
-               RETURN(rc);
-
-       OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 9);
-       if (bulk == NULL)
-               GOTO(out_sa, rc = -ENOMEM);
-
-       la->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE | LA_TYPE |
-                       LA_SIZE | LA_UID | LA_GID | LA_FLAGS | LA_NLINK;
+       la->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_BTIME | LA_MODE |
+                       LA_TYPE | LA_SIZE | LA_UID | LA_GID | LA_FLAGS |
+                       LA_NLINK;
 
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(o), NULL, osa->atime, 16);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(o), NULL, osa->mtime, 16);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(o), NULL, osa->ctime, 16);
+       SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(o), NULL, osa->btime, 16);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(o), NULL, &osa->mode, 8);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(o), NULL, &osa->size, 8);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(o), NULL, &osa->nlink, 8);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(o), NULL, &osa->uid, 8);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(o), NULL, &osa->gid, 8);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(o), NULL, &osa->flags, 8);
+       LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
 
-       rc = -sa_bulk_lookup(sa_hdl, bulk, cnt);
+       rc = -sa_bulk_lookup(obj->oo_sa_hdl, bulk, cnt);
        if (rc)
-               GOTO(out_bulk, rc);
+               GOTO(out_sa, rc);
+
+#ifdef ZFS_PROJINHERIT
+       if (o->od_projectused_dn && osa->flags & ZFS_PROJID) {
+               rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PROJID(o),
+                               &osa->projid, 8);
+               if (rc)
+                       GOTO(out_sa, rc);
+
+               la->la_projid = osa->projid;
+               la->la_valid |= LA_PROJID;
+               obj->oo_with_projid = 1;
+       } else {
+               la->la_projid = ZFS_DEFAULT_PROJID;
+               la->la_valid &= ~LA_PROJID;
+       }
+#else
+       la->la_projid = 0;
+       la->la_valid &= ~LA_PROJID;
+#endif
 
        la->la_atime = osa->atime[0];
        la->la_mtime = osa->mtime[0];
        la->la_ctime = osa->ctime[0];
+       la->la_btime = osa->btime[0];
        la->la_mode = osa->mode;
        la->la_uid = osa->uid;
        la->la_gid = osa->gid;
@@ -231,43 +245,50 @@ int __osd_object_attr_get(const struct lu_env *env, struct osd_device *o,
        la->la_flags = attrs_zfs2fs(osa->flags);
        la->la_size = osa->size;
 
+       /* Try to get extra flags from LMA */
+       lma = (struct lustre_mdt_attrs *)osd_oti_get(env)->oti_buf;
+       buf.lb_buf = lma;
+       buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
+       down_read(&obj->oo_guard);
+       rc = osd_xattr_get_lma(env, obj, &buf);
+       if (!rc) {
+               lma->lma_incompat = le32_to_cpu(lma->lma_incompat);
+               obj->oo_lma_flags =
+                       lma_to_lustre_flags(lma->lma_incompat);
+       } else if (rc == -ENODATA ||
+                  !(S_ISDIR(la->la_mode) &&
+                    dt_object_exists(&obj->oo_dt))) {
+               rc = 0;
+       }
+       up_read(&obj->oo_guard);
+
        if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode)) {
-               rc = -sa_lookup(sa_hdl, SA_ZPL_RDEV(o), &osa->rdev, 8);
+               rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_RDEV(o), &osa->rdev, 8);
                if (rc)
-                       GOTO(out_bulk, rc);
+                       GOTO(out_sa, rc);
                la->la_rdev = osa->rdev;
                la->la_valid |= LA_RDEV;
        }
-out_bulk:
-       OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 9);
 out_sa:
-       sa_handle_destroy(sa_hdl);
 
        RETURN(rc);
 }
 
-int __osd_obj2dbuf(const struct lu_env *env, objset_t *os,
-                  uint64_t oid, dmu_buf_t **dbp)
+int __osd_obj2dnode(objset_t *os, uint64_t oid, dnode_t **dnp)
 {
-       dmu_object_info_t *doi = &osd_oti_get(env)->oti_doi;
+       dmu_buf_t *db;
+       dmu_buf_impl_t *dbi;
        int rc;
 
-       rc = -sa_buf_hold(os, oid, osd_obj_tag, dbp);
+       rc = -dmu_bonus_hold(os, oid, osd_obj_tag, &db);
        if (rc)
                return rc;
 
-       dmu_object_info_from_db(*dbp, doi);
-       if (unlikely (oid != DMU_USERUSED_OBJECT &&
-           oid != DMU_GROUPUSED_OBJECT && doi->doi_bonus_type != DMU_OT_SA)) {
-               sa_buf_rele(*dbp, osd_obj_tag);
-               *dbp = NULL;
-               return -EINVAL;
-       }
-
-       LASSERT(*dbp);
-       LASSERT((*dbp)->db_object == oid);
-       LASSERT((*dbp)->db_offset == -1);
-       LASSERT((*dbp)->db_data != NULL);
+       dbi = (dmu_buf_impl_t *)db;
+       DB_DNODE_ENTER(dbi);
+       *dnp = DB_DNODE(dbi);
+       DB_DNODE_EXIT(dbi);
+       LASSERT(*dnp != NULL);
 
        return 0;
 }
@@ -285,35 +306,111 @@ struct lu_object *osd_object_alloc(const struct lu_env *env,
        OBD_SLAB_ALLOC_PTR_GFP(mo, osd_object_kmem, GFP_NOFS);
        if (mo != NULL) {
                struct lu_object *l;
+               struct lu_object_header *h;
+               struct osd_device *o = osd_dev(d);
 
                l = &mo->oo_dt.do_lu;
-               dt_object_init(&mo->oo_dt, NULL, d);
+               if (unlikely(o->od_in_init)) {
+                       OBD_ALLOC_PTR(h);
+                       if (!h) {
+                               OBD_FREE_PTR(mo);
+                               return NULL;
+                       }
+
+                       lu_object_header_init(h);
+                       lu_object_init(l, h, d);
+                       lu_object_add_top(h, l);
+                       mo->oo_header = h;
+               } else {
+                       dt_object_init(&mo->oo_dt, NULL, d);
+                       mo->oo_header = NULL;
+               }
+
                mo->oo_dt.do_ops = &osd_obj_ops;
                l->lo_ops = &osd_lu_obj_ops;
                INIT_LIST_HEAD(&mo->oo_sa_linkage);
+               INIT_LIST_HEAD(&mo->oo_unlinked_linkage);
                init_rwsem(&mo->oo_sem);
-               sema_init(&mo->oo_guard, 1);
+               init_rwsem(&mo->oo_guard);
                rwlock_init(&mo->oo_attr_lock);
+               mo->oo_destroy = OSD_DESTROY_NONE;
                return l;
        } else {
                return NULL;
        }
 }
 
+static void osd_obj_set_blksize(const struct lu_env *env,
+                               struct osd_device *osd, struct osd_object *obj)
+{
+       const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
+       dmu_tx_t *tx;
+       dnode_t *dn = obj->oo_dn;
+       uint32_t blksz;
+       int rc = 0;
+       ENTRY;
+
+       LASSERT(!osd_oti_get(env)->oti_in_trans);
+
+       tx = dmu_tx_create(osd->od_os);
+       if (!tx) {
+               CERROR("%s: fail to create tx to set blksize for "DFID"\n",
+                      osd->od_svname, PFID(fid));
+               RETURN_EXIT;
+       }
+
+       dmu_tx_hold_bonus(tx, dn->dn_object);
+       rc = -dmu_tx_assign(tx, TXG_WAIT);
+       if (rc) {
+               dmu_tx_abort(tx);
+               CERROR("%s: fail to assign tx to set blksize for "DFID
+                      ": rc = %d\n", osd->od_svname, PFID(fid), rc);
+               RETURN_EXIT;
+       }
+
+       down_write(&obj->oo_guard);
+       if (unlikely((1 << dn->dn_datablkshift) >= PAGE_SIZE))
+               GOTO(out, rc = 1);
+
+       blksz = dn->dn_datablksz;
+       if (!is_power_of_2(blksz))
+               blksz = size_roundup_power2(blksz);
+
+       if (blksz > osd->od_max_blksz)
+               blksz = osd->od_max_blksz;
+       else if (blksz < PAGE_SIZE)
+               blksz = PAGE_SIZE;
+       rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object, blksz, 0, tx);
+
+       GOTO(out, rc);
+
+out:
+       up_write(&obj->oo_guard);
+       if (rc) {
+               dmu_tx_abort(tx);
+               if (unlikely(obj->oo_dn->dn_maxblkid > 0))
+                       rc = 1;
+               if (rc < 0)
+                       CERROR("%s: fail to set blksize for "DFID": rc = %d\n",
+                              osd->od_svname, PFID(fid), rc);
+       } else {
+               dmu_tx_commit(tx);
+               CDEBUG(D_INODE, "%s: set blksize as %u for "DFID"\n",
+                      osd->od_svname, blksz, PFID(fid));
+       }
+}
+
 /*
  * Concurrency: shouldn't matter.
  */
-int osd_object_init0(const struct lu_env *env, struct osd_object *obj)
+static int osd_object_init0(const struct lu_env *env, struct osd_object *obj)
 {
        struct osd_device       *osd = osd_obj2dev(obj);
-       const struct lu_fid     *fid  = lu_object_fid(&obj->oo_dt.do_lu);
+       const struct lu_fid     *fid = lu_object_fid(&obj->oo_dt.do_lu);
        int                      rc = 0;
        ENTRY;
 
-       if (obj->oo_db == NULL)
-               RETURN(0);
-
-       /* object exist */
+       LASSERT(obj->oo_dn);
 
        rc = osd_object_sa_init(obj, osd);
        if (rc)
@@ -324,10 +421,19 @@ int osd_object_init0(const struct lu_env *env, struct osd_object *obj)
        if (rc)
                RETURN(rc);
 
-       if (likely(!fid_is_acct(fid)))
+       if (likely(!fid_is_acct(fid))) {
                /* no body operations for accounting objects */
                obj->oo_dt.do_body_ops = &osd_body_ops;
 
+               if (S_ISREG(obj->oo_attr.la_mode) &&
+                   obj->oo_dn->dn_maxblkid == 0 &&
+                   (1 << obj->oo_dn->dn_datablkshift) < PAGE_SIZE &&
+                   (fid_is_idif(fid) || fid_is_norm(fid) ||
+                    fid_is_echo(fid)) &&
+                   osd->od_is_ost && !osd->od_dt_dev.dd_rdonly)
+                       osd_obj_set_blksize(env, osd, obj);
+       }
+
        /*
         * initialize object before marking it existing
         */
@@ -345,14 +451,15 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
        struct lu_buf           buf;
        int                     rc;
        struct lustre_mdt_attrs *lma;
+       const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
        ENTRY;
 
-       CLASSERT(sizeof(info->oti_buf) >= sizeof(*lma));
+       BUILD_BUG_ON(sizeof(info->oti_buf) < sizeof(*lma));
        lma = (struct lustre_mdt_attrs *)info->oti_buf;
        buf.lb_buf = lma;
        buf.lb_len = sizeof(info->oti_buf);
 
-       rc = osd_xattr_get(env, &obj->oo_dt, &buf, XATTR_NAME_LMA, BYPASS_CAPA);
+       rc = osd_xattr_get(env, &obj->oo_dt, &buf, XATTR_NAME_LMA);
        if (rc > 0) {
                rc = 0;
                lustre_lma_swab(lma);
@@ -361,8 +468,23 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
                        CWARN("%s: unsupported incompat LMA feature(s) %#x for "
                              "fid = "DFID"\n", osd_obj2dev(obj)->od_svname,
                              lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
-                             PFID(lu_object_fid(&obj->oo_dt.do_lu)));
+                             PFID(rfid));
                        rc = -EOPNOTSUPP;
+               } else if (unlikely(!lu_fid_eq(rfid, &lma->lma_self_fid))) {
+                       CERROR("%s: FID-in-LMA "DFID" does not match the "
+                             "object self-fid "DFID"\n",
+                             osd_obj2dev(obj)->od_svname,
+                             PFID(&lma->lma_self_fid), PFID(rfid));
+                       rc = -EREMCHG;
+               } else {
+                       struct osd_device *osd = osd_obj2dev(obj);
+
+                       if (lma->lma_compat & LMAC_STRIPE_INFO &&
+                           osd->od_is_ost)
+                               obj->oo_pfid_in_lma = 1;
+                       if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
+                           osd->od_remote_parent_dir != ZFS_NO_OBJECT)
+                               lu_object_set_agent_entry(&obj->oo_dt.do_lu);
                }
        } else if (rc == -ENODATA) {
                /* haven't initialize LMA xattr */
@@ -372,6 +494,35 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
        RETURN(rc);
 }
 
+/**
+ * Helper function to retrieve DMU object id from fid for accounting object
+ */
+static dnode_t *osd_quota_fid2dmu(const struct osd_device *osd,
+                                 const struct lu_fid *fid)
+{
+       dnode_t *dn = NULL;
+
+       LASSERT(fid_is_acct(fid));
+
+       switch (fid_oid(fid)) {
+       case ACCT_USER_OID:
+               dn = osd->od_userused_dn;
+               break;
+       case ACCT_GROUP_OID:
+               dn = osd->od_groupused_dn;
+               break;
+#ifdef ZFS_PROJINHERIT
+       case ACCT_PROJECT_OID:
+               dn = osd->od_projectused_dn;
+               break;
+#endif
+       default:
+               break;
+       }
+
+       return dn;
+}
+
 /*
  * Concurrency: no concurrent access is possible that early in object
  * life-cycle.
@@ -379,10 +530,18 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
                           const struct lu_object_conf *conf)
 {
-       struct osd_object       *obj = osd_obj(l);
-       struct osd_device       *osd = osd_obj2dev(obj);
-       uint64_t                 oid;
-       int                      rc;
+       struct osd_object *obj = osd_obj(l);
+       struct osd_device *osd = osd_obj2dev(obj);
+       const struct lu_fid *fid = lu_object_fid(l);
+       struct lustre_scrub *scrub = &osd->od_scrub;
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct luz_direntry *zde = &info->oti_zde;
+       struct osd_idmap_cache *idc;
+       char *name = info->oti_str;
+       uint64_t oid;
+       int rc = 0;
+       int rc1;
+       bool remote = false;
        ENTRY;
 
        LASSERT(osd_invariant(obj));
@@ -390,30 +549,138 @@ static int osd_object_init(const struct lu_env *env, struct lu_object *l,
        if (fid_is_otable_it(&l->lo_header->loh_fid)) {
                obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
                l->lo_header->loh_attr |= LOHA_EXISTS;
-               RETURN(0);
+
+               GOTO(out, rc = 0);
        }
 
-       rc = osd_fid_lookup(env, osd, lu_object_fid(l), &oid);
-       if (rc == 0) {
-               LASSERT(obj->oo_db == NULL);
-               rc = __osd_obj2dbuf(env, osd->od_os, oid, &obj->oo_db);
-               if (rc != 0) {
-                       CERROR("%s: lookup "DFID"/"LPX64" failed: rc = %d\n",
-                              osd->od_svname, PFID(lu_object_fid(l)), oid, rc);
-                       GOTO(out, rc);
+       if (conf && conf->loc_flags & LOC_F_NEW)
+               GOTO(out, rc = 0);
+
+       if (unlikely(fid_is_acct(fid))) {
+               obj->oo_dn = osd_quota_fid2dmu(osd, fid);
+               if (obj->oo_dn) {
+                       obj->oo_dt.do_index_ops = &osd_acct_index_ops;
+                       l->lo_header->loh_attr |= LOHA_EXISTS;
                }
-               LASSERT(obj->oo_db);
-               rc = osd_object_init0(env, obj);
-               if (rc != 0)
-                       GOTO(out, rc);
 
-               rc = osd_check_lma(env, obj);
-               if (rc != 0)
-                       GOTO(out, rc);
-       } else if (rc == -ENOENT) {
-               rc = 0;
+               GOTO(out, rc = 0);
        }
-       LASSERT(osd_invariant(obj));
+
+       idc = osd_idc_find(env, osd, fid);
+       if (idc && !idc->oic_remote && idc->oic_dnode != ZFS_NO_OBJECT) {
+               oid = idc->oic_dnode;
+               goto zget;
+       }
+
+       rc = -ENOENT;
+       if (!list_empty(&osd->od_scrub.os_inconsistent_items))
+               rc = osd_oii_lookup(osd, fid, &oid);
+
+       if (rc)
+               rc = osd_fid_lookup(env, osd, fid, &oid);
+
+       if (rc == -ENOENT) {
+               if (likely(!(fid_is_norm(fid) || fid_is_igif(fid)) ||
+                          fid_is_on_ost(env, osd, fid) ||
+                          !zfs_test_bit(osd_oi_fid2idx(osd, fid),
+                                        scrub->os_file.sf_oi_bitmap)))
+                       GOTO(out, rc = 0);
+
+               rc = -EREMCHG;
+               goto trigger;
+       }
+
+       if (rc)
+               GOTO(out, rc);
+
+zget:
+       LASSERT(obj->oo_dn == NULL);
+
+       rc = __osd_obj2dnode(osd->od_os, oid, &obj->oo_dn);
+       /* EEXIST will be returned if object is being deleted in ZFS */
+       if (rc == -EEXIST)
+               GOTO(out, rc = 0);
+
+       if (rc) {
+               CERROR("%s: lookup "DFID"/%#llx failed: rc = %d\n",
+                      osd->od_svname, PFID(lu_object_fid(l)), oid, rc);
+               GOTO(out, rc);
+       }
+
+       rc = osd_object_init0(env, obj);
+       if (rc)
+               GOTO(out, rc);
+
+       if (unlikely(obj->oo_header))
+               GOTO(out, rc = 0);
+
+       rc = osd_check_lma(env, obj);
+       if (rc != -EREMCHG)
+               GOTO(out, rc);
+
+       osd_scrub_refresh_mapping(env, osd, fid, oid, DTO_INDEX_DELETE, true,
+                                 NULL);
+
+trigger:
+       /* We still have chance to get the valid dnode: for the object that is
+        * referenced by remote name entry, the object on the local MDT will be
+        * linked under the dir /REMOTE_PARENT_DIR with its FID string as name.
+        *
+        * During the OI scrub, if we cannot find the OI mapping, we may still
+        * have change to map the FID to local OID via lookup the dir
+        * /REMOTE_PARENT_DIR. */
+       if (!remote && !fid_is_on_ost(env, osd, fid)) {
+               osd_fid2str(name, fid, sizeof(info->oti_str));
+               rc = osd_zap_lookup(osd, osd->od_remote_parent_dir,
+                                   NULL, name, 8, 3, (void *)zde);
+               if (!rc) {
+                       oid = zde->lzd_reg.zde_dnode;
+                       osd_dnode_rele(obj->oo_dn);
+                       obj->oo_dn = NULL;
+                       remote = true;
+                       goto zget;
+               }
+       }
+
+       /* The case someone triggered the OI scrub already. */
+       if (scrub->os_running) {
+               if (!rc) {
+                       LASSERT(remote);
+
+                       lu_object_set_agent_entry(l);
+                       osd_oii_insert(env, osd, fid, oid, false);
+               } else {
+                       rc = -EINPROGRESS;
+               }
+
+               GOTO(out, rc);
+       }
+
+       /* The case NOT allow to trigger OI scrub automatically. */
+       if (osd->od_auto_scrub_interval == AS_NEVER)
+               GOTO(out, rc);
+
+       /* It is me to trigger the OI scrub. */
+       rc1 = osd_scrub_start(env, osd, SS_CLEAR_DRYRUN |
+                             SS_CLEAR_FAILOUT | SS_AUTO_FULL);
+       CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
+                    "%s: trigger OI scrub by RPC for "DFID"/%#llx: rc = %d\n",
+                    osd_name(osd), PFID(fid), oid, rc1);
+       if (!rc) {
+               LASSERT(remote);
+
+               lu_object_set_agent_entry(l);
+               if (!rc1)
+                       osd_oii_insert(env, osd, fid, oid, false);
+       } else {
+               if (!rc1)
+                       rc = -EINPROGRESS;
+               else
+                       rc = -EREMCHG;
+       }
+
+       GOTO(out, rc);
+
 out:
        RETURN(rc);
 }
@@ -425,238 +692,226 @@ out:
 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
 {
        struct osd_object *obj = osd_obj(l);
+       struct lu_object_header *h = obj->oo_header;
 
        LASSERT(osd_invariant(obj));
 
        dt_object_fini(&obj->oo_dt);
+       /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
        OBD_SLAB_FREE_PTR(obj, osd_object_kmem);
+       if (unlikely(h))
+               lu_object_header_free(h);
 }
 
-static void __osd_declare_object_destroy(const struct lu_env *env,
-                                        struct osd_object *obj,
-                                        struct osd_thandle *oh)
+static int
+osd_object_unlinked_add(struct osd_object *obj, struct osd_thandle *oh)
 {
-       struct osd_device       *osd = osd_obj2dev(obj);
-       dmu_buf_t               *db = obj->oo_db;
-       zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
-       uint64_t                 oid = db->db_object, xid;
-       dmu_tx_t                *tx = oh->ot_tx;
-       zap_cursor_t            *zc;
-       int                      rc = 0;
+       int rc = -EBUSY;
 
-       dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
+       LASSERT(obj->oo_destroy == OSD_DESTROY_ASYNC);
 
-       /* zap holding xattrs */
-       if (obj->oo_xattr != ZFS_NO_OBJECT) {
-               oid = obj->oo_xattr;
-
-               dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
+       /* the object is supposed to be exclusively locked by
+        * the caller (osd_destroy()), while the transaction
+        * (oh) is per-thread and not shared */
+       if (likely(list_empty(&obj->oo_unlinked_linkage))) {
+               list_add(&obj->oo_unlinked_linkage, &oh->ot_unlinked_list);
+               rc = 0;
+       }
 
-               rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
-               if (rc)
-                       goto out;
+       return rc;
+}
 
-               while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
-                       BUG_ON(za->za_integer_length != sizeof(uint64_t));
-                       BUG_ON(za->za_num_integers != 1);
+/* Default to max data size covered by a level-1 indirect block */
+static unsigned long osd_sync_destroy_max_size =
+       1UL << (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT + SPA_MAXBLOCKSHIFT);
+module_param(osd_sync_destroy_max_size, ulong, 0444);
+MODULE_PARM_DESC(osd_sync_destroy_max_size, "Maximum object size to use synchronous destroy.");
 
-                       rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
-                                        sizeof(uint64_t), 1, &xid);
-                       if (rc) {
-                               CERROR("%s: xattr lookup failed: rc = %d\n",
-                                      osd->od_svname, rc);
-                               goto out_err;
-                       }
-                       dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
-
-                       zap_cursor_advance(zc);
-               }
-               if (rc == -ENOENT)
-                       rc = 0;
-out_err:
-               osd_zap_cursor_fini(zc);
+static inline void
+osd_object_set_destroy_type(struct osd_object *obj)
+{
+       /*
+        * Lock-less OST_WRITE can race with OST_DESTROY, so set destroy type
+        * only once and use it consistently thereafter.
+        */
+       down_write(&obj->oo_guard);
+       if (obj->oo_destroy == OSD_DESTROY_NONE) {
+               if (obj->oo_attr.la_size <= osd_sync_destroy_max_size)
+                       obj->oo_destroy = OSD_DESTROY_SYNC;
+               else /* Larger objects are destroyed asynchronously */
+                       obj->oo_destroy = OSD_DESTROY_ASYNC;
        }
-out:
-       if (rc && tx->tx_err == 0)
-               tx->tx_err = -rc;
+       up_write(&obj->oo_guard);
 }
 
-static int osd_declare_object_destroy(const struct lu_env *env,
-                                     struct dt_object *dt,
-                                     struct thandle *th)
+static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
+                              struct thandle *th)
 {
-       char                    *buf = osd_oti_get(env)->oti_str;
        const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_device       *osd = osd_obj2dev(obj);
        struct osd_thandle      *oh;
-       uint64_t                 zapid;
+       dnode_t *dn;
        int                      rc;
+       uint64_t                 zapid;
        ENTRY;
 
        LASSERT(th != NULL);
        LASSERT(dt_object_exists(dt));
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh->ot_tx != NULL);
 
-       /* declare that we'll destroy the object */
-       __osd_declare_object_destroy(env, obj, oh);
+       dmu_tx_mark_netfree(oh->ot_tx);
 
        /* declare that we'll remove object from fid-dnode mapping */
-       zapid = osd_get_name_n_idx(env, osd, fid, buf);
-       dmu_tx_hold_bonus(oh->ot_tx, zapid);
-       dmu_tx_hold_zap(oh->ot_tx, zapid, 0, buf);
+       zapid = osd_get_name_n_idx(env, osd, fid, NULL, 0, &dn);
+       osd_tx_hold_zap(oh->ot_tx, zapid, dn, FALSE, NULL);
 
-       /* declare that we'll remove object from inode accounting ZAPs */
-       dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
-       dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, 0, buf);
-       dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
-       dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, 0, buf);
+       osd_declare_xattrs_destroy(env, obj, oh);
 
        /* one less inode */
        rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
-                              obj->oo_attr.la_gid, -1, oh, false, NULL, false);
+                              obj->oo_attr.la_gid, obj->oo_attr.la_projid,
+                              -1, oh, NULL, OSD_QID_INODE);
        if (rc)
                RETURN(rc);
 
        /* data to be truncated */
        rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
-                              obj->oo_attr.la_gid, 0, oh, true, NULL, false);
-       RETURN(rc);
-}
-
-/*
- * Delete a DMU object
- *
- * The transaction passed to this routine must have
- * dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END) called
- * and then assigned to a transaction group.
- *
- * This will release db and set it to NULL to prevent further dbuf releases.
- */
-static int __osd_object_destroy(const struct lu_env *env,
-                               struct osd_object *obj,
-                               dmu_tx_t *tx, void *tag)
-{
-       struct osd_device       *osd = osd_obj2dev(obj);
-       uint64_t                 xid;
-       zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
-       zap_cursor_t            *zc;
-       int                      rc;
+                              obj->oo_attr.la_gid, obj->oo_attr.la_projid,
+                              0, oh, NULL, OSD_QID_BLK);
+       if (rc)
+               RETURN(rc);
 
-       /* Assert that the transaction has been assigned to a
-          transaction group. */
-       LASSERT(tx->tx_txg != 0);
+       osd_object_set_destroy_type(obj);
+       if (obj->oo_destroy == OSD_DESTROY_SYNC)
+               dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object,
+                                0, DMU_OBJECT_END);
+       else
+               osd_tx_hold_zap(oh->ot_tx, osd->od_unlinked->dn_object,
+                               osd->od_unlinked, TRUE, NULL);
 
-       /* zap holding xattrs */
-       if (obj->oo_xattr != ZFS_NO_OBJECT) {
-               rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
-               if (rc)
-                       return rc;
-               while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
-                       BUG_ON(za->za_integer_length != sizeof(uint64_t));
-                       BUG_ON(za->za_num_integers != 1);
-
-                       rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
-                                        sizeof(uint64_t), 1, &xid);
-                       if (rc) {
-                               CERROR("%s: lookup xattr %s failed: rc = %d\n",
-                                      osd->od_svname, za->za_name, rc);
-                               continue;
-                       }
-                       rc = -dmu_object_free(osd->od_os, xid, tx);
-                       if (rc)
-                               CERROR("%s: fetch xattr %s failed: rc = %d\n",
-                                      osd->od_svname, za->za_name, rc);
-                       zap_cursor_advance(zc);
-               }
-               osd_zap_cursor_fini(zc);
+       /* remove agent entry (if have) from remote parent */
+       if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
+               osd_tx_hold_zap(oh->ot_tx, osd->od_remote_parent_dir,
+                               NULL, FALSE, NULL);
 
-               rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
-               if (rc)
-                       CERROR("%s: freeing xattr failed: rc = %d\n",
-                              osd->od_svname, rc);
-       }
+       /* will help to find FID->ino when this object is being
+        * added to PENDING/ */
+       osd_idc_find_and_init(env, osd, obj);
 
-       return -dmu_object_free(osd->od_os, obj->oo_db->db_object, tx);
+       RETURN(0);
 }
 
-static int osd_object_destroy(const struct lu_env *env,
-                             struct dt_object *dt,
-                             struct thandle *th)
+static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
+                      struct thandle *th)
 {
-       char                    *buf = osd_oti_get(env)->oti_str;
+       struct osd_thread_info  *info = osd_oti_get(env);
+       char                    *buf = info->oti_str;
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_device       *osd = osd_obj2dev(obj);
        const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
        struct osd_thandle      *oh;
        int                      rc;
-       uint64_t                 zapid;
+       uint64_t                 oid, zapid;
+       dnode_t *zdn;
        ENTRY;
 
-       LASSERT(obj->oo_db != NULL);
-       LASSERT(dt_object_exists(dt));
-       LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
+       down_write(&obj->oo_guard);
+
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out, rc = -ENOENT);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       LASSERT(obj->oo_dn != NULL);
+
+       oh = container_of(th, struct osd_thandle, ot_super);
        LASSERT(oh != NULL);
        LASSERT(oh->ot_tx != NULL);
 
-       zapid = osd_get_name_n_idx(env, osd, fid, buf);
-
        /* remove obj ref from index dir (it depends) */
-       rc = -zap_remove(osd->od_os, zapid, buf, oh->ot_tx);
+       zapid = osd_get_name_n_idx(env, osd, fid, buf,
+                                  sizeof(info->oti_str), &zdn);
+       rc = osd_xattrs_destroy(env, obj, oh);
        if (rc) {
-               CERROR("%s: zap_remove() failed: rc = %d\n",
-                      osd->od_svname, rc);
+               CERROR("%s: cannot destroy xattrs for %s: rc = %d\n",
+                      osd->od_svname, buf, rc);
                GOTO(out, rc);
        }
 
-       /* Remove object from inode accounting. It is not fatal for the destroy
-        * operation if something goes wrong while updating accounting, but we
-        * still log an error message to notify the administrator */
-       rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
-                       obj->oo_attr.la_uid, -1, oh->ot_tx);
-       if (rc)
-               CERROR("%s: failed to remove "DFID" from accounting ZAP for usr"
-                       " %d: rc = %d\n", osd->od_svname, PFID(fid),
-                       obj->oo_attr.la_uid, rc);
-       rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
-                               obj->oo_attr.la_gid, -1, oh->ot_tx);
-       if (rc)
-               CERROR("%s: failed to remove "DFID" from accounting ZAP for grp"
-                       " %d: rc = %d\n", osd->od_svname, PFID(fid),
-                       obj->oo_attr.la_gid, rc);
+       if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
+               rc = osd_delete_from_remote_parent(env, osd, obj, oh, true);
+               if (rc)
+                       GOTO(out, rc);
+       }
 
-       /* kill object */
-       rc = __osd_object_destroy(env, obj, oh->ot_tx, osd_obj_tag);
-       if (rc) {
-               CERROR("%s: __osd_object_destroy() failed: rc = %d\n",
-                      osd->od_svname, rc);
-               GOTO(out, rc);
+       oid = obj->oo_dn->dn_object;
+       if (unlikely(obj->oo_destroy == OSD_DESTROY_NONE)) {
+               /* this may happen if the destroy wasn't declared
+                * e.g. when the object is created and then destroyed
+                * in the same transaction - we don't need additional
+                * space for destroy specifically */
+               LASSERT(obj->oo_attr.la_size <= osd_sync_destroy_max_size);
+               rc = -dmu_object_free(osd->od_os, oid, oh->ot_tx);
+               if (rc)
+                       CERROR("%s: failed to free %s/%#llx: rc = %d\n",
+                              osd->od_svname, buf, oid, rc);
+       } else if (obj->oo_destroy == OSD_DESTROY_SYNC) {
+               rc = -dmu_object_free(osd->od_os, oid, oh->ot_tx);
+               if (rc)
+                       CERROR("%s: failed to free %s/%#llx: rc = %d\n",
+                              osd->od_svname, buf, oid, rc);
+       } else { /* asynchronous destroy */
+               char *key = info->oti_key;
+
+               rc = osd_object_unlinked_add(obj, oh);
+               if (rc)
+                       GOTO(out, rc);
+
+               snprintf(key, sizeof(info->oti_key), "%llx", oid);
+               rc = osd_zap_add(osd, osd->od_unlinked->dn_object,
+                                osd->od_unlinked, key, 8, 1, &oid, oh->ot_tx);
+               if (rc)
+                       CERROR("%s: zap_add_int() failed %s/%#llx: rc = %d\n",
+                              osd->od_svname, buf, oid, rc);
        }
 
+       /* Remove the OI mapping after the destroy to handle the race with
+        * OI scrub that may insert missed OI mapping during the interval. */
+       rc = osd_zap_remove(osd, zapid, zdn, buf, oh->ot_tx);
+       if (unlikely(rc == -ENOENT))
+               rc = 0;
+       if (rc)
+               CERROR("%s: zap_remove(%s) failed: rc = %d\n",
+                      osd->od_svname, buf, rc);
+
+       GOTO(out, rc);
+
 out:
        /* not needed in the cache anymore */
        set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
-
+       if (rc == 0)
+               obj->oo_destroyed = 1;
+       up_write(&obj->oo_guard);
        RETURN (0);
 }
 
 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
 {
        struct osd_object *obj = osd_obj(l);
-
-       if (obj->oo_db != NULL) {
-               osd_object_sa_fini(obj);
-               if (obj->oo_sa_xattr) {
-                       nvlist_free(obj->oo_sa_xattr);
-                       obj->oo_sa_xattr = NULL;
+       const struct lu_fid *fid = lu_object_fid(l);
+
+       if (obj->oo_dn) {
+               if (likely(!fid_is_acct(fid))) {
+                       osd_object_sa_fini(obj);
+                       if (obj->oo_sa_xattr) {
+                               nvlist_free(obj->oo_sa_xattr);
+                               obj->oo_sa_xattr = NULL;
+                       }
+                       osd_dnode_rele(obj->oo_dn);
+                       list_del(&obj->oo_sa_linkage);
                }
-               sa_buf_rele(obj->oo_db, osd_obj_tag);
-               list_del(&obj->oo_sa_linkage);
-               obj->oo_db = NULL;
+               obj->oo_dn = NULL;
        }
 }
 
@@ -679,28 +934,27 @@ static int osd_object_print(const struct lu_env *env, void *cookie,
        return (*p)(env, cookie, LUSTRE_OSD_ZFS_NAME"-object@%p", o);
 }
 
-static void osd_object_read_lock(const struct lu_env *env,
-                                struct dt_object *dt, unsigned role)
+static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
+                         unsigned role)
 {
        struct osd_object *obj = osd_dt_obj(dt);
 
        LASSERT(osd_invariant(obj));
 
-       down_read(&obj->oo_sem);
+       down_read_nested(&obj->oo_sem, role);
 }
 
-static void osd_object_write_lock(const struct lu_env *env,
-                                 struct dt_object *dt, unsigned role)
+static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
+                          unsigned role)
 {
        struct osd_object *obj = osd_dt_obj(dt);
 
        LASSERT(osd_invariant(obj));
 
-       down_write(&obj->oo_sem);
+       down_write_nested(&obj->oo_sem, role);
 }
 
-static void osd_object_read_unlock(const struct lu_env *env,
-                                  struct dt_object *dt)
+static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
 {
        struct osd_object *obj = osd_dt_obj(dt);
 
@@ -708,17 +962,15 @@ static void osd_object_read_unlock(const struct lu_env *env,
        up_read(&obj->oo_sem);
 }
 
-static void osd_object_write_unlock(const struct lu_env *env,
-                                    struct dt_object *dt)
+static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
 {
-        struct osd_object *obj = osd_dt_obj(dt);
+       struct osd_object *obj = osd_dt_obj(dt);
 
-        LASSERT(osd_invariant(obj));
+       LASSERT(osd_invariant(obj));
        up_write(&obj->oo_sem);
 }
 
-static int osd_object_write_locked(const struct lu_env *env,
-                                  struct dt_object *dt)
+static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
 {
        struct osd_object *obj = osd_dt_obj(dt);
        int rc = 1;
@@ -732,22 +984,42 @@ static int osd_object_write_locked(const struct lu_env *env,
        return rc;
 }
 
-static int osd_attr_get(const struct lu_env *env,
-                       struct dt_object *dt,
-                       struct lu_attr *attr,
-                       struct lustre_capa *capa)
+static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
+                       struct lu_attr *attr)
 {
-       struct osd_object       *obj = osd_dt_obj(dt);
-       uint64_t                 blocks;
-       uint32_t                 blksize;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct osd_device *osd = osd_obj2dev(obj);
+       uint64_t blocks;
+       uint32_t blksize;
+       int rc = 0;
+
+       down_read(&obj->oo_guard);
+
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out, rc = -ENOENT);
+
+       if (unlikely(fid_is_acct(lu_object_fid(&dt->do_lu))))
+               GOTO(out, rc = 0);
 
-       LASSERT(dt_object_exists(dt));
        LASSERT(osd_invariant(obj));
-       LASSERT(obj->oo_db);
+       LASSERT(obj->oo_dn);
 
        read_lock(&obj->oo_attr_lock);
        *attr = obj->oo_attr;
+       if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
+               attr->la_valid |= LA_FLAGS;
+               attr->la_flags |= LUSTRE_ORPHAN_FL;
+       }
+       if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
+               attr->la_valid |= LA_FLAGS;
+               attr->la_flags |= LUSTRE_ENCRYPT_FL;
+       }
        read_unlock(&obj->oo_attr_lock);
+       if (attr->la_valid & LA_FLAGS && attr->la_flags & LUSTRE_ORPHAN_FL)
+               CDEBUG(D_INFO, "%s: set orphan flag on "DFID" (%#llx/%#x)\n",
+                      osd_obj2dev(obj)->od_svname,
+                      PFID(lu_object_fid(&dt->do_lu)),
+                      attr->la_valid, obj->oo_lma_flags);
 
        /* with ZFS_DEBUG zrl_add_debug() called by DB_DNODE_ENTER()
         * from within sa_object_size() can block on a mutex, so
@@ -755,17 +1027,23 @@ static int osd_attr_get(const struct lu_env *env,
        sa_object_size(obj->oo_sa_hdl, &blksize, &blocks);
        /* we do not control size of indices, so always calculate
         * it from number of blocks reported by DMU */
-       if (S_ISDIR(attr->la_mode))
+       if (S_ISDIR(attr->la_mode)) {
                attr->la_size = 512 * blocks;
+               rc = -zap_count(osd->od_os, obj->oo_dn->dn_object,
+                               &attr->la_dirent_count);
+       }
        /* Block size may be not set; suggest maximal I/O transfers. */
        if (blksize == 0)
-               blksize = 1ULL << SPA_MAXBLOCKSHIFT;
+               blksize = osd_spa_maxblocksize(
+                       dmu_objset_spa(osd_obj2dev(obj)->od_os));
 
        attr->la_blksize = blksize;
        attr->la_blocks = blocks;
        attr->la_valid |= LA_BLOCKS | LA_BLKSIZE;
 
-       return 0;
+out:
+       up_read(&obj->oo_guard);
+       return rc;
 }
 
 /* Simple wrapper on top of qsd API which implement quota transfer for osd
@@ -775,14 +1053,14 @@ static inline int qsd_transfer(const struct lu_env *env,
                               struct qsd_instance *qsd,
                               struct lquota_trans *trans, int qtype,
                               __u64 orig_id, __u64 new_id, __u64 bspace,
-                              struct lquota_id_info *qi)
+                              struct lquota_id_info *qi, bool ignore_edquot)
 {
        int     rc;
 
        if (unlikely(qsd == NULL))
                return 0;
 
-       LASSERT(qtype >= 0 && qtype < MAXQUOTAS);
+       LASSERT(qtype >= 0 && qtype < LL_MAXQUOTAS);
        qi->lqi_type = qtype;
 
        /* inode accounting */
@@ -792,7 +1070,7 @@ static inline int qsd_transfer(const struct lu_env *env,
        qi->lqi_id.qid_uid = new_id;
        qi->lqi_space      = 1;
        rc = qsd_op_begin(env, qsd, trans, qi, NULL);
-       if (rc == -EDQUOT || rc == -EINPROGRESS)
+       if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
                rc = 0;
        if (rc)
                return rc;
@@ -814,7 +1092,7 @@ static inline int qsd_transfer(const struct lu_env *env,
        qi->lqi_id.qid_uid = new_id;
        qi->lqi_space      = bspace;
        rc = qsd_op_begin(env, qsd, trans, qi, NULL);
-       if (rc == -EDQUOT || rc == -EINPROGRESS)
+       if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
                rc = 0;
        if (rc)
                return rc;
@@ -835,66 +1113,129 @@ static int osd_declare_attr_set(const struct lu_env *env,
                                struct thandle *handle)
 {
        struct osd_thread_info  *info = osd_oti_get(env);
-       char                    *buf = osd_oti_get(env)->oti_str;
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_device       *osd = osd_obj2dev(obj);
+       dmu_tx_hold_t           *txh;
        struct osd_thandle      *oh;
        uint64_t                 bspace;
        uint32_t                 blksize;
-       int                      rc;
+       int                      rc = 0;
+       bool                     found;
        ENTRY;
 
-       if (!dt_object_exists(dt)) {
-               /* XXX: sanity check that object creation is declared */
-               RETURN(0);
-       }
 
        LASSERT(handle != NULL);
        LASSERT(osd_invariant(obj));
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
+
+       down_read(&obj->oo_guard);
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out_sem, rc = 0);
 
        LASSERT(obj->oo_sa_hdl != NULL);
        LASSERT(oh->ot_tx != NULL);
-       dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
+       /* regular attributes are part of the bonus buffer */
+       /* let's check whether this object is already part of
+        * transaction.. */
+       found = false;
+       for (txh = list_head(&oh->ot_tx->tx_holds); txh;
+            txh = list_next(&oh->ot_tx->tx_holds, txh)) {
+               if (txh->txh_dnode == NULL)
+                       continue;
+               if (txh->txh_dnode->dn_object != obj->oo_dn->dn_object)
+                       continue;
+               /* this object is part of the transaction already
+                * we don't need to declare bonus again */
+               found = true;
+               break;
+       }
+       if (!found)
+               dmu_tx_hold_bonus(oh->ot_tx, obj->oo_dn->dn_object);
        if (oh->ot_tx->tx_err != 0)
-               RETURN(-oh->ot_tx->tx_err);
+               GOTO(out_sem, rc = -oh->ot_tx->tx_err);
 
-       sa_object_size(obj->oo_sa_hdl, &blksize, &bspace);
-       bspace = toqb(bspace * blksize);
+       if (attr && attr->la_valid & LA_FLAGS) {
+               /* LMA is usually a part of bonus, no need to declare
+                * anything else */
+       }
 
-       if (attr && attr->la_valid & LA_UID) {
-               /* account for user inode tracking ZAP update */
-               dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
-               dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, TRUE, buf);
+       if (attr && (attr->la_valid & (LA_UID | LA_GID | LA_PROJID))) {
+               sa_object_size(obj->oo_sa_hdl, &blksize, &bspace);
+               bspace = toqb(bspace * 512);
 
+               CDEBUG(D_QUOTA,
+                      "%s: enforce quota on UID %u, GID %u, the quota space is %lld (%u)\n",
+                      osd->od_svname,
+                      attr->la_uid, attr->la_gid, bspace, blksize);
+       }
+       /* to preserve locking order - qsd_transfer() may need to flush
+        * currently running transaction when we're out of quota. */
+       up_read(&obj->oo_guard);
+
+       if (attr && attr->la_valid & LA_UID) {
                /* quota enforcement for user */
                if (attr->la_uid != obj->oo_attr.la_uid) {
-                       rc = qsd_transfer(env, osd->od_quota_slave,
+                       rc = qsd_transfer(env, osd_def_qsd(osd),
                                          &oh->ot_quota_trans, USRQUOTA,
                                          obj->oo_attr.la_uid, attr->la_uid,
-                                         bspace, &info->oti_qi);
+                                         bspace, &info->oti_qi, true);
                        if (rc)
-                               RETURN(rc);
+                               GOTO(out, rc);
                }
        }
        if (attr && attr->la_valid & LA_GID) {
-               /* account for user inode tracking ZAP update */
-               dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
-               dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, TRUE, buf);
-
                /* quota enforcement for group */
                if (attr->la_gid != obj->oo_attr.la_gid) {
-                       rc = qsd_transfer(env, osd->od_quota_slave,
+                       rc = qsd_transfer(env, osd_def_qsd(osd),
                                          &oh->ot_quota_trans, GRPQUOTA,
                                          obj->oo_attr.la_gid, attr->la_gid,
-                                         bspace, &info->oti_qi);
+                                         bspace, &info->oti_qi,
+                                         !(attr->la_flags &
+                                                       LUSTRE_SET_SYNC_FL));
                        if (rc)
-                               RETURN(rc);
+                               GOTO(out, rc);
                }
        }
-
-       RETURN(0);
+#ifdef ZFS_PROJINHERIT
+       if (attr && attr->la_valid & LA_PROJID) {
+               /* quota enforcement for project */
+               if (attr->la_projid != obj->oo_attr.la_projid) {
+                       if (!osd->od_projectused_dn)
+                               GOTO(out, rc = -EOPNOTSUPP);
+
+                       /* Usually, if project quota is upgradable for the
+                        * device, then the upgrade will be done before or when
+                        * mount the device. So when we come here, this project
+                        * should have project ID attribute already (that is
+                        * zero by default).  Otherwise, there was something
+                        * wrong during the former upgrade, let's return failure
+                        * to report that.
+                        *
+                        * Please note that, different from other attributes,
+                        * you can NOT simply set the project ID attribute under
+                        * such case, because adding (NOT change) project ID
+                        * attribute needs to change the object's attribute
+                        * layout to match zfs backend quota accounting
+                        * requirement. */
+                       if (unlikely(!obj->oo_with_projid))
+                               GOTO(out, rc = -ENXIO);
+
+                       rc = qsd_transfer(env, osd_def_qsd(osd),
+                                         &oh->ot_quota_trans, PRJQUOTA,
+                                         obj->oo_attr.la_projid,
+                                         attr->la_projid, bspace,
+                                         &info->oti_qi, true);
+                       if (rc)
+                               GOTO(out, rc);
+               }
+       }
+#endif
+out:
+       RETURN(rc);
+out_sem:
+       up_read(&obj->oo_guard);
+       RETURN(rc);
 }
 
 /*
@@ -905,74 +1246,128 @@ static int osd_declare_attr_set(const struct lu_env *env,
  * to a transaction group.
  */
 static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
-                       const struct lu_attr *la, struct thandle *handle,
-                       struct lustre_capa *capa)
+                       const struct lu_attr *la, struct thandle *handle)
 {
+       struct osd_thread_info  *info = osd_oti_get(env);
+       sa_bulk_attr_t          *bulk = osd_oti_get(env)->oti_attr_bulk;
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_device       *osd = osd_obj2dev(obj);
        struct osd_thandle      *oh;
-       struct osa_attr         *osa = &osd_oti_get(env)->oti_osa;
-       sa_bulk_attr_t          *bulk;
+       struct osa_attr         *osa = &info->oti_osa;
        __u64                    valid = la->la_valid;
        int                      cnt;
        int                      rc = 0;
 
        ENTRY;
+
+       down_read(&obj->oo_guard);
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out, rc = -ENOENT);
+
        LASSERT(handle != NULL);
-       LASSERT(dt_object_exists(dt));
        LASSERT(osd_invariant(obj));
        LASSERT(obj->oo_sa_hdl);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        /* Assert that the transaction has been assigned to a
           transaction group. */
        LASSERT(oh->ot_tx->tx_txg != 0);
 
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) && !osd->od_is_ost) {
+               struct zpl_direntry *zde = &info->oti_zde.lzd_reg;
+               char *buf = info->oti_str;
+               dnode_t *zdn = NULL;
+               uint64_t zapid;
+
+               zapid = osd_get_name_n_idx(env, osd, lu_object_fid(&dt->do_lu),
+                                          buf, sizeof(info->oti_str), &zdn);
+               rc = osd_zap_lookup(osd, zapid, zdn, buf, 8,
+                                   sizeof(*zde) / 8, zde);
+               if (!rc) {
+                       zde->zde_dnode -= 1;
+                       rc = -zap_update(osd->od_os, zapid, buf, 8,
+                                        sizeof(*zde) / 8, zde, oh->ot_tx);
+               }
+               if (rc > 0)
+                       rc = 0;
+               GOTO(out, rc);
+       }
+
        /* Only allow set size for regular file */
        if (!S_ISREG(dt->do_lu.lo_header->loh_attr))
                valid &= ~(LA_SIZE | LA_BLOCKS);
 
-       if (valid == 0)
-               RETURN(0);
+       if (valid & LA_CTIME && la->la_ctime == obj->oo_attr.la_ctime)
+               valid &= ~LA_CTIME;
 
-       OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 10);
-       if (bulk == NULL)
-               RETURN(-ENOMEM);
+       if (valid & LA_MTIME && la->la_mtime == obj->oo_attr.la_mtime)
+               valid &= ~LA_MTIME;
 
-       /* do both accounting updates outside oo_attr_lock below */
-       if ((valid & LA_UID) && (la->la_uid != obj->oo_attr.la_uid)) {
-               /* Update user accounting. Failure isn't fatal, but we still
-                * log an error message */
-               rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
-                                       la->la_uid, 1, oh->ot_tx);
-               if (rc)
-                       CERROR("%s: failed to update accounting ZAP for user "
-                               "%d (%d)\n", osd->od_svname, la->la_uid, rc);
-               rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
-                                       obj->oo_attr.la_uid, -1, oh->ot_tx);
-               if (rc)
-                       CERROR("%s: failed to update accounting ZAP for user "
-                               "%d (%d)\n", osd->od_svname,
-                               obj->oo_attr.la_uid, rc);
-       }
-       if ((valid & LA_GID) && (la->la_gid != obj->oo_attr.la_gid)) {
-               /* Update group accounting. Failure isn't fatal, but we still
-                * log an error message */
-               rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
-                                       la->la_gid, 1, oh->ot_tx);
-               if (rc)
-                       CERROR("%s: failed to update accounting ZAP for user "
-                               "%d (%d)\n", osd->od_svname, la->la_gid, rc);
-               rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
-                                       obj->oo_attr.la_gid, -1, oh->ot_tx);
-               if (rc)
-                       CERROR("%s: failed to update accounting ZAP for user "
-                               "%d (%d)\n", osd->od_svname,
-                               obj->oo_attr.la_gid, rc);
+       if (valid & LA_ATIME && la->la_atime == obj->oo_attr.la_atime)
+               valid &= ~LA_ATIME;
+
+       if (valid == 0)
+               GOTO(out, rc = 0);
+
+       if (valid & LA_FLAGS) {
+               struct lustre_mdt_attrs *lma;
+               struct lu_buf buf;
+               int size = 0;
+
+               if (la->la_flags & LUSTRE_LMA_FL_MASKS) {
+                       LASSERT(!obj->oo_pfid_in_lma);
+                       BUILD_BUG_ON(sizeof(info->oti_buf) < sizeof(*lma));
+                       lma = (struct lustre_mdt_attrs *)&info->oti_buf;
+                       buf.lb_buf = lma;
+                       buf.lb_len = sizeof(info->oti_buf);
+
+                       /* Please do NOT call osd_xattr_get() directly, that
+                        * will cause recursive down_read() on oo_guard. */
+                       rc = osd_xattr_get_internal(env, obj, &buf,
+                                                   XATTR_NAME_LMA, &size);
+                       if (!rc && unlikely(size < sizeof(*lma))) {
+                               rc = -EINVAL;
+                       } else if (!rc) {
+                               lma->lma_incompat =
+                                       le32_to_cpu(lma->lma_incompat);
+                               lma->lma_incompat |=
+                                       lustre_to_lma_flags(la->la_flags);
+                               lma->lma_incompat =
+                                       cpu_to_le32(lma->lma_incompat);
+                               buf.lb_buf = lma;
+                               buf.lb_len = sizeof(*lma);
+                               rc = osd_xattr_set_internal(env, obj, &buf,
+                                                           XATTR_NAME_LMA,
+                                                           LU_XATTR_REPLACE,
+                                                           oh);
+                       }
+                       if (rc < 0) {
+                               CWARN("%s: failed to set LMA flags: rc = %d\n",
+                                      osd->od_svname, rc);
+                               GOTO(out, rc);
+                       } else {
+                               obj->oo_lma_flags =
+                                       la->la_flags & LUSTRE_LMA_FL_MASKS;
+                       }
+               }
        }
 
        write_lock(&obj->oo_attr_lock);
        cnt = 0;
+
+       if (valid & LA_PROJID) {
+#ifdef ZFS_PROJINHERIT
+               if (osd->od_projectused_dn) {
+                       LASSERT(obj->oo_with_projid);
+
+                       osa->projid = obj->oo_attr.la_projid = la->la_projid;
+                       SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
+                                        &osa->projid, 8);
+               } else
+#endif
+                       valid &= ~LA_PROJID;
+       }
+
        if (valid & LA_ATIME) {
                osa->atime[0] = obj->oo_attr.la_atime = la->la_atime;
                SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL,
@@ -1016,6 +1411,10 @@ static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
                /* many flags are not supported by zfs, so ensure a good cached
                 * copy */
                obj->oo_attr.la_flags = attrs_zfs2fs(osa->flags);
+#ifdef ZFS_PROJINHERIT
+               if (obj->oo_with_projid && osd->od_projectused_dn)
+                       osa->flags |= ZFS_PROJID;
+#endif
                SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL,
                                 &osa->flags, 8);
        }
@@ -1032,9 +1431,11 @@ static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
        obj->oo_attr.la_valid |= valid;
        write_unlock(&obj->oo_attr_lock);
 
+       LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
        rc = osd_object_sa_bulk_update(obj, bulk, cnt, oh);
 
-       OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 10);
+out:
+       up_read(&obj->oo_guard);
        RETURN(rc);
 }
 
@@ -1052,22 +1453,28 @@ static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
 
        ah->dah_parent = parent;
        ah->dah_mode = child_mode;
+
+       if (parent != NULL && !dt_object_remote(parent)) {
+               /* will help to find FID->ino at dt_insert("..") */
+               struct osd_object *pobj = osd_dt_obj(parent);
+
+               osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
+       }
 }
 
-static int osd_declare_object_create(const struct lu_env *env,
-                                    struct dt_object *dt,
-                                    struct lu_attr *attr,
-                                    struct dt_allocation_hint *hint,
-                                    struct dt_object_format *dof,
-                                    struct thandle *handle)
+static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
+                             struct lu_attr *attr,
+                             struct dt_allocation_hint *hint,
+                             struct dt_object_format *dof,
+                             struct thandle *handle)
 {
-       char                    *buf = osd_oti_get(env)->oti_str;
        const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_device       *osd = osd_obj2dev(obj);
        struct osd_thandle      *oh;
        uint64_t                 zapid;
-       int                      rc;
+       dnode_t                 *dn;
+       int                      rc, dnode_size;
        ENTRY;
 
        LASSERT(dof);
@@ -1084,21 +1491,28 @@ static int osd_declare_object_create(const struct lu_env *env,
        }
 
        LASSERT(handle != NULL);
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_tx != NULL);
 
+       /* this is the minimum set of EAs on every Lustre object */
+       obj->oo_ea_in_bonus = OSD_BASE_EA_IN_BONUS;
+       /* reserve 32 bytes for extra stuff like ACLs */
+       dnode_size = size_roundup_power2(obj->oo_ea_in_bonus + 32);
+
        switch (dof->dof_type) {
                case DFT_DIR:
                        dt->do_index_ops = &osd_dir_ops;
+                       /* fallthrough */
                case DFT_INDEX:
                        /* for zap create */
-                       dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT, 1, NULL);
+                       dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT, FALSE, NULL);
+                       dmu_tx_hold_sa_create(oh->ot_tx, dnode_size);
                        break;
                case DFT_REGULAR:
                case DFT_SYM:
                case DFT_NODE:
                        /* first, we'll create new object */
-                       dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
+                       dmu_tx_hold_sa_create(oh->ot_tx, dnode_size);
                        break;
 
                default:
@@ -1107,43 +1521,38 @@ static int osd_declare_object_create(const struct lu_env *env,
        }
 
        /* and we'll add it to some mapping */
-       zapid = osd_get_name_n_idx(env, osd, fid, buf);
-       dmu_tx_hold_bonus(oh->ot_tx, zapid);
-       dmu_tx_hold_zap(oh->ot_tx, zapid, TRUE, buf);
+       zapid = osd_get_name_n_idx(env, osd, fid, NULL, 0, &dn);
+       osd_tx_hold_zap(oh->ot_tx, zapid, dn, TRUE, NULL);
 
-       /* we will also update inode accounting ZAPs */
-       dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
-       dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, TRUE, buf);
-       dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
-       dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, TRUE, buf);
+       /* will help to find FID->ino mapping at dt_insert() */
+       osd_idc_find_and_init(env, osd, obj);
 
-       dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
+       rc = osd_declare_quota(env, osd, attr->la_uid, attr->la_gid,
+                              attr->la_projid, 1, oh, NULL, OSD_QID_INODE);
 
-       __osd_xattr_declare_set(env, obj, sizeof(struct lustre_mdt_attrs),
-                               XATTR_NAME_LMA, oh);
-
-       rc = osd_declare_quota(env, osd, attr->la_uid, attr->la_gid, 1, oh,
-                              false, NULL, false);
        RETURN(rc);
 }
 
 int __osd_attr_init(const struct lu_env *env, struct osd_device *osd,
-                   uint64_t oid, dmu_tx_t *tx, struct lu_attr *la,
-                   uint64_t parent)
+                   struct osd_object *obj, sa_handle_t *sa_hdl, dmu_tx_t *tx,
+                   struct lu_attr *la, uint64_t parent,
+                   nvlist_t *xattr)
 {
-       sa_bulk_attr_t  *bulk;
-       sa_handle_t     *sa_hdl;
-       struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
-       uint64_t         gen;
-       uint64_t         crtime[2];
-       timestruc_t      now;
-       int              cnt;
-       int              rc;
+       sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
+       struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
+       uint64_t gen;
+       inode_timespec_t now;
+       int cnt;
+       int rc;
+       char *dxattr = NULL;
+       size_t sa_size;
 
-       gethrestime(&now);
-       gen = dmu_tx_get_txg(tx);
 
-       ZFS_TIME_ENCODE(&now, crtime);
+       LASSERT(sa_hdl);
+
+       gen = dmu_tx_get_txg(tx);
+       gethrestime(&now);
+       ZFS_TIME_ENCODE(&now, osa->btime);
 
        osa->atime[0] = la->la_atime;
        osa->ctime[0] = la->la_ctime;
@@ -1153,26 +1562,32 @@ int __osd_attr_init(const struct lu_env *env, struct osd_device *osd,
        osa->gid = la->la_gid;
        osa->rdev = la->la_rdev;
        osa->nlink = la->la_nlink;
-       osa->flags = attrs_fs2zfs(la->la_flags);
+       if (la->la_valid & LA_FLAGS)
+               osa->flags = attrs_fs2zfs(la->la_flags);
+       else
+               osa->flags = 0;
        osa->size  = la->la_size;
-
-       /* Now add in all of the "SA" attributes */
-       rc = -sa_handle_get(osd->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
-       if (rc)
-               return rc;
-
-       OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 13);
-       if (bulk == NULL) {
-               rc = -ENOMEM;
-               goto out;
+#ifdef ZFS_PROJINHERIT
+       if (osd->od_projectused_dn) {
+               if (la->la_valid & LA_PROJID)
+                       osa->projid = la->la_projid;
+               else
+                       osa->projid = ZFS_DEFAULT_PROJID;
+               osa->flags |= ZFS_PROJID;
+               if (obj)
+                       obj->oo_with_projid = 1;
+       } else {
+               osa->flags &= ~ZFS_PROJID;
        }
+#endif
+
        /*
         * we need to create all SA below upon object create.
         *
         * XXX The attribute order matters since the accounting callback relies
         * on static offsets (i.e. SA_*_OFFSET, see zfs_space_delta_cb()) to
-        * look up the UID/GID attributes. Moreover, the callback does not seem
-        * to support the spill block.
+        * look up the UID/GID/PROJID attributes. Moreover, the callback does
+        * not seem to support the spill block.
         * We define attributes in the same order as SA_*_OFFSET in order to
         * work around the problem. See ORI-610.
         */
@@ -1187,36 +1602,124 @@ int __osd_attr_init(const struct lu_env *env, struct osd_device *osd,
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
-       SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
+       SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, osa->btime, 16);
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
+#ifdef ZFS_PROJINHERIT
+       if (osd->od_projectused_dn)
+               SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
+                                &osa->projid, 8);
+#endif
        SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
+       LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
+
+       if (xattr) {
+               rc = -nvlist_size(xattr, &sa_size, NV_ENCODE_XDR);
+               LASSERT(rc == 0);
+
+               dxattr = osd_zio_buf_alloc(sa_size);
+               LASSERT(dxattr);
+
+               rc = -nvlist_pack(xattr, &dxattr, &sa_size,
+                               NV_ENCODE_XDR, KM_SLEEP);
+               LASSERT(rc == 0);
+
+               SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd),
+                               NULL, dxattr, sa_size);
+       }
 
        rc = -sa_replace_all_by_template(sa_hdl, bulk, cnt, tx);
+       if (dxattr)
+               osd_zio_buf_free(dxattr, sa_size);
+
+       return rc;
+}
+
+int osd_find_new_dnode(const struct lu_env *env, dmu_tx_t *tx,
+                      uint64_t oid, dnode_t **dnp)
+{
+       dmu_tx_hold_t *txh;
+       int rc = 0;
+
+       /* take dnode_t from tx to save on dnode#->dnode_t lookup */
+       for (txh = list_tail(&tx->tx_holds); txh;
+            txh = list_prev(&tx->tx_holds, txh)) {
+               dnode_t *dn = txh->txh_dnode;
+               dmu_buf_impl_t *db;
+
+               if (dn == NULL)
+                       continue;
+               if (dn->dn_object != oid)
+                       continue;
+               db = dn->dn_bonus;
+               if (db == NULL) {
+                       rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
+                       if (dn->dn_bonus == NULL)
+                               dbuf_create_bonus(dn);
+                       rw_exit(&dn->dn_struct_rwlock);
+               }
+               db = dn->dn_bonus;
+               LASSERT(db);
+               LASSERT(dn->dn_handle);
+               DB_DNODE_ENTER(db);
+               if (zfs_refcount_add(&db->db_holds, osd_obj_tag) == 1) {
+                       zfs_refcount_add(&dn->dn_holds, osd_obj_tag);
+                       atomic_inc_32(&dn->dn_dbufs_count);
+               }
+               *dnp = dn;
+               DB_DNODE_EXIT(db);
+               dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH);
+               break;
+       }
+
+       if (unlikely(*dnp == NULL))
+               rc = __osd_obj2dnode(tx->tx_objset, oid, dnp);
 
-       OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 13);
-out:
-       sa_handle_destroy(sa_hdl);
        return rc;
 }
 
+#ifdef HAVE_DMU_OBJECT_ALLOC_DNSIZE
+int osd_find_dnsize(struct osd_device *osd, int ea_in_bonus)
+{
+       int dnsize;
+
+       if (osd->od_dnsize == ZFS_DNSIZE_AUTO) {
+               dnsize = DNODE_MIN_SIZE;
+               do {
+                       if (DN_BONUS_SIZE(dnsize) >= ea_in_bonus + 32)
+                               break;
+                       dnsize <<= 1;
+               } while (dnsize < DNODE_MAX_SIZE);
+               if (dnsize > DNODE_MAX_SIZE)
+                       dnsize = DNODE_MAX_SIZE;
+       } else if (osd->od_dnsize == ZFS_DNSIZE_1K) {
+               dnsize = 1024;
+       } else if (osd->od_dnsize == ZFS_DNSIZE_2K) {
+               dnsize = 2048;
+       } else if (osd->od_dnsize == ZFS_DNSIZE_4K) {
+               dnsize = 4096;
+       } else if (osd->od_dnsize == ZFS_DNSIZE_8K) {
+               dnsize = 8192;
+       } else if (osd->od_dnsize == ZFS_DNSIZE_16K) {
+               dnsize = 16384;
+       } else {
+               dnsize = DNODE_MIN_SIZE;
+       }
+       return dnsize;
+}
+#endif
+
 /*
  * The transaction passed to this routine must have
  * dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT) called and then assigned
  * to a transaction group.
  */
-int __osd_object_create(const struct lu_env *env, struct osd_object *obj,
-                       dmu_buf_t **dbp, dmu_tx_t *tx, struct lu_attr *la,
-                       uint64_t parent)
+int __osd_object_create(const struct lu_env *env, struct osd_device *osd,
+                       struct osd_object *obj, const struct lu_fid *fid,
+                       dnode_t **dnp, dmu_tx_t *tx, struct lu_attr *la)
 {
-       uint64_t             oid;
-       int                  rc;
-       struct osd_device   *osd = osd_obj2dev(obj);
-       const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
-       dmu_object_type_t    type = DMU_OT_PLAIN_FILE_CONTENTS;
-
-       /* Assert that the transaction has been assigned to a
-          transaction group. */
-       LASSERT(tx->tx_txg != 0);
+       dmu_object_type_t type = DMU_OT_PLAIN_FILE_CONTENTS;
+       uint64_t oid;
+       int size;
 
        /* Use DMU_OTN_UINT8_METADATA for local objects so their data blocks
         * would get an additional ditto copy */
@@ -1224,25 +1727,19 @@ int __osd_object_create(const struct lu_env *env, struct osd_object *obj,
                     fid_seq_is_local_file(fid_seq(fid))))
                type = DMU_OTN_UINT8_METADATA;
 
-       /* Create a new DMU object. */
-       oid = dmu_object_alloc(osd->od_os, type, 0,
-                              DMU_OT_SA, DN_MAX_BONUSLEN, tx);
-       rc = -sa_buf_hold(osd->od_os, oid, osd_obj_tag, dbp);
-       LASSERTF(rc == 0, "sa_buf_hold "LPU64" failed: %d\n", oid, rc);
+       /* Create a new DMU object using the default dnode size. */
+       if (obj)
+               size = obj->oo_ea_in_bonus;
+       else
+               size = OSD_BASE_EA_IN_BONUS;
+       oid = osd_dmu_object_alloc(osd->od_os, type, 0,
+                                  osd_find_dnsize(osd, size), tx);
 
        LASSERT(la->la_valid & LA_MODE);
        la->la_size = 0;
        la->la_nlink = 1;
 
-       rc = __osd_attr_init(env, osd, oid, tx, la, parent);
-       if (rc != 0) {
-               sa_buf_rele(*dbp, osd_obj_tag);
-               *dbp = NULL;
-               dmu_object_free(osd->od_os, oid, tx);
-               return rc;
-       }
-
-       return 0;
+       return osd_find_new_dnode(env, tx, oid, dnp);
 }
 
 /*
@@ -1256,88 +1753,90 @@ int __osd_object_create(const struct lu_env *env, struct osd_object *obj,
  * then we might need to re-evaluate the use of this flag and instead do
  * a conversion from the different internal ZAP hash formats being used. */
 int __osd_zap_create(const struct lu_env *env, struct osd_device *osd,
-                    dmu_buf_t **zap_dbp, dmu_tx_t *tx,
-                    struct lu_attr *la, uint64_t parent, zap_flags_t flags)
+                    dnode_t **dnp, dmu_tx_t *tx, struct lu_attr *la,
+                    unsigned dnsize, zap_flags_t flags)
 {
        uint64_t oid;
-       int      rc;
 
        /* Assert that the transaction has been assigned to a
           transaction group. */
        LASSERT(tx->tx_txg != 0);
+       *dnp = NULL;
 
-       oid = zap_create_flags(osd->od_os, 0, flags | ZAP_FLAG_HASH64,
-                              DMU_OT_DIRECTORY_CONTENTS,
-                              14, /* == ZFS fzap_default_block_shift */
-                              DN_MAX_INDBLKSHIFT, /* indirect block shift */
-                              DMU_OT_SA, DN_MAX_BONUSLEN, tx);
-
-       rc = -sa_buf_hold(osd->od_os, oid, osd_obj_tag, zap_dbp);
-       if (rc)
-               return rc;
+       oid = osd_zap_create_flags(osd->od_os, 0, flags | ZAP_FLAG_HASH64,
+                                  DMU_OT_DIRECTORY_CONTENTS,
+                                  14, /* == ZFS fzap_default_blockshift */
+                                  DN_MAX_INDBLKSHIFT, /* indirect blockshift */
+                                  dnsize, tx);
 
-       LASSERT(la->la_valid & LA_MODE);
        la->la_size = 2;
        la->la_nlink = 1;
 
-       return __osd_attr_init(env, osd, oid, tx, la, parent);
+       return osd_find_new_dnode(env, tx, oid, dnp);
 }
 
-static dmu_buf_t *osd_mkidx(const struct lu_env *env, struct osd_object *obj,
-                           struct lu_attr *la, uint64_t parent,
-                           struct osd_thandle *oh)
+static dnode_t *osd_mkidx(const struct lu_env *env, struct osd_object *obj,
+                         struct lu_attr *la, struct osd_thandle *oh)
 {
-       dmu_buf_t *db;
-       int        rc;
+       struct osd_device *osd = osd_obj2dev(obj);
+       dnode_t *dn;
+       int rc;
 
        /* Index file should be created as regular file in order not to confuse
         * ZPL which could interpret them as directory.
         * We set ZAP_FLAG_UINT64_KEY to let ZFS know than we are going to use
         * binary keys */
        LASSERT(S_ISREG(la->la_mode));
-       rc = __osd_zap_create(env, osd_obj2dev(obj), &db, oh->ot_tx, la, parent,
-                             ZAP_FLAG_UINT64_KEY);
+       rc = __osd_zap_create(env, osd, &dn, oh->ot_tx, la,
+               osd_find_dnsize(osd, obj->oo_ea_in_bonus), ZAP_FLAG_UINT64_KEY);
        if (rc)
                return ERR_PTR(rc);
-       return db;
+       return dn;
 }
 
-static dmu_buf_t *osd_mkdir(const struct lu_env *env, struct osd_object *obj,
-                           struct lu_attr *la, uint64_t parent,
-                           struct osd_thandle *oh)
+static dnode_t *osd_mkdir(const struct lu_env *env, struct osd_object *obj,
+                         struct lu_attr *la, struct osd_thandle *oh)
 {
-       dmu_buf_t *db;
-       int        rc;
+       struct osd_device *osd = osd_obj2dev(obj);
+       dnode_t *dn;
+       int rc;
 
        LASSERT(S_ISDIR(la->la_mode));
-       rc = __osd_zap_create(env, osd_obj2dev(obj), &db,
-                             oh->ot_tx, la, parent, 0);
+       rc = __osd_zap_create(env, osd, &dn, oh->ot_tx, la,
+                             osd_find_dnsize(osd, obj->oo_ea_in_bonus), 0);
        if (rc)
                return ERR_PTR(rc);
-       return db;
+       return dn;
 }
 
-static dmu_buf_t *osd_mkreg(const struct lu_env *env, struct osd_object *obj,
-                           struct lu_attr *la, uint64_t parent,
-                           struct osd_thandle *oh)
+static dnode_t *osd_mkreg(const struct lu_env *env, struct osd_object *obj,
+                         struct lu_attr *la, struct osd_thandle *oh)
 {
-       dmu_buf_t         *db;
-       int                rc;
+       const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
        struct osd_device *osd = osd_obj2dev(obj);
+       dnode_t *dn;
+       int rc;
 
        LASSERT(S_ISREG(la->la_mode));
-       rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
+       rc = __osd_object_create(env, osd, obj, fid, &dn, oh->ot_tx, la);
        if (rc)
                return ERR_PTR(rc);
 
-       /*
-        * XXX: a hack, OST to use bigger blocksize. we need
-        * a method in OSD API to control this from OFD/MDD
-        */
-       if (!lu_device_is_md(osd2lu_dev(osd))) {
-               rc = -dmu_object_set_blocksize(osd->od_os,
-                                              db->db_object,
-                               128 << 10, 0, oh->ot_tx);
+       if ((fid_is_idif(fid) || fid_is_norm(fid) || fid_is_echo(fid))) {
+               /* The minimum block size must be at least page size otherwise
+                * it will break the assumption in tgt_thread_big_cache where
+                * the array size is PTLRPC_MAX_BRW_PAGES. It will also affect
+                * RDMA due to subpage transfer size */
+               rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
+                                              PAGE_SIZE, 0, oh->ot_tx);
+               if (unlikely(rc)) {
+                       CERROR("%s: can't change blocksize: %d\n",
+                              osd->od_svname, rc);
+                       return ERR_PTR(rc);
+               }
+       } else if ((fid_is_llog(fid))) {
+               rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
+                                              LLOG_MIN_CHUNK_SIZE, 0, oh->ot_tx);
                if (unlikely(rc)) {
                        CERROR("%s: can't change blocksize: %d\n",
                               osd->od_svname, rc);
@@ -1345,45 +1844,45 @@ static dmu_buf_t *osd_mkreg(const struct lu_env *env, struct osd_object *obj,
                }
        }
 
-       return db;
+       return dn;
 }
 
-static dmu_buf_t *osd_mksym(const struct lu_env *env, struct osd_object *obj,
-                           struct lu_attr *la, uint64_t parent,
-                           struct osd_thandle *oh)
+static dnode_t *osd_mksym(const struct lu_env *env, struct osd_object *obj,
+                         struct lu_attr *la, struct osd_thandle *oh)
 {
-       dmu_buf_t *db;
-       int        rc;
+       dnode_t *dn;
+       int rc;
 
        LASSERT(S_ISLNK(la->la_mode));
-       rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
+       rc = __osd_object_create(env, osd_obj2dev(obj), obj,
+                                lu_object_fid(&obj->oo_dt.do_lu),
+                                &dn, oh->ot_tx, la);
        if (rc)
                return ERR_PTR(rc);
-       return db;
+       return dn;
 }
 
-static dmu_buf_t *osd_mknod(const struct lu_env *env, struct osd_object *obj,
-                           struct lu_attr *la, uint64_t parent,
-                           struct osd_thandle *oh)
+static dnode_t *osd_mknod(const struct lu_env *env, struct osd_object *obj,
+                         struct lu_attr *la, struct osd_thandle *oh)
 {
-       dmu_buf_t *db;
-       int        rc;
+       dnode_t *dn;
+       int rc;
 
-       la->la_valid = LA_MODE;
        if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode))
                la->la_valid |= LA_RDEV;
 
-       rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
+       rc = __osd_object_create(env, osd_obj2dev(obj), obj,
+                                lu_object_fid(&obj->oo_dt.do_lu),
+                                &dn, oh->ot_tx, la);
        if (rc)
                return ERR_PTR(rc);
-       return db;
+       return dn;
 }
 
-typedef dmu_buf_t *(*osd_obj_type_f)(const struct lu_env *env,
-                                    struct osd_object *obj,
-                                    struct lu_attr *la,
-                                    uint64_t parent,
-                                    struct osd_thandle *oh);
+typedef dnode_t *(*osd_obj_type_f)(const struct lu_env *env,
+                                  struct osd_object *obj,
+                                  struct lu_attr *la,
+                                  struct osd_thandle *oh);
 
 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
 {
@@ -1413,133 +1912,149 @@ static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
 }
 
 /*
- * Primitives for directory (i.e. ZAP) handling
+ * Concurrency: @dt is write locked.
  */
-static inline int osd_init_lma(const struct lu_env *env, struct osd_object *obj,
-                              const struct lu_fid *fid, struct osd_thandle *oh)
+static int osd_create(const struct lu_env *env, struct dt_object *dt,
+                     struct lu_attr *attr, struct dt_allocation_hint *hint,
+                     struct dt_object_format *dof, struct thandle *th)
 {
        struct osd_thread_info  *info = osd_oti_get(env);
        struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
-       struct lu_buf            buf;
-       int rc;
-
-       lustre_lma_init(lma, fid, 0, 0);
-       lustre_lma_swab(lma);
-       buf.lb_buf = lma;
-       buf.lb_len = sizeof(*lma);
-
-       rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
-                                   LU_XATTR_CREATE, oh, BYPASS_CAPA);
-
-       return rc;
-}
-
-/*
- * Concurrency: @dt is write locked.
- */
-static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
-                            struct lu_attr *attr,
-                            struct dt_allocation_hint *hint,
-                            struct dt_object_format *dof,
-                            struct thandle *th)
-{
-       struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
+       struct zpl_direntry     *zde = &info->oti_zde.lzd_reg;
        const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_device       *osd = osd_obj2dev(obj);
-       char                    *buf = osd_oti_get(env)->oti_str;
+       char                    *buf = info->oti_str;
        struct osd_thandle      *oh;
-       dmu_buf_t               *db;
-       uint64_t                 zapid;
+       dnode_t *dn = NULL, *zdn = NULL;
+       uint64_t                 zapid, parent = 0;
        int                      rc;
+       __u32 compat = 0;
 
        ENTRY;
 
+       LASSERT(!fid_is_acct(fid));
+
        /* concurrent create declarations should not see
         * the object inconsistent (db, attr, etc).
         * in regular cases acquisition should be cheap */
-       down(&obj->oo_guard);
+       down_write(&obj->oo_guard);
+
+       if (unlikely(dt_object_exists(dt)))
+               GOTO(out, rc = -EEXIST);
 
        LASSERT(osd_invariant(obj));
-       LASSERT(!dt_object_exists(dt));
        LASSERT(dof != NULL);
 
        LASSERT(th != NULL);
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
 
-       /*
-        * XXX missing: Quote handling.
-        */
-
-       LASSERT(obj->oo_db == NULL);
+       LASSERT(obj->oo_dn == NULL);
 
        /* to follow ZFS on-disk format we need
         * to initialize parent dnode properly */
-       zapid = 0;
-       if (hint && hint->dah_parent)
-               zapid = osd_dt_obj(hint->dah_parent)->oo_db->db_object;
+       if (hint != NULL && hint->dah_parent != NULL &&
+           !dt_object_remote(hint->dah_parent))
+               parent = osd_dt_obj(hint->dah_parent)->oo_dn->dn_object;
+
+       /* we may fix some attributes, better do not change the source */
+       obj->oo_attr = *attr;
+       obj->oo_attr.la_size = 0;
+       obj->oo_attr.la_nlink = 0;
+       obj->oo_attr.la_valid |= LA_SIZE | LA_NLINK | LA_TYPE;
+
+#ifdef ZFS_PROJINHERIT
+       if (osd->od_projectused_dn) {
+               if (!(obj->oo_attr.la_valid & LA_PROJID))
+                       obj->oo_attr.la_projid = ZFS_DEFAULT_PROJID;
+               obj->oo_with_projid = 1;
+       }
+#endif
 
-       db = osd_create_type_f(dof->dof_type)(env, obj, attr, zapid, oh);
-       if (IS_ERR(db))
-               GOTO(out, rc = PTR_ERR(db));
+       dn = osd_create_type_f(dof->dof_type)(env, obj, &obj->oo_attr, oh);
+       if (IS_ERR(dn)) {
+               rc = PTR_ERR(dn);
+               dn = NULL;
+               GOTO(out, rc);
+       }
 
        zde->zde_pad = 0;
-       zde->zde_dnode = db->db_object;
-       zde->zde_type = IFTODT(attr->la_mode & S_IFMT);
+       zde->zde_dnode = dn->dn_object;
+       zde->zde_type = S_DT(attr->la_mode & S_IFMT);
+
+       zapid = osd_get_name_n_idx(env, osd, fid, buf,
+                                  sizeof(info->oti_str), &zdn);
+       if (CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY) ||
+           (osd->od_is_ost && OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY)))
+               goto skip_add;
 
-       zapid = osd_get_name_n_idx(env, osd, fid, buf);
+       if (osd->od_is_ost && OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
+               zde->zde_dnode++;
 
-       rc = -zap_add(osd->od_os, zapid, buf, 8, 1, zde, oh->ot_tx);
+       rc = osd_zap_add(osd, zapid, zdn, buf, 8, 1, zde, oh->ot_tx);
        if (rc)
                GOTO(out, rc);
 
-       /* Add new object to inode accounting.
-        * Errors are not considered as fatal */
-       rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
-                               (attr->la_valid & LA_UID) ? attr->la_uid : 0, 1,
-                               oh->ot_tx);
+skip_add:
+       obj->oo_dn = dn;
+       /* Now add in all of the "SA" attributes */
+       rc = osd_sa_handle_get(obj);
        if (rc)
-               CERROR("%s: failed to add "DFID" to accounting ZAP for usr %d "
-                       "(%d)\n", osd->od_svname, PFID(fid), attr->la_uid, rc);
-       rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
-                               (attr->la_valid & LA_GID) ? attr->la_gid : 0, 1,
-                               oh->ot_tx);
+               GOTO(out, rc);
+
+       rc = -nvlist_alloc(&obj->oo_sa_xattr, NV_UNIQUE_NAME, KM_SLEEP);
+       if (rc)
+               GOTO(out, rc);
+
+       /* initialize LMA */
+       if (fid_is_idif(fid) || (fid_is_norm(fid) && osd->od_is_ost))
+               compat |= LMAC_FID_ON_OST;
+       lustre_lma_init(lma, fid, compat, 0);
+       lustre_lma_swab(lma);
+       rc = -nvlist_add_byte_array(obj->oo_sa_xattr, XATTR_NAME_LMA,
+                                   (uchar_t *)lma, sizeof(*lma));
        if (rc)
-               CERROR("%s: failed to add "DFID" to accounting ZAP for grp %d "
-                       "(%d)\n", osd->od_svname, PFID(fid), attr->la_gid, rc);
+               GOTO(out, rc);
 
        /* configure new osd object */
-       obj->oo_db = db;
-       rc = osd_object_init0(env, obj);
-       LASSERT(ergo(rc == 0, dt_object_exists(dt)));
-       LASSERT(osd_invariant(obj));
+       obj->oo_parent = parent != 0 ? parent : zapid;
+       obj->oo_late_attr_set = 1;
+       rc = __osd_sa_xattr_schedule_update(env, obj, oh);
+       if (rc)
+               GOTO(out, rc);
 
-       rc = osd_init_lma(env, obj, fid, oh);
-       if (rc) {
-               CERROR("%s: can not set LMA on "DFID": rc = %d\n",
-                      osd->od_svname, PFID(fid), rc);
-               /* ignore errors during LMA initialization */
-               rc = 0;
-       }
+       /* XXX: oo_lma_flags */
+       obj->oo_dt.do_lu.lo_header->loh_attr |= obj->oo_attr.la_mode & S_IFMT;
+       if (likely(!fid_is_acct(lu_object_fid(&obj->oo_dt.do_lu))))
+               /* no body operations for accounting objects */
+               obj->oo_dt.do_body_ops = &osd_body_ops;
+
+       osd_idc_find_and_init(env, osd, obj);
 
 out:
-       up(&obj->oo_guard);
+       if (unlikely(rc && dn)) {
+               dmu_object_free(osd->od_os, dn->dn_object, oh->ot_tx);
+               osd_dnode_rele(dn);
+               obj->oo_dn = NULL;
+       } else if (!rc) {
+               obj->oo_dt.do_lu.lo_header->loh_attr |= LOHA_EXISTS;
+       }
+       up_write(&obj->oo_guard);
        RETURN(rc);
 }
 
-static int osd_declare_object_ref_add(const struct lu_env *env,
-                                     struct dt_object *dt,
-                                     struct thandle *th)
+static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
+                              struct thandle *th)
 {
+       osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev), osd_dt_obj(dt));
        return osd_declare_attr_set(env, dt, NULL, th);
 }
 
 /*
  * Concurrency: @dt is write locked.
  */
-static int osd_object_ref_add(const struct lu_env *env,
-                             struct dt_object *dt,
-                             struct thandle *handle)
+static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
+                      struct thandle *handle)
 {
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_thandle      *oh;
@@ -1549,33 +2064,38 @@ static int osd_object_ref_add(const struct lu_env *env,
 
        ENTRY;
 
+       down_read(&obj->oo_guard);
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out, rc = -ENOENT);
+
        LASSERT(osd_invariant(obj));
-       LASSERT(dt_object_exists(dt));
        LASSERT(obj->oo_sa_hdl != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
 
        write_lock(&obj->oo_attr_lock);
        nlink = ++obj->oo_attr.la_nlink;
        write_unlock(&obj->oo_attr_lock);
 
        rc = osd_object_sa_update(obj, SA_ZPL_LINKS(osd), &nlink, 8, oh);
-       return rc;
+
+out:
+       up_read(&obj->oo_guard);
+       RETURN(rc);
 }
 
-static int osd_declare_object_ref_del(const struct lu_env *env,
-                                     struct dt_object *dt,
-                                     struct thandle *handle)
+static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
+                              struct thandle *handle)
 {
+       osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev), osd_dt_obj(dt));
        return osd_declare_attr_set(env, dt, NULL, handle);
 }
 
 /*
  * Concurrency: @dt is write locked.
  */
-static int osd_object_ref_del(const struct lu_env *env,
-                             struct dt_object *dt,
-                             struct thandle *handle)
+static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
+                      struct thandle *handle)
 {
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_thandle      *oh;
@@ -1585,11 +2105,15 @@ static int osd_object_ref_del(const struct lu_env *env,
 
        ENTRY;
 
+       down_read(&obj->oo_guard);
+
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out, rc = -ENOENT);
+
        LASSERT(osd_invariant(obj));
-       LASSERT(dt_object_exists(dt));
        LASSERT(obj->oo_sa_hdl != NULL);
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
        LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
 
        write_lock(&obj->oo_attr_lock);
@@ -1597,193 +2121,75 @@ static int osd_object_ref_del(const struct lu_env *env,
        write_unlock(&obj->oo_attr_lock);
 
        rc = osd_object_sa_update(obj, SA_ZPL_LINKS(osd), &nlink, 8, oh);
-       return rc;
+
+out:
+       up_read(&obj->oo_guard);
+       RETURN(rc);
 }
 
-static int capa_is_sane(const struct lu_env *env, struct osd_device *dev,
-                       struct lustre_capa *capa, struct lustre_capa_key *keys)
+static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
+                          __u64 start, __u64 end)
 {
-       struct osd_thread_info  *oti = osd_oti_get(env);
-       struct obd_capa         *oc;
-       int                      i, rc = 0;
+       struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
+       uint64_t txg = 0;
        ENTRY;
 
-       oc = capa_lookup(dev->od_capa_hash, capa, 0);
-       if (oc) {
-               if (capa_is_expired(oc)) {
-                       DEBUG_CAPA(D_ERROR, capa, "expired");
-                       rc = -ESTALE;
-               }
-               capa_put(oc);
-               RETURN(rc);
-       }
-
-       spin_lock(&capa_lock);
-       for (i = 0; i < 2; i++) {
-               if (keys[i].lk_keyid == capa->lc_keyid) {
-                       oti->oti_capa_key = keys[i];
-                       break;
-               }
-       }
-       spin_unlock(&capa_lock);
-
-       if (i == 2) {
-               DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
-               RETURN(-ESTALE);
-       }
+       if (osd->od_dt_dev.dd_rdonly)
+               RETURN(0);
 
-       rc = capa_hmac(oti->oti_capa.lc_hmac, capa, oti->oti_capa_key.lk_key);
-       if (rc)
-               RETURN(rc);
-       if (memcmp(oti->oti_capa.lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac)))
-       {
-               DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
-               RETURN(-EACCES);
+       txg = osd_db_dirty_txg(osd_dt_obj(dt)->oo_dn->dn_dbuf);
+       if (txg) {
+               /* the object is dirty or being synced */
+               if (osd_object_sync_delay_us < 0)
+                       txg_wait_synced(dmu_objset_pool(osd->od_os), txg);
+               else
+                       udelay(osd_object_sync_delay_us);
        }
 
-       oc = capa_add(dev->od_capa_hash, capa);
-       capa_put(oc);
-
        RETURN(0);
 }
 
-static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
-                          struct lustre_capa *capa, __u64 opc)
+static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
 {
-       const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
-       struct osd_device       *dev = osd_dev(dt->do_lu.lo_dev);
-       int                      rc;
-
-       if (!dev->od_fl_capa)
-               return 0;
-
-       if (capa == BYPASS_CAPA)
-               return 0;
-
-       if (!capa) {
-               CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
-               return -EACCES;
-       }
-
-       if (!lu_fid_eq(fid, &capa->lc_fid)) {
-               DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",PFID(fid));
-               return -EACCES;
-       }
-
-       if (!capa_opc_supported(capa, opc)) {
-               DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
-               return -EACCES;
-       }
-
-       if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
-               DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
-               return -EACCES;
-       }
-
        return 0;
 }
 
-static struct obd_capa *osd_capa_get(const struct lu_env *env,
-                                    struct dt_object *dt,
-                                    struct lustre_capa *old,
-                                    __u64 opc)
-{
-       struct osd_thread_info  *info = osd_oti_get(env);
-       const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
-       struct osd_object       *obj = osd_dt_obj(dt);
-       struct osd_device       *dev = osd_obj2dev(obj);
-       struct lustre_capa_key  *key = &info->oti_capa_key;
-       struct lustre_capa      *capa = &info->oti_capa;
-       struct obd_capa         *oc;
-       int                      rc;
-       ENTRY;
-
-       if (!dev->od_fl_capa)
-               RETURN(ERR_PTR(-ENOENT));
-
-       LASSERT(dt_object_exists(dt));
-       LASSERT(osd_invariant(obj));
-
-       /* renewal sanity check */
-       if (old && osd_object_auth(env, dt, old, opc))
-               RETURN(ERR_PTR(-EACCES));
-
-       capa->lc_fid = *fid;
-       capa->lc_opc = opc;
-       capa->lc_uid = 0;
-       capa->lc_flags = dev->od_capa_alg << 24;
-       capa->lc_timeout = dev->od_capa_timeout;
-       capa->lc_expiry = 0;
-
-       oc = capa_lookup(dev->od_capa_hash, capa, 1);
-       if (oc) {
-               LASSERT(!capa_is_expired(oc));
-               RETURN(oc);
-       }
-
-       spin_lock(&capa_lock);
-       *key = dev->od_capa_keys[1];
-       spin_unlock(&capa_lock);
-
-       capa->lc_keyid = key->lk_keyid;
-       capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
-
-       rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
-       if (rc) {
-               DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
-               LBUG();
-               RETURN(ERR_PTR(rc));
-       }
-
-       oc = capa_add(dev->od_capa_hash, capa);
-       RETURN(oc);
-}
-
-static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
-                          __u64 start, __u64 end)
+static bool osd_check_stale(struct dt_object *dt)
 {
-       struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
-       ENTRY;
-
-       /* XXX: no other option than syncing the whole filesystem until we
-        * support ZIL.  If the object tracked the txg that it was last
-        * modified in, it could pass that txg here instead of "0".  Maybe
-        * the changes are already committed, so no wait is needed at all? */
-       txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
-
-       RETURN(0);
+       return false;
 }
 
-static struct dt_object_operations osd_obj_ops = {
-       .do_read_lock           = osd_object_read_lock,
-       .do_write_lock          = osd_object_write_lock,
-       .do_read_unlock         = osd_object_read_unlock,
-       .do_write_unlock        = osd_object_write_unlock,
-       .do_write_locked        = osd_object_write_locked,
+static const struct dt_object_operations osd_obj_ops = {
+       .do_read_lock           = osd_read_lock,
+       .do_write_lock          = osd_write_lock,
+       .do_read_unlock         = osd_read_unlock,
+       .do_write_unlock        = osd_write_unlock,
+       .do_write_locked        = osd_write_locked,
        .do_attr_get            = osd_attr_get,
        .do_declare_attr_set    = osd_declare_attr_set,
        .do_attr_set            = osd_attr_set,
        .do_ah_init             = osd_ah_init,
-       .do_declare_create      = osd_declare_object_create,
-       .do_create              = osd_object_create,
-       .do_declare_destroy     = osd_declare_object_destroy,
-       .do_destroy             = osd_object_destroy,
+       .do_declare_create      = osd_declare_create,
+       .do_create              = osd_create,
+       .do_declare_destroy     = osd_declare_destroy,
+       .do_destroy             = osd_destroy,
        .do_index_try           = osd_index_try,
-       .do_declare_ref_add     = osd_declare_object_ref_add,
-       .do_ref_add             = osd_object_ref_add,
-       .do_declare_ref_del     = osd_declare_object_ref_del,
-       .do_ref_del             = osd_object_ref_del,
+       .do_declare_ref_add     = osd_declare_ref_add,
+       .do_ref_add             = osd_ref_add,
+       .do_declare_ref_del     = osd_declare_ref_del,
+       .do_ref_del             = osd_ref_del,
        .do_xattr_get           = osd_xattr_get,
        .do_declare_xattr_set   = osd_declare_xattr_set,
        .do_xattr_set           = osd_xattr_set,
        .do_declare_xattr_del   = osd_declare_xattr_del,
        .do_xattr_del           = osd_xattr_del,
        .do_xattr_list          = osd_xattr_list,
-       .do_capa_get            = osd_capa_get,
        .do_object_sync         = osd_object_sync,
+       .do_invalidate          = osd_invalidate,
+       .do_check_stale         = osd_check_stale,
 };
 
-static struct lu_object_operations osd_lu_obj_ops = {
+static const struct lu_object_operations osd_lu_obj_ops = {
        .loo_object_init        = osd_object_init,
        .loo_object_delete      = osd_object_delete,
        .loo_object_release     = osd_object_release,
@@ -1794,14 +2200,17 @@ static struct lu_object_operations osd_lu_obj_ops = {
 
 static int osd_otable_it_attr_get(const struct lu_env *env,
                                struct dt_object *dt,
-                               struct lu_attr *attr,
-                               struct lustre_capa *capa)
+                               struct lu_attr *attr)
 {
        attr->la_valid = 0;
        return 0;
 }
 
-static struct dt_object_operations osd_obj_otable_it_ops = {
-        .do_attr_get    = osd_otable_it_attr_get,
-        .do_index_try   = osd_index_try,
+static const struct dt_object_operations osd_obj_otable_it_ops = {
+       .do_attr_get            = osd_otable_it_attr_get,
+       .do_index_try           = osd_index_try,
 };
+
+module_param(osd_object_sync_delay_us, int, 0644);
+MODULE_PARM_DESC(osd_object_sync_delay_us,
+                "If zero or larger delay N usec instead of doing object sync");