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