4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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
23 * Copyright (c) 2013, Intel Corporation.
26 * lustre/lfsck/lfsck_internal.h
28 * Shared definitions and declarations for the LFSCK.
30 * Author: Fan, Yong <fan.yong@intel.com>
33 #ifndef _LFSCK_INTERNAL_H
34 # define _LFSCK_INTERNAL_H
36 #include <lustre/lustre_lfsck_user.h>
37 #include <lustre/lustre_user.h>
38 #include <lustre/lustre_idl.h>
39 #include <lustre_lfsck.h>
41 #include <lu_object.h>
42 #include <dt_object.h>
43 #include <md_object.h>
44 #include <lustre_net.h>
45 #include <lustre_dlm.h>
46 #include <lustre_fid.h>
47 #include <md_object.h>
49 #define HALF_SEC (HZ >> 1)
50 #define LFSCK_CHECKPOINT_INTERVAL 60
52 #define LFSCK_NAMEENTRY_DEAD 1 /* The object has been unlinked. */
53 #define LFSCK_NAMEENTRY_REMOVED 2 /* The entry has been removed. */
54 #define LFSCK_NAMEENTRY_RECREATED 3 /* The entry has been recreated. */
57 /* Finish the first cycle scanning. */
58 LF_SCANNED_ONCE = 0x00000001ULL,
60 /* There is some namespace inconsistency. */
61 LF_INCONSISTENT = 0x00000002ULL,
63 /* The device is upgraded from 1.8 format. */
64 LF_UPGRADE = 0x00000004ULL,
66 /* The server ever restarted during the LFSCK, and may miss to process
67 * some objects check/repair. */
68 LF_INCOMPLETE = 0x00000008ULL,
70 /* The LAST_ID (file) crashed. */
71 LF_CRASHED_LASTID = 0x00000010ULL,
74 struct lfsck_position {
75 /* low layer object table-based iteration position. */
78 /* parent FID for directory traversal. */
79 struct lu_fid lp_dir_parent;
81 /* namespace-based directory traversal position. */
85 struct lfsck_bookmark {
86 /* Magic number to detect that this struct contains valid data. */
89 /* For compatible with old versions. */
92 /* See 'enum lfsck_param_flags' */
95 /* How many items can be scanned at most per second. */
98 /* For 64-bits aligned. */
101 /* For future using. */
102 __u64 lb_reserved[6];
105 struct lfsck_namespace {
106 /* Magic number to detect that this struct contains valid data. */
109 /* See 'enum lfsck_status'. */
112 /* See 'enum lfsck_flags'. */
115 /* How many completed LFSCK runs on the device. */
116 __u32 ln_success_count;
118 /* How long the LFSCK phase1 has run in seconds. */
119 __u32 ln_run_time_phase1;
121 /* How long the LFSCK phase2 has run in seconds. */
122 __u32 ln_run_time_phase2;
124 /* Time for the last LFSCK completed in seconds since epoch. */
125 __u64 ln_time_last_complete;
127 /* Time for the latest LFSCK ran in seconds since epoch. */
128 __u64 ln_time_latest_start;
130 /* Time for the last LFSCK checkpoint in seconds since epoch. */
131 __u64 ln_time_last_checkpoint;
133 /* Position for the latest LFSCK started from. */
134 struct lfsck_position ln_pos_latest_start;
136 /* Position for the last LFSCK checkpoint. */
137 struct lfsck_position ln_pos_last_checkpoint;
139 /* Position for the first should be updated object. */
140 struct lfsck_position ln_pos_first_inconsistent;
142 /* How many items (including dir) have been checked. */
143 __u64 ln_items_checked;
145 /* How many items have been repaired. */
146 __u64 ln_items_repaired;
148 /* How many items failed to be processed. */
149 __u64 ln_items_failed;
151 /* How many directories have been traversed. */
152 __u64 ln_dirs_checked;
154 /* How many multiple-linked objects have been checked. */
155 __u64 ln_mlinked_checked;
157 /* How many objects have been double scanned. */
158 __u64 ln_objs_checked_phase2;
160 /* How many objects have been reparied during double scan. */
161 __u64 ln_objs_repaired_phase2;
163 /* How many objects failed to be processed during double scan. */
164 __u64 ln_objs_failed_phase2;
166 /* How many objects with nlink fixed. */
167 __u64 ln_objs_nlink_repaired;
169 /* How many objects were lost before, but found back now. */
170 __u64 ln_objs_lost_found;
172 /* The latest object has been processed (failed) during double scan. */
173 struct lu_fid ln_fid_latest_scanned_phase2;
175 /* For further using. 256-bytes aligned now. */
176 __u64 ln_reserved[2];
179 enum lfsck_layout_inconsistency_type {
182 LLIT_UNMATCHED_PAIR = 2,
183 LLIT_MULTIPLE_REFERENCED = 3,
185 LLIT_INCONSISTENT_OWNER = 5,
187 LLIT_MAX = LLIT_OTHERS
190 struct lfsck_layout {
191 /* Magic number to detect that this struct contains valid data. */
194 /* See 'enum lfsck_status'. */
197 /* See 'enum lfsck_flags'. */
200 /* How many completed LFSCK runs on the device. */
201 __u32 ll_success_count;
203 /* How long the LFSCK phase1 has run in seconds. */
204 __u32 ll_run_time_phase1;
206 /* How long the LFSCK phase2 has run in seconds. */
207 __u32 ll_run_time_phase2;
209 /* Time for the last LFSCK completed in seconds since epoch. */
210 __u64 ll_time_last_complete;
212 /* Time for the latest LFSCK ran in seconds since epoch. */
213 __u64 ll_time_latest_start;
215 /* Time for the last LFSCK checkpoint in seconds since epoch. */
216 __u64 ll_time_last_checkpoint;
218 /* Position for the latest LFSCK started from. */
219 __u64 ll_pos_latest_start;
221 /* Position for the last LFSCK checkpoint. */
222 __u64 ll_pos_last_checkpoint;
224 /* Position for the first should be updated object. */
225 __u64 ll_pos_first_inconsistent;
227 /* How many objects have been checked. */
228 __u64 ll_objs_checked_phase1;
230 /* How many objects failed to be processed. */
231 __u64 ll_objs_failed_phase1;
233 /* How many objects have been double scanned. */
234 __u64 ll_objs_checked_phase2;
236 /* How many objects failed to be processed during double scan. */
237 __u64 ll_objs_failed_phase2;
239 /* kinds of inconsistency have been repaired.
240 * ll_objs_repaired[type - 1] is the count for the given @type. */
241 __u64 ll_objs_repaired[LLIT_MAX];
243 /* How many objects have been skipped because of related
244 * MDT(s)/OST(s) do not participate in the LFSCK */
245 __u64 ll_objs_skipped;
247 /* For further using. 256-bytes aligned now. */
248 __u64 ll_reserved[12];
251 struct lfsck_component;
253 struct lfsck_operations {
254 int (*lfsck_reset)(const struct lu_env *env,
255 struct lfsck_component *com,
258 void (*lfsck_fail)(const struct lu_env *env,
259 struct lfsck_component *com,
262 int (*lfsck_checkpoint)(const struct lu_env *env,
263 struct lfsck_component *com,
266 int (*lfsck_prep)(const struct lu_env *env,
267 struct lfsck_component *com);
269 int (*lfsck_exec_oit)(const struct lu_env *env,
270 struct lfsck_component *com,
271 struct dt_object *obj);
273 int (*lfsck_exec_dir)(const struct lu_env *env,
274 struct lfsck_component *com,
275 struct dt_object *obj,
276 struct lu_dirent *ent);
278 int (*lfsck_post)(const struct lu_env *env,
279 struct lfsck_component *com,
283 int (*lfsck_dump)(const struct lu_env *env,
284 struct lfsck_component *com,
288 int (*lfsck_double_scan)(const struct lu_env *env,
289 struct lfsck_component *com);
291 void (*lfsck_data_release)(const struct lu_env *env,
292 struct lfsck_component *com);
295 struct lfsck_component {
296 /* into lfsck_instance::li_list_(scan,double_scan,idle} */
299 /* into lfsck_instance::li_list_dir */
300 cfs_list_t lc_link_dir;
301 struct rw_semaphore lc_sem;
304 struct lfsck_position lc_pos_start;
305 struct lfsck_instance *lc_lfsck;
306 struct dt_object *lc_obj;
307 struct lfsck_operations *lc_ops;
312 /* The time for last checkpoint, jiffies */
313 cfs_time_t lc_time_last_checkpoint;
315 /* The time for next checkpoint, jiffies */
316 cfs_time_t lc_time_next_checkpoint;
320 /* How many objects have been checked since last checkpoint. */
321 __u32 lc_new_checked;
323 /* How many objects have been scanned since last sleep. */
324 __u32 lc_new_scanned;
326 unsigned int lc_journal:1;
330 struct lfsck_instance {
331 struct mutex li_mutex;
334 /* Link into the lfsck_instance_list. */
337 /* For the components in (first) scanning via otable-based iteration. */
338 cfs_list_t li_list_scan;
340 /* For the components in scanning via directory traversal. Because
341 * directory traversal cannot guarantee all the object be scanned,
342 * so the component in the li_list_dir must be in li_list_scan. */
343 cfs_list_t li_list_dir;
345 /* For the components in double scanning. */
346 cfs_list_t li_list_double_scan;
348 /* For the components those are not scanning now. */
349 cfs_list_t li_list_idle;
352 struct ptlrpc_thread li_thread;
354 /* The time for last checkpoint, jiffies */
355 cfs_time_t li_time_last_checkpoint;
357 /* The time for next checkpoint, jiffies */
358 cfs_time_t li_time_next_checkpoint;
360 lfsck_out_notify li_out_notify;
361 void *li_out_notify_data;
362 struct dt_device *li_next;
363 struct dt_device *li_bottom;
364 struct ldlm_namespace *li_namespace;
365 struct local_oid_storage *li_los;
366 struct lu_fid li_local_root_fid; /* backend root "/" */
367 struct lu_fid li_global_root_fid; /* /ROOT */
368 struct dt_object *li_bookmark_obj;
369 struct lfsck_bookmark li_bookmark_ram;
370 struct lfsck_bookmark li_bookmark_disk;
371 struct lfsck_position li_pos_current;
373 /* Obj for otable-based iteration */
374 struct dt_object *li_obj_oit;
376 /* Obj for directory traversal */
377 struct dt_object *li_obj_dir;
379 /* It for otable-based iteration */
380 struct dt_it *li_di_oit;
382 /* It for directory traversal */
383 struct dt_it *li_di_dir;
385 /* namespace-based directory traversal position. */
388 /* Arguments for low layer otable-based iteration. */
391 /* Arugments for namespace-based directory traversal. */
394 /* Schedule for every N objects. */
397 /* Sleep N jiffies for each schedule. */
400 /* How many objects have been scanned since last sleep. */
401 __u32 li_new_scanned;
403 unsigned int li_paused:1, /* The lfsck is paused. */
404 li_oit_over:1, /* oit is finished. */
405 li_drop_dryrun:1, /* Ever dryrun, not now. */
406 li_master:1, /* Master instance or not. */
407 li_current_oit_processed:1;
410 enum lfsck_linkea_flags {
411 /* The linkea entries does not match the object nlinks. */
412 LLF_UNMATCH_NLINKS = 0x01,
414 /* Fail to repair the multiple-linked objects during the double scan. */
415 LLF_REPAIR_FAILED = 0x02,
418 struct lfsck_thread_args {
419 struct lu_env lta_env;
420 struct lfsck_instance *lta_lfsck;
421 struct lfsck_component *lta_com;
424 struct lfsck_thread_info {
425 struct lu_name lti_name;
426 struct lu_buf lti_buf;
427 struct lu_buf lti_linkea_buf;
428 struct lu_fid lti_fid;
429 struct lu_fid lti_fid2;
430 struct lu_attr lti_la;
431 struct ost_id lti_oi;
433 struct lustre_mdt_attrs lti_lma;
434 /* old LMA for compatibility */
435 char lti_lma_old[LMA_OLD_SIZE];
437 struct dt_object_format lti_dof;
438 /* lti_ent and lti_key must be conjoint,
439 * then lti_ent::lde_name will be lti_key. */
440 struct lu_dirent lti_ent;
441 char lti_key[NAME_MAX + 16];
445 const char *lfsck_status2names(enum lfsck_status status);
446 void lfsck_component_cleanup(const struct lu_env *env,
447 struct lfsck_component *com);
448 void lfsck_instance_cleanup(const struct lu_env *env,
449 struct lfsck_instance *lfsck);
450 int lfsck_bits_dump(char **buf, int *len, int bits, const char *names[],
452 int lfsck_time_dump(char **buf, int *len, __u64 time, const char *prefix);
453 int lfsck_pos_dump(char **buf, int *len, struct lfsck_position *pos,
455 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
456 struct lfsck_position *pos, bool init);
457 void lfsck_control_speed(struct lfsck_instance *lfsck);
458 void lfsck_control_speed_by_self(struct lfsck_component *com);
459 int lfsck_reset(const struct lu_env *env, struct lfsck_instance *lfsck,
461 struct lfsck_thread_args *lfsck_thread_args_init(struct lfsck_instance *lfsck,
462 struct lfsck_component *com);
463 void lfsck_thread_args_fini(struct lfsck_thread_args *lta);
464 void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
466 int lfsck_checkpoint(const struct lu_env *env, struct lfsck_instance *lfsck);
467 int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck);
468 int lfsck_exec_oit(const struct lu_env *env, struct lfsck_instance *lfsck,
469 struct dt_object *obj);
470 int lfsck_exec_dir(const struct lu_env *env, struct lfsck_instance *lfsck,
471 struct dt_object *obj, struct lu_dirent *ent);
472 int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
474 int lfsck_double_scan(const struct lu_env *env, struct lfsck_instance *lfsck);
477 int lfsck_master_engine(void *args);
479 /* lfsck_bookmark.c */
480 int lfsck_bookmark_store(const struct lu_env *env,
481 struct lfsck_instance *lfsck);
482 int lfsck_bookmark_setup(const struct lu_env *env,
483 struct lfsck_instance *lfsck);
485 /* lfsck_namespace.c */
486 int lfsck_namespace_setup(const struct lu_env *env,
487 struct lfsck_instance *lfsck);
490 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
492 extern const char *lfsck_flags_names[];
493 extern const char *lfsck_param_names[];
494 extern struct lu_context_key lfsck_thread_key;
496 static inline struct lfsck_thread_info *
497 lfsck_env_info(const struct lu_env *env)
499 struct lfsck_thread_info *info;
501 info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
502 LASSERT(info != NULL);
506 static inline const struct lu_name *
507 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
509 struct lu_name *lname;
511 lname = &lfsck_env_info(env)->lti_name;
512 lname->ln_name = area;
513 lname->ln_namelen = len;
517 static inline struct lu_buf *
518 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
522 buf = &lfsck_env_info(env)->lti_buf;
528 static inline const struct lu_buf *
529 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
533 buf = &lfsck_env_info(env)->lti_buf;
534 buf->lb_buf = (void *)area;
539 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
541 return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
544 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
546 return lu_object_fid(&obj->do_lu);
549 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
551 memset(pos, 0, sizeof(*pos));
554 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
556 return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
559 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
560 const struct lfsck_position *pos2)
562 if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
565 if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
568 if (fid_is_zero(&pos1->lp_dir_parent) &&
569 !fid_is_zero(&pos2->lp_dir_parent))
572 if (!fid_is_zero(&pos1->lp_dir_parent) &&
573 fid_is_zero(&pos2->lp_dir_parent))
576 if (fid_is_zero(&pos1->lp_dir_parent) &&
577 fid_is_zero(&pos2->lp_dir_parent))
580 LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
582 if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
585 if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
591 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
592 struct lfsck_position *src)
594 des->lp_oit_cookie = le64_to_cpu(src->lp_oit_cookie);
595 fid_le_to_cpu(&des->lp_dir_parent, &src->lp_dir_parent);
596 des->lp_dir_cookie = le64_to_cpu(src->lp_dir_cookie);
599 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
600 struct lfsck_position *src)
602 des->lp_oit_cookie = cpu_to_le64(src->lp_oit_cookie);
603 fid_cpu_to_le(&des->lp_dir_parent, &src->lp_dir_parent);
604 des->lp_dir_cookie = cpu_to_le64(src->lp_dir_cookie);
607 static inline umode_t lfsck_object_type(const struct dt_object *obj)
609 return lu_object_attr(&obj->do_lu);
612 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
614 struct lu_object_header *loh = obj->do_lu.lo_header;
616 return !!test_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
619 static inline struct dt_object *lfsck_object_find(const struct lu_env *env,
620 struct lfsck_instance *lfsck,
621 const struct lu_fid *fid)
623 return lu2dt(lu_object_find_slice(env, dt2lu_dev(lfsck->li_next),
627 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
629 lu_object_get(&obj->do_lu);
633 static inline void lfsck_object_put(const struct lu_env *env,
634 struct dt_object *obj)
636 lu_object_put(env, &obj->do_lu);
639 static inline struct lfsck_component *
640 lfsck_component_get(struct lfsck_component *com)
642 atomic_inc(&com->lc_ref);
647 static inline void lfsck_component_put(const struct lu_env *env,
648 struct lfsck_component *com)
650 if (atomic_dec_and_test(&com->lc_ref)) {
651 if (com->lc_obj != NULL)
652 lu_object_put_nocache(env, &com->lc_obj->do_lu);
653 if (com->lc_file_ram != NULL)
654 OBD_FREE(com->lc_file_ram, com->lc_file_size);
655 if (com->lc_file_disk != NULL)
656 OBD_FREE(com->lc_file_disk, com->lc_file_size);
657 if (com->lc_data != NULL) {
658 LASSERT(com->lc_ops->lfsck_data_release != NULL);
660 com->lc_ops->lfsck_data_release(env, com);
667 static inline struct lfsck_instance *
668 lfsck_instance_get(struct lfsck_instance *lfsck)
670 atomic_inc(&lfsck->li_ref);
675 static inline void lfsck_instance_put(const struct lu_env *env,
676 struct lfsck_instance *lfsck)
678 if (atomic_dec_and_test(&lfsck->li_ref))
679 lfsck_instance_cleanup(env, lfsck);
682 static inline mdsno_t lfsck_dev_idx(struct dt_device *dev)
684 return dev->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
687 #endif /* _LFSCK_INTERNAL_H */