Whamcloud - gitweb
LU-4629 lmv: fix issue found by Klocwork Insight tool
[fs/lustre-release.git] / lustre / lmv / lmv_obd.c
index 2d24c6e..26ee849 100644 (file)
 #include <lustre_fid.h>
 #include "lmv_internal.h"
 
-int raw_name2idx(int hashtype, int count, const char *name, int namelen)
+/* This hash is only for testing purpose */
+static inline unsigned int
+lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
 {
-       unsigned int    c = 0;
-       int             idx;
+       unsigned int c = 0;
+       const unsigned char *p = (const unsigned char *)name;
 
-       LASSERT(namelen > 0);
+       while (--namelen >= 0)
+               c += p[namelen];
 
-       if (filename_is_volatile(name, namelen, &idx)) {
-               if (idx >= 0 && idx < count)
-                       return idx;
-               goto choose_hash;
-       }
+       c = c % count;
+
+       return c;
+}
+
+static inline unsigned int
+lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
+{
+       __u64   hash;
+
+       hash = lustre_hash_fnv_1a_64(name, namelen);
+
+       hash = hash % count;
+
+       return hash;
+}
+
+int lmv_name_to_stripe_index(enum lmv_hash_type hashtype,
+                            unsigned int max_mdt_index,
+                            const char *name, int namelen)
+{
+       int     idx;
 
-       if (count <= 1)
+       LASSERT(namelen > 0);
+       if (max_mdt_index <= 1)
                return 0;
 
-choose_hash:
        switch (hashtype) {
-       case MEA_MAGIC_LAST_CHAR:
-               c = mea_last_char_hash(count, name, namelen);
-               break;
-       case MEA_MAGIC_ALL_CHARS:
-               c = mea_all_chars_hash(count, name, namelen);
+       case LMV_HASH_TYPE_ALL_CHARS:
+               idx = lmv_hash_all_chars(max_mdt_index, name, namelen);
                break;
-       case MEA_MAGIC_HASH_SEGMENT:
-               CERROR("Unsupported hash type MEA_MAGIC_HASH_SEGMENT\n");
+       case LMV_HASH_TYPE_FNV_1A_64:
+               idx = lmv_hash_fnv1a(max_mdt_index, name, namelen);
                break;
+       /* LMV_HASH_TYPE_MIGRATION means the file is being migrated,
+        * and the file should be accessed by client, except for
+        * lookup(see lmv_intent_lookup), return -EACCES here */
+       case LMV_HASH_TYPE_MIGRATION:
+               CERROR("%.*s is being migrated: rc = %d\n", namelen,
+                      name, -EACCES);
+               return -EACCES;
        default:
                CERROR("Unknown hash type 0x%x\n", hashtype);
+               return -EINVAL;
        }
 
-       LASSERT(c < count);
-       return c;
+       CDEBUG(D_INFO, "name %.*s hash_type %d idx %d\n", namelen, name,
+              hashtype, idx);
+
+       LASSERT(idx < max_mdt_index);
+       return idx;
 }
 
 static void lmv_activate_target(struct lmv_obd *lmv,
@@ -1322,27 +1350,25 @@ static int lmv_placement_policy(struct obd_device *obd,
         * If stripe_offset is provided during setdirstripe
         * (setdirstripe -i xx), xx MDS will be choosen.
         */
-       if (op_data->op_cli_flags & CLI_SET_MEA) {
+       if (op_data->op_cli_flags & CLI_SET_MEA && op_data->op_data != NULL) {
                struct lmv_user_md *lum;
 
-               lum = (struct lmv_user_md *)op_data->op_data;
-               if (lum->lum_type == LMV_STRIPE_TYPE &&
-                   lum->lum_stripe_offset != -1) {
-                       if (lum->lum_stripe_offset >= lmv->desc.ld_tgt_count) {
-                               CERROR("%s: Stripe_offset %d > MDT count %d:"
-                                      " rc = %d\n", obd->obd_name,
-                                      lum->lum_stripe_offset,
-                                      lmv->desc.ld_tgt_count, -ERANGE);
-                               RETURN(-ERANGE);
-                       }
+               lum = op_data->op_data;
+
+               if (lum->lum_stripe_offset != (__u32)-1) {
                        *mds = lum->lum_stripe_offset;
-                       RETURN(0);
+               } else {
+                       /* -1 means default, which will be in the same MDT with
+                        * the stripe */
+                       *mds = op_data->op_mds;
+                       lum->lum_stripe_offset = op_data->op_mds;
                }
+       } else {
+               /* Allocate new fid on target according to operation type and
+                * parent home mds. */
+               *mds = op_data->op_mds;
        }
 
-       /* Allocate new fid on target according to operation type and parent
-        * home mds. */
-       *mds = op_data->op_mds;
        RETURN(0);
 }
 
@@ -1504,7 +1530,7 @@ static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
 
                obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
 
-               if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
+               if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
                        GOTO(out, rc = -EINVAL);
                if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
                        GOTO(out, rc = -EINVAL);
@@ -1751,19 +1777,52 @@ static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
         RETURN(rc);
 }
 
+/**
+ * Choosing the MDT by name or FID in @op_data.
+ * For non-striped directory, it will locate MDT by fid.
+ * For striped-directory, it will locate MDT by name. And also
+ * it will reset op_fid1 with the FID of the choosen stripe.
+ **/
+struct lmv_tgt_desc *
+lmv_locate_target_for_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
+                          const char *name, int namelen, struct lu_fid *fid,
+                          mdsno_t *mds)
+{
+       struct lmv_tgt_desc     *tgt;
+       const struct lmv_oinfo  *oinfo;
+
+       oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
+       if (IS_ERR(oinfo))
+               RETURN((void *)oinfo);
+       *fid = oinfo->lmo_fid;
+       *mds = oinfo->lmo_mds;
+       tgt = lmv_get_target(lmv, *mds);
+
+       CDEBUG(D_INFO, "locate on mds %u "DFID"\n", *mds, PFID(fid));
+       return tgt;
+}
+
 struct lmv_tgt_desc
 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
                struct lu_fid *fid)
 {
-       struct lmv_tgt_desc *tgt;
+       struct lmv_stripe_md    *lsm = op_data->op_mea1;
+       struct lmv_tgt_desc     *tgt;
 
-       tgt = lmv_find_target(lmv, fid);
-       if (IS_ERR(tgt))
-               return tgt;
+       if (lsm == NULL || lsm->lsm_md_stripe_count <= 1 ||
+           op_data->op_namelen == 0 ||
+           lsm->lsm_md_magic == LMV_MAGIC_MIGRATE) {
+               tgt = lmv_find_target(lmv, fid);
+               if (IS_ERR(tgt))
+                       return tgt;
 
-       op_data->op_mds = tgt->ltd_idx;
+               op_data->op_mds = tgt->ltd_idx;
+               return tgt;
+       }
 
-       return tgt;
+       return lmv_locate_target_for_name(lmv, lsm, op_data->op_name,
+                                         op_data->op_namelen, fid,
+                                         &op_data->op_mds);
 }
 
 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
@@ -1788,18 +1847,28 @@ int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
        if (IS_ERR(tgt))
                RETURN(PTR_ERR(tgt));
 
+       CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
+              op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
+              op_data->op_mds);
+
        rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
        if (rc)
                RETURN(rc);
 
-       CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
-              op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
-              op_data->op_mds);
+       /* Send the create request to the MDT where the object
+        * will be located */
+       tgt = lmv_find_target(lmv, &op_data->op_fid2);
+       if (IS_ERR(tgt))
+               RETURN(PTR_ERR(tgt));
+
+       op_data->op_mds = tgt->ltd_idx;
+
+       CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
+              PFID(&op_data->op_fid2), op_data->op_mds);
 
        op_data->op_flags |= MF_MDC_CANCEL_FID1;
        rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
                       cap_effective, rdev, request);
-
        if (rc == 0) {
                if (*request == NULL)
                        RETURN(rc);
@@ -1987,23 +2056,25 @@ lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
          NULL)
 
-static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
-                            int op_tgt, ldlm_mode_t mode, int bits, int flag)
+static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
+                           struct md_op_data *op_data,
+                           int op_tgt, ldlm_mode_t mode, int bits, int flag)
 {
-        struct lu_fid          *fid = md_op_data_fid(op_data, flag);
-        struct obd_device      *obd = exp->exp_obd;
-        struct lmv_obd         *lmv = &obd->u.lmv;
-        struct lmv_tgt_desc    *tgt;
-        ldlm_policy_data_t      policy = {{0}};
-        int                     rc = 0;
-        ENTRY;
+       struct lu_fid          *fid = md_op_data_fid(op_data, flag);
+       struct obd_device      *obd = exp->exp_obd;
+       struct lmv_obd         *lmv = &obd->u.lmv;
+       ldlm_policy_data_t      policy = {{ 0 }};
+       int                     rc = 0;
+       ENTRY;
 
-        if (!fid_is_sane(fid))
-                RETURN(0);
+       if (!fid_is_sane(fid))
+               RETURN(0);
 
-       tgt = lmv_find_target(lmv, fid);
-       if (IS_ERR(tgt))
-               RETURN(PTR_ERR(tgt));
+       if (tgt == NULL) {
+               tgt = lmv_find_target(lmv, fid);
+               if (IS_ERR(tgt))
+                       RETURN(PTR_ERR(tgt));
+       }
 
        if (tgt->ltd_idx != op_tgt) {
                CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
@@ -2047,6 +2118,18 @@ static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
        op_data->op_fsuid = current_fsuid();
        op_data->op_fsgid = current_fsgid();
        op_data->op_cap = cfs_curproc_cap_pack();
+       if (op_data->op_mea2 != NULL) {
+               struct lmv_stripe_md    *lsm = op_data->op_mea2;
+               const struct lmv_oinfo  *oinfo;
+
+               oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
+                                               op_data->op_namelen);
+               if (IS_ERR(oinfo))
+                       RETURN(PTR_ERR(oinfo));
+
+               op_data->op_fid2 = oinfo->lmo_fid;
+       }
+
        tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
        if (IS_ERR(tgt))
                RETURN(PTR_ERR(tgt));
@@ -2055,7 +2138,7 @@ static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
         * Cancel UPDATE lock on child (fid1).
         */
        op_data->op_flags |= MF_MDC_CANCEL_FID2;
-       rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
+       rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
                              MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
        if (rc != 0)
                RETURN(rc);
@@ -2069,33 +2152,66 @@ static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
                       const char *old, int oldlen, const char *new, int newlen,
                       struct ptlrpc_request **request)
 {
-        struct obd_device       *obd = exp->exp_obd;
-        struct lmv_obd          *lmv = &obd->u.lmv;
-        struct lmv_tgt_desc     *src_tgt;
-       struct lmv_tgt_desc     *tgt_tgt;
+       struct obd_device       *obd = exp->exp_obd;
+       struct lmv_obd          *lmv = &obd->u.lmv;
+       struct lmv_tgt_desc     *src_tgt;
        int                     rc;
        ENTRY;
 
-        LASSERT(oldlen != 0);
+       LASSERT(oldlen != 0);
 
-        CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
-               oldlen, old, PFID(&op_data->op_fid1),
-               newlen, new, PFID(&op_data->op_fid2));
+       CDEBUG(D_INODE, "RENAME %.*s in "DFID":%d to %.*s in "DFID":%d\n",
+              oldlen, old, PFID(&op_data->op_fid1),
+              op_data->op_mea1 ? op_data->op_mea1->lsm_md_stripe_count : 0,
+              newlen, new, PFID(&op_data->op_fid2),
+              op_data->op_mea2 ? op_data->op_mea2->lsm_md_stripe_count : 0);
 
-        rc = lmv_check_connect(obd);
-        if (rc)
-                RETURN(rc);
+       rc = lmv_check_connect(obd);
+       if (rc)
+               RETURN(rc);
 
        op_data->op_fsuid = current_fsuid();
        op_data->op_fsgid = current_fsgid();
        op_data->op_cap = cfs_curproc_cap_pack();
-       src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
+       if (op_data->op_cli_flags & CLI_MIGRATE) {
+               LASSERTF(fid_is_sane(&op_data->op_fid3), "invalid FID "DFID"\n",
+                        PFID(&op_data->op_fid3));
+               rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
+               if (rc)
+                       RETURN(rc);
+               src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid3);
+       } else {
+               if (op_data->op_mea1 != NULL) {
+                       struct lmv_stripe_md    *lsm = op_data->op_mea1;
+
+                       src_tgt = lmv_locate_target_for_name(lmv, lsm, old,
+                                                            oldlen,
+                                                            &op_data->op_fid1,
+                                                            &op_data->op_mds);
+                       if (IS_ERR(src_tgt))
+                               RETURN(PTR_ERR(src_tgt));
+               } else {
+                       src_tgt = lmv_find_target(lmv, &op_data->op_fid1);
+                       if (IS_ERR(src_tgt))
+                               RETURN(PTR_ERR(src_tgt));
+
+                       op_data->op_mds = src_tgt->ltd_idx;
+               }
+
+               if (op_data->op_mea2) {
+                       struct lmv_stripe_md    *lsm = op_data->op_mea2;
+                       const struct lmv_oinfo  *oinfo;
+
+                       oinfo = lsm_name_to_stripe_info(lsm, new, newlen);
+                       if (IS_ERR(oinfo))
+                               RETURN(PTR_ERR(oinfo));
+
+                       op_data->op_fid2 = oinfo->lmo_fid;
+               }
+       }
        if (IS_ERR(src_tgt))
                RETURN(PTR_ERR(src_tgt));
 
-       tgt_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
-       if (IS_ERR(tgt_tgt))
-               RETURN(PTR_ERR(tgt_tgt));
        /*
         * LOOKUP lock on src child (fid3) should also be cancelled for
         * src_tgt in mdc_rename.
@@ -2106,30 +2222,50 @@ static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
         * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
         * own target.
         */
-       rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
+       rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
                              LCK_EX, MDS_INODELOCK_UPDATE,
                              MF_MDC_CANCEL_FID2);
 
+       if (rc != 0)
+               RETURN(rc);
        /*
-        * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
+        * Cancel LOOKUP locks on source child (fid3) for parent tgt_tgt.
         */
-       if (rc == 0) {
-               rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
+       if (fid_is_sane(&op_data->op_fid3)) {
+               struct lmv_tgt_desc *tgt;
+
+               tgt = lmv_find_target(lmv, &op_data->op_fid1);
+               if (IS_ERR(tgt))
+                       RETURN(PTR_ERR(tgt));
+
+               /* Cancel LOOKUP lock on its parent */
+               rc = lmv_early_cancel(exp, tgt, op_data, src_tgt->ltd_idx,
                                      LCK_EX, MDS_INODELOCK_LOOKUP,
-                                     MF_MDC_CANCEL_FID4);
+                                     MF_MDC_CANCEL_FID3);
+               if (rc != 0)
+                       RETURN(rc);
+
+               rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
+                                     LCK_EX, MDS_INODELOCK_FULL,
+                                     MF_MDC_CANCEL_FID3);
+               if (rc != 0)
+                       RETURN(rc);
        }
 
        /*
         * Cancel all the locks on tgt child (fid4).
         */
-       if (rc == 0)
-               rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
+       if (fid_is_sane(&op_data->op_fid4))
+               rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
                                      LCK_EX, MDS_INODELOCK_FULL,
                                      MF_MDC_CANCEL_FID4);
 
-       if (rc == 0)
-               rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
-                              new, newlen, request);
+       CDEBUG(D_INODE, DFID":m%d to "DFID"\n", PFID(&op_data->op_fid1),
+              op_data->op_mds, PFID(&op_data->op_fid2));
+
+       rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen, new, newlen,
+                      request);
+
        RETURN(rc);
 }
 
@@ -2288,7 +2424,8 @@ static void lmv_adjust_dirpages(struct page **pages, int ncfspgs, int nlupgs)
 
 #define NORMAL_MAX_STRIPES 4
 int lmv_read_entry(struct obd_export *exp, struct md_op_data *op_data,
-                  struct md_callback *cb_op, struct lu_dirent **ldp)
+                  struct md_callback *cb_op, struct lu_dirent **ldp,
+                  struct page **ppage)
 {
        struct obd_device       *obd = exp->exp_obd;
        struct lmv_obd          *lmv = &obd->u.lmv;
@@ -2298,6 +2435,7 @@ int lmv_read_entry(struct obd_export *exp, struct md_op_data *op_data,
        int                     stripe_count;
        __u64                   min_hash;
        int                     min_idx = 0;
+       struct page             *min_page = NULL;
        int                     i;
        int                     rc;
        ENTRY;
@@ -2323,6 +2461,7 @@ int lmv_read_entry(struct obd_export *exp, struct md_op_data *op_data,
        min_hash = MDS_DIR_END_OFF;
        for (i = 0; i < stripe_count; i++) {
                struct lmv_tgt_desc *tgt;
+               struct page *page = NULL;
 
                if (likely(lsm == NULL)) {
                        tgt = lmv_find_target(lmv, &op_data->op_fid1);
@@ -2338,12 +2477,16 @@ int lmv_read_entry(struct obd_export *exp, struct md_op_data *op_data,
                        op_data->op_stripe_offset = i;
                }
 
-               rc = md_read_entry(tgt->ltd_exp, op_data, cb_op, &ents[i]);
+               rc = md_read_entry(tgt->ltd_exp, op_data, cb_op, &ents[i],
+                                  &page);
                if (rc != 0)
                        GOTO(out, rc);
 
                if (ents[i] != NULL &&
                    le64_to_cpu(ents[i]->lde_hash) <= min_hash) {
+                       if (min_page != NULL)
+                               page_cache_release(min_page);
+                       min_page = page;
                        min_hash = le64_to_cpu(ents[i]->lde_hash);
                        min_idx = i;
                }
@@ -2357,6 +2500,13 @@ out:
        if (stripe_count > NORMAL_MAX_STRIPES && ents != NULL)
                OBD_FREE(ents, sizeof(ents[0]) * stripe_count);
 
+       if (rc != 0 && min_page != NULL) {
+               kunmap(min_page);
+               page_cache_release(min_page);
+       } else {
+               *ppage = min_page;
+       }
+
        RETURN(rc);
 }
 
@@ -2366,6 +2516,7 @@ static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
        struct obd_device       *obd = exp->exp_obd;
        struct lmv_obd          *lmv = &obd->u.lmv;
        struct lmv_tgt_desc     *tgt = NULL;
+       struct lmv_tgt_desc     *parent_tgt = NULL;
        struct mdt_body         *body;
        int                     rc;
        ENTRY;
@@ -2375,12 +2526,32 @@ static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
                RETURN(rc);
 retry:
        /* Send unlink requests to the MDT where the child is located */
-       if (likely(!fid_is_zero(&op_data->op_fid2)))
-               tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
-       else
+       if (likely(!fid_is_zero(&op_data->op_fid2))) {
+               tgt = lmv_find_target(lmv, &op_data->op_fid2);
+               if (IS_ERR(tgt))
+                       RETURN(PTR_ERR(tgt));
+
+               /* For striped dir, we need to locate the parent as well */
+               if (op_data->op_mea1 != NULL &&
+                   op_data->op_mea1->lsm_md_stripe_count > 1) {
+                       struct lmv_tgt_desc *tmp;
+
+                       LASSERT(op_data->op_name != NULL &&
+                               op_data->op_namelen != 0);
+                       tmp = lmv_locate_target_for_name(lmv,
+                                                  op_data->op_mea1,
+                                                  op_data->op_name,
+                                                  op_data->op_namelen,
+                                                  &op_data->op_fid1,
+                                                  &op_data->op_mds);
+                       if (IS_ERR(tmp))
+                               RETURN(PTR_ERR(tmp));
+               }
+       } else {
                tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
-       if (IS_ERR(tgt))
-               RETURN(PTR_ERR(tgt));
+               if (IS_ERR(tgt))
+                       RETURN(PTR_ERR(tgt));
+       }
 
        op_data->op_fsuid = current_fsuid();
        op_data->op_fsgid = current_fsgid();
@@ -2398,9 +2569,18 @@ retry:
        /*
         * Cancel FULL locks on child (fid3).
         */
-       rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
-                             MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
+       parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
+       if (IS_ERR(parent_tgt))
+               RETURN(PTR_ERR(parent_tgt));
 
+       if (parent_tgt != tgt) {
+               rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
+                                     LCK_EX, MDS_INODELOCK_LOOKUP,
+                                     MF_MDC_CANCEL_FID3);
+       }
+
+       rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
+                             MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
        if (rc != 0)
                RETURN(rc);
 
@@ -2733,12 +2913,25 @@ int lmv_unpack_md(struct obd_export *exp, struct lmv_stripe_md **lsmp,
        }
 
        /* Unpack memmd */
-       if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1) {
-               CERROR("%s: invalid magic %x.\n", exp->exp_obd->obd_name,
-                      le32_to_cpu(lmm->lmv_magic));
-               RETURN(-EINVAL);
+       if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
+           le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_MIGRATE &&
+           le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
+               CERROR("%s: invalid lmv magic %x: rc = %d\n",
+                      exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
+                      -EIO);
+               RETURN(-EIO);
        }
 
+       if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1 ||
+           le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_MIGRATE)
+               lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
+       else
+               /**
+                * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
+                * stripecount should be 0 then.
+                */
+               lsm_size = lmv_stripe_md_size(0);
+
        lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
        if (lsm == NULL) {
                OBD_ALLOC(lsm, lsm_size);
@@ -2750,6 +2943,7 @@ int lmv_unpack_md(struct obd_export *exp, struct lmv_stripe_md **lsmp,
 
        switch (le32_to_cpu(lmm->lmv_magic)) {
        case LMV_MAGIC_V1:
+       case LMV_MAGIC_MIGRATE:
                rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
                break;
        default:
@@ -2901,8 +3095,10 @@ int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
        struct lmv_tgt_desc     *tgt = lmv->tgts[0];
        ENTRY;
 
-       if (md->lmv != NULL)
+       if (md->lmv != NULL) {
                lmv_free_memmd(md->lmv);
+               md->lmv = NULL;
+       }
        if (tgt == NULL || tgt->ltd_exp == NULL)
                RETURN(-EINVAL);
        RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
@@ -3009,7 +3205,7 @@ int lmv_intent_getattr_async(struct obd_export *exp,
        if (rc)
                RETURN(rc);
 
-       tgt = lmv_find_target(lmv, &op_data->op_fid1);
+       tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
        if (IS_ERR(tgt))
                RETURN(PTR_ERR(tgt));
 
@@ -3117,6 +3313,51 @@ int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
         RETURN(rc);
 }
 
+int lmv_update_lsm_md(struct obd_export *exp, struct lmv_stripe_md *lsm,
+                     struct mdt_body *body, ldlm_blocking_callback cb_blocking)
+{
+       if (lsm->lsm_md_stripe_count <= 1)
+               return 0;
+
+       return lmv_revalidate_slaves(exp, body, lsm, cb_blocking, 0);
+}
+
+int lmv_merge_attr(struct obd_export *exp, const struct lmv_stripe_md *lsm,
+                  struct cl_attr *attr)
+{
+#ifdef __KERNEL__
+       int i;
+
+       for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
+               struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
+
+               CDEBUG(D_INFO, ""DFID" size %llu, nlink %u, atime %lu ctime"
+                      "%lu, mtime %lu.\n", PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
+                      i_size_read(inode), inode->i_nlink,
+                      LTIME_S(inode->i_atime), LTIME_S(inode->i_ctime),
+                      LTIME_S(inode->i_mtime));
+
+               /* for slave stripe, it needs to subtract nlink for . and .. */
+               if (i != 0)
+                       attr->cat_nlink += inode->i_nlink - 2;
+               else
+                       attr->cat_nlink = inode->i_nlink;
+
+               attr->cat_size += i_size_read(inode);
+
+               if (attr->cat_atime < LTIME_S(inode->i_atime))
+                       attr->cat_atime = LTIME_S(inode->i_atime);
+
+               if (attr->cat_ctime < LTIME_S(inode->i_ctime))
+                       attr->cat_ctime = LTIME_S(inode->i_ctime);
+
+               if (attr->cat_mtime < LTIME_S(inode->i_mtime))
+                       attr->cat_mtime = LTIME_S(inode->i_mtime);
+       }
+#endif
+       return 0;
+}
+
 struct obd_ops lmv_obd_ops = {
         .o_owner                = THIS_MODULE,
         .o_setup                = lmv_setup,
@@ -3160,8 +3401,10 @@ struct md_ops lmv_md_ops = {
         .m_cancel_unused        = lmv_cancel_unused,
         .m_set_lock_data        = lmv_set_lock_data,
         .m_lock_match           = lmv_lock_match,
-        .m_get_lustre_md        = lmv_get_lustre_md,
-        .m_free_lustre_md       = lmv_free_lustre_md,
+       .m_get_lustre_md        = lmv_get_lustre_md,
+       .m_free_lustre_md       = lmv_free_lustre_md,
+       .m_update_lsm_md        = lmv_update_lsm_md,
+       .m_merge_attr           = lmv_merge_attr,
         .m_set_open_replay_data = lmv_set_open_replay_data,
         .m_clear_open_replay_data = lmv_clear_open_replay_data,
         .m_renew_capa           = lmv_renew_capa,