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