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 *dev = lfsck_obj2dev(obj);
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, dev);
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_local(env, dev, th);
145 rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMA, fl, th, BYPASS_CAPA);
150 dt_trans_stop(env, dev, 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 seq_server_site *ss = lfsck_dev_site(lfsck);
198 __u32 idx = lfsck_dev_idx(lfsck);
202 if (list_empty(&lfsck->li_list_dir) || !S_ISDIR(lfsck_object_type(obj)))
207 *fid = *lfsck_dto2fid(obj);
209 /* Global /ROOT is visible. */
210 if (unlikely(lu_fid_eq(fid, &lfsck->li_global_root_fid)))
213 /* Backend root is invisible. */
214 if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid)))
217 if (!fid_is_norm(fid) && !fid_is_igif(fid))
220 fld_range_set_mdt(range);
221 rc = fld_local_lookup(env, ss->ss_server_fld,
222 fid_seq(fid), range);
223 if (rc != 0 || range->lsr_index != idx)
224 /* Current FID should NOT be for the input parameter
225 * @obj, because the lfsck_master_oit_engine() has
226 * filtered out agent object. So current FID is for
227 * the ancestor of the original input parameter @obj.
228 * So the ancestor is a remote directory. The input
229 * parameter @obj is local directory, and should be
230 * scanned under such case. */
233 /* normal FID on this target (locally) must be for the
234 * client-side visiable object. */
235 if (fid_is_norm(fid))
239 obj = lfsck_object_find_bottom(env, lfsck, fid);
244 if (!dt_object_exists(obj))
248 dt_read_lock(env, obj, MOR_TGT_CHILD);
249 if (unlikely(lfsck_is_dead_obj(obj))) {
250 dt_read_unlock(env, obj);
255 rc = dt_xattr_get(env, obj,
256 lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK,
258 dt_read_unlock(env, obj);
262 if (rc < 0 && rc != -ENODATA)
265 rc = lfsck_parent_fid(env, obj, fid);
267 lfsck_object_put(env, obj);
273 if (!fid_is_sane(fid))
278 if (depth > 0 && obj != NULL)
279 lfsck_object_put(env, obj);
284 static int lfsck_load_stripe_lmv(const struct lu_env *env,
285 struct lfsck_instance *lfsck,
286 struct dt_object *obj)
288 struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
289 struct lfsck_lmv *llmv;
293 LASSERT(lfsck->li_obj_dir == NULL);
294 LASSERT(lfsck->li_lmv == NULL);
296 rc = lfsck_read_stripe_lmv(env, obj, lmv);
297 if (rc == -ENODATA) {
298 lfsck->li_obj_dir = lfsck_object_get(obj);
310 if (lmv->lmv_magic == LMV_MAGIC) {
311 struct lfsck_slave_lmv_rec *lslr;
314 llmv->ll_lmv_master = 1;
315 if (lmv->lmv_stripe_count < 1)
316 stripes = LFSCK_LMV_DEF_STRIPES;
317 else if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
318 stripes = LFSCK_LMV_MAX_STRIPES;
320 stripes = lmv->lmv_stripe_count;
322 OBD_ALLOC_LARGE(lslr, sizeof(*lslr) * stripes);
329 llmv->ll_stripes_allocated = stripes;
330 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
331 llmv->ll_lslr = lslr;
333 llmv->ll_lmv_slave = 1;
336 lfsck->li_obj_dir = lfsck_object_get(obj);
338 atomic_set(&llmv->ll_ref, 1);
339 lfsck->li_lmv = llmv;
344 /* LFSCK wrap functions */
346 static void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
349 struct lfsck_component *com;
351 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
352 com->lc_ops->lfsck_fail(env, com, new_checked);
356 void lfsck_close_dir(const struct lu_env *env,
357 struct lfsck_instance *lfsck, int result)
359 struct lfsck_component *com;
362 if (lfsck->li_lmv != NULL) {
363 lfsck->li_lmv->ll_exit_value = result;
364 if (lfsck->li_obj_dir != NULL) {
365 list_for_each_entry(com, &lfsck->li_list_dir,
367 com->lc_ops->lfsck_close_dir(env, com);
371 lfsck_lmv_put(env, lfsck->li_lmv);
372 lfsck->li_lmv = NULL;
375 if (lfsck->li_di_dir != NULL) {
376 const struct dt_it_ops *dir_iops;
377 struct dt_it *dir_di = lfsck->li_di_dir;
379 LASSERT(lfsck->li_obj_dir != NULL);
381 dir_iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
382 lfsck_di_dir_put(env, lfsck);
383 dir_iops->fini(env, dir_di);
386 if (lfsck->li_obj_dir != NULL) {
387 struct dt_object *dir_obj = lfsck->li_obj_dir;
389 lfsck->li_obj_dir = NULL;
390 lfsck_object_put(env, dir_obj);
396 int lfsck_open_dir(const struct lu_env *env,
397 struct lfsck_instance *lfsck, __u64 cookie)
399 struct dt_object *obj = lfsck->li_obj_dir;
400 struct dt_it *di = lfsck->li_di_dir;
401 struct lfsck_component *com;
402 const struct dt_it_ops *iops;
406 LASSERT(obj != NULL);
409 if (unlikely(!dt_try_as_dir(env, obj)))
410 GOTO(out, rc = -ENOTDIR);
412 list_for_each_entry(com, &lfsck->li_list_dir, lc_link_dir) {
413 rc = com->lc_ops->lfsck_open_dir(env, com);
418 iops = &obj->do_index_ops->dio_it;
419 di = iops->init(env, obj, lfsck->li_args_dir, BYPASS_CAPA);
421 GOTO(out, rc = PTR_ERR(di));
423 rc = iops->load(env, di, cookie);
424 if (rc == 0 || (rc > 0 && cookie > 0))
425 rc = iops->next(env, di);
433 lfsck->li_cookie_dir = iops->store(env, di);
434 spin_lock(&lfsck->li_lock);
435 lfsck->li_di_dir = di;
436 spin_unlock(&lfsck->li_lock);
443 lfsck_close_dir(env, lfsck, rc);
448 static int lfsck_checkpoint(const struct lu_env *env,
449 struct lfsck_instance *lfsck)
451 struct lfsck_component *com;
455 if (likely(cfs_time_beforeq(cfs_time_current(),
456 lfsck->li_time_next_checkpoint)))
459 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
460 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
461 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
466 lfsck->li_time_last_checkpoint = cfs_time_current();
467 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
468 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
469 return rc1 != 0 ? rc1 : rc;
472 static int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
473 struct lfsck_start_param *lsp)
475 struct dt_object *obj = NULL;
476 struct lfsck_component *com;
477 struct lfsck_component *next;
478 struct lfsck_position *pos = NULL;
479 const struct dt_it_ops *iops =
480 &lfsck->li_obj_oit->do_index_ops->dio_it;
484 LASSERT(lfsck->li_obj_dir == NULL);
485 LASSERT(lfsck->li_di_dir == NULL);
487 lfsck->li_current_oit_processed = 0;
488 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
489 com->lc_new_checked = 0;
490 rc = com->lc_ops->lfsck_prep(env, com, lsp);
495 (!lfsck_pos_is_zero(&com->lc_pos_start) &&
496 lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
497 pos = &com->lc_pos_start;
500 /* Init otable-based iterator. */
502 rc = iops->load(env, lfsck->li_di_oit, 0);
504 lfsck->li_oit_over = 1;
511 rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
515 lfsck->li_oit_over = 1;
517 if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
520 /* Find the directory for namespace-based traverse. */
521 obj = lfsck_object_find_bottom(env, lfsck, &pos->lp_dir_parent);
523 RETURN(PTR_ERR(obj));
525 /* Remote directory will be scanned by the LFSCK instance
526 * on the MDT where the remote object really resides on. */
527 if (!dt_object_exists(obj) || dt_object_remote(obj) ||
528 unlikely(!S_ISDIR(lfsck_object_type(obj))))
531 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
533 /* For the master MDT-object of a striped directory,
534 * reset the iteration from the directory beginning. */
535 if (lfsck->li_lmv != NULL && lfsck->li_lmv->ll_lmv_master)
536 pos->lp_dir_cookie = 0;
538 rc = lfsck_open_dir(env, lfsck, pos->lp_dir_cookie);
540 /* The end of the directory. */
548 lfsck_object_put(env, obj);
551 lfsck_close_dir(env, lfsck, rc);
552 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
554 com->lc_ops->lfsck_post(env, com, rc, true);
561 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, true);
562 lfsck->li_pos_current = lfsck->li_pos_checkpoint;
563 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
564 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
569 lfsck->li_time_last_checkpoint = cfs_time_current();
570 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
571 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
575 static int lfsck_exec_oit(const struct lu_env *env,
576 struct lfsck_instance *lfsck, struct dt_object *obj)
578 struct lfsck_component *com;
582 LASSERT(lfsck->li_obj_dir == NULL);
584 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
585 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
590 rc = lfsck_needs_scan_dir(env, lfsck, obj);
594 rc = lfsck_load_stripe_lmv(env, lfsck, obj);
596 rc = lfsck_open_dir(env, lfsck, 0);
602 lfsck_fail(env, lfsck, false);
605 lfsck_close_dir(env, lfsck, rc);
607 return rc > 0 ? 0 : rc;
610 static int lfsck_exec_dir(const struct lu_env *env,
611 struct lfsck_instance *lfsck,
612 struct lu_dirent *ent, __u16 type)
614 struct lfsck_component *com;
617 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
618 rc = com->lc_ops->lfsck_exec_dir(env, com, ent, type);
625 static int lfsck_master_dir_engine(const struct lu_env *env,
626 struct lfsck_instance *lfsck);
628 static int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
631 struct lfsck_component *com;
632 struct lfsck_component *next;
635 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
636 lfsck_close_dir(env, lfsck, result);
638 while (thread_is_running(&lfsck->li_thread) && rc > 0 &&
639 !list_empty(&lfsck->li_list_lmv)) {
640 struct lfsck_lmv_unit *llu;
642 spin_lock(&lfsck->li_lock);
643 llu = list_entry(lfsck->li_list_lmv.next,
644 struct lfsck_lmv_unit, llu_link);
645 list_del_init(&llu->llu_link);
646 spin_unlock(&lfsck->li_lock);
648 lfsck->li_lmv = &llu->llu_lmv;
649 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
650 rc = lfsck_open_dir(env, lfsck, 0);
652 rc = lfsck_master_dir_engine(env, lfsck);
653 lfsck_close_dir(env, lfsck, result);
659 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
660 rc = com->lc_ops->lfsck_post(env, com, result, false);
662 CDEBUG(D_LFSCK, "%s: lfsck_post at the component %u: "
663 "rc = %d\n", lfsck_lfsck2name(lfsck),
664 (__u32)com->lc_type, rc);
667 lfsck->li_time_last_checkpoint = cfs_time_current();
668 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
669 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
671 /* Ignore some component post failure to make other can go ahead. */
675 static int lfsck_double_scan(const struct lu_env *env,
676 struct lfsck_instance *lfsck)
678 struct lfsck_component *com;
679 struct lfsck_component *next;
680 struct l_wait_info lwi = { 0 };
684 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
685 rc = com->lc_ops->lfsck_double_scan(env, com);
690 l_wait_event(lfsck->li_thread.t_ctl_waitq,
691 atomic_read(&lfsck->li_double_scan_count) == 0,
694 if (lfsck->li_status != LS_PAUSED &&
695 lfsck->li_status != LS_CO_PAUSED) {
696 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
698 spin_lock(&lfsck->li_lock);
699 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
700 spin_unlock(&lfsck->li_lock);
704 return rc1 != 0 ? rc1 : rc;
707 static void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck)
709 struct lfsck_component *com;
710 struct lfsck_component *next;
712 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
714 if (com->lc_ops->lfsck_quit != NULL)
715 com->lc_ops->lfsck_quit(env, com);
717 spin_lock(&lfsck->li_lock);
718 list_del_init(&com->lc_link_dir);
719 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
720 spin_unlock(&lfsck->li_lock);
723 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
725 if (com->lc_ops->lfsck_quit != NULL)
726 com->lc_ops->lfsck_quit(env, com);
728 spin_lock(&lfsck->li_lock);
729 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
730 spin_unlock(&lfsck->li_lock);
736 static int lfsck_master_dir_engine(const struct lu_env *env,
737 struct lfsck_instance *lfsck)
739 struct lfsck_thread_info *info = lfsck_env_info(env);
740 struct dt_object *dir = lfsck->li_obj_dir;
741 const struct dt_it_ops *iops = &dir->do_index_ops->dio_it;
742 struct dt_it *di = lfsck->li_di_dir;
743 struct lu_dirent *ent =
744 (struct lu_dirent *)info->lti_key;
745 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
746 struct ptlrpc_thread *thread = &lfsck->li_thread;
752 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY2, cfs_fail_val) &&
753 unlikely(!thread_is_running(thread))) {
754 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
755 "parent "DFID", cookie "LPX64"\n",
756 lfsck_lfsck2name(lfsck),
757 PFID(lfsck_dto2fid(dir)), lfsck->li_cookie_dir);
762 lfsck->li_new_scanned++;
763 rc = iops->rec(env, di, (struct dt_rec *)ent,
766 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
770 CDEBUG(D_LFSCK, "%s: scan dir failed at rec(), "
771 "parent "DFID", cookie "LPX64": rc = %d\n",
772 lfsck_lfsck2name(lfsck),
773 PFID(lfsck_dto2fid(dir)),
774 lfsck->li_cookie_dir, rc);
775 lfsck_fail(env, lfsck, true);
776 if (bk->lb_param & LPF_FAILOUT)
782 if (ent->lde_attrs & LUDA_IGNORE &&
783 strcmp(ent->lde_name, dotdot) != 0)
786 /* The type in the @ent structure may has been overwritten,
787 * so we need to pass the @type parameter independently. */
788 rc = lfsck_exec_dir(env, lfsck, ent, type);
789 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
793 rc = lfsck_checkpoint(env, lfsck);
794 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
798 lfsck_control_speed(lfsck);
799 if (unlikely(!thread_is_running(thread))) {
800 CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
801 "parent "DFID", cookie "LPX64"\n",
802 lfsck_lfsck2name(lfsck),
803 PFID(lfsck_dto2fid(dir)),
804 lfsck->li_cookie_dir);
808 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
809 spin_lock(&lfsck->li_lock);
810 thread_set_flags(thread, SVC_STOPPING);
811 spin_unlock(&lfsck->li_lock);
815 rc = iops->next(env, di);
818 if (rc > 0 && !lfsck->li_oit_over)
819 lfsck_close_dir(env, lfsck, rc);
825 * Object-table based iteration engine.
827 * Object-table based iteration is the basic linear engine to scan all the
828 * objects on current device in turn. For each object, it calls all the
829 * registered LFSCK component(s)' API to perform related consistency
832 * It flushes related LFSCK trace files to disk via making checkpoint
833 * periodically. Then if the server crashed or the LFSCK is paused, the
834 * LFSCK can resume from the latest checkpoint.
836 * It also controls the whole LFSCK speed via lfsck_control_speed() to
837 * avoid the server to become overload.
839 * \param[in] env pointer to the thread context
840 * \param[in] lfsck pointer to the lfsck instance
842 * \retval positive number if all objects have been scanned
843 * \retval 0 if the iteration is stopped or paused
844 * \retval negative error number on failure
846 static int lfsck_master_oit_engine(const struct lu_env *env,
847 struct lfsck_instance *lfsck)
849 struct lfsck_thread_info *info = lfsck_env_info(env);
850 const struct dt_it_ops *iops =
851 &lfsck->li_obj_oit->do_index_ops->dio_it;
852 struct dt_it *di = lfsck->li_di_oit;
853 struct lu_fid *fid = &info->lti_fid;
854 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
855 struct ptlrpc_thread *thread = &lfsck->li_thread;
856 struct seq_server_site *ss = lfsck_dev_site(lfsck);
857 __u32 idx = lfsck_dev_idx(lfsck);
861 if (unlikely(ss == NULL))
865 struct dt_object *target;
866 bool update_lma = false;
868 if (lfsck->li_di_dir != NULL) {
869 rc = lfsck_master_dir_engine(env, lfsck);
874 if (unlikely(lfsck->li_oit_over))
877 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY1, cfs_fail_val) &&
878 unlikely(!thread_is_running(thread))) {
879 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
881 lfsck_lfsck2name(lfsck), iops->store(env, di));
886 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
889 lfsck->li_current_oit_processed = 1;
891 if (!list_empty(&lfsck->li_list_lmv)) {
892 struct lfsck_lmv_unit *llu;
894 spin_lock(&lfsck->li_lock);
895 llu = list_entry(lfsck->li_list_lmv.next,
896 struct lfsck_lmv_unit, llu_link);
897 list_del_init(&llu->llu_link);
898 spin_unlock(&lfsck->li_lock);
900 lfsck->li_lmv = &llu->llu_lmv;
901 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
902 rc = lfsck_open_dir(env, lfsck, 0);
904 rc = lfsck_master_dir_engine(env, lfsck);
910 lfsck->li_new_scanned++;
911 lfsck->li_pos_current.lp_oit_cookie = iops->store(env, di);
912 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
914 CDEBUG(D_LFSCK, "%s: OIT scan failed at rec(): "
915 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
916 lfsck_fail(env, lfsck, true);
917 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
923 if (unlikely(!fid_is_sane(fid))) {
924 CDEBUG(D_LFSCK, "%s: OIT scan find invalid FID "DFID
926 lfsck_lfsck2name(lfsck), PFID(fid));
930 if (fid_is_idif(fid)) {
931 __u32 idx1 = fid_idif_ost_idx(fid);
933 LASSERT(!lfsck->li_master);
935 /* It is an old format device, update the LMA. */
937 struct ost_id *oi = &info->lti_oi;
939 fid_to_ostid(fid, oi);
940 ostid_to_fid(fid, oi, idx);
943 } else if (!fid_is_norm(fid) && !fid_is_igif(fid) &&
944 !fid_is_last_id(fid) &&
945 !lu_fid_eq(fid, &lfsck->li_global_root_fid)) {
947 /* If the FID/object is only used locally and invisible
948 * to external nodes, then LFSCK will not handle it.
950 * dot_lustre sequence has been handled specially. */
953 struct lu_seq_range *range = &info->lti_range;
955 if (lfsck->li_master)
956 fld_range_set_mdt(range);
958 fld_range_set_ost(range);
959 rc = fld_local_lookup(env, ss->ss_server_fld,
960 fid_seq(fid), range);
961 if (rc != 0 || range->lsr_index != idx) {
962 /* Remote object will be handled by the LFSCK
963 * instance on the MDT where the remote object
964 * really resides on. */
970 target = lfsck_object_find_bottom(env, lfsck, fid);
971 if (IS_ERR(target)) {
972 CDEBUG(D_LFSCK, "%s: OIT scan failed at find target "
973 DFID", cookie "LPU64": rc = %d\n",
974 lfsck_lfsck2name(lfsck), PFID(fid),
975 iops->store(env, di), rc);
976 lfsck_fail(env, lfsck, true);
977 if (bk->lb_param & LPF_FAILOUT)
978 RETURN(PTR_ERR(target));
983 if (dt_object_exists(target)) {
985 rc = lfsck_update_lma(env, lfsck, target);
987 CDEBUG(D_LFSCK, "%s: fail to update "
988 "LMA for "DFID": rc = %d\n",
989 lfsck_lfsck2name(lfsck),
990 PFID(lfsck_dto2fid(target)), rc);
993 rc = lfsck_exec_oit(env, lfsck, target);
995 lfsck_object_put(env, target);
996 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
1000 rc = lfsck_checkpoint(env, lfsck);
1001 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
1005 lfsck_control_speed(lfsck);
1007 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
1008 spin_lock(&lfsck->li_lock);
1009 thread_set_flags(thread, SVC_STOPPING);
1010 spin_unlock(&lfsck->li_lock);
1014 rc = iops->next(env, di);
1015 if (unlikely(rc > 0))
1016 lfsck->li_oit_over = 1;
1017 else if (likely(rc == 0))
1018 lfsck->li_current_oit_processed = 0;
1020 if (unlikely(!thread_is_running(thread))) {
1021 CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
1022 "cookie "LPU64"\n", lfsck_lfsck2name(lfsck),
1023 iops->store(env, di));
1026 } while (rc == 0 || lfsck->li_di_dir != NULL);
1031 int lfsck_master_engine(void *args)
1033 struct lfsck_thread_args *lta = args;
1034 struct lu_env *env = <a->lta_env;
1035 struct lfsck_instance *lfsck = lta->lta_lfsck;
1036 struct ptlrpc_thread *thread = &lfsck->li_thread;
1037 struct dt_object *oit_obj = lfsck->li_obj_oit;
1038 const struct dt_it_ops *oit_iops = &oit_obj->do_index_ops->dio_it;
1039 struct dt_it *oit_di;
1040 struct l_wait_info lwi = { 0 };
1044 if (lfsck->li_master &&
1045 (!list_empty(&lfsck->li_list_scan) ||
1046 !list_empty(&lfsck->li_list_double_scan))) {
1047 rc = lfsck_verify_lpf(env, lfsck);
1048 /* Fail to verify the .lustre/lost+found/MDTxxxx/ may be not
1049 * fatal, because the .lustre/lost+found/ maybe not accessed
1050 * by the LFSCK if it does not add orphans or others to such
1051 * directory. So go ahead until hit failure when really uses
1054 CDEBUG(D_LFSCK, "%s: master engine fail to verify the "
1055 ".lustre/lost+found/, go ahead: rc = %d\n",
1056 lfsck_lfsck2name(lfsck), rc);
1059 oit_di = oit_iops->init(env, oit_obj, lfsck->li_args_oit, BYPASS_CAPA);
1060 if (IS_ERR(oit_di)) {
1061 rc = PTR_ERR(oit_di);
1062 CDEBUG(D_LFSCK, "%s: master engine fail to init iteration: "
1063 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1065 GOTO(fini_args, rc);
1068 spin_lock(&lfsck->li_lock);
1069 lfsck->li_di_oit = oit_di;
1070 spin_unlock(&lfsck->li_lock);
1071 rc = lfsck_prep(env, lfsck, lta->lta_lsp);
1075 CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = %#x, dir_flags = %#x, "
1076 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1077 ", pid = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1078 lfsck->li_pos_checkpoint.lp_oit_cookie,
1079 lfsck->li_pos_checkpoint.lp_dir_cookie,
1080 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1083 spin_lock(&lfsck->li_lock);
1084 thread_set_flags(thread, SVC_RUNNING);
1085 spin_unlock(&lfsck->li_lock);
1086 wake_up_all(&thread->t_ctl_waitq);
1088 l_wait_event(thread->t_ctl_waitq,
1089 lfsck->li_start_unplug ||
1090 !thread_is_running(thread),
1092 if (!thread_is_running(thread))
1093 GOTO(fini_oit, rc = 0);
1095 if (!list_empty(&lfsck->li_list_scan) ||
1096 list_empty(&lfsck->li_list_double_scan))
1097 rc = lfsck_master_oit_engine(env, lfsck);
1101 lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
1102 CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = %#x, dir_flags = %#x, "
1103 "oit_cookie = "LPU64", dir_cookie = "LPX64", parent = "DFID
1104 ", pid = %d, rc = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1105 lfsck->li_pos_checkpoint.lp_oit_cookie,
1106 lfsck->li_pos_checkpoint.lp_dir_cookie,
1107 PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1110 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
1111 rc = lfsck_post(env, lfsck, rc);
1113 lfsck_close_dir(env, lfsck, rc);
1116 lfsck_di_oit_put(env, lfsck);
1117 oit_iops->fini(env, oit_di);
1119 if (!list_empty(&lfsck->li_list_double_scan))
1120 rc = lfsck_double_scan(env, lfsck);
1124 lfsck_quit(env, lfsck);
1127 /* XXX: Purge the pinned objects in the future. */
1130 spin_lock(&lfsck->li_lock);
1131 thread_set_flags(thread, SVC_STOPPED);
1132 spin_unlock(&lfsck->li_lock);
1133 wake_up_all(&thread->t_ctl_waitq);
1134 lfsck_thread_args_fini(lta);
1138 static inline bool lfsck_assistant_req_empty(struct lfsck_assistant_data *lad)
1142 spin_lock(&lad->lad_lock);
1143 if (list_empty(&lad->lad_req_list))
1145 spin_unlock(&lad->lad_lock);
1151 * Query the LFSCK status from the instatnces on remote servers.
1153 * The LFSCK assistant thread queries the LFSCK instances on other
1154 * servers (MDT/OST) about their status, such as whether they have
1155 * finished the phase1/phase2 scanning or not, and so on.
1157 * \param[in] env pointer to the thread context
1158 * \param[in] com pointer to the lfsck component
1160 * \retval 0 for success
1161 * \retval negative error number on failure
1163 static int lfsck_assistant_query_others(const struct lu_env *env,
1164 struct lfsck_component *com)
1166 struct lfsck_thread_info *info = lfsck_env_info(env);
1167 struct lfsck_request *lr = &info->lti_lr;
1168 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1169 struct lfsck_instance *lfsck = com->lc_lfsck;
1170 struct lfsck_assistant_data *lad = com->lc_data;
1171 struct ptlrpc_request_set *set;
1172 struct lfsck_tgt_descs *ltds;
1173 struct lfsck_tgt_desc *ltd;
1174 struct list_head *phase_head;
1179 set = ptlrpc_prep_set();
1183 lad->lad_touch_gen++;
1184 memset(lr, 0, sizeof(*lr));
1185 lr->lr_event = LE_QUERY;
1186 lr->lr_active = com->lc_type;
1187 laia->laia_com = com;
1189 laia->laia_shared = 0;
1191 if (!list_empty(&lad->lad_mdt_phase1_list)) {
1192 ltds = &lfsck->li_mdt_descs;
1194 phase_head = &lad->lad_mdt_phase1_list;
1195 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1200 ltds = &lfsck->li_ost_descs;
1201 lr->lr_flags = LEF_TO_OST;
1202 phase_head = &lad->lad_ost_phase1_list;
1205 laia->laia_ltds = ltds;
1206 spin_lock(<ds->ltd_lock);
1207 while (!list_empty(phase_head)) {
1208 struct list_head *phase_list;
1211 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1212 ltd = list_entry(phase_head->next,
1213 struct lfsck_tgt_desc,
1214 ltd_layout_phase_list);
1215 phase_list = <d->ltd_layout_phase_list;
1216 gen = <d->ltd_layout_gen;
1218 ltd = list_entry(phase_head->next,
1219 struct lfsck_tgt_desc,
1220 ltd_namespace_phase_list);
1221 phase_list = <d->ltd_namespace_phase_list;
1222 gen = <d->ltd_namespace_gen;
1225 if (*gen == lad->lad_touch_gen)
1228 *gen = lad->lad_touch_gen;
1229 list_move_tail(phase_list, phase_head);
1230 atomic_inc(<d->ltd_ref);
1231 laia->laia_ltd = ltd;
1232 spin_unlock(<ds->ltd_lock);
1233 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1234 lfsck_async_interpret_common,
1237 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to query "
1238 "%s %x for %s: rc = %d\n",
1239 lfsck_lfsck2name(lfsck),
1240 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1241 ltd->ltd_index, lad->lad_name, rc);
1245 spin_lock(<ds->ltd_lock);
1247 spin_unlock(<ds->ltd_lock);
1249 rc = ptlrpc_set_wait(set);
1251 ptlrpc_set_destroy(set);
1255 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST) &&
1256 list_empty(&lad->lad_mdt_phase1_list))
1260 ptlrpc_set_destroy(set);
1262 RETURN(rc1 != 0 ? rc1 : rc);
1266 * Notify the LFSCK event to the instances on remote servers.
1268 * The LFSCK assistant thread notifies the LFSCK instances on other
1269 * servers (MDT/OST) about some events, such as start new scanning,
1270 * stop the scanning, this LFSCK instance will exit, and so on.
1272 * \param[in] env pointer to the thread context
1273 * \param[in] com pointer to the lfsck component
1274 * \param[in] lr pointer to the LFSCK event request
1276 * \retval 0 for success
1277 * \retval negative error number on failure
1279 static int lfsck_assistant_notify_others(const struct lu_env *env,
1280 struct lfsck_component *com,
1281 struct lfsck_request *lr)
1283 struct lfsck_thread_info *info = lfsck_env_info(env);
1284 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1285 struct lfsck_instance *lfsck = com->lc_lfsck;
1286 struct lfsck_assistant_data *lad = com->lc_data;
1287 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1288 struct ptlrpc_request_set *set;
1289 struct lfsck_tgt_descs *ltds;
1290 struct lfsck_tgt_desc *ltd;
1291 struct lfsck_tgt_desc *next;
1297 set = ptlrpc_prep_set();
1301 lr->lr_index = lfsck_dev_idx(lfsck);
1302 lr->lr_active = com->lc_type;
1303 laia->laia_com = com;
1305 laia->laia_shared = 0;
1307 switch (lr->lr_event) {
1309 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1312 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN;
1313 lr->lr_speed = bk->lb_speed_limit;
1314 lr->lr_version = bk->lb_version;
1315 lr->lr_param |= bk->lb_param;
1316 lr->lr_async_windows = bk->lb_async_windows;
1317 lr->lr_flags = LEF_TO_OST;
1319 /* Notify OSTs firstly, then handle other MDTs if needed. */
1320 ltds = &lfsck->li_ost_descs;
1321 laia->laia_ltds = ltds;
1322 down_read(<ds->ltd_rw_sem);
1323 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1324 ltd = lfsck_tgt_get(ltds, idx);
1325 LASSERT(ltd != NULL);
1327 laia->laia_ltd = ltd;
1328 ltd->ltd_layout_done = 0;
1329 ltd->ltd_synced_failures = 0;
1330 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1331 lfsck_async_interpret_common,
1332 laia, LFSCK_NOTIFY);
1334 lfsck_lad_set_bitmap(env, com, idx);
1335 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1336 "notify OST %x for %s start: rc = %d\n",
1337 lfsck_lfsck2name(lfsck), idx,
1342 up_read(<ds->ltd_rw_sem);
1345 rc = ptlrpc_set_wait(set);
1347 ptlrpc_set_destroy(set);
1352 if (!(bk->lb_param & LPF_ALL_TGT))
1355 /* link other MDT targets locallly. */
1356 ltds = &lfsck->li_mdt_descs;
1357 spin_lock(<ds->ltd_lock);
1358 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1359 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1360 ltd = LTD_TGT(ltds, idx);
1361 LASSERT(ltd != NULL);
1363 if (!list_empty(<d->ltd_layout_list))
1366 list_add_tail(<d->ltd_layout_list,
1367 &lad->lad_mdt_list);
1368 list_add_tail(<d->ltd_layout_phase_list,
1369 &lad->lad_mdt_phase1_list);
1372 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1373 ltd = LTD_TGT(ltds, idx);
1374 LASSERT(ltd != NULL);
1376 if (!list_empty(<d->ltd_namespace_list))
1379 list_add_tail(<d->ltd_namespace_list,
1380 &lad->lad_mdt_list);
1381 list_add_tail(<d->ltd_namespace_phase_list,
1382 &lad->lad_mdt_phase1_list);
1385 spin_unlock(<ds->ltd_lock);
1388 case LE_PHASE2_DONE:
1389 case LE_PEER_EXIT: {
1390 struct list_head *phase_head;
1392 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1393 if (bk->lb_param & LPF_ALL_TGT) {
1394 phase_head = &lad->lad_mdt_list;
1395 ltds = &lfsck->li_mdt_descs;
1396 if (lr->lr_event == LE_STOP) {
1397 /* unlink other MDT targets locallly. */
1398 spin_lock(<ds->ltd_lock);
1399 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1400 list_for_each_entry_safe(ltd, next,
1401 phase_head, ltd_layout_list) {
1403 <d->ltd_layout_phase_list);
1405 <d->ltd_layout_list);
1408 list_for_each_entry_safe(ltd, next,
1410 ltd_namespace_list) {
1412 <d->ltd_namespace_phase_list);
1414 <d->ltd_namespace_list);
1417 spin_unlock(<ds->ltd_lock);
1419 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1422 lr->lr_flags |= LEF_TO_OST;
1423 phase_head = &lad->lad_ost_list;
1424 ltds = &lfsck->li_ost_descs;
1426 lr->lr_flags &= ~LEF_TO_OST;
1428 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1431 lr->lr_flags |= LEF_TO_OST;
1432 phase_head = &lad->lad_ost_list;
1433 ltds = &lfsck->li_ost_descs;
1437 laia->laia_ltds = ltds;
1438 spin_lock(<ds->ltd_lock);
1439 while (!list_empty(phase_head)) {
1440 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1441 ltd = list_entry(phase_head->next,
1442 struct lfsck_tgt_desc,
1444 if (!list_empty(<d->ltd_layout_phase_list))
1446 <d->ltd_layout_phase_list);
1447 list_del_init(<d->ltd_layout_list);
1449 ltd = list_entry(phase_head->next,
1450 struct lfsck_tgt_desc,
1451 ltd_namespace_list);
1452 if (!list_empty(<d->ltd_namespace_phase_list))
1454 <d->ltd_namespace_phase_list);
1455 list_del_init(<d->ltd_namespace_list);
1457 atomic_inc(<d->ltd_ref);
1458 laia->laia_ltd = ltd;
1459 spin_unlock(<ds->ltd_lock);
1460 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1461 lfsck_async_interpret_common,
1462 laia, LFSCK_NOTIFY);
1464 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1465 "notify %s %x for %s stop/phase2_done/"
1466 "peer_exit: rc = %d\n",
1467 lfsck_lfsck2name(lfsck),
1468 (lr->lr_flags & LEF_TO_OST) ?
1469 "OST" : "MDT", ltd->ltd_index,
1473 spin_lock(<ds->ltd_lock);
1475 spin_unlock(<ds->ltd_lock);
1477 rc = ptlrpc_set_wait(set);
1479 ptlrpc_set_destroy(set);
1483 if (com->lc_type == LFSCK_TYPE_LAYOUT &&
1484 !(lr->lr_flags & LEF_TO_OST)) {
1485 lr->lr_flags |= LEF_TO_OST;
1486 phase_head = &lad->lad_ost_list;
1487 ltds = &lfsck->li_ost_descs;
1492 case LE_PHASE1_DONE:
1493 lad->lad_ops->la_sync_failures(env, com, lr);
1494 lad->lad_touch_gen++;
1495 ltds = &lfsck->li_mdt_descs;
1496 laia->laia_ltds = ltds;
1497 spin_lock(<ds->ltd_lock);
1498 while (!list_empty(&lad->lad_mdt_list)) {
1499 struct list_head *list;
1502 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1503 ltd = list_entry(lad->lad_mdt_list.next,
1504 struct lfsck_tgt_desc,
1506 list = <d->ltd_layout_list;
1507 gen = <d->ltd_layout_gen;
1509 struct lfsck_namespace *ns = com->lc_file_ram;
1511 ltd = list_entry(lad->lad_mdt_list.next,
1512 struct lfsck_tgt_desc,
1513 ltd_namespace_list);
1514 list = <d->ltd_namespace_list;
1515 gen = <d->ltd_namespace_gen;
1516 lr->lr_flags2 = ns->ln_flags & ~LF_INCOMPLETE;
1519 if (*gen == lad->lad_touch_gen)
1522 *gen = lad->lad_touch_gen;
1523 list_move_tail(list, &lad->lad_mdt_list);
1524 if (ltd->ltd_synced_failures)
1527 atomic_inc(<d->ltd_ref);
1528 laia->laia_ltd = ltd;
1529 spin_unlock(<ds->ltd_lock);
1530 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1531 lfsck_async_interpret_common,
1532 laia, LFSCK_NOTIFY);
1534 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1535 "notify MDT %x for %s phase1 done: "
1536 "rc = %d\n", lfsck_lfsck2name(lfsck),
1537 ltd->ltd_index, lad->lad_name, rc);
1540 spin_lock(<ds->ltd_lock);
1542 spin_unlock(<ds->ltd_lock);
1545 CDEBUG(D_LFSCK, "%s: LFSCK assistant unexpected LFSCK event: "
1546 "rc = %d\n", lfsck_lfsck2name(lfsck), lr->lr_event);
1551 rc1 = ptlrpc_set_wait(set);
1552 ptlrpc_set_destroy(set);
1554 RETURN(rc != 0 ? rc : rc1);
1558 * The LFSCK assistant thread is triggered by the LFSCK main engine.
1559 * They co-work together as an asynchronous pipeline: the LFSCK main
1560 * engine scans the system and pre-fetches the objects, attributes,
1561 * or name entries, etc, and pushes them into the pipeline as input
1562 * requests for the LFSCK assistant thread; on the other end of the
1563 * pipeline, the LFSCK assistant thread performs the real check and
1564 * repair for every request from the main engine.
1566 * Generally, the assistant engine may be blocked when check/repair
1567 * something, so the LFSCK main engine will run some faster. On the
1568 * other hand, the LFSCK main engine will drive multiple assistant
1569 * threads in parallel, means for each LFSCK component on the master
1570 * (such as layout LFSCK, namespace LFSCK), there is an independent
1571 * LFSCK assistant thread. So under such 1:N multiple asynchronous
1572 * pipelines mode, the whole LFSCK performance will be much better
1573 * than check/repair everything by the LFSCK main engine itself.
1575 int lfsck_assistant_engine(void *args)
1577 struct lfsck_thread_args *lta = args;
1578 struct lu_env *env = <a->lta_env;
1579 struct lfsck_component *com = lta->lta_com;
1580 struct lfsck_instance *lfsck = lta->lta_lfsck;
1581 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1582 struct lfsck_position *pos = &com->lc_pos_start;
1583 struct lfsck_thread_info *info = lfsck_env_info(env);
1584 struct lfsck_request *lr = &info->lti_lr;
1585 struct lfsck_assistant_data *lad = com->lc_data;
1586 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1587 struct ptlrpc_thread *athread = &lad->lad_thread;
1588 struct lfsck_assistant_operations *lao = lad->lad_ops;
1589 struct lfsck_assistant_req *lar;
1590 struct l_wait_info lwi = { 0 };
1596 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread start\n",
1597 lfsck_lfsck2name(lfsck), lad->lad_name);
1599 memset(lr, 0, sizeof(*lr));
1600 lr->lr_event = LE_START;
1601 if (pos->lp_oit_cookie <= 1)
1602 lr->lr_param = LPF_RESET;
1603 rc = lfsck_assistant_notify_others(env, com, lr);
1605 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to notify others "
1606 "to start %s: rc = %d\n",
1607 lfsck_lfsck2name(lfsck), lad->lad_name, rc);
1611 spin_lock(&lad->lad_lock);
1612 thread_set_flags(athread, SVC_RUNNING);
1613 spin_unlock(&lad->lad_lock);
1614 wake_up_all(&mthread->t_ctl_waitq);
1617 while (!list_empty(&lad->lad_req_list)) {
1618 bool wakeup = false;
1620 if (unlikely(lad->lad_exit ||
1621 !thread_is_running(mthread)))
1622 GOTO(cleanup1, rc = lad->lad_post_result);
1624 lar = list_entry(lad->lad_req_list.next,
1625 struct lfsck_assistant_req,
1627 /* Only the lfsck_assistant_engine thread itself can
1628 * remove the "lar" from the head of the list, LFSCK
1629 * engine thread only inserts other new "lar" at the
1630 * end of the list. So it is safe to handle current
1631 * "lar" without the spin_lock. */
1632 rc = lao->la_handler_p1(env, com, lar);
1633 spin_lock(&lad->lad_lock);
1634 list_del_init(&lar->lar_list);
1635 lad->lad_prefetched--;
1636 /* Wake up the main engine thread only when the list
1637 * is empty or half of the prefetched items have been
1638 * handled to avoid too frequent thread schedule. */
1639 if (lad->lad_prefetched <= (bk->lb_async_windows / 2))
1641 spin_unlock(&lad->lad_lock);
1643 wake_up_all(&mthread->t_ctl_waitq);
1645 lao->la_req_fini(env, lar);
1646 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1650 l_wait_event(athread->t_ctl_waitq,
1651 !lfsck_assistant_req_empty(lad) ||
1654 lad->lad_to_double_scan,
1657 if (unlikely(lad->lad_exit))
1658 GOTO(cleanup1, rc = lad->lad_post_result);
1660 if (!list_empty(&lad->lad_req_list))
1663 if (lad->lad_to_post) {
1664 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread post\n",
1665 lfsck_lfsck2name(lfsck), lad->lad_name);
1667 if (unlikely(lad->lad_exit))
1668 GOTO(cleanup1, rc = lad->lad_post_result);
1670 lad->lad_to_post = 0;
1671 LASSERT(lad->lad_post_result > 0);
1673 memset(lr, 0, sizeof(*lr));
1674 lr->lr_event = LE_PHASE1_DONE;
1675 lr->lr_status = lad->lad_post_result;
1676 rc = lfsck_assistant_notify_others(env, com, lr);
1678 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to "
1679 "notify others for %s post: rc = %d\n",
1680 lfsck_lfsck2name(lfsck),
1683 /* Wakeup the master engine to go ahead. */
1684 wake_up_all(&mthread->t_ctl_waitq);
1687 if (lad->lad_to_double_scan) {
1688 lad->lad_to_double_scan = 0;
1689 atomic_inc(&lfsck->li_double_scan_count);
1690 lad->lad_in_double_scan = 1;
1691 wake_up_all(&mthread->t_ctl_waitq);
1693 com->lc_new_checked = 0;
1694 com->lc_new_scanned = 0;
1695 com->lc_time_last_checkpoint = cfs_time_current();
1696 com->lc_time_next_checkpoint =
1697 com->lc_time_last_checkpoint +
1698 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1700 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before "
1701 "the second-stage scaning\n",
1702 lfsck_lfsck2name(lfsck));
1704 /* Flush async updates before handling orphan. */
1705 rc2 = dt_sync(env, lfsck->li_next);
1707 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 "
1708 "scan start, synced: rc = %d\n",
1709 lfsck_lfsck2name(lfsck), rc2);
1711 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1712 GOTO(cleanup2, rc = 0);
1714 while (lad->lad_in_double_scan) {
1715 rc = lfsck_assistant_query_others(env, com);
1716 if (lfsck_phase2_next_ready(lad))
1722 /* Pull LFSCK status on related targets once
1723 * per 30 seconds if we are not notified. */
1724 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
1725 cfs_time_seconds(1),
1727 rc = l_wait_event(athread->t_ctl_waitq,
1728 lfsck_phase2_next_ready(lad) ||
1730 !thread_is_running(mthread),
1733 if (unlikely(lad->lad_exit ||
1734 !thread_is_running(mthread)))
1735 GOTO(cleanup2, rc = 0);
1737 if (rc == -ETIMEDOUT)
1744 rc = lao->la_handler_p2(env, com);
1748 if (unlikely(lad->lad_exit ||
1749 !thread_is_running(mthread)))
1750 GOTO(cleanup2, rc = 0);
1756 /* Cleanup the unfinished requests. */
1757 spin_lock(&lad->lad_lock);
1759 lad->lad_assistant_status = rc;
1761 if (lad->lad_exit && lad->lad_post_result <= 0)
1762 lao->la_fill_pos(env, com, &lfsck->li_pos_checkpoint);
1764 while (!list_empty(&lad->lad_req_list)) {
1765 lar = list_entry(lad->lad_req_list.next,
1766 struct lfsck_assistant_req,
1768 list_del_init(&lar->lar_list);
1769 lad->lad_prefetched--;
1770 spin_unlock(&lad->lad_lock);
1771 lao->la_req_fini(env, lar);
1772 spin_lock(&lad->lad_lock);
1774 spin_unlock(&lad->lad_lock);
1776 LASSERTF(lad->lad_prefetched == 0, "unmatched prefeteched objs %d\n",
1777 lad->lad_prefetched);
1780 memset(lr, 0, sizeof(*lr));
1782 lr->lr_event = LE_PHASE2_DONE;
1784 } else if (rc == 0) {
1785 if (lfsck->li_flags & LPF_ALL_TGT) {
1786 lr->lr_event = LE_STOP;
1787 lr->lr_status = LS_STOPPED;
1789 lr->lr_event = LE_PEER_EXIT;
1790 switch (lfsck->li_status) {
1793 lr->lr_status = LS_CO_PAUSED;
1797 lr->lr_status = LS_CO_STOPPED;
1800 CDEBUG(D_LFSCK, "%s: LFSCK assistant unknown "
1801 "status: rc = %d\n",
1802 lfsck_lfsck2name(lfsck),
1804 lr->lr_status = LS_CO_FAILED;
1809 if (lfsck->li_flags & LPF_ALL_TGT) {
1810 lr->lr_event = LE_STOP;
1811 lr->lr_status = LS_FAILED;
1813 lr->lr_event = LE_PEER_EXIT;
1814 lr->lr_status = LS_CO_FAILED;
1818 rc1 = lfsck_assistant_notify_others(env, com, lr);
1820 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to notify "
1821 "others for %s quit: rc = %d\n",
1822 lfsck_lfsck2name(lfsck), lad->lad_name, rc1);
1826 CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before exit\n",
1827 lfsck_lfsck2name(lfsck));
1829 /* Flush async updates before exit. */
1830 rc2 = dt_sync(env, lfsck->li_next);
1832 CDEBUG(D_LFSCK, "%s: LFSCK assistant synced before exit: rc = %d\n",
1833 lfsck_lfsck2name(lfsck), rc2);
1835 /* Under force exit case, some requests may be just freed without
1836 * verification, those objects should be re-handled when next run.
1837 * So not update the on-disk trace file under such case. */
1838 if (lad->lad_in_double_scan) {
1840 rc1 = lao->la_double_scan_result(env, com, rc);
1842 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 scan "
1843 "finished: rc = %d\n",
1844 lfsck_lfsck2name(lfsck), rc1 != 0 ? rc1 : rc);
1848 if (lad->lad_in_double_scan)
1849 atomic_dec(&lfsck->li_double_scan_count);
1851 spin_lock(&lad->lad_lock);
1852 lad->lad_assistant_status = (rc1 != 0 ? rc1 : rc);
1853 thread_set_flags(athread, SVC_STOPPED);
1854 wake_up_all(&mthread->t_ctl_waitq);
1855 spin_unlock(&lad->lad_lock);
1857 CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread exit: rc = %d\n",
1858 lfsck_lfsck2name(lfsck), lad->lad_name,
1859 lad->lad_assistant_status);
1861 lfsck_thread_args_fini(lta);