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