2 * Copyright (C) 2012 Red Hat. All rights reserved.
4 * This file is released under the GPL.
8 #include "dm-bio-prison.h"
9 #include "dm-bio-record.h"
10 #include "dm-cache-metadata.h"
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/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 /*----------------------------------------------------------------*/
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,
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
);
52 static void clear_bitset(void *bitset
, unsigned nr_entries
)
54 size_t s
= bitset_size_in_bytes(nr_entries
);
58 static void free_bitset(unsigned long *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.
71 bio_end_io_t
*bi_end_io
;
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
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 */
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.
127 * Data is written to both cache and origin. Blocks are never
128 * dirty. Potential performance benfit for reads only.
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.
141 struct cache_features
{
142 enum cache_metadata_mode mode
;
143 enum cache_io_mode io_mode
;
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
{
168 struct invalidation_request
{
169 struct list_head list
;
170 struct cblock_range
*cblocks
;
175 wait_queue_head_t result_wait
;
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
;
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
;
235 atomic_t quiescing_ack
;
238 * cache_size entries, dirty if set
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;
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
{
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.
306 struct dm_bio_details bio_details
;
309 struct dm_cache_migration
{
310 struct list_head list
;
313 unsigned long start_jiffies
;
314 dm_oblock_t old_oblock
;
315 dm_oblock_t new_oblock
;
323 bool requeue_holder
: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).
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
);
366 atomic_inc(&mg
->cache
->nr_allocated_migrations
);
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
)
383 p
->mg
= alloc_migration(cache
);
389 p
->cell1
= alloc_prison_cell(cache
);
395 p
->cell2
= alloc_prison_cell(cache
);
403 static void prealloc_free_structs(struct cache
*cache
, struct prealloc
*p
)
406 free_prison_cell(cache
, p
->cell2
);
409 free_prison_cell(cache
, p
->cell1
);
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
;
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
;
437 } else if (p
->cell2
) {
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
)
462 /*----------------------------------------------------------------*/
464 static void build_key(dm_oblock_t begin
, dm_oblock_t end
, struct dm_cell_key
*key
)
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
)
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
);
490 free_fn(free_context
, cell_prealloc
);
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
,
507 struct prealloc
*structs
,
508 struct dm_bio_prison_cell
**cell_result
)
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
);
519 prealloc_put_cell(structs
, cell_prealloc
);
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
559 static dm_block_t
block_div(dm_block_t b
, uint32_t n
)
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
;
573 oblocks
= block_div(oblocks
, cache
->sectors_per_block
);
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
)
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
)
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
)
615 spin_lock_irqsave(&cache
->lock
, flags
);
616 r
= test_bit(from_dblock(b
), cache
->discard_bitset
);
617 spin_unlock_irqrestore(&cache
->lock
, flags
);
622 static bool is_discarded_oblock(struct cache
*cache
, dm_oblock_t b
)
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
);
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 /*----------------------------------------------------------------
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
);
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
);
702 pb
->req_nr
= dm_bio_get_target_bio_nr(bio
);
703 pb
->all_io_entry
= NULL
;
708 /*----------------------------------------------------------------
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
,
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
);
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
)
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
))) {
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
,
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
);
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
);
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
)
805 if (!bio_triggers_commit(cache
, bio
)) {
806 generic_make_request(bio
);
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
);
826 static void defer_writethrough_bio(struct cache
*cache
, struct bio
*bio
)
830 spin_lock_irqsave(&cache
->lock
, flags
);
831 bio_list_add(&cache
->deferred_writethrough_bios
, bio
);
832 spin_unlock_irqrestore(&cache
->lock
, flags
);
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
);
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
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
);
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
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
,
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
,
907 spin_lock_irqsave(&cache
->lock
, flags
);
908 __cell_defer(cache
, cell
, holder
);
909 spin_unlock_irqrestore(&cache
->lock
, flags
);
914 static void free_io_migration(struct dm_cache_migration
*mg
)
916 dec_io_migrations(mg
->cache
);
920 static void migration_failure(struct dm_cache_migration
*mg
)
922 struct cache
*cache
= mg
->cache
;
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);
935 cell_defer(cache
, mg
->new_ocell
, true);
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
)
948 struct cache
*cache
= mg
->cache
;
951 clear_dirty(cache
, mg
->old_oblock
, mg
->cblock
);
952 cell_defer(cache
, mg
->old_ocell
, false);
953 free_io_migration(mg
);
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
,
962 cell_defer(cache
, mg
->new_ocell
, true);
963 free_io_migration(mg
);
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
);
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
)
984 struct cache
*cache
= mg
->cache
;
987 DMWARN("writeback unexpectedly triggered commit");
990 } else if (mg
->demote
) {
991 cell_defer(cache
, mg
->old_ocell
, mg
->promote
? false : true);
996 spin_lock_irqsave(&cache
->lock
, flags
);
997 list_add_tail(&mg
->list
, &cache
->quiesced_migrations
);
998 spin_unlock_irqrestore(&cache
->lock
, flags
);
1002 policy_remove_mapping(cache
->policy
, mg
->old_oblock
);
1003 free_io_migration(mg
);
1007 if (mg
->requeue_holder
) {
1008 clear_dirty(cache
, mg
->new_oblock
, mg
->cblock
);
1009 cell_defer(cache
, mg
->new_ocell
, true);
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
)
1031 spin_lock_irqsave(&cache
->lock
, flags
);
1032 list_add_tail(&mg
->list
, &cache
->completed_migrations
);
1033 spin_unlock_irqrestore(&cache
->lock
, flags
);
1038 static void issue_copy(struct dm_cache_migration
*mg
)
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
) {
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
);
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
);
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
);
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
);
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
)
1128 *e
= to_dblock(block_div(se
, cache
->discard_block_size
));
1131 static void issue_discard(struct dm_cache_migration
*mg
)
1134 struct bio
*bio
= mg
->new_ocell
->holder
;
1136 calc_discard_block_range(mg
->cache
, bio
, &b
, &e
);
1138 set_discard(mg
->cache
, b
);
1139 b
= to_dblock(from_dblock(b
) + 1);
1143 cell_defer(mg
->cache
, mg
->new_ocell
, false);
1147 static void issue_copy_or_discard(struct dm_cache_migration
*mg
)
1150 struct cache
*cache
= mg
->cache
;
1157 if (mg
->writeback
|| mg
->demote
)
1158 avoid
= !is_dirty(cache
, mg
->cblock
) ||
1159 is_discarded_oblock(cache
, mg
->old_oblock
);
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
);
1172 avoid
? avoid_copy(mg
) : issue_copy(mg
);
1175 static void complete_migration(struct dm_cache_migration
*mg
)
1178 migration_failure(mg
);
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
)
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
);
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
);
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
)
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
);
1257 mg
->discard
= false;
1258 mg
->writeback
= false;
1261 mg
->requeue_holder
= true;
1262 mg
->invalidate
= false;
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
);
1281 mg
->discard
= false;
1282 mg
->writeback
= true;
1284 mg
->promote
= false;
1285 mg
->requeue_holder
= true;
1286 mg
->invalidate
= false;
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
,
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
);
1307 mg
->discard
= false;
1308 mg
->writeback
= false;
1311 mg
->requeue_holder
= true;
1312 mg
->invalidate
= false;
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
);
1336 mg
->discard
= false;
1337 mg
->writeback
= false;
1339 mg
->promote
= false;
1340 mg
->requeue_holder
= true;
1341 mg
->invalidate
= true;
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
);
1360 mg
->writeback
= false;
1362 mg
->promote
= false;
1363 mg
->requeue_holder
= false;
1364 mg
->invalidate
= false;
1366 mg
->old_ocell
= NULL
;
1367 mg
->new_ocell
= cell
;
1368 mg
->start_jiffies
= jiffies
;
1370 quiesce_migration(mg
);
1373 /*----------------------------------------------------------------
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
);
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
);
1394 remap_to_origin(cache
, bio
);
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
1406 static void process_discard_bio(struct cache
*cache
, struct prealloc
*structs
,
1411 struct dm_bio_prison_cell
*cell_prealloc
, *new_ocell
;
1413 calc_discard_block_range(cache
, bio
, &b
, &e
);
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
);
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 static void process_bio(struct cache
*cache
, struct prealloc
*structs
,
1452 bool release_cell
= true;
1453 dm_oblock_t block
= get_bio_block(cache
, bio
);
1454 struct dm_bio_prison_cell
*cell_prealloc
, *old_ocell
, *new_ocell
;
1455 struct policy_result lookup_result
;
1456 bool passthrough
= passthrough_mode(&cache
->features
);
1457 bool discarded_block
, can_migrate
;
1460 * Check to see if that block is currently migrating.
1462 cell_prealloc
= prealloc_get_cell(structs
);
1463 r
= bio_detain(cache
, block
, bio
, cell_prealloc
,
1464 (cell_free_fn
) prealloc_put_cell
,
1465 structs
, &new_ocell
);
1469 discarded_block
= is_discarded_oblock(cache
, block
);
1470 can_migrate
= !passthrough
&& (discarded_block
|| spare_migration_bandwidth(cache
));
1472 r
= policy_map(cache
->policy
, block
, true, can_migrate
, discarded_block
,
1473 bio
, &lookup_result
);
1475 if (r
== -EWOULDBLOCK
)
1476 /* migration has been denied */
1477 lookup_result
.op
= POLICY_MISS
;
1479 switch (lookup_result
.op
) {
1482 inc_miss_counter(cache
, bio
);
1485 * Passthrough always maps to the origin,
1486 * invalidating any cache blocks that are written
1490 if (bio_data_dir(bio
) == WRITE
) {
1491 atomic_inc(&cache
->stats
.demotion
);
1492 invalidate(cache
, structs
, block
, lookup_result
.cblock
, new_ocell
);
1493 release_cell
= false;
1496 /* FIXME: factor out issue_origin() */
1497 remap_to_origin_clear_discard(cache
, bio
, block
);
1498 inc_and_issue(cache
, bio
, new_ocell
);
1501 inc_hit_counter(cache
, bio
);
1503 if (bio_data_dir(bio
) == WRITE
&&
1504 writethrough_mode(&cache
->features
) &&
1505 !is_dirty(cache
, lookup_result
.cblock
)) {
1506 remap_to_origin_then_cache(cache
, bio
, block
, lookup_result
.cblock
);
1507 inc_and_issue(cache
, bio
, new_ocell
);
1510 remap_to_cache_dirty(cache
, bio
, block
, lookup_result
.cblock
);
1511 inc_and_issue(cache
, bio
, new_ocell
);
1518 inc_miss_counter(cache
, bio
);
1519 remap_to_origin_clear_discard(cache
, bio
, block
);
1520 inc_and_issue(cache
, bio
, new_ocell
);
1524 atomic_inc(&cache
->stats
.promotion
);
1525 promote(cache
, structs
, block
, lookup_result
.cblock
, new_ocell
);
1526 release_cell
= false;
1529 case POLICY_REPLACE
:
1530 cell_prealloc
= prealloc_get_cell(structs
);
1531 r
= bio_detain(cache
, lookup_result
.old_oblock
, bio
, cell_prealloc
,
1532 (cell_free_fn
) prealloc_put_cell
,
1533 structs
, &old_ocell
);
1536 * We have to be careful to avoid lock inversion of
1537 * the cells. So we back off, and wait for the
1538 * old_ocell to become free.
1540 policy_force_mapping(cache
->policy
, block
,
1541 lookup_result
.old_oblock
);
1542 atomic_inc(&cache
->stats
.cache_cell_clash
);
1545 atomic_inc(&cache
->stats
.demotion
);
1546 atomic_inc(&cache
->stats
.promotion
);
1548 demote_then_promote(cache
, structs
, lookup_result
.old_oblock
,
1549 block
, lookup_result
.cblock
,
1550 old_ocell
, new_ocell
);
1551 release_cell
= false;
1555 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__
,
1556 (unsigned) lookup_result
.op
);
1561 cell_defer(cache
, new_ocell
, false);
1564 static int need_commit_due_to_time(struct cache
*cache
)
1566 return !time_in_range(jiffies
, cache
->last_commit_jiffies
,
1567 cache
->last_commit_jiffies
+ COMMIT_PERIOD
);
1570 static int commit_if_needed(struct cache
*cache
)
1574 if ((cache
->commit_requested
|| need_commit_due_to_time(cache
)) &&
1575 dm_cache_changed_this_transaction(cache
->cmd
)) {
1576 atomic_inc(&cache
->stats
.commit_count
);
1577 cache
->commit_requested
= false;
1578 r
= dm_cache_commit(cache
->cmd
, false);
1579 cache
->last_commit_jiffies
= jiffies
;
1585 static void process_deferred_bios(struct cache
*cache
)
1587 unsigned long flags
;
1588 struct bio_list bios
;
1590 struct prealloc structs
;
1592 memset(&structs
, 0, sizeof(structs
));
1593 bio_list_init(&bios
);
1595 spin_lock_irqsave(&cache
->lock
, flags
);
1596 bio_list_merge(&bios
, &cache
->deferred_bios
);
1597 bio_list_init(&cache
->deferred_bios
);
1598 spin_unlock_irqrestore(&cache
->lock
, flags
);
1600 while (!bio_list_empty(&bios
)) {
1602 * If we've got no free migration structs, and processing
1603 * this bio might require one, we pause until there are some
1604 * prepared mappings to process.
1606 if (prealloc_data_structs(cache
, &structs
)) {
1607 spin_lock_irqsave(&cache
->lock
, flags
);
1608 bio_list_merge(&cache
->deferred_bios
, &bios
);
1609 spin_unlock_irqrestore(&cache
->lock
, flags
);
1613 bio
= bio_list_pop(&bios
);
1615 if (bio
->bi_rw
& REQ_FLUSH
)
1616 process_flush_bio(cache
, bio
);
1617 else if (bio
->bi_rw
& REQ_DISCARD
)
1618 process_discard_bio(cache
, &structs
, bio
);
1620 process_bio(cache
, &structs
, bio
);
1623 prealloc_free_structs(cache
, &structs
);
1626 static void process_deferred_flush_bios(struct cache
*cache
, bool submit_bios
)
1628 unsigned long flags
;
1629 struct bio_list bios
;
1632 bio_list_init(&bios
);
1634 spin_lock_irqsave(&cache
->lock
, flags
);
1635 bio_list_merge(&bios
, &cache
->deferred_flush_bios
);
1636 bio_list_init(&cache
->deferred_flush_bios
);
1637 spin_unlock_irqrestore(&cache
->lock
, flags
);
1640 * These bios have already been through inc_ds()
1642 while ((bio
= bio_list_pop(&bios
)))
1643 submit_bios
? generic_make_request(bio
) : bio_io_error(bio
);
1646 static void process_deferred_writethrough_bios(struct cache
*cache
)
1648 unsigned long flags
;
1649 struct bio_list bios
;
1652 bio_list_init(&bios
);
1654 spin_lock_irqsave(&cache
->lock
, flags
);
1655 bio_list_merge(&bios
, &cache
->deferred_writethrough_bios
);
1656 bio_list_init(&cache
->deferred_writethrough_bios
);
1657 spin_unlock_irqrestore(&cache
->lock
, flags
);
1660 * These bios have already been through inc_ds()
1662 while ((bio
= bio_list_pop(&bios
)))
1663 generic_make_request(bio
);
1666 static void writeback_some_dirty_blocks(struct cache
*cache
)
1671 struct prealloc structs
;
1672 struct dm_bio_prison_cell
*old_ocell
;
1674 memset(&structs
, 0, sizeof(structs
));
1676 while (spare_migration_bandwidth(cache
)) {
1677 if (prealloc_data_structs(cache
, &structs
))
1680 r
= policy_writeback_work(cache
->policy
, &oblock
, &cblock
);
1684 r
= get_cell(cache
, oblock
, &structs
, &old_ocell
);
1686 policy_set_dirty(cache
->policy
, oblock
);
1690 writeback(cache
, &structs
, oblock
, cblock
, old_ocell
);
1693 prealloc_free_structs(cache
, &structs
);
1696 /*----------------------------------------------------------------
1698 * Dropping something from the cache *without* writing back.
1699 *--------------------------------------------------------------*/
1701 static void process_invalidation_request(struct cache
*cache
, struct invalidation_request
*req
)
1704 uint64_t begin
= from_cblock(req
->cblocks
->begin
);
1705 uint64_t end
= from_cblock(req
->cblocks
->end
);
1707 while (begin
!= end
) {
1708 r
= policy_remove_cblock(cache
->policy
, to_cblock(begin
));
1710 r
= dm_cache_remove_mapping(cache
->cmd
, to_cblock(begin
));
1714 } else if (r
== -ENODATA
) {
1715 /* harmless, already unmapped */
1719 DMERR("policy_remove_cblock failed");
1726 cache
->commit_requested
= true;
1729 atomic_set(&req
->complete
, 1);
1731 wake_up(&req
->result_wait
);
1734 static void process_invalidation_requests(struct cache
*cache
)
1736 struct list_head list
;
1737 struct invalidation_request
*req
, *tmp
;
1739 INIT_LIST_HEAD(&list
);
1740 spin_lock(&cache
->invalidation_lock
);
1741 list_splice_init(&cache
->invalidation_requests
, &list
);
1742 spin_unlock(&cache
->invalidation_lock
);
1744 list_for_each_entry_safe (req
, tmp
, &list
, list
)
1745 process_invalidation_request(cache
, req
);
1748 /*----------------------------------------------------------------
1750 *--------------------------------------------------------------*/
1751 static bool is_quiescing(struct cache
*cache
)
1753 return atomic_read(&cache
->quiescing
);
1756 static void ack_quiescing(struct cache
*cache
)
1758 if (is_quiescing(cache
)) {
1759 atomic_inc(&cache
->quiescing_ack
);
1760 wake_up(&cache
->quiescing_wait
);
1764 static void wait_for_quiescing_ack(struct cache
*cache
)
1766 wait_event(cache
->quiescing_wait
, atomic_read(&cache
->quiescing_ack
));
1769 static void start_quiescing(struct cache
*cache
)
1771 atomic_inc(&cache
->quiescing
);
1772 wait_for_quiescing_ack(cache
);
1775 static void stop_quiescing(struct cache
*cache
)
1777 atomic_set(&cache
->quiescing
, 0);
1778 atomic_set(&cache
->quiescing_ack
, 0);
1781 static void wait_for_migrations(struct cache
*cache
)
1783 wait_event(cache
->migration_wait
, !atomic_read(&cache
->nr_allocated_migrations
));
1786 static void stop_worker(struct cache
*cache
)
1788 cancel_delayed_work(&cache
->waker
);
1789 flush_workqueue(cache
->wq
);
1792 static void requeue_deferred_io(struct cache
*cache
)
1795 struct bio_list bios
;
1797 bio_list_init(&bios
);
1798 bio_list_merge(&bios
, &cache
->deferred_bios
);
1799 bio_list_init(&cache
->deferred_bios
);
1801 while ((bio
= bio_list_pop(&bios
)))
1802 bio_endio(bio
, DM_ENDIO_REQUEUE
);
1805 static int more_work(struct cache
*cache
)
1807 if (is_quiescing(cache
))
1808 return !list_empty(&cache
->quiesced_migrations
) ||
1809 !list_empty(&cache
->completed_migrations
) ||
1810 !list_empty(&cache
->need_commit_migrations
);
1812 return !bio_list_empty(&cache
->deferred_bios
) ||
1813 !bio_list_empty(&cache
->deferred_flush_bios
) ||
1814 !bio_list_empty(&cache
->deferred_writethrough_bios
) ||
1815 !list_empty(&cache
->quiesced_migrations
) ||
1816 !list_empty(&cache
->completed_migrations
) ||
1817 !list_empty(&cache
->need_commit_migrations
) ||
1821 static void do_worker(struct work_struct
*ws
)
1823 struct cache
*cache
= container_of(ws
, struct cache
, worker
);
1826 if (!is_quiescing(cache
)) {
1827 writeback_some_dirty_blocks(cache
);
1828 process_deferred_writethrough_bios(cache
);
1829 process_deferred_bios(cache
);
1830 process_invalidation_requests(cache
);
1833 process_migrations(cache
, &cache
->quiesced_migrations
, issue_copy_or_discard
);
1834 process_migrations(cache
, &cache
->completed_migrations
, complete_migration
);
1836 if (commit_if_needed(cache
)) {
1837 process_deferred_flush_bios(cache
, false);
1838 process_migrations(cache
, &cache
->need_commit_migrations
, migration_failure
);
1841 * FIXME: rollback metadata or just go into a
1842 * failure mode and error everything
1845 process_deferred_flush_bios(cache
, true);
1846 process_migrations(cache
, &cache
->need_commit_migrations
,
1847 migration_success_post_commit
);
1850 ack_quiescing(cache
);
1852 } while (more_work(cache
));
1856 * We want to commit periodically so that not too much
1857 * unwritten metadata builds up.
1859 static void do_waker(struct work_struct
*ws
)
1861 struct cache
*cache
= container_of(to_delayed_work(ws
), struct cache
, waker
);
1862 policy_tick(cache
->policy
);
1864 queue_delayed_work(cache
->wq
, &cache
->waker
, COMMIT_PERIOD
);
1867 /*----------------------------------------------------------------*/
1869 static int is_congested(struct dm_dev
*dev
, int bdi_bits
)
1871 struct request_queue
*q
= bdev_get_queue(dev
->bdev
);
1872 return bdi_congested(&q
->backing_dev_info
, bdi_bits
);
1875 static int cache_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
1877 struct cache
*cache
= container_of(cb
, struct cache
, callbacks
);
1879 return is_congested(cache
->origin_dev
, bdi_bits
) ||
1880 is_congested(cache
->cache_dev
, bdi_bits
);
1883 /*----------------------------------------------------------------
1885 *--------------------------------------------------------------*/
1888 * This function gets called on the error paths of the constructor, so we
1889 * have to cope with a partially initialised struct.
1891 static void destroy(struct cache
*cache
)
1895 if (cache
->migration_pool
)
1896 mempool_destroy(cache
->migration_pool
);
1898 if (cache
->all_io_ds
)
1899 dm_deferred_set_destroy(cache
->all_io_ds
);
1902 dm_bio_prison_destroy(cache
->prison
);
1905 destroy_workqueue(cache
->wq
);
1907 if (cache
->dirty_bitset
)
1908 free_bitset(cache
->dirty_bitset
);
1910 if (cache
->discard_bitset
)
1911 free_bitset(cache
->discard_bitset
);
1914 dm_kcopyd_client_destroy(cache
->copier
);
1917 dm_cache_metadata_close(cache
->cmd
);
1919 if (cache
->metadata_dev
)
1920 dm_put_device(cache
->ti
, cache
->metadata_dev
);
1922 if (cache
->origin_dev
)
1923 dm_put_device(cache
->ti
, cache
->origin_dev
);
1925 if (cache
->cache_dev
)
1926 dm_put_device(cache
->ti
, cache
->cache_dev
);
1929 dm_cache_policy_destroy(cache
->policy
);
1931 for (i
= 0; i
< cache
->nr_ctr_args
; i
++)
1932 kfree(cache
->ctr_args
[i
]);
1933 kfree(cache
->ctr_args
);
1938 static void cache_dtr(struct dm_target
*ti
)
1940 struct cache
*cache
= ti
->private;
1945 static sector_t
get_dev_size(struct dm_dev
*dev
)
1947 return i_size_read(dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
1950 /*----------------------------------------------------------------*/
1953 * Construct a cache device mapping.
1955 * cache <metadata dev> <cache dev> <origin dev> <block size>
1956 * <#feature args> [<feature arg>]*
1957 * <policy> <#policy args> [<policy arg>]*
1959 * metadata dev : fast device holding the persistent metadata
1960 * cache dev : fast device holding cached data blocks
1961 * origin dev : slow device holding original data blocks
1962 * block size : cache unit size in sectors
1964 * #feature args : number of feature arguments passed
1965 * feature args : writethrough. (The default is writeback.)
1967 * policy : the replacement policy to use
1968 * #policy args : an even number of policy arguments corresponding
1969 * to key/value pairs passed to the policy
1970 * policy args : key/value pairs passed to the policy
1971 * E.g. 'sequential_threshold 1024'
1972 * See cache-policies.txt for details.
1974 * Optional feature arguments are:
1975 * writethrough : write through caching that prohibits cache block
1976 * content from being different from origin block content.
1977 * Without this argument, the default behaviour is to write
1978 * back cache block contents later for performance reasons,
1979 * so they may differ from the corresponding origin blocks.
1982 struct dm_target
*ti
;
1984 struct dm_dev
*metadata_dev
;
1986 struct dm_dev
*cache_dev
;
1987 sector_t cache_sectors
;
1989 struct dm_dev
*origin_dev
;
1990 sector_t origin_sectors
;
1992 uint32_t block_size
;
1994 const char *policy_name
;
1996 const char **policy_argv
;
1998 struct cache_features features
;
2001 static void destroy_cache_args(struct cache_args
*ca
)
2003 if (ca
->metadata_dev
)
2004 dm_put_device(ca
->ti
, ca
->metadata_dev
);
2007 dm_put_device(ca
->ti
, ca
->cache_dev
);
2010 dm_put_device(ca
->ti
, ca
->origin_dev
);
2015 static bool at_least_one_arg(struct dm_arg_set
*as
, char **error
)
2018 *error
= "Insufficient args";
2025 static int parse_metadata_dev(struct cache_args
*ca
, struct dm_arg_set
*as
,
2029 sector_t metadata_dev_size
;
2030 char b
[BDEVNAME_SIZE
];
2032 if (!at_least_one_arg(as
, error
))
2035 r
= dm_get_device(ca
->ti
, dm_shift_arg(as
), FMODE_READ
| FMODE_WRITE
,
2038 *error
= "Error opening metadata device";
2042 metadata_dev_size
= get_dev_size(ca
->metadata_dev
);
2043 if (metadata_dev_size
> DM_CACHE_METADATA_MAX_SECTORS_WARNING
)
2044 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2045 bdevname(ca
->metadata_dev
->bdev
, b
), THIN_METADATA_MAX_SECTORS
);
2050 static int parse_cache_dev(struct cache_args
*ca
, struct dm_arg_set
*as
,
2055 if (!at_least_one_arg(as
, error
))
2058 r
= dm_get_device(ca
->ti
, dm_shift_arg(as
), FMODE_READ
| FMODE_WRITE
,
2061 *error
= "Error opening cache device";
2064 ca
->cache_sectors
= get_dev_size(ca
->cache_dev
);
2069 static int parse_origin_dev(struct cache_args
*ca
, struct dm_arg_set
*as
,
2074 if (!at_least_one_arg(as
, error
))
2077 r
= dm_get_device(ca
->ti
, dm_shift_arg(as
), FMODE_READ
| FMODE_WRITE
,
2080 *error
= "Error opening origin device";
2084 ca
->origin_sectors
= get_dev_size(ca
->origin_dev
);
2085 if (ca
->ti
->len
> ca
->origin_sectors
) {
2086 *error
= "Device size larger than cached device";
2093 static int parse_block_size(struct cache_args
*ca
, struct dm_arg_set
*as
,
2096 unsigned long block_size
;
2098 if (!at_least_one_arg(as
, error
))
2101 if (kstrtoul(dm_shift_arg(as
), 10, &block_size
) || !block_size
||
2102 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
2103 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
2104 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
2105 *error
= "Invalid data block size";
2109 if (block_size
> ca
->cache_sectors
) {
2110 *error
= "Data block size is larger than the cache device";
2114 ca
->block_size
= block_size
;
2119 static void init_features(struct cache_features
*cf
)
2121 cf
->mode
= CM_WRITE
;
2122 cf
->io_mode
= CM_IO_WRITEBACK
;
2125 static int parse_features(struct cache_args
*ca
, struct dm_arg_set
*as
,
2128 static struct dm_arg _args
[] = {
2129 {0, 1, "Invalid number of cache feature arguments"},
2135 struct cache_features
*cf
= &ca
->features
;
2139 r
= dm_read_arg_group(_args
, as
, &argc
, error
);
2144 arg
= dm_shift_arg(as
);
2146 if (!strcasecmp(arg
, "writeback"))
2147 cf
->io_mode
= CM_IO_WRITEBACK
;
2149 else if (!strcasecmp(arg
, "writethrough"))
2150 cf
->io_mode
= CM_IO_WRITETHROUGH
;
2152 else if (!strcasecmp(arg
, "passthrough"))
2153 cf
->io_mode
= CM_IO_PASSTHROUGH
;
2156 *error
= "Unrecognised cache feature requested";
2164 static int parse_policy(struct cache_args
*ca
, struct dm_arg_set
*as
,
2167 static struct dm_arg _args
[] = {
2168 {0, 1024, "Invalid number of policy arguments"},
2173 if (!at_least_one_arg(as
, error
))
2176 ca
->policy_name
= dm_shift_arg(as
);
2178 r
= dm_read_arg_group(_args
, as
, &ca
->policy_argc
, error
);
2182 ca
->policy_argv
= (const char **)as
->argv
;
2183 dm_consume_args(as
, ca
->policy_argc
);
2188 static int parse_cache_args(struct cache_args
*ca
, int argc
, char **argv
,
2192 struct dm_arg_set as
;
2197 r
= parse_metadata_dev(ca
, &as
, error
);
2201 r
= parse_cache_dev(ca
, &as
, error
);
2205 r
= parse_origin_dev(ca
, &as
, error
);
2209 r
= parse_block_size(ca
, &as
, error
);
2213 r
= parse_features(ca
, &as
, error
);
2217 r
= parse_policy(ca
, &as
, error
);
2224 /*----------------------------------------------------------------*/
2226 static struct kmem_cache
*migration_cache
;
2228 #define NOT_CORE_OPTION 1
2230 static int process_config_option(struct cache
*cache
, const char *key
, const char *value
)
2234 if (!strcasecmp(key
, "migration_threshold")) {
2235 if (kstrtoul(value
, 10, &tmp
))
2238 cache
->migration_threshold
= tmp
;
2242 return NOT_CORE_OPTION
;
2245 static int set_config_value(struct cache
*cache
, const char *key
, const char *value
)
2247 int r
= process_config_option(cache
, key
, value
);
2249 if (r
== NOT_CORE_OPTION
)
2250 r
= policy_set_config_value(cache
->policy
, key
, value
);
2253 DMWARN("bad config value for %s: %s", key
, value
);
2258 static int set_config_values(struct cache
*cache
, int argc
, const char **argv
)
2263 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2268 r
= set_config_value(cache
, argv
[0], argv
[1]);
2279 static int create_cache_policy(struct cache
*cache
, struct cache_args
*ca
,
2282 struct dm_cache_policy
*p
= dm_cache_policy_create(ca
->policy_name
,
2284 cache
->origin_sectors
,
2285 cache
->sectors_per_block
);
2287 *error
= "Error creating cache's policy";
2296 * We want the discard block size to be at least the size of the cache
2297 * block size and have no more than 2^14 discard blocks across the origin.
2299 #define MAX_DISCARD_BLOCKS (1 << 14)
2301 static bool too_many_discard_blocks(sector_t discard_block_size
,
2302 sector_t origin_size
)
2304 (void) sector_div(origin_size
, discard_block_size
);
2306 return origin_size
> MAX_DISCARD_BLOCKS
;
2309 static sector_t
calculate_discard_block_size(sector_t cache_block_size
,
2310 sector_t origin_size
)
2312 sector_t discard_block_size
= cache_block_size
;
2315 while (too_many_discard_blocks(discard_block_size
, origin_size
))
2316 discard_block_size
*= 2;
2318 return discard_block_size
;
2321 static void set_cache_size(struct cache
*cache
, dm_cblock_t size
)
2323 dm_block_t nr_blocks
= from_cblock(size
);
2325 if (nr_blocks
> (1 << 20) && cache
->cache_size
!= size
)
2326 DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
2327 "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
2328 "Please consider increasing the cache block size to reduce the overall cache block count.",
2329 (unsigned long long) nr_blocks
);
2331 cache
->cache_size
= size
;
2334 #define DEFAULT_MIGRATION_THRESHOLD 2048
2336 static int cache_create(struct cache_args
*ca
, struct cache
**result
)
2339 char **error
= &ca
->ti
->error
;
2340 struct cache
*cache
;
2341 struct dm_target
*ti
= ca
->ti
;
2342 dm_block_t origin_blocks
;
2343 struct dm_cache_metadata
*cmd
;
2344 bool may_format
= ca
->features
.mode
== CM_WRITE
;
2346 cache
= kzalloc(sizeof(*cache
), GFP_KERNEL
);
2351 ti
->private = cache
;
2352 ti
->num_flush_bios
= 2;
2353 ti
->flush_supported
= true;
2355 ti
->num_discard_bios
= 1;
2356 ti
->discards_supported
= true;
2357 ti
->discard_zeroes_data_unsupported
= true;
2358 ti
->split_discard_bios
= false;
2360 cache
->features
= ca
->features
;
2361 ti
->per_bio_data_size
= get_per_bio_data_size(cache
);
2363 cache
->callbacks
.congested_fn
= cache_is_congested
;
2364 dm_table_add_target_callbacks(ti
->table
, &cache
->callbacks
);
2366 cache
->metadata_dev
= ca
->metadata_dev
;
2367 cache
->origin_dev
= ca
->origin_dev
;
2368 cache
->cache_dev
= ca
->cache_dev
;
2370 ca
->metadata_dev
= ca
->origin_dev
= ca
->cache_dev
= NULL
;
2372 /* FIXME: factor out this whole section */
2373 origin_blocks
= cache
->origin_sectors
= ca
->origin_sectors
;
2374 origin_blocks
= block_div(origin_blocks
, ca
->block_size
);
2375 cache
->origin_blocks
= to_oblock(origin_blocks
);
2377 cache
->sectors_per_block
= ca
->block_size
;
2378 if (dm_set_target_max_io_len(ti
, cache
->sectors_per_block
)) {
2383 if (ca
->block_size
& (ca
->block_size
- 1)) {
2384 dm_block_t cache_size
= ca
->cache_sectors
;
2386 cache
->sectors_per_block_shift
= -1;
2387 cache_size
= block_div(cache_size
, ca
->block_size
);
2388 set_cache_size(cache
, to_cblock(cache_size
));
2390 cache
->sectors_per_block_shift
= __ffs(ca
->block_size
);
2391 set_cache_size(cache
, to_cblock(ca
->cache_sectors
>> cache
->sectors_per_block_shift
));
2394 r
= create_cache_policy(cache
, ca
, error
);
2398 cache
->policy_nr_args
= ca
->policy_argc
;
2399 cache
->migration_threshold
= DEFAULT_MIGRATION_THRESHOLD
;
2401 r
= set_config_values(cache
, ca
->policy_argc
, ca
->policy_argv
);
2403 *error
= "Error setting cache policy's config values";
2407 cmd
= dm_cache_metadata_open(cache
->metadata_dev
->bdev
,
2408 ca
->block_size
, may_format
,
2409 dm_cache_policy_get_hint_size(cache
->policy
));
2411 *error
= "Error creating metadata object";
2417 if (passthrough_mode(&cache
->features
)) {
2420 r
= dm_cache_metadata_all_clean(cache
->cmd
, &all_clean
);
2422 *error
= "dm_cache_metadata_all_clean() failed";
2427 *error
= "Cannot enter passthrough mode unless all blocks are clean";
2433 spin_lock_init(&cache
->lock
);
2434 bio_list_init(&cache
->deferred_bios
);
2435 bio_list_init(&cache
->deferred_flush_bios
);
2436 bio_list_init(&cache
->deferred_writethrough_bios
);
2437 INIT_LIST_HEAD(&cache
->quiesced_migrations
);
2438 INIT_LIST_HEAD(&cache
->completed_migrations
);
2439 INIT_LIST_HEAD(&cache
->need_commit_migrations
);
2440 atomic_set(&cache
->nr_allocated_migrations
, 0);
2441 atomic_set(&cache
->nr_io_migrations
, 0);
2442 init_waitqueue_head(&cache
->migration_wait
);
2444 init_waitqueue_head(&cache
->quiescing_wait
);
2445 atomic_set(&cache
->quiescing
, 0);
2446 atomic_set(&cache
->quiescing_ack
, 0);
2449 atomic_set(&cache
->nr_dirty
, 0);
2450 cache
->dirty_bitset
= alloc_bitset(from_cblock(cache
->cache_size
));
2451 if (!cache
->dirty_bitset
) {
2452 *error
= "could not allocate dirty bitset";
2455 clear_bitset(cache
->dirty_bitset
, from_cblock(cache
->cache_size
));
2457 cache
->discard_block_size
=
2458 calculate_discard_block_size(cache
->sectors_per_block
,
2459 cache
->origin_sectors
);
2460 cache
->discard_nr_blocks
= to_dblock(dm_sector_div_up(cache
->origin_sectors
,
2461 cache
->discard_block_size
));
2462 cache
->discard_bitset
= alloc_bitset(from_dblock(cache
->discard_nr_blocks
));
2463 if (!cache
->discard_bitset
) {
2464 *error
= "could not allocate discard bitset";
2467 clear_bitset(cache
->discard_bitset
, from_dblock(cache
->discard_nr_blocks
));
2469 cache
->copier
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2470 if (IS_ERR(cache
->copier
)) {
2471 *error
= "could not create kcopyd client";
2472 r
= PTR_ERR(cache
->copier
);
2476 cache
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
2478 *error
= "could not create workqueue for metadata object";
2481 INIT_WORK(&cache
->worker
, do_worker
);
2482 INIT_DELAYED_WORK(&cache
->waker
, do_waker
);
2483 cache
->last_commit_jiffies
= jiffies
;
2485 cache
->prison
= dm_bio_prison_create();
2486 if (!cache
->prison
) {
2487 *error
= "could not create bio prison";
2491 cache
->all_io_ds
= dm_deferred_set_create();
2492 if (!cache
->all_io_ds
) {
2493 *error
= "could not create all_io deferred set";
2497 cache
->migration_pool
= mempool_create_slab_pool(MIGRATION_POOL_SIZE
,
2499 if (!cache
->migration_pool
) {
2500 *error
= "Error creating cache's migration mempool";
2504 cache
->need_tick_bio
= true;
2505 cache
->sized
= false;
2506 cache
->invalidate
= false;
2507 cache
->commit_requested
= false;
2508 cache
->loaded_mappings
= false;
2509 cache
->loaded_discards
= false;
2513 atomic_set(&cache
->stats
.demotion
, 0);
2514 atomic_set(&cache
->stats
.promotion
, 0);
2515 atomic_set(&cache
->stats
.copies_avoided
, 0);
2516 atomic_set(&cache
->stats
.cache_cell_clash
, 0);
2517 atomic_set(&cache
->stats
.commit_count
, 0);
2518 atomic_set(&cache
->stats
.discard_count
, 0);
2520 spin_lock_init(&cache
->invalidation_lock
);
2521 INIT_LIST_HEAD(&cache
->invalidation_requests
);
2531 static int copy_ctr_args(struct cache
*cache
, int argc
, const char **argv
)
2536 copy
= kcalloc(argc
, sizeof(*copy
), GFP_KERNEL
);
2539 for (i
= 0; i
< argc
; i
++) {
2540 copy
[i
] = kstrdup(argv
[i
], GFP_KERNEL
);
2549 cache
->nr_ctr_args
= argc
;
2550 cache
->ctr_args
= copy
;
2555 static int cache_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2558 struct cache_args
*ca
;
2559 struct cache
*cache
= NULL
;
2561 ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
2563 ti
->error
= "Error allocating memory for cache";
2568 r
= parse_cache_args(ca
, argc
, argv
, &ti
->error
);
2572 r
= cache_create(ca
, &cache
);
2576 r
= copy_ctr_args(cache
, argc
- 3, (const char **)argv
+ 3);
2582 ti
->private = cache
;
2585 destroy_cache_args(ca
);
2589 static int __cache_map(struct cache
*cache
, struct bio
*bio
, struct dm_bio_prison_cell
**cell
)
2592 dm_oblock_t block
= get_bio_block(cache
, bio
);
2593 size_t pb_data_size
= get_per_bio_data_size(cache
);
2594 bool can_migrate
= false;
2595 bool discarded_block
;
2596 struct policy_result lookup_result
;
2597 struct per_bio_data
*pb
= init_per_bio_data(bio
, pb_data_size
);
2599 if (unlikely(from_oblock(block
) >= from_oblock(cache
->origin_blocks
))) {
2601 * This can only occur if the io goes to a partial block at
2602 * the end of the origin device. We don't cache these.
2603 * Just remap to the origin and carry on.
2605 remap_to_origin(cache
, bio
);
2606 return DM_MAPIO_REMAPPED
;
2609 if (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
| REQ_DISCARD
)) {
2610 defer_bio(cache
, bio
);
2611 return DM_MAPIO_SUBMITTED
;
2615 * Check to see if that block is currently migrating.
2617 *cell
= alloc_prison_cell(cache
);
2619 defer_bio(cache
, bio
);
2620 return DM_MAPIO_SUBMITTED
;
2623 r
= bio_detain(cache
, block
, bio
, *cell
,
2624 (cell_free_fn
) free_prison_cell
,
2628 defer_bio(cache
, bio
);
2630 return DM_MAPIO_SUBMITTED
;
2633 discarded_block
= is_discarded_oblock(cache
, block
);
2635 r
= policy_map(cache
->policy
, block
, false, can_migrate
, discarded_block
,
2636 bio
, &lookup_result
);
2637 if (r
== -EWOULDBLOCK
) {
2638 cell_defer(cache
, *cell
, true);
2639 return DM_MAPIO_SUBMITTED
;
2642 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r
);
2643 cell_defer(cache
, *cell
, false);
2645 return DM_MAPIO_SUBMITTED
;
2648 r
= DM_MAPIO_REMAPPED
;
2649 switch (lookup_result
.op
) {
2651 if (passthrough_mode(&cache
->features
)) {
2652 if (bio_data_dir(bio
) == WRITE
) {
2654 * We need to invalidate this block, so
2655 * defer for the worker thread.
2657 cell_defer(cache
, *cell
, true);
2658 r
= DM_MAPIO_SUBMITTED
;
2661 inc_miss_counter(cache
, bio
);
2662 remap_to_origin_clear_discard(cache
, bio
, block
);
2666 inc_hit_counter(cache
, bio
);
2667 if (bio_data_dir(bio
) == WRITE
&& writethrough_mode(&cache
->features
) &&
2668 !is_dirty(cache
, lookup_result
.cblock
))
2669 remap_to_origin_then_cache(cache
, bio
, block
, lookup_result
.cblock
);
2671 remap_to_cache_dirty(cache
, bio
, block
, lookup_result
.cblock
);
2676 inc_miss_counter(cache
, bio
);
2677 if (pb
->req_nr
!= 0) {
2679 * This is a duplicate writethrough io that is no
2680 * longer needed because the block has been demoted.
2683 cell_defer(cache
, *cell
, false);
2684 r
= DM_MAPIO_SUBMITTED
;
2687 remap_to_origin_clear_discard(cache
, bio
, block
);
2692 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__
,
2693 (unsigned) lookup_result
.op
);
2694 cell_defer(cache
, *cell
, false);
2696 r
= DM_MAPIO_SUBMITTED
;
2702 static int cache_map(struct dm_target
*ti
, struct bio
*bio
)
2705 struct dm_bio_prison_cell
*cell
= NULL
;
2706 struct cache
*cache
= ti
->private;
2708 r
= __cache_map(cache
, bio
, &cell
);
2709 if (r
== DM_MAPIO_REMAPPED
&& cell
) {
2710 inc_ds(cache
, bio
, cell
);
2711 cell_defer(cache
, cell
, false);
2717 static int cache_end_io(struct dm_target
*ti
, struct bio
*bio
, int error
)
2719 struct cache
*cache
= ti
->private;
2720 unsigned long flags
;
2721 size_t pb_data_size
= get_per_bio_data_size(cache
);
2722 struct per_bio_data
*pb
= get_per_bio_data(bio
, pb_data_size
);
2725 policy_tick(cache
->policy
);
2727 spin_lock_irqsave(&cache
->lock
, flags
);
2728 cache
->need_tick_bio
= true;
2729 spin_unlock_irqrestore(&cache
->lock
, flags
);
2732 check_for_quiesced_migrations(cache
, pb
);
2737 static int write_dirty_bitset(struct cache
*cache
)
2741 for (i
= 0; i
< from_cblock(cache
->cache_size
); i
++) {
2742 r
= dm_cache_set_dirty(cache
->cmd
, to_cblock(i
),
2743 is_dirty(cache
, to_cblock(i
)));
2751 static int write_discard_bitset(struct cache
*cache
)
2755 r
= dm_cache_discard_bitset_resize(cache
->cmd
, cache
->discard_block_size
,
2756 cache
->discard_nr_blocks
);
2758 DMERR("could not resize on-disk discard bitset");
2762 for (i
= 0; i
< from_dblock(cache
->discard_nr_blocks
); i
++) {
2763 r
= dm_cache_set_discard(cache
->cmd
, to_dblock(i
),
2764 is_discarded(cache
, to_dblock(i
)));
2773 * returns true on success
2775 static bool sync_metadata(struct cache
*cache
)
2779 r1
= write_dirty_bitset(cache
);
2781 DMERR("could not write dirty bitset");
2783 r2
= write_discard_bitset(cache
);
2785 DMERR("could not write discard bitset");
2789 r3
= dm_cache_write_hints(cache
->cmd
, cache
->policy
);
2791 DMERR("could not write hints");
2794 * If writing the above metadata failed, we still commit, but don't
2795 * set the clean shutdown flag. This will effectively force every
2796 * dirty bit to be set on reload.
2798 r4
= dm_cache_commit(cache
->cmd
, !r1
&& !r2
&& !r3
);
2800 DMERR("could not write cache metadata. Data loss may occur.");
2802 return !r1
&& !r2
&& !r3
&& !r4
;
2805 static void cache_postsuspend(struct dm_target
*ti
)
2807 struct cache
*cache
= ti
->private;
2809 start_quiescing(cache
);
2810 wait_for_migrations(cache
);
2812 requeue_deferred_io(cache
);
2813 stop_quiescing(cache
);
2815 (void) sync_metadata(cache
);
2818 static int load_mapping(void *context
, dm_oblock_t oblock
, dm_cblock_t cblock
,
2819 bool dirty
, uint32_t hint
, bool hint_valid
)
2822 struct cache
*cache
= context
;
2824 r
= policy_load_mapping(cache
->policy
, oblock
, cblock
, hint
, hint_valid
);
2829 set_dirty(cache
, oblock
, cblock
);
2831 clear_dirty(cache
, oblock
, cblock
);
2837 * The discard block size in the on disk metadata is not
2838 * neccessarily the same as we're currently using. So we have to
2839 * be careful to only set the discarded attribute if we know it
2840 * covers a complete block of the new size.
2842 struct discard_load_info
{
2843 struct cache
*cache
;
2846 * These blocks are sized using the on disk dblock size, rather
2847 * than the current one.
2849 dm_block_t block_size
;
2850 dm_block_t discard_begin
, discard_end
;
2853 static void discard_load_info_init(struct cache
*cache
,
2854 struct discard_load_info
*li
)
2857 li
->discard_begin
= li
->discard_end
= 0;
2860 static void set_discard_range(struct discard_load_info
*li
)
2864 if (li
->discard_begin
== li
->discard_end
)
2868 * Convert to sectors.
2870 b
= li
->discard_begin
* li
->block_size
;
2871 e
= li
->discard_end
* li
->block_size
;
2874 * Then convert back to the current dblock size.
2876 b
= dm_sector_div_up(b
, li
->cache
->discard_block_size
);
2877 sector_div(e
, li
->cache
->discard_block_size
);
2880 * The origin may have shrunk, so we need to check we're still in
2883 if (e
> from_dblock(li
->cache
->discard_nr_blocks
))
2884 e
= from_dblock(li
->cache
->discard_nr_blocks
);
2887 set_discard(li
->cache
, to_dblock(b
));
2890 static int load_discard(void *context
, sector_t discard_block_size
,
2891 dm_dblock_t dblock
, bool discard
)
2893 struct discard_load_info
*li
= context
;
2895 li
->block_size
= discard_block_size
;
2898 if (from_dblock(dblock
) == li
->discard_end
)
2900 * We're already in a discard range, just extend it.
2902 li
->discard_end
= li
->discard_end
+ 1ULL;
2906 * Emit the old range and start a new one.
2908 set_discard_range(li
);
2909 li
->discard_begin
= from_dblock(dblock
);
2910 li
->discard_end
= li
->discard_begin
+ 1ULL;
2913 set_discard_range(li
);
2914 li
->discard_begin
= li
->discard_end
= 0;
2920 static dm_cblock_t
get_cache_dev_size(struct cache
*cache
)
2922 sector_t size
= get_dev_size(cache
->cache_dev
);
2923 (void) sector_div(size
, cache
->sectors_per_block
);
2924 return to_cblock(size
);
2927 static bool can_resize(struct cache
*cache
, dm_cblock_t new_size
)
2929 if (from_cblock(new_size
) > from_cblock(cache
->cache_size
))
2933 * We can't drop a dirty block when shrinking the cache.
2935 while (from_cblock(new_size
) < from_cblock(cache
->cache_size
)) {
2936 new_size
= to_cblock(from_cblock(new_size
) + 1);
2937 if (is_dirty(cache
, new_size
)) {
2938 DMERR("unable to shrink cache; cache block %llu is dirty",
2939 (unsigned long long) from_cblock(new_size
));
2947 static int resize_cache_dev(struct cache
*cache
, dm_cblock_t new_size
)
2951 r
= dm_cache_resize(cache
->cmd
, new_size
);
2953 DMERR("could not resize cache metadata");
2957 set_cache_size(cache
, new_size
);
2962 static int cache_preresume(struct dm_target
*ti
)
2965 struct cache
*cache
= ti
->private;
2966 dm_cblock_t csize
= get_cache_dev_size(cache
);
2969 * Check to see if the cache has resized.
2971 if (!cache
->sized
) {
2972 r
= resize_cache_dev(cache
, csize
);
2976 cache
->sized
= true;
2978 } else if (csize
!= cache
->cache_size
) {
2979 if (!can_resize(cache
, csize
))
2982 r
= resize_cache_dev(cache
, csize
);
2987 if (!cache
->loaded_mappings
) {
2988 r
= dm_cache_load_mappings(cache
->cmd
, cache
->policy
,
2989 load_mapping
, cache
);
2991 DMERR("could not load cache mappings");
2995 cache
->loaded_mappings
= true;
2998 if (!cache
->loaded_discards
) {
2999 struct discard_load_info li
;
3002 * The discard bitset could have been resized, or the
3003 * discard block size changed. To be safe we start by
3004 * setting every dblock to not discarded.
3006 clear_bitset(cache
->discard_bitset
, from_dblock(cache
->discard_nr_blocks
));
3008 discard_load_info_init(cache
, &li
);
3009 r
= dm_cache_load_discards(cache
->cmd
, load_discard
, &li
);
3011 DMERR("could not load origin discards");
3014 set_discard_range(&li
);
3016 cache
->loaded_discards
= true;
3022 static void cache_resume(struct dm_target
*ti
)
3024 struct cache
*cache
= ti
->private;
3026 cache
->need_tick_bio
= true;
3027 do_waker(&cache
->waker
.work
);
3033 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
3034 * <cache block size> <#used cache blocks>/<#total cache blocks>
3035 * <#read hits> <#read misses> <#write hits> <#write misses>
3036 * <#demotions> <#promotions> <#dirty>
3037 * <#features> <features>*
3038 * <#core args> <core args>
3039 * <policy name> <#policy args> <policy args>*
3041 static void cache_status(struct dm_target
*ti
, status_type_t type
,
3042 unsigned status_flags
, char *result
, unsigned maxlen
)
3047 dm_block_t nr_free_blocks_metadata
= 0;
3048 dm_block_t nr_blocks_metadata
= 0;
3049 char buf
[BDEVNAME_SIZE
];
3050 struct cache
*cache
= ti
->private;
3051 dm_cblock_t residency
;
3054 case STATUSTYPE_INFO
:
3055 /* Commit to ensure statistics aren't out-of-date */
3056 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
)) {
3057 r
= dm_cache_commit(cache
->cmd
, false);
3059 DMERR("could not commit metadata for accurate status");
3062 r
= dm_cache_get_free_metadata_block_count(cache
->cmd
,
3063 &nr_free_blocks_metadata
);
3065 DMERR("could not get metadata free block count");
3069 r
= dm_cache_get_metadata_dev_size(cache
->cmd
, &nr_blocks_metadata
);
3071 DMERR("could not get metadata device size");
3075 residency
= policy_residency(cache
->policy
);
3077 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
3078 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE
,
3079 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
3080 (unsigned long long)nr_blocks_metadata
,
3081 cache
->sectors_per_block
,
3082 (unsigned long long) from_cblock(residency
),
3083 (unsigned long long) from_cblock(cache
->cache_size
),
3084 (unsigned) atomic_read(&cache
->stats
.read_hit
),
3085 (unsigned) atomic_read(&cache
->stats
.read_miss
),
3086 (unsigned) atomic_read(&cache
->stats
.write_hit
),
3087 (unsigned) atomic_read(&cache
->stats
.write_miss
),
3088 (unsigned) atomic_read(&cache
->stats
.demotion
),
3089 (unsigned) atomic_read(&cache
->stats
.promotion
),
3090 (unsigned long) atomic_read(&cache
->nr_dirty
));
3092 if (writethrough_mode(&cache
->features
))
3093 DMEMIT("1 writethrough ");
3095 else if (passthrough_mode(&cache
->features
))
3096 DMEMIT("1 passthrough ");
3098 else if (writeback_mode(&cache
->features
))
3099 DMEMIT("1 writeback ");
3102 DMERR("internal error: unknown io mode: %d", (int) cache
->features
.io_mode
);
3106 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache
->migration_threshold
);
3108 DMEMIT("%s ", dm_cache_policy_get_name(cache
->policy
));
3110 r
= policy_emit_config_values(cache
->policy
, result
+ sz
, maxlen
- sz
);
3112 DMERR("policy_emit_config_values returned %d", r
);
3117 case STATUSTYPE_TABLE
:
3118 format_dev_t(buf
, cache
->metadata_dev
->bdev
->bd_dev
);
3120 format_dev_t(buf
, cache
->cache_dev
->bdev
->bd_dev
);
3122 format_dev_t(buf
, cache
->origin_dev
->bdev
->bd_dev
);
3125 for (i
= 0; i
< cache
->nr_ctr_args
- 1; i
++)
3126 DMEMIT(" %s", cache
->ctr_args
[i
]);
3127 if (cache
->nr_ctr_args
)
3128 DMEMIT(" %s", cache
->ctr_args
[cache
->nr_ctr_args
- 1]);
3138 * A cache block range can take two forms:
3140 * i) A single cblock, eg. '3456'
3141 * ii) A begin and end cblock with dots between, eg. 123-234
3143 static int parse_cblock_range(struct cache
*cache
, const char *str
,
3144 struct cblock_range
*result
)
3151 * Try and parse form (ii) first.
3153 r
= sscanf(str
, "%llu-%llu%c", &b
, &e
, &dummy
);
3158 result
->begin
= to_cblock(b
);
3159 result
->end
= to_cblock(e
);
3164 * That didn't work, try form (i).
3166 r
= sscanf(str
, "%llu%c", &b
, &dummy
);
3171 result
->begin
= to_cblock(b
);
3172 result
->end
= to_cblock(from_cblock(result
->begin
) + 1u);
3176 DMERR("invalid cblock range '%s'", str
);
3180 static int validate_cblock_range(struct cache
*cache
, struct cblock_range
*range
)
3182 uint64_t b
= from_cblock(range
->begin
);
3183 uint64_t e
= from_cblock(range
->end
);
3184 uint64_t n
= from_cblock(cache
->cache_size
);
3187 DMERR("begin cblock out of range: %llu >= %llu", b
, n
);
3192 DMERR("end cblock out of range: %llu > %llu", e
, n
);
3197 DMERR("invalid cblock range: %llu >= %llu", b
, e
);
3204 static int request_invalidation(struct cache
*cache
, struct cblock_range
*range
)
3206 struct invalidation_request req
;
3208 INIT_LIST_HEAD(&req
.list
);
3209 req
.cblocks
= range
;
3210 atomic_set(&req
.complete
, 0);
3212 init_waitqueue_head(&req
.result_wait
);
3214 spin_lock(&cache
->invalidation_lock
);
3215 list_add(&req
.list
, &cache
->invalidation_requests
);
3216 spin_unlock(&cache
->invalidation_lock
);
3219 wait_event(req
.result_wait
, atomic_read(&req
.complete
));
3223 static int process_invalidate_cblocks_message(struct cache
*cache
, unsigned count
,
3224 const char **cblock_ranges
)
3228 struct cblock_range range
;
3230 if (!passthrough_mode(&cache
->features
)) {
3231 DMERR("cache has to be in passthrough mode for invalidation");
3235 for (i
= 0; i
< count
; i
++) {
3236 r
= parse_cblock_range(cache
, cblock_ranges
[i
], &range
);
3240 r
= validate_cblock_range(cache
, &range
);
3245 * Pass begin and end origin blocks to the worker and wake it.
3247 r
= request_invalidation(cache
, &range
);
3259 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
3261 * The key migration_threshold is supported by the cache target core.
3263 static int cache_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
3265 struct cache
*cache
= ti
->private;
3270 if (!strcasecmp(argv
[0], "invalidate_cblocks"))
3271 return process_invalidate_cblocks_message(cache
, argc
- 1, (const char **) argv
+ 1);
3276 return set_config_value(cache
, argv
[0], argv
[1]);
3279 static int cache_iterate_devices(struct dm_target
*ti
,
3280 iterate_devices_callout_fn fn
, void *data
)
3283 struct cache
*cache
= ti
->private;
3285 r
= fn(ti
, cache
->cache_dev
, 0, get_dev_size(cache
->cache_dev
), data
);
3287 r
= fn(ti
, cache
->origin_dev
, 0, ti
->len
, data
);
3293 * We assume I/O is going to the origin (which is the volume
3294 * more likely to have restrictions e.g. by being striped).
3295 * (Looking up the exact location of the data would be expensive
3296 * and could always be out of date by the time the bio is submitted.)
3298 static int cache_bvec_merge(struct dm_target
*ti
,
3299 struct bvec_merge_data
*bvm
,
3300 struct bio_vec
*biovec
, int max_size
)
3302 struct cache
*cache
= ti
->private;
3303 struct request_queue
*q
= bdev_get_queue(cache
->origin_dev
->bdev
);
3305 if (!q
->merge_bvec_fn
)
3308 bvm
->bi_bdev
= cache
->origin_dev
->bdev
;
3309 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
3312 static void set_discard_limits(struct cache
*cache
, struct queue_limits
*limits
)
3315 * FIXME: these limits may be incompatible with the cache device
3317 limits
->max_discard_sectors
= min_t(sector_t
, cache
->discard_block_size
* 1024,
3318 cache
->origin_sectors
);
3319 limits
->discard_granularity
= cache
->discard_block_size
<< SECTOR_SHIFT
;
3322 static void cache_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
3324 struct cache
*cache
= ti
->private;
3325 uint64_t io_opt_sectors
= limits
->io_opt
>> SECTOR_SHIFT
;
3328 * If the system-determined stacked limits are compatible with the
3329 * cache's blocksize (io_opt is a factor) do not override them.
3331 if (io_opt_sectors
< cache
->sectors_per_block
||
3332 do_div(io_opt_sectors
, cache
->sectors_per_block
)) {
3333 blk_limits_io_min(limits
, cache
->sectors_per_block
<< SECTOR_SHIFT
);
3334 blk_limits_io_opt(limits
, cache
->sectors_per_block
<< SECTOR_SHIFT
);
3336 set_discard_limits(cache
, limits
);
3339 /*----------------------------------------------------------------*/
3341 static struct target_type cache_target
= {
3343 .version
= {1, 6, 0},
3344 .module
= THIS_MODULE
,
3348 .end_io
= cache_end_io
,
3349 .postsuspend
= cache_postsuspend
,
3350 .preresume
= cache_preresume
,
3351 .resume
= cache_resume
,
3352 .status
= cache_status
,
3353 .message
= cache_message
,
3354 .iterate_devices
= cache_iterate_devices
,
3355 .merge
= cache_bvec_merge
,
3356 .io_hints
= cache_io_hints
,
3359 static int __init
dm_cache_init(void)
3363 r
= dm_register_target(&cache_target
);
3365 DMERR("cache target registration failed: %d", r
);
3369 migration_cache
= KMEM_CACHE(dm_cache_migration
, 0);
3370 if (!migration_cache
) {
3371 dm_unregister_target(&cache_target
);
3378 static void __exit
dm_cache_exit(void)
3380 dm_unregister_target(&cache_target
);
3381 kmem_cache_destroy(migration_cache
);
3384 module_init(dm_cache_init
);
3385 module_exit(dm_cache_exit
);
3387 MODULE_DESCRIPTION(DM_NAME
" cache target");
3388 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3389 MODULE_LICENSE("GPL");