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