Whamcloud - gitweb
01ea4117ad7dd8cdf3413d26786483c58511a66e
[fs/lustre-release.git] / lustre / lfsck / lfsck_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2017, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_lib.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LFSCK
32
33 #include <linux/kthread.h>
34 #include <linux/sched.h>
35 #include <linux/list.h>
36 #include <linux/delay.h>
37 #include <lu_object.h>
38 #include <dt_object.h>
39 #include <md_object.h>
40 #include <lustre_fld.h>
41 #include <lustre_lib.h>
42 #include <lustre_net.h>
43 #include <lustre_lfsck.h>
44
45 #include "lfsck_internal.h"
46
47 #define LFSCK_CHECKPOINT_SKIP   1
48
49 /* define lfsck thread key */
50 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
51
52 static void lfsck_key_fini(const struct lu_context *ctx,
53                            struct lu_context_key *key, void *data)
54 {
55         struct lfsck_thread_info *info = data;
56
57         lu_buf_free(&info->lti_linkea_buf);
58         lu_buf_free(&info->lti_linkea_buf2);
59         lu_buf_free(&info->lti_big_buf);
60         OBD_FREE_PTR(info);
61 }
62
63 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
64 LU_KEY_INIT_GENERIC(lfsck);
65
66 static LIST_HEAD(lfsck_instance_list);
67 static LIST_HEAD(lfsck_ost_orphan_list);
68 static LIST_HEAD(lfsck_mdt_orphan_list);
69 static DEFINE_SPINLOCK(lfsck_instance_lock);
70
71 const char *const lfsck_flags_names[] = {
72         "scanned-once",
73         "inconsistent",
74         "upgrade",
75         "incomplete",
76         "crashed_lastid",
77         NULL
78 };
79
80 const char *const lfsck_param_names[] = {
81         NULL,
82         "failout",
83         "dryrun",
84         "all_targets",
85         "broadcast",
86         "orphan",
87         "create_ostobj",
88         "create_mdtobj",
89         NULL,
90         "delay_create_ostobj",
91         NULL
92 };
93
94 enum lfsck_verify_lpf_types {
95         LVLT_BY_BOOKMARK        = 0,
96         LVLT_BY_NAMEENTRY       = 1,
97 };
98
99 static inline void
100 lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
101 {
102         if (type == LFSCK_TYPE_LAYOUT) {
103                 ltd->ltd_layout_status = LS_MAX;
104                 ltd->ltd_layout_repaired = 0;
105         } else {
106                 ltd->ltd_namespace_status = LS_MAX;
107                 ltd->ltd_namespace_repaired = 0;
108         }
109 }
110
111 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
112 {
113         spin_lock_init(&ltds->ltd_lock);
114         init_rwsem(&ltds->ltd_rw_sem);
115         INIT_LIST_HEAD(&ltds->ltd_orphan);
116         ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
117         if (ltds->ltd_tgts_bitmap == NULL)
118                 return -ENOMEM;
119
120         return 0;
121 }
122
123 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
124 {
125         struct lfsck_tgt_desc   *ltd;
126         struct lfsck_tgt_desc   *next;
127         int                      idx;
128
129         down_write(&ltds->ltd_rw_sem);
130
131         list_for_each_entry_safe(ltd, next, &ltds->ltd_orphan,
132                                  ltd_orphan_list) {
133                 list_del_init(&ltd->ltd_orphan_list);
134                 lfsck_tgt_put(ltd);
135         }
136
137         if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
138                 up_write(&ltds->ltd_rw_sem);
139
140                 return;
141         }
142
143         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
144                 ltd = lfsck_ltd2tgt(ltds, idx);
145                 if (likely(ltd != NULL)) {
146                         LASSERT(list_empty(&ltd->ltd_layout_list));
147                         LASSERT(list_empty(&ltd->ltd_layout_phase_list));
148                         LASSERT(list_empty(&ltd->ltd_namespace_list));
149                         LASSERT(list_empty(&ltd->ltd_namespace_phase_list));
150
151                         ltds->ltd_tgtnr--;
152                         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
153                         lfsck_assign_tgt(ltds, NULL, idx);
154                         lfsck_tgt_put(ltd);
155                 }
156         }
157
158         LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
159                  ltds->ltd_tgtnr);
160
161         for (idx = 0; idx < 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 | LA_TYPE;
1094         memset(dof, 0, sizeof(*dof));
1095         dof->dof_type = dt_mode_to_dft(S_IFDIR);
1096
1097         if (node == 0)
1098                 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1099         else
1100                 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1101         if (rc == 0)
1102                 lfsck->li_lpf_obj = child;
1103
1104         GOTO(unlock, rc);
1105
1106 unlock:
1107         lfsck_unlock(llh);
1108         if (rc != 0 && child != NULL && !IS_ERR(child))
1109                 lfsck_object_put(env, child);
1110
1111         return rc;
1112 }
1113
1114 /**
1115  * Scan .lustre/lost+found for bad name entries and remove them.
1116  *
1117  * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1118  * index in the system. Any other formatted name is invalid and should be
1119  * removed.
1120  *
1121  * \param[in] env       pointer to the thread context
1122  * \param[in] lfsck     pointer to the lfsck instance
1123  *
1124  * \retval              0 for success
1125  * \retval              negative error number on failure
1126  */
1127 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1128                                       struct lfsck_instance *lfsck)
1129 {
1130         struct dt_object        *parent = lfsck->li_lpf_root_obj;
1131         struct lu_dirent        *ent    =
1132                         (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1133         const struct dt_it_ops  *iops   = &parent->do_index_ops->dio_it;
1134         struct dt_it            *it;
1135         int                      rc;
1136         ENTRY;
1137
1138         it = iops->init(env, parent, LUDA_64BITHASH);
1139         if (IS_ERR(it))
1140                 RETURN(PTR_ERR(it));
1141
1142         rc = iops->load(env, it, 0);
1143         if (rc == 0)
1144                 rc = iops->next(env, it);
1145         else if (rc > 0)
1146                 rc = 0;
1147
1148         while (rc == 0) {
1149                 int off = 3;
1150
1151                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1152                 if (rc != 0)
1153                         break;
1154
1155                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1156                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1157                         goto next;
1158
1159                 /* name length must be strlen("MDTxxxx") */
1160                 if (ent->lde_namelen != 7)
1161                         goto remove;
1162
1163                 if (memcmp(ent->lde_name, "MDT", off) != 0)
1164                         goto remove;
1165
1166                 while (off < 7 && isxdigit(ent->lde_name[off]))
1167                         off++;
1168
1169                 if (off != 7) {
1170
1171 remove:
1172                         rc = lfsck_lpf_remove_name_entry(env, lfsck,
1173                                                          ent->lde_name);
1174                         if (rc != 0)
1175                                 break;
1176                 }
1177
1178 next:
1179                 rc = iops->next(env, it);
1180         }
1181
1182         iops->put(env, it);
1183         iops->fini(env, it);
1184
1185         RETURN(rc > 0 ? 0 : rc);
1186 }
1187
1188 static int lfsck_update_lpf_entry(const struct lu_env *env,
1189                                   struct lfsck_instance *lfsck,
1190                                   struct dt_object *parent,
1191                                   struct dt_object *child,
1192                                   const char *name,
1193                                   enum lfsck_verify_lpf_types type)
1194 {
1195         int rc;
1196
1197         if (type == LVLT_BY_BOOKMARK) {
1198                 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1199                                              lfsck_dto2fid(child), S_IFDIR);
1200         } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1201                 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1202                 rc = lfsck_bookmark_store(env, lfsck);
1203
1204                 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1205                        " in the bookmark file: rc = %d\n",
1206                        lfsck_lfsck2name(lfsck),
1207                        PFID(lfsck_dto2fid(child)), rc);
1208         }
1209
1210         return rc;
1211 }
1212
1213 /**
1214  * Check whether the @child back references the @parent.
1215  *
1216  * Two cases:
1217  * 1) The child's FID is stored in the bookmark file. If the child back
1218  *    references the parent (LU_LPF_FID object) via its ".." entry, then
1219  *    insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1220  *    the child back references another parent2, then:
1221  * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1222  * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1223  *      references the child. So keep them there. As the LFSCK processing,
1224  *      the parent3 may be found, then when the LFSCK run next time, the
1225  *      inconsistency can be repaired.
1226  *
1227  * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1228  *    entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1229  *    via its ".." entry, then update the bookmark file, otherwise, if the child
1230  *    back references another parent2, then:
1231  * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1232  *      from .lustre/lost+found/;
1233  * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1234  *      sub-directory name entry and update the child;
1235  * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1236  *      or not, then keep them there.
1237  *
1238  * \param[in] env       pointer to the thread context
1239  * \param[in] lfsck     pointer to the lfsck instance
1240  * \param[in] child     pointer to the lost+found sub-directory object
1241  * \param[in] name      the name for lost+found sub-directory object
1242  * \param[out] fid      pointer to the buffer to hold the FID of the object
1243  *                      (called it as parent2) that is referenced via the
1244  *                      child's dotdot entry; it also can be the FID that
1245  *                      is referenced by the name entry under the parent2.
1246  * \param[in] type      to indicate where the child's FID is stored in
1247  *
1248  * \retval              positive number for uncertain inconsistency
1249  * \retval              0 for success
1250  * \retval              negative error number on failure
1251  */
1252 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1253                                   struct lfsck_instance *lfsck,
1254                                   struct dt_object *child, const char *name,
1255                                   struct lu_fid *fid,
1256                                   enum lfsck_verify_lpf_types type)
1257 {
1258         struct dt_object         *parent  = lfsck->li_lpf_root_obj;
1259         struct lfsck_thread_info *info    = lfsck_env_info(env);
1260         char                     *name2   = info->lti_key;
1261         struct lu_fid            *fid2    = &info->lti_fid3;
1262         struct dt_object         *parent2 = NULL;
1263         struct lustre_handle      lh      = { 0 };
1264         int                       rc;
1265         ENTRY;
1266
1267         fid_zero(fid);
1268         rc = dt_lookup(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_is_dryrun(lfsck))
1430                 RETURN(0);
1431
1432         if (lfsck->li_lpf_root_obj != NULL)
1433                 RETURN(0);
1434
1435         if (node == 0) {
1436                 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1437                                                   &LU_LPF_FID);
1438         } else {
1439                 struct lfsck_tgt_desc *ltd;
1440
1441                 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1442                 if (unlikely(ltd == NULL))
1443                         RETURN(-ENXIO);
1444
1445                 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1446                                                   &LU_LPF_FID);
1447                 lfsck_tgt_put(ltd);
1448         }
1449
1450         if (IS_ERR(parent))
1451                 RETURN(PTR_ERR(parent));
1452
1453         LASSERT(dt_object_exists(parent));
1454
1455         if (unlikely(!dt_try_as_dir(env, parent))) {
1456                 lfsck_object_put(env, parent);
1457
1458                 GOTO(put, rc = -ENOTDIR);
1459         }
1460
1461         lfsck->li_lpf_root_obj = parent;
1462         if (node == 0) {
1463                 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1464                 if (rc != 0)
1465                         CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1466                                "for bad sub-directories: rc = %d\n",
1467                                lfsck_lfsck2name(lfsck), rc);
1468         }
1469
1470         /* child2 */
1471         snprintf(name, 8, "MDT%04x", node);
1472         rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1473                        (const struct dt_key *)name);
1474         if (rc == -ENOENT) {
1475                 rc = 0;
1476                 goto find_child1;
1477         }
1478
1479         if (rc != 0)
1480                 GOTO(put, rc);
1481
1482         /* Invalid FID in the name entry, remove the name entry. */
1483         if (!fid_is_norm(cfid)) {
1484                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1485                 if (rc != 0)
1486                         GOTO(put, rc);
1487
1488                 goto find_child1;
1489         }
1490
1491         child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1492         if (IS_ERR(child2))
1493                 GOTO(put, rc = PTR_ERR(child2));
1494
1495         if (unlikely(!dt_object_exists(child2) ||
1496                      dt_object_remote(child2)) ||
1497                      !S_ISDIR(lfsck_object_type(child2))) {
1498                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1499                 if (rc != 0)
1500                         GOTO(put, rc);
1501
1502                 goto find_child1;
1503         }
1504
1505         if (unlikely(!dt_try_as_dir(env, child2))) {
1506                 lfsck_object_put(env, child2);
1507                 child2 = NULL;
1508                 rc = -ENOTDIR;
1509         }
1510
1511 find_child1:
1512         if (fid_is_zero(&bk->lb_lpf_fid))
1513                 goto check_child2;
1514
1515         if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1516                 if (lfsck->li_lpf_obj == NULL) {
1517                         lu_object_get(&child2->do_lu);
1518                         lfsck->li_lpf_obj = child2;
1519                 }
1520
1521                 cname = lfsck_name_get_const(env, name, strlen(name));
1522                 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1523
1524                 GOTO(put, rc);
1525         }
1526
1527         if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1528                 struct lu_fid tfid = bk->lb_lpf_fid;
1529
1530                 /* Invalid FID record in the bookmark file, reset it. */
1531                 fid_zero(&bk->lb_lpf_fid);
1532                 rc = lfsck_bookmark_store(env, lfsck);
1533
1534                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1535                        " in the bookmark file: rc = %d\n",
1536                        lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1537
1538                 if (rc != 0)
1539                         GOTO(put, rc);
1540
1541                 goto check_child2;
1542         }
1543
1544         child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1545         if (IS_ERR(child1)) {
1546                 child1 = NULL;
1547                 goto check_child2;
1548         }
1549
1550         if (unlikely(!dt_object_exists(child1) ||
1551                      dt_object_remote(child1)) ||
1552                      !S_ISDIR(lfsck_object_type(child1))) {
1553                 /* Invalid FID record in the bookmark file, reset it. */
1554                 fid_zero(&bk->lb_lpf_fid);
1555                 rc = lfsck_bookmark_store(env, lfsck);
1556
1557                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1558                        " in the bookmark file: rc = %d\n",
1559                        lfsck_lfsck2name(lfsck),
1560                        PFID(lfsck_dto2fid(child1)), rc);
1561
1562                 if (rc != 0)
1563                         GOTO(put, rc);
1564
1565                 lfsck_object_put(env, child1);
1566                 child1 = NULL;
1567                 goto check_child2;
1568         }
1569
1570         if (unlikely(!dt_try_as_dir(env, child1))) {
1571                 lfsck_object_put(env, child1);
1572                 child1 = NULL;
1573                 rc = -ENOTDIR;
1574                 goto check_child2;
1575         }
1576
1577         rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1578                                     LVLT_BY_BOOKMARK);
1579         if (lu_fid_eq(pfid, &LU_LPF_FID))
1580                 GOTO(put, rc);
1581
1582 check_child2:
1583         if (child2 != NULL)
1584                 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1585                                             pfid, LVLT_BY_NAMEENTRY);
1586
1587         GOTO(put, rc);
1588
1589 put:
1590         if (lfsck->li_lpf_obj != NULL) {
1591                 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1592                         lfsck_object_put(env, lfsck->li_lpf_obj);
1593                         lfsck->li_lpf_obj = NULL;
1594                         rc = -ENOTDIR;
1595                 }
1596         } else if (rc == 0) {
1597                 rc = lfsck_create_lpf(env, lfsck);
1598         }
1599
1600         if (child2 != NULL && !IS_ERR(child2))
1601                 lfsck_object_put(env, child2);
1602         if (child1 != NULL && !IS_ERR(child1))
1603                 lfsck_object_put(env, child1);
1604
1605         return rc;
1606 }
1607
1608 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1609 {
1610         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
1611         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
1612         char                    *prefix;
1613         int                      rc     = 0;
1614         ENTRY;
1615
1616         if (unlikely(ss == NULL))
1617                 RETURN(-ENXIO);
1618
1619         OBD_ALLOC_PTR(lfsck->li_seq);
1620         if (lfsck->li_seq == NULL)
1621                 RETURN(-ENOMEM);
1622
1623         OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1624         if (prefix == NULL)
1625                 GOTO(out, rc = -ENOMEM);
1626
1627         snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1628         seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1629                              ss->ss_server_seq);
1630         OBD_FREE(prefix, MAX_OBD_NAME + 7);
1631
1632         if (fid_is_sane(&bk->lb_last_fid))
1633                 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1634
1635         RETURN(0);
1636
1637 out:
1638         OBD_FREE_PTR(lfsck->li_seq);
1639         lfsck->li_seq = NULL;
1640
1641         return rc;
1642 }
1643
1644 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1645 {
1646         if (lfsck->li_seq != NULL) {
1647                 seq_client_fini(lfsck->li_seq);
1648                 OBD_FREE_PTR(lfsck->li_seq);
1649                 lfsck->li_seq = NULL;
1650         }
1651 }
1652
1653 void lfsck_instance_cleanup(const struct lu_env *env,
1654                             struct lfsck_instance *lfsck)
1655 {
1656         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1657         struct lfsck_component  *com;
1658         struct lfsck_component  *next;
1659         struct lfsck_lmv_unit   *llu;
1660         struct lfsck_lmv_unit   *llu_next;
1661         struct lfsck_lmv        *llmv;
1662         ENTRY;
1663
1664         LASSERT(list_empty(&lfsck->li_link));
1665         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1666
1667         if (lfsck->li_obj_oit != NULL) {
1668                 lfsck_object_put(env, lfsck->li_obj_oit);
1669                 lfsck->li_obj_oit = NULL;
1670         }
1671
1672         LASSERT(lfsck->li_obj_dir == NULL);
1673         LASSERT(lfsck->li_lmv == NULL);
1674
1675         list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1676                 llmv = &llu->llu_lmv;
1677
1678                 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1679                          "still in using: %u\n",
1680                          atomic_read(&llmv->ll_ref));
1681
1682                 lfsck_lmv_put(env, llmv);
1683         }
1684
1685         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1686                 lfsck_component_cleanup(env, com);
1687         }
1688
1689         LASSERT(list_empty(&lfsck->li_list_dir));
1690
1691         list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1692                                  lc_link) {
1693                 lfsck_component_cleanup(env, com);
1694         }
1695
1696         list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1697                 lfsck_component_cleanup(env, com);
1698         }
1699
1700         lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1701         lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1702
1703         if (lfsck->li_lfsck_dir != NULL) {
1704                 lfsck_object_put(env, lfsck->li_lfsck_dir);
1705                 lfsck->li_lfsck_dir = NULL;
1706         }
1707
1708         if (lfsck->li_bookmark_obj != NULL) {
1709                 lfsck_object_put(env, lfsck->li_bookmark_obj);
1710                 lfsck->li_bookmark_obj = NULL;
1711         }
1712
1713         if (lfsck->li_lpf_obj != NULL) {
1714                 lfsck_object_put(env, lfsck->li_lpf_obj);
1715                 lfsck->li_lpf_obj = NULL;
1716         }
1717
1718         if (lfsck->li_lpf_root_obj != NULL) {
1719                 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1720                 lfsck->li_lpf_root_obj = NULL;
1721         }
1722
1723         if (lfsck->li_los != NULL) {
1724                 local_oid_storage_fini(env, lfsck->li_los);
1725                 lfsck->li_los = NULL;
1726         }
1727
1728         lfsck_fid_fini(lfsck);
1729
1730         OBD_FREE_PTR(lfsck);
1731 }
1732
1733 static inline struct lfsck_instance *
1734 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1735 {
1736         struct lfsck_instance *lfsck;
1737
1738         list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1739                 if (lfsck->li_bottom == key) {
1740                         if (ref)
1741                                 lfsck_instance_get(lfsck);
1742                         if (unlink)
1743                                 list_del_init(&lfsck->li_link);
1744
1745                         return lfsck;
1746                 }
1747         }
1748
1749         return NULL;
1750 }
1751
1752 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1753                                            bool unlink)
1754 {
1755         struct lfsck_instance *lfsck;
1756
1757         spin_lock(&lfsck_instance_lock);
1758         lfsck = __lfsck_instance_find(key, ref, unlink);
1759         spin_unlock(&lfsck_instance_lock);
1760
1761         return lfsck;
1762 }
1763
1764 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1765 {
1766         struct lfsck_instance *tmp;
1767
1768         spin_lock(&lfsck_instance_lock);
1769         list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1770                 if (lfsck->li_bottom == tmp->li_bottom) {
1771                         spin_unlock(&lfsck_instance_lock);
1772                         return -EEXIST;
1773                 }
1774         }
1775
1776         list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1777         spin_unlock(&lfsck_instance_lock);
1778         return 0;
1779 }
1780
1781 void lfsck_bits_dump(struct seq_file *m, int bits, const char *const names[],
1782                      const char *prefix)
1783 {
1784         int flag;
1785         int i;
1786         bool newline = (bits != 0 ? false : true);
1787
1788         seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1789
1790         for (i = 0, flag = 1; bits != 0; i++, flag = BIT(i)) {
1791                 if (flag & bits) {
1792                         bits &= ~flag;
1793                         if (names[i] != NULL) {
1794                                 if (bits == 0)
1795                                         newline = true;
1796
1797                                 seq_printf(m, "%s%c", names[i],
1798                                            newline ? '\n' : ',');
1799                         }
1800                 }
1801         }
1802
1803         if (!newline)
1804                 seq_putc(m, '\n');
1805 }
1806
1807 void lfsck_time_dump(struct seq_file *m, time64_t time, const char *name)
1808 {
1809         if (time == 0) {
1810                 seq_printf(m, "%s_time: N/A\n", name);
1811                 seq_printf(m, "time_since_%s: N/A\n", name);
1812         } else {
1813                 seq_printf(m, "%s_time: %lld\n", name, time);
1814                 seq_printf(m, "time_since_%s: %lld seconds\n",
1815                            name, ktime_get_real_seconds() - time);
1816         }
1817 }
1818
1819 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1820                     const char *prefix)
1821 {
1822         if (fid_is_zero(&pos->lp_dir_parent)) {
1823                 if (pos->lp_oit_cookie == 0) {
1824                         seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1825                         return;
1826                 }
1827                 seq_printf(m, "%s: %llu, N/A, N/A\n",
1828                            prefix, pos->lp_oit_cookie);
1829         } else {
1830                 seq_printf(m, "%s: %llu, "DFID", %#llx\n",
1831                            prefix, pos->lp_oit_cookie,
1832                            PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1833         }
1834 }
1835
1836 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1837                     struct lfsck_position *pos, bool init)
1838 {
1839         const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1840
1841         if (unlikely(lfsck->li_di_oit == NULL)) {
1842                 memset(pos, 0, sizeof(*pos));
1843                 return;
1844         }
1845
1846         pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1847         if (!lfsck->li_current_oit_processed && !init)
1848                 pos->lp_oit_cookie--;
1849
1850         if (unlikely(pos->lp_oit_cookie == 0))
1851                 pos->lp_oit_cookie = 1;
1852
1853         if (lfsck->li_di_dir != NULL) {
1854                 struct dt_object *dto = lfsck->li_obj_dir;
1855
1856                 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1857                                                         lfsck->li_di_dir);
1858
1859                 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1860                         fid_zero(&pos->lp_dir_parent);
1861                         pos->lp_dir_cookie = 0;
1862                 } else {
1863                         pos->lp_dir_parent = *lfsck_dto2fid(dto);
1864                 }
1865         } else {
1866                 fid_zero(&pos->lp_dir_parent);
1867                 pos->lp_dir_cookie = 0;
1868         }
1869 }
1870
1871 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1872 {
1873         bool dirty = false;
1874
1875         if (limit != LFSCK_SPEED_NO_LIMIT) {
1876                 if (limit > cfs_time_seconds(1)) {
1877                         lfsck->li_sleep_rate = limit / cfs_time_seconds(1);
1878                         lfsck->li_sleep_jif = 1;
1879                 } else {
1880                         lfsck->li_sleep_rate = 1;
1881                         lfsck->li_sleep_jif = cfs_time_seconds(1) / limit;
1882                 }
1883         } else {
1884                 lfsck->li_sleep_jif = 0;
1885                 lfsck->li_sleep_rate = 0;
1886         }
1887
1888         if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1889                 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1890                 dirty = true;
1891         }
1892
1893         return dirty;
1894 }
1895
1896 void lfsck_control_speed(struct lfsck_instance *lfsck)
1897 {
1898         struct ptlrpc_thread *thread = &lfsck->li_thread;
1899
1900         if (lfsck->li_sleep_jif > 0 &&
1901             lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1902                 wait_event_idle_timeout(thread->t_ctl_waitq,
1903                                         !thread_is_running(thread),
1904                                         lfsck->li_sleep_jif);
1905                 lfsck->li_new_scanned = 0;
1906         }
1907 }
1908
1909 void lfsck_control_speed_by_self(struct lfsck_component *com)
1910 {
1911         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1912         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1913
1914         if (lfsck->li_sleep_jif > 0 &&
1915             com->lc_new_scanned >= lfsck->li_sleep_rate) {
1916                 wait_event_idle_timeout(thread->t_ctl_waitq,
1917                                         !thread_is_running(thread),
1918                                         lfsck->li_sleep_jif);
1919                 com->lc_new_scanned = 0;
1920         }
1921 }
1922
1923 static struct lfsck_thread_args *
1924 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1925                        struct lfsck_component *com,
1926                        struct lfsck_start_param *lsp)
1927 {
1928         struct lfsck_thread_args *lta;
1929         int                       rc;
1930
1931         OBD_ALLOC_PTR(lta);
1932         if (lta == NULL)
1933                 return ERR_PTR(-ENOMEM);
1934
1935         rc = lu_env_init(&lta->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1936         if (rc != 0) {
1937                 OBD_FREE_PTR(lta);
1938                 return ERR_PTR(rc);
1939         }
1940
1941         lta->lta_lfsck = lfsck_instance_get(lfsck);
1942         if (com != NULL)
1943                 lta->lta_com = lfsck_component_get(com);
1944
1945         lta->lta_lsp = lsp;
1946
1947         return lta;
1948 }
1949
1950 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1951 {
1952         if (lta->lta_com != NULL)
1953                 lfsck_component_put(&lta->lta_env, lta->lta_com);
1954         lfsck_instance_put(&lta->lta_env, lta->lta_lfsck);
1955         lu_env_fini(&lta->lta_env);
1956         OBD_FREE_PTR(lta);
1957 }
1958
1959 struct lfsck_assistant_data *
1960 lfsck_assistant_data_init(const struct lfsck_assistant_operations *lao,
1961                           const char *name)
1962 {
1963         struct lfsck_assistant_data *lad;
1964
1965         OBD_ALLOC_PTR(lad);
1966         if (lad != NULL) {
1967                 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1968                 if (lad->lad_bitmap == NULL) {
1969                         OBD_FREE_PTR(lad);
1970                         return NULL;
1971                 }
1972
1973                 INIT_LIST_HEAD(&lad->lad_req_list);
1974                 spin_lock_init(&lad->lad_lock);
1975                 INIT_LIST_HEAD(&lad->lad_ost_list);
1976                 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1977                 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1978                 INIT_LIST_HEAD(&lad->lad_mdt_list);
1979                 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1980                 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1981                 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1982                 lad->lad_ops = lao;
1983                 lad->lad_name = name;
1984         }
1985
1986         return lad;
1987 }
1988
1989 struct lfsck_assistant_object *
1990 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1991                             const struct lu_attr *attr, __u64 cookie,
1992                             bool is_dir)
1993 {
1994         struct lfsck_assistant_object   *lso;
1995
1996         OBD_ALLOC_PTR(lso);
1997         if (lso == NULL)
1998                 return ERR_PTR(-ENOMEM);
1999
2000         lso->lso_fid = *fid;
2001         if (attr != NULL)
2002                 lso->lso_attr = *attr;
2003
2004         atomic_set(&lso->lso_ref, 1);
2005         lso->lso_oit_cookie = cookie;
2006         if (is_dir)
2007                 lso->lso_is_dir = 1;
2008
2009         return lso;
2010 }
2011
2012 struct dt_object *
2013 lfsck_assistant_object_load(const struct lu_env *env,
2014                             struct lfsck_instance *lfsck,
2015                             struct lfsck_assistant_object *lso)
2016 {
2017         struct dt_object *obj;
2018
2019         obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2020         if (IS_ERR(obj))
2021                 return obj;
2022
2023         if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2024                 lso->lso_dead = 1;
2025                 lfsck_object_put(env, obj);
2026
2027                 return ERR_PTR(-ENOENT);
2028         }
2029
2030         if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2031                 lfsck_object_put(env, obj);
2032
2033                 return ERR_PTR(-ENOTDIR);
2034         }
2035
2036         return obj;
2037 }
2038
2039 /**
2040  * Generic LFSCK asynchronous communication interpretor function.
2041  * The LFSCK RPC reply for both the event notification and status
2042  * querying will be handled here.
2043  *
2044  * \param[in] env       pointer to the thread context
2045  * \param[in] req       pointer to the LFSCK request
2046  * \param[in] args      pointer to the lfsck_async_interpret_args
2047  * \param[in] rc        the result for handling the LFSCK request
2048  *
2049  * \retval              0 for success
2050  * \retval              negative error number on failure
2051  */
2052 int lfsck_async_interpret_common(const struct lu_env *env,
2053                                  struct ptlrpc_request *req,
2054                                  void *args, int rc)
2055 {
2056         struct lfsck_async_interpret_args *laia = args;
2057         struct lfsck_component            *com  = laia->laia_com;
2058         struct lfsck_assistant_data       *lad  = com->lc_data;
2059         struct lfsck_tgt_descs            *ltds = laia->laia_ltds;
2060         struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
2061         struct lfsck_request              *lr   = laia->laia_lr;
2062
2063         LASSERT(com->lc_lfsck->li_master);
2064
2065         switch (lr->lr_event) {
2066         case LE_START:
2067                 if (unlikely(rc == -EINPROGRESS)) {
2068                         ltd->ltd_retry_start = 1;
2069                         break;
2070                 }
2071
2072                 if (rc != 0) {
2073                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2074                                "start: rc = %d\n",
2075                                lfsck_lfsck2name(com->lc_lfsck),
2076                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2077                                ltd->ltd_index, lad->lad_name, rc);
2078
2079                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2080                                 struct lfsck_layout *lo = com->lc_file_ram;
2081
2082                                 if (lr->lr_flags & LEF_TO_OST)
2083                                         lfsck_lad_set_bitmap(env, com,
2084                                                              ltd->ltd_index);
2085                                 else
2086                                         lo->ll_flags |= LF_INCOMPLETE;
2087                         } else {
2088                                 struct lfsck_namespace *ns = com->lc_file_ram;
2089
2090                                 /* If some MDT does not join the namespace
2091                                  * LFSCK, then we cannot know whether there
2092                                  * is some name entry on such MDT that with
2093                                  * the referenced MDT-object on this MDT or
2094                                  * not. So the namespace LFSCK on this MDT
2095                                  * cannot handle orphan MDT-objects properly.
2096                                  * So we mark the LFSCK as LF_INCOMPLETE and
2097                                  * skip orphan MDT-objects handling. */
2098                                 ns->ln_flags |= LF_INCOMPLETE;
2099                         }
2100                         break;
2101                 }
2102
2103                 spin_lock(&ltds->ltd_lock);
2104                 if (ltd->ltd_dead) {
2105                         spin_unlock(&ltds->ltd_lock);
2106                         break;
2107                 }
2108
2109                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2110                         struct list_head *list;
2111                         struct list_head *phase_list;
2112
2113                         if (ltd->ltd_layout_done) {
2114                                 spin_unlock(&ltds->ltd_lock);
2115                                 break;
2116                         }
2117
2118                         if (lr->lr_flags & LEF_TO_OST) {
2119                                 list = &lad->lad_ost_list;
2120                                 phase_list = &lad->lad_ost_phase1_list;
2121                         } else {
2122                                 list = &lad->lad_mdt_list;
2123                                 phase_list = &lad->lad_mdt_phase1_list;
2124                         }
2125
2126                         if (list_empty(&ltd->ltd_layout_list))
2127                                 list_add_tail(&ltd->ltd_layout_list, list);
2128                         if (list_empty(&ltd->ltd_layout_phase_list))
2129                                 list_add_tail(&ltd->ltd_layout_phase_list,
2130                                               phase_list);
2131                 } else {
2132                         if (ltd->ltd_namespace_done) {
2133                                 spin_unlock(&ltds->ltd_lock);
2134                                 break;
2135                         }
2136
2137                         if (list_empty(&ltd->ltd_namespace_list))
2138                                 list_add_tail(&ltd->ltd_namespace_list,
2139                                               &lad->lad_mdt_list);
2140                         if (list_empty(&ltd->ltd_namespace_phase_list))
2141                                 list_add_tail(&ltd->ltd_namespace_phase_list,
2142                                               &lad->lad_mdt_phase1_list);
2143                 }
2144                 spin_unlock(&ltds->ltd_lock);
2145                 break;
2146         case LE_STOP:
2147         case LE_PHASE1_DONE:
2148         case LE_PHASE2_DONE:
2149         case LE_PEER_EXIT:
2150                 if (rc != 0 && rc != -EALREADY)
2151                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2152                               "event = %d, rc = %d\n",
2153                               lfsck_lfsck2name(com->lc_lfsck),
2154                               (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2155                               ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2156                 break;
2157         case LE_QUERY: {
2158                 struct lfsck_reply *reply;
2159                 struct list_head *list;
2160                 struct list_head *phase_list;
2161
2162                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2163                         list = &ltd->ltd_layout_list;
2164                         phase_list = &ltd->ltd_layout_phase_list;
2165                 } else {
2166                         list = &ltd->ltd_namespace_list;
2167                         phase_list = &ltd->ltd_namespace_phase_list;
2168                 }
2169
2170                 if (rc != 0) {
2171                         if (lr->lr_flags & LEF_QUERY_ALL) {
2172                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2173                                 break;
2174                         }
2175
2176                         spin_lock(&ltds->ltd_lock);
2177                         list_del_init(phase_list);
2178                         list_del_init(list);
2179                         spin_unlock(&ltds->ltd_lock);
2180                         break;
2181                 }
2182
2183                 reply = req_capsule_server_get(&req->rq_pill,
2184                                                &RMF_LFSCK_REPLY);
2185                 if (reply == NULL) {
2186                         rc = -EPROTO;
2187                         CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2188                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2189                                lad->lad_name, rc);
2190
2191                         if (lr->lr_flags & LEF_QUERY_ALL) {
2192                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2193                                 break;
2194                         }
2195
2196                         spin_lock(&ltds->ltd_lock);
2197                         list_del_init(phase_list);
2198                         list_del_init(list);
2199                         spin_unlock(&ltds->ltd_lock);
2200                         break;
2201                 }
2202
2203                 if (lr->lr_flags & LEF_QUERY_ALL) {
2204                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2205                                 ltd->ltd_layout_status = reply->lr_status;
2206                                 ltd->ltd_layout_repaired = reply->lr_repaired;
2207                         } else {
2208                                 ltd->ltd_namespace_status = reply->lr_status;
2209                                 ltd->ltd_namespace_repaired =
2210                                                         reply->lr_repaired;
2211                         }
2212                         break;
2213                 }
2214
2215                 switch (reply->lr_status) {
2216                 case LS_SCANNING_PHASE1:
2217                         break;
2218                 case LS_SCANNING_PHASE2:
2219                         spin_lock(&ltds->ltd_lock);
2220                         list_del_init(phase_list);
2221                         if (ltd->ltd_dead) {
2222                                 spin_unlock(&ltds->ltd_lock);
2223                                 break;
2224                         }
2225
2226                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2227                                 if (ltd->ltd_layout_done) {
2228                                         spin_unlock(&ltds->ltd_lock);
2229                                         break;
2230                                 }
2231
2232                                 if (lr->lr_flags & LEF_TO_OST)
2233                                         list_add_tail(phase_list,
2234                                                 &lad->lad_ost_phase2_list);
2235                                 else
2236                                         list_add_tail(phase_list,
2237                                                 &lad->lad_mdt_phase2_list);
2238                         } else {
2239                                 if (ltd->ltd_namespace_done) {
2240                                         spin_unlock(&ltds->ltd_lock);
2241                                         break;
2242                                 }
2243
2244                                 list_add_tail(phase_list,
2245                                               &lad->lad_mdt_phase2_list);
2246                         }
2247                         spin_unlock(&ltds->ltd_lock);
2248                         break;
2249                 default:
2250                         spin_lock(&ltds->ltd_lock);
2251                         list_del_init(phase_list);
2252                         list_del_init(list);
2253                         spin_unlock(&ltds->ltd_lock);
2254                         break;
2255                 }
2256                 break;
2257         }
2258         default:
2259                 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2260                        lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2261                 break;
2262         }
2263
2264         if (!laia->laia_shared) {
2265                 lfsck_tgt_put(ltd);
2266                 lfsck_component_put(env, com);
2267         }
2268
2269         return 0;
2270 }
2271
2272 static void lfsck_interpret(const struct lu_env *env,
2273                             struct lfsck_instance *lfsck,
2274                             struct ptlrpc_request *req, void *args, int result)
2275 {
2276         struct lfsck_async_interpret_args *laia = args;
2277         struct lfsck_component            *com;
2278
2279         LASSERT(laia->laia_com == NULL);
2280         LASSERT(laia->laia_shared);
2281
2282         spin_lock(&lfsck->li_lock);
2283         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2284                 laia->laia_com = com;
2285                 lfsck_async_interpret_common(env, req, laia, result);
2286         }
2287
2288         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2289                 laia->laia_com = com;
2290                 lfsck_async_interpret_common(env, req, laia, result);
2291         }
2292         spin_unlock(&lfsck->li_lock);
2293 }
2294
2295 static int lfsck_stop_notify(const struct lu_env *env,
2296                              struct lfsck_instance *lfsck,
2297                              struct lfsck_tgt_descs *ltds,
2298                              struct lfsck_tgt_desc *ltd, __u16 type)
2299 {
2300         struct lfsck_component *com;
2301         int                     rc = 0;
2302         ENTRY;
2303
2304         LASSERT(lfsck->li_master);
2305
2306         spin_lock(&lfsck->li_lock);
2307         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2308         if (com == NULL)
2309                 com = __lfsck_component_find(lfsck, type,
2310                                              &lfsck->li_list_double_scan);
2311         if (com != NULL)
2312                 lfsck_component_get(com);
2313         spin_unlock(&lfsck->li_lock);
2314
2315         if (com != NULL) {
2316                 struct lfsck_thread_info          *info  = lfsck_env_info(env);
2317                 struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2318                 struct lfsck_request              *lr    = &info->lti_lr;
2319                 struct lfsck_assistant_data       *lad   = com->lc_data;
2320                 struct list_head                  *list;
2321                 struct list_head                  *phase_list;
2322                 struct ptlrpc_request_set         *set;
2323
2324                 set = ptlrpc_prep_set();
2325                 if (set == NULL) {
2326                         lfsck_component_put(env, com);
2327
2328                         RETURN(-ENOMEM);
2329                 }
2330
2331                 if (type == LFSCK_TYPE_LAYOUT) {
2332                         list = &ltd->ltd_layout_list;
2333                         phase_list = &ltd->ltd_layout_phase_list;
2334                 } else {
2335                         list = &ltd->ltd_namespace_list;
2336                         phase_list = &ltd->ltd_namespace_phase_list;
2337                 }
2338
2339                 spin_lock(&ltds->ltd_lock);
2340                 if (list_empty(list)) {
2341                         LASSERT(list_empty(phase_list));
2342                         spin_unlock(&ltds->ltd_lock);
2343                         ptlrpc_set_destroy(set);
2344
2345                         RETURN(0);
2346                 }
2347
2348                 list_del_init(phase_list);
2349                 list_del_init(list);
2350                 spin_unlock(&ltds->ltd_lock);
2351
2352                 memset(lr, 0, sizeof(*lr));
2353                 lr->lr_index = lfsck_dev_idx(lfsck);
2354                 lr->lr_event = LE_PEER_EXIT;
2355                 lr->lr_active = type;
2356                 lr->lr_status = LS_CO_PAUSED;
2357                 if (ltds == &lfsck->li_ost_descs)
2358                         lr->lr_flags = LEF_TO_OST;
2359
2360                 memset(laia, 0, sizeof(*laia));
2361                 laia->laia_com = com;
2362                 laia->laia_ltds = ltds;
2363                 atomic_inc(&ltd->ltd_ref);
2364                 laia->laia_ltd = ltd;
2365                 laia->laia_lr = lr;
2366
2367                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2368                                          lfsck_async_interpret_common,
2369                                          laia, LFSCK_NOTIFY);
2370                 if (rc != 0) {
2371                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2372                                "co-stop for %s: rc = %d\n",
2373                                lfsck_lfsck2name(lfsck),
2374                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2375                                ltd->ltd_index, lad->lad_name, rc);
2376                         lfsck_tgt_put(ltd);
2377                 } else {
2378                         rc = ptlrpc_set_wait(env, set);
2379                 }
2380
2381                 ptlrpc_set_destroy(set);
2382                 lfsck_component_put(env, com);
2383         }
2384
2385         RETURN(rc);
2386 }
2387
2388 static int lfsck_async_interpret(const struct lu_env *env,
2389                                  struct ptlrpc_request *req,
2390                                  void *args, int rc)
2391 {
2392         struct lfsck_async_interpret_args *laia = args;
2393         struct lfsck_instance             *lfsck;
2394
2395         lfsck = container_of(laia->laia_ltds, struct lfsck_instance,
2396                              li_mdt_descs);
2397         lfsck_interpret(env, lfsck, req, laia, rc);
2398         lfsck_tgt_put(laia->laia_ltd);
2399         if (rc != 0 && laia->laia_result != -EALREADY)
2400                 laia->laia_result = rc;
2401
2402         return 0;
2403 }
2404
2405 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2406                         struct lfsck_request *lr,
2407                         struct ptlrpc_request_set *set,
2408                         ptlrpc_interpterer_t interpreter,
2409                         void *args, int request)
2410 {
2411         struct lfsck_async_interpret_args *laia;
2412         struct ptlrpc_request             *req;
2413         struct lfsck_request              *tmp;
2414         struct req_format                 *format;
2415         int                                rc;
2416
2417         switch (request) {
2418         case LFSCK_NOTIFY:
2419                 format = &RQF_LFSCK_NOTIFY;
2420                 break;
2421         case LFSCK_QUERY:
2422                 format = &RQF_LFSCK_QUERY;
2423                 break;
2424         default:
2425                 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2426                        exp->exp_obd->obd_name, request, -EINVAL);
2427                 return -EINVAL;
2428         }
2429
2430         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2431         if (req == NULL)
2432                 return -ENOMEM;
2433
2434         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2435         if (rc != 0) {
2436                 ptlrpc_request_free(req);
2437
2438                 return rc;
2439         }
2440
2441         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2442         *tmp = *lr;
2443         ptlrpc_request_set_replen(req);
2444
2445         laia = ptlrpc_req_async_args(laia, req);
2446         *laia = *(struct lfsck_async_interpret_args *)args;
2447         if (laia->laia_com != NULL)
2448                 lfsck_component_get(laia->laia_com);
2449         req->rq_interpret_reply = interpreter;
2450         req->rq_allow_intr = 1;
2451         req->rq_no_delay = 1;
2452         ptlrpc_set_add_req(set, req);
2453
2454         return 0;
2455 }
2456
2457 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2458 {
2459         struct lfsck_thread_info          *info  = lfsck_env_info(env);
2460         struct lfsck_request              *lr    = &info->lti_lr;
2461         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2462         struct lfsck_instance             *lfsck = com->lc_lfsck;
2463         struct lfsck_tgt_descs            *ltds  = &lfsck->li_mdt_descs;
2464         struct lfsck_tgt_desc             *ltd;
2465         struct ptlrpc_request_set         *set;
2466         int                                idx;
2467         int                                rc;
2468         ENTRY;
2469
2470         memset(lr, 0, sizeof(*lr));
2471         lr->lr_event = LE_QUERY;
2472         lr->lr_active = com->lc_type;
2473         lr->lr_flags = LEF_QUERY_ALL;
2474
2475         memset(laia, 0, sizeof(*laia));
2476         laia->laia_com = com;
2477         laia->laia_lr = lr;
2478
2479         set = ptlrpc_prep_set();
2480         if (set == NULL)
2481                 RETURN(-ENOMEM);
2482
2483 again:
2484         laia->laia_ltds = ltds;
2485         down_read(&ltds->ltd_rw_sem);
2486         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2487                 ltd = lfsck_tgt_get(ltds, idx);
2488                 LASSERT(ltd != NULL);
2489
2490                 laia->laia_ltd = ltd;
2491                 up_read(&ltds->ltd_rw_sem);
2492                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2493                                          lfsck_async_interpret_common,
2494                                          laia, LFSCK_QUERY);
2495                 if (rc != 0) {
2496                         struct lfsck_assistant_data *lad = com->lc_data;
2497
2498                         CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2499                                "rc = %d\n", lfsck_lfsck2name(lfsck),
2500                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2501                                ltd->ltd_index, lad->lad_name, rc);
2502                         lfsck_reset_ltd_status(ltd, com->lc_type);
2503                         lfsck_tgt_put(ltd);
2504                 }
2505                 down_read(&ltds->ltd_rw_sem);
2506         }
2507         up_read(&ltds->ltd_rw_sem);
2508
2509         if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2510                 ltds = &lfsck->li_ost_descs;
2511                 lr->lr_flags |= LEF_TO_OST;
2512                 goto again;
2513         }
2514
2515         rc = ptlrpc_set_wait(env, set);
2516         ptlrpc_set_destroy(set);
2517
2518         RETURN(rc);
2519 }
2520
2521 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2522                           struct lfsck_start_param *lsp)
2523 {
2524         struct lfsck_instance           *lfsck   = com->lc_lfsck;
2525         struct lfsck_assistant_data     *lad     = com->lc_data;
2526         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
2527         struct ptlrpc_thread            *athread = &lad->lad_thread;
2528         struct lfsck_thread_args        *lta;
2529         struct task_struct              *task;
2530         int                              rc;
2531         ENTRY;
2532
2533         lad->lad_assistant_status = 0;
2534         lad->lad_post_result = 0;
2535         lad->lad_flags = 0;
2536         lad->lad_advance_lock = false;
2537         thread_set_flags(athread, 0);
2538
2539         lta = lfsck_thread_args_init(lfsck, com, lsp);
2540         if (IS_ERR(lta))
2541                 RETURN(PTR_ERR(lta));
2542
2543         task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2544         if (IS_ERR(task)) {
2545                 rc = PTR_ERR(task);
2546                 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2547                        "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2548                 lfsck_thread_args_fini(lta);
2549         } else {
2550                 wait_event_idle(mthread->t_ctl_waitq,
2551                                 thread_is_running(athread) ||
2552                                 thread_is_stopped(athread) ||
2553                                 !thread_is_starting(mthread));
2554                 if (unlikely(!thread_is_starting(mthread)))
2555                         /* stopped by race */
2556                         rc = -ESRCH;
2557                 else if (unlikely(!thread_is_running(athread)))
2558                         rc = lad->lad_assistant_status;
2559                 else
2560                         rc = 0;
2561         }
2562
2563         RETURN(rc);
2564 }
2565
2566 int lfsck_checkpoint_generic(const struct lu_env *env,
2567                              struct lfsck_component *com)
2568 {
2569         struct lfsck_assistant_data     *lad     = com->lc_data;
2570         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2571         struct ptlrpc_thread            *athread = &lad->lad_thread;
2572
2573         wait_event_idle(mthread->t_ctl_waitq,
2574                         list_empty(&lad->lad_req_list) ||
2575                         !thread_is_running(mthread) ||
2576                         thread_is_stopped(athread));
2577
2578         if (!thread_is_running(mthread) || thread_is_stopped(athread))
2579                 return LFSCK_CHECKPOINT_SKIP;
2580
2581         return 0;
2582 }
2583
2584 void lfsck_post_generic(const struct lu_env *env,
2585                         struct lfsck_component *com, int *result)
2586 {
2587         struct lfsck_assistant_data     *lad     = com->lc_data;
2588         struct ptlrpc_thread            *athread = &lad->lad_thread;
2589         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2590
2591         lad->lad_post_result = *result;
2592         if (*result <= 0)
2593                 set_bit(LAD_EXIT, &lad->lad_flags);
2594         set_bit(LAD_TO_POST, &lad->lad_flags);
2595
2596         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2597                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2598
2599         wake_up_all(&athread->t_ctl_waitq);
2600         wait_event_idle(mthread->t_ctl_waitq,
2601                         (*result > 0 && list_empty(&lad->lad_req_list)) ||
2602                         thread_is_stopped(athread));
2603
2604         if (lad->lad_assistant_status < 0)
2605                 *result = lad->lad_assistant_status;
2606
2607         CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2608                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2609 }
2610
2611 int lfsck_double_scan_generic(const struct lu_env *env,
2612                               struct lfsck_component *com, int status)
2613 {
2614         struct lfsck_assistant_data     *lad     = com->lc_data;
2615         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2616         struct ptlrpc_thread            *athread = &lad->lad_thread;
2617
2618         if (status != LS_SCANNING_PHASE2)
2619                 set_bit(LAD_EXIT, &lad->lad_flags);
2620         else
2621                 set_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags);
2622
2623         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2624                "status %d\n",
2625                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2626
2627         wake_up_all(&athread->t_ctl_waitq);
2628         wait_event_idle(mthread->t_ctl_waitq,
2629                         test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags) ||
2630                         thread_is_stopped(athread));
2631
2632         CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2633                "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2634                lad->lad_assistant_status);
2635
2636         if (lad->lad_assistant_status < 0)
2637                 return lad->lad_assistant_status;
2638
2639         return 0;
2640 }
2641
2642 void lfsck_quit_generic(const struct lu_env *env,
2643                         struct lfsck_component *com)
2644 {
2645         struct lfsck_assistant_data     *lad     = com->lc_data;
2646         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2647         struct ptlrpc_thread            *athread = &lad->lad_thread;
2648
2649         set_bit(LAD_EXIT, &lad->lad_flags);
2650         wake_up_all(&athread->t_ctl_waitq);
2651         wait_event_idle(mthread->t_ctl_waitq,
2652                         thread_is_init(athread) ||
2653                         thread_is_stopped(athread));
2654 }
2655
2656 int lfsck_load_one_trace_file(const struct lu_env *env,
2657                               struct lfsck_component *com,
2658                               struct dt_object *parent,
2659                               struct dt_object **child,
2660                               const struct dt_index_features *ft,
2661                               const char *name, bool reset)
2662 {
2663         struct lfsck_instance *lfsck = com->lc_lfsck;
2664         struct dt_object *obj;
2665         int rc;
2666         ENTRY;
2667
2668         if (*child != NULL) {
2669                 struct dt_it *it;
2670                 const struct dt_it_ops *iops;
2671                 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2672
2673                 if (!reset)
2674                         RETURN(0);
2675
2676                 obj = *child;
2677                 rc = obj->do_ops->do_index_try(env, obj, ft);
2678                 if (rc)
2679                         /* unlink by force */
2680                         goto unlink;
2681
2682                 iops = &obj->do_index_ops->dio_it;
2683                 it = iops->init(env, obj, 0);
2684                 if (IS_ERR(it))
2685                         /* unlink by force */
2686                         goto unlink;
2687
2688                 fid_zero(fid);
2689                 rc = iops->get(env, it, (const struct dt_key *)fid);
2690                 if (rc >= 0) {
2691                         rc = iops->next(env, it);
2692                         iops->put(env, it);
2693                 }
2694                 iops->fini(env, it);
2695                 if (rc > 0)
2696                         /* "rc > 0" means the index file is empty. */
2697                         RETURN(0);
2698
2699 unlink:
2700                 /* The old index is not empty, remove it firstly. */
2701                 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2702                 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2703                              "%s: unlink lfsck sub trace file %s: rc = %d\n",
2704                              lfsck_lfsck2name(com->lc_lfsck), name, rc);
2705                 if (rc)
2706                         RETURN(rc);
2707
2708                 if (*child) {
2709                         lfsck_object_put(env, *child);
2710                         *child = NULL;
2711                 }
2712         } else if (reset) {
2713                 goto unlink;
2714         }
2715
2716         obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2717                                          S_IFREG | S_IRUGO | S_IWUSR, ft);
2718         if (IS_ERR(obj))
2719                 RETURN(PTR_ERR(obj));
2720
2721         rc = obj->do_ops->do_index_try(env, obj, ft);
2722         if (rc) {
2723                 lfsck_object_put(env, obj);
2724                 CDEBUG(D_LFSCK, "%s: LFSCK fail to load "
2725                        "sub trace file %s: rc = %d\n",
2726                        lfsck_lfsck2name(com->lc_lfsck), name, rc);
2727         } else {
2728                 *child = obj;
2729         }
2730
2731         RETURN(rc);
2732 }
2733
2734 int lfsck_load_sub_trace_files(const struct lu_env *env,
2735                                struct lfsck_component *com,
2736                                const struct dt_index_features *ft,
2737                                const char *prefix, bool reset)
2738 {
2739         char *name = lfsck_env_info(env)->lti_key;
2740         struct lfsck_sub_trace_obj *lsto;
2741         int rc;
2742         int i;
2743
2744         for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2745              i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2746                 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2747                 rc = lfsck_load_one_trace_file(env, com,
2748                                 com->lc_lfsck->li_lfsck_dir,
2749                                 &lsto->lsto_obj, ft, name, reset);
2750         }
2751
2752         return rc;
2753 }
2754
2755 /* external interfaces */
2756 int lfsck_get_speed(char *buf, struct dt_device *key)
2757 {
2758         struct lu_env           env;
2759         struct lfsck_instance  *lfsck;
2760         int                     rc;
2761         ENTRY;
2762
2763         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2764         if (rc != 0)
2765                 RETURN(rc);
2766
2767         lfsck = lfsck_instance_find(key, true, false);
2768         if (lfsck && buf) {
2769                 rc = sprintf(buf, "%u\n",
2770                              lfsck->li_bookmark_ram.lb_speed_limit);
2771                 lfsck_instance_put(&env, lfsck);
2772         } else {
2773                 rc = -ENXIO;
2774         }
2775
2776         lu_env_fini(&env);
2777
2778         RETURN(rc);
2779 }
2780 EXPORT_SYMBOL(lfsck_get_speed);
2781
2782 int lfsck_set_speed(struct dt_device *key, __u32 val)
2783 {
2784         struct lu_env           env;
2785         struct lfsck_instance  *lfsck;
2786         int                     rc;
2787         ENTRY;
2788
2789         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2790         if (rc != 0)
2791                 RETURN(rc);
2792
2793         lfsck = lfsck_instance_find(key, true, false);
2794         if (likely(lfsck != NULL)) {
2795                 mutex_lock(&lfsck->li_mutex);
2796                 if (__lfsck_set_speed(lfsck, val))
2797                         rc = lfsck_bookmark_store(&env, lfsck);
2798                 mutex_unlock(&lfsck->li_mutex);
2799                 lfsck_instance_put(&env, lfsck);
2800         } else {
2801                 rc = -ENXIO;
2802         }
2803
2804         lu_env_fini(&env);
2805
2806         RETURN(rc);
2807 }
2808 EXPORT_SYMBOL(lfsck_set_speed);
2809
2810 int lfsck_get_windows(char *buf, struct dt_device *key)
2811 {
2812         struct lu_env           env;
2813         struct lfsck_instance  *lfsck;
2814         int                     rc;
2815         ENTRY;
2816
2817         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2818         if (rc != 0)
2819                 RETURN(rc);
2820
2821         lfsck = lfsck_instance_find(key, true, false);
2822         if (likely(lfsck != NULL)) {
2823                 rc = sprintf(buf, "%u\n",
2824                              lfsck->li_bookmark_ram.lb_async_windows);
2825                 lfsck_instance_put(&env, lfsck);
2826         } else {
2827                 rc = -ENXIO;
2828         }
2829
2830         lu_env_fini(&env);
2831
2832         RETURN(rc);
2833 }
2834 EXPORT_SYMBOL(lfsck_get_windows);
2835
2836 int lfsck_set_windows(struct dt_device *key, unsigned int val)
2837 {
2838         struct lu_env           env;
2839         struct lfsck_instance  *lfsck;
2840         int                     rc;
2841         ENTRY;
2842
2843         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2844         if (rc != 0)
2845                 RETURN(rc);
2846
2847         lfsck = lfsck_instance_find(key, true, false);
2848         if (likely(lfsck != NULL)) {
2849                 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2850                         CWARN("%s: invalid async windows size that may "
2851                               "cause memory issues. The valid range is "
2852                               "[1 - %u].\n",
2853                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2854                         rc = -EINVAL;
2855                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2856                         mutex_lock(&lfsck->li_mutex);
2857                         lfsck->li_bookmark_ram.lb_async_windows = val;
2858                         rc = lfsck_bookmark_store(&env, lfsck);
2859                         mutex_unlock(&lfsck->li_mutex);
2860                 }
2861                 lfsck_instance_put(&env, lfsck);
2862         } else {
2863                 rc = -ENXIO;
2864         }
2865
2866         lu_env_fini(&env);
2867
2868         RETURN(rc);
2869 }
2870 EXPORT_SYMBOL(lfsck_set_windows);
2871
2872 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2873 {
2874         struct lu_env           env;
2875         struct lfsck_instance  *lfsck;
2876         struct lfsck_component *com;
2877         int                     rc;
2878         ENTRY;
2879
2880         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2881         if (rc != 0)
2882                 RETURN(rc);
2883
2884         lfsck = lfsck_instance_find(key, true, false);
2885         if (likely(lfsck != NULL)) {
2886                 com = lfsck_component_find(lfsck, type);
2887                 if (likely(com != NULL)) {
2888                         com->lc_ops->lfsck_dump(&env, com, m);
2889                         lfsck_component_put(&env, com);
2890                 } else {
2891                         rc = -ENOTSUPP;
2892                 }
2893
2894                 lfsck_instance_put(&env, lfsck);
2895         } else {
2896                 rc = -ENXIO;
2897         }
2898
2899         lu_env_fini(&env);
2900
2901         RETURN(rc);
2902 }
2903 EXPORT_SYMBOL(lfsck_dump);
2904
2905 static int lfsck_stop_all(const struct lu_env *env,
2906                           struct lfsck_instance *lfsck,
2907                           struct lfsck_stop *stop)
2908 {
2909         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2910         struct lfsck_request              *lr     = &info->lti_lr;
2911         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2912         struct ptlrpc_request_set         *set;
2913         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2914         struct lfsck_tgt_desc             *ltd;
2915         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2916         __u32                              idx;
2917         int                                rc     = 0;
2918         int                                rc1    = 0;
2919         ENTRY;
2920
2921         LASSERT(stop->ls_flags & LPF_BROADCAST);
2922
2923         set = ptlrpc_prep_set();
2924         if (unlikely(set == NULL))
2925                 RETURN(-ENOMEM);
2926
2927         memset(lr, 0, sizeof(*lr));
2928         lr->lr_event = LE_STOP;
2929         lr->lr_index = lfsck_dev_idx(lfsck);
2930         lr->lr_status = stop->ls_status;
2931         lr->lr_version = bk->lb_version;
2932         lr->lr_active = LFSCK_TYPES_ALL;
2933         lr->lr_param = stop->ls_flags;
2934
2935         memset(laia, 0, sizeof(*laia));
2936         laia->laia_ltds = ltds;
2937         laia->laia_lr = lr;
2938         laia->laia_shared = 1;
2939
2940         down_read(&ltds->ltd_rw_sem);
2941         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2942                 ltd = lfsck_tgt_get(ltds, idx);
2943                 LASSERT(ltd != NULL);
2944
2945                 laia->laia_ltd = ltd;
2946                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2947                                          lfsck_async_interpret, laia,
2948                                          LFSCK_NOTIFY);
2949                 if (rc != 0) {
2950                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2951                         lfsck_tgt_put(ltd);
2952                         CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2953                                "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2954                         rc1 = rc;
2955                 }
2956         }
2957         up_read(&ltds->ltd_rw_sem);
2958
2959         rc = ptlrpc_set_wait(env, set);
2960         ptlrpc_set_destroy(set);
2961
2962         if (rc == 0)
2963                 rc = laia->laia_result;
2964
2965         if (rc == -EALREADY)
2966                 rc = 0;
2967
2968         if (rc != 0)
2969                 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2970                        lfsck_lfsck2name(lfsck), rc);
2971
2972         RETURN(rc != 0 ? rc : rc1);
2973 }
2974
2975 static int lfsck_start_all(const struct lu_env *env,
2976                            struct lfsck_instance *lfsck,
2977                            struct lfsck_start *start)
2978 {
2979         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2980         struct lfsck_request              *lr     = &info->lti_lr;
2981         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2982         struct ptlrpc_request_set         *set;
2983         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2984         struct lfsck_tgt_desc             *ltd;
2985         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2986         __u32                              idx;
2987         int                                rc     = 0;
2988         bool retry = false;
2989         ENTRY;
2990
2991         LASSERT(start->ls_flags & LPF_BROADCAST);
2992
2993         memset(lr, 0, sizeof(*lr));
2994         lr->lr_event = LE_START;
2995         lr->lr_index = lfsck_dev_idx(lfsck);
2996         lr->lr_speed = bk->lb_speed_limit;
2997         lr->lr_version = bk->lb_version;
2998         lr->lr_active = start->ls_active;
2999         lr->lr_param = start->ls_flags;
3000         lr->lr_async_windows = bk->lb_async_windows;
3001         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
3002                        LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
3003                        LSV_CREATE_MDTOBJ;
3004
3005         memset(laia, 0, sizeof(*laia));
3006         laia->laia_ltds = ltds;
3007         laia->laia_lr = lr;
3008         laia->laia_shared = 1;
3009
3010 again:
3011         set = ptlrpc_prep_set();
3012         if (unlikely(!set))
3013                 RETURN(-ENOMEM);
3014
3015         down_read(&ltds->ltd_rw_sem);
3016         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3017                 ltd = lfsck_tgt_get(ltds, idx);
3018                 LASSERT(ltd != NULL);
3019
3020                 if (retry && !ltd->ltd_retry_start) {
3021                         lfsck_tgt_put(ltd);
3022                         continue;
3023                 }
3024
3025                 laia->laia_ltd = ltd;
3026                 ltd->ltd_retry_start = 0;
3027                 ltd->ltd_layout_done = 0;
3028                 ltd->ltd_namespace_done = 0;
3029                 ltd->ltd_synced_failures = 0;
3030                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3031                                          lfsck_async_interpret, laia,
3032                                          LFSCK_NOTIFY);
3033                 if (rc != 0) {
3034                         lfsck_interpret(env, lfsck, NULL, laia, rc);
3035                         lfsck_tgt_put(ltd);
3036                         CERROR("%s: cannot notify MDT %x for LFSCK "
3037                                "start, failout: rc = %d\n",
3038                                lfsck_lfsck2name(lfsck), idx, rc);
3039                         break;
3040                 }
3041         }
3042         up_read(&ltds->ltd_rw_sem);
3043
3044         if (rc != 0) {
3045                 ptlrpc_set_destroy(set);
3046
3047                 RETURN(rc);
3048         }
3049
3050         rc = ptlrpc_set_wait(env, set);
3051         ptlrpc_set_destroy(set);
3052
3053         if (rc == 0)
3054                 rc = laia->laia_result;
3055
3056         if (unlikely(rc == -EINPROGRESS)) {
3057                 retry = true;
3058                 schedule_timeout_interruptible(cfs_time_seconds(1));
3059                 set_current_state(TASK_RUNNING);
3060                 if (!signal_pending(current) &&
3061                     thread_is_running(&lfsck->li_thread))
3062                         goto again;
3063
3064                 rc = -EINTR;
3065         }
3066
3067         if (rc != 0) {
3068                 struct lfsck_stop *stop = &info->lti_stop;
3069
3070                 CERROR("%s: cannot start LFSCK on some MDTs, "
3071                        "stop all: rc = %d\n",
3072                        lfsck_lfsck2name(lfsck), rc);
3073                 if (rc != -EALREADY) {
3074                         stop->ls_status = LS_FAILED;
3075                         stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3076                         lfsck_stop_all(env, lfsck, stop);
3077                 }
3078         }
3079
3080         RETURN(rc);
3081 }
3082
3083 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3084                 struct lfsck_start_param *lsp)
3085 {
3086         struct lfsck_start              *start  = lsp->lsp_start;
3087         struct lfsck_instance           *lfsck;
3088         struct lfsck_bookmark           *bk;
3089         struct ptlrpc_thread            *thread;
3090         struct lfsck_component          *com;
3091         struct lfsck_thread_args        *lta;
3092         struct task_struct              *task;
3093         struct lfsck_tgt_descs          *ltds;
3094         struct lfsck_tgt_desc           *ltd;
3095         __u32                            idx;
3096         int                              rc     = 0;
3097         __u16                            valid  = 0;
3098         __u16                            flags  = 0;
3099         __u16                            type   = 1;
3100         ENTRY;
3101
3102         if (key->dd_rdonly)
3103                 RETURN(-EROFS);
3104
3105         lfsck = lfsck_instance_find(key, true, false);
3106         if (unlikely(lfsck == NULL))
3107                 RETURN(-ENXIO);
3108
3109         if (unlikely(lfsck->li_stopping))
3110                 GOTO(put, rc = -ENXIO);
3111
3112         /* System is not ready, try again later. */
3113         if (unlikely(lfsck->li_namespace == NULL ||
3114                      lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3115                 GOTO(put, rc = -EINPROGRESS);
3116
3117         /* start == NULL means auto trigger paused LFSCK. */
3118         if (!start) {
3119                 if (list_empty(&lfsck->li_list_scan) ||
3120                     OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3121                         GOTO(put, rc = 0);
3122         } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3123                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3124                        lfsck_lfsck2name(lfsck));
3125
3126                 GOTO(put, rc = -EPERM);
3127         }
3128
3129         bk = &lfsck->li_bookmark_ram;
3130         thread = &lfsck->li_thread;
3131         mutex_lock(&lfsck->li_mutex);
3132         spin_lock(&lfsck->li_lock);
3133         if (unlikely(thread_is_stopping(thread))) {
3134                 /* Someone is stopping the LFSCK. */
3135                 spin_unlock(&lfsck->li_lock);
3136                 GOTO(out, rc = -EBUSY);
3137         }
3138
3139         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3140                 rc = -EALREADY;
3141                 if (unlikely(start == NULL)) {
3142                         spin_unlock(&lfsck->li_lock);
3143                         GOTO(out, rc);
3144                 }
3145
3146                 while (start->ls_active != 0) {
3147                         if (!(type & start->ls_active)) {
3148                                 type <<= 1;
3149                                 continue;
3150                         }
3151
3152                         com = __lfsck_component_find(lfsck, type,
3153                                                      &lfsck->li_list_scan);
3154                         if (com == NULL)
3155                                 com = __lfsck_component_find(lfsck, type,
3156                                                 &lfsck->li_list_double_scan);
3157                         if (com == NULL) {
3158                                 rc = -EOPNOTSUPP;
3159                                 break;
3160                         }
3161
3162                         if (com->lc_ops->lfsck_join != NULL) {
3163                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
3164                                 if (rc != 0 && rc != -EALREADY)
3165                                         break;
3166                         }
3167                         start->ls_active &= ~type;
3168                         type <<= 1;
3169                 }
3170                 spin_unlock(&lfsck->li_lock);
3171                 GOTO(out, rc);
3172         }
3173         spin_unlock(&lfsck->li_lock);
3174
3175         lfsck->li_status = 0;
3176         lfsck->li_oit_over = 0;
3177         lfsck->li_start_unplug = 0;
3178         lfsck->li_drop_dryrun = 0;
3179         lfsck->li_new_scanned = 0;
3180
3181         /* For auto trigger. */
3182         if (start == NULL)
3183                 goto trigger;
3184
3185         start->ls_version = bk->lb_version;
3186
3187         if (start->ls_active != 0) {
3188                 struct lfsck_component *next;
3189
3190                 if (start->ls_active == LFSCK_TYPES_ALL)
3191                         start->ls_active = LFSCK_TYPES_SUPPORTED;
3192
3193                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3194                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3195                         GOTO(out, rc = -ENOTSUPP);
3196                 }
3197
3198                 list_for_each_entry_safe(com, next,
3199                                          &lfsck->li_list_scan, lc_link) {
3200                         if (!(com->lc_type & start->ls_active)) {
3201                                 rc = com->lc_ops->lfsck_post(env, com, 0,
3202                                                              false);
3203                                 if (rc != 0)
3204                                         GOTO(out, rc);
3205                         }
3206                 }
3207
3208                 while (start->ls_active != 0) {
3209                         if (type & start->ls_active) {
3210                                 com = __lfsck_component_find(lfsck, type,
3211                                                         &lfsck->li_list_idle);
3212                                 if (com != NULL)
3213                                         /* The component status will be updated
3214                                          * when its prep() is called later by
3215                                          * the LFSCK main engine. */
3216                                         list_move_tail(&com->lc_link,
3217                                                        &lfsck->li_list_scan);
3218                                 start->ls_active &= ~type;
3219                         }
3220                         type <<= 1;
3221                 }
3222         }
3223
3224         if (list_empty(&lfsck->li_list_scan)) {
3225                 /* The speed limit will be used to control both the LFSCK and
3226                  * low layer scrub (if applied), need to be handled firstly. */
3227                 if (start->ls_valid & LSV_SPEED_LIMIT) {
3228                         if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3229                                 rc = lfsck_bookmark_store(env, lfsck);
3230                                 if (rc != 0)
3231                                         GOTO(out, rc);
3232                         }
3233                 }
3234
3235                 goto trigger;
3236         }
3237
3238         if (start->ls_flags & LPF_RESET)
3239                 flags |= DOIF_RESET;
3240
3241         rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3242         if (rc != 0)
3243                 GOTO(out, rc);
3244
3245         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3246                 start->ls_active |= com->lc_type;
3247                 if (flags & DOIF_RESET) {
3248                         rc = com->lc_ops->lfsck_reset(env, com, false);
3249                         if (rc != 0)
3250                                 GOTO(out, rc);
3251                 }
3252         }
3253
3254         ltds = &lfsck->li_mdt_descs;
3255         down_read(&ltds->ltd_rw_sem);
3256         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3257                 ltd = lfsck_ltd2tgt(ltds, idx);
3258                 LASSERT(ltd != NULL);
3259
3260                 ltd->ltd_layout_done = 0;
3261                 ltd->ltd_namespace_done = 0;
3262                 ltd->ltd_synced_failures = 0;
3263                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3264                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3265                 list_del_init(&ltd->ltd_layout_phase_list);
3266                 list_del_init(&ltd->ltd_layout_list);
3267                 list_del_init(&ltd->ltd_namespace_phase_list);
3268                 list_del_init(&ltd->ltd_namespace_list);
3269         }
3270         up_read(&ltds->ltd_rw_sem);
3271
3272         ltds = &lfsck->li_ost_descs;
3273         down_read(&ltds->ltd_rw_sem);
3274         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3275                 ltd = lfsck_ltd2tgt(ltds, idx);
3276                 LASSERT(ltd != NULL);
3277
3278                 ltd->ltd_layout_done = 0;
3279                 ltd->ltd_synced_failures = 0;
3280                 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3281                 list_del_init(&ltd->ltd_layout_phase_list);
3282                 list_del_init(&ltd->ltd_layout_list);
3283         }
3284         up_read(&ltds->ltd_rw_sem);
3285
3286 trigger:
3287         lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3288         if (bk->lb_param & LPF_DRYRUN)
3289                 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3290
3291         if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3292                 valid |= DOIV_ERROR_HANDLE;
3293                 if (start->ls_flags & LPF_FAILOUT)
3294                         flags |= DOIF_FAILOUT;
3295         }
3296
3297         if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3298                 valid |= DOIV_DRYRUN;
3299                 if (start->ls_flags & LPF_DRYRUN)
3300                         flags |= DOIF_DRYRUN;
3301         }
3302
3303         if (!list_empty(&lfsck->li_list_scan))
3304                 flags |= DOIF_OUTUSED;
3305
3306         lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3307         lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3308         if (IS_ERR(lta))
3309                 GOTO(out, rc = PTR_ERR(lta));
3310
3311         __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3312         spin_lock(&lfsck->li_lock);
3313         thread_set_flags(thread, SVC_STARTING);
3314         spin_unlock(&lfsck->li_lock);
3315         task = kthread_run(lfsck_master_engine, lta, "lfsck");
3316         if (IS_ERR(task)) {
3317                 rc = PTR_ERR(task);
3318                 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3319                        lfsck_lfsck2name(lfsck), rc);
3320                 lfsck_thread_args_fini(lta);
3321
3322                 GOTO(out, rc);
3323         }
3324
3325         wait_event_idle(thread->t_ctl_waitq,
3326                         thread_is_running(thread) ||
3327                         thread_is_stopped(thread));
3328         if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3329                 lfsck->li_start_unplug = 1;
3330                 wake_up_all(&thread->t_ctl_waitq);
3331
3332                 GOTO(out, rc = 0);
3333         }
3334
3335         /* release lfsck::li_mutex to avoid deadlock. */
3336         mutex_unlock(&lfsck->li_mutex);
3337         rc = lfsck_start_all(env, lfsck, start);
3338         if (rc != 0) {
3339                 spin_lock(&lfsck->li_lock);
3340                 if (thread_is_stopped(thread)) {
3341                         spin_unlock(&lfsck->li_lock);
3342                 } else {
3343                         lfsck->li_status = LS_FAILED;
3344                         lfsck->li_flags = 0;
3345                         thread_set_flags(thread, SVC_STOPPING);
3346                         spin_unlock(&lfsck->li_lock);
3347
3348                         lfsck->li_start_unplug = 1;
3349                         wake_up_all(&thread->t_ctl_waitq);
3350                         wait_event_idle(thread->t_ctl_waitq,
3351                                         thread_is_stopped(thread));
3352                 }
3353         } else {
3354                 lfsck->li_start_unplug = 1;
3355                 wake_up_all(&thread->t_ctl_waitq);
3356         }
3357
3358         GOTO(put, rc);
3359
3360 out:
3361         mutex_unlock(&lfsck->li_mutex);
3362
3363 put:
3364         lfsck_instance_put(env, lfsck);
3365
3366         return rc < 0 ? rc : 0;
3367 }
3368 EXPORT_SYMBOL(lfsck_start);
3369
3370 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3371                struct lfsck_stop *stop)
3372 {
3373         struct lfsck_instance   *lfsck;
3374         struct ptlrpc_thread    *thread;
3375         int                      rc     = 0;
3376         int                      rc1    = 0;
3377         ENTRY;
3378
3379         lfsck = lfsck_instance_find(key, true, false);
3380         if (unlikely(lfsck == NULL))
3381                 RETURN(-ENXIO);
3382
3383         thread = &lfsck->li_thread;
3384         if (stop && stop->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3385                 CERROR("%s: only allow to specify '-A' via MDS\n",
3386                        lfsck_lfsck2name(lfsck));
3387                 GOTO(put, rc = -EPERM);
3388         }
3389
3390         spin_lock(&lfsck->li_lock);
3391         /* The target is umounted */
3392         if (stop && stop->ls_status == LS_PAUSED)
3393                 lfsck->li_stopping = 1;
3394
3395         if (thread_is_init(thread) || thread_is_stopped(thread))
3396                 /* no error if LFSCK stopped already, or not started */
3397                 GOTO(unlock, rc = 0);
3398
3399         if (thread_is_stopping(thread))
3400                 /* Someone is stopping LFSCK. */
3401                 GOTO(unlock, rc = -EINPROGRESS);
3402
3403         if (stop) {
3404                 lfsck->li_status = stop->ls_status;
3405                 lfsck->li_flags = stop->ls_flags;
3406         } else {
3407                 lfsck->li_status = LS_STOPPED;
3408                 lfsck->li_flags = 0;
3409         }
3410
3411         thread_set_flags(thread, SVC_STOPPING);
3412
3413         LASSERT(lfsck->li_task != NULL);
3414         cfs_force_sig(SIGINT, lfsck->li_task);
3415
3416         if (lfsck->li_master) {
3417                 struct lfsck_component *com;
3418                 struct lfsck_assistant_data *lad;
3419
3420                 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3421                         lad = com->lc_data;
3422                         spin_lock(&lad->lad_lock);
3423                         if (lad->lad_task != NULL)
3424                                 cfs_force_sig(SIGINT, lad->lad_task);
3425                         spin_unlock(&lad->lad_lock);
3426                 }
3427
3428                 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3429                         lad = com->lc_data;
3430                         spin_lock(&lad->lad_lock);
3431                         if (lad->lad_task != NULL)
3432                                 cfs_force_sig(SIGINT, lad->lad_task);
3433                         spin_unlock(&lad->lad_lock);
3434                 }
3435         }
3436
3437         wake_up_all(&thread->t_ctl_waitq);
3438         spin_unlock(&lfsck->li_lock);
3439         if (stop && stop->ls_flags & LPF_BROADCAST)
3440                 rc1 = lfsck_stop_all(env, lfsck, stop);
3441
3442         /* It was me set the status as 'stopping' just now, if it is not
3443          * 'stopping' now, then either stopped, or re-started by race. */
3444         wait_event_idle(thread->t_ctl_waitq,
3445                         !thread_is_stopping(thread));
3446
3447         GOTO(put, rc = 0);
3448
3449 unlock:
3450         spin_unlock(&lfsck->li_lock);
3451 put:
3452         lfsck_instance_put(env, lfsck);
3453
3454         return rc != 0 ? rc : rc1;
3455 }
3456 EXPORT_SYMBOL(lfsck_stop);
3457
3458 int lfsck_in_notify_local(const struct lu_env *env, struct dt_device *key,
3459                           struct lfsck_req_local *lrl, struct thandle *th)
3460 {
3461         struct lfsck_instance *lfsck;
3462         struct lfsck_component *com;
3463         int rc = -EOPNOTSUPP;
3464         ENTRY;
3465
3466         lfsck = lfsck_instance_find(key, true, false);
3467         if (unlikely(!lfsck))
3468                 RETURN(-ENXIO);
3469
3470         com = lfsck_component_find(lfsck, lrl->lrl_active);
3471         if (likely(com && com->lc_ops->lfsck_in_notify_local)) {
3472                 rc = com->lc_ops->lfsck_in_notify_local(env, com, lrl, th);
3473                 lfsck_component_put(env, com);
3474         }
3475
3476         lfsck_instance_put(env, lfsck);
3477
3478         RETURN(rc);
3479 }
3480 EXPORT_SYMBOL(lfsck_in_notify_local);
3481
3482 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3483                     struct lfsck_request *lr)
3484 {
3485         int rc = -EOPNOTSUPP;
3486         ENTRY;
3487
3488         switch (lr->lr_event) {
3489         case LE_START: {
3490                 struct lfsck_start       *start = &lfsck_env_info(env)->lti_start;
3491                 struct lfsck_start_param  lsp;
3492
3493                 memset(start, 0, sizeof(*start));
3494                 start->ls_valid = lr->lr_valid;
3495                 start->ls_speed_limit = lr->lr_speed;
3496                 start->ls_version = lr->lr_version;
3497                 start->ls_active = lr->lr_active;
3498                 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3499                 start->ls_async_windows = lr->lr_async_windows;
3500
3501                 lsp.lsp_start = start;
3502                 lsp.lsp_index = lr->lr_index;
3503                 lsp.lsp_index_valid = 1;
3504                 rc = lfsck_start(env, key, &lsp);
3505                 break;
3506         }
3507         case LE_STOP: {
3508                 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3509
3510                 memset(stop, 0, sizeof(*stop));
3511                 stop->ls_status = lr->lr_status;
3512                 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3513                 rc = lfsck_stop(env, key, stop);
3514                 break;
3515         }
3516         case LE_PHASE1_DONE:
3517         case LE_PHASE2_DONE:
3518         case LE_PEER_EXIT:
3519         case LE_CONDITIONAL_DESTROY:
3520         case LE_SET_LMV_MASTER:
3521         case LE_SET_LMV_SLAVE:
3522         case LE_PAIRS_VERIFY: {
3523                 struct lfsck_instance  *lfsck;
3524                 struct lfsck_component *com;
3525
3526                 lfsck = lfsck_instance_find(key, true, false);
3527                 if (unlikely(lfsck == NULL))
3528                         RETURN(-ENXIO);
3529
3530                 com = lfsck_component_find(lfsck, lr->lr_active);
3531                 if (likely(com)) {
3532                         rc = com->lc_ops->lfsck_in_notify(env, com, lr);
3533                         lfsck_component_put(env, com);
3534                 }
3535
3536                 lfsck_instance_put(env, lfsck);
3537                 break;
3538         }
3539         default:
3540                 break;
3541         }
3542
3543         RETURN(rc);
3544 }
3545 EXPORT_SYMBOL(lfsck_in_notify);
3546
3547 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3548                 struct lfsck_request *req, struct lfsck_reply *rep,
3549                 struct lfsck_query *que)
3550 {
3551         struct lfsck_instance  *lfsck;
3552         struct lfsck_component *com;
3553         int                     i;
3554         int                     rc = 0;
3555         __u16                   type;
3556         ENTRY;
3557
3558         lfsck = lfsck_instance_find(key, true, false);
3559         if (unlikely(lfsck == NULL))
3560                 RETURN(-ENXIO);
3561
3562         if (que != NULL) {
3563                 if (que->lu_types == LFSCK_TYPES_ALL)
3564                         que->lu_types =
3565                                 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3566
3567                 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3568                         que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3569
3570                         GOTO(out, rc = -ENOTSUPP);
3571                 }
3572
3573                 for (i = 0, type = BIT(i); i < LFSCK_TYPE_BITS;
3574                      i++, type = BIT(i)) {
3575                         if (!(que->lu_types & type))
3576                                 continue;
3577
3578 again:
3579                         com = lfsck_component_find(lfsck, type);
3580                         if (unlikely(com == NULL))
3581                                 GOTO(out, rc = -ENOTSUPP);
3582
3583                         memset(que->lu_mdts_count[i], 0,
3584                                sizeof(__u32) * (LS_MAX + 1));
3585                         memset(que->lu_osts_count[i], 0,
3586                                sizeof(__u32) * (LS_MAX + 1));
3587                         que->lu_repaired[i] = 0;
3588                         rc = com->lc_ops->lfsck_query(env, com, req, rep,
3589                                                       que, i);
3590                         lfsck_component_put(env, com);
3591                         if  (rc < 0)
3592                                 GOTO(out, rc);
3593                 }
3594
3595                 if (!(que->lu_flags & LPF_WAIT))
3596                         GOTO(out, rc);
3597
3598                 for (i = 0, type = BIT(i); i < LFSCK_TYPE_BITS;
3599                      i++, type = BIT(i)) {
3600                         if (!(que->lu_types & type))
3601                                 continue;
3602
3603                         if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3604                             que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3605                             que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3606                             que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3607                                 /* If it is required to wait, then sleep
3608                                  * 3 seconds and try to query again.
3609                                  */
3610                                 unsigned long timeout =
3611                                         msecs_to_jiffies(3000) + 1;
3612                                 while (timeout &&
3613                                        !fatal_signal_pending(current))
3614                                         timeout = schedule_timeout_killable(
3615                                                 timeout);
3616                                 if (timeout == 0)
3617                                         goto again;
3618                         }
3619                 }
3620         } else {
3621                 com = lfsck_component_find(lfsck, req->lr_active);
3622                 if (likely(com != NULL)) {
3623                         rc = com->lc_ops->lfsck_query(env, com, req, rep,
3624                                                       que, -1);
3625                         lfsck_component_put(env, com);
3626                 } else {
3627                         rc = -ENOTSUPP;
3628                 }
3629         }
3630
3631         GOTO(out, rc);
3632
3633 out:
3634         lfsck_instance_put(env, lfsck);
3635         return rc;
3636 }
3637 EXPORT_SYMBOL(lfsck_query);
3638
3639 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3640                              struct ldlm_namespace *ns)
3641 {
3642         struct lfsck_instance  *lfsck;
3643         int                     rc      = -ENXIO;
3644
3645         lfsck = lfsck_instance_find(key, true, false);
3646         if (likely(lfsck != NULL)) {
3647                 lfsck->li_namespace = ns;
3648                 lfsck_instance_put(env, lfsck);
3649                 rc = 0;
3650         }
3651
3652         return rc;
3653 }
3654 EXPORT_SYMBOL(lfsck_register_namespace);
3655
3656 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3657                    struct dt_device *next, struct obd_device *obd,
3658                    lfsck_out_notify notify, void *notify_data, bool master)
3659 {
3660         struct lfsck_instance   *lfsck;
3661         struct dt_object        *root  = NULL;
3662         struct dt_object        *obj   = NULL;
3663         struct lu_fid           *fid   = &lfsck_env_info(env)->lti_fid;
3664         int                      rc;
3665         ENTRY;
3666
3667         lfsck = lfsck_instance_find(key, false, false);
3668         if (unlikely(lfsck != NULL))
3669                 RETURN(-EEXIST);
3670
3671         OBD_ALLOC_PTR(lfsck);
3672         if (lfsck == NULL)
3673                 RETURN(-ENOMEM);
3674
3675         mutex_init(&lfsck->li_mutex);
3676         spin_lock_init(&lfsck->li_lock);
3677         INIT_LIST_HEAD(&lfsck->li_link);
3678         INIT_LIST_HEAD(&lfsck->li_list_scan);
3679         INIT_LIST_HEAD(&lfsck->li_list_dir);
3680         INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3681         INIT_LIST_HEAD(&lfsck->li_list_idle);
3682         INIT_LIST_HEAD(&lfsck->li_list_lmv);
3683         atomic_set(&lfsck->li_ref, 1);
3684         atomic_set(&lfsck->li_double_scan_count, 0);
3685         init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3686         lfsck->li_out_notify = notify;
3687         lfsck->li_out_notify_data = notify_data;
3688         lfsck->li_next = next;
3689         lfsck->li_bottom = key;
3690         lfsck->li_obd = obd;
3691
3692         rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3693         if (rc != 0)
3694                 GOTO(out, rc);
3695
3696         rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3697         if (rc != 0)
3698                 GOTO(out, rc);
3699
3700         fid->f_seq = FID_SEQ_LOCAL_NAME;
3701         fid->f_oid = 1;
3702         fid->f_ver = 0;