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