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