When a table is loaded for a device that already has an active era
table, the new table's constructor opens the metadata device and reads
the superblock into its own struct era_metadata, while the old table
keeps using and committing the metadata until it is suspended. The new
table's view is then stale, and its first commit, in preresume, writes
it over what the old table committed after the load:
dmsetup reload era --table "..."
dd if=/dev/zero of=/dev/mapper/era bs=64k seek=500 count=1 oflag=direct
dmsetup suspend era
dmsetup resume era
The write to block 500 is missing from the era metadata afterwards.
The stale view also refers to metadata blocks that the old table may
have freed and reused since. A reload is how an era device is resized,
so this is not an unusual thing to do.
A status request for the inactive table commits the stale view too,
while the old table is still active.
Read the metadata again at the first preresume, when the old table has
been suspended and has done its final commit. This is done in the
worker, which is the only user of the metadata. Until then the target
doesn't write to the metadata device: status doesn't commit, the worker
doesn't digest writesets, and postsuspend does nothing.
The resize that follows is moved into the worker as well. It was done
directly from preresume while the worker could be running, so a status
request at that time could start digesting writesets into the era array
while it was being resized, and commit while preresume was committing.
Fixes: eec40579d848 ("dm: add era target")
Cc: [email protected]
Signed-off-by: Ronnie Sahlberg <[email protected]>
---
An AI coding assistant (Claude) was used to review this patch, to move
the resize into the worker, and to help with the commit message.
Tested on 7.4-rc4 in a VM with lockdep and PROVE_RCU enabled: writes
between reload and resume, status and clear of an inactive table, and
shrinking and growing by reload, with the metadata checked by era_check
and era_invalidate afterwards.
drivers/md/dm-era-target.c | 117 ++++++++++++++++++++++++++-----------
1 file changed, 82 insertions(+), 35 deletions(-)
diff --git a/drivers/md/dm-era-target.c b/drivers/md/dm-era-target.c
index ea499adca4ce..801ca632581b 100644
--- a/drivers/md/dm-era-target.c
+++ b/drivers/md/dm-era-target.c
@@ -1187,6 +1187,13 @@ struct era {
int sectors_per_block_shift;
struct era_metadata *md;
+ /*
+ * Set by the first preresume, once the metadata has been read again
+ * and resized. Until then another table may still be using the
+ * metadata device, and nothing may be written to it.
+ */
+ bool activated;
+
struct workqueue_struct *wq;
struct work_struct worker;
@@ -1205,7 +1212,9 @@ struct rpc {
int (*fn0)(struct era_metadata *md);
int (*fn1)(struct era_metadata *md, void *ref);
+ int (*fn_era)(struct era *era);
void *arg;
+ bool no_commit;
int result;
struct completion complete;
@@ -1338,8 +1347,15 @@ static void process_rpc_calls(struct era *era)
spin_unlock(&era->rpc_lock);
list_for_each_entry_safe(rpc, tmp, &calls, list) {
- rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md,
rpc->arg);
- need_commit = true;
+ if (rpc->fn_era)
+ rpc->result = rpc->fn_era(era);
+ else if (rpc->fn0)
+ rpc->result = rpc->fn0(era->md);
+ else
+ rpc->result = rpc->fn1(era->md, rpc->arg);
+
+ if (!rpc->no_commit)
+ need_commit = true;
}
if (need_commit) {
@@ -1365,8 +1381,10 @@ static void do_work(struct work_struct *ws)
{
struct era *era = container_of(ws, struct era, worker);
- kick_off_digest(era);
- process_old_eras(era);
+ if (era->activated) {
+ kick_off_digest(era);
+ process_old_eras(era);
+ }
process_deferred_bios(era);
process_rpc_calls(era);
}
@@ -1400,22 +1418,7 @@ static int perform_rpc(struct era *era, struct rpc *rpc)
static int in_worker0(struct era *era, int (*fn)(struct era_metadata *md))
{
- struct rpc rpc;
-
- rpc.fn0 = fn;
- rpc.fn1 = NULL;
-
- return perform_rpc(era, &rpc);
-}
-
-static int in_worker1(struct era *era,
- int (*fn)(struct era_metadata *md, void *ref), void *arg)
-{
- struct rpc rpc;
-
- rpc.fn0 = NULL;
- rpc.fn1 = fn;
- rpc.arg = arg;
+ struct rpc rpc = { .fn0 = fn };
return perform_rpc(era, &rpc);
}
@@ -1596,6 +1599,10 @@ static void era_postsuspend(struct dm_target *ti)
int r;
struct era *era = ti->private;
+ /* Never resumed, so our view of the metadata may be stale. */
+ if (!era->activated)
+ return;
+
r = in_worker0(era, metadata_era_archive);
if (r) {
DMERR("%s: couldn't archive current era", __func__);
@@ -1611,30 +1618,64 @@ static void era_postsuspend(struct dm_target *ti)
}
}
-static int era_preresume(struct dm_target *ti)
+/*
+ * The metadata was opened by the constructor, but if this table replaces
+ * another one then that table kept changing the metadata until it was
+ * suspended. So read it again now that we are the only user.
+ *
+ * Runs in the worker, and commits by itself so that nothing is written if
+ * the metadata can't be read.
+ */
+static int era_activate(struct era *era)
{
int r;
- struct era *era = ti->private;
+ struct era_metadata *md;
dm_block_t new_size = calc_nr_blocks(era);
- if (era->nr_blocks != new_size) {
- r = metadata_resize(era->md, &new_size);
- if (r) {
- DMERR("%s: metadata_resize failed", __func__);
- return r;
- }
+ md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block,
+ false);
+ if (IS_ERR(md)) {
+ DMERR("%s: couldn't reopen metadata", __func__);
+ return PTR_ERR(md);
+ }
- r = metadata_commit(era->md);
- if (r) {
- DMERR("%s: metadata_commit failed", __func__);
- return r;
- }
+ metadata_close(era->md);
+ era->md = md;
+ memset(&era->digest, 0, sizeof(era->digest));
+
+ r = metadata_resize(md, &new_size);
+ if (r) {
+ DMERR("%s: metadata_resize failed", __func__);
+ return r;
+ }
- era->nr_blocks = new_size;
+ r = metadata_commit(md);
+ if (r) {
+ DMERR("%s: metadata_commit failed", __func__);
+ return r;
}
+ era->nr_blocks = new_size;
+ WRITE_ONCE(era->activated, true);
+
+ return 0;
+}
+
+static int era_preresume(struct dm_target *ti)
+{
+ int r;
+ struct era *era = ti->private;
+
start_worker(era);
+ if (!era->activated) {
+ struct rpc rpc = { .fn_era = era_activate, .no_commit = true };
+
+ r = perform_rpc(era, &rpc);
+ if (r)
+ return r;
+ }
+
r = in_worker0(era, metadata_era_rollover);
if (r) {
DMERR("%s: metadata_era_rollover failed", __func__);
@@ -1658,10 +1699,16 @@ static void era_status(struct dm_target *ti,
status_type_t type,
ssize_t sz = 0;
struct metadata_stats stats;
char buf[BDEVNAME_SIZE];
+ struct rpc rpc = { .fn1 = metadata_get_stats, .arg = &stats };
switch (type) {
case STATUSTYPE_INFO:
- r = in_worker1(era, metadata_get_stats, &stats);
+ /*
+ * Before the first resume our view of the metadata may be
+ * stale and must not be written.
+ */
+ rpc.no_commit = !READ_ONCE(era->activated);
+ r = perform_rpc(era, &rpc);
if (r)
goto err;
base-commit: ebb7a8dc1e74c705daffe11c2faf38ec2628f838
--
2.56.0