Whamcloud - gitweb
LU-13124 scrub: check for multiple linked file
[fs/lustre-release.git] / lustre / lfsck / lfsck_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2017, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_lib.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LFSCK
32
33 #include <linux/kthread.h>
34 #include <linux/sched.h>
35 #include <linux/list.h>
36 #include <linux/delay.h>
37 #include <lu_object.h>
38 #include <dt_object.h>
39 #include <md_object.h>
40 #include <lustre_fld.h>
41 #include <lustre_lib.h>
42 #include <lustre_net.h>
43 #include <lustre_lfsck.h>
44
45 #include "lfsck_internal.h"
46
47 #define LFSCK_CHECKPOINT_SKIP   1
48
49 /* define lfsck thread key */
50 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
51
52 static void lfsck_key_fini(const struct lu_context *ctx,
53                            struct lu_context_key *key, void *data)
54 {
55         struct lfsck_thread_info *info = data;
56
57         lu_buf_free(&info->lti_linkea_buf);
58         lu_buf_free(&info->lti_linkea_buf2);
59         lu_buf_free(&info->lti_big_buf);
60         OBD_FREE_PTR(info);
61 }
62
63 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
64 LU_KEY_INIT_GENERIC(lfsck);
65
66 static LIST_HEAD(lfsck_instance_list);
67 static LIST_HEAD(lfsck_ost_orphan_list);
68 static LIST_HEAD(lfsck_mdt_orphan_list);
69 static DEFINE_SPINLOCK(lfsck_instance_lock);
70
71 const char *const lfsck_flags_names[] = {
72         "scanned-once",
73         "inconsistent",
74         "upgrade",
75         "incomplete",
76         "crashed_lastid",
77         NULL
78 };
79
80 const char *const lfsck_param_names[] = {
81         NULL,
82         "failout",
83         "dryrun",
84         "all_targets",
85         "broadcast",
86         "orphan",
87         "create_ostobj",
88         "create_mdtobj",
89         NULL,
90         "delay_create_ostobj",
91         NULL
92 };
93
94 enum lfsck_verify_lpf_types {
95         LVLT_BY_BOOKMARK        = 0,
96         LVLT_BY_NAMEENTRY       = 1,
97 };
98
99 static inline void
100 lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
101 {
102         if (type == LFSCK_TYPE_LAYOUT) {
103                 ltd->ltd_layout_status = LS_MAX;
104                 ltd->ltd_layout_repaired = 0;
105         } else {
106                 ltd->ltd_namespace_status = LS_MAX;
107                 ltd->ltd_namespace_repaired = 0;
108         }
109 }
110
111 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
112 {
113         spin_lock_init(&ltds->ltd_lock);
114         init_rwsem(&ltds->ltd_rw_sem);
115         INIT_LIST_HEAD(&ltds->ltd_orphan);
116         ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
117         if (ltds->ltd_tgts_bitmap == NULL)
118                 return -ENOMEM;
119
120         return 0;
121 }
122
123 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
124 {
125         struct lfsck_tgt_desc   *ltd;
126         struct lfsck_tgt_desc   *next;
127         int                      idx;
128
129         down_write(&ltds->ltd_rw_sem);
130
131         list_for_each_entry_safe(ltd, next, &ltds->ltd_orphan,
132                                  ltd_orphan_list) {
133                 list_del_init(&ltd->ltd_orphan_list);
134                 lfsck_tgt_put(ltd);
135         }
136
137         if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
138                 up_write(&ltds->ltd_rw_sem);
139
140                 return;
141         }
142
143         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
144                 ltd = lfsck_ltd2tgt(ltds, idx);
145                 if (likely(ltd != NULL)) {
146                         LASSERT(list_empty(&ltd->ltd_layout_list));
147                         LASSERT(list_empty(&ltd->ltd_layout_phase_list));
148                         LASSERT(list_empty(&ltd->ltd_namespace_list));
149                         LASSERT(list_empty(&ltd->ltd_namespace_phase_list));
150
151                         ltds->ltd_tgtnr--;
152                         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
153                         lfsck_assign_tgt(ltds, NULL, idx);
154                         lfsck_tgt_put(ltd);
155                 }
156         }
157
158         LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
159                  ltds->ltd_tgtnr);
160
161         for (idx = 0; idx < ARRAY_SIZE(ltds->ltd_tgts_idx); idx++) {
162                 if (ltds->ltd_tgts_idx[idx] != NULL) {
163                         OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
164                         ltds->ltd_tgts_idx[idx] = NULL;
165                 }
166         }
167
168         CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
169         ltds->ltd_tgts_bitmap = NULL;
170         up_write(&ltds->ltd_rw_sem);
171 }
172
173 static int __lfsck_add_target(const struct lu_env *env,
174                               struct lfsck_instance *lfsck,
175                               struct lfsck_tgt_desc *ltd,
176                               bool for_ost, bool locked)
177 {
178         struct lfsck_tgt_descs *ltds;
179         __u32                   index = ltd->ltd_index;
180         int                     rc    = 0;
181         ENTRY;
182
183         if (for_ost)
184                 ltds = &lfsck->li_ost_descs;
185         else
186                 ltds = &lfsck->li_mdt_descs;
187
188         if (!locked)
189                 down_write(&ltds->ltd_rw_sem);
190
191         LASSERT(ltds->ltd_tgts_bitmap != NULL);
192
193         if (index >= ltds->ltd_tgts_bitmap->size) {
194                 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
195                                     (__u32)BITS_PER_LONG);
196                 struct cfs_bitmap *old_bitmap = ltds->ltd_tgts_bitmap;
197                 struct cfs_bitmap *new_bitmap;
198
199                 while (newsize < index + 1)
200                         newsize <<= 1;
201
202                 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
203                 if (new_bitmap == NULL)
204                         GOTO(unlock, rc = -ENOMEM);
205
206                 if (ltds->ltd_tgtnr > 0)
207                         cfs_bitmap_copy(new_bitmap, old_bitmap);
208                 ltds->ltd_tgts_bitmap = new_bitmap;
209                 CFS_FREE_BITMAP(old_bitmap);
210         }
211
212         if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
213                 CERROR("%s: the device %s (%u) is registered already\n",
214                        lfsck_lfsck2name(lfsck),
215                        ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
216                 GOTO(unlock, rc = -EEXIST);
217         }
218
219         if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
220                 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
221                 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
222                         GOTO(unlock, rc = -ENOMEM);
223         }
224
225         lfsck_assign_tgt(ltds, ltd, index);
226         cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
227         ltds->ltd_tgtnr++;
228
229         GOTO(unlock, rc = 0);
230
231 unlock:
232         if (!locked)
233                 up_write(&ltds->ltd_rw_sem);
234
235         return rc;
236 }
237
238 static int lfsck_add_target_from_orphan(const struct lu_env *env,
239                                         struct lfsck_instance *lfsck)
240 {
241         struct lfsck_tgt_descs  *ltds    = &lfsck->li_ost_descs;
242         struct lfsck_tgt_desc   *ltd;
243         struct lfsck_tgt_desc   *next;
244         struct list_head        *head    = &lfsck_ost_orphan_list;
245         int                      rc;
246         bool                     for_ost = true;
247
248 again:
249         spin_lock(&lfsck_instance_lock);
250         list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
251                 if (ltd->ltd_key == lfsck->li_bottom)
252                         list_move_tail(&ltd->ltd_orphan_list,
253                                        &ltds->ltd_orphan);
254         }
255         spin_unlock(&lfsck_instance_lock);
256
257         down_write(&ltds->ltd_rw_sem);
258         while (!list_empty(&ltds->ltd_orphan)) {
259                 ltd = list_entry(ltds->ltd_orphan.next,
260                                  struct lfsck_tgt_desc,
261                                  ltd_orphan_list);
262                 list_del_init(&ltd->ltd_orphan_list);
263                 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
264                 /* Do not hold the semaphore for too long time. */
265                 up_write(&ltds->ltd_rw_sem);
266                 if (rc != 0)
267                         return rc;
268
269                 down_write(&ltds->ltd_rw_sem);
270         }
271         up_write(&ltds->ltd_rw_sem);
272
273         if (for_ost) {
274                 ltds = &lfsck->li_mdt_descs;
275                 head = &lfsck_mdt_orphan_list;
276                 for_ost = false;
277                 goto again;
278         }
279
280         return 0;
281 }
282
283 static inline struct lfsck_component *
284 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
285                        struct list_head *list)
286 {
287         struct lfsck_component *com;
288
289         list_for_each_entry(com, list, lc_link) {
290                 if (com->lc_type == type)
291                         return com;
292         }
293         return NULL;
294 }
295
296 struct lfsck_component *
297 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
298 {
299         struct lfsck_component *com;
300
301         spin_lock(&lfsck->li_lock);
302         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
303         if (com != NULL)
304                 goto unlock;
305
306         com = __lfsck_component_find(lfsck, type,
307                                      &lfsck->li_list_double_scan);
308         if (com != NULL)
309                 goto unlock;
310
311         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
312
313 unlock:
314         if (com != NULL)
315                 lfsck_component_get(com);
316         spin_unlock(&lfsck->li_lock);
317         return com;
318 }
319
320 void lfsck_component_cleanup(const struct lu_env *env,
321                              struct lfsck_component *com)
322 {
323         if (!list_empty(&com->lc_link))
324                 list_del_init(&com->lc_link);
325         if (!list_empty(&com->lc_link_dir))
326                 list_del_init(&com->lc_link_dir);
327
328         lfsck_component_put(env, com);
329 }
330
331 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
332                     struct lu_fid *fid, bool locked)
333 {
334         struct lfsck_bookmark   *bk = &lfsck->li_bookmark_ram;
335         int                      rc = 0;
336         ENTRY;
337
338         if (!locked)
339                 mutex_lock(&lfsck->li_mutex);
340
341         rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
342         if (rc >= 0) {
343                 bk->lb_last_fid = *fid;
344                 /* We do not care about whether the subsequent sub-operations
345                  * failed or not. The worst case is that one FID is lost that
346                  * is not a big issue for the LFSCK since it is relative rare
347                  * for LFSCK create. */
348                 rc = lfsck_bookmark_store(env, lfsck);
349         }
350
351         if (!locked)
352                 mutex_unlock(&lfsck->li_mutex);
353
354         RETURN(rc);
355 }
356
357 static int __lfsck_ibits_lock(const struct lu_env *env,
358                               struct lfsck_instance *lfsck,
359                               struct dt_object *obj, struct ldlm_res_id *resid,
360                               struct lustre_handle *lh, __u64 bits,
361                               enum ldlm_mode mode)
362 {
363         struct lfsck_thread_info        *info   = lfsck_env_info(env);
364         union ldlm_policy_data          *policy = &info->lti_policy;
365         __u64                            flags  = LDLM_FL_ATOMIC_CB;
366         int                              rc;
367
368         LASSERT(lfsck->li_namespace != NULL);
369
370         memset(policy, 0, sizeof(*policy));
371         policy->l_inodebits.bits = bits;
372         if (dt_object_remote(obj)) {
373                 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
374
375                 memset(einfo, 0, sizeof(*einfo));
376                 einfo->ei_type = LDLM_IBITS;
377                 einfo->ei_mode = mode;
378                 einfo->ei_cb_bl = ldlm_blocking_ast;
379                 einfo->ei_cb_cp = ldlm_completion_ast;
380                 einfo->ei_res_id = resid;
381
382                 rc = dt_object_lock(env, obj, lh, einfo, policy);
383                 /* for regular checks LFSCK doesn't use LDLM locking,
384                  * so the state isn't coherent. here we just took LDLM
385                  * lock for coherency and it's time to invalidate
386                  * previous state */
387                 if (rc == ELDLM_OK)
388                         dt_invalidate(env, obj);
389         } else {
390                 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
391                                             LDLM_IBITS, policy, mode,
392                                             &flags, ldlm_blocking_ast,
393                                             ldlm_completion_ast, NULL, NULL,
394                                             0, LVB_T_NONE, NULL, lh);
395         }
396
397         if (rc == ELDLM_OK) {
398                 rc = 0;
399         } else {
400                 memset(lh, 0, sizeof(*lh));
401                 rc = -EIO;
402         }
403
404         return rc;
405 }
406
407 /**
408  * Request the specified ibits lock for the given object.
409  *
410  * Before the LFSCK modifying on the namespace visible object,
411  * it needs to acquire related ibits ldlm lock.
412  *
413  * \param[in] env       pointer to the thread context
414  * \param[in] lfsck     pointer to the lfsck instance
415  * \param[in] obj       pointer to the dt_object to be locked
416  * \param[out] lh       pointer to the lock handle
417  * \param[in] bits      the bits for the ldlm lock to be acquired
418  * \param[in] mode      the mode for the ldlm lock to be acquired
419  *
420  * \retval              0 for success
421  * \retval              negative error number on failure
422  */
423 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
424                      struct dt_object *obj, struct lustre_handle *lh,
425                      __u64 bits, enum ldlm_mode mode)
426 {
427         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
428
429         LASSERT(!lustre_handle_is_used(lh));
430
431         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
432         return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
433 }
434
435 /**
436  * Request the remote LOOKUP lock for the given object.
437  *
438  * If \a pobj is remote, the LOOKUP lock of \a obj is on the MDT where
439  * \a pobj is, acquire LOOKUP lock there.
440  *
441  * \param[in] env       pointer to the thread context
442  * \param[in] lfsck     pointer to the lfsck instance
443  * \param[in] pobj      pointer to parent dt_object
444  * \param[in] obj       pointer to the dt_object to be locked
445  * \param[out] lh       pointer to the lock handle
446  * \param[in] mode      the mode for the ldlm lock to be acquired
447  *
448  * \retval              0 for success
449  * \retval              negative error number on failure
450  */
451 int lfsck_remote_lookup_lock(const struct lu_env *env,
452                              struct lfsck_instance *lfsck,
453                              struct dt_object *pobj, struct dt_object *obj,
454                              struct lustre_handle *lh, enum ldlm_mode mode)
455 {
456         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
457
458         LASSERT(!lustre_handle_is_used(lh));
459
460         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
461         return __lfsck_ibits_lock(env, lfsck, pobj, resid, lh,
462                                   MDS_INODELOCK_LOOKUP, mode);
463 }
464
465 /**
466  * Release the the specified ibits lock.
467  *
468  * If the lock has been acquired before, release it
469  * and cleanup the handle. Otherwise, do nothing.
470  *
471  * \param[in] lh        pointer to the lock handle
472  * \param[in] mode      the mode for the ldlm lock to be released
473  */
474 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
475 {
476         if (lustre_handle_is_used(lh)) {
477                 ldlm_lock_decref(lh, mode);
478                 memset(lh, 0, sizeof(*lh));
479         }
480 }
481
482 /**
483  * Request compound ibits locks for the given <obj, name> pairs.
484  *
485  * Before the LFSCK modifying on the namespace visible object, it needs to
486  * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
487  * the lock purpose. But the simple lfsck_ibits_lock for directory-based
488  * modificationis (such as insert name entry to the directory) may be too
489  * coarse-grained and not efficient.
490  *
491  * The lfsck_lock() will request compound ibits locks on the specified
492  * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
493  * lock on the directory object, and the regular ibits lock on the name hash.
494  *
495  * \param[in] env       pointer to the thread context
496  * \param[in] lfsck     pointer to the lfsck instance
497  * \param[in] obj       pointer to the dt_object to be locked
498  * \param[in] name      used for building the PDO lock resource
499  * \param[out] llh      pointer to the lfsck_lock_handle
500  * \param[in] bits      the bits for the ldlm lock to be acquired
501  * \param[in] mode      the mode for the ldlm lock to be acquired
502  *
503  * \retval              0 for success
504  * \retval              negative error number on failure
505  */
506 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
507                struct dt_object *obj, const char *name,
508                struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
509 {
510         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
511         int                 rc;
512
513         LASSERT(S_ISDIR(lfsck_object_type(obj)));
514         LASSERT(name != NULL);
515         LASSERT(name[0] != 0);
516         LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
517         LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
518
519         switch (mode) {
520         case LCK_EX:
521                 llh->llh_pdo_mode = LCK_EX;
522                 break;
523         case LCK_PW:
524                 llh->llh_pdo_mode = LCK_CW;
525                 break;
526         case LCK_PR:
527                 llh->llh_pdo_mode = LCK_CR;
528                 break;
529         default:
530                 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
531                        DFID"\n", lfsck_lfsck2name(lfsck), mode,
532                        PFID(lfsck_dto2fid(obj)));
533                 LBUG();
534         }
535
536         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
537         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
538                                 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
539         if (rc != 0)
540                 return rc;
541
542         llh->llh_reg_mode = mode;
543         resid->name[LUSTRE_RES_ID_HSH_OFF] = ll_full_name_hash(NULL, name,
544                                                                strlen(name));
545         LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
546         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
547                                 bits, llh->llh_reg_mode);
548         if (rc != 0)
549                 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
550
551         return rc;
552 }
553
554 /**
555  * Release the the compound ibits locks.
556  *
557  * \param[in] llh       pointer to the lfsck_lock_handle to be released
558  */
559 void lfsck_unlock(struct lfsck_lock_handle *llh)
560 {
561         lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
562         lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
563 }
564
565 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
566                               struct lfsck_instance *lfsck,
567                               const struct lu_fid *fid)
568 {
569         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
570         struct lu_seq_range     *range  = &lfsck_env_info(env)->lti_range;
571         int                      rc;
572
573         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
574                 /* "ROOT" is always on the MDT0. */
575                 if (lu_fid_eq(fid, &lfsck->li_global_root_fid))
576                         return 0;
577
578                 return lfsck_dev_idx(lfsck);
579         }
580
581         fld_range_set_mdt(range);
582         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
583         if (rc == 0)
584                 rc = range->lsr_index;
585
586         return rc;
587 }
588
589 const char dot[] = ".";
590 const char dotdot[] = "..";
591 static const char dotlustre[] = ".lustre";
592 static const char lostfound[] = "lost+found";
593
594 /**
595  * Remove the name entry from the .lustre/lost+found directory.
596  *
597  * No need to care about the object referenced by the name entry,
598  * either the name entry is invalid or redundant, or the referenced
599  * object has been processed or will be handled by others.
600  *
601  * \param[in] env       pointer to the thread context
602  * \param[in] lfsck     pointer to the lfsck instance
603  * \param[in] name      the name for the name entry to be removed
604  *
605  * \retval              0 for success
606  * \retval              negative error number on failure
607  */
608 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
609                                        struct lfsck_instance *lfsck,
610                                        const char *name)
611 {
612         struct dt_object        *parent = lfsck->li_lpf_root_obj;
613         struct dt_device        *dev    = lfsck_obj2dev(parent);
614         struct thandle          *th;
615         struct lfsck_lock_handle *llh   = &lfsck_env_info(env)->lti_llh;
616         int                      rc;
617         ENTRY;
618
619         rc = lfsck_lock(env, lfsck, parent, name, llh,
620                         MDS_INODELOCK_UPDATE, LCK_PW);
621         if (rc != 0)
622                 RETURN(rc);
623
624         th = lfsck_trans_create(env, dev, lfsck);
625         if (IS_ERR(th))
626                 GOTO(unlock, rc = PTR_ERR(th));
627
628         rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
629         if (rc != 0)
630                 GOTO(stop, rc);
631
632         rc = dt_declare_ref_del(env, parent, th);
633         if (rc != 0)
634                 GOTO(stop, rc);
635
636         rc = dt_trans_start_local(env, dev, th);
637         if (rc != 0)
638                 GOTO(stop, rc);
639
640         rc = dt_delete(env, parent, (const struct dt_key *)name, th);
641         if (rc != 0)
642                 GOTO(stop, rc);
643
644         dt_write_lock(env, parent, 0);
645         rc = dt_ref_del(env, parent, th);
646         dt_write_unlock(env, parent);
647
648         GOTO(stop, rc);
649
650 stop:
651         dt_trans_stop(env, dev, th);
652
653 unlock:
654         lfsck_unlock(llh);
655
656         CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
657                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
658
659         return rc;
660 }
661
662 static int lfsck_create_lpf_local(const struct lu_env *env,
663                                   struct lfsck_instance *lfsck,
664                                   struct dt_object *child,
665                                   struct lu_attr *la,
666                                   struct dt_object_format *dof,
667                                   const char *name)
668 {
669         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
670         struct dt_object        *parent = lfsck->li_lpf_root_obj;
671         struct dt_device        *dev    = lfsck_obj2dev(child);
672         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
673         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
674         const struct lu_fid     *cfid   = lfsck_dto2fid(child);
675         struct thandle          *th     = NULL;
676         struct linkea_data       ldata  = { NULL };
677         struct lu_buf            linkea_buf;
678         const struct lu_name    *cname;
679         loff_t                   pos    = 0;
680         int                      len    = sizeof(struct lfsck_bookmark);
681         int                      rc;
682         ENTRY;
683
684         cname = lfsck_name_get_const(env, name, strlen(name));
685         rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
686                               cname, lfsck_dto2fid(parent));
687         if (rc != 0)
688                 RETURN(rc);
689
690         th = lfsck_trans_create(env, dev, lfsck);
691         if (IS_ERR(th))
692                 RETURN(PTR_ERR(th));
693
694         /* 1a. create child */
695         rc = dt_declare_create(env, child, la, NULL, dof, th);
696         if (rc != 0)
697                 GOTO(stop, rc);
698
699         if (!dt_try_as_dir(env, child))
700                 GOTO(stop, rc = -ENOTDIR);
701
702         /* 2a. increase child nlink */
703         rc = dt_declare_ref_add(env, child, th);
704         if (rc != 0)
705                 GOTO(stop, rc);
706
707         /* 3a. insert dot into child dir */
708         rec->rec_type = S_IFDIR;
709         rec->rec_fid = cfid;
710         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
711                                (const struct dt_key *)dot, th);
712         if (rc != 0)
713                 GOTO(stop, rc);
714
715         /* 4a. insert dotdot into child dir */
716         rec->rec_fid = &LU_LPF_FID;
717         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
718                                (const struct dt_key *)dotdot, th);
719         if (rc != 0)
720                 GOTO(stop, rc);
721
722         /* 5a. insert linkEA for child */
723         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
724                        ldata.ld_leh->leh_len);
725         rc = dt_declare_xattr_set(env, child, &linkea_buf,
726                                   XATTR_NAME_LINK, 0, th);
727         if (rc != 0)
728                 GOTO(stop, rc);
729
730         /* 6a. insert name into parent dir */
731         rec->rec_type = S_IFDIR;
732         rec->rec_fid = cfid;
733         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
734                                (const struct dt_key *)name, th);
735         if (rc != 0)
736                 GOTO(stop, rc);
737
738         /* 7a. increase parent nlink */
739         rc = dt_declare_ref_add(env, parent, th);
740         if (rc != 0)
741                 GOTO(stop, rc);
742
743         /* 8a. update bookmark */
744         rc = dt_declare_record_write(env, bk_obj,
745                                      lfsck_buf_get(env, bk, len), 0, th);
746         if (rc != 0)
747                 GOTO(stop, rc);
748
749         rc = dt_trans_start_local(env, dev, th);
750         if (rc != 0)
751                 GOTO(stop, rc);
752
753         dt_write_lock(env, child, 0);
754         /* 1b. create child */
755         rc = dt_create(env, child, la, NULL, dof, th);
756         if (rc != 0)
757                 GOTO(unlock, rc);
758
759         /* 2b. increase child nlink */
760         rc = dt_ref_add(env, child, th);
761         if (rc != 0)
762                 GOTO(unlock, rc);
763
764         /* 3b. insert dot into child dir */
765         rec->rec_fid = cfid;
766         rc = dt_insert(env, child, (const struct dt_rec *)rec,
767                        (const struct dt_key *)dot, th);
768         if (rc != 0)
769                 GOTO(unlock, rc);
770
771         /* 4b. insert dotdot into child dir */
772         rec->rec_fid = &LU_LPF_FID;
773         rc = dt_insert(env, child, (const struct dt_rec *)rec,
774                        (const struct dt_key *)dotdot, th);
775         if (rc != 0)
776                 GOTO(unlock, rc);
777
778         /* 5b. insert linkEA for child. */
779         rc = dt_xattr_set(env, child, &linkea_buf,
780                           XATTR_NAME_LINK, 0, th);
781         dt_write_unlock(env, child);
782         if (rc != 0)
783                 GOTO(stop, rc);
784
785         /* 6b. insert name into parent dir */
786         rec->rec_fid = cfid;
787         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
788                        (const struct dt_key *)name, th);
789         if (rc != 0)
790                 GOTO(stop, rc);
791
792         dt_write_lock(env, parent, 0);
793         /* 7b. increase parent nlink */
794         rc = dt_ref_add(env, parent, th);
795         dt_write_unlock(env, parent);
796         if (rc != 0)
797                 GOTO(stop, rc);
798
799         bk->lb_lpf_fid = *cfid;
800         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
801
802         /* 8b. update bookmark */
803         rc = dt_record_write(env, bk_obj,
804                              lfsck_buf_get(env, bk, len), &pos, th);
805
806         GOTO(stop, rc);
807
808 unlock:
809         dt_write_unlock(env, child);
810
811 stop:
812         dt_trans_stop(env, dev, th);
813
814         return rc;
815 }
816
817 static int lfsck_create_lpf_remote(const struct lu_env *env,
818                                    struct lfsck_instance *lfsck,
819                                    struct dt_object *child,
820                                    struct lu_attr *la,
821                                    struct dt_object_format *dof,
822                                    const char *name)
823 {
824         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
825         struct dt_object        *parent = lfsck->li_lpf_root_obj;
826         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
827         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
828         const struct lu_fid     *cfid   = lfsck_dto2fid(child);
829         struct thandle          *th     = NULL;
830         struct linkea_data       ldata  = { NULL };
831         struct lu_buf            linkea_buf;
832         const struct lu_name    *cname;
833         struct dt_device        *dev;
834         loff_t                   pos    = 0;
835         int                      len    = sizeof(struct lfsck_bookmark);
836         int                      rc;
837         ENTRY;
838
839         cname = lfsck_name_get_const(env, name, strlen(name));
840         rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
841                               cname, lfsck_dto2fid(parent));
842         if (rc != 0)
843                 RETURN(rc);
844
845         /* Create .lustre/lost+found/MDTxxxx. */
846
847         /* XXX: Currently, cross-MDT create operation needs to create the child
848          *      object firstly, then insert name into the parent directory. For
849          *      this case, the child object resides on current MDT (local), but
850          *      the parent ".lustre/lost+found" may be on remote MDT. It is not
851          *      easy to contain all the sub-modifications orderly within single
852          *      transaction.
853          *
854          *      To avoid more inconsistency, we split the create operation into
855          *      two transactions:
856          *
857          *      1) create the child and update the lfsck_bookmark::lb_lpf_fid
858          *         locally.
859          *      2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
860          *         remotely.
861          *
862          *      If 1) done, but 2) failed, then go ahead, the LFSCK will try to
863          *      repair such inconsistency when LFSCK run next time. */
864
865         /* Transaction I: locally */
866
867         dev = lfsck_obj2dev(child);
868         th = lfsck_trans_create(env, dev, lfsck);
869         if (IS_ERR(th))
870                 RETURN(PTR_ERR(th));
871
872         /* 1a. create child */
873         rc = dt_declare_create(env, child, la, NULL, dof, th);
874         if (rc != 0)
875                 GOTO(stop, rc);
876
877         if (!dt_try_as_dir(env, child))
878                 GOTO(stop, rc = -ENOTDIR);
879
880         /* 2a. increase child nlink */
881         rc = dt_declare_ref_add(env, child, th);
882         if (rc != 0)
883                 GOTO(stop, rc);
884
885         /* 3a. insert dot into child dir */
886         rec->rec_type = S_IFDIR;
887         rec->rec_fid = cfid;
888         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
889                                (const struct dt_key *)dot, th);
890         if (rc != 0)
891                 GOTO(stop, rc);
892
893         /* 4a. insert dotdot into child dir */
894         rec->rec_fid = &LU_LPF_FID;
895         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
896                                (const struct dt_key *)dotdot, th);
897         if (rc != 0)
898                 GOTO(stop, rc);
899
900         /* 5a. insert linkEA for child */
901         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
902                        ldata.ld_leh->leh_len);
903         rc = dt_declare_xattr_set(env, child, &linkea_buf,
904                                   XATTR_NAME_LINK, 0, th);
905         if (rc != 0)
906                 GOTO(stop, rc);
907
908         /* 6a. update bookmark */
909         rc = dt_declare_record_write(env, bk_obj,
910                                      lfsck_buf_get(env, bk, len), 0, th);
911         if (rc != 0)
912                 GOTO(stop, rc);
913
914         rc = dt_trans_start_local(env, dev, th);
915         if (rc != 0)
916                 GOTO(stop, rc);
917
918         dt_write_lock(env, child, 0);
919         /* 1b. create child */
920         rc = dt_create(env, child, la, NULL, dof, th);
921         if (rc != 0)
922                 GOTO(unlock, rc);
923
924         /* 2b. increase child nlink */
925         rc = dt_ref_add(env, child, th);
926         if (rc != 0)
927                 GOTO(unlock, rc);
928
929         /* 3b. insert dot into child dir */
930         rec->rec_type = S_IFDIR;
931         rec->rec_fid = cfid;
932         rc = dt_insert(env, child, (const struct dt_rec *)rec,
933                        (const struct dt_key *)dot, th);
934         if (rc != 0)
935                 GOTO(unlock, rc);
936
937         /* 4b. insert dotdot into child dir */
938         rec->rec_fid = &LU_LPF_FID;
939         rc = dt_insert(env, child, (const struct dt_rec *)rec,
940                        (const struct dt_key *)dotdot, th);
941         if (rc != 0)
942                 GOTO(unlock, rc);
943
944         /* 5b. insert linkEA for child */
945         rc = dt_xattr_set(env, child, &linkea_buf,
946                           XATTR_NAME_LINK, 0, th);
947         if (rc != 0)
948                 GOTO(unlock, rc);
949
950         bk->lb_lpf_fid = *cfid;
951         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
952
953         /* 6b. update bookmark */
954         rc = dt_record_write(env, bk_obj,
955                              lfsck_buf_get(env, bk, len), &pos, th);
956
957         dt_write_unlock(env, child);
958         dt_trans_stop(env, dev, th);
959         if (rc != 0)
960                 RETURN(rc);
961
962         /* Transaction II: remotely */
963
964         dev = lfsck_obj2dev(parent);
965         th = lfsck_trans_create(env, dev, lfsck);
966         if (IS_ERR(th))
967                 RETURN(PTR_ERR(th));
968
969         th->th_sync = 1;
970         /* 5a. insert name into parent dir */
971         rec->rec_fid = cfid;
972         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
973                                (const struct dt_key *)name, th);
974         if (rc != 0)
975                 GOTO(stop, rc);
976
977         /* 6a. increase parent nlink */
978         rc = dt_declare_ref_add(env, parent, th);
979         if (rc != 0)
980                 GOTO(stop, rc);
981
982         rc = dt_trans_start_local(env, dev, th);
983         if (rc != 0)
984                 GOTO(stop, rc);
985
986         /* 5b. insert name into parent dir */
987         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
988                        (const struct dt_key *)name, th);
989         if (rc != 0)
990                 GOTO(stop, rc);
991
992         dt_write_lock(env, parent, 0);
993         /* 6b. increase parent nlink */
994         rc = dt_ref_add(env, parent, th);
995         dt_write_unlock(env, parent);
996
997         GOTO(stop, rc);
998
999 unlock:
1000         dt_write_unlock(env, child);
1001 stop:
1002         dt_trans_stop(env, dev, th);
1003
1004         if (rc != 0 && dev == lfsck_obj2dev(parent))
1005                 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
1006                        "for orphans, but failed to insert the name %s "
1007                        "to the .lustre/lost+found/. Such inconsistency "
1008                        "will be repaired when LFSCK run next time: rc = %d\n",
1009                        lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
1010
1011         return rc;
1012 }
1013
1014 /**
1015  * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
1016  *
1017  * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
1018  * orphans and other uncertain inconsistent objects found during the
1019  * LFSCK. Such directory will be created by the LFSCK engine on the
1020  * local MDT before the LFSCK scanning.
1021  *
1022  * \param[in] env       pointer to the thread context
1023  * \param[in] lfsck     pointer to the lfsck instance
1024  *
1025  * \retval              0 for success
1026  * \retval              negative error number on failure
1027  */
1028 static int lfsck_create_lpf(const struct lu_env *env,
1029                             struct lfsck_instance *lfsck)
1030 {
1031         struct lfsck_bookmark    *bk    = &lfsck->li_bookmark_ram;
1032         struct lfsck_thread_info *info  = lfsck_env_info(env);
1033         struct lu_fid            *cfid  = &info->lti_fid2;
1034         struct lu_attr           *la    = &info->lti_la;
1035         struct dt_object_format  *dof   = &info->lti_dof;
1036         struct dt_object         *parent = lfsck->li_lpf_root_obj;
1037         struct dt_object         *child = NULL;
1038         struct lfsck_lock_handle *llh   = &info->lti_llh;
1039         char                      name[8];
1040         int                       node  = lfsck_dev_idx(lfsck);
1041         int                       rc    = 0;
1042         ENTRY;
1043
1044         LASSERT(lfsck->li_master);
1045         LASSERT(parent != NULL);
1046         LASSERT(lfsck->li_lpf_obj == NULL);
1047
1048         snprintf(name, 8, "MDT%04x", node);
1049         rc = lfsck_lock(env, lfsck, parent, name, llh,
1050                         MDS_INODELOCK_UPDATE, LCK_PW);
1051         if (rc != 0)
1052                 RETURN(rc);
1053
1054         if (fid_is_zero(&bk->lb_lpf_fid)) {
1055                 /* There is corner case that: in former LFSCK scanning we have
1056                  * created the .lustre/lost+found/MDTxxxx but failed to update
1057                  * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1058                  * it from MDT0 firstly. */
1059                 rc = dt_lookup_dir(env, parent, name, cfid);
1060                 if (rc != 0 && rc != -ENOENT)
1061                         GOTO(unlock, rc);
1062
1063                 if (rc == 0) {
1064                         bk->lb_lpf_fid = *cfid;
1065                         rc = lfsck_bookmark_store(env, lfsck);
1066                 } else {
1067                         rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1068                 }
1069                 if (rc != 0)
1070                         GOTO(unlock, rc);
1071         } else {
1072                 *cfid = bk->lb_lpf_fid;
1073         }
1074
1075         child = lfsck_object_find_bottom_new(env, lfsck, cfid);
1076         if (IS_ERR(child))
1077                 GOTO(unlock, rc = PTR_ERR(child));
1078
1079         if (dt_object_exists(child) != 0) {
1080                 if (unlikely(!dt_try_as_dir(env, child)))
1081                         rc = -ENOTDIR;
1082                 else
1083                         lfsck->li_lpf_obj = child;
1084
1085                 GOTO(unlock, rc);
1086         }
1087
1088         memset(la, 0, sizeof(*la));
1089         la->la_atime = la->la_mtime = la->la_ctime = ktime_get_real_seconds();
1090         la->la_mode = S_IFDIR | S_IRWXU;
1091         la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1092                        LA_UID | LA_GID | LA_TYPE;
1093         memset(dof, 0, sizeof(*dof));
1094         dof->dof_type = dt_mode_to_dft(S_IFDIR);
1095
1096         if (node == 0)
1097                 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1098         else
1099                 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1100         if (rc == 0)
1101                 lfsck->li_lpf_obj = child;
1102
1103         GOTO(unlock, rc);
1104
1105 unlock:
1106         lfsck_unlock(llh);
1107         if (rc != 0 && child != NULL && !IS_ERR(child))
1108                 lfsck_object_put(env, child);
1109
1110         return rc;
1111 }
1112
1113 /**
1114  * Scan .lustre/lost+found for bad name entries and remove them.
1115  *
1116  * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1117  * index in the system. Any other formatted name is invalid and should be
1118  * removed.
1119  *
1120  * \param[in] env       pointer to the thread context
1121  * \param[in] lfsck     pointer to the lfsck instance
1122  *
1123  * \retval              0 for success
1124  * \retval              negative error number on failure
1125  */
1126 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1127                                       struct lfsck_instance *lfsck)
1128 {
1129         struct dt_object        *parent = lfsck->li_lpf_root_obj;
1130         struct lu_dirent        *ent    =
1131                         (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1132         const struct dt_it_ops  *iops   = &parent->do_index_ops->dio_it;
1133         struct dt_it            *it;
1134         int                      rc;
1135         ENTRY;
1136
1137         it = iops->init(env, parent, LUDA_64BITHASH);
1138         if (IS_ERR(it))
1139                 RETURN(PTR_ERR(it));
1140
1141         rc = iops->load(env, it, 0);
1142         if (rc == 0)
1143                 rc = iops->next(env, it);
1144         else if (rc > 0)
1145                 rc = 0;
1146
1147         while (rc == 0) {
1148                 int off = 3;
1149
1150                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1151                 if (rc != 0)
1152                         break;
1153
1154                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1155                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1156                         goto next;
1157
1158                 /* name length must be strlen("MDTxxxx") */
1159                 if (ent->lde_namelen != 7)
1160                         goto remove;
1161
1162                 if (memcmp(ent->lde_name, "MDT", off) != 0)
1163                         goto remove;
1164
1165                 while (off < 7 && isxdigit(ent->lde_name[off]))
1166                         off++;
1167
1168                 if (off != 7) {
1169
1170 remove:
1171                         rc = lfsck_lpf_remove_name_entry(env, lfsck,
1172                                                          ent->lde_name);
1173                         if (rc != 0)
1174                                 break;
1175                 }
1176
1177 next:
1178                 rc = iops->next(env, it);
1179         }
1180
1181         iops->put(env, it);
1182         iops->fini(env, it);
1183
1184         RETURN(rc > 0 ? 0 : rc);
1185 }
1186
1187 static int lfsck_update_lpf_entry(const struct lu_env *env,
1188                                   struct lfsck_instance *lfsck,
1189                                   struct dt_object *parent,
1190                                   struct dt_object *child,
1191                                   const char *name,
1192                                   enum lfsck_verify_lpf_types type)
1193 {
1194         int rc;
1195
1196         if (type == LVLT_BY_BOOKMARK) {
1197                 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1198                                              lfsck_dto2fid(child), S_IFDIR);
1199         } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1200                 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1201                 rc = lfsck_bookmark_store(env, lfsck);
1202
1203                 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1204                        " in the bookmark file: rc = %d\n",
1205                        lfsck_lfsck2name(lfsck),
1206                        PFID(lfsck_dto2fid(child)), rc);
1207         }
1208
1209         return rc;
1210 }
1211
1212 /**
1213  * Check whether the @child back references the @parent.
1214  *
1215  * Two cases:
1216  * 1) The child's FID is stored in the bookmark file. If the child back
1217  *    references the parent (LU_LPF_FID object) via its ".." entry, then
1218  *    insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1219  *    the child back references another parent2, then:
1220  * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1221  * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1222  *      references the child. So keep them there. As the LFSCK processing,
1223  *      the parent3 may be found, then when the LFSCK run next time, the
1224  *      inconsistency can be repaired.
1225  *
1226  * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1227  *    entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1228  *    via its ".." entry, then update the bookmark file, otherwise, if the child
1229  *    back references another parent2, then:
1230  * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1231  *      from .lustre/lost+found/;
1232  * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1233  *      sub-directory name entry and update the child;
1234  * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1235  *      or not, then keep them there.
1236  *
1237  * \param[in] env       pointer to the thread context
1238  * \param[in] lfsck     pointer to the lfsck instance
1239  * \param[in] child     pointer to the lost+found sub-directory object
1240  * \param[in] name      the name for lost+found sub-directory object
1241  * \param[out] fid      pointer to the buffer to hold the FID of the object
1242  *                      (called it as parent2) that is referenced via the
1243  *                      child's dotdot entry; it also can be the FID that
1244  *                      is referenced by the name entry under the parent2.
1245  * \param[in] type      to indicate where the child's FID is stored in
1246  *
1247  * \retval              positive number for uncertain inconsistency
1248  * \retval              0 for success
1249  * \retval              negative error number on failure
1250  */
1251 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1252                                   struct lfsck_instance *lfsck,
1253                                   struct dt_object *child, const char *name,
1254                                   struct lu_fid *fid,
1255                                   enum lfsck_verify_lpf_types type)
1256 {
1257         struct dt_object         *parent  = lfsck->li_lpf_root_obj;
1258         struct lfsck_thread_info *info    = lfsck_env_info(env);
1259         char                     *name2   = info->lti_key;
1260         struct lu_fid            *fid2    = &info->lti_fid3;
1261         struct dt_object         *parent2 = NULL;
1262         struct lustre_handle      lh      = { 0 };
1263         int                       rc;
1264         ENTRY;
1265
1266         fid_zero(fid);
1267         rc = dt_lookup_dir(env, child, dotdot, fid);
1268         if (rc != 0)
1269                 GOTO(linkea, rc);
1270
1271         if (!fid_is_sane(fid))
1272                 GOTO(linkea, rc = -EINVAL);
1273
1274         if (lu_fid_eq(fid, &LU_LPF_FID)) {
1275                 const struct lu_name *cname;
1276
1277                 if (lfsck->li_lpf_obj == NULL) {
1278                         lu_object_get(&child->do_lu);
1279                         lfsck->li_lpf_obj = child;
1280                 }
1281
1282                 cname = lfsck_name_get_const(env, name, strlen(name));
1283                 rc = lfsck_verify_linkea(env, lfsck, child, cname, &LU_LPF_FID);
1284                 if (rc == 0)
1285                         rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1286                                                     name, type);
1287
1288                 GOTO(out_done, rc);
1289         }
1290
1291         parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1292         if (IS_ERR(parent2))
1293                 GOTO(linkea, parent2);
1294
1295         if (!dt_object_exists(parent2)) {
1296                 lfsck_object_put(env, parent2);
1297
1298                 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1299         }
1300
1301         if (!dt_try_as_dir(env, parent2)) {
1302                 lfsck_object_put(env, parent2);
1303
1304                 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1305         }
1306
1307 linkea:
1308         /* To prevent rename/unlink race */
1309         rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1310                               MDS_INODELOCK_UPDATE, LCK_PR);
1311         if (rc != 0)
1312                 GOTO(out_put, rc);
1313
1314         dt_read_lock(env, child, 0);
1315         rc = lfsck_links_get_first(env, child, name2, fid2);
1316         if (rc != 0) {
1317                 dt_read_unlock(env, child);
1318                 lfsck_ibits_unlock(&lh, LCK_PR);
1319
1320                 GOTO(out_put, rc = 1);
1321         }
1322
1323         /* It is almost impossible that the bookmark file (or the name entry)
1324          * and the linkEA hit the same data corruption. Trust the linkEA. */
1325         if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1326                 dt_read_unlock(env, child);
1327                 lfsck_ibits_unlock(&lh, LCK_PR);
1328
1329                 *fid = *fid2;
1330                 if (lfsck->li_lpf_obj == NULL) {
1331                         lu_object_get(&child->do_lu);
1332                         lfsck->li_lpf_obj = child;
1333                 }
1334
1335                 /* Update the child's dotdot entry */
1336                 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1337                                              &LU_LPF_FID, S_IFDIR);
1338                 if (rc == 0)
1339                         rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1340                                                     name, type);
1341
1342                 GOTO(out_put, rc);
1343         }
1344
1345         if (parent2 == NULL || IS_ERR(parent2)) {
1346                 dt_read_unlock(env, child);
1347                 lfsck_ibits_unlock(&lh, LCK_PR);
1348
1349                 GOTO(out_done, rc = 1);
1350         }
1351
1352         rc = dt_lookup_dir(env, parent2, name2, fid);
1353         dt_read_unlock(env, child);
1354         lfsck_ibits_unlock(&lh, LCK_PR);
1355         if (rc != 0 && rc != -ENOENT)
1356                 GOTO(out_put, rc);
1357
1358         if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1359                 if (type == LVLT_BY_BOOKMARK)
1360                         GOTO(out_put, rc = 1);
1361
1362                 /* Trust the name entry, update the child's dotdot entry. */
1363                 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1364                                              &LU_LPF_FID, S_IFDIR);
1365
1366                 GOTO(out_put, rc);
1367         }
1368
1369         if (type == LVLT_BY_BOOKMARK) {
1370                 /* Invalid FID record in the bookmark file, reset it. */
1371                 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1372                 rc = lfsck_bookmark_store(env, lfsck);
1373
1374                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1375                        " in the bookmark file: rc = %d\n",
1376                        lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1377         } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1378                 /* The name entry is wrong, remove it. */
1379                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1380         }
1381
1382         GOTO(out_put, rc);
1383
1384 out_put:
1385         if (parent2 != NULL && !IS_ERR(parent2))
1386                 lfsck_object_put(env, parent2);
1387
1388 out_done:
1389         return rc;
1390 }
1391
1392 /**
1393  * Verify the /ROOT/.lustre/lost+found/ directory.
1394  *
1395  * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1396  * the LFSCK does not exactly know how to handle, such as orphans. So before
1397  * the LFSCK scanning the system, the consistency of such directory needs to
1398  * be verified firstly to allow the users to use it during the LFSCK.
1399  *
1400  * \param[in] env       pointer to the thread context
1401  * \param[in] lfsck     pointer to the lfsck instance
1402  *
1403  * \retval              positive number for uncertain inconsistency
1404  * \retval              0 for success
1405  * \retval              negative error number on failure
1406  */
1407 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1408 {
1409         struct lfsck_thread_info *info   = lfsck_env_info(env);
1410         struct lu_fid            *pfid   = &info->lti_fid;
1411         struct lu_fid            *cfid   = &info->lti_fid2;
1412         struct lfsck_bookmark    *bk     = &lfsck->li_bookmark_ram;
1413         struct dt_object         *parent;
1414         /* child1's FID is in the bookmark file. */
1415         struct dt_object         *child1 = NULL;
1416         /* child2's FID is in the name entry MDTxxxx. */
1417         struct dt_object         *child2 = NULL;
1418         const struct lu_name     *cname;
1419         char                      name[8];
1420         int                       node   = lfsck_dev_idx(lfsck);
1421         int                       rc     = 0;
1422         ENTRY;
1423
1424         LASSERT(lfsck->li_master);
1425
1426         if (lfsck_is_dryrun(lfsck))
1427                 RETURN(0);
1428
1429         if (lfsck->li_lpf_root_obj != NULL)
1430                 RETURN(0);
1431
1432         if (node == 0) {
1433                 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1434                                                   &LU_LPF_FID);
1435         } else {
1436                 struct lfsck_tgt_desc *ltd;
1437
1438                 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1439                 if (unlikely(ltd == NULL))
1440                         RETURN(-ENXIO);
1441
1442                 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1443                                                   &LU_LPF_FID);
1444                 lfsck_tgt_put(ltd);
1445         }
1446
1447         if (IS_ERR(parent))
1448                 RETURN(PTR_ERR(parent));
1449
1450         LASSERT(dt_object_exists(parent));
1451
1452         if (unlikely(!dt_try_as_dir(env, parent))) {
1453                 lfsck_object_put(env, parent);
1454
1455                 GOTO(put, rc = -ENOTDIR);
1456         }
1457
1458         lfsck->li_lpf_root_obj = parent;
1459         if (node == 0) {
1460                 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1461                 if (rc != 0)
1462                         CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1463                                "for bad sub-directories: rc = %d\n",
1464                                lfsck_lfsck2name(lfsck), rc);
1465         }
1466
1467         /* child2 */
1468         snprintf(name, 8, "MDT%04x", node);
1469         rc = dt_lookup_dir(env, parent, name, cfid);
1470         if (rc == -ENOENT) {
1471                 rc = 0;
1472                 goto find_child1;
1473         }
1474
1475         if (rc != 0)
1476                 GOTO(put, rc);
1477
1478         /* Invalid FID in the name entry, remove the name entry. */
1479         if (!fid_is_norm(cfid)) {
1480                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1481                 if (rc != 0)
1482                         GOTO(put, rc);
1483
1484                 goto find_child1;
1485         }
1486
1487         child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1488         if (IS_ERR(child2))
1489                 GOTO(put, rc = PTR_ERR(child2));
1490
1491         if (unlikely(!dt_object_exists(child2) ||
1492                      dt_object_remote(child2)) ||
1493                      !S_ISDIR(lfsck_object_type(child2))) {
1494                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1495                 if (rc != 0)
1496                         GOTO(put, rc);
1497
1498                 goto find_child1;
1499         }
1500
1501         if (unlikely(!dt_try_as_dir(env, child2))) {
1502                 lfsck_object_put(env, child2);
1503                 child2 = NULL;
1504                 rc = -ENOTDIR;
1505         }
1506
1507 find_child1:
1508         if (fid_is_zero(&bk->lb_lpf_fid))
1509                 goto check_child2;
1510
1511         if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1512                 if (lfsck->li_lpf_obj == NULL) {
1513                         lu_object_get(&child2->do_lu);
1514                         lfsck->li_lpf_obj = child2;
1515                 }
1516
1517                 cname = lfsck_name_get_const(env, name, strlen(name));
1518                 rc = lfsck_verify_linkea(env, lfsck, child2, cname,
1519                                          &LU_LPF_FID);
1520
1521                 GOTO(put, rc);
1522         }
1523
1524         if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1525                 struct lu_fid tfid = bk->lb_lpf_fid;
1526
1527                 /* Invalid FID record in the bookmark file, reset it. */
1528                 fid_zero(&bk->lb_lpf_fid);
1529                 rc = lfsck_bookmark_store(env, lfsck);
1530
1531                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1532                        " in the bookmark file: rc = %d\n",
1533                        lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1534
1535                 if (rc != 0)
1536                         GOTO(put, rc);
1537
1538                 goto check_child2;
1539         }
1540
1541         child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1542         if (IS_ERR(child1)) {
1543                 child1 = NULL;
1544                 goto check_child2;
1545         }
1546
1547         if (unlikely(!dt_object_exists(child1) ||
1548                      dt_object_remote(child1)) ||
1549                      !S_ISDIR(lfsck_object_type(child1))) {
1550                 /* Invalid FID record in the bookmark file, reset it. */
1551                 fid_zero(&bk->lb_lpf_fid);
1552                 rc = lfsck_bookmark_store(env, lfsck);
1553
1554                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1555                        " in the bookmark file: rc = %d\n",
1556                        lfsck_lfsck2name(lfsck),
1557                        PFID(lfsck_dto2fid(child1)), rc);
1558
1559                 if (rc != 0)
1560                         GOTO(put, rc);
1561
1562                 lfsck_object_put(env, child1);
1563                 child1 = NULL;
1564                 goto check_child2;
1565         }
1566
1567         if (unlikely(!dt_try_as_dir(env, child1))) {
1568                 lfsck_object_put(env, child1);
1569                 child1 = NULL;
1570                 rc = -ENOTDIR;
1571                 goto check_child2;
1572         }
1573
1574         rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1575                                     LVLT_BY_BOOKMARK);
1576         if (lu_fid_eq(pfid, &LU_LPF_FID))
1577                 GOTO(put, rc);
1578
1579 check_child2:
1580         if (child2 != NULL)
1581                 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1582                                             pfid, LVLT_BY_NAMEENTRY);
1583
1584         GOTO(put, rc);
1585
1586 put:
1587         if (lfsck->li_lpf_obj != NULL) {
1588                 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1589                         lfsck_object_put(env, lfsck->li_lpf_obj);
1590                         lfsck->li_lpf_obj = NULL;
1591                         rc = -ENOTDIR;
1592                 }
1593         } else if (rc == 0) {
1594                 rc = lfsck_create_lpf(env, lfsck);
1595         }
1596
1597         if (child2 != NULL && !IS_ERR(child2))
1598                 lfsck_object_put(env, child2);
1599         if (child1 != NULL && !IS_ERR(child1))
1600                 lfsck_object_put(env, child1);
1601
1602         return rc;
1603 }
1604
1605 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1606 {
1607         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
1608         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
1609         char                    *prefix;
1610         int                      rc     = 0;
1611         ENTRY;
1612
1613         if (unlikely(ss == NULL))
1614                 RETURN(-ENXIO);
1615
1616         OBD_ALLOC_PTR(lfsck->li_seq);
1617         if (lfsck->li_seq == NULL)
1618                 RETURN(-ENOMEM);
1619
1620         OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1621         if (prefix == NULL)
1622                 GOTO(out, rc = -ENOMEM);
1623
1624         snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1625         seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1626                              ss->ss_server_seq);
1627         OBD_FREE(prefix, MAX_OBD_NAME + 7);
1628
1629         if (fid_is_sane(&bk->lb_last_fid))
1630                 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1631
1632         RETURN(0);
1633
1634 out:
1635         OBD_FREE_PTR(lfsck->li_seq);
1636         lfsck->li_seq = NULL;
1637
1638         return rc;
1639 }
1640
1641 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1642 {
1643         if (lfsck->li_seq != NULL) {
1644                 seq_client_fini(lfsck->li_seq);
1645                 OBD_FREE_PTR(lfsck->li_seq);
1646                 lfsck->li_seq = NULL;
1647         }
1648 }
1649
1650 void lfsck_instance_cleanup(const struct lu_env *env,
1651                             struct lfsck_instance *lfsck)
1652 {
1653         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1654         struct lfsck_component  *com;
1655         struct lfsck_component  *next;
1656         struct lfsck_lmv_unit   *llu;
1657         struct lfsck_lmv_unit   *llu_next;
1658         struct lfsck_lmv        *llmv;
1659         ENTRY;
1660
1661         LASSERT(list_empty(&lfsck->li_link));
1662         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1663
1664         if (lfsck->li_obj_oit != NULL) {
1665                 lfsck_object_put(env, lfsck->li_obj_oit);
1666                 lfsck->li_obj_oit = NULL;
1667         }
1668
1669         LASSERT(lfsck->li_obj_dir == NULL);
1670         LASSERT(lfsck->li_lmv == NULL);
1671
1672         list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1673                 llmv = &llu->llu_lmv;
1674
1675                 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1676                          "still in using: %u\n",
1677                          atomic_read(&llmv->ll_ref));
1678
1679                 lfsck_lmv_put(env, llmv);
1680         }
1681
1682         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1683                 lfsck_component_cleanup(env, com);
1684         }
1685
1686         LASSERT(list_empty(&lfsck->li_list_dir));
1687
1688         list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1689                                  lc_link) {
1690                 lfsck_component_cleanup(env, com);
1691         }
1692
1693         list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1694                 lfsck_component_cleanup(env, com);
1695         }
1696
1697         lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1698         lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1699
1700         if (lfsck->li_lfsck_dir != NULL) {
1701                 lfsck_object_put(env, lfsck->li_lfsck_dir);
1702                 lfsck->li_lfsck_dir = NULL;
1703         }
1704
1705         if (lfsck->li_bookmark_obj != NULL) {
1706                 lfsck_object_put(env, lfsck->li_bookmark_obj);
1707                 lfsck->li_bookmark_obj = NULL;
1708         }
1709
1710         if (lfsck->li_lpf_obj != NULL) {
1711                 lfsck_object_put(env, lfsck->li_lpf_obj);
1712                 lfsck->li_lpf_obj = NULL;
1713         }
1714
1715         if (lfsck->li_lpf_root_obj != NULL) {
1716                 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1717                 lfsck->li_lpf_root_obj = NULL;
1718         }
1719
1720         if (lfsck->li_los != NULL) {
1721                 local_oid_storage_fini(env, lfsck->li_los);
1722                 lfsck->li_los = NULL;
1723         }
1724
1725         lfsck_fid_fini(lfsck);
1726
1727         OBD_FREE_PTR(lfsck);
1728 }
1729
1730 static inline struct lfsck_instance *
1731 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1732 {
1733         struct lfsck_instance *lfsck;
1734
1735         list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1736                 if (lfsck->li_bottom == key) {
1737                         if (ref)
1738                                 lfsck_instance_get(lfsck);
1739                         if (unlink)
1740                                 list_del_init(&lfsck->li_link);
1741
1742                         return lfsck;
1743                 }
1744         }
1745
1746         return NULL;
1747 }
1748
1749 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1750                                            bool unlink)
1751 {
1752         struct lfsck_instance *lfsck;
1753
1754         spin_lock(&lfsck_instance_lock);
1755         lfsck = __lfsck_instance_find(key, ref, unlink);
1756         spin_unlock(&lfsck_instance_lock);
1757
1758         return lfsck;
1759 }
1760
1761 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1762 {
1763         struct lfsck_instance *tmp;
1764
1765         spin_lock(&lfsck_instance_lock);
1766         list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1767                 if (lfsck->li_bottom == tmp->li_bottom) {
1768                         spin_unlock(&lfsck_instance_lock);
1769                         return -EEXIST;
1770                 }
1771         }
1772
1773         list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1774         spin_unlock(&lfsck_instance_lock);
1775         return 0;
1776 }
1777
1778 void lfsck_bits_dump(struct seq_file *m, int bits, const char *const names[],
1779                      const char *prefix)
1780 {
1781         int flag;
1782         int i;
1783         bool newline = (bits != 0 ? false : true);
1784
1785         seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1786
1787         for (i = 0, flag = 1; bits != 0; i++, flag = BIT(i)) {
1788                 if (flag & bits) {
1789                         bits &= ~flag;
1790                         if (names[i] != NULL) {
1791                                 if (bits == 0)
1792                                         newline = true;
1793
1794                                 seq_printf(m, "%s%c", names[i],
1795                                            newline ? '\n' : ',');
1796                         }
1797                 }
1798         }
1799
1800         if (!newline)
1801                 seq_putc(m, '\n');
1802 }
1803
1804 void lfsck_time_dump(struct seq_file *m, time64_t time, const char *name)
1805 {
1806         if (time == 0) {
1807                 seq_printf(m, "%s_time: N/A\n", name);
1808                 seq_printf(m, "time_since_%s: N/A\n", name);
1809         } else {
1810                 seq_printf(m, "%s_time: %lld\n", name, time);
1811                 seq_printf(m, "time_since_%s: %lld seconds\n",
1812                            name, ktime_get_real_seconds() - time);
1813         }
1814 }
1815
1816 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1817                     const char *prefix)
1818 {
1819         if (fid_is_zero(&pos->lp_dir_parent)) {
1820                 if (pos->lp_oit_cookie == 0) {
1821                         seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1822                         return;
1823                 }
1824                 seq_printf(m, "%s: %llu, N/A, N/A\n",
1825                            prefix, pos->lp_oit_cookie);
1826         } else {
1827                 seq_printf(m, "%s: %llu, "DFID", %#llx\n",
1828                            prefix, pos->lp_oit_cookie,
1829                            PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1830         }
1831 }
1832
1833 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1834                     struct lfsck_position *pos, bool init)
1835 {
1836         const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1837
1838         if (unlikely(lfsck->li_di_oit == NULL)) {
1839                 memset(pos, 0, sizeof(*pos));
1840                 return;
1841         }
1842
1843         pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1844         if (!lfsck->li_current_oit_processed && !init)
1845                 pos->lp_oit_cookie--;
1846
1847         if (unlikely(pos->lp_oit_cookie == 0))
1848                 pos->lp_oit_cookie = 1;
1849
1850         if (lfsck->li_di_dir != NULL) {
1851                 struct dt_object *dto = lfsck->li_obj_dir;
1852
1853                 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1854                                                         lfsck->li_di_dir);
1855
1856                 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1857                         fid_zero(&pos->lp_dir_parent);
1858                         pos->lp_dir_cookie = 0;
1859                 } else {
1860                         pos->lp_dir_parent = *lfsck_dto2fid(dto);
1861                 }
1862         } else {
1863                 fid_zero(&pos->lp_dir_parent);
1864                 pos->lp_dir_cookie = 0;
1865         }
1866 }
1867
1868 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1869 {
1870         bool dirty = false;
1871
1872         if (limit != LFSCK_SPEED_NO_LIMIT) {
1873                 if (limit > cfs_time_seconds(1)) {
1874                         lfsck->li_sleep_rate = limit / cfs_time_seconds(1);
1875                         lfsck->li_sleep_jif = 1;
1876                 } else {
1877                         lfsck->li_sleep_rate = 1;
1878                         lfsck->li_sleep_jif = cfs_time_seconds(1) / limit;
1879                 }
1880         } else {
1881                 lfsck->li_sleep_jif = 0;
1882                 lfsck->li_sleep_rate = 0;
1883         }
1884
1885         if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1886                 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1887                 dirty = true;
1888         }
1889
1890         return dirty;
1891 }
1892
1893 void lfsck_control_speed(struct lfsck_instance *lfsck)
1894 {
1895         struct ptlrpc_thread *thread = &lfsck->li_thread;
1896
1897         if (lfsck->li_sleep_jif > 0 &&
1898             lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1899                 wait_event_idle_timeout(thread->t_ctl_waitq,
1900                                         !thread_is_running(thread),
1901                                         lfsck->li_sleep_jif);
1902                 lfsck->li_new_scanned = 0;
1903         }
1904 }
1905
1906 void lfsck_control_speed_by_self(struct lfsck_component *com)
1907 {
1908         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1909         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1910
1911         if (lfsck->li_sleep_jif > 0 &&
1912             com->lc_new_scanned >= lfsck->li_sleep_rate) {
1913                 wait_event_idle_timeout(thread->t_ctl_waitq,
1914                                         !thread_is_running(thread),
1915                                         lfsck->li_sleep_jif);
1916                 com->lc_new_scanned = 0;
1917         }
1918 }
1919
1920 static struct lfsck_thread_args *
1921 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1922                        struct lfsck_component *com,
1923                        struct lfsck_start_param *lsp)
1924 {
1925         struct lfsck_thread_args *lta;
1926         int                       rc;
1927
1928         OBD_ALLOC_PTR(lta);
1929         if (lta == NULL)
1930                 return ERR_PTR(-ENOMEM);
1931
1932         rc = lu_env_init(&lta->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1933         if (rc != 0) {
1934                 OBD_FREE_PTR(lta);
1935                 return ERR_PTR(rc);
1936         }
1937
1938         lta->lta_lfsck = lfsck_instance_get(lfsck);
1939         if (com != NULL)
1940                 lta->lta_com = lfsck_component_get(com);
1941
1942         lta->lta_lsp = lsp;
1943
1944         return lta;
1945 }
1946
1947 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1948 {
1949         if (lta->lta_com != NULL)
1950                 lfsck_component_put(&lta->lta_env, lta->lta_com);
1951         lfsck_instance_put(&lta->lta_env, lta->lta_lfsck);
1952         lu_env_fini(&lta->lta_env);
1953         OBD_FREE_PTR(lta);
1954 }
1955
1956 struct lfsck_assistant_data *
1957 lfsck_assistant_data_init(const struct lfsck_assistant_operations *lao,
1958                           const char *name)
1959 {
1960         struct lfsck_assistant_data *lad;
1961
1962         OBD_ALLOC_PTR(lad);
1963         if (lad != NULL) {
1964                 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1965                 if (lad->lad_bitmap == NULL) {
1966                         OBD_FREE_PTR(lad);
1967                         return NULL;
1968                 }
1969
1970                 INIT_LIST_HEAD(&lad->lad_req_list);
1971                 spin_lock_init(&lad->lad_lock);
1972                 INIT_LIST_HEAD(&lad->lad_ost_list);
1973                 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1974                 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1975                 INIT_LIST_HEAD(&lad->lad_mdt_list);
1976                 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1977                 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1978                 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1979                 lad->lad_ops = lao;
1980                 lad->lad_name = name;
1981         }
1982
1983         return lad;
1984 }
1985
1986 struct lfsck_assistant_object *
1987 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1988                             const struct lu_attr *attr, __u64 cookie,
1989                             bool is_dir)
1990 {
1991         struct lfsck_assistant_object   *lso;
1992
1993         OBD_ALLOC_PTR(lso);
1994         if (lso == NULL)
1995                 return ERR_PTR(-ENOMEM);
1996
1997         lso->lso_fid = *fid;
1998         if (attr != NULL)
1999                 lso->lso_attr = *attr;
2000
2001         atomic_set(&lso->lso_ref, 1);
2002         lso->lso_oit_cookie = cookie;
2003         if (is_dir)
2004                 lso->lso_is_dir = 1;
2005
2006         return lso;
2007 }
2008
2009 struct dt_object *
2010 lfsck_assistant_object_load(const struct lu_env *env,
2011                             struct lfsck_instance *lfsck,
2012                             struct lfsck_assistant_object *lso)
2013 {
2014         struct dt_object *obj;
2015
2016         obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2017         if (IS_ERR(obj))
2018                 return obj;
2019
2020         if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2021                 lso->lso_dead = 1;
2022                 lfsck_object_put(env, obj);
2023
2024                 return ERR_PTR(-ENOENT);
2025         }
2026
2027         if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2028                 lfsck_object_put(env, obj);
2029
2030                 return ERR_PTR(-ENOTDIR);
2031         }
2032
2033         return obj;
2034 }
2035
2036 /**
2037  * Generic LFSCK asynchronous communication interpretor function.
2038  * The LFSCK RPC reply for both the event notification and status
2039  * querying will be handled here.
2040  *
2041  * \param[in] env       pointer to the thread context
2042  * \param[in] req       pointer to the LFSCK request
2043  * \param[in] args      pointer to the lfsck_async_interpret_args
2044  * \param[in] rc        the result for handling the LFSCK request
2045  *
2046  * \retval              0 for success
2047  * \retval              negative error number on failure
2048  */
2049 int lfsck_async_interpret_common(const struct lu_env *env,
2050                                  struct ptlrpc_request *req,
2051                                  void *args, int rc)
2052 {
2053         struct lfsck_async_interpret_args *laia = args;
2054         struct lfsck_component            *com  = laia->laia_com;
2055         struct lfsck_assistant_data       *lad  = com->lc_data;
2056         struct lfsck_tgt_descs            *ltds = laia->laia_ltds;
2057         struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
2058         struct lfsck_request              *lr   = laia->laia_lr;
2059
2060         LASSERT(com->lc_lfsck->li_master);
2061
2062         switch (lr->lr_event) {
2063         case LE_START:
2064                 if (unlikely(rc == -EINPROGRESS)) {
2065                         ltd->ltd_retry_start = 1;
2066                         break;
2067                 }
2068
2069                 if (rc != 0) {
2070                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2071                                "start: rc = %d\n",
2072                                lfsck_lfsck2name(com->lc_lfsck),
2073                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2074                                ltd->ltd_index, lad->lad_name, rc);
2075
2076                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2077                                 struct lfsck_layout *lo = com->lc_file_ram;
2078
2079                                 if (lr->lr_flags & LEF_TO_OST)
2080                                         lfsck_lad_set_bitmap(env, com,
2081                                                              ltd->ltd_index);
2082                                 else
2083                                         lo->ll_flags |= LF_INCOMPLETE;
2084                         } else {
2085                                 struct lfsck_namespace *ns = com->lc_file_ram;
2086
2087                                 /* If some MDT does not join the namespace
2088                                  * LFSCK, then we cannot know whether there
2089                                  * is some name entry on such MDT that with
2090                                  * the referenced MDT-object on this MDT or
2091                                  * not. So the namespace LFSCK on this MDT
2092                                  * cannot handle orphan MDT-objects properly.
2093                                  * So we mark the LFSCK as LF_INCOMPLETE and
2094                                  * skip orphan MDT-objects handling. */
2095                                 ns->ln_flags |= LF_INCOMPLETE;
2096                         }
2097                         break;
2098                 }
2099
2100                 spin_lock(&ltds->ltd_lock);
2101                 if (ltd->ltd_dead) {
2102                         spin_unlock(&ltds->ltd_lock);
2103                         break;
2104                 }
2105
2106                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2107                         struct list_head *list;
2108                         struct list_head *phase_list;
2109
2110                         if (ltd->ltd_layout_done) {
2111                                 spin_unlock(&ltds->ltd_lock);
2112                                 break;
2113                         }
2114
2115                         if (lr->lr_flags & LEF_TO_OST) {
2116                                 list = &lad->lad_ost_list;
2117                                 phase_list = &lad->lad_ost_phase1_list;
2118                         } else {
2119                                 list = &lad->lad_mdt_list;
2120                                 phase_list = &lad->lad_mdt_phase1_list;
2121                         }
2122
2123                         if (list_empty(&ltd->ltd_layout_list))
2124                                 list_add_tail(&ltd->ltd_layout_list, list);
2125                         if (list_empty(&ltd->ltd_layout_phase_list))
2126                                 list_add_tail(&ltd->ltd_layout_phase_list,
2127                                               phase_list);
2128                 } else {
2129                         if (ltd->ltd_namespace_done) {
2130                                 spin_unlock(&ltds->ltd_lock);
2131                                 break;
2132                         }
2133
2134                         if (list_empty(&ltd->ltd_namespace_list))
2135                                 list_add_tail(&ltd->ltd_namespace_list,
2136                                               &lad->lad_mdt_list);
2137                         if (list_empty(&ltd->ltd_namespace_phase_list))
2138                                 list_add_tail(&ltd->ltd_namespace_phase_list,
2139                                               &lad->lad_mdt_phase1_list);
2140                 }
2141                 spin_unlock(&ltds->ltd_lock);
2142                 break;
2143         case LE_STOP:
2144         case LE_PHASE1_DONE:
2145         case LE_PHASE2_DONE:
2146         case LE_PEER_EXIT:
2147                 if (rc != 0 && rc != -EALREADY)
2148                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2149                               "event = %d, rc = %d\n",
2150                               lfsck_lfsck2name(com->lc_lfsck),
2151                               (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2152                               ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2153                 break;
2154         case LE_QUERY: {
2155                 struct lfsck_reply *reply;
2156                 struct list_head *list;
2157                 struct list_head *phase_list;
2158
2159                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2160                         list = &ltd->ltd_layout_list;
2161                         phase_list = &ltd->ltd_layout_phase_list;
2162                 } else {
2163                         list = &ltd->ltd_namespace_list;
2164                         phase_list = &ltd->ltd_namespace_phase_list;
2165                 }
2166
2167                 if (rc != 0) {
2168                         if (lr->lr_flags & LEF_QUERY_ALL) {
2169                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2170                                 break;
2171                         }
2172
2173                         spin_lock(&ltds->ltd_lock);
2174                         list_del_init(phase_list);
2175                         list_del_init(list);
2176                         spin_unlock(&ltds->ltd_lock);
2177                         break;
2178                 }
2179
2180                 reply = req_capsule_server_get(&req->rq_pill,
2181                                                &RMF_LFSCK_REPLY);
2182                 if (reply == NULL) {
2183                         rc = -EPROTO;
2184                         CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2185                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2186                                lad->lad_name, rc);
2187
2188                         if (lr->lr_flags & LEF_QUERY_ALL) {
2189                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2190                                 break;
2191                         }
2192
2193                         spin_lock(&ltds->ltd_lock);
2194                         list_del_init(phase_list);
2195                         list_del_init(list);
2196                         spin_unlock(&ltds->ltd_lock);
2197                         break;
2198                 }
2199
2200                 if (lr->lr_flags & LEF_QUERY_ALL) {
2201                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2202                                 ltd->ltd_layout_status = reply->lr_status;
2203                                 ltd->ltd_layout_repaired = reply->lr_repaired;
2204                         } else {
2205                                 ltd->ltd_namespace_status = reply->lr_status;
2206                                 ltd->ltd_namespace_repaired =
2207                                                         reply->lr_repaired;
2208                         }
2209                         break;
2210                 }
2211
2212                 switch (reply->lr_status) {
2213                 case LS_SCANNING_PHASE1:
2214                         break;
2215                 case LS_SCANNING_PHASE2:
2216                         spin_lock(&ltds->ltd_lock);
2217                         list_del_init(phase_list);
2218                         if (ltd->ltd_dead) {
2219                                 spin_unlock(&ltds->ltd_lock);
2220                                 break;
2221                         }
2222
2223                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2224                                 if (ltd->ltd_layout_done) {
2225                                         spin_unlock(&ltds->ltd_lock);
2226                                         break;
2227                                 }
2228
2229                                 if (lr->lr_flags & LEF_TO_OST)
2230                                         list_add_tail(phase_list,
2231                                                 &lad->lad_ost_phase2_list);
2232                                 else
2233                                         list_add_tail(phase_list,
2234                                                 &lad->lad_mdt_phase2_list);
2235                         } else {
2236                                 if (ltd->ltd_namespace_done) {
2237                                         spin_unlock(&ltds->ltd_lock);
2238                                         break;
2239                                 }
2240
2241                                 list_add_tail(phase_list,
2242                                               &lad->lad_mdt_phase2_list);
2243                         }
2244                         spin_unlock(&ltds->ltd_lock);
2245                         break;
2246                 default:
2247                         spin_lock(&ltds->ltd_lock);
2248                         list_del_init(phase_list);
2249                         list_del_init(list);
2250                         spin_unlock(&ltds->ltd_lock);
2251                         break;
2252                 }
2253                 break;
2254         }
2255         default:
2256                 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2257                        lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2258                 break;
2259         }
2260
2261         if (!laia->laia_shared) {
2262                 lfsck_tgt_put(ltd);
2263                 lfsck_component_put(env, com);
2264         }
2265
2266         return 0;
2267 }
2268
2269 static void lfsck_interpret(const struct lu_env *env,
2270                             struct lfsck_instance *lfsck,
2271                             struct ptlrpc_request *req, void *args, int result)
2272 {
2273         struct lfsck_async_interpret_args *laia = args;
2274         struct lfsck_component            *com;
2275
2276         LASSERT(laia->laia_com == NULL);
2277         LASSERT(laia->laia_shared);
2278
2279         spin_lock(&lfsck->li_lock);
2280         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2281                 laia->laia_com = com;
2282                 lfsck_async_interpret_common(env, req, laia, result);
2283         }
2284
2285         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2286                 laia->laia_com = com;
2287                 lfsck_async_interpret_common(env, req, laia, result);
2288         }
2289         spin_unlock(&lfsck->li_lock);
2290 }
2291
2292 static int lfsck_stop_notify(const struct lu_env *env,
2293                              struct lfsck_instance *lfsck,
2294                              struct lfsck_tgt_descs *ltds,
2295                              struct lfsck_tgt_desc *ltd, __u16 type)
2296 {
2297         struct lfsck_component *com;
2298         int                     rc = 0;
2299         ENTRY;
2300
2301         LASSERT(lfsck->li_master);
2302
2303         spin_lock(&lfsck->li_lock);
2304         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2305         if (com == NULL)
2306                 com = __lfsck_component_find(lfsck, type,
2307                                              &lfsck->li_list_double_scan);
2308         if (com != NULL)
2309                 lfsck_component_get(com);
2310         spin_unlock(&lfsck->li_lock);
2311
2312         if (com != NULL) {
2313                 struct lfsck_thread_info          *info  = lfsck_env_info(env);
2314                 struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2315                 struct lfsck_request              *lr    = &info->lti_lr;
2316                 struct lfsck_assistant_data       *lad   = com->lc_data;
2317                 struct list_head                  *list;
2318                 struct list_head                  *phase_list;
2319                 struct ptlrpc_request_set         *set;
2320
2321                 set = ptlrpc_prep_set();
2322                 if (set == NULL) {
2323                         lfsck_component_put(env, com);
2324
2325                         RETURN(-ENOMEM);
2326                 }
2327
2328                 if (type == LFSCK_TYPE_LAYOUT) {
2329                         list = &ltd->ltd_layout_list;
2330                         phase_list = &ltd->ltd_layout_phase_list;
2331                 } else {
2332                         list = &ltd->ltd_namespace_list;
2333                         phase_list = &ltd->ltd_namespace_phase_list;
2334                 }
2335
2336                 spin_lock(&ltds->ltd_lock);
2337                 if (list_empty(list)) {
2338                         LASSERT(list_empty(phase_list));
2339                         spin_unlock(&ltds->ltd_lock);
2340                         ptlrpc_set_destroy(set);
2341
2342                         RETURN(0);
2343                 }
2344
2345                 list_del_init(phase_list);
2346                 list_del_init(list);
2347                 spin_unlock(&ltds->ltd_lock);
2348
2349                 memset(lr, 0, sizeof(*lr));
2350                 lr->lr_index = lfsck_dev_idx(lfsck);
2351                 lr->lr_event = LE_PEER_EXIT;
2352                 lr->lr_active = type;
2353                 lr->lr_status = LS_CO_PAUSED;
2354                 if (ltds == &lfsck->li_ost_descs)
2355                         lr->lr_flags = LEF_TO_OST;
2356
2357                 memset(laia, 0, sizeof(*laia));
2358                 laia->laia_com = com;
2359                 laia->laia_ltds = ltds;
2360                 atomic_inc(&ltd->ltd_ref);
2361                 laia->laia_ltd = ltd;
2362                 laia->laia_lr = lr;
2363
2364                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2365                                          lfsck_async_interpret_common,
2366                                          laia, LFSCK_NOTIFY);
2367                 if (rc != 0) {
2368                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2369                                "co-stop for %s: rc = %d\n",
2370                                lfsck_lfsck2name(lfsck),
2371                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2372                                ltd->ltd_index, lad->lad_name, rc);
2373                         lfsck_tgt_put(ltd);
2374                 } else {
2375                         rc = ptlrpc_set_wait(env, set);
2376                 }
2377
2378                 ptlrpc_set_destroy(set);
2379                 lfsck_component_put(env, com);
2380         }
2381
2382         RETURN(rc);
2383 }
2384
2385 static int lfsck_async_interpret(const struct lu_env *env,
2386                                  struct ptlrpc_request *req,
2387                                  void *args, int rc)
2388 {
2389         struct lfsck_async_interpret_args *laia = args;
2390         struct lfsck_instance             *lfsck;
2391
2392         lfsck = container_of(laia->laia_ltds, struct lfsck_instance,
2393                              li_mdt_descs);
2394         lfsck_interpret(env, lfsck, req, laia, rc);
2395         lfsck_tgt_put(laia->laia_ltd);
2396         if (rc != 0 && laia->laia_result != -EALREADY)
2397                 laia->laia_result = rc;
2398
2399         return 0;
2400 }
2401
2402 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2403                         struct lfsck_request *lr,
2404                         struct ptlrpc_request_set *set,
2405                         ptlrpc_interpterer_t interpreter,
2406                         void *args, int request)
2407 {
2408         struct lfsck_async_interpret_args *laia;
2409         struct ptlrpc_request             *req;
2410         struct lfsck_request              *tmp;
2411         struct req_format                 *format;
2412         int                                rc;
2413
2414         switch (request) {
2415         case LFSCK_NOTIFY:
2416                 format = &RQF_LFSCK_NOTIFY;
2417                 break;
2418         case LFSCK_QUERY:
2419                 format = &RQF_LFSCK_QUERY;
2420                 break;
2421         default:
2422                 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2423                        exp->exp_obd->obd_name, request, -EINVAL);
2424                 return -EINVAL;
2425         }
2426
2427         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2428         if (req == NULL)
2429                 return -ENOMEM;
2430
2431         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2432         if (rc != 0) {
2433                 ptlrpc_request_free(req);
2434
2435                 return rc;
2436         }
2437
2438         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2439         *tmp = *lr;
2440         ptlrpc_request_set_replen(req);
2441
2442         laia = ptlrpc_req_async_args(laia, req);
2443         *laia = *(struct lfsck_async_interpret_args *)args;
2444         if (laia->laia_com != NULL)
2445                 lfsck_component_get(laia->laia_com);
2446         req->rq_interpret_reply = interpreter;
2447         req->rq_allow_intr = 1;
2448         req->rq_no_delay = 1;
2449         ptlrpc_set_add_req(set, req);
2450
2451         return 0;
2452 }
2453
2454 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2455 {
2456         struct lfsck_thread_info          *info  = lfsck_env_info(env);
2457         struct lfsck_request              *lr    = &info->lti_lr;
2458         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2459         struct lfsck_instance             *lfsck = com->lc_lfsck;
2460         struct lfsck_tgt_descs            *ltds  = &lfsck->li_mdt_descs;
2461         struct lfsck_tgt_desc             *ltd;
2462         struct ptlrpc_request_set         *set;
2463         int                                idx;
2464         int                                rc;
2465         ENTRY;
2466
2467         memset(lr, 0, sizeof(*lr));
2468         lr->lr_event = LE_QUERY;
2469         lr->lr_active = com->lc_type;
2470         lr->lr_flags = LEF_QUERY_ALL;
2471
2472         memset(laia, 0, sizeof(*laia));
2473         laia->laia_com = com;
2474         laia->laia_lr = lr;
2475
2476         set = ptlrpc_prep_set();
2477         if (set == NULL)
2478                 RETURN(-ENOMEM);
2479
2480 again:
2481         laia->laia_ltds = ltds;
2482         down_read(&ltds->ltd_rw_sem);
2483         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2484                 ltd = lfsck_tgt_get(ltds, idx);
2485                 LASSERT(ltd != NULL);
2486
2487                 laia->laia_ltd = ltd;
2488                 up_read(&ltds->ltd_rw_sem);
2489                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2490                                          lfsck_async_interpret_common,
2491                                          laia, LFSCK_QUERY);
2492                 if (rc != 0) {
2493                         struct lfsck_assistant_data *lad = com->lc_data;
2494
2495                         CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2496                                "rc = %d\n", lfsck_lfsck2name(lfsck),
2497                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2498                                ltd->ltd_index, lad->lad_name, rc);
2499                         lfsck_reset_ltd_status(ltd, com->lc_type);
2500                         lfsck_tgt_put(ltd);
2501                 }
2502                 down_read(&ltds->ltd_rw_sem);
2503         }
2504         up_read(&ltds->ltd_rw_sem);
2505
2506         if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2507                 ltds = &lfsck->li_ost_descs;
2508                 lr->lr_flags |= LEF_TO_OST;
2509                 goto again;
2510         }
2511
2512         rc = ptlrpc_set_wait(env, set);
2513         ptlrpc_set_destroy(set);
2514
2515         RETURN(rc);
2516 }
2517
2518 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2519                           struct lfsck_start_param *lsp)
2520 {
2521         struct lfsck_instance           *lfsck   = com->lc_lfsck;
2522         struct lfsck_assistant_data     *lad     = com->lc_data;
2523         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
2524         struct ptlrpc_thread            *athread = &lad->lad_thread;
2525         struct lfsck_thread_args        *lta;
2526         struct task_struct              *task;
2527         int                              rc;
2528         ENTRY;
2529
2530         lad->lad_assistant_status = 0;
2531         lad->lad_post_result = 0;
2532         lad->lad_flags = 0;
2533         lad->lad_advance_lock = false;
2534         thread_set_flags(athread, 0);
2535
2536         lta = lfsck_thread_args_init(lfsck, com, lsp);
2537         if (IS_ERR(lta))
2538                 RETURN(PTR_ERR(lta));
2539
2540         task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2541         if (IS_ERR(task)) {
2542                 rc = PTR_ERR(task);
2543                 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2544                        "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2545                 lfsck_thread_args_fini(lta);
2546         } else {
2547                 wait_event_idle(mthread->t_ctl_waitq,
2548                                 thread_is_running(athread) ||
2549                                 thread_is_stopped(athread) ||
2550                                 !thread_is_starting(mthread));
2551                 if (unlikely(!thread_is_starting(mthread)))
2552                         /* stopped by race */
2553                         rc = -ESRCH;
2554                 else if (unlikely(!thread_is_running(athread)))
2555                         rc = lad->lad_assistant_status;
2556                 else
2557                         rc = 0;
2558         }
2559
2560         RETURN(rc);
2561 }
2562
2563 int lfsck_checkpoint_generic(const struct lu_env *env,
2564                              struct lfsck_component *com)
2565 {
2566         struct lfsck_assistant_data     *lad     = com->lc_data;
2567         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2568         struct ptlrpc_thread            *athread = &lad->lad_thread;
2569
2570         wait_event_idle(mthread->t_ctl_waitq,
2571                         list_empty(&lad->lad_req_list) ||
2572                         !thread_is_running(mthread) ||
2573                         thread_is_stopped(athread));
2574
2575         if (!thread_is_running(mthread) || thread_is_stopped(athread))
2576                 return LFSCK_CHECKPOINT_SKIP;
2577
2578         return 0;
2579 }
2580
2581 void lfsck_post_generic(const struct lu_env *env,
2582                         struct lfsck_component *com, int *result)
2583 {
2584         struct lfsck_assistant_data     *lad     = com->lc_data;
2585         struct ptlrpc_thread            *athread = &lad->lad_thread;
2586         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2587
2588         lad->lad_post_result = *result;
2589         if (*result <= 0)
2590                 set_bit(LAD_EXIT, &lad->lad_flags);
2591         set_bit(LAD_TO_POST, &lad->lad_flags);
2592
2593         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2594                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2595
2596         wake_up(&athread->t_ctl_waitq);
2597         wait_event_idle(mthread->t_ctl_waitq,
2598                         (*result > 0 && list_empty(&lad->lad_req_list)) ||
2599                         thread_is_stopped(athread));
2600
2601         if (lad->lad_assistant_status < 0)
2602                 *result = lad->lad_assistant_status;
2603
2604         CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2605                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2606 }
2607
2608 int lfsck_double_scan_generic(const struct lu_env *env,
2609                               struct lfsck_component *com, int status)
2610 {
2611         struct lfsck_assistant_data     *lad     = com->lc_data;
2612         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2613         struct ptlrpc_thread            *athread = &lad->lad_thread;
2614
2615         if (status != LS_SCANNING_PHASE2)
2616                 set_bit(LAD_EXIT, &lad->lad_flags);
2617         else
2618                 set_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags);
2619
2620         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2621                "status %d\n",
2622                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2623
2624         wake_up(&athread->t_ctl_waitq);
2625         wait_event_idle(mthread->t_ctl_waitq,
2626                         test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags) ||
2627                         thread_is_stopped(athread));
2628
2629         CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2630                "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2631                lad->lad_assistant_status);
2632
2633         if (lad->lad_assistant_status < 0)
2634                 return lad->lad_assistant_status;
2635
2636         return 0;
2637 }
2638
2639 void lfsck_quit_generic(const struct lu_env *env,
2640                         struct lfsck_component *com)
2641 {
2642         struct lfsck_assistant_data     *lad     = com->lc_data;
2643         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2644         struct ptlrpc_thread            *athread = &lad->lad_thread;
2645
2646         set_bit(LAD_EXIT, &lad->lad_flags);
2647         wake_up(&athread->t_ctl_waitq);
2648         wait_event_idle(mthread->t_ctl_waitq,
2649                         thread_is_init(athread) ||
2650                         thread_is_stopped(athread));
2651 }
2652
2653 int lfsck_load_one_trace_file(const struct lu_env *env,
2654                               struct lfsck_component *com,
2655                               struct dt_object *parent,
2656                               struct dt_object **child,
2657                               const struct dt_index_features *ft,
2658                               const char *name, bool reset)
2659 {
2660         struct lfsck_instance *lfsck = com->lc_lfsck;
2661         struct dt_object *obj;
2662         int rc;
2663         ENTRY;
2664
2665         if (*child != NULL) {
2666                 struct dt_it *it;
2667                 const struct dt_it_ops *iops;
2668                 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2669
2670                 if (!reset)
2671                         RETURN(0);
2672
2673                 obj = *child;
2674                 rc = obj->do_ops->do_index_try(env, obj, ft);
2675                 if (rc)
2676                         /* unlink by force */
2677                         goto unlink;
2678
2679                 iops = &obj->do_index_ops->dio_it;
2680                 it = iops->init(env, obj, 0);
2681                 if (IS_ERR(it))
2682                         /* unlink by force */
2683                         goto unlink;
2684
2685                 fid_zero(fid);
2686                 rc = iops->get(env, it, (const struct dt_key *)fid);
2687                 if (rc >= 0) {
2688                         rc = iops->next(env, it);
2689                         iops->put(env, it);
2690                 }
2691                 iops->fini(env, it);
2692                 if (rc > 0)
2693                         /* "rc > 0" means the index file is empty. */
2694                         RETURN(0);
2695
2696 unlink:
2697                 /* The old index is not empty, remove it firstly. */
2698                 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2699                 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2700                              "%s: unlink lfsck sub trace file %s: rc = %d\n",
2701                              lfsck_lfsck2name(com->lc_lfsck), name, rc);
2702                 if (rc)
2703                         RETURN(rc);
2704
2705                 if (*child) {
2706                         lfsck_object_put(env, *child);
2707                         *child = NULL;
2708                 }
2709         } else if (reset) {
2710                 goto unlink;
2711         }
2712
2713         obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2714                                          S_IFREG | S_IRUGO | S_IWUSR, ft);
2715         if (IS_ERR(obj))
2716                 RETURN(PTR_ERR(obj));
2717
2718         rc = obj->do_ops->do_index_try(env, obj, ft);
2719         if (rc) {
2720                 lfsck_object_put(env, obj);
2721                 CDEBUG(D_LFSCK, "%s: LFSCK fail to load "
2722                        "sub trace file %s: rc = %d\n",
2723                        lfsck_lfsck2name(com->lc_lfsck), name, rc);
2724         } else {
2725                 *child = obj;
2726         }
2727
2728         RETURN(rc);
2729 }
2730
2731 int lfsck_load_sub_trace_files(const struct lu_env *env,
2732                                struct lfsck_component *com,
2733                                const struct dt_index_features *ft,
2734                                const char *prefix, bool reset)
2735 {
2736         char *name = lfsck_env_info(env)->lti_key;
2737         struct lfsck_sub_trace_obj *lsto;
2738         int rc;
2739         int i;
2740
2741         for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2742              i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2743                 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2744                 rc = lfsck_load_one_trace_file(env, com,
2745                                 com->lc_lfsck->li_lfsck_dir,
2746                                 &lsto->lsto_obj, ft, name, reset);
2747         }
2748
2749         return rc;
2750 }
2751
2752 /* external interfaces */
2753 int lfsck_get_speed(char *buf, struct dt_device *key)
2754 {
2755         struct lu_env           env;
2756         struct lfsck_instance  *lfsck;
2757         int                     rc;
2758         ENTRY;
2759
2760         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2761         if (rc != 0)
2762                 RETURN(rc);
2763
2764         lfsck = lfsck_instance_find(key, true, false);
2765         if (lfsck && buf) {
2766                 rc = sprintf(buf, "%u\n",
2767                              lfsck->li_bookmark_ram.lb_speed_limit);
2768                 lfsck_instance_put(&env, lfsck);
2769         } else {
2770                 rc = -ENXIO;
2771         }
2772
2773         lu_env_fini(&env);
2774
2775         RETURN(rc);
2776 }
2777 EXPORT_SYMBOL(lfsck_get_speed);
2778
2779 int lfsck_set_speed(struct dt_device *key, __u32 val)
2780 {
2781         struct lu_env           env;
2782         struct lfsck_instance  *lfsck;
2783         int                     rc;
2784         ENTRY;
2785
2786         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2787         if (rc != 0)
2788                 RETURN(rc);
2789
2790         lfsck = lfsck_instance_find(key, true, false);
2791         if (likely(lfsck != NULL)) {
2792                 mutex_lock(&lfsck->li_mutex);
2793                 if (__lfsck_set_speed(lfsck, val))
2794                         rc = lfsck_bookmark_store(&env, lfsck);
2795                 mutex_unlock(&lfsck->li_mutex);
2796                 lfsck_instance_put(&env, lfsck);
2797         } else {
2798                 rc = -ENXIO;
2799         }
2800
2801         lu_env_fini(&env);
2802
2803         RETURN(rc);
2804 }
2805 EXPORT_SYMBOL(lfsck_set_speed);
2806
2807 int lfsck_get_windows(char *buf, struct dt_device *key)
2808 {
2809         struct lu_env           env;
2810         struct lfsck_instance  *lfsck;
2811         int                     rc;
2812         ENTRY;
2813
2814         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2815         if (rc != 0)
2816                 RETURN(rc);
2817
2818         lfsck = lfsck_instance_find(key, true, false);
2819         if (likely(lfsck != NULL)) {
2820                 rc = sprintf(buf, "%u\n",
2821                              lfsck->li_bookmark_ram.lb_async_windows);
2822                 lfsck_instance_put(&env, lfsck);
2823         } else {
2824                 rc = -ENXIO;
2825         }
2826
2827         lu_env_fini(&env);
2828
2829         RETURN(rc);
2830 }
2831 EXPORT_SYMBOL(lfsck_get_windows);
2832
2833 int lfsck_set_windows(struct dt_device *key, unsigned int val)
2834 {
2835         struct lu_env           env;
2836         struct lfsck_instance  *lfsck;
2837         int                     rc;
2838         ENTRY;
2839
2840         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2841         if (rc != 0)
2842                 RETURN(rc);
2843
2844         lfsck = lfsck_instance_find(key, true, false);
2845         if (likely(lfsck != NULL)) {
2846                 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2847                         CWARN("%s: invalid async windows size that may "
2848                               "cause memory issues. The valid range is "
2849                               "[1 - %u].\n",
2850                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2851                         rc = -EINVAL;
2852                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2853                         mutex_lock(&lfsck->li_mutex);
2854                         lfsck->li_bookmark_ram.lb_async_windows = val;
2855                         rc = lfsck_bookmark_store(&env, lfsck);
2856                         mutex_unlock(&lfsck->li_mutex);
2857                 }
2858                 lfsck_instance_put(&env, lfsck);
2859         } else {
2860                 rc = -ENXIO;
2861         }
2862
2863         lu_env_fini(&env);
2864
2865         RETURN(rc);
2866 }
2867 EXPORT_SYMBOL(lfsck_set_windows);
2868
2869 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2870 {
2871         struct lu_env           env;
2872         struct lfsck_instance  *lfsck;
2873         struct lfsck_component *com;
2874         int                     rc;
2875         ENTRY;
2876
2877         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2878         if (rc != 0)
2879                 RETURN(rc);
2880
2881         lfsck = lfsck_instance_find(key, true, false);
2882         if (likely(lfsck != NULL)) {
2883                 com = lfsck_component_find(lfsck, type);
2884                 if (likely(com != NULL)) {
2885                         com->lc_ops->lfsck_dump(&env, com, m);
2886                         lfsck_component_put(&env, com);
2887                 } else {
2888                         rc = -ENOTSUPP;
2889                 }
2890
2891                 lfsck_instance_put(&env, lfsck);
2892         } else {
2893                 rc = -ENXIO;
2894         }
2895
2896         lu_env_fini(&env);
2897
2898         RETURN(rc);
2899 }
2900 EXPORT_SYMBOL(lfsck_dump);
2901
2902 static int lfsck_stop_all(const struct lu_env *env,
2903                           struct lfsck_instance *lfsck,
2904                           struct lfsck_stop *stop)
2905 {
2906         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2907         struct lfsck_request              *lr     = &info->lti_lr;
2908         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2909         struct ptlrpc_request_set         *set;
2910         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2911         struct lfsck_tgt_desc             *ltd;
2912         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2913         __u32                              idx;
2914         int                                rc     = 0;
2915         int                                rc1    = 0;
2916         ENTRY;
2917
2918         LASSERT(stop->ls_flags & LPF_BROADCAST);
2919
2920         set = ptlrpc_prep_set();
2921         if (unlikely(set == NULL))
2922                 RETURN(-ENOMEM);
2923
2924         memset(lr, 0, sizeof(*lr));
2925         lr->lr_event = LE_STOP;
2926         lr->lr_index = lfsck_dev_idx(lfsck);
2927         lr->lr_status = stop->ls_status;
2928         lr->lr_version = bk->lb_version;
2929         lr->lr_active = LFSCK_TYPES_ALL;
2930         lr->lr_param = stop->ls_flags;
2931
2932         memset(laia, 0, sizeof(*laia));
2933         laia->laia_ltds = ltds;
2934         laia->laia_lr = lr;
2935         laia->laia_shared = 1;
2936
2937         down_read(&ltds->ltd_rw_sem);
2938         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2939                 ltd = lfsck_tgt_get(ltds, idx);
2940                 LASSERT(ltd != NULL);
2941
2942                 laia->laia_ltd = ltd;
2943                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2944                                          lfsck_async_interpret, laia,
2945                                          LFSCK_NOTIFY);
2946                 if (rc != 0) {
2947                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2948                         lfsck_tgt_put(ltd);
2949                         CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2950                                "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2951                         rc1 = rc;
2952                 }
2953         }
2954         up_read(&ltds->ltd_rw_sem);
2955
2956         rc = ptlrpc_set_wait(env, set);
2957         ptlrpc_set_destroy(set);
2958
2959         if (rc == 0)
2960                 rc = laia->laia_result;
2961
2962         if (rc == -EALREADY)
2963                 rc = 0;
2964
2965         if (rc != 0)
2966                 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2967                        lfsck_lfsck2name(lfsck), rc);
2968
2969         RETURN(rc != 0 ? rc : rc1);
2970 }
2971
2972 static int lfsck_start_all(const struct lu_env *env,
2973                            struct lfsck_instance *lfsck,
2974                            struct lfsck_start *start)
2975 {
2976         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2977         struct lfsck_request              *lr     = &info->lti_lr;
2978         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2979         struct ptlrpc_request_set         *set;
2980         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2981         struct lfsck_tgt_desc             *ltd;
2982         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2983         __u32                              idx;
2984         int                                rc     = 0;
2985         bool retry = false;
2986         ENTRY;
2987
2988         LASSERT(start->ls_flags & LPF_BROADCAST);
2989
2990         memset(lr, 0, sizeof(*lr));
2991         lr->lr_event = LE_START;
2992         lr->lr_index = lfsck_dev_idx(lfsck);
2993         lr->lr_speed = bk->lb_speed_limit;
2994         lr->lr_version = bk->lb_version;
2995         lr->lr_active = start->ls_active;
2996         lr->lr_param = start->ls_flags;
2997         lr->lr_async_windows = bk->lb_async_windows;
2998         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2999                        LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
3000                        LSV_CREATE_MDTOBJ;
3001
3002         memset(laia, 0, sizeof(*laia));
3003         laia->laia_ltds = ltds;
3004         laia->laia_lr = lr;
3005         laia->laia_shared = 1;
3006
3007 again:
3008         set = ptlrpc_prep_set();
3009         if (unlikely(!set))
3010                 RETURN(-ENOMEM);
3011
3012         down_read(&ltds->ltd_rw_sem);
3013         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3014                 ltd = lfsck_tgt_get(ltds, idx);
3015                 LASSERT(ltd != NULL);
3016
3017                 if (retry && !ltd->ltd_retry_start) {
3018                         lfsck_tgt_put(ltd);
3019                         continue;
3020                 }
3021
3022                 laia->laia_ltd = ltd;
3023                 ltd->ltd_retry_start = 0;
3024                 ltd->ltd_layout_done = 0;
3025                 ltd->ltd_namespace_done = 0;
3026                 ltd->ltd_synced_failures = 0;
3027                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3028                                          lfsck_async_interpret, laia,
3029                                          LFSCK_NOTIFY);
3030                 if (rc != 0) {
3031                         lfsck_interpret(env, lfsck, NULL, laia, rc);
3032                         lfsck_tgt_put(ltd);
3033                         CERROR("%s: cannot notify MDT %x for LFSCK "
3034                                "start, failout: rc = %d\n",
3035                                lfsck_lfsck2name(lfsck), idx, rc);
3036                         break;
3037                 }
3038         }
3039         up_read(&ltds->ltd_rw_sem);
3040
3041         if (rc != 0) {
3042                 ptlrpc_set_destroy(set);
3043
3044                 RETURN(rc);
3045         }
3046
3047         rc = ptlrpc_set_wait(env, set);
3048         ptlrpc_set_destroy(set);
3049
3050         if (rc == 0)
3051                 rc = laia->laia_result;
3052
3053         if (unlikely(rc == -EINPROGRESS)) {
3054                 retry = true;
3055                 schedule_timeout_interruptible(cfs_time_seconds(1));
3056                 set_current_state(TASK_RUNNING);
3057                 if (!signal_pending(current) &&
3058                     thread_is_running(&lfsck->li_thread))
3059                         goto again;
3060
3061                 rc = -EINTR;
3062         }
3063
3064         if (rc != 0) {
3065                 struct lfsck_stop *stop = &info->lti_stop;
3066
3067                 CERROR("%s: cannot start LFSCK on some MDTs, "
3068                        "stop all: rc = %d\n",
3069                        lfsck_lfsck2name(lfsck), rc);
3070                 if (rc != -EALREADY) {
3071                         stop->ls_status = LS_FAILED;
3072                         stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3073                         lfsck_stop_all(env, lfsck, stop);
3074                 }
3075         }
3076
3077         RETURN(rc);
3078 }
3079
3080 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3081                 struct lfsck_start_param *lsp)
3082 {
3083         struct lfsck_start              *start  = lsp->lsp_start;
3084         struct lfsck_instance           *lfsck;
3085         struct lfsck_bookmark           *bk;
3086         struct ptlrpc_thread            *thread;
3087         struct lfsck_component          *com;
3088         struct lfsck_thread_args        *lta;
3089         struct task_struct              *task;
3090         struct lfsck_tgt_descs          *ltds;
3091         struct lfsck_tgt_desc           *ltd;
3092         __u32                            idx;
3093         int                              rc     = 0;
3094         __u16                            valid  = 0;
3095         __u16                            flags  = 0;
3096         __u16                            type   = 1;
3097         ENTRY;
3098
3099         if (key->dd_rdonly)
3100                 RETURN(-EROFS);
3101
3102         lfsck = lfsck_instance_find(key, true, false);
3103         if (unlikely(lfsck == NULL))
3104                 RETURN(-ENXIO);
3105
3106         if (unlikely(lfsck->li_stopping))
3107                 GOTO(put, rc = -ENXIO);
3108
3109         /* System is not ready, try again later. */
3110         if (unlikely(lfsck->li_namespace == NULL ||
3111                      lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3112                 GOTO(put, rc = -EINPROGRESS);
3113
3114         /* start == NULL means auto trigger paused LFSCK. */
3115         if (!start) {
3116                 if (list_empty(&lfsck->li_list_scan) ||
3117                     OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3118                         GOTO(put, rc = 0);
3119         } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3120                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3121                        lfsck_lfsck2name(lfsck));
3122
3123                 GOTO(put, rc = -EPERM);
3124         }
3125
3126         bk = &lfsck->li_bookmark_ram;
3127         thread = &lfsck->li_thread;
3128         mutex_lock(&lfsck->li_mutex);
3129         spin_lock(&lfsck->li_lock);
3130         if (unlikely(thread_is_stopping(thread))) {
3131                 /* Someone is stopping the LFSCK. */
3132                 spin_unlock(&lfsck->li_lock);
3133                 GOTO(out, rc = -EBUSY);
3134         }
3135
3136         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3137                 rc = -EALREADY;
3138                 if (unlikely(start == NULL)) {
3139                         spin_unlock(&lfsck->li_lock);
3140                         GOTO(out, rc);
3141                 }
3142
3143                 while (start->ls_active != 0) {
3144                         if (!(type & start->ls_active)) {
3145                                 type <<= 1;
3146                                 continue;
3147                         }
3148
3149                         com = __lfsck_component_find(lfsck, type,
3150                                                      &lfsck->li_list_scan);
3151                         if (com == NULL)
3152                                 com = __lfsck_component_find(lfsck, type,
3153                                                 &lfsck->li_list_double_scan);
3154                         if (com == NULL) {
3155                                 rc = -EOPNOTSUPP;
3156                                 break;
3157                         }
3158
3159                         if (com->lc_ops->lfsck_join != NULL) {
3160                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
3161                                 if (rc != 0 && rc != -EALREADY)
3162                                         break;
3163                         }
3164                         start->ls_active &= ~type;
3165                         type <<= 1;
3166                 }
3167                 spin_unlock(&lfsck->li_lock);
3168                 GOTO(out, rc);
3169         }
3170         spin_unlock(&lfsck->li_lock);
3171
3172         lfsck->li_status = 0;
3173         lfsck->li_oit_over = 0;
3174         lfsck->li_start_unplug = 0;
3175         lfsck->li_drop_dryrun = 0;
3176         lfsck->li_new_scanned = 0;
3177
3178         /* For auto trigger. */
3179         if (start == NULL)
3180                 goto trigger;
3181
3182         start->ls_version = bk->lb_version;
3183
3184         if (start->ls_active != 0) {
3185                 struct lfsck_component *next;
3186
3187                 if (start->ls_active == LFSCK_TYPES_ALL)
3188                         start->ls_active = LFSCK_TYPES_SUPPORTED;
3189
3190                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3191                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3192                         GOTO(out, rc = -ENOTSUPP);
3193                 }
3194
3195                 list_for_each_entry_safe(com, next,
3196                                          &lfsck->li_list_scan, lc_link) {
3197                         if (!(com->lc_type & start->ls_active)) {
3198                                 rc = com->lc_ops->lfsck_post(env, com, 0,
3199                                                              false);
3200                                 if (rc != 0)
3201                                         GOTO(out, rc);
3202                         }
3203                 }
3204
3205                 while (start->ls_active != 0) {
3206                         if (type & start->ls_active) {
3207                                 com = __lfsck_component_find(lfsck, type,