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