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