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