4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2013, 2015, Intel Corporation.
26 * lustre/lfsck/lfsck_engine.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lu_object.h>
34 #include <dt_object.h>
35 #include <lustre_net.h>
36 #include <lustre_fid.h>
37 #include <obd_support.h>
38 #include <lustre_lib.h>
40 #include "lfsck_internal.h"
42 int lfsck_unpack_ent(struct lu_dirent *ent, __u64 *cookie, __u16 *type)
45 int align = sizeof(*lt) - 1;
48 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
49 *cookie = le64_to_cpu(ent->lde_hash);
50 ent->lde_reclen = le16_to_cpu(ent->lde_reclen);
51 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
52 ent->lde_attrs = le32_to_cpu(ent->lde_attrs);
54 if (unlikely(!(ent->lde_attrs & LUDA_TYPE)))
57 len = (ent->lde_namelen + align) & ~align;
58 lt = (struct luda_type *)(ent->lde_name + len);
59 *type = le16_to_cpu(lt->lt_type);
61 /* Make sure the name is terminated with '\0'. The data (object type)
62 * after ent::lde_name maybe broken, but we have stored such data in
63 * the output parameter @type as above. */
64 ent->lde_name[ent->lde_namelen] = '\0';
69 static void lfsck_di_oit_put(const struct lu_env *env, struct lfsck_instance *lfsck)
71 const struct dt_it_ops *iops;
74 spin_lock(&lfsck->li_lock);
75 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
76 di = lfsck->li_di_oit;
77 lfsck->li_di_oit = NULL;
78 spin_unlock(&lfsck->li_lock);
82 static void lfsck_di_dir_put(const struct lu_env *env, struct lfsck_instance *lfsck)
84 const struct dt_it_ops *iops;
87 spin_lock(&lfsck->li_lock);
88 iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
89 di = lfsck->li_di_dir;
90 lfsck->li_di_dir = NULL;
91 lfsck->li_cookie_dir = 0;
92 spin_unlock(&lfsck->li_lock);
96 static int lfsck_parent_fid(const struct lu_env *env, struct dt_object *obj,
99 if (unlikely(!S_ISDIR(lfsck_object_type(obj)) ||
100 !dt_try_as_dir(env, obj)))
103 return dt_lookup(env, obj, (struct dt_rec *)fid,
104 (const struct dt_key *)"..");
108 * Check whether needs to scan the directory or not.
110 * 1) If we are not doing namespace LFSCK, or the given @obj is not directory,
111 * then needs not to scan the @obj. Otherwise,
112 * 2) Global /ROOT needs to be scanned, backend root needs not to be scanned.
113 * 3) If the @obj is neither IGIF nor normal FID (including .lustre and its
114 * sub-directories that have been scanned when the LFSCK engine start),
115 * then needs not to be scanned.
116 * 4) If it is a remote object, then scanning the object will be done on the
117 * MDT on which the object really resides.
118 * 5) If the local object has normal FID, then needs to be scanned. Otherwise,
119 * 6) If the object has linkEA, then needs to be scanned. Otherwise,
120 * 7) If none of the previous conditions are true, we need to check the parent
121 * directories whether this subdirectory is in a tree that should be scanned.
122 * Set the parent as current @obj, repeat 2)-7).
124 * \param[in] env pointer to the thread context
125 * \param[in] lfsck pointer to the lfsck instance
126 * \param[in] obj pointer to the object to be checked
128 * \retval positive number if the directory needs to be scanned
129 * \retval 0 if the directory needs NOT to be scanned
130 * \retval negative error number on failure
132 static int lfsck_needs_scan_dir(const struct lu_env *env,
133 struct lfsck_instance *lfsck,
134 struct dt_object *obj)
136 struct lfsck_thread_info *info = lfsck_env_info(env);
137 struct lu_fid *fid = &info->lti_fid;
138 struct lu_seq_range *range = &info->lti_range;
139 struct seq_server_site *ss = lfsck_dev_site(lfsck);
140 __u32 idx = lfsck_dev_idx(lfsck);
144 if (list_empty(&lfsck->li_list_dir) || !S_ISDIR(lfsck_object_type(obj)))
149 *fid = *lfsck_dto2fid(obj);
151 /* Global /ROOT is visible. */
152 if (unlikely(lu_fid_eq(fid, &lfsck->li_global_root_fid)))
155 /* Backend root is invisible. */
156 if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid)))
159 if (!fid_is_norm(fid) && !fid_is_igif(fid))
162 fld_range_set_mdt(range);
163 rc = fld_local_lookup(env, ss->ss_server_fld,
164 fid_seq(fid), range);
165 if (rc != 0 || range->lsr_index != idx)
166 /* Current FID should NOT be for the input parameter
167 * @obj, because the lfsck_master_oit_engine() has
168 * filtered out agent object. So current FID is for
169 * the ancestor of the original input parameter @obj.
170 * So the ancestor is a remote directory. The input
171 * parameter @obj is local directory, and should be
172 * scanned under such case. */
175 /* normal FID on this target (locally) must be for the
176 * client-side visiable object. */
177 if (fid_is_norm(fid))
181 obj = lfsck_object_find_bottom(env, lfsck, fid);
186 if (!dt_object_exists(obj))
190 dt_read_lock(env, obj, MOR_TGT_CHILD);
191 if (unlikely(lfsck_is_dead_obj(obj))) {
192 dt_read_unlock(env, obj);
197 rc = dt_xattr_get(env, obj,
198 lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK);
199 dt_read_unlock(env, obj);
203 if (rc < 0 && rc != -ENODATA)
206 rc = lfsck_parent_fid(env, obj, fid);
208 lfsck_object_put(env, obj);
214 if (!fid_is_sane(fid))
219 if (depth > 0 && obj != NULL)
220 lfsck_object_put(env, obj);
225 static int lfsck_load_stripe_lmv(const struct lu_env *env,
226 struct lfsck_instance *lfsck,
227 struct dt_object *obj)
229 struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
230 struct lfsck_lmv *llmv;
234 LASSERT(lfsck->li_obj_dir == NULL);
235 LASSERT(lfsck->li_lmv == NULL);
237 rc = lfsck_read_stripe_lmv(env, obj, lmv);
238 if (rc == -ENODATA) {
239 lfsck->li_obj_dir = lfsck_object_get(obj);
251 if (lmv->lmv_magic == LMV_MAGIC) {
252 struct lfsck_slave_lmv_rec *lslr;
255 llmv->ll_lmv_master = 1;
256 if (lmv->lmv_stripe_count < 1)
257 stripes = LFSCK_LMV_DEF_STRIPES;
258 else if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
259 stripes = LFSCK_LMV_MAX_STRIPES;
261 stripes = lmv->lmv_stripe_count;
263 OBD_ALLOC_LARGE(lslr, sizeof(*lslr) * stripes);
270 llmv->ll_stripes_allocated = stripes;
271 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
272 llmv->ll_lslr = lslr;
274 llmv->ll_lmv_slave = 1;
277 lfsck->li_obj_dir = lfsck_object_get(obj);
279 atomic_set(&llmv->ll_ref, 1);
280 lfsck->li_lmv = llmv;
285 /* LFSCK wrap functions */
287 static void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
290 struct lfsck_component *com;
292 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
293 com->lc_ops->lfsck_fail(env, com, new_checked);
297 void lfsck_close_dir(const struct lu_env *env,
298 struct lfsck_instance *lfsck, int result)
300 struct lfsck_component *com;
303 if (lfsck->li_lmv != NULL) {
304 lfsck->li_lmv->ll_exit_value = result;
305 if (lfsck->li_obj_dir != NULL) {
306 list_for_each_entry(com, &lfsck->li_list_dir,
308 com->lc_ops->lfsck_close_dir(env, com);
312 lfsck_lmv_put(env, lfsck->li_lmv);
313 lfsck->li_lmv = NULL;
316 if (lfsck->li_di_dir != NULL) {
317 const struct dt_it_ops *dir_iops;
318 struct dt_it *dir_di = lfsck->li_di_dir;
320 LASSERT(lfsck->li_obj_dir != NULL);
322 dir_iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
323 lfsck_di_dir_put(env, lfsck);
324 dir_iops->fini(env, dir_di);
327 if (lfsck->li_obj_dir != NULL) {
328 struct dt_object *dir_obj = lfsck->li_obj_dir;
330 lfsck->li_obj_dir = NULL;
331 lfsck_object_put(env, dir_obj);
337 int lfsck_open_dir(const struct lu_env *env,
338 struct lfsck_instance *lfsck, __u64 cookie)
340 struct dt_object *obj = lfsck->li_obj_dir;
341 struct dt_it *di = lfsck->li_di_dir;
342 struct lfsck_component *com;
343 const struct dt_it_ops *iops;
347 LASSERT(obj != NULL);
350 if (unlikely(!dt_try_as_dir(env, obj)))
351 GOTO(out, rc = -ENOTDIR);
353 list_for_each_entry(com, &lfsck->li_list_dir, lc_link_dir) {
354 rc = com->lc_ops->lfsck_open_dir(env, com);
359 iops = &obj->do_index_ops->dio_it;
360 di = iops->init(env, obj, lfsck->li_args_dir);
362 GOTO(out, rc = PTR_ERR(di));
364 rc = iops->load(env, di, cookie);
365 if (rc == 0 || (rc > 0 && cookie > 0))
366 rc = iops->next(env, di);
374 lfsck->li_cookie_dir = iops->store(env, di);
375 spin_lock(&lfsck->li_lock);
376 lfsck->li_di_dir = di;
377 spin_unlock(&lfsck->li_lock);
384 lfsck_close_dir(env, lfsck, rc);
389 static int lfsck_checkpoint(const struct lu_env *env,
390 struct lfsck_instance *lfsck)
392 struct lfsck_component *com;
396 if (likely(cfs_time_beforeq(cfs_time_current(),
397 lfsck->li_time_next_checkpoint)))
400 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
401 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
402 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
407 lfsck->li_time_last_checkpoint = cfs_time_current();
408 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
409 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
410 return rc1 != 0 ? rc1 : rc;
413 static int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
414 struct lfsck_start_param *lsp)
416 struct dt_object *obj = NULL;
417 struct lfsck_component *com;
418 struct lfsck_component *next;
419 struct lfsck_position *pos = NULL;
420 const struct dt_it_ops *iops =
421 &lfsck->li_obj_oit->do_index_ops->dio_it;
425 LASSERT(lfsck->li_obj_dir == NULL);
426 LASSERT(lfsck->li_di_dir == NULL);
428 lfsck->li_current_oit_processed = 0;
429 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
430 com->lc_new_checked = 0;
431 rc = com->lc_ops->lfsck_prep(env, com, lsp);
436 (!lfsck_pos_is_zero(&com->lc_pos_start) &&
437 lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
438 pos = &com->lc_pos_start;
441 /* Init otable-based iterator. */
443 rc = iops->load(env, lfsck->li_di_oit, 0);
445 lfsck->li_oit_over = 1;
452 rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
456 lfsck->li_oit_over = 1;
458 if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
461 /* Find the directory for namespace-based traverse. */
462 obj = lfsck_object_find_bottom(env, lfsck, &pos->lp_dir_parent);
464 RETURN(PTR_ERR(obj));
466 /* Remote directory will be scanned by the LFSCK instance
467 * on the MDT where the remote object really resides on. */
468 if (!dt_object_exists(obj) || dt_object_remote(obj) ||
469 unlikely(!S_ISDIR(lfsck_object_type(obj))))
472 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
474 /* For the master MDT-object of a striped directory,
475 * reset the iteration from the directory beginning. */
476 if (lfsck->li_lmv != NULL && lfsck->li_lmv->ll_lmv_master)
477 pos->lp_dir_cookie = 0;
479 rc = lfsck_open_dir(env, lfsck, pos->lp_dir_cookie);
481 /* The end of the directory. */
489 lfsck_object_put(env, obj);
492 lfsck_close_dir(env, lfsck, rc);
493 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
495 com->lc_ops->lfsck_post(env, com, rc, true);
502 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, true);
503 lfsck->li_pos_current = lfsck->li_pos_checkpoint;
504 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
505 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
510 lfsck->li_time_last_checkpoint = cfs_time_current();
511 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
512 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
516 static int lfsck_exec_oit(const struct lu_env *env,
517 struct lfsck_instance *lfsck, struct dt_object *obj)
519 struct lfsck_component *com;
523 LASSERT(lfsck->li_obj_dir == NULL);
525 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
526 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
531 rc = lfsck_needs_scan_dir(env, lfsck, obj);
535 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
537 rc = lfsck_open_dir(env, lfsck, 0);
543 lfsck_fail(env, lfsck, false);
546 lfsck_close_dir(env, lfsck, rc);
548 return rc > 0 ? 0 : rc;
551 static int lfsck_exec_dir(const struct lu_env *env,
552 struct lfsck_instance *lfsck,
553 struct lfsck_assistant_object *lso,
554 struct lu_dirent *ent, __u16 type)
556 struct lfsck_component *com;
559 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
560 rc = com->lc_ops->lfsck_exec_dir(env, com, lso, ent, type);
567 static int lfsck_master_dir_engine(const struct lu_env *env,
568 struct lfsck_instance *lfsck);
570 static int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
573 struct lfsck_component *com;
574 struct lfsck_component *next;
577 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
578 lfsck_close_dir(env, lfsck, result);
580 while (thread_is_running(&lfsck->li_thread) && rc > 0 &&
581 !list_empty(&lfsck->li_list_lmv)) {
582 struct lfsck_lmv_unit *llu;
584 spin_lock(&lfsck->li_lock);
585 llu = list_entry(lfsck->li_list_lmv.next,
586 struct lfsck_lmv_unit, llu_link);
587 list_del_init(&llu->llu_link);
588 spin_unlock(&lfsck->li_lock);
590 lfsck->li_lmv = &llu->llu_lmv;
591 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
592 rc = lfsck_open_dir(env, lfsck, 0);
594 rc = lfsck_master_dir_engine(env, lfsck);
595 lfsck_close_dir(env, lfsck, result);
601 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
602 rc = com->lc_ops->lfsck_post(env, com, result, false);
604 CDEBUG(D_LFSCK, "%s: lfsck_post at the component %u: "
605 "rc = %d\n", lfsck_lfsck2name(lfsck),
606 (__u32)com->lc_type, rc);
609 lfsck->li_time_last_checkpoint = cfs_time_current();
610 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
611 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
613 /* Ignore some component post failure to make other can go ahead. */
617 static int lfsck_double_scan(const struct lu_env *env,
618 struct lfsck_instance *lfsck)
620 struct lfsck_component *com;
621 struct lfsck_component *next;
622 struct l_wait_info lwi = { 0 };
626 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
627 rc = com->lc_ops->lfsck_double_scan(env, com);
632 l_wait_event(lfsck->li_thread.t_ctl_waitq,
633 atomic_read(&lfsck->li_double_scan_count) == 0,
636 if (lfsck->li_status != LS_PAUSED &&
637 lfsck->li_status != LS_CO_PAUSED) {
638 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
640 spin_lock(&lfsck->li_lock);
641 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
642 spin_unlock(&lfsck->li_lock);
646 return rc1 != 0 ? rc1 : rc;
649 static void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck)
651 struct lfsck_component *com;
652 struct lfsck_component *next;
654 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
656 if (com->lc_ops->lfsck_quit != NULL)
657 com->lc_ops->lfsck_quit(env, com);
659 spin_lock(&lfsck->li_lock);
660 list_del_init(&com->lc_link_dir);
661 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
662 spin_unlock(&lfsck->li_lock);
665 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
667 if (com->lc_ops->lfsck_quit != NULL)
668 com->lc_ops->lfsck_quit(env, com);
670 spin_lock(&lfsck->li_lock);
671 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
672 spin_unlock(&lfsck->li_lock);
678 static int lfsck_master_dir_engine(const struct lu_env *env,
679 struct lfsck_instance *lfsck)
681 struct lfsck_thread_info *info = lfsck_env_info(env);
682 struct dt_object *dir = lfsck->li_obj_dir;
683 const struct dt_it_ops *iops = &dir->do_index_ops->dio_it;
684 struct dt_it *di = lfsck->li_di_dir;
685 struct lu_dirent *ent =
686 (struct lu_dirent *)info->lti_key;
687 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
688 struct ptlrpc_thread *thread = &lfsck->li_thread;
689 struct lfsck_assistant_object *lso = NULL;
695 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY2, cfs_fail_val) &&
696 unlikely(!thread_is_running(thread))) {
697 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
698 "parent "DFID", cookie "LPX64"\n",
699 lfsck_lfsck2name(lfsck),
700 PFID(lfsck_dto2fid(dir)), lfsck->li_cookie_dir);
705 lfsck->li_new_scanned++;
706 rc = iops->rec(env, di, (struct dt_rec *)ent,
709 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
713 CDEBUG(D_LFSCK, "%s: scan dir failed at rec(), "
714 "parent "DFID", cookie "LPX64": rc = %d\n",
715 lfsck_lfsck2name(lfsck),
716 PFID(lfsck_dto2fid(dir)),
717 lfsck->li_cookie_dir, rc);
718 lfsck_fail(env, lfsck, true);
719 if (bk->lb_param & LPF_FAILOUT)
725 if (ent->lde_attrs & LUDA_IGNORE)
728 /* skip dot entry. */
729 if (ent->lde_namelen == 1 && ent->lde_name[0] == '.')
733 lso = lfsck_assistant_object_init(env,
734 lfsck_dto2fid(dir), NULL,
735 lfsck->li_pos_current.lp_oit_cookie, true);
737 if (bk->lb_param & LPF_FAILOUT)
738 RETURN(PTR_ERR(lso));
745 /* The type in the @ent structure may has been overwritten,
746 * so we need to pass the @type parameter independently. */
747 rc = lfsck_exec_dir(env, lfsck, lso, ent, type);
748 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
752 rc = lfsck_checkpoint(env, lfsck);
753 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
757 lfsck_control_speed(lfsck);
758 if (unlikely(!thread_is_running(thread))) {
759 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
760 "parent "DFID", cookie "LPX64"\n",
761 lfsck_lfsck2name(lfsck),
762 PFID(lfsck_dto2fid(dir)),
763 lfsck->li_cookie_dir);
767 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
768 spin_lock(&lfsck->li_lock);
769 thread_set_flags(thread, SVC_STOPPING);
770 spin_unlock(&lfsck->li_lock);
771 GOTO(out, rc = -EINVAL);
774 rc = iops->next(env, di);
777 if (rc > 0 && !lfsck->li_oit_over)
778 lfsck_close_dir(env, lfsck, rc);
784 lfsck_assistant_object_put(env, lso);
790 * Object-table based iteration engine.
792 * Object-table based iteration is the basic linear engine to scan all the
793 * objects on current device in turn. For each object, it calls all the
794 * registered LFSCK component(s)' API to perform related consistency
797 * It flushes related LFSCK trace files to disk via making checkpoint
798 * periodically. Then if the server crashed or the LFSCK is paused, the
799 * LFSCK can resume from the latest checkpoint.
801 * It also controls the whole LFSCK speed via lfsck_control_speed() to
802 * avoid the server to become overload.
804 * \param[in] env pointer to the thread context
805 * \param[in] lfsck pointer to the lfsck instance
807 * \retval positive number if all objects have been scanned
808 * \retval 0 if the iteration is stopped or paused
809 * \retval negative error number on failure
811 static int lfsck_master_oit_engine(const struct lu_env *env,
812 struct lfsck_instance *lfsck)
814 struct lfsck_thread_info *info = lfsck_env_info(env);
815 const struct dt_it_ops *iops =
816 &lfsck->li_obj_oit->do_index_ops->dio_it;
817 struct dt_it *di = lfsck->li_di_oit;
818 struct lu_fid *fid = &info->lti_fid;
819 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
820 struct ptlrpc_thread *thread = &lfsck->li_thread;
821 struct seq_server_site *ss = lfsck_dev_site(lfsck);
822 __u32 idx = lfsck_dev_idx(lfsck);
826 if (unlikely(ss == NULL))
830 struct dt_object *target;
832 if (lfsck->li_di_dir != NULL) {
833 rc = lfsck_master_dir_engine(env, lfsck);
838 if (unlikely(lfsck->li_oit_over))
841 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY1, cfs_fail_val) &&
842 unlikely(!thread_is_running(thread))) {
843 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
845 lfsck_lfsck2name(lfsck), iops->store(env, di));
850 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
853 lfsck->li_current_oit_processed = 1;
855 if (!list_empty(&lfsck->li_list_lmv)) {
856 struct lfsck_lmv_unit *llu;
858 spin_lock(&lfsck->li_lock);
859 llu = list_entry(lfsck->li_list_lmv.next,
860 struct lfsck_lmv_unit, llu_link);
861 list_del_init(&llu->llu_link);
862 spin_unlock(&lfsck->li_lock);
864 lfsck->li_lmv = &llu->llu_lmv;
865 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
866 rc = lfsck_open_dir(env, lfsck, 0);
868 rc = lfsck_master_dir_engine(env, lfsck);
874 lfsck->li_new_scanned++;
875 lfsck->li_pos_current.lp_oit_cookie = iops->store(env, di);
876 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
878 CDEBUG(D_LFSCK, "%s: OIT scan failed at rec(): "
879 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
880 lfsck_fail(env, lfsck, true);
881 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
887 if (unlikely(!fid_is_sane(fid))) {
888 CDEBUG(D_LFSCK, "%s: OIT scan find invalid FID "DFID
890 lfsck_lfsck2name(lfsck), PFID(fid));
894 if (fid_is_idif(fid)) {
895 __u32 idx1 = fid_idif_ost_idx(fid);
897 LASSERT(!lfsck->li_master);
900 struct ost_id *oi = &info->lti_oi;
902 if (unlikely(idx1 != 0)) {
903 CDEBUG(D_LFSCK, "%s: invalid IDIF "DFID
904 ", not match device index %u\n",
905 lfsck_lfsck2name(lfsck),
911 /* rebuild the IDIF with index to
912 * avoid double instances for the
914 fid_to_ostid(fid, oi);
915 ostid_to_fid(fid, oi, idx);
917 } else if (!fid_is_norm(fid) && !fid_is_igif(fid) &&
918 !fid_is_last_id(fid) &&
919 !lu_fid_eq(fid, &lfsck->li_global_root_fid)) {
921 /* If the FID/object is only used locally and invisible
922 * to external nodes, then LFSCK will not handle it.
924 * dot_lustre sequence has been handled specially. */
927 struct lu_seq_range *range = &info->lti_range;
929 if (lfsck->li_master)
930 fld_range_set_mdt(range);
932 fld_range_set_ost(range);
933 rc = fld_local_lookup(env, ss->ss_server_fld,
934 fid_seq(fid), range);
935 if (rc != 0 || range->lsr_index != idx) {
936 /* Remote object will be handled by the LFSCK
937 * instance on the MDT where the remote object
938 * really resides on. */
944 target = lfsck_object_find_bottom(env, lfsck, fid);
945 if (IS_ERR(target)) {
946 CDEBUG(D_LFSCK, "%s: OIT scan failed at find target "
947 DFID", cookie "LPU64": rc = %d\n",
948 lfsck_lfsck2name(lfsck), PFID(fid),
949 iops->store(env, di), rc);
950 lfsck_fail(env, lfsck, true);
951 if (bk->lb_param & LPF_FAILOUT)
952 RETURN(PTR_ERR(target));
957 if (dt_object_exists(target))
958 rc = lfsck_exec_oit(env, lfsck, target);
960 lfsck_object_put(env, target);
961 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
965 rc = lfsck_checkpoint(env, lfsck);
966 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
970 lfsck_control_speed(lfsck);
972 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
973 spin_lock(&lfsck->li_lock);
974 thread_set_flags(thread, SVC_STOPPING);
975 spin_unlock(&lfsck->li_lock);
979 rc = iops->next(env, di);
980 if (unlikely(rc > 0))
981 lfsck->li_oit_over = 1;
982 else if (likely(rc == 0))
983 lfsck->li_current_oit_processed = 0;
985 if (unlikely(!thread_is_running(thread))) {
986 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
987 "cookie "LPU64"\n", lfsck_lfsck2name(lfsck),
988 iops->store(env, di));
991 } while (rc == 0 || lfsck->li_di_dir != NULL);
996 int lfsck_master_engine(void *args)
998 struct lfsck_thread_args *lta = args;
999 struct lu_env *env = <a->lta_env;
1000 struct lfsck_instance *lfsck = lta->lta_lfsck;
1001 struct ptlrpc_thread *thread = &lfsck->li_thread;
1002 struct dt_object *oit_obj = lfsck->li_obj_oit;
1003 const struct dt_it_ops *oit_iops = &oit_obj->do_index_ops->dio_it;
1004 struct dt_it *oit_di;
1005 struct l_wait_info lwi = { 0 };
1009 /* There will be some objects verification during the LFSCK start,
1010 * such as the subsequent lfsck_verify_lpf(). Trigger low layer OI
1011 * OI scrub before that to handle the potential inconsistence. */
1012 oit_di = oit_iops->init(env, oit_obj, lfsck->li_args_oit);
1013 if (IS_ERR(oit_di)) {
1014 rc = PTR_ERR(oit_di);
1015 CDEBUG(D_LFSCK, "%s: master engine fail to init iteration: "
1016 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1018 GOTO(fini_args, rc);
1021 if (lfsck->li_master &&
1022 (!list_empty(&lfsck->li_list_scan) ||
1023 !list_empty(&lfsck->li_list_double_scan))) {
1024 rc = lfsck_verify_lpf(env, lfsck);
1025 /* Fail to verify the .lustre/lost+found/MDTxxxx/ may be not
1026 * fatal, because the .lustre/lost+found/ maybe not accessed
1027 * by the LFSCK if it does not add orphans or others to such
1028 * directory. So go ahead until hit failure when really uses
1031 CDEBUG(D_LFSCK, "%s: master engine fail to verify the "
1032 ".lustre/lost+found/, go ahead: rc = %d\n",
1033 lfsck_lfsck2name(lfsck), rc);
1036 spin_lock(&lfsck->li_lock);
1037 lfsck->li_di_oit = oit_di;
1038 spin_unlock(&lfsck->li_lock);
1039 rc = lfsck_prep(env, lfsck, lta->lta_lsp);
1043 CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = %#x, dir_flags = %#x, "
1044 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1045 ", pid = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1046 lfsck->li_pos_checkpoint.lp_oit_cookie,
1047 lfsck->li_pos_checkpoint.lp_dir_cookie,
1048 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1051 spin_lock(&lfsck->li_lock);
1052 thread_set_flags(thread, SVC_RUNNING);
1053 spin_unlock(&lfsck->li_lock);
1054 wake_up_all(&thread->t_ctl_waitq);
1056 l_wait_event(thread->t_ctl_waitq,
1057 lfsck->li_start_unplug ||
1058 !thread_is_running(thread),
1060 if (!thread_is_running(thread))
1061 GOTO(fini_oit, rc = 0);
1063 if (!list_empty(&lfsck->li_list_scan) ||
1064 list_empty(&lfsck->li_list_double_scan))
1065 rc = lfsck_master_oit_engine(env, lfsck);
1069 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
1070 CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = %#x, dir_flags = %#x, "
1071 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1072 ", pid = %d, rc = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1073 lfsck->li_pos_checkpoint.lp_oit_cookie,
1074 lfsck->li_pos_checkpoint.lp_dir_cookie,
1075 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1078 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
1079 rc = lfsck_post(env, lfsck, rc);
1081 lfsck_close_dir(env, lfsck, rc);
1084 lfsck_di_oit_put(env, lfsck);
1085 oit_iops->fini(env, oit_di);
1087 if (!list_empty(&lfsck->li_list_double_scan))
1088 rc = lfsck_double_scan(env, lfsck);
1092 lfsck_quit(env, lfsck);
1095 /* XXX: Purge the pinned objects in the future. */
1098 spin_lock(&lfsck->li_lock);
1099 thread_set_flags(thread, SVC_STOPPED);
1100 spin_unlock(&lfsck->li_lock);
1101 wake_up_all(&thread->t_ctl_waitq);
1102 lfsck_thread_args_fini(lta);
1106 static inline bool lfsck_assistant_req_empty(struct lfsck_assistant_data *lad)
1110 spin_lock(&lad->lad_lock);
1111 if (list_empty(&lad->lad_req_list))
1113 spin_unlock(&lad->lad_lock);
1119 * Query the LFSCK status from the instatnces on remote servers.
1121 * The LFSCK assistant thread queries the LFSCK instances on other
1122 * servers (MDT/OST) about their status, such as whether they have
1123 * finished the phase1/phase2 scanning or not, and so on.
1125 * \param[in] env pointer to the thread context
1126 * \param[in] com pointer to the lfsck component
1128 * \retval 0 for success
1129 * \retval negative error number on failure
1131 static int lfsck_assistant_query_others(const struct lu_env *env,
1132 struct lfsck_component *com)
1134 struct lfsck_thread_info *info = lfsck_env_info(env);
1135 struct lfsck_request *lr = &info->lti_lr;
1136 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1137 struct lfsck_instance *lfsck = com->lc_lfsck;
1138 struct lfsck_assistant_data *lad = com->lc_data;
1139 struct ptlrpc_request_set *set;
1140 struct lfsck_tgt_descs *ltds;
1141 struct lfsck_tgt_desc *ltd;
1142 struct list_head *phase_head;
1147 set = ptlrpc_prep_set();
1151 lad->lad_touch_gen++;
1152 memset(lr, 0, sizeof(*lr));
1153 lr->lr_event = LE_QUERY;
1154 lr->lr_active = com->lc_type;
1156 memset(laia, 0, sizeof(*laia));
1157 laia->laia_com = com;
1160 if (!list_empty(&lad->lad_mdt_phase1_list)) {
1161 ltds = &lfsck->li_mdt_descs;
1163 phase_head = &lad->lad_mdt_phase1_list;
1164 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1169 ltds = &lfsck->li_ost_descs;
1170 lr->lr_flags = LEF_TO_OST;
1171 phase_head = &lad->lad_ost_phase1_list;
1174 laia->laia_ltds = ltds;
1175 spin_lock(<ds->ltd_lock);
1176 while (!list_empty(phase_head)) {
1177 struct list_head *phase_list;
1180 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1181 ltd = list_entry(phase_head->next,
1182 struct lfsck_tgt_desc,
1183 ltd_layout_phase_list);
1184 phase_list = <d->ltd_layout_phase_list;
1185 gen = <d->ltd_layout_gen;
1187 ltd = list_entry(phase_head->next,
1188 struct lfsck_tgt_desc,
1189 ltd_namespace_phase_list);
1190 phase_list = <d->ltd_namespace_phase_list;
1191 gen = <d->ltd_namespace_gen;
1194 if (*gen == lad->lad_touch_gen)
1197 *gen = lad->lad_touch_gen;
1198 list_move_tail(phase_list, phase_head);
1199 atomic_inc(<d->ltd_ref);
1200 laia->laia_ltd = ltd;
1201 spin_unlock(<ds->ltd_lock);
1202 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1203 lfsck_async_interpret_common,
1206 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to query "
1207 "%s %x for %s: rc = %d\n",
1208 lfsck_lfsck2name(lfsck),
1209 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1210 ltd->ltd_index, lad->lad_name, rc);
1214 spin_lock(<ds->ltd_lock);
1216 spin_unlock(<ds->ltd_lock);
1218 rc = ptlrpc_set_wait(set);
1220 ptlrpc_set_destroy(set);
1224 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST) &&
1225 list_empty(&lad->lad_mdt_phase1_list))
1229 ptlrpc_set_destroy(set);
1231 RETURN(rc1 != 0 ? rc1 : rc);
1235 * Notify the LFSCK event to the instances on remote servers.
1237 * The LFSCK assistant thread notifies the LFSCK instances on other
1238 * servers (MDT/OST) about some events, such as start new scanning,
1239 * stop the scanning, this LFSCK instance will exit, and so on.
1241 * \param[in] env pointer to the thread context
1242 * \param[in] com pointer to the lfsck component
1243 * \param[in] lr pointer to the LFSCK event request
1245 * \retval 0 for success
1246 * \retval negative error number on failure
1248 static int lfsck_assistant_notify_others(const struct lu_env *env,
1249 struct lfsck_component *com,
1250 struct lfsck_request *lr)
1252 struct lfsck_thread_info *info = lfsck_env_info(env);
1253 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1254 struct lfsck_instance *lfsck = com->lc_lfsck;
1255 struct lfsck_assistant_data *lad = com->lc_data;
1256 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1257 struct ptlrpc_request_set *set;
1258 struct lfsck_tgt_descs *ltds;
1259 struct lfsck_tgt_desc *ltd;
1260 struct lfsck_tgt_desc *next;
1266 set = ptlrpc_prep_set();
1270 lr->lr_index = lfsck_dev_idx(lfsck);
1271 lr->lr_active = com->lc_type;
1273 memset(laia, 0, sizeof(*laia));
1274 laia->laia_com = com;
1277 switch (lr->lr_event) {
1279 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1282 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN;
1283 lr->lr_speed = bk->lb_speed_limit;
1284 lr->lr_version = bk->lb_version;
1285 lr->lr_param |= bk->lb_param;
1286 lr->lr_async_windows = bk->lb_async_windows;
1287 lr->lr_flags = LEF_TO_OST;
1289 /* Notify OSTs firstly, then handle other MDTs if needed. */
1290 ltds = &lfsck->li_ost_descs;
1291 laia->laia_ltds = ltds;
1292 down_read(<ds->ltd_rw_sem);
1293 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1294 ltd = lfsck_tgt_get(ltds, idx);
1295 LASSERT(ltd != NULL);
1297 laia->laia_ltd = ltd;
1298 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1299 lfsck_async_interpret_common,
1300 laia, LFSCK_NOTIFY);
1302 lfsck_lad_set_bitmap(env, com, idx);
1303 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1304 "notify OST %x for %s start: rc = %d\n",
1305 lfsck_lfsck2name(lfsck), idx,
1310 up_read(<ds->ltd_rw_sem);
1313 rc = ptlrpc_set_wait(set);
1315 ptlrpc_set_destroy(set);
1320 if (!(bk->lb_param & LPF_ALL_TGT))
1323 /* link other MDT targets locallly. */
1324 ltds = &lfsck->li_mdt_descs;
1325 spin_lock(<ds->ltd_lock);
1326 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1327 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1328 ltd = lfsck_ltd2tgt(ltds, idx);
1329 LASSERT(ltd != NULL);
1331 if (!list_empty(<d->ltd_layout_list))
1334 list_add_tail(<d->ltd_layout_list,
1335 &lad->lad_mdt_list);
1336 list_add_tail(<d->ltd_layout_phase_list,
1337 &lad->lad_mdt_phase1_list);
1340 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1341 ltd = lfsck_ltd2tgt(ltds, idx);
1342 LASSERT(ltd != NULL);
1344 if (!list_empty(<d->ltd_namespace_list))
1347 list_add_tail(<d->ltd_namespace_list,
1348 &lad->lad_mdt_list);
1349 list_add_tail(<d->ltd_namespace_phase_list,
1350 &lad->lad_mdt_phase1_list);
1353 spin_unlock(<ds->ltd_lock);
1356 case LE_PHASE2_DONE:
1357 case LE_PEER_EXIT: {
1358 struct list_head *phase_head;
1360 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1361 if (bk->lb_param & LPF_ALL_TGT) {
1362 phase_head = &lad->lad_mdt_list;
1363 ltds = &lfsck->li_mdt_descs;
1364 if (lr->lr_event == LE_STOP) {
1365 /* unlink other MDT targets locallly. */
1366 spin_lock(<ds->ltd_lock);
1367 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1368 list_for_each_entry_safe(ltd, next,
1369 phase_head, ltd_layout_list) {
1371 <d->ltd_layout_phase_list);
1373 <d->ltd_layout_list);
1376 list_for_each_entry_safe(ltd, next,
1378 ltd_namespace_list) {
1380 <d->ltd_namespace_phase_list);
1382 <d->ltd_namespace_list);
1385 spin_unlock(<ds->ltd_lock);
1387 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1390 lr->lr_flags |= LEF_TO_OST;
1391 phase_head = &lad->lad_ost_list;
1392 ltds = &lfsck->li_ost_descs;
1394 lr->lr_flags &= ~LEF_TO_OST;
1396 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1399 lr->lr_flags |= LEF_TO_OST;
1400 phase_head = &lad->lad_ost_list;
1401 ltds = &lfsck->li_ost_descs;
1405 laia->laia_ltds = ltds;
1406 spin_lock(<ds->ltd_lock);
1407 while (!list_empty(phase_head)) {
1408 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1409 ltd = list_entry(phase_head->next,
1410 struct lfsck_tgt_desc,
1412 if (!list_empty(<d->ltd_layout_phase_list))
1414 <d->ltd_layout_phase_list);
1415 list_del_init(<d->ltd_layout_list);
1417 ltd = list_entry(phase_head->next,
1418 struct lfsck_tgt_desc,
1419 ltd_namespace_list);
1420 if (!list_empty(<d->ltd_namespace_phase_list))
1422 <d->ltd_namespace_phase_list);
1423 list_del_init(<d->ltd_namespace_list);
1425 atomic_inc(<d->ltd_ref);
1426 laia->laia_ltd = ltd;
1427 spin_unlock(<ds->ltd_lock);
1428 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1429 lfsck_async_interpret_common,
1430 laia, LFSCK_NOTIFY);
1432 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1433 "notify %s %x for %s stop/phase2_done/"
1434 "peer_exit: rc = %d\n",
1435 lfsck_lfsck2name(lfsck),
1436 (lr->lr_flags & LEF_TO_OST) ?
1437 "OST" : "MDT", ltd->ltd_index,
1441 spin_lock(<ds->ltd_lock);
1443 spin_unlock(<ds->ltd_lock);
1445 rc = ptlrpc_set_wait(set);
1447 ptlrpc_set_destroy(set);
1451 if (com->lc_type == LFSCK_TYPE_LAYOUT &&
1452 !(lr->lr_flags & LEF_TO_OST)) {
1453 lr->lr_flags |= LEF_TO_OST;
1454 phase_head = &lad->lad_ost_list;
1455 ltds = &lfsck->li_ost_descs;
1460 case LE_PHASE1_DONE:
1461 lad->lad_ops->la_sync_failures(env, com, lr);
1462 lad->lad_touch_gen++;
1463 ltds = &lfsck->li_mdt_descs;
1464 laia->laia_ltds = ltds;
1465 spin_lock(<ds->ltd_lock);
1466 while (!list_empty(&lad->lad_mdt_list)) {
1467 struct list_head *list;
1470 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1471 ltd = list_entry(lad->lad_mdt_list.next,
1472 struct lfsck_tgt_desc,
1474 list = <d->ltd_layout_list;
1475 gen = <d->ltd_layout_gen;
1477 struct lfsck_namespace *ns = com->lc_file_ram;
1479 ltd = list_entry(lad->lad_mdt_list.next,
1480 struct lfsck_tgt_desc,
1481 ltd_namespace_list);
1482 list = <d->ltd_namespace_list;
1483 gen = <d->ltd_namespace_gen;
1484 lr->lr_flags2 = ns->ln_flags & ~LF_INCOMPLETE;
1487 if (*gen == lad->lad_touch_gen)
1490 *gen = lad->lad_touch_gen;
1491 list_move_tail(list, &lad->lad_mdt_list);
1492 if (ltd->ltd_synced_failures)
1495 atomic_inc(<d->ltd_ref);
1496 laia->laia_ltd = ltd;
1497 spin_unlock(<ds->ltd_lock);
1498 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1499 lfsck_async_interpret_common,
1500 laia, LFSCK_NOTIFY);
1502 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1503 "notify MDT %x for %s phase1 done: "
1504 "rc = %d\n", lfsck_lfsck2name(lfsck),
1505 ltd->ltd_index, lad->lad_name, rc);
1508 spin_lock(<ds->ltd_lock);
1510 spin_unlock(<ds->ltd_lock);
1513 CDEBUG(D_LFSCK, "%s: LFSCK assistant unexpected LFSCK event: "
1514 "rc = %d\n", lfsck_lfsck2name(lfsck), lr->lr_event);
1519 rc1 = ptlrpc_set_wait(set);
1520 ptlrpc_set_destroy(set);
1522 RETURN(rc != 0 ? rc : rc1);
1526 * The LFSCK assistant thread is triggered by the LFSCK main engine.
1527 * They co-work together as an asynchronous pipeline: the LFSCK main
1528 * engine scans the system and pre-fetches the objects, attributes,
1529 * or name entries, etc, and pushes them into the pipeline as input
1530 * requests for the LFSCK assistant thread; on the other end of the
1531 * pipeline, the LFSCK assistant thread performs the real check and
1532 * repair for every request from the main engine.
1534 * Generally, the assistant engine may be blocked when check/repair
1535 * something, so the LFSCK main engine will run some faster. On the
1536 * other hand, the LFSCK main engine will drive multiple assistant
1537 * threads in parallel, means for each LFSCK component on the master
1538 * (such as layout LFSCK, namespace LFSCK), there is an independent
1539 * LFSCK assistant thread. So under such 1:N multiple asynchronous
1540 * pipelines mode, the whole LFSCK performance will be much better
1541 * than check/repair everything by the LFSCK main engine itself.
1543 int lfsck_assistant_engine(void *args)
1545 struct lfsck_thread_args *lta = args;
1546 struct lu_env *env = <a->lta_env;
1547 struct lfsck_component *com = lta->lta_com;
1548 struct lfsck_instance *lfsck = lta->lta_lfsck;
1549 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1550 struct lfsck_position *pos = &com->lc_pos_start;
1551 struct lfsck_thread_info *info = lfsck_env_info(env);
1552 struct lfsck_request *lr = &info->lti_lr;
1553 struct lfsck_assistant_data *lad = com->lc_data;
1554 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1555 struct ptlrpc_thread *athread = &lad->lad_thread;
1556 struct lfsck_assistant_operations *lao = lad->lad_ops;
1557 struct lfsck_assistant_req *lar;
1558 struct l_wait_info lwi = { 0 };
1564 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread start\n",
1565 lfsck_lfsck2name(lfsck), lad->lad_name);
1567 memset(lr, 0, sizeof(*lr));
1568 lr->lr_event = LE_START;
1569 if (pos->lp_oit_cookie <= 1)
1570 lr->lr_param = LPF_RESET;
1571 rc = lfsck_assistant_notify_others(env, com, lr);
1573 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to notify others "
1574 "to start %s: rc = %d\n",
1575 lfsck_lfsck2name(lfsck), lad->lad_name, rc);
1579 spin_lock(&lad->lad_lock);
1580 lad->lad_task = current;
1581 thread_set_flags(athread, SVC_RUNNING);
1582 spin_unlock(&lad->lad_lock);
1583 wake_up_all(&mthread->t_ctl_waitq);
1586 while (!list_empty(&lad->lad_req_list)) {
1587 bool wakeup = false;
1589 if (unlikely(lad->lad_exit ||
1590 !thread_is_running(mthread)))
1591 GOTO(cleanup1, rc = lad->lad_post_result);
1593 lar = list_entry(lad->lad_req_list.next,
1594 struct lfsck_assistant_req,
1596 /* Only the lfsck_assistant_engine thread itself can
1597 * remove the "lar" from the head of the list, LFSCK
1598 * engine thread only inserts other new "lar" at the
1599 * end of the list. So it is safe to handle current
1600 * "lar" without the spin_lock. */
1601 rc = lao->la_handler_p1(env, com, lar);
1602 spin_lock(&lad->lad_lock);
1603 list_del_init(&lar->lar_list);
1604 lad->lad_prefetched--;
1605 /* Wake up the main engine thread only when the list
1606 * is empty or half of the prefetched items have been
1607 * handled to avoid too frequent thread schedule. */
1608 if (lad->lad_prefetched <= (bk->lb_async_windows / 2))
1610 spin_unlock(&lad->lad_lock);
1612 wake_up_all(&mthread->t_ctl_waitq);
1614 lao->la_req_fini(env, lar);
1615 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1619 l_wait_event(athread->t_ctl_waitq,
1620 !lfsck_assistant_req_empty(lad) ||
1623 lad->lad_to_double_scan,
1626 if (unlikely(lad->lad_exit))
1627 GOTO(cleanup1, rc = lad->lad_post_result);
1629 if (!list_empty(&lad->lad_req_list))
1632 if (lad->lad_to_post) {
1633 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread post\n",
1634 lfsck_lfsck2name(lfsck), lad->lad_name);
1636 if (unlikely(lad->lad_exit))
1637 GOTO(cleanup1, rc = lad->lad_post_result);
1639 lad->lad_to_post = 0;
1640 LASSERT(lad->lad_post_result > 0);
1642 /* Wakeup the master engine to go ahead. */
1643 wake_up_all(&mthread->t_ctl_waitq);
1645 memset(lr, 0, sizeof(*lr));
1646 lr->lr_event = LE_PHASE1_DONE;
1647 lr->lr_status = lad->lad_post_result;
1648 rc = lfsck_assistant_notify_others(env, com, lr);
1650 CDEBUG(D_LFSCK, "%s: LFSCK assistant notified "
1651 "others for %s post: rc = %d\n",
1652 lfsck_lfsck2name(lfsck),
1656 if (lad->lad_to_double_scan) {
1657 lad->lad_to_double_scan = 0;
1658 atomic_inc(&lfsck->li_double_scan_count);
1659 lad->lad_in_double_scan = 1;
1660 wake_up_all(&mthread->t_ctl_waitq);
1662 com->lc_new_checked = 0;
1663 com->lc_new_scanned = 0;
1664 com->lc_time_last_checkpoint = cfs_time_current();
1665 com->lc_time_next_checkpoint =
1666 com->lc_time_last_checkpoint +
1667 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1669 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before "
1670 "the second-stage scaning\n",
1671 lfsck_lfsck2name(lfsck));
1673 /* Flush async updates before handling orphan. */
1674 rc2 = dt_sync(env, lfsck->li_next);
1676 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 "
1677 "scan start, synced: rc = %d\n",
1678 lfsck_lfsck2name(lfsck), rc2);
1680 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1681 GOTO(cleanup2, rc = 0);
1683 while (lad->lad_in_double_scan) {
1684 rc = lfsck_assistant_query_others(env, com);
1685 if (lfsck_phase2_next_ready(lad))
1691 /* Pull LFSCK status on related targets once
1692 * per 30 seconds if we are not notified. */
1693 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
1694 cfs_time_seconds(1),
1696 rc = l_wait_event(athread->t_ctl_waitq,
1697 lfsck_phase2_next_ready(lad) ||
1699 !thread_is_running(mthread),
1702 if (unlikely(lad->lad_exit ||
1703 !thread_is_running(mthread)))
1704 GOTO(cleanup2, rc = 0);
1706 if (rc == -ETIMEDOUT)
1713 rc = lao->la_handler_p2(env, com);
1717 if (unlikely(lad->lad_exit ||
1718 !thread_is_running(mthread)))
1719 GOTO(cleanup2, rc = 0);
1725 /* Cleanup the unfinished requests. */
1726 spin_lock(&lad->lad_lock);
1728 lad->lad_assistant_status = rc;
1730 if (lad->lad_exit && lad->lad_post_result <= 0)
1731 lao->la_fill_pos(env, com, &lfsck->li_pos_checkpoint);
1733 while (!list_empty(&lad->lad_req_list)) {
1734 lar = list_entry(lad->lad_req_list.next,
1735 struct lfsck_assistant_req,
1737 list_del_init(&lar->lar_list);
1738 lad->lad_prefetched--;
1739 spin_unlock(&lad->lad_lock);
1740 lao->la_req_fini(env, lar);
1741 spin_lock(&lad->lad_lock);
1743 spin_unlock(&lad->lad_lock);
1745 LASSERTF(lad->lad_prefetched == 0, "unmatched prefeteched objs %d\n",
1746 lad->lad_prefetched);
1749 memset(lr, 0, sizeof(*lr));
1751 lr->lr_event = LE_PHASE2_DONE;
1753 } else if (rc == 0) {
1754 if (lfsck->li_flags & LPF_ALL_TGT) {
1755 lr->lr_event = LE_STOP;
1756 lr->lr_status = LS_STOPPED;
1758 lr->lr_event = LE_PEER_EXIT;
1759 switch (lfsck->li_status) {
1762 lr->lr_status = LS_CO_PAUSED;
1766 lr->lr_status = LS_CO_STOPPED;
1769 CDEBUG(D_LFSCK, "%s: LFSCK assistant unknown "
1770 "status: rc = %d\n",
1771 lfsck_lfsck2name(lfsck),
1773 lr->lr_status = LS_CO_FAILED;
1778 if (lfsck->li_flags & LPF_ALL_TGT) {
1779 lr->lr_event = LE_STOP;
1780 lr->lr_status = LS_FAILED;
1782 lr->lr_event = LE_PEER_EXIT;
1783 lr->lr_status = LS_CO_FAILED;
1787 rc1 = lfsck_assistant_notify_others(env, com, lr);
1789 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to notify "
1790 "others for %s quit: rc = %d\n",
1791 lfsck_lfsck2name(lfsck), lad->lad_name, rc1);
1795 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before exit\n",
1796 lfsck_lfsck2name(lfsck));
1798 /* Flush async updates before exit. */
1799 rc2 = dt_sync(env, lfsck->li_next);
1801 CDEBUG(D_LFSCK, "%s: LFSCK assistant synced before exit: rc = %d\n",
1802 lfsck_lfsck2name(lfsck), rc2);
1804 /* Under force exit case, some requests may be just freed without
1805 * verification, those objects should be re-handled when next run.
1806 * So not update the on-disk trace file under such case. */
1807 if (lad->lad_in_double_scan) {
1809 rc1 = lao->la_double_scan_result(env, com, rc);
1811 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 scan "
1812 "finished: rc = %d\n",
1813 lfsck_lfsck2name(lfsck), rc1 != 0 ? rc1 : rc);
1817 if (lad->lad_in_double_scan)
1818 atomic_dec(&lfsck->li_double_scan_count);
1820 spin_lock(&lad->lad_lock);
1821 lad->lad_assistant_status = (rc1 != 0 ? rc1 : rc);
1822 thread_set_flags(athread, SVC_STOPPED);
1823 wake_up_all(&mthread->t_ctl_waitq);
1824 lad->lad_task = NULL;
1825 spin_unlock(&lad->lad_lock);
1827 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread exit: rc = %d\n",
1828 lfsck_lfsck2name(lfsck), lad->lad_name,
1829 lad->lad_assistant_status);
1831 lfsck_thread_args_fini(lta);