/* * Glossary: * * oblock: index of an origin block * cblock: index of a cache block * promotion: movement of a block from origin to cache * demotion: movement of a block from cache to origin * migration: movement of a block between the origin and cache device, * either direction
*/
/* * The batcher collects together pieces of work that need a particular * operation to occur before they can proceed (typically a commit).
*/ struct batcher { /* * The operation that everyone is waiting for.
*/
blk_status_t (*commit_op)(void *context); void *commit_context;
/* * This is how bios should be issued once the commit op is complete * (accounted_request).
*/ void (*issue_op)(struct bio *bio, void *context); void *issue_context;
/* * Queued work gets put on here after commit.
*/ struct workqueue_struct *wq;
/* * We have to grab these before the commit_op to avoid a race * condition.
*/
spin_lock_irq(&b->lock);
list_splice_init(&b->work_items, &work_items);
bio_list_merge_init(&bios, &b->bios);
b->commit_scheduled = false;
spin_unlock_irq(&b->lock);
r = b->commit_op(b->commit_context);
list_for_each_entry_safe(ws, tmp, &work_items, entry) {
k = container_of(ws, struct continuation, ws);
k->input = r;
INIT_LIST_HEAD(&ws->entry); /* to avoid a WARN_ON */
queue_work(b->wq, ws);
}
while ((bio = bio_list_pop(&bios))) { if (r) {
bio->bi_status = r;
bio_endio(bio);
} else
b->issue_op(bio, b->issue_context);
}
}
/* * There are a couple of places where we let a bio run, but want to do some * work before calling its endio function. We do this by temporarily * changing the endio fn.
*/ struct dm_hook_info {
bio_end_io_t *bi_end_io;
};
/* * The block size of the device holding cache data must be * between 32KB and 1GB.
*/ #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT) #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
enum cache_metadata_mode {
CM_WRITE, /* metadata may be changed */
CM_READ_ONLY, /* metadata may not be changed */
CM_FAIL
};
enum cache_io_mode { /* * Data is written to cached blocks only. These blocks are marked * dirty. If you lose the cache device you will lose data. * Potential performance increase for both reads and writes.
*/
CM_IO_WRITEBACK,
/* * Data is written to both cache and origin. Blocks are never * dirty. Potential performance benfit for reads only.
*/
CM_IO_WRITETHROUGH,
/* * A degraded mode useful for various cache coherency situations * (eg, rolling back snapshots). Reads and writes always go to the * origin. If a write goes to a cached oblock, then the cache * block is invalidated.
*/
CM_IO_PASSTHROUGH
};
/* * The number of in flight migrations that are performing * background io. eg, promotion, writeback.
*/
atomic_t nr_io_migrations;
struct bio_list deferred_bios;
struct rw_semaphore quiesce_lock;
/* * origin_blocks entries, discarded if set.
*/
dm_dblock_t discard_nr_blocks; unsignedlong *discard_bitset;
uint32_t discard_block_size; /* a power of 2 times sectors per block */
/* * Rather than reconstructing the table line for the status we just * save it and regurgitate.
*/ unsignedint nr_ctr_args; constchar **ctr_args;
/* * Cache_size entries. Set bits indicate blocks mapped beyond the * target length, which are marked for invalidation.
*/ unsignedlong *invalid_bitset;
};
/* * We have two lock levels. Level 0, which is used to prevent WRITEs, and * level 1 which prevents *both* READs and WRITEs.
*/ #define WRITE_LOCK_LEVEL 0 #define READ_WRITE_LOCK_LEVEL 1
/* * These two are called when setting after migrations to force the policy * and dirty bitset to be in sync.
*/ staticvoid force_set_dirty(struct cache *cache, dm_cblock_t cblock)
{ if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset))
atomic_inc(&cache->nr_dirty);
policy_set_dirty(cache->policy, cblock);
}
staticvoid force_clear_dirty(struct cache *cache, dm_cblock_t cblock)
{ if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) { if (atomic_dec_return(&cache->nr_dirty) == 0)
dm_table_event(cache->ti->table);
}
staticvoid remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
dm_oblock_t oblock)
{ // FIXME: check_if_tick_bio_needed() is called way too much through this interface
check_if_tick_bio_needed(cache, bio);
remap_to_origin(cache, bio); if (bio_data_dir(bio) == WRITE)
clear_discard(cache, oblock_to_dblock(cache, oblock));
}
/* * When running in writethrough mode we need to send writes to clean blocks * to both the cache and origin devices. Clone the bio and send them in parallel.
*/ staticvoid remap_to_origin_and_cache(struct cache *cache, struct bio *bio,
dm_oblock_t oblock, dm_cblock_t cblock)
{ struct bio *origin_bio = bio_alloc_clone(cache->origin_dev->bdev, bio,
GFP_NOIO, &cache->bs);
BUG_ON(!origin_bio);
bio_chain(origin_bio, bio);
if (bio_data_dir(origin_bio) == WRITE)
clear_discard(cache, oblock_to_dblock(cache, oblock));
submit_bio(origin_bio);
if (get_cache_mode(cache) >= CM_READ_ONLY) return;
DMERR_LIMIT("%s: aborting current metadata transaction", dev_name); if (dm_cache_metadata_abort(cache->cmd)) {
DMERR("%s: failed to abort metadata transaction", dev_name);
set_cache_mode(cache, CM_FAIL);
}
if (dm_cache_metadata_set_needs_check(cache->cmd)) {
DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
set_cache_mode(cache, CM_FAIL);
}
}
/* * The overwrite bio is part of the copy operation, as such it does * not set/clear discard or dirty flags.
*/ if (mg->op->op == POLICY_PROMOTE)
remap_to_cache(mg->cache, bio, mg->op->cblock); else
remap_to_origin(mg->cache, bio);
if (mg->overwrite_bio) { if (success)
force_set_dirty(cache, cblock); elseif (mg->k.input)
mg->overwrite_bio->bi_status = mg->k.input; else
mg->overwrite_bio->bi_status = BLK_STS_IOERR;
bio_endio(mg->overwrite_bio);
} else { if (success)
force_clear_dirty(cache, cblock);
dec_io_migrations(cache);
} break;
case POLICY_DEMOTE: /* * We clear dirty here to update the nr_dirty counter.
*/ if (success)
force_clear_dirty(cache, cblock);
policy_complete_background_work(cache->policy, op, success);
dec_io_migrations(cache); break;
case POLICY_WRITEBACK: if (success)
force_clear_dirty(cache, cblock);
policy_complete_background_work(cache->policy, op, success);
dec_io_migrations(cache); break;
}
bio_list_init(&bios); if (mg->cell) { if (dm_cell_unlock_v2(cache->prison, mg->cell, &bios))
free_prison_cell(cache, mg->cell);
}
case POLICY_DEMOTE:
r = dm_cache_remove_mapping(cache->cmd, op->cblock); if (r) {
DMERR_LIMIT("%s: migration failed; couldn't update on disk metadata",
cache_device_name(cache));
metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
mg_complete(mg, false); return;
}
/* * It would be nice if we only had to commit when a REQ_FLUSH * comes through. But there's one scenario that we have to * look out for: * * - vblock x in a cache block * - domotion occurs * - cache block gets reallocated and over written * - crash * * When we recover, because there was no commit the cache will * rollback to having the data for vblock x in the cache block. * But the cache block has since been overwritten, so it'll end * up pointing to data that was never in 'x' during the history * of the device. * * To avoid this issue we require a commit as part of the * demotion operation.
*/
init_continuation(&mg->k, mg_success);
continue_after_commit(&cache->committer, &mg->k);
schedule_commit(&cache->committer); break;
case POLICY_WRITEBACK:
mg_complete(mg, true); break;
}
}
/* * Did the copy succeed?
*/ if (mg->k.input)
mg_complete(mg, false);
else { /* * Now we want the lock to prevent both reads and writes.
*/
r = dm_cell_lock_promote_v2(mg->cache->prison, mg->cell,
READ_WRITE_LOCK_LEVEL); if (r < 0)
mg_complete(mg, false);
if (mg->overwrite_bio) { /* * No exclusive lock was held when we last checked if the bio * was optimisable. So we have to check again in case things * have changed (eg, the block may no longer be discarded).
*/ if (!optimisable_bio(mg->cache, mg->overwrite_bio, mg->op->oblock)) { /* * Fallback to a real full copy after doing some tidying up.
*/ bool rb = bio_detain_shared(mg->cache, mg->op->oblock, mg->overwrite_bio);
BUG_ON(rb); /* An exclusive lock must _not_ be held for this block */
mg->overwrite_bio = NULL;
inc_io_migrations(mg->cache);
mg_full_copy(ws); return;
}
/* * It's safe to do this here, even though it's new data * because all IO has been locked out of the block. * * mg_lock_writes() already took READ_WRITE_LOCK_LEVEL * so _not_ using mg_upgrade_lock() as continutation.
*/
overwrite(mg, mg_update_metadata_after_copy);
/* * Prevent writes to the block, but allow reads to continue. * Unless we're using an overwrite bio, in which case we lock * everything.
*/
build_key(mg->op->oblock, oblock_succ(mg->op->oblock), &key);
r = dm_cell_lock_v2(cache->prison, &key,
mg->overwrite_bio ? READ_WRITE_LOCK_LEVEL : WRITE_LOCK_LEVEL,
prealloc, &mg->cell); if (r < 0) {
free_prison_cell(cache, prealloc);
mg_complete(mg, false); return r;
}
if (mg->cell != prealloc)
free_prison_cell(cache, prealloc);
if (r == 0)
mg_copy(&mg->k.ws); else
quiesce(mg, mg_copy);
staticint invalidate_cblock(struct cache *cache, dm_cblock_t cblock)
{ int r;
r = policy_invalidate_mapping(cache->policy, cblock); if (!r) {
r = dm_cache_remove_mapping(cache->cmd, cblock); if (r) {
DMERR_LIMIT("%s: invalidation failed; couldn't update on disk metadata",
cache_device_name(cache));
metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
}
if (mg->cell != prealloc)
free_prison_cell(cache, prealloc);
if (r)
quiesce(mg, invalidate_remove);
else { /* * We can't call invalidate_remove() directly here because we * might still be in request context.
*/
init_continuation(&mg->k, invalidate_remove);
queue_work(cache->wq, &mg->k.ws);
}
staticint map_bio(struct cache *cache, struct bio *bio, dm_oblock_t block, bool *commit_needed)
{ int r, data_dir; bool rb, background_queued;
dm_cblock_t cblock;
*commit_needed = false;
rb = bio_detain_shared(cache, block, bio); if (!rb) { /* * An exclusive lock is held for this block, so we have to * wait. We set the commit_needed flag so the current * transaction will be committed asap, allowing this lock * to be dropped.
*/
*commit_needed = true; return DM_MAPIO_SUBMITTED;
}
data_dir = bio_data_dir(bio);
if (optimisable_bio(cache, bio, block)) { struct policy_work *op = NULL;
r = policy_lookup_with_work(cache->policy, block, &cblock, data_dir, true, &op); if (unlikely(r && r != -ENOENT)) {
DMERR_LIMIT("%s: policy_lookup_with_work() failed with r = %d",
cache_device_name(cache), r);
bio_io_error(bio); return DM_MAPIO_SUBMITTED;
}
if (r == -ENOENT && op) {
bio_drop_shared_lock(cache, bio);
BUG_ON(op->op != POLICY_PROMOTE);
mg_start(cache, op, bio); return DM_MAPIO_SUBMITTED;
}
} else {
r = policy_lookup(cache->policy, block, &cblock, data_dir, false, &background_queued); if (unlikely(r && r != -ENOENT)) {
DMERR_LIMIT("%s: policy_lookup() failed with r = %d",
cache_device_name(cache), r);
bio_io_error(bio); return DM_MAPIO_SUBMITTED;
}
if (background_queued)
wake_migration_worker(cache);
}
if (r == -ENOENT) { struct per_bio_data *pb = get_per_bio_data(bio);
/* * Miss.
*/
inc_miss_counter(cache, bio); if (pb->req_nr == 0) {
accounted_begin(cache, bio);
remap_to_origin_clear_discard(cache, bio, block);
} else { /* * This is a duplicate writethrough io that is no * longer needed because the block has been demoted.
*/
bio_endio(bio); return DM_MAPIO_SUBMITTED;
}
} else { /* * Hit.
*/
inc_hit_counter(cache, bio);
/* * Passthrough always maps to the origin, invalidating any * cache blocks that are written to.
*/ if (passthrough_mode(cache)) { if (bio_data_dir(bio) == WRITE) {
bio_drop_shared_lock(cache, bio);
atomic_inc(&cache->stats.demotion);
invalidate_start(cache, cblock, block, bio);
} else
remap_to_origin_clear_discard(cache, bio, block);
} else { if (bio_data_dir(bio) == WRITE && writethrough_mode(cache) &&
!is_dirty(cache, cblock)) {
remap_to_origin_and_cache(cache, bio, block, cblock);
accounted_begin(cache, bio);
} else
remap_to_cache_dirty(cache, bio, block, cblock);
}
}
/* * dm core turns FUA requests into a separate payload and FLUSH req.
*/ if (bio->bi_opf & REQ_FUA) { /* * issue_after_commit will call accounted_begin a second time. So * we call accounted_complete() to avoid double accounting.
*/
accounted_complete(cache, bio);
issue_after_commit(&cache->committer, bio);
*commit_needed = true; return DM_MAPIO_SUBMITTED;
}
return DM_MAPIO_REMAPPED;
}
staticbool process_bio(struct cache *cache, struct bio *bio)
{ bool commit_needed;
if (map_bio(cache, bio, get_bio_block(cache, bio), &commit_needed) == DM_MAPIO_REMAPPED)
dm_submit_bio_remap(bio, NULL);
return commit_needed;
}
/* * A non-zero return indicates read_only or fail_io mode.
*/ staticint commit(struct cache *cache, bool clean_shutdown)
{ int r;
if (get_cache_mode(cache) >= CM_READ_ONLY) return -EINVAL;
atomic_inc(&cache->stats.commit_count);
r = dm_cache_commit(cache->cmd, clean_shutdown); if (r)
metadata_operation_failed(cache, "dm_cache_commit", r);
return r;
}
/* * Used by the batcher.
*/ static blk_status_t commit_op(void *context)
{ struct cache *cache = context;
if (dm_cache_changed_this_transaction(cache->cmd)) return errno_to_blk_status(commit(cache, false));
/* * FIXME: do we need to lock the region? Or can we just assume the * user wont be so foolish as to issue discard concurrently with * other IO?
*/
calc_discard_block_range(cache, bio, &b, &e); while (b != e) {
set_discard(cache, b);
b = to_dblock(from_dblock(b) + 1);
}
if (cache->features.discard_passdown) {
remap_to_origin(cache, bio);
dm_submit_bio_remap(bio, NULL);
} else
bio_endio(bio);
/* * We want to commit periodically so that not too much * unwritten metadata builds up.
*/ staticvoid do_waker(struct work_struct *ws)
{ struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
/* * This function gets called on the error paths of the constructor, so we * have to cope with a partially initialised struct.
*/ staticvoid __destroy(struct cache *cache)
{
mempool_exit(&cache->migration_pool);
if (cache->prison)
dm_bio_prison_destroy_v2(cache->prison);
if (cache->wq)
destroy_workqueue(cache->wq);
if (cache->dirty_bitset)
free_bitset(cache->dirty_bitset);
if (cache->discard_bitset)
free_bitset(cache->discard_bitset);
if (cache->invalid_bitset)
free_bitset(cache->invalid_bitset);
if (cache->copier)
dm_kcopyd_client_destroy(cache->copier);
if (cache->cmd)
dm_cache_metadata_close(cache->cmd);
if (cache->metadata_dev)
dm_put_device(cache->ti, cache->metadata_dev);
if (cache->origin_dev)
dm_put_device(cache->ti, cache->origin_dev);
if (cache->cache_dev)
dm_put_device(cache->ti, cache->cache_dev);
if (cache->policy)
dm_cache_policy_destroy(cache->policy);
/* * Construct a cache device mapping. * * cache <metadata dev> <cache dev> <origin dev> <block size> * <#feature args> [<feature arg>]* * <policy> <#policy args> [<policy arg>]* * * metadata dev : fast device holding the persistent metadata * cache dev : fast device holding cached data blocks * origin dev : slow device holding original data blocks * block size : cache unit size in sectors * * #feature args : number of feature arguments passed * feature args : writethrough. (The default is writeback.) * * policy : the replacement policy to use * #policy args : an even number of policy arguments corresponding * to key/value pairs passed to the policy * policy args : key/value pairs passed to the policy * E.g. 'sequential_threshold 1024' * See cache-policies.txt for details. * * Optional feature arguments are: * writethrough : write through caching that prohibits cache block * content from being different from origin block content. * Without this argument, the default behaviour is to write * back cache block contents later for performance reasons, * so they may differ from the corresponding origin blocks.
*/ struct cache_args { struct dm_target *ti;
struct dm_dev *metadata_dev;
struct dm_dev *cache_dev;
sector_t cache_sectors;
struct dm_dev *origin_dev;
uint32_t block_size;
constchar *policy_name; int policy_argc; constchar **policy_argv;
struct cache_features features;
};
staticvoid destroy_cache_args(struct cache_args *ca)
{ if (ca->metadata_dev)
dm_put_device(ca->ti, ca->metadata_dev);
if (ca->cache_dev)
dm_put_device(ca->ti, ca->cache_dev);
if (ca->origin_dev)
dm_put_device(ca->ti, ca->origin_dev);
r = dm_get_device(ca->ti, dm_shift_arg(as),
BLK_OPEN_READ | BLK_OPEN_WRITE, &ca->metadata_dev); if (r) {
*error = "Error opening metadata device"; return r;
}
metadata_dev_size = get_dev_size(ca->metadata_dev); if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
DMWARN("Metadata device %pg is larger than %u sectors: excess space will not be used.",
ca->metadata_dev->bdev, THIN_METADATA_MAX_SECTORS);
/* * We want the discard block size to be at least the size of the cache * block size and have no more than 2^14 discard blocks across the origin.
*/ #define MAX_DISCARD_BLOCKS (1 << 14)
if (nr_blocks > (1 << 20) && cache->cache_size != size)
DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n" "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n" "Please consider increasing the cache block size to reduce the overall cache block count.",
(unsignedlonglong) nr_blocks);
int r; bool commit_needed;
dm_oblock_t block = get_bio_block(cache, bio);
init_per_bio_data(bio); if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) { /* * This can only occur if the io goes to a partial block at * the end of the origin device. We don't cache these. * Just remap to the origin and carry on.
*/
remap_to_origin(cache, bio);
accounted_begin(cache, bio); return DM_MAPIO_REMAPPED;
}
if (discard_or_flush(bio)) {
defer_bio(cache, bio); return DM_MAPIO_SUBMITTED;
}
r = map_bio(cache, bio, block, &commit_needed); if (commit_needed)
schedule_commit(&cache->committer);
staticint write_dirty_bitset(struct cache *cache)
{ int r;
if (get_cache_mode(cache) >= CM_READ_ONLY) return -EINVAL;
r = dm_cache_set_dirty_bits(cache->cmd, from_cblock(cache->cache_size), cache->dirty_bitset); if (r)
metadata_operation_failed(cache, "dm_cache_set_dirty_bits", r);
return r;
}
staticint write_discard_bitset(struct cache *cache)
{ unsignedint i, r;
if (get_cache_mode(cache) >= CM_READ_ONLY) return -EINVAL;
r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
cache->discard_nr_blocks); if (r) {
DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r); return r;
}
for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
r = dm_cache_set_discard(cache->cmd, to_dblock(i),
is_discarded(cache, to_dblock(i))); if (r) {
metadata_operation_failed(cache, "dm_cache_set_discard", r); return r;
}
}
return 0;
}
staticint write_hints(struct cache *cache)
{ int r;
if (get_cache_mode(cache) >= CM_READ_ONLY) return -EINVAL;
r = dm_cache_write_hints(cache->cmd, cache->policy); if (r) {
metadata_operation_failed(cache, "dm_cache_write_hints", r); return r;
}
return 0;
}
/* * returns true on success
*/ staticbool sync_metadata(struct cache *cache)
{ int r1, r2, r3, r4;
r1 = write_dirty_bitset(cache); if (r1)
DMERR("%s: could not write dirty bitset", cache_device_name(cache));
r2 = write_discard_bitset(cache); if (r2)
DMERR("%s: could not write discard bitset", cache_device_name(cache));
save_stats(cache);
r3 = write_hints(cache); if (r3)
DMERR("%s: could not write hints", cache_device_name(cache));
/* * If writing the above metadata failed, we still commit, but don't * set the clean shutdown flag. This will effectively force every * dirty bit to be set on reload.
*/
r4 = commit(cache, !r1 && !r2 && !r3); if (r4)
DMERR("%s: could not write cache metadata", cache_device_name(cache));
/* * The discard block size in the on disk metadata is not * necessarily the same as we're currently using. So we have to * be careful to only set the discarded attribute if we know it * covers a complete block of the new size.
*/ struct discard_load_info { struct cache *cache;
/* * These blocks are sized using the on disk dblock size, rather * than the current one.
*/
dm_block_t block_size;
dm_block_t discard_begin, discard_end;
};
/* * Convert to sectors.
*/
b = li->discard_begin * li->block_size;
e = li->discard_end * li->block_size;
/* * Then convert back to the current dblock size.
*/
b = dm_sector_div_up(b, li->cache->discard_block_size);
sector_div(e, li->cache->discard_block_size);
/* * The origin may have shrunk, so we need to check we're still in * bounds.
*/ if (e > from_dblock(li->cache->discard_nr_blocks))
e = from_dblock(li->cache->discard_nr_blocks);
for (; b < e; b++)
set_discard(li->cache, to_dblock(b));
}
if (discard) { if (from_dblock(dblock) == li->discard_end) /* * We're already in a discard range, just extend it.
*/
li->discard_end = li->discard_end + 1ULL;
else { /* * Emit the old range and start a new one.
*/
set_discard_range(li);
li->discard_begin = from_dblock(dblock);
li->discard_end = li->discard_begin + 1ULL;
}
} else {
set_discard_range(li);
li->discard_begin = li->discard_end = 0;
}
staticbool can_resume(struct cache *cache)
{ /* * Disallow retrying the resume operation for devices that failed the * first resume attempt, as the failure leaves the policy object partially * initialized. Retrying could trigger BUG_ON when loading cache mappings * into the incomplete policy object.
*/ if (cache->sized && !cache->loaded_mappings) { if (get_cache_mode(cache) != CM_WRITE)
DMERR("%s: unable to resume a failed-loaded cache, please check metadata.",
cache_device_name(cache)); else
DMERR("%s: unable to resume cache due to missing proper cache table reload",
cache_device_name(cache)); returnfalse;
}
returntrue;
}
staticbool can_resize(struct cache *cache, dm_cblock_t new_size)
{ if (from_cblock(new_size) > from_cblock(cache->cache_size)) {
DMERR("%s: unable to extend cache due to missing cache table reload",
cache_device_name(cache)); returnfalse;
}
/* * We can't drop a dirty block when shrinking the cache.
*/ if (cache->loaded_mappings) {
new_size = to_cblock(find_next_bit(cache->dirty_bitset,
from_cblock(cache->cache_size),
from_cblock(new_size))); if (new_size != cache->cache_size) {
DMERR("%s: unable to shrink cache; cache block %llu is dirty",
cache_device_name(cache),
(unsignedlonglong) from_cblock(new_size)); returnfalse;
}
}
returntrue;
}
staticint resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
{ int r;
r = dm_cache_resize(cache->cmd, new_size); if (r) {
DMERR("%s: could not resize cache metadata", cache_device_name(cache));
metadata_operation_failed(cache, "dm_cache_resize", r); return r;
}
for_each_set_bit(i, cache->invalid_bitset, nr_blocks) {
r = dm_cache_remove_mapping(cache->cmd, to_cblock(i)); if (r) {
DMERR_LIMIT("%s: invalidation failed; couldn't update on disk metadata",
cache_device_name(cache)); return r;
}
}
return 0;
}
staticint cache_preresume(struct dm_target *ti)
{ int r = 0; struct cache *cache = ti->private;
dm_cblock_t csize = get_cache_dev_size(cache);
if (!can_resume(cache)) return -EINVAL;
/* * Check to see if the cache has resized.
*/ if (!cache->sized || csize != cache->cache_size) { if (!can_resize(cache, csize)) return -EINVAL;
r = resize_cache_dev(cache, csize); if (r) return r;
cache->sized = true;
}
if (!cache->loaded_mappings) { /* * The fast device could have been resized since the last * failed preresume attempt. To be safe we start by a blank * bitset for cache blocks.
*/
clear_bitset(cache->invalid_bitset, from_cblock(cache->cache_size));
r = dm_cache_load_mappings(cache->cmd, cache->policy,
load_filtered_mapping, cache); if (r) {
DMERR("%s: could not load cache mappings", cache_device_name(cache)); if (r != -EFBIG)
metadata_operation_failed(cache, "dm_cache_load_mappings", r); return r;
}
r = truncate_oblocks(cache); if (r) {
metadata_operation_failed(cache, "dm_cache_remove_mapping", r); return r;
}
cache->loaded_mappings = true;
}
if (!cache->loaded_discards) { struct discard_load_info li;
/* * The discard bitset could have been resized, or the * discard block size changed. To be safe we start by * setting every dblock to not discarded.
*/
clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
discard_load_info_init(cache, &li);
r = dm_cache_load_discards(cache->cmd, load_discard, &li); if (r) {
DMERR("%s: could not load origin discards", cache_device_name(cache));
metadata_operation_failed(cache, "dm_cache_load_discards", r); return r;
}
set_discard_range(&li);
/* * Defines a range of cblocks, begin to (end - 1) are in the range. end is * the one-past-the-end value.
*/ struct cblock_range {
dm_cblock_t begin;
dm_cblock_t end;
};
/* * A cache block range can take two forms: * * i) A single cblock, eg. '3456' * ii) A begin and end cblock with a dash between, eg. 123-234
*/ staticint parse_cblock_range(struct cache *cache, constchar *str, struct cblock_range *result)
{ char dummy;
uint64_t b, e; int r;
/* * Try and parse form (ii) first.
*/
r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
staticinline dm_cblock_t cblock_succ(dm_cblock_t b)
{ return to_cblock(from_cblock(b) + 1);
}
staticint request_invalidation(struct cache *cache, struct cblock_range *range)
{ int r = 0;
/* * We don't need to do any locking here because we know we're in * passthrough mode. There's is potential for a race between an * invalidation triggered by an io and an invalidation message. This * is harmless, we must not worry if the policy call fails.
*/ while (range->begin != range->end) {
r = invalidate_cblock(cache, range->begin); if (r) return r;
range->begin = cblock_succ(range->begin);
}
cache->commit_requested = true; return r;
}
staticint process_invalidate_cblocks_message(struct cache *cache, unsignedint count, constchar **cblock_ranges)
{ int r = 0; unsignedint i; struct cblock_range range;
if (!passthrough_mode(cache)) {
DMERR("%s: cache has to be in passthrough mode for invalidation",
cache_device_name(cache)); return -EPERM;
}
for (i = 0; i < count; i++) {
r = parse_cblock_range(cache, cblock_ranges[i], &range); if (r) break;
r = validate_cblock_range(cache, &range); if (r) break;
/* * Pass begin and end origin blocks to the worker and wake it.
*/
r = request_invalidation(cache, &range); if (r) break;
}
return r;
}
/* * Supports * "<key> <value>" * and * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]* * * The key migration_threshold is supported by the cache target core.
*/ staticint cache_message(struct dm_target *ti, unsignedint argc, char **argv, char *result, unsignedint maxlen)
{ struct cache *cache = ti->private;
if (!argc) return -EINVAL;
if (get_cache_mode(cache) >= CM_READ_ONLY) {
DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
cache_device_name(cache)); return -EOPNOTSUPP;
}
if (!cache->features.discard_passdown) { /* No passdown is done so setting own virtual limits */
limits->max_hw_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
cache->origin_sectors);
limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT; return;
}
/* * cache_iterate_devices() is stacking both origin and fast device limits * but discards aren't passed to fast device, so inherit origin's limits.
*/
limits->max_hw_discard_sectors = origin_limits->max_hw_discard_sectors;
limits->discard_granularity = origin_limits->discard_granularity;
limits->discard_alignment = origin_limits->discard_alignment;
}
/* * If the system-determined stacked limits are compatible with the * cache's blocksize (io_opt is a factor) do not override them.
*/ if (io_opt_sectors < cache->sectors_per_block ||
do_div(io_opt_sectors, cache->sectors_per_block)) {
limits->io_min = cache->sectors_per_block << SECTOR_SHIFT;
limits->io_opt = cache->sectors_per_block << SECTOR_SHIFT;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.