mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / md / dm-cache-target.c
blob17718456587c7e9d844d7fc73a8eccbbb815ed23
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/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 /*----------------------------------------------------------------*/
28 * Glossary:
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,
35 * either direction
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);
48 return vzalloc(s);
51 static void clear_bitset(void *bitset, unsigned nr_entries)
53 size_t s = bitset_size_in_bytes(nr_entries);
54 memset(bitset, 0, s);
57 static void free_bitset(unsigned long *bits)
59 vfree(bits);
62 /*----------------------------------------------------------------*/
64 #define PRISON_CELLS 1024
65 #define MIGRATION_POOL_SIZE 128
66 #define COMMIT_PERIOD HZ
67 #define MIGRATION_COUNT_WINDOW 10
70 * The block size of the device holding cache data must be
71 * between 32KB and 1GB.
73 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
74 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
77 * FIXME: the cache is read/write for the time being.
79 enum cache_mode {
80 CM_WRITE, /* metadata may be changed */
81 CM_READ_ONLY, /* metadata may not be changed */
84 struct cache_features {
85 enum cache_mode mode;
86 bool write_through:1;
89 struct cache_stats {
90 atomic_t read_hit;
91 atomic_t read_miss;
92 atomic_t write_hit;
93 atomic_t write_miss;
94 atomic_t demotion;
95 atomic_t promotion;
96 atomic_t copies_avoided;
97 atomic_t cache_cell_clash;
98 atomic_t commit_count;
99 atomic_t discard_count;
102 struct cache {
103 struct dm_target *ti;
104 struct dm_target_callbacks callbacks;
106 struct dm_cache_metadata *cmd;
109 * Metadata is written to this device.
111 struct dm_dev *metadata_dev;
114 * The slower of the two data devices. Typically a spindle.
116 struct dm_dev *origin_dev;
119 * The faster of the two data devices. Typically an SSD.
121 struct dm_dev *cache_dev;
124 * Size of the origin device in _complete_ blocks and native sectors.
126 dm_oblock_t origin_blocks;
127 sector_t origin_sectors;
130 * Size of the cache device in blocks.
132 dm_cblock_t cache_size;
135 * Fields for converting from sectors to blocks.
137 uint32_t sectors_per_block;
138 int sectors_per_block_shift;
140 spinlock_t lock;
141 struct bio_list deferred_bios;
142 struct bio_list deferred_flush_bios;
143 struct bio_list deferred_writethrough_bios;
144 struct list_head quiesced_migrations;
145 struct list_head completed_migrations;
146 struct list_head need_commit_migrations;
147 sector_t migration_threshold;
148 wait_queue_head_t migration_wait;
149 atomic_t nr_allocated_migrations;
152 * The number of in flight migrations that are performing
153 * background io. eg, promotion, writeback.
155 atomic_t nr_io_migrations;
157 wait_queue_head_t quiescing_wait;
158 atomic_t quiescing_ack;
161 * cache_size entries, dirty if set
163 atomic_t nr_dirty;
164 unsigned long *dirty_bitset;
167 * origin_blocks entries, discarded if set.
169 dm_dblock_t discard_nr_blocks;
170 unsigned long *discard_bitset;
171 uint32_t discard_block_size;
174 * Rather than reconstructing the table line for the status we just
175 * save it and regurgitate.
177 unsigned nr_ctr_args;
178 const char **ctr_args;
180 struct dm_kcopyd_client *copier;
181 struct workqueue_struct *wq;
182 struct work_struct worker;
184 struct delayed_work waker;
185 unsigned long last_commit_jiffies;
187 struct dm_bio_prison *prison;
188 struct dm_deferred_set *all_io_ds;
190 mempool_t *migration_pool;
192 struct dm_cache_policy *policy;
193 unsigned policy_nr_args;
195 bool need_tick_bio:1;
196 bool sized:1;
197 bool quiescing:1;
198 bool commit_requested:1;
199 bool loaded_mappings:1;
200 bool loaded_discards:1;
203 * Cache features such as write-through.
205 struct cache_features features;
207 struct cache_stats stats;
210 struct per_bio_data {
211 bool tick:1;
212 unsigned req_nr:2;
213 struct dm_deferred_entry *all_io_entry;
216 * writethrough fields. These MUST remain at the end of this
217 * structure and the 'cache' member must be the first as it
218 * is used to determine the offset of the writethrough fields.
220 struct cache *cache;
221 dm_cblock_t cblock;
222 bio_end_io_t *saved_bi_end_io;
223 struct dm_bio_details bio_details;
226 struct dm_cache_migration {
227 struct list_head list;
228 struct cache *cache;
230 unsigned long start_jiffies;
231 dm_oblock_t old_oblock;
232 dm_oblock_t new_oblock;
233 dm_cblock_t cblock;
235 bool err:1;
236 bool writeback:1;
237 bool demote:1;
238 bool promote:1;
240 struct dm_bio_prison_cell *old_ocell;
241 struct dm_bio_prison_cell *new_ocell;
245 * Processing a bio in the worker thread may require these memory
246 * allocations. We prealloc to avoid deadlocks (the same worker thread
247 * frees them back to the mempool).
249 struct prealloc {
250 struct dm_cache_migration *mg;
251 struct dm_bio_prison_cell *cell1;
252 struct dm_bio_prison_cell *cell2;
255 static void wake_worker(struct cache *cache)
257 queue_work(cache->wq, &cache->worker);
260 /*----------------------------------------------------------------*/
262 static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
264 /* FIXME: change to use a local slab. */
265 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
268 static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
270 dm_bio_prison_free_cell(cache->prison, cell);
273 static struct dm_cache_migration *alloc_migration(struct cache *cache)
275 struct dm_cache_migration *mg;
277 mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
278 if (mg) {
279 mg->cache = cache;
280 atomic_inc(&mg->cache->nr_allocated_migrations);
283 return mg;
286 static void free_migration(struct dm_cache_migration *mg)
288 if (atomic_dec_and_test(&mg->cache->nr_allocated_migrations))
289 wake_up(&mg->cache->migration_wait);
291 mempool_free(mg, mg->cache->migration_pool);
294 static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
296 if (!p->mg) {
297 p->mg = alloc_migration(cache);
298 if (!p->mg)
299 return -ENOMEM;
302 if (!p->cell1) {
303 p->cell1 = alloc_prison_cell(cache);
304 if (!p->cell1)
305 return -ENOMEM;
308 if (!p->cell2) {
309 p->cell2 = alloc_prison_cell(cache);
310 if (!p->cell2)
311 return -ENOMEM;
314 return 0;
317 static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
319 if (p->cell2)
320 free_prison_cell(cache, p->cell2);
322 if (p->cell1)
323 free_prison_cell(cache, p->cell1);
325 if (p->mg)
326 free_migration(p->mg);
329 static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
331 struct dm_cache_migration *mg = p->mg;
333 BUG_ON(!mg);
334 p->mg = NULL;
336 return mg;
340 * You must have a cell within the prealloc struct to return. If not this
341 * function will BUG() rather than returning NULL.
343 static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
345 struct dm_bio_prison_cell *r = NULL;
347 if (p->cell1) {
348 r = p->cell1;
349 p->cell1 = NULL;
351 } else if (p->cell2) {
352 r = p->cell2;
353 p->cell2 = NULL;
354 } else
355 BUG();
357 return r;
361 * You can't have more than two cells in a prealloc struct. BUG() will be
362 * called if you try and overfill.
364 static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
366 if (!p->cell2)
367 p->cell2 = cell;
369 else if (!p->cell1)
370 p->cell1 = cell;
372 else
373 BUG();
376 /*----------------------------------------------------------------*/
378 static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
380 key->virtual = 0;
381 key->dev = 0;
382 key->block = from_oblock(oblock);
386 * The caller hands in a preallocated cell, and a free function for it.
387 * The cell will be freed if there's an error, or if it wasn't used because
388 * a cell with that key already exists.
390 typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
392 static int bio_detain(struct cache *cache, dm_oblock_t oblock,
393 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
394 cell_free_fn free_fn, void *free_context,
395 struct dm_bio_prison_cell **cell_result)
397 int r;
398 struct dm_cell_key key;
400 build_key(oblock, &key);
401 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
402 if (r)
403 free_fn(free_context, cell_prealloc);
405 return r;
408 static int get_cell(struct cache *cache,
409 dm_oblock_t oblock,
410 struct prealloc *structs,
411 struct dm_bio_prison_cell **cell_result)
413 int r;
414 struct dm_cell_key key;
415 struct dm_bio_prison_cell *cell_prealloc;
417 cell_prealloc = prealloc_get_cell(structs);
419 build_key(oblock, &key);
420 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
421 if (r)
422 prealloc_put_cell(structs, cell_prealloc);
424 return r;
427 /*----------------------------------------------------------------*/
429 static bool is_dirty(struct cache *cache, dm_cblock_t b)
431 return test_bit(from_cblock(b), cache->dirty_bitset);
434 static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
436 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
437 atomic_inc(&cache->nr_dirty);
438 policy_set_dirty(cache->policy, oblock);
442 static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
444 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
445 policy_clear_dirty(cache->policy, oblock);
446 if (atomic_dec_return(&cache->nr_dirty) == 0)
447 dm_table_event(cache->ti->table);
451 /*----------------------------------------------------------------*/
453 static bool block_size_is_power_of_two(struct cache *cache)
455 return cache->sectors_per_block_shift >= 0;
458 /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
459 #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
460 __always_inline
461 #endif
462 static dm_block_t block_div(dm_block_t b, uint32_t n)
464 do_div(b, n);
466 return b;
469 static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
471 uint32_t discard_blocks = cache->discard_block_size;
472 dm_block_t b = from_oblock(oblock);
474 if (!block_size_is_power_of_two(cache))
475 discard_blocks = discard_blocks / cache->sectors_per_block;
476 else
477 discard_blocks >>= cache->sectors_per_block_shift;
479 b = block_div(b, discard_blocks);
481 return to_dblock(b);
484 static void set_discard(struct cache *cache, dm_dblock_t b)
486 unsigned long flags;
488 atomic_inc(&cache->stats.discard_count);
490 spin_lock_irqsave(&cache->lock, flags);
491 set_bit(from_dblock(b), cache->discard_bitset);
492 spin_unlock_irqrestore(&cache->lock, flags);
495 static void clear_discard(struct cache *cache, dm_dblock_t b)
497 unsigned long flags;
499 spin_lock_irqsave(&cache->lock, flags);
500 clear_bit(from_dblock(b), cache->discard_bitset);
501 spin_unlock_irqrestore(&cache->lock, flags);
504 static bool is_discarded(struct cache *cache, dm_dblock_t b)
506 int r;
507 unsigned long flags;
509 spin_lock_irqsave(&cache->lock, flags);
510 r = test_bit(from_dblock(b), cache->discard_bitset);
511 spin_unlock_irqrestore(&cache->lock, flags);
513 return r;
516 static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
518 int r;
519 unsigned long flags;
521 spin_lock_irqsave(&cache->lock, flags);
522 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
523 cache->discard_bitset);
524 spin_unlock_irqrestore(&cache->lock, flags);
526 return r;
529 /*----------------------------------------------------------------*/
531 static void load_stats(struct cache *cache)
533 struct dm_cache_statistics stats;
535 dm_cache_metadata_get_stats(cache->cmd, &stats);
536 atomic_set(&cache->stats.read_hit, stats.read_hits);
537 atomic_set(&cache->stats.read_miss, stats.read_misses);
538 atomic_set(&cache->stats.write_hit, stats.write_hits);
539 atomic_set(&cache->stats.write_miss, stats.write_misses);
542 static void save_stats(struct cache *cache)
544 struct dm_cache_statistics stats;
546 stats.read_hits = atomic_read(&cache->stats.read_hit);
547 stats.read_misses = atomic_read(&cache->stats.read_miss);
548 stats.write_hits = atomic_read(&cache->stats.write_hit);
549 stats.write_misses = atomic_read(&cache->stats.write_miss);
551 dm_cache_metadata_set_stats(cache->cmd, &stats);
554 /*----------------------------------------------------------------
555 * Per bio data
556 *--------------------------------------------------------------*/
559 * If using writeback, leave out struct per_bio_data's writethrough fields.
561 #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
562 #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
564 static size_t get_per_bio_data_size(struct cache *cache)
566 return cache->features.write_through ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
569 static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
571 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
572 BUG_ON(!pb);
573 return pb;
576 static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
578 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
580 pb->tick = false;
581 pb->req_nr = dm_bio_get_target_bio_nr(bio);
582 pb->all_io_entry = NULL;
584 return pb;
587 /*----------------------------------------------------------------
588 * Remapping
589 *--------------------------------------------------------------*/
590 static void remap_to_origin(struct cache *cache, struct bio *bio)
592 bio->bi_bdev = cache->origin_dev->bdev;
595 static void remap_to_cache(struct cache *cache, struct bio *bio,
596 dm_cblock_t cblock)
598 sector_t bi_sector = bio->bi_sector;
600 bio->bi_bdev = cache->cache_dev->bdev;
601 if (!block_size_is_power_of_two(cache))
602 bio->bi_sector = (from_cblock(cblock) * cache->sectors_per_block) +
603 sector_div(bi_sector, cache->sectors_per_block);
604 else
605 bio->bi_sector = (from_cblock(cblock) << cache->sectors_per_block_shift) |
606 (bi_sector & (cache->sectors_per_block - 1));
609 static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
611 unsigned long flags;
612 size_t pb_data_size = get_per_bio_data_size(cache);
613 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
615 spin_lock_irqsave(&cache->lock, flags);
616 if (cache->need_tick_bio &&
617 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
618 pb->tick = true;
619 cache->need_tick_bio = false;
621 spin_unlock_irqrestore(&cache->lock, flags);
624 static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
625 dm_oblock_t oblock)
627 check_if_tick_bio_needed(cache, bio);
628 remap_to_origin(cache, bio);
629 if (bio_data_dir(bio) == WRITE)
630 clear_discard(cache, oblock_to_dblock(cache, oblock));
633 static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
634 dm_oblock_t oblock, dm_cblock_t cblock)
636 remap_to_cache(cache, bio, cblock);
637 if (bio_data_dir(bio) == WRITE) {
638 set_dirty(cache, oblock, cblock);
639 clear_discard(cache, oblock_to_dblock(cache, oblock));
643 static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
645 sector_t block_nr = bio->bi_sector;
647 if (!block_size_is_power_of_two(cache))
648 (void) sector_div(block_nr, cache->sectors_per_block);
649 else
650 block_nr >>= cache->sectors_per_block_shift;
652 return to_oblock(block_nr);
655 static int bio_triggers_commit(struct cache *cache, struct bio *bio)
657 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
660 static void issue(struct cache *cache, struct bio *bio)
662 unsigned long flags;
664 if (!bio_triggers_commit(cache, bio)) {
665 generic_make_request(bio);
666 return;
670 * Batch together any bios that trigger commits and then issue a
671 * single commit for them in do_worker().
673 spin_lock_irqsave(&cache->lock, flags);
674 cache->commit_requested = true;
675 bio_list_add(&cache->deferred_flush_bios, bio);
676 spin_unlock_irqrestore(&cache->lock, flags);
679 static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
681 unsigned long flags;
683 spin_lock_irqsave(&cache->lock, flags);
684 bio_list_add(&cache->deferred_writethrough_bios, bio);
685 spin_unlock_irqrestore(&cache->lock, flags);
687 wake_worker(cache);
690 static void writethrough_endio(struct bio *bio, int err)
692 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
693 bio->bi_end_io = pb->saved_bi_end_io;
695 if (err) {
696 bio_endio(bio, err);
697 return;
700 dm_bio_restore(&pb->bio_details, bio);
701 remap_to_cache(pb->cache, bio, pb->cblock);
704 * We can't issue this bio directly, since we're in interrupt
705 * context. So it gets put on a bio list for processing by the
706 * worker thread.
708 defer_writethrough_bio(pb->cache, bio);
712 * When running in writethrough mode we need to send writes to clean blocks
713 * to both the cache and origin devices. In future we'd like to clone the
714 * bio and send them in parallel, but for now we're doing them in
715 * series as this is easier.
717 static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
718 dm_oblock_t oblock, dm_cblock_t cblock)
720 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
722 pb->cache = cache;
723 pb->cblock = cblock;
724 pb->saved_bi_end_io = bio->bi_end_io;
725 dm_bio_record(&pb->bio_details, bio);
726 bio->bi_end_io = writethrough_endio;
728 remap_to_origin_clear_discard(pb->cache, bio, oblock);
731 /*----------------------------------------------------------------
732 * Migration processing
734 * Migration covers moving data from the origin device to the cache, or
735 * vice versa.
736 *--------------------------------------------------------------*/
737 static void inc_io_migrations(struct cache *cache)
739 atomic_inc(&cache->nr_io_migrations);
742 static void dec_io_migrations(struct cache *cache)
744 atomic_dec(&cache->nr_io_migrations);
747 static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
748 bool holder)
750 (holder ? dm_cell_release : dm_cell_release_no_holder)
751 (cache->prison, cell, &cache->deferred_bios);
752 free_prison_cell(cache, cell);
755 static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
756 bool holder)
758 unsigned long flags;
760 spin_lock_irqsave(&cache->lock, flags);
761 __cell_defer(cache, cell, holder);
762 spin_unlock_irqrestore(&cache->lock, flags);
764 wake_worker(cache);
767 static void free_io_migration(struct dm_cache_migration *mg)
769 dec_io_migrations(mg->cache);
770 free_migration(mg);
773 static void migration_failure(struct dm_cache_migration *mg)
775 struct cache *cache = mg->cache;
777 if (mg->writeback) {
778 DMWARN_LIMIT("writeback failed; couldn't copy block");
779 set_dirty(cache, mg->old_oblock, mg->cblock);
780 cell_defer(cache, mg->old_ocell, false);
782 } else if (mg->demote) {
783 DMWARN_LIMIT("demotion failed; couldn't copy block");
784 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
786 cell_defer(cache, mg->old_ocell, mg->promote ? 0 : 1);
787 if (mg->promote)
788 cell_defer(cache, mg->new_ocell, 1);
789 } else {
790 DMWARN_LIMIT("promotion failed; couldn't copy block");
791 policy_remove_mapping(cache->policy, mg->new_oblock);
792 cell_defer(cache, mg->new_ocell, 1);
795 free_io_migration(mg);
798 static void migration_success_pre_commit(struct dm_cache_migration *mg)
800 unsigned long flags;
801 struct cache *cache = mg->cache;
803 if (mg->writeback) {
804 clear_dirty(cache, mg->old_oblock, mg->cblock);
805 cell_defer(cache, mg->old_ocell, false);
806 free_io_migration(mg);
807 return;
809 } else if (mg->demote) {
810 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
811 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
812 policy_force_mapping(cache->policy, mg->new_oblock,
813 mg->old_oblock);
814 if (mg->promote)
815 cell_defer(cache, mg->new_ocell, true);
816 free_io_migration(mg);
817 return;
819 } else {
820 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
821 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
822 policy_remove_mapping(cache->policy, mg->new_oblock);
823 free_io_migration(mg);
824 return;
828 spin_lock_irqsave(&cache->lock, flags);
829 list_add_tail(&mg->list, &cache->need_commit_migrations);
830 cache->commit_requested = true;
831 spin_unlock_irqrestore(&cache->lock, flags);
834 static void migration_success_post_commit(struct dm_cache_migration *mg)
836 unsigned long flags;
837 struct cache *cache = mg->cache;
839 if (mg->writeback) {
840 DMWARN("writeback unexpectedly triggered commit");
841 return;
843 } else if (mg->demote) {
844 cell_defer(cache, mg->old_ocell, mg->promote ? 0 : 1);
846 if (mg->promote) {
847 mg->demote = false;
849 spin_lock_irqsave(&cache->lock, flags);
850 list_add_tail(&mg->list, &cache->quiesced_migrations);
851 spin_unlock_irqrestore(&cache->lock, flags);
853 } else
854 free_io_migration(mg);
856 } else {
857 clear_dirty(cache, mg->new_oblock, mg->cblock);
858 cell_defer(cache, mg->new_ocell, true);
859 free_io_migration(mg);
863 static void copy_complete(int read_err, unsigned long write_err, void *context)
865 unsigned long flags;
866 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
867 struct cache *cache = mg->cache;
869 if (read_err || write_err)
870 mg->err = true;
872 spin_lock_irqsave(&cache->lock, flags);
873 list_add_tail(&mg->list, &cache->completed_migrations);
874 spin_unlock_irqrestore(&cache->lock, flags);
876 wake_worker(cache);
879 static void issue_copy_real(struct dm_cache_migration *mg)
881 int r;
882 struct dm_io_region o_region, c_region;
883 struct cache *cache = mg->cache;
884 sector_t cblock = from_cblock(mg->cblock);
886 o_region.bdev = cache->origin_dev->bdev;
887 o_region.count = cache->sectors_per_block;
889 c_region.bdev = cache->cache_dev->bdev;
890 c_region.sector = cblock * cache->sectors_per_block;
891 c_region.count = cache->sectors_per_block;
893 if (mg->writeback || mg->demote) {
894 /* demote */
895 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
896 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
897 } else {
898 /* promote */
899 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
900 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
903 if (r < 0)
904 migration_failure(mg);
907 static void avoid_copy(struct dm_cache_migration *mg)
909 atomic_inc(&mg->cache->stats.copies_avoided);
910 migration_success_pre_commit(mg);
913 static void issue_copy(struct dm_cache_migration *mg)
915 bool avoid;
916 struct cache *cache = mg->cache;
918 if (mg->writeback || mg->demote)
919 avoid = !is_dirty(cache, mg->cblock) ||
920 is_discarded_oblock(cache, mg->old_oblock);
921 else
922 avoid = is_discarded_oblock(cache, mg->new_oblock);
924 avoid ? avoid_copy(mg) : issue_copy_real(mg);
927 static void complete_migration(struct dm_cache_migration *mg)
929 if (mg->err)
930 migration_failure(mg);
931 else
932 migration_success_pre_commit(mg);
935 static void process_migrations(struct cache *cache, struct list_head *head,
936 void (*fn)(struct dm_cache_migration *))
938 unsigned long flags;
939 struct list_head list;
940 struct dm_cache_migration *mg, *tmp;
942 INIT_LIST_HEAD(&list);
943 spin_lock_irqsave(&cache->lock, flags);
944 list_splice_init(head, &list);
945 spin_unlock_irqrestore(&cache->lock, flags);
947 list_for_each_entry_safe(mg, tmp, &list, list)
948 fn(mg);
951 static void __queue_quiesced_migration(struct dm_cache_migration *mg)
953 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
956 static void queue_quiesced_migration(struct dm_cache_migration *mg)
958 unsigned long flags;
959 struct cache *cache = mg->cache;
961 spin_lock_irqsave(&cache->lock, flags);
962 __queue_quiesced_migration(mg);
963 spin_unlock_irqrestore(&cache->lock, flags);
965 wake_worker(cache);
968 static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
970 unsigned long flags;
971 struct dm_cache_migration *mg, *tmp;
973 spin_lock_irqsave(&cache->lock, flags);
974 list_for_each_entry_safe(mg, tmp, work, list)
975 __queue_quiesced_migration(mg);
976 spin_unlock_irqrestore(&cache->lock, flags);
978 wake_worker(cache);
981 static void check_for_quiesced_migrations(struct cache *cache,
982 struct per_bio_data *pb)
984 struct list_head work;
986 if (!pb->all_io_entry)
987 return;
989 INIT_LIST_HEAD(&work);
990 if (pb->all_io_entry)
991 dm_deferred_entry_dec(pb->all_io_entry, &work);
993 if (!list_empty(&work))
994 queue_quiesced_migrations(cache, &work);
997 static void quiesce_migration(struct dm_cache_migration *mg)
999 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1000 queue_quiesced_migration(mg);
1003 static void promote(struct cache *cache, struct prealloc *structs,
1004 dm_oblock_t oblock, dm_cblock_t cblock,
1005 struct dm_bio_prison_cell *cell)
1007 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1009 mg->err = false;
1010 mg->writeback = false;
1011 mg->demote = false;
1012 mg->promote = true;
1013 mg->cache = cache;
1014 mg->new_oblock = oblock;
1015 mg->cblock = cblock;
1016 mg->old_ocell = NULL;
1017 mg->new_ocell = cell;
1018 mg->start_jiffies = jiffies;
1020 inc_io_migrations(cache);
1021 quiesce_migration(mg);
1024 static void writeback(struct cache *cache, struct prealloc *structs,
1025 dm_oblock_t oblock, dm_cblock_t cblock,
1026 struct dm_bio_prison_cell *cell)
1028 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1030 mg->err = false;
1031 mg->writeback = true;
1032 mg->demote = false;
1033 mg->promote = false;
1034 mg->cache = cache;
1035 mg->old_oblock = oblock;
1036 mg->cblock = cblock;
1037 mg->old_ocell = cell;
1038 mg->new_ocell = NULL;
1039 mg->start_jiffies = jiffies;
1041 inc_io_migrations(cache);
1042 quiesce_migration(mg);
1045 static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1046 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1047 dm_cblock_t cblock,
1048 struct dm_bio_prison_cell *old_ocell,
1049 struct dm_bio_prison_cell *new_ocell)
1051 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1053 mg->err = false;
1054 mg->writeback = false;
1055 mg->demote = true;
1056 mg->promote = true;
1057 mg->cache = cache;
1058 mg->old_oblock = old_oblock;
1059 mg->new_oblock = new_oblock;
1060 mg->cblock = cblock;
1061 mg->old_ocell = old_ocell;
1062 mg->new_ocell = new_ocell;
1063 mg->start_jiffies = jiffies;
1065 inc_io_migrations(cache);
1066 quiesce_migration(mg);
1069 /*----------------------------------------------------------------
1070 * bio processing
1071 *--------------------------------------------------------------*/
1072 static void defer_bio(struct cache *cache, struct bio *bio)
1074 unsigned long flags;
1076 spin_lock_irqsave(&cache->lock, flags);
1077 bio_list_add(&cache->deferred_bios, bio);
1078 spin_unlock_irqrestore(&cache->lock, flags);
1080 wake_worker(cache);
1083 static void process_flush_bio(struct cache *cache, struct bio *bio)
1085 size_t pb_data_size = get_per_bio_data_size(cache);
1086 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1088 BUG_ON(bio->bi_size);
1089 if (!pb->req_nr)
1090 remap_to_origin(cache, bio);
1091 else
1092 remap_to_cache(cache, bio, 0);
1094 issue(cache, bio);
1098 * People generally discard large parts of a device, eg, the whole device
1099 * when formatting. Splitting these large discards up into cache block
1100 * sized ios and then quiescing (always neccessary for discard) takes too
1101 * long.
1103 * We keep it simple, and allow any size of discard to come in, and just
1104 * mark off blocks on the discard bitset. No passdown occurs!
1106 * To implement passdown we need to change the bio_prison such that a cell
1107 * can have a key that spans many blocks.
1109 static void process_discard_bio(struct cache *cache, struct bio *bio)
1111 dm_block_t start_block = dm_sector_div_up(bio->bi_sector,
1112 cache->discard_block_size);
1113 dm_block_t end_block = bio->bi_sector + bio_sectors(bio);
1114 dm_block_t b;
1116 end_block = block_div(end_block, cache->discard_block_size);
1118 for (b = start_block; b < end_block; b++)
1119 set_discard(cache, to_dblock(b));
1121 bio_endio(bio, 0);
1124 static bool spare_migration_bandwidth(struct cache *cache)
1126 sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
1127 cache->sectors_per_block;
1128 return current_volume < cache->migration_threshold;
1131 static bool is_writethrough_io(struct cache *cache, struct bio *bio,
1132 dm_cblock_t cblock)
1134 return bio_data_dir(bio) == WRITE &&
1135 cache->features.write_through && !is_dirty(cache, cblock);
1138 static void inc_hit_counter(struct cache *cache, struct bio *bio)
1140 atomic_inc(bio_data_dir(bio) == READ ?
1141 &cache->stats.read_hit : &cache->stats.write_hit);
1144 static void inc_miss_counter(struct cache *cache, struct bio *bio)
1146 atomic_inc(bio_data_dir(bio) == READ ?
1147 &cache->stats.read_miss : &cache->stats.write_miss);
1150 static void process_bio(struct cache *cache, struct prealloc *structs,
1151 struct bio *bio)
1153 int r;
1154 bool release_cell = true;
1155 dm_oblock_t block = get_bio_block(cache, bio);
1156 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1157 struct policy_result lookup_result;
1158 size_t pb_data_size = get_per_bio_data_size(cache);
1159 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1160 bool discarded_block = is_discarded_oblock(cache, block);
1161 bool can_migrate = discarded_block || spare_migration_bandwidth(cache);
1164 * Check to see if that block is currently migrating.
1166 cell_prealloc = prealloc_get_cell(structs);
1167 r = bio_detain(cache, block, bio, cell_prealloc,
1168 (cell_free_fn) prealloc_put_cell,
1169 structs, &new_ocell);
1170 if (r > 0)
1171 return;
1173 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1174 bio, &lookup_result);
1176 if (r == -EWOULDBLOCK)
1177 /* migration has been denied */
1178 lookup_result.op = POLICY_MISS;
1180 switch (lookup_result.op) {
1181 case POLICY_HIT:
1182 inc_hit_counter(cache, bio);
1183 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1185 if (is_writethrough_io(cache, bio, lookup_result.cblock))
1186 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
1187 else
1188 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1190 issue(cache, bio);
1191 break;
1193 case POLICY_MISS:
1194 inc_miss_counter(cache, bio);
1195 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1196 remap_to_origin_clear_discard(cache, bio, block);
1197 issue(cache, bio);
1198 break;
1200 case POLICY_NEW:
1201 atomic_inc(&cache->stats.promotion);
1202 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1203 release_cell = false;
1204 break;
1206 case POLICY_REPLACE:
1207 cell_prealloc = prealloc_get_cell(structs);
1208 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1209 (cell_free_fn) prealloc_put_cell,
1210 structs, &old_ocell);
1211 if (r > 0) {
1213 * We have to be careful to avoid lock inversion of
1214 * the cells. So we back off, and wait for the
1215 * old_ocell to become free.
1217 policy_force_mapping(cache->policy, block,
1218 lookup_result.old_oblock);
1219 atomic_inc(&cache->stats.cache_cell_clash);
1220 break;
1222 atomic_inc(&cache->stats.demotion);
1223 atomic_inc(&cache->stats.promotion);
1225 demote_then_promote(cache, structs, lookup_result.old_oblock,
1226 block, lookup_result.cblock,
1227 old_ocell, new_ocell);
1228 release_cell = false;
1229 break;
1231 default:
1232 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1233 (unsigned) lookup_result.op);
1234 bio_io_error(bio);
1237 if (release_cell)
1238 cell_defer(cache, new_ocell, false);
1241 static int need_commit_due_to_time(struct cache *cache)
1243 return jiffies < cache->last_commit_jiffies ||
1244 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1247 static int commit_if_needed(struct cache *cache)
1249 if (dm_cache_changed_this_transaction(cache->cmd) &&
1250 (cache->commit_requested || need_commit_due_to_time(cache))) {
1251 atomic_inc(&cache->stats.commit_count);
1252 cache->last_commit_jiffies = jiffies;
1253 cache->commit_requested = false;
1254 return dm_cache_commit(cache->cmd, false);
1257 return 0;
1260 static void process_deferred_bios(struct cache *cache)
1262 unsigned long flags;
1263 struct bio_list bios;
1264 struct bio *bio;
1265 struct prealloc structs;
1267 memset(&structs, 0, sizeof(structs));
1268 bio_list_init(&bios);
1270 spin_lock_irqsave(&cache->lock, flags);
1271 bio_list_merge(&bios, &cache->deferred_bios);
1272 bio_list_init(&cache->deferred_bios);
1273 spin_unlock_irqrestore(&cache->lock, flags);
1275 while (!bio_list_empty(&bios)) {
1277 * If we've got no free migration structs, and processing
1278 * this bio might require one, we pause until there are some
1279 * prepared mappings to process.
1281 if (prealloc_data_structs(cache, &structs)) {
1282 spin_lock_irqsave(&cache->lock, flags);
1283 bio_list_merge(&cache->deferred_bios, &bios);
1284 spin_unlock_irqrestore(&cache->lock, flags);
1285 break;
1288 bio = bio_list_pop(&bios);
1290 if (bio->bi_rw & REQ_FLUSH)
1291 process_flush_bio(cache, bio);
1292 else if (bio->bi_rw & REQ_DISCARD)
1293 process_discard_bio(cache, bio);
1294 else
1295 process_bio(cache, &structs, bio);
1298 prealloc_free_structs(cache, &structs);
1301 static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1303 unsigned long flags;
1304 struct bio_list bios;
1305 struct bio *bio;
1307 bio_list_init(&bios);
1309 spin_lock_irqsave(&cache->lock, flags);
1310 bio_list_merge(&bios, &cache->deferred_flush_bios);
1311 bio_list_init(&cache->deferred_flush_bios);
1312 spin_unlock_irqrestore(&cache->lock, flags);
1314 while ((bio = bio_list_pop(&bios)))
1315 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1318 static void process_deferred_writethrough_bios(struct cache *cache)
1320 unsigned long flags;
1321 struct bio_list bios;
1322 struct bio *bio;
1324 bio_list_init(&bios);
1326 spin_lock_irqsave(&cache->lock, flags);
1327 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1328 bio_list_init(&cache->deferred_writethrough_bios);
1329 spin_unlock_irqrestore(&cache->lock, flags);
1331 while ((bio = bio_list_pop(&bios)))
1332 generic_make_request(bio);
1335 static void writeback_some_dirty_blocks(struct cache *cache)
1337 int r = 0;
1338 dm_oblock_t oblock;
1339 dm_cblock_t cblock;
1340 struct prealloc structs;
1341 struct dm_bio_prison_cell *old_ocell;
1343 memset(&structs, 0, sizeof(structs));
1345 while (spare_migration_bandwidth(cache)) {
1346 if (prealloc_data_structs(cache, &structs))
1347 break;
1349 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1350 if (r)
1351 break;
1353 r = get_cell(cache, oblock, &structs, &old_ocell);
1354 if (r) {
1355 policy_set_dirty(cache->policy, oblock);
1356 break;
1359 writeback(cache, &structs, oblock, cblock, old_ocell);
1362 prealloc_free_structs(cache, &structs);
1365 /*----------------------------------------------------------------
1366 * Main worker loop
1367 *--------------------------------------------------------------*/
1368 static bool is_quiescing(struct cache *cache)
1370 int r;
1371 unsigned long flags;
1373 spin_lock_irqsave(&cache->lock, flags);
1374 r = cache->quiescing;
1375 spin_unlock_irqrestore(&cache->lock, flags);
1377 return r;
1380 static void ack_quiescing(struct cache *cache)
1382 if (is_quiescing(cache)) {
1383 atomic_inc(&cache->quiescing_ack);
1384 wake_up(&cache->quiescing_wait);
1388 static void wait_for_quiescing_ack(struct cache *cache)
1390 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1393 static void start_quiescing(struct cache *cache)
1395 unsigned long flags;
1397 spin_lock_irqsave(&cache->lock, flags);
1398 cache->quiescing = true;
1399 spin_unlock_irqrestore(&cache->lock, flags);
1401 wait_for_quiescing_ack(cache);
1404 static void stop_quiescing(struct cache *cache)
1406 unsigned long flags;
1408 spin_lock_irqsave(&cache->lock, flags);
1409 cache->quiescing = false;
1410 spin_unlock_irqrestore(&cache->lock, flags);
1412 atomic_set(&cache->quiescing_ack, 0);
1415 static void wait_for_migrations(struct cache *cache)
1417 wait_event(cache->migration_wait, !atomic_read(&cache->nr_allocated_migrations));
1420 static void stop_worker(struct cache *cache)
1422 cancel_delayed_work(&cache->waker);
1423 flush_workqueue(cache->wq);
1426 static void requeue_deferred_io(struct cache *cache)
1428 struct bio *bio;
1429 struct bio_list bios;
1431 bio_list_init(&bios);
1432 bio_list_merge(&bios, &cache->deferred_bios);
1433 bio_list_init(&cache->deferred_bios);
1435 while ((bio = bio_list_pop(&bios)))
1436 bio_endio(bio, DM_ENDIO_REQUEUE);
1439 static int more_work(struct cache *cache)
1441 if (is_quiescing(cache))
1442 return !list_empty(&cache->quiesced_migrations) ||
1443 !list_empty(&cache->completed_migrations) ||
1444 !list_empty(&cache->need_commit_migrations);
1445 else
1446 return !bio_list_empty(&cache->deferred_bios) ||
1447 !bio_list_empty(&cache->deferred_flush_bios) ||
1448 !bio_list_empty(&cache->deferred_writethrough_bios) ||
1449 !list_empty(&cache->quiesced_migrations) ||
1450 !list_empty(&cache->completed_migrations) ||
1451 !list_empty(&cache->need_commit_migrations);
1454 static void do_worker(struct work_struct *ws)
1456 struct cache *cache = container_of(ws, struct cache, worker);
1458 do {
1459 if (!is_quiescing(cache)) {
1460 writeback_some_dirty_blocks(cache);
1461 process_deferred_writethrough_bios(cache);
1462 process_deferred_bios(cache);
1465 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1466 process_migrations(cache, &cache->completed_migrations, complete_migration);
1468 if (commit_if_needed(cache)) {
1469 process_deferred_flush_bios(cache, false);
1472 * FIXME: rollback metadata or just go into a
1473 * failure mode and error everything
1475 } else {
1476 process_deferred_flush_bios(cache, true);
1477 process_migrations(cache, &cache->need_commit_migrations,
1478 migration_success_post_commit);
1481 ack_quiescing(cache);
1483 } while (more_work(cache));
1487 * We want to commit periodically so that not too much
1488 * unwritten metadata builds up.
1490 static void do_waker(struct work_struct *ws)
1492 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
1493 policy_tick(cache->policy);
1494 wake_worker(cache);
1495 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1498 /*----------------------------------------------------------------*/
1500 static int is_congested(struct dm_dev *dev, int bdi_bits)
1502 struct request_queue *q = bdev_get_queue(dev->bdev);
1503 return bdi_congested(&q->backing_dev_info, bdi_bits);
1506 static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1508 struct cache *cache = container_of(cb, struct cache, callbacks);
1510 return is_congested(cache->origin_dev, bdi_bits) ||
1511 is_congested(cache->cache_dev, bdi_bits);
1514 /*----------------------------------------------------------------
1515 * Target methods
1516 *--------------------------------------------------------------*/
1519 * This function gets called on the error paths of the constructor, so we
1520 * have to cope with a partially initialised struct.
1522 static void destroy(struct cache *cache)
1524 unsigned i;
1526 if (cache->migration_pool)
1527 mempool_destroy(cache->migration_pool);
1529 if (cache->all_io_ds)
1530 dm_deferred_set_destroy(cache->all_io_ds);
1532 if (cache->prison)
1533 dm_bio_prison_destroy(cache->prison);
1535 if (cache->wq)
1536 destroy_workqueue(cache->wq);
1538 if (cache->dirty_bitset)
1539 free_bitset(cache->dirty_bitset);
1541 if (cache->discard_bitset)
1542 free_bitset(cache->discard_bitset);
1544 if (cache->copier)
1545 dm_kcopyd_client_destroy(cache->copier);
1547 if (cache->cmd)
1548 dm_cache_metadata_close(cache->cmd);
1550 if (cache->metadata_dev)
1551 dm_put_device(cache->ti, cache->metadata_dev);
1553 if (cache->origin_dev)
1554 dm_put_device(cache->ti, cache->origin_dev);
1556 if (cache->cache_dev)
1557 dm_put_device(cache->ti, cache->cache_dev);
1559 if (cache->policy)
1560 dm_cache_policy_destroy(cache->policy);
1562 for (i = 0; i < cache->nr_ctr_args ; i++)
1563 kfree(cache->ctr_args[i]);
1564 kfree(cache->ctr_args);
1566 kfree(cache);
1569 static void cache_dtr(struct dm_target *ti)
1571 struct cache *cache = ti->private;
1573 destroy(cache);
1576 static sector_t get_dev_size(struct dm_dev *dev)
1578 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1581 /*----------------------------------------------------------------*/
1584 * Construct a cache device mapping.
1586 * cache <metadata dev> <cache dev> <origin dev> <block size>
1587 * <#feature args> [<feature arg>]*
1588 * <policy> <#policy args> [<policy arg>]*
1590 * metadata dev : fast device holding the persistent metadata
1591 * cache dev : fast device holding cached data blocks
1592 * origin dev : slow device holding original data blocks
1593 * block size : cache unit size in sectors
1595 * #feature args : number of feature arguments passed
1596 * feature args : writethrough. (The default is writeback.)
1598 * policy : the replacement policy to use
1599 * #policy args : an even number of policy arguments corresponding
1600 * to key/value pairs passed to the policy
1601 * policy args : key/value pairs passed to the policy
1602 * E.g. 'sequential_threshold 1024'
1603 * See cache-policies.txt for details.
1605 * Optional feature arguments are:
1606 * writethrough : write through caching that prohibits cache block
1607 * content from being different from origin block content.
1608 * Without this argument, the default behaviour is to write
1609 * back cache block contents later for performance reasons,
1610 * so they may differ from the corresponding origin blocks.
1612 struct cache_args {
1613 struct dm_target *ti;
1615 struct dm_dev *metadata_dev;
1617 struct dm_dev *cache_dev;
1618 sector_t cache_sectors;
1620 struct dm_dev *origin_dev;
1621 sector_t origin_sectors;
1623 uint32_t block_size;
1625 const char *policy_name;
1626 int policy_argc;
1627 const char **policy_argv;
1629 struct cache_features features;
1632 static void destroy_cache_args(struct cache_args *ca)
1634 if (ca->metadata_dev)
1635 dm_put_device(ca->ti, ca->metadata_dev);
1637 if (ca->cache_dev)
1638 dm_put_device(ca->ti, ca->cache_dev);
1640 if (ca->origin_dev)
1641 dm_put_device(ca->ti, ca->origin_dev);
1643 kfree(ca);
1646 static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1648 if (!as->argc) {
1649 *error = "Insufficient args";
1650 return false;
1653 return true;
1656 static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1657 char **error)
1659 int r;
1660 sector_t metadata_dev_size;
1661 char b[BDEVNAME_SIZE];
1663 if (!at_least_one_arg(as, error))
1664 return -EINVAL;
1666 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1667 &ca->metadata_dev);
1668 if (r) {
1669 *error = "Error opening metadata device";
1670 return r;
1673 metadata_dev_size = get_dev_size(ca->metadata_dev);
1674 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1675 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1676 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1678 return 0;
1681 static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1682 char **error)
1684 int r;
1686 if (!at_least_one_arg(as, error))
1687 return -EINVAL;
1689 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1690 &ca->cache_dev);
1691 if (r) {
1692 *error = "Error opening cache device";
1693 return r;
1695 ca->cache_sectors = get_dev_size(ca->cache_dev);
1697 return 0;
1700 static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1701 char **error)
1703 int r;
1705 if (!at_least_one_arg(as, error))
1706 return -EINVAL;
1708 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1709 &ca->origin_dev);
1710 if (r) {
1711 *error = "Error opening origin device";
1712 return r;
1715 ca->origin_sectors = get_dev_size(ca->origin_dev);
1716 if (ca->ti->len > ca->origin_sectors) {
1717 *error = "Device size larger than cached device";
1718 return -EINVAL;
1721 return 0;
1724 static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1725 char **error)
1727 unsigned long block_size;
1729 if (!at_least_one_arg(as, error))
1730 return -EINVAL;
1732 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
1733 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1734 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1735 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
1736 *error = "Invalid data block size";
1737 return -EINVAL;
1740 if (block_size > ca->cache_sectors) {
1741 *error = "Data block size is larger than the cache device";
1742 return -EINVAL;
1745 ca->block_size = block_size;
1747 return 0;
1750 static void init_features(struct cache_features *cf)
1752 cf->mode = CM_WRITE;
1753 cf->write_through = false;
1756 static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
1757 char **error)
1759 static struct dm_arg _args[] = {
1760 {0, 1, "Invalid number of cache feature arguments"},
1763 int r;
1764 unsigned argc;
1765 const char *arg;
1766 struct cache_features *cf = &ca->features;
1768 init_features(cf);
1770 r = dm_read_arg_group(_args, as, &argc, error);
1771 if (r)
1772 return -EINVAL;
1774 while (argc--) {
1775 arg = dm_shift_arg(as);
1777 if (!strcasecmp(arg, "writeback"))
1778 cf->write_through = false;
1780 else if (!strcasecmp(arg, "writethrough"))
1781 cf->write_through = true;
1783 else {
1784 *error = "Unrecognised cache feature requested";
1785 return -EINVAL;
1789 return 0;
1792 static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
1793 char **error)
1795 static struct dm_arg _args[] = {
1796 {0, 1024, "Invalid number of policy arguments"},
1799 int r;
1801 if (!at_least_one_arg(as, error))
1802 return -EINVAL;
1804 ca->policy_name = dm_shift_arg(as);
1806 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
1807 if (r)
1808 return -EINVAL;
1810 ca->policy_argv = (const char **)as->argv;
1811 dm_consume_args(as, ca->policy_argc);
1813 return 0;
1816 static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
1817 char **error)
1819 int r;
1820 struct dm_arg_set as;
1822 as.argc = argc;
1823 as.argv = argv;
1825 r = parse_metadata_dev(ca, &as, error);
1826 if (r)
1827 return r;
1829 r = parse_cache_dev(ca, &as, error);
1830 if (r)
1831 return r;
1833 r = parse_origin_dev(ca, &as, error);
1834 if (r)
1835 return r;
1837 r = parse_block_size(ca, &as, error);
1838 if (r)
1839 return r;
1841 r = parse_features(ca, &as, error);
1842 if (r)
1843 return r;
1845 r = parse_policy(ca, &as, error);
1846 if (r)
1847 return r;
1849 return 0;
1852 /*----------------------------------------------------------------*/
1854 static struct kmem_cache *migration_cache;
1856 #define NOT_CORE_OPTION 1
1858 static int process_config_option(struct cache *cache, const char *key, const char *value)
1860 unsigned long tmp;
1862 if (!strcasecmp(key, "migration_threshold")) {
1863 if (kstrtoul(value, 10, &tmp))
1864 return -EINVAL;
1866 cache->migration_threshold = tmp;
1867 return 0;
1870 return NOT_CORE_OPTION;
1873 static int set_config_value(struct cache *cache, const char *key, const char *value)
1875 int r = process_config_option(cache, key, value);
1877 if (r == NOT_CORE_OPTION)
1878 r = policy_set_config_value(cache->policy, key, value);
1880 if (r)
1881 DMWARN("bad config value for %s: %s", key, value);
1883 return r;
1886 static int set_config_values(struct cache *cache, int argc, const char **argv)
1888 int r = 0;
1890 if (argc & 1) {
1891 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
1892 return -EINVAL;
1895 while (argc) {
1896 r = set_config_value(cache, argv[0], argv[1]);
1897 if (r)
1898 break;
1900 argc -= 2;
1901 argv += 2;
1904 return r;
1907 static int create_cache_policy(struct cache *cache, struct cache_args *ca,
1908 char **error)
1910 cache->policy = dm_cache_policy_create(ca->policy_name,
1911 cache->cache_size,
1912 cache->origin_sectors,
1913 cache->sectors_per_block);
1914 if (!cache->policy) {
1915 *error = "Error creating cache's policy";
1916 return -ENOMEM;
1919 return 0;
1922 #define DEFAULT_MIGRATION_THRESHOLD 2048
1924 static int cache_create(struct cache_args *ca, struct cache **result)
1926 int r = 0;
1927 char **error = &ca->ti->error;
1928 struct cache *cache;
1929 struct dm_target *ti = ca->ti;
1930 dm_block_t origin_blocks;
1931 struct dm_cache_metadata *cmd;
1932 bool may_format = ca->features.mode == CM_WRITE;
1934 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
1935 if (!cache)
1936 return -ENOMEM;
1938 cache->ti = ca->ti;
1939 ti->private = cache;
1940 ti->num_flush_bios = 2;
1941 ti->flush_supported = true;
1943 ti->num_discard_bios = 1;
1944 ti->discards_supported = true;
1945 ti->discard_zeroes_data_unsupported = true;
1946 /* Discard bios must be split on a block boundary */
1947 ti->split_discard_bios = true;
1949 cache->features = ca->features;
1950 ti->per_bio_data_size = get_per_bio_data_size(cache);
1952 cache->callbacks.congested_fn = cache_is_congested;
1953 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
1955 cache->metadata_dev = ca->metadata_dev;
1956 cache->origin_dev = ca->origin_dev;
1957 cache->cache_dev = ca->cache_dev;
1959 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
1961 /* FIXME: factor out this whole section */
1962 origin_blocks = cache->origin_sectors = ca->origin_sectors;
1963 origin_blocks = block_div(origin_blocks, ca->block_size);
1964 cache->origin_blocks = to_oblock(origin_blocks);
1966 cache->sectors_per_block = ca->block_size;
1967 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
1968 r = -EINVAL;
1969 goto bad;
1972 if (ca->block_size & (ca->block_size - 1)) {
1973 dm_block_t cache_size = ca->cache_sectors;
1975 cache->sectors_per_block_shift = -1;
1976 cache_size = block_div(cache_size, ca->block_size);
1977 cache->cache_size = to_cblock(cache_size);
1978 } else {
1979 cache->sectors_per_block_shift = __ffs(ca->block_size);
1980 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
1983 r = create_cache_policy(cache, ca, error);
1984 if (r)
1985 goto bad;
1987 cache->policy_nr_args = ca->policy_argc;
1988 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
1990 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
1991 if (r) {
1992 *error = "Error setting cache policy's config values";
1993 goto bad;
1996 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
1997 ca->block_size, may_format,
1998 dm_cache_policy_get_hint_size(cache->policy));
1999 if (IS_ERR(cmd)) {
2000 *error = "Error creating metadata object";
2001 r = PTR_ERR(cmd);
2002 goto bad;
2004 cache->cmd = cmd;
2006 spin_lock_init(&cache->lock);
2007 bio_list_init(&cache->deferred_bios);
2008 bio_list_init(&cache->deferred_flush_bios);
2009 bio_list_init(&cache->deferred_writethrough_bios);
2010 INIT_LIST_HEAD(&cache->quiesced_migrations);
2011 INIT_LIST_HEAD(&cache->completed_migrations);
2012 INIT_LIST_HEAD(&cache->need_commit_migrations);
2013 atomic_set(&cache->nr_allocated_migrations, 0);
2014 atomic_set(&cache->nr_io_migrations, 0);
2015 init_waitqueue_head(&cache->migration_wait);
2017 init_waitqueue_head(&cache->quiescing_wait);
2018 atomic_set(&cache->quiescing_ack, 0);
2020 r = -ENOMEM;
2021 atomic_set(&cache->nr_dirty, 0);
2022 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2023 if (!cache->dirty_bitset) {
2024 *error = "could not allocate dirty bitset";
2025 goto bad;
2027 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2029 cache->discard_block_size = cache->sectors_per_block;
2030 cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks);
2031 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2032 if (!cache->discard_bitset) {
2033 *error = "could not allocate discard bitset";
2034 goto bad;
2036 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2038 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2039 if (IS_ERR(cache->copier)) {
2040 *error = "could not create kcopyd client";
2041 r = PTR_ERR(cache->copier);
2042 goto bad;
2045 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2046 if (!cache->wq) {
2047 *error = "could not create workqueue for metadata object";
2048 goto bad;
2050 INIT_WORK(&cache->worker, do_worker);
2051 INIT_DELAYED_WORK(&cache->waker, do_waker);
2052 cache->last_commit_jiffies = jiffies;
2054 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2055 if (!cache->prison) {
2056 *error = "could not create bio prison";
2057 goto bad;
2060 cache->all_io_ds = dm_deferred_set_create();
2061 if (!cache->all_io_ds) {
2062 *error = "could not create all_io deferred set";
2063 goto bad;
2066 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2067 migration_cache);
2068 if (!cache->migration_pool) {
2069 *error = "Error creating cache's migration mempool";
2070 goto bad;
2073 cache->need_tick_bio = true;
2074 cache->sized = false;
2075 cache->quiescing = false;
2076 cache->commit_requested = false;
2077 cache->loaded_mappings = false;
2078 cache->loaded_discards = false;
2080 load_stats(cache);
2082 atomic_set(&cache->stats.demotion, 0);
2083 atomic_set(&cache->stats.promotion, 0);
2084 atomic_set(&cache->stats.copies_avoided, 0);
2085 atomic_set(&cache->stats.cache_cell_clash, 0);
2086 atomic_set(&cache->stats.commit_count, 0);
2087 atomic_set(&cache->stats.discard_count, 0);
2089 *result = cache;
2090 return 0;
2092 bad:
2093 destroy(cache);
2094 return r;
2097 static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2099 unsigned i;
2100 const char **copy;
2102 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2103 if (!copy)
2104 return -ENOMEM;
2105 for (i = 0; i < argc; i++) {
2106 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2107 if (!copy[i]) {
2108 while (i--)
2109 kfree(copy[i]);
2110 kfree(copy);
2111 return -ENOMEM;
2115 cache->nr_ctr_args = argc;
2116 cache->ctr_args = copy;
2118 return 0;
2121 static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2123 int r = -EINVAL;
2124 struct cache_args *ca;
2125 struct cache *cache = NULL;
2127 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2128 if (!ca) {
2129 ti->error = "Error allocating memory for cache";
2130 return -ENOMEM;
2132 ca->ti = ti;
2134 r = parse_cache_args(ca, argc, argv, &ti->error);
2135 if (r)
2136 goto out;
2138 r = cache_create(ca, &cache);
2139 if (r)
2140 goto out;
2142 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2143 if (r) {
2144 destroy(cache);
2145 goto out;
2148 ti->private = cache;
2150 out:
2151 destroy_cache_args(ca);
2152 return r;
2155 static int cache_map(struct dm_target *ti, struct bio *bio)
2157 struct cache *cache = ti->private;
2159 int r;
2160 dm_oblock_t block = get_bio_block(cache, bio);
2161 size_t pb_data_size = get_per_bio_data_size(cache);
2162 bool can_migrate = false;
2163 bool discarded_block;
2164 struct dm_bio_prison_cell *cell;
2165 struct policy_result lookup_result;
2166 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
2168 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
2170 * This can only occur if the io goes to a partial block at
2171 * the end of the origin device. We don't cache these.
2172 * Just remap to the origin and carry on.
2174 remap_to_origin(cache, bio);
2175 return DM_MAPIO_REMAPPED;
2178 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2179 defer_bio(cache, bio);
2180 return DM_MAPIO_SUBMITTED;
2184 * Check to see if that block is currently migrating.
2186 cell = alloc_prison_cell(cache);
2187 if (!cell) {
2188 defer_bio(cache, bio);
2189 return DM_MAPIO_SUBMITTED;
2192 r = bio_detain(cache, block, bio, cell,
2193 (cell_free_fn) free_prison_cell,
2194 cache, &cell);
2195 if (r) {
2196 if (r < 0)
2197 defer_bio(cache, bio);
2199 return DM_MAPIO_SUBMITTED;
2202 discarded_block = is_discarded_oblock(cache, block);
2204 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2205 bio, &lookup_result);
2206 if (r == -EWOULDBLOCK) {
2207 cell_defer(cache, cell, true);
2208 return DM_MAPIO_SUBMITTED;
2210 } else if (r) {
2211 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
2212 bio_io_error(bio);
2213 return DM_MAPIO_SUBMITTED;
2216 switch (lookup_result.op) {
2217 case POLICY_HIT:
2218 inc_hit_counter(cache, bio);
2219 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2221 if (is_writethrough_io(cache, bio, lookup_result.cblock))
2222 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2223 else
2224 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
2226 cell_defer(cache, cell, false);
2227 break;
2229 case POLICY_MISS:
2230 inc_miss_counter(cache, bio);
2231 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2233 if (pb->req_nr != 0) {
2235 * This is a duplicate writethrough io that is no
2236 * longer needed because the block has been demoted.
2238 bio_endio(bio, 0);
2239 cell_defer(cache, cell, false);
2240 return DM_MAPIO_SUBMITTED;
2241 } else {
2242 remap_to_origin_clear_discard(cache, bio, block);
2243 cell_defer(cache, cell, false);
2245 break;
2247 default:
2248 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2249 (unsigned) lookup_result.op);
2250 bio_io_error(bio);
2251 return DM_MAPIO_SUBMITTED;
2254 return DM_MAPIO_REMAPPED;
2257 static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2259 struct cache *cache = ti->private;
2260 unsigned long flags;
2261 size_t pb_data_size = get_per_bio_data_size(cache);
2262 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
2264 if (pb->tick) {
2265 policy_tick(cache->policy);
2267 spin_lock_irqsave(&cache->lock, flags);
2268 cache->need_tick_bio = true;
2269 spin_unlock_irqrestore(&cache->lock, flags);
2272 check_for_quiesced_migrations(cache, pb);
2274 return 0;
2277 static int write_dirty_bitset(struct cache *cache)
2279 unsigned i, r;
2281 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2282 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2283 is_dirty(cache, to_cblock(i)));
2284 if (r)
2285 return r;
2288 return 0;
2291 static int write_discard_bitset(struct cache *cache)
2293 unsigned i, r;
2295 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2296 cache->discard_nr_blocks);
2297 if (r) {
2298 DMERR("could not resize on-disk discard bitset");
2299 return r;
2302 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2303 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2304 is_discarded(cache, to_dblock(i)));
2305 if (r)
2306 return r;
2309 return 0;
2312 static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock,
2313 uint32_t hint)
2315 struct cache *cache = context;
2316 return dm_cache_save_hint(cache->cmd, cblock, hint);
2319 static int write_hints(struct cache *cache)
2321 int r;
2323 r = dm_cache_begin_hints(cache->cmd, cache->policy);
2324 if (r) {
2325 DMERR("dm_cache_begin_hints failed");
2326 return r;
2329 r = policy_walk_mappings(cache->policy, save_hint, cache);
2330 if (r)
2331 DMERR("policy_walk_mappings failed");
2333 return r;
2337 * returns true on success
2339 static bool sync_metadata(struct cache *cache)
2341 int r1, r2, r3, r4;
2343 r1 = write_dirty_bitset(cache);
2344 if (r1)
2345 DMERR("could not write dirty bitset");
2347 r2 = write_discard_bitset(cache);
2348 if (r2)
2349 DMERR("could not write discard bitset");
2351 save_stats(cache);
2353 r3 = write_hints(cache);
2354 if (r3)
2355 DMERR("could not write hints");
2358 * If writing the above metadata failed, we still commit, but don't
2359 * set the clean shutdown flag. This will effectively force every
2360 * dirty bit to be set on reload.
2362 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2363 if (r4)
2364 DMERR("could not write cache metadata. Data loss may occur.");
2366 return !r1 && !r2 && !r3 && !r4;
2369 static void cache_postsuspend(struct dm_target *ti)
2371 struct cache *cache = ti->private;
2373 start_quiescing(cache);
2374 wait_for_migrations(cache);
2375 stop_worker(cache);
2376 requeue_deferred_io(cache);
2377 stop_quiescing(cache);
2379 (void) sync_metadata(cache);
2382 static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2383 bool dirty, uint32_t hint, bool hint_valid)
2385 int r;
2386 struct cache *cache = context;
2388 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2389 if (r)
2390 return r;
2392 if (dirty)
2393 set_dirty(cache, oblock, cblock);
2394 else
2395 clear_dirty(cache, oblock, cblock);
2397 return 0;
2400 static int load_discard(void *context, sector_t discard_block_size,
2401 dm_dblock_t dblock, bool discard)
2403 struct cache *cache = context;
2405 /* FIXME: handle mis-matched block size */
2407 if (discard)
2408 set_discard(cache, dblock);
2409 else
2410 clear_discard(cache, dblock);
2412 return 0;
2415 static int cache_preresume(struct dm_target *ti)
2417 int r = 0;
2418 struct cache *cache = ti->private;
2419 sector_t actual_cache_size = get_dev_size(cache->cache_dev);
2420 (void) sector_div(actual_cache_size, cache->sectors_per_block);
2423 * Check to see if the cache has resized.
2425 if (from_cblock(cache->cache_size) != actual_cache_size || !cache->sized) {
2426 cache->cache_size = to_cblock(actual_cache_size);
2428 r = dm_cache_resize(cache->cmd, cache->cache_size);
2429 if (r) {
2430 DMERR("could not resize cache metadata");
2431 return r;
2434 cache->sized = true;
2437 if (!cache->loaded_mappings) {
2438 r = dm_cache_load_mappings(cache->cmd, cache->policy,
2439 load_mapping, cache);
2440 if (r) {
2441 DMERR("could not load cache mappings");
2442 return r;
2445 cache->loaded_mappings = true;
2448 if (!cache->loaded_discards) {
2449 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2450 if (r) {
2451 DMERR("could not load origin discards");
2452 return r;
2455 cache->loaded_discards = true;
2458 return r;
2461 static void cache_resume(struct dm_target *ti)
2463 struct cache *cache = ti->private;
2465 cache->need_tick_bio = true;
2466 do_waker(&cache->waker.work);
2470 * Status format:
2472 * <#used metadata blocks>/<#total metadata blocks>
2473 * <#read hits> <#read misses> <#write hits> <#write misses>
2474 * <#demotions> <#promotions> <#blocks in cache> <#dirty>
2475 * <#features> <features>*
2476 * <#core args> <core args>
2477 * <#policy args> <policy args>*
2479 static void cache_status(struct dm_target *ti, status_type_t type,
2480 unsigned status_flags, char *result, unsigned maxlen)
2482 int r = 0;
2483 unsigned i;
2484 ssize_t sz = 0;
2485 dm_block_t nr_free_blocks_metadata = 0;
2486 dm_block_t nr_blocks_metadata = 0;
2487 char buf[BDEVNAME_SIZE];
2488 struct cache *cache = ti->private;
2489 dm_cblock_t residency;
2491 switch (type) {
2492 case STATUSTYPE_INFO:
2493 /* Commit to ensure statistics aren't out-of-date */
2494 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2495 r = dm_cache_commit(cache->cmd, false);
2496 if (r)
2497 DMERR("could not commit metadata for accurate status");
2500 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2501 &nr_free_blocks_metadata);
2502 if (r) {
2503 DMERR("could not get metadata free block count");
2504 goto err;
2507 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2508 if (r) {
2509 DMERR("could not get metadata device size");
2510 goto err;
2513 residency = policy_residency(cache->policy);
2515 DMEMIT("%llu/%llu %u %u %u %u %u %u %llu %lu ",
2516 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2517 (unsigned long long)nr_blocks_metadata,
2518 (unsigned) atomic_read(&cache->stats.read_hit),
2519 (unsigned) atomic_read(&cache->stats.read_miss),
2520 (unsigned) atomic_read(&cache->stats.write_hit),
2521 (unsigned) atomic_read(&cache->stats.write_miss),
2522 (unsigned) atomic_read(&cache->stats.demotion),
2523 (unsigned) atomic_read(&cache->stats.promotion),
2524 (unsigned long long) from_cblock(residency),
2525 (unsigned long) atomic_read(&cache->nr_dirty));
2527 if (cache->features.write_through)
2528 DMEMIT("1 writethrough ");
2529 else
2530 DMEMIT("0 ");
2532 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
2533 if (sz < maxlen) {
2534 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2535 if (r)
2536 DMERR("policy_emit_config_values returned %d", r);
2539 break;
2541 case STATUSTYPE_TABLE:
2542 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2543 DMEMIT("%s ", buf);
2544 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2545 DMEMIT("%s ", buf);
2546 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2547 DMEMIT("%s", buf);
2549 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2550 DMEMIT(" %s", cache->ctr_args[i]);
2551 if (cache->nr_ctr_args)
2552 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2555 return;
2557 err:
2558 DMEMIT("Error");
2562 * Supports <key> <value>.
2564 * The key migration_threshold is supported by the cache target core.
2566 static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
2568 struct cache *cache = ti->private;
2570 if (argc != 2)
2571 return -EINVAL;
2573 return set_config_value(cache, argv[0], argv[1]);
2576 static int cache_iterate_devices(struct dm_target *ti,
2577 iterate_devices_callout_fn fn, void *data)
2579 int r = 0;
2580 struct cache *cache = ti->private;
2582 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
2583 if (!r)
2584 r = fn(ti, cache->origin_dev, 0, ti->len, data);
2586 return r;
2590 * We assume I/O is going to the origin (which is the volume
2591 * more likely to have restrictions e.g. by being striped).
2592 * (Looking up the exact location of the data would be expensive
2593 * and could always be out of date by the time the bio is submitted.)
2595 static int cache_bvec_merge(struct dm_target *ti,
2596 struct bvec_merge_data *bvm,
2597 struct bio_vec *biovec, int max_size)
2599 struct cache *cache = ti->private;
2600 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
2602 if (!q->merge_bvec_fn)
2603 return max_size;
2605 bvm->bi_bdev = cache->origin_dev->bdev;
2606 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2609 static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
2612 * FIXME: these limits may be incompatible with the cache device
2614 limits->max_discard_sectors = cache->discard_block_size;
2615 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
2618 static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
2620 struct cache *cache = ti->private;
2621 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
2624 * If the system-determined stacked limits are compatible with the
2625 * cache's blocksize (io_opt is a factor) do not override them.
2627 if (io_opt_sectors < cache->sectors_per_block ||
2628 do_div(io_opt_sectors, cache->sectors_per_block)) {
2629 blk_limits_io_min(limits, 0);
2630 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
2632 set_discard_limits(cache, limits);
2635 /*----------------------------------------------------------------*/
2637 static struct target_type cache_target = {
2638 .name = "cache",
2639 .version = {1, 1, 1},
2640 .module = THIS_MODULE,
2641 .ctr = cache_ctr,
2642 .dtr = cache_dtr,
2643 .map = cache_map,
2644 .end_io = cache_end_io,
2645 .postsuspend = cache_postsuspend,
2646 .preresume = cache_preresume,
2647 .resume = cache_resume,
2648 .status = cache_status,
2649 .message = cache_message,
2650 .iterate_devices = cache_iterate_devices,
2651 .merge = cache_bvec_merge,
2652 .io_hints = cache_io_hints,
2655 static int __init dm_cache_init(void)
2657 int r;
2659 r = dm_register_target(&cache_target);
2660 if (r) {
2661 DMERR("cache target registration failed: %d", r);
2662 return r;
2665 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
2666 if (!migration_cache) {
2667 dm_unregister_target(&cache_target);
2668 return -ENOMEM;
2671 return 0;
2674 static void __exit dm_cache_exit(void)
2676 dm_unregister_target(&cache_target);
2677 kmem_cache_destroy(migration_cache);
2680 module_init(dm_cache_init);
2681 module_exit(dm_cache_exit);
2683 MODULE_DESCRIPTION(DM_NAME " cache target");
2684 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
2685 MODULE_LICENSE("GPL");