Whamcloud - gitweb
LU-1267 lfsck: framework (3) for MDT-OST consistency
[fs/lustre-release.git] / lustre / lfsck / lfsck_layout.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
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.
15
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_layout.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34 #define DEBUG_SUBSYSTEM S_LFSCK
35
36 #include <linux/bitops.h>
37
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>
48
49 #include "lfsck_internal.h"
50
51 #define LFSCK_LAYOUT_MAGIC              0xB173AE14
52
53 static const char lfsck_layout_name[] = "lfsck_layout";
54
55 struct lfsck_layout_seq {
56         struct list_head         lls_list;
57         __u64                    lls_seq;
58         __u64                    lls_lastid;
59         __u64                    lls_lastid_known;
60         struct dt_object        *lls_lastid_obj;
61         unsigned int             lls_dirty:1;
62 };
63
64 struct lfsck_layout_slave_data {
65         /* list for lfsck_layout_seq */
66         struct list_head         llsd_seq_list;
67 };
68
69 struct lfsck_layout_object {
70         struct dt_object        *llo_obj;
71         struct lu_attr           llo_attr;
72         atomic_t                 llo_ref;
73         __u16                    llo_gen;
74 };
75
76 struct lfsck_layout_req {
77         struct list_head                 llr_list;
78         struct lfsck_layout_object      *llr_parent;
79         struct dt_object                *llr_child;
80         __u32                            llr_ost_idx;
81         __u32                            llr_lov_idx; /* offset in LOV EA */
82 };
83
84 struct lfsck_layout_master_data {
85         struct list_head        llmd_req_list;
86         spinlock_t              llmd_lock;
87         struct ptlrpc_thread    llmd_thread;
88         atomic_t                llmd_rpcs_in_flight;
89         int                     llmd_prefetched;
90         int                     llmd_assistant_status;
91         int                     llmd_post_result;
92         unsigned int            llmd_to_post:1,
93                                 llmd_to_double_scan:1,
94                                 llmd_in_double_scan:1,
95                                 llmd_exit:1;
96 };
97
98 static inline void lfsck_layout_object_put(const struct lu_env *env,
99                                            struct lfsck_layout_object *llo)
100 {
101         if (atomic_dec_and_test(&llo->llo_ref)) {
102                 lfsck_object_put(env, llo->llo_obj);
103                 OBD_FREE_PTR(llo);
104         }
105 }
106
107 static inline void lfsck_layout_req_fini(const struct lu_env *env,
108                                          struct lfsck_layout_req *llr)
109 {
110         lu_object_put(env, &llr->llr_child->do_lu);
111         lfsck_layout_object_put(env, llr->llr_parent);
112         OBD_FREE_PTR(llr);
113 }
114
115 static inline bool lfsck_layout_req_empty(struct lfsck_layout_master_data *llmd)
116 {
117         bool empty = false;
118
119         spin_lock(&llmd->llmd_lock);
120         if (list_empty(&llmd->llmd_req_list))
121                 empty = true;
122         spin_unlock(&llmd->llmd_lock);
123
124         return empty;
125 }
126
127 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
128                                    const struct lfsck_layout *src)
129 {
130         int i;
131
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);
154 }
155
156 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
157                                    const struct lfsck_layout *src)
158 {
159         int i;
160
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);
183 }
184
185 /**
186  * \retval +ve: the lfsck_layout is broken, the caller should reset it.
187  * \retval 0: succeed.
188  * \retval -ve: failed cases.
189  */
190 static int lfsck_layout_load(const struct lu_env *env,
191                              struct lfsck_component *com)
192 {
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;
196         loff_t                           pos    = 0;
197         int                              rc;
198
199         rc = dbo->dbo_read(env, com->lc_obj,
200                            lfsck_buf_get(env, com->lc_file_disk, size), &pos,
201                            BYPASS_CAPA);
202         if (rc == 0) {
203                 return -ENOENT;
204         } else if (rc < 0) {
205                 CWARN("%s: failed to load lfsck_layout: rc = %d\n",
206                       lfsck_lfsck2name(com->lc_lfsck), rc);
207                 return 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);
211                 return 1;
212         }
213
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);
219                 return 1;
220         }
221
222         return 0;
223 }
224
225 static int lfsck_layout_store(const struct lu_env *env,
226                               struct lfsck_component *com)
227 {
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;
233         loff_t                    pos           = 0;
234         int                       rc;
235         ENTRY;
236
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);
243                 RETURN(rc);
244         }
245
246         rc = dt_declare_record_write(env, obj, size, pos, handle);
247         if (rc != 0) {
248                 CERROR("%s: fail to declare trans for storing lfsck_layout(1): "
249                        "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
250                 GOTO(out, rc);
251         }
252
253         rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
254         if (rc != 0) {
255                 CERROR("%s: fail to start trans for storing lfsck_layout: "
256                        "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
257                 GOTO(out, rc);
258         }
259
260         rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos,
261                              handle);
262         if (rc != 0)
263                 CERROR("%s: fail to store lfsck_layout(1): size = %d, "
264                        "rc = %d\n", lfsck_lfsck2name(lfsck), (int)size, rc);
265
266         GOTO(out, rc);
267
268 out:
269         dt_trans_stop(env, lfsck->li_bottom, handle);
270
271         return rc;
272 }
273
274 static int lfsck_layout_init(const struct lu_env *env,
275                              struct lfsck_component *com)
276 {
277         struct lfsck_layout *lo = com->lc_file_ram;
278         int rc;
279
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);
286
287         return rc;
288 }
289
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)
292 {
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;
296         int                      rc;
297
298         fld_range_set_any(&range);
299         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), &range);
300         if (rc == 0) {
301                 if (fld_range_is_ost(&range))
302                         return 1;
303
304                 return 0;
305         }
306
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);
312
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;
320         }
321
322         rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID, BYPASS_CAPA);
323
324         return rc > 0;
325 }
326
327 static struct lfsck_layout_seq *
328 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
329 {
330         struct lfsck_layout_seq *lls;
331
332         list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
333                 if (lls->lls_seq == seq)
334                         return lls;
335
336                 if (lls->lls_seq > seq)
337                         return NULL;
338         }
339
340         return NULL;
341 }
342
343 static void
344 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
345                         struct lfsck_layout_seq *lls)
346 {
347         struct lfsck_layout_seq *tmp;
348         struct list_head        *pos = &llsd->llsd_seq_list;
349
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;
353                         break;
354                 }
355         }
356         list_add_tail(&lls->lls_list, pos);
357 }
358
359 static int
360 lfsck_layout_lastid_create(const struct lu_env *env,
361                            struct lfsck_instance *lfsck,
362                            struct dt_object *obj)
363 {
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;
369         struct thandle           *th;
370         __u64                     lastid = 0;
371         loff_t                    pos    = 0;
372         int                       rc;
373         ENTRY;
374
375         CDEBUG(D_LFSCK, "To create LAST_ID for <seq> "LPX64"\n",
376                fid_seq(lfsck_dto2fid(obj)));
377
378         if (bk->lb_param & LPF_DRYRUN)
379                 return 0;
380
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);
385
386         th = dt_trans_create(env, dt);
387         if (IS_ERR(th))
388                 RETURN(rc = PTR_ERR(th));
389
390         rc = dt_declare_create(env, obj, la, NULL, dof, th);
391         if (rc != 0)
392                 GOTO(stop, rc);
393
394         rc = dt_declare_record_write(env, obj, sizeof(lastid), pos, th);
395         if (rc != 0)
396                 GOTO(stop, rc);
397
398         rc = dt_trans_start_local(env, dt, th);
399         if (rc != 0)
400                 GOTO(stop, rc);
401
402         dt_write_lock(env, obj, 0);
403         if (likely(!dt_object_exists(obj))) {
404                 rc = dt_create(env, obj, la, NULL, dof, th);
405                 if (rc == 0)
406                         rc = dt_record_write(env, obj,
407                                 lfsck_buf_get(env, &lastid, sizeof(lastid)),
408                                 &pos, th);
409         }
410         dt_write_unlock(env, obj);
411
412         GOTO(stop, rc);
413
414 stop:
415         dt_trans_stop(env, dt, th);
416
417         return rc;
418 }
419
420 static int
421 lfsck_layout_lastid_reload(const struct lu_env *env,
422                            struct lfsck_component *com,
423                            struct lfsck_layout_seq *lls)
424 {
425         __u64   lastid;
426         loff_t  pos     = 0;
427         int     rc;
428
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))
434                 return rc;
435
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;
440
441                 lls->lls_lastid = lls->lls_lastid_known;
442                 lls->lls_dirty = 1;
443                 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
444                         LASSERT(lfsck->li_out_notify != NULL);
445
446                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
447                                              LE_LASTID_REBUILDING);
448                         lo->ll_flags |= LF_CRASHED_LASTID;
449                 }
450         } else if (lastid >= lls->lls_lastid) {
451                 lls->lls_lastid = lastid;
452                 lls->lls_dirty = 0;
453         }
454
455         return 0;
456 }
457
458 static int
459 lfsck_layout_lastid_store(const struct lu_env *env,
460                           struct lfsck_component *com)
461 {
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;
467         struct thandle                  *th;
468         __u64                            lastid;
469         int                              rc     = 0;
470         int                              rc1    = 0;
471
472         list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
473                 loff_t pos = 0;
474
475                 /* XXX: Add the code back if we really found related
476                  *      inconsistent cases in the future. */
477 #if 0
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:
482                          *
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))
493                                 continue;
494                 }
495 #endif
496
497                 CDEBUG(D_LFSCK, "To sync the LAST_ID for <seq> "LPX64
498                        " as <oid> "LPU64"\n", lls->lls_seq, lls->lls_lastid);
499
500                 if (bk->lb_param & LPF_DRYRUN) {
501                         lls->lls_dirty = 0;
502                         continue;
503                 }
504
505                 th = dt_trans_create(env, dt);
506                 if (IS_ERR(th)) {
507                         rc1 = PTR_ERR(th);
508                         CERROR("%s: (1) failed to store "LPX64": rc = %d\n",
509                                lfsck_lfsck2name(com->lc_lfsck),
510                                lls->lls_seq, rc1);
511                         continue;
512                 }
513
514                 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
515                                              sizeof(lastid), pos, th);
516                 if (rc != 0)
517                         goto stop;
518
519                 rc = dt_trans_start_local(env, dt, th);
520                 if (rc != 0)
521                         goto stop;
522
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);
529                 if (rc == 0)
530                         lls->lls_dirty = 0;
531
532 stop:
533                 dt_trans_stop(env, dt, th);
534                 if (rc != 0) {
535                         rc1 = rc;
536                         CERROR("%s: (2) failed to store "LPX64": rc = %d\n",
537                                lfsck_lfsck2name(com->lc_lfsck),
538                                lls->lls_seq, rc1);
539                 }
540         }
541
542         return rc1;
543 }
544
545 static int
546 lfsck_layout_lastid_load(const struct lu_env *env,
547                          struct lfsck_component *com,
548                          struct lfsck_layout_seq *lls)
549 {
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;
554         loff_t                   pos    = 0;
555         int                      rc;
556         ENTRY;
557
558         lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck->li_bottom));
559         obj = dt_locate(env, lfsck->li_bottom, fid);
560         if (IS_ERR(obj))
561                 RETURN(PTR_ERR(obj));
562
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);
567
568                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
569                                              LE_LASTID_REBUILDING);
570                         lo->ll_flags |= LF_CRASHED_LASTID;
571
572                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
573                             cfs_fail_val > 0) {
574                                 struct l_wait_info lwi = LWI_TIMEOUT(
575                                                 cfs_time_seconds(cfs_fail_val),
576                                                 NULL, NULL);
577
578                                 up_write(&com->lc_sem);
579                                 l_wait_event(lfsck->li_thread.t_ctl_waitq,
580                                              !thread_is_running(&lfsck->li_thread),
581                                              &lwi);
582                                 down_write(&com->lc_sem);
583                         }
584                 }
585
586                 rc = lfsck_layout_lastid_create(env, lfsck, obj);
587         } else {
588                 dt_read_lock(env, obj, 0);
589                 rc = dt_read(env, obj,
590                         lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
591                         &pos);
592                 dt_read_unlock(env, obj);
593                 if (rc != 0 && rc != sizeof(__u64))
594                         GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
595
596                 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
597                         LASSERT(lfsck->li_out_notify != NULL);
598
599                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
600                                              LE_LASTID_REBUILDING);
601                         lo->ll_flags |= LF_CRASHED_LASTID;
602                 }
603
604                 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
605                 rc = 0;
606         }
607
608         GOTO(out, rc);
609
610 out:
611         if (rc != 0)
612                 lfsck_object_put(env, obj);
613         else
614                 lls->lls_lastid_obj = obj;
615
616         return rc;
617 }
618
619 static int lfsck_layout_master_query_others(const struct lu_env *env,
620                                             struct lfsck_component *com)
621 {
622         /* XXX: to be implemented. */
623
624         return 0;
625 }
626
627 static inline bool
628 lfsck_layout_master_to_orphan(struct lfsck_layout_master_data *llmd)
629 {
630         /* XXX: to be implemented. */
631
632         return 1;
633 }
634
635 static int lfsck_layout_master_notify_others(const struct lu_env *env,
636                                              struct lfsck_component *com,
637                                              struct lfsck_request *lr)
638 {
639         /* XXX: to be implemented. */
640
641         return 0;
642 }
643
644 static int lfsck_layout_double_scan_result(const struct lu_env *env,
645                                            struct lfsck_component *com,
646                                            int rc)
647 {
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;
651
652         down_write(&com->lc_sem);
653
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;
658
659         if (rc > 0) {
660                 com->lc_journal = 0;
661                 if (lo->ll_flags & LF_INCOMPLETE)
662                         lo->ll_status = LS_PARTIAL;
663                 else
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;
672                 else
673                         lo->ll_status = LS_STOPPED;
674         } else {
675                 lo->ll_status = LS_FAILED;
676         }
677
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);
683         }
684
685         rc = lfsck_layout_store(env, com);
686
687         up_write(&com->lc_sem);
688
689         return rc;
690 }
691
692 static int lfsck_layout_assistant(void *args)
693 {
694         struct lfsck_thread_args        *lta     = args;
695         struct lu_env                   *env     = &lta->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 };
707         int                              rc      = 0;
708         int                              rc1     = 0;
709         ENTRY;
710
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 |
715                        LSV_ASYNC_WINDOWS;
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;
723
724         rc = lfsck_layout_master_notify_others(env, com, lr);
725         if (rc != 0) {
726                 CERROR("%s: fail to notify others for layout start: rc = %d\n",
727                        lfsck_lfsck2name(lfsck), rc);
728                 GOTO(fini, rc);
729         }
730
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);
735
736         while (1) {
737                 while (!list_empty(&llmd->llmd_req_list)) {
738                         bool wakeup = false;
739
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 ||
744                                      llmd->llmd_exit,
745                                      &lwi);
746
747                         if (unlikely(llmd->llmd_exit))
748                                 GOTO(cleanup1, rc = llmd->llmd_post_result);
749
750                         /* XXX: To be extended in other patch.
751                          *
752                          * Compare the OST side attribute with local attribute,
753                          * and fix it if found inconsistency. */
754
755                         spin_lock(&llmd->llmd_lock);
756                         llr = list_entry(llmd->llmd_req_list.next,
757                                          struct lfsck_layout_req,
758                                          llr_list);
759                         list_del_init(&llr->llr_list);
760                         if (bk->lb_async_windows != 0 &&
761                             llmd->llmd_prefetched >= bk->lb_async_windows)
762                                 wakeup = true;
763
764                         llmd->llmd_prefetched--;
765                         spin_unlock(&llmd->llmd_lock);
766                         if (wakeup)
767                                 wake_up_all(&mthread->t_ctl_waitq);
768
769                         lfsck_layout_req_fini(env, llr);
770                 }
771
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);
775
776                 l_wait_event(athread->t_ctl_waitq,
777                              !lfsck_layout_req_empty(llmd) ||
778                              llmd->llmd_exit ||
779                              llmd->llmd_to_post ||
780                              llmd->llmd_to_double_scan,
781                              &lwi);
782
783                 if (unlikely(llmd->llmd_exit))
784                         GOTO(cleanup1, rc = llmd->llmd_post_result);
785
786                 if (!list_empty(&llmd->llmd_req_list))
787                         continue;
788
789                 if (llmd->llmd_to_post) {
790                         llmd->llmd_to_post = 0;
791                         LASSERT(llmd->llmd_post_result > 0);
792
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);
799                         if (rc != 0)
800                                 CERROR("%s: failed to notify others "
801                                        "for layout post: rc = %d\n",
802                                        lfsck_lfsck2name(lfsck), rc);
803
804                         /* Wakeup the master engine to go ahead. */
805                         wake_up_all(&mthread->t_ctl_waitq);
806                 }
807
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);
813
814                         while (llmd->llmd_in_double_scan) {
815                                 rc = lfsck_layout_master_query_others(env, com);
816                                 if (lfsck_layout_master_to_orphan(llmd))
817                                         goto orphan;
818
819                                 if (rc < 0)
820                                         GOTO(cleanup2, rc);
821
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),
825                                                            cfs_time_seconds(1),
826                                                            NULL, NULL);
827                                 rc = l_wait_event(athread->t_ctl_waitq,
828                                         lfsck_layout_master_to_orphan(llmd) ||
829                                         llmd->llmd_exit ||
830                                         !thread_is_running(mthread),
831                                         &lwi);
832
833                                 if (unlikely(llmd->llmd_exit ||
834                                              !thread_is_running(mthread)))
835                                         GOTO(cleanup2, rc = 0);
836
837                                 if (rc == -ETIMEDOUT)
838                                         continue;
839
840                                 if (rc < 0)
841                                         GOTO(cleanup2, rc);
842
843 orphan:
844                                 /* XXX: real double scan for ost orphans. */
845
846                                 GOTO(cleanup2, rc = 1);
847                         }
848                 }
849         }
850
851 cleanup1:
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,
857                                  llr_list);
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);
863         }
864         spin_unlock(&llmd->llmd_lock);
865
866         LASSERTF(llmd->llmd_prefetched == 0, "unmatched prefeteched objs %d\n",
867                  llmd->llmd_prefetched);
868
869         l_wait_event(athread->t_ctl_waitq,
870                      atomic_read(&llmd->llmd_rpcs_in_flight) == 0,
871                      &lwi);
872
873 cleanup2:
874         memset(lr, 0, sizeof(*lr));
875         lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
876         lr->lr_active = LT_LAYOUT;
877         if (rc > 0) {
878                 lr->lr_event = LE_PHASE2_DONE;
879                 lr->lr_status = rc;
880         } else if (rc == 0) {
881                 lr->lr_event = LE_STOP;
882                 if (lfsck->li_paused)
883                         lr->lr_status = LS_CO_PAUSED;
884                 else
885                         lr->lr_status = LS_CO_STOPPED;
886         } else {
887                 lr->lr_event = LE_STOP;
888                 lr->lr_status = LS_CO_FAILED;
889         }
890
891         rc1 = lfsck_layout_master_notify_others(env, com, lr);
892         if (rc1 != 0) {
893                 CERROR("%s: failed to notify others for layout quit: rc = %d\n",
894                        lfsck_lfsck2name(lfsck), rc1);
895                 rc = rc1;
896         }
897
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);
903
904 fini:
905         if (llmd->llmd_in_double_scan)
906                 atomic_dec(&lfsck->li_double_scan_count);
907
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);
914
915         return rc;
916 }
917
918 /* layout APIs */
919
920 static int lfsck_layout_reset(const struct lu_env *env,
921                               struct lfsck_component *com, bool init)
922 {
923         struct lfsck_layout     *lo    = com->lc_file_ram;
924         int                      rc;
925
926         down_write(&com->lc_sem);
927         if (init) {
928                 memset(lo, 0, com->lc_file_size);
929         } else {
930                 __u32 count = lo->ll_success_count;
931                 __u64 last_time = lo->ll_time_last_complete;
932
933                 memset(lo, 0, com->lc_file_size);
934                 lo->ll_success_count = count;
935                 lo->ll_time_last_complete = last_time;
936         }
937
938         lo->ll_magic = LFSCK_LAYOUT_MAGIC;
939         lo->ll_status = LS_INIT;
940
941         rc = lfsck_layout_store(env, com);
942         up_write(&com->lc_sem);
943
944         return rc;
945 }
946
947 static void lfsck_layout_fail(const struct lu_env *env,
948                               struct lfsck_component *com, bool new_checked)
949 {
950         struct lfsck_layout *lo = com->lc_file_ram;
951
952         down_write(&com->lc_sem);
953         if (new_checked)
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;
958
959                 lo->ll_pos_first_inconsistent =
960                         lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
961                                                         lfsck->li_di_oit);
962         }
963         up_write(&com->lc_sem);
964 }
965
966 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
967                                           struct lfsck_component *com, bool init)
968 {
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 };
975         int                              rc;
976
977         if (com->lc_new_checked == 0 && !init)
978                 return 0;
979
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),
985                      &lwi);
986
987         if (!thread_is_running(mthread) || thread_is_stopped(athread))
988                 return 0;
989
990         down_write(&com->lc_sem);
991         if (init) {
992                 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
993         } else {
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;
1001         }
1002
1003         rc = lfsck_layout_store(env, com);
1004         up_write(&com->lc_sem);
1005
1006         return rc;
1007 }
1008
1009 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
1010                                          struct lfsck_component *com, bool init)
1011 {
1012         struct lfsck_instance   *lfsck = com->lc_lfsck;
1013         struct lfsck_layout     *lo    = com->lc_file_ram;
1014         int                      rc;
1015
1016         if (com->lc_new_checked == 0 && !init)
1017                 return 0;
1018
1019         down_write(&com->lc_sem);
1020
1021         if (init) {
1022                 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
1023         } else {
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;
1031         }
1032
1033         rc = lfsck_layout_store(env, com);
1034
1035         up_write(&com->lc_sem);
1036
1037         return rc;
1038 }
1039
1040 static int lfsck_layout_slave_prep(const struct lu_env *env,
1041                                    struct lfsck_component *com)
1042 {
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;
1046
1047         /* XXX: For a new scanning, generate OST-objects
1048          *      bitmap for orphan detection. */
1049
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) {
1054                 int rc;
1055
1056                 rc = lfsck_layout_reset(env, com, false);
1057                 if (rc != 0)
1058                         return rc;
1059         }
1060
1061         down_write(&com->lc_sem);
1062
1063         lo->ll_time_latest_start = cfs_time_current_sec();
1064
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;
1074                 } else {
1075                         int i;
1076
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;
1086
1087                         pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1088                 }
1089         } else {
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;
1094                 else
1095                         pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1096         }
1097         spin_unlock(&lfsck->li_lock);
1098
1099         up_write(&com->lc_sem);
1100
1101         return 0;
1102 }
1103
1104 static int lfsck_layout_master_prep(const struct lu_env *env,
1105                                     struct lfsck_component *com)
1106 {
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;
1112         long                             rc;
1113         ENTRY;
1114
1115         rc = lfsck_layout_slave_prep(env, com);
1116         if (rc != 0)
1117                 RETURN(rc);
1118
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);
1126
1127         lta = lfsck_thread_args_init(lfsck, com);
1128         if (IS_ERR(lta))
1129                 RETURN(PTR_ERR(lta));
1130
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);
1136         } else {
1137                 struct l_wait_info lwi = { 0 };
1138
1139                 l_wait_event(mthread->t_ctl_waitq,
1140                              thread_is_running(athread) ||
1141                              thread_is_stopped(athread),
1142                              &lwi);
1143                 if (unlikely(!thread_is_running(athread)))
1144                         rc = llmd->llmd_assistant_status;
1145                 else
1146                         rc = 0;
1147         }
1148
1149         RETURN(rc);
1150 }
1151
1152 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
1153                                         struct lfsck_component *com,
1154                                         struct dt_object *obj)
1155 {
1156         /* XXX: To be implemented in other patches.
1157          *
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.
1161          *
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.
1165          *
1166          * All above processing is async mode with pipeline. */
1167
1168         return 0;
1169 }
1170
1171 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
1172                                        struct lfsck_component *com,
1173                                        struct dt_object *obj)
1174 {
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;
1180         __u64                            seq;
1181         __u64                            oid;
1182         int                              rc;
1183         ENTRY;
1184
1185         /* XXX: Update OST-objects bitmap for orphan detection. */
1186
1187         LASSERT(llsd != NULL);
1188
1189         down_write(&com->lc_sem);
1190         if (fid_is_idif(fid))
1191                 seq = 0;
1192         else if (!fid_is_norm(fid) ||
1193                  !fid_is_for_ostobj(env, lfsck->li_next, obj, fid))
1194                 GOTO(unlock, rc = 0);
1195         else
1196                 seq = fid_seq(fid);
1197         com->lc_new_checked++;
1198
1199         lls = lfsck_layout_seq_lookup(llsd, seq);
1200         if (lls == NULL) {
1201                 OBD_ALLOC_PTR(lls);
1202                 if (unlikely(lls == NULL))
1203                         GOTO(unlock, rc = -ENOMEM);
1204
1205                 INIT_LIST_HEAD(&lls->lls_list);
1206                 lls->lls_seq = seq;
1207                 rc = lfsck_layout_lastid_load(env, com, lls);
1208                 if (rc != 0) {
1209                         lo->ll_objs_failed_phase1++;
1210                         OBD_FREE_PTR(lls);
1211                         GOTO(unlock, rc);
1212                 }
1213
1214                 lfsck_layout_seq_insert(llsd, lls);
1215         }
1216
1217         if (unlikely(fid_is_last_id(fid)))
1218                 GOTO(unlock, rc = 0);
1219
1220         oid = fid_oid(fid);
1221         if (oid > lls->lls_lastid_known)
1222                 lls->lls_lastid_known = oid;
1223
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
1230                                       ": rc = %d\n",
1231                                       lfsck_lfsck2name(com->lc_lfsck),
1232                                       lls->lls_seq, rc);
1233                         if (oid <= lls->lls_lastid)
1234                                 GOTO(unlock, rc = 0);
1235
1236                         LASSERT(lfsck->li_out_notify != NULL);
1237
1238                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1239                                              LE_LASTID_REBUILDING);
1240                         lo->ll_flags |= LF_CRASHED_LASTID;
1241                 }
1242
1243                 lls->lls_lastid = oid;
1244                 lls->lls_dirty = 1;
1245         }
1246
1247         GOTO(unlock, rc = 0);
1248
1249 unlock:
1250         up_write(&com->lc_sem);
1251
1252         return rc;
1253 }
1254
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)
1259 {
1260         return 0;
1261 }
1262
1263 static int lfsck_layout_master_post(const struct lu_env *env,
1264                                     struct lfsck_component *com,
1265                                     int result, bool init)
1266 {
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 };
1273         int                              rc;
1274         ENTRY;
1275
1276
1277         llmd->llmd_post_result = result;
1278         llmd->llmd_to_post = 1;
1279         if (llmd->llmd_post_result <= 0)
1280                 llmd->llmd_exit = 1;
1281
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),
1287                      &lwi);
1288
1289         if (llmd->llmd_assistant_status < 0)
1290                 result = llmd->llmd_assistant_status;
1291
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;
1300
1301         if (result > 0) {
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;
1310                 } else {
1311                         lo->ll_status = LS_STOPPED;
1312                         list_del_init(&com->lc_link);
1313                         list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1314                 }
1315         } else {
1316                 lo->ll_status = LS_FAILED;
1317                 list_del_init(&com->lc_link);
1318                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1319         }
1320         spin_unlock(&lfsck->li_lock);
1321
1322         if (!init) {
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;
1328         }
1329
1330         rc = lfsck_layout_store(env, com);
1331         up_write(&com->lc_sem);
1332
1333         RETURN(rc);
1334 }
1335
1336 static int lfsck_layout_slave_post(const struct lu_env *env,
1337                                    struct lfsck_component *com,
1338                                    int result, bool init)
1339 {
1340         struct lfsck_instance   *lfsck = com->lc_lfsck;
1341         struct lfsck_layout     *lo    = com->lc_file_ram;
1342         int                      rc;
1343         bool                     done  = false;
1344
1345         rc = lfsck_layout_lastid_store(env, com);
1346         if (rc != 0)
1347                 result = rc;
1348
1349         LASSERT(lfsck->li_out_notify != NULL);
1350
1351         down_write(&com->lc_sem);
1352
1353         spin_lock(&lfsck->li_lock);
1354         if (!init)
1355                 lo->ll_pos_last_checkpoint =
1356                                         lfsck->li_pos_current.lp_oit_cookie;
1357         if (result > 0) {
1358                 lo->ll_status = LS_SCANNING_PHASE2;
1359                 lo->ll_flags |= LF_SCANNED_ONCE;
1360                 if (lo->ll_flags & LF_CRASHED_LASTID) {
1361                         done = true;
1362                         lo->ll_flags &= ~LF_CRASHED_LASTID;
1363                 }
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;
1370                 } else {
1371                         lo->ll_status = LS_STOPPED;
1372                         list_del_init(&com->lc_link);
1373                         list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1374                 }
1375         } else {
1376                 lo->ll_status = LS_FAILED;
1377                 list_del_init(&com->lc_link);
1378                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1379         }
1380         spin_unlock(&lfsck->li_lock);
1381
1382         if (done)
1383                 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1384                                      LE_LASTID_REBUILT);
1385
1386         if (!init) {
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;
1392         }
1393
1394         rc = lfsck_layout_store(env, com);
1395
1396         up_write(&com->lc_sem);
1397
1398         return rc;
1399 }
1400
1401 static int lfsck_layout_dump(const struct lu_env *env,
1402                              struct lfsck_component *com, char *buf, int len)
1403 {
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;
1407         int                      save  = len;
1408         int                      ret   = -ENOSPC;
1409         int                      rc;
1410
1411         down_read(&com->lc_sem);
1412         rc = snprintf(buf, len,
1413                       "name: lfsck_layout\n"
1414                       "magic: %#x\n"
1415                       "version: %d\n"
1416                       "status: %s\n",
1417                       lo->ll_magic,
1418                       bk->lb_version,
1419                       lfsck_status2names(lo->ll_status));
1420         if (rc <= 0)
1421                 goto out;
1422
1423         buf += rc;
1424         len -= rc;
1425         rc = lfsck_bits_dump(&buf, &len, lo->ll_flags, lfsck_flags_names,
1426                              "flags");
1427         if (rc < 0)
1428                 goto out;
1429
1430         rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1431                              "param");
1432         if (rc < 0)
1433                 goto out;
1434
1435         rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_complete,
1436                              "time_since_last_completed");
1437         if (rc < 0)
1438                 goto out;
1439
1440         rc = lfsck_time_dump(&buf, &len, lo->ll_time_latest_start,
1441                              "time_since_latest_start");
1442         if (rc < 0)
1443                 goto out;
1444
1445         rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_checkpoint,
1446                              "time_since_last_checkpoint");
1447         if (rc < 0)
1448                 goto out;
1449
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);
1457         if (rc <= 0)
1458                 goto out;
1459
1460         buf += rc;
1461         len -= rc;
1462
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);
1484         if (rc <= 0)
1485                 goto out;
1486
1487         buf += rc;
1488         len -= rc;
1489
1490         if (lo->ll_status == LS_SCANNING_PHASE1) {
1491                 __u64 pos;
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);
1500
1501                 if (duration != 0)
1502                         do_div(new_checked, duration);
1503                 if (rtime != 0)
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",
1514                               checked,
1515                               lo->ll_objs_checked_phase2,
1516                               rtime,
1517                               lo->ll_run_time_phase2,
1518                               speed,
1519                               new_checked);
1520                 if (rc <= 0)
1521                         goto out;
1522
1523                 buf += rc;
1524                 len -= rc;
1525
1526                 LASSERT(lfsck->li_di_oit != NULL);
1527
1528                 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1529
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)
1536                         pos--;
1537                 rc = snprintf(buf, len, "current_position: "LPU64"\n", pos);
1538                 if (rc <= 0)
1539                         goto out;
1540
1541                 buf += rc;
1542                 len -= rc;
1543         } else {
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;
1547
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,
1566                               speed1,
1567                               speed2);
1568                 if (rc <= 0)
1569                         goto out;
1570
1571                 buf += rc;
1572                 len -= rc;
1573         }
1574         ret = save - len;
1575
1576 out:
1577         up_read(&com->lc_sem);
1578
1579         return ret;
1580 }
1581
1582 static int lfsck_layout_master_double_scan(const struct lu_env *env,
1583                                            struct lfsck_component *com)
1584 {
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 };
1590
1591         if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1592                 return 0;
1593
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),
1599                      &lwi);
1600         if (llmd->llmd_assistant_status < 0)
1601                 return llmd->llmd_assistant_status;
1602
1603         return 0;
1604 }
1605
1606 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
1607                                           struct lfsck_component *com)
1608 {
1609         struct lfsck_instance   *lfsck = com->lc_lfsck;
1610         struct lfsck_layout     *lo    = com->lc_file_ram;
1611         int                      rc    = 1;
1612
1613         if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1614                 return 0;
1615
1616         atomic_inc(&lfsck->li_double_scan_count);
1617
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);
1623
1624         rc = lfsck_layout_double_scan_result(env, com, rc);
1625
1626         if (atomic_dec_and_test(&lfsck->li_double_scan_count))
1627                 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
1628
1629         return rc;
1630 }
1631
1632 static void lfsck_layout_master_data_release(const struct lu_env *env,
1633                                              struct lfsck_component *com)
1634 {
1635         struct lfsck_layout_master_data *llmd = com->lc_data;
1636
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);
1642
1643         com->lc_data = NULL;
1644         OBD_FREE_PTR(llmd);
1645 }
1646
1647 static void lfsck_layout_slave_data_release(const struct lu_env *env,
1648                                             struct lfsck_component *com)
1649 {
1650         struct lfsck_layout_slave_data  *llsd   = com->lc_data;
1651         struct lfsck_layout_seq         *lls;
1652         struct lfsck_layout_seq         *next;
1653
1654         LASSERT(llsd != NULL);
1655
1656         com->lc_data = NULL;
1657
1658         list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
1659                                      lls_list) {
1660                 list_del_init(&lls->lls_list);
1661                 lfsck_object_put(env, lls->lls_lastid_obj);
1662                 OBD_FREE_PTR(lls);
1663         }
1664
1665         OBD_FREE_PTR(llsd);
1666 }
1667
1668 static void lfsck_layout_master_quit(const struct lu_env *env,
1669                                      struct lfsck_component *com)
1670 {
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 };
1675
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),
1681                      &lwi);
1682 }
1683
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,
1696 };
1697
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,
1709 };
1710
1711 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
1712 {
1713         struct lfsck_component  *com;
1714         struct lfsck_layout     *lo;
1715         struct dt_object        *root = NULL;
1716         struct dt_object        *obj;
1717         int                      rc;
1718         ENTRY;
1719
1720         OBD_ALLOC_PTR(com);
1721         if (com == NULL)
1722                 RETURN(-ENOMEM);
1723
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;
1732
1733                 com->lc_ops = &lfsck_layout_master_ops;
1734                 OBD_ALLOC_PTR(llmd);
1735                 if (llmd == NULL)
1736                         GOTO(out, rc = -ENOMEM);
1737
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;
1743         } else {
1744                 struct lfsck_layout_slave_data *llsd;
1745
1746                 com->lc_ops = &lfsck_layout_slave_ops;
1747                 OBD_ALLOC_PTR(llsd);
1748                 if (llsd == NULL)
1749                         GOTO(out, rc = -ENOMEM);
1750
1751                 INIT_LIST_HEAD(&llsd->llsd_seq_list);
1752                 com->lc_data = llsd;
1753         }
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);
1758
1759         OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1760         if (com->lc_file_disk == NULL)
1761                 GOTO(out, rc = -ENOMEM);
1762
1763         root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1764         if (IS_ERR(root))
1765                 GOTO(out, rc = PTR_ERR(root));
1766
1767         if (unlikely(!dt_try_as_dir(env, root)))
1768                 GOTO(out, rc = -ENOTDIR);
1769
1770         obj = local_file_find_or_create(env, lfsck->li_los, root,
1771                                         lfsck_layout_name,
1772                                         S_IFREG | S_IRUGO | S_IWUSR);
1773         if (IS_ERR(obj))
1774                 GOTO(out, rc = PTR_ERR(obj));
1775
1776         com->lc_obj = obj;
1777         rc = lfsck_layout_load(env, com);
1778         if (rc > 0)
1779                 rc = lfsck_layout_reset(env, com, true);
1780         else if (rc == -ENOENT)
1781                 rc = lfsck_layout_init(env, com);
1782
1783         if (rc != 0)
1784                 GOTO(out, rc);
1785
1786         lo = com->lc_file_ram;
1787         switch (lo->ll_status) {
1788         case LS_INIT:
1789         case LS_COMPLETED:
1790         case LS_FAILED:
1791         case LS_STOPPED:
1792         case LS_PARTIAL:
1793                 spin_lock(&lfsck->li_lock);
1794                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1795                 spin_unlock(&lfsck->li_lock);
1796                 break;
1797         default:
1798                 CERROR("%s: unknown lfsck_layout status: rc = %u\n",
1799                        lfsck_lfsck2name(lfsck), lo->ll_status);
1800                 /* fall through */
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;
1808                 /* fall through */
1809         case LS_PAUSED:
1810         case LS_CRASHED:
1811         case LS_CO_FAILED:
1812         case LS_CO_STOPPED:
1813         case LS_CO_PAUSED:
1814                 spin_lock(&lfsck->li_lock);
1815                 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1816                 spin_unlock(&lfsck->li_lock);
1817                 break;
1818         }
1819
1820         if (lo->ll_flags & LF_CRASHED_LASTID) {
1821                 LASSERT(lfsck->li_out_notify != NULL);
1822
1823                 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1824                                      LE_LASTID_REBUILDING);
1825         }
1826
1827         GOTO(out, rc = 0);
1828
1829 out:
1830         if (root != NULL && !IS_ERR(root))
1831                 lu_object_put(env, &root->do_lu);
1832
1833         if (rc != 0)
1834                 lfsck_component_cleanup(env, com);
1835
1836         return rc;
1837 }