Whamcloud - gitweb
LU-15886 lfsck: remove unreasonable assertions
[fs/lustre-release.git] / lustre / lfsck / lfsck_lib.c
index 9d9165f..7e36a18 100644 (file)
@@ -20,7 +20,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright (c) 2013, 2016, Intel Corporation.
+ * Copyright (c) 2013, 2017, Intel Corporation.
  */
 /*
  * lustre/lfsck/lfsck_lib.c
@@ -33,6 +33,7 @@
 #include <linux/kthread.h>
 #include <linux/sched.h>
 #include <linux/list.h>
+#include <linux/delay.h>
 #include <lu_object.h>
 #include <dt_object.h>
 #include <md_object.h>
@@ -62,12 +63,12 @@ static void lfsck_key_fini(const struct lu_context *ctx,
 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
 LU_KEY_INIT_GENERIC(lfsck);
 
-static struct list_head lfsck_instance_list;
-static struct list_head lfsck_ost_orphan_list;
-static struct list_head lfsck_mdt_orphan_list;
+static LIST_HEAD(lfsck_instance_list);
+static LIST_HEAD(lfsck_ost_orphan_list);
+static LIST_HEAD(lfsck_mdt_orphan_list);
 static DEFINE_SPINLOCK(lfsck_instance_lock);
 
-const char *lfsck_flags_names[] = {
+const char *const lfsck_flags_names[] = {
        "scanned-once",
        "inconsistent",
        "upgrade",
@@ -76,7 +77,7 @@ const char *lfsck_flags_names[] = {
        NULL
 };
 
-const char *lfsck_param_names[] = {
+const char *const lfsck_param_names[] = {
        NULL,
        "failout",
        "dryrun",
@@ -157,7 +158,7 @@ static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
        LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
                 ltds->ltd_tgtnr);
 
-       for (idx = 0; idx < TGT_PTRS; idx++) {
+       for (idx = 0; idx < ARRAY_SIZE(ltds->ltd_tgts_idx); idx++) {
                if (ltds->ltd_tgts_idx[idx] != NULL) {
                        OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
                        ltds->ltd_tgts_idx[idx] = NULL;
@@ -377,10 +378,17 @@ static int __lfsck_ibits_lock(const struct lu_env *env,
                einfo->ei_cb_bl = ldlm_blocking_ast;
                einfo->ei_cb_cp = ldlm_completion_ast;
                einfo->ei_res_id = resid;
+               einfo->ei_req_slot = 1;
 
                rc = dt_object_lock(env, obj, lh, einfo, policy);
+               /* for regular checks LFSCK doesn't use LDLM locking,
+                * so the state isn't coherent. here we just took LDLM
+                * lock for coherency and it's time to invalidate
+                * previous state */
+               if (rc == ELDLM_OK)
+                       dt_invalidate(env, obj);
        } else {
-               rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
+               rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
                                            LDLM_IBITS, policy, mode,
                                            &flags, ldlm_blocking_ast,
                                            ldlm_completion_ast, NULL, NULL,
@@ -426,6 +434,36 @@ int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
 }
 
 /**
+ * Request the remote LOOKUP lock for the given object.
+ *
+ * If \a pobj is remote, the LOOKUP lock of \a obj is on the MDT where
+ * \a pobj is, acquire LOOKUP lock there.
+ *
+ * \param[in] env      pointer to the thread context
+ * \param[in] lfsck    pointer to the lfsck instance
+ * \param[in] pobj     pointer to parent dt_object
+ * \param[in] obj      pointer to the dt_object to be locked
+ * \param[out] lh      pointer to the lock handle
+ * \param[in] mode     the mode for the ldlm lock to be acquired
+ *
+ * \retval             0 for success
+ * \retval             negative error number on failure
+ */
+int lfsck_remote_lookup_lock(const struct lu_env *env,
+                            struct lfsck_instance *lfsck,
+                            struct dt_object *pobj, struct dt_object *obj,
+                            struct lustre_handle *lh, 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, pobj, resid, lh,
+                                 MDS_INODELOCK_LOOKUP, mode);
+}
+
+/**
  * Release the the specified ibits lock.
  *
  * If the lock has been acquired before, release it
@@ -584,7 +622,7 @@ static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
        if (rc != 0)
                RETURN(rc);
 
-       th = dt_trans_create(env, dev);
+       th = lfsck_trans_create(env, dev, lfsck);
        if (IS_ERR(th))
                GOTO(unlock, rc = PTR_ERR(th));
 
@@ -650,7 +688,7 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
        if (rc != 0)
                RETURN(rc);
 
-       th = dt_trans_create(env, dev);
+       th = lfsck_trans_create(env, dev, lfsck);
        if (IS_ERR(th))
                RETURN(PTR_ERR(th));
 
@@ -727,14 +765,14 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
        /* 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, 1);
+                      (const struct dt_key *)dot, th);
        if (rc != 0)
                GOTO(unlock, rc);
 
        /* 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, 1);
+                      (const struct dt_key *)dotdot, th);
        if (rc != 0)
                GOTO(unlock, rc);
 
@@ -748,7 +786,7 @@ static int lfsck_create_lpf_local(const struct lu_env *env,
        /* 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, 1);
+                      (const struct dt_key *)name, th);
        if (rc != 0)
                GOTO(stop, rc);
 
@@ -828,7 +866,7 @@ static int lfsck_create_lpf_remote(const struct lu_env *env,
        /* Transaction I: locally */
 
        dev = lfsck_obj2dev(child);
-       th = dt_trans_create(env, dev);
+       th = lfsck_trans_create(env, dev, lfsck);
        if (IS_ERR(th))
                RETURN(PTR_ERR(th));
 
@@ -893,14 +931,14 @@ static int lfsck_create_lpf_remote(const struct lu_env *env,
        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, 1);
+                      (const struct dt_key *)dot, th);
        if (rc != 0)
                GOTO(unlock, rc);
 
        /* 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, 1);
+                      (const struct dt_key *)dotdot, th);
        if (rc != 0)
                GOTO(unlock, rc);
 
@@ -925,7 +963,7 @@ static int lfsck_create_lpf_remote(const struct lu_env *env,
        /* Transaction II: remotely */
 
        dev = lfsck_obj2dev(parent);
-       th = dt_trans_create(env, dev);
+       th = lfsck_trans_create(env, dev, lfsck);
        if (IS_ERR(th))
                RETURN(PTR_ERR(th));
 
@@ -948,7 +986,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, 1);
+                      (const struct dt_key *)name, th);
        if (rc != 0)
                GOTO(stop, rc);
 
@@ -1019,8 +1057,7 @@ static int lfsck_create_lpf(const struct lu_env *env,
                 * 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);
+               rc = dt_lookup_dir(env, parent, name, cfid);
                if (rc != 0 && rc != -ENOENT)
                        GOTO(unlock, rc);
 
@@ -1053,7 +1090,7 @@ static int lfsck_create_lpf(const struct lu_env *env,
        la->la_atime = la->la_mtime = la->la_ctime = ktime_get_real_seconds();
        la->la_mode = S_IFDIR | S_IRWXU;
        la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
-                      LA_UID | LA_GID;
+                      LA_UID | LA_GID | LA_TYPE;
        memset(dof, 0, sizeof(*dof));
        dof->dof_type = dt_mode_to_dft(S_IFDIR);
 
@@ -1228,8 +1265,7 @@ static int lfsck_verify_lpf_pairs(const struct lu_env *env,
        ENTRY;
 
        fid_zero(fid);
-       rc = dt_lookup(env, child, (struct dt_rec *)fid,
-                      (const struct dt_key *)dotdot);
+       rc = dt_lookup_dir(env, child, dotdot, fid);
        if (rc != 0)
                GOTO(linkea, rc);
 
@@ -1245,7 +1281,7 @@ static int lfsck_verify_lpf_pairs(const struct lu_env *env,
                }
 
                cname = lfsck_name_get_const(env, name, strlen(name));
-               rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
+               rc = lfsck_verify_linkea(env, lfsck, child, cname, &LU_LPF_FID);
                if (rc == 0)
                        rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
                                                    name, type);
@@ -1314,8 +1350,7 @@ linkea:
                GOTO(out_done, rc = 1);
        }
 
-       rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
-                      (const struct dt_key *)name2);
+       rc = dt_lookup_dir(env, parent2, name2, fid);
        dt_read_unlock(env, child);
        lfsck_ibits_unlock(&lh, LCK_PR);
        if (rc != 0 && rc != -ENOENT)
@@ -1389,6 +1424,9 @@ int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
 
        LASSERT(lfsck->li_master);
 
+       if (lfsck_is_dryrun(lfsck))
+               RETURN(0);
+
        if (lfsck->li_lpf_root_obj != NULL)
                RETURN(0);
 
@@ -1429,8 +1467,7 @@ int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
 
        /* child2 */
        snprintf(name, 8, "MDT%04x", node);
-       rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
-                      (const struct dt_key *)name);
+       rc = dt_lookup_dir(env, parent, name, cfid);
        if (rc == -ENOENT) {
                rc = 0;
                goto find_child1;
@@ -1462,11 +1499,8 @@ int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
                goto find_child1;
        }
 
-       if (unlikely(!dt_try_as_dir(env, child2))) {
-               lfsck_object_put(env, child2);
-               child2 = NULL;
-               rc = -ENOTDIR;
-       }
+       if (unlikely(!dt_try_as_dir(env, child2)))
+               GOTO(put, rc = -ENOTDIR);
 
 find_child1:
        if (fid_is_zero(&bk->lb_lpf_fid))
@@ -1479,7 +1513,8 @@ find_child1:
                }
 
                cname = lfsck_name_get_const(env, name, strlen(name));
-               rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
+               rc = lfsck_verify_linkea(env, lfsck, child2, cname,
+                                        &LU_LPF_FID);
 
                GOTO(put, rc);
        }
@@ -1585,11 +1620,9 @@ static int lfsck_fid_init(struct lfsck_instance *lfsck)
                GOTO(out, rc = -ENOMEM);
 
        snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
-       rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
+       seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
                             ss->ss_server_seq);
        OBD_FREE(prefix, MAX_OBD_NAME + 7);
-       if (rc != 0)
-               GOTO(out, rc);
 
        if (fid_is_sane(&bk->lb_last_fid))
                lfsck->li_seq->lcs_fid = bk->lb_last_fid;
@@ -1631,9 +1664,6 @@ void lfsck_instance_cleanup(const struct lu_env *env,
                lfsck->li_obj_oit = NULL;
        }
 
-       LASSERT(lfsck->li_obj_dir == NULL);
-       LASSERT(lfsck->li_lmv == NULL);
-
        list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
                llmv = &llu->llu_lmv;
 
@@ -1740,7 +1770,7 @@ static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
        return 0;
 }
 
-void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
+void lfsck_bits_dump(struct seq_file *m, int bits, const char *const names[],
                     const char *prefix)
 {
        int flag;
@@ -1749,7 +1779,7 @@ void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
 
        seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
 
-       for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
+       for (i = 0, flag = 1; bits != 0; i++, flag = BIT(i)) {
                if (flag & bits) {
                        bits &= ~flag;
                        if (names[i] != NULL) {
@@ -1809,8 +1839,10 @@ void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
        if (!lfsck->li_current_oit_processed && !init)
                pos->lp_oit_cookie--;
 
-       LASSERT(pos->lp_oit_cookie > 0);
+       if (unlikely(pos->lp_oit_cookie == 0))
+               pos->lp_oit_cookie = 1;
 
+       spin_lock(&lfsck->li_lock);
        if (lfsck->li_di_dir != NULL) {
                struct dt_object *dto = lfsck->li_obj_dir;
 
@@ -1827,6 +1859,7 @@ void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
                fid_zero(&pos->lp_dir_parent);
                pos->lp_dir_cookie = 0;
        }
+       spin_unlock(&lfsck->li_lock);
 }
 
 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
@@ -1834,14 +1867,12 @@ bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
        bool dirty = false;
 
        if (limit != LFSCK_SPEED_NO_LIMIT) {
-               if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
-                       lfsck->li_sleep_rate = limit /
-                                              msecs_to_jiffies(MSEC_PER_SEC);
+               if (limit > cfs_time_seconds(1)) {
+                       lfsck->li_sleep_rate = limit / cfs_time_seconds(1);
                        lfsck->li_sleep_jif = 1;
                } else {
                        lfsck->li_sleep_rate = 1;
-                       lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
-                                             limit;
+                       lfsck->li_sleep_jif = cfs_time_seconds(1) / limit;
                }
        } else {
                lfsck->li_sleep_jif = 0;
@@ -1859,16 +1890,12 @@ bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
 void lfsck_control_speed(struct lfsck_instance *lfsck)
 {
        struct ptlrpc_thread *thread = &lfsck->li_thread;
-       struct l_wait_info    lwi;
 
        if (lfsck->li_sleep_jif > 0 &&
            lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
-               lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
-                                      LWI_ON_SIGNAL_NOOP, NULL);
-
-               l_wait_event(thread->t_ctl_waitq,
-                            !thread_is_running(thread),
-                            &lwi);
+               wait_event_idle_timeout(thread->t_ctl_waitq,
+                                       !thread_is_running(thread),
+                                       lfsck->li_sleep_jif);
                lfsck->li_new_scanned = 0;
        }
 }
@@ -1877,16 +1904,12 @@ void lfsck_control_speed_by_self(struct lfsck_component *com)
 {
        struct lfsck_instance   *lfsck  = com->lc_lfsck;
        struct ptlrpc_thread    *thread = &lfsck->li_thread;
-       struct l_wait_info       lwi;
 
        if (lfsck->li_sleep_jif > 0 &&
            com->lc_new_scanned >= lfsck->li_sleep_rate) {
-               lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
-                                      LWI_ON_SIGNAL_NOOP, NULL);
-
-               l_wait_event(thread->t_ctl_waitq,
-                            !thread_is_running(thread),
-                            &lwi);
+               wait_event_idle_timeout(thread->t_ctl_waitq,
+                                       !thread_is_running(thread),
+                                       lfsck->li_sleep_jif);
                com->lc_new_scanned = 0;
        }
 }
@@ -1928,7 +1951,7 @@ void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
 }
 
 struct lfsck_assistant_data *
-lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
+lfsck_assistant_data_init(const struct lfsck_assistant_operations *lao,
                          const char *name)
 {
        struct lfsck_assistant_data *lad;
@@ -2346,7 +2369,7 @@ static int lfsck_stop_notify(const struct lu_env *env,
                               ltd->ltd_index, lad->lad_name, rc);
                        lfsck_tgt_put(ltd);
                } else {
-                       rc = ptlrpc_set_wait(set);
+                       rc = ptlrpc_set_wait(env, set);
                }
 
                ptlrpc_set_destroy(set);
@@ -2363,8 +2386,8 @@ static int lfsck_async_interpret(const struct lu_env *env,
        struct lfsck_async_interpret_args *laia = args;
        struct lfsck_instance             *lfsck;
 
-       lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
-                             li_mdt_descs);
+       lfsck = container_of(laia->laia_ltds, struct lfsck_instance,
+                            li_mdt_descs);
        lfsck_interpret(env, lfsck, req, laia, rc);
        lfsck_tgt_put(laia->laia_ltd);
        if (rc != 0 && laia->laia_result != -EALREADY)
@@ -2413,12 +2436,13 @@ int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
        *tmp = *lr;
        ptlrpc_request_set_replen(req);
 
-       laia = ptlrpc_req_async_args(req);
+       laia = ptlrpc_req_async_args(laia, req);
        *laia = *(struct lfsck_async_interpret_args *)args;
        if (laia->laia_com != NULL)
                lfsck_component_get(laia->laia_com);
        req->rq_interpret_reply = interpreter;
        req->rq_allow_intr = 1;
+       req->rq_no_delay = 1;
        ptlrpc_set_add_req(set, req);
 
        return 0;
@@ -2482,7 +2506,7 @@ again:
                goto again;
        }
 
-       rc = ptlrpc_set_wait(set);
+       rc = ptlrpc_set_wait(env, set);
        ptlrpc_set_destroy(set);
 
        RETURN(rc);
@@ -2502,10 +2526,7 @@ int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
 
        lad->lad_assistant_status = 0;
        lad->lad_post_result = 0;
-       lad->lad_to_post = 0;
-       lad->lad_to_double_scan = 0;
-       lad->lad_in_double_scan = 0;
-       lad->lad_exit = 0;
+       lad->lad_flags = 0;
        lad->lad_advance_lock = false;
        thread_set_flags(athread, 0);
 
@@ -2520,13 +2541,10 @@ int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
                       "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
                lfsck_thread_args_fini(lta);
        } else {
-               struct l_wait_info lwi = { 0 };
-
-               l_wait_event(mthread->t_ctl_waitq,
-                            thread_is_running(athread) ||
-                            thread_is_stopped(athread) ||
-                            !thread_is_starting(mthread),
-                            &lwi);
+               wait_event_idle(mthread->t_ctl_waitq,
+                               thread_is_running(athread) ||
+                               thread_is_stopped(athread) ||
+                               !thread_is_starting(mthread));
                if (unlikely(!thread_is_starting(mthread)))
                        /* stopped by race */
                        rc = -ESRCH;
@@ -2545,13 +2563,11 @@ int lfsck_checkpoint_generic(const struct lu_env *env,
        struct lfsck_assistant_data     *lad     = com->lc_data;
        struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
        struct ptlrpc_thread            *athread = &lad->lad_thread;
-       struct l_wait_info               lwi     = { 0 };
 
-       l_wait_event(mthread->t_ctl_waitq,
-                    list_empty(&lad->lad_req_list) ||
-                    !thread_is_running(mthread) ||
-                    thread_is_stopped(athread),
-                    &lwi);
+       wait_event_idle(mthread->t_ctl_waitq,
+                       list_empty(&lad->lad_req_list) ||
+                       !thread_is_running(mthread) ||
+                       thread_is_stopped(athread));
 
        if (!thread_is_running(mthread) || thread_is_stopped(athread))
                return LFSCK_CHECKPOINT_SKIP;
@@ -2565,21 +2581,19 @@ void lfsck_post_generic(const struct lu_env *env,
        struct lfsck_assistant_data     *lad     = com->lc_data;
        struct ptlrpc_thread            *athread = &lad->lad_thread;
        struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
-       struct l_wait_info               lwi     = { 0 };
 
        lad->lad_post_result = *result;
        if (*result <= 0)
-               lad->lad_exit = 1;
-       lad->lad_to_post = 1;
+               set_bit(LAD_EXIT, &lad->lad_flags);
+       set_bit(LAD_TO_POST, &lad->lad_flags);
 
        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)) ||
-                    thread_is_stopped(athread),
-                    &lwi);
+       wake_up(&athread->t_ctl_waitq);
+       wait_event_idle(mthread->t_ctl_waitq,
+                       (*result > 0 && list_empty(&lad->lad_req_list)) ||
+                       thread_is_stopped(athread));
 
        if (lad->lad_assistant_status < 0)
                *result = lad->lad_assistant_status;
@@ -2594,22 +2608,20 @@ int lfsck_double_scan_generic(const struct lu_env *env,
        struct lfsck_assistant_data     *lad     = com->lc_data;
        struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
        struct ptlrpc_thread            *athread = &lad->lad_thread;
-       struct l_wait_info               lwi     = { 0 };
 
        if (status != LS_SCANNING_PHASE2)
-               lad->lad_exit = 1;
+               set_bit(LAD_EXIT, &lad->lad_flags);
        else
-               lad->lad_to_double_scan = 1;
+               set_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags);
 
        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);
+       wake_up(&athread->t_ctl_waitq);
+       wait_event_idle(mthread->t_ctl_waitq,
+                       test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags) ||
+                       thread_is_stopped(athread));
 
        CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
               "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
@@ -2627,14 +2639,12 @@ void lfsck_quit_generic(const struct lu_env *env,
        struct lfsck_assistant_data     *lad     = com->lc_data;
        struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
        struct ptlrpc_thread            *athread = &lad->lad_thread;
-       struct l_wait_info               lwi     = { 0 };
 
-       lad->lad_exit = 1;
-       wake_up_all(&athread->t_ctl_waitq);
-       l_wait_event(mthread->t_ctl_waitq,
-                    thread_is_init(athread) ||
-                    thread_is_stopped(athread),
-                    &lwi);
+       set_bit(LAD_EXIT, &lad->lad_flags);
+       wake_up(&athread->t_ctl_waitq);
+       wait_event_idle(mthread->t_ctl_waitq,
+                       thread_is_init(athread) ||
+                       thread_is_stopped(athread));
 }
 
 int lfsck_load_one_trace_file(const struct lu_env *env,
@@ -2703,10 +2713,14 @@ unlink:
                RETURN(PTR_ERR(obj));
 
        rc = obj->do_ops->do_index_try(env, obj, ft);
-       if (rc)
+       if (rc) {
                lfsck_object_put(env, obj);
-       else
+               CDEBUG(D_LFSCK, "%s: LFSCK fail to load "
+                      "sub trace file %s: rc = %d\n",
+                      lfsck_lfsck2name(com->lc_lfsck), name, rc);
+       } else {
                *child = obj;
+       }
 
        RETURN(rc);
 }
@@ -2733,8 +2747,7 @@ int lfsck_load_sub_trace_files(const struct lu_env *env,
 }
 
 /* external interfaces */
-
-int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
+int lfsck_get_speed(char *buf, struct dt_device *key)
 {
        struct lu_env           env;
        struct lfsck_instance  *lfsck;
@@ -2746,8 +2759,9 @@ int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
                RETURN(rc);
 
        lfsck = lfsck_instance_find(key, true, false);
-       if (likely(lfsck != NULL)) {
-               seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
+       if (lfsck && buf) {
+               rc = sprintf(buf, "%u\n",
+                            lfsck->li_bookmark_ram.lb_speed_limit);
                lfsck_instance_put(&env, lfsck);
        } else {
                rc = -ENXIO;
@@ -2787,7 +2801,7 @@ int lfsck_set_speed(struct dt_device *key, __u32 val)
 }
 EXPORT_SYMBOL(lfsck_set_speed);
 
-int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
+int lfsck_get_windows(char *buf, struct dt_device *key)
 {
        struct lu_env           env;
        struct lfsck_instance  *lfsck;
@@ -2800,7 +2814,8 @@ int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
 
        lfsck = lfsck_instance_find(key, true, false);
        if (likely(lfsck != NULL)) {
-               seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
+               rc = sprintf(buf, "%u\n",
+                            lfsck->li_bookmark_ram.lb_async_windows);
                lfsck_instance_put(&env, lfsck);
        } else {
                rc = -ENXIO;
@@ -2812,7 +2827,7 @@ int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
 }
 EXPORT_SYMBOL(lfsck_get_windows);
 
-int lfsck_set_windows(struct dt_device *key, int val)
+int lfsck_set_windows(struct dt_device *key, unsigned int val)
 {
        struct lu_env           env;
        struct lfsck_instance  *lfsck;
@@ -2935,7 +2950,7 @@ static int lfsck_stop_all(const struct lu_env *env,
        }
        up_read(&ltds->ltd_rw_sem);
 
-       rc = ptlrpc_set_wait(set);
+       rc = ptlrpc_set_wait(env, set);
        ptlrpc_set_destroy(set);
 
        if (rc == 0)
@@ -3026,7 +3041,7 @@ again:
                RETURN(rc);
        }
 
-       rc = ptlrpc_set_wait(set);
+       rc = ptlrpc_set_wait(env, set);
        ptlrpc_set_destroy(set);
 
        if (rc == 0)
@@ -3034,8 +3049,7 @@ again:
 
        if (unlikely(rc == -EINPROGRESS)) {
                retry = true;
-               set_current_state(TASK_INTERRUPTIBLE);
-               schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC));
+               schedule_timeout_interruptible(cfs_time_seconds(1));
                set_current_state(TASK_RUNNING);
                if (!signal_pending(current) &&
                    thread_is_running(&lfsck->li_thread))
@@ -3068,7 +3082,6 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key,
        struct lfsck_bookmark           *bk;
        struct ptlrpc_thread            *thread;
        struct lfsck_component          *com;
-       struct l_wait_info               lwi    = { 0 };
        struct lfsck_thread_args        *lta;
        struct task_struct              *task;
        struct lfsck_tgt_descs          *ltds;
@@ -3087,6 +3100,9 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key,
        if (unlikely(lfsck == NULL))
                RETURN(-ENXIO);
 
+       if (unlikely(lfsck->li_stopping))
+               GOTO(put, rc = -ENXIO);
+
        /* System is not ready, try again later. */
        if (unlikely(lfsck->li_namespace == NULL ||
                     lfsck_dev_site(lfsck)->ss_server_fld == NULL))
@@ -3300,13 +3316,12 @@ trigger:
                GOTO(out, rc);
        }
 
-       l_wait_event(thread->t_ctl_waitq,
-                    thread_is_running(thread) ||
-                    thread_is_stopped(thread),
-                    &lwi);
+       wait_event_idle(thread->t_ctl_waitq,
+                       thread_is_running(thread) ||
+                       thread_is_stopped(thread));
        if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
                lfsck->li_start_unplug = 1;
-               wake_up_all(&thread->t_ctl_waitq);
+               wake_up(&thread->t_ctl_waitq);
 
                GOTO(out, rc = 0);
        }
@@ -3325,14 +3340,13 @@ trigger:
                        spin_unlock(&lfsck->li_lock);
 
                        lfsck->li_start_unplug = 1;
-                       wake_up_all(&thread->t_ctl_waitq);
-                       l_wait_event(thread->t_ctl_waitq,
-                                    thread_is_stopped(thread),
-                                    &lwi);
+                       wake_up(&thread->t_ctl_waitq);
+                       wait_event_idle(thread->t_ctl_waitq,
+                                       thread_is_stopped(thread));
                }
        } else {
                lfsck->li_start_unplug = 1;
-               wake_up_all(&thread->t_ctl_waitq);
+               wake_up(&thread->t_ctl_waitq);
        }
 
        GOTO(put, rc);
@@ -3352,7 +3366,6 @@ int lfsck_stop(const struct lu_env *env, struct dt_device *key,
 {
        struct lfsck_instance   *lfsck;
        struct ptlrpc_thread    *thread;
-       struct l_wait_info       lwi    = { 0 };
        int                      rc     = 0;
        int                      rc1    = 0;
        ENTRY;
@@ -3369,6 +3382,10 @@ int lfsck_stop(const struct lu_env *env, struct dt_device *key,
        }
 
        spin_lock(&lfsck->li_lock);
+       /* The target is umounted */
+       if (stop && stop->ls_status == LS_PAUSED)
+               lfsck->li_stopping = 1;
+
        if (thread_is_init(thread) || thread_is_stopped(thread))
                /* no error if LFSCK stopped already, or not started */
                GOTO(unlock, rc = 0);
@@ -3387,6 +3404,9 @@ int lfsck_stop(const struct lu_env *env, struct dt_device *key,
 
        thread_set_flags(thread, SVC_STOPPING);
 
+       LASSERT(lfsck->li_task != NULL);
+       cfs_force_sig(SIGINT, lfsck->li_task);
+
        if (lfsck->li_master) {
                struct lfsck_component *com;
                struct lfsck_assistant_data *lad;
@@ -3395,7 +3415,7 @@ int lfsck_stop(const struct lu_env *env, struct dt_device *key,
                        lad = com->lc_data;
                        spin_lock(&lad->lad_lock);
                        if (lad->lad_task != NULL)
-                               force_sig(SIGINT, lad->lad_task);
+                               cfs_force_sig(SIGINT, lad->lad_task);
                        spin_unlock(&lad->lad_lock);
                }
 
@@ -3403,21 +3423,20 @@ int lfsck_stop(const struct lu_env *env, struct dt_device *key,
                        lad = com->lc_data;
                        spin_lock(&lad->lad_lock);
                        if (lad->lad_task != NULL)
-                               force_sig(SIGINT, lad->lad_task);
+                               cfs_force_sig(SIGINT, lad->lad_task);
                        spin_unlock(&lad->lad_lock);
                }
        }
 
-       wake_up_all(&thread->t_ctl_waitq);
+       wake_up(&thread->t_ctl_waitq);
        spin_unlock(&lfsck->li_lock);
        if (stop && stop->ls_flags & LPF_BROADCAST)
                rc1 = lfsck_stop_all(env, lfsck, stop);
 
        /* It was me set the status as 'stopping' just now, if it is not
         * 'stopping' now, then either stopped, or re-started by race. */
-       l_wait_event(thread->t_ctl_waitq,
-                    !thread_is_stopping(thread),
-                    &lwi);
+       wait_event_idle(thread->t_ctl_waitq,
+                       !thread_is_stopping(thread));
 
        GOTO(put, rc = 0);
 
@@ -3545,8 +3564,8 @@ int lfsck_query(const struct lu_env *env, struct dt_device *key,
                        GOTO(out, rc = -ENOTSUPP);
                }
 
-               for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
-                    i++, type = 1 << i) {
+               for (i = 0, type = BIT(i); i < LFSCK_TYPE_BITS;
+                    i++, type = BIT(i)) {
                        if (!(que->lu_types & type))
                                continue;
 
@@ -3570,8 +3589,8 @@ again:
                if (!(que->lu_flags & LPF_WAIT))
                        GOTO(out, rc);
 
-               for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
-                    i++, type = 1 << i) {
+               for (i = 0, type = BIT(i); i < LFSCK_TYPE_BITS;
+                    i++, type = BIT(i)) {
                        if (!(que->lu_types & type))
                                continue;
 
@@ -3579,17 +3598,16 @@ again:
                            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)
+                                * 3 seconds and try to query again.
+                                */
+                               unsigned long timeout =
+                                       msecs_to_jiffies(3000) + 1;
+                               while (timeout &&
+                                      !fatal_signal_pending(current))
+                                       timeout = schedule_timeout_killable(
+                                               timeout);
+                               if (timeout == 0)
                                        goto again;
                        }
                }
@@ -3688,9 +3706,6 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
        if (IS_ERR(root))
                GOTO(out, rc = PTR_ERR(root));
 
-       if (unlikely(!dt_try_as_dir(env, root)))
-               GOTO(out, rc = -ENOTDIR);
-
        lfsck->li_local_root_fid = *fid;
        if (master) {
                lfsck->li_master = 1;
@@ -3698,9 +3713,8 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
                        struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
                        const struct lu_name *cname;
 
-                       rc = dt_lookup(env, root,
-                               (struct dt_rec *)(&lfsck->li_global_root_fid),
-                               (const struct dt_key *)"ROOT");
+                       rc = dt_lookup_dir(env, root, "ROOT",
+                                          &lfsck->li_global_root_fid);
                        if (rc != 0)
                                GOTO(out, rc);
 
@@ -3708,11 +3722,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
                        if (IS_ERR(obj))
                                GOTO(out, rc = PTR_ERR(obj));
 
-                       if (unlikely(!dt_try_as_dir(env, obj)))
-                               GOTO(out, rc = -ENOTDIR);
-
-                       rc = dt_lookup(env, obj, (struct dt_rec *)fid,
-                               (const struct dt_key *)dotlustre);
+                       rc = dt_lookup_dir(env, obj, dotlustre, fid);
                        if (rc != 0)
                                GOTO(out, rc);
 
@@ -3723,7 +3733,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
 
                        cname = lfsck_name_get_const(env, dotlustre,
                                                     strlen(dotlustre));
-                       rc = lfsck_verify_linkea(env, obj, cname,
+                       rc = lfsck_verify_linkea(env, lfsck, obj, cname,
                                                 &lfsck->li_global_root_fid);
                        if (rc != 0)
                                GOTO(out, rc);
@@ -3732,8 +3742,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
                                GOTO(out, rc = -ENOTDIR);
 
                        *pfid = *fid;
-                       rc = dt_lookup(env, obj, (struct dt_rec *)fid,
-                                      (const struct dt_key *)lostfound);
+                       rc = dt_lookup_dir(env, obj, lostfound, fid);
                        if (rc != 0)
                                GOTO(out, rc);
 
@@ -3744,7 +3753,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key,
 
                        cname = lfsck_name_get_const(env, lostfound,
                                                     strlen(lostfound));
-                       rc = lfsck_verify_linkea(env, obj, cname, pfid);
+                       rc = lfsck_verify_linkea(env, lfsck, obj, cname, pfid);
                        if (rc != 0)
                                GOTO(out, rc);
 
@@ -3949,9 +3958,6 @@ static int __init lfsck_init(void)
 {
        int rc;
 
-       INIT_LIST_HEAD(&lfsck_instance_list);
-       INIT_LIST_HEAD(&lfsck_ost_orphan_list);
-       INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
        lfsck_key_init_generic(&lfsck_thread_key, NULL);
        rc = lu_context_key_register(&lfsck_thread_key);
        if (!rc) {