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