2 * Copyright (C) 2012 Red Hat. All rights reserved.
4 * This file is released under the GPL.
8 #include "dm-bio-prison.h"
9 #include "dm-bio-record.h"
10 #include "dm-cache-metadata.h"
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/init.h>
15 #include <linux/mempool.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
20 #define DM_MSG_PREFIX "cache"
22 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle
,
23 "A percentage of time allocated for copying to and/or from cache");
25 /*----------------------------------------------------------------*/
30 * oblock: index of an origin block
31 * cblock: index of a cache block
32 * promotion: movement of a block from origin to cache
33 * demotion: movement of a block from cache to origin
34 * migration: movement of a block between the origin and cache device,
38 /*----------------------------------------------------------------*/
40 static size_t bitset_size_in_bytes(unsigned nr_entries
)
42 return sizeof(unsigned long) * dm_div_up(nr_entries
, BITS_PER_LONG
);
45 static unsigned long *alloc_bitset(unsigned nr_entries
)
47 size_t s
= bitset_size_in_bytes(nr_entries
);
51 static void clear_bitset(void *bitset
, unsigned nr_entries
)
53 size_t s
= bitset_size_in_bytes(nr_entries
);
57 static void free_bitset(unsigned long *bits
)
62 /*----------------------------------------------------------------*/
65 * There are a couple of places where we let a bio run, but want to do some
66 * work before calling its endio function. We do this by temporarily
67 * changing the endio fn.
70 bio_end_io_t
*bi_end_io
;
74 static void dm_hook_bio(struct dm_hook_info
*h
, struct bio
*bio
,
75 bio_end_io_t
*bi_end_io
, void *bi_private
)
77 h
->bi_end_io
= bio
->bi_end_io
;
78 h
->bi_private
= bio
->bi_private
;
80 bio
->bi_end_io
= bi_end_io
;
81 bio
->bi_private
= bi_private
;
84 static void dm_unhook_bio(struct dm_hook_info
*h
, struct bio
*bio
)
86 bio
->bi_end_io
= h
->bi_end_io
;
87 bio
->bi_private
= h
->bi_private
;
90 /*----------------------------------------------------------------*/
92 #define PRISON_CELLS 1024
93 #define MIGRATION_POOL_SIZE 128
94 #define COMMIT_PERIOD HZ
95 #define MIGRATION_COUNT_WINDOW 10
98 * The block size of the device holding cache data must be
99 * between 32KB and 1GB.
101 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
102 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
105 * FIXME: the cache is read/write for the time being.
107 enum cache_metadata_mode
{
108 CM_WRITE
, /* metadata may be changed */
109 CM_READ_ONLY
, /* metadata may not be changed */
114 * Data is written to cached blocks only. These blocks are marked
115 * dirty. If you lose the cache device you will lose data.
116 * Potential performance increase for both reads and writes.
121 * Data is written to both cache and origin. Blocks are never
122 * dirty. Potential performance benfit for reads only.
127 * A degraded mode useful for various cache coherency situations
128 * (eg, rolling back snapshots). Reads and writes always go to the
129 * origin. If a write goes to a cached oblock, then the cache
130 * block is invalidated.
135 struct cache_features
{
136 enum cache_metadata_mode mode
;
137 enum cache_io_mode io_mode
;
147 atomic_t copies_avoided
;
148 atomic_t cache_cell_clash
;
149 atomic_t commit_count
;
150 atomic_t discard_count
;
154 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
155 * the one-past-the-end value.
157 struct cblock_range
{
162 struct invalidation_request
{
163 struct list_head list
;
164 struct cblock_range
*cblocks
;
169 wait_queue_head_t result_wait
;
173 struct dm_target
*ti
;
174 struct dm_target_callbacks callbacks
;
176 struct dm_cache_metadata
*cmd
;
179 * Metadata is written to this device.
181 struct dm_dev
*metadata_dev
;
184 * The slower of the two data devices. Typically a spindle.
186 struct dm_dev
*origin_dev
;
189 * The faster of the two data devices. Typically an SSD.
191 struct dm_dev
*cache_dev
;
194 * Size of the origin device in _complete_ blocks and native sectors.
196 dm_oblock_t origin_blocks
;
197 sector_t origin_sectors
;
200 * Size of the cache device in blocks.
202 dm_cblock_t cache_size
;
205 * Fields for converting from sectors to blocks.
207 uint32_t sectors_per_block
;
208 int sectors_per_block_shift
;
211 struct bio_list deferred_bios
;
212 struct bio_list deferred_flush_bios
;
213 struct bio_list deferred_writethrough_bios
;
214 struct list_head quiesced_migrations
;
215 struct list_head completed_migrations
;
216 struct list_head need_commit_migrations
;
217 sector_t migration_threshold
;
218 wait_queue_head_t migration_wait
;
219 atomic_t nr_migrations
;
221 wait_queue_head_t quiescing_wait
;
223 atomic_t quiescing_ack
;
226 * cache_size entries, dirty if set
228 dm_cblock_t nr_dirty
;
229 unsigned long *dirty_bitset
;
232 * origin_blocks entries, discarded if set.
234 dm_dblock_t discard_nr_blocks
;
235 unsigned long *discard_bitset
;
236 uint32_t discard_block_size
; /* a power of 2 times sectors per block */
239 * Rather than reconstructing the table line for the status we just
240 * save it and regurgitate.
242 unsigned nr_ctr_args
;
243 const char **ctr_args
;
245 struct dm_kcopyd_client
*copier
;
246 struct workqueue_struct
*wq
;
247 struct work_struct worker
;
249 struct delayed_work waker
;
250 unsigned long last_commit_jiffies
;
252 struct dm_bio_prison
*prison
;
253 struct dm_deferred_set
*all_io_ds
;
255 mempool_t
*migration_pool
;
256 struct dm_cache_migration
*next_migration
;
258 struct dm_cache_policy
*policy
;
259 unsigned policy_nr_args
;
261 bool need_tick_bio
:1;
264 bool commit_requested
:1;
265 bool loaded_mappings
:1;
266 bool loaded_discards
:1;
269 * Cache features such as write-through.
271 struct cache_features features
;
273 struct cache_stats stats
;
276 * Invalidation fields.
278 spinlock_t invalidation_lock
;
279 struct list_head invalidation_requests
;
282 struct per_bio_data
{
285 struct dm_deferred_entry
*all_io_entry
;
288 * writethrough fields. These MUST remain at the end of this
289 * structure and the 'cache' member must be the first as it
290 * is used to determine the offset of the writethrough fields.
294 struct dm_hook_info hook_info
;
295 struct dm_bio_details bio_details
;
298 struct dm_cache_migration
{
299 struct list_head list
;
302 unsigned long start_jiffies
;
303 dm_oblock_t old_oblock
;
304 dm_oblock_t new_oblock
;
311 bool requeue_holder
:1;
314 struct dm_bio_prison_cell
*old_ocell
;
315 struct dm_bio_prison_cell
*new_ocell
;
319 * Processing a bio in the worker thread may require these memory
320 * allocations. We prealloc to avoid deadlocks (the same worker thread
321 * frees them back to the mempool).
324 struct dm_cache_migration
*mg
;
325 struct dm_bio_prison_cell
*cell1
;
326 struct dm_bio_prison_cell
*cell2
;
329 static void wake_worker(struct cache
*cache
)
331 queue_work(cache
->wq
, &cache
->worker
);
334 /*----------------------------------------------------------------*/
336 static struct dm_bio_prison_cell
*alloc_prison_cell(struct cache
*cache
)
338 /* FIXME: change to use a local slab. */
339 return dm_bio_prison_alloc_cell(cache
->prison
, GFP_NOWAIT
);
342 static void free_prison_cell(struct cache
*cache
, struct dm_bio_prison_cell
*cell
)
344 dm_bio_prison_free_cell(cache
->prison
, cell
);
347 static int prealloc_data_structs(struct cache
*cache
, struct prealloc
*p
)
350 p
->mg
= mempool_alloc(cache
->migration_pool
, GFP_NOWAIT
);
356 p
->cell1
= alloc_prison_cell(cache
);
362 p
->cell2
= alloc_prison_cell(cache
);
370 static void prealloc_free_structs(struct cache
*cache
, struct prealloc
*p
)
373 free_prison_cell(cache
, p
->cell2
);
376 free_prison_cell(cache
, p
->cell1
);
379 mempool_free(p
->mg
, cache
->migration_pool
);
382 static struct dm_cache_migration
*prealloc_get_migration(struct prealloc
*p
)
384 struct dm_cache_migration
*mg
= p
->mg
;
393 * You must have a cell within the prealloc struct to return. If not this
394 * function will BUG() rather than returning NULL.
396 static struct dm_bio_prison_cell
*prealloc_get_cell(struct prealloc
*p
)
398 struct dm_bio_prison_cell
*r
= NULL
;
404 } else if (p
->cell2
) {
414 * You can't have more than two cells in a prealloc struct. BUG() will be
415 * called if you try and overfill.
417 static void prealloc_put_cell(struct prealloc
*p
, struct dm_bio_prison_cell
*cell
)
429 /*----------------------------------------------------------------*/
431 static void build_key(dm_oblock_t oblock
, struct dm_cell_key
*key
)
435 key
->block
= from_oblock(oblock
);
439 * The caller hands in a preallocated cell, and a free function for it.
440 * The cell will be freed if there's an error, or if it wasn't used because
441 * a cell with that key already exists.
443 typedef void (*cell_free_fn
)(void *context
, struct dm_bio_prison_cell
*cell
);
445 static int bio_detain(struct cache
*cache
, dm_oblock_t oblock
,
446 struct bio
*bio
, struct dm_bio_prison_cell
*cell_prealloc
,
447 cell_free_fn free_fn
, void *free_context
,
448 struct dm_bio_prison_cell
**cell_result
)
451 struct dm_cell_key key
;
453 build_key(oblock
, &key
);
454 r
= dm_bio_detain(cache
->prison
, &key
, bio
, cell_prealloc
, cell_result
);
456 free_fn(free_context
, cell_prealloc
);
461 static int get_cell(struct cache
*cache
,
463 struct prealloc
*structs
,
464 struct dm_bio_prison_cell
**cell_result
)
467 struct dm_cell_key key
;
468 struct dm_bio_prison_cell
*cell_prealloc
;
470 cell_prealloc
= prealloc_get_cell(structs
);
472 build_key(oblock
, &key
);
473 r
= dm_get_cell(cache
->prison
, &key
, cell_prealloc
, cell_result
);
475 prealloc_put_cell(structs
, cell_prealloc
);
480 /*----------------------------------------------------------------*/
482 static bool is_dirty(struct cache
*cache
, dm_cblock_t b
)
484 return test_bit(from_cblock(b
), cache
->dirty_bitset
);
487 static void set_dirty(struct cache
*cache
, dm_oblock_t oblock
, dm_cblock_t cblock
)
489 if (!test_and_set_bit(from_cblock(cblock
), cache
->dirty_bitset
)) {
490 cache
->nr_dirty
= to_cblock(from_cblock(cache
->nr_dirty
) + 1);
491 policy_set_dirty(cache
->policy
, oblock
);
495 static void clear_dirty(struct cache
*cache
, dm_oblock_t oblock
, dm_cblock_t cblock
)
497 if (test_and_clear_bit(from_cblock(cblock
), cache
->dirty_bitset
)) {
498 policy_clear_dirty(cache
->policy
, oblock
);
499 cache
->nr_dirty
= to_cblock(from_cblock(cache
->nr_dirty
) - 1);
500 if (!from_cblock(cache
->nr_dirty
))
501 dm_table_event(cache
->ti
->table
);
505 /*----------------------------------------------------------------*/
507 static bool block_size_is_power_of_two(struct cache
*cache
)
509 return cache
->sectors_per_block_shift
>= 0;
512 /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
513 #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
516 static dm_block_t
block_div(dm_block_t b
, uint32_t n
)
523 static dm_dblock_t
oblock_to_dblock(struct cache
*cache
, dm_oblock_t oblock
)
525 uint32_t discard_blocks
= cache
->discard_block_size
;
526 dm_block_t b
= from_oblock(oblock
);
528 if (!block_size_is_power_of_two(cache
))
529 discard_blocks
= discard_blocks
/ cache
->sectors_per_block
;
531 discard_blocks
>>= cache
->sectors_per_block_shift
;
533 b
= block_div(b
, discard_blocks
);
538 static void set_discard(struct cache
*cache
, dm_dblock_t b
)
542 atomic_inc(&cache
->stats
.discard_count
);
544 spin_lock_irqsave(&cache
->lock
, flags
);
545 set_bit(from_dblock(b
), cache
->discard_bitset
);
546 spin_unlock_irqrestore(&cache
->lock
, flags
);
549 static void clear_discard(struct cache
*cache
, dm_dblock_t b
)
553 spin_lock_irqsave(&cache
->lock
, flags
);
554 clear_bit(from_dblock(b
), cache
->discard_bitset
);
555 spin_unlock_irqrestore(&cache
->lock
, flags
);
558 static bool is_discarded(struct cache
*cache
, dm_dblock_t b
)
563 spin_lock_irqsave(&cache
->lock
, flags
);
564 r
= test_bit(from_dblock(b
), cache
->discard_bitset
);
565 spin_unlock_irqrestore(&cache
->lock
, flags
);
570 static bool is_discarded_oblock(struct cache
*cache
, dm_oblock_t b
)
575 spin_lock_irqsave(&cache
->lock
, flags
);
576 r
= test_bit(from_dblock(oblock_to_dblock(cache
, b
)),
577 cache
->discard_bitset
);
578 spin_unlock_irqrestore(&cache
->lock
, flags
);
583 /*----------------------------------------------------------------*/
585 static void load_stats(struct cache
*cache
)
587 struct dm_cache_statistics stats
;
589 dm_cache_metadata_get_stats(cache
->cmd
, &stats
);
590 atomic_set(&cache
->stats
.read_hit
, stats
.read_hits
);
591 atomic_set(&cache
->stats
.read_miss
, stats
.read_misses
);
592 atomic_set(&cache
->stats
.write_hit
, stats
.write_hits
);
593 atomic_set(&cache
->stats
.write_miss
, stats
.write_misses
);
596 static void save_stats(struct cache
*cache
)
598 struct dm_cache_statistics stats
;
600 stats
.read_hits
= atomic_read(&cache
->stats
.read_hit
);
601 stats
.read_misses
= atomic_read(&cache
->stats
.read_miss
);
602 stats
.write_hits
= atomic_read(&cache
->stats
.write_hit
);
603 stats
.write_misses
= atomic_read(&cache
->stats
.write_miss
);
605 dm_cache_metadata_set_stats(cache
->cmd
, &stats
);
608 /*----------------------------------------------------------------
610 *--------------------------------------------------------------*/
613 * If using writeback, leave out struct per_bio_data's writethrough fields.
615 #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
616 #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
618 static bool writethrough_mode(struct cache_features
*f
)
620 return f
->io_mode
== CM_IO_WRITETHROUGH
;
623 static bool writeback_mode(struct cache_features
*f
)
625 return f
->io_mode
== CM_IO_WRITEBACK
;
628 static bool passthrough_mode(struct cache_features
*f
)
630 return f
->io_mode
== CM_IO_PASSTHROUGH
;
633 static size_t get_per_bio_data_size(struct cache
*cache
)
635 return writethrough_mode(&cache
->features
) ? PB_DATA_SIZE_WT
: PB_DATA_SIZE_WB
;
638 static struct per_bio_data
*get_per_bio_data(struct bio
*bio
, size_t data_size
)
640 struct per_bio_data
*pb
= dm_per_bio_data(bio
, data_size
);
645 static struct per_bio_data
*init_per_bio_data(struct bio
*bio
, size_t data_size
)
647 struct per_bio_data
*pb
= get_per_bio_data(bio
, data_size
);
650 pb
->req_nr
= dm_bio_get_target_bio_nr(bio
);
651 pb
->all_io_entry
= NULL
;
656 /*----------------------------------------------------------------
658 *--------------------------------------------------------------*/
659 static void remap_to_origin(struct cache
*cache
, struct bio
*bio
)
661 bio
->bi_bdev
= cache
->origin_dev
->bdev
;
664 static void remap_to_cache(struct cache
*cache
, struct bio
*bio
,
667 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
669 bio
->bi_bdev
= cache
->cache_dev
->bdev
;
670 if (!block_size_is_power_of_two(cache
))
671 bio
->bi_iter
.bi_sector
=
672 (from_cblock(cblock
) * cache
->sectors_per_block
) +
673 sector_div(bi_sector
, cache
->sectors_per_block
);
675 bio
->bi_iter
.bi_sector
=
676 (from_cblock(cblock
) << cache
->sectors_per_block_shift
) |
677 (bi_sector
& (cache
->sectors_per_block
- 1));
680 static void check_if_tick_bio_needed(struct cache
*cache
, struct bio
*bio
)
683 size_t pb_data_size
= get_per_bio_data_size(cache
);
684 struct per_bio_data
*pb
= get_per_bio_data(bio
, pb_data_size
);
686 spin_lock_irqsave(&cache
->lock
, flags
);
687 if (cache
->need_tick_bio
&&
688 !(bio
->bi_rw
& (REQ_FUA
| REQ_FLUSH
| REQ_DISCARD
))) {
690 cache
->need_tick_bio
= false;
692 spin_unlock_irqrestore(&cache
->lock
, flags
);
695 static void remap_to_origin_clear_discard(struct cache
*cache
, struct bio
*bio
,
698 check_if_tick_bio_needed(cache
, bio
);
699 remap_to_origin(cache
, bio
);
700 if (bio_data_dir(bio
) == WRITE
)
701 clear_discard(cache
, oblock_to_dblock(cache
, oblock
));
704 static void remap_to_cache_dirty(struct cache
*cache
, struct bio
*bio
,
705 dm_oblock_t oblock
, dm_cblock_t cblock
)
707 check_if_tick_bio_needed(cache
, bio
);
708 remap_to_cache(cache
, bio
, cblock
);
709 if (bio_data_dir(bio
) == WRITE
) {
710 set_dirty(cache
, oblock
, cblock
);
711 clear_discard(cache
, oblock_to_dblock(cache
, oblock
));
715 static dm_oblock_t
get_bio_block(struct cache
*cache
, struct bio
*bio
)
717 sector_t block_nr
= bio
->bi_iter
.bi_sector
;
719 if (!block_size_is_power_of_two(cache
))
720 (void) sector_div(block_nr
, cache
->sectors_per_block
);
722 block_nr
>>= cache
->sectors_per_block_shift
;
724 return to_oblock(block_nr
);
727 static int bio_triggers_commit(struct cache
*cache
, struct bio
*bio
)
729 return bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
732 static void issue(struct cache
*cache
, struct bio
*bio
)
736 if (!bio_triggers_commit(cache
, bio
)) {
737 generic_make_request(bio
);
742 * Batch together any bios that trigger commits and then issue a
743 * single commit for them in do_worker().
745 spin_lock_irqsave(&cache
->lock
, flags
);
746 cache
->commit_requested
= true;
747 bio_list_add(&cache
->deferred_flush_bios
, bio
);
748 spin_unlock_irqrestore(&cache
->lock
, flags
);
751 static void defer_writethrough_bio(struct cache
*cache
, struct bio
*bio
)
755 spin_lock_irqsave(&cache
->lock
, flags
);
756 bio_list_add(&cache
->deferred_writethrough_bios
, bio
);
757 spin_unlock_irqrestore(&cache
->lock
, flags
);
762 static void writethrough_endio(struct bio
*bio
, int err
)
764 struct per_bio_data
*pb
= get_per_bio_data(bio
, PB_DATA_SIZE_WT
);
766 dm_unhook_bio(&pb
->hook_info
, bio
);
773 dm_bio_restore(&pb
->bio_details
, bio
);
774 remap_to_cache(pb
->cache
, bio
, pb
->cblock
);
777 * We can't issue this bio directly, since we're in interrupt
778 * context. So it gets put on a bio list for processing by the
781 defer_writethrough_bio(pb
->cache
, bio
);
785 * When running in writethrough mode we need to send writes to clean blocks
786 * to both the cache and origin devices. In future we'd like to clone the
787 * bio and send them in parallel, but for now we're doing them in
788 * series as this is easier.
790 static void remap_to_origin_then_cache(struct cache
*cache
, struct bio
*bio
,
791 dm_oblock_t oblock
, dm_cblock_t cblock
)
793 struct per_bio_data
*pb
= get_per_bio_data(bio
, PB_DATA_SIZE_WT
);
797 dm_hook_bio(&pb
->hook_info
, bio
, writethrough_endio
, NULL
);
798 dm_bio_record(&pb
->bio_details
, bio
);
800 remap_to_origin_clear_discard(pb
->cache
, bio
, oblock
);
803 /*----------------------------------------------------------------
804 * Migration processing
806 * Migration covers moving data from the origin device to the cache, or
808 *--------------------------------------------------------------*/
809 static void free_migration(struct dm_cache_migration
*mg
)
811 mempool_free(mg
, mg
->cache
->migration_pool
);
814 static void inc_nr_migrations(struct cache
*cache
)
816 atomic_inc(&cache
->nr_migrations
);
819 static void dec_nr_migrations(struct cache
*cache
)
821 atomic_dec(&cache
->nr_migrations
);
824 * Wake the worker in case we're suspending the target.
826 wake_up(&cache
->migration_wait
);
829 static void __cell_defer(struct cache
*cache
, struct dm_bio_prison_cell
*cell
,
832 (holder
? dm_cell_release
: dm_cell_release_no_holder
)
833 (cache
->prison
, cell
, &cache
->deferred_bios
);
834 free_prison_cell(cache
, cell
);
837 static void cell_defer(struct cache
*cache
, struct dm_bio_prison_cell
*cell
,
842 spin_lock_irqsave(&cache
->lock
, flags
);
843 __cell_defer(cache
, cell
, holder
);
844 spin_unlock_irqrestore(&cache
->lock
, flags
);
849 static void cleanup_migration(struct dm_cache_migration
*mg
)
851 struct cache
*cache
= mg
->cache
;
853 dec_nr_migrations(cache
);
856 static void migration_failure(struct dm_cache_migration
*mg
)
858 struct cache
*cache
= mg
->cache
;
861 DMWARN_LIMIT("writeback failed; couldn't copy block");
862 set_dirty(cache
, mg
->old_oblock
, mg
->cblock
);
863 cell_defer(cache
, mg
->old_ocell
, false);
865 } else if (mg
->demote
) {
866 DMWARN_LIMIT("demotion failed; couldn't copy block");
867 policy_force_mapping(cache
->policy
, mg
->new_oblock
, mg
->old_oblock
);
869 cell_defer(cache
, mg
->old_ocell
, mg
->promote
? false : true);
871 cell_defer(cache
, mg
->new_ocell
, true);
873 DMWARN_LIMIT("promotion failed; couldn't copy block");
874 policy_remove_mapping(cache
->policy
, mg
->new_oblock
);
875 cell_defer(cache
, mg
->new_ocell
, true);
878 cleanup_migration(mg
);
881 static void migration_success_pre_commit(struct dm_cache_migration
*mg
)
884 struct cache
*cache
= mg
->cache
;
887 cell_defer(cache
, mg
->old_ocell
, false);
888 clear_dirty(cache
, mg
->old_oblock
, mg
->cblock
);
889 cleanup_migration(mg
);
892 } else if (mg
->demote
) {
893 if (dm_cache_remove_mapping(cache
->cmd
, mg
->cblock
)) {
894 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
895 policy_force_mapping(cache
->policy
, mg
->new_oblock
,
898 cell_defer(cache
, mg
->new_ocell
, true);
899 cleanup_migration(mg
);
903 if (dm_cache_insert_mapping(cache
->cmd
, mg
->cblock
, mg
->new_oblock
)) {
904 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
905 policy_remove_mapping(cache
->policy
, mg
->new_oblock
);
906 cleanup_migration(mg
);
911 spin_lock_irqsave(&cache
->lock
, flags
);
912 list_add_tail(&mg
->list
, &cache
->need_commit_migrations
);
913 cache
->commit_requested
= true;
914 spin_unlock_irqrestore(&cache
->lock
, flags
);
917 static void migration_success_post_commit(struct dm_cache_migration
*mg
)
920 struct cache
*cache
= mg
->cache
;
923 DMWARN("writeback unexpectedly triggered commit");
926 } else if (mg
->demote
) {
927 cell_defer(cache
, mg
->old_ocell
, mg
->promote
? false : true);
932 spin_lock_irqsave(&cache
->lock
, flags
);
933 list_add_tail(&mg
->list
, &cache
->quiesced_migrations
);
934 spin_unlock_irqrestore(&cache
->lock
, flags
);
938 policy_remove_mapping(cache
->policy
, mg
->old_oblock
);
939 cleanup_migration(mg
);
943 if (mg
->requeue_holder
)
944 cell_defer(cache
, mg
->new_ocell
, true);
946 bio_endio(mg
->new_ocell
->holder
, 0);
947 cell_defer(cache
, mg
->new_ocell
, false);
949 clear_dirty(cache
, mg
->new_oblock
, mg
->cblock
);
950 cleanup_migration(mg
);
954 static void copy_complete(int read_err
, unsigned long write_err
, void *context
)
957 struct dm_cache_migration
*mg
= (struct dm_cache_migration
*) context
;
958 struct cache
*cache
= mg
->cache
;
960 if (read_err
|| write_err
)
963 spin_lock_irqsave(&cache
->lock
, flags
);
964 list_add_tail(&mg
->list
, &cache
->completed_migrations
);
965 spin_unlock_irqrestore(&cache
->lock
, flags
);
970 static void issue_copy_real(struct dm_cache_migration
*mg
)
973 struct dm_io_region o_region
, c_region
;
974 struct cache
*cache
= mg
->cache
;
976 o_region
.bdev
= cache
->origin_dev
->bdev
;
977 o_region
.count
= cache
->sectors_per_block
;
979 c_region
.bdev
= cache
->cache_dev
->bdev
;
980 c_region
.sector
= from_cblock(mg
->cblock
) * cache
->sectors_per_block
;
981 c_region
.count
= cache
->sectors_per_block
;
983 if (mg
->writeback
|| mg
->demote
) {
985 o_region
.sector
= from_oblock(mg
->old_oblock
) * cache
->sectors_per_block
;
986 r
= dm_kcopyd_copy(cache
->copier
, &c_region
, 1, &o_region
, 0, copy_complete
, mg
);
989 o_region
.sector
= from_oblock(mg
->new_oblock
) * cache
->sectors_per_block
;
990 r
= dm_kcopyd_copy(cache
->copier
, &o_region
, 1, &c_region
, 0, copy_complete
, mg
);
994 DMERR_LIMIT("issuing migration failed");
995 migration_failure(mg
);
999 static void overwrite_endio(struct bio
*bio
, int err
)
1001 struct dm_cache_migration
*mg
= bio
->bi_private
;
1002 struct cache
*cache
= mg
->cache
;
1003 size_t pb_data_size
= get_per_bio_data_size(cache
);
1004 struct per_bio_data
*pb
= get_per_bio_data(bio
, pb_data_size
);
1005 unsigned long flags
;
1010 spin_lock_irqsave(&cache
->lock
, flags
);
1011 list_add_tail(&mg
->list
, &cache
->completed_migrations
);
1012 dm_unhook_bio(&pb
->hook_info
, bio
);
1013 mg
->requeue_holder
= false;
1014 spin_unlock_irqrestore(&cache
->lock
, flags
);
1019 static void issue_overwrite(struct dm_cache_migration
*mg
, struct bio
*bio
)
1021 size_t pb_data_size
= get_per_bio_data_size(mg
->cache
);
1022 struct per_bio_data
*pb
= get_per_bio_data(bio
, pb_data_size
);
1024 dm_hook_bio(&pb
->hook_info
, bio
, overwrite_endio
, mg
);
1025 remap_to_cache_dirty(mg
->cache
, bio
, mg
->new_oblock
, mg
->cblock
);
1026 generic_make_request(bio
);
1029 static bool bio_writes_complete_block(struct cache
*cache
, struct bio
*bio
)
1031 return (bio_data_dir(bio
) == WRITE
) &&
1032 (bio
->bi_iter
.bi_size
== (cache
->sectors_per_block
<< SECTOR_SHIFT
));
1035 static void avoid_copy(struct dm_cache_migration
*mg
)
1037 atomic_inc(&mg
->cache
->stats
.copies_avoided
);
1038 migration_success_pre_commit(mg
);
1041 static void issue_copy(struct dm_cache_migration
*mg
)
1044 struct cache
*cache
= mg
->cache
;
1046 if (mg
->writeback
|| mg
->demote
)
1047 avoid
= !is_dirty(cache
, mg
->cblock
) ||
1048 is_discarded_oblock(cache
, mg
->old_oblock
);
1050 struct bio
*bio
= mg
->new_ocell
->holder
;
1052 avoid
= is_discarded_oblock(cache
, mg
->new_oblock
);
1054 if (!avoid
&& bio_writes_complete_block(cache
, bio
)) {
1055 issue_overwrite(mg
, bio
);
1060 avoid
? avoid_copy(mg
) : issue_copy_real(mg
);
1063 static void complete_migration(struct dm_cache_migration
*mg
)
1066 migration_failure(mg
);
1068 migration_success_pre_commit(mg
);
1071 static void process_migrations(struct cache
*cache
, struct list_head
*head
,
1072 void (*fn
)(struct dm_cache_migration
*))
1074 unsigned long flags
;
1075 struct list_head list
;
1076 struct dm_cache_migration
*mg
, *tmp
;
1078 INIT_LIST_HEAD(&list
);
1079 spin_lock_irqsave(&cache
->lock
, flags
);
1080 list_splice_init(head
, &list
);
1081 spin_unlock_irqrestore(&cache
->lock
, flags
);
1083 list_for_each_entry_safe(mg
, tmp
, &list
, list
)
1087 static void __queue_quiesced_migration(struct dm_cache_migration
*mg
)
1089 list_add_tail(&mg
->list
, &mg
->cache
->quiesced_migrations
);
1092 static void queue_quiesced_migration(struct dm_cache_migration
*mg
)
1094 unsigned long flags
;
1095 struct cache
*cache
= mg
->cache
;
1097 spin_lock_irqsave(&cache
->lock
, flags
);
1098 __queue_quiesced_migration(mg
);
1099 spin_unlock_irqrestore(&cache
->lock
, flags
);
1104 static void queue_quiesced_migrations(struct cache
*cache
, struct list_head
*work
)
1106 unsigned long flags
;
1107 struct dm_cache_migration
*mg
, *tmp
;
1109 spin_lock_irqsave(&cache
->lock
, flags
);
1110 list_for_each_entry_safe(mg
, tmp
, work
, list
)
1111 __queue_quiesced_migration(mg
);
1112 spin_unlock_irqrestore(&cache
->lock
, flags
);
1117 static void check_for_quiesced_migrations(struct cache
*cache
,
1118 struct per_bio_data
*pb
)
1120 struct list_head work
;
1122 if (!pb
->all_io_entry
)
1125 INIT_LIST_HEAD(&work
);
1126 if (pb
->all_io_entry
)
1127 dm_deferred_entry_dec(pb
->all_io_entry
, &work
);
1129 if (!list_empty(&work
))
1130 queue_quiesced_migrations(cache
, &work
);
1133 static void quiesce_migration(struct dm_cache_migration
*mg
)
1135 if (!dm_deferred_set_add_work(mg
->cache
->all_io_ds
, &mg
->list
))
1136 queue_quiesced_migration(mg
);
1139 static void promote(struct cache
*cache
, struct prealloc
*structs
,
1140 dm_oblock_t oblock
, dm_cblock_t cblock
,
1141 struct dm_bio_prison_cell
*cell
)
1143 struct dm_cache_migration
*mg
= prealloc_get_migration(structs
);
1146 mg
->writeback
= false;
1149 mg
->requeue_holder
= true;
1150 mg
->invalidate
= false;
1152 mg
->new_oblock
= oblock
;
1153 mg
->cblock
= cblock
;
1154 mg
->old_ocell
= NULL
;
1155 mg
->new_ocell
= cell
;
1156 mg
->start_jiffies
= jiffies
;
1158 inc_nr_migrations(cache
);
1159 quiesce_migration(mg
);
1162 static void writeback(struct cache
*cache
, struct prealloc
*structs
,
1163 dm_oblock_t oblock
, dm_cblock_t cblock
,
1164 struct dm_bio_prison_cell
*cell
)
1166 struct dm_cache_migration
*mg
= prealloc_get_migration(structs
);
1169 mg
->writeback
= true;
1171 mg
->promote
= false;
1172 mg
->requeue_holder
= true;
1173 mg
->invalidate
= false;
1175 mg
->old_oblock
= oblock
;
1176 mg
->cblock
= cblock
;
1177 mg
->old_ocell
= cell
;
1178 mg
->new_ocell
= NULL
;
1179 mg
->start_jiffies
= jiffies
;
1181 inc_nr_migrations(cache
);
1182 quiesce_migration(mg
);
1185 static void demote_then_promote(struct cache
*cache
, struct prealloc
*structs
,
1186 dm_oblock_t old_oblock
, dm_oblock_t new_oblock
,
1188 struct dm_bio_prison_cell
*old_ocell
,
1189 struct dm_bio_prison_cell
*new_ocell
)
1191 struct dm_cache_migration
*mg
= prealloc_get_migration(structs
);
1194 mg
->writeback
= false;
1197 mg
->requeue_holder
= true;
1198 mg
->invalidate
= false;
1200 mg
->old_oblock
= old_oblock
;
1201 mg
->new_oblock
= new_oblock
;
1202 mg
->cblock
= cblock
;
1203 mg
->old_ocell
= old_ocell
;
1204 mg
->new_ocell
= new_ocell
;
1205 mg
->start_jiffies
= jiffies
;
1207 inc_nr_migrations(cache
);
1208 quiesce_migration(mg
);
1212 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1213 * block are thrown away.
1215 static void invalidate(struct cache
*cache
, struct prealloc
*structs
,
1216 dm_oblock_t oblock
, dm_cblock_t cblock
,
1217 struct dm_bio_prison_cell
*cell
)
1219 struct dm_cache_migration
*mg
= prealloc_get_migration(structs
);
1222 mg
->writeback
= false;
1224 mg
->promote
= false;
1225 mg
->requeue_holder
= true;
1226 mg
->invalidate
= true;
1228 mg
->old_oblock
= oblock
;
1229 mg
->cblock
= cblock
;
1230 mg
->old_ocell
= cell
;
1231 mg
->new_ocell
= NULL
;
1232 mg
->start_jiffies
= jiffies
;
1234 inc_nr_migrations(cache
);
1235 quiesce_migration(mg
);
1238 /*----------------------------------------------------------------
1240 *--------------------------------------------------------------*/
1241 static void defer_bio(struct cache
*cache
, struct bio
*bio
)
1243 unsigned long flags
;
1245 spin_lock_irqsave(&cache
->lock
, flags
);
1246 bio_list_add(&cache
->deferred_bios
, bio
);
1247 spin_unlock_irqrestore(&cache
->lock
, flags
);
1252 static void process_flush_bio(struct cache
*cache
, struct bio
*bio
)
1254 size_t pb_data_size
= get_per_bio_data_size(cache
);
1255 struct per_bio_data
*pb
= get_per_bio_data(bio
, pb_data_size
);
1257 BUG_ON(bio
->bi_iter
.bi_size
);
1259 remap_to_origin(cache
, bio
);
1261 remap_to_cache(cache
, bio
, 0);
1267 * People generally discard large parts of a device, eg, the whole device
1268 * when formatting. Splitting these large discards up into cache block
1269 * sized ios and then quiescing (always neccessary for discard) takes too
1272 * We keep it simple, and allow any size of discard to come in, and just
1273 * mark off blocks on the discard bitset. No passdown occurs!
1275 * To implement passdown we need to change the bio_prison such that a cell
1276 * can have a key that spans many blocks.
1278 static void process_discard_bio(struct cache
*cache
, struct bio
*bio
)
1280 dm_block_t start_block
= dm_sector_div_up(bio
->bi_iter
.bi_sector
,
1281 cache
->discard_block_size
);
1282 dm_block_t end_block
= bio_end_sector(bio
);
1285 end_block
= block_div(end_block
, cache
->discard_block_size
);
1287 for (b
= start_block
; b
< end_block
; b
++)
1288 set_discard(cache
, to_dblock(b
));
1293 static bool spare_migration_bandwidth(struct cache
*cache
)
1295 sector_t current_volume
= (atomic_read(&cache
->nr_migrations
) + 1) *
1296 cache
->sectors_per_block
;
1297 return current_volume
< cache
->migration_threshold
;
1300 static void inc_hit_counter(struct cache
*cache
, struct bio
*bio
)
1302 atomic_inc(bio_data_dir(bio
) == READ
?
1303 &cache
->stats
.read_hit
: &cache
->stats
.write_hit
);
1306 static void inc_miss_counter(struct cache
*cache
, struct bio
*bio
)
1308 atomic_inc(bio_data_dir(bio
) == READ
?
1309 &cache
->stats
.read_miss
: &cache
->stats
.write_miss
);
1312 static void issue_cache_bio(struct cache
*cache
, struct bio
*bio
,
1313 struct per_bio_data
*pb
,
1314 dm_oblock_t oblock
, dm_cblock_t cblock
)
1316 pb
->all_io_entry
= dm_deferred_entry_inc(cache
->all_io_ds
);
1317 remap_to_cache_dirty(cache
, bio
, oblock
, cblock
);
1321 static void process_bio(struct cache
*cache
, struct prealloc
*structs
,
1325 bool release_cell
= true;
1326 dm_oblock_t block
= get_bio_block(cache
, bio
);
1327 struct dm_bio_prison_cell
*cell_prealloc
, *old_ocell
, *new_ocell
;
1328 struct policy_result lookup_result
;
1329 size_t pb_data_size
= get_per_bio_data_size(cache
);
1330 struct per_bio_data
*pb
= get_per_bio_data(bio
, pb_data_size
);
1331 bool discarded_block
= is_discarded_oblock(cache
, block
);
1332 bool passthrough
= passthrough_mode(&cache
->features
);
1333 bool can_migrate
= !passthrough
&& (discarded_block
|| spare_migration_bandwidth(cache
));
1336 * Check to see if that block is currently migrating.
1338 cell_prealloc
= prealloc_get_cell(structs
);
1339 r
= bio_detain(cache
, block
, bio
, cell_prealloc
,
1340 (cell_free_fn
) prealloc_put_cell
,
1341 structs
, &new_ocell
);
1345 r
= policy_map(cache
->policy
, block
, true, can_migrate
, discarded_block
,
1346 bio
, &lookup_result
);
1348 if (r
== -EWOULDBLOCK
)
1349 /* migration has been denied */
1350 lookup_result
.op
= POLICY_MISS
;
1352 switch (lookup_result
.op
) {
1355 inc_miss_counter(cache
, bio
);
1358 * Passthrough always maps to the origin,
1359 * invalidating any cache blocks that are written
1363 if (bio_data_dir(bio
) == WRITE
) {
1364 atomic_inc(&cache
->stats
.demotion
);
1365 invalidate(cache
, structs
, block
, lookup_result
.cblock
, new_ocell
);
1366 release_cell
= false;
1369 /* FIXME: factor out issue_origin() */
1370 pb
->all_io_entry
= dm_deferred_entry_inc(cache
->all_io_ds
);
1371 remap_to_origin_clear_discard(cache
, bio
, block
);
1375 inc_hit_counter(cache
, bio
);
1377 if (bio_data_dir(bio
) == WRITE
&&
1378 writethrough_mode(&cache
->features
) &&
1379 !is_dirty(cache
, lookup_result
.cblock
)) {
1380 pb
->all_io_entry
= dm_deferred_entry_inc(cache
->all_io_ds
);
1381 remap_to_origin_then_cache(cache
, bio
, block
, lookup_result
.cblock
);
1384 issue_cache_bio(cache
, bio
, pb
, block
, lookup_result
.cblock
);
1390 inc_miss_counter(cache
, bio
);
1391 pb
->all_io_entry
= dm_deferred_entry_inc(cache
->all_io_ds
);
1392 remap_to_origin_clear_discard(cache
, bio
, block
);
1397 atomic_inc(&cache
->stats
.promotion
);
1398 promote(cache
, structs
, block
, lookup_result
.cblock
, new_ocell
);
1399 release_cell
= false;
1402 case POLICY_REPLACE
:
1403 cell_prealloc
= prealloc_get_cell(structs
);
1404 r
= bio_detain(cache
, lookup_result
.old_oblock
, bio
, cell_prealloc
,
1405 (cell_free_fn
) prealloc_put_cell
,
1406 structs
, &old_ocell
);
1409 * We have to be careful to avoid lock inversion of
1410 * the cells. So we back off, and wait for the
1411 * old_ocell to become free.
1413 policy_force_mapping(cache
->policy
, block
,
1414 lookup_result
.old_oblock
);
1415 atomic_inc(&cache
->stats
.cache_cell_clash
);
1418 atomic_inc(&cache
->stats
.demotion
);
1419 atomic_inc(&cache
->stats
.promotion
);
1421 demote_then_promote(cache
, structs
, lookup_result
.old_oblock
,
1422 block
, lookup_result
.cblock
,
1423 old_ocell
, new_ocell
);
1424 release_cell
= false;
1428 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__
,
1429 (unsigned) lookup_result
.op
);
1434 cell_defer(cache
, new_ocell
, false);
1437 static int need_commit_due_to_time(struct cache
*cache
)
1439 return jiffies
< cache
->last_commit_jiffies
||
1440 jiffies
> cache
->last_commit_jiffies
+ COMMIT_PERIOD
;
1443 static int commit_if_needed(struct cache
*cache
)
1447 if ((cache
->commit_requested
|| need_commit_due_to_time(cache
)) &&
1448 dm_cache_changed_this_transaction(cache
->cmd
)) {
1449 atomic_inc(&cache
->stats
.commit_count
);
1450 cache
->commit_requested
= false;
1451 r
= dm_cache_commit(cache
->cmd
, false);
1452 cache
->last_commit_jiffies
= jiffies
;
1458 static void process_deferred_bios(struct cache
*cache
)
1460 unsigned long flags
;
1461 struct bio_list bios
;
1463 struct prealloc structs
;
1465 memset(&structs
, 0, sizeof(structs
));
1466 bio_list_init(&bios
);
1468 spin_lock_irqsave(&cache
->lock
, flags
);
1469 bio_list_merge(&bios
, &cache
->deferred_bios
);
1470 bio_list_init(&cache
->deferred_bios
);
1471 spin_unlock_irqrestore(&cache
->lock
, flags
);
1473 while (!bio_list_empty(&bios
)) {
1475 * If we've got no free migration structs, and processing
1476 * this bio might require one, we pause until there are some
1477 * prepared mappings to process.
1479 if (prealloc_data_structs(cache
, &structs
)) {
1480 spin_lock_irqsave(&cache
->lock
, flags
);
1481 bio_list_merge(&cache
->deferred_bios
, &bios
);
1482 spin_unlock_irqrestore(&cache
->lock
, flags
);
1486 bio
= bio_list_pop(&bios
);
1488 if (bio
->bi_rw
& REQ_FLUSH
)
1489 process_flush_bio(cache
, bio
);
1490 else if (bio
->bi_rw
& REQ_DISCARD
)
1491 process_discard_bio(cache
, bio
);
1493 process_bio(cache
, &structs
, bio
);
1496 prealloc_free_structs(cache
, &structs
);
1499 static void process_deferred_flush_bios(struct cache
*cache
, bool submit_bios
)
1501 unsigned long flags
;
1502 struct bio_list bios
;
1505 bio_list_init(&bios
);
1507 spin_lock_irqsave(&cache
->lock
, flags
);
1508 bio_list_merge(&bios
, &cache
->deferred_flush_bios
);
1509 bio_list_init(&cache
->deferred_flush_bios
);
1510 spin_unlock_irqrestore(&cache
->lock
, flags
);
1512 while ((bio
= bio_list_pop(&bios
)))
1513 submit_bios
? generic_make_request(bio
) : bio_io_error(bio
);
1516 static void process_deferred_writethrough_bios(struct cache
*cache
)
1518 unsigned long flags
;
1519 struct bio_list bios
;
1522 bio_list_init(&bios
);
1524 spin_lock_irqsave(&cache
->lock
, flags
);
1525 bio_list_merge(&bios
, &cache
->deferred_writethrough_bios
);
1526 bio_list_init(&cache
->deferred_writethrough_bios
);
1527 spin_unlock_irqrestore(&cache
->lock
, flags
);
1529 while ((bio
= bio_list_pop(&bios
)))
1530 generic_make_request(bio
);
1533 static void writeback_some_dirty_blocks(struct cache
*cache
)
1538 struct prealloc structs
;
1539 struct dm_bio_prison_cell
*old_ocell
;
1541 memset(&structs
, 0, sizeof(structs
));
1543 while (spare_migration_bandwidth(cache
)) {
1544 if (prealloc_data_structs(cache
, &structs
))
1547 r
= policy_writeback_work(cache
->policy
, &oblock
, &cblock
);
1551 r
= get_cell(cache
, oblock
, &structs
, &old_ocell
);
1553 policy_set_dirty(cache
->policy
, oblock
);
1557 writeback(cache
, &structs
, oblock
, cblock
, old_ocell
);
1560 prealloc_free_structs(cache
, &structs
);
1563 /*----------------------------------------------------------------
1565 * Dropping something from the cache *without* writing back.
1566 *--------------------------------------------------------------*/
1568 static void process_invalidation_request(struct cache
*cache
, struct invalidation_request
*req
)
1571 uint64_t begin
= from_cblock(req
->cblocks
->begin
);
1572 uint64_t end
= from_cblock(req
->cblocks
->end
);
1574 while (begin
!= end
) {
1575 r
= policy_remove_cblock(cache
->policy
, to_cblock(begin
));
1577 r
= dm_cache_remove_mapping(cache
->cmd
, to_cblock(begin
));
1581 } else if (r
== -ENODATA
) {
1582 /* harmless, already unmapped */
1586 DMERR("policy_remove_cblock failed");
1593 cache
->commit_requested
= true;
1596 atomic_set(&req
->complete
, 1);
1598 wake_up(&req
->result_wait
);
1601 static void process_invalidation_requests(struct cache
*cache
)
1603 struct list_head list
;
1604 struct invalidation_request
*req
, *tmp
;
1606 INIT_LIST_HEAD(&list
);
1607 spin_lock(&cache
->invalidation_lock
);
1608 list_splice_init(&cache
->invalidation_requests
, &list
);
1609 spin_unlock(&cache
->invalidation_lock
);
1611 list_for_each_entry_safe (req
, tmp
, &list
, list
)
1612 process_invalidation_request(cache
, req
);
1615 /*----------------------------------------------------------------
1617 *--------------------------------------------------------------*/
1618 static bool is_quiescing(struct cache
*cache
)
1620 return atomic_read(&cache
->quiescing
);
1623 static void ack_quiescing(struct cache
*cache
)
1625 if (is_quiescing(cache
)) {
1626 atomic_inc(&cache
->quiescing_ack
);
1627 wake_up(&cache
->quiescing_wait
);
1631 static void wait_for_quiescing_ack(struct cache
*cache
)
1633 wait_event(cache
->quiescing_wait
, atomic_read(&cache
->quiescing_ack
));
1636 static void start_quiescing(struct cache
*cache
)
1638 atomic_inc(&cache
->quiescing
);
1639 wait_for_quiescing_ack(cache
);
1642 static void stop_quiescing(struct cache
*cache
)
1644 atomic_set(&cache
->quiescing
, 0);
1645 atomic_set(&cache
->quiescing_ack
, 0);
1648 static void wait_for_migrations(struct cache
*cache
)
1650 wait_event(cache
->migration_wait
, !atomic_read(&cache
->nr_migrations
));
1653 static void stop_worker(struct cache
*cache
)
1655 cancel_delayed_work(&cache
->waker
);
1656 flush_workqueue(cache
->wq
);
1659 static void requeue_deferred_io(struct cache
*cache
)
1662 struct bio_list bios
;
1664 bio_list_init(&bios
);
1665 bio_list_merge(&bios
, &cache
->deferred_bios
);
1666 bio_list_init(&cache
->deferred_bios
);
1668 while ((bio
= bio_list_pop(&bios
)))
1669 bio_endio(bio
, DM_ENDIO_REQUEUE
);
1672 static int more_work(struct cache
*cache
)
1674 if (is_quiescing(cache
))
1675 return !list_empty(&cache
->quiesced_migrations
) ||
1676 !list_empty(&cache
->completed_migrations
) ||
1677 !list_empty(&cache
->need_commit_migrations
);
1679 return !bio_list_empty(&cache
->deferred_bios
) ||
1680 !bio_list_empty(&cache
->deferred_flush_bios
) ||
1681 !bio_list_empty(&cache
->deferred_writethrough_bios
) ||
1682 !list_empty(&cache
->quiesced_migrations
) ||
1683 !list_empty(&cache
->completed_migrations
) ||
1684 !list_empty(&cache
->need_commit_migrations
) ||
1688 static void do_worker(struct work_struct
*ws
)
1690 struct cache
*cache
= container_of(ws
, struct cache
, worker
);
1693 if (!is_quiescing(cache
)) {
1694 writeback_some_dirty_blocks(cache
);
1695 process_deferred_writethrough_bios(cache
);
1696 process_deferred_bios(cache
);
1697 process_invalidation_requests(cache
);
1700 process_migrations(cache
, &cache
->quiesced_migrations
, issue_copy
);
1701 process_migrations(cache
, &cache
->completed_migrations
, complete_migration
);
1703 if (commit_if_needed(cache
)) {
1704 process_deferred_flush_bios(cache
, false);
1707 * FIXME: rollback metadata or just go into a
1708 * failure mode and error everything
1711 process_deferred_flush_bios(cache
, true);
1712 process_migrations(cache
, &cache
->need_commit_migrations
,
1713 migration_success_post_commit
);
1716 ack_quiescing(cache
);
1718 } while (more_work(cache
));
1722 * We want to commit periodically so that not too much
1723 * unwritten metadata builds up.
1725 static void do_waker(struct work_struct
*ws
)
1727 struct cache
*cache
= container_of(to_delayed_work(ws
), struct cache
, waker
);
1728 policy_tick(cache
->policy
);
1730 queue_delayed_work(cache
->wq
, &cache
->waker
, COMMIT_PERIOD
);
1733 /*----------------------------------------------------------------*/
1735 static int is_congested(struct dm_dev
*dev
, int bdi_bits
)
1737 struct request_queue
*q
= bdev_get_queue(dev
->bdev
);
1738 return bdi_congested(&q
->backing_dev_info
, bdi_bits
);
1741 static int cache_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
1743 struct cache
*cache
= container_of(cb
, struct cache
, callbacks
);
1745 return is_congested(cache
->origin_dev
, bdi_bits
) ||
1746 is_congested(cache
->cache_dev
, bdi_bits
);
1749 /*----------------------------------------------------------------
1751 *--------------------------------------------------------------*/
1754 * This function gets called on the error paths of the constructor, so we
1755 * have to cope with a partially initialised struct.
1757 static void destroy(struct cache
*cache
)
1761 if (cache
->next_migration
)
1762 mempool_free(cache
->next_migration
, cache
->migration_pool
);
1764 if (cache
->migration_pool
)
1765 mempool_destroy(cache
->migration_pool
);
1767 if (cache
->all_io_ds
)
1768 dm_deferred_set_destroy(cache
->all_io_ds
);
1771 dm_bio_prison_destroy(cache
->prison
);
1774 destroy_workqueue(cache
->wq
);
1776 if (cache
->dirty_bitset
)
1777 free_bitset(cache
->dirty_bitset
);
1779 if (cache
->discard_bitset
)
1780 free_bitset(cache
->discard_bitset
);
1783 dm_kcopyd_client_destroy(cache
->copier
);
1786 dm_cache_metadata_close(cache
->cmd
);
1788 if (cache
->metadata_dev
)
1789 dm_put_device(cache
->ti
, cache
->metadata_dev
);
1791 if (cache
->origin_dev
)
1792 dm_put_device(cache
->ti
, cache
->origin_dev
);
1794 if (cache
->cache_dev
)
1795 dm_put_device(cache
->ti
, cache
->cache_dev
);
1798 dm_cache_policy_destroy(cache
->policy
);
1800 for (i
= 0; i
< cache
->nr_ctr_args
; i
++)
1801 kfree(cache
->ctr_args
[i
]);
1802 kfree(cache
->ctr_args
);
1807 static void cache_dtr(struct dm_target
*ti
)
1809 struct cache
*cache
= ti
->private;
1814 static sector_t
get_dev_size(struct dm_dev
*dev
)
1816 return i_size_read(dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
1819 /*----------------------------------------------------------------*/
1822 * Construct a cache device mapping.
1824 * cache <metadata dev> <cache dev> <origin dev> <block size>
1825 * <#feature args> [<feature arg>]*
1826 * <policy> <#policy args> [<policy arg>]*
1828 * metadata dev : fast device holding the persistent metadata
1829 * cache dev : fast device holding cached data blocks
1830 * origin dev : slow device holding original data blocks
1831 * block size : cache unit size in sectors
1833 * #feature args : number of feature arguments passed
1834 * feature args : writethrough. (The default is writeback.)
1836 * policy : the replacement policy to use
1837 * #policy args : an even number of policy arguments corresponding
1838 * to key/value pairs passed to the policy
1839 * policy args : key/value pairs passed to the policy
1840 * E.g. 'sequential_threshold 1024'
1841 * See cache-policies.txt for details.
1843 * Optional feature arguments are:
1844 * writethrough : write through caching that prohibits cache block
1845 * content from being different from origin block content.
1846 * Without this argument, the default behaviour is to write
1847 * back cache block contents later for performance reasons,
1848 * so they may differ from the corresponding origin blocks.
1851 struct dm_target
*ti
;
1853 struct dm_dev
*metadata_dev
;
1855 struct dm_dev
*cache_dev
;
1856 sector_t cache_sectors
;
1858 struct dm_dev
*origin_dev
;
1859 sector_t origin_sectors
;
1861 uint32_t block_size
;
1863 const char *policy_name
;
1865 const char **policy_argv
;
1867 struct cache_features features
;
1870 static void destroy_cache_args(struct cache_args
*ca
)
1872 if (ca
->metadata_dev
)
1873 dm_put_device(ca
->ti
, ca
->metadata_dev
);
1876 dm_put_device(ca
->ti
, ca
->cache_dev
);
1879 dm_put_device(ca
->ti
, ca
->origin_dev
);
1884 static bool at_least_one_arg(struct dm_arg_set
*as
, char **error
)
1887 *error
= "Insufficient args";
1894 static int parse_metadata_dev(struct cache_args
*ca
, struct dm_arg_set
*as
,
1898 sector_t metadata_dev_size
;
1899 char b
[BDEVNAME_SIZE
];
1901 if (!at_least_one_arg(as
, error
))
1904 r
= dm_get_device(ca
->ti
, dm_shift_arg(as
), FMODE_READ
| FMODE_WRITE
,
1907 *error
= "Error opening metadata device";
1911 metadata_dev_size
= get_dev_size(ca
->metadata_dev
);
1912 if (metadata_dev_size
> DM_CACHE_METADATA_MAX_SECTORS_WARNING
)
1913 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1914 bdevname(ca
->metadata_dev
->bdev
, b
), THIN_METADATA_MAX_SECTORS
);
1919 static int parse_cache_dev(struct cache_args
*ca
, struct dm_arg_set
*as
,
1924 if (!at_least_one_arg(as
, error
))
1927 r
= dm_get_device(ca
->ti
, dm_shift_arg(as
), FMODE_READ
| FMODE_WRITE
,
1930 *error
= "Error opening cache device";
1933 ca
->cache_sectors
= get_dev_size(ca
->cache_dev
);
1938 static int parse_origin_dev(struct cache_args
*ca
, struct dm_arg_set
*as
,
1943 if (!at_least_one_arg(as
, error
))
1946 r
= dm_get_device(ca
->ti
, dm_shift_arg(as
), FMODE_READ
| FMODE_WRITE
,
1949 *error
= "Error opening origin device";
1953 ca
->origin_sectors
= get_dev_size(ca
->origin_dev
);
1954 if (ca
->ti
->len
> ca
->origin_sectors
) {
1955 *error
= "Device size larger than cached device";
1962 static int parse_block_size(struct cache_args
*ca
, struct dm_arg_set
*as
,
1965 unsigned long block_size
;
1967 if (!at_least_one_arg(as
, error
))
1970 if (kstrtoul(dm_shift_arg(as
), 10, &block_size
) || !block_size
||
1971 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
1972 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
1973 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
1974 *error
= "Invalid data block size";
1978 if (block_size
> ca
->cache_sectors
) {
1979 *error
= "Data block size is larger than the cache device";
1983 ca
->block_size
= block_size
;
1988 static void init_features(struct cache_features
*cf
)
1990 cf
->mode
= CM_WRITE
;
1991 cf
->io_mode
= CM_IO_WRITEBACK
;
1994 static int parse_features(struct cache_args
*ca
, struct dm_arg_set
*as
,
1997 static struct dm_arg _args
[] = {
1998 {0, 1, "Invalid number of cache feature arguments"},
2004 struct cache_features
*cf
= &ca
->features
;
2008 r
= dm_read_arg_group(_args
, as
, &argc
, error
);
2013 arg
= dm_shift_arg(as
);
2015 if (!strcasecmp(arg
, "writeback"))
2016 cf
->io_mode
= CM_IO_WRITEBACK
;
2018 else if (!strcasecmp(arg
, "writethrough"))
2019 cf
->io_mode
= CM_IO_WRITETHROUGH
;
2021 else if (!strcasecmp(arg
, "passthrough"))
2022 cf
->io_mode
= CM_IO_PASSTHROUGH
;
2025 *error
= "Unrecognised cache feature requested";
2033 static int parse_policy(struct cache_args
*ca
, struct dm_arg_set
*as
,
2036 static struct dm_arg _args
[] = {
2037 {0, 1024, "Invalid number of policy arguments"},
2042 if (!at_least_one_arg(as
, error
))
2045 ca
->policy_name
= dm_shift_arg(as
);
2047 r
= dm_read_arg_group(_args
, as
, &ca
->policy_argc
, error
);
2051 ca
->policy_argv
= (const char **)as
->argv
;
2052 dm_consume_args(as
, ca
->policy_argc
);
2057 static int parse_cache_args(struct cache_args
*ca
, int argc
, char **argv
,
2061 struct dm_arg_set as
;
2066 r
= parse_metadata_dev(ca
, &as
, error
);
2070 r
= parse_cache_dev(ca
, &as
, error
);
2074 r
= parse_origin_dev(ca
, &as
, error
);
2078 r
= parse_block_size(ca
, &as
, error
);
2082 r
= parse_features(ca
, &as
, error
);
2086 r
= parse_policy(ca
, &as
, error
);
2093 /*----------------------------------------------------------------*/
2095 static struct kmem_cache
*migration_cache
;
2097 #define NOT_CORE_OPTION 1
2099 static int process_config_option(struct cache
*cache
, const char *key
, const char *value
)
2103 if (!strcasecmp(key
, "migration_threshold")) {
2104 if (kstrtoul(value
, 10, &tmp
))
2107 cache
->migration_threshold
= tmp
;
2111 return NOT_CORE_OPTION
;
2114 static int set_config_value(struct cache
*cache
, const char *key
, const char *value
)
2116 int r
= process_config_option(cache
, key
, value
);
2118 if (r
== NOT_CORE_OPTION
)
2119 r
= policy_set_config_value(cache
->policy
, key
, value
);
2122 DMWARN("bad config value for %s: %s", key
, value
);
2127 static int set_config_values(struct cache
*cache
, int argc
, const char **argv
)
2132 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2137 r
= set_config_value(cache
, argv
[0], argv
[1]);
2148 static int create_cache_policy(struct cache
*cache
, struct cache_args
*ca
,
2151 struct dm_cache_policy
*p
= dm_cache_policy_create(ca
->policy_name
,
2153 cache
->origin_sectors
,
2154 cache
->sectors_per_block
);
2156 *error
= "Error creating cache's policy";
2165 * We want the discard block size to be a power of two, at least the size
2166 * of the cache block size, and have no more than 2^14 discard blocks
2167 * across the origin.
2169 #define MAX_DISCARD_BLOCKS (1 << 14)
2171 static bool too_many_discard_blocks(sector_t discard_block_size
,
2172 sector_t origin_size
)
2174 (void) sector_div(origin_size
, discard_block_size
);
2176 return origin_size
> MAX_DISCARD_BLOCKS
;
2179 static sector_t
calculate_discard_block_size(sector_t cache_block_size
,
2180 sector_t origin_size
)
2182 sector_t discard_block_size
;
2184 discard_block_size
= roundup_pow_of_two(cache_block_size
);
2187 while (too_many_discard_blocks(discard_block_size
, origin_size
))
2188 discard_block_size
*= 2;
2190 return discard_block_size
;
2193 #define DEFAULT_MIGRATION_THRESHOLD 2048
2195 static int cache_create(struct cache_args
*ca
, struct cache
**result
)
2198 char **error
= &ca
->ti
->error
;
2199 struct cache
*cache
;
2200 struct dm_target
*ti
= ca
->ti
;
2201 dm_block_t origin_blocks
;
2202 struct dm_cache_metadata
*cmd
;
2203 bool may_format
= ca
->features
.mode
== CM_WRITE
;
2205 cache
= kzalloc(sizeof(*cache
), GFP_KERNEL
);
2210 ti
->private = cache
;
2211 ti
->num_flush_bios
= 2;
2212 ti
->flush_supported
= true;
2214 ti
->num_discard_bios
= 1;
2215 ti
->discards_supported
= true;
2216 ti
->discard_zeroes_data_unsupported
= true;
2218 cache
->features
= ca
->features
;
2219 ti
->per_bio_data_size
= get_per_bio_data_size(cache
);
2221 cache
->callbacks
.congested_fn
= cache_is_congested
;
2222 dm_table_add_target_callbacks(ti
->table
, &cache
->callbacks
);
2224 cache
->metadata_dev
= ca
->metadata_dev
;
2225 cache
->origin_dev
= ca
->origin_dev
;
2226 cache
->cache_dev
= ca
->cache_dev
;
2228 ca
->metadata_dev
= ca
->origin_dev
= ca
->cache_dev
= NULL
;
2230 /* FIXME: factor out this whole section */
2231 origin_blocks
= cache
->origin_sectors
= ca
->origin_sectors
;
2232 origin_blocks
= block_div(origin_blocks
, ca
->block_size
);
2233 cache
->origin_blocks
= to_oblock(origin_blocks
);
2235 cache
->sectors_per_block
= ca
->block_size
;
2236 if (dm_set_target_max_io_len(ti
, cache
->sectors_per_block
)) {
2241 if (ca
->block_size
& (ca
->block_size
- 1)) {
2242 dm_block_t cache_size
= ca
->cache_sectors
;
2244 cache
->sectors_per_block_shift
= -1;
2245 cache_size
= block_div(cache_size
, ca
->block_size
);
2246 cache
->cache_size
= to_cblock(cache_size
);
2248 cache
->sectors_per_block_shift
= __ffs(ca
->block_size
);
2249 cache
->cache_size
= to_cblock(ca
->cache_sectors
>> cache
->sectors_per_block_shift
);
2252 r
= create_cache_policy(cache
, ca
, error
);
2256 cache
->policy_nr_args
= ca
->policy_argc
;
2257 cache
->migration_threshold
= DEFAULT_MIGRATION_THRESHOLD
;
2259 r
= set_config_values(cache
, ca
->policy_argc
, ca
->policy_argv
);
2261 *error
= "Error setting cache policy's config values";
2265 cmd
= dm_cache_metadata_open(cache
->metadata_dev
->bdev
,
2266 ca
->block_size
, may_format
,
2267 dm_cache_policy_get_hint_size(cache
->policy
));
2269 *error
= "Error creating metadata object";
2275 if (passthrough_mode(&cache
->features
)) {
2278 r
= dm_cache_metadata_all_clean(cache
->cmd
, &all_clean
);
2280 *error
= "dm_cache_metadata_all_clean() failed";
2285 *error
= "Cannot enter passthrough mode unless all blocks are clean";
2291 spin_lock_init(&cache
->lock
);
2292 bio_list_init(&cache
->deferred_bios
);
2293 bio_list_init(&cache
->deferred_flush_bios
);
2294 bio_list_init(&cache
->deferred_writethrough_bios
);
2295 INIT_LIST_HEAD(&cache
->quiesced_migrations
);
2296 INIT_LIST_HEAD(&cache
->completed_migrations
);
2297 INIT_LIST_HEAD(&cache
->need_commit_migrations
);
2298 atomic_set(&cache
->nr_migrations
, 0);
2299 init_waitqueue_head(&cache
->migration_wait
);
2301 init_waitqueue_head(&cache
->quiescing_wait
);
2302 atomic_set(&cache
->quiescing
, 0);
2303 atomic_set(&cache
->quiescing_ack
, 0);
2306 cache
->nr_dirty
= 0;
2307 cache
->dirty_bitset
= alloc_bitset(from_cblock(cache
->cache_size
));
2308 if (!cache
->dirty_bitset
) {
2309 *error
= "could not allocate dirty bitset";
2312 clear_bitset(cache
->dirty_bitset
, from_cblock(cache
->cache_size
));
2314 cache
->discard_block_size
=
2315 calculate_discard_block_size(cache
->sectors_per_block
,
2316 cache
->origin_sectors
);
2317 cache
->discard_nr_blocks
= oblock_to_dblock(cache
, cache
->origin_blocks
);
2318 cache
->discard_bitset
= alloc_bitset(from_dblock(cache
->discard_nr_blocks
));
2319 if (!cache
->discard_bitset
) {
2320 *error
= "could not allocate discard bitset";
2323 clear_bitset(cache
->discard_bitset
, from_dblock(cache
->discard_nr_blocks
));
2325 cache
->copier
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2326 if (IS_ERR(cache
->copier
)) {
2327 *error
= "could not create kcopyd client";
2328 r
= PTR_ERR(cache
->copier
);
2332 cache
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
2334 *error
= "could not create workqueue for metadata object";
2337 INIT_WORK(&cache
->worker
, do_worker
);
2338 INIT_DELAYED_WORK(&cache
->waker
, do_waker
);
2339 cache
->last_commit_jiffies
= jiffies
;
2341 cache
->prison
= dm_bio_prison_create(PRISON_CELLS
);
2342 if (!cache
->prison
) {
2343 *error
= "could not create bio prison";
2347 cache
->all_io_ds
= dm_deferred_set_create();
2348 if (!cache
->all_io_ds
) {
2349 *error
= "could not create all_io deferred set";
2353 cache
->migration_pool
= mempool_create_slab_pool(MIGRATION_POOL_SIZE
,
2355 if (!cache
->migration_pool
) {
2356 *error
= "Error creating cache's migration mempool";
2360 cache
->next_migration
= NULL
;
2362 cache
->need_tick_bio
= true;
2363 cache
->sized
= false;
2364 cache
->invalidate
= false;
2365 cache
->commit_requested
= false;
2366 cache
->loaded_mappings
= false;
2367 cache
->loaded_discards
= false;
2371 atomic_set(&cache
->stats
.demotion
, 0);
2372 atomic_set(&cache
->stats
.promotion
, 0);
2373 atomic_set(&cache
->stats
.copies_avoided
, 0);
2374 atomic_set(&cache
->stats
.cache_cell_clash
, 0);
2375 atomic_set(&cache
->stats
.commit_count
, 0);
2376 atomic_set(&cache
->stats
.discard_count
, 0);
2378 spin_lock_init(&cache
->invalidation_lock
);
2379 INIT_LIST_HEAD(&cache
->invalidation_requests
);
2389 static int copy_ctr_args(struct cache
*cache
, int argc
, const char **argv
)
2394 copy
= kcalloc(argc
, sizeof(*copy
), GFP_KERNEL
);
2397 for (i
= 0; i
< argc
; i
++) {
2398 copy
[i
] = kstrdup(argv
[i
], GFP_KERNEL
);
2407 cache
->nr_ctr_args
= argc
;
2408 cache
->ctr_args
= copy
;
2413 static int cache_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2416 struct cache_args
*ca
;
2417 struct cache
*cache
= NULL
;
2419 ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
2421 ti
->error
= "Error allocating memory for cache";
2426 r
= parse_cache_args(ca
, argc
, argv
, &ti
->error
);
2430 r
= cache_create(ca
, &cache
);
2434 r
= copy_ctr_args(cache
, argc
- 3, (const char **)argv
+ 3);
2440 ti
->private = cache
;
2443 destroy_cache_args(ca
);
2447 static int cache_map(struct dm_target
*ti
, struct bio
*bio
)
2449 struct cache
*cache
= ti
->private;
2452 dm_oblock_t block
= get_bio_block(cache
, bio
);
2453 size_t pb_data_size
= get_per_bio_data_size(cache
);
2454 bool can_migrate
= false;
2455 bool discarded_block
;
2456 struct dm_bio_prison_cell
*cell
;
2457 struct policy_result lookup_result
;
2458 struct per_bio_data
*pb
;
2460 if (from_oblock(block
) > from_oblock(cache
->origin_blocks
)) {
2462 * This can only occur if the io goes to a partial block at
2463 * the end of the origin device. We don't cache these.
2464 * Just remap to the origin and carry on.
2466 remap_to_origin_clear_discard(cache
, bio
, block
);
2467 return DM_MAPIO_REMAPPED
;
2470 pb
= init_per_bio_data(bio
, pb_data_size
);
2472 if (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
| REQ_DISCARD
)) {
2473 defer_bio(cache
, bio
);
2474 return DM_MAPIO_SUBMITTED
;
2478 * Check to see if that block is currently migrating.
2480 cell
= alloc_prison_cell(cache
);
2482 defer_bio(cache
, bio
);
2483 return DM_MAPIO_SUBMITTED
;
2486 r
= bio_detain(cache
, block
, bio
, cell
,
2487 (cell_free_fn
) free_prison_cell
,
2491 defer_bio(cache
, bio
);
2493 return DM_MAPIO_SUBMITTED
;
2496 discarded_block
= is_discarded_oblock(cache
, block
);
2498 r
= policy_map(cache
->policy
, block
, false, can_migrate
, discarded_block
,
2499 bio
, &lookup_result
);
2500 if (r
== -EWOULDBLOCK
) {
2501 cell_defer(cache
, cell
, true);
2502 return DM_MAPIO_SUBMITTED
;
2505 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r
);
2507 return DM_MAPIO_SUBMITTED
;
2510 r
= DM_MAPIO_REMAPPED
;
2511 switch (lookup_result
.op
) {
2513 if (passthrough_mode(&cache
->features
)) {
2514 if (bio_data_dir(bio
) == WRITE
) {
2516 * We need to invalidate this block, so
2517 * defer for the worker thread.
2519 cell_defer(cache
, cell
, true);
2520 r
= DM_MAPIO_SUBMITTED
;
2523 pb
->all_io_entry
= dm_deferred_entry_inc(cache
->all_io_ds
);
2524 inc_miss_counter(cache
, bio
);
2525 remap_to_origin_clear_discard(cache
, bio
, block
);
2527 cell_defer(cache
, cell
, false);
2531 inc_hit_counter(cache
, bio
);
2533 if (bio_data_dir(bio
) == WRITE
&& writethrough_mode(&cache
->features
) &&
2534 !is_dirty(cache
, lookup_result
.cblock
))
2535 remap_to_origin_then_cache(cache
, bio
, block
, lookup_result
.cblock
);
2537 remap_to_cache_dirty(cache
, bio
, block
, lookup_result
.cblock
);
2539 cell_defer(cache
, cell
, false);
2544 inc_miss_counter(cache
, bio
);
2545 pb
->all_io_entry
= dm_deferred_entry_inc(cache
->all_io_ds
);
2547 if (pb
->req_nr
!= 0) {
2549 * This is a duplicate writethrough io that is no
2550 * longer needed because the block has been demoted.
2553 cell_defer(cache
, cell
, false);
2554 return DM_MAPIO_SUBMITTED
;
2556 remap_to_origin_clear_discard(cache
, bio
, block
);
2557 cell_defer(cache
, cell
, false);
2562 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__
,
2563 (unsigned) lookup_result
.op
);
2565 r
= DM_MAPIO_SUBMITTED
;
2571 static int cache_end_io(struct dm_target
*ti
, struct bio
*bio
, int error
)
2573 struct cache
*cache
= ti
->private;
2574 unsigned long flags
;
2575 size_t pb_data_size
= get_per_bio_data_size(cache
);
2576 struct per_bio_data
*pb
= get_per_bio_data(bio
, pb_data_size
);
2579 policy_tick(cache
->policy
);
2581 spin_lock_irqsave(&cache
->lock
, flags
);
2582 cache
->need_tick_bio
= true;
2583 spin_unlock_irqrestore(&cache
->lock
, flags
);
2586 check_for_quiesced_migrations(cache
, pb
);
2591 static int write_dirty_bitset(struct cache
*cache
)
2595 for (i
= 0; i
< from_cblock(cache
->cache_size
); i
++) {
2596 r
= dm_cache_set_dirty(cache
->cmd
, to_cblock(i
),
2597 is_dirty(cache
, to_cblock(i
)));
2605 static int write_discard_bitset(struct cache
*cache
)
2609 r
= dm_cache_discard_bitset_resize(cache
->cmd
, cache
->discard_block_size
,
2610 cache
->discard_nr_blocks
);
2612 DMERR("could not resize on-disk discard bitset");
2616 for (i
= 0; i
< from_dblock(cache
->discard_nr_blocks
); i
++) {
2617 r
= dm_cache_set_discard(cache
->cmd
, to_dblock(i
),
2618 is_discarded(cache
, to_dblock(i
)));
2626 static int save_hint(void *context
, dm_cblock_t cblock
, dm_oblock_t oblock
,
2629 struct cache
*cache
= context
;
2630 return dm_cache_save_hint(cache
->cmd
, cblock
, hint
);
2633 static int write_hints(struct cache
*cache
)
2637 r
= dm_cache_begin_hints(cache
->cmd
, cache
->policy
);
2639 DMERR("dm_cache_begin_hints failed");
2643 r
= policy_walk_mappings(cache
->policy
, save_hint
, cache
);
2645 DMERR("policy_walk_mappings failed");
2651 * returns true on success
2653 static bool sync_metadata(struct cache
*cache
)
2657 r1
= write_dirty_bitset(cache
);
2659 DMERR("could not write dirty bitset");
2661 r2
= write_discard_bitset(cache
);
2663 DMERR("could not write discard bitset");
2667 r3
= write_hints(cache
);
2669 DMERR("could not write hints");
2672 * If writing the above metadata failed, we still commit, but don't
2673 * set the clean shutdown flag. This will effectively force every
2674 * dirty bit to be set on reload.
2676 r4
= dm_cache_commit(cache
->cmd
, !r1
&& !r2
&& !r3
);
2678 DMERR("could not write cache metadata. Data loss may occur.");
2680 return !r1
&& !r2
&& !r3
&& !r4
;
2683 static void cache_postsuspend(struct dm_target
*ti
)
2685 struct cache
*cache
= ti
->private;
2687 start_quiescing(cache
);
2688 wait_for_migrations(cache
);
2690 requeue_deferred_io(cache
);
2691 stop_quiescing(cache
);
2693 (void) sync_metadata(cache
);
2696 static int load_mapping(void *context
, dm_oblock_t oblock
, dm_cblock_t cblock
,
2697 bool dirty
, uint32_t hint
, bool hint_valid
)
2700 struct cache
*cache
= context
;
2702 r
= policy_load_mapping(cache
->policy
, oblock
, cblock
, hint
, hint_valid
);
2707 set_dirty(cache
, oblock
, cblock
);
2709 clear_dirty(cache
, oblock
, cblock
);
2714 static int load_discard(void *context
, sector_t discard_block_size
,
2715 dm_dblock_t dblock
, bool discard
)
2717 struct cache
*cache
= context
;
2719 /* FIXME: handle mis-matched block size */
2722 set_discard(cache
, dblock
);
2724 clear_discard(cache
, dblock
);
2729 static dm_cblock_t
get_cache_dev_size(struct cache
*cache
)
2731 sector_t size
= get_dev_size(cache
->cache_dev
);
2732 (void) sector_div(size
, cache
->sectors_per_block
);
2733 return to_cblock(size
);
2736 static bool can_resize(struct cache
*cache
, dm_cblock_t new_size
)
2738 if (from_cblock(new_size
) > from_cblock(cache
->cache_size
))
2742 * We can't drop a dirty block when shrinking the cache.
2744 while (from_cblock(new_size
) < from_cblock(cache
->cache_size
)) {
2745 new_size
= to_cblock(from_cblock(new_size
) + 1);
2746 if (is_dirty(cache
, new_size
)) {
2747 DMERR("unable to shrink cache; cache block %llu is dirty",
2748 (unsigned long long) from_cblock(new_size
));
2756 static int resize_cache_dev(struct cache
*cache
, dm_cblock_t new_size
)
2760 r
= dm_cache_resize(cache
->cmd
, cache
->cache_size
);
2762 DMERR("could not resize cache metadata");
2766 cache
->cache_size
= new_size
;
2771 static int cache_preresume(struct dm_target
*ti
)
2774 struct cache
*cache
= ti
->private;
2775 dm_cblock_t csize
= get_cache_dev_size(cache
);
2778 * Check to see if the cache has resized.
2780 if (!cache
->sized
) {
2781 r
= resize_cache_dev(cache
, csize
);
2785 cache
->sized
= true;
2787 } else if (csize
!= cache
->cache_size
) {
2788 if (!can_resize(cache
, csize
))
2791 r
= resize_cache_dev(cache
, csize
);
2796 if (!cache
->loaded_mappings
) {
2797 r
= dm_cache_load_mappings(cache
->cmd
, cache
->policy
,
2798 load_mapping
, cache
);
2800 DMERR("could not load cache mappings");
2804 cache
->loaded_mappings
= true;
2807 if (!cache
->loaded_discards
) {
2808 r
= dm_cache_load_discards(cache
->cmd
, load_discard
, cache
);
2810 DMERR("could not load origin discards");
2814 cache
->loaded_discards
= true;
2820 static void cache_resume(struct dm_target
*ti
)
2822 struct cache
*cache
= ti
->private;
2824 cache
->need_tick_bio
= true;
2825 do_waker(&cache
->waker
.work
);
2831 * <#used metadata blocks>/<#total metadata blocks>
2832 * <#read hits> <#read misses> <#write hits> <#write misses>
2833 * <#demotions> <#promotions> <#blocks in cache> <#dirty>
2834 * <#features> <features>*
2835 * <#core args> <core args>
2836 * <#policy args> <policy args>*
2838 static void cache_status(struct dm_target
*ti
, status_type_t type
,
2839 unsigned status_flags
, char *result
, unsigned maxlen
)
2844 dm_block_t nr_free_blocks_metadata
= 0;
2845 dm_block_t nr_blocks_metadata
= 0;
2846 char buf
[BDEVNAME_SIZE
];
2847 struct cache
*cache
= ti
->private;
2848 dm_cblock_t residency
;
2851 case STATUSTYPE_INFO
:
2852 /* Commit to ensure statistics aren't out-of-date */
2853 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
)) {
2854 r
= dm_cache_commit(cache
->cmd
, false);
2856 DMERR("could not commit metadata for accurate status");
2859 r
= dm_cache_get_free_metadata_block_count(cache
->cmd
,
2860 &nr_free_blocks_metadata
);
2862 DMERR("could not get metadata free block count");
2866 r
= dm_cache_get_metadata_dev_size(cache
->cmd
, &nr_blocks_metadata
);
2868 DMERR("could not get metadata device size");
2872 residency
= policy_residency(cache
->policy
);
2874 DMEMIT("%llu/%llu %u %u %u %u %u %u %llu %u ",
2875 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
2876 (unsigned long long)nr_blocks_metadata
,
2877 (unsigned) atomic_read(&cache
->stats
.read_hit
),
2878 (unsigned) atomic_read(&cache
->stats
.read_miss
),
2879 (unsigned) atomic_read(&cache
->stats
.write_hit
),
2880 (unsigned) atomic_read(&cache
->stats
.write_miss
),
2881 (unsigned) atomic_read(&cache
->stats
.demotion
),
2882 (unsigned) atomic_read(&cache
->stats
.promotion
),
2883 (unsigned long long) from_cblock(residency
),
2886 if (writethrough_mode(&cache
->features
))
2887 DMEMIT("1 writethrough ");
2889 else if (passthrough_mode(&cache
->features
))
2890 DMEMIT("1 passthrough ");
2892 else if (writeback_mode(&cache
->features
))
2893 DMEMIT("1 writeback ");
2896 DMERR("internal error: unknown io mode: %d", (int) cache
->features
.io_mode
);
2900 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache
->migration_threshold
);
2902 r
= policy_emit_config_values(cache
->policy
, result
+ sz
, maxlen
- sz
);
2904 DMERR("policy_emit_config_values returned %d", r
);
2909 case STATUSTYPE_TABLE
:
2910 format_dev_t(buf
, cache
->metadata_dev
->bdev
->bd_dev
);
2912 format_dev_t(buf
, cache
->cache_dev
->bdev
->bd_dev
);
2914 format_dev_t(buf
, cache
->origin_dev
->bdev
->bd_dev
);
2917 for (i
= 0; i
< cache
->nr_ctr_args
- 1; i
++)
2918 DMEMIT(" %s", cache
->ctr_args
[i
]);
2919 if (cache
->nr_ctr_args
)
2920 DMEMIT(" %s", cache
->ctr_args
[cache
->nr_ctr_args
- 1]);
2930 * A cache block range can take two forms:
2932 * i) A single cblock, eg. '3456'
2933 * ii) A begin and end cblock with dots between, eg. 123-234
2935 static int parse_cblock_range(struct cache
*cache
, const char *str
,
2936 struct cblock_range
*result
)
2943 * Try and parse form (ii) first.
2945 r
= sscanf(str
, "%llu-%llu%c", &b
, &e
, &dummy
);
2950 result
->begin
= to_cblock(b
);
2951 result
->end
= to_cblock(e
);
2956 * That didn't work, try form (i).
2958 r
= sscanf(str
, "%llu%c", &b
, &dummy
);
2963 result
->begin
= to_cblock(b
);
2964 result
->end
= to_cblock(from_cblock(result
->begin
) + 1u);
2968 DMERR("invalid cblock range '%s'", str
);
2972 static int validate_cblock_range(struct cache
*cache
, struct cblock_range
*range
)
2974 uint64_t b
= from_cblock(range
->begin
);
2975 uint64_t e
= from_cblock(range
->end
);
2976 uint64_t n
= from_cblock(cache
->cache_size
);
2979 DMERR("begin cblock out of range: %llu >= %llu", b
, n
);
2984 DMERR("end cblock out of range: %llu > %llu", e
, n
);
2989 DMERR("invalid cblock range: %llu >= %llu", b
, e
);
2996 static int request_invalidation(struct cache
*cache
, struct cblock_range
*range
)
2998 struct invalidation_request req
;
3000 INIT_LIST_HEAD(&req
.list
);
3001 req
.cblocks
= range
;
3002 atomic_set(&req
.complete
, 0);
3004 init_waitqueue_head(&req
.result_wait
);
3006 spin_lock(&cache
->invalidation_lock
);
3007 list_add(&req
.list
, &cache
->invalidation_requests
);
3008 spin_unlock(&cache
->invalidation_lock
);
3011 wait_event(req
.result_wait
, atomic_read(&req
.complete
));
3015 static int process_invalidate_cblocks_message(struct cache
*cache
, unsigned count
,
3016 const char **cblock_ranges
)
3020 struct cblock_range range
;
3022 if (!passthrough_mode(&cache
->features
)) {
3023 DMERR("cache has to be in passthrough mode for invalidation");
3027 for (i
= 0; i
< count
; i
++) {
3028 r
= parse_cblock_range(cache
, cblock_ranges
[i
], &range
);
3032 r
= validate_cblock_range(cache
, &range
);
3037 * Pass begin and end origin blocks to the worker and wake it.
3039 r
= request_invalidation(cache
, &range
);
3051 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
3053 * The key migration_threshold is supported by the cache target core.
3055 static int cache_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
3057 struct cache
*cache
= ti
->private;
3062 if (!strcasecmp(argv
[0], "invalidate_cblocks"))
3063 return process_invalidate_cblocks_message(cache
, argc
- 1, (const char **) argv
+ 1);
3068 return set_config_value(cache
, argv
[0], argv
[1]);
3071 static int cache_iterate_devices(struct dm_target
*ti
,
3072 iterate_devices_callout_fn fn
, void *data
)
3075 struct cache
*cache
= ti
->private;
3077 r
= fn(ti
, cache
->cache_dev
, 0, get_dev_size(cache
->cache_dev
), data
);
3079 r
= fn(ti
, cache
->origin_dev
, 0, ti
->len
, data
);
3085 * We assume I/O is going to the origin (which is the volume
3086 * more likely to have restrictions e.g. by being striped).
3087 * (Looking up the exact location of the data would be expensive
3088 * and could always be out of date by the time the bio is submitted.)
3090 static int cache_bvec_merge(struct dm_target
*ti
,
3091 struct bvec_merge_data
*bvm
,
3092 struct bio_vec
*biovec
, int max_size
)
3094 struct cache
*cache
= ti
->private;
3095 struct request_queue
*q
= bdev_get_queue(cache
->origin_dev
->bdev
);
3097 if (!q
->merge_bvec_fn
)
3100 bvm
->bi_bdev
= cache
->origin_dev
->bdev
;
3101 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
3104 static void set_discard_limits(struct cache
*cache
, struct queue_limits
*limits
)
3107 * FIXME: these limits may be incompatible with the cache device
3109 limits
->max_discard_sectors
= cache
->discard_block_size
* 1024;
3110 limits
->discard_granularity
= cache
->discard_block_size
<< SECTOR_SHIFT
;
3113 static void cache_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
3115 struct cache
*cache
= ti
->private;
3116 uint64_t io_opt_sectors
= limits
->io_opt
>> SECTOR_SHIFT
;
3119 * If the system-determined stacked limits are compatible with the
3120 * cache's blocksize (io_opt is a factor) do not override them.
3122 if (io_opt_sectors
< cache
->sectors_per_block
||
3123 do_div(io_opt_sectors
, cache
->sectors_per_block
)) {
3124 blk_limits_io_min(limits
, 0);
3125 blk_limits_io_opt(limits
, cache
->sectors_per_block
<< SECTOR_SHIFT
);
3127 set_discard_limits(cache
, limits
);
3130 /*----------------------------------------------------------------*/
3132 static struct target_type cache_target
= {
3134 .version
= {1, 2, 0},
3135 .module
= THIS_MODULE
,
3139 .end_io
= cache_end_io
,
3140 .postsuspend
= cache_postsuspend
,
3141 .preresume
= cache_preresume
,
3142 .resume
= cache_resume
,
3143 .status
= cache_status
,
3144 .message
= cache_message
,
3145 .iterate_devices
= cache_iterate_devices
,
3146 .merge
= cache_bvec_merge
,
3147 .io_hints
= cache_io_hints
,
3150 static int __init
dm_cache_init(void)
3154 r
= dm_register_target(&cache_target
);
3156 DMERR("cache target registration failed: %d", r
);
3160 migration_cache
= KMEM_CACHE(dm_cache_migration
, 0);
3161 if (!migration_cache
) {
3162 dm_unregister_target(&cache_target
);
3169 static void __exit
dm_cache_exit(void)
3171 dm_unregister_target(&cache_target
);
3172 kmem_cache_destroy(migration_cache
);
3175 module_init(dm_cache_init
);
3176 module_exit(dm_cache_exit
);
3178 MODULE_DESCRIPTION(DM_NAME
" cache target");
3179 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3180 MODULE_LICENSE("GPL");