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