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