diff --git a/src/blockstore/ondisk_formats.h b/src/blockstore/ondisk_formats.h index 6faa950a..74e1cdf1 100644 --- a/src/blockstore/ondisk_formats.h +++ b/src/blockstore/ondisk_formats.h @@ -33,7 +33,7 @@ struct __attribute__((__packed__)) journal_entry_start uint16_t magic; uint16_t type; uint32_t size; - uint32_t reserved; + uint32_t reserved; // crc32_prev of the first entry at journal_start, 0 = unknown (older versions) uint64_t journal_start; uint64_t version; uint32_t data_csum_type; diff --git a/src/blockstore/v1/flush.cpp b/src/blockstore/v1/flush.cpp index f554f76b..175062e9 100644 --- a/src/blockstore/v1/flush.cpp +++ b/src/blockstore/v1/flush.cpp @@ -1512,7 +1512,7 @@ bool journal_flusher_co::trim_journal(int wait_base) .type = JE_START, .size = ((!bs->dsk.data_csum_type && ((journal_entry_start*)flusher->journal_superblock)->version == JOURNAL_VERSION_V1) ? (uint32_t)JE_START_V1_SIZE : (uint32_t)JE_START_V2_SIZE), - .reserved = 0, + .reserved = bs->journal.get_crc32_prev(new_trim_pos), .journal_start = new_trim_pos, .version = (uint64_t)(!bs->dsk.data_csum_type && ((journal_entry_start*)flusher->journal_superblock)->version == JOURNAL_VERSION_V1 ? JOURNAL_VERSION_V1 : JOURNAL_VERSION_V2), @@ -1550,6 +1550,7 @@ bool journal_flusher_co::trim_journal(int wait_base) { bs->journal.dirty_start = new_trim_pos; } + bs->journal.forget_sectors(bs->journal.used_start, new_trim_pos); bs->journal.used_start = new_trim_pos; #ifdef BLOCKSTORE_DEBUG printf("Journal trimmed to %08jx (next_free=%08jx dirty_start=%08jx)\n", bs->journal.used_start, bs->journal.next_free, bs->journal.dirty_start); diff --git a/src/blockstore/v1/init.cpp b/src/blockstore/v1/init.cpp index 667a0595..f606c1f8 100644 --- a/src/blockstore/v1/init.cpp +++ b/src/blockstore/v1/init.cpp @@ -1,6 +1,7 @@ // Copyright (c) Vitaliy Filippov, 2019+ // License: VNPL-1.1 (see README.md for details) +#include #include "str_util.h" #include "impl.h" #include "internal.h" @@ -512,6 +513,8 @@ int blockstore_init_journal::loop() goto resume_7; else if (wait_state == 8) goto resume_8; + else if (wait_state == 9) + goto resume_9; printf("Reading blockstore journal\n"); if (!bs->journal.inmemory) submitted_buf = memalign_or_die(MEM_ALIGNMENT, 2*bs->journal.block_size); @@ -559,8 +562,6 @@ resume_1: } else { - // Cool effect. Same operations result in journal replay. - // FIXME: Randomize initial crc32. Track crc32 when trimming. printf("Resetting journal\n"); GET_SQE(); data->iov = (struct iovec){ submitted_buf, (size_t)(2*bs->journal.block_size) }; @@ -637,10 +638,13 @@ resume_1: exit(1); } next_free = journal_pos = bs->journal.used_start = je_start->journal_start; + // The trim records what the first entry chains to: if the last write of that sector was + // lost, it still holds an earlier pass of the journal, which must not be replayed + crc32_last = je_start->reserved; + started = crc32_last != 0; if (!bs->journal.inmemory) free(submitted_buf); submitted_buf = NULL; - crc32_last = 0; // Read journal while (1) { @@ -753,6 +757,50 @@ resume_1: exit(1); } } + // next_free == used_start after replaying anything means the replay went all the way around + if (!bs->readonly && !(entries_loaded > 0 && bs->journal.next_free == bs->journal.used_start) && + (bs->journal.next_free >= bs->journal.used_start + ? bs->journal.len-bs->journal.block_size - (bs->journal.next_free-bs->journal.used_start) + : bs->journal.used_start - bs->journal.next_free) > bs->journal.block_size) + { + // Sectors a crashed instance left behind a hole chain to the same entries written into it again, + // so start our chain with an entry nobody wrote: a no-op rollback of the absent object 0:0 + init_write_buf = bs->journal.inmemory + ? (uint8_t*)bs->journal.buffer + bs->journal.next_free + : memalign_or_die(MEM_ALIGNMENT, bs->journal.block_size); + memset(init_write_buf, 0, bs->journal.block_size); + { + journal_entry *je = (journal_entry*)init_write_buf; + std::random_device rd; + je->magic = JOURNAL_MAGIC; + je->type = JE_ROLLBACK; + je->size = sizeof(journal_entry_rollback); + je->crc32_prev = crc32_last; + je->rollback.oid = {}; + je->rollback.version = ((uint64_t)rd() << 32) | rd(); + bs->journal.sector_crc32_prev[bs->journal.next_free] = crc32_last; + je->crc32 = je_crc32(je); + crc32_last = je->crc32; + } + GET_SQE(); + data->iov = { init_write_buf, (size_t)bs->journal.block_size }; + data->callback = simple_callback; + io_uring_prep_writev(sqe, bs->dsk.journal_fd, &data->iov, 1, bs->journal.offset + bs->journal.next_free); + wait_count++; + bs->ringloop->submit(); + resume_9: + if (wait_count > 0) + { + wait_state = 9; + return 1; + } + if (!bs->journal.inmemory) + free(init_write_buf); + init_write_buf = NULL; + bs->journal.next_free += bs->journal.block_size; + if (bs->journal.next_free >= bs->journal.len) + bs->journal.next_free = bs->journal.block_size; + } bs->flusher->mark_trim_possible(); bs->journal.dirty_start = bs->journal.next_free; if (!bs->readonly) @@ -845,6 +893,10 @@ int blockstore_init_journal::handle_journal_part(void *buf, uint64_t done_pos, u break; } } + if (pos == 0) + { + bs->journal.sector_crc32_prev[proc_pos] = je->crc32_prev; + } if (je->type == JE_SMALL_WRITE || je->type == JE_SMALL_WRITE_INSTANT) { #ifdef BLOCKSTORE_DEBUG diff --git a/src/blockstore/v1/journal.cpp b/src/blockstore/v1/journal.cpp index 845376b7..01aeb25a 100644 --- a/src/blockstore/v1/journal.cpp +++ b/src/blockstore/v1/journal.cpp @@ -159,6 +159,7 @@ journal_entry* prefill_single_journal_entry(journal_t & journal, uint16_t type, } journal.sector_info[journal.cur_sector].written = false; journal.sector_info[journal.cur_sector].offset = journal.next_free; + journal.sector_crc32_prev[journal.next_free] = journal.crc32_last; journal.in_sector_pos = 0; auto next_next_free = (journal.next_free+journal.block_size) < journal.len ? journal.next_free + journal.block_size : journal.block_size; // double check that next_free doesn't cross used_start from the left @@ -289,7 +290,7 @@ journal_t::~journal_t() buffer = NULL; } -uint64_t journal_t::get_trim_pos() +uint64_t journal_t::get_used_trim_pos() { auto journal_used_it = used_sectors.lower_bound(used_start); if (journal_used_it == used_sectors.end()) @@ -330,6 +331,47 @@ uint64_t journal_t::get_trim_pos() return used_start; } +static bool in_ring_range(uint64_t pos, uint64_t start, uint64_t end) +{ + return start <= end ? (pos >= start && pos < end) : (pos >= start || pos < end); +} + +uint64_t journal_t::get_trim_pos() +{ + uint64_t pos = get_used_trim_pos(); + // Entries may still be appended to the current sector even if nothing in it is in use + uint64_t cur = sector_info[cur_sector].offset; + if (in_sector_pos < block_size && !(sector_info[cur_sector].written && no_same_sector_overwrites) && + pos != used_start && in_ring_range(cur, used_start, pos)) + { + return cur; + } + return pos; +} + +// What the first entry at chains to, 0 if unknown +uint32_t journal_t::get_crc32_prev(uint64_t pos) +{ + auto it = sector_crc32_prev.find(pos); + if (it != sector_crc32_prev.end()) + return it->second; + // The next sector isn't started yet, and it will chain to the last entry + return pos == next_free ? crc32_last : 0; +} + +void journal_t::forget_sectors(uint64_t start, uint64_t end) +{ + if (start <= end) + { + sector_crc32_prev.erase(sector_crc32_prev.lower_bound(start), sector_crc32_prev.lower_bound(end)); + } + else + { + sector_crc32_prev.erase(sector_crc32_prev.lower_bound(start), sector_crc32_prev.end()); + sector_crc32_prev.erase(sector_crc32_prev.begin(), sector_crc32_prev.lower_bound(end)); + } +} + void journal_t::dump_diagnostics() { auto journal_used_it = used_sectors.lower_bound(used_start); diff --git a/src/blockstore/v1/journal.h b/src/blockstore/v1/journal.h index 866dc1cc..ba364d4f 100644 --- a/src/blockstore/v1/journal.h +++ b/src/blockstore/v1/journal.h @@ -52,10 +52,15 @@ struct journal_t // Used sector map // May use ~ 80 MB per 1 GB of used journal space in the worst case std::map used_sectors; + // crc32_prev of the first entry of each sector in use, kept in the superblock for the replay + std::map sector_crc32_prev; ~journal_t(); bool trim(); + uint64_t get_used_trim_pos(); uint64_t get_trim_pos(); + uint32_t get_crc32_prev(uint64_t pos); + void forget_sectors(uint64_t start, uint64_t end); void dump_diagnostics(); inline bool entry_fits(int size) { diff --git a/src/blockstore/v1/write.cpp b/src/blockstore/v1/write.cpp index 43e5906d..5fb55872 100644 --- a/src/blockstore/v1/write.cpp +++ b/src/blockstore/v1/write.cpp @@ -13,11 +13,12 @@ namespace v1 { // order, resurrecting the object. Returns false if a preceding write is still running - // its completion refers to the entry, so we have to let it finish first. // -// Entries which are already synced must NOT be forgotten. Their journal entries are on the disk -// and can't be taken back, while the deletion itself only reaches the journal on the next sync. -// Forgetting them drops their journal references, and the journal may then be trimmed past the -// object's big write while its small writes still sit in later sectors kept alive by other -// objects - recovery would then replay those small writes with no base under them. +// Entries which are already in the journal must NOT be forgotten: synced ones, and small writes, +// which are journaled when written. Their entries may be on the disk, or get there with another +// sync, and can't be taken back, while the deletion itself only reaches the journal on the next +// sync. Forgetting them drops their journal references, and the journal may then be trimmed past +// the object's big write or its first small writes while later small writes still sit in sectors +// kept alive by other objects - recovery would then replay those small writes with no base under them. // An entry which isn't in the unsynced lists any more has been taken over by a sync which is // still in flight: continue_sync() swaps those lists over to itself, but the entries only get // the SYNCED state later, in ack_sync() @@ -46,19 +47,17 @@ bool blockstore_impl_t::forget_unstable_before_delete(blockstore_op_t *op, block break; if (IS_IN_FLIGHT(prev_it->second.state)) return false; - if (!is_still_unsynced(IS_BIG_WRITE(prev_it->second.state) ? unsynced_big_writes : unsynced_small_writes, prev_it->first)) + if (!IS_BIG_WRITE(prev_it->second.state)) + break; + if (!is_still_unsynced(unsynced_big_writes, prev_it->first)) { // A sync in flight already owns it, so we can't erase it here: that would leave // the sync with a dangling dirty_db entry in ack_sync() and would decrement - // unstable_unsynced a second time. - // A small write is journaled as soon as it's dequeued, so it is already in front - // of our deletion. A big write is not: its journal entry is only written when the + // unstable_unsynced a second time. And its journal entry is only written when the // sync reaches its journaling phase, which may well be after our deletion is // journaled. Journal replay would then take it for a new write over the deletion // and resurrect the object, so wait for that sync to finish instead - if (IS_BIG_WRITE(prev_it->second.state)) - return false; - break; + return false; } first_it = prev_it; } @@ -71,20 +70,17 @@ bool blockstore_impl_t::forget_unstable_before_delete(blockstore_op_t *op, block PRIV(op)->forgot_unsynced = true; for (auto it = first_it; it != dirty_it; it++) { - // Drop it from the unsynced lists so that a later sync doesn't journal it - bool is_big = IS_BIG_WRITE(it->second.state); - auto & unsynced = is_big ? unsynced_big_writes : unsynced_small_writes; - for (size_t i = 0; i < unsynced.size(); i++) + // Drop it from the unsynced list so that a later sync doesn't journal it + for (size_t i = 0; i < unsynced_big_writes.size(); i++) { - if (unsynced[i].oid == it->first.oid && unsynced[i].version == it->first.version) + if (unsynced_big_writes[i] == it->first) { - unsynced.erase(unsynced.begin()+i, unsynced.begin()+i+1); - if (is_big && immediate_commit != IMMEDIATE_ALL) - unsynced_big_write_count--; + unsynced_big_writes.erase(unsynced_big_writes.begin()+i); + unsynced_big_write_count--; break; } } - if (!(it->second.state & BS_ST_INSTANT) && !IS_SYNCED(it->second.state)) + if (!(it->second.state & BS_ST_INSTANT)) { unstable_unsynced--; assert(unstable_unsynced >= 0); diff --git a/src/disk_tool/disk_tool_journal.cpp b/src/disk_tool/disk_tool_journal.cpp index a065879b..f62baabb 100644 --- a/src/disk_tool/disk_tool_journal.cpp +++ b/src/disk_tool/disk_tool_journal.cpp @@ -168,8 +168,8 @@ int disk_tool_t::process_journal(std::function block_fn, bool do_ope started = false; crc32_last = 0; block_fn(data); - started = false; - crc32_last = 0; + crc32_last = je->start.reserved; + started = crc32_last != 0; journal_pos = je->start.journal_start; while (1) { diff --git a/src/test/test_blockstore_v1.cpp b/src/test/test_blockstore_v1.cpp index 5dd7c60e..61c8d1d2 100644 --- a/src/test/test_blockstore_v1.cpp +++ b/src/test/test_blockstore_v1.cpp @@ -36,6 +36,11 @@ struct bs_test_t return bs->dsk; } + bool journal_is_empty() + { + return bs->journal.used_start == bs->journal.next_free; + } + v1::journal_flusher_t* flusher() { return bs->flusher; @@ -1434,7 +1439,8 @@ static void test_delete_over_ec_write_unstable() { auto dirty = test.find_dirty_entry(op.oid, 2); - assert(!dirty); + assert(dirty); + assert(IS_STABLE(dirty->state)); dirty = test.find_dirty_entry(op.oid, 3); assert(dirty); assert(IS_STABLE(dirty->state)); @@ -3222,6 +3228,193 @@ static void test_tombstone_unsupported() assert(op.retval == -EOPNOTSUPP); } +static int read_object_retval(bs_test_t & test, object_id oid, uint8_t *buf) +{ + blockstore_op_t rd; + rd.opcode = BS_OP_READ; + rd.oid = oid; + rd.version = UINT64_MAX; + rd.offset = 0; + rd.len = 4096; + rd.buf = buf; + test.exec_op(&rd); + return rd.retval; +} + +// Create , then lose the journal write of its deletion in a power outage while the +// journal sector of a new object behind it does reach the disk +static void lose_delete_in_front_of_written_sector(bs_test_t & test, object_id victim, object_id revenant, uint8_t *buf) +{ + uint64_t journal_start = test.dsk().journal_offset; + uint64_t journal_end = test.dsk().journal_offset + test.dsk().journal_len; + blockstore_op_t wr; + wr.opcode = BS_OP_WRITE_STABLE; + wr.oid = victim; + wr.version = 1; + wr.offset = 0; + wr.len = 4096; + wr.buf = buf; + test.exec_op(&wr); + assert(wr.retval == (int)wr.len); + + printf("deleting the object, but its journal write never reaches the disk\n"); + bool dropped = false; + test.sqe_handler = [&](io_uring_sqe *sqe) + { + if (!dropped && sqe->fd == MOCK_DATA_FD && sqe->opcode == IORING_OP_WRITEV && + sqe->off >= journal_start && sqe->off < journal_end) + { + dropped = true; + return true; + } + return false; + }; + blockstore_op_t del; + del.opcode = BS_OP_DELETE; + del.oid = victim; + del.version = 2; + del.offset = 0; + del.len = 0; + del.buf = NULL; + del.callback = [](blockstore_op_t *op) {}; + test.bs->enqueue_op(&del); + for (int i = 0; i < 20 && !dropped; i++) + test.ringloop->loop(); + assert(dropped); + + printf("creating another object, its journal sector behind the hole is written\n"); + wr.oid = revenant; + wr.callback = [](blockstore_op_t *op) {}; + test.bs->enqueue_op(&wr); + for (int i = 0; i < 20; i++) + test.ringloop->loop(); + + printf("power outage\n"); + test.sqe_handler = NULL; + test.destroy_bs(); + test.ringloop->reset(); + test.init(); + assert(read_object_retval(test, victim, buf) == 4096); + assert(read_object_retval(test, revenant, buf) == -ENOENT); +} + +static void restart_cleanly(bs_test_t & test) +{ + printf("clean restart\n"); + while (!test.bs->is_safe_to_stop()) + test.ringloop->loop(); + test.destroy_bs(); + test.ringloop->reset(); + test.init(); +} + +// Recovery stops at the hole, but the same entry written into the hole anew used to make the +// sector behind it a valid continuation again, so the next restart brought the lost object back +static void test_restart_does_not_revive_sectors_behind_a_hole() +{ + printf("\n-- test_restart_does_not_revive_sectors_behind_a_hole\n"); + + bs_test_t test; + test.default_cfg(); + // Every entry gets a sector of its own, so the lost deletion starts its sector + test.config["journal_no_same_sector_overwrites"] = "true"; + test.init(); + printf("blockstore initialized\n"); + + object_id victim = { .inode = 1, .stripe = 0 }; + object_id revenant = { .inode = 1, .stripe = 0x20000 }; + uint8_t *buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, 4096); + memset(buf, 0xAA, 4096); + lose_delete_in_front_of_written_sector(test, victim, revenant, buf); + + printf("deleting the object again writes the lost entry anew\n"); + blockstore_op_t del; + del.opcode = BS_OP_DELETE; + del.oid = victim; + del.version = 2; + del.offset = 0; + del.len = 0; + del.buf = NULL; + test.exec_op(&del); + assert(del.retval == 0); + + restart_cleanly(test); + assert(read_object_retval(test, victim, buf) == -ENOENT); + assert(read_object_retval(test, revenant, buf) == -ENOENT); + + free(buf); +} + +// A journal trimmed to its end starts where nothing of the current journal is written yet. Its +// first entry used to be taken on trust, so a sector a crashed instance had written there came back +static void test_emptied_journal_does_not_start_with_a_stale_sector() +{ + printf("\n-- test_emptied_journal_does_not_start_with_a_stale_sector\n"); + + bs_test_t test; + test.default_cfg(); + test.config["journal_no_same_sector_overwrites"] = "true"; + test.init(); + printf("blockstore initialized\n"); + + object_id victim = { .inode = 1, .stripe = 0 }; + object_id revenant = { .inode = 1, .stripe = 0x20000 }; + uint8_t *buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, 4096); + memset(buf, 0xAA, 4096); + lose_delete_in_front_of_written_sector(test, victim, revenant, buf); + + printf("flushing everything trims the journal to its end\n"); + test.force_compaction(); + assert(test.journal_is_empty()); + + restart_cleanly(test); + assert(read_object_retval(test, victim, buf) == 4096); + assert(read_object_retval(test, revenant, buf) == -ENOENT); + + free(buf); +} + +// Preparing an OSD again zeroes only the first block of its journal, and sectors of the previous +// OSD stay behind it. A chain starting the same way as theirs would continue into them on replay +static void test_new_journal_does_not_chain_to_a_previous_one() +{ + printf("\n-- test_new_journal_does_not_chain_to_a_previous_one\n"); + + bs_test_t test; + test.default_cfg(); + test.config["journal_no_same_sector_overwrites"] = "true"; + test.init(); + printf("blockstore initialized\n"); + + object_id oid = { .inode = 1, .stripe = 0 }; + uint8_t *buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, 4096); + memset(buf, 0xAA, 4096); + blockstore_op_t wr; + wr.opcode = BS_OP_WRITE_STABLE; + wr.oid = oid; + wr.version = 1; + wr.offset = 0; + wr.len = 4096; + wr.buf = buf; + test.exec_op(&wr); + assert(wr.retval == (int)wr.len); + while (!test.bs->is_safe_to_stop()) + test.ringloop->loop(); + test.destroy_bs(); + + printf("preparing the OSD again zeroes the first blocks of its metadata and journal\n"); + test.data_disk->clear(parse_size(test.config["meta_offset"]), 4096); + test.data_disk->clear(parse_size(test.config["journal_offset"]), 4096); + test.ringloop->reset(); + test.init(); + assert(read_object_retval(test, oid, buf) == -ENOENT); + + restart_cleanly(test); + assert(read_object_retval(test, oid, buf) == -ENOENT); + + free(buf); +} + int main(int narg, char *args[]) { test_preserve_corruption(); @@ -3262,5 +3455,8 @@ int main(int narg, char *args[]) test_small_write_waits_for_its_own_big_write(); test_small_write_waits_for_its_own_big_write_imm(); test_tombstone_unsupported(); + test_restart_does_not_revive_sectors_behind_a_hole(); + test_emptied_journal_does_not_start_with_a_stale_sector(); + test_new_journal_does_not_chain_to_a_previous_one(); return 0; }