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