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