drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / drivers / md / dm-cache-target.c
blobaaeeabfab09b0add08bdae18d816ea9541901ccb
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Red Hat. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #include "dm.h"
9 #include "dm-bio-prison-v2.h"
10 #include "dm-bio-record.h"
11 #include "dm-cache-metadata.h"
12 #include "dm-io-tracker.h"
14 #include <linux/dm-io.h>
15 #include <linux/dm-kcopyd.h>
16 #include <linux/jiffies.h>
17 #include <linux/init.h>
18 #include <linux/mempool.h>
19 #include <linux/module.h>
20 #include <linux/rwsem.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
24 #define DM_MSG_PREFIX "cache"
26 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
27 "A percentage of time allocated for copying to and/or from cache");
29 /*----------------------------------------------------------------*/
32 * Glossary:
34 * oblock: index of an origin block
35 * cblock: index of a cache block
36 * promotion: movement of a block from origin to cache
37 * demotion: movement of a block from cache to origin
38 * migration: movement of a block between the origin and cache device,
39 * either direction
42 /*----------------------------------------------------------------*/
45 * Represents a chunk of future work. 'input' allows continuations to pass
46 * values between themselves, typically error values.
48 struct continuation {
49 struct work_struct ws;
50 blk_status_t input;
53 static inline void init_continuation(struct continuation *k,
54 void (*fn)(struct work_struct *))
56 INIT_WORK(&k->ws, fn);
57 k->input = 0;
60 static inline void queue_continuation(struct workqueue_struct *wq,
61 struct continuation *k)
63 queue_work(wq, &k->ws);
66 /*----------------------------------------------------------------*/
69 * The batcher collects together pieces of work that need a particular
70 * operation to occur before they can proceed (typically a commit).
72 struct batcher {
74 * The operation that everyone is waiting for.
76 blk_status_t (*commit_op)(void *context);
77 void *commit_context;
80 * This is how bios should be issued once the commit op is complete
81 * (accounted_request).
83 void (*issue_op)(struct bio *bio, void *context);
84 void *issue_context;
87 * Queued work gets put on here after commit.
89 struct workqueue_struct *wq;
91 spinlock_t lock;
92 struct list_head work_items;
93 struct bio_list bios;
94 struct work_struct commit_work;
96 bool commit_scheduled;
99 static void __commit(struct work_struct *_ws)
101 struct batcher *b = container_of(_ws, struct batcher, commit_work);
102 blk_status_t r;
103 struct list_head work_items;
104 struct work_struct *ws, *tmp;
105 struct continuation *k;
106 struct bio *bio;
107 struct bio_list bios;
109 INIT_LIST_HEAD(&work_items);
110 bio_list_init(&bios);
113 * We have to grab these before the commit_op to avoid a race
114 * condition.
116 spin_lock_irq(&b->lock);
117 list_splice_init(&b->work_items, &work_items);
118 bio_list_merge_init(&bios, &b->bios);
119 b->commit_scheduled = false;
120 spin_unlock_irq(&b->lock);
122 r = b->commit_op(b->commit_context);
124 list_for_each_entry_safe(ws, tmp, &work_items, entry) {
125 k = container_of(ws, struct continuation, ws);
126 k->input = r;
127 INIT_LIST_HEAD(&ws->entry); /* to avoid a WARN_ON */
128 queue_work(b->wq, ws);
131 while ((bio = bio_list_pop(&bios))) {
132 if (r) {
133 bio->bi_status = r;
134 bio_endio(bio);
135 } else
136 b->issue_op(bio, b->issue_context);
140 static void batcher_init(struct batcher *b,
141 blk_status_t (*commit_op)(void *),
142 void *commit_context,
143 void (*issue_op)(struct bio *bio, void *),
144 void *issue_context,
145 struct workqueue_struct *wq)
147 b->commit_op = commit_op;
148 b->commit_context = commit_context;
149 b->issue_op = issue_op;
150 b->issue_context = issue_context;
151 b->wq = wq;
153 spin_lock_init(&b->lock);
154 INIT_LIST_HEAD(&b->work_items);
155 bio_list_init(&b->bios);
156 INIT_WORK(&b->commit_work, __commit);
157 b->commit_scheduled = false;
160 static void async_commit(struct batcher *b)
162 queue_work(b->wq, &b->commit_work);
165 static void continue_after_commit(struct batcher *b, struct continuation *k)
167 bool commit_scheduled;
169 spin_lock_irq(&b->lock);
170 commit_scheduled = b->commit_scheduled;
171 list_add_tail(&k->ws.entry, &b->work_items);
172 spin_unlock_irq(&b->lock);
174 if (commit_scheduled)
175 async_commit(b);
179 * Bios are errored if commit failed.
181 static void issue_after_commit(struct batcher *b, struct bio *bio)
183 bool commit_scheduled;
185 spin_lock_irq(&b->lock);
186 commit_scheduled = b->commit_scheduled;
187 bio_list_add(&b->bios, bio);
188 spin_unlock_irq(&b->lock);
190 if (commit_scheduled)
191 async_commit(b);
195 * Call this if some urgent work is waiting for the commit to complete.
197 static void schedule_commit(struct batcher *b)
199 bool immediate;
201 spin_lock_irq(&b->lock);
202 immediate = !list_empty(&b->work_items) || !bio_list_empty(&b->bios);
203 b->commit_scheduled = true;
204 spin_unlock_irq(&b->lock);
206 if (immediate)
207 async_commit(b);
211 * There are a couple of places where we let a bio run, but want to do some
212 * work before calling its endio function. We do this by temporarily
213 * changing the endio fn.
215 struct dm_hook_info {
216 bio_end_io_t *bi_end_io;
219 static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
220 bio_end_io_t *bi_end_io, void *bi_private)
222 h->bi_end_io = bio->bi_end_io;
224 bio->bi_end_io = bi_end_io;
225 bio->bi_private = bi_private;
228 static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
230 bio->bi_end_io = h->bi_end_io;
233 /*----------------------------------------------------------------*/
235 #define MIGRATION_POOL_SIZE 128
236 #define COMMIT_PERIOD HZ
237 #define MIGRATION_COUNT_WINDOW 10
240 * The block size of the device holding cache data must be
241 * between 32KB and 1GB.
243 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
244 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
246 enum cache_metadata_mode {
247 CM_WRITE, /* metadata may be changed */
248 CM_READ_ONLY, /* metadata may not be changed */
249 CM_FAIL
252 enum cache_io_mode {
254 * Data is written to cached blocks only. These blocks are marked
255 * dirty. If you lose the cache device you will lose data.
256 * Potential performance increase for both reads and writes.
258 CM_IO_WRITEBACK,
261 * Data is written to both cache and origin. Blocks are never
262 * dirty. Potential performance benfit for reads only.
264 CM_IO_WRITETHROUGH,
267 * A degraded mode useful for various cache coherency situations
268 * (eg, rolling back snapshots). Reads and writes always go to the
269 * origin. If a write goes to a cached oblock, then the cache
270 * block is invalidated.
272 CM_IO_PASSTHROUGH
275 struct cache_features {
276 enum cache_metadata_mode mode;
277 enum cache_io_mode io_mode;
278 unsigned int metadata_version;
279 bool discard_passdown:1;
282 struct cache_stats {
283 atomic_t read_hit;
284 atomic_t read_miss;
285 atomic_t write_hit;
286 atomic_t write_miss;
287 atomic_t demotion;
288 atomic_t promotion;
289 atomic_t writeback;
290 atomic_t copies_avoided;
291 atomic_t cache_cell_clash;
292 atomic_t commit_count;
293 atomic_t discard_count;
296 struct cache {
297 struct dm_target *ti;
298 spinlock_t lock;
301 * Fields for converting from sectors to blocks.
303 int sectors_per_block_shift;
304 sector_t sectors_per_block;
306 struct dm_cache_metadata *cmd;
309 * Metadata is written to this device.
311 struct dm_dev *metadata_dev;
314 * The slower of the two data devices. Typically a spindle.
316 struct dm_dev *origin_dev;
319 * The faster of the two data devices. Typically an SSD.
321 struct dm_dev *cache_dev;
324 * Size of the origin device in _complete_ blocks and native sectors.
326 dm_oblock_t origin_blocks;
327 sector_t origin_sectors;
330 * Size of the cache device in blocks.
332 dm_cblock_t cache_size;
335 * Invalidation fields.
337 spinlock_t invalidation_lock;
338 struct list_head invalidation_requests;
340 sector_t migration_threshold;
341 wait_queue_head_t migration_wait;
342 atomic_t nr_allocated_migrations;
345 * The number of in flight migrations that are performing
346 * background io. eg, promotion, writeback.
348 atomic_t nr_io_migrations;
350 struct bio_list deferred_bios;
352 struct rw_semaphore quiesce_lock;
355 * origin_blocks entries, discarded if set.
357 dm_dblock_t discard_nr_blocks;
358 unsigned long *discard_bitset;
359 uint32_t discard_block_size; /* a power of 2 times sectors per block */
362 * Rather than reconstructing the table line for the status we just
363 * save it and regurgitate.
365 unsigned int nr_ctr_args;
366 const char **ctr_args;
368 struct dm_kcopyd_client *copier;
369 struct work_struct deferred_bio_worker;
370 struct work_struct migration_worker;
371 struct workqueue_struct *wq;
372 struct delayed_work waker;
373 struct dm_bio_prison_v2 *prison;
376 * cache_size entries, dirty if set
378 unsigned long *dirty_bitset;
379 atomic_t nr_dirty;
381 unsigned int policy_nr_args;
382 struct dm_cache_policy *policy;
385 * Cache features such as write-through.
387 struct cache_features features;
389 struct cache_stats stats;
391 bool need_tick_bio:1;
392 bool sized:1;
393 bool invalidate:1;
394 bool commit_requested:1;
395 bool loaded_mappings:1;
396 bool loaded_discards:1;
398 struct rw_semaphore background_work_lock;
400 struct batcher committer;
401 struct work_struct commit_ws;
403 struct dm_io_tracker tracker;
405 mempool_t migration_pool;
407 struct bio_set bs;
410 struct per_bio_data {
411 bool tick:1;
412 unsigned int req_nr:2;
413 struct dm_bio_prison_cell_v2 *cell;
414 struct dm_hook_info hook_info;
415 sector_t len;
418 struct dm_cache_migration {
419 struct continuation k;
420 struct cache *cache;
422 struct policy_work *op;
423 struct bio *overwrite_bio;
424 struct dm_bio_prison_cell_v2 *cell;
426 dm_cblock_t invalidate_cblock;
427 dm_oblock_t invalidate_oblock;
430 /*----------------------------------------------------------------*/
432 static bool writethrough_mode(struct cache *cache)
434 return cache->features.io_mode == CM_IO_WRITETHROUGH;
437 static bool writeback_mode(struct cache *cache)
439 return cache->features.io_mode == CM_IO_WRITEBACK;
442 static inline bool passthrough_mode(struct cache *cache)
444 return unlikely(cache->features.io_mode == CM_IO_PASSTHROUGH);
447 /*----------------------------------------------------------------*/
449 static void wake_deferred_bio_worker(struct cache *cache)
451 queue_work(cache->wq, &cache->deferred_bio_worker);
454 static void wake_migration_worker(struct cache *cache)
456 if (passthrough_mode(cache))
457 return;
459 queue_work(cache->wq, &cache->migration_worker);
462 /*----------------------------------------------------------------*/
464 static struct dm_bio_prison_cell_v2 *alloc_prison_cell(struct cache *cache)
466 return dm_bio_prison_alloc_cell_v2(cache->prison, GFP_NOIO);
469 static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell_v2 *cell)
471 dm_bio_prison_free_cell_v2(cache->prison, cell);
474 static struct dm_cache_migration *alloc_migration(struct cache *cache)
476 struct dm_cache_migration *mg;
478 mg = mempool_alloc(&cache->migration_pool, GFP_NOIO);
480 memset(mg, 0, sizeof(*mg));
482 mg->cache = cache;
483 atomic_inc(&cache->nr_allocated_migrations);
485 return mg;
488 static void free_migration(struct dm_cache_migration *mg)
490 struct cache *cache = mg->cache;
492 if (atomic_dec_and_test(&cache->nr_allocated_migrations))
493 wake_up(&cache->migration_wait);
495 mempool_free(mg, &cache->migration_pool);
498 /*----------------------------------------------------------------*/
500 static inline dm_oblock_t oblock_succ(dm_oblock_t b)
502 return to_oblock(from_oblock(b) + 1ull);
505 static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key_v2 *key)
507 key->virtual = 0;
508 key->dev = 0;
509 key->block_begin = from_oblock(begin);
510 key->block_end = from_oblock(end);
514 * We have two lock levels. Level 0, which is used to prevent WRITEs, and
515 * level 1 which prevents *both* READs and WRITEs.
517 #define WRITE_LOCK_LEVEL 0
518 #define READ_WRITE_LOCK_LEVEL 1
520 static unsigned int lock_level(struct bio *bio)
522 return bio_data_dir(bio) == WRITE ?
523 WRITE_LOCK_LEVEL :
524 READ_WRITE_LOCK_LEVEL;
528 *--------------------------------------------------------------
529 * Per bio data
530 *--------------------------------------------------------------
533 static struct per_bio_data *get_per_bio_data(struct bio *bio)
535 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
537 BUG_ON(!pb);
538 return pb;
541 static struct per_bio_data *init_per_bio_data(struct bio *bio)
543 struct per_bio_data *pb = get_per_bio_data(bio);
545 pb->tick = false;
546 pb->req_nr = dm_bio_get_target_bio_nr(bio);
547 pb->cell = NULL;
548 pb->len = 0;
550 return pb;
553 /*----------------------------------------------------------------*/
555 static void defer_bio(struct cache *cache, struct bio *bio)
557 spin_lock_irq(&cache->lock);
558 bio_list_add(&cache->deferred_bios, bio);
559 spin_unlock_irq(&cache->lock);
561 wake_deferred_bio_worker(cache);
564 static void defer_bios(struct cache *cache, struct bio_list *bios)
566 spin_lock_irq(&cache->lock);
567 bio_list_merge_init(&cache->deferred_bios, bios);
568 spin_unlock_irq(&cache->lock);
570 wake_deferred_bio_worker(cache);
573 /*----------------------------------------------------------------*/
575 static bool bio_detain_shared(struct cache *cache, dm_oblock_t oblock, struct bio *bio)
577 bool r;
578 struct per_bio_data *pb;
579 struct dm_cell_key_v2 key;
580 dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL);
581 struct dm_bio_prison_cell_v2 *cell_prealloc, *cell;
583 cell_prealloc = alloc_prison_cell(cache); /* FIXME: allow wait if calling from worker */
585 build_key(oblock, end, &key);
586 r = dm_cell_get_v2(cache->prison, &key, lock_level(bio), bio, cell_prealloc, &cell);
587 if (!r) {
589 * Failed to get the lock.
591 free_prison_cell(cache, cell_prealloc);
592 return r;
595 if (cell != cell_prealloc)
596 free_prison_cell(cache, cell_prealloc);
598 pb = get_per_bio_data(bio);
599 pb->cell = cell;
601 return r;
604 /*----------------------------------------------------------------*/
606 static bool is_dirty(struct cache *cache, dm_cblock_t b)
608 return test_bit(from_cblock(b), cache->dirty_bitset);
611 static void set_dirty(struct cache *cache, dm_cblock_t cblock)
613 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
614 atomic_inc(&cache->nr_dirty);
615 policy_set_dirty(cache->policy, cblock);
620 * These two are called when setting after migrations to force the policy
621 * and dirty bitset to be in sync.
623 static void force_set_dirty(struct cache *cache, dm_cblock_t cblock)
625 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset))
626 atomic_inc(&cache->nr_dirty);
627 policy_set_dirty(cache->policy, cblock);
630 static void force_clear_dirty(struct cache *cache, dm_cblock_t cblock)
632 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
633 if (atomic_dec_return(&cache->nr_dirty) == 0)
634 dm_table_event(cache->ti->table);
637 policy_clear_dirty(cache->policy, cblock);
640 /*----------------------------------------------------------------*/
642 static bool block_size_is_power_of_two(struct cache *cache)
644 return cache->sectors_per_block_shift >= 0;
647 static dm_block_t block_div(dm_block_t b, uint32_t n)
649 do_div(b, n);
651 return b;
654 static dm_block_t oblocks_per_dblock(struct cache *cache)
656 dm_block_t oblocks = cache->discard_block_size;
658 if (block_size_is_power_of_two(cache))
659 oblocks >>= cache->sectors_per_block_shift;
660 else
661 oblocks = block_div(oblocks, cache->sectors_per_block);
663 return oblocks;
666 static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
668 return to_dblock(block_div(from_oblock(oblock),
669 oblocks_per_dblock(cache)));
672 static void set_discard(struct cache *cache, dm_dblock_t b)
674 BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks));
675 atomic_inc(&cache->stats.discard_count);
677 spin_lock_irq(&cache->lock);
678 set_bit(from_dblock(b), cache->discard_bitset);
679 spin_unlock_irq(&cache->lock);
682 static void clear_discard(struct cache *cache, dm_dblock_t b)
684 spin_lock_irq(&cache->lock);
685 clear_bit(from_dblock(b), cache->discard_bitset);
686 spin_unlock_irq(&cache->lock);
689 static bool is_discarded(struct cache *cache, dm_dblock_t b)
691 int r;
693 spin_lock_irq(&cache->lock);
694 r = test_bit(from_dblock(b), cache->discard_bitset);
695 spin_unlock_irq(&cache->lock);
697 return r;
700 static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
702 int r;
704 spin_lock_irq(&cache->lock);
705 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
706 cache->discard_bitset);
707 spin_unlock_irq(&cache->lock);
709 return r;
713 * -------------------------------------------------------------
714 * Remapping
715 *--------------------------------------------------------------
717 static void remap_to_origin(struct cache *cache, struct bio *bio)
719 bio_set_dev(bio, cache->origin_dev->bdev);
722 static void remap_to_cache(struct cache *cache, struct bio *bio,
723 dm_cblock_t cblock)
725 sector_t bi_sector = bio->bi_iter.bi_sector;
726 sector_t block = from_cblock(cblock);
728 bio_set_dev(bio, cache->cache_dev->bdev);
729 if (!block_size_is_power_of_two(cache))
730 bio->bi_iter.bi_sector =
731 (block * cache->sectors_per_block) +
732 sector_div(bi_sector, cache->sectors_per_block);
733 else
734 bio->bi_iter.bi_sector =
735 (block << cache->sectors_per_block_shift) |
736 (bi_sector & (cache->sectors_per_block - 1));
739 static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
741 struct per_bio_data *pb;
743 spin_lock_irq(&cache->lock);
744 if (cache->need_tick_bio && !op_is_flush(bio->bi_opf) &&
745 bio_op(bio) != REQ_OP_DISCARD) {
746 pb = get_per_bio_data(bio);
747 pb->tick = true;
748 cache->need_tick_bio = false;
750 spin_unlock_irq(&cache->lock);
753 static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
754 dm_oblock_t oblock)
756 // FIXME: check_if_tick_bio_needed() is called way too much through this interface
757 check_if_tick_bio_needed(cache, bio);
758 remap_to_origin(cache, bio);
759 if (bio_data_dir(bio) == WRITE)
760 clear_discard(cache, oblock_to_dblock(cache, oblock));
763 static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
764 dm_oblock_t oblock, dm_cblock_t cblock)
766 check_if_tick_bio_needed(cache, bio);
767 remap_to_cache(cache, bio, cblock);
768 if (bio_data_dir(bio) == WRITE) {
769 set_dirty(cache, cblock);
770 clear_discard(cache, oblock_to_dblock(cache, oblock));
774 static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
776 sector_t block_nr = bio->bi_iter.bi_sector;
778 if (!block_size_is_power_of_two(cache))
779 (void) sector_div(block_nr, cache->sectors_per_block);
780 else
781 block_nr >>= cache->sectors_per_block_shift;
783 return to_oblock(block_nr);
786 static bool accountable_bio(struct cache *cache, struct bio *bio)
788 return bio_op(bio) != REQ_OP_DISCARD;
791 static void accounted_begin(struct cache *cache, struct bio *bio)
793 struct per_bio_data *pb;
795 if (accountable_bio(cache, bio)) {
796 pb = get_per_bio_data(bio);
797 pb->len = bio_sectors(bio);
798 dm_iot_io_begin(&cache->tracker, pb->len);
802 static void accounted_complete(struct cache *cache, struct bio *bio)
804 struct per_bio_data *pb = get_per_bio_data(bio);
806 dm_iot_io_end(&cache->tracker, pb->len);
809 static void accounted_request(struct cache *cache, struct bio *bio)
811 accounted_begin(cache, bio);
812 dm_submit_bio_remap(bio, NULL);
815 static void issue_op(struct bio *bio, void *context)
817 struct cache *cache = context;
819 accounted_request(cache, bio);
823 * When running in writethrough mode we need to send writes to clean blocks
824 * to both the cache and origin devices. Clone the bio and send them in parallel.
826 static void remap_to_origin_and_cache(struct cache *cache, struct bio *bio,
827 dm_oblock_t oblock, dm_cblock_t cblock)
829 struct bio *origin_bio = bio_alloc_clone(cache->origin_dev->bdev, bio,
830 GFP_NOIO, &cache->bs);
832 BUG_ON(!origin_bio);
834 bio_chain(origin_bio, bio);
836 if (bio_data_dir(origin_bio) == WRITE)
837 clear_discard(cache, oblock_to_dblock(cache, oblock));
838 submit_bio(origin_bio);
840 remap_to_cache(cache, bio, cblock);
844 *--------------------------------------------------------------
845 * Failure modes
846 *--------------------------------------------------------------
848 static enum cache_metadata_mode get_cache_mode(struct cache *cache)
850 return cache->features.mode;
853 static const char *cache_device_name(struct cache *cache)
855 return dm_table_device_name(cache->ti->table);
858 static void notify_mode_switch(struct cache *cache, enum cache_metadata_mode mode)
860 static const char *descs[] = {
861 "write",
862 "read-only",
863 "fail"
866 dm_table_event(cache->ti->table);
867 DMINFO("%s: switching cache to %s mode",
868 cache_device_name(cache), descs[(int)mode]);
871 static void set_cache_mode(struct cache *cache, enum cache_metadata_mode new_mode)
873 bool needs_check;
874 enum cache_metadata_mode old_mode = get_cache_mode(cache);
876 if (dm_cache_metadata_needs_check(cache->cmd, &needs_check)) {
877 DMERR("%s: unable to read needs_check flag, setting failure mode.",
878 cache_device_name(cache));
879 new_mode = CM_FAIL;
882 if (new_mode == CM_WRITE && needs_check) {
883 DMERR("%s: unable to switch cache to write mode until repaired.",
884 cache_device_name(cache));
885 if (old_mode != new_mode)
886 new_mode = old_mode;
887 else
888 new_mode = CM_READ_ONLY;
891 /* Never move out of fail mode */
892 if (old_mode == CM_FAIL)
893 new_mode = CM_FAIL;
895 switch (new_mode) {
896 case CM_FAIL:
897 case CM_READ_ONLY:
898 dm_cache_metadata_set_read_only(cache->cmd);
899 break;
901 case CM_WRITE:
902 dm_cache_metadata_set_read_write(cache->cmd);
903 break;
906 cache->features.mode = new_mode;
908 if (new_mode != old_mode)
909 notify_mode_switch(cache, new_mode);
912 static void abort_transaction(struct cache *cache)
914 const char *dev_name = cache_device_name(cache);
916 if (get_cache_mode(cache) >= CM_READ_ONLY)
917 return;
919 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
920 if (dm_cache_metadata_abort(cache->cmd)) {
921 DMERR("%s: failed to abort metadata transaction", dev_name);
922 set_cache_mode(cache, CM_FAIL);
925 if (dm_cache_metadata_set_needs_check(cache->cmd)) {
926 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
927 set_cache_mode(cache, CM_FAIL);
931 static void metadata_operation_failed(struct cache *cache, const char *op, int r)
933 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
934 cache_device_name(cache), op, r);
935 abort_transaction(cache);
936 set_cache_mode(cache, CM_READ_ONLY);
939 /*----------------------------------------------------------------*/
941 static void load_stats(struct cache *cache)
943 struct dm_cache_statistics stats;
945 dm_cache_metadata_get_stats(cache->cmd, &stats);
946 atomic_set(&cache->stats.read_hit, stats.read_hits);
947 atomic_set(&cache->stats.read_miss, stats.read_misses);
948 atomic_set(&cache->stats.write_hit, stats.write_hits);
949 atomic_set(&cache->stats.write_miss, stats.write_misses);
952 static void save_stats(struct cache *cache)
954 struct dm_cache_statistics stats;
956 if (get_cache_mode(cache) >= CM_READ_ONLY)
957 return;
959 stats.read_hits = atomic_read(&cache->stats.read_hit);
960 stats.read_misses = atomic_read(&cache->stats.read_miss);
961 stats.write_hits = atomic_read(&cache->stats.write_hit);
962 stats.write_misses = atomic_read(&cache->stats.write_miss);
964 dm_cache_metadata_set_stats(cache->cmd, &stats);
967 static void update_stats(struct cache_stats *stats, enum policy_operation op)
969 switch (op) {
970 case POLICY_PROMOTE:
971 atomic_inc(&stats->promotion);
972 break;
974 case POLICY_DEMOTE:
975 atomic_inc(&stats->demotion);
976 break;
978 case POLICY_WRITEBACK:
979 atomic_inc(&stats->writeback);
980 break;
985 *---------------------------------------------------------------------
986 * Migration processing
988 * Migration covers moving data from the origin device to the cache, or
989 * vice versa.
990 *---------------------------------------------------------------------
992 static void inc_io_migrations(struct cache *cache)
994 atomic_inc(&cache->nr_io_migrations);
997 static void dec_io_migrations(struct cache *cache)
999 atomic_dec(&cache->nr_io_migrations);
1002 static bool discard_or_flush(struct bio *bio)
1004 return bio_op(bio) == REQ_OP_DISCARD || op_is_flush(bio->bi_opf);
1007 static void calc_discard_block_range(struct cache *cache, struct bio *bio,
1008 dm_dblock_t *b, dm_dblock_t *e)
1010 sector_t sb = bio->bi_iter.bi_sector;
1011 sector_t se = bio_end_sector(bio);
1013 *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
1015 if (se - sb < cache->discard_block_size)
1016 *e = *b;
1017 else
1018 *e = to_dblock(block_div(se, cache->discard_block_size));
1021 /*----------------------------------------------------------------*/
1023 static void prevent_background_work(struct cache *cache)
1025 lockdep_off();
1026 down_write(&cache->background_work_lock);
1027 lockdep_on();
1030 static void allow_background_work(struct cache *cache)
1032 lockdep_off();
1033 up_write(&cache->background_work_lock);
1034 lockdep_on();
1037 static bool background_work_begin(struct cache *cache)
1039 bool r;
1041 lockdep_off();
1042 r = down_read_trylock(&cache->background_work_lock);
1043 lockdep_on();
1045 return r;
1048 static void background_work_end(struct cache *cache)
1050 lockdep_off();
1051 up_read(&cache->background_work_lock);
1052 lockdep_on();
1055 /*----------------------------------------------------------------*/
1057 static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1059 return (bio_data_dir(bio) == WRITE) &&
1060 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
1063 static bool optimisable_bio(struct cache *cache, struct bio *bio, dm_oblock_t block)
1065 return writeback_mode(cache) &&
1066 (is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio));
1069 static void quiesce(struct dm_cache_migration *mg,
1070 void (*continuation)(struct work_struct *))
1072 init_continuation(&mg->k, continuation);
1073 dm_cell_quiesce_v2(mg->cache->prison, mg->cell, &mg->k.ws);
1076 static struct dm_cache_migration *ws_to_mg(struct work_struct *ws)
1078 struct continuation *k = container_of(ws, struct continuation, ws);
1080 return container_of(k, struct dm_cache_migration, k);
1083 static void copy_complete(int read_err, unsigned long write_err, void *context)
1085 struct dm_cache_migration *mg = container_of(context, struct dm_cache_migration, k);
1087 if (read_err || write_err)
1088 mg->k.input = BLK_STS_IOERR;
1090 queue_continuation(mg->cache->wq, &mg->k);
1093 static void copy(struct dm_cache_migration *mg, bool promote)
1095 struct dm_io_region o_region, c_region;
1096 struct cache *cache = mg->cache;
1098 o_region.bdev = cache->origin_dev->bdev;
1099 o_region.sector = from_oblock(mg->op->oblock) * cache->sectors_per_block;
1100 o_region.count = cache->sectors_per_block;
1102 c_region.bdev = cache->cache_dev->bdev;
1103 c_region.sector = from_cblock(mg->op->cblock) * cache->sectors_per_block;
1104 c_region.count = cache->sectors_per_block;
1106 if (promote)
1107 dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, &mg->k);
1108 else
1109 dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, &mg->k);
1112 static void bio_drop_shared_lock(struct cache *cache, struct bio *bio)
1114 struct per_bio_data *pb = get_per_bio_data(bio);
1116 if (pb->cell && dm_cell_put_v2(cache->prison, pb->cell))
1117 free_prison_cell(cache, pb->cell);
1118 pb->cell = NULL;
1121 static void overwrite_endio(struct bio *bio)
1123 struct dm_cache_migration *mg = bio->bi_private;
1124 struct cache *cache = mg->cache;
1125 struct per_bio_data *pb = get_per_bio_data(bio);
1127 dm_unhook_bio(&pb->hook_info, bio);
1129 if (bio->bi_status)
1130 mg->k.input = bio->bi_status;
1132 queue_continuation(cache->wq, &mg->k);
1135 static void overwrite(struct dm_cache_migration *mg,
1136 void (*continuation)(struct work_struct *))
1138 struct bio *bio = mg->overwrite_bio;
1139 struct per_bio_data *pb = get_per_bio_data(bio);
1141 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1144 * The overwrite bio is part of the copy operation, as such it does
1145 * not set/clear discard or dirty flags.
1147 if (mg->op->op == POLICY_PROMOTE)
1148 remap_to_cache(mg->cache, bio, mg->op->cblock);
1149 else
1150 remap_to_origin(mg->cache, bio);
1152 init_continuation(&mg->k, continuation);
1153 accounted_request(mg->cache, bio);
1157 * Migration steps:
1159 * 1) exclusive lock preventing WRITEs
1160 * 2) quiesce
1161 * 3) copy or issue overwrite bio
1162 * 4) upgrade to exclusive lock preventing READs and WRITEs
1163 * 5) quiesce
1164 * 6) update metadata and commit
1165 * 7) unlock
1167 static void mg_complete(struct dm_cache_migration *mg, bool success)
1169 struct bio_list bios;
1170 struct cache *cache = mg->cache;
1171 struct policy_work *op = mg->op;
1172 dm_cblock_t cblock = op->cblock;
1174 if (success)
1175 update_stats(&cache->stats, op->op);
1177 switch (op->op) {
1178 case POLICY_PROMOTE:
1179 clear_discard(cache, oblock_to_dblock(cache, op->oblock));
1180 policy_complete_background_work(cache->policy, op, success);
1182 if (mg->overwrite_bio) {
1183 if (success)
1184 force_set_dirty(cache, cblock);
1185 else if (mg->k.input)
1186 mg->overwrite_bio->bi_status = mg->k.input;
1187 else
1188 mg->overwrite_bio->bi_status = BLK_STS_IOERR;
1189 bio_endio(mg->overwrite_bio);
1190 } else {
1191 if (success)
1192 force_clear_dirty(cache, cblock);
1193 dec_io_migrations(cache);
1195 break;
1197 case POLICY_DEMOTE:
1199 * We clear dirty here to update the nr_dirty counter.
1201 if (success)
1202 force_clear_dirty(cache, cblock);
1203 policy_complete_background_work(cache->policy, op, success);
1204 dec_io_migrations(cache);
1205 break;
1207 case POLICY_WRITEBACK:
1208 if (success)
1209 force_clear_dirty(cache, cblock);
1210 policy_complete_background_work(cache->policy, op, success);
1211 dec_io_migrations(cache);
1212 break;
1215 bio_list_init(&bios);
1216 if (mg->cell) {
1217 if (dm_cell_unlock_v2(cache->prison, mg->cell, &bios))
1218 free_prison_cell(cache, mg->cell);
1221 free_migration(mg);
1222 defer_bios(cache, &bios);
1223 wake_migration_worker(cache);
1225 background_work_end(cache);
1228 static void mg_success(struct work_struct *ws)
1230 struct dm_cache_migration *mg = ws_to_mg(ws);
1232 mg_complete(mg, mg->k.input == 0);
1235 static void mg_update_metadata(struct work_struct *ws)
1237 int r;
1238 struct dm_cache_migration *mg = ws_to_mg(ws);
1239 struct cache *cache = mg->cache;
1240 struct policy_work *op = mg->op;
1242 switch (op->op) {
1243 case POLICY_PROMOTE:
1244 r = dm_cache_insert_mapping(cache->cmd, op->cblock, op->oblock);
1245 if (r) {
1246 DMERR_LIMIT("%s: migration failed; couldn't insert mapping",
1247 cache_device_name(cache));
1248 metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
1250 mg_complete(mg, false);
1251 return;
1253 mg_complete(mg, true);
1254 break;
1256 case POLICY_DEMOTE:
1257 r = dm_cache_remove_mapping(cache->cmd, op->cblock);
1258 if (r) {
1259 DMERR_LIMIT("%s: migration failed; couldn't update on disk metadata",
1260 cache_device_name(cache));
1261 metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
1263 mg_complete(mg, false);
1264 return;
1268 * It would be nice if we only had to commit when a REQ_FLUSH
1269 * comes through. But there's one scenario that we have to
1270 * look out for:
1272 * - vblock x in a cache block
1273 * - domotion occurs
1274 * - cache block gets reallocated and over written
1275 * - crash
1277 * When we recover, because there was no commit the cache will
1278 * rollback to having the data for vblock x in the cache block.
1279 * But the cache block has since been overwritten, so it'll end
1280 * up pointing to data that was never in 'x' during the history
1281 * of the device.
1283 * To avoid this issue we require a commit as part of the
1284 * demotion operation.
1286 init_continuation(&mg->k, mg_success);
1287 continue_after_commit(&cache->committer, &mg->k);
1288 schedule_commit(&cache->committer);
1289 break;
1291 case POLICY_WRITEBACK:
1292 mg_complete(mg, true);
1293 break;
1297 static void mg_update_metadata_after_copy(struct work_struct *ws)
1299 struct dm_cache_migration *mg = ws_to_mg(ws);
1302 * Did the copy succeed?
1304 if (mg->k.input)
1305 mg_complete(mg, false);
1306 else
1307 mg_update_metadata(ws);
1310 static void mg_upgrade_lock(struct work_struct *ws)
1312 int r;
1313 struct dm_cache_migration *mg = ws_to_mg(ws);
1316 * Did the copy succeed?
1318 if (mg->k.input)
1319 mg_complete(mg, false);
1321 else {
1323 * Now we want the lock to prevent both reads and writes.
1325 r = dm_cell_lock_promote_v2(mg->cache->prison, mg->cell,
1326 READ_WRITE_LOCK_LEVEL);
1327 if (r < 0)
1328 mg_complete(mg, false);
1330 else if (r)
1331 quiesce(mg, mg_update_metadata);
1333 else
1334 mg_update_metadata(ws);
1338 static void mg_full_copy(struct work_struct *ws)
1340 struct dm_cache_migration *mg = ws_to_mg(ws);
1341 struct cache *cache = mg->cache;
1342 struct policy_work *op = mg->op;
1343 bool is_policy_promote = (op->op == POLICY_PROMOTE);
1345 if ((!is_policy_promote && !is_dirty(cache, op->cblock)) ||
1346 is_discarded_oblock(cache, op->oblock)) {
1347 mg_upgrade_lock(ws);
1348 return;
1351 init_continuation(&mg->k, mg_upgrade_lock);
1352 copy(mg, is_policy_promote);
1355 static void mg_copy(struct work_struct *ws)
1357 struct dm_cache_migration *mg = ws_to_mg(ws);
1359 if (mg->overwrite_bio) {
1361 * No exclusive lock was held when we last checked if the bio
1362 * was optimisable. So we have to check again in case things
1363 * have changed (eg, the block may no longer be discarded).
1365 if (!optimisable_bio(mg->cache, mg->overwrite_bio, mg->op->oblock)) {
1367 * Fallback to a real full copy after doing some tidying up.
1369 bool rb = bio_detain_shared(mg->cache, mg->op->oblock, mg->overwrite_bio);
1371 BUG_ON(rb); /* An exclusive lock must _not_ be held for this block */
1372 mg->overwrite_bio = NULL;
1373 inc_io_migrations(mg->cache);
1374 mg_full_copy(ws);
1375 return;
1379 * It's safe to do this here, even though it's new data
1380 * because all IO has been locked out of the block.
1382 * mg_lock_writes() already took READ_WRITE_LOCK_LEVEL
1383 * so _not_ using mg_upgrade_lock() as continutation.
1385 overwrite(mg, mg_update_metadata_after_copy);
1387 } else
1388 mg_full_copy(ws);
1391 static int mg_lock_writes(struct dm_cache_migration *mg)
1393 int r;
1394 struct dm_cell_key_v2 key;
1395 struct cache *cache = mg->cache;
1396 struct dm_bio_prison_cell_v2 *prealloc;
1398 prealloc = alloc_prison_cell(cache);
1401 * Prevent writes to the block, but allow reads to continue.
1402 * Unless we're using an overwrite bio, in which case we lock
1403 * everything.
1405 build_key(mg->op->oblock, oblock_succ(mg->op->oblock), &key);
1406 r = dm_cell_lock_v2(cache->prison, &key,
1407 mg->overwrite_bio ? READ_WRITE_LOCK_LEVEL : WRITE_LOCK_LEVEL,
1408 prealloc, &mg->cell);
1409 if (r < 0) {
1410 free_prison_cell(cache, prealloc);
1411 mg_complete(mg, false);
1412 return r;
1415 if (mg->cell != prealloc)
1416 free_prison_cell(cache, prealloc);
1418 if (r == 0)
1419 mg_copy(&mg->k.ws);
1420 else
1421 quiesce(mg, mg_copy);
1423 return 0;
1426 static int mg_start(struct cache *cache, struct policy_work *op, struct bio *bio)
1428 struct dm_cache_migration *mg;
1430 if (!background_work_begin(cache)) {
1431 policy_complete_background_work(cache->policy, op, false);
1432 return -EPERM;
1435 mg = alloc_migration(cache);
1437 mg->op = op;
1438 mg->overwrite_bio = bio;
1440 if (!bio)
1441 inc_io_migrations(cache);
1443 return mg_lock_writes(mg);
1447 *--------------------------------------------------------------
1448 * invalidation processing
1449 *--------------------------------------------------------------
1452 static void invalidate_complete(struct dm_cache_migration *mg, bool success)
1454 struct bio_list bios;
1455 struct cache *cache = mg->cache;
1457 bio_list_init(&bios);
1458 if (dm_cell_unlock_v2(cache->prison, mg->cell, &bios))
1459 free_prison_cell(cache, mg->cell);
1461 if (!success && mg->overwrite_bio)
1462 bio_io_error(mg->overwrite_bio);
1464 free_migration(mg);
1465 defer_bios(cache, &bios);
1467 background_work_end(cache);
1470 static void invalidate_completed(struct work_struct *ws)
1472 struct dm_cache_migration *mg = ws_to_mg(ws);
1474 invalidate_complete(mg, !mg->k.input);
1477 static int invalidate_cblock(struct cache *cache, dm_cblock_t cblock)
1479 int r;
1481 r = policy_invalidate_mapping(cache->policy, cblock);
1482 if (!r) {
1483 r = dm_cache_remove_mapping(cache->cmd, cblock);
1484 if (r) {
1485 DMERR_LIMIT("%s: invalidation failed; couldn't update on disk metadata",
1486 cache_device_name(cache));
1487 metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
1490 } else if (r == -ENODATA) {
1492 * Harmless, already unmapped.
1494 r = 0;
1496 } else
1497 DMERR("%s: policy_invalidate_mapping failed", cache_device_name(cache));
1499 return r;
1502 static void invalidate_remove(struct work_struct *ws)
1504 int r;
1505 struct dm_cache_migration *mg = ws_to_mg(ws);
1506 struct cache *cache = mg->cache;
1508 r = invalidate_cblock(cache, mg->invalidate_cblock);
1509 if (r) {
1510 invalidate_complete(mg, false);
1511 return;
1514 init_continuation(&mg->k, invalidate_completed);
1515 continue_after_commit(&cache->committer, &mg->k);
1516 remap_to_origin_clear_discard(cache, mg->overwrite_bio, mg->invalidate_oblock);
1517 mg->overwrite_bio = NULL;
1518 schedule_commit(&cache->committer);
1521 static int invalidate_lock(struct dm_cache_migration *mg)
1523 int r;
1524 struct dm_cell_key_v2 key;
1525 struct cache *cache = mg->cache;
1526 struct dm_bio_prison_cell_v2 *prealloc;
1528 prealloc = alloc_prison_cell(cache);
1530 build_key(mg->invalidate_oblock, oblock_succ(mg->invalidate_oblock), &key);
1531 r = dm_cell_lock_v2(cache->prison, &key,
1532 READ_WRITE_LOCK_LEVEL, prealloc, &mg->cell);
1533 if (r < 0) {
1534 free_prison_cell(cache, prealloc);
1535 invalidate_complete(mg, false);
1536 return r;
1539 if (mg->cell != prealloc)
1540 free_prison_cell(cache, prealloc);
1542 if (r)
1543 quiesce(mg, invalidate_remove);
1545 else {
1547 * We can't call invalidate_remove() directly here because we
1548 * might still be in request context.
1550 init_continuation(&mg->k, invalidate_remove);
1551 queue_work(cache->wq, &mg->k.ws);
1554 return 0;
1557 static int invalidate_start(struct cache *cache, dm_cblock_t cblock,
1558 dm_oblock_t oblock, struct bio *bio)
1560 struct dm_cache_migration *mg;
1562 if (!background_work_begin(cache))
1563 return -EPERM;
1565 mg = alloc_migration(cache);
1567 mg->overwrite_bio = bio;
1568 mg->invalidate_cblock = cblock;
1569 mg->invalidate_oblock = oblock;
1571 return invalidate_lock(mg);
1575 *--------------------------------------------------------------
1576 * bio processing
1577 *--------------------------------------------------------------
1580 enum busy {
1581 IDLE,
1582 BUSY
1585 static enum busy spare_migration_bandwidth(struct cache *cache)
1587 bool idle = dm_iot_idle_for(&cache->tracker, HZ);
1588 sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
1589 cache->sectors_per_block;
1591 if (idle && current_volume <= cache->migration_threshold)
1592 return IDLE;
1593 else
1594 return BUSY;
1597 static void inc_hit_counter(struct cache *cache, struct bio *bio)
1599 atomic_inc(bio_data_dir(bio) == READ ?
1600 &cache->stats.read_hit : &cache->stats.write_hit);
1603 static void inc_miss_counter(struct cache *cache, struct bio *bio)
1605 atomic_inc(bio_data_dir(bio) == READ ?
1606 &cache->stats.read_miss : &cache->stats.write_miss);
1609 /*----------------------------------------------------------------*/
1611 static int map_bio(struct cache *cache, struct bio *bio, dm_oblock_t block,
1612 bool *commit_needed)
1614 int r, data_dir;
1615 bool rb, background_queued;
1616 dm_cblock_t cblock;
1618 *commit_needed = false;
1620 rb = bio_detain_shared(cache, block, bio);
1621 if (!rb) {
1623 * An exclusive lock is held for this block, so we have to
1624 * wait. We set the commit_needed flag so the current
1625 * transaction will be committed asap, allowing this lock
1626 * to be dropped.
1628 *commit_needed = true;
1629 return DM_MAPIO_SUBMITTED;
1632 data_dir = bio_data_dir(bio);
1634 if (optimisable_bio(cache, bio, block)) {
1635 struct policy_work *op = NULL;
1637 r = policy_lookup_with_work(cache->policy, block, &cblock, data_dir, true, &op);
1638 if (unlikely(r && r != -ENOENT)) {
1639 DMERR_LIMIT("%s: policy_lookup_with_work() failed with r = %d",
1640 cache_device_name(cache), r);
1641 bio_io_error(bio);
1642 return DM_MAPIO_SUBMITTED;
1645 if (r == -ENOENT && op) {
1646 bio_drop_shared_lock(cache, bio);
1647 BUG_ON(op->op != POLICY_PROMOTE);
1648 mg_start(cache, op, bio);
1649 return DM_MAPIO_SUBMITTED;
1651 } else {
1652 r = policy_lookup(cache->policy, block, &cblock, data_dir, false, &background_queued);
1653 if (unlikely(r && r != -ENOENT)) {
1654 DMERR_LIMIT("%s: policy_lookup() failed with r = %d",
1655 cache_device_name(cache), r);
1656 bio_io_error(bio);
1657 return DM_MAPIO_SUBMITTED;
1660 if (background_queued)
1661 wake_migration_worker(cache);
1664 if (r == -ENOENT) {
1665 struct per_bio_data *pb = get_per_bio_data(bio);
1668 * Miss.
1670 inc_miss_counter(cache, bio);
1671 if (pb->req_nr == 0) {
1672 accounted_begin(cache, bio);
1673 remap_to_origin_clear_discard(cache, bio, block);
1674 } else {
1676 * This is a duplicate writethrough io that is no
1677 * longer needed because the block has been demoted.
1679 bio_endio(bio);
1680 return DM_MAPIO_SUBMITTED;
1682 } else {
1684 * Hit.
1686 inc_hit_counter(cache, bio);
1689 * Passthrough always maps to the origin, invalidating any
1690 * cache blocks that are written to.
1692 if (passthrough_mode(cache)) {
1693 if (bio_data_dir(bio) == WRITE) {
1694 bio_drop_shared_lock(cache, bio);
1695 atomic_inc(&cache->stats.demotion);
1696 invalidate_start(cache, cblock, block, bio);
1697 } else
1698 remap_to_origin_clear_discard(cache, bio, block);
1699 } else {
1700 if (bio_data_dir(bio) == WRITE && writethrough_mode(cache) &&
1701 !is_dirty(cache, cblock)) {
1702 remap_to_origin_and_cache(cache, bio, block, cblock);
1703 accounted_begin(cache, bio);
1704 } else
1705 remap_to_cache_dirty(cache, bio, block, cblock);
1710 * dm core turns FUA requests into a separate payload and FLUSH req.
1712 if (bio->bi_opf & REQ_FUA) {
1714 * issue_after_commit will call accounted_begin a second time. So
1715 * we call accounted_complete() to avoid double accounting.
1717 accounted_complete(cache, bio);
1718 issue_after_commit(&cache->committer, bio);
1719 *commit_needed = true;
1720 return DM_MAPIO_SUBMITTED;
1723 return DM_MAPIO_REMAPPED;
1726 static bool process_bio(struct cache *cache, struct bio *bio)
1728 bool commit_needed;
1730 if (map_bio(cache, bio, get_bio_block(cache, bio), &commit_needed) == DM_MAPIO_REMAPPED)
1731 dm_submit_bio_remap(bio, NULL);
1733 return commit_needed;
1737 * A non-zero return indicates read_only or fail_io mode.
1739 static int commit(struct cache *cache, bool clean_shutdown)
1741 int r;
1743 if (get_cache_mode(cache) >= CM_READ_ONLY)
1744 return -EINVAL;
1746 atomic_inc(&cache->stats.commit_count);
1747 r = dm_cache_commit(cache->cmd, clean_shutdown);
1748 if (r)
1749 metadata_operation_failed(cache, "dm_cache_commit", r);
1751 return r;
1755 * Used by the batcher.
1757 static blk_status_t commit_op(void *context)
1759 struct cache *cache = context;
1761 if (dm_cache_changed_this_transaction(cache->cmd))
1762 return errno_to_blk_status(commit(cache, false));
1764 return 0;
1767 /*----------------------------------------------------------------*/
1769 static bool process_flush_bio(struct cache *cache, struct bio *bio)
1771 struct per_bio_data *pb = get_per_bio_data(bio);
1773 if (!pb->req_nr)
1774 remap_to_origin(cache, bio);
1775 else
1776 remap_to_cache(cache, bio, 0);
1778 issue_after_commit(&cache->committer, bio);
1779 return true;
1782 static bool process_discard_bio(struct cache *cache, struct bio *bio)
1784 dm_dblock_t b, e;
1787 * FIXME: do we need to lock the region? Or can we just assume the
1788 * user wont be so foolish as to issue discard concurrently with
1789 * other IO?
1791 calc_discard_block_range(cache, bio, &b, &e);
1792 while (b != e) {
1793 set_discard(cache, b);
1794 b = to_dblock(from_dblock(b) + 1);
1797 if (cache->features.discard_passdown) {
1798 remap_to_origin(cache, bio);
1799 dm_submit_bio_remap(bio, NULL);
1800 } else
1801 bio_endio(bio);
1803 return false;
1806 static void process_deferred_bios(struct work_struct *ws)
1808 struct cache *cache = container_of(ws, struct cache, deferred_bio_worker);
1810 bool commit_needed = false;
1811 struct bio_list bios;
1812 struct bio *bio;
1814 bio_list_init(&bios);
1816 spin_lock_irq(&cache->lock);
1817 bio_list_merge_init(&bios, &cache->deferred_bios);
1818 spin_unlock_irq(&cache->lock);
1820 while ((bio = bio_list_pop(&bios))) {
1821 if (bio->bi_opf & REQ_PREFLUSH)
1822 commit_needed = process_flush_bio(cache, bio) || commit_needed;
1824 else if (bio_op(bio) == REQ_OP_DISCARD)
1825 commit_needed = process_discard_bio(cache, bio) || commit_needed;
1827 else
1828 commit_needed = process_bio(cache, bio) || commit_needed;
1829 cond_resched();
1832 if (commit_needed)
1833 schedule_commit(&cache->committer);
1837 *--------------------------------------------------------------
1838 * Main worker loop
1839 *--------------------------------------------------------------
1841 static void requeue_deferred_bios(struct cache *cache)
1843 struct bio *bio;
1844 struct bio_list bios;
1846 bio_list_init(&bios);
1847 bio_list_merge_init(&bios, &cache->deferred_bios);
1849 while ((bio = bio_list_pop(&bios))) {
1850 bio->bi_status = BLK_STS_DM_REQUEUE;
1851 bio_endio(bio);
1852 cond_resched();
1857 * We want to commit periodically so that not too much
1858 * unwritten metadata builds up.
1860 static void do_waker(struct work_struct *ws)
1862 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
1864 policy_tick(cache->policy, true);
1865 wake_migration_worker(cache);
1866 schedule_commit(&cache->committer);
1867 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1870 static void check_migrations(struct work_struct *ws)
1872 int r;
1873 struct policy_work *op;
1874 struct cache *cache = container_of(ws, struct cache, migration_worker);
1875 enum busy b;
1877 for (;;) {
1878 b = spare_migration_bandwidth(cache);
1880 r = policy_get_background_work(cache->policy, b == IDLE, &op);
1881 if (r == -ENODATA)
1882 break;
1884 if (r) {
1885 DMERR_LIMIT("%s: policy_background_work failed",
1886 cache_device_name(cache));
1887 break;
1890 r = mg_start(cache, op, NULL);
1891 if (r)
1892 break;
1894 cond_resched();
1899 *--------------------------------------------------------------
1900 * Target methods
1901 *--------------------------------------------------------------
1905 * This function gets called on the error paths of the constructor, so we
1906 * have to cope with a partially initialised struct.
1908 static void destroy(struct cache *cache)
1910 unsigned int i;
1912 mempool_exit(&cache->migration_pool);
1914 if (cache->prison)
1915 dm_bio_prison_destroy_v2(cache->prison);
1917 cancel_delayed_work_sync(&cache->waker);
1918 if (cache->wq)
1919 destroy_workqueue(cache->wq);
1921 if (cache->dirty_bitset)
1922 free_bitset(cache->dirty_bitset);
1924 if (cache->discard_bitset)
1925 free_bitset(cache->discard_bitset);
1927 if (cache->copier)
1928 dm_kcopyd_client_destroy(cache->copier);
1930 if (cache->cmd)
1931 dm_cache_metadata_close(cache->cmd);
1933 if (cache->metadata_dev)
1934 dm_put_device(cache->ti, cache->metadata_dev);
1936 if (cache->origin_dev)
1937 dm_put_device(cache->ti, cache->origin_dev);
1939 if (cache->cache_dev)
1940 dm_put_device(cache->ti, cache->cache_dev);
1942 if (cache->policy)
1943 dm_cache_policy_destroy(cache->policy);
1945 for (i = 0; i < cache->nr_ctr_args ; i++)
1946 kfree(cache->ctr_args[i]);
1947 kfree(cache->ctr_args);
1949 bioset_exit(&cache->bs);
1951 kfree(cache);
1954 static void cache_dtr(struct dm_target *ti)
1956 struct cache *cache = ti->private;
1958 destroy(cache);
1961 static sector_t get_dev_size(struct dm_dev *dev)
1963 return bdev_nr_sectors(dev->bdev);
1966 /*----------------------------------------------------------------*/
1969 * Construct a cache device mapping.
1971 * cache <metadata dev> <cache dev> <origin dev> <block size>
1972 * <#feature args> [<feature arg>]*
1973 * <policy> <#policy args> [<policy arg>]*
1975 * metadata dev : fast device holding the persistent metadata
1976 * cache dev : fast device holding cached data blocks
1977 * origin dev : slow device holding original data blocks
1978 * block size : cache unit size in sectors
1980 * #feature args : number of feature arguments passed
1981 * feature args : writethrough. (The default is writeback.)
1983 * policy : the replacement policy to use
1984 * #policy args : an even number of policy arguments corresponding
1985 * to key/value pairs passed to the policy
1986 * policy args : key/value pairs passed to the policy
1987 * E.g. 'sequential_threshold 1024'
1988 * See cache-policies.txt for details.
1990 * Optional feature arguments are:
1991 * writethrough : write through caching that prohibits cache block
1992 * content from being different from origin block content.
1993 * Without this argument, the default behaviour is to write
1994 * back cache block contents later for performance reasons,
1995 * so they may differ from the corresponding origin blocks.
1997 struct cache_args {
1998 struct dm_target *ti;
2000 struct dm_dev *metadata_dev;
2002 struct dm_dev *cache_dev;
2003 sector_t cache_sectors;
2005 struct dm_dev *origin_dev;
2006 sector_t origin_sectors;
2008 uint32_t block_size;
2010 const char *policy_name;
2011 int policy_argc;
2012 const char **policy_argv;
2014 struct cache_features features;
2017 static void destroy_cache_args(struct cache_args *ca)
2019 if (ca->metadata_dev)
2020 dm_put_device(ca->ti, ca->metadata_dev);
2022 if (ca->cache_dev)
2023 dm_put_device(ca->ti, ca->cache_dev);
2025 if (ca->origin_dev)
2026 dm_put_device(ca->ti, ca->origin_dev);
2028 kfree(ca);
2031 static bool at_least_one_arg(struct dm_arg_set *as, char **error)
2033 if (!as->argc) {
2034 *error = "Insufficient args";
2035 return false;
2038 return true;
2041 static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
2042 char **error)
2044 int r;
2045 sector_t metadata_dev_size;
2047 if (!at_least_one_arg(as, error))
2048 return -EINVAL;
2050 r = dm_get_device(ca->ti, dm_shift_arg(as),
2051 BLK_OPEN_READ | BLK_OPEN_WRITE, &ca->metadata_dev);
2052 if (r) {
2053 *error = "Error opening metadata device";
2054 return r;
2057 metadata_dev_size = get_dev_size(ca->metadata_dev);
2058 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
2059 DMWARN("Metadata device %pg is larger than %u sectors: excess space will not be used.",
2060 ca->metadata_dev->bdev, THIN_METADATA_MAX_SECTORS);
2062 return 0;
2065 static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
2066 char **error)
2068 int r;
2070 if (!at_least_one_arg(as, error))
2071 return -EINVAL;
2073 r = dm_get_device(ca->ti, dm_shift_arg(as),
2074 BLK_OPEN_READ | BLK_OPEN_WRITE, &ca->cache_dev);
2075 if (r) {
2076 *error = "Error opening cache device";
2077 return r;
2079 ca->cache_sectors = get_dev_size(ca->cache_dev);
2081 return 0;
2084 static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
2085 char **error)
2087 int r;
2089 if (!at_least_one_arg(as, error))
2090 return -EINVAL;
2092 r = dm_get_device(ca->ti, dm_shift_arg(as),
2093 BLK_OPEN_READ | BLK_OPEN_WRITE, &ca->origin_dev);
2094 if (r) {
2095 *error = "Error opening origin device";
2096 return r;
2099 ca->origin_sectors = get_dev_size(ca->origin_dev);
2100 if (ca->ti->len > ca->origin_sectors) {
2101 *error = "Device size larger than cached device";
2102 return -EINVAL;
2105 return 0;
2108 static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
2109 char **error)
2111 unsigned long block_size;
2113 if (!at_least_one_arg(as, error))
2114 return -EINVAL;
2116 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
2117 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2118 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2119 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
2120 *error = "Invalid data block size";
2121 return -EINVAL;
2124 if (block_size > ca->cache_sectors) {
2125 *error = "Data block size is larger than the cache device";
2126 return -EINVAL;
2129 ca->block_size = block_size;
2131 return 0;
2134 static void init_features(struct cache_features *cf)
2136 cf->mode = CM_WRITE;
2137 cf->io_mode = CM_IO_WRITEBACK;
2138 cf->metadata_version = 1;
2139 cf->discard_passdown = true;
2142 static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2143 char **error)
2145 static const struct dm_arg _args[] = {
2146 {0, 3, "Invalid number of cache feature arguments"},
2149 int r, mode_ctr = 0;
2150 unsigned int argc;
2151 const char *arg;
2152 struct cache_features *cf = &ca->features;
2154 init_features(cf);
2156 r = dm_read_arg_group(_args, as, &argc, error);
2157 if (r)
2158 return -EINVAL;
2160 while (argc--) {
2161 arg = dm_shift_arg(as);
2163 if (!strcasecmp(arg, "writeback")) {
2164 cf->io_mode = CM_IO_WRITEBACK;
2165 mode_ctr++;
2168 else if (!strcasecmp(arg, "writethrough")) {
2169 cf->io_mode = CM_IO_WRITETHROUGH;
2170 mode_ctr++;
2173 else if (!strcasecmp(arg, "passthrough")) {
2174 cf->io_mode = CM_IO_PASSTHROUGH;
2175 mode_ctr++;
2178 else if (!strcasecmp(arg, "metadata2"))
2179 cf->metadata_version = 2;
2181 else if (!strcasecmp(arg, "no_discard_passdown"))
2182 cf->discard_passdown = false;
2184 else {
2185 *error = "Unrecognised cache feature requested";
2186 return -EINVAL;
2190 if (mode_ctr > 1) {
2191 *error = "Duplicate cache io_mode features requested";
2192 return -EINVAL;
2195 return 0;
2198 static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2199 char **error)
2201 static const struct dm_arg _args[] = {
2202 {0, 1024, "Invalid number of policy arguments"},
2205 int r;
2207 if (!at_least_one_arg(as, error))
2208 return -EINVAL;
2210 ca->policy_name = dm_shift_arg(as);
2212 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2213 if (r)
2214 return -EINVAL;
2216 ca->policy_argv = (const char **)as->argv;
2217 dm_consume_args(as, ca->policy_argc);
2219 return 0;
2222 static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2223 char **error)
2225 int r;
2226 struct dm_arg_set as;
2228 as.argc = argc;
2229 as.argv = argv;
2231 r = parse_metadata_dev(ca, &as, error);
2232 if (r)
2233 return r;
2235 r = parse_cache_dev(ca, &as, error);
2236 if (r)
2237 return r;
2239 r = parse_origin_dev(ca, &as, error);
2240 if (r)
2241 return r;
2243 r = parse_block_size(ca, &as, error);
2244 if (r)
2245 return r;
2247 r = parse_features(ca, &as, error);
2248 if (r)
2249 return r;
2251 r = parse_policy(ca, &as, error);
2252 if (r)
2253 return r;
2255 return 0;
2258 /*----------------------------------------------------------------*/
2260 static struct kmem_cache *migration_cache;
2262 #define NOT_CORE_OPTION 1
2264 static int process_config_option(struct cache *cache, const char *key, const char *value)
2266 unsigned long tmp;
2268 if (!strcasecmp(key, "migration_threshold")) {
2269 if (kstrtoul(value, 10, &tmp))
2270 return -EINVAL;
2272 cache->migration_threshold = tmp;
2273 return 0;
2276 return NOT_CORE_OPTION;
2279 static int set_config_value(struct cache *cache, const char *key, const char *value)
2281 int r = process_config_option(cache, key, value);
2283 if (r == NOT_CORE_OPTION)
2284 r = policy_set_config_value(cache->policy, key, value);
2286 if (r)
2287 DMWARN("bad config value for %s: %s", key, value);
2289 return r;
2292 static int set_config_values(struct cache *cache, int argc, const char **argv)
2294 int r = 0;
2296 if (argc & 1) {
2297 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2298 return -EINVAL;
2301 while (argc) {
2302 r = set_config_value(cache, argv[0], argv[1]);
2303 if (r)
2304 break;
2306 argc -= 2;
2307 argv += 2;
2310 return r;
2313 static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2314 char **error)
2316 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2317 cache->cache_size,
2318 cache->origin_sectors,
2319 cache->sectors_per_block);
2320 if (IS_ERR(p)) {
2321 *error = "Error creating cache's policy";
2322 return PTR_ERR(p);
2324 cache->policy = p;
2325 BUG_ON(!cache->policy);
2327 return 0;
2331 * We want the discard block size to be at least the size of the cache
2332 * block size and have no more than 2^14 discard blocks across the origin.
2334 #define MAX_DISCARD_BLOCKS (1 << 14)
2336 static bool too_many_discard_blocks(sector_t discard_block_size,
2337 sector_t origin_size)
2339 (void) sector_div(origin_size, discard_block_size);
2341 return origin_size > MAX_DISCARD_BLOCKS;
2344 static sector_t calculate_discard_block_size(sector_t cache_block_size,
2345 sector_t origin_size)
2347 sector_t discard_block_size = cache_block_size;
2349 if (origin_size)
2350 while (too_many_discard_blocks(discard_block_size, origin_size))
2351 discard_block_size *= 2;
2353 return discard_block_size;
2356 static void set_cache_size(struct cache *cache, dm_cblock_t size)
2358 dm_block_t nr_blocks = from_cblock(size);
2360 if (nr_blocks > (1 << 20) && cache->cache_size != size)
2361 DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
2362 "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
2363 "Please consider increasing the cache block size to reduce the overall cache block count.",
2364 (unsigned long long) nr_blocks);
2366 cache->cache_size = size;
2369 #define DEFAULT_MIGRATION_THRESHOLD 2048
2371 static int cache_create(struct cache_args *ca, struct cache **result)
2373 int r = 0;
2374 char **error = &ca->ti->error;
2375 struct cache *cache;
2376 struct dm_target *ti = ca->ti;
2377 dm_block_t origin_blocks;
2378 struct dm_cache_metadata *cmd;
2379 bool may_format = ca->features.mode == CM_WRITE;
2381 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2382 if (!cache)
2383 return -ENOMEM;
2385 cache->ti = ca->ti;
2386 ti->private = cache;
2387 ti->accounts_remapped_io = true;
2388 ti->num_flush_bios = 2;
2389 ti->flush_supported = true;
2391 ti->num_discard_bios = 1;
2392 ti->discards_supported = true;
2394 ti->per_io_data_size = sizeof(struct per_bio_data);
2396 cache->features = ca->features;
2397 if (writethrough_mode(cache)) {
2398 /* Create bioset for writethrough bios issued to origin */
2399 r = bioset_init(&cache->bs, BIO_POOL_SIZE, 0, 0);
2400 if (r)
2401 goto bad;
2404 cache->metadata_dev = ca->metadata_dev;
2405 cache->origin_dev = ca->origin_dev;
2406 cache->cache_dev = ca->cache_dev;
2408 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2410 origin_blocks = cache->origin_sectors = ca->origin_sectors;
2411 origin_blocks = block_div(origin_blocks, ca->block_size);
2412 cache->origin_blocks = to_oblock(origin_blocks);
2414 cache->sectors_per_block = ca->block_size;
2415 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2416 r = -EINVAL;
2417 goto bad;
2420 if (ca->block_size & (ca->block_size - 1)) {
2421 dm_block_t cache_size = ca->cache_sectors;
2423 cache->sectors_per_block_shift = -1;
2424 cache_size = block_div(cache_size, ca->block_size);
2425 set_cache_size(cache, to_cblock(cache_size));
2426 } else {
2427 cache->sectors_per_block_shift = __ffs(ca->block_size);
2428 set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
2431 r = create_cache_policy(cache, ca, error);
2432 if (r)
2433 goto bad;
2435 cache->policy_nr_args = ca->policy_argc;
2436 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2438 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2439 if (r) {
2440 *error = "Error setting cache policy's config values";
2441 goto bad;
2444 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2445 ca->block_size, may_format,
2446 dm_cache_policy_get_hint_size(cache->policy),
2447 ca->features.metadata_version);
2448 if (IS_ERR(cmd)) {
2449 *error = "Error creating metadata object";
2450 r = PTR_ERR(cmd);
2451 goto bad;
2453 cache->cmd = cmd;
2454 set_cache_mode(cache, CM_WRITE);
2455 if (get_cache_mode(cache) != CM_WRITE) {
2456 *error = "Unable to get write access to metadata, please check/repair metadata.";
2457 r = -EINVAL;
2458 goto bad;
2461 if (passthrough_mode(cache)) {
2462 bool all_clean;
2464 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2465 if (r) {
2466 *error = "dm_cache_metadata_all_clean() failed";
2467 goto bad;
2470 if (!all_clean) {
2471 *error = "Cannot enter passthrough mode unless all blocks are clean";
2472 r = -EINVAL;
2473 goto bad;
2476 policy_allow_migrations(cache->policy, false);
2479 spin_lock_init(&cache->lock);
2480 bio_list_init(&cache->deferred_bios);
2481 atomic_set(&cache->nr_allocated_migrations, 0);
2482 atomic_set(&cache->nr_io_migrations, 0);
2483 init_waitqueue_head(&cache->migration_wait);
2485 r = -ENOMEM;
2486 atomic_set(&cache->nr_dirty, 0);
2487 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2488 if (!cache->dirty_bitset) {
2489 *error = "could not allocate dirty bitset";
2490 goto bad;
2492 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2494 cache->discard_block_size =
2495 calculate_discard_block_size(cache->sectors_per_block,
2496 cache->origin_sectors);
2497 cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
2498 cache->discard_block_size));
2499 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2500 if (!cache->discard_bitset) {
2501 *error = "could not allocate discard bitset";
2502 goto bad;
2504 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2506 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2507 if (IS_ERR(cache->copier)) {
2508 *error = "could not create kcopyd client";
2509 r = PTR_ERR(cache->copier);
2510 goto bad;
2513 cache->wq = alloc_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM, 0);
2514 if (!cache->wq) {
2515 *error = "could not create workqueue for metadata object";
2516 goto bad;
2518 INIT_WORK(&cache->deferred_bio_worker, process_deferred_bios);
2519 INIT_WORK(&cache->migration_worker, check_migrations);
2520 INIT_DELAYED_WORK(&cache->waker, do_waker);
2522 cache->prison = dm_bio_prison_create_v2(cache->wq);
2523 if (!cache->prison) {
2524 *error = "could not create bio prison";
2525 goto bad;
2528 r = mempool_init_slab_pool(&cache->migration_pool, MIGRATION_POOL_SIZE,
2529 migration_cache);
2530 if (r) {
2531 *error = "Error creating cache's migration mempool";
2532 goto bad;
2535 cache->need_tick_bio = true;
2536 cache->sized = false;
2537 cache->invalidate = false;
2538 cache->commit_requested = false;
2539 cache->loaded_mappings = false;
2540 cache->loaded_discards = false;
2542 load_stats(cache);
2544 atomic_set(&cache->stats.demotion, 0);
2545 atomic_set(&cache->stats.promotion, 0);
2546 atomic_set(&cache->stats.copies_avoided, 0);
2547 atomic_set(&cache->stats.cache_cell_clash, 0);
2548 atomic_set(&cache->stats.commit_count, 0);
2549 atomic_set(&cache->stats.discard_count, 0);
2551 spin_lock_init(&cache->invalidation_lock);
2552 INIT_LIST_HEAD(&cache->invalidation_requests);
2554 batcher_init(&cache->committer, commit_op, cache,
2555 issue_op, cache, cache->wq);
2556 dm_iot_init(&cache->tracker);
2558 init_rwsem(&cache->background_work_lock);
2559 prevent_background_work(cache);
2561 *result = cache;
2562 return 0;
2563 bad:
2564 destroy(cache);
2565 return r;
2568 static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2570 unsigned int i;
2571 const char **copy;
2573 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2574 if (!copy)
2575 return -ENOMEM;
2576 for (i = 0; i < argc; i++) {
2577 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2578 if (!copy[i]) {
2579 while (i--)
2580 kfree(copy[i]);
2581 kfree(copy);
2582 return -ENOMEM;
2586 cache->nr_ctr_args = argc;
2587 cache->ctr_args = copy;
2589 return 0;
2592 static int cache_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2594 int r = -EINVAL;
2595 struct cache_args *ca;
2596 struct cache *cache = NULL;
2598 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2599 if (!ca) {
2600 ti->error = "Error allocating memory for cache";
2601 return -ENOMEM;
2603 ca->ti = ti;
2605 r = parse_cache_args(ca, argc, argv, &ti->error);
2606 if (r)
2607 goto out;
2609 r = cache_create(ca, &cache);
2610 if (r)
2611 goto out;
2613 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2614 if (r) {
2615 destroy(cache);
2616 goto out;
2619 ti->private = cache;
2620 out:
2621 destroy_cache_args(ca);
2622 return r;
2625 /*----------------------------------------------------------------*/
2627 static int cache_map(struct dm_target *ti, struct bio *bio)
2629 struct cache *cache = ti->private;
2631 int r;
2632 bool commit_needed;
2633 dm_oblock_t block = get_bio_block(cache, bio);
2635 init_per_bio_data(bio);
2636 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
2638 * This can only occur if the io goes to a partial block at
2639 * the end of the origin device. We don't cache these.
2640 * Just remap to the origin and carry on.
2642 remap_to_origin(cache, bio);
2643 accounted_begin(cache, bio);
2644 return DM_MAPIO_REMAPPED;
2647 if (discard_or_flush(bio)) {
2648 defer_bio(cache, bio);
2649 return DM_MAPIO_SUBMITTED;
2652 r = map_bio(cache, bio, block, &commit_needed);
2653 if (commit_needed)
2654 schedule_commit(&cache->committer);
2656 return r;
2659 static int cache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *error)
2661 struct cache *cache = ti->private;
2662 unsigned long flags;
2663 struct per_bio_data *pb = get_per_bio_data(bio);
2665 if (pb->tick) {
2666 policy_tick(cache->policy, false);
2668 spin_lock_irqsave(&cache->lock, flags);
2669 cache->need_tick_bio = true;
2670 spin_unlock_irqrestore(&cache->lock, flags);
2673 bio_drop_shared_lock(cache, bio);
2674 accounted_complete(cache, bio);
2676 return DM_ENDIO_DONE;
2679 static int write_dirty_bitset(struct cache *cache)
2681 int r;
2683 if (get_cache_mode(cache) >= CM_READ_ONLY)
2684 return -EINVAL;
2686 r = dm_cache_set_dirty_bits(cache->cmd, from_cblock(cache->cache_size), cache->dirty_bitset);
2687 if (r)
2688 metadata_operation_failed(cache, "dm_cache_set_dirty_bits", r);
2690 return r;
2693 static int write_discard_bitset(struct cache *cache)
2695 unsigned int i, r;
2697 if (get_cache_mode(cache) >= CM_READ_ONLY)
2698 return -EINVAL;
2700 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2701 cache->discard_nr_blocks);
2702 if (r) {
2703 DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
2704 metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
2705 return r;
2708 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2709 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2710 is_discarded(cache, to_dblock(i)));
2711 if (r) {
2712 metadata_operation_failed(cache, "dm_cache_set_discard", r);
2713 return r;
2717 return 0;
2720 static int write_hints(struct cache *cache)
2722 int r;
2724 if (get_cache_mode(cache) >= CM_READ_ONLY)
2725 return -EINVAL;
2727 r = dm_cache_write_hints(cache->cmd, cache->policy);
2728 if (r) {
2729 metadata_operation_failed(cache, "dm_cache_write_hints", r);
2730 return r;
2733 return 0;
2737 * returns true on success
2739 static bool sync_metadata(struct cache *cache)
2741 int r1, r2, r3, r4;
2743 r1 = write_dirty_bitset(cache);
2744 if (r1)
2745 DMERR("%s: could not write dirty bitset", cache_device_name(cache));
2747 r2 = write_discard_bitset(cache);
2748 if (r2)
2749 DMERR("%s: could not write discard bitset", cache_device_name(cache));
2751 save_stats(cache);
2753 r3 = write_hints(cache);
2754 if (r3)
2755 DMERR("%s: could not write hints", cache_device_name(cache));
2758 * If writing the above metadata failed, we still commit, but don't
2759 * set the clean shutdown flag. This will effectively force every
2760 * dirty bit to be set on reload.
2762 r4 = commit(cache, !r1 && !r2 && !r3);
2763 if (r4)
2764 DMERR("%s: could not write cache metadata", cache_device_name(cache));
2766 return !r1 && !r2 && !r3 && !r4;
2769 static void cache_postsuspend(struct dm_target *ti)
2771 struct cache *cache = ti->private;
2773 prevent_background_work(cache);
2774 BUG_ON(atomic_read(&cache->nr_io_migrations));
2776 cancel_delayed_work_sync(&cache->waker);
2777 drain_workqueue(cache->wq);
2778 WARN_ON(cache->tracker.in_flight);
2781 * If it's a flush suspend there won't be any deferred bios, so this
2782 * call is harmless.
2784 requeue_deferred_bios(cache);
2786 if (get_cache_mode(cache) == CM_WRITE)
2787 (void) sync_metadata(cache);
2790 static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2791 bool dirty, uint32_t hint, bool hint_valid)
2793 struct cache *cache = context;
2795 if (dirty) {
2796 set_bit(from_cblock(cblock), cache->dirty_bitset);
2797 atomic_inc(&cache->nr_dirty);
2798 } else
2799 clear_bit(from_cblock(cblock), cache->dirty_bitset);
2801 return policy_load_mapping(cache->policy, oblock, cblock, dirty, hint, hint_valid);
2805 * The discard block size in the on disk metadata is not
2806 * necessarily the same as we're currently using. So we have to
2807 * be careful to only set the discarded attribute if we know it
2808 * covers a complete block of the new size.
2810 struct discard_load_info {
2811 struct cache *cache;
2814 * These blocks are sized using the on disk dblock size, rather
2815 * than the current one.
2817 dm_block_t block_size;
2818 dm_block_t discard_begin, discard_end;
2821 static void discard_load_info_init(struct cache *cache,
2822 struct discard_load_info *li)
2824 li->cache = cache;
2825 li->discard_begin = li->discard_end = 0;
2828 static void set_discard_range(struct discard_load_info *li)
2830 sector_t b, e;
2832 if (li->discard_begin == li->discard_end)
2833 return;
2836 * Convert to sectors.
2838 b = li->discard_begin * li->block_size;
2839 e = li->discard_end * li->block_size;
2842 * Then convert back to the current dblock size.
2844 b = dm_sector_div_up(b, li->cache->discard_block_size);
2845 sector_div(e, li->cache->discard_block_size);
2848 * The origin may have shrunk, so we need to check we're still in
2849 * bounds.
2851 if (e > from_dblock(li->cache->discard_nr_blocks))
2852 e = from_dblock(li->cache->discard_nr_blocks);
2854 for (; b < e; b++)
2855 set_discard(li->cache, to_dblock(b));
2858 static int load_discard(void *context, sector_t discard_block_size,
2859 dm_dblock_t dblock, bool discard)
2861 struct discard_load_info *li = context;
2863 li->block_size = discard_block_size;
2865 if (discard) {
2866 if (from_dblock(dblock) == li->discard_end)
2868 * We're already in a discard range, just extend it.
2870 li->discard_end = li->discard_end + 1ULL;
2872 else {
2874 * Emit the old range and start a new one.
2876 set_discard_range(li);
2877 li->discard_begin = from_dblock(dblock);
2878 li->discard_end = li->discard_begin + 1ULL;
2880 } else {
2881 set_discard_range(li);
2882 li->discard_begin = li->discard_end = 0;
2885 return 0;
2888 static dm_cblock_t get_cache_dev_size(struct cache *cache)
2890 sector_t size = get_dev_size(cache->cache_dev);
2891 (void) sector_div(size, cache->sectors_per_block);
2892 return to_cblock(size);
2895 static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2897 if (from_cblock(new_size) > from_cblock(cache->cache_size)) {
2898 if (cache->sized) {
2899 DMERR("%s: unable to extend cache due to missing cache table reload",
2900 cache_device_name(cache));
2901 return false;
2906 * We can't drop a dirty block when shrinking the cache.
2908 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2909 new_size = to_cblock(from_cblock(new_size) + 1);
2910 if (is_dirty(cache, new_size)) {
2911 DMERR("%s: unable to shrink cache; cache block %llu is dirty",
2912 cache_device_name(cache),
2913 (unsigned long long) from_cblock(new_size));
2914 return false;
2918 return true;
2921 static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2923 int r;
2925 r = dm_cache_resize(cache->cmd, new_size);
2926 if (r) {
2927 DMERR("%s: could not resize cache metadata", cache_device_name(cache));
2928 metadata_operation_failed(cache, "dm_cache_resize", r);
2929 return r;
2932 set_cache_size(cache, new_size);
2934 return 0;
2937 static int cache_preresume(struct dm_target *ti)
2939 int r = 0;
2940 struct cache *cache = ti->private;
2941 dm_cblock_t csize = get_cache_dev_size(cache);
2944 * Check to see if the cache has resized.
2946 if (!cache->sized) {
2947 r = resize_cache_dev(cache, csize);
2948 if (r)
2949 return r;
2951 cache->sized = true;
2953 } else if (csize != cache->cache_size) {
2954 if (!can_resize(cache, csize))
2955 return -EINVAL;
2957 r = resize_cache_dev(cache, csize);
2958 if (r)
2959 return r;
2962 if (!cache->loaded_mappings) {
2963 r = dm_cache_load_mappings(cache->cmd, cache->policy,
2964 load_mapping, cache);
2965 if (r) {
2966 DMERR("%s: could not load cache mappings", cache_device_name(cache));
2967 metadata_operation_failed(cache, "dm_cache_load_mappings", r);
2968 return r;
2971 cache->loaded_mappings = true;
2974 if (!cache->loaded_discards) {
2975 struct discard_load_info li;
2978 * The discard bitset could have been resized, or the
2979 * discard block size changed. To be safe we start by
2980 * setting every dblock to not discarded.
2982 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2984 discard_load_info_init(cache, &li);
2985 r = dm_cache_load_discards(cache->cmd, load_discard, &li);
2986 if (r) {
2987 DMERR("%s: could not load origin discards", cache_device_name(cache));
2988 metadata_operation_failed(cache, "dm_cache_load_discards", r);
2989 return r;
2991 set_discard_range(&li);
2993 cache->loaded_discards = true;
2996 return r;
2999 static void cache_resume(struct dm_target *ti)
3001 struct cache *cache = ti->private;
3003 cache->need_tick_bio = true;
3004 allow_background_work(cache);
3005 do_waker(&cache->waker.work);
3008 static void emit_flags(struct cache *cache, char *result,
3009 unsigned int maxlen, ssize_t *sz_ptr)
3011 ssize_t sz = *sz_ptr;
3012 struct cache_features *cf = &cache->features;
3013 unsigned int count = (cf->metadata_version == 2) + !cf->discard_passdown + 1;
3015 DMEMIT("%u ", count);
3017 if (cf->metadata_version == 2)
3018 DMEMIT("metadata2 ");
3020 if (writethrough_mode(cache))
3021 DMEMIT("writethrough ");
3023 else if (passthrough_mode(cache))
3024 DMEMIT("passthrough ");
3026 else if (writeback_mode(cache))
3027 DMEMIT("writeback ");
3029 else {
3030 DMEMIT("unknown ");
3031 DMERR("%s: internal error: unknown io mode: %d",
3032 cache_device_name(cache), (int) cf->io_mode);
3035 if (!cf->discard_passdown)
3036 DMEMIT("no_discard_passdown ");
3038 *sz_ptr = sz;
3042 * Status format:
3044 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
3045 * <cache block size> <#used cache blocks>/<#total cache blocks>
3046 * <#read hits> <#read misses> <#write hits> <#write misses>
3047 * <#demotions> <#promotions> <#dirty>
3048 * <#features> <features>*
3049 * <#core args> <core args>
3050 * <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
3052 static void cache_status(struct dm_target *ti, status_type_t type,
3053 unsigned int status_flags, char *result, unsigned int maxlen)
3055 int r = 0;
3056 unsigned int i;
3057 ssize_t sz = 0;
3058 dm_block_t nr_free_blocks_metadata = 0;
3059 dm_block_t nr_blocks_metadata = 0;
3060 char buf[BDEVNAME_SIZE];
3061 struct cache *cache = ti->private;
3062 dm_cblock_t residency;
3063 bool needs_check;
3065 switch (type) {
3066 case STATUSTYPE_INFO:
3067 if (get_cache_mode(cache) == CM_FAIL) {
3068 DMEMIT("Fail");
3069 break;
3072 /* Commit to ensure statistics aren't out-of-date */
3073 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
3074 (void) commit(cache, false);
3076 r = dm_cache_get_free_metadata_block_count(cache->cmd, &nr_free_blocks_metadata);
3077 if (r) {
3078 DMERR("%s: dm_cache_get_free_metadata_block_count returned %d",
3079 cache_device_name(cache), r);
3080 goto err;
3083 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
3084 if (r) {
3085 DMERR("%s: dm_cache_get_metadata_dev_size returned %d",
3086 cache_device_name(cache), r);
3087 goto err;
3090 residency = policy_residency(cache->policy);
3092 DMEMIT("%u %llu/%llu %llu %llu/%llu %u %u %u %u %u %u %lu ",
3093 (unsigned int)DM_CACHE_METADATA_BLOCK_SIZE,
3094 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3095 (unsigned long long)nr_blocks_metadata,
3096 (unsigned long long)cache->sectors_per_block,
3097 (unsigned long long) from_cblock(residency),
3098 (unsigned long long) from_cblock(cache->cache_size),
3099 (unsigned int) atomic_read(&cache->stats.read_hit),
3100 (unsigned int) atomic_read(&cache->stats.read_miss),
3101 (unsigned int) atomic_read(&cache->stats.write_hit),
3102 (unsigned int) atomic_read(&cache->stats.write_miss),
3103 (unsigned int) atomic_read(&cache->stats.demotion),
3104 (unsigned int) atomic_read(&cache->stats.promotion),
3105 (unsigned long) atomic_read(&cache->nr_dirty));
3107 emit_flags(cache, result, maxlen, &sz);
3109 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
3111 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
3112 if (sz < maxlen) {
3113 r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
3114 if (r)
3115 DMERR("%s: policy_emit_config_values returned %d",
3116 cache_device_name(cache), r);
3119 if (get_cache_mode(cache) == CM_READ_ONLY)
3120 DMEMIT("ro ");
3121 else
3122 DMEMIT("rw ");
3124 r = dm_cache_metadata_needs_check(cache->cmd, &needs_check);
3126 if (r || needs_check)
3127 DMEMIT("needs_check ");
3128 else
3129 DMEMIT("- ");
3131 break;
3133 case STATUSTYPE_TABLE:
3134 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
3135 DMEMIT("%s ", buf);
3136 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
3137 DMEMIT("%s ", buf);
3138 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
3139 DMEMIT("%s", buf);
3141 for (i = 0; i < cache->nr_ctr_args - 1; i++)
3142 DMEMIT(" %s", cache->ctr_args[i]);
3143 if (cache->nr_ctr_args)
3144 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
3145 break;
3147 case STATUSTYPE_IMA:
3148 DMEMIT_TARGET_NAME_VERSION(ti->type);
3149 if (get_cache_mode(cache) == CM_FAIL)
3150 DMEMIT(",metadata_mode=fail");
3151 else if (get_cache_mode(cache) == CM_READ_ONLY)
3152 DMEMIT(",metadata_mode=ro");
3153 else
3154 DMEMIT(",metadata_mode=rw");
3156 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
3157 DMEMIT(",cache_metadata_device=%s", buf);
3158 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
3159 DMEMIT(",cache_device=%s", buf);
3160 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
3161 DMEMIT(",cache_origin_device=%s", buf);
3162 DMEMIT(",writethrough=%c", writethrough_mode(cache) ? 'y' : 'n');
3163 DMEMIT(",writeback=%c", writeback_mode(cache) ? 'y' : 'n');
3164 DMEMIT(",passthrough=%c", passthrough_mode(cache) ? 'y' : 'n');
3165 DMEMIT(",metadata2=%c", cache->features.metadata_version == 2 ? 'y' : 'n');
3166 DMEMIT(",no_discard_passdown=%c", cache->features.discard_passdown ? 'n' : 'y');
3167 DMEMIT(";");
3168 break;
3171 return;
3173 err:
3174 DMEMIT("Error");
3178 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
3179 * the one-past-the-end value.
3181 struct cblock_range {
3182 dm_cblock_t begin;
3183 dm_cblock_t end;
3187 * A cache block range can take two forms:
3189 * i) A single cblock, eg. '3456'
3190 * ii) A begin and end cblock with a dash between, eg. 123-234
3192 static int parse_cblock_range(struct cache *cache, const char *str,
3193 struct cblock_range *result)
3195 char dummy;
3196 uint64_t b, e;
3197 int r;
3200 * Try and parse form (ii) first.
3202 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
3204 if (r == 2) {
3205 result->begin = to_cblock(b);
3206 result->end = to_cblock(e);
3207 return 0;
3211 * That didn't work, try form (i).
3213 r = sscanf(str, "%llu%c", &b, &dummy);
3215 if (r == 1) {
3216 result->begin = to_cblock(b);
3217 result->end = to_cblock(from_cblock(result->begin) + 1u);
3218 return 0;
3221 DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
3222 return -EINVAL;
3225 static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
3227 uint64_t b = from_cblock(range->begin);
3228 uint64_t e = from_cblock(range->end);
3229 uint64_t n = from_cblock(cache->cache_size);
3231 if (b >= n) {
3232 DMERR("%s: begin cblock out of range: %llu >= %llu",
3233 cache_device_name(cache), b, n);
3234 return -EINVAL;
3237 if (e > n) {
3238 DMERR("%s: end cblock out of range: %llu > %llu",
3239 cache_device_name(cache), e, n);
3240 return -EINVAL;
3243 if (b >= e) {
3244 DMERR("%s: invalid cblock range: %llu >= %llu",
3245 cache_device_name(cache), b, e);
3246 return -EINVAL;
3249 return 0;
3252 static inline dm_cblock_t cblock_succ(dm_cblock_t b)
3254 return to_cblock(from_cblock(b) + 1);
3257 static int request_invalidation(struct cache *cache, struct cblock_range *range)
3259 int r = 0;
3262 * We don't need to do any locking here because we know we're in
3263 * passthrough mode. There's is potential for a race between an
3264 * invalidation triggered by an io and an invalidation message. This
3265 * is harmless, we must not worry if the policy call fails.
3267 while (range->begin != range->end) {
3268 r = invalidate_cblock(cache, range->begin);
3269 if (r)
3270 return r;
3272 range->begin = cblock_succ(range->begin);
3275 cache->commit_requested = true;
3276 return r;
3279 static int process_invalidate_cblocks_message(struct cache *cache, unsigned int count,
3280 const char **cblock_ranges)
3282 int r = 0;
3283 unsigned int i;
3284 struct cblock_range range;
3286 if (!passthrough_mode(cache)) {
3287 DMERR("%s: cache has to be in passthrough mode for invalidation",
3288 cache_device_name(cache));
3289 return -EPERM;
3292 for (i = 0; i < count; i++) {
3293 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3294 if (r)
3295 break;
3297 r = validate_cblock_range(cache, &range);
3298 if (r)
3299 break;
3302 * Pass begin and end origin blocks to the worker and wake it.
3304 r = request_invalidation(cache, &range);
3305 if (r)
3306 break;
3309 return r;
3313 * Supports
3314 * "<key> <value>"
3315 * and
3316 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
3318 * The key migration_threshold is supported by the cache target core.
3320 static int cache_message(struct dm_target *ti, unsigned int argc, char **argv,
3321 char *result, unsigned int maxlen)
3323 struct cache *cache = ti->private;
3325 if (!argc)
3326 return -EINVAL;
3328 if (get_cache_mode(cache) >= CM_READ_ONLY) {
3329 DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
3330 cache_device_name(cache));
3331 return -EOPNOTSUPP;
3334 if (!strcasecmp(argv[0], "invalidate_cblocks"))
3335 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3337 if (argc != 2)
3338 return -EINVAL;
3340 return set_config_value(cache, argv[0], argv[1]);
3343 static int cache_iterate_devices(struct dm_target *ti,
3344 iterate_devices_callout_fn fn, void *data)
3346 int r = 0;
3347 struct cache *cache = ti->private;
3349 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3350 if (!r)
3351 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3353 return r;
3357 * If discard_passdown was enabled verify that the origin device
3358 * supports discards. Disable discard_passdown if not.
3360 static void disable_passdown_if_not_supported(struct cache *cache)
3362 struct block_device *origin_bdev = cache->origin_dev->bdev;
3363 struct queue_limits *origin_limits = &bdev_get_queue(origin_bdev)->limits;
3364 const char *reason = NULL;
3366 if (!cache->features.discard_passdown)
3367 return;
3369 if (!bdev_max_discard_sectors(origin_bdev))
3370 reason = "discard unsupported";
3372 else if (origin_limits->max_discard_sectors < cache->sectors_per_block)
3373 reason = "max discard sectors smaller than a block";
3375 if (reason) {
3376 DMWARN("Origin device (%pg) %s: Disabling discard passdown.",
3377 origin_bdev, reason);
3378 cache->features.discard_passdown = false;
3382 static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3384 struct block_device *origin_bdev = cache->origin_dev->bdev;
3385 struct queue_limits *origin_limits = &bdev_get_queue(origin_bdev)->limits;
3387 if (!cache->features.discard_passdown) {
3388 /* No passdown is done so setting own virtual limits */
3389 limits->max_hw_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
3390 cache->origin_sectors);
3391 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
3392 return;
3396 * cache_iterate_devices() is stacking both origin and fast device limits
3397 * but discards aren't passed to fast device, so inherit origin's limits.
3399 limits->max_hw_discard_sectors = origin_limits->max_hw_discard_sectors;
3400 limits->discard_granularity = origin_limits->discard_granularity;
3401 limits->discard_alignment = origin_limits->discard_alignment;
3404 static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3406 struct cache *cache = ti->private;
3407 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3410 * If the system-determined stacked limits are compatible with the
3411 * cache's blocksize (io_opt is a factor) do not override them.
3413 if (io_opt_sectors < cache->sectors_per_block ||
3414 do_div(io_opt_sectors, cache->sectors_per_block)) {
3415 limits->io_min = cache->sectors_per_block << SECTOR_SHIFT;
3416 limits->io_opt = cache->sectors_per_block << SECTOR_SHIFT;
3419 disable_passdown_if_not_supported(cache);
3420 set_discard_limits(cache, limits);
3423 /*----------------------------------------------------------------*/
3425 static struct target_type cache_target = {
3426 .name = "cache",
3427 .version = {2, 2, 0},
3428 .module = THIS_MODULE,
3429 .ctr = cache_ctr,
3430 .dtr = cache_dtr,
3431 .map = cache_map,
3432 .end_io = cache_end_io,
3433 .postsuspend = cache_postsuspend,
3434 .preresume = cache_preresume,
3435 .resume = cache_resume,
3436 .status = cache_status,
3437 .message = cache_message,
3438 .iterate_devices = cache_iterate_devices,
3439 .io_hints = cache_io_hints,
3442 static int __init dm_cache_init(void)
3444 int r;
3446 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3447 if (!migration_cache)
3448 return -ENOMEM;
3450 r = dm_register_target(&cache_target);
3451 if (r) {
3452 kmem_cache_destroy(migration_cache);
3453 return r;
3456 return 0;
3459 static void __exit dm_cache_exit(void)
3461 dm_unregister_target(&cache_target);
3462 kmem_cache_destroy(migration_cache);
3465 module_init(dm_cache_init);
3466 module_exit(dm_cache_exit);
3468 MODULE_DESCRIPTION(DM_NAME " cache target");
3469 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3470 MODULE_LICENSE("GPL");