Whamcloud - gitweb
LU-7340 mdd: changelogs garbage collection
[fs/lustre-release.git] / lustre / mdd / mdd_dir.c
index b1bff5c..a353f8b 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2013, Intel Corporation.
+ * Copyright (c) 2011, 2016, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -53,8 +49,8 @@ static const char dot[] = ".";
 static const char dotdot[] = "..";
 
 static struct lu_name lname_dotdot = {
-        (char *) dotdot,
-        sizeof(dotdot) - 1
+       .ln_name        = (char *) dotdot,
+       .ln_namelen     = sizeof(dotdot) - 1,
 };
 
 static inline int
@@ -85,11 +81,12 @@ __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
        if (unlikely(mdd_is_dead_obj(mdd_obj)))
                RETURN(-ESTALE);
 
+       if (!mdd_object_exists(mdd_obj))
+               RETURN(-ESTALE);
+
        if (mdd_object_remote(mdd_obj)) {
                CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
                       mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
-       } else if (!mdd_object_exists(mdd_obj)) {
-               RETURN(-ESTALE);
        }
 
        rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
@@ -99,8 +96,7 @@ __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
 
        if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
                   dt_try_as_dir(env, dir)))
-               rc = dt_lookup(env, dir, (struct dt_rec *)fid, key,
-                              mdd_object_capa(env, mdd_obj));
+               rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
        else
                rc = -ENOTDIR;
 
@@ -115,20 +111,137 @@ int mdd_lookup(const struct lu_env *env,
         int rc;
         ENTRY;
 
-       rc = mdd_la_get(env, md2mdd_obj(pobj), pattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
        if (rc != 0)
                RETURN(rc);
 
-       rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
+       rc = __mdd_lookup(env, pobj, pattr, lname, fid,
+                         (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
         RETURN(rc);
 }
 
+/** Read the link EA into a temp buffer.
+ * Uses the mdd_thread_info::mti_big_buf since it is generally large.
+ * A pointer to the buffer is stored in \a ldata::ld_buf.
+ *
+ * \retval 0 or error
+ */
+static int __mdd_links_read(const struct lu_env *env,
+                           struct mdd_object *mdd_obj,
+                           struct linkea_data *ldata)
+{
+       int rc;
+
+       if (!mdd_object_exists(mdd_obj))
+               return -ENODATA;
+
+       /* First try a small buf */
+       LASSERT(env != NULL);
+       ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
+                                              PAGE_SIZE);
+       if (ldata->ld_buf->lb_buf == NULL)
+               return -ENOMEM;
+
+       rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
+       if (rc == -ERANGE) {
+               /* Buf was too small, figure out what we need. */
+               lu_buf_free(ldata->ld_buf);
+               rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
+                                  XATTR_NAME_LINK);
+               if (rc < 0)
+                       return rc;
+               ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
+               if (ldata->ld_buf->lb_buf == NULL)
+                       return -ENOMEM;
+               rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
+                                 XATTR_NAME_LINK);
+       }
+       if (rc < 0) {
+               lu_buf_free(ldata->ld_buf);
+               ldata->ld_buf = NULL;
+               return rc;
+       }
+
+       return linkea_init(ldata);
+}
+
+static int mdd_links_read(const struct lu_env *env,
+                         struct mdd_object *mdd_obj,
+                         struct linkea_data *ldata)
+{
+       int rc;
+
+       rc = __mdd_links_read(env, mdd_obj, ldata);
+       if (!rc)
+               rc = linkea_init(ldata);
+
+       return rc;
+}
+
+static int mdd_links_read_with_rec(const struct lu_env *env,
+                                  struct mdd_object *mdd_obj,
+                                  struct linkea_data *ldata)
+{
+       int rc;
+
+       rc = __mdd_links_read(env, mdd_obj, ldata);
+       if (!rc)
+               rc = linkea_init_with_rec(ldata);
+
+       return rc;
+}
+
+/**
+ * Get parent FID of the directory
+ *
+ * Read parent FID from linkEA, if that fails, then do lookup
+ * dotdot to get the parent FID.
+ *
+ * \param[in] env      execution environment
+ * \param[in] obj      object from which to find the parent FID
+ * \param[in] attr     attribute of the object
+ * \param[out] fid     fid to get the parent FID
+ *
+ * \retval             0 if getting the parent FID succeeds.
+ * \retval             negative errno if getting the parent FID fails.
+ **/
 static inline int mdd_parent_fid(const struct lu_env *env,
                                 struct mdd_object *obj,
                                 const struct lu_attr *attr,
                                 struct lu_fid *fid)
 {
-       return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
+       struct mdd_thread_info  *info = mdd_env_info(env);
+       struct linkea_data      ldata = { NULL };
+       struct lu_buf           *buf = &info->mti_link_buf;
+       struct lu_name          lname;
+       int                     rc = 0;
+
+       ENTRY;
+
+       LASSERT(S_ISDIR(mdd_object_type(obj)));
+
+       buf = lu_buf_check_and_alloc(buf, PATH_MAX);
+       if (buf->lb_buf == NULL)
+               GOTO(lookup, rc = 0);
+
+       ldata.ld_buf = buf;
+       rc = mdd_links_read_with_rec(env, obj, &ldata);
+       if (rc != 0)
+               GOTO(lookup, rc);
+
+       LASSERT(ldata.ld_leh != NULL);
+       /* Directory should only have 1 parent */
+       if (ldata.ld_leh->leh_reccount > 1)
+               GOTO(lookup, rc);
+
+       ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
+
+       linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
+       if (likely(fid_is_sane(fid)))
+               RETURN(0);
+lookup:
+       rc =  __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
+       RETURN(rc);
 }
 
 /*
@@ -170,8 +283,7 @@ static int mdd_is_parent(const struct lu_env *env,
                 RETURN(0);
 
         for(;;) {
-               /* this is done recursively, bypass capa for each obj */
-               mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
+               /* this is done recursively */
                rc = mdd_parent_fid(env, p1, attr, pfid);
                if (rc)
                        GOTO(out, rc);
@@ -181,18 +293,16 @@ static int mdd_is_parent(const struct lu_env *env,
                        GOTO(out, rc = 0);
                 if (lu_fid_eq(pfid, lf))
                         GOTO(out, rc = 1);
-                if (parent)
-                        mdd_object_put(env, parent);
+               if (parent != NULL)
+                       mdd_object_put(env, parent);
 
                parent = mdd_object_find(env, mdd, pfid);
-               if (IS_ERR(parent)) {
+               if (IS_ERR(parent))
                        GOTO(out, rc = PTR_ERR(parent));
-               } else if (mdd_object_remote(parent)) {
-                       /*FIXME: Because of the restriction of rename in Phase I.
-                        * If the parent is remote, we just assumed lf is not the
-                        * parent of P1 for now */
-                       GOTO(out, rc = 0);
-               }
+
+               if (!mdd_object_exists(parent))
+                       GOTO(out, rc = -EINVAL);
+
                p1 = parent;
         }
         EXIT;
@@ -206,7 +316,7 @@ out:
  * No permission check is needed.
  *
  * returns 1: if fid is ancestor of @mo;
- * returns 0: if fid is not a ancestor of @mo;
+ * returns 0: if fid is not an ancestor of @mo;
  *
  * returns EREMOTE if remote object is found, fid of remote object is saved to
  * @fid;
@@ -224,7 +334,7 @@ int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
        if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
                RETURN(0);
 
-       rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
+       rc = mdd_la_get(env, md2mdd_obj(mo), attr);
        if (rc != 0)
                RETURN(rc);
 
@@ -265,7 +375,7 @@ static int mdd_dir_is_empty(const struct lu_env *env,
                RETURN(-ENOTDIR);
 
        iops = &obj->do_index_ops->dio_it;
-       it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
+       it = iops->init(env, obj, LUDA_64BITHASH);
        if (!IS_ERR(it)) {
                result = iops->get(env, it, (const struct dt_key *)"");
                if (result > 0) {
@@ -337,8 +447,6 @@ int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
                   const struct lu_attr *pattr, struct mdd_object *cobj,
                   bool check_perm)
 {
-       struct mdd_thread_info *info = mdd_env_info(env);
-       struct lu_buf   *xbuf;
        int rc = 0;
        ENTRY;
 
@@ -348,19 +456,6 @@ int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
        if (mdd_is_dead_obj(pobj))
                RETURN(-ENOENT);
 
-       /* If the parent is a sub-stripe, check whether it is dead */
-       xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
-       rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV,
-                          mdd_object_capa(env, pobj));
-       if (unlikely(rc > 0)) {
-               struct lmv_mds_md_v1  *lmv1 = xbuf->lb_buf;
-
-               if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
-                   le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
-                       RETURN(-ESTALE);
-       }
-       rc = 0;
-
        if (check_perm)
                rc = mdd_permission_internal_locked(env, pobj, pattr,
                                                    MAY_WRITE | MAY_EXEC,
@@ -380,17 +475,16 @@ int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
        if (mdd_is_dead_obj(pobj))
                RETURN(-ENOENT);
 
-       if ((attr->la_valid & LA_FLAGS) &&
-           (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
+       if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
                RETURN(-EPERM);
 
        rc = mdd_permission_internal_locked(env, pobj, pattr,
                                            MAY_WRITE | MAY_EXEC,
                                            MOR_TGT_PARENT);
-       if (rc)
+       if (rc != 0)
                RETURN(rc);
 
-       if (mdd_is_append(pobj))
+       if (pattr->la_flags & LUSTRE_APPEND_FL)
                RETURN(-EPERM);
 
        RETURN(rc);
@@ -445,7 +539,7 @@ static int mdd_may_delete_entry(const struct lu_env *env,
                        RETURN(rc);
        }
 
-       if (mdd_is_append(pobj))
+       if (pattr->la_flags & LUSTRE_APPEND_FL)
                RETURN(-EPERM);
 
        RETURN(0);
@@ -482,11 +576,7 @@ int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
        if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
                RETURN(-EPERM);
 
-       if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
-               RETURN(-EPERM);
-
-       if ((tattr->la_valid & LA_FLAGS) &&
-           (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
+       if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
                RETURN(-EPERM);
 
        /* additional check the rename case */
@@ -546,11 +636,11 @@ static int mdd_link_sanity_check(const struct lu_env *env,
        if (rc < 0)
                RETURN(rc);
 
-        if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
-                RETURN(-EPERM);
+       if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
+               RETURN(-EPERM);
 
-        if (S_ISDIR(mdd_object_type(src_obj)))
-                RETURN(-EPERM);
+       if (S_ISDIR(mdd_object_type(src_obj)))
+               RETURN(-EPERM);
 
        LASSERT(src_obj != tgt_obj);
        rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
@@ -563,15 +653,14 @@ static int mdd_link_sanity_check(const struct lu_env *env,
 }
 
 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
-                                  const char *name, struct thandle *handle,
-                                  struct lustre_capa *capa)
+                                  const char *name, struct thandle *handle)
 {
        struct dt_object *next = mdd_object_child(pobj);
        int rc;
        ENTRY;
 
        if (dt_try_as_dir(env, next))
-               rc = dt_delete(env, next, (struct dt_key *)name, handle, capa);
+               rc = dt_delete(env, next, (struct dt_key *)name, handle);
        else
                rc = -ENOTDIR;
 
@@ -582,8 +671,7 @@ static int __mdd_index_insert_only(const struct lu_env *env,
                                   struct mdd_object *pobj,
                                   const struct lu_fid *lf, __u32 type,
                                   const char *name,
-                                  struct thandle *handle,
-                                  struct lustre_capa *capa)
+                                  struct thandle *handle)
 {
        struct dt_object *next = mdd_object_child(pobj);
        int               rc;
@@ -598,7 +686,7 @@ static int __mdd_index_insert_only(const struct lu_env *env,
                rec->rec_type = type;
                ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
                rc = dt_insert(env, next, (const struct dt_rec *)rec,
-                              (const struct dt_key *)name, handle, capa,
+                              (const struct dt_key *)name, handle,
                               ignore_quota);
        } else {
                rc = -ENOTDIR;
@@ -609,13 +697,12 @@ static int __mdd_index_insert_only(const struct lu_env *env,
 /* insert named index, add reference if isdir */
 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
                              const struct lu_fid *lf, __u32 type,
-                             const char *name, struct thandle *handle,
-                             struct lustre_capa *capa)
+                             const char *name, struct thandle *handle)
 {
        int rc;
        ENTRY;
 
-       rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle, capa);
+       rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
        if (rc == 0 && S_ISDIR(type)) {
                mdd_write_lock(env, pobj, MOR_TGT_PARENT);
                mdo_ref_add(env, pobj, handle);
@@ -627,13 +714,13 @@ static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
 
 /* delete named index, drop reference if isdir */
 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
-                              const char *name, int is_dir, struct thandle *handle,
-                              struct lustre_capa *capa)
+                             const char *name, int is_dir,
+                             struct thandle *handle)
 {
         int               rc;
         ENTRY;
 
-        rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
+       rc = __mdd_index_delete_only(env, pobj, name, handle);
         if (rc == 0 && is_dir) {
                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
                 mdo_ref_del(env, pobj, handle);
@@ -643,15 +730,37 @@ static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
         RETURN(rc);
 }
 
+static int mdd_llog_record_calc_size(const struct lu_env *env,
+                                    const struct lu_name *tname,
+                                    const struct lu_name *sname)
+{
+       const struct lu_ucred   *uc = lu_ucred(env);
+       enum changelog_rec_flags crf = CLF_EXTRA_FLAGS;
+       enum changelog_rec_extra_flags crfe = CLFE_UIDGID;
+
+       if (sname != NULL)
+               crf |= CLF_RENAME;
+
+       if (uc != NULL && uc->uc_jobid[0] != '\0')
+               crf |= CLF_JOBID;
+
+       return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
+                            changelog_rec_offset(crf, crfe) +
+                            (tname != NULL ? tname->ln_namelen : 0) +
+                            (sname != NULL ? 1 + sname->ln_namelen : 0));
+}
+
 int mdd_declare_changelog_store(const struct lu_env *env,
                                struct mdd_device *mdd,
-                               const struct lu_name *fname,
+                               const struct lu_name *tname,
+                               const struct lu_name *sname,
                                struct thandle *handle)
 {
        struct obd_device               *obd = mdd2obd_dev(mdd);
        struct llog_ctxt                *ctxt;
        struct llog_changelog_rec       *rec;
        struct lu_buf                   *buf;
+       struct thandle                  *llog_th;
        int                              reclen;
        int                              rc;
 
@@ -659,8 +768,7 @@ int mdd_declare_changelog_store(const struct lu_env *env,
        if (!(mdd->mdd_cl.mc_flags & CLM_ON))
                return 0;
 
-       reclen = llog_data_len(sizeof(*rec) +
-                              (fname != NULL ? fname->ln_namelen : 0));
+       reclen = mdd_llog_record_calc_size(env, tname, sname);
        buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
        if (buf->lb_buf == NULL)
                return -ENOMEM;
@@ -673,102 +781,167 @@ int mdd_declare_changelog_store(const struct lu_env *env,
        if (ctxt == NULL)
                return -ENXIO;
 
-       rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
+       llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
+       if (IS_ERR(llog_th))
+               GOTO(out_put, rc = PTR_ERR(llog_th));
+
+       rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
+
+out_put:
        llog_ctxt_put(ctxt);
 
        return rc;
 }
 
-static int mdd_declare_changelog_ext_store(const struct lu_env *env,
-                                          struct mdd_device *mdd,
-                                          const struct lu_name *tname,
-                                          const struct lu_name *sname,
-                                          struct thandle *handle)
-{
-       struct obd_device               *obd = mdd2obd_dev(mdd);
-       struct llog_ctxt                *ctxt;
-       struct llog_changelog_ext_rec   *rec;
-       struct lu_buf                   *buf;
-       int                              reclen;
-       int                              rc;
-
-       /* Not recording */
-       if (!(mdd->mdd_cl.mc_flags & CLM_ON))
-               return 0;
+struct mdd_changelog_gc {
+       struct mdd_device *mcgc_mdd;
+       bool mcgc_found;
+       __u32 mcgc_maxtime;
+       __u64 mcgc_maxindexes;
+       __u32 mcgc_id;
+};
 
-       reclen = llog_data_len(sizeof(*rec) +
-                              (tname != NULL ? tname->ln_namelen : 0) +
-                              (sname != NULL ? 1 + sname->ln_namelen : 0));
-       buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
-       if (buf->lb_buf == NULL)
-               return -ENOMEM;
+/* return first registered ChangeLog user idle since too long
+ * use ChangeLog's user plain LLOG mtime for this */
+static int mdd_changelog_gc_cb(const struct lu_env *env,
+                              struct llog_handle *llh,
+                              struct llog_rec_hdr *hdr, void *data)
+{
+       struct llog_changelog_user_rec  *rec;
+       struct mdd_changelog_gc *mcgc = (struct mdd_changelog_gc *)data;
+       struct mdd_device *mdd = mcgc->mcgc_mdd;
+       ENTRY;
 
-       rec = buf->lb_buf;
-       rec->cr_hdr.lrh_len = reclen;
-       rec->cr_hdr.lrh_type = CHANGELOG_REC;
+       if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
+               RETURN(-ENXIO);
 
-       ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
-       if (ctxt == NULL)
-               return -ENXIO;
+       rec = container_of(hdr, struct llog_changelog_user_rec,
+                          cur_hdr);
 
-       rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
-       llog_ctxt_put(ctxt);
+       /* find oldest idle user, based on last record update/cancel time (new
+        * behavior), or for old user records, last record index vs current
+        * ChangeLog index. Late users with old record format will be treated
+        * first as we assume they could be idle since longer
+        */
+       if (rec->cur_time != 0) {
+               __u32 time_now = (__u32)get_seconds();
+               __u32 time_out = rec->cur_time +
+                                mdd->mdd_changelog_max_idle_time;
+               __u32 idle_time = time_now - rec->cur_time;
+
+               /* treat oldest idle user first, and if no old format user
+                * has been already selected
+                */
+               if (time_after32(time_now, time_out) &&
+                   idle_time > mcgc->mcgc_maxtime &&
+                   mcgc->mcgc_maxindexes == 0) {
+                       mcgc->mcgc_maxtime = idle_time;
+                       mcgc->mcgc_id = rec->cur_id;
+                       mcgc->mcgc_found = true;
+               }
+       } else {
+               /* old user record with no idle time stamp, so use empirical
+                * method based on its current index/position
+                */
+               __u64 idle_indexes;
+
+               idle_indexes = mdd->mdd_cl.mc_index - rec->cur_endrec;
+
+               /* treat user with the oldest/smallest current index first */
+               if (idle_indexes >= mdd->mdd_changelog_max_idle_indexes &&
+                   idle_indexes > mcgc->mcgc_maxindexes) {
+                       mcgc->mcgc_maxindexes = idle_indexes;
+                       mcgc->mcgc_id = rec->cur_id;
+                       mcgc->mcgc_found = true;
+               }
 
-       return rc;
+       }
+       RETURN(0);
 }
 
-/** Add a changelog entry \a rec to the changelog llog
- * \param mdd
- * \param rec
- * \param handle - currently ignored since llogs start their own transaction;
- *                 this will hopefully be fixed in llog rewrite
- * \retval 0 ok
- */
-int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
-                       struct llog_changelog_rec *rec, struct thandle *th)
+/* recover space from long-term inactive ChangeLog users */
+static int mdd_chlg_garbage_collect(void *data)
 {
-       struct obd_device       *obd = mdd2obd_dev(mdd);
-       struct llog_ctxt        *ctxt;
-       int                      rc;
+       struct mdd_device *mdd = (struct mdd_device *)data;
+       struct lu_env             *env = NULL;
+       int                        rc;
+       struct llog_ctxt *ctxt;
+       struct mdd_changelog_gc mcgc = {
+               .mcgc_mdd = mdd,
+               .mcgc_found = false,
+               .mcgc_maxtime = 0,
+               .mcgc_maxindexes = 0,
+       };
+       ENTRY;
 
-       rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
-       rec->cr_hdr.lrh_type = CHANGELOG_REC;
-       rec->cr.cr_time = cl_time();
+       CDEBUG(D_HA, "%s: ChangeLog garbage collect thread start\n",
+              mdd2obd_dev(mdd)->obd_name);
 
-       spin_lock(&mdd->mdd_cl.mc_lock);
-       /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
-        * but as long as the MDD transactions are ordered correctly for e.g.
-        * rename conflicts, I don't think this should matter. */
-       rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
-       spin_unlock(&mdd->mdd_cl.mc_lock);
+       OBD_ALLOC_PTR(env);
+       if (env == NULL)
+               GOTO(out, rc = -ENOMEM);
 
-       ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
-       if (ctxt == NULL)
-               return -ENXIO;
+       rc = lu_env_init(env, LCT_MD_THREAD);
+       if (rc)
+               GOTO(out, rc);
 
-       rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
-       llog_ctxt_put(ctxt);
-       if (rc > 0)
-               rc = 0;
+       for (;;) {
+               ctxt = llog_get_context(mdd2obd_dev(mdd),
+                                       LLOG_CHANGELOG_USER_ORIG_CTXT);
+               if (ctxt == NULL ||
+                   (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
+                       GOTO(out_env, rc = -ENXIO);
+
+               rc = llog_cat_process(env, ctxt->loc_handle,
+                                     mdd_changelog_gc_cb, &mcgc, 0, 0);
+               if (rc != 0 || mcgc.mcgc_found == false)
+                       break;
+               llog_ctxt_put(ctxt);
+
+               CWARN("%s: Force deregister of ChangeLog user cl%d idle more "
+                     "than %us\n", mdd2obd_dev(mdd)->obd_name, mcgc.mcgc_id,
+                     mcgc.mcgc_maxtime);
+
+               mdd_changelog_user_purge(env, mdd, mcgc.mcgc_id);
+
+               /* try again to search for another candidate */
+               mcgc.mcgc_found = false;
+               mcgc.mcgc_maxtime = 0;
+               mcgc.mcgc_maxindexes = 0;
+       }
+
+out_env:
+       if (ctxt != NULL)
+               llog_ctxt_put(ctxt);
+
+       lu_env_fini(env);
+       GOTO(out, rc);
+out:
+       if (env)
+               OBD_FREE_PTR(env);
+       mdd->mdd_cl.mc_gc_task = NULL;
        return rc;
 }
 
-/** Add a changelog_ext entry \a rec to the changelog llog
+/** Add a changelog entry \a rec to the changelog llog
  * \param mdd
  * \param rec
  * \param handle - currently ignored since llogs start their own transaction;
  *             this will hopefully be fixed in llog rewrite
  * \retval 0 ok
  */
-static int
-mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
-                       struct llog_changelog_ext_rec *rec, struct thandle *th)
+int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
+                       struct llog_changelog_rec *rec, struct thandle *th)
 {
        struct obd_device       *obd = mdd2obd_dev(mdd);
        struct llog_ctxt        *ctxt;
+       struct thandle          *llog_th;
        int                      rc;
+       bool                     run_gc_task;
+
+       rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
+                                           changelog_rec_varsize(&rec->cr));
 
-       rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
        /* llog_lvfs_write_rec sets the llog tail len */
        rec->cr_hdr.lrh_type = CHANGELOG_REC;
        rec->cr.cr_time = cl_time();
@@ -784,71 +957,100 @@ mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
        if (ctxt == NULL)
                return -ENXIO;
 
+       llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
+       if (IS_ERR(llog_th))
+               GOTO(out_put, rc = PTR_ERR(llog_th));
+
        /* nested journal transaction */
-       rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
+       rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
+
+       /* time to recover some space ?? */
+       spin_lock(&mdd->mdd_cl.mc_lock);
+       if (unlikely(mdd->mdd_changelog_gc && (ktime_get_real_seconds() -
+           mdd->mdd_cl.mc_gc_time > mdd->mdd_changelog_min_gc_interval) &&
+           mdd->mdd_cl.mc_gc_task == NULL &&
+           llog_cat_free_space(ctxt->loc_handle) <=
+                               mdd->mdd_changelog_min_free_cat_entries)) {
+               CWARN("%s: low on changelog_catalog free entries, starting "
+                     "ChangeLog garbage collection thread\n", obd->obd_name);
+
+               /* indicate further kthread run will occur outside right after
+                * critical section
+                */
+               mdd->mdd_cl.mc_gc_task = (struct task_struct *)(-1);
+               run_gc_task = true;
+       }
+       spin_unlock(&mdd->mdd_cl.mc_lock);
+       if (run_gc_task) {
+               struct task_struct *gc_task;
+
+               gc_task = kthread_run(mdd_chlg_garbage_collect, mdd,
+                                     "chlg_gc_thread");
+               if (IS_ERR(gc_task)) {
+                       CERROR("%s: cannot start ChangeLog garbage collection "
+                              "thread: rc = %ld\n", obd->obd_name,
+                              PTR_ERR(gc_task));
+                       mdd->mdd_cl.mc_gc_task = NULL;
+               } else {
+                       CDEBUG(D_HA, "%s: ChangeLog garbage collection thread "
+                              "has started with Pid %d\n", obd->obd_name,
+                              gc_task->pid);
+                       mdd->mdd_cl.mc_gc_task = gc_task;
+                       mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
+               }
+       }
+out_put:
        llog_ctxt_put(ctxt);
        if (rc > 0)
                rc = 0;
-
        return rc;
 }
 
-/** Store a namespace change changelog record
- * If this fails, we must fail the whole transaction; we don't
- * want the change to commit without the log entry.
- * \param target - mdd_object of change
- * \param parent - parent dir/object
- * \param tname - target name string
- * \param handle - transacion handle
- */
-int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
-                          enum changelog_rec_type type, unsigned flags,
-                          struct mdd_object *target, struct mdd_object *parent,
-                          const struct lu_name *tname, struct thandle *handle)
+static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
+                                        const struct lu_fid *sfid,
+                                        const struct lu_fid *spfid,
+                                        const struct lu_name *sname)
 {
-       struct llog_changelog_rec *rec;
-       struct lu_buf *buf;
-       int reclen;
-       int rc;
-       ENTRY;
+       struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
+       size_t extsize = sname->ln_namelen + 1;
 
-       /* Not recording */
-       if (!(mdd->mdd_cl.mc_flags & CLM_ON))
-               RETURN(0);
-       if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
-               RETURN(0);
+       LASSERT(sfid != NULL);
+       LASSERT(spfid != NULL);
+       LASSERT(sname != NULL);
 
-       LASSERT(target != NULL);
-       LASSERT(parent != NULL);
-       LASSERT(tname != NULL);
-       LASSERT(handle != NULL);
+       rnm->cr_sfid = *sfid;
+       rnm->cr_spfid = *spfid;
 
-       reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
-       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;
+       changelog_rec_name(rec)[rec->cr_namelen] = '\0';
+       strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
+       rec->cr_namelen += extsize;
+}
 
-       rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
-       rec->cr.cr_type = (__u32)type;
-       rec->cr.cr_tfid = *mdo2fid(target);
-       rec->cr.cr_pfid = *mdo2fid(parent);
-       rec->cr.cr_namelen = tname->ln_namelen;
-       memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
+void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
+{
+       struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
 
-       target->mod_cltime = cfs_time_current_64();
+       if (jobid == NULL || jobid[0] == '\0')
+               return;
 
-       rc = mdd_changelog_store(env, mdd, rec, handle);
-       if (rc < 0) {
-               CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
-                       rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
-                       PFID(&rec->cr.cr_pfid));
-               RETURN(-EFAULT);
-       }
+       strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
+}
 
-       RETURN(0);
+void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
+{
+       struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
+
+       ef->cr_extra_flags = eflags;
 }
 
+void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
+                                   __u64 uid, __u64 gid)
+{
+       struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
+
+       uidgid->cr_uid = uid;
+       uidgid->cr_gid = gid;
+}
 
 /** Store a namespace change changelog record
  * If this fails, we must fail the whole transaction; we don't
@@ -859,69 +1061,91 @@ int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
  * \param spfid - source parent fid
  * \param tname - target name string
  * \param sname - source name string
- * \param handle - transacion handle
+ * \param handle - transaction handle
  */
-static int mdd_changelog_ext_ns_store(const struct lu_env  *env,
-                                     struct mdd_device    *mdd,
-                                     enum changelog_rec_type type,
-                                     unsigned flags,
-                                     struct mdd_object    *target,
-                                     const struct lu_fid  *tpfid,
-                                     const struct lu_fid  *sfid,
-                                     const struct lu_fid  *spfid,
-                                     const struct lu_name *tname,
-                                     const struct lu_name *sname,
-                                     struct thandle *handle)
+int mdd_changelog_ns_store(const struct lu_env *env,
+                          struct mdd_device *mdd,
+                          enum changelog_rec_type type,
+                          enum changelog_rec_flags crf,
+                          struct mdd_object *target,
+                          const struct lu_fid *tpfid,
+                          const struct lu_fid *sfid,
+                          const struct lu_fid *spfid,
+                          const struct lu_name *tname,
+                          const struct lu_name *sname,
+                          struct thandle *handle)
 {
-       struct llog_changelog_ext_rec *rec;
-       struct lu_buf *buf;
-       int reclen;
-       int rc;
+       const struct lu_ucred           *uc = lu_ucred(env);
+       struct llog_changelog_rec       *rec;
+       struct lu_buf                   *buf;
+       int                              reclen;
+       __u64                            xflags = CLFE_INVALID;
+       int                              rc;
        ENTRY;
 
        /* Not recording */
        if (!(mdd->mdd_cl.mc_flags & CLM_ON))
                RETURN(0);
+
        if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
                RETURN(0);
 
-       LASSERT(sfid != NULL);
        LASSERT(tpfid != NULL);
        LASSERT(tname != NULL);
        LASSERT(handle != NULL);
 
-       reclen = llog_data_len(sizeof(*rec) +
-                              sname != NULL ? 1 + sname->ln_namelen : 0);
+       reclen = mdd_llog_record_calc_size(env, tname, sname);
        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_EXT_VERSION | (CLF_FLAGMASK & flags);
+       crf &= CLF_FLAGMASK;
+       crf |= CLF_EXTRA_FLAGS;
+
+       if (uc != NULL && uc->uc_jobid[0] != '\0')
+               crf |= CLF_JOBID;
+
+       if (sname != NULL)
+               crf |= CLF_RENAME;
+       else
+               crf |= CLF_VERSION;
+
+       xflags |= CLFE_UIDGID;
+
+       rec->cr.cr_flags = crf;
+
+       if (crf & CLF_EXTRA_FLAGS) {
+               mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
+               if (xflags & CLFE_UIDGID)
+                       mdd_changelog_rec_extra_uidgid(&rec->cr,
+                                                      uc->uc_uid, uc->uc_gid);
+       }
+
        rec->cr.cr_type = (__u32)type;
        rec->cr.cr_pfid = *tpfid;
-       rec->cr.cr_sfid = *sfid;
-       rec->cr.cr_spfid = *spfid;
        rec->cr.cr_namelen = tname->ln_namelen;
-       memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
-       if (sname) {
-               rec->cr.cr_name[tname->ln_namelen] = '\0';
-               memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
-                       sname->ln_namelen);
-               rec->cr.cr_namelen += 1 + sname->ln_namelen;
-       }
+       memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
+
+       if (crf & CLF_RENAME)
+               mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
+
+       if (crf & CLF_JOBID)
+               mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
 
        if (likely(target != NULL)) {
                rec->cr.cr_tfid = *mdo2fid(target);
-               target->mod_cltime = cfs_time_current_64();
+               target->mod_cltime = ktime_get();
        } else {
                fid_zero(&rec->cr.cr_tfid);
        }
 
-       rc = mdd_changelog_ext_store(env, mdd, rec, handle);
+       rc = mdd_changelog_store(env, mdd, rec, handle);
        if (rc < 0) {
-               CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
-                       rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
+               CERROR("%s: cannot store changelog record: type = %d, "
+                      "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
+                      mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
+                      PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
                return -EFAULT;
        }
 
@@ -1003,41 +1227,32 @@ static int mdd_linkea_prepare(const struct lu_env *env,
                              struct linkea_data *ldata)
 {
        int rc = 0;
-       int rc2 = 0;
        ENTRY;
 
        if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
-               return 0;
+               RETURN(0);
 
        LASSERT(oldpfid != NULL || newpfid != NULL);
 
-       if (mdd_obj->mod_flags & DEAD_OBJ) {
-               /* Prevent linkea to be updated which is NOT necessary. */
-               ldata->ld_reclen = 0;
-               /* No more links, don't bother */
+       if (mdd_obj->mod_flags & DEAD_OBJ)
+               /* Unnecessary to update linkEA for dead object.  */
                RETURN(0);
-       }
 
        if (oldpfid != NULL) {
                rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
                if (rc) {
-                       if ((check == 1) ||
-                           (rc != -ENODATA && rc != -ENOENT))
+                       if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
                                RETURN(rc);
+
                        /* No changes done. */
                        rc = 0;
                }
        }
 
        /* If renaming, add the new record */
-       if (newpfid != NULL) {
-               /* even if the add fails, we still delete the out-of-date
-                * old link */
-               rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
-                                     first, check);
-       }
-
-       rc = rc != 0 ? rc : rc2;
+       if (newpfid != NULL)
+               rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
+                                    first, check);
 
        RETURN(rc);
 }
@@ -1059,40 +1274,37 @@ int mdd_links_rename(const struct lu_env *env,
                ldata = &mdd_env_info(env)->mti_link_data;
                memset(ldata, 0, sizeof(*ldata));
                rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
-                                       newpfid, newlname, first, check,
-                                       ldata);
-               if (rc != 0)
+                                       newpfid, newlname, first, check, ldata);
+               if (rc)
                        GOTO(out, rc);
        }
 
-       if (ldata->ld_reclen != 0)
+       if (!(mdd_obj->mod_flags & DEAD_OBJ))
                rc = mdd_links_write(env, mdd_obj, ldata, handle);
-       EXIT;
+
+       GOTO(out, rc);
+
 out:
        if (rc != 0) {
-               int error = 1;
-               if (rc == -EOVERFLOW || rc == -ENOSPC)
-                       error = 0;
-               if (oldpfid == NULL)
-                       CDEBUG(error ? D_ERROR : D_OTHER,
-                              "link_ea add '%.*s' failed %d "DFID"\n",
-                              newlname->ln_namelen, newlname->ln_name,
+               if (newlname == NULL)
+                       CERROR("link_ea add failed %d "DFID"\n",
                               rc, PFID(mdd_object_fid(mdd_obj)));
+               else if (oldpfid == NULL)
+                       CERROR("link_ea add '%.*s' failed %d "DFID"\n",
+                              newlname->ln_namelen, newlname->ln_name, rc,
+                              PFID(mdd_object_fid(mdd_obj)));
                else if (newpfid == NULL)
-                       CDEBUG(error ? D_ERROR : D_OTHER,
-                              "link_ea del '%.*s' failed %d "DFID"\n",
-                              oldlname->ln_namelen, oldlname->ln_name,
-                              rc, PFID(mdd_object_fid(mdd_obj)));
+                       CERROR("link_ea del '%.*s' failed %d "DFID"\n",
+                              oldlname->ln_namelen, oldlname->ln_name, rc,
+                              PFID(mdd_object_fid(mdd_obj)));
                else
-                       CDEBUG(error ? D_ERROR : D_OTHER,
-                              "link_ea rename '%.*s'->'%.*s' failed %d "
-                              DFID"\n",
-                              oldlname->ln_namelen, oldlname->ln_name,
-                              newlname->ln_namelen, newlname->ln_name,
-                              rc, PFID(mdd_object_fid(mdd_obj)));
+                       CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
+                              "\n", oldlname->ln_namelen, oldlname->ln_name,
+                              newlname->ln_namelen, newlname->ln_name, rc,
+                              PFID(mdd_object_fid(mdd_obj)));
        }
 
-       if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
+       if (is_vmalloc_addr(ldata->ld_buf))
                /* if we vmalloced a large buffer drop it */
                lu_buf_free(ldata->ld_buf);
 
@@ -1121,51 +1333,6 @@ static inline int mdd_links_del(const struct lu_env *env,
 }
 
 /** Read the link EA into a temp buffer.
- * Uses the mdd_thread_info::mti_big_buf since it is generally large.
- * A pointer to the buffer is stored in \a ldata::ld_buf.
- *
- * \retval 0 or error
- */
-int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
-                  struct linkea_data *ldata)
-{
-       int rc;
-
-       if (!mdd_object_exists(mdd_obj))
-               return -ENODATA;
-
-       /* First try a small buf */
-       LASSERT(env != NULL);
-       ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
-                                              PAGE_CACHE_SIZE);
-       if (ldata->ld_buf->lb_buf == NULL)
-               return -ENOMEM;
-
-       rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
-                         BYPASS_CAPA);
-       if (rc == -ERANGE) {
-               /* Buf was too small, figure out what we need. */
-               lu_buf_free(ldata->ld_buf);
-               rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
-                                  XATTR_NAME_LINK, BYPASS_CAPA);
-               if (rc < 0)
-                       return rc;
-               ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
-               if (ldata->ld_buf->lb_buf == NULL)
-                       return -ENOMEM;
-               rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
-                                 XATTR_NAME_LINK, BYPASS_CAPA);
-       }
-       if (rc < 0) {
-               lu_buf_free(ldata->ld_buf);
-               ldata->ld_buf = NULL;
-               return rc;
-       }
-
-       return linkea_init(ldata);
-}
-
-/** Read the link EA into a temp buffer.
  * Uses the name_buf since it is generally large.
  * \retval IS_ERR err
  * \retval ptr to \a lu_buf (always \a mti_big_buf)
@@ -1173,7 +1340,7 @@ int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
 struct lu_buf *mdd_links_get(const struct lu_env *env,
                             struct mdd_object *mdd_obj)
 {
-       struct linkea_data ldata = { 0 };
+       struct linkea_data ldata = { NULL };
        int rc;
 
        rc = mdd_links_read(env, mdd_obj, &ldata);
@@ -1183,18 +1350,33 @@ struct lu_buf *mdd_links_get(const struct lu_env *env,
 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
                    struct linkea_data *ldata, struct thandle *handle)
 {
-       const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
-                                                    ldata->ld_leh->leh_len);
+       const struct lu_buf *buf;
+       int                 rc;
+
+       if (ldata == NULL || ldata->ld_buf == NULL ||
+           ldata->ld_leh == NULL)
+               return 0;
 
        if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
                return 0;
 
-       return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
-                            mdd_object_capa(env, mdd_obj));
+again:
+       buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
+                               ldata->ld_leh->leh_len);
+       rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
+       if (unlikely(rc == -ENOSPC)) {
+               rc = linkea_overflow_shrink(ldata);
+               if (likely(rc > 0))
+                       goto again;
+       }
+
+       return rc;
 }
 
-int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
-                         struct thandle *handle, struct linkea_data *ldata)
+static int mdd_declare_links_add(const struct lu_env *env,
+                                struct mdd_object *mdd_obj,
+                                struct thandle *handle,
+                                struct linkea_data *ldata)
 {
        int     rc;
        int     ea_len;
@@ -1204,14 +1386,14 @@ int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
                ea_len = ldata->ld_leh->leh_len;
                linkea = ldata->ld_buf->lb_buf;
        } else {
-               ea_len = DEFAULT_LINKEA_SIZE;
+               ea_len = MAX_LINKEA_SIZE;
                linkea = NULL;
        }
 
-       /* XXX: max size? */
        rc = mdo_declare_xattr_set(env, mdd_obj,
                                   mdd_buf_get_const(env, linkea, ea_len),
                                   XATTR_NAME_LINK, 0, handle);
+
        return rc;
 }
 
@@ -1238,16 +1420,20 @@ static int mdd_declare_link(const struct lu_env *env,
                            struct lu_attr *la,
                            struct linkea_data *data)
 {
+       struct lu_fid tfid = *mdo2fid(c);
        int rc;
 
-       rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
+       if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
+               tfid.f_oid = cfs_fail_val;
+
+       rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
                                      name->ln_name, handle);
        if (rc != 0)
-                return rc;
+               return rc;
 
-        rc = mdo_declare_ref_add(env, c, handle);
-        if (rc)
-                return rc;
+       rc = mdo_declare_ref_add(env, c, handle);
+       if (rc != 0)
+               return rc;
 
        la->la_valid = LA_CTIME | LA_MTIME;
        rc = mdo_declare_attr_set(env, p, la, handle);
@@ -1256,16 +1442,16 @@ static int mdd_declare_link(const struct lu_env *env,
 
        la->la_valid = LA_CTIME;
        rc = mdo_declare_attr_set(env, c, la, handle);
-        if (rc)
-                return rc;
+       if (rc != 0)
+               return rc;
 
        rc = mdd_declare_links_add(env, c, handle, data);
-        if (rc)
-                return rc;
+       if (rc != 0)
+               return rc;
 
-        rc = mdd_declare_changelog_store(env, mdd, name, handle);
+       rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
 
-        return rc;
+       return rc;
 }
 
 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
@@ -1278,20 +1464,30 @@ static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
         struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
        struct lu_attr    *cattr = MDD_ENV_VAR(env, cattr);
        struct lu_attr    *tattr = MDD_ENV_VAR(env, tattr);
-        struct mdd_device *mdd = mdo2mdd(src_obj);
-        struct thandle *handle;
+       struct mdd_device *mdd = mdo2mdd(src_obj);
+       struct thandle *handle;
+       struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
        struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
        int rc;
        ENTRY;
 
-       rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_sobj, cattr);
        if (rc != 0)
                RETURN(rc);
 
-       rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_tobj, tattr);
        if (rc != 0)
                RETURN(rc);
 
+       /*
+        * If we are using project inheritance, we only allow hard link
+        * creation in our tree when the project IDs are the same;
+        * otherwise the tree quota mechanism could be circumvented.
+        */
+       if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
+           (tattr->la_projid != cattr->la_projid))
+               RETURN(-EXDEV);
+
         handle = mdd_trans_create(env, mdd);
         if (IS_ERR(handle))
                 GOTO(out_pending, rc = PTR_ERR(handle));
@@ -1301,6 +1497,14 @@ static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
        LASSERT(ma->ma_attr.la_valid & LA_CTIME);
        la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
 
+       /* Note: even this function will change ldata, but it comes from
+        * thread_info, which is completely temporary and only seen in
+        * this function, so we do not need reset ldata once it fails.*/
+       rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdo2fid(mdd_tobj),
+                               lname, 0, 0, ldata);
+       if (rc != 0)
+               GOTO(stop, rc);
+
        rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
                              la, ldata);
         if (rc)
@@ -1316,26 +1520,18 @@ static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
        if (rc)
                GOTO(out_unlock, rc);
 
-       rc = mdo_ref_add(env, mdd_sobj, handle);
-       if (rc)
-               GOTO(out_unlock, rc);
-
-
-       if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
-               struct lu_fid tfid = *mdo2fid(mdd_sobj);
-
-               tfid.f_oid++;
-               rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
-                                            mdd_object_type(mdd_sobj),
-                                            name, handle,
-                                            mdd_object_capa(env, mdd_tobj));
-       } else {
-               rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
-                                            mdd_object_type(mdd_sobj),
-                                            name, handle,
-                                            mdd_object_capa(env, mdd_tobj));
+       if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
+               rc = mdo_ref_add(env, mdd_sobj, handle);
+               if (rc != 0)
+                       GOTO(out_unlock, rc);
        }
 
+       *tfid = *mdo2fid(mdd_sobj);
+       if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
+               tfid->f_oid = cfs_fail_val;
+
+       rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
+                                    mdd_object_type(mdd_sobj), name, handle);
        if (rc != 0) {
                mdo_ref_del(env, mdd_sobj, handle);
                GOTO(out_unlock, rc);
@@ -1348,54 +1544,45 @@ static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
 
        la->la_valid = LA_CTIME;
        rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
-       if (rc == 0) {
-               rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
-                                       mdo2fid(mdd_tobj), lname, 0, 0,
-                                       ldata);
-               if (rc == 0)
-                       mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
-                                     lname, handle, ldata, 0);
-               /* The failure of links_add should not cause the link
-                * failure, reset rc here */
-               rc = 0;
-       }
-        EXIT;
+       if (rc == 0)
+               /* Note: The failure of links_add should not cause the
+                * link failure, so do not check return value. */
+               mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
+                             lname, handle, ldata, 0);
+
+       EXIT;
 out_unlock:
-        mdd_write_unlock(env, mdd_sobj);
-        if (rc == 0)
+       mdd_write_unlock(env, mdd_sobj);
+       if (rc == 0)
                rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
-                                           mdd_tobj, lname, handle);
+                                           mdo2fid(mdd_tobj), NULL, NULL,
+                                           lname, NULL, handle);
 stop:
-       mdd_trans_stop(env, mdd, rc, handle);
-
-       if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
+       rc = mdd_trans_stop(env, mdd, rc, handle);
+       if (is_vmalloc_addr(ldata->ld_buf))
                /* if we vmalloced a large buffer drop it */
                lu_buf_free(ldata->ld_buf);
 out_pending:
-        return rc;
+       return rc;
 }
 
-static int mdd_mark_dead_object(const struct lu_env *env,
+static int mdd_mark_orphan_object(const struct lu_env *env,
                                struct mdd_object *obj, struct thandle *handle,
                                bool declare)
 {
        struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
        int rc;
 
-       if (!declare)
-               obj->mod_flags |= DEAD_OBJ;
-
        if (!S_ISDIR(mdd_object_type(obj)))
                return 0;
 
        attr->la_valid = LA_FLAGS;
-       attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
+       attr->la_flags = LUSTRE_ORPHAN_FL;
 
        if (declare)
                rc = mdo_declare_attr_set(env, obj, attr, handle);
        else
-               rc = mdo_attr_set(env, obj, attr, handle,
-                                 mdd_object_capa(env, obj));
+               rc = mdo_attr_set(env, obj, attr, handle);
 
        return rc;
 }
@@ -1406,15 +1593,18 @@ static int mdd_declare_finish_unlink(const struct lu_env *env,
 {
        int     rc;
 
-       rc = mdd_mark_dead_object(env, obj, handle, true);
+       /* Sigh, we do not know if the unlink object will become orphan in
+        * declare phase, but fortunately the flags here does not matter
+        * in current declare implementation */
+       rc = mdd_mark_orphan_object(env, obj, handle, true);
        if (rc != 0)
                return rc;
 
-       rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
+       rc = mdo_declare_destroy(env, obj, handle);
        if (rc != 0)
                return rc;
 
-       rc = mdo_declare_destroy(env, obj, handle);
+       rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
        if (rc != 0)
                return rc;
 
@@ -1429,34 +1619,37 @@ int mdd_finish_unlink(const struct lu_env *env,
                      struct thandle *th)
 {
        int rc = 0;
-        int is_dir = S_ISDIR(ma->ma_attr.la_mode);
-        ENTRY;
+       int is_dir = S_ISDIR(ma->ma_attr.la_mode);
+       ENTRY;
 
-        LASSERT(mdd_write_locked(env, obj) != 0);
+       LASSERT(mdd_write_locked(env, obj) != 0);
 
        if (ma->ma_attr.la_nlink == 0 || is_dir) {
-               rc = mdd_mark_dead_object(env, obj, th, false);
-               if (rc != 0)
-                       RETURN(rc);
-
-                /* add new orphan and the object
-                 * will be deleted during mdd_close() */
-                if (obj->mod_count) {
-                        rc = __mdd_orphan_add(env, obj, th);
-                        if (rc == 0)
-                                CDEBUG(D_HA, "Object "DFID" is inserted into "
-                                        "orphan list, open count = %d\n",
-                                        PFID(mdd_object_fid(obj)),
-                                        obj->mod_count);
-                        else
-                                CERROR("Object "DFID" fail to be an orphan, "
-                                       "open count = %d, maybe cause failed "
-                                       "open replay\n",
-                                        PFID(mdd_object_fid(obj)),
-                                        obj->mod_count);
-                } else {
+               /* add new orphan and the object
+                * will be deleted during mdd_close() */
+               obj->mod_flags |= DEAD_OBJ;
+               if (obj->mod_count) {
+                       rc = __mdd_orphan_add(env, obj, th);
+                       if (rc == 0)
+                               CDEBUG(D_HA, "Object "DFID" is inserted into "
+                                       "orphan list, open count = %d\n",
+                                       PFID(mdd_object_fid(obj)),
+                                       obj->mod_count);
+                       else
+                               CERROR("Object "DFID" fail to be an orphan, "
+                                      "open count = %d, maybe cause failed "
+                                      "open replay\n",
+                                       PFID(mdd_object_fid(obj)),
+                                       obj->mod_count);
+
+                       /* mark object as an orphan here, not
+                        * before __mdd_orphan_add() as racing
+                        * mdd_la_get() may propagate ORPHAN_OBJ
+                        * causing the asserition */
+                       rc = mdd_mark_orphan_object(env, obj, th, false);
+               } else {
                        rc = mdo_destroy(env, obj, th);
-                }
+               }
        } else if (!is_dir) {
                /* old files may not have link ea; ignore errors */
                mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
@@ -1531,7 +1724,7 @@ static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
                        return rc;
 
                /* FIXME: need changelog for remove entry */
-               rc = mdd_declare_changelog_store(env, mdd, name, handle);
+               rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
        }
 
        return rc;
@@ -1556,14 +1749,13 @@ static bool mdd_hsm_archive_exists(const struct lu_env *env,
 
                hsm_buf = mdd_buf_get(env, NULL, 0);
                lu_buf_alloc(hsm_buf, buflen);
-               rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
-                                  mdd_object_capa(env, obj));
+               rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
                rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
                lu_buf_free(hsm_buf);
                if (rc < 0)
                        RETURN(false);
 
-               ma->ma_valid = MA_HSM;
+               ma->ma_valid |= MA_HSM;
        }
        if (ma->ma_hsm.mh_flags & HS_EXISTS)
                RETURN(true);
@@ -1591,7 +1783,7 @@ static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
        struct mdd_object *mdd_cobj = NULL;
        struct mdd_device *mdd = mdo2mdd(pobj);
        struct thandle    *handle;
-       int rc, is_dir = 0;
+       int rc, is_dir = 0, cl_flags = 0;
        ENTRY;
 
        /* cobj == NULL means only delete name entry */
@@ -1601,17 +1793,22 @@ static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
                        RETURN(-ENOENT);
        }
 
-       rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_pobj, pattr);
        if (rc)
                RETURN(rc);
 
        if (likely(mdd_cobj != NULL)) {
                /* fetch cattr */
-               rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
+               rc = mdd_la_get(env, mdd_cobj, cattr);
                if (rc)
                        RETURN(rc);
 
                is_dir = S_ISDIR(cattr->la_mode);
+               /* search for an existing archive.
+                * we should check ahead as the object
+                * can be destroyed in this transaction */
+               if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
+                       cl_flags |= CLF_UNLINK_HSM_EXISTS;
        }
 
        rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
@@ -1635,20 +1832,22 @@ static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
                mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
 
        if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
-               rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
-                                       mdd_object_capa(env, mdd_pobj));
+               rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
                if (rc)
                        GOTO(cleanup, rc);
        }
 
+       if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
+           OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
+               GOTO(cleanup, rc = 0);
+
        if (likely(mdd_cobj != NULL)) {
                rc = mdo_ref_del(env, mdd_cobj, handle);
                if (rc != 0) {
                        __mdd_index_insert_only(env, mdd_pobj,
                                                mdo2fid(mdd_cobj),
                                                mdd_object_type(mdd_cobj),
-                                               name, handle,
-                                               mdd_object_capa(env, mdd_pobj));
+                                               name, handle);
                        GOTO(cleanup, rc);
                }
 
@@ -1657,7 +1856,7 @@ static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
                        mdo_ref_del(env, mdd_cobj, handle);
 
                /* fetch updated nlink */
-               rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
+               rc = mdd_la_get(env, mdd_cobj, cattr);
                if (rc)
                        GOTO(cleanup, rc);
        }
@@ -1687,38 +1886,42 @@ static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
        ma->ma_attr = *cattr;
        ma->ma_valid |= MA_INODE;
        rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
+       if (rc != 0)
+               GOTO(cleanup, rc);
 
        /* fetch updated nlink */
-       if (rc == 0)
-               rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_cobj, cattr);
+       /* if object is removed then we can't get its attrs,
+        * use last get */
+       if (rc == -ENOENT) {
+               cattr->la_nlink = 0;
+               rc = 0;
+       }
 
-       /* if object is removed then we can't get its attrs, use last get */
        if (cattr->la_nlink == 0) {
                ma->ma_attr = *cattr;
                ma->ma_valid |= MA_INODE;
        }
+
        EXIT;
 cleanup:
        if (likely(mdd_cobj != NULL))
                mdd_write_unlock(env, mdd_cobj);
 
        if (rc == 0) {
-               int cl_flags = 0;
-
-               if (cattr->la_nlink == 0) {
+               if (cattr->la_nlink == 0)
                        cl_flags |= CLF_UNLINK_LAST;
-                       /* search for an existing archive */
-                       if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
-                               cl_flags |= CLF_UNLINK_HSM_EXISTS;
-               }
+               else
+                       cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
 
                rc = mdd_changelog_ns_store(env, mdd,
                        is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
-                       mdd_cobj, mdd_pobj, lname, handle);
+                       mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
+                       handle);
        }
 
 stop:
-       mdd_trans_stop(env, mdd, rc, handle);
+       rc = mdd_trans_stop(env, mdd, rc, handle);
 
        return rc;
 }
@@ -1738,9 +1941,11 @@ static int mdd_cd_sanity_check(const struct lu_env *env,
         RETURN(0);
 }
 
-static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
-                           struct md_object *cobj, const struct md_op_spec *spec,
-                           struct md_attr *ma)
+static int mdd_create_data(const struct lu_env *env,
+                          struct md_object *pobj,
+                          struct md_object *cobj,
+                          const struct md_op_spec *spec,
+                          struct md_attr *ma)
 {
        struct mdd_device *mdd = mdo2mdd(cobj);
        struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
@@ -1765,22 +1970,22 @@ static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
         *    striping can be specified or not
         * 2) CMD?
         */
-       rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
+       rc = mdd_la_get(env, son, attr);
        if (rc)
                RETURN(rc);
 
        /* calling ->ah_make_hint() is used to transfer information from parent */
        mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
 
-        handle = mdd_trans_create(env, mdd);
-        if (IS_ERR(handle))
-                GOTO(out_free, rc = PTR_ERR(handle));
+       handle = mdd_trans_create(env, mdd);
+       if (IS_ERR(handle))
+               GOTO(out_free, rc = PTR_ERR(handle));
 
-        /*
-         * XXX: Setting the lov ea is not locked but setting the attr is locked?
-         * Should this be fixed?
-         */
-       CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
+       /*
+        * XXX: Setting the lov ea is not locked but setting the attr is locked?
+        * Should this be fixed?
+        */
+       CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
               spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
               spec->sp_cr_flags, spec->no_create);
 
@@ -1796,7 +2001,7 @@ static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
        if (rc)
                GOTO(stop, rc);
 
-       rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
+       rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
        if (rc)
                GOTO(stop, rc);
 
@@ -1805,7 +2010,7 @@ static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
                GOTO(stop, rc);
 
        rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
-                         0, handle, mdd_object_capa(env, son));
+                         0, handle);
 
        if (rc)
                GOTO(stop, rc);
@@ -1813,7 +2018,8 @@ static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
        rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
 
 stop:
-       mdd_trans_stop(env, mdd, rc, handle);
+       rc = mdd_trans_stop(env, mdd, rc, handle);
+
 out_free:
        RETURN(rc);
 }
@@ -1821,24 +2027,15 @@ out_free:
 static int mdd_declare_object_initialize(const struct lu_env *env,
                                         struct mdd_object *parent,
                                         struct mdd_object *child,
-                                        struct lu_attr *attr,
+                                        const struct lu_attr *attr,
                                         struct thandle *handle)
 {
        int rc;
        ENTRY;
 
-       /*
-        * inode mode has been set in creation time, and it's based on umask,
-        * la_mode and acl, don't set here again! (which will go wrong
-        * because below function doesn't consider umask).
-        * I'd suggest set all object attributes in creation time, see above.
-        */
        LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
-       attr->la_valid &= ~(LA_MODE | LA_TYPE);
-       rc = mdo_declare_attr_set(env, child, attr, handle);
-       attr->la_valid |= LA_MODE | LA_TYPE;
-       if (rc != 0 || !S_ISDIR(attr->la_mode))
-               RETURN(rc);
+       if (!S_ISDIR(attr->la_mode))
+               RETURN(0);
 
        rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
                                      dot, handle);
@@ -1861,28 +2058,17 @@ static int mdd_object_initialize(const struct lu_env *env,
                                 struct lu_attr *attr, struct thandle *handle,
                                 const struct md_op_spec *spec)
 {
-        int rc;
-        ENTRY;
-
-        /*
-         * Update attributes for child.
-         *
-         * FIXME:
-         *  (1) the valid bits should be converted between Lustre and Linux;
-         *  (2) maybe, the child attributes should be set in OSD when creation.
-         */
+       int rc = 0;
+       ENTRY;
 
-       rc = mdd_attr_set_internal(env, child, attr, handle, 0);
-       /* arguments are supposed to stay the same */
        if (S_ISDIR(attr->la_mode)) {
                 /* Add "." and ".." for newly created dir */
                 mdo_ref_add(env, child, handle);
                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
-                                            S_IFDIR, dot, handle, BYPASS_CAPA);
+                                            S_IFDIR, dot, handle);
                if (rc == 0)
                        rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
-                                                    dotdot, handle,
-                                                    BYPASS_CAPA);
+                                                    dotdot, handle);
                if (rc != 0)
                        mdo_ref_del(env, child, handle);
        }
@@ -1922,9 +2108,9 @@ static int mdd_create_sanity_check(const struct lu_env *env,
        int rc;
        ENTRY;
 
-        /* EEXIST check */
-        if (mdd_is_dead_obj(obj))
-                RETURN(-ENOENT);
+       /* EEXIST check */
+       if (mdd_is_dead_obj(obj))
+               RETURN(-ENOENT);
 
        /*
          * In some cases this lookup is not needed - we know before if name
@@ -1946,6 +2132,24 @@ static int mdd_create_sanity_check(const struct lu_env *env,
                check_perm = false;
        }
 
+       if (S_ISDIR(cattr->la_mode) &&
+           unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
+           spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
+               const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
+
+               if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC) &&
+                   le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
+                       rc = -EINVAL;
+                       CERROR("%s: invalid lmv_user_md: magic = %x, "
+                              "stripe_offset = %d, stripe_count = %u: "
+                              "rc = %d\n", mdd2obd_dev(m)->obd_name,
+                               le32_to_cpu(lum->lum_magic),
+                              (int)le32_to_cpu(lum->lum_stripe_offset),
+                              le32_to_cpu(lum->lum_stripe_count), rc);
+                       return rc;
+               }
+       }
+
        rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
        if (rc != 0)
                RETURN(rc);
@@ -1959,19 +2163,29 @@ static int mdd_create_sanity_check(const struct lu_env *env,
                }
        }
 
+       /* Inherit project ID from parent directory */
+       if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
+               cattr->la_projid = pattr->la_projid;
+               if (S_ISDIR(cattr->la_mode)) {
+                       cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
+                       cattr->la_valid |= LA_FLAGS;
+               }
+               cattr->la_valid |= LA_PROJID;
+       }
+
        rc = mdd_name_check(m, lname);
        if (rc < 0)
                RETURN(rc);
 
        switch (cattr->la_mode & S_IFMT) {
-        case S_IFLNK: {
-                unsigned int symlen = strlen(spec->u.sp_symname) + 1;
+       case S_IFLNK: {
+               unsigned int symlen = strlen(spec->u.sp_symname) + 1;
 
-                if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
-                        RETURN(-ENAMETOOLONG);
-                else
-                        RETURN(0);
-        }
+               if (symlen > m->mdd_dt_conf.ddp_symlink_max)
+                       RETURN(-ENAMETOOLONG);
+               else
+                       RETURN(0);
+       }
         case S_IFDIR:
         case S_IFREG:
         case S_IFCHR:
@@ -1987,7 +2201,7 @@ static int mdd_create_sanity_check(const struct lu_env *env,
         RETURN(rc);
 }
 
-static int mdd_declare_object_create(const struct lu_env *env,
+static int mdd_declare_create_object(const struct lu_env *env,
                                     struct mdd_device *mdd,
                                     struct mdd_object *p, struct mdd_object *c,
                                     struct lu_attr *attr,
@@ -1997,12 +2211,13 @@ static int mdd_declare_object_create(const struct lu_env *env,
                                     struct lu_buf *acl_buf,
                                     struct dt_allocation_hint *hint)
 {
+       const struct lu_buf *buf;
        int rc;
 
-       rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
+       rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
                                                hint);
-        if (rc)
-                GOTO(out, rc);
+       if (rc)
+               GOTO(out, rc);
 
 #ifdef CONFIG_FS_POSIX_ACL
        if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
@@ -2032,8 +2247,6 @@ static int mdd_declare_object_create(const struct lu_env *env,
        /* replay case, create LOV EA from client data */
        if (spec->no_create ||
            (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
-               const struct lu_buf *buf;
-
                buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
                                        spec->u.sp_ea.eadatalen);
                rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
@@ -2053,6 +2266,16 @@ static int mdd_declare_object_create(const struct lu_env *env,
                 if (rc)
                         GOTO(out, rc);
         }
+
+       if (spec->sp_cr_file_secctx_name != NULL) {
+               buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
+                                       spec->sp_cr_file_secctx_size);
+               rc = mdo_declare_xattr_set(env, c, buf,
+                                          spec->sp_cr_file_secctx_name, 0,
+                                          handle);
+               if (rc < 0)
+                       GOTO(out, rc);
+       }
 out:
        return rc;
 }
@@ -2070,7 +2293,7 @@ static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
 {
        int rc;
 
-       rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
+       rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
                                       def_acl_buf, acl_buf, hint);
        if (rc)
                GOTO(out, rc);
@@ -2103,17 +2326,10 @@ static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
                if (rc)
                        return rc;
 
-               rc = mdd_declare_changelog_store(env, mdd, name, handle);
+               rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
                if (rc)
                        return rc;
        }
-
-       /* XXX: For remote create, it should indicate the remote RPC
-        * will be sent after local transaction is finished, which
-        * is not very nice, but it will be removed once we fully support
-        * async update */
-       if (mdd_object_remote(p) && handle->th_update != NULL)
-               handle->th_update->tu_sent_after_local_trans = 1;
 out:
        return rc;
 }
@@ -2133,7 +2349,7 @@ static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
 
        mdd_read_lock(env, pobj, MOR_TGT_PARENT);
        rc = mdo_xattr_get(env, pobj, def_acl_buf,
-                          XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
+                          XATTR_NAME_ACL_DEFAULT);
        mdd_read_unlock(env, pobj);
        if (rc > 0) {
                /* If there are default ACL, fix mode/ACL by default ACL */
@@ -2163,17 +2379,18 @@ static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
 /**
  * Create a metadata object and initialize it, set acl, xattr.
  **/
-static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
+static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
                             struct mdd_object *son, struct lu_attr *attr,
                             struct md_op_spec *spec, struct lu_buf *acl_buf,
                             struct lu_buf *def_acl_buf,
                             struct dt_allocation_hint *hint,
                             struct thandle *handle)
 {
-       int                     rc;
+       const struct lu_buf *buf;
+       int rc;
 
        mdd_write_lock(env, son, MOR_TGT_CHILD);
-       rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
+       rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
                                        hint);
        if (rc)
                GOTO(unlock, rc);
@@ -2197,20 +2414,20 @@ static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
        /* During creation, there are only a few cases we need do xattr_set to
         * create stripes.
         * 1. regular file: see comments above.
-        * 2. create striped directory with provided stripeEA.
-        * 3. create striped directory because inherit default layout from the
-        * parent. */
+        * 2. dir: inherit default striping or pool settings from parent.
+        * 3. create striped directory with provided stripeEA.
+        * 4. create striped directory because inherit default layout from the
+        * parent.
+        */
        if (spec->no_create ||
            (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
            S_ISDIR(attr->la_mode)) {
-               const struct lu_buf *buf;
-
                buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
                                        spec->u.sp_ea.eadatalen);
                rc = mdo_xattr_set(env, son, buf,
                                   S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
                                                            XATTR_NAME_LOV, 0,
-                                  handle, BYPASS_CAPA);
+                                  handle);
                if (rc != 0)
                        GOTO(err_destroy, rc);
        }
@@ -2221,7 +2438,7 @@ static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
                /* set default acl */
                rc = mdo_xattr_set(env, son, def_acl_buf,
                                   XATTR_NAME_ACL_DEFAULT, 0,
-                                  handle, BYPASS_CAPA);
+                                  handle);
                if (rc)
                        GOTO(err_destroy, rc);
        }
@@ -2229,7 +2446,7 @@ static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
        if (acl_buf != NULL && acl_buf->lb_len > 0) {
                rc = mdo_xattr_set(env, son, acl_buf,
                                   XATTR_NAME_ACL_ACCESS,
-                                  0, handle, BYPASS_CAPA);
+                                  0, handle);
                if (rc)
                        GOTO(err_destroy, rc);
        }
@@ -2240,12 +2457,10 @@ static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
                struct dt_object *dt = mdd_object_child(son);
                const char *target_name = spec->u.sp_symname;
                int sym_len = strlen(target_name);
-               const struct lu_buf *buf;
                loff_t pos = 0;
 
                buf = mdd_buf_get_const(env, target_name, sym_len);
                rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
-                                               mdd_object_capa(env, son),
                                                uc->uc_cap &
                                                CFS_CAP_SYS_RESOURCE_MASK);
 
@@ -2255,6 +2470,15 @@ static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
                        GOTO(err_initlized, rc = -EFAULT);
        }
 
+       if (spec->sp_cr_file_secctx_name != NULL) {
+               buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
+                                       spec->sp_cr_file_secctx_size);
+               rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
+                                  0, handle);
+               if (rc < 0)
+                       GOTO(err_initlized, rc);
+       }
+
 err_initlized:
        if (unlikely(rc != 0)) {
                int rc2;
@@ -2276,12 +2500,93 @@ unlock:
        RETURN(rc);
 }
 
-/*
+static int mdd_index_delete(const struct lu_env *env,
+                           struct mdd_object *mdd_pobj,
+                           struct lu_attr *cattr,
+                           const struct lu_name *lname)
+{
+       struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
+       struct thandle *handle;
+       int rc;
+       ENTRY;
+
+       handle = mdd_trans_create(env, mdd);
+       if (IS_ERR(handle))
+               RETURN(PTR_ERR(handle));
+
+       rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
+                                     handle);
+       if (rc != 0)
+               GOTO(stop, rc);
+
+       if (S_ISDIR(cattr->la_mode)) {
+               rc = mdo_declare_ref_del(env, mdd_pobj, handle);
+               if (rc != 0)
+                       GOTO(stop, rc);
+       }
+
+       /* Since this will only be used in the error handler path,
+        * Let's set the thandle to be local and not mess the transno */
+       handle->th_local = 1;
+       rc = mdd_trans_start(env, mdd, handle);
+       if (rc)
+               GOTO(stop, rc);
+
+       rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
+                               S_ISDIR(cattr->la_mode), handle);
+       if (rc)
+               GOTO(stop, rc);
+stop:
+       rc = mdd_trans_stop(env, mdd, rc, handle);
+
+       RETURN(rc);
+}
+
+/**
  * Create object and insert it into namespace.
+ *
+ * Two operations have to be performed:
+ *
+ *  - an allocation of a new object (->do_create()), and
+ *  - an insertion into a parent index (->dio_insert()).
+ *
+ * Due to locking, operation order is not important, when both are
+ * successful, *but* error handling cases are quite different:
+ *
+ *  - if insertion is done first, and following object creation fails,
+ *  insertion has to be rolled back, but this operation might fail
+ *  also leaving us with dangling index entry.
+ *
+ *  - if creation is done first, is has to be undone if insertion fails,
+ *  leaving us with leaked space, which is not good but not fatal.
+ *
+ * It seems that creation-first is simplest solution, but it is sub-optimal
+ * in the frequent
+ *
+ * $ mkdir foo
+ * $ mkdir foo
+ *
+ * case, because second mkdir is bound to create object, only to
+ * destroy it immediately.
+ *
+ * To avoid this follow local file systems that do double lookup:
+ *
+ * 0. lookup -> -EEXIST (mdd_create_sanity_check())
+ * 1. create            (mdd_create_object_internal())
+ * 2. insert            (__mdd_index_insert(), lookup again)
+ *
+ * \param[in] pobj     parent object
+ * \param[in] lname    name of child being created
+ * \param[in,out] child        child object being created
+ * \param[in] spec     additional create parameters
+ * \param[in] ma       attributes for new child object
+ *
+ * \retval             0 on success
+ * \retval             negative errno on failure
  */
 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
                      const struct lu_name *lname, struct md_object *child,
-                     struct md_op_spec *spec, struct md_attrma)
+                     struct md_op_spec *spec, struct md_attr *ma)
 {
        struct mdd_thread_info  *info = mdd_env_info(env);
        struct lu_attr          *la = &info->mti_la_for_fix;
@@ -2300,43 +2605,7 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
        int                      rc2;
        ENTRY;
 
-        /*
-         * Two operations have to be performed:
-         *
-         *  - an allocation of a new object (->do_create()), and
-         *
-         *  - an insertion into a parent index (->dio_insert()).
-         *
-         * Due to locking, operation order is not important, when both are
-         * successful, *but* error handling cases are quite different:
-         *
-         *  - if insertion is done first, and following object creation fails,
-         *  insertion has to be rolled back, but this operation might fail
-         *  also leaving us with dangling index entry.
-         *
-         *  - if creation is done first, is has to be undone if insertion
-         *  fails, leaving us with leaked space, which is neither good, nor
-         *  fatal.
-         *
-         * It seems that creation-first is simplest solution, but it is
-         * sub-optimal in the frequent
-         *
-         *         $ mkdir foo
-         *         $ mkdir foo
-         *
-         * case, because second mkdir is bound to create object, only to
-         * destroy it immediately.
-         *
-         * To avoid this follow local file systems that do double lookup:
-         *
-         *     0. lookup -> -EEXIST (mdd_create_sanity_check())
-         *
-         *     1. create            (mdd_object_create_internal())
-         *
-         *     2. insert            (__mdd_index_insert(), lookup again)
-         */
-
-       rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_pobj, pattr);
        if (rc != 0)
                RETURN(rc);
 
@@ -2345,15 +2614,16 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
        if (rc)
                RETURN(rc);
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
                GOTO(out_free, rc = -EINPROGRESS);
 
        handle = mdd_trans_create(env, mdd);
        if (IS_ERR(handle))
                GOTO(out_free, rc = PTR_ERR(handle));
 
-       acl_buf.lb_buf = info->mti_xattr_buf;
-       acl_buf.lb_len = sizeof(info->mti_xattr_buf);
+       lu_buf_check_and_alloc(&info->mti_xattr_buf,
+                              mdd->mdd_dt_conf.ddp_max_ea_size);
+       acl_buf = info->mti_xattr_buf;
        def_acl_buf.lb_buf = info->mti_key;
        def_acl_buf.lb_len = sizeof(info->mti_key);
        rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
@@ -2363,7 +2633,7 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
        mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
 
        memset(ldata, 0, sizeof(*ldata));
-       if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT2)) {
+       if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
                struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
 
                tfid.f_oid--;
@@ -2378,26 +2648,26 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
        rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
                                handle, spec, ldata, &def_acl_buf, &acl_buf,
                                hint);
-        if (rc)
-                GOTO(out_stop, rc);
+       if (rc)
+               GOTO(out_stop, rc);
 
-        rc = mdd_trans_start(env, mdd, handle);
-        if (rc)
-                GOTO(out_stop, rc);
+       rc = mdd_trans_start(env, mdd, handle);
+       if (rc)
+               GOTO(out_stop, rc);
 
-       rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
+       rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
                               &def_acl_buf, hint, handle);
        if (rc != 0)
                GOTO(out_stop, rc);
 
        if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
                mdd_write_lock(env, son, MOR_TGT_CHILD);
+               son->mod_flags |= VOLATILE_OBJ;
                rc = __mdd_orphan_add(env, son, handle);
                GOTO(out_volatile, rc);
        } else {
                rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
-                                       attr->la_mode, name, handle,
-                                       mdd_object_capa(env, mdd_pobj));
+                                       attr->la_mode, name, handle);
                if (rc != 0)
                        GOTO(err_created, rc);
 
@@ -2415,14 +2685,12 @@ static int mdd_create(const struct lu_env *env, struct md_object *pobj,
        EXIT;
 err_insert:
        if (rc != 0) {
-               int rc2;
-
                if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
                        rc2 = __mdd_orphan_del(env, son, handle);
                else
                        rc2 = __mdd_index_delete(env, mdd_pobj, name,
                                                 S_ISDIR(attr->la_mode),
-                                                handle, BYPASS_CAPA);
+                                                handle);
                if (rc2 != 0)
                        goto out_stop;
 
@@ -2460,13 +2728,22 @@ out_volatile:
                                S_ISDIR(attr->la_mode) ? CL_MKDIR :
                                S_ISREG(attr->la_mode) ? CL_CREATE :
                                S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
-                               0, son, mdd_pobj, lname, handle);
+                               0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
+                               NULL, handle);
 out_stop:
        rc2 = mdd_trans_stop(env, mdd, rc, handle);
-       if (rc == 0)
+       if (rc == 0) {
+               /* If creation fails, it is most likely due to the remote update
+                * failure, because local transaction will mostly succeed at
+                * this stage. There is no easy way to rollback all of previous
+                * updates, so let's remove the object from namespace, and
+                * LFSCK should handle the orphan object. */
+               if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
+                       mdd_index_delete(env, mdd_pobj, attr, lname);
                rc = rc2;
+       }
 out_free:
-       if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
+       if (is_vmalloc_addr(ldata->ld_buf))
                /* if we vmalloced a large buffer drop it */
                lu_buf_free(ldata->ld_buf);
 
@@ -2539,6 +2816,17 @@ static int mdd_rename_sanity_check(const struct lu_env *env,
         * before mdd_rename and enable MDS_PERM_BYPASS. */
        LASSERT(sobj);
 
+       /*
+        * If we are using project inheritance, we only allow renames
+        * into our tree when the project IDs are the same; otherwise
+        * tree quota mechanism would be circumvented.
+        */
+       if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
+           tpattr->la_projid != cattr->la_projid) ||
+           ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
+           (pattr->la_projid != tpattr->la_projid)))
+               RETURN(-EXDEV);
+
        rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
        if (rc)
                RETURN(rc);
@@ -2567,8 +2855,8 @@ static int mdd_declare_rename(const struct lu_env *env,
                              struct mdd_object *mdd_tpobj,
                              struct mdd_object *mdd_sobj,
                              struct mdd_object *mdd_tobj,
-                             const struct lu_name *tname,
                              const struct lu_name *sname,
+                             const struct lu_name *tname,
                              struct md_attr *ma,
                              struct linkea_data *ldata,
                              struct thandle *handle)
@@ -2640,15 +2928,13 @@ static int mdd_declare_rename(const struct lu_env *env,
        if (rc != 0)
                return rc;
 
-        /* name from target dir (old name), we declare it unconditionally
-         * as mdd_rename() calls delete unconditionally as well. so just
-         * to balance declarations vs calls to change ... */
-        rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
-        if (rc)
-                return rc;
-
         if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
                 /* delete target child in target parent directory */
+               rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
+                                             handle);
+               if (rc)
+                       return rc;
+
                 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
                 if (rc)
                         return rc;
@@ -2676,7 +2962,7 @@ static int mdd_declare_rename(const struct lu_env *env,
                        return rc;
         }
 
-       rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
+       rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
         if (rc)
                 return rc;
 
@@ -2717,22 +3003,29 @@ static int mdd_rename(const struct lu_env *env,
                mdd_tobj = md2mdd_obj(tobj);
 
        mdd_sobj = mdd_object_find(env, mdd, lf);
+       if (IS_ERR(mdd_sobj))
+               RETURN(PTR_ERR(mdd_sobj));
 
-       rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_sobj, cattr);
        if (rc)
                GOTO(out_pending, rc);
 
-       rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_spobj, pattr);
        if (rc)
                GOTO(out_pending, rc);
 
        if (mdd_tobj) {
-               rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
+               rc = mdd_la_get(env, mdd_tobj, tattr);
                if (rc)
                        GOTO(out_pending, rc);
+               /* search for an existing archive.
+                * we should check ahead as the object
+                * can be destroyed in this transaction */
+               if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
+                       cl_flags |= CLF_RENAME_LAST_EXISTS;
        }
 
-       rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_tpobj, tpattr);
        if (rc)
                GOTO(out_pending, rc);
 
@@ -2745,13 +3038,22 @@ static int mdd_rename(const struct lu_env *env,
        if (rc < 0)
                GOTO(out_pending, rc);
 
+       /* FIXME: Should consider tobj and sobj too in rename_lock. */
+       rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
+       if (rc < 0)
+               GOTO(out_pending, rc);
+
         handle = mdd_trans_create(env, mdd);
         if (IS_ERR(handle))
                 GOTO(out_pending, rc = PTR_ERR(handle));
 
        memset(ldata, 0, sizeof(*ldata));
-       mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
-                          ltname, 1, 0, ldata);
+       rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
+                               lsname, mdd_object_fid(mdd_tpobj), ltname,
+                               1, 0, ldata);
+       if (rc)
+               GOTO(stop, rc);
+
        rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
                                mdd_tobj, lsname, ltname, ma, ldata, handle);
        if (rc)
@@ -2761,64 +3063,60 @@ static int mdd_rename(const struct lu_env *env,
         if (rc)
                 GOTO(stop, rc);
 
-        /* FIXME: Should consider tobj and sobj too in rename_lock. */
-       rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
-       if (rc < 0)
-               GOTO(cleanup_unlocked, rc);
-
        is_dir = S_ISDIR(cattr->la_mode);
 
-        /* Remove source name from source directory */
-        rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
-                                mdd_object_capa(env, mdd_spobj));
-        if (rc)
-                GOTO(cleanup, rc);
+       /* Remove source name from source directory */
+       rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
+       if (rc != 0)
+               GOTO(stop, rc);
 
-        /* "mv dir1 dir2" needs "dir1/.." link update */
-        if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
-                rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
-                                        mdd_object_capa(env, mdd_sobj));
+       /* "mv dir1 dir2" needs "dir1/.." link update */
+       if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
+               rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
                if (rc != 0)
                        GOTO(fixup_spobj2, rc);
 
                rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
-                                            dotdot, handle,
-                                            mdd_object_capa(env, mdd_sobj));
+                                            dotdot, handle);
                if (rc != 0)
                         GOTO(fixup_spobj, rc);
         }
 
-        /* Remove target name from target directory
-         * Here tobj can be remote one, so we do index_delete unconditionally
-         * and -ENOENT is allowed.
-         */
-        rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
-                                mdd_object_capa(env, mdd_tpobj));
-        if (rc != 0) {
-                if (mdd_tobj) {
-                        /* tname might been renamed to something else */
-                        GOTO(fixup_spobj, rc);
-                }
-                if (rc != -ENOENT)
-                        GOTO(fixup_spobj, rc);
-        }
+       if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
+               rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
+               if (rc != 0)
+                       /* tname might been renamed to something else */
+                       GOTO(fixup_spobj, rc);
+       }
 
         /* Insert new fid with target name into target dir */
        rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
-                               tname, handle, mdd_object_capa(env, mdd_tpobj));
+                               tname, handle);
        if (rc != 0)
                 GOTO(fixup_tpobj, rc);
 
         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
 
-        /* XXX: mdd_sobj must be local one if it is NOT NULL. */
-       if (mdd_sobj) {
-               la->la_valid = LA_CTIME;
-               rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
-               if (rc)
-                       GOTO(fixup_tpobj, rc);
-       }
+       /* XXX: mdd_sobj must be local one if it is NOT NULL. */
+       la->la_valid = LA_CTIME;
+       rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
+       if (rc)
+               GOTO(fixup_tpobj, rc);
+
+       /* Update the linkEA for the source object */
+       mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
+       rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
+                             mdo2fid(mdd_tpobj), ltname, handle, ldata,
+                             0, 0);
+       if (rc == -ENOENT)
+               /* Old files might not have EA entry */
+               mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
+                             lsname, handle, NULL, 0);
+       mdd_write_unlock(env, mdd_sobj);
+       /* We don't fail the transaction if the link ea can't be
+          updated -- fid2path will use alternate lookup method. */
+       rc = 0;
 
         /* Remove old target object
          * For tobj is remote case cmm layer has processed
@@ -2842,7 +3140,7 @@ static int mdd_rename(const struct lu_env *env,
                tobj_ref = 1;
 
                /* fetch updated nlink */
-               rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
+               rc = mdd_la_get(env, mdd_tobj, tattr);
                if (rc != 0) {
                        CERROR("%s: Failed to get nlink for tobj "
                                DFID": rc = %d\n",
@@ -2875,8 +3173,13 @@ static int mdd_rename(const struct lu_env *env,
                }
 
                /* fetch updated nlink */
-               rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
-               if (rc != 0) {
+               rc = mdd_la_get(env, mdd_tobj, tattr);
+               if (rc == -ENOENT) {
+                       /* the object got removed, let's
+                        * return the latest known attributes */
+                       tattr->la_nlink = 0;
+                       rc = 0;
+               } else if (rc != 0) {
                        CERROR("%s: Failed to get nlink for tobj "
                                DFID": rc = %d\n",
                                mdd2obd_dev(mdd)->obd_name,
@@ -2887,11 +3190,10 @@ static int mdd_rename(const struct lu_env *env,
                ma->ma_attr = *tattr;
                ma->ma_valid |= MA_INODE;
 
-               if (tattr->la_nlink == 0) {
+               if (tattr->la_nlink == 0)
                        cl_flags |= CLF_RENAME_LAST;
-                       if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
-                               cl_flags |= CLF_RENAME_LAST_EXISTS;
-               }
+               else
+                       cl_flags &= ~CLF_RENAME_LAST_EXISTS;
         }
 
        la->la_valid = LA_CTIME | LA_MTIME;
@@ -2902,29 +3204,15 @@ static int mdd_rename(const struct lu_env *env,
        if (mdd_spobj != mdd_tpobj) {
                la->la_valid = LA_CTIME | LA_MTIME;
                rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
+               if (rc != 0)
+                       GOTO(fixup_tpobj, rc);
        }
 
-       if (rc == 0 && mdd_sobj) {
-               mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
-               rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
-                                     mdo2fid(mdd_tpobj), ltname, handle, NULL,
-                                     0, 0);
-                if (rc == -ENOENT)
-                        /* Old files might not have EA entry */
-                        mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
-                                     lsname, handle, NULL, 0);
-                mdd_write_unlock(env, mdd_sobj);
-                /* We don't fail the transaction if the link ea can't be
-                   updated -- fid2path will use alternate lookup method. */
-                rc = 0;
-        }
-
         EXIT;
 
 fixup_tpobj:
         if (rc) {
-                rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
-                                         BYPASS_CAPA);
+               rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
                 if (rc2)
                         CWARN("tp obj fix error %d\n",rc2);
 
@@ -2939,7 +3227,7 @@ fixup_tpobj:
                        rc2 = __mdd_index_insert(env, mdd_tpobj,
                                                  mdo2fid(mdd_tobj),
                                                  mdd_object_type(mdd_tobj),
-                                                 tname, handle, BYPASS_CAPA);
+                                                 tname, handle);
                        if (rc2 != 0)
                                CWARN("tp obj fix error: rc = %d\n", rc2);
                }
@@ -2947,16 +3235,14 @@ fixup_tpobj:
 
 fixup_spobj:
        if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
-               rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
-                                             BYPASS_CAPA);
-
+               rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
                if (rc2)
                        CWARN("%s: sp obj dotdot delete error: rc = %d\n",
                               mdd2obd_dev(mdd)->obd_name, rc2);
 
 
                rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
-                                             dotdot, handle, BYPASS_CAPA);
+                                             dotdot, handle);
                if (rc2 != 0)
                        CWARN("%s: sp obj dotdot insert error: rc = %d\n",
                              mdd2obd_dev(mdd)->obd_name, rc2);
@@ -2966,7 +3252,7 @@ fixup_spobj2:
        if (rc != 0) {
                rc2 = __mdd_index_insert(env, mdd_spobj, lf,
                                         mdd_object_type(mdd_sobj), sname,
-                                        handle, BYPASS_CAPA);
+                                        handle);
                if (rc2 != 0)
                        CWARN("sp obj fix error: rc = %d\n", rc2);
        }
@@ -2975,15 +3261,13 @@ cleanup:
        if (tobj_locked)
                mdd_write_unlock(env, mdd_tobj);
 
-cleanup_unlocked:
        if (rc == 0)
-               rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
-                                               mdd_tobj, tpobj_fid, lf,
-                                               spobj_fid, ltname, lsname,
-                                               handle);
+               rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
+                                           mdd_tobj, tpobj_fid, lf, spobj_fid,
+                                           ltname, lsname, handle);
 
 stop:
-       mdd_trans_stop(env, mdd, rc, handle);
+       rc = mdd_trans_stop(env, mdd, rc, handle);
 
 out_pending:
        mdd_object_put(env, mdd_sobj);
@@ -2996,13 +3280,14 @@ out_pending:
  **/
 static int mdd_linkea_update_child_internal(const struct lu_env *env,
                                            struct mdd_object *parent,
+                                           struct mdd_object *newparent,
                                            struct mdd_object *child,
                                            const char *name, int namelen,
                                            struct thandle *handle,
                                            bool declare)
 {
        struct mdd_thread_info  *info = mdd_env_info(env);
-       struct linkea_data      ldata = {0};
+       struct linkea_data      ldata = { NULL };
        struct lu_buf           *buf = &info->mti_link_buf;
        int                     count;
        int                     rc = 0;
@@ -3032,7 +3317,7 @@ static int mdd_linkea_update_child_internal(const struct lu_env *env,
                                    &lname, &fid);
 
                if (strncmp(lname.ln_name, name, namelen) != 0 ||
-                   lu_fid_eq(&fid, mdd_object_fid(parent))) {
+                   !lu_fid_eq(&fid, mdd_object_fid(parent))) {
                        ldata.ld_lee = (struct link_ea_entry *)
                                       ((char *)ldata.ld_lee +
                                        ldata.ld_reclen);
@@ -3042,10 +3327,10 @@ static int mdd_linkea_update_child_internal(const struct lu_env *env,
                CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
                       mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
                       lname.ln_namelen, lname.ln_name,
-                      PFID(mdd_object_fid(parent)));
+                      PFID(mdd_object_fid(newparent)));
                /* update to the new parent fid */
                linkea_entry_pack(ldata.ld_lee, &lname,
-                                 mdd_object_fid(parent));
+                                 mdd_object_fid(newparent));
                if (declare)
                        rc = mdd_declare_links_add(env, child, handle, &ldata);
                else
@@ -3057,21 +3342,25 @@ static int mdd_linkea_update_child_internal(const struct lu_env *env,
 
 static int mdd_linkea_declare_update_child(const struct lu_env *env,
                                           struct mdd_object *parent,
+                                          struct mdd_object *newparent,
                                           struct mdd_object *child,
                                           const char *name, int namelen,
                                           struct thandle *handle)
 {
-       return mdd_linkea_update_child_internal(env, parent, child, name,
+       return mdd_linkea_update_child_internal(env, parent, newparent,
+                                               child, name,
                                                namelen, handle, true);
 }
 
 static int mdd_linkea_update_child(const struct lu_env *env,
                                   struct mdd_object *parent,
+                                  struct mdd_object *newparent,
                                   struct mdd_object *child,
                                   const char *name, int namelen,
                                   struct thandle *handle)
 {
-       return mdd_linkea_update_child_internal(env, parent, child, name,
+       return mdd_linkea_update_child_internal(env, parent, newparent,
+                                               child, name,
                                                namelen, handle, false);
 }
 
@@ -3080,33 +3369,20 @@ static int mdd_update_linkea_internal(const struct lu_env *env,
                                      struct mdd_object *mdd_sobj,
                                      struct mdd_object *mdd_tobj,
                                      const struct lu_name *child_name,
+                                     struct linkea_data *ldata,
                                      struct thandle *handle,
                                      int declare)
 {
        struct mdd_thread_info  *info = mdd_env_info(env);
-       struct linkea_data      *ldata = &info->mti_link_data;
        int                     count;
        int                     rc = 0;
        ENTRY;
 
-       rc = mdd_links_read(env, mdd_sobj, ldata);
-       if (rc != 0) {
-               if (rc == -ENOENT || rc == -ENODATA)
-                       rc = 0;
-               RETURN(rc);
-       }
-
-       if (declare)
-               rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
-       else
-               rc = mdd_links_write(env, mdd_tobj, ldata, handle);
-
-       if (rc != 0)
-               RETURN(rc);
+       LASSERT(ldata->ld_buf != NULL);
+       LASSERT(ldata->ld_leh != NULL);
 
        /* If it is mulitple links file, we need update the name entry for
         * all parent */
-       LASSERT(ldata->ld_leh != NULL);
        ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
        for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
                struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
@@ -3116,8 +3392,6 @@ static int mdd_update_linkea_internal(const struct lu_env *env,
 
                linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
                                    &lname, &fid);
-               ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
-                                                        ldata->ld_reclen);
                pobj = mdd_object_find(env, mdd, &fid);
                if (IS_ERR(pobj)) {
                        CWARN("%s: cannot find obj "DFID": rc = %ld\n",
@@ -3129,7 +3403,7 @@ static int mdd_update_linkea_internal(const struct lu_env *env,
                if (!mdd_object_exists(pobj)) {
                        CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
                              mdd2obd_dev(mdd)->obd_name, PFID(&fid));
-                       GOTO(next_put, rc);
+                       goto next_put;
                }
 
                if (pobj == mdd_pobj &&
@@ -3139,7 +3413,7 @@ static int mdd_update_linkea_internal(const struct lu_env *env,
                        CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
                              mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
                              PFID(&fid));
-                       GOTO(next_put, rc);
+                       goto next_put;
                }
 
                CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
@@ -3169,17 +3443,42 @@ static int mdd_update_linkea_internal(const struct lu_env *env,
                        if (rc)
                                GOTO(next_put, rc);
                } else {
-                       rc = __mdd_index_delete(env, pobj, lname.ln_name,
-                                               0, handle,
-                                               mdd_object_capa(env, pobj));
-                       if (rc)
+                       char *tmp_name = info->mti_key;
+
+                       if (lname.ln_namelen >= sizeof(info->mti_key)) {
+                               /* lnamelen is too big(> NAME_MAX + 16),
+                                * something wrong about this linkea, let's
+                                * skip it */
+                               CWARN("%s: the name %.*s is too long under "
+                                     DFID"\n", mdd2obd_dev(mdd)->obd_name,
+                                     lname.ln_namelen, lname.ln_name,
+                                     PFID(&fid));
+                               goto next_put;
+                       }
+
+                       /* Note: lname might be without \0 at the end, see
+                        * linkea_entry_unpack(), let's add extra \0 by
+                        * snprintf */
+                       snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
+                                lname.ln_namelen, lname.ln_name);
+                       lname.ln_name = tmp_name;
+
+                       /* Let's check if this linkEA still valid, before
+                        * it might be packed into the RPC buffer. */
+                       rc = mdd_lookup(env, &pobj->mod_obj, &lname,
+                                       &info->mti_fid, NULL);
+                       if (rc < 0 || !lu_fid_eq(&info->mti_fid,
+                                                mdd_object_fid(mdd_sobj)))
+                               GOTO(next_put, rc == -ENOENT ? 0 : rc);
+
+                       rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
+                       if (rc != 0)
                                GOTO(next_put, rc);
 
                        rc = __mdd_index_insert(env, pobj,
                                        mdd_object_fid(mdd_tobj),
                                        mdd_object_type(mdd_tobj),
-                                       lname.ln_name, handle,
-                                       mdd_object_capa(env, pobj));
+                                       tmp_name, handle);
                        if (rc != 0)
                                GOTO(next_put, rc);
 
@@ -3197,6 +3496,9 @@ next_put:
                mdd_object_put(env, pobj);
                if (rc != 0)
                        break;
+
+               ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
+                                                        ldata->ld_reclen);
        }
 
        RETURN(rc);
@@ -3219,8 +3521,7 @@ static int mdd_migrate_xattrs(const struct lu_env *env,
        int                     rc;
 
        /* retrieve xattr list from the old object */
-       list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
-                                   mdd_object_capa(env, mdd_sobj));
+       list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
        if (list_xsize == -ENODATA)
                return 0;
 
@@ -3233,8 +3534,7 @@ static int mdd_migrate_xattrs(const struct lu_env *env,
 
        list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
        list_xbuf.lb_len = list_xsize;
-       rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
-                           mdd_object_capa(env, mdd_sobj));
+       rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
        if (rc < 0)
                return rc;
        rc = 0;
@@ -3242,8 +3542,7 @@ static int mdd_migrate_xattrs(const struct lu_env *env,
        xname = list_xbuf.lb_buf;
        while (rem > 0) {
                xlen = strnlen(xname, rem - 1) + 1;
-               if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
-                   strcmp(XATTR_NAME_LMA, xname) == 0 ||
+               if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
                    strcmp(XATTR_NAME_LMV, xname) == 0)
                        goto next;
 
@@ -3252,9 +3551,7 @@ static int mdd_migrate_xattrs(const struct lu_env *env,
                    !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
                        goto next;
 
-               xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
-                                     xname,
-                                     mdd_object_capa(env, mdd_sobj));
+               xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
                if (xsize == -ENODATA)
                        goto next;
                if (xsize < 0)
@@ -3266,8 +3563,7 @@ static int mdd_migrate_xattrs(const struct lu_env *env,
 
                xbuf.lb_len = xsize;
                xbuf.lb_buf = info->mti_link_buf.lb_buf;
-               rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
-                                  mdd_object_capa(env, mdd_sobj));
+               rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
                if (rc == -ENODATA)
                        goto next;
                if (rc < 0)
@@ -3289,15 +3585,25 @@ static int mdd_migrate_xattrs(const struct lu_env *env,
                if (rc != 0)
                        GOTO(stop_trans, rc);
 
-               rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
-                                  mdd_object_capa(env, mdd_sobj));
+again:
+               rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
                if (rc == -EEXIST)
                        GOTO(stop_trans, rc = 0);
 
+               if (unlikely(rc == -ENOSPC &&
+                            strcmp(xname, XATTR_NAME_LINK) == 0)) {
+                       rc = linkea_overflow_shrink(
+                                       (struct linkea_data *)(xbuf.lb_buf));
+                       if (likely(rc > 0)) {
+                               xbuf.lb_len = rc;
+                               goto again;
+                       }
+               }
+
                if (rc != 0)
                        GOTO(stop_trans, rc);
 stop_trans:
-               mdd_trans_stop(env, mdd, rc, handle);
+               rc = mdd_trans_stop(env, mdd, rc, handle);
                if (rc != 0)
                        GOTO(out, rc);
 next:
@@ -3315,6 +3621,7 @@ static int mdd_declare_migrate_create(const struct lu_env *env,
                                      struct md_op_spec *spec,
                                      struct lu_attr *la,
                                      union lmv_mds_md *mgr_ea,
+                                     struct linkea_data *ldata,
                                      struct thandle *handle)
 {
        struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
@@ -3322,7 +3629,7 @@ static int mdd_declare_migrate_create(const struct lu_env *env,
        int                     rc;
        int                     mgr_easize;
 
-       rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
+       rc = mdd_declare_create_object_internal(env, mdd_pobj, mdd_tobj, la,
                                                handle, spec, NULL);
        if (rc != 0)
                return rc;
@@ -3342,6 +3649,10 @@ static int mdd_declare_migrate_create(const struct lu_env *env,
                                             buf, 0, handle);
                if (rc != 0)
                        return rc;
+       } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
+               rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
+               if (rc != 0)
+                       return rc;
        }
 
        if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
@@ -3362,7 +3673,6 @@ static int mdd_declare_migrate_create(const struct lu_env *env,
 
        la_flag->la_valid = LA_FLAGS;
        la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
-       mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
        rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
 
        return rc;
@@ -3372,19 +3682,21 @@ static int mdd_migrate_create(const struct lu_env *env,
                              struct mdd_object *mdd_pobj,
                              struct mdd_object *mdd_sobj,
                              struct mdd_object *mdd_tobj,
+                             const struct lu_name *lname,
                              struct lu_attr *la)
 {
        struct mdd_thread_info  *info = mdd_env_info(env);
        struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
        struct md_op_spec       *spec = &info->mti_spec;
-       struct lu_buf           lmm_buf = { 0 };
-       struct lu_buf           link_buf = { 0 };
-       const struct lu_buf     *buf;
+       struct lu_buf           lmm_buf = { NULL };
+       struct lu_buf           link_buf = { NULL };
+       struct lu_buf            mgr_buf;
        struct thandle          *handle;
        struct lmv_mds_md_v1    *mgr_ea;
        struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
        struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
        int                     mgr_easize;
+       struct linkea_data      *ldata = &mdd_env_info(env)->mti_link_data;
        int                     rc;
        ENTRY;
 
@@ -3393,11 +3705,14 @@ static int mdd_migrate_create(const struct lu_env *env,
        spec->sp_cr_lookup = 0;
        spec->sp_feat = &dt_directory_features;
        if (S_ISLNK(la->la_mode)) {
+               const struct lu_buf *buf;
+
                buf = lu_buf_check_and_alloc(
                                &mdd_env_info(env)->mti_big_buf,
                                la->la_size + 1);
                link_buf = *buf;
                link_buf.lb_len = la->la_size + 1;
+               memset(link_buf.lb_buf, 0, link_buf.lb_len);
                rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
                if (rc <= 0) {
                        rc = rc != 0 ? rc : -EFAULT;
@@ -3407,7 +3722,7 @@ static int mdd_migrate_create(const struct lu_env *env,
                        RETURN(rc);
                }
                spec->u.sp_symname = link_buf.lb_buf;
-       } else if S_ISREG(la->la_mode) {
+       } else if (S_ISREG(la->la_mode)) {
                /* retrieve lov of the old object */
                rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
                if (rc != 0 && rc != -ENODATA)
@@ -3417,9 +3732,22 @@ static int mdd_migrate_create(const struct lu_env *env,
                        spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
                        spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
                }
+       } else if (S_ISDIR(la->la_mode)) {
+               rc = mdd_links_read_with_rec(env, mdd_sobj, ldata);
+               if (rc == -ENODATA) {
+                       /* ignore the non-linkEA error */
+                       ldata = NULL;
+                       rc = 0;
+               }
+               if (rc < 0)
+                       RETURN(rc);
        }
 
-       mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
+       mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
+       lu_buf_check_and_alloc(&info->mti_xattr_buf, mgr_easize);
+       mgr_buf.lb_buf = info->mti_xattr_buf.lb_buf;
+       mgr_buf.lb_len = mgr_easize;
+       mgr_ea = mgr_buf.lb_buf;
        memset(mgr_ea, 0, sizeof(*mgr_ea));
        mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
        mgr_ea->lmv_stripe_count = cpu_to_le32(2);
@@ -3438,10 +3766,8 @@ static int mdd_migrate_create(const struct lu_env *env,
         * the last step of migration, so we set th_local = 1 to avoid
         * update last rcvd for this transaction */
        handle->th_local = 1;
-       rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
-                                       spec, la,
-                                       (union lmv_mds_md *)info->mti_xattr_buf,
-                                       handle);
+       rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj, spec,
+                                       la, mgr_buf.lb_buf, ldata, handle);
        if (rc != 0)
                GOTO(stop_trans, rc);
 
@@ -3449,19 +3775,25 @@ static int mdd_migrate_create(const struct lu_env *env,
        if (rc != 0)
                GOTO(stop_trans, rc);
 
+       /* don't set nlink from the original object */
+       la->la_valid &= ~LA_NLINK;
+
        /* create the target object */
-       rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
+       rc = mdd_create_object(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
                               hint, handle);
        if (rc != 0)
                GOTO(stop_trans, rc);
 
+       if (S_ISDIR(la->la_mode) && ldata != NULL) {
+               rc = mdd_links_write(env, mdd_tobj, ldata, handle);
+               if (rc != 0)
+                       GOTO(stop_trans, rc);
+       }
+
        /* Set MIGRATE EA on the source inode, so once the migration needs
         * to be re-done during failover, the re-do process can locate the
         * target object which is already being created. */
-       mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
-       buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
-       rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
-                          handle, mdd_object_capa(env, mdd_sobj));
+       rc = mdo_xattr_set(env, mdd_sobj, &mgr_buf, XATTR_NAME_LMV, 0, handle);
        if (rc != 0)
                GOTO(stop_trans, rc);
 
@@ -3472,12 +3804,10 @@ static int mdd_migrate_create(const struct lu_env *env,
         * flag and approve the migration */
        la_flag->la_valid = LA_FLAGS;
        la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
-       mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
-       rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
-                         mdd_object_capa(env, mdd_sobj));
+       rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
 stop_trans:
        if (handle != NULL)
-               mdd_trans_stop(env, mdd, rc, handle);
+               rc = mdd_trans_stop(env, mdd, rc, handle);
 out_free:
        if (lmm_buf.lb_buf != NULL)
                OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
@@ -3494,9 +3824,9 @@ static int mdd_migrate_entries(const struct lu_env *env,
        struct thandle          *handle;
        struct dt_it            *it;
        const struct dt_it_ops  *iops;
-       int                      rc;
        int                      result;
        struct lu_dirent        *ent;
+       int                      rc;
        ENTRY;
 
        OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
@@ -3509,8 +3839,7 @@ static int mdd_migrate_entries(const struct lu_env *env,
         * iterate directories
         */
        iops = &next->do_index_ops->dio_it;
-       it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
-                       mdd_object_capa(env, mdd_sobj));
+       it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
        if (IS_ERR(it))
                GOTO(out_ent, rc = PTR_ERR(it));
 
@@ -3530,7 +3859,6 @@ static int mdd_migrate_entries(const struct lu_env *env,
                struct mdd_object       *child;
                char                    *name = mdd_env_info(env)->mti_key;
                int                     len;
-               int                     recsize;
                int                     is_dir;
                bool                    target_exist = false;
 
@@ -3548,7 +3876,6 @@ static int mdd_migrate_entries(const struct lu_env *env,
                }
 
                fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
-               recsize = le16_to_cpu(ent->lde_reclen);
 
                /* Insert new fid with target name into target dir */
                if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
@@ -3560,6 +3887,7 @@ static int mdd_migrate_entries(const struct lu_env *env,
                if (IS_ERR(child))
                        GOTO(out, rc = PTR_ERR(child));
 
+               mdd_write_lock(env, child, MOR_SRC_CHILD);
                is_dir = S_ISDIR(mdd_object_type(child));
 
                snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
@@ -3569,15 +3897,14 @@ static int mdd_migrate_entries(const struct lu_env *env,
                        struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
 
                        rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
-                                      (struct dt_key *)name,
-                                      mdd_object_capa(env, mdd_tobj));
+                                      (struct dt_key *)name);
                        if (unlikely(rc == 0))
                                target_exist = true;
                }
 
                handle = mdd_trans_create(env, mdd);
                if (IS_ERR(handle))
-                       GOTO(out, rc = PTR_ERR(handle));
+                       GOTO(out_put, rc = PTR_ERR(handle));
 
                /* Note: this transaction is part of migration, and it is not
                 * the last step of migration, so we set th_local = 1 to avoid
@@ -3620,7 +3947,7 @@ static int mdd_migrate_entries(const struct lu_env *env,
                                GOTO(out_put, rc);
                }
 
-               rc = mdd_linkea_declare_update_child(env, mdd_tobj,
+               rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
                                                     child, name,
                                                     strlen(name),
                                                     handle);
@@ -3637,44 +3964,36 @@ static int mdd_migrate_entries(const struct lu_env *env,
                if (likely(!target_exist)) {
                        rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
                                                mdd_object_type(child),
-                                               name, handle,
-                                               mdd_object_capa(env, mdd_tobj));
+                                               name, handle);
                        if (rc != 0)
                                GOTO(out_put, rc);
-
-                       if (is_dir) {
-                               rc = mdo_ref_add(env, mdd_tobj, handle);
-                               if (rc != 0)
-                                       GOTO(out_put, rc);
-
-                       }
                }
 
-               rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
-                                       mdd_object_capa(env, mdd_sobj));
+               rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
                if (rc != 0)
                        GOTO(out_put, rc);
 
                if (is_dir) {
-                       rc = __mdd_index_delete_only(env, child, dotdot, handle,
-                                                  mdd_object_capa(env, child));
+                       rc = __mdd_index_delete_only(env, child, dotdot,
+                                                    handle);
                        if (rc != 0)
                                GOTO(out_put, rc);
 
                        rc = __mdd_index_insert_only(env, child,
                                         mdd_object_fid(mdd_tobj), S_IFDIR,
-                                        dotdot, handle,
-                                        mdd_object_capa(env, child));
+                                        dotdot, handle);
                        if (rc != 0)
                                GOTO(out_put, rc);
                }
 
-               rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
+               rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
+                                            child, name,
                                             strlen(name), handle);
 
 out_put:
+               mdd_write_unlock(env, child);
                mdd_object_put(env, child);
-               mdd_trans_stop(env, mdd, rc, handle);
+               rc = mdd_trans_stop(env, mdd, rc, handle);
                if (rc != 0)
                        GOTO(out, rc);
 next:
@@ -3697,22 +4016,24 @@ static int mdd_declare_update_linkea(const struct lu_env *env,
                                     struct mdd_object *mdd_pobj,
                                     struct mdd_object *mdd_sobj,
                                     struct mdd_object *mdd_tobj,
-                                    struct thandle *handle,
-                                    const struct lu_name *child_name)
+                                    const struct lu_name *child_name,
+                                    struct linkea_data *ldata,
+                                    struct thandle *handle)
 {
        return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
-                                         child_name, handle, 1);
+                                         child_name, ldata, handle, 1);
 }
 
 static int mdd_update_linkea(const struct lu_env *env,
                             struct mdd_object *mdd_pobj,
                             struct mdd_object *mdd_sobj,
                             struct mdd_object *mdd_tobj,
-                            struct thandle *handle,
-                            const struct lu_name *child_name)
+                            const struct lu_name *child_name,
+                            struct linkea_data *ldata,
+                            struct thandle *handle)
 {
        return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
-                                         child_name, handle, 0);
+                                         child_name, ldata, handle, 0);
 }
 
 static int mdd_declare_migrate_update_name(const struct lu_env *env,
@@ -3722,15 +4043,16 @@ static int mdd_declare_migrate_update_name(const struct lu_env *env,
                                           const struct lu_name *lname,
                                           struct lu_attr *la,
                                           struct lu_attr *parent_la,
+                                          struct linkea_data *ldata,
                                           struct thandle *handle)
 {
-       struct lu_attr  *la_flag = MDD_ENV_VAR(env, tattr);
-       int             rc;
+       struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
+       struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
+       int rc;
 
        /* Revert IMMUTABLE flag */
        la_flag->la_valid = LA_FLAGS;
        la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
-       mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
        rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
        if (rc != 0)
                return rc;
@@ -3740,16 +4062,25 @@ static int mdd_declare_migrate_update_name(const struct lu_env *env,
        if (rc != 0)
                return rc;
 
-       rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
-                                      mdd_tobj, handle, lname);
-       if (rc != 0)
-               return rc;
+       if (ldata->ld_buf != NULL) {
+               rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
+                                              mdd_tobj, lname, ldata, handle);
+               if (rc != 0)
+                       return rc;
+       }
 
        if (S_ISREG(mdd_object_type(mdd_sobj))) {
                rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
                                           handle);
                if (rc != 0)
                        return rc;
+
+               handle->th_complex = 1;
+               rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
+                                          XATTR_NAME_FID,
+                                          LU_XATTR_REPLACE, handle);
+               if (rc < 0)
+                       return rc;
        }
 
        if (S_ISDIR(mdd_object_type(mdd_sobj))) {
@@ -3765,6 +4096,10 @@ static int mdd_declare_migrate_update_name(const struct lu_env *env,
        if (rc != 0)
                return rc;
 
+       rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL);
+       if (rc != 0)
+               return rc;
+
        if (S_ISDIR(mdd_object_type(mdd_sobj))) {
                rc = mdo_declare_ref_add(env, mdd_pobj, handle);
                if (rc != 0)
@@ -3792,6 +4127,10 @@ static int mdd_declare_migrate_update_name(const struct lu_env *env,
                return rc;
 
        rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
+       if (rc != 0)
+               return rc;
+
+       rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
 
        return rc;
 }
@@ -3807,6 +4146,7 @@ static int mdd_migrate_update_name(const struct lu_env *env,
        struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
        struct lu_attr          *la_flag = MDD_ENV_VAR(env, tattr);
        struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
+       struct linkea_data      *ldata = &mdd_env_info(env)->mti_link_data;
        struct thandle          *handle;
        int                     is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
        const char              *name = lname->ln_name;
@@ -3818,16 +4158,22 @@ static int mdd_migrate_update_name(const struct lu_env *env,
        p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
        p_la->la_valid = LA_CTIME;
 
-       rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
+       rc = mdd_la_get(env, mdd_sobj, so_attr);
        if (rc != 0)
                RETURN(rc);
 
+       ldata->ld_buf = NULL;
+       rc = mdd_links_read(env, mdd_sobj, ldata);
+       if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
+               RETURN(rc);
+
        handle = mdd_trans_create(env, mdd);
        if (IS_ERR(handle))
                RETURN(PTR_ERR(handle));
 
        rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
-                                            lname, so_attr, p_la, handle);
+                                            lname, so_attr, p_la, ldata,
+                                            handle);
        if (rc != 0) {
                /* If the migration can not be fit in one transaction, just
                 * leave it in the original MDT */
@@ -3849,75 +4195,116 @@ static int mdd_migrate_update_name(const struct lu_env *env,
        /* Revert IMMUTABLE flag */
        la_flag->la_valid = LA_FLAGS;
        la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
-       mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
-       rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
-                         mdd_object_capa(env, mdd_pobj));
+       rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
        if (rc != 0)
                GOTO(stop_trans, rc);
 
        /* Remove source name from source directory */
-       rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
-                               mdd_object_capa(env, mdd_pobj));
+       rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
        if (rc != 0)
                GOTO(stop_trans, rc);
 
-       rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
-                              handle, lname);
-       if (rc != 0)
-               GOTO(stop_trans, rc);
+       if (ldata->ld_buf != NULL) {
+               rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
+                                      lname, ldata, handle);
+               if (rc != 0)
+                       GOTO(stop_trans, rc);
+
+               /*  linkea update might decrease the source object
+                *  nlink, let's get the attr again after ref_del */
+               rc = mdd_la_get(env, mdd_sobj, so_attr);
+               if (rc != 0)
+                       GOTO(stop_trans, rc);
+       }
 
        if (S_ISREG(so_attr->la_mode)) {
                if (so_attr->la_nlink == 1) {
                        rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
-                                          handle,
-                                          mdd_object_capa(env, mdd_sobj));
+                                          handle);
                        if (rc != 0 && rc != -ENODATA)
                                GOTO(stop_trans, rc);
+
+                       rc = mdo_xattr_set(env, mdd_tobj, NULL,
+                                          XATTR_NAME_FID,
+                                          LU_XATTR_REPLACE, handle);
+                       if (rc < 0)
+                               GOTO(stop_trans, rc);
                }
        }
 
        /* Insert new fid with target name into target dir */
        rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
-                               mdd_object_type(mdd_tobj), name,
-                               handle, mdd_object_capa(env, mdd_pobj));
+                               mdd_object_type(mdd_tobj), name, handle);
        if (rc != 0)
                GOTO(stop_trans, rc);
 
-       rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
-                          NULL, 1);
+       mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
+
+       mdd_sobj->mod_flags |= DEAD_OBJ;
+       rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
        if (rc != 0)
-               GOTO(stop_trans, rc);
+               GOTO(out_unlock, rc);
+
+       rc = __mdd_orphan_add(env, mdd_sobj, handle);
+       if (rc != 0)
+               GOTO(out_unlock, rc);
 
-       mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
        mdo_ref_del(env, mdd_sobj, handle);
        if (is_dir)
                mdo_ref_del(env, mdd_sobj, handle);
 
        /* Get the attr again after ref_del */
-       rc = mdd_la_get(env, mdd_sobj, so_attr,
-                       mdd_object_capa(env, mdd_sobj));
+       rc = mdd_la_get(env, mdd_sobj, so_attr);
        if (rc != 0)
                GOTO(out_unlock, rc);
 
        ma->ma_attr = *so_attr;
        ma->ma_valid |= MA_INODE;
-       rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
-       if (rc != 0)
-               GOTO(out_unlock, rc);
 
        rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
        if (rc != 0)
                GOTO(out_unlock, rc);
 
+       rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
+                              mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
+                              mdo2fid(mdd_pobj), lname, lname, handle);
+       if (rc != 0) {
+               CWARN("%s: changelog for migrate %s "DFID
+                     "under "DFID" failed: rc = %d\n",
+                     mdd2obd_dev(mdd)->obd_name, lname->ln_name,
+                     PFID(mdd_object_fid(mdd_sobj)),
+                     PFID(mdd_object_fid(mdd_pobj)), rc);
+               /* Sigh, there are no easy way to migrate back the object, so
+                * let's reset the result to 0 for now XXX */
+               rc = 0;
+       }
 out_unlock:
        mdd_write_unlock(env, mdd_sobj);
 
 stop_trans:
-       mdd_trans_stop(env, mdd, rc, handle);
+       rc = mdd_trans_stop(env, mdd, rc, handle);
 
        RETURN(rc);
 }
 
+static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
+                         const struct lu_fid *fid, __u32 *mdt_index)
+{
+       struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
+       struct seq_server_site *ss;
+       int rc;
+
+       ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
+
+       range->lsr_flags = LU_SEQ_RANGE_MDT;
+       rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
+       if (rc != 0)
+               return rc;
+
+       *mdt_index = range->lsr_index;
+
+       return 0;
+}
 /**
  * Check whether we should migrate the file/dir
  * return val
@@ -3934,11 +4321,12 @@ static int mdd_migrate_sanity_check(const struct lu_env *env,
 {
        struct mdd_thread_info  *info = mdd_env_info(env);
        struct linkea_data      *ldata = &info->mti_link_data;
+       struct mdd_device       *mdd = mdo2mdd(&pobj->mod_obj);
        int                     mgr_easize;
        struct lu_buf           *mgr_buf;
        int                     count;
        int                     rc;
-
+       __u64 mdt_index;
        ENTRY;
 
        mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
@@ -3946,8 +4334,7 @@ static int mdd_migrate_sanity_check(const struct lu_env *env,
        if (mgr_buf->lb_buf == NULL)
                RETURN(-ENOMEM);
 
-       rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
-                          mdd_object_capa(env, sobj));
+       rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
        if (rc > 0) {
                union lmv_mds_md *lmm = mgr_buf->lb_buf;
 
@@ -3960,7 +4347,6 @@ static int mdd_migrate_sanity_check(const struct lu_env *env,
                        struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
 
                        sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
-                       sobj->mod_flags &= ~IMMUTE_OBJ;
                        CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
                               mdd2obd_dev(mdd)->obd_name,
                               PFID(mdd_object_fid(sobj)));
@@ -3982,46 +4368,46 @@ static int mdd_migrate_sanity_check(const struct lu_env *env,
        if (rc != 0) {
                /* For multiple links files, if there are no linkEA data at all,
                 * means the file might be created before linkEA is enabled, and
-                * all all of its links should not be migrated yet, otherwise
-                * it should have some linkEA there */
+                * all of its links should not be migrated yet, otherwise it
+                * should have some linkEA there */
                if (rc == -ENOENT || rc == -ENODATA)
                        RETURN(1);
                RETURN(rc);
        }
 
-       /* If it is mulitple links file, we need update the name entry for
-        * all parent */
+       mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
+       /* If there are still links locally, then the file will not be
+        * migrated. */
        LASSERT(ldata->ld_leh != NULL);
+
+       /* If the linkEA is overflow, then means there are some unknown name
+        * entries under unknown parents, that will prevent the migration. */
+       if (unlikely(ldata->ld_leh->leh_overflow_time))
+               RETURN(1);
+
        ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
        for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
-               struct mdd_device       *mdd = mdo2mdd(&sobj->mod_obj);
-               struct mdd_object       *lpobj;
                struct lu_name          lname;
                struct lu_fid           fid;
+               __u32                   parent_mdt_index;
 
                linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
                                    &lname, &fid);
                ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
                                                         ldata->ld_reclen);
-               lpobj = mdd_object_find(env, mdd, &fid);
-               if (IS_ERR(lpobj)) {
-                       CWARN("%s: cannot find obj "DFID": rc = %ld\n",
-                             mdd2obd_dev(mdd)->obd_name, PFID(&fid),
-                             PTR_ERR(lpobj));
-                       continue;
-               }
 
-               if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
-                       CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
-                              PFID(&fid), lname.ln_namelen, lname.ln_name);
-                       mdd_object_put(env, lpobj);
+               rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
+               if (rc != 0)
+                       RETURN(rc);
+
+               /* Migrate the object only if none of its parents are on the
+                * current MDT. */
+               if (parent_mdt_index != mdt_index)
                        continue;
-               }
 
                CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
                       PFID(mdd_object_fid(sobj)), lname.ln_namelen,
                       lname.ln_name, PFID(&fid));
-               mdd_object_put(env, lpobj);
                rc = 1;
                break;
        }
@@ -4036,17 +4422,19 @@ static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
        struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
        struct mdd_device       *mdd = mdo2mdd(pobj);
        struct mdd_object       *mdd_sobj = md2mdd_obj(sobj);
-       struct mdd_object       *mdd_tobj = NULL;
+       struct mdd_object       *mdd_tobj = md2mdd_obj(tobj);
        struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
        struct lu_attr          *pattr = MDD_ENV_VAR(env, pattr);
+       bool                    created = false;
        int                     rc;
 
        ENTRY;
        /* If the file will being migrated, it will check whether
         * the file is being opened by someone else right now */
        mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
-       if (mdd_sobj->mod_count >= 1) {
-               CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
+       if (mdd_sobj->mod_count > 0) {
+               CDEBUG(D_OTHER,
+                      "%s: "DFID"%s is already opened count %d: rc = %d\n",
                       mdd2obd_dev(mdd)->obd_name,
                       PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
                       mdd_sobj->mod_count, -EBUSY);
@@ -4055,11 +4443,11 @@ static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
        }
        mdd_read_unlock(env, mdd_sobj);
 
-       rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
+       rc = mdd_la_get(env, mdd_sobj, so_attr);
        if (rc != 0)
                GOTO(put, rc);
 
-       rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
+       rc = mdd_la_get(env, mdd_pobj, pattr);
        if (rc != 0)
                GOTO(put, rc);
 
@@ -4092,14 +4480,15 @@ static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
 
        /* step 1: Check whether the orphan object has been created, and create
         * orphan object on the remote MDT if needed */
-       mdd_tobj = md2mdd_obj(tobj);
        if (!mdd_object_exists(mdd_tobj)) {
                rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
-                                       so_attr);
+                                       lname, so_attr);
                if (rc != 0)
                        GOTO(put, rc);
+               created = true;
        }
 
+       LASSERT(mdd_object_exists(mdd_tobj));
        /* step 2: migrate xattr */
        rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
        if (rc != 0)
@@ -4113,13 +4502,20 @@ static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
                if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
                                                  OBD_FAIL_MDS_REINT_NET_REP)))
                        GOTO(put, rc = 0);
+       } else {
+               OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
        }
 
+       LASSERT(mdd_object_exists(mdd_tobj));
        /* step 4: update name entry to the new object */
        rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
                                     ma);
        if (rc != 0)
                GOTO(put, rc);
+
+       /* newly created target was not locked, don't cache its attributes */
+       if (created)
+               mdd_invalidate(env, tobj);
 put:
        RETURN(rc);
 }