Skip to content

Commit 21c503d

Browse files
mingnusgregkh
authored andcommitted
dm cache: fix dirty mapping checking in passthrough mode switching
[ Upstream commit 3225867 ] As mentioned in commit 9b1cc9f ("dm cache: share cache-metadata object across inactive and active DM tables"), dm-cache assumed table reload occurs after suspension, while LVM's table preload breaks this assumption. The dirty mapping check for passthrough mode was designed around this assumption and is performed during table creation, causing the check to fail with preload while metadata updates are ongoing. This risks loading dirty mappings into passthrough mode, resulting in data loss. Reproduce steps: 1. Create a writeback cache with zero migration_threshold to produce dirty mappings dmsetup create cmeta --table "0 8192 linear /dev/sdc 0" dmsetup create cdata --table "0 131072 linear /dev/sdc 8192" dmsetup create corig --table "0 262144 linear /dev/sdc 262144" dd if=/dev/zero of=/dev/mapper/cmeta bs=4k count=1 oflag=direct dmsetup create cache --table "0 262144 cache /dev/mapper/cmeta \ /dev/mapper/cdata /dev/mapper/corig 128 2 metadata2 writeback smq \ 2 migration_threshold 0" 2. Preload a table in passthrough mode dmsetup reload cache --table "0 262144 cache /dev/mapper/cmeta \ /dev/mapper/cdata /dev/mapper/corig 128 2 metadata2 passthrough smq 0" 3. Write to the first cache block to make it dirty fio --filename=/dev/mapper/cache --name=populate --rw=write --bs=4k \ --direct=1 --size=64k 4. Resume the inactive table. Now it's possible to load the dirty block into passthrough mode. dmsetup resume cache Fix by moving the checks to the preresume phase to support table preloading. Also remove the unused function dm_cache_metadata_all_clean. Fixes: 2ee57d5 ("dm cache: add passthrough mode") Signed-off-by: Ming-Hung Tsai <mtsai@redhat.com> Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent cb8b250 commit 21c503d

3 files changed

Lines changed: 8 additions & 33 deletions

File tree

drivers/md/dm-cache-metadata.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,17 +1714,6 @@ int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *
17141714
return r;
17151715
}
17161716

1717-
int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1718-
{
1719-
int r;
1720-
1721-
READ_LOCK(cmd);
1722-
r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1723-
READ_UNLOCK(cmd);
1724-
1725-
return r;
1726-
}
1727-
17281717
void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
17291718
{
17301719
WRITE_LOCK_VOID(cmd);

drivers/md/dm-cache-metadata.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
135135
*/
136136
int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *p);
137137

138-
/*
139-
* Query method. Are all the blocks in the cache clean?
140-
*/
141-
int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result);
142-
143138
int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result);
144139
int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd);
145140
void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd);

drivers/md/dm-cache-target.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,23 +2506,8 @@ static int cache_create(struct cache_args *ca, struct cache **result)
25062506
goto bad;
25072507
}
25082508

2509-
if (passthrough_mode(cache)) {
2510-
bool all_clean;
2511-
2512-
r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2513-
if (r) {
2514-
*error = "dm_cache_metadata_all_clean() failed";
2515-
goto bad;
2516-
}
2517-
2518-
if (!all_clean) {
2519-
*error = "Cannot enter passthrough mode unless all blocks are clean";
2520-
r = -EINVAL;
2521-
goto bad;
2522-
}
2523-
2509+
if (passthrough_mode(cache))
25242510
policy_allow_migrations(cache->policy, false);
2525-
}
25262511

25272512
spin_lock_init(&cache->lock);
25282513
bio_list_init(&cache->deferred_bios);
@@ -2849,6 +2834,12 @@ static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
28492834
struct cache *cache = context;
28502835

28512836
if (dirty) {
2837+
if (passthrough_mode(cache)) {
2838+
DMERR("%s: cannot enter passthrough mode unless all blocks are clean",
2839+
cache_device_name(cache));
2840+
return -EBUSY;
2841+
}
2842+
28522843
set_bit(from_cblock(cblock), cache->dirty_bitset);
28532844
atomic_inc(&cache->nr_dirty);
28542845
} else
@@ -3082,7 +3073,7 @@ static int cache_preresume(struct dm_target *ti)
30823073
load_filtered_mapping, cache);
30833074
if (r) {
30843075
DMERR("%s: could not load cache mappings", cache_device_name(cache));
3085-
if (r != -EFBIG)
3076+
if (r != -EFBIG && r != -EBUSY)
30863077
metadata_operation_failed(cache, "dm_cache_load_mappings", r);
30873078
return r;
30883079
}

0 commit comments

Comments
 (0)