From dff684b27a6ce0946a23c258ef30e94d4df3df41 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 18 Nov 2026 11:15:47 +0300 Subject: [PATCH 1/2] Store v1: Do not skip checksum verification of reads that start after a parallel flush used_clean_obj_t::was_changed was set by a flush (or by a read started during one) and never reset while the entry had references, so a read started after the flush had finished, but while an older read still held the same clean block, also returned data without verifying checksums. Count flushes in the entry instead and remember the count at the start of each read, skipping verification only if it changed or a flush was already running. --- src/blockstore/v1/flush.cpp | 2 +- src/blockstore/v1/impl.h | 4 +- src/blockstore/v1/read.cpp | 8 +-- src/test/test_blockstore_v1.cpp | 103 ++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 6 deletions(-) diff --git a/src/blockstore/v1/flush.cpp b/src/blockstore/v1/flush.cpp index 8832952d..0ebf84f3 100644 --- a/src/blockstore/v1/flush.cpp +++ b/src/blockstore/v1/flush.cpp @@ -543,7 +543,7 @@ resume_2: auto uo_it = bs->used_clean_objects.find(clean_loc); if (uo_it != bs->used_clean_objects.end()) { - uo_it->second.was_changed = true; + uo_it->second.changes++; } if (!bs->journal.inmemory) { diff --git a/src/blockstore/v1/impl.h b/src/blockstore/v1/impl.h index 0e67640a..3a8cf69c 100644 --- a/src/blockstore/v1/impl.h +++ b/src/blockstore/v1/impl.h @@ -78,7 +78,7 @@ struct used_clean_obj_t { int refs; bool was_freed; // was freed by a parallel flush? - bool was_changed; // was changed by a parallel flush? + uint64_t changes; // number of parallel flushes which changed it while it was used }; // https://github.com/algorithm-ninja/cpp-btree @@ -101,6 +101,8 @@ struct blockstore_op_private_t // Read uint64_t clean_loc_used; + // used_clean_obj_t::changes at read start, UINT64_MAX if a flush was already running + uint64_t clean_changes; std::vector read_vec; // Sync, write diff --git a/src/blockstore/v1/read.cpp b/src/blockstore/v1/read.cpp index e6426a9c..587c32d7 100644 --- a/src/blockstore/v1/read.cpp +++ b/src/blockstore/v1/read.cpp @@ -787,8 +787,8 @@ bool blockstore_impl_t::fulfill_clean_read(blockstore_op_t *read_op, uint64_t & { auto & uo = used_clean_objects[clean_loc]; uo.refs++; - if (dsk.csum_block_size && flusher->is_mutated(clean_loc)) - uo.was_changed = true; + PRIV(read_op)->clean_changes = dsk.csum_block_size && flusher->is_mutated(clean_loc) + ? UINT64_MAX : uo.changes; PRIV(read_op)->clean_loc_used = clean_loc + 1; } return true; @@ -1018,9 +1018,9 @@ void blockstore_impl_t::handle_read_event(ring_data_t *data, blockstore_op_t *op else { // BIG_WRITE from journal or clean data - // Do not verify checksums if the data location is/was mutated by flushers + // Do not verify checksums if the data location was mutated by a flush during this read auto & uo = used_clean_objects.at((rv[i].disk_offset / dsk.data_block_size) * dsk.data_block_size); - if (!uo.was_changed) + if (PRIV(op)->clean_changes != UINT64_MAX && uo.changes == PRIV(op)->clean_changes) { verify_clean_padded_checksums( op, rv[i].disk_offset, rv[i].csum_buf, (rv[i].copy_flags & COPY_BUF_JOURNALED_BIG), iov, n_iov, diff --git a/src/test/test_blockstore_v1.cpp b/src/test/test_blockstore_v1.cpp index b3c71cc8..ebfc0ee7 100644 --- a/src/test/test_blockstore_v1.cpp +++ b/src/test/test_blockstore_v1.cpp @@ -851,6 +851,108 @@ static void test_read_free_clean_loc_used() free(read_op.buf); } +// A read which starts after a flush has finished must verify checksums even if +// an older read, which overlapped the flush, still holds the same clean block +static void test_read_after_flush_verifies_csum() +{ + printf("\n-- test_read_after_flush_verifies_csum\n"); + + bs_test_t test; + test.default_cfg(); + test.config["data_csum_type"] = "crc32c"; + test.config["csum_block_size"] = "16384"; + test.init(); + + printf("write v1 0+128k\n"); + blockstore_op_t op; + op.opcode = BS_OP_WRITE_STABLE; + op.oid = { .inode = 1, .stripe = 0 }; + op.version = 1; + op.offset = 0; + op.len = 128*1024; + op.buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, op.len); + memset(op.buf, 0xAA, op.len); + test.exec_op(&op); + assert(op.retval == op.len); + test.force_compaction(); + + printf("start read A 0+128k and pause its completion\n"); + ring_data_t *data_read = NULL; + uint64_t clean_loc = 0; + test.sqe_handler = [&](io_uring_sqe *sqe) + { + if (sqe->off >= test.dsk().data_offset && sqe->fd == MOCK_DATA_FD && !data_read) + { + bool ok = test.data_disk->submit(sqe); + assert(ok); + clean_loc = sqe->off - test.dsk().data_offset; + data_read = (ring_data_t*)sqe->user_data; + return true; + } + return false; + }; + blockstore_op_t read_a; + read_a.opcode = BS_OP_READ; + read_a.oid = op.oid; + read_a.version = UINT64_MAX; + read_a.offset = 0; + read_a.len = 128*1024; + read_a.buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, read_a.len); + bool done_a = false; + read_a.callback = [&](blockstore_op_t *op) { done_a = true; }; + test.bs->enqueue_op(&read_a); + while (!data_read) + test.ringloop->loop(); + + printf("write v2 16k+4k and flush it in place while read A is running\n"); + op.version = 2; + op.offset = 16384; + op.len = 4096; + memset(op.buf, 0xBB, op.len); + test.exec_op(&op); + assert(op.retval == op.len); + test.force_compaction(); + + printf("corrupt clean data at 64k\n"); + { + uint8_t *buf = (uint8_t*)malloc_or_die(4096); + memset(buf, 0xCC, 4096); + io_uring_sqe sqe = {}; + ring_data_t data = {}; + iovec v = { .iov_base = buf, .iov_len = 4096 }; + sqe.opcode = IORING_OP_WRITEV; + sqe.off = test.dsk().data_offset + clean_loc + 65536; + sqe.addr = (uint64_t)&v; + sqe.len = 1; + sqe.user_data = (uint64_t)&data; + bool ok = test.data_disk->submit(&sqe); + assert(ok); + assert(data.res == 4096); + free(buf); + } + + printf("read B 64k+16k, started after the flush - should fail with -EDOM\n"); + blockstore_op_t read_b; + read_b.opcode = BS_OP_READ; + read_b.oid = op.oid; + read_b.version = UINT64_MAX; + read_b.offset = 65536; + read_b.len = 16384; + read_b.buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, read_b.len); + test.exec_op(&read_b); + assert(read_b.retval == -EDOM); + + printf("complete read A - it overlapped the flush, so it isn't verified\n"); + test.ringloop->mark_completed(data_read); + while (!done_a) + test.ringloop->loop(); + assert(read_a.retval == read_a.len); + + free(op.buf); + free(read_a.buf); + free(read_b.buf); +} + static void test_meta_tail_reload() { printf("\n-- test_meta_tail_reload\n"); @@ -2898,6 +3000,7 @@ int main(int narg, char *args[]) test_read_padded_big_write_outside_read(); test_read_retry_on_ring_full_1M_csum4k_clean(); test_read_free_clean_loc_used(); + test_read_after_flush_verifies_csum(); test_meta_tail_reload(); test_write_no_space_eagain(); test_read_bitmap_of_deleted_object(); -- 2.47.3 From 42ca04ad8c1f9377578172da28c46546a6a6126f Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Thu, 19 Nov 2026 21:07:35 +0300 Subject: [PATCH 2/2] Store v1: Fix false checksum mismatches of reads running in parallel to a flush A read of a clean block which was still running when the same data was overwritten and flushed could fail with a false -EDOM in two cases: - The flush moved the object to a new block (redirect-write). The metadata entry of the old block is zeroed out, in memory too, so the read verified its data against zero bitmap and checksums. Only the new location was marked as changed in used_clean_objects. - csum_block_size == bitmap_granularity and the same blocks were overwritten in place. The read verified old data against new checksums, because this branch never skipped verification. Mark the old location as changed too, and do not verify clean data in the second branch if its location was changed during the read. Reads which start during a flush are still verified there, because they take the overwritten blocks from the journal. --- src/blockstore/v1/flush.cpp | 6 ++++++ src/blockstore/v1/impl.h | 2 +- src/blockstore/v1/read.cpp | 7 +++++-- src/test/test_blockstore_v1.cpp | 29 ++++++++++++++++++----------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/blockstore/v1/flush.cpp b/src/blockstore/v1/flush.cpp index 0ebf84f3..04d30210 100644 --- a/src/blockstore/v1/flush.cpp +++ b/src/blockstore/v1/flush.cpp @@ -601,6 +601,12 @@ resume_2: { // zero out old metadata entry { + // Reads of the old location must not verify checksums against the zeroed entry + auto uo_it = bs->used_clean_objects.find(old_clean_loc); + if (uo_it != bs->used_clean_objects.end()) + { + uo_it->second.changes++; + } clean_disk_entry *old_entry = (clean_disk_entry*)((uint8_t*)meta_old.buf + meta_old.pos*bs->dsk.clean_entry_size); if (old_entry->oid.inode != 0 && old_entry->oid != cur.oid) { diff --git a/src/blockstore/v1/impl.h b/src/blockstore/v1/impl.h index 3a8cf69c..160624b9 100644 --- a/src/blockstore/v1/impl.h +++ b/src/blockstore/v1/impl.h @@ -101,7 +101,7 @@ struct blockstore_op_private_t // Read uint64_t clean_loc_used; - // used_clean_obj_t::changes at read start, UINT64_MAX if a flush was already running + // used_clean_obj_t::changes at read start, UINT64_MAX if a flush was already running and the read is padded uint64_t clean_changes; std::vector read_vec; diff --git a/src/blockstore/v1/read.cpp b/src/blockstore/v1/read.cpp index 587c32d7..d590b78a 100644 --- a/src/blockstore/v1/read.cpp +++ b/src/blockstore/v1/read.cpp @@ -787,7 +787,7 @@ bool blockstore_impl_t::fulfill_clean_read(blockstore_op_t *read_op, uint64_t & { auto & uo = used_clean_objects[clean_loc]; uo.refs++; - PRIV(read_op)->clean_changes = dsk.csum_block_size && flusher->is_mutated(clean_loc) + PRIV(read_op)->clean_changes = dsk.csum_block_size > dsk.bitmap_granularity && flusher->is_mutated(clean_loc) ? UINT64_MAX : uo.changes; PRIV(read_op)->clean_loc_used = clean_loc + 1; } @@ -1048,6 +1048,9 @@ void blockstore_impl_t::handle_read_event(ring_data_t *data, blockstore_op_t *op } else { + // Do not verify clean data if its location was mutated by a flush during this read + bool clean_changed = PRIV(op)->clean_loc_used && PRIV(op)->clean_changes != + used_clean_objects.at(PRIV(op)->clean_loc_used-1).changes; for (auto & vec: rv) { if (vec.copy_flags & COPY_BUF_META_BLOCK) @@ -1059,7 +1062,7 @@ void blockstore_impl_t::handle_read_event(ring_data_t *data, blockstore_op_t *op free_dirty_dyn_data(vec); continue; } - if (vec.csum_buf) + if (vec.csum_buf && ((vec.copy_flags & COPY_BUF_JOURNAL) || !clean_changed)) { uint32_t *csum = (uint32_t*)vec.csum_buf; for (size_t p = 0; p < vec.len; p += dsk.csum_block_size, csum++) diff --git a/src/test/test_blockstore_v1.cpp b/src/test/test_blockstore_v1.cpp index ebfc0ee7..ed59422a 100644 --- a/src/test/test_blockstore_v1.cpp +++ b/src/test/test_blockstore_v1.cpp @@ -852,15 +852,17 @@ static void test_read_free_clean_loc_used() } // A read which starts after a flush has finished must verify checksums even if -// an older read, which overlapped the flush, still holds the same clean block -static void test_read_after_flush_verifies_csum() +// an older read, which overlapped the flush, still holds the same clean block. +// And that older read must not fail with a false -EDOM, neither when the flush +// changes the block in place, nor when it moves the object to a new block (big) +static void test_read_after_flush_verifies_csum(const char *csum_block_size, bool big) { - printf("\n-- test_read_after_flush_verifies_csum\n"); + printf("\n-- test_read_after_flush_verifies_csum csum_block_size=%s%s\n", csum_block_size, big ? " big" : ""); bs_test_t test; test.default_cfg(); test.config["data_csum_type"] = "crc32c"; - test.config["csum_block_size"] = "16384"; + test.config["csum_block_size"] = csum_block_size; test.init(); printf("write v1 0+128k\n"); @@ -904,17 +906,19 @@ static void test_read_after_flush_verifies_csum() while (!data_read) test.ringloop->loop(); - printf("write v2 16k+4k and flush it in place while read A is running\n"); + printf("write v2 %s and flush it while read A is running\n", big ? "0+128k" : "16k+4k"); op.version = 2; - op.offset = 16384; - op.len = 4096; + op.offset = big ? 0 : 16384; + op.len = big ? 128*1024 : 4096; memset(op.buf, 0xBB, op.len); test.exec_op(&op); assert(op.retval == op.len); test.force_compaction(); - printf("corrupt clean data at 64k\n"); + // The object is moved by a big write, so there's nothing to corrupt in the old block + if (!big) { + printf("corrupt clean data at 64k\n"); uint8_t *buf = (uint8_t*)malloc_or_die(4096); memset(buf, 0xCC, 4096); io_uring_sqe sqe = {}; @@ -931,7 +935,7 @@ static void test_read_after_flush_verifies_csum() free(buf); } - printf("read B 64k+16k, started after the flush - should fail with -EDOM\n"); + printf("read B 64k+16k, started after the flush - should %s\n", big ? "succeed" : "fail with -EDOM"); blockstore_op_t read_b; read_b.opcode = BS_OP_READ; read_b.oid = op.oid; @@ -940,7 +944,7 @@ static void test_read_after_flush_verifies_csum() read_b.len = 16384; read_b.buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, read_b.len); test.exec_op(&read_b); - assert(read_b.retval == -EDOM); + assert(read_b.retval == (big ? (int)read_b.len : -EDOM)); printf("complete read A - it overlapped the flush, so it isn't verified\n"); test.ringloop->mark_completed(data_read); @@ -3000,7 +3004,10 @@ int main(int narg, char *args[]) test_read_padded_big_write_outside_read(); test_read_retry_on_ring_full_1M_csum4k_clean(); test_read_free_clean_loc_used(); - test_read_after_flush_verifies_csum(); + test_read_after_flush_verifies_csum("16384", false); + test_read_after_flush_verifies_csum("16384", true); + test_read_after_flush_verifies_csum("4096", false); + test_read_after_flush_verifies_csum("4096", true); test_meta_tail_reload(); test_write_no_space_eagain(); test_read_bitmap_of_deleted_object(); -- 2.47.3