Whamcloud - gitweb
LU-2915 lfsck: NO scrub for agent inode and remote parent
[fs/lustre-release.git] / lustre / mdd / mdd_lfsck.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) 2012, 2013, Intel Corporation.
24  */
25 /*
26  * lustre/mdd/mdd_lfsck.c
27  *
28  * Top-level entry points into mdd module
29  *
30  * LFSCK controller, which scans the whole device through low layer
31  * iteration APIs, drives all lfsck compeonents, controls the speed.
32  *
33  * Author: Fan Yong <yong.fan@whamcloud.com>
34  */
35
36 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <lustre/lustre_idl.h>
42 #include <lustre_fid.h>
43 #include <obd_support.h>
44
45 #include "mdd_internal.h"
46 #include "mdd_lfsck.h"
47
48 #define HALF_SEC                        (CFS_HZ >> 1)
49 #define LFSCK_CHECKPOINT_INTERVAL       60
50
51 #define LFSCK_NAMEENTRY_DEAD            1 /* The object has been unlinked. */
52 #define LFSCK_NAMEENTRY_REMOVED         2 /* The entry has been removed. */
53 #define LFSCK_NAMEENTRY_RECREATED       3 /* The entry has been recreated. */
54
55 const char lfsck_bookmark_name[] = "lfsck_bookmark";
56 const char lfsck_namespace_name[] = "lfsck_namespace";
57
58 static const char *lfsck_status_names[] = {
59         "init",
60         "scanning-phase1",
61         "scanning-phase2",
62         "completed",
63         "failed",
64         "stopped",
65         "paused",
66         "crashed",
67         NULL
68 };
69
70 static const char *lfsck_flags_names[] = {
71         "scanned-once",
72         "inconsistent",
73         "upgrade",
74         NULL
75 };
76
77 static const char *lfsck_param_names[] = {
78         "failout",
79         "dryrun",
80         NULL
81 };
82
83 /* misc functions */
84
85 static inline struct mdd_device *mdd_lfsck2mdd(struct md_lfsck *lfsck)
86 {
87         return container_of0(lfsck, struct mdd_device, mdd_lfsck);
88 }
89
90 static inline char *mdd_lfsck2name(struct md_lfsck *lfsck)
91 {
92         struct mdd_device *mdd = mdd_lfsck2mdd(lfsck);
93
94         return mdd2obd_dev(mdd)->obd_name;
95 }
96
97 static inline void mdd_lfsck_component_get(struct lfsck_component *com)
98 {
99         atomic_inc(&com->lc_ref);
100 }
101
102 static inline void mdd_lfsck_component_put(const struct lu_env *env,
103                                            struct lfsck_component *com)
104 {
105         if (atomic_dec_and_test(&com->lc_ref)) {
106                 if (com->lc_obj != NULL)
107                         lu_object_put(env, &com->lc_obj->do_lu);
108                 if (com->lc_file_ram != NULL)
109                         OBD_FREE(com->lc_file_ram, com->lc_file_size);
110                 if (com->lc_file_disk != NULL)
111                         OBD_FREE(com->lc_file_disk, com->lc_file_size);
112                 OBD_FREE_PTR(com);
113         }
114 }
115
116 static inline struct lfsck_component *
117 __mdd_lfsck_component_find(struct md_lfsck *lfsck, __u16 type, cfs_list_t *list)
118 {
119         struct lfsck_component *com;
120
121         cfs_list_for_each_entry(com, list, lc_link) {
122                 if (com->lc_type == type)
123                         return com;
124         }
125         return NULL;
126 }
127
128 static struct lfsck_component *
129 mdd_lfsck_component_find(struct md_lfsck *lfsck, __u16 type)
130 {
131         struct lfsck_component *com;
132
133         spin_lock(&lfsck->ml_lock);
134         com = __mdd_lfsck_component_find(lfsck, type, &lfsck->ml_list_scan);
135         if (com != NULL)
136                 goto unlock;
137
138         com = __mdd_lfsck_component_find(lfsck, type,
139                                          &lfsck->ml_list_double_scan);
140         if (com != NULL)
141                 goto unlock;
142
143         com = __mdd_lfsck_component_find(lfsck, type, &lfsck->ml_list_idle);
144
145 unlock:
146         if (com != NULL)
147                 mdd_lfsck_component_get(com);
148         spin_unlock(&lfsck->ml_lock);
149         return com;
150 }
151
152 static void mdd_lfsck_component_cleanup(const struct lu_env *env,
153                                         struct lfsck_component *com)
154 {
155         if (!cfs_list_empty(&com->lc_link))
156                 cfs_list_del_init(&com->lc_link);
157         if (!cfs_list_empty(&com->lc_link_dir))
158                 cfs_list_del_init(&com->lc_link_dir);
159
160         mdd_lfsck_component_put(env, com);
161 }
162
163 static int lfsck_bits_dump(char **buf, int *len, int bits, const char *names[],
164                            const char *prefix)
165 {
166         int save = *len;
167         int flag;
168         int rc;
169         int i;
170
171         rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
172         if (rc <= 0)
173                 return -ENOSPC;
174
175         *buf += rc;
176         *len -= rc;
177         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
178                 if (flag & bits) {
179                         bits &= ~flag;
180                         rc = snprintf(*buf, *len, "%s%c", names[i],
181                                       bits != 0 ? ',' : '\n');
182                         if (rc <= 0)
183                                 return -ENOSPC;
184
185                         *buf += rc;
186                         *len -= rc;
187                 }
188         }
189         return save - *len;
190 }
191
192 static int lfsck_time_dump(char **buf, int *len, __u64 time, const char *prefix)
193 {
194         int rc;
195
196         if (time != 0)
197                 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
198                               cfs_time_current_sec() - time);
199         else
200                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
201         if (rc <= 0)
202                 return -ENOSPC;
203
204         *buf += rc;
205         *len -= rc;
206         return rc;
207 }
208
209 static int lfsck_pos_dump(char **buf, int *len, struct lfsck_position *pos,
210                           const char *prefix)
211 {
212         int rc;
213
214         if (fid_is_zero(&pos->lp_dir_parent)) {
215                 if (pos->lp_oit_cookie == 0)
216                         rc = snprintf(*buf, *len, "%s: N/A, N/A, N/A\n",
217                                       prefix);
218                 else
219                         rc = snprintf(*buf, *len, "%s: "LPU64", N/A, N/A\n",
220                                       prefix, pos->lp_oit_cookie);
221         } else {
222                 rc = snprintf(*buf, *len, "%s: "LPU64", "DFID", "LPU64"\n",
223                               prefix, pos->lp_oit_cookie,
224                               PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
225         }
226         if (rc <= 0)
227                 return -ENOSPC;
228
229         *buf += rc;
230         *len -= rc;
231         return rc;
232 }
233
234 static void mdd_lfsck_pos_fill(const struct lu_env *env, struct md_lfsck *lfsck,
235                                struct lfsck_position *pos, bool init)
236 {
237         const struct dt_it_ops *iops = &lfsck->ml_obj_oit->do_index_ops->dio_it;
238
239         spin_lock(&lfsck->ml_lock);
240         if (unlikely(lfsck->ml_di_oit == NULL)) {
241                 spin_unlock(&lfsck->ml_lock);
242                 memset(pos, 0, sizeof(*pos));
243                 return;
244         }
245
246         pos->lp_oit_cookie = iops->store(env, lfsck->ml_di_oit);
247         if (!lfsck->ml_current_oit_processed && !init)
248                 pos->lp_oit_cookie--;
249
250         LASSERT(pos->lp_oit_cookie > 0);
251
252         if (lfsck->ml_di_dir != NULL) {
253                 struct dt_object *dto = lfsck->ml_obj_dir;
254
255                 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
256                                                         lfsck->ml_di_dir);
257
258                 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
259                         fid_zero(&pos->lp_dir_parent);
260                         pos->lp_dir_cookie = 0;
261                 } else {
262                         pos->lp_dir_parent = *lu_object_fid(&dto->do_lu);
263                 }
264         } else {
265                 fid_zero(&pos->lp_dir_parent);
266                 pos->lp_dir_cookie = 0;
267         }
268         spin_unlock(&lfsck->ml_lock);
269 }
270
271 static inline void mdd_lfsck_pos_set_zero(struct lfsck_position *pos)
272 {
273         memset(pos, 0, sizeof(*pos));
274 }
275
276 static inline int mdd_lfsck_pos_is_zero(const struct lfsck_position *pos)
277 {
278         return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
279 }
280
281 static inline int mdd_lfsck_pos_is_eq(const struct lfsck_position *pos1,
282                                       const struct lfsck_position *pos2)
283 {
284         if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
285                 return -1;
286
287         if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
288                 return 1;
289
290         if (fid_is_zero(&pos1->lp_dir_parent) &&
291             !fid_is_zero(&pos2->lp_dir_parent))
292                 return -1;
293
294         if (!fid_is_zero(&pos1->lp_dir_parent) &&
295             fid_is_zero(&pos2->lp_dir_parent))
296                 return 1;
297
298         if (fid_is_zero(&pos1->lp_dir_parent) &&
299             fid_is_zero(&pos2->lp_dir_parent))
300                 return 0;
301
302         LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
303
304         if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
305                 return -1;
306
307         if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
308                 return 1;
309
310         return 0;
311 }
312
313 static void mdd_lfsck_close_dir(const struct lu_env *env,
314                                 struct md_lfsck *lfsck)
315 {
316         struct dt_object        *dir_obj  = lfsck->ml_obj_dir;
317         const struct dt_it_ops  *dir_iops = &dir_obj->do_index_ops->dio_it;
318         struct dt_it            *dir_di   = lfsck->ml_di_dir;
319
320         spin_lock(&lfsck->ml_lock);
321         lfsck->ml_di_dir = NULL;
322         spin_unlock(&lfsck->ml_lock);
323
324         dir_iops->put(env, dir_di);
325         dir_iops->fini(env, dir_di);
326         lfsck->ml_obj_dir = NULL;
327         lu_object_put(env, &dir_obj->do_lu);
328 }
329
330 static void __mdd_lfsck_set_speed(struct md_lfsck *lfsck, __u32 limit)
331 {
332         lfsck->ml_bookmark_ram.lb_speed_limit = limit;
333         if (limit != LFSCK_SPEED_NO_LIMIT) {
334                 if (limit > CFS_HZ) {
335                         lfsck->ml_sleep_rate = limit / CFS_HZ;
336                         lfsck->ml_sleep_jif = 1;
337                 } else {
338                         lfsck->ml_sleep_rate = 1;
339                         lfsck->ml_sleep_jif = CFS_HZ / limit;
340                 }
341         } else {
342                 lfsck->ml_sleep_jif = 0;
343                 lfsck->ml_sleep_rate = 0;
344         }
345 }
346
347 static void mdd_lfsck_control_speed(struct md_lfsck *lfsck)
348 {
349         struct ptlrpc_thread *thread = &lfsck->ml_thread;
350         struct l_wait_info    lwi;
351
352         if (lfsck->ml_sleep_jif > 0 &&
353             lfsck->ml_new_scanned >= lfsck->ml_sleep_rate) {
354                 spin_lock(&lfsck->ml_lock);
355                 if (likely(lfsck->ml_sleep_jif > 0 &&
356                            lfsck->ml_new_scanned >= lfsck->ml_sleep_rate)) {
357                         lwi = LWI_TIMEOUT_INTR(lfsck->ml_sleep_jif, NULL,
358                                                LWI_ON_SIGNAL_NOOP, NULL);
359                         spin_unlock(&lfsck->ml_lock);
360
361                         l_wait_event(thread->t_ctl_waitq,
362                                      !thread_is_running(thread),
363                                      &lwi);
364                         lfsck->ml_new_scanned = 0;
365                 } else {
366                         spin_unlock(&lfsck->ml_lock);
367                 }
368         }
369 }
370
371 /* lfsck_bookmark file ops */
372
373 static void inline mdd_lfsck_bookmark_to_cpu(struct lfsck_bookmark *des,
374                                              struct lfsck_bookmark *src)
375 {
376         des->lb_magic = le32_to_cpu(src->lb_magic);
377         des->lb_version = le16_to_cpu(src->lb_version);
378         des->lb_param = le16_to_cpu(src->lb_param);
379         des->lb_speed_limit = le32_to_cpu(src->lb_speed_limit);
380 }
381
382 static void inline mdd_lfsck_bookmark_to_le(struct lfsck_bookmark *des,
383                                             struct lfsck_bookmark *src)
384 {
385         des->lb_magic = cpu_to_le32(src->lb_magic);
386         des->lb_version = cpu_to_le16(src->lb_version);
387         des->lb_param = cpu_to_le16(src->lb_param);
388         des->lb_speed_limit = cpu_to_le32(src->lb_speed_limit);
389 }
390
391 static int mdd_lfsck_bookmark_load(const struct lu_env *env,
392                                    struct md_lfsck *lfsck)
393 {
394         loff_t pos = 0;
395         int    len = sizeof(struct lfsck_bookmark);
396         int    rc;
397
398         rc = dt_record_read(env, lfsck->ml_bookmark_obj,
399                             mdd_buf_get(env, &lfsck->ml_bookmark_disk, len),
400                             &pos);
401         if (rc == 0) {
402                 struct lfsck_bookmark *bm = &lfsck->ml_bookmark_ram;
403
404                 mdd_lfsck_bookmark_to_cpu(bm, &lfsck->ml_bookmark_disk);
405                 if (bm->lb_magic != LFSCK_BOOKMARK_MAGIC) {
406                         CWARN("%.16s: invalid lfsck_bookmark magic "
407                               "0x%x != 0x%x\n", mdd_lfsck2name(lfsck),
408                               bm->lb_magic, LFSCK_BOOKMARK_MAGIC);
409                         /* Process it as new lfsck_bookmark. */
410                         rc = -ENODATA;
411                 }
412         } else {
413                 if (rc == -EFAULT && pos == 0)
414                         /* return -ENODATA for empty lfsck_bookmark. */
415                         rc = -ENODATA;
416                 else
417                         CERROR("%.16s: fail to load lfsck_bookmark, "
418                                "expected = %d, rc = %d\n",
419                                mdd_lfsck2name(lfsck), len, rc);
420         }
421         return rc;
422 }
423
424 static int mdd_lfsck_bookmark_store(const struct lu_env *env,
425                                     struct md_lfsck *lfsck)
426 {
427         struct mdd_device *mdd    = mdd_lfsck2mdd(lfsck);
428         struct thandle    *handle;
429         struct dt_object  *obj    = lfsck->ml_bookmark_obj;
430         loff_t             pos    = 0;
431         int                len    = sizeof(struct lfsck_bookmark);
432         int                rc;
433         ENTRY;
434
435         mdd_lfsck_bookmark_to_le(&lfsck->ml_bookmark_disk,
436                                  &lfsck->ml_bookmark_ram);
437         handle = dt_trans_create(env, mdd->mdd_bottom);
438         if (IS_ERR(handle)) {
439                 rc = PTR_ERR(handle);
440                 CERROR("%.16s: fail to create trans for storing "
441                        "lfsck_bookmark: %d\n,", mdd_lfsck2name(lfsck), rc);
442                 RETURN(rc);
443         }
444
445         rc = dt_declare_record_write(env, obj, len, 0, handle);
446         if (rc != 0) {
447                 CERROR("%.16s: fail to declare trans for storing "
448                        "lfsck_bookmark: %d\n,", mdd_lfsck2name(lfsck), rc);
449                 GOTO(out, rc);
450         }
451
452         rc = dt_trans_start_local(env, mdd->mdd_bottom, handle);
453         if (rc != 0) {
454                 CERROR("%.16s: fail to start trans for storing "
455                        "lfsck_bookmark: %d\n,", mdd_lfsck2name(lfsck), rc);
456                 GOTO(out, rc);
457         }
458
459         rc = dt_record_write(env, obj,
460                              mdd_buf_get(env, &lfsck->ml_bookmark_disk, len),
461                              &pos, handle);
462         if (rc != 0)
463                 CERROR("%.16s: fail to store lfsck_bookmark, expected = %d, "
464                        "rc = %d\n", mdd_lfsck2name(lfsck), len, rc);
465
466         GOTO(out, rc);
467
468 out:
469         dt_trans_stop(env, mdd->mdd_bottom, handle);
470         return rc;
471 }
472
473 static int mdd_lfsck_bookmark_init(const struct lu_env *env,
474                                    struct md_lfsck *lfsck)
475 {
476         struct lfsck_bookmark *mb = &lfsck->ml_bookmark_ram;
477         int rc;
478
479         memset(mb, 0, sizeof(*mb));
480         mb->lb_magic = LFSCK_BOOKMARK_MAGIC;
481         mb->lb_version = LFSCK_VERSION_V2;
482         mutex_lock(&lfsck->ml_mutex);
483         rc = mdd_lfsck_bookmark_store(env, lfsck);
484         mutex_unlock(&lfsck->ml_mutex);
485         return rc;
486 }
487
488 /* lfsck_namespace file ops */
489
490 static void inline mdd_lfsck_position_to_cpu(struct lfsck_position *des,
491                                              struct lfsck_position *src)
492 {
493         des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
494         fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
495         des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
496 }
497
498 static void inline mdd_lfsck_position_to_le(struct lfsck_position *des,
499                                              struct lfsck_position *src)
500 {
501         des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
502         fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
503         des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
504 }
505
506 static void inline mdd_lfsck_namespace_to_cpu(struct lfsck_namespace *des,
507                                               struct lfsck_namespace *src)
508 {
509         des->ln_magic = le32_to_cpu(src->ln_magic);
510         des->ln_status = le32_to_cpu(src->ln_status);
511         des->ln_flags = le32_to_cpu(src->ln_flags);
512         des->ln_success_count = le32_to_cpu(src->ln_success_count);
513         des->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
514         des->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
515         des->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
516         des->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
517         des->ln_time_last_checkpoint =
518                                 le64_to_cpu(src->ln_time_last_checkpoint);
519         mdd_lfsck_position_to_cpu(&des->ln_pos_latest_start,
520                                   &src->ln_pos_latest_start);
521         mdd_lfsck_position_to_cpu(&des->ln_pos_last_checkpoint,
522                                   &src->ln_pos_last_checkpoint);
523         mdd_lfsck_position_to_cpu(&des->ln_pos_first_inconsistent,
524                                   &src->ln_pos_first_inconsistent);
525         des->ln_items_checked = le64_to_cpu(src->ln_items_checked);
526         des->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
527         des->ln_items_failed = le64_to_cpu(src->ln_items_failed);
528         des->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
529         des->ln_mlinked_checked = le64_to_cpu(src->ln_mlinked_checked);
530         des->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
531         des->ln_objs_repaired_phase2 =
532                                 le64_to_cpu(src->ln_objs_repaired_phase2);
533         des->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
534         des->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
535         des->ln_objs_lost_found = le64_to_cpu(src->ln_objs_lost_found);
536         fid_le_to_cpu(&des->ln_fid_latest_scanned_phase2,
537                       &src->ln_fid_latest_scanned_phase2);
538 }
539
540 static void inline mdd_lfsck_namespace_to_le(struct lfsck_namespace *des,
541                                              struct lfsck_namespace *src)
542 {
543         des->ln_magic = cpu_to_le32(src->ln_magic);
544         des->ln_status = cpu_to_le32(src->ln_status);
545         des->ln_flags = cpu_to_le32(src->ln_flags);
546         des->ln_success_count = cpu_to_le32(src->ln_success_count);
547         des->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
548         des->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
549         des->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
550         des->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
551         des->ln_time_last_checkpoint =
552                                 cpu_to_le64(src->ln_time_last_checkpoint);
553         mdd_lfsck_position_to_le(&des->ln_pos_latest_start,
554                                  &src->ln_pos_latest_start);
555         mdd_lfsck_position_to_le(&des->ln_pos_last_checkpoint,
556                                  &src->ln_pos_last_checkpoint);
557         mdd_lfsck_position_to_le(&des->ln_pos_first_inconsistent,
558                                  &src->ln_pos_first_inconsistent);
559         des->ln_items_checked = cpu_to_le64(src->ln_items_checked);
560         des->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
561         des->ln_items_failed = cpu_to_le64(src->ln_items_failed);
562         des->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
563         des->ln_mlinked_checked = cpu_to_le64(src->ln_mlinked_checked);
564         des->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
565         des->ln_objs_repaired_phase2 =
566                                 cpu_to_le64(src->ln_objs_repaired_phase2);
567         des->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
568         des->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
569         des->ln_objs_lost_found = cpu_to_le64(src->ln_objs_lost_found);
570         fid_cpu_to_le(&des->ln_fid_latest_scanned_phase2,
571                       &src->ln_fid_latest_scanned_phase2);
572 }
573
574 /**
575  * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
576  * \retval 0: succeed.
577  * \retval -ve: failed cases.
578  */
579 static int mdd_lfsck_namespace_load(const struct lu_env *env,
580                                     struct lfsck_component *com)
581 {
582         int len = com->lc_file_size;
583         int rc;
584
585         rc = dt_xattr_get(env, com->lc_obj,
586                           mdd_buf_get(env, com->lc_file_disk, len),
587                           XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
588         if (rc == len) {
589                 struct lfsck_namespace *ns = com->lc_file_ram;
590
591                 mdd_lfsck_namespace_to_cpu(ns,
592                                 (struct lfsck_namespace *)com->lc_file_disk);
593                 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
594                         CWARN("%.16s: invalid lfsck_namespace magic "
595                               "0x%x != 0x%x\n",
596                               mdd_lfsck2name(com->lc_lfsck),
597                               ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
598                         rc = 1;
599                 } else {
600                         rc = 0;
601                 }
602         } else if (rc != -ENODATA) {
603                 CERROR("%.16s: fail to load lfsck_namespace, expected = %d, "
604                        "rc = %d\n", mdd_lfsck2name(com->lc_lfsck), len, rc);
605                 if (rc >= 0)
606                         rc = 1;
607         }
608         return rc;
609 }
610
611 static int mdd_lfsck_namespace_store(const struct lu_env *env,
612                                      struct lfsck_component *com, bool init)
613 {
614         struct dt_object  *obj    = com->lc_obj;
615         struct md_lfsck   *lfsck  = com->lc_lfsck;
616         struct mdd_device *mdd    = mdd_lfsck2mdd(lfsck);
617         struct thandle    *handle;
618         int                len    = com->lc_file_size;
619         int                rc;
620         ENTRY;
621
622         mdd_lfsck_namespace_to_le((struct lfsck_namespace *)com->lc_file_disk,
623                                   (struct lfsck_namespace *)com->lc_file_ram);
624         handle = dt_trans_create(env, mdd->mdd_bottom);
625         if (IS_ERR(handle)) {
626                 rc = PTR_ERR(handle);
627                 CERROR("%.16s: fail to create trans for storing "
628                        "lfsck_namespace: %d\n,", mdd_lfsck2name(lfsck), rc);
629                 RETURN(rc);
630         }
631
632         rc = dt_declare_xattr_set(env, obj,
633                                   mdd_buf_get(env, com->lc_file_disk, len),
634                                   XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
635         if (rc != 0) {
636                 CERROR("%.16s: fail to declare trans for storing "
637                        "lfsck_namespace: %d\n,", mdd_lfsck2name(lfsck), rc);
638                 GOTO(out, rc);
639         }
640
641         rc = dt_trans_start_local(env, mdd->mdd_bottom, handle);
642         if (rc != 0) {
643                 CERROR("%.16s: fail to start trans for storing "
644                        "lfsck_namespace: %d\n,", mdd_lfsck2name(lfsck), rc);
645                 GOTO(out, rc);
646         }
647
648         rc = dt_xattr_set(env, obj,
649                           mdd_buf_get(env, com->lc_file_disk, len),
650                           XATTR_NAME_LFSCK_NAMESPACE,
651                           init ? LU_XATTR_CREATE : LU_XATTR_REPLACE,
652                           handle, BYPASS_CAPA);
653         if (rc != 0)
654                 CERROR("%.16s: fail to store lfsck_namespace, len = %d, "
655                        "rc = %d\n", mdd_lfsck2name(lfsck), len, rc);
656
657         GOTO(out, rc);
658
659 out:
660         dt_trans_stop(env, mdd->mdd_bottom, handle);
661         return rc;
662 }
663
664 static int mdd_lfsck_namespace_init(const struct lu_env *env,
665                                     struct lfsck_component *com)
666 {
667         struct lfsck_namespace *ns = (struct lfsck_namespace *)com->lc_file_ram;
668         int rc;
669
670         memset(ns, 0, sizeof(*ns));
671         ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
672         ns->ln_status = LS_INIT;
673         down_write(&com->lc_sem);
674         rc = mdd_lfsck_namespace_store(env, com, true);
675         up_write(&com->lc_sem);
676         return rc;
677 }
678
679 static int mdd_lfsck_namespace_lookup(const struct lu_env *env,
680                                       struct lfsck_component *com,
681                                       const struct lu_fid *fid,
682                                       __u8 *flags)
683 {
684         struct lu_fid *key = &mdd_env_info(env)->mti_fid;
685         int            rc;
686
687         fid_cpu_to_be(key, fid);
688         rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)flags,
689                        (const struct dt_key *)key, BYPASS_CAPA);
690         return rc;
691 }
692
693 static int mdd_lfsck_namespace_delete(const struct lu_env *env,
694                                       struct lfsck_component *com,
695                                       const struct lu_fid *fid)
696 {
697         struct mdd_device *mdd    = mdd_lfsck2mdd(com->lc_lfsck);
698         struct lu_fid     *key    = &mdd_env_info(env)->mti_fid;
699         struct thandle    *handle;
700         struct dt_object *obj     = com->lc_obj;
701         int               rc;
702         ENTRY;
703
704         handle = dt_trans_create(env, mdd->mdd_bottom);
705         if (IS_ERR(handle))
706                 RETURN(PTR_ERR(handle));
707
708         rc = dt_declare_delete(env, obj, (const struct dt_key *)fid, handle);
709         if (rc != 0)
710                 GOTO(out, rc);
711
712         rc = dt_trans_start_local(env, mdd->mdd_bottom, handle);
713         if (rc != 0)
714                 GOTO(out, rc);
715
716         fid_cpu_to_be(key, fid);
717         rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
718                        BYPASS_CAPA);
719
720         GOTO(out, rc);
721
722 out:
723         dt_trans_stop(env, mdd->mdd_bottom, handle);
724         return rc;
725 }
726
727 static int mdd_lfsck_namespace_update(const struct lu_env *env,
728                                       struct lfsck_component *com,
729                                       const struct lu_fid *fid,
730                                       __u8 flags, bool force)
731 {
732         struct mdd_device *mdd    = mdd_lfsck2mdd(com->lc_lfsck);
733         struct lu_fid     *key    = &mdd_env_info(env)->mti_fid;
734         struct thandle    *handle;
735         struct dt_object *obj     = com->lc_obj;
736         int               rc;
737         bool              exist   = false;
738         __u8              tf;
739         ENTRY;
740
741         rc = mdd_lfsck_namespace_lookup(env, com, fid, &tf);
742         if (rc != 0 && rc != -ENOENT)
743                 RETURN(rc);
744
745         if (rc == 0) {
746                 if (!force || flags == tf)
747                         RETURN(0);
748
749                 exist = true;
750                 handle = dt_trans_create(env, mdd->mdd_bottom);
751                 if (IS_ERR(handle))
752                         RETURN(PTR_ERR(handle));
753
754                 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid,
755                                        handle);
756                 if (rc != 0)
757                         GOTO(out, rc);
758         } else {
759                 handle = dt_trans_create(env, mdd->mdd_bottom);
760                 if (IS_ERR(handle))
761                         RETURN(PTR_ERR(handle));
762         }
763
764         rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
765                                (const struct dt_key *)fid, handle);
766         if (rc != 0)
767                 GOTO(out, rc);
768
769         rc = dt_trans_start_local(env, mdd->mdd_bottom, handle);
770         if (rc != 0)
771                 GOTO(out, rc);
772
773         fid_cpu_to_be(key, fid);
774         if (exist) {
775                 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
776                                BYPASS_CAPA);
777                 if (rc != 0) {
778                         CERROR("%s: fail to insert "DFID", rc = %d\n",
779                                mdd_lfsck2name(com->lc_lfsck), PFID(fid), rc);
780                         GOTO(out, rc);
781                 }
782         }
783
784         rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
785                        (const struct dt_key *)key, handle, BYPASS_CAPA, 1);
786
787         GOTO(out, rc);
788
789 out:
790         dt_trans_stop(env, mdd->mdd_bottom, handle);
791         return rc;
792 }
793
794 /**
795  * \retval +ve  repaired
796  * \retval 0    no need to repair
797  * \retval -ve  error cases
798  */
799 static int mdd_lfsck_namespace_double_scan_one(const struct lu_env *env,
800                                                struct lfsck_component *com,
801                                                struct mdd_object *child,
802                                                __u8 flags)
803 {
804         struct mdd_thread_info  *info     = mdd_env_info(env);
805         struct lu_attr          *la       = &info->mti_la;
806         struct lu_name          *cname    = &info->mti_name;
807         struct lu_fid           *pfid     = &info->mti_fid;
808         struct lu_fid           *cfid     = &info->mti_fid2;
809         struct md_lfsck         *lfsck    = com->lc_lfsck;
810         struct mdd_device       *mdd      = mdd_lfsck2mdd(lfsck);
811         struct lfsck_bookmark   *bk       = &lfsck->ml_bookmark_ram;
812         struct lfsck_namespace  *ns       =
813                                 (struct lfsck_namespace *)com->lc_file_ram;
814         struct linkea_data       ldata    = { 0 };
815         struct thandle          *handle   = NULL;
816         bool                     locked   = false;
817         bool                     update   = false;
818         int                      count;
819         int                      rc;
820         ENTRY;
821
822         if (com->lc_journal) {
823
824 again:
825                 LASSERT(!locked);
826
827                 com->lc_journal = 1;
828                 handle = mdd_trans_create(env, mdd);
829                 if (IS_ERR(handle))
830                         RETURN(rc = PTR_ERR(handle));
831
832                 rc = mdd_declare_links_add(env, child, handle, NULL);
833                 if (rc != 0)
834                         GOTO(stop, rc);
835
836                 rc = mdd_trans_start(env, mdd, handle);
837                 if (rc != 0)
838                         GOTO(stop, rc);
839
840                 mdd_write_lock(env, child, MOR_TGT_CHILD);
841                 locked = true;
842         }
843
844         if (unlikely(mdd_is_dead_obj(child)))
845                 GOTO(stop, rc = 0);
846
847         rc = mdd_links_read(env, child, &ldata);
848         if (rc != 0) {
849                 if ((bk->lb_param & LPF_DRYRUN) &&
850                     (rc == -EINVAL || rc == -ENODATA))
851                         rc = 1;
852
853                 GOTO(stop, rc);
854         }
855
856         rc = mdd_la_get(env, child, la, BYPASS_CAPA);
857         if (rc != 0)
858                 GOTO(stop, rc);
859
860         ldata.ld_lee = LINKEA_FIRST_ENTRY(ldata);
861         count = ldata.ld_leh->leh_reccount;
862         while (count-- > 0) {
863                 struct mdd_object *parent = NULL;
864                 struct dt_object *dir;
865
866                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname,
867                                     pfid);
868                 if (!fid_is_sane(pfid))
869                         goto shrink;
870
871                 parent = mdd_object_find(env, mdd, pfid);
872                 if (parent == NULL)
873                         goto shrink;
874                 else if (IS_ERR(parent))
875                         GOTO(stop, rc = PTR_ERR(parent));
876
877                 if (!mdd_object_exists(parent))
878                         goto shrink;
879
880                 /* XXX: Currently, skip remote object, the consistency for
881                  *      remote object will be processed in LFSCK phase III. */
882                 if (mdd_object_remote(parent)) {
883                         mdd_object_put(env, parent);
884                         ldata.ld_lee = LINKEA_NEXT_ENTRY(ldata);
885                         continue;
886                 }
887
888                 dir = mdd_object_child(parent);
889                 if (unlikely(!dt_try_as_dir(env, dir)))
890                         goto shrink;
891
892                 /* To guarantee the 'name' is terminated with '0'. */
893                 memcpy(info->mti_key, cname->ln_name, cname->ln_namelen);
894                 info->mti_key[cname->ln_namelen] = 0;
895                 cname->ln_name = info->mti_key;
896                 rc = dt_lookup(env, dir, (struct dt_rec *)cfid,
897                                (const struct dt_key *)cname->ln_name,
898                                BYPASS_CAPA);
899                 if (rc != 0 && rc != -ENOENT) {
900                         mdd_object_put(env, parent);
901                         GOTO(stop, rc);
902                 }
903
904                 if (rc == 0) {
905                         if (lu_fid_eq(cfid, mdo2fid(child))) {
906                                 mdd_object_put(env, parent);
907                                 ldata.ld_lee = LINKEA_NEXT_ENTRY(ldata);
908                                 continue;
909                         }
910
911                         goto shrink;
912                 }
913
914                 if (ldata.ld_leh->leh_reccount > la->la_nlink)
915                         goto shrink;
916
917                 /* XXX: For the case of there is linkea entry, but without name
918                  *      entry pointing to the object, and the object link count
919                  *      isn't less than the count of name entries, then add the
920                  *      name entry back to namespace.
921                  *
922                  *      It is out of LFSCK 1.5 scope, will implement it in the
923                  *      future. Keep the linkEA entry. */
924                 mdd_object_put(env, parent);
925                 ldata.ld_lee = LINKEA_NEXT_ENTRY(ldata);
926                 continue;
927
928 shrink:
929                 if (parent != NULL)
930                         mdd_object_put(env, parent);
931                 if (bk->lb_param & LPF_DRYRUN)
932                         RETURN(1);
933
934                 CDEBUG(D_LFSCK, "Remove linkEA: "DFID"[%.*s], "DFID"\n",
935                        PFID(mdo2fid(child)), cname->ln_namelen, cname->ln_name,
936                        PFID(pfid));
937                 linkea_del_buf(&ldata, cname);
938                 update = true;
939         }
940
941         if (update) {
942                 if (!com->lc_journal) {
943                         com->lc_journal = 1;
944                         goto again;
945                 }
946
947                 rc = mdd_links_write(env, child, &ldata, handle);
948         }
949
950         GOTO(stop, rc);
951
952 stop:
953         if (locked)
954                 mdd_write_unlock(env, child);
955
956         if (handle != NULL)
957                 mdd_trans_stop(env, mdd, rc, handle);
958
959         if (rc == 0 && update) {
960                 ns->ln_objs_nlink_repaired++;
961                 rc = 1;
962         }
963         return rc;
964 }
965
966 /* namespace APIs */
967
968 static int mdd_lfsck_namespace_reset(const struct lu_env *env,
969                                      struct lfsck_component *com, bool init)
970 {
971         struct lfsck_namespace  *ns   = (struct lfsck_namespace *)com->lc_file_ram;
972         struct mdd_device       *mdd  = mdd_lfsck2mdd(com->lc_lfsck);
973         struct dt_object        *dto, *root;
974         int                      rc;
975         ENTRY;
976
977         down_write(&com->lc_sem);
978         if (init) {
979                 memset(ns, 0, sizeof(*ns));
980         } else {
981                 __u32 count = ns->ln_success_count;
982                 __u64 last_time = ns->ln_time_last_complete;
983
984                 memset(ns, 0, sizeof(*ns));
985                 ns->ln_success_count = count;
986                 ns->ln_time_last_complete = last_time;
987         }
988         ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
989         ns->ln_status = LS_INIT;
990
991         root = dt_locate(env, mdd->mdd_bottom, &mdd->mdd_local_root_fid);
992         if (unlikely(IS_ERR(root)))
993                 GOTO(out, rc = PTR_ERR(root));
994
995         rc = local_object_unlink(env, mdd->mdd_bottom, root,
996                                  lfsck_namespace_name);
997         if (rc != 0)
998                 GOTO(out, rc);
999
1000         dto = local_index_find_or_create(env, mdd->mdd_los, root,
1001                                          lfsck_namespace_name,
1002                                          S_IFREG | S_IRUGO | S_IWUSR,
1003                                          &dt_lfsck_features);
1004         if (IS_ERR(dto))
1005                 GOTO(out, rc = PTR_ERR(dto));
1006
1007         rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
1008         if (rc != 0)
1009                 GOTO(out, rc);
1010         com->lc_obj = dto;
1011
1012         rc = mdd_lfsck_namespace_store(env, com, true);
1013
1014         GOTO(out, rc);
1015 out:
1016         lu_object_put(env, &root->do_lu);
1017         up_write(&com->lc_sem);
1018         return rc;
1019 }
1020
1021 static void
1022 mdd_lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
1023                          bool new_checked)
1024 {
1025         struct lfsck_namespace *ns = (struct lfsck_namespace *)com->lc_file_ram;
1026
1027         down_write(&com->lc_sem);
1028         if (new_checked)
1029                 com->lc_new_checked++;
1030         ns->ln_items_failed++;
1031         if (mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1032                 mdd_lfsck_pos_fill(env, com->lc_lfsck,
1033                                    &ns->ln_pos_first_inconsistent, false);
1034         up_write(&com->lc_sem);
1035 }
1036
1037 static int mdd_lfsck_namespace_checkpoint(const struct lu_env *env,
1038                                           struct lfsck_component *com,
1039                                           bool init)
1040 {
1041         struct md_lfsck         *lfsck = com->lc_lfsck;
1042         struct lfsck_namespace  *ns    =
1043                                 (struct lfsck_namespace *)com->lc_file_ram;
1044         int                      rc;
1045
1046         if (com->lc_new_checked == 0 && !init)
1047                 return 0;
1048
1049         down_write(&com->lc_sem);
1050
1051         if (init) {
1052                 ns->ln_pos_latest_start = lfsck->ml_pos_current;
1053         } else {
1054                 ns->ln_pos_last_checkpoint = lfsck->ml_pos_current;
1055                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1056                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1057                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1058                 ns->ln_items_checked += com->lc_new_checked;
1059                 com->lc_new_checked = 0;
1060         }
1061
1062         rc = mdd_lfsck_namespace_store(env, com, false);
1063
1064         up_write(&com->lc_sem);
1065         return rc;
1066 }
1067
1068 static int mdd_lfsck_namespace_prep(const struct lu_env *env,
1069                                     struct lfsck_component *com)
1070 {
1071         struct md_lfsck         *lfsck  = com->lc_lfsck;
1072         struct lfsck_namespace  *ns     =
1073                                 (struct lfsck_namespace *)com->lc_file_ram;
1074         struct lfsck_position   *pos    = &com->lc_pos_start;
1075
1076         if (ns->ln_status == LS_COMPLETED) {
1077                 int rc;
1078
1079                 rc = mdd_lfsck_namespace_reset(env, com, false);
1080                 if (rc != 0)
1081                         return rc;
1082         }
1083
1084         down_write(&com->lc_sem);
1085
1086         ns->ln_time_latest_start = cfs_time_current_sec();
1087
1088         spin_lock(&lfsck->ml_lock);
1089         if (ns->ln_flags & LF_SCANNED_ONCE) {
1090                 if (!lfsck->ml_drop_dryrun ||
1091                     mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
1092                         ns->ln_status = LS_SCANNING_PHASE2;
1093                         cfs_list_del_init(&com->lc_link);
1094                         cfs_list_add_tail(&com->lc_link,
1095                                           &lfsck->ml_list_double_scan);
1096                         if (!cfs_list_empty(&com->lc_link_dir))
1097                                 cfs_list_del_init(&com->lc_link_dir);
1098                         mdd_lfsck_pos_set_zero(pos);
1099                 } else {
1100                         ns->ln_status = LS_SCANNING_PHASE1;
1101                         ns->ln_run_time_phase1 = 0;
1102                         ns->ln_run_time_phase2 = 0;
1103                         ns->ln_items_checked = 0;
1104                         ns->ln_items_repaired = 0;
1105                         ns->ln_items_failed = 0;
1106                         ns->ln_dirs_checked = 0;
1107                         ns->ln_mlinked_checked = 0;
1108                         ns->ln_objs_checked_phase2 = 0;
1109                         ns->ln_objs_repaired_phase2 = 0;
1110                         ns->ln_objs_failed_phase2 = 0;
1111                         ns->ln_objs_nlink_repaired = 0;
1112                         ns->ln_objs_lost_found = 0;
1113                         fid_zero(&ns->ln_fid_latest_scanned_phase2);
1114                         if (cfs_list_empty(&com->lc_link_dir))
1115                                 cfs_list_add_tail(&com->lc_link_dir,
1116                                                   &lfsck->ml_list_dir);
1117                         *pos = ns->ln_pos_first_inconsistent;
1118                 }
1119         } else {
1120                 ns->ln_status = LS_SCANNING_PHASE1;
1121                 if (cfs_list_empty(&com->lc_link_dir))
1122                         cfs_list_add_tail(&com->lc_link_dir,
1123                                           &lfsck->ml_list_dir);
1124                 if (!lfsck->ml_drop_dryrun ||
1125                     mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
1126                         *pos = ns->ln_pos_last_checkpoint;
1127                         pos->lp_oit_cookie++;
1128                 } else {
1129                         *pos = ns->ln_pos_first_inconsistent;
1130                 }
1131         }
1132         spin_unlock(&lfsck->ml_lock);
1133
1134         up_write(&com->lc_sem);
1135         return 0;
1136 }
1137
1138 static int mdd_lfsck_namespace_exec_oit(const struct lu_env *env,
1139                                         struct lfsck_component *com,
1140                                         struct mdd_object *obj)
1141 {
1142         down_write(&com->lc_sem);
1143         com->lc_new_checked++;
1144         if (S_ISDIR(mdd_object_type(obj)))
1145                 ((struct lfsck_namespace *)com->lc_file_ram)->ln_dirs_checked++;
1146         up_write(&com->lc_sem);
1147         return 0;
1148 }
1149
1150 static int mdd_declare_lfsck_namespace_exec_dir(const struct lu_env *env,
1151                                                 struct mdd_object *obj,
1152                                                 struct thandle *handle)
1153 {
1154         int rc;
1155
1156         /* For destroying all invalid linkEA entries. */
1157         rc = mdo_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
1158         if (rc != 0)
1159                 return rc;
1160
1161         /* For insert new linkEA entry. */
1162         rc = mdd_declare_links_add(env, obj, handle, NULL);
1163         return rc;
1164 }
1165
1166 static int mdd_lfsck_namespace_check_exist(const struct lu_env *env,
1167                                            struct md_lfsck *lfsck,
1168                                            struct mdd_object *obj,
1169                                            const char *name)
1170 {
1171         struct dt_object *dir = lfsck->ml_obj_dir;
1172         struct lu_fid    *fid = &mdd_env_info(env)->mti_fid;
1173         int               rc;
1174         ENTRY;
1175
1176         if (unlikely(mdd_is_dead_obj(obj)))
1177                 RETURN(LFSCK_NAMEENTRY_DEAD);
1178
1179         rc = dt_lookup(env, dir, (struct dt_rec *)fid,
1180                        (const struct dt_key *)name, BYPASS_CAPA);
1181         if (rc == -ENOENT)
1182                 RETURN(LFSCK_NAMEENTRY_REMOVED);
1183
1184         if (rc < 0)
1185                 RETURN(rc);
1186
1187         if (!lu_fid_eq(fid, mdo2fid(obj)))
1188                 RETURN(LFSCK_NAMEENTRY_RECREATED);
1189
1190         RETURN(0);
1191 }
1192
1193 static int mdd_lfsck_namespace_exec_dir(const struct lu_env *env,
1194                                         struct lfsck_component *com,
1195                                         struct mdd_object *obj,
1196                                         struct lu_dirent *ent)
1197 {
1198         struct mdd_thread_info     *info     = mdd_env_info(env);
1199         struct lu_attr             *la       = &info->mti_la;
1200         struct md_lfsck            *lfsck    = com->lc_lfsck;
1201         struct lfsck_bookmark      *bk       = &lfsck->ml_bookmark_ram;
1202         struct lfsck_namespace     *ns       =
1203                                 (struct lfsck_namespace *)com->lc_file_ram;
1204         struct mdd_device          *mdd      = mdd_lfsck2mdd(lfsck);
1205         struct linkea_data          ldata    = { 0 };
1206         const struct lu_fid        *pfid     =
1207                                 lu_object_fid(&lfsck->ml_obj_dir->do_lu);
1208         const struct lu_fid        *cfid     = mdo2fid(obj);
1209         const struct lu_name       *cname;
1210         struct thandle             *handle   = NULL;
1211         bool                        repaired = false;
1212         bool                        locked   = false;
1213         int                         count    = 0;
1214         int                         rc;
1215         ENTRY;
1216
1217         cname = mdd_name_get_const(env, ent->lde_name, ent->lde_namelen);
1218         down_write(&com->lc_sem);
1219         com->lc_new_checked++;
1220
1221         if (ent->lde_attrs & LUDA_UPGRADE) {
1222                 ns->ln_flags |= LF_UPGRADE;
1223                 repaired = true;
1224         } else if (ent->lde_attrs & LUDA_REPAIR) {
1225                 ns->ln_flags |= LF_INCONSISTENT;
1226                 repaired = true;
1227         }
1228
1229         if (ent->lde_name[0] == '.' &&
1230             (ent->lde_namelen == 1 ||
1231              (ent->lde_namelen == 2 && ent->lde_name[1] == '.') ||
1232              fid_is_dot_lustre(&ent->lde_fid)))
1233                 GOTO(out, rc = 0);
1234
1235         if (!(bk->lb_param & LPF_DRYRUN) &&
1236             (com->lc_journal || repaired)) {
1237
1238 again:
1239                 LASSERT(!locked);
1240
1241                 com->lc_journal = 1;
1242                 handle = mdd_trans_create(env, mdd);
1243                 if (IS_ERR(handle))
1244                         GOTO(out, rc = PTR_ERR(handle));
1245
1246                 rc = mdd_declare_lfsck_namespace_exec_dir(env, obj, handle);
1247                 if (rc != 0)
1248                         GOTO(stop, rc);
1249
1250                 rc = mdd_trans_start(env, mdd, handle);
1251                 if (rc != 0)
1252                         GOTO(stop, rc);
1253
1254                 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1255                 locked = true;
1256         }
1257
1258         rc = mdd_lfsck_namespace_check_exist(env, lfsck, obj, ent->lde_name);
1259         if (rc != 0)
1260                 GOTO(stop, rc);
1261
1262         rc = mdd_links_read(env, obj, &ldata);
1263         if (rc == 0) {
1264                 count = ldata.ld_leh->leh_reccount;
1265                 rc = linkea_links_find(&ldata, cname, pfid);
1266                 if (rc == 0) {
1267                         /* For dir, if there are more than one linkea entries,
1268                          * then remove all the other redundant linkea entries.*/
1269                         if (unlikely(count > 1 &&
1270                                      S_ISDIR(mdd_object_type(obj))))
1271                                 goto unmatch;
1272
1273                         goto record;
1274                 } else {
1275
1276 unmatch:
1277                         ns->ln_flags |= LF_INCONSISTENT;
1278                         if (bk->lb_param & LPF_DRYRUN) {
1279                                 repaired = true;
1280                                 goto record;
1281                         }
1282
1283                         /*For dir, remove the unmatched linkea entry directly.*/
1284                         if (S_ISDIR(mdd_object_type(obj))) {
1285                                 if (!com->lc_journal)
1286                                         goto again;
1287
1288                                 rc = mdo_xattr_del(env, obj, XATTR_NAME_LINK,
1289                                                    handle, BYPASS_CAPA);
1290                                 if (rc != 0)
1291                                         GOTO(stop, rc);
1292
1293                                 goto nodata;
1294                         } else {
1295                                 goto add;
1296                         }
1297                 }
1298         } else if (unlikely(rc == -EINVAL)) {
1299                 ns->ln_flags |= LF_INCONSISTENT;
1300                 if (bk->lb_param & LPF_DRYRUN) {
1301                         count = 1;
1302                         repaired = true;
1303                         goto record;
1304                 }
1305
1306                 if (!com->lc_journal)
1307                         goto again;
1308
1309                 /* The magic crashed, we are not sure whether there are more
1310                  * corrupt data in the linkea, so remove all linkea entries. */
1311                 rc = mdo_xattr_del(env, obj, XATTR_NAME_LINK, handle,
1312                                    BYPASS_CAPA);
1313                 if (rc != 0)
1314                         GOTO(stop, rc);
1315
1316                 goto nodata;
1317         } else if (rc == -ENODATA) {
1318                 ns->ln_flags |= LF_UPGRADE;
1319                 if (bk->lb_param & LPF_DRYRUN) {
1320                         count = 1;
1321                         repaired = true;
1322                         goto record;
1323                 }
1324
1325 nodata:
1326                 rc = linkea_data_new(&ldata, &mdd_env_info(env)->mti_link_buf);
1327                 if (rc != 0)
1328                         GOTO(stop, rc);
1329
1330 add:
1331                 if (!com->lc_journal)
1332                         goto again;
1333
1334                 rc = linkea_add_buf(&ldata, cname, pfid);
1335                 if (rc != 0)
1336                         GOTO(stop, rc);
1337
1338                 rc = mdd_links_write(env, obj, &ldata, handle);
1339                 if (rc != 0)
1340                         GOTO(stop, rc);
1341
1342                 count = ldata.ld_leh->leh_reccount;
1343                 repaired = true;
1344         } else {
1345                 GOTO(stop, rc);
1346         }
1347
1348 record:
1349         LASSERT(count > 0);
1350
1351         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1352         if (rc != 0)
1353                 GOTO(stop, rc);
1354
1355         if ((count == 1) &&
1356             (la->la_nlink == 1 || S_ISDIR(mdd_object_type(obj))))
1357                 /* Usually, it is for single linked object or dir, do nothing.*/
1358                 GOTO(stop, rc);
1359
1360         /* Following modification will be in another transaction.  */
1361         if (handle != NULL) {
1362                 LASSERT(mdd_write_locked(env, obj));
1363
1364                 mdd_write_unlock(env, obj);
1365                 locked = false;
1366
1367                 mdd_trans_stop(env, mdd, 0, handle);
1368                 handle = NULL;
1369         }
1370
1371         ns->ln_mlinked_checked++;
1372         rc = mdd_lfsck_namespace_update(env, com, cfid,
1373                         count != la->la_nlink ? LLF_UNMATCH_NLINKS : 0, false);
1374
1375         GOTO(out, rc);
1376
1377 stop:
1378         if (locked)
1379                 mdd_write_unlock(env, obj);
1380
1381         if (handle != NULL)
1382                 mdd_trans_stop(env, mdd, rc, handle);
1383
1384 out:
1385         if (rc < 0) {
1386                 ns->ln_items_failed++;
1387                 if (mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1388                         mdd_lfsck_pos_fill(env, lfsck,
1389                                            &ns->ln_pos_first_inconsistent,
1390                                            false);
1391                 if (!(bk->lb_param & LPF_FAILOUT))
1392                         rc = 0;
1393         } else {
1394                 if (repaired)
1395                         ns->ln_items_repaired++;
1396                 else
1397                         com->lc_journal = 0;
1398                 rc = 0;
1399         }
1400         up_write(&com->lc_sem);
1401         return rc;
1402 }
1403
1404 static int mdd_lfsck_namespace_post(const struct lu_env *env,
1405                                     struct lfsck_component *com,
1406                                     int result, bool init)
1407 {
1408         struct md_lfsck         *lfsck = com->lc_lfsck;
1409         struct lfsck_namespace  *ns    =
1410                                 (struct lfsck_namespace *)com->lc_file_ram;
1411         int                      rc;
1412
1413         down_write(&com->lc_sem);
1414
1415         spin_lock(&lfsck->ml_lock);
1416         if (!init)
1417                 ns->ln_pos_last_checkpoint = lfsck->ml_pos_current;
1418         if (result > 0) {
1419                 ns->ln_status = LS_SCANNING_PHASE2;
1420                 ns->ln_flags |= LF_SCANNED_ONCE;
1421                 ns->ln_flags &= ~LF_UPGRADE;
1422                 cfs_list_del_init(&com->lc_link);
1423                 cfs_list_del_init(&com->lc_link_dir);
1424                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_double_scan);
1425         } else if (result == 0) {
1426                 if (lfsck->ml_paused) {
1427                         ns->ln_status = LS_PAUSED;
1428                 } else {
1429                         ns->ln_status = LS_STOPPED;
1430                         cfs_list_del_init(&com->lc_link);
1431                         cfs_list_del_init(&com->lc_link_dir);
1432                         cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1433                 }
1434         } else {
1435                 ns->ln_status = LS_FAILED;
1436                 cfs_list_del_init(&com->lc_link);
1437                 cfs_list_del_init(&com->lc_link_dir);
1438                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1439         }
1440         spin_unlock(&lfsck->ml_lock);
1441
1442         if (!init) {
1443                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1444                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1445                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1446                 ns->ln_items_checked += com->lc_new_checked;
1447                 com->lc_new_checked = 0;
1448         }
1449
1450         rc = mdd_lfsck_namespace_store(env, com, false);
1451
1452         up_write(&com->lc_sem);
1453         return rc;
1454 }
1455
1456 static int
1457 mdd_lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
1458                          char *buf, int len)
1459 {
1460         struct md_lfsck         *lfsck = com->lc_lfsck;
1461         struct lfsck_bookmark   *bk    = &lfsck->ml_bookmark_ram;
1462         struct lfsck_namespace  *ns    =
1463                                 (struct lfsck_namespace *)com->lc_file_ram;
1464         int                      save  = len;
1465         int                      ret   = -ENOSPC;
1466         int                      rc;
1467
1468         down_read(&com->lc_sem);
1469         rc = snprintf(buf, len,
1470                       "name: lfsck_namespace\n"
1471                       "magic: 0x%x\n"
1472                       "version: %d\n"
1473                       "status: %s\n",
1474                       ns->ln_magic,
1475                       bk->lb_version,
1476                       lfsck_status_names[ns->ln_status]);
1477         if (rc <= 0)
1478                 goto out;
1479
1480         buf += rc;
1481         len -= rc;
1482         rc = lfsck_bits_dump(&buf, &len, ns->ln_flags, lfsck_flags_names,
1483                              "flags");
1484         if (rc < 0)
1485                 goto out;
1486
1487         rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1488                              "param");
1489         if (rc < 0)
1490                 goto out;
1491
1492         rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_complete,
1493                              "time_since_last_completed");
1494         if (rc < 0)
1495                 goto out;
1496
1497         rc = lfsck_time_dump(&buf, &len, ns->ln_time_latest_start,
1498                              "time_since_latest_start");
1499         if (rc < 0)
1500                 goto out;
1501
1502         rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_checkpoint,
1503                              "time_since_last_checkpoint");
1504         if (rc < 0)
1505                 goto out;
1506
1507         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_latest_start,
1508                             "latest_start_position");
1509         if (rc < 0)
1510                 goto out;
1511
1512         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_last_checkpoint,
1513                             "last_checkpoint_position");
1514         if (rc < 0)
1515                 goto out;
1516
1517         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_first_inconsistent,
1518                             "first_failure_position");
1519         if (rc < 0)
1520                 goto out;
1521
1522         if (ns->ln_status == LS_SCANNING_PHASE1) {
1523                 struct lfsck_position pos;
1524                 cfs_duration_t duration = cfs_time_current() -
1525                                           lfsck->ml_time_last_checkpoint;
1526                 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1527                 __u64 speed = checked;
1528                 __u64 new_checked = com->lc_new_checked * CFS_HZ;
1529                 __u32 rtime = ns->ln_run_time_phase1 +
1530                               cfs_duration_sec(duration + HALF_SEC);
1531
1532                 if (duration != 0)
1533                         do_div(new_checked, duration);
1534                 if (rtime != 0)
1535                         do_div(speed, rtime);
1536                 rc = snprintf(buf, len,
1537                               "checked_phase1: "LPU64"\n"
1538                               "checked_phase2: "LPU64"\n"
1539                               "updated_phase1: "LPU64"\n"
1540                               "updated_phase2: "LPU64"\n"
1541                               "failed_phase1: "LPU64"\n"
1542                               "failed_phase2: "LPU64"\n"
1543                               "dirs: "LPU64"\n"
1544                               "M-linked: "LPU64"\n"
1545                               "nlinks_repaired: "LPU64"\n"
1546                               "lost_found: "LPU64"\n"
1547                               "success_count: %u\n"
1548                               "run_time_phase1: %u seconds\n"
1549                               "run_time_phase2: %u seconds\n"
1550                               "average_speed_phase1: "LPU64" items/sec\n"
1551                               "average_speed_phase2: N/A\n"
1552                               "real-time_speed_phase1: "LPU64" items/sec\n"
1553                               "real-time_speed_phase2: N/A\n",
1554                               checked,
1555                               ns->ln_objs_checked_phase2,
1556                               ns->ln_items_repaired,
1557                               ns->ln_objs_repaired_phase2,
1558                               ns->ln_items_failed,
1559                               ns->ln_objs_failed_phase2,
1560                               ns->ln_dirs_checked,
1561                               ns->ln_mlinked_checked,
1562                               ns->ln_objs_nlink_repaired,
1563                               ns->ln_objs_lost_found,
1564                               ns->ln_success_count,
1565                               rtime,
1566                               ns->ln_run_time_phase2,
1567                               speed,
1568                               new_checked);
1569                 if (rc <= 0)
1570                         goto out;
1571
1572                 buf += rc;
1573                 len -= rc;
1574                 mdd_lfsck_pos_fill(env, lfsck, &pos, false);
1575                 rc = lfsck_pos_dump(&buf, &len, &pos, "current_position");
1576                 if (rc <= 0)
1577                         goto out;
1578         } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1579                 cfs_duration_t duration = cfs_time_current() -
1580                                           lfsck->ml_time_last_checkpoint;
1581                 __u64 checked = ns->ln_objs_checked_phase2 +
1582                                 com->lc_new_checked;
1583                 __u64 speed1 = ns->ln_items_checked;
1584                 __u64 speed2 = checked;
1585                 __u64 new_checked = com->lc_new_checked * CFS_HZ;
1586                 __u32 rtime = ns->ln_run_time_phase2 +
1587                               cfs_duration_sec(duration + HALF_SEC);
1588
1589                 if (duration != 0)
1590                         do_div(new_checked, duration);
1591                 if (ns->ln_run_time_phase1 != 0)
1592                         do_div(speed1, ns->ln_run_time_phase1);
1593                 if (rtime != 0)
1594                         do_div(speed2, rtime);
1595                 rc = snprintf(buf, len,
1596                               "checked_phase1: "LPU64"\n"
1597                               "checked_phase2: "LPU64"\n"
1598                               "updated_phase1: "LPU64"\n"
1599                               "updated_phase2: "LPU64"\n"
1600                               "failed_phase1: "LPU64"\n"
1601                               "failed_phase2: "LPU64"\n"
1602                               "dirs: "LPU64"\n"
1603                               "M-linked: "LPU64"\n"
1604                               "nlinks_repaired: "LPU64"\n"
1605                               "lost_found: "LPU64"\n"
1606                               "success_count: %u\n"
1607                               "run_time_phase1: %u seconds\n"
1608                               "run_time_phase2: %u seconds\n"
1609                               "average_speed_phase1: "LPU64" items/sec\n"
1610                               "average_speed_phase2: "LPU64" objs/sec\n"
1611                               "real-time_speed_phase1: N/A\n"
1612                               "real-time_speed_phase2: "LPU64" objs/sec\n"
1613                               "current_position: "DFID"\n",
1614                               ns->ln_items_checked,
1615                               checked,
1616                               ns->ln_items_repaired,
1617                               ns->ln_objs_repaired_phase2,
1618                               ns->ln_items_failed,
1619                               ns->ln_objs_failed_phase2,
1620                               ns->ln_dirs_checked,
1621                               ns->ln_mlinked_checked,
1622                               ns->ln_objs_nlink_repaired,
1623                               ns->ln_objs_lost_found,
1624                               ns->ln_success_count,
1625                               ns->ln_run_time_phase1,
1626                               rtime,
1627                               speed1,
1628                               speed2,
1629                               new_checked,
1630                               PFID(&ns->ln_fid_latest_scanned_phase2));
1631                 if (rc <= 0)
1632                         goto out;
1633
1634                 buf += rc;
1635                 len -= rc;
1636         } else {
1637                 __u64 speed1 = ns->ln_items_checked;
1638                 __u64 speed2 = ns->ln_objs_checked_phase2;
1639
1640                 if (ns->ln_run_time_phase1 != 0)
1641                         do_div(speed1, ns->ln_run_time_phase1);
1642                 if (ns->ln_run_time_phase2 != 0)
1643                         do_div(speed2, ns->ln_run_time_phase2);
1644                 rc = snprintf(buf, len,
1645                               "checked_phase1: "LPU64"\n"
1646                               "checked_phase2: "LPU64"\n"
1647                               "updated_phase1: "LPU64"\n"
1648                               "updated_phase2: "LPU64"\n"
1649                               "failed_phase1: "LPU64"\n"
1650                               "failed_phase2: "LPU64"\n"
1651                               "dirs: "LPU64"\n"
1652                               "M-linked: "LPU64"\n"
1653                               "nlinks_repaired: "LPU64"\n"
1654                               "lost_found: "LPU64"\n"
1655                               "success_count: %u\n"
1656                               "run_time_phase1: %u seconds\n"
1657                               "run_time_phase2: %u seconds\n"
1658                               "average_speed_phase1: "LPU64" items/sec\n"
1659                               "average_speed_phase2: "LPU64" objs/sec\n"
1660                               "real-time_speed_phase1: N/A\n"
1661                               "real-time_speed_phase2: N/A\n"
1662                               "current_position: N/A\n",
1663                               ns->ln_items_checked,
1664                               ns->ln_objs_checked_phase2,
1665                               ns->ln_items_repaired,
1666                               ns->ln_objs_repaired_phase2,
1667                               ns->ln_items_failed,
1668                               ns->ln_objs_failed_phase2,
1669                               ns->ln_dirs_checked,
1670                               ns->ln_mlinked_checked,
1671                               ns->ln_objs_nlink_repaired,
1672                               ns->ln_objs_lost_found,
1673                               ns->ln_success_count,
1674                               ns->ln_run_time_phase1,
1675                               ns->ln_run_time_phase2,
1676                               speed1,
1677                               speed2);
1678                 if (rc <= 0)
1679                         goto out;
1680
1681                 buf += rc;
1682                 len -= rc;
1683         }
1684         ret = save - len;
1685
1686 out:
1687         up_read(&com->lc_sem);
1688         return ret;
1689 }
1690
1691 static int mdd_lfsck_namespace_double_scan(const struct lu_env *env,
1692                                            struct lfsck_component *com)
1693 {
1694         struct md_lfsck         *lfsck  = com->lc_lfsck;
1695         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
1696         struct mdd_device       *mdd    = mdd_lfsck2mdd(lfsck);
1697         struct lfsck_bookmark   *bk     = &lfsck->ml_bookmark_ram;
1698         struct lfsck_namespace  *ns     =
1699                                 (struct lfsck_namespace *)com->lc_file_ram;
1700         struct dt_object        *obj    = com->lc_obj;
1701         const struct dt_it_ops  *iops   = &obj->do_index_ops->dio_it;
1702         struct mdd_object       *target;
1703         struct dt_it            *di;
1704         struct dt_key           *key;
1705         struct lu_fid            fid;
1706         int                      rc;
1707         __u8                     flags;
1708         ENTRY;
1709
1710         lfsck->ml_new_scanned = 0;
1711         lfsck->ml_time_last_checkpoint = cfs_time_current();
1712         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
1713                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1714
1715         di = iops->init(env, obj, 0, BYPASS_CAPA);
1716         if (IS_ERR(di))
1717                 RETURN(PTR_ERR(di));
1718
1719         fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
1720         rc = iops->get(env, di, (const struct dt_key *)&fid);
1721         if (rc < 0)
1722                 GOTO(fini, rc);
1723
1724         /* Skip the start one, which either has been processed or non-exist. */
1725         rc = iops->next(env, di);
1726         if (rc != 0)
1727                 GOTO(put, rc);
1728
1729         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1730                 GOTO(put, rc = 0);
1731
1732         do {
1733                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
1734                     cfs_fail_val > 0) {
1735                         struct l_wait_info lwi;
1736
1737                         lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
1738                                           NULL, NULL);
1739                         l_wait_event(thread->t_ctl_waitq,
1740                                      !thread_is_running(thread),
1741                                      &lwi);
1742                 }
1743
1744                 key = iops->key(env, di);
1745                 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
1746                 target = mdd_object_find(env, mdd, &fid);
1747                 down_write(&com->lc_sem);
1748                 if (target == NULL) {
1749                         rc = 0;
1750                         goto checkpoint;
1751                 } else if (IS_ERR(target)) {
1752                         rc = PTR_ERR(target);
1753                         goto checkpoint;
1754                 }
1755
1756                 /* XXX: Currently, skip remote object, the consistency for
1757                  *      remote object will be processed in LFSCK phase III. */
1758                 if (!mdd_object_exists(target) || mdd_object_remote(target))
1759                         goto obj_put;
1760
1761                 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
1762                 if (rc == 0)
1763                         rc = mdd_lfsck_namespace_double_scan_one(env, com,
1764                                                                  target, flags);
1765
1766 obj_put:
1767                 mdd_object_put(env, target);
1768
1769 checkpoint:
1770                 lfsck->ml_new_scanned++;
1771                 com->lc_new_checked++;
1772                 ns->ln_fid_latest_scanned_phase2 = fid;
1773                 if (rc > 0)
1774                         ns->ln_objs_repaired_phase2++;
1775                 else if (rc < 0)
1776                         ns->ln_objs_failed_phase2++;
1777                 up_write(&com->lc_sem);
1778
1779                 if ((rc == 0) || ((rc > 0) && !(bk->lb_param & LPF_DRYRUN))) {
1780                         mdd_lfsck_namespace_delete(env, com, &fid);
1781                 } else if (rc < 0) {
1782                         flags |= LLF_REPAIR_FAILED;
1783                         mdd_lfsck_namespace_update(env, com, &fid, flags, true);
1784                 }
1785
1786                 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1787                         GOTO(put, rc);
1788
1789                 if (likely(cfs_time_beforeq(cfs_time_current(),
1790                                             lfsck->ml_time_next_checkpoint)) ||
1791                     com->lc_new_checked == 0)
1792                         goto speed;
1793
1794                 down_write(&com->lc_sem);
1795                 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1796                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1797                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1798                 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1799                 com->lc_new_checked = 0;
1800                 rc = mdd_lfsck_namespace_store(env, com, false);
1801                 up_write(&com->lc_sem);
1802                 if (rc != 0)
1803                         GOTO(put, rc);
1804
1805                 lfsck->ml_time_last_checkpoint = cfs_time_current();
1806                 lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
1807                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1808
1809 speed:
1810                 mdd_lfsck_control_speed(lfsck);
1811                 if (unlikely(!thread_is_running(thread)))
1812                         GOTO(put, rc = 0);
1813
1814                 rc = iops->next(env, di);
1815         } while (rc == 0);
1816
1817         GOTO(put, rc);
1818
1819 put:
1820         iops->put(env, di);
1821
1822 fini:
1823         iops->fini(env, di);
1824         down_write(&com->lc_sem);
1825
1826         ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1827                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1828         ns->ln_time_last_checkpoint = cfs_time_current_sec();
1829         ns->ln_objs_checked_phase2 += com->lc_new_checked;
1830         com->lc_new_checked = 0;
1831
1832         if (rc > 0) {
1833                 com->lc_journal = 0;
1834                 ns->ln_status = LS_COMPLETED;
1835                 if (!(bk->lb_param & LPF_DRYRUN))
1836                         ns->ln_flags &=
1837                         ~(LF_SCANNED_ONCE | LF_INCONSISTENT | LF_UPGRADE);
1838                 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
1839                 ns->ln_success_count++;
1840         } else if (rc == 0) {
1841                 if (lfsck->ml_paused)
1842                         ns->ln_status = LS_PAUSED;
1843                 else
1844                         ns->ln_status = LS_STOPPED;
1845         } else {
1846                 ns->ln_status = LS_FAILED;
1847         }
1848
1849         if (ns->ln_status != LS_PAUSED) {
1850                 spin_lock(&lfsck->ml_lock);
1851                 cfs_list_del_init(&com->lc_link);
1852                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1853                 spin_unlock(&lfsck->ml_lock);
1854         }
1855
1856         rc = mdd_lfsck_namespace_store(env, com, false);
1857
1858         up_write(&com->lc_sem);
1859         return rc;
1860 }
1861
1862 static struct lfsck_operations mdd_lfsck_namespace_ops = {
1863         .lfsck_reset            = mdd_lfsck_namespace_reset,
1864         .lfsck_fail             = mdd_lfsck_namespace_fail,
1865         .lfsck_checkpoint       = mdd_lfsck_namespace_checkpoint,
1866         .lfsck_prep             = mdd_lfsck_namespace_prep,
1867         .lfsck_exec_oit         = mdd_lfsck_namespace_exec_oit,
1868         .lfsck_exec_dir         = mdd_lfsck_namespace_exec_dir,
1869         .lfsck_post             = mdd_lfsck_namespace_post,
1870         .lfsck_dump             = mdd_lfsck_namespace_dump,
1871         .lfsck_double_scan      = mdd_lfsck_namespace_double_scan,
1872 };
1873
1874 /* LFSCK component setup/cleanup functions */
1875
1876 static int mdd_lfsck_namespace_setup(const struct lu_env *env,
1877                                      struct md_lfsck *lfsck)
1878 {
1879         struct mdd_device       *mdd = mdd_lfsck2mdd(lfsck);
1880         struct lfsck_component  *com;
1881         struct lfsck_namespace  *ns;
1882         struct dt_object        *obj, *root;
1883         int                      rc;
1884         ENTRY;
1885
1886         OBD_ALLOC_PTR(com);
1887         if (com == NULL)
1888                 RETURN(-ENOMEM);
1889
1890         CFS_INIT_LIST_HEAD(&com->lc_link);
1891         CFS_INIT_LIST_HEAD(&com->lc_link_dir);
1892         init_rwsem(&com->lc_sem);
1893         atomic_set(&com->lc_ref, 1);
1894         com->lc_lfsck = lfsck;
1895         com->lc_type = LT_NAMESPACE;
1896         com->lc_ops = &mdd_lfsck_namespace_ops;
1897         com->lc_file_size = sizeof(struct lfsck_namespace);
1898         OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1899         if (com->lc_file_ram == NULL)
1900                 GOTO(out, rc = -ENOMEM);
1901
1902         OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1903         if (com->lc_file_disk == NULL)
1904                 GOTO(out, rc = -ENOMEM);
1905
1906         root = dt_locate(env, mdd->mdd_bottom, &mdd->mdd_local_root_fid);
1907         if (unlikely(IS_ERR(root)))
1908                 GOTO(out, rc = PTR_ERR(root));
1909
1910         obj = local_index_find_or_create(env, mdd->mdd_los, root,
1911                                          lfsck_namespace_name,
1912                                          S_IFREG | S_IRUGO | S_IWUSR,
1913                                          &dt_lfsck_features);
1914         lu_object_put(env, &root->do_lu);
1915         if (IS_ERR(obj))
1916                 GOTO(out, rc = PTR_ERR(obj));
1917
1918         com->lc_obj = obj;
1919         rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
1920         if (rc != 0)
1921                 GOTO(out, rc);
1922
1923         rc = mdd_lfsck_namespace_load(env, com);
1924         if (rc > 0)
1925                 rc = mdd_lfsck_namespace_reset(env, com, true);
1926         else if (rc == -ENODATA)
1927                 rc = mdd_lfsck_namespace_init(env, com);
1928         if (rc != 0)
1929                 GOTO(out, rc);
1930
1931         ns = (struct lfsck_namespace *)com->lc_file_ram;
1932         switch (ns->ln_status) {
1933         case LS_INIT:
1934         case LS_COMPLETED:
1935         case LS_FAILED:
1936         case LS_STOPPED:
1937                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1938                 break;
1939         default:
1940                 CERROR("%s: unknown status: %u\n",
1941                        mdd_lfsck2name(lfsck), ns->ln_status);
1942                 /* fall through */
1943         case LS_SCANNING_PHASE1:
1944         case LS_SCANNING_PHASE2:
1945                 /* No need to store the status to disk right now.
1946                  * If the system crashed before the status stored,
1947                  * it will be loaded back when next time. */
1948                 ns->ln_status = LS_CRASHED;
1949                 /* fall through */
1950         case LS_PAUSED:
1951         case LS_CRASHED:
1952                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_scan);
1953                 cfs_list_add_tail(&com->lc_link_dir, &lfsck->ml_list_dir);
1954                 break;
1955         }
1956
1957         GOTO(out, rc = 0);
1958
1959 out:
1960         if (rc != 0)
1961                 mdd_lfsck_component_cleanup(env, com);
1962         return rc;
1963 }
1964
1965 /* helper functions for framework */
1966
1967 static int object_needs_lfsck(const struct lu_env *env, struct mdd_device *mdd,
1968                               struct mdd_object *obj)
1969 {
1970         struct lu_fid *fid   = &mdd_env_info(env)->mti_fid;
1971         int            depth = 0;
1972         int            rc;
1973
1974         LASSERT(S_ISDIR(mdd_object_type(obj)));
1975
1976         while (1) {
1977                 if (mdd_is_root(mdd, mdo2fid(obj))) {
1978                         if (depth > 0)
1979                                 mdd_object_put(env, obj);
1980                         return 1;
1981                 }
1982
1983                 /* .lustre doesn't contain "real" user objects, no need lfsck */
1984                 if (fid_is_dot_lustre(mdo2fid(obj))) {
1985                         if (depth > 0)
1986                                 mdd_object_put(env, obj);
1987                         return 0;
1988                 }
1989
1990                 mdd_read_lock(env, obj, MOR_TGT_CHILD);
1991                 if (unlikely(mdd_is_dead_obj(obj))) {
1992                         mdd_read_unlock(env, obj);
1993                         if (depth > 0)
1994                                 mdd_object_put(env, obj);
1995                         return 0;
1996                 }
1997
1998                 rc = dt_xattr_get(env, mdd_object_child(obj),
1999                                   mdd_buf_get(env, NULL, 0), XATTR_NAME_LINK,
2000                                   BYPASS_CAPA);
2001                 mdd_read_unlock(env, obj);
2002                 if (rc >= 0) {
2003                         if (depth > 0)
2004                                 mdd_object_put(env, obj);
2005                         return 1;
2006                 }
2007
2008                 if (rc < 0 && rc != -ENODATA) {
2009                         if (depth > 0)
2010                                 mdd_object_put(env, obj);
2011                         return rc;
2012                 }
2013
2014                 rc = mdd_parent_fid(env, obj, fid);
2015                 if (depth > 0)
2016                         mdd_object_put(env, obj);
2017                 if (rc != 0)
2018                         return rc;
2019
2020                 if (unlikely(lu_fid_eq(fid, &mdd->mdd_local_root_fid)))
2021                         return 0;
2022
2023                 obj = mdd_object_find(env, mdd, fid);
2024                 if (obj == NULL)
2025                         return 0;
2026                 else if (IS_ERR(obj))
2027                         return PTR_ERR(obj);
2028
2029                 if (!mdd_object_exists(obj)) {
2030                         mdd_object_put(env, obj);
2031                         return 0;
2032                 }
2033
2034                 /* Currently, only client visible directory can be remote. */
2035                 if (mdd_object_remote(obj)) {
2036                         mdd_object_put(env, obj);
2037                         return 1;
2038                 }
2039
2040                 depth++;
2041         }
2042         return 0;
2043 }
2044
2045 static void mdd_lfsck_unpack_ent(struct lu_dirent *ent)
2046 {
2047         fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
2048         ent->lde_hash = le64_to_cpu(ent->lde_hash);
2049         ent->lde_reclen = le16_to_cpu(ent->lde_reclen);
2050         ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
2051         ent->lde_attrs = le32_to_cpu(ent->lde_attrs);
2052
2053         /* Make sure the name is terminated with '0'.
2054          * The data (type) after ent::lde_name maybe
2055          * broken, but we do not care. */
2056         ent->lde_name[ent->lde_namelen] = 0;
2057 }
2058
2059 /* LFSCK wrap functions */
2060
2061 static void mdd_lfsck_fail(const struct lu_env *env, struct md_lfsck *lfsck,
2062                            bool new_checked)
2063 {
2064         struct lfsck_component *com;
2065
2066         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2067                 com->lc_ops->lfsck_fail(env, com, new_checked);
2068         }
2069 }
2070
2071 static int mdd_lfsck_checkpoint(const struct lu_env *env,
2072                                 struct md_lfsck *lfsck)
2073 {
2074         struct lfsck_component *com;
2075         int                     rc;
2076
2077         if (likely(cfs_time_beforeq(cfs_time_current(),
2078                                     lfsck->ml_time_next_checkpoint)))
2079                 return 0;
2080
2081         mdd_lfsck_pos_fill(env, lfsck, &lfsck->ml_pos_current, false);
2082         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2083                 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
2084                 if (rc != 0)
2085                         return rc;;
2086         }
2087
2088         lfsck->ml_time_last_checkpoint = cfs_time_current();
2089         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
2090                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2091         return 0;
2092 }
2093
2094 static int mdd_lfsck_prep(struct lu_env *env, struct md_lfsck *lfsck)
2095 {
2096         struct mdd_device      *mdd     = mdd_lfsck2mdd(lfsck);
2097         struct mdd_object      *obj     = NULL;
2098         struct dt_object       *dt_obj;
2099         struct lfsck_component *com;
2100         struct lfsck_component *next;
2101         struct lfsck_position  *pos     = NULL;
2102         const struct dt_it_ops *iops    =
2103                                 &lfsck->ml_obj_oit->do_index_ops->dio_it;
2104         struct dt_it           *di;
2105         int                     rc;
2106         ENTRY;
2107
2108         LASSERT(lfsck->ml_obj_dir == NULL);
2109         LASSERT(lfsck->ml_di_dir == NULL);
2110
2111         lfsck->ml_current_oit_processed = 0;
2112         cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_scan, lc_link) {
2113                 com->lc_new_checked = 0;
2114                 if (lfsck->ml_bookmark_ram.lb_param & LPF_DRYRUN)
2115                         com->lc_journal = 0;
2116
2117                 rc = com->lc_ops->lfsck_prep(env, com);
2118                 if (rc != 0)
2119                         RETURN(rc);
2120
2121                 if ((pos == NULL) ||
2122                     (!mdd_lfsck_pos_is_zero(&com->lc_pos_start) &&
2123                      mdd_lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
2124                         pos = &com->lc_pos_start;
2125         }
2126
2127         /* Init otable-based iterator. */
2128         if (pos == NULL) {
2129                 rc = iops->load(env, lfsck->ml_di_oit, 0);
2130                 if (rc > 0) {
2131                         lfsck->ml_oit_over = 1;
2132                         rc = 0;
2133                 }
2134
2135                 GOTO(out, rc);
2136         }
2137
2138         rc = iops->load(env, lfsck->ml_di_oit, pos->lp_oit_cookie);
2139         if (rc < 0)
2140                 GOTO(out, rc);
2141         else if (rc > 0)
2142                 lfsck->ml_oit_over = 1;
2143
2144         if (fid_is_zero(&pos->lp_dir_parent))
2145                 GOTO(out, rc = 0);
2146
2147         /* Find the directory for namespace-based traverse. */
2148         obj = mdd_object_find(env, mdd, &pos->lp_dir_parent);
2149         if (obj == NULL)
2150                 GOTO(out, rc = 0);
2151         else if (IS_ERR(obj))
2152                 RETURN(PTR_ERR(obj));
2153
2154         /* XXX: Currently, skip remote object, the consistency for
2155          *      remote object will be processed in LFSCK phase III. */
2156         if (!mdd_object_exists(obj) || mdd_object_remote(obj) ||
2157             unlikely(!S_ISDIR(mdd_object_type(obj))))
2158                 GOTO(out, rc = 0);
2159
2160         if (unlikely(mdd_is_dead_obj(obj)))
2161                 GOTO(out, rc = 0);
2162
2163         dt_obj = mdd_object_child(obj);
2164         if (unlikely(!dt_try_as_dir(env, dt_obj)))
2165                 GOTO(out, rc = -ENOTDIR);
2166
2167         /* Init the namespace-based directory traverse. */
2168         iops = &dt_obj->do_index_ops->dio_it;
2169         di = iops->init(env, dt_obj, lfsck->ml_args_dir, BYPASS_CAPA);
2170         if (IS_ERR(di))
2171                 GOTO(out, rc = PTR_ERR(di));
2172
2173         LASSERT(pos->lp_dir_cookie < MDS_DIR_END_OFF);
2174
2175         rc = iops->load(env, di, pos->lp_dir_cookie);
2176         if ((rc == 0) || (rc > 0 && pos->lp_dir_cookie > 0))
2177                 rc = iops->next(env, di);
2178         else if (rc > 0)
2179                 rc = 0;
2180
2181         if (rc != 0) {
2182                 iops->put(env, di);
2183                 iops->fini(env, di);
2184                 GOTO(out, rc);
2185         }
2186
2187         lfsck->ml_obj_dir = dt_obj;
2188         spin_lock(&lfsck->ml_lock);
2189         lfsck->ml_di_dir = di;
2190         spin_unlock(&lfsck->ml_lock);
2191         obj = NULL;
2192
2193         GOTO(out, rc = 0);
2194
2195 out:
2196         if (obj != NULL)
2197                 mdd_object_put(env, obj);
2198
2199         if (rc < 0) {
2200                 cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_scan,
2201                                              lc_link)
2202                         com->lc_ops->lfsck_post(env, com, rc, true);
2203
2204                 return rc;
2205         }
2206
2207         rc = 0;
2208         mdd_lfsck_pos_fill(env, lfsck, &lfsck->ml_pos_current, true);
2209         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2210                 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
2211                 if (rc != 0)
2212                         break;
2213         }
2214
2215         lfsck->ml_time_last_checkpoint = cfs_time_current();
2216         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
2217                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2218         return rc;
2219 }
2220
2221 static int mdd_lfsck_exec_oit(const struct lu_env *env, struct md_lfsck *lfsck,
2222                               struct mdd_object *obj)
2223 {
2224         struct lfsck_component *com;
2225         struct dt_object       *dt_obj;
2226         const struct dt_it_ops *iops;
2227         struct dt_it           *di;
2228         int                     rc;
2229         ENTRY;
2230
2231         LASSERT(lfsck->ml_obj_dir == NULL);
2232
2233         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2234                 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
2235                 if (rc != 0)
2236                         RETURN(rc);
2237         }
2238
2239         if (!S_ISDIR(mdd_object_type(obj)) ||
2240             cfs_list_empty(&lfsck->ml_list_dir))
2241                RETURN(0);
2242
2243         rc = object_needs_lfsck(env, mdd_lfsck2mdd(lfsck), obj);
2244         if (rc <= 0)
2245                 GOTO(out, rc);
2246
2247         if (unlikely(mdd_is_dead_obj(obj)))
2248                 GOTO(out, rc = 0);
2249
2250         dt_obj = mdd_object_child(obj);
2251         if (unlikely(!dt_try_as_dir(env, dt_obj)))
2252                 GOTO(out, rc = -ENOTDIR);
2253
2254         iops = &dt_obj->do_index_ops->dio_it;
2255         di = iops->init(env, dt_obj, lfsck->ml_args_dir, BYPASS_CAPA);
2256         if (IS_ERR(di))
2257                 GOTO(out, rc = PTR_ERR(di));
2258
2259         rc = iops->load(env, di, 0);
2260         if (rc == 0)
2261                 rc = iops->next(env, di);
2262         else if (rc > 0)
2263                 rc = 0;
2264
2265         if (rc != 0) {
2266                 iops->put(env, di);
2267                 iops->fini(env, di);
2268                 GOTO(out, rc);
2269         }
2270
2271         mdd_object_get(obj);
2272         lfsck->ml_obj_dir = dt_obj;
2273         spin_lock(&lfsck->ml_lock);
2274         lfsck->ml_di_dir = di;
2275         spin_unlock(&lfsck->ml_lock);
2276
2277         GOTO(out, rc = 0);
2278
2279 out:
2280         if (rc < 0)
2281                 mdd_lfsck_fail(env, lfsck, false);
2282         return (rc > 0 ? 0 : rc);
2283 }
2284
2285 static int mdd_lfsck_exec_dir(const struct lu_env *env, struct md_lfsck *lfsck,
2286                               struct mdd_object *obj, struct lu_dirent *ent)
2287 {
2288         struct lfsck_component *com;
2289         int                     rc;
2290
2291         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2292                 rc = com->lc_ops->lfsck_exec_dir(env, com, obj, ent);
2293                 if (rc != 0)
2294                         return rc;
2295         }
2296         return 0;
2297 }
2298
2299 static int mdd_lfsck_post(const struct lu_env *env, struct md_lfsck *lfsck,
2300                           int result)
2301 {
2302         struct lfsck_component *com;
2303         struct lfsck_component *next;
2304         int                     rc;
2305
2306         mdd_lfsck_pos_fill(env, lfsck, &lfsck->ml_pos_current, false);
2307         cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_scan, lc_link) {
2308                 rc = com->lc_ops->lfsck_post(env, com, result, false);
2309                 if (rc != 0)
2310                         return rc;
2311         }
2312
2313         lfsck->ml_time_last_checkpoint = cfs_time_current();
2314         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
2315                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2316         return result;
2317 }
2318
2319 static int mdd_lfsck_double_scan(const struct lu_env *env,
2320                                  struct md_lfsck *lfsck)
2321 {
2322         struct lfsck_component *com;
2323         struct lfsck_component *next;
2324         int                     rc;
2325
2326         cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_double_scan,
2327                                      lc_link) {
2328                 if (lfsck->ml_bookmark_ram.lb_param & LPF_DRYRUN)
2329                         com->lc_journal = 0;
2330
2331                 rc = com->lc_ops->lfsck_double_scan(env, com);
2332                 if (rc != 0)
2333                         return rc;
2334         }
2335         return 0;
2336 }
2337
2338 /* LFSCK engines */
2339
2340 static int mdd_lfsck_dir_engine(const struct lu_env *env,
2341                                 struct md_lfsck *lfsck)
2342 {
2343         struct mdd_thread_info  *info   = mdd_env_info(env);
2344         struct mdd_device       *mdd    = mdd_lfsck2mdd(lfsck);
2345         const struct dt_it_ops  *iops   =
2346                         &lfsck->ml_obj_dir->do_index_ops->dio_it;
2347         struct dt_it            *di     = lfsck->ml_di_dir;
2348         struct lu_dirent        *ent    = &info->mti_ent;
2349         struct lu_fid           *fid    = &info->mti_fid;
2350         struct lfsck_bookmark   *bk     = &lfsck->ml_bookmark_ram;
2351         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
2352         int                      rc;
2353         ENTRY;
2354
2355         do {
2356                 struct mdd_object *child;
2357
2358                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY2) &&
2359                     cfs_fail_val > 0) {
2360                         struct l_wait_info lwi;
2361
2362                         lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2363                                           NULL, NULL);
2364                         l_wait_event(thread->t_ctl_waitq,
2365                                      !thread_is_running(thread),
2366                                      &lwi);
2367                 }
2368
2369                 lfsck->ml_new_scanned++;
2370                 rc = iops->rec(env, di, (struct dt_rec *)ent,
2371                                lfsck->ml_args_dir);
2372                 if (rc != 0) {
2373                         mdd_lfsck_fail(env, lfsck, true);
2374                         if (bk->lb_param & LPF_FAILOUT)
2375                                 RETURN(rc);
2376                         else
2377                                 goto checkpoint;
2378                 }
2379
2380                 mdd_lfsck_unpack_ent(ent);
2381                 if (ent->lde_attrs & LUDA_IGNORE)
2382                         goto checkpoint;
2383
2384                 *fid = ent->lde_fid;
2385                 child = mdd_object_find(env, mdd, fid);
2386                 if (child == NULL) {
2387                         goto checkpoint;
2388                 } else if (IS_ERR(child)) {
2389                         mdd_lfsck_fail(env, lfsck, true);
2390                         if (bk->lb_param & LPF_FAILOUT)
2391                                 RETURN(PTR_ERR(child));
2392                         else
2393                                 goto checkpoint;
2394                 }
2395
2396                 /* XXX: Currently, skip remote object, the consistency for
2397                  *      remote object will be processed in LFSCK phase III. */
2398                 if (mdd_object_exists(child) && !mdd_object_remote(child))
2399                         rc = mdd_lfsck_exec_dir(env, lfsck, child, ent);
2400                 mdd_object_put(env, child);
2401                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2402                         RETURN(rc);
2403
2404 checkpoint:
2405                 rc = mdd_lfsck_checkpoint(env, lfsck);
2406                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2407                         RETURN(rc);
2408
2409                 /* Rate control. */
2410                 mdd_lfsck_control_speed(lfsck);
2411                 if (unlikely(!thread_is_running(thread)))
2412                         RETURN(0);
2413
2414                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
2415                         spin_lock(&lfsck->ml_lock);
2416                         thread_set_flags(thread, SVC_STOPPING);
2417                         spin_unlock(&lfsck->ml_lock);
2418                         RETURN(-EINVAL);
2419                 }
2420
2421                 rc = iops->next(env, di);
2422         } while (rc == 0);
2423
2424         if (rc > 0 && !lfsck->ml_oit_over)
2425                 mdd_lfsck_close_dir(env, lfsck);
2426
2427         RETURN(rc);
2428 }
2429
2430 static int mdd_lfsck_oit_engine(const struct lu_env *env,
2431                                 struct md_lfsck *lfsck)
2432 {
2433         struct mdd_thread_info  *info   = mdd_env_info(env);
2434         struct mdd_device       *mdd    = mdd_lfsck2mdd(lfsck);
2435         const struct dt_it_ops  *iops   =
2436                                 &lfsck->ml_obj_oit->do_index_ops->dio_it;
2437         struct dt_it            *di     = lfsck->ml_di_oit;
2438         struct lu_fid           *fid    = &info->mti_fid;
2439         struct lfsck_bookmark   *bk     = &lfsck->ml_bookmark_ram;
2440         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
2441         int                      rc;
2442         ENTRY;
2443
2444         do {
2445                 struct mdd_object *target;
2446
2447                 if (lfsck->ml_di_dir != NULL) {
2448                         rc = mdd_lfsck_dir_engine(env, lfsck);
2449                         if (rc <= 0)
2450                                 RETURN(rc);
2451                 }
2452
2453                 if (unlikely(lfsck->ml_oit_over))
2454                         RETURN(1);
2455
2456                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY1) &&
2457                     cfs_fail_val > 0) {
2458                         struct l_wait_info lwi;
2459
2460                         lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2461                                           NULL, NULL);
2462                         l_wait_event(thread->t_ctl_waitq,
2463                                      !thread_is_running(thread),
2464                                      &lwi);
2465                 }
2466
2467                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
2468                         RETURN(0);
2469
2470                 lfsck->ml_current_oit_processed = 1;
2471                 lfsck->ml_new_scanned++;
2472                 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
2473                 if (rc != 0) {
2474                         mdd_lfsck_fail(env, lfsck, true);
2475                         if (bk->lb_param & LPF_FAILOUT)
2476                                 RETURN(rc);
2477                         else
2478                                 goto checkpoint;
2479                 }
2480
2481                 target = mdd_object_find(env, mdd, fid);
2482                 if (target == NULL) {
2483                         goto checkpoint;
2484                 } else if (IS_ERR(target)) {
2485                         mdd_lfsck_fail(env, lfsck, true);
2486                         if (bk->lb_param & LPF_FAILOUT)
2487                                 RETURN(PTR_ERR(target));
2488                         else
2489                                 goto checkpoint;
2490                 }
2491
2492                 /* XXX: Currently, skip remote object, the consistency for
2493                  *      remote object will be processed in LFSCK phase III. */
2494                 if (mdd_object_exists(target) && !mdd_object_remote(target))
2495                         rc = mdd_lfsck_exec_oit(env, lfsck, target);
2496                 mdd_object_put(env, target);
2497                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2498                         RETURN(rc);
2499
2500 checkpoint:
2501                 rc = mdd_lfsck_checkpoint(env, lfsck);
2502                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2503                         RETURN(rc);
2504
2505                 /* Rate control. */
2506                 mdd_lfsck_control_speed(lfsck);
2507
2508                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
2509                         spin_lock(&lfsck->ml_lock);
2510                         thread_set_flags(thread, SVC_STOPPING);
2511                         spin_unlock(&lfsck->ml_lock);
2512                         RETURN(-EINVAL);
2513                 }
2514
2515                 rc = iops->next(env, di);
2516                 if (unlikely(rc > 0))
2517                         lfsck->ml_oit_over = 1;
2518                 else if (likely(rc == 0))
2519                         lfsck->ml_current_oit_processed = 0;
2520
2521                 if (unlikely(!thread_is_running(thread)))
2522                         RETURN(0);
2523         } while (rc == 0 || lfsck->ml_di_dir != NULL);
2524
2525         RETURN(rc);
2526 }
2527
2528 static int mdd_lfsck_main(void *args)
2529 {
2530         struct lu_env            env;
2531         struct md_lfsck         *lfsck    = (struct md_lfsck *)args;
2532         struct ptlrpc_thread    *thread   = &lfsck->ml_thread;
2533         struct dt_object        *oit_obj  = lfsck->ml_obj_oit;
2534         const struct dt_it_ops  *oit_iops = &oit_obj->do_index_ops->dio_it;
2535         struct dt_it            *oit_di;
2536         int                      rc;
2537         ENTRY;
2538
2539         cfs_daemonize("lfsck");
2540         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2541         if (rc != 0) {
2542                 CERROR("%s: LFSCK, fail to init env, rc = %d\n",
2543                        mdd_lfsck2name(lfsck), rc);
2544                 GOTO(noenv, rc);
2545         }
2546
2547         oit_di = oit_iops->init(&env, oit_obj, lfsck->ml_args_oit, BYPASS_CAPA);
2548         if (IS_ERR(oit_di)) {
2549                 rc = PTR_ERR(oit_di);
2550                 CERROR("%s: LFSCK, fail to init iteration, rc = %d\n",
2551                        mdd_lfsck2name(lfsck), rc);
2552                 GOTO(fini_env, rc);
2553         }
2554
2555         spin_lock(&lfsck->ml_lock);
2556         lfsck->ml_di_oit = oit_di;
2557         spin_unlock(&lfsck->ml_lock);
2558         rc = mdd_lfsck_prep(&env, lfsck);
2559         if (rc != 0)
2560                 GOTO(fini_oit, rc);
2561
2562         CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = 0x%x, dir_flags = 0x%x, "
2563                "oit_cookie = "LPU64", dir_cookie = "LPU64", parent = "DFID
2564                ", pid = %d\n", lfsck->ml_args_oit, lfsck->ml_args_dir,
2565                lfsck->ml_pos_current.lp_oit_cookie,
2566                lfsck->ml_pos_current.lp_dir_cookie,
2567                PFID(&lfsck->ml_pos_current.lp_dir_parent),
2568                cfs_curproc_pid());
2569
2570         spin_lock(&lfsck->ml_lock);
2571         thread_set_flags(thread, SVC_RUNNING);
2572         spin_unlock(&lfsck->ml_lock);
2573         cfs_waitq_broadcast(&thread->t_ctl_waitq);
2574
2575         if (!cfs_list_empty(&lfsck->ml_list_scan) ||
2576             cfs_list_empty(&lfsck->ml_list_double_scan))
2577                 rc = mdd_lfsck_oit_engine(&env, lfsck);
2578         else
2579                 rc = 1;
2580
2581         CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = 0x%x, dir_flags = 0x%x, "
2582                "oit_cookie = "LPU64", dir_cookie = "LPU64", parent = "DFID
2583                ", pid = %d, rc = %d\n", lfsck->ml_args_oit, lfsck->ml_args_dir,
2584                lfsck->ml_pos_current.lp_oit_cookie,
2585                lfsck->ml_pos_current.lp_dir_cookie,
2586                PFID(&lfsck->ml_pos_current.lp_dir_parent),
2587                cfs_curproc_pid(), rc);
2588
2589         if (lfsck->ml_paused && cfs_list_empty(&lfsck->ml_list_scan))
2590                 oit_iops->put(&env, oit_di);
2591
2592         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
2593                 rc = mdd_lfsck_post(&env, lfsck, rc);
2594         if (lfsck->ml_di_dir != NULL)
2595                 mdd_lfsck_close_dir(&env, lfsck);
2596
2597 fini_oit:
2598         spin_lock(&lfsck->ml_lock);
2599         lfsck->ml_di_oit = NULL;
2600         spin_unlock(&lfsck->ml_lock);
2601
2602         oit_iops->fini(&env, oit_di);
2603         if (rc == 1) {
2604                 if (!cfs_list_empty(&lfsck->ml_list_double_scan))
2605                         rc = mdd_lfsck_double_scan(&env, lfsck);
2606                 else
2607                         rc = 0;
2608         }
2609
2610         /* XXX: Purge the pinned objects in the future. */
2611
2612 fini_env:
2613         lu_env_fini(&env);
2614
2615 noenv:
2616         spin_lock(&lfsck->ml_lock);
2617         thread_set_flags(thread, SVC_STOPPED);
2618         cfs_waitq_broadcast(&thread->t_ctl_waitq);
2619         spin_unlock(&lfsck->ml_lock);
2620         return rc;
2621 }
2622
2623 /* external interfaces */
2624
2625 int mdd_lfsck_set_speed(const struct lu_env *env, struct md_lfsck *lfsck,
2626                         __u32 limit)
2627 {
2628         int rc;
2629
2630         mutex_lock(&lfsck->ml_mutex);
2631         __mdd_lfsck_set_speed(lfsck, limit);
2632         rc = mdd_lfsck_bookmark_store(env, lfsck);
2633         mutex_unlock(&lfsck->ml_mutex);
2634         return rc;
2635 }
2636
2637 int mdd_lfsck_dump(const struct lu_env *env, struct md_lfsck *lfsck,
2638                    __u16 type, char *buf, int len)
2639 {
2640         struct lfsck_component *com;
2641         int                     rc;
2642
2643         if (!lfsck->ml_initialized)
2644                 return -ENODEV;
2645
2646         com = mdd_lfsck_component_find(lfsck, type);
2647         if (com == NULL)
2648                 return -ENOTSUPP;
2649
2650         rc = com->lc_ops->lfsck_dump(env, com, buf, len);
2651         mdd_lfsck_component_put(env, com);
2652         return rc;
2653 }
2654
2655 int mdd_lfsck_start(const struct lu_env *env, struct md_lfsck *lfsck,
2656                     struct lfsck_start *start)
2657 {
2658         struct lfsck_bookmark  *bk     = &lfsck->ml_bookmark_ram;
2659         struct ptlrpc_thread   *thread = &lfsck->ml_thread;
2660         struct lfsck_component *com;
2661         struct l_wait_info      lwi    = { 0 };
2662         bool                    dirty  = false;
2663         int                     rc     = 0;
2664         __u16                   valid  = 0;
2665         __u16                   flags  = 0;
2666         ENTRY;
2667
2668         if (lfsck->ml_obj_oit == NULL)
2669                 RETURN(-ENOTSUPP);
2670
2671         /* start == NULL means auto trigger paused LFSCK. */
2672         if ((start == NULL) &&
2673             (cfs_list_empty(&lfsck->ml_list_scan) ||
2674              OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2675                 RETURN(0);
2676
2677         mutex_lock(&lfsck->ml_mutex);
2678         spin_lock(&lfsck->ml_lock);
2679         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2680                 spin_unlock(&lfsck->ml_lock);
2681                 mutex_unlock(&lfsck->ml_mutex);
2682                 RETURN(-EALREADY);
2683         }
2684
2685         spin_unlock(&lfsck->ml_lock);
2686
2687         lfsck->ml_paused = 0;
2688         lfsck->ml_oit_over = 0;
2689         lfsck->ml_drop_dryrun = 0;
2690         lfsck->ml_new_scanned = 0;
2691
2692         /* For auto trigger. */
2693         if (start == NULL)
2694                 goto trigger;
2695
2696         start->ls_version = bk->lb_version;
2697         if (start->ls_valid & LSV_SPEED_LIMIT) {
2698                 __mdd_lfsck_set_speed(lfsck, start->ls_speed_limit);
2699                 dirty = true;
2700         }
2701
2702         if (start->ls_valid & LSV_ERROR_HANDLE) {
2703                 valid |= DOIV_ERROR_HANDLE;
2704                 if (start->ls_flags & LPF_FAILOUT)
2705                         flags |= DOIF_FAILOUT;
2706
2707                 if ((start->ls_flags & LPF_FAILOUT) &&
2708                     !(bk->lb_param & LPF_FAILOUT)) {
2709                         bk->lb_param |= LPF_FAILOUT;
2710                         dirty = true;
2711                 } else if (!(start->ls_flags & LPF_FAILOUT) &&
2712                            (bk->lb_param & LPF_FAILOUT)) {
2713                         bk->lb_param &= ~LPF_FAILOUT;
2714                         dirty = true;
2715                 }
2716         }
2717
2718         if (start->ls_valid & LSV_DRYRUN) {
2719                 if ((start->ls_flags & LPF_DRYRUN) &&
2720                     !(bk->lb_param & LPF_DRYRUN)) {
2721                         bk->lb_param |= LPF_DRYRUN;
2722                         dirty = true;
2723                 } else if (!(start->ls_flags & LPF_DRYRUN) &&
2724                            (bk->lb_param & LPF_DRYRUN)) {
2725                         bk->lb_param &= ~LPF_DRYRUN;
2726                         lfsck->ml_drop_dryrun = 1;
2727                         dirty = true;
2728                 }
2729         }
2730
2731         if (dirty) {
2732                 rc = mdd_lfsck_bookmark_store(env, lfsck);
2733                 if (rc != 0)
2734                         GOTO(out, rc);
2735         }
2736
2737         if (start->ls_flags & LPF_RESET)
2738                 flags |= DOIF_RESET;
2739
2740         if (start->ls_active != 0) {
2741                 struct lfsck_component *next;
2742                 __u16 type = 1;
2743
2744                 if (start->ls_active == LFSCK_TYPES_ALL)
2745                         start->ls_active = LFSCK_TYPES_SUPPORTED;
2746
2747                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2748                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2749                         GOTO(out, rc = -ENOTSUPP);
2750                 }
2751
2752                 cfs_list_for_each_entry_safe(com, next,
2753                                              &lfsck->ml_list_scan, lc_link) {
2754                         if (!(com->lc_type & start->ls_active)) {
2755                                 rc = com->lc_ops->lfsck_post(env, com, 0,
2756                                                              false);
2757                                 if (rc != 0)
2758                                         GOTO(out, rc);
2759                         }
2760                 }
2761
2762                 while (start->ls_active != 0) {
2763                         if (type & start->ls_active) {
2764                                 com = __mdd_lfsck_component_find(lfsck, type,
2765                                                         &lfsck->ml_list_idle);
2766                                 if (com != NULL) {
2767                                         /* The component status will be updated
2768                                          * when its prep() is called later by
2769                                          * the LFSCK main engine. */
2770                                         cfs_list_del_init(&com->lc_link);
2771                                         cfs_list_add_tail(&com->lc_link,
2772                                                           &lfsck->ml_list_scan);
2773                                 }
2774                                 start->ls_active &= ~type;
2775                         }
2776                         type <<= 1;
2777                 }
2778         }
2779
2780         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2781                 start->ls_active |= com->lc_type;
2782                 if (flags & DOIF_RESET) {
2783                         rc = com->lc_ops->lfsck_reset(env, com, false);
2784                         if (rc != 0)
2785                                 GOTO(out, rc);
2786                 }
2787         }
2788
2789 trigger:
2790         lfsck->ml_args_dir = LUDA_64BITHASH | LUDA_VERIFY;
2791         if (bk->lb_param & LPF_DRYRUN)
2792                 lfsck->ml_args_dir |= LUDA_VERIFY_DRYRUN;
2793
2794         if (bk->lb_param & LPF_FAILOUT) {
2795                 valid |= DOIV_ERROR_HANDLE;
2796                 flags |= DOIF_FAILOUT;
2797         }
2798
2799         if (!cfs_list_empty(&lfsck->ml_list_scan))
2800                 flags |= DOIF_OUTUSED;
2801
2802         lfsck->ml_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2803         thread_set_flags(thread, 0);
2804         rc = cfs_create_thread(mdd_lfsck_main, lfsck, 0);
2805         if (rc < 0)
2806                 CERROR("%s: cannot start LFSCK thread, rc = %d\n",
2807                        mdd_lfsck2name(lfsck), rc);
2808         else
2809                 l_wait_event(thread->t_ctl_waitq,
2810                              thread_is_running(thread) ||
2811                              thread_is_stopped(thread),
2812                              &lwi);
2813
2814         GOTO(out, rc = 0);
2815
2816 out:
2817         mutex_unlock(&lfsck->ml_mutex);
2818         return (rc < 0 ? rc : 0);
2819 }
2820
2821 int mdd_lfsck_stop(const struct lu_env *env, struct md_lfsck *lfsck,
2822                    bool pause)
2823 {
2824         struct ptlrpc_thread *thread = &lfsck->ml_thread;
2825         struct l_wait_info    lwi    = { 0 };
2826         ENTRY;
2827
2828         if (!lfsck->ml_initialized)
2829                 RETURN(0);
2830
2831         mutex_lock(&lfsck->ml_mutex);
2832         spin_lock(&lfsck->ml_lock);
2833         if (thread_is_init(thread) || thread_is_stopped(thread)) {
2834                 spin_unlock(&lfsck->ml_lock);
2835                 mutex_unlock(&lfsck->ml_mutex);
2836                 RETURN(-EALREADY);
2837         }
2838
2839         if (pause)
2840                 lfsck->ml_paused = 1;
2841         thread_set_flags(thread, SVC_STOPPING);
2842         /* The LFSCK thread may be sleeping on low layer wait queue,
2843          * wake it up. */
2844         if (likely(lfsck->ml_di_oit != NULL))
2845                 lfsck->ml_obj_oit->do_index_ops->dio_it.put(env,
2846                                                             lfsck->ml_di_oit);
2847         spin_unlock(&lfsck->ml_lock);
2848
2849         cfs_waitq_broadcast(&thread->t_ctl_waitq);
2850         l_wait_event(thread->t_ctl_waitq,
2851                      thread_is_stopped(thread),
2852                      &lwi);
2853         mutex_unlock(&lfsck->ml_mutex);
2854
2855         RETURN(0);
2856 }
2857
2858 static const struct lu_fid lfsck_it_fid = { .f_seq = FID_SEQ_LOCAL_FILE,
2859                                             .f_oid = OTABLE_IT_OID,
2860                                             .f_ver = 0 };
2861
2862 int mdd_lfsck_setup(const struct lu_env *env, struct mdd_device *mdd)
2863 {
2864         struct md_lfsck         *lfsck = &mdd->mdd_lfsck;
2865         struct dt_object        *obj;
2866         struct lu_fid            fid;
2867         int                      rc;
2868
2869         ENTRY;
2870
2871         LASSERT(!lfsck->ml_initialized);
2872
2873         lfsck->ml_initialized = 1;
2874         mutex_init(&lfsck->ml_mutex);
2875         spin_lock_init(&lfsck->ml_lock);
2876         CFS_INIT_LIST_HEAD(&lfsck->ml_list_scan);
2877         CFS_INIT_LIST_HEAD(&lfsck->ml_list_dir);
2878         CFS_INIT_LIST_HEAD(&lfsck->ml_list_double_scan);
2879         CFS_INIT_LIST_HEAD(&lfsck->ml_list_idle);
2880         cfs_waitq_init(&lfsck->ml_thread.t_ctl_waitq);
2881
2882         obj = dt_locate(env, mdd->mdd_bottom, &lfsck_it_fid);
2883         if (IS_ERR(obj))
2884                 RETURN(PTR_ERR(obj));
2885
2886         lfsck->ml_obj_oit = obj;
2887         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
2888         if (rc != 0) {
2889                 if (rc == -ENOTSUPP)
2890                         RETURN(0);
2891                 GOTO(out, rc);
2892         }
2893
2894         /* LFSCK bookmark */
2895         fid_zero(&fid);
2896         rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
2897                                    lfsck_bookmark_name,
2898                                    S_IFREG | S_IRUGO | S_IWUSR, &fid);
2899         if (rc < 0)
2900                 GOTO(out, rc);
2901
2902         obj = dt_locate(env, mdd->mdd_bottom, &fid);
2903         if (IS_ERR(obj))
2904                 GOTO(out, rc = PTR_ERR(obj));
2905
2906         LASSERT(lu_object_exists(&obj->do_lu));
2907         lfsck->ml_bookmark_obj = obj;
2908
2909         rc = mdd_lfsck_bookmark_load(env, lfsck);
2910         if (rc == -ENODATA)
2911                 rc = mdd_lfsck_bookmark_init(env, lfsck);
2912         if (rc != 0)
2913                 GOTO(out, rc);
2914
2915         rc = mdd_lfsck_namespace_setup(env, lfsck);
2916         if (rc < 0)
2917                 GOTO(out, rc);
2918         /* XXX: LFSCK components initialization to be added here. */
2919         RETURN(0);
2920 out:
2921         lu_object_put(env, &lfsck->ml_obj_oit->do_lu);
2922         lfsck->ml_obj_oit = NULL;
2923         return 0;
2924 }
2925
2926 void mdd_lfsck_cleanup(const struct lu_env *env, struct mdd_device *mdd)
2927 {
2928         struct md_lfsck         *lfsck  = &mdd->mdd_lfsck;
2929         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
2930         struct lfsck_component  *com;
2931
2932         if (!lfsck->ml_initialized)
2933                 return;
2934
2935         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
2936
2937         if (lfsck->ml_obj_oit != NULL) {
2938                 lu_object_put(env, &lfsck->ml_obj_oit->do_lu);
2939                 lfsck->ml_obj_oit = NULL;
2940         }
2941
2942         LASSERT(lfsck->ml_obj_dir == NULL);
2943
2944         if (lfsck->ml_bookmark_obj != NULL) {
2945                 lu_object_put(env, &lfsck->ml_bookmark_obj->do_lu);
2946                 lfsck->ml_bookmark_obj = NULL;
2947         }
2948
2949         while (!cfs_list_empty(&lfsck->ml_list_scan)) {
2950                 com = cfs_list_entry(lfsck->ml_list_scan.next,
2951                                      struct lfsck_component,
2952                                      lc_link);
2953                 mdd_lfsck_component_cleanup(env, com);
2954         }
2955
2956         LASSERT(cfs_list_empty(&lfsck->ml_list_dir));
2957
2958         while (!cfs_list_empty(&lfsck->ml_list_double_scan)) {
2959                 com = cfs_list_entry(lfsck->ml_list_double_scan.next,
2960                                      struct lfsck_component,
2961                                      lc_link);
2962                 mdd_lfsck_component_cleanup(env, com);
2963         }
2964
2965         while (!cfs_list_empty(&lfsck->ml_list_idle)) {
2966                 com = cfs_list_entry(lfsck->ml_list_idle.next,
2967                                      struct lfsck_component,
2968                                      lc_link);
2969                 mdd_lfsck_component_cleanup(env, com);
2970         }
2971 }