Whamcloud - gitweb
LU-3534 osp: move RPC pack from declare to execution phase
[fs/lustre-release.git] / lustre / mdd / mdd_object.c
index 6248eec..0bfd37d 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2011, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -49,8 +49,7 @@
 #include <lprocfs_status.h>
 /* fid_be_cpu(), fid_cpu_to_be(). */
 #include <lustre_fid.h>
-#include <obd_lov.h>
-
+#include <lustre_idmap.h>
 #include <lustre_param.h>
 #include <lustre_mds.h>
 #include <lustre/lustre_idl.h>
 #include "mdd_internal.h"
 
 static const struct lu_object_operations mdd_lu_obj_ops;
-extern cfs_mem_cache_t *mdd_object_kmem;
 
 static int mdd_xattr_get(const struct lu_env *env,
                          struct md_object *obj, struct lu_buf *buf,
                          const char *name);
 
-int mdd_data_get(const struct lu_env *env, struct mdd_object *obj,
-                 void **data)
-{
-        if (mdd_object_exists(obj) == 0) {
-                CERROR("%s: object "DFID" not found: rc = -2\n",
-                       mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
-                return -ENOENT;
-        }
-        mdo_data_get(env, obj, data);
-        return 0;
-}
-
 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
-               struct lu_attr *la, struct lustre_capa *capa)
+              struct lu_attr *la)
 {
         if (mdd_object_exists(obj) == 0) {
                 CERROR("%s: object "DFID" not found: rc = -2\n",
                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
                 return -ENOENT;
         }
-        return mdo_attr_get(env, obj, la, capa);
-}
 
-static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
-{
-        obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
-
-        if (flags & LUSTRE_APPEND_FL)
-                obj->mod_flags |= APPEND_OBJ;
-
-        if (flags & LUSTRE_IMMUTABLE_FL)
-                obj->mod_flags |= IMMUTE_OBJ;
+       return mdo_attr_get(env, obj, la);
 }
 
 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
 {
         struct mdd_thread_info *info;
 
+       lu_env_refill((struct lu_env *)env);
         info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
         LASSERT(info != NULL);
         return info;
@@ -109,96 +86,45 @@ struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
 
 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
 {
-        struct lu_buf *buf;
-
-        buf = &mdd_env_info(env)->mti_buf;
-        buf->lb_buf = area;
-        buf->lb_len = len;
-        return buf;
-}
+       struct lu_buf *buf;
 
-void mdd_buf_put(struct lu_buf *buf)
-{
-       if (buf == NULL || buf->lb_buf == NULL)
-               return;
-       OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
-       *buf = LU_BUF_NULL;
+       buf = &mdd_env_info(env)->mti_buf[0];
+       buf->lb_buf = area;
+       buf->lb_len = len;
+       return buf;
 }
 
 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
                                        const void *area, ssize_t len)
 {
-        struct lu_buf *buf;
-
-        buf = &mdd_env_info(env)->mti_buf;
-        buf->lb_buf = (void *)area;
-        buf->lb_len = len;
-        return buf;
-}
-
-struct lu_buf *mdd_buf_alloc(const struct lu_env *env, ssize_t len)
-{
-       struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
+       struct lu_buf *buf;
 
-       if ((len > buf->lb_len) && (buf->lb_buf != NULL)) {
-               OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
-               *buf = LU_BUF_NULL;
-       }
-       if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
-               buf->lb_len = len;
-               OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
-               if (buf->lb_buf == NULL)
-                       *buf = LU_BUF_NULL;
-       }
+       buf = &mdd_env_info(env)->mti_buf[0];
+       buf->lb_buf = (void *)area;
+       buf->lb_len = len;
        return buf;
 }
 
-/** Increase the size of the \a mti_big_buf.
- * preserves old data in buffer
- * old buffer remains unchanged on error
- * \retval 0 or -ENOMEM
- */
-int mdd_buf_grow(const struct lu_env *env, ssize_t len)
-{
-        struct lu_buf *oldbuf = &mdd_env_info(env)->mti_big_buf;
-        struct lu_buf buf;
-
-        LASSERT(len >= oldbuf->lb_len);
-        OBD_ALLOC_LARGE(buf.lb_buf, len);
-
-        if (buf.lb_buf == NULL)
-                return -ENOMEM;
-
-        buf.lb_len = len;
-        memcpy(buf.lb_buf, oldbuf->lb_buf, oldbuf->lb_len);
-
-        OBD_FREE_LARGE(oldbuf->lb_buf, oldbuf->lb_len);
-
-        memcpy(oldbuf, &buf, sizeof(buf));
-
-        return 0;
-}
-
 struct lu_object *mdd_object_alloc(const struct lu_env *env,
-                                   const struct lu_object_header *hdr,
-                                   struct lu_device *d)
+                                  const struct lu_object_header *hdr,
+                                  struct lu_device *d)
 {
-        struct mdd_object *mdd_obj;
-
-       OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, CFS_ALLOC_IO);
-        if (mdd_obj != NULL) {
-                struct lu_object *o;
-
-                o = mdd2lu_obj(mdd_obj);
-                lu_object_init(o, NULL, d);
-                mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
-                mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
-                mdd_obj->mod_count = 0;
-                o->lo_ops = &mdd_lu_obj_ops;
-                return o;
-        } else {
-                return NULL;
-        }
+       struct mdd_object *mdd_obj;
+
+       OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
+       if (mdd_obj != NULL) {
+               struct lu_object *o;
+
+               o = mdd2lu_obj(mdd_obj);
+               lu_object_init(o, NULL, d);
+               mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
+               mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
+               mdd_obj->mod_count = 0;
+               o->lo_ops = &mdd_lu_obj_ops;
+               return o;
+       } else {
+               return NULL;
+       }
 }
 
 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
@@ -213,7 +139,6 @@ static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
         mdd_obj->mod_cltime = 0;
         under = &d->mdd_child->dd_lu_dev;
         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
-        mdd_pdlock_init(mdd_obj);
        if (IS_ERR(below))
                RETURN(PTR_ERR(below));
 
@@ -224,10 +149,16 @@ static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
 
 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
 {
-       if (lu_object_exists(o))
-                return mdd_get_flags(env, lu2mdd_obj(o));
-        else
-                return 0;
+       int rc = 0;
+
+       if (lu_object_exists(o)) {
+               struct mdd_object *mdd_obj = lu2mdd_obj(o);
+               struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
+
+               rc = mdd_la_get(env, mdd_obj, attr);
+       }
+
+       return rc;
 }
 
 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
@@ -262,271 +193,22 @@ struct mdd_object *mdd_object_find(const struct lu_env *env,
         return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
 }
 
-static int mdd_path2fid(const struct lu_env *env, struct mdd_device *mdd,
-                        const char *path, struct lu_fid *fid)
-{
-        struct lu_buf *buf;
-        struct lu_fid *f = &mdd_env_info(env)->mti_fid;
-        struct mdd_object *obj;
-        struct lu_name *lname = &mdd_env_info(env)->mti_name;
-        char *name;
-        int rc = 0;
-        ENTRY;
-
-        /* temp buffer for path element */
-        buf = mdd_buf_alloc(env, PATH_MAX);
-        if (buf->lb_buf == NULL)
-                RETURN(-ENOMEM);
-
-        lname->ln_name = name = buf->lb_buf;
-        lname->ln_namelen = 0;
-        *f = mdd->mdd_root_fid;
-
-        while(1) {
-                while (*path == '/')
-                        path++;
-                if (*path == '\0')
-                        break;
-                while (*path != '/' && *path != '\0') {
-                        *name = *path;
-                        path++;
-                        name++;
-                        lname->ln_namelen++;
-                }
-
-                *name = '\0';
-                /* find obj corresponding to fid */
-                obj = mdd_object_find(env, mdd, f);
-                if (obj == NULL)
-                        GOTO(out, rc = -EREMOTE);
-                if (IS_ERR(obj))
-                        GOTO(out, rc = PTR_ERR(obj));
-                /* get child fid from parent and name */
-                rc = mdd_lookup(env, &obj->mod_obj, lname, f, NULL);
-                mdd_object_put(env, obj);
-                if (rc)
-                        break;
-
-                name = buf->lb_buf;
-                lname->ln_namelen = 0;
-        }
-
-        if (!rc)
-                *fid = *f;
-out:
-        RETURN(rc);
-}
-
-/** The maximum depth that fid2path() will search.
- * This is limited only because we want to store the fids for
- * historical path lookup purposes.
- */
-#define MAX_PATH_DEPTH 100
-
-/** mdd_path() lookup structure. */
-struct path_lookup_info {
-        __u64                pli_recno;        /**< history point */
-        __u64                pli_currec;       /**< current record */
-        struct lu_fid        pli_fid;
-        struct lu_fid        pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
-        struct mdd_object   *pli_mdd_obj;
-        char                *pli_path;         /**< full path */
-        int                  pli_pathlen;
-        int                  pli_linkno;       /**< which hardlink to follow */
-        int                  pli_fidcount;     /**< number of \a pli_fids */
-};
-
-static int mdd_path_current(const struct lu_env *env,
-                            struct path_lookup_info *pli)
-{
-        struct mdd_device *mdd = mdo2mdd(&pli->pli_mdd_obj->mod_obj);
-        struct mdd_object *mdd_obj;
-        struct lu_buf     *buf = NULL;
-        struct link_ea_header *leh;
-        struct link_ea_entry  *lee;
-        struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
-        struct lu_fid  *tmpfid = &mdd_env_info(env)->mti_fid;
-        char *ptr;
-        int reclen;
-        int rc;
-        ENTRY;
-
-        ptr = pli->pli_path + pli->pli_pathlen - 1;
-        *ptr = 0;
-        --ptr;
-        pli->pli_fidcount = 0;
-        pli->pli_fids[0] = *(struct lu_fid *)mdd_object_fid(pli->pli_mdd_obj);
-
-        while (!mdd_is_root(mdd, &pli->pli_fids[pli->pli_fidcount])) {
-                mdd_obj = mdd_object_find(env, mdd,
-                                          &pli->pli_fids[pli->pli_fidcount]);
-                if (mdd_obj == NULL)
-                        GOTO(out, rc = -EREMOTE);
-                if (IS_ERR(mdd_obj))
-                        GOTO(out, rc = PTR_ERR(mdd_obj));
-                rc = lu_object_exists(&mdd_obj->mod_obj.mo_lu);
-                if (rc <= 0) {
-                        mdd_object_put(env, mdd_obj);
-                        if (rc == -1)
-                                rc = -EREMOTE;
-                        else if (rc == 0)
-                                /* Do I need to error out here? */
-                                rc = -ENOENT;
-                        GOTO(out, rc);
-                }
-
-                /* Get parent fid and object name */
-                mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
-                buf = mdd_links_get(env, mdd_obj);
-                mdd_read_unlock(env, mdd_obj);
-                mdd_object_put(env, mdd_obj);
-                if (IS_ERR(buf))
-                        GOTO(out, rc = PTR_ERR(buf));
-
-                leh = buf->lb_buf;
-                lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
-                mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
-
-                /* If set, use link #linkno for path lookup, otherwise use
-                   link #0.  Only do this for the final path element. */
-                if ((pli->pli_fidcount == 0) &&
-                    (pli->pli_linkno < leh->leh_reccount)) {
-                        int count;
-                        for (count = 0; count < pli->pli_linkno; count++) {
-                                lee = (struct link_ea_entry *)
-                                     ((char *)lee + reclen);
-                                mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
-                        }
-                        if (pli->pli_linkno < leh->leh_reccount - 1)
-                                /* indicate to user there are more links */
-                                pli->pli_linkno++;
-                }
-
-                /* Pack the name in the end of the buffer */
-                ptr -= tmpname->ln_namelen;
-                if (ptr - 1 <= pli->pli_path)
-                        GOTO(out, rc = -EOVERFLOW);
-                strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
-                *(--ptr) = '/';
-
-                /* Store the parent fid for historic lookup */
-                if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
-                        GOTO(out, rc = -EOVERFLOW);
-                pli->pli_fids[pli->pli_fidcount] = *tmpfid;
-        }
-
-        /* Verify that our path hasn't changed since we started the lookup.
-           Record the current index, and verify the path resolves to the
-           same fid. If it does, then the path is correct as of this index. */
-       spin_lock(&mdd->mdd_cl.mc_lock);
-       pli->pli_currec = mdd->mdd_cl.mc_index;
-       spin_unlock(&mdd->mdd_cl.mc_lock);
-        rc = mdd_path2fid(env, mdd, ptr, &pli->pli_fid);
-        if (rc) {
-                CDEBUG(D_INFO, "mdd_path2fid(%s) failed %d\n", ptr, rc);
-                GOTO (out, rc = -EAGAIN);
-        }
-        if (!lu_fid_eq(&pli->pli_fids[0], &pli->pli_fid)) {
-                CDEBUG(D_INFO, "mdd_path2fid(%s) found another FID o="DFID
-                       " n="DFID"\n", ptr, PFID(&pli->pli_fids[0]),
-                       PFID(&pli->pli_fid));
-                GOTO(out, rc = -EAGAIN);
-        }
-        ptr++; /* skip leading / */
-        memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
-
-        EXIT;
-out:
-        if (buf && !IS_ERR(buf) && buf->lb_len > OBD_ALLOC_BIG)
-                /* if we vmalloced a large buffer drop it */
-                mdd_buf_put(buf);
-
-        return rc;
-}
-
-static int mdd_path_historic(const struct lu_env *env,
-                             struct path_lookup_info *pli)
-{
-        return 0;
-}
-
-/* Returns the full path to this fid, as of changelog record recno. */
-static int mdd_path(const struct lu_env *env, struct md_object *obj,
-                    char *path, int pathlen, __u64 *recno, int *linkno)
-{
-        struct path_lookup_info *pli;
-        int tries = 3;
-        int rc = -EAGAIN;
-        ENTRY;
-
-        if (pathlen < 3)
-                RETURN(-EOVERFLOW);
-
-        if (mdd_is_root(mdo2mdd(obj), mdd_object_fid(md2mdd_obj(obj)))) {
-                path[0] = '\0';
-                RETURN(0);
-        }
-
-        OBD_ALLOC_PTR(pli);
-        if (pli == NULL)
-                RETURN(-ENOMEM);
-
-        pli->pli_mdd_obj = md2mdd_obj(obj);
-        pli->pli_recno = *recno;
-        pli->pli_path = path;
-        pli->pli_pathlen = pathlen;
-        pli->pli_linkno = *linkno;
-
-        /* Retry multiple times in case file is being moved */
-        while (tries-- && rc == -EAGAIN)
-                rc = mdd_path_current(env, pli);
-
-        /* For historical path lookup, the current links may not have existed
-         * at "recno" time.  We must switch over to earlier links/parents
-         * by using the changelog records.  If the earlier parent doesn't
-         * exist, we must search back through the changelog to reconstruct
-         * its parents, then check if it exists, etc.
-         * We may ignore this problem for the initial implementation and
-         * state that an "original" hardlink must still exist for us to find
-         * historic path name. */
-        if (pli->pli_recno != -1) {
-                rc = mdd_path_historic(env, pli);
-        } else {
-                *recno = pli->pli_currec;
-                /* Return next link index to caller */
-                *linkno = pli->pli_linkno;
-        }
-
-        OBD_FREE_PTR(pli);
-
-        RETURN (rc);
-}
-
-int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj)
-{
-        struct lu_attr *la = &mdd_env_info(env)->mti_la;
-        int rc;
-
-        ENTRY;
-        rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
-        if (rc == 0) {
-                mdd_flags_xlate(obj, la->la_flags);
-        }
-        RETURN(rc);
-}
-
 /*
  * No permission check is needed.
  */
 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
                 struct md_attr *ma)
 {
-       int rc;
-        ENTRY;
+       struct mdd_object *mdd_obj = md2mdd_obj(obj);
+       int               rc;
 
-       return mdd_la_get(env, md2mdd_obj(obj), &ma->ma_attr,
-                         mdd_object_capa(env, md2mdd_obj(obj)));
-        RETURN(rc);
+       ENTRY;
+
+       rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
+       if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
+               ma->ma_attr.la_nlink = 0;
+
+       RETURN(rc);
 }
 
 /*
@@ -547,9 +229,14 @@ static int mdd_xattr_get(const struct lu_env *env,
                 return -ENOENT;
         }
 
+       /* If the object has been delete from the namespace, then
+        * get linkEA should return -ENOENT as well */
+       if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
+                     strcmp(name, XATTR_NAME_LINK) == 0))
+               RETURN(-ENOENT);
+
         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
-        rc = mdo_xattr_get(env, mdd_obj, buf, name,
-                           mdd_object_capa(env, mdd_obj));
+       rc = mdo_xattr_get(env, mdd_obj, buf, name);
         mdd_read_unlock(env, mdd_obj);
 
         RETURN(rc);
@@ -559,8 +246,8 @@ static int mdd_xattr_get(const struct lu_env *env,
  * Permission check is done when open,
  * no need check again.
  */
-static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
-                        struct lu_buf *buf)
+int mdd_readlink(const struct lu_env *env, struct md_object *obj,
+                struct lu_buf *buf)
 {
         struct mdd_object *mdd_obj = md2mdd_obj(obj);
         struct dt_object  *next;
@@ -575,9 +262,11 @@ static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
         }
 
         next = mdd_object_child(mdd_obj);
-        mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
-        rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
-                                         mdd_object_capa(env, mdd_obj));
+       LASSERT(next != NULL);
+       LASSERT(next->do_body_ops != NULL);
+       LASSERT(next->do_body_ops->dbo_read != NULL);
+       mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
+       rc = dt_read(env, next, buf, &pos);
         mdd_read_unlock(env, mdd_obj);
         RETURN(rc);
 }
@@ -594,10 +283,38 @@ static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
         ENTRY;
 
         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
-        rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
+       rc = mdo_xattr_list(env, mdd_obj, buf);
         mdd_read_unlock(env, mdd_obj);
 
-        RETURN(rc);
+       if (rc < 0)
+               RETURN(rc);
+
+       /*
+        * Filter out XATTR_NAME_LINK if this is an orphan object.  See
+        * mdd_xattr_get().
+        */
+       if (unlikely(mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ))) {
+               char   *end = (char *)buf->lb_buf + rc;
+               char   *p = buf->lb_buf;
+
+               while (p < end) {
+                       char   *next = p + strlen(p) + 1;
+
+                       if (strcmp(p, XATTR_NAME_LINK) == 0) {
+                               if (end - next > 0)
+                                       memmove(p, next, end - next);
+                               rc -= next - p;
+                               CDEBUG(D_INFO, "Filtered out "XATTR_NAME_LINK
+                                      " of orphan "DFID"\n",
+                                      PFID(mdd_object_fid(mdd_obj)));
+                               break;
+                       }
+
+                       p = next;
+               }
+       }
+
+       RETURN(rc);
 }
 
 int mdd_declare_object_create_internal(const struct lu_env *env,
@@ -605,10 +322,10 @@ int mdd_declare_object_create_internal(const struct lu_env *env,
                                       struct mdd_object *c,
                                       struct lu_attr *attr,
                                       struct thandle *handle,
-                                      const struct md_op_spec *spec)
+                                      const struct md_op_spec *spec,
+                                      struct dt_allocation_hint *hint)
 {
         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
-       struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
         const struct dt_index_features *feat = spec->sp_feat;
         int rc;
         ENTRY;
@@ -637,10 +354,10 @@ int mdd_declare_object_create_internal(const struct lu_env *env,
 
 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
                               struct mdd_object *c, struct lu_attr *attr,
-                               struct thandle *handle,
-                               const struct md_op_spec *spec)
+                              struct thandle *handle,
+                              const struct md_op_spec *spec,
+                              struct dt_allocation_hint *hint)
 {
-        struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
         int rc;
         ENTRY;
@@ -649,44 +366,17 @@ int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
 
        rc = mdo_create_obj(env, c, attr, hint, dof, handle);
 
-       LASSERT(ergo(rc == 0, mdd_object_exists(c)));
-
        RETURN(rc);
 }
 
-/**
- * Make sure the ctime is increased only.
- */
-static inline int mdd_attr_check(const struct lu_env *env,
-                                 struct mdd_object *obj,
-                                 struct lu_attr *attr)
-{
-        struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
-        int rc;
-        ENTRY;
-
-        if (attr->la_valid & LA_CTIME) {
-                rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
-                if (rc)
-                        RETURN(rc);
-
-                if (attr->la_ctime < tmp_la->la_ctime)
-                        attr->la_valid &= ~(LA_MTIME | LA_CTIME);
-                else if (attr->la_valid == LA_CTIME &&
-                         attr->la_ctime == tmp_la->la_ctime)
-                        attr->la_valid &= ~LA_CTIME;
-        }
-        RETURN(0);
-}
-
 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
-                         struct lu_attr *attr, struct thandle *handle,
+                         const struct lu_attr *attr, struct thandle *handle,
                          int needacl)
 {
         int rc;
         ENTRY;
 
-        rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
+       rc = mdo_attr_set(env, obj, attr, handle);
 #ifdef CONFIG_FS_POSIX_ACL
         if (!rc && (attr->la_valid & LA_MODE) && needacl)
                 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
@@ -694,20 +384,29 @@ int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
         RETURN(rc);
 }
 
-int mdd_attr_check_set_internal(const struct lu_env *env,
-                               struct mdd_object *obj, struct lu_attr *attr,
-                               struct thandle *handle, int needacl)
+int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
+                   const struct lu_attr *oattr, struct lu_attr *attr,
+                   struct thandle *handle)
 {
-        int rc;
-        ENTRY;
-
-        rc = mdd_attr_check(env, obj, attr);
-        if (rc)
-                RETURN(rc);
+       int rc = 0;
+       ENTRY;
 
-        if (attr->la_valid)
-                rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
-        RETURN(rc);
+       LASSERT(attr->la_valid & LA_CTIME);
+       LASSERT(oattr != NULL);
+
+       /* Make sure the ctime is increased only, however, it's not strictly
+        * reliable at here because there is not guarantee to hold lock on
+        * object, so we just bypass some unnecessary cmtime setting first
+        * and OSD has to check it again. */
+       if (attr->la_ctime < oattr->la_ctime)
+               attr->la_valid &= ~(LA_MTIME | LA_CTIME);
+       else if (attr->la_valid == LA_CTIME &&
+                attr->la_ctime == oattr->la_ctime)
+               attr->la_valid &= ~LA_CTIME;
+
+       if (attr->la_valid != 0)
+               rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
+       RETURN(rc);
 }
 
 /*
@@ -718,23 +417,25 @@ int mdd_attr_check_set_internal(const struct lu_env *env,
  * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
  */
 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
-                       struct lu_attr *la, const unsigned long flags)
+                       const struct lu_attr *oattr, struct lu_attr *la,
+                       const unsigned long flags)
 {
-        struct lu_attr   *tmp_la     = &mdd_env_info(env)->mti_la;
        struct lu_ucred  *uc;
-        int               rc;
-        ENTRY;
+       int               rc = 0;
+       ENTRY;
 
-        if (!la->la_valid)
-                RETURN(0);
+       if (!la->la_valid)
+               RETURN(0);
 
-        /* Do not permit change file type */
-        if (la->la_valid & LA_TYPE)
-                RETURN(-EPERM);
+       /* Do not permit change file type */
+       if (la->la_valid & LA_TYPE)
+               RETURN(-EPERM);
 
-        /* They should not be processed by setattr */
-        if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
-                RETURN(-EPERM);
+       /* They should not be processed by setattr */
+       if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
+               RETURN(-EPERM);
+
+       LASSERT(oattr != NULL);
 
        /* export destroy does not have ->le_ses, but we may want
         * to drop LUSTRE_SOM_FL. */
@@ -742,202 +443,196 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
        if (uc == NULL)
                RETURN(0);
 
-        rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
-        if (rc)
-                RETURN(rc);
-
-        if (la->la_valid == LA_CTIME) {
+       if (la->la_valid == LA_CTIME) {
                if (!(flags & MDS_PERM_BYPASS))
-                        /* This is only for set ctime when rename's source is
-                         * on remote MDS. */
-                       rc = mdd_may_delete(env, NULL, obj, tmp_la, NULL, 1, 0);
-                if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
-                        la->la_valid &= ~LA_CTIME;
-                RETURN(rc);
-        }
+                       /* This is only for set ctime when rename's source is
+                        * on remote MDS. */
+                       rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
+                                           1, 0);
+               if (rc == 0 && la->la_ctime <= oattr->la_ctime)
+                       la->la_valid &= ~LA_CTIME;
+               RETURN(rc);
+       }
 
-        if (la->la_valid == LA_ATIME) {
-                /* This is atime only set for read atime update on close. */
-                if (la->la_atime >= tmp_la->la_atime &&
-                    la->la_atime < (tmp_la->la_atime +
-                                    mdd_obj2mdd_dev(obj)->mdd_atime_diff))
-                        la->la_valid &= ~LA_ATIME;
-                RETURN(0);
-        }
+       if (la->la_valid == LA_ATIME) {
+               /* This is atime only set for read atime update on close. */
+               if (la->la_atime >= oattr->la_atime &&
+                   la->la_atime < (oattr->la_atime +
+                               mdd_obj2mdd_dev(obj)->mdd_atime_diff))
+                       la->la_valid &= ~LA_ATIME;
+               RETURN(0);
+       }
 
-        /* Check if flags change. */
-        if (la->la_valid & LA_FLAGS) {
-                unsigned int oldflags = 0;
-                unsigned int newflags = la->la_flags &
-                                (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
+       /* Check if flags change. */
+       if (la->la_valid & LA_FLAGS) {
+               unsigned int oldflags = oattr->la_flags &
+                               (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
+               unsigned int newflags = la->la_flags &
+                               (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
 
-               if ((uc->uc_fsuid != tmp_la->la_uid) &&
+               if ((uc->uc_fsuid != oattr->la_uid) &&
                    !md_capable(uc, CFS_CAP_FOWNER))
                        RETURN(-EPERM);
 
-               /* XXX: the IMMUTABLE and APPEND_ONLY flags can
+               /* The IMMUTABLE and APPEND_ONLY flags can
                 * only be changed by the relevant capability. */
-               if (mdd_is_immutable(obj))
-                       oldflags |= LUSTRE_IMMUTABLE_FL;
-               if (mdd_is_append(obj))
-                       oldflags |= LUSTRE_APPEND_FL;
                if ((oldflags ^ newflags) &&
                    !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
                        RETURN(-EPERM);
 
-                if (!S_ISDIR(tmp_la->la_mode))
-                        la->la_flags &= ~LUSTRE_DIRSYNC_FL;
-        }
+               if (!S_ISDIR(oattr->la_mode))
+                       la->la_flags &= ~LUSTRE_DIRSYNC_FL;
+       }
 
-        if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
-            (la->la_valid & ~LA_FLAGS) &&
+       if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
+           (la->la_valid & ~LA_FLAGS) &&
            !(flags & MDS_PERM_BYPASS))
-                RETURN(-EPERM);
+               RETURN(-EPERM);
 
        /* Check for setting the obj time. */
        if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
            !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
-               if ((uc->uc_fsuid != tmp_la->la_uid) &&
+               if ((uc->uc_fsuid != oattr->la_uid) &&
                    !md_capable(uc, CFS_CAP_FOWNER)) {
-                       rc = mdd_permission_internal(env, obj, tmp_la,
+                       rc = mdd_permission_internal(env, obj, oattr,
                                                     MAY_WRITE);
                        if (rc)
                                RETURN(rc);
                }
        }
 
-        if (la->la_valid & LA_KILL_SUID) {
-                la->la_valid &= ~LA_KILL_SUID;
-                if ((tmp_la->la_mode & S_ISUID) &&
-                    !(la->la_valid & LA_MODE)) {
-                        la->la_mode = tmp_la->la_mode;
-                        la->la_valid |= LA_MODE;
-                }
-                la->la_mode &= ~S_ISUID;
-        }
+       if (la->la_valid & LA_KILL_SUID) {
+               la->la_valid &= ~LA_KILL_SUID;
+               if ((oattr->la_mode & S_ISUID) &&
+                   !(la->la_valid & LA_MODE)) {
+                       la->la_mode = oattr->la_mode;
+                       la->la_valid |= LA_MODE;
+               }
+               la->la_mode &= ~S_ISUID;
+       }
 
-        if (la->la_valid & LA_KILL_SGID) {
-                la->la_valid &= ~LA_KILL_SGID;
-                if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
-                                        (S_ISGID | S_IXGRP)) &&
-                    !(la->la_valid & LA_MODE)) {
-                        la->la_mode = tmp_la->la_mode;
-                        la->la_valid |= LA_MODE;
-                }
-                la->la_mode &= ~S_ISGID;
-        }
+       if (la->la_valid & LA_KILL_SGID) {
+               la->la_valid &= ~LA_KILL_SGID;
+               if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
+                       (S_ISGID | S_IXGRP)) &&
+                   !(la->la_valid & LA_MODE)) {
+                       la->la_mode = oattr->la_mode;
+                       la->la_valid |= LA_MODE;
+               }
+               la->la_mode &= ~S_ISGID;
+       }
 
-        /* Make sure a caller can chmod. */
-        if (la->la_valid & LA_MODE) {
+       /* Make sure a caller can chmod. */
+       if (la->la_valid & LA_MODE) {
                if (!(flags & MDS_PERM_BYPASS) &&
-                   (uc->uc_fsuid != tmp_la->la_uid) &&
+                   (uc->uc_fsuid != oattr->la_uid) &&
                    !md_capable(uc, CFS_CAP_FOWNER))
                        RETURN(-EPERM);
 
-                if (la->la_mode == (cfs_umode_t) -1)
-                        la->la_mode = tmp_la->la_mode;
-                else
-                        la->la_mode = (la->la_mode & S_IALLUGO) |
-                                      (tmp_la->la_mode & ~S_IALLUGO);
+               if (la->la_mode == (umode_t) -1)
+                       la->la_mode = oattr->la_mode;
+               else
+                       la->la_mode = (la->la_mode & S_IALLUGO) |
+                                       (oattr->la_mode & ~S_IALLUGO);
 
                /* Also check the setgid bit! */
                if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
-                                      la->la_gid : tmp_la->la_gid) &&
+                                      la->la_gid : oattr->la_gid) &&
                    !md_capable(uc, CFS_CAP_FSETID))
                        la->la_mode &= ~S_ISGID;
        } else {
-              la->la_mode = tmp_la->la_mode;
+              la->la_mode = oattr->la_mode;
        }
 
-        /* Make sure a caller can chown. */
-        if (la->la_valid & LA_UID) {
-                if (la->la_uid == (uid_t) -1)
-                        la->la_uid = tmp_la->la_uid;
-               if (((uc->uc_fsuid != tmp_la->la_uid) ||
-                    (la->la_uid != tmp_la->la_uid)) &&
+       /* Make sure a caller can chown. */
+       if (la->la_valid & LA_UID) {
+               if (la->la_uid == (uid_t) -1)
+                       la->la_uid = oattr->la_uid;
+               if (((uc->uc_fsuid != oattr->la_uid) ||
+                    (la->la_uid != oattr->la_uid)) &&
                    !md_capable(uc, CFS_CAP_CHOWN))
                        RETURN(-EPERM);
 
-                /* If the user or group of a non-directory has been
-                 * changed by a non-root user, remove the setuid bit.
-                 * 19981026 David C Niemi <niemi@tux.org>
-                 *
-                 * Changed this to apply to all users, including root,
-                 * to avoid some races. This is the behavior we had in
-                 * 2.0. The check for non-root was definitely wrong
-                 * for 2.2 anyway, as it should have been using
-                 * CAP_FSETID rather than fsuid -- 19990830 SD. */
-                if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
-                    !S_ISDIR(tmp_la->la_mode)) {
-                        la->la_mode &= ~S_ISUID;
-                        la->la_valid |= LA_MODE;
-                }
-        }
+               /* If the user or group of a non-directory has been
+                * changed by a non-root user, remove the setuid bit.
+                * 19981026 David C Niemi <niemi@tux.org>
+                *
+                * Changed this to apply to all users, including root,
+                * to avoid some races. This is the behavior we had in
+                * 2.0. The check for non-root was definitely wrong
+                * for 2.2 anyway, as it should have been using
+                * CAP_FSETID rather than fsuid -- 19990830 SD. */
+               if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
+               !S_ISDIR(oattr->la_mode)) {
+                       la->la_mode &= ~S_ISUID;
+                       la->la_valid |= LA_MODE;
+               }
+       }
 
-        /* Make sure caller can chgrp. */
-        if (la->la_valid & LA_GID) {
-                if (la->la_gid == (gid_t) -1)
-                        la->la_gid = tmp_la->la_gid;
-               if (((uc->uc_fsuid != tmp_la->la_uid) ||
-                    ((la->la_gid != tmp_la->la_gid) &&
+       /* Make sure caller can chgrp. */
+       if (la->la_valid & LA_GID) {
+               if (la->la_gid == (gid_t) -1)
+                       la->la_gid = oattr->la_gid;
+               if (((uc->uc_fsuid != oattr->la_uid) ||
+                    ((la->la_gid != oattr->la_gid) &&
                      !lustre_in_group_p(uc, la->la_gid))) &&
                    !md_capable(uc, CFS_CAP_CHOWN))
                        RETURN(-EPERM);
 
-                /* Likewise, if the user or group of a non-directory
-                 * has been changed by a non-root user, remove the
-                 * setgid bit UNLESS there is no group execute bit
-                 * (this would be a file marked for mandatory
-                 * locking).  19981026 David C Niemi <niemi@tux.org>
-                 *
-                 * Removed the fsuid check (see the comment above) --
-                 * 19990830 SD. */
-                if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
-                     (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
-                        la->la_mode &= ~S_ISGID;
-                        la->la_valid |= LA_MODE;
-                }
-        }
+               /* Likewise, if the user or group of a non-directory
+                * has been changed by a non-root user, remove the
+                * setgid bit UNLESS there is no group execute bit
+                * (this would be a file marked for mandatory
+                * locking).  19981026 David C Niemi <niemi@tux.org>
+                *
+                * Removed the fsuid check (see the comment above) --
+                * 19990830 SD. */
+               if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
+                   (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
+                       la->la_mode &= ~S_ISGID;
+                       la->la_valid |= LA_MODE;
+               }
+       }
 
-        /* For both Size-on-MDS case and truncate case,
-         * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
+       /* For both Size-on-MDS case and truncate case,
+        * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
         * We distinguish them by "flags & MDS_SOM".
-         * For SOM case, it is true, the MAY_WRITE perm has been checked
-         * when open, no need check again. For truncate case, it is false,
-         * the MAY_WRITE perm should be checked here. */
+        * For SOM case, it is true, the MAY_WRITE perm has been checked
+        * when open, no need check again. For truncate case, it is false,
+        * the MAY_WRITE perm should be checked here. */
        if (flags & MDS_SOM) {
-                /* For the "Size-on-MDS" setattr update, merge coming
-                 * attributes with the set in the inode. BUG 10641 */
-                if ((la->la_valid & LA_ATIME) &&
-                    (la->la_atime <= tmp_la->la_atime))
-                        la->la_valid &= ~LA_ATIME;
-
-                /* OST attributes do not have a priority over MDS attributes,
-                 * so drop times if ctime is equal. */
-                if ((la->la_valid & LA_CTIME) &&
-                    (la->la_ctime <= tmp_la->la_ctime))
-                        la->la_valid &= ~(LA_MTIME | LA_CTIME);
-        } else {
-                if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
+               /* For the "Size-on-MDS" setattr update, merge coming
+                * attributes with the set in the inode. BUG 10641 */
+               if ((la->la_valid & LA_ATIME) &&
+                   (la->la_atime <= oattr->la_atime))
+                       la->la_valid &= ~LA_ATIME;
+
+               /* OST attributes do not have a priority over MDS attributes,
+                * so drop times if ctime is equal. */
+               if ((la->la_valid & LA_CTIME) &&
+                   (la->la_ctime <= oattr->la_ctime))
+                       la->la_valid &= ~(LA_MTIME | LA_CTIME);
+       } else {
+               if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
                        if (!((flags & MDS_OWNEROVERRIDE) &&
-                             (uc->uc_fsuid == tmp_la->la_uid)) &&
+                             (uc->uc_fsuid == oattr->la_uid)) &&
                            !(flags & MDS_PERM_BYPASS)) {
                                rc = mdd_permission_internal(env, obj,
-                                                            tmp_la, MAY_WRITE);
+                                                            oattr, MAY_WRITE);
                                if (rc != 0)
                                        RETURN(rc);
                        }
-                }
-                if (la->la_valid & LA_CTIME) {
-                        /* The pure setattr, it has the priority over what is
-                         * already set, do not drop it if ctime is equal. */
-                        if (la->la_ctime < tmp_la->la_ctime)
-                                la->la_valid &= ~(LA_ATIME | LA_MTIME |
-                                                  LA_CTIME);
-                }
-        }
+               }
+               if (la->la_valid & LA_CTIME) {
+                       /* The pure setattr, it has the priority over what is
+                        * already set, do not drop it if ctime is equal. */
+                       if (la->la_ctime < oattr->la_ctime)
+                               la->la_valid &= ~(LA_ATIME | LA_MTIME |
+                                                       LA_CTIME);
+               }
+       }
 
-        RETURN(0);
+       RETURN(0);
 }
 
 /** Store a data change changelog record
@@ -946,12 +641,11 @@ static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
  * \param mdd_obj - mdd_object of change
  * \param handle - transacion handle
  */
-static int mdd_changelog_data_store(const struct lu_env *env,
-                                   struct mdd_device *mdd,
-                                   enum changelog_rec_type type,
-                                   int flags, struct mdd_object *mdd_obj,
-                                   struct thandle *handle)
+int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
+                            enum changelog_rec_type type, int flags,
+                            struct mdd_object *mdd_obj, struct thandle *handle)
 {
+       const struct lu_ucred           *uc = lu_ucred(env);
        const struct lu_fid             *tfid;
        struct llog_changelog_rec       *rec;
        struct lu_buf                   *buf;
@@ -977,25 +671,32 @@ static int mdd_changelog_data_store(const struct lu_env *env,
                 RETURN(0);
         }
 
-        reclen = llog_data_len(sizeof(*rec));
-        buf = mdd_buf_alloc(env, reclen);
-        if (buf->lb_buf == NULL)
-                RETURN(-ENOMEM);
+       flags = (flags & CLF_FLAGMASK) | CLF_VERSION;
+       if (uc != NULL && uc->uc_jobid[0] != '\0')
+               flags |= CLF_JOBID;
+
+       reclen = llog_data_len(changelog_rec_offset(flags & CLF_SUPPORTED));
+       buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
+       if (buf->lb_buf == NULL)
+               RETURN(-ENOMEM);
        rec = buf->lb_buf;
 
-        rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
-        rec->cr.cr_type = (__u32)type;
-        rec->cr.cr_tfid = *tfid;
-        rec->cr.cr_namelen = 0;
-        mdd_obj->mod_cltime = cfs_time_current_64();
+       rec->cr.cr_flags = flags;
+       rec->cr.cr_type = (__u32)type;
+       rec->cr.cr_tfid = *tfid;
+       rec->cr.cr_namelen = 0;
+       mdd_obj->mod_cltime = cfs_time_current_64();
+
+       if (flags & CLF_JOBID)
+               mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
 
        rc = mdd_changelog_store(env, mdd, rec, handle);
 
        RETURN(rc);
 }
 
-int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
-                  int flags, struct md_object *obj)
+static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
+                        int flags, struct md_object *obj)
 {
         struct thandle *handle;
         struct mdd_object *mdd_obj = md2mdd_obj(obj);
@@ -1007,9 +708,9 @@ int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
         if (IS_ERR(handle))
                RETURN(PTR_ERR(handle));
 
-        rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
-        if (rc)
-                GOTO(stop, rc);
+       rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
+       if (rc)
+               GOTO(stop, rc);
 
         rc = mdd_trans_start(env, mdd, handle);
         if (rc)
@@ -1042,19 +743,19 @@ static int mdd_attr_set_changelog(const struct lu_env *env,
         struct mdd_device *mdd = mdo2mdd(obj);
         int bits, type = 0;
 
-        bits = (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
+       bits =  (valid & LA_SIZE)  ? 1 << CL_TRUNC : 0;
+       bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
         bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
         bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
         bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
         bits = bits & mdd->mdd_cl.mc_mask;
+       /* This is an implementation limit rather than a protocol limit */
+       CLASSERT(CL_LAST <= sizeof(int) * 8);
         if (bits == 0)
                 return 0;
 
         /* The record type is the lowest non-masked set bit */
-        while (bits && ((bits & 1) == 0)) {
-                bits = bits >> 1;
-                type++;
-        }
+       type = __ffs(bits);
 
         /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
         return mdd_changelog_data_store(env, mdd, type, (int)valid,
@@ -1077,7 +778,7 @@ static int mdd_declare_attr_set(const struct lu_env *env,
        if (attr->la_valid & LA_MODE) {
                 mdd_read_lock(env, obj, MOR_TGT_CHILD);
                rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
-                                  XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
+                                  XATTR_NAME_ACL_ACCESS);
                 mdd_read_unlock(env, obj);
                 if (rc == -EOPNOTSUPP || rc == -ENODATA)
                         rc = 0;
@@ -1095,31 +796,67 @@ static int mdd_declare_attr_set(const struct lu_env *env,
         }
 #endif
 
-       rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
+       rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
        return rc;
 }
 
+/*
+ * LU-3671
+ *
+ * permission changes may require sync operation, to mitigate performance
+ * impact, only do this for dir and when permission is reduced.
+ *
+ * For regular files, version is updated with permission change (see VBR), async
+ * permission won't cause any issue, while missing permission change on
+ * directory may affect accessibility of other objects after recovery.
+ */
+static inline bool permission_needs_sync(const struct lu_attr *old,
+                                        const struct lu_attr *new)
+{
+       if (!S_ISDIR(old->la_mode))
+               return false;
+
+       if (new->la_valid & (LA_UID | LA_GID))
+               return true;
+
+       if (new->la_valid & LA_MODE &&
+           new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
+               return true;
+
+       if ((new->la_valid & LA_MODE) &&
+           ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
+            (old->la_mode & S_IRWXUGO))
+               return true;
+
+       return false;
+}
+
 /* set attr and LOV EA at once, return updated attr */
 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
                 const struct md_attr *ma)
 {
-        struct mdd_object *mdd_obj = md2mdd_obj(obj);
-        struct mdd_device *mdd = mdo2mdd(obj);
-        struct thandle *handle;
-        struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
+       struct mdd_object *mdd_obj = md2mdd_obj(obj);
+       struct mdd_device *mdd = mdo2mdd(obj);
+       struct thandle *handle;
+       struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
+       struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
        const struct lu_attr *la = &ma->ma_attr;
        int rc;
-        ENTRY;
+       ENTRY;
 
        /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
        LASSERT((ma->ma_valid & MA_LOV) == 0);
        LASSERT((ma->ma_valid & MA_HSM) == 0);
        LASSERT((ma->ma_valid & MA_SOM) == 0);
 
-        *la_copy = ma->ma_attr;
-       rc = mdd_fix_attr(env, mdd_obj, la_copy, ma->ma_attr_flags);
+       rc = mdd_la_get(env, mdd_obj, attr);
        if (rc)
-                RETURN(rc);
+               RETURN(rc);
+
+       *la_copy = ma->ma_attr;
+       rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
+       if (rc)
+               RETURN(rc);
 
         /* setattr on "close" only change atime, or do nothing */
        if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
@@ -1137,50 +874,44 @@ int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
         if (rc)
                 GOTO(stop, rc);
 
-        /* permission changes may require sync operation */
-       if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
-                handle->th_sync |= !!mdd->mdd_sync_permission;
+       if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
+               handle->th_sync = 1;
 
        if (la->la_valid & (LA_MTIME | LA_CTIME))
                 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
                       la->la_mtime, la->la_ctime);
 
-        if (la_copy->la_valid & LA_FLAGS) {
+       mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
+       if (la_copy->la_valid & LA_FLAGS)
                rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
-                if (rc == 0)
-                        mdd_flags_xlate(mdd_obj, la_copy->la_flags);
-        } else if (la_copy->la_valid) {            /* setattr */
+       else if (la_copy->la_valid) /* setattr */
                rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
-        }
+       mdd_write_unlock(env, mdd_obj);
+
+       if (rc == 0)
+               rc = mdd_attr_set_changelog(env, obj, handle, la->la_valid);
+
+       GOTO(stop, rc);
 
-        if (rc == 0)
-                rc = mdd_attr_set_changelog(env, obj, handle,
-                                           la->la_valid);
 stop:
-        mdd_trans_stop(env, mdd, rc, handle);
-        RETURN(rc);
+       mdd_trans_stop(env, mdd, rc, handle);
+       return rc;
 }
 
 static int mdd_xattr_sanity_check(const struct lu_env *env,
-                                 struct mdd_object *obj)
+                                 struct mdd_object *obj,
+                                 const struct lu_attr *attr)
 {
-       struct lu_attr  *tmp_la = &mdd_env_info(env)->mti_la;
        struct lu_ucred *uc     = lu_ucred_assert(env);
-       int rc;
        ENTRY;
 
-       if (mdd_is_immutable(obj) || mdd_is_append(obj))
+       if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
                RETURN(-EPERM);
 
-       rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
-       if (rc)
-               RETURN(rc);
-
-       if ((uc->uc_fsuid != tmp_la->la_uid) &&
-           !md_capable(uc, CFS_CAP_FOWNER))
+       if ((uc->uc_fsuid != attr->la_uid) && !md_capable(uc, CFS_CAP_FOWNER))
                RETURN(-EPERM);
 
-       RETURN(rc);
+       RETURN(0);
 }
 
 static int mdd_declare_xattr_set(const struct lu_env *env,
@@ -1196,18 +927,23 @@ static int mdd_declare_xattr_set(const struct lu_env *env,
        if (rc)
                return rc;
 
-       /* Only record user xattr changes */
-       if ((strncmp("user.", name, 5) == 0)) {
-               rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
+       /* Only record user and layout xattr changes */
+       if (strncmp(XATTR_USER_PREFIX, name,
+                   sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
+           strcmp(XATTR_NAME_LOV, name) == 0) {
+               rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
                if (rc)
                        return rc;
        }
 
        /* If HSM data is modified, this could add a changelog */
-       if (strncmp(XATTR_NAME_HSM, name, sizeof(XATTR_NAME_HSM) - 1) == 0)
-               rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
+       if (strcmp(XATTR_NAME_HSM, name) == 0) {
+               rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
+               if (rc)
+                       return rc;
+       }
 
-       rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
+       rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
        return rc;
 }
 
@@ -1228,7 +964,7 @@ static int mdd_hsm_update_locked(const struct lu_env *env,
        struct mdd_thread_info *info = mdd_env_info(env);
        struct mdd_device      *mdd = mdo2mdd(obj);
        struct mdd_object      *mdd_obj = md2mdd_obj(obj);
-       struct lu_buf          *current_buf = &info->mti_buf;
+       struct lu_buf          *current_buf;
        struct md_hsm          *current_mh;
        struct md_hsm          *new_mh;
        int                     rc;
@@ -1239,12 +975,11 @@ static int mdd_hsm_update_locked(const struct lu_env *env,
                RETURN(-ENOMEM);
 
        /* Read HSM attrs from disk */
-       current_buf->lb_buf = info->mti_xattr_buf;
-       current_buf->lb_len = sizeof(info->mti_xattr_buf);
        CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
-       rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
-                          mdd_object_capa(env, mdd_obj));
-       rc = lustre_buf2hsm(info->mti_xattr_buf, rc, current_mh);
+       current_buf = mdd_buf_get(env, info->mti_xattr_buf,
+                                 sizeof(info->mti_xattr_buf));
+       rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM);
+       rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
        if (rc < 0 && rc != -ENODATA)
                GOTO(free, rc);
        else if (rc == -ENODATA)
@@ -1274,6 +1009,8 @@ free:
        return(rc);
 }
 
+static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
+                        const char *name);
 
 /**
  * The caller should guarantee to update the object ctime
@@ -1284,25 +1021,45 @@ static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
                         int fl)
 {
        struct mdd_object       *mdd_obj = md2mdd_obj(obj);
+       struct lu_attr          *attr = MDD_ENV_VAR(env, cattr);
        struct mdd_device       *mdd = mdo2mdd(obj);
        struct thandle          *handle;
        int                      rc;
-        ENTRY;
+       ENTRY;
 
-       if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
-               rc = mdd_acl_set(env, mdd_obj, buf, fl);
+       rc = mdd_la_get(env, mdd_obj, attr);
+       if (rc)
                RETURN(rc);
-       }
 
-       rc = mdd_xattr_sanity_check(env, mdd_obj);
+       rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
        if (rc)
                RETURN(rc);
 
+       if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
+           strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
+               struct posix_acl *acl;
+
+               /* user may set empty ACL, which should be treated as removing
+                * ACL. */
+               acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
+                                          buf->lb_len);
+               if (acl == NULL) {
+                       rc = mdd_xattr_del(env, obj, name);
+                       RETURN(rc);
+               }
+               posix_acl_release(acl);
+       }
+
+       if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
+               rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
+               RETURN(rc);
+       }
+
        handle = mdd_trans_create(env, mdd);
        if (IS_ERR(handle))
                RETURN(PTR_ERR(handle));
 
-       rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, 0, handle);
+       rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
        if (rc)
                GOTO(stop, rc);
 
@@ -1310,13 +1067,9 @@ static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
        if (rc)
                GOTO(stop, rc);
 
-       /* security-replated changes may require sync */
-       if (!strcmp(name, XATTR_NAME_ACL_ACCESS))
-               handle->th_sync |= !!mdd->mdd_sync_permission;
-
        mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
 
-       if (strncmp(XATTR_NAME_HSM, name, sizeof(XATTR_NAME_HSM) - 1) == 0) {
+       if (strcmp(XATTR_NAME_HSM, name) == 0) {
                rc = mdd_hsm_update_locked(env, obj, buf, handle);
                if (rc) {
                        mdd_write_unlock(env, mdd_obj);
@@ -1324,14 +1077,15 @@ static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
                }
        }
 
-       rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
-                          mdd_object_capa(env, mdd_obj));
+       rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle);
        mdd_write_unlock(env, mdd_obj);
        if (rc)
                GOTO(stop, rc);
 
-       /* Only record system & user xattr changes */
-       if (strncmp(XATTR_USER_PREFIX, name,
+       if (strcmp(XATTR_NAME_LOV, name) == 0)
+               rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, mdd_obj,
+                                             handle);
+       else if (strncmp(XATTR_USER_PREFIX, name,
                        sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
            strncmp(POSIX_ACL_XATTR_ACCESS, name,
                        sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
@@ -1352,35 +1106,41 @@ static int mdd_declare_xattr_del(const struct lu_env *env,
                                  const char *name,
                                  struct thandle *handle)
 {
-        int rc;
+       int rc;
 
-        rc = mdo_declare_xattr_del(env, obj, name, handle);
-        if (rc)
-                return rc;
+       rc = mdo_declare_xattr_del(env, obj, name, handle);
+       if (rc)
+               return rc;
 
-        /* Only record user xattr changes */
-        if ((strncmp("user.", name, 5) == 0))
-                rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
+       /* Only record user xattr changes */
+       if ((strncmp(XATTR_USER_PREFIX, name,
+                    sizeof(XATTR_USER_PREFIX) - 1) == 0))
+               rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
 
-        return rc;
+       return rc;
 }
 
 /**
  * The caller should guarantee to update the object ctime
  * after xattr_set if needed.
  */
-int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
-                  const char *name)
+static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
+                        const char *name)
 {
-        struct mdd_object *mdd_obj = md2mdd_obj(obj);
-        struct mdd_device *mdd = mdo2mdd(obj);
-        struct thandle *handle;
-        int  rc;
-        ENTRY;
+       struct mdd_object *mdd_obj = md2mdd_obj(obj);
+       struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
+       struct mdd_device *mdd = mdo2mdd(obj);
+       struct thandle *handle;
+       int  rc;
+       ENTRY;
 
-        rc = mdd_xattr_sanity_check(env, mdd_obj);
-        if (rc)
-                RETURN(rc);
+       rc = mdd_la_get(env, mdd_obj, attr);
+       if (rc)
+               RETURN(rc);
+
+       rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
+       if (rc)
+               RETURN(rc);
 
         handle = mdd_trans_create(env, mdd);
         if (IS_ERR(handle))
@@ -1395,8 +1155,7 @@ int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
                 GOTO(stop, rc);
 
         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
-        rc = mdo_xattr_del(env, mdd_obj, name, handle,
-                           mdd_object_capa(env, mdd_obj));
+       rc = mdo_xattr_del(env, mdd_obj, name, handle);
         mdd_write_unlock(env, mdd_obj);
        if (rc)
                GOTO(stop, rc);
@@ -1421,92 +1180,121 @@ stop:
  * read lov EA of an object
  * return the lov EA in an allocated lu_buf
  */
-static struct lu_buf *mdd_get_lov_ea(const struct lu_env *env,
-                                    struct mdd_object *obj)
+int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
+                  struct lu_buf *lmm_buf)
 {
        struct lu_buf   *buf = &mdd_env_info(env)->mti_big_buf;
-       struct lu_buf   *lmm_buf = NULL;
-       int              rc, sz;
+       int              rc, bufsize;
        ENTRY;
 
 repeat:
-       rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV,
-                          mdd_object_capa(env, obj));
+       rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV);
+
+       if (rc == -ERANGE) {
+               /* mti_big_buf is allocated but is too small
+                * we need to increase it */
+               buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
+                                            buf->lb_len * 2);
+               if (buf->lb_buf == NULL)
+                       GOTO(out, rc = -ENOMEM);
+               goto repeat;
+       }
+
        if (rc < 0)
-               GOTO(out, rc);
+               RETURN(rc);
 
        if (rc == 0)
-               GOTO(out, rc = -ENODATA);
+               RETURN(-ENODATA);
 
-       sz = rc;
+       bufsize = rc;
        if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
                /* mti_big_buf was not allocated, so we have to
                 * allocate it based on the ea size */
-               buf = mdd_buf_alloc(env, sz);
+               buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
+                                            bufsize);
                if (buf->lb_buf == NULL)
                        GOTO(out, rc = -ENOMEM);
                goto repeat;
        }
 
-       OBD_ALLOC_PTR(lmm_buf);
-       if (!lmm_buf)
+       lu_buf_alloc(lmm_buf, bufsize);
+       if (lmm_buf->lb_buf == NULL)
                GOTO(out, rc = -ENOMEM);
 
-       OBD_ALLOC(lmm_buf->lb_buf, sz);
-       if (!lmm_buf->lb_buf)
-               GOTO(free, rc = -ENOMEM);
-
-       memcpy(lmm_buf->lb_buf, buf->lb_buf, sz);
-       lmm_buf->lb_len = sz;
-
-       GOTO(out, rc = 0);
+       memcpy(lmm_buf->lb_buf, buf->lb_buf, bufsize);
+       rc = 0;
+       EXIT;
 
-free:
-       if (lmm_buf)
-               OBD_FREE_PTR(lmm_buf);
 out:
-       if (rc)
-               return ERR_PTR(rc);
-       return lmm_buf;
+       if (rc < 0)
+               lu_buf_free(lmm_buf);
+       return rc;
 }
 
+static int mdd_xattr_hsm_replace(const struct lu_env *env,
+                                struct mdd_object *o, struct lu_buf *buf,
+                                struct thandle *handle)
+{
+       struct hsm_attrs *attrs;
+       __u32 hsm_flags;
+       int flags = 0;
+       int rc;
+       ENTRY;
+
+       rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
+                          handle);
+       if (rc != 0)
+               RETURN(rc);
+
+       attrs = buf->lb_buf;
+       hsm_flags = le32_to_cpu(attrs->hsm_flags);
+       if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
+               RETURN(0);
+
+       /* Add a changelog record for release. */
+       hsm_set_cl_event(&flags, HE_RELEASE);
+       rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
+                                     flags, o, handle);
+       RETURN(rc);
+}
 
 /*
  *  check if layout swapping between 2 objects is allowed
  *  the rules are:
+ *  - only normal FIDs or non-system IGIFs
  *  - same type of objects
  *  - same owner/group (so quotas are still valid)
  */
 static int mdd_layout_swap_allowed(const struct lu_env *env,
                                   struct mdd_object *o1,
-                                  struct mdd_object *o2)
+                                  const struct lu_attr *attr1,
+                                  struct mdd_object *o2,
+                                  const struct lu_attr *attr2)
 {
        const struct lu_fid     *fid1, *fid2;
-       __u32                    uid, gid;
-       struct lu_attr          *tmp_la = &mdd_env_info(env)->mti_la;
-       int                      rc;
        ENTRY;
 
        fid1 = mdo2fid(o1);
        fid2 = mdo2fid(o2);
 
-       if (!fid_is_norm(fid1) || !fid_is_norm(fid2) ||
-           (mdd_object_type(o1) != mdd_object_type(o2)))
-               RETURN(-EPERM);
+       if (!fid_is_norm(fid1) &&
+           (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
+               RETURN(-EBADF);
 
-       tmp_la->la_valid = 0;
-       rc = mdd_la_get(env, o1, tmp_la, BYPASS_CAPA);
-       if (rc)
-               RETURN(rc);
-       uid = tmp_la->la_uid;
-       gid = tmp_la->la_gid;
+       if (!fid_is_norm(fid2) &&
+           (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
+               RETURN(-EBADF);
 
-       tmp_la->la_valid = 0;
-       rc = mdd_la_get(env, o2, tmp_la, BYPASS_CAPA);
-       if (rc)
-               RETURN(rc);
+       if (mdd_object_type(o1) != mdd_object_type(o2)) {
+               if (S_ISDIR(mdd_object_type(o1)))
+                       RETURN(-ENOTDIR);
+               if (S_ISREG(mdd_object_type(o1)))
+                       RETURN(-EISDIR);
+               RETURN(-EBADF);
+       }
 
-       if ((uid != tmp_la->la_uid) || (gid != tmp_la->la_gid))
+       if ((attr1->la_uid != attr2->la_uid) ||
+           (attr1->la_gid != attr2->la_gid))
                RETURN(-EPERM);
 
        RETURN(0);
@@ -1518,35 +1306,48 @@ static int mdd_layout_swap_allowed(const struct lu_env *env,
 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
                            struct md_object *obj2, __u64 flags)
 {
-       struct mdd_object       *o1, *o2, *fst_o, *snd_o;
-       struct lu_buf           *lmm1_buf = NULL, *lmm2_buf = NULL;
-       struct lu_buf           *fst_buf, *snd_buf;
-       struct lov_mds_md       *fst_lmm, *snd_lmm, *old_fst_lmm = NULL;
-       struct thandle          *handle;
+       struct mdd_thread_info  *info = mdd_env_info(env);
+       struct mdd_object       *fst_o = md2mdd_obj(obj1);
+       struct mdd_object       *snd_o = md2mdd_obj(obj2);
+       struct lu_attr          *fst_la = MDD_ENV_VAR(env, cattr);
+       struct lu_attr          *snd_la = MDD_ENV_VAR(env, tattr);
        struct mdd_device       *mdd = mdo2mdd(obj1);
-       int                      rc;
+       struct lov_mds_md       *fst_lmm, *snd_lmm;
+       struct lu_buf           *fst_buf = &info->mti_buf[0];
+       struct lu_buf           *snd_buf = &info->mti_buf[1];
+       struct lu_buf           *fst_hsm_buf = &info->mti_buf[2];
+       struct lu_buf           *snd_hsm_buf = &info->mti_buf[3];
+       struct ost_id           *saved_oi = NULL;
+       struct thandle          *handle;
        __u16                    fst_gen, snd_gen;
+       int                      fst_fl;
+       int                      rc;
+       int                      rc2;
        ENTRY;
 
+       CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
+       memset(info->mti_buf, 0, sizeof(info->mti_buf));
+
        /* we have to sort the 2 obj, so locking will always
         * be in the same order, even in case of 2 concurrent swaps */
-       rc = lu_fid_cmp(mdo2fid(md2mdd_obj(obj1)),
-                      mdo2fid(md2mdd_obj(obj2)));
-       /* same fid ? */
-       if (rc == 0)
+       rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
+       if (rc == 0) /* same fid ? */
                RETURN(-EPERM);
 
-       if (rc > 0) {
-               o1 = md2mdd_obj(obj1);
-               o2 = md2mdd_obj(obj2);
-       } else {
-               o1 = md2mdd_obj(obj2);
-               o2 = md2mdd_obj(obj1);
-       }
+       if (rc < 0)
+               swap(fst_o, snd_o);
+
+       rc = mdd_la_get(env, fst_o, fst_la);
+       if (rc != 0)
+               RETURN(rc);
+
+       rc = mdd_la_get(env, snd_o, snd_la);
+       if (rc != 0)
+               RETURN(rc);
 
        /* check if layout swapping is allowed */
-       rc = mdd_layout_swap_allowed(env, o1, o2);
-       if (rc)
+       rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la);
+       if (rc != 0)
                RETURN(rc);
 
        handle = mdd_trans_create(env, mdd);
@@ -1554,133 +1355,178 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
                RETURN(PTR_ERR(handle));
 
        /* objects are already sorted */
-       mdd_write_lock(env, o1, MOR_TGT_CHILD);
-       mdd_write_lock(env, o2, MOR_TGT_CHILD);
-
-       lmm1_buf = mdd_get_lov_ea(env, o1);
-       if (IS_ERR(lmm1_buf)) {
-               rc = PTR_ERR(lmm1_buf);
-               lmm1_buf = NULL;
-               if (rc != -ENODATA)
-                       GOTO(unlock, rc);
-       }
+       mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
+       mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
 
-       lmm2_buf = mdd_get_lov_ea(env, o2);
-       if (IS_ERR(lmm2_buf)) {
-               rc = PTR_ERR(lmm2_buf);
-               lmm2_buf = NULL;
-               if (rc != -ENODATA)
-                       GOTO(unlock, rc);
-       }
+       rc = mdd_get_lov_ea(env, fst_o, fst_buf);
+       if (rc < 0 && rc != -ENODATA)
+               GOTO(stop, rc);
+
+       rc = mdd_get_lov_ea(env, snd_o, snd_buf);
+       if (rc < 0 && rc != -ENODATA)
+               GOTO(stop, rc);
 
        /* swapping 2 non existant layouts is a success */
-       if ((lmm1_buf == NULL) && (lmm2_buf == NULL))
-               GOTO(unlock, rc = 0);
+       if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
+               GOTO(stop, rc = 0);
 
        /* to help inode migration between MDT, it is better to
         * start by the no layout file (if one), so we order the swap */
-       if (lmm1_buf == NULL) {
-               fst_o = o1;
-               fst_buf = lmm1_buf;
-               snd_o = o2;
-               snd_buf = lmm2_buf;
-       } else {
-               fst_o = o2;
-               fst_buf = lmm2_buf;
-               snd_o = o1;
-               snd_buf = lmm1_buf;
+       if (snd_buf->lb_buf == NULL) {
+               swap(fst_o, snd_o);
+               swap(fst_buf, snd_buf);
        }
 
        /* lmm and generation layout initialization */
-       if (fst_buf) {
+       if (fst_buf->lb_buf != NULL) {
                fst_lmm = fst_buf->lb_buf;
                fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
+               fst_fl  = LU_XATTR_REPLACE;
        } else {
                fst_lmm = NULL;
                fst_gen = 0;
+               fst_fl  = LU_XATTR_CREATE;
        }
 
-       if (snd_buf) {
-               snd_lmm = snd_buf->lb_buf;
-               snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
-       } else {
-               snd_lmm = NULL;
-               snd_gen = 0;
-       }
-
-       /* save the orignal lmm common header of first file
-        * to be able to roll back */
-       OBD_ALLOC_PTR(old_fst_lmm);
-       if (old_fst_lmm == NULL)
-               GOTO(unlock, rc = -ENOMEM);
-
-       memcpy(old_fst_lmm, fst_lmm, sizeof(*old_fst_lmm));
+       snd_lmm = snd_buf->lb_buf;
+       snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
 
        /* increase the generation layout numbers */
        snd_gen++;
        fst_gen++;
 
        /* set the file specific informations in lmm */
-       if (fst_lmm) {
+       if (fst_lmm != NULL) {
+               saved_oi = &info->mti_oa.o_oi;
+
+               *saved_oi = fst_lmm->lmm_oi;
                fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
-               fst_lmm->lmm_object_seq = snd_lmm->lmm_object_seq;
-               fst_lmm->lmm_object_id = snd_lmm->lmm_object_id;
+               fst_lmm->lmm_oi = snd_lmm->lmm_oi;
+               snd_lmm->lmm_oi = *saved_oi;
+       } else {
+               if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1))
+                       snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
+               else if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3))
+                       snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
+               else
+                       GOTO(stop, rc = -EPROTO);
        }
+       snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
+
+       /* Prepare HSM attribute if it's required */
+       if (flags & SWAP_LAYOUTS_MDS_HSM) {
+               const int buflen = sizeof(struct hsm_attrs);
+
+               lu_buf_alloc(fst_hsm_buf, buflen);
+               lu_buf_alloc(snd_hsm_buf, buflen);
+               if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
+                       GOTO(stop, rc = -ENOMEM);
+
+               /* Read HSM attribute */
+               rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM);
+               if (rc < 0)
+                       GOTO(stop, rc);
+
+               rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM);
+               if (rc < 0)
+                       GOTO(stop, rc);
+
+               rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
+                                          XATTR_NAME_HSM, LU_XATTR_REPLACE,
+                                          handle);
+               if (rc < 0)
+                       GOTO(stop, rc);
 
-       if (snd_lmm) {
-               snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
-               snd_lmm->lmm_object_seq = old_fst_lmm->lmm_object_seq;
-               snd_lmm->lmm_object_id = old_fst_lmm->lmm_object_id;
+               rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
+                                          XATTR_NAME_HSM, LU_XATTR_REPLACE,
+                                          handle);
+               if (rc < 0)
+                       GOTO(stop, rc);
        }
 
        /* prepare transaction */
        rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
-                                  LU_XATTR_REPLACE, handle);
-       if (rc)
+                                  fst_fl, handle);
+       if (rc != 0)
                GOTO(stop, rc);
 
-       rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf, XATTR_NAME_LOV,
-                                  LU_XATTR_REPLACE, handle);
-       if (rc)
+       if (fst_buf->lb_buf != NULL)
+               rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
+                                          XATTR_NAME_LOV, LU_XATTR_REPLACE,
+                                          handle);
+       else
+               rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
+                                          handle);
+       if (rc != 0)
                GOTO(stop, rc);
 
        rc = mdd_trans_start(env, mdd, handle);
-       if (rc)
+       if (rc != 0)
                GOTO(stop, rc);
 
-       rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV,
-                          LU_XATTR_REPLACE, handle,
-                          mdd_object_capa(env, fst_o));
-       if (rc)
+       if (flags & SWAP_LAYOUTS_MDS_HSM) {
+               rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
+               if (rc < 0)
+                       GOTO(stop, rc);
+
+               rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
+               if (rc < 0) {
+                       rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
+                                                   handle);
+                       if (rc2 < 0)
+                               CERROR("%s: restore "DFID" HSM error: %d/%d\n",
+                                      mdd_obj_dev_name(fst_o),
+                                      PFID(mdo2fid(fst_o)), rc, rc2);
+                       GOTO(stop, rc);
+               }
+       }
+
+       rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle);
+       if (rc != 0)
                GOTO(stop, rc);
 
-       rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
-                          LU_XATTR_REPLACE, handle,
-                          mdd_object_capa(env, snd_o));
-       if (rc) {
-               int     rc2;
+       if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
+               rc = -EOPNOTSUPP;
+       } else {
+               if (fst_buf->lb_buf != NULL)
+                       rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
+                                          LU_XATTR_REPLACE, handle);
+               else
+                       rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle);
+       }
+
+       if (rc != 0) {
+               int steps = 0;
 
                /* failure on second file, but first was done, so we have
-                * to roll back first */
-               /* restore object_id, object_seq and generation number
-                * on first file */
-               if (fst_lmm) {
-                       fst_lmm->lmm_object_id = old_fst_lmm->lmm_object_id;
-                       fst_lmm->lmm_object_seq = old_fst_lmm->lmm_object_seq;
-                       fst_lmm->lmm_layout_gen = old_fst_lmm->lmm_layout_gen;
+                * to roll back first. */
+               if (fst_buf->lb_buf != NULL) {
+                       fst_lmm->lmm_oi = *saved_oi;
+                       fst_lmm->lmm_layout_gen = cpu_to_le16(fst_gen - 1);
+                       rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
+                                           LU_XATTR_REPLACE, handle);
+               } else {
+                       rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle);
                }
+               if (rc2 < 0)
+                       goto do_lbug;
+
+               ++steps;
+               rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
+               if (rc2 < 0)
+                       goto do_lbug;
+
+               ++steps;
+               rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
 
-               rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
-                                   LU_XATTR_REPLACE, handle,
-                                   mdd_object_capa(env, fst_o));
-               if (rc2) {
+       do_lbug:
+               if (rc2 < 0) {
                        /* very bad day */
-                       CERROR("%s: unable to roll back after swap layouts"
-                              " failure between "DFID" and "DFID
-                              " rc2 = %d rc = %d)\n",
-                              mdd2obd_dev(mdd)->obd_name,
+                       CERROR("%s: unable to roll back layout swap. FIDs: "
+                              DFID" and "DFID "error: %d/%d, steps: %d\n",
+                              mdd_obj_dev_name(fst_o),
                               PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
-                              rc2, rc);
+                              rc, rc2, steps);
                        /* a solution to avoid journal commit is to panic,
                         * but it has strong consequences so we use LBUG to
                         * allow sysdamin to choose to panic or not
@@ -1689,37 +1535,52 @@ static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
                }
                GOTO(stop, rc);
        }
+
+       /* Issue one changelog record per file */
+       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
+       if (rc)
+               GOTO(stop, rc);
+
+       rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
+       if (rc)
+               GOTO(stop, rc);
        EXIT;
 
 stop:
        mdd_trans_stop(env, mdd, rc, handle);
-unlock:
-       mdd_write_unlock(env, o2);
-       mdd_write_unlock(env, o1);
-
-       if (lmm1_buf && lmm1_buf->lb_buf)
-               OBD_FREE(lmm1_buf->lb_buf, lmm1_buf->lb_len);
-       if (lmm1_buf)
-               OBD_FREE_PTR(lmm1_buf);
-
-       if (lmm2_buf && lmm2_buf->lb_buf)
-               OBD_FREE(lmm2_buf->lb_buf, lmm2_buf->lb_len);
-       if (lmm2_buf)
-               OBD_FREE_PTR(lmm2_buf);
-
-       if (old_fst_lmm)
-               OBD_FREE_PTR(old_fst_lmm);
+       mdd_write_unlock(env, snd_o);
+       mdd_write_unlock(env, fst_o);
 
+       lu_buf_free(fst_buf);
+       lu_buf_free(snd_buf);
+       lu_buf_free(fst_hsm_buf);
+       lu_buf_free(snd_hsm_buf);
        return rc;
 }
 
 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
-               struct mdd_object *child, struct lu_attr *attr)
+                         struct mdd_object *child, const struct lu_attr *attr,
+                         const struct md_op_spec *spec,
+                         struct dt_allocation_hint *hint)
 {
-       struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
-       struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
+       struct dt_object *np = parent ?  mdd_object_child(parent) : NULL;
        struct dt_object *nc = mdd_object_child(child);
 
+       memset(hint, 0, sizeof(*hint));
+
+       /* For striped directory, give striping EA to lod_ah_init, which will
+        * decide the stripe_offset and stripe count by it. */
+       if (S_ISDIR(attr->la_mode) &&
+           unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
+               hint->dah_eadata = spec->u.sp_ea.eadata;
+               hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
+       } else {
+               hint->dah_eadata = NULL;
+               hint->dah_eadata_len = 0;
+       }
+
+       CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
+              hint->dah_eadata, hint->dah_eadata_len);
        /* @hint will be initialized by underlying device. */
        nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
 }
@@ -1727,7 +1588,7 @@ void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
 /*
  * do NOT or the MAY_*'s, you'll get the weakest
  */
-int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
+int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
 {
        int res = 0;
 
@@ -1753,45 +1614,41 @@ int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
 }
 
 static int mdd_open_sanity_check(const struct lu_env *env,
-                                 struct mdd_object *obj, int flag)
+                               struct mdd_object *obj,
+                               const struct lu_attr *attr, int flag)
 {
-        struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
-        int mode, rc;
-        ENTRY;
-
-        /* EEXIST check */
-        if (mdd_is_dead_obj(obj))
-                RETURN(-ENOENT);
+       int mode, rc;
+       ENTRY;
 
-        rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
-        if (rc)
-               RETURN(rc);
+       /* EEXIST check */
+       if (mdd_is_dead_obj(obj))
+               RETURN(-ENOENT);
 
-        if (S_ISLNK(tmp_la->la_mode))
-                RETURN(-ELOOP);
+       if (S_ISLNK(attr->la_mode))
+               RETURN(-ELOOP);
 
-        mode = accmode(env, tmp_la, flag);
+       mode = accmode(env, attr, flag);
 
-        if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
-                RETURN(-EISDIR);
+       if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
+               RETURN(-EISDIR);
 
-        if (!(flag & MDS_OPEN_CREATED)) {
-                rc = mdd_permission_internal(env, obj, tmp_la, mode);
-                if (rc)
-                        RETURN(rc);
-        }
+       if (!(flag & MDS_OPEN_CREATED)) {
+               rc = mdd_permission_internal(env, obj, attr, mode);
+               if (rc)
+                       RETURN(rc);
+       }
 
-        if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
-            S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
-                flag &= ~MDS_OPEN_TRUNC;
+       if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
+           S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
+               flag &= ~MDS_OPEN_TRUNC;
 
-        /* For writing append-only file must open it with append mode. */
-        if (mdd_is_append(obj)) {
-                if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
-                        RETURN(-EPERM);
-                if (flag & MDS_OPEN_TRUNC)
-                        RETURN(-EPERM);
-        }
+       /* For writing append-only file must open it with append mode. */
+       if (attr->la_flags & LUSTRE_APPEND_FL) {
+               if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
+                       RETURN(-EPERM);
+               if (flag & MDS_OPEN_TRUNC)
+                       RETURN(-EPERM);
+       }
 
 #if 0
         /*
@@ -1802,48 +1659,37 @@ static int mdd_open_sanity_check(const struct lu_env *env,
 
                if (uc && ((uc->uc_valid == UCRED_OLD) ||
                           (uc->uc_valid == UCRED_NEW)) &&
-                   (uc->uc_fsuid != tmp_la->la_uid) &&
+                   (uc->uc_fsuid != attr->la_uid) &&
                    !md_capable(uc, CFS_CAP_FOWNER))
                        RETURN(-EPERM);
         }
 #endif
 
-        RETURN(0);
+       RETURN(0);
 }
 
 static int mdd_open(const struct lu_env *env, struct md_object *obj,
-                    int flags)
+                   int flags)
 {
-        struct mdd_object *mdd_obj = md2mdd_obj(obj);
-        int rc = 0;
-
-        mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
-
-        rc = mdd_open_sanity_check(env, mdd_obj, flags);
-        if (rc == 0)
-                mdd_obj->mod_count++;
-
-        mdd_write_unlock(env, mdd_obj);
-        return rc;
-}
+       struct mdd_object *mdd_obj = md2mdd_obj(obj);
+       struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
+       int rc = 0;
 
-int mdd_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
-                            struct md_attr *ma, struct thandle *handle)
-{
-        return mdo_declare_destroy(env, obj, handle);
-}
+       mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
 
-/* return md_attr back,
- * if it is last unlink then return lov ea + llog cookie*/
-int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
-                    struct md_attr *ma, struct thandle *handle)
-{
-       int rc;
-        ENTRY;
+       rc = mdd_la_get(env, mdd_obj, attr);
+       if (rc != 0)
+               GOTO(out, rc);
 
-       rc = mdo_destroy(env, obj, handle);
+       rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
+       if (rc != 0)
+               GOTO(out, rc);
 
-        RETURN(rc);
+       mdd_obj->mod_count++;
+       EXIT;
+out:
+       mdd_write_unlock(env, mdd_obj);
+       return rc;
 }
 
 static int mdd_declare_close(const struct lu_env *env,
@@ -1899,9 +1745,9 @@ again:
                 if (rc)
                         GOTO(stop, rc);
 
-                rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
-                if (rc)
-                        GOTO(stop, rc);
+               rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
+               if (rc)
+                       GOTO(stop, rc);
 
                 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
                 if (rc)
@@ -1909,8 +1755,7 @@ again:
         }
 
         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
-       rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
-                       mdd_object_capa(env, mdd_obj));
+       rc = mdd_la_get(env, mdd_obj, &ma->ma_attr);
        if (rc != 0) {
                CERROR("Failed to get lu_attr of "DFID": %d\n",
                       PFID(mdd_object_fid(mdd_obj)), rc);
@@ -1973,12 +1818,12 @@ out:
                 if (handle == NULL) {
                         handle = mdd_trans_create(env, mdo2mdd(obj));
                         if (IS_ERR(handle))
-                                GOTO(stop, rc = IS_ERR(handle));
+                               GOTO(stop, rc = PTR_ERR(handle));
 
-                        rc = mdd_declare_changelog_store(env, mdd, NULL,
-                                                         handle);
-                        if (rc)
-                                GOTO(stop, rc);
+                       rc = mdd_declare_changelog_store(env, mdd, NULL, NULL,
+                                                        handle);
+                       if (rc)
+                               GOTO(stop, rc);
 
                         rc = mdd_trans_start(env, mdo2mdd(obj), handle);
                         if (rc)
@@ -1990,9 +1835,10 @@ out:
         }
 
 stop:
-        if (handle != NULL)
-                mdd_trans_stop(env, mdd, rc, handle);
-        return rc;
+       if (handle != NULL && !IS_ERR(handle))
+               mdd_trans_stop(env, mdd, rc, handle);
+
+       return rc;
 }
 
 /*
@@ -2015,7 +1861,7 @@ static int mdd_readpage_sanity_check(const struct lu_env *env,
 }
 
 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
-                             int nob, const struct dt_it_ops *iops,
+                             size_t nob, const struct dt_it_ops *iops,
                              struct dt_it *it, __u32 attr, void *arg)
 {
        struct lu_dirpage       *dp = &lp->lp_dir;
@@ -2024,8 +1870,12 @@ static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
        __u64                    hash = 0;
        struct lu_dirent        *ent;
        struct lu_dirent        *last = NULL;
+       struct lu_fid            fid;
        int                      first = 1;
 
+       if (nob < sizeof(*dp))
+               return -EINVAL;
+
         memset(area, 0, sizeof (*dp));
         area += sizeof (*dp);
         nob  -= sizeof (*dp);
@@ -2033,7 +1883,7 @@ static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
         ent  = area;
         do {
                 int    len;
-                int    recsize;
+               size_t recsize;
 
                 len = iops->key_size(env, it);
 
@@ -2060,6 +1910,12 @@ static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
                         /* osd might not able to pack all attributes,
                          * so recheck rec length */
                         recsize = le16_to_cpu(ent->lde_reclen);
+
+                       if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
+                               fid_le_to_cpu(&fid, &ent->lde_fid);
+                               if (fid_is_dot_lustre(&fid))
+                                       goto next;
+                       }
                 } else {
                         result = (last != NULL) ? 0 :-EINVAL;
                         goto out;
@@ -2123,12 +1979,12 @@ int mdd_readpage(const struct lu_env *env, struct md_object *obj,
                 LASSERT(rdpg->rp_pages != NULL);
 
                 pg = rdpg->rp_pages[0];
-                dp = (struct lu_dirpage*)cfs_kmap(pg);
+               dp = (struct lu_dirpage *)kmap(pg);
                 memset(dp, 0 , sizeof(struct lu_dirpage));
                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
                 dp->ldp_hash_end   = cpu_to_le64(MDS_DIR_END_OFF);
                 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
-                cfs_kunmap(pg);
+               kunmap(pg);
                 GOTO(out_unlock, rc = LU_PAGE_SIZE);
         }
 
@@ -2137,17 +1993,18 @@ int mdd_readpage(const struct lu_env *env, struct md_object *obj,
        if (rc >= 0) {
                struct lu_dirpage       *dp;
 
-               dp = cfs_kmap(rdpg->rp_pages[0]);
+               dp = kmap(rdpg->rp_pages[0]);
                dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
                if (rc == 0) {
                        /*
                         * No pages were processed, mark this for first page
                         * and send back.
                         */
+                       dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
                        dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
                        rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
                }
-               cfs_kunmap(rdpg->rp_pages[0]);
+               kunmap(rdpg->rp_pages[0]);
        }
 
        GOTO(out_unlock, rc);
@@ -2158,28 +2015,40 @@ out_unlock:
 
 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
 {
-        struct mdd_object *mdd_obj = md2mdd_obj(obj);
+       struct mdd_object *mdd_obj = md2mdd_obj(obj);
 
-        if (mdd_object_exists(mdd_obj) == 0) {
-                CERROR("%s: object "DFID" not found: rc = -2\n",
-                       mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
-                return -ENOENT;
-        }
-        return dt_object_sync(env, mdd_object_child(mdd_obj));
+       if (mdd_object_exists(mdd_obj) == 0) {
+               int rc = -ENOENT;
+
+               CERROR("%s: object "DFID" not found: rc = %d\n",
+                      mdd_obj_dev_name(mdd_obj),
+                      PFID(mdd_object_fid(mdd_obj)), rc);
+               return rc;
+       }
+       return dt_object_sync(env, mdd_object_child(mdd_obj),
+                             0, OBD_OBJECT_EOF);
 }
 
 static int mdd_object_lock(const struct lu_env *env,
                           struct md_object *obj,
                           struct lustre_handle *lh,
                           struct ldlm_enqueue_info *einfo,
-                          void *policy)
+                          ldlm_policy_data_t *policy)
 {
        struct mdd_object *mdd_obj = md2mdd_obj(obj);
-       LASSERT(mdd_object_exists(mdd_obj));
        return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
                              einfo, policy);
 }
 
+static int mdd_object_unlock(const struct lu_env *env,
+                            struct md_object *obj,
+                            struct ldlm_enqueue_info *einfo,
+                            ldlm_policy_data_t *policy)
+{
+       struct mdd_object *mdd_obj = md2mdd_obj(obj);
+       return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
+}
+
 const struct md_object_operations mdd_obj_ops = {
        .moo_permission         = mdd_permission,
        .moo_attr_get           = mdd_attr_get,
@@ -2194,8 +2063,7 @@ const struct md_object_operations mdd_obj_ops = {
        .moo_readpage           = mdd_readpage,
        .moo_readlink           = mdd_readlink,
        .moo_changelog          = mdd_changelog,
-       .moo_capa_get           = mdd_capa_get,
        .moo_object_sync        = mdd_object_sync,
-       .moo_path               = mdd_path,
        .moo_object_lock        = mdd_object_lock,
+       .moo_object_unlock      = mdd_object_unlock,
 };