Whamcloud - gitweb
LU-14472 quota: skip non-exist or inact tgt for lfs_quota
[fs/lustre-release.git] / lustre / lmv / lmv_obd.c
index 6747995..4fded87 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
@@ -36,9 +35,7 @@
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/user_namespace.h>
-#ifdef HAVE_UIDGID_HEADER
-# include <linux/uidgid.h>
-#endif
+#include <linux/uidgid.h>
 #include <linux/slab.h>
 #include <linux/pagemap.h>
 #include <linux/mm.h>
@@ -63,11 +60,12 @@ static int lmv_check_connect(struct obd_device *obd);
 void lmv_activate_target(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
                         int activate)
 {
-        if (tgt->ltd_active == activate)
-                return;
+       if (tgt->ltd_active == activate)
+               return;
 
-        tgt->ltd_active = activate;
-        lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
+       tgt->ltd_active = activate;
+       lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count +=
+               (activate ? 1 : -1);
 
        tgt->ltd_exp->exp_obd->obd_inactive = !activate;
 }
@@ -130,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,
@@ -276,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);
@@ -303,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.
@@ -343,19 +341,19 @@ int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
 
        tgt->ltd_active = 1;
        tgt->ltd_exp = mdc_exp;
-       lmv->desc.ld_active_tgt_count++;
+       lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count++;
 
        md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
 
-       rc = lqos_add_tgt(&lmv->lmv_qos, tgt);
+       rc = lu_qos_add_tgt(&lmv->lmv_qos, tgt);
        if (rc) {
                obd_disconnect(mdc_exp);
                RETURN(rc);
        }
 
        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);
 
@@ -370,8 +368,7 @@ int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
 static void lmv_del_target(struct lmv_obd *lmv, struct lu_tgt_desc *tgt)
 {
        LASSERT(tgt);
-       lqos_del_tgt(&lmv->lmv_qos, tgt);
-       lu_tgt_descs_del(&lmv->lmv_mdt_descs, tgt);
+       ltd_del_tgt(&lmv->lmv_mdt_descs, tgt);
        OBD_FREE_PTR(tgt);
 }
 
@@ -382,7 +379,6 @@ static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
        struct lmv_obd *lmv = &obd->u.lmv;
        struct lmv_tgt_desc *tgt;
        struct lu_tgt_descs *ltd = &lmv->lmv_mdt_descs;
-       int orig_tgt_count = 0;
        int rc = 0;
 
        ENTRY;
@@ -406,11 +402,7 @@ static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
        tgt->ltd_active = 0;
 
        mutex_lock(&ltd->ltd_mutex);
-       rc = lu_tgt_descs_add(ltd, tgt);
-       if (!rc && index >= lmv->desc.ld_tgt_count) {
-               orig_tgt_count = lmv->desc.ld_tgt_count;
-               lmv->desc.ld_tgt_count = index + 1;
-       }
+       rc = ltd_add_tgt(ltd, tgt);
        mutex_unlock(&ltd->ltd_mutex);
 
        if (rc)
@@ -421,14 +413,10 @@ static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
                RETURN(0);
 
        rc = lmv_connect_mdc(obd, tgt);
-       if (rc != 0) {
-               mutex_lock(&ltd->ltd_mutex);
-               lmv->desc.ld_tgt_count = orig_tgt_count;
-               memset(tgt, 0, sizeof(*tgt));
-               mutex_unlock(&ltd->ltd_mutex);
-       } else {
+       if (!rc) {
                int easize = sizeof(struct lmv_stripe_md) +
-                       lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
+                       lmv->lmv_mdt_count * sizeof(struct lu_fid);
+
                lmv_init_ea_size(obd->obd_self_export, easize, 0);
        }
 
@@ -455,7 +443,7 @@ static int lmv_check_connect(struct obd_device *obd)
        if (lmv->connected)
                GOTO(unlock, rc = 0);
 
-       if (lmv->desc.ld_tgt_count == 0) {
+       if (!lmv->lmv_mdt_count) {
                CERROR("%s: no targets configured: rc = -EINVAL\n",
                       obd->obd_name);
                GOTO(unlock, rc = -EINVAL);
@@ -477,7 +465,7 @@ static int lmv_check_connect(struct obd_device *obd)
        }
 
        lmv->connected = 1;
-       easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
+       easize = lmv_mds_md_size(lmv->lmv_mdt_count, LMV_MAGIC);
        lmv_init_ea_size(obd->obd_self_export, easize, 0);
        EXIT;
 unlock:
@@ -491,7 +479,7 @@ out_disc:
                if (!tgt->ltd_exp)
                        continue;
 
-               --lmv->desc.ld_active_tgt_count;
+               --lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count;
                obd_disconnect(tgt->ltd_exp);
        }
 
@@ -500,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,
@@ -522,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);
 
-        lmv_activate_target(lmv, tgt, 0);
-        tgt->ltd_exp = NULL;
-        RETURN(0);
+       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);
 }
 
 static int lmv_disconnect(struct obd_export *exp)
@@ -557,10 +545,8 @@ static int lmv_disconnect(struct obd_export *exp)
        if (lmv->lmv_tgts_kobj)
                kobject_put(lmv->lmv_tgts_kobj);
 
-       if (!lmv->connected)
-               class_export_put(exp);
-       rc = class_disconnect(exp);
        lmv->connected = 0;
+       rc = class_disconnect(exp);
 
        RETURN(rc);
 }
@@ -568,8 +554,8 @@ static int lmv_disconnect(struct obd_export *exp)
 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
                        void __user *uarg)
 {
-       struct obd_device *obddev = class_exp2obd(exp);
-       struct lmv_obd *lmv = &obddev->u.lmv;
+       struct obd_device *obd = class_exp2obd(exp);
+       struct lmv_obd *lmv = &obd->u.lmv;
        struct getinfo_fid2path *gf;
        struct lmv_tgt_desc *tgt;
        struct getinfo_fid2path *remote_gf = NULL;
@@ -823,11 +809,11 @@ err_fput:
 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
                         int len, void *karg, void __user *uarg)
 {
-       struct obd_device *obddev = class_exp2obd(exp);
-       struct lmv_obd *lmv = &obddev->u.lmv;
+       struct obd_device *obd = class_exp2obd(exp);
+       struct lmv_obd *lmv = &obd->u.lmv;
        struct lu_tgt_desc *tgt = NULL;
        int set = 0;
-       __u32 count = lmv->desc.ld_tgt_count;
+       __u32 count = lmv->lmv_mdt_count;
        int rc = 0;
 
        ENTRY;
@@ -843,11 +829,15 @@ static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
                __u32 index;
 
                memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
-               if (index >= count)
+
+               if (index >= lmv->lmv_mdt_descs.ltd_tgts_size)
                        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);
@@ -874,14 +864,10 @@ static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
        case OBD_IOC_QUOTACTL: {
                struct if_quotactl *qctl = karg;
                struct obd_quotactl *oqctl;
+               struct obd_import *imp;
 
                if (qctl->qc_valid == QC_MDTIDX) {
-                       if (count <= qctl->qc_idx)
-                               RETURN(-EINVAL);
-
                        tgt = lmv_tgt(lmv, qctl->qc_idx);
-                       if (!tgt || !tgt->ltd_exp)
-                               RETURN(-EINVAL);
                } else if (qctl->qc_valid == QC_UUID) {
                        lmv_foreach_tgt(lmv, tgt) {
                                if (!obd_uuid_equals(&tgt->ltd_uuid,
@@ -897,10 +883,19 @@ static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
                        RETURN(-EINVAL);
                }
 
-               if (tgt->ltd_index >= count)
-                       RETURN(-EAGAIN);
+               if (!tgt)
+                       RETURN(-ENODEV);
+
+               if (!tgt->ltd_exp)
+                       RETURN(-EINVAL);
+
+               imp = class_exp2cliimp(tgt->ltd_exp);
+               if (!tgt->ltd_active && imp->imp_state != LUSTRE_IMP_IDLE) {
+                       qctl->qc_valid = QC_MDTIDX;
+                       qctl->obd_uuid = tgt->ltd_uuid;
+                       RETURN(-ENODATA);
+               }
 
-               LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
                OBD_ALLOC_PTR(oqctl);
                if (!oqctl)
                        RETURN(-ENOMEM);
@@ -1036,9 +1031,9 @@ hsm_req_err:
        case LL_IOC_HSM_CT_START: {
                struct lustre_kernelcomm *lk = karg;
                if (lk->lk_flags & LK_FLG_STOP)
-                       rc = lmv_hsm_ct_unregister(obddev, cmd, len, lk, uarg);
+                       rc = lmv_hsm_ct_unregister(obd, cmd, len, lk, uarg);
                else
-                       rc = lmv_hsm_ct_register(obddev, cmd, len, lk, uarg);
+                       rc = lmv_hsm_ct_register(obd, cmd, len, lk, uarg);
                break;
        }
        default:
@@ -1049,7 +1044,7 @@ hsm_req_err:
                        /* ll_umount_begin() sets force flag but for lmv, not
                         * mdc. Let's pass it through */
                        mdc_obd = class_exp2obd(tgt->ltd_exp);
-                       mdc_obd->obd_force = obddev->obd_force;
+                       mdc_obd->obd_force = obd->obd_force;
                        err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
                        if (err) {
                                if (tgt->ltd_active) {
@@ -1069,111 +1064,38 @@ hsm_req_err:
        RETURN(rc);
 }
 
-/**
- * This is _inode_ placement policy function (not name).
- */
-static u32 lmv_placement_policy(struct obd_device *obd,
-                               struct md_op_data *op_data)
+int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
+                 struct lu_fid *fid, struct md_op_data *op_data)
 {
+       struct obd_device *obd = class_exp2obd(exp);
        struct lmv_obd *lmv = &obd->u.lmv;
-       struct lmv_user_md *lum;
-       u32 mdt;
-
-       ENTRY;
-
-       if (lmv->desc.ld_tgt_count == 1)
-               RETURN(0);
-
-       lum = op_data->op_data;
-       /*
-        * Choose MDT by
-        * 1. See if the stripe offset is specified by lum.
-        * 2. If parent has default LMV, and its hash type is "space", choose
-        *    MDT with QoS. (see lmv_locate_tgt_qos()).
-        * 3. Then check if default LMV stripe offset is not -1.
-        * 4. Finally choose MDS by name hash if the parent
-        *    is striped directory. (see lmv_locate_tgt()).
-        *
-        * presently explicit MDT location is not supported
-        * for foreign dirs (as it can't be embedded into free
-        * format LMV, like with lum_stripe_offset), so we only
-        * rely on default stripe offset or then name hashing.
-        */
-       if (op_data->op_cli_flags & CLI_SET_MEA && lum != NULL &&
-           le32_to_cpu(lum->lum_magic != LMV_MAGIC_FOREIGN) &&
-           le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
-               mdt = le32_to_cpu(lum->lum_stripe_offset);
-       } else if (op_data->op_code == LUSTRE_OPC_MKDIR &&
-                  !lmv_dir_striped(op_data->op_mea1) &&
-                  lmv_dir_qos_mkdir(op_data->op_default_mea1)) {
-               mdt = op_data->op_mds;
-       } else if (op_data->op_code == LUSTRE_OPC_MKDIR &&
-                  op_data->op_default_mea1 &&
-                  op_data->op_default_mea1->lsm_md_master_mdt_index !=
-                       (__u32)-1) {
-               mdt = op_data->op_default_mea1->lsm_md_master_mdt_index;
-               op_data->op_mds = mdt;
-       } else {
-               mdt = op_data->op_mds;
-       }
-
-       RETURN(mdt);
-}
-
-int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
-{
        struct lmv_tgt_desc *tgt;
        int rc;
 
        ENTRY;
 
-       tgt = lmv_tgt(lmv, mds);
+       LASSERT(op_data);
+       LASSERT(fid);
+
+       tgt = lmv_tgt(lmv, op_data->op_mds);
        if (!tgt)
                RETURN(-ENODEV);
 
+       if (!tgt->ltd_active || !tgt->ltd_exp)
+               RETURN(-ENODEV);
+
        /*
         * New seq alloc and FLD setup should be atomic. Otherwise we may find
         * on server that seq in new allocated fid is not yet known.
         */
        mutex_lock(&tgt->ltd_fid_mutex);
-
-       if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
-               GOTO(out, rc = -ENODEV);
-
-       /*
-        * Asking underlying tgt layer to allocate new fid.
-        */
        rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
+       mutex_unlock(&tgt->ltd_fid_mutex);
        if (rc > 0) {
                LASSERT(fid_is_sane(fid));
                rc = 0;
        }
 
-       EXIT;
-out:
-       mutex_unlock(&tgt->ltd_fid_mutex);
-       return rc;
-}
-
-int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
-                 struct lu_fid *fid, struct md_op_data *op_data)
-{
-       struct obd_device *obd = class_exp2obd(exp);
-       struct lmv_obd *lmv = &obd->u.lmv;
-       u32 mds;
-       int rc;
-
-       ENTRY;
-
-       LASSERT(op_data != NULL);
-       LASSERT(fid != NULL);
-
-       mds = lmv_placement_policy(obd, op_data);
-
-       rc = __lmv_fid_alloc(lmv, fid, mds);
-       if (rc)
-               CERROR("Can't alloc new fid, rc %d\n", rc);
-
        RETURN(rc);
 }
 
@@ -1181,7 +1103,7 @@ static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
 {
        struct lmv_obd *lmv = &obd->u.lmv;
        struct lmv_desc *desc;
-       struct lnet_process_id lnet_id;
+       struct lnet_processid lnet_id;
        int i = 0;
        int rc;
 
@@ -1199,34 +1121,24 @@ static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
                RETURN(-EINVAL);
        }
 
-       obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
-       lmv->desc.ld_tgt_count = 0;
-       lmv->desc.ld_active_tgt_count = 0;
-       lmv->desc.ld_qos_maxage = LMV_DESC_QOS_MAXAGE_DEFAULT;
+       obd_str2uuid(&lmv->lmv_mdt_descs.ltd_lmv_desc.ld_uuid,
+                    desc->ld_uuid.uuid);
+       lmv->lmv_mdt_descs.ltd_lmv_desc.ld_tgt_count = 0;
+       lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count = 0;
+       lmv->lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage =
+               LMV_DESC_QOS_MAXAGE_DEFAULT;
        lmv->max_def_easize = 0;
        lmv->max_easize = 0;
 
        spin_lock_init(&lmv->lmv_lock);
 
-       /* Set up allocation policy (QoS and RR) */
-       INIT_LIST_HEAD(&lmv->lmv_qos.lq_svr_list);
-       init_rwsem(&lmv->lmv_qos.lq_rw_sem);
-       lmv->lmv_qos.lq_dirty = 1;
-       lmv->lmv_qos.lq_reset = 1;
-       /* Default priority is toward free space balance */
-       lmv->lmv_qos.lq_prio_free = 232;
-       /* Default threshold for rr (roughly 17%) */
-       lmv->lmv_qos.lq_threshold_rr = 43;
-
-       lu_qos_rr_init(&lmv->lmv_qos.lq_rr);
-
        /*
         * initialize rr_index to lower 32bit of netid, so that client
         * can distribute subdirs evenly from the beginning.
         */
        while (LNetGetId(i++, &lnet_id) != -ENOENT) {
-               if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND) {
-                       lmv->lmv_qos_rr_index = (u32)lnet_id.nid;
+               if (!nid_is_lo0(&lnet_id.nid)) {
+                       lmv->lmv_qos_rr_index = ntohl(lnet_id.nid.nid_addr[0]);
                        break;
                }
        }
@@ -1241,7 +1153,7 @@ static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
        if (rc)
                CERROR("Can't init FLD, err %d\n", rc);
 
-       rc = lu_tgt_descs_init(&lmv->lmv_mdt_descs);
+       rc = lu_tgt_descs_init(&lmv->lmv_mdt_descs, true);
        if (rc)
                CWARN("%s: error initialize target table: rc = %d\n",
                      obd->obd_name, rc);
@@ -1258,6 +1170,11 @@ static int lmv_cleanup(struct obd_device *obd)
        ENTRY;
 
        fld_client_fini(&lmv->lmv_fld);
+       fld_client_debugfs_fini(&lmv->lmv_fld);
+
+       lprocfs_obd_cleanup(obd);
+       lprocfs_free_md_stats(obd);
+
        lmv_foreach_tgt_safe(lmv, tgt, tmp)
                lmv_del_target(lmv, tgt);
        lu_tgt_descs_fini(&lmv->lmv_mdt_descs);
@@ -1304,21 +1221,21 @@ static int lmv_select_statfs_mdt(struct lmv_obd *lmv, __u32 flags)
        if (flags & OBD_STATFS_FOR_MDT0)
                return 0;
 
-       if (lmv->lmv_statfs_start || lmv->desc.ld_tgt_count == 1)
+       if (lmv->lmv_statfs_start || lmv->lmv_mdt_count == 1)
                return lmv->lmv_statfs_start;
 
        /* choose initial MDT for this client */
        for (i = 0;; i++) {
-               struct lnet_process_id lnet_id;
+               struct lnet_processid lnet_id;
                if (LNetGetId(i, &lnet_id) == -ENOENT)
                        break;
 
-               if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND) {
+               if (!nid_is_lo0(&lnet_id.nid)) {
                        /* We dont need a full 64-bit modulus, just enough
                         * to distribute the requests across MDTs evenly.
                         */
-                       lmv->lmv_statfs_start =
-                               (u32)lnet_id.nid % lmv->desc.ld_tgt_count;
+                       lmv->lmv_statfs_start = nidhash(&lnet_id.nid) %
+                                               lmv->lmv_mdt_count;
                        break;
                }
        }
@@ -1336,6 +1253,7 @@ static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
        __u32 i;
        __u32 idx;
        int rc = 0;
+       int err = 0;
 
        ENTRY;
 
@@ -1346,20 +1264,25 @@ static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
        /* distribute statfs among MDTs */
        idx = lmv_select_statfs_mdt(lmv, flags);
 
-       for (i = 0; i < lmv->desc.ld_tgt_count; i++, idx++) {
-               idx = idx % lmv->desc.ld_tgt_count;
+       for (i = 0; i < lmv->lmv_mdt_descs.ltd_tgts_size; i++, idx++) {
+               idx = idx % lmv->lmv_mdt_descs.ltd_tgts_size;
                tgt = lmv_tgt(lmv, idx);
                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);
+                       err = rc;
+                       /* Try another MDT */
+                       if (flags & OBD_STATFS_SUM)
+                               continue;
                        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 */
@@ -1369,7 +1292,7 @@ static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
                         * clients can be mounted as long as MDT0 is in
                         * service */
                        *osfs = *temp;
-                       break;
+                       GOTO(out_free_temp, rc);
                }
 
                if (i == 0) {
@@ -1382,11 +1305,12 @@ static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
                        osfs->os_granted += temp->os_granted;
                }
        }
-
-       EXIT;
+       /* There is no stats from some MDTs, data incomplete */
+       if (err)
+               rc = err;
 out_free_temp:
        OBD_FREE(temp, sizeof(*temp));
-       return rc;
+       RETURN(rc);
 }
 
 static int lmv_statfs_update(void *cookie, int rc)
@@ -1406,7 +1330,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;
@@ -1423,7 +1347,7 @@ int lmv_statfs_check_update(struct obd_device *obd, struct lmv_tgt_desc *tgt)
        int rc;
 
        if (ktime_get_seconds() - tgt->ltd_statfs_age <
-           obd->u.lmv.desc.ld_qos_maxage)
+           obd->u.lmv.lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage)
                return 0;
 
        rc = obd_statfs_async(tgt->ltd_exp, &oinfo, 0, NULL);
@@ -1555,10 +1479,173 @@ static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
        RETURN(rc);
 }
 
+static struct lu_tgt_desc *lmv_locate_tgt_qos(struct lmv_obd *lmv, __u32 mdt,
+                                             unsigned short dir_depth)
+{
+       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;
+
+       ENTRY;
+
+       if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
+               RETURN(ERR_PTR(-EAGAIN));
+
+       down_write(&lmv->lmv_qos.lq_rw_sem);
+
+       if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
+               GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
+
+       rc = ltd_qos_penalties_calc(&lmv->lmv_mdt_descs);
+       if (rc)
+               GOTO(unlock, tgt = ERR_PTR(rc));
+
+       lmv_foreach_tgt(lmv, tgt) {
+               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. It's more likely for low level directories
+        * "16 / (dir_depth + 10)" is the factor to make it more unlikely for
+        * top level directories, while more likely for low levels.
+        */
+       rand = total_avail * 16 / (total_usable * (dir_depth + 10));
+       if (cur && cur->ltd_qos.ltq_avail >= rand) {
+               tgt = cur;
+               GOTO(unlock, tgt);
+       }
+
+       rand = lu_prandom_u64_max(total_weight);
+
+       lmv_foreach_connected_tgt(lmv, tgt) {
+               if (!tgt->ltd_qos.ltq_usable)
+                       continue;
+
+               cur_weight += tgt->ltd_qos.ltq_weight;
+               if (cur_weight < rand)
+                       continue;
+
+               ltd_qos_update(&lmv->lmv_mdt_descs, tgt, &total_weight);
+               GOTO(unlock, tgt);
+       }
+
+       /* no proper target found */
+       GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
+unlock:
+       up_write(&lmv->lmv_qos.lq_rw_sem);
+
+       return tgt;
+}
+
+static struct lu_tgt_desc *lmv_locate_tgt_rr(struct lmv_obd *lmv)
+{
+       struct lu_tgt_desc *tgt;
+       int i;
+       int index;
+
+       ENTRY;
+
+       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;
+               tgt = lmv_tgt(lmv, index);
+               if (!tgt || !tgt->ltd_exp || !tgt->ltd_active)
+                       continue;
+
+               lmv->lmv_qos_rr_index = (tgt->ltd_index + 1) %
+                                       lmv->lmv_mdt_descs.ltd_tgts_size;
+               spin_unlock(&lmv->lmv_lock);
+
+               RETURN(tgt);
+       }
+       spin_unlock(&lmv->lmv_lock);
+
+       RETURN(ERR_PTR(-ENODEV));
+}
+
+/* locate MDT which is less full (avoid the most full MDT) */
+static struct lu_tgt_desc *lmv_locate_tgt_lf(struct lmv_obd *lmv)
+{
+       struct lu_tgt_desc *min = NULL;
+       struct lu_tgt_desc *tgt;
+       __u64 avail = 0;
+       __u64 rand;
+
+       ENTRY;
+
+       if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
+               RETURN(ERR_PTR(-EAGAIN));
+
+       down_write(&lmv->lmv_qos.lq_rw_sem);
+
+       if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
+               GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
+
+       lmv_foreach_tgt(lmv, tgt) {
+               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);
+               avail += tgt->ltd_qos.ltq_avail;
+               if (!min || min->ltd_qos.ltq_avail > tgt->ltd_qos.ltq_avail)
+                       min = tgt;
+       }
+
+       /* avoid the most full MDT */
+       if (min)
+               avail -= min->ltd_qos.ltq_avail;
+
+       rand = lu_prandom_u64_max(avail);
+       avail = 0;
+       lmv_foreach_connected_tgt(lmv, tgt) {
+               if (!tgt->ltd_qos.ltq_usable)
+                       continue;
+
+               if (tgt == min)
+                       continue;
+
+               avail += tgt->ltd_qos.ltq_avail;
+               if (avail < rand)
+                       continue;
+
+               GOTO(unlock, tgt);
+       }
+
+       /* no proper target found */
+       GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
+unlock:
+       up_write(&lmv->lmv_qos.lq_rw_sem);
+
+       RETURN(tgt);
+}
+
+/* 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;
@@ -1577,12 +1664,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);
@@ -1597,15 +1687,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, normally it will locate MDT by FID, but if this
- * directory has default LMV, and its hash type is "space", locate MDT with QoS.
+ * 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.
@@ -1624,7 +1714,7 @@ lmv_locate_tgt(struct lmv_obd *lmv, struct md_op_data *op_data)
         * index if the file under striped dir is being restored, see
         * ct_restore(). */
        if (op_data->op_bias & MDS_CREATE_VOLATILE &&
-           (int)op_data->op_mds != -1) {
+           op_data->op_mds != LMV_OFFSET_DEFAULT) {
                tgt = lmv_tgt(lmv, op_data->op_mds);
                if (!tgt)
                        return ERR_PTR(-ENODEV);
@@ -1652,35 +1742,12 @@ lmv_locate_tgt(struct lmv_obd *lmv, struct md_op_data *op_data)
                op_data->op_mds = oinfo->lmo_mds;
                tgt = lmv_tgt(lmv, oinfo->lmo_mds);
                if (!tgt)
-                       tgt = ERR_PTR(-ENODEV);
-       } else if (op_data->op_code == LUSTRE_OPC_MKDIR &&
-                  lmv_dir_qos_mkdir(op_data->op_default_mea1) &&
-                  !lmv_dir_striped(lsm)) {
-               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 when mkdir under dir with "space" hash,
-                * this means the cached statfs may be stale, and current mkdir
-                * may not follow QoS accurately, but it's not serious, and it
-                * avoids periodic statfs when client doesn't mkdir under
-                * "space" hashed directories.
-                *
-                * TODO: after MDT support QoS object allocation, also update
-                * statfs for 'lfs mkdir -i -1 ...", currently it's done in user
-                * space.
-                */
-               if (!IS_ERR(tgt)) {
-                       struct obd_device *obd;
-
-                       obd = container_of(lmv, struct obd_device, u.lmv);
-                       lmv_statfs_check_update(obd, tgt);
-               }
+                       return ERR_PTR(-ENODEV);
        } else {
                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;
@@ -1694,7 +1761,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;
@@ -1728,71 +1795,209 @@ lmv_locate_tgt2(struct lmv_obd *lmv, struct md_op_data *op_data)
                                &op_data->op_mds, true);
 }
 
+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_layout_changing(op_data->op_mea1));
+       LASSERT(!op_data->op_new_layout);
+
+       tgt = lmv_locate_tgt(lmv, op_data);
+       if (IS_ERR(tgt))
+               return PTR_ERR(tgt);
+
+       rc = md_getattr_name(tgt->ltd_exp, op_data, &request);
+       if (!rc) {
+               ptlrpc_req_finished(request);
+               return -EEXIST;
+       }
+
+       return rc;
+}
+
+/* mkdir by QoS upon 'lfs mkdir -i -1'.
+ *
+ * NB, mkdir by QoS only if parent is not striped, this is to avoid remote
+ * directories under striped directory.
+ */
+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;
+
+       if (op_data->op_code != LUSTRE_OPC_MKDIR)
+               return false;
+
+       if (lmv_dir_striped(op_data->op_mea1))
+               return false;
+
+       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;
+}
+
+/* mkdir by QoS if either ROOT or parent default LMV is space balanced. */
+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;
+
+       if (op_data->op_code != LUSTRE_OPC_MKDIR)
+               return false;
+
+       if (lmv_dir_striped(op_data->op_mea1))
+               return false;
+
+       return (op_data->op_flags & MF_QOS_MKDIR) ||
+              (lsm && lsm->lsm_md_master_mdt_index == LMV_OFFSET_DEFAULT);
+}
+
+/* if parent default LMV is space balanced, and
+ * 1. max_inherit_rr is set
+ * 2. or parent is ROOT
+ * mkdir roundrobin. Or if parent doesn't have default LMV, while ROOT default
+ * LMV requests roundrobin mkdir, do the same.
+ * NB, this needs to check server is balanced, which is done 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 (op_data->op_flags & MF_RR_MKDIR) ||
+              (lsm && 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)
+{
+       const struct lmv_user_md *lum = op_data->op_data;
+
+       return op_data->op_code == LUSTRE_OPC_MKDIR &&
+              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;
+}
+
+/* parent default LMV master_mdt_index is not -1. */
+static inline bool
+lmv_op_default_specific_mkdir(const struct md_op_data *op_data)
+{
+       return op_data->op_code == LUSTRE_OPC_MKDIR &&
+              op_data->op_default_mea1 &&
+              op_data->op_default_mea1->lsm_md_master_mdt_index !=
+                       LMV_OFFSET_DEFAULT;
+}
+
+/* locate MDT by space usage */
+static struct lu_tgt_desc *lmv_locate_tgt_by_space(struct lmv_obd *lmv,
+                                                  struct md_op_data *op_data,
+                                                  struct lmv_tgt_desc *tgt)
+{
+       struct lmv_tgt_desc *tmp = tgt;
+
+       tgt = lmv_locate_tgt_qos(lmv, op_data->op_mds, op_data->op_dir_depth);
+       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 not necessary, don't create remote directory. */
+                       tgt = tmp;
+               else
+                       tgt = lmv_locate_tgt_rr(lmv);
+       }
+
+       /*
+        * 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)) {
+               op_data->op_mds = tgt->ltd_index;
+               lmv_statfs_check_update(lmv2obd_dev(lmv), tgt);
+       }
+
+       return tgt;
+}
+
 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,
+               gid_t gid, kernel_cap_t cap_effective, __u64 rdev,
                struct ptlrpc_request **request)
 {
        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;
 
-       if (!lmv->desc.ld_active_tgt_count)
+       if (!lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count)
                RETURN(-EIO);
 
        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, to avoid creating new file under old layout of
-                * migrating directory, check old layout here.
+                * name in both old and new layout, check old layout on client.
                 */
-               tgt = lmv_locate_tgt(lmv, op_data);
-               if (IS_ERR(tgt))
-                       RETURN(PTR_ERR(tgt));
-
-               rc = md_getattr_name(tgt->ltd_exp, op_data, request);
-               if (!rc) {
-                       ptlrpc_req_finished(*request);
-                       *request = NULL;
-                       RETURN(-EEXIST);
-               }
-
+               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));
 
-       CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
-               (int)op_data->op_namelen, op_data->op_name,
-               PFID(&op_data->op_fid1), op_data->op_mds);
-
-       rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
-       if (rc)
-               RETURN(rc);
+       /* the order to apply policy in mkdir:
+        * 1. is "lfs mkdir -i N"? mkdir on MDT N.
+        * 2. is "lfs mkdir -i -1"? mkdir by space usage.
+        * 3. is starting MDT specified in default LMV? mkdir on MDT N.
+        * 4. is default LMV space balanced? mkdir by space usage.
+        */
+       if (lmv_op_user_specific_mkdir(op_data)) {
+               struct lmv_user_md *lum = op_data->op_data;
 
-       if (exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE) {
-               /* Send the create request to the MDT where the object
-                * will be located */
-               tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
+               op_data->op_mds = le32_to_cpu(lum->lum_stripe_offset);
+               tgt = lmv_tgt(lmv, op_data->op_mds);
+               if (!tgt)
+                       RETURN(-ENODEV);
+       } else if (lmv_op_user_qos_mkdir(op_data)) {
+               tgt = lmv_locate_tgt_by_space(lmv, op_data, tgt);
+               if (IS_ERR(tgt))
+                       RETURN(PTR_ERR(tgt));
+       } else if (lmv_op_default_specific_mkdir(op_data)) {
+               op_data->op_mds =
+                       op_data->op_default_mea1->lsm_md_master_mdt_index;
+               tgt = lmv_tgt(lmv, op_data->op_mds);
+               if (!tgt)
+                       RETURN(-ENODEV);
+       } else if (lmv_op_default_qos_mkdir(op_data)) {
+               tgt = lmv_locate_tgt_by_space(lmv, op_data, tgt);
                if (IS_ERR(tgt))
                        RETURN(PTR_ERR(tgt));
-
-               op_data->op_mds = tgt->ltd_index;
        }
 
-       CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
-              PFID(&op_data->op_fid2), op_data->op_mds);
+retry:
+       rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
+       if (rc)
+               RETURN(rc);
+
+       CDEBUG(D_INODE, "CREATE name '%.*s' "DFID" on "DFID" -> mds #%x\n",
+               (int)op_data->op_namelen, op_data->op_name,
+               PFID(&op_data->op_fid2), PFID(&op_data->op_fid1),
+               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,
@@ -1802,7 +2007,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
@@ -1845,7 +2073,11 @@ lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
        ENTRY;
 
 retry:
-       tgt = lmv_locate_tgt(lmv, op_data);
+       if (op_data->op_namelen == 2 &&
+           op_data->op_name[0] == '.' && op_data->op_name[1] == '.')
+               tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
+       else
+               tgt = lmv_locate_tgt(lmv, op_data);
        if (IS_ERR(tgt))
                RETURN(PTR_ERR(tgt));
 
@@ -1944,7 +2176,7 @@ static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
 
        op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
        op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
-       op_data->op_cap = cfs_curproc_cap_pack();
+       op_data->op_cap = current_cap();
 
        tgt = lmv_locate_tgt2(lmv, op_data);
        if (IS_ERR(tgt))
@@ -1964,6 +2196,53 @@ static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
        RETURN(rc);
 }
 
+/* migrate the top directory */
+static inline bool lmv_op_topdir_migrate(const struct md_op_data *op_data)
+{
+       if (!S_ISDIR(op_data->op_mode))
+               return false;
+
+       if (lmv_dir_layout_changing(op_data->op_mea1))
+               return false;
+
+       return true;
+}
+
+/* migrate top dir to specific MDTs */
+static inline bool lmv_topdir_specific_migrate(const struct md_op_data *op_data)
+{
+       const struct lmv_user_md *lum = op_data->op_data;
+
+       if (!lmv_op_topdir_migrate(op_data))
+               return false;
+
+       return le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
+}
+
+/* migrate top dir in QoS mode if user issued "lfs migrate -m -1..." */
+static inline bool lmv_topdir_qos_migrate(const struct md_op_data *op_data)
+{
+       const struct lmv_user_md *lum = op_data->op_data;
+
+       if (!lmv_op_topdir_migrate(op_data))
+               return false;
+
+       return le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
+}
+
+static inline bool lmv_subdir_specific_migrate(const struct md_op_data *op_data)
+{
+       const struct lmv_user_md *lum = op_data->op_data;
+
+       if (!S_ISDIR(op_data->op_mode))
+               return false;
+
+       if (!lmv_dir_layout_changing(op_data->op_mea1))
+               return false;
+
+       return le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
+}
+
 static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
                        const char *name, size_t namelen,
                        struct ptlrpc_request **request)
@@ -1976,7 +2255,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;
@@ -1988,36 +2267,22 @@ static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
 
        op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
        op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
-       op_data->op_cap = cfs_curproc_cap_pack();
+       op_data->op_cap = current_cap();
 
        parent_tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
        if (IS_ERR(parent_tgt))
                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);
 
@@ -2025,20 +2290,21 @@ 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);
+
+                       /* parent unchanged and update namespace only */
+                       if (lu_fid_eq(&op_data->op_fid4, &op_data->op_fid2) &&
+                           op_data->op_bias & MDS_MIGRATE_NSONLY)
+                               RETURN(-EALREADY);
                }
        } else {
                sp_tgt = parent_tgt;
@@ -2048,10 +2314,57 @@ static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
        if (IS_ERR(child_tgt))
                RETURN(PTR_ERR(child_tgt));
 
-       if (!S_ISDIR(op_data->op_mode) && tp_tgt)
-               rc = __lmv_fid_alloc(lmv, &target_fid, tp_tgt->ltd_index);
-       else
-               rc = lmv_fid_alloc(NULL, exp, &target_fid, op_data);
+       if (lmv_topdir_specific_migrate(op_data)) {
+               struct lmv_user_md *lum = op_data->op_data;
+
+               op_data->op_mds = le32_to_cpu(lum->lum_stripe_offset);
+       } else if (lmv_topdir_qos_migrate(op_data)) {
+               tgt = lmv_locate_tgt_lf(lmv);
+               if (tgt == ERR_PTR(-EAGAIN))
+                       tgt = lmv_locate_tgt_rr(lmv);
+               if (IS_ERR(tgt))
+                       RETURN(PTR_ERR(tgt));
+
+               op_data->op_mds = tgt->ltd_index;
+       } else if (lmv_subdir_specific_migrate(op_data)) {
+               struct lmv_user_md *lum = op_data->op_data;
+               __u32 i;
+
+               LASSERT(tp_tgt);
+               if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
+                       /* adjust MDTs in lum, since subdir is located on where
+                        * its parent stripe is, not the first specified MDT.
+                        */
+                       for (i = 0; i < le32_to_cpu(lum->lum_stripe_count);
+                            i++) {
+                               if (le32_to_cpu(lum->lum_objects[i].lum_mds) ==
+                                   tp_tgt->ltd_index)
+                                       break;
+                       }
+
+                       if (i == le32_to_cpu(lum->lum_stripe_count))
+                               RETURN(-ENODEV);
+
+                       lum->lum_objects[i].lum_mds =
+                               lum->lum_objects[0].lum_mds;
+                       lum->lum_objects[0].lum_mds =
+                               cpu_to_le32(tp_tgt->ltd_index);
+               }
+               /* NB, the above adjusts subdir migration for command like
+                * "lfs migrate -m 0,1,2 ...", but for migration like
+                * "lfs migrate -m 0 -c 2 ...", the top dir is migrated to MDT0
+                * and MDT1, however its subdir may be migrated to MDT1 and MDT2
+                */
+
+               lum->lum_stripe_offset = cpu_to_le32(tp_tgt->ltd_index);
+               op_data->op_mds = tp_tgt->ltd_index;
+       } else if (tp_tgt) {
+               op_data->op_mds = tp_tgt->ltd_index;
+       } else {
+               op_data->op_mds = sp_tgt->ltd_index;
+       }
+
+       rc = lmv_fid_alloc(NULL, exp, &target_fid, op_data);
        if (rc)
                RETURN(rc);
 
@@ -2150,7 +2463,7 @@ static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
 
        op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
        op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
-       op_data->op_cap = cfs_curproc_cap_pack();
+       op_data->op_cap = current_cap();
 
        op_data->op_name = new;
        op_data->op_namelen = newlen;
@@ -2330,7 +2643,7 @@ struct stripe_dirent {
 struct lmv_dir_ctxt {
        struct lmv_obd          *ldc_lmv;
        struct md_op_data       *ldc_op_data;
-       struct md_callback      *ldc_cb_op;
+       struct md_readdir_info  *ldc_mrinfo;
        __u64                    ldc_hash;
        int                      ldc_count;
        struct stripe_dirent     ldc_stripes[0];
@@ -2432,7 +2745,7 @@ static struct lu_dirent *stripe_dirent_load(struct lmv_dir_ctxt *ctxt,
                op_data->op_fid2 = oinfo->lmo_fid;
                op_data->op_data = oinfo->lmo_root;
 
-               rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_cb_op, hash,
+               rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_mrinfo, hash,
                                  &stripe->sd_page);
 
                op_data->op_fid1 = fid;
@@ -2453,6 +2766,7 @@ static struct lu_dirent *stripe_dirent_load(struct lmv_dir_ctxt *ctxt,
                LASSERT(!ent);
                /* treat error as eof, so dir can be partially accessed */
                stripe->sd_eof = true;
+               ctxt->ldc_mrinfo->mr_partial_readdir_rc = rc;
                LCONSOLE_WARN("dir "DFID" stripe %d readdir failed: %d, "
                              "directory is partially accessed!\n",
                              PFID(&ctxt->ldc_op_data->op_fid1), stripe_index,
@@ -2554,7 +2868,8 @@ static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
  *
  * \param[in] exp      obd export refer to LMV
  * \param[in] op_data  hold those MD parameters of read_entry
- * \param[in] cb_op    ldlm callback being used in enqueue in mdc_read_entry
+ * \param[in] mrinfo   ldlm callback being used in enqueue in mdc_read_entry,
+ *                     and partial readdir result will be stored in it.
  * \param[in] offset   starting hash offset
  * \param[out] ppage   the page holding the entry. Note: because the entry
  *                      will be accessed in upper layer, so we need hold the
@@ -2566,8 +2881,8 @@ static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
  */
 static int lmv_striped_read_page(struct obd_export *exp,
                                 struct md_op_data *op_data,
-                                struct md_callback *cb_op,
-                                __u64 offset, struct page **ppage)
+                                struct md_readdir_info *mrinfo, __u64 offset,
+                                struct page **ppage)
 {
        struct page *page = NULL;
        struct lu_dirpage *dp;
@@ -2605,7 +2920,7 @@ static int lmv_striped_read_page(struct obd_export *exp,
                GOTO(free_page, rc = -ENOMEM);
        ctxt->ldc_lmv = &exp->exp_obd->u.lmv;
        ctxt->ldc_op_data = op_data;
-       ctxt->ldc_cb_op = cb_op;
+       ctxt->ldc_mrinfo = mrinfo;
        ctxt->ldc_hash = offset;
        ctxt->ldc_count = stripe_count;
 
@@ -2677,9 +2992,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_readdir_info *mrinfo, __u64 offset,
+                        struct page **ppage)
 {
        struct obd_device *obd = exp->exp_obd;
        struct lmv_obd *lmv = &obd->u.lmv;
@@ -2692,7 +3007,7 @@ int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
                RETURN(-ENODATA);
 
        if (unlikely(lmv_dir_striped(op_data->op_mea1))) {
-               rc = lmv_striped_read_page(exp, op_data, cb_op, offset, ppage);
+               rc = lmv_striped_read_page(exp, op_data, mrinfo, offset, ppage);
                RETURN(rc);
        }
 
@@ -2700,7 +3015,7 @@ int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
        if (IS_ERR(tgt))
                RETURN(PTR_ERR(tgt));
 
-       rc = md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
+       rc = md_read_page(tgt->ltd_exp, op_data, mrinfo, offset, ppage);
 
        RETURN(rc);
 }
@@ -2743,7 +3058,7 @@ static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
 
        op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
        op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
-       op_data->op_cap = cfs_curproc_cap_pack();
+       op_data->op_cap = current_cap();
 
 retry:
        parent_tgt = lmv_locate_tgt(lmv, op_data);
@@ -2815,9 +3130,6 @@ static int lmv_precleanup(struct obd_device *obd)
 {
        ENTRY;
        libcfs_kkuc_group_rem(&obd->obd_uuid, 0, KUC_GRP_HSM);
-       fld_client_debugfs_fini(&obd->u.lmv.lmv_fld);
-       lprocfs_obd_cleanup(obd);
-       lprocfs_free_md_stats(obd);
        RETURN(0);
 }
 
@@ -2879,7 +3191,7 @@ static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
                        exp->exp_connect_data = *(struct obd_connect_data *)val;
                RETURN(rc);
        } else if (KEY_IS(KEY_TGT_COUNT)) {
-               *((int *)val) = lmv->desc.ld_tgt_count;
+               *((int *)val) = lmv->lmv_mdt_descs.ltd_tgts_size;
                RETURN(0);
        }
 
@@ -2890,10 +3202,10 @@ static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
 static int lmv_rmfid(struct obd_export *exp, struct fid_array *fa,
                     int *__rcs, struct ptlrpc_request_set *_set)
 {
-       struct obd_device *obddev = class_exp2obd(exp);
+       struct obd_device *obd = class_exp2obd(exp);
        struct ptlrpc_request_set *set = _set;
-       struct lmv_obd *lmv = &obddev->u.lmv;
-       int tgt_count = lmv->desc.ld_tgt_count;
+       struct lmv_obd *lmv = &obd->u.lmv;
+       int tgt_count = lmv->lmv_mdt_count;
        struct lu_tgt_desc *tgt;
        struct fid_array *fat, **fas = NULL;
        int i, rc, **rcs = NULL;
@@ -2905,10 +3217,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);
 
@@ -2928,7 +3240,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);
 
@@ -2967,13 +3279,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);
 }
@@ -2994,13 +3306,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;
 
@@ -3042,8 +3354,8 @@ static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
        lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
        lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
        lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
-       if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
-               lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
+       if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE))
+               lsm->lsm_md_hash_type = cfs_fail_val ?: LMV_HASH_TYPE_UNKNOWN;
        else
                lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
        lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
@@ -3055,9 +3367,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);
@@ -3068,7 +3381,7 @@ static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
                 * set default value -1, so lmv_locate_tgt() knows this stripe
                 * target is not initialized.
                 */
-               lsm->lsm_md_oinfo[i].lmo_mds = (u32)-1;
+               lsm->lsm_md_oinfo[i].lmo_mds = LMV_OFFSET_DEFAULT;
                if (!fid_is_sane(&lsm->lsm_md_oinfo[i].lmo_fid))
                        continue;
 
@@ -3095,6 +3408,9 @@ 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;
 }
@@ -3126,10 +3442,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);
@@ -3257,10 +3571,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;
@@ -3280,8 +3595,8 @@ enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
         * since this can be easily found, and only try others if that fails.
         */
        for (i = 0, index = lmv_fid2tgt_index(lmv, fid);
-            i < lmv->desc.ld_tgt_count;
-            i++, index = (index + 1) % lmv->desc.ld_tgt_count) {
+            i < lmv->lmv_mdt_descs.ltd_tgts_size;
+            i++, index = (index + 1) % lmv->lmv_mdt_descs.ltd_tgts_size) {
                if (index < 0) {
                        CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
                               obd->obd_name, PFID(fid), index);
@@ -3301,9 +3616,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);
@@ -3311,10 +3627,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;
@@ -3335,9 +3651,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;
@@ -3352,8 +3668,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;
@@ -3368,8 +3684,8 @@ 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_enqueue_info *minfo)
 {
        struct md_op_data *op_data = &minfo->mi_data;
        struct obd_device *obd = exp->exp_obd;
@@ -3387,7 +3703,7 @@ int lmv_intent_getattr_async(struct obd_export *exp,
        if (IS_ERR(ptgt))
                RETURN(PTR_ERR(ptgt));
 
-       ctgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
+       ctgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
        if (IS_ERR(ctgt))
                RETURN(PTR_ERR(ctgt));
 
@@ -3403,8 +3719,8 @@ int lmv_intent_getattr_async(struct obd_export *exp,
        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;
@@ -3421,13 +3737,15 @@ 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;
 
-       LASSERT(lmv_dir_striped(lsm));
+       if (!lmv_dir_striped(lsm))
+               RETURN(-ESTALE);
 
        oinfo = lsm_name_to_stripe_info(lsm, name, namelen, false);
        if (IS_ERR(oinfo))
@@ -3443,8 +3761,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;
@@ -3537,7 +3855,7 @@ static int lmv_merge_attr(struct obd_export *exp,
        return 0;
 }
 
-struct obd_ops lmv_obd_ops = {
+static const struct obd_ops lmv_obd_ops = {
         .o_owner                = THIS_MODULE,
         .o_setup                = lmv_setup,
         .o_cleanup              = lmv_cleanup,
@@ -3555,7 +3873,7 @@ struct obd_ops lmv_obd_ops = {
         .o_quotactl             = lmv_quotactl
 };
 
-struct md_ops lmv_md_ops = {
+static const struct md_ops lmv_md_ops = {
        .m_get_root             = lmv_get_root,
         .m_null_inode          = lmv_null_inode,
         .m_close                = lmv_close,
@@ -3591,7 +3909,7 @@ 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);
 }