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