Whamcloud - gitweb
LU-10268 lfsck: postpone lfsck start until initialized
[fs/lustre-release.git] / lustre / lfsck / lfsck_internal.h
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, 2016, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_internal.h
27  *
28  * Shared definitions and declarations for the LFSCK.
29  *
30  * Author: Fan, Yong <fan.yong@intel.com>
31  */
32
33 #ifndef _LFSCK_INTERNAL_H
34 # define _LFSCK_INTERNAL_H
35
36 #include <lustre_lfsck.h>
37 #include <obd.h>
38 #include <lu_object.h>
39 #include <dt_object.h>
40 #include <md_object.h>
41 #include <lustre_net.h>
42 #include <lustre_dlm.h>
43 #include <lustre_fid.h>
44 #include <md_object.h>
45 #include <lustre_linkea.h>
46
47 #define LFSCK_CHECKPOINT_INTERVAL       60
48
49 enum lfsck_flags {
50         /* Finish the first cycle scanning. */
51         LF_SCANNED_ONCE         = 0x00000001ULL,
52
53         /* There is some namespace inconsistency. */
54         LF_INCONSISTENT         = 0x00000002ULL,
55
56         /* The device is upgraded from 1.8 format. */
57         LF_UPGRADE              = 0x00000004ULL,
58
59         /* The server ever restarted during the LFSCK, and may miss to process
60          * some objects check/repair. */
61         LF_INCOMPLETE           = 0x00000008ULL,
62
63         /* The LAST_ID (file) crashed. */
64         LF_CRASHED_LASTID       = 0x00000010ULL,
65 };
66
67 struct lfsck_position {
68         /* low layer object table-based iteration position. */
69         __u64   lp_oit_cookie;
70
71         /* parent FID for directory traversal. */
72         struct lu_fid lp_dir_parent;
73
74         /* namespace-based directory traversal position. */
75         __u64   lp_dir_cookie;
76 };
77
78 struct lfsck_bookmark {
79         /* Magic number to detect that this struct contains valid data. */
80         __u32   lb_magic;
81
82         /* For compatible with old versions. */
83         __u16   lb_version;
84
85         /* See 'enum lfsck_param_flags' */
86         __u16   lb_param;
87
88         /* How many items can be scanned at most per second. */
89         __u32   lb_speed_limit;
90
91         /* The windows size for async requests pipeline. */
92         __u16   lb_async_windows;
93
94         /* For 64-bits aligned. */
95         __u16   lb_padding;
96
97         /* The FID for .lustre/lost+found/MDTxxxx */
98         struct lu_fid   lb_lpf_fid;
99
100         /* The FID for the last MDT-object created by the LFSCK repairing. */
101         struct lu_fid   lb_last_fid;
102
103         /* For future using. */
104         __u64   lb_reserved[2];
105 };
106
107 enum lfsck_namespace_trace_flags {
108         LNTF_CHECK_LINKEA       = 0x01,
109         LNTF_CHECK_PARENT       = 0x02,
110         LNTF_CHECK_ORPHAN       = 0x08,
111         LNTF_UNCERTAIN_LMV      = 0x10,
112         LNTF_RECHECK_NAME_HASH  = 0x20,
113         LNTF_ALL                = 0xff
114 };
115
116 enum lfsck_namespace_inconsistency_type {
117         LNIT_NONE               = 0,
118         LNIT_BAD_LINKEA         = 1,
119         LNIT_UNMATCHED_PAIRS    = 2,
120         LNIT_DANGLING           = 3,
121         LNIT_MUL_REF            = 4,
122         LNIT_BAD_TYPE           = 5,
123         LNIT_BAD_DIRENT         = 6,
124 };
125
126 struct lfsck_namespace {
127         /* Magic number to detect that this struct contains valid data. */
128         __u32   ln_magic;
129
130         /* See 'enum lfsck_status'. */
131         __u32   ln_status;
132
133         /* See 'enum lfsck_flags'. */
134         __u32   ln_flags;
135
136         /* How many completed LFSCK runs on the device. */
137         __u32   ln_success_count;
138
139         /*  How long the LFSCK phase1 has run in seconds. */
140         time64_t ln_run_time_phase1;
141
142         /*  How long the LFSCK phase2 has run in seconds. */
143         time64_t ln_run_time_phase2;
144
145         /* Time for the last LFSCK completed in seconds since epoch. */
146         time64_t ln_time_last_complete;
147
148         /* Time for the latest LFSCK ran in seconds since epoch. */
149         time64_t ln_time_latest_start;
150
151         /* Time for the last LFSCK checkpoint in seconds since epoch. */
152         time64_t ln_time_last_checkpoint;
153
154         /* Position for the latest LFSCK started from. */
155         struct lfsck_position   ln_pos_latest_start;
156
157         /* Position for the last LFSCK checkpoint. */
158         struct lfsck_position   ln_pos_last_checkpoint;
159
160         /* Position for the first should be updated object. */
161         struct lfsck_position   ln_pos_first_inconsistent;
162
163         /* How many items (including dir) have been checked. */
164         __u64   ln_items_checked;
165
166         /* How many items have been repaired. */
167         __u64   ln_items_repaired;
168
169         /* How many items failed to be processed. */
170         __u64   ln_items_failed;
171
172         /* How many directories have been traversed. */
173         __u64   ln_dirs_checked;
174
175         /* How many objects have been double scanned. */
176         __u64   ln_objs_checked_phase2;
177
178         /* How many objects have been reparied during double scan. */
179         __u64   ln_objs_repaired_phase2;
180
181         /* How many objects failed to be processed during double scan. */
182         __u64   ln_objs_failed_phase2;
183
184         /* How many objects with nlink fixed. */
185         __u64   ln_objs_nlink_repaired;
186
187         /* The latest object has been processed (failed) during double scan. */
188         struct lu_fid   ln_fid_latest_scanned_phase2;
189
190         /* How many FID-in-dirent entries have been repaired. */
191         __u64   ln_dirent_repaired;
192
193         /* How many linkEA entries have been repaired. */
194         __u64   ln_linkea_repaired;
195
196         /* How many multiple-linked objects have been checked. */
197         __u64   ln_mul_linked_checked;
198
199         /* How many multiple-linked objects have been repaired. */
200         __u64   ln_mul_linked_repaired;
201
202         /* How many undefined inconsistency found in phase2. */
203         __u64   ln_unknown_inconsistency;
204
205         /* How many unmatched pairs have been repaired. */
206         __u64   ln_unmatched_pairs_repaired;
207
208         /* How many dangling name entries have been found/repaired. */
209         __u64   ln_dangling_repaired;
210
211         /* How many multiple referenced name entries have been
212          * found/repaired. */
213         __u64   ln_mul_ref_repaired;
214
215         /* How many name entries with bad file type have been repaired. */
216         __u64   ln_bad_type_repaired;
217
218         /* How many lost name entries have been re-inserted. */
219         __u64   ln_lost_dirent_repaired;
220
221         /* How many objects under /lost+found have been scanned. */
222         __u64   ln_local_lpf_scanned;
223
224         /* How many objects under /lost+found have been moved to
225          * namespace visible directory. */
226         __u64   ln_local_lpf_moved;
227
228         /* How many objects under /lost+found have been skipped. */
229         __u64   ln_local_lpf_skipped;
230
231         /* How many objects under /lost+found failed to be processed. */
232         __u64   ln_local_lpf_failed;
233
234         /* How many striped directories (master) have been scanned. */
235         __u64   ln_striped_dirs_scanned;
236
237         /* How many striped directories (master) have been repaired. */
238         __u64   ln_striped_dirs_repaired;
239
240         /* How many striped directories (master) failed verification. */
241         __u64   ln_striped_dirs_failed;
242
243         /* How many striped directories (master) has been disabled. */
244         __u64   ln_striped_dirs_disabled;
245
246         /* How many striped directory's (master) have been skipped
247          * (for shards verification) because of lost master LMV EA. */
248         __u64   ln_striped_dirs_skipped;
249
250         /* How many striped directory's shards (slave) have been scanned. */
251         __u64   ln_striped_shards_scanned;
252
253         /* How many striped directory's shards (slave) have been repaired. */
254         __u64   ln_striped_shards_repaired;
255
256         /* How many striped directory's shards (slave) failed verification. */
257         __u64   ln_striped_shards_failed;
258
259         /* How many striped directory's shards (slave) have been skipped
260          * (for name hash verification) because do not know whether the slave
261          * LMV EA is valid or not. */
262         __u64   ln_striped_shards_skipped;
263
264         /* How many name entries under striped directory with bad name
265          * hash have been repaired. */
266         __u64   ln_name_hash_repaired;
267
268         /* The size of MDT targets bitmap with nbits. Such bitmap records
269          * the MDTs that contain non-verified MDT-objects. */
270         __u32   ln_bitmap_size;
271
272         /* For further using. 256-bytes aligned now. */
273         __u32   ln_reserved_1;
274
275         /* Time for the latest LFSCK scan in seconds from the beginning. */
276         time64_t ln_time_latest_reset;
277
278         /* How many linkEA overflow timestamp have been cleared. */
279         __u64   ln_linkea_overflow_cleared;
280
281         /* For further using. 256-bytes aligned now. */
282         __u64   ln_reserved[12];
283 };
284
285 enum lfsck_layout_inconsistency_type {
286         LLIT_NONE                       = 0,
287         LLIT_DANGLING                   = 1,
288         LLIT_UNMATCHED_PAIR             = 2,
289         LLIT_MULTIPLE_REFERENCED        = 3,
290         LLIT_ORPHAN                     = 4,
291         LLIT_INCONSISTENT_OWNER         = 5,
292         LLIT_OTHERS                     = 6,
293         LLIT_MAX                        = LLIT_OTHERS
294 };
295
296 struct lfsck_layout {
297         /* Magic number to detect that this struct contains valid data. */
298         __u32   ll_magic;
299
300         /* See 'enum lfsck_status'. */
301         __u32   ll_status;
302
303         /* See 'enum lfsck_flags'. */
304         __u32   ll_flags;
305
306         /* How many completed LFSCK runs on the device. */
307         __u32   ll_success_count;
308
309         /*  How long the LFSCK phase1 has run in seconds. */
310         time64_t ll_run_time_phase1;
311
312         /*  How long the LFSCK phase2 has run in seconds. */
313         time64_t ll_run_time_phase2;
314
315         /* Time for the last LFSCK completed in seconds since epoch. */
316         time64_t ll_time_last_complete;
317
318         /* Time for the latest LFSCK ran in seconds since epoch. */
319         time64_t ll_time_latest_start;
320
321         /* Time for the last LFSCK checkpoint in seconds since epoch. */
322         time64_t ll_time_last_checkpoint;
323
324         /* Position for the latest LFSCK started from. */
325         __u64   ll_pos_latest_start;
326
327         /* Position for the last LFSCK checkpoint. */
328         __u64   ll_pos_last_checkpoint;
329
330         /* Position for the first object to be fixed or
331          * failed to be checked in the phase1. */
332         __u64   ll_pos_first_inconsistent;
333
334         /* How many objects have been checked. */
335         __u64   ll_objs_checked_phase1;
336
337         /* How many objects failed to be processed. */
338         __u64   ll_objs_failed_phase1;
339
340         /* How many objects have been double scanned. */
341         __u64   ll_objs_checked_phase2;
342
343         /* How many objects failed to be processed during double scan. */
344         __u64   ll_objs_failed_phase2;
345
346         /* kinds of inconsistency have been or to be repaired.
347          * ll_objs_repaired[type - 1] is the count for the given @type. */
348         __u64   ll_objs_repaired[LLIT_MAX];
349
350         /* How many objects have been skipped because of related
351          * MDT(s)/OST(s) do not participate in the LFSCK */
352         __u64   ll_objs_skipped;
353
354         /* The size of ll_ost_bitmap with nbits. */
355         __u32   ll_bitmap_size;
356
357         /* For further using. 256-bytes aligned now. */
358         __u32   ll_reserved_1;
359
360         /* The latest object has been processed (failed) during double scan. */
361         struct lfsck_layout_dangling_key ll_lldk_latest_scanned_phase2;
362
363         /* For further using */
364         u64     ll_reserved_2[7];
365
366         /* The OST targets bitmap to record the OSTs that contain
367          * non-verified OST-objects. */
368         __u8    ll_ost_bitmap[0];
369 };
370
371 struct lfsck_assistant_object {
372         struct lu_fid           lso_fid;
373         __u64                   lso_oit_cookie;
374         struct lu_attr          lso_attr;
375         atomic_t                lso_ref;
376         unsigned int            lso_dead:1,
377                                 lso_is_dir:1;
378 };
379
380 struct lfsck_component;
381 struct lfsck_tgt_descs;
382 struct lfsck_tgt_desc;
383
384 struct lfsck_operations {
385         int (*lfsck_reset)(const struct lu_env *env,
386                            struct lfsck_component *com,
387                            bool init);
388
389         void (*lfsck_fail)(const struct lu_env *env,
390                            struct lfsck_component *com,
391                            bool new_checked);
392
393         void (*lfsck_close_dir)(const struct lu_env *env,
394                                 struct lfsck_component *com);
395
396         int (*lfsck_open_dir)(const struct lu_env *env,
397                               struct lfsck_component *com);
398
399         int (*lfsck_checkpoint)(const struct lu_env *env,
400                                 struct lfsck_component *com,
401                                 bool init);
402
403         int (*lfsck_prep)(const struct lu_env *env,
404                           struct lfsck_component *com,
405                           struct lfsck_start_param *lsp);
406
407         int (*lfsck_exec_oit)(const struct lu_env *env,
408                               struct lfsck_component *com,
409                               struct dt_object *obj);
410
411         int (*lfsck_exec_dir)(const struct lu_env *env,
412                               struct lfsck_component *com,
413                               struct lfsck_assistant_object *lso,
414                               struct lu_dirent *ent,
415                               __u16 type);
416
417         int (*lfsck_post)(const struct lu_env *env,
418                           struct lfsck_component *com,
419                           int result,
420                           bool init);
421
422         void (*lfsck_dump)(const struct lu_env *env,
423                            struct lfsck_component *com,
424                            struct seq_file *m);
425
426         int (*lfsck_double_scan)(const struct lu_env *env,
427                                  struct lfsck_component *com);
428
429         void (*lfsck_data_release)(const struct lu_env *env,
430                                    struct lfsck_component *com);
431
432         void (*lfsck_quit)(const struct lu_env *env,
433                            struct lfsck_component *com);
434
435         int (*lfsck_in_notify_local)(const struct lu_env *env,
436                                      struct lfsck_component *com,
437                                      struct lfsck_req_local *lrl,
438                                      struct thandle *th);
439
440         int (*lfsck_in_notify)(const struct lu_env *env,
441                                struct lfsck_component *com,
442                                struct lfsck_request *lr);
443
444         int (*lfsck_query)(const struct lu_env *env,
445                            struct lfsck_component *com,
446                            struct lfsck_request *req,
447                            struct lfsck_reply *rep,
448                            struct lfsck_query *que, int idx);
449
450         int (*lfsck_join)(const struct lu_env *env,
451                           struct lfsck_component *com,
452                           struct lfsck_start_param *lsp);
453 };
454
455 #define TGT_PTRS                256     /* number of pointers at 1st level */
456 #define TGT_PTRS_PER_BLOCK      256     /* number of pointers at 2nd level */
457
458 struct lfsck_tgt_desc {
459         struct list_head   ltd_orphan_list;
460         struct dt_device  *ltd_tgt;
461         struct dt_device  *ltd_key;
462         struct obd_export *ltd_exp;
463         struct list_head   ltd_layout_list;
464         struct list_head   ltd_layout_phase_list;
465         struct list_head   ltd_namespace_list;
466         struct list_head   ltd_namespace_phase_list;
467         __u32              ltd_layout_status;
468         __u32              ltd_namespace_status;
469         __u64              ltd_layout_repaired;
470         __u64              ltd_namespace_repaired;
471         atomic_t           ltd_ref;
472         __u32              ltd_index;
473         __u32              ltd_layout_gen;
474         __u32              ltd_namespace_gen;
475         unsigned int       ltd_dead:1,
476                            ltd_retry_start:1,
477                            ltd_layout_done:1,
478                            ltd_namespace_done:1,
479                            ltd_synced_failures:1;
480 };
481
482 struct lfsck_tgt_desc_idx {
483         struct lfsck_tgt_desc *ldi_tgts[TGT_PTRS_PER_BLOCK];
484 };
485
486 struct lfsck_tgt_descs {
487         /* list of known TGTs */
488         struct lfsck_tgt_desc_idx       *ltd_tgts_idx[TGT_PTRS];
489
490         /* bitmap of TGTs available */
491         struct cfs_bitmap                       *ltd_tgts_bitmap;
492
493         /* for lfsck_tgt_desc::ltd_xxx_list */
494         spinlock_t                       ltd_lock;
495
496         /* for tgts table accessing and changes */
497         struct rw_semaphore              ltd_rw_sem;
498
499         /* Temporary list for orphan targets. */
500         struct list_head                 ltd_orphan;
501
502         /* number of registered TGTs */
503         __u32                            ltd_tgtnr;
504 };
505
506 static inline struct lfsck_tgt_desc *
507 lfsck_ltd2tgt(struct lfsck_tgt_descs *ltd, __u32 index)
508 {
509         __u32 idx1 = index / TGT_PTRS_PER_BLOCK;
510         __u32 idx2 = index % TGT_PTRS_PER_BLOCK;
511         struct lfsck_tgt_desc *__tgt = NULL;
512
513         if (unlikely(idx1 >= TGT_PTRS))
514                 CDEBUG(D_LFSCK, "The target idx %u is invalid.\n", index);
515         else if (likely(ltd->ltd_tgts_idx[idx1] != NULL))
516                 __tgt = ltd->ltd_tgts_idx[idx1]->ldi_tgts[idx2];
517
518         return __tgt;
519 }
520
521 static inline void lfsck_assign_tgt(struct lfsck_tgt_descs *ltd,
522                                     struct lfsck_tgt_desc *tgt, __u32 index)
523 {
524         __u32 idx1 = index / TGT_PTRS_PER_BLOCK;
525         __u32 idx2 = index % TGT_PTRS_PER_BLOCK;
526
527         if (likely(idx1 < TGT_PTRS && ltd->ltd_tgts_idx[idx1] != NULL))
528                 ltd->ltd_tgts_idx[idx1]->ldi_tgts[idx2] = tgt;
529 }
530
531 #define LFSCK_STF_BITS  4
532 /* If want to adjust the LFSCK_STF_COUNT, please change LFSCK_STF_BITS. */
533 #define LFSCK_STF_COUNT (1 << LFSCK_STF_BITS)
534
535 struct lfsck_sub_trace_obj {
536         struct dt_object        *lsto_obj;
537         struct mutex             lsto_mutex;
538 };
539
540 struct lfsck_component {
541         /* into lfsck_instance::li_list_(scan,double_scan,idle} */
542         struct list_head         lc_link;
543
544         /* into lfsck_instance::li_list_dir */
545         struct list_head         lc_link_dir;
546
547         struct rw_semaphore      lc_sem;
548         atomic_t                 lc_ref;
549
550         struct lfsck_position    lc_pos_start;
551         struct lfsck_instance   *lc_lfsck;
552         struct dt_object        *lc_obj;
553         struct lfsck_sub_trace_obj lc_sub_trace_objs[LFSCK_STF_COUNT];
554         struct lfsck_operations *lc_ops;
555         void                    *lc_file_ram;
556         void                    *lc_file_disk;
557         void                    *lc_data;
558         struct lu_fid            lc_fid_latest_scanned_phase2;
559
560         /* The time for last checkpoint, seconds */
561         time64_t                 lc_time_last_checkpoint;
562
563         /* The time for next checkpoint, seconds */
564         time64_t                 lc_time_next_checkpoint;
565
566         __u32                    lc_file_size;
567
568         /* How many objects have been checked since last checkpoint. */
569         __u32                    lc_new_checked;
570
571         /* How many objects have been scanned since last sleep. */
572         __u32                    lc_new_scanned;
573
574         __u16                    lc_type;
575 };
576
577 #define LFSCK_LMV_MAX_STRIPES   LMV_MAX_STRIPE_COUNT
578 #define LFSCK_LMV_DEF_STRIPES   4
579
580 /* Warning: NOT change the lfsck_slave_lmv_flags members order,
581  *          otherwise the lfsck_record_lmv() may be wrong. */
582 enum lfsck_slave_lmv_flags {
583         LSLF_NONE       = 0,
584         LSLF_BAD_INDEX2 = 1,
585         LSLF_NO_LMVEA   = 2,
586         LSLF_DANGLING   = 3,
587         LSLF_BAD_INDEX1 = 4,
588 };
589
590 /* When the namespace LFSCK scans a striped directory, it will record all
591  * the known shards' information in the structure "lfsck_slave_lmv_rec",
592  * including the shard's FID, index, slave LMV EA, and so on. Each shard
593  * will take one lfsck_slave_lmv_rec slot. After the 1st cycle scanning
594  * the striped directory, the LFSCK will get all the information about
595  * whether there are some inconsistency, and then it can repair them in
596  * the 2nd cycle scanning. */
597 struct lfsck_slave_lmv_rec {
598         struct lu_fid   lslr_fid;
599         __u32           lslr_stripe_count;
600         __u32           lslr_index; /* the index in name or in slave lmv */
601         __u32           lslr_hash_type;
602         __u32           lslr_flags;
603 };
604
605 struct lfsck_lmv {
606         struct lmv_mds_md_v1             ll_lmv;
607         atomic_t                         ll_ref;
608         int                              ll_stripes_allocated;
609         int                              ll_stripes_filled;
610         int                              ll_exit_value;
611         __u32                            ll_max_stripe_count;
612         __u32                            ll_max_filled_off;
613         __u32                            ll_hash_type;
614         unsigned int                     ll_lmv_master:1,
615                                          ll_lmv_slave:1,
616                                          ll_lmv_verified:1,
617                                          ll_lmv_updated:1,
618                                          ll_inline:1,
619                                          ll_failed:1,
620                                          ll_ignore:1;
621         struct lfsck_slave_lmv_rec      *ll_lslr; /* may be vmalloc'd */
622 };
623
624 /* If the namespace LFSCK finds that the master MDT-object of a striped
625  * directory lost its master LMV EA, it will re-generate the master LMV
626  * EA and notify the LFSCK instance on the MDT on which the striped dir
627  * master MDT-object resides to rescan the striped directory. To do that,
628  * the notify handler will insert a "lfsck_lmv_unit" structure into the
629  * lfsck::li_list_lmv. The LFSCK instance will scan such list from time
630  * to time to check whether needs to rescan some stirped directories. */
631 struct lfsck_lmv_unit {
632         struct list_head         llu_link;
633         struct lfsck_lmv         llu_lmv;
634         struct dt_object        *llu_obj;
635         struct lfsck_instance   *llu_lfsck;
636 };
637
638 struct lfsck_rec_lmv_save {
639         struct lu_fid           lrls_fid;
640         struct lmv_mds_md_v1    lrls_lmv;
641 };
642
643 /* Allow lfsck_record_lmv() to be called recursively at most three times. */
644 #define LFSCK_REC_LMV_MAX_DEPTH 3
645
646 struct lfsck_instance {
647         struct mutex              li_mutex;
648         spinlock_t                li_lock;
649
650         /* Link into the lfsck_instance_list. */
651         struct list_head          li_link;
652
653         /* For the components in (first) scanning via otable-based iteration. */
654         struct list_head          li_list_scan;
655
656         /* For the components in scanning via directory traversal. Because
657          * directory traversal cannot guarantee all the object be scanned,
658          * so the component in the li_list_dir must be in li_list_scan. */
659         struct list_head          li_list_dir;
660
661         /* For the components in double scanning. */
662         struct list_head          li_list_double_scan;
663
664         /* For the components those are not scanning now. */
665         struct list_head          li_list_idle;
666
667         /* For the lfsck_lmv_unit to be handled. */
668         struct list_head          li_list_lmv;
669
670         atomic_t                  li_ref;
671         atomic_t                  li_double_scan_count;
672         struct ptlrpc_thread      li_thread;
673
674         /* The time for last checkpoint, seconds */
675         time64_t                  li_time_last_checkpoint;
676
677         /* The time for next checkpoint, seconds */
678         time64_t                  li_time_next_checkpoint;
679
680         lfsck_out_notify          li_out_notify;
681         void                     *li_out_notify_data;
682         struct dt_device         *li_next;
683         struct dt_device         *li_bottom;
684         struct obd_device        *li_obd;
685         struct ldlm_namespace    *li_namespace;
686         struct local_oid_storage *li_los;
687         struct lu_fid             li_local_root_fid;  /* backend root "/" */
688         struct lu_fid             li_global_root_fid; /* /ROOT */
689         struct dt_object         *li_lfsck_dir;
690         struct dt_object         *li_bookmark_obj;
691         struct dt_object         *li_lpf_obj;
692         struct dt_object         *li_lpf_root_obj;
693         struct lu_client_seq     *li_seq;
694         struct lfsck_bookmark     li_bookmark_ram;
695         struct lfsck_bookmark     li_bookmark_disk;
696         struct lfsck_position     li_pos_current;
697         struct lfsck_position     li_pos_checkpoint;
698
699         struct lfsck_lmv         *li_lmv;
700
701         /* Obj for otable-based iteration */
702         struct dt_object         *li_obj_oit;
703
704         /* Obj for directory traversal */
705         struct dt_object         *li_obj_dir;
706
707         /* It for otable-based iteration */
708         struct dt_it             *li_di_oit;
709
710         /* It for directory traversal */
711         struct dt_it             *li_di_dir;
712
713         /* Description of OST */
714         struct lfsck_tgt_descs    li_ost_descs;
715
716         /* Description of MDT */
717         struct lfsck_tgt_descs    li_mdt_descs;
718
719         /* namespace-based directory traversal position. */
720         __u64                     li_cookie_dir;
721
722         /* Arguments for low layer otable-based iteration. */
723         __u32                     li_args_oit;
724
725         /* Arugments for namespace-based directory traversal. */
726         __u32                     li_args_dir;
727
728         /* Schedule for every N objects. */
729         __u32                     li_sleep_rate;
730
731         /* Sleep N jiffies for each schedule. */
732         __u32                     li_sleep_jif;
733
734         /* How many objects have been scanned since last sleep. */
735         __u32                     li_new_scanned;
736
737         /* The status when the LFSCK stopped or paused. */
738         __u32                     li_status;
739
740         /* The flags when the lFSCK stopped or paused. */
741         __u32                     li_flags;
742
743         unsigned int              li_oit_over:1, /* oit is finished. */
744                                   li_drop_dryrun:1, /* Ever dryrun, not now. */
745                                   li_master:1, /* Master instance or not. */
746                                   li_current_oit_processed:1,
747                                   li_start_unplug:1;
748         struct lfsck_rec_lmv_save li_rec_lmv_save[LFSCK_REC_LMV_MAX_DEPTH];
749 };
750
751 struct lfsck_async_interpret_args {
752         struct lfsck_component          *laia_com;
753         struct lfsck_tgt_descs          *laia_ltds;
754         struct lfsck_tgt_desc           *laia_ltd;
755         struct lfsck_request            *laia_lr;
756         atomic_t                        *laia_count;
757         int                              laia_result;
758         unsigned int                     laia_shared:1;
759 };
760
761 struct lfsck_thread_args {
762         struct lu_env                    lta_env;
763         struct lfsck_instance           *lta_lfsck;
764         struct lfsck_component          *lta_com;
765         struct lfsck_start_param        *lta_lsp;
766 };
767
768 struct lfsck_assistant_req {
769         struct list_head                 lar_list;
770         struct lfsck_assistant_object   *lar_parent;
771 };
772
773 struct lfsck_namespace_req {
774         struct lfsck_assistant_req       lnr_lar;
775         struct lfsck_lmv                *lnr_lmv;
776         struct lu_fid                    lnr_fid;
777         __u64                            lnr_dir_cookie;
778         __u32                            lnr_attr;
779         __u32                            lnr_size;
780         __u16                            lnr_type;
781         __u16                            lnr_namelen;
782         char                             lnr_name[0];
783 };
784
785 struct lfsck_layout_req {
786         struct lfsck_assistant_req       llr_lar;
787         struct dt_object                *llr_child;
788         __u32                            llr_comp_id;
789         __u32                            llr_ost_idx;
790         __u32                            llr_lov_idx; /* offset in LOV EA */
791 };
792
793 struct lfsck_assistant_operations {
794         int (*la_handler_p1)(const struct lu_env *env,
795                              struct lfsck_component *com,
796                              struct lfsck_assistant_req *lar);
797
798         int (*la_handler_p2)(const struct lu_env *env,
799                              struct lfsck_component *com);
800
801         void (*la_fill_pos)(const struct lu_env *env,
802                             struct lfsck_component *com,
803                             struct lfsck_position *pos);
804
805         int (*la_double_scan_result)(const struct lu_env *env,
806                                      struct lfsck_component *com,
807                                      int rc);
808
809         void (*la_req_fini)(const struct lu_env *env,
810                             struct lfsck_assistant_req *lar);
811
812         void (*la_sync_failures)(const struct lu_env *env,
813                                  struct lfsck_component *com,
814                                  struct lfsck_request *lr);
815 };
816
817 struct lfsck_assistant_data {
818         spinlock_t                               lad_lock;
819         struct list_head                         lad_req_list;
820
821         /* list for the ost targets involve LFSCK. */
822         struct list_head                         lad_ost_list;
823
824         /* list for the ost targets in phase1 scanning. */
825         struct list_head                         lad_ost_phase1_list;
826
827         /* list for the ost targets in phase2 scanning. */
828         struct list_head                         lad_ost_phase2_list;
829
830         /* list for the mdt targets involve LFSCK. */
831         struct list_head                         lad_mdt_list;
832
833         /* list for the mdt targets in phase1 scanning. */
834         struct list_head                         lad_mdt_phase1_list;
835
836         /* list for the mdt targets in phase2 scanning. */
837         struct list_head                         lad_mdt_phase2_list;
838
839         const char                              *lad_name;
840         struct ptlrpc_thread                     lad_thread;
841         struct task_struct                      *lad_task;
842
843         struct lfsck_assistant_operations       *lad_ops;
844
845         struct cfs_bitmap                               *lad_bitmap;
846
847         __u32                                    lad_touch_gen;
848         int                                      lad_prefetched;
849         int                                      lad_assistant_status;
850         int                                      lad_post_result;
851         unsigned int                             lad_to_post:1,
852                                                  lad_to_double_scan:1,
853                                                  lad_in_double_scan:1,
854                                                  lad_exit:1,
855                                                  lad_incomplete:1;
856         bool                                     lad_advance_lock;
857 };
858
859 #define LFSCK_TMPBUF_LEN        64
860
861 struct lfsck_lock_handle {
862         struct lustre_handle    llh_pdo_lh;
863         struct lustre_handle    llh_reg_lh;
864         enum ldlm_mode          llh_pdo_mode;
865         enum ldlm_mode          llh_reg_mode;
866 };
867
868 struct lfsck_thread_info {
869         struct lu_name          lti_name_const;
870         struct lu_name          lti_name;
871         struct lu_name          lti_name2;
872         struct lu_buf           lti_buf;
873         struct lu_buf           lti_linkea_buf;
874         struct lu_buf           lti_linkea_buf2;
875         struct lu_buf           lti_big_buf;
876         struct lu_fid           lti_fid;
877         struct lu_fid           lti_fid2;
878         struct lu_fid           lti_fid3;
879         struct lu_fid           lti_fid4;
880         struct lu_attr          lti_la;
881         struct lu_attr          lti_la2;
882         struct ost_id           lti_oi;
883         struct lustre_ost_attrs lti_loa;
884         struct dt_object_format lti_dof;
885         /* There will be '\0' at the end of the name. */
886         char            lti_key[sizeof(struct lu_dirent) + NAME_MAX + 1];
887         char                    lti_tmpbuf[LFSCK_TMPBUF_LEN];
888         char                    lti_tmpbuf2[LFSCK_TMPBUF_LEN];
889         struct lfsck_request    lti_lr;
890         struct lfsck_async_interpret_args lti_laia;
891         struct lfsck_async_interpret_args lti_laia2;
892         struct lfsck_start      lti_start;
893         struct lfsck_stop       lti_stop;
894         union ldlm_policy_data  lti_policy;
895         struct ldlm_enqueue_info lti_einfo;
896         struct ldlm_res_id      lti_resid;
897         struct filter_fid       lti_ff;
898         struct dt_allocation_hint lti_hint;
899         struct lu_orphan_rec_v2 lti_rec;
900         struct lov_user_md      lti_lum;
901         struct dt_insert_rec    lti_dt_rec;
902         struct lu_object_conf   lti_conf;
903         struct lu_seq_range     lti_range;
904         struct lmv_mds_md_v1    lti_lmv;
905         struct lmv_mds_md_v1    lti_lmv2;
906         struct lmv_mds_md_v1    lti_lmv3;
907         struct lmv_mds_md_v1    lti_lmv4;
908         struct lfsck_lock_handle lti_llh;
909         struct lfsck_layout_dangling_key lti_lldk;
910 };
911
912 /* lfsck_lib.c */
913 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
914                     struct lu_fid *fid, bool locked);
915 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
916                      struct dt_object *obj, struct lustre_handle *lh,
917                      __u64 bits, enum ldlm_mode mode);
918 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode);
919 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
920                struct dt_object *obj, const char *name,
921                struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode);
922 void lfsck_unlock(struct lfsck_lock_handle *llh);
923 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
924                               struct lfsck_instance *lfsck,
925                               const struct lu_fid *fid);
926 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck);
927 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
928                                            bool unlink);
929 struct lfsck_component *lfsck_component_find(struct lfsck_instance *lfsck,
930                                              __u16 type);
931 void lfsck_component_cleanup(const struct lu_env *env,
932                              struct lfsck_component *com);
933 void lfsck_instance_cleanup(const struct lu_env *env,
934                             struct lfsck_instance *lfsck);
935 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
936                      const char *prefix);
937 void lfsck_time_dump(struct seq_file *m, time64_t time, const char *name);
938 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
939                     const char *prefix);
940 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
941                     struct lfsck_position *pos, bool init);
942 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit);
943 void lfsck_control_speed(struct lfsck_instance *lfsck);
944 void lfsck_control_speed_by_self(struct lfsck_component *com);
945 void lfsck_thread_args_fini(struct lfsck_thread_args *lta);
946 struct lfsck_assistant_data *
947 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
948                           const char *name);
949 struct lfsck_assistant_object *
950 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
951                             const struct lu_attr *attr, __u64 cookie,
952                             bool is_dir);
953 struct dt_object *
954 lfsck_assistant_object_load(const struct lu_env *env,
955                             struct lfsck_instance *lfsck,
956                             struct lfsck_assistant_object *lso);
957 int lfsck_async_interpret_common(const struct lu_env *env,
958                                  struct ptlrpc_request *req,
959                                  void *args, int rc);
960 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
961                         struct lfsck_request *lr,
962                         struct ptlrpc_request_set *set,
963                         ptlrpc_interpterer_t interpterer,
964                         void *args, int request);
965 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com);
966 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
967                           struct lfsck_start_param *lsp);
968 int lfsck_checkpoint_generic(const struct lu_env *env,
969                              struct lfsck_component *com);
970 void lfsck_post_generic(const struct lu_env *env,
971                         struct lfsck_component *com, int *result);
972 int lfsck_double_scan_generic(const struct lu_env *env,
973                               struct lfsck_component *com, int status);
974 void lfsck_quit_generic(const struct lu_env *env,
975                         struct lfsck_component *com);
976 int lfsck_load_one_trace_file(const struct lu_env *env,
977                               struct lfsck_component *com,
978                               struct dt_object *parent,
979                               struct dt_object **child,
980                               const struct dt_index_features *ft,
981                               const char *name, bool reset);
982 int lfsck_load_sub_trace_files(const struct lu_env *env,
983                                struct lfsck_component *com,
984                                const struct dt_index_features *ft,
985                                const char *prefix, bool reset);
986
987 /* lfsck_engine.c */
988 int lfsck_unpack_ent(struct lu_dirent *ent, __u64 *cookie, __u16 *type);
989 void lfsck_close_dir(const struct lu_env *env,
990                      struct lfsck_instance *lfsck, int result);
991 int lfsck_open_dir(const struct lu_env *env,
992                    struct lfsck_instance *lfsck, __u64 cookie);
993 int lfsck_master_engine(void *args);
994 int lfsck_assistant_engine(void *args);
995
996 /* lfsck_bookmark.c */
997 void lfsck_bookmark_cpu_to_le(struct lfsck_bookmark *des,
998                               struct lfsck_bookmark *src);
999 int lfsck_bookmark_store(const struct lu_env *env,
1000                          struct lfsck_instance *lfsck);
1001 int lfsck_bookmark_setup(const struct lu_env *env,
1002                          struct lfsck_instance *lfsck);
1003 int lfsck_set_param(const struct lu_env *env, struct lfsck_instance *lfsck,
1004                     struct lfsck_start *start, bool reset);
1005
1006 /* lfsck_namespace.c */
1007 int lfsck_namespace_trace_update(const struct lu_env *env,
1008                                  struct lfsck_component *com,
1009                                  const struct lu_fid *fid,
1010                                  const __u8 flags, bool add);
1011 int lfsck_namespace_check_exist(const struct lu_env *env,
1012                                 struct dt_object *dir,
1013                                 struct dt_object *obj, const char *name);
1014 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
1015                        struct linkea_data *ldata, bool with_rec);
1016 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1017                                    struct lfsck_component *com,
1018                                    struct dt_object *obj,
1019                                    struct linkea_data *ldata);
1020 int lfsck_namespace_repair_dangling(const struct lu_env *env,
1021                                     struct lfsck_component *com,
1022                                     struct dt_object *parent,
1023                                     struct dt_object *child,
1024                                     struct lfsck_namespace_req *lnr);
1025 int lfsck_namespace_repair_dirent(const struct lu_env *env,
1026                                   struct lfsck_component *com,
1027                                   struct dt_object *parent,
1028                                   struct dt_object *child,
1029                                   const char *name, const char *name2,
1030                                   __u16 type, bool update, bool dec);
1031 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
1032                         const struct lu_name *cname, const struct lu_fid *pfid);
1033 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
1034                           char *name, struct lu_fid *pfid);
1035 int lfsck_update_name_entry(const struct lu_env *env,
1036                             struct lfsck_instance *lfsck,
1037                             struct dt_object *dir, const char *name,
1038                             const struct lu_fid *fid, __u32 type);
1039 int lfsck_namespace_setup(const struct lu_env *env,
1040                           struct lfsck_instance *lfsck);
1041
1042 /* lfsck_striped_dir.c */
1043 void lfsck_lmv_put(const struct lu_env *env, struct lfsck_lmv *llmv);
1044 int lfsck_read_stripe_lmv(const struct lu_env *env, struct dt_object *obj,
1045                           struct lmv_mds_md_v1 *lmv);
1046 int lfsck_shard_name_to_index(const struct lu_env *env, const char *name,
1047                               int namelen, __u16 type,
1048                               const struct lu_fid *fid);
1049 bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
1050                                      struct lfsck_lmv *llmv,
1051                                      const char *name, int namelen);
1052 int lfsck_namespace_check_name(const struct lu_env *env,
1053                                struct dt_object *parent,
1054                                struct dt_object *child,
1055                                const struct lu_name *cname);
1056 int lfsck_namespace_update_lmv(const struct lu_env *env,
1057                                struct lfsck_component *com,
1058                                struct dt_object *obj,
1059                                struct lmv_mds_md_v1 *lmv, bool locked);
1060 int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
1061                                         struct lfsck_component *com,
1062                                         struct dt_object *obj,
1063                                         struct lfsck_lmv *llmv);
1064 int lfsck_namespace_scan_shard(const struct lu_env *env,
1065                                struct lfsck_component *com,
1066                                struct dt_object *child);
1067 int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
1068                                             struct lfsck_component *com,
1069                                             struct dt_object *obj);
1070 int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
1071                                          struct lfsck_component *com,
1072                                          struct dt_object *shard,
1073                                          struct lfsck_lmv *llmv,
1074                                          const char *name);
1075 int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
1076                                        struct lfsck_component *com,
1077                                        struct lfsck_namespace_req *lnr);
1078 int lfsck_namespace_handle_striped_master(const struct lu_env *env,
1079                                           struct lfsck_component *com,
1080                                           struct lfsck_namespace_req *lnr);
1081
1082 /* lfsck_layout.c */
1083 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
1084
1085 extern const char dot[];
1086 extern const char dotdot[];
1087 extern const char *lfsck_flags_names[];
1088 extern const char *lfsck_param_names[];
1089 extern struct lu_context_key lfsck_thread_key;
1090
1091 static inline bool name_is_dot_or_dotdot(const char *name, int namelen)
1092 {
1093         return name[0] == '.' &&
1094                (namelen == 1 || (namelen == 2 && name[1] == '.'));
1095 }
1096
1097 static inline struct dt_device *lfsck_obj2dev(struct dt_object *obj)
1098 {
1099         return container_of0(obj->do_lu.lo_dev, struct dt_device, dd_lu_dev);
1100 }
1101
1102 static inline struct lfsck_thread_info *
1103 lfsck_env_info(const struct lu_env *env)
1104 {
1105         struct lfsck_thread_info *info;
1106
1107         info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
1108         LASSERT(info != NULL);
1109         return info;
1110 }
1111
1112 static inline const struct lu_name *
1113 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
1114 {
1115         struct lu_name *lname;
1116
1117         lname = &lfsck_env_info(env)->lti_name_const;
1118         lname->ln_name = area;
1119         lname->ln_namelen = len;
1120         return lname;
1121 }
1122
1123 static inline void
1124 lfsck_buf_init(struct lu_buf *buf, void *area, ssize_t len)
1125 {
1126         buf->lb_buf = area;
1127         buf->lb_len = len;
1128 }
1129
1130 static inline struct lu_buf *
1131 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
1132 {
1133         struct lu_buf *buf;
1134
1135         buf = &lfsck_env_info(env)->lti_buf;
1136         buf->lb_buf = area;
1137         buf->lb_len = len;
1138         return buf;
1139 }
1140
1141 static inline const struct lu_buf *
1142 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
1143 {
1144         struct lu_buf *buf;
1145
1146         buf = &lfsck_env_info(env)->lti_buf;
1147         buf->lb_buf = (void *)area;
1148         buf->lb_len = len;
1149         return buf;
1150 }
1151
1152 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
1153 {
1154         return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
1155 }
1156
1157 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
1158 {
1159         return lu_object_fid(&obj->do_lu);
1160 }
1161
1162 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
1163 {
1164         memset(pos, 0, sizeof(*pos));
1165 }
1166
1167 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
1168 {
1169         return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
1170 }
1171
1172 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
1173                                   const struct lfsck_position *pos2)
1174 {
1175         if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
1176                 return -1;
1177
1178         if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
1179                 return 1;
1180
1181         if (fid_is_zero(&pos1->lp_dir_parent) &&
1182             !fid_is_zero(&pos2->lp_dir_parent))
1183                 return -1;
1184
1185         if (!fid_is_zero(&pos1->lp_dir_parent) &&
1186             fid_is_zero(&pos2->lp_dir_parent))
1187                 return 1;
1188
1189         if (fid_is_zero(&pos1->lp_dir_parent) &&
1190             fid_is_zero(&pos2->lp_dir_parent))
1191                 return 0;
1192
1193         LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
1194
1195         if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
1196                 return -1;
1197
1198         if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
1199                 return 1;
1200
1201         return 0;
1202 }
1203
1204 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
1205                                             struct lfsck_position *src)
1206 {
1207         des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
1208         fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
1209         des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
1210 }
1211
1212 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
1213                                             struct lfsck_position *src)
1214 {
1215         des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
1216         fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
1217         des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
1218 }
1219
1220 static inline umode_t lfsck_object_type(const struct dt_object *obj)
1221 {
1222         return lu_object_attr(&obj->do_lu);
1223 }
1224
1225 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
1226 {
1227         return lu_object_is_dying(obj->do_lu.lo_header);
1228 }
1229
1230 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
1231 {
1232         lu_object_get(&obj->do_lu);
1233         return obj;
1234 }
1235
1236 static inline void lfsck_object_put(const struct lu_env *env,
1237                                     struct dt_object *obj)
1238 {
1239         dt_object_put(env, obj);
1240 }
1241
1242 static inline struct seq_server_site
1243 *lfsck_dev_site(struct lfsck_instance *lfsck)
1244 {
1245         return lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1246 }
1247
1248 static inline u32 lfsck_dev_idx(struct lfsck_instance *lfsck)
1249 {
1250         return lfsck_dev_site(lfsck)->ss_node_id;
1251 }
1252
1253 static inline struct dt_object *
1254 lfsck_object_find_by_dev_new(const struct lu_env *env, struct dt_device *dev,
1255                              const struct lu_fid *fid)
1256 {
1257         struct lu_object_conf   *conf = &lfsck_env_info(env)->lti_conf;
1258
1259         conf->loc_flags = LOC_F_NEW;
1260         return lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, conf));
1261 }
1262
1263 static inline struct dt_object *
1264 lfsck_object_find_by_dev(const struct lu_env *env, struct dt_device *dev,
1265                          const struct lu_fid *fid)
1266 {
1267         return lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, NULL));
1268 }
1269
1270 static inline struct dt_device *
1271 lfsck_find_dev_by_fid(const struct lu_env *env, struct lfsck_instance *lfsck,
1272                       const struct lu_fid *fid)
1273 {
1274         struct dt_device *dev;
1275         int               idx;
1276
1277         if (!lfsck->li_master)
1278                 return lfsck->li_bottom;
1279
1280         idx = lfsck_find_mdt_idx_by_fid(env, lfsck, fid);
1281         if (idx < 0)
1282                 return ERR_PTR(idx);
1283
1284         if (idx == lfsck_dev_idx(lfsck)) {
1285                 dev = lfsck->li_bottom;
1286         } else {
1287                 struct lfsck_tgt_desc *ltd;
1288
1289                 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, idx);
1290                 if (unlikely(ltd == NULL))
1291                         return ERR_PTR(-ENODEV);
1292
1293                 dev = ltd->ltd_tgt;
1294         }
1295
1296         return dev;
1297 }
1298
1299 static inline struct dt_object *
1300 lfsck_object_find_bottom(const struct lu_env *env, struct lfsck_instance *lfsck,
1301                          const struct lu_fid *fid)
1302 {
1303         struct dt_device *dev;
1304
1305         dev = lfsck_find_dev_by_fid(env, lfsck, fid);
1306         if (IS_ERR(dev))
1307                 return (struct dt_object *)dev;
1308
1309         return lfsck_object_find_by_dev(env, dev, fid);
1310 }
1311
1312 static inline struct dt_object *
1313 lfsck_object_find_bottom_new(const struct lu_env *env,
1314                              struct lfsck_instance *lfsck,
1315                              const struct lu_fid *fid)
1316 {
1317         struct dt_device *dev;
1318
1319         dev = lfsck_find_dev_by_fid(env, lfsck, fid);
1320         if (IS_ERR(dev))
1321                 return (struct dt_object *)dev;
1322
1323         return lfsck_object_find_by_dev_new(env, dev, fid);
1324 }
1325
1326 static inline struct dt_object *
1327 lfsck_object_locate(struct dt_device *dev, struct dt_object *obj)
1328 {
1329         if (lfsck_obj2dev(obj) == dev) {
1330                 return obj;
1331         } else {
1332                 struct lu_object *lo;
1333
1334                 lo = lu_object_locate(obj->do_lu.lo_header,
1335                                       dev->dd_lu_dev.ld_type);
1336                 if (unlikely(lo == NULL))
1337                         return ERR_PTR(-ENOENT);
1338
1339                 return lu2dt(lo);
1340         }
1341 }
1342
1343 static inline struct lfsck_tgt_desc *lfsck_tgt_get(struct lfsck_tgt_descs *ltds,
1344                                                    __u32 index)
1345 {
1346         struct lfsck_tgt_desc *ltd;
1347
1348         ltd = lfsck_ltd2tgt(ltds, index);
1349         if (ltd != NULL)
1350                 atomic_inc(&ltd->ltd_ref);
1351
1352         return ltd;
1353 }
1354
1355 static inline void lfsck_tgt_put(struct lfsck_tgt_desc *ltd)
1356 {
1357         if (atomic_dec_and_test(&ltd->ltd_ref))
1358                 OBD_FREE_PTR(ltd);
1359 }
1360
1361 static inline struct lfsck_component *
1362 lfsck_component_get(struct lfsck_component *com)
1363 {
1364         atomic_inc(&com->lc_ref);
1365
1366         return com;
1367 }
1368
1369 static inline void lfsck_component_put(const struct lu_env *env,
1370                                        struct lfsck_component *com)
1371 {
1372         if (atomic_dec_and_test(&com->lc_ref)) {
1373                 struct lfsck_sub_trace_obj *lsto;
1374                 int                         i;
1375
1376                 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
1377                      i < LFSCK_STF_COUNT; i++, lsto++) {
1378                         if (lsto->lsto_obj != NULL)
1379                                 lfsck_object_put(env, lsto->lsto_obj);
1380                 }
1381
1382                 if (com->lc_obj != NULL)
1383                         lfsck_object_put(env, com->lc_obj);
1384                 if (com->lc_file_ram != NULL)
1385                         OBD_FREE(com->lc_file_ram, com->lc_file_size);
1386                 if (com->lc_file_disk != NULL)
1387                         OBD_FREE(com->lc_file_disk, com->lc_file_size);
1388                 if (com->lc_data != NULL) {
1389                         LASSERT(com->lc_ops->lfsck_data_release != NULL);
1390
1391                         com->lc_ops->lfsck_data_release(env, com);
1392                 }
1393
1394                 OBD_FREE_PTR(com);
1395         }
1396 }
1397
1398 static inline struct lfsck_instance *
1399 lfsck_instance_get(struct lfsck_instance *lfsck)
1400 {
1401         atomic_inc(&lfsck->li_ref);
1402
1403         return lfsck;
1404 }
1405
1406 static inline void lfsck_instance_put(const struct lu_env *env,
1407                                       struct lfsck_instance *lfsck)
1408 {
1409         if (atomic_dec_and_test(&lfsck->li_ref))
1410                 lfsck_instance_cleanup(env, lfsck);
1411 }
1412
1413 static inline bool lfsck_phase2_next_ready(struct lfsck_assistant_data *lad)
1414 {
1415         return list_empty(&lad->lad_mdt_phase1_list) &&
1416                (!list_empty(&lad->lad_ost_phase2_list) ||
1417                 list_empty(&lad->lad_ost_phase1_list));
1418 }
1419
1420 static inline void lfsck_lad_set_bitmap(const struct lu_env *env,
1421                                         struct lfsck_component *com,
1422                                         __u32 index)
1423 {
1424         struct lfsck_assistant_data     *lad    = com->lc_data;
1425         struct cfs_bitmap               *bitmap = lad->lad_bitmap;
1426
1427         LASSERT(com->lc_lfsck->li_master);
1428         LASSERT(bitmap != NULL);
1429
1430         if (likely(bitmap->size > index)) {
1431                 cfs_bitmap_set(bitmap, index);
1432                 lad->lad_incomplete = 1;
1433         } else if (com->lc_type == LFSCK_TYPE_NAMESPACE) {
1434                 struct lfsck_namespace *ns = com->lc_file_ram;
1435
1436                 ns->ln_flags |= LF_INCOMPLETE;
1437         }
1438
1439         CDEBUG(D_LFSCK, "%s: %s LFSCK set bitmap (%p/%u) for idx %u\n",
1440                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, bitmap,
1441                bitmap->size, index);
1442 }
1443
1444 static inline int lfsck_links_read(const struct lu_env *env,
1445                                    struct dt_object *obj,
1446                                    struct linkea_data *ldata)
1447 {
1448         ldata->ld_buf =
1449                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
1450                                        MAX_LINKEA_SIZE);
1451
1452         return __lfsck_links_read(env, obj, ldata, false);
1453 }
1454
1455 /* Read linkEA for the given object, the linkEA should contain
1456  * at least one entry, otherwise, -ENODATA will be returned. */
1457 static inline int lfsck_links_read_with_rec(const struct lu_env *env,
1458                                             struct dt_object *obj,
1459                                             struct linkea_data *ldata)
1460 {
1461         ldata->ld_buf =
1462                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
1463                                        MAX_LINKEA_SIZE);
1464
1465         return __lfsck_links_read(env, obj, ldata, true);
1466 }
1467
1468 static inline int lfsck_links_read2_with_rec(const struct lu_env *env,
1469                                              struct dt_object *obj,
1470                                              struct linkea_data *ldata)
1471 {
1472         ldata->ld_buf =
1473                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf2,
1474                                        MAX_LINKEA_SIZE);
1475
1476         return __lfsck_links_read(env, obj, ldata, true);
1477 }
1478
1479 static inline struct lfsck_lmv *lfsck_lmv_get(struct lfsck_lmv *llmv)
1480 {
1481         if (llmv != NULL)
1482                 atomic_inc(&llmv->ll_ref);
1483
1484         return llmv;
1485 }
1486
1487 static inline int lfsck_sub_trace_file_fid2idx(const struct lu_fid *fid)
1488 {
1489         return fid->f_oid & (LFSCK_STF_COUNT - 1);
1490 }
1491
1492 static inline void lfsck_lmv_header_le_to_cpu(struct lmv_mds_md_v1 *dst,
1493                                               const struct lmv_mds_md_v1 *src)
1494 {
1495         dst->lmv_magic = le32_to_cpu(src->lmv_magic);
1496         dst->lmv_stripe_count = le32_to_cpu(src->lmv_stripe_count);
1497         dst->lmv_master_mdt_index = le32_to_cpu(src->lmv_master_mdt_index);
1498         dst->lmv_hash_type = le32_to_cpu(src->lmv_hash_type);
1499         dst->lmv_layout_version = le32_to_cpu(src->lmv_layout_version);
1500 }
1501
1502 static inline void lfsck_lmv_header_cpu_to_le(struct lmv_mds_md_v1 *dst,
1503                                               const struct lmv_mds_md_v1 *src)
1504 {
1505         dst->lmv_magic = cpu_to_le32(src->lmv_magic);
1506         dst->lmv_stripe_count = cpu_to_le32(src->lmv_stripe_count);
1507         dst->lmv_master_mdt_index = cpu_to_le32(src->lmv_master_mdt_index);
1508         dst->lmv_hash_type = cpu_to_le32(src->lmv_hash_type);
1509         dst->lmv_layout_version = cpu_to_le32(src->lmv_layout_version);
1510 }
1511
1512 static inline struct lfsck_assistant_object *
1513 lfsck_assistant_object_get(struct lfsck_assistant_object *lso)
1514 {
1515         atomic_inc(&lso->lso_ref);
1516
1517         return lso;
1518 }
1519
1520 static inline void
1521 lfsck_assistant_object_put(const struct lu_env *env,
1522                            struct lfsck_assistant_object *lso)
1523 {
1524         if (atomic_dec_and_test(&lso->lso_ref))
1525                 OBD_FREE_PTR(lso);
1526 }
1527 #endif /* _LFSCK_INTERNAL_H */