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, 2014, 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_update_lma(const struct lu_env *env,
97 struct lfsck_instance *lfsck, struct dt_object *obj)
99 struct lfsck_thread_info *info = lfsck_env_info(env);
100 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
101 struct dt_device *dt = lfsck->li_bottom;
102 struct lustre_mdt_attrs *lma = &info->lti_lma;
109 if (bk->lb_param & LPF_DRYRUN)
112 buf = lfsck_buf_get(env, info->lti_lma_old, LMA_OLD_SIZE);
113 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LMA, BYPASS_CAPA);
118 fl = LU_XATTR_CREATE;
119 lustre_lma_init(lma, lfsck_dto2fid(obj), LMAC_FID_ON_OST, 0);
121 if (rc != LMA_OLD_SIZE && rc != sizeof(struct lustre_mdt_attrs))
124 fl = LU_XATTR_REPLACE;
125 lustre_lma_swab(lma);
126 lustre_lma_init(lma, lfsck_dto2fid(obj),
127 lma->lma_compat | LMAC_FID_ON_OST,
130 lustre_lma_swab(lma);
132 th = dt_trans_create(env, dt);
136 buf = lfsck_buf_get(env, lma, sizeof(*lma));
137 rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_LMA, fl, th);
141 rc = dt_trans_start(env, dt, th);
145 rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMA, fl, th, BYPASS_CAPA);
150 dt_trans_stop(env, dt, th);
154 static int lfsck_parent_fid(const struct lu_env *env, struct dt_object *obj,
157 if (unlikely(!S_ISDIR(lfsck_object_type(obj)) ||
158 !dt_try_as_dir(env, obj)))
161 return dt_lookup(env, obj, (struct dt_rec *)fid,
162 (const struct dt_key *)"..", BYPASS_CAPA);
166 * Check whether needs to scan the directory or not.
168 * 1) If we are not doing namespace LFSCK, or the given @obj is not directory,
169 * then needs not to scan the @obj. Otherwise,
170 * 2) Global /ROOT needs to be scanned, backend root needs not to be scanned.
171 * 3) If the @obj is neither IGIF nor normal FID (including .lustre and its
172 * sub-directories that have been scanned when the LFSCK engine start),
173 * then needs not to be scanned.
174 * 4) If it is a remote object, then scanning the object will be done on the
175 * MDT on which the object really resides.
176 * 5) If the local object has normal FID, then needs to be scanned. Otherwise,
177 * 6) If the object has linkEA, then needs to be scanned. Otherwise,
178 * 7) If none of the previous conditions are true, we need to check the parent
179 * directories whether this subdirectory is in a tree that should be scanned.
180 * Set the parent as current @obj, repeat 2)-7).
182 * \param[in] env pointer to the thread context
183 * \param[in] lfsck pointer to the lfsck instance
184 * \param[in] obj pointer to the object to be checked
186 * \retval positive number if the directory needs to be scanned
187 * \retval 0 if the directory needs NOT to be scanned
188 * \retval negative error number on failure
190 static int lfsck_needs_scan_dir(const struct lu_env *env,
191 struct lfsck_instance *lfsck,
192 struct dt_object *obj)
194 struct lfsck_thread_info *info = lfsck_env_info(env);
195 struct lu_fid *fid = &info->lti_fid;
196 struct lu_seq_range *range = &info->lti_range;
197 struct dt_device *dev = lfsck->li_bottom;
198 struct seq_server_site *ss = lu_site2seq(dev->dd_lu_dev.ld_site);
199 __u32 idx = lfsck_dev_idx(dev);
203 if (list_empty(&lfsck->li_list_dir) || !S_ISDIR(lfsck_object_type(obj)))
208 *fid = *lfsck_dto2fid(obj);
210 /* Global /ROOT is visible. */
211 if (unlikely(lu_fid_eq(fid, &lfsck->li_global_root_fid)))
214 /* Backend root is invisible. */
215 if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid)))
218 if (!fid_is_norm(fid) && !fid_is_igif(fid))
221 fld_range_set_mdt(range);
222 rc = fld_local_lookup(env, ss->ss_server_fld,
223 fid_seq(fid), range);
224 if (rc != 0 || range->lsr_index != idx)
225 /* Current FID should NOT be for the input parameter
226 * @obj, because the lfsck_master_oit_engine() has
227 * filtered out agent object. So current FID is for
228 * the ancestor of the original input parameter @obj.
229 * So the ancestor is a remote directory. The input
230 * parameter @obj is local directory, and should be
231 * scanned under such case. */
234 /* normal FID on this target (locally) must be for the
235 * client-side visiable object. */
236 if (fid_is_norm(fid))
240 obj = lfsck_object_find(env, lfsck, fid);
245 if (!dt_object_exists(obj))
249 dt_read_lock(env, obj, MOR_TGT_CHILD);
250 if (unlikely(lfsck_is_dead_obj(obj))) {
251 dt_read_unlock(env, obj);
256 rc = dt_xattr_get(env, obj,
257 lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK,
259 dt_read_unlock(env, obj);
263 if (rc < 0 && rc != -ENODATA)
266 rc = lfsck_parent_fid(env, obj, fid);
268 lfsck_object_put(env, obj);
274 if (!fid_is_sane(fid))
279 if (depth > 0 && obj != NULL)
280 lfsck_object_put(env, obj);
285 static int lfsck_load_stripe_lmv(const struct lu_env *env,
286 struct lfsck_instance *lfsck,
287 struct dt_object *obj)
289 struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
290 struct lfsck_lmv *llmv;
294 LASSERT(lfsck->li_obj_dir == NULL);
295 LASSERT(lfsck->li_lmv == NULL);
297 rc = lfsck_read_stripe_lmv(env, obj, lmv);
298 if (rc == -ENODATA) {
299 lfsck->li_obj_dir = lfsck_object_get(obj);
311 if (lmv->lmv_magic == LMV_MAGIC) {
312 struct lfsck_slave_lmv_rec *lslr;
315 llmv->ll_lmv_master = 1;
316 if (lmv->lmv_stripe_count < 1)
317 stripes = LFSCK_LMV_DEF_STRIPES;
318 else if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
319 stripes = LFSCK_LMV_MAX_STRIPES;
321 stripes = lmv->lmv_stripe_count;
323 OBD_ALLOC_LARGE(lslr, sizeof(*lslr) * stripes);
330 /* Find the object against the bottom device. */
331 obj = lfsck_object_find_by_dev(env, lfsck->li_bottom,
334 OBD_FREE_LARGE(lslr, sizeof(*lslr) * stripes);
337 RETURN(PTR_ERR(obj));
340 llmv->ll_stripes_allocated = stripes;
341 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
342 llmv->ll_lslr = lslr;
343 lfsck->li_obj_dir = obj;
345 llmv->ll_lmv_slave = 1;
346 lfsck->li_obj_dir = lfsck_object_get(obj);
350 atomic_set(&llmv->ll_ref, 1);
351 lfsck->li_lmv = llmv;
356 /* LFSCK wrap functions */
358 static void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
361 struct lfsck_component *com;
363 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
364 com->lc_ops->lfsck_fail(env, com, new_checked);
368 void lfsck_close_dir(const struct lu_env *env,
369 struct lfsck_instance *lfsck, int result)
371 struct lfsck_component *com;
374 if (lfsck->li_lmv != NULL) {
375 lfsck->li_lmv->ll_exit_value = result;
376 if (lfsck->li_obj_dir != NULL) {
377 list_for_each_entry(com, &lfsck->li_list_dir,
379 com->lc_ops->lfsck_close_dir(env, com);
383 lfsck_lmv_put(env, lfsck->li_lmv);
384 lfsck->li_lmv = NULL;
387 if (lfsck->li_di_dir != NULL) {
388 const struct dt_it_ops *dir_iops;
389 struct dt_it *dir_di = lfsck->li_di_dir;
391 LASSERT(lfsck->li_obj_dir != NULL);
393 dir_iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
394 lfsck_di_dir_put(env, lfsck);
395 dir_iops->fini(env, dir_di);
398 if (lfsck->li_obj_dir != NULL) {
399 struct dt_object *dir_obj = lfsck->li_obj_dir;
401 lfsck->li_obj_dir = NULL;
402 lfsck_object_put(env, dir_obj);
408 int lfsck_open_dir(const struct lu_env *env,
409 struct lfsck_instance *lfsck, __u64 cookie)
411 struct dt_object *obj = lfsck->li_obj_dir;
412 struct dt_it *di = lfsck->li_di_dir;
413 struct lfsck_component *com;
414 const struct dt_it_ops *iops;
418 LASSERT(obj != NULL);
421 if (unlikely(!dt_try_as_dir(env, obj)))
422 GOTO(out, rc = -ENOTDIR);
424 list_for_each_entry(com, &lfsck->li_list_dir, lc_link_dir) {
425 rc = com->lc_ops->lfsck_open_dir(env, com);
430 iops = &obj->do_index_ops->dio_it;
431 di = iops->init(env, obj, lfsck->li_args_dir, BYPASS_CAPA);
433 GOTO(out, rc = PTR_ERR(di));
435 rc = iops->load(env, di, cookie);
436 if (rc == 0 || (rc > 0 && cookie > 0))
437 rc = iops->next(env, di);
445 lfsck->li_cookie_dir = iops->store(env, di);
446 spin_lock(&lfsck->li_lock);
447 lfsck->li_di_dir = di;
448 spin_unlock(&lfsck->li_lock);
455 lfsck_close_dir(env, lfsck, rc);
460 static int lfsck_checkpoint(const struct lu_env *env,
461 struct lfsck_instance *lfsck)
463 struct lfsck_component *com;
467 if (likely(cfs_time_beforeq(cfs_time_current(),
468 lfsck->li_time_next_checkpoint)))
471 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
472 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
473 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
478 lfsck->li_time_last_checkpoint = cfs_time_current();
479 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
480 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
481 return rc1 != 0 ? rc1 : rc;
484 static int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
485 struct lfsck_start_param *lsp)
487 struct dt_object *obj = NULL;
488 struct lfsck_component *com;
489 struct lfsck_component *next;
490 struct lfsck_position *pos = NULL;
491 const struct dt_it_ops *iops =
492 &lfsck->li_obj_oit->do_index_ops->dio_it;
496 LASSERT(lfsck->li_obj_dir == NULL);
497 LASSERT(lfsck->li_di_dir == NULL);
499 lfsck->li_current_oit_processed = 0;
500 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
501 com->lc_new_checked = 0;
502 rc = com->lc_ops->lfsck_prep(env, com, lsp);
507 (!lfsck_pos_is_zero(&com->lc_pos_start) &&
508 lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
509 pos = &com->lc_pos_start;
512 /* Init otable-based iterator. */
514 rc = iops->load(env, lfsck->li_di_oit, 0);
516 lfsck->li_oit_over = 1;
523 rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
527 lfsck->li_oit_over = 1;
529 if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
532 /* Find the directory for namespace-based traverse. */
533 obj = lfsck_object_find(env, lfsck, &pos->lp_dir_parent);
535 RETURN(PTR_ERR(obj));
537 /* Remote directory will be scanned by the LFSCK instance
538 * on the MDT where the remote object really resides on. */
539 if (!dt_object_exists(obj) || dt_object_remote(obj) ||
540 unlikely(!S_ISDIR(lfsck_object_type(obj))))
543 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
545 /* For the master MDT-object of a striped directory,
546 * reset the iteration from the directory beginning. */
547 if (lfsck->li_lmv != NULL && lfsck->li_lmv->ll_lmv_master)
548 pos->lp_dir_cookie = 0;
550 rc = lfsck_open_dir(env, lfsck, pos->lp_dir_cookie);
552 /* The end of the directory. */
560 lfsck_object_put(env, obj);
563 lfsck_close_dir(env, lfsck, rc);
564 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
566 com->lc_ops->lfsck_post(env, com, rc, true);
573 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, true);
574 lfsck->li_pos_current = lfsck->li_pos_checkpoint;
575 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
576 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
581 lfsck->li_time_last_checkpoint = cfs_time_current();
582 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
583 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
587 static int lfsck_exec_oit(const struct lu_env *env,
588 struct lfsck_instance *lfsck, struct dt_object *obj)
590 struct lfsck_component *com;
594 LASSERT(lfsck->li_obj_dir == NULL);
596 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
597 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
602 rc = lfsck_needs_scan_dir(env, lfsck, obj);
606 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
608 rc = lfsck_open_dir(env, lfsck, 0);
614 lfsck_fail(env, lfsck, false);
617 lfsck_close_dir(env, lfsck, rc);
619 return rc > 0 ? 0 : rc;
622 static int lfsck_exec_dir(const struct lu_env *env,
623 struct lfsck_instance *lfsck,
624 struct lu_dirent *ent, __u16 type)
626 struct lfsck_component *com;
629 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
630 rc = com->lc_ops->lfsck_exec_dir(env, com, ent, type);
637 static int lfsck_master_dir_engine(const struct lu_env *env,
638 struct lfsck_instance *lfsck);
640 static int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
643 struct lfsck_component *com;
644 struct lfsck_component *next;
647 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
648 lfsck_close_dir(env, lfsck, result);
650 while (thread_is_running(&lfsck->li_thread) && rc > 0 &&
651 !list_empty(&lfsck->li_list_lmv)) {
652 struct lfsck_lmv_unit *llu;
654 spin_lock(&lfsck->li_lock);
655 llu = list_entry(lfsck->li_list_lmv.next,
656 struct lfsck_lmv_unit, llu_link);
657 list_del_init(&llu->llu_link);
658 spin_unlock(&lfsck->li_lock);
660 lfsck->li_lmv = &llu->llu_lmv;
661 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
662 rc = lfsck_open_dir(env, lfsck, 0);
664 rc = lfsck_master_dir_engine(env, lfsck);
665 lfsck_close_dir(env, lfsck, result);
671 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
672 rc = com->lc_ops->lfsck_post(env, com, result, false);
674 CDEBUG(D_LFSCK, "%s: lfsck_post at the component %u: "
675 "rc = %d\n", lfsck_lfsck2name(lfsck),
676 (__u32)com->lc_type, rc);
679 lfsck->li_time_last_checkpoint = cfs_time_current();
680 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
681 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
683 /* Ignore some component post failure to make other can go ahead. */
687 static int lfsck_double_scan(const struct lu_env *env,
688 struct lfsck_instance *lfsck)
690 struct lfsck_component *com;
691 struct lfsck_component *next;
692 struct l_wait_info lwi = { 0 };
696 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
697 rc = com->lc_ops->lfsck_double_scan(env, com);
702 l_wait_event(lfsck->li_thread.t_ctl_waitq,
703 atomic_read(&lfsck->li_double_scan_count) == 0,
706 if (lfsck->li_status != LS_PAUSED &&
707 lfsck->li_status != LS_CO_PAUSED) {
708 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
710 spin_lock(&lfsck->li_lock);
711 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
712 spin_unlock(&lfsck->li_lock);
716 return rc1 != 0 ? rc1 : rc;
719 static void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck)
721 struct lfsck_component *com;
722 struct lfsck_component *next;
724 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
726 if (com->lc_ops->lfsck_quit != NULL)
727 com->lc_ops->lfsck_quit(env, com);
729 spin_lock(&lfsck->li_lock);
730 list_del_init(&com->lc_link_dir);
731 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
732 spin_unlock(&lfsck->li_lock);
735 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
737 if (com->lc_ops->lfsck_quit != NULL)
738 com->lc_ops->lfsck_quit(env, com);
740 spin_lock(&lfsck->li_lock);
741 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
742 spin_unlock(&lfsck->li_lock);
748 static int lfsck_master_dir_engine(const struct lu_env *env,
749 struct lfsck_instance *lfsck)
751 struct lfsck_thread_info *info = lfsck_env_info(env);
752 struct dt_object *dir = lfsck->li_obj_dir;
753 const struct dt_it_ops *iops = &dir->do_index_ops->dio_it;
754 struct dt_it *di = lfsck->li_di_dir;
755 struct lu_dirent *ent =
756 (struct lu_dirent *)info->lti_key;
757 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
758 struct ptlrpc_thread *thread = &lfsck->li_thread;
764 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY2, cfs_fail_val) &&
765 unlikely(!thread_is_running(thread))) {
766 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
767 "parent "DFID", cookie "LPX64"\n",
768 lfsck_lfsck2name(lfsck),
769 PFID(lfsck_dto2fid(dir)), lfsck->li_cookie_dir);
774 lfsck->li_new_scanned++;
775 rc = iops->rec(env, di, (struct dt_rec *)ent,
778 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
782 CDEBUG(D_LFSCK, "%s: scan dir failed at rec(), "
783 "parent "DFID", cookie "LPX64": rc = %d\n",
784 lfsck_lfsck2name(lfsck),
785 PFID(lfsck_dto2fid(dir)),
786 lfsck->li_cookie_dir, rc);
787 lfsck_fail(env, lfsck, true);
788 if (bk->lb_param & LPF_FAILOUT)
794 if (ent->lde_attrs & LUDA_IGNORE &&
795 strcmp(ent->lde_name, dotdot) != 0)
798 /* The type in the @ent structure may has been overwritten,
799 * so we need to pass the @type parameter independently. */
800 rc = lfsck_exec_dir(env, lfsck, ent, type);
801 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
805 rc = lfsck_checkpoint(env, lfsck);
806 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
810 lfsck_control_speed(lfsck);
811 if (unlikely(!thread_is_running(thread))) {
812 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
813 "parent "DFID", cookie "LPX64"\n",
814 lfsck_lfsck2name(lfsck),
815 PFID(lfsck_dto2fid(dir)),
816 lfsck->li_cookie_dir);
820 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
821 spin_lock(&lfsck->li_lock);
822 thread_set_flags(thread, SVC_STOPPING);
823 spin_unlock(&lfsck->li_lock);
827 rc = iops->next(env, di);
830 if (rc > 0 && !lfsck->li_oit_over)
831 lfsck_close_dir(env, lfsck, rc);
837 * Object-table based iteration engine.
839 * Object-table based iteration is the basic linear engine to scan all the
840 * objects on current device in turn. For each object, it calls all the
841 * registered LFSCK component(s)' API to perform related consistency
844 * It flushes related LFSCK trace files to disk via making checkpoint
845 * periodically. Then if the server crashed or the LFSCK is paused, the
846 * LFSCK can resume from the latest checkpoint.
848 * It also controls the whole LFSCK speed via lfsck_control_speed() to
849 * avoid the server to become overload.
851 * \param[in] env pointer to the thread context
852 * \param[in] lfsck pointer to the lfsck instance
854 * \retval positive number if all objects have been scanned
855 * \retval 0 if the iteration is stopped or paused
856 * \retval negative error number on failure
858 static int lfsck_master_oit_engine(const struct lu_env *env,
859 struct lfsck_instance *lfsck)
861 struct lfsck_thread_info *info = lfsck_env_info(env);
862 const struct dt_it_ops *iops =
863 &lfsck->li_obj_oit->do_index_ops->dio_it;
864 struct dt_it *di = lfsck->li_di_oit;
865 struct lu_fid *fid = &info->lti_fid;
866 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
867 struct ptlrpc_thread *thread = &lfsck->li_thread;
868 struct dt_device *dev = lfsck->li_bottom;
869 struct seq_server_site *ss = lu_site2seq(dev->dd_lu_dev.ld_site);
870 __u32 idx = lfsck_dev_idx(dev);
874 if (unlikely(ss == NULL))
878 struct dt_object *target;
879 bool update_lma = false;
881 if (lfsck->li_di_dir != NULL) {
882 rc = lfsck_master_dir_engine(env, lfsck);
887 if (unlikely(lfsck->li_oit_over))
890 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY1, cfs_fail_val) &&
891 unlikely(!thread_is_running(thread))) {
892 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
894 lfsck_lfsck2name(lfsck), iops->store(env, di));
899 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
902 lfsck->li_current_oit_processed = 1;
904 if (!list_empty(&lfsck->li_list_lmv)) {
905 struct lfsck_lmv_unit *llu;
907 spin_lock(&lfsck->li_lock);
908 llu = list_entry(lfsck->li_list_lmv.next,
909 struct lfsck_lmv_unit, llu_link);
910 list_del_init(&llu->llu_link);
911 spin_unlock(&lfsck->li_lock);
913 lfsck->li_lmv = &llu->llu_lmv;
914 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
915 rc = lfsck_open_dir(env, lfsck, 0);
917 rc = lfsck_master_dir_engine(env, lfsck);
923 lfsck->li_new_scanned++;
924 lfsck->li_pos_current.lp_oit_cookie = iops->store(env, di);
925 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
927 CDEBUG(D_LFSCK, "%s: OIT scan failed at rec(): "
928 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
929 lfsck_fail(env, lfsck, true);
930 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
936 if (unlikely(!fid_is_sane(fid))) {
937 CDEBUG(D_LFSCK, "%s: OIT scan find invalid FID "DFID
939 lfsck_lfsck2name(lfsck), PFID(fid));
943 if (fid_is_idif(fid)) {
944 __u32 idx1 = fid_idif_ost_idx(fid);
946 LASSERT(!lfsck->li_master);
948 /* It is an old format device, update the LMA. */
950 struct ost_id *oi = &info->lti_oi;
952 fid_to_ostid(fid, oi);
953 ostid_to_fid(fid, oi, idx);
956 } else if (!fid_is_norm(fid) && !fid_is_igif(fid) &&
957 !fid_is_last_id(fid) &&
958 !lu_fid_eq(fid, &lfsck->li_global_root_fid)) {
960 /* If the FID/object is only used locally and invisible
961 * to external nodes, then LFSCK will not handle it.
963 * dot_lustre sequence has been handled specially. */
966 struct lu_seq_range *range = &info->lti_range;
968 if (lfsck->li_master)
969 fld_range_set_mdt(range);
971 fld_range_set_ost(range);
972 rc = fld_local_lookup(env, ss->ss_server_fld,
973 fid_seq(fid), range);
974 if (rc != 0 || range->lsr_index != idx) {
975 /* Remote object will be handled by the LFSCK
976 * instance on the MDT where the remote object
977 * really resides on. */
983 target = lfsck_object_find(env, lfsck, fid);
984 if (IS_ERR(target)) {
985 CDEBUG(D_LFSCK, "%s: OIT scan failed at find target "
986 DFID", cookie "LPU64": rc = %d\n",
987 lfsck_lfsck2name(lfsck), PFID(fid),
988 iops->store(env, di), rc);
989 lfsck_fail(env, lfsck, true);
990 if (bk->lb_param & LPF_FAILOUT)
991 RETURN(PTR_ERR(target));
996 if (dt_object_exists(target)) {
998 rc = lfsck_update_lma(env, lfsck, target);
1000 CDEBUG(D_LFSCK, "%s: fail to update "
1001 "LMA for "DFID": rc = %d\n",
1002 lfsck_lfsck2name(lfsck),
1003 PFID(lfsck_dto2fid(target)), rc);
1006 rc = lfsck_exec_oit(env, lfsck, target);
1008 lfsck_object_put(env, target);
1009 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
1013 rc = lfsck_checkpoint(env, lfsck);
1014 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
1018 lfsck_control_speed(lfsck);
1020 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
1021 spin_lock(&lfsck->li_lock);
1022 thread_set_flags(thread, SVC_STOPPING);
1023 spin_unlock(&lfsck->li_lock);
1027 rc = iops->next(env, di);
1028 if (unlikely(rc > 0))
1029 lfsck->li_oit_over = 1;
1030 else if (likely(rc == 0))
1031 lfsck->li_current_oit_processed = 0;
1033 if (unlikely(!thread_is_running(thread))) {
1034 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
1035 "cookie "LPU64"\n", lfsck_lfsck2name(lfsck),
1036 iops->store(env, di));
1039 } while (rc == 0 || lfsck->li_di_dir != NULL);
1044 int lfsck_master_engine(void *args)
1046 struct lfsck_thread_args *lta = args;
1047 struct lu_env *env = <a->lta_env;
1048 struct lfsck_instance *lfsck = lta->lta_lfsck;
1049 struct ptlrpc_thread *thread = &lfsck->li_thread;
1050 struct dt_object *oit_obj = lfsck->li_obj_oit;
1051 const struct dt_it_ops *oit_iops = &oit_obj->do_index_ops->dio_it;
1052 struct dt_it *oit_di;
1053 struct l_wait_info lwi = { 0 };
1057 if (lfsck->li_master &&
1058 (!list_empty(&lfsck->li_list_scan) ||
1059 !list_empty(&lfsck->li_list_double_scan))) {
1060 rc = lfsck_verify_lpf(env, lfsck);
1061 /* Fail to verify the .lustre/lost+found/MDTxxxx/ may be not
1062 * fatal, because the .lustre/lost+found/ maybe not accessed
1063 * by the LFSCK if it does not add orphans or others to such
1064 * directory. So go ahead until hit failure when really uses
1067 CDEBUG(D_LFSCK, "%s: master engine fail to verify the "
1068 ".lustre/lost+found/, go ahead: rc = %d\n",
1069 lfsck_lfsck2name(lfsck), rc);
1072 oit_di = oit_iops->init(env, oit_obj, lfsck->li_args_oit, BYPASS_CAPA);
1073 if (IS_ERR(oit_di)) {
1074 rc = PTR_ERR(oit_di);
1075 CDEBUG(D_LFSCK, "%s: master engine fail to init iteration: "
1076 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1078 GOTO(fini_args, rc);
1081 spin_lock(&lfsck->li_lock);
1082 lfsck->li_di_oit = oit_di;
1083 spin_unlock(&lfsck->li_lock);
1084 rc = lfsck_prep(env, lfsck, lta->lta_lsp);
1088 CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = %#x, dir_flags = %#x, "
1089 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1090 ", pid = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1091 lfsck->li_pos_checkpoint.lp_oit_cookie,
1092 lfsck->li_pos_checkpoint.lp_dir_cookie,
1093 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1096 spin_lock(&lfsck->li_lock);
1097 thread_set_flags(thread, SVC_RUNNING);
1098 spin_unlock(&lfsck->li_lock);
1099 wake_up_all(&thread->t_ctl_waitq);
1101 l_wait_event(thread->t_ctl_waitq,
1102 lfsck->li_start_unplug ||
1103 !thread_is_running(thread),
1105 if (!thread_is_running(thread))
1106 GOTO(fini_oit, rc = 0);
1108 if (!list_empty(&lfsck->li_list_scan) ||
1109 list_empty(&lfsck->li_list_double_scan))
1110 rc = lfsck_master_oit_engine(env, lfsck);
1114 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
1115 CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = %#x, dir_flags = %#x, "
1116 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1117 ", pid = %d, rc = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1118 lfsck->li_pos_checkpoint.lp_oit_cookie,
1119 lfsck->li_pos_checkpoint.lp_dir_cookie,
1120 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1123 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
1124 rc = lfsck_post(env, lfsck, rc);
1126 lfsck_close_dir(env, lfsck, rc);
1129 lfsck_di_oit_put(env, lfsck);
1130 oit_iops->fini(env, oit_di);
1132 if (!list_empty(&lfsck->li_list_double_scan))
1133 rc = lfsck_double_scan(env, lfsck);
1137 lfsck_quit(env, lfsck);
1140 /* XXX: Purge the pinned objects in the future. */
1143 spin_lock(&lfsck->li_lock);
1144 thread_set_flags(thread, SVC_STOPPED);
1145 spin_unlock(&lfsck->li_lock);
1146 wake_up_all(&thread->t_ctl_waitq);
1147 lfsck_thread_args_fini(lta);
1151 static inline bool lfsck_assistant_req_empty(struct lfsck_assistant_data *lad)
1155 spin_lock(&lad->lad_lock);
1156 if (list_empty(&lad->lad_req_list))
1158 spin_unlock(&lad->lad_lock);
1164 * Query the LFSCK status from the instatnces on remote servers.
1166 * The LFSCK assistant thread queries the LFSCK instances on other
1167 * servers (MDT/OST) about their status, such as whether they have
1168 * finished the phase1/phase2 scanning or not, and so on.
1170 * \param[in] env pointer to the thread context
1171 * \param[in] com pointer to the lfsck component
1173 * \retval 0 for success
1174 * \retval negative error number on failure
1176 static int lfsck_assistant_query_others(const struct lu_env *env,
1177 struct lfsck_component *com)
1179 struct lfsck_thread_info *info = lfsck_env_info(env);
1180 struct lfsck_request *lr = &info->lti_lr;
1181 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1182 struct lfsck_instance *lfsck = com->lc_lfsck;
1183 struct lfsck_assistant_data *lad = com->lc_data;
1184 struct ptlrpc_request_set *set;
1185 struct lfsck_tgt_descs *ltds;
1186 struct lfsck_tgt_desc *ltd;
1187 struct list_head *phase_head;
1192 set = ptlrpc_prep_set();
1196 lad->lad_touch_gen++;
1197 memset(lr, 0, sizeof(*lr));
1198 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1199 lr->lr_event = LE_QUERY;
1200 lr->lr_active = com->lc_type;
1201 laia->laia_com = com;
1203 laia->laia_shared = 0;
1205 if (!list_empty(&lad->lad_mdt_phase1_list)) {
1206 ltds = &lfsck->li_mdt_descs;
1208 phase_head = &lad->lad_mdt_phase1_list;
1209 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1214 ltds = &lfsck->li_ost_descs;
1215 lr->lr_flags = LEF_TO_OST;
1216 phase_head = &lad->lad_ost_phase1_list;
1219 laia->laia_ltds = ltds;
1220 spin_lock(<ds->ltd_lock);
1221 while (!list_empty(phase_head)) {
1222 struct list_head *phase_list;
1225 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1226 ltd = list_entry(phase_head->next,
1227 struct lfsck_tgt_desc,
1228 ltd_layout_phase_list);
1229 phase_list = <d->ltd_layout_phase_list;
1230 gen = <d->ltd_layout_gen;
1232 ltd = list_entry(phase_head->next,
1233 struct lfsck_tgt_desc,
1234 ltd_namespace_phase_list);
1235 phase_list = <d->ltd_namespace_phase_list;
1236 gen = <d->ltd_namespace_gen;
1239 if (*gen == lad->lad_touch_gen)
1242 *gen = lad->lad_touch_gen;
1243 list_move_tail(phase_list, phase_head);
1244 atomic_inc(<d->ltd_ref);
1245 laia->laia_ltd = ltd;
1246 spin_unlock(<ds->ltd_lock);
1247 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1248 lfsck_async_interpret_common,
1251 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to query "
1252 "%s %x for %s: rc = %d\n",
1253 lfsck_lfsck2name(lfsck),
1254 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1255 ltd->ltd_index, lad->lad_name, rc);
1259 spin_lock(<ds->ltd_lock);
1261 spin_unlock(<ds->ltd_lock);
1263 rc = ptlrpc_set_wait(set);
1265 ptlrpc_set_destroy(set);
1269 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST) &&
1270 list_empty(&lad->lad_mdt_phase1_list))
1274 ptlrpc_set_destroy(set);
1276 RETURN(rc1 != 0 ? rc1 : rc);
1280 * Notify the LFSCK event to the instances on remote servers.
1282 * The LFSCK assistant thread notifies the LFSCK instances on other
1283 * servers (MDT/OST) about some events, such as start new scanning,
1284 * stop the scanning, this LFSCK instance will exit, and so on.
1286 * \param[in] env pointer to the thread context
1287 * \param[in] com pointer to the lfsck component
1288 * \param[in] lr pointer to the LFSCK event request
1290 * \retval 0 for success
1291 * \retval negative error number on failure
1293 static int lfsck_assistant_notify_others(const struct lu_env *env,
1294 struct lfsck_component *com,
1295 struct lfsck_request *lr)
1297 struct lfsck_thread_info *info = lfsck_env_info(env);
1298 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1299 struct lfsck_instance *lfsck = com->lc_lfsck;
1300 struct lfsck_assistant_data *lad = com->lc_data;
1301 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1302 struct ptlrpc_request_set *set;
1303 struct lfsck_tgt_descs *ltds;
1304 struct lfsck_tgt_desc *ltd;
1305 struct lfsck_tgt_desc *next;
1311 set = ptlrpc_prep_set();
1315 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1316 lr->lr_active = com->lc_type;
1317 laia->laia_com = com;
1319 laia->laia_shared = 0;
1321 switch (lr->lr_event) {
1323 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1326 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN;
1327 lr->lr_speed = bk->lb_speed_limit;
1328 lr->lr_version = bk->lb_version;
1329 lr->lr_param |= bk->lb_param;
1330 lr->lr_async_windows = bk->lb_async_windows;
1331 lr->lr_flags = LEF_TO_OST;
1333 /* Notify OSTs firstly, then handle other MDTs if needed. */
1334 ltds = &lfsck->li_ost_descs;
1335 laia->laia_ltds = ltds;
1336 down_read(<ds->ltd_rw_sem);
1337 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1338 ltd = lfsck_tgt_get(ltds, idx);
1339 LASSERT(ltd != NULL);
1341 laia->laia_ltd = ltd;
1342 ltd->ltd_layout_done = 0;
1343 ltd->ltd_synced_failures = 0;
1344 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1345 lfsck_async_interpret_common,
1346 laia, LFSCK_NOTIFY);
1348 lfsck_lad_set_bitmap(env, com, idx);
1349 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1350 "notify OST %x for %s start: rc = %d\n",
1351 lfsck_lfsck2name(lfsck), idx,
1356 up_read(<ds->ltd_rw_sem);
1359 rc = ptlrpc_set_wait(set);
1361 ptlrpc_set_destroy(set);
1366 if (!(bk->lb_param & LPF_ALL_TGT))
1369 /* link other MDT targets locallly. */
1370 ltds = &lfsck->li_mdt_descs;
1371 spin_lock(<ds->ltd_lock);
1372 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1373 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1374 ltd = LTD_TGT(ltds, idx);
1375 LASSERT(ltd != NULL);
1377 if (!list_empty(<d->ltd_layout_list))
1380 list_add_tail(<d->ltd_layout_list,
1381 &lad->lad_mdt_list);
1382 list_add_tail(<d->ltd_layout_phase_list,
1383 &lad->lad_mdt_phase1_list);
1386 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1387 ltd = LTD_TGT(ltds, idx);
1388 LASSERT(ltd != NULL);
1390 if (!list_empty(<d->ltd_namespace_list))
1393 list_add_tail(<d->ltd_namespace_list,
1394 &lad->lad_mdt_list);
1395 list_add_tail(<d->ltd_namespace_phase_list,
1396 &lad->lad_mdt_phase1_list);
1399 spin_unlock(<ds->ltd_lock);
1402 case LE_PHASE2_DONE:
1403 case LE_PEER_EXIT: {
1404 struct list_head *phase_head;
1406 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1407 if (bk->lb_param & LPF_ALL_TGT) {
1408 phase_head = &lad->lad_mdt_list;
1409 ltds = &lfsck->li_mdt_descs;
1410 if (lr->lr_event == LE_STOP) {
1411 /* unlink other MDT targets locallly. */
1412 spin_lock(<ds->ltd_lock);
1413 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1414 list_for_each_entry_safe(ltd, next,
1415 phase_head, ltd_layout_list) {
1417 <d->ltd_layout_phase_list);
1419 <d->ltd_layout_list);
1422 list_for_each_entry_safe(ltd, next,
1424 ltd_namespace_list) {
1426 <d->ltd_namespace_phase_list);
1428 <d->ltd_namespace_list);
1431 spin_unlock(<ds->ltd_lock);
1433 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1436 lr->lr_flags |= LEF_TO_OST;
1437 phase_head = &lad->lad_ost_list;
1438 ltds = &lfsck->li_ost_descs;
1440 lr->lr_flags &= ~LEF_TO_OST;
1442 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1445 lr->lr_flags |= LEF_TO_OST;
1446 phase_head = &lad->lad_ost_list;
1447 ltds = &lfsck->li_ost_descs;
1451 laia->laia_ltds = ltds;
1452 spin_lock(<ds->ltd_lock);
1453 while (!list_empty(phase_head)) {
1454 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1455 ltd = list_entry(phase_head->next,
1456 struct lfsck_tgt_desc,
1458 if (!list_empty(<d->ltd_layout_phase_list))
1460 <d->ltd_layout_phase_list);
1461 list_del_init(<d->ltd_layout_list);
1463 ltd = list_entry(phase_head->next,
1464 struct lfsck_tgt_desc,
1465 ltd_namespace_list);
1466 if (!list_empty(<d->ltd_namespace_phase_list))
1468 <d->ltd_namespace_phase_list);
1469 list_del_init(<d->ltd_namespace_list);
1471 atomic_inc(<d->ltd_ref);
1472 laia->laia_ltd = ltd;
1473 spin_unlock(<ds->ltd_lock);
1474 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1475 lfsck_async_interpret_common,
1476 laia, LFSCK_NOTIFY);
1478 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1479 "notify %s %x for %s stop/phase2_done/"
1480 "peer_exit: rc = %d\n",
1481 lfsck_lfsck2name(lfsck),
1482 (lr->lr_flags & LEF_TO_OST) ?
1483 "OST" : "MDT", ltd->ltd_index,
1487 spin_lock(<ds->ltd_lock);
1489 spin_unlock(<ds->ltd_lock);
1491 rc = ptlrpc_set_wait(set);
1493 ptlrpc_set_destroy(set);
1497 if (com->lc_type == LFSCK_TYPE_LAYOUT &&
1498 !(lr->lr_flags & LEF_TO_OST)) {
1499 lr->lr_flags |= LEF_TO_OST;
1500 phase_head = &lad->lad_ost_list;
1501 ltds = &lfsck->li_ost_descs;
1506 case LE_PHASE1_DONE:
1507 lad->lad_ops->la_sync_failures(env, com, lr);
1508 lad->lad_touch_gen++;
1509 ltds = &lfsck->li_mdt_descs;
1510 laia->laia_ltds = ltds;
1511 spin_lock(<ds->ltd_lock);
1512 while (!list_empty(&lad->lad_mdt_list)) {
1513 struct list_head *list;
1516 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1517 ltd = list_entry(lad->lad_mdt_list.next,
1518 struct lfsck_tgt_desc,
1520 list = <d->ltd_layout_list;
1521 gen = <d->ltd_layout_gen;
1523 struct lfsck_namespace *ns = com->lc_file_ram;
1525 ltd = list_entry(lad->lad_mdt_list.next,
1526 struct lfsck_tgt_desc,
1527 ltd_namespace_list);
1528 list = <d->ltd_namespace_list;
1529 gen = <d->ltd_namespace_gen;
1530 lr->lr_flags2 = ns->ln_flags & ~LF_INCOMPLETE;
1533 if (*gen == lad->lad_touch_gen)
1536 *gen = lad->lad_touch_gen;
1537 list_move_tail(list, &lad->lad_mdt_list);
1538 if (ltd->ltd_synced_failures)
1541 atomic_inc(<d->ltd_ref);
1542 laia->laia_ltd = ltd;
1543 spin_unlock(<ds->ltd_lock);
1544 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1545 lfsck_async_interpret_common,
1546 laia, LFSCK_NOTIFY);
1548 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1549 "notify MDT %x for %s phase1 done: "
1550 "rc = %d\n", lfsck_lfsck2name(lfsck),
1551 ltd->ltd_index, lad->lad_name, rc);
1554 spin_lock(<ds->ltd_lock);
1556 spin_unlock(<ds->ltd_lock);
1559 CDEBUG(D_LFSCK, "%s: LFSCK assistant unexpected LFSCK event: "
1560 "rc = %d\n", lfsck_lfsck2name(lfsck), lr->lr_event);
1565 rc1 = ptlrpc_set_wait(set);
1566 ptlrpc_set_destroy(set);
1568 RETURN(rc != 0 ? rc : rc1);
1572 * The LFSCK assistant thread is triggered by the LFSCK main engine.
1573 * They co-work together as an asynchronous pipeline: the LFSCK main
1574 * engine scans the system and pre-fetches the objects, attributes,
1575 * or name entries, etc, and pushes them into the pipeline as input
1576 * requests for the LFSCK assistant thread; on the other end of the
1577 * pipeline, the LFSCK assistant thread performs the real check and
1578 * repair for every request from the main engine.
1580 * Generally, the assistant engine may be blocked when check/repair
1581 * something, so the LFSCK main engine will run some faster. On the
1582 * other hand, the LFSCK main engine will drive multiple assistant
1583 * threads in parallel, means for each LFSCK component on the master
1584 * (such as layout LFSCK, namespace LFSCK), there is an independent
1585 * LFSCK assistant thread. So under such 1:N multiple asynchronous
1586 * pipelines mode, the whole LFSCK performance will be much better
1587 * than check/repair everything by the LFSCK main engine itself.
1589 int lfsck_assistant_engine(void *args)
1591 struct lfsck_thread_args *lta = args;
1592 struct lu_env *env = <a->lta_env;
1593 struct lfsck_component *com = lta->lta_com;
1594 struct lfsck_instance *lfsck = lta->lta_lfsck;
1595 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1596 struct lfsck_position *pos = &com->lc_pos_start;
1597 struct lfsck_thread_info *info = lfsck_env_info(env);
1598 struct lfsck_request *lr = &info->lti_lr;
1599 struct lfsck_assistant_data *lad = com->lc_data;
1600 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1601 struct ptlrpc_thread *athread = &lad->lad_thread;
1602 struct lfsck_assistant_operations *lao = lad->lad_ops;
1603 struct lfsck_assistant_req *lar;
1604 struct l_wait_info lwi = { 0 };
1610 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread start\n",
1611 lfsck_lfsck2name(lfsck), lad->lad_name);
1613 memset(lr, 0, sizeof(*lr));
1614 lr->lr_event = LE_START;
1615 if (pos->lp_oit_cookie <= 1)
1616 lr->lr_param = LPF_RESET;
1617 rc = lfsck_assistant_notify_others(env, com, lr);
1619 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to notify others "
1620 "to start %s: rc = %d\n",
1621 lfsck_lfsck2name(lfsck), lad->lad_name, rc);
1625 spin_lock(&lad->lad_lock);
1626 thread_set_flags(athread, SVC_RUNNING);
1627 spin_unlock(&lad->lad_lock);
1628 wake_up_all(&mthread->t_ctl_waitq);
1631 while (!list_empty(&lad->lad_req_list)) {
1632 bool wakeup = false;
1634 if (unlikely(lad->lad_exit ||
1635 !thread_is_running(mthread)))
1636 GOTO(cleanup1, rc = lad->lad_post_result);
1638 lar = list_entry(lad->lad_req_list.next,
1639 struct lfsck_assistant_req,
1641 /* Only the lfsck_assistant_engine thread itself can
1642 * remove the "lar" from the head of the list, LFSCK
1643 * engine thread only inserts other new "lar" at the
1644 * end of the list. So it is safe to handle current
1645 * "lar" without the spin_lock. */
1646 rc = lao->la_handler_p1(env, com, lar);
1647 spin_lock(&lad->lad_lock);
1648 list_del_init(&lar->lar_list);
1649 lad->lad_prefetched--;
1650 /* Wake up the main engine thread only when the list
1651 * is empty or half of the prefetched items have been
1652 * handled to avoid too frequent thread schedule. */
1653 if (lad->lad_prefetched == 0 ||
1654 (bk->lb_async_windows != 0 &&
1655 bk->lb_async_windows / 2 ==
1656 lad->lad_prefetched))
1658 spin_unlock(&lad->lad_lock);
1660 wake_up_all(&mthread->t_ctl_waitq);
1662 lao->la_req_fini(env, lar);
1663 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1667 l_wait_event(athread->t_ctl_waitq,
1668 !lfsck_assistant_req_empty(lad) ||
1671 lad->lad_to_double_scan,
1674 if (unlikely(lad->lad_exit))
1675 GOTO(cleanup1, rc = lad->lad_post_result);
1677 if (!list_empty(&lad->lad_req_list))
1680 if (lad->lad_to_post) {
1681 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread post\n",
1682 lfsck_lfsck2name(lfsck), lad->lad_name);
1684 if (unlikely(lad->lad_exit))
1685 GOTO(cleanup1, rc = lad->lad_post_result);
1687 lad->lad_to_post = 0;
1688 LASSERT(lad->lad_post_result > 0);
1690 memset(lr, 0, sizeof(*lr));
1691 lr->lr_event = LE_PHASE1_DONE;
1692 lr->lr_status = lad->lad_post_result;
1693 rc = lfsck_assistant_notify_others(env, com, lr);
1695 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to "
1696 "notify others for %s post: rc = %d\n",
1697 lfsck_lfsck2name(lfsck),
1700 /* Wakeup the master engine to go ahead. */
1701 wake_up_all(&mthread->t_ctl_waitq);
1704 if (lad->lad_to_double_scan) {
1705 lad->lad_to_double_scan = 0;
1706 atomic_inc(&lfsck->li_double_scan_count);
1707 lad->lad_in_double_scan = 1;
1708 wake_up_all(&mthread->t_ctl_waitq);
1710 com->lc_new_checked = 0;
1711 com->lc_new_scanned = 0;
1712 com->lc_time_last_checkpoint = cfs_time_current();
1713 com->lc_time_next_checkpoint =
1714 com->lc_time_last_checkpoint +
1715 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1717 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before "
1718 "the second-stage scaning\n",
1719 lfsck_lfsck2name(lfsck));
1721 /* Flush async updates before handling orphan. */
1722 rc2 = dt_sync(env, lfsck->li_next);
1724 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 "
1725 "scan start, synced: rc = %d\n",
1726 lfsck_lfsck2name(lfsck), rc2);
1728 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1729 GOTO(cleanup2, rc = 0);
1731 while (lad->lad_in_double_scan) {
1732 rc = lfsck_assistant_query_others(env, com);
1733 if (lfsck_phase2_next_ready(lad))
1739 /* Pull LFSCK status on related targets once
1740 * per 30 seconds if we are not notified. */
1741 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
1742 cfs_time_seconds(1),
1744 rc = l_wait_event(athread->t_ctl_waitq,
1745 lfsck_phase2_next_ready(lad) ||
1747 !thread_is_running(mthread),
1750 if (unlikely(lad->lad_exit ||
1751 !thread_is_running(mthread)))
1752 GOTO(cleanup2, rc = 0);
1754 if (rc == -ETIMEDOUT)
1761 rc = lao->la_handler_p2(env, com);
1765 if (unlikely(lad->lad_exit ||
1766 !thread_is_running(mthread)))
1767 GOTO(cleanup2, rc = 0);
1773 /* Cleanup the unfinished requests. */
1774 spin_lock(&lad->lad_lock);
1776 lad->lad_assistant_status = rc;
1778 if (lad->lad_exit && lad->lad_post_result <= 0)
1779 lao->la_fill_pos(env, com, &lfsck->li_pos_checkpoint);
1781 while (!list_empty(&lad->lad_req_list)) {
1782 lar = list_entry(lad->lad_req_list.next,
1783 struct lfsck_assistant_req,
1785 list_del_init(&lar->lar_list);
1786 lad->lad_prefetched--;
1787 spin_unlock(&lad->lad_lock);
1788 lao->la_req_fini(env, lar);
1789 spin_lock(&lad->lad_lock);
1791 spin_unlock(&lad->lad_lock);
1793 LASSERTF(lad->lad_prefetched == 0, "unmatched prefeteched objs %d\n",
1794 lad->lad_prefetched);
1797 memset(lr, 0, sizeof(*lr));
1799 lr->lr_event = LE_PHASE2_DONE;
1801 } else if (rc == 0) {
1802 if (lfsck->li_flags & LPF_ALL_TGT) {
1803 lr->lr_event = LE_STOP;
1804 lr->lr_status = LS_STOPPED;
1806 lr->lr_event = LE_PEER_EXIT;
1807 switch (lfsck->li_status) {
1810 lr->lr_status = LS_CO_PAUSED;
1814 lr->lr_status = LS_CO_STOPPED;
1817 CDEBUG(D_LFSCK, "%s: LFSCK assistant unknown "
1818 "status: rc = %d\n",
1819 lfsck_lfsck2name(lfsck),
1821 lr->lr_status = LS_CO_FAILED;
1826 if (lfsck->li_flags & LPF_ALL_TGT) {
1827 lr->lr_event = LE_STOP;
1828 lr->lr_status = LS_FAILED;
1830 lr->lr_event = LE_PEER_EXIT;
1831 lr->lr_status = LS_CO_FAILED;
1835 rc1 = lfsck_assistant_notify_others(env, com, lr);
1837 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to notify "
1838 "others for %s quit: rc = %d\n",
1839 lfsck_lfsck2name(lfsck), lad->lad_name, rc1);
1843 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before exit\n",
1844 lfsck_lfsck2name(lfsck));
1846 /* Flush async updates before exit. */
1847 rc2 = dt_sync(env, lfsck->li_next);
1849 CDEBUG(D_LFSCK, "%s: LFSCK assistant synced before exit: rc = %d\n",
1850 lfsck_lfsck2name(lfsck), rc2);
1852 /* Under force exit case, some requests may be just freed without
1853 * verification, those objects should be re-handled when next run.
1854 * So not update the on-disk trace file under such case. */
1855 if (lad->lad_in_double_scan) {
1857 rc1 = lao->la_double_scan_result(env, com, rc);
1859 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 scan "
1860 "finished: rc = %d\n",
1861 lfsck_lfsck2name(lfsck), rc1 != 0 ? rc1 : rc);
1865 if (lad->lad_in_double_scan)
1866 atomic_dec(&lfsck->li_double_scan_count);
1868 spin_lock(&lad->lad_lock);
1869 lad->lad_assistant_status = (rc1 != 0 ? rc1 : rc);
1870 thread_set_flags(athread, SVC_STOPPED);
1871 wake_up_all(&mthread->t_ctl_waitq);
1872 spin_unlock(&lad->lad_lock);
1874 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread exit: rc = %d\n",
1875 lfsck_lfsck2name(lfsck), lad->lad_name,
1876 lad->lad_assistant_status);
1878 lfsck_thread_args_fini(lta);