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) 2012, 2013, 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. */
237 /* normal FID on this target (locally) must be for the
238 * client-side visiable object. */
239 if (fid_is_norm(fid))
243 obj = lfsck_object_find(env, lfsck, fid);
248 if (!dt_object_exists(obj))
252 dt_read_lock(env, obj, MOR_TGT_CHILD);
253 if (unlikely(lfsck_is_dead_obj(obj))) {
254 dt_read_unlock(env, obj);
259 rc = dt_xattr_get(env, obj,
260 lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK,
262 dt_read_unlock(env, obj);
266 if (rc < 0 && rc != -ENODATA)
269 rc = lfsck_parent_fid(env, obj, fid);
271 lfsck_object_put(env, obj);
277 if (!fid_is_sane(fid))
282 if (depth > 0 && obj != NULL)
283 lfsck_object_put(env, obj);
288 static int lfsck_load_stripe_lmv(const struct lu_env *env,
289 struct lfsck_instance *lfsck,
290 struct dt_object *obj)
292 struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
293 struct lfsck_lmv *llmv;
297 LASSERT(lfsck->li_obj_dir == NULL);
298 LASSERT(lfsck->li_lmv == NULL);
300 rc = lfsck_read_stripe_lmv(env, obj, lmv);
301 if (rc == -ENODATA) {
302 lfsck->li_obj_dir = lfsck_object_get(obj);
314 if (lmv->lmv_magic == LMV_MAGIC) {
315 struct lfsck_slave_lmv_rec *lslr;
318 llmv->ll_lmv_master = 1;
319 if (lmv->lmv_stripe_count < 1)
320 stripes = LFSCK_LMV_DEF_STRIPES;
321 else if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
322 stripes = LFSCK_LMV_MAX_STRIPES;
324 stripes = lmv->lmv_stripe_count;
326 OBD_ALLOC_LARGE(lslr, sizeof(*lslr) * stripes);
333 /* Find the object against the bottom device. */
334 obj = lfsck_object_find_by_dev(env, lfsck->li_bottom,
337 OBD_FREE_LARGE(lslr, sizeof(*lslr) * stripes);
340 RETURN(PTR_ERR(obj));
343 llmv->ll_stripes_allocated = stripes;
344 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
345 llmv->ll_lslr = lslr;
346 lfsck->li_obj_dir = obj;
348 llmv->ll_lmv_slave = 1;
349 lfsck->li_obj_dir = lfsck_object_get(obj);
353 atomic_set(&llmv->ll_ref, 1);
354 lfsck->li_lmv = llmv;
359 /* LFSCK wrap functions */
361 static void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
364 struct lfsck_component *com;
366 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
367 com->lc_ops->lfsck_fail(env, com, new_checked);
371 void lfsck_close_dir(const struct lu_env *env,
372 struct lfsck_instance *lfsck, int result)
374 struct lfsck_component *com;
377 if (lfsck->li_lmv != NULL) {
378 lfsck->li_lmv->ll_exit_value = result;
379 if (lfsck->li_obj_dir != NULL) {
380 list_for_each_entry(com, &lfsck->li_list_dir,
382 com->lc_ops->lfsck_close_dir(env, com);
386 lfsck_lmv_put(env, lfsck->li_lmv);
387 lfsck->li_lmv = NULL;
390 if (lfsck->li_di_dir != NULL) {
391 const struct dt_it_ops *dir_iops =
392 &lfsck->li_obj_dir->do_index_ops->dio_it;
393 struct dt_it *dir_di = lfsck->li_di_dir;
395 lfsck_di_dir_put(env, lfsck);
396 dir_iops->fini(env, dir_di);
399 if (lfsck->li_obj_dir != NULL) {
400 struct dt_object *dir_obj = lfsck->li_obj_dir;
402 lfsck->li_obj_dir = NULL;
403 lfsck_object_put(env, dir_obj);
409 int lfsck_open_dir(const struct lu_env *env,
410 struct lfsck_instance *lfsck, __u64 cookie)
412 struct dt_object *obj = lfsck->li_obj_dir;
413 struct dt_it *di = lfsck->li_di_dir;
414 struct lfsck_component *com;
415 const struct dt_it_ops *iops;
419 LASSERT(obj != NULL);
422 if (unlikely(!dt_try_as_dir(env, obj)))
423 GOTO(out, rc = -ENOTDIR);
425 list_for_each_entry(com, &lfsck->li_list_dir, lc_link_dir) {
426 rc = com->lc_ops->lfsck_open_dir(env, com);
431 iops = &obj->do_index_ops->dio_it;
432 di = iops->init(env, obj, lfsck->li_args_dir, BYPASS_CAPA);
434 GOTO(out, rc = PTR_ERR(di));
436 rc = iops->load(env, di, cookie);
437 if (rc == 0 || (rc > 0 && cookie > 0))
438 rc = iops->next(env, di);
446 lfsck->li_cookie_dir = iops->store(env, di);
447 spin_lock(&lfsck->li_lock);
448 lfsck->li_di_dir = di;
449 spin_unlock(&lfsck->li_lock);
456 lfsck_close_dir(env, lfsck, rc);
461 static int lfsck_checkpoint(const struct lu_env *env,
462 struct lfsck_instance *lfsck)
464 struct lfsck_component *com;
468 if (likely(cfs_time_beforeq(cfs_time_current(),
469 lfsck->li_time_next_checkpoint)))
472 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
473 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
474 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
479 lfsck->li_time_last_checkpoint = cfs_time_current();
480 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
481 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
482 return rc1 != 0 ? rc1 : rc;
485 static int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
486 struct lfsck_start_param *lsp)
488 struct dt_object *obj = NULL;
489 struct lfsck_component *com;
490 struct lfsck_component *next;
491 struct lfsck_position *pos = NULL;
492 const struct dt_it_ops *iops =
493 &lfsck->li_obj_oit->do_index_ops->dio_it;
497 LASSERT(lfsck->li_obj_dir == NULL);
498 LASSERT(lfsck->li_di_dir == NULL);
500 lfsck->li_current_oit_processed = 0;
501 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
502 com->lc_new_checked = 0;
503 rc = com->lc_ops->lfsck_prep(env, com, lsp);
508 (!lfsck_pos_is_zero(&com->lc_pos_start) &&
509 lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
510 pos = &com->lc_pos_start;
513 /* Init otable-based iterator. */
515 rc = iops->load(env, lfsck->li_di_oit, 0);
517 lfsck->li_oit_over = 1;
524 rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
528 lfsck->li_oit_over = 1;
530 if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
533 /* Find the directory for namespace-based traverse. */
534 obj = lfsck_object_find(env, lfsck, &pos->lp_dir_parent);
536 RETURN(PTR_ERR(obj));
538 /* Remote directory will be scanned by the LFSCK instance
539 * on the MDT where the remote object really resides on. */
540 if (!dt_object_exists(obj) || dt_object_remote(obj) ||
541 unlikely(!S_ISDIR(lfsck_object_type(obj))))
544 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
546 /* For the master MDT-object of a striped directory,
547 * reset the iteration from the directory beginning. */
548 if (lfsck->li_lmv != NULL && lfsck->li_lmv->ll_lmv_master)
549 pos->lp_dir_cookie = 0;
551 rc = lfsck_open_dir(env, lfsck, pos->lp_dir_cookie);
558 lfsck_object_put(env, obj);
561 lfsck_close_dir(env, lfsck, rc);
562 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
564 com->lc_ops->lfsck_post(env, com, rc, true);
571 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, true);
572 lfsck->li_pos_current = lfsck->li_pos_checkpoint;
573 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
574 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
579 lfsck->li_time_last_checkpoint = cfs_time_current();
580 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
581 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
585 static int lfsck_exec_oit(const struct lu_env *env,
586 struct lfsck_instance *lfsck, struct dt_object *obj)
588 struct lfsck_component *com;
592 LASSERT(lfsck->li_obj_dir == NULL);
594 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
595 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
600 rc = lfsck_needs_scan_dir(env, lfsck, obj);
604 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
606 rc = lfsck_open_dir(env, lfsck, 0);
612 lfsck_fail(env, lfsck, false);
615 lfsck_close_dir(env, lfsck, rc);
617 return rc > 0 ? 0 : rc;
620 static int lfsck_exec_dir(const struct lu_env *env,
621 struct lfsck_instance *lfsck,
622 struct lu_dirent *ent, __u16 type)
624 struct lfsck_component *com;
627 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
628 rc = com->lc_ops->lfsck_exec_dir(env, com, ent, type);
635 static int lfsck_master_dir_engine(const struct lu_env *env,
636 struct lfsck_instance *lfsck);
638 static int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
641 struct lfsck_component *com;
642 struct lfsck_component *next;
645 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
646 lfsck_close_dir(env, lfsck, result);
648 while (thread_is_running(&lfsck->li_thread) && rc > 0 &&
649 !list_empty(&lfsck->li_list_lmv)) {
650 struct lfsck_lmv_unit *llu;
652 spin_lock(&lfsck->li_lock);
653 llu = list_entry(lfsck->li_list_lmv.next,
654 struct lfsck_lmv_unit, llu_link);
655 list_del_init(&llu->llu_link);
656 spin_unlock(&lfsck->li_lock);
658 lfsck->li_lmv = &llu->llu_lmv;
659 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
660 rc = lfsck_open_dir(env, lfsck, 0);
662 rc = lfsck_master_dir_engine(env, lfsck);
663 lfsck_close_dir(env, lfsck, result);
669 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
670 rc = com->lc_ops->lfsck_post(env, com, result, false);
672 CDEBUG(D_LFSCK, "%s: lfsck_post at the component %u: "
673 "rc = %d\n", lfsck_lfsck2name(lfsck),
674 (__u32)com->lc_type, rc);
677 lfsck->li_time_last_checkpoint = cfs_time_current();
678 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
679 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
681 /* Ignore some component post failure to make other can go ahead. */
685 static int lfsck_double_scan(const struct lu_env *env,
686 struct lfsck_instance *lfsck)
688 struct lfsck_component *com;
689 struct lfsck_component *next;
690 struct l_wait_info lwi = { 0 };
694 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
695 rc = com->lc_ops->lfsck_double_scan(env, com);
700 l_wait_event(lfsck->li_thread.t_ctl_waitq,
701 atomic_read(&lfsck->li_double_scan_count) == 0,
704 if (lfsck->li_status != LS_PAUSED &&
705 lfsck->li_status != LS_CO_PAUSED) {
706 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
708 spin_lock(&lfsck->li_lock);
709 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
710 spin_unlock(&lfsck->li_lock);
714 return rc1 != 0 ? rc1 : rc;
717 static void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck)
719 struct lfsck_component *com;
720 struct lfsck_component *next;
722 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
724 if (com->lc_ops->lfsck_quit != NULL)
725 com->lc_ops->lfsck_quit(env, com);
727 spin_lock(&lfsck->li_lock);
728 list_del_init(&com->lc_link_dir);
729 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
730 spin_unlock(&lfsck->li_lock);
733 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
735 if (com->lc_ops->lfsck_quit != NULL)
736 com->lc_ops->lfsck_quit(env, com);
738 spin_lock(&lfsck->li_lock);
739 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
740 spin_unlock(&lfsck->li_lock);
746 static int lfsck_master_dir_engine(const struct lu_env *env,
747 struct lfsck_instance *lfsck)
749 struct lfsck_thread_info *info = lfsck_env_info(env);
750 struct dt_object *dir = lfsck->li_obj_dir;
751 const struct dt_it_ops *iops = &dir->do_index_ops->dio_it;
752 struct dt_it *di = lfsck->li_di_dir;
753 struct lu_dirent *ent =
754 (struct lu_dirent *)info->lti_key;
755 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
756 struct ptlrpc_thread *thread = &lfsck->li_thread;
762 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY2) &&
764 struct l_wait_info lwi;
766 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
768 l_wait_event(thread->t_ctl_waitq,
769 !thread_is_running(thread),
772 if (unlikely(!thread_is_running(thread))) {
773 CDEBUG(D_LFSCK, "%s: scan dir exit for engine "
774 "stop, parent "DFID", cookie "LPX64"\n",
775 lfsck_lfsck2name(lfsck),
776 PFID(lfsck_dto2fid(dir)),
777 lfsck->li_cookie_dir);
782 lfsck->li_new_scanned++;
783 rc = iops->rec(env, di, (struct dt_rec *)ent,
786 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
790 CDEBUG(D_LFSCK, "%s: scan dir failed at rec(), "
791 "parent "DFID", cookie "LPX64": rc = %d\n",
792 lfsck_lfsck2name(lfsck),
793 PFID(lfsck_dto2fid(dir)),
794 lfsck->li_cookie_dir, rc);
795 lfsck_fail(env, lfsck, true);
796 if (bk->lb_param & LPF_FAILOUT)
802 if (ent->lde_attrs & LUDA_IGNORE &&
803 strcmp(ent->lde_name, dotdot) != 0)
806 /* The type in the @ent structure may has been overwritten,
807 * so we need to pass the @type parameter independently. */
808 rc = lfsck_exec_dir(env, lfsck, ent, type);
809 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
813 rc = lfsck_checkpoint(env, lfsck);
814 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
818 lfsck_control_speed(lfsck);
819 if (unlikely(!thread_is_running(thread))) {
820 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
821 "parent "DFID", cookie "LPX64"\n",
822 lfsck_lfsck2name(lfsck),
823 PFID(lfsck_dto2fid(dir)),
824 lfsck->li_cookie_dir);
828 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
829 spin_lock(&lfsck->li_lock);
830 thread_set_flags(thread, SVC_STOPPING);
831 spin_unlock(&lfsck->li_lock);
835 rc = iops->next(env, di);
838 if (rc > 0 && !lfsck->li_oit_over)
839 lfsck_close_dir(env, lfsck, rc);
845 * Object-table based iteration engine.
847 * Object-table based iteration is the basic linear engine to scan all the
848 * objects on current device in turn. For each object, it calls all the
849 * registered LFSCK component(s)' API to perform related consistency
852 * It flushes related LFSCK trace files to disk via making checkpoint
853 * periodically. Then if the server crashed or the LFSCK is paused, the
854 * LFSCK can resume from the latest checkpoint.
856 * It also controls the whole LFSCK speed via lfsck_control_speed() to
857 * avoid the server to become overload.
859 * \param[in] env pointer to the thread context
860 * \param[in] lfsck pointer to the lfsck instance
862 * \retval positive number if all objects have been scanned
863 * \retval 0 if the iteration is stopped or paused
864 * \retval negative error number on failure
866 static int lfsck_master_oit_engine(const struct lu_env *env,
867 struct lfsck_instance *lfsck)
869 struct lfsck_thread_info *info = lfsck_env_info(env);
870 const struct dt_it_ops *iops =
871 &lfsck->li_obj_oit->do_index_ops->dio_it;
872 struct dt_it *di = lfsck->li_di_oit;
873 struct lu_fid *fid = &info->lti_fid;
874 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
875 struct ptlrpc_thread *thread = &lfsck->li_thread;
876 struct dt_device *dev = lfsck->li_bottom;
877 struct seq_server_site *ss = lu_site2seq(dev->dd_lu_dev.ld_site);
878 __u32 idx = lfsck_dev_idx(dev);
882 if (unlikely(ss == NULL))
886 struct dt_object *target;
887 bool update_lma = false;
889 if (lfsck->li_di_dir != NULL) {
890 rc = lfsck_master_dir_engine(env, lfsck);
895 if (unlikely(lfsck->li_oit_over))
898 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY1) &&
900 struct l_wait_info lwi;
902 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
904 l_wait_event(thread->t_ctl_waitq,
905 !thread_is_running(thread),
908 if (unlikely(!thread_is_running(thread))) {
909 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine "
910 "stop, cookie "LPU64"\n",
911 lfsck_lfsck2name(lfsck),
912 iops->store(env, di));
917 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
920 lfsck->li_current_oit_processed = 1;
922 if (!list_empty(&lfsck->li_list_lmv)) {
923 struct lfsck_lmv_unit *llu;
925 spin_lock(&lfsck->li_lock);
926 llu = list_entry(lfsck->li_list_lmv.next,
927 struct lfsck_lmv_unit, llu_link);
928 list_del_init(&llu->llu_link);
929 spin_unlock(&lfsck->li_lock);
931 lfsck->li_lmv = &llu->llu_lmv;
932 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
933 rc = lfsck_open_dir(env, lfsck, 0);
935 rc = lfsck_master_dir_engine(env, lfsck);
941 lfsck->li_new_scanned++;
942 lfsck->li_pos_current.lp_oit_cookie = iops->store(env, di);
943 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
945 CDEBUG(D_LFSCK, "%s: OIT scan failed at rec(): "
946 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
947 lfsck_fail(env, lfsck, true);
948 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
954 if (fid_is_idif(fid)) {
955 __u32 idx1 = fid_idif_ost_idx(fid);
957 LASSERT(!lfsck->li_master);
959 /* It is an old format device, update the LMA. */
961 struct ost_id *oi = &info->lti_oi;
963 fid_to_ostid(fid, oi);
964 ostid_to_fid(fid, oi, idx);
967 } else if (!fid_is_norm(fid) && !fid_is_igif(fid) &&
968 !fid_is_last_id(fid) &&
969 !lu_fid_eq(fid, &lfsck->li_global_root_fid)) {
971 /* If the FID/object is only used locally and invisible
972 * to external nodes, then LFSCK will not handle it.
974 * dot_lustre sequence has been handled specially. */
977 struct lu_seq_range *range = &info->lti_range;
979 if (lfsck->li_master)
980 fld_range_set_mdt(range);
982 fld_range_set_ost(range);
983 rc = fld_local_lookup(env, ss->ss_server_fld,
984 fid_seq(fid), range);
985 if (rc != 0 || range->lsr_index != idx) {
986 /* Remote object will be handled by the LFSCK
987 * instance on the MDT where the remote object
988 * really resides on. */
994 target = lfsck_object_find(env, lfsck, fid);
995 if (IS_ERR(target)) {
996 CDEBUG(D_LFSCK, "%s: OIT scan failed at find target "
997 DFID", cookie "LPU64": rc = %d\n",
998 lfsck_lfsck2name(lfsck), PFID(fid),
999 iops->store(env, di), rc);
1000 lfsck_fail(env, lfsck, true);
1001 if (bk->lb_param & LPF_FAILOUT)
1002 RETURN(PTR_ERR(target));
1007 if (dt_object_exists(target)) {
1009 rc = lfsck_update_lma(env, lfsck, target);
1011 CDEBUG(D_LFSCK, "%s: fail to update "
1012 "LMA for "DFID": rc = %d\n",
1013 lfsck_lfsck2name(lfsck),
1014 PFID(lfsck_dto2fid(target)), rc);
1017 rc = lfsck_exec_oit(env, lfsck, target);
1019 lfsck_object_put(env, target);
1020 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
1024 rc = lfsck_checkpoint(env, lfsck);
1025 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
1029 lfsck_control_speed(lfsck);
1031 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
1032 spin_lock(&lfsck->li_lock);
1033 thread_set_flags(thread, SVC_STOPPING);
1034 spin_unlock(&lfsck->li_lock);
1038 rc = iops->next(env, di);
1039 if (unlikely(rc > 0))
1040 lfsck->li_oit_over = 1;
1041 else if (likely(rc == 0))
1042 lfsck->li_current_oit_processed = 0;
1044 if (unlikely(!thread_is_running(thread))) {
1045 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
1046 "cookie "LPU64"\n", lfsck_lfsck2name(lfsck),
1047 iops->store(env, di));
1050 } while (rc == 0 || lfsck->li_di_dir != NULL);
1055 int lfsck_master_engine(void *args)
1057 struct lfsck_thread_args *lta = args;
1058 struct lu_env *env = <a->lta_env;
1059 struct lfsck_instance *lfsck = lta->lta_lfsck;
1060 struct ptlrpc_thread *thread = &lfsck->li_thread;
1061 struct dt_object *oit_obj = lfsck->li_obj_oit;
1062 const struct dt_it_ops *oit_iops = &oit_obj->do_index_ops->dio_it;
1063 struct dt_it *oit_di;
1064 struct l_wait_info lwi = { 0 };
1068 if (lfsck->li_master &&
1069 (!list_empty(&lfsck->li_list_scan) ||
1070 !list_empty(&lfsck->li_list_double_scan))) {
1071 rc = lfsck_verify_lpf(env, lfsck);
1072 /* Fail to verify the .lustre/lost+found/MDTxxxx/ may be not
1073 * fatal, because the .lustre/lost+found/ maybe not accessed
1074 * by the LFSCK if it does not add orphans or others to such
1075 * directory. So go ahead until hit failure when really uses
1078 CDEBUG(D_LFSCK, "%s: master engine fail to verify the "
1079 ".lustre/lost+found/, go ahead: rc = %d\n",
1080 lfsck_lfsck2name(lfsck), rc);
1083 oit_di = oit_iops->init(env, oit_obj, lfsck->li_args_oit, BYPASS_CAPA);
1084 if (IS_ERR(oit_di)) {
1085 rc = PTR_ERR(oit_di);
1086 CDEBUG(D_LFSCK, "%s: master engine fail to init iteration: "
1087 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1089 GOTO(fini_args, rc);
1092 spin_lock(&lfsck->li_lock);
1093 lfsck->li_di_oit = oit_di;
1094 spin_unlock(&lfsck->li_lock);
1095 rc = lfsck_prep(env, lfsck, lta->lta_lsp);
1099 CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = %#x, dir_flags = %#x, "
1100 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1101 ", pid = %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 spin_lock(&lfsck->li_lock);
1108 thread_set_flags(thread, SVC_RUNNING);
1109 spin_unlock(&lfsck->li_lock);
1110 wake_up_all(&thread->t_ctl_waitq);
1112 l_wait_event(thread->t_ctl_waitq,
1113 lfsck->li_start_unplug ||
1114 !thread_is_running(thread),
1116 if (!thread_is_running(thread))
1117 GOTO(fini_oit, rc = 0);
1119 if (!list_empty(&lfsck->li_list_scan) ||
1120 list_empty(&lfsck->li_list_double_scan))
1121 rc = lfsck_master_oit_engine(env, lfsck);
1125 CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = %#x, dir_flags = %#x, "
1126 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1127 ", pid = %d, rc = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1128 lfsck->li_pos_checkpoint.lp_oit_cookie,
1129 lfsck->li_pos_checkpoint.lp_dir_cookie,
1130 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1133 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
1134 rc = lfsck_post(env, lfsck, rc);
1136 lfsck_close_dir(env, lfsck, rc);
1139 lfsck_di_oit_put(env, lfsck);
1140 oit_iops->fini(env, oit_di);
1142 if (!list_empty(&lfsck->li_list_double_scan))
1143 rc = lfsck_double_scan(env, lfsck);
1147 lfsck_quit(env, lfsck);
1150 /* XXX: Purge the pinned objects in the future. */
1153 spin_lock(&lfsck->li_lock);
1154 thread_set_flags(thread, SVC_STOPPED);
1155 spin_unlock(&lfsck->li_lock);
1156 wake_up_all(&thread->t_ctl_waitq);
1157 lfsck_thread_args_fini(lta);
1161 static inline bool lfsck_assistant_req_empty(struct lfsck_assistant_data *lad)
1165 spin_lock(&lad->lad_lock);
1166 if (list_empty(&lad->lad_req_list))
1168 spin_unlock(&lad->lad_lock);
1174 * Query the LFSCK status from the instatnces on remote servers.
1176 * The LFSCK assistant thread queries the LFSCK instances on other
1177 * servers (MDT/OST) about their status, such as whether they have
1178 * finished the phase1/phase2 scanning or not, and so on.
1180 * \param[in] env pointer to the thread context
1181 * \param[in] com pointer to the lfsck component
1183 * \retval 0 for success
1184 * \retval negative error number on failure
1186 static int lfsck_assistant_query_others(const struct lu_env *env,
1187 struct lfsck_component *com)
1189 struct lfsck_thread_info *info = lfsck_env_info(env);
1190 struct lfsck_request *lr = &info->lti_lr;
1191 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1192 struct lfsck_instance *lfsck = com->lc_lfsck;
1193 struct lfsck_assistant_data *lad = com->lc_data;
1194 struct ptlrpc_request_set *set;
1195 struct lfsck_tgt_descs *ltds;
1196 struct lfsck_tgt_desc *ltd;
1197 struct list_head *phase_head;
1202 set = ptlrpc_prep_set();
1206 lad->lad_touch_gen++;
1207 memset(lr, 0, sizeof(*lr));
1208 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1209 lr->lr_event = LE_QUERY;
1210 lr->lr_active = com->lc_type;
1211 laia->laia_com = com;
1213 laia->laia_shared = 0;
1215 if (!list_empty(&lad->lad_mdt_phase1_list)) {
1216 ltds = &lfsck->li_mdt_descs;
1218 phase_head = &lad->lad_mdt_phase1_list;
1219 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1224 ltds = &lfsck->li_ost_descs;
1225 lr->lr_flags = LEF_TO_OST;
1226 phase_head = &lad->lad_ost_phase1_list;
1229 laia->laia_ltds = ltds;
1230 spin_lock(<ds->ltd_lock);
1231 while (!list_empty(phase_head)) {
1232 struct list_head *phase_list;
1235 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1236 ltd = list_entry(phase_head->next,
1237 struct lfsck_tgt_desc,
1238 ltd_layout_phase_list);
1239 phase_list = <d->ltd_layout_phase_list;
1240 gen = <d->ltd_layout_gen;
1242 ltd = list_entry(phase_head->next,
1243 struct lfsck_tgt_desc,
1244 ltd_namespace_phase_list);
1245 phase_list = <d->ltd_namespace_phase_list;
1246 gen = <d->ltd_namespace_gen;
1249 if (*gen == lad->lad_touch_gen)
1252 *gen = lad->lad_touch_gen;
1253 list_move_tail(phase_list, phase_head);
1254 atomic_inc(<d->ltd_ref);
1255 laia->laia_ltd = ltd;
1256 spin_unlock(<ds->ltd_lock);
1257 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1258 lfsck_async_interpret_common,
1261 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to query "
1262 "%s %x for %s: rc = %d\n",
1263 lfsck_lfsck2name(lfsck),
1264 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1265 ltd->ltd_index, lad->lad_name, rc);
1269 spin_lock(<ds->ltd_lock);
1271 spin_unlock(<ds->ltd_lock);
1273 rc = ptlrpc_set_wait(set);
1275 ptlrpc_set_destroy(set);
1279 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST) &&
1280 list_empty(&lad->lad_mdt_phase1_list))
1284 ptlrpc_set_destroy(set);
1286 RETURN(rc1 != 0 ? rc1 : rc);
1290 * Notify the LFSCK event to the instatnces on remote servers.
1292 * The LFSCK assistant thread notifies the LFSCK instances on other
1293 * servers (MDT/OST) about some events, such as start new scanning,
1294 * stop the scanning, this LFSCK instance will exit, and so on.
1296 * \param[in] env pointer to the thread context
1297 * \param[in] com pointer to the lfsck component
1298 * \param[in] lr pointer to the LFSCK event request
1300 * \retval 0 for success
1301 * \retval negative error number on failure
1303 static int lfsck_assistant_notify_others(const struct lu_env *env,
1304 struct lfsck_component *com,
1305 struct lfsck_request *lr)
1307 struct lfsck_thread_info *info = lfsck_env_info(env);
1308 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1309 struct lfsck_instance *lfsck = com->lc_lfsck;
1310 struct lfsck_assistant_data *lad = com->lc_data;
1311 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1312 struct ptlrpc_request_set *set;
1313 struct lfsck_tgt_descs *ltds;
1314 struct lfsck_tgt_desc *ltd;
1315 struct lfsck_tgt_desc *next;
1321 set = ptlrpc_prep_set();
1325 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1326 lr->lr_active = com->lc_type;
1327 laia->laia_com = com;
1329 laia->laia_shared = 0;
1331 switch (lr->lr_event) {
1333 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1336 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
1337 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ;
1338 lr->lr_speed = bk->lb_speed_limit;
1339 lr->lr_version = bk->lb_version;
1340 lr->lr_param |= bk->lb_param;
1341 lr->lr_async_windows = bk->lb_async_windows;
1342 lr->lr_flags = LEF_TO_OST;
1344 /* Notify OSTs firstly, then handle other MDTs if needed. */
1345 ltds = &lfsck->li_ost_descs;
1346 laia->laia_ltds = ltds;
1347 down_read(<ds->ltd_rw_sem);
1348 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1349 ltd = lfsck_tgt_get(ltds, idx);
1350 LASSERT(ltd != NULL);
1352 laia->laia_ltd = ltd;
1353 ltd->ltd_layout_done = 0;
1354 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1355 lfsck_async_interpret_common,
1356 laia, LFSCK_NOTIFY);
1358 lfsck_lad_set_bitmap(env, com, idx);
1359 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1360 "notify OST %x for %s start: rc = %d\n",
1361 lfsck_lfsck2name(lfsck), idx,
1366 up_read(<ds->ltd_rw_sem);
1369 rc = ptlrpc_set_wait(set);
1371 ptlrpc_set_destroy(set);
1376 if (!(bk->lb_param & LPF_ALL_TGT))
1379 /* link other MDT targets locallly. */
1380 ltds = &lfsck->li_mdt_descs;
1381 spin_lock(<ds->ltd_lock);
1382 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1383 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1384 ltd = LTD_TGT(ltds, idx);
1385 LASSERT(ltd != NULL);
1387 if (!list_empty(<d->ltd_layout_list))
1390 list_add_tail(<d->ltd_layout_list,
1391 &lad->lad_mdt_list);
1392 list_add_tail(<d->ltd_layout_phase_list,
1393 &lad->lad_mdt_phase1_list);
1396 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1397 ltd = LTD_TGT(ltds, idx);
1398 LASSERT(ltd != NULL);
1400 if (!list_empty(<d->ltd_namespace_list))
1403 list_add_tail(<d->ltd_namespace_list,
1404 &lad->lad_mdt_list);
1405 list_add_tail(<d->ltd_namespace_phase_list,
1406 &lad->lad_mdt_phase1_list);
1409 spin_unlock(<ds->ltd_lock);
1412 case LE_PHASE2_DONE:
1413 case LE_PEER_EXIT: {
1414 struct list_head *phase_head;
1416 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1417 if (bk->lb_param & LPF_ALL_TGT) {
1418 phase_head = &lad->lad_mdt_list;
1419 ltds = &lfsck->li_mdt_descs;
1420 if (lr->lr_event == LE_STOP) {
1421 /* unlink other MDT targets locallly. */
1422 spin_lock(<ds->ltd_lock);
1423 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1424 list_for_each_entry_safe(ltd, next,
1425 phase_head, ltd_layout_list) {
1427 <d->ltd_layout_phase_list);
1429 <d->ltd_layout_list);
1432 list_for_each_entry_safe(ltd, next,
1434 ltd_namespace_list) {
1436 <d->ltd_namespace_phase_list);
1438 <d->ltd_namespace_list);
1441 spin_unlock(<ds->ltd_lock);
1443 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1446 lr->lr_flags |= LEF_TO_OST;
1447 phase_head = &lad->lad_ost_list;
1448 ltds = &lfsck->li_ost_descs;
1450 lr->lr_flags &= ~LEF_TO_OST;
1452 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1455 lr->lr_flags |= LEF_TO_OST;
1456 phase_head = &lad->lad_ost_list;
1457 ltds = &lfsck->li_ost_descs;
1461 laia->laia_ltds = ltds;
1462 spin_lock(<ds->ltd_lock);
1463 while (!list_empty(phase_head)) {
1464 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1465 ltd = list_entry(phase_head->next,
1466 struct lfsck_tgt_desc,
1468 if (!list_empty(<d->ltd_layout_phase_list))
1470 <d->ltd_layout_phase_list);
1471 list_del_init(<d->ltd_layout_list);
1473 ltd = list_entry(phase_head->next,
1474 struct lfsck_tgt_desc,
1475 ltd_namespace_list);
1476 if (!list_empty(<d->ltd_namespace_phase_list))
1478 <d->ltd_namespace_phase_list);
1479 list_del_init(<d->ltd_namespace_list);
1481 atomic_inc(<d->ltd_ref);
1482 laia->laia_ltd = ltd;
1483 spin_unlock(<ds->ltd_lock);
1484 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1485 lfsck_async_interpret_common,
1486 laia, LFSCK_NOTIFY);
1488 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1489 "notify %s %x for %s stop/phase2_done/"
1490 "peer_exit: rc = %d\n",
1491 lfsck_lfsck2name(lfsck),
1492 (lr->lr_flags & LEF_TO_OST) ?
1493 "OST" : "MDT", ltd->ltd_index,
1497 spin_lock(<ds->ltd_lock);
1499 spin_unlock(<ds->ltd_lock);
1501 rc = ptlrpc_set_wait(set);
1503 ptlrpc_set_destroy(set);
1507 if (com->lc_type == LFSCK_TYPE_LAYOUT &&
1508 !(lr->lr_flags & LEF_TO_OST)) {
1509 lr->lr_flags |= LEF_TO_OST;
1510 phase_head = &lad->lad_ost_list;
1511 ltds = &lfsck->li_ost_descs;
1516 case LE_PHASE1_DONE:
1517 lad->lad_ops->la_sync_failures(env, com, lr);
1518 lad->lad_touch_gen++;
1519 ltds = &lfsck->li_mdt_descs;
1520 laia->laia_ltds = ltds;
1521 spin_lock(<ds->ltd_lock);
1522 while (!list_empty(&lad->lad_mdt_list)) {
1523 struct list_head *list;
1526 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1527 ltd = list_entry(lad->lad_mdt_list.next,
1528 struct lfsck_tgt_desc,
1530 list = <d->ltd_layout_list;
1531 gen = <d->ltd_layout_gen;
1533 struct lfsck_namespace *ns = com->lc_file_ram;
1535 ltd = list_entry(lad->lad_mdt_list.next,
1536 struct lfsck_tgt_desc,
1537 ltd_namespace_list);
1538 list = <d->ltd_namespace_list;
1539 gen = <d->ltd_namespace_gen;
1540 lr->lr_flags2 = ns->ln_flags & ~LF_INCOMPLETE;
1543 if (*gen == lad->lad_touch_gen)
1546 *gen = lad->lad_touch_gen;
1547 list_move_tail(list, &lad->lad_mdt_list);
1548 if (ltd->ltd_namespace_failed)
1551 atomic_inc(<d->ltd_ref);
1552 laia->laia_ltd = ltd;
1553 spin_unlock(<ds->ltd_lock);
1554 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1555 lfsck_async_interpret_common,
1556 laia, LFSCK_NOTIFY);
1558 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1559 "notify MDT %x for %s phase1 done: "
1560 "rc = %d\n", lfsck_lfsck2name(lfsck),
1561 ltd->ltd_index, lad->lad_name, rc);
1564 spin_lock(<ds->ltd_lock);
1566 spin_unlock(<ds->ltd_lock);
1569 CDEBUG(D_LFSCK, "%s: LFSCK assistant unexpected LFSCK event: "
1570 "rc = %d\n", lfsck_lfsck2name(lfsck), lr->lr_event);
1575 rc1 = ptlrpc_set_wait(set);
1576 ptlrpc_set_destroy(set);
1578 RETURN(rc != 0 ? rc : rc1);
1582 * The LFSCK assistant thread is triggered by the LFSCK main engine.
1583 * They co-work together as an asynchronous pipeline: the LFSCK main
1584 * engine scans the system and pre-fetches the objects, attributes,
1585 * or name entries, etc, and pushes them into the pipeline as input
1586 * requests for the LFSCK assistant thread; on the other end of the
1587 * pipeline, the LFSCK assistant thread performs the real check and
1588 * repair for every request from the main engine.
1590 * Generally, the assistant engine may be blocked when check/repair
1591 * something, so the LFSCK main engine will run some faster. On the
1592 * other hand, the LFSCK main engine will drive multiple assistant
1593 * threads in parallel, means for each LFSCK component on the master
1594 * (such as layout LFSCK, namespace LFSCK), there is an independent
1595 * LFSCK assistant thread. So under such 1:N multiple asynchronous
1596 * pipelines mode, the whole LFSCK performance will be much better
1597 * than check/repair everything by the LFSCK main engine itself.
1599 int lfsck_assistant_engine(void *args)
1601 struct lfsck_thread_args *lta = args;
1602 struct lu_env *env = <a->lta_env;
1603 struct lfsck_component *com = lta->lta_com;
1604 struct lfsck_instance *lfsck = lta->lta_lfsck;
1605 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1606 struct lfsck_position *pos = &com->lc_pos_start;
1607 struct lfsck_thread_info *info = lfsck_env_info(env);
1608 struct lfsck_request *lr = &info->lti_lr;
1609 struct lfsck_assistant_data *lad = com->lc_data;
1610 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1611 struct ptlrpc_thread *athread = &lad->lad_thread;
1612 struct lfsck_assistant_operations *lao = lad->lad_ops;
1613 struct lfsck_assistant_req *lar;
1614 struct l_wait_info lwi = { 0 };
1619 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread start\n",
1620 lfsck_lfsck2name(lfsck), lad->lad_name);
1622 memset(lr, 0, sizeof(*lr));
1623 lr->lr_event = LE_START;
1624 if (pos->lp_oit_cookie <= 1)
1625 lr->lr_param = LPF_RESET;
1626 rc = lfsck_assistant_notify_others(env, com, lr);
1628 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to notify others "
1629 "to start %s: rc = %d\n",
1630 lfsck_lfsck2name(lfsck), lad->lad_name, rc);
1634 spin_lock(&lad->lad_lock);
1635 thread_set_flags(athread, SVC_RUNNING);
1636 spin_unlock(&lad->lad_lock);
1637 wake_up_all(&mthread->t_ctl_waitq);
1640 while (!list_empty(&lad->lad_req_list)) {
1641 bool wakeup = false;
1643 if (unlikely(lad->lad_exit ||
1644 !thread_is_running(mthread)))
1645 GOTO(cleanup1, rc = lad->lad_post_result);
1647 lar = list_entry(lad->lad_req_list.next,
1648 struct lfsck_assistant_req,
1650 /* Only the lfsck_assistant_engine thread itself can
1651 * remove the "lar" from the head of the list, LFSCK
1652 * engine thread only inserts other new "lar" at the
1653 * end of the list. So it is safe to handle current
1654 * "lar" without the spin_lock. */
1655 rc = lao->la_handler_p1(env, com, lar);
1656 spin_lock(&lad->lad_lock);
1657 list_del_init(&lar->lar_list);
1658 lad->lad_prefetched--;
1659 /* Wake up the main engine thread only when the list
1660 * is empty or half of the prefetched items have been
1661 * handled to avoid too frequent thread schedule. */
1662 if (lad->lad_prefetched == 0 ||
1663 (bk->lb_async_windows != 0 &&
1664 bk->lb_async_windows / 2 ==
1665 lad->lad_prefetched))
1667 spin_unlock(&lad->lad_lock);
1669 wake_up_all(&mthread->t_ctl_waitq);
1671 lao->la_req_fini(env, lar);
1672 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1676 l_wait_event(athread->t_ctl_waitq,
1677 !lfsck_assistant_req_empty(lad) ||
1680 lad->lad_to_double_scan,
1683 if (unlikely(lad->lad_exit))
1684 GOTO(cleanup1, rc = lad->lad_post_result);
1686 if (!list_empty(&lad->lad_req_list))
1689 if (lad->lad_to_post) {
1690 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread post\n",
1691 lfsck_lfsck2name(lfsck), lad->lad_name);
1693 if (unlikely(lad->lad_exit))
1694 GOTO(cleanup1, rc = lad->lad_post_result);
1696 lad->lad_to_post = 0;
1697 LASSERT(lad->lad_post_result > 0);
1699 memset(lr, 0, sizeof(*lr));
1700 lr->lr_event = LE_PHASE1_DONE;
1701 lr->lr_status = lad->lad_post_result;
1702 rc = lfsck_assistant_notify_others(env, com, lr);
1704 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to "
1705 "notify others for %s post: rc = %d\n",
1706 lfsck_lfsck2name(lfsck),
1709 /* Wakeup the master engine to go ahead. */
1710 wake_up_all(&mthread->t_ctl_waitq);
1713 if (lad->lad_to_double_scan) {
1714 lad->lad_to_double_scan = 0;
1715 atomic_inc(&lfsck->li_double_scan_count);
1716 lad->lad_in_double_scan = 1;
1717 wake_up_all(&mthread->t_ctl_waitq);
1719 com->lc_new_checked = 0;
1720 com->lc_new_scanned = 0;
1721 com->lc_time_last_checkpoint = cfs_time_current();
1722 com->lc_time_next_checkpoint =
1723 com->lc_time_last_checkpoint +
1724 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1726 /* Flush async updates before handling orphan. */
1727 dt_sync(env, lfsck->li_next);
1729 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 "
1730 "scan start\n", lfsck_lfsck2name(lfsck));
1732 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1733 GOTO(cleanup2, rc = 0);
1735 while (lad->lad_in_double_scan) {
1736 rc = lfsck_assistant_query_others(env, com);
1737 if (lfsck_phase2_next_ready(lad))
1743 /* Pull LFSCK status on related targets once
1744 * per 30 seconds if we are not notified. */
1745 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
1746 cfs_time_seconds(1),
1748 rc = l_wait_event(athread->t_ctl_waitq,
1749 lfsck_phase2_next_ready(lad) ||
1751 !thread_is_running(mthread),
1754 if (unlikely(lad->lad_exit ||
1755 !thread_is_running(mthread)))
1756 GOTO(cleanup2, rc = 0);
1758 if (rc == -ETIMEDOUT)
1765 rc = lao->la_handler_p2(env, com);
1769 if (unlikely(lad->lad_exit ||
1770 !thread_is_running(mthread)))
1771 GOTO(cleanup2, rc = 0);
1777 /* Cleanup the unfinished requests. */
1778 spin_lock(&lad->lad_lock);
1780 lad->lad_assistant_status = rc;
1782 if (lad->lad_exit && lad->lad_post_result <= 0)
1783 lao->la_fill_pos(env, com, &lfsck->li_pos_checkpoint);
1785 while (!list_empty(&lad->lad_req_list)) {
1786 lar = list_entry(lad->lad_req_list.next,
1787 struct lfsck_assistant_req,
1789 list_del_init(&lar->lar_list);
1790 lad->lad_prefetched--;
1791 spin_unlock(&lad->lad_lock);
1792 lao->la_req_fini(env, lar);
1793 spin_lock(&lad->lad_lock);
1795 spin_unlock(&lad->lad_lock);
1797 LASSERTF(lad->lad_prefetched == 0, "unmatched prefeteched objs %d\n",
1798 lad->lad_prefetched);
1801 memset(lr, 0, sizeof(*lr));
1803 lr->lr_event = LE_PHASE2_DONE;
1805 } else if (rc == 0) {
1806 if (lfsck->li_flags & LPF_ALL_TGT) {
1807 lr->lr_event = LE_STOP;
1808 lr->lr_status = LS_STOPPED;
1810 lr->lr_event = LE_PEER_EXIT;
1811 switch (lfsck->li_status) {
1814 lr->lr_status = LS_CO_PAUSED;
1818 lr->lr_status = LS_CO_STOPPED;
1821 CDEBUG(D_LFSCK, "%s: LFSCK assistant unknown "
1822 "status: rc = %d\n",
1823 lfsck_lfsck2name(lfsck),
1825 lr->lr_status = LS_CO_FAILED;
1830 if (lfsck->li_flags & LPF_ALL_TGT) {
1831 lr->lr_event = LE_STOP;
1832 lr->lr_status = LS_FAILED;
1834 lr->lr_event = LE_PEER_EXIT;
1835 lr->lr_status = LS_CO_FAILED;
1839 rc1 = lfsck_assistant_notify_others(env, com, lr);
1841 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to notify "
1842 "others for %s quit: rc = %d\n",
1843 lfsck_lfsck2name(lfsck), lad->lad_name, rc1);
1847 /* Flush async updates before exit. */
1848 dt_sync(env, lfsck->li_next);
1850 /* Under force exit case, some requests may be just freed without
1851 * verification, those objects should be re-handled when next run.
1852 * So not update the on-disk trace file under such case. */
1853 if (lad->lad_in_double_scan) {
1855 rc1 = lao->la_double_scan_result(env, com, rc);
1857 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 scan "
1858 "finished: rc = %d\n",
1859 lfsck_lfsck2name(lfsck), rc1 != 0 ? rc1 : rc);
1863 if (lad->lad_in_double_scan)
1864 atomic_dec(&lfsck->li_double_scan_count);
1866 spin_lock(&lad->lad_lock);
1867 lad->lad_assistant_status = (rc1 != 0 ? rc1 : rc);
1868 thread_set_flags(athread, SVC_STOPPED);
1869 wake_up_all(&mthread->t_ctl_waitq);
1870 spin_unlock(&lad->lad_lock);
1872 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread exit: rc = %d\n",
1873 lfsck_lfsck2name(lfsck), lad->lad_name,
1874 lad->lad_assistant_status);
1876 lfsck_thread_args_fini(lta);