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