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