diff --git a/src/blockstore/v1/flush.cpp b/src/blockstore/v1/flush.cpp index 8832952d..04d30210 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) { @@ -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 0e67640a..160624b9 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 and the read is padded + 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..d590b78a 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 > dsk.bitmap_granularity && 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, @@ -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 b3c71cc8..ed59422a 100644 --- a/src/test/test_blockstore_v1.cpp +++ b/src/test/test_blockstore_v1.cpp @@ -851,6 +851,112 @@ 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. +// 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 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"] = csum_block_size; + 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 %s and flush it while read A is running\n", big ? "0+128k" : "16k+4k"); + op.version = 2; + 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(); + + // 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 = {}; + 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 %s\n", big ? "succeed" : "fail with -EDOM"); + 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 == (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); + 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 +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("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();