Whamcloud - gitweb
LU-8300 lfsck: pass LOC_F_NEW when create new 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, 2015, 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                        msecs_to_jiffies(MSEC_PER_SEC >> 1)
51 #define LFSCK_CHECKPOINT_INTERVAL       60
52
53 enum lfsck_flags {
54         /* Finish the first cycle scanning. */
55         LF_SCANNED_ONCE         = 0x00000001ULL,
56
57         /* There is some namespace inconsistency. */
58         LF_INCONSISTENT         = 0x00000002ULL,
59
60         /* The device is upgraded from 1.8 format. */
61         LF_UPGRADE              = 0x00000004ULL,
62
63         /* The server ever restarted during the LFSCK, and may miss to process
64          * some objects check/repair. */
65         LF_INCOMPLETE           = 0x00000008ULL,
66
67         /* The LAST_ID (file) crashed. */
68         LF_CRASHED_LASTID       = 0x00000010ULL,
69 };
70
71 struct lfsck_position {
72         /* low layer object table-based iteration position. */
73         __u64   lp_oit_cookie;
74
75         /* parent FID for directory traversal. */
76         struct lu_fid lp_dir_parent;
77
78         /* namespace-based directory traversal position. */
79         __u64   lp_dir_cookie;
80 };
81
82 struct lfsck_bookmark {
83         /* Magic number to detect that this struct contains valid data. */
84         __u32   lb_magic;
85
86         /* For compatible with old versions. */
87         __u16   lb_version;
88
89         /* See 'enum lfsck_param_flags' */
90         __u16   lb_param;
91
92         /* How many items can be scanned at most per second. */
93         __u32   lb_speed_limit;
94
95         /* The windows size for async requests pipeline. */
96         __u16   lb_async_windows;
97
98         /* For 64-bits aligned. */
99         __u16   lb_padding;
100
101         /* The FID for .lustre/lost+found/MDTxxxx */
102         struct lu_fid   lb_lpf_fid;
103
104         /* The FID for the last MDT-object created by the LFSCK repairing. */
105         struct lu_fid   lb_last_fid;
106
107         /* For future using. */
108         __u64   lb_reserved[2];
109 };
110
111 enum lfsck_namespace_trace_flags {
112         LNTF_CHECK_LINKEA       = 0x01,
113         LNTF_CHECK_PARENT       = 0x02,
114         LNTF_SKIP_NLINK         = 0x04,
115         LNTF_CHECK_ORPHAN       = 0x08,
116         LNTF_UNCERTAIN_LMV      = 0x10,
117         LNTF_RECHECK_NAME_HASH  = 0x20,
118         LNTF_ALL                = 0xff
119 };
120
121 enum lfsck_namespace_inconsistency_type {
122         LNIT_NONE               = 0,
123         LNIT_BAD_LINKEA         = 1,
124         LNIT_UNMATCHED_PAIRS    = 2,
125         LNIT_DANGLING           = 3,
126         LNIT_MUL_REF            = 4,
127         LNIT_BAD_TYPE           = 5,
128         LNIT_BAD_DIRENT         = 6,
129 };
130
131 struct lfsck_namespace {
132         /* Magic number to detect that this struct contains valid data. */
133         __u32   ln_magic;
134
135         /* See 'enum lfsck_status'. */
136         __u32   ln_status;
137
138         /* See 'enum lfsck_flags'. */
139         __u32   ln_flags;
140
141         /* How many completed LFSCK runs on the device. */
142         __u32   ln_success_count;
143
144         /*  How long the LFSCK phase1 has run in seconds. */
145         __u32   ln_run_time_phase1;
146
147         /*  How long the LFSCK phase2 has run in seconds. */
148         __u32   ln_run_time_phase2;
149
150         /* Time for the last LFSCK completed in seconds since epoch. */
151         __u64   ln_time_last_complete;
152
153         /* Time for the latest LFSCK ran in seconds since epoch. */
154         __u64   ln_time_latest_start;
155
156         /* Time for the last LFSCK checkpoint in seconds since epoch. */
157         __u64   ln_time_last_checkpoint;
158
159         /* Position for the latest LFSCK started from. */
160         struct lfsck_position   ln_pos_latest_start;
161
162         /* Position for the last LFSCK checkpoint. */
163         struct lfsck_position   ln_pos_last_checkpoint;
164
165         /* Position for the first should be updated object. */
166         struct lfsck_position   ln_pos_first_inconsistent;
167
168         /* How many items (including dir) have been checked. */
169         __u64   ln_items_checked;
170
171         /* How many items have been repaired. */
172         __u64   ln_items_repaired;
173
174         /* How many items failed to be processed. */
175         __u64   ln_items_failed;
176
177         /* How many directories have been traversed. */
178         __u64   ln_dirs_checked;
179
180         /* How many objects have been double scanned. */
181         __u64   ln_objs_checked_phase2;
182
183         /* How many objects have been reparied during double scan. */
184         __u64   ln_objs_repaired_phase2;
185
186         /* How many objects failed to be processed during double scan. */
187         __u64   ln_objs_failed_phase2;
188
189         /* How many objects with nlink fixed. */
190         __u64   ln_objs_nlink_repaired;
191
192         /* The latest object has been processed (failed) during double scan. */
193         struct lu_fid   ln_fid_latest_scanned_phase2;
194
195         /* How many FID-in-dirent entries have been repaired. */
196         __u64   ln_dirent_repaired;
197
198         /* How many linkEA entries have been repaired. */
199         __u64   ln_linkea_repaired;
200
201         /* How many multiple-linked objects have been checked. */
202         __u64   ln_mul_linked_checked;
203
204         /* How many multiple-linked objects have been repaired. */
205         __u64   ln_mul_linked_repaired;
206
207         /* How many undefined inconsistency found in phase2. */
208         __u64   ln_unknown_inconsistency;
209
210         /* How many unmatched pairs have been repaired. */
211         __u64   ln_unmatched_pairs_repaired;
212
213         /* How many dangling name entries have been found/repaired. */
214         __u64   ln_dangling_repaired;
215
216         /* How many multiple referenced name entries have been
217          * found/repaired. */
218         __u64   ln_mul_ref_repaired;
219
220         /* How many name entries with bad file type have been repaired. */
221         __u64   ln_bad_type_repaired;
222
223         /* How many lost name entries have been re-inserted. */
224         __u64   ln_lost_dirent_repaired;
225
226         /* How many objects under /lost+found have been scanned. */
227         __u64   ln_local_lpf_scanned;
228
229         /* How many objects under /lost+found have been moved to
230          * namespace visible directory. */
231         __u64   ln_local_lpf_moved;
232
233         /* How many objects under /lost+found have been skipped. */
234         __u64   ln_local_lpf_skipped;
235
236         /* How many objects under /lost+found failed to be processed. */
237         __u64   ln_local_lpf_failed;
238
239         /* How many striped directories (master) have been scanned. */
240         __u64   ln_striped_dirs_scanned;
241
242         /* How many striped directories (master) have been repaired. */
243         __u64   ln_striped_dirs_repaired;
244
245         /* How many striped directories (master) failed verification. */
246         __u64   ln_striped_dirs_failed;
247
248         /* How many striped directories (master) has been disabled. */
249         __u64   ln_striped_dirs_disabled;
250
251         /* How many striped directory's (master) have been skipped
252          * (for shards verification) because of lost master LMV EA. */
253         __u64   ln_striped_dirs_skipped;
254
255         /* How many striped directory's shards (slave) have been scanned. */
256         __u64   ln_striped_shards_scanned;
257
258         /* How many striped directory's shards (slave) have been repaired. */
259         __u64   ln_striped_shards_repaired;
260
261         /* How many striped directory's shards (slave) failed verification. */
262         __u64   ln_striped_shards_failed;
263
264         /* How many striped directory's shards (slave) have been skipped
265          * (for name hash verification) because do not know whether the slave
266          * LMV EA is valid or not. */
267         __u64   ln_striped_shards_skipped;
268
269         /* How many name entries under striped directory with bad name
270          * hash have been repaired. */
271         __u64   ln_name_hash_repaired;
272
273         /* The size of MDT targets bitmap with nbits. Such bitmap records
274          * the MDTs that contain non-verified MDT-objects. */
275         __u32   ln_bitmap_size;
276
277         __u32   ln_reserved_1;
278         /* For further using. 256-bytes aligned now. */
279         __u64   ln_reserved[15];
280 };
281
282 enum lfsck_layout_inconsistency_type {
283         LLIT_NONE                       = 0,
284         LLIT_DANGLING                   = 1,
285         LLIT_UNMATCHED_PAIR             = 2,
286         LLIT_MULTIPLE_REFERENCED        = 3,
287         LLIT_ORPHAN                     = 4,
288         LLIT_INCONSISTENT_OWNER         = 5,
289         LLIT_OTHERS                     = 6,
290         LLIT_MAX                        = LLIT_OTHERS
291 };
292
293 struct lfsck_layout {
294         /* Magic number to detect that this struct contains valid data. */
295         __u32   ll_magic;
296
297         /* See 'enum lfsck_status'. */
298         __u32   ll_status;
299
300         /* See 'enum lfsck_flags'. */
301         __u32   ll_flags;
302
303         /* How many completed LFSCK runs on the device. */
304         __u32   ll_success_count;
305
306         /*  How long the LFSCK phase1 has run in seconds. */
307         __u32   ll_run_time_phase1;
308
309         /*  How long the LFSCK phase2 has run in seconds. */
310         __u32   ll_run_time_phase2;
311
312         /* Time for the last LFSCK completed in seconds since epoch. */
313         __u64   ll_time_last_complete;
314
315         /* Time for the latest LFSCK ran in seconds since epoch. */
316         __u64   ll_time_latest_start;
317
318         /* Time for the last LFSCK checkpoint in seconds since epoch. */
319         __u64   ll_time_last_checkpoint;
320
321         /* Position for the latest LFSCK started from. */
322         __u64   ll_pos_latest_start;
323
324         /* Position for the last LFSCK checkpoint. */
325         __u64   ll_pos_last_checkpoint;
326
327         /* Position for the first object to be fixed or
328          * failed to be checked in the phase1. */
329         __u64   ll_pos_first_inconsistent;
330
331         /* How many objects have been checked. */
332         __u64   ll_objs_checked_phase1;
333
334         /* How many objects failed to be processed. */
335         __u64   ll_objs_failed_phase1;
336
337         /* How many objects have been double scanned. */
338         __u64   ll_objs_checked_phase2;
339
340         /* How many objects failed to be processed during double scan. */
341         __u64   ll_objs_failed_phase2;
342
343         /* kinds of inconsistency have been or to be repaired.
344          * ll_objs_repaired[type - 1] is the count for the given @type. */
345         __u64   ll_objs_repaired[LLIT_MAX];
346
347         /* How many objects have been skipped because of related
348          * MDT(s)/OST(s) do not participate in the LFSCK */
349         __u64   ll_objs_skipped;
350
351         /* The size of ll_ost_bitmap with nbits. */
352         __u32   ll_bitmap_size;
353
354         /* For further using. 256-bytes aligned now. */
355         __u32   ll_reserved_1;
356         __u64   ll_reserved_2[11];
357
358         /* The OST targets bitmap to record the OSTs that contain
359          * non-verified OST-objects. */
360         __u8    ll_ost_bitmap[0];
361 };
362
363 struct lfsck_assistant_object {
364         struct lu_fid           lso_fid;
365         __u64                   lso_oit_cookie;
366         struct lu_attr          lso_attr;
367         atomic_t                lso_ref;
368         unsigned int            lso_dead:1,
369                                 lso_is_dir:1;
370 };
371
372 struct lfsck_component;
373 struct lfsck_tgt_descs;
374 struct lfsck_tgt_desc;
375
376 struct lfsck_operations {
377         int (*lfsck_reset)(const struct lu_env *env,
378                            struct lfsck_component *com,
379                            bool init);
380
381         void (*lfsck_fail)(const struct lu_env *env,
382                            struct lfsck_component *com,
383                            bool new_checked);
384
385         void (*lfsck_close_dir)(const struct lu_env *env,
386                                 struct lfsck_component *com);
387
388         int (*lfsck_open_dir)(const struct lu_env *env,
389                               struct lfsck_component *com);
390
391         int (*lfsck_checkpoint)(const struct lu_env *env,
392                                 struct lfsck_component *com,
393                                 bool init);
394
395         int (*lfsck_prep)(const struct lu_env *env,
396                           struct lfsck_component *com,
397                           struct lfsck_start_param *lsp);
398
399         int (*lfsck_exec_oit)(const struct lu_env *env,
400                               struct lfsck_component *com,
401                               struct dt_object *obj);
402
403         int (*lfsck_exec_dir)(const struct lu_env *env,
404                               struct lfsck_component *com,
405                               struct lfsck_assistant_object *lso,
406                               struct lu_dirent *ent,
407                               __u16 type);
408
409         int (*lfsck_post)(const struct lu_env *env,
410                           struct lfsck_component *com,
411                           int result,
412                           bool init);
413
414         void (*lfsck_dump)(const struct lu_env *env,
415                            struct lfsck_component *com,
416                            struct seq_file *m);
417
418         int (*lfsck_double_scan)(const struct lu_env *env,
419                                  struct lfsck_component *com);
420
421         void (*lfsck_data_release)(const struct lu_env *env,
422                                    struct lfsck_component *com);
423
424         void (*lfsck_quit)(const struct lu_env *env,
425                            struct lfsck_component *com);
426
427         int (*lfsck_in_notify)(const struct lu_env *env,
428                                struct lfsck_component *com,
429                                struct lfsck_request *lr,
430                                struct thandle *th);
431
432         int (*lfsck_query)(const struct lu_env *env,
433                            struct lfsck_component *com,
434                            struct lfsck_request *req,
435                            struct lfsck_reply *rep,
436                            struct lfsck_query *que, int idx);
437
438         int (*lfsck_join)(const struct lu_env *env,
439                           struct lfsck_component *com,
440                           struct lfsck_start_param *lsp);
441 };
442
443 #define TGT_PTRS                256     /* number of pointers at 1st level */
444 #define TGT_PTRS_PER_BLOCK      256     /* number of pointers at 2nd level */
445
446 struct lfsck_tgt_desc {
447         struct list_head   ltd_orphan_list;
448         struct dt_device  *ltd_tgt;
449         struct dt_device  *ltd_key;
450         struct obd_export *ltd_exp;
451         struct list_head   ltd_layout_list;
452         struct list_head   ltd_layout_phase_list;
453         struct list_head   ltd_namespace_list;
454         struct list_head   ltd_namespace_phase_list;
455         __u32              ltd_layout_status;
456         __u32              ltd_namespace_status;
457         __u64              ltd_layout_repaired;
458         __u64              ltd_namespace_repaired;
459         atomic_t           ltd_ref;
460         __u32              ltd_index;
461         __u32              ltd_layout_gen;
462         __u32              ltd_namespace_gen;
463         unsigned int       ltd_dead:1,
464                            ltd_layout_done:1,
465                            ltd_namespace_done:1,
466                            ltd_synced_failures:1;
467 };
468
469 struct lfsck_tgt_desc_idx {
470         struct lfsck_tgt_desc *ldi_tgts[TGT_PTRS_PER_BLOCK];
471 };
472
473 struct lfsck_tgt_descs {
474         /* list of known TGTs */
475         struct lfsck_tgt_desc_idx       *ltd_tgts_idx[TGT_PTRS];
476
477         /* bitmap of TGTs available */
478         struct cfs_bitmap                       *ltd_tgts_bitmap;
479
480         /* for lfsck_tgt_desc::ltd_xxx_list */
481         spinlock_t                       ltd_lock;
482
483         /* for tgts table accessing and changes */
484         struct rw_semaphore              ltd_rw_sem;
485
486         /* Temporary list for orphan targets. */
487         struct list_head                 ltd_orphan;
488
489         /* number of registered TGTs */
490         __u32                            ltd_tgtnr;
491 };
492
493 static inline struct lfsck_tgt_desc *
494 lfsck_ltd2tgt(struct lfsck_tgt_descs *ltd, __u32 index)
495 {
496         __u32 idx1 = index / TGT_PTRS_PER_BLOCK;
497         __u32 idx2 = index % TGT_PTRS_PER_BLOCK;
498         struct lfsck_tgt_desc *__tgt = NULL;
499
500         if (unlikely(idx1 >= TGT_PTRS))
501                 CDEBUG(D_LFSCK, "The target idx %u is invalid.\n", index);
502         else if (likely(ltd->ltd_tgts_idx[idx1] != NULL))
503                 __tgt = ltd->ltd_tgts_idx[idx1]->ldi_tgts[idx2];
504
505         return __tgt;
506 }
507
508 static inline void lfsck_assign_tgt(struct lfsck_tgt_descs *ltd,
509                                     struct lfsck_tgt_desc *tgt, __u32 index)
510 {
511         __u32 idx1 = index / TGT_PTRS_PER_BLOCK;
512         __u32 idx2 = index % TGT_PTRS_PER_BLOCK;
513
514         if (likely(idx1 < TGT_PTRS && ltd->ltd_tgts_idx[idx1] != NULL))
515                 ltd->ltd_tgts_idx[idx1]->ldi_tgts[idx2] = tgt;
516 }
517
518 #define LFSCK_STF_BITS  4
519 /* If want to adjust the LFSCK_STF_COUNT, please change LFSCK_STF_BITS. */
520 #define LFSCK_STF_COUNT (1 << LFSCK_STF_BITS)
521
522 struct lfsck_sub_trace_obj {
523         struct dt_object        *lsto_obj;
524         struct mutex             lsto_mutex;
525 };
526
527 struct lfsck_component {
528         /* into lfsck_instance::li_list_(scan,double_scan,idle} */
529         struct list_head         lc_link;
530
531         /* into lfsck_instance::li_list_dir */
532         struct list_head         lc_link_dir;
533
534         struct rw_semaphore      lc_sem;
535         atomic_t                 lc_ref;
536
537         struct lfsck_position    lc_pos_start;
538         struct lfsck_instance   *lc_lfsck;
539         struct dt_object        *lc_obj;
540         struct lfsck_sub_trace_obj lc_sub_trace_objs[LFSCK_STF_COUNT];
541         struct lfsck_operations *lc_ops;
542         void                    *lc_file_ram;
543         void                    *lc_file_disk;
544         void                    *lc_data;
545         struct lu_fid            lc_fid_latest_scanned_phase2;
546
547         /* The time for last checkpoint, jiffies */
548         cfs_time_t               lc_time_last_checkpoint;
549
550         /* The time for next checkpoint, jiffies */
551         cfs_time_t               lc_time_next_checkpoint;
552
553         __u32                    lc_file_size;
554
555         /* How many objects have been checked since last checkpoint. */
556         __u32                    lc_new_checked;
557
558         /* How many objects have been scanned since last sleep. */
559         __u32                    lc_new_scanned;
560
561         __u16                    lc_type;
562 };
563
564 #define LFSCK_LMV_MAX_STRIPES   LMV_MAX_STRIPE_COUNT
565 #define LFSCK_LMV_DEF_STRIPES   4
566
567 /* Warning: NOT change the lfsck_slave_lmv_flags members order,
568  *          otherwise the lfsck_record_lmv() may be wrong. */
569 enum lfsck_slave_lmv_flags {
570         LSLF_NONE       = 0,
571         LSLF_BAD_INDEX2 = 1,
572         LSLF_NO_LMVEA   = 2,
573         LSLF_DANGLING   = 3,
574         LSLF_BAD_INDEX1 = 4,
575 };
576
577 /* When the namespace LFSCK scans a striped directory, it will record all
578  * the known shards' information in the structure "lfsck_slave_lmv_rec",
579  * including the shard's FID, index, slave LMV EA, and so on. Each shard
580  * will take one lfsck_slave_lmv_rec slot. After the 1st cycle scanning
581  * the striped directory, the LFSCK will get all the information about
582  * whether there are some inconsistency, and then it can repair them in
583  * the 2nd cycle scanning. */
584 struct lfsck_slave_lmv_rec {
585         struct lu_fid   lslr_fid;
586         __u32           lslr_stripe_count;
587         __u32           lslr_index; /* the index in name or in slave lmv */
588         __u32           lslr_hash_type;
589         __u32           lslr_flags;
590 };
591
592 struct lfsck_lmv {
593         struct lmv_mds_md_v1             ll_lmv;
594         atomic_t                         ll_ref;
595         int                              ll_stripes_allocated;
596         int                              ll_stripes_filled;
597         int                              ll_exit_value;
598         __u32                            ll_max_stripe_count;
599         __u32                            ll_max_filled_off;
600         __u32                            ll_hash_type;
601         unsigned int                     ll_lmv_master:1,
602                                          ll_lmv_slave:1,
603                                          ll_lmv_verified:1,
604                                          ll_lmv_updated:1,
605                                          ll_inline:1,
606                                          ll_failed:1,
607                                          ll_ignore:1;
608         struct lfsck_slave_lmv_rec      *ll_lslr; /* may be vmalloc'd */
609 };
610
611 /* If the namespace LFSCK finds that the master MDT-object of a striped
612  * directory lost its master LMV EA, it will re-generate the master LMV
613  * EA and notify the LFSCK instance on the MDT on which the striped dir
614  * master MDT-object resides to rescan the striped directory. To do that,
615  * the notify handler will insert a "lfsck_lmv_unit" structure into the
616  * lfsck::li_list_lmv. The LFSCK instance will scan such list from time
617  * to time to check whether needs to rescan some stirped directories. */
618 struct lfsck_lmv_unit {
619         struct list_head         llu_link;
620         struct lfsck_lmv         llu_lmv;
621         struct dt_object        *llu_obj;
622         struct lfsck_instance   *llu_lfsck;
623 };
624
625 struct lfsck_rec_lmv_save {
626         struct lu_fid           lrls_fid;
627         struct lmv_mds_md_v1    lrls_lmv;
628 };
629
630 /* Allow lfsck_record_lmv() to be called recursively at most three times. */
631 #define LFSCK_REC_LMV_MAX_DEPTH 3
632
633 struct lfsck_instance {
634         struct mutex              li_mutex;
635         spinlock_t                li_lock;
636
637         /* Link into the lfsck_instance_list. */
638         struct list_head          li_link;
639
640         /* For the components in (first) scanning via otable-based iteration. */
641         struct list_head          li_list_scan;
642
643         /* For the components in scanning via directory traversal. Because
644          * directory traversal cannot guarantee all the object be scanned,
645          * so the component in the li_list_dir must be in li_list_scan. */
646         struct list_head          li_list_dir;
647
648         /* For the components in double scanning. */
649         struct list_head          li_list_double_scan;
650
651         /* For the components those are not scanning now. */
652         struct list_head          li_list_idle;
653
654         /* For the lfsck_lmv_unit to be handled. */
655         struct list_head          li_list_lmv;
656
657         atomic_t                  li_ref;
658         atomic_t                  li_double_scan_count;
659         struct ptlrpc_thread      li_thread;
660
661         /* The time for last checkpoint, jiffies */
662         cfs_time_t                li_time_last_checkpoint;
663
664         /* The time for next checkpoint, jiffies */
665         cfs_time_t                li_time_next_checkpoint;
666
667         lfsck_out_notify          li_out_notify;
668         void                     *li_out_notify_data;
669         struct dt_device         *li_next;
670         struct dt_device         *li_bottom;
671         struct obd_device        *li_obd;
672         struct ldlm_namespace    *li_namespace;
673         struct local_oid_storage *li_los;
674         struct lu_fid             li_local_root_fid;  /* backend root "/" */
675         struct lu_fid             li_global_root_fid; /* /ROOT */
676         struct dt_object         *li_lfsck_dir;
677         struct dt_object         *li_bookmark_obj;
678         struct dt_object         *li_lpf_obj;
679         struct dt_object         *li_lpf_root_obj;
680         struct lu_client_seq     *li_seq;
681         struct lfsck_bookmark     li_bookmark_ram;
682         struct lfsck_bookmark     li_bookmark_disk;
683         struct lfsck_position     li_pos_current;
684         struct lfsck_position     li_pos_checkpoint;
685
686         struct lfsck_lmv         *li_lmv;
687
688         /* Obj for otable-based iteration */
689         struct dt_object         *li_obj_oit;
690
691         /* Obj for directory traversal */
692         struct dt_object         *li_obj_dir;
693
694         /* It for otable-based iteration */
695         struct dt_it             *li_di_oit;
696
697         /* It for directory traversal */
698         struct dt_it             *li_di_dir;
699
700         /* Description of OST */
701         struct lfsck_tgt_descs    li_ost_descs;
702
703         /* Description of MDT */
704         struct lfsck_tgt_descs    li_mdt_descs;
705
706         /* namespace-based directory traversal position. */
707         __u64                     li_cookie_dir;
708
709         /* Arguments for low layer otable-based iteration. */
710         __u32                     li_args_oit;
711
712         /* Arugments for namespace-based directory traversal. */
713         __u32                     li_args_dir;
714
715         /* Schedule for every N objects. */
716         __u32                     li_sleep_rate;
717
718         /* Sleep N jiffies for each schedule. */
719         __u32                     li_sleep_jif;
720
721         /* How many objects have been scanned since last sleep. */
722         __u32                     li_new_scanned;
723
724         /* The status when the LFSCK stopped or paused. */
725         __u32                     li_status;
726
727         /* The flags when the lFSCK stopped or paused. */
728         __u32                     li_flags;
729
730         unsigned int              li_oit_over:1, /* oit is finished. */
731                                   li_drop_dryrun:1, /* Ever dryrun, not now. */
732                                   li_master:1, /* Master instance or not. */
733                                   li_current_oit_processed:1,
734                                   li_start_unplug:1;
735         struct lfsck_rec_lmv_save li_rec_lmv_save[LFSCK_REC_LMV_MAX_DEPTH];
736 };
737
738 struct lfsck_async_interpret_args {
739         struct lfsck_component          *laia_com;
740         struct lfsck_tgt_descs          *laia_ltds;
741         struct lfsck_tgt_desc           *laia_ltd;
742         struct lfsck_request            *laia_lr;
743         atomic_t                        *laia_count;
744         int                              laia_result;
745         unsigned int                     laia_shared:1;
746 };
747
748 struct lfsck_thread_args {
749         struct lu_env                    lta_env;
750         struct lfsck_instance           *lta_lfsck;
751         struct lfsck_component          *lta_com;
752         struct lfsck_start_param        *lta_lsp;
753 };
754
755 struct lfsck_assistant_req {
756         struct list_head                 lar_list;
757         struct lfsck_assistant_object   *lar_parent;
758 };
759
760 struct lfsck_namespace_req {
761         struct lfsck_assistant_req       lnr_lar;
762         struct lfsck_lmv                *lnr_lmv;
763         struct lu_fid                    lnr_fid;
764         __u64                            lnr_dir_cookie;
765         __u32                            lnr_attr;
766         __u32                            lnr_size;
767         __u16                            lnr_type;
768         __u16                            lnr_namelen;
769         char                             lnr_name[0];
770 };
771
772 struct lfsck_layout_req {
773         struct lfsck_assistant_req       llr_lar;
774         struct dt_object                *llr_child;
775         __u32                            llr_ost_idx;
776         __u32                            llr_lov_idx; /* offset in LOV EA */
777 };
778
779 struct lfsck_assistant_operations {
780         int (*la_handler_p1)(const struct lu_env *env,
781                              struct lfsck_component *com,
782                              struct lfsck_assistant_req *lar);
783
784         int (*la_handler_p2)(const struct lu_env *env,
785                              struct lfsck_component *com);
786
787         void (*la_fill_pos)(const struct lu_env *env,
788                             struct lfsck_component *com,
789                             struct lfsck_position *pos);
790
791         int (*la_double_scan_result)(const struct lu_env *env,
792                                      struct lfsck_component *com,
793                                      int rc);
794
795         void (*la_req_fini)(const struct lu_env *env,
796                             struct lfsck_assistant_req *lar);
797
798         void (*la_sync_failures)(const struct lu_env *env,
799                                  struct lfsck_component *com,
800                                  struct lfsck_request *lr);
801 };
802
803 struct lfsck_assistant_data {
804         spinlock_t                               lad_lock;
805         struct list_head                         lad_req_list;
806
807         /* list for the ost targets involve LFSCK. */
808         struct list_head                         lad_ost_list;
809
810         /* list for the ost targets in phase1 scanning. */
811         struct list_head                         lad_ost_phase1_list;
812
813         /* list for the ost targets in phase2 scanning. */
814         struct list_head                         lad_ost_phase2_list;
815
816         /* list for the mdt targets involve LFSCK. */
817         struct list_head                         lad_mdt_list;
818
819         /* list for the mdt targets in phase1 scanning. */
820         struct list_head                         lad_mdt_phase1_list;
821
822         /* list for the mdt targets in phase2 scanning. */
823         struct list_head                         lad_mdt_phase2_list;
824
825         const char                              *lad_name;
826         struct ptlrpc_thread                     lad_thread;
827         struct task_struct                      *lad_task;
828
829         struct lfsck_assistant_operations       *lad_ops;
830
831         struct cfs_bitmap                               *lad_bitmap;
832
833         __u32                                    lad_touch_gen;
834         int                                      lad_prefetched;
835         int                                      lad_assistant_status;
836         int                                      lad_post_result;
837         unsigned int                             lad_to_post:1,
838                                                  lad_to_double_scan:1,
839                                                  lad_in_double_scan:1,
840                                                  lad_exit:1,
841                                                  lad_incomplete:1;
842         bool                                     lad_advance_lock;
843 };
844
845 #define LFSCK_TMPBUF_LEN        64
846
847 struct lfsck_lock_handle {
848         struct lustre_handle    llh_pdo_lh;
849         struct lustre_handle    llh_reg_lh;
850         enum ldlm_mode          llh_pdo_mode;
851         enum ldlm_mode          llh_reg_mode;
852 };
853
854 struct lfsck_thread_info {
855         struct lu_name          lti_name_const;
856         struct lu_name          lti_name;
857         struct lu_name          lti_name2;
858         struct lu_buf           lti_buf;
859         struct lu_buf           lti_linkea_buf;
860         struct lu_buf           lti_linkea_buf2;
861         struct lu_buf           lti_big_buf;
862         struct lu_fid           lti_fid;
863         struct lu_fid           lti_fid2;
864         struct lu_fid           lti_fid3;
865         struct lu_attr          lti_la;
866         struct lu_attr          lti_la2;
867         struct ost_id           lti_oi;
868         union {
869                 struct lustre_mdt_attrs lti_lma;
870                 /* old LMA for compatibility */
871                 char                    lti_lma_old[LMA_OLD_SIZE];
872         };
873         struct dt_object_format lti_dof;
874         /* There will be '\0' at the end of the name. */
875         char            lti_key[sizeof(struct lu_dirent) + NAME_MAX + 1];
876         char                    lti_tmpbuf[LFSCK_TMPBUF_LEN];
877         char                    lti_tmpbuf2[LFSCK_TMPBUF_LEN];
878         struct lfsck_request    lti_lr;
879         struct lfsck_async_interpret_args lti_laia;
880         struct lfsck_async_interpret_args lti_laia2;
881         struct lfsck_start      lti_start;
882         struct lfsck_stop       lti_stop;
883         union ldlm_policy_data  lti_policy;
884         struct ldlm_enqueue_info lti_einfo;
885         struct ldlm_res_id      lti_resid;
886         union {
887                 struct filter_fid_old   lti_old_pfid;
888                 struct filter_fid       lti_new_pfid;
889         };
890         struct dt_allocation_hint lti_hint;
891         struct lu_orphan_rec    lti_rec;
892         struct lov_user_md      lti_lum;
893         struct dt_insert_rec    lti_dt_rec;
894         struct lu_object_conf   lti_conf;
895         struct lu_seq_range     lti_range;
896         struct lmv_mds_md_v1    lti_lmv;
897         struct lmv_mds_md_v1    lti_lmv2;
898         struct lmv_mds_md_v1    lti_lmv3;
899         struct lmv_mds_md_v1    lti_lmv4;
900         struct lfsck_lock_handle lti_llh;
901 };
902
903 /* lfsck_lib.c */
904 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
905                     struct lu_fid *fid, bool locked);
906 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
907                      struct dt_object *obj, struct lustre_handle *lh,
908                      __u64 bits, enum ldlm_mode mode);
909 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode);
910 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
911                struct dt_object *obj, const char *name,
912                struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode);
913 void lfsck_unlock(struct lfsck_lock_handle *llh);
914 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
915                               struct lfsck_instance *lfsck,
916                               const struct lu_fid *fid);
917 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck);
918 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
919                                            bool unlink);
920 struct lfsck_component *lfsck_component_find(struct lfsck_instance *lfsck,
921                                              __u16 type);
922 void lfsck_component_cleanup(const struct lu_env *env,
923                              struct lfsck_component *com);
924 void lfsck_instance_cleanup(const struct lu_env *env,
925                             struct lfsck_instance *lfsck);
926 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
927                      const char *prefix);
928 void lfsck_time_dump(struct seq_file *m, __u64 time, const char *name);
929 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
930                     const char *prefix);
931 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
932                     struct lfsck_position *pos, bool init);
933 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit);
934 void lfsck_control_speed(struct lfsck_instance *lfsck);
935 void lfsck_control_speed_by_self(struct lfsck_component *com);
936 void lfsck_thread_args_fini(struct lfsck_thread_args *lta);
937 struct lfsck_assistant_data *
938 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
939                           const char *name);
940 struct lfsck_assistant_object *
941 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
942                             const struct lu_attr *attr, __u64 cookie,
943                             bool is_dir);
944 struct dt_object *
945 lfsck_assistant_object_load(const struct lu_env *env,
946                             struct lfsck_instance *lfsck,
947                             struct lfsck_assistant_object *lso);
948 int lfsck_async_interpret_common(const struct lu_env *env,
949                                  struct ptlrpc_request *req,
950                                  void *args, int rc);
951 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
952                         struct lfsck_request *lr,
953                         struct ptlrpc_request_set *set,
954                         ptlrpc_interpterer_t interpterer,
955                         void *args, int request);
956 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com);
957 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
958                           struct lfsck_start_param *lsp);
959 int lfsck_checkpoint_generic(const struct lu_env *env,
960                              struct lfsck_component *com);
961 void lfsck_post_generic(const struct lu_env *env,
962                         struct lfsck_component *com, int *result);
963 int lfsck_double_scan_generic(const struct lu_env *env,
964                               struct lfsck_component *com, int status);
965 void lfsck_quit_generic(const struct lu_env *env,
966                         struct lfsck_component *com);
967
968 /* lfsck_engine.c */
969 int lfsck_unpack_ent(struct lu_dirent *ent, __u64 *cookie, __u16 *type);
970 void lfsck_close_dir(const struct lu_env *env,
971                      struct lfsck_instance *lfsck, int result);
972 int lfsck_open_dir(const struct lu_env *env,
973                    struct lfsck_instance *lfsck, __u64 cookie);
974 int lfsck_master_engine(void *args);
975 int lfsck_assistant_engine(void *args);
976
977 /* lfsck_bookmark.c */
978 void lfsck_bookmark_cpu_to_le(struct lfsck_bookmark *des,
979                               struct lfsck_bookmark *src);
980 int lfsck_bookmark_store(const struct lu_env *env,
981                          struct lfsck_instance *lfsck);
982 int lfsck_bookmark_setup(const struct lu_env *env,
983                          struct lfsck_instance *lfsck);
984 int lfsck_set_param(const struct lu_env *env, struct lfsck_instance *lfsck,
985                     struct lfsck_start *start, bool reset);
986
987 /* lfsck_namespace.c */
988 int lfsck_namespace_trace_update(const struct lu_env *env,
989                                  struct lfsck_component *com,
990                                  const struct lu_fid *fid,
991                                  const __u8 flags, bool add);
992 int lfsck_namespace_check_exist(const struct lu_env *env,
993                                 struct dt_object *dir,
994                                 struct dt_object *obj, const char *name);
995 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
996                        struct linkea_data *ldata);
997 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
998                                    struct lfsck_component *com,
999                                    struct dt_object *obj,
1000                                    struct linkea_data *ldata);
1001 int lfsck_namespace_repair_dangling(const struct lu_env *env,
1002                                     struct lfsck_component *com,
1003                                     struct dt_object *parent,
1004                                     struct dt_object *child,
1005                                     struct lfsck_namespace_req *lnr);
1006 int lfsck_namespace_repair_dirent(const struct lu_env *env,
1007                                   struct lfsck_component *com,
1008                                   struct dt_object *parent,
1009                                   struct dt_object *child,
1010                                   const char *name, const char *name2,
1011                                   __u16 type, bool update, bool dec);
1012 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
1013                         const struct lu_name *cname, const struct lu_fid *pfid);
1014 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
1015                           char *name, struct lu_fid *pfid);
1016 int lfsck_update_name_entry(const struct lu_env *env,
1017                             struct lfsck_instance *lfsck,
1018                             struct dt_object *dir, const char *name,
1019                             const struct lu_fid *fid, __u32 type);
1020 int lfsck_namespace_setup(const struct lu_env *env,
1021                           struct lfsck_instance *lfsck);
1022
1023 /* lfsck_striped_dir.c */
1024 void lfsck_lmv_put(const struct lu_env *env, struct lfsck_lmv *llmv);
1025 int lfsck_read_stripe_lmv(const struct lu_env *env, struct dt_object *obj,
1026                           struct lmv_mds_md_v1 *lmv);
1027 int lfsck_shard_name_to_index(const struct lu_env *env, const char *name,
1028                               int namelen, __u16 type,
1029                               const struct lu_fid *fid);
1030 bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
1031                                      struct lfsck_lmv *llmv,
1032                                      const char *name, int namelen);
1033 int lfsck_namespace_check_name(const struct lu_env *env,
1034                                struct dt_object *parent,
1035                                struct dt_object *child,
1036                                const struct lu_name *cname);
1037 int lfsck_namespace_update_lmv(const struct lu_env *env,
1038                                struct lfsck_component *com,
1039                                struct dt_object *obj,
1040                                struct lmv_mds_md_v1 *lmv, bool locked);
1041 int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
1042                                         struct lfsck_component *com,
1043                                         struct dt_object *obj,
1044                                         struct lfsck_lmv *llmv);
1045 int lfsck_namespace_scan_shard(const struct lu_env *env,
1046                                struct lfsck_component *com,
1047                                struct dt_object *child);
1048 int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
1049                                             struct lfsck_component *com,
1050                                             struct dt_object *obj);
1051 int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
1052                                          struct lfsck_component *com,
1053                                          struct dt_object *shard,
1054                                          struct lfsck_lmv *llmv,
1055                                          const char *name);
1056 int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
1057                                        struct lfsck_component *com,
1058                                        struct lfsck_namespace_req *lnr);
1059 int lfsck_namespace_handle_striped_master(const struct lu_env *env,
1060                                           struct lfsck_component *com,
1061                                           struct lfsck_namespace_req *lnr);
1062
1063 /* lfsck_layout.c */
1064 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
1065
1066 extern const char dot[];
1067 extern const char dotdot[];
1068 extern const char *lfsck_flags_names[];
1069 extern const char *lfsck_param_names[];
1070 extern struct lu_context_key lfsck_thread_key;
1071
1072 static inline bool name_is_dot_or_dotdot(const char *name, int namelen)
1073 {
1074         return name[0] == '.' &&
1075                (namelen == 1 || (namelen == 2 && name[1] == '.'));
1076 }
1077
1078 static inline struct dt_device *lfsck_obj2dev(struct dt_object *obj)
1079 {
1080         return container_of0(obj->do_lu.lo_dev, struct dt_device, dd_lu_dev);
1081 }
1082
1083 static inline struct lfsck_thread_info *
1084 lfsck_env_info(const struct lu_env *env)
1085 {
1086         struct lfsck_thread_info *info;
1087
1088         info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
1089         LASSERT(info != NULL);
1090         return info;
1091 }
1092
1093 static inline const struct lu_name *
1094 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
1095 {
1096         struct lu_name *lname;
1097
1098         lname = &lfsck_env_info(env)->lti_name_const;
1099         lname->ln_name = area;
1100         lname->ln_namelen = len;
1101         return lname;
1102 }
1103
1104 static inline void
1105 lfsck_buf_init(struct lu_buf *buf, void *area, ssize_t len)
1106 {
1107         buf->lb_buf = area;
1108         buf->lb_len = len;
1109 }
1110
1111 static inline struct lu_buf *
1112 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
1113 {
1114         struct lu_buf *buf;
1115
1116         buf = &lfsck_env_info(env)->lti_buf;
1117         buf->lb_buf = area;
1118         buf->lb_len = len;
1119         return buf;
1120 }
1121
1122 static inline const struct lu_buf *
1123 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
1124 {
1125         struct lu_buf *buf;
1126
1127         buf = &lfsck_env_info(env)->lti_buf;
1128         buf->lb_buf = (void *)area;
1129         buf->lb_len = len;
1130         return buf;
1131 }
1132
1133 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
1134 {
1135         return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
1136 }
1137
1138 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
1139 {
1140         return lu_object_fid(&obj->do_lu);
1141 }
1142
1143 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
1144 {
1145         memset(pos, 0, sizeof(*pos));
1146 }
1147
1148 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
1149 {
1150         return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
1151 }
1152
1153 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
1154                                   const struct lfsck_position *pos2)
1155 {
1156         if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
1157                 return -1;
1158
1159         if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
1160                 return 1;
1161
1162         if (fid_is_zero(&pos1->lp_dir_parent) &&
1163             !fid_is_zero(&pos2->lp_dir_parent))
1164                 return -1;
1165
1166         if (!fid_is_zero(&pos1->lp_dir_parent) &&
1167             fid_is_zero(&pos2->lp_dir_parent))
1168                 return 1;
1169
1170         if (fid_is_zero(&pos1->lp_dir_parent) &&
1171             fid_is_zero(&pos2->lp_dir_parent))
1172                 return 0;
1173
1174         LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
1175
1176         if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
1177                 return -1;
1178
1179         if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
1180                 return 1;
1181
1182         return 0;
1183 }
1184
1185 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
1186                                             struct lfsck_position *src)
1187 {
1188         des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
1189         fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
1190         des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
1191 }
1192
1193 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
1194                                             struct lfsck_position *src)
1195 {
1196         des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
1197         fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
1198         des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
1199 }
1200
1201 static inline umode_t lfsck_object_type(const struct dt_object *obj)
1202 {
1203         return lu_object_attr(&obj->do_lu);
1204 }
1205
1206 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
1207 {
1208         struct lu_object_header *loh = obj->do_lu.lo_header;
1209
1210         return !!test_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1211 }
1212
1213 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
1214 {
1215         lu_object_get(&obj->do_lu);
1216         return obj;
1217 }
1218
1219 static inline void lfsck_object_put(const struct lu_env *env,
1220                                     struct dt_object *obj)
1221 {
1222         lu_object_put(env, &obj->do_lu);
1223 }
1224
1225 static inline struct seq_server_site
1226 *lfsck_dev_site(struct lfsck_instance *lfsck)
1227 {
1228         return lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1229 }
1230
1231 static inline u32 lfsck_dev_idx(struct lfsck_instance *lfsck)
1232 {
1233         return lfsck_dev_site(lfsck)->ss_node_id;
1234 }
1235
1236 static inline struct dt_object *
1237 lfsck_object_find_by_dev_new(const struct lu_env *env, struct dt_device *dev,
1238                              const struct lu_fid *fid)
1239 {
1240         struct lu_object_conf   *conf = &lfsck_env_info(env)->lti_conf;
1241
1242         conf->loc_flags = LOC_F_NEW;
1243         return lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, conf));
1244 }
1245
1246 static inline struct dt_object *
1247 lfsck_object_find_by_dev_nowait(const struct lu_env *env, struct dt_device *dev,
1248                                 const struct lu_fid *fid)
1249 {
1250         struct lu_object_conf   *conf = &lfsck_env_info(env)->lti_conf;
1251
1252         conf->loc_flags = LOC_F_NOWAIT;
1253         return lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, conf));
1254 }
1255
1256 static inline struct dt_object *
1257 lfsck_object_find_by_dev(const struct lu_env *env, struct dt_device *dev,
1258                          const struct lu_fid *fid)
1259 {
1260         return lu2dt(lu_object_find_slice(env, dt2lu_dev(dev), fid, NULL));
1261 }
1262
1263 static inline struct dt_device *
1264 lfsck_find_dev_by_fid(const struct lu_env *env, struct lfsck_instance *lfsck,
1265                       const struct lu_fid *fid)
1266 {
1267         struct dt_device *dev;
1268         int               idx;
1269
1270         if (!lfsck->li_master)
1271                 return lfsck->li_bottom;
1272
1273         idx = lfsck_find_mdt_idx_by_fid(env, lfsck, fid);
1274         if (idx < 0)
1275                 return ERR_PTR(idx);
1276
1277         if (idx == lfsck_dev_idx(lfsck)) {
1278                 dev = lfsck->li_bottom;
1279         } else {
1280                 struct lfsck_tgt_desc *ltd;
1281
1282                 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, idx);
1283                 if (unlikely(ltd == NULL))
1284                         return ERR_PTR(-ENODEV);
1285
1286                 dev = ltd->ltd_tgt;
1287         }
1288
1289         return dev;
1290 }
1291
1292 static inline struct dt_object *
1293 lfsck_object_find_bottom(const struct lu_env *env, struct lfsck_instance *lfsck,
1294                          const struct lu_fid *fid)
1295 {
1296         struct dt_device *dev;
1297
1298         dev = lfsck_find_dev_by_fid(env, lfsck, fid);
1299         if (IS_ERR(dev))
1300                 return (struct dt_object *)dev;
1301
1302         return lfsck_object_find_by_dev(env, dev, fid);
1303 }
1304
1305 static inline struct dt_object *
1306 lfsck_object_find_bottom_nowait(const struct lu_env *env,
1307                                 struct lfsck_instance *lfsck,
1308                                 const struct lu_fid *fid)
1309 {
1310         struct dt_device *dev;
1311
1312         dev = lfsck_find_dev_by_fid(env, lfsck, fid);
1313         if (IS_ERR(dev))
1314                 return (struct dt_object *)dev;
1315
1316         return lfsck_object_find_by_dev_nowait(env, dev, fid);
1317 }
1318
1319 static inline struct dt_object *
1320 lfsck_object_find_bottom_new(const struct lu_env *env,
1321                              struct lfsck_instance *lfsck,
1322                              const struct lu_fid *fid)
1323 {
1324         struct dt_device *dev;
1325
1326         dev = lfsck_find_dev_by_fid(env, lfsck, fid);
1327         if (IS_ERR(dev))
1328                 return (struct dt_object *)dev;
1329
1330         return lfsck_object_find_by_dev_new(env, dev, fid);
1331 }
1332
1333 static inline struct dt_object *
1334 lfsck_object_locate(struct dt_device *dev, struct dt_object *obj)
1335 {
1336         if (lfsck_obj2dev(obj) == dev) {
1337                 return obj;
1338         } else {
1339                 struct lu_object *lo;
1340
1341                 lo = lu_object_locate(obj->do_lu.lo_header,
1342                                       dev->dd_lu_dev.ld_type);
1343                 if (unlikely(lo == NULL))
1344                         return ERR_PTR(-ENOENT);
1345
1346                 return lu2dt(lo);
1347         }
1348 }
1349
1350 static inline struct lfsck_tgt_desc *lfsck_tgt_get(struct lfsck_tgt_descs *ltds,
1351                                                    __u32 index)
1352 {
1353         struct lfsck_tgt_desc *ltd;
1354
1355         ltd = lfsck_ltd2tgt(ltds, index);
1356         if (ltd != NULL)
1357                 atomic_inc(&ltd->ltd_ref);
1358
1359         return ltd;
1360 }
1361
1362 static inline void lfsck_tgt_put(struct lfsck_tgt_desc *ltd)
1363 {
1364         if (atomic_dec_and_test(&ltd->ltd_ref))
1365                 OBD_FREE_PTR(ltd);
1366 }
1367
1368 static inline struct lfsck_component *
1369 lfsck_component_get(struct lfsck_component *com)
1370 {
1371         atomic_inc(&com->lc_ref);
1372
1373         return com;
1374 }
1375
1376 static inline void lfsck_component_put(const struct lu_env *env,
1377                                        struct lfsck_component *com)
1378 {
1379         if (atomic_dec_and_test(&com->lc_ref)) {
1380                 struct lfsck_sub_trace_obj *lsto;
1381                 int                         i;
1382
1383                 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
1384                      i < LFSCK_STF_COUNT; i++, lsto++) {
1385                         if (lsto->lsto_obj != NULL)
1386                                 lfsck_object_put(env, lsto->lsto_obj);
1387                 }
1388
1389                 if (com->lc_obj != NULL)
1390                         lfsck_object_put(env, com->lc_obj);
1391                 if (com->lc_file_ram != NULL)
1392                         OBD_FREE(com->lc_file_ram, com->lc_file_size);
1393                 if (com->lc_file_disk != NULL)
1394                         OBD_FREE(com->lc_file_disk, com->lc_file_size);
1395                 if (com->lc_data != NULL) {
1396                         LASSERT(com->lc_ops->lfsck_data_release != NULL);
1397
1398                         com->lc_ops->lfsck_data_release(env, com);
1399                 }
1400
1401                 OBD_FREE_PTR(com);
1402         }
1403 }
1404
1405 static inline struct lfsck_instance *
1406 lfsck_instance_get(struct lfsck_instance *lfsck)
1407 {
1408         atomic_inc(&lfsck->li_ref);
1409
1410         return lfsck;
1411 }
1412
1413 static inline void lfsck_instance_put(const struct lu_env *env,
1414                                       struct lfsck_instance *lfsck)
1415 {
1416         if (atomic_dec_and_test(&lfsck->li_ref))
1417                 lfsck_instance_cleanup(env, lfsck);
1418 }
1419
1420 static inline bool lfsck_phase2_next_ready(struct lfsck_assistant_data *lad)
1421 {
1422         return list_empty(&lad->lad_mdt_phase1_list) &&
1423                (!list_empty(&lad->lad_ost_phase2_list) ||
1424                 list_empty(&lad->lad_ost_phase1_list));
1425 }
1426
1427 static inline void lfsck_lad_set_bitmap(const struct lu_env *env,
1428                                         struct lfsck_component *com,
1429                                         __u32 index)
1430 {
1431         struct lfsck_assistant_data     *lad    = com->lc_data;
1432         struct cfs_bitmap               *bitmap = lad->lad_bitmap;
1433
1434         LASSERT(com->lc_lfsck->li_master);
1435         LASSERT(bitmap != NULL);
1436
1437         if (likely(bitmap->size > index)) {
1438                 cfs_bitmap_set(bitmap, index);
1439                 lad->lad_incomplete = 1;
1440         } else if (com->lc_type == LFSCK_TYPE_NAMESPACE) {
1441                 struct lfsck_namespace *ns = com->lc_file_ram;
1442
1443                 ns->ln_flags |= LF_INCOMPLETE;
1444         }
1445 }
1446
1447 static inline int lfsck_links_read(const struct lu_env *env,
1448                                    struct dt_object *obj,
1449                                    struct linkea_data *ldata)
1450 {
1451         ldata->ld_buf =
1452                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
1453                                        PAGE_SIZE);
1454
1455         return __lfsck_links_read(env, obj, ldata);
1456 }
1457
1458 static inline int lfsck_links_read2(const struct lu_env *env,
1459                                     struct dt_object *obj,
1460                                     struct linkea_data *ldata)
1461 {
1462         ldata->ld_buf =
1463                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf2,
1464                                        PAGE_SIZE);
1465
1466         return __lfsck_links_read(env, obj, ldata);
1467 }
1468
1469 static inline struct lfsck_lmv *lfsck_lmv_get(struct lfsck_lmv *llmv)
1470 {
1471         if (llmv != NULL)
1472                 atomic_inc(&llmv->ll_ref);
1473
1474         return llmv;
1475 }
1476
1477 static inline int lfsck_sub_trace_file_fid2idx(const struct lu_fid *fid)
1478 {
1479         return fid->f_oid & (LFSCK_STF_COUNT - 1);
1480 }
1481
1482 static inline void lfsck_lmv_header_le_to_cpu(struct lmv_mds_md_v1 *dst,
1483                                               const struct lmv_mds_md_v1 *src)
1484 {
1485         dst->lmv_magic = le32_to_cpu(src->lmv_magic);
1486         dst->lmv_stripe_count = le32_to_cpu(src->lmv_stripe_count);
1487         dst->lmv_master_mdt_index = le32_to_cpu(src->lmv_master_mdt_index);
1488         dst->lmv_hash_type = le32_to_cpu(src->lmv_hash_type);
1489         dst->lmv_layout_version = le32_to_cpu(src->lmv_layout_version);
1490 }
1491
1492 static inline void lfsck_lmv_header_cpu_to_le(struct lmv_mds_md_v1 *dst,
1493                                               const struct lmv_mds_md_v1 *src)
1494 {
1495         dst->lmv_magic = cpu_to_le32(src->lmv_magic);
1496         dst->lmv_stripe_count = cpu_to_le32(src->lmv_stripe_count);
1497         dst->lmv_master_mdt_index = cpu_to_le32(src->lmv_master_mdt_index);
1498         dst->lmv_hash_type = cpu_to_le32(src->lmv_hash_type);
1499         dst->lmv_layout_version = cpu_to_le32(src->lmv_layout_version);
1500 }
1501
1502 static inline struct lfsck_assistant_object *
1503 lfsck_assistant_object_get(struct lfsck_assistant_object *lso)
1504 {
1505         atomic_inc(&lso->lso_ref);
1506
1507         return lso;
1508 }
1509
1510 static inline void
1511 lfsck_assistant_object_put(const struct lu_env *env,
1512                            struct lfsck_assistant_object *lso)
1513 {
1514         if (atomic_dec_and_test(&lso->lso_ref))
1515                 OBD_FREE_PTR(lso);
1516 }
1517 #endif /* _LFSCK_INTERNAL_H */