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