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