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