gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / md / dm-cache-target.c
blobe049becaaf2d208dd3d9275cce170ac1674f708b
1 /*
2 * Copyright (C) 2012 Red Hat. All rights reserved.
4 * This file is released under the GPL.
5 */
7 #include "dm.h"
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/jiffies.h>
15 #include <linux/init.h>
16 #include <linux/mempool.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
21 #define DM_MSG_PREFIX "cache"
23 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
24 "A percentage of time allocated for copying to and/or from cache");
26 /*----------------------------------------------------------------*/
29 * Glossary:
31 * oblock: index of an origin block
32 * cblock: index of a cache block
33 * promotion: movement of a block from origin to cache
34 * demotion: movement of a block from cache to origin
35 * migration: movement of a block between the origin and cache device,
36 * either direction
39 /*----------------------------------------------------------------*/
41 static size_t bitset_size_in_bytes(unsigned nr_entries)
43 return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
46 static unsigned long *alloc_bitset(unsigned nr_entries)
48 size_t s = bitset_size_in_bytes(nr_entries);
49 return vzalloc(s);
52 static void clear_bitset(void *bitset, unsigned nr_entries)
54 size_t s = bitset_size_in_bytes(nr_entries);
55 memset(bitset, 0, s);
58 static void free_bitset(unsigned long *bits)
60 vfree(bits);
63 /*----------------------------------------------------------------*/
66 * There are a couple of places where we let a bio run, but want to do some
67 * work before calling its endio function. We do this by temporarily
68 * changing the endio fn.
70 struct dm_hook_info {
71 bio_end_io_t *bi_end_io;
72 void *bi_private;
75 static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
76 bio_end_io_t *bi_end_io, void *bi_private)
78 h->bi_end_io = bio->bi_end_io;
79 h->bi_private = bio->bi_private;
81 bio->bi_end_io = bi_end_io;
82 bio->bi_private = bi_private;
85 static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
87 bio->bi_end_io = h->bi_end_io;
88 bio->bi_private = h->bi_private;
91 * Must bump bi_remaining to allow bio to complete with
92 * restored bi_end_io.
94 atomic_inc(&bio->bi_remaining);
97 /*----------------------------------------------------------------*/
99 #define MIGRATION_POOL_SIZE 128
100 #define COMMIT_PERIOD HZ
101 #define MIGRATION_COUNT_WINDOW 10
104 * The block size of the device holding cache data must be
105 * between 32KB and 1GB.
107 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
108 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
111 * FIXME: the cache is read/write for the time being.
113 enum cache_metadata_mode {
114 CM_WRITE, /* metadata may be changed */
115 CM_READ_ONLY, /* metadata may not be changed */
118 enum cache_io_mode {
120 * Data is written to cached blocks only. These blocks are marked
121 * dirty. If you lose the cache device you will lose data.
122 * Potential performance increase for both reads and writes.
124 CM_IO_WRITEBACK,
127 * Data is written to both cache and origin. Blocks are never
128 * dirty. Potential performance benfit for reads only.
130 CM_IO_WRITETHROUGH,
133 * A degraded mode useful for various cache coherency situations
134 * (eg, rolling back snapshots). Reads and writes always go to the
135 * origin. If a write goes to a cached oblock, then the cache
136 * block is invalidated.
138 CM_IO_PASSTHROUGH
141 struct cache_features {
142 enum cache_metadata_mode mode;
143 enum cache_io_mode io_mode;
146 struct cache_stats {
147 atomic_t read_hit;
148 atomic_t read_miss;
149 atomic_t write_hit;
150 atomic_t write_miss;
151 atomic_t demotion;
152 atomic_t promotion;
153 atomic_t copies_avoided;
154 atomic_t cache_cell_clash;
155 atomic_t commit_count;
156 atomic_t discard_count;
160 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
161 * the one-past-the-end value.
163 struct cblock_range {
164 dm_cblock_t begin;
165 dm_cblock_t end;
168 struct invalidation_request {
169 struct list_head list;
170 struct cblock_range *cblocks;
172 atomic_t complete;
173 int err;
175 wait_queue_head_t result_wait;
178 struct cache {
179 struct dm_target *ti;
180 struct dm_target_callbacks callbacks;
182 struct dm_cache_metadata *cmd;
185 * Metadata is written to this device.
187 struct dm_dev *metadata_dev;
190 * The slower of the two data devices. Typically a spindle.
192 struct dm_dev *origin_dev;
195 * The faster of the two data devices. Typically an SSD.
197 struct dm_dev *cache_dev;
200 * Size of the origin device in _complete_ blocks and native sectors.
202 dm_oblock_t origin_blocks;
203 sector_t origin_sectors;
206 * Size of the cache device in blocks.
208 dm_cblock_t cache_size;
211 * Fields for converting from sectors to blocks.
213 uint32_t sectors_per_block;
214 int sectors_per_block_shift;
216 spinlock_t lock;
217 struct bio_list deferred_bios;
218 struct bio_list deferred_flush_bios;
219 struct bio_list deferred_writethrough_bios;
220 struct list_head quiesced_migrations;
221 struct list_head completed_migrations;
222 struct list_head need_commit_migrations;
223 sector_t migration_threshold;
224 wait_queue_head_t migration_wait;
225 atomic_t nr_allocated_migrations;
228 * The number of in flight migrations that are performing
229 * background io. eg, promotion, writeback.
231 atomic_t nr_io_migrations;
233 wait_queue_head_t quiescing_wait;
234 atomic_t quiescing;
235 atomic_t quiescing_ack;
238 * cache_size entries, dirty if set
240 atomic_t nr_dirty;
241 unsigned long *dirty_bitset;
244 * origin_blocks entries, discarded if set.
246 dm_dblock_t discard_nr_blocks;
247 unsigned long *discard_bitset;
248 uint32_t discard_block_size; /* a power of 2 times sectors per block */
251 * Rather than reconstructing the table line for the status we just
252 * save it and regurgitate.
254 unsigned nr_ctr_args;
255 const char **ctr_args;
257 struct dm_kcopyd_client *copier;
258 struct workqueue_struct *wq;
259 struct work_struct worker;
261 struct delayed_work waker;
262 unsigned long last_commit_jiffies;
264 struct dm_bio_prison *prison;
265 struct dm_deferred_set *all_io_ds;
267 mempool_t *migration_pool;
269 struct dm_cache_policy *policy;
270 unsigned policy_nr_args;
272 bool need_tick_bio:1;
273 bool sized:1;
274 bool invalidate:1;
275 bool commit_requested:1;
276 bool loaded_mappings:1;
277 bool loaded_discards:1;
280 * Cache features such as write-through.
282 struct cache_features features;
284 struct cache_stats stats;
287 * Invalidation fields.
289 spinlock_t invalidation_lock;
290 struct list_head invalidation_requests;
293 struct per_bio_data {
294 bool tick:1;
295 unsigned req_nr:2;
296 struct dm_deferred_entry *all_io_entry;
297 struct dm_hook_info hook_info;
300 * writethrough fields. These MUST remain at the end of this
301 * structure and the 'cache' member must be the first as it
302 * is used to determine the offset of the writethrough fields.
304 struct cache *cache;
305 dm_cblock_t cblock;
306 struct dm_bio_details bio_details;
309 struct dm_cache_migration {
310 struct list_head list;
311 struct cache *cache;
313 unsigned long start_jiffies;
314 dm_oblock_t old_oblock;
315 dm_oblock_t new_oblock;
316 dm_cblock_t cblock;
318 bool err:1;
319 bool discard:1;
320 bool writeback:1;
321 bool demote:1;
322 bool promote:1;
323 bool requeue_holder:1;
324 bool invalidate:1;
326 struct dm_bio_prison_cell *old_ocell;
327 struct dm_bio_prison_cell *new_ocell;
331 * Processing a bio in the worker thread may require these memory
332 * allocations. We prealloc to avoid deadlocks (the same worker thread
333 * frees them back to the mempool).
335 struct prealloc {
336 struct dm_cache_migration *mg;
337 struct dm_bio_prison_cell *cell1;
338 struct dm_bio_prison_cell *cell2;
341 static void wake_worker(struct cache *cache)
343 queue_work(cache->wq, &cache->worker);
346 /*----------------------------------------------------------------*/
348 static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
350 /* FIXME: change to use a local slab. */
351 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
354 static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
356 dm_bio_prison_free_cell(cache->prison, cell);
359 static struct dm_cache_migration *alloc_migration(struct cache *cache)
361 struct dm_cache_migration *mg;
363 mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
364 if (mg) {
365 mg->cache = cache;
366 atomic_inc(&mg->cache->nr_allocated_migrations);
369 return mg;
372 static void free_migration(struct dm_cache_migration *mg)
374 if (atomic_dec_and_test(&mg->cache->nr_allocated_migrations))
375 wake_up(&mg->cache->migration_wait);
377 mempool_free(mg, mg->cache->migration_pool);
380 static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
382 if (!p->mg) {
383 p->mg = alloc_migration(cache);
384 if (!p->mg)
385 return -ENOMEM;
388 if (!p->cell1) {
389 p->cell1 = alloc_prison_cell(cache);
390 if (!p->cell1)
391 return -ENOMEM;
394 if (!p->cell2) {
395 p->cell2 = alloc_prison_cell(cache);
396 if (!p->cell2)
397 return -ENOMEM;
400 return 0;
403 static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
405 if (p->cell2)
406 free_prison_cell(cache, p->cell2);
408 if (p->cell1)
409 free_prison_cell(cache, p->cell1);
411 if (p->mg)
412 free_migration(p->mg);
415 static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
417 struct dm_cache_migration *mg = p->mg;
419 BUG_ON(!mg);
420 p->mg = NULL;
422 return mg;
426 * You must have a cell within the prealloc struct to return. If not this
427 * function will BUG() rather than returning NULL.
429 static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
431 struct dm_bio_prison_cell *r = NULL;
433 if (p->cell1) {
434 r = p->cell1;
435 p->cell1 = NULL;
437 } else if (p->cell2) {
438 r = p->cell2;
439 p->cell2 = NULL;
440 } else
441 BUG();
443 return r;
447 * You can't have more than two cells in a prealloc struct. BUG() will be
448 * called if you try and overfill.
450 static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
452 if (!p->cell2)
453 p->cell2 = cell;
455 else if (!p->cell1)
456 p->cell1 = cell;
458 else
459 BUG();
462 /*----------------------------------------------------------------*/
464 static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key *key)
466 key->virtual = 0;
467 key->dev = 0;
468 key->block_begin = from_oblock(begin);
469 key->block_end = from_oblock(end);
473 * The caller hands in a preallocated cell, and a free function for it.
474 * The cell will be freed if there's an error, or if it wasn't used because
475 * a cell with that key already exists.
477 typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
479 static int bio_detain_range(struct cache *cache, dm_oblock_t oblock_begin, dm_oblock_t oblock_end,
480 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
481 cell_free_fn free_fn, void *free_context,
482 struct dm_bio_prison_cell **cell_result)
484 int r;
485 struct dm_cell_key key;
487 build_key(oblock_begin, oblock_end, &key);
488 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
489 if (r)
490 free_fn(free_context, cell_prealloc);
492 return r;
495 static int bio_detain(struct cache *cache, dm_oblock_t oblock,
496 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
497 cell_free_fn free_fn, void *free_context,
498 struct dm_bio_prison_cell **cell_result)
500 dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL);
501 return bio_detain_range(cache, oblock, end, bio,
502 cell_prealloc, free_fn, free_context, cell_result);
505 static int get_cell(struct cache *cache,
506 dm_oblock_t oblock,
507 struct prealloc *structs,
508 struct dm_bio_prison_cell **cell_result)
510 int r;
511 struct dm_cell_key key;
512 struct dm_bio_prison_cell *cell_prealloc;
514 cell_prealloc = prealloc_get_cell(structs);
516 build_key(oblock, to_oblock(from_oblock(oblock) + 1ULL), &key);
517 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
518 if (r)
519 prealloc_put_cell(structs, cell_prealloc);
521 return r;
524 /*----------------------------------------------------------------*/
526 static bool is_dirty(struct cache *cache, dm_cblock_t b)
528 return test_bit(from_cblock(b), cache->dirty_bitset);
531 static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
533 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
534 atomic_inc(&cache->nr_dirty);
535 policy_set_dirty(cache->policy, oblock);
539 static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
541 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
542 policy_clear_dirty(cache->policy, oblock);
543 if (atomic_dec_return(&cache->nr_dirty) == 0)
544 dm_table_event(cache->ti->table);
548 /*----------------------------------------------------------------*/
550 static bool block_size_is_power_of_two(struct cache *cache)
552 return cache->sectors_per_block_shift >= 0;
555 /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
556 #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
557 __always_inline
558 #endif
559 static dm_block_t block_div(dm_block_t b, uint32_t n)
561 do_div(b, n);
563 return b;
566 static dm_block_t oblocks_per_dblock(struct cache *cache)
568 dm_block_t oblocks = cache->discard_block_size;
570 if (block_size_is_power_of_two(cache))
571 oblocks >>= cache->sectors_per_block_shift;
572 else
573 oblocks = block_div(oblocks, cache->sectors_per_block);
575 return oblocks;
578 static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
580 return to_dblock(block_div(from_oblock(oblock),
581 oblocks_per_dblock(cache)));
584 static dm_oblock_t dblock_to_oblock(struct cache *cache, dm_dblock_t dblock)
586 return to_oblock(from_dblock(dblock) * oblocks_per_dblock(cache));
589 static void set_discard(struct cache *cache, dm_dblock_t b)
591 unsigned long flags;
593 BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks));
594 atomic_inc(&cache->stats.discard_count);
596 spin_lock_irqsave(&cache->lock, flags);
597 set_bit(from_dblock(b), cache->discard_bitset);
598 spin_unlock_irqrestore(&cache->lock, flags);
601 static void clear_discard(struct cache *cache, dm_dblock_t b)
603 unsigned long flags;
605 spin_lock_irqsave(&cache->lock, flags);
606 clear_bit(from_dblock(b), cache->discard_bitset);
607 spin_unlock_irqrestore(&cache->lock, flags);
610 static bool is_discarded(struct cache *cache, dm_dblock_t b)
612 int r;
613 unsigned long flags;
615 spin_lock_irqsave(&cache->lock, flags);
616 r = test_bit(from_dblock(b), cache->discard_bitset);
617 spin_unlock_irqrestore(&cache->lock, flags);
619 return r;
622 static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
624 int r;
625 unsigned long flags;
627 spin_lock_irqsave(&cache->lock, flags);
628 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
629 cache->discard_bitset);
630 spin_unlock_irqrestore(&cache->lock, flags);
632 return r;
635 /*----------------------------------------------------------------*/
637 static void load_stats(struct cache *cache)
639 struct dm_cache_statistics stats;
641 dm_cache_metadata_get_stats(cache->cmd, &stats);
642 atomic_set(&cache->stats.read_hit, stats.read_hits);
643 atomic_set(&cache->stats.read_miss, stats.read_misses);
644 atomic_set(&cache->stats.write_hit, stats.write_hits);
645 atomic_set(&cache->stats.write_miss, stats.write_misses);
648 static void save_stats(struct cache *cache)
650 struct dm_cache_statistics stats;
652 stats.read_hits = atomic_read(&cache->stats.read_hit);
653 stats.read_misses = atomic_read(&cache->stats.read_miss);
654 stats.write_hits = atomic_read(&cache->stats.write_hit);
655 stats.write_misses = atomic_read(&cache->stats.write_miss);
657 dm_cache_metadata_set_stats(cache->cmd, &stats);
660 /*----------------------------------------------------------------
661 * Per bio data
662 *--------------------------------------------------------------*/
665 * If using writeback, leave out struct per_bio_data's writethrough fields.
667 #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
668 #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
670 static bool writethrough_mode(struct cache_features *f)
672 return f->io_mode == CM_IO_WRITETHROUGH;
675 static bool writeback_mode(struct cache_features *f)
677 return f->io_mode == CM_IO_WRITEBACK;
680 static bool passthrough_mode(struct cache_features *f)
682 return f->io_mode == CM_IO_PASSTHROUGH;
685 static size_t get_per_bio_data_size(struct cache *cache)
687 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
690 static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
692 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
693 BUG_ON(!pb);
694 return pb;
697 static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
699 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
701 pb->tick = false;
702 pb->req_nr = dm_bio_get_target_bio_nr(bio);
703 pb->all_io_entry = NULL;
705 return pb;
708 /*----------------------------------------------------------------
709 * Remapping
710 *--------------------------------------------------------------*/
711 static void remap_to_origin(struct cache *cache, struct bio *bio)
713 bio->bi_bdev = cache->origin_dev->bdev;
716 static void remap_to_cache(struct cache *cache, struct bio *bio,
717 dm_cblock_t cblock)
719 sector_t bi_sector = bio->bi_iter.bi_sector;
720 sector_t block = from_cblock(cblock);
722 bio->bi_bdev = cache->cache_dev->bdev;
723 if (!block_size_is_power_of_two(cache))
724 bio->bi_iter.bi_sector =
725 (block * cache->sectors_per_block) +
726 sector_div(bi_sector, cache->sectors_per_block);
727 else
728 bio->bi_iter.bi_sector =
729 (block << cache->sectors_per_block_shift) |
730 (bi_sector & (cache->sectors_per_block - 1));
733 static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
735 unsigned long flags;
736 size_t pb_data_size = get_per_bio_data_size(cache);
737 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
739 spin_lock_irqsave(&cache->lock, flags);
740 if (cache->need_tick_bio &&
741 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
742 pb->tick = true;
743 cache->need_tick_bio = false;
745 spin_unlock_irqrestore(&cache->lock, flags);
748 static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
749 dm_oblock_t oblock)
751 check_if_tick_bio_needed(cache, bio);
752 remap_to_origin(cache, bio);
753 if (bio_data_dir(bio) == WRITE)
754 clear_discard(cache, oblock_to_dblock(cache, oblock));
757 static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
758 dm_oblock_t oblock, dm_cblock_t cblock)
760 check_if_tick_bio_needed(cache, bio);
761 remap_to_cache(cache, bio, cblock);
762 if (bio_data_dir(bio) == WRITE) {
763 set_dirty(cache, oblock, cblock);
764 clear_discard(cache, oblock_to_dblock(cache, oblock));
768 static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
770 sector_t block_nr = bio->bi_iter.bi_sector;
772 if (!block_size_is_power_of_two(cache))
773 (void) sector_div(block_nr, cache->sectors_per_block);
774 else
775 block_nr >>= cache->sectors_per_block_shift;
777 return to_oblock(block_nr);
780 static int bio_triggers_commit(struct cache *cache, struct bio *bio)
782 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
786 * You must increment the deferred set whilst the prison cell is held. To
787 * encourage this, we ask for 'cell' to be passed in.
789 static void inc_ds(struct cache *cache, struct bio *bio,
790 struct dm_bio_prison_cell *cell)
792 size_t pb_data_size = get_per_bio_data_size(cache);
793 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
795 BUG_ON(!cell);
796 BUG_ON(pb->all_io_entry);
798 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
801 static void issue(struct cache *cache, struct bio *bio)
803 unsigned long flags;
805 if (!bio_triggers_commit(cache, bio)) {
806 generic_make_request(bio);
807 return;
811 * Batch together any bios that trigger commits and then issue a
812 * single commit for them in do_worker().
814 spin_lock_irqsave(&cache->lock, flags);
815 cache->commit_requested = true;
816 bio_list_add(&cache->deferred_flush_bios, bio);
817 spin_unlock_irqrestore(&cache->lock, flags);
820 static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
822 inc_ds(cache, bio, cell);
823 issue(cache, bio);
826 static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
828 unsigned long flags;
830 spin_lock_irqsave(&cache->lock, flags);
831 bio_list_add(&cache->deferred_writethrough_bios, bio);
832 spin_unlock_irqrestore(&cache->lock, flags);
834 wake_worker(cache);
837 static void writethrough_endio(struct bio *bio, int err)
839 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
841 dm_unhook_bio(&pb->hook_info, bio);
843 if (err) {
844 bio_endio(bio, err);
845 return;
848 dm_bio_restore(&pb->bio_details, bio);
849 remap_to_cache(pb->cache, bio, pb->cblock);
852 * We can't issue this bio directly, since we're in interrupt
853 * context. So it gets put on a bio list for processing by the
854 * worker thread.
856 defer_writethrough_bio(pb->cache, bio);
860 * When running in writethrough mode we need to send writes to clean blocks
861 * to both the cache and origin devices. In future we'd like to clone the
862 * bio and send them in parallel, but for now we're doing them in
863 * series as this is easier.
865 static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
866 dm_oblock_t oblock, dm_cblock_t cblock)
868 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
870 pb->cache = cache;
871 pb->cblock = cblock;
872 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
873 dm_bio_record(&pb->bio_details, bio);
875 remap_to_origin_clear_discard(pb->cache, bio, oblock);
878 /*----------------------------------------------------------------
879 * Migration processing
881 * Migration covers moving data from the origin device to the cache, or
882 * vice versa.
883 *--------------------------------------------------------------*/
884 static void inc_io_migrations(struct cache *cache)
886 atomic_inc(&cache->nr_io_migrations);
889 static void dec_io_migrations(struct cache *cache)
891 atomic_dec(&cache->nr_io_migrations);
894 static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
895 bool holder)
897 (holder ? dm_cell_release : dm_cell_release_no_holder)
898 (cache->prison, cell, &cache->deferred_bios);
899 free_prison_cell(cache, cell);
902 static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
903 bool holder)
905 unsigned long flags;
907 spin_lock_irqsave(&cache->lock, flags);
908 __cell_defer(cache, cell, holder);
909 spin_unlock_irqrestore(&cache->lock, flags);
911 wake_worker(cache);
914 static void free_io_migration(struct dm_cache_migration *mg)
916 dec_io_migrations(mg->cache);
917 free_migration(mg);
920 static void migration_failure(struct dm_cache_migration *mg)
922 struct cache *cache = mg->cache;
924 if (mg->writeback) {
925 DMWARN_LIMIT("writeback failed; couldn't copy block");
926 set_dirty(cache, mg->old_oblock, mg->cblock);
927 cell_defer(cache, mg->old_ocell, false);
929 } else if (mg->demote) {
930 DMWARN_LIMIT("demotion failed; couldn't copy block");
931 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
933 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
934 if (mg->promote)
935 cell_defer(cache, mg->new_ocell, true);
936 } else {
937 DMWARN_LIMIT("promotion failed; couldn't copy block");
938 policy_remove_mapping(cache->policy, mg->new_oblock);
939 cell_defer(cache, mg->new_ocell, true);
942 free_io_migration(mg);
945 static void migration_success_pre_commit(struct dm_cache_migration *mg)
947 unsigned long flags;
948 struct cache *cache = mg->cache;
950 if (mg->writeback) {
951 clear_dirty(cache, mg->old_oblock, mg->cblock);
952 cell_defer(cache, mg->old_ocell, false);
953 free_io_migration(mg);
954 return;
956 } else if (mg->demote) {
957 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
958 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
959 policy_force_mapping(cache->policy, mg->new_oblock,
960 mg->old_oblock);
961 if (mg->promote)
962 cell_defer(cache, mg->new_ocell, true);
963 free_io_migration(mg);
964 return;
966 } else {
967 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
968 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
969 policy_remove_mapping(cache->policy, mg->new_oblock);
970 free_io_migration(mg);
971 return;
975 spin_lock_irqsave(&cache->lock, flags);
976 list_add_tail(&mg->list, &cache->need_commit_migrations);
977 cache->commit_requested = true;
978 spin_unlock_irqrestore(&cache->lock, flags);
981 static void migration_success_post_commit(struct dm_cache_migration *mg)
983 unsigned long flags;
984 struct cache *cache = mg->cache;
986 if (mg->writeback) {
987 DMWARN("writeback unexpectedly triggered commit");
988 return;
990 } else if (mg->demote) {
991 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
993 if (mg->promote) {
994 mg->demote = false;
996 spin_lock_irqsave(&cache->lock, flags);
997 list_add_tail(&mg->list, &cache->quiesced_migrations);
998 spin_unlock_irqrestore(&cache->lock, flags);
1000 } else {
1001 if (mg->invalidate)
1002 policy_remove_mapping(cache->policy, mg->old_oblock);
1003 free_io_migration(mg);
1006 } else {
1007 if (mg->requeue_holder) {
1008 clear_dirty(cache, mg->new_oblock, mg->cblock);
1009 cell_defer(cache, mg->new_ocell, true);
1010 } else {
1012 * The block was promoted via an overwrite, so it's dirty.
1014 set_dirty(cache, mg->new_oblock, mg->cblock);
1015 bio_endio(mg->new_ocell->holder, 0);
1016 cell_defer(cache, mg->new_ocell, false);
1018 free_io_migration(mg);
1022 static void copy_complete(int read_err, unsigned long write_err, void *context)
1024 unsigned long flags;
1025 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
1026 struct cache *cache = mg->cache;
1028 if (read_err || write_err)
1029 mg->err = true;
1031 spin_lock_irqsave(&cache->lock, flags);
1032 list_add_tail(&mg->list, &cache->completed_migrations);
1033 spin_unlock_irqrestore(&cache->lock, flags);
1035 wake_worker(cache);
1038 static void issue_copy(struct dm_cache_migration *mg)
1040 int r;
1041 struct dm_io_region o_region, c_region;
1042 struct cache *cache = mg->cache;
1043 sector_t cblock = from_cblock(mg->cblock);
1045 o_region.bdev = cache->origin_dev->bdev;
1046 o_region.count = cache->sectors_per_block;
1048 c_region.bdev = cache->cache_dev->bdev;
1049 c_region.sector = cblock * cache->sectors_per_block;
1050 c_region.count = cache->sectors_per_block;
1052 if (mg->writeback || mg->demote) {
1053 /* demote */
1054 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
1055 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
1056 } else {
1057 /* promote */
1058 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1059 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1062 if (r < 0) {
1063 DMERR_LIMIT("issuing migration failed");
1064 migration_failure(mg);
1068 static void overwrite_endio(struct bio *bio, int err)
1070 struct dm_cache_migration *mg = bio->bi_private;
1071 struct cache *cache = mg->cache;
1072 size_t pb_data_size = get_per_bio_data_size(cache);
1073 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1074 unsigned long flags;
1076 dm_unhook_bio(&pb->hook_info, bio);
1078 if (err)
1079 mg->err = true;
1081 mg->requeue_holder = false;
1083 spin_lock_irqsave(&cache->lock, flags);
1084 list_add_tail(&mg->list, &cache->completed_migrations);
1085 spin_unlock_irqrestore(&cache->lock, flags);
1087 wake_worker(cache);
1090 static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1092 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1093 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1095 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1096 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
1099 * No need to inc_ds() here, since the cell will be held for the
1100 * duration of the io.
1102 generic_make_request(bio);
1105 static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1107 return (bio_data_dir(bio) == WRITE) &&
1108 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
1111 static void avoid_copy(struct dm_cache_migration *mg)
1113 atomic_inc(&mg->cache->stats.copies_avoided);
1114 migration_success_pre_commit(mg);
1117 static void calc_discard_block_range(struct cache *cache, struct bio *bio,
1118 dm_dblock_t *b, dm_dblock_t *e)
1120 sector_t sb = bio->bi_iter.bi_sector;
1121 sector_t se = bio_end_sector(bio);
1123 *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
1125 if (se - sb < cache->discard_block_size)
1126 *e = *b;
1127 else
1128 *e = to_dblock(block_div(se, cache->discard_block_size));
1131 static void issue_discard(struct dm_cache_migration *mg)
1133 dm_dblock_t b, e;
1134 struct bio *bio = mg->new_ocell->holder;
1136 calc_discard_block_range(mg->cache, bio, &b, &e);
1137 while (b != e) {
1138 set_discard(mg->cache, b);
1139 b = to_dblock(from_dblock(b) + 1);
1142 bio_endio(bio, 0);
1143 cell_defer(mg->cache, mg->new_ocell, false);
1144 free_migration(mg);
1147 static void issue_copy_or_discard(struct dm_cache_migration *mg)
1149 bool avoid;
1150 struct cache *cache = mg->cache;
1152 if (mg->discard) {
1153 issue_discard(mg);
1154 return;
1157 if (mg->writeback || mg->demote)
1158 avoid = !is_dirty(cache, mg->cblock) ||
1159 is_discarded_oblock(cache, mg->old_oblock);
1160 else {
1161 struct bio *bio = mg->new_ocell->holder;
1163 avoid = is_discarded_oblock(cache, mg->new_oblock);
1165 if (writeback_mode(&cache->features) &&
1166 !avoid && bio_writes_complete_block(cache, bio)) {
1167 issue_overwrite(mg, bio);
1168 return;
1172 avoid ? avoid_copy(mg) : issue_copy(mg);
1175 static void complete_migration(struct dm_cache_migration *mg)
1177 if (mg->err)
1178 migration_failure(mg);
1179 else
1180 migration_success_pre_commit(mg);
1183 static void process_migrations(struct cache *cache, struct list_head *head,
1184 void (*fn)(struct dm_cache_migration *))
1186 unsigned long flags;
1187 struct list_head list;
1188 struct dm_cache_migration *mg, *tmp;
1190 INIT_LIST_HEAD(&list);
1191 spin_lock_irqsave(&cache->lock, flags);
1192 list_splice_init(head, &list);
1193 spin_unlock_irqrestore(&cache->lock, flags);
1195 list_for_each_entry_safe(mg, tmp, &list, list)
1196 fn(mg);
1199 static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1201 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1204 static void queue_quiesced_migration(struct dm_cache_migration *mg)
1206 unsigned long flags;
1207 struct cache *cache = mg->cache;
1209 spin_lock_irqsave(&cache->lock, flags);
1210 __queue_quiesced_migration(mg);
1211 spin_unlock_irqrestore(&cache->lock, flags);
1213 wake_worker(cache);
1216 static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1218 unsigned long flags;
1219 struct dm_cache_migration *mg, *tmp;
1221 spin_lock_irqsave(&cache->lock, flags);
1222 list_for_each_entry_safe(mg, tmp, work, list)
1223 __queue_quiesced_migration(mg);
1224 spin_unlock_irqrestore(&cache->lock, flags);
1226 wake_worker(cache);
1229 static void check_for_quiesced_migrations(struct cache *cache,
1230 struct per_bio_data *pb)
1232 struct list_head work;
1234 if (!pb->all_io_entry)
1235 return;
1237 INIT_LIST_HEAD(&work);
1238 dm_deferred_entry_dec(pb->all_io_entry, &work);
1240 if (!list_empty(&work))
1241 queue_quiesced_migrations(cache, &work);
1244 static void quiesce_migration(struct dm_cache_migration *mg)
1246 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1247 queue_quiesced_migration(mg);
1250 static void promote(struct cache *cache, struct prealloc *structs,
1251 dm_oblock_t oblock, dm_cblock_t cblock,
1252 struct dm_bio_prison_cell *cell)
1254 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1256 mg->err = false;
1257 mg->discard = false;
1258 mg->writeback = false;
1259 mg->demote = false;
1260 mg->promote = true;
1261 mg->requeue_holder = true;
1262 mg->invalidate = false;
1263 mg->cache = cache;
1264 mg->new_oblock = oblock;
1265 mg->cblock = cblock;
1266 mg->old_ocell = NULL;
1267 mg->new_ocell = cell;
1268 mg->start_jiffies = jiffies;
1270 inc_io_migrations(cache);
1271 quiesce_migration(mg);
1274 static void writeback(struct cache *cache, struct prealloc *structs,
1275 dm_oblock_t oblock, dm_cblock_t cblock,
1276 struct dm_bio_prison_cell *cell)
1278 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1280 mg->err = false;
1281 mg->discard = false;
1282 mg->writeback = true;
1283 mg->demote = false;
1284 mg->promote = false;
1285 mg->requeue_holder = true;
1286 mg->invalidate = false;
1287 mg->cache = cache;
1288 mg->old_oblock = oblock;
1289 mg->cblock = cblock;
1290 mg->old_ocell = cell;
1291 mg->new_ocell = NULL;
1292 mg->start_jiffies = jiffies;
1294 inc_io_migrations(cache);
1295 quiesce_migration(mg);
1298 static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1299 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1300 dm_cblock_t cblock,
1301 struct dm_bio_prison_cell *old_ocell,
1302 struct dm_bio_prison_cell *new_ocell)
1304 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1306 mg->err = false;
1307 mg->discard = false;
1308 mg->writeback = false;
1309 mg->demote = true;
1310 mg->promote = true;
1311 mg->requeue_holder = true;
1312 mg->invalidate = false;
1313 mg->cache = cache;
1314 mg->old_oblock = old_oblock;
1315 mg->new_oblock = new_oblock;
1316 mg->cblock = cblock;
1317 mg->old_ocell = old_ocell;
1318 mg->new_ocell = new_ocell;
1319 mg->start_jiffies = jiffies;
1321 inc_io_migrations(cache);
1322 quiesce_migration(mg);
1326 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1327 * block are thrown away.
1329 static void invalidate(struct cache *cache, struct prealloc *structs,
1330 dm_oblock_t oblock, dm_cblock_t cblock,
1331 struct dm_bio_prison_cell *cell)
1333 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1335 mg->err = false;
1336 mg->discard = false;
1337 mg->writeback = false;
1338 mg->demote = true;
1339 mg->promote = false;
1340 mg->requeue_holder = true;
1341 mg->invalidate = true;
1342 mg->cache = cache;
1343 mg->old_oblock = oblock;
1344 mg->cblock = cblock;
1345 mg->old_ocell = cell;
1346 mg->new_ocell = NULL;
1347 mg->start_jiffies = jiffies;
1349 inc_io_migrations(cache);
1350 quiesce_migration(mg);
1353 static void discard(struct cache *cache, struct prealloc *structs,
1354 struct dm_bio_prison_cell *cell)
1356 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1358 mg->err = false;
1359 mg->discard = true;
1360 mg->writeback = false;
1361 mg->demote = false;
1362 mg->promote = false;
1363 mg->requeue_holder = false;
1364 mg->invalidate = false;
1365 mg->cache = cache;
1366 mg->old_ocell = NULL;
1367 mg->new_ocell = cell;
1368 mg->start_jiffies = jiffies;
1370 quiesce_migration(mg);
1373 /*----------------------------------------------------------------
1374 * bio processing
1375 *--------------------------------------------------------------*/
1376 static void defer_bio(struct cache *cache, struct bio *bio)
1378 unsigned long flags;
1380 spin_lock_irqsave(&cache->lock, flags);
1381 bio_list_add(&cache->deferred_bios, bio);
1382 spin_unlock_irqrestore(&cache->lock, flags);
1384 wake_worker(cache);
1387 static void process_flush_bio(struct cache *cache, struct bio *bio)
1389 size_t pb_data_size = get_per_bio_data_size(cache);
1390 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1392 BUG_ON(bio->bi_iter.bi_size);
1393 if (!pb->req_nr)
1394 remap_to_origin(cache, bio);
1395 else
1396 remap_to_cache(cache, bio, 0);
1399 * REQ_FLUSH is not directed at any particular block so we don't
1400 * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH
1401 * by dm-core.
1403 issue(cache, bio);
1406 static void process_discard_bio(struct cache *cache, struct prealloc *structs,
1407 struct bio *bio)
1409 int r;
1410 dm_dblock_t b, e;
1411 struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
1413 calc_discard_block_range(cache, bio, &b, &e);
1414 if (b == e) {
1415 bio_endio(bio, 0);
1416 return;
1419 cell_prealloc = prealloc_get_cell(structs);
1420 r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc,
1421 (cell_free_fn) prealloc_put_cell,
1422 structs, &new_ocell);
1423 if (r > 0)
1424 return;
1426 discard(cache, structs, new_ocell);
1429 static bool spare_migration_bandwidth(struct cache *cache)
1431 sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
1432 cache->sectors_per_block;
1433 return current_volume < cache->migration_threshold;
1436 static void inc_hit_counter(struct cache *cache, struct bio *bio)
1438 atomic_inc(bio_data_dir(bio) == READ ?
1439 &cache->stats.read_hit : &cache->stats.write_hit);
1442 static void inc_miss_counter(struct cache *cache, struct bio *bio)
1444 atomic_inc(bio_data_dir(bio) == READ ?
1445 &cache->stats.read_miss : &cache->stats.write_miss);
1448 /*----------------------------------------------------------------*/
1450 struct old_oblock_lock {
1451 struct policy_locker locker;
1452 struct cache *cache;
1453 struct prealloc *structs;
1454 struct dm_bio_prison_cell *cell;
1457 static int null_locker(struct policy_locker *locker, dm_oblock_t b)
1459 /* This should never be called */
1460 BUG();
1461 return 0;
1464 static int cell_locker(struct policy_locker *locker, dm_oblock_t b)
1466 struct old_oblock_lock *l = container_of(locker, struct old_oblock_lock, locker);
1467 struct dm_bio_prison_cell *cell_prealloc = prealloc_get_cell(l->structs);
1469 return bio_detain(l->cache, b, NULL, cell_prealloc,
1470 (cell_free_fn) prealloc_put_cell,
1471 l->structs, &l->cell);
1474 static void process_bio(struct cache *cache, struct prealloc *structs,
1475 struct bio *bio)
1477 int r;
1478 bool release_cell = true;
1479 dm_oblock_t block = get_bio_block(cache, bio);
1480 struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
1481 struct policy_result lookup_result;
1482 bool passthrough = passthrough_mode(&cache->features);
1483 bool discarded_block, can_migrate;
1484 struct old_oblock_lock ool;
1487 * Check to see if that block is currently migrating.
1489 cell_prealloc = prealloc_get_cell(structs);
1490 r = bio_detain(cache, block, bio, cell_prealloc,
1491 (cell_free_fn) prealloc_put_cell,
1492 structs, &new_ocell);
1493 if (r > 0)
1494 return;
1496 discarded_block = is_discarded_oblock(cache, block);
1497 can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
1499 ool.locker.fn = cell_locker;
1500 ool.cache = cache;
1501 ool.structs = structs;
1502 ool.cell = NULL;
1503 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1504 bio, &ool.locker, &lookup_result);
1506 if (r == -EWOULDBLOCK)
1507 /* migration has been denied */
1508 lookup_result.op = POLICY_MISS;
1510 switch (lookup_result.op) {
1511 case POLICY_HIT:
1512 if (passthrough) {
1513 inc_miss_counter(cache, bio);
1516 * Passthrough always maps to the origin,
1517 * invalidating any cache blocks that are written
1518 * to.
1521 if (bio_data_dir(bio) == WRITE) {
1522 atomic_inc(&cache->stats.demotion);
1523 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1524 release_cell = false;
1526 } else {
1527 /* FIXME: factor out issue_origin() */
1528 remap_to_origin_clear_discard(cache, bio, block);
1529 inc_and_issue(cache, bio, new_ocell);
1531 } else {
1532 inc_hit_counter(cache, bio);
1534 if (bio_data_dir(bio) == WRITE &&
1535 writethrough_mode(&cache->features) &&
1536 !is_dirty(cache, lookup_result.cblock)) {
1537 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
1538 inc_and_issue(cache, bio, new_ocell);
1540 } else {
1541 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1542 inc_and_issue(cache, bio, new_ocell);
1546 break;
1548 case POLICY_MISS:
1549 inc_miss_counter(cache, bio);
1550 remap_to_origin_clear_discard(cache, bio, block);
1551 inc_and_issue(cache, bio, new_ocell);
1552 break;
1554 case POLICY_NEW:
1555 atomic_inc(&cache->stats.promotion);
1556 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1557 release_cell = false;
1558 break;
1560 case POLICY_REPLACE:
1561 atomic_inc(&cache->stats.demotion);
1562 atomic_inc(&cache->stats.promotion);
1563 demote_then_promote(cache, structs, lookup_result.old_oblock,
1564 block, lookup_result.cblock,
1565 ool.cell, new_ocell);
1566 release_cell = false;
1567 break;
1569 default:
1570 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1571 (unsigned) lookup_result.op);
1572 bio_io_error(bio);
1575 if (release_cell)
1576 cell_defer(cache, new_ocell, false);
1579 static int need_commit_due_to_time(struct cache *cache)
1581 return !time_in_range(jiffies, cache->last_commit_jiffies,
1582 cache->last_commit_jiffies + COMMIT_PERIOD);
1585 static int commit_if_needed(struct cache *cache)
1587 int r = 0;
1589 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1590 dm_cache_changed_this_transaction(cache->cmd)) {
1591 atomic_inc(&cache->stats.commit_count);
1592 cache->commit_requested = false;
1593 r = dm_cache_commit(cache->cmd, false);
1594 cache->last_commit_jiffies = jiffies;
1597 return r;
1600 static void process_deferred_bios(struct cache *cache)
1602 unsigned long flags;
1603 struct bio_list bios;
1604 struct bio *bio;
1605 struct prealloc structs;
1607 memset(&structs, 0, sizeof(structs));
1608 bio_list_init(&bios);
1610 spin_lock_irqsave(&cache->lock, flags);
1611 bio_list_merge(&bios, &cache->deferred_bios);
1612 bio_list_init(&cache->deferred_bios);
1613 spin_unlock_irqrestore(&cache->lock, flags);
1615 while (!bio_list_empty(&bios)) {
1617 * If we've got no free migration structs, and processing
1618 * this bio might require one, we pause until there are some
1619 * prepared mappings to process.
1621 if (prealloc_data_structs(cache, &structs)) {
1622 spin_lock_irqsave(&cache->lock, flags);
1623 bio_list_merge(&cache->deferred_bios, &bios);
1624 spin_unlock_irqrestore(&cache->lock, flags);
1625 break;
1628 bio = bio_list_pop(&bios);
1630 if (bio->bi_rw & REQ_FLUSH)
1631 process_flush_bio(cache, bio);
1632 else if (bio->bi_rw & REQ_DISCARD)
1633 process_discard_bio(cache, &structs, bio);
1634 else
1635 process_bio(cache, &structs, bio);
1638 prealloc_free_structs(cache, &structs);
1641 static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1643 unsigned long flags;
1644 struct bio_list bios;
1645 struct bio *bio;
1647 bio_list_init(&bios);
1649 spin_lock_irqsave(&cache->lock, flags);
1650 bio_list_merge(&bios, &cache->deferred_flush_bios);
1651 bio_list_init(&cache->deferred_flush_bios);
1652 spin_unlock_irqrestore(&cache->lock, flags);
1655 * These bios have already been through inc_ds()
1657 while ((bio = bio_list_pop(&bios)))
1658 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1661 static void process_deferred_writethrough_bios(struct cache *cache)
1663 unsigned long flags;
1664 struct bio_list bios;
1665 struct bio *bio;
1667 bio_list_init(&bios);
1669 spin_lock_irqsave(&cache->lock, flags);
1670 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1671 bio_list_init(&cache->deferred_writethrough_bios);
1672 spin_unlock_irqrestore(&cache->lock, flags);
1675 * These bios have already been through inc_ds()
1677 while ((bio = bio_list_pop(&bios)))
1678 generic_make_request(bio);
1681 static void writeback_some_dirty_blocks(struct cache *cache)
1683 int r = 0;
1684 dm_oblock_t oblock;
1685 dm_cblock_t cblock;
1686 struct prealloc structs;
1687 struct dm_bio_prison_cell *old_ocell;
1689 memset(&structs, 0, sizeof(structs));
1691 while (spare_migration_bandwidth(cache)) {
1692 if (prealloc_data_structs(cache, &structs))
1693 break;
1695 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1696 if (r)
1697 break;
1699 r = get_cell(cache, oblock, &structs, &old_ocell);
1700 if (r) {
1701 policy_set_dirty(cache->policy, oblock);
1702 break;
1705 writeback(cache, &structs, oblock, cblock, old_ocell);
1708 prealloc_free_structs(cache, &structs);
1711 /*----------------------------------------------------------------
1712 * Invalidations.
1713 * Dropping something from the cache *without* writing back.
1714 *--------------------------------------------------------------*/
1716 static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1718 int r = 0;
1719 uint64_t begin = from_cblock(req->cblocks->begin);
1720 uint64_t end = from_cblock(req->cblocks->end);
1722 while (begin != end) {
1723 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1724 if (!r) {
1725 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1726 if (r)
1727 break;
1729 } else if (r == -ENODATA) {
1730 /* harmless, already unmapped */
1731 r = 0;
1733 } else {
1734 DMERR("policy_remove_cblock failed");
1735 break;
1738 begin++;
1741 cache->commit_requested = true;
1743 req->err = r;
1744 atomic_set(&req->complete, 1);
1746 wake_up(&req->result_wait);
1749 static void process_invalidation_requests(struct cache *cache)
1751 struct list_head list;
1752 struct invalidation_request *req, *tmp;
1754 INIT_LIST_HEAD(&list);
1755 spin_lock(&cache->invalidation_lock);
1756 list_splice_init(&cache->invalidation_requests, &list);
1757 spin_unlock(&cache->invalidation_lock);
1759 list_for_each_entry_safe (req, tmp, &list, list)
1760 process_invalidation_request(cache, req);
1763 /*----------------------------------------------------------------
1764 * Main worker loop
1765 *--------------------------------------------------------------*/
1766 static bool is_quiescing(struct cache *cache)
1768 return atomic_read(&cache->quiescing);
1771 static void ack_quiescing(struct cache *cache)
1773 if (is_quiescing(cache)) {
1774 atomic_inc(&cache->quiescing_ack);
1775 wake_up(&cache->quiescing_wait);
1779 static void wait_for_quiescing_ack(struct cache *cache)
1781 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1784 static void start_quiescing(struct cache *cache)
1786 atomic_inc(&cache->quiescing);
1787 wait_for_quiescing_ack(cache);
1790 static void stop_quiescing(struct cache *cache)
1792 atomic_set(&cache->quiescing, 0);
1793 atomic_set(&cache->quiescing_ack, 0);
1796 static void wait_for_migrations(struct cache *cache)
1798 wait_event(cache->migration_wait, !atomic_read(&cache->nr_allocated_migrations));
1801 static void stop_worker(struct cache *cache)
1803 cancel_delayed_work(&cache->waker);
1804 flush_workqueue(cache->wq);
1807 static void requeue_deferred_io(struct cache *cache)
1809 struct bio *bio;
1810 struct bio_list bios;
1812 bio_list_init(&bios);
1813 bio_list_merge(&bios, &cache->deferred_bios);
1814 bio_list_init(&cache->deferred_bios);
1816 while ((bio = bio_list_pop(&bios)))
1817 bio_endio(bio, DM_ENDIO_REQUEUE);
1820 static int more_work(struct cache *cache)
1822 if (is_quiescing(cache))
1823 return !list_empty(&cache->quiesced_migrations) ||
1824 !list_empty(&cache->completed_migrations) ||
1825 !list_empty(&cache->need_commit_migrations);
1826 else
1827 return !bio_list_empty(&cache->deferred_bios) ||
1828 !bio_list_empty(&cache->deferred_flush_bios) ||
1829 !bio_list_empty(&cache->deferred_writethrough_bios) ||
1830 !list_empty(&cache->quiesced_migrations) ||
1831 !list_empty(&cache->completed_migrations) ||
1832 !list_empty(&cache->need_commit_migrations) ||
1833 cache->invalidate;
1836 static void do_worker(struct work_struct *ws)
1838 struct cache *cache = container_of(ws, struct cache, worker);
1840 do {
1841 if (!is_quiescing(cache)) {
1842 writeback_some_dirty_blocks(cache);
1843 process_deferred_writethrough_bios(cache);
1844 process_deferred_bios(cache);
1845 process_invalidation_requests(cache);
1848 process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard);
1849 process_migrations(cache, &cache->completed_migrations, complete_migration);
1851 if (commit_if_needed(cache)) {
1852 process_deferred_flush_bios(cache, false);
1853 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
1856 * FIXME: rollback metadata or just go into a
1857 * failure mode and error everything
1859 } else {
1860 process_deferred_flush_bios(cache, true);
1861 process_migrations(cache, &cache->need_commit_migrations,
1862 migration_success_post_commit);
1865 ack_quiescing(cache);
1867 } while (more_work(cache));
1871 * We want to commit periodically so that not too much
1872 * unwritten metadata builds up.
1874 static void do_waker(struct work_struct *ws)
1876 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
1877 policy_tick(cache->policy);
1878 wake_worker(cache);
1879 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1882 /*----------------------------------------------------------------*/
1884 static int is_congested(struct dm_dev *dev, int bdi_bits)
1886 struct request_queue *q = bdev_get_queue(dev->bdev);
1887 return bdi_congested(&q->backing_dev_info, bdi_bits);
1890 static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1892 struct cache *cache = container_of(cb, struct cache, callbacks);
1894 return is_congested(cache->origin_dev, bdi_bits) ||
1895 is_congested(cache->cache_dev, bdi_bits);
1898 /*----------------------------------------------------------------
1899 * Target methods
1900 *--------------------------------------------------------------*/
1903 * This function gets called on the error paths of the constructor, so we
1904 * have to cope with a partially initialised struct.
1906 static void destroy(struct cache *cache)
1908 unsigned i;
1910 if (cache->migration_pool)
1911 mempool_destroy(cache->migration_pool);
1913 if (cache->all_io_ds)
1914 dm_deferred_set_destroy(cache->all_io_ds);
1916 if (cache->prison)
1917 dm_bio_prison_destroy(cache->prison);
1919 if (cache->wq)
1920 destroy_workqueue(cache->wq);
1922 if (cache->dirty_bitset)
1923 free_bitset(cache->dirty_bitset);
1925 if (cache->discard_bitset)
1926 free_bitset(cache->discard_bitset);
1928 if (cache->copier)
1929 dm_kcopyd_client_destroy(cache->copier);
1931 if (cache->cmd)
1932 dm_cache_metadata_close(cache->cmd);
1934 if (cache->metadata_dev)
1935 dm_put_device(cache->ti, cache->metadata_dev);
1937 if (cache->origin_dev)
1938 dm_put_device(cache->ti, cache->origin_dev);
1940 if (cache->cache_dev)
1941 dm_put_device(cache->ti, cache->cache_dev);
1943 if (cache->policy)
1944 dm_cache_policy_destroy(cache->policy);
1946 for (i = 0; i < cache->nr_ctr_args ; i++)
1947 kfree(cache->ctr_args[i]);
1948 kfree(cache->ctr_args);
1950 kfree(cache);
1953 static void cache_dtr(struct dm_target *ti)
1955 struct cache *cache = ti->private;
1957 destroy(cache);
1960 static sector_t get_dev_size(struct dm_dev *dev)
1962 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1965 /*----------------------------------------------------------------*/
1968 * Construct a cache device mapping.
1970 * cache <metadata dev> <cache dev> <origin dev> <block size>
1971 * <#feature args> [<feature arg>]*
1972 * <policy> <#policy args> [<policy arg>]*
1974 * metadata dev : fast device holding the persistent metadata
1975 * cache dev : fast device holding cached data blocks
1976 * origin dev : slow device holding original data blocks
1977 * block size : cache unit size in sectors
1979 * #feature args : number of feature arguments passed
1980 * feature args : writethrough. (The default is writeback.)
1982 * policy : the replacement policy to use
1983 * #policy args : an even number of policy arguments corresponding
1984 * to key/value pairs passed to the policy
1985 * policy args : key/value pairs passed to the policy
1986 * E.g. 'sequential_threshold 1024'
1987 * See cache-policies.txt for details.
1989 * Optional feature arguments are:
1990 * writethrough : write through caching that prohibits cache block
1991 * content from being different from origin block content.
1992 * Without this argument, the default behaviour is to write
1993 * back cache block contents later for performance reasons,
1994 * so they may differ from the corresponding origin blocks.
1996 struct cache_args {
1997 struct dm_target *ti;
1999 struct dm_dev *metadata_dev;
2001 struct dm_dev *cache_dev;
2002 sector_t cache_sectors;
2004 struct dm_dev *origin_dev;
2005 sector_t origin_sectors;
2007 uint32_t block_size;
2009 const char *policy_name;
2010 int policy_argc;
2011 const char **policy_argv;
2013 struct cache_features features;
2016 static void destroy_cache_args(struct cache_args *ca)
2018 if (ca->metadata_dev)
2019 dm_put_device(ca->ti, ca->metadata_dev);
2021 if (ca->cache_dev)
2022 dm_put_device(ca->ti, ca->cache_dev);
2024 if (ca->origin_dev)
2025 dm_put_device(ca->ti, ca->origin_dev);
2027 kfree(ca);
2030 static bool at_least_one_arg(struct dm_arg_set *as, char **error)
2032 if (!as->argc) {
2033 *error = "Insufficient args";
2034 return false;
2037 return true;
2040 static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
2041 char **error)
2043 int r;
2044 sector_t metadata_dev_size;
2045 char b[BDEVNAME_SIZE];
2047 if (!at_least_one_arg(as, error))
2048 return -EINVAL;
2050 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2051 &ca->metadata_dev);
2052 if (r) {
2053 *error = "Error opening metadata device";
2054 return r;
2057 metadata_dev_size = get_dev_size(ca->metadata_dev);
2058 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
2059 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2060 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
2062 return 0;
2065 static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
2066 char **error)
2068 int r;
2070 if (!at_least_one_arg(as, error))
2071 return -EINVAL;
2073 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2074 &ca->cache_dev);
2075 if (r) {
2076 *error = "Error opening cache device";
2077 return r;
2079 ca->cache_sectors = get_dev_size(ca->cache_dev);
2081 return 0;
2084 static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
2085 char **error)
2087 int r;
2089 if (!at_least_one_arg(as, error))
2090 return -EINVAL;
2092 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2093 &ca->origin_dev);
2094 if (r) {
2095 *error = "Error opening origin device";
2096 return r;
2099 ca->origin_sectors = get_dev_size(ca->origin_dev);
2100 if (ca->ti->len > ca->origin_sectors) {
2101 *error = "Device size larger than cached device";
2102 return -EINVAL;
2105 return 0;
2108 static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
2109 char **error)
2111 unsigned long block_size;
2113 if (!at_least_one_arg(as, error))
2114 return -EINVAL;
2116 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
2117 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2118 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2119 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
2120 *error = "Invalid data block size";
2121 return -EINVAL;
2124 if (block_size > ca->cache_sectors) {
2125 *error = "Data block size is larger than the cache device";
2126 return -EINVAL;
2129 ca->block_size = block_size;
2131 return 0;
2134 static void init_features(struct cache_features *cf)
2136 cf->mode = CM_WRITE;
2137 cf->io_mode = CM_IO_WRITEBACK;
2140 static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2141 char **error)
2143 static struct dm_arg _args[] = {
2144 {0, 1, "Invalid number of cache feature arguments"},
2147 int r;
2148 unsigned argc;
2149 const char *arg;
2150 struct cache_features *cf = &ca->features;
2152 init_features(cf);
2154 r = dm_read_arg_group(_args, as, &argc, error);
2155 if (r)
2156 return -EINVAL;
2158 while (argc--) {
2159 arg = dm_shift_arg(as);
2161 if (!strcasecmp(arg, "writeback"))
2162 cf->io_mode = CM_IO_WRITEBACK;
2164 else if (!strcasecmp(arg, "writethrough"))
2165 cf->io_mode = CM_IO_WRITETHROUGH;
2167 else if (!strcasecmp(arg, "passthrough"))
2168 cf->io_mode = CM_IO_PASSTHROUGH;
2170 else {
2171 *error = "Unrecognised cache feature requested";
2172 return -EINVAL;
2176 return 0;
2179 static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2180 char **error)
2182 static struct dm_arg _args[] = {
2183 {0, 1024, "Invalid number of policy arguments"},
2186 int r;
2188 if (!at_least_one_arg(as, error))
2189 return -EINVAL;
2191 ca->policy_name = dm_shift_arg(as);
2193 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2194 if (r)
2195 return -EINVAL;
2197 ca->policy_argv = (const char **)as->argv;
2198 dm_consume_args(as, ca->policy_argc);
2200 return 0;
2203 static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2204 char **error)
2206 int r;
2207 struct dm_arg_set as;
2209 as.argc = argc;
2210 as.argv = argv;
2212 r = parse_metadata_dev(ca, &as, error);
2213 if (r)
2214 return r;
2216 r = parse_cache_dev(ca, &as, error);
2217 if (r)
2218 return r;
2220 r = parse_origin_dev(ca, &as, error);
2221 if (r)
2222 return r;
2224 r = parse_block_size(ca, &as, error);
2225 if (r)
2226 return r;
2228 r = parse_features(ca, &as, error);
2229 if (r)
2230 return r;
2232 r = parse_policy(ca, &as, error);
2233 if (r)
2234 return r;
2236 return 0;
2239 /*----------------------------------------------------------------*/
2241 static struct kmem_cache *migration_cache;
2243 #define NOT_CORE_OPTION 1
2245 static int process_config_option(struct cache *cache, const char *key, const char *value)
2247 unsigned long tmp;
2249 if (!strcasecmp(key, "migration_threshold")) {
2250 if (kstrtoul(value, 10, &tmp))
2251 return -EINVAL;
2253 cache->migration_threshold = tmp;
2254 return 0;
2257 return NOT_CORE_OPTION;
2260 static int set_config_value(struct cache *cache, const char *key, const char *value)
2262 int r = process_config_option(cache, key, value);
2264 if (r == NOT_CORE_OPTION)
2265 r = policy_set_config_value(cache->policy, key, value);
2267 if (r)
2268 DMWARN("bad config value for %s: %s", key, value);
2270 return r;
2273 static int set_config_values(struct cache *cache, int argc, const char **argv)
2275 int r = 0;
2277 if (argc & 1) {
2278 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2279 return -EINVAL;
2282 while (argc) {
2283 r = set_config_value(cache, argv[0], argv[1]);
2284 if (r)
2285 break;
2287 argc -= 2;
2288 argv += 2;
2291 return r;
2294 static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2295 char **error)
2297 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2298 cache->cache_size,
2299 cache->origin_sectors,
2300 cache->sectors_per_block);
2301 if (IS_ERR(p)) {
2302 *error = "Error creating cache's policy";
2303 return PTR_ERR(p);
2305 cache->policy = p;
2307 return 0;
2311 * We want the discard block size to be at least the size of the cache
2312 * block size and have no more than 2^14 discard blocks across the origin.
2314 #define MAX_DISCARD_BLOCKS (1 << 14)
2316 static bool too_many_discard_blocks(sector_t discard_block_size,
2317 sector_t origin_size)
2319 (void) sector_div(origin_size, discard_block_size);
2321 return origin_size > MAX_DISCARD_BLOCKS;
2324 static sector_t calculate_discard_block_size(sector_t cache_block_size,
2325 sector_t origin_size)
2327 sector_t discard_block_size = cache_block_size;
2329 if (origin_size)
2330 while (too_many_discard_blocks(discard_block_size, origin_size))
2331 discard_block_size *= 2;
2333 return discard_block_size;
2336 static void set_cache_size(struct cache *cache, dm_cblock_t size)
2338 dm_block_t nr_blocks = from_cblock(size);
2340 if (nr_blocks > (1 << 20) && cache->cache_size != size)
2341 DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
2342 "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
2343 "Please consider increasing the cache block size to reduce the overall cache block count.",
2344 (unsigned long long) nr_blocks);
2346 cache->cache_size = size;
2349 #define DEFAULT_MIGRATION_THRESHOLD 2048
2351 static int cache_create(struct cache_args *ca, struct cache **result)
2353 int r = 0;
2354 char **error = &ca->ti->error;
2355 struct cache *cache;
2356 struct dm_target *ti = ca->ti;
2357 dm_block_t origin_blocks;
2358 struct dm_cache_metadata *cmd;
2359 bool may_format = ca->features.mode == CM_WRITE;
2361 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2362 if (!cache)
2363 return -ENOMEM;
2365 cache->ti = ca->ti;
2366 ti->private = cache;
2367 ti->num_flush_bios = 2;
2368 ti->flush_supported = true;
2370 ti->num_discard_bios = 1;
2371 ti->discards_supported = true;
2372 ti->discard_zeroes_data_unsupported = true;
2373 ti->split_discard_bios = false;
2375 cache->features = ca->features;
2376 ti->per_bio_data_size = get_per_bio_data_size(cache);
2378 cache->callbacks.congested_fn = cache_is_congested;
2379 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2381 cache->metadata_dev = ca->metadata_dev;
2382 cache->origin_dev = ca->origin_dev;
2383 cache->cache_dev = ca->cache_dev;
2385 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2387 /* FIXME: factor out this whole section */
2388 origin_blocks = cache->origin_sectors = ca->origin_sectors;
2389 origin_blocks = block_div(origin_blocks, ca->block_size);
2390 cache->origin_blocks = to_oblock(origin_blocks);
2392 cache->sectors_per_block = ca->block_size;
2393 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2394 r = -EINVAL;
2395 goto bad;
2398 if (ca->block_size & (ca->block_size - 1)) {
2399 dm_block_t cache_size = ca->cache_sectors;
2401 cache->sectors_per_block_shift = -1;
2402 cache_size = block_div(cache_size, ca->block_size);
2403 set_cache_size(cache, to_cblock(cache_size));
2404 } else {
2405 cache->sectors_per_block_shift = __ffs(ca->block_size);
2406 set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
2409 r = create_cache_policy(cache, ca, error);
2410 if (r)
2411 goto bad;
2413 cache->policy_nr_args = ca->policy_argc;
2414 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2416 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2417 if (r) {
2418 *error = "Error setting cache policy's config values";
2419 goto bad;
2422 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2423 ca->block_size, may_format,
2424 dm_cache_policy_get_hint_size(cache->policy));
2425 if (IS_ERR(cmd)) {
2426 *error = "Error creating metadata object";
2427 r = PTR_ERR(cmd);
2428 goto bad;
2430 cache->cmd = cmd;
2432 if (passthrough_mode(&cache->features)) {
2433 bool all_clean;
2435 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2436 if (r) {
2437 *error = "dm_cache_metadata_all_clean() failed";
2438 goto bad;
2441 if (!all_clean) {
2442 *error = "Cannot enter passthrough mode unless all blocks are clean";
2443 r = -EINVAL;
2444 goto bad;
2448 spin_lock_init(&cache->lock);
2449 bio_list_init(&cache->deferred_bios);
2450 bio_list_init(&cache->deferred_flush_bios);
2451 bio_list_init(&cache->deferred_writethrough_bios);
2452 INIT_LIST_HEAD(&cache->quiesced_migrations);
2453 INIT_LIST_HEAD(&cache->completed_migrations);
2454 INIT_LIST_HEAD(&cache->need_commit_migrations);
2455 atomic_set(&cache->nr_allocated_migrations, 0);
2456 atomic_set(&cache->nr_io_migrations, 0);
2457 init_waitqueue_head(&cache->migration_wait);
2459 init_waitqueue_head(&cache->quiescing_wait);
2460 atomic_set(&cache->quiescing, 0);
2461 atomic_set(&cache->quiescing_ack, 0);
2463 r = -ENOMEM;
2464 atomic_set(&cache->nr_dirty, 0);
2465 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2466 if (!cache->dirty_bitset) {
2467 *error = "could not allocate dirty bitset";
2468 goto bad;
2470 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2472 cache->discard_block_size =
2473 calculate_discard_block_size(cache->sectors_per_block,
2474 cache->origin_sectors);
2475 cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
2476 cache->discard_block_size));
2477 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2478 if (!cache->discard_bitset) {
2479 *error = "could not allocate discard bitset";
2480 goto bad;
2482 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2484 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2485 if (IS_ERR(cache->copier)) {
2486 *error = "could not create kcopyd client";
2487 r = PTR_ERR(cache->copier);
2488 goto bad;
2491 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2492 if (!cache->wq) {
2493 *error = "could not create workqueue for metadata object";
2494 goto bad;
2496 INIT_WORK(&cache->worker, do_worker);
2497 INIT_DELAYED_WORK(&cache->waker, do_waker);
2498 cache->last_commit_jiffies = jiffies;
2500 cache->prison = dm_bio_prison_create();
2501 if (!cache->prison) {
2502 *error = "could not create bio prison";
2503 goto bad;
2506 cache->all_io_ds = dm_deferred_set_create();
2507 if (!cache->all_io_ds) {
2508 *error = "could not create all_io deferred set";
2509 goto bad;
2512 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2513 migration_cache);
2514 if (!cache->migration_pool) {
2515 *error = "Error creating cache's migration mempool";
2516 goto bad;
2519 cache->need_tick_bio = true;
2520 cache->sized = false;
2521 cache->invalidate = false;
2522 cache->commit_requested = false;
2523 cache->loaded_mappings = false;
2524 cache->loaded_discards = false;
2526 load_stats(cache);
2528 atomic_set(&cache->stats.demotion, 0);
2529 atomic_set(&cache->stats.promotion, 0);
2530 atomic_set(&cache->stats.copies_avoided, 0);
2531 atomic_set(&cache->stats.cache_cell_clash, 0);
2532 atomic_set(&cache->stats.commit_count, 0);
2533 atomic_set(&cache->stats.discard_count, 0);
2535 spin_lock_init(&cache->invalidation_lock);
2536 INIT_LIST_HEAD(&cache->invalidation_requests);
2538 *result = cache;
2539 return 0;
2541 bad:
2542 destroy(cache);
2543 return r;
2546 static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2548 unsigned i;
2549 const char **copy;
2551 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2552 if (!copy)
2553 return -ENOMEM;
2554 for (i = 0; i < argc; i++) {
2555 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2556 if (!copy[i]) {
2557 while (i--)
2558 kfree(copy[i]);
2559 kfree(copy);
2560 return -ENOMEM;
2564 cache->nr_ctr_args = argc;
2565 cache->ctr_args = copy;
2567 return 0;
2570 static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2572 int r = -EINVAL;
2573 struct cache_args *ca;
2574 struct cache *cache = NULL;
2576 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2577 if (!ca) {
2578 ti->error = "Error allocating memory for cache";
2579 return -ENOMEM;
2581 ca->ti = ti;
2583 r = parse_cache_args(ca, argc, argv, &ti->error);
2584 if (r)
2585 goto out;
2587 r = cache_create(ca, &cache);
2588 if (r)
2589 goto out;
2591 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2592 if (r) {
2593 destroy(cache);
2594 goto out;
2597 ti->private = cache;
2599 out:
2600 destroy_cache_args(ca);
2601 return r;
2604 static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell)
2606 int r;
2607 dm_oblock_t block = get_bio_block(cache, bio);
2608 size_t pb_data_size = get_per_bio_data_size(cache);
2609 bool can_migrate = false;
2610 bool discarded_block;
2611 struct policy_result lookup_result;
2612 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
2613 struct old_oblock_lock ool;
2615 ool.locker.fn = null_locker;
2617 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
2619 * This can only occur if the io goes to a partial block at
2620 * the end of the origin device. We don't cache these.
2621 * Just remap to the origin and carry on.
2623 remap_to_origin(cache, bio);
2624 return DM_MAPIO_REMAPPED;
2627 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2628 defer_bio(cache, bio);
2629 return DM_MAPIO_SUBMITTED;
2633 * Check to see if that block is currently migrating.
2635 *cell = alloc_prison_cell(cache);
2636 if (!*cell) {
2637 defer_bio(cache, bio);
2638 return DM_MAPIO_SUBMITTED;
2641 r = bio_detain(cache, block, bio, *cell,
2642 (cell_free_fn) free_prison_cell,
2643 cache, cell);
2644 if (r) {
2645 if (r < 0)
2646 defer_bio(cache, bio);
2648 return DM_MAPIO_SUBMITTED;
2651 discarded_block = is_discarded_oblock(cache, block);
2653 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2654 bio, &ool.locker, &lookup_result);
2655 if (r == -EWOULDBLOCK) {
2656 cell_defer(cache, *cell, true);
2657 return DM_MAPIO_SUBMITTED;
2659 } else if (r) {
2660 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
2661 cell_defer(cache, *cell, false);
2662 bio_io_error(bio);
2663 return DM_MAPIO_SUBMITTED;
2666 r = DM_MAPIO_REMAPPED;
2667 switch (lookup_result.op) {
2668 case POLICY_HIT:
2669 if (passthrough_mode(&cache->features)) {
2670 if (bio_data_dir(bio) == WRITE) {
2672 * We need to invalidate this block, so
2673 * defer for the worker thread.
2675 cell_defer(cache, *cell, true);
2676 r = DM_MAPIO_SUBMITTED;
2678 } else {
2679 inc_miss_counter(cache, bio);
2680 remap_to_origin_clear_discard(cache, bio, block);
2683 } else {
2684 inc_hit_counter(cache, bio);
2685 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2686 !is_dirty(cache, lookup_result.cblock))
2687 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2688 else
2689 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
2691 break;
2693 case POLICY_MISS:
2694 inc_miss_counter(cache, bio);
2695 if (pb->req_nr != 0) {
2697 * This is a duplicate writethrough io that is no
2698 * longer needed because the block has been demoted.
2700 bio_endio(bio, 0);
2701 cell_defer(cache, *cell, false);
2702 r = DM_MAPIO_SUBMITTED;
2704 } else
2705 remap_to_origin_clear_discard(cache, bio, block);
2707 break;
2709 default:
2710 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2711 (unsigned) lookup_result.op);
2712 cell_defer(cache, *cell, false);
2713 bio_io_error(bio);
2714 r = DM_MAPIO_SUBMITTED;
2717 return r;
2720 static int cache_map(struct dm_target *ti, struct bio *bio)
2722 int r;
2723 struct dm_bio_prison_cell *cell = NULL;
2724 struct cache *cache = ti->private;
2726 r = __cache_map(cache, bio, &cell);
2727 if (r == DM_MAPIO_REMAPPED && cell) {
2728 inc_ds(cache, bio, cell);
2729 cell_defer(cache, cell, false);
2732 return r;
2735 static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2737 struct cache *cache = ti->private;
2738 unsigned long flags;
2739 size_t pb_data_size = get_per_bio_data_size(cache);
2740 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
2742 if (pb->tick) {
2743 policy_tick(cache->policy);
2745 spin_lock_irqsave(&cache->lock, flags);
2746 cache->need_tick_bio = true;
2747 spin_unlock_irqrestore(&cache->lock, flags);
2750 check_for_quiesced_migrations(cache, pb);
2752 return 0;
2755 static int write_dirty_bitset(struct cache *cache)
2757 unsigned i, r;
2759 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2760 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2761 is_dirty(cache, to_cblock(i)));
2762 if (r)
2763 return r;
2766 return 0;
2769 static int write_discard_bitset(struct cache *cache)
2771 unsigned i, r;
2773 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2774 cache->discard_nr_blocks);
2775 if (r) {
2776 DMERR("could not resize on-disk discard bitset");
2777 return r;
2780 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2781 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2782 is_discarded(cache, to_dblock(i)));
2783 if (r)
2784 return r;
2787 return 0;
2791 * returns true on success
2793 static bool sync_metadata(struct cache *cache)
2795 int r1, r2, r3, r4;
2797 r1 = write_dirty_bitset(cache);
2798 if (r1)
2799 DMERR("could not write dirty bitset");
2801 r2 = write_discard_bitset(cache);
2802 if (r2)
2803 DMERR("could not write discard bitset");
2805 save_stats(cache);
2807 r3 = dm_cache_write_hints(cache->cmd, cache->policy);
2808 if (r3)
2809 DMERR("could not write hints");
2812 * If writing the above metadata failed, we still commit, but don't
2813 * set the clean shutdown flag. This will effectively force every
2814 * dirty bit to be set on reload.
2816 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2817 if (r4)
2818 DMERR("could not write cache metadata. Data loss may occur.");
2820 return !r1 && !r2 && !r3 && !r4;
2823 static void cache_postsuspend(struct dm_target *ti)
2825 struct cache *cache = ti->private;
2827 start_quiescing(cache);
2828 wait_for_migrations(cache);
2829 stop_worker(cache);
2830 requeue_deferred_io(cache);
2831 stop_quiescing(cache);
2833 (void) sync_metadata(cache);
2836 static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2837 bool dirty, uint32_t hint, bool hint_valid)
2839 int r;
2840 struct cache *cache = context;
2842 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2843 if (r)
2844 return r;
2846 if (dirty)
2847 set_dirty(cache, oblock, cblock);
2848 else
2849 clear_dirty(cache, oblock, cblock);
2851 return 0;
2855 * The discard block size in the on disk metadata is not
2856 * neccessarily the same as we're currently using. So we have to
2857 * be careful to only set the discarded attribute if we know it
2858 * covers a complete block of the new size.
2860 struct discard_load_info {
2861 struct cache *cache;
2864 * These blocks are sized using the on disk dblock size, rather
2865 * than the current one.
2867 dm_block_t block_size;
2868 dm_block_t discard_begin, discard_end;
2871 static void discard_load_info_init(struct cache *cache,
2872 struct discard_load_info *li)
2874 li->cache = cache;
2875 li->discard_begin = li->discard_end = 0;
2878 static void set_discard_range(struct discard_load_info *li)
2880 sector_t b, e;
2882 if (li->discard_begin == li->discard_end)
2883 return;
2886 * Convert to sectors.
2888 b = li->discard_begin * li->block_size;
2889 e = li->discard_end * li->block_size;
2892 * Then convert back to the current dblock size.
2894 b = dm_sector_div_up(b, li->cache->discard_block_size);
2895 sector_div(e, li->cache->discard_block_size);
2898 * The origin may have shrunk, so we need to check we're still in
2899 * bounds.
2901 if (e > from_dblock(li->cache->discard_nr_blocks))
2902 e = from_dblock(li->cache->discard_nr_blocks);
2904 for (; b < e; b++)
2905 set_discard(li->cache, to_dblock(b));
2908 static int load_discard(void *context, sector_t discard_block_size,
2909 dm_dblock_t dblock, bool discard)
2911 struct discard_load_info *li = context;
2913 li->block_size = discard_block_size;
2915 if (discard) {
2916 if (from_dblock(dblock) == li->discard_end)
2918 * We're already in a discard range, just extend it.
2920 li->discard_end = li->discard_end + 1ULL;
2922 else {
2924 * Emit the old range and start a new one.
2926 set_discard_range(li);
2927 li->discard_begin = from_dblock(dblock);
2928 li->discard_end = li->discard_begin + 1ULL;
2930 } else {
2931 set_discard_range(li);
2932 li->discard_begin = li->discard_end = 0;
2935 return 0;
2938 static dm_cblock_t get_cache_dev_size(struct cache *cache)
2940 sector_t size = get_dev_size(cache->cache_dev);
2941 (void) sector_div(size, cache->sectors_per_block);
2942 return to_cblock(size);
2945 static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2947 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2948 return true;
2951 * We can't drop a dirty block when shrinking the cache.
2953 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2954 new_size = to_cblock(from_cblock(new_size) + 1);
2955 if (is_dirty(cache, new_size)) {
2956 DMERR("unable to shrink cache; cache block %llu is dirty",
2957 (unsigned long long) from_cblock(new_size));
2958 return false;
2962 return true;
2965 static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2967 int r;
2969 r = dm_cache_resize(cache->cmd, new_size);
2970 if (r) {
2971 DMERR("could not resize cache metadata");
2972 return r;
2975 set_cache_size(cache, new_size);
2977 return 0;
2980 static int cache_preresume(struct dm_target *ti)
2982 int r = 0;
2983 struct cache *cache = ti->private;
2984 dm_cblock_t csize = get_cache_dev_size(cache);
2987 * Check to see if the cache has resized.
2989 if (!cache->sized) {
2990 r = resize_cache_dev(cache, csize);
2991 if (r)
2992 return r;
2994 cache->sized = true;
2996 } else if (csize != cache->cache_size) {
2997 if (!can_resize(cache, csize))
2998 return -EINVAL;
3000 r = resize_cache_dev(cache, csize);
3001 if (r)
3002 return r;
3005 if (!cache->loaded_mappings) {
3006 r = dm_cache_load_mappings(cache->cmd, cache->policy,
3007 load_mapping, cache);
3008 if (r) {
3009 DMERR("could not load cache mappings");
3010 return r;
3013 cache->loaded_mappings = true;
3016 if (!cache->loaded_discards) {
3017 struct discard_load_info li;
3020 * The discard bitset could have been resized, or the
3021 * discard block size changed. To be safe we start by
3022 * setting every dblock to not discarded.
3024 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
3026 discard_load_info_init(cache, &li);
3027 r = dm_cache_load_discards(cache->cmd, load_discard, &li);
3028 if (r) {
3029 DMERR("could not load origin discards");
3030 return r;
3032 set_discard_range(&li);
3034 cache->loaded_discards = true;
3037 return r;
3040 static void cache_resume(struct dm_target *ti)
3042 struct cache *cache = ti->private;
3044 cache->need_tick_bio = true;
3045 do_waker(&cache->waker.work);
3049 * Status format:
3051 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
3052 * <cache block size> <#used cache blocks>/<#total cache blocks>
3053 * <#read hits> <#read misses> <#write hits> <#write misses>
3054 * <#demotions> <#promotions> <#dirty>
3055 * <#features> <features>*
3056 * <#core args> <core args>
3057 * <policy name> <#policy args> <policy args>*
3059 static void cache_status(struct dm_target *ti, status_type_t type,
3060 unsigned status_flags, char *result, unsigned maxlen)
3062 int r = 0;
3063 unsigned i;
3064 ssize_t sz = 0;
3065 dm_block_t nr_free_blocks_metadata = 0;
3066 dm_block_t nr_blocks_metadata = 0;
3067 char buf[BDEVNAME_SIZE];
3068 struct cache *cache = ti->private;
3069 dm_cblock_t residency;
3071 switch (type) {
3072 case STATUSTYPE_INFO:
3073 /* Commit to ensure statistics aren't out-of-date */
3074 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
3075 r = dm_cache_commit(cache->cmd, false);
3076 if (r)
3077 DMERR("could not commit metadata for accurate status");
3080 r = dm_cache_get_free_metadata_block_count(cache->cmd,
3081 &nr_free_blocks_metadata);
3082 if (r) {
3083 DMERR("could not get metadata free block count");
3084 goto err;
3087 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
3088 if (r) {
3089 DMERR("could not get metadata device size");
3090 goto err;
3093 residency = policy_residency(cache->policy);
3095 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
3096 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
3097 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3098 (unsigned long long)nr_blocks_metadata,
3099 cache->sectors_per_block,
3100 (unsigned long long) from_cblock(residency),
3101 (unsigned long long) from_cblock(cache->cache_size),
3102 (unsigned) atomic_read(&cache->stats.read_hit),
3103 (unsigned) atomic_read(&cache->stats.read_miss),
3104 (unsigned) atomic_read(&cache->stats.write_hit),
3105 (unsigned) atomic_read(&cache->stats.write_miss),
3106 (unsigned) atomic_read(&cache->stats.demotion),
3107 (unsigned) atomic_read(&cache->stats.promotion),
3108 (unsigned long) atomic_read(&cache->nr_dirty));
3110 if (writethrough_mode(&cache->features))
3111 DMEMIT("1 writethrough ");
3113 else if (passthrough_mode(&cache->features))
3114 DMEMIT("1 passthrough ");
3116 else if (writeback_mode(&cache->features))
3117 DMEMIT("1 writeback ");
3119 else {
3120 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
3121 goto err;
3124 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
3126 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
3127 if (sz < maxlen) {
3128 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
3129 if (r)
3130 DMERR("policy_emit_config_values returned %d", r);
3133 break;
3135 case STATUSTYPE_TABLE:
3136 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
3137 DMEMIT("%s ", buf);
3138 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
3139 DMEMIT("%s ", buf);
3140 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
3141 DMEMIT("%s", buf);
3143 for (i = 0; i < cache->nr_ctr_args - 1; i++)
3144 DMEMIT(" %s", cache->ctr_args[i]);
3145 if (cache->nr_ctr_args)
3146 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
3149 return;
3151 err:
3152 DMEMIT("Error");
3156 * A cache block range can take two forms:
3158 * i) A single cblock, eg. '3456'
3159 * ii) A begin and end cblock with dots between, eg. 123-234
3161 static int parse_cblock_range(struct cache *cache, const char *str,
3162 struct cblock_range *result)
3164 char dummy;
3165 uint64_t b, e;
3166 int r;
3169 * Try and parse form (ii) first.
3171 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
3172 if (r < 0)
3173 return r;
3175 if (r == 2) {
3176 result->begin = to_cblock(b);
3177 result->end = to_cblock(e);
3178 return 0;
3182 * That didn't work, try form (i).
3184 r = sscanf(str, "%llu%c", &b, &dummy);
3185 if (r < 0)
3186 return r;
3188 if (r == 1) {
3189 result->begin = to_cblock(b);
3190 result->end = to_cblock(from_cblock(result->begin) + 1u);
3191 return 0;
3194 DMERR("invalid cblock range '%s'", str);
3195 return -EINVAL;
3198 static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
3200 uint64_t b = from_cblock(range->begin);
3201 uint64_t e = from_cblock(range->end);
3202 uint64_t n = from_cblock(cache->cache_size);
3204 if (b >= n) {
3205 DMERR("begin cblock out of range: %llu >= %llu", b, n);
3206 return -EINVAL;
3209 if (e > n) {
3210 DMERR("end cblock out of range: %llu > %llu", e, n);
3211 return -EINVAL;
3214 if (b >= e) {
3215 DMERR("invalid cblock range: %llu >= %llu", b, e);
3216 return -EINVAL;
3219 return 0;
3222 static int request_invalidation(struct cache *cache, struct cblock_range *range)
3224 struct invalidation_request req;
3226 INIT_LIST_HEAD(&req.list);
3227 req.cblocks = range;
3228 atomic_set(&req.complete, 0);
3229 req.err = 0;
3230 init_waitqueue_head(&req.result_wait);
3232 spin_lock(&cache->invalidation_lock);
3233 list_add(&req.list, &cache->invalidation_requests);
3234 spin_unlock(&cache->invalidation_lock);
3235 wake_worker(cache);
3237 wait_event(req.result_wait, atomic_read(&req.complete));
3238 return req.err;
3241 static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
3242 const char **cblock_ranges)
3244 int r = 0;
3245 unsigned i;
3246 struct cblock_range range;
3248 if (!passthrough_mode(&cache->features)) {
3249 DMERR("cache has to be in passthrough mode for invalidation");
3250 return -EPERM;
3253 for (i = 0; i < count; i++) {
3254 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3255 if (r)
3256 break;
3258 r = validate_cblock_range(cache, &range);
3259 if (r)
3260 break;
3263 * Pass begin and end origin blocks to the worker and wake it.
3265 r = request_invalidation(cache, &range);
3266 if (r)
3267 break;
3270 return r;
3274 * Supports
3275 * "<key> <value>"
3276 * and
3277 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
3279 * The key migration_threshold is supported by the cache target core.
3281 static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3283 struct cache *cache = ti->private;
3285 if (!argc)
3286 return -EINVAL;
3288 if (!strcasecmp(argv[0], "invalidate_cblocks"))
3289 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3291 if (argc != 2)
3292 return -EINVAL;
3294 return set_config_value(cache, argv[0], argv[1]);
3297 static int cache_iterate_devices(struct dm_target *ti,
3298 iterate_devices_callout_fn fn, void *data)
3300 int r = 0;
3301 struct cache *cache = ti->private;
3303 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3304 if (!r)
3305 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3307 return r;
3311 * We assume I/O is going to the origin (which is the volume
3312 * more likely to have restrictions e.g. by being striped).
3313 * (Looking up the exact location of the data would be expensive
3314 * and could always be out of date by the time the bio is submitted.)
3316 static int cache_bvec_merge(struct dm_target *ti,
3317 struct bvec_merge_data *bvm,
3318 struct bio_vec *biovec, int max_size)
3320 struct cache *cache = ti->private;
3321 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3323 if (!q->merge_bvec_fn)
3324 return max_size;
3326 bvm->bi_bdev = cache->origin_dev->bdev;
3327 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3330 static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3333 * FIXME: these limits may be incompatible with the cache device
3335 limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
3336 cache->origin_sectors);
3337 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
3340 static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3342 struct cache *cache = ti->private;
3343 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3346 * If the system-determined stacked limits are compatible with the
3347 * cache's blocksize (io_opt is a factor) do not override them.
3349 if (io_opt_sectors < cache->sectors_per_block ||
3350 do_div(io_opt_sectors, cache->sectors_per_block)) {
3351 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
3352 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3354 set_discard_limits(cache, limits);
3357 /*----------------------------------------------------------------*/
3359 static struct target_type cache_target = {
3360 .name = "cache",
3361 .version = {1, 6, 0},
3362 .module = THIS_MODULE,
3363 .ctr = cache_ctr,
3364 .dtr = cache_dtr,
3365 .map = cache_map,
3366 .end_io = cache_end_io,
3367 .postsuspend = cache_postsuspend,
3368 .preresume = cache_preresume,
3369 .resume = cache_resume,
3370 .status = cache_status,
3371 .message = cache_message,
3372 .iterate_devices = cache_iterate_devices,
3373 .merge = cache_bvec_merge,
3374 .io_hints = cache_io_hints,
3377 static int __init dm_cache_init(void)
3379 int r;
3381 r = dm_register_target(&cache_target);
3382 if (r) {
3383 DMERR("cache target registration failed: %d", r);
3384 return r;
3387 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3388 if (!migration_cache) {
3389 dm_unregister_target(&cache_target);
3390 return -ENOMEM;
3393 return 0;
3396 static void __exit dm_cache_exit(void)
3398 dm_unregister_target(&cache_target);
3399 kmem_cache_destroy(migration_cache);
3402 module_init(dm_cache_init);
3403 module_exit(dm_cache_exit);
3405 MODULE_DESCRIPTION(DM_NAME " cache target");
3406 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3407 MODULE_LICENSE("GPL");