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