From 78807eb24454077acbdde7b86ecdb19181c677c1 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Wed, 27 Nov 2019 11:35:11 +0300 Subject: [PATCH] Fix journal space check (do not overwrite the beginning of the journal) --- blockstore_journal.cpp | 8 ++++---- blockstore_journal.h | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/blockstore_journal.cpp b/blockstore_journal.cpp index 937c19ec..047dccce 100644 --- a/blockstore_journal.cpp +++ b/blockstore_journal.cpp @@ -7,12 +7,12 @@ blockstore_journal_check_t::blockstore_journal_check_t(blockstore *bs) next_pos = bs->journal.next_free; next_sector = bs->journal.cur_sector; next_in_pos = bs->journal.in_sector_pos; + right_dir = next_pos >= bs->journal.used_start; } // Check if we can write entries of bytes and data bytes after them to the journal int blockstore_journal_check_t::check_available(blockstore_operation *op, int required, int size, int data_after) { - bool wrapped = false; while (1) { int fits = (512 - next_in_pos) / size; @@ -30,7 +30,7 @@ int blockstore_journal_check_t::check_available(blockstore_operation *op, int re if (next_pos >= bs->journal.len) { next_pos = 512; - wrapped = true; + right_dir = false; } next_in_pos = 0; if (bs->journal.sector_info[next_sector].usage_count > 0) @@ -49,11 +49,11 @@ int blockstore_journal_check_t::check_available(blockstore_operation *op, int re next_pos = next_pos + data_after; if (next_pos > bs->journal.len) { - wrapped = true; next_pos = 512 + data_after; + right_dir = false; } } - if (wrapped && next_pos >= bs->journal.used_start) + if (!right_dir && next_pos >= bs->journal.used_start-512) { // No space in the journal. Wait for it. op->wait_for = WAIT_JOURNAL; diff --git a/blockstore_journal.h b/blockstore_journal.h index e2ff8192..7773c8c5 100644 --- a/blockstore_journal.h +++ b/blockstore_journal.h @@ -137,6 +137,7 @@ struct blockstore_journal_check_t blockstore *bs; uint64_t next_pos, next_sector, next_in_pos; int sectors_required; + bool right_dir; // writing to the end or the beginning of the ring buffer blockstore_journal_check_t(blockstore *bs); int check_available(blockstore_operation *op, int required, int size, int data_after);