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