Whamcloud - gitweb
LU-4788 lfsck: enable verification for remote object
[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, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_internal.h
27  *
28  * Shared definitions and declarations for the LFSCK.
29  *
30  * Author: Fan, Yong <fan.yong@intel.com>
31  */
32
33 #ifndef _LFSCK_INTERNAL_H
34 # define _LFSCK_INTERNAL_H
35
36 #include <lustre/lustre_lfsck_user.h>
37 #include <lustre/lustre_user.h>
38 #include <lustre/lustre_idl.h>
39 #include <lustre_lfsck.h>
40 #include <obd.h>
41 #include <lu_object.h>
42 #include <dt_object.h>
43 #include <md_object.h>
44 #include <lustre_net.h>
45 #include <lustre_dlm.h>
46 #include <lustre_fid.h>
47 #include <md_object.h>
48 #include <lustre_linkea.h>
49
50 #define HALF_SEC                        (HZ >> 1)
51 #define LFSCK_CHECKPOINT_INTERVAL       60
52
53 enum lfsck_flags {
54         /* Finish the first cycle scanning. */
55         LF_SCANNED_ONCE         = 0x00000001ULL,
56
57         /* There is some namespace inconsistency. */
58         LF_INCONSISTENT         = 0x00000002ULL,
59
60         /* The device is upgraded from 1.8 format. */
61         LF_UPGRADE              = 0x00000004ULL,
62
63         /* The server ever restarted during the LFSCK, and may miss to process
64          * some objects check/repair. */
65         LF_INCOMPLETE           = 0x00000008ULL,
66
67         /* The LAST_ID (file) crashed. */
68         LF_CRASHED_LASTID       = 0x00000010ULL,
69 };
70
71 struct lfsck_position {
72         /* low layer object table-based iteration position. */
73         __u64   lp_oit_cookie;
74
75         /* parent FID for directory traversal. */
76         struct lu_fid lp_dir_parent;
77
78         /* namespace-based directory traversal position. */
79         __u64   lp_dir_cookie;
80 };
81
82 struct lfsck_bookmark {
83         /* Magic number to detect that this struct contains valid data. */
84         __u32   lb_magic;
85
86         /* For compatible with old versions. */
87         __u16   lb_version;
88
89         /* See 'enum lfsck_param_flags' */
90         __u16   lb_param;
91
92         /* How many items can be scanned at most per second. */
93         __u32   lb_speed_limit;
94
95         /* The windows size for async requests pipeline. */
96         __u16   lb_async_windows;
97
98         /* For 64-bits aligned. */
99         __u16   lb_padding;
100
101         /* The FID for .lustre/lost+found/MDTxxxx */
102         struct lu_fid   lb_lpf_fid;
103
104         /* The FID for the last MDT-object created by the LFSCK repairing. */
105         struct lu_fid   lb_last_fid;
106
107         /* For future using. */
108         __u64   lb_reserved[2];
109 };
110
111 enum lfsck_namespace_trace_flags {
112         LNTF_CHECK_LINKEA       = 0x01,
113         LNTF_CHECK_PARENT       = 0x02,
114         LNTF_ALL                = 0xff
115 };
116
117 struct lfsck_namespace {
118         /* Magic number to detect that this struct contains valid data. */
119         __u32   ln_magic;
120
121         /* See 'enum lfsck_status'. */
122         __u32   ln_status;
123
124         /* See 'enum lfsck_flags'. */
125         __u32   ln_flags;
126
127         /* How many completed LFSCK runs on the device. */
128         __u32   ln_success_count;
129
130         /*  How long the LFSCK phase1 has run in seconds. */
131         __u32   ln_run_time_phase1;
132
133         /*  How long the LFSCK phase2 has run in seconds. */
134         __u32   ln_run_time_phase2;
135
136         /* Time for the last LFSCK completed in seconds since epoch. */
137         __u64   ln_time_last_complete;
138
139         /* Time for the latest LFSCK ran in seconds since epoch. */
140         __u64   ln_time_latest_start;
141
142         /* Time for the last LFSCK checkpoint in seconds since epoch. */
143         __u64   ln_time_last_checkpoint;
144
145         /* Position for the latest LFSCK started from. */
146         struct lfsck_position   ln_pos_latest_start;
147
148         /* Position for the last LFSCK checkpoint. */
149         struct lfsck_position   ln_pos_last_checkpoint;
150
151         /* Position for the first should be updated object. */
152         struct lfsck_position   ln_pos_first_inconsistent;
153
154         /* How many items (including dir) have been checked. */
155         __u64   ln_items_checked;
156
157         /* How many items have been repaired. */
158         __u64   ln_items_repaired;
159
160         /* How many items failed to be processed. */
161         __u64   ln_items_failed;
162
163         /* How many directories have been traversed. */
164         __u64   ln_dirs_checked;
165
166         /* How many objects have been double scanned. */
167         __u64   ln_objs_checked_phase2;
168
169         /* How many objects have been reparied during double scan. */
170         __u64   ln_objs_repaired_phase2;
171
172         /* How many objects failed to be processed during double scan. */
173         __u64   ln_objs_failed_phase2;
174
175         /* How many objects with nlink fixed. */
176         __u64   ln_objs_nlink_repaired;
177
178         /* How many objects were lost before, but found back now. */
179         __u64   ln_objs_lost_found;
180
181         /* The latest object has been processed (failed) during double scan. */
182         struct lu_fid   ln_fid_latest_scanned_phase2;
183
184         /* How many FID-in-dirent entries have been repaired. */
185         __u64   ln_dirent_repaired;
186
187         /* How many linkEA entries have been repaired. */
188         __u64   ln_linkea_repaired;
189
190         /* How many multiple-linked objects have been checked. */
191         __u64   ln_mul_linked_checked;
192
193         /* How many multiple-linked objects have been repaired. */
194         __u64   ln_mul_linked_repaired;
195
196         /* For further using. 256-bytes aligned now. */
197         __u64   ln_reserved[31];
198 };
199
200 enum lfsck_layout_inconsistency_type {
201         LLIT_NONE                       = 0,
202         LLIT_DANGLING                   = 1,
203         LLIT_UNMATCHED_PAIR             = 2,
204         LLIT_MULTIPLE_REFERENCED        = 3,
205         LLIT_ORPHAN                     = 4,
206         LLIT_INCONSISTENT_OWNER         = 5,
207         LLIT_OTHERS                     = 6,
208         LLIT_MAX                        = LLIT_OTHERS
209 };
210
211 struct lfsck_layout {
212         /* Magic number to detect that this struct contains valid data. */
213         __u32   ll_magic;
214
215         /* See 'enum lfsck_status'. */
216         __u32   ll_status;
217
218         /* See 'enum lfsck_flags'. */
219         __u32   ll_flags;
220
221         /* How many completed LFSCK runs on the device. */
222         __u32   ll_success_count;
223
224         /*  How long the LFSCK phase1 has run in seconds. */
225         __u32   ll_run_time_phase1;
226
227         /*  How long the LFSCK phase2 has run in seconds. */
228         __u32   ll_run_time_phase2;
229
230         /* Time for the last LFSCK completed in seconds since epoch. */
231         __u64   ll_time_last_complete;
232
233         /* Time for the latest LFSCK ran in seconds since epoch. */
234         __u64   ll_time_latest_start;
235
236         /* Time for the last LFSCK checkpoint in seconds since epoch. */
237         __u64   ll_time_last_checkpoint;
238
239         /* Position for the latest LFSCK started from. */
240         __u64   ll_pos_latest_start;
241
242         /* Position for the last LFSCK checkpoint. */
243         __u64   ll_pos_last_checkpoint;
244
245         /* Position for the first object to be fixed or
246          * failed to be checked in the phase1. */
247         __u64   ll_pos_first_inconsistent;
248
249         /* How many objects have been checked. */
250         __u64   ll_objs_checked_phase1;
251
252         /* How many objects failed to be processed. */
253         __u64   ll_objs_failed_phase1;
254
255         /* How many objects have been double scanned. */
256         __u64   ll_objs_checked_phase2;
257
258         /* How many objects failed to be processed during double scan. */
259         __u64   ll_objs_failed_phase2;
260
261         /* kinds of inconsistency have been or to be repaired.
262          * ll_objs_repaired[type - 1] is the count for the given @type. */
263         __u64   ll_objs_repaired[LLIT_MAX];
264
265         /* How many objects have been skipped because of related
266          * MDT(s)/OST(s) do not participate in the LFSCK */
267         __u64   ll_objs_skipped;
268
269         /* The size of ll_ost_bitmap with nbits. */
270         __u32   ll_bitmap_size;
271
272         /* For further using. 256-bytes aligned now. */
273         __u32   ll_reserved_1;
274         __u64   ll_reserved_2[11];
275
276         /* The OST targets bitmap to record the OSTs that contain
277          * non-verified OST-objects. */
278         __u8    ll_ost_bitmap[0];
279 };
280
281 struct lfsck_component;
282 struct lfsck_tgt_descs;
283 struct lfsck_tgt_desc;
284
285 struct lfsck_operations {
286         int (*lfsck_reset)(const struct lu_env *env,
287                            struct lfsck_component *com,
288                            bool init);
289
290         void (*lfsck_fail)(const struct lu_env *env,
291                            struct lfsck_component *com,
292                            bool new_checked);
293
294         int (*lfsck_checkpoint)(const struct lu_env *env,
295                                 struct lfsck_component *com,
296                                 bool init);
297
298         int (*lfsck_prep)(const struct lu_env *env,
299                           struct lfsck_component *com,
300                           struct lfsck_start_param *lsp);
301
302         int (*lfsck_exec_oit)(const struct lu_env *env,
303                               struct lfsck_component *com,
304                               struct dt_object *obj);
305
306         int (*lfsck_exec_dir)(const struct lu_env *env,
307                               struct lfsck_component *com,
308                               struct lu_dirent *ent,
309                               __u16 type);
310
311         int (*lfsck_post)(const struct lu_env *env,
312                           struct lfsck_component *com,
313                           int result,
314                           bool init);
315
316         int (*lfsck_dump)(const struct lu_env *env,
317                           struct lfsck_component *com,
318                           struct seq_file *m);
319
320         int (*lfsck_double_scan)(const struct lu_env *env,
321                                  struct lfsck_component *com);
322
323         void (*lfsck_data_release)(const struct lu_env *env,
324                                    struct lfsck_component *com);
325
326         void (*lfsck_quit)(const struct lu_env *env,
327                            struct lfsck_component *com);
328
329         int (*lfsck_in_notify)(const struct lu_env *env,
330                                struct lfsck_component *com,
331                                struct lfsck_request *lr);
332
333         int (*lfsck_query)(const struct lu_env *env,
334                            struct lfsck_component *com);
335
336         int (*lfsck_join)(const struct lu_env *env,
337                           struct lfsck_component *com,
338                           struct lfsck_start_param *lsp);
339 };
340
341 #define TGT_PTRS                256     /* number of pointers at 1st level */
342 #define TGT_PTRS_PER_BLOCK      256     /* number of pointers at 2nd level */
343
344 struct lfsck_tgt_desc {
345         struct list_head   ltd_orphan_list;
346         struct dt_device  *ltd_tgt;
347         struct dt_device  *ltd_key;
348         struct obd_export *ltd_exp;
349         struct list_head   ltd_layout_list;
350         struct list_head   ltd_layout_phase_list;
351         struct list_head   ltd_namespace_list;
352         struct list_head   ltd_namespace_phase_list;
353         atomic_t           ltd_ref;
354         __u32              ltd_index;
355         __u32              ltd_layout_gen;
356         __u32              ltd_namespace_gen;
357         unsigned int       ltd_dead:1,
358                            ltd_layout_done:1,
359                            ltd_namespace_done:1;
360 };
361
362 struct lfsck_tgt_desc_idx {
363         struct lfsck_tgt_desc *ldi_tgts[TGT_PTRS_PER_BLOCK];
364 };
365
366 struct lfsck_tgt_descs {
367         /* list of known TGTs */
368         struct lfsck_tgt_desc_idx       *ltd_tgts_idx[TGT_PTRS];
369
370         /* bitmap of TGTs available */
371         cfs_bitmap_t                    *ltd_tgts_bitmap;
372
373         /* for lfsck_tgt_desc::ltd_xxx_list */
374         spinlock_t                       ltd_lock;
375
376         /* for tgts table accessing and changes */
377         struct rw_semaphore              ltd_rw_sem;
378
379         /* Temporary list for orphan targets. */
380         struct list_head                 ltd_orphan;
381
382         /* number of registered TGTs */
383         __u32                            ltd_tgtnr;
384 };
385
386 #define LTD_TGT(ltd, index)     \
387         ((ltd)->ltd_tgts_idx[(index) / TGT_PTRS_PER_BLOCK]->\
388          ldi_tgts[(index) % TGT_PTRS_PER_BLOCK])
389
390 #define OST_TGT(lfsck, index)   LTD_TGT(&lfsck->li_ost_descs, index)
391 #define MDT_TGT(lfsck, index)   LTD_TGT(&lfsck->li_mdt_descs, index)
392
393 struct lfsck_component {
394         /* into lfsck_instance::li_list_(scan,double_scan,idle} */
395         struct list_head         lc_link;
396
397         /* into lfsck_instance::li_list_dir */
398         struct list_head         lc_link_dir;
399
400         struct rw_semaphore      lc_sem;
401         atomic_t                 lc_ref;
402
403         struct lfsck_position    lc_pos_start;
404         struct lfsck_instance   *lc_lfsck;
405         struct dt_object        *lc_obj;
406         struct lfsck_operations *lc_ops;
407         void                    *lc_file_ram;
408         void                    *lc_file_disk;
409         void                    *lc_data;
410         struct lu_fid            lc_fid_latest_scanned_phase2;
411
412         /* The time for last checkpoint, jiffies */
413         cfs_time_t               lc_time_last_checkpoint;
414
415         /* The time for next checkpoint, jiffies */
416         cfs_time_t               lc_time_next_checkpoint;
417
418         __u32                    lc_file_size;
419
420         /* How many objects have been checked since last checkpoint. */
421         __u32                    lc_new_checked;
422
423         /* How many objects have been scanned since last sleep. */
424         __u32                    lc_new_scanned;
425
426         __u16                    lc_type;
427 };
428
429 struct lfsck_instance {
430         struct mutex              li_mutex;
431         spinlock_t                li_lock;
432
433         /* Link into the lfsck_instance_list. */
434         struct list_head          li_link;
435
436         /* For the components in (first) scanning via otable-based iteration. */
437         struct list_head          li_list_scan;
438
439         /* For the components in scanning via directory traversal. Because
440          * directory traversal cannot guarantee all the object be scanned,
441          * so the component in the li_list_dir must be in li_list_scan. */
442         struct list_head          li_list_dir;
443
444         /* For the components in double scanning. */
445         struct list_head          li_list_double_scan;
446
447         /* For the components those are not scanning now. */
448         struct list_head          li_list_idle;
449
450         atomic_t                  li_ref;
451         atomic_t                  li_double_scan_count;
452         struct ptlrpc_thread      li_thread;
453
454         /* The time for last checkpoint, jiffies */
455         cfs_time_t                li_time_last_checkpoint;
456
457         /* The time for next checkpoint, jiffies */
458         cfs_time_t                li_time_next_checkpoint;
459
460         lfsck_out_notify          li_out_notify;
461         void                     *li_out_notify_data;
462         struct dt_device         *li_next;
463         struct dt_device         *li_bottom;
464         struct obd_device        *li_obd;
465         struct ldlm_namespace    *li_namespace;
466         struct local_oid_storage *li_los;
467         struct lu_fid             li_local_root_fid;  /* backend root "/" */
468         struct lu_fid             li_global_root_fid; /* /ROOT */
469         struct dt_object         *li_bookmark_obj;
470         struct dt_object         *li_lpf_obj;
471         struct lu_client_seq     *li_seq;
472         struct lfsck_bookmark     li_bookmark_ram;
473         struct lfsck_bookmark     li_bookmark_disk;
474         struct lfsck_position     li_pos_current;
475         struct lfsck_position     li_pos_checkpoint;
476
477         /* Obj for otable-based iteration */
478         struct dt_object         *li_obj_oit;
479
480         /* Obj for directory traversal */
481         struct dt_object         *li_obj_dir;
482
483         /* It for otable-based iteration */
484         struct dt_it             *li_di_oit;
485
486         /* It for directory traversal */
487         struct dt_it             *li_di_dir;
488
489         /* Description of OST */
490         struct lfsck_tgt_descs    li_ost_descs;
491
492         /* Description of MDT */
493         struct lfsck_tgt_descs    li_mdt_descs;
494
495         /* namespace-based directory traversal position. */
496         __u64                     li_cookie_dir;
497
498         /* Arguments for low layer otable-based iteration. */
499         __u32                     li_args_oit;
500
501         /* Arugments for namespace-based directory traversal. */
502         __u32                     li_args_dir;
503
504         /* Schedule for every N objects. */
505         __u32                     li_sleep_rate;
506
507         /* Sleep N jiffies for each schedule. */
508         __u32                     li_sleep_jif;
509
510         /* How many objects have been scanned since last sleep. */
511         __u32                     li_new_scanned;
512
513         /* The status when the LFSCK stopped or paused. */
514         __u32                     li_status;
515
516         /* The flags when the lFSCK stopped or paused. */
517         __u32                     li_flags;
518
519         unsigned int              li_oit_over:1, /* oit is finished. */
520                                   li_drop_dryrun:1, /* Ever dryrun, not now. */
521                                   li_master:1, /* Master instance or not. */
522                                   li_current_oit_processed:1,
523                                   li_start_unplug:1;
524 };
525
526 struct lfsck_async_interpret_args {
527         struct lfsck_component          *laia_com;
528         struct lfsck_tgt_descs          *laia_ltds;
529         struct lfsck_tgt_desc           *laia_ltd;
530         struct lfsck_request            *laia_lr;
531         atomic_t                        *laia_count;
532         int                              laia_result;
533         unsigned int                     laia_shared:1;
534 };
535
536 struct lfsck_thread_args {
537         struct lu_env                    lta_env;
538         struct lfsck_instance           *lta_lfsck;
539         struct lfsck_component          *lta_com;
540         struct lfsck_start_param        *lta_lsp;
541 };
542
543 struct lfsck_assistant_req {
544         struct list_head        lar_list;
545 };
546
547 struct lfsck_assistant_operations {
548         int (*la_handler_p1)(const struct lu_env *env,
549                              struct lfsck_component *com,
550                              struct lfsck_assistant_req *lar);
551
552         int (*la_handler_p2)(const struct lu_env *env,
553                              struct lfsck_component *com);
554
555         void (*la_fill_pos)(const struct lu_env *env,
556                             struct lfsck_component *com,
557                             struct lfsck_position *pos);
558
559         int (*la_double_scan_result)(const struct lu_env *env,
560                                      struct lfsck_component *com,
561                                      int rc);
562
563         void (*la_req_fini)(const struct lu_env *env,
564                             struct lfsck_assistant_req *lar);
565
566         void (*la_sync_failures)(const struct lu_env *env,
567                                  struct lfsck_component *com,
568                                  struct lfsck_request *lr);
569 };
570
571 struct lfsck_assistant_data {
572         spinlock_t                               lad_lock;
573         struct list_head                         lad_req_list;
574
575         /* list for the ost targets involve LFSCK. */
576         struct list_head                         lad_ost_list;
577
578         /* list for the ost targets in phase1 scanning. */
579         struct list_head                         lad_ost_phase1_list;
580
581         /* list for the ost targets in phase1 scanning. */
582         struct list_head                         lad_ost_phase2_list;
583
584         /* list for the mdt targets involve LFSCK. */
585         struct list_head                         lad_mdt_list;
586
587         /* list for the mdt targets in phase1 scanning. */
588         struct list_head                         lad_mdt_phase1_list;
589
590         /* list for the mdt targets in phase1 scanning. */
591         struct list_head                         lad_mdt_phase2_list;
592
593         const char                              *lad_name;
594         struct ptlrpc_thread                     lad_thread;
595
596         struct lfsck_assistant_operations       *lad_ops;
597
598         cfs_bitmap_t                            *lad_bitmap;
599
600         __u32                                    lad_touch_gen;
601         int                                      lad_prefetched;
602         int                                      lad_assistant_status;
603         int                                      lad_post_result;
604         unsigned int                             lad_to_post:1,
605                                                  lad_to_double_scan:1,
606                                                  lad_in_double_scan:1,
607                                                  lad_exit:1,
608                                                  lad_incomplete:1;
609 };
610
611 #define LFSCK_TMPBUF_LEN        64
612
613 struct lfsck_thread_info {
614         struct lu_name          lti_name_const;
615         struct lu_name          lti_name;
616         struct lu_buf           lti_buf;
617         struct lu_buf           lti_linkea_buf;
618         struct lu_buf           lti_linkea_buf2;
619         struct lu_buf           lti_big_buf;
620         struct lu_fid           lti_fid;
621         struct lu_fid           lti_fid2;
622         struct lu_fid           lti_fid3;
623         struct lu_attr          lti_la;
624         struct lu_attr          lti_la2;
625         struct lu_attr          lti_la3;
626         struct ost_id           lti_oi;
627         union {
628                 struct lustre_mdt_attrs lti_lma;
629                 /* old LMA for compatibility */
630                 char                    lti_lma_old[LMA_OLD_SIZE];
631         };
632         struct dt_object_format lti_dof;
633         /* There will be '\0' at the end of the name. */
634         char            lti_key[sizeof(struct lu_dirent) + NAME_MAX + 1];
635         char                    lti_tmpbuf[LFSCK_TMPBUF_LEN];
636         struct lfsck_request    lti_lr;
637         struct lfsck_async_interpret_args lti_laia;
638         struct lfsck_async_interpret_args lti_laia2;
639         struct lfsck_start      lti_start;
640         struct lfsck_stop       lti_stop;
641         ldlm_policy_data_t      lti_policy;
642         struct ldlm_res_id      lti_resid;
643         union {
644                 struct filter_fid_old   lti_old_pfid;
645                 struct filter_fid       lti_new_pfid;
646         };
647         struct dt_allocation_hint lti_hint;
648         struct lu_orphan_rec    lti_rec;
649         struct lov_user_md      lti_lum;
650         struct dt_insert_rec    lti_dt_rec;
651         struct lu_object_conf   lti_conf;
652         struct lu_seq_range     lti_range;
653 };
654
655 /* lfsck_lib.c */
656 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
657                     struct lu_fid *fid, bool locked);
658 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
659                      struct dt_object *obj, struct lustre_handle *lh,
660                      __u64 bits, ldlm_mode_t mode);
661 void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode);
662 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
663                               struct lfsck_instance *lfsck,
664                               const struct lu_fid *fid);
665 int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck);
666 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck);
667 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
668                                            bool unlink);
669 struct lfsck_component *lfsck_component_find(struct lfsck_instance *lfsck,
670                                              __u16 type);
671 const char *lfsck_status2names(enum lfsck_status status);
672 void lfsck_component_cleanup(const struct lu_env *env,
673                              struct lfsck_component *com);
674 void lfsck_instance_cleanup(const struct lu_env *env,
675                             struct lfsck_instance *lfsck);
676 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
677                     const char *prefix);
678 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix);
679 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
680                    const char *prefix);
681 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
682                     struct lfsck_position *pos, bool init);
683 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit);
684 void lfsck_control_speed(struct lfsck_instance *lfsck);
685 void lfsck_control_speed_by_self(struct lfsck_component *com);
686 void lfsck_thread_args_fini(struct lfsck_thread_args *lta);
687 struct lfsck_assistant_data *
688 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
689                           const char *name);
690 int lfsck_async_interpret_common(const struct lu_env *env,
691                                  struct ptlrpc_request *req,
692                                  void *args, int rc);
693 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
694                         struct lfsck_request *lr,
695                         struct ptlrpc_request_set *set,
696                         ptlrpc_interpterer_t interpterer,
697                         void *args, int request);
698 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
699                           struct lfsck_start_param *lsp);
700 int lfsck_checkpoint_generic(const struct lu_env *env,
701                              struct lfsck_component *com);
702 void lfsck_post_generic(const struct lu_env *env,
703                         struct lfsck_component *com, int *result);
704 int lfsck_double_scan_generic(const struct lu_env *env,
705                               struct lfsck_component *com, int status);
706 void lfsck_quit_generic(const struct lu_env *env,
707                         struct lfsck_component *com);
708
709 /* lfsck_engine.c */
710 int lfsck_master_engine(void *args);
711 int lfsck_assistant_engine(void *args);
712
713 /* lfsck_bookmark.c */
714 void lfsck_bookmark_cpu_to_le(struct lfsck_bookmark *des,
715                               struct lfsck_bookmark *src);
716 int lfsck_bookmark_store(const struct lu_env *env,
717                          struct lfsck_instance *lfsck);
718 int lfsck_bookmark_setup(const struct lu_env *env,
719                          struct lfsck_instance *lfsck);
720 int lfsck_set_param(const struct lu_env *env, struct lfsck_instance *lfsck,
721                     struct lfsck_start *start, bool reset);
722
723 /* lfsck_namespace.c */
724 int lfsck_namespace_trace_update(const struct lu_env *env,
725                                  struct lfsck_component *com,
726                                  const struct lu_fid *fid,
727                                  const __u8 flags, bool add);
728 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
729                        struct linkea_data *ldata);
730 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
731                         struct dt_object *obj, const struct lu_name *cname,
732                         const struct lu_fid *pfid);
733 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
734                           char *name, struct lu_fid *pfid);
735 int lfsck_remove_name_entry(const struct lu_env *env,
736                             struct lfsck_instance *lfsck,
737                             struct dt_object *parent,
738                             const char *name, __u32 type);
739 int lfsck_update_name_entry(const struct lu_env *env,
740                             struct lfsck_instance *lfsck,
741                             struct dt_object *parent, const char *name,
742                             const struct lu_fid *pfid, __u32 type);
743 int lfsck_namespace_setup(const struct lu_env *env,
744                           struct lfsck_instance *lfsck);
745
746 /* lfsck_layout.c */
747 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
748
749 extern const char *lfsck_flags_names[];
750 extern const char *lfsck_param_names[];
751 extern struct lu_context_key lfsck_thread_key;
752
753 static inline struct dt_device *lfsck_obj2dt_dev(struct dt_object *obj)
754 {
755         return container_of0(obj->do_lu.lo_dev, struct dt_device, dd_lu_dev);
756 }
757
758 static inline struct lfsck_thread_info *
759 lfsck_env_info(const struct lu_env *env)
760 {
761         struct lfsck_thread_info *info;
762
763         info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
764         LASSERT(info != NULL);
765         return info;
766 }
767
768 static inline const struct lu_name *
769 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
770 {
771         struct lu_name *lname;
772
773         lname = &lfsck_env_info(env)->lti_name_const;
774         lname->ln_name = area;
775         lname->ln_namelen = len;
776         return lname;
777 }
778
779 static inline void
780 lfsck_buf_init(struct lu_buf *buf, void *area, ssize_t len)
781 {
782         buf->lb_buf = area;
783         buf->lb_len = len;
784 }
785
786 static inline struct lu_buf *
787 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
788 {
789         struct lu_buf *buf;
790
791         buf = &lfsck_env_info(env)->lti_buf;
792         buf->lb_buf = area;
793         buf->lb_len = len;
794         return buf;
795 }
796
797 static inline const struct lu_buf *
798 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
799 {
800         struct lu_buf *buf;
801
802         buf = &lfsck_env_info(env)->lti_buf;
803         buf->lb_buf = (void *)area;
804         buf->lb_len = len;
805         return buf;
806 }
807
808 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
809 {
810         return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
811 }
812
813 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
814 {
815         return lu_object_fid(&obj->do_lu);
816 }
817
818 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
819 {
820         memset(pos, 0, sizeof(*pos));
821 }
822
823 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
824 {
825         return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
826 }
827
828 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
829                                   const struct lfsck_position *pos2)
830 {
831         if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
832                 return -1;
833
834         if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
835                 return 1;
836
837         if (fid_is_zero(&pos1->lp_dir_parent) &&
838             !fid_is_zero(&pos2->lp_dir_parent))
839                 return -1;
840
841         if (!fid_is_zero(&pos1->lp_dir_parent) &&
842             fid_is_zero(&pos2->lp_dir_parent))
843                 return 1;
844
845         if (fid_is_zero(&pos1->lp_dir_parent) &&
846             fid_is_zero(&pos2->lp_dir_parent))
847                 return 0;
848
849         LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
850
851         if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
852                 return -1;
853
854         if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
855                 return 1;
856
857         return 0;
858 }
859
860 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
861                                             struct lfsck_position *src)
862 {
863         des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
864         fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
865         des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
866 }
867
868 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
869                                             struct lfsck_position *src)
870 {
871         des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
872         fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
873         des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
874 }
875
876 static inline umode_t lfsck_object_type(const struct dt_object *obj)
877 {
878         return lu_object_attr(&obj->do_lu);
879 }
880
881 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
882 {
883         struct lu_object_header *loh = obj->do_lu.lo_header;
884
885         return !!test_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
886 }
887
888 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
889 {
890         lu_object_get(&obj->do_lu);
891         return obj;
892 }
893
894 static inline void lfsck_object_put(const struct lu_env *env,
895                                     struct dt_object *obj)
896 {
897         lu_object_put(env, &obj->do_lu);
898 }
899
900 static inline struct dt_object *
901 lfsck_object_find_by_dev_nowait(const struct lu_env *env, struct dt_device *dev,
902                                 const struct lu_fid *fid)
903 {
904         struct lu_object_conf   *conf = &lfsck_env_info(env)->lti_conf;
905         struct dt_object        *obj;
906
907         conf->loc_flags = LOC_F_NOWAIT;
908         obj = lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, conf));
909         if (unlikely(obj == NULL))
910                 return ERR_PTR(-ENOENT);
911
912         return obj;
913 }
914
915 static inline struct dt_object *
916 lfsck_object_find_by_dev(const struct lu_env *env, struct dt_device *dev,
917                          const struct lu_fid *fid)
918 {
919         struct dt_object *obj;
920
921         obj = lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, NULL));
922         if (unlikely(obj == NULL))
923                 return ERR_PTR(-ENOENT);
924
925         return obj;
926 }
927
928 static inline struct dt_object *lfsck_object_find(const struct lu_env *env,
929                                                   struct lfsck_instance *lfsck,
930                                                   const struct lu_fid *fid)
931 {
932         return lfsck_object_find_by_dev(env, lfsck->li_next, fid);
933 }
934
935 static inline struct lfsck_tgt_desc *lfsck_tgt_get(struct lfsck_tgt_descs *ltds,
936                                                    __u32 index)
937 {
938         struct lfsck_tgt_desc *ltd;
939
940         ltd = LTD_TGT(ltds, index);
941         if (ltd != NULL)
942                 atomic_inc(&ltd->ltd_ref);
943
944         return ltd;
945 }
946
947 static inline void lfsck_tgt_put(struct lfsck_tgt_desc *ltd)
948 {
949         if (atomic_dec_and_test(&ltd->ltd_ref))
950                 OBD_FREE_PTR(ltd);
951 }
952
953 static inline struct lfsck_component *
954 lfsck_component_get(struct lfsck_component *com)
955 {
956         atomic_inc(&com->lc_ref);
957
958         return com;
959 }
960
961 static inline void lfsck_component_put(const struct lu_env *env,
962                                        struct lfsck_component *com)
963 {
964         if (atomic_dec_and_test(&com->lc_ref)) {
965                 if (com->lc_obj != NULL)
966                         lu_object_put_nocache(env, &com->lc_obj->do_lu);
967                 if (com->lc_file_ram != NULL)
968                         OBD_FREE(com->lc_file_ram, com->lc_file_size);
969                 if (com->lc_file_disk != NULL)
970                         OBD_FREE(com->lc_file_disk, com->lc_file_size);
971                 if (com->lc_data != NULL) {
972                         LASSERT(com->lc_ops->lfsck_data_release != NULL);
973
974                         com->lc_ops->lfsck_data_release(env, com);
975                 }
976
977                 OBD_FREE_PTR(com);
978         }
979 }
980
981 static inline struct lfsck_instance *
982 lfsck_instance_get(struct lfsck_instance *lfsck)
983 {
984         atomic_inc(&lfsck->li_ref);
985
986         return lfsck;
987 }
988
989 static inline void lfsck_instance_put(const struct lu_env *env,
990                                       struct lfsck_instance *lfsck)
991 {
992         if (atomic_dec_and_test(&lfsck->li_ref))
993                 lfsck_instance_cleanup(env, lfsck);
994 }
995
996 static inline u32 lfsck_dev_idx(struct dt_device *dev)
997 {
998         return dev->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
999 }
1000
1001 static inline bool lfsck_phase2_next_ready(struct lfsck_assistant_data *lad)
1002 {
1003         return list_empty(&lad->lad_mdt_phase1_list) &&
1004                (!list_empty(&lad->lad_ost_phase2_list) ||
1005                 list_empty(&lad->lad_ost_phase1_list));
1006 }
1007
1008 static inline void lfsck_lad_set_bitmap(const struct lu_env *env,
1009                                         struct lfsck_component *com,
1010                                         __u32 index)
1011 {
1012         struct lfsck_assistant_data     *lad    = com->lc_data;
1013         cfs_bitmap_t                    *bitmap = lad->lad_bitmap;
1014
1015         LASSERT(com->lc_lfsck->li_master);
1016         LASSERT(bitmap != NULL);
1017         LASSERTF(bitmap->size > index, "invalid index: nbits %d, index %u\n",
1018                  bitmap->size, index);
1019
1020         cfs_bitmap_set(bitmap, index);
1021         lad->lad_incomplete = 1;
1022 }
1023
1024 static inline int lfsck_links_read(const struct lu_env *env,
1025                                    struct dt_object *obj,
1026                                    struct linkea_data *ldata)
1027 {
1028         ldata->ld_buf =
1029                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
1030                                        PAGE_CACHE_SIZE);
1031
1032         return __lfsck_links_read(env, obj, ldata);
1033 }
1034
1035 static inline int lfsck_links_read2(const struct lu_env *env,
1036                                     struct dt_object *obj,
1037                                     struct linkea_data *ldata)
1038 {
1039         ldata->ld_buf =
1040                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf2,
1041                                        PAGE_CACHE_SIZE);
1042
1043         return __lfsck_links_read(env, obj, ldata);
1044 }
1045
1046 #endif /* _LFSCK_INTERNAL_H */