Whamcloud - gitweb
LU-1267 lfsck: enhance RPCs (1) 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                 lo->ll_status = lfsck->li_status;
671                 if (lo->ll_status == 0)
672                         lo->ll_status = LS_STOPPED;
673         } else {
674                 lo->ll_status = LS_FAILED;
675         }
676
677         if (lo->ll_status != LS_PAUSED) {
678                 spin_lock(&lfsck->li_lock);
679                 list_del_init(&com->lc_link);
680                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
681                 spin_unlock(&lfsck->li_lock);
682         }
683
684         rc = lfsck_layout_store(env, com);
685
686         up_write(&com->lc_sem);
687
688         return rc;
689 }
690
691 static int lfsck_layout_assistant(void *args)
692 {
693         struct lfsck_thread_args        *lta     = args;
694         struct lu_env                   *env     = &lta->lta_env;
695         struct lfsck_component          *com     = lta->lta_com;
696         struct lfsck_instance           *lfsck   = lta->lta_lfsck;
697         struct lfsck_bookmark           *bk      = &lfsck->li_bookmark_ram;
698         struct lfsck_position           *pos     = &com->lc_pos_start;
699         struct lfsck_thread_info        *info    = lfsck_env_info(env);
700         struct lfsck_request            *lr      = &info->lti_lr;
701         struct lfsck_layout_master_data *llmd    = com->lc_data;
702         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
703         struct ptlrpc_thread            *athread = &llmd->llmd_thread;
704         struct lfsck_layout_req         *llr;
705         struct l_wait_info               lwi     = { 0 };
706         int                              rc      = 0;
707         int                              rc1     = 0;
708         ENTRY;
709
710         memset(lr, 0, sizeof(*lr));
711         lr->lr_event = LE_START;
712         lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
713         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
714                        LSV_ASYNC_WINDOWS;
715         lr->lr_speed = bk->lb_speed_limit;
716         lr->lr_version = bk->lb_version;
717         lr->lr_active = LT_LAYOUT;
718         lr->lr_param = bk->lb_param;
719         lr->lr_async_windows = bk->lb_async_windows;
720         if (pos->lp_oit_cookie <= 1)
721                 lr->lr_param |= LPF_RESET;
722
723         rc = lfsck_layout_master_notify_others(env, com, lr);
724         if (rc != 0) {
725                 CERROR("%s: fail to notify others for layout start: rc = %d\n",
726                        lfsck_lfsck2name(lfsck), rc);
727                 GOTO(fini, rc);
728         }
729
730         spin_lock(&llmd->llmd_lock);
731         thread_set_flags(athread, SVC_RUNNING);
732         spin_unlock(&llmd->llmd_lock);
733         wake_up_all(&mthread->t_ctl_waitq);
734
735         while (1) {
736                 while (!list_empty(&llmd->llmd_req_list)) {
737                         bool wakeup = false;
738
739                         l_wait_event(athread->t_ctl_waitq,
740                                      bk->lb_async_windows == 0 ||
741                                      atomic_read(&llmd->llmd_rpcs_in_flight) <
742                                                 bk->lb_async_windows ||
743                                      llmd->llmd_exit,
744                                      &lwi);
745
746                         if (unlikely(llmd->llmd_exit))
747                                 GOTO(cleanup1, rc = llmd->llmd_post_result);
748
749                         /* XXX: To be extended in other patch.
750                          *
751                          * Compare the OST side attribute with local attribute,
752                          * and fix it if found inconsistency. */
753
754                         spin_lock(&llmd->llmd_lock);
755                         llr = list_entry(llmd->llmd_req_list.next,
756                                          struct lfsck_layout_req,
757                                          llr_list);
758                         list_del_init(&llr->llr_list);
759                         if (bk->lb_async_windows != 0 &&
760                             llmd->llmd_prefetched >= bk->lb_async_windows)
761                                 wakeup = true;
762
763                         llmd->llmd_prefetched--;
764                         spin_unlock(&llmd->llmd_lock);
765                         if (wakeup)
766                                 wake_up_all(&mthread->t_ctl_waitq);
767
768                         lfsck_layout_req_fini(env, llr);
769                 }
770
771                 /* Wakeup the master engine if it is waiting in checkpoint. */
772                 if (atomic_read(&llmd->llmd_rpcs_in_flight) == 0)
773                         wake_up_all(&mthread->t_ctl_waitq);
774
775                 l_wait_event(athread->t_ctl_waitq,
776                              !lfsck_layout_req_empty(llmd) ||
777                              llmd->llmd_exit ||
778                              llmd->llmd_to_post ||
779                              llmd->llmd_to_double_scan,
780                              &lwi);
781
782                 if (unlikely(llmd->llmd_exit))
783                         GOTO(cleanup1, rc = llmd->llmd_post_result);
784
785                 if (!list_empty(&llmd->llmd_req_list))
786                         continue;
787
788                 if (llmd->llmd_to_post) {
789                         llmd->llmd_to_post = 0;
790                         LASSERT(llmd->llmd_post_result > 0);
791
792                         memset(lr, 0, sizeof(*lr));
793                         lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
794                         lr->lr_active = LT_LAYOUT;
795                         lr->lr_event = LE_PHASE1_DONE;
796                         lr->lr_status = llmd->llmd_post_result;
797                         rc = lfsck_layout_master_notify_others(env, com, lr);
798                         if (rc != 0)
799                                 CERROR("%s: failed to notify others "
800                                        "for layout post: rc = %d\n",
801                                        lfsck_lfsck2name(lfsck), rc);
802
803                         /* Wakeup the master engine to go ahead. */
804                         wake_up_all(&mthread->t_ctl_waitq);
805                 }
806
807                 if (llmd->llmd_to_double_scan) {
808                         llmd->llmd_to_double_scan = 0;
809                         atomic_inc(&lfsck->li_double_scan_count);
810                         llmd->llmd_in_double_scan = 1;
811                         wake_up_all(&mthread->t_ctl_waitq);
812
813                         while (llmd->llmd_in_double_scan) {
814                                 rc = lfsck_layout_master_query_others(env, com);
815                                 if (lfsck_layout_master_to_orphan(llmd))
816                                         goto orphan;
817
818                                 if (rc < 0)
819                                         GOTO(cleanup2, rc);
820
821                                 /* Pull LFSCK status on related targets once
822                                  * per 30 seconds if we are not notified. */
823                                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
824                                                            cfs_time_seconds(1),
825                                                            NULL, NULL);
826                                 rc = l_wait_event(athread->t_ctl_waitq,
827                                         lfsck_layout_master_to_orphan(llmd) ||
828                                         llmd->llmd_exit ||
829                                         !thread_is_running(mthread),
830                                         &lwi);
831
832                                 if (unlikely(llmd->llmd_exit ||
833                                              !thread_is_running(mthread)))
834                                         GOTO(cleanup2, rc = 0);
835
836                                 if (rc == -ETIMEDOUT)
837                                         continue;
838
839                                 if (rc < 0)
840                                         GOTO(cleanup2, rc);
841
842 orphan:
843                                 /* XXX: real double scan for ost orphans. */
844
845                                 GOTO(cleanup2, rc = 1);
846                         }
847                 }
848         }
849
850 cleanup1:
851         /* Cleanup the unfinished requests. */
852         spin_lock(&llmd->llmd_lock);
853         while (!list_empty(&llmd->llmd_req_list)) {
854                 llr = list_entry(llmd->llmd_req_list.next,
855                                  struct lfsck_layout_req,
856                                  llr_list);
857                 list_del_init(&llr->llr_list);
858                 llmd->llmd_prefetched--;
859                 spin_unlock(&llmd->llmd_lock);
860                 lfsck_layout_req_fini(env, llr);
861                 spin_lock(&llmd->llmd_lock);
862         }
863         spin_unlock(&llmd->llmd_lock);
864
865         LASSERTF(llmd->llmd_prefetched == 0, "unmatched prefeteched objs %d\n",
866                  llmd->llmd_prefetched);
867
868         l_wait_event(athread->t_ctl_waitq,
869                      atomic_read(&llmd->llmd_rpcs_in_flight) == 0,
870                      &lwi);
871
872 cleanup2:
873         memset(lr, 0, sizeof(*lr));
874         lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
875         lr->lr_active = LT_LAYOUT;
876         if (rc > 0) {
877                 lr->lr_event = LE_PHASE2_DONE;
878                 lr->lr_status = rc;
879         } else if (rc == 0) {
880                 lr->lr_event = LE_STOP;
881                 if (lfsck->li_status == LS_PAUSED ||
882                     lfsck->li_status == LS_CO_PAUSED)
883                         lr->lr_status = LS_CO_PAUSED;
884                 else if (lfsck->li_status == LS_STOPPED ||
885                          lfsck->li_status == LS_CO_STOPPED)
886                         lr->lr_status = LS_CO_STOPPED;
887                 else
888                         LBUG();
889         } else {
890                 lr->lr_event = LE_STOP;
891                 lr->lr_status = LS_CO_FAILED;
892         }
893
894         rc1 = lfsck_layout_master_notify_others(env, com, lr);
895         if (rc1 != 0) {
896                 CERROR("%s: failed to notify others for layout quit: rc = %d\n",
897                        lfsck_lfsck2name(lfsck), rc1);
898                 rc = rc1;
899         }
900
901         /* Under force exit case, some requests may be just freed without
902          * verification, those objects should be re-handled when next run.
903          * So not update the on-disk tracing file under such case. */
904         if (!llmd->llmd_exit)
905                 rc1 = lfsck_layout_double_scan_result(env, com, rc);
906
907 fini:
908         if (llmd->llmd_in_double_scan)
909                 atomic_dec(&lfsck->li_double_scan_count);
910
911         spin_lock(&llmd->llmd_lock);
912         llmd->llmd_assistant_status = (rc1 != 0 ? rc1 : rc);
913         thread_set_flags(athread, SVC_STOPPED);
914         wake_up_all(&mthread->t_ctl_waitq);
915         spin_unlock(&llmd->llmd_lock);
916         lfsck_thread_args_fini(lta);
917
918         return rc;
919 }
920
921 /* layout APIs */
922
923 static int lfsck_layout_reset(const struct lu_env *env,
924                               struct lfsck_component *com, bool init)
925 {
926         struct lfsck_layout     *lo    = com->lc_file_ram;
927         int                      rc;
928
929         down_write(&com->lc_sem);
930         if (init) {
931                 memset(lo, 0, com->lc_file_size);
932         } else {
933                 __u32 count = lo->ll_success_count;
934                 __u64 last_time = lo->ll_time_last_complete;
935
936                 memset(lo, 0, com->lc_file_size);
937                 lo->ll_success_count = count;
938                 lo->ll_time_last_complete = last_time;
939         }
940
941         lo->ll_magic = LFSCK_LAYOUT_MAGIC;
942         lo->ll_status = LS_INIT;
943
944         rc = lfsck_layout_store(env, com);
945         up_write(&com->lc_sem);
946
947         return rc;
948 }
949
950 static void lfsck_layout_fail(const struct lu_env *env,
951                               struct lfsck_component *com, bool new_checked)
952 {
953         struct lfsck_layout *lo = com->lc_file_ram;
954
955         down_write(&com->lc_sem);
956         if (new_checked)
957                 com->lc_new_checked++;
958         lo->ll_objs_failed_phase1++;
959         if (lo->ll_pos_first_inconsistent == 0) {
960                 struct lfsck_instance *lfsck = com->lc_lfsck;
961
962                 lo->ll_pos_first_inconsistent =
963                         lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
964                                                         lfsck->li_di_oit);
965         }
966         up_write(&com->lc_sem);
967 }
968
969 static int lfsck_layout_master_checkpoint(const struct lu_env *env,
970                                           struct lfsck_component *com, bool init)
971 {
972         struct lfsck_instance           *lfsck   = com->lc_lfsck;
973         struct lfsck_layout             *lo      = com->lc_file_ram;
974         struct lfsck_layout_master_data *llmd    = com->lc_data;
975         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
976         struct ptlrpc_thread            *athread = &llmd->llmd_thread;
977         struct l_wait_info               lwi     = { 0 };
978         int                              rc;
979
980         if (com->lc_new_checked == 0 && !init)
981                 return 0;
982
983         l_wait_event(mthread->t_ctl_waitq,
984                      (list_empty(&llmd->llmd_req_list) &&
985                       atomic_read(&llmd->llmd_rpcs_in_flight) == 0) ||
986                      !thread_is_running(mthread) ||
987                      thread_is_stopped(athread),
988                      &lwi);
989
990         if (!thread_is_running(mthread) || thread_is_stopped(athread))
991                 return 0;
992
993         down_write(&com->lc_sem);
994         if (init) {
995                 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
996         } else {
997                 lo->ll_pos_last_checkpoint =
998                                         lfsck->li_pos_current.lp_oit_cookie;
999                 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1000                                 HALF_SEC - lfsck->li_time_last_checkpoint);
1001                 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1002                 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1003                 com->lc_new_checked = 0;
1004         }
1005
1006         rc = lfsck_layout_store(env, com);
1007         up_write(&com->lc_sem);
1008
1009         return rc;
1010 }
1011
1012 static int lfsck_layout_slave_checkpoint(const struct lu_env *env,
1013                                          struct lfsck_component *com, bool init)
1014 {
1015         struct lfsck_instance   *lfsck = com->lc_lfsck;
1016         struct lfsck_layout     *lo    = com->lc_file_ram;
1017         int                      rc;
1018
1019         if (com->lc_new_checked == 0 && !init)
1020                 return 0;
1021
1022         down_write(&com->lc_sem);
1023
1024         if (init) {
1025                 lo->ll_pos_latest_start = lfsck->li_pos_current.lp_oit_cookie;
1026         } else {
1027                 lo->ll_pos_last_checkpoint =
1028                                         lfsck->li_pos_current.lp_oit_cookie;
1029                 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1030                                 HALF_SEC - lfsck->li_time_last_checkpoint);
1031                 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1032                 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1033                 com->lc_new_checked = 0;
1034         }
1035
1036         rc = lfsck_layout_store(env, com);
1037
1038         up_write(&com->lc_sem);
1039
1040         return rc;
1041 }
1042
1043 static int lfsck_layout_slave_prep(const struct lu_env *env,
1044                                    struct lfsck_component *com)
1045 {
1046         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1047         struct lfsck_layout     *lo     = com->lc_file_ram;
1048         struct lfsck_position   *pos    = &com->lc_pos_start;
1049
1050         /* XXX: For a new scanning, generate OST-objects
1051          *      bitmap for orphan detection. */
1052
1053         fid_zero(&pos->lp_dir_parent);
1054         pos->lp_dir_cookie = 0;
1055         if (lo->ll_status == LS_COMPLETED ||
1056             lo->ll_status == LS_PARTIAL) {
1057                 int rc;
1058
1059                 rc = lfsck_layout_reset(env, com, false);
1060                 if (rc != 0)
1061                         return rc;
1062         }
1063
1064         down_write(&com->lc_sem);
1065
1066         lo->ll_time_latest_start = cfs_time_current_sec();
1067
1068         spin_lock(&lfsck->li_lock);
1069         if (lo->ll_flags & LF_SCANNED_ONCE) {
1070                 if (!lfsck->li_drop_dryrun ||
1071                     lo->ll_pos_first_inconsistent == 0) {
1072                         lo->ll_status = LS_SCANNING_PHASE2;
1073                         list_del_init(&com->lc_link);
1074                         list_add_tail(&com->lc_link,
1075                                       &lfsck->li_list_double_scan);
1076                         pos->lp_oit_cookie = 0;
1077                 } else {
1078                         int i;
1079
1080                         lo->ll_status = LS_SCANNING_PHASE1;
1081                         lo->ll_run_time_phase1 = 0;
1082                         lo->ll_run_time_phase2 = 0;
1083                         lo->ll_objs_checked_phase1 = 0;
1084                         lo->ll_objs_checked_phase2 = 0;
1085                         lo->ll_objs_failed_phase1 = 0;
1086                         lo->ll_objs_failed_phase2 = 0;
1087                         for (i = 0; i < LLIT_MAX; i++)
1088                                 lo->ll_objs_repaired[i] = 0;
1089
1090                         pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1091                 }
1092         } else {
1093                 lo->ll_status = LS_SCANNING_PHASE1;
1094                 if (!lfsck->li_drop_dryrun ||
1095                     lo->ll_pos_first_inconsistent == 0)
1096                         pos->lp_oit_cookie = lo->ll_pos_last_checkpoint + 1;
1097                 else
1098                         pos->lp_oit_cookie = lo->ll_pos_first_inconsistent;
1099         }
1100         spin_unlock(&lfsck->li_lock);
1101
1102         up_write(&com->lc_sem);
1103
1104         return 0;
1105 }
1106
1107 static int lfsck_layout_master_prep(const struct lu_env *env,
1108                                     struct lfsck_component *com)
1109 {
1110         struct lfsck_instance           *lfsck   = com->lc_lfsck;
1111         struct lfsck_layout_master_data *llmd    = com->lc_data;
1112         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
1113         struct ptlrpc_thread            *athread = &llmd->llmd_thread;
1114         struct lfsck_thread_args        *lta;
1115         long                             rc;
1116         ENTRY;
1117
1118         rc = lfsck_layout_slave_prep(env, com);
1119         if (rc != 0)
1120                 RETURN(rc);
1121
1122         llmd->llmd_assistant_status = 0;
1123         llmd->llmd_post_result = 0;
1124         llmd->llmd_to_post = 0;
1125         llmd->llmd_to_double_scan = 0;
1126         llmd->llmd_in_double_scan = 0;
1127         llmd->llmd_exit = 0;
1128         thread_set_flags(athread, 0);
1129
1130         lta = lfsck_thread_args_init(lfsck, com);
1131         if (IS_ERR(lta))
1132                 RETURN(PTR_ERR(lta));
1133
1134         rc = PTR_ERR(kthread_run(lfsck_layout_assistant, lta, "lfsck_layout"));
1135         if (IS_ERR_VALUE(rc)) {
1136                 CERROR("%s: Cannot start LFSCK layout assistant thread: "
1137                        "rc = %ld\n", lfsck_lfsck2name(lfsck), rc);
1138                 lfsck_thread_args_fini(lta);
1139         } else {
1140                 struct l_wait_info lwi = { 0 };
1141
1142                 l_wait_event(mthread->t_ctl_waitq,
1143                              thread_is_running(athread) ||
1144                              thread_is_stopped(athread),
1145                              &lwi);
1146                 if (unlikely(!thread_is_running(athread)))
1147                         rc = llmd->llmd_assistant_status;
1148                 else
1149                         rc = 0;
1150         }
1151
1152         RETURN(rc);
1153 }
1154
1155 static int lfsck_layout_master_exec_oit(const struct lu_env *env,
1156                                         struct lfsck_component *com,
1157                                         struct dt_object *obj)
1158 {
1159         /* XXX: To be implemented in other patches.
1160          *
1161          * For the given object, read its layout EA locally. For each stripe,
1162          * pre-fetch the OST-object's attribute and generate an structure
1163          * lfsck_layout_req on the list ::llmd_req_list.
1164          *
1165          * For each request on the ::llmd_req_list, the lfsck_layout_assistant
1166          * thread will compare the OST side attribute with local attribute,
1167          * if inconsistent, then repair it.
1168          *
1169          * All above processing is async mode with pipeline. */
1170
1171         return 0;
1172 }
1173
1174 static int lfsck_layout_slave_exec_oit(const struct lu_env *env,
1175                                        struct lfsck_component *com,
1176                                        struct dt_object *obj)
1177 {
1178         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1179         struct lfsck_layout             *lo     = com->lc_file_ram;
1180         const struct lu_fid             *fid    = lfsck_dto2fid(obj);
1181         struct lfsck_layout_slave_data  *llsd   = com->lc_data;
1182         struct lfsck_layout_seq         *lls;
1183         __u64                            seq;
1184         __u64                            oid;
1185         int                              rc;
1186         ENTRY;
1187
1188         /* XXX: Update OST-objects bitmap for orphan detection. */
1189
1190         LASSERT(llsd != NULL);
1191
1192         down_write(&com->lc_sem);
1193         if (fid_is_idif(fid))
1194                 seq = 0;
1195         else if (!fid_is_norm(fid) ||
1196                  !fid_is_for_ostobj(env, lfsck->li_next, obj, fid))
1197                 GOTO(unlock, rc = 0);
1198         else
1199                 seq = fid_seq(fid);
1200         com->lc_new_checked++;
1201
1202         lls = lfsck_layout_seq_lookup(llsd, seq);
1203         if (lls == NULL) {
1204                 OBD_ALLOC_PTR(lls);
1205                 if (unlikely(lls == NULL))
1206                         GOTO(unlock, rc = -ENOMEM);
1207
1208                 INIT_LIST_HEAD(&lls->lls_list);
1209                 lls->lls_seq = seq;
1210                 rc = lfsck_layout_lastid_load(env, com, lls);
1211                 if (rc != 0) {
1212                         lo->ll_objs_failed_phase1++;
1213                         OBD_FREE_PTR(lls);
1214                         GOTO(unlock, rc);
1215                 }
1216
1217                 lfsck_layout_seq_insert(llsd, lls);
1218         }
1219
1220         if (unlikely(fid_is_last_id(fid)))
1221                 GOTO(unlock, rc = 0);
1222
1223         oid = fid_oid(fid);
1224         if (oid > lls->lls_lastid_known)
1225                 lls->lls_lastid_known = oid;
1226
1227         if (oid > lls->lls_lastid) {
1228                 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1229                         /* OFD may create new objects during LFSCK scanning. */
1230                         rc = lfsck_layout_lastid_reload(env, com, lls);
1231                         if (unlikely(rc != 0))
1232                                 CWARN("%s: failed to reload LAST_ID for "LPX64
1233                                       ": rc = %d\n",
1234                                       lfsck_lfsck2name(com->lc_lfsck),
1235                                       lls->lls_seq, rc);
1236                         if (oid <= lls->lls_lastid)
1237                                 GOTO(unlock, rc = 0);
1238
1239                         LASSERT(lfsck->li_out_notify != NULL);
1240
1241                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1242                                              LE_LASTID_REBUILDING);
1243                         lo->ll_flags |= LF_CRASHED_LASTID;
1244                 }
1245
1246                 lls->lls_lastid = oid;
1247                 lls->lls_dirty = 1;
1248         }
1249
1250         GOTO(unlock, rc = 0);
1251
1252 unlock:
1253         up_write(&com->lc_sem);
1254
1255         return rc;
1256 }
1257
1258 static int lfsck_layout_exec_dir(const struct lu_env *env,
1259                                  struct lfsck_component *com,
1260                                  struct dt_object *obj,
1261                                  struct lu_dirent *ent)
1262 {
1263         return 0;
1264 }
1265
1266 static int lfsck_layout_master_post(const struct lu_env *env,
1267                                     struct lfsck_component *com,
1268                                     int result, bool init)
1269 {
1270         struct lfsck_instance           *lfsck   = com->lc_lfsck;
1271         struct lfsck_layout             *lo      = com->lc_file_ram;
1272         struct lfsck_layout_master_data *llmd    = com->lc_data;
1273         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
1274         struct ptlrpc_thread            *athread = &llmd->llmd_thread;
1275         struct l_wait_info               lwi     = { 0 };
1276         int                              rc;
1277         ENTRY;
1278
1279
1280         llmd->llmd_post_result = result;
1281         llmd->llmd_to_post = 1;
1282         if (llmd->llmd_post_result <= 0)
1283                 llmd->llmd_exit = 1;
1284
1285         wake_up_all(&athread->t_ctl_waitq);
1286         l_wait_event(mthread->t_ctl_waitq,
1287                      (result > 0 && list_empty(&llmd->llmd_req_list) &&
1288                       atomic_read(&llmd->llmd_rpcs_in_flight) == 0) ||
1289                      thread_is_stopped(athread),
1290                      &lwi);
1291
1292         if (llmd->llmd_assistant_status < 0)
1293                 result = llmd->llmd_assistant_status;
1294
1295         down_write(&com->lc_sem);
1296         spin_lock(&lfsck->li_lock);
1297         /* When LFSCK failed, there may be some prefetched objects those are
1298          * not been processed yet, we do not know the exactly position, then
1299          * just restart from last check-point next time. */
1300         if (!init && !llmd->llmd_exit)
1301                 lo->ll_pos_last_checkpoint =
1302                                         lfsck->li_pos_current.lp_oit_cookie;
1303
1304         if (result > 0) {
1305                 lo->ll_status = LS_SCANNING_PHASE2;
1306                 lo->ll_flags |= LF_SCANNED_ONCE;
1307                 lo->ll_flags &= ~LF_UPGRADE;
1308                 list_del_init(&com->lc_link);
1309                 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1310         } else if (result == 0) {
1311                 lo->ll_status = lfsck->li_status;
1312                 if (lo->ll_status == 0)
1313                         lo->ll_status = LS_STOPPED;
1314                 if (lo->ll_status != LS_PAUSED) {
1315                         list_del_init(&com->lc_link);
1316                         list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1317                 }
1318         } else {
1319                 lo->ll_status = LS_FAILED;
1320                 list_del_init(&com->lc_link);
1321                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1322         }
1323         spin_unlock(&lfsck->li_lock);
1324
1325         if (!init) {
1326                 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1327                                 HALF_SEC - lfsck->li_time_last_checkpoint);
1328                 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1329                 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1330                 com->lc_new_checked = 0;
1331         }
1332
1333         rc = lfsck_layout_store(env, com);
1334         up_write(&com->lc_sem);
1335
1336         RETURN(rc);
1337 }
1338
1339 static int lfsck_layout_slave_post(const struct lu_env *env,
1340                                    struct lfsck_component *com,
1341                                    int result, bool init)
1342 {
1343         struct lfsck_instance   *lfsck = com->lc_lfsck;
1344         struct lfsck_layout     *lo    = com->lc_file_ram;
1345         int                      rc;
1346         bool                     done  = false;
1347
1348         rc = lfsck_layout_lastid_store(env, com);
1349         if (rc != 0)
1350                 result = rc;
1351
1352         LASSERT(lfsck->li_out_notify != NULL);
1353
1354         down_write(&com->lc_sem);
1355
1356         spin_lock(&lfsck->li_lock);
1357         if (!init)
1358                 lo->ll_pos_last_checkpoint =
1359                                         lfsck->li_pos_current.lp_oit_cookie;
1360         if (result > 0) {
1361                 lo->ll_status = LS_SCANNING_PHASE2;
1362                 lo->ll_flags |= LF_SCANNED_ONCE;
1363                 if (lo->ll_flags & LF_CRASHED_LASTID) {
1364                         done = true;
1365                         lo->ll_flags &= ~LF_CRASHED_LASTID;
1366                 }
1367                 lo->ll_flags &= ~LF_UPGRADE;
1368                 list_del_init(&com->lc_link);
1369                 list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1370         } else if (result == 0) {
1371                 lo->ll_status = lfsck->li_status;
1372                 if (lo->ll_status == 0)
1373                         lo->ll_status = LS_STOPPED;
1374                 if (lo->ll_status != LS_PAUSED) {
1375                         list_del_init(&com->lc_link);
1376                         list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1377                 }
1378         } else {
1379                 lo->ll_status = LS_FAILED;
1380                 list_del_init(&com->lc_link);
1381                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1382         }
1383         spin_unlock(&lfsck->li_lock);
1384
1385         if (done)
1386                 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1387                                      LE_LASTID_REBUILT);
1388
1389         if (!init) {
1390                 lo->ll_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1391                                 HALF_SEC - lfsck->li_time_last_checkpoint);
1392                 lo->ll_time_last_checkpoint = cfs_time_current_sec();
1393                 lo->ll_objs_checked_phase1 += com->lc_new_checked;
1394                 com->lc_new_checked = 0;
1395         }
1396
1397         rc = lfsck_layout_store(env, com);
1398
1399         up_write(&com->lc_sem);
1400
1401         return rc;
1402 }
1403
1404 static int lfsck_layout_dump(const struct lu_env *env,
1405                              struct lfsck_component *com, char *buf, int len)
1406 {
1407         struct lfsck_instance   *lfsck = com->lc_lfsck;
1408         struct lfsck_bookmark   *bk    = &lfsck->li_bookmark_ram;
1409         struct lfsck_layout     *lo    = com->lc_file_ram;
1410         int                      save  = len;
1411         int                      ret   = -ENOSPC;
1412         int                      rc;
1413
1414         down_read(&com->lc_sem);
1415         rc = snprintf(buf, len,
1416                       "name: lfsck_layout\n"
1417                       "magic: %#x\n"
1418                       "version: %d\n"
1419                       "status: %s\n",
1420                       lo->ll_magic,
1421                       bk->lb_version,
1422                       lfsck_status2names(lo->ll_status));
1423         if (rc <= 0)
1424                 goto out;
1425
1426         buf += rc;
1427         len -= rc;
1428         rc = lfsck_bits_dump(&buf, &len, lo->ll_flags, lfsck_flags_names,
1429                              "flags");
1430         if (rc < 0)
1431                 goto out;
1432
1433         rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1434                              "param");
1435         if (rc < 0)
1436                 goto out;
1437
1438         rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_complete,
1439                              "time_since_last_completed");
1440         if (rc < 0)
1441                 goto out;
1442
1443         rc = lfsck_time_dump(&buf, &len, lo->ll_time_latest_start,
1444                              "time_since_latest_start");
1445         if (rc < 0)
1446                 goto out;
1447
1448         rc = lfsck_time_dump(&buf, &len, lo->ll_time_last_checkpoint,
1449                              "time_since_last_checkpoint");
1450         if (rc < 0)
1451                 goto out;
1452
1453         rc = snprintf(buf, len,
1454                       "latest_start_position: "LPU64"\n"
1455                       "last_checkpoint_position: "LPU64"\n"
1456                       "first_failure_position: "LPU64"\n",
1457                       lo->ll_pos_latest_start,
1458                       lo->ll_pos_last_checkpoint,
1459                       lo->ll_pos_first_inconsistent);
1460         if (rc <= 0)
1461                 goto out;
1462
1463         buf += rc;
1464         len -= rc;
1465
1466         rc = snprintf(buf, len,
1467                       "success_count: %u\n"
1468                       "repaired_dangling: "LPU64"\n"
1469                       "repaired_unmatched_pair: "LPU64"\n"
1470                       "repaired_multiple_referenced: "LPU64"\n"
1471                       "repaired_orphan: "LPU64"\n"
1472                       "repaired_inconsistent_owner: "LPU64"\n"
1473                       "repaired_others: "LPU64"\n"
1474                       "skipped: "LPU64"\n"
1475                       "failed_phase1: "LPU64"\n"
1476                       "failed_phase2: "LPU64"\n",
1477                       lo->ll_success_count,
1478                       lo->ll_objs_repaired[LLIT_DANGLING - 1],
1479                       lo->ll_objs_repaired[LLIT_UNMATCHED_PAIR - 1],
1480                       lo->ll_objs_repaired[LLIT_MULTIPLE_REFERENCED - 1],
1481                       lo->ll_objs_repaired[LLIT_ORPHAN - 1],
1482                       lo->ll_objs_repaired[LLIT_INCONSISTENT_OWNER - 1],
1483                       lo->ll_objs_repaired[LLIT_OTHERS - 1],
1484                       lo->ll_objs_skipped,
1485                       lo->ll_objs_failed_phase1,
1486                       lo->ll_objs_failed_phase2);
1487         if (rc <= 0)
1488                 goto out;
1489
1490         buf += rc;
1491         len -= rc;
1492
1493         if (lo->ll_status == LS_SCANNING_PHASE1) {
1494                 __u64 pos;
1495                 const struct dt_it_ops *iops;
1496                 cfs_duration_t duration = cfs_time_current() -
1497                                           lfsck->li_time_last_checkpoint;
1498                 __u64 checked = lo->ll_objs_checked_phase1 + com->lc_new_checked;
1499                 __u64 speed = checked;
1500                 __u64 new_checked = com->lc_new_checked * HZ;
1501                 __u32 rtime = lo->ll_run_time_phase1 +
1502                               cfs_duration_sec(duration + HALF_SEC);
1503
1504                 if (duration != 0)
1505                         do_div(new_checked, duration);
1506                 if (rtime != 0)
1507                         do_div(speed, rtime);
1508                 rc = snprintf(buf, len,
1509                               "checked_phase1: "LPU64"\n"
1510                               "checked_phase2: "LPU64"\n"
1511                               "run_time_phase1: %u seconds\n"
1512                               "run_time_phase2: %u seconds\n"
1513                               "average_speed_phase1: "LPU64" items/sec\n"
1514                               "average_speed_phase2: N/A\n"
1515                               "real-time_speed_phase1: "LPU64" items/sec\n"
1516                               "real-time_speed_phase2: N/A\n",
1517                               checked,
1518                               lo->ll_objs_checked_phase2,
1519                               rtime,
1520                               lo->ll_run_time_phase2,
1521                               speed,
1522                               new_checked);
1523                 if (rc <= 0)
1524                         goto out;
1525
1526                 buf += rc;
1527                 len -= rc;
1528
1529                 LASSERT(lfsck->li_di_oit != NULL);
1530
1531                 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1532
1533                 /* The low layer otable-based iteration position may NOT
1534                  * exactly match the layout-based directory traversal
1535                  * cookie. Generally, it is not a serious issue. But the
1536                  * caller should NOT make assumption on that. */
1537                 pos = iops->store(env, lfsck->li_di_oit);
1538                 if (!lfsck->li_current_oit_processed)
1539                         pos--;
1540                 rc = snprintf(buf, len, "current_position: "LPU64"\n", pos);
1541                 if (rc <= 0)
1542                         goto out;
1543
1544                 buf += rc;
1545                 len -= rc;
1546         } else {
1547                 /* XXX: LS_SCANNING_PHASE2 will be handled in the future. */
1548                 __u64 speed1 = lo->ll_objs_checked_phase1;
1549                 __u64 speed2 = lo->ll_objs_checked_phase2;
1550
1551                 if (lo->ll_run_time_phase1 != 0)
1552                         do_div(speed1, lo->ll_run_time_phase1);
1553                 if (lo->ll_run_time_phase2 != 0)
1554                         do_div(speed2, lo->ll_run_time_phase2);
1555                 rc = snprintf(buf, len,
1556                               "checked_phase1: "LPU64"\n"
1557                               "checked_phase2: "LPU64"\n"
1558                               "run_time_phase1: %u seconds\n"
1559                               "run_time_phase2: %u seconds\n"
1560                               "average_speed_phase1: "LPU64" items/sec\n"
1561                               "average_speed_phase2: "LPU64" objs/sec\n"
1562                               "real-time_speed_phase1: N/A\n"
1563                               "real-time_speed_phase2: N/A\n"
1564                               "current_position: N/A\n",
1565                               lo->ll_objs_checked_phase1,
1566                               lo->ll_objs_checked_phase2,
1567                               lo->ll_run_time_phase1,
1568                               lo->ll_run_time_phase2,
1569                               speed1,
1570                               speed2);
1571                 if (rc <= 0)
1572                         goto out;
1573
1574                 buf += rc;
1575                 len -= rc;
1576         }
1577         ret = save - len;
1578
1579 out:
1580         up_read(&com->lc_sem);
1581
1582         return ret;
1583 }
1584
1585 static int lfsck_layout_master_double_scan(const struct lu_env *env,
1586                                            struct lfsck_component *com)
1587 {
1588         struct lfsck_layout_master_data *llmd    = com->lc_data;
1589         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
1590         struct ptlrpc_thread            *athread = &llmd->llmd_thread;
1591         struct lfsck_layout             *lo      = com->lc_file_ram;
1592         struct l_wait_info               lwi     = { 0 };
1593
1594         if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1595                 return 0;
1596
1597         llmd->llmd_to_double_scan = 1;
1598         wake_up_all(&athread->t_ctl_waitq);
1599         l_wait_event(mthread->t_ctl_waitq,
1600                      llmd->llmd_in_double_scan ||
1601                      thread_is_stopped(athread),
1602                      &lwi);
1603         if (llmd->llmd_assistant_status < 0)
1604                 return llmd->llmd_assistant_status;
1605
1606         return 0;
1607 }
1608
1609 static int lfsck_layout_slave_double_scan(const struct lu_env *env,
1610                                           struct lfsck_component *com)
1611 {
1612         struct lfsck_instance   *lfsck = com->lc_lfsck;
1613         struct lfsck_layout     *lo    = com->lc_file_ram;
1614         int                      rc    = 1;
1615
1616         if (unlikely(lo->ll_status != LS_SCANNING_PHASE2))
1617                 return 0;
1618
1619         atomic_inc(&lfsck->li_double_scan_count);
1620
1621         com->lc_new_checked = 0;
1622         com->lc_new_scanned = 0;
1623         com->lc_time_last_checkpoint = cfs_time_current();
1624         com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
1625                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1626
1627         rc = lfsck_layout_double_scan_result(env, com, rc);
1628
1629         if (atomic_dec_and_test(&lfsck->li_double_scan_count))
1630                 wake_up_all(&lfsck->li_thread.t_ctl_waitq);
1631
1632         return rc;
1633 }
1634
1635 static void lfsck_layout_master_data_release(const struct lu_env *env,
1636                                              struct lfsck_component *com)
1637 {
1638         struct lfsck_layout_master_data *llmd = com->lc_data;
1639
1640         LASSERT(llmd != NULL);
1641         LASSERT(thread_is_init(&llmd->llmd_thread) ||
1642                 thread_is_stopped(&llmd->llmd_thread));
1643         LASSERT(list_empty(&llmd->llmd_req_list));
1644         LASSERT(atomic_read(&llmd->llmd_rpcs_in_flight) == 0);
1645
1646         com->lc_data = NULL;
1647         OBD_FREE_PTR(llmd);
1648 }
1649
1650 static void lfsck_layout_slave_data_release(const struct lu_env *env,
1651                                             struct lfsck_component *com)
1652 {
1653         struct lfsck_layout_slave_data  *llsd   = com->lc_data;
1654         struct lfsck_layout_seq         *lls;
1655         struct lfsck_layout_seq         *next;
1656
1657         LASSERT(llsd != NULL);
1658
1659         com->lc_data = NULL;
1660
1661         list_for_each_entry_safe(lls, next, &llsd->llsd_seq_list,
1662                                      lls_list) {
1663                 list_del_init(&lls->lls_list);
1664                 lfsck_object_put(env, lls->lls_lastid_obj);
1665                 OBD_FREE_PTR(lls);
1666         }
1667
1668         OBD_FREE_PTR(llsd);
1669 }
1670
1671 static void lfsck_layout_master_quit(const struct lu_env *env,
1672                                      struct lfsck_component *com)
1673 {
1674         struct lfsck_layout_master_data *llmd    = com->lc_data;
1675         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
1676         struct ptlrpc_thread            *athread = &llmd->llmd_thread;
1677         struct l_wait_info               lwi     = { 0 };
1678
1679         llmd->llmd_exit = 1;
1680         wake_up_all(&athread->t_ctl_waitq);
1681         l_wait_event(mthread->t_ctl_waitq,
1682                      thread_is_init(athread) ||
1683                      thread_is_stopped(athread),
1684                      &lwi);
1685 }
1686
1687 static int lfsck_layout_master_in_notify(const struct lu_env *env,
1688                                          struct lfsck_component *com,
1689                                          struct lfsck_request *lr)
1690 {
1691         /* XXX: to record the event from layout slave on the OST. */
1692         return 0;
1693 }
1694
1695 static int lfsck_layout_slave_in_notify(const struct lu_env *env,
1696                                         struct lfsck_component *com,
1697                                         struct lfsck_request *lr)
1698 {
1699         /* XXX: to record the event from layout master on the MDT. */
1700         return 0;
1701 }
1702
1703 static int lfsck_layout_query(const struct lu_env *env,
1704                               struct lfsck_component *com)
1705 {
1706         struct lfsck_layout *lo = com->lc_file_ram;
1707
1708         return lo->ll_status;
1709 }
1710
1711 static struct lfsck_operations lfsck_layout_master_ops = {
1712         .lfsck_reset            = lfsck_layout_reset,
1713         .lfsck_fail             = lfsck_layout_fail,
1714         .lfsck_checkpoint       = lfsck_layout_master_checkpoint,
1715         .lfsck_prep             = lfsck_layout_master_prep,
1716         .lfsck_exec_oit         = lfsck_layout_master_exec_oit,
1717         .lfsck_exec_dir         = lfsck_layout_exec_dir,
1718         .lfsck_post             = lfsck_layout_master_post,
1719         .lfsck_dump             = lfsck_layout_dump,
1720         .lfsck_double_scan      = lfsck_layout_master_double_scan,
1721         .lfsck_data_release     = lfsck_layout_master_data_release,
1722         .lfsck_quit             = lfsck_layout_master_quit,
1723         .lfsck_in_notify        = lfsck_layout_master_in_notify,
1724         .lfsck_query            = lfsck_layout_query,
1725 };
1726
1727 static struct lfsck_operations lfsck_layout_slave_ops = {
1728         .lfsck_reset            = lfsck_layout_reset,
1729         .lfsck_fail             = lfsck_layout_fail,
1730         .lfsck_checkpoint       = lfsck_layout_slave_checkpoint,
1731         .lfsck_prep             = lfsck_layout_slave_prep,
1732         .lfsck_exec_oit         = lfsck_layout_slave_exec_oit,
1733         .lfsck_exec_dir         = lfsck_layout_exec_dir,
1734         .lfsck_post             = lfsck_layout_slave_post,
1735         .lfsck_dump             = lfsck_layout_dump,
1736         .lfsck_double_scan      = lfsck_layout_slave_double_scan,
1737         .lfsck_data_release     = lfsck_layout_slave_data_release,
1738         .lfsck_in_notify        = lfsck_layout_slave_in_notify,
1739         .lfsck_query            = lfsck_layout_query,
1740 };
1741
1742 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck)
1743 {
1744         struct lfsck_component  *com;
1745         struct lfsck_layout     *lo;
1746         struct dt_object        *root = NULL;
1747         struct dt_object        *obj;
1748         int                      rc;
1749         ENTRY;
1750
1751         OBD_ALLOC_PTR(com);
1752         if (com == NULL)
1753                 RETURN(-ENOMEM);
1754
1755         INIT_LIST_HEAD(&com->lc_link);
1756         INIT_LIST_HEAD(&com->lc_link_dir);
1757         init_rwsem(&com->lc_sem);
1758         atomic_set(&com->lc_ref, 1);
1759         com->lc_lfsck = lfsck;
1760         com->lc_type = LT_LAYOUT;
1761         if (lfsck->li_master) {
1762                 struct lfsck_layout_master_data *llmd;
1763
1764                 com->lc_ops = &lfsck_layout_master_ops;
1765                 OBD_ALLOC_PTR(llmd);
1766                 if (llmd == NULL)
1767                         GOTO(out, rc = -ENOMEM);
1768
1769                 INIT_LIST_HEAD(&llmd->llmd_req_list);
1770                 spin_lock_init(&llmd->llmd_lock);
1771                 init_waitqueue_head(&llmd->llmd_thread.t_ctl_waitq);
1772                 atomic_set(&llmd->llmd_rpcs_in_flight, 0);
1773                 com->lc_data = llmd;
1774         } else {
1775                 struct lfsck_layout_slave_data *llsd;
1776
1777                 com->lc_ops = &lfsck_layout_slave_ops;
1778                 OBD_ALLOC_PTR(llsd);
1779                 if (llsd == NULL)
1780                         GOTO(out, rc = -ENOMEM);
1781
1782                 INIT_LIST_HEAD(&llsd->llsd_seq_list);
1783                 com->lc_data = llsd;
1784         }
1785         com->lc_file_size = sizeof(*lo);
1786         OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1787         if (com->lc_file_ram == NULL)
1788                 GOTO(out, rc = -ENOMEM);
1789
1790         OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1791         if (com->lc_file_disk == NULL)
1792                 GOTO(out, rc = -ENOMEM);
1793
1794         root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1795         if (IS_ERR(root))
1796                 GOTO(out, rc = PTR_ERR(root));
1797
1798         if (unlikely(!dt_try_as_dir(env, root)))
1799                 GOTO(out, rc = -ENOTDIR);
1800
1801         obj = local_file_find_or_create(env, lfsck->li_los, root,
1802                                         lfsck_layout_name,
1803                                         S_IFREG | S_IRUGO | S_IWUSR);
1804         if (IS_ERR(obj))
1805                 GOTO(out, rc = PTR_ERR(obj));
1806
1807         com->lc_obj = obj;
1808         rc = lfsck_layout_load(env, com);
1809         if (rc > 0)
1810                 rc = lfsck_layout_reset(env, com, true);
1811         else if (rc == -ENOENT)
1812                 rc = lfsck_layout_init(env, com);
1813
1814         if (rc != 0)
1815                 GOTO(out, rc);
1816
1817         lo = com->lc_file_ram;
1818         switch (lo->ll_status) {
1819         case LS_INIT:
1820         case LS_COMPLETED:
1821         case LS_FAILED:
1822         case LS_STOPPED:
1823         case LS_PARTIAL:
1824                 spin_lock(&lfsck->li_lock);
1825                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1826                 spin_unlock(&lfsck->li_lock);
1827                 break;
1828         default:
1829                 CERROR("%s: unknown lfsck_layout status: rc = %u\n",
1830                        lfsck_lfsck2name(lfsck), lo->ll_status);
1831                 /* fall through */
1832         case LS_SCANNING_PHASE1:
1833         case LS_SCANNING_PHASE2:
1834                 /* No need to store the status to disk right now.
1835                  * If the system crashed before the status stored,
1836                  * it will be loaded back when next time. */
1837                 lo->ll_status = LS_CRASHED;
1838                 lo->ll_flags |= LF_INCOMPLETE;
1839                 /* fall through */
1840         case LS_PAUSED:
1841         case LS_CRASHED:
1842         case LS_CO_FAILED:
1843         case LS_CO_STOPPED:
1844         case LS_CO_PAUSED:
1845                 spin_lock(&lfsck->li_lock);
1846                 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1847                 spin_unlock(&lfsck->li_lock);
1848                 break;
1849         }
1850
1851         if (lo->ll_flags & LF_CRASHED_LASTID) {
1852                 LASSERT(lfsck->li_out_notify != NULL);
1853
1854                 lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1855                                      LE_LASTID_REBUILDING);
1856         }
1857
1858         GOTO(out, rc = 0);
1859
1860 out:
1861         if (root != NULL && !IS_ERR(root))
1862                 lu_object_put(env, &root->do_lu);
1863
1864         if (rc != 0)
1865                 lfsck_component_cleanup(env, com);
1866
1867         return rc;
1868 }