From: Li Xi <lixi@xxxxxxx> These debug codes are added to run the multiple pass1 check thread one by one in order. If all the codes are correct, fsck of multiple threads should have exactly the same outcome with single thread. Signed-off-by: Li Xi <lixi@xxxxxxx> Signed-off-by: Wang Shilong <wshilong@xxxxxxx> --- e2fsck/e2fsck.h | 14 ++++++++++++++ e2fsck/pass1.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/e2fsck/e2fsck.h b/e2fsck/e2fsck.h index f8e98f73..61761684 100644 --- a/e2fsck/e2fsck.h +++ b/e2fsck/e2fsck.h @@ -451,6 +451,17 @@ struct e2fsck_struct { }; +#ifdef DEBUG_THREADS +/* + * Enabling DEBUG_THREADS would cause the parall fsck threads run sequentially + */ +struct e2fsck_thread_debug { + pthread_mutex_t etd_mutex; + pthread_cond_t etd_cond; + int etd_finished_threads; +}; +#endif + struct e2fsck_thread_info { /* ID returned by pthread_create() */ pthread_t eti_thread_id; @@ -460,6 +471,9 @@ struct e2fsck_thread_info { int eti_started; /* Context used for this thread */ e2fsck_t eti_thread_ctx; +#ifdef DEBUG_THREADS + struct e2fsck_thread_debug *eti_debug; +#endif }; /* Data structures to evaluate whether an extent tree needs rebuilding. */ diff --git a/e2fsck/pass1.c b/e2fsck/pass1.c index 1f47cbff..f8115679 100644 --- a/e2fsck/pass1.c +++ b/e2fsck/pass1.c @@ -1210,7 +1210,8 @@ void _e2fsck_pass1(e2fsck_t ctx) ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs); pass1_readahead(ctx, &ra_group, &ino_threshold); - if (!(ctx->options & E2F_OPT_PREEN)) + if (!(ctx->options & E2F_OPT_PREEN) && + ((!ctx->global_ctx) || (ctx->thread_info.et_thread_index == 0))) fix_problem(ctx, PR_1_PASS_HEADER, &pctx); if (ext2fs_has_feature_dir_index(fs->super) && @@ -2635,6 +2636,17 @@ static void *e2fsck_pass1_thread(void *arg) { struct e2fsck_thread_info *info = arg; e2fsck_t thread_ctx = info->eti_thread_ctx; +#ifdef DEBUG_THREADS + struct e2fsck_thread_debug *thread_debug = info->eti_debug; +#endif + +#ifdef DEBUG_THREADS + pthread_mutex_lock(&thread_debug->etd_mutex); + while (info->eti_thread_index > thread_debug->etd_finished_threads) { + pthread_cond_wait(&thread_debug->etd_cond, &thread_debug->etd_mutex); + } + pthread_mutex_unlock(&thread_debug->etd_mutex); +#endif #ifdef HAVE_SETJMP_H /* @@ -2659,6 +2671,14 @@ out: thread_ctx->thread_info.et_group_start, thread_ctx->thread_info.et_group_end, thread_ctx->thread_info.et_inode_number); + +#ifdef DEBUG_THREADS + pthread_mutex_lock(&thread_debug->etd_mutex); + thread_debug->etd_finished_threads++; + pthread_cond_broadcast(&thread_debug->etd_cond); + pthread_mutex_unlock(&thread_debug->etd_mutex); +#endif + return NULL; } @@ -2672,6 +2692,12 @@ static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, struct e2fsck_thread_info *tmp_pinfo; int i; e2fsck_t thread_ctx; +#ifdef DEBUG_THREADS + struct e2fsck_thread_debug thread_debug = + {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0}; + + thread_debug.etd_finished_threads = 0; +#endif retval = pthread_attr_init(&attr); if (retval) { @@ -2692,6 +2718,9 @@ static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, for (i = 0; i < num_threads; i++) { tmp_pinfo = &infos[i]; tmp_pinfo->eti_thread_index = i; +#ifdef DEBUG_THREADS + tmp_pinfo->eti_debug = &thread_debug; +#endif retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx, i, num_threads); if (retval) { -- 2.25.2