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 <lustre_net.h>
44 #include <lustre_dlm.h>
45 #include <lustre_fid.h>
46 #include <md_object.h>
48 #define HALF_SEC (HZ >> 1)
49 #define LFSCK_CHECKPOINT_INTERVAL 60
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. */
56 /* Finish the first cycle scanning. */
57 LF_SCANNED_ONCE = 0x00000001ULL,
59 /* There is some namespace inconsistency. */
60 LF_INCONSISTENT = 0x00000002ULL,
62 /* The device is upgraded from 1.8 format. */
63 LF_UPGRADE = 0x00000004ULL,
65 /* The server ever restarted during the LFSCK, and may miss to process
66 * some objects check/repair. */
67 LF_INCOMPLETE = 0x00000008ULL,
70 struct lfsck_position {
71 /* low layer object table-based iteration position. */
74 /* parent FID for directory traversal. */
75 struct lu_fid lp_dir_parent;
77 /* namespace-based directory traversal position. */
81 struct lfsck_bookmark {
82 /* Magic number to detect that this struct contains valid data. */
85 /* For compatible with old versions. */
88 /* See 'enum lfsck_param_flags' */
91 /* How many items can be scanned at most per second. */
94 /* For 64-bits aligned. */
97 /* For future using. */
101 struct lfsck_namespace {
102 /* Magic number to detect that this struct contains valid data. */
105 /* See 'enum lfsck_status'. */
108 /* See 'enum lfsck_flags'. */
111 /* How many completed LFSCK runs on the device. */
112 __u32 ln_success_count;
114 /* How long the LFSCK phase1 has run in seconds. */
115 __u32 ln_run_time_phase1;
117 /* How long the LFSCK phase2 has run in seconds. */
118 __u32 ln_run_time_phase2;
120 /* Time for the last LFSCK completed in seconds since epoch. */
121 __u64 ln_time_last_complete;
123 /* Time for the latest LFSCK ran in seconds since epoch. */
124 __u64 ln_time_latest_start;
126 /* Time for the last LFSCK checkpoint in seconds since epoch. */
127 __u64 ln_time_last_checkpoint;
129 /* Position for the latest LFSCK started from. */
130 struct lfsck_position ln_pos_latest_start;
132 /* Position for the last LFSCK checkpoint. */
133 struct lfsck_position ln_pos_last_checkpoint;
135 /* Position for the first should be updated object. */
136 struct lfsck_position ln_pos_first_inconsistent;
138 /* How many items (including dir) have been checked. */
139 __u64 ln_items_checked;
141 /* How many items have been repaired. */
142 __u64 ln_items_repaired;
144 /* How many items failed to be processed. */
145 __u64 ln_items_failed;
147 /* How many directories have been traversed. */
148 __u64 ln_dirs_checked;
150 /* How many multiple-linked objects have been checked. */
151 __u64 ln_mlinked_checked;
153 /* How many objects have been double scanned. */
154 __u64 ln_objs_checked_phase2;
156 /* How many objects have been reparied during double scan. */
157 __u64 ln_objs_repaired_phase2;
159 /* How many objects failed to be processed during double scan. */
160 __u64 ln_objs_failed_phase2;
162 /* How many objects with nlink fixed. */
163 __u64 ln_objs_nlink_repaired;
165 /* How many objects were lost before, but found back now. */
166 __u64 ln_objs_lost_found;
168 /* The latest object has been processed (failed) during double scan. */
169 struct lu_fid ln_fid_latest_scanned_phase2;
171 /* For further using. 256-bytes aligned now. */
172 __u64 ln_reserved[2];
175 enum lfsck_layout_inconsistency_type {
178 LLIT_UNMATCHED_PAIR = 2,
179 LLIT_MULTIPLE_REFERENCED = 3,
181 LLIT_INCONSISTENT_OWNER = 5,
183 LLIT_MAX = LLIT_OTHERS
186 struct lfsck_layout {
187 /* Magic number to detect that this struct contains valid data. */
190 /* See 'enum lfsck_status'. */
193 /* See 'enum lfsck_flags'. */
196 /* How many completed LFSCK runs on the device. */
197 __u32 ll_success_count;
199 /* How long the LFSCK phase1 has run in seconds. */
200 __u32 ll_run_time_phase1;
202 /* How long the LFSCK phase2 has run in seconds. */
203 __u32 ll_run_time_phase2;
205 /* Time for the last LFSCK completed in seconds since epoch. */
206 __u64 ll_time_last_complete;
208 /* Time for the latest LFSCK ran in seconds since epoch. */
209 __u64 ll_time_latest_start;
211 /* Time for the last LFSCK checkpoint in seconds since epoch. */
212 __u64 ll_time_last_checkpoint;
214 /* Position for the latest LFSCK started from. */
215 __u64 ll_pos_latest_start;
217 /* Position for the last LFSCK checkpoint. */
218 __u64 ll_pos_last_checkpoint;
220 /* Position for the first should be updated object. */
221 __u64 ll_pos_first_inconsistent;
223 /* How many objects have been checked. */
224 __u64 ll_objs_checked_phase1;
226 /* How many objects failed to be processed. */
227 __u64 ll_objs_failed_phase1;
229 /* How many objects have been double scanned. */
230 __u64 ll_objs_checked_phase2;
232 /* How many objects failed to be processed during double scan. */
233 __u64 ll_objs_failed_phase2;
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];
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;
243 /* For further using. 256-bytes aligned now. */
244 __u64 ll_reserved[12];
247 struct lfsck_component;
249 struct lfsck_operations {
250 int (*lfsck_reset)(const struct lu_env *env,
251 struct lfsck_component *com,
254 void (*lfsck_fail)(const struct lu_env *env,
255 struct lfsck_component *com,
258 int (*lfsck_checkpoint)(const struct lu_env *env,
259 struct lfsck_component *com,
262 int (*lfsck_prep)(const struct lu_env *env,
263 struct lfsck_component *com);
265 int (*lfsck_exec_oit)(const struct lu_env *env,
266 struct lfsck_component *com,
267 struct dt_object *obj);
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);
274 int (*lfsck_post)(const struct lu_env *env,
275 struct lfsck_component *com,
279 int (*lfsck_dump)(const struct lu_env *env,
280 struct lfsck_component *com,
284 int (*lfsck_double_scan)(const struct lu_env *env,
285 struct lfsck_component *com);
288 struct lfsck_component {
289 /* into lfsck_instance::li_list_(scan,double_scan,idle} */
292 /* into lfsck_instance::li_list_dir */
293 cfs_list_t lc_link_dir;
294 struct rw_semaphore lc_sem;
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;
304 /* The time for last checkpoint, jiffies */
305 cfs_time_t lc_time_last_checkpoint;
307 /* The time for next checkpoint, jiffies */
308 cfs_time_t lc_time_next_checkpoint;
312 /* How many objects have been checked since last checkpoint. */
313 __u32 lc_new_checked;
315 /* How many objects have been scanned since last sleep. */
316 __u32 lc_new_scanned;
318 unsigned int lc_journal:1;
322 struct lfsck_instance {
323 struct mutex li_mutex;
326 /* Link into the lfsck_instance_list. */
329 /* For the components in (first) scanning via otable-based iteration. */
330 cfs_list_t li_list_scan;
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;
337 /* For the components in double scanning. */
338 cfs_list_t li_list_double_scan;
340 /* For the components those are not scanning now. */
341 cfs_list_t li_list_idle;
344 struct ptlrpc_thread li_thread;
346 /* The time for last checkpoint, jiffies */
347 cfs_time_t li_time_last_checkpoint;
349 /* The time for next checkpoint, jiffies */
350 cfs_time_t li_time_next_checkpoint;
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;
363 /* Obj for otable-based iteration */
364 struct dt_object *li_obj_oit;
366 /* Obj for directory traversal */
367 struct dt_object *li_obj_dir;
369 /* It for otable-based iteration */
370 struct dt_it *li_di_oit;
372 /* It for directory traversal */
373 struct dt_it *li_di_dir;
375 /* namespace-based directory traversal position. */
378 /* Arguments for low layer otable-based iteration. */
381 /* Arugments for namespace-based directory traversal. */
384 /* Schedule for every N objects. */
387 /* Sleep N jiffies for each schedule. */
390 /* How many objects have been scanned since last sleep. */
391 __u32 li_new_scanned;
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;
400 enum lfsck_linkea_flags {
401 /* The linkea entries does not match the object nlinks. */
402 LLF_UNMATCH_NLINKS = 0x01,
404 /* Fail to repair the multiple-linked objects during the double scan. */
405 LLF_REPAIR_FAILED = 0x02,
408 struct lfsck_thread_args {
409 struct lu_env lta_env;
410 struct lfsck_instance *lta_lfsck;
411 struct lfsck_component *lta_com;
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;
423 struct lustre_mdt_attrs lti_lma;
424 /* old LMA for compatibility */
425 char lti_lma_old[LMA_OLD_SIZE];
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];
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[],
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,
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,
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,
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,
463 int lfsck_double_scan(const struct lu_env *env, struct lfsck_instance *lfsck);
466 int lfsck_master_engine(void *args);
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);
474 /* lfsck_namespace.c */
475 int lfsck_namespace_setup(const struct lu_env *env,
476 struct lfsck_instance *lfsck);
479 int lfsck_layout_setup(const struct lu_env *env, struct lfsck_instance *lfsck);
481 extern const char *lfsck_flags_names[];
482 extern const char *lfsck_param_names[];
483 extern struct lu_context_key lfsck_thread_key;
485 static inline struct lfsck_thread_info *
486 lfsck_env_info(const struct lu_env *env)
488 struct lfsck_thread_info *info;
490 info = lu_context_key_get(&env->le_ctx, &lfsck_thread_key);
491 LASSERT(info != NULL);
495 static inline const struct lu_name *
496 lfsck_name_get_const(const struct lu_env *env, const void *area, ssize_t len)
498 struct lu_name *lname;
500 lname = &lfsck_env_info(env)->lti_name;
501 lname->ln_name = area;
502 lname->ln_namelen = len;
506 static inline struct lu_buf *
507 lfsck_buf_get(const struct lu_env *env, void *area, ssize_t len)
511 buf = &lfsck_env_info(env)->lti_buf;
517 static inline const struct lu_buf *
518 lfsck_buf_get_const(const struct lu_env *env, const void *area, ssize_t len)
522 buf = &lfsck_env_info(env)->lti_buf;
523 buf->lb_buf = (void *)area;
528 static inline char *lfsck_lfsck2name(struct lfsck_instance *lfsck)
530 return lfsck->li_bottom->dd_lu_dev.ld_obd->obd_name;
533 static inline const struct lu_fid *lfsck_dto2fid(const struct dt_object *obj)
535 return lu_object_fid(&obj->do_lu);
538 static inline void lfsck_pos_set_zero(struct lfsck_position *pos)
540 memset(pos, 0, sizeof(*pos));
543 static inline int lfsck_pos_is_zero(const struct lfsck_position *pos)
545 return pos->lp_oit_cookie == 0 && fid_is_zero(&pos->lp_dir_parent);
548 static inline int lfsck_pos_is_eq(const struct lfsck_position *pos1,
549 const struct lfsck_position *pos2)
551 if (pos1->lp_oit_cookie < pos2->lp_oit_cookie)
554 if (pos1->lp_oit_cookie > pos2->lp_oit_cookie)
557 if (fid_is_zero(&pos1->lp_dir_parent) &&
558 !fid_is_zero(&pos2->lp_dir_parent))
561 if (!fid_is_zero(&pos1->lp_dir_parent) &&
562 fid_is_zero(&pos2->lp_dir_parent))
565 if (fid_is_zero(&pos1->lp_dir_parent) &&
566 fid_is_zero(&pos2->lp_dir_parent))
569 LASSERT(lu_fid_eq(&pos1->lp_dir_parent, &pos2->lp_dir_parent));
571 if (pos1->lp_dir_cookie < pos2->lp_dir_cookie)
574 if (pos1->lp_dir_cookie > pos2->lp_dir_cookie)
580 static void inline lfsck_position_le_to_cpu(struct lfsck_position *des,
581 struct lfsck_position *src)
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);
588 static void inline lfsck_position_cpu_to_le(struct lfsck_position *des,
589 struct lfsck_position *src)
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);
596 static inline umode_t lfsck_object_type(const struct dt_object *obj)
598 return lu_object_attr(&obj->do_lu);
601 static inline int lfsck_is_dead_obj(const struct dt_object *obj)
603 struct lu_object_header *loh = obj->do_lu.lo_header;
605 return !!test_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
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)
612 return lu2dt(lu_object_find_slice(env, dt2lu_dev(lfsck->li_next),
616 static inline struct dt_object *lfsck_object_get(struct dt_object *obj)
618 lu_object_get(&obj->do_lu);
622 static inline void lfsck_object_put(const struct lu_env *env,
623 struct dt_object *obj)
625 lu_object_put(env, &obj->do_lu);
628 static inline struct lfsck_component *
629 lfsck_component_get(struct lfsck_component *com)
631 atomic_inc(&com->lc_ref);
636 static inline void lfsck_component_put(const struct lu_env *env,
637 struct lfsck_component *com)
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);
650 static inline struct lfsck_instance *
651 lfsck_instance_get(struct lfsck_instance *lfsck)
653 atomic_inc(&lfsck->li_ref);
658 static inline void lfsck_instance_put(const struct lu_env *env,
659 struct lfsck_instance *lfsck)
661 if (atomic_dec_and_test(&lfsck->li_ref))
662 lfsck_instance_cleanup(env, lfsck);
665 static inline mdsno_t lfsck_dev_idx(struct dt_device *dev)
667 return dev->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
670 #endif /* _LFSCK_INTERNAL_H */