Whamcloud - gitweb
LU-14762 lmv: compare space to mkdir on parent MDT
[fs/lustre-release.git] / lustre / lmv / lmv_obd.c
index 1484f67..ba1b656 100644 (file)
@@ -27,7 +27,6 @@
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  */
 
 #define DEBUG_SUBSYSTEM S_LMV
@@ -129,12 +128,12 @@ static int lmv_set_mdc_active(struct lmv_obd *lmv,
        return rc;
 }
 
-struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
+static struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
 {
        struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
        struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
 
-       return (tgt == NULL) ? NULL : obd_get_uuid(tgt->ltd_exp);
+       return tgt ? obd_get_uuid(tgt->ltd_exp) : NULL;
 }
 
 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
@@ -275,14 +274,14 @@ static int lmv_init_ea_size(struct obd_export *exp, __u32 easize,
 
 #define MAX_STRING_SIZE 128
 
-int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
+static int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
 {
-        struct lmv_obd          *lmv = &obd->u.lmv;
-        struct obd_device       *mdc_obd;
-        struct obd_export       *mdc_exp;
-        struct lu_fld_target     target;
-        int                      rc;
-        ENTRY;
+       struct lmv_obd *lmv = &obd->u.lmv;
+       struct obd_device *mdc_obd;
+       struct obd_export *mdc_exp;
+       struct lu_fld_target target;
+       int  rc;
+       ENTRY;
 
        mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
                                        &obd->obd_uuid);
@@ -302,10 +301,10 @@ int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
 
        rc = obd_connect(NULL, &mdc_exp, mdc_obd, &obd->obd_uuid,
                         &lmv->conn_data, lmv->lmv_cache);
-        if (rc) {
-                CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
-                RETURN(rc);
-        }
+       if (rc) {
+               CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
+               RETURN(rc);
+       }
 
        /*
         * Init fid sequence client for this mdc and add new fld target.
@@ -353,8 +352,8 @@ int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
        }
 
        CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
-               mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
-               atomic_read(&obd->obd_refcount));
+              mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
+              atomic_read(&obd->obd_refcount));
 
        lmv_statfs_check_update(obd, tgt);
 
@@ -489,20 +488,20 @@ out_disc:
 
 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
 {
-        struct lmv_obd         *lmv = &obd->u.lmv;
-        struct obd_device      *mdc_obd;
-        int                     rc;
-        ENTRY;
+       struct lmv_obd *lmv = &obd->u.lmv;
+       struct obd_device *mdc_obd;
+       int rc;
+       ENTRY;
 
-        LASSERT(tgt != NULL);
-        LASSERT(obd != NULL);
+       LASSERT(tgt != NULL);
+       LASSERT(obd != NULL);
 
-        mdc_obd = class_exp2obd(tgt->ltd_exp);
+       mdc_obd = class_exp2obd(tgt->ltd_exp);
 
-        if (mdc_obd) {
-                mdc_obd->obd_force = obd->obd_force;
-                mdc_obd->obd_fail = obd->obd_fail;
-                mdc_obd->obd_no_recov = obd->obd_no_recov;
+       if (mdc_obd) {
+               mdc_obd->obd_force = obd->obd_force;
+               mdc_obd->obd_fail = obd->obd_fail;
+               mdc_obd->obd_no_recov = obd->obd_no_recov;
 
                if (lmv->lmv_tgts_kobj)
                        sysfs_remove_link(lmv->lmv_tgts_kobj,
@@ -511,24 +510,24 @@ static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
 
        rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
        if (rc)
-               CERROR("Can't finanize fids factory\n");
-
-        CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
-               tgt->ltd_exp->exp_obd->obd_name,
-               tgt->ltd_exp->exp_obd->obd_uuid.uuid);
-
-        obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
-        rc = obd_disconnect(tgt->ltd_exp);
-        if (rc) {
-                if (tgt->ltd_active) {
-                        CERROR("Target %s disconnect error %d\n",
-                               tgt->ltd_uuid.uuid, rc);
-                }
-        }
+               CERROR("Can't finalize fids factory\n");
+
+       CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
+              tgt->ltd_exp->exp_obd->obd_name,
+              tgt->ltd_exp->exp_obd->obd_uuid.uuid);
+
+       obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
+       rc = obd_disconnect(tgt->ltd_exp);
+       if (rc) {
+               if (tgt->ltd_active) {
+                       CERROR("Target %s disconnect error %d\n",
+                              tgt->ltd_uuid.uuid, rc);
+               }
+       }
 
-        lmv_activate_target(lmv, tgt, 0);
-        tgt->ltd_exp = NULL;
-        RETURN(0);
+       lmv_activate_target(lmv, tgt, 0);
+       tgt->ltd_exp = NULL;
+       RETURN(0);
 }
 
 static int lmv_disconnect(struct obd_export *exp)
@@ -837,7 +836,10 @@ static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
                        RETURN(-ENODEV);
 
                tgt = lmv_tgt(lmv, index);
-               if (!tgt || !tgt->ltd_active)
+               if (!tgt)
+                       RETURN(-EAGAIN);
+
+               if (!tgt->ltd_active)
                        RETURN(-ENODATA);
 
                mdc_obd = class_exp2obd(tgt->ltd_exp);
@@ -1126,7 +1128,7 @@ static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
         * can distribute subdirs evenly from the beginning.
         */
        while (LNetGetId(i++, &lnet_id) != -ENOENT) {
-               if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND) {
+               if (lnet_id.nid != LNET_NID_LO_0) {
                        lmv->lmv_qos_rr_index = (u32)lnet_id.nid;
                        break;
                }
@@ -1214,7 +1216,7 @@ static int lmv_select_statfs_mdt(struct lmv_obd *lmv, __u32 flags)
                if (LNetGetId(i, &lnet_id) == -ENOENT)
                        break;
 
-               if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND) {
+               if (lnet_id.nid != LNET_NID_LO_0) {
                        /* We dont need a full 64-bit modulus, just enough
                         * to distribute the requests across MDTs evenly.
                         */
@@ -1253,14 +1255,15 @@ static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
                if (!tgt || !tgt->ltd_exp)
                        continue;
 
-               rc = obd_statfs(env, tgt->ltd_exp, temp, max_age, flags);
+               rc = obd_statfs(env, tgt->ltd_exp, temp, max_age,
+                               flags | OBD_STATFS_NESTED);
                if (rc) {
                        CERROR("%s: can't stat MDS #%d: rc = %d\n",
                               tgt->ltd_exp->exp_obd->obd_name, i, rc);
                        GOTO(out_free_temp, rc);
                }
 
-               if (temp->os_state & OS_STATE_SUM ||
+               if (temp->os_state & OS_STATFS_SUM ||
                    flags == OBD_STATFS_FOR_MDT0) {
                        /* reset to the last aggregated values
                         * and don't sum with non-aggrated data */
@@ -1307,7 +1310,7 @@ static int lmv_statfs_update(void *cookie, int rc)
                tgt->ltd_statfs = *osfs;
                tgt->ltd_statfs_age = ktime_get_seconds();
                spin_unlock(&lmv->lmv_lock);
-               lmv->lmv_qos.lq_dirty = 1;
+               set_bit(LQ_DIRTY, &lmv->lmv_qos.lq_flags);
        }
 
        return rc;
@@ -1458,9 +1461,11 @@ static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
 
 static struct lu_tgt_desc *lmv_locate_tgt_qos(struct lmv_obd *lmv, __u32 *mdt)
 {
-       struct lu_tgt_desc *tgt;
+       struct lu_tgt_desc *tgt, *cur = NULL;
+       __u64 total_avail = 0;
        __u64 total_weight = 0;
        __u64 cur_weight = 0;
+       int total_usable = 0;
        __u64 rand;
        int rc;
 
@@ -1479,13 +1484,29 @@ static struct lu_tgt_desc *lmv_locate_tgt_qos(struct lmv_obd *lmv, __u32 *mdt)
                GOTO(unlock, tgt = ERR_PTR(rc));
 
        lmv_foreach_tgt(lmv, tgt) {
-               tgt->ltd_qos.ltq_usable = 0;
-               if (!tgt->ltd_exp || !tgt->ltd_active)
+               if (!tgt->ltd_exp || !tgt->ltd_active) {
+                       tgt->ltd_qos.ltq_usable = 0;
                        continue;
+               }
 
                tgt->ltd_qos.ltq_usable = 1;
                lu_tgt_qos_weight_calc(tgt);
+               if (tgt->ltd_index == *mdt)
+                       cur = tgt;
+               total_avail += tgt->ltd_qos.ltq_avail;
                total_weight += tgt->ltd_qos.ltq_weight;
+               total_usable++;
+       }
+
+       /* if current MDT has above-average space, within range of the QOS
+        * threshold, stay on the same MDT to avoid creating needless remote
+        * MDT directories.
+        */
+       rand = total_avail * (256 - lmv->lmv_qos.lq_threshold_rr) /
+               (total_usable * 256);
+       if (cur && cur->ltd_qos.ltq_avail >= rand) {
+               tgt = cur;
+               GOTO(unlock, rc = 0);
        }
 
        rand = lu_prandom_u64_max(total_weight);
@@ -1519,7 +1540,7 @@ static struct lu_tgt_desc *lmv_locate_tgt_rr(struct lmv_obd *lmv, __u32 *mdt)
 
        ENTRY;
 
-       spin_lock(&lmv->lmv_qos.lq_rr.lqr_alloc);
+       spin_lock(&lmv->lmv_lock);
        for (i = 0; i < lmv->lmv_mdt_descs.ltd_tgts_size; i++) {
                index = (i + lmv->lmv_qos_rr_index) %
                        lmv->lmv_mdt_descs.ltd_tgts_size;
@@ -1530,19 +1551,22 @@ static struct lu_tgt_desc *lmv_locate_tgt_rr(struct lmv_obd *lmv, __u32 *mdt)
                *mdt = tgt->ltd_index;
                lmv->lmv_qos_rr_index = (*mdt + 1) %
                                        lmv->lmv_mdt_descs.ltd_tgts_size;
-               spin_unlock(&lmv->lmv_qos.lq_rr.lqr_alloc);
+               spin_unlock(&lmv->lmv_lock);
 
                RETURN(tgt);
        }
-       spin_unlock(&lmv->lmv_qos.lq_rr.lqr_alloc);
+       spin_unlock(&lmv->lmv_lock);
 
        RETURN(ERR_PTR(-ENODEV));
 }
 
+/* locate MDT by file name, for striped directory, the file name hash decides
+ * which stripe its dirent is stored.
+ */
 static struct lmv_tgt_desc *
 lmv_locate_tgt_by_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
                       const char *name, int namelen, struct lu_fid *fid,
-                      __u32 *mds, bool post_migrate)
+                      __u32 *mds, bool new_layout)
 {
        struct lmv_tgt_desc *tgt;
        const struct lmv_oinfo *oinfo;
@@ -1561,12 +1585,15 @@ lmv_locate_tgt_by_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
                        return ERR_PTR(-EBADF);
                oinfo = &lsm->lsm_md_oinfo[cfs_fail_val];
        } else {
-               oinfo = lsm_name_to_stripe_info(lsm, name, namelen,
-                                               post_migrate);
+               oinfo = lsm_name_to_stripe_info(lsm, name, namelen, new_layout);
                if (IS_ERR(oinfo))
                        return ERR_CAST(oinfo);
        }
 
+       /* check stripe FID is sane */
+       if (!fid_is_sane(&oinfo->lmo_fid))
+               return ERR_PTR(-ENODEV);
+
        *fid = oinfo->lmo_fid;
        *mds = oinfo->lmo_mds;
        tgt = lmv_tgt(lmv, oinfo->lmo_mds);
@@ -1581,14 +1608,15 @@ lmv_locate_tgt_by_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
  *
  * For striped directory, it will locate the stripe by name hash, if hash_type
  * is unknown, it will return the stripe specified by 'op_data->op_stripe_index'
- * which is set outside, and if dir is migrating, 'op_data->op_post_migrate'
+ * which is set outside, and if dir is migrating, 'op_data->op_new_layout'
  * indicates whether old or new layout is used to locate.
  *
  * For plain direcotry, it just locate the MDT of op_data->op_fid1.
  *
- * \param[in] lmv      LMV device
- * \param[in] op_data  client MD stack parameters, name, namelen
- *                      mds_num etc.
+ * \param[in] lmv              LMV device
+ * \param[in/out] op_data      client MD stack parameters, name, namelen etc,
+ *                             op_mds and op_fid1 will be updated if op_mea1
+ *                             indicates fid1 represents a striped directory.
  *
  * retval              pointer to the lmv_tgt_desc if succeed.
  *                      ERR_PTR(errno) if failed.
@@ -1640,7 +1668,7 @@ lmv_locate_tgt(struct lmv_obd *lmv, struct md_op_data *op_data)
                tgt = lmv_locate_tgt_by_name(lmv, op_data->op_mea1,
                                op_data->op_name, op_data->op_namelen,
                                &op_data->op_fid1, &op_data->op_mds,
-                               op_data->op_post_migrate);
+                               op_data->op_new_layout);
        }
 
        return tgt;
@@ -1654,7 +1682,7 @@ lmv_locate_tgt2(struct lmv_obd *lmv, struct md_op_data *op_data)
        int rc;
 
        LASSERT(op_data->op_name);
-       if (lmv_dir_migrating(op_data->op_mea2)) {
+       if (lmv_dir_layout_changing(op_data->op_mea2)) {
                struct lu_fid fid1 = op_data->op_fid1;
                struct lmv_stripe_md *lsm1 = op_data->op_mea1;
                struct ptlrpc_request *request = NULL;
@@ -1688,13 +1716,14 @@ lmv_locate_tgt2(struct lmv_obd *lmv, struct md_op_data *op_data)
                                &op_data->op_mds, true);
 }
 
-int lmv_migrate_existence_check(struct lmv_obd *lmv, struct md_op_data *op_data)
+int lmv_old_layout_lookup(struct lmv_obd *lmv, struct md_op_data *op_data)
 {
        struct lu_tgt_desc *tgt;
        struct ptlrpc_request *request;
        int rc;
 
-       LASSERT(lmv_dir_migrating(op_data->op_mea1));
+       LASSERT(lmv_dir_layout_changing(op_data->op_mea1));
+       LASSERT(!op_data->op_new_layout);
 
        tgt = lmv_locate_tgt(lmv, op_data);
        if (IS_ERR(tgt))
@@ -1709,6 +1738,22 @@ int lmv_migrate_existence_check(struct lmv_obd *lmv, struct md_op_data *op_data)
        return rc;
 }
 
+static inline bool lmv_op_user_qos_mkdir(const struct md_op_data *op_data)
+{
+       const struct lmv_user_md *lum = op_data->op_data;
+
+       return (op_data->op_cli_flags & CLI_SET_MEA) && lum &&
+              le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC &&
+              le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
+}
+
+static inline bool lmv_op_default_qos_mkdir(const struct md_op_data *op_data)
+{
+       const struct lmv_stripe_md *lsm = op_data->op_default_mea1;
+
+       return lsm && lsm->lsm_md_master_mdt_index == LMV_OFFSET_DEFAULT;
+}
+
 /* mkdir by QoS in two cases:
  * 1. 'lfs mkdir -i -1'
  * 2. parent default LMV master_mdt_index is -1
@@ -1718,27 +1763,38 @@ int lmv_migrate_existence_check(struct lmv_obd *lmv, struct md_op_data *op_data)
  */
 static inline bool lmv_op_qos_mkdir(const struct md_op_data *op_data)
 {
-       const struct lmv_stripe_md *lsm = op_data->op_default_mea1;
-       const struct lmv_user_md *lum = op_data->op_data;
-
        if (op_data->op_code != LUSTRE_OPC_MKDIR)
                return false;
 
        if (lmv_dir_striped(op_data->op_mea1))
                return false;
 
-       if (op_data->op_cli_flags & CLI_SET_MEA && lum &&
-           (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
-            le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) &&
-           le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT)
+       if (lmv_op_user_qos_mkdir(op_data))
                return true;
 
-       if (lsm && lsm->lsm_md_master_mdt_index == LMV_OFFSET_DEFAULT)
+       if (lmv_op_default_qos_mkdir(op_data))
                return true;
 
        return false;
 }
 
+/* if default LMV is set, and its index is LMV_OFFSET_DEFAULT, and
+ * 1. max_inherit_rr is set and is not LMV_INHERIT_RR_NONE
+ * 2. or parent is ROOT
+ * mkdir roundrobin.
+ * NB, this also needs to check server is balanced, which is checked by caller.
+ */
+static inline bool lmv_op_default_rr_mkdir(const struct md_op_data *op_data)
+{
+       const struct lmv_stripe_md *lsm = op_data->op_default_mea1;
+
+       if (!lmv_op_default_qos_mkdir(op_data))
+               return false;
+
+       return lsm->lsm_md_max_inherit_rr != LMV_INHERIT_RR_NONE ||
+              fid_is_root(&op_data->op_fid1);
+}
+
 /* 'lfs mkdir -i <specific_MDT>' */
 static inline bool lmv_op_user_specific_mkdir(const struct md_op_data *op_data)
 {
@@ -1760,6 +1816,7 @@ lmv_op_default_specific_mkdir(const struct md_op_data *op_data)
               op_data->op_default_mea1->lsm_md_master_mdt_index !=
                        LMV_OFFSET_DEFAULT;
 }
+
 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
                const void *data, size_t datalen, umode_t mode, uid_t uid,
                gid_t gid, cfs_cap_t cap_effective, __u64 rdev,
@@ -1768,6 +1825,7 @@ int lmv_create(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;
+       struct mdt_body *repbody;
        int rc;
 
        ENTRY;
@@ -1778,35 +1836,23 @@ int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
        if (lmv_dir_bad_hash(op_data->op_mea1))
                RETURN(-EBADF);
 
-       if (lmv_dir_migrating(op_data->op_mea1)) {
+       if (lmv_dir_layout_changing(op_data->op_mea1)) {
                /*
                 * if parent is migrating, create() needs to lookup existing
                 * name in both old and new layout, check old layout on client.
                 */
-               rc = lmv_migrate_existence_check(lmv, op_data);
+               rc = lmv_old_layout_lookup(lmv, op_data);
                if (rc != -ENOENT)
                        RETURN(rc);
 
-               op_data->op_post_migrate = true;
+               op_data->op_new_layout = true;
        }
 
        tgt = lmv_locate_tgt(lmv, op_data);
        if (IS_ERR(tgt))
                RETURN(PTR_ERR(tgt));
 
-       if (lmv_op_qos_mkdir(op_data)) {
-               tgt = lmv_locate_tgt_qos(lmv, &op_data->op_mds);
-               if (tgt == ERR_PTR(-EAGAIN))
-                       tgt = lmv_locate_tgt_rr(lmv, &op_data->op_mds);
-               /*
-                * only update statfs after QoS mkdir, this means the cached
-                * statfs may be stale, and current mkdir may not follow QoS
-                * accurately, but it's not serious, and avoids periodic statfs
-                * when client doesn't mkdir by QoS.
-                */
-               if (!IS_ERR(tgt))
-                       lmv_statfs_check_update(obd, tgt);
-       } else if (lmv_op_user_specific_mkdir(op_data)) {
+       if (lmv_op_user_specific_mkdir(op_data)) {
                struct lmv_user_md *lum = op_data->op_data;
 
                op_data->op_mds = le32_to_cpu(lum->lum_stripe_offset);
@@ -1819,11 +1865,34 @@ int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
                tgt = lmv_tgt(lmv, op_data->op_mds);
                if (!tgt)
                        RETURN(-ENODEV);
-       }
+       } else if (lmv_op_qos_mkdir(op_data)) {
+               struct lmv_tgt_desc *tmp = tgt;
 
-       if (IS_ERR(tgt))
-               RETURN(PTR_ERR(tgt));
+               tgt = lmv_locate_tgt_qos(lmv, &op_data->op_mds);
+               if (tgt == ERR_PTR(-EAGAIN)) {
+                       if (ltd_qos_is_balanced(&lmv->lmv_mdt_descs) &&
+                           !lmv_op_default_rr_mkdir(op_data) &&
+                           !lmv_op_user_qos_mkdir(op_data))
+                               /* if it's not necessary, don't create remote
+                                * directory.
+                                */
+                               tgt = tmp;
+                       else
+                               tgt = lmv_locate_tgt_rr(lmv, &op_data->op_mds);
+               }
+               if (IS_ERR(tgt))
+                       RETURN(PTR_ERR(tgt));
+
+               /*
+                * only update statfs after QoS mkdir, this means the cached
+                * statfs may be stale, and current mkdir may not follow QoS
+                * accurately, but it's not serious, and avoids periodic statfs
+                * when client doesn't mkdir by QoS.
+                */
+               lmv_statfs_check_update(obd, tgt);
+       }
 
+retry:
        rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
        if (rc)
                RETURN(rc);
@@ -1841,7 +1910,30 @@ int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
                        RETURN(rc);
                CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
        }
-       RETURN(rc);
+
+       /* dir restripe needs to send to MDT where dir is located */
+       if (rc != -EREMOTE ||
+           !(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH))
+               RETURN(rc);
+
+       repbody = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
+       if (repbody == NULL)
+               RETURN(-EPROTO);
+
+       /* Not cross-ref case, just get out of here. */
+       if (likely(!(repbody->mbo_valid & OBD_MD_MDS)))
+               RETURN(rc);
+
+       op_data->op_fid2 = repbody->mbo_fid1;
+       ptlrpc_req_finished(*request);
+       *request = NULL;
+
+       tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
+       if (IS_ERR(tgt))
+               RETURN(PTR_ERR(tgt));
+
+       op_data->op_mds = tgt->ltd_index;
+       goto retry;
 }
 
 static int
@@ -2015,7 +2107,7 @@ static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
        struct lmv_tgt_desc *tp_tgt = NULL;
        struct lmv_tgt_desc *child_tgt;
        struct lmv_tgt_desc *tgt;
-       struct lu_fid target_fid;
+       struct lu_fid target_fid = { 0 };
        int rc;
 
        ENTRY;
@@ -2034,29 +2126,15 @@ static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
                RETURN(PTR_ERR(parent_tgt));
 
        if (lmv_dir_striped(lsm)) {
-               __u32 hash_type = lsm->lsm_md_hash_type;
-               __u32 stripe_count = lsm->lsm_md_stripe_count;
-
-               /*
-                * old stripes are appended after new stripes for migrating
-                * directory.
-                */
-               if (lmv_dir_migrating(lsm)) {
-                       hash_type = lsm->lsm_md_migrate_hash;
-                       stripe_count -= lsm->lsm_md_migrate_offset;
-               }
+               const struct lmv_oinfo *oinfo;
 
-               rc = lmv_name_to_stripe_index(hash_type, stripe_count, name,
-                                             namelen);
-               if (rc < 0)
-                       RETURN(rc);
-
-               if (lmv_dir_migrating(lsm))
-                       rc += lsm->lsm_md_migrate_offset;
+               oinfo = lsm_name_to_stripe_info(lsm, name, namelen, false);
+               if (IS_ERR(oinfo))
+                       RETURN(PTR_ERR(oinfo));
 
-               /* save it in fid4 temporarily for early cancel */
-               op_data->op_fid4 = lsm->lsm_md_oinfo[rc].lmo_fid;
-               sp_tgt = lmv_tgt(lmv, lsm->lsm_md_oinfo[rc].lmo_mds);
+               /* save source stripe FID in fid4 temporarily for ELC */
+               op_data->op_fid4 = oinfo->lmo_fid;
+               sp_tgt = lmv_tgt(lmv, oinfo->lmo_mds);
                if (!sp_tgt)
                        RETURN(-ENODEV);
 
@@ -2064,18 +2142,14 @@ static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
                 * if parent is being migrated too, fill op_fid2 with target
                 * stripe fid, otherwise the target stripe is not created yet.
                 */
-               if (lmv_dir_migrating(lsm)) {
-                       hash_type = lsm->lsm_md_hash_type &
-                                   ~LMV_HASH_FLAG_MIGRATION;
-                       stripe_count = lsm->lsm_md_migrate_offset;
-
-                       rc = lmv_name_to_stripe_index(hash_type, stripe_count,
-                                                     name, namelen);
-                       if (rc < 0)
-                               RETURN(rc);
-
-                       op_data->op_fid2 = lsm->lsm_md_oinfo[rc].lmo_fid;
-                       tp_tgt = lmv_tgt(lmv, lsm->lsm_md_oinfo[rc].lmo_mds);
+               if (lmv_dir_layout_changing(lsm)) {
+                       oinfo = lsm_name_to_stripe_info(lsm, name, namelen,
+                                                       true);
+                       if (IS_ERR(oinfo))
+                               RETURN(PTR_ERR(oinfo));
+
+                       op_data->op_fid2 = oinfo->lmo_fid;
+                       tp_tgt = lmv_tgt(lmv, oinfo->lmo_mds);
                        if (!tp_tgt)
                                RETURN(-ENODEV);
                }
@@ -2726,9 +2800,9 @@ free_page:
        return rc;
 }
 
-int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
-                 struct md_callback *cb_op, __u64 offset,
-                 struct page **ppage)
+static int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
+                        struct md_callback *cb_op, __u64 offset,
+                        struct page **ppage)
 {
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -2954,10 +3028,10 @@ static int lmv_rmfid(struct obd_export *exp, struct fid_array *fa,
        }
 
        /* split FIDs by targets */
-       OBD_ALLOC(fas, sizeof(fas) * tgt_count);
+       OBD_ALLOC_PTR_ARRAY(fas, tgt_count);
        if (fas == NULL)
                GOTO(out, rc = -ENOMEM);
-       OBD_ALLOC(rcs, sizeof(int *) * tgt_count);
+       OBD_ALLOC_PTR_ARRAY(rcs, tgt_count);
        if (rcs == NULL)
                GOTO(out_fas, rc = -ENOMEM);
 
@@ -2977,7 +3051,7 @@ static int lmv_rmfid(struct obd_export *exp, struct fid_array *fa,
                if (!fas[idx])
                        GOTO(out, rc = -ENOMEM);
                if (!rcs[idx])
-                       OBD_ALLOC(rcs[idx], sizeof(int) * fa->fa_nr);
+                       OBD_ALLOC_PTR_ARRAY(rcs[idx], fa->fa_nr);
                if (!rcs[idx])
                        GOTO(out, rc = -ENOMEM);
 
@@ -3016,13 +3090,13 @@ out:
                        OBD_FREE(fas[i], offsetof(struct fid_array,
                                                fa_fids[fa->fa_nr]));
                if (rcs && rcs[i])
-                       OBD_FREE(rcs[i], sizeof(int) * fa->fa_nr);
+                       OBD_FREE_PTR_ARRAY(rcs[i], fa->fa_nr);
        }
        if (rcs)
-               OBD_FREE(rcs, sizeof(int *) * tgt_count);
+               OBD_FREE_PTR_ARRAY(rcs, tgt_count);
 out_fas:
        if (fas)
-               OBD_FREE(fas, sizeof(fas) * tgt_count);
+               OBD_FREE_PTR_ARRAY(fas, tgt_count);
 
        RETURN(rc);
 }
@@ -3043,13 +3117,13 @@ out_fas:
  * \retval 0           on success
  * \retval negative    negated errno on failure
  */
-int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
-                       __u32 keylen, void *key, __u32 vallen, void *val,
-                       struct ptlrpc_request_set *set)
+static int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
+                             __u32 keylen, void *key, __u32 vallen, void *val,
+                             struct ptlrpc_request_set *set)
 {
-       struct lmv_tgt_desc     *tgt = NULL;
-       struct obd_device       *obd;
-       struct lmv_obd          *lmv;
+       struct lmv_tgt_desc *tgt;
+       struct obd_device *obd;
+       struct lmv_obd *lmv;
        int rc = 0;
        ENTRY;
 
@@ -3104,9 +3178,10 @@ static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
        if (cplen >= sizeof(lsm->lsm_md_pool_name))
                RETURN(-E2BIG);
 
-       CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %#x "
+       CDEBUG(D_INFO, "unpack lsm count %d/%d, master %d hash_type %#x/%#x "
               "layout_version %d\n", lsm->lsm_md_stripe_count,
-              lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
+              lsm->lsm_md_migrate_offset, lsm->lsm_md_master_mdt_index,
+              lsm->lsm_md_hash_type, lsm->lsm_md_migrate_hash,
               lsm->lsm_md_layout_version);
 
        stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
@@ -3144,6 +3219,8 @@ static inline int lmv_unpack_user_md(struct obd_export *exp,
        lsm->lsm_md_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
        lsm->lsm_md_master_mdt_index = le32_to_cpu(lmu->lum_stripe_offset);
        lsm->lsm_md_hash_type = le32_to_cpu(lmu->lum_hash_type);
+       lsm->lsm_md_max_inherit = lmu->lum_max_inherit;
+       lsm->lsm_md_max_inherit_rr = lmu->lum_max_inherit_rr;
        lsm->lsm_md_pool_name[LOV_MAXPOOLNAME] = 0;
 
        return 0;
@@ -3176,10 +3253,8 @@ static int lmv_unpackmd(struct obd_export *exp, struct lmv_stripe_md **lsmp,
                }
 
                if (lmv_dir_striped(lsm)) {
-                       for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
-                               if (lsm->lsm_md_oinfo[i].lmo_root)
-                                       iput(lsm->lsm_md_oinfo[i].lmo_root);
-                       }
+                       for (i = 0; i < lsm->lsm_md_stripe_count; i++)
+                               iput(lsm->lsm_md_oinfo[i].lmo_root);
                        lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
                } else {
                        lsm_size = lmv_stripe_md_size(0);
@@ -3307,10 +3382,11 @@ static int lmv_set_lock_data(struct obd_export *exp,
        RETURN(rc);
 }
 
-enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
-                             const struct lu_fid *fid, enum ldlm_type type,
-                             union ldlm_policy_data *policy,
-                             enum ldlm_mode mode, struct lustre_handle *lockh)
+static enum ldlm_mode
+lmv_lock_match(struct obd_export *exp, __u64 flags,
+              const struct lu_fid *fid, enum ldlm_type type,
+              union ldlm_policy_data *policy,
+              enum ldlm_mode mode, struct lustre_handle *lockh)
 {
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -3351,9 +3427,10 @@ enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
        RETURN(0);
 }
 
-int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
-                     struct obd_export *dt_exp, struct obd_export *md_exp,
-                     struct lustre_md *md)
+static int
+lmv_get_lustre_md(struct obd_export *exp, struct req_capsule *pill,
+                 struct obd_export *dt_exp, struct obd_export *md_exp,
+                 struct lustre_md *md)
 {
        struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
        struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
@@ -3361,10 +3438,10 @@ int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
        if (!tgt || !tgt->ltd_exp)
                return -EINVAL;
 
-       return md_get_lustre_md(tgt->ltd_exp, req, dt_exp, md_exp, md);
+       return md_get_lustre_md(tgt->ltd_exp, pill, dt_exp, md_exp, md);
 }
 
-int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
+static int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
 {
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -3385,9 +3462,9 @@ int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
        RETURN(md_free_lustre_md(tgt->ltd_exp, md));
 }
 
-int lmv_set_open_replay_data(struct obd_export *exp,
-                            struct obd_client_handle *och,
-                            struct lookup_intent *it)
+static int lmv_set_open_replay_data(struct obd_export *exp,
+                                   struct obd_client_handle *och,
+                                   struct lookup_intent *it)
 {
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -3402,8 +3479,8 @@ int lmv_set_open_replay_data(struct obd_export *exp,
        RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
 }
 
-int lmv_clear_open_replay_data(struct obd_export *exp,
-                              struct obd_client_handle *och)
+static int lmv_clear_open_replay_data(struct obd_export *exp,
+                                     struct obd_client_handle *och)
 {
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -3418,10 +3495,10 @@ int lmv_clear_open_replay_data(struct obd_export *exp,
        RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
 }
 
-int lmv_intent_getattr_async(struct obd_export *exp,
-                            struct md_enqueue_info *minfo)
+static int lmv_intent_getattr_async(struct obd_export *exp,
+                                   struct md_op_item *item)
 {
-       struct md_op_data *op_data = &minfo->mi_data;
+       struct md_op_data *op_data = &item->mop_data;
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
        struct lmv_tgt_desc *ptgt;
@@ -3448,13 +3525,13 @@ int lmv_intent_getattr_async(struct obd_export *exp,
        if (ctgt != ptgt)
                RETURN(-EREMOTE);
 
-       rc = md_intent_getattr_async(ptgt->ltd_exp, minfo);
+       rc = md_intent_getattr_async(ptgt->ltd_exp, item);
 
        RETURN(rc);
 }
 
-int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
-                       struct lu_fid *fid, __u64 *bits)
+static int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
+                              struct lu_fid *fid, __u64 *bits)
 {
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -3471,9 +3548,10 @@ int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
        RETURN(rc);
 }
 
-int lmv_get_fid_from_lsm(struct obd_export *exp,
-                        const struct lmv_stripe_md *lsm,
-                        const char *name, int namelen, struct lu_fid *fid)
+static int lmv_get_fid_from_lsm(struct obd_export *exp,
+                               const struct lmv_stripe_md *lsm,
+                               const char *name, int namelen,
+                               struct lu_fid *fid)
 {
        const struct lmv_oinfo *oinfo;
 
@@ -3493,8 +3571,8 @@ int lmv_get_fid_from_lsm(struct obd_export *exp,
  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
  * we directly fetch data from the slave MDTs.
  */
-int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
-                struct obd_quotactl *oqctl)
+static int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
+                       struct obd_quotactl *oqctl)
 {
        struct obd_device *obd = class_exp2obd(exp);
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -3641,7 +3719,7 @@ static const struct md_ops lmv_md_ops = {
 
 static int __init lmv_init(void)
 {
-       return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
+       return class_register_type(&lmv_obd_ops, &lmv_md_ops, true,
                                   LUSTRE_LMV_NAME, NULL);
 }