Whamcloud - gitweb
LU-5516 lfsck: repair the lost 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         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_buf           lti_buf;
656         struct lu_buf           lti_linkea_buf;
657         struct lu_buf           lti_linkea_buf2;
658         struct lu_buf           lti_big_buf;
659         struct lu_fid           lti_fid;
660         struct lu_fid           lti_fid2;
661         struct lu_fid           lti_fid3;
662         struct lu_fid           lti_fid4;
663         struct lu_fid           lti_fid5;
664         struct lu_attr          lti_la;
665         struct lu_attr          lti_la2;
666         struct lu_attr          lti_la3;
667         struct ost_id           lti_oi;
668         union {
669                 struct lustre_mdt_attrs lti_lma;
670                 /* old LMA for compatibility */
671                 char                    lti_lma_old[LMA_OLD_SIZE];
672         };
673         struct dt_object_format lti_dof;
674         /* There will be '\0' at the end of the name. */
675         char            lti_key[sizeof(struct lu_dirent) + NAME_MAX + 1];
676         char                    lti_tmpbuf[LFSCK_TMPBUF_LEN];
677         struct lfsck_request    lti_lr;
678         struct lfsck_async_interpret_args lti_laia;
679         struct lfsck_async_interpret_args lti_laia2;
680         struct lfsck_start      lti_start;
681         struct lfsck_stop       lti_stop;
682         ldlm_policy_data_t      lti_policy;
683         struct ldlm_res_id      lti_resid;
684         union {
685                 struct filter_fid_old   lti_old_pfid;
686                 struct filter_fid       lti_new_pfid;
687         };
688         struct dt_allocation_hint lti_hint;
689         struct lu_orphan_rec    lti_rec;
690         struct lov_user_md      lti_lum;
691         struct dt_insert_rec    lti_dt_rec;
692         struct lu_object_conf   lti_conf;
693         struct lu_seq_range     lti_range;
694 };
695
696 /* lfsck_lib.c */
697 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
698                     struct lu_fid *fid, bool locked);
699 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
700                      struct dt_object *obj, struct lustre_handle *lh,
701                      __u64 bits, ldlm_mode_t mode);
702 void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode);
703 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
704                               struct lfsck_instance *lfsck,
705                               const struct lu_fid *fid);
706 int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck);
707 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck);
708 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
709                                            bool unlink);
710 struct lfsck_component *lfsck_component_find(struct lfsck_instance *lfsck,
711                                              __u16 type);
712 const char *lfsck_status2names(enum lfsck_status status);
713 void lfsck_component_cleanup(const struct lu_env *env,
714                              struct lfsck_component *com);
715 void lfsck_instance_cleanup(const struct lu_env *env,
716                             struct lfsck_instance *lfsck);
717 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
718                     const char *prefix);
719 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix);
720 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
721                    const char *prefix);
722 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
723                     struct lfsck_position *pos, bool init);
724 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit);
725 void lfsck_control_speed(struct lfsck_instance *lfsck);
726 void lfsck_control_speed_by_self(struct lfsck_component *com);
727 void lfsck_thread_args_fini(struct lfsck_thread_args *lta);
728 struct lfsck_assistant_data *
729 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
730                           const char *name);
731 int lfsck_async_interpret_common(const struct lu_env *env,
732                                  struct ptlrpc_request *req,
733                                  void *args, int rc);
734 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
735                         struct lfsck_request *lr,
736                         struct ptlrpc_request_set *set,
737                         ptlrpc_interpterer_t interpterer,
738                         void *args, int request);
739 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
740                           struct lfsck_start_param *lsp);
741 int lfsck_checkpoint_generic(const struct lu_env *env,
742                              struct lfsck_component *com);
743 void lfsck_post_generic(const struct lu_env *env,
744                         struct lfsck_component *com, int *result);
745 int lfsck_double_scan_generic(const struct lu_env *env,
746                               struct lfsck_component *com, int status);
747 void lfsck_quit_generic(const struct lu_env *env,
748                         struct lfsck_component *com);
749
750 /* lfsck_engine.c */
751 int lfsck_master_engine(void *args);
752 int lfsck_assistant_engine(void *args);
753
754 /* lfsck_bookmark.c */
755 void lfsck_bookmark_cpu_to_le(struct lfsck_bookmark *des,
756                               struct lfsck_bookmark *src);
757 int lfsck_bookmark_store(const struct lu_env *env,
758                          struct lfsck_instance *lfsck);
759 int lfsck_bookmark_setup(const struct lu_env *env,
760                          struct lfsck_instance *lfsck);
761 int lfsck_set_param(const struct lu_env *env, struct lfsck_instance *lfsck,
762                     struct lfsck_start *start, bool reset);
763
764 /* lfsck_namespace.c */
765 int lfsck_namespace_trace_update(const struct lu_env *env,
766                                  struct lfsck_component *com,
767                                  const struct lu_fid *fid,
768                                  const __u8 flags, bool add);
769 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
770                        struct linkea_data *ldata);
771 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
772                                    struct lfsck_component *com,
773                                    struct dt_object *obj,
774                                    struct linkea_data *ldata);
775 int lfsck_namespace_repair_dangling(const struct lu_env *env,
776                                     struct lfsck_component *com,
777                                     struct dt_object *child,
778                                     struct lfsck_namespace_req *lnr);
779 int lfsck_namespace_repair_dirent(const struct lu_env *env,
780                                   struct lfsck_component *com,
781                                   struct dt_object *parent,
782                                   struct dt_object *child,
783                                   const char *name, const char *name2,
784                                   __u16 type, bool update, bool dec);
785 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
786                         struct dt_object *obj, const struct lu_name *cname,
787                         const struct lu_fid *pfid);
788 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
789                           char *name, struct lu_fid *pfid);
790 int lfsck_remove_name_entry(const struct lu_env *env,
791                             struct lfsck_instance *lfsck,
792                             struct dt_object *parent,
793                             const char *name, __u32 type);
794 int lfsck_update_name_entry(const struct lu_env *env,
795                             struct lfsck_instance *lfsck,
796                             struct dt_object *parent, const char *name,
797                             const struct lu_fid *pfid, __u32 type);
798 int lfsck_namespace_setup(const struct lu_env *env,
799                           struct lfsck_instance *lfsck);
800
801 /* lfsck_layout.c */
802 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
803
804 extern const char dot[];
805 extern const char dotdot[];
806 extern const char *lfsck_flags_names[];
807 extern const char *lfsck_param_names[];
808 extern struct lu_context_key lfsck_thread_key;
809
810 static inline struct dt_device *lfsck_obj2dt_dev(struct dt_object *obj)
811 {
812         return container_of0(obj->do_lu.lo_dev, struct dt_device, dd_lu_dev);
813 }
814
815 static inline struct lfsck_thread_info *
816 lfsck_env_info(const struct lu_env *env)
817 {
818         struct lfsck_thread_info *info;
819
820         info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
821         LASSERT(info != NULL);
822         return info;
823 }
824
825 static inline const struct lu_name *
826 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
827 {
828         struct lu_name *lname;
829
830         lname = &lfsck_env_info(env)->lti_name_const;
831         lname->ln_name = area;
832         lname->ln_namelen = len;
833         return lname;
834 }
835
836 static inline void
837 lfsck_buf_init(struct lu_buf *buf, void *area, ssize_t len)
838 {
839         buf->lb_buf = area;
840         buf->lb_len = len;
841 }
842
843 static inline struct lu_buf *
844 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
845 {
846         struct lu_buf *buf;
847
848         buf = &lfsck_env_info(env)->lti_buf;
849         buf->lb_buf = area;
850         buf->lb_len = len;
851         return buf;
852 }
853
854 static inline const struct lu_buf *
855 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
856 {
857         struct lu_buf *buf;
858
859         buf = &lfsck_env_info(env)->lti_buf;
860         buf->lb_buf = (void *)area;
861         buf->lb_len = len;
862         return buf;
863 }
864
865 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
866 {
867         return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
868 }
869
870 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
871 {
872         return lu_object_fid(&obj->do_lu);
873 }
874
875 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
876 {
877         memset(pos, 0, sizeof(*pos));
878 }
879
880 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
881 {
882         return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
883 }
884
885 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
886                                   const struct lfsck_position *pos2)
887 {
888         if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
889                 return -1;
890
891         if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
892                 return 1;
893
894         if (fid_is_zero(&pos1->lp_dir_parent) &&
895             !fid_is_zero(&pos2->lp_dir_parent))
896                 return -1;
897
898         if (!fid_is_zero(&pos1->lp_dir_parent) &&
899             fid_is_zero(&pos2->lp_dir_parent))
900                 return 1;
901
902         if (fid_is_zero(&pos1->lp_dir_parent) &&
903             fid_is_zero(&pos2->lp_dir_parent))
904                 return 0;
905
906         LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
907
908         if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
909                 return -1;
910
911         if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
912                 return 1;
913
914         return 0;
915 }
916
917 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
918                                             struct lfsck_position *src)
919 {
920         des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
921         fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
922         des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
923 }
924
925 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
926                                             struct lfsck_position *src)
927 {
928         des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
929         fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
930         des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
931 }
932
933 static inline umode_t lfsck_object_type(const struct dt_object *obj)
934 {
935         return lu_object_attr(&obj->do_lu);
936 }
937
938 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
939 {
940         struct lu_object_header *loh = obj->do_lu.lo_header;
941
942         return !!test_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
943 }
944
945 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
946 {
947         lu_object_get(&obj->do_lu);
948         return obj;
949 }
950
951 static inline void lfsck_object_put(const struct lu_env *env,
952                                     struct dt_object *obj)
953 {
954         lu_object_put(env, &obj->do_lu);
955 }
956
957 static inline u32 lfsck_dev_idx(struct dt_device *dev)
958 {
959         return dev->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
960 }
961
962 static inline struct dt_object *
963 lfsck_object_find_by_dev_nowait(const struct lu_env *env, struct dt_device *dev,
964                                 const struct lu_fid *fid)
965 {
966         struct lu_object_conf   *conf = &lfsck_env_info(env)->lti_conf;
967         struct dt_object        *obj;
968
969         conf->loc_flags = LOC_F_NOWAIT;
970         obj = lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, conf));
971         if (unlikely(obj == NULL))
972                 return ERR_PTR(-ENOENT);
973
974         return obj;
975 }
976
977 static inline struct dt_object *
978 lfsck_object_find_by_dev(const struct lu_env *env, struct dt_device *dev,
979                          const struct lu_fid *fid)
980 {
981         struct dt_object *obj;
982
983         obj = lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, NULL));
984         if (unlikely(obj == NULL))
985                 return ERR_PTR(-ENOENT);
986
987         return obj;
988 }
989
990 static inline struct dt_object *lfsck_object_find(const struct lu_env *env,
991                                                   struct lfsck_instance *lfsck,
992                                                   const struct lu_fid *fid)
993 {
994         return lfsck_object_find_by_dev(env, lfsck->li_next, fid);
995 }
996
997 static inline struct dt_object *
998 lfsck_object_find_bottom(const struct lu_env *env, struct lfsck_instance *lfsck,
999                          const struct lu_fid *fid)
1000 {
1001         struct dt_device *dev;
1002         int               idx;
1003
1004         idx = lfsck_find_mdt_idx_by_fid(env, lfsck, fid);
1005         if (idx < 0)
1006                 return ERR_PTR(idx);
1007
1008         if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
1009                 dev = lfsck->li_bottom;
1010         } else {
1011                 struct lfsck_tgt_desc *ltd;
1012
1013                 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
1014                 if (unlikely(ltd == NULL))
1015                         return ERR_PTR(-ENODEV);
1016
1017                 dev = ltd->ltd_tgt;
1018         }
1019
1020         return lfsck_object_find_by_dev(env, dev, fid);
1021 }
1022
1023 static inline struct lfsck_tgt_desc *lfsck_tgt_get(struct lfsck_tgt_descs *ltds,
1024                                                    __u32 index)
1025 {
1026         struct lfsck_tgt_desc *ltd;
1027
1028         ltd = LTD_TGT(ltds, index);
1029         if (ltd != NULL)
1030                 atomic_inc(&ltd->ltd_ref);
1031
1032         return ltd;
1033 }
1034
1035 static inline void lfsck_tgt_put(struct lfsck_tgt_desc *ltd)
1036 {
1037         if (atomic_dec_and_test(&ltd->ltd_ref))
1038                 OBD_FREE_PTR(ltd);
1039 }
1040
1041 static inline struct lfsck_component *
1042 lfsck_component_get(struct lfsck_component *com)
1043 {
1044         atomic_inc(&com->lc_ref);
1045
1046         return com;
1047 }
1048
1049 static inline void lfsck_component_put(const struct lu_env *env,
1050                                        struct lfsck_component *com)
1051 {
1052         if (atomic_dec_and_test(&com->lc_ref)) {
1053                 if (com->lc_obj != NULL)
1054                         lu_object_put_nocache(env, &com->lc_obj->do_lu);
1055                 if (com->lc_file_ram != NULL)
1056                         OBD_FREE(com->lc_file_ram, com->lc_file_size);
1057                 if (com->lc_file_disk != NULL)
1058                         OBD_FREE(com->lc_file_disk, com->lc_file_size);
1059                 if (com->lc_data != NULL) {
1060                         LASSERT(com->lc_ops->lfsck_data_release != NULL);
1061
1062                         com->lc_ops->lfsck_data_release(env, com);
1063                 }
1064
1065                 OBD_FREE_PTR(com);
1066         }
1067 }
1068
1069 static inline struct lfsck_instance *
1070 lfsck_instance_get(struct lfsck_instance *lfsck)
1071 {
1072         atomic_inc(&lfsck->li_ref);
1073
1074         return lfsck;
1075 }
1076
1077 static inline void lfsck_instance_put(const struct lu_env *env,
1078                                       struct lfsck_instance *lfsck)
1079 {
1080         if (atomic_dec_and_test(&lfsck->li_ref))
1081                 lfsck_instance_cleanup(env, lfsck);
1082 }
1083
1084 static inline bool lfsck_phase2_next_ready(struct lfsck_assistant_data *lad)
1085 {
1086         return list_empty(&lad->lad_mdt_phase1_list) &&
1087                (!list_empty(&lad->lad_ost_phase2_list) ||
1088                 list_empty(&lad->lad_ost_phase1_list));
1089 }
1090
1091 static inline void lfsck_lad_set_bitmap(const struct lu_env *env,
1092                                         struct lfsck_component *com,
1093                                         __u32 index)
1094 {
1095         struct lfsck_assistant_data     *lad    = com->lc_data;
1096         cfs_bitmap_t                    *bitmap = lad->lad_bitmap;
1097
1098         LASSERT(com->lc_lfsck->li_master);
1099         LASSERT(bitmap != NULL);
1100         LASSERTF(bitmap->size > index, "invalid index: nbits %d, index %u\n",
1101                  bitmap->size, index);
1102
1103         cfs_bitmap_set(bitmap, index);
1104         lad->lad_incomplete = 1;
1105 }
1106
1107 static inline int lfsck_links_read(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_buf,
1113                                        PAGE_CACHE_SIZE);
1114
1115         return __lfsck_links_read(env, obj, ldata);
1116 }
1117
1118 static inline int lfsck_links_read2(const struct lu_env *env,
1119                                     struct dt_object *obj,
1120                                     struct linkea_data *ldata)
1121 {
1122         ldata->ld_buf =
1123                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf2,
1124                                        PAGE_CACHE_SIZE);
1125
1126         return __lfsck_links_read(env, obj, ldata);
1127 }
1128
1129 #endif /* _LFSCK_INTERNAL_H */