Whamcloud - gitweb
LU-9019 libcfs: avoid using HZ and msecs_to_jiffies()
[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 <lu_object.h>
37 #include <dt_object.h>
38 #include <md_object.h>
39 #include <lustre_fld.h>
40 #include <lustre_lib.h>
41 #include <lustre_net.h>
42 #include <lustre_lfsck.h>
43
44 #include "lfsck_internal.h"
45
46 #define LFSCK_CHECKPOINT_SKIP   1
47
48 /* define lfsck thread key */
49 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
50
51 static void lfsck_key_fini(const struct lu_context *ctx,
52                            struct lu_context_key *key, void *data)
53 {
54         struct lfsck_thread_info *info = data;
55
56         lu_buf_free(&info->lti_linkea_buf);
57         lu_buf_free(&info->lti_linkea_buf2);
58         lu_buf_free(&info->lti_big_buf);
59         OBD_FREE_PTR(info);
60 }
61
62 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
63 LU_KEY_INIT_GENERIC(lfsck);
64
65 static struct list_head lfsck_instance_list;
66 static struct list_head lfsck_ost_orphan_list;
67 static struct list_head lfsck_mdt_orphan_list;
68 static DEFINE_SPINLOCK(lfsck_instance_lock);
69
70 const char *lfsck_flags_names[] = {
71         "scanned-once",
72         "inconsistent",
73         "upgrade",
74         "incomplete",
75         "crashed_lastid",
76         NULL
77 };
78
79 const char *lfsck_param_names[] = {
80         NULL,
81         "failout",
82         "dryrun",
83         "all_targets",
84         "broadcast",
85         "orphan",
86         "create_ostobj",
87         "create_mdtobj",
88         NULL,
89         "delay_create_ostobj",
90         NULL
91 };
92
93 enum lfsck_verify_lpf_types {
94         LVLT_BY_BOOKMARK        = 0,
95         LVLT_BY_NAMEENTRY       = 1,
96 };
97
98 static inline void
99 lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
100 {
101         if (type == LFSCK_TYPE_LAYOUT) {
102                 ltd->ltd_layout_status = LS_MAX;
103                 ltd->ltd_layout_repaired = 0;
104         } else {
105                 ltd->ltd_namespace_status = LS_MAX;
106                 ltd->ltd_namespace_repaired = 0;
107         }
108 }
109
110 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
111 {
112         spin_lock_init(&ltds->ltd_lock);
113         init_rwsem(&ltds->ltd_rw_sem);
114         INIT_LIST_HEAD(&ltds->ltd_orphan);
115         ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
116         if (ltds->ltd_tgts_bitmap == NULL)
117                 return -ENOMEM;
118
119         return 0;
120 }
121
122 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
123 {
124         struct lfsck_tgt_desc   *ltd;
125         struct lfsck_tgt_desc   *next;
126         int                      idx;
127
128         down_write(&ltds->ltd_rw_sem);
129
130         list_for_each_entry_safe(ltd, next, &ltds->ltd_orphan,
131                                  ltd_orphan_list) {
132                 list_del_init(&ltd->ltd_orphan_list);
133                 lfsck_tgt_put(ltd);
134         }
135
136         if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
137                 up_write(&ltds->ltd_rw_sem);
138
139                 return;
140         }
141
142         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
143                 ltd = lfsck_ltd2tgt(ltds, idx);
144                 if (likely(ltd != NULL)) {
145                         LASSERT(list_empty(&ltd->ltd_layout_list));
146                         LASSERT(list_empty(&ltd->ltd_layout_phase_list));
147                         LASSERT(list_empty(&ltd->ltd_namespace_list));
148                         LASSERT(list_empty(&ltd->ltd_namespace_phase_list));
149
150                         ltds->ltd_tgtnr--;
151                         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
152                         lfsck_assign_tgt(ltds, NULL, idx);
153                         lfsck_tgt_put(ltd);
154                 }
155         }
156
157         LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
158                  ltds->ltd_tgtnr);
159
160         for (idx = 0; idx < TGT_PTRS; idx++) {
161                 if (ltds->ltd_tgts_idx[idx] != NULL) {
162                         OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
163                         ltds->ltd_tgts_idx[idx] = NULL;
164                 }
165         }
166
167         CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
168         ltds->ltd_tgts_bitmap = NULL;
169         up_write(&ltds->ltd_rw_sem);
170 }
171
172 static int __lfsck_add_target(const struct lu_env *env,
173                               struct lfsck_instance *lfsck,
174                               struct lfsck_tgt_desc *ltd,
175                               bool for_ost, bool locked)
176 {
177         struct lfsck_tgt_descs *ltds;
178         __u32                   index = ltd->ltd_index;
179         int                     rc    = 0;
180         ENTRY;
181
182         if (for_ost)
183                 ltds = &lfsck->li_ost_descs;
184         else
185                 ltds = &lfsck->li_mdt_descs;
186
187         if (!locked)
188                 down_write(&ltds->ltd_rw_sem);
189
190         LASSERT(ltds->ltd_tgts_bitmap != NULL);
191
192         if (index >= ltds->ltd_tgts_bitmap->size) {
193                 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
194                                     (__u32)BITS_PER_LONG);
195                 struct cfs_bitmap *old_bitmap = ltds->ltd_tgts_bitmap;
196                 struct cfs_bitmap *new_bitmap;
197
198                 while (newsize < index + 1)
199                         newsize <<= 1;
200
201                 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
202                 if (new_bitmap == NULL)
203                         GOTO(unlock, rc = -ENOMEM);
204
205                 if (ltds->ltd_tgtnr > 0)
206                         cfs_bitmap_copy(new_bitmap, old_bitmap);
207                 ltds->ltd_tgts_bitmap = new_bitmap;
208                 CFS_FREE_BITMAP(old_bitmap);
209         }
210
211         if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
212                 CERROR("%s: the device %s (%u) is registered already\n",
213                        lfsck_lfsck2name(lfsck),
214                        ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
215                 GOTO(unlock, rc = -EEXIST);
216         }
217
218         if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
219                 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
220                 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
221                         GOTO(unlock, rc = -ENOMEM);
222         }
223
224         lfsck_assign_tgt(ltds, ltd, index);
225         cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
226         ltds->ltd_tgtnr++;
227
228         GOTO(unlock, rc = 0);
229
230 unlock:
231         if (!locked)
232                 up_write(&ltds->ltd_rw_sem);
233
234         return rc;
235 }
236
237 static int lfsck_add_target_from_orphan(const struct lu_env *env,
238                                         struct lfsck_instance *lfsck)
239 {
240         struct lfsck_tgt_descs  *ltds    = &lfsck->li_ost_descs;
241         struct lfsck_tgt_desc   *ltd;
242         struct lfsck_tgt_desc   *next;
243         struct list_head        *head    = &lfsck_ost_orphan_list;
244         int                      rc;
245         bool                     for_ost = true;
246
247 again:
248         spin_lock(&lfsck_instance_lock);
249         list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
250                 if (ltd->ltd_key == lfsck->li_bottom)
251                         list_move_tail(&ltd->ltd_orphan_list,
252                                        &ltds->ltd_orphan);
253         }
254         spin_unlock(&lfsck_instance_lock);
255
256         down_write(&ltds->ltd_rw_sem);
257         while (!list_empty(&ltds->ltd_orphan)) {
258                 ltd = list_entry(ltds->ltd_orphan.next,
259                                  struct lfsck_tgt_desc,
260                                  ltd_orphan_list);
261                 list_del_init(&ltd->ltd_orphan_list);
262                 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
263                 /* Do not hold the semaphore for too long time. */
264                 up_write(&ltds->ltd_rw_sem);
265                 if (rc != 0)
266                         return rc;
267
268                 down_write(&ltds->ltd_rw_sem);
269         }
270         up_write(&ltds->ltd_rw_sem);
271
272         if (for_ost) {
273                 ltds = &lfsck->li_mdt_descs;
274                 head = &lfsck_mdt_orphan_list;
275                 for_ost = false;
276                 goto again;
277         }
278
279         return 0;
280 }
281
282 static inline struct lfsck_component *
283 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
284                        struct list_head *list)
285 {
286         struct lfsck_component *com;
287
288         list_for_each_entry(com, list, lc_link) {
289                 if (com->lc_type == type)
290                         return com;
291         }
292         return NULL;
293 }
294
295 struct lfsck_component *
296 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
297 {
298         struct lfsck_component *com;
299
300         spin_lock(&lfsck->li_lock);
301         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
302         if (com != NULL)
303                 goto unlock;
304
305         com = __lfsck_component_find(lfsck, type,
306                                      &lfsck->li_list_double_scan);
307         if (com != NULL)
308                 goto unlock;
309
310         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
311
312 unlock:
313         if (com != NULL)
314                 lfsck_component_get(com);
315         spin_unlock(&lfsck->li_lock);
316         return com;
317 }
318
319 void lfsck_component_cleanup(const struct lu_env *env,
320                              struct lfsck_component *com)
321 {
322         if (!list_empty(&com->lc_link))
323                 list_del_init(&com->lc_link);
324         if (!list_empty(&com->lc_link_dir))
325                 list_del_init(&com->lc_link_dir);
326
327         lfsck_component_put(env, com);
328 }
329
330 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
331                     struct lu_fid *fid, bool locked)
332 {
333         struct lfsck_bookmark   *bk = &lfsck->li_bookmark_ram;
334         int                      rc = 0;
335         ENTRY;
336
337         if (!locked)
338                 mutex_lock(&lfsck->li_mutex);
339
340         rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
341         if (rc >= 0) {
342                 bk->lb_last_fid = *fid;
343                 /* We do not care about whether the subsequent sub-operations
344                  * failed or not. The worst case is that one FID is lost that
345                  * is not a big issue for the LFSCK since it is relative rare
346                  * for LFSCK create. */
347                 rc = lfsck_bookmark_store(env, lfsck);
348         }
349
350         if (!locked)
351                 mutex_unlock(&lfsck->li_mutex);
352
353         RETURN(rc);
354 }
355
356 static int __lfsck_ibits_lock(const struct lu_env *env,
357                               struct lfsck_instance *lfsck,
358                               struct dt_object *obj, struct ldlm_res_id *resid,
359                               struct lustre_handle *lh, __u64 bits,
360                               enum ldlm_mode mode)
361 {
362         struct lfsck_thread_info        *info   = lfsck_env_info(env);
363         union ldlm_policy_data          *policy = &info->lti_policy;
364         __u64                            flags  = LDLM_FL_ATOMIC_CB;
365         int                              rc;
366
367         LASSERT(lfsck->li_namespace != NULL);
368
369         memset(policy, 0, sizeof(*policy));
370         policy->l_inodebits.bits = bits;
371         if (dt_object_remote(obj)) {
372                 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
373
374                 memset(einfo, 0, sizeof(*einfo));
375                 einfo->ei_type = LDLM_IBITS;
376                 einfo->ei_mode = mode;
377                 einfo->ei_cb_bl = ldlm_blocking_ast;
378                 einfo->ei_cb_cp = ldlm_completion_ast;
379                 einfo->ei_res_id = resid;
380
381                 rc = dt_object_lock(env, obj, lh, einfo, policy);
382                 /* for regular checks LFSCK doesn't use LDLM locking,
383                  * so the state isn't coherent. here we just took LDLM
384                  * lock for coherency and it's time to invalidate
385                  * previous state */
386                 if (rc == ELDLM_OK)
387                         dt_invalidate(env, obj);
388         } else {
389                 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
390                                             LDLM_IBITS, policy, mode,
391                                             &flags, ldlm_blocking_ast,
392                                             ldlm_completion_ast, NULL, NULL,
393                                             0, LVB_T_NONE, NULL, lh);
394         }
395
396         if (rc == ELDLM_OK) {
397                 rc = 0;
398         } else {
399                 memset(lh, 0, sizeof(*lh));
400                 rc = -EIO;
401         }
402
403         return rc;
404 }
405
406 /**
407  * Request the specified ibits lock for the given object.
408  *
409  * Before the LFSCK modifying on the namespace visible object,
410  * it needs to acquire related ibits ldlm lock.
411  *
412  * \param[in] env       pointer to the thread context
413  * \param[in] lfsck     pointer to the lfsck instance
414  * \param[in] obj       pointer to the dt_object to be locked
415  * \param[out] lh       pointer to the lock handle
416  * \param[in] bits      the bits for the ldlm lock to be acquired
417  * \param[in] mode      the mode for the ldlm lock to be acquired
418  *
419  * \retval              0 for success
420  * \retval              negative error number on failure
421  */
422 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
423                      struct dt_object *obj, struct lustre_handle *lh,
424                      __u64 bits, enum ldlm_mode mode)
425 {
426         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
427
428         LASSERT(!lustre_handle_is_used(lh));
429
430         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
431         return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
432 }
433
434 /**
435  * Release the the specified ibits lock.
436  *
437  * If the lock has been acquired before, release it
438  * and cleanup the handle. Otherwise, do nothing.
439  *
440  * \param[in] lh        pointer to the lock handle
441  * \param[in] mode      the mode for the ldlm lock to be released
442  */
443 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
444 {
445         if (lustre_handle_is_used(lh)) {
446                 ldlm_lock_decref(lh, mode);
447                 memset(lh, 0, sizeof(*lh));
448         }
449 }
450
451 /**
452  * Request compound ibits locks for the given <obj, name> pairs.
453  *
454  * Before the LFSCK modifying on the namespace visible object, it needs to
455  * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
456  * the lock purpose. But the simple lfsck_ibits_lock for directory-based
457  * modificationis (such as insert name entry to the directory) may be too
458  * coarse-grained and not efficient.
459  *
460  * The lfsck_lock() will request compound ibits locks on the specified
461  * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
462  * lock on the directory object, and the regular ibits lock on the name hash.
463  *
464  * \param[in] env       pointer to the thread context
465  * \param[in] lfsck     pointer to the lfsck instance
466  * \param[in] obj       pointer to the dt_object to be locked
467  * \param[in] name      used for building the PDO lock resource
468  * \param[out] llh      pointer to the lfsck_lock_handle
469  * \param[in] bits      the bits for the ldlm lock to be acquired
470  * \param[in] mode      the mode for the ldlm lock to be acquired
471  *
472  * \retval              0 for success
473  * \retval              negative error number on failure
474  */
475 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
476                struct dt_object *obj, const char *name,
477                struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
478 {
479         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
480         int                 rc;
481
482         LASSERT(S_ISDIR(lfsck_object_type(obj)));
483         LASSERT(name != NULL);
484         LASSERT(name[0] != 0);
485         LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
486         LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
487
488         switch (mode) {
489         case LCK_EX:
490                 llh->llh_pdo_mode = LCK_EX;
491                 break;
492         case LCK_PW:
493                 llh->llh_pdo_mode = LCK_CW;
494                 break;
495         case LCK_PR:
496                 llh->llh_pdo_mode = LCK_CR;
497                 break;
498         default:
499                 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
500                        DFID"\n", lfsck_lfsck2name(lfsck), mode,
501                        PFID(lfsck_dto2fid(obj)));
502                 LBUG();
503         }
504
505         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
506         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
507                                 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
508         if (rc != 0)
509                 return rc;
510
511         llh->llh_reg_mode = mode;
512         resid->name[LUSTRE_RES_ID_HSH_OFF] = ll_full_name_hash(NULL, name,
513                                                                strlen(name));
514         LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
515         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
516                                 bits, llh->llh_reg_mode);
517         if (rc != 0)
518                 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
519
520         return rc;
521 }
522
523 /**
524  * Release the the compound ibits locks.
525  *
526  * \param[in] llh       pointer to the lfsck_lock_handle to be released
527  */
528 void lfsck_unlock(struct lfsck_lock_handle *llh)
529 {
530         lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
531         lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
532 }
533
534 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
535                               struct lfsck_instance *lfsck,
536                               const struct lu_fid *fid)
537 {
538         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
539         struct lu_seq_range     *range  = &lfsck_env_info(env)->lti_range;
540         int                      rc;
541
542         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
543                 /* "ROOT" is always on the MDT0. */
544                 if (lu_fid_eq(fid, &lfsck->li_global_root_fid))
545                         return 0;
546
547                 return lfsck_dev_idx(lfsck);
548         }
549
550         fld_range_set_mdt(range);
551         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
552         if (rc == 0)
553                 rc = range->lsr_index;
554
555         return rc;
556 }
557
558 const char dot[] = ".";
559 const char dotdot[] = "..";
560 static const char dotlustre[] = ".lustre";
561 static const char lostfound[] = "lost+found";
562
563 /**
564  * Remove the name entry from the .lustre/lost+found directory.
565  *
566  * No need to care about the object referenced by the name entry,
567  * either the name entry is invalid or redundant, or the referenced
568  * object has been processed or will be handled by others.
569  *
570  * \param[in] env       pointer to the thread context
571  * \param[in] lfsck     pointer to the lfsck instance
572  * \param[in] name      the name for the name entry to be removed
573  *
574  * \retval              0 for success
575  * \retval              negative error number on failure
576  */
577 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
578                                        struct lfsck_instance *lfsck,
579                                        const char *name)
580 {
581         struct dt_object        *parent = lfsck->li_lpf_root_obj;
582         struct dt_device        *dev    = lfsck_obj2dev(parent);
583         struct thandle          *th;
584         struct lfsck_lock_handle *llh   = &lfsck_env_info(env)->lti_llh;
585         int                      rc;
586         ENTRY;
587
588         rc = lfsck_lock(env, lfsck, parent, name, llh,
589                         MDS_INODELOCK_UPDATE, LCK_PW);
590         if (rc != 0)
591                 RETURN(rc);
592
593         th = dt_trans_create(env, dev);
594         if (IS_ERR(th))
595                 GOTO(unlock, rc = PTR_ERR(th));
596
597         rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
598         if (rc != 0)
599                 GOTO(stop, rc);
600
601         rc = dt_declare_ref_del(env, parent, th);
602         if (rc != 0)
603                 GOTO(stop, rc);
604
605         rc = dt_trans_start_local(env, dev, th);
606         if (rc != 0)
607                 GOTO(stop, rc);
608
609         rc = dt_delete(env, parent, (const struct dt_key *)name, th);
610         if (rc != 0)
611                 GOTO(stop, rc);
612
613         dt_write_lock(env, parent, 0);
614         rc = dt_ref_del(env, parent, th);
615         dt_write_unlock(env, parent);
616
617         GOTO(stop, rc);
618
619 stop:
620         dt_trans_stop(env, dev, th);
621
622 unlock:
623         lfsck_unlock(llh);
624
625         CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
626                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
627
628         return rc;
629 }
630
631 static int lfsck_create_lpf_local(const struct lu_env *env,
632                                   struct lfsck_instance *lfsck,
633                                   struct dt_object *child,
634                                   struct lu_attr *la,
635                                   struct dt_object_format *dof,
636                                   const char *name)
637 {
638         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
639         struct dt_object        *parent = lfsck->li_lpf_root_obj;
640         struct dt_device        *dev    = lfsck_obj2dev(child);
641         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
642         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
643         const struct lu_fid     *cfid   = lfsck_dto2fid(child);
644         struct thandle          *th     = NULL;
645         struct linkea_data       ldata  = { NULL };
646         struct lu_buf            linkea_buf;
647         const struct lu_name    *cname;
648         loff_t                   pos    = 0;
649         int                      len    = sizeof(struct lfsck_bookmark);
650         int                      rc;
651         ENTRY;
652
653         cname = lfsck_name_get_const(env, name, strlen(name));
654         rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
655                               cname, lfsck_dto2fid(parent));
656         if (rc != 0)
657                 RETURN(rc);
658
659         th = dt_trans_create(env, dev);
660         if (IS_ERR(th))
661                 RETURN(PTR_ERR(th));
662
663         /* 1a. create child */
664         rc = dt_declare_create(env, child, la, NULL, dof, th);
665         if (rc != 0)
666                 GOTO(stop, rc);
667
668         if (!dt_try_as_dir(env, child))
669                 GOTO(stop, rc = -ENOTDIR);
670
671         /* 2a. increase child nlink */
672         rc = dt_declare_ref_add(env, child, th);
673         if (rc != 0)
674                 GOTO(stop, rc);
675
676         /* 3a. insert dot into child dir */
677         rec->rec_type = S_IFDIR;
678         rec->rec_fid = cfid;
679         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
680                                (const struct dt_key *)dot, th);
681         if (rc != 0)
682                 GOTO(stop, rc);
683
684         /* 4a. insert dotdot into child dir */
685         rec->rec_fid = &LU_LPF_FID;
686         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
687                                (const struct dt_key *)dotdot, th);
688         if (rc != 0)
689                 GOTO(stop, rc);
690
691         /* 5a. insert linkEA for child */
692         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
693                        ldata.ld_leh->leh_len);
694         rc = dt_declare_xattr_set(env, child, &linkea_buf,
695                                   XATTR_NAME_LINK, 0, th);
696         if (rc != 0)
697                 GOTO(stop, rc);
698
699         /* 6a. insert name into parent dir */
700         rec->rec_type = S_IFDIR;
701         rec->rec_fid = cfid;
702         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
703                                (const struct dt_key *)name, th);
704         if (rc != 0)
705                 GOTO(stop, rc);
706
707         /* 7a. increase parent nlink */
708         rc = dt_declare_ref_add(env, parent, th);
709         if (rc != 0)
710                 GOTO(stop, rc);
711
712         /* 8a. update bookmark */
713         rc = dt_declare_record_write(env, bk_obj,
714                                      lfsck_buf_get(env, bk, len), 0, th);
715         if (rc != 0)
716                 GOTO(stop, rc);
717
718         rc = dt_trans_start_local(env, dev, th);
719         if (rc != 0)
720                 GOTO(stop, rc);
721
722         dt_write_lock(env, child, 0);
723         /* 1b. create child */
724         rc = dt_create(env, child, la, NULL, dof, th);
725         if (rc != 0)
726                 GOTO(unlock, rc);
727
728         /* 2b. increase child nlink */
729         rc = dt_ref_add(env, child, th);
730         if (rc != 0)
731                 GOTO(unlock, rc);
732
733         /* 3b. insert dot into child dir */
734         rec->rec_fid = cfid;
735         rc = dt_insert(env, child, (const struct dt_rec *)rec,
736                        (const struct dt_key *)dot, th);
737         if (rc != 0)
738                 GOTO(unlock, rc);
739
740         /* 4b. insert dotdot into child dir */
741         rec->rec_fid = &LU_LPF_FID;
742         rc = dt_insert(env, child, (const struct dt_rec *)rec,
743                        (const struct dt_key *)dotdot, th);
744         if (rc != 0)
745                 GOTO(unlock, rc);
746
747         /* 5b. insert linkEA for child. */
748         rc = dt_xattr_set(env, child, &linkea_buf,
749                           XATTR_NAME_LINK, 0, th);
750         dt_write_unlock(env, child);
751         if (rc != 0)
752                 GOTO(stop, rc);
753
754         /* 6b. insert name into parent dir */
755         rec->rec_fid = cfid;
756         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
757                        (const struct dt_key *)name, th);
758         if (rc != 0)
759                 GOTO(stop, rc);
760
761         dt_write_lock(env, parent, 0);
762         /* 7b. increase parent nlink */
763         rc = dt_ref_add(env, parent, th);
764         dt_write_unlock(env, parent);
765         if (rc != 0)
766                 GOTO(stop, rc);
767
768         bk->lb_lpf_fid = *cfid;
769         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
770
771         /* 8b. update bookmark */
772         rc = dt_record_write(env, bk_obj,
773                              lfsck_buf_get(env, bk, len), &pos, th);
774
775         GOTO(stop, rc);
776
777 unlock:
778         dt_write_unlock(env, child);
779
780 stop:
781         dt_trans_stop(env, dev, th);
782
783         return rc;
784 }
785
786 static int lfsck_create_lpf_remote(const struct lu_env *env,
787                                    struct lfsck_instance *lfsck,
788                                    struct dt_object *child,
789                                    struct lu_attr *la,
790                                    struct dt_object_format *dof,
791                                    const char *name)
792 {
793         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
794         struct dt_object        *parent = lfsck->li_lpf_root_obj;
795         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
796         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
797         const struct lu_fid     *cfid   = lfsck_dto2fid(child);
798         struct thandle          *th     = NULL;
799         struct linkea_data       ldata  = { NULL };
800         struct lu_buf            linkea_buf;
801         const struct lu_name    *cname;
802         struct dt_device        *dev;
803         loff_t                   pos    = 0;
804         int                      len    = sizeof(struct lfsck_bookmark);
805         int                      rc;
806         ENTRY;
807
808         cname = lfsck_name_get_const(env, name, strlen(name));
809         rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
810                               cname, lfsck_dto2fid(parent));
811         if (rc != 0)
812                 RETURN(rc);
813
814         /* Create .lustre/lost+found/MDTxxxx. */
815
816         /* XXX: Currently, cross-MDT create operation needs to create the child
817          *      object firstly, then insert name into the parent directory. For
818          *      this case, the child object resides on current MDT (local), but
819          *      the parent ".lustre/lost+found" may be on remote MDT. It is not
820          *      easy to contain all the sub-modifications orderly within single
821          *      transaction.
822          *
823          *      To avoid more inconsistency, we split the create operation into
824          *      two transactions:
825          *
826          *      1) create the child and update the lfsck_bookmark::lb_lpf_fid
827          *         locally.
828          *      2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
829          *         remotely.
830          *
831          *      If 1) done, but 2) failed, then go ahead, the LFSCK will try to
832          *      repair such inconsistency when LFSCK run next time. */
833
834         /* Transaction I: locally */
835
836         dev = lfsck_obj2dev(child);
837         th = dt_trans_create(env, dev);
838         if (IS_ERR(th))
839                 RETURN(PTR_ERR(th));
840
841         /* 1a. create child */
842         rc = dt_declare_create(env, child, la, NULL, dof, th);
843         if (rc != 0)
844                 GOTO(stop, rc);
845
846         if (!dt_try_as_dir(env, child))
847                 GOTO(stop, rc = -ENOTDIR);
848
849         /* 2a. increase child nlink */
850         rc = dt_declare_ref_add(env, child, th);
851         if (rc != 0)
852                 GOTO(stop, rc);
853
854         /* 3a. insert dot into child dir */
855         rec->rec_type = S_IFDIR;
856         rec->rec_fid = cfid;
857         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
858                                (const struct dt_key *)dot, th);
859         if (rc != 0)
860                 GOTO(stop, rc);
861
862         /* 4a. insert dotdot into child dir */
863         rec->rec_fid = &LU_LPF_FID;
864         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
865                                (const struct dt_key *)dotdot, th);
866         if (rc != 0)
867                 GOTO(stop, rc);
868
869         /* 5a. insert linkEA for child */
870         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
871                        ldata.ld_leh->leh_len);
872         rc = dt_declare_xattr_set(env, child, &linkea_buf,
873                                   XATTR_NAME_LINK, 0, th);
874         if (rc != 0)
875                 GOTO(stop, rc);
876
877         /* 6a. update bookmark */
878         rc = dt_declare_record_write(env, bk_obj,
879                                      lfsck_buf_get(env, bk, len), 0, th);
880         if (rc != 0)
881                 GOTO(stop, rc);
882
883         rc = dt_trans_start_local(env, dev, th);
884         if (rc != 0)
885                 GOTO(stop, rc);
886
887         dt_write_lock(env, child, 0);
888         /* 1b. create child */
889         rc = dt_create(env, child, la, NULL, dof, th);
890         if (rc != 0)
891                 GOTO(unlock, rc);
892
893         /* 2b. increase child nlink */
894         rc = dt_ref_add(env, child, th);
895         if (rc != 0)
896                 GOTO(unlock, rc);
897
898         /* 3b. insert dot into child dir */
899         rec->rec_type = S_IFDIR;
900         rec->rec_fid = cfid;
901         rc = dt_insert(env, child, (const struct dt_rec *)rec,
902                        (const struct dt_key *)dot, th);
903         if (rc != 0)
904                 GOTO(unlock, rc);
905
906         /* 4b. insert dotdot into child dir */
907         rec->rec_fid = &LU_LPF_FID;
908         rc = dt_insert(env, child, (const struct dt_rec *)rec,
909                        (const struct dt_key *)dotdot, th);
910         if (rc != 0)
911                 GOTO(unlock, rc);
912
913         /* 5b. insert linkEA for child */
914         rc = dt_xattr_set(env, child, &linkea_buf,
915                           XATTR_NAME_LINK, 0, th);
916         if (rc != 0)
917                 GOTO(unlock, rc);
918
919         bk->lb_lpf_fid = *cfid;
920         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
921
922         /* 6b. update bookmark */
923         rc = dt_record_write(env, bk_obj,
924                              lfsck_buf_get(env, bk, len), &pos, th);
925
926         dt_write_unlock(env, child);
927         dt_trans_stop(env, dev, th);
928         if (rc != 0)
929                 RETURN(rc);
930
931         /* Transaction II: remotely */
932
933         dev = lfsck_obj2dev(parent);
934         th = dt_trans_create(env, dev);
935         if (IS_ERR(th))
936                 RETURN(PTR_ERR(th));
937
938         th->th_sync = 1;
939         /* 5a. insert name into parent dir */
940         rec->rec_fid = cfid;
941         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
942                                (const struct dt_key *)name, th);
943         if (rc != 0)
944                 GOTO(stop, rc);
945
946         /* 6a. increase parent nlink */
947         rc = dt_declare_ref_add(env, parent, th);
948         if (rc != 0)
949                 GOTO(stop, rc);
950
951         rc = dt_trans_start_local(env, dev, th);
952         if (rc != 0)
953                 GOTO(stop, rc);
954
955         /* 5b. insert name into parent dir */
956         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
957                        (const struct dt_key *)name, th);
958         if (rc != 0)
959                 GOTO(stop, rc);
960
961         dt_write_lock(env, parent, 0);
962         /* 6b. increase parent nlink */
963         rc = dt_ref_add(env, parent, th);
964         dt_write_unlock(env, parent);
965
966         GOTO(stop, rc);
967
968 unlock:
969         dt_write_unlock(env, child);
970 stop:
971         dt_trans_stop(env, dev, th);
972
973         if (rc != 0 && dev == lfsck_obj2dev(parent))
974                 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
975                        "for orphans, but failed to insert the name %s "
976                        "to the .lustre/lost+found/. Such inconsistency "
977                        "will be repaired when LFSCK run next time: rc = %d\n",
978                        lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
979
980         return rc;
981 }
982
983 /**
984  * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
985  *
986  * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
987  * orphans and other uncertain inconsistent objects found during the
988  * LFSCK. Such directory will be created by the LFSCK engine on the
989  * local MDT before the LFSCK scanning.
990  *
991  * \param[in] env       pointer to the thread context
992  * \param[in] lfsck     pointer to the lfsck instance
993  *
994  * \retval              0 for success
995  * \retval              negative error number on failure
996  */
997 static int lfsck_create_lpf(const struct lu_env *env,
998                             struct lfsck_instance *lfsck)
999 {
1000         struct lfsck_bookmark    *bk    = &lfsck->li_bookmark_ram;
1001         struct lfsck_thread_info *info  = lfsck_env_info(env);
1002         struct lu_fid            *cfid  = &info->lti_fid2;
1003         struct lu_attr           *la    = &info->lti_la;
1004         struct dt_object_format  *dof   = &info->lti_dof;
1005         struct dt_object         *parent = lfsck->li_lpf_root_obj;
1006         struct dt_object         *child = NULL;
1007         struct lfsck_lock_handle *llh   = &info->lti_llh;
1008         char                      name[8];
1009         int                       node  = lfsck_dev_idx(lfsck);
1010         int                       rc    = 0;
1011         ENTRY;
1012
1013         LASSERT(lfsck->li_master);
1014         LASSERT(parent != NULL);
1015         LASSERT(lfsck->li_lpf_obj == NULL);
1016
1017         snprintf(name, 8, "MDT%04x", node);
1018         rc = lfsck_lock(env, lfsck, parent, name, llh,
1019                         MDS_INODELOCK_UPDATE, LCK_PW);
1020         if (rc != 0)
1021                 RETURN(rc);
1022
1023         if (fid_is_zero(&bk->lb_lpf_fid)) {
1024                 /* There is corner case that: in former LFSCK scanning we have
1025                  * created the .lustre/lost+found/MDTxxxx but failed to update
1026                  * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1027                  * it from MDT0 firstly. */
1028                 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1029                                (const struct dt_key *)name);
1030                 if (rc != 0 && rc != -ENOENT)
1031                         GOTO(unlock, rc);
1032
1033                 if (rc == 0) {
1034                         bk->lb_lpf_fid = *cfid;
1035                         rc = lfsck_bookmark_store(env, lfsck);
1036                 } else {
1037                         rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1038                 }
1039                 if (rc != 0)
1040                         GOTO(unlock, rc);
1041         } else {
1042                 *cfid = bk->lb_lpf_fid;
1043         }
1044
1045         child = lfsck_object_find_bottom_new(env, lfsck, cfid);
1046         if (IS_ERR(child))
1047                 GOTO(unlock, rc = PTR_ERR(child));
1048
1049         if (dt_object_exists(child) != 0) {
1050                 if (unlikely(!dt_try_as_dir(env, child)))
1051                         rc = -ENOTDIR;
1052                 else
1053                         lfsck->li_lpf_obj = child;
1054
1055                 GOTO(unlock, rc);
1056         }
1057
1058         memset(la, 0, sizeof(*la));
1059         la->la_atime = la->la_mtime = la->la_ctime = ktime_get_real_seconds();
1060         la->la_mode = S_IFDIR | S_IRWXU;
1061         la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1062                        LA_UID | LA_GID;
1063         memset(dof, 0, sizeof(*dof));
1064         dof->dof_type = dt_mode_to_dft(S_IFDIR);
1065
1066         if (node == 0)
1067                 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1068         else
1069                 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1070         if (rc == 0)
1071                 lfsck->li_lpf_obj = child;
1072
1073         GOTO(unlock, rc);
1074
1075 unlock:
1076         lfsck_unlock(llh);
1077         if (rc != 0 && child != NULL && !IS_ERR(child))
1078                 lfsck_object_put(env, child);
1079
1080         return rc;
1081 }
1082
1083 /**
1084  * Scan .lustre/lost+found for bad name entries and remove them.
1085  *
1086  * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1087  * index in the system. Any other formatted name is invalid and should be
1088  * removed.
1089  *
1090  * \param[in] env       pointer to the thread context
1091  * \param[in] lfsck     pointer to the lfsck instance
1092  *
1093  * \retval              0 for success
1094  * \retval              negative error number on failure
1095  */
1096 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1097                                       struct lfsck_instance *lfsck)
1098 {
1099         struct dt_object        *parent = lfsck->li_lpf_root_obj;
1100         struct lu_dirent        *ent    =
1101                         (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1102         const struct dt_it_ops  *iops   = &parent->do_index_ops->dio_it;
1103         struct dt_it            *it;
1104         int                      rc;
1105         ENTRY;
1106
1107         it = iops->init(env, parent, LUDA_64BITHASH);
1108         if (IS_ERR(it))
1109                 RETURN(PTR_ERR(it));
1110
1111         rc = iops->load(env, it, 0);
1112         if (rc == 0)
1113                 rc = iops->next(env, it);
1114         else if (rc > 0)
1115                 rc = 0;
1116
1117         while (rc == 0) {
1118                 int off = 3;
1119
1120                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1121                 if (rc != 0)
1122                         break;
1123
1124                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1125                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1126                         goto next;
1127
1128                 /* name length must be strlen("MDTxxxx") */
1129                 if (ent->lde_namelen != 7)
1130                         goto remove;
1131
1132                 if (memcmp(ent->lde_name, "MDT", off) != 0)
1133                         goto remove;
1134
1135                 while (off < 7 && isxdigit(ent->lde_name[off]))
1136                         off++;
1137
1138                 if (off != 7) {
1139
1140 remove:
1141                         rc = lfsck_lpf_remove_name_entry(env, lfsck,
1142                                                          ent->lde_name);
1143                         if (rc != 0)
1144                                 break;
1145                 }
1146
1147 next:
1148                 rc = iops->next(env, it);
1149         }
1150
1151         iops->put(env, it);
1152         iops->fini(env, it);
1153
1154         RETURN(rc > 0 ? 0 : rc);
1155 }
1156
1157 static int lfsck_update_lpf_entry(const struct lu_env *env,
1158                                   struct lfsck_instance *lfsck,
1159                                   struct dt_object *parent,
1160                                   struct dt_object *child,
1161                                   const char *name,
1162                                   enum lfsck_verify_lpf_types type)
1163 {
1164         int rc;
1165
1166         if (type == LVLT_BY_BOOKMARK) {
1167                 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1168                                              lfsck_dto2fid(child), S_IFDIR);
1169         } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1170                 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1171                 rc = lfsck_bookmark_store(env, lfsck);
1172
1173                 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1174                        " in the bookmark file: rc = %d\n",
1175                        lfsck_lfsck2name(lfsck),
1176                        PFID(lfsck_dto2fid(child)), rc);
1177         }
1178
1179         return rc;
1180 }
1181
1182 /**
1183  * Check whether the @child back references the @parent.
1184  *
1185  * Two cases:
1186  * 1) The child's FID is stored in the bookmark file. If the child back
1187  *    references the parent (LU_LPF_FID object) via its ".." entry, then
1188  *    insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1189  *    the child back references another parent2, then:
1190  * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1191  * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1192  *      references the child. So keep them there. As the LFSCK processing,
1193  *      the parent3 may be found, then when the LFSCK run next time, the
1194  *      inconsistency can be repaired.
1195  *
1196  * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1197  *    entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1198  *    via its ".." entry, then update the bookmark file, otherwise, if the child
1199  *    back references another parent2, then:
1200  * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1201  *      from .lustre/lost+found/;
1202  * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1203  *      sub-directory name entry and update the child;
1204  * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1205  *      or not, then keep them there.
1206  *
1207  * \param[in] env       pointer to the thread context
1208  * \param[in] lfsck     pointer to the lfsck instance
1209  * \param[in] child     pointer to the lost+found sub-directory object
1210  * \param[in] name      the name for lost+found sub-directory object
1211  * \param[out] fid      pointer to the buffer to hold the FID of the object
1212  *                      (called it as parent2) that is referenced via the
1213  *                      child's dotdot entry; it also can be the FID that
1214  *                      is referenced by the name entry under the parent2.
1215  * \param[in] type      to indicate where the child's FID is stored in
1216  *
1217  * \retval              positive number for uncertain inconsistency
1218  * \retval              0 for success
1219  * \retval              negative error number on failure
1220  */
1221 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1222                                   struct lfsck_instance *lfsck,
1223                                   struct dt_object *child, const char *name,
1224                                   struct lu_fid *fid,
1225                                   enum lfsck_verify_lpf_types type)
1226 {
1227         struct dt_object         *parent  = lfsck->li_lpf_root_obj;
1228         struct lfsck_thread_info *info    = lfsck_env_info(env);
1229         char                     *name2   = info->lti_key;
1230         struct lu_fid            *fid2    = &info->lti_fid3;
1231         struct dt_object         *parent2 = NULL;
1232         struct lustre_handle      lh      = { 0 };
1233         int                       rc;
1234         ENTRY;
1235
1236         fid_zero(fid);
1237         rc = dt_lookup(env, child, (struct dt_rec *)fid,
1238                        (const struct dt_key *)dotdot);
1239         if (rc != 0)
1240                 GOTO(linkea, rc);
1241
1242         if (!fid_is_sane(fid))
1243                 GOTO(linkea, rc = -EINVAL);
1244
1245         if (lu_fid_eq(fid, &LU_LPF_FID)) {
1246                 const struct lu_name *cname;
1247
1248                 if (lfsck->li_lpf_obj == NULL) {
1249                         lu_object_get(&child->do_lu);
1250                         lfsck->li_lpf_obj = child;
1251                 }
1252
1253                 cname = lfsck_name_get_const(env, name, strlen(name));
1254                 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1255                 if (rc == 0)
1256                         rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1257                                                     name, type);
1258
1259                 GOTO(out_done, rc);
1260         }
1261
1262         parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1263         if (IS_ERR(parent2))
1264                 GOTO(linkea, parent2);
1265
1266         if (!dt_object_exists(parent2)) {
1267                 lfsck_object_put(env, parent2);
1268
1269                 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1270         }
1271
1272         if (!dt_try_as_dir(env, parent2)) {
1273                 lfsck_object_put(env, parent2);
1274
1275                 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1276         }
1277
1278 linkea:
1279         /* To prevent rename/unlink race */
1280         rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1281                               MDS_INODELOCK_UPDATE, LCK_PR);
1282         if (rc != 0)
1283                 GOTO(out_put, rc);
1284
1285         dt_read_lock(env, child, 0);
1286         rc = lfsck_links_get_first(env, child, name2, fid2);
1287         if (rc != 0) {
1288                 dt_read_unlock(env, child);
1289                 lfsck_ibits_unlock(&lh, LCK_PR);
1290
1291                 GOTO(out_put, rc = 1);
1292         }
1293
1294         /* It is almost impossible that the bookmark file (or the name entry)
1295          * and the linkEA hit the same data corruption. Trust the linkEA. */
1296         if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1297                 dt_read_unlock(env, child);
1298                 lfsck_ibits_unlock(&lh, LCK_PR);
1299
1300                 *fid = *fid2;
1301                 if (lfsck->li_lpf_obj == NULL) {
1302                         lu_object_get(&child->do_lu);
1303                         lfsck->li_lpf_obj = child;
1304                 }
1305
1306                 /* Update the child's dotdot entry */
1307                 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1308                                              &LU_LPF_FID, S_IFDIR);
1309                 if (rc == 0)
1310                         rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1311                                                     name, type);
1312
1313                 GOTO(out_put, rc);
1314         }
1315
1316         if (parent2 == NULL || IS_ERR(parent2)) {
1317                 dt_read_unlock(env, child);
1318                 lfsck_ibits_unlock(&lh, LCK_PR);
1319
1320                 GOTO(out_done, rc = 1);
1321         }
1322
1323         rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1324                        (const struct dt_key *)name2);
1325         dt_read_unlock(env, child);
1326         lfsck_ibits_unlock(&lh, LCK_PR);
1327         if (rc != 0 && rc != -ENOENT)
1328                 GOTO(out_put, rc);
1329
1330         if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1331                 if (type == LVLT_BY_BOOKMARK)
1332                         GOTO(out_put, rc = 1);
1333
1334                 /* Trust the name entry, update the child's dotdot entry. */
1335                 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1336                                              &LU_LPF_FID, S_IFDIR);
1337
1338                 GOTO(out_put, rc);
1339         }
1340
1341         if (type == LVLT_BY_BOOKMARK) {
1342                 /* Invalid FID record in the bookmark file, reset it. */
1343                 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1344                 rc = lfsck_bookmark_store(env, lfsck);
1345
1346                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1347                        " in the bookmark file: rc = %d\n",
1348                        lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1349         } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1350                 /* The name entry is wrong, remove it. */
1351                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1352         }
1353
1354         GOTO(out_put, rc);
1355
1356 out_put:
1357         if (parent2 != NULL && !IS_ERR(parent2))
1358                 lfsck_object_put(env, parent2);
1359
1360 out_done:
1361         return rc;
1362 }
1363
1364 /**
1365  * Verify the /ROOT/.lustre/lost+found/ directory.
1366  *
1367  * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1368  * the LFSCK does not exactly know how to handle, such as orphans. So before
1369  * the LFSCK scanning the system, the consistency of such directory needs to
1370  * be verified firstly to allow the users to use it during the LFSCK.
1371  *
1372  * \param[in] env       pointer to the thread context
1373  * \param[in] lfsck     pointer to the lfsck instance
1374  *
1375  * \retval              positive number for uncertain inconsistency
1376  * \retval              0 for success
1377  * \retval              negative error number on failure
1378  */
1379 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1380 {
1381         struct lfsck_thread_info *info   = lfsck_env_info(env);
1382         struct lu_fid            *pfid   = &info->lti_fid;
1383         struct lu_fid            *cfid   = &info->lti_fid2;
1384         struct lfsck_bookmark    *bk     = &lfsck->li_bookmark_ram;
1385         struct dt_object         *parent;
1386         /* child1's FID is in the bookmark file. */
1387         struct dt_object         *child1 = NULL;
1388         /* child2's FID is in the name entry MDTxxxx. */
1389         struct dt_object         *child2 = NULL;
1390         const struct lu_name     *cname;
1391         char                      name[8];
1392         int                       node   = lfsck_dev_idx(lfsck);
1393         int                       rc     = 0;
1394         ENTRY;
1395
1396         LASSERT(lfsck->li_master);
1397
1398         if (lfsck->li_lpf_root_obj != NULL)
1399                 RETURN(0);
1400
1401         if (node == 0) {
1402                 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1403                                                   &LU_LPF_FID);
1404         } else {
1405                 struct lfsck_tgt_desc *ltd;
1406
1407                 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1408                 if (unlikely(ltd == NULL))
1409                         RETURN(-ENXIO);
1410
1411                 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1412                                                   &LU_LPF_FID);
1413                 lfsck_tgt_put(ltd);
1414         }
1415
1416         if (IS_ERR(parent))
1417                 RETURN(PTR_ERR(parent));
1418
1419         LASSERT(dt_object_exists(parent));
1420
1421         if (unlikely(!dt_try_as_dir(env, parent))) {
1422                 lfsck_object_put(env, parent);
1423
1424                 GOTO(put, rc = -ENOTDIR);
1425         }
1426
1427         lfsck->li_lpf_root_obj = parent;
1428         if (node == 0) {
1429                 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1430                 if (rc != 0)
1431                         CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1432                                "for bad sub-directories: rc = %d\n",
1433                                lfsck_lfsck2name(lfsck), rc);
1434         }
1435
1436         /* child2 */
1437         snprintf(name, 8, "MDT%04x", node);
1438         rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1439                        (const struct dt_key *)name);
1440         if (rc == -ENOENT) {
1441                 rc = 0;
1442                 goto find_child1;
1443         }
1444
1445         if (rc != 0)
1446                 GOTO(put, rc);
1447
1448         /* Invalid FID in the name entry, remove the name entry. */
1449         if (!fid_is_norm(cfid)) {
1450                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1451                 if (rc != 0)
1452                         GOTO(put, rc);
1453
1454                 goto find_child1;
1455         }
1456
1457         child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1458         if (IS_ERR(child2))
1459                 GOTO(put, rc = PTR_ERR(child2));
1460
1461         if (unlikely(!dt_object_exists(child2) ||
1462                      dt_object_remote(child2)) ||
1463                      !S_ISDIR(lfsck_object_type(child2))) {
1464                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1465                 if (rc != 0)
1466                         GOTO(put, rc);
1467
1468                 goto find_child1;
1469         }
1470
1471         if (unlikely(!dt_try_as_dir(env, child2))) {
1472                 lfsck_object_put(env, child2);
1473                 child2 = NULL;
1474                 rc = -ENOTDIR;
1475         }
1476
1477 find_child1:
1478         if (fid_is_zero(&bk->lb_lpf_fid))
1479                 goto check_child2;
1480
1481         if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1482                 if (lfsck->li_lpf_obj == NULL) {
1483                         lu_object_get(&child2->do_lu);
1484                         lfsck->li_lpf_obj = child2;
1485                 }
1486
1487                 cname = lfsck_name_get_const(env, name, strlen(name));
1488                 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1489
1490                 GOTO(put, rc);
1491         }
1492
1493         if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1494                 struct lu_fid tfid = bk->lb_lpf_fid;
1495
1496                 /* Invalid FID record in the bookmark file, reset it. */
1497                 fid_zero(&bk->lb_lpf_fid);
1498                 rc = lfsck_bookmark_store(env, lfsck);
1499
1500                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1501                        " in the bookmark file: rc = %d\n",
1502                        lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1503
1504                 if (rc != 0)
1505                         GOTO(put, rc);
1506
1507                 goto check_child2;
1508         }
1509
1510         child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1511         if (IS_ERR(child1)) {
1512                 child1 = NULL;
1513                 goto check_child2;
1514         }
1515
1516         if (unlikely(!dt_object_exists(child1) ||
1517                      dt_object_remote(child1)) ||
1518                      !S_ISDIR(lfsck_object_type(child1))) {
1519                 /* Invalid FID record in the bookmark file, reset it. */
1520                 fid_zero(&bk->lb_lpf_fid);
1521                 rc = lfsck_bookmark_store(env, lfsck);
1522
1523                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1524                        " in the bookmark file: rc = %d\n",
1525                        lfsck_lfsck2name(lfsck),
1526                        PFID(lfsck_dto2fid(child1)), rc);
1527
1528                 if (rc != 0)
1529                         GOTO(put, rc);
1530
1531                 lfsck_object_put(env, child1);
1532                 child1 = NULL;
1533                 goto check_child2;
1534         }
1535
1536         if (unlikely(!dt_try_as_dir(env, child1))) {
1537                 lfsck_object_put(env, child1);
1538                 child1 = NULL;
1539                 rc = -ENOTDIR;
1540                 goto check_child2;
1541         }
1542
1543         rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1544                                     LVLT_BY_BOOKMARK);
1545         if (lu_fid_eq(pfid, &LU_LPF_FID))
1546                 GOTO(put, rc);
1547
1548 check_child2:
1549         if (child2 != NULL)
1550                 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1551                                             pfid, LVLT_BY_NAMEENTRY);
1552
1553         GOTO(put, rc);
1554
1555 put:
1556         if (lfsck->li_lpf_obj != NULL) {
1557                 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1558                         lfsck_object_put(env, lfsck->li_lpf_obj);
1559                         lfsck->li_lpf_obj = NULL;
1560                         rc = -ENOTDIR;
1561                 }
1562         } else if (rc == 0) {
1563                 rc = lfsck_create_lpf(env, lfsck);
1564         }
1565
1566         if (child2 != NULL && !IS_ERR(child2))
1567                 lfsck_object_put(env, child2);
1568         if (child1 != NULL && !IS_ERR(child1))
1569                 lfsck_object_put(env, child1);
1570
1571         return rc;
1572 }
1573
1574 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1575 {
1576         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
1577         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
1578         char                    *prefix;
1579         int                      rc     = 0;
1580         ENTRY;
1581
1582         if (unlikely(ss == NULL))
1583                 RETURN(-ENXIO);
1584
1585         OBD_ALLOC_PTR(lfsck->li_seq);
1586         if (lfsck->li_seq == NULL)
1587                 RETURN(-ENOMEM);
1588
1589         OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1590         if (prefix == NULL)
1591                 GOTO(out, rc = -ENOMEM);
1592
1593         snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1594         rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1595                              ss->ss_server_seq);
1596         OBD_FREE(prefix, MAX_OBD_NAME + 7);
1597         if (rc != 0)
1598                 GOTO(out, rc);
1599
1600         if (fid_is_sane(&bk->lb_last_fid))
1601                 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1602
1603         RETURN(0);
1604
1605 out:
1606         OBD_FREE_PTR(lfsck->li_seq);
1607         lfsck->li_seq = NULL;
1608
1609         return rc;
1610 }
1611
1612 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1613 {
1614         if (lfsck->li_seq != NULL) {
1615                 seq_client_fini(lfsck->li_seq);
1616                 OBD_FREE_PTR(lfsck->li_seq);
1617                 lfsck->li_seq = NULL;
1618         }
1619 }
1620
1621 void lfsck_instance_cleanup(const struct lu_env *env,
1622                             struct lfsck_instance *lfsck)
1623 {
1624         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1625         struct lfsck_component  *com;
1626         struct lfsck_component  *next;
1627         struct lfsck_lmv_unit   *llu;
1628         struct lfsck_lmv_unit   *llu_next;
1629         struct lfsck_lmv        *llmv;
1630         ENTRY;
1631
1632         LASSERT(list_empty(&lfsck->li_link));
1633         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1634
1635         if (lfsck->li_obj_oit != NULL) {
1636                 lfsck_object_put(env, lfsck->li_obj_oit);
1637                 lfsck->li_obj_oit = NULL;
1638         }
1639
1640         LASSERT(lfsck->li_obj_dir == NULL);
1641         LASSERT(lfsck->li_lmv == NULL);
1642
1643         list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1644                 llmv = &llu->llu_lmv;
1645
1646                 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1647                          "still in using: %u\n",
1648                          atomic_read(&llmv->ll_ref));
1649
1650                 lfsck_lmv_put(env, llmv);
1651         }
1652
1653         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1654                 lfsck_component_cleanup(env, com);
1655         }
1656
1657         LASSERT(list_empty(&lfsck->li_list_dir));
1658
1659         list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1660                                  lc_link) {
1661                 lfsck_component_cleanup(env, com);
1662         }
1663
1664         list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1665                 lfsck_component_cleanup(env, com);
1666         }
1667
1668         lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1669         lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1670
1671         if (lfsck->li_lfsck_dir != NULL) {
1672                 lfsck_object_put(env, lfsck->li_lfsck_dir);
1673                 lfsck->li_lfsck_dir = NULL;
1674         }
1675
1676         if (lfsck->li_bookmark_obj != NULL) {
1677                 lfsck_object_put(env, lfsck->li_bookmark_obj);
1678                 lfsck->li_bookmark_obj = NULL;
1679         }
1680
1681         if (lfsck->li_lpf_obj != NULL) {
1682                 lfsck_object_put(env, lfsck->li_lpf_obj);
1683                 lfsck->li_lpf_obj = NULL;
1684         }
1685
1686         if (lfsck->li_lpf_root_obj != NULL) {
1687                 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1688                 lfsck->li_lpf_root_obj = NULL;
1689         }
1690
1691         if (lfsck->li_los != NULL) {
1692                 local_oid_storage_fini(env, lfsck->li_los);
1693                 lfsck->li_los = NULL;
1694         }
1695
1696         lfsck_fid_fini(lfsck);
1697
1698         OBD_FREE_PTR(lfsck);
1699 }
1700
1701 static inline struct lfsck_instance *
1702 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1703 {
1704         struct lfsck_instance *lfsck;
1705
1706         list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1707                 if (lfsck->li_bottom == key) {
1708                         if (ref)
1709                                 lfsck_instance_get(lfsck);
1710                         if (unlink)
1711                                 list_del_init(&lfsck->li_link);
1712
1713                         return lfsck;
1714                 }
1715         }
1716
1717         return NULL;
1718 }
1719
1720 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1721                                            bool unlink)
1722 {
1723         struct lfsck_instance *lfsck;
1724
1725         spin_lock(&lfsck_instance_lock);
1726         lfsck = __lfsck_instance_find(key, ref, unlink);
1727         spin_unlock(&lfsck_instance_lock);
1728
1729         return lfsck;
1730 }
1731
1732 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1733 {
1734         struct lfsck_instance *tmp;
1735
1736         spin_lock(&lfsck_instance_lock);
1737         list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1738                 if (lfsck->li_bottom == tmp->li_bottom) {
1739                         spin_unlock(&lfsck_instance_lock);
1740                         return -EEXIST;
1741                 }
1742         }
1743
1744         list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1745         spin_unlock(&lfsck_instance_lock);
1746         return 0;
1747 }
1748
1749 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1750                      const char *prefix)
1751 {
1752         int flag;
1753         int i;
1754         bool newline = (bits != 0 ? false : true);
1755
1756         seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1757
1758         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1759                 if (flag & bits) {
1760                         bits &= ~flag;
1761                         if (names[i] != NULL) {
1762                                 if (bits == 0)
1763                                         newline = true;
1764
1765                                 seq_printf(m, "%s%c", names[i],
1766                                            newline ? '\n' : ',');
1767                         }
1768                 }
1769         }
1770
1771         if (!newline)
1772                 seq_putc(m, '\n');
1773 }
1774
1775 void lfsck_time_dump(struct seq_file *m, time64_t time, const char *name)
1776 {
1777         if (time == 0) {
1778                 seq_printf(m, "%s_time: N/A\n", name);
1779                 seq_printf(m, "time_since_%s: N/A\n", name);
1780         } else {
1781                 seq_printf(m, "%s_time: %lld\n", name, time);
1782                 seq_printf(m, "time_since_%s: %lld seconds\n",
1783                            name, ktime_get_real_seconds() - time);
1784         }
1785 }
1786
1787 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1788                     const char *prefix)
1789 {
1790         if (fid_is_zero(&pos->lp_dir_parent)) {
1791                 if (pos->lp_oit_cookie == 0) {
1792                         seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1793                         return;
1794                 }
1795                 seq_printf(m, "%s: %llu, N/A, N/A\n",
1796                            prefix, pos->lp_oit_cookie);
1797         } else {
1798                 seq_printf(m, "%s: %llu, "DFID", %#llx\n",
1799                            prefix, pos->lp_oit_cookie,
1800                            PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1801         }
1802 }
1803
1804 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1805                     struct lfsck_position *pos, bool init)
1806 {
1807         const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1808
1809         if (unlikely(lfsck->li_di_oit == NULL)) {
1810                 memset(pos, 0, sizeof(*pos));
1811                 return;
1812         }
1813
1814         pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1815         if (!lfsck->li_current_oit_processed && !init)
1816                 pos->lp_oit_cookie--;
1817
1818         if (unlikely(pos->lp_oit_cookie == 0))
1819                 pos->lp_oit_cookie = 1;
1820
1821         if (lfsck->li_di_dir != NULL) {
1822                 struct dt_object *dto = lfsck->li_obj_dir;
1823
1824                 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1825                                                         lfsck->li_di_dir);
1826
1827                 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1828                         fid_zero(&pos->lp_dir_parent);
1829                         pos->lp_dir_cookie = 0;
1830                 } else {
1831                         pos->lp_dir_parent = *lfsck_dto2fid(dto);
1832                 }
1833         } else {
1834                 fid_zero(&pos->lp_dir_parent);
1835                 pos->lp_dir_cookie = 0;
1836         }
1837 }
1838
1839 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1840 {
1841         bool dirty = false;
1842
1843         if (limit != LFSCK_SPEED_NO_LIMIT) {
1844                 if (limit > cfs_time_seconds(1)) {
1845                         lfsck->li_sleep_rate = limit / cfs_time_seconds(1);
1846                         lfsck->li_sleep_jif = 1;
1847                 } else {
1848                         lfsck->li_sleep_rate = 1;
1849                         lfsck->li_sleep_jif = cfs_time_seconds(1) / limit;
1850                 }
1851         } else {
1852                 lfsck->li_sleep_jif = 0;
1853                 lfsck->li_sleep_rate = 0;
1854         }
1855
1856         if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1857                 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1858                 dirty = true;
1859         }
1860
1861         return dirty;
1862 }
1863
1864 void lfsck_control_speed(struct lfsck_instance *lfsck)
1865 {
1866         struct ptlrpc_thread *thread = &lfsck->li_thread;
1867         struct l_wait_info    lwi;
1868
1869         if (lfsck->li_sleep_jif > 0 &&
1870             lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1871                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1872                                        LWI_ON_SIGNAL_NOOP, NULL);
1873
1874                 l_wait_event(thread->t_ctl_waitq,
1875                              !thread_is_running(thread),
1876                              &lwi);
1877                 lfsck->li_new_scanned = 0;
1878         }
1879 }
1880
1881 void lfsck_control_speed_by_self(struct lfsck_component *com)
1882 {
1883         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1884         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1885         struct l_wait_info       lwi;
1886
1887         if (lfsck->li_sleep_jif > 0 &&
1888             com->lc_new_scanned >= lfsck->li_sleep_rate) {
1889                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1890                                        LWI_ON_SIGNAL_NOOP, NULL);
1891
1892                 l_wait_event(thread->t_ctl_waitq,
1893                              !thread_is_running(thread),
1894                              &lwi);
1895                 com->lc_new_scanned = 0;
1896         }
1897 }
1898
1899 static struct lfsck_thread_args *
1900 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1901                        struct lfsck_component *com,
1902                        struct lfsck_start_param *lsp)
1903 {
1904         struct lfsck_thread_args *lta;
1905         int                       rc;
1906
1907         OBD_ALLOC_PTR(lta);
1908         if (lta == NULL)
1909                 return ERR_PTR(-ENOMEM);
1910
1911         rc = lu_env_init(&lta->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1912         if (rc != 0) {
1913                 OBD_FREE_PTR(lta);
1914                 return ERR_PTR(rc);
1915         }
1916
1917         lta->lta_lfsck = lfsck_instance_get(lfsck);
1918         if (com != NULL)
1919                 lta->lta_com = lfsck_component_get(com);
1920
1921         lta->lta_lsp = lsp;
1922
1923         return lta;
1924 }
1925
1926 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1927 {
1928         if (lta->lta_com != NULL)
1929                 lfsck_component_put(&lta->lta_env, lta->lta_com);
1930         lfsck_instance_put(&lta->lta_env, lta->lta_lfsck);
1931         lu_env_fini(&lta->lta_env);
1932         OBD_FREE_PTR(lta);
1933 }
1934
1935 struct lfsck_assistant_data *
1936 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1937                           const char *name)
1938 {
1939         struct lfsck_assistant_data *lad;
1940
1941         OBD_ALLOC_PTR(lad);
1942         if (lad != NULL) {
1943                 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1944                 if (lad->lad_bitmap == NULL) {
1945                         OBD_FREE_PTR(lad);
1946                         return NULL;
1947                 }
1948
1949                 INIT_LIST_HEAD(&lad->lad_req_list);
1950                 spin_lock_init(&lad->lad_lock);
1951                 INIT_LIST_HEAD(&lad->lad_ost_list);
1952                 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1953                 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1954                 INIT_LIST_HEAD(&lad->lad_mdt_list);
1955                 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1956                 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1957                 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1958                 lad->lad_ops = lao;
1959                 lad->lad_name = name;
1960         }
1961
1962         return lad;
1963 }
1964
1965 struct lfsck_assistant_object *
1966 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1967                             const struct lu_attr *attr, __u64 cookie,
1968                             bool is_dir)
1969 {
1970         struct lfsck_assistant_object   *lso;
1971
1972         OBD_ALLOC_PTR(lso);
1973         if (lso == NULL)
1974                 return ERR_PTR(-ENOMEM);
1975
1976         lso->lso_fid = *fid;
1977         if (attr != NULL)
1978                 lso->lso_attr = *attr;
1979
1980         atomic_set(&lso->lso_ref, 1);
1981         lso->lso_oit_cookie = cookie;
1982         if (is_dir)
1983                 lso->lso_is_dir = 1;
1984
1985         return lso;
1986 }
1987
1988 struct dt_object *
1989 lfsck_assistant_object_load(const struct lu_env *env,
1990                             struct lfsck_instance *lfsck,
1991                             struct lfsck_assistant_object *lso)
1992 {
1993         struct dt_object *obj;
1994
1995         obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
1996         if (IS_ERR(obj))
1997                 return obj;
1998
1999         if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2000                 lso->lso_dead = 1;
2001                 lfsck_object_put(env, obj);
2002
2003                 return ERR_PTR(-ENOENT);
2004         }
2005
2006         if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2007                 lfsck_object_put(env, obj);
2008
2009                 return ERR_PTR(-ENOTDIR);
2010         }
2011
2012         return obj;
2013 }
2014
2015 /**
2016  * Generic LFSCK asynchronous communication interpretor function.
2017  * The LFSCK RPC reply for both the event notification and status
2018  * querying will be handled here.
2019  *
2020  * \param[in] env       pointer to the thread context
2021  * \param[in] req       pointer to the LFSCK request
2022  * \param[in] args      pointer to the lfsck_async_interpret_args
2023  * \param[in] rc        the result for handling the LFSCK request
2024  *
2025  * \retval              0 for success
2026  * \retval              negative error number on failure
2027  */
2028 int lfsck_async_interpret_common(const struct lu_env *env,
2029                                  struct ptlrpc_request *req,
2030                                  void *args, int rc)
2031 {
2032         struct lfsck_async_interpret_args *laia = args;
2033         struct lfsck_component            *com  = laia->laia_com;
2034         struct lfsck_assistant_data       *lad  = com->lc_data;
2035         struct lfsck_tgt_descs            *ltds = laia->laia_ltds;
2036         struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
2037         struct lfsck_request              *lr   = laia->laia_lr;
2038
2039         LASSERT(com->lc_lfsck->li_master);
2040
2041         switch (lr->lr_event) {
2042         case LE_START:
2043                 if (unlikely(rc == -EINPROGRESS)) {
2044                         ltd->ltd_retry_start = 1;
2045                         break;
2046                 }
2047
2048                 if (rc != 0) {
2049                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2050                                "start: rc = %d\n",
2051                                lfsck_lfsck2name(com->lc_lfsck),
2052                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2053                                ltd->ltd_index, lad->lad_name, rc);
2054
2055                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2056                                 struct lfsck_layout *lo = com->lc_file_ram;
2057
2058                                 if (lr->lr_flags & LEF_TO_OST)
2059                                         lfsck_lad_set_bitmap(env, com,
2060                                                              ltd->ltd_index);
2061                                 else
2062                                         lo->ll_flags |= LF_INCOMPLETE;
2063                         } else {
2064                                 struct lfsck_namespace *ns = com->lc_file_ram;
2065
2066                                 /* If some MDT does not join the namespace
2067                                  * LFSCK, then we cannot know whether there
2068                                  * is some name entry on such MDT that with
2069                                  * the referenced MDT-object on this MDT or
2070                                  * not. So the namespace LFSCK on this MDT
2071                                  * cannot handle orphan MDT-objects properly.
2072                                  * So we mark the LFSCK as LF_INCOMPLETE and
2073                                  * skip orphan MDT-objects handling. */
2074                                 ns->ln_flags |= LF_INCOMPLETE;
2075                         }
2076                         break;
2077                 }
2078
2079                 spin_lock(&ltds->ltd_lock);
2080                 if (ltd->ltd_dead) {
2081                         spin_unlock(&ltds->ltd_lock);
2082                         break;
2083                 }
2084
2085                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2086                         struct list_head *list;
2087                         struct list_head *phase_list;
2088
2089                         if (ltd->ltd_layout_done) {
2090                                 spin_unlock(&ltds->ltd_lock);
2091                                 break;
2092                         }
2093
2094                         if (lr->lr_flags & LEF_TO_OST) {
2095                                 list = &lad->lad_ost_list;
2096                                 phase_list = &lad->lad_ost_phase1_list;
2097                         } else {
2098                                 list = &lad->lad_mdt_list;
2099                                 phase_list = &lad->lad_mdt_phase1_list;
2100                         }
2101
2102                         if (list_empty(&ltd->ltd_layout_list))
2103                                 list_add_tail(&ltd->ltd_layout_list, list);
2104                         if (list_empty(&ltd->ltd_layout_phase_list))
2105                                 list_add_tail(&ltd->ltd_layout_phase_list,
2106                                               phase_list);
2107                 } else {
2108                         if (ltd->ltd_namespace_done) {
2109                                 spin_unlock(&ltds->ltd_lock);
2110                                 break;
2111                         }
2112
2113                         if (list_empty(&ltd->ltd_namespace_list))
2114                                 list_add_tail(&ltd->ltd_namespace_list,
2115                                               &lad->lad_mdt_list);
2116                         if (list_empty(&ltd->ltd_namespace_phase_list))
2117                                 list_add_tail(&ltd->ltd_namespace_phase_list,
2118                                               &lad->lad_mdt_phase1_list);
2119                 }
2120                 spin_unlock(&ltds->ltd_lock);
2121                 break;
2122         case LE_STOP:
2123         case LE_PHASE1_DONE:
2124         case LE_PHASE2_DONE:
2125         case LE_PEER_EXIT:
2126                 if (rc != 0 && rc != -EALREADY)
2127                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2128                               "event = %d, rc = %d\n",
2129                               lfsck_lfsck2name(com->lc_lfsck),
2130                               (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2131                               ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2132                 break;
2133         case LE_QUERY: {
2134                 struct lfsck_reply *reply;
2135                 struct list_head *list;
2136                 struct list_head *phase_list;
2137
2138                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2139                         list = &ltd->ltd_layout_list;
2140                         phase_list = &ltd->ltd_layout_phase_list;
2141                 } else {
2142                         list = &ltd->ltd_namespace_list;
2143                         phase_list = &ltd->ltd_namespace_phase_list;
2144                 }
2145
2146                 if (rc != 0) {
2147                         if (lr->lr_flags & LEF_QUERY_ALL) {
2148                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2149                                 break;
2150                         }
2151
2152                         spin_lock(&ltds->ltd_lock);
2153                         list_del_init(phase_list);
2154                         list_del_init(list);
2155                         spin_unlock(&ltds->ltd_lock);
2156                         break;
2157                 }
2158
2159                 reply = req_capsule_server_get(&req->rq_pill,
2160                                                &RMF_LFSCK_REPLY);
2161                 if (reply == NULL) {
2162                         rc = -EPROTO;
2163                         CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2164                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2165                                lad->lad_name, rc);
2166
2167                         if (lr->lr_flags & LEF_QUERY_ALL) {
2168                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2169                                 break;
2170                         }
2171
2172                         spin_lock(&ltds->ltd_lock);
2173                         list_del_init(phase_list);
2174                         list_del_init(list);
2175                         spin_unlock(&ltds->ltd_lock);
2176                         break;
2177                 }
2178
2179                 if (lr->lr_flags & LEF_QUERY_ALL) {
2180                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2181                                 ltd->ltd_layout_status = reply->lr_status;
2182                                 ltd->ltd_layout_repaired = reply->lr_repaired;
2183                         } else {
2184                                 ltd->ltd_namespace_status = reply->lr_status;
2185                                 ltd->ltd_namespace_repaired =
2186                                                         reply->lr_repaired;
2187                         }
2188                         break;
2189                 }
2190
2191                 switch (reply->lr_status) {
2192                 case LS_SCANNING_PHASE1:
2193                         break;
2194                 case LS_SCANNING_PHASE2:
2195                         spin_lock(&ltds->ltd_lock);
2196                         list_del_init(phase_list);
2197                         if (ltd->ltd_dead) {
2198                                 spin_unlock(&ltds->ltd_lock);
2199                                 break;
2200                         }
2201
2202                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2203                                 if (ltd->ltd_layout_done) {
2204                                         spin_unlock(&ltds->ltd_lock);
2205                                         break;
2206                                 }
2207
2208                                 if (lr->lr_flags & LEF_TO_OST)
2209                                         list_add_tail(phase_list,
2210                                                 &lad->lad_ost_phase2_list);
2211                                 else
2212                                         list_add_tail(phase_list,
2213                                                 &lad->lad_mdt_phase2_list);
2214                         } else {
2215                                 if (ltd->ltd_namespace_done) {
2216                                         spin_unlock(&ltds->ltd_lock);
2217                                         break;
2218                                 }
2219
2220                                 list_add_tail(phase_list,
2221                                               &lad->lad_mdt_phase2_list);
2222                         }
2223                         spin_unlock(&ltds->ltd_lock);
2224                         break;
2225                 default:
2226                         spin_lock(&ltds->ltd_lock);
2227                         list_del_init(phase_list);
2228                         list_del_init(list);
2229                         spin_unlock(&ltds->ltd_lock);
2230                         break;
2231                 }
2232                 break;
2233         }
2234         default:
2235                 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2236                        lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2237                 break;
2238         }
2239
2240         if (!laia->laia_shared) {
2241                 lfsck_tgt_put(ltd);
2242                 lfsck_component_put(env, com);
2243         }
2244
2245         return 0;
2246 }
2247
2248 static void lfsck_interpret(const struct lu_env *env,
2249                             struct lfsck_instance *lfsck,
2250                             struct ptlrpc_request *req, void *args, int result)
2251 {
2252         struct lfsck_async_interpret_args *laia = args;
2253         struct lfsck_component            *com;
2254
2255         LASSERT(laia->laia_com == NULL);
2256         LASSERT(laia->laia_shared);
2257
2258         spin_lock(&lfsck->li_lock);
2259         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2260                 laia->laia_com = com;
2261                 lfsck_async_interpret_common(env, req, laia, result);
2262         }
2263
2264         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2265                 laia->laia_com = com;
2266                 lfsck_async_interpret_common(env, req, laia, result);
2267         }
2268         spin_unlock(&lfsck->li_lock);
2269 }
2270
2271 static int lfsck_stop_notify(const struct lu_env *env,
2272                              struct lfsck_instance *lfsck,
2273                              struct lfsck_tgt_descs *ltds,
2274                              struct lfsck_tgt_desc *ltd, __u16 type)
2275 {
2276         struct lfsck_component *com;
2277         int                     rc = 0;
2278         ENTRY;
2279
2280         LASSERT(lfsck->li_master);
2281
2282         spin_lock(&lfsck->li_lock);
2283         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2284         if (com == NULL)
2285                 com = __lfsck_component_find(lfsck, type,
2286                                              &lfsck->li_list_double_scan);
2287         if (com != NULL)
2288                 lfsck_component_get(com);
2289         spin_unlock(&lfsck->li_lock);
2290
2291         if (com != NULL) {
2292                 struct lfsck_thread_info          *info  = lfsck_env_info(env);
2293                 struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2294                 struct lfsck_request              *lr    = &info->lti_lr;
2295                 struct lfsck_assistant_data       *lad   = com->lc_data;
2296                 struct list_head                  *list;
2297                 struct list_head                  *phase_list;
2298                 struct ptlrpc_request_set         *set;
2299
2300                 set = ptlrpc_prep_set();
2301                 if (set == NULL) {
2302                         lfsck_component_put(env, com);
2303
2304                         RETURN(-ENOMEM);
2305                 }
2306
2307                 if (type == LFSCK_TYPE_LAYOUT) {
2308                         list = &ltd->ltd_layout_list;
2309                         phase_list = &ltd->ltd_layout_phase_list;
2310                 } else {
2311                         list = &ltd->ltd_namespace_list;
2312                         phase_list = &ltd->ltd_namespace_phase_list;
2313                 }
2314
2315                 spin_lock(&ltds->ltd_lock);
2316                 if (list_empty(list)) {
2317                         LASSERT(list_empty(phase_list));
2318                         spin_unlock(&ltds->ltd_lock);
2319                         ptlrpc_set_destroy(set);
2320
2321                         RETURN(0);
2322                 }
2323
2324                 list_del_init(phase_list);
2325                 list_del_init(list);
2326                 spin_unlock(&ltds->ltd_lock);
2327
2328                 memset(lr, 0, sizeof(*lr));
2329                 lr->lr_index = lfsck_dev_idx(lfsck);
2330                 lr->lr_event = LE_PEER_EXIT;
2331                 lr->lr_active = type;
2332                 lr->lr_status = LS_CO_PAUSED;
2333                 if (ltds == &lfsck->li_ost_descs)
2334                         lr->lr_flags = LEF_TO_OST;
2335
2336                 memset(laia, 0, sizeof(*laia));
2337                 laia->laia_com = com;
2338                 laia->laia_ltds = ltds;
2339                 atomic_inc(&ltd->ltd_ref);
2340                 laia->laia_ltd = ltd;
2341                 laia->laia_lr = lr;
2342
2343                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2344                                          lfsck_async_interpret_common,
2345                                          laia, LFSCK_NOTIFY);
2346                 if (rc != 0) {
2347                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2348                                "co-stop for %s: rc = %d\n",
2349                                lfsck_lfsck2name(lfsck),
2350                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2351                                ltd->ltd_index, lad->lad_name, rc);
2352                         lfsck_tgt_put(ltd);
2353                 } else {
2354                         rc = ptlrpc_set_wait(env, set);
2355                 }
2356
2357                 ptlrpc_set_destroy(set);
2358                 lfsck_component_put(env, com);
2359         }
2360
2361         RETURN(rc);
2362 }
2363
2364 static int lfsck_async_interpret(const struct lu_env *env,
2365                                  struct ptlrpc_request *req,
2366                                  void *args, int rc)
2367 {
2368         struct lfsck_async_interpret_args *laia = args;
2369         struct lfsck_instance             *lfsck;
2370
2371         lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2372                               li_mdt_descs);
2373         lfsck_interpret(env, lfsck, req, laia, rc);
2374         lfsck_tgt_put(laia->laia_ltd);
2375         if (rc != 0 && laia->laia_result != -EALREADY)
2376                 laia->laia_result = rc;
2377
2378         return 0;
2379 }
2380
2381 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2382                         struct lfsck_request *lr,
2383                         struct ptlrpc_request_set *set,
2384                         ptlrpc_interpterer_t interpreter,
2385                         void *args, int request)
2386 {
2387         struct lfsck_async_interpret_args *laia;
2388         struct ptlrpc_request             *req;
2389         struct lfsck_request              *tmp;
2390         struct req_format                 *format;
2391         int                                rc;
2392
2393         switch (request) {
2394         case LFSCK_NOTIFY:
2395                 format = &RQF_LFSCK_NOTIFY;
2396                 break;
2397         case LFSCK_QUERY:
2398                 format = &RQF_LFSCK_QUERY;
2399                 break;
2400         default:
2401                 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2402                        exp->exp_obd->obd_name, request, -EINVAL);
2403                 return -EINVAL;
2404         }
2405
2406         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2407         if (req == NULL)
2408                 return -ENOMEM;
2409
2410         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2411         if (rc != 0) {
2412                 ptlrpc_request_free(req);
2413
2414                 return rc;
2415         }
2416
2417         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2418         *tmp = *lr;
2419         ptlrpc_request_set_replen(req);
2420
2421         laia = ptlrpc_req_async_args(req);
2422         *laia = *(struct lfsck_async_interpret_args *)args;
2423         if (laia->laia_com != NULL)
2424                 lfsck_component_get(laia->laia_com);
2425         req->rq_interpret_reply = interpreter;
2426         req->rq_allow_intr = 1;
2427         req->rq_no_delay = 1;
2428         ptlrpc_set_add_req(set, req);
2429
2430         return 0;
2431 }
2432
2433 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2434 {
2435         struct lfsck_thread_info          *info  = lfsck_env_info(env);
2436         struct lfsck_request              *lr    = &info->lti_lr;
2437         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2438         struct lfsck_instance             *lfsck = com->lc_lfsck;
2439         struct lfsck_tgt_descs            *ltds  = &lfsck->li_mdt_descs;
2440         struct lfsck_tgt_desc             *ltd;
2441         struct ptlrpc_request_set         *set;
2442         int                                idx;
2443         int                                rc;
2444         ENTRY;
2445
2446         memset(lr, 0, sizeof(*lr));
2447         lr->lr_event = LE_QUERY;
2448         lr->lr_active = com->lc_type;
2449         lr->lr_flags = LEF_QUERY_ALL;
2450
2451         memset(laia, 0, sizeof(*laia));
2452         laia->laia_com = com;
2453         laia->laia_lr = lr;
2454
2455         set = ptlrpc_prep_set();
2456         if (set == NULL)
2457                 RETURN(-ENOMEM);
2458
2459 again:
2460         laia->laia_ltds = ltds;
2461         down_read(&ltds->ltd_rw_sem);
2462         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2463                 ltd = lfsck_tgt_get(ltds, idx);
2464                 LASSERT(ltd != NULL);
2465
2466                 laia->laia_ltd = ltd;
2467                 up_read(&ltds->ltd_rw_sem);
2468                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2469                                          lfsck_async_interpret_common,
2470                                          laia, LFSCK_QUERY);
2471                 if (rc != 0) {
2472                         struct lfsck_assistant_data *lad = com->lc_data;
2473
2474                         CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2475                                "rc = %d\n", lfsck_lfsck2name(lfsck),
2476                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2477                                ltd->ltd_index, lad->lad_name, rc);
2478                         lfsck_reset_ltd_status(ltd, com->lc_type);
2479                         lfsck_tgt_put(ltd);
2480                 }
2481                 down_read(&ltds->ltd_rw_sem);
2482         }
2483         up_read(&ltds->ltd_rw_sem);
2484
2485         if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2486                 ltds = &lfsck->li_ost_descs;
2487                 lr->lr_flags |= LEF_TO_OST;
2488                 goto again;
2489         }
2490
2491         rc = ptlrpc_set_wait(env, set);
2492         ptlrpc_set_destroy(set);
2493
2494         RETURN(rc);
2495 }
2496
2497 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2498                           struct lfsck_start_param *lsp)
2499 {
2500         struct lfsck_instance           *lfsck   = com->lc_lfsck;
2501         struct lfsck_assistant_data     *lad     = com->lc_data;
2502         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
2503         struct ptlrpc_thread            *athread = &lad->lad_thread;
2504         struct lfsck_thread_args        *lta;
2505         struct task_struct              *task;
2506         int                              rc;
2507         ENTRY;
2508
2509         lad->lad_assistant_status = 0;
2510         lad->lad_post_result = 0;
2511         lad->lad_flags = 0;
2512         lad->lad_advance_lock = false;
2513         thread_set_flags(athread, 0);
2514
2515         lta = lfsck_thread_args_init(lfsck, com, lsp);
2516         if (IS_ERR(lta))
2517                 RETURN(PTR_ERR(lta));
2518
2519         task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2520         if (IS_ERR(task)) {
2521                 rc = PTR_ERR(task);
2522                 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2523                        "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2524                 lfsck_thread_args_fini(lta);
2525         } else {
2526                 struct l_wait_info lwi = { 0 };
2527
2528                 l_wait_event(mthread->t_ctl_waitq,
2529                              thread_is_running(athread) ||
2530                              thread_is_stopped(athread) ||
2531                              !thread_is_starting(mthread),
2532                              &lwi);
2533                 if (unlikely(!thread_is_starting(mthread)))
2534                         /* stopped by race */
2535                         rc = -ESRCH;
2536                 else if (unlikely(!thread_is_running(athread)))
2537                         rc = lad->lad_assistant_status;
2538                 else
2539                         rc = 0;
2540         }
2541
2542         RETURN(rc);
2543 }
2544
2545 int lfsck_checkpoint_generic(const struct lu_env *env,
2546                              struct lfsck_component *com)
2547 {
2548         struct lfsck_assistant_data     *lad     = com->lc_data;
2549         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2550         struct ptlrpc_thread            *athread = &lad->lad_thread;
2551         struct l_wait_info               lwi     = { 0 };
2552
2553         l_wait_event(mthread->t_ctl_waitq,
2554                      list_empty(&lad->lad_req_list) ||
2555                      !thread_is_running(mthread) ||
2556                      thread_is_stopped(athread),
2557                      &lwi);
2558
2559         if (!thread_is_running(mthread) || thread_is_stopped(athread))
2560                 return LFSCK_CHECKPOINT_SKIP;
2561
2562         return 0;
2563 }
2564
2565 void lfsck_post_generic(const struct lu_env *env,
2566                         struct lfsck_component *com, int *result)
2567 {
2568         struct lfsck_assistant_data     *lad     = com->lc_data;
2569         struct ptlrpc_thread            *athread = &lad->lad_thread;
2570         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2571         struct l_wait_info               lwi     = { 0 };
2572
2573         lad->lad_post_result = *result;
2574         if (*result <= 0)
2575                 set_bit(LAD_EXIT, &lad->lad_flags);
2576         set_bit(LAD_TO_POST, &lad->lad_flags);
2577
2578         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2579                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2580
2581         wake_up_all(&athread->t_ctl_waitq);
2582         l_wait_event(mthread->t_ctl_waitq,
2583                      (*result > 0 && list_empty(&lad->lad_req_list)) ||
2584                      thread_is_stopped(athread),
2585                      &lwi);
2586
2587         if (lad->lad_assistant_status < 0)
2588                 *result = lad->lad_assistant_status;
2589
2590         CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2591                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2592 }
2593
2594 int lfsck_double_scan_generic(const struct lu_env *env,
2595                               struct lfsck_component *com, int status)
2596 {
2597         struct lfsck_assistant_data     *lad     = com->lc_data;
2598         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2599         struct ptlrpc_thread            *athread = &lad->lad_thread;
2600         struct l_wait_info               lwi     = { 0 };
2601
2602         if (status != LS_SCANNING_PHASE2)
2603                 set_bit(LAD_EXIT, &lad->lad_flags);
2604         else
2605                 set_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags);
2606
2607         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2608                "status %d\n",
2609                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2610
2611         wake_up_all(&athread->t_ctl_waitq);
2612         l_wait_event(mthread->t_ctl_waitq,
2613                      test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags) ||
2614                      thread_is_stopped(athread),
2615                      &lwi);
2616
2617         CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2618                "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2619                lad->lad_assistant_status);
2620
2621         if (lad->lad_assistant_status < 0)
2622                 return lad->lad_assistant_status;
2623
2624         return 0;
2625 }
2626
2627 void lfsck_quit_generic(const struct lu_env *env,
2628                         struct lfsck_component *com)
2629 {
2630         struct lfsck_assistant_data     *lad     = com->lc_data;
2631         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2632         struct ptlrpc_thread            *athread = &lad->lad_thread;
2633         struct l_wait_info               lwi     = { 0 };
2634
2635         set_bit(LAD_EXIT, &lad->lad_flags);
2636         wake_up_all(&athread->t_ctl_waitq);
2637         l_wait_event(mthread->t_ctl_waitq,
2638                      thread_is_init(athread) ||
2639                      thread_is_stopped(athread),
2640                      &lwi);
2641 }
2642
2643 int lfsck_load_one_trace_file(const struct lu_env *env,
2644                               struct lfsck_component *com,
2645                               struct dt_object *parent,
2646                               struct dt_object **child,
2647                               const struct dt_index_features *ft,
2648                               const char *name, bool reset)
2649 {
2650         struct lfsck_instance *lfsck = com->lc_lfsck;
2651         struct dt_object *obj;
2652         int rc;
2653         ENTRY;
2654
2655         if (*child != NULL) {
2656                 struct dt_it *it;
2657                 const struct dt_it_ops *iops;
2658                 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2659
2660                 if (!reset)
2661                         RETURN(0);
2662
2663                 obj = *child;
2664                 rc = obj->do_ops->do_index_try(env, obj, ft);
2665                 if (rc)
2666                         /* unlink by force */
2667                         goto unlink;
2668
2669                 iops = &obj->do_index_ops->dio_it;
2670                 it = iops->init(env, obj, 0);
2671                 if (IS_ERR(it))
2672                         /* unlink by force */
2673                         goto unlink;
2674
2675                 fid_zero(fid);
2676                 rc = iops->get(env, it, (const struct dt_key *)fid);
2677                 if (rc >= 0) {
2678                         rc = iops->next(env, it);
2679                         iops->put(env, it);
2680                 }
2681                 iops->fini(env, it);
2682                 if (rc > 0)
2683                         /* "rc > 0" means the index file is empty. */
2684                         RETURN(0);
2685
2686 unlink:
2687                 /* The old index is not empty, remove it firstly. */
2688                 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2689                 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2690                              "%s: unlink lfsck sub trace file %s: rc = %d\n",
2691                              lfsck_lfsck2name(com->lc_lfsck), name, rc);
2692                 if (rc)
2693                         RETURN(rc);
2694
2695                 if (*child) {
2696                         lfsck_object_put(env, *child);
2697                         *child = NULL;
2698                 }
2699         } else if (reset) {
2700                 goto unlink;
2701         }
2702
2703         obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2704                                          S_IFREG | S_IRUGO | S_IWUSR, ft);
2705         if (IS_ERR(obj))
2706                 RETURN(PTR_ERR(obj));
2707
2708         rc = obj->do_ops->do_index_try(env, obj, ft);
2709         if (rc) {
2710                 lfsck_object_put(env, obj);
2711                 CDEBUG(D_LFSCK, "%s: LFSCK fail to load "
2712                        "sub trace file %s: rc = %d\n",
2713                        lfsck_lfsck2name(com->lc_lfsck), name, rc);
2714         } else {
2715                 *child = obj;
2716         }
2717
2718         RETURN(rc);
2719 }
2720
2721 int lfsck_load_sub_trace_files(const struct lu_env *env,
2722                                struct lfsck_component *com,
2723                                const struct dt_index_features *ft,
2724                                const char *prefix, bool reset)
2725 {
2726         char *name = lfsck_env_info(env)->lti_key;
2727         struct lfsck_sub_trace_obj *lsto;
2728         int rc;
2729         int i;
2730
2731         for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2732              i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2733                 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2734                 rc = lfsck_load_one_trace_file(env, com,
2735                                 com->lc_lfsck->li_lfsck_dir,
2736                                 &lsto->lsto_obj, ft, name, reset);
2737         }
2738
2739         return rc;
2740 }
2741
2742 /* external interfaces */
2743 int lfsck_get_speed(char *buf, struct dt_device *key)
2744 {
2745         struct lu_env           env;
2746         struct lfsck_instance  *lfsck;
2747         int                     rc;
2748         ENTRY;
2749
2750         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2751         if (rc != 0)
2752                 RETURN(rc);
2753
2754         lfsck = lfsck_instance_find(key, true, false);
2755         if (lfsck && buf) {
2756                 rc = sprintf(buf, "%u\n",
2757                              lfsck->li_bookmark_ram.lb_speed_limit);
2758                 lfsck_instance_put(&env, lfsck);
2759         } else {
2760                 rc = -ENXIO;
2761         }
2762
2763         lu_env_fini(&env);
2764
2765         RETURN(rc);
2766 }
2767 EXPORT_SYMBOL(lfsck_get_speed);
2768
2769 int lfsck_set_speed(struct dt_device *key, __u32 val)
2770 {
2771         struct lu_env           env;
2772         struct lfsck_instance  *lfsck;
2773         int                     rc;
2774         ENTRY;
2775
2776         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2777         if (rc != 0)
2778                 RETURN(rc);
2779
2780         lfsck = lfsck_instance_find(key, true, false);
2781         if (likely(lfsck != NULL)) {
2782                 mutex_lock(&lfsck->li_mutex);
2783                 if (__lfsck_set_speed(lfsck, val))
2784                         rc = lfsck_bookmark_store(&env, lfsck);
2785                 mutex_unlock(&lfsck->li_mutex);
2786                 lfsck_instance_put(&env, lfsck);
2787         } else {
2788                 rc = -ENXIO;
2789         }
2790
2791         lu_env_fini(&env);
2792
2793         RETURN(rc);
2794 }
2795 EXPORT_SYMBOL(lfsck_set_speed);
2796
2797 int lfsck_get_windows(char *buf, struct dt_device *key)
2798 {
2799         struct lu_env           env;
2800         struct lfsck_instance  *lfsck;
2801         int                     rc;
2802         ENTRY;
2803
2804         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2805         if (rc != 0)
2806                 RETURN(rc);
2807
2808         lfsck = lfsck_instance_find(key, true, false);
2809         if (likely(lfsck != NULL)) {
2810                 rc = sprintf(buf, "%u\n",
2811                              lfsck->li_bookmark_ram.lb_async_windows);
2812                 lfsck_instance_put(&env, lfsck);
2813         } else {
2814                 rc = -ENXIO;
2815         }
2816
2817         lu_env_fini(&env);
2818
2819         RETURN(rc);
2820 }
2821 EXPORT_SYMBOL(lfsck_get_windows);
2822
2823 int lfsck_set_windows(struct dt_device *key, unsigned int val)
2824 {
2825         struct lu_env           env;
2826         struct lfsck_instance  *lfsck;
2827         int                     rc;
2828         ENTRY;
2829
2830         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2831         if (rc != 0)
2832                 RETURN(rc);
2833
2834         lfsck = lfsck_instance_find(key, true, false);
2835         if (likely(lfsck != NULL)) {
2836                 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2837                         CWARN("%s: invalid async windows size that may "
2838                               "cause memory issues. The valid range is "
2839                               "[1 - %u].\n",
2840                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2841                         rc = -EINVAL;
2842                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2843                         mutex_lock(&lfsck->li_mutex);
2844                         lfsck->li_bookmark_ram.lb_async_windows = val;
2845                         rc = lfsck_bookmark_store(&env, lfsck);
2846                         mutex_unlock(&lfsck->li_mutex);
2847                 }
2848                 lfsck_instance_put(&env, lfsck);
2849         } else {
2850                 rc = -ENXIO;
2851         }
2852
2853         lu_env_fini(&env);
2854
2855         RETURN(rc);
2856 }
2857 EXPORT_SYMBOL(lfsck_set_windows);
2858
2859 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2860 {
2861         struct lu_env           env;
2862         struct lfsck_instance  *lfsck;
2863         struct lfsck_component *com;
2864         int                     rc;
2865         ENTRY;
2866
2867         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2868         if (rc != 0)
2869                 RETURN(rc);
2870
2871         lfsck = lfsck_instance_find(key, true, false);
2872         if (likely(lfsck != NULL)) {
2873                 com = lfsck_component_find(lfsck, type);
2874                 if (likely(com != NULL)) {
2875                         com->lc_ops->lfsck_dump(&env, com, m);
2876                         lfsck_component_put(&env, com);
2877                 } else {
2878                         rc = -ENOTSUPP;
2879                 }
2880
2881                 lfsck_instance_put(&env, lfsck);
2882         } else {
2883                 rc = -ENXIO;
2884         }
2885
2886         lu_env_fini(&env);
2887
2888         RETURN(rc);
2889 }
2890 EXPORT_SYMBOL(lfsck_dump);
2891
2892 static int lfsck_stop_all(const struct lu_env *env,
2893                           struct lfsck_instance *lfsck,
2894                           struct lfsck_stop *stop)
2895 {
2896         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2897         struct lfsck_request              *lr     = &info->lti_lr;
2898         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2899         struct ptlrpc_request_set         *set;
2900         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2901         struct lfsck_tgt_desc             *ltd;
2902         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2903         __u32                              idx;
2904         int                                rc     = 0;
2905         int                                rc1    = 0;
2906         ENTRY;
2907
2908         LASSERT(stop->ls_flags & LPF_BROADCAST);
2909
2910         set = ptlrpc_prep_set();
2911         if (unlikely(set == NULL))
2912                 RETURN(-ENOMEM);
2913
2914         memset(lr, 0, sizeof(*lr));
2915         lr->lr_event = LE_STOP;
2916         lr->lr_index = lfsck_dev_idx(lfsck);
2917         lr->lr_status = stop->ls_status;
2918         lr->lr_version = bk->lb_version;
2919         lr->lr_active = LFSCK_TYPES_ALL;
2920         lr->lr_param = stop->ls_flags;
2921
2922         memset(laia, 0, sizeof(*laia));
2923         laia->laia_ltds = ltds;
2924         laia->laia_lr = lr;
2925         laia->laia_shared = 1;
2926
2927         down_read(&ltds->ltd_rw_sem);
2928         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2929                 ltd = lfsck_tgt_get(ltds, idx);
2930                 LASSERT(ltd != NULL);
2931
2932                 laia->laia_ltd = ltd;
2933                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2934                                          lfsck_async_interpret, laia,
2935                                          LFSCK_NOTIFY);
2936                 if (rc != 0) {
2937                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2938                         lfsck_tgt_put(ltd);
2939                         CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2940                                "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2941                         rc1 = rc;
2942                 }
2943         }
2944         up_read(&ltds->ltd_rw_sem);
2945
2946         rc = ptlrpc_set_wait(env, set);
2947         ptlrpc_set_destroy(set);
2948
2949         if (rc == 0)
2950                 rc = laia->laia_result;
2951
2952         if (rc == -EALREADY)
2953                 rc = 0;
2954
2955         if (rc != 0)
2956                 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2957                        lfsck_lfsck2name(lfsck), rc);
2958
2959         RETURN(rc != 0 ? rc : rc1);
2960 }
2961
2962 static int lfsck_start_all(const struct lu_env *env,
2963                            struct lfsck_instance *lfsck,
2964                            struct lfsck_start *start)
2965 {
2966         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2967         struct lfsck_request              *lr     = &info->lti_lr;
2968         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2969         struct ptlrpc_request_set         *set;
2970         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2971         struct lfsck_tgt_desc             *ltd;
2972         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2973         __u32                              idx;
2974         int                                rc     = 0;
2975         bool retry = false;
2976         ENTRY;
2977
2978         LASSERT(start->ls_flags & LPF_BROADCAST);
2979
2980         memset(lr, 0, sizeof(*lr));
2981         lr->lr_event = LE_START;
2982         lr->lr_index = lfsck_dev_idx(lfsck);
2983         lr->lr_speed = bk->lb_speed_limit;
2984         lr->lr_version = bk->lb_version;
2985         lr->lr_active = start->ls_active;
2986         lr->lr_param = start->ls_flags;
2987         lr->lr_async_windows = bk->lb_async_windows;
2988         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2989                        LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2990                        LSV_CREATE_MDTOBJ;
2991
2992         memset(laia, 0, sizeof(*laia));
2993         laia->laia_ltds = ltds;
2994         laia->laia_lr = lr;
2995         laia->laia_shared = 1;
2996
2997 again:
2998         set = ptlrpc_prep_set();
2999         if (unlikely(!set))
3000                 RETURN(-ENOMEM);
3001
3002         down_read(&ltds->ltd_rw_sem);
3003         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3004                 ltd = lfsck_tgt_get(ltds, idx);
3005                 LASSERT(ltd != NULL);
3006
3007                 if (retry && !ltd->ltd_retry_start) {
3008                         lfsck_tgt_put(ltd);
3009                         continue;
3010                 }
3011
3012                 laia->laia_ltd = ltd;
3013                 ltd->ltd_retry_start = 0;
3014                 ltd->ltd_layout_done = 0;
3015                 ltd->ltd_namespace_done = 0;
3016                 ltd->ltd_synced_failures = 0;
3017                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3018                                          lfsck_async_interpret, laia,
3019                                          LFSCK_NOTIFY);
3020                 if (rc != 0) {
3021                         lfsck_interpret(env, lfsck, NULL, laia, rc);
3022                         lfsck_tgt_put(ltd);
3023                         CERROR("%s: cannot notify MDT %x for LFSCK "
3024                                "start, failout: rc = %d\n",
3025                                lfsck_lfsck2name(lfsck), idx, rc);
3026                         break;
3027                 }
3028         }
3029         up_read(&ltds->ltd_rw_sem);
3030
3031         if (rc != 0) {
3032                 ptlrpc_set_destroy(set);
3033
3034                 RETURN(rc);
3035         }
3036
3037         rc = ptlrpc_set_wait(env, set);
3038         ptlrpc_set_destroy(set);
3039
3040         if (rc == 0)
3041                 rc = laia->laia_result;
3042
3043         if (unlikely(rc == -EINPROGRESS)) {
3044                 retry = true;
3045                 set_current_state(TASK_INTERRUPTIBLE);
3046                 schedule_timeout(cfs_time_seconds(1));
3047                 set_current_state(TASK_RUNNING);
3048                 if (!signal_pending(current) &&
3049                     thread_is_running(&lfsck->li_thread))
3050                         goto again;
3051
3052                 rc = -EINTR;
3053         }
3054
3055         if (rc != 0) {
3056                 struct lfsck_stop *stop = &info->lti_stop;
3057
3058                 CERROR("%s: cannot start LFSCK on some MDTs, "
3059                        "stop all: rc = %d\n",
3060                        lfsck_lfsck2name(lfsck), rc);
3061                 if (rc != -EALREADY) {
3062                         stop->ls_status = LS_FAILED;
3063                         stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3064                         lfsck_stop_all(env, lfsck, stop);
3065                 }
3066         }
3067
3068         RETURN(rc);
3069 }
3070
3071 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3072                 struct lfsck_start_param *lsp)
3073 {
3074         struct lfsck_start              *start  = lsp->lsp_start;
3075         struct lfsck_instance           *lfsck;
3076         struct lfsck_bookmark           *bk;
3077         struct ptlrpc_thread            *thread;
3078         struct lfsck_component          *com;
3079         struct l_wait_info               lwi    = { 0 };
3080         struct lfsck_thread_args        *lta;
3081         struct task_struct              *task;
3082         struct lfsck_tgt_descs          *ltds;
3083         struct lfsck_tgt_desc           *ltd;
3084         __u32                            idx;
3085         int                              rc     = 0;
3086         __u16                            valid  = 0;
3087         __u16                            flags  = 0;
3088         __u16                            type   = 1;
3089         ENTRY;
3090
3091         if (key->dd_rdonly)
3092                 RETURN(-EROFS);
3093
3094         lfsck = lfsck_instance_find(key, true, false);
3095         if (unlikely(lfsck == NULL))
3096                 RETURN(-ENXIO);
3097
3098         if (unlikely(lfsck->li_stopping))
3099                 GOTO(put, rc = -ENXIO);
3100
3101         /* System is not ready, try again later. */
3102         if (unlikely(lfsck->li_namespace == NULL ||
3103                      lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3104                 GOTO(put, rc = -EINPROGRESS);
3105
3106         /* start == NULL means auto trigger paused LFSCK. */
3107         if (!start) {
3108                 if (list_empty(&lfsck->li_list_scan) ||
3109                     OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3110                         GOTO(put, rc = 0);
3111         } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3112                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3113                        lfsck_lfsck2name(lfsck));
3114
3115                 GOTO(put, rc = -EPERM);
3116         }
3117
3118         bk = &lfsck->li_bookmark_ram;
3119         thread = &lfsck->li_thread;
3120         mutex_lock(&lfsck->li_mutex);
3121         spin_lock(&lfsck->li_lock);
3122         if (unlikely(thread_is_stopping(thread))) {
3123                 /* Someone is stopping the LFSCK. */
3124                 spin_unlock(&lfsck->li_lock);
3125                 GOTO(out, rc = -EBUSY);
3126         }
3127
3128         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3129                 rc = -EALREADY;
3130                 if (unlikely(start == NULL)) {
3131                         spin_unlock(&lfsck->li_lock);
3132                         GOTO(out, rc);
3133                 }
3134
3135                 while (start->ls_active != 0) {
3136                         if (!(type & start->ls_active)) {
3137                                 type <<= 1;
3138                                 continue;
3139                         }
3140
3141                         com = __lfsck_component_find(lfsck, type,
3142                                                      &lfsck->li_list_scan);
3143                         if (com == NULL)
3144                                 com = __lfsck_component_find(lfsck, type,
3145                                                 &lfsck->li_list_double_scan);
3146                         if (com == NULL) {
3147                                 rc = -EOPNOTSUPP;
3148                                 break;
3149                         }
3150
3151                         if (com->lc_ops->lfsck_join != NULL) {
3152                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
3153                                 if (rc != 0 && rc != -EALREADY)
3154                                         break;
3155                         }
3156                         start->ls_active &= ~type;
3157                         type <<= 1;
3158                 }
3159                 spin_unlock(&lfsck->li_lock);
3160                 GOTO(out, rc);
3161         }
3162         spin_unlock(&lfsck->li_lock);
3163
3164         lfsck->li_status = 0;
3165         lfsck->li_oit_over = 0;
3166         lfsck->li_start_unplug = 0;
3167         lfsck->li_drop_dryrun = 0;
3168         lfsck->li_new_scanned = 0;
3169
3170         /* For auto trigger. */
3171         if (start == NULL)
3172                 goto trigger;
3173
3174         start->ls_version = bk->lb_version;
3175
3176         if (start->ls_active != 0) {
3177                 struct lfsck_component *next;
3178
3179                 if (start->ls_active == LFSCK_TYPES_ALL)
3180                         start->ls_active = LFSCK_TYPES_SUPPORTED;
3181
3182                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3183                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3184                         GOTO(out, rc = -ENOTSUPP);
3185                 }
3186
3187                 list_for_each_entry_safe(com, next,
3188                                          &lfsck->li_list_scan, lc_link) {
3189                         if (!(com->lc_type & start->ls_active)) {
3190                                 rc = com->lc_ops->lfsck_post(env, com, 0,
3191                                                              false);
3192                                 if (rc != 0)
3193                                         GOTO(out, rc);
3194                         }
3195                 }
3196
3197                 while (start->ls_active != 0) {
3198                         if (type & start->ls_active) {
3199                                 com = __lfsck_component_find(lfsck, type,
3200                                                         &lfsck->li_list_idle);
3201                                 if (com != NULL)
3202                                         /* The component status will be updated
3203                                          * when its prep() is called later by
3204                                          * the LFSCK main engine. */
3205                                         list_move_tail(&com->lc_link,
3206                                                        &lfsck->li_list_scan);
3207                                 start->ls_active &= ~type;
3208                         }
3209                         type <<= 1;
3210                 }
3211         }
3212
3213         if (list_empty(&lfsck->li_list_scan)) {
3214                 /* The speed limit will be used to control both the LFSCK and
3215                  * low layer scrub (if applied), need to be handled firstly. */
3216                 if (start->ls_valid & LSV_SPEED_LIMIT) {
3217                         if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3218                                 rc = lfsck_bookmark_store(env, lfsck);
3219                                 if (rc != 0)
3220                                         GOTO(out, rc);
3221                         }
3222                 }
3223
3224                 goto trigger;
3225         }
3226
3227         if (start->ls_flags & LPF_RESET)
3228                 flags |= DOIF_RESET;
3229
3230         rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3231         if (rc != 0)
3232                 GOTO(out, rc);
3233
3234         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3235                 start->ls_active |= com->lc_type;
3236                 if (flags & DOIF_RESET) {
3237                         rc = com->lc_ops->lfsck_reset(env, com, false);
3238                         if (rc != 0)
3239                                 GOTO(out, rc);
3240                 }
3241         }
3242
3243         ltds = &lfsck->li_mdt_descs;
3244         down_read(&ltds->ltd_rw_sem);
3245         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3246                 ltd = lfsck_ltd2tgt(ltds, idx);
3247                 LASSERT(ltd != NULL);
3248
3249                 ltd->ltd_layout_done = 0;
3250                 ltd->ltd_namespace_done = 0;
3251                 ltd->ltd_synced_failures = 0;
3252                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3253                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3254                 list_del_init(&ltd->ltd_layout_phase_list);
3255                 list_del_init(&ltd->ltd_layout_list);
3256                 list_del_init(&ltd->ltd_namespace_phase_list);
3257                 list_del_init(&ltd->ltd_namespace_list);
3258         }
3259         up_read(&ltds->ltd_rw_sem);
3260
3261         ltds = &lfsck->li_ost_descs;
3262         down_read(&ltds->ltd_rw_sem);
3263         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3264                 ltd = lfsck_ltd2tgt(ltds, idx);
3265                 LASSERT(ltd != NULL);
3266
3267                 ltd->ltd_layout_done = 0;
3268                 ltd->ltd_synced_failures = 0;
3269                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3270                 list_del_init(&ltd->ltd_layout_phase_list);
3271                 list_del_init(&ltd->ltd_layout_list);
3272         }
3273         up_read(&ltds->ltd_rw_sem);
3274
3275 trigger:
3276         lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3277         if (bk->lb_param & LPF_DRYRUN)
3278                 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3279
3280         if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3281                 valid |= DOIV_ERROR_HANDLE;
3282                 if (start->ls_flags & LPF_FAILOUT)
3283                         flags |= DOIF_FAILOUT;
3284         }
3285
3286         if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3287                 valid |= DOIV_DRYRUN;
3288                 if (start->ls_flags & LPF_DRYRUN)
3289                         flags |= DOIF_DRYRUN;
3290         }
3291
3292         if (!list_empty(&lfsck->li_list_scan))
3293                 flags |= DOIF_OUTUSED;
3294
3295         lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3296         lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3297         if (IS_ERR(lta))
3298                 GOTO(out, rc = PTR_ERR(lta));
3299
3300         __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3301         spin_lock(&lfsck->li_lock);
3302         thread_set_flags(thread, SVC_STARTING);
3303         spin_unlock(&lfsck->li_lock);
3304         task = kthread_run(lfsck_master_engine, lta, "lfsck");
3305         if (IS_ERR(task)) {
3306                 rc = PTR_ERR(task);
3307                 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3308                        lfsck_lfsck2name(lfsck), rc);
3309                 lfsck_thread_args_fini(lta);
3310
3311                 GOTO(out, rc);
3312         }
3313
3314         l_wait_event(thread->t_ctl_waitq,
3315                      thread_is_running(thread) ||
3316                      thread_is_stopped(thread),
3317                      &lwi);
3318         if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3319                 lfsck->li_start_unplug = 1;
3320                 wake_up_all(&thread->t_ctl_waitq);
3321
3322                 GOTO(out, rc = 0);
3323         }
3324
3325         /* release lfsck::li_mutex to avoid deadlock. */
3326         mutex_unlock(&lfsck->li_mutex);
3327         rc = lfsck_start_all(env, lfsck, start);
3328         if (rc != 0) {
3329                 spin_lock(&lfsck->li_lock);
3330                 if (thread_is_stopped(thread)) {
3331                         spin_unlock(&lfsck->li_lock);
3332                 } else {
3333                         lfsck->li_status = LS_FAILED;
3334                         lfsck->li_flags = 0;
3335                         thread_set_flags(thread, SVC_STOPPING);
3336                         spin_unlock(&lfsck->li_lock);
3337
3338                         lfsck->li_start_unplug = 1;
3339                         wake_up_all(&thread->t_ctl_waitq);
3340                         l_wait_event(thread->t_ctl_waitq,
3341                                      thread_is_stopped(thread),
3342                                      &lwi);
3343                 }
3344         } else {
3345                 lfsck->li_start_unplug = 1;
3346                 wake_up_all(&thread->t_ctl_waitq);
3347         }
3348
3349         GOTO(put, rc);
3350
3351 out:
3352         mutex_unlock(&lfsck->li_mutex);
3353
3354 put:
3355         lfsck_instance_put(env, lfsck);
3356
3357         return rc < 0 ? rc : 0;
3358 }
3359 EXPORT_SYMBOL(lfsck_start);
3360
3361 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3362                struct lfsck_stop *stop)
3363 {
3364         struct lfsck_instance   *lfsck;
3365         struct ptlrpc_thread    *thread;
3366         struct l_wait_info       lwi    = { 0 };
3367         int                      rc     = 0;
3368         int                      rc1    = 0;
3369         ENTRY;
3370
3371         lfsck = lfsck_instance_find(key, true, false);
3372         if (unlikely(lfsck == NULL))
3373                 RETURN(-ENXIO);
3374
3375         thread = &lfsck->li_thread;
3376         if (stop && stop->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3377                 CERROR("%s: only allow to specify '-A' via MDS\n",
3378                        lfsck_lfsck2name(lfsck));
3379                 GOTO(put, rc = -EPERM);
3380         }
3381
3382         spin_lock(&lfsck->li_lock);
3383         /* The target is umounted */
3384         if (stop && stop->ls_status == LS_PAUSED)
3385                 lfsck->li_stopping = 1;
3386
3387         if (thread_is_init(thread) || thread_is_stopped(thread))
3388                 /* no error if LFSCK stopped already, or not started */
3389                 GOTO(unlock, rc = 0);
3390
3391         if (thread_is_stopping(thread))
3392                 /* Someone is stopping LFSCK. */
3393                 GOTO(unlock, rc = -EINPROGRESS);
3394
3395         if (stop) {
3396                 lfsck->li_status = stop->ls_status;
3397                 lfsck->li_flags = stop->ls_flags;
3398         } else {
3399                 lfsck->li_status = LS_STOPPED;
3400                 lfsck->li_flags = 0;
3401         }
3402
3403         thread_set_flags(thread, SVC_STOPPING);
3404
3405         LASSERT(lfsck->li_task != NULL);
3406         force_sig(SIGINT, lfsck->li_task);
3407
3408         if (lfsck->li_master) {
3409                 struct lfsck_component *com;
3410                 struct lfsck_assistant_data *lad;
3411
3412                 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3413                         lad = com->lc_data;
3414                         spin_lock(&lad->lad_lock);
3415                         if (lad->lad_task != NULL)
3416                                 force_sig(SIGINT, lad->lad_task);
3417                         spin_unlock(&lad->lad_lock);
3418                 }
3419
3420                 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3421                         lad = com->lc_data;
3422                         spin_lock(&lad->lad_lock);
3423                         if (lad->lad_task != NULL)
3424                                 force_sig(SIGINT, lad->lad_task);
3425                         spin_unlock(&lad->lad_lock);
3426                 }
3427         }
3428
3429         wake_up_all(&thread->t_ctl_waitq);
3430         spin_unlock(&lfsck->li_lock);
3431         if (stop && stop->ls_flags & LPF_BROADCAST)
3432                 rc1 = lfsck_stop_all(env, lfsck, stop);
3433
3434         /* It was me set the status as 'stopping' just now, if it is not
3435          * 'stopping' now, then either stopped, or re-started by race. */
3436         l_wait_event(thread->t_ctl_waitq,
3437                      !thread_is_stopping(thread),
3438                      &lwi);
3439
3440         GOTO(put, rc = 0);
3441
3442 unlock:
3443         spin_unlock(&lfsck->li_lock);
3444 put:
3445         lfsck_instance_put(env, lfsck);
3446
3447         return rc != 0 ? rc : rc1;
3448 }
3449 EXPORT_SYMBOL(lfsck_stop);
3450
3451 int lfsck_in_notify_local(const struct lu_env *env, struct dt_device *key,
3452                           struct lfsck_req_local *lrl, struct thandle *th)
3453 {
3454         struct lfsck_instance *lfsck;
3455         struct lfsck_component *com;
3456         int rc = -EOPNOTSUPP;
3457         ENTRY;
3458
3459         lfsck = lfsck_instance_find(key, true, false);
3460         if (unlikely(!lfsck))
3461                 RETURN(-ENXIO);
3462
3463         com = lfsck_component_find(lfsck, lrl->lrl_active);
3464         if (likely(com && com->lc_ops->lfsck_in_notify_local)) {
3465                 rc = com->lc_ops->lfsck_in_notify_local(env, com, lrl, th);
3466                 lfsck_component_put(env, com);
3467         }
3468
3469         lfsck_instance_put(env, lfsck);
3470
3471         RETURN(rc);
3472 }
3473 EXPORT_SYMBOL(lfsck_in_notify_local);
3474
3475 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3476                     struct lfsck_request *lr)
3477 {
3478         int rc = -EOPNOTSUPP;
3479         ENTRY;
3480
3481         switch (lr->lr_event) {
3482         case LE_START: {
3483                 struct lfsck_start       *start = &lfsck_env_info(env)->lti_start;
3484                 struct lfsck_start_param  lsp;
3485
3486                 memset(start, 0, sizeof(*start));
3487                 start->ls_valid = lr->lr_valid;
3488                 start->ls_speed_limit = lr->lr_speed;
3489                 start->ls_version = lr->lr_version;
3490                 start->ls_active = lr->lr_active;
3491                 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3492                 start->ls_async_windows = lr->lr_async_windows;
3493
3494                 lsp.lsp_start = start;
3495                 lsp.lsp_index = lr->lr_index;
3496                 lsp.lsp_index_valid = 1;
3497                 rc = lfsck_start(env, key, &lsp);
3498                 break;
3499         }
3500         case LE_STOP: {
3501                 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3502
3503                 memset(stop, 0, sizeof(*stop));
3504                 stop->ls_status = lr->lr_status;
3505                 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3506                 rc = lfsck_stop(env, key, stop);
3507                 break;
3508         }
3509         case LE_PHASE1_DONE:
3510         case LE_PHASE2_DONE:
3511         case LE_PEER_EXIT:
3512         case LE_CONDITIONAL_DESTROY:
3513         case LE_SET_LMV_MASTER:
3514         case LE_SET_LMV_SLAVE:
3515         case LE_PAIRS_VERIFY: {
3516                 struct lfsck_instance  *lfsck;
3517                 struct lfsck_component *com;
3518
3519                 lfsck = lfsck_instance_find(key, true, false);
3520                 if (unlikely(lfsck == NULL))
3521                         RETURN(-ENXIO);
3522
3523                 com = lfsck_component_find(lfsck, lr->lr_active);
3524                 if (likely(com)) {
3525                         rc = com->lc_ops->lfsck_in_notify(env, com, lr);
3526                         lfsck_component_put(env, com);
3527                 }
3528
3529                 lfsck_instance_put(env, lfsck);
3530                 break;
3531         }
3532         default:
3533                 break;
3534         }
3535
3536         RETURN(rc);
3537 }
3538 EXPORT_SYMBOL(lfsck_in_notify);
3539
3540 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3541                 struct lfsck_request *req, struct lfsck_reply *rep,
3542                 struct lfsck_query *que)
3543 {
3544         struct lfsck_instance  *lfsck;
3545         struct lfsck_component *com;
3546         int                     i;
3547         int                     rc = 0;
3548         __u16                   type;
3549         ENTRY;
3550
3551         lfsck = lfsck_instance_find(key, true, false);
3552         if (unlikely(lfsck == NULL))
3553                 RETURN(-ENXIO);
3554
3555         if (que != NULL) {
3556                 if (que->lu_types == LFSCK_TYPES_ALL)
3557                         que->lu_types =
3558                                 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3559
3560                 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3561                         que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3562
3563                         GOTO(out, rc = -ENOTSUPP);
3564                 }
3565
3566                 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3567                      i++, type = 1 << i) {
3568                         if (!(que->lu_types & type))
3569                                 continue;
3570
3571 again:
3572                         com = lfsck_component_find(lfsck, type);
3573                         if (unlikely(com == NULL))
3574                                 GOTO(out, rc = -ENOTSUPP);
3575
3576                         memset(que->lu_mdts_count[i], 0,
3577                                sizeof(__u32) * (LS_MAX + 1));
3578                         memset(que->lu_osts_count[i], 0,
3579                                sizeof(__u32) * (LS_MAX + 1));
3580                         que->lu_repaired[i] = 0;
3581                         rc = com->lc_ops->lfsck_query(env, com, req, rep,
3582                                                       que, i);
3583                         lfsck_component_put(env, com);
3584                         if  (rc < 0)
3585                                 GOTO(out, rc);
3586                 }
3587
3588                 if (!(que->lu_flags & LPF_WAIT))
3589                         GOTO(out, rc);
3590
3591                 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3592                      i++, type = 1 << i) {
3593                         if (!(que->lu_types & type))
3594                                 continue;
3595
3596                         if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3597                             que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3598                             que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3599                             que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3600                                 struct l_wait_info lwi;
3601
3602                                 /* If it is required to wait, then sleep
3603                                  * 3 seconds and try to query again. */
3604                                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
3605                                                        NULL,
3606                                                        LWI_ON_SIGNAL_NOOP,
3607                                                        NULL);
3608                                 rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
3609                                                   0, &lwi);
3610                                 if (rc == -ETIMEDOUT)
3611                                         goto again;
3612                         }
3613                 }
3614         } else {
3615                 com = lfsck_component_find(lfsck, req->lr_active);
3616                 if (likely(com != NULL)) {
3617                         rc = com->lc_ops->lfsck_query(env, com, req, rep,
3618                                                       que, -1);
3619                         lfsck_component_put(env, com);
3620                 } else {
3621                         rc = -ENOTSUPP;
3622                 }
3623         }
3624
3625         GOTO(out, rc);
3626
3627 out:
3628         lfsck_instance_put(env, lfsck);
3629         return rc;
3630 }
3631 EXPORT_SYMBOL(lfsck_query);
3632
3633 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3634                              struct ldlm_namespace *ns)
3635 {
3636         struct lfsck_instance  *lfsck;
3637         int                     rc      = -ENXIO;
3638
3639         lfsck = lfsck_instance_find(key, true, false);
3640         if (likely(lfsck != NULL)) {
3641                 lfsck->li_namespace = ns;
3642                 lfsck_instance_put(env, lfsck);
3643                 rc = 0;
3644         }
3645
3646         return rc;
3647 }
3648 EXPORT_SYMBOL(lfsck_register_namespace);
3649
3650 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3651                    struct dt_device *next, struct obd_device *obd,
3652                    lfsck_out_notify notify, void *notify_data, bool master)
3653 {
3654         struct lfsck_instance   *lfsck;
3655         struct dt_object        *root  = NULL;
3656         struct dt_object        *obj   = NULL;
3657         struct lu_fid           *fid   = &lfsck_env_info(env)->lti_fid;
3658         int                      rc;
3659         ENTRY;
3660
3661         lfsck = lfsck_instance_find(key, false, false);
3662         if (unlikely(lfsck != NULL))
3663                 RETURN(-EEXIST);
3664
3665         OBD_ALLOC_PTR(lfsck);
3666         if (lfsck == NULL)
3667                 RETURN(-ENOMEM);
3668
3669         mutex_init(&lfsck->li_mutex);
3670         spin_lock_init(&lfsck->li_lock);
3671         INIT_LIST_HEAD(&lfsck->li_link);
3672         INIT_LIST_HEAD(&lfsck->li_list_scan);
3673         INIT_LIST_HEAD(&lfsck->li_list_dir);
3674         INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3675         INIT_LIST_HEAD(&lfsck->li_list_idle);
3676         INIT_LIST_HEAD(&lfsck->li_list_lmv);
3677         atomic_set(&lfsck->li_ref, 1);
3678         atomic_set(&lfsck->li_double_scan_count, 0);
3679         init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3680         lfsck->li_out_notify = notify;
3681         lfsck->li_out_notify_data = notify_data;
3682         lfsck->li_next = next;
3683         lfsck->li_bottom = key;
3684         lfsck->li_obd = obd;
3685
3686         rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3687         if (rc != 0)
3688                 GOTO(out, rc);
3689
3690         rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3691         if (rc != 0)
3692                 GOTO(out, rc);
3693
3694         fid->f_seq = FID_SEQ_LOCAL_NAME;
3695         fid->f_oid = 1;
3696         fid->f_ver = 0;
3697         rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3698         if (rc != 0)
3699                 GOTO(out, rc);
3700
3701         rc = dt_root_get(env, key, fid);
3702         if (rc != 0)
3703                 GOTO(out, rc);
3704
3705         root = dt_locate(env, key, fid);
3706         if (IS_ERR(root))
3707                 GOTO(out, rc = PTR_ERR(root));
3708
3709         if (unlikely(!dt_try_as_dir(env, root)))
3710                 GOTO(out, rc = -ENOTDIR);
3711
3712         lfsck->li_local_root_fid = *fid;
3713         if (master) {
3714                 lfsck->li_master = 1;
3715                 if (lfsck_dev_idx(lfsck) == 0) {
3716                         struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3717                         const struct lu_name *cname;
3718
3719                         rc = dt_lookup(env, root,
3720                                 (struct dt_rec *)(&lfsck->li_global_root_fid),
3721                                 (const struct dt_key *)"ROOT");
3722                         if (rc != 0)
3723                                 GOTO(out, rc);
3724
3725                         obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3726                         if (IS_ERR(obj))
3727                                 GOTO(out, rc = PTR_ERR(obj));
3728
3729                         if (unlikely(!dt_try_as_dir(env, obj)))
3730                                 GOTO(out, rc = -ENOTDIR);
3731
3732                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3733                                 (const struct dt_key *)dotlustre);
3734                         if (rc != 0)
3735                                 GOTO(out, rc);
3736
3737                         lfsck_object_put(env, obj);
3738                         obj = dt_locate(env, key, fid);
3739                         if (IS_ERR(obj))
3740                                 GOTO(out, rc = PTR_ERR(obj));
3741
3742                         cname = lfsck_name_get_const(env, dotlustre,
3743                                                      strlen(dotlustre));
3744                         rc = lfsck_verify_linkea(env, obj, cname,
3745                                                  &lfsck->li_global_root_fid);
3746                         if (rc != 0)
3747                                 GOTO(out, rc);
3748
3749                         if (unlikely(!dt_try_as_dir(env, obj)))
3750                                 GOTO(out, rc = -ENOTDIR);
3751
3752                         *pfid = *fid;
3753                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3754                                        (const struct dt_key *)lostfound);
3755                         if (rc != 0)
3756                                 GOTO(out, rc);
3757
3758                         lfsck_object_put(env, obj);
3759                         obj = dt_locate(env, key, fid);
3760                         if (IS_ERR(obj))
3761                                 GOTO(out, rc = PTR_ERR(obj));
3762
3763                         cname = lfsck_name_get_const(env, lostfound,
3764                                                      strlen(lostfound));
3765                         rc = lfsck_verify_linkea(env, obj, cname, pfid);
3766                         if (rc != 0)
3767                                 GOTO(out, rc);
3768
3769                         lfsck_object_put(env, obj);
3770                         obj = NULL;
3771                 }
3772         }
3773
3774         fid->f_seq = FID_SEQ_LOCAL_FILE;
3775         fid->f_oid = OTABLE_IT_OID;
3776         fid->f_ver = 0;
3777         obj = dt_locate(env, key, fid);
3778         if (IS_ERR(obj))
3779                 GOTO(out, rc = PTR_ERR(obj));
3780
3781         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3782         if (rc != 0)
3783                 GOTO(out, rc);
3784
3785         lfsck->li_obj_oit = obj;
3786         obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3787                                         S_IFDIR | S_IRUGO | S_IWUSR);
3788         if (IS_ERR(obj))
3789                 GOTO(out, rc = PTR_ERR(obj));
3790
3791         lu_object_get(&obj->do_lu);
3792         lfsck->li_lfsck_dir = obj;
3793         rc = lfsck_bookmark_setup(env, lfsck);
3794         if (rc != 0)
3795                 GOTO(out, rc);
3796
3797         if (master) {
3798                 rc = lfsck_fid_init(lfsck);
3799                 if (rc < 0)
3800                         GOTO(out, rc);
3801
3802                 rc = lfsck_namespace_setup(env, lfsck);
3803                 if (rc < 0)
3804                         GOTO(out, rc);
3805         }
3806
3807         rc = lfsck_layout_setup(env, lfsck);
3808         if (rc < 0)
3809                 GOTO(out, rc);
3810
3811         /* XXX: more LFSCK components initialization to be added here. */
3812
3813         rc = lfsck_instance_add(lfsck);
3814         if (rc == 0)
3815                 rc = lfsck_add_target_from_orphan(env, lfsck);
3816 out:
3817         if (obj != NULL && !IS_ERR(obj))
3818                 lfsck_object_put(env, obj);
3819         if (root != NULL && !IS_ERR(root))
3820                 lfsck_object_put(env, root);
3821         if (rc != 0)
3822                 lfsck_instance_cleanup(env, lfsck);
3823         return rc;
3824 }
3825 EXPORT_SYMBOL(lfsck_register);
3826
3827 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3828 {
3829         struct lfsck_instance *lfsck;
3830
3831         lfsck = lfsck_instance_find(key, false, true);
3832         if (lfsck != NULL)
3833                 lfsck_instance_put(env, lfsck);
3834 }
3835 EXPORT_SYMBOL(lfsck_degister);
3836
3837 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3838                      struct dt_device *tgt, struct obd_export *exp,
3839                      __u32 index, bool for_ost)
3840 {
3841         struct lfsck_instance   *lfsck;
3842         struct lfsck_tgt_desc   *ltd;
3843         int                      rc;
3844         ENTRY;
3845
3846         OBD_ALLOC_PTR(ltd);
3847         if (ltd == NULL)
3848                 RETURN(-ENOMEM);
3849
3850         ltd->ltd_tgt = tgt;
3851         ltd->ltd_key = key;
3852         ltd->ltd_exp = exp;
3853         INIT_LIST_HEAD(&ltd->ltd_orphan_list);
3854         INIT_LIST_HEAD(&ltd->ltd_layout_list);
3855         INIT_LIST_HEAD(&ltd->ltd_layout_phase_list);
3856         INIT_LIST_HEAD(&ltd->ltd_namespace_list);
3857         INIT_LIST_HEAD(&ltd->ltd_namespace_phase_list);
3858         atomic_set(&ltd->ltd_ref, 1);
3859         ltd->ltd_index = index;
3860
3861         spin_lock(&lfsck_instance_lock);
3862         lfsck = __lfsck_instance_find(key, true, false);
3863         if (lfsck == NULL) {
3864                 if (for_ost)
3865                         list_add_tail(&ltd->ltd_orphan_list,
3866                                       &lfsck_ost_orphan_list);
3867                 else
3868                         list_add_tail(&ltd->ltd_orphan_list,
3869                                       &lfsck_mdt_orphan_list);
3870                 spin_unlock(&lfsck_instance_lock);
3871
3872                 RETURN(0);
3873         }
3874         spin_unlock(&lfsck_instance_lock);
3875
3876         rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3877         if (rc != 0)
3878                 lfsck_tgt_put(ltd);
3879
3880         lfsck_instance_put(env, lfsck);
3881
3882         RETURN(rc);
3883 }
3884 EXPORT_SYMBOL(lfsck_add_target);
3885
3886 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3887                       struct dt_device *tgt, __u32 index, bool for_ost)
3888 {
3889         struct lfsck_instance   *lfsck;
3890         struct lfsck_tgt_descs  *ltds;
3891         struct lfsck_tgt_desc   *ltd;
3892         struct list_head        *head;
3893
3894         if (for_ost)
3895                 head = &lfsck_ost_orphan_list;
3896         else
3897                 head = &lfsck_mdt_orphan_list;
3898
3899         spin_lock(&lfsck_instance_lock);
3900         list_for_each_entry(ltd, head, ltd_orphan_list) {
3901                 if (ltd->ltd_tgt == tgt) {
3902                         list_del_init(&ltd->ltd_orphan_list);
3903                         spin_unlock(&lfsck_instance_lock);
3904                         lfsck_tgt_put(ltd);
3905
3906                         return;
3907                 }
3908         }
3909
3910         ltd = NULL;
3911         lfsck = __lfsck_instance_find(key, true, false);
3912         spin_unlock(&lfsck_instance_lock);
3913         if (unlikely(lfsck == NULL))
3914                 return;
3915
3916         if (for_ost)
3917                 ltds = &lfsck->li_ost_descs;
3918         else
3919                 ltds = &lfsck->li_mdt_descs;
3920
3921         down_write(&ltds->ltd_rw_sem);
3922         LASSERT(ltds->ltd_tgts_bitmap != NULL);
3923
3924         if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3925                 goto unlock;
3926
3927         ltd = lfsck_ltd2tgt(ltds, index);
3928         if (unlikely(ltd == NULL))
3929                 goto unlock;
3930
3931         LASSERT(ltds->ltd_tgtnr > 0);
3932
3933         ltds->ltd_tgtnr--;
3934         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3935         lfsck_assign_tgt(ltds, NULL, index);
3936
3937 unlock:
3938         if (ltd == NULL) {
3939                 if (for_ost)
3940                         head = &lfsck->li_ost_descs.ltd_orphan;
3941                 else
3942                         head = &lfsck->li_mdt_descs.ltd_orphan;
3943
3944                 list_for_each_entry(ltd, head, ltd_orphan_list) {
3945                         if (ltd->ltd_tgt == tgt) {
3946                                 list_del_init(&ltd->ltd_orphan_list);
3947                                 break;
3948                         }
3949                 }
3950         }
3951
3952         up_write(&ltds->ltd_rw_sem);
3953         if (ltd != NULL) {
3954                 spin_lock(&ltds->ltd_lock);
3955                 ltd->ltd_dead = 1;
3956                 spin_unlock(&ltds->ltd_lock);
3957                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3958                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3959                 lfsck_tgt_put(ltd);
3960         }
3961
3962         lfsck_instance_put(env, lfsck);
3963 }
3964 EXPORT_SYMBOL(lfsck_del_target);
3965
3966 static int __init lfsck_init(void)
3967 {
3968         int rc;
3969
3970         INIT_LIST_HEAD(&lfsck_instance_list);
3971         INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3972         INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3973         lfsck_key_init_generic(&lfsck_thread_key, NULL);
3974         rc = lu_context_key_register(&lfsck_thread_key);
3975         if (!rc) {
3976                 tgt_register_lfsck_in_notify_local(lfsck_in_notify_local);
3977                 tgt_register_lfsck_in_notify(lfsck_in_notify);
3978                 tgt_register_lfsck_query(lfsck_query);
3979         }
3980
3981         return rc;
3982 }
3983
3984 static void __exit lfsck_exit(void)
3985 {
3986         struct lfsck_tgt_desc *ltd;
3987         struct lfsck_tgt_desc *next;
3988
3989         LASSERT(list_empty(&lfsck_instance_list));
3990
3991         list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3992                                  ltd_orphan_list) {
3993                 list_del_init(&ltd->ltd_orphan_list);
3994                 lfsck_tgt_put(ltd);
3995         }
3996
3997         list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3998                                  ltd_orphan_list) {
3999                 list_del_init(&ltd->ltd_orphan_list);
4000                 lfsck_tgt_put(ltd);
4001         }
4002
4003         lu_context_key_degister(&lfsck_thread_key);
4004 }
4005
4006 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
4007 MODULE_DESCRIPTION("Lustre File System Checker");
4008 MODULE_VERSION(LUSTRE_VERSION_STRING);
4009 MODULE_LICENSE("GPL");
4010
4011 module_init(lfsck_init);
4012 module_exit(lfsck_exit);