Whamcloud - gitweb
LU-1267 lfsck: framework (1) for MDT-OST consistency
[fs/lustre-release.git] / lustre / lfsck / lfsck_internal.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_internal.h
27  *
28  * Shared definitions and declarations for the LFSCK.
29  *
30  * Author: Fan, Yong <fan.yong@intel.com>
31  */
32
33 #ifndef _LFSCK_INTERNAL_H
34 # define _LFSCK_INTERNAL_H
35
36 #include <lustre/lustre_lfsck_user.h>
37 #include <lustre/lustre_user.h>
38 #include <lustre/lustre_idl.h>
39 #include <lustre_lfsck.h>
40 #include <obd.h>
41 #include <lu_object.h>
42 #include <dt_object.h>
43 #include <lustre_net.h>
44 #include <lustre_dlm.h>
45 #include <lustre_fid.h>
46 #include <md_object.h>
47
48 #define HALF_SEC                        (HZ >> 1)
49 #define LFSCK_CHECKPOINT_INTERVAL       60
50
51 #define LFSCK_NAMEENTRY_DEAD            1 /* The object has been unlinked. */
52 #define LFSCK_NAMEENTRY_REMOVED         2 /* The entry has been removed. */
53 #define LFSCK_NAMEENTRY_RECREATED       3 /* The entry has been recreated. */
54
55 enum lfsck_flags {
56         /* Finish the first cycle scanning. */
57         LF_SCANNED_ONCE         = 0x00000001ULL,
58
59         /* There is some namespace inconsistency. */
60         LF_INCONSISTENT         = 0x00000002ULL,
61
62         /* The device is upgraded from 1.8 format. */
63         LF_UPGRADE              = 0x00000004ULL,
64
65         /* The server ever restarted during the LFSCK, and may miss to process
66          * some objects check/repair. */
67         LF_INCOMPLETE           = 0x00000008ULL,
68 };
69
70 struct lfsck_position {
71         /* low layer object table-based iteration position. */
72         __u64   lp_oit_cookie;
73
74         /* parent FID for directory traversal. */
75         struct lu_fid lp_dir_parent;
76
77         /* namespace-based directory traversal position. */
78         __u64   lp_dir_cookie;
79 };
80
81 struct lfsck_bookmark {
82         /* Magic number to detect that this struct contains valid data. */
83         __u32   lb_magic;
84
85         /* For compatible with old versions. */
86         __u16   lb_version;
87
88         /* See 'enum lfsck_param_flags' */
89         __u16   lb_param;
90
91         /* How many items can be scanned at most per second. */
92         __u32   lb_speed_limit;
93
94         /* For 64-bits aligned. */
95         __u32   lb_padding;
96
97         /* For future using. */
98         __u64   lb_reserved[6];
99 };
100
101 struct lfsck_namespace {
102         /* Magic number to detect that this struct contains valid data. */
103         __u32   ln_magic;
104
105         /* See 'enum lfsck_status'. */
106         __u32   ln_status;
107
108         /* See 'enum lfsck_flags'. */
109         __u32   ln_flags;
110
111         /* How many completed LFSCK runs on the device. */
112         __u32   ln_success_count;
113
114         /*  How long the LFSCK phase1 has run in seconds. */
115         __u32   ln_run_time_phase1;
116
117         /*  How long the LFSCK phase2 has run in seconds. */
118         __u32   ln_run_time_phase2;
119
120         /* Time for the last LFSCK completed in seconds since epoch. */
121         __u64   ln_time_last_complete;
122
123         /* Time for the latest LFSCK ran in seconds since epoch. */
124         __u64   ln_time_latest_start;
125
126         /* Time for the last LFSCK checkpoint in seconds since epoch. */
127         __u64   ln_time_last_checkpoint;
128
129         /* Position for the latest LFSCK started from. */
130         struct lfsck_position   ln_pos_latest_start;
131
132         /* Position for the last LFSCK checkpoint. */
133         struct lfsck_position   ln_pos_last_checkpoint;
134
135         /* Position for the first should be updated object. */
136         struct lfsck_position   ln_pos_first_inconsistent;
137
138         /* How many items (including dir) have been checked. */
139         __u64   ln_items_checked;
140
141         /* How many items have been repaired. */
142         __u64   ln_items_repaired;
143
144         /* How many items failed to be processed. */
145         __u64   ln_items_failed;
146
147         /* How many directories have been traversed. */
148         __u64   ln_dirs_checked;
149
150         /* How many multiple-linked objects have been checked. */
151         __u64   ln_mlinked_checked;
152
153         /* How many objects have been double scanned. */
154         __u64   ln_objs_checked_phase2;
155
156         /* How many objects have been reparied during double scan. */
157         __u64   ln_objs_repaired_phase2;
158
159         /* How many objects failed to be processed during double scan. */
160         __u64   ln_objs_failed_phase2;
161
162         /* How many objects with nlink fixed. */
163         __u64   ln_objs_nlink_repaired;
164
165         /* How many objects were lost before, but found back now. */
166         __u64   ln_objs_lost_found;
167
168         /* The latest object has been processed (failed) during double scan. */
169         struct lu_fid   ln_fid_latest_scanned_phase2;
170
171         /* For further using. 256-bytes aligned now. */
172         __u64   ln_reserved[2];
173 };
174
175 enum lfsck_layout_inconsistency_type {
176         LLIT_NONE                       = 0,
177         LLIT_DANGLING                   = 1,
178         LLIT_UNMATCHED_PAIR             = 2,
179         LLIT_MULTIPLE_REFERENCED        = 3,
180         LLIT_ORPHAN                     = 4,
181         LLIT_INCONSISTENT_OWNER         = 5,
182         LLIT_OTHERS                     = 6,
183         LLIT_MAX                        = LLIT_OTHERS
184 };
185
186 struct lfsck_layout {
187         /* Magic number to detect that this struct contains valid data. */
188         __u32   ll_magic;
189
190         /* See 'enum lfsck_status'. */
191         __u32   ll_status;
192
193         /* See 'enum lfsck_flags'. */
194         __u32   ll_flags;
195
196         /* How many completed LFSCK runs on the device. */
197         __u32   ll_success_count;
198
199         /*  How long the LFSCK phase1 has run in seconds. */
200         __u32   ll_run_time_phase1;
201
202         /*  How long the LFSCK phase2 has run in seconds. */
203         __u32   ll_run_time_phase2;
204
205         /* Time for the last LFSCK completed in seconds since epoch. */
206         __u64   ll_time_last_complete;
207
208         /* Time for the latest LFSCK ran in seconds since epoch. */
209         __u64   ll_time_latest_start;
210
211         /* Time for the last LFSCK checkpoint in seconds since epoch. */
212         __u64   ll_time_last_checkpoint;
213
214         /* Position for the latest LFSCK started from. */
215         __u64   ll_pos_latest_start;
216
217         /* Position for the last LFSCK checkpoint. */
218         __u64   ll_pos_last_checkpoint;
219
220         /* Position for the first should be updated object. */
221         __u64   ll_pos_first_inconsistent;
222
223         /* How many objects have been checked. */
224         __u64   ll_objs_checked_phase1;
225
226         /* How many objects failed to be processed. */
227         __u64   ll_objs_failed_phase1;
228
229         /* How many objects have been double scanned. */
230         __u64   ll_objs_checked_phase2;
231
232         /* How many objects failed to be processed during double scan. */
233         __u64   ll_objs_failed_phase2;
234
235         /* kinds of inconsistency have been repaired.
236          * ll_objs_repaired[type - 1] is the count for the given @type. */
237         __u64   ll_objs_repaired[LLIT_MAX];
238
239         /* How many objects have been skipped because of related
240          * MDT(s)/OST(s) do not participate in the LFSCK */
241         __u64   ll_objs_skipped;
242
243         /* For further using. 256-bytes aligned now. */
244         __u64   ll_reserved[12];
245 };
246
247 struct lfsck_component;
248
249 struct lfsck_operations {
250         int (*lfsck_reset)(const struct lu_env *env,
251                            struct lfsck_component *com,
252                            bool init);
253
254         void (*lfsck_fail)(const struct lu_env *env,
255                            struct lfsck_component *com,
256                            bool new_checked);
257
258         int (*lfsck_checkpoint)(const struct lu_env *env,
259                                 struct lfsck_component *com,
260                                 bool init);
261
262         int (*lfsck_prep)(const struct lu_env *env,
263                           struct lfsck_component *com);
264
265         int (*lfsck_exec_oit)(const struct lu_env *env,
266                               struct lfsck_component *com,
267                               struct dt_object *obj);
268
269         int (*lfsck_exec_dir)(const struct lu_env *env,
270                               struct lfsck_component *com,
271                               struct dt_object *obj,
272                               struct lu_dirent *ent);
273
274         int (*lfsck_post)(const struct lu_env *env,
275                           struct lfsck_component *com,
276                           int result,
277                           bool init);
278
279         int (*lfsck_dump)(const struct lu_env *env,
280                           struct lfsck_component *com,
281                           char *buf,
282                           int len);
283
284         int (*lfsck_double_scan)(const struct lu_env *env,
285                                  struct lfsck_component *com);
286 };
287
288 struct lfsck_component {
289         /* into lfsck_instance::li_list_(scan,double_scan,idle} */
290         cfs_list_t               lc_link;
291
292         /* into lfsck_instance::li_list_dir */
293         cfs_list_t               lc_link_dir;
294         struct rw_semaphore      lc_sem;
295         atomic_t                 lc_ref;
296
297         struct lfsck_position    lc_pos_start;
298         struct lfsck_instance   *lc_lfsck;
299         struct dt_object        *lc_obj;
300         struct lfsck_operations *lc_ops;
301         void                    *lc_file_ram;
302         void                    *lc_file_disk;
303
304         /* The time for last checkpoint, jiffies */
305         cfs_time_t               lc_time_last_checkpoint;
306
307         /* The time for next checkpoint, jiffies */
308         cfs_time_t               lc_time_next_checkpoint;
309
310         __u32                    lc_file_size;
311
312         /* How many objects have been checked since last checkpoint. */
313         __u32                    lc_new_checked;
314
315         /* How many objects have been scanned since last sleep. */
316         __u32                    lc_new_scanned;
317
318         unsigned int             lc_journal:1;
319         __u16                    lc_type;
320 };
321
322 struct lfsck_instance {
323         struct mutex              li_mutex;
324         spinlock_t                li_lock;
325
326         /* Link into the lfsck_instance_list. */
327         cfs_list_t                li_link;
328
329         /* For the components in (first) scanning via otable-based iteration. */
330         cfs_list_t                li_list_scan;
331
332         /* For the components in scanning via directory traversal. Because
333          * directory traversal cannot guarantee all the object be scanned,
334          * so the component in the li_list_dir must be in li_list_scan. */
335         cfs_list_t                li_list_dir;
336
337         /* For the components in double scanning. */
338         cfs_list_t                li_list_double_scan;
339
340         /* For the components those are not scanning now. */
341         cfs_list_t                li_list_idle;
342
343         atomic_t                  li_ref;
344         struct ptlrpc_thread      li_thread;
345
346         /* The time for last checkpoint, jiffies */
347         cfs_time_t                li_time_last_checkpoint;
348
349         /* The time for next checkpoint, jiffies */
350         cfs_time_t                li_time_next_checkpoint;
351
352         struct dt_device         *li_next;
353         struct dt_device         *li_bottom;
354         struct ldlm_namespace    *li_namespace;
355         struct local_oid_storage *li_los;
356         struct lu_fid             li_local_root_fid;  /* backend root "/" */
357         struct lu_fid             li_global_root_fid; /* /ROOT */
358         struct dt_object         *li_bookmark_obj;
359         struct lfsck_bookmark     li_bookmark_ram;
360         struct lfsck_bookmark     li_bookmark_disk;
361         struct lfsck_position     li_pos_current;
362
363         /* Obj for otable-based iteration */
364         struct dt_object         *li_obj_oit;
365
366         /* Obj for directory traversal */
367         struct dt_object         *li_obj_dir;
368
369         /* It for otable-based iteration */
370         struct dt_it             *li_di_oit;
371
372         /* It for directory traversal */
373         struct dt_it             *li_di_dir;
374
375         /* namespace-based directory traversal position. */
376         __u64                     li_cookie_dir;
377
378         /* Arguments for low layer otable-based iteration. */
379         __u32                     li_args_oit;
380
381         /* Arugments for namespace-based directory traversal. */
382         __u32                     li_args_dir;
383
384         /* Schedule for every N objects. */
385         __u32                     li_sleep_rate;
386
387         /* Sleep N jiffies for each schedule. */
388         __u32                     li_sleep_jif;
389
390         /* How many objects have been scanned since last sleep. */
391         __u32                     li_new_scanned;
392
393         unsigned int              li_paused:1, /* The lfsck is paused. */
394                                   li_oit_over:1, /* oit is finished. */
395                                   li_drop_dryrun:1, /* Ever dryrun, not now. */
396                                   li_master:1, /* Master instance or not. */
397                                   li_current_oit_processed:1;
398 };
399
400 enum lfsck_linkea_flags {
401         /* The linkea entries does not match the object nlinks. */
402         LLF_UNMATCH_NLINKS      = 0x01,
403
404         /* Fail to repair the multiple-linked objects during the double scan. */
405         LLF_REPAIR_FAILED       = 0x02,
406 };
407
408 struct lfsck_thread_args {
409         struct lu_env            lta_env;
410         struct lfsck_instance   *lta_lfsck;
411         struct lfsck_component  *lta_com;
412 };
413
414 struct lfsck_thread_info {
415         struct lu_name          lti_name;
416         struct lu_buf           lti_buf;
417         struct lu_buf           lti_linkea_buf;
418         struct lu_fid           lti_fid;
419         struct lu_fid           lti_fid2;
420         struct lu_attr          lti_la;
421         struct ost_id           lti_oi;
422         union {
423                 struct lustre_mdt_attrs lti_lma;
424                 /* old LMA for compatibility */
425                 char                    lti_lma_old[LMA_OLD_SIZE];
426         };
427         /* lti_ent and lti_key must be conjoint,
428          * then lti_ent::lde_name will be lti_key. */
429         struct lu_dirent        lti_ent;
430         char                    lti_key[NAME_MAX + 16];
431 };
432
433 /* lfsck_lib.c */
434 const char *lfsck_status2names(enum lfsck_status status);
435 void lfsck_component_cleanup(const struct lu_env *env,
436                              struct lfsck_component *com);
437 void lfsck_instance_cleanup(const struct lu_env *env,
438                             struct lfsck_instance *lfsck);
439 int lfsck_bits_dump(char **buf, int *len, int bits, const char *names[],
440                     const char *prefix);
441 int lfsck_time_dump(char **buf, int *len, __u64 time, const char *prefix);
442 int lfsck_pos_dump(char **buf, int *len, struct lfsck_position *pos,
443                    const char *prefix);
444 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
445                     struct lfsck_position *pos, bool init);
446 void lfsck_control_speed(struct lfsck_instance *lfsck);
447 void lfsck_control_speed_by_self(struct lfsck_component *com);
448 int lfsck_reset(const struct lu_env *env, struct lfsck_instance *lfsck,
449                 bool init);
450 struct lfsck_thread_args *lfsck_thread_args_init(struct lfsck_instance *lfsck,
451                                                  struct lfsck_component *com);
452 void lfsck_thread_args_fini(struct lfsck_thread_args *lta);
453 void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
454                 bool new_checked);
455 int lfsck_checkpoint(const struct lu_env *env, struct lfsck_instance *lfsck);
456 int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck);
457 int lfsck_exec_oit(const struct lu_env *env, struct lfsck_instance *lfsck,
458                    struct dt_object *obj);
459 int lfsck_exec_dir(const struct lu_env *env, struct lfsck_instance *lfsck,
460                    struct dt_object *obj, struct lu_dirent *ent);
461 int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
462                int result);
463 int lfsck_double_scan(const struct lu_env *env, struct lfsck_instance *lfsck);
464
465 /* lfsck_engine.c */
466 int lfsck_master_engine(void *args);
467
468 /* lfsck_bookmark.c */
469 int lfsck_bookmark_store(const struct lu_env *env,
470                          struct lfsck_instance *lfsck);
471 int lfsck_bookmark_setup(const struct lu_env *env,
472                          struct lfsck_instance *lfsck);
473
474 /* lfsck_namespace.c */
475 int lfsck_namespace_setup(const struct lu_env *env,
476                           struct lfsck_instance *lfsck);
477
478 /* lfsck_layout.c */
479 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
480
481 extern const char *lfsck_flags_names[];
482 extern const char *lfsck_param_names[];
483 extern struct lu_context_key lfsck_thread_key;
484
485 static inline struct lfsck_thread_info *
486 lfsck_env_info(const struct lu_env *env)
487 {
488         struct lfsck_thread_info *info;
489
490         info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
491         LASSERT(info != NULL);
492         return info;
493 }
494
495 static inline const struct lu_name *
496 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
497 {
498         struct lu_name *lname;
499
500         lname = &lfsck_env_info(env)->lti_name;
501         lname->ln_name = area;
502         lname->ln_namelen = len;
503         return lname;
504 }
505
506 static inline struct lu_buf *
507 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
508 {
509         struct lu_buf *buf;
510
511         buf = &lfsck_env_info(env)->lti_buf;
512         buf->lb_buf = area;
513         buf->lb_len = len;
514         return buf;
515 }
516
517 static inline const struct lu_buf *
518 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
519 {
520         struct lu_buf *buf;
521
522         buf = &lfsck_env_info(env)->lti_buf;
523         buf->lb_buf = (void *)area;
524         buf->lb_len = len;
525         return buf;
526 }
527
528 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
529 {
530         return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
531 }
532
533 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
534 {
535         return lu_object_fid(&obj->do_lu);
536 }
537
538 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
539 {
540         memset(pos, 0, sizeof(*pos));
541 }
542
543 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
544 {
545         return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
546 }
547
548 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
549                                   const struct lfsck_position *pos2)
550 {
551         if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
552                 return -1;
553
554         if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
555                 return 1;
556
557         if (fid_is_zero(&pos1->lp_dir_parent) &&
558             !fid_is_zero(&pos2->lp_dir_parent))
559                 return -1;
560
561         if (!fid_is_zero(&pos1->lp_dir_parent) &&
562             fid_is_zero(&pos2->lp_dir_parent))
563                 return 1;
564
565         if (fid_is_zero(&pos1->lp_dir_parent) &&
566             fid_is_zero(&pos2->lp_dir_parent))
567                 return 0;
568
569         LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
570
571         if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
572                 return -1;
573
574         if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
575                 return 1;
576
577         return 0;
578 }
579
580 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
581                                             struct lfsck_position *src)
582 {
583         des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
584         fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
585         des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
586 }
587
588 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
589                                             struct lfsck_position *src)
590 {
591         des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
592         fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
593         des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
594 }
595
596 static inline umode_t lfsck_object_type(const struct dt_object *obj)
597 {
598         return lu_object_attr(&obj->do_lu);
599 }
600
601 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
602 {
603         struct lu_object_header *loh = obj->do_lu.lo_header;
604
605         return !!test_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
606 }
607
608 static inline struct dt_object *lfsck_object_find(const struct lu_env *env,
609                                                   struct lfsck_instance *lfsck,
610                                                   const struct lu_fid *fid)
611 {
612         return lu2dt(lu_object_find_slice(env, dt2lu_dev(lfsck->li_next),
613                      fid, NULL));
614 }
615
616 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
617 {
618         lu_object_get(&obj->do_lu);
619         return obj;
620 }
621
622 static inline void lfsck_object_put(const struct lu_env *env,
623                                     struct dt_object *obj)
624 {
625         lu_object_put(env, &obj->do_lu);
626 }
627
628 static inline struct lfsck_component *
629 lfsck_component_get(struct lfsck_component *com)
630 {
631         atomic_inc(&com->lc_ref);
632
633         return com;
634 }
635
636 static inline void lfsck_component_put(const struct lu_env *env,
637                                        struct lfsck_component *com)
638 {
639         if (atomic_dec_and_test(&com->lc_ref)) {
640                 if (com->lc_obj != NULL)
641                         lu_object_put_nocache(env, &com->lc_obj->do_lu);
642                 if (com->lc_file_ram != NULL)
643                         OBD_FREE(com->lc_file_ram, com->lc_file_size);
644                 if (com->lc_file_disk != NULL)
645                         OBD_FREE(com->lc_file_disk, com->lc_file_size);
646                 OBD_FREE_PTR(com);
647         }
648 }
649
650 static inline struct lfsck_instance *
651 lfsck_instance_get(struct lfsck_instance *lfsck)
652 {
653         atomic_inc(&lfsck->li_ref);
654
655         return lfsck;
656 }
657
658 static inline void lfsck_instance_put(const struct lu_env *env,
659                                       struct lfsck_instance *lfsck)
660 {
661         if (atomic_dec_and_test(&lfsck->li_ref))
662                 lfsck_instance_cleanup(env, lfsck);
663 }
664
665 static inline mdsno_t lfsck_dev_idx(struct dt_device *dev)
666 {
667         return dev->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
668 }
669
670 #endif /* _LFSCK_INTERNAL_H */