Whamcloud - gitweb
LU-10406 mdt: invalidate cache upon LDLM lock
[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 > msecs_to_jiffies(MSEC_PER_SEC)) {
1845                         lfsck->li_sleep_rate = limit /
1846                                                msecs_to_jiffies(MSEC_PER_SEC);
1847                         lfsck->li_sleep_jif = 1;
1848                 } else {
1849                         lfsck->li_sleep_rate = 1;
1850                         lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1851                                               limit;
1852                 }
1853         } else {
1854                 lfsck->li_sleep_jif = 0;
1855                 lfsck->li_sleep_rate = 0;
1856         }
1857
1858         if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1859                 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1860                 dirty = true;
1861         }
1862
1863         return dirty;
1864 }
1865
1866 void lfsck_control_speed(struct lfsck_instance *lfsck)
1867 {
1868         struct ptlrpc_thread *thread = &lfsck->li_thread;
1869         struct l_wait_info    lwi;
1870
1871         if (lfsck->li_sleep_jif > 0 &&
1872             lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1873                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1874                                        LWI_ON_SIGNAL_NOOP, NULL);
1875
1876                 l_wait_event(thread->t_ctl_waitq,
1877                              !thread_is_running(thread),
1878                              &lwi);
1879                 lfsck->li_new_scanned = 0;
1880         }
1881 }
1882
1883 void lfsck_control_speed_by_self(struct lfsck_component *com)
1884 {
1885         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1886         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1887         struct l_wait_info       lwi;
1888
1889         if (lfsck->li_sleep_jif > 0 &&
1890             com->lc_new_scanned >= lfsck->li_sleep_rate) {
1891                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1892                                        LWI_ON_SIGNAL_NOOP, NULL);
1893
1894                 l_wait_event(thread->t_ctl_waitq,
1895                              !thread_is_running(thread),
1896                              &lwi);
1897                 com->lc_new_scanned = 0;
1898         }
1899 }
1900
1901 static struct lfsck_thread_args *
1902 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1903                        struct lfsck_component *com,
1904                        struct lfsck_start_param *lsp)
1905 {
1906         struct lfsck_thread_args *lta;
1907         int                       rc;
1908
1909         OBD_ALLOC_PTR(lta);
1910         if (lta == NULL)
1911                 return ERR_PTR(-ENOMEM);
1912
1913         rc = lu_env_init(&lta->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1914         if (rc != 0) {
1915                 OBD_FREE_PTR(lta);
1916                 return ERR_PTR(rc);
1917         }
1918
1919         lta->lta_lfsck = lfsck_instance_get(lfsck);
1920         if (com != NULL)
1921                 lta->lta_com = lfsck_component_get(com);
1922
1923         lta->lta_lsp = lsp;
1924
1925         return lta;
1926 }
1927
1928 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1929 {
1930         if (lta->lta_com != NULL)
1931                 lfsck_component_put(&lta->lta_env, lta->lta_com);
1932         lfsck_instance_put(&lta->lta_env, lta->lta_lfsck);
1933         lu_env_fini(&lta->lta_env);
1934         OBD_FREE_PTR(lta);
1935 }
1936
1937 struct lfsck_assistant_data *
1938 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1939                           const char *name)
1940 {
1941         struct lfsck_assistant_data *lad;
1942
1943         OBD_ALLOC_PTR(lad);
1944         if (lad != NULL) {
1945                 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1946                 if (lad->lad_bitmap == NULL) {
1947                         OBD_FREE_PTR(lad);
1948                         return NULL;
1949                 }
1950
1951                 INIT_LIST_HEAD(&lad->lad_req_list);
1952                 spin_lock_init(&lad->lad_lock);
1953                 INIT_LIST_HEAD(&lad->lad_ost_list);
1954                 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1955                 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1956                 INIT_LIST_HEAD(&lad->lad_mdt_list);
1957                 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1958                 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1959                 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1960                 lad->lad_ops = lao;
1961                 lad->lad_name = name;
1962         }
1963
1964         return lad;
1965 }
1966
1967 struct lfsck_assistant_object *
1968 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1969                             const struct lu_attr *attr, __u64 cookie,
1970                             bool is_dir)
1971 {
1972         struct lfsck_assistant_object   *lso;
1973
1974         OBD_ALLOC_PTR(lso);
1975         if (lso == NULL)
1976                 return ERR_PTR(-ENOMEM);
1977
1978         lso->lso_fid = *fid;
1979         if (attr != NULL)
1980                 lso->lso_attr = *attr;
1981
1982         atomic_set(&lso->lso_ref, 1);
1983         lso->lso_oit_cookie = cookie;
1984         if (is_dir)
1985                 lso->lso_is_dir = 1;
1986
1987         return lso;
1988 }
1989
1990 struct dt_object *
1991 lfsck_assistant_object_load(const struct lu_env *env,
1992                             struct lfsck_instance *lfsck,
1993                             struct lfsck_assistant_object *lso)
1994 {
1995         struct dt_object *obj;
1996
1997         obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
1998         if (IS_ERR(obj))
1999                 return obj;
2000
2001         if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2002                 lso->lso_dead = 1;
2003                 lfsck_object_put(env, obj);
2004
2005                 return ERR_PTR(-ENOENT);
2006         }
2007
2008         if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2009                 lfsck_object_put(env, obj);
2010
2011                 return ERR_PTR(-ENOTDIR);
2012         }
2013
2014         return obj;
2015 }
2016
2017 /**
2018  * Generic LFSCK asynchronous communication interpretor function.
2019  * The LFSCK RPC reply for both the event notification and status
2020  * querying will be handled here.
2021  *
2022  * \param[in] env       pointer to the thread context
2023  * \param[in] req       pointer to the LFSCK request
2024  * \param[in] args      pointer to the lfsck_async_interpret_args
2025  * \param[in] rc        the result for handling the LFSCK request
2026  *
2027  * \retval              0 for success
2028  * \retval              negative error number on failure
2029  */
2030 int lfsck_async_interpret_common(const struct lu_env *env,
2031                                  struct ptlrpc_request *req,
2032                                  void *args, int rc)
2033 {
2034         struct lfsck_async_interpret_args *laia = args;
2035         struct lfsck_component            *com  = laia->laia_com;
2036         struct lfsck_assistant_data       *lad  = com->lc_data;
2037         struct lfsck_tgt_descs            *ltds = laia->laia_ltds;
2038         struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
2039         struct lfsck_request              *lr   = laia->laia_lr;
2040
2041         LASSERT(com->lc_lfsck->li_master);
2042
2043         switch (lr->lr_event) {
2044         case LE_START:
2045                 if (unlikely(rc == -EINPROGRESS)) {
2046                         ltd->ltd_retry_start = 1;
2047                         break;
2048                 }
2049
2050                 if (rc != 0) {
2051                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2052                                "start: rc = %d\n",
2053                                lfsck_lfsck2name(com->lc_lfsck),
2054                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2055                                ltd->ltd_index, lad->lad_name, rc);
2056
2057                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2058                                 struct lfsck_layout *lo = com->lc_file_ram;
2059
2060                                 if (lr->lr_flags & LEF_TO_OST)
2061                                         lfsck_lad_set_bitmap(env, com,
2062                                                              ltd->ltd_index);
2063                                 else
2064                                         lo->ll_flags |= LF_INCOMPLETE;
2065                         } else {
2066                                 struct lfsck_namespace *ns = com->lc_file_ram;
2067
2068                                 /* If some MDT does not join the namespace
2069                                  * LFSCK, then we cannot know whether there
2070                                  * is some name entry on such MDT that with
2071                                  * the referenced MDT-object on this MDT or
2072                                  * not. So the namespace LFSCK on this MDT
2073                                  * cannot handle orphan MDT-objects properly.
2074                                  * So we mark the LFSCK as LF_INCOMPLETE and
2075                                  * skip orphan MDT-objects handling. */
2076                                 ns->ln_flags |= LF_INCOMPLETE;
2077                         }
2078                         break;
2079                 }
2080
2081                 spin_lock(&ltds->ltd_lock);
2082                 if (ltd->ltd_dead) {
2083                         spin_unlock(&ltds->ltd_lock);
2084                         break;
2085                 }
2086
2087                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2088                         struct list_head *list;
2089                         struct list_head *phase_list;
2090
2091                         if (ltd->ltd_layout_done) {
2092                                 spin_unlock(&ltds->ltd_lock);
2093                                 break;
2094                         }
2095
2096                         if (lr->lr_flags & LEF_TO_OST) {
2097                                 list = &lad->lad_ost_list;
2098                                 phase_list = &lad->lad_ost_phase1_list;
2099                         } else {
2100                                 list = &lad->lad_mdt_list;
2101                                 phase_list = &lad->lad_mdt_phase1_list;
2102                         }
2103
2104                         if (list_empty(&ltd->ltd_layout_list))
2105                                 list_add_tail(&ltd->ltd_layout_list, list);
2106                         if (list_empty(&ltd->ltd_layout_phase_list))
2107                                 list_add_tail(&ltd->ltd_layout_phase_list,
2108                                               phase_list);
2109                 } else {
2110                         if (ltd->ltd_namespace_done) {
2111                                 spin_unlock(&ltds->ltd_lock);
2112                                 break;
2113                         }
2114
2115                         if (list_empty(&ltd->ltd_namespace_list))
2116                                 list_add_tail(&ltd->ltd_namespace_list,
2117                                               &lad->lad_mdt_list);
2118                         if (list_empty(&ltd->ltd_namespace_phase_list))
2119                                 list_add_tail(&ltd->ltd_namespace_phase_list,
2120                                               &lad->lad_mdt_phase1_list);
2121                 }
2122                 spin_unlock(&ltds->ltd_lock);
2123                 break;
2124         case LE_STOP:
2125         case LE_PHASE1_DONE:
2126         case LE_PHASE2_DONE:
2127         case LE_PEER_EXIT:
2128                 if (rc != 0 && rc != -EALREADY)
2129                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2130                               "event = %d, rc = %d\n",
2131                               lfsck_lfsck2name(com->lc_lfsck),
2132                               (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2133                               ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2134                 break;
2135         case LE_QUERY: {
2136                 struct lfsck_reply *reply;
2137                 struct list_head *list;
2138                 struct list_head *phase_list;
2139
2140                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2141                         list = &ltd->ltd_layout_list;
2142                         phase_list = &ltd->ltd_layout_phase_list;
2143                 } else {
2144                         list = &ltd->ltd_namespace_list;
2145                         phase_list = &ltd->ltd_namespace_phase_list;
2146                 }
2147
2148                 if (rc != 0) {
2149                         if (lr->lr_flags & LEF_QUERY_ALL) {
2150                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2151                                 break;
2152                         }
2153
2154                         spin_lock(&ltds->ltd_lock);
2155                         list_del_init(phase_list);
2156                         list_del_init(list);
2157                         spin_unlock(&ltds->ltd_lock);
2158                         break;
2159                 }
2160
2161                 reply = req_capsule_server_get(&req->rq_pill,
2162                                                &RMF_LFSCK_REPLY);
2163                 if (reply == NULL) {
2164                         rc = -EPROTO;
2165                         CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2166                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2167                                lad->lad_name, rc);
2168
2169                         if (lr->lr_flags & LEF_QUERY_ALL) {
2170                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2171                                 break;
2172                         }
2173
2174                         spin_lock(&ltds->ltd_lock);
2175                         list_del_init(phase_list);
2176                         list_del_init(list);
2177                         spin_unlock(&ltds->ltd_lock);
2178                         break;
2179                 }
2180
2181                 if (lr->lr_flags & LEF_QUERY_ALL) {
2182                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2183                                 ltd->ltd_layout_status = reply->lr_status;
2184                                 ltd->ltd_layout_repaired = reply->lr_repaired;
2185                         } else {
2186                                 ltd->ltd_namespace_status = reply->lr_status;
2187                                 ltd->ltd_namespace_repaired =
2188                                                         reply->lr_repaired;
2189                         }
2190                         break;
2191                 }
2192
2193                 switch (reply->lr_status) {
2194                 case LS_SCANNING_PHASE1:
2195                         break;
2196                 case LS_SCANNING_PHASE2:
2197                         spin_lock(&ltds->ltd_lock);
2198                         list_del_init(phase_list);
2199                         if (ltd->ltd_dead) {
2200                                 spin_unlock(&ltds->ltd_lock);
2201                                 break;
2202                         }
2203
2204                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2205                                 if (ltd->ltd_layout_done) {
2206                                         spin_unlock(&ltds->ltd_lock);
2207                                         break;
2208                                 }
2209
2210                                 if (lr->lr_flags & LEF_TO_OST)
2211                                         list_add_tail(phase_list,
2212                                                 &lad->lad_ost_phase2_list);
2213                                 else
2214                                         list_add_tail(phase_list,
2215                                                 &lad->lad_mdt_phase2_list);
2216                         } else {
2217                                 if (ltd->ltd_namespace_done) {
2218                                         spin_unlock(&ltds->ltd_lock);
2219                                         break;
2220                                 }
2221
2222                                 list_add_tail(phase_list,
2223                                               &lad->lad_mdt_phase2_list);
2224                         }
2225                         spin_unlock(&ltds->ltd_lock);
2226                         break;
2227                 default:
2228                         spin_lock(&ltds->ltd_lock);
2229                         list_del_init(phase_list);
2230                         list_del_init(list);
2231                         spin_unlock(&ltds->ltd_lock);
2232                         break;
2233                 }
2234                 break;
2235         }
2236         default:
2237                 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2238                        lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2239                 break;
2240         }
2241
2242         if (!laia->laia_shared) {
2243                 lfsck_tgt_put(ltd);
2244                 lfsck_component_put(env, com);
2245         }
2246
2247         return 0;
2248 }
2249
2250 static void lfsck_interpret(const struct lu_env *env,
2251                             struct lfsck_instance *lfsck,
2252                             struct ptlrpc_request *req, void *args, int result)
2253 {
2254         struct lfsck_async_interpret_args *laia = args;
2255         struct lfsck_component            *com;
2256
2257         LASSERT(laia->laia_com == NULL);
2258         LASSERT(laia->laia_shared);
2259
2260         spin_lock(&lfsck->li_lock);
2261         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2262                 laia->laia_com = com;
2263                 lfsck_async_interpret_common(env, req, laia, result);
2264         }
2265
2266         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2267                 laia->laia_com = com;
2268                 lfsck_async_interpret_common(env, req, laia, result);
2269         }
2270         spin_unlock(&lfsck->li_lock);
2271 }
2272
2273 static int lfsck_stop_notify(const struct lu_env *env,
2274                              struct lfsck_instance *lfsck,
2275                              struct lfsck_tgt_descs *ltds,
2276                              struct lfsck_tgt_desc *ltd, __u16 type)
2277 {
2278         struct lfsck_component *com;
2279         int                     rc = 0;
2280         ENTRY;
2281
2282         LASSERT(lfsck->li_master);
2283
2284         spin_lock(&lfsck->li_lock);
2285         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2286         if (com == NULL)
2287                 com = __lfsck_component_find(lfsck, type,
2288                                              &lfsck->li_list_double_scan);
2289         if (com != NULL)
2290                 lfsck_component_get(com);
2291         spin_unlock(&lfsck->li_lock);
2292
2293         if (com != NULL) {
2294                 struct lfsck_thread_info          *info  = lfsck_env_info(env);
2295                 struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2296                 struct lfsck_request              *lr    = &info->lti_lr;
2297                 struct lfsck_assistant_data       *lad   = com->lc_data;
2298                 struct list_head                  *list;
2299                 struct list_head                  *phase_list;
2300                 struct ptlrpc_request_set         *set;
2301
2302                 set = ptlrpc_prep_set();
2303                 if (set == NULL) {
2304                         lfsck_component_put(env, com);
2305
2306                         RETURN(-ENOMEM);
2307                 }
2308
2309                 if (type == LFSCK_TYPE_LAYOUT) {
2310                         list = &ltd->ltd_layout_list;
2311                         phase_list = &ltd->ltd_layout_phase_list;
2312                 } else {
2313                         list = &ltd->ltd_namespace_list;
2314                         phase_list = &ltd->ltd_namespace_phase_list;
2315                 }
2316
2317                 spin_lock(&ltds->ltd_lock);
2318                 if (list_empty(list)) {
2319                         LASSERT(list_empty(phase_list));
2320                         spin_unlock(&ltds->ltd_lock);
2321                         ptlrpc_set_destroy(set);
2322
2323                         RETURN(0);
2324                 }
2325
2326                 list_del_init(phase_list);
2327                 list_del_init(list);
2328                 spin_unlock(&ltds->ltd_lock);
2329
2330                 memset(lr, 0, sizeof(*lr));
2331                 lr->lr_index = lfsck_dev_idx(lfsck);
2332                 lr->lr_event = LE_PEER_EXIT;
2333                 lr->lr_active = type;
2334                 lr->lr_status = LS_CO_PAUSED;
2335                 if (ltds == &lfsck->li_ost_descs)
2336                         lr->lr_flags = LEF_TO_OST;
2337
2338                 memset(laia, 0, sizeof(*laia));
2339                 laia->laia_com = com;
2340                 laia->laia_ltds = ltds;
2341                 atomic_inc(&ltd->ltd_ref);
2342                 laia->laia_ltd = ltd;
2343                 laia->laia_lr = lr;
2344
2345                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2346                                          lfsck_async_interpret_common,
2347                                          laia, LFSCK_NOTIFY);
2348                 if (rc != 0) {
2349                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2350                                "co-stop for %s: rc = %d\n",
2351                                lfsck_lfsck2name(lfsck),
2352                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2353                                ltd->ltd_index, lad->lad_name, rc);
2354                         lfsck_tgt_put(ltd);
2355                 } else {
2356                         rc = ptlrpc_set_wait(env, set);
2357                 }
2358
2359                 ptlrpc_set_destroy(set);
2360                 lfsck_component_put(env, com);
2361         }
2362
2363         RETURN(rc);
2364 }
2365
2366 static int lfsck_async_interpret(const struct lu_env *env,
2367                                  struct ptlrpc_request *req,
2368                                  void *args, int rc)
2369 {
2370         struct lfsck_async_interpret_args *laia = args;
2371         struct lfsck_instance             *lfsck;
2372
2373         lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2374                               li_mdt_descs);
2375         lfsck_interpret(env, lfsck, req, laia, rc);
2376         lfsck_tgt_put(laia->laia_ltd);
2377         if (rc != 0 && laia->laia_result != -EALREADY)
2378                 laia->laia_result = rc;
2379
2380         return 0;
2381 }
2382
2383 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2384                         struct lfsck_request *lr,
2385                         struct ptlrpc_request_set *set,
2386                         ptlrpc_interpterer_t interpreter,
2387                         void *args, int request)
2388 {
2389         struct lfsck_async_interpret_args *laia;
2390         struct ptlrpc_request             *req;
2391         struct lfsck_request              *tmp;
2392         struct req_format                 *format;
2393         int                                rc;
2394
2395         switch (request) {
2396         case LFSCK_NOTIFY:
2397                 format = &RQF_LFSCK_NOTIFY;
2398                 break;
2399         case LFSCK_QUERY:
2400                 format = &RQF_LFSCK_QUERY;
2401                 break;
2402         default:
2403                 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2404                        exp->exp_obd->obd_name, request, -EINVAL);
2405                 return -EINVAL;
2406         }
2407
2408         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2409         if (req == NULL)
2410                 return -ENOMEM;
2411
2412         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2413         if (rc != 0) {
2414                 ptlrpc_request_free(req);
2415
2416                 return rc;
2417         }
2418
2419         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2420         *tmp = *lr;
2421         ptlrpc_request_set_replen(req);
2422
2423         laia = ptlrpc_req_async_args(req);
2424         *laia = *(struct lfsck_async_interpret_args *)args;
2425         if (laia->laia_com != NULL)
2426                 lfsck_component_get(laia->laia_com);
2427         req->rq_interpret_reply = interpreter;
2428         req->rq_allow_intr = 1;
2429         req->rq_no_delay = 1;
2430         ptlrpc_set_add_req(set, req);
2431
2432         return 0;
2433 }
2434
2435 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2436 {
2437         struct lfsck_thread_info          *info  = lfsck_env_info(env);
2438         struct lfsck_request              *lr    = &info->lti_lr;
2439         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2440         struct lfsck_instance             *lfsck = com->lc_lfsck;
2441         struct lfsck_tgt_descs            *ltds  = &lfsck->li_mdt_descs;
2442         struct lfsck_tgt_desc             *ltd;
2443         struct ptlrpc_request_set         *set;
2444         int                                idx;
2445         int                                rc;
2446         ENTRY;
2447
2448         memset(lr, 0, sizeof(*lr));
2449         lr->lr_event = LE_QUERY;
2450         lr->lr_active = com->lc_type;
2451         lr->lr_flags = LEF_QUERY_ALL;
2452
2453         memset(laia, 0, sizeof(*laia));
2454         laia->laia_com = com;
2455         laia->laia_lr = lr;
2456
2457         set = ptlrpc_prep_set();
2458         if (set == NULL)
2459                 RETURN(-ENOMEM);
2460
2461 again:
2462         laia->laia_ltds = ltds;
2463         down_read(&ltds->ltd_rw_sem);
2464         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2465                 ltd = lfsck_tgt_get(ltds, idx);
2466                 LASSERT(ltd != NULL);
2467
2468                 laia->laia_ltd = ltd;
2469                 up_read(&ltds->ltd_rw_sem);
2470                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2471                                          lfsck_async_interpret_common,
2472                                          laia, LFSCK_QUERY);
2473                 if (rc != 0) {
2474                         struct lfsck_assistant_data *lad = com->lc_data;
2475
2476                         CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2477                                "rc = %d\n", lfsck_lfsck2name(lfsck),
2478                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2479                                ltd->ltd_index, lad->lad_name, rc);
2480                         lfsck_reset_ltd_status(ltd, com->lc_type);
2481                         lfsck_tgt_put(ltd);
2482                 }
2483                 down_read(&ltds->ltd_rw_sem);
2484         }
2485         up_read(&ltds->ltd_rw_sem);
2486
2487         if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2488                 ltds = &lfsck->li_ost_descs;
2489                 lr->lr_flags |= LEF_TO_OST;
2490                 goto again;
2491         }
2492
2493         rc = ptlrpc_set_wait(env, set);
2494         ptlrpc_set_destroy(set);
2495
2496         RETURN(rc);
2497 }
2498
2499 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2500                           struct lfsck_start_param *lsp)
2501 {
2502         struct lfsck_instance           *lfsck   = com->lc_lfsck;
2503         struct lfsck_assistant_data     *lad     = com->lc_data;
2504         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
2505         struct ptlrpc_thread            *athread = &lad->lad_thread;
2506         struct lfsck_thread_args        *lta;
2507         struct task_struct              *task;
2508         int                              rc;
2509         ENTRY;
2510
2511         lad->lad_assistant_status = 0;
2512         lad->lad_post_result = 0;
2513         lad->lad_to_post = 0;
2514         lad->lad_to_double_scan = 0;
2515         lad->lad_in_double_scan = 0;
2516         lad->lad_exit = 0;
2517         lad->lad_advance_lock = false;
2518         thread_set_flags(athread, 0);
2519
2520         lta = lfsck_thread_args_init(lfsck, com, lsp);
2521         if (IS_ERR(lta))
2522                 RETURN(PTR_ERR(lta));
2523
2524         task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2525         if (IS_ERR(task)) {
2526                 rc = PTR_ERR(task);
2527                 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2528                        "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2529                 lfsck_thread_args_fini(lta);
2530         } else {
2531                 struct l_wait_info lwi = { 0 };
2532
2533                 l_wait_event(mthread->t_ctl_waitq,
2534                              thread_is_running(athread) ||
2535                              thread_is_stopped(athread) ||
2536                              !thread_is_starting(mthread),
2537                              &lwi);
2538                 if (unlikely(!thread_is_starting(mthread)))
2539                         /* stopped by race */
2540                         rc = -ESRCH;
2541                 else if (unlikely(!thread_is_running(athread)))
2542                         rc = lad->lad_assistant_status;
2543                 else
2544                         rc = 0;
2545         }
2546
2547         RETURN(rc);
2548 }
2549
2550 int lfsck_checkpoint_generic(const struct lu_env *env,
2551                              struct lfsck_component *com)
2552 {
2553         struct lfsck_assistant_data     *lad     = com->lc_data;
2554         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2555         struct ptlrpc_thread            *athread = &lad->lad_thread;
2556         struct l_wait_info               lwi     = { 0 };
2557
2558         l_wait_event(mthread->t_ctl_waitq,
2559                      list_empty(&lad->lad_req_list) ||
2560                      !thread_is_running(mthread) ||
2561                      thread_is_stopped(athread),
2562                      &lwi);
2563
2564         if (!thread_is_running(mthread) || thread_is_stopped(athread))
2565                 return LFSCK_CHECKPOINT_SKIP;
2566
2567         return 0;
2568 }
2569
2570 void lfsck_post_generic(const struct lu_env *env,
2571                         struct lfsck_component *com, int *result)
2572 {
2573         struct lfsck_assistant_data     *lad     = com->lc_data;
2574         struct ptlrpc_thread            *athread = &lad->lad_thread;
2575         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2576         struct l_wait_info               lwi     = { 0 };
2577
2578         lad->lad_post_result = *result;
2579         if (*result <= 0)
2580                 lad->lad_exit = 1;
2581         lad->lad_to_post = 1;
2582
2583         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2584                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2585
2586         wake_up_all(&athread->t_ctl_waitq);
2587         l_wait_event(mthread->t_ctl_waitq,
2588                      (*result > 0 && list_empty(&lad->lad_req_list)) ||
2589                      thread_is_stopped(athread),
2590                      &lwi);
2591
2592         if (lad->lad_assistant_status < 0)
2593                 *result = lad->lad_assistant_status;
2594
2595         CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2596                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2597 }
2598
2599 int lfsck_double_scan_generic(const struct lu_env *env,
2600                               struct lfsck_component *com, int status)
2601 {
2602         struct lfsck_assistant_data     *lad     = com->lc_data;
2603         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2604         struct ptlrpc_thread            *athread = &lad->lad_thread;
2605         struct l_wait_info               lwi     = { 0 };
2606
2607         if (status != LS_SCANNING_PHASE2)
2608                 lad->lad_exit = 1;
2609         else
2610                 lad->lad_to_double_scan = 1;
2611
2612         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2613                "status %d\n",
2614                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2615
2616         wake_up_all(&athread->t_ctl_waitq);
2617         l_wait_event(mthread->t_ctl_waitq,
2618                      lad->lad_in_double_scan ||
2619                      thread_is_stopped(athread),
2620                      &lwi);
2621
2622         CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2623                "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2624                lad->lad_assistant_status);
2625
2626         if (lad->lad_assistant_status < 0)
2627                 return lad->lad_assistant_status;
2628
2629         return 0;
2630 }
2631
2632 void lfsck_quit_generic(const struct lu_env *env,
2633                         struct lfsck_component *com)
2634 {
2635         struct lfsck_assistant_data     *lad     = com->lc_data;
2636         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2637         struct ptlrpc_thread            *athread = &lad->lad_thread;
2638         struct l_wait_info               lwi     = { 0 };
2639
2640         lad->lad_exit = 1;
2641         wake_up_all(&athread->t_ctl_waitq);
2642         l_wait_event(mthread->t_ctl_waitq,
2643                      thread_is_init(athread) ||
2644                      thread_is_stopped(athread),
2645                      &lwi);
2646 }
2647
2648 int lfsck_load_one_trace_file(const struct lu_env *env,
2649                               struct lfsck_component *com,
2650                               struct dt_object *parent,
2651                               struct dt_object **child,
2652                               const struct dt_index_features *ft,
2653                               const char *name, bool reset)
2654 {
2655         struct lfsck_instance *lfsck = com->lc_lfsck;
2656         struct dt_object *obj;
2657         int rc;
2658         ENTRY;
2659
2660         if (*child != NULL) {
2661                 struct dt_it *it;
2662                 const struct dt_it_ops *iops;
2663                 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2664
2665                 if (!reset)
2666                         RETURN(0);
2667
2668                 obj = *child;
2669                 rc = obj->do_ops->do_index_try(env, obj, ft);
2670                 if (rc)
2671                         /* unlink by force */
2672                         goto unlink;
2673
2674                 iops = &obj->do_index_ops->dio_it;
2675                 it = iops->init(env, obj, 0);
2676                 if (IS_ERR(it))
2677                         /* unlink by force */
2678                         goto unlink;
2679
2680                 fid_zero(fid);
2681                 rc = iops->get(env, it, (const struct dt_key *)fid);
2682                 if (rc >= 0) {
2683                         rc = iops->next(env, it);
2684                         iops->put(env, it);
2685                 }
2686                 iops->fini(env, it);
2687                 if (rc > 0)
2688                         /* "rc > 0" means the index file is empty. */
2689                         RETURN(0);
2690
2691 unlink:
2692                 /* The old index is not empty, remove it firstly. */
2693                 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2694                 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2695                              "%s: unlink lfsck sub trace file %s: rc = %d\n",
2696                              lfsck_lfsck2name(com->lc_lfsck), name, rc);
2697                 if (rc)
2698                         RETURN(rc);
2699
2700                 if (*child) {
2701                         lfsck_object_put(env, *child);
2702                         *child = NULL;
2703                 }
2704         } else if (reset) {
2705                 goto unlink;
2706         }
2707
2708         obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2709                                          S_IFREG | S_IRUGO | S_IWUSR, ft);
2710         if (IS_ERR(obj))
2711                 RETURN(PTR_ERR(obj));
2712
2713         rc = obj->do_ops->do_index_try(env, obj, ft);
2714         if (rc) {
2715                 lfsck_object_put(env, obj);
2716                 CDEBUG(D_LFSCK, "%s: LFSCK fail to load "
2717                        "sub trace file %s: rc = %d\n",
2718                        lfsck_lfsck2name(com->lc_lfsck), name, rc);
2719         } else {
2720                 *child = obj;
2721         }
2722
2723         RETURN(rc);
2724 }
2725
2726 int lfsck_load_sub_trace_files(const struct lu_env *env,
2727                                struct lfsck_component *com,
2728                                const struct dt_index_features *ft,
2729                                const char *prefix, bool reset)
2730 {
2731         char *name = lfsck_env_info(env)->lti_key;
2732         struct lfsck_sub_trace_obj *lsto;
2733         int rc;
2734         int i;
2735
2736         for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2737              i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2738                 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2739                 rc = lfsck_load_one_trace_file(env, com,
2740                                 com->lc_lfsck->li_lfsck_dir,
2741                                 &lsto->lsto_obj, ft, name, reset);
2742         }
2743
2744         return rc;
2745 }
2746
2747 /* external interfaces */
2748 int lfsck_get_speed(struct seq_file *m, char *buf, struct dt_device *key)
2749 {
2750         struct lu_env           env;
2751         struct lfsck_instance  *lfsck;
2752         int                     rc;
2753         ENTRY;
2754
2755         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2756         if (rc != 0)
2757                 RETURN(rc);
2758
2759         lfsck = lfsck_instance_find(key, true, false);
2760         if (likely(lfsck != NULL)) {
2761                 if (m) {
2762                         seq_printf(m, "%u\n",
2763                                    lfsck->li_bookmark_ram.lb_speed_limit);
2764                 } else if (buf) {
2765                         rc = sprintf(buf, "%u\n",
2766                                      lfsck->li_bookmark_ram.lb_speed_limit);
2767                 }
2768                 lfsck_instance_put(&env, lfsck);
2769         } else {
2770                 rc = -ENXIO;
2771         }
2772
2773         lu_env_fini(&env);
2774
2775         RETURN(rc);
2776 }
2777 EXPORT_SYMBOL(lfsck_get_speed);
2778
2779 int lfsck_set_speed(struct dt_device *key, __u32 val)
2780 {
2781         struct lu_env           env;
2782         struct lfsck_instance  *lfsck;
2783         int                     rc;
2784         ENTRY;
2785
2786         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2787         if (rc != 0)
2788                 RETURN(rc);
2789
2790         lfsck = lfsck_instance_find(key, true, false);
2791         if (likely(lfsck != NULL)) {
2792                 mutex_lock(&lfsck->li_mutex);
2793                 if (__lfsck_set_speed(lfsck, val))
2794                         rc = lfsck_bookmark_store(&env, lfsck);
2795                 mutex_unlock(&lfsck->li_mutex);
2796                 lfsck_instance_put(&env, lfsck);
2797         } else {
2798                 rc = -ENXIO;
2799         }
2800
2801         lu_env_fini(&env);
2802
2803         RETURN(rc);
2804 }
2805 EXPORT_SYMBOL(lfsck_set_speed);
2806
2807 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2808 {
2809         struct lu_env           env;
2810         struct lfsck_instance  *lfsck;
2811         int                     rc;
2812         ENTRY;
2813
2814         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2815         if (rc != 0)
2816                 RETURN(rc);
2817
2818         lfsck = lfsck_instance_find(key, true, false);
2819         if (likely(lfsck != NULL)) {
2820                 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2821                 lfsck_instance_put(&env, lfsck);
2822         } else {
2823                 rc = -ENXIO;
2824         }
2825
2826         lu_env_fini(&env);
2827
2828         RETURN(rc);
2829 }
2830 EXPORT_SYMBOL(lfsck_get_windows);
2831
2832 int lfsck_set_windows(struct dt_device *key, unsigned int val)
2833 {
2834         struct lu_env           env;
2835         struct lfsck_instance  *lfsck;
2836         int                     rc;
2837         ENTRY;
2838
2839         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2840         if (rc != 0)
2841                 RETURN(rc);
2842
2843         lfsck = lfsck_instance_find(key, true, false);
2844         if (likely(lfsck != NULL)) {
2845                 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2846                         CWARN("%s: invalid async windows size that may "
2847                               "cause memory issues. The valid range is "
2848                               "[1 - %u].\n",
2849                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2850                         rc = -EINVAL;
2851                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2852                         mutex_lock(&lfsck->li_mutex);
2853                         lfsck->li_bookmark_ram.lb_async_windows = val;
2854                         rc = lfsck_bookmark_store(&env, lfsck);
2855                         mutex_unlock(&lfsck->li_mutex);
2856                 }
2857                 lfsck_instance_put(&env, lfsck);
2858         } else {
2859                 rc = -ENXIO;
2860         }
2861
2862         lu_env_fini(&env);
2863
2864         RETURN(rc);
2865 }
2866 EXPORT_SYMBOL(lfsck_set_windows);
2867
2868 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2869 {
2870         struct lu_env           env;
2871         struct lfsck_instance  *lfsck;
2872         struct lfsck_component *com;
2873         int                     rc;
2874         ENTRY;
2875
2876         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2877         if (rc != 0)
2878                 RETURN(rc);
2879
2880         lfsck = lfsck_instance_find(key, true, false);
2881         if (likely(lfsck != NULL)) {
2882                 com = lfsck_component_find(lfsck, type);
2883                 if (likely(com != NULL)) {
2884                         com->lc_ops->lfsck_dump(&env, com, m);
2885                         lfsck_component_put(&env, com);
2886                 } else {
2887                         rc = -ENOTSUPP;
2888                 }
2889
2890                 lfsck_instance_put(&env, lfsck);
2891         } else {
2892                 rc = -ENXIO;
2893         }
2894
2895         lu_env_fini(&env);
2896
2897         RETURN(rc);
2898 }
2899 EXPORT_SYMBOL(lfsck_dump);
2900
2901 static int lfsck_stop_all(const struct lu_env *env,
2902                           struct lfsck_instance *lfsck,
2903                           struct lfsck_stop *stop)
2904 {
2905         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2906         struct lfsck_request              *lr     = &info->lti_lr;
2907         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2908         struct ptlrpc_request_set         *set;
2909         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2910         struct lfsck_tgt_desc             *ltd;
2911         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2912         __u32                              idx;
2913         int                                rc     = 0;
2914         int                                rc1    = 0;
2915         ENTRY;
2916
2917         LASSERT(stop->ls_flags & LPF_BROADCAST);
2918
2919         set = ptlrpc_prep_set();
2920         if (unlikely(set == NULL))
2921                 RETURN(-ENOMEM);
2922
2923         memset(lr, 0, sizeof(*lr));
2924         lr->lr_event = LE_STOP;
2925         lr->lr_index = lfsck_dev_idx(lfsck);
2926         lr->lr_status = stop->ls_status;
2927         lr->lr_version = bk->lb_version;
2928         lr->lr_active = LFSCK_TYPES_ALL;
2929         lr->lr_param = stop->ls_flags;
2930
2931         memset(laia, 0, sizeof(*laia));
2932         laia->laia_ltds = ltds;
2933         laia->laia_lr = lr;
2934         laia->laia_shared = 1;
2935
2936         down_read(&ltds->ltd_rw_sem);
2937         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2938                 ltd = lfsck_tgt_get(ltds, idx);
2939                 LASSERT(ltd != NULL);
2940
2941                 laia->laia_ltd = ltd;
2942                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2943                                          lfsck_async_interpret, laia,
2944                                          LFSCK_NOTIFY);
2945                 if (rc != 0) {
2946                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2947                         lfsck_tgt_put(ltd);
2948                         CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2949                                "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2950                         rc1 = rc;
2951                 }
2952         }
2953         up_read(&ltds->ltd_rw_sem);
2954
2955         rc = ptlrpc_set_wait(env, set);
2956         ptlrpc_set_destroy(set);
2957
2958         if (rc == 0)
2959                 rc = laia->laia_result;
2960
2961         if (rc == -EALREADY)
2962                 rc = 0;
2963
2964         if (rc != 0)
2965                 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2966                        lfsck_lfsck2name(lfsck), rc);
2967
2968         RETURN(rc != 0 ? rc : rc1);
2969 }
2970
2971 static int lfsck_start_all(const struct lu_env *env,
2972                            struct lfsck_instance *lfsck,
2973                            struct lfsck_start *start)
2974 {
2975         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2976         struct lfsck_request              *lr     = &info->lti_lr;
2977         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2978         struct ptlrpc_request_set         *set;
2979         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2980         struct lfsck_tgt_desc             *ltd;
2981         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2982         __u32                              idx;
2983         int                                rc     = 0;
2984         bool retry = false;
2985         ENTRY;
2986
2987         LASSERT(start->ls_flags & LPF_BROADCAST);
2988
2989         memset(lr, 0, sizeof(*lr));
2990         lr->lr_event = LE_START;
2991         lr->lr_index = lfsck_dev_idx(lfsck);
2992         lr->lr_speed = bk->lb_speed_limit;
2993         lr->lr_version = bk->lb_version;
2994         lr->lr_active = start->ls_active;
2995         lr->lr_param = start->ls_flags;
2996         lr->lr_async_windows = bk->lb_async_windows;
2997         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2998                        LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2999                        LSV_CREATE_MDTOBJ;
3000
3001         memset(laia, 0, sizeof(*laia));
3002         laia->laia_ltds = ltds;
3003         laia->laia_lr = lr;
3004         laia->laia_shared = 1;
3005
3006 again:
3007         set = ptlrpc_prep_set();
3008         if (unlikely(!set))
3009                 RETURN(-ENOMEM);
3010
3011         down_read(&ltds->ltd_rw_sem);
3012         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3013                 ltd = lfsck_tgt_get(ltds, idx);
3014                 LASSERT(ltd != NULL);
3015
3016                 if (retry && !ltd->ltd_retry_start) {
3017                         lfsck_tgt_put(ltd);
3018                         continue;
3019                 }
3020
3021                 laia->laia_ltd = ltd;
3022                 ltd->ltd_retry_start = 0;
3023                 ltd->ltd_layout_done = 0;
3024                 ltd->ltd_namespace_done = 0;
3025                 ltd->ltd_synced_failures = 0;
3026                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3027                                          lfsck_async_interpret, laia,
3028                                          LFSCK_NOTIFY);
3029                 if (rc != 0) {
3030                         lfsck_interpret(env, lfsck, NULL, laia, rc);
3031                         lfsck_tgt_put(ltd);
3032                         CERROR("%s: cannot notify MDT %x for LFSCK "
3033                                "start, failout: rc = %d\n",
3034                                lfsck_lfsck2name(lfsck), idx, rc);
3035                         break;
3036                 }
3037         }
3038         up_read(&ltds->ltd_rw_sem);
3039
3040         if (rc != 0) {
3041                 ptlrpc_set_destroy(set);
3042
3043                 RETURN(rc);
3044         }
3045
3046         rc = ptlrpc_set_wait(env, set);
3047         ptlrpc_set_destroy(set);
3048
3049         if (rc == 0)
3050                 rc = laia->laia_result;
3051
3052         if (unlikely(rc == -EINPROGRESS)) {
3053                 retry = true;
3054                 set_current_state(TASK_INTERRUPTIBLE);
3055                 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC));
3056                 set_current_state(TASK_RUNNING);
3057                 if (!signal_pending(current) &&
3058                     thread_is_running(&lfsck->li_thread))
3059                         goto again;
3060
3061                 rc = -EINTR;
3062         }
3063
3064         if (rc != 0) {
3065                 struct lfsck_stop *stop = &info->lti_stop;
3066
3067                 CERROR("%s: cannot start LFSCK on some MDTs, "
3068                        "stop all: rc = %d\n",
3069                        lfsck_lfsck2name(lfsck), rc);
3070                 if (rc != -EALREADY) {
3071                         stop->ls_status = LS_FAILED;
3072                         stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3073                         lfsck_stop_all(env, lfsck, stop);
3074                 }
3075         }
3076
3077         RETURN(rc);
3078 }
3079
3080 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3081                 struct lfsck_start_param *lsp)
3082 {
3083         struct lfsck_start              *start  = lsp->lsp_start;
3084         struct lfsck_instance           *lfsck;
3085         struct lfsck_bookmark           *bk;
3086         struct ptlrpc_thread            *thread;
3087         struct lfsck_component          *com;
3088         struct l_wait_info               lwi    = { 0 };
3089         struct lfsck_thread_args        *lta;
3090         struct task_struct              *task;
3091         struct lfsck_tgt_descs          *ltds;
3092         struct lfsck_tgt_desc           *ltd;
3093         __u32                            idx;
3094         int                              rc     = 0;
3095         __u16                            valid  = 0;
3096         __u16                            flags  = 0;
3097         __u16                            type   = 1;
3098         ENTRY;
3099
3100         if (key->dd_rdonly)
3101                 RETURN(-EROFS);
3102
3103         lfsck = lfsck_instance_find(key, true, false);
3104         if (unlikely(lfsck == NULL))
3105                 RETURN(-ENXIO);
3106
3107         if (unlikely(lfsck->li_stopping))
3108                 GOTO(put, rc = -ENXIO);
3109
3110         /* System is not ready, try again later. */
3111         if (unlikely(lfsck->li_namespace == NULL ||
3112                      lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3113                 GOTO(put, rc = -EINPROGRESS);
3114
3115         /* start == NULL means auto trigger paused LFSCK. */
3116         if (!start) {
3117                 if (list_empty(&lfsck->li_list_scan) ||
3118                     OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3119                         GOTO(put, rc = 0);
3120         } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3121                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3122                        lfsck_lfsck2name(lfsck));
3123
3124                 GOTO(put, rc = -EPERM);
3125         }
3126
3127         bk = &lfsck->li_bookmark_ram;
3128         thread = &lfsck->li_thread;
3129         mutex_lock(&lfsck->li_mutex);
3130         spin_lock(&lfsck->li_lock);
3131         if (unlikely(thread_is_stopping(thread))) {
3132                 /* Someone is stopping the LFSCK. */
3133                 spin_unlock(&lfsck->li_lock);
3134                 GOTO(out, rc = -EBUSY);
3135         }
3136
3137         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3138                 rc = -EALREADY;
3139                 if (unlikely(start == NULL)) {
3140                         spin_unlock(&lfsck->li_lock);
3141                         GOTO(out, rc);
3142                 }
3143
3144                 while (start->ls_active != 0) {
3145                         if (!(type & start->ls_active)) {
3146                                 type <<= 1;
3147                                 continue;
3148                         }
3149
3150                         com = __lfsck_component_find(lfsck, type,
3151                                                      &lfsck->li_list_scan);
3152                         if (com == NULL)
3153                                 com = __lfsck_component_find(lfsck, type,
3154                                                 &lfsck->li_list_double_scan);
3155                         if (com == NULL) {
3156                                 rc = -EOPNOTSUPP;
3157                                 break;
3158                         }
3159
3160                         if (com->lc_ops->lfsck_join != NULL) {
3161                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
3162                                 if (rc != 0 && rc != -EALREADY)
3163                                         break;
3164                         }
3165                         start->ls_active &= ~type;
3166                         type <<= 1;
3167                 }
3168                 spin_unlock(&lfsck->li_lock);
3169                 GOTO(out, rc);
3170         }
3171         spin_unlock(&lfsck->li_lock);
3172
3173         lfsck->li_status = 0;
3174         lfsck->li_oit_over = 0;
3175         lfsck->li_start_unplug = 0;
3176         lfsck->li_drop_dryrun = 0;
3177         lfsck->li_new_scanned = 0;
3178
3179         /* For auto trigger. */
3180         if (start == NULL)
3181                 goto trigger;
3182
3183         start->ls_version = bk->lb_version;
3184
3185         if (start->ls_active != 0) {
3186                 struct lfsck_component *next;
3187
3188                 if (start->ls_active == LFSCK_TYPES_ALL)
3189                         start->ls_active = LFSCK_TYPES_SUPPORTED;
3190
3191                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3192                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3193                         GOTO(out, rc = -ENOTSUPP);
3194                 }
3195
3196                 list_for_each_entry_safe(com, next,
3197                                          &lfsck->li_list_scan, lc_link) {
3198                         if (!(com->lc_type & start->ls_active)) {
3199                                 rc = com->lc_ops->lfsck_post(env, com, 0,
3200                                                              false);
3201                                 if (rc != 0)
3202                                         GOTO(out, rc);
3203                         }
3204                 }
3205
3206                 while (start->ls_active != 0) {
3207                         if (type & start->ls_active) {
3208                                 com = __lfsck_component_find(lfsck, type,
3209                                                         &lfsck->li_list_idle);
3210                                 if (com != NULL)
3211                                         /* The component status will be updated
3212                                          * when its prep() is called later by
3213                                          * the LFSCK main engine. */
3214                                         list_move_tail(&com->lc_link,
3215                                                        &lfsck->li_list_scan);
3216                                 start->ls_active &= ~type;
3217                         }
3218                         type <<= 1;
3219                 }
3220         }
3221
3222         if (list_empty(&lfsck->li_list_scan)) {
3223                 /* The speed limit will be used to control both the LFSCK and
3224                  * low layer scrub (if applied), need to be handled firstly. */
3225                 if (start->ls_valid & LSV_SPEED_LIMIT) {
3226                         if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3227                                 rc = lfsck_bookmark_store(env, lfsck);
3228                                 if (rc != 0)
3229                                         GOTO(out, rc);
3230                         }
3231                 }
3232
3233                 goto trigger;
3234         }
3235
3236         if (start->ls_flags & LPF_RESET)
3237                 flags |= DOIF_RESET;
3238
3239         rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3240         if (rc != 0)
3241                 GOTO(out, rc);
3242
3243         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3244                 start->ls_active |= com->lc_type;
3245                 if (flags & DOIF_RESET) {
3246                         rc = com->lc_ops->lfsck_reset(env, com, false);
3247                         if (rc != 0)
3248                                 GOTO(out, rc);
3249                 }
3250         }
3251
3252         ltds = &lfsck->li_mdt_descs;
3253         down_read(&ltds->ltd_rw_sem);
3254         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3255                 ltd = lfsck_ltd2tgt(ltds, idx);
3256                 LASSERT(ltd != NULL);
3257
3258                 ltd->ltd_layout_done = 0;
3259                 ltd->ltd_namespace_done = 0;
3260                 ltd->ltd_synced_failures = 0;
3261                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3262                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3263                 list_del_init(&ltd->ltd_layout_phase_list);
3264                 list_del_init(&ltd->ltd_layout_list);
3265                 list_del_init(&ltd->ltd_namespace_phase_list);
3266                 list_del_init(&ltd->ltd_namespace_list);
3267         }
3268         up_read(&ltds->ltd_rw_sem);
3269
3270         ltds = &lfsck->li_ost_descs;
3271         down_read(&ltds->ltd_rw_sem);
3272         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3273                 ltd = lfsck_ltd2tgt(ltds, idx);
3274                 LASSERT(ltd != NULL);
3275
3276                 ltd->ltd_layout_done = 0;
3277                 ltd->ltd_synced_failures = 0;
3278                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3279                 list_del_init(&ltd->ltd_layout_phase_list);
3280                 list_del_init(&ltd->ltd_layout_list);
3281         }
3282         up_read(&ltds->ltd_rw_sem);
3283
3284 trigger:
3285         lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3286         if (bk->lb_param & LPF_DRYRUN)
3287                 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3288
3289         if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3290                 valid |= DOIV_ERROR_HANDLE;
3291                 if (start->ls_flags & LPF_FAILOUT)
3292                         flags |= DOIF_FAILOUT;
3293         }
3294
3295         if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3296                 valid |= DOIV_DRYRUN;
3297                 if (start->ls_flags & LPF_DRYRUN)
3298                         flags |= DOIF_DRYRUN;
3299         }
3300
3301         if (!list_empty(&lfsck->li_list_scan))
3302                 flags |= DOIF_OUTUSED;
3303
3304         lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3305         lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3306         if (IS_ERR(lta))
3307                 GOTO(out, rc = PTR_ERR(lta));
3308
3309         __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3310         spin_lock(&lfsck->li_lock);
3311         thread_set_flags(thread, SVC_STARTING);
3312         spin_unlock(&lfsck->li_lock);
3313         task = kthread_run(lfsck_master_engine, lta, "lfsck");
3314         if (IS_ERR(task)) {
3315                 rc = PTR_ERR(task);
3316                 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3317                        lfsck_lfsck2name(lfsck), rc);
3318                 lfsck_thread_args_fini(lta);
3319
3320                 GOTO(out, rc);
3321         }
3322
3323         l_wait_event(thread->t_ctl_waitq,
3324                      thread_is_running(thread) ||
3325                      thread_is_stopped(thread),
3326                      &lwi);
3327         if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3328                 lfsck->li_start_unplug = 1;
3329                 wake_up_all(&thread->t_ctl_waitq);
3330
3331                 GOTO(out, rc = 0);
3332         }
3333
3334         /* release lfsck::li_mutex to avoid deadlock. */
3335         mutex_unlock(&lfsck->li_mutex);
3336         rc = lfsck_start_all(env, lfsck, start);
3337         if (rc != 0) {
3338                 spin_lock(&lfsck->li_lock);
3339                 if (thread_is_stopped(thread)) {
3340                         spin_unlock(&lfsck->li_lock);
3341                 } else {
3342                         lfsck->li_status = LS_FAILED;
3343                         lfsck->li_flags = 0;
3344                         thread_set_flags(thread, SVC_STOPPING);
3345                         spin_unlock(&lfsck->li_lock);
3346
3347                         lfsck->li_start_unplug = 1;
3348                         wake_up_all(&thread->t_ctl_waitq);
3349                         l_wait_event(thread->t_ctl_waitq,
3350                                      thread_is_stopped(thread),
3351                                      &lwi);
3352                 }
3353         } else {
3354                 lfsck->li_start_unplug = 1;
3355                 wake_up_all(&thread->t_ctl_waitq);
3356         }
3357
3358         GOTO(put, rc);
3359
3360 out:
3361         mutex_unlock(&lfsck->li_mutex);
3362
3363 put:
3364         lfsck_instance_put(env, lfsck);
3365
3366         return rc < 0 ? rc : 0;
3367 }
3368 EXPORT_SYMBOL(lfsck_start);
3369
3370 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3371                struct lfsck_stop *stop)
3372 {
3373         struct lfsck_instance   *lfsck;
3374         struct ptlrpc_thread    *thread;
3375         struct l_wait_info       lwi    = { 0 };
3376         int                      rc     = 0;
3377         int                      rc1    = 0;
3378         ENTRY;
3379
3380         lfsck = lfsck_instance_find(key, true, false);
3381         if (unlikely(lfsck == NULL))
3382                 RETURN(-ENXIO);
3383
3384         thread = &lfsck->li_thread;
3385         if (stop && stop->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3386                 CERROR("%s: only allow to specify '-A' via MDS\n",
3387                        lfsck_lfsck2name(lfsck));
3388                 GOTO(put, rc = -EPERM);
3389         }
3390
3391         spin_lock(&lfsck->li_lock);
3392         /* The target is umounted */
3393         if (stop && stop->ls_status == LS_PAUSED)
3394                 lfsck->li_stopping = 1;
3395
3396         if (thread_is_init(thread) || thread_is_stopped(thread))
3397                 /* no error if LFSCK stopped already, or not started */
3398                 GOTO(unlock, rc = 0);
3399
3400         if (thread_is_stopping(thread))
3401                 /* Someone is stopping LFSCK. */
3402                 GOTO(unlock, rc = -EINPROGRESS);
3403
3404         if (stop) {
3405                 lfsck->li_status = stop->ls_status;
3406                 lfsck->li_flags = stop->ls_flags;
3407         } else {
3408                 lfsck->li_status = LS_STOPPED;
3409                 lfsck->li_flags = 0;
3410         }
3411
3412         thread_set_flags(thread, SVC_STOPPING);
3413
3414         LASSERT(lfsck->li_task != NULL);
3415         force_sig(SIGINT, lfsck->li_task);
3416
3417         if (lfsck->li_master) {
3418                 struct lfsck_component *com;
3419                 struct lfsck_assistant_data *lad;
3420
3421                 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3422                         lad = com->lc_data;
3423                         spin_lock(&lad->lad_lock);
3424                         if (lad->lad_task != NULL)
3425                                 force_sig(SIGINT, lad->lad_task);
3426                         spin_unlock(&lad->lad_lock);
3427                 }
3428
3429                 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3430                         lad = com->lc_data;
3431                         spin_lock(&lad->lad_lock);
3432                         if (lad->lad_task != NULL)
3433                                 force_sig(SIGINT, lad->lad_task);
3434                         spin_unlock(&lad->lad_lock);
3435                 }
3436         }
3437
3438         wake_up_all(&thread->t_ctl_waitq);
3439         spin_unlock(&lfsck->li_lock);
3440         if (stop && stop->ls_flags & LPF_BROADCAST)
3441                 rc1 = lfsck_stop_all(env, lfsck, stop);
3442
3443         /* It was me set the status as 'stopping' just now, if it is not
3444          * 'stopping' now, then either stopped, or re-started by race. */
3445         l_wait_event(thread->t_ctl_waitq,
3446                      !thread_is_stopping(thread),
3447                      &lwi);
3448
3449         GOTO(put, rc = 0);
3450
3451 unlock:
3452         spin_unlock(&lfsck->li_lock);
3453 put:
3454         lfsck_instance_put(env, lfsck);
3455
3456         return rc != 0 ? rc : rc1;
3457 }
3458 EXPORT_SYMBOL(lfsck_stop);
3459
3460 int lfsck_in_notify_local(const struct lu_env *env, struct dt_device *key,
3461                           struct lfsck_req_local *lrl, struct thandle *th)
3462 {
3463         struct lfsck_instance *lfsck;
3464         struct lfsck_component *com;
3465         int rc = -EOPNOTSUPP;
3466         ENTRY;
3467
3468         lfsck = lfsck_instance_find(key, true, false);
3469         if (unlikely(!lfsck))
3470                 RETURN(-ENXIO);
3471
3472         com = lfsck_component_find(lfsck, lrl->lrl_active);
3473         if (likely(com && com->lc_ops->lfsck_in_notify_local)) {
3474                 rc = com->lc_ops->lfsck_in_notify_local(env, com, lrl, th);
3475                 lfsck_component_put(env, com);
3476         }
3477
3478         lfsck_instance_put(env, lfsck);
3479
3480         RETURN(rc);
3481 }
3482 EXPORT_SYMBOL(lfsck_in_notify_local);
3483
3484 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3485                     struct lfsck_request *lr)
3486 {
3487         int rc = -EOPNOTSUPP;
3488         ENTRY;
3489
3490         switch (lr->lr_event) {
3491         case LE_START: {
3492                 struct lfsck_start       *start = &lfsck_env_info(env)->lti_start;
3493                 struct lfsck_start_param  lsp;
3494
3495                 memset(start, 0, sizeof(*start));
3496                 start->ls_valid = lr->lr_valid;
3497                 start->ls_speed_limit = lr->lr_speed;
3498                 start->ls_version = lr->lr_version;
3499                 start->ls_active = lr->lr_active;
3500                 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3501                 start->ls_async_windows = lr->lr_async_windows;
3502
3503                 lsp.lsp_start = start;
3504                 lsp.lsp_index = lr->lr_index;
3505                 lsp.lsp_index_valid = 1;
3506                 rc = lfsck_start(env, key, &lsp);
3507                 break;
3508         }
3509         case LE_STOP: {
3510                 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3511
3512                 memset(stop, 0, sizeof(*stop));
3513                 stop->ls_status = lr->lr_status;
3514                 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3515                 rc = lfsck_stop(env, key, stop);
3516                 break;
3517         }
3518         case LE_PHASE1_DONE:
3519         case LE_PHASE2_DONE:
3520         case LE_PEER_EXIT:
3521         case LE_CONDITIONAL_DESTROY:
3522         case LE_SET_LMV_MASTER:
3523         case LE_SET_LMV_SLAVE:
3524         case LE_PAIRS_VERIFY: {
3525                 struct lfsck_instance  *lfsck;
3526                 struct lfsck_component *com;
3527
3528                 lfsck = lfsck_instance_find(key, true, false);
3529                 if (unlikely(lfsck == NULL))
3530                         RETURN(-ENXIO);
3531
3532                 com = lfsck_component_find(lfsck, lr->lr_active);
3533                 if (likely(com)) {
3534                         rc = com->lc_ops->lfsck_in_notify(env, com, lr);
3535                         lfsck_component_put(env, com);
3536                 }
3537
3538                 lfsck_instance_put(env, lfsck);
3539                 break;
3540         }
3541         default:
3542                 break;
3543         }
3544
3545         RETURN(rc);
3546 }
3547 EXPORT_SYMBOL(lfsck_in_notify);
3548
3549 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3550                 struct lfsck_request *req, struct lfsck_reply *rep,
3551                 struct lfsck_query *que)
3552 {
3553         struct lfsck_instance  *lfsck;
3554         struct lfsck_component *com;
3555         int                     i;
3556         int                     rc = 0;
3557         __u16                   type;
3558         ENTRY;
3559
3560         lfsck = lfsck_instance_find(key, true, false);
3561         if (unlikely(lfsck == NULL))
3562                 RETURN(-ENXIO);
3563
3564         if (que != NULL) {
3565                 if (que->lu_types == LFSCK_TYPES_ALL)
3566                         que->lu_types =
3567                                 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3568
3569                 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3570                         que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3571
3572                         GOTO(out, rc = -ENOTSUPP);
3573                 }
3574
3575                 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3576                      i++, type = 1 << i) {
3577                         if (!(que->lu_types & type))
3578                                 continue;
3579
3580 again:
3581                         com = lfsck_component_find(lfsck, type);
3582                         if (unlikely(com == NULL))
3583                                 GOTO(out, rc = -ENOTSUPP);
3584
3585                         memset(que->lu_mdts_count[i], 0,
3586                                sizeof(__u32) * (LS_MAX + 1));
3587                         memset(que->lu_osts_count[i], 0,
3588                                sizeof(__u32) * (LS_MAX + 1));
3589                         que->lu_repaired[i] = 0;
3590                         rc = com->lc_ops->lfsck_query(env, com, req, rep,
3591                                                       que, i);
3592                         lfsck_component_put(env, com);
3593                         if  (rc < 0)
3594                                 GOTO(out, rc);
3595                 }
3596
3597                 if (!(que->lu_flags & LPF_WAIT))
3598                         GOTO(out, rc);
3599
3600                 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3601                      i++, type = 1 << i) {
3602                         if (!(que->lu_types & type))
3603                                 continue;
3604
3605                         if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3606                             que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3607                             que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3608                             que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3609                                 struct l_wait_info lwi;
3610
3611                                 /* If it is required to wait, then sleep
3612                                  * 3 seconds and try to query again. */
3613                                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
3614                                                        NULL,
3615                                                        LWI_ON_SIGNAL_NOOP,
3616                                                        NULL);
3617                                 rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
3618                                                   0, &lwi);
3619                                 if (rc == -ETIMEDOUT)
3620                                         goto again;
3621                         }
3622                 }
3623         } else {
3624                 com = lfsck_component_find(lfsck, req->lr_active);
3625                 if (likely(com != NULL)) {
3626                         rc = com->lc_ops->lfsck_query(env, com, req, rep,
3627                                                       que, -1);
3628                         lfsck_component_put(env, com);
3629                 } else {
3630                         rc = -ENOTSUPP;
3631                 }
3632         }
3633
3634         GOTO(out, rc);
3635
3636 out:
3637         lfsck_instance_put(env, lfsck);
3638         return rc;
3639 }
3640 EXPORT_SYMBOL(lfsck_query);
3641
3642 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3643                              struct ldlm_namespace *ns)
3644 {
3645         struct lfsck_instance  *lfsck;
3646         int                     rc      = -ENXIO;
3647
3648         lfsck = lfsck_instance_find(key, true, false);
3649         if (likely(lfsck != NULL)) {
3650                 lfsck->li_namespace = ns;
3651                 lfsck_instance_put(env, lfsck);
3652                 rc = 0;
3653         }
3654
3655         return rc;
3656 }
3657 EXPORT_SYMBOL(lfsck_register_namespace);
3658
3659 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3660                    struct dt_device *next, struct obd_device *obd,
3661                    lfsck_out_notify notify, void *notify_data, bool master)
3662 {
3663         struct lfsck_instance   *lfsck;
3664         struct dt_object        *root  = NULL;
3665         struct dt_object        *obj   = NULL;
3666         struct lu_fid           *fid   = &lfsck_env_info(env)->lti_fid;
3667         int                      rc;
3668         ENTRY;
3669
3670         lfsck = lfsck_instance_find(key, false, false);
3671         if (unlikely(lfsck != NULL))
3672                 RETURN(-EEXIST);
3673
3674         OBD_ALLOC_PTR(lfsck);
3675         if (lfsck == NULL)
3676                 RETURN(-ENOMEM);
3677
3678         mutex_init(&lfsck->li_mutex);
3679         spin_lock_init(&lfsck->li_lock);
3680         INIT_LIST_HEAD(&lfsck->li_link);
3681         INIT_LIST_HEAD(&lfsck->li_list_scan);
3682         INIT_LIST_HEAD(&lfsck->li_list_dir);
3683         INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3684         INIT_LIST_HEAD(&lfsck->li_list_idle);
3685         INIT_LIST_HEAD(&lfsck->li_list_lmv);
3686         atomic_set(&lfsck->li_ref, 1);
3687         atomic_set(&lfsck->li_double_scan_count, 0);
3688         init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3689         lfsck->li_out_notify = notify;
3690         lfsck->li_out_notify_data = notify_data;
3691         lfsck->li_next = next;
3692         lfsck->li_bottom = key;
3693         lfsck->li_obd = obd;
3694
3695         rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3696         if (rc != 0)
3697                 GOTO(out, rc);
3698
3699         rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3700         if (rc != 0)
3701                 GOTO(out, rc);
3702
3703         fid->f_seq = FID_SEQ_LOCAL_NAME;
3704         fid->f_oid = 1;
3705         fid->f_ver = 0;
3706         rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3707         if (rc != 0)
3708                 GOTO(out, rc);
3709
3710         rc = dt_root_get(env, key, fid);
3711         if (rc != 0)
3712                 GOTO(out, rc);
3713
3714         root = dt_locate(env, key, fid);
3715         if (IS_ERR(root))
3716                 GOTO(out, rc = PTR_ERR(root));
3717
3718         if (unlikely(!dt_try_as_dir(env, root)))
3719                 GOTO(out, rc = -ENOTDIR);
3720
3721         lfsck->li_local_root_fid = *fid;
3722         if (master) {
3723                 lfsck->li_master = 1;
3724                 if (lfsck_dev_idx(lfsck) == 0) {
3725                         struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3726                         const struct lu_name *cname;
3727
3728                         rc = dt_lookup(env, root,
3729                                 (struct dt_rec *)(&lfsck->li_global_root_fid),
3730                                 (const struct dt_key *)"ROOT");
3731                         if (rc != 0)
3732                                 GOTO(out, rc);
3733
3734                         obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3735                         if (IS_ERR(obj))
3736                                 GOTO(out, rc = PTR_ERR(obj));
3737
3738                         if (unlikely(!dt_try_as_dir(env, obj)))
3739                                 GOTO(out, rc = -ENOTDIR);
3740
3741                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3742                                 (const struct dt_key *)dotlustre);
3743                         if (rc != 0)
3744                                 GOTO(out, rc);
3745
3746                         lfsck_object_put(env, obj);
3747                         obj = dt_locate(env, key, fid);
3748                         if (IS_ERR(obj))
3749                                 GOTO(out, rc = PTR_ERR(obj));
3750
3751                         cname = lfsck_name_get_const(env, dotlustre,
3752                                                      strlen(dotlustre));
3753                         rc = lfsck_verify_linkea(env, obj, cname,
3754                                                  &lfsck->li_global_root_fid);
3755                         if (rc != 0)
3756                                 GOTO(out, rc);
3757
3758                         if (unlikely(!dt_try_as_dir(env, obj)))
3759                                 GOTO(out, rc = -ENOTDIR);
3760
3761                         *pfid = *fid;
3762                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3763                                        (const struct dt_key *)lostfound);
3764                         if (rc != 0)
3765                                 GOTO(out, rc);
3766
3767                         lfsck_object_put(env, obj);
3768                         obj = dt_locate(env, key, fid);
3769                         if (IS_ERR(obj))
3770                                 GOTO(out, rc = PTR_ERR(obj));
3771
3772                         cname = lfsck_name_get_const(env, lostfound,
3773                                                      strlen(lostfound));
3774                         rc = lfsck_verify_linkea(env, obj, cname, pfid);
3775                         if (rc != 0)
3776                                 GOTO(out, rc);
3777
3778                         lfsck_object_put(env, obj);
3779                         obj = NULL;
3780                 }
3781         }
3782
3783         fid->f_seq = FID_SEQ_LOCAL_FILE;
3784         fid->f_oid = OTABLE_IT_OID;
3785         fid->f_ver = 0;
3786         obj = dt_locate(env, key, fid);
3787         if (IS_ERR(obj))
3788                 GOTO(out, rc = PTR_ERR(obj));
3789
3790         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3791         if (rc != 0)
3792                 GOTO(out, rc);
3793
3794         lfsck->li_obj_oit = obj;
3795         obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3796                                         S_IFDIR | S_IRUGO | S_IWUSR);
3797         if (IS_ERR(obj))
3798                 GOTO(out, rc = PTR_ERR(obj));
3799
3800         lu_object_get(&obj->do_lu);
3801         lfsck->li_lfsck_dir = obj;
3802         rc = lfsck_bookmark_setup(env, lfsck);
3803         if (rc != 0)
3804                 GOTO(out, rc);
3805
3806         if (master) {
3807                 rc = lfsck_fid_init(lfsck);
3808                 if (rc < 0)
3809                         GOTO(out, rc);
3810
3811                 rc = lfsck_namespace_setup(env, lfsck);
3812                 if (rc < 0)
3813                         GOTO(out, rc);
3814         }
3815
3816         rc = lfsck_layout_setup(env, lfsck);
3817         if (rc < 0)
3818                 GOTO(out, rc);
3819
3820         /* XXX: more LFSCK components initialization to be added here. */
3821
3822         rc = lfsck_instance_add(lfsck);
3823         if (rc == 0)
3824                 rc = lfsck_add_target_from_orphan(env, lfsck);
3825 out:
3826         if (obj != NULL && !IS_ERR(obj))
3827                 lfsck_object_put(env, obj);
3828         if (root != NULL && !IS_ERR(root))
3829                 lfsck_object_put(env, root);
3830         if (rc != 0)
3831                 lfsck_instance_cleanup(env, lfsck);
3832         return rc;
3833 }
3834 EXPORT_SYMBOL(lfsck_register);
3835
3836 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3837 {
3838         struct lfsck_instance *lfsck;
3839
3840         lfsck = lfsck_instance_find(key, false, true);
3841         if (lfsck != NULL)
3842                 lfsck_instance_put(env, lfsck);
3843 }
3844 EXPORT_SYMBOL(lfsck_degister);
3845
3846 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3847                      struct dt_device *tgt, struct obd_export *exp,
3848                      __u32 index, bool for_ost)
3849 {
3850         struct lfsck_instance   *lfsck;
3851         struct lfsck_tgt_desc   *ltd;
3852         int                      rc;
3853         ENTRY;
3854
3855         OBD_ALLOC_PTR(ltd);
3856         if (ltd == NULL)
3857                 RETURN(-ENOMEM);
3858
3859         ltd->ltd_tgt = tgt;
3860         ltd->ltd_key = key;
3861         ltd->ltd_exp = exp;
3862         INIT_LIST_HEAD(&ltd->ltd_orphan_list);
3863         INIT_LIST_HEAD(&ltd->ltd_layout_list);
3864         INIT_LIST_HEAD(&ltd->ltd_layout_phase_list);
3865         INIT_LIST_HEAD(&ltd->ltd_namespace_list);
3866         INIT_LIST_HEAD(&ltd->ltd_namespace_phase_list);
3867         atomic_set(&ltd->ltd_ref, 1);
3868         ltd->ltd_index = index;
3869
3870         spin_lock(&lfsck_instance_lock);
3871         lfsck = __lfsck_instance_find(key, true, false);
3872         if (lfsck == NULL) {
3873                 if (for_ost)
3874                         list_add_tail(&ltd->ltd_orphan_list,
3875                                       &lfsck_ost_orphan_list);
3876                 else
3877                         list_add_tail(&ltd->ltd_orphan_list,
3878                                       &lfsck_mdt_orphan_list);
3879                 spin_unlock(&lfsck_instance_lock);
3880
3881                 RETURN(0);
3882         }
3883         spin_unlock(&lfsck_instance_lock);
3884
3885         rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3886         if (rc != 0)
3887                 lfsck_tgt_put(ltd);
3888
3889         lfsck_instance_put(env, lfsck);
3890
3891         RETURN(rc);
3892 }
3893 EXPORT_SYMBOL(lfsck_add_target);
3894
3895 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3896                       struct dt_device *tgt, __u32 index, bool for_ost)
3897 {
3898         struct lfsck_instance   *lfsck;
3899         struct lfsck_tgt_descs  *ltds;
3900         struct lfsck_tgt_desc   *ltd;
3901         struct list_head        *head;
3902
3903         if (for_ost)
3904                 head = &lfsck_ost_orphan_list;
3905         else
3906                 head = &lfsck_mdt_orphan_list;
3907
3908         spin_lock(&lfsck_instance_lock);
3909         list_for_each_entry(ltd, head, ltd_orphan_list) {
3910                 if (ltd->ltd_tgt == tgt) {
3911                         list_del_init(&ltd->ltd_orphan_list);
3912                         spin_unlock(&lfsck_instance_lock);
3913                         lfsck_tgt_put(ltd);
3914
3915                         return;
3916                 }
3917         }
3918
3919         ltd = NULL;
3920         lfsck = __lfsck_instance_find(key, true, false);
3921         spin_unlock(&lfsck_instance_lock);
3922         if (unlikely(lfsck == NULL))
3923                 return;
3924
3925         if (for_ost)
3926                 ltds = &lfsck->li_ost_descs;
3927         else
3928                 ltds = &lfsck->li_mdt_descs;
3929
3930         down_write(&ltds->ltd_rw_sem);
3931         LASSERT(ltds->ltd_tgts_bitmap != NULL);
3932
3933         if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3934                 goto unlock;
3935
3936         ltd = lfsck_ltd2tgt(ltds, index);
3937         if (unlikely(ltd == NULL))
3938                 goto unlock;
3939
3940         LASSERT(ltds->ltd_tgtnr > 0);
3941
3942         ltds->ltd_tgtnr--;
3943         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3944         lfsck_assign_tgt(ltds, NULL, index);
3945
3946 unlock:
3947         if (ltd == NULL) {
3948                 if (for_ost)
3949                         head = &lfsck->li_ost_descs.ltd_orphan;
3950                 else
3951                         head = &lfsck->li_mdt_descs.ltd_orphan;
3952
3953                 list_for_each_entry(ltd, head, ltd_orphan_list) {
3954                         if (ltd->ltd_tgt == tgt) {
3955                                 list_del_init(&ltd->ltd_orphan_list);
3956                                 break;
3957                         }
3958                 }
3959         }
3960
3961         up_write(&ltds->ltd_rw_sem);
3962         if (ltd != NULL) {
3963                 spin_lock(&ltds->ltd_lock);
3964                 ltd->ltd_dead = 1;
3965                 spin_unlock(&ltds->ltd_lock);
3966                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3967                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3968                 lfsck_tgt_put(ltd);
3969         }
3970
3971         lfsck_instance_put(env, lfsck);
3972 }
3973 EXPORT_SYMBOL(lfsck_del_target);
3974
3975 static int __init lfsck_init(void)
3976 {
3977         int rc;
3978
3979         INIT_LIST_HEAD(&lfsck_instance_list);
3980         INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3981         INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3982         lfsck_key_init_generic(&lfsck_thread_key, NULL);
3983         rc = lu_context_key_register(&lfsck_thread_key);
3984         if (!rc) {
3985                 tgt_register_lfsck_in_notify_local(lfsck_in_notify_local);
3986                 tgt_register_lfsck_in_notify(lfsck_in_notify);
3987                 tgt_register_lfsck_query(lfsck_query);
3988         }
3989
3990         return rc;
3991 }
3992
3993 static void __exit lfsck_exit(void)
3994 {
3995         struct lfsck_tgt_desc *ltd;
3996         struct lfsck_tgt_desc *next;
3997
3998         LASSERT(list_empty(&lfsck_instance_list));
3999
4000         list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
4001                                  ltd_orphan_list) {
4002                 list_del_init(&ltd->ltd_orphan_list);
4003                 lfsck_tgt_put(ltd);
4004         }
4005
4006         list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
4007                                  ltd_orphan_list) {
4008                 list_del_init(&ltd->ltd_orphan_list);
4009                 lfsck_tgt_put(ltd);
4010         }
4011
4012         lu_context_key_degister(&lfsck_thread_key);
4013 }
4014
4015 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
4016 MODULE_DESCRIPTION("Lustre File System Checker");
4017 MODULE_VERSION(LUSTRE_VERSION_STRING);
4018 MODULE_LICENSE("GPL");
4019
4020 module_init(lfsck_init);
4021 module_exit(lfsck_exit);