From 2b716f60b473212848cf07f0009a9f445b6724ca Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 03:03:59 +0300 Subject: [PATCH] osd: Free the old object state in mark_object() only after inserting the new one mark_object() freed the object's old state entry before it inserted the new one. When the object was alone in its state, malloc returned the same block, so the new state had the old address. Callers tell a swap by comparing the pointers, so a primary read that had just marked a copy corrupted saw no change and failed with EIO instead of retrying on the copy that is left. Unlink the object from the old state first, insert it into the new one, and release the old entry last. --- src/osd/osd_primary.cpp | 5 ++- src/test/osd_test.cpp | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/osd/osd_primary.cpp b/src/osd/osd_primary.cpp index be1b3bee..b127170f 100644 --- a/src/osd/osd_primary.cpp +++ b/src/osd/osd_primary.cpp @@ -534,10 +534,10 @@ pg_osd_set_state_t *osd_t::mark_object(pg_t & pg, object_id oid, pg_osd_set_stat return object_state; } int old_pg_state = pg.state; + pg_osd_set_state_t *old_state = object_state; if (object_state) { remove_object_from_state(oid, &object_state, pg, false); - deref_object_state(pg, &object_state, ref); } // Insert object into the new state and retry object_state = add_object_to_set(pg, oid, new_set, old_pg_state, 2); @@ -545,6 +545,9 @@ pg_osd_set_state_t *osd_t::mark_object(pg_t & pg, object_id oid, pg_osd_set_stat { object_state->ref_count++; } + // Free the old state only now: callers detect the swap by comparing pointers, + // and a new state allocated at the freed address would look like no change + deref_object_state(pg, &old_state, ref); return object_state; } diff --git a/src/test/osd_test.cpp b/src/test/osd_test.cpp index e0db38fa..5cd5f0f2 100644 --- a/src/test/osd_test.cpp +++ b/src/test/osd_test.cpp @@ -4652,6 +4652,78 @@ void test_replicated_add_object_to_set_degraded() printf("test_replicated_add_object_to_set_degraded passed\n"); } +// A read that marks a copy corrupted retries when the object's state changed. Here the +// object is alone in its state {1 corrupted, 2, 3}, so the swap frees that entry, and the +// new state may come back at the same address: the read must still retry on OSD 3 +void test_read_eio_retries_when_mark_reuses_state() +{ + printf("test_read_eio_retries_when_mark_reuses_state\n"); + + osd_test_fixture_t f; + f.configure_replicated_pool(/*pool_id*/ 1, /*pg_size*/ 3, /*pg_minsize*/ 2, /*pg_count*/ 1, + { { 1, 2, 3 } }); + f.start(json11::Json::object { + { "osd_num", 1 }, + { "etcd_address", "127.0.0.1:2379" }, + { "immediate_commit", "all" }, + { "block_size", 131072 }, + { "bitmap_granularity", 4096 }, + { "no_recovery", true }, + }); + f.connect_peer(2); + f.connect_peer(3); + + inode_t inode = INODE_WITH_POOL(1, 1); + object_id oid = { .inode = inode, .stripe = 0 }; + f.reply_local_list({ { oid, 1 } }, 1); + f.reply_peer_list(2, { { oid, 1 } }, 1); + f.reply_peer_list(3, { { oid, 1 } }, 1); + f.pump(); + auto & pg = f.pg(1, 1); + assert(pg.state == PG_ACTIVE); + + int read_retval = -1; + auto *read = make_self_rw(OSD_OP_READ, inode, 0, 4096); + read->callback = [&read_retval](osd_op_t *op) { read_retval = op->reply.hdr.retval; }; + f.exec(read); + auto *local_read = f.bs->take(BS_OP_READ); + assert(local_read->len == 4096); + local_read->retval = -EIO; + local_read->callback(local_read); + auto *sr = f.peer_take(2, OSD_OP_SEC_READ); + sr->reply.sec_rw.version = 1; + f.peer_complete(sr, 4096); + assert(read_retval == 4096); + delete read; + assert(pg.state == (PG_ACTIVE|PG_HAS_DEGRADED|PG_HAS_CORRUPTED)); + assert(pg.degraded_objects.size() == 1 && pg.state_dict.size() == 1); + pg_osd_set_state_t *old_state = pg.degraded_objects.find(oid)->second; + assert(old_state->object_count == 1 && old_state->ref_count == 0); + + read_retval = -1; + read = make_self_rw(OSD_OP_READ, inode, 0, 4096); + read->callback = [&read_retval](osd_op_t *op) { read_retval = op->reply.hdr.retval; }; + f.exec(read); + assert(f.bs->take(BS_OP_READ, false) == NULL); + sr = f.peer_take(2, OSD_OP_SEC_READ); + f.peer_complete(sr, -EIO); + + // OSD 2 got marked: the state is a different set, whatever its address + pg_osd_set_state_t *new_state = pg.degraded_objects.find(oid)->second; + int n_corrupted = 0; + for (auto & loc: new_state->osd_set) + n_corrupted += (loc.loc_bad & LOC_CORRUPTED) ? 1 : 0; + assert(n_corrupted == 2 && pg.state_dict.size() == 1); + auto *retry = f.peer_take_opt(3, OSD_OP_SEC_READ); + assert(retry); + retry->reply.sec_rw.version = 1; + f.peer_complete(retry, 4096); + assert(read_retval == 4096); + delete read; + + printf("test_read_eio_retries_when_mark_reuses_state passed\n"); +} + int main(int narg, char *args[]) { test_load_global_config(); @@ -4718,6 +4790,7 @@ int main(int narg, char *args[]) test_delete_waits_for_stabilize_snapshot(); test_delete_waits_only_for_covered_objects(); test_replicated_add_object_to_set_degraded(); + test_read_eio_retries_when_mark_reuses_state(); test_tombstone_object_counters(); test_tombstone_inline_misplaced(); test_tombstone_forget_counters(); -- 2.47.3