Whamcloud - gitweb
e00d8560a9f09047f0eaccaa8f0559bc3a0721f6
[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
49 #define HALF_SEC                        (HZ >> 1)
50 #define LFSCK_CHECKPOINT_INTERVAL       60
51
52 #define LFSCK_NAMEENTRY_DEAD            1 /* The object has been unlinked. */
53 #define LFSCK_NAMEENTRY_REMOVED         2 /* The entry has been removed. */
54 #define LFSCK_NAMEENTRY_RECREATED       3 /* The entry has been recreated. */
55
56 enum lfsck_flags {
57         /* Finish the first cycle scanning. */
58         LF_SCANNED_ONCE         = 0x00000001ULL,
59
60         /* There is some namespace inconsistency. */
61         LF_INCONSISTENT         = 0x00000002ULL,
62
63         /* The device is upgraded from 1.8 format. */
64         LF_UPGRADE              = 0x00000004ULL,
65
66         /* The server ever restarted during the LFSCK, and may miss to process
67          * some objects check/repair. */
68         LF_INCOMPLETE           = 0x00000008ULL,
69
70         /* The LAST_ID (file) crashed. */
71         LF_CRASHED_LASTID       = 0x00000010ULL,
72 };
73
74 struct lfsck_position {
75         /* low layer object table-based iteration position. */
76         __u64   lp_oit_cookie;
77
78         /* parent FID for directory traversal. */
79         struct lu_fid lp_dir_parent;
80
81         /* namespace-based directory traversal position. */
82         __u64   lp_dir_cookie;
83 };
84
85 struct lfsck_bookmark {
86         /* Magic number to detect that this struct contains valid data. */
87         __u32   lb_magic;
88
89         /* For compatible with old versions. */
90         __u16   lb_version;
91
92         /* See 'enum lfsck_param_flags' */
93         __u16   lb_param;
94
95         /* How many items can be scanned at most per second. */
96         __u32   lb_speed_limit;
97
98         /* The windows size for async requests pipeline. */
99         __u16   lb_async_windows;
100
101         /* For 64-bits aligned. */
102         __u16   lb_padding;
103
104         /* The FID for .lustre/lost+found/MDTxxxx */
105         struct lu_fid   lb_lpf_fid;
106
107         /* The FID for the last MDT-object created by the LFSCK repairing. */
108         struct lu_fid   lb_last_fid;
109
110         /* For future using. */
111         __u64   lb_reserved[2];
112 };
113
114 struct lfsck_namespace {
115         /* Magic number to detect that this struct contains valid data. */
116         __u32   ln_magic;
117
118         /* See 'enum lfsck_status'. */
119         __u32   ln_status;
120
121         /* See 'enum lfsck_flags'. */
122         __u32   ln_flags;
123
124         /* How many completed LFSCK runs on the device. */
125         __u32   ln_success_count;
126
127         /*  How long the LFSCK phase1 has run in seconds. */
128         __u32   ln_run_time_phase1;
129
130         /*  How long the LFSCK phase2 has run in seconds. */
131         __u32   ln_run_time_phase2;
132
133         /* Time for the last LFSCK completed in seconds since epoch. */
134         __u64   ln_time_last_complete;
135
136         /* Time for the latest LFSCK ran in seconds since epoch. */
137         __u64   ln_time_latest_start;
138
139         /* Time for the last LFSCK checkpoint in seconds since epoch. */
140         __u64   ln_time_last_checkpoint;
141
142         /* Position for the latest LFSCK started from. */
143         struct lfsck_position   ln_pos_latest_start;
144
145         /* Position for the last LFSCK checkpoint. */
146         struct lfsck_position   ln_pos_last_checkpoint;
147
148         /* Position for the first should be updated object. */
149         struct lfsck_position   ln_pos_first_inconsistent;
150
151         /* How many items (including dir) have been checked. */
152         __u64   ln_items_checked;
153
154         /* How many items have been repaired. */
155         __u64   ln_items_repaired;
156
157         /* How many items failed to be processed. */
158         __u64   ln_items_failed;
159
160         /* How many directories have been traversed. */
161         __u64   ln_dirs_checked;
162
163         /* How many multiple-linked objects have been checked. */
164         __u64   ln_mlinked_checked;
165
166         /* How many objects have been double scanned. */
167         __u64   ln_objs_checked_phase2;
168
169         /* How many objects have been reparied during double scan. */
170         __u64   ln_objs_repaired_phase2;
171
172         /* How many objects failed to be processed during double scan. */
173         __u64   ln_objs_failed_phase2;
174
175         /* How many objects with nlink fixed. */
176         __u64   ln_objs_nlink_repaired;
177
178         /* How many objects were lost before, but found back now. */
179         __u64   ln_objs_lost_found;
180
181         /* The latest object has been processed (failed) during double scan. */
182         struct lu_fid   ln_fid_latest_scanned_phase2;
183
184         /* How many FID-in-dirent entries have been repaired. */
185         __u64   ln_dirent_repaired;
186
187         /* How many linkEA entries have been repaired. */
188         __u64   ln_linkea_repaired;
189 };
190
191 enum lfsck_layout_inconsistency_type {
192         LLIT_NONE                       = 0,
193         LLIT_DANGLING                   = 1,
194         LLIT_UNMATCHED_PAIR             = 2,
195         LLIT_MULTIPLE_REFERENCED        = 3,
196         LLIT_ORPHAN                     = 4,
197         LLIT_INCONSISTENT_OWNER         = 5,
198         LLIT_OTHERS                     = 6,
199         LLIT_MAX                        = LLIT_OTHERS
200 };
201
202 struct lfsck_layout {
203         /* Magic number to detect that this struct contains valid data. */
204         __u32   ll_magic;
205
206         /* See 'enum lfsck_status'. */
207         __u32   ll_status;
208
209         /* See 'enum lfsck_flags'. */
210         __u32   ll_flags;
211
212         /* How many completed LFSCK runs on the device. */
213         __u32   ll_success_count;
214
215         /*  How long the LFSCK phase1 has run in seconds. */
216         __u32   ll_run_time_phase1;
217
218         /*  How long the LFSCK phase2 has run in seconds. */
219         __u32   ll_run_time_phase2;
220
221         /* Time for the last LFSCK completed in seconds since epoch. */
222         __u64   ll_time_last_complete;
223
224         /* Time for the latest LFSCK ran in seconds since epoch. */
225         __u64   ll_time_latest_start;
226
227         /* Time for the last LFSCK checkpoint in seconds since epoch. */
228         __u64   ll_time_last_checkpoint;
229
230         /* Position for the latest LFSCK started from. */
231         __u64   ll_pos_latest_start;
232
233         /* Position for the last LFSCK checkpoint. */
234         __u64   ll_pos_last_checkpoint;
235
236         /* Position for the first object to be fixed or
237          * failed to be checked in the phase1. */
238         __u64   ll_pos_first_inconsistent;
239
240         /* How many objects have been checked. */
241         __u64   ll_objs_checked_phase1;
242
243         /* How many objects failed to be processed. */
244         __u64   ll_objs_failed_phase1;
245
246         /* How many objects have been double scanned. */
247         __u64   ll_objs_checked_phase2;
248
249         /* How many objects failed to be processed during double scan. */
250         __u64   ll_objs_failed_phase2;
251
252         /* kinds of inconsistency have been or to be repaired.
253          * ll_objs_repaired[type - 1] is the count for the given @type. */
254         __u64   ll_objs_repaired[LLIT_MAX];
255
256         /* How many objects have been skipped because of related
257          * MDT(s)/OST(s) do not participate in the LFSCK */
258         __u64   ll_objs_skipped;
259
260         /* For further using. 256-bytes aligned now. */
261         __u64   ll_reserved[12];
262 };
263
264 struct lfsck_component;
265 struct lfsck_tgt_descs;
266 struct lfsck_tgt_desc;
267
268 struct lfsck_operations {
269         int (*lfsck_reset)(const struct lu_env *env,
270                            struct lfsck_component *com,
271                            bool init);
272
273         void (*lfsck_fail)(const struct lu_env *env,
274                            struct lfsck_component *com,
275                            bool new_checked);
276
277         int (*lfsck_checkpoint)(const struct lu_env *env,
278                                 struct lfsck_component *com,
279                                 bool init);
280
281         int (*lfsck_prep)(const struct lu_env *env,
282                           struct lfsck_component *com,
283                           struct lfsck_start_param *lsp);
284
285         int (*lfsck_exec_oit)(const struct lu_env *env,
286                               struct lfsck_component *com,
287                               struct dt_object *obj);
288
289         int (*lfsck_exec_dir)(const struct lu_env *env,
290                               struct lfsck_component *com,
291                               struct dt_object *obj,
292                               struct lu_dirent *ent);
293
294         int (*lfsck_post)(const struct lu_env *env,
295                           struct lfsck_component *com,
296                           int result,
297                           bool init);
298
299         int (*lfsck_interpret)(const struct lu_env *env,
300                                struct ptlrpc_request *req,
301                                void *args,
302                                int rc);
303
304         int (*lfsck_dump)(const struct lu_env *env,
305                           struct lfsck_component *com,
306                           struct seq_file *m);
307
308         int (*lfsck_double_scan)(const struct lu_env *env,
309                                  struct lfsck_component *com);
310
311         void (*lfsck_data_release)(const struct lu_env *env,
312                                    struct lfsck_component *com);
313
314         void (*lfsck_quit)(const struct lu_env *env,
315                            struct lfsck_component *com);
316
317         int (*lfsck_in_notify)(const struct lu_env *env,
318                                struct lfsck_component *com,
319                                struct lfsck_request *lr);
320
321         int (*lfsck_query)(const struct lu_env *env,
322                            struct lfsck_component *com);
323
324         int (*lfsck_stop_notify)(const struct lu_env *env,
325                                  struct lfsck_component *com,
326                                  struct lfsck_tgt_descs *ltds,
327                                  struct lfsck_tgt_desc *ltd,
328                                  struct ptlrpc_request_set *set);
329
330         int (*lfsck_join)(const struct lu_env *env,
331                           struct lfsck_component *com,
332                           struct lfsck_start_param *lsp);
333 };
334
335 #define TGT_PTRS                256     /* number of pointers at 1st level */
336 #define TGT_PTRS_PER_BLOCK      256     /* number of pointers at 2nd level */
337
338 struct lfsck_tgt_desc {
339         struct list_head   ltd_orphan_list;
340         struct dt_device  *ltd_tgt;
341         struct dt_device  *ltd_key;
342         struct obd_export *ltd_exp;
343         struct list_head   ltd_layout_list;
344         struct list_head   ltd_layout_phase_list;
345         atomic_t           ltd_ref;
346         __u32              ltd_index;
347         __u32              ltd_layout_gen;
348         unsigned int       ltd_dead:1,
349                            ltd_layout_done:1;
350 };
351
352 struct lfsck_tgt_desc_idx {
353         struct lfsck_tgt_desc *ldi_tgts[TGT_PTRS_PER_BLOCK];
354 };
355
356 struct lfsck_tgt_descs {
357         /* list of known TGTs */
358         struct lfsck_tgt_desc_idx       *ltd_tgts_idx[TGT_PTRS];
359
360         /* bitmap of TGTs available */
361         cfs_bitmap_t                    *ltd_tgts_bitmap;
362
363         /* for lfsck_tgt_desc::ltd_xxx_list */
364         spinlock_t                       ltd_lock;
365
366         /* for tgts table accessing and changes */
367         struct rw_semaphore              ltd_rw_sem;
368
369         /* Temporary list for orphan targets. */
370         struct list_head                 ltd_orphan;
371
372         /* number of registered TGTs */
373         int                              ltd_tgtnr;
374 };
375
376 #define LTD_TGT(ltd, index)     \
377         ((ltd)->ltd_tgts_idx[(index) / TGT_PTRS_PER_BLOCK]->\
378          ldi_tgts[(index) % TGT_PTRS_PER_BLOCK])
379
380 #define OST_TGT(lfsck, index)   LTD_TGT(&lfsck->li_ost_descs, index)
381 #define MDT_TGT(lfsck, index)   LTD_TGT(&lfsck->li_mdt_descs, index)
382
383 struct lfsck_component {
384         /* into lfsck_instance::li_list_(scan,double_scan,idle} */
385         cfs_list_t               lc_link;
386
387         /* into lfsck_instance::li_list_dir */
388         cfs_list_t               lc_link_dir;
389
390         struct rw_semaphore      lc_sem;
391         atomic_t                 lc_ref;
392
393         struct lfsck_position    lc_pos_start;
394         struct lfsck_instance   *lc_lfsck;
395         struct dt_object        *lc_obj;
396         struct lfsck_operations *lc_ops;
397         void                    *lc_file_ram;
398         void                    *lc_file_disk;
399         void                    *lc_data;
400         struct lu_fid            lc_fid_latest_scanned_phase2;
401
402         /* The time for last checkpoint, jiffies */
403         cfs_time_t               lc_time_last_checkpoint;
404
405         /* The time for next checkpoint, jiffies */
406         cfs_time_t               lc_time_next_checkpoint;
407
408         __u32                    lc_file_size;
409
410         /* How many objects have been checked since last checkpoint. */
411         __u32                    lc_new_checked;
412
413         /* How many objects have been scanned since last sleep. */
414         __u32                    lc_new_scanned;
415
416         unsigned int             lc_journal:1;
417         __u16                    lc_type;
418 };
419
420 struct lfsck_instance {
421         struct mutex              li_mutex;
422         spinlock_t                li_lock;
423
424         /* Link into the lfsck_instance_list. */
425         cfs_list_t                li_link;
426
427         /* For the components in (first) scanning via otable-based iteration. */
428         cfs_list_t                li_list_scan;
429
430         /* For the components in scanning via directory traversal. Because
431          * directory traversal cannot guarantee all the object be scanned,
432          * so the component in the li_list_dir must be in li_list_scan. */
433         cfs_list_t                li_list_dir;
434
435         /* For the components in double scanning. */
436         cfs_list_t                li_list_double_scan;
437
438         /* For the components those are not scanning now. */
439         cfs_list_t                li_list_idle;
440
441         atomic_t                  li_ref;
442         atomic_t                  li_double_scan_count;
443         struct ptlrpc_thread      li_thread;
444
445         /* The time for last checkpoint, jiffies */
446         cfs_time_t                li_time_last_checkpoint;
447
448         /* The time for next checkpoint, jiffies */
449         cfs_time_t                li_time_next_checkpoint;
450
451         lfsck_out_notify          li_out_notify;
452         void                     *li_out_notify_data;
453         struct dt_device         *li_next;
454         struct dt_device         *li_bottom;
455         struct obd_device        *li_obd;
456         struct ldlm_namespace    *li_namespace;
457         struct local_oid_storage *li_los;
458         struct lu_fid             li_local_root_fid;  /* backend root "/" */
459         struct lu_fid             li_global_root_fid; /* /ROOT */
460         struct dt_object         *li_bookmark_obj;
461         struct dt_object         *li_lpf_obj;
462         struct lu_client_seq     *li_seq;
463         struct lfsck_bookmark     li_bookmark_ram;
464         struct lfsck_bookmark     li_bookmark_disk;
465         struct lfsck_position     li_pos_current;
466
467         /* Obj for otable-based iteration */
468         struct dt_object         *li_obj_oit;
469
470         /* Obj for directory traversal */
471         struct dt_object         *li_obj_dir;
472
473         /* It for otable-based iteration */
474         struct dt_it             *li_di_oit;
475
476         /* It for directory traversal */
477         struct dt_it             *li_di_dir;
478
479         /* Description of OST */
480         struct lfsck_tgt_descs    li_ost_descs;
481
482         /* Description of MDT */
483         struct lfsck_tgt_descs    li_mdt_descs;
484
485         /* namespace-based directory traversal position. */
486         __u64                     li_cookie_dir;
487
488         /* Arguments for low layer otable-based iteration. */
489         __u32                     li_args_oit;
490
491         /* Arugments for namespace-based directory traversal. */
492         __u32                     li_args_dir;
493
494         /* Schedule for every N objects. */
495         __u32                     li_sleep_rate;
496
497         /* Sleep N jiffies for each schedule. */
498         __u32                     li_sleep_jif;
499
500         /* How many objects have been scanned since last sleep. */
501         __u32                     li_new_scanned;
502
503         /* The status when the LFSCK stopped or paused. */
504         __u32                     li_status;
505
506         /* The flags when the lFSCK stopped or paused. */
507         __u32                     li_flags;
508
509         unsigned int              li_oit_over:1, /* oit is finished. */
510                                   li_drop_dryrun:1, /* Ever dryrun, not now. */
511                                   li_master:1, /* Master instance or not. */
512                                   li_current_oit_processed:1,
513                                   li_start_unplug:1;
514 };
515
516 enum lfsck_linkea_flags {
517         /* The linkea entries does not match the object nlinks. */
518         LLF_UNMATCH_NLINKS      = 0x01,
519
520         /* Fail to repair the multiple-linked objects during the double scan. */
521         LLF_REPAIR_FAILED       = 0x02,
522 };
523
524 struct lfsck_async_interpret_args {
525         struct lfsck_component          *laia_com;
526         struct lfsck_tgt_descs          *laia_ltds;
527         struct lfsck_tgt_desc           *laia_ltd;
528         struct lfsck_request            *laia_lr;
529         int                              laia_result;
530         unsigned int                     laia_shared:1;
531 };
532
533 struct lfsck_thread_args {
534         struct lu_env                    lta_env;
535         struct lfsck_instance           *lta_lfsck;
536         struct lfsck_component          *lta_com;
537         struct lfsck_start_param        *lta_lsp;
538 };
539
540 #define LFSCK_TMPBUF_LEN        64
541
542 struct lfsck_thread_info {
543         struct lu_name          lti_name;
544         struct lu_buf           lti_buf;
545         struct lu_buf           lti_linkea_buf;
546         struct lu_buf           lti_big_buf;
547         struct lu_fid           lti_fid;
548         struct lu_fid           lti_fid2;
549         struct lu_fid           lti_fid3;
550         struct lu_attr          lti_la;
551         struct lu_attr          lti_la2;
552         struct lu_attr          lti_la3;
553         struct ost_id           lti_oi;
554         union {
555                 struct lustre_mdt_attrs lti_lma;
556                 /* old LMA for compatibility */
557                 char                    lti_lma_old[LMA_OLD_SIZE];
558         };
559         struct dt_object_format lti_dof;
560         /* lti_ent and lti_key must be conjoint,
561          * then lti_ent::lde_name will be lti_key. */
562         struct lu_dirent        lti_ent;
563         char                    lti_key[NAME_MAX + 16];
564         char                    lti_tmpbuf[LFSCK_TMPBUF_LEN];
565         struct lfsck_request    lti_lr;
566         struct lfsck_async_interpret_args lti_laia;
567         struct lfsck_start      lti_start;
568         struct lfsck_stop       lti_stop;
569         ldlm_policy_data_t      lti_policy;
570         struct ldlm_res_id      lti_resid;
571         union {
572                 struct filter_fid_old   lti_old_pfid;
573                 struct filter_fid       lti_new_pfid;
574         };
575         struct dt_allocation_hint lti_hint;
576         struct lu_orphan_rec    lti_rec;
577         struct lov_user_md      lti_lum;
578 };
579
580 /* lfsck_lib.c */
581 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
582                     struct lu_fid *fid, bool locked);
583 int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck);
584 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
585                                            bool unlink);
586 struct lfsck_component *lfsck_component_find(struct lfsck_instance *lfsck,
587                                              __u16 type);
588 const char *lfsck_status2names(enum lfsck_status status);
589 void lfsck_component_cleanup(const struct lu_env *env,
590                              struct lfsck_component *com);
591 void lfsck_instance_cleanup(const struct lu_env *env,
592                             struct lfsck_instance *lfsck);
593 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
594                     const char *prefix);
595 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix);
596 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
597                    const char *prefix);
598 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
599                     struct lfsck_position *pos, bool init);
600 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit);
601 void lfsck_control_speed(struct lfsck_instance *lfsck);
602 void lfsck_control_speed_by_self(struct lfsck_component *com);
603 int lfsck_reset(const struct lu_env *env, struct lfsck_instance *lfsck,
604                 bool init);
605 struct lfsck_thread_args *lfsck_thread_args_init(struct lfsck_instance *lfsck,
606                                                  struct lfsck_component *com,
607                                                  struct lfsck_start_param *lsp);
608 void lfsck_thread_args_fini(struct lfsck_thread_args *lta);
609 void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
610                 bool new_checked);
611 int lfsck_checkpoint(const struct lu_env *env, struct lfsck_instance *lfsck);
612 int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
613                struct lfsck_start_param *lsp);
614 int lfsck_exec_oit(const struct lu_env *env, struct lfsck_instance *lfsck,
615                    struct dt_object *obj);
616 int lfsck_exec_dir(const struct lu_env *env, struct lfsck_instance *lfsck,
617                    struct dt_object *obj, struct lu_dirent *ent);
618 int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
619                int result);
620 int lfsck_double_scan(const struct lu_env *env, struct lfsck_instance *lfsck);
621 void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck);
622 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
623                         struct lfsck_request *lr,
624                         struct ptlrpc_request_set *set,
625                         ptlrpc_interpterer_t interpterer,
626                         void *args, int request);
627
628 /* lfsck_engine.c */
629 int lfsck_master_engine(void *args);
630
631 /* lfsck_bookmark.c */
632 void lfsck_bookmark_cpu_to_le(struct lfsck_bookmark *des,
633                               struct lfsck_bookmark *src);
634 int lfsck_bookmark_store(const struct lu_env *env,
635                          struct lfsck_instance *lfsck);
636 int lfsck_bookmark_setup(const struct lu_env *env,
637                          struct lfsck_instance *lfsck);
638 int lfsck_set_param(const struct lu_env *env, struct lfsck_instance *lfsck,
639                     struct lfsck_start *start, bool reset);
640
641 /* lfsck_namespace.c */
642 int lfsck_namespace_setup(const struct lu_env *env,
643                           struct lfsck_instance *lfsck);
644
645 /* lfsck_layout.c */
646 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
647
648 extern const char *lfsck_flags_names[];
649 extern const char *lfsck_param_names[];
650 extern struct lu_context_key lfsck_thread_key;
651
652 static inline struct dt_device *lfsck_obj2dt_dev(struct dt_object *obj)
653 {
654         return container_of0(obj->do_lu.lo_dev, struct dt_device, dd_lu_dev);
655 }
656
657 static inline struct lfsck_thread_info *
658 lfsck_env_info(const struct lu_env *env)
659 {
660         struct lfsck_thread_info *info;
661
662         info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
663         LASSERT(info != NULL);
664         return info;
665 }
666
667 static inline const struct lu_name *
668 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
669 {
670         struct lu_name *lname;
671
672         lname = &lfsck_env_info(env)->lti_name;
673         lname->ln_name = area;
674         lname->ln_namelen = len;
675         return lname;
676 }
677
678 static inline struct lu_buf *
679 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
680 {
681         struct lu_buf *buf;
682
683         buf = &lfsck_env_info(env)->lti_buf;
684         buf->lb_buf = area;
685         buf->lb_len = len;
686         return buf;
687 }
688
689 static inline const struct lu_buf *
690 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
691 {
692         struct lu_buf *buf;
693
694         buf = &lfsck_env_info(env)->lti_buf;
695         buf->lb_buf = (void *)area;
696         buf->lb_len = len;
697         return buf;
698 }
699
700 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
701 {
702         return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
703 }
704
705 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
706 {
707         return lu_object_fid(&obj->do_lu);
708 }
709
710 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
711 {
712         memset(pos, 0, sizeof(*pos));
713 }
714
715 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
716 {
717         return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
718 }
719
720 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
721                                   const struct lfsck_position *pos2)
722 {
723         if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
724                 return -1;
725
726         if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
727                 return 1;
728
729         if (fid_is_zero(&pos1->lp_dir_parent) &&
730             !fid_is_zero(&pos2->lp_dir_parent))
731                 return -1;
732
733         if (!fid_is_zero(&pos1->lp_dir_parent) &&
734             fid_is_zero(&pos2->lp_dir_parent))
735                 return 1;
736
737         if (fid_is_zero(&pos1->lp_dir_parent) &&
738             fid_is_zero(&pos2->lp_dir_parent))
739                 return 0;
740
741         LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
742
743         if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
744                 return -1;
745
746         if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
747                 return 1;
748
749         return 0;
750 }
751
752 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
753                                             struct lfsck_position *src)
754 {
755         des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
756         fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
757         des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
758 }
759
760 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
761                                             struct lfsck_position *src)
762 {
763         des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
764         fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
765         des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
766 }
767
768 static inline umode_t lfsck_object_type(const struct dt_object *obj)
769 {
770         return lu_object_attr(&obj->do_lu);
771 }
772
773 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
774 {
775         struct lu_object_header *loh = obj->do_lu.lo_header;
776
777         return !!test_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
778 }
779
780 static inline struct dt_object *lfsck_object_find(const struct lu_env *env,
781                                                   struct lfsck_instance *lfsck,
782                                                   const struct lu_fid *fid)
783 {
784         return lu2dt(lu_object_find_slice(env, dt2lu_dev(lfsck->li_next),
785                      fid, NULL));
786 }
787
788 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
789 {
790         lu_object_get(&obj->do_lu);
791         return obj;
792 }
793
794 static inline void lfsck_object_put(const struct lu_env *env,
795                                     struct dt_object *obj)
796 {
797         lu_object_put(env, &obj->do_lu);
798 }
799
800 static inline struct dt_object *
801 lfsck_object_find_by_dev(const struct lu_env *env, struct dt_device *dev,
802                          const struct lu_fid *fid)
803 {
804         struct dt_object *obj;
805
806         obj = lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, NULL));
807         if (unlikely(obj == NULL))
808                 return ERR_PTR(-ENOENT);
809
810         return obj;
811 }
812
813 static inline struct lfsck_tgt_desc *lfsck_tgt_get(struct lfsck_tgt_descs *ltds,
814                                                    __u32 index)
815 {
816         struct lfsck_tgt_desc *ltd;
817
818         ltd = LTD_TGT(ltds, index);
819         if (ltd != NULL)
820                 atomic_inc(&ltd->ltd_ref);
821
822         return ltd;
823 }
824
825 static inline void lfsck_tgt_put(struct lfsck_tgt_desc *ltd)
826 {
827         if (atomic_dec_and_test(&ltd->ltd_ref))
828                 OBD_FREE_PTR(ltd);
829 }
830
831 static inline struct lfsck_component *
832 lfsck_component_get(struct lfsck_component *com)
833 {
834         atomic_inc(&com->lc_ref);
835
836         return com;
837 }
838
839 static inline void lfsck_component_put(const struct lu_env *env,
840                                        struct lfsck_component *com)
841 {
842         if (atomic_dec_and_test(&com->lc_ref)) {
843                 if (com->lc_obj != NULL)
844                         lu_object_put_nocache(env, &com->lc_obj->do_lu);
845                 if (com->lc_file_ram != NULL)
846                         OBD_FREE(com->lc_file_ram, com->lc_file_size);
847                 if (com->lc_file_disk != NULL)
848                         OBD_FREE(com->lc_file_disk, com->lc_file_size);
849                 if (com->lc_data != NULL) {
850                         LASSERT(com->lc_ops->lfsck_data_release != NULL);
851
852                         com->lc_ops->lfsck_data_release(env, com);
853                 }
854
855                 OBD_FREE_PTR(com);
856         }
857 }
858
859 static inline struct lfsck_instance *
860 lfsck_instance_get(struct lfsck_instance *lfsck)
861 {
862         atomic_inc(&lfsck->li_ref);
863
864         return lfsck;
865 }
866
867 static inline void lfsck_instance_put(const struct lu_env *env,
868                                       struct lfsck_instance *lfsck)
869 {
870         if (atomic_dec_and_test(&lfsck->li_ref))
871                 lfsck_instance_cleanup(env, lfsck);
872 }
873
874 static inline mdsno_t lfsck_dev_idx(struct dt_device *dev)
875 {
876         return dev->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
877 }
878
879 #endif /* _LFSCK_INTERNAL_H */