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