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, Intel Corporation.
26 * lustre/lfsck/lfsck_layout.c
28 * Author: Fan, Yong <fan.yong@intel.com>
32 # define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_LFSCK
36 #include <linux/bitops.h>
38 #include <lustre/lustre_idl.h>
39 #include <lu_object.h>
40 #include <dt_object.h>
41 #include <lustre_linkea.h>
42 #include <lustre_fid.h>
43 #include <lustre_lib.h>
44 #include <lustre_net.h>
45 #include <lustre/lustre_user.h>
46 #include <md_object.h>
47 #include <obd_class.h>
49 #include "lfsck_internal.h"
51 #define LFSCK_LAYOUT_MAGIC 0xB173AE14
53 static const char lfsck_layout_name[] = "lfsck_layout";
55 struct lfsck_layout_seq {
56 struct list_head lls_list;
59 __u64 lls_lastid_known;
60 struct dt_object *lls_lastid_obj;
61 unsigned int lls_dirty:1;
64 struct lfsck_layout_slave_data {
65 /* list for lfsck_layout_seq */
66 struct list_head llsd_seq_list;
69 struct lfsck_layout_object {
70 struct dt_object *llo_obj;
71 struct lu_attr llo_attr;
76 struct lfsck_layout_req {
77 struct list_head llr_list;
78 struct lfsck_layout_object *llr_parent;
79 struct dt_object *llr_child;
81 __u32 llr_lov_idx; /* offset in LOV EA */
84 struct lfsck_layout_master_data {
85 struct list_head llmd_req_list;
87 struct ptlrpc_thread llmd_thread;
88 atomic_t llmd_rpcs_in_flight;
90 int llmd_assistant_status;
92 unsigned int llmd_to_post:1,
93 llmd_to_double_scan:1,
94 llmd_in_double_scan:1,
98 static inline void lfsck_layout_object_put(const struct lu_env *env,
99 struct lfsck_layout_object *llo)
101 if (atomic_dec_and_test(&llo->llo_ref)) {
102 lfsck_object_put(env, llo->llo_obj);
107 static inline void lfsck_layout_req_fini(const struct lu_env *env,
108 struct lfsck_layout_req *llr)
110 lu_object_put(env, &llr->llr_child->do_lu);
111 lfsck_layout_object_put(env, llr->llr_parent);
115 static inline bool lfsck_layout_req_empty(struct lfsck_layout_master_data *llmd)
119 spin_lock(&llmd->llmd_lock);
120 if (list_empty(&llmd->llmd_req_list))
122 spin_unlock(&llmd->llmd_lock);
127 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
128 const struct lfsck_layout *src)
132 des->ll_magic = le32_to_cpu(src->ll_magic);
133 des->ll_status = le32_to_cpu(src->ll_status);
134 des->ll_flags = le32_to_cpu(src->ll_flags);
135 des->ll_success_count = le32_to_cpu(src->ll_success_count);
136 des->ll_run_time_phase1 = le32_to_cpu(src->ll_run_time_phase1);
137 des->ll_run_time_phase2 = le32_to_cpu(src->ll_run_time_phase2);
138 des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
139 des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
140 des->ll_time_last_checkpoint =
141 le64_to_cpu(src->ll_time_last_checkpoint);
142 des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
143 des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
144 des->ll_pos_first_inconsistent =
145 le64_to_cpu(src->ll_pos_first_inconsistent);
146 des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
147 des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
148 des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
149 des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
150 for (i = 0; i < LLIT_MAX; i++)
151 des->ll_objs_repaired[i] =
152 le64_to_cpu(src->ll_objs_repaired[i]);
153 des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
156 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
157 const struct lfsck_layout *src)
161 des->ll_magic = cpu_to_le32(src->ll_magic);
162 des->ll_status = cpu_to_le32(src->ll_status);
163 des->ll_flags = cpu_to_le32(src->ll_flags);
164 des->ll_success_count = cpu_to_le32(src->ll_success_count);
165 des->ll_run_time_phase1 = cpu_to_le32(src->ll_run_time_phase1);
166 des->ll_run_time_phase2 = cpu_to_le32(src->ll_run_time_phase2);
167 des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
168 des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
169 des->ll_time_last_checkpoint =
170 cpu_to_le64(src->ll_time_last_checkpoint);
171 des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
172 des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
173 des->ll_pos_first_inconsistent =
174 cpu_to_le64(src->ll_pos_first_inconsistent);
175 des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
176 des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
177 des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
178 des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
179 for (i = 0; i < LLIT_MAX; i++)
180 des->ll_objs_repaired[i] =
181 cpu_to_le64(src->ll_objs_repaired[i]);
182 des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
186 * \retval +ve: the lfsck_layout is broken, the caller should reset it.
187 * \retval 0: succeed.
188 * \retval -ve: failed cases.
190 static int lfsck_layout_load(const struct lu_env *env,
191 struct lfsck_component *com)
193 struct lfsck_layout *lo = com->lc_file_ram;
194 const struct dt_body_operations *dbo = com->lc_obj->do_body_ops;
195 ssize_t size = com->lc_file_size;
199 rc = dbo->dbo_read(env, com->lc_obj,
200 lfsck_buf_get(env, com->lc_file_disk, size), &pos,
205 CWARN("%s: failed to load lfsck_layout: rc = %d\n",
206 lfsck_lfsck2name(com->lc_lfsck), rc);
208 } else if (rc != size) {
209 CWARN("%s: crashed lfsck_layout, to be reset: rc = %d\n",
210 lfsck_lfsck2name(com->lc_lfsck), rc);
214 lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
215 if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
216 CWARN("%s: invalid lfsck_layout magic %#x != %#x, "
217 "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
218 lo->ll_magic, LFSCK_LAYOUT_MAGIC);
225 static int lfsck_layout_store(const struct lu_env *env,
226 struct lfsck_component *com)
228 struct dt_object *obj = com->lc_obj;
229 struct lfsck_instance *lfsck = com->lc_lfsck;
230 struct lfsck_layout *lo = com->lc_file_disk;
231 struct thandle *handle;
232 ssize_t size = com->lc_file_size;
237 lfsck_layout_cpu_to_le(lo, com->lc_file_ram);
238 handle = dt_trans_create(env, lfsck->li_bottom);
239 if (IS_ERR(handle)) {
240 rc = PTR_ERR(handle);
241 CERROR("%s: fail to create trans for storing lfsck_layout: "
242 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
246 rc = dt_declare_record_write(env, obj, size, pos, handle);
248 CERROR("%s: fail to declare trans for storing lfsck_layout(1): "
249 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
253 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
255 CERROR("%s: fail to start trans for storing lfsck_layout: "
256 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
260 rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos,
263 CERROR("%s: fail to store lfsck_layout(1): size = %d, "
264 "rc = %d\n", lfsck_lfsck2name(lfsck), (int)size, rc);
269 dt_trans_stop(env, lfsck->li_bottom, handle);
274 static int lfsck_layout_init(const struct lu_env *env,
275 struct lfsck_component *com)
277 struct lfsck_layout *lo = com->lc_file_ram;
280 memset(lo, 0, com->lc_file_size);
281 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
282 lo->ll_status = LS_INIT;
283 down_write(&com->lc_sem);
284 rc = lfsck_layout_store(env, com);
285 up_write(&com->lc_sem);
290 static int fid_is_for_ostobj(const struct lu_env *env, struct dt_device *dt,
291 struct dt_object *obj, const struct lu_fid *fid)
293 struct seq_server_site *ss = lu_site2seq(dt->dd_lu_dev.ld_site);
294 struct lu_seq_range range = { 0 };
295 struct lustre_mdt_attrs *lma;
298 fld_range_set_any(&range);
299 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), &range);
301 if (fld_range_is_ost(&range))
307 lma = &lfsck_env_info(env)->lti_lma;
308 rc = dt_xattr_get(env, obj, lfsck_buf_get(env, lma, sizeof(*lma)),
309 XATTR_NAME_LMA, BYPASS_CAPA);
310 if (rc == sizeof(*lma)) {
311 lustre_lma_swab(lma);
313 /* Generally, the low layer OSD create handler or OI scrub
314 * will set the LMAC_FID_ON_OST for all external visible
315 * OST-objects. But to make the otable-based iteration to
316 * be independent from OI scrub in spite of it got failure
317 * or not, we check the LMAC_FID_ON_OST here to guarantee
318 * that the LFSCK will not repair something by wrong. */
319 return lma->lma_compat & LMAC_FID_ON_OST ? 1 : 0;
322 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID, BYPASS_CAPA);
327 static struct lfsck_layout_seq *
328 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
330 struct lfsck_layout_seq *lls;
332 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
333 if (lls->lls_seq == seq)
336 if (lls->lls_seq > seq)
344 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
345 struct lfsck_layout_seq *lls)
347 struct lfsck_layout_seq *tmp;
348 struct list_head *pos = &llsd->llsd_seq_list;
350 list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
351 if (lls->lls_seq < tmp->lls_seq) {
352 pos = &tmp->lls_list;
356 list_add_tail(&lls->lls_list, pos);
360 lfsck_layout_lastid_create(const struct lu_env *env,
361 struct lfsck_instance *lfsck,
362 struct dt_object *obj)
364 struct lfsck_thread_info *info = lfsck_env_info(env);
365 struct lu_attr *la = &info->lti_la;
366 struct dt_object_format *dof = &info->lti_dof;
367 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
368 struct dt_device *dt = lfsck->li_bottom;
375 CDEBUG(D_LFSCK, "To create LAST_ID for <seq> "LPX64"\n",
376 fid_seq(lfsck_dto2fid(obj)));
378 if (bk->lb_param & LPF_DRYRUN)
381 memset(la, 0, sizeof(*la));
382 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
383 la->la_valid = LA_MODE | LA_UID | LA_GID;
384 dof->dof_type = dt_mode_to_dft(S_IFREG);
386 th = dt_trans_create(env, dt);
388 RETURN(rc = PTR_ERR(th));
390 rc = dt_declare_create(env, obj, la, NULL, dof, th);
394 rc = dt_declare_record_write(env, obj, sizeof(lastid), pos, th);
398 rc = dt_trans_start_local(env, dt, th);
402 dt_write_lock(env, obj, 0);
403 if (likely(!dt_object_exists(obj))) {
404 rc = dt_create(env, obj, la, NULL, dof, th);
406 rc = dt_record_write(env, obj,
407 lfsck_buf_get(env, &lastid, sizeof(lastid)),
410 dt_write_unlock(env, obj);
415 dt_trans_stop(env, dt, th);
421 lfsck_layout_lastid_reload(const struct lu_env *env,
422 struct lfsck_component *com,
423 struct lfsck_layout_seq *lls)
429 dt_read_lock(env, lls->lls_lastid_obj, 0);
430 rc = dt_record_read(env, lls->lls_lastid_obj,
431 lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
432 dt_read_unlock(env, lls->lls_lastid_obj);
433 if (unlikely(rc != 0))
436 lastid = le64_to_cpu(lastid);
437 if (lastid < lls->lls_lastid_known) {
438 struct lfsck_instance *lfsck = com->lc_lfsck;
439 struct lfsck_layout *lo = com->lc_file_ram;
441 lls->lls_lastid = lls->lls_lastid_known;
443 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
444 LASSERT(lfsck->li_out_notify != NULL);
446 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
447 LE_LASTID_REBUILDING);
448 lo->ll_flags |= LF_CRASHED_LASTID;
450 } else if (lastid >= lls->lls_lastid) {
451 lls->lls_lastid = lastid;
459 lfsck_layout_lastid_store(const struct lu_env *env,
460 struct lfsck_component *com)
462 struct lfsck_instance *lfsck = com->lc_lfsck;
463 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
464 struct dt_device *dt = lfsck->li_bottom;
465 struct lfsck_layout_slave_data *llsd = com->lc_data;
466 struct lfsck_layout_seq *lls;
472 list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
475 /* XXX: Add the code back if we really found related
476 * inconsistent cases in the future. */
478 if (!lls->lls_dirty) {
479 /* In OFD, before the pre-creation, the LAST_ID
480 * file will be updated firstly, which may hide
481 * some potential crashed cases. For example:
483 * The old obj1's ID is higher than old LAST_ID
484 * but lower than the new LAST_ID, but the LFSCK
485 * have not touch the obj1 until the OFD updated
486 * the LAST_ID. So the LFSCK does not regard it
487 * as crashed case. But when OFD does not create
488 * successfully, it will set the LAST_ID as the
489 * real created objects' ID, then LFSCK needs to
490 * found related inconsistency. */
491 rc = lfsck_layout_lastid_reload(env, com, lls);
492 if (likely(!lls->lls_dirty))
497 CDEBUG(D_LFSCK, "To sync the LAST_ID for <seq> "LPX64
498 " as <oid> "LPU64"\n", lls->lls_seq, lls->lls_lastid);
500 if (bk->lb_param & LPF_DRYRUN) {
505 th = dt_trans_create(env, dt);
508 CERROR("%s: (1) failed to store "LPX64": rc = %d\n",
509 lfsck_lfsck2name(com->lc_lfsck),
514 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
515 sizeof(lastid), pos, th);
519 rc = dt_trans_start_local(env, dt, th);
523 lastid = cpu_to_le64(lls->lls_lastid);
524 dt_write_lock(env, lls->lls_lastid_obj, 0);
525 rc = dt_record_write(env, lls->lls_lastid_obj,
526 lfsck_buf_get(env, &lastid,
527 sizeof(lastid)), &pos, th);
528 dt_write_unlock(env, lls->lls_lastid_obj);
533 dt_trans_stop(env, dt, th);
536 CERROR("%s: (2) failed to store "LPX64": rc = %d\n",
537 lfsck_lfsck2name(com->lc_lfsck),
546 lfsck_layout_lastid_load(const struct lu_env *env,
547 struct lfsck_component *com,
548 struct lfsck_layout_seq *lls)
550 struct lfsck_instance *lfsck = com->lc_lfsck;
551 struct lfsck_layout *lo = com->lc_file_ram;
552 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
553 struct dt_object *obj;
558 lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck->li_bottom));
559 obj = dt_locate(env, lfsck->li_bottom, fid);
561 RETURN(PTR_ERR(obj));
563 /* LAST_ID crashed, to be rebuilt */
564 if (!dt_object_exists(obj)) {
565 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
566 LASSERT(lfsck->li_out_notify != NULL);
568 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
569 LE_LASTID_REBUILDING);
570 lo->ll_flags |= LF_CRASHED_LASTID;
572 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
574 struct l_wait_info lwi = LWI_TIMEOUT(
575 cfs_time_seconds(cfs_fail_val),
578 up_write(&com->lc_sem);
579 l_wait_event(lfsck->li_thread.t_ctl_waitq,
580 !thread_is_running(&lfsck->li_thread),
582 down_write(&com->lc_sem);
586 rc = lfsck_layout_lastid_create(env, lfsck, obj);
588 dt_read_lock(env, obj, 0);
589 rc = dt_read(env, obj,
590 lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
592 dt_read_unlock(env, obj);
593 if (rc != 0 && rc != sizeof(__u64))
594 GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
596 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
597 LASSERT(lfsck->li_out_notify != NULL);
599 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
600 LE_LASTID_REBUILDING);
601 lo->ll_flags |= LF_CRASHED_LASTID;
604 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
612 lfsck_object_put(env, obj);
614 lls->lls_lastid_obj = obj;
619 static int lfsck_layout_master_query_others(const struct lu_env *env,
620 struct lfsck_component *com)
622 /* XXX: to be implemented. */
628 lfsck_layout_master_to_orphan(struct lfsck_layout_master_data *llmd)
630 /* XXX: to be implemented. */
635 static int lfsck_layout_master_notify_others(const struct lu_env *env,
636 struct lfsck_component *com,
637 struct lfsck_request *lr)
639 /* XXX: to be implemented. */
644 static int lfsck_layout_double_scan_result(const struct lu_env *env,
645 struct lfsck_component *com,
648 struct lfsck_instance *lfsck = com->lc_lfsck;
649 struct lfsck_layout *lo = com->lc_file_ram;
650 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
652 down_write(&com->lc_sem);
654 lo->ll_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
655 HALF_SEC - lfsck->li_time_last_checkpoint);
656 lo->ll_time_last_checkpoint = cfs_time_current_sec();
657 lo->ll_objs_checked_phase2 += com->lc_new_checked;
661 if (lo->ll_flags & LF_INCOMPLETE)
662 lo->ll_status = LS_PARTIAL;
664 lo->ll_status = LS_COMPLETED;
665 if (!(bk->lb_param & LPF_DRYRUN))
666 lo->ll_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
667 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
668 lo->ll_success_count++;
669 } else if (rc == 0) {
670 lo->ll_status = lfsck->li_status;
671 if (lo->ll_status == 0)
672 lo->ll_status = LS_STOPPED;
674 lo->ll_status = LS_FAILED;
677 if (lo->ll_status != LS_PAUSED) {
678 spin_lock(&lfsck->li_lock);
679 list_del_init(&com->lc_link);
680 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
681 spin_unlock(&lfsck->li_lock);
684 rc = lfsck_layout_store(env, com);
686 up_write(&com->lc_sem);
691 static int lfsck_layout_assistant(void *args)
693 struct lfsck_thread_args *lta = args;
694 struct lu_env *env = <a->lta_env;
695 struct lfsck_component *com = lta->lta_com;
696 struct lfsck_instance *lfsck = lta->lta_lfsck;
697 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
698 struct lfsck_position *pos = &com->lc_pos_start;
699 struct lfsck_thread_info *info = lfsck_env_info(env);
700 struct lfsck_request *lr = &info->lti_lr;
701 struct lfsck_layout_master_data *llmd = com->lc_data;
702 struct ptlrpc_thread *mthread = &lfsck->li_thread;
703 struct ptlrpc_thread *athread = &llmd->llmd_thread;
704 struct lfsck_layout_req *llr;
705 struct l_wait_info lwi = { 0 };
710 memset(lr, 0, sizeof(*lr));
711 lr->lr_event = LE_START;
712 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
713 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
715 lr->lr_speed = bk->lb_speed_limit;
716 lr->lr_version = bk->lb_version;
717 lr->lr_active = LT_LAYOUT;
718 lr->lr_param = bk->lb_param;
719 lr->lr_async_windows = bk->lb_async_windows;
720 if (pos->lp_oit_cookie <= 1)
721 lr->lr_param |= LPF_RESET;
723 rc = lfsck_layout_master_notify_others(env, com, lr);
725 CERROR("%s: fail to notify others for layout start: rc = %d\n",
726 lfsck_lfsck2name(lfsck), rc);
730 spin_lock(&llmd->llmd_lock);
731 thread_set_flags(athread, SVC_RUNNING);
732 spin_unlock(&llmd->llmd_lock);
733 wake_up_all(&mthread->t_ctl_waitq);
736 while (!list_empty(&llmd->llmd_req_list)) {
739 l_wait_event(athread->t_ctl_waitq,
740 bk->lb_async_windows == 0 ||
741 atomic_read(&llmd->llmd_rpcs_in_flight) <
742 bk->lb_async_windows ||
746 if (unlikely(llmd->llmd_exit))
747 GOTO(cleanup1, rc = llmd->llmd_post_result);
749 /* XXX: To be extended in other patch.
751 * Compare the OST side attribute with local attribute,
752 * and fix it if found inconsistency. */
754 spin_lock(&llmd->llmd_lock);
755 llr = list_entry(llmd->llmd_req_list.next,
756 struct lfsck_layout_req,
758 list_del_init(&llr->llr_list);
759 if (bk->lb_async_windows != 0 &&
760 llmd->llmd_prefetched >= bk->lb_async_windows)
763 llmd->llmd_prefetched--;
764 spin_unlock(&llmd->llmd_lock);
766 wake_up_all(&mthread->t_ctl_waitq);
768 lfsck_layout_req_fini(env, llr);
771 /* Wakeup the master engine if it is waiting in checkpoint. */
772 if (atomic_read(&llmd->llmd_rpcs_in_flight) == 0)
773 wake_up_all(&mthread->t_ctl_waitq);
775 l_wait_event(athread->t_ctl_waitq,
776 !lfsck_layout_req_empty(llmd) ||
778 llmd->llmd_to_post ||
779 llmd->llmd_to_double_scan,
782 if (unlikely(llmd->llmd_exit))
783 GOTO(cleanup1, rc = llmd->llmd_post_result);
785 if (!list_empty(&llmd->llmd_req_list))
788 if (llmd->llmd_to_post) {
789 llmd->llmd_to_post = 0;
790 LASSERT(llmd->llmd_post_result > 0);
792 memset(lr, 0, sizeof(*lr));
793 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
794 lr->lr_active = LT_LAYOUT;
795 lr->lr_event = LE_PHASE1_DONE;
796 lr->lr_status = llmd->llmd_post_result;
797 rc = lfsck_layout_master_notify_others(env, com, lr);
799 CERROR("%s: failed to notify others "
800 "for layout post: rc = %d\n",
801 lfsck_lfsck2name(lfsck), rc);
803 /* Wakeup the master engine to go ahead. */
804 wake_up_all(&mthread->t_ctl_waitq);
807 if (llmd->llmd_to_double_scan) {
808 llmd->llmd_to_double_scan = 0;
809 atomic_inc(&lfsck->li_double_scan_count);
810 llmd->llmd_in_double_scan = 1;
811 wake_up_all(&mthread->t_ctl_waitq);
813 while (llmd->llmd_in_double_scan) {
814 rc = lfsck_layout_master_query_others(env, com);
815 if (lfsck_layout_master_to_orphan(llmd))
821 /* Pull LFSCK status on related targets once
822 * per 30 seconds if we are not notified. */
823 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
826 rc = l_wait_event(athread->t_ctl_waitq,
827 lfsck_layout_master_to_orphan(llmd) ||
829 !thread_is_running(mthread),
832 if (unlikely(llmd->llmd_exit ||
833 !thread_is_running(mthread)))
834 GOTO(cleanup2, rc = 0);
836 if (rc == -ETIMEDOUT)
843 /* XXX: real double scan for ost orphans. */
845 GOTO(cleanup2, rc = 1);
851 /* Cleanup the unfinished requests. */
852 spin_lock(&llmd->llmd_lock);
853 while (!list_empty(&llmd->llmd_req_list)) {
854 llr = list_entry(llmd->llmd_req_list.next,
855 struct lfsck_layout_req,
857 list_del_init(&llr->llr_list);
858 llmd->llmd_prefetched--;
859 spin_unlock(&llmd->llmd_lock);
860 lfsck_layout_req_fini(env, llr);
861 spin_lock(&llmd->llmd_lock);
863 spin_unlock(&llmd->llmd_lock);
865 LASSERTF(llmd->llmd_prefetched == 0, "unmatched prefeteched objs %d\n",
866 llmd->llmd_prefetched);
868 l_wait_event(athread->t_ctl_waitq,
869 atomic_read(&llmd->llmd_rpcs_in_flight) == 0,
873 memset(lr, 0, sizeof(*lr));
874 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
875 lr->lr_active = LT_LAYOUT;
877 lr->lr_event = LE_PHASE2_DONE;
879 } else if (rc == 0) {
880 lr->lr_event = LE_STOP;
881 if (lfsck->li_status == LS_PAUSED ||
882 lfsck->li_status == LS_CO_PAUSED)
883 lr->lr_status = LS_CO_PAUSED;
884 else if (lfsck->li_status == LS_STOPPED ||
885 lfsck->li_status == LS_CO_STOPPED)
886 lr->lr_status = LS_CO_STOPPED;
890 lr->lr_event = LE_STOP;
891 lr->lr_status = LS_CO_FAILED;
894 rc1 = lfsck_layout_master_notify_others(env, com, lr);
896 CERROR("%s: failed to notify others for layout quit: rc = %d\n",
897 lfsck_lfsck2name(lfsck), rc1);
901 /* Under force exit case, some requests may be just freed without
902 * verification, those objects should be re-handled when next run.
903 * So not update the on-disk tracing file under such case. */
904 if (!llmd->llmd_exit)
905 rc1 = lfsck_layout_double_scan_result(env, com, rc);
908 if (llmd->llmd_in_double_scan)
909 atomic_dec(&lfsck->li_double_scan_count);
911 spin_lock(&llmd->llmd_lock);
912 llmd->llmd_assistant_status = (rc1 != 0 ? rc1 : rc);
913 thread_set_flags(athread, SVC_STOPPED);
914 wake_up_all(&mthread->t_ctl_waitq);
915 spin_unlock(&llmd->llmd_lock);
916 lfsck_thread_args_fini(lta);
923 static int lfsck_layout_reset(const struct lu_env *env,
924 struct lfsck_component *com, bool init)
926 struct lfsck_layout *lo = com->lc_file_ram;
929 down_write(&com->lc_sem);
931 memset(lo, 0, com->lc_file_size);
933 __u32 count = lo->ll_success_count;
934 __u64 last_time = lo->ll_time_last_complete;
936 memset(lo, 0, com->lc_file_size);
937 lo->ll_success_count = count;
938 lo->ll_time_last_complete = last_time;
941 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
942 lo->ll_status = LS_INIT;
944 rc = lfsck_layout_store(env, com);
945 up_write(&com->lc_sem);
950 static void lfsck_layout_fail(const struct lu_env *env,
951 struct lfsck_component *com, bool new_checked)
953 struct lfsck_layout *lo = com->lc_file_ram;
955 down_write(&com->lc_sem);
957 com->lc_new_checked++;
958 lo->ll_objs_failed_phase1++;
959 if (lo->ll_pos_first_inconsistent == 0) {
960 struct lfsck_instance *lfsck = com->lc_lfsck;
962 lo->ll_pos_first_inconsistent =
963 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
966 up_write(&com->lc_sem);
969 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
970 struct lfsck_component *com, bool init)
972 struct lfsck_instance *lfsck = com->lc_lfsck;
973 struct lfsck_layout *lo = com->lc_file_ram;
974 struct lfsck_layout_master_data *llmd = com->lc_data;
975 struct ptlrpc_thread *mthread = &lfsck->li_thread;
976 struct ptlrpc_thread *athread = &llmd->llmd_thread;
977 struct l_wait_info lwi = { 0 };
980 if (com->lc_new_checked == 0 && !init)
983 l_wait_event(mthread->t_ctl_waitq,
984 (list_empty(&llmd->llmd_req_list) &&
985 atomic_read(&llmd->llmd_rpcs_in_flight) == 0) ||
986 !thread_is_running(mthread) ||
987 thread_is_stopped(athread),
990 if (!thread_is_running(mthread) || thread_is_stopped(athread))
993 down_write(&com->lc_sem);
995 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
997 lo->ll_pos_last_checkpoint =
998 lfsck->li_pos_current.lp_oit_cookie;
999 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1000 HALF_SEC - lfsck->li_time_last_checkpoint);
1001 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1002 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1003 com->lc_new_checked = 0;
1006 rc = lfsck_layout_store(env, com);
1007 up_write(&com->lc_sem);
1012 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
1013 struct lfsck_component *com, bool init)
1015 struct lfsck_instance *lfsck = com->lc_lfsck;
1016 struct lfsck_layout *lo = com->lc_file_ram;
1019 if (com->lc_new_checked == 0 && !init)
1022 down_write(&com->lc_sem);
1025 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
1027 lo->ll_pos_last_checkpoint =
1028 lfsck->li_pos_current.lp_oit_cookie;
1029 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1030 HALF_SEC - lfsck->li_time_last_checkpoint);
1031 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1032 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1033 com->lc_new_checked = 0;
1036 rc = lfsck_layout_store(env, com);
1038 up_write(&com->lc_sem);
1043 static int lfsck_layout_slave_prep(const struct lu_env *env,
1044 struct lfsck_component *com)
1046 struct lfsck_instance *lfsck = com->lc_lfsck;
1047 struct lfsck_layout *lo = com->lc_file_ram;
1048 struct lfsck_position *pos = &com->lc_pos_start;
1050 /* XXX: For a new scanning, generate OST-objects
1051 * bitmap for orphan detection. */
1053 fid_zero(&pos->lp_dir_parent);
1054 pos->lp_dir_cookie = 0;
1055 if (lo->ll_status == LS_COMPLETED ||
1056 lo->ll_status == LS_PARTIAL) {
1059 rc = lfsck_layout_reset(env, com, false);
1064 down_write(&com->lc_sem);
1066 lo->ll_time_latest_start = cfs_time_current_sec();
1068 spin_lock(&lfsck->li_lock);
1069 if (lo->ll_flags & LF_SCANNED_ONCE) {
1070 if (!lfsck->li_drop_dryrun ||
1071 lo->ll_pos_first_inconsistent == 0) {
1072 lo->ll_status = LS_SCANNING_PHASE2;
1073 list_del_init(&com->lc_link);
1074 list_add_tail(&com->lc_link,
1075 &lfsck->li_list_double_scan);
1076 pos->lp_oit_cookie = 0;
1080 lo->ll_status = LS_SCANNING_PHASE1;
1081 lo->ll_run_time_phase1 = 0;
1082 lo->ll_run_time_phase2 = 0;
1083 lo->ll_objs_checked_phase1 = 0;
1084 lo->ll_objs_checked_phase2 = 0;
1085 lo->ll_objs_failed_phase1 = 0;
1086 lo->ll_objs_failed_phase2 = 0;
1087 for (i = 0; i < LLIT_MAX; i++)
1088 lo->ll_objs_repaired[i] = 0;
1090 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1093 lo->ll_status = LS_SCANNING_PHASE1;
1094 if (!lfsck->li_drop_dryrun ||
1095 lo->ll_pos_first_inconsistent == 0)
1096 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
1098 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1100 spin_unlock(&lfsck->li_lock);
1102 up_write(&com->lc_sem);
1107 static int lfsck_layout_master_prep(const struct lu_env *env,
1108 struct lfsck_component *com)
1110 struct lfsck_instance *lfsck = com->lc_lfsck;
1111 struct lfsck_layout_master_data *llmd = com->lc_data;
1112 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1113 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1114 struct lfsck_thread_args *lta;
1118 rc = lfsck_layout_slave_prep(env, com);
1122 llmd->llmd_assistant_status = 0;
1123 llmd->llmd_post_result = 0;
1124 llmd->llmd_to_post = 0;
1125 llmd->llmd_to_double_scan = 0;
1126 llmd->llmd_in_double_scan = 0;
1127 llmd->llmd_exit = 0;
1128 thread_set_flags(athread, 0);
1130 lta = lfsck_thread_args_init(lfsck, com);
1132 RETURN(PTR_ERR(lta));
1134 rc = PTR_ERR(kthread_run(lfsck_layout_assistant, lta, "lfsck_layout"));
1135 if (IS_ERR_VALUE(rc)) {
1136 CERROR("%s: Cannot start LFSCK layout assistant thread: "
1137 "rc = %ld\n", lfsck_lfsck2name(lfsck), rc);
1138 lfsck_thread_args_fini(lta);
1140 struct l_wait_info lwi = { 0 };
1142 l_wait_event(mthread->t_ctl_waitq,
1143 thread_is_running(athread) ||
1144 thread_is_stopped(athread),
1146 if (unlikely(!thread_is_running(athread)))
1147 rc = llmd->llmd_assistant_status;
1155 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
1156 struct lfsck_component *com,
1157 struct dt_object *obj)
1159 /* XXX: To be implemented in other patches.
1161 * For the given object, read its layout EA locally. For each stripe,
1162 * pre-fetch the OST-object's attribute and generate an structure
1163 * lfsck_layout_req on the list ::llmd_req_list.
1165 * For each request on the ::llmd_req_list, the lfsck_layout_assistant
1166 * thread will compare the OST side attribute with local attribute,
1167 * if inconsistent, then repair it.
1169 * All above processing is async mode with pipeline. */
1174 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
1175 struct lfsck_component *com,
1176 struct dt_object *obj)
1178 struct lfsck_instance *lfsck = com->lc_lfsck;
1179 struct lfsck_layout *lo = com->lc_file_ram;
1180 const struct lu_fid *fid = lfsck_dto2fid(obj);
1181 struct lfsck_layout_slave_data *llsd = com->lc_data;
1182 struct lfsck_layout_seq *lls;
1188 /* XXX: Update OST-objects bitmap for orphan detection. */
1190 LASSERT(llsd != NULL);
1192 down_write(&com->lc_sem);
1193 if (fid_is_idif(fid))
1195 else if (!fid_is_norm(fid) ||
1196 !fid_is_for_ostobj(env, lfsck->li_next, obj, fid))
1197 GOTO(unlock, rc = 0);
1200 com->lc_new_checked++;
1202 lls = lfsck_layout_seq_lookup(llsd, seq);
1205 if (unlikely(lls == NULL))
1206 GOTO(unlock, rc = -ENOMEM);
1208 INIT_LIST_HEAD(&lls->lls_list);
1210 rc = lfsck_layout_lastid_load(env, com, lls);
1212 lo->ll_objs_failed_phase1++;
1217 lfsck_layout_seq_insert(llsd, lls);
1220 if (unlikely(fid_is_last_id(fid)))
1221 GOTO(unlock, rc = 0);
1224 if (oid > lls->lls_lastid_known)
1225 lls->lls_lastid_known = oid;
1227 if (oid > lls->lls_lastid) {
1228 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1229 /* OFD may create new objects during LFSCK scanning. */
1230 rc = lfsck_layout_lastid_reload(env, com, lls);
1231 if (unlikely(rc != 0))
1232 CWARN("%s: failed to reload LAST_ID for "LPX64
1234 lfsck_lfsck2name(com->lc_lfsck),
1236 if (oid <= lls->lls_lastid)
1237 GOTO(unlock, rc = 0);
1239 LASSERT(lfsck->li_out_notify != NULL);
1241 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1242 LE_LASTID_REBUILDING);
1243 lo->ll_flags |= LF_CRASHED_LASTID;
1246 lls->lls_lastid = oid;
1250 GOTO(unlock, rc = 0);
1253 up_write(&com->lc_sem);
1258 static int lfsck_layout_exec_dir(const struct lu_env *env,
1259 struct lfsck_component *com,
1260 struct dt_object *obj,
1261 struct lu_dirent *ent)
1266 static int lfsck_layout_master_post(const struct lu_env *env,
1267 struct lfsck_component *com,
1268 int result, bool init)
1270 struct lfsck_instance *lfsck = com->lc_lfsck;
1271 struct lfsck_layout *lo = com->lc_file_ram;
1272 struct lfsck_layout_master_data *llmd = com->lc_data;
1273 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1274 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1275 struct l_wait_info lwi = { 0 };
1280 llmd->llmd_post_result = result;
1281 llmd->llmd_to_post = 1;
1282 if (llmd->llmd_post_result <= 0)
1283 llmd->llmd_exit = 1;
1285 wake_up_all(&athread->t_ctl_waitq);
1286 l_wait_event(mthread->t_ctl_waitq,
1287 (result > 0 && list_empty(&llmd->llmd_req_list) &&
1288 atomic_read(&llmd->llmd_rpcs_in_flight) == 0) ||
1289 thread_is_stopped(athread),
1292 if (llmd->llmd_assistant_status < 0)
1293 result = llmd->llmd_assistant_status;
1295 down_write(&com->lc_sem);
1296 spin_lock(&lfsck->li_lock);
1297 /* When LFSCK failed, there may be some prefetched objects those are
1298 * not been processed yet, we do not know the exactly position, then
1299 * just restart from last check-point next time. */
1300 if (!init && !llmd->llmd_exit)
1301 lo->ll_pos_last_checkpoint =
1302 lfsck->li_pos_current.lp_oit_cookie;
1305 lo->ll_status = LS_SCANNING_PHASE2;
1306 lo->ll_flags |= LF_SCANNED_ONCE;
1307 lo->ll_flags &= ~LF_UPGRADE;
1308 list_del_init(&com->lc_link);
1309 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1310 } else if (result == 0) {
1311 lo->ll_status = lfsck->li_status;
1312 if (lo->ll_status == 0)
1313 lo->ll_status = LS_STOPPED;
1314 if (lo->ll_status != LS_PAUSED) {
1315 list_del_init(&com->lc_link);
1316 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1319 lo->ll_status = LS_FAILED;
1320 list_del_init(&com->lc_link);
1321 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1323 spin_unlock(&lfsck->li_lock);
1326 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1327 HALF_SEC - lfsck->li_time_last_checkpoint);
1328 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1329 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1330 com->lc_new_checked = 0;
1333 rc = lfsck_layout_store(env, com);
1334 up_write(&com->lc_sem);
1339 static int lfsck_layout_slave_post(const struct lu_env *env,
1340 struct lfsck_component *com,
1341 int result, bool init)
1343 struct lfsck_instance *lfsck = com->lc_lfsck;
1344 struct lfsck_layout *lo = com->lc_file_ram;
1348 rc = lfsck_layout_lastid_store(env, com);
1352 LASSERT(lfsck->li_out_notify != NULL);
1354 down_write(&com->lc_sem);
1356 spin_lock(&lfsck->li_lock);
1358 lo->ll_pos_last_checkpoint =
1359 lfsck->li_pos_current.lp_oit_cookie;
1361 lo->ll_status = LS_SCANNING_PHASE2;
1362 lo->ll_flags |= LF_SCANNED_ONCE;
1363 if (lo->ll_flags & LF_CRASHED_LASTID) {
1365 lo->ll_flags &= ~LF_CRASHED_LASTID;
1367 lo->ll_flags &= ~LF_UPGRADE;
1368 list_del_init(&com->lc_link);
1369 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1370 } else if (result == 0) {
1371 lo->ll_status = lfsck->li_status;
1372 if (lo->ll_status == 0)
1373 lo->ll_status = LS_STOPPED;
1374 if (lo->ll_status != LS_PAUSED) {
1375 list_del_init(&com->lc_link);
1376 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1379 lo->ll_status = LS_FAILED;
1380 list_del_init(&com->lc_link);
1381 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1383 spin_unlock(&lfsck->li_lock);
1386 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1390 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1391 HALF_SEC - lfsck->li_time_last_checkpoint);
1392 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1393 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1394 com->lc_new_checked = 0;
1397 rc = lfsck_layout_store(env, com);
1399 up_write(&com->lc_sem);
1404 static int lfsck_layout_dump(const struct lu_env *env,
1405 struct lfsck_component *com, char *buf, int len)
1407 struct lfsck_instance *lfsck = com->lc_lfsck;
1408 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1409 struct lfsck_layout *lo = com->lc_file_ram;
1414 down_read(&com->lc_sem);
1415 rc = snprintf(buf, len,
1416 "name: lfsck_layout\n"
1422 lfsck_status2names(lo->ll_status));
1428 rc = lfsck_bits_dump(&buf, &len, lo->ll_flags, lfsck_flags_names,
1433 rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1438 rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_complete,
1439 "time_since_last_completed");
1443 rc = lfsck_time_dump(&buf, &len, lo->ll_time_latest_start,
1444 "time_since_latest_start");
1448 rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_checkpoint,
1449 "time_since_last_checkpoint");
1453 rc = snprintf(buf, len,
1454 "latest_start_position: "LPU64"\n"
1455 "last_checkpoint_position: "LPU64"\n"
1456 "first_failure_position: "LPU64"\n",
1457 lo->ll_pos_latest_start,
1458 lo->ll_pos_last_checkpoint,
1459 lo->ll_pos_first_inconsistent);
1466 rc = snprintf(buf, len,
1467 "success_count: %u\n"
1468 "repaired_dangling: "LPU64"\n"
1469 "repaired_unmatched_pair: "LPU64"\n"
1470 "repaired_multiple_referenced: "LPU64"\n"
1471 "repaired_orphan: "LPU64"\n"
1472 "repaired_inconsistent_owner: "LPU64"\n"
1473 "repaired_others: "LPU64"\n"
1474 "skipped: "LPU64"\n"
1475 "failed_phase1: "LPU64"\n"
1476 "failed_phase2: "LPU64"\n",
1477 lo->ll_success_count,
1478 lo->ll_objs_repaired[LLIT_DANGLING - 1],
1479 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
1480 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
1481 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
1482 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
1483 lo->ll_objs_repaired[LLIT_OTHERS - 1],
1484 lo->ll_objs_skipped,
1485 lo->ll_objs_failed_phase1,
1486 lo->ll_objs_failed_phase2);
1493 if (lo->ll_status == LS_SCANNING_PHASE1) {
1495 const struct dt_it_ops *iops;
1496 cfs_duration_t duration = cfs_time_current() -
1497 lfsck->li_time_last_checkpoint;
1498 __u64 checked = lo->ll_objs_checked_phase1 + com->lc_new_checked;
1499 __u64 speed = checked;
1500 __u64 new_checked = com->lc_new_checked * HZ;
1501 __u32 rtime = lo->ll_run_time_phase1 +
1502 cfs_duration_sec(duration + HALF_SEC);
1505 do_div(new_checked, duration);
1507 do_div(speed, rtime);
1508 rc = snprintf(buf, len,
1509 "checked_phase1: "LPU64"\n"
1510 "checked_phase2: "LPU64"\n"
1511 "run_time_phase1: %u seconds\n"
1512 "run_time_phase2: %u seconds\n"
1513 "average_speed_phase1: "LPU64" items/sec\n"
1514 "average_speed_phase2: N/A\n"
1515 "real-time_speed_phase1: "LPU64" items/sec\n"
1516 "real-time_speed_phase2: N/A\n",
1518 lo->ll_objs_checked_phase2,
1520 lo->ll_run_time_phase2,
1529 LASSERT(lfsck->li_di_oit != NULL);
1531 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1533 /* The low layer otable-based iteration position may NOT
1534 * exactly match the layout-based directory traversal
1535 * cookie. Generally, it is not a serious issue. But the
1536 * caller should NOT make assumption on that. */
1537 pos = iops->store(env, lfsck->li_di_oit);
1538 if (!lfsck->li_current_oit_processed)
1540 rc = snprintf(buf, len, "current_position: "LPU64"\n", pos);
1547 /* XXX: LS_SCANNING_PHASE2 will be handled in the future. */
1548 __u64 speed1 = lo->ll_objs_checked_phase1;
1549 __u64 speed2 = lo->ll_objs_checked_phase2;
1551 if (lo->ll_run_time_phase1 != 0)
1552 do_div(speed1, lo->ll_run_time_phase1);
1553 if (lo->ll_run_time_phase2 != 0)
1554 do_div(speed2, lo->ll_run_time_phase2);
1555 rc = snprintf(buf, len,
1556 "checked_phase1: "LPU64"\n"
1557 "checked_phase2: "LPU64"\n"
1558 "run_time_phase1: %u seconds\n"
1559 "run_time_phase2: %u seconds\n"
1560 "average_speed_phase1: "LPU64" items/sec\n"
1561 "average_speed_phase2: "LPU64" objs/sec\n"
1562 "real-time_speed_phase1: N/A\n"
1563 "real-time_speed_phase2: N/A\n"
1564 "current_position: N/A\n",
1565 lo->ll_objs_checked_phase1,
1566 lo->ll_objs_checked_phase2,
1567 lo->ll_run_time_phase1,
1568 lo->ll_run_time_phase2,
1580 up_read(&com->lc_sem);
1585 static int lfsck_layout_master_double_scan(const struct lu_env *env,
1586 struct lfsck_component *com)
1588 struct lfsck_layout_master_data *llmd = com->lc_data;
1589 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
1590 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1591 struct lfsck_layout *lo = com->lc_file_ram;
1592 struct l_wait_info lwi = { 0 };
1594 if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1597 llmd->llmd_to_double_scan = 1;
1598 wake_up_all(&athread->t_ctl_waitq);
1599 l_wait_event(mthread->t_ctl_waitq,
1600 llmd->llmd_in_double_scan ||
1601 thread_is_stopped(athread),
1603 if (llmd->llmd_assistant_status < 0)
1604 return llmd->llmd_assistant_status;
1609 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
1610 struct lfsck_component *com)
1612 struct lfsck_instance *lfsck = com->lc_lfsck;
1613 struct lfsck_layout *lo = com->lc_file_ram;
1616 if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1619 atomic_inc(&lfsck->li_double_scan_count);
1621 com->lc_new_checked = 0;
1622 com->lc_new_scanned = 0;
1623 com->lc_time_last_checkpoint = cfs_time_current();
1624 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
1625 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1627 rc = lfsck_layout_double_scan_result(env, com, rc);
1629 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
1630 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
1635 static void lfsck_layout_master_data_release(const struct lu_env *env,
1636 struct lfsck_component *com)
1638 struct lfsck_layout_master_data *llmd = com->lc_data;
1640 LASSERT(llmd != NULL);
1641 LASSERT(thread_is_init(&llmd->llmd_thread) ||
1642 thread_is_stopped(&llmd->llmd_thread));
1643 LASSERT(list_empty(&llmd->llmd_req_list));
1644 LASSERT(atomic_read(&llmd->llmd_rpcs_in_flight) == 0);
1646 com->lc_data = NULL;
1650 static void lfsck_layout_slave_data_release(const struct lu_env *env,
1651 struct lfsck_component *com)
1653 struct lfsck_layout_slave_data *llsd = com->lc_data;
1654 struct lfsck_layout_seq *lls;
1655 struct lfsck_layout_seq *next;
1657 LASSERT(llsd != NULL);
1659 com->lc_data = NULL;
1661 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
1663 list_del_init(&lls->lls_list);
1664 lfsck_object_put(env, lls->lls_lastid_obj);
1671 static void lfsck_layout_master_quit(const struct lu_env *env,
1672 struct lfsck_component *com)
1674 struct lfsck_layout_master_data *llmd = com->lc_data;
1675 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
1676 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1677 struct l_wait_info lwi = { 0 };
1679 llmd->llmd_exit = 1;
1680 wake_up_all(&athread->t_ctl_waitq);
1681 l_wait_event(mthread->t_ctl_waitq,
1682 thread_is_init(athread) ||
1683 thread_is_stopped(athread),
1687 static int lfsck_layout_master_in_notify(const struct lu_env *env,
1688 struct lfsck_component *com,
1689 struct lfsck_request *lr)
1691 /* XXX: to record the event from layout slave on the OST. */
1695 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
1696 struct lfsck_component *com,
1697 struct lfsck_request *lr)
1699 /* XXX: to record the event from layout master on the MDT. */
1703 static int lfsck_layout_query(const struct lu_env *env,
1704 struct lfsck_component *com)
1706 struct lfsck_layout *lo = com->lc_file_ram;
1708 return lo->ll_status;
1711 static struct lfsck_operations lfsck_layout_master_ops = {
1712 .lfsck_reset = lfsck_layout_reset,
1713 .lfsck_fail = lfsck_layout_fail,
1714 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
1715 .lfsck_prep = lfsck_layout_master_prep,
1716 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
1717 .lfsck_exec_dir = lfsck_layout_exec_dir,
1718 .lfsck_post = lfsck_layout_master_post,
1719 .lfsck_dump = lfsck_layout_dump,
1720 .lfsck_double_scan = lfsck_layout_master_double_scan,
1721 .lfsck_data_release = lfsck_layout_master_data_release,
1722 .lfsck_quit = lfsck_layout_master_quit,
1723 .lfsck_in_notify = lfsck_layout_master_in_notify,
1724 .lfsck_query = lfsck_layout_query,
1727 static struct lfsck_operations lfsck_layout_slave_ops = {
1728 .lfsck_reset = lfsck_layout_reset,
1729 .lfsck_fail = lfsck_layout_fail,
1730 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
1731 .lfsck_prep = lfsck_layout_slave_prep,
1732 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
1733 .lfsck_exec_dir = lfsck_layout_exec_dir,
1734 .lfsck_post = lfsck_layout_slave_post,
1735 .lfsck_dump = lfsck_layout_dump,
1736 .lfsck_double_scan = lfsck_layout_slave_double_scan,
1737 .lfsck_data_release = lfsck_layout_slave_data_release,
1738 .lfsck_in_notify = lfsck_layout_slave_in_notify,
1739 .lfsck_query = lfsck_layout_query,
1742 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
1744 struct lfsck_component *com;
1745 struct lfsck_layout *lo;
1746 struct dt_object *root = NULL;
1747 struct dt_object *obj;
1755 INIT_LIST_HEAD(&com->lc_link);
1756 INIT_LIST_HEAD(&com->lc_link_dir);
1757 init_rwsem(&com->lc_sem);
1758 atomic_set(&com->lc_ref, 1);
1759 com->lc_lfsck = lfsck;
1760 com->lc_type = LT_LAYOUT;
1761 if (lfsck->li_master) {
1762 struct lfsck_layout_master_data *llmd;
1764 com->lc_ops = &lfsck_layout_master_ops;
1765 OBD_ALLOC_PTR(llmd);
1767 GOTO(out, rc = -ENOMEM);
1769 INIT_LIST_HEAD(&llmd->llmd_req_list);
1770 spin_lock_init(&llmd->llmd_lock);
1771 init_waitqueue_head(&llmd->llmd_thread.t_ctl_waitq);
1772 atomic_set(&llmd->llmd_rpcs_in_flight, 0);
1773 com->lc_data = llmd;
1775 struct lfsck_layout_slave_data *llsd;
1777 com->lc_ops = &lfsck_layout_slave_ops;
1778 OBD_ALLOC_PTR(llsd);
1780 GOTO(out, rc = -ENOMEM);
1782 INIT_LIST_HEAD(&llsd->llsd_seq_list);
1783 com->lc_data = llsd;
1785 com->lc_file_size = sizeof(*lo);
1786 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1787 if (com->lc_file_ram == NULL)
1788 GOTO(out, rc = -ENOMEM);
1790 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1791 if (com->lc_file_disk == NULL)
1792 GOTO(out, rc = -ENOMEM);
1794 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1796 GOTO(out, rc = PTR_ERR(root));
1798 if (unlikely(!dt_try_as_dir(env, root)))
1799 GOTO(out, rc = -ENOTDIR);
1801 obj = local_file_find_or_create(env, lfsck->li_los, root,
1803 S_IFREG | S_IRUGO | S_IWUSR);
1805 GOTO(out, rc = PTR_ERR(obj));
1808 rc = lfsck_layout_load(env, com);
1810 rc = lfsck_layout_reset(env, com, true);
1811 else if (rc == -ENOENT)
1812 rc = lfsck_layout_init(env, com);
1817 lo = com->lc_file_ram;
1818 switch (lo->ll_status) {
1824 spin_lock(&lfsck->li_lock);
1825 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1826 spin_unlock(&lfsck->li_lock);
1829 CERROR("%s: unknown lfsck_layout status: rc = %u\n",
1830 lfsck_lfsck2name(lfsck), lo->ll_status);
1832 case LS_SCANNING_PHASE1:
1833 case LS_SCANNING_PHASE2:
1834 /* No need to store the status to disk right now.
1835 * If the system crashed before the status stored,
1836 * it will be loaded back when next time. */
1837 lo->ll_status = LS_CRASHED;
1838 lo->ll_flags |= LF_INCOMPLETE;
1845 spin_lock(&lfsck->li_lock);
1846 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1847 spin_unlock(&lfsck->li_lock);
1851 if (lo->ll_flags & LF_CRASHED_LASTID) {
1852 LASSERT(lfsck->li_out_notify != NULL);
1854 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1855 LE_LASTID_REBUILDING);
1861 if (root != NULL && !IS_ERR(root))
1862 lu_object_put(env, &root->do_lu);
1865 lfsck_component_cleanup(env, com);