From 0fd3f6a21edd374ad5e222c0a43b8a279c63de66 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Mon, 23 Nov 2026 21:49:48 +0300 Subject: [PATCH] Return the read error instead of EDOM when a disk read fails Both stores verified the checksums of a read even when its I/O failed, over a buffer the read never filled. The check then failed, EIO became EDOM and the log got "Checksum mismatch" lines for data that is fine, for example on every read from a disk that has just been removed. Verify checksums only when the reads succeeded. --- src/blockstore/blockstore_read.cpp | 4 +-- src/blockstore/v1/read.cpp | 9 +++-- src/test/test_blockstore.cpp | 50 ++++++++++++++++++++++++++++ src/test/test_blockstore_v1.cpp | 53 ++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 4 deletions(-) diff --git a/src/blockstore/blockstore_read.cpp b/src/blockstore/blockstore_read.cpp index b90dfe6a..e96c2ab7 100644 --- a/src/blockstore/blockstore_read.cpp +++ b/src/blockstore/blockstore_read.cpp @@ -405,8 +405,8 @@ void blockstore_impl_t::handle_read_event(ring_data_t *data, blockstore_op_t *op } if (PRIV(op)->pending_ops == 0) { - // verify checksums if required - if (dsk.csum_block_size && !verify_read_checksums(op)) + // verify checksums if required, but not of a buffer that a failed read didn't fill + if (op->retval >= 0 && dsk.csum_block_size && !verify_read_checksums(op)) op->retval = -EDOM; else if (op->retval == 0) op->retval = op->len; diff --git a/src/blockstore/v1/read.cpp b/src/blockstore/v1/read.cpp index d590b78a..9ec44259 100644 --- a/src/blockstore/v1/read.cpp +++ b/src/blockstore/v1/read.cpp @@ -941,6 +941,7 @@ void blockstore_impl_t::handle_read_event(ring_data_t *data, blockstore_op_t *op if (dsk.csum_block_size) { // verify checksums if required + bool read_failed = op->retval < 0; auto & rv = PRIV(op)->read_vec; void *meta_block = NULL; if (dsk.csum_block_size > dsk.bitmap_granularity) @@ -999,7 +1000,11 @@ void blockstore_impl_t::handle_read_event(ring_data_t *data, blockstore_op_t *op single_iov = { .iov_base = op->buf + rv[i].offset - op->offset, .iov_len = rv[i].len }; } bool ok = true; - if (rv[i].copy_flags & COPY_BUF_JOURNAL) + if (read_failed) + { + // Nothing to verify, the failed read didn't fill the buffer + } + else if (rv[i].copy_flags & COPY_BUF_JOURNAL) { // SMALL_WRITE from journal verify_journal_checksums( @@ -1062,7 +1067,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 && ((vec.copy_flags & COPY_BUF_JOURNAL) || !clean_changed)) + if (!read_failed && 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.cpp b/src/test/test_blockstore.cpp index e01beba5..6b71fd35 100644 --- a/src/test/test_blockstore.cpp +++ b/src/test/test_blockstore.cpp @@ -2523,6 +2523,55 @@ static void test_tombstone() } } +// A read that fails with an I/O error has to return that error. Checksums of the buffer +// that the failed read didn't fill must not be verified and turn it into -EDOM +static void test_read_error_is_not_edom() +{ + printf("\n-- test_read_error_is_not_edom\n"); + + bs_test_t test; + test.default_cfg(); + test.init(); + + blockstore_op_t op; + op.opcode = BS_OP_WRITE_STABLE; + op.oid = { .inode = 1, .stripe = 0 }; + op.version = 1; + op.offset = 0; + op.len = 16*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); + + // Fail every read from the data area + int failed_reads = 0; + test.sqe_handler = [&](io_uring_sqe *sqe) + { + if (sqe->fd != MOCK_DATA_FD || sqe->opcode != IORING_OP_READV || sqe->off < test.bs->dsk.data_offset) + return false; + ((ring_data_t*)sqe->user_data)->res = -EIO; + test.ringloop->mark_completed((ring_data_t*)sqe->user_data); + failed_reads++; + return true; + }; + blockstore_op_t read_op; + read_op.opcode = BS_OP_READ; + read_op.oid = { .inode = 1, .stripe = 0 }; + read_op.version = UINT64_MAX; + read_op.offset = 0; + read_op.len = 16*1024; + read_op.buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, read_op.len); + memset(read_op.buf, 0xCC, read_op.len); + test.exec_op(&read_op); + test.sqe_handler = NULL; + assert(failed_reads > 0); + assert(read_op.retval == -EIO); + + free(op.buf); + free(read_op.buf); +} + int main(int narg, char *args[]) { test_simple(); @@ -2556,5 +2605,6 @@ int main(int narg, char *args[]) test_no_intent_over_unwritten_compaction(); test_big_write_after_redirect_intent(); test_tombstone(); + test_read_error_is_not_edom(); return 0; } diff --git a/src/test/test_blockstore_v1.cpp b/src/test/test_blockstore_v1.cpp index 5dd7c60e..7f654a6e 100644 --- a/src/test/test_blockstore_v1.cpp +++ b/src/test/test_blockstore_v1.cpp @@ -3222,6 +3222,57 @@ static void test_tombstone_unsupported() assert(op.retval == -EOPNOTSUPP); } +// A read that fails with an I/O error has to return that error. Checksums of the buffer +// that the failed read didn't fill must not be verified and turn it into -EDOM +static void test_read_error_is_not_edom(const char *csum_block_size) +{ + printf("\n-- test_read_error_is_not_edom csum_block_size=%s\n", csum_block_size); + + bs_test_t test; + test.default_cfg(); + test.config["data_csum_type"] = "crc32c"; + test.config["csum_block_size"] = csum_block_size; + test.init(); + + blockstore_op_t op; + op.opcode = BS_OP_WRITE_STABLE; + op.oid = { .inode = 1, .stripe = 0 }; + op.version = 1; + op.offset = 0; + op.len = 16*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); + + // Fail every read from the data area + int failed_reads = 0; + test.sqe_handler = [&](io_uring_sqe *sqe) + { + if (sqe->fd != MOCK_DATA_FD || sqe->opcode != IORING_OP_READV || sqe->off < test.dsk().data_offset) + return false; + ((ring_data_t*)sqe->user_data)->res = -EIO; + test.ringloop->mark_completed((ring_data_t*)sqe->user_data); + failed_reads++; + return true; + }; + blockstore_op_t read_op; + read_op.opcode = BS_OP_READ; + read_op.oid = { .inode = 1, .stripe = 0 }; + read_op.version = UINT64_MAX; + read_op.offset = 0; + read_op.len = 16*1024; + read_op.buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, read_op.len); + memset(read_op.buf, 0xCC, read_op.len); + test.exec_op(&read_op); + test.sqe_handler = NULL; + assert(failed_reads > 0); + assert(read_op.retval == -EIO); + + free(op.buf); + free(read_op.buf); +} + int main(int narg, char *args[]) { test_preserve_corruption(); @@ -3236,6 +3287,8 @@ int main(int narg, char *args[]) 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_read_error_is_not_edom("16384"); + test_read_error_is_not_edom("4096"); test_meta_tail_reload(); test_write_no_space_eagain(); test_read_bitmap_of_deleted_object(); -- 2.47.3