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