Whamcloud - gitweb
LU-17676 build: configure should prefer to ask if
[fs/lustre-release.git] / lustre / lfsck / lfsck_striped_dir.c
index c42d233..6716522 100644 (file)
@@ -20,7 +20,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright (c) 2014, Intel Corporation.
+ * Copyright (c) 2014, 2017, Intel Corporation.
  */
 /*
  * lustre/lfsck/lfsck_striped_dir.c
 
 #define DEBUG_SUBSYSTEM S_LFSCK
 
-#include <lustre/lustre_idl.h>
 #include <lu_object.h>
 #include <dt_object.h>
 #include <md_object.h>
 #include <lustre_lib.h>
 #include <lustre_net.h>
 #include <lustre_lmv.h>
-#include <lustre/lustre_user.h>
 
 #include "lfsck_internal.h"
 
@@ -170,14 +168,13 @@ void lfsck_lmv_put(const struct lu_env *env, struct lfsck_lmv *llmv)
 
                        LASSERT(llmv->ll_lslr != NULL);
 
-                       OBD_FREE_LARGE(llmv->ll_lslr,
-                                      sizeof(*llmv->ll_lslr) *
-                                      llmv->ll_stripes_allocated);
+                       OBD_FREE_PTR_ARRAY_LARGE(llmv->ll_lslr,
+                                                llmv->ll_stripes_allocated);
                        OBD_FREE_PTR(llu);
                } else {
                        if (llmv->ll_lslr != NULL)
-                               OBD_FREE_LARGE(llmv->ll_lslr,
-                                       sizeof(*llmv->ll_lslr) *
+                               OBD_FREE_PTR_ARRAY_LARGE(
+                                       llmv->ll_lslr,
                                        llmv->ll_stripes_allocated);
 
                        OBD_FREE_PTR(llmv);
@@ -196,22 +193,25 @@ void lfsck_lmv_put(const struct lu_env *env, struct lfsck_lmv *llmv)
  * \param[in] del_lmv  true if need to drop the LMV EA
  *
  * \retval             positive number if nothing to be done
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 static int lfsck_disable_master_lmv(const struct lu_env *env,
                                    struct lfsck_component *com,
                                    struct dt_object *obj, bool del_lmv)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       struct lu_attr                  *la     = &info->lti_la;
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct dt_device                *dev    = lfsck_obj2dt_dev(obj);
-       struct thandle                  *th     = NULL;
-       int                              rc     = 0;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lu_attr *la = &info->lti_la;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct dt_device *dev = lfsck_obj2dev(obj);
+       struct thandle *th = NULL;
+       int rc = 0;
+
        ENTRY;
+       if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
+               GOTO(log, rc = 0);
 
-       th = dt_trans_create(env, dev);
+       th = lfsck_trans_create(env, dev, lfsck);
        if (IS_ERR(th))
                GOTO(log, rc = PTR_ERR(th));
 
@@ -234,20 +234,17 @@ static int lfsck_disable_master_lmv(const struct lu_env *env,
        if (unlikely(lfsck_is_dead_obj(obj)))
                GOTO(unlock, rc = 1);
 
-       if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
-               GOTO(unlock, rc = 0);
-
        if (del_lmv) {
-               rc = dt_xattr_del(env, obj, XATTR_NAME_LMV, th, BYPASS_CAPA);
+               rc = dt_xattr_del(env, obj, XATTR_NAME_LMV, th);
                if (rc != 0)
                        GOTO(unlock, rc);
        }
 
-       rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
+       rc = dt_attr_get(env, obj, la);
        if (rc == 0 && !(la->la_flags & LUSTRE_IMMUTABLE_FL)) {
                la->la_valid = LA_FLAGS;
                la->la_flags |= LUSTRE_IMMUTABLE_FL;
-               rc = dt_attr_set(env, obj, la, th, BYPASS_CAPA);
+               rc = dt_attr_set(env, obj, la, th);
        }
 
        GOTO(unlock, rc);
@@ -259,8 +256,8 @@ stop:
        dt_trans_stop(env, dev, th);
 
 log:
-       CDEBUG(D_LFSCK, "%s: namespace LFSCK set the master MDT-object of "
-              "the striped directory "DFID" as read-only: rc = %d\n",
+       CDEBUG(D_LFSCK,
+              "%s: namespace LFSCK set the master MDT-object of the striped directory "DFID" as read-only: rc = %d\n",
               lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
 
        if (rc <= 0) {
@@ -290,20 +287,21 @@ static inline bool lfsck_is_valid_slave_lmv(struct lmv_mds_md_v1 *lmv)
  *
  * \param[in] env      pointer to the thread context
  * \param[in] com      pointer to the lfsck component
+ * \param[in] obj      pointer to the striped directory to be handled
  * \param[in] lnr      pointer to the namespace request that contains the
  *                     striped directory to be handled and other information
  *
  * \retval             positive number if nothing to be done
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 static int lfsck_remove_lmv(const struct lu_env *env,
                            struct lfsck_component *com,
+                           struct dt_object *obj,
                            struct lfsck_namespace_req *lnr)
 {
-       struct dt_object        *obj    = lnr->lnr_obj;
-       struct lustre_handle     lh     = { 0 };
-       int                      rc;
+       struct lustre_handle lh = { 0 };
+       int rc;
 
        lnr->lnr_lmv->ll_ignore = 1;
        rc = lfsck_ibits_lock(env, com->lc_lfsck, obj, &lh,
@@ -335,13 +333,13 @@ static int lfsck_remove_dirent(const struct lu_env *env,
                               struct dt_object *dir,
                               const struct lu_fid *fid, __u32 index)
 {
-       struct lfsck_thread_info        *info = lfsck_env_info(env);
-       struct dt_object                *obj;
-       int                              rc;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct dt_object *obj;
+       int rc;
 
        snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
                 PFID(fid), index);
-       obj = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, fid);
+       obj = lfsck_object_find_bottom(env, com->lc_lfsck, fid);
        if (IS_ERR(obj))
                return PTR_ERR(obj);
 
@@ -367,6 +365,7 @@ static int lfsck_remove_dirent(const struct lu_env *env,
  *
  * \param[in] env      pointer to the thread context
  * \param[in] com      pointer to the lfsck component
+ * \param[in] dir      pointer to the striped directory to be handled
  * \param[in] lslr     pointer to lfsck_disable_master_lmv slot which content
  *                     will be replaced by the given information
  * \param[in] lnr      contain the shard's FID to be used to fill the
@@ -377,20 +376,21 @@ static int lfsck_remove_dirent(const struct lu_env *env,
  * \param[in] index    the old shard's index in the striped directory
  * \param[in] flags    the new shard's flags in the @lslr slot
  *
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 static int lfsck_replace_lmv(const struct lu_env *env,
                             struct lfsck_component *com,
+                            struct dt_object *dir,
                             struct lfsck_slave_lmv_rec *lslr,
                             struct lfsck_namespace_req *lnr,
                             struct lmv_mds_md_v1 *lmv,
                             __u32 index, __u32 flags)
 {
        struct lfsck_lmv *llmv = lnr->lnr_lmv;
-       int               rc;
+       int rc;
 
-       rc = lfsck_remove_dirent(env, com, lnr->lnr_obj,
+       rc = lfsck_remove_dirent(env, com, dir,
                                 &lslr->lslr_fid, index);
        if (rc < 0)
                return rc;
@@ -454,6 +454,7 @@ static int lfsck_replace_lmv(const struct lu_env *env,
  *
  * \param[in] env      pointer to the thread context
  * \param[in] com      pointer to the lfsck component
+ * \param[in] dir      pointer to the striped directory to be handled
  * \param[in] lnr      contain the shard's FID to fill the @lslr slot,
  *                     it also records the known max filled index and
  *                     the known max stripe count
@@ -472,7 +473,7 @@ static int lfsck_replace_lmv(const struct lu_env *env,
  *                     we define the max depth can be called recursively
  *                     (LFSCK_REC_LMV_MAX_DEPTH)
  *
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             "-ERANGE" for invalid @shard_idx
  * \retval             "-EEXIST" for the required lslr slot has been
  *                     occupied by other shard
@@ -480,24 +481,23 @@ static int lfsck_replace_lmv(const struct lu_env *env,
  */
 static int lfsck_record_lmv(const struct lu_env *env,
                            struct lfsck_component *com,
+                           struct dt_object *dir,
                            struct lfsck_namespace_req *lnr,
                            struct lmv_mds_md_v1 *lmv, __u32 shard_idx,
                            __u32 flags, __u32 flags2, __u32 *depth)
 {
-       struct lfsck_instance      *lfsck = com->lc_lfsck;
-       struct lfsck_lmv           *llmv  = lnr->lnr_lmv;
-       struct dt_object           *dir   = lnr->lnr_obj;
-       const struct lu_fid        *fid   = &lnr->lnr_fid;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lfsck_lmv *llmv = lnr->lnr_lmv;
+       const struct lu_fid *fid = &lnr->lnr_fid;
        struct lfsck_slave_lmv_rec *lslr;
        struct lfsck_rec_lmv_save  *lrls;
-       int                         index = shard_idx;
-       int                         rc    = 0;
-       ENTRY;
+       int index = shard_idx;
+       int rc = 0;
 
-       CDEBUG(D_LFSCK, "%s: record slave LMV EA for the striped directory "
-              DFID": shard = "DFID", index = %u, flags = %u, flags2 = %u, "
-              "depth = %d\n", lfsck_lfsck2name(lfsck),
-              PFID(lfsck_dto2fid(dir)), PFID(fid),
+       ENTRY;
+       CDEBUG(D_INFO,
+              "%s: record slave LMV EA for the striped directory "DFID": shard = "DFID", index = %u, flags = %u, flags2 = %u, depth = %d\n",
+              lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(dir)), PFID(fid),
               index, flags, flags2, *depth);
 
        if (index < 0 || index >= LFSCK_LMV_MAX_STRIPES)
@@ -508,7 +508,7 @@ static int lfsck_record_lmv(const struct lu_env *env,
                int new_stripes = index + 1;
                size_t old_size = sizeof(*lslr) * llmv->ll_stripes_allocated;
 
-               OBD_ALLOC_LARGE(new_lslr, sizeof(*new_lslr) * new_stripes);
+               OBD_ALLOC_PTR_ARRAY_LARGE(new_lslr, new_stripes);
                if (new_lslr == NULL) {
                        llmv->ll_failed = 1;
 
@@ -560,14 +560,14 @@ static int lfsck_record_lmv(const struct lu_env *env,
                /* The existing one is a normal valid object. */
                switch (flags) {
                case LSLF_NONE:
-                       /* The two 'valid' name entries claims the same
-                        * index, the LFSCK cannot distinguish which one
-                        * is correct. Then remove the master LMV EA to
-                        * make all shards to be visible to client, and
-                        * mark the master MDT-object as read-only. The
-                        * administrator can handle the conflict with
-                        * more human knowledge. */
-                       rc = lfsck_remove_lmv(env, com, lnr);
+                       /* The two 'valid' name entries claims the same index,
+                        * the LFSCK cannot distinguish which one is correct.
+                        * Then remove the master LMV EA to make all shards to
+                        * be visible to client, and mark the master MDT-object
+                        * as read-only. The administrator can handle the
+                        * conflict with more human knowledge.
+                        */
+                       rc = lfsck_remove_lmv(env, com, dir, lnr);
                        break;
                case LSLF_BAD_INDEX2:
                        GOTO(out, rc = -EEXIST);
@@ -576,21 +576,21 @@ static int lfsck_record_lmv(const struct lu_env *env,
 no_lmvea:
                        if (llmv->ll_lmv.lmv_hash_type &
                            LMV_HASH_FLAG_LOST_LMV) {
-                               /* If the master LMV EA was re-generated
-                                * by the former LFSCK reparation, and
-                                * before such reparation, someone has
-                                * created the conflict object, but the
-                                * LFSCK did not detect such conflict,
-                                * then we have to remove the master
-                                * LMV EA and mark the master MDT-object
-                                * as read-only. The administrator can
-                                * handle the conflict with more human
-                                * knowledge. */
-                               rc = lfsck_remove_lmv(env, com, lnr);
+                               /* If the master LMV EA was re-generated by the
+                                * former LFSCK reparation, and before such
+                                * reparation, someone has created the conflict
+                                * object, but the LFSCK did not detect such
+                                * conflict, then we have to remove the master
+                                * LMV EA and mark the master MDT-object as
+                                * read-only. The administrator can handle the
+                                * conflict with more human knowledge.
+                                */
+                               rc = lfsck_remove_lmv(env, com, dir, lnr);
                        } else {
                                /* Otherwise, remove the current name entry,
                                 * and add its FID in the LFSCK tracing file
-                                * for further processing. */
+                                * for further processing.
+                                */
                                rc = lfsck_namespace_trace_update(env, com, fid,
                                                LNTF_CHECK_PARENT, true);
                                if (rc == 0)
@@ -608,17 +608,18 @@ no_lmvea:
                        lmv->lmv_master_mdt_index = shard_idx;
                        /* The name entry claims an index that is conflict
                         * with a valid existing name entry, then try the
-                        * index in the lmv recursively. */
-                       rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+                        * index in the lmv recursively.
+                        */
+                       rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
                                LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
                        lmv->lmv_master_mdt_index = index;
                        if (rc == -ERANGE || rc == -EEXIST)
-                               /* The index in the lmv is invalid or
-                                * also conflict with other. Then we do
-                                * not know how to resolve the conflict.
-                                * We will handle it as handle the case
-                                * of 'LSLF_NONE' vs 'LSLF_NONE'. */
-                               rc = lfsck_remove_lmv(env, com, lnr);
+                               /* The index in the lmv is invalid or conflict
+                                * with other. Then we do not know how to solve
+                                * the conflict. We will handle it as handle the
+                                * case of 'LSLF_NONE' vs 'LSLF_NONE'.
+                                */
+                               rc = lfsck_remove_lmv(env, com, dir, lnr);
 
                        break;
                default:
@@ -634,30 +635,30 @@ no_lmvea:
 none:
                        if (llmv->ll_lmv.lmv_hash_type &
                            LMV_HASH_FLAG_LOST_LMV) {
-                               /* If the master LMV EA was re-generated
-                                * by the former LFSCK reparation, and
-                                * before such reparation, someone has
-                                * created the conflict object, but the
-                                * LFSCK did not detect such conflict,
-                                * then we have to remove the master
-                                * LMV EA and mark the master MDT-object
-                                * as read-only. The administrator can
-                                * handle the conflict with more human
-                                * knowledge. */
-                               rc = lfsck_remove_lmv(env, com, lnr);
+                               /* If the master LMV EA was re-generated by the
+                                * former LFSCK reparation, and before such
+                                * reparation, someone has created the conflict
+                                * object, but the LFSCK did not detect such
+                                * conflict, then we have to remove the master
+                                * LMV EA and mark the master MDT-object as
+                                * read-only. The administrator can handle the
+                                * conflict with more human knowledge.
+                                */
+                               rc = lfsck_remove_lmv(env, com, dir, lnr);
                        } else {
                                lrls = &lfsck->li_rec_lmv_save[*depth - 1];
                                lrls->lrls_fid = lslr->lslr_fid;
                                /* Otherwise, remove the existing name entry,
-                                * and add its FID in the LFSCK tracing file
-                                * for further processing. Refill the slot
-                                * with current slave LMV EA. */
+                                * and add its FID in the LFSCK tracing file for
+                                * further processing. Refill the slot with
+                                * current slave LMV EA.
+                                */
                                rc = lfsck_namespace_trace_update(env,
                                                com, &lrls->lrls_fid,
                                                LNTF_CHECK_PARENT, true);
                                if (rc == 0)
-                                       rc = lfsck_replace_lmv(env, com, lslr,
-                                                       lnr, lmv, index, flags);
+                                       rc = lfsck_replace_lmv(env, com, dir,
+                                               lslr, lnr, lmv, index, flags);
                        }
 
                        break;
@@ -677,8 +678,9 @@ none:
                        lmv->lmv_master_mdt_index = shard_idx;
                        /* The name entry claims an index that is conflict
                         * with a valid existing name entry, then try the
-                        * index in the lmv recursively. */
-                       rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+                        * index in the lmv recursively.
+                        */
+                       rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
                                LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
                        lmv->lmv_master_mdt_index = index;
                        if (rc == -ERANGE || rc == -EEXIST) {
@@ -699,13 +701,15 @@ none:
                case LSLF_BAD_INDEX2:
                case LSLF_NO_LMVEA:
                        /* Remove the existing dangling name entry.
-                        * Refill the lslr slot with the given LMV. */
-                       rc = lfsck_replace_lmv(env, com, lslr, lnr,
+                        * Refill the lslr slot with the given LMV.
+                        */
+                       rc = lfsck_replace_lmv(env, com, dir, lslr, lnr,
                                               lmv, index, flags);
                        break;
                case LSLF_DANGLING:
                        /* Two dangling name entries conflict,
-                        * remove the current one. */
+                        * remove the current one.
+                        */
                        rc = lfsck_remove_dirent(env, com, dir, fid, index);
                        break;
                case LSLF_BAD_INDEX1:
@@ -713,16 +717,18 @@ none:
                        lmv->lmv_master_mdt_index = shard_idx;
                        /* The name entry claims an index that is conflict
                         * with a valid existing name entry, then try the
-                        * index in the lmv recursively. */
-                       rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+                        * index in the lmv recursively.
+                        */
+                       rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
                                LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
                        lmv->lmv_master_mdt_index = index;
                        if (rc == -ERANGE || rc == -EEXIST)
                                /* If the index in the lmv is invalid or
                                 * also conflict with other, then remove
                                 * the existing dangling name entry.
-                                * Refill the lslr slot with the given LMV. */
-                               rc = lfsck_replace_lmv(env, com, lslr, lnr,
+                                * Refill the lslr slot with the given LMV.
+                                */
+                               rc = lfsck_replace_lmv(env, com, dir, lslr, lnr,
                                                       lmv, shard_idx, flags);
 
                        break;
@@ -745,9 +751,8 @@ none:
                lmv->lmv_hash_type = lslr->lslr_hash_type;
                index = lslr->lslr_index;
 
-               /* The existing one has another possible slot,
-                * try it recursively. */
-               rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+               /* Existing one has another possible slot, try recursively. */
+               rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
                                      LSLF_BAD_INDEX2, flags, depth);
                *lmv = lrls->lrls_lmv;
                lnr->lnr_fid = lrls->lrls_fid;
@@ -786,8 +791,9 @@ conflict:
                         * make all shards to be visible to client, and
                         * mark the master MDT-object as read-only. The
                         * administrator can handle the conflict with
-                        * more human knowledge. */
-                       rc = lfsck_remove_lmv(env, com, lnr);
+                        * more human knowledge.
+                        */
+                       rc = lfsck_remove_lmv(env, com, dir, lnr);
                        break;
                case LSLF_BAD_INDEX2:
                        GOTO(out, rc = -EEXIST);
@@ -802,17 +808,18 @@ conflict:
                        lmv->lmv_master_mdt_index = shard_idx;
                        /* The name entry claims an index that is conflict
                         * with a valid existing name entry, then try the
-                        * index in the lmv recursively. */
-                       rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+                        * index in the lmv recursively.
+                        */
+                       rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
                                LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
                        lmv->lmv_master_mdt_index = index;
                        if (rc == -ERANGE || rc == -EEXIST)
-                               /* The index in the lmv is invalid or
-                                * also conflict with other. Then we do
-                                * not know how to resolve the conflict.
-                                * We will handle it as handle the case
-                                * of 'LSLF_NONE' vs 'LSLF_NONE'. */
-                               rc = lfsck_remove_lmv(env, com, lnr);
+                               /* The index in the lmv is invalid or also
+                                * conflict with other. Then we do not know how
+                                * to resolve the conflict. We will handle it as
+                                * handle the case of 'LSLF_NONE' vs 'LSLF_NONE'
+                                */
+                               rc = lfsck_remove_lmv(env, com, dir, lnr);
 
                        break;
                }
@@ -834,36 +841,92 @@ out:
        return rc > 0 ? 0 : rc;
 }
 
-int lfsck_read_stripe_lmv(const struct lu_env *env, struct dt_object *obj,
+/**
+ * Read LMV from bottom object, so it doesn't contain stripe FIDs.
+ *
+ * TODO: test migrating/foreign directory lfsck
+ *
+ * \param[in] env      thread env
+ * \param[in] lfsck    lfsck instance
+ * \param[in] obj      dt object
+ * \param[out] lmv     LMV data pointer
+ *
+ * \retval             0 on success
+ * \retval             -ENODATA on no LMV, corrupt LMV, dir is dead or foreign
+ *                     -ev on other failures
+ */
+int lfsck_read_stripe_lmv(const struct lu_env *env,
+                         struct lfsck_instance *lfsck,
+                         struct dt_object *obj,
                          struct lmv_mds_md_v1 *lmv)
 {
-       struct dt_object *bottom;
-       int               rc;
-
-       /* Currently, we only store the LMV header on disk. It is the LOD's
-        * duty to iterate the master MDT-object's directory to compose the
-        * integrated LMV EA. But here, we only want to load the LMV header,
-        * so we need to bypass LOD to avoid unnecessary iteration in LOD. */
-       bottom = lu2dt(container_of0(obj->do_lu.lo_header->loh_layers.prev,
-                                    struct lu_object, lo_linkage));
-       if (unlikely(bottom == NULL))
-               return -ENOENT;
-
-       dt_read_lock(env, bottom, 0);
-       rc = dt_xattr_get(env, bottom, lfsck_buf_get(env, lmv, sizeof(*lmv)),
-                         XATTR_NAME_LMV, BYPASS_CAPA);
-       dt_read_unlock(env, bottom);
-       if (rc != sizeof(*lmv))
-               return rc > 0 ? -EINVAL : rc;
-
-       lfsck_lmv_header_le_to_cpu(lmv, lmv);
-       if ((lmv->lmv_magic == LMV_MAGIC &&
-            !(lmv->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) ||
-           (lmv->lmv_magic == LMV_MAGIC_STRIPE &&
-            !(lmv->lmv_hash_type & LMV_HASH_FLAG_DEAD)))
-               return 0;
-
-       return -ENODATA;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lu_buf *buf = &info->lti_buf;
+       struct lmv_foreign_md *lfm;
+       int rc;
+
+       /* use bottom object to avoid reading in shard FIDs */
+       obj = lfsck_object_find_bottom(env, lfsck, lu_object_fid(&obj->do_lu));
+       if (IS_ERR(obj))
+               return PTR_ERR(obj);
+
+       dt_read_lock(env, obj, 0);
+       buf->lb_buf = lmv;
+       buf->lb_len = sizeof(*lmv);
+       rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LMV);
+       if (unlikely(rc == -ERANGE)) {
+               buf = &info->lti_big_buf;
+               /* this may be a foreign LMV */
+               rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
+               if (rc > sizeof(*lmv)) {
+                       int rc1;
+
+                       lu_buf_check_and_alloc(buf, rc);
+                       rc1 = dt_xattr_get(env, obj, buf, XATTR_NAME_LMV);
+                       if (rc != rc1)
+                               rc = -ENODATA;
+               } else {
+                       rc = -ENODATA;
+               }
+       }
+       dt_read_unlock(env, obj);
+
+       lfsck_object_put(env, obj);
+
+       if (rc > offsetof(typeof(*lfm), lfm_value) &&
+           *((__u32 *)buf->lb_buf) == LMV_MAGIC_FOREIGN) {
+               __u32 value_len;
+
+               lfm = buf->lb_buf;
+               value_len = le32_to_cpu(lfm->lfm_length);
+               CDEBUG(D_INFO,
+                      "foreign LMV EA, magic %x, len %u, type %x, flags %x, for dir "DFID"\n",
+                      le32_to_cpu(lfm->lfm_magic), value_len,
+                      le32_to_cpu(lfm->lfm_type), le32_to_cpu(lfm->lfm_flags),
+                      PFID(lfsck_dto2fid(obj)));
+
+               if (rc != value_len + offsetof(typeof(*lfm), lfm_value))
+                       CDEBUG(D_LFSCK,
+                              "foreign LMV EA internal size %u does not match EA full size %d for dir "DFID"\n",
+                              value_len, rc, PFID(lfsck_dto2fid(obj)));
+
+               /* no further usage/decode of foreign LMV outside */
+               return -ENODATA;
+       }
+
+       if (rc == sizeof(*lmv)) {
+               rc = 0;
+               lfsck_lmv_header_le_to_cpu(lmv, lmv);
+               /* if LMV is corrupt, return -ENODATA */
+               if (lmv->lmv_magic != LMV_MAGIC_V1 &&
+                   lmv->lmv_magic != LMV_MAGIC_STRIPE)
+                       rc = -ENODATA;
+       } else if (rc >= 0) {
+               /* LMV is corrupt */
+               rc = -ENODATA;
+       }
+
+       return rc;
 }
 
 /**
@@ -886,9 +949,9 @@ int lfsck_read_stripe_lmv(const struct lu_env *env, struct dt_object *obj,
 int lfsck_shard_name_to_index(const struct lu_env *env, const char *name,
                              int namelen, __u16 type, const struct lu_fid *fid)
 {
-       char    *name2  = lfsck_env_info(env)->lti_tmpbuf2;
-       int      len;
-       int      idx    = 0;
+       char *name2 = lfsck_env_info(env)->lti_tmpbuf2;
+       int len;
+       int idx = 0;
 
        if (!S_ISDIR(type))
                return -ENOTDIR;
@@ -913,24 +976,30 @@ int lfsck_shard_name_to_index(const struct lu_env *env, const char *name,
        return idx;
 }
 
+static inline bool lfsck_name_hash_match(struct lmv_mds_md_v1 *lmv,
+                                        const char *name, int namelen)
+{
+       int idx;
+
+       idx = lmv_name_to_stripe_index_old(lmv, name, namelen);
+       if (idx == lmv->lmv_master_mdt_index)
+               return true;
+
+       if (!lmv_hash_is_layout_changing(lmv->lmv_hash_type))
+               return false;
+
+       idx = lmv_name_to_stripe_index(lmv, name, namelen);
+       return (idx == lmv->lmv_master_mdt_index);
+}
+
 bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
                                     struct lfsck_lmv *llmv,
                                     const char *name, int namelen)
 {
-       struct lmv_mds_md_v1    *lmv;
-       int                      idx;
-
        if (llmv == NULL || !llmv->ll_lmv_slave || !llmv->ll_lmv_verified)
                return true;
 
-       lmv = &llmv->ll_lmv;
-       idx = lmv_name_to_stripe_index(lmv->lmv_hash_type,
-                                      lmv->lmv_stripe_count,
-                                      name, namelen);
-       if (unlikely(idx != lmv->lmv_master_mdt_index))
-               return false;
-
-       return true;
+       return lfsck_name_hash_match(&llmv->ll_lmv, name, namelen);
 }
 
 /**
@@ -952,15 +1021,15 @@ bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
  * \retval             negative error number on failure
  */
 int lfsck_namespace_check_name(const struct lu_env *env,
+                              struct lfsck_instance *lfsck,
                               struct dt_object *parent,
                               struct dt_object *child,
                               const struct lu_name *cname)
 {
-       struct lmv_mds_md_v1    *lmv = &lfsck_env_info(env)->lti_lmv;
-       int                      idx;
-       int                      rc;
+       struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
+       int rc;
 
-       rc = lfsck_read_stripe_lmv(env, parent, lmv);
+       rc = lfsck_read_stripe_lmv(env, lfsck, parent, lmv);
        if (rc != 0)
                RETURN(rc == -ENODATA ? 0 : rc);
 
@@ -968,11 +1037,8 @@ int lfsck_namespace_check_name(const struct lu_env *env,
                if (!lfsck_is_valid_slave_lmv(lmv))
                        return 0;
 
-               idx = lmv_name_to_stripe_index(lmv->lmv_hash_type,
-                                              lmv->lmv_stripe_count,
-                                              cname->ln_name,
-                                              cname->ln_namelen);
-               if (unlikely(idx != lmv->lmv_master_mdt_index))
+               if (!lfsck_name_hash_match(lmv, cname->ln_name,
+                                          cname->ln_namelen))
                        return 1;
        } else if (lfsck_shard_name_to_index(env, cname->ln_name,
                        cname->ln_namelen, lfsck_object_type(child),
@@ -1001,19 +1067,22 @@ int lfsck_namespace_update_lmv(const struct lu_env *env,
                               struct dt_object *obj,
                               struct lmv_mds_md_v1 *lmv, bool locked)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       struct lmv_mds_md_v1            *lmv4   = &info->lti_lmv4;
-       struct lu_buf                   *buf    = &info->lti_buf;
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct dt_device                *dev    = lfsck_obj2dt_dev(obj);
-       struct thandle                  *th     = NULL;
-       struct lustre_handle             lh     = { 0 };
-       int                              rc     = 0;
-       int                              rc1    = 0;
-       ENTRY;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lmv_mds_md_v1 *lmv4 = &info->lti_lmv4;
+       struct lu_buf *buf = &info->lti_buf;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct dt_device *dev = lfsck_obj2dev(obj);
+       struct thandle *th = NULL;
+       struct lustre_handle lh = { 0 };
+       int rc = 0;
+       int rc1 = 0;
 
+       ENTRY;
        LASSERT(lmv4 != lmv);
 
+       if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
+               GOTO(out, rc = 0);
+
        lfsck_lmv_header_cpu_to_le(lmv4, lmv);
        lfsck_buf_init(buf, lmv4, sizeof(*lmv4));
 
@@ -1025,12 +1094,13 @@ int lfsck_namespace_update_lmv(const struct lu_env *env,
                        GOTO(log, rc);
        }
 
-       th = dt_trans_create(env, dev);
+       th = lfsck_trans_create(env, dev, lfsck);
        if (IS_ERR(th))
                GOTO(log, rc = PTR_ERR(th));
 
        /* For remote updating LMV EA, there will be further LFSCK action on
-        * remote MDT after the updating, so update the LMV EA synchronously. */
+        * remote MDT after the updating, so update the LMV EA synchronously.
+        */
        if (dt_object_remote(obj))
                th->th_sync = 1;
 
@@ -1046,10 +1116,7 @@ int lfsck_namespace_update_lmv(const struct lu_env *env,
        if (unlikely(lfsck_is_dead_obj(obj)))
                GOTO(unlock, rc = 1);
 
-       if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
-               GOTO(unlock, rc = 0);
-
-       rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th, BYPASS_CAPA);
+       rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
 
        GOTO(unlock, rc);
 
@@ -1063,8 +1130,10 @@ stop:
 
 log:
        lfsck_ibits_unlock(&lh, LCK_EX);
-       CDEBUG(D_LFSCK, "%s: namespace LFSCK updated the %s LMV EA "
-              "for the object "DFID": rc = %d\n",
+
+out:
+       CDEBUG(D_LFSCK,
+              "%s: namespace LFSCK updated the %s LMV EA for the object "DFID": rc = %d\n",
               lfsck_lfsck2name(lfsck),
               lmv->lmv_magic == LMV_MAGIC ? "master" : "slave",
               PFID(lfsck_dto2fid(obj)), rc);
@@ -1098,27 +1167,26 @@ static int lfsck_allow_regenerate_master_lmv(const struct lu_env *env,
                                             const struct lu_fid *cfid,
                                             __u32 cidx)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       struct lu_fid                   *tfid   = &info->lti_fid3;
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct lu_dirent                *ent    =
-                       (struct lu_dirent *)info->lti_key;
-       const struct dt_it_ops          *iops;
-       struct dt_it                    *di;
-       __u64                            cookie;
-       __u32                            args;
-       int                              rc;
-       __u16                            type;
-       ENTRY;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lu_fid *tfid = &info->lti_fid3;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lu_dirent *ent = (struct lu_dirent *)info->lti_key;
+       const struct dt_it_ops *iops;
+       struct dt_it *di;
+       __u64 cookie;
+       __u32 args;
+       int rc;
+       __u16 type;
 
-       if (unlikely(!dt_try_as_dir(env, obj)))
+       ENTRY;
+       if (unlikely(!dt_try_as_dir(env, obj, true)))
                RETURN(-ENOTDIR);
 
        /* Check whether the shard and the master MDT-object matches or not. */
        snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf), DFID":%u",
                 PFID(cfid), cidx);
        rc = dt_lookup(env, obj, (struct dt_rec *)tfid,
-                      (const struct dt_key *)info->lti_tmpbuf, BYPASS_CAPA);
+                      (const struct dt_key *)info->lti_tmpbuf);
        if (rc != 0)
                RETURN(rc);
 
@@ -1127,7 +1195,7 @@ static int lfsck_allow_regenerate_master_lmv(const struct lu_env *env,
 
        args = lfsck->li_args_dir & ~(LUDA_VERIFY | LUDA_VERIFY_DRYRUN);
        iops = &obj->do_index_ops->dio_it;
-       di = iops->init(env, obj, args, BYPASS_CAPA);
+       di = iops->init(env, obj, args);
        if (IS_ERR(di))
                RETURN(PTR_ERR(di));
 
@@ -1168,7 +1236,8 @@ static int lfsck_allow_regenerate_master_lmv(const struct lu_env *env,
                 *      will cause such subdir to be invisible to client, and
                 *      if its index occupies some lost shard index, then the
                 *      LFSCK will use it to replace the bad shard, and cause
-                *      the subdir (itself) to be invisible for ever. */
+                *      the subdir (itself) to be invisible for ever.
+                */
                if (lfsck_shard_name_to_index(env, ent->lde_name,
                                ent->lde_namelen, type, &ent->lde_fid) < 0)
                        GOTO(out, rc = 1);
@@ -1197,7 +1266,7 @@ out:
  * \param[in] index    the MDT index on which the LFSCK instance to be notified
  *
  * \retval             positive number if nothing to be done
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 static int lfsck_namespace_notify_lmv_remote(const struct lu_env *env,
@@ -1206,14 +1275,14 @@ static int lfsck_namespace_notify_lmv_remote(const struct lu_env *env,
                                             __u32 event, __u32 flags,
                                             __u32 index)
 {
-       struct lfsck_request            *lr     = &lfsck_env_info(env)->lti_lr;
-       const struct lu_fid             *fid    = lfsck_dto2fid(obj);
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct lfsck_tgt_desc           *ltd    = NULL;
-       struct ptlrpc_request           *req    = NULL;
-       int                              rc;
-       ENTRY;
+       struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
+       const struct lu_fid *fid = lfsck_dto2fid(obj);
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lfsck_tgt_desc *ltd = NULL;
+       struct ptlrpc_request *req = NULL;
+       int rc;
 
+       ENTRY;
        ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, index);
        if (ltd == NULL)
                GOTO(out, rc = -ENODEV);
@@ -1233,22 +1302,21 @@ static int lfsck_namespace_notify_lmv_remote(const struct lu_env *env,
        lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
        memset(lr, 0, sizeof(*lr));
        lr->lr_event = event;
-       lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
+       lr->lr_index = lfsck_dev_idx(lfsck);
        lr->lr_active = LFSCK_TYPE_NAMESPACE;
        lr->lr_fid = *fid;
        lr->lr_flags = flags;
 
        ptlrpc_request_set_replen(req);
        rc = ptlrpc_queue_wait(req);
-       ptlrpc_req_finished(req);
+       ptlrpc_req_put(req);
 
        GOTO(out, rc = (rc == -ENOENT ? 1 : rc));
 
 out:
-       CDEBUG(D_LFSCK, "%s: namespace LFSCK notify LMV EA updated for the "
-              "object "DFID" on MDT %x remotely with event %u, flags %u: "
-              "rc = %d\n", lfsck_lfsck2name(lfsck), PFID(fid), index,
-              event, flags, rc);
+       CDEBUG(D_LFSCK,
+              "%s: namespace LFSCK notify LMV EA updated for the object "DFID" on MDT %x remotely with event %u, flags %u: rc = %d\n",
+              lfsck_lfsck2name(lfsck), PFID(fid), index, event, flags, rc);
 
        if (ltd != NULL)
                lfsck_tgt_put(ltd);
@@ -1264,27 +1332,27 @@ out:
  * \param[in] obj      pointer to the striped directory to be rescanned
  *
  * \retval             positive number if nothing to be done
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
                                            struct lfsck_component *com,
                                            struct dt_object *obj)
 {
-       struct lfsck_instance      *lfsck = com->lc_lfsck;
-       struct lfsck_namespace     *ns    = com->lc_file_ram;
-       struct lmv_mds_md_v1       *lmv4  = &lfsck_env_info(env)->lti_lmv4;
-       struct lfsck_lmv_unit      *llu;
-       struct lfsck_lmv           *llmv;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lfsck_namespace *ns = com->lc_file_ram;
+       struct lmv_mds_md_v1 *lmv4 = &lfsck_env_info(env)->lti_lmv4;
+       struct lfsck_lmv_unit *llu;
+       struct lfsck_lmv *llmv;
        struct lfsck_slave_lmv_rec *lslr;
-       int                         count = 0;
-       int                         rc;
-       ENTRY;
+       int count = 0;
+       int rc;
 
+       ENTRY;
        if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
                RETURN(0);
 
-       rc = lfsck_read_stripe_lmv(env, obj, lmv4);
+       rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv4);
        if (rc != 0)
                RETURN(rc);
 
@@ -1299,7 +1367,7 @@ int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
        else
                count = lmv4->lmv_stripe_count;
 
-       OBD_ALLOC_LARGE(lslr, sizeof(struct lfsck_slave_lmv_rec) * count);
+       OBD_ALLOC_PTR_ARRAY_LARGE(lslr, count);
        if (lslr == NULL) {
                OBD_FREE_PTR(llu);
 
@@ -1326,6 +1394,7 @@ int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
                lfsck_lmv_put(env, llmv);
        } else {
                ns->ln_striped_dirs_repaired++;
+               llmv->ll_counted = 1;
                spin_lock(&lfsck->li_lock);
                list_add_tail(&llu->llu_link, &lfsck->li_list_lmv);
                spin_unlock(&lfsck->li_lock);
@@ -1352,41 +1421,33 @@ int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
  *
  * \param[in] env      pointer to the thread context
  * \param[in] com      pointer to the lfsck component
- * \param[in] dir      pointer to the object on which the LMV EA will be set
+ * \param[in] obj      pointer to the object on which the LMV EA will be set
  * \param[in] lmv      pointer to the buffer holding the new LMV EA
  * \param[in] cfid     the shard's FID used for verification
  * \param[in] cidx     the shard's index used for verification
  * \param[in] flags    to indicate which element(s) in the LMV EA will be set
  *
  * \retval             positive number if nothing to be done
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 static int lfsck_namespace_set_lmv_master(const struct lu_env *env,
                                          struct lfsck_component *com,
-                                         struct dt_object *dir,
+                                         struct dt_object *obj,
                                          struct lmv_mds_md_v1 *lmv,
                                          const struct lu_fid *cfid,
                                          __u32 cidx, __u32 flags)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       struct lmv_mds_md_v1            *lmv3   = &info->lti_lmv3;
-       struct lu_seq_range             *range  = &info->lti_range;
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct seq_server_site          *ss     =
-                       lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
-       struct dt_object                *obj;
-       struct lustre_handle             lh     = { 0 };
-       int                              pidx   = -1;
-       int                              rc     = 0;
-       ENTRY;
-
-       /* Find the bottom object to bypass LOD when set LMV EA. */
-       obj = lu2dt(container_of0(dir->do_lu.lo_header->loh_layers.prev,
-                                 struct lu_object, lo_linkage));
-       if (unlikely(obj == NULL))
-               RETURN(-ENOENT);
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lmv_mds_md_v1 *lmv3 = &info->lti_lmv3;
+       struct lu_seq_range *range = &info->lti_range;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct seq_server_site *ss = lfsck_dev_site(lfsck);
+       struct lustre_handle lh = { 0 };
+       int pidx = -1;
+       int rc = 0;
 
+       ENTRY;
        fld_range_set_mdt(range);
        rc = fld_server_lookup(env, ss->ss_server_fld,
                               fid_seq(lfsck_dto2fid(obj)), range);
@@ -1400,7 +1461,7 @@ static int lfsck_namespace_set_lmv_master(const struct lu_env *env,
        if (rc != 0)
                GOTO(log, rc);
 
-       rc = lfsck_read_stripe_lmv(env, obj, lmv3);
+       rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv3);
        if (rc == -ENODATA) {
                if (!(flags & LEF_SET_LMV_ALL))
                        GOTO(log, rc);
@@ -1418,6 +1479,7 @@ static int lfsck_namespace_set_lmv_master(const struct lu_env *env,
 
        lmv3->lmv_magic = LMV_MAGIC;
        lmv3->lmv_master_mdt_index = pidx;
+       lmv3->lmv_layout_version++;
 
        if (flags & LEF_SET_LMV_ALL) {
                rc = lfsck_allow_regenerate_master_lmv(env, com, obj,
@@ -1481,7 +1543,7 @@ log:
  * \param[in] name     the name of the bad name hash
  *
  * \retval             positive number if nothing to be done
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
@@ -1490,16 +1552,15 @@ int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
                                         struct lfsck_lmv *llmv,
                                         const char *name)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       struct lu_fid                   *pfid   = &info->lti_fid3;
-       struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct dt_object                *parent = NULL;
-       int                              rc     = 0;
-       ENTRY;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lu_fid *pfid = &info->lti_fid3;
+       struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct dt_object *parent = NULL;
+       int rc = 0;
 
-       rc = dt_lookup(env, shard, (struct dt_rec *)pfid,
-                      (const struct dt_key *)dotdot, BYPASS_CAPA);
+       ENTRY;
+       rc = dt_lookup_dir(env, shard, dotdot, pfid);
        if (rc != 0 || !fid_is_sane(pfid))
                GOTO(log, rc);
 
@@ -1507,8 +1568,19 @@ int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
        if (IS_ERR(parent))
                GOTO(log, rc = PTR_ERR(parent));
 
+       if (unlikely(!dt_object_exists(parent)))
+               /* The parent object was previously accessed when verifying
+                * the slave LMV EA.  If this condition is true it is because
+                * the striped directory is being removed.
+                */
+               GOTO(log, rc = 1);
+
        *lmv2 = llmv->ll_lmv;
-       lmv2->lmv_hash_type = LMV_HASH_TYPE_UNKNOWN | LMV_HASH_FLAG_BAD_TYPE;
+       /* only set BAD_TYPE here, do not clear hash type or MIGRATION flag,
+        * so that user can resume dir migration if this is caused by dir
+        * migration failure.
+        */
+       lmv2->lmv_hash_type |= LMV_HASH_FLAG_BAD_TYPE;
        rc = lfsck_namespace_set_lmv_master(env, com, parent, lmv2,
                                            lfsck_dto2fid(shard),
                                            llmv->ll_lmv.lmv_master_mdt_index,
@@ -1517,10 +1589,9 @@ int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
        GOTO(log, rc);
 
 log:
-       CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name hash "
-              "on the MDT %x, parent "DFID", name %s, shard_%x "DFID
-              ": rc = %d\n",
-              lfsck_lfsck2name(lfsck), lfsck_dev_idx(lfsck->li_bottom),
+       CDEBUG(D_LFSCK,
+              "%s: namespace LFSCK assistant found bad name hash on the MDT %x, parent "DFID", name %s, shard_%x "DFID": rc = %d\n",
+              lfsck_lfsck2name(lfsck), lfsck_dev_idx(lfsck),
               PFID(pfid), name, llmv->ll_lmv.lmv_master_mdt_index,
               PFID(lfsck_dto2fid(shard)), rc);
 
@@ -1554,31 +1625,30 @@ int lfsck_namespace_scan_shard(const struct lu_env *env,
                               struct lfsck_component *com,
                               struct dt_object *child)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       struct lmv_mds_md_v1            *lmv    = &info->lti_lmv;
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct lfsck_namespace          *ns     = com->lc_file_ram;
-       struct ptlrpc_thread            *thread = &lfsck->li_thread;
-       struct lu_dirent                *ent    =
-                       (struct lu_dirent *)info->lti_key;
-       struct lfsck_bookmark           *bk     = &lfsck->li_bookmark_ram;
-       struct lfsck_lmv                *llmv   = NULL;
-       const struct dt_it_ops          *iops;
-       struct dt_it                    *di;
-       __u64                            cookie;
-       __u32                            args;
-       int                              rc;
-       __u16                            type;
-       ENTRY;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lfsck_namespace *ns = com->lc_file_ram;
+       struct ptlrpc_thread *thread = &lfsck->li_thread;
+       struct lu_dirent *ent = (struct lu_dirent *)info->lti_key;
+       struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
+       struct lfsck_lmv *llmv = NULL;
+       const struct dt_it_ops *iops;
+       struct dt_it *di;
+       __u64 cookie;
+       __u32 args;
+       int rc;
+       __u16 type;
 
-       rc = lfsck_read_stripe_lmv(env, child, lmv);
+       ENTRY;
+       rc = lfsck_read_stripe_lmv(env, lfsck, child, lmv);
        if (rc != 0)
                RETURN(rc == -ENODATA ? 1 : rc);
 
        if (lmv->lmv_magic != LMV_MAGIC_STRIPE)
                RETURN(1);
 
-       if (unlikely(!dt_try_as_dir(env, child)))
+       if (unlikely(!dt_try_as_dir(env, child, true)))
                RETURN(-ENOTDIR);
 
        OBD_ALLOC_PTR(llmv);
@@ -1592,7 +1662,7 @@ int lfsck_namespace_scan_shard(const struct lu_env *env,
 
        args = lfsck->li_args_dir & ~(LUDA_VERIFY | LUDA_VERIFY_DRYRUN);
        iops = &child->do_index_ops->dio_it;
-       di = iops->init(env, child, args, BYPASS_CAPA);
+       di = iops->init(env, child, args);
        if (IS_ERR(di))
                GOTO(out, rc = PTR_ERR(di));
 
@@ -1603,19 +1673,9 @@ int lfsck_namespace_scan_shard(const struct lu_env *env,
                rc = 0;
 
        while (rc == 0) {
-               if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
-                   cfs_fail_val > 0) {
-                       struct l_wait_info lwi;
-
-                       lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
-                                         NULL, NULL);
-                       l_wait_event(thread->t_ctl_waitq,
-                                    !thread_is_running(thread),
-                                    &lwi);
-
-                       if (unlikely(!thread_is_running(thread)))
-                               GOTO(out, rc = 0);
-               }
+               if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
+                   unlikely(!thread_is_running(thread)))
+                       GOTO(out, rc = 0);
 
                rc = iops->rec(env, di, (struct dt_rec *)ent, args);
                if (rc == 0)
@@ -1637,7 +1697,7 @@ int lfsck_namespace_scan_shard(const struct lu_env *env,
                        ns->ln_flags |= LF_INCONSISTENT;
                        rc = lfsck_namespace_repair_bad_name_hash(env, com,
                                                child, llmv, ent->lde_name);
-                       if (rc >= 0)
+                       if (rc == 0)
                                ns->ln_name_hash_repaired++;
                }
 
@@ -1649,7 +1709,7 @@ int lfsck_namespace_scan_shard(const struct lu_env *env,
                if (unlikely(!thread_is_running(thread)))
                        GOTO(out, rc = 0);
 
-               if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
+               if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
                        spin_lock(&lfsck->li_lock);
                        thread_set_flags(thread, SVC_STOPPING);
                        spin_unlock(&lfsck->li_lock);
@@ -1683,7 +1743,8 @@ out:
  * \param[in] obj      pointer to the object which LMV EA will be checked
  * \param[in] llmv     pointer to buffer holding the slave LMV EA
  *
- * \retval             zero for succeed
+ * \retval             positive number if nothing to be done
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
@@ -1691,19 +1752,19 @@ int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
                                        struct dt_object *obj,
                                        struct lfsck_lmv *llmv)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       char                            *name   = info->lti_key;
-       char                            *name2;
-       struct lu_fid                   *pfid   = &info->lti_fid3;
-       struct lu_fid                   *tfid   = &info->lti_fid4;
-       const struct lu_fid             *cfid   = lfsck_dto2fid(obj);
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct lmv_mds_md_v1            *clmv   = &llmv->ll_lmv;
-       struct lmv_mds_md_v1            *plmv   = &info->lti_lmv;
-       struct dt_object                *parent = NULL;
-       int                              rc     = 0;
-       ENTRY;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       char *name = info->lti_key;
+       char *name2;
+       struct lu_fid *pfid = &info->lti_fid3;
+       const struct lu_fid *cfid = lfsck_dto2fid(obj);
+       struct lu_fid tfid;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lmv_mds_md_v1 *clmv = &llmv->ll_lmv;
+       struct lmv_mds_md_v1 *plmv = &info->lti_lmv;
+       struct dt_object *parent = NULL;
+       int rc = 0;
 
+       ENTRY;
        if (!lfsck_is_valid_slave_lmv(clmv)) {
                rc = lfsck_namespace_trace_update(env, com, cfid,
                                        LNTF_UNCERTAIN_LMV, true);
@@ -1711,8 +1772,7 @@ int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
                GOTO(out, rc);
        }
 
-       rc = dt_lookup(env, obj, (struct dt_rec *)pfid,
-                      (const struct dt_key *)dotdot, BYPASS_CAPA);
+       rc = dt_lookup_dir(env, obj, dotdot, pfid);
        if (rc != 0 || !fid_is_sane(pfid)) {
                rc = lfsck_namespace_trace_update(env, com, cfid,
                                        LNTF_UNCERTAIN_LMV, true);
@@ -1720,7 +1780,9 @@ int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
                GOTO(out, rc);
        }
 
-       parent = lfsck_object_find(env, lfsck, pfid);
+       CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
+
+       parent = lfsck_object_find_bottom(env, lfsck, pfid);
        if (IS_ERR(parent)) {
                rc = lfsck_namespace_trace_update(env, com, cfid,
                                        LNTF_UNCERTAIN_LMV, true);
@@ -1728,13 +1790,20 @@ int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
                GOTO(out, rc);
        }
 
-       rc = lfsck_read_stripe_lmv(env, parent, plmv);
+       if (unlikely(!dt_object_exists(parent)))
+               GOTO(out, rc = 1);
+
+       if (unlikely(!dt_try_as_dir(env, parent, true)))
+               GOTO(out, rc = -ENOTDIR);
+
+       rc = lfsck_read_stripe_lmv(env, lfsck, parent, plmv);
        if (rc != 0) {
                int rc1;
 
                /* If the parent has no LMV EA, then it maybe because:
                 * 1) The parent lost the LMV EA.
-                * 2) The child claims a wrong (slave) LMV EA. */
+                * 2) The child claims a wrong (slave) LMV EA.
+                */
                if (rc == -ENODATA)
                        rc = lfsck_namespace_set_lmv_master(env, com, parent,
                                        clmv, cfid, clmv->lmv_master_mdt_index,
@@ -1758,7 +1827,8 @@ int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
        }
 
        /* If the master hash type has been set as LMV_HASH_TYPE_UNKNOWN,
-        * then the slave hash type is not important. */
+        * then the slave hash type is not important.
+        */
        if ((plmv->lmv_hash_type & LMV_HASH_TYPE_MASK) ==
            LMV_HASH_TYPE_UNKNOWN &&
            plmv->lmv_hash_type & LMV_HASH_FLAG_BAD_TYPE)
@@ -1777,16 +1847,15 @@ int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
                 PFID(cfid), clmv->lmv_master_mdt_index);
        name2 = info->lti_tmpbuf2;
 
-       rc = lfsck_links_get_first(env, obj, name, tfid);
-       if (rc == 0 && strcmp(name, name2) == 0 && lu_fid_eq(pfid, tfid)) {
+       rc = lfsck_links_get_first(env, obj, name, &tfid);
+       if (rc == 0 && strcmp(name, name2) == 0 && lu_fid_eq(pfid, &tfid)) {
                llmv->ll_lmv_verified = 1;
 
                GOTO(out, rc);
        }
 
-       rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
-                      (const struct dt_key *)name2, BYPASS_CAPA);
-       if (rc != 0 || !lu_fid_eq(cfid, tfid))
+       rc = dt_lookup_dir(env, parent, name2, &tfid);
+       if (rc != 0 || !lu_fid_eq(cfid, &tfid))
                rc = lfsck_namespace_trace_update(env, com, cfid,
                                                  LNTF_UNCERTAIN_LMV, true);
        else
@@ -1822,35 +1891,35 @@ out:
  * \param[in] lnr      pointer to the namespace request that contains the
  *                     striped directory or the shard
  *
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
                                       struct lfsck_component *com,
                                       struct lfsck_namespace_req *lnr)
 {
-       struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       struct lfsck_instance           *lfsck  = com->lc_lfsck;
-       struct lfsck_namespace          *ns     = com->lc_file_ram;
-       struct lfsck_lmv                *llmv   = lnr->lnr_lmv;
-       struct lmv_mds_md_v1            *lmv    = &llmv->ll_lmv;
-       struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
-       struct dt_object                *dir    = lnr->lnr_obj;
-       const struct lu_fid             *pfid   = lfsck_dto2fid(dir);
-       struct lu_seq_range             *range  = &info->lti_range;
-       struct seq_server_site          *ss     =
-                       lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
-       __u32                            stripe_count;
-       __u32                            hash_type;
-       int                              rc     = 0;
-       int                              i;
-       ENTRY;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lfsck_namespace *ns = com->lc_file_ram;
+       struct lfsck_lmv *llmv = lnr->lnr_lmv;
+       struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
+       struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
+       struct lfsck_assistant_object *lso = lnr->lnr_lar.lar_parent;
+       const struct lu_fid *pfid = &lso->lso_fid;
+       struct dt_object *dir = NULL;
+       struct dt_object *obj = NULL;
+       struct lu_seq_range *range = &info->lti_range;
+       struct seq_server_site *ss = lfsck_dev_site(lfsck);
+       __u32 stripe_count;
+       __u32 hash_type;
+       int rc = 0;
+       int i;
 
+       ENTRY;
        if (llmv->ll_lmv_slave) {
                if (llmv->ll_lmv_verified) {
                        ns->ln_striped_shards_scanned++;
-                       lfsck_namespace_trace_update(env, com,
-                                       lfsck_dto2fid(dir),
+                       lfsck_namespace_trace_update(env, com, pfid,
                                        LNTF_UNCERTAIN_LMV |
                                        LNTF_RECHECK_NAME_HASH, false);
                }
@@ -1860,13 +1929,15 @@ int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
 
        /* Either the striped directory has been disabled or only part of
         * the striped directory have been scanned. The LFSCK cannot repair
-        * something based on incompleted knowledge. So skip it. */
+        * something based on incompleted knowledge. So skip it.
+        */
        if (llmv->ll_ignore || llmv->ll_exit_value <= 0)
                RETURN(0);
 
        /* There ever been some failure, as to the LFSCK cannot know whether
         * it has got the global knowledge about the LMV EA consistency or not,
-        * so it cannot make reparation about the incompleted knowledge. */
+        * so it cannot make reparation about the incompleted knowledge.
+        */
        if (llmv->ll_failed) {
                ns->ln_striped_dirs_scanned++;
                ns->ln_striped_dirs_failed++;
@@ -1899,23 +1970,31 @@ int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
        }
 
        if (llmv->ll_lmv_updated) {
+               if (dir == NULL) {
+                       dir = lfsck_assistant_object_load(env, lfsck, lso);
+                       if (IS_ERR(dir)) {
+                               rc = PTR_ERR(dir);
+
+                               RETURN(rc == -ENOENT ? 0 : rc);
+                       }
+               }
+
                lmv->lmv_layout_version++;
                rc = lfsck_namespace_update_lmv(env, com, dir, lmv, false);
                if (rc != 0)
                        RETURN(rc);
 
                ns->ln_striped_dirs_scanned++;
-               ns->ln_striped_dirs_repaired++;
+               if (!llmv->ll_counted)
+                       ns->ln_striped_dirs_repaired++;
        }
 
        fld_range_set_mdt(range);
        for (i = 0; i <= llmv->ll_max_filled_off; i++) {
-               struct dt_object *obj = NULL;
                struct lfsck_slave_lmv_rec *lslr = llmv->ll_lslr + i;
                const struct lu_fid *cfid = &lslr->lslr_fid;
                const struct lu_name *cname;
-               struct linkea_data ldata = { 0 };
-               int len;
+               struct linkea_data ldata = { NULL };
                int rc1 = 0;
                bool repair_linkea = false;
                bool repair_lmvea = false;
@@ -1930,13 +2009,27 @@ int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
                if (fid_is_zero(cfid))
                        continue;
 
-               len = snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
-                              DFID":%u", PFID(cfid), i);
-               cname = lfsck_name_get_const(env, info->lti_tmpbuf, len);
-               memcpy(lnr->lnr_name, info->lti_tmpbuf, len);
-
+               lnr->lnr_fid = *cfid;
+               lnr->lnr_namelen = scnprintf(lnr->lnr_name,
+                                            lnr->lnr_size - sizeof(*lnr),
+                                            DFID":%u", PFID(cfid), i);
+               cname = lfsck_name_get_const(env, lnr->lnr_name,
+                                            lnr->lnr_namelen);
                obj = lfsck_object_find_bottom(env, lfsck, cfid);
                if (IS_ERR(obj)) {
+                       if (dir == NULL) {
+                               dir = lfsck_assistant_object_load(env, lfsck,
+                                                                 lso);
+                               if (IS_ERR(dir)) {
+                                       if (PTR_ERR(dir) == -ENOENT)
+                                               RETURN(0);
+
+                                       dir = NULL;
+                               }
+                       } else if (lfsck_is_dead_obj(dir)) {
+                               GOTO(out, rc = 0);
+                       }
+
                        rc1 = PTR_ERR(obj);
                        goto next;
                }
@@ -1951,7 +2044,8 @@ int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
                        break;
                case LSLF_BAD_INDEX2:
                        /* The index in the slave LMV EA is right,
-                        * the name entry should be updated. */
+                        * the name entry should be updated.
+                        */
                        rename = true;
                        snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2),
                                 DFID":%u", PFID(cfid), lslr->lslr_index);
@@ -1963,7 +2057,8 @@ int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
                        break;
                case LSLF_BAD_INDEX1:
                        /* The index in the name entry is right,
-                        * the slave LMV EA should be updated. */
+                        * the slave LMV EA should be updated.
+                        */
                case LSLF_NO_LMVEA:
                        repair_lmvea = true;
                        break;
@@ -1974,7 +2069,7 @@ int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
                        break;
                }
 
-               rc1 = lfsck_links_read(env, obj, &ldata);
+               rc1 = lfsck_links_read_with_rec(env, obj, &ldata);
                if (rc1 == -ENOENT) {
                        create = true;
                        goto repair;
@@ -1999,7 +2094,21 @@ int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
 
 repair:
                if (create) {
-                       rc1 = lfsck_namespace_repair_dangling(env, com,
+                       if (dir == NULL) {
+                               dir = lfsck_assistant_object_load(env, lfsck,
+                                                                 lso);
+                               if (IS_ERR(dir)) {
+                                       rc1 = PTR_ERR(dir);
+
+                                       if (rc1 == -ENOENT)
+                                               GOTO(out, rc = 0);
+
+                                       dir = NULL;
+                                       goto next;
+                               }
+                       }
+
+                       rc1 = lfsck_namespace_repair_dangling(env, com, dir,
                                                              obj, lnr);
                        if (rc1 >= 0) {
                                create_repaired = true;
@@ -2049,7 +2158,8 @@ repair:
 
                                /* The slave LMV EA on the remote shard is
                                 * correct, just notify the LFSCK instance
-                                * on such MDT to re-verify the name_hash. */
+                                * on such MDT to re-verify the name_hash.
+                                */
                                rc1 = lfsck_namespace_notify_lmv_remote(env,
                                                com, obj, LE_SET_LMV_SLAVE,
                                                LEF_RECHECK_NAME_HASH,
@@ -2064,6 +2174,20 @@ repair:
                }
 
                if (rename) {
+                       if (dir == NULL) {
+                               dir = lfsck_assistant_object_load(env, lfsck,
+                                                                 lso);
+                               if (IS_ERR(dir)) {
+                                       rc1 = PTR_ERR(dir);
+
+                                       if (rc1 == -ENOENT)
+                                               GOTO(out, rc = 0);
+
+                                       dir = NULL;
+                                       goto next;
+                               }
+                       }
+
                        rc1 = lfsck_namespace_repair_dirent(env, com, dir, obj,
                                        info->lti_tmpbuf2, lnr->lnr_name,
                                        lnr->lnr_type, true, false);
@@ -2084,23 +2208,34 @@ repair:
                if (repair_linkea) {
                        struct lustre_handle lh = { 0 };
 
-                       rc1 = linkea_data_new(&ldata, &info->lti_big_buf);
-                       if (rc1 != 0)
-                               goto next;
+                       if (dir == NULL) {
+                               dir = lfsck_assistant_object_load(env, lfsck,
+                                                                 lso);
+                               if (IS_ERR(dir)) {
+                                       rc1 = PTR_ERR(dir);
+
+                                       if (rc1 == -ENOENT)
+                                               GOTO(out, rc = 0);
 
-                       rc1 = linkea_add_buf(&ldata, cname, lfsck_dto2fid(dir));
+                                       dir = NULL;
+                                       goto next;
+                               }
+                       }
+
+                       rc1 = linkea_links_new(&ldata, &info->lti_big_buf,
+                                              cname, lfsck_dto2fid(dir));
                        if (rc1 != 0)
                                goto next;
 
                        rc1 = lfsck_ibits_lock(env, lfsck, obj, &lh,
                                               MDS_INODELOCK_UPDATE |
                                               MDS_INODELOCK_XATTR, LCK_EX);
-                       lfsck_ibits_unlock(&lh, LCK_EX);
                        if (rc1 != 0)
                                goto next;
 
                        rc1 = lfsck_namespace_rebuild_linkea(env, com, obj,
                                                             &ldata);
+                       lfsck_ibits_unlock(&lh, LCK_EX);
                        if (rc1 >= 0) {
                                linkea_repaired = true;
                                if (rc1 > 0)
@@ -2109,20 +2244,25 @@ repair:
                }
 
 next:
-               CDEBUG(D_LFSCK, "%s: namespace LFSCK repair the shard "
-                     "%d "DFID" of the striped directory "DFID" with "
-                     "dangling %s/%s, rename %s/%s, llinkea %s/%s, "
-                     "repair_lmvea %s/%s: rc = %d\n", lfsck_lfsck2name(lfsck),
-                     i, PFID(cfid), PFID(&lnr->lnr_fid),
-                     create ? "yes" : "no", create_repaired ? "yes" : "no",
-                     rename ? "yes" : "no", rename_repaired ? "yes" : "no",
-                     repair_linkea ? "yes" : "no",
-                     linkea_repaired ? "yes" : "no",
-                     repair_lmvea ? "yes" : "no",
-                     lmvea_repaired ? "yes" : "no", rc1);
-
-               if (obj != NULL && !IS_ERR(obj))
+               if (create || rename || repair_linkea || repair_lmvea) {
+                       CDEBUG(D_LFSCK,
+                              "%s: namespace LFSCK repair the shard %d "DFID" of the striped directory "DFID" with dangling %s/%s, rename %s/%s, llinkea %s/%s, repair_lmvea %s/%s: rc = %d\n",
+                              lfsck_lfsck2name(lfsck),
+                              i, PFID(cfid), PFID(pfid),
+                              create ? "yes" : "no",
+                              create_repaired ? "yes" : "no",
+                              rename ? "yes" : "no",
+                              rename_repaired ? "yes" : "no",
+                              repair_linkea ? "yes" : "no",
+                              linkea_repaired ? "yes" : "no",
+                              repair_lmvea ? "yes" : "no",
+                              lmvea_repaired ? "yes" : "no", rc1);
+               }
+
+               if (obj != NULL && !IS_ERR(obj)) {
                        lfsck_object_put(env, obj);
+                       obj = NULL;
+               }
 
                if (rc1 < 0) {
                        rc = rc1;
@@ -2130,7 +2270,16 @@ next:
                }
        }
 
-       RETURN(rc);
+       GOTO(out, rc);
+
+out:
+       if (obj != NULL && !IS_ERR(obj))
+               lfsck_object_put(env, obj);
+
+       if (dir != NULL && !IS_ERR(dir))
+               lfsck_object_put(env, dir);
+
+       return rc;
 }
 
 /**
@@ -2179,48 +2328,57 @@ next:
  * \param[in] lnr      pointer to the namespace request that contains the
  *                     shard's name, parent object, parent's LMV, and ect.
  *
- * \retval             zero for succeed
+ * \retval             zero for success
  * \retval             negative error number on failure
  */
 int lfsck_namespace_handle_striped_master(const struct lu_env *env,
                                          struct lfsck_component *com,
                                          struct lfsck_namespace_req *lnr)
 {
-       struct lfsck_thread_info   *info        = lfsck_env_info(env);
-       struct lmv_mds_md_v1       *lmv         = &info->lti_lmv;
-       struct lfsck_instance      *lfsck       = com->lc_lfsck;
-       struct lfsck_namespace     *ns          = com->lc_file_ram;
-       struct lfsck_lmv           *llmv        = lnr->lnr_lmv;
-       struct dt_object           *dir         = lnr->lnr_obj;
-       const struct lu_fid        *pfid        = lfsck_dto2fid(dir);
-       struct dt_object           *obj         = NULL;
-       struct dt_device           *dev         = NULL;
-       int                         shard_idx   = 0;
-       int                         stripe      = 0;
-       int                         rc          = 0;
-       int                         depth       = 0;
-       bool                        repaired    = false;
+       struct lfsck_thread_info *info = lfsck_env_info(env);
+       struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
+       struct lfsck_instance *lfsck = com->lc_lfsck;
+       struct lfsck_namespace *ns = com->lc_file_ram;
+       struct lfsck_lmv *llmv = lnr->lnr_lmv;
+       struct lfsck_assistant_object *lso = lnr->lnr_lar.lar_parent;
+       const struct lu_fid *pfid = &lso->lso_fid;
+       struct dt_object *dir;
+       struct dt_object *obj = NULL;
+       struct dt_device *dev = NULL;
+       int shard_idx = 0;
+       int stripe = 0;
+       int rc = 0;
+       int depth = 0;
+       bool repaired = false;
        enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
-       ENTRY;
 
+       ENTRY;
        if (unlikely(llmv->ll_ignore))
                RETURN(0);
 
+       dir = lfsck_assistant_object_load(env, lfsck, lso);
+       if (IS_ERR(dir)) {
+               rc = PTR_ERR(dir);
+
+               RETURN(rc == -ENOENT ? 0 : rc);
+       }
+
        shard_idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
        if (shard_idx < 0)
                GOTO(fail_lmv, rc = shard_idx);
 
-       if (shard_idx == lfsck_dev_idx(lfsck->li_bottom)) {
+       if (shard_idx == lfsck_dev_idx(lfsck)) {
                if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
                        GOTO(out, rc = 0);
 
-               dev = lfsck->li_next;
+               dev = lfsck->li_bottom;
        } else {
                struct lfsck_tgt_desc *ltd;
 
                /* Usually, some local filesystem consistency verification
                 * tools can guarantee the local namespace tree consistenct.
-                * So the LFSCK will only verify the remote directory. */
+                * So the LFSCK will only verify the remote directory.
+                */
                if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
                        rc = lfsck_namespace_trace_update(env, com, pfid,
                                                LNTF_CHECK_PARENT, true);
@@ -2228,10 +2386,10 @@ int lfsck_namespace_handle_striped_master(const struct lu_env *env,
                        GOTO(out, rc);
                }
 
-               ltd = LTD_TGT(&lfsck->li_mdt_descs, shard_idx);
+               ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, shard_idx);
                if (unlikely(ltd == NULL)) {
-                       CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
-                              "did not join the namespace LFSCK\n",
+                       CDEBUG(D_LFSCK,
+                              "%s: cannot talk with MDT %x which did not join the namespace LFSCK\n",
                               lfsck_lfsck2name(lfsck), shard_idx);
                        lfsck_lad_set_bitmap(env, com, shard_idx);
 
@@ -2242,8 +2400,12 @@ int lfsck_namespace_handle_striped_master(const struct lu_env *env,
        }
 
        obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
-       if (IS_ERR(obj))
+       if (IS_ERR(obj)) {
+               if (lfsck_is_dead_obj(dir))
+                       RETURN(0);
+
                GOTO(fail_lmv, rc = PTR_ERR(obj));
+       }
 
        if (!dt_object_exists(obj)) {
                stripe = lfsck_shard_name_to_index(env, lnr->lnr_name,
@@ -2259,7 +2421,7 @@ dangling:
                if (rc == 0) {
                        memset(lmv, 0, sizeof(*lmv));
                        lmv->lmv_magic = LMV_MAGIC;
-                       rc = lfsck_record_lmv(env, com, lnr, lmv, stripe,
+                       rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
                                              LSLF_DANGLING, LSLF_NONE, &depth);
                }
 
@@ -2275,17 +2437,18 @@ dangling:
                GOTO(out, rc = 0);
        }
 
-       rc = lfsck_read_stripe_lmv(env, obj, lmv);
+       rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv);
        if (unlikely(rc == -ENOENT))
                /* It may happen when the remote object has been removed,
-                * but the local MDT does not aware of that. */
+                * but the local MDT does not aware of that.
+                */
                goto dangling;
 
        if (rc == -ENODATA)
-               rc = lfsck_record_lmv(env, com, lnr, lmv, stripe,
+               rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
                                      LSLF_NO_LMVEA, LSLF_NONE, &depth);
        else if (rc == 0)
-               rc = lfsck_record_lmv(env, com, lnr, lmv, stripe,
+               rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
                                      lmv->lmv_master_mdt_index != stripe ?
                                      LSLF_BAD_INDEX1 : LSLF_NONE, LSLF_NONE,
                                      &depth);
@@ -2319,16 +2482,15 @@ out:
        }
 
        if (rc < 0) {
-               CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
-                      "the shard: "DFID", parent "DFID", name %.*s: rc = %d\n",
+               CDEBUG(D_LFSCK,
+                      "%s: namespace LFSCK assistant fail to handle the shard: "DFID", parent "DFID", name %.*s: rc = %d\n",
                       lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
-                      PFID(lfsck_dto2fid(lnr->lnr_obj)),
-                      lnr->lnr_namelen, lnr->lnr_name, rc);
+                      PFID(pfid), lnr->lnr_namelen, lnr->lnr_name, rc);
 
                if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
                     rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
                     rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
-                   dev != NULL && dev != lfsck->li_next)
+                   dev != NULL && dev != lfsck->li_bottom)
                        lfsck_lad_set_bitmap(env, com, shard_idx);
 
                if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT))
@@ -2355,5 +2517,7 @@ out:
        if (obj != NULL && !IS_ERR(obj))
                lfsck_object_put(env, obj);
 
+       lfsck_object_put(env, dir);
+
        return rc;
 }