Whamcloud - gitweb
LU-8301 lfsck: handle ROOT fid properly
[fs/lustre-release.git] / lustre / lfsck / lfsck_lib.c
index 6a9e7e0..e3be947 100644 (file)
@@ -20,7 +20,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright (c) 2013, 2014, Intel Corporation.
+ * Copyright (c) 2013, 2015, Intel Corporation.
  */
 /*
  * lustre/lfsck/lfsck_lib.c
@@ -30,7 +30,9 @@
 
 #define DEBUG_SUBSYSTEM S_LFSCK
 
-#include <libcfs/list.h>
+#include <linux/kthread.h>
+#include <linux/sched.h>
+#include <linux/list.h>
 #include <lu_object.h>
 #include <dt_object.h>
 #include <md_object.h>
@@ -66,21 +68,6 @@ static struct list_head lfsck_ost_orphan_list;
 static struct list_head lfsck_mdt_orphan_list;
 static DEFINE_SPINLOCK(lfsck_instance_lock);
 
-static const char *lfsck_status_names[] = {
-       [LS_INIT]               = "init",
-       [LS_SCANNING_PHASE1]    = "scanning-phase1",
-       [LS_SCANNING_PHASE2]    = "scanning-phase2",
-       [LS_COMPLETED]          = "completed",
-       [LS_FAILED]             = "failed",
-       [LS_STOPPED]            = "stopped",
-       [LS_PAUSED]             = "paused",
-       [LS_CRASHED]            = "crashed",
-       [LS_PARTIAL]            = "partial",
-       [LS_CO_FAILED]          = "co-failed",
-       [LS_CO_STOPPED]         = "co-stopped",
-       [LS_CO_PAUSED]          = "co-paused"
-};
-
 const char *lfsck_flags_names[] = {
        "scanned-once",
        "inconsistent",
@@ -107,12 +94,16 @@ enum lfsck_verify_lpf_types {
        LVLT_BY_NAMEENTRY       = 1,
 };
 
-const char *lfsck_status2names(enum lfsck_status status)
+static inline void
+lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
 {
-       if (unlikely(status < 0 || status >= LS_MAX))
-               return "unknown";
-
-       return lfsck_status_names[status];
+       if (type == LFSCK_TYPE_LAYOUT) {
+               ltd->ltd_layout_status = LS_MAX;
+               ltd->ltd_layout_repaired = 0;
+       } else {
+               ltd->ltd_namespace_status = LS_MAX;
+               ltd->ltd_namespace_repaired = 0;
+       }
 }
 
 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
@@ -148,7 +139,7 @@ static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
        }
 
        cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
-               ltd = LTD_TGT(ltds, idx);
+               ltd = lfsck_ltd2tgt(ltds, idx);
                if (likely(ltd != NULL)) {
                        LASSERT(list_empty(&ltd->ltd_layout_list));
                        LASSERT(list_empty(&ltd->ltd_layout_phase_list));
@@ -157,7 +148,7 @@ static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
 
                        ltds->ltd_tgtnr--;
                        cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
-                       LTD_TGT(ltds, idx) = NULL;
+                       lfsck_assign_tgt(ltds, NULL, idx);
                        lfsck_tgt_put(ltd);
                }
        }
@@ -200,8 +191,8 @@ static int __lfsck_add_target(const struct lu_env *env,
        if (index >= ltds->ltd_tgts_bitmap->size) {
                __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
                                    (__u32)BITS_PER_LONG);
-               cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
-               cfs_bitmap_t *new_bitmap;
+               struct cfs_bitmap *old_bitmap = ltds->ltd_tgts_bitmap;
+               struct cfs_bitmap *new_bitmap;
 
                while (newsize < index + 1)
                        newsize <<= 1;
@@ -229,7 +220,7 @@ static int __lfsck_add_target(const struct lu_env *env,
                        GOTO(unlock, rc = -ENOMEM);
        }
 
-       LTD_TGT(ltds, index) = ltd;
+       lfsck_assign_tgt(ltds, ltd, index);
        cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
        ltds->ltd_tgtnr++;
 
@@ -361,29 +352,14 @@ int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
        RETURN(rc);
 }
 
-/**
- * Request the specified ibits lock for the given object.
- *
- * Before the LFSCK modifying on the namespace visible object,
- * it needs to acquire related ibits ldlm lock.
- *
- * \param[in] env      pointer to the thread context
- * \param[in] lfsck    pointer to the lfsck instance
- * \param[in] obj      pointer to the dt_object to be locked
- * \param[out] lh      pointer to the lock handle
- * \param[in] ibits    the bits for the ldlm lock to be acquired
- * \param[in] mode     the mode for the ldlm lock to be acquired
- *
- * \retval             0 for success
- * \retval             negative error number on failure
- */
-int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
-                    struct dt_object *obj, struct lustre_handle *lh,
-                    __u64 bits, ldlm_mode_t mode)
+static int __lfsck_ibits_lock(const struct lu_env *env,
+                             struct lfsck_instance *lfsck,
+                             struct dt_object *obj, struct ldlm_res_id *resid,
+                             struct lustre_handle *lh, __u64 bits,
+                             enum ldlm_mode mode)
 {
        struct lfsck_thread_info        *info   = lfsck_env_info(env);
-       ldlm_policy_data_t              *policy = &info->lti_policy;
-       struct ldlm_res_id              *resid  = &info->lti_resid;
+       union ldlm_policy_data          *policy = &info->lti_policy;
        __u64                            flags  = LDLM_FL_ATOMIC_CB;
        int                              rc;
 
@@ -391,7 +367,6 @@ int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
 
        memset(policy, 0, sizeof(*policy));
        policy->l_inodebits.bits = bits;
-       fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
        if (dt_object_remote(obj)) {
                struct ldlm_enqueue_info *einfo = &info->lti_einfo;
 
@@ -422,6 +397,34 @@ int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
 }
 
 /**
+ * Request the specified ibits lock for the given object.
+ *
+ * Before the LFSCK modifying on the namespace visible object,
+ * it needs to acquire related ibits ldlm lock.
+ *
+ * \param[in] env      pointer to the thread context
+ * \param[in] lfsck    pointer to the lfsck instance
+ * \param[in] obj      pointer to the dt_object to be locked
+ * \param[out] lh      pointer to the lock handle
+ * \param[in] bits     the bits for the ldlm lock to be acquired
+ * \param[in] mode     the mode for the ldlm lock to be acquired
+ *
+ * \retval             0 for success
+ * \retval             negative error number on failure
+ */
+int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
+                    struct dt_object *obj, struct lustre_handle *lh,
+                    __u64 bits, enum ldlm_mode mode)
+{
+       struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
+
+       LASSERT(!lustre_handle_is_used(lh));
+
+       fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
+       return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
+}
+
+/**
  * Release the the specified ibits lock.
  *
  * If the lock has been acquired before, release it
@@ -430,7 +433,7 @@ int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
  * \param[in] lh       pointer to the lock handle
  * \param[in] mode     the mode for the ldlm lock to be released
  */
-void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode)
+void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
 {
        if (lustre_handle_is_used(lh)) {
                ldlm_lock_decref(lh, mode);
@@ -438,6 +441,88 @@ void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode)
        }
 }
 
+/**
+ * Request compound ibits locks for the given <obj, name> pairs.
+ *
+ * Before the LFSCK modifying on the namespace visible object, it needs to
+ * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
+ * the lock purpose. But the simple lfsck_ibits_lock for directory-based
+ * modificationis (such as insert name entry to the directory) may be too
+ * coarse-grained and not efficient.
+ *
+ * The lfsck_lock() will request compound ibits locks on the specified
+ * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
+ * lock on the directory object, and the regular ibits lock on the name hash.
+ *
+ * \param[in] env      pointer to the thread context
+ * \param[in] lfsck    pointer to the lfsck instance
+ * \param[in] obj      pointer to the dt_object to be locked
+ * \param[in] name     used for building the PDO lock resource
+ * \param[out] llh     pointer to the lfsck_lock_handle
+ * \param[in] bits     the bits for the ldlm lock to be acquired
+ * \param[in] mode     the mode for the ldlm lock to be acquired
+ *
+ * \retval             0 for success
+ * \retval             negative error number on failure
+ */
+int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
+              struct dt_object *obj, const char *name,
+              struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
+{
+       struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
+       int                 rc;
+
+       LASSERT(S_ISDIR(lfsck_object_type(obj)));
+       LASSERT(name != NULL);
+       LASSERT(name[0] != 0);
+       LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
+       LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
+
+       switch (mode) {
+       case LCK_EX:
+               llh->llh_pdo_mode = LCK_EX;
+               break;
+       case LCK_PW:
+               llh->llh_pdo_mode = LCK_CW;
+               break;
+       case LCK_PR:
+               llh->llh_pdo_mode = LCK_CR;
+               break;
+       default:
+               CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
+                      DFID"\n", lfsck_lfsck2name(lfsck), mode,
+                      PFID(lfsck_dto2fid(obj)));
+               LBUG();
+       }
+
+       fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
+       rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
+                               MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
+       if (rc != 0)
+               return rc;
+
+       llh->llh_reg_mode = mode;
+       resid->name[LUSTRE_RES_ID_HSH_OFF] = full_name_hash(name, strlen(name));
+       LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
+       rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
+                               bits, llh->llh_reg_mode);
+       if (rc != 0)
+               lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
+
+       return rc;
+}
+
+/**
+ * Release the the compound ibits locks.
+ *
+ * \param[in] llh      pointer to the lfsck_lock_handle to be released
+ */
+void lfsck_unlock(struct lfsck_lock_handle *llh)
+{
+       lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
+       lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
+}
+
 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
                              struct lfsck_instance *lfsck,
                              const struct lu_fid *fid)
@@ -446,6 +531,14 @@ int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
        struct lu_seq_range     *range  = &lfsck_env_info(env)->lti_range;
        int                      rc;
 
+       if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
+               /* "ROOT" is always on the MDT0. */
+               if (lu_fid_eq(fid, &lfsck->li_global_root_fid))
+                       return 0;
+
+               return lfsck_dev_idx(lfsck);
+       }
+
        fld_range_set_mdt(range);
        rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
        if (rc == 0)
@@ -480,12 +573,12 @@ static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
        struct dt_object        *parent = lfsck->li_lpf_root_obj;
        struct dt_device        *dev    = lfsck_obj2dev(parent);
        struct thandle          *th;
-       struct lustre_handle     lh     = { 0 };
+       struct lfsck_lock_handle *llh   = &lfsck_env_info(env)->lti_llh;
        int                      rc;
        ENTRY;
 
-       rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
-                             MDS_INODELOCK_UPDATE, LCK_EX);
+       rc = lfsck_lock(env, lfsck, parent, name, llh,
+                       MDS_INODELOCK_UPDATE, LCK_PW);
        if (rc != 0)
                RETURN(rc);
 
@@ -505,8 +598,7 @@ static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
        if (rc != 0)
                GOTO(stop, rc);
 
-       rc = dt_delete(env, parent, (const struct dt_key *)name, th,
-                      BYPASS_CAPA);
+       rc = dt_delete(env, parent, (const struct dt_key *)name, th);
        if (rc != 0)
                GOTO(stop, rc);
 
@@ -520,7 +612,7 @@ stop:
        dt_trans_stop(env, dev, th);
 
 unlock:
-       lfsck_ibits_unlock(&lh, LCK_EX);
+       lfsck_unlock(llh);
 
        CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
               lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
@@ -569,12 +661,30 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
        if (rc != 0)
                GOTO(stop, rc);
 
+       if (!dt_try_as_dir(env, child))
+               GOTO(stop, rc = -ENOTDIR);
+
        /* 2a. increase child nlink */
        rc = dt_declare_ref_add(env, child, th);
        if (rc != 0)
                GOTO(stop, rc);
 
-       /* 3a. insert linkEA for child */
+       /* 3a. insert dot into child dir */
+       rec->rec_type = S_IFDIR;
+       rec->rec_fid = cfid;
+       rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
+                              (const struct dt_key *)dot, th);
+       if (rc != 0)
+               GOTO(stop, rc);
+
+       /* 4a. insert dotdot into child dir */
+       rec->rec_fid = &LU_LPF_FID;
+       rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
+                              (const struct dt_key *)dotdot, th);
+       if (rc != 0)
+               GOTO(stop, rc);
+
+       /* 5a. insert linkEA for child */
        lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
                       ldata.ld_leh->leh_len);
        rc = dt_declare_xattr_set(env, child, &linkea_buf,
@@ -582,7 +692,7 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
        if (rc != 0)
                GOTO(stop, rc);
 
-       /* 4a. insert name into parent dir */
+       /* 6a. insert name into parent dir */
        rec->rec_type = S_IFDIR;
        rec->rec_fid = cfid;
        rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
@@ -590,12 +700,12 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
        if (rc != 0)
                GOTO(stop, rc);
 
-       /* 5a. increase parent nlink */
+       /* 7a. increase parent nlink */
        rc = dt_declare_ref_add(env, parent, th);
        if (rc != 0)
                GOTO(stop, rc);
 
-       /* 6a. update bookmark */
+       /* 8a. update bookmark */
        rc = dt_declare_record_write(env, bk_obj,
                                     lfsck_buf_get(env, bk, len), 0, th);
        if (rc != 0)
@@ -606,49 +716,46 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
                GOTO(stop, rc);
 
        dt_write_lock(env, child, 0);
-       /* 1b.1. create child */
+       /* 1b. create child */
        rc = dt_create(env, child, la, NULL, dof, th);
        if (rc != 0)
                GOTO(unlock, rc);
 
-       if (unlikely(!dt_try_as_dir(env, child)))
-               GOTO(unlock, rc = -ENOTDIR);
+       /* 2b. increase child nlink */
+       rc = dt_ref_add(env, child, th);
+       if (rc != 0)
+               GOTO(unlock, rc);
 
-       /* 1b.2. insert dot into child dir */
+       /* 3b. insert dot into child dir */
        rec->rec_fid = cfid;
        rc = dt_insert(env, child, (const struct dt_rec *)rec,
-                      (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
+                      (const struct dt_key *)dot, th, 1);
        if (rc != 0)
                GOTO(unlock, rc);
 
-       /* 1b.3. insert dotdot into child dir */
+       /* 4b. insert dotdot into child dir */
        rec->rec_fid = &LU_LPF_FID;
        rc = dt_insert(env, child, (const struct dt_rec *)rec,
-                      (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
-       if (rc != 0)
-               GOTO(unlock, rc);
-
-       /* 2b. increase child nlink */
-       rc = dt_ref_add(env, child, th);
+                      (const struct dt_key *)dotdot, th, 1);
        if (rc != 0)
                GOTO(unlock, rc);
 
-       /* 3b. insert linkEA for child. */
+       /* 5b. insert linkEA for child. */
        rc = dt_xattr_set(env, child, &linkea_buf,
-                         XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
+                         XATTR_NAME_LINK, 0, th);
        dt_write_unlock(env, child);
        if (rc != 0)
                GOTO(stop, rc);
 
-       /* 4b. insert name into parent dir */
+       /* 6b. insert name into parent dir */
        rec->rec_fid = cfid;
        rc = dt_insert(env, parent, (const struct dt_rec *)rec,
-                      (const struct dt_key *)name, th, BYPASS_CAPA, 1);
+                      (const struct dt_key *)name, th, 1);
        if (rc != 0)
                GOTO(stop, rc);
 
        dt_write_lock(env, parent, 0);
-       /* 5b. increase parent nlink */
+       /* 7b. increase parent nlink */
        rc = dt_ref_add(env, parent, th);
        dt_write_unlock(env, parent);
        if (rc != 0)
@@ -657,7 +764,7 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
        bk->lb_lpf_fid = *cfid;
        lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
 
-       /* 6b. update bookmark */
+       /* 8b. update bookmark */
        rc = dt_record_write(env, bk_obj,
                             lfsck_buf_get(env, bk, len), &pos, th);
 
@@ -736,12 +843,30 @@ static int lfsck_create_lpf_remote(const struct lu_env *env,
        if (rc != 0)
                GOTO(stop, rc);
 
+       if (!dt_try_as_dir(env, child))
+               GOTO(stop, rc = -ENOTDIR);
+
        /* 2a. increase child nlink */
        rc = dt_declare_ref_add(env, child, th);
        if (rc != 0)
                GOTO(stop, rc);
 
-       /* 3a. insert linkEA for child */
+       /* 3a. insert dot into child dir */
+       rec->rec_type = S_IFDIR;
+       rec->rec_fid = cfid;
+       rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
+                              (const struct dt_key *)dot, th);
+       if (rc != 0)
+               GOTO(stop, rc);
+
+       /* 4a. insert dotdot into child dir */
+       rec->rec_fid = &LU_LPF_FID;
+       rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
+                              (const struct dt_key *)dotdot, th);
+       if (rc != 0)
+               GOTO(stop, rc);
+
+       /* 5a. insert linkEA for child */
        lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
                       ldata.ld_leh->leh_len);
        rc = dt_declare_xattr_set(env, child, &linkea_buf,
@@ -749,7 +874,7 @@ static int lfsck_create_lpf_remote(const struct lu_env *env,
        if (rc != 0)
                GOTO(stop, rc);
 
-       /* 4a. update bookmark */
+       /* 6a. update bookmark */
        rc = dt_declare_record_write(env, bk_obj,
                                     lfsck_buf_get(env, bk, len), 0, th);
        if (rc != 0)
@@ -760,44 +885,41 @@ static int lfsck_create_lpf_remote(const struct lu_env *env,
                GOTO(stop, rc);
 
        dt_write_lock(env, child, 0);
-       /* 1b.1. create child */
+       /* 1b. create child */
        rc = dt_create(env, child, la, NULL, dof, th);
        if (rc != 0)
                GOTO(unlock, rc);
 
-       if (unlikely(!dt_try_as_dir(env, child)))
-               GOTO(unlock, rc = -ENOTDIR);
+       /* 2b. increase child nlink */
+       rc = dt_ref_add(env, child, th);
+       if (rc != 0)
+               GOTO(unlock, rc);
 
-       /* 1b.2. insert dot into child dir */
+       /* 3b. insert dot into child dir */
        rec->rec_type = S_IFDIR;
        rec->rec_fid = cfid;
        rc = dt_insert(env, child, (const struct dt_rec *)rec,
-                      (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
+                      (const struct dt_key *)dot, th, 1);
        if (rc != 0)
                GOTO(unlock, rc);
 
-       /* 1b.3. insert dotdot into child dir */
+       /* 4b. insert dotdot into child dir */
        rec->rec_fid = &LU_LPF_FID;
        rc = dt_insert(env, child, (const struct dt_rec *)rec,
-                      (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
+                      (const struct dt_key *)dotdot, th, 1);
        if (rc != 0)
                GOTO(unlock, rc);
 
-       /* 2b. increase child nlink */
-       rc = dt_ref_add(env, child, th);
-       if (rc != 0)
-               GOTO(unlock, rc);
-
-       /* 3b. insert linkEA for child */
+       /* 5b. insert linkEA for child */
        rc = dt_xattr_set(env, child, &linkea_buf,
-                         XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
+                         XATTR_NAME_LINK, 0, th);
        if (rc != 0)
                GOTO(unlock, rc);
 
        bk->lb_lpf_fid = *cfid;
        lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
 
-       /* 4b. update bookmark */
+       /* 6b. update bookmark */
        rc = dt_record_write(env, bk_obj,
                             lfsck_buf_get(env, bk, len), &pos, th);
 
@@ -832,7 +954,7 @@ static int lfsck_create_lpf_remote(const struct lu_env *env,
 
        /* 5b. insert name into parent dir */
        rc = dt_insert(env, parent, (const struct dt_rec *)rec,
-                      (const struct dt_key *)name, th, BYPASS_CAPA, 1);
+                      (const struct dt_key *)name, th, 1);
        if (rc != 0)
                GOTO(stop, rc);
 
@@ -882,7 +1004,7 @@ static int lfsck_create_lpf(const struct lu_env *env,
        struct dt_object_format  *dof   = &info->lti_dof;
        struct dt_object         *parent = lfsck->li_lpf_root_obj;
        struct dt_object         *child = NULL;
-       struct lustre_handle      lh    = { 0 };
+       struct lfsck_lock_handle *llh   = &info->lti_llh;
        char                      name[8];
        int                       node  = lfsck_dev_idx(lfsck);
        int                       rc    = 0;
@@ -892,19 +1014,19 @@ static int lfsck_create_lpf(const struct lu_env *env,
        LASSERT(parent != NULL);
        LASSERT(lfsck->li_lpf_obj == NULL);
 
-       rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
-                             MDS_INODELOCK_UPDATE, LCK_EX);
+       snprintf(name, 8, "MDT%04x", node);
+       rc = lfsck_lock(env, lfsck, parent, name, llh,
+                       MDS_INODELOCK_UPDATE, LCK_PW);
        if (rc != 0)
                RETURN(rc);
 
-       snprintf(name, 8, "MDT%04x", node);
        if (fid_is_zero(&bk->lb_lpf_fid)) {
                /* There is corner case that: in former LFSCK scanning we have
                 * created the .lustre/lost+found/MDTxxxx but failed to update
                 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
                 * it from MDT0 firstly. */
                rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
-                              (const struct dt_key *)name, BYPASS_CAPA);
+                              (const struct dt_key *)name);
                if (rc != 0 && rc != -ENOENT)
                        GOTO(unlock, rc);
 
@@ -920,7 +1042,7 @@ static int lfsck_create_lpf(const struct lu_env *env,
                *cfid = bk->lb_lpf_fid;
        }
 
-       child = lfsck_object_find_bottom(env, lfsck, cfid);
+       child = lfsck_object_find_bottom_new(env, lfsck, cfid);
        if (IS_ERR(child))
                GOTO(unlock, rc = PTR_ERR(child));
 
@@ -951,7 +1073,7 @@ static int lfsck_create_lpf(const struct lu_env *env,
        GOTO(unlock, rc);
 
 unlock:
-       lfsck_ibits_unlock(&lh, LCK_EX);
+       lfsck_unlock(llh);
        if (rc != 0 && child != NULL && !IS_ERR(child))
                lfsck_object_put(env, child);
 
@@ -982,7 +1104,7 @@ static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
        int                      rc;
        ENTRY;
 
-       it = iops->init(env, parent, LUDA_64BITHASH, BYPASS_CAPA);
+       it = iops->init(env, parent, LUDA_64BITHASH);
        if (IS_ERR(it))
                RETURN(PTR_ERR(it));
 
@@ -1113,7 +1235,7 @@ static int lfsck_verify_lpf_pairs(const struct lu_env *env,
 
        fid_zero(fid);
        rc = dt_lookup(env, child, (struct dt_rec *)fid,
-                      (const struct dt_key *)dotdot, BYPASS_CAPA);
+                      (const struct dt_key *)dotdot);
        if (rc != 0)
                GOTO(linkea, rc);
 
@@ -1199,7 +1321,7 @@ linkea:
        }
 
        rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
-                      (const struct dt_key *)name2, BYPASS_CAPA);
+                      (const struct dt_key *)name2);
        dt_read_unlock(env, child);
        lfsck_ibits_unlock(&lh, LCK_PR);
        if (rc != 0 && rc != -ENOENT)
@@ -1311,61 +1433,13 @@ int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
                               lfsck_lfsck2name(lfsck), rc);
        }
 
-       if (!fid_is_zero(&bk->lb_lpf_fid)) {
-               if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
-                       struct lu_fid tfid = bk->lb_lpf_fid;
-
-                       /* Invalid FID record in the bookmark file, reset it. */
-                       fid_zero(&bk->lb_lpf_fid);
-                       rc = lfsck_bookmark_store(env, lfsck);
-
-                       CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
-                              " in the bookmark file: rc = %d\n",
-                              lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
-
-                       if (rc != 0)
-                               GOTO(put, rc);
-               } else {
-                       child1 = lfsck_object_find_bottom(env, lfsck,
-                                                         &bk->lb_lpf_fid);
-                       if (IS_ERR(child1)) {
-                               child1 = NULL;
-                               goto find_child2;
-                       }
-
-                       if (unlikely(!dt_object_exists(child1) ||
-                                    dt_object_remote(child1)) ||
-                                    !S_ISDIR(lfsck_object_type(child1))) {
-                               /* Invalid FID record in the bookmark file,
-                                * reset it. */
-                               fid_zero(&bk->lb_lpf_fid);
-                               rc = lfsck_bookmark_store(env, lfsck);
-
-                               CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
-                                      " in the bookmark file: rc = %d\n",
-                                      lfsck_lfsck2name(lfsck),
-                                      PFID(lfsck_dto2fid(child1)), rc);
-
-                               if (rc != 0)
-                                       GOTO(put, rc);
-
-                               lfsck_object_put(env, child1);
-                               child1 = NULL;
-                       } else if (unlikely(!dt_try_as_dir(env, child1))) {
-                               GOTO(put, rc = -ENOTDIR);
-                       }
-               }
-       }
-
-find_child2:
+       /* child2 */
        snprintf(name, 8, "MDT%04x", node);
        rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
-                      (const struct dt_key *)name, BYPASS_CAPA);
+                      (const struct dt_key *)name);
        if (rc == -ENOENT) {
-               if (!fid_is_zero(&bk->lb_lpf_fid))
-                       goto check_child1;
-
-               GOTO(put, rc = 0);
+               rc = 0;
+               goto find_child1;
        }
 
        if (rc != 0)
@@ -1377,7 +1451,7 @@ find_child2:
                if (rc != 0)
                        GOTO(put, rc);
 
-               goto check_child1;
+               goto find_child1;
        }
 
        child2 = lfsck_object_find_bottom(env, lfsck, cfid);
@@ -1391,23 +1465,20 @@ find_child2:
                if (rc != 0)
                        GOTO(put, rc);
 
-               goto check_child1;
+               goto find_child1;
        }
 
-       if (unlikely(!dt_try_as_dir(env, child2)))
-               GOTO(put, rc = -ENOTDIR);
+       if (unlikely(!dt_try_as_dir(env, child2))) {
+               lfsck_object_put(env, child2);
+               child2 = NULL;
+               rc = -ENOTDIR;
+       }
 
-       if (child1 == NULL) {
-               rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
-                                           pfid, LVLT_BY_NAMEENTRY);
-       } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
-               rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
-                                           pfid, LVLT_BY_BOOKMARK);
-               if (!lu_fid_eq(pfid, &LU_LPF_FID))
-                       rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
-                                                   name, pfid,
-                                                   LVLT_BY_NAMEENTRY);
-       } else {
+find_child1:
+       if (fid_is_zero(&bk->lb_lpf_fid))
+               goto check_child2;
+
+       if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
                if (lfsck->li_lpf_obj == NULL) {
                        lu_object_get(&child2->do_lu);
                        lfsck->li_lpf_obj = child2;
@@ -1415,14 +1486,69 @@ find_child2:
 
                cname = lfsck_name_get_const(env, name, strlen(name));
                rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
+
+               GOTO(put, rc);
        }
 
-       GOTO(put, rc);
+       if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
+               struct lu_fid tfid = bk->lb_lpf_fid;
+
+               /* Invalid FID record in the bookmark file, reset it. */
+               fid_zero(&bk->lb_lpf_fid);
+               rc = lfsck_bookmark_store(env, lfsck);
+
+               CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
+                      " in the bookmark file: rc = %d\n",
+                      lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
+
+               if (rc != 0)
+                       GOTO(put, rc);
+
+               goto check_child2;
+       }
+
+       child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
+       if (IS_ERR(child1)) {
+               child1 = NULL;
+               goto check_child2;
+       }
+
+       if (unlikely(!dt_object_exists(child1) ||
+                    dt_object_remote(child1)) ||
+                    !S_ISDIR(lfsck_object_type(child1))) {
+               /* Invalid FID record in the bookmark file, reset it. */
+               fid_zero(&bk->lb_lpf_fid);
+               rc = lfsck_bookmark_store(env, lfsck);
+
+               CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
+                      " in the bookmark file: rc = %d\n",
+                      lfsck_lfsck2name(lfsck),
+                      PFID(lfsck_dto2fid(child1)), rc);
+
+               if (rc != 0)
+                       GOTO(put, rc);
+
+               lfsck_object_put(env, child1);
+               child1 = NULL;
+               goto check_child2;
+       }
+
+       if (unlikely(!dt_try_as_dir(env, child1))) {
+               lfsck_object_put(env, child1);
+               child1 = NULL;
+               rc = -ENOTDIR;
+               goto check_child2;
+       }
 
-check_child1:
-       if (child1 != NULL)
-               rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
-                                           pfid, LVLT_BY_BOOKMARK);
+       rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
+                                   LVLT_BY_BOOKMARK);
+       if (lu_fid_eq(pfid, &LU_LPF_FID))
+               GOTO(put, rc);
+
+check_child2:
+       if (child2 != NULL)
+               rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
+                                           pfid, LVLT_BY_NAMEENTRY);
 
        GOTO(put, rc);
 
@@ -1620,8 +1746,8 @@ static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
        return 0;
 }
 
-int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
-                   const char *prefix)
+void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
+                    const char *prefix)
 {
        int flag;
        int i;
@@ -1643,36 +1769,36 @@ int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
        }
 
        if (!newline)
-               seq_printf(m, "\n");
-       return 0;
+               seq_putc(m, '\n');
 }
 
-int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix)
+void lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
 {
-       if (time != 0)
-               seq_printf(m, "%s: "LPU64" seconds\n", prefix,
-                         cfs_time_current_sec() - time);
-       else
-               seq_printf(m, "%s: N/A\n", prefix);
-       return 0;
+       if (time == 0) {
+               seq_printf(m, "%s_time: N/A\n", name);
+               seq_printf(m, "time_since_%s: N/A\n", name);
+       } else {
+               seq_printf(m, "%s_time: %llu\n", name, time);
+               seq_printf(m, "time_since_%s: %llu seconds\n",
+                          name, cfs_time_current_sec() - time);
+       }
 }
 
-int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
-                  const char *prefix)
+void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
+                   const char *prefix)
 {
        if (fid_is_zero(&pos->lp_dir_parent)) {
-               if (pos->lp_oit_cookie == 0)
-                       seq_printf(m, "%s: N/A, N/A, N/A\n",
-                                  prefix);
-               else
-                       seq_printf(m, "%s: "LPU64", N/A, N/A\n",
-                                  prefix, pos->lp_oit_cookie);
+               if (pos->lp_oit_cookie == 0) {
+                       seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
+                       return;
+               }
+               seq_printf(m, "%s: %llu, N/A, N/A\n",
+                          prefix, pos->lp_oit_cookie);
        } else {
-               seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
+               seq_printf(m, "%s: %llu, "DFID", %#llx\n",
                           prefix, pos->lp_oit_cookie,
                           PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
        }
-       return 0;
 }
 
 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
@@ -1837,6 +1963,56 @@ lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
        return lad;
 }
 
+struct lfsck_assistant_object *
+lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
+                           const struct lu_attr *attr, __u64 cookie,
+                           bool is_dir)
+{
+       struct lfsck_assistant_object   *lso;
+
+       OBD_ALLOC_PTR(lso);
+       if (lso == NULL)
+               return ERR_PTR(-ENOMEM);
+
+       lso->lso_fid = *fid;
+       if (attr != NULL)
+               lso->lso_attr = *attr;
+
+       atomic_set(&lso->lso_ref, 1);
+       lso->lso_oit_cookie = cookie;
+       if (is_dir)
+               lso->lso_is_dir = 1;
+
+       return lso;
+}
+
+struct dt_object *
+lfsck_assistant_object_load(const struct lu_env *env,
+                           struct lfsck_instance *lfsck,
+                           struct lfsck_assistant_object *lso)
+{
+       struct dt_object *obj;
+
+       obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
+       if (IS_ERR(obj))
+               return obj;
+
+       if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
+               lso->lso_dead = 1;
+               lfsck_object_put(env, obj);
+
+               return ERR_PTR(-ENOENT);
+       }
+
+       if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
+               lfsck_object_put(env, obj);
+
+               return ERR_PTR(-ENOTDIR);
+       }
+
+       return obj;
+}
+
 /**
  * Generic LFSCK asynchronous communication interpretor function.
  * The LFSCK RPC reply for both the event notification and status
@@ -1964,6 +2140,11 @@ int lfsck_async_interpret_common(const struct lu_env *env,
                }
 
                if (rc != 0) {
+                       if (lr->lr_flags & LEF_QUERY_ALL) {
+                               lfsck_reset_ltd_status(ltd, com->lc_type);
+                               break;
+                       }
+
                        spin_lock(&ltds->ltd_lock);
                        list_del_init(phase_list);
                        list_del_init(list);
@@ -1978,6 +2159,12 @@ int lfsck_async_interpret_common(const struct lu_env *env,
                        CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
                               "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
                               lad->lad_name, rc);
+
+                       if (lr->lr_flags & LEF_QUERY_ALL) {
+                               lfsck_reset_ltd_status(ltd, com->lc_type);
+                               break;
+                       }
+
                        spin_lock(&ltds->ltd_lock);
                        list_del_init(phase_list);
                        list_del_init(list);
@@ -1985,6 +2172,18 @@ int lfsck_async_interpret_common(const struct lu_env *env,
                        break;
                }
 
+               if (lr->lr_flags & LEF_QUERY_ALL) {
+                       if (com->lc_type == LFSCK_TYPE_LAYOUT) {
+                               ltd->ltd_layout_status = reply->lr_status;
+                               ltd->ltd_layout_repaired = reply->lr_repaired;
+                       } else {
+                               ltd->ltd_namespace_status = reply->lr_status;
+                               ltd->ltd_namespace_repaired =
+                                                       reply->lr_repaired;
+                       }
+                       break;
+               }
+
                switch (reply->lr_status) {
                case LS_SCANNING_PHASE1:
                        break;
@@ -2130,12 +2329,12 @@ static int lfsck_stop_notify(const struct lu_env *env,
                if (ltds == &lfsck->li_ost_descs)
                        lr->lr_flags = LEF_TO_OST;
 
+               memset(laia, 0, sizeof(*laia));
                laia->laia_com = com;
                laia->laia_ltds = ltds;
                atomic_inc(&ltd->ltd_ref);
                laia->laia_ltd = ltd;
                laia->laia_lr = lr;
-               laia->laia_shared = 0;
 
                rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
                                         lfsck_async_interpret_common,
@@ -2220,11 +2419,76 @@ int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
        if (laia->laia_com != NULL)
                lfsck_component_get(laia->laia_com);
        req->rq_interpret_reply = interpreter;
+       req->rq_allow_intr = 1;
        ptlrpc_set_add_req(set, req);
 
        return 0;
 }
 
+int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
+{
+       struct lfsck_thread_info          *info  = lfsck_env_info(env);
+       struct lfsck_request              *lr    = &info->lti_lr;
+       struct lfsck_async_interpret_args *laia  = &info->lti_laia;
+       struct lfsck_instance             *lfsck = com->lc_lfsck;
+       struct lfsck_tgt_descs            *ltds  = &lfsck->li_mdt_descs;
+       struct lfsck_tgt_desc             *ltd;
+       struct ptlrpc_request_set         *set;
+       int                                idx;
+       int                                rc;
+       ENTRY;
+
+       memset(lr, 0, sizeof(*lr));
+       lr->lr_event = LE_QUERY;
+       lr->lr_active = com->lc_type;
+       lr->lr_flags = LEF_QUERY_ALL;
+
+       memset(laia, 0, sizeof(*laia));
+       laia->laia_com = com;
+       laia->laia_lr = lr;
+
+       set = ptlrpc_prep_set();
+       if (set == NULL)
+               RETURN(-ENOMEM);
+
+again:
+       laia->laia_ltds = ltds;
+       down_read(&ltds->ltd_rw_sem);
+       cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
+               ltd = lfsck_tgt_get(ltds, idx);
+               LASSERT(ltd != NULL);
+
+               laia->laia_ltd = ltd;
+               up_read(&ltds->ltd_rw_sem);
+               rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
+                                        lfsck_async_interpret_common,
+                                        laia, LFSCK_QUERY);
+               if (rc != 0) {
+                       struct lfsck_assistant_data *lad = com->lc_data;
+
+                       CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
+                              "rc = %d\n", lfsck_lfsck2name(lfsck),
+                              (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
+                              ltd->ltd_index, lad->lad_name, rc);
+                       lfsck_reset_ltd_status(ltd, com->lc_type);
+                       lfsck_tgt_put(ltd);
+               }
+               down_read(&ltds->ltd_rw_sem);
+       }
+       up_read(&ltds->ltd_rw_sem);
+
+       if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
+               ltds = &lfsck->li_ost_descs;
+               lr->lr_flags |= LEF_TO_OST;
+               goto again;
+       }
+
+       rc = ptlrpc_set_wait(set);
+       ptlrpc_set_destroy(set);
+
+       RETURN(rc);
+}
+
 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
                          struct lfsck_start_param *lsp)
 {
@@ -2243,6 +2507,7 @@ int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
        lad->lad_to_double_scan = 0;
        lad->lad_in_double_scan = 0;
        lad->lad_exit = 0;
+       lad->lad_advance_lock = false;
        thread_set_flags(athread, 0);
 
        lta = lfsck_thread_args_init(lfsck, com, lsp);
@@ -2279,9 +2544,6 @@ int lfsck_checkpoint_generic(const struct lu_env *env,
        struct ptlrpc_thread            *athread = &lad->lad_thread;
        struct l_wait_info               lwi     = { 0 };
 
-       if (com->lc_new_checked == 0)
-               return LFSCK_CHECKPOINT_SKIP;
-
        l_wait_event(mthread->t_ctl_waitq,
                     list_empty(&lad->lad_req_list) ||
                     !thread_is_running(mthread) ||
@@ -2307,6 +2569,9 @@ void lfsck_post_generic(const struct lu_env *env,
                lad->lad_exit = 1;
        lad->lad_to_post = 1;
 
+       CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
+              lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
+
        wake_up_all(&athread->t_ctl_waitq);
        l_wait_event(mthread->t_ctl_waitq,
                     (*result > 0 && list_empty(&lad->lad_req_list)) ||
@@ -2315,6 +2580,9 @@ void lfsck_post_generic(const struct lu_env *env,
 
        if (lad->lad_assistant_status < 0)
                *result = lad->lad_assistant_status;
+
+       CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
+              lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
 }
 
 int lfsck_double_scan_generic(const struct lu_env *env,
@@ -2330,12 +2598,20 @@ int lfsck_double_scan_generic(const struct lu_env *env,
        else
                lad->lad_to_double_scan = 1;
 
+       CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
+              "status %d\n",
+              lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
+
        wake_up_all(&athread->t_ctl_waitq);
        l_wait_event(mthread->t_ctl_waitq,
                     lad->lad_in_double_scan ||
                     thread_is_stopped(athread),
                     &lwi);
 
+       CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
+              "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
+              lad->lad_assistant_status);
+
        if (lad->lad_assistant_status < 0)
                return lad->lad_assistant_status;
 
@@ -2385,7 +2661,7 @@ int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
 }
 EXPORT_SYMBOL(lfsck_get_speed);
 
-int lfsck_set_speed(struct dt_device *key, int val)
+int lfsck_set_speed(struct dt_device *key, __u32 val)
 {
        struct lu_env           env;
        struct lfsck_instance  *lfsck;
@@ -2490,7 +2766,7 @@ int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
        if (likely(lfsck != NULL)) {
                com = lfsck_component_find(lfsck, type);
                if (likely(com != NULL)) {
-                       rc = com->lc_ops->lfsck_dump(&env, com, m);
+                       com->lc_ops->lfsck_dump(&env, com, m);
                        lfsck_component_put(&env, com);
                } else {
                        rc = -ENOTSUPP;
@@ -2537,10 +2813,9 @@ static int lfsck_stop_all(const struct lu_env *env,
        lr->lr_active = LFSCK_TYPES_ALL;
        lr->lr_param = stop->ls_flags;
 
-       laia->laia_com = NULL;
+       memset(laia, 0, sizeof(*laia));
        laia->laia_ltds = ltds;
        laia->laia_lr = lr;
-       laia->laia_result = 0;
        laia->laia_shared = 1;
 
        down_read(&ltds->ltd_rw_sem);
@@ -2611,10 +2886,9 @@ static int lfsck_start_all(const struct lu_env *env,
                       LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
                       LSV_CREATE_MDTOBJ;
 
-       laia->laia_com = NULL;
+       memset(laia, 0, sizeof(*laia));
        laia->laia_ltds = ltds;
        laia->laia_lr = lr;
-       laia->laia_result = 0;
        laia->laia_shared = 1;
 
        down_read(&ltds->ltd_rw_sem);
@@ -2679,6 +2953,9 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key,
        struct l_wait_info               lwi    = { 0 };
        struct lfsck_thread_args        *lta;
        struct task_struct              *task;
+       struct lfsck_tgt_descs          *ltds;
+       struct lfsck_tgt_desc           *ltd;
+       __u32                            idx;
        int                              rc     = 0;
        __u16                            valid  = 0;
        __u16                            flags  = 0;
@@ -2825,6 +3102,38 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key,
                }
        }
 
+       ltds = &lfsck->li_mdt_descs;
+       down_read(&ltds->ltd_rw_sem);
+       cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
+               ltd = lfsck_ltd2tgt(ltds, idx);
+               LASSERT(ltd != NULL);
+
+               ltd->ltd_layout_done = 0;
+               ltd->ltd_namespace_done = 0;
+               ltd->ltd_synced_failures = 0;
+               lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
+               lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
+               list_del_init(&ltd->ltd_layout_phase_list);
+               list_del_init(&ltd->ltd_layout_list);
+               list_del_init(&ltd->ltd_namespace_phase_list);
+               list_del_init(&ltd->ltd_namespace_list);
+       }
+       up_read(&ltds->ltd_rw_sem);
+
+       ltds = &lfsck->li_ost_descs;
+       down_read(&ltds->ltd_rw_sem);
+       cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
+               ltd = lfsck_ltd2tgt(ltds, idx);
+               LASSERT(ltd != NULL);
+
+               ltd->ltd_layout_done = 0;
+               ltd->ltd_synced_failures = 0;
+               lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
+               list_del_init(&ltd->ltd_layout_phase_list);
+               list_del_init(&ltd->ltd_layout_list);
+       }
+       up_read(&ltds->ltd_rw_sem);
+
 trigger:
        lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
        if (bk->lb_param & LPF_DRYRUN)
@@ -2953,6 +3262,28 @@ int lfsck_stop(const struct lu_env *env, struct dt_device *key,
        }
 
        thread_set_flags(thread, SVC_STOPPING);
+
+       if (lfsck->li_master) {
+               struct lfsck_component *com;
+               struct lfsck_assistant_data *lad;
+
+               list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
+                       lad = com->lc_data;
+                       spin_lock(&lad->lad_lock);
+                       if (lad->lad_task != NULL)
+                               force_sig(SIGINT, lad->lad_task);
+                       spin_unlock(&lad->lad_lock);
+               }
+
+               list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
+                       lad = com->lc_data;
+                       spin_lock(&lad->lad_lock);
+                       if (lad->lad_task != NULL)
+                               force_sig(SIGINT, lad->lad_task);
+                       spin_unlock(&lad->lad_lock);
+               }
+       }
+
        spin_unlock(&lfsck->li_lock);
 
        wake_up_all(&thread->t_ctl_waitq);
@@ -3039,29 +3370,97 @@ int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
 EXPORT_SYMBOL(lfsck_in_notify);
 
 int lfsck_query(const struct lu_env *env, struct dt_device *key,
-               struct lfsck_request *lr)
+               struct lfsck_request *req, struct lfsck_reply *rep,
+               struct lfsck_query *que)
 {
        struct lfsck_instance  *lfsck;
        struct lfsck_component *com;
-       int                     rc;
+       int                     i;
+       int                     rc = 0;
+       __u16                   type;
        ENTRY;
 
        lfsck = lfsck_instance_find(key, true, false);
        if (unlikely(lfsck == NULL))
                RETURN(-ENXIO);
 
-       com = lfsck_component_find(lfsck, lr->lr_active);
-       if (likely(com != NULL)) {
-               rc = com->lc_ops->lfsck_query(env, com);
-               lfsck_component_put(env, com);
+       if (que != NULL) {
+               if (que->lu_types == LFSCK_TYPES_ALL)
+                       que->lu_types =
+                               LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
+
+               if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
+                       que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
+
+                       GOTO(out, rc = -ENOTSUPP);
+               }
+
+               for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
+                    i++, type = 1 << i) {
+                       if (!(que->lu_types & type))
+                               continue;
+
+again:
+                       com = lfsck_component_find(lfsck, type);
+                       if (unlikely(com == NULL))
+                               GOTO(out, rc = -ENOTSUPP);
+
+                       memset(que->lu_mdts_count[i], 0,
+                              sizeof(__u32) * (LS_MAX + 1));
+                       memset(que->lu_osts_count[i], 0,
+                              sizeof(__u32) * (LS_MAX + 1));
+                       que->lu_repaired[i] = 0;
+                       rc = com->lc_ops->lfsck_query(env, com, req, rep,
+                                                     que, i);
+                       lfsck_component_put(env, com);
+                       if  (rc < 0)
+                               GOTO(out, rc);
+               }
+
+               if (!(que->lu_flags & LPF_WAIT))
+                       GOTO(out, rc);
+
+               for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
+                    i++, type = 1 << i) {
+                       if (!(que->lu_types & type))
+                               continue;
+
+                       if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
+                           que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
+                           que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
+                           que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
+                               struct l_wait_info lwi;
+
+                               /* If it is required to wait, then sleep
+                                * 3 seconds and try to query again. */
+                               lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
+                                                      NULL,
+                                                      LWI_ON_SIGNAL_NOOP,
+                                                      NULL);
+                               rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
+                                                 0, &lwi);
+                               if (rc == -ETIMEDOUT)
+                                       goto again;
+                       }
+               }
        } else {
-               rc = -ENOTSUPP;
+               com = lfsck_component_find(lfsck, req->lr_active);
+               if (likely(com != NULL)) {
+                       rc = com->lc_ops->lfsck_query(env, com, req, rep,
+                                                     que, -1);
+                       lfsck_component_put(env, com);
+               } else {
+                       rc = -ENOTSUPP;
+               }
        }
 
-       lfsck_instance_put(env, lfsck);
+       GOTO(out, rc);
 
-       RETURN(rc);
+out:
+       lfsck_instance_put(env, lfsck);
+       return rc;
 }
+EXPORT_SYMBOL(lfsck_query);
 
 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
                             struct ldlm_namespace *ns)
@@ -3151,7 +3550,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
 
                        rc = dt_lookup(env, root,
                                (struct dt_rec *)(&lfsck->li_global_root_fid),
-                               (const struct dt_key *)"ROOT", BYPASS_CAPA);
+                               (const struct dt_key *)"ROOT");
                        if (rc != 0)
                                GOTO(out, rc);
 
@@ -3163,7 +3562,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
                                GOTO(out, rc = -ENOTDIR);
 
                        rc = dt_lookup(env, obj, (struct dt_rec *)fid,
-                               (const struct dt_key *)dotlustre, BYPASS_CAPA);
+                               (const struct dt_key *)dotlustre);
                        if (rc != 0)
                                GOTO(out, rc);
 
@@ -3184,8 +3583,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
 
                        *pfid = *fid;
                        rc = dt_lookup(env, obj, (struct dt_rec *)fid,
-                                      (const struct dt_key *)lostfound,
-                                      BYPASS_CAPA);
+                                      (const struct dt_key *)lostfound);
                        if (rc != 0)
                                GOTO(out, rc);
 
@@ -3358,7 +3756,7 @@ void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
        if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
                goto unlock;
 
-       ltd = LTD_TGT(ltds, index);
+       ltd = lfsck_ltd2tgt(ltds, index);
        if (unlikely(ltd == NULL))
                goto unlock;
 
@@ -3366,7 +3764,7 @@ void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
 
        ltds->ltd_tgtnr--;
        cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
-       LTD_TGT(ltds, index) = NULL;
+       lfsck_assign_tgt(ltds, NULL, index);
 
 unlock:
        if (ltd == NULL) {
@@ -3436,8 +3834,10 @@ static void __exit lfsck_exit(void)
        lu_context_key_degister(&lfsck_thread_key);
 }
 
-MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
-MODULE_DESCRIPTION("LFSCK");
+MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
+MODULE_DESCRIPTION("Lustre File System Checker");
+MODULE_VERSION(LUSTRE_VERSION_STRING);
 MODULE_LICENSE("GPL");
 
-cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);
+module_init(lfsck_init);
+module_exit(lfsck_exit);