Whamcloud - gitweb
LU-7904 osd: honor LOC_F_NEW
[fs/lustre-release.git] / lustre / osd-zfs / osd_object.c
index 6bf8185..fa9ec89 100644 (file)
 /*
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
- */
-/*
- * Copyright (c) 2012, 2013, Intel Corporation.
- * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -138,7 +136,7 @@ void osd_object_sa_dirty_rele(struct osd_thandle *oh)
        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);
+                                struct osd_object, oo_sa_linkage);
                sa_spill_rele(obj->oo_sa_hdl);
                write_lock(&obj->oo_attr_lock);
                list_del_init(&obj->oo_sa_linkage);
@@ -233,6 +231,28 @@ 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 flag from LMA. Right now, only LMAI_ORPHAN
+        * flags is stored in LMA, and it is only for orphan directory */
+       if (S_ISDIR(la->la_mode) && dt_object_exists(&obj->oo_dt)) {
+               struct osd_thread_info *info = osd_oti_get(env);
+               struct lustre_mdt_attrs *lma;
+               struct lu_buf buf;
+
+               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);
+               if (rc > 0) {
+                       rc = 0;
+                       lma->lma_incompat = le32_to_cpu(lma->lma_incompat);
+                       obj->oo_lma_flags =
+                               lma_to_lustre_flags(lma->lma_incompat);
+
+               } else if (rc == -ENODATA) {
+                       rc = 0;
+               }
+       }
+
        if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode)) {
                rc = -sa_lookup(sa_hdl, SA_ZPL_RDEV(o), &osa->rdev, 8);
                if (rc)
@@ -293,9 +313,11 @@ struct lu_object *osd_object_alloc(const struct lu_env *env,
                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;
@@ -308,7 +330,7 @@ struct lu_object *osd_object_alloc(const struct lu_env *env,
 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;
 
@@ -354,7 +376,7 @@ static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
        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);
@@ -395,10 +417,18 @@ static int osd_object_init(const struct lu_env *env, struct lu_object *l,
                RETURN(0);
        }
 
+       if (conf != NULL && conf->loc_flags & LOC_F_NEW)
+               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);
+               /* EEXIST will be returned if object is being deleted in ZFS */
+               if (rc == -EEXIST) {
+                       rc = 0;
+                       GOTO(out, rc);
+               }
                if (rc != 0) {
                        CERROR("%s: lookup "DFID"/"LPX64" failed: rc = %d\n",
                               osd->od_svname, PFID(lu_object_fid(l)), oid, rc);
@@ -434,53 +464,45 @@ static void osd_object_free(const struct lu_env *env, struct lu_object *l)
        OBD_SLAB_FREE_PTR(obj, osd_object_kmem);
 }
 
-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);
-
-               rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
-               if (rc)
-                       goto out;
+       /* the object is supposed to be exclusively locked by
+        * the caller (osd_object_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;
+       }
 
-               while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
-                       BUG_ON(za->za_integer_length != sizeof(uint64_t));
-                       BUG_ON(za->za_num_integers != 1);
+       return rc;
+}
 
-                       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);
+/* 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.");
 
-                       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,
@@ -492,8 +514,8 @@ static int osd_declare_object_destroy(const struct lu_env *env,
        struct osd_object       *obj = osd_dt_obj(dt);
        struct osd_device       *osd = osd_obj2dev(obj);
        struct osd_thandle      *oh;
-       uint64_t                 zapid;
        int                      rc;
+       uint64_t                 zapid;
        ENTRY;
 
        LASSERT(th != NULL);
@@ -502,19 +524,18 @@ static int osd_declare_object_destroy(const struct lu_env *env,
        oh = container_of0(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);
-
        /* 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);
+       dmu_tx_hold_zap(oh->ot_tx, zapid, FALSE, buf);
+
+       osd_declare_xattrs_destroy(env, obj, oh);
 
        /* 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_zap(oh->ot_tx, osd->od_iusr_oid, FALSE, 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);
+       dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, FALSE, buf);
 
        /* one less inode */
        rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
@@ -525,68 +546,21 @@ static int osd_declare_object_destroy(const struct lu_env *env,
        /* 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;
-
-       /* Assert that the transaction has been assigned to a
-          transaction group. */
-       LASSERT(tx->tx_txg != 0);
+       if (rc)
+               RETURN(rc);
 
-       /* 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);
+       osd_object_set_destroy_type(obj);
+       if (obj->oo_destroy == OSD_DESTROY_SYNC)
+               dmu_tx_hold_free(oh->ot_tx, obj->oo_db->db_object,
+                                0, DMU_OBJECT_END);
+       else
+               dmu_tx_hold_zap(oh->ot_tx, osd->od_unlinkedid, TRUE, 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);
-       }
-
-       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)
+                             struct dt_object *dt, struct thandle *th)
 {
        char                    *buf = osd_oti_get(env)->oti_str;
        struct osd_object       *obj = osd_dt_obj(dt);
@@ -594,24 +568,33 @@ static int osd_object_destroy(const struct lu_env *env,
        const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
        struct osd_thandle      *oh;
        int                      rc;
-       uint64_t                 zapid;
+       uint64_t                 oid, zapid;
        ENTRY;
 
+       down_write(&obj->oo_guard);
+
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out, rc = -ENOENT);
+
        LASSERT(obj->oo_db != NULL);
-       LASSERT(dt_object_exists(dt));
-       LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
 
        oh = container_of0(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) */
+       zapid = osd_get_name_n_idx(env, osd, fid, buf);
        rc = -zap_remove(osd->od_os, zapid, buf, oh->ot_tx);
        if (rc) {
-               CERROR("%s: zap_remove() failed: rc = %d\n",
-                      osd->od_svname, rc);
+               CERROR("%s: zap_remove(%s) failed: rc = %d\n",
+                      osd->od_svname, buf, rc);
+               GOTO(out, rc);
+       }
+
+       rc = osd_xattrs_destroy(env, obj, oh);
+       if (rc) {
+               CERROR("%s: cannot destroy xattrs for %s: rc = %d\n",
+                      osd->od_svname, buf, rc);
                GOTO(out, rc);
        }
 
@@ -619,30 +602,52 @@ static int osd_object_destroy(const struct lu_env *env,
         * 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);
+                               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);
+                      " %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);
+                      " %d: rc = %d\n", osd->od_svname, PFID(fid),
+                      obj->oo_attr.la_gid, rc);
+
+       oid = obj->oo_db->db_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 "LPU64": 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 "LPU64": rc = %d\n",
+                              osd->od_svname, buf, oid, rc);
+       } else { /* asynchronous destroy */
+               rc = osd_object_unlinked_add(obj, oh);
+               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);
+               rc = -zap_add_int(osd->od_os, osd->od_unlinkedid,
+                                 oid, oh->ot_tx);
+               if (rc)
+                       CERROR("%s: zap_add_int() failed %s "LPU64": rc = %d\n",
+                              osd->od_svname, buf, oid, 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);
 }
 
@@ -688,7 +693,7 @@ static void osd_object_read_lock(const struct lu_env *env,
 
        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,
@@ -698,7 +703,7 @@ static void osd_object_write_lock(const struct lu_env *env,
 
        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,
@@ -736,19 +741,25 @@ static int osd_object_write_locked(const struct lu_env *env,
 
 static int osd_attr_get(const struct lu_env *env,
                        struct dt_object *dt,
-                       struct lu_attr *attr,
-                       struct lustre_capa *capa)
+                       struct lu_attr *attr)
 {
        struct osd_object       *obj = osd_dt_obj(dt);
        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);
 
-       LASSERT(dt_object_exists(dt));
        LASSERT(osd_invariant(obj));
        LASSERT(obj->oo_db);
 
        read_lock(&obj->oo_attr_lock);
        *attr = obj->oo_attr;
+       if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
+               attr->la_flags |= LUSTRE_ORPHAN_FL;
        read_unlock(&obj->oo_attr_lock);
 
        /* with ZFS_DEBUG zrl_add_debug() called by DB_DNODE_ENTER()
@@ -761,13 +772,16 @@ static int osd_attr_get(const struct lu_env *env,
                attr->la_size = 512 * blocks;
        /* 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
@@ -843,28 +857,31 @@ static int osd_declare_attr_set(const struct lu_env *env,
        struct osd_thandle      *oh;
        uint64_t                 bspace;
        uint32_t                 blksize;
-       int                      rc;
+       int                      rc = 0;
        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);
 
+       down_read(&obj->oo_guard);
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
+               GOTO(out, 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);
        if (oh->ot_tx->tx_err != 0)
-               RETURN(-oh->ot_tx->tx_err);
+               GOTO(out, rc = -oh->ot_tx->tx_err);
 
        sa_object_size(obj->oo_sa_hdl, &blksize, &bspace);
        bspace = toqb(bspace * blksize);
 
+       __osd_xattr_declare_set(env, obj, sizeof(struct lustre_mdt_attrs),
+                               XATTR_NAME_LMA, oh);
+
        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);
@@ -877,7 +894,7 @@ static int osd_declare_attr_set(const struct lu_env *env,
                                          obj->oo_attr.la_uid, attr->la_uid,
                                          bspace, &info->oti_qi);
                        if (rc)
-                               RETURN(rc);
+                               GOTO(out, rc);
                }
        }
        if (attr && attr->la_valid & LA_GID) {
@@ -892,11 +909,13 @@ static int osd_declare_attr_set(const struct lu_env *env,
                                          obj->oo_attr.la_gid, attr->la_gid,
                                          bspace, &info->oti_qi);
                        if (rc)
-                               RETURN(rc);
+                               GOTO(out, rc);
                }
        }
 
-       RETURN(0);
+out:
+       up_read(&obj->oo_guard);
+       RETURN(rc);
 }
 
 /*
@@ -907,21 +926,25 @@ 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);
        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;
+       struct osa_attr         *osa = &info->oti_osa;
        sa_bulk_attr_t          *bulk;
        __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);
 
@@ -934,12 +957,54 @@ static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
        if (!S_ISREG(dt->do_lu.lo_header->loh_attr))
                valid &= ~(LA_SIZE | LA_BLOCKS);
 
+       if (valid & LA_CTIME && la->la_ctime == obj->oo_attr.la_ctime)
+               valid &= ~LA_CTIME;
+
+       if (valid & LA_MTIME && la->la_mtime == obj->oo_attr.la_mtime)
+               valid &= ~LA_MTIME;
+
+       if (valid & LA_ATIME && la->la_atime == obj->oo_attr.la_atime)
+               valid &= ~LA_ATIME;
+
        if (valid == 0)
-               RETURN(0);
+               GOTO(out, rc = 0);
+
+       if (valid & LA_FLAGS) {
+               struct lustre_mdt_attrs *lma;
+               struct lu_buf buf;
+
+               if (la->la_flags & LUSTRE_LMA_FL_MASKS) {
+                       CLASSERT(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);
+                       if (rc > 0) {
+                               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);
+                               RETURN(rc);
+                       }
+               }
+       }
 
        OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 10);
        if (bulk == NULL)
-               RETURN(-ENOMEM);
+               GOTO(out, rc = -ENOMEM);
 
        /* do both accounting updates outside oo_attr_lock below */
        if ((valid & LA_UID) && (la->la_uid != obj->oo_attr.la_uid)) {
@@ -1037,6 +1102,8 @@ static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
        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,7 +1119,6 @@ static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
 {
        LASSERT(ah);
 
-       memset(ah, 0, sizeof(*ah));
        ah->dah_parent = parent;
        ah->dah_mode = child_mode;
 }
@@ -1092,6 +1158,7 @@ static int osd_declare_object_create(const struct lu_env *env,
 
        switch (dof->dof_type) {
                case DFT_DIR:
+                       dt->do_index_ops = &osd_dir_ops;
                case DFT_INDEX:
                        /* for zap create */
                        dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT, 1, NULL);
@@ -1206,19 +1273,28 @@ out:
  * 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_device *osd,
+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)
 {
-       uint64_t oid;
-       int      rc;
+       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);
 
+       /* Use DMU_OTN_UINT8_METADATA for local objects so their data blocks
+        * would get an additional ditto copy */
+       if (unlikely(S_ISREG(la->la_mode) &&
+                    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, DMU_OT_PLAIN_FILE_CONTENTS, 0,
+       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);
@@ -1276,7 +1352,7 @@ int __osd_zap_create(const struct lu_env *env, struct osd_device *osd,
        return __osd_attr_init(env, osd, oid, tx, la, parent);
 }
 
-static dmu_buf_t *osd_mkidx(const struct lu_env *env, struct osd_device *osd,
+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)
 {
@@ -1288,14 +1364,14 @@ static dmu_buf_t *osd_mkidx(const struct lu_env *env, struct osd_device *osd,
         * 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, &db, oh->ot_tx, la, parent,
+       rc = __osd_zap_create(env, osd_obj2dev(obj), &db, oh->ot_tx, la, parent,
                              ZAP_FLAG_UINT64_KEY);
        if (rc)
                return ERR_PTR(rc);
        return db;
 }
 
-static dmu_buf_t *osd_mkdir(const struct lu_env *env, struct osd_device *osd,
+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)
 {
@@ -1303,32 +1379,36 @@ static dmu_buf_t *osd_mkdir(const struct lu_env *env, struct osd_device *osd,
        int        rc;
 
        LASSERT(S_ISDIR(la->la_mode));
-       rc = __osd_zap_create(env, osd, &db, oh->ot_tx, la, parent, 0);
+       rc = __osd_zap_create(env, osd_obj2dev(obj), &db,
+                             oh->ot_tx, la, parent, 0);
        if (rc)
                return ERR_PTR(rc);
        return db;
 }
 
-static dmu_buf_t* osd_mkreg(const struct lu_env *env, struct osd_device *osd,
+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)
 {
-       dmu_buf_t *db;
-       int         rc;
+       dmu_buf_t         *db;
+       int                rc;
+       struct osd_device *osd = osd_obj2dev(obj);
 
        LASSERT(S_ISREG(la->la_mode));
-       rc = __osd_object_create(env, osd, &db, oh->ot_tx, la, parent);
+       rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
        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
+        * XXX: This heuristic is non-optimal.  It would be better to
+        * increase the blocksize up to osd->od_max_blksz during the write.
+        * This is exactly how the ZPL behaves and it ensures that the right
+        * blocksize is selected based on the file size rather than the
+        * making broad assumptions based on the osd type.
         */
        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);
+               rc = -dmu_object_set_blocksize(osd->od_os, db->db_object,
+                                              osd->od_max_blksz, 0, oh->ot_tx);
                if (unlikely(rc)) {
                        CERROR("%s: can't change blocksize: %d\n",
                               osd->od_svname, rc);
@@ -1339,7 +1419,7 @@ static dmu_buf_t* osd_mkreg(const struct lu_env *env, struct osd_device *osd,
        return db;
 }
 
-static dmu_buf_t *osd_mksym(const struct lu_env *env, struct osd_device *osd,
+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)
 {
@@ -1347,13 +1427,13 @@ static dmu_buf_t *osd_mksym(const struct lu_env *env, struct osd_device *osd,
        int        rc;
 
        LASSERT(S_ISLNK(la->la_mode));
-       rc = __osd_object_create(env, osd, &db, oh->ot_tx, la, parent);
+       rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
        if (rc)
                return ERR_PTR(rc);
        return db;
 }
 
-static dmu_buf_t *osd_mknod(const struct lu_env *env, struct osd_device *osd,
+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)
 {
@@ -1364,14 +1444,14 @@ static dmu_buf_t *osd_mknod(const struct lu_env *env, struct osd_device *osd,
        if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode))
                la->la_valid |= LA_RDEV;
 
-       rc = __osd_object_create(env, osd, &db, oh->ot_tx, la, parent);
+       rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
        if (rc)
                return ERR_PTR(rc);
        return db;
 }
 
 typedef dmu_buf_t *(*osd_obj_type_f)(const struct lu_env *env,
-                                    struct osd_device *osd,
+                                    struct osd_object *obj,
                                     struct lu_attr *la,
                                     uint64_t parent,
                                     struct osd_thandle *oh);
@@ -1420,7 +1500,7 @@ static inline int osd_init_lma(const struct lu_env *env, struct osd_object *obj,
        buf.lb_len = sizeof(*lma);
 
        rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
-                                   LU_XATTR_CREATE, oh, BYPASS_CAPA);
+                                   LU_XATTR_CREATE, oh);
 
        return rc;
 }
@@ -1449,10 +1529,12 @@ static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
        /* 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);
@@ -1467,10 +1549,11 @@ static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
        /* to follow ZFS on-disk format we need
         * to initialize parent dnode properly */
        zapid = 0;
-       if (hint && hint->dah_parent)
+       if (hint != NULL && hint->dah_parent != NULL &&
+           !dt_object_remote(hint->dah_parent))
                zapid = osd_dt_obj(hint->dah_parent)->oo_db->db_object;
 
-       db = osd_create_type_f(dof->dof_type)(env, osd, attr, zapid, oh);
+       db = osd_create_type_f(dof->dof_type)(env, obj, attr, zapid, oh);
        if (IS_ERR(db))
                GOTO(out, rc = PTR_ERR(db));
 
@@ -1506,15 +1589,12 @@ static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
        LASSERT(osd_invariant(obj));
 
        rc = osd_init_lma(env, obj, fid, oh);
-       if (rc) {
+       if (rc != 0)
                CERROR("%s: can not set LMA on "DFID": rc = %d\n",
                       osd->od_svname, PFID(fid), rc);
-               /* ignore errors during LMA initialization */
-               rc = 0;
-       }
 
 out:
-       up(&obj->oo_guard);
+       up_write(&obj->oo_guard);
        RETURN(rc);
 }
 
@@ -1540,8 +1620,11 @@ 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);
@@ -1551,7 +1634,10 @@ static int osd_object_ref_add(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 osd_declare_object_ref_del(const struct lu_env *env,
@@ -1576,8 +1662,12 @@ 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);
@@ -1588,146 +1678,10 @@ 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;
-}
-
-static int capa_is_sane(const struct lu_env *env, struct osd_device *dev,
-                       struct lustre_capa *capa, struct lustre_capa_key *keys)
-{
-       struct osd_thread_info  *oti = osd_oti_get(env);
-       struct obd_capa         *oc;
-       int                      i, rc = 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);
-       }
 
-       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);
-       }
-
-       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)
-{
-       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);
+out:
+       up_read(&obj->oo_guard);
+       RETURN(rc);
 }
 
 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
@@ -1770,7 +1724,6 @@ static struct dt_object_operations osd_obj_ops = {
        .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,
 };
 
@@ -1785,8 +1738,7 @@ 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;
@@ -1796,4 +1748,3 @@ static struct dt_object_operations osd_obj_otable_it_ops = {
         .do_attr_get    = osd_otable_it_attr_get,
         .do_index_try   = osd_index_try,
 };
-