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 if (lfsck->li_paused)
671 lo->ll_status = LS_PAUSED;
673 lo->ll_status = LS_STOPPED;
675 lo->ll_status = LS_FAILED;
678 if (lo->ll_status != LS_PAUSED) {
679 spin_lock(&lfsck->li_lock);
680 list_del_init(&com->lc_link);
681 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
682 spin_unlock(&lfsck->li_lock);
685 rc = lfsck_layout_store(env, com);
687 up_write(&com->lc_sem);
692 static int lfsck_layout_assistant(void *args)
694 struct lfsck_thread_args *lta = args;
695 struct lu_env *env = <a->lta_env;
696 struct lfsck_component *com = lta->lta_com;
697 struct lfsck_instance *lfsck = lta->lta_lfsck;
698 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
699 struct lfsck_position *pos = &com->lc_pos_start;
700 struct lfsck_thread_info *info = lfsck_env_info(env);
701 struct lfsck_request *lr = &info->lti_lr;
702 struct lfsck_layout_master_data *llmd = com->lc_data;
703 struct ptlrpc_thread *mthread = &lfsck->li_thread;
704 struct ptlrpc_thread *athread = &llmd->llmd_thread;
705 struct lfsck_layout_req *llr;
706 struct l_wait_info lwi = { 0 };
711 memset(lr, 0, sizeof(*lr));
712 lr->lr_event = LE_START;
713 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
714 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
716 lr->lr_speed = bk->lb_speed_limit;
717 lr->lr_version = bk->lb_version;
718 lr->lr_active = LT_LAYOUT;
719 lr->lr_param = bk->lb_param;
720 lr->lr_async_windows = bk->lb_async_windows;
721 if (pos->lp_oit_cookie <= 1)
722 lr->lr_param |= LPF_RESET;
724 rc = lfsck_layout_master_notify_others(env, com, lr);
726 CERROR("%s: fail to notify others for layout start: rc = %d\n",
727 lfsck_lfsck2name(lfsck), rc);
731 spin_lock(&llmd->llmd_lock);
732 thread_set_flags(athread, SVC_RUNNING);
733 spin_unlock(&llmd->llmd_lock);
734 wake_up_all(&mthread->t_ctl_waitq);
737 while (!list_empty(&llmd->llmd_req_list)) {
740 l_wait_event(athread->t_ctl_waitq,
741 bk->lb_async_windows == 0 ||
742 atomic_read(&llmd->llmd_rpcs_in_flight) <
743 bk->lb_async_windows ||
747 if (unlikely(llmd->llmd_exit))
748 GOTO(cleanup1, rc = llmd->llmd_post_result);
750 /* XXX: To be extended in other patch.
752 * Compare the OST side attribute with local attribute,
753 * and fix it if found inconsistency. */
755 spin_lock(&llmd->llmd_lock);
756 llr = list_entry(llmd->llmd_req_list.next,
757 struct lfsck_layout_req,
759 list_del_init(&llr->llr_list);
760 if (bk->lb_async_windows != 0 &&
761 llmd->llmd_prefetched >= bk->lb_async_windows)
764 llmd->llmd_prefetched--;
765 spin_unlock(&llmd->llmd_lock);
767 wake_up_all(&mthread->t_ctl_waitq);
769 lfsck_layout_req_fini(env, llr);
772 /* Wakeup the master engine if it is waiting in checkpoint. */
773 if (atomic_read(&llmd->llmd_rpcs_in_flight) == 0)
774 wake_up_all(&mthread->t_ctl_waitq);
776 l_wait_event(athread->t_ctl_waitq,
777 !lfsck_layout_req_empty(llmd) ||
779 llmd->llmd_to_post ||
780 llmd->llmd_to_double_scan,
783 if (unlikely(llmd->llmd_exit))
784 GOTO(cleanup1, rc = llmd->llmd_post_result);
786 if (!list_empty(&llmd->llmd_req_list))
789 if (llmd->llmd_to_post) {
790 llmd->llmd_to_post = 0;
791 LASSERT(llmd->llmd_post_result > 0);
793 memset(lr, 0, sizeof(*lr));
794 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
795 lr->lr_active = LT_LAYOUT;
796 lr->lr_event = LE_PHASE1_DONE;
797 lr->lr_status = llmd->llmd_post_result;
798 rc = lfsck_layout_master_notify_others(env, com, lr);
800 CERROR("%s: failed to notify others "
801 "for layout post: rc = %d\n",
802 lfsck_lfsck2name(lfsck), rc);
804 /* Wakeup the master engine to go ahead. */
805 wake_up_all(&mthread->t_ctl_waitq);
808 if (llmd->llmd_to_double_scan) {
809 llmd->llmd_to_double_scan = 0;
810 atomic_inc(&lfsck->li_double_scan_count);
811 llmd->llmd_in_double_scan = 1;
812 wake_up_all(&mthread->t_ctl_waitq);
814 while (llmd->llmd_in_double_scan) {
815 rc = lfsck_layout_master_query_others(env, com);
816 if (lfsck_layout_master_to_orphan(llmd))
822 /* Pull LFSCK status on related targets once
823 * per 30 seconds if we are not notified. */
824 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
827 rc = l_wait_event(athread->t_ctl_waitq,
828 lfsck_layout_master_to_orphan(llmd) ||
830 !thread_is_running(mthread),
833 if (unlikely(llmd->llmd_exit ||
834 !thread_is_running(mthread)))
835 GOTO(cleanup2, rc = 0);
837 if (rc == -ETIMEDOUT)
844 /* XXX: real double scan for ost orphans. */
846 GOTO(cleanup2, rc = 1);
852 /* Cleanup the unfinished requests. */
853 spin_lock(&llmd->llmd_lock);
854 while (!list_empty(&llmd->llmd_req_list)) {
855 llr = list_entry(llmd->llmd_req_list.next,
856 struct lfsck_layout_req,
858 list_del_init(&llr->llr_list);
859 llmd->llmd_prefetched--;
860 spin_unlock(&llmd->llmd_lock);
861 lfsck_layout_req_fini(env, llr);
862 spin_lock(&llmd->llmd_lock);
864 spin_unlock(&llmd->llmd_lock);
866 LASSERTF(llmd->llmd_prefetched == 0, "unmatched prefeteched objs %d\n",
867 llmd->llmd_prefetched);
869 l_wait_event(athread->t_ctl_waitq,
870 atomic_read(&llmd->llmd_rpcs_in_flight) == 0,
874 memset(lr, 0, sizeof(*lr));
875 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
876 lr->lr_active = LT_LAYOUT;
878 lr->lr_event = LE_PHASE2_DONE;
880 } else if (rc == 0) {
881 lr->lr_event = LE_STOP;
882 if (lfsck->li_paused)
883 lr->lr_status = LS_CO_PAUSED;
885 lr->lr_status = LS_CO_STOPPED;
887 lr->lr_event = LE_STOP;
888 lr->lr_status = LS_CO_FAILED;
891 rc1 = lfsck_layout_master_notify_others(env, com, lr);
893 CERROR("%s: failed to notify others for layout quit: rc = %d\n",
894 lfsck_lfsck2name(lfsck), rc1);
898 /* Under force exit case, some requests may be just freed without
899 * verification, those objects should be re-handled when next run.
900 * So not update the on-disk tracing file under such case. */
901 if (!llmd->llmd_exit)
902 rc1 = lfsck_layout_double_scan_result(env, com, rc);
905 if (llmd->llmd_in_double_scan)
906 atomic_dec(&lfsck->li_double_scan_count);
908 spin_lock(&llmd->llmd_lock);
909 llmd->llmd_assistant_status = (rc1 != 0 ? rc1 : rc);
910 thread_set_flags(athread, SVC_STOPPED);
911 wake_up_all(&mthread->t_ctl_waitq);
912 spin_unlock(&llmd->llmd_lock);
913 lfsck_thread_args_fini(lta);
920 static int lfsck_layout_reset(const struct lu_env *env,
921 struct lfsck_component *com, bool init)
923 struct lfsck_layout *lo = com->lc_file_ram;
926 down_write(&com->lc_sem);
928 memset(lo, 0, com->lc_file_size);
930 __u32 count = lo->ll_success_count;
931 __u64 last_time = lo->ll_time_last_complete;
933 memset(lo, 0, com->lc_file_size);
934 lo->ll_success_count = count;
935 lo->ll_time_last_complete = last_time;
938 lo->ll_magic = LFSCK_LAYOUT_MAGIC;
939 lo->ll_status = LS_INIT;
941 rc = lfsck_layout_store(env, com);
942 up_write(&com->lc_sem);
947 static void lfsck_layout_fail(const struct lu_env *env,
948 struct lfsck_component *com, bool new_checked)
950 struct lfsck_layout *lo = com->lc_file_ram;
952 down_write(&com->lc_sem);
954 com->lc_new_checked++;
955 lo->ll_objs_failed_phase1++;
956 if (lo->ll_pos_first_inconsistent == 0) {
957 struct lfsck_instance *lfsck = com->lc_lfsck;
959 lo->ll_pos_first_inconsistent =
960 lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
963 up_write(&com->lc_sem);
966 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
967 struct lfsck_component *com, bool init)
969 struct lfsck_instance *lfsck = com->lc_lfsck;
970 struct lfsck_layout *lo = com->lc_file_ram;
971 struct lfsck_layout_master_data *llmd = com->lc_data;
972 struct ptlrpc_thread *mthread = &lfsck->li_thread;
973 struct ptlrpc_thread *athread = &llmd->llmd_thread;
974 struct l_wait_info lwi = { 0 };
977 if (com->lc_new_checked == 0 && !init)
980 l_wait_event(mthread->t_ctl_waitq,
981 (list_empty(&llmd->llmd_req_list) &&
982 atomic_read(&llmd->llmd_rpcs_in_flight) == 0) ||
983 !thread_is_running(mthread) ||
984 thread_is_stopped(athread),
987 if (!thread_is_running(mthread) || thread_is_stopped(athread))
990 down_write(&com->lc_sem);
992 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
994 lo->ll_pos_last_checkpoint =
995 lfsck->li_pos_current.lp_oit_cookie;
996 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
997 HALF_SEC - lfsck->li_time_last_checkpoint);
998 lo->ll_time_last_checkpoint = cfs_time_current_sec();
999 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1000 com->lc_new_checked = 0;
1003 rc = lfsck_layout_store(env, com);
1004 up_write(&com->lc_sem);
1009 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
1010 struct lfsck_component *com, bool init)
1012 struct lfsck_instance *lfsck = com->lc_lfsck;
1013 struct lfsck_layout *lo = com->lc_file_ram;
1016 if (com->lc_new_checked == 0 && !init)
1019 down_write(&com->lc_sem);
1022 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
1024 lo->ll_pos_last_checkpoint =
1025 lfsck->li_pos_current.lp_oit_cookie;
1026 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1027 HALF_SEC - lfsck->li_time_last_checkpoint);
1028 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1029 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1030 com->lc_new_checked = 0;
1033 rc = lfsck_layout_store(env, com);
1035 up_write(&com->lc_sem);
1040 static int lfsck_layout_slave_prep(const struct lu_env *env,
1041 struct lfsck_component *com)
1043 struct lfsck_instance *lfsck = com->lc_lfsck;
1044 struct lfsck_layout *lo = com->lc_file_ram;
1045 struct lfsck_position *pos = &com->lc_pos_start;
1047 /* XXX: For a new scanning, generate OST-objects
1048 * bitmap for orphan detection. */
1050 fid_zero(&pos->lp_dir_parent);
1051 pos->lp_dir_cookie = 0;
1052 if (lo->ll_status == LS_COMPLETED ||
1053 lo->ll_status == LS_PARTIAL) {
1056 rc = lfsck_layout_reset(env, com, false);
1061 down_write(&com->lc_sem);
1063 lo->ll_time_latest_start = cfs_time_current_sec();
1065 spin_lock(&lfsck->li_lock);
1066 if (lo->ll_flags & LF_SCANNED_ONCE) {
1067 if (!lfsck->li_drop_dryrun ||
1068 lo->ll_pos_first_inconsistent == 0) {
1069 lo->ll_status = LS_SCANNING_PHASE2;
1070 list_del_init(&com->lc_link);
1071 list_add_tail(&com->lc_link,
1072 &lfsck->li_list_double_scan);
1073 pos->lp_oit_cookie = 0;
1077 lo->ll_status = LS_SCANNING_PHASE1;
1078 lo->ll_run_time_phase1 = 0;
1079 lo->ll_run_time_phase2 = 0;
1080 lo->ll_objs_checked_phase1 = 0;
1081 lo->ll_objs_checked_phase2 = 0;
1082 lo->ll_objs_failed_phase1 = 0;
1083 lo->ll_objs_failed_phase2 = 0;
1084 for (i = 0; i < LLIT_MAX; i++)
1085 lo->ll_objs_repaired[i] = 0;
1087 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1090 lo->ll_status = LS_SCANNING_PHASE1;
1091 if (!lfsck->li_drop_dryrun ||
1092 lo->ll_pos_first_inconsistent == 0)
1093 pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
1095 pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1097 spin_unlock(&lfsck->li_lock);
1099 up_write(&com->lc_sem);
1104 static int lfsck_layout_master_prep(const struct lu_env *env,
1105 struct lfsck_component *com)
1107 struct lfsck_instance *lfsck = com->lc_lfsck;
1108 struct lfsck_layout_master_data *llmd = com->lc_data;
1109 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1110 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1111 struct lfsck_thread_args *lta;
1115 rc = lfsck_layout_slave_prep(env, com);
1119 llmd->llmd_assistant_status = 0;
1120 llmd->llmd_post_result = 0;
1121 llmd->llmd_to_post = 0;
1122 llmd->llmd_to_double_scan = 0;
1123 llmd->llmd_in_double_scan = 0;
1124 llmd->llmd_exit = 0;
1125 thread_set_flags(athread, 0);
1127 lta = lfsck_thread_args_init(lfsck, com);
1129 RETURN(PTR_ERR(lta));
1131 rc = PTR_ERR(kthread_run(lfsck_layout_assistant, lta, "lfsck_layout"));
1132 if (IS_ERR_VALUE(rc)) {
1133 CERROR("%s: Cannot start LFSCK layout assistant thread: "
1134 "rc = %ld\n", lfsck_lfsck2name(lfsck), rc);
1135 lfsck_thread_args_fini(lta);
1137 struct l_wait_info lwi = { 0 };
1139 l_wait_event(mthread->t_ctl_waitq,
1140 thread_is_running(athread) ||
1141 thread_is_stopped(athread),
1143 if (unlikely(!thread_is_running(athread)))
1144 rc = llmd->llmd_assistant_status;
1152 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
1153 struct lfsck_component *com,
1154 struct dt_object *obj)
1156 /* XXX: To be implemented in other patches.
1158 * For the given object, read its layout EA locally. For each stripe,
1159 * pre-fetch the OST-object's attribute and generate an structure
1160 * lfsck_layout_req on the list ::llmd_req_list.
1162 * For each request on the ::llmd_req_list, the lfsck_layout_assistant
1163 * thread will compare the OST side attribute with local attribute,
1164 * if inconsistent, then repair it.
1166 * All above processing is async mode with pipeline. */
1171 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
1172 struct lfsck_component *com,
1173 struct dt_object *obj)
1175 struct lfsck_instance *lfsck = com->lc_lfsck;
1176 struct lfsck_layout *lo = com->lc_file_ram;
1177 const struct lu_fid *fid = lfsck_dto2fid(obj);
1178 struct lfsck_layout_slave_data *llsd = com->lc_data;
1179 struct lfsck_layout_seq *lls;
1185 /* XXX: Update OST-objects bitmap for orphan detection. */
1187 LASSERT(llsd != NULL);
1189 down_write(&com->lc_sem);
1190 if (fid_is_idif(fid))
1192 else if (!fid_is_norm(fid) ||
1193 !fid_is_for_ostobj(env, lfsck->li_next, obj, fid))
1194 GOTO(unlock, rc = 0);
1197 com->lc_new_checked++;
1199 lls = lfsck_layout_seq_lookup(llsd, seq);
1202 if (unlikely(lls == NULL))
1203 GOTO(unlock, rc = -ENOMEM);
1205 INIT_LIST_HEAD(&lls->lls_list);
1207 rc = lfsck_layout_lastid_load(env, com, lls);
1209 lo->ll_objs_failed_phase1++;
1214 lfsck_layout_seq_insert(llsd, lls);
1217 if (unlikely(fid_is_last_id(fid)))
1218 GOTO(unlock, rc = 0);
1221 if (oid > lls->lls_lastid_known)
1222 lls->lls_lastid_known = oid;
1224 if (oid > lls->lls_lastid) {
1225 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1226 /* OFD may create new objects during LFSCK scanning. */
1227 rc = lfsck_layout_lastid_reload(env, com, lls);
1228 if (unlikely(rc != 0))
1229 CWARN("%s: failed to reload LAST_ID for "LPX64
1231 lfsck_lfsck2name(com->lc_lfsck),
1233 if (oid <= lls->lls_lastid)
1234 GOTO(unlock, rc = 0);
1236 LASSERT(lfsck->li_out_notify != NULL);
1238 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1239 LE_LASTID_REBUILDING);
1240 lo->ll_flags |= LF_CRASHED_LASTID;
1243 lls->lls_lastid = oid;
1247 GOTO(unlock, rc = 0);
1250 up_write(&com->lc_sem);
1255 static int lfsck_layout_exec_dir(const struct lu_env *env,
1256 struct lfsck_component *com,
1257 struct dt_object *obj,
1258 struct lu_dirent *ent)
1263 static int lfsck_layout_master_post(const struct lu_env *env,
1264 struct lfsck_component *com,
1265 int result, bool init)
1267 struct lfsck_instance *lfsck = com->lc_lfsck;
1268 struct lfsck_layout *lo = com->lc_file_ram;
1269 struct lfsck_layout_master_data *llmd = com->lc_data;
1270 struct ptlrpc_thread *mthread = &lfsck->li_thread;
1271 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1272 struct l_wait_info lwi = { 0 };
1277 llmd->llmd_post_result = result;
1278 llmd->llmd_to_post = 1;
1279 if (llmd->llmd_post_result <= 0)
1280 llmd->llmd_exit = 1;
1282 wake_up_all(&athread->t_ctl_waitq);
1283 l_wait_event(mthread->t_ctl_waitq,
1284 (result > 0 && list_empty(&llmd->llmd_req_list) &&
1285 atomic_read(&llmd->llmd_rpcs_in_flight) == 0) ||
1286 thread_is_stopped(athread),
1289 if (llmd->llmd_assistant_status < 0)
1290 result = llmd->llmd_assistant_status;
1292 down_write(&com->lc_sem);
1293 spin_lock(&lfsck->li_lock);
1294 /* When LFSCK failed, there may be some prefetched objects those are
1295 * not been processed yet, we do not know the exactly position, then
1296 * just restart from last check-point next time. */
1297 if (!init && !llmd->llmd_exit)
1298 lo->ll_pos_last_checkpoint =
1299 lfsck->li_pos_current.lp_oit_cookie;
1302 lo->ll_status = LS_SCANNING_PHASE2;
1303 lo->ll_flags |= LF_SCANNED_ONCE;
1304 lo->ll_flags &= ~LF_UPGRADE;
1305 list_del_init(&com->lc_link);
1306 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1307 } else if (result == 0) {
1308 if (lfsck->li_paused) {
1309 lo->ll_status = LS_PAUSED;
1311 lo->ll_status = LS_STOPPED;
1312 list_del_init(&com->lc_link);
1313 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1316 lo->ll_status = LS_FAILED;
1317 list_del_init(&com->lc_link);
1318 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1320 spin_unlock(&lfsck->li_lock);
1323 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1324 HALF_SEC - lfsck->li_time_last_checkpoint);
1325 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1326 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1327 com->lc_new_checked = 0;
1330 rc = lfsck_layout_store(env, com);
1331 up_write(&com->lc_sem);
1336 static int lfsck_layout_slave_post(const struct lu_env *env,
1337 struct lfsck_component *com,
1338 int result, bool init)
1340 struct lfsck_instance *lfsck = com->lc_lfsck;
1341 struct lfsck_layout *lo = com->lc_file_ram;
1345 rc = lfsck_layout_lastid_store(env, com);
1349 LASSERT(lfsck->li_out_notify != NULL);
1351 down_write(&com->lc_sem);
1353 spin_lock(&lfsck->li_lock);
1355 lo->ll_pos_last_checkpoint =
1356 lfsck->li_pos_current.lp_oit_cookie;
1358 lo->ll_status = LS_SCANNING_PHASE2;
1359 lo->ll_flags |= LF_SCANNED_ONCE;
1360 if (lo->ll_flags & LF_CRASHED_LASTID) {
1362 lo->ll_flags &= ~LF_CRASHED_LASTID;
1364 lo->ll_flags &= ~LF_UPGRADE;
1365 list_del_init(&com->lc_link);
1366 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1367 } else if (result == 0) {
1368 if (lfsck->li_paused) {
1369 lo->ll_status = LS_PAUSED;
1371 lo->ll_status = LS_STOPPED;
1372 list_del_init(&com->lc_link);
1373 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1376 lo->ll_status = LS_FAILED;
1377 list_del_init(&com->lc_link);
1378 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1380 spin_unlock(&lfsck->li_lock);
1383 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1387 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1388 HALF_SEC - lfsck->li_time_last_checkpoint);
1389 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1390 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1391 com->lc_new_checked = 0;
1394 rc = lfsck_layout_store(env, com);
1396 up_write(&com->lc_sem);
1401 static int lfsck_layout_dump(const struct lu_env *env,
1402 struct lfsck_component *com, char *buf, int len)
1404 struct lfsck_instance *lfsck = com->lc_lfsck;
1405 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1406 struct lfsck_layout *lo = com->lc_file_ram;
1411 down_read(&com->lc_sem);
1412 rc = snprintf(buf, len,
1413 "name: lfsck_layout\n"
1419 lfsck_status2names(lo->ll_status));
1425 rc = lfsck_bits_dump(&buf, &len, lo->ll_flags, lfsck_flags_names,
1430 rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1435 rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_complete,
1436 "time_since_last_completed");
1440 rc = lfsck_time_dump(&buf, &len, lo->ll_time_latest_start,
1441 "time_since_latest_start");
1445 rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_checkpoint,
1446 "time_since_last_checkpoint");
1450 rc = snprintf(buf, len,
1451 "latest_start_position: "LPU64"\n"
1452 "last_checkpoint_position: "LPU64"\n"
1453 "first_failure_position: "LPU64"\n",
1454 lo->ll_pos_latest_start,
1455 lo->ll_pos_last_checkpoint,
1456 lo->ll_pos_first_inconsistent);
1463 rc = snprintf(buf, len,
1464 "success_count: %u\n"
1465 "repaired_dangling: "LPU64"\n"
1466 "repaired_unmatched_pair: "LPU64"\n"
1467 "repaired_multiple_referenced: "LPU64"\n"
1468 "repaired_orphan: "LPU64"\n"
1469 "repaired_inconsistent_owner: "LPU64"\n"
1470 "repaired_others: "LPU64"\n"
1471 "skipped: "LPU64"\n"
1472 "failed_phase1: "LPU64"\n"
1473 "failed_phase2: "LPU64"\n",
1474 lo->ll_success_count,
1475 lo->ll_objs_repaired[LLIT_DANGLING - 1],
1476 lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
1477 lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
1478 lo->ll_objs_repaired[LLIT_ORPHAN - 1],
1479 lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
1480 lo->ll_objs_repaired[LLIT_OTHERS - 1],
1481 lo->ll_objs_skipped,
1482 lo->ll_objs_failed_phase1,
1483 lo->ll_objs_failed_phase2);
1490 if (lo->ll_status == LS_SCANNING_PHASE1) {
1492 const struct dt_it_ops *iops;
1493 cfs_duration_t duration = cfs_time_current() -
1494 lfsck->li_time_last_checkpoint;
1495 __u64 checked = lo->ll_objs_checked_phase1 + com->lc_new_checked;
1496 __u64 speed = checked;
1497 __u64 new_checked = com->lc_new_checked * HZ;
1498 __u32 rtime = lo->ll_run_time_phase1 +
1499 cfs_duration_sec(duration + HALF_SEC);
1502 do_div(new_checked, duration);
1504 do_div(speed, rtime);
1505 rc = snprintf(buf, len,
1506 "checked_phase1: "LPU64"\n"
1507 "checked_phase2: "LPU64"\n"
1508 "run_time_phase1: %u seconds\n"
1509 "run_time_phase2: %u seconds\n"
1510 "average_speed_phase1: "LPU64" items/sec\n"
1511 "average_speed_phase2: N/A\n"
1512 "real-time_speed_phase1: "LPU64" items/sec\n"
1513 "real-time_speed_phase2: N/A\n",
1515 lo->ll_objs_checked_phase2,
1517 lo->ll_run_time_phase2,
1526 LASSERT(lfsck->li_di_oit != NULL);
1528 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1530 /* The low layer otable-based iteration position may NOT
1531 * exactly match the layout-based directory traversal
1532 * cookie. Generally, it is not a serious issue. But the
1533 * caller should NOT make assumption on that. */
1534 pos = iops->store(env, lfsck->li_di_oit);
1535 if (!lfsck->li_current_oit_processed)
1537 rc = snprintf(buf, len, "current_position: "LPU64"\n", pos);
1544 /* XXX: LS_SCANNING_PHASE2 will be handled in the future. */
1545 __u64 speed1 = lo->ll_objs_checked_phase1;
1546 __u64 speed2 = lo->ll_objs_checked_phase2;
1548 if (lo->ll_run_time_phase1 != 0)
1549 do_div(speed1, lo->ll_run_time_phase1);
1550 if (lo->ll_run_time_phase2 != 0)
1551 do_div(speed2, lo->ll_run_time_phase2);
1552 rc = snprintf(buf, len,
1553 "checked_phase1: "LPU64"\n"
1554 "checked_phase2: "LPU64"\n"
1555 "run_time_phase1: %u seconds\n"
1556 "run_time_phase2: %u seconds\n"
1557 "average_speed_phase1: "LPU64" items/sec\n"
1558 "average_speed_phase2: "LPU64" objs/sec\n"
1559 "real-time_speed_phase1: N/A\n"
1560 "real-time_speed_phase2: N/A\n"
1561 "current_position: N/A\n",
1562 lo->ll_objs_checked_phase1,
1563 lo->ll_objs_checked_phase2,
1564 lo->ll_run_time_phase1,
1565 lo->ll_run_time_phase2,
1577 up_read(&com->lc_sem);
1582 static int lfsck_layout_master_double_scan(const struct lu_env *env,
1583 struct lfsck_component *com)
1585 struct lfsck_layout_master_data *llmd = com->lc_data;
1586 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
1587 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1588 struct lfsck_layout *lo = com->lc_file_ram;
1589 struct l_wait_info lwi = { 0 };
1591 if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1594 llmd->llmd_to_double_scan = 1;
1595 wake_up_all(&athread->t_ctl_waitq);
1596 l_wait_event(mthread->t_ctl_waitq,
1597 llmd->llmd_in_double_scan ||
1598 thread_is_stopped(athread),
1600 if (llmd->llmd_assistant_status < 0)
1601 return llmd->llmd_assistant_status;
1606 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
1607 struct lfsck_component *com)
1609 struct lfsck_instance *lfsck = com->lc_lfsck;
1610 struct lfsck_layout *lo = com->lc_file_ram;
1613 if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1616 atomic_inc(&lfsck->li_double_scan_count);
1618 com->lc_new_checked = 0;
1619 com->lc_new_scanned = 0;
1620 com->lc_time_last_checkpoint = cfs_time_current();
1621 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
1622 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1624 rc = lfsck_layout_double_scan_result(env, com, rc);
1626 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
1627 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
1632 static void lfsck_layout_master_data_release(const struct lu_env *env,
1633 struct lfsck_component *com)
1635 struct lfsck_layout_master_data *llmd = com->lc_data;
1637 LASSERT(llmd != NULL);
1638 LASSERT(thread_is_init(&llmd->llmd_thread) ||
1639 thread_is_stopped(&llmd->llmd_thread));
1640 LASSERT(list_empty(&llmd->llmd_req_list));
1641 LASSERT(atomic_read(&llmd->llmd_rpcs_in_flight) == 0);
1643 com->lc_data = NULL;
1647 static void lfsck_layout_slave_data_release(const struct lu_env *env,
1648 struct lfsck_component *com)
1650 struct lfsck_layout_slave_data *llsd = com->lc_data;
1651 struct lfsck_layout_seq *lls;
1652 struct lfsck_layout_seq *next;
1654 LASSERT(llsd != NULL);
1656 com->lc_data = NULL;
1658 list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
1660 list_del_init(&lls->lls_list);
1661 lfsck_object_put(env, lls->lls_lastid_obj);
1668 static void lfsck_layout_master_quit(const struct lu_env *env,
1669 struct lfsck_component *com)
1671 struct lfsck_layout_master_data *llmd = com->lc_data;
1672 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
1673 struct ptlrpc_thread *athread = &llmd->llmd_thread;
1674 struct l_wait_info lwi = { 0 };
1676 llmd->llmd_exit = 1;
1677 wake_up_all(&athread->t_ctl_waitq);
1678 l_wait_event(mthread->t_ctl_waitq,
1679 thread_is_init(athread) ||
1680 thread_is_stopped(athread),
1684 static struct lfsck_operations lfsck_layout_master_ops = {
1685 .lfsck_reset = lfsck_layout_reset,
1686 .lfsck_fail = lfsck_layout_fail,
1687 .lfsck_checkpoint = lfsck_layout_master_checkpoint,
1688 .lfsck_prep = lfsck_layout_master_prep,
1689 .lfsck_exec_oit = lfsck_layout_master_exec_oit,
1690 .lfsck_exec_dir = lfsck_layout_exec_dir,
1691 .lfsck_post = lfsck_layout_master_post,
1692 .lfsck_dump = lfsck_layout_dump,
1693 .lfsck_double_scan = lfsck_layout_master_double_scan,
1694 .lfsck_data_release = lfsck_layout_master_data_release,
1695 .lfsck_quit = lfsck_layout_master_quit,
1698 static struct lfsck_operations lfsck_layout_slave_ops = {
1699 .lfsck_reset = lfsck_layout_reset,
1700 .lfsck_fail = lfsck_layout_fail,
1701 .lfsck_checkpoint = lfsck_layout_slave_checkpoint,
1702 .lfsck_prep = lfsck_layout_slave_prep,
1703 .lfsck_exec_oit = lfsck_layout_slave_exec_oit,
1704 .lfsck_exec_dir = lfsck_layout_exec_dir,
1705 .lfsck_post = lfsck_layout_slave_post,
1706 .lfsck_dump = lfsck_layout_dump,
1707 .lfsck_double_scan = lfsck_layout_slave_double_scan,
1708 .lfsck_data_release = lfsck_layout_slave_data_release,
1711 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
1713 struct lfsck_component *com;
1714 struct lfsck_layout *lo;
1715 struct dt_object *root = NULL;
1716 struct dt_object *obj;
1724 INIT_LIST_HEAD(&com->lc_link);
1725 INIT_LIST_HEAD(&com->lc_link_dir);
1726 init_rwsem(&com->lc_sem);
1727 atomic_set(&com->lc_ref, 1);
1728 com->lc_lfsck = lfsck;
1729 com->lc_type = LT_LAYOUT;
1730 if (lfsck->li_master) {
1731 struct lfsck_layout_master_data *llmd;
1733 com->lc_ops = &lfsck_layout_master_ops;
1734 OBD_ALLOC_PTR(llmd);
1736 GOTO(out, rc = -ENOMEM);
1738 INIT_LIST_HEAD(&llmd->llmd_req_list);
1739 spin_lock_init(&llmd->llmd_lock);
1740 init_waitqueue_head(&llmd->llmd_thread.t_ctl_waitq);
1741 atomic_set(&llmd->llmd_rpcs_in_flight, 0);
1742 com->lc_data = llmd;
1744 struct lfsck_layout_slave_data *llsd;
1746 com->lc_ops = &lfsck_layout_slave_ops;
1747 OBD_ALLOC_PTR(llsd);
1749 GOTO(out, rc = -ENOMEM);
1751 INIT_LIST_HEAD(&llsd->llsd_seq_list);
1752 com->lc_data = llsd;
1754 com->lc_file_size = sizeof(*lo);
1755 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1756 if (com->lc_file_ram == NULL)
1757 GOTO(out, rc = -ENOMEM);
1759 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1760 if (com->lc_file_disk == NULL)
1761 GOTO(out, rc = -ENOMEM);
1763 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1765 GOTO(out, rc = PTR_ERR(root));
1767 if (unlikely(!dt_try_as_dir(env, root)))
1768 GOTO(out, rc = -ENOTDIR);
1770 obj = local_file_find_or_create(env, lfsck->li_los, root,
1772 S_IFREG | S_IRUGO | S_IWUSR);
1774 GOTO(out, rc = PTR_ERR(obj));
1777 rc = lfsck_layout_load(env, com);
1779 rc = lfsck_layout_reset(env, com, true);
1780 else if (rc == -ENOENT)
1781 rc = lfsck_layout_init(env, com);
1786 lo = com->lc_file_ram;
1787 switch (lo->ll_status) {
1793 spin_lock(&lfsck->li_lock);
1794 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1795 spin_unlock(&lfsck->li_lock);
1798 CERROR("%s: unknown lfsck_layout status: rc = %u\n",
1799 lfsck_lfsck2name(lfsck), lo->ll_status);
1801 case LS_SCANNING_PHASE1:
1802 case LS_SCANNING_PHASE2:
1803 /* No need to store the status to disk right now.
1804 * If the system crashed before the status stored,
1805 * it will be loaded back when next time. */
1806 lo->ll_status = LS_CRASHED;
1807 lo->ll_flags |= LF_INCOMPLETE;
1814 spin_lock(&lfsck->li_lock);
1815 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1816 spin_unlock(&lfsck->li_lock);
1820 if (lo->ll_flags & LF_CRASHED_LASTID) {
1821 LASSERT(lfsck->li_out_notify != NULL);
1823 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1824 LE_LASTID_REBUILDING);
1830 if (root != NULL && !IS_ERR(root))
1831 lu_object_put(env, &root->do_lu);
1834 lfsck_component_cleanup(env, com);