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