Commit 028ae9f7 authored by Joe Thornber's avatar Joe Thornber Committed by Mike Snitzer

dm cache: add fail io mode and needs_check flag

If a cache metadata operation fails (e.g. transaction commit) the
cache's metadata device will abort the current transaction, set a new
needs_check flag, and the cache will transition to "read-only" mode.  If
aborting the transaction or setting the needs_check flag fails the cache
will transition to "fail-io" mode.

Once needs_check is set the cache device will not be allowed to
activate.  Activation requires write access to metadata.  Future work is
needed to add proper support for running the cache in read-only mode.

Once in fail-io mode the cache will report a status of "Fail".

Also, add commit() wrapper that will disallow commits if in read_only or
fail mode.
Signed-off-by: default avatarJoe Thornber <ejt@redhat.com>
Signed-off-by: default avatarMike Snitzer <snitzer@redhat.com>
parent 88bf5184
...@@ -221,6 +221,7 @@ Status ...@@ -221,6 +221,7 @@ Status
<#read hits> <#read misses> <#write hits> <#write misses> <#read hits> <#read misses> <#write hits> <#write misses>
<#demotions> <#promotions> <#dirty> <#features> <features>* <#demotions> <#promotions> <#dirty> <#features> <features>*
<#core args> <core args>* <policy name> <#policy args> <policy args>* <#core args> <core args>* <policy name> <#policy args> <policy args>*
<cache metadata mode>
metadata block size : Fixed block size for each metadata block in metadata block size : Fixed block size for each metadata block in
sectors sectors
...@@ -251,8 +252,12 @@ core args : Key/value pairs for tuning the core ...@@ -251,8 +252,12 @@ core args : Key/value pairs for tuning the core
e.g. migration_threshold e.g. migration_threshold
policy name : Name of the policy policy name : Name of the policy
#policy args : Number of policy arguments to follow (must be even) #policy args : Number of policy arguments to follow (must be even)
policy args : Key/value pairs policy args : Key/value pairs e.g. sequential_threshold
e.g. sequential_threshold cache metadata mode : ro if read-only, rw if read-write
In serious cases where even a read-only mode is deemed unsafe
no further I/O will be permitted and the status will just
contain the string 'Fail'. The userspace recovery tools
should then be used.
Messages Messages
-------- --------
......
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
enum superblock_flag_bits { enum superblock_flag_bits {
/* for spotting crashes that would invalidate the dirty bitset */ /* for spotting crashes that would invalidate the dirty bitset */
CLEAN_SHUTDOWN, CLEAN_SHUTDOWN,
/* metadata must be checked using the tools */
NEEDS_CHECK,
}; };
/* /*
...@@ -107,6 +109,7 @@ struct dm_cache_metadata { ...@@ -107,6 +109,7 @@ struct dm_cache_metadata {
struct dm_disk_bitset discard_info; struct dm_disk_bitset discard_info;
struct rw_semaphore root_lock; struct rw_semaphore root_lock;
unsigned long flags;
dm_block_t root; dm_block_t root;
dm_block_t hint_root; dm_block_t hint_root;
dm_block_t discard_root; dm_block_t discard_root;
...@@ -129,6 +132,14 @@ struct dm_cache_metadata { ...@@ -129,6 +132,14 @@ struct dm_cache_metadata {
* buffer before the superblock is locked and updated. * buffer before the superblock is locked and updated.
*/ */
__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
/*
* Set if a transaction has to be aborted but the attempt to roll
* back to the previous (good) transaction failed. The only
* metadata operation permissible in this state is the closing of
* the device.
*/
bool fail_io:1;
}; };
/*------------------------------------------------------------------- /*-------------------------------------------------------------------
...@@ -527,6 +538,7 @@ static unsigned long clear_clean_shutdown(unsigned long flags) ...@@ -527,6 +538,7 @@ static unsigned long clear_clean_shutdown(unsigned long flags)
static void read_superblock_fields(struct dm_cache_metadata *cmd, static void read_superblock_fields(struct dm_cache_metadata *cmd,
struct cache_disk_superblock *disk_super) struct cache_disk_superblock *disk_super)
{ {
cmd->flags = le32_to_cpu(disk_super->flags);
cmd->root = le64_to_cpu(disk_super->mapping_root); cmd->root = le64_to_cpu(disk_super->mapping_root);
cmd->hint_root = le64_to_cpu(disk_super->hint_root); cmd->hint_root = le64_to_cpu(disk_super->hint_root);
cmd->discard_root = le64_to_cpu(disk_super->discard_root); cmd->discard_root = le64_to_cpu(disk_super->discard_root);
...@@ -625,6 +637,7 @@ static int __commit_transaction(struct dm_cache_metadata *cmd, ...@@ -625,6 +637,7 @@ static int __commit_transaction(struct dm_cache_metadata *cmd,
if (mutator) if (mutator)
update_flags(disk_super, mutator); update_flags(disk_super, mutator);
disk_super->flags = cpu_to_le32(cmd->flags);
disk_super->mapping_root = cpu_to_le64(cmd->root); disk_super->mapping_root = cpu_to_le64(cmd->root);
disk_super->hint_root = cpu_to_le64(cmd->hint_root); disk_super->hint_root = cpu_to_le64(cmd->hint_root);
disk_super->discard_root = cpu_to_le64(cmd->discard_root); disk_super->discard_root = cpu_to_le64(cmd->discard_root);
...@@ -693,6 +706,7 @@ static struct dm_cache_metadata *metadata_open(struct block_device *bdev, ...@@ -693,6 +706,7 @@ static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
cmd->cache_blocks = 0; cmd->cache_blocks = 0;
cmd->policy_hint_size = policy_hint_size; cmd->policy_hint_size = policy_hint_size;
cmd->changed = true; cmd->changed = true;
cmd->fail_io = false;
r = __create_persistent_data_objects(cmd, may_format_device); r = __create_persistent_data_objects(cmd, may_format_device);
if (r) { if (r) {
...@@ -796,6 +810,7 @@ void dm_cache_metadata_close(struct dm_cache_metadata *cmd) ...@@ -796,6 +810,7 @@ void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
list_del(&cmd->list); list_del(&cmd->list);
mutex_unlock(&table_lock); mutex_unlock(&table_lock);
if (!cmd->fail_io)
__destroy_persistent_data_objects(cmd); __destroy_persistent_data_objects(cmd);
kfree(cmd); kfree(cmd);
} }
...@@ -848,13 +863,26 @@ static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd, ...@@ -848,13 +863,26 @@ static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
return 0; return 0;
} }
#define WRITE_LOCK(cmd) \
if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) \
return -EINVAL; \
down_write(&cmd->root_lock)
#define WRITE_LOCK_VOID(cmd) \
if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) \
return; \
down_write(&cmd->root_lock)
#define WRITE_UNLOCK(cmd) \
up_write(&cmd->root_lock)
int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size) int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
{ {
int r; int r;
bool clean; bool clean;
__le64 null_mapping = pack_value(0, 0); __le64 null_mapping = pack_value(0, 0);
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
__dm_bless_for_disk(&null_mapping); __dm_bless_for_disk(&null_mapping);
if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) { if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
...@@ -880,7 +908,7 @@ int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size) ...@@ -880,7 +908,7 @@ int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
cmd->changed = true; cmd->changed = true;
out: out:
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -891,7 +919,7 @@ int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd, ...@@ -891,7 +919,7 @@ int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
{ {
int r; int r;
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
r = dm_bitset_resize(&cmd->discard_info, r = dm_bitset_resize(&cmd->discard_info,
cmd->discard_root, cmd->discard_root,
from_dblock(cmd->discard_nr_blocks), from_dblock(cmd->discard_nr_blocks),
...@@ -903,7 +931,7 @@ int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd, ...@@ -903,7 +931,7 @@ int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
} }
cmd->changed = true; cmd->changed = true;
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -946,9 +974,9 @@ int dm_cache_set_discard(struct dm_cache_metadata *cmd, ...@@ -946,9 +974,9 @@ int dm_cache_set_discard(struct dm_cache_metadata *cmd,
{ {
int r; int r;
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
r = __discard(cmd, dblock, discard); r = __discard(cmd, dblock, discard);
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -1020,9 +1048,9 @@ int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock) ...@@ -1020,9 +1048,9 @@ int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
{ {
int r; int r;
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
r = __remove(cmd, cblock); r = __remove(cmd, cblock);
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -1048,9 +1076,9 @@ int dm_cache_insert_mapping(struct dm_cache_metadata *cmd, ...@@ -1048,9 +1076,9 @@ int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
{ {
int r; int r;
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
r = __insert(cmd, cblock, oblock); r = __insert(cmd, cblock, oblock);
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -1234,9 +1262,9 @@ int dm_cache_set_dirty(struct dm_cache_metadata *cmd, ...@@ -1234,9 +1262,9 @@ int dm_cache_set_dirty(struct dm_cache_metadata *cmd,
{ {
int r; int r;
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
r = __dirty(cmd, cblock, dirty); r = __dirty(cmd, cblock, dirty);
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -1252,9 +1280,9 @@ void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd, ...@@ -1252,9 +1280,9 @@ void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd, void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
struct dm_cache_statistics *stats) struct dm_cache_statistics *stats)
{ {
down_write(&cmd->root_lock); WRITE_LOCK_VOID(cmd);
cmd->stats = *stats; cmd->stats = *stats;
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
} }
int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown) int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
...@@ -1263,7 +1291,7 @@ int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown) ...@@ -1263,7 +1291,7 @@ int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
flags_mutator mutator = (clean_shutdown ? set_clean_shutdown : flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
clear_clean_shutdown); clear_clean_shutdown);
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
r = __commit_transaction(cmd, mutator); r = __commit_transaction(cmd, mutator);
if (r) if (r)
goto out; goto out;
...@@ -1271,7 +1299,7 @@ int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown) ...@@ -1271,7 +1299,7 @@ int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
r = __begin_transaction(cmd); r = __begin_transaction(cmd);
out: out:
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -1376,9 +1404,9 @@ int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy * ...@@ -1376,9 +1404,9 @@ int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *
{ {
int r; int r;
down_write(&cmd->root_lock); WRITE_LOCK(cmd);
r = write_hints(cmd, policy); r = write_hints(cmd, policy);
up_write(&cmd->root_lock); WRITE_UNLOCK(cmd);
return r; return r;
} }
...@@ -1387,3 +1415,70 @@ int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result) ...@@ -1387,3 +1415,70 @@ int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
{ {
return blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result); return blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
} }
void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
{
WRITE_LOCK_VOID(cmd);
dm_bm_set_read_only(cmd->bm);
WRITE_UNLOCK(cmd);
}
void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
{
WRITE_LOCK_VOID(cmd);
dm_bm_set_read_write(cmd->bm);
WRITE_UNLOCK(cmd);
}
int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
{
int r;
struct dm_block *sblock;
struct cache_disk_superblock *disk_super;
/*
* We ignore fail_io for this function.
*/
down_write(&cmd->root_lock);
set_bit(NEEDS_CHECK, &cmd->flags);
r = superblock_lock(cmd, &sblock);
if (r) {
DMERR("couldn't read superblock");
goto out;
}
disk_super = dm_block_data(sblock);
disk_super->flags = cpu_to_le32(cmd->flags);
dm_bm_unlock(sblock);
out:
up_write(&cmd->root_lock);
return r;
}
bool dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd)
{
bool needs_check;
down_read(&cmd->root_lock);
needs_check = !!test_bit(NEEDS_CHECK, &cmd->flags);
up_read(&cmd->root_lock);
return needs_check;
}
int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
{
int r;
WRITE_LOCK(cmd);
__destroy_persistent_data_objects(cmd);
r = __create_persistent_data_objects(cmd, false);
if (r)
cmd->fail_io = true;
WRITE_UNLOCK(cmd);
return r;
}
...@@ -102,6 +102,10 @@ struct dm_cache_statistics { ...@@ -102,6 +102,10 @@ struct dm_cache_statistics {
void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd, void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
struct dm_cache_statistics *stats); 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, void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
struct dm_cache_statistics *stats); struct dm_cache_statistics *stats);
...@@ -133,6 +137,12 @@ int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy * ...@@ -133,6 +137,12 @@ int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *
*/ */
int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result); int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result);
bool dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd);
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);
/*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/
#endif /* DM_CACHE_METADATA_H */ #endif /* DM_CACHE_METADATA_H */
...@@ -89,13 +89,15 @@ static inline void policy_tick(struct dm_cache_policy *p) ...@@ -89,13 +89,15 @@ static inline void policy_tick(struct dm_cache_policy *p)
return p->tick(p); return p->tick(p);
} }
static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result, unsigned maxlen) static inline int policy_emit_config_values(struct dm_cache_policy *p, char *result,
unsigned maxlen, ssize_t *sz_ptr)
{ {
ssize_t sz = 0; ssize_t sz = *sz_ptr;
if (p->emit_config_values) if (p->emit_config_values)
return p->emit_config_values(p, result, maxlen); return p->emit_config_values(p, result, maxlen, sz_ptr);
DMEMIT("0"); DMEMIT("0 ");
*sz_ptr = sz;
return 0; return 0;
} }
......
...@@ -1323,22 +1323,24 @@ static int mq_set_config_value(struct dm_cache_policy *p, ...@@ -1323,22 +1323,24 @@ static int mq_set_config_value(struct dm_cache_policy *p,
return 0; return 0;
} }
static int mq_emit_config_values(struct dm_cache_policy *p, char *result, unsigned maxlen) static int mq_emit_config_values(struct dm_cache_policy *p, char *result,
unsigned maxlen, ssize_t *sz_ptr)
{ {
ssize_t sz = 0; ssize_t sz = *sz_ptr;
struct mq_policy *mq = to_mq_policy(p); struct mq_policy *mq = to_mq_policy(p);
DMEMIT("10 random_threshold %u " DMEMIT("10 random_threshold %u "
"sequential_threshold %u " "sequential_threshold %u "
"discard_promote_adjustment %u " "discard_promote_adjustment %u "
"read_promote_adjustment %u " "read_promote_adjustment %u "
"write_promote_adjustment %u", "write_promote_adjustment %u ",
mq->tracker.thresholds[PATTERN_RANDOM], mq->tracker.thresholds[PATTERN_RANDOM],
mq->tracker.thresholds[PATTERN_SEQUENTIAL], mq->tracker.thresholds[PATTERN_SEQUENTIAL],
mq->discard_promote_adjustment, mq->discard_promote_adjustment,
mq->read_promote_adjustment, mq->read_promote_adjustment,
mq->write_promote_adjustment); mq->write_promote_adjustment);
*sz_ptr = sz;
return 0; return 0;
} }
......
...@@ -208,8 +208,8 @@ struct dm_cache_policy { ...@@ -208,8 +208,8 @@ struct dm_cache_policy {
/* /*
* Configuration. * Configuration.
*/ */
int (*emit_config_values)(struct dm_cache_policy *p, int (*emit_config_values)(struct dm_cache_policy *p, char *result,
char *result, unsigned maxlen); unsigned maxlen, ssize_t *sz_ptr);
int (*set_config_value)(struct dm_cache_policy *p, int (*set_config_value)(struct dm_cache_policy *p,
const char *key, const char *value); const char *key, const char *value);
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment