Whamcloud - gitweb
9f340e8617a9ee883ec2ef97abc615a757e8ffbb
[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         if (!fid_is_zero(&bk->lb_lpf_fid)) {
1439                 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1440                         struct lu_fid tfid = bk->lb_lpf_fid;
1441
1442                         /* Invalid FID record in the bookmark file, reset it. */
1443                         fid_zero(&bk->lb_lpf_fid);
1444                         rc = lfsck_bookmark_store(env, lfsck);
1445
1446                         CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1447                                " in the bookmark file: rc = %d\n",
1448                                lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1449
1450                         if (rc != 0)
1451                                 GOTO(put, rc);
1452                 } else {
1453                         child1 = lfsck_object_find_bottom(env, lfsck,
1454                                                           &bk->lb_lpf_fid);
1455                         if (IS_ERR(child1)) {
1456                                 child1 = NULL;
1457                                 goto find_child2;
1458                         }
1459
1460                         if (unlikely(!dt_object_exists(child1) ||
1461                                      dt_object_remote(child1)) ||
1462                                      !S_ISDIR(lfsck_object_type(child1))) {
1463                                 /* Invalid FID record in the bookmark file,
1464                                  * reset it. */
1465                                 fid_zero(&bk->lb_lpf_fid);
1466                                 rc = lfsck_bookmark_store(env, lfsck);
1467
1468                                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1469                                        " in the bookmark file: rc = %d\n",
1470                                        lfsck_lfsck2name(lfsck),
1471                                        PFID(lfsck_dto2fid(child1)), rc);
1472
1473                                 if (rc != 0)
1474                                         GOTO(put, rc);
1475
1476                                 lfsck_object_put(env, child1);
1477                                 child1 = NULL;
1478                         } else if (unlikely(!dt_try_as_dir(env, child1))) {
1479                                 GOTO(put, rc = -ENOTDIR);
1480                         }
1481                 }
1482         }
1483
1484 find_child2:
1485         snprintf(name, 8, "MDT%04x", node);
1486         rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1487                        (const struct dt_key *)name);
1488         if (rc == -ENOENT) {
1489                 if (!fid_is_zero(&bk->lb_lpf_fid))
1490                         goto check_child1;
1491
1492                 GOTO(put, rc = 0);
1493         }
1494
1495         if (rc != 0)
1496                 GOTO(put, rc);
1497
1498         /* Invalid FID in the name entry, remove the name entry. */
1499         if (!fid_is_norm(cfid)) {
1500                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1501                 if (rc != 0)
1502                         GOTO(put, rc);
1503
1504                 goto check_child1;
1505         }
1506
1507         child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1508         if (IS_ERR(child2))
1509                 GOTO(put, rc = PTR_ERR(child2));
1510
1511         if (unlikely(!dt_object_exists(child2) ||
1512                      dt_object_remote(child2)) ||
1513                      !S_ISDIR(lfsck_object_type(child2))) {
1514                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1515                 if (rc != 0)
1516                         GOTO(put, rc);
1517
1518                 goto check_child1;
1519         }
1520
1521         if (unlikely(!dt_try_as_dir(env, child2)))
1522                 GOTO(put, rc = -ENOTDIR);
1523
1524         if (child1 == NULL) {
1525                 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1526                                             pfid, LVLT_BY_NAMEENTRY);
1527         } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1528                 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1529                                             pfid, LVLT_BY_BOOKMARK);
1530                 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1531                         rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
1532                                                     name, pfid,
1533                                                     LVLT_BY_NAMEENTRY);
1534         } else {
1535                 if (lfsck->li_lpf_obj == NULL) {
1536                         lu_object_get(&child2->do_lu);
1537                         lfsck->li_lpf_obj = child2;
1538                 }
1539
1540                 cname = lfsck_name_get_const(env, name, strlen(name));
1541                 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1542         }
1543
1544         GOTO(put, rc);
1545
1546 check_child1:
1547         if (child1 != NULL)
1548                 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1549                                             pfid, LVLT_BY_BOOKMARK);
1550
1551         GOTO(put, rc);
1552
1553 put:
1554         if (lfsck->li_lpf_obj != NULL) {
1555                 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1556                         lfsck_object_put(env, lfsck->li_lpf_obj);
1557                         lfsck->li_lpf_obj = NULL;
1558                         rc = -ENOTDIR;
1559                 }
1560         } else if (rc == 0) {
1561                 rc = lfsck_create_lpf(env, lfsck);
1562         }
1563
1564         if (child2 != NULL && !IS_ERR(child2))
1565                 lfsck_object_put(env, child2);
1566         if (child1 != NULL && !IS_ERR(child1))
1567                 lfsck_object_put(env, child1);
1568
1569         return rc;
1570 }
1571
1572 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1573 {
1574         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
1575         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
1576         char                    *prefix;
1577         int                      rc     = 0;
1578         ENTRY;
1579
1580         if (unlikely(ss == NULL))
1581                 RETURN(-ENXIO);
1582
1583         OBD_ALLOC_PTR(lfsck->li_seq);
1584         if (lfsck->li_seq == NULL)
1585                 RETURN(-ENOMEM);
1586
1587         OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1588         if (prefix == NULL)
1589                 GOTO(out, rc = -ENOMEM);
1590
1591         snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1592         rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1593                              ss->ss_server_seq);
1594         OBD_FREE(prefix, MAX_OBD_NAME + 7);
1595         if (rc != 0)
1596                 GOTO(out, rc);
1597
1598         if (fid_is_sane(&bk->lb_last_fid))
1599                 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1600
1601         RETURN(0);
1602
1603 out:
1604         OBD_FREE_PTR(lfsck->li_seq);
1605         lfsck->li_seq = NULL;
1606
1607         return rc;
1608 }
1609
1610 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1611 {
1612         if (lfsck->li_seq != NULL) {
1613                 seq_client_fini(lfsck->li_seq);
1614                 OBD_FREE_PTR(lfsck->li_seq);
1615                 lfsck->li_seq = NULL;
1616         }
1617 }
1618
1619 void lfsck_instance_cleanup(const struct lu_env *env,
1620                             struct lfsck_instance *lfsck)
1621 {
1622         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1623         struct lfsck_component  *com;
1624         struct lfsck_component  *next;
1625         struct lfsck_lmv_unit   *llu;
1626         struct lfsck_lmv_unit   *llu_next;
1627         struct lfsck_lmv        *llmv;
1628         ENTRY;
1629
1630         LASSERT(list_empty(&lfsck->li_link));
1631         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1632
1633         if (lfsck->li_obj_oit != NULL) {
1634                 lfsck_object_put(env, lfsck->li_obj_oit);
1635                 lfsck->li_obj_oit = NULL;
1636         }
1637
1638         LASSERT(lfsck->li_obj_dir == NULL);
1639         LASSERT(lfsck->li_lmv == NULL);
1640
1641         list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1642                 llmv = &llu->llu_lmv;
1643
1644                 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1645                          "still in using: %u\n",
1646                          atomic_read(&llmv->ll_ref));
1647
1648                 lfsck_lmv_put(env, llmv);
1649         }
1650
1651         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1652                 lfsck_component_cleanup(env, com);
1653         }
1654
1655         LASSERT(list_empty(&lfsck->li_list_dir));
1656
1657         list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1658                                  lc_link) {
1659                 lfsck_component_cleanup(env, com);
1660         }
1661
1662         list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1663                 lfsck_component_cleanup(env, com);
1664         }
1665
1666         lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1667         lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1668
1669         if (lfsck->li_lfsck_dir != NULL) {
1670                 lfsck_object_put(env, lfsck->li_lfsck_dir);
1671                 lfsck->li_lfsck_dir = NULL;
1672         }
1673
1674         if (lfsck->li_bookmark_obj != NULL) {
1675                 lfsck_object_put(env, lfsck->li_bookmark_obj);
1676                 lfsck->li_bookmark_obj = NULL;
1677         }
1678
1679         if (lfsck->li_lpf_obj != NULL) {
1680                 lfsck_object_put(env, lfsck->li_lpf_obj);
1681                 lfsck->li_lpf_obj = NULL;
1682         }
1683
1684         if (lfsck->li_lpf_root_obj != NULL) {
1685                 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1686                 lfsck->li_lpf_root_obj = NULL;
1687         }
1688
1689         if (lfsck->li_los != NULL) {
1690                 local_oid_storage_fini(env, lfsck->li_los);
1691                 lfsck->li_los = NULL;
1692         }
1693
1694         lfsck_fid_fini(lfsck);
1695
1696         OBD_FREE_PTR(lfsck);
1697 }
1698
1699 static inline struct lfsck_instance *
1700 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1701 {
1702         struct lfsck_instance *lfsck;
1703
1704         list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1705                 if (lfsck->li_bottom == key) {
1706                         if (ref)
1707                                 lfsck_instance_get(lfsck);
1708                         if (unlink)
1709                                 list_del_init(&lfsck->li_link);
1710
1711                         return lfsck;
1712                 }
1713         }
1714
1715         return NULL;
1716 }
1717
1718 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1719                                            bool unlink)
1720 {
1721         struct lfsck_instance *lfsck;
1722
1723         spin_lock(&lfsck_instance_lock);
1724         lfsck = __lfsck_instance_find(key, ref, unlink);
1725         spin_unlock(&lfsck_instance_lock);
1726
1727         return lfsck;
1728 }
1729
1730 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1731 {
1732         struct lfsck_instance *tmp;
1733
1734         spin_lock(&lfsck_instance_lock);
1735         list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1736                 if (lfsck->li_bottom == tmp->li_bottom) {
1737                         spin_unlock(&lfsck_instance_lock);
1738                         return -EEXIST;
1739                 }
1740         }
1741
1742         list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1743         spin_unlock(&lfsck_instance_lock);
1744         return 0;
1745 }
1746
1747 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1748                     const char *prefix)
1749 {
1750         int flag;
1751         int i;
1752         bool newline = (bits != 0 ? false : true);
1753         int rc;
1754
1755         rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1756         if (rc < 0)
1757                 return rc;
1758
1759         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1760                 if (flag & bits) {
1761                         bits &= ~flag;
1762                         if (names[i] != NULL) {
1763                                 if (bits == 0)
1764                                         newline = true;
1765
1766                                 rc = seq_printf(m, "%s%c", names[i],
1767                                                 newline ? '\n' : ',');
1768                                 if (rc < 0)
1769                                         return rc;
1770                         }
1771                 }
1772         }
1773
1774         if (!newline)
1775                 rc = seq_printf(m, "\n");
1776
1777         return rc;
1778 }
1779
1780 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1781 {
1782         int rc;
1783
1784         if (time == 0) {
1785                 rc = seq_printf(m, "%s_time: N/A\n", name);
1786                 if (rc == 0)
1787                         rc = seq_printf(m, "time_since_%s: N/A\n", name);
1788
1789                 return rc;
1790         }
1791
1792         rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1793         if (rc == 0)
1794                 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1795                                 name, cfs_time_current_sec() - time);
1796
1797         return rc;
1798 }
1799
1800 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1801                    const char *prefix)
1802 {
1803         if (fid_is_zero(&pos->lp_dir_parent)) {
1804                 if (pos->lp_oit_cookie == 0)
1805                         return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1806
1807                 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1808                                   prefix, pos->lp_oit_cookie);
1809         }
1810
1811         return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1812                           prefix, pos->lp_oit_cookie,
1813                           PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1814 }
1815
1816 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1817                     struct lfsck_position *pos, bool init)
1818 {
1819         const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1820
1821         if (unlikely(lfsck->li_di_oit == NULL)) {
1822                 memset(pos, 0, sizeof(*pos));
1823                 return;
1824         }
1825
1826         pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1827         if (!lfsck->li_current_oit_processed && !init)
1828                 pos->lp_oit_cookie--;
1829
1830         LASSERT(pos->lp_oit_cookie > 0);
1831
1832         if (lfsck->li_di_dir != NULL) {
1833                 struct dt_object *dto = lfsck->li_obj_dir;
1834
1835                 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1836                                                         lfsck->li_di_dir);
1837
1838                 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1839                         fid_zero(&pos->lp_dir_parent);
1840                         pos->lp_dir_cookie = 0;
1841                 } else {
1842                         pos->lp_dir_parent = *lfsck_dto2fid(dto);
1843                 }
1844         } else {
1845                 fid_zero(&pos->lp_dir_parent);
1846                 pos->lp_dir_cookie = 0;
1847         }
1848 }
1849
1850 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1851 {
1852         bool dirty = false;
1853
1854         if (limit != LFSCK_SPEED_NO_LIMIT) {
1855                 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1856                         lfsck->li_sleep_rate = limit /
1857                                                msecs_to_jiffies(MSEC_PER_SEC);
1858                         lfsck->li_sleep_jif = 1;
1859                 } else {
1860                         lfsck->li_sleep_rate = 1;
1861                         lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1862                                               limit;
1863                 }
1864         } else {
1865                 lfsck->li_sleep_jif = 0;
1866                 lfsck->li_sleep_rate = 0;
1867         }
1868
1869         if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1870                 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1871                 dirty = true;
1872         }
1873
1874         return dirty;
1875 }
1876
1877 void lfsck_control_speed(struct lfsck_instance *lfsck)
1878 {
1879         struct ptlrpc_thread *thread = &lfsck->li_thread;
1880         struct l_wait_info    lwi;
1881
1882         if (lfsck->li_sleep_jif > 0 &&
1883             lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1884                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1885                                        LWI_ON_SIGNAL_NOOP, NULL);
1886
1887                 l_wait_event(thread->t_ctl_waitq,
1888                              !thread_is_running(thread),
1889                              &lwi);
1890                 lfsck->li_new_scanned = 0;
1891         }
1892 }
1893
1894 void lfsck_control_speed_by_self(struct lfsck_component *com)
1895 {
1896         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1897         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1898         struct l_wait_info       lwi;
1899
1900         if (lfsck->li_sleep_jif > 0 &&
1901             com->lc_new_scanned >= lfsck->li_sleep_rate) {
1902                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1903                                        LWI_ON_SIGNAL_NOOP, NULL);
1904
1905                 l_wait_event(thread->t_ctl_waitq,
1906                              !thread_is_running(thread),
1907                              &lwi);
1908                 com->lc_new_scanned = 0;
1909         }
1910 }
1911
1912 static struct lfsck_thread_args *
1913 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1914                        struct lfsck_component *com,
1915                        struct lfsck_start_param *lsp)
1916 {
1917         struct lfsck_thread_args *lta;
1918         int                       rc;
1919
1920         OBD_ALLOC_PTR(lta);
1921         if (lta == NULL)
1922                 return ERR_PTR(-ENOMEM);
1923
1924         rc = lu_env_init(&lta->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1925         if (rc != 0) {
1926                 OBD_FREE_PTR(lta);
1927                 return ERR_PTR(rc);
1928         }
1929
1930         lta->lta_lfsck = lfsck_instance_get(lfsck);
1931         if (com != NULL)
1932                 lta->lta_com = lfsck_component_get(com);
1933
1934         lta->lta_lsp = lsp;
1935
1936         return lta;
1937 }
1938
1939 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1940 {
1941         if (lta->lta_com != NULL)
1942                 lfsck_component_put(&lta->lta_env, lta->lta_com);
1943         lfsck_instance_put(&lta->lta_env, lta->lta_lfsck);
1944         lu_env_fini(&lta->lta_env);
1945         OBD_FREE_PTR(lta);
1946 }
1947
1948 struct lfsck_assistant_data *
1949 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1950                           const char *name)
1951 {
1952         struct lfsck_assistant_data *lad;
1953
1954         OBD_ALLOC_PTR(lad);
1955         if (lad != NULL) {
1956                 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1957                 if (lad->lad_bitmap == NULL) {
1958                         OBD_FREE_PTR(lad);
1959                         return NULL;
1960                 }
1961
1962                 INIT_LIST_HEAD(&lad->lad_req_list);
1963                 spin_lock_init(&lad->lad_lock);
1964                 INIT_LIST_HEAD(&lad->lad_ost_list);
1965                 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1966                 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1967                 INIT_LIST_HEAD(&lad->lad_mdt_list);
1968                 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1969                 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1970                 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1971                 lad->lad_ops = lao;
1972                 lad->lad_name = name;
1973         }
1974
1975         return lad;
1976 }
1977
1978 struct lfsck_assistant_object *
1979 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1980                             const struct lu_attr *attr, __u64 cookie,
1981                             bool is_dir)
1982 {
1983         struct lfsck_assistant_object   *lso;
1984
1985         OBD_ALLOC_PTR(lso);
1986         if (lso == NULL)
1987                 return ERR_PTR(-ENOMEM);
1988
1989         lso->lso_fid = *fid;
1990         if (attr != NULL)
1991                 lso->lso_attr = *attr;
1992
1993         atomic_set(&lso->lso_ref, 1);
1994         lso->lso_oit_cookie = cookie;
1995         if (is_dir)
1996                 lso->lso_is_dir = 1;
1997
1998         return lso;
1999 }
2000
2001 struct dt_object *
2002 lfsck_assistant_object_load(const struct lu_env *env,
2003                             struct lfsck_instance *lfsck,
2004                             struct lfsck_assistant_object *lso)
2005 {
2006         struct dt_object *obj;
2007
2008         obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2009         if (IS_ERR(obj))
2010                 return obj;
2011
2012         if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2013                 lso->lso_dead = 1;
2014                 lfsck_object_put(env, obj);
2015
2016                 return ERR_PTR(-ENOENT);
2017         }
2018
2019         if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2020                 lfsck_object_put(env, obj);
2021
2022                 return ERR_PTR(-ENOTDIR);
2023         }
2024
2025         return obj;
2026 }
2027
2028 /**
2029  * Generic LFSCK asynchronous communication interpretor function.
2030  * The LFSCK RPC reply for both the event notification and status
2031  * querying will be handled here.
2032  *
2033  * \param[in] env       pointer to the thread context
2034  * \param[in] req       pointer to the LFSCK request
2035  * \param[in] args      pointer to the lfsck_async_interpret_args
2036  * \param[in] rc        the result for handling the LFSCK request
2037  *
2038  * \retval              0 for success
2039  * \retval              negative error number on failure
2040  */
2041 int lfsck_async_interpret_common(const struct lu_env *env,
2042                                  struct ptlrpc_request *req,
2043                                  void *args, int rc)
2044 {
2045         struct lfsck_async_interpret_args *laia = args;
2046         struct lfsck_component            *com  = laia->laia_com;
2047         struct lfsck_assistant_data       *lad  = com->lc_data;
2048         struct lfsck_tgt_descs            *ltds = laia->laia_ltds;
2049         struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
2050         struct lfsck_request              *lr   = laia->laia_lr;
2051
2052         LASSERT(com->lc_lfsck->li_master);
2053
2054         switch (lr->lr_event) {
2055         case LE_START:
2056                 if (rc != 0) {
2057                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2058                                "start: rc = %d\n",
2059                                lfsck_lfsck2name(com->lc_lfsck),
2060                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2061                                ltd->ltd_index, lad->lad_name, rc);
2062
2063                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2064                                 struct lfsck_layout *lo = com->lc_file_ram;
2065
2066                                 if (lr->lr_flags & LEF_TO_OST)
2067                                         lfsck_lad_set_bitmap(env, com,
2068                                                              ltd->ltd_index);
2069                                 else
2070                                         lo->ll_flags |= LF_INCOMPLETE;
2071                         } else {
2072                                 struct lfsck_namespace *ns = com->lc_file_ram;
2073
2074                                 /* If some MDT does not join the namespace
2075                                  * LFSCK, then we cannot know whether there
2076                                  * is some name entry on such MDT that with
2077                                  * the referenced MDT-object on this MDT or
2078                                  * not. So the namespace LFSCK on this MDT
2079                                  * cannot handle orphan MDT-objects properly.
2080                                  * So we mark the LFSCK as LF_INCOMPLETE and
2081                                  * skip orphan MDT-objects handling. */
2082                                 ns->ln_flags |= LF_INCOMPLETE;
2083                         }
2084                         break;
2085                 }
2086
2087                 spin_lock(&ltds->ltd_lock);
2088                 if (ltd->ltd_dead) {
2089                         spin_unlock(&ltds->ltd_lock);
2090                         break;
2091                 }
2092
2093                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2094                         struct list_head *list;
2095                         struct list_head *phase_list;
2096
2097                         if (ltd->ltd_layout_done) {
2098                                 spin_unlock(&ltds->ltd_lock);
2099                                 break;
2100                         }
2101
2102                         if (lr->lr_flags & LEF_TO_OST) {
2103                                 list = &lad->lad_ost_list;
2104                                 phase_list = &lad->lad_ost_phase1_list;
2105                         } else {
2106                                 list = &lad->lad_mdt_list;
2107                                 phase_list = &lad->lad_mdt_phase1_list;
2108                         }
2109
2110                         if (list_empty(&ltd->ltd_layout_list))
2111                                 list_add_tail(&ltd->ltd_layout_list, list);
2112                         if (list_empty(&ltd->ltd_layout_phase_list))
2113                                 list_add_tail(&ltd->ltd_layout_phase_list,
2114                                               phase_list);
2115                 } else {
2116                         if (ltd->ltd_namespace_done) {
2117                                 spin_unlock(&ltds->ltd_lock);
2118                                 break;
2119                         }
2120
2121                         if (list_empty(&ltd->ltd_namespace_list))
2122                                 list_add_tail(&ltd->ltd_namespace_list,
2123                                               &lad->lad_mdt_list);
2124                         if (list_empty(&ltd->ltd_namespace_phase_list))
2125                                 list_add_tail(&ltd->ltd_namespace_phase_list,
2126                                               &lad->lad_mdt_phase1_list);
2127                 }
2128                 spin_unlock(&ltds->ltd_lock);
2129                 break;
2130         case LE_STOP:
2131         case LE_PHASE1_DONE:
2132         case LE_PHASE2_DONE:
2133         case LE_PEER_EXIT:
2134                 if (rc != 0 && rc != -EALREADY)
2135                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2136                               "event = %d, rc = %d\n",
2137                               lfsck_lfsck2name(com->lc_lfsck),
2138                               (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2139                               ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2140                 break;
2141         case LE_QUERY: {
2142                 struct lfsck_reply *reply;
2143                 struct list_head *list;
2144                 struct list_head *phase_list;
2145
2146                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2147                         list = &ltd->ltd_layout_list;
2148                         phase_list = &ltd->ltd_layout_phase_list;
2149                 } else {
2150                         list = &ltd->ltd_namespace_list;
2151                         phase_list = &ltd->ltd_namespace_phase_list;
2152                 }
2153
2154                 if (rc != 0) {
2155                         spin_lock(&ltds->ltd_lock);
2156                         list_del_init(phase_list);
2157                         list_del_init(list);
2158                         spin_unlock(&ltds->ltd_lock);
2159                         break;
2160                 }
2161
2162                 reply = req_capsule_server_get(&req->rq_pill,
2163                                                &RMF_LFSCK_REPLY);
2164                 if (reply == NULL) {
2165                         rc = -EPROTO;
2166                         CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2167                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2168                                lad->lad_name, rc);
2169                         spin_lock(&ltds->ltd_lock);
2170                         list_del_init(phase_list);
2171                         list_del_init(list);
2172                         spin_unlock(&ltds->ltd_lock);
2173                         break;
2174                 }
2175
2176                 switch (reply->lr_status) {
2177                 case LS_SCANNING_PHASE1:
2178                         break;
2179                 case LS_SCANNING_PHASE2:
2180                         spin_lock(&ltds->ltd_lock);
2181                         list_del_init(phase_list);
2182                         if (ltd->ltd_dead) {
2183                                 spin_unlock(&ltds->ltd_lock);
2184                                 break;
2185                         }
2186
2187                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2188                                 if (ltd->ltd_layout_done) {
2189                                         spin_unlock(&ltds->ltd_lock);
2190                                         break;
2191                                 }
2192
2193                                 if (lr->lr_flags & LEF_TO_OST)
2194                                         list_add_tail(phase_list,
2195                                                 &lad->lad_ost_phase2_list);
2196                                 else
2197                                         list_add_tail(phase_list,
2198                                                 &lad->lad_mdt_phase2_list);
2199                         } else {
2200                                 if (ltd->ltd_namespace_done) {
2201                                         spin_unlock(&ltds->ltd_lock);
2202                                         break;
2203                                 }
2204
2205                                 list_add_tail(phase_list,
2206                                               &lad->lad_mdt_phase2_list);
2207                         }
2208                         spin_unlock(&ltds->ltd_lock);
2209                         break;
2210                 default:
2211                         spin_lock(&ltds->ltd_lock);
2212                         list_del_init(phase_list);
2213                         list_del_init(list);
2214                         spin_unlock(&ltds->ltd_lock);
2215                         break;
2216                 }
2217                 break;
2218         }
2219         default:
2220                 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2221                        lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2222                 break;
2223         }
2224
2225         if (!laia->laia_shared) {
2226                 lfsck_tgt_put(ltd);
2227                 lfsck_component_put(env, com);
2228         }
2229
2230         return 0;
2231 }
2232
2233 static void lfsck_interpret(const struct lu_env *env,
2234                             struct lfsck_instance *lfsck,
2235                             struct ptlrpc_request *req, void *args, int result)
2236 {
2237         struct lfsck_async_interpret_args *laia = args;
2238         struct lfsck_component            *com;
2239
2240         LASSERT(laia->laia_com == NULL);
2241         LASSERT(laia->laia_shared);
2242
2243         spin_lock(&lfsck->li_lock);
2244         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2245                 laia->laia_com = com;
2246                 lfsck_async_interpret_common(env, req, laia, result);
2247         }
2248
2249         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2250                 laia->laia_com = com;
2251                 lfsck_async_interpret_common(env, req, laia, result);
2252         }
2253         spin_unlock(&lfsck->li_lock);
2254 }
2255
2256 static int lfsck_stop_notify(const struct lu_env *env,
2257                              struct lfsck_instance *lfsck,
2258                              struct lfsck_tgt_descs *ltds,
2259                              struct lfsck_tgt_desc *ltd, __u16 type)
2260 {
2261         struct lfsck_component *com;
2262         int                     rc = 0;
2263         ENTRY;
2264
2265         LASSERT(lfsck->li_master);
2266
2267         spin_lock(&lfsck->li_lock);
2268         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2269         if (com == NULL)
2270                 com = __lfsck_component_find(lfsck, type,
2271                                              &lfsck->li_list_double_scan);
2272         if (com != NULL)
2273                 lfsck_component_get(com);
2274         spin_unlock(&lfsck->li_lock);
2275
2276         if (com != NULL) {
2277                 struct lfsck_thread_info          *info  = lfsck_env_info(env);
2278                 struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2279                 struct lfsck_request              *lr    = &info->lti_lr;
2280                 struct lfsck_assistant_data       *lad   = com->lc_data;
2281                 struct list_head                  *list;
2282                 struct list_head                  *phase_list;
2283                 struct ptlrpc_request_set         *set;
2284
2285                 set = ptlrpc_prep_set();
2286                 if (set == NULL) {
2287                         lfsck_component_put(env, com);
2288
2289                         RETURN(-ENOMEM);
2290                 }
2291
2292                 if (type == LFSCK_TYPE_LAYOUT) {
2293                         list = &ltd->ltd_layout_list;
2294                         phase_list = &ltd->ltd_layout_phase_list;
2295                 } else {
2296                         list = &ltd->ltd_namespace_list;
2297                         phase_list = &ltd->ltd_namespace_phase_list;
2298                 }
2299
2300                 spin_lock(&ltds->ltd_lock);
2301                 if (list_empty(list)) {
2302                         LASSERT(list_empty(phase_list));
2303                         spin_unlock(&ltds->ltd_lock);
2304                         ptlrpc_set_destroy(set);
2305
2306                         RETURN(0);
2307                 }
2308
2309                 list_del_init(phase_list);
2310                 list_del_init(list);
2311                 spin_unlock(&ltds->ltd_lock);
2312
2313                 memset(lr, 0, sizeof(*lr));
2314                 lr->lr_index = lfsck_dev_idx(lfsck);
2315                 lr->lr_event = LE_PEER_EXIT;
2316                 lr->lr_active = type;
2317                 lr->lr_status = LS_CO_PAUSED;
2318                 if (ltds == &lfsck->li_ost_descs)
2319                         lr->lr_flags = LEF_TO_OST;
2320
2321                 laia->laia_com = com;
2322                 laia->laia_ltds = ltds;
2323                 atomic_inc(&ltd->ltd_ref);
2324                 laia->laia_ltd = ltd;
2325                 laia->laia_lr = lr;
2326                 laia->laia_shared = 0;
2327
2328                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2329                                          lfsck_async_interpret_common,
2330                                          laia, LFSCK_NOTIFY);
2331                 if (rc != 0) {
2332                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2333                                "co-stop for %s: rc = %d\n",
2334                                lfsck_lfsck2name(lfsck),
2335                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2336                                ltd->ltd_index, lad->lad_name, rc);
2337                         lfsck_tgt_put(ltd);
2338                 } else {
2339                         rc = ptlrpc_set_wait(set);
2340                 }
2341
2342                 ptlrpc_set_destroy(set);
2343                 lfsck_component_put(env, com);
2344         }
2345
2346         RETURN(rc);
2347 }
2348
2349 static int lfsck_async_interpret(const struct lu_env *env,
2350                                  struct ptlrpc_request *req,
2351                                  void *args, int rc)
2352 {
2353         struct lfsck_async_interpret_args *laia = args;
2354         struct lfsck_instance             *lfsck;
2355
2356         lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2357                               li_mdt_descs);
2358         lfsck_interpret(env, lfsck, req, laia, rc);
2359         lfsck_tgt_put(laia->laia_ltd);
2360         if (rc != 0 && laia->laia_result != -EALREADY)
2361                 laia->laia_result = rc;
2362
2363         return 0;
2364 }
2365
2366 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2367                         struct lfsck_request *lr,
2368                         struct ptlrpc_request_set *set,
2369                         ptlrpc_interpterer_t interpreter,
2370                         void *args, int request)
2371 {
2372         struct lfsck_async_interpret_args *laia;
2373         struct ptlrpc_request             *req;
2374         struct lfsck_request              *tmp;
2375         struct req_format                 *format;
2376         int                                rc;
2377
2378         switch (request) {
2379         case LFSCK_NOTIFY:
2380                 format = &RQF_LFSCK_NOTIFY;
2381                 break;
2382         case LFSCK_QUERY:
2383                 format = &RQF_LFSCK_QUERY;
2384                 break;
2385         default:
2386                 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2387                        exp->exp_obd->obd_name, request, -EINVAL);
2388                 return -EINVAL;
2389         }
2390
2391         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2392         if (req == NULL)
2393                 return -ENOMEM;
2394
2395         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2396         if (rc != 0) {
2397                 ptlrpc_request_free(req);
2398
2399                 return rc;
2400         }
2401
2402         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2403         *tmp = *lr;
2404         ptlrpc_request_set_replen(req);
2405
2406         laia = ptlrpc_req_async_args(req);
2407         *laia = *(struct lfsck_async_interpret_args *)args;
2408         if (laia->laia_com != NULL)
2409                 lfsck_component_get(laia->laia_com);
2410         req->rq_interpret_reply = interpreter;
2411         ptlrpc_set_add_req(set, req);
2412
2413         return 0;
2414 }
2415
2416 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2417                           struct lfsck_start_param *lsp)
2418 {
2419         struct lfsck_instance           *lfsck   = com->lc_lfsck;
2420         struct lfsck_assistant_data     *lad     = com->lc_data;
2421         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
2422         struct ptlrpc_thread            *athread = &lad->lad_thread;
2423         struct lfsck_thread_args        *lta;
2424         struct task_struct              *task;
2425         int                              rc;
2426         ENTRY;
2427
2428         lad->lad_assistant_status = 0;
2429         lad->lad_post_result = 0;
2430         lad->lad_to_post = 0;
2431         lad->lad_to_double_scan = 0;
2432         lad->lad_in_double_scan = 0;
2433         lad->lad_exit = 0;
2434         lad->lad_advance_lock = false;
2435         thread_set_flags(athread, 0);
2436
2437         lta = lfsck_thread_args_init(lfsck, com, lsp);
2438         if (IS_ERR(lta))
2439                 RETURN(PTR_ERR(lta));
2440
2441         task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2442         if (IS_ERR(task)) {
2443                 rc = PTR_ERR(task);
2444                 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2445                        "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2446                 lfsck_thread_args_fini(lta);
2447         } else {
2448                 struct l_wait_info lwi = { 0 };
2449
2450                 l_wait_event(mthread->t_ctl_waitq,
2451                              thread_is_running(athread) ||
2452                              thread_is_stopped(athread),
2453                              &lwi);
2454                 if (unlikely(!thread_is_running(athread)))
2455                         rc = lad->lad_assistant_status;
2456                 else
2457                         rc = 0;
2458         }
2459
2460         RETURN(rc);
2461 }
2462
2463 int lfsck_checkpoint_generic(const struct lu_env *env,
2464                              struct lfsck_component *com)
2465 {
2466         struct lfsck_assistant_data     *lad     = com->lc_data;
2467         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2468         struct ptlrpc_thread            *athread = &lad->lad_thread;
2469         struct l_wait_info               lwi     = { 0 };
2470
2471         l_wait_event(mthread->t_ctl_waitq,
2472                      list_empty(&lad->lad_req_list) ||
2473                      !thread_is_running(mthread) ||
2474                      thread_is_stopped(athread),
2475                      &lwi);
2476
2477         if (!thread_is_running(mthread) || thread_is_stopped(athread))
2478                 return LFSCK_CHECKPOINT_SKIP;
2479
2480         return 0;
2481 }
2482
2483 void lfsck_post_generic(const struct lu_env *env,
2484                         struct lfsck_component *com, int *result)
2485 {
2486         struct lfsck_assistant_data     *lad     = com->lc_data;
2487         struct ptlrpc_thread            *athread = &lad->lad_thread;
2488         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2489         struct l_wait_info               lwi     = { 0 };
2490
2491         lad->lad_post_result = *result;
2492         if (*result <= 0)
2493                 lad->lad_exit = 1;
2494         lad->lad_to_post = 1;
2495
2496         wake_up_all(&athread->t_ctl_waitq);
2497         l_wait_event(mthread->t_ctl_waitq,
2498                      (*result > 0 && list_empty(&lad->lad_req_list)) ||
2499                      thread_is_stopped(athread),
2500                      &lwi);
2501
2502         if (lad->lad_assistant_status < 0)
2503                 *result = lad->lad_assistant_status;
2504 }
2505
2506 int lfsck_double_scan_generic(const struct lu_env *env,
2507                               struct lfsck_component *com, int status)
2508 {
2509         struct lfsck_assistant_data     *lad     = com->lc_data;
2510         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2511         struct ptlrpc_thread            *athread = &lad->lad_thread;
2512         struct l_wait_info               lwi     = { 0 };
2513
2514         if (status != LS_SCANNING_PHASE2)
2515                 lad->lad_exit = 1;
2516         else
2517                 lad->lad_to_double_scan = 1;
2518
2519         wake_up_all(&athread->t_ctl_waitq);
2520         l_wait_event(mthread->t_ctl_waitq,
2521                      lad->lad_in_double_scan ||
2522                      thread_is_stopped(athread),
2523                      &lwi);
2524
2525         if (lad->lad_assistant_status < 0)
2526                 return lad->lad_assistant_status;
2527
2528         return 0;
2529 }
2530
2531 void lfsck_quit_generic(const struct lu_env *env,
2532                         struct lfsck_component *com)
2533 {
2534         struct lfsck_assistant_data     *lad     = com->lc_data;
2535         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2536         struct ptlrpc_thread            *athread = &lad->lad_thread;
2537         struct l_wait_info               lwi     = { 0 };
2538
2539         lad->lad_exit = 1;
2540         wake_up_all(&athread->t_ctl_waitq);
2541         l_wait_event(mthread->t_ctl_waitq,
2542                      thread_is_init(athread) ||
2543                      thread_is_stopped(athread),
2544                      &lwi);
2545 }
2546
2547 /* external interfaces */
2548
2549 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2550 {
2551         struct lu_env           env;
2552         struct lfsck_instance  *lfsck;
2553         int                     rc;
2554         ENTRY;
2555
2556         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2557         if (rc != 0)
2558                 RETURN(rc);
2559
2560         lfsck = lfsck_instance_find(key, true, false);
2561         if (likely(lfsck != NULL)) {
2562                 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2563                 lfsck_instance_put(&env, lfsck);
2564         } else {
2565                 rc = -ENXIO;
2566         }
2567
2568         lu_env_fini(&env);
2569
2570         RETURN(rc);
2571 }
2572 EXPORT_SYMBOL(lfsck_get_speed);
2573
2574 int lfsck_set_speed(struct dt_device *key, int val)
2575 {
2576         struct lu_env           env;
2577         struct lfsck_instance  *lfsck;
2578         int                     rc;
2579         ENTRY;
2580
2581         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2582         if (rc != 0)
2583                 RETURN(rc);
2584
2585         lfsck = lfsck_instance_find(key, true, false);
2586         if (likely(lfsck != NULL)) {
2587                 mutex_lock(&lfsck->li_mutex);
2588                 if (__lfsck_set_speed(lfsck, val))
2589                         rc = lfsck_bookmark_store(&env, lfsck);
2590                 mutex_unlock(&lfsck->li_mutex);
2591                 lfsck_instance_put(&env, lfsck);
2592         } else {
2593                 rc = -ENXIO;
2594         }
2595
2596         lu_env_fini(&env);
2597
2598         RETURN(rc);
2599 }
2600 EXPORT_SYMBOL(lfsck_set_speed);
2601
2602 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2603 {
2604         struct lu_env           env;
2605         struct lfsck_instance  *lfsck;
2606         int                     rc;
2607         ENTRY;
2608
2609         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2610         if (rc != 0)
2611                 RETURN(rc);
2612
2613         lfsck = lfsck_instance_find(key, true, false);
2614         if (likely(lfsck != NULL)) {
2615                 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2616                 lfsck_instance_put(&env, lfsck);
2617         } else {
2618                 rc = -ENXIO;
2619         }
2620
2621         lu_env_fini(&env);
2622
2623         RETURN(rc);
2624 }
2625 EXPORT_SYMBOL(lfsck_get_windows);
2626
2627 int lfsck_set_windows(struct dt_device *key, int val)
2628 {
2629         struct lu_env           env;
2630         struct lfsck_instance  *lfsck;
2631         int                     rc;
2632         ENTRY;
2633
2634         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2635         if (rc != 0)
2636                 RETURN(rc);
2637
2638         lfsck = lfsck_instance_find(key, true, false);
2639         if (likely(lfsck != NULL)) {
2640                 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2641                         CWARN("%s: invalid async windows size that may "
2642                               "cause memory issues. The valid range is "
2643                               "[1 - %u].\n",
2644                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2645                         rc = -EINVAL;
2646                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2647                         mutex_lock(&lfsck->li_mutex);
2648                         lfsck->li_bookmark_ram.lb_async_windows = val;
2649                         rc = lfsck_bookmark_store(&env, lfsck);
2650                         mutex_unlock(&lfsck->li_mutex);
2651                 }
2652                 lfsck_instance_put(&env, lfsck);
2653         } else {
2654                 rc = -ENXIO;
2655         }
2656
2657         lu_env_fini(&env);
2658
2659         RETURN(rc);
2660 }
2661 EXPORT_SYMBOL(lfsck_set_windows);
2662
2663 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2664 {
2665         struct lu_env           env;
2666         struct lfsck_instance  *lfsck;
2667         struct lfsck_component *com;
2668         int                     rc;
2669         ENTRY;
2670
2671         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2672         if (rc != 0)
2673                 RETURN(rc);
2674
2675         lfsck = lfsck_instance_find(key, true, false);
2676         if (likely(lfsck != NULL)) {
2677                 com = lfsck_component_find(lfsck, type);
2678                 if (likely(com != NULL)) {
2679                         rc = com->lc_ops->lfsck_dump(&env, com, m);
2680                         lfsck_component_put(&env, com);
2681                 } else {
2682                         rc = -ENOTSUPP;
2683                 }
2684
2685                 lfsck_instance_put(&env, lfsck);
2686         } else {
2687                 rc = -ENXIO;
2688         }
2689
2690         lu_env_fini(&env);
2691
2692         RETURN(rc);
2693 }
2694 EXPORT_SYMBOL(lfsck_dump);
2695
2696 static int lfsck_stop_all(const struct lu_env *env,
2697                           struct lfsck_instance *lfsck,
2698                           struct lfsck_stop *stop)
2699 {
2700         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2701         struct lfsck_request              *lr     = &info->lti_lr;
2702         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2703         struct ptlrpc_request_set         *set;
2704         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2705         struct lfsck_tgt_desc             *ltd;
2706         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2707         __u32                              idx;
2708         int                                rc     = 0;
2709         int                                rc1    = 0;
2710         ENTRY;
2711
2712         LASSERT(stop->ls_flags & LPF_BROADCAST);
2713
2714         set = ptlrpc_prep_set();
2715         if (unlikely(set == NULL))
2716                 RETURN(-ENOMEM);
2717
2718         memset(lr, 0, sizeof(*lr));
2719         lr->lr_event = LE_STOP;
2720         lr->lr_index = lfsck_dev_idx(lfsck);
2721         lr->lr_status = stop->ls_status;
2722         lr->lr_version = bk->lb_version;
2723         lr->lr_active = LFSCK_TYPES_ALL;
2724         lr->lr_param = stop->ls_flags;
2725
2726         laia->laia_com = NULL;
2727         laia->laia_ltds = ltds;
2728         laia->laia_lr = lr;
2729         laia->laia_result = 0;
2730         laia->laia_shared = 1;
2731
2732         down_read(&ltds->ltd_rw_sem);
2733         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2734                 ltd = lfsck_tgt_get(ltds, idx);
2735                 LASSERT(ltd != NULL);
2736
2737                 laia->laia_ltd = ltd;
2738                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2739                                          lfsck_async_interpret, laia,
2740                                          LFSCK_NOTIFY);
2741                 if (rc != 0) {
2742                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2743                         lfsck_tgt_put(ltd);
2744                         CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2745                                "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2746                         rc1 = rc;
2747                 }
2748         }
2749         up_read(&ltds->ltd_rw_sem);
2750
2751         rc = ptlrpc_set_wait(set);
2752         ptlrpc_set_destroy(set);
2753
2754         if (rc == 0)
2755                 rc = laia->laia_result;
2756
2757         if (rc == -EALREADY)
2758                 rc = 0;
2759
2760         if (rc != 0)
2761                 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2762                        lfsck_lfsck2name(lfsck), rc);
2763
2764         RETURN(rc != 0 ? rc : rc1);
2765 }
2766
2767 static int lfsck_start_all(const struct lu_env *env,
2768                            struct lfsck_instance *lfsck,
2769                            struct lfsck_start *start)
2770 {
2771         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2772         struct lfsck_request              *lr     = &info->lti_lr;
2773         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2774         struct ptlrpc_request_set         *set;
2775         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2776         struct lfsck_tgt_desc             *ltd;
2777         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2778         __u32                              idx;
2779         int                                rc     = 0;
2780         ENTRY;
2781
2782         LASSERT(start->ls_flags & LPF_BROADCAST);
2783
2784         set = ptlrpc_prep_set();
2785         if (unlikely(set == NULL))
2786                 RETURN(-ENOMEM);
2787
2788         memset(lr, 0, sizeof(*lr));
2789         lr->lr_event = LE_START;
2790         lr->lr_index = lfsck_dev_idx(lfsck);
2791         lr->lr_speed = bk->lb_speed_limit;
2792         lr->lr_version = bk->lb_version;
2793         lr->lr_active = start->ls_active;
2794         lr->lr_param = start->ls_flags;
2795         lr->lr_async_windows = bk->lb_async_windows;
2796         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2797                        LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2798                        LSV_CREATE_MDTOBJ;
2799
2800         laia->laia_com = NULL;
2801         laia->laia_ltds = ltds;
2802         laia->laia_lr = lr;
2803         laia->laia_result = 0;
2804         laia->laia_shared = 1;
2805
2806         down_read(&ltds->ltd_rw_sem);
2807         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2808                 ltd = lfsck_tgt_get(ltds, idx);
2809                 LASSERT(ltd != NULL);
2810
2811                 laia->laia_ltd = ltd;
2812                 ltd->ltd_layout_done = 0;
2813                 ltd->ltd_namespace_done = 0;
2814                 ltd->ltd_synced_failures = 0;
2815                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2816                                          lfsck_async_interpret, laia,
2817                                          LFSCK_NOTIFY);
2818                 if (rc != 0) {
2819                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2820                         lfsck_tgt_put(ltd);
2821                         CERROR("%s: cannot notify MDT %x for LFSCK "
2822                                "start, failout: rc = %d\n",
2823                                lfsck_lfsck2name(lfsck), idx, rc);
2824                         break;
2825                 }
2826         }
2827         up_read(&ltds->ltd_rw_sem);
2828
2829         if (rc != 0) {
2830                 ptlrpc_set_destroy(set);
2831
2832                 RETURN(rc);
2833         }
2834
2835         rc = ptlrpc_set_wait(set);
2836         ptlrpc_set_destroy(set);
2837
2838         if (rc == 0)
2839                 rc = laia->laia_result;
2840
2841         if (rc != 0) {
2842                 struct lfsck_stop *stop = &info->lti_stop;
2843
2844                 CERROR("%s: cannot start LFSCK on some MDTs, "
2845                        "stop all: rc = %d\n",
2846                        lfsck_lfsck2name(lfsck), rc);
2847                 if (rc != -EALREADY) {
2848                         stop->ls_status = LS_FAILED;
2849                         stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2850                         lfsck_stop_all(env, lfsck, stop);
2851                 }
2852         }
2853
2854         RETURN(rc);
2855 }
2856
2857 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2858                 struct lfsck_start_param *lsp)
2859 {
2860         struct lfsck_start              *start  = lsp->lsp_start;
2861         struct lfsck_instance           *lfsck;
2862         struct lfsck_bookmark           *bk;
2863         struct ptlrpc_thread            *thread;
2864         struct lfsck_component          *com;
2865         struct l_wait_info               lwi    = { 0 };
2866         struct lfsck_thread_args        *lta;
2867         struct task_struct              *task;
2868         int                              rc     = 0;
2869         __u16                            valid  = 0;
2870         __u16                            flags  = 0;
2871         __u16                            type   = 1;
2872         ENTRY;
2873
2874         lfsck = lfsck_instance_find(key, true, false);
2875         if (unlikely(lfsck == NULL))
2876                 RETURN(-ENXIO);
2877
2878         /* System is not ready, try again later. */
2879         if (unlikely(lfsck->li_namespace == NULL))
2880                 GOTO(put, rc = -EAGAIN);
2881
2882         /* start == NULL means auto trigger paused LFSCK. */
2883         if ((start == NULL) &&
2884             (list_empty(&lfsck->li_list_scan) ||
2885              OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2886                 GOTO(put, rc = 0);
2887
2888         bk = &lfsck->li_bookmark_ram;
2889         thread = &lfsck->li_thread;
2890         mutex_lock(&lfsck->li_mutex);
2891         spin_lock(&lfsck->li_lock);
2892         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2893                 rc = -EALREADY;
2894                 if (unlikely(start == NULL)) {
2895                         spin_unlock(&lfsck->li_lock);
2896                         GOTO(out, rc);
2897                 }
2898
2899                 while (start->ls_active != 0) {
2900                         if (!(type & start->ls_active)) {
2901                                 type <<= 1;
2902                                 continue;
2903                         }
2904
2905                         com = __lfsck_component_find(lfsck, type,
2906                                                      &lfsck->li_list_scan);
2907                         if (com == NULL)
2908                                 com = __lfsck_component_find(lfsck, type,
2909                                                 &lfsck->li_list_double_scan);
2910                         if (com == NULL) {
2911                                 rc = -EOPNOTSUPP;
2912                                 break;
2913                         }
2914
2915                         if (com->lc_ops->lfsck_join != NULL) {
2916                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
2917                                 if (rc != 0 && rc != -EALREADY)
2918                                         break;
2919                         }
2920                         start->ls_active &= ~type;
2921                         type <<= 1;
2922                 }
2923                 spin_unlock(&lfsck->li_lock);
2924                 GOTO(out, rc);
2925         }
2926         spin_unlock(&lfsck->li_lock);
2927
2928         lfsck->li_status = 0;
2929         lfsck->li_oit_over = 0;
2930         lfsck->li_start_unplug = 0;
2931         lfsck->li_drop_dryrun = 0;
2932         lfsck->li_new_scanned = 0;
2933
2934         /* For auto trigger. */
2935         if (start == NULL)
2936                 goto trigger;
2937
2938         if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2939                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2940                        lfsck_lfsck2name(lfsck));
2941
2942                 GOTO(out, rc = -EPERM);
2943         }
2944
2945         start->ls_version = bk->lb_version;
2946
2947         if (start->ls_active != 0) {
2948                 struct lfsck_component *next;
2949
2950                 if (start->ls_active == LFSCK_TYPES_ALL)
2951                         start->ls_active = LFSCK_TYPES_SUPPORTED;
2952
2953                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2954                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2955                         GOTO(out, rc = -ENOTSUPP);
2956                 }
2957
2958                 list_for_each_entry_safe(com, next,
2959                                          &lfsck->li_list_scan, lc_link) {
2960                         if (!(com->lc_type & start->ls_active)) {
2961                                 rc = com->lc_ops->lfsck_post(env, com, 0,
2962                                                              false);
2963                                 if (rc != 0)
2964                                         GOTO(out, rc);
2965                         }
2966                 }
2967
2968                 while (start->ls_active != 0) {
2969                         if (type & start->ls_active) {
2970                                 com = __lfsck_component_find(lfsck, type,
2971                                                         &lfsck->li_list_idle);
2972                                 if (com != NULL)
2973                                         /* The component status will be updated
2974                                          * when its prep() is called later by
2975                                          * the LFSCK main engine. */
2976                                         list_move_tail(&com->lc_link,
2977                                                        &lfsck->li_list_scan);
2978                                 start->ls_active &= ~type;
2979                         }
2980                         type <<= 1;
2981                 }
2982         }
2983
2984         if (list_empty(&lfsck->li_list_scan)) {
2985                 /* The speed limit will be used to control both the LFSCK and
2986                  * low layer scrub (if applied), need to be handled firstly. */
2987                 if (start->ls_valid & LSV_SPEED_LIMIT) {
2988                         if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2989                                 rc = lfsck_bookmark_store(env, lfsck);
2990                                 if (rc != 0)
2991                                         GOTO(out, rc);
2992                         }
2993                 }
2994
2995                 goto trigger;
2996         }
2997
2998         if (start->ls_flags & LPF_RESET)
2999                 flags |= DOIF_RESET;
3000
3001         rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3002         if (rc != 0)
3003                 GOTO(out, rc);
3004
3005         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3006                 start->ls_active |= com->lc_type;
3007                 if (flags & DOIF_RESET) {
3008                         rc = com->lc_ops->lfsck_reset(env, com, false);
3009                         if (rc != 0)
3010                                 GOTO(out, rc);
3011                 }
3012         }
3013
3014 trigger:
3015         lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3016         if (bk->lb_param & LPF_DRYRUN)
3017                 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3018
3019         if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3020                 valid |= DOIV_ERROR_HANDLE;
3021                 if (start->ls_flags & LPF_FAILOUT)
3022                         flags |= DOIF_FAILOUT;
3023         }
3024
3025         if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3026                 valid |= DOIV_DRYRUN;
3027                 if (start->ls_flags & LPF_DRYRUN)
3028                         flags |= DOIF_DRYRUN;
3029         }
3030
3031         if (!list_empty(&lfsck->li_list_scan))
3032                 flags |= DOIF_OUTUSED;
3033
3034         lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3035         thread_set_flags(thread, 0);
3036         lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3037         if (IS_ERR(lta))
3038                 GOTO(out, rc = PTR_ERR(lta));
3039
3040         __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3041         task = kthread_run(lfsck_master_engine, lta, "lfsck");
3042         if (IS_ERR(task)) {
3043                 rc = PTR_ERR(task);
3044                 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3045                        lfsck_lfsck2name(lfsck), rc);
3046                 lfsck_thread_args_fini(lta);
3047
3048                 GOTO(out, rc);
3049         }
3050
3051         l_wait_event(thread->t_ctl_waitq,
3052                      thread_is_running(thread) ||
3053                      thread_is_stopped(thread),
3054                      &lwi);
3055         if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3056                 lfsck->li_start_unplug = 1;
3057                 wake_up_all(&thread->t_ctl_waitq);
3058
3059                 GOTO(out, rc = 0);
3060         }
3061
3062         /* release lfsck::li_mutex to avoid deadlock. */
3063         mutex_unlock(&lfsck->li_mutex);
3064         rc = lfsck_start_all(env, lfsck, start);
3065         if (rc != 0) {
3066                 spin_lock(&lfsck->li_lock);
3067                 if (thread_is_stopped(thread)) {
3068                         spin_unlock(&lfsck->li_lock);
3069                 } else {
3070                         lfsck->li_status = LS_FAILED;
3071                         lfsck->li_flags = 0;
3072                         thread_set_flags(thread, SVC_STOPPING);
3073                         spin_unlock(&lfsck->li_lock);
3074
3075                         lfsck->li_start_unplug = 1;
3076                         wake_up_all(&thread->t_ctl_waitq);
3077                         l_wait_event(thread->t_ctl_waitq,
3078                                      thread_is_stopped(thread),
3079                                      &lwi);
3080                 }
3081         } else {
3082                 lfsck->li_start_unplug = 1;
3083                 wake_up_all(&thread->t_ctl_waitq);
3084         }
3085
3086         GOTO(put, rc);
3087
3088 out:
3089         mutex_unlock(&lfsck->li_mutex);
3090
3091 put:
3092         lfsck_instance_put(env, lfsck);
3093
3094         return rc < 0 ? rc : 0;
3095 }
3096 EXPORT_SYMBOL(lfsck_start);
3097
3098 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3099                struct lfsck_stop *stop)
3100 {
3101         struct lfsck_instance   *lfsck;
3102         struct ptlrpc_thread    *thread;
3103         struct l_wait_info       lwi    = { 0 };
3104         int                      rc     = 0;
3105         int                      rc1    = 0;
3106         ENTRY;
3107
3108         lfsck = lfsck_instance_find(key, true, false);
3109         if (unlikely(lfsck == NULL))
3110                 RETURN(-ENXIO);
3111
3112         thread = &lfsck->li_thread;
3113         /* release lfsck::li_mutex to avoid deadlock. */
3114         if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3115                 if (!lfsck->li_master) {
3116                         CERROR("%s: only allow to specify '-A' via MDS\n",
3117                                lfsck_lfsck2name(lfsck));
3118
3119                         GOTO(out, rc = -EPERM);
3120                 }
3121
3122                 rc1 = lfsck_stop_all(env, lfsck, stop);
3123         }
3124
3125         mutex_lock(&lfsck->li_mutex);
3126         spin_lock(&lfsck->li_lock);
3127         /* no error if LFSCK is already stopped, or was never started */
3128         if (thread_is_init(thread) || thread_is_stopped(thread)) {
3129                 spin_unlock(&lfsck->li_lock);
3130                 GOTO(out, rc = 0);
3131         }
3132
3133         if (stop != NULL) {
3134                 lfsck->li_status = stop->ls_status;
3135                 lfsck->li_flags = stop->ls_flags;
3136         } else {
3137                 lfsck->li_status = LS_STOPPED;
3138                 lfsck->li_flags = 0;
3139         }
3140
3141         thread_set_flags(thread, SVC_STOPPING);
3142         spin_unlock(&lfsck->li_lock);
3143
3144         wake_up_all(&thread->t_ctl_waitq);
3145         l_wait_event(thread->t_ctl_waitq,
3146                      thread_is_stopped(thread),
3147                      &lwi);
3148
3149         GOTO(out, rc = 0);
3150
3151 out:
3152         mutex_unlock(&lfsck->li_mutex);
3153         lfsck_instance_put(env, lfsck);
3154
3155         return rc != 0 ? rc : rc1;
3156 }
3157 EXPORT_SYMBOL(lfsck_stop);
3158
3159 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3160                     struct lfsck_request *lr, struct thandle *th)
3161 {
3162         int rc = -EOPNOTSUPP;
3163         ENTRY;
3164
3165         switch (lr->lr_event) {
3166         case LE_START: {
3167                 struct lfsck_start       *start = &lfsck_env_info(env)->lti_start;
3168                 struct lfsck_start_param  lsp;
3169
3170                 memset(start, 0, sizeof(*start));
3171                 start->ls_valid = lr->lr_valid;
3172                 start->ls_speed_limit = lr->lr_speed;
3173                 start->ls_version = lr->lr_version;
3174                 start->ls_active = lr->lr_active;
3175                 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3176                 start->ls_async_windows = lr->lr_async_windows;
3177
3178                 lsp.lsp_start = start;
3179                 lsp.lsp_index = lr->lr_index;
3180                 lsp.lsp_index_valid = 1;
3181                 rc = lfsck_start(env, key, &lsp);
3182                 break;
3183         }
3184         case LE_STOP: {
3185                 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3186
3187                 memset(stop, 0, sizeof(*stop));
3188                 stop->ls_status = lr->lr_status;
3189                 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3190                 rc = lfsck_stop(env, key, stop);
3191                 break;
3192         }
3193         case LE_PHASE1_DONE:
3194         case LE_PHASE2_DONE:
3195         case LE_FID_ACCESSED:
3196         case LE_PEER_EXIT:
3197         case LE_CONDITIONAL_DESTROY:
3198         case LE_SKIP_NLINK_DECLARE:
3199         case LE_SKIP_NLINK:
3200         case LE_SET_LMV_MASTER:
3201         case LE_SET_LMV_SLAVE:
3202         case LE_PAIRS_VERIFY: {
3203                 struct lfsck_instance  *lfsck;
3204                 struct lfsck_component *com;
3205
3206                 lfsck = lfsck_instance_find(key, true, false);
3207                 if (unlikely(lfsck == NULL))
3208                         RETURN(-ENXIO);
3209
3210                 com = lfsck_component_find(lfsck, lr->lr_active);
3211                 if (likely(com != NULL)) {
3212                         rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3213                         lfsck_component_put(env, com);
3214                 }
3215
3216                 lfsck_instance_put(env, lfsck);
3217                 break;
3218         }
3219         default:
3220                 break;
3221         }
3222
3223         RETURN(rc);
3224 }
3225 EXPORT_SYMBOL(lfsck_in_notify);
3226
3227 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3228                 struct lfsck_request *lr)
3229 {
3230         struct lfsck_instance  *lfsck;
3231         struct lfsck_component *com;
3232         int                     rc;
3233         ENTRY;
3234
3235         lfsck = lfsck_instance_find(key, true, false);
3236         if (unlikely(lfsck == NULL))
3237                 RETURN(-ENXIO);
3238
3239         com = lfsck_component_find(lfsck, lr->lr_active);
3240         if (likely(com != NULL)) {
3241                 rc = com->lc_ops->lfsck_query(env, com);
3242                 lfsck_component_put(env, com);
3243         } else {
3244                 rc = -ENOTSUPP;
3245         }
3246
3247         lfsck_instance_put(env, lfsck);
3248
3249         RETURN(rc);
3250 }
3251
3252 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3253                              struct ldlm_namespace *ns)
3254 {
3255         struct lfsck_instance  *lfsck;
3256         int                     rc      = -ENXIO;
3257
3258         lfsck = lfsck_instance_find(key, true, false);
3259         if (likely(lfsck != NULL)) {
3260                 lfsck->li_namespace = ns;
3261                 lfsck_instance_put(env, lfsck);
3262                 rc = 0;
3263         }
3264
3265         return rc;
3266 }
3267 EXPORT_SYMBOL(lfsck_register_namespace);
3268
3269 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3270                    struct dt_device *next, struct obd_device *obd,
3271                    lfsck_out_notify notify, void *notify_data, bool master)
3272 {
3273         struct lfsck_instance   *lfsck;
3274         struct dt_object        *root  = NULL;
3275         struct dt_object        *obj   = NULL;
3276         struct lu_fid           *fid   = &lfsck_env_info(env)->lti_fid;
3277         int                      rc;
3278         ENTRY;
3279
3280         lfsck = lfsck_instance_find(key, false, false);
3281         if (unlikely(lfsck != NULL))
3282                 RETURN(-EEXIST);
3283
3284         OBD_ALLOC_PTR(lfsck);
3285         if (lfsck == NULL)
3286                 RETURN(-ENOMEM);
3287
3288         mutex_init(&lfsck->li_mutex);
3289         spin_lock_init(&lfsck->li_lock);
3290         INIT_LIST_HEAD(&lfsck->li_link);
3291         INIT_LIST_HEAD(&lfsck->li_list_scan);
3292         INIT_LIST_HEAD(&lfsck->li_list_dir);
3293         INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3294         INIT_LIST_HEAD(&lfsck->li_list_idle);
3295         INIT_LIST_HEAD(&lfsck->li_list_lmv);
3296         atomic_set(&lfsck->li_ref, 1);
3297         atomic_set(&lfsck->li_double_scan_count, 0);
3298         init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3299         lfsck->li_out_notify = notify;
3300         lfsck->li_out_notify_data = notify_data;
3301         lfsck->li_next = next;
3302         lfsck->li_bottom = key;
3303         lfsck->li_obd = obd;
3304
3305         rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3306         if (rc != 0)
3307                 GOTO(out, rc);
3308
3309         rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3310         if (rc != 0)
3311                 GOTO(out, rc);
3312
3313         fid->f_seq = FID_SEQ_LOCAL_NAME;
3314         fid->f_oid = 1;
3315         fid->f_ver = 0;
3316         rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3317         if (rc != 0)
3318                 GOTO(out, rc);
3319
3320         rc = dt_root_get(env, key, fid);
3321         if (rc != 0)
3322                 GOTO(out, rc);
3323
3324         root = dt_locate(env, key, fid);
3325         if (IS_ERR(root))
3326                 GOTO(out, rc = PTR_ERR(root));
3327
3328         if (unlikely(!dt_try_as_dir(env, root)))
3329                 GOTO(out, rc = -ENOTDIR);
3330
3331         lfsck->li_local_root_fid = *fid;
3332         if (master) {
3333                 lfsck->li_master = 1;
3334                 if (lfsck_dev_idx(lfsck) == 0) {
3335                         struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3336                         const struct lu_name *cname;
3337
3338                         rc = dt_lookup(env, root,
3339                                 (struct dt_rec *)(&lfsck->li_global_root_fid),
3340                                 (const struct dt_key *)"ROOT");
3341                         if (rc != 0)
3342                                 GOTO(out, rc);
3343
3344                         obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3345                         if (IS_ERR(obj))
3346                                 GOTO(out, rc = PTR_ERR(obj));
3347
3348                         if (unlikely(!dt_try_as_dir(env, obj)))
3349                                 GOTO(out, rc = -ENOTDIR);
3350
3351                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3352                                 (const struct dt_key *)dotlustre);
3353                         if (rc != 0)
3354                                 GOTO(out, rc);
3355
3356                         lfsck_object_put(env, obj);
3357                         obj = dt_locate(env, key, fid);
3358                         if (IS_ERR(obj))
3359                                 GOTO(out, rc = PTR_ERR(obj));
3360
3361                         cname = lfsck_name_get_const(env, dotlustre,
3362                                                      strlen(dotlustre));
3363                         rc = lfsck_verify_linkea(env, obj, cname,
3364                                                  &lfsck->li_global_root_fid);
3365                         if (rc != 0)
3366                                 GOTO(out, rc);
3367
3368                         if (unlikely(!dt_try_as_dir(env, obj)))
3369                                 GOTO(out, rc = -ENOTDIR);
3370
3371                         *pfid = *fid;
3372                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3373                                        (const struct dt_key *)lostfound);
3374                         if (rc != 0)
3375                                 GOTO(out, rc);
3376
3377                         lfsck_object_put(env, obj);
3378                         obj = dt_locate(env, key, fid);
3379                         if (IS_ERR(obj))
3380                                 GOTO(out, rc = PTR_ERR(obj));
3381
3382                         cname = lfsck_name_get_const(env, lostfound,
3383                                                      strlen(lostfound));
3384                         rc = lfsck_verify_linkea(env, obj, cname, pfid);
3385                         if (rc != 0)
3386                                 GOTO(out, rc);
3387
3388                         lfsck_object_put(env, obj);
3389                         obj = NULL;
3390                 }
3391         }
3392
3393         fid->f_seq = FID_SEQ_LOCAL_FILE;
3394         fid->f_oid = OTABLE_IT_OID;
3395         fid->f_ver = 0;
3396         obj = dt_locate(env, key, fid);
3397         if (IS_ERR(obj))
3398                 GOTO(out, rc = PTR_ERR(obj));
3399
3400         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3401         if (rc != 0)
3402                 GOTO(out, rc);
3403
3404         lfsck->li_obj_oit = obj;
3405         obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3406                                         S_IFDIR | S_IRUGO | S_IWUSR);
3407         if (IS_ERR(obj))
3408                 GOTO(out, rc = PTR_ERR(obj));
3409
3410         lu_object_get(&obj->do_lu);
3411         lfsck->li_lfsck_dir = obj;
3412         rc = lfsck_bookmark_setup(env, lfsck);
3413         if (rc != 0)
3414                 GOTO(out, rc);
3415
3416         if (master) {
3417                 rc = lfsck_fid_init(lfsck);
3418                 if (rc < 0)
3419                         GOTO(out, rc);
3420
3421                 rc = lfsck_namespace_setup(env, lfsck);
3422                 if (rc < 0)
3423                         GOTO(out, rc);
3424         }
3425
3426         rc = lfsck_layout_setup(env, lfsck);
3427         if (rc < 0)
3428                 GOTO(out, rc);
3429
3430         /* XXX: more LFSCK components initialization to be added here. */
3431
3432         rc = lfsck_instance_add(lfsck);
3433         if (rc == 0)
3434                 rc = lfsck_add_target_from_orphan(env, lfsck);
3435 out:
3436         if (obj != NULL && !IS_ERR(obj))
3437                 lfsck_object_put(env, obj);
3438         if (root != NULL && !IS_ERR(root))
3439                 lfsck_object_put(env, root);
3440         if (rc != 0)
3441                 lfsck_instance_cleanup(env, lfsck);
3442         return rc;
3443 }
3444 EXPORT_SYMBOL(lfsck_register);
3445
3446 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3447 {
3448         struct lfsck_instance *lfsck;
3449
3450         lfsck = lfsck_instance_find(key, false, true);
3451         if (lfsck != NULL)
3452                 lfsck_instance_put(env, lfsck);
3453 }
3454 EXPORT_SYMBOL(lfsck_degister);
3455
3456 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3457                      struct dt_device *tgt, struct obd_export *exp,
3458                      __u32 index, bool for_ost)
3459 {
3460         struct lfsck_instance   *lfsck;
3461         struct lfsck_tgt_desc   *ltd;
3462         int                      rc;
3463         ENTRY;
3464
3465         OBD_ALLOC_PTR(ltd);
3466         if (ltd == NULL)
3467                 RETURN(-ENOMEM);
3468
3469         ltd->ltd_tgt = tgt;
3470         ltd->ltd_key = key;
3471         ltd->ltd_exp = exp;
3472         INIT_LIST_HEAD(&ltd->ltd_orphan_list);
3473         INIT_LIST_HEAD(&ltd->ltd_layout_list);
3474         INIT_LIST_HEAD(&ltd->ltd_layout_phase_list);
3475         INIT_LIST_HEAD(&ltd->ltd_namespace_list);
3476         INIT_LIST_HEAD(&ltd->ltd_namespace_phase_list);
3477         atomic_set(&ltd->ltd_ref, 1);
3478         ltd->ltd_index = index;
3479
3480         spin_lock(&lfsck_instance_lock);
3481         lfsck = __lfsck_instance_find(key, true, false);
3482         if (lfsck == NULL) {
3483                 if (for_ost)
3484                         list_add_tail(&ltd->ltd_orphan_list,
3485                                       &lfsck_ost_orphan_list);
3486                 else
3487                         list_add_tail(&ltd->ltd_orphan_list,
3488                                       &lfsck_mdt_orphan_list);
3489                 spin_unlock(&lfsck_instance_lock);
3490
3491                 RETURN(0);
3492         }
3493         spin_unlock(&lfsck_instance_lock);
3494
3495         rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3496         if (rc != 0)
3497                 lfsck_tgt_put(ltd);
3498
3499         lfsck_instance_put(env, lfsck);
3500
3501         RETURN(rc);
3502 }
3503 EXPORT_SYMBOL(lfsck_add_target);
3504
3505 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3506                       struct dt_device *tgt, __u32 index, bool for_ost)
3507 {
3508         struct lfsck_instance   *lfsck;
3509         struct lfsck_tgt_descs  *ltds;
3510         struct lfsck_tgt_desc   *ltd;
3511         struct list_head        *head;
3512
3513         if (for_ost)
3514                 head = &lfsck_ost_orphan_list;
3515         else
3516                 head = &lfsck_mdt_orphan_list;
3517
3518         spin_lock(&lfsck_instance_lock);
3519         list_for_each_entry(ltd, head, ltd_orphan_list) {
3520                 if (ltd->ltd_tgt == tgt) {
3521                         list_del_init(&ltd->ltd_orphan_list);
3522                         spin_unlock(&lfsck_instance_lock);
3523                         lfsck_tgt_put(ltd);
3524
3525                         return;
3526                 }
3527         }
3528
3529         ltd = NULL;
3530         lfsck = __lfsck_instance_find(key, true, false);
3531         spin_unlock(&lfsck_instance_lock);
3532         if (unlikely(lfsck == NULL))
3533                 return;
3534
3535         if (for_ost)
3536                 ltds = &lfsck->li_ost_descs;
3537         else
3538                 ltds = &lfsck->li_mdt_descs;
3539
3540         down_write(&ltds->ltd_rw_sem);
3541         LASSERT(ltds->ltd_tgts_bitmap != NULL);
3542
3543         if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3544                 goto unlock;
3545
3546         ltd = lfsck_ltd2tgt(ltds, index);
3547         if (unlikely(ltd == NULL))
3548                 goto unlock;
3549
3550         LASSERT(ltds->ltd_tgtnr > 0);
3551
3552         ltds->ltd_tgtnr--;
3553         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3554         lfsck_assign_tgt(ltds, NULL, index);
3555
3556 unlock:
3557         if (ltd == NULL) {
3558                 if (for_ost)
3559                         head = &lfsck->li_ost_descs.ltd_orphan;
3560                 else
3561                         head = &lfsck->li_mdt_descs.ltd_orphan;
3562
3563                 list_for_each_entry(ltd, head, ltd_orphan_list) {
3564                         if (ltd->ltd_tgt == tgt) {
3565                                 list_del_init(&ltd->ltd_orphan_list);
3566                                 break;
3567                         }
3568                 }
3569         }
3570
3571         up_write(&ltds->ltd_rw_sem);
3572         if (ltd != NULL) {
3573                 spin_lock(&ltds->ltd_lock);
3574                 ltd->ltd_dead = 1;
3575                 spin_unlock(&ltds->ltd_lock);
3576                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3577                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3578                 lfsck_tgt_put(ltd);
3579         }
3580
3581         lfsck_instance_put(env, lfsck);
3582 }
3583 EXPORT_SYMBOL(lfsck_del_target);
3584
3585 static int __init lfsck_init(void)
3586 {
3587         int rc;
3588
3589         INIT_LIST_HEAD(&lfsck_instance_list);
3590         INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3591         INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3592         lfsck_key_init_generic(&lfsck_thread_key, NULL);
3593         rc = lu_context_key_register(&lfsck_thread_key);
3594         if (rc == 0) {
3595                 tgt_register_lfsck_in_notify(lfsck_in_notify);
3596                 tgt_register_lfsck_query(lfsck_query);
3597         }
3598
3599         return rc;
3600 }
3601
3602 static void __exit lfsck_exit(void)
3603 {
3604         struct lfsck_tgt_desc *ltd;
3605         struct lfsck_tgt_desc *next;
3606
3607         LASSERT(list_empty(&lfsck_instance_list));
3608
3609         list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3610                                  ltd_orphan_list) {
3611                 list_del_init(&ltd->ltd_orphan_list);
3612                 lfsck_tgt_put(ltd);
3613         }
3614
3615         list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3616                                  ltd_orphan_list) {
3617                 list_del_init(&ltd->ltd_orphan_list);
3618                 lfsck_tgt_put(ltd);
3619         }
3620
3621         lu_context_key_degister(&lfsck_thread_key);
3622 }
3623
3624 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3625 MODULE_DESCRIPTION("Lustre File System Checker");
3626 MODULE_VERSION(LUSTRE_VERSION_STRING);
3627 MODULE_LICENSE("GPL");
3628
3629 module_init(lfsck_init);
3630 module_exit(lfsck_exit);