2 * Copyright (C) 2012 Red Hat. All rights reserved.
4 * This file is released under the GPL.
8 #include "dm-bio-prison.h"
9 #include "dm-bio-record.h"
10 #include "dm-cache-metadata.h"
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/init.h>
15 #include <linux/mempool.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
20 #define DM_MSG_PREFIX "cache"
22 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
23 "A percentage of time allocated for copying to and/or from cache");
25 /*----------------------------------------------------------------*/
30 * oblock: index of an origin block
31 * cblock: index of a cache block
32 * promotion: movement of a block from origin to cache
33 * demotion: movement of a block from cache to origin
34 * migration: movement of a block between the origin and cache device,
38 /*----------------------------------------------------------------*/
40 static size_t bitset_size_in_bytes(unsigned nr_entries)
42 return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
45 static unsigned long *alloc_bitset(unsigned nr_entries)
47 size_t s = bitset_size_in_bytes(nr_entries);
51 static void clear_bitset(void *bitset, unsigned nr_entries)
53 size_t s = bitset_size_in_bytes(nr_entries);
57 static void free_bitset(unsigned long *bits)
62 /*----------------------------------------------------------------*/
65 * There are a couple of places where we let a bio run, but want to do some
66 * work before calling its endio function. We do this by temporarily
67 * changing the endio fn.
70 bio_end_io_t *bi_end_io;
74 static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
75 bio_end_io_t *bi_end_io, void *bi_private)
77 h->bi_end_io = bio->bi_end_io;
78 h->bi_private = bio->bi_private;
80 bio->bi_end_io = bi_end_io;
81 bio->bi_private = bi_private;
84 static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
86 bio->bi_end_io = h->bi_end_io;
87 bio->bi_private = h->bi_private;
90 /*----------------------------------------------------------------*/
92 #define PRISON_CELLS 1024
93 #define MIGRATION_POOL_SIZE 128
94 #define COMMIT_PERIOD HZ
95 #define MIGRATION_COUNT_WINDOW 10
98 * The block size of the device holding cache data must be
99 * between 32KB and 1GB.
101 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
102 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
105 * FIXME: the cache is read/write for the time being.
107 enum cache_metadata_mode {
108 CM_WRITE, /* metadata may be changed */
109 CM_READ_ONLY, /* metadata may not be changed */
114 * Data is written to cached blocks only. These blocks are marked
115 * dirty. If you lose the cache device you will lose data.
116 * Potential performance increase for both reads and writes.
121 * Data is written to both cache and origin. Blocks are never
122 * dirty. Potential performance benfit for reads only.
127 * A degraded mode useful for various cache coherency situations
128 * (eg, rolling back snapshots). Reads and writes always go to the
129 * origin. If a write goes to a cached oblock, then the cache
130 * block is invalidated.
135 struct cache_features {
136 enum cache_metadata_mode mode;
137 enum cache_io_mode io_mode;
147 atomic_t copies_avoided;
148 atomic_t cache_cell_clash;
149 atomic_t commit_count;
150 atomic_t discard_count;
154 struct dm_target *ti;
155 struct dm_target_callbacks callbacks;
157 struct dm_cache_metadata *cmd;
160 * Metadata is written to this device.
162 struct dm_dev *metadata_dev;
165 * The slower of the two data devices. Typically a spindle.
167 struct dm_dev *origin_dev;
170 * The faster of the two data devices. Typically an SSD.
172 struct dm_dev *cache_dev;
175 * Size of the origin device in _complete_ blocks and native sectors.
177 dm_oblock_t origin_blocks;
178 sector_t origin_sectors;
181 * Size of the cache device in blocks.
183 dm_cblock_t cache_size;
186 * Fields for converting from sectors to blocks.
188 uint32_t sectors_per_block;
189 int sectors_per_block_shift;
192 struct bio_list deferred_bios;
193 struct bio_list deferred_flush_bios;
194 struct bio_list deferred_writethrough_bios;
195 struct list_head quiesced_migrations;
196 struct list_head completed_migrations;
197 struct list_head need_commit_migrations;
198 sector_t migration_threshold;
199 wait_queue_head_t migration_wait;
200 atomic_t nr_migrations;
202 wait_queue_head_t quiescing_wait;
204 atomic_t quiescing_ack;
207 * cache_size entries, dirty if set
209 dm_cblock_t nr_dirty;
210 unsigned long *dirty_bitset;
213 * origin_blocks entries, discarded if set.
215 dm_dblock_t discard_nr_blocks;
216 unsigned long *discard_bitset;
217 uint32_t discard_block_size; /* a power of 2 times sectors per block */
220 * Rather than reconstructing the table line for the status we just
221 * save it and regurgitate.
223 unsigned nr_ctr_args;
224 const char **ctr_args;
226 struct dm_kcopyd_client *copier;
227 struct workqueue_struct *wq;
228 struct work_struct worker;
230 struct delayed_work waker;
231 unsigned long last_commit_jiffies;
233 struct dm_bio_prison *prison;
234 struct dm_deferred_set *all_io_ds;
236 mempool_t *migration_pool;
237 struct dm_cache_migration *next_migration;
239 struct dm_cache_policy *policy;
240 unsigned policy_nr_args;
242 bool need_tick_bio:1;
244 bool commit_requested:1;
245 bool loaded_mappings:1;
246 bool loaded_discards:1;
249 * Cache features such as write-through.
251 struct cache_features features;
253 struct cache_stats stats;
256 struct per_bio_data {
259 struct dm_deferred_entry *all_io_entry;
262 * writethrough fields. These MUST remain at the end of this
263 * structure and the 'cache' member must be the first as it
264 * is used to determine the offset of the writethrough fields.
268 struct dm_hook_info hook_info;
269 struct dm_bio_details bio_details;
272 struct dm_cache_migration {
273 struct list_head list;
276 unsigned long start_jiffies;
277 dm_oblock_t old_oblock;
278 dm_oblock_t new_oblock;
285 bool requeue_holder:1;
287 struct dm_bio_prison_cell *old_ocell;
288 struct dm_bio_prison_cell *new_ocell;
292 * Processing a bio in the worker thread may require these memory
293 * allocations. We prealloc to avoid deadlocks (the same worker thread
294 * frees them back to the mempool).
297 struct dm_cache_migration *mg;
298 struct dm_bio_prison_cell *cell1;
299 struct dm_bio_prison_cell *cell2;
302 static void wake_worker(struct cache *cache)
304 queue_work(cache->wq, &cache->worker);
307 /*----------------------------------------------------------------*/
309 static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
311 /* FIXME: change to use a local slab. */
312 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
315 static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
317 dm_bio_prison_free_cell(cache->prison, cell);
320 static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
323 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
329 p->cell1 = alloc_prison_cell(cache);
335 p->cell2 = alloc_prison_cell(cache);
343 static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
346 free_prison_cell(cache, p->cell2);
349 free_prison_cell(cache, p->cell1);
352 mempool_free(p->mg, cache->migration_pool);
355 static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
357 struct dm_cache_migration *mg = p->mg;
366 * You must have a cell within the prealloc struct to return. If not this
367 * function will BUG() rather than returning NULL.
369 static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
371 struct dm_bio_prison_cell *r = NULL;
377 } else if (p->cell2) {
387 * You can't have more than two cells in a prealloc struct. BUG() will be
388 * called if you try and overfill.
390 static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
402 /*----------------------------------------------------------------*/
404 static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
408 key->block = from_oblock(oblock);
412 * The caller hands in a preallocated cell, and a free function for it.
413 * The cell will be freed if there's an error, or if it wasn't used because
414 * a cell with that key already exists.
416 typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
418 static int bio_detain(struct cache *cache, dm_oblock_t oblock,
419 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
420 cell_free_fn free_fn, void *free_context,
421 struct dm_bio_prison_cell **cell_result)
424 struct dm_cell_key key;
426 build_key(oblock, &key);
427 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
429 free_fn(free_context, cell_prealloc);
434 static int get_cell(struct cache *cache,
436 struct prealloc *structs,
437 struct dm_bio_prison_cell **cell_result)
440 struct dm_cell_key key;
441 struct dm_bio_prison_cell *cell_prealloc;
443 cell_prealloc = prealloc_get_cell(structs);
445 build_key(oblock, &key);
446 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
448 prealloc_put_cell(structs, cell_prealloc);
453 /*----------------------------------------------------------------*/
455 static bool is_dirty(struct cache *cache, dm_cblock_t b)
457 return test_bit(from_cblock(b), cache->dirty_bitset);
460 static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
462 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
463 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) + 1);
464 policy_set_dirty(cache->policy, oblock);
468 static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
470 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
471 policy_clear_dirty(cache->policy, oblock);
472 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) - 1);
473 if (!from_cblock(cache->nr_dirty))
474 dm_table_event(cache->ti->table);
478 /*----------------------------------------------------------------*/
480 static bool block_size_is_power_of_two(struct cache *cache)
482 return cache->sectors_per_block_shift >= 0;
485 /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
486 #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
489 static dm_block_t block_div(dm_block_t b, uint32_t n)
496 static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
498 uint32_t discard_blocks = cache->discard_block_size;
499 dm_block_t b = from_oblock(oblock);
501 if (!block_size_is_power_of_two(cache))
502 discard_blocks = discard_blocks / cache->sectors_per_block;
504 discard_blocks >>= cache->sectors_per_block_shift;
506 b = block_div(b, discard_blocks);
511 static void set_discard(struct cache *cache, dm_dblock_t b)
515 atomic_inc(&cache->stats.discard_count);
517 spin_lock_irqsave(&cache->lock, flags);
518 set_bit(from_dblock(b), cache->discard_bitset);
519 spin_unlock_irqrestore(&cache->lock, flags);
522 static void clear_discard(struct cache *cache, dm_dblock_t b)
526 spin_lock_irqsave(&cache->lock, flags);
527 clear_bit(from_dblock(b), cache->discard_bitset);
528 spin_unlock_irqrestore(&cache->lock, flags);
531 static bool is_discarded(struct cache *cache, dm_dblock_t b)
536 spin_lock_irqsave(&cache->lock, flags);
537 r = test_bit(from_dblock(b), cache->discard_bitset);
538 spin_unlock_irqrestore(&cache->lock, flags);
543 static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
548 spin_lock_irqsave(&cache->lock, flags);
549 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
550 cache->discard_bitset);
551 spin_unlock_irqrestore(&cache->lock, flags);
556 /*----------------------------------------------------------------*/
558 static void load_stats(struct cache *cache)
560 struct dm_cache_statistics stats;
562 dm_cache_metadata_get_stats(cache->cmd, &stats);
563 atomic_set(&cache->stats.read_hit, stats.read_hits);
564 atomic_set(&cache->stats.read_miss, stats.read_misses);
565 atomic_set(&cache->stats.write_hit, stats.write_hits);
566 atomic_set(&cache->stats.write_miss, stats.write_misses);
569 static void save_stats(struct cache *cache)
571 struct dm_cache_statistics stats;
573 stats.read_hits = atomic_read(&cache->stats.read_hit);
574 stats.read_misses = atomic_read(&cache->stats.read_miss);
575 stats.write_hits = atomic_read(&cache->stats.write_hit);
576 stats.write_misses = atomic_read(&cache->stats.write_miss);
578 dm_cache_metadata_set_stats(cache->cmd, &stats);
581 /*----------------------------------------------------------------
583 *--------------------------------------------------------------*/
586 * If using writeback, leave out struct per_bio_data's writethrough fields.
588 #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
589 #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
591 static bool writethrough_mode(struct cache_features *f)
593 return f->io_mode == CM_IO_WRITETHROUGH;
596 static bool writeback_mode(struct cache_features *f)
598 return f->io_mode == CM_IO_WRITEBACK;
601 static bool passthrough_mode(struct cache_features *f)
603 return f->io_mode == CM_IO_PASSTHROUGH;
606 static size_t get_per_bio_data_size(struct cache *cache)
608 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
611 static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
613 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
618 static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
620 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
623 pb->req_nr = dm_bio_get_target_bio_nr(bio);
624 pb->all_io_entry = NULL;
629 /*----------------------------------------------------------------
631 *--------------------------------------------------------------*/
632 static void remap_to_origin(struct cache *cache, struct bio *bio)
634 bio->bi_bdev = cache->origin_dev->bdev;
637 static void remap_to_cache(struct cache *cache, struct bio *bio,
640 sector_t bi_sector = bio->bi_sector;
642 bio->bi_bdev = cache->cache_dev->bdev;
643 if (!block_size_is_power_of_two(cache))
644 bio->bi_sector = (from_cblock(cblock) * cache->sectors_per_block) +
645 sector_div(bi_sector, cache->sectors_per_block);
647 bio->bi_sector = (from_cblock(cblock) << cache->sectors_per_block_shift) |
648 (bi_sector & (cache->sectors_per_block - 1));
651 static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
654 size_t pb_data_size = get_per_bio_data_size(cache);
655 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
657 spin_lock_irqsave(&cache->lock, flags);
658 if (cache->need_tick_bio &&
659 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
661 cache->need_tick_bio = false;
663 spin_unlock_irqrestore(&cache->lock, flags);
666 static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
669 check_if_tick_bio_needed(cache, bio);
670 remap_to_origin(cache, bio);
671 if (bio_data_dir(bio) == WRITE)
672 clear_discard(cache, oblock_to_dblock(cache, oblock));
675 static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
676 dm_oblock_t oblock, dm_cblock_t cblock)
678 check_if_tick_bio_needed(cache, bio);
679 remap_to_cache(cache, bio, cblock);
680 if (bio_data_dir(bio) == WRITE) {
681 set_dirty(cache, oblock, cblock);
682 clear_discard(cache, oblock_to_dblock(cache, oblock));
686 static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
688 sector_t block_nr = bio->bi_sector;
690 if (!block_size_is_power_of_two(cache))
691 (void) sector_div(block_nr, cache->sectors_per_block);
693 block_nr >>= cache->sectors_per_block_shift;
695 return to_oblock(block_nr);
698 static int bio_triggers_commit(struct cache *cache, struct bio *bio)
700 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
703 static void issue(struct cache *cache, struct bio *bio)
707 if (!bio_triggers_commit(cache, bio)) {
708 generic_make_request(bio);
713 * Batch together any bios that trigger commits and then issue a
714 * single commit for them in do_worker().
716 spin_lock_irqsave(&cache->lock, flags);
717 cache->commit_requested = true;
718 bio_list_add(&cache->deferred_flush_bios, bio);
719 spin_unlock_irqrestore(&cache->lock, flags);
722 static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
726 spin_lock_irqsave(&cache->lock, flags);
727 bio_list_add(&cache->deferred_writethrough_bios, bio);
728 spin_unlock_irqrestore(&cache->lock, flags);
733 static void writethrough_endio(struct bio *bio, int err)
735 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
737 dm_unhook_bio(&pb->hook_info, bio);
744 dm_bio_restore(&pb->bio_details, bio);
745 remap_to_cache(pb->cache, bio, pb->cblock);
748 * We can't issue this bio directly, since we're in interrupt
749 * context. So it gets put on a bio list for processing by the
752 defer_writethrough_bio(pb->cache, bio);
756 * When running in writethrough mode we need to send writes to clean blocks
757 * to both the cache and origin devices. In future we'd like to clone the
758 * bio and send them in parallel, but for now we're doing them in
759 * series as this is easier.
761 static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
762 dm_oblock_t oblock, dm_cblock_t cblock)
764 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
768 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
769 dm_bio_record(&pb->bio_details, bio);
771 remap_to_origin_clear_discard(pb->cache, bio, oblock);
774 /*----------------------------------------------------------------
775 * Migration processing
777 * Migration covers moving data from the origin device to the cache, or
779 *--------------------------------------------------------------*/
780 static void free_migration(struct dm_cache_migration *mg)
782 mempool_free(mg, mg->cache->migration_pool);
785 static void inc_nr_migrations(struct cache *cache)
787 atomic_inc(&cache->nr_migrations);
790 static void dec_nr_migrations(struct cache *cache)
792 atomic_dec(&cache->nr_migrations);
795 * Wake the worker in case we're suspending the target.
797 wake_up(&cache->migration_wait);
800 static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
803 (holder ? dm_cell_release : dm_cell_release_no_holder)
804 (cache->prison, cell, &cache->deferred_bios);
805 free_prison_cell(cache, cell);
808 static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
813 spin_lock_irqsave(&cache->lock, flags);
814 __cell_defer(cache, cell, holder);
815 spin_unlock_irqrestore(&cache->lock, flags);
820 static void cleanup_migration(struct dm_cache_migration *mg)
822 struct cache *cache = mg->cache;
824 dec_nr_migrations(cache);
827 static void migration_failure(struct dm_cache_migration *mg)
829 struct cache *cache = mg->cache;
832 DMWARN_LIMIT("writeback failed; couldn't copy block");
833 set_dirty(cache, mg->old_oblock, mg->cblock);
834 cell_defer(cache, mg->old_ocell, false);
836 } else if (mg->demote) {
837 DMWARN_LIMIT("demotion failed; couldn't copy block");
838 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
840 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
842 cell_defer(cache, mg->new_ocell, true);
844 DMWARN_LIMIT("promotion failed; couldn't copy block");
845 policy_remove_mapping(cache->policy, mg->new_oblock);
846 cell_defer(cache, mg->new_ocell, true);
849 cleanup_migration(mg);
852 static void migration_success_pre_commit(struct dm_cache_migration *mg)
855 struct cache *cache = mg->cache;
858 cell_defer(cache, mg->old_ocell, false);
859 clear_dirty(cache, mg->old_oblock, mg->cblock);
860 cleanup_migration(mg);
863 } else if (mg->demote) {
864 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
865 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
866 policy_force_mapping(cache->policy, mg->new_oblock,
869 cell_defer(cache, mg->new_ocell, true);
870 cleanup_migration(mg);
874 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
875 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
876 policy_remove_mapping(cache->policy, mg->new_oblock);
877 cleanup_migration(mg);
882 spin_lock_irqsave(&cache->lock, flags);
883 list_add_tail(&mg->list, &cache->need_commit_migrations);
884 cache->commit_requested = true;
885 spin_unlock_irqrestore(&cache->lock, flags);
888 static void migration_success_post_commit(struct dm_cache_migration *mg)
891 struct cache *cache = mg->cache;
894 DMWARN("writeback unexpectedly triggered commit");
897 } else if (mg->demote) {
898 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
903 spin_lock_irqsave(&cache->lock, flags);
904 list_add_tail(&mg->list, &cache->quiesced_migrations);
905 spin_unlock_irqrestore(&cache->lock, flags);
908 cleanup_migration(mg);
911 if (mg->requeue_holder)
912 cell_defer(cache, mg->new_ocell, true);
914 bio_endio(mg->new_ocell->holder, 0);
915 cell_defer(cache, mg->new_ocell, false);
917 clear_dirty(cache, mg->new_oblock, mg->cblock);
918 cleanup_migration(mg);
922 static void copy_complete(int read_err, unsigned long write_err, void *context)
925 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
926 struct cache *cache = mg->cache;
928 if (read_err || write_err)
931 spin_lock_irqsave(&cache->lock, flags);
932 list_add_tail(&mg->list, &cache->completed_migrations);
933 spin_unlock_irqrestore(&cache->lock, flags);
938 static void issue_copy_real(struct dm_cache_migration *mg)
941 struct dm_io_region o_region, c_region;
942 struct cache *cache = mg->cache;
944 o_region.bdev = cache->origin_dev->bdev;
945 o_region.count = cache->sectors_per_block;
947 c_region.bdev = cache->cache_dev->bdev;
948 c_region.sector = from_cblock(mg->cblock) * cache->sectors_per_block;
949 c_region.count = cache->sectors_per_block;
951 if (mg->writeback || mg->demote) {
953 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
954 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
957 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
958 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
962 DMERR_LIMIT("issuing migration failed");
963 migration_failure(mg);
967 static void overwrite_endio(struct bio *bio, int err)
969 struct dm_cache_migration *mg = bio->bi_private;
970 struct cache *cache = mg->cache;
971 size_t pb_data_size = get_per_bio_data_size(cache);
972 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
978 spin_lock_irqsave(&cache->lock, flags);
979 list_add_tail(&mg->list, &cache->completed_migrations);
980 dm_unhook_bio(&pb->hook_info, bio);
981 mg->requeue_holder = false;
982 spin_unlock_irqrestore(&cache->lock, flags);
987 static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
989 size_t pb_data_size = get_per_bio_data_size(mg->cache);
990 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
992 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
993 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
994 generic_make_request(bio);
997 static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
999 return (bio_data_dir(bio) == WRITE) &&
1000 (bio->bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
1003 static void avoid_copy(struct dm_cache_migration *mg)
1005 atomic_inc(&mg->cache->stats.copies_avoided);
1006 migration_success_pre_commit(mg);
1009 static void issue_copy(struct dm_cache_migration *mg)
1012 struct cache *cache = mg->cache;
1014 if (mg->writeback || mg->demote)
1015 avoid = !is_dirty(cache, mg->cblock) ||
1016 is_discarded_oblock(cache, mg->old_oblock);
1018 struct bio *bio = mg->new_ocell->holder;
1020 avoid = is_discarded_oblock(cache, mg->new_oblock);
1022 if (!avoid && bio_writes_complete_block(cache, bio)) {
1023 issue_overwrite(mg, bio);
1028 avoid ? avoid_copy(mg) : issue_copy_real(mg);
1031 static void complete_migration(struct dm_cache_migration *mg)
1034 migration_failure(mg);
1036 migration_success_pre_commit(mg);
1039 static void process_migrations(struct cache *cache, struct list_head *head,
1040 void (*fn)(struct dm_cache_migration *))
1042 unsigned long flags;
1043 struct list_head list;
1044 struct dm_cache_migration *mg, *tmp;
1046 INIT_LIST_HEAD(&list);
1047 spin_lock_irqsave(&cache->lock, flags);
1048 list_splice_init(head, &list);
1049 spin_unlock_irqrestore(&cache->lock, flags);
1051 list_for_each_entry_safe(mg, tmp, &list, list)
1055 static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1057 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1060 static void queue_quiesced_migration(struct dm_cache_migration *mg)
1062 unsigned long flags;
1063 struct cache *cache = mg->cache;
1065 spin_lock_irqsave(&cache->lock, flags);
1066 __queue_quiesced_migration(mg);
1067 spin_unlock_irqrestore(&cache->lock, flags);
1072 static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1074 unsigned long flags;
1075 struct dm_cache_migration *mg, *tmp;
1077 spin_lock_irqsave(&cache->lock, flags);
1078 list_for_each_entry_safe(mg, tmp, work, list)
1079 __queue_quiesced_migration(mg);
1080 spin_unlock_irqrestore(&cache->lock, flags);
1085 static void check_for_quiesced_migrations(struct cache *cache,
1086 struct per_bio_data *pb)
1088 struct list_head work;
1090 if (!pb->all_io_entry)
1093 INIT_LIST_HEAD(&work);
1094 if (pb->all_io_entry)
1095 dm_deferred_entry_dec(pb->all_io_entry, &work);
1097 if (!list_empty(&work))
1098 queue_quiesced_migrations(cache, &work);
1101 static void quiesce_migration(struct dm_cache_migration *mg)
1103 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1104 queue_quiesced_migration(mg);
1107 static void promote(struct cache *cache, struct prealloc *structs,
1108 dm_oblock_t oblock, dm_cblock_t cblock,
1109 struct dm_bio_prison_cell *cell)
1111 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1114 mg->writeback = false;
1117 mg->requeue_holder = true;
1119 mg->new_oblock = oblock;
1120 mg->cblock = cblock;
1121 mg->old_ocell = NULL;
1122 mg->new_ocell = cell;
1123 mg->start_jiffies = jiffies;
1125 inc_nr_migrations(cache);
1126 quiesce_migration(mg);
1129 static void writeback(struct cache *cache, struct prealloc *structs,
1130 dm_oblock_t oblock, dm_cblock_t cblock,
1131 struct dm_bio_prison_cell *cell)
1133 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1136 mg->writeback = true;
1138 mg->promote = false;
1139 mg->requeue_holder = true;
1141 mg->old_oblock = oblock;
1142 mg->cblock = cblock;
1143 mg->old_ocell = cell;
1144 mg->new_ocell = NULL;
1145 mg->start_jiffies = jiffies;
1147 inc_nr_migrations(cache);
1148 quiesce_migration(mg);
1151 static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1152 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1154 struct dm_bio_prison_cell *old_ocell,
1155 struct dm_bio_prison_cell *new_ocell)
1157 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1160 mg->writeback = false;
1163 mg->requeue_holder = true;
1165 mg->old_oblock = old_oblock;
1166 mg->new_oblock = new_oblock;
1167 mg->cblock = cblock;
1168 mg->old_ocell = old_ocell;
1169 mg->new_ocell = new_ocell;
1170 mg->start_jiffies = jiffies;
1172 inc_nr_migrations(cache);
1173 quiesce_migration(mg);
1177 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1178 * block are thrown away.
1180 static void invalidate(struct cache *cache, struct prealloc *structs,
1181 dm_oblock_t oblock, dm_cblock_t cblock,
1182 struct dm_bio_prison_cell *cell)
1184 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1187 mg->writeback = false;
1189 mg->promote = false;
1190 mg->requeue_holder = true;
1192 mg->old_oblock = oblock;
1193 mg->cblock = cblock;
1194 mg->old_ocell = cell;
1195 mg->new_ocell = NULL;
1196 mg->start_jiffies = jiffies;
1198 inc_nr_migrations(cache);
1199 quiesce_migration(mg);
1202 /*----------------------------------------------------------------
1204 *--------------------------------------------------------------*/
1205 static void defer_bio(struct cache *cache, struct bio *bio)
1207 unsigned long flags;
1209 spin_lock_irqsave(&cache->lock, flags);
1210 bio_list_add(&cache->deferred_bios, bio);
1211 spin_unlock_irqrestore(&cache->lock, flags);
1216 static void process_flush_bio(struct cache *cache, struct bio *bio)
1218 size_t pb_data_size = get_per_bio_data_size(cache);
1219 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1221 BUG_ON(bio->bi_size);
1223 remap_to_origin(cache, bio);
1225 remap_to_cache(cache, bio, 0);
1231 * People generally discard large parts of a device, eg, the whole device
1232 * when formatting. Splitting these large discards up into cache block
1233 * sized ios and then quiescing (always neccessary for discard) takes too
1236 * We keep it simple, and allow any size of discard to come in, and just
1237 * mark off blocks on the discard bitset. No passdown occurs!
1239 * To implement passdown we need to change the bio_prison such that a cell
1240 * can have a key that spans many blocks.
1242 static void process_discard_bio(struct cache *cache, struct bio *bio)
1244 dm_block_t start_block = dm_sector_div_up(bio->bi_sector,
1245 cache->discard_block_size);
1246 dm_block_t end_block = bio->bi_sector + bio_sectors(bio);
1249 end_block = block_div(end_block, cache->discard_block_size);
1251 for (b = start_block; b < end_block; b++)
1252 set_discard(cache, to_dblock(b));
1257 static bool spare_migration_bandwidth(struct cache *cache)
1259 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1260 cache->sectors_per_block;
1261 return current_volume < cache->migration_threshold;
1264 static void inc_hit_counter(struct cache *cache, struct bio *bio)
1266 atomic_inc(bio_data_dir(bio) == READ ?
1267 &cache->stats.read_hit : &cache->stats.write_hit);
1270 static void inc_miss_counter(struct cache *cache, struct bio *bio)
1272 atomic_inc(bio_data_dir(bio) == READ ?
1273 &cache->stats.read_miss : &cache->stats.write_miss);
1276 static void issue_cache_bio(struct cache *cache, struct bio *bio,
1277 struct per_bio_data *pb,
1278 dm_oblock_t oblock, dm_cblock_t cblock)
1280 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1281 remap_to_cache_dirty(cache, bio, oblock, cblock);
1285 static void process_bio(struct cache *cache, struct prealloc *structs,
1289 bool release_cell = true;
1290 dm_oblock_t block = get_bio_block(cache, bio);
1291 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1292 struct policy_result lookup_result;
1293 size_t pb_data_size = get_per_bio_data_size(cache);
1294 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1295 bool discarded_block = is_discarded_oblock(cache, block);
1296 bool passthrough = passthrough_mode(&cache->features);
1297 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
1300 * Check to see if that block is currently migrating.
1302 cell_prealloc = prealloc_get_cell(structs);
1303 r = bio_detain(cache, block, bio, cell_prealloc,
1304 (cell_free_fn) prealloc_put_cell,
1305 structs, &new_ocell);
1309 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1310 bio, &lookup_result);
1312 if (r == -EWOULDBLOCK)
1313 /* migration has been denied */
1314 lookup_result.op = POLICY_MISS;
1316 switch (lookup_result.op) {
1319 inc_miss_counter(cache, bio);
1322 * Passthrough always maps to the origin,
1323 * invalidating any cache blocks that are written
1327 if (bio_data_dir(bio) == WRITE) {
1328 atomic_inc(&cache->stats.demotion);
1329 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1330 release_cell = false;
1333 /* FIXME: factor out issue_origin() */
1334 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1335 remap_to_origin_clear_discard(cache, bio, block);
1339 inc_hit_counter(cache, bio);
1341 if (bio_data_dir(bio) == WRITE &&
1342 writethrough_mode(&cache->features) &&
1343 !is_dirty(cache, lookup_result.cblock)) {
1344 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1345 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
1348 issue_cache_bio(cache, bio, pb, block, lookup_result.cblock);
1354 inc_miss_counter(cache, bio);
1355 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1356 remap_to_origin_clear_discard(cache, bio, block);
1361 atomic_inc(&cache->stats.promotion);
1362 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1363 release_cell = false;
1366 case POLICY_REPLACE:
1367 cell_prealloc = prealloc_get_cell(structs);
1368 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1369 (cell_free_fn) prealloc_put_cell,
1370 structs, &old_ocell);
1373 * We have to be careful to avoid lock inversion of
1374 * the cells. So we back off, and wait for the
1375 * old_ocell to become free.
1377 policy_force_mapping(cache->policy, block,
1378 lookup_result.old_oblock);
1379 atomic_inc(&cache->stats.cache_cell_clash);
1382 atomic_inc(&cache->stats.demotion);
1383 atomic_inc(&cache->stats.promotion);
1385 demote_then_promote(cache, structs, lookup_result.old_oblock,
1386 block, lookup_result.cblock,
1387 old_ocell, new_ocell);
1388 release_cell = false;
1392 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1393 (unsigned) lookup_result.op);
1398 cell_defer(cache, new_ocell, false);
1401 static int need_commit_due_to_time(struct cache *cache)
1403 return jiffies < cache->last_commit_jiffies ||
1404 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1407 static int commit_if_needed(struct cache *cache)
1411 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1412 dm_cache_changed_this_transaction(cache->cmd)) {
1413 atomic_inc(&cache->stats.commit_count);
1414 cache->commit_requested = false;
1415 r = dm_cache_commit(cache->cmd, false);
1416 cache->last_commit_jiffies = jiffies;
1422 static void process_deferred_bios(struct cache *cache)
1424 unsigned long flags;
1425 struct bio_list bios;
1427 struct prealloc structs;
1429 memset(&structs, 0, sizeof(structs));
1430 bio_list_init(&bios);
1432 spin_lock_irqsave(&cache->lock, flags);
1433 bio_list_merge(&bios, &cache->deferred_bios);
1434 bio_list_init(&cache->deferred_bios);
1435 spin_unlock_irqrestore(&cache->lock, flags);
1437 while (!bio_list_empty(&bios)) {
1439 * If we've got no free migration structs, and processing
1440 * this bio might require one, we pause until there are some
1441 * prepared mappings to process.
1443 if (prealloc_data_structs(cache, &structs)) {
1444 spin_lock_irqsave(&cache->lock, flags);
1445 bio_list_merge(&cache->deferred_bios, &bios);
1446 spin_unlock_irqrestore(&cache->lock, flags);
1450 bio = bio_list_pop(&bios);
1452 if (bio->bi_rw & REQ_FLUSH)
1453 process_flush_bio(cache, bio);
1454 else if (bio->bi_rw & REQ_DISCARD)
1455 process_discard_bio(cache, bio);
1457 process_bio(cache, &structs, bio);
1460 prealloc_free_structs(cache, &structs);
1463 static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1465 unsigned long flags;
1466 struct bio_list bios;
1469 bio_list_init(&bios);
1471 spin_lock_irqsave(&cache->lock, flags);
1472 bio_list_merge(&bios, &cache->deferred_flush_bios);
1473 bio_list_init(&cache->deferred_flush_bios);
1474 spin_unlock_irqrestore(&cache->lock, flags);
1476 while ((bio = bio_list_pop(&bios)))
1477 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1480 static void process_deferred_writethrough_bios(struct cache *cache)
1482 unsigned long flags;
1483 struct bio_list bios;
1486 bio_list_init(&bios);
1488 spin_lock_irqsave(&cache->lock, flags);
1489 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1490 bio_list_init(&cache->deferred_writethrough_bios);
1491 spin_unlock_irqrestore(&cache->lock, flags);
1493 while ((bio = bio_list_pop(&bios)))
1494 generic_make_request(bio);
1497 static void writeback_some_dirty_blocks(struct cache *cache)
1502 struct prealloc structs;
1503 struct dm_bio_prison_cell *old_ocell;
1505 memset(&structs, 0, sizeof(structs));
1507 while (spare_migration_bandwidth(cache)) {
1508 if (prealloc_data_structs(cache, &structs))
1511 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1515 r = get_cell(cache, oblock, &structs, &old_ocell);
1517 policy_set_dirty(cache->policy, oblock);
1521 writeback(cache, &structs, oblock, cblock, old_ocell);
1524 prealloc_free_structs(cache, &structs);
1527 /*----------------------------------------------------------------
1529 *--------------------------------------------------------------*/
1530 static bool is_quiescing(struct cache *cache)
1532 return atomic_read(&cache->quiescing);
1535 static void ack_quiescing(struct cache *cache)
1537 if (is_quiescing(cache)) {
1538 atomic_inc(&cache->quiescing_ack);
1539 wake_up(&cache->quiescing_wait);
1543 static void wait_for_quiescing_ack(struct cache *cache)
1545 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1548 static void start_quiescing(struct cache *cache)
1550 atomic_inc(&cache->quiescing);
1551 wait_for_quiescing_ack(cache);
1554 static void stop_quiescing(struct cache *cache)
1556 atomic_set(&cache->quiescing, 0);
1557 atomic_set(&cache->quiescing_ack, 0);
1560 static void wait_for_migrations(struct cache *cache)
1562 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1565 static void stop_worker(struct cache *cache)
1567 cancel_delayed_work(&cache->waker);
1568 flush_workqueue(cache->wq);
1571 static void requeue_deferred_io(struct cache *cache)
1574 struct bio_list bios;
1576 bio_list_init(&bios);
1577 bio_list_merge(&bios, &cache->deferred_bios);
1578 bio_list_init(&cache->deferred_bios);
1580 while ((bio = bio_list_pop(&bios)))
1581 bio_endio(bio, DM_ENDIO_REQUEUE);
1584 static int more_work(struct cache *cache)
1586 if (is_quiescing(cache))
1587 return !list_empty(&cache->quiesced_migrations) ||
1588 !list_empty(&cache->completed_migrations) ||
1589 !list_empty(&cache->need_commit_migrations);
1591 return !bio_list_empty(&cache->deferred_bios) ||
1592 !bio_list_empty(&cache->deferred_flush_bios) ||
1593 !bio_list_empty(&cache->deferred_writethrough_bios) ||
1594 !list_empty(&cache->quiesced_migrations) ||
1595 !list_empty(&cache->completed_migrations) ||
1596 !list_empty(&cache->need_commit_migrations);
1599 static void do_worker(struct work_struct *ws)
1601 struct cache *cache = container_of(ws, struct cache, worker);
1604 if (!is_quiescing(cache)) {
1605 writeback_some_dirty_blocks(cache);
1606 process_deferred_writethrough_bios(cache);
1607 process_deferred_bios(cache);
1610 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1611 process_migrations(cache, &cache->completed_migrations, complete_migration);
1613 if (commit_if_needed(cache)) {
1614 process_deferred_flush_bios(cache, false);
1617 * FIXME: rollback metadata or just go into a
1618 * failure mode and error everything
1621 process_deferred_flush_bios(cache, true);
1622 process_migrations(cache, &cache->need_commit_migrations,
1623 migration_success_post_commit);
1626 ack_quiescing(cache);
1628 } while (more_work(cache));
1632 * We want to commit periodically so that not too much
1633 * unwritten metadata builds up.
1635 static void do_waker(struct work_struct *ws)
1637 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
1638 policy_tick(cache->policy);
1640 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1643 /*----------------------------------------------------------------*/
1645 static int is_congested(struct dm_dev *dev, int bdi_bits)
1647 struct request_queue *q = bdev_get_queue(dev->bdev);
1648 return bdi_congested(&q->backing_dev_info, bdi_bits);
1651 static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1653 struct cache *cache = container_of(cb, struct cache, callbacks);
1655 return is_congested(cache->origin_dev, bdi_bits) ||
1656 is_congested(cache->cache_dev, bdi_bits);
1659 /*----------------------------------------------------------------
1661 *--------------------------------------------------------------*/
1664 * This function gets called on the error paths of the constructor, so we
1665 * have to cope with a partially initialised struct.
1667 static void destroy(struct cache *cache)
1671 if (cache->next_migration)
1672 mempool_free(cache->next_migration, cache->migration_pool);
1674 if (cache->migration_pool)
1675 mempool_destroy(cache->migration_pool);
1677 if (cache->all_io_ds)
1678 dm_deferred_set_destroy(cache->all_io_ds);
1681 dm_bio_prison_destroy(cache->prison);
1684 destroy_workqueue(cache->wq);
1686 if (cache->dirty_bitset)
1687 free_bitset(cache->dirty_bitset);
1689 if (cache->discard_bitset)
1690 free_bitset(cache->discard_bitset);
1693 dm_kcopyd_client_destroy(cache->copier);
1696 dm_cache_metadata_close(cache->cmd);
1698 if (cache->metadata_dev)
1699 dm_put_device(cache->ti, cache->metadata_dev);
1701 if (cache->origin_dev)
1702 dm_put_device(cache->ti, cache->origin_dev);
1704 if (cache->cache_dev)
1705 dm_put_device(cache->ti, cache->cache_dev);
1708 dm_cache_policy_destroy(cache->policy);
1710 for (i = 0; i < cache->nr_ctr_args ; i++)
1711 kfree(cache->ctr_args[i]);
1712 kfree(cache->ctr_args);
1717 static void cache_dtr(struct dm_target *ti)
1719 struct cache *cache = ti->private;
1724 static sector_t get_dev_size(struct dm_dev *dev)
1726 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1729 /*----------------------------------------------------------------*/
1732 * Construct a cache device mapping.
1734 * cache <metadata dev> <cache dev> <origin dev> <block size>
1735 * <#feature args> [<feature arg>]*
1736 * <policy> <#policy args> [<policy arg>]*
1738 * metadata dev : fast device holding the persistent metadata
1739 * cache dev : fast device holding cached data blocks
1740 * origin dev : slow device holding original data blocks
1741 * block size : cache unit size in sectors
1743 * #feature args : number of feature arguments passed
1744 * feature args : writethrough. (The default is writeback.)
1746 * policy : the replacement policy to use
1747 * #policy args : an even number of policy arguments corresponding
1748 * to key/value pairs passed to the policy
1749 * policy args : key/value pairs passed to the policy
1750 * E.g. 'sequential_threshold 1024'
1751 * See cache-policies.txt for details.
1753 * Optional feature arguments are:
1754 * writethrough : write through caching that prohibits cache block
1755 * content from being different from origin block content.
1756 * Without this argument, the default behaviour is to write
1757 * back cache block contents later for performance reasons,
1758 * so they may differ from the corresponding origin blocks.
1761 struct dm_target *ti;
1763 struct dm_dev *metadata_dev;
1765 struct dm_dev *cache_dev;
1766 sector_t cache_sectors;
1768 struct dm_dev *origin_dev;
1769 sector_t origin_sectors;
1771 uint32_t block_size;
1773 const char *policy_name;
1775 const char **policy_argv;
1777 struct cache_features features;
1780 static void destroy_cache_args(struct cache_args *ca)
1782 if (ca->metadata_dev)
1783 dm_put_device(ca->ti, ca->metadata_dev);
1786 dm_put_device(ca->ti, ca->cache_dev);
1789 dm_put_device(ca->ti, ca->origin_dev);
1794 static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1797 *error = "Insufficient args";
1804 static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1808 sector_t metadata_dev_size;
1809 char b[BDEVNAME_SIZE];
1811 if (!at_least_one_arg(as, error))
1814 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1817 *error = "Error opening metadata device";
1821 metadata_dev_size = get_dev_size(ca->metadata_dev);
1822 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1823 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1824 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1829 static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1834 if (!at_least_one_arg(as, error))
1837 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1840 *error = "Error opening cache device";
1843 ca->cache_sectors = get_dev_size(ca->cache_dev);
1848 static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1853 if (!at_least_one_arg(as, error))
1856 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1859 *error = "Error opening origin device";
1863 ca->origin_sectors = get_dev_size(ca->origin_dev);
1864 if (ca->ti->len > ca->origin_sectors) {
1865 *error = "Device size larger than cached device";
1872 static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1875 unsigned long block_size;
1877 if (!at_least_one_arg(as, error))
1880 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
1881 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1882 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1883 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
1884 *error = "Invalid data block size";
1888 if (block_size > ca->cache_sectors) {
1889 *error = "Data block size is larger than the cache device";
1893 ca->block_size = block_size;
1898 static void init_features(struct cache_features *cf)
1900 cf->mode = CM_WRITE;
1901 cf->io_mode = CM_IO_WRITEBACK;
1904 static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
1907 static struct dm_arg _args[] = {
1908 {0, 1, "Invalid number of cache feature arguments"},
1914 struct cache_features *cf = &ca->features;
1918 r = dm_read_arg_group(_args, as, &argc, error);
1923 arg = dm_shift_arg(as);
1925 if (!strcasecmp(arg, "writeback"))
1926 cf->io_mode = CM_IO_WRITEBACK;
1928 else if (!strcasecmp(arg, "writethrough"))
1929 cf->io_mode = CM_IO_WRITETHROUGH;
1931 else if (!strcasecmp(arg, "passthrough"))
1932 cf->io_mode = CM_IO_PASSTHROUGH;
1935 *error = "Unrecognised cache feature requested";
1943 static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
1946 static struct dm_arg _args[] = {
1947 {0, 1024, "Invalid number of policy arguments"},
1952 if (!at_least_one_arg(as, error))
1955 ca->policy_name = dm_shift_arg(as);
1957 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
1961 ca->policy_argv = (const char **)as->argv;
1962 dm_consume_args(as, ca->policy_argc);
1967 static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
1971 struct dm_arg_set as;
1976 r = parse_metadata_dev(ca, &as, error);
1980 r = parse_cache_dev(ca, &as, error);
1984 r = parse_origin_dev(ca, &as, error);
1988 r = parse_block_size(ca, &as, error);
1992 r = parse_features(ca, &as, error);
1996 r = parse_policy(ca, &as, error);
2003 /*----------------------------------------------------------------*/
2005 static struct kmem_cache *migration_cache;
2007 #define NOT_CORE_OPTION 1
2009 static int process_config_option(struct cache *cache, const char *key, const char *value)
2013 if (!strcasecmp(key, "migration_threshold")) {
2014 if (kstrtoul(value, 10, &tmp))
2017 cache->migration_threshold = tmp;
2021 return NOT_CORE_OPTION;
2024 static int set_config_value(struct cache *cache, const char *key, const char *value)
2026 int r = process_config_option(cache, key, value);
2028 if (r == NOT_CORE_OPTION)
2029 r = policy_set_config_value(cache->policy, key, value);
2032 DMWARN("bad config value for %s: %s", key, value);
2037 static int set_config_values(struct cache *cache, int argc, const char **argv)
2042 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2047 r = set_config_value(cache, argv[0], argv[1]);
2058 static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2061 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2063 cache->origin_sectors,
2064 cache->sectors_per_block);
2066 *error = "Error creating cache's policy";
2075 * We want the discard block size to be a power of two, at least the size
2076 * of the cache block size, and have no more than 2^14 discard blocks
2077 * across the origin.
2079 #define MAX_DISCARD_BLOCKS (1 << 14)
2081 static bool too_many_discard_blocks(sector_t discard_block_size,
2082 sector_t origin_size)
2084 (void) sector_div(origin_size, discard_block_size);
2086 return origin_size > MAX_DISCARD_BLOCKS;
2089 static sector_t calculate_discard_block_size(sector_t cache_block_size,
2090 sector_t origin_size)
2092 sector_t discard_block_size;
2094 discard_block_size = roundup_pow_of_two(cache_block_size);
2097 while (too_many_discard_blocks(discard_block_size, origin_size))
2098 discard_block_size *= 2;
2100 return discard_block_size;
2103 #define DEFAULT_MIGRATION_THRESHOLD 2048
2105 static int cache_create(struct cache_args *ca, struct cache **result)
2108 char **error = &ca->ti->error;
2109 struct cache *cache;
2110 struct dm_target *ti = ca->ti;
2111 dm_block_t origin_blocks;
2112 struct dm_cache_metadata *cmd;
2113 bool may_format = ca->features.mode == CM_WRITE;
2115 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2120 ti->private = cache;
2121 ti->num_flush_bios = 2;
2122 ti->flush_supported = true;
2124 ti->num_discard_bios = 1;
2125 ti->discards_supported = true;
2126 ti->discard_zeroes_data_unsupported = true;
2128 cache->features = ca->features;
2129 ti->per_bio_data_size = get_per_bio_data_size(cache);
2131 cache->callbacks.congested_fn = cache_is_congested;
2132 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2134 cache->metadata_dev = ca->metadata_dev;
2135 cache->origin_dev = ca->origin_dev;
2136 cache->cache_dev = ca->cache_dev;
2138 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2140 /* FIXME: factor out this whole section */
2141 origin_blocks = cache->origin_sectors = ca->origin_sectors;
2142 origin_blocks = block_div(origin_blocks, ca->block_size);
2143 cache->origin_blocks = to_oblock(origin_blocks);
2145 cache->sectors_per_block = ca->block_size;
2146 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2151 if (ca->block_size & (ca->block_size - 1)) {
2152 dm_block_t cache_size = ca->cache_sectors;
2154 cache->sectors_per_block_shift = -1;
2155 cache_size = block_div(cache_size, ca->block_size);
2156 cache->cache_size = to_cblock(cache_size);
2158 cache->sectors_per_block_shift = __ffs(ca->block_size);
2159 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2162 r = create_cache_policy(cache, ca, error);
2166 cache->policy_nr_args = ca->policy_argc;
2167 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2169 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2171 *error = "Error setting cache policy's config values";
2175 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2176 ca->block_size, may_format,
2177 dm_cache_policy_get_hint_size(cache->policy));
2179 *error = "Error creating metadata object";
2185 if (passthrough_mode(&cache->features)) {
2188 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2190 *error = "dm_cache_metadata_all_clean() failed";
2195 *error = "Cannot enter passthrough mode unless all blocks are clean";
2201 spin_lock_init(&cache->lock);
2202 bio_list_init(&cache->deferred_bios);
2203 bio_list_init(&cache->deferred_flush_bios);
2204 bio_list_init(&cache->deferred_writethrough_bios);
2205 INIT_LIST_HEAD(&cache->quiesced_migrations);
2206 INIT_LIST_HEAD(&cache->completed_migrations);
2207 INIT_LIST_HEAD(&cache->need_commit_migrations);
2208 atomic_set(&cache->nr_migrations, 0);
2209 init_waitqueue_head(&cache->migration_wait);
2211 init_waitqueue_head(&cache->quiescing_wait);
2212 atomic_set(&cache->quiescing, 0);
2213 atomic_set(&cache->quiescing_ack, 0);
2216 cache->nr_dirty = 0;
2217 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2218 if (!cache->dirty_bitset) {
2219 *error = "could not allocate dirty bitset";
2222 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2224 cache->discard_block_size =
2225 calculate_discard_block_size(cache->sectors_per_block,
2226 cache->origin_sectors);
2227 cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks);
2228 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2229 if (!cache->discard_bitset) {
2230 *error = "could not allocate discard bitset";
2233 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2235 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2236 if (IS_ERR(cache->copier)) {
2237 *error = "could not create kcopyd client";
2238 r = PTR_ERR(cache->copier);
2242 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2244 *error = "could not create workqueue for metadata object";
2247 INIT_WORK(&cache->worker, do_worker);
2248 INIT_DELAYED_WORK(&cache->waker, do_waker);
2249 cache->last_commit_jiffies = jiffies;
2251 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2252 if (!cache->prison) {
2253 *error = "could not create bio prison";
2257 cache->all_io_ds = dm_deferred_set_create();
2258 if (!cache->all_io_ds) {
2259 *error = "could not create all_io deferred set";
2263 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2265 if (!cache->migration_pool) {
2266 *error = "Error creating cache's migration mempool";
2270 cache->next_migration = NULL;
2272 cache->need_tick_bio = true;
2273 cache->sized = false;
2274 cache->commit_requested = false;
2275 cache->loaded_mappings = false;
2276 cache->loaded_discards = false;
2280 atomic_set(&cache->stats.demotion, 0);
2281 atomic_set(&cache->stats.promotion, 0);
2282 atomic_set(&cache->stats.copies_avoided, 0);
2283 atomic_set(&cache->stats.cache_cell_clash, 0);
2284 atomic_set(&cache->stats.commit_count, 0);
2285 atomic_set(&cache->stats.discard_count, 0);
2295 static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2300 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2303 for (i = 0; i < argc; i++) {
2304 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2313 cache->nr_ctr_args = argc;
2314 cache->ctr_args = copy;
2319 static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2322 struct cache_args *ca;
2323 struct cache *cache = NULL;
2325 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2327 ti->error = "Error allocating memory for cache";
2332 r = parse_cache_args(ca, argc, argv, &ti->error);
2336 r = cache_create(ca, &cache);
2340 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2346 ti->private = cache;
2349 destroy_cache_args(ca);
2353 static int cache_map(struct dm_target *ti, struct bio *bio)
2355 struct cache *cache = ti->private;
2358 dm_oblock_t block = get_bio_block(cache, bio);
2359 size_t pb_data_size = get_per_bio_data_size(cache);
2360 bool can_migrate = false;
2361 bool discarded_block;
2362 struct dm_bio_prison_cell *cell;
2363 struct policy_result lookup_result;
2364 struct per_bio_data *pb;
2366 if (from_oblock(block) > from_oblock(cache->origin_blocks)) {
2368 * This can only occur if the io goes to a partial block at
2369 * the end of the origin device. We don't cache these.
2370 * Just remap to the origin and carry on.
2372 remap_to_origin_clear_discard(cache, bio, block);
2373 return DM_MAPIO_REMAPPED;
2376 pb = init_per_bio_data(bio, pb_data_size);
2378 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2379 defer_bio(cache, bio);
2380 return DM_MAPIO_SUBMITTED;
2384 * Check to see if that block is currently migrating.
2386 cell = alloc_prison_cell(cache);
2388 defer_bio(cache, bio);
2389 return DM_MAPIO_SUBMITTED;
2392 r = bio_detain(cache, block, bio, cell,
2393 (cell_free_fn) free_prison_cell,
2397 defer_bio(cache, bio);
2399 return DM_MAPIO_SUBMITTED;
2402 discarded_block = is_discarded_oblock(cache, block);
2404 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2405 bio, &lookup_result);
2406 if (r == -EWOULDBLOCK) {
2407 cell_defer(cache, cell, true);
2408 return DM_MAPIO_SUBMITTED;
2411 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
2413 return DM_MAPIO_SUBMITTED;
2416 r = DM_MAPIO_REMAPPED;
2417 switch (lookup_result.op) {
2419 if (passthrough_mode(&cache->features)) {
2420 if (bio_data_dir(bio) == WRITE) {
2422 * We need to invalidate this block, so
2423 * defer for the worker thread.
2425 cell_defer(cache, cell, true);
2426 r = DM_MAPIO_SUBMITTED;
2429 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2430 inc_miss_counter(cache, bio);
2431 remap_to_origin_clear_discard(cache, bio, block);
2433 cell_defer(cache, cell, false);
2437 inc_hit_counter(cache, bio);
2439 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2440 !is_dirty(cache, lookup_result.cblock))
2441 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2443 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
2445 cell_defer(cache, cell, false);
2450 inc_miss_counter(cache, bio);
2451 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2453 if (pb->req_nr != 0) {
2455 * This is a duplicate writethrough io that is no
2456 * longer needed because the block has been demoted.
2459 cell_defer(cache, cell, false);
2460 return DM_MAPIO_SUBMITTED;
2462 remap_to_origin_clear_discard(cache, bio, block);
2463 cell_defer(cache, cell, false);
2468 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2469 (unsigned) lookup_result.op);
2471 r = DM_MAPIO_SUBMITTED;
2477 static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2479 struct cache *cache = ti->private;
2480 unsigned long flags;
2481 size_t pb_data_size = get_per_bio_data_size(cache);
2482 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
2485 policy_tick(cache->policy);
2487 spin_lock_irqsave(&cache->lock, flags);
2488 cache->need_tick_bio = true;
2489 spin_unlock_irqrestore(&cache->lock, flags);
2492 check_for_quiesced_migrations(cache, pb);
2497 static int write_dirty_bitset(struct cache *cache)
2501 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2502 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2503 is_dirty(cache, to_cblock(i)));
2511 static int write_discard_bitset(struct cache *cache)
2515 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2516 cache->discard_nr_blocks);
2518 DMERR("could not resize on-disk discard bitset");
2522 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2523 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2524 is_discarded(cache, to_dblock(i)));
2532 static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock,
2535 struct cache *cache = context;
2536 return dm_cache_save_hint(cache->cmd, cblock, hint);
2539 static int write_hints(struct cache *cache)
2543 r = dm_cache_begin_hints(cache->cmd, cache->policy);
2545 DMERR("dm_cache_begin_hints failed");
2549 r = policy_walk_mappings(cache->policy, save_hint, cache);
2551 DMERR("policy_walk_mappings failed");
2557 * returns true on success
2559 static bool sync_metadata(struct cache *cache)
2563 r1 = write_dirty_bitset(cache);
2565 DMERR("could not write dirty bitset");
2567 r2 = write_discard_bitset(cache);
2569 DMERR("could not write discard bitset");
2573 r3 = write_hints(cache);
2575 DMERR("could not write hints");
2578 * If writing the above metadata failed, we still commit, but don't
2579 * set the clean shutdown flag. This will effectively force every
2580 * dirty bit to be set on reload.
2582 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2584 DMERR("could not write cache metadata. Data loss may occur.");
2586 return !r1 && !r2 && !r3 && !r4;
2589 static void cache_postsuspend(struct dm_target *ti)
2591 struct cache *cache = ti->private;
2593 start_quiescing(cache);
2594 wait_for_migrations(cache);
2596 requeue_deferred_io(cache);
2597 stop_quiescing(cache);
2599 (void) sync_metadata(cache);
2602 static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2603 bool dirty, uint32_t hint, bool hint_valid)
2606 struct cache *cache = context;
2608 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2613 set_dirty(cache, oblock, cblock);
2615 clear_dirty(cache, oblock, cblock);
2620 static int load_discard(void *context, sector_t discard_block_size,
2621 dm_dblock_t dblock, bool discard)
2623 struct cache *cache = context;
2625 /* FIXME: handle mis-matched block size */
2628 set_discard(cache, dblock);
2630 clear_discard(cache, dblock);
2635 static dm_cblock_t get_cache_dev_size(struct cache *cache)
2637 sector_t size = get_dev_size(cache->cache_dev);
2638 (void) sector_div(size, cache->sectors_per_block);
2639 return to_cblock(size);
2642 static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2644 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2648 * We can't drop a dirty block when shrinking the cache.
2650 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2651 new_size = to_cblock(from_cblock(new_size) + 1);
2652 if (is_dirty(cache, new_size)) {
2653 DMERR("unable to shrink cache; cache block %llu is dirty",
2654 (unsigned long long) from_cblock(new_size));
2662 static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2666 r = dm_cache_resize(cache->cmd, cache->cache_size);
2668 DMERR("could not resize cache metadata");
2672 cache->cache_size = new_size;
2677 static int cache_preresume(struct dm_target *ti)
2680 struct cache *cache = ti->private;
2681 dm_cblock_t csize = get_cache_dev_size(cache);
2684 * Check to see if the cache has resized.
2686 if (!cache->sized) {
2687 r = resize_cache_dev(cache, csize);
2691 cache->sized = true;
2693 } else if (csize != cache->cache_size) {
2694 if (!can_resize(cache, csize))
2697 r = resize_cache_dev(cache, csize);
2702 if (!cache->loaded_mappings) {
2703 r = dm_cache_load_mappings(cache->cmd, cache->policy,
2704 load_mapping, cache);
2706 DMERR("could not load cache mappings");
2710 cache->loaded_mappings = true;
2713 if (!cache->loaded_discards) {
2714 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2716 DMERR("could not load origin discards");
2720 cache->loaded_discards = true;
2726 static void cache_resume(struct dm_target *ti)
2728 struct cache *cache = ti->private;
2730 cache->need_tick_bio = true;
2731 do_waker(&cache->waker.work);
2737 * <#used metadata blocks>/<#total metadata blocks>
2738 * <#read hits> <#read misses> <#write hits> <#write misses>
2739 * <#demotions> <#promotions> <#blocks in cache> <#dirty>
2740 * <#features> <features>*
2741 * <#core args> <core args>
2742 * <#policy args> <policy args>*
2744 static void cache_status(struct dm_target *ti, status_type_t type,
2745 unsigned status_flags, char *result, unsigned maxlen)
2750 dm_block_t nr_free_blocks_metadata = 0;
2751 dm_block_t nr_blocks_metadata = 0;
2752 char buf[BDEVNAME_SIZE];
2753 struct cache *cache = ti->private;
2754 dm_cblock_t residency;
2757 case STATUSTYPE_INFO:
2758 /* Commit to ensure statistics aren't out-of-date */
2759 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2760 r = dm_cache_commit(cache->cmd, false);
2762 DMERR("could not commit metadata for accurate status");
2765 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2766 &nr_free_blocks_metadata);
2768 DMERR("could not get metadata free block count");
2772 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2774 DMERR("could not get metadata device size");
2778 residency = policy_residency(cache->policy);
2780 DMEMIT("%llu/%llu %u %u %u %u %u %u %llu %u ",
2781 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2782 (unsigned long long)nr_blocks_metadata,
2783 (unsigned) atomic_read(&cache->stats.read_hit),
2784 (unsigned) atomic_read(&cache->stats.read_miss),
2785 (unsigned) atomic_read(&cache->stats.write_hit),
2786 (unsigned) atomic_read(&cache->stats.write_miss),
2787 (unsigned) atomic_read(&cache->stats.demotion),
2788 (unsigned) atomic_read(&cache->stats.promotion),
2789 (unsigned long long) from_cblock(residency),
2792 if (writethrough_mode(&cache->features))
2793 DMEMIT("1 writethrough ");
2795 else if (passthrough_mode(&cache->features))
2796 DMEMIT("1 passthrough ");
2798 else if (writeback_mode(&cache->features))
2799 DMEMIT("1 writeback ");
2802 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2806 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
2808 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2810 DMERR("policy_emit_config_values returned %d", r);
2815 case STATUSTYPE_TABLE:
2816 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2818 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2820 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2823 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2824 DMEMIT(" %s", cache->ctr_args[i]);
2825 if (cache->nr_ctr_args)
2826 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2836 * Supports <key> <value>.
2838 * The key migration_threshold is supported by the cache target core.
2840 static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
2842 struct cache *cache = ti->private;
2847 return set_config_value(cache, argv[0], argv[1]);
2850 static int cache_iterate_devices(struct dm_target *ti,
2851 iterate_devices_callout_fn fn, void *data)
2854 struct cache *cache = ti->private;
2856 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
2858 r = fn(ti, cache->origin_dev, 0, ti->len, data);
2864 * We assume I/O is going to the origin (which is the volume
2865 * more likely to have restrictions e.g. by being striped).
2866 * (Looking up the exact location of the data would be expensive
2867 * and could always be out of date by the time the bio is submitted.)
2869 static int cache_bvec_merge(struct dm_target *ti,
2870 struct bvec_merge_data *bvm,
2871 struct bio_vec *biovec, int max_size)
2873 struct cache *cache = ti->private;
2874 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
2876 if (!q->merge_bvec_fn)
2879 bvm->bi_bdev = cache->origin_dev->bdev;
2880 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2883 static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
2886 * FIXME: these limits may be incompatible with the cache device
2888 limits->max_discard_sectors = cache->discard_block_size * 1024;
2889 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
2892 static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
2894 struct cache *cache = ti->private;
2895 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
2898 * If the system-determined stacked limits are compatible with the
2899 * cache's blocksize (io_opt is a factor) do not override them.
2901 if (io_opt_sectors < cache->sectors_per_block ||
2902 do_div(io_opt_sectors, cache->sectors_per_block)) {
2903 blk_limits_io_min(limits, 0);
2904 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
2906 set_discard_limits(cache, limits);
2909 /*----------------------------------------------------------------*/
2911 static struct target_type cache_target = {
2913 .version = {1, 2, 0},
2914 .module = THIS_MODULE,
2918 .end_io = cache_end_io,
2919 .postsuspend = cache_postsuspend,
2920 .preresume = cache_preresume,
2921 .resume = cache_resume,
2922 .status = cache_status,
2923 .message = cache_message,
2924 .iterate_devices = cache_iterate_devices,
2925 .merge = cache_bvec_merge,
2926 .io_hints = cache_io_hints,
2929 static int __init dm_cache_init(void)
2933 r = dm_register_target(&cache_target);
2935 DMERR("cache target registration failed: %d", r);
2939 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
2940 if (!migration_cache) {
2941 dm_unregister_target(&cache_target);
2948 static void __exit dm_cache_exit(void)
2950 dm_unregister_target(&cache_target);
2951 kmem_cache_destroy(migration_cache);
2954 module_init(dm_cache_init);
2955 module_exit(dm_cache_exit);
2957 MODULE_DESCRIPTION(DM_NAME " cache target");
2958 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
2959 MODULE_LICENSE("GPL");