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, 2016, 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 lu_attr *la = &info->lti_la;
140 struct seq_server_site *ss = lfsck_dev_site(lfsck);
141 __u32 idx = lfsck_dev_idx(lfsck);
145 if (list_empty(&lfsck->li_list_dir) || !S_ISDIR(lfsck_object_type(obj)))
148 *fid = *lfsck_dto2fid(obj);
149 rc = dt_attr_get(env, obj, la);
150 if (unlikely(rc || (la->la_valid & LA_FLAGS &&
151 la->la_flags & LUSTRE_ORPHAN_FL))) {
152 /* Orphan directory is empty, does not need scan. */
154 "%s: skip orphan dir "DFID", %llx/%x: rc = %d\n",
155 lfsck_lfsck2name(lfsck), PFID(fid),
156 la->la_valid, la->la_flags, rc);
164 /* Global /ROOT is visible. */
165 if (unlikely(lu_fid_eq(fid, &lfsck->li_global_root_fid)))
168 /* Backend root is invisible. */
169 if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid)))
172 if (!fid_is_norm(fid) && !fid_is_igif(fid))
175 fld_range_set_mdt(range);
176 rc = fld_local_lookup(env, ss->ss_server_fld,
177 fid_seq(fid), range);
178 if (rc != 0 || range->lsr_index != idx)
179 /* Current FID should NOT be for the input parameter
180 * @obj, because the lfsck_master_oit_engine() has
181 * filtered out agent object. So current FID is for
182 * the ancestor of the original input parameter @obj.
183 * So the ancestor is a remote directory. The input
184 * parameter @obj is local directory, and should be
185 * scanned under such case. */
188 /* normal FID on this target (locally) must be for the
189 * client-side visiable object. */
190 if (fid_is_norm(fid))
193 /* Only true after "obj = NULL" set below */
195 obj = lfsck_object_find_bottom(env, lfsck, fid);
200 if (!dt_object_exists(obj))
204 dt_read_lock(env, obj, DT_TGT_CHILD);
205 if (unlikely(lfsck_is_dead_obj(obj))) {
206 dt_read_unlock(env, obj);
211 rc = dt_xattr_get(env, obj,
212 lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK);
213 dt_read_unlock(env, obj);
217 if (rc < 0 && rc != -ENODATA)
220 rc = lfsck_parent_fid(env, obj, fid);
222 lfsck_object_put(env, obj);
228 if (!fid_is_sane(fid))
233 if (depth > 0 && obj != NULL)
234 lfsck_object_put(env, obj);
239 static int lfsck_load_stripe_lmv(const struct lu_env *env,
240 struct lfsck_instance *lfsck,
241 struct dt_object *obj)
243 struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
244 struct lfsck_lmv *llmv;
248 LASSERT(lfsck->li_obj_dir == NULL);
249 LASSERT(lfsck->li_lmv == NULL);
251 rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv);
252 if (rc == -ENODATA) {
253 lfsck->li_obj_dir = lfsck_object_get(obj);
265 if (lmv->lmv_magic == LMV_MAGIC) {
266 struct lfsck_slave_lmv_rec *lslr;
269 llmv->ll_lmv_master = 1;
270 if (lmv->lmv_stripe_count < 1)
271 stripes = LFSCK_LMV_DEF_STRIPES;
272 else if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
273 stripes = LFSCK_LMV_MAX_STRIPES;
275 stripes = lmv->lmv_stripe_count;
277 OBD_ALLOC_PTR_ARRAY_LARGE(lslr, stripes);
284 llmv->ll_stripes_allocated = stripes;
285 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
286 llmv->ll_lslr = lslr;
288 llmv->ll_lmv_slave = 1;
291 lfsck->li_obj_dir = lfsck_object_get(obj);
293 atomic_set(&llmv->ll_ref, 1);
294 lfsck->li_lmv = llmv;
299 /* LFSCK wrap functions */
301 static void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
304 struct lfsck_component *com;
306 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
307 com->lc_ops->lfsck_fail(env, com, new_checked);
311 void lfsck_close_dir(const struct lu_env *env,
312 struct lfsck_instance *lfsck, int result)
314 struct lfsck_component *com;
317 if (lfsck->li_lmv != NULL) {
318 lfsck->li_lmv->ll_exit_value = result;
319 if (lfsck->li_obj_dir != NULL) {
320 list_for_each_entry(com, &lfsck->li_list_dir,
322 com->lc_ops->lfsck_close_dir(env, com);
326 lfsck_lmv_put(env, lfsck->li_lmv);
327 lfsck->li_lmv = NULL;
330 if (lfsck->li_di_dir != NULL) {
331 const struct dt_it_ops *dir_iops;
332 struct dt_it *dir_di = lfsck->li_di_dir;
334 LASSERT(lfsck->li_obj_dir != NULL);
336 dir_iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
337 lfsck_di_dir_put(env, lfsck);
338 dir_iops->fini(env, dir_di);
341 if (lfsck->li_obj_dir != NULL) {
342 struct dt_object *dir_obj = lfsck->li_obj_dir;
344 lfsck->li_obj_dir = NULL;
345 lfsck_object_put(env, dir_obj);
351 int lfsck_open_dir(const struct lu_env *env,
352 struct lfsck_instance *lfsck, __u64 cookie)
354 struct dt_object *obj = lfsck->li_obj_dir;
355 struct dt_it *di = lfsck->li_di_dir;
356 struct lfsck_component *com;
357 const struct dt_it_ops *iops;
361 LASSERT(obj != NULL);
364 if (unlikely(!dt_try_as_dir(env, obj)))
365 GOTO(out, rc = -ENOTDIR);
367 list_for_each_entry(com, &lfsck->li_list_dir, lc_link_dir) {
368 rc = com->lc_ops->lfsck_open_dir(env, com);
373 iops = &obj->do_index_ops->dio_it;
374 di = iops->init(env, obj, lfsck->li_args_dir);
376 GOTO(out, rc = PTR_ERR(di));
378 rc = iops->load(env, di, cookie);
381 else if (rc == 0 || (rc > 0 && cookie > 0))
382 rc = iops->next(env, di);
390 lfsck->li_cookie_dir = iops->store(env, di);
391 spin_lock(&lfsck->li_lock);
392 lfsck->li_di_dir = di;
393 spin_unlock(&lfsck->li_lock);
400 lfsck_close_dir(env, lfsck, rc);
405 static int lfsck_checkpoint(const struct lu_env *env,
406 struct lfsck_instance *lfsck)
408 struct lfsck_component *com;
412 if (likely(ktime_get_seconds() <= lfsck->li_time_next_checkpoint))
415 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
416 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
417 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
422 lfsck->li_time_last_checkpoint = ktime_get_seconds();
423 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
424 LFSCK_CHECKPOINT_INTERVAL;
425 return rc1 != 0 ? rc1 : rc;
428 static int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
429 struct lfsck_start_param *lsp)
431 struct dt_object *obj = NULL;
432 struct lfsck_component *com;
433 struct lfsck_component *next;
434 struct lfsck_position *pos = NULL;
435 const struct dt_it_ops *iops =
436 &lfsck->li_obj_oit->do_index_ops->dio_it;
440 LASSERT(lfsck->li_obj_dir == NULL);
441 LASSERT(lfsck->li_di_dir == NULL);
443 lfsck->li_current_oit_processed = 0;
444 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
445 com->lc_new_checked = 0;
446 rc = com->lc_ops->lfsck_prep(env, com, lsp);
451 (!lfsck_pos_is_zero(&com->lc_pos_start) &&
452 lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
453 pos = &com->lc_pos_start;
456 /* Init otable-based iterator. */
458 rc = iops->load(env, lfsck->li_di_oit, 0);
459 if (rc > 0 || unlikely(rc == -ENODATA)) {
460 lfsck->li_oit_over = 1;
467 rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
468 if (rc > 0 || unlikely(rc == -ENODATA))
469 lfsck->li_oit_over = 1;
473 if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
476 /* Find the directory for namespace-based traverse. */
477 obj = lfsck_object_find_bottom(env, lfsck, &pos->lp_dir_parent);
479 RETURN(PTR_ERR(obj));
481 /* Remote directory will be scanned by the LFSCK instance
482 * on the MDT where the remote object really resides on. */
483 if (!dt_object_exists(obj) || dt_object_remote(obj) ||
484 unlikely(!S_ISDIR(lfsck_object_type(obj))))
487 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
489 /* For the master MDT-object of a striped directory,
490 * reset the iteration from the directory beginning. */
491 if (lfsck->li_lmv != NULL && lfsck->li_lmv->ll_lmv_master)
492 pos->lp_dir_cookie = 0;
494 rc = lfsck_open_dir(env, lfsck, pos->lp_dir_cookie);
496 /* The end of the directory. */
504 lfsck_object_put(env, obj);
507 lfsck_close_dir(env, lfsck, rc);
508 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
510 com->lc_ops->lfsck_post(env, com, rc, true);
517 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, true);
518 lfsck->li_pos_current = lfsck->li_pos_checkpoint;
519 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
520 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
525 lfsck->li_time_last_checkpoint = ktime_get_seconds();
526 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
527 LFSCK_CHECKPOINT_INTERVAL;
531 static int lfsck_exec_oit(const struct lu_env *env,
532 struct lfsck_instance *lfsck, struct dt_object *obj)
534 struct lfsck_component *com;
538 LASSERT(lfsck->li_obj_dir == NULL);
540 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
541 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
546 rc = lfsck_needs_scan_dir(env, lfsck, obj);
550 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
552 rc = lfsck_open_dir(env, lfsck, 0);
558 lfsck_fail(env, lfsck, false);
561 lfsck_close_dir(env, lfsck, rc);
563 return rc > 0 ? 0 : rc;
566 static int lfsck_exec_dir(const struct lu_env *env,
567 struct lfsck_instance *lfsck,
568 struct lfsck_assistant_object *lso,
569 struct lu_dirent *ent, __u16 type)
571 struct lfsck_component *com;
574 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
575 rc = com->lc_ops->lfsck_exec_dir(env, com, lso, ent, type);
582 static int lfsck_master_dir_engine(const struct lu_env *env,
583 struct lfsck_instance *lfsck);
585 static int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
588 struct lfsck_component *com;
589 struct lfsck_component *next;
592 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
593 lfsck_close_dir(env, lfsck, result);
595 while (thread_is_running(&lfsck->li_thread) && rc > 0 &&
596 !list_empty(&lfsck->li_list_lmv)) {
597 struct lfsck_lmv_unit *llu;
599 spin_lock(&lfsck->li_lock);
600 llu = list_entry(lfsck->li_list_lmv.next,
601 struct lfsck_lmv_unit, llu_link);
602 list_del_init(&llu->llu_link);
603 spin_unlock(&lfsck->li_lock);
605 lfsck->li_lmv = &llu->llu_lmv;
606 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
607 rc = lfsck_open_dir(env, lfsck, 0);
609 rc = lfsck_master_dir_engine(env, lfsck);
610 lfsck_close_dir(env, lfsck, result);
616 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
617 rc = com->lc_ops->lfsck_post(env, com, result, false);
619 CDEBUG(D_LFSCK, "%s: lfsck_post at the component %u: "
620 "rc = %d\n", lfsck_lfsck2name(lfsck),
621 (__u32)com->lc_type, rc);
624 lfsck->li_time_last_checkpoint = ktime_get_seconds();
625 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
626 LFSCK_CHECKPOINT_INTERVAL;
628 /* Ignore some component post failure to make other can go ahead. */
632 static int lfsck_double_scan(const struct lu_env *env,
633 struct lfsck_instance *lfsck)
635 struct lfsck_component *com;
636 struct lfsck_component *next;
640 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
641 rc = com->lc_ops->lfsck_double_scan(env, com);
646 wait_event_idle(lfsck->li_thread.t_ctl_waitq,
647 atomic_read(&lfsck->li_double_scan_count) == 0);
649 if (lfsck->li_status != LS_PAUSED &&
650 lfsck->li_status != LS_CO_PAUSED) {
651 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
653 spin_lock(&lfsck->li_lock);
654 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
655 spin_unlock(&lfsck->li_lock);
659 return rc1 != 0 ? rc1 : rc;
662 static void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck)
664 struct lfsck_component *com;
665 struct lfsck_component *next;
667 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
669 if (com->lc_ops->lfsck_quit != NULL)
670 com->lc_ops->lfsck_quit(env, com);
672 spin_lock(&lfsck->li_lock);
673 list_del_init(&com->lc_link_dir);
674 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
675 spin_unlock(&lfsck->li_lock);
678 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
680 if (com->lc_ops->lfsck_quit != NULL)
681 com->lc_ops->lfsck_quit(env, com);
683 spin_lock(&lfsck->li_lock);
684 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
685 spin_unlock(&lfsck->li_lock);
691 static int lfsck_master_dir_engine(const struct lu_env *env,
692 struct lfsck_instance *lfsck)
694 struct lfsck_thread_info *info = lfsck_env_info(env);
695 struct dt_object *dir = lfsck->li_obj_dir;
696 const struct dt_it_ops *iops = &dir->do_index_ops->dio_it;
697 struct dt_it *di = lfsck->li_di_dir;
698 struct lu_dirent *ent =
699 (struct lu_dirent *)info->lti_key;
700 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
701 struct ptlrpc_thread *thread = &lfsck->li_thread;
702 struct lfsck_assistant_object *lso = NULL;
708 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY2, cfs_fail_val) &&
709 unlikely(!thread_is_running(thread))) {
710 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
711 "parent "DFID", cookie %#llx\n",
712 lfsck_lfsck2name(lfsck),
713 PFID(lfsck_dto2fid(dir)), lfsck->li_cookie_dir);
718 lfsck->li_new_scanned++;
719 rc = iops->rec(env, di, (struct dt_rec *)ent,
722 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
726 CDEBUG(D_LFSCK, "%s: scan dir failed at rec(), "
727 "parent "DFID", cookie %#llx: rc = %d\n",
728 lfsck_lfsck2name(lfsck),
729 PFID(lfsck_dto2fid(dir)),
730 lfsck->li_cookie_dir, rc);
731 lfsck_fail(env, lfsck, true);
732 if (bk->lb_param & LPF_FAILOUT)
738 if (ent->lde_attrs & LUDA_IGNORE)
741 /* skip dot entry. */
742 if (ent->lde_namelen == 1 && ent->lde_name[0] == '.')
746 lso = lfsck_assistant_object_init(env,
747 lfsck_dto2fid(dir), NULL,
748 lfsck->li_pos_current.lp_oit_cookie, true);
750 if (bk->lb_param & LPF_FAILOUT)
751 RETURN(PTR_ERR(lso));
758 /* The type in the @ent structure may has been overwritten,
759 * so we need to pass the @type parameter independently. */
760 rc = lfsck_exec_dir(env, lfsck, lso, ent, type);
761 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
765 rc = lfsck_checkpoint(env, lfsck);
766 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
770 lfsck_control_speed(lfsck);
771 if (unlikely(!thread_is_running(thread))) {
772 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
773 "parent "DFID", cookie %#llx\n",
774 lfsck_lfsck2name(lfsck),
775 PFID(lfsck_dto2fid(dir)),
776 lfsck->li_cookie_dir);
780 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
781 spin_lock(&lfsck->li_lock);
782 thread_set_flags(thread, SVC_STOPPING);
783 spin_unlock(&lfsck->li_lock);
784 GOTO(out, rc = -EINVAL);
787 rc = iops->next(env, di);
789 CDEBUG(D_LFSCK, "%s dir engine fail to locate next "
790 "for the directory "DFID": rc = %d\n",
791 lfsck_lfsck2name(lfsck),
792 PFID(&lfsck->li_pos_current.lp_dir_parent), rc);
795 if (rc > 0 && !lfsck->li_oit_over)
796 lfsck_close_dir(env, lfsck, rc);
802 lfsck_assistant_object_put(env, lso);
808 * Object-table based iteration engine.
810 * Object-table based iteration is the basic linear engine to scan all the
811 * objects on current device in turn. For each object, it calls all the
812 * registered LFSCK component(s)' API to perform related consistency
815 * It flushes related LFSCK trace files to disk via making checkpoint
816 * periodically. Then if the server crashed or the LFSCK is paused, the
817 * LFSCK can resume from the latest checkpoint.
819 * It also controls the whole LFSCK speed via lfsck_control_speed() to
820 * avoid the server to become overload.
822 * \param[in] env pointer to the thread context
823 * \param[in] lfsck pointer to the lfsck instance
825 * \retval positive number if all objects have been scanned
826 * \retval 0 if the iteration is stopped or paused
827 * \retval negative error number on failure
829 static int lfsck_master_oit_engine(const struct lu_env *env,
830 struct lfsck_instance *lfsck)
832 struct lfsck_thread_info *info = lfsck_env_info(env);
833 const struct dt_it_ops *iops =
834 &lfsck->li_obj_oit->do_index_ops->dio_it;
835 struct dt_it *di = lfsck->li_di_oit;
836 struct lu_fid *fid = &info->lti_fid;
837 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
838 struct ptlrpc_thread *thread = &lfsck->li_thread;
839 struct seq_server_site *ss = lfsck_dev_site(lfsck);
840 __u32 idx = lfsck_dev_idx(lfsck);
844 if (unlikely(ss == NULL))
848 struct dt_object *target;
850 if (lfsck->li_di_dir != NULL) {
851 rc = lfsck_master_dir_engine(env, lfsck);
856 if (unlikely(lfsck->li_oit_over))
859 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY1, cfs_fail_val) &&
860 unlikely(!thread_is_running(thread))) {
861 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
863 lfsck_lfsck2name(lfsck), iops->store(env, di));
868 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
871 lfsck->li_current_oit_processed = 1;
873 if (!list_empty(&lfsck->li_list_lmv)) {
874 struct lfsck_lmv_unit *llu;
876 spin_lock(&lfsck->li_lock);
877 llu = list_entry(lfsck->li_list_lmv.next,
878 struct lfsck_lmv_unit, llu_link);
879 list_del_init(&llu->llu_link);
880 spin_unlock(&lfsck->li_lock);
882 lfsck->li_lmv = &llu->llu_lmv;
883 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
884 rc = lfsck_open_dir(env, lfsck, 0);
886 rc = lfsck_master_dir_engine(env, lfsck);
892 lfsck->li_new_scanned++;
893 lfsck->li_pos_current.lp_oit_cookie = iops->store(env, di);
894 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
896 CDEBUG(D_LFSCK, "%s: OIT scan failed at rec(): "
897 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
898 lfsck_fail(env, lfsck, true);
899 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
905 if (unlikely(!fid_is_sane(fid))) {
906 CDEBUG(D_LFSCK, "%s: OIT scan find invalid FID "DFID
908 lfsck_lfsck2name(lfsck), PFID(fid));
912 if (fid_is_idif(fid)) {
913 __u32 idx1 = fid_idif_ost_idx(fid);
915 LASSERT(!lfsck->li_master);
918 struct ost_id *oi = &info->lti_oi;
920 if (unlikely(idx1 != 0)) {
921 CDEBUG(D_LFSCK, "%s: invalid IDIF "DFID
922 ", not match device index %u\n",
923 lfsck_lfsck2name(lfsck),
929 /* rebuild the IDIF with index to
930 * avoid double instances for the
932 fid_to_ostid(fid, oi);
933 ostid_to_fid(fid, oi, idx);
935 } else if (!fid_is_norm(fid) && !fid_is_igif(fid) &&
936 !fid_is_last_id(fid) &&
937 !lu_fid_eq(fid, &lfsck->li_global_root_fid)) {
939 /* If the FID/object is only used locally and invisible
940 * to external nodes, then LFSCK will not handle it.
942 * dot_lustre sequence has been handled specially. */
945 struct lu_seq_range *range = &info->lti_range;
947 if (lfsck->li_master)
948 fld_range_set_mdt(range);
950 fld_range_set_ost(range);
951 rc = fld_local_lookup(env, ss->ss_server_fld,
952 fid_seq(fid), range);
953 if (rc != 0 || range->lsr_index != idx) {
954 /* Remote object will be handled by the LFSCK
955 * instance on the MDT where the remote object
956 * really resides on. */
962 target = lfsck_object_find_bottom(env, lfsck, fid);
963 if (IS_ERR(target)) {
964 CDEBUG(D_LFSCK, "%s: OIT scan failed at find target "
965 DFID", cookie %llu: rc = %d\n",
966 lfsck_lfsck2name(lfsck), PFID(fid),
967 iops->store(env, di), rc);
968 lfsck_fail(env, lfsck, true);
969 if (bk->lb_param & LPF_FAILOUT)
970 RETURN(PTR_ERR(target));
975 if (dt_object_exists(target))
976 rc = lfsck_exec_oit(env, lfsck, target);
978 lfsck_object_put(env, target);
979 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
983 rc = lfsck_checkpoint(env, lfsck);
984 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
988 lfsck_control_speed(lfsck);
990 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
991 spin_lock(&lfsck->li_lock);
992 thread_set_flags(thread, SVC_STOPPING);
993 spin_unlock(&lfsck->li_lock);
997 rc = iops->next(env, di);
998 if (unlikely(rc > 0))
999 lfsck->li_oit_over = 1;
1000 else if (likely(rc == 0))
1001 lfsck->li_current_oit_processed = 0;
1003 CDEBUG(D_LFSCK, "%s oit engine fail to locate next at "
1004 "%llu: rc = %d\n", lfsck_lfsck2name(lfsck),
1005 iops->store(env, di), rc);
1007 if (unlikely(!thread_is_running(thread))) {
1008 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
1009 "cookie %llu\n", lfsck_lfsck2name(lfsck),
1010 iops->store(env, di));
1013 } while (rc == 0 || lfsck->li_di_dir != NULL);
1018 int lfsck_master_engine(void *args)
1020 struct lfsck_thread_args *lta = args;
1021 struct lu_env *env = <a->lta_env;
1022 struct lfsck_instance *lfsck = lta->lta_lfsck;
1023 struct ptlrpc_thread *thread = &lfsck->li_thread;
1024 struct dt_object *oit_obj = lfsck->li_obj_oit;
1025 const struct dt_it_ops *oit_iops = &oit_obj->do_index_ops->dio_it;
1026 struct dt_it *oit_di;
1030 spin_lock(&lfsck->li_lock);
1031 lfsck->li_task = current;
1032 spin_unlock(&lfsck->li_lock);
1034 /* There will be some objects verification during the LFSCK start,
1035 * such as the subsequent lfsck_verify_lpf(). Trigger low layer OI
1036 * OI scrub before that to handle the potential inconsistence. */
1037 oit_di = oit_iops->init(env, oit_obj, lfsck->li_args_oit);
1038 if (IS_ERR(oit_di)) {
1039 rc = PTR_ERR(oit_di);
1040 CDEBUG(D_LFSCK, "%s: master engine fail to init iteration: "
1041 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1043 GOTO(fini_args, rc);
1046 if (lfsck->li_master &&
1047 (!list_empty(&lfsck->li_list_scan) ||
1048 !list_empty(&lfsck->li_list_double_scan))) {
1049 rc = lfsck_verify_lpf(env, lfsck);
1050 /* Fail to verify the .lustre/lost+found/MDTxxxx/ may be not
1051 * fatal, because the .lustre/lost+found/ maybe not accessed
1052 * by the LFSCK if it does not add orphans or others to such
1053 * directory. So go ahead until hit failure when really uses
1056 CDEBUG(D_LFSCK, "%s: master engine fail to verify the "
1057 ".lustre/lost+found/, go ahead: rc = %d\n",
1058 lfsck_lfsck2name(lfsck), rc);
1061 spin_lock(&lfsck->li_lock);
1062 lfsck->li_di_oit = oit_di;
1063 spin_unlock(&lfsck->li_lock);
1064 rc = lfsck_prep(env, lfsck, lta->lta_lsp);
1068 CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = %#x, dir_flags = %#x, "
1069 "oit_cookie = %llu, dir_cookie = %#llx, parent = "DFID
1070 ", pid = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1071 lfsck->li_pos_checkpoint.lp_oit_cookie,
1072 lfsck->li_pos_checkpoint.lp_dir_cookie,
1073 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1076 spin_lock(&lfsck->li_lock);
1077 if (unlikely(!thread_is_starting(thread))) {
1078 spin_unlock(&lfsck->li_lock);
1079 GOTO(fini_oit, rc = 0);
1082 thread_set_flags(thread, SVC_RUNNING);
1083 spin_unlock(&lfsck->li_lock);
1084 wake_up_all(&thread->t_ctl_waitq);
1086 wait_event_idle(thread->t_ctl_waitq,
1087 lfsck->li_start_unplug ||
1088 !thread_is_running(thread));
1089 if (!thread_is_running(thread))
1090 GOTO(fini_oit, rc = 0);
1092 if (!list_empty(&lfsck->li_list_scan) ||
1093 list_empty(&lfsck->li_list_double_scan))
1094 rc = lfsck_master_oit_engine(env, lfsck);
1098 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
1099 CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = %#x, dir_flags = %#x, "
1100 "oit_cookie = %llu, dir_cookie = %#llx, parent = "DFID
1101 ", pid = %d, rc = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1102 lfsck->li_pos_checkpoint.lp_oit_cookie,
1103 lfsck->li_pos_checkpoint.lp_dir_cookie,
1104 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1107 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
1108 rc = lfsck_post(env, lfsck, rc);
1110 lfsck_close_dir(env, lfsck, rc);
1113 lfsck_di_oit_put(env, lfsck);
1114 oit_iops->fini(env, oit_di);
1116 if (!list_empty(&lfsck->li_list_double_scan))
1117 rc = lfsck_double_scan(env, lfsck);
1121 lfsck_quit(env, lfsck);
1124 /* XXX: Purge the pinned objects in the future. */
1127 spin_lock(&lfsck->li_lock);
1128 thread_set_flags(thread, SVC_STOPPED);
1129 lfsck->li_task = NULL;
1130 spin_unlock(&lfsck->li_lock);
1131 wake_up_all(&thread->t_ctl_waitq);
1132 lfsck_thread_args_fini(lta);
1136 static inline bool lfsck_assistant_req_empty(struct lfsck_assistant_data *lad)
1140 spin_lock(&lad->lad_lock);
1141 if (list_empty(&lad->lad_req_list))
1143 spin_unlock(&lad->lad_lock);
1149 * Query the LFSCK status from the instatnces on remote servers.
1151 * The LFSCK assistant thread queries the LFSCK instances on other
1152 * servers (MDT/OST) about their status, such as whether they have
1153 * finished the phase1/phase2 scanning or not, and so on.
1155 * \param[in] env pointer to the thread context
1156 * \param[in] com pointer to the lfsck component
1158 * \retval 0 for success
1159 * \retval negative error number on failure
1161 static int lfsck_assistant_query_others(const struct lu_env *env,
1162 struct lfsck_component *com)
1164 struct lfsck_thread_info *info = lfsck_env_info(env);
1165 struct lfsck_request *lr = &info->lti_lr;
1166 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1167 struct lfsck_instance *lfsck = com->lc_lfsck;
1168 struct lfsck_assistant_data *lad = com->lc_data;
1169 struct ptlrpc_request_set *set;
1170 struct lfsck_tgt_descs *ltds;
1171 struct lfsck_tgt_desc *ltd;
1172 struct list_head *phase_head;
1177 set = ptlrpc_prep_set();
1181 lad->lad_touch_gen++;
1182 memset(lr, 0, sizeof(*lr));
1183 lr->lr_event = LE_QUERY;
1184 lr->lr_active = com->lc_type;
1186 memset(laia, 0, sizeof(*laia));
1187 laia->laia_com = com;
1190 if (!list_empty(&lad->lad_mdt_phase1_list)) {
1191 ltds = &lfsck->li_mdt_descs;
1193 phase_head = &lad->lad_mdt_phase1_list;
1194 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1199 ltds = &lfsck->li_ost_descs;
1200 lr->lr_flags = LEF_TO_OST;
1201 phase_head = &lad->lad_ost_phase1_list;
1204 laia->laia_ltds = ltds;
1205 spin_lock(<ds->ltd_lock);
1206 while (!list_empty(phase_head)) {
1207 struct list_head *phase_list;
1210 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1211 ltd = list_entry(phase_head->next,
1212 struct lfsck_tgt_desc,
1213 ltd_layout_phase_list);
1214 phase_list = <d->ltd_layout_phase_list;
1215 gen = <d->ltd_layout_gen;
1217 ltd = list_entry(phase_head->next,
1218 struct lfsck_tgt_desc,
1219 ltd_namespace_phase_list);
1220 phase_list = <d->ltd_namespace_phase_list;
1221 gen = <d->ltd_namespace_gen;
1224 if (*gen == lad->lad_touch_gen)
1227 *gen = lad->lad_touch_gen;
1228 list_move_tail(phase_list, phase_head);
1229 atomic_inc(<d->ltd_ref);
1230 laia->laia_ltd = ltd;
1231 spin_unlock(<ds->ltd_lock);
1232 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1233 lfsck_async_interpret_common,
1236 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to query "
1237 "%s %x for %s: rc = %d\n",
1238 lfsck_lfsck2name(lfsck),
1239 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1240 ltd->ltd_index, lad->lad_name, rc);
1244 spin_lock(<ds->ltd_lock);
1246 spin_unlock(<ds->ltd_lock);
1248 rc = ptlrpc_set_wait(env, set);
1250 ptlrpc_set_destroy(set);
1254 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST) &&
1255 list_empty(&lad->lad_mdt_phase1_list))
1259 ptlrpc_set_destroy(set);
1261 RETURN(rc1 != 0 ? rc1 : rc);
1265 * Notify the LFSCK event to the instances on remote servers.
1267 * The LFSCK assistant thread notifies the LFSCK instances on other
1268 * servers (MDT/OST) about some events, such as start new scanning,
1269 * stop the scanning, this LFSCK instance will exit, and so on.
1271 * \param[in] env pointer to the thread context
1272 * \param[in] com pointer to the lfsck component
1273 * \param[in] lr pointer to the LFSCK event request
1275 * \retval 0 for success
1276 * \retval negative error number on failure
1278 static int lfsck_assistant_notify_others(const struct lu_env *env,
1279 struct lfsck_component *com,
1280 struct lfsck_request *lr)
1282 struct lfsck_thread_info *info = lfsck_env_info(env);
1283 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1284 struct lfsck_instance *lfsck = com->lc_lfsck;
1285 struct lfsck_assistant_data *lad = com->lc_data;
1286 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1287 struct ptlrpc_request_set *set;
1288 struct lfsck_tgt_descs *ltds;
1289 struct lfsck_tgt_desc *ltd;
1290 struct lfsck_tgt_desc *next;
1296 set = ptlrpc_prep_set();
1300 lr->lr_index = lfsck_dev_idx(lfsck);
1301 lr->lr_active = com->lc_type;
1303 memset(laia, 0, sizeof(*laia));
1304 laia->laia_com = com;
1307 switch (lr->lr_event) {
1309 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1312 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN;
1313 lr->lr_speed = bk->lb_speed_limit;
1314 lr->lr_version = bk->lb_version;
1315 lr->lr_param |= bk->lb_param;
1316 lr->lr_async_windows = bk->lb_async_windows;
1317 lr->lr_flags = LEF_TO_OST;
1319 /* Notify OSTs firstly, then handle other MDTs if needed. */
1320 ltds = &lfsck->li_ost_descs;
1321 laia->laia_ltds = ltds;
1322 down_read(<ds->ltd_rw_sem);
1323 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1324 ltd = lfsck_tgt_get(ltds, idx);
1325 LASSERT(ltd != NULL);
1327 laia->laia_ltd = ltd;
1328 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1329 lfsck_async_interpret_common,
1330 laia, LFSCK_NOTIFY);
1332 lfsck_lad_set_bitmap(env, com, idx);
1333 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1334 "notify OST %x for %s start: rc = %d\n",
1335 lfsck_lfsck2name(lfsck), idx,
1340 up_read(<ds->ltd_rw_sem);
1343 rc = ptlrpc_set_wait(env, set);
1345 ptlrpc_set_destroy(set);
1350 if (!(bk->lb_param & LPF_ALL_TGT))
1353 /* link other MDT targets locallly. */
1354 ltds = &lfsck->li_mdt_descs;
1355 spin_lock(<ds->ltd_lock);
1356 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1357 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1358 ltd = lfsck_ltd2tgt(ltds, idx);
1359 LASSERT(ltd != NULL);
1361 if (!list_empty(<d->ltd_layout_list))
1364 list_add_tail(<d->ltd_layout_list,
1365 &lad->lad_mdt_list);
1366 list_add_tail(<d->ltd_layout_phase_list,
1367 &lad->lad_mdt_phase1_list);
1370 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1371 ltd = lfsck_ltd2tgt(ltds, idx);
1372 LASSERT(ltd != NULL);
1374 if (!list_empty(<d->ltd_namespace_list))
1377 list_add_tail(<d->ltd_namespace_list,
1378 &lad->lad_mdt_list);
1379 list_add_tail(<d->ltd_namespace_phase_list,
1380 &lad->lad_mdt_phase1_list);
1383 spin_unlock(<ds->ltd_lock);
1386 case LE_PHASE2_DONE:
1387 case LE_PEER_EXIT: {
1388 struct list_head *phase_head;
1390 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1391 if (bk->lb_param & LPF_ALL_TGT) {
1392 phase_head = &lad->lad_mdt_list;
1393 ltds = &lfsck->li_mdt_descs;
1394 if (lr->lr_event == LE_STOP) {
1395 /* unlink other MDT targets locallly. */
1396 spin_lock(<ds->ltd_lock);
1397 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1398 list_for_each_entry_safe(ltd, next,
1399 phase_head, ltd_layout_list) {
1401 <d->ltd_layout_phase_list);
1403 <d->ltd_layout_list);
1406 list_for_each_entry_safe(ltd, next,
1408 ltd_namespace_list) {
1410 <d->ltd_namespace_phase_list);
1412 <d->ltd_namespace_list);
1415 spin_unlock(<ds->ltd_lock);
1417 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1420 lr->lr_flags |= LEF_TO_OST;
1421 phase_head = &lad->lad_ost_list;
1422 ltds = &lfsck->li_ost_descs;
1424 lr->lr_flags &= ~LEF_TO_OST;
1426 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1429 lr->lr_flags |= LEF_TO_OST;
1430 phase_head = &lad->lad_ost_list;
1431 ltds = &lfsck->li_ost_descs;
1435 laia->laia_ltds = ltds;
1436 spin_lock(<ds->ltd_lock);
1437 while (!list_empty(phase_head)) {
1438 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1439 ltd = list_entry(phase_head->next,
1440 struct lfsck_tgt_desc,
1442 if (!list_empty(<d->ltd_layout_phase_list))
1444 <d->ltd_layout_phase_list);
1445 list_del_init(<d->ltd_layout_list);
1447 ltd = list_entry(phase_head->next,
1448 struct lfsck_tgt_desc,
1449 ltd_namespace_list);
1450 if (!list_empty(<d->ltd_namespace_phase_list))
1452 <d->ltd_namespace_phase_list);
1453 list_del_init(<d->ltd_namespace_list);
1455 atomic_inc(<d->ltd_ref);
1456 laia->laia_ltd = ltd;
1457 spin_unlock(<ds->ltd_lock);
1458 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1459 lfsck_async_interpret_common,
1460 laia, LFSCK_NOTIFY);
1462 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1463 "notify %s %x for %s stop/phase2_done/"
1464 "peer_exit: rc = %d\n",
1465 lfsck_lfsck2name(lfsck),
1466 (lr->lr_flags & LEF_TO_OST) ?
1467 "OST" : "MDT", ltd->ltd_index,
1471 spin_lock(<ds->ltd_lock);
1473 spin_unlock(<ds->ltd_lock);
1475 rc = ptlrpc_set_wait(env, set);
1477 ptlrpc_set_destroy(set);
1481 if (com->lc_type == LFSCK_TYPE_LAYOUT &&
1482 !(lr->lr_flags & LEF_TO_OST)) {
1483 lr->lr_flags |= LEF_TO_OST;
1484 phase_head = &lad->lad_ost_list;
1485 ltds = &lfsck->li_ost_descs;
1490 case LE_PHASE1_DONE:
1491 lad->lad_ops->la_sync_failures(env, com, lr);
1492 lad->lad_touch_gen++;
1493 ltds = &lfsck->li_mdt_descs;
1494 laia->laia_ltds = ltds;
1495 spin_lock(<ds->ltd_lock);
1496 while (!list_empty(&lad->lad_mdt_list)) {
1497 struct list_head *list;
1500 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1501 ltd = list_entry(lad->lad_mdt_list.next,
1502 struct lfsck_tgt_desc,
1504 list = <d->ltd_layout_list;
1505 gen = <d->ltd_layout_gen;
1507 struct lfsck_namespace *ns = com->lc_file_ram;
1509 ltd = list_entry(lad->lad_mdt_list.next,
1510 struct lfsck_tgt_desc,
1511 ltd_namespace_list);
1512 list = <d->ltd_namespace_list;
1513 gen = <d->ltd_namespace_gen;
1514 lr->lr_flags2 = ns->ln_flags & ~LF_INCOMPLETE;
1517 if (*gen == lad->lad_touch_gen)
1520 *gen = lad->lad_touch_gen;
1521 list_move_tail(list, &lad->lad_mdt_list);
1522 if (ltd->ltd_synced_failures)
1525 atomic_inc(<d->ltd_ref);
1526 laia->laia_ltd = ltd;
1527 spin_unlock(<ds->ltd_lock);
1528 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1529 lfsck_async_interpret_common,
1530 laia, LFSCK_NOTIFY);
1532 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1533 "notify MDT %x for %s phase1 done: "
1534 "rc = %d\n", lfsck_lfsck2name(lfsck),
1535 ltd->ltd_index, lad->lad_name, rc);
1538 spin_lock(<ds->ltd_lock);
1540 spin_unlock(<ds->ltd_lock);
1543 CDEBUG(D_LFSCK, "%s: LFSCK assistant unexpected LFSCK event: "
1544 "rc = %d\n", lfsck_lfsck2name(lfsck), lr->lr_event);
1549 rc1 = ptlrpc_set_wait(env, set);
1550 ptlrpc_set_destroy(set);
1552 RETURN(rc != 0 ? rc : rc1);
1556 * The LFSCK assistant thread is triggered by the LFSCK main engine.
1557 * They co-work together as an asynchronous pipeline: the LFSCK main
1558 * engine scans the system and pre-fetches the objects, attributes,
1559 * or name entries, etc, and pushes them into the pipeline as input
1560 * requests for the LFSCK assistant thread; on the other end of the
1561 * pipeline, the LFSCK assistant thread performs the real check and
1562 * repair for every request from the main engine.
1564 * Generally, the assistant engine may be blocked when check/repair
1565 * something, so the LFSCK main engine will run some faster. On the
1566 * other hand, the LFSCK main engine will drive multiple assistant
1567 * threads in parallel, means for each LFSCK component on the master
1568 * (such as layout LFSCK, namespace LFSCK), there is an independent
1569 * LFSCK assistant thread. So under such 1:N multiple asynchronous
1570 * pipelines mode, the whole LFSCK performance will be much better
1571 * than check/repair everything by the LFSCK main engine itself.
1573 int lfsck_assistant_engine(void *args)
1575 struct lfsck_thread_args *lta = args;
1576 struct lu_env *env = <a->lta_env;
1577 struct lfsck_component *com = lta->lta_com;
1578 struct lfsck_instance *lfsck = lta->lta_lfsck;
1579 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1580 struct lfsck_position *pos = &com->lc_pos_start;
1581 struct lfsck_thread_info *info = lfsck_env_info(env);
1582 struct lfsck_request *lr = &info->lti_lr;
1583 struct lfsck_assistant_data *lad = com->lc_data;
1584 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1585 struct ptlrpc_thread *athread = &lad->lad_thread;
1586 struct lfsck_assistant_operations *lao = lad->lad_ops;
1587 struct lfsck_assistant_req *lar;
1593 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread start\n",
1594 lfsck_lfsck2name(lfsck), lad->lad_name);
1596 memset(lr, 0, sizeof(*lr));
1597 lr->lr_event = LE_START;
1598 if (pos->lp_oit_cookie <= 1)
1599 lr->lr_param = LPF_RESET;
1600 rc = lfsck_assistant_notify_others(env, com, lr);
1602 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to notify others "
1603 "to start %s: rc = %d\n",
1604 lfsck_lfsck2name(lfsck), lad->lad_name, rc);
1608 spin_lock(&lad->lad_lock);
1609 lad->lad_task = current;
1610 thread_set_flags(athread, SVC_RUNNING);
1611 spin_unlock(&lad->lad_lock);
1612 wake_up_all(&mthread->t_ctl_waitq);
1615 while (!list_empty(&lad->lad_req_list)) {
1616 bool wakeup = false;
1618 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags) ||
1619 !thread_is_running(mthread)))
1620 GOTO(cleanup, rc = lad->lad_post_result);
1622 lar = list_entry(lad->lad_req_list.next,
1623 struct lfsck_assistant_req,
1625 /* Only the lfsck_assistant_engine thread itself can
1626 * remove the "lar" from the head of the list, LFSCK
1627 * engine thread only inserts other new "lar" at the
1628 * end of the list. So it is safe to handle current
1629 * "lar" without the spin_lock. */
1630 rc = lao->la_handler_p1(env, com, lar);
1631 spin_lock(&lad->lad_lock);
1632 list_del_init(&lar->lar_list);
1633 lad->lad_prefetched--;
1634 /* Wake up the main engine thread only when the list
1635 * is empty or half of the prefetched items have been
1636 * handled to avoid too frequent thread schedule. */
1637 if (lad->lad_prefetched <= (bk->lb_async_windows / 2))
1639 spin_unlock(&lad->lad_lock);
1641 wake_up_all(&mthread->t_ctl_waitq);
1643 lao->la_req_fini(env, lar);
1644 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1648 wait_event_idle(athread->t_ctl_waitq,
1649 !lfsck_assistant_req_empty(lad) ||
1650 test_bit(LAD_EXIT, &lad->lad_flags) ||
1651 test_bit(LAD_TO_POST, &lad->lad_flags) ||
1652 test_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags));
1654 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags)))
1655 GOTO(cleanup, rc = lad->lad_post_result);
1657 if (!list_empty(&lad->lad_req_list))
1660 if (test_bit(LAD_TO_POST, &lad->lad_flags)) {
1661 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread post\n",
1662 lfsck_lfsck2name(lfsck), lad->lad_name);
1664 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags)))
1665 GOTO(cleanup, rc = lad->lad_post_result);
1667 clear_bit(LAD_TO_POST, &lad->lad_flags);
1668 LASSERT(lad->lad_post_result > 0);
1670 /* Wakeup the master engine to go ahead. */
1671 wake_up_all(&mthread->t_ctl_waitq);
1673 memset(lr, 0, sizeof(*lr));
1674 lr->lr_event = LE_PHASE1_DONE;
1675 lr->lr_status = lad->lad_post_result;
1676 rc = lfsck_assistant_notify_others(env, com, lr);
1678 CDEBUG(D_LFSCK, "%s: LFSCK assistant notified "
1679 "others for %s post: rc = %d\n",
1680 lfsck_lfsck2name(lfsck),
1684 if (test_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags)) {
1685 clear_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags);
1686 atomic_inc(&lfsck->li_double_scan_count);
1687 set_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags);
1688 wake_up_all(&mthread->t_ctl_waitq);
1690 com->lc_new_checked = 0;
1691 com->lc_new_scanned = 0;
1692 com->lc_time_last_checkpoint = ktime_get_seconds();
1693 com->lc_time_next_checkpoint =
1694 com->lc_time_last_checkpoint +
1695 LFSCK_CHECKPOINT_INTERVAL;
1697 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before "
1698 "the second-stage scaning\n",
1699 lfsck_lfsck2name(lfsck));
1701 /* Flush async updates before handling orphan. */
1702 rc2 = dt_sync(env, lfsck->li_next);
1704 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 "
1705 "scan start, synced: rc = %d\n",
1706 lfsck_lfsck2name(lfsck), rc2);
1708 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1709 GOTO(cleanup, rc = 0);
1711 while (test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags)) {
1714 rc = lfsck_assistant_query_others(env, com);
1715 if (lfsck_phase2_next_ready(lad))
1721 /* Pull LFSCK status on related targets once
1722 * per 30 seconds if we are not notified. */
1723 while (seconds > 0 &&
1724 wait_event_idle_timeout(
1725 athread->t_ctl_waitq,
1726 lfsck_phase2_next_ready(lad) ||
1729 !thread_is_running(mthread),
1730 cfs_time_seconds(1)) == 0)
1734 test_bit(LAD_EXIT, &lad->lad_flags) ||
1735 !thread_is_running(mthread)))
1736 GOTO(cleanup, rc = 0);
1742 rc = lao->la_handler_p2(env, com);
1747 test_bit(LAD_EXIT, &lad->lad_flags) ||
1748 !thread_is_running(mthread)))
1749 GOTO(cleanup, rc = 0);
1755 /* Cleanup the unfinished requests. */
1756 spin_lock(&lad->lad_lock);
1758 lad->lad_assistant_status = rc;
1760 if (test_bit(LAD_EXIT, &lad->lad_flags) && lad->lad_post_result <= 0)
1761 lao->la_fill_pos(env, com, &lfsck->li_pos_checkpoint);
1763 thread_set_flags(athread, SVC_STOPPING);
1764 while (!list_empty(&lad->lad_req_list)) {
1765 lar = list_entry(lad->lad_req_list.next,
1766 struct lfsck_assistant_req,
1768 list_del_init(&lar->lar_list);
1769 lad->lad_prefetched--;
1770 spin_unlock(&lad->lad_lock);
1771 lao->la_req_fini(env, lar);
1772 spin_lock(&lad->lad_lock);
1774 spin_unlock(&lad->lad_lock);
1776 memset(lr, 0, sizeof(*lr));
1778 lr->lr_event = LE_PHASE2_DONE;
1780 } else if (rc == 0) {
1781 if (lfsck->li_flags & LPF_ALL_TGT) {
1782 lr->lr_event = LE_STOP;
1783 lr->lr_status = LS_STOPPED;
1785 lr->lr_event = LE_PEER_EXIT;
1786 switch (lfsck->li_status) {
1789 lr->lr_status = LS_CO_PAUSED;
1793 lr->lr_status = LS_CO_STOPPED;
1796 CDEBUG(D_LFSCK, "%s: LFSCK assistant unknown "
1797 "status: rc = %d\n",
1798 lfsck_lfsck2name(lfsck),
1800 lr->lr_status = LS_CO_FAILED;
1805 if (lfsck->li_flags & LPF_ALL_TGT) {
1806 lr->lr_event = LE_STOP;
1807 lr->lr_status = LS_FAILED;
1809 lr->lr_event = LE_PEER_EXIT;
1810 lr->lr_status = LS_CO_FAILED;
1814 rc1 = lfsck_assistant_notify_others(env, com, lr);
1816 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to notify "
1817 "others for %s quit: rc = %d\n",
1818 lfsck_lfsck2name(lfsck), lad->lad_name, rc1);
1822 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before exit\n",
1823 lfsck_lfsck2name(lfsck));
1825 /* Flush async updates before exit. */
1826 rc2 = dt_sync(env, lfsck->li_next);
1828 CDEBUG(D_LFSCK, "%s: LFSCK assistant synced before exit: rc = %d\n",
1829 lfsck_lfsck2name(lfsck), rc2);
1831 /* Under force exit case, some requests may be just freed without
1832 * verification, those objects should be re-handled when next run.
1833 * So not update the on-disk trace file under such case. */
1834 if (test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags)) {
1835 if (!test_bit(LAD_EXIT, &lad->lad_flags))
1836 rc1 = lao->la_double_scan_result(env, com, rc);
1838 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 scan "
1839 "finished: rc = %d\n",
1840 lfsck_lfsck2name(lfsck), rc1 != 0 ? rc1 : rc);
1844 if (test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags))
1845 atomic_dec(&lfsck->li_double_scan_count);
1847 spin_lock(&lad->lad_lock);
1848 lad->lad_assistant_status = (rc1 != 0 ? rc1 : rc);
1849 thread_set_flags(athread, SVC_STOPPED);
1850 lad->lad_task = NULL;
1851 spin_unlock(&lad->lad_lock);
1853 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread exit: rc = %d\n",
1854 lfsck_lfsck2name(lfsck), lad->lad_name,
1855 lad->lad_assistant_status);
1857 lfsck_thread_args_fini(lta);
1858 wake_up_all(&mthread->t_ctl_waitq);