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