mirror of
https://github.com/torvalds/linux.git
synced 2026-04-18 14:53:58 -04:00
dm-cache assumes all cache blocks are dirty when it recovers from an
unclean shutdown. Given that the passthrough mode doesn't handle dirty
blocks, we should not load a cache in passthrough mode if it was not
cleanly shut down; or we'll risk data loss while updating an actually
dirty block.
Also bump the target version to 2.4.0 to mark completion of passthrough
mode fixes.
Reproduce steps:
1. Create a writeback cache with zero migration_threshold to produce
dirty blocks.
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. Write the first cache block dirty
fio --filename=/dev/mapper/cache --name=populate --rw=write --bs=4k \
--direct=1 --size=64k
3. Ensure the number of dirty blocks is 1. This status query triggers
metadata commit without flushing the dirty bitset, setting up the
unclean shutdown state.
dmsetup status cache | awk '{print $14}'
4. Force reboot, leaving the cache uncleanly shutdown.
echo b > /proc/sysrq-trigger
5. Activate the above cache components, and verify the first data block
remains dirty.
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/mapper/cdata of=/tmp/cb0.bin bs=64k count=1
dd if=/dev/mapper/corig of=/tmp/ob0.bin bs=64k count=1
md5sum /tmp/cb0.bin /tmp/ob0.bin # expected to be different
6. Try bringing up the cache in passthrough mode. It succeeds, while the
first cache block was loaded dirty due to unclean shutdown, violates
the passthrough mode's constraints.
dmsetup create cache --table "0 262144 cache /dev/mapper/cmeta \
/dev/mapper/cdata /dev/mapper/corig 128 2 metadata2 passthrough smq 0"
dmsetup status cache | awk '{print $14}'
7. (Optional) Demonstrate the integrity issue: invalidating the dirty
block in passthrough mode doesn't write back the dirty data, causing
data loss.
fio --filename=/dev/mapper/cache --name=invalidate --rw=write --bs=4k \
--direct=1 --size=4k # overwrite the first 4k to trigger invalidation
dmsetup remove cache
dd if=/dev/mapper/corig of=/tmp/ob0new.bin bs=64k count=1
cb0sum=$(dd if=/tmp/cb0.bin bs=4k count=15 skip=1 | md5sum | \
awk '{print $1}')
ob0newsum=$(dd if=/tmp/ob0new.bin bs=4k count=15 skip=1 | md5sum | \
awk '{print $1}')
echo "$cb0sum, $ob0newsum" # remaining 60k should differ (data loss)
Signed-off-by: Ming-Hung Tsai <mtsai@redhat.com>
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
152 lines
5.2 KiB
C
152 lines
5.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
/*
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
|
*
|
|
* This file is released under the GPL.
|
|
*/
|
|
|
|
#ifndef DM_CACHE_METADATA_H
|
|
#define DM_CACHE_METADATA_H
|
|
|
|
#include "dm-cache-block-types.h"
|
|
#include "dm-cache-policy-internal.h"
|
|
#include "persistent-data/dm-space-map-metadata.h"
|
|
|
|
/*----------------------------------------------------------------*/
|
|
|
|
#define DM_CACHE_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE
|
|
|
|
/* FIXME: remove this restriction */
|
|
/*
|
|
* The metadata device is currently limited in size.
|
|
*/
|
|
#define DM_CACHE_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS
|
|
|
|
/*
|
|
* A metadata device larger than 16GB triggers a warning.
|
|
*/
|
|
#define DM_CACHE_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
|
|
|
|
/*----------------------------------------------------------------*/
|
|
|
|
/*
|
|
* Ext[234]-style compat feature flags.
|
|
*
|
|
* A new feature which old metadata will still be compatible with should
|
|
* define a DM_CACHE_FEATURE_COMPAT_* flag (rarely useful).
|
|
*
|
|
* A new feature that is not compatible with old code should define a
|
|
* DM_CACHE_FEATURE_INCOMPAT_* flag and guard the relevant code with
|
|
* that flag.
|
|
*
|
|
* A new feature that is not compatible with old code accessing the
|
|
* metadata RDWR should define a DM_CACHE_FEATURE_RO_COMPAT_* flag and
|
|
* guard the relevant code with that flag.
|
|
*
|
|
* As these various flags are defined they should be added to the
|
|
* following masks.
|
|
*/
|
|
|
|
#define DM_CACHE_FEATURE_COMPAT_SUPP 0UL
|
|
#define DM_CACHE_FEATURE_COMPAT_RO_SUPP 0UL
|
|
#define DM_CACHE_FEATURE_INCOMPAT_SUPP 0UL
|
|
|
|
struct dm_cache_metadata;
|
|
|
|
/*
|
|
* Reopens or creates a new, empty metadata volume. Returns an ERR_PTR on
|
|
* failure. If reopening then features must match.
|
|
*/
|
|
struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
|
|
sector_t data_block_size,
|
|
bool may_format_device,
|
|
size_t policy_hint_size,
|
|
unsigned int metadata_version);
|
|
|
|
void dm_cache_metadata_close(struct dm_cache_metadata *cmd);
|
|
|
|
/*
|
|
* The metadata needs to know how many cache blocks there are. We don't
|
|
* care about the origin, assuming the core target is giving us valid
|
|
* origin blocks to map to.
|
|
*/
|
|
int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size);
|
|
|
|
int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
|
|
sector_t discard_block_size,
|
|
dm_dblock_t new_nr_entries);
|
|
|
|
typedef int (*load_discard_fn)(void *context, sector_t discard_block_size,
|
|
dm_dblock_t dblock, bool discarded);
|
|
int dm_cache_load_discards(struct dm_cache_metadata *cmd,
|
|
load_discard_fn fn, void *context);
|
|
|
|
int dm_cache_set_discard(struct dm_cache_metadata *cmd, dm_dblock_t dblock, bool discard);
|
|
|
|
int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock);
|
|
int dm_cache_insert_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock, dm_oblock_t oblock);
|
|
int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd);
|
|
|
|
typedef int (*load_mapping_fn)(void *context, dm_oblock_t oblock,
|
|
dm_cblock_t cblock, bool dirty,
|
|
uint32_t hint, bool hint_valid);
|
|
int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
|
|
struct dm_cache_policy *policy,
|
|
load_mapping_fn fn,
|
|
void *context);
|
|
|
|
int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
|
|
unsigned int nr_bits, unsigned long *bits);
|
|
|
|
struct dm_cache_statistics {
|
|
uint32_t read_hits;
|
|
uint32_t read_misses;
|
|
uint32_t write_hits;
|
|
uint32_t write_misses;
|
|
};
|
|
|
|
void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
|
|
struct dm_cache_statistics *stats);
|
|
|
|
/*
|
|
* 'void' because it's no big deal if it fails.
|
|
*/
|
|
void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
|
|
struct dm_cache_statistics *stats);
|
|
|
|
int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown);
|
|
|
|
int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
|
|
dm_block_t *result);
|
|
|
|
int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
|
|
dm_block_t *result);
|
|
|
|
/*
|
|
* The policy is invited to save a 32bit hint value for every cblock (eg,
|
|
* for a hit count). These are stored against the policy name. If
|
|
* policies are changed, then hints will be lost. If the machine crashes,
|
|
* hints will be lost.
|
|
*
|
|
* The hints are indexed by the cblock, but many policies will not
|
|
* necessarily have a fast way of accessing efficiently via cblock. So
|
|
* rather than querying the policy for each cblock, we let it walk its data
|
|
* structures and fill in the hints in whatever order it wishes.
|
|
*/
|
|
int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *p);
|
|
|
|
int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result);
|
|
int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd);
|
|
void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd);
|
|
void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd);
|
|
int dm_cache_metadata_abort(struct dm_cache_metadata *cmd);
|
|
|
|
/*
|
|
* Query method. Was the metadata cleanly shut down when opened?
|
|
*/
|
|
int dm_cache_metadata_clean_when_opened(struct dm_cache_metadata *cmd, bool *result);
|
|
|
|
/*----------------------------------------------------------------*/
|
|
|
|
#endif /* DM_CACHE_METADATA_H */
|