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