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