dm era: add lazy mode
Subsystems (2)
PATCH v2 1/1 dm era: add lazy mode
The first write to a block in each era is deferred to the worker, which records the block in the on-disk writeset and commits the metadata before the write is issued. This makes tracking crash safe, but every batch of first writes waits for a metadata commit, including two cache flushes of the metadata device.
Add an optional "lazy" mode which trades that cost for a full resync after a crash, the same trade MD RAID makes without a write-intent bitmap:
era <metadata dev> <origin dev> <block size> [<#feature args> [lazy]
[clean_delay <ms>]]
In lazy mode era_map() marks new blocks in core only and lets the write through. The bits are recorded in a second "pending" bitmap and written to the on-disk writeset with the next metadata commit.
While in-core bits may not be on disk, the superblock carries a new dirty flag (bit 1; bit 0 is "clean shutdown" in thin-provisioning-tools) and is written as metadata version 2, so kernels without lazy mode refuse to open it instead of trusting incomplete writesets. Clean metadata is still written as version 1.
The metadata is marked dirty by the worker before the first write to a new block after it was clean; until that commit has succeeded such bios are deferred as before. It is marked clean on suspend, and after clean_delay milliseconds (default 1000, 0 disables) without writes to new blocks: era_map() is stopped from marking new blocks, an RCU grace period waits for calls already marking, and the pending bits are committed together with the clean flag. So a crash while idle loses nothing, and each idle to busy transition costs one commit.
If the metadata is found dirty on activation, with or without lazy mode, the new era starts with every block marked, on disk and in core, in the same commit that clears the flag. era_invalidate then reports every block for any era since the crash.
Because era_map() no longer goes through the worker, an era rollover in lazy mode switches era_map() to the new writeset, and waits for an RCU grace period, before writing out and archiving the old one. If archiving fails the metadata is kept marked dirty, and the next era, whether started by a message or on activation, has every block marked instead of losing writes.
Without the lazy feature argument the target behaves as before.
---
Changes in v2 (from the Sashiko review of v1):
- Keep the superblock dirty while the invalidation is pending, so the resize commit in era_preresume() can't mark crashed metadata clean before the invalidated era is committed.
- After a failed archive, the next rollover (also one started by a checkpoint or take_metadata_snap message) starts an era with every block marked, not only the next activation.
- Clear the invalidation flags before the rollover's commit, so the metadata is marked clean afterwards.
- Free the old writeset's on-disk bitset if archiving it fails.
- Wake the worker after messages archived writesets, so they are digested in lazy mode, where writes don't go through the worker.
- Only set lazy_activity if it isn't set already.
v1: https://lore.kernel.org/dm-devel/[email protected]/
This depends on a matching thin-provisioning-tools change, so that era_check, era_dump, era_restore and era_repair handle the new dirty flag and version 2 metadata, and era_invalidate reports every block for crashed metadata:
https://github.com/jthornber/thin-provisioning-tools/pull/332
The two only make sense together: without the tools change, era_check rejects dirty metadata, and era_restore/era_repair would silently mark crashed metadata clean.
This patch was developed with an AI coding assistant (Claude), which wrote the implementation and documentation from my design notes; I reviewed and directed the changes.
Tested on 7.4-rc4 in a VM with lockdep and PROVE_RCU enabled:
- table parsing,
- dirty/clean transitions, checked on the on-disk superblock,
- era_invalidate accuracy across checkpoints,
- simulated crashes (metadata device swapped to dm-error) while dirty and while clean,
- a non-lazy table on dirty metadata,
- digestion of writesets archived by a checkpoint in lazy mode (new in v2, fails on v1),
- fio random writes covering every block once while checkpoint and take_metadata_snap run concurrently.
The archive and commit failure paths changed in v2 are not covered by tests; they need a metadata write failure at an exact point. No performance numbers: the test VM's disk is not suitable.
.../admin-guide/device-mapper/era.rst | 35 +- drivers/md/dm-era-target.c | 431 ++++++++++++++++-- 2 files changed, 437 insertions(+), 29 deletions(-)
Documentation/admin-guide/device-mapper/era.rst +34 -1
@@ -18,15 +18,48 @@ coherency after rolling back a vendor snapshot. Constructor =========== -era <metadata dev> <origin dev> <block size> +era <metadata dev> <origin dev> <block size> [<#feature args> [<arg>]*] ================ ====================================================== metadata dev fast device holding the persistent metadata origin dev device holding data blocks that may change block size block size of origin data device, granularity that is tracked by the target + #feature args number of feature arguments that follow ================ ====================================================== +Optional feature arguments are: + + ================ ====================================================== + lazy Track writes in memory only, and write the tracking + information to the metadata device later. See + "Lazy mode" below. + clean_delay <ms> Lazy mode only. Mark the metadata clean after this + many milliseconds without a write to a block that is + not already marked in the current era. 0 means the + metadata is only marked clean on suspend. Default 1000. + ================ ====================================================== + +Lazy mode +========= + +By default the first write to each block in an era waits for the block to +be recorded on the metadata device, which costs a metadata commit. + +In lazy mode writes are never delayed by metadata updates. Instead, the +metadata is marked dirty before the first write after it was clean, and +marked clean again once all tracking information has been written: on +suspend, and after clean_delay milliseconds without writes to new blocks. + +If the target is activated and the metadata is still marked dirty, the +tracking information from before the unclean shutdown may be incomplete. +The new era then starts with every block marked as written, so the next +era_invalidate reports the whole device. + +This check is done whether or not the new table uses lazy mode. While +the metadata is dirty it carries metadata format version 2, which kernels +without lazy mode support refuse to open. Clean metadata uses version 1. + Messages ========
drivers/md/dm-era-target.c +403 -28
684 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] dm era: incomplete metadata snapshot in metadata_take_snap() - [Medium] dm era: premature clearing…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] dm era: incomplete metadata snapshot in metadata_take_snap()
- [Medium] dm era: premature clearing of invalidation flags in metadata_era_rollover()
--
commit c7e0d839221a7af599bacfffe68cbee06cb7fae0
Author: Ronnie Sahlberg <[email protected]>
dm era: add lazy mode
This commit adds an optional "lazy" feature mode to dm-era that records newly written blocks in memory and flushes them on later metadata commits while marking the on-disk superblock dirty until all pending bits are persisted.
Quoted 4 lines
drivers/md/dm-era-target.c +0
-0
[ ... ]
Quoted 34 lines
drivers/md/dm-era-target.c +29
-1@@ -946,7 +1053,47 @@ static int metadata_new_era(struct era_metadata *md)return 0 ; } -static int metadata_era_rollover (struct era_metadata *md) +/* +* era_map() sets bits without the worker in lazy mode, so switch it to the +* new writeset before archiving the old one. +*/ +static int metadata_era_rollover_lazy (struct era_metadata *md) +{ +int r; +struct writeset *old_writeset = md->current_writeset; +struct writeset *new_writeset =next_writeset (md); + + r =writeset_init (&md->bitset_info, new_writeset, md->nr_blocks, + md->needs_invalidate); +if (r) { +DMERR ("%s: writeset_init failed" , __func__); +return r; + } + +swap_writeset (md, new_writeset); + +if (old_writeset->md.root !=INVALID_WRITESET_ROOT ) { + r =writeset_sync_pending (md, old_writeset); +if (!r) + r =__metadata_era_archive (md, old_writeset, + md->current_era); +if (r) { +DMERR ("%s: couldn't archive era %u" , __func__, + md->current_era); + md->tracking_lost = true; +dm_disk_bitset_init (md->tm, &md->bitset_info);
drivers/md/dm-era-target.c static int metadata_new_era(struct era_metadata *md) · line 1094
Quoted 10 lines
drivers/md/dm-era-target.c +10
-0+dm_bitset_del (&md->bitset_info, old_writeset->md.root); + old_writeset->md.root =INVALID_WRITESET_ROOT ; +return r; + } + } + + md->current_era++; + +return 0 ; +}
[Severity: Medium]
Can metadata_take_snap() capture an incomplete metadata snapshot after an archive failure here?
When archiving old_writeset fails, new_writeset (initialized with all bits clear) has already been swapped into md->current_writeset and old_writeset is deleted. If the next rollover is triggered by take_metadata_snap, metadata_era_rollover() sets md->needs_invalidate = true and calls metadata_era_rollover_lazy(), which archives that incomplete writeset into md->writeset_tree_root and puts the all-ones invalidation writeset into the new md->current_writeset.
Because a metadata snapshot only includes writeset_tree_root and era_array_root (and not current_writeset), the snapshot misses both the writes from the failed archive and the all-ones invalidation writeset, while metadata_era_rollover() clears needs_invalidate and tracking_lost before the snapshot commit.
[ ... ]
Quoted 15 lines
drivers/md/dm-era-target.c +11
-0@@ -969,6 +1116,28 @@ static int metadata_era_rollover(struct era_metadata *md)return 0 ; } +static int metadata_era_rollover (struct era_metadata *md) +{ +int r; + +/* Writes may have been lost, so mark every block in the new era. */ +if (md->tracking_lost) + md->needs_invalidate = true; + +if (md->lazy) + r =metadata_era_rollover_lazy (md); +else
drivers/md/dm-era-target.c static int metadata_era_rollover(struct era_metadata *md) · line 1139
Quoted 10 lines
drivers/md/dm-era-target.c +10
-0+ r =__metadata_era_rollover (md); + +/* Cleared before the commit, which marks the metadata clean. */ +if (!r && md->needs_invalidate) { + md->needs_invalidate = false; + md->tracking_lost = false; + } + +return r; +}
[Severity: Medium]
If the caller's metadata_commit() fails after metadata_era_rollover() clears md->needs_invalidate and md->tracking_lost, is the invalidation state lost?
process_rpc_calls() and metadata_take_snap() call metadata_commit() after metadata_era_rollover() returns, without restoring needs_invalidate or tracking_lost on commit failure. In addition, when a non-lazy target opens dirty metadata (setting needs_invalidate) and rolls over on preresume, a later metadata_era_archive() failure in era_postsuspend() only sets tracking_lost if era->md->lazy is true, so the following metadata_commit() writes a clean version 1 superblock even though the invalidated era was not archived.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1