Whamcloud - gitweb
7ab15f97ccc72c57dcd3cbc7056b1fe720eb39dc
[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: need more processing for remote object in the future. */
881                 if (mdd_object_remote(parent)) {
882                         mdd_object_put(env, parent);
883                         ldata.ld_lee = LINKEA_NEXT_ENTRY(ldata);
884                         continue;
885                 }
886
887                 dir = mdd_object_child(parent);
888                 if (unlikely(!dt_try_as_dir(env, dir)))
889                         goto shrink;
890
891                 /* To guarantee the 'name' is terminated with '0'. */
892                 memcpy(info->mti_key, cname->ln_name, cname->ln_namelen);
893                 info->mti_key[cname->ln_namelen] = 0;
894                 cname->ln_name = info->mti_key;
895                 rc = dt_lookup(env, dir, (struct dt_rec *)cfid,
896                                (const struct dt_key *)cname->ln_name,
897                                BYPASS_CAPA);
898                 if (rc != 0 && rc != -ENOENT) {
899                         mdd_object_put(env, parent);
900                         GOTO(stop, rc);
901                 }
902
903                 if (rc == 0) {
904                         if (lu_fid_eq(cfid, mdo2fid(child))) {
905                                 mdd_object_put(env, parent);
906                                 ldata.ld_lee = LINKEA_NEXT_ENTRY(ldata);
907                                 continue;
908                         }
909
910                         goto shrink;
911                 }
912
913                 if (ldata.ld_leh->leh_reccount > la->la_nlink)
914                         goto shrink;
915
916                 /* XXX: For the case of there is linkea entry, but without name
917                  *      entry pointing to the object, and the object link count
918                  *      isn't less than the count of name entries, then add the
919                  *      name entry back to namespace.
920                  *
921                  *      It is out of LFSCK 1.5 scope, will implement it in the
922                  *      future. Keep the linkEA entry. */
923                 mdd_object_put(env, parent);
924                 ldata.ld_lee = LINKEA_NEXT_ENTRY(ldata);
925                 continue;
926
927 shrink:
928                 if (parent != NULL)
929                         mdd_object_put(env, parent);
930                 if (bk->lb_param & LPF_DRYRUN)
931                         RETURN(1);
932
933                 CDEBUG(D_LFSCK, "Remove linkEA: "DFID"[%.*s], "DFID"\n",
934                        PFID(mdo2fid(child)), cname->ln_namelen, cname->ln_name,
935                        PFID(pfid));
936                 linkea_del_buf(&ldata, cname);
937                 update = true;
938         }
939
940         if (update) {
941                 if (!com->lc_journal) {
942                         com->lc_journal = 1;
943                         goto again;
944                 }
945
946                 rc = mdd_links_write(env, child, &ldata, handle);
947         }
948
949         GOTO(stop, rc);
950
951 stop:
952         if (locked)
953                 mdd_write_unlock(env, child);
954
955         if (handle != NULL)
956                 mdd_trans_stop(env, mdd, rc, handle);
957
958         if (rc == 0 && update) {
959                 ns->ln_objs_nlink_repaired++;
960                 rc = 1;
961         }
962         return rc;
963 }
964
965 /* namespace APIs */
966
967 static int mdd_lfsck_namespace_reset(const struct lu_env *env,
968                                      struct lfsck_component *com, bool init)
969 {
970         struct lfsck_namespace  *ns   = (struct lfsck_namespace *)com->lc_file_ram;
971         struct mdd_device       *mdd  = mdd_lfsck2mdd(com->lc_lfsck);
972         struct dt_object        *dto, *root;
973         int                      rc;
974         ENTRY;
975
976         down_write(&com->lc_sem);
977         if (init) {
978                 memset(ns, 0, sizeof(*ns));
979         } else {
980                 __u32 count = ns->ln_success_count;
981                 __u64 last_time = ns->ln_time_last_complete;
982
983                 memset(ns, 0, sizeof(*ns));
984                 ns->ln_success_count = count;
985                 ns->ln_time_last_complete = last_time;
986         }
987         ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
988         ns->ln_status = LS_INIT;
989
990         root = dt_locate(env, mdd->mdd_bottom, &mdd->mdd_local_root_fid);
991         if (unlikely(IS_ERR(root)))
992                 GOTO(out, rc = PTR_ERR(root));
993
994         rc = local_object_unlink(env, mdd->mdd_bottom, root,
995                                  lfsck_namespace_name);
996         if (rc != 0)
997                 GOTO(out, rc);
998
999         dto = local_index_find_or_create(env, mdd->mdd_los, root,
1000                                          lfsck_namespace_name,
1001                                          S_IFREG | S_IRUGO | S_IWUSR,
1002                                          &dt_lfsck_features);
1003         if (IS_ERR(dto))
1004                 GOTO(out, rc = PTR_ERR(dto));
1005
1006         rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
1007         if (rc != 0)
1008                 GOTO(out, rc);
1009         com->lc_obj = dto;
1010
1011         rc = mdd_lfsck_namespace_store(env, com, true);
1012
1013         GOTO(out, rc);
1014 out:
1015         lu_object_put(env, &root->do_lu);
1016         up_write(&com->lc_sem);
1017         return rc;
1018 }
1019
1020 static void
1021 mdd_lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
1022                          bool new_checked)
1023 {
1024         struct lfsck_namespace *ns = (struct lfsck_namespace *)com->lc_file_ram;
1025
1026         down_write(&com->lc_sem);
1027         if (new_checked)
1028                 com->lc_new_checked++;
1029         ns->ln_items_failed++;
1030         if (mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1031                 mdd_lfsck_pos_fill(env, com->lc_lfsck,
1032                                    &ns->ln_pos_first_inconsistent, false);
1033         up_write(&com->lc_sem);
1034 }
1035
1036 static int mdd_lfsck_namespace_checkpoint(const struct lu_env *env,
1037                                           struct lfsck_component *com,
1038                                           bool init)
1039 {
1040         struct md_lfsck         *lfsck = com->lc_lfsck;
1041         struct lfsck_namespace  *ns    =
1042                                 (struct lfsck_namespace *)com->lc_file_ram;
1043         int                      rc;
1044
1045         if (com->lc_new_checked == 0 && !init)
1046                 return 0;
1047
1048         down_write(&com->lc_sem);
1049
1050         if (init) {
1051                 ns->ln_pos_latest_start = lfsck->ml_pos_current;
1052         } else {
1053                 ns->ln_pos_last_checkpoint = lfsck->ml_pos_current;
1054                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1055                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1056                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1057                 ns->ln_items_checked += com->lc_new_checked;
1058                 com->lc_new_checked = 0;
1059         }
1060
1061         rc = mdd_lfsck_namespace_store(env, com, false);
1062
1063         up_write(&com->lc_sem);
1064         return rc;
1065 }
1066
1067 static int mdd_lfsck_namespace_prep(const struct lu_env *env,
1068                                     struct lfsck_component *com)
1069 {
1070         struct md_lfsck         *lfsck  = com->lc_lfsck;
1071         struct lfsck_namespace  *ns     =
1072                                 (struct lfsck_namespace *)com->lc_file_ram;
1073         struct lfsck_position   *pos    = &com->lc_pos_start;
1074
1075         if (ns->ln_status == LS_COMPLETED) {
1076                 int rc;
1077
1078                 rc = mdd_lfsck_namespace_reset(env, com, false);
1079                 if (rc != 0)
1080                         return rc;
1081         }
1082
1083         down_write(&com->lc_sem);
1084
1085         ns->ln_time_latest_start = cfs_time_current_sec();
1086
1087         spin_lock(&lfsck->ml_lock);
1088         if (ns->ln_flags & LF_SCANNED_ONCE) {
1089                 if (!lfsck->ml_drop_dryrun ||
1090                     mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
1091                         ns->ln_status = LS_SCANNING_PHASE2;
1092                         cfs_list_del_init(&com->lc_link);
1093                         cfs_list_add_tail(&com->lc_link,
1094                                           &lfsck->ml_list_double_scan);
1095                         if (!cfs_list_empty(&com->lc_link_dir))
1096                                 cfs_list_del_init(&com->lc_link_dir);
1097                         mdd_lfsck_pos_set_zero(pos);
1098                 } else {
1099                         ns->ln_status = LS_SCANNING_PHASE1;
1100                         ns->ln_run_time_phase1 = 0;
1101                         ns->ln_run_time_phase2 = 0;
1102                         ns->ln_items_checked = 0;
1103                         ns->ln_items_repaired = 0;
1104                         ns->ln_items_failed = 0;
1105                         ns->ln_dirs_checked = 0;
1106                         ns->ln_mlinked_checked = 0;
1107                         ns->ln_objs_checked_phase2 = 0;
1108                         ns->ln_objs_repaired_phase2 = 0;
1109                         ns->ln_objs_failed_phase2 = 0;
1110                         ns->ln_objs_nlink_repaired = 0;
1111                         ns->ln_objs_lost_found = 0;
1112                         fid_zero(&ns->ln_fid_latest_scanned_phase2);
1113                         if (cfs_list_empty(&com->lc_link_dir))
1114                                 cfs_list_add_tail(&com->lc_link_dir,
1115                                                   &lfsck->ml_list_dir);
1116                         *pos = ns->ln_pos_first_inconsistent;
1117                 }
1118         } else {
1119                 ns->ln_status = LS_SCANNING_PHASE1;
1120                 if (cfs_list_empty(&com->lc_link_dir))
1121                         cfs_list_add_tail(&com->lc_link_dir,
1122                                           &lfsck->ml_list_dir);
1123                 if (!lfsck->ml_drop_dryrun ||
1124                     mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
1125                         *pos = ns->ln_pos_last_checkpoint;
1126                         pos->lp_oit_cookie++;
1127                 } else {
1128                         *pos = ns->ln_pos_first_inconsistent;
1129                 }
1130         }
1131         spin_unlock(&lfsck->ml_lock);
1132
1133         up_write(&com->lc_sem);
1134         return 0;
1135 }
1136
1137 static int mdd_lfsck_namespace_exec_oit(const struct lu_env *env,
1138                                         struct lfsck_component *com,
1139                                         struct mdd_object *obj)
1140 {
1141         down_write(&com->lc_sem);
1142         com->lc_new_checked++;
1143         if (S_ISDIR(mdd_object_type(obj)))
1144                 ((struct lfsck_namespace *)com->lc_file_ram)->ln_dirs_checked++;
1145         up_write(&com->lc_sem);
1146         return 0;
1147 }
1148
1149 static int mdd_declare_lfsck_namespace_exec_dir(const struct lu_env *env,
1150                                                 struct mdd_object *obj,
1151                                                 struct thandle *handle)
1152 {
1153         int rc;
1154
1155         /* For destroying all invalid linkEA entries. */
1156         rc = mdo_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
1157         if (rc != 0)
1158                 return rc;
1159
1160         /* For insert new linkEA entry. */
1161         rc = mdd_declare_links_add(env, obj, handle, NULL);
1162         return rc;
1163 }
1164
1165 static int mdd_lfsck_namespace_check_exist(const struct lu_env *env,
1166                                            struct md_lfsck *lfsck,
1167                                            struct mdd_object *obj,
1168                                            const char *name)
1169 {
1170         struct dt_object *dir = lfsck->ml_obj_dir;
1171         struct lu_fid    *fid = &mdd_env_info(env)->mti_fid;
1172         int               rc;
1173         ENTRY;
1174
1175         if (unlikely(mdd_is_dead_obj(obj)))
1176                 RETURN(LFSCK_NAMEENTRY_DEAD);
1177
1178         rc = dt_lookup(env, dir, (struct dt_rec *)fid,
1179                        (const struct dt_key *)name, BYPASS_CAPA);
1180         if (rc == -ENOENT)
1181                 RETURN(LFSCK_NAMEENTRY_REMOVED);
1182
1183         if (rc < 0)
1184                 RETURN(rc);
1185
1186         if (!lu_fid_eq(fid, mdo2fid(obj)))
1187                 RETURN(LFSCK_NAMEENTRY_RECREATED);
1188
1189         RETURN(0);
1190 }
1191
1192 static int mdd_lfsck_namespace_exec_dir(const struct lu_env *env,
1193                                         struct lfsck_component *com,
1194                                         struct mdd_object *obj,
1195                                         struct lu_dirent *ent)
1196 {
1197         struct mdd_thread_info     *info     = mdd_env_info(env);
1198         struct lu_attr             *la       = &info->mti_la;
1199         struct md_lfsck            *lfsck    = com->lc_lfsck;
1200         struct lfsck_bookmark      *bk       = &lfsck->ml_bookmark_ram;
1201         struct lfsck_namespace     *ns       =
1202                                 (struct lfsck_namespace *)com->lc_file_ram;
1203         struct mdd_device          *mdd      = mdd_lfsck2mdd(lfsck);
1204         struct linkea_data          ldata    = { 0 };
1205         const struct lu_fid        *pfid     =
1206                                 lu_object_fid(&lfsck->ml_obj_dir->do_lu);
1207         const struct lu_fid        *cfid     = mdo2fid(obj);
1208         const struct lu_name       *cname;
1209         struct thandle             *handle   = NULL;
1210         bool                        repaired = false;
1211         bool                        locked   = false;
1212         int                         count    = 0;
1213         int                         rc;
1214         ENTRY;
1215
1216         cname = mdd_name_get_const(env, ent->lde_name, ent->lde_namelen);
1217         down_write(&com->lc_sem);
1218         com->lc_new_checked++;
1219
1220         if (ent->lde_attrs & LUDA_UPGRADE) {
1221                 ns->ln_flags |= LF_UPGRADE;
1222                 repaired = true;
1223         } else if (ent->lde_attrs & LUDA_REPAIR) {
1224                 ns->ln_flags |= LF_INCONSISTENT;
1225                 repaired = true;
1226         }
1227
1228         if (ent->lde_name[0] == '.' &&
1229             (ent->lde_namelen == 1 ||
1230              (ent->lde_namelen == 2 && ent->lde_name[1] == '.') ||
1231              fid_is_dot_lustre(&ent->lde_fid)))
1232                 GOTO(out, rc = 0);
1233
1234         if (!(bk->lb_param & LPF_DRYRUN) &&
1235             (com->lc_journal || repaired)) {
1236
1237 again:
1238                 LASSERT(!locked);
1239
1240                 com->lc_journal = 1;
1241                 handle = mdd_trans_create(env, mdd);
1242                 if (IS_ERR(handle))
1243                         GOTO(out, rc = PTR_ERR(handle));
1244
1245                 rc = mdd_declare_lfsck_namespace_exec_dir(env, obj, handle);
1246                 if (rc != 0)
1247                         GOTO(stop, rc);
1248
1249                 rc = mdd_trans_start(env, mdd, handle);
1250                 if (rc != 0)
1251                         GOTO(stop, rc);
1252
1253                 mdd_write_lock(env, obj, MOR_TGT_CHILD);
1254                 locked = true;
1255         }
1256
1257         rc = mdd_lfsck_namespace_check_exist(env, lfsck, obj, ent->lde_name);
1258         if (rc != 0)
1259                 GOTO(stop, rc);
1260
1261         rc = mdd_links_read(env, obj, &ldata);
1262         if (rc == 0) {
1263                 count = ldata.ld_leh->leh_reccount;
1264                 rc = linkea_links_find(&ldata, cname, pfid);
1265                 if (rc == 0) {
1266                         /* For dir, if there are more than one linkea entries,
1267                          * then remove all the other redundant linkea entries.*/
1268                         if (unlikely(count > 1 &&
1269                                      S_ISDIR(mdd_object_type(obj))))
1270                                 goto unmatch;
1271
1272                         goto record;
1273                 } else {
1274
1275 unmatch:
1276                         ns->ln_flags |= LF_INCONSISTENT;
1277                         if (bk->lb_param & LPF_DRYRUN) {
1278                                 repaired = true;
1279                                 goto record;
1280                         }
1281
1282                         /*For dir, remove the unmatched linkea entry directly.*/
1283                         if (S_ISDIR(mdd_object_type(obj))) {
1284                                 if (!com->lc_journal)
1285                                         goto again;
1286
1287                                 rc = mdo_xattr_del(env, obj, XATTR_NAME_LINK,
1288                                                    handle, BYPASS_CAPA);
1289                                 if (rc != 0)
1290                                         GOTO(stop, rc);
1291
1292                                 goto nodata;
1293                         } else {
1294                                 goto add;
1295                         }
1296                 }
1297         } else if (unlikely(rc == -EINVAL)) {
1298                 ns->ln_flags |= LF_INCONSISTENT;
1299                 if (bk->lb_param & LPF_DRYRUN) {
1300                         count = 1;
1301                         repaired = true;
1302                         goto record;
1303                 }
1304
1305                 if (!com->lc_journal)
1306                         goto again;
1307
1308                 /* The magic crashed, we are not sure whether there are more
1309                  * corrupt data in the linkea, so remove all linkea entries. */
1310                 rc = mdo_xattr_del(env, obj, XATTR_NAME_LINK, handle,
1311                                    BYPASS_CAPA);
1312                 if (rc != 0)
1313                         GOTO(stop, rc);
1314
1315                 goto nodata;
1316         } else if (rc == -ENODATA) {
1317                 ns->ln_flags |= LF_UPGRADE;
1318                 if (bk->lb_param & LPF_DRYRUN) {
1319                         count = 1;
1320                         repaired = true;
1321                         goto record;
1322                 }
1323
1324 nodata:
1325                 rc = linkea_data_new(&ldata, &mdd_env_info(env)->mti_link_buf);
1326                 if (rc != 0)
1327                         GOTO(stop, rc);
1328
1329 add:
1330                 if (!com->lc_journal)
1331                         goto again;
1332
1333                 rc = linkea_add_buf(&ldata, cname, pfid);
1334                 if (rc != 0)
1335                         GOTO(stop, rc);
1336
1337                 rc = mdd_links_write(env, obj, &ldata, handle);
1338                 if (rc != 0)
1339                         GOTO(stop, rc);
1340
1341                 count = ldata.ld_leh->leh_reccount;
1342                 repaired = true;
1343         } else {
1344                 GOTO(stop, rc);
1345         }
1346
1347 record:
1348         LASSERT(count > 0);
1349
1350         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1351         if (rc != 0)
1352                 GOTO(stop, rc);
1353
1354         if ((count == 1) &&
1355             (la->la_nlink == 1 || S_ISDIR(mdd_object_type(obj))))
1356                 /* Usually, it is for single linked object or dir, do nothing.*/
1357                 GOTO(stop, rc);
1358
1359         /* Following modification will be in another transaction.  */
1360         if (handle != NULL) {
1361                 LASSERT(mdd_write_locked(env, obj));
1362
1363                 mdd_write_unlock(env, obj);
1364                 locked = false;
1365
1366                 mdd_trans_stop(env, mdd, 0, handle);
1367                 handle = NULL;
1368         }
1369
1370         ns->ln_mlinked_checked++;
1371         rc = mdd_lfsck_namespace_update(env, com, cfid,
1372                         count != la->la_nlink ? LLF_UNMATCH_NLINKS : 0, false);
1373
1374         GOTO(out, rc);
1375
1376 stop:
1377         if (locked)
1378                 mdd_write_unlock(env, obj);
1379
1380         if (handle != NULL)
1381                 mdd_trans_stop(env, mdd, rc, handle);
1382
1383 out:
1384         if (rc < 0) {
1385                 ns->ln_items_failed++;
1386                 if (mdd_lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1387                         mdd_lfsck_pos_fill(env, lfsck,
1388                                            &ns->ln_pos_first_inconsistent,
1389                                            false);
1390                 if (!(bk->lb_param & LPF_FAILOUT))
1391                         rc = 0;
1392         } else {
1393                 if (repaired)
1394                         ns->ln_items_repaired++;
1395                 else
1396                         com->lc_journal = 0;
1397                 rc = 0;
1398         }
1399         up_write(&com->lc_sem);
1400         return rc;
1401 }
1402
1403 static int mdd_lfsck_namespace_post(const struct lu_env *env,
1404                                     struct lfsck_component *com,
1405                                     int result, bool init)
1406 {
1407         struct md_lfsck         *lfsck = com->lc_lfsck;
1408         struct lfsck_namespace  *ns    =
1409                                 (struct lfsck_namespace *)com->lc_file_ram;
1410         int                      rc;
1411
1412         down_write(&com->lc_sem);
1413
1414         spin_lock(&lfsck->ml_lock);
1415         if (!init)
1416                 ns->ln_pos_last_checkpoint = lfsck->ml_pos_current;
1417         if (result > 0) {
1418                 ns->ln_status = LS_SCANNING_PHASE2;
1419                 ns->ln_flags |= LF_SCANNED_ONCE;
1420                 ns->ln_flags &= ~LF_UPGRADE;
1421                 cfs_list_del_init(&com->lc_link);
1422                 cfs_list_del_init(&com->lc_link_dir);
1423                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_double_scan);
1424         } else if (result == 0) {
1425                 if (lfsck->ml_paused) {
1426                         ns->ln_status = LS_PAUSED;
1427                 } else {
1428                         ns->ln_status = LS_STOPPED;
1429                         cfs_list_del_init(&com->lc_link);
1430                         cfs_list_del_init(&com->lc_link_dir);
1431                         cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1432                 }
1433         } else {
1434                 ns->ln_status = LS_FAILED;
1435                 cfs_list_del_init(&com->lc_link);
1436                 cfs_list_del_init(&com->lc_link_dir);
1437                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1438         }
1439         spin_unlock(&lfsck->ml_lock);
1440
1441         if (!init) {
1442                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1443                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1444                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1445                 ns->ln_items_checked += com->lc_new_checked;
1446                 com->lc_new_checked = 0;
1447         }
1448
1449         rc = mdd_lfsck_namespace_store(env, com, false);
1450
1451         up_write(&com->lc_sem);
1452         return rc;
1453 }
1454
1455 static int
1456 mdd_lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
1457                          char *buf, int len)
1458 {
1459         struct md_lfsck         *lfsck = com->lc_lfsck;
1460         struct lfsck_bookmark   *bk    = &lfsck->ml_bookmark_ram;
1461         struct lfsck_namespace  *ns    =
1462                                 (struct lfsck_namespace *)com->lc_file_ram;
1463         int                      save  = len;
1464         int                      ret   = -ENOSPC;
1465         int                      rc;
1466
1467         down_read(&com->lc_sem);
1468         rc = snprintf(buf, len,
1469                       "name: lfsck_namespace\n"
1470                       "magic: 0x%x\n"
1471                       "version: %d\n"
1472                       "status: %s\n",
1473                       ns->ln_magic,
1474                       bk->lb_version,
1475                       lfsck_status_names[ns->ln_status]);
1476         if (rc <= 0)
1477                 goto out;
1478
1479         buf += rc;
1480         len -= rc;
1481         rc = lfsck_bits_dump(&buf, &len, ns->ln_flags, lfsck_flags_names,
1482                              "flags");
1483         if (rc < 0)
1484                 goto out;
1485
1486         rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1487                              "param");
1488         if (rc < 0)
1489                 goto out;
1490
1491         rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_complete,
1492                              "time_since_last_completed");
1493         if (rc < 0)
1494                 goto out;
1495
1496         rc = lfsck_time_dump(&buf, &len, ns->ln_time_latest_start,
1497                              "time_since_latest_start");
1498         if (rc < 0)
1499                 goto out;
1500
1501         rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_checkpoint,
1502                              "time_since_last_checkpoint");
1503         if (rc < 0)
1504                 goto out;
1505
1506         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_latest_start,
1507                             "latest_start_position");
1508         if (rc < 0)
1509                 goto out;
1510
1511         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_last_checkpoint,
1512                             "last_checkpoint_position");
1513         if (rc < 0)
1514                 goto out;
1515
1516         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_first_inconsistent,
1517                             "first_failure_position");
1518         if (rc < 0)
1519                 goto out;
1520
1521         if (ns->ln_status == LS_SCANNING_PHASE1) {
1522                 struct lfsck_position pos;
1523                 cfs_duration_t duration = cfs_time_current() -
1524                                           lfsck->ml_time_last_checkpoint;
1525                 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1526                 __u64 speed = checked;
1527                 __u64 new_checked = com->lc_new_checked * CFS_HZ;
1528                 __u32 rtime = ns->ln_run_time_phase1 +
1529                               cfs_duration_sec(duration + HALF_SEC);
1530
1531                 if (duration != 0)
1532                         do_div(new_checked, duration);
1533                 if (rtime != 0)
1534                         do_div(speed, rtime);
1535                 rc = snprintf(buf, len,
1536                               "checked_phase1: "LPU64"\n"
1537                               "checked_phase2: "LPU64"\n"
1538                               "updated_phase1: "LPU64"\n"
1539                               "updated_phase2: "LPU64"\n"
1540                               "failed_phase1: "LPU64"\n"
1541                               "failed_phase2: "LPU64"\n"
1542                               "dirs: "LPU64"\n"
1543                               "M-linked: "LPU64"\n"
1544                               "nlinks_repaired: "LPU64"\n"
1545                               "lost_found: "LPU64"\n"
1546                               "success_count: %u\n"
1547                               "run_time_phase1: %u seconds\n"
1548                               "run_time_phase2: %u seconds\n"
1549                               "average_speed_phase1: "LPU64" items/sec\n"
1550                               "average_speed_phase2: N/A\n"
1551                               "real-time_speed_phase1: "LPU64" items/sec\n"
1552                               "real-time_speed_phase2: N/A\n",
1553                               checked,
1554                               ns->ln_objs_checked_phase2,
1555                               ns->ln_items_repaired,
1556                               ns->ln_objs_repaired_phase2,
1557                               ns->ln_items_failed,
1558                               ns->ln_objs_failed_phase2,
1559                               ns->ln_dirs_checked,
1560                               ns->ln_mlinked_checked,
1561                               ns->ln_objs_nlink_repaired,
1562                               ns->ln_objs_lost_found,
1563                               ns->ln_success_count,
1564                               rtime,
1565                               ns->ln_run_time_phase2,
1566                               speed,
1567                               new_checked);
1568                 if (rc <= 0)
1569                         goto out;
1570
1571                 buf += rc;
1572                 len -= rc;
1573                 mdd_lfsck_pos_fill(env, lfsck, &pos, false);
1574                 rc = lfsck_pos_dump(&buf, &len, &pos, "current_position");
1575                 if (rc <= 0)
1576                         goto out;
1577         } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1578                 cfs_duration_t duration = cfs_time_current() -
1579                                           lfsck->ml_time_last_checkpoint;
1580                 __u64 checked = ns->ln_objs_checked_phase2 +
1581                                 com->lc_new_checked;
1582                 __u64 speed1 = ns->ln_items_checked;
1583                 __u64 speed2 = checked;
1584                 __u64 new_checked = com->lc_new_checked * CFS_HZ;
1585                 __u32 rtime = ns->ln_run_time_phase2 +
1586                               cfs_duration_sec(duration + HALF_SEC);
1587
1588                 if (duration != 0)
1589                         do_div(new_checked, duration);
1590                 if (ns->ln_run_time_phase1 != 0)
1591                         do_div(speed1, ns->ln_run_time_phase1);
1592                 if (rtime != 0)
1593                         do_div(speed2, rtime);
1594                 rc = snprintf(buf, len,
1595                               "checked_phase1: "LPU64"\n"
1596                               "checked_phase2: "LPU64"\n"
1597                               "updated_phase1: "LPU64"\n"
1598                               "updated_phase2: "LPU64"\n"
1599                               "failed_phase1: "LPU64"\n"
1600                               "failed_phase2: "LPU64"\n"
1601                               "dirs: "LPU64"\n"
1602                               "M-linked: "LPU64"\n"
1603                               "nlinks_repaired: "LPU64"\n"
1604                               "lost_found: "LPU64"\n"
1605                               "success_count: %u\n"
1606                               "run_time_phase1: %u seconds\n"
1607                               "run_time_phase2: %u seconds\n"
1608                               "average_speed_phase1: "LPU64" items/sec\n"
1609                               "average_speed_phase2: "LPU64" objs/sec\n"
1610                               "real-time_speed_phase1: N/A\n"
1611                               "real-time_speed_phase2: "LPU64" objs/sec\n"
1612                               "current_position: "DFID"\n",
1613                               ns->ln_items_checked,
1614                               checked,
1615                               ns->ln_items_repaired,
1616                               ns->ln_objs_repaired_phase2,
1617                               ns->ln_items_failed,
1618                               ns->ln_objs_failed_phase2,
1619                               ns->ln_dirs_checked,
1620                               ns->ln_mlinked_checked,
1621                               ns->ln_objs_nlink_repaired,
1622                               ns->ln_objs_lost_found,
1623                               ns->ln_success_count,
1624                               ns->ln_run_time_phase1,
1625                               rtime,
1626                               speed1,
1627                               speed2,
1628                               new_checked,
1629                               PFID(&ns->ln_fid_latest_scanned_phase2));
1630                 if (rc <= 0)
1631                         goto out;
1632
1633                 buf += rc;
1634                 len -= rc;
1635         } else {
1636                 __u64 speed1 = ns->ln_items_checked;
1637                 __u64 speed2 = ns->ln_objs_checked_phase2;
1638
1639                 if (ns->ln_run_time_phase1 != 0)
1640                         do_div(speed1, ns->ln_run_time_phase1);
1641                 if (ns->ln_run_time_phase2 != 0)
1642                         do_div(speed2, ns->ln_run_time_phase2);
1643                 rc = snprintf(buf, len,
1644                               "checked_phase1: "LPU64"\n"
1645                               "checked_phase2: "LPU64"\n"
1646                               "updated_phase1: "LPU64"\n"
1647                               "updated_phase2: "LPU64"\n"
1648                               "failed_phase1: "LPU64"\n"
1649                               "failed_phase2: "LPU64"\n"
1650                               "dirs: "LPU64"\n"
1651                               "M-linked: "LPU64"\n"
1652                               "nlinks_repaired: "LPU64"\n"
1653                               "lost_found: "LPU64"\n"
1654                               "success_count: %u\n"
1655                               "run_time_phase1: %u seconds\n"
1656                               "run_time_phase2: %u seconds\n"
1657                               "average_speed_phase1: "LPU64" items/sec\n"
1658                               "average_speed_phase2: "LPU64" objs/sec\n"
1659                               "real-time_speed_phase1: N/A\n"
1660                               "real-time_speed_phase2: N/A\n"
1661                               "current_position: N/A\n",
1662                               ns->ln_items_checked,
1663                               ns->ln_objs_checked_phase2,
1664                               ns->ln_items_repaired,
1665                               ns->ln_objs_repaired_phase2,
1666                               ns->ln_items_failed,
1667                               ns->ln_objs_failed_phase2,
1668                               ns->ln_dirs_checked,
1669                               ns->ln_mlinked_checked,
1670                               ns->ln_objs_nlink_repaired,
1671                               ns->ln_objs_lost_found,
1672                               ns->ln_success_count,
1673                               ns->ln_run_time_phase1,
1674                               ns->ln_run_time_phase2,
1675                               speed1,
1676                               speed2);
1677                 if (rc <= 0)
1678                         goto out;
1679
1680                 buf += rc;
1681                 len -= rc;
1682         }
1683         ret = save - len;
1684
1685 out:
1686         up_read(&com->lc_sem);
1687         return ret;
1688 }
1689
1690 static int mdd_lfsck_namespace_double_scan(const struct lu_env *env,
1691                                            struct lfsck_component *com)
1692 {
1693         struct md_lfsck         *lfsck  = com->lc_lfsck;
1694         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
1695         struct mdd_device       *mdd    = mdd_lfsck2mdd(lfsck);
1696         struct lfsck_bookmark   *bk     = &lfsck->ml_bookmark_ram;
1697         struct lfsck_namespace  *ns     =
1698                                 (struct lfsck_namespace *)com->lc_file_ram;
1699         struct dt_object        *obj    = com->lc_obj;
1700         const struct dt_it_ops  *iops   = &obj->do_index_ops->dio_it;
1701         struct mdd_object       *target;
1702         struct dt_it            *di;
1703         struct dt_key           *key;
1704         struct lu_fid            fid;
1705         int                      rc;
1706         __u8                     flags;
1707         ENTRY;
1708
1709         lfsck->ml_new_scanned = 0;
1710         lfsck->ml_time_last_checkpoint = cfs_time_current();
1711         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
1712                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1713
1714         di = iops->init(env, obj, 0, BYPASS_CAPA);
1715         if (IS_ERR(di))
1716                 RETURN(PTR_ERR(di));
1717
1718         fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
1719         rc = iops->get(env, di, (const struct dt_key *)&fid);
1720         if (rc < 0)
1721                 GOTO(fini, rc);
1722
1723         /* Skip the start one, which either has been processed or non-exist. */
1724         rc = iops->next(env, di);
1725         if (rc != 0)
1726                 GOTO(put, rc);
1727
1728         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1729                 GOTO(put, rc = 0);
1730
1731         do {
1732                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
1733                     cfs_fail_val > 0) {
1734                         struct l_wait_info lwi;
1735
1736                         lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
1737                                           NULL, NULL);
1738                         l_wait_event(thread->t_ctl_waitq,
1739                                      !thread_is_running(thread),
1740                                      &lwi);
1741                 }
1742
1743                 key = iops->key(env, di);
1744                 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
1745                 target = mdd_object_find(env, mdd, &fid);
1746                 down_write(&com->lc_sem);
1747                 if (target == NULL) {
1748                         rc = 0;
1749                         goto checkpoint;
1750                 } else if (IS_ERR(target)) {
1751                         rc = PTR_ERR(target);
1752                         goto checkpoint;
1753                 }
1754
1755                 /* XXX: need more processing for remote object in the future. */
1756                 if (!mdd_object_exists(target) || mdd_object_remote(target))
1757                         goto obj_put;
1758
1759                 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
1760                 if (rc == 0)
1761                         rc = mdd_lfsck_namespace_double_scan_one(env, com,
1762                                                                  target, flags);
1763
1764 obj_put:
1765                 mdd_object_put(env, target);
1766
1767 checkpoint:
1768                 lfsck->ml_new_scanned++;
1769                 com->lc_new_checked++;
1770                 ns->ln_fid_latest_scanned_phase2 = fid;
1771                 if (rc > 0)
1772                         ns->ln_objs_repaired_phase2++;
1773                 else if (rc < 0)
1774                         ns->ln_objs_failed_phase2++;
1775                 up_write(&com->lc_sem);
1776
1777                 if ((rc == 0) || ((rc > 0) && !(bk->lb_param & LPF_DRYRUN))) {
1778                         mdd_lfsck_namespace_delete(env, com, &fid);
1779                 } else if (rc < 0) {
1780                         flags |= LLF_REPAIR_FAILED;
1781                         mdd_lfsck_namespace_update(env, com, &fid, flags, true);
1782                 }
1783
1784                 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1785                         GOTO(put, rc);
1786
1787                 if (likely(cfs_time_beforeq(cfs_time_current(),
1788                                             lfsck->ml_time_next_checkpoint)) ||
1789                     com->lc_new_checked == 0)
1790                         goto speed;
1791
1792                 down_write(&com->lc_sem);
1793                 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1794                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1795                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1796                 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1797                 com->lc_new_checked = 0;
1798                 rc = mdd_lfsck_namespace_store(env, com, false);
1799                 up_write(&com->lc_sem);
1800                 if (rc != 0)
1801                         GOTO(put, rc);
1802
1803                 lfsck->ml_time_last_checkpoint = cfs_time_current();
1804                 lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
1805                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1806
1807 speed:
1808                 mdd_lfsck_control_speed(lfsck);
1809                 if (unlikely(!thread_is_running(thread)))
1810                         GOTO(put, rc = 0);
1811
1812                 rc = iops->next(env, di);
1813         } while (rc == 0);
1814
1815         GOTO(put, rc);
1816
1817 put:
1818         iops->put(env, di);
1819
1820 fini:
1821         iops->fini(env, di);
1822         down_write(&com->lc_sem);
1823
1824         ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1825                                 HALF_SEC - lfsck->ml_time_last_checkpoint);
1826         ns->ln_time_last_checkpoint = cfs_time_current_sec();
1827         ns->ln_objs_checked_phase2 += com->lc_new_checked;
1828         com->lc_new_checked = 0;
1829
1830         if (rc > 0) {
1831                 com->lc_journal = 0;
1832                 ns->ln_status = LS_COMPLETED;
1833                 if (!(bk->lb_param & LPF_DRYRUN))
1834                         ns->ln_flags &=
1835                         ~(LF_SCANNED_ONCE | LF_INCONSISTENT | LF_UPGRADE);
1836                 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
1837                 ns->ln_success_count++;
1838         } else if (rc == 0) {
1839                 if (lfsck->ml_paused)
1840                         ns->ln_status = LS_PAUSED;
1841                 else
1842                         ns->ln_status = LS_STOPPED;
1843         } else {
1844                 ns->ln_status = LS_FAILED;
1845         }
1846
1847         if (ns->ln_status != LS_PAUSED) {
1848                 spin_lock(&lfsck->ml_lock);
1849                 cfs_list_del_init(&com->lc_link);
1850                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1851                 spin_unlock(&lfsck->ml_lock);
1852         }
1853
1854         rc = mdd_lfsck_namespace_store(env, com, false);
1855
1856         up_write(&com->lc_sem);
1857         return rc;
1858 }
1859
1860 static struct lfsck_operations mdd_lfsck_namespace_ops = {
1861         .lfsck_reset            = mdd_lfsck_namespace_reset,
1862         .lfsck_fail             = mdd_lfsck_namespace_fail,
1863         .lfsck_checkpoint       = mdd_lfsck_namespace_checkpoint,
1864         .lfsck_prep             = mdd_lfsck_namespace_prep,
1865         .lfsck_exec_oit         = mdd_lfsck_namespace_exec_oit,
1866         .lfsck_exec_dir         = mdd_lfsck_namespace_exec_dir,
1867         .lfsck_post             = mdd_lfsck_namespace_post,
1868         .lfsck_dump             = mdd_lfsck_namespace_dump,
1869         .lfsck_double_scan      = mdd_lfsck_namespace_double_scan,
1870 };
1871
1872 /* LFSCK component setup/cleanup functions */
1873
1874 static int mdd_lfsck_namespace_setup(const struct lu_env *env,
1875                                      struct md_lfsck *lfsck)
1876 {
1877         struct mdd_device       *mdd = mdd_lfsck2mdd(lfsck);
1878         struct lfsck_component  *com;
1879         struct lfsck_namespace  *ns;
1880         struct dt_object        *obj, *root;
1881         int                      rc;
1882         ENTRY;
1883
1884         OBD_ALLOC_PTR(com);
1885         if (com == NULL)
1886                 RETURN(-ENOMEM);
1887
1888         CFS_INIT_LIST_HEAD(&com->lc_link);
1889         CFS_INIT_LIST_HEAD(&com->lc_link_dir);
1890         init_rwsem(&com->lc_sem);
1891         atomic_set(&com->lc_ref, 1);
1892         com->lc_lfsck = lfsck;
1893         com->lc_type = LT_NAMESPACE;
1894         com->lc_ops = &mdd_lfsck_namespace_ops;
1895         com->lc_file_size = sizeof(struct lfsck_namespace);
1896         OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1897         if (com->lc_file_ram == NULL)
1898                 GOTO(out, rc = -ENOMEM);
1899
1900         OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1901         if (com->lc_file_disk == NULL)
1902                 GOTO(out, rc = -ENOMEM);
1903
1904         root = dt_locate(env, mdd->mdd_bottom, &mdd->mdd_local_root_fid);
1905         if (unlikely(IS_ERR(root)))
1906                 GOTO(out, rc = PTR_ERR(root));
1907
1908         obj = local_index_find_or_create(env, mdd->mdd_los, root,
1909                                          lfsck_namespace_name,
1910                                          S_IFREG | S_IRUGO | S_IWUSR,
1911                                          &dt_lfsck_features);
1912         lu_object_put(env, &root->do_lu);
1913         if (IS_ERR(obj))
1914                 GOTO(out, rc = PTR_ERR(obj));
1915
1916         com->lc_obj = obj;
1917         rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
1918         if (rc != 0)
1919                 GOTO(out, rc);
1920
1921         rc = mdd_lfsck_namespace_load(env, com);
1922         if (rc > 0)
1923                 rc = mdd_lfsck_namespace_reset(env, com, true);
1924         else if (rc == -ENODATA)
1925                 rc = mdd_lfsck_namespace_init(env, com);
1926         if (rc != 0)
1927                 GOTO(out, rc);
1928
1929         ns = (struct lfsck_namespace *)com->lc_file_ram;
1930         switch (ns->ln_status) {
1931         case LS_INIT:
1932         case LS_COMPLETED:
1933         case LS_FAILED:
1934         case LS_STOPPED:
1935                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_idle);
1936                 break;
1937         default:
1938                 CERROR("%s: unknown status: %u\n",
1939                        mdd_lfsck2name(lfsck), ns->ln_status);
1940                 /* fall through */
1941         case LS_SCANNING_PHASE1:
1942         case LS_SCANNING_PHASE2:
1943                 /* No need to store the status to disk right now.
1944                  * If the system crashed before the status stored,
1945                  * it will be loaded back when next time. */
1946                 ns->ln_status = LS_CRASHED;
1947                 /* fall through */
1948         case LS_PAUSED:
1949         case LS_CRASHED:
1950                 cfs_list_add_tail(&com->lc_link, &lfsck->ml_list_scan);
1951                 cfs_list_add_tail(&com->lc_link_dir, &lfsck->ml_list_dir);
1952                 break;
1953         }
1954
1955         GOTO(out, rc = 0);
1956
1957 out:
1958         if (rc != 0)
1959                 mdd_lfsck_component_cleanup(env, com);
1960         return rc;
1961 }
1962
1963 /* helper functions for framework */
1964
1965 static int object_needs_lfsck(const struct lu_env *env, struct mdd_device *mdd,
1966                               struct mdd_object *obj)
1967 {
1968         struct lu_fid *fid   = &mdd_env_info(env)->mti_fid;
1969         int            depth = 0;
1970         int            rc;
1971
1972         LASSERT(S_ISDIR(mdd_object_type(obj)));
1973
1974         while (1) {
1975                 if (mdd_is_root(mdd, mdo2fid(obj))) {
1976                         if (depth > 0)
1977                                 mdd_object_put(env, obj);
1978                         return 1;
1979                 }
1980
1981                 /* .lustre doesn't contain "real" user objects, no need lfsck */
1982                 if (fid_is_dot_lustre(mdo2fid(obj))) {
1983                         if (depth > 0)
1984                                 mdd_object_put(env, obj);
1985                         return 0;
1986                 }
1987
1988                 mdd_read_lock(env, obj, MOR_TGT_CHILD);
1989                 if (unlikely(mdd_is_dead_obj(obj))) {
1990                         mdd_read_unlock(env, obj);
1991                         if (depth > 0)
1992                                 mdd_object_put(env, obj);
1993                         return 0;
1994                 }
1995
1996                 rc = dt_xattr_get(env, mdd_object_child(obj),
1997                                   mdd_buf_get(env, NULL, 0), XATTR_NAME_LINK,
1998                                   BYPASS_CAPA);
1999                 mdd_read_unlock(env, obj);
2000                 if (rc >= 0) {
2001                         if (depth > 0)
2002                                 mdd_object_put(env, obj);
2003                         return 1;
2004                 }
2005
2006                 if (rc < 0 && rc != -ENODATA) {
2007                         if (depth > 0)
2008                                 mdd_object_put(env, obj);
2009                         return rc;
2010                 }
2011
2012                 rc = mdd_parent_fid(env, obj, fid);
2013                 if (depth > 0)
2014                         mdd_object_put(env, obj);
2015                 if (rc != 0)
2016                         return rc;
2017
2018                 if (unlikely(lu_fid_eq(fid, &mdd->mdd_local_root_fid)))
2019                         return 0;
2020
2021                 obj = mdd_object_find(env, mdd, fid);
2022                 if (obj == NULL)
2023                         return 0;
2024                 else if (IS_ERR(obj))
2025                         return PTR_ERR(obj);
2026
2027                 if (!mdd_object_exists(obj)) {
2028                         mdd_object_put(env, obj);
2029                         return 0;
2030                 }
2031
2032                 /* Currently, only client visible directory can be remote. */
2033                 if (mdd_object_remote(obj)) {
2034                         mdd_object_put(env, obj);
2035                         return 1;
2036                 }
2037
2038                 depth++;
2039         }
2040         return 0;
2041 }
2042
2043 static void mdd_lfsck_unpack_ent(struct lu_dirent *ent)
2044 {
2045         fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
2046         ent->lde_hash = le64_to_cpu(ent->lde_hash);
2047         ent->lde_reclen = le16_to_cpu(ent->lde_reclen);
2048         ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
2049         ent->lde_attrs = le32_to_cpu(ent->lde_attrs);
2050
2051         /* Make sure the name is terminated with '0'.
2052          * The data (type) after ent::lde_name maybe
2053          * broken, but we do not care. */
2054         ent->lde_name[ent->lde_namelen] = 0;
2055 }
2056
2057 /* LFSCK wrap functions */
2058
2059 static void mdd_lfsck_fail(const struct lu_env *env, struct md_lfsck *lfsck,
2060                            bool new_checked)
2061 {
2062         struct lfsck_component *com;
2063
2064         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2065                 com->lc_ops->lfsck_fail(env, com, new_checked);
2066         }
2067 }
2068
2069 static int mdd_lfsck_checkpoint(const struct lu_env *env,
2070                                 struct md_lfsck *lfsck)
2071 {
2072         struct lfsck_component *com;
2073         int                     rc;
2074
2075         if (likely(cfs_time_beforeq(cfs_time_current(),
2076                                     lfsck->ml_time_next_checkpoint)))
2077                 return 0;
2078
2079         mdd_lfsck_pos_fill(env, lfsck, &lfsck->ml_pos_current, false);
2080         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2081                 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
2082                 if (rc != 0)
2083                         return rc;;
2084         }
2085
2086         lfsck->ml_time_last_checkpoint = cfs_time_current();
2087         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
2088                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2089         return 0;
2090 }
2091
2092 static int mdd_lfsck_prep(struct lu_env *env, struct md_lfsck *lfsck)
2093 {
2094         struct mdd_device      *mdd     = mdd_lfsck2mdd(lfsck);
2095         struct mdd_object      *obj     = NULL;
2096         struct dt_object       *dt_obj;
2097         struct lfsck_component *com;
2098         struct lfsck_component *next;
2099         struct lfsck_position  *pos     = NULL;
2100         const struct dt_it_ops *iops    =
2101                                 &lfsck->ml_obj_oit->do_index_ops->dio_it;
2102         struct dt_it           *di;
2103         int                     rc;
2104         ENTRY;
2105
2106         LASSERT(lfsck->ml_obj_dir == NULL);
2107         LASSERT(lfsck->ml_di_dir == NULL);
2108
2109         lfsck->ml_current_oit_processed = 0;
2110         cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_scan, lc_link) {
2111                 com->lc_new_checked = 0;
2112                 if (lfsck->ml_bookmark_ram.lb_param & LPF_DRYRUN)
2113                         com->lc_journal = 0;
2114
2115                 rc = com->lc_ops->lfsck_prep(env, com);
2116                 if (rc != 0)
2117                         RETURN(rc);
2118
2119                 if ((pos == NULL) ||
2120                     (!mdd_lfsck_pos_is_zero(&com->lc_pos_start) &&
2121                      mdd_lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
2122                         pos = &com->lc_pos_start;
2123         }
2124
2125         /* Init otable-based iterator. */
2126         if (pos == NULL) {
2127                 rc = iops->load(env, lfsck->ml_di_oit, 0);
2128                 if (rc > 0) {
2129                         lfsck->ml_oit_over = 1;
2130                         rc = 0;
2131                 }
2132
2133                 GOTO(out, rc);
2134         }
2135
2136         rc = iops->load(env, lfsck->ml_di_oit, pos->lp_oit_cookie);
2137         if (rc < 0)
2138                 GOTO(out, rc);
2139         else if (rc > 0)
2140                 lfsck->ml_oit_over = 1;
2141
2142         if (fid_is_zero(&pos->lp_dir_parent))
2143                 GOTO(out, rc = 0);
2144
2145         /* Find the directory for namespace-based traverse. */
2146         obj = mdd_object_find(env, mdd, &pos->lp_dir_parent);
2147         if (obj == NULL)
2148                 GOTO(out, rc = 0);
2149         else if (IS_ERR(obj))
2150                 RETURN(PTR_ERR(obj));
2151
2152         /* XXX: need more processing for remote object in the future. */
2153         if (!mdd_object_exists(obj) || mdd_object_remote(obj) ||
2154             unlikely(!S_ISDIR(mdd_object_type(obj))))
2155                 GOTO(out, rc = 0);
2156
2157         if (unlikely(mdd_is_dead_obj(obj)))
2158                 GOTO(out, rc = 0);
2159
2160         dt_obj = mdd_object_child(obj);
2161         if (unlikely(!dt_try_as_dir(env, dt_obj)))
2162                 GOTO(out, rc = -ENOTDIR);
2163
2164         /* Init the namespace-based directory traverse. */
2165         iops = &dt_obj->do_index_ops->dio_it;
2166         di = iops->init(env, dt_obj, lfsck->ml_args_dir, BYPASS_CAPA);
2167         if (IS_ERR(di))
2168                 GOTO(out, rc = PTR_ERR(di));
2169
2170         LASSERT(pos->lp_dir_cookie < MDS_DIR_END_OFF);
2171
2172         rc = iops->load(env, di, pos->lp_dir_cookie);
2173         if ((rc == 0) || (rc > 0 && pos->lp_dir_cookie > 0))
2174                 rc = iops->next(env, di);
2175         else if (rc > 0)
2176                 rc = 0;
2177
2178         if (rc != 0) {
2179                 iops->put(env, di);
2180                 iops->fini(env, di);
2181                 GOTO(out, rc);
2182         }
2183
2184         lfsck->ml_obj_dir = dt_obj;
2185         spin_lock(&lfsck->ml_lock);
2186         lfsck->ml_di_dir = di;
2187         spin_unlock(&lfsck->ml_lock);
2188         obj = NULL;
2189
2190         GOTO(out, rc = 0);
2191
2192 out:
2193         if (obj != NULL)
2194                 mdd_object_put(env, obj);
2195
2196         if (rc < 0) {
2197                 cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_scan,
2198                                              lc_link)
2199                         com->lc_ops->lfsck_post(env, com, rc, true);
2200
2201                 return rc;
2202         }
2203
2204         rc = 0;
2205         mdd_lfsck_pos_fill(env, lfsck, &lfsck->ml_pos_current, true);
2206         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2207                 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
2208                 if (rc != 0)
2209                         break;
2210         }
2211
2212         lfsck->ml_time_last_checkpoint = cfs_time_current();
2213         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
2214                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2215         return rc;
2216 }
2217
2218 static int mdd_lfsck_exec_oit(const struct lu_env *env, struct md_lfsck *lfsck,
2219                               struct mdd_object *obj)
2220 {
2221         struct lfsck_component *com;
2222         struct dt_object       *dt_obj;
2223         const struct dt_it_ops *iops;
2224         struct dt_it           *di;
2225         int                     rc;
2226         ENTRY;
2227
2228         LASSERT(lfsck->ml_obj_dir == NULL);
2229
2230         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2231                 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
2232                 if (rc != 0)
2233                         RETURN(rc);
2234         }
2235
2236         if (!S_ISDIR(mdd_object_type(obj)) ||
2237             cfs_list_empty(&lfsck->ml_list_dir))
2238                RETURN(0);
2239
2240         rc = object_needs_lfsck(env, mdd_lfsck2mdd(lfsck), obj);
2241         if (rc <= 0)
2242                 GOTO(out, rc);
2243
2244         if (unlikely(mdd_is_dead_obj(obj)))
2245                 GOTO(out, rc = 0);
2246
2247         dt_obj = mdd_object_child(obj);
2248         if (unlikely(!dt_try_as_dir(env, dt_obj)))
2249                 GOTO(out, rc = -ENOTDIR);
2250
2251         iops = &dt_obj->do_index_ops->dio_it;
2252         di = iops->init(env, dt_obj, lfsck->ml_args_dir, BYPASS_CAPA);
2253         if (IS_ERR(di))
2254                 GOTO(out, rc = PTR_ERR(di));
2255
2256         rc = iops->load(env, di, 0);
2257         if (rc == 0)
2258                 rc = iops->next(env, di);
2259         else if (rc > 0)
2260                 rc = 0;
2261
2262         if (rc != 0) {
2263                 iops->put(env, di);
2264                 iops->fini(env, di);
2265                 GOTO(out, rc);
2266         }
2267
2268         mdd_object_get(obj);
2269         lfsck->ml_obj_dir = dt_obj;
2270         spin_lock(&lfsck->ml_lock);
2271         lfsck->ml_di_dir = di;
2272         spin_unlock(&lfsck->ml_lock);
2273
2274         GOTO(out, rc = 0);
2275
2276 out:
2277         if (rc < 0)
2278                 mdd_lfsck_fail(env, lfsck, false);
2279         return (rc > 0 ? 0 : rc);
2280 }
2281
2282 static int mdd_lfsck_exec_dir(const struct lu_env *env, struct md_lfsck *lfsck,
2283                               struct mdd_object *obj, struct lu_dirent *ent)
2284 {
2285         struct lfsck_component *com;
2286         int                     rc;
2287
2288         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2289                 rc = com->lc_ops->lfsck_exec_dir(env, com, obj, ent);
2290                 if (rc != 0)
2291                         return rc;
2292         }
2293         return 0;
2294 }
2295
2296 static int mdd_lfsck_post(const struct lu_env *env, struct md_lfsck *lfsck,
2297                           int result)
2298 {
2299         struct lfsck_component *com;
2300         struct lfsck_component *next;
2301         int                     rc;
2302
2303         mdd_lfsck_pos_fill(env, lfsck, &lfsck->ml_pos_current, false);
2304         cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_scan, lc_link) {
2305                 rc = com->lc_ops->lfsck_post(env, com, result, false);
2306                 if (rc != 0)
2307                         return rc;
2308         }
2309
2310         lfsck->ml_time_last_checkpoint = cfs_time_current();
2311         lfsck->ml_time_next_checkpoint = lfsck->ml_time_last_checkpoint +
2312                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2313         return result;
2314 }
2315
2316 static int mdd_lfsck_double_scan(const struct lu_env *env,
2317                                  struct md_lfsck *lfsck)
2318 {
2319         struct lfsck_component *com;
2320         struct lfsck_component *next;
2321         int                     rc;
2322
2323         cfs_list_for_each_entry_safe(com, next, &lfsck->ml_list_double_scan,
2324                                      lc_link) {
2325                 if (lfsck->ml_bookmark_ram.lb_param & LPF_DRYRUN)
2326                         com->lc_journal = 0;
2327
2328                 rc = com->lc_ops->lfsck_double_scan(env, com);
2329                 if (rc != 0)
2330                         return rc;
2331         }
2332         return 0;
2333 }
2334
2335 /* LFSCK engines */
2336
2337 static int mdd_lfsck_dir_engine(const struct lu_env *env,
2338                                 struct md_lfsck *lfsck)
2339 {
2340         struct mdd_thread_info  *info   = mdd_env_info(env);
2341         struct mdd_device       *mdd    = mdd_lfsck2mdd(lfsck);
2342         const struct dt_it_ops  *iops   =
2343                         &lfsck->ml_obj_dir->do_index_ops->dio_it;
2344         struct dt_it            *di     = lfsck->ml_di_dir;
2345         struct lu_dirent        *ent    = &info->mti_ent;
2346         struct lu_fid           *fid    = &info->mti_fid;
2347         struct lfsck_bookmark   *bk     = &lfsck->ml_bookmark_ram;
2348         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
2349         int                      rc;
2350         ENTRY;
2351
2352         do {
2353                 struct mdd_object *child;
2354
2355                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY2) &&
2356                     cfs_fail_val > 0) {
2357                         struct l_wait_info lwi;
2358
2359                         lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2360                                           NULL, NULL);
2361                         l_wait_event(thread->t_ctl_waitq,
2362                                      !thread_is_running(thread),
2363                                      &lwi);
2364                 }
2365
2366                 lfsck->ml_new_scanned++;
2367                 rc = iops->rec(env, di, (struct dt_rec *)ent,
2368                                lfsck->ml_args_dir);
2369                 if (rc != 0) {
2370                         mdd_lfsck_fail(env, lfsck, true);
2371                         if (bk->lb_param & LPF_FAILOUT)
2372                                 RETURN(rc);
2373                         else
2374                                 goto checkpoint;
2375                 }
2376
2377                 mdd_lfsck_unpack_ent(ent);
2378                 if (ent->lde_attrs & LUDA_IGNORE)
2379                         goto checkpoint;
2380
2381                 *fid = ent->lde_fid;
2382                 child = mdd_object_find(env, mdd, fid);
2383                 if (child == NULL) {
2384                         goto checkpoint;
2385                 } else if (IS_ERR(child)) {
2386                         mdd_lfsck_fail(env, lfsck, true);
2387                         if (bk->lb_param & LPF_FAILOUT)
2388                                 RETURN(PTR_ERR(child));
2389                         else
2390                                 goto checkpoint;
2391                 }
2392
2393                 /* XXX: need more processing for remote object in the future. */
2394                 if (mdd_object_exists(child) && !mdd_object_remote(child))
2395                         rc = mdd_lfsck_exec_dir(env, lfsck, child, ent);
2396                 mdd_object_put(env, child);
2397                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2398                         RETURN(rc);
2399
2400 checkpoint:
2401                 rc = mdd_lfsck_checkpoint(env, lfsck);
2402                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2403                         RETURN(rc);
2404
2405                 /* Rate control. */
2406                 mdd_lfsck_control_speed(lfsck);
2407                 if (unlikely(!thread_is_running(thread)))
2408                         RETURN(0);
2409
2410                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
2411                         spin_lock(&lfsck->ml_lock);
2412                         thread_set_flags(thread, SVC_STOPPING);
2413                         spin_unlock(&lfsck->ml_lock);
2414                         RETURN(-EINVAL);
2415                 }
2416
2417                 rc = iops->next(env, di);
2418         } while (rc == 0);
2419
2420         if (rc > 0 && !lfsck->ml_oit_over)
2421                 mdd_lfsck_close_dir(env, lfsck);
2422
2423         RETURN(rc);
2424 }
2425
2426 static int mdd_lfsck_oit_engine(const struct lu_env *env,
2427                                 struct md_lfsck *lfsck)
2428 {
2429         struct mdd_thread_info  *info   = mdd_env_info(env);
2430         struct mdd_device       *mdd    = mdd_lfsck2mdd(lfsck);
2431         const struct dt_it_ops  *iops   =
2432                                 &lfsck->ml_obj_oit->do_index_ops->dio_it;
2433         struct dt_it            *di     = lfsck->ml_di_oit;
2434         struct lu_fid           *fid    = &info->mti_fid;
2435         struct lfsck_bookmark   *bk     = &lfsck->ml_bookmark_ram;
2436         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
2437         int                      rc;
2438         ENTRY;
2439
2440         do {
2441                 struct mdd_object *target;
2442
2443                 if (lfsck->ml_di_dir != NULL) {
2444                         rc = mdd_lfsck_dir_engine(env, lfsck);
2445                         if (rc <= 0)
2446                                 RETURN(rc);
2447                 }
2448
2449                 if (unlikely(lfsck->ml_oit_over))
2450                         RETURN(1);
2451
2452                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY1) &&
2453                     cfs_fail_val > 0) {
2454                         struct l_wait_info lwi;
2455
2456                         lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2457                                           NULL, NULL);
2458                         l_wait_event(thread->t_ctl_waitq,
2459                                      !thread_is_running(thread),
2460                                      &lwi);
2461                 }
2462
2463                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
2464                         RETURN(0);
2465
2466                 lfsck->ml_current_oit_processed = 1;
2467                 lfsck->ml_new_scanned++;
2468                 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
2469                 if (rc != 0) {
2470                         mdd_lfsck_fail(env, lfsck, true);
2471                         if (bk->lb_param & LPF_FAILOUT)
2472                                 RETURN(rc);
2473                         else
2474                                 goto checkpoint;
2475                 }
2476
2477                 target = mdd_object_find(env, mdd, fid);
2478                 if (target == NULL) {
2479                         goto checkpoint;
2480                 } else if (IS_ERR(target)) {
2481                         mdd_lfsck_fail(env, lfsck, true);
2482                         if (bk->lb_param & LPF_FAILOUT)
2483                                 RETURN(PTR_ERR(target));
2484                         else
2485                                 goto checkpoint;
2486                 }
2487
2488                 /* XXX: In fact, low layer otable-based iteration should not
2489                  *      return agent object. But before LU-2646 resolved, we
2490                  *      need more processing for agent object. */
2491                 if (mdd_object_exists(target) && !mdd_object_remote(target))
2492                         rc = mdd_lfsck_exec_oit(env, lfsck, target);
2493                 mdd_object_put(env, target);
2494                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2495                         RETURN(rc);
2496
2497 checkpoint:
2498                 rc = mdd_lfsck_checkpoint(env, lfsck);
2499                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
2500                         RETURN(rc);
2501
2502                 /* Rate control. */
2503                 mdd_lfsck_control_speed(lfsck);
2504
2505                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
2506                         spin_lock(&lfsck->ml_lock);
2507                         thread_set_flags(thread, SVC_STOPPING);
2508                         spin_unlock(&lfsck->ml_lock);
2509                         RETURN(-EINVAL);
2510                 }
2511
2512                 rc = iops->next(env, di);
2513                 if (unlikely(rc > 0))
2514                         lfsck->ml_oit_over = 1;
2515                 else if (likely(rc == 0))
2516                         lfsck->ml_current_oit_processed = 0;
2517
2518                 if (unlikely(!thread_is_running(thread)))
2519                         RETURN(0);
2520         } while (rc == 0 || lfsck->ml_di_dir != NULL);
2521
2522         RETURN(rc);
2523 }
2524
2525 static int mdd_lfsck_main(void *args)
2526 {
2527         struct lu_env            env;
2528         struct md_lfsck         *lfsck    = (struct md_lfsck *)args;
2529         struct ptlrpc_thread    *thread   = &lfsck->ml_thread;
2530         struct dt_object        *oit_obj  = lfsck->ml_obj_oit;
2531         const struct dt_it_ops  *oit_iops = &oit_obj->do_index_ops->dio_it;
2532         struct dt_it            *oit_di;
2533         int                      rc;
2534         ENTRY;
2535
2536         cfs_daemonize("lfsck");
2537         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2538         if (rc != 0) {
2539                 CERROR("%s: LFSCK, fail to init env, rc = %d\n",
2540                        mdd_lfsck2name(lfsck), rc);
2541                 GOTO(noenv, rc);
2542         }
2543
2544         oit_di = oit_iops->init(&env, oit_obj, lfsck->ml_args_oit, BYPASS_CAPA);
2545         if (IS_ERR(oit_di)) {
2546                 rc = PTR_ERR(oit_di);
2547                 CERROR("%s: LFSCK, fail to init iteration, rc = %d\n",
2548                        mdd_lfsck2name(lfsck), rc);
2549                 GOTO(fini_env, rc);
2550         }
2551
2552         spin_lock(&lfsck->ml_lock);
2553         lfsck->ml_di_oit = oit_di;
2554         spin_unlock(&lfsck->ml_lock);
2555         rc = mdd_lfsck_prep(&env, lfsck);
2556         if (rc != 0)
2557                 GOTO(fini_oit, rc);
2558
2559         CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = 0x%x, dir_flags = 0x%x, "
2560                "oit_cookie = "LPU64", dir_cookie = "LPU64", parent = "DFID
2561                ", pid = %d\n", lfsck->ml_args_oit, lfsck->ml_args_dir,
2562                lfsck->ml_pos_current.lp_oit_cookie,
2563                lfsck->ml_pos_current.lp_dir_cookie,
2564                PFID(&lfsck->ml_pos_current.lp_dir_parent),
2565                cfs_curproc_pid());
2566
2567         spin_lock(&lfsck->ml_lock);
2568         thread_set_flags(thread, SVC_RUNNING);
2569         spin_unlock(&lfsck->ml_lock);
2570         cfs_waitq_broadcast(&thread->t_ctl_waitq);
2571
2572         if (!cfs_list_empty(&lfsck->ml_list_scan) ||
2573             cfs_list_empty(&lfsck->ml_list_double_scan))
2574                 rc = mdd_lfsck_oit_engine(&env, lfsck);
2575         else
2576                 rc = 1;
2577
2578         CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = 0x%x, dir_flags = 0x%x, "
2579                "oit_cookie = "LPU64", dir_cookie = "LPU64", parent = "DFID
2580                ", pid = %d, rc = %d\n", lfsck->ml_args_oit, lfsck->ml_args_dir,
2581                lfsck->ml_pos_current.lp_oit_cookie,
2582                lfsck->ml_pos_current.lp_dir_cookie,
2583                PFID(&lfsck->ml_pos_current.lp_dir_parent),
2584                cfs_curproc_pid(), rc);
2585
2586         if (lfsck->ml_paused && cfs_list_empty(&lfsck->ml_list_scan))
2587                 oit_iops->put(&env, oit_di);
2588
2589         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
2590                 rc = mdd_lfsck_post(&env, lfsck, rc);
2591         if (lfsck->ml_di_dir != NULL)
2592                 mdd_lfsck_close_dir(&env, lfsck);
2593
2594 fini_oit:
2595         spin_lock(&lfsck->ml_lock);
2596         lfsck->ml_di_oit = NULL;
2597         spin_unlock(&lfsck->ml_lock);
2598
2599         oit_iops->fini(&env, oit_di);
2600         if (rc == 1) {
2601                 if (!cfs_list_empty(&lfsck->ml_list_double_scan))
2602                         rc = mdd_lfsck_double_scan(&env, lfsck);
2603                 else
2604                         rc = 0;
2605         }
2606
2607         /* XXX: Purge the pinned objects in the future. */
2608
2609 fini_env:
2610         lu_env_fini(&env);
2611
2612 noenv:
2613         spin_lock(&lfsck->ml_lock);
2614         thread_set_flags(thread, SVC_STOPPED);
2615         cfs_waitq_broadcast(&thread->t_ctl_waitq);
2616         spin_unlock(&lfsck->ml_lock);
2617         return rc;
2618 }
2619
2620 /* external interfaces */
2621
2622 int mdd_lfsck_set_speed(const struct lu_env *env, struct md_lfsck *lfsck,
2623                         __u32 limit)
2624 {
2625         int rc;
2626
2627         mutex_lock(&lfsck->ml_mutex);
2628         __mdd_lfsck_set_speed(lfsck, limit);
2629         rc = mdd_lfsck_bookmark_store(env, lfsck);
2630         mutex_unlock(&lfsck->ml_mutex);
2631         return rc;
2632 }
2633
2634 int mdd_lfsck_dump(const struct lu_env *env, struct md_lfsck *lfsck,
2635                    __u16 type, char *buf, int len)
2636 {
2637         struct lfsck_component *com;
2638         int                     rc;
2639
2640         if (!lfsck->ml_initialized)
2641                 return -ENODEV;
2642
2643         com = mdd_lfsck_component_find(lfsck, type);
2644         if (com == NULL)
2645                 return -ENOTSUPP;
2646
2647         rc = com->lc_ops->lfsck_dump(env, com, buf, len);
2648         mdd_lfsck_component_put(env, com);
2649         return rc;
2650 }
2651
2652 int mdd_lfsck_start(const struct lu_env *env, struct md_lfsck *lfsck,
2653                     struct lfsck_start *start)
2654 {
2655         struct lfsck_bookmark  *bk     = &lfsck->ml_bookmark_ram;
2656         struct ptlrpc_thread   *thread = &lfsck->ml_thread;
2657         struct lfsck_component *com;
2658         struct l_wait_info      lwi    = { 0 };
2659         bool                    dirty  = false;
2660         int                     rc     = 0;
2661         __u16                   valid  = 0;
2662         __u16                   flags  = 0;
2663         ENTRY;
2664
2665         if (lfsck->ml_obj_oit == NULL)
2666                 RETURN(-ENOTSUPP);
2667
2668         /* start == NULL means auto trigger paused LFSCK. */
2669         if ((start == NULL) &&
2670             (cfs_list_empty(&lfsck->ml_list_scan) ||
2671              OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2672                 RETURN(0);
2673
2674         mutex_lock(&lfsck->ml_mutex);
2675         spin_lock(&lfsck->ml_lock);
2676         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2677                 spin_unlock(&lfsck->ml_lock);
2678                 mutex_unlock(&lfsck->ml_mutex);
2679                 RETURN(-EALREADY);
2680         }
2681
2682         spin_unlock(&lfsck->ml_lock);
2683
2684         lfsck->ml_paused = 0;
2685         lfsck->ml_oit_over = 0;
2686         lfsck->ml_drop_dryrun = 0;
2687         lfsck->ml_new_scanned = 0;
2688
2689         /* For auto trigger. */
2690         if (start == NULL)
2691                 goto trigger;
2692
2693         start->ls_version = bk->lb_version;
2694         if (start->ls_valid & LSV_SPEED_LIMIT) {
2695                 __mdd_lfsck_set_speed(lfsck, start->ls_speed_limit);
2696                 dirty = true;
2697         }
2698
2699         if (start->ls_valid & LSV_ERROR_HANDLE) {
2700                 valid |= DOIV_ERROR_HANDLE;
2701                 if (start->ls_flags & LPF_FAILOUT)
2702                         flags |= DOIF_FAILOUT;
2703
2704                 if ((start->ls_flags & LPF_FAILOUT) &&
2705                     !(bk->lb_param & LPF_FAILOUT)) {
2706                         bk->lb_param |= LPF_FAILOUT;
2707                         dirty = true;
2708                 } else if (!(start->ls_flags & LPF_FAILOUT) &&
2709                            (bk->lb_param & LPF_FAILOUT)) {
2710                         bk->lb_param &= ~LPF_FAILOUT;
2711                         dirty = true;
2712                 }
2713         }
2714
2715         if (start->ls_valid & LSV_DRYRUN) {
2716                 if ((start->ls_flags & LPF_DRYRUN) &&
2717                     !(bk->lb_param & LPF_DRYRUN)) {
2718                         bk->lb_param |= LPF_DRYRUN;
2719                         dirty = true;
2720                 } else if (!(start->ls_flags & LPF_DRYRUN) &&
2721                            (bk->lb_param & LPF_DRYRUN)) {
2722                         bk->lb_param &= ~LPF_DRYRUN;
2723                         lfsck->ml_drop_dryrun = 1;
2724                         dirty = true;
2725                 }
2726         }
2727
2728         if (dirty) {
2729                 rc = mdd_lfsck_bookmark_store(env, lfsck);
2730                 if (rc != 0)
2731                         GOTO(out, rc);
2732         }
2733
2734         if (start->ls_flags & LPF_RESET)
2735                 flags |= DOIF_RESET;
2736
2737         if (start->ls_active != 0) {
2738                 struct lfsck_component *next;
2739                 __u16 type = 1;
2740
2741                 if (start->ls_active == LFSCK_TYPES_ALL)
2742                         start->ls_active = LFSCK_TYPES_SUPPORTED;
2743
2744                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2745                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2746                         GOTO(out, rc = -ENOTSUPP);
2747                 }
2748
2749                 cfs_list_for_each_entry_safe(com, next,
2750                                              &lfsck->ml_list_scan, lc_link) {
2751                         if (!(com->lc_type & start->ls_active)) {
2752                                 rc = com->lc_ops->lfsck_post(env, com, 0,
2753                                                              false);
2754                                 if (rc != 0)
2755                                         GOTO(out, rc);
2756                         }
2757                 }
2758
2759                 while (start->ls_active != 0) {
2760                         if (type & start->ls_active) {
2761                                 com = __mdd_lfsck_component_find(lfsck, type,
2762                                                         &lfsck->ml_list_idle);
2763                                 if (com != NULL) {
2764                                         /* The component status will be updated
2765                                          * when its prep() is called later by
2766                                          * the LFSCK main engine. */
2767                                         cfs_list_del_init(&com->lc_link);
2768                                         cfs_list_add_tail(&com->lc_link,
2769                                                           &lfsck->ml_list_scan);
2770                                 }
2771                                 start->ls_active &= ~type;
2772                         }
2773                         type <<= 1;
2774                 }
2775         }
2776
2777         cfs_list_for_each_entry(com, &lfsck->ml_list_scan, lc_link) {
2778                 start->ls_active |= com->lc_type;
2779                 if (flags & DOIF_RESET) {
2780                         rc = com->lc_ops->lfsck_reset(env, com, false);
2781                         if (rc != 0)
2782                                 GOTO(out, rc);
2783                 }
2784         }
2785
2786 trigger:
2787         lfsck->ml_args_dir = LUDA_64BITHASH | LUDA_VERIFY;
2788         if (bk->lb_param & LPF_DRYRUN)
2789                 lfsck->ml_args_dir |= LUDA_VERIFY_DRYRUN;
2790
2791         if (bk->lb_param & LPF_FAILOUT) {
2792                 valid |= DOIV_ERROR_HANDLE;
2793                 flags |= DOIF_FAILOUT;
2794         }
2795
2796         if (!cfs_list_empty(&lfsck->ml_list_scan))
2797                 flags |= DOIF_OUTUSED;
2798
2799         lfsck->ml_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2800         thread_set_flags(thread, 0);
2801         rc = cfs_create_thread(mdd_lfsck_main, lfsck, 0);
2802         if (rc < 0)
2803                 CERROR("%s: cannot start LFSCK thread, rc = %d\n",
2804                        mdd_lfsck2name(lfsck), rc);
2805         else
2806                 l_wait_event(thread->t_ctl_waitq,
2807                              thread_is_running(thread) ||
2808                              thread_is_stopped(thread),
2809                              &lwi);
2810
2811         GOTO(out, rc = 0);
2812
2813 out:
2814         mutex_unlock(&lfsck->ml_mutex);
2815         return (rc < 0 ? rc : 0);
2816 }
2817
2818 int mdd_lfsck_stop(const struct lu_env *env, struct md_lfsck *lfsck,
2819                    bool pause)
2820 {
2821         struct ptlrpc_thread *thread = &lfsck->ml_thread;
2822         struct l_wait_info    lwi    = { 0 };
2823         ENTRY;
2824
2825         if (!lfsck->ml_initialized)
2826                 RETURN(0);
2827
2828         mutex_lock(&lfsck->ml_mutex);
2829         spin_lock(&lfsck->ml_lock);
2830         if (thread_is_init(thread) || thread_is_stopped(thread)) {
2831                 spin_unlock(&lfsck->ml_lock);
2832                 mutex_unlock(&lfsck->ml_mutex);
2833                 RETURN(-EALREADY);
2834         }
2835
2836         if (pause)
2837                 lfsck->ml_paused = 1;
2838         thread_set_flags(thread, SVC_STOPPING);
2839         /* The LFSCK thread may be sleeping on low layer wait queue,
2840          * wake it up. */
2841         if (likely(lfsck->ml_di_oit != NULL))
2842                 lfsck->ml_obj_oit->do_index_ops->dio_it.put(env,
2843                                                             lfsck->ml_di_oit);
2844         spin_unlock(&lfsck->ml_lock);
2845
2846         cfs_waitq_broadcast(&thread->t_ctl_waitq);
2847         l_wait_event(thread->t_ctl_waitq,
2848                      thread_is_stopped(thread),
2849                      &lwi);
2850         mutex_unlock(&lfsck->ml_mutex);
2851
2852         RETURN(0);
2853 }
2854
2855 static const struct lu_fid lfsck_it_fid = { .f_seq = FID_SEQ_LOCAL_FILE,
2856                                             .f_oid = OTABLE_IT_OID,
2857                                             .f_ver = 0 };
2858
2859 int mdd_lfsck_setup(const struct lu_env *env, struct mdd_device *mdd)
2860 {
2861         struct md_lfsck         *lfsck = &mdd->mdd_lfsck;
2862         struct dt_object        *obj;
2863         struct lu_fid            fid;
2864         int                      rc;
2865
2866         ENTRY;
2867
2868         LASSERT(!lfsck->ml_initialized);
2869
2870         lfsck->ml_initialized = 1;
2871         mutex_init(&lfsck->ml_mutex);
2872         spin_lock_init(&lfsck->ml_lock);
2873         CFS_INIT_LIST_HEAD(&lfsck->ml_list_scan);
2874         CFS_INIT_LIST_HEAD(&lfsck->ml_list_dir);
2875         CFS_INIT_LIST_HEAD(&lfsck->ml_list_double_scan);
2876         CFS_INIT_LIST_HEAD(&lfsck->ml_list_idle);
2877         cfs_waitq_init(&lfsck->ml_thread.t_ctl_waitq);
2878
2879         obj = dt_locate(env, mdd->mdd_bottom, &lfsck_it_fid);
2880         if (IS_ERR(obj))
2881                 RETURN(PTR_ERR(obj));
2882
2883         lfsck->ml_obj_oit = obj;
2884         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
2885         if (rc != 0) {
2886                 if (rc == -ENOTSUPP)
2887                         RETURN(0);
2888                 GOTO(out, rc);
2889         }
2890
2891         /* LFSCK bookmark */
2892         fid_zero(&fid);
2893         rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
2894                                    lfsck_bookmark_name,
2895                                    S_IFREG | S_IRUGO | S_IWUSR, &fid);
2896         if (rc < 0)
2897                 GOTO(out, rc);
2898
2899         obj = dt_locate(env, mdd->mdd_bottom, &fid);
2900         if (IS_ERR(obj))
2901                 GOTO(out, rc = PTR_ERR(obj));
2902
2903         LASSERT(lu_object_exists(&obj->do_lu));
2904         lfsck->ml_bookmark_obj = obj;
2905
2906         rc = mdd_lfsck_bookmark_load(env, lfsck);
2907         if (rc == -ENODATA)
2908                 rc = mdd_lfsck_bookmark_init(env, lfsck);
2909         if (rc != 0)
2910                 GOTO(out, rc);
2911
2912         rc = mdd_lfsck_namespace_setup(env, lfsck);
2913         if (rc < 0)
2914                 GOTO(out, rc);
2915         /* XXX: LFSCK components initialization to be added here. */
2916         RETURN(0);
2917 out:
2918         lu_object_put(env, &lfsck->ml_obj_oit->do_lu);
2919         lfsck->ml_obj_oit = NULL;
2920         return 0;
2921 }
2922
2923 void mdd_lfsck_cleanup(const struct lu_env *env, struct mdd_device *mdd)
2924 {
2925         struct md_lfsck         *lfsck  = &mdd->mdd_lfsck;
2926         struct ptlrpc_thread    *thread = &lfsck->ml_thread;
2927         struct lfsck_component  *com;
2928
2929         if (!lfsck->ml_initialized)
2930                 return;
2931
2932         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
2933
2934         if (lfsck->ml_obj_oit != NULL) {
2935                 lu_object_put(env, &lfsck->ml_obj_oit->do_lu);
2936                 lfsck->ml_obj_oit = NULL;
2937         }
2938
2939         LASSERT(lfsck->ml_obj_dir == NULL);
2940
2941         if (lfsck->ml_bookmark_obj != NULL) {
2942                 lu_object_put(env, &lfsck->ml_bookmark_obj->do_lu);
2943                 lfsck->ml_bookmark_obj = NULL;
2944         }
2945
2946         while (!cfs_list_empty(&lfsck->ml_list_scan)) {
2947                 com = cfs_list_entry(lfsck->ml_list_scan.next,
2948                                      struct lfsck_component,
2949                                      lc_link);
2950                 mdd_lfsck_component_cleanup(env, com);
2951         }
2952
2953         LASSERT(cfs_list_empty(&lfsck->ml_list_dir));
2954
2955         while (!cfs_list_empty(&lfsck->ml_list_double_scan)) {
2956                 com = cfs_list_entry(lfsck->ml_list_double_scan.next,
2957                                      struct lfsck_component,
2958                                      lc_link);
2959                 mdd_lfsck_component_cleanup(env, com);
2960         }
2961
2962         while (!cfs_list_empty(&lfsck->ml_list_idle)) {
2963                 com = cfs_list_entry(lfsck->ml_list_idle.next,
2964                                      struct lfsck_component,
2965                                      lc_link);
2966                 mdd_lfsck_component_cleanup(env, com);
2967         }
2968 }