GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / md / dm-cache-target.c
1 /*
2  * Copyright (C) 2012 Red Hat. All rights reserved.
3  *
4  * This file is released under the GPL.
5  */
6
7 #include "dm.h"
8 #include "dm-bio-prison-v2.h"
9 #include "dm-bio-record.h"
10 #include "dm-cache-metadata.h"
11
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/jiffies.h>
15 #include <linux/init.h>
16 #include <linux/mempool.h>
17 #include <linux/module.h>
18 #include <linux/rwsem.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21
22 #define DM_MSG_PREFIX "cache"
23
24 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
25         "A percentage of time allocated for copying to and/or from cache");
26
27 /*----------------------------------------------------------------*/
28
29 /*
30  * Glossary:
31  *
32  * oblock: index of an origin block
33  * cblock: index of a cache block
34  * promotion: movement of a block from origin to cache
35  * demotion: movement of a block from cache to origin
36  * migration: movement of a block between the origin and cache device,
37  *            either direction
38  */
39
40 /*----------------------------------------------------------------*/
41
42 struct io_tracker {
43         spinlock_t lock;
44
45         /*
46          * Sectors of in-flight IO.
47          */
48         sector_t in_flight;
49
50         /*
51          * The time, in jiffies, when this device became idle (if it is
52          * indeed idle).
53          */
54         unsigned long idle_time;
55         unsigned long last_update_time;
56 };
57
58 static void iot_init(struct io_tracker *iot)
59 {
60         spin_lock_init(&iot->lock);
61         iot->in_flight = 0ul;
62         iot->idle_time = 0ul;
63         iot->last_update_time = jiffies;
64 }
65
66 static bool __iot_idle_for(struct io_tracker *iot, unsigned long jifs)
67 {
68         if (iot->in_flight)
69                 return false;
70
71         return time_after(jiffies, iot->idle_time + jifs);
72 }
73
74 static bool iot_idle_for(struct io_tracker *iot, unsigned long jifs)
75 {
76         bool r;
77         unsigned long flags;
78
79         spin_lock_irqsave(&iot->lock, flags);
80         r = __iot_idle_for(iot, jifs);
81         spin_unlock_irqrestore(&iot->lock, flags);
82
83         return r;
84 }
85
86 static void iot_io_begin(struct io_tracker *iot, sector_t len)
87 {
88         unsigned long flags;
89
90         spin_lock_irqsave(&iot->lock, flags);
91         iot->in_flight += len;
92         spin_unlock_irqrestore(&iot->lock, flags);
93 }
94
95 static void __iot_io_end(struct io_tracker *iot, sector_t len)
96 {
97         if (!len)
98                 return;
99
100         iot->in_flight -= len;
101         if (!iot->in_flight)
102                 iot->idle_time = jiffies;
103 }
104
105 static void iot_io_end(struct io_tracker *iot, sector_t len)
106 {
107         unsigned long flags;
108
109         spin_lock_irqsave(&iot->lock, flags);
110         __iot_io_end(iot, len);
111         spin_unlock_irqrestore(&iot->lock, flags);
112 }
113
114 /*----------------------------------------------------------------*/
115
116 /*
117  * Represents a chunk of future work.  'input' allows continuations to pass
118  * values between themselves, typically error values.
119  */
120 struct continuation {
121         struct work_struct ws;
122         blk_status_t input;
123 };
124
125 static inline void init_continuation(struct continuation *k,
126                                      void (*fn)(struct work_struct *))
127 {
128         INIT_WORK(&k->ws, fn);
129         k->input = 0;
130 }
131
132 static inline void queue_continuation(struct workqueue_struct *wq,
133                                       struct continuation *k)
134 {
135         queue_work(wq, &k->ws);
136 }
137
138 /*----------------------------------------------------------------*/
139
140 /*
141  * The batcher collects together pieces of work that need a particular
142  * operation to occur before they can proceed (typically a commit).
143  */
144 struct batcher {
145         /*
146          * The operation that everyone is waiting for.
147          */
148         blk_status_t (*commit_op)(void *context);
149         void *commit_context;
150
151         /*
152          * This is how bios should be issued once the commit op is complete
153          * (accounted_request).
154          */
155         void (*issue_op)(struct bio *bio, void *context);
156         void *issue_context;
157
158         /*
159          * Queued work gets put on here after commit.
160          */
161         struct workqueue_struct *wq;
162
163         spinlock_t lock;
164         struct list_head work_items;
165         struct bio_list bios;
166         struct work_struct commit_work;
167
168         bool commit_scheduled;
169 };
170
171 static void __commit(struct work_struct *_ws)
172 {
173         struct batcher *b = container_of(_ws, struct batcher, commit_work);
174         blk_status_t r;
175         unsigned long flags;
176         struct list_head work_items;
177         struct work_struct *ws, *tmp;
178         struct continuation *k;
179         struct bio *bio;
180         struct bio_list bios;
181
182         INIT_LIST_HEAD(&work_items);
183         bio_list_init(&bios);
184
185         /*
186          * We have to grab these before the commit_op to avoid a race
187          * condition.
188          */
189         spin_lock_irqsave(&b->lock, flags);
190         list_splice_init(&b->work_items, &work_items);
191         bio_list_merge(&bios, &b->bios);
192         bio_list_init(&b->bios);
193         b->commit_scheduled = false;
194         spin_unlock_irqrestore(&b->lock, flags);
195
196         r = b->commit_op(b->commit_context);
197
198         list_for_each_entry_safe(ws, tmp, &work_items, entry) {
199                 k = container_of(ws, struct continuation, ws);
200                 k->input = r;
201                 INIT_LIST_HEAD(&ws->entry); /* to avoid a WARN_ON */
202                 queue_work(b->wq, ws);
203         }
204
205         while ((bio = bio_list_pop(&bios))) {
206                 if (r) {
207                         bio->bi_status = r;
208                         bio_endio(bio);
209                 } else
210                         b->issue_op(bio, b->issue_context);
211         }
212 }
213
214 static void batcher_init(struct batcher *b,
215                          blk_status_t (*commit_op)(void *),
216                          void *commit_context,
217                          void (*issue_op)(struct bio *bio, void *),
218                          void *issue_context,
219                          struct workqueue_struct *wq)
220 {
221         b->commit_op = commit_op;
222         b->commit_context = commit_context;
223         b->issue_op = issue_op;
224         b->issue_context = issue_context;
225         b->wq = wq;
226
227         spin_lock_init(&b->lock);
228         INIT_LIST_HEAD(&b->work_items);
229         bio_list_init(&b->bios);
230         INIT_WORK(&b->commit_work, __commit);
231         b->commit_scheduled = false;
232 }
233
234 static void async_commit(struct batcher *b)
235 {
236         queue_work(b->wq, &b->commit_work);
237 }
238
239 static void continue_after_commit(struct batcher *b, struct continuation *k)
240 {
241         unsigned long flags;
242         bool commit_scheduled;
243
244         spin_lock_irqsave(&b->lock, flags);
245         commit_scheduled = b->commit_scheduled;
246         list_add_tail(&k->ws.entry, &b->work_items);
247         spin_unlock_irqrestore(&b->lock, flags);
248
249         if (commit_scheduled)
250                 async_commit(b);
251 }
252
253 /*
254  * Bios are errored if commit failed.
255  */
256 static void issue_after_commit(struct batcher *b, struct bio *bio)
257 {
258        unsigned long flags;
259        bool commit_scheduled;
260
261        spin_lock_irqsave(&b->lock, flags);
262        commit_scheduled = b->commit_scheduled;
263        bio_list_add(&b->bios, bio);
264        spin_unlock_irqrestore(&b->lock, flags);
265
266        if (commit_scheduled)
267                async_commit(b);
268 }
269
270 /*
271  * Call this if some urgent work is waiting for the commit to complete.
272  */
273 static void schedule_commit(struct batcher *b)
274 {
275         bool immediate;
276         unsigned long flags;
277
278         spin_lock_irqsave(&b->lock, flags);
279         immediate = !list_empty(&b->work_items) || !bio_list_empty(&b->bios);
280         b->commit_scheduled = true;
281         spin_unlock_irqrestore(&b->lock, flags);
282
283         if (immediate)
284                 async_commit(b);
285 }
286
287 /*
288  * There are a couple of places where we let a bio run, but want to do some
289  * work before calling its endio function.  We do this by temporarily
290  * changing the endio fn.
291  */
292 struct dm_hook_info {
293         bio_end_io_t *bi_end_io;
294 };
295
296 static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
297                         bio_end_io_t *bi_end_io, void *bi_private)
298 {
299         h->bi_end_io = bio->bi_end_io;
300
301         bio->bi_end_io = bi_end_io;
302         bio->bi_private = bi_private;
303 }
304
305 static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
306 {
307         bio->bi_end_io = h->bi_end_io;
308 }
309
310 /*----------------------------------------------------------------*/
311
312 #define MIGRATION_POOL_SIZE 128
313 #define COMMIT_PERIOD HZ
314 #define MIGRATION_COUNT_WINDOW 10
315
316 /*
317  * The block size of the device holding cache data must be
318  * between 32KB and 1GB.
319  */
320 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
321 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
322
323 enum cache_metadata_mode {
324         CM_WRITE,               /* metadata may be changed */
325         CM_READ_ONLY,           /* metadata may not be changed */
326         CM_FAIL
327 };
328
329 enum cache_io_mode {
330         /*
331          * Data is written to cached blocks only.  These blocks are marked
332          * dirty.  If you lose the cache device you will lose data.
333          * Potential performance increase for both reads and writes.
334          */
335         CM_IO_WRITEBACK,
336
337         /*
338          * Data is written to both cache and origin.  Blocks are never
339          * dirty.  Potential performance benfit for reads only.
340          */
341         CM_IO_WRITETHROUGH,
342
343         /*
344          * A degraded mode useful for various cache coherency situations
345          * (eg, rolling back snapshots).  Reads and writes always go to the
346          * origin.  If a write goes to a cached oblock, then the cache
347          * block is invalidated.
348          */
349         CM_IO_PASSTHROUGH
350 };
351
352 struct cache_features {
353         enum cache_metadata_mode mode;
354         enum cache_io_mode io_mode;
355         unsigned metadata_version;
356 };
357
358 struct cache_stats {
359         atomic_t read_hit;
360         atomic_t read_miss;
361         atomic_t write_hit;
362         atomic_t write_miss;
363         atomic_t demotion;
364         atomic_t promotion;
365         atomic_t writeback;
366         atomic_t copies_avoided;
367         atomic_t cache_cell_clash;
368         atomic_t commit_count;
369         atomic_t discard_count;
370 };
371
372 struct cache {
373         struct dm_target *ti;
374         spinlock_t lock;
375
376         /*
377          * Fields for converting from sectors to blocks.
378          */
379         int sectors_per_block_shift;
380         sector_t sectors_per_block;
381
382         struct dm_cache_metadata *cmd;
383
384         /*
385          * Metadata is written to this device.
386          */
387         struct dm_dev *metadata_dev;
388
389         /*
390          * The slower of the two data devices.  Typically a spindle.
391          */
392         struct dm_dev *origin_dev;
393
394         /*
395          * The faster of the two data devices.  Typically an SSD.
396          */
397         struct dm_dev *cache_dev;
398
399         /*
400          * Size of the origin device in _complete_ blocks and native sectors.
401          */
402         dm_oblock_t origin_blocks;
403         sector_t origin_sectors;
404
405         /*
406          * Size of the cache device in blocks.
407          */
408         dm_cblock_t cache_size;
409
410         /*
411          * Invalidation fields.
412          */
413         spinlock_t invalidation_lock;
414         struct list_head invalidation_requests;
415
416         sector_t migration_threshold;
417         wait_queue_head_t migration_wait;
418         atomic_t nr_allocated_migrations;
419
420         /*
421          * The number of in flight migrations that are performing
422          * background io. eg, promotion, writeback.
423          */
424         atomic_t nr_io_migrations;
425
426         struct bio_list deferred_bios;
427
428         struct rw_semaphore quiesce_lock;
429
430         struct dm_target_callbacks callbacks;
431
432         /*
433          * origin_blocks entries, discarded if set.
434          */
435         dm_dblock_t discard_nr_blocks;
436         unsigned long *discard_bitset;
437         uint32_t discard_block_size; /* a power of 2 times sectors per block */
438
439         /*
440          * Rather than reconstructing the table line for the status we just
441          * save it and regurgitate.
442          */
443         unsigned nr_ctr_args;
444         const char **ctr_args;
445
446         struct dm_kcopyd_client *copier;
447         struct work_struct deferred_bio_worker;
448         struct work_struct migration_worker;
449         struct workqueue_struct *wq;
450         struct delayed_work waker;
451         struct dm_bio_prison_v2 *prison;
452
453         /*
454          * cache_size entries, dirty if set
455          */
456         unsigned long *dirty_bitset;
457         atomic_t nr_dirty;
458
459         unsigned policy_nr_args;
460         struct dm_cache_policy *policy;
461
462         /*
463          * Cache features such as write-through.
464          */
465         struct cache_features features;
466
467         struct cache_stats stats;
468
469         bool need_tick_bio:1;
470         bool sized:1;
471         bool invalidate:1;
472         bool commit_requested:1;
473         bool loaded_mappings:1;
474         bool loaded_discards:1;
475
476         struct rw_semaphore background_work_lock;
477
478         struct batcher committer;
479         struct work_struct commit_ws;
480
481         struct io_tracker tracker;
482
483         mempool_t migration_pool;
484
485         struct bio_set bs;
486 };
487
488 struct per_bio_data {
489         bool tick:1;
490         unsigned req_nr:2;
491         struct dm_bio_prison_cell_v2 *cell;
492         struct dm_hook_info hook_info;
493         sector_t len;
494 };
495
496 struct dm_cache_migration {
497         struct continuation k;
498         struct cache *cache;
499
500         struct policy_work *op;
501         struct bio *overwrite_bio;
502         struct dm_bio_prison_cell_v2 *cell;
503
504         dm_cblock_t invalidate_cblock;
505         dm_oblock_t invalidate_oblock;
506 };
507
508 /*----------------------------------------------------------------*/
509
510 static bool writethrough_mode(struct cache *cache)
511 {
512         return cache->features.io_mode == CM_IO_WRITETHROUGH;
513 }
514
515 static bool writeback_mode(struct cache *cache)
516 {
517         return cache->features.io_mode == CM_IO_WRITEBACK;
518 }
519
520 static inline bool passthrough_mode(struct cache *cache)
521 {
522         return unlikely(cache->features.io_mode == CM_IO_PASSTHROUGH);
523 }
524
525 /*----------------------------------------------------------------*/
526
527 static void wake_deferred_bio_worker(struct cache *cache)
528 {
529         queue_work(cache->wq, &cache->deferred_bio_worker);
530 }
531
532 static void wake_migration_worker(struct cache *cache)
533 {
534         if (passthrough_mode(cache))
535                 return;
536
537         queue_work(cache->wq, &cache->migration_worker);
538 }
539
540 /*----------------------------------------------------------------*/
541
542 static struct dm_bio_prison_cell_v2 *alloc_prison_cell(struct cache *cache)
543 {
544         return dm_bio_prison_alloc_cell_v2(cache->prison, GFP_NOIO);
545 }
546
547 static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell_v2 *cell)
548 {
549         dm_bio_prison_free_cell_v2(cache->prison, cell);
550 }
551
552 static struct dm_cache_migration *alloc_migration(struct cache *cache)
553 {
554         struct dm_cache_migration *mg;
555
556         mg = mempool_alloc(&cache->migration_pool, GFP_NOIO);
557
558         memset(mg, 0, sizeof(*mg));
559
560         mg->cache = cache;
561         atomic_inc(&cache->nr_allocated_migrations);
562
563         return mg;
564 }
565
566 static void free_migration(struct dm_cache_migration *mg)
567 {
568         struct cache *cache = mg->cache;
569
570         if (atomic_dec_and_test(&cache->nr_allocated_migrations))
571                 wake_up(&cache->migration_wait);
572
573         mempool_free(mg, &cache->migration_pool);
574 }
575
576 /*----------------------------------------------------------------*/
577
578 static inline dm_oblock_t oblock_succ(dm_oblock_t b)
579 {
580         return to_oblock(from_oblock(b) + 1ull);
581 }
582
583 static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key_v2 *key)
584 {
585         key->virtual = 0;
586         key->dev = 0;
587         key->block_begin = from_oblock(begin);
588         key->block_end = from_oblock(end);
589 }
590
591 /*
592  * We have two lock levels.  Level 0, which is used to prevent WRITEs, and
593  * level 1 which prevents *both* READs and WRITEs.
594  */
595 #define WRITE_LOCK_LEVEL 0
596 #define READ_WRITE_LOCK_LEVEL 1
597
598 static unsigned lock_level(struct bio *bio)
599 {
600         return bio_data_dir(bio) == WRITE ?
601                 WRITE_LOCK_LEVEL :
602                 READ_WRITE_LOCK_LEVEL;
603 }
604
605 /*----------------------------------------------------------------
606  * Per bio data
607  *--------------------------------------------------------------*/
608
609 static struct per_bio_data *get_per_bio_data(struct bio *bio)
610 {
611         struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
612         BUG_ON(!pb);
613         return pb;
614 }
615
616 static struct per_bio_data *init_per_bio_data(struct bio *bio)
617 {
618         struct per_bio_data *pb = get_per_bio_data(bio);
619
620         pb->tick = false;
621         pb->req_nr = dm_bio_get_target_bio_nr(bio);
622         pb->cell = NULL;
623         pb->len = 0;
624
625         return pb;
626 }
627
628 /*----------------------------------------------------------------*/
629
630 static void defer_bio(struct cache *cache, struct bio *bio)
631 {
632         unsigned long flags;
633
634         spin_lock_irqsave(&cache->lock, flags);
635         bio_list_add(&cache->deferred_bios, bio);
636         spin_unlock_irqrestore(&cache->lock, flags);
637
638         wake_deferred_bio_worker(cache);
639 }
640
641 static void defer_bios(struct cache *cache, struct bio_list *bios)
642 {
643         unsigned long flags;
644
645         spin_lock_irqsave(&cache->lock, flags);
646         bio_list_merge(&cache->deferred_bios, bios);
647         bio_list_init(bios);
648         spin_unlock_irqrestore(&cache->lock, flags);
649
650         wake_deferred_bio_worker(cache);
651 }
652
653 /*----------------------------------------------------------------*/
654
655 static bool bio_detain_shared(struct cache *cache, dm_oblock_t oblock, struct bio *bio)
656 {
657         bool r;
658         struct per_bio_data *pb;
659         struct dm_cell_key_v2 key;
660         dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL);
661         struct dm_bio_prison_cell_v2 *cell_prealloc, *cell;
662
663         cell_prealloc = alloc_prison_cell(cache); /* FIXME: allow wait if calling from worker */
664
665         build_key(oblock, end, &key);
666         r = dm_cell_get_v2(cache->prison, &key, lock_level(bio), bio, cell_prealloc, &cell);
667         if (!r) {
668                 /*
669                  * Failed to get the lock.
670                  */
671                 free_prison_cell(cache, cell_prealloc);
672                 return r;
673         }
674
675         if (cell != cell_prealloc)
676                 free_prison_cell(cache, cell_prealloc);
677
678         pb = get_per_bio_data(bio);
679         pb->cell = cell;
680
681         return r;
682 }
683
684 /*----------------------------------------------------------------*/
685
686 static bool is_dirty(struct cache *cache, dm_cblock_t b)
687 {
688         return test_bit(from_cblock(b), cache->dirty_bitset);
689 }
690
691 static void set_dirty(struct cache *cache, dm_cblock_t cblock)
692 {
693         if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
694                 atomic_inc(&cache->nr_dirty);
695                 policy_set_dirty(cache->policy, cblock);
696         }
697 }
698
699 /*
700  * These two are called when setting after migrations to force the policy
701  * and dirty bitset to be in sync.
702  */
703 static void force_set_dirty(struct cache *cache, dm_cblock_t cblock)
704 {
705         if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset))
706                 atomic_inc(&cache->nr_dirty);
707         policy_set_dirty(cache->policy, cblock);
708 }
709
710 static void force_clear_dirty(struct cache *cache, dm_cblock_t cblock)
711 {
712         if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
713                 if (atomic_dec_return(&cache->nr_dirty) == 0)
714                         dm_table_event(cache->ti->table);
715         }
716
717         policy_clear_dirty(cache->policy, cblock);
718 }
719
720 /*----------------------------------------------------------------*/
721
722 static bool block_size_is_power_of_two(struct cache *cache)
723 {
724         return cache->sectors_per_block_shift >= 0;
725 }
726
727 /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
728 #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
729 __always_inline
730 #endif
731 static dm_block_t block_div(dm_block_t b, uint32_t n)
732 {
733         do_div(b, n);
734
735         return b;
736 }
737
738 static dm_block_t oblocks_per_dblock(struct cache *cache)
739 {
740         dm_block_t oblocks = cache->discard_block_size;
741
742         if (block_size_is_power_of_two(cache))
743                 oblocks >>= cache->sectors_per_block_shift;
744         else
745                 oblocks = block_div(oblocks, cache->sectors_per_block);
746
747         return oblocks;
748 }
749
750 static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
751 {
752         return to_dblock(block_div(from_oblock(oblock),
753                                    oblocks_per_dblock(cache)));
754 }
755
756 static void set_discard(struct cache *cache, dm_dblock_t b)
757 {
758         unsigned long flags;
759
760         BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks));
761         atomic_inc(&cache->stats.discard_count);
762
763         spin_lock_irqsave(&cache->lock, flags);
764         set_bit(from_dblock(b), cache->discard_bitset);
765         spin_unlock_irqrestore(&cache->lock, flags);
766 }
767
768 static void clear_discard(struct cache *cache, dm_dblock_t b)
769 {
770         unsigned long flags;
771
772         spin_lock_irqsave(&cache->lock, flags);
773         clear_bit(from_dblock(b), cache->discard_bitset);
774         spin_unlock_irqrestore(&cache->lock, flags);
775 }
776
777 static bool is_discarded(struct cache *cache, dm_dblock_t b)
778 {
779         int r;
780         unsigned long flags;
781
782         spin_lock_irqsave(&cache->lock, flags);
783         r = test_bit(from_dblock(b), cache->discard_bitset);
784         spin_unlock_irqrestore(&cache->lock, flags);
785
786         return r;
787 }
788
789 static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
790 {
791         int r;
792         unsigned long flags;
793
794         spin_lock_irqsave(&cache->lock, flags);
795         r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
796                      cache->discard_bitset);
797         spin_unlock_irqrestore(&cache->lock, flags);
798
799         return r;
800 }
801
802 /*----------------------------------------------------------------
803  * Remapping
804  *--------------------------------------------------------------*/
805 static void remap_to_origin(struct cache *cache, struct bio *bio)
806 {
807         bio_set_dev(bio, cache->origin_dev->bdev);
808 }
809
810 static void remap_to_cache(struct cache *cache, struct bio *bio,
811                            dm_cblock_t cblock)
812 {
813         sector_t bi_sector = bio->bi_iter.bi_sector;
814         sector_t block = from_cblock(cblock);
815
816         bio_set_dev(bio, cache->cache_dev->bdev);
817         if (!block_size_is_power_of_two(cache))
818                 bio->bi_iter.bi_sector =
819                         (block * cache->sectors_per_block) +
820                         sector_div(bi_sector, cache->sectors_per_block);
821         else
822                 bio->bi_iter.bi_sector =
823                         (block << cache->sectors_per_block_shift) |
824                         (bi_sector & (cache->sectors_per_block - 1));
825 }
826
827 static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
828 {
829         unsigned long flags;
830         struct per_bio_data *pb;
831
832         spin_lock_irqsave(&cache->lock, flags);
833         if (cache->need_tick_bio && !op_is_flush(bio->bi_opf) &&
834             bio_op(bio) != REQ_OP_DISCARD) {
835                 pb = get_per_bio_data(bio);
836                 pb->tick = true;
837                 cache->need_tick_bio = false;
838         }
839         spin_unlock_irqrestore(&cache->lock, flags);
840 }
841
842 static void __remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
843                                             dm_oblock_t oblock, bool bio_has_pbd)
844 {
845         if (bio_has_pbd)
846                 check_if_tick_bio_needed(cache, bio);
847         remap_to_origin(cache, bio);
848         if (bio_data_dir(bio) == WRITE)
849                 clear_discard(cache, oblock_to_dblock(cache, oblock));
850 }
851
852 static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
853                                           dm_oblock_t oblock)
854 {
855         // FIXME: check_if_tick_bio_needed() is called way too much through this interface
856         __remap_to_origin_clear_discard(cache, bio, oblock, true);
857 }
858
859 static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
860                                  dm_oblock_t oblock, dm_cblock_t cblock)
861 {
862         check_if_tick_bio_needed(cache, bio);
863         remap_to_cache(cache, bio, cblock);
864         if (bio_data_dir(bio) == WRITE) {
865                 set_dirty(cache, cblock);
866                 clear_discard(cache, oblock_to_dblock(cache, oblock));
867         }
868 }
869
870 static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
871 {
872         sector_t block_nr = bio->bi_iter.bi_sector;
873
874         if (!block_size_is_power_of_two(cache))
875                 (void) sector_div(block_nr, cache->sectors_per_block);
876         else
877                 block_nr >>= cache->sectors_per_block_shift;
878
879         return to_oblock(block_nr);
880 }
881
882 static bool accountable_bio(struct cache *cache, struct bio *bio)
883 {
884         return bio_op(bio) != REQ_OP_DISCARD;
885 }
886
887 static void accounted_begin(struct cache *cache, struct bio *bio)
888 {
889         struct per_bio_data *pb;
890
891         if (accountable_bio(cache, bio)) {
892                 pb = get_per_bio_data(bio);
893                 pb->len = bio_sectors(bio);
894                 iot_io_begin(&cache->tracker, pb->len);
895         }
896 }
897
898 static void accounted_complete(struct cache *cache, struct bio *bio)
899 {
900         struct per_bio_data *pb = get_per_bio_data(bio);
901
902         iot_io_end(&cache->tracker, pb->len);
903 }
904
905 static void accounted_request(struct cache *cache, struct bio *bio)
906 {
907         accounted_begin(cache, bio);
908         generic_make_request(bio);
909 }
910
911 static void issue_op(struct bio *bio, void *context)
912 {
913         struct cache *cache = context;
914         accounted_request(cache, bio);
915 }
916
917 /*
918  * When running in writethrough mode we need to send writes to clean blocks
919  * to both the cache and origin devices.  Clone the bio and send them in parallel.
920  */
921 static void remap_to_origin_and_cache(struct cache *cache, struct bio *bio,
922                                       dm_oblock_t oblock, dm_cblock_t cblock)
923 {
924         struct bio *origin_bio = bio_clone_fast(bio, GFP_NOIO, &cache->bs);
925
926         BUG_ON(!origin_bio);
927
928         bio_chain(origin_bio, bio);
929         /*
930          * Passing false to __remap_to_origin_clear_discard() skips
931          * all code that might use per_bio_data (since clone doesn't have it)
932          */
933         __remap_to_origin_clear_discard(cache, origin_bio, oblock, false);
934         submit_bio(origin_bio);
935
936         remap_to_cache(cache, bio, cblock);
937 }
938
939 /*----------------------------------------------------------------
940  * Failure modes
941  *--------------------------------------------------------------*/
942 static enum cache_metadata_mode get_cache_mode(struct cache *cache)
943 {
944         return cache->features.mode;
945 }
946
947 static const char *cache_device_name(struct cache *cache)
948 {
949         return dm_device_name(dm_table_get_md(cache->ti->table));
950 }
951
952 static void notify_mode_switch(struct cache *cache, enum cache_metadata_mode mode)
953 {
954         const char *descs[] = {
955                 "write",
956                 "read-only",
957                 "fail"
958         };
959
960         dm_table_event(cache->ti->table);
961         DMINFO("%s: switching cache to %s mode",
962                cache_device_name(cache), descs[(int)mode]);
963 }
964
965 static void set_cache_mode(struct cache *cache, enum cache_metadata_mode new_mode)
966 {
967         bool needs_check;
968         enum cache_metadata_mode old_mode = get_cache_mode(cache);
969
970         if (dm_cache_metadata_needs_check(cache->cmd, &needs_check)) {
971                 DMERR("%s: unable to read needs_check flag, setting failure mode.",
972                       cache_device_name(cache));
973                 new_mode = CM_FAIL;
974         }
975
976         if (new_mode == CM_WRITE && needs_check) {
977                 DMERR("%s: unable to switch cache to write mode until repaired.",
978                       cache_device_name(cache));
979                 if (old_mode != new_mode)
980                         new_mode = old_mode;
981                 else
982                         new_mode = CM_READ_ONLY;
983         }
984
985         /* Never move out of fail mode */
986         if (old_mode == CM_FAIL)
987                 new_mode = CM_FAIL;
988
989         switch (new_mode) {
990         case CM_FAIL:
991         case CM_READ_ONLY:
992                 dm_cache_metadata_set_read_only(cache->cmd);
993                 break;
994
995         case CM_WRITE:
996                 dm_cache_metadata_set_read_write(cache->cmd);
997                 break;
998         }
999
1000         cache->features.mode = new_mode;
1001
1002         if (new_mode != old_mode)
1003                 notify_mode_switch(cache, new_mode);
1004 }
1005
1006 static void abort_transaction(struct cache *cache)
1007 {
1008         const char *dev_name = cache_device_name(cache);
1009
1010         if (get_cache_mode(cache) >= CM_READ_ONLY)
1011                 return;
1012
1013         DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
1014         if (dm_cache_metadata_abort(cache->cmd)) {
1015                 DMERR("%s: failed to abort metadata transaction", dev_name);
1016                 set_cache_mode(cache, CM_FAIL);
1017         }
1018
1019         if (dm_cache_metadata_set_needs_check(cache->cmd)) {
1020                 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
1021                 set_cache_mode(cache, CM_FAIL);
1022         }
1023 }
1024
1025 static void metadata_operation_failed(struct cache *cache, const char *op, int r)
1026 {
1027         DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
1028                     cache_device_name(cache), op, r);
1029         abort_transaction(cache);
1030         set_cache_mode(cache, CM_READ_ONLY);
1031 }
1032
1033 /*----------------------------------------------------------------*/
1034
1035 static void load_stats(struct cache *cache)
1036 {
1037         struct dm_cache_statistics stats;
1038
1039         dm_cache_metadata_get_stats(cache->cmd, &stats);
1040         atomic_set(&cache->stats.read_hit, stats.read_hits);
1041         atomic_set(&cache->stats.read_miss, stats.read_misses);
1042         atomic_set(&cache->stats.write_hit, stats.write_hits);
1043         atomic_set(&cache->stats.write_miss, stats.write_misses);
1044 }
1045
1046 static void save_stats(struct cache *cache)
1047 {
1048         struct dm_cache_statistics stats;
1049
1050         if (get_cache_mode(cache) >= CM_READ_ONLY)
1051                 return;
1052
1053         stats.read_hits = atomic_read(&cache->stats.read_hit);
1054         stats.read_misses = atomic_read(&cache->stats.read_miss);
1055         stats.write_hits = atomic_read(&cache->stats.write_hit);
1056         stats.write_misses = atomic_read(&cache->stats.write_miss);
1057
1058         dm_cache_metadata_set_stats(cache->cmd, &stats);
1059 }
1060
1061 static void update_stats(struct cache_stats *stats, enum policy_operation op)
1062 {
1063         switch (op) {
1064         case POLICY_PROMOTE:
1065                 atomic_inc(&stats->promotion);
1066                 break;
1067
1068         case POLICY_DEMOTE:
1069                 atomic_inc(&stats->demotion);
1070                 break;
1071
1072         case POLICY_WRITEBACK:
1073                 atomic_inc(&stats->writeback);
1074                 break;
1075         }
1076 }
1077
1078 /*----------------------------------------------------------------
1079  * Migration processing
1080  *
1081  * Migration covers moving data from the origin device to the cache, or
1082  * vice versa.
1083  *--------------------------------------------------------------*/
1084
1085 static void inc_io_migrations(struct cache *cache)
1086 {
1087         atomic_inc(&cache->nr_io_migrations);
1088 }
1089
1090 static void dec_io_migrations(struct cache *cache)
1091 {
1092         atomic_dec(&cache->nr_io_migrations);
1093 }
1094
1095 static bool discard_or_flush(struct bio *bio)
1096 {
1097         return bio_op(bio) == REQ_OP_DISCARD || op_is_flush(bio->bi_opf);
1098 }
1099
1100 static void calc_discard_block_range(struct cache *cache, struct bio *bio,
1101                                      dm_dblock_t *b, dm_dblock_t *e)
1102 {
1103         sector_t sb = bio->bi_iter.bi_sector;
1104         sector_t se = bio_end_sector(bio);
1105
1106         *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
1107
1108         if (se - sb < cache->discard_block_size)
1109                 *e = *b;
1110         else
1111                 *e = to_dblock(block_div(se, cache->discard_block_size));
1112 }
1113
1114 /*----------------------------------------------------------------*/
1115
1116 static void prevent_background_work(struct cache *cache)
1117 {
1118         lockdep_off();
1119         down_write(&cache->background_work_lock);
1120         lockdep_on();
1121 }
1122
1123 static void allow_background_work(struct cache *cache)
1124 {
1125         lockdep_off();
1126         up_write(&cache->background_work_lock);
1127         lockdep_on();
1128 }
1129
1130 static bool background_work_begin(struct cache *cache)
1131 {
1132         bool r;
1133
1134         lockdep_off();
1135         r = down_read_trylock(&cache->background_work_lock);
1136         lockdep_on();
1137
1138         return r;
1139 }
1140
1141 static void background_work_end(struct cache *cache)
1142 {
1143         lockdep_off();
1144         up_read(&cache->background_work_lock);
1145         lockdep_on();
1146 }
1147
1148 /*----------------------------------------------------------------*/
1149
1150 static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1151 {
1152         return (bio_data_dir(bio) == WRITE) &&
1153                 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
1154 }
1155
1156 static bool optimisable_bio(struct cache *cache, struct bio *bio, dm_oblock_t block)
1157 {
1158         return writeback_mode(cache) &&
1159                 (is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio));
1160 }
1161
1162 static void quiesce(struct dm_cache_migration *mg,
1163                     void (*continuation)(struct work_struct *))
1164 {
1165         init_continuation(&mg->k, continuation);
1166         dm_cell_quiesce_v2(mg->cache->prison, mg->cell, &mg->k.ws);
1167 }
1168
1169 static struct dm_cache_migration *ws_to_mg(struct work_struct *ws)
1170 {
1171         struct continuation *k = container_of(ws, struct continuation, ws);
1172         return container_of(k, struct dm_cache_migration, k);
1173 }
1174
1175 static void copy_complete(int read_err, unsigned long write_err, void *context)
1176 {
1177         struct dm_cache_migration *mg = container_of(context, struct dm_cache_migration, k);
1178
1179         if (read_err || write_err)
1180                 mg->k.input = BLK_STS_IOERR;
1181
1182         queue_continuation(mg->cache->wq, &mg->k);
1183 }
1184
1185 static void copy(struct dm_cache_migration *mg, bool promote)
1186 {
1187         struct dm_io_region o_region, c_region;
1188         struct cache *cache = mg->cache;
1189
1190         o_region.bdev = cache->origin_dev->bdev;
1191         o_region.sector = from_oblock(mg->op->oblock) * cache->sectors_per_block;
1192         o_region.count = cache->sectors_per_block;
1193
1194         c_region.bdev = cache->cache_dev->bdev;
1195         c_region.sector = from_cblock(mg->op->cblock) * cache->sectors_per_block;
1196         c_region.count = cache->sectors_per_block;
1197
1198         if (promote)
1199                 dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, &mg->k);
1200         else
1201                 dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, &mg->k);
1202 }
1203
1204 static void bio_drop_shared_lock(struct cache *cache, struct bio *bio)
1205 {
1206         struct per_bio_data *pb = get_per_bio_data(bio);
1207
1208         if (pb->cell && dm_cell_put_v2(cache->prison, pb->cell))
1209                 free_prison_cell(cache, pb->cell);
1210         pb->cell = NULL;
1211 }
1212
1213 static void overwrite_endio(struct bio *bio)
1214 {
1215         struct dm_cache_migration *mg = bio->bi_private;
1216         struct cache *cache = mg->cache;
1217         struct per_bio_data *pb = get_per_bio_data(bio);
1218
1219         dm_unhook_bio(&pb->hook_info, bio);
1220
1221         if (bio->bi_status)
1222                 mg->k.input = bio->bi_status;
1223
1224         queue_continuation(cache->wq, &mg->k);
1225 }
1226
1227 static void overwrite(struct dm_cache_migration *mg,
1228                       void (*continuation)(struct work_struct *))
1229 {
1230         struct bio *bio = mg->overwrite_bio;
1231         struct per_bio_data *pb = get_per_bio_data(bio);
1232
1233         dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1234
1235         /*
1236          * The overwrite bio is part of the copy operation, as such it does
1237          * not set/clear discard or dirty flags.
1238          */
1239         if (mg->op->op == POLICY_PROMOTE)
1240                 remap_to_cache(mg->cache, bio, mg->op->cblock);
1241         else
1242                 remap_to_origin(mg->cache, bio);
1243
1244         init_continuation(&mg->k, continuation);
1245         accounted_request(mg->cache, bio);
1246 }
1247
1248 /*
1249  * Migration steps:
1250  *
1251  * 1) exclusive lock preventing WRITEs
1252  * 2) quiesce
1253  * 3) copy or issue overwrite bio
1254  * 4) upgrade to exclusive lock preventing READs and WRITEs
1255  * 5) quiesce
1256  * 6) update metadata and commit
1257  * 7) unlock
1258  */
1259 static void mg_complete(struct dm_cache_migration *mg, bool success)
1260 {
1261         struct bio_list bios;
1262         struct cache *cache = mg->cache;
1263         struct policy_work *op = mg->op;
1264         dm_cblock_t cblock = op->cblock;
1265
1266         if (success)
1267                 update_stats(&cache->stats, op->op);
1268
1269         switch (op->op) {
1270         case POLICY_PROMOTE:
1271                 clear_discard(cache, oblock_to_dblock(cache, op->oblock));
1272                 policy_complete_background_work(cache->policy, op, success);
1273
1274                 if (mg->overwrite_bio) {
1275                         if (success)
1276                                 force_set_dirty(cache, cblock);
1277                         else if (mg->k.input)
1278                                 mg->overwrite_bio->bi_status = mg->k.input;
1279                         else
1280                                 mg->overwrite_bio->bi_status = BLK_STS_IOERR;
1281                         bio_endio(mg->overwrite_bio);
1282                 } else {
1283                         if (success)
1284                                 force_clear_dirty(cache, cblock);
1285                         dec_io_migrations(cache);
1286                 }
1287                 break;
1288
1289         case POLICY_DEMOTE:
1290                 /*
1291                  * We clear dirty here to update the nr_dirty counter.
1292                  */
1293                 if (success)
1294                         force_clear_dirty(cache, cblock);
1295                 policy_complete_background_work(cache->policy, op, success);
1296                 dec_io_migrations(cache);
1297                 break;
1298
1299         case POLICY_WRITEBACK:
1300                 if (success)
1301                         force_clear_dirty(cache, cblock);
1302                 policy_complete_background_work(cache->policy, op, success);
1303                 dec_io_migrations(cache);
1304                 break;
1305         }
1306
1307         bio_list_init(&bios);
1308         if (mg->cell) {
1309                 if (dm_cell_unlock_v2(cache->prison, mg->cell, &bios))
1310                         free_prison_cell(cache, mg->cell);
1311         }
1312
1313         free_migration(mg);
1314         defer_bios(cache, &bios);
1315         wake_migration_worker(cache);
1316
1317         background_work_end(cache);
1318 }
1319
1320 static void mg_success(struct work_struct *ws)
1321 {
1322         struct dm_cache_migration *mg = ws_to_mg(ws);
1323         mg_complete(mg, mg->k.input == 0);
1324 }
1325
1326 static void mg_update_metadata(struct work_struct *ws)
1327 {
1328         int r;
1329         struct dm_cache_migration *mg = ws_to_mg(ws);
1330         struct cache *cache = mg->cache;
1331         struct policy_work *op = mg->op;
1332
1333         switch (op->op) {
1334         case POLICY_PROMOTE:
1335                 r = dm_cache_insert_mapping(cache->cmd, op->cblock, op->oblock);
1336                 if (r) {
1337                         DMERR_LIMIT("%s: migration failed; couldn't insert mapping",
1338                                     cache_device_name(cache));
1339                         metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
1340
1341                         mg_complete(mg, false);
1342                         return;
1343                 }
1344                 mg_complete(mg, true);
1345                 break;
1346
1347         case POLICY_DEMOTE:
1348                 r = dm_cache_remove_mapping(cache->cmd, op->cblock);
1349                 if (r) {
1350                         DMERR_LIMIT("%s: migration failed; couldn't update on disk metadata",
1351                                     cache_device_name(cache));
1352                         metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
1353
1354                         mg_complete(mg, false);
1355                         return;
1356                 }
1357
1358                 /*
1359                  * It would be nice if we only had to commit when a REQ_FLUSH
1360                  * comes through.  But there's one scenario that we have to
1361                  * look out for:
1362                  *
1363                  * - vblock x in a cache block
1364                  * - domotion occurs
1365                  * - cache block gets reallocated and over written
1366                  * - crash
1367                  *
1368                  * When we recover, because there was no commit the cache will
1369                  * rollback to having the data for vblock x in the cache block.
1370                  * But the cache block has since been overwritten, so it'll end
1371                  * up pointing to data that was never in 'x' during the history
1372                  * of the device.
1373                  *
1374                  * To avoid this issue we require a commit as part of the
1375                  * demotion operation.
1376                  */
1377                 init_continuation(&mg->k, mg_success);
1378                 continue_after_commit(&cache->committer, &mg->k);
1379                 schedule_commit(&cache->committer);
1380                 break;
1381
1382         case POLICY_WRITEBACK:
1383                 mg_complete(mg, true);
1384                 break;
1385         }
1386 }
1387
1388 static void mg_update_metadata_after_copy(struct work_struct *ws)
1389 {
1390         struct dm_cache_migration *mg = ws_to_mg(ws);
1391
1392         /*
1393          * Did the copy succeed?
1394          */
1395         if (mg->k.input)
1396                 mg_complete(mg, false);
1397         else
1398                 mg_update_metadata(ws);
1399 }
1400
1401 static void mg_upgrade_lock(struct work_struct *ws)
1402 {
1403         int r;
1404         struct dm_cache_migration *mg = ws_to_mg(ws);
1405
1406         /*
1407          * Did the copy succeed?
1408          */
1409         if (mg->k.input)
1410                 mg_complete(mg, false);
1411
1412         else {
1413                 /*
1414                  * Now we want the lock to prevent both reads and writes.
1415                  */
1416                 r = dm_cell_lock_promote_v2(mg->cache->prison, mg->cell,
1417                                             READ_WRITE_LOCK_LEVEL);
1418                 if (r < 0)
1419                         mg_complete(mg, false);
1420
1421                 else if (r)
1422                         quiesce(mg, mg_update_metadata);
1423
1424                 else
1425                         mg_update_metadata(ws);
1426         }
1427 }
1428
1429 static void mg_full_copy(struct work_struct *ws)
1430 {
1431         struct dm_cache_migration *mg = ws_to_mg(ws);
1432         struct cache *cache = mg->cache;
1433         struct policy_work *op = mg->op;
1434         bool is_policy_promote = (op->op == POLICY_PROMOTE);
1435
1436         if ((!is_policy_promote && !is_dirty(cache, op->cblock)) ||
1437             is_discarded_oblock(cache, op->oblock)) {
1438                 mg_upgrade_lock(ws);
1439                 return;
1440         }
1441
1442         init_continuation(&mg->k, mg_upgrade_lock);
1443         copy(mg, is_policy_promote);
1444 }
1445
1446 static void mg_copy(struct work_struct *ws)
1447 {
1448         struct dm_cache_migration *mg = ws_to_mg(ws);
1449
1450         if (mg->overwrite_bio) {
1451                 /*
1452                  * No exclusive lock was held when we last checked if the bio
1453                  * was optimisable.  So we have to check again in case things
1454                  * have changed (eg, the block may no longer be discarded).
1455                  */
1456                 if (!optimisable_bio(mg->cache, mg->overwrite_bio, mg->op->oblock)) {
1457                         /*
1458                          * Fallback to a real full copy after doing some tidying up.
1459                          */
1460                         bool rb = bio_detain_shared(mg->cache, mg->op->oblock, mg->overwrite_bio);
1461                         BUG_ON(rb); /* An exclussive lock must _not_ be held for this block */
1462                         mg->overwrite_bio = NULL;
1463                         inc_io_migrations(mg->cache);
1464                         mg_full_copy(ws);
1465                         return;
1466                 }
1467
1468                 /*
1469                  * It's safe to do this here, even though it's new data
1470                  * because all IO has been locked out of the block.
1471                  *
1472                  * mg_lock_writes() already took READ_WRITE_LOCK_LEVEL
1473                  * so _not_ using mg_upgrade_lock() as continutation.
1474                  */
1475                 overwrite(mg, mg_update_metadata_after_copy);
1476
1477         } else
1478                 mg_full_copy(ws);
1479 }
1480
1481 static int mg_lock_writes(struct dm_cache_migration *mg)
1482 {
1483         int r;
1484         struct dm_cell_key_v2 key;
1485         struct cache *cache = mg->cache;
1486         struct dm_bio_prison_cell_v2 *prealloc;
1487
1488         prealloc = alloc_prison_cell(cache);
1489
1490         /*
1491          * Prevent writes to the block, but allow reads to continue.
1492          * Unless we're using an overwrite bio, in which case we lock
1493          * everything.
1494          */
1495         build_key(mg->op->oblock, oblock_succ(mg->op->oblock), &key);
1496         r = dm_cell_lock_v2(cache->prison, &key,
1497                             mg->overwrite_bio ?  READ_WRITE_LOCK_LEVEL : WRITE_LOCK_LEVEL,
1498                             prealloc, &mg->cell);
1499         if (r < 0) {
1500                 free_prison_cell(cache, prealloc);
1501                 mg_complete(mg, false);
1502                 return r;
1503         }
1504
1505         if (mg->cell != prealloc)
1506                 free_prison_cell(cache, prealloc);
1507
1508         if (r == 0)
1509                 mg_copy(&mg->k.ws);
1510         else
1511                 quiesce(mg, mg_copy);
1512
1513         return 0;
1514 }
1515
1516 static int mg_start(struct cache *cache, struct policy_work *op, struct bio *bio)
1517 {
1518         struct dm_cache_migration *mg;
1519
1520         if (!background_work_begin(cache)) {
1521                 policy_complete_background_work(cache->policy, op, false);
1522                 return -EPERM;
1523         }
1524
1525         mg = alloc_migration(cache);
1526
1527         mg->op = op;
1528         mg->overwrite_bio = bio;
1529
1530         if (!bio)
1531                 inc_io_migrations(cache);
1532
1533         return mg_lock_writes(mg);
1534 }
1535
1536 /*----------------------------------------------------------------
1537  * invalidation processing
1538  *--------------------------------------------------------------*/
1539
1540 static void invalidate_complete(struct dm_cache_migration *mg, bool success)
1541 {
1542         struct bio_list bios;
1543         struct cache *cache = mg->cache;
1544
1545         bio_list_init(&bios);
1546         if (dm_cell_unlock_v2(cache->prison, mg->cell, &bios))
1547                 free_prison_cell(cache, mg->cell);
1548
1549         if (!success && mg->overwrite_bio)
1550                 bio_io_error(mg->overwrite_bio);
1551
1552         free_migration(mg);
1553         defer_bios(cache, &bios);
1554
1555         background_work_end(cache);
1556 }
1557
1558 static void invalidate_completed(struct work_struct *ws)
1559 {
1560         struct dm_cache_migration *mg = ws_to_mg(ws);
1561         invalidate_complete(mg, !mg->k.input);
1562 }
1563
1564 static int invalidate_cblock(struct cache *cache, dm_cblock_t cblock)
1565 {
1566         int r = policy_invalidate_mapping(cache->policy, cblock);
1567         if (!r) {
1568                 r = dm_cache_remove_mapping(cache->cmd, cblock);
1569                 if (r) {
1570                         DMERR_LIMIT("%s: invalidation failed; couldn't update on disk metadata",
1571                                     cache_device_name(cache));
1572                         metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
1573                 }
1574
1575         } else if (r == -ENODATA) {
1576                 /*
1577                  * Harmless, already unmapped.
1578                  */
1579                 r = 0;
1580
1581         } else
1582                 DMERR("%s: policy_invalidate_mapping failed", cache_device_name(cache));
1583
1584         return r;
1585 }
1586
1587 static void invalidate_remove(struct work_struct *ws)
1588 {
1589         int r;
1590         struct dm_cache_migration *mg = ws_to_mg(ws);
1591         struct cache *cache = mg->cache;
1592
1593         r = invalidate_cblock(cache, mg->invalidate_cblock);
1594         if (r) {
1595                 invalidate_complete(mg, false);
1596                 return;
1597         }
1598
1599         init_continuation(&mg->k, invalidate_completed);
1600         continue_after_commit(&cache->committer, &mg->k);
1601         remap_to_origin_clear_discard(cache, mg->overwrite_bio, mg->invalidate_oblock);
1602         mg->overwrite_bio = NULL;
1603         schedule_commit(&cache->committer);
1604 }
1605
1606 static int invalidate_lock(struct dm_cache_migration *mg)
1607 {
1608         int r;
1609         struct dm_cell_key_v2 key;
1610         struct cache *cache = mg->cache;
1611         struct dm_bio_prison_cell_v2 *prealloc;
1612
1613         prealloc = alloc_prison_cell(cache);
1614
1615         build_key(mg->invalidate_oblock, oblock_succ(mg->invalidate_oblock), &key);
1616         r = dm_cell_lock_v2(cache->prison, &key,
1617                             READ_WRITE_LOCK_LEVEL, prealloc, &mg->cell);
1618         if (r < 0) {
1619                 free_prison_cell(cache, prealloc);
1620                 invalidate_complete(mg, false);
1621                 return r;
1622         }
1623
1624         if (mg->cell != prealloc)
1625                 free_prison_cell(cache, prealloc);
1626
1627         if (r)
1628                 quiesce(mg, invalidate_remove);
1629
1630         else {
1631                 /*
1632                  * We can't call invalidate_remove() directly here because we
1633                  * might still be in request context.
1634                  */
1635                 init_continuation(&mg->k, invalidate_remove);
1636                 queue_work(cache->wq, &mg->k.ws);
1637         }
1638
1639         return 0;
1640 }
1641
1642 static int invalidate_start(struct cache *cache, dm_cblock_t cblock,
1643                             dm_oblock_t oblock, struct bio *bio)
1644 {
1645         struct dm_cache_migration *mg;
1646
1647         if (!background_work_begin(cache))
1648                 return -EPERM;
1649
1650         mg = alloc_migration(cache);
1651
1652         mg->overwrite_bio = bio;
1653         mg->invalidate_cblock = cblock;
1654         mg->invalidate_oblock = oblock;
1655
1656         return invalidate_lock(mg);
1657 }
1658
1659 /*----------------------------------------------------------------
1660  * bio processing
1661  *--------------------------------------------------------------*/
1662
1663 enum busy {
1664         IDLE,
1665         BUSY
1666 };
1667
1668 static enum busy spare_migration_bandwidth(struct cache *cache)
1669 {
1670         bool idle = iot_idle_for(&cache->tracker, HZ);
1671         sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
1672                 cache->sectors_per_block;
1673
1674         if (idle && current_volume <= cache->migration_threshold)
1675                 return IDLE;
1676         else
1677                 return BUSY;
1678 }
1679
1680 static void inc_hit_counter(struct cache *cache, struct bio *bio)
1681 {
1682         atomic_inc(bio_data_dir(bio) == READ ?
1683                    &cache->stats.read_hit : &cache->stats.write_hit);
1684 }
1685
1686 static void inc_miss_counter(struct cache *cache, struct bio *bio)
1687 {
1688         atomic_inc(bio_data_dir(bio) == READ ?
1689                    &cache->stats.read_miss : &cache->stats.write_miss);
1690 }
1691
1692 /*----------------------------------------------------------------*/
1693
1694 static int map_bio(struct cache *cache, struct bio *bio, dm_oblock_t block,
1695                    bool *commit_needed)
1696 {
1697         int r, data_dir;
1698         bool rb, background_queued;
1699         dm_cblock_t cblock;
1700
1701         *commit_needed = false;
1702
1703         rb = bio_detain_shared(cache, block, bio);
1704         if (!rb) {
1705                 /*
1706                  * An exclusive lock is held for this block, so we have to
1707                  * wait.  We set the commit_needed flag so the current
1708                  * transaction will be committed asap, allowing this lock
1709                  * to be dropped.
1710                  */
1711                 *commit_needed = true;
1712                 return DM_MAPIO_SUBMITTED;
1713         }
1714
1715         data_dir = bio_data_dir(bio);
1716
1717         if (optimisable_bio(cache, bio, block)) {
1718                 struct policy_work *op = NULL;
1719
1720                 r = policy_lookup_with_work(cache->policy, block, &cblock, data_dir, true, &op);
1721                 if (unlikely(r && r != -ENOENT)) {
1722                         DMERR_LIMIT("%s: policy_lookup_with_work() failed with r = %d",
1723                                     cache_device_name(cache), r);
1724                         bio_io_error(bio);
1725                         return DM_MAPIO_SUBMITTED;
1726                 }
1727
1728                 if (r == -ENOENT && op) {
1729                         bio_drop_shared_lock(cache, bio);
1730                         BUG_ON(op->op != POLICY_PROMOTE);
1731                         mg_start(cache, op, bio);
1732                         return DM_MAPIO_SUBMITTED;
1733                 }
1734         } else {
1735                 r = policy_lookup(cache->policy, block, &cblock, data_dir, false, &background_queued);
1736                 if (unlikely(r && r != -ENOENT)) {
1737                         DMERR_LIMIT("%s: policy_lookup() failed with r = %d",
1738                                     cache_device_name(cache), r);
1739                         bio_io_error(bio);
1740                         return DM_MAPIO_SUBMITTED;
1741                 }
1742
1743                 if (background_queued)
1744                         wake_migration_worker(cache);
1745         }
1746
1747         if (r == -ENOENT) {
1748                 struct per_bio_data *pb = get_per_bio_data(bio);
1749
1750                 /*
1751                  * Miss.
1752                  */
1753                 inc_miss_counter(cache, bio);
1754                 if (pb->req_nr == 0) {
1755                         accounted_begin(cache, bio);
1756                         remap_to_origin_clear_discard(cache, bio, block);
1757                 } else {
1758                         /*
1759                          * This is a duplicate writethrough io that is no
1760                          * longer needed because the block has been demoted.
1761                          */
1762                         bio_endio(bio);
1763                         return DM_MAPIO_SUBMITTED;
1764                 }
1765         } else {
1766                 /*
1767                  * Hit.
1768                  */
1769                 inc_hit_counter(cache, bio);
1770
1771                 /*
1772                  * Passthrough always maps to the origin, invalidating any
1773                  * cache blocks that are written to.
1774                  */
1775                 if (passthrough_mode(cache)) {
1776                         if (bio_data_dir(bio) == WRITE) {
1777                                 bio_drop_shared_lock(cache, bio);
1778                                 atomic_inc(&cache->stats.demotion);
1779                                 invalidate_start(cache, cblock, block, bio);
1780                         } else
1781                                 remap_to_origin_clear_discard(cache, bio, block);
1782                 } else {
1783                         if (bio_data_dir(bio) == WRITE && writethrough_mode(cache) &&
1784                             !is_dirty(cache, cblock)) {
1785                                 remap_to_origin_and_cache(cache, bio, block, cblock);
1786                                 accounted_begin(cache, bio);
1787                         } else
1788                                 remap_to_cache_dirty(cache, bio, block, cblock);
1789                 }
1790         }
1791
1792         /*
1793          * dm core turns FUA requests into a separate payload and FLUSH req.
1794          */
1795         if (bio->bi_opf & REQ_FUA) {
1796                 /*
1797                  * issue_after_commit will call accounted_begin a second time.  So
1798                  * we call accounted_complete() to avoid double accounting.
1799                  */
1800                 accounted_complete(cache, bio);
1801                 issue_after_commit(&cache->committer, bio);
1802                 *commit_needed = true;
1803                 return DM_MAPIO_SUBMITTED;
1804         }
1805
1806         return DM_MAPIO_REMAPPED;
1807 }
1808
1809 static bool process_bio(struct cache *cache, struct bio *bio)
1810 {
1811         bool commit_needed;
1812
1813         if (map_bio(cache, bio, get_bio_block(cache, bio), &commit_needed) == DM_MAPIO_REMAPPED)
1814                 generic_make_request(bio);
1815
1816         return commit_needed;
1817 }
1818
1819 /*
1820  * A non-zero return indicates read_only or fail_io mode.
1821  */
1822 static int commit(struct cache *cache, bool clean_shutdown)
1823 {
1824         int r;
1825
1826         if (get_cache_mode(cache) >= CM_READ_ONLY)
1827                 return -EINVAL;
1828
1829         atomic_inc(&cache->stats.commit_count);
1830         r = dm_cache_commit(cache->cmd, clean_shutdown);
1831         if (r)
1832                 metadata_operation_failed(cache, "dm_cache_commit", r);
1833
1834         return r;
1835 }
1836
1837 /*
1838  * Used by the batcher.
1839  */
1840 static blk_status_t commit_op(void *context)
1841 {
1842         struct cache *cache = context;
1843
1844         if (dm_cache_changed_this_transaction(cache->cmd))
1845                 return errno_to_blk_status(commit(cache, false));
1846
1847         return 0;
1848 }
1849
1850 /*----------------------------------------------------------------*/
1851
1852 static bool process_flush_bio(struct cache *cache, struct bio *bio)
1853 {
1854         struct per_bio_data *pb = get_per_bio_data(bio);
1855
1856         if (!pb->req_nr)
1857                 remap_to_origin(cache, bio);
1858         else
1859                 remap_to_cache(cache, bio, 0);
1860
1861         issue_after_commit(&cache->committer, bio);
1862         return true;
1863 }
1864
1865 static bool process_discard_bio(struct cache *cache, struct bio *bio)
1866 {
1867         dm_dblock_t b, e;
1868
1869         // FIXME: do we need to lock the region?  Or can we just assume the
1870         // user wont be so foolish as to issue discard concurrently with
1871         // other IO?
1872         calc_discard_block_range(cache, bio, &b, &e);
1873         while (b != e) {
1874                 set_discard(cache, b);
1875                 b = to_dblock(from_dblock(b) + 1);
1876         }
1877
1878         bio_endio(bio);
1879
1880         return false;
1881 }
1882
1883 static void process_deferred_bios(struct work_struct *ws)
1884 {
1885         struct cache *cache = container_of(ws, struct cache, deferred_bio_worker);
1886
1887         unsigned long flags;
1888         bool commit_needed = false;
1889         struct bio_list bios;
1890         struct bio *bio;
1891
1892         bio_list_init(&bios);
1893
1894         spin_lock_irqsave(&cache->lock, flags);
1895         bio_list_merge(&bios, &cache->deferred_bios);
1896         bio_list_init(&cache->deferred_bios);
1897         spin_unlock_irqrestore(&cache->lock, flags);
1898
1899         while ((bio = bio_list_pop(&bios))) {
1900                 if (bio->bi_opf & REQ_PREFLUSH)
1901                         commit_needed = process_flush_bio(cache, bio) || commit_needed;
1902
1903                 else if (bio_op(bio) == REQ_OP_DISCARD)
1904                         commit_needed = process_discard_bio(cache, bio) || commit_needed;
1905
1906                 else
1907                         commit_needed = process_bio(cache, bio) || commit_needed;
1908                 cond_resched();
1909         }
1910
1911         if (commit_needed)
1912                 schedule_commit(&cache->committer);
1913 }
1914
1915 /*----------------------------------------------------------------
1916  * Main worker loop
1917  *--------------------------------------------------------------*/
1918
1919 static void requeue_deferred_bios(struct cache *cache)
1920 {
1921         struct bio *bio;
1922         struct bio_list bios;
1923
1924         bio_list_init(&bios);
1925         bio_list_merge(&bios, &cache->deferred_bios);
1926         bio_list_init(&cache->deferred_bios);
1927
1928         while ((bio = bio_list_pop(&bios))) {
1929                 bio->bi_status = BLK_STS_DM_REQUEUE;
1930                 bio_endio(bio);
1931                 cond_resched();
1932         }
1933 }
1934
1935 /*
1936  * We want to commit periodically so that not too much
1937  * unwritten metadata builds up.
1938  */
1939 static void do_waker(struct work_struct *ws)
1940 {
1941         struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
1942
1943         policy_tick(cache->policy, true);
1944         wake_migration_worker(cache);
1945         schedule_commit(&cache->committer);
1946         queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1947 }
1948
1949 static void check_migrations(struct work_struct *ws)
1950 {
1951         int r;
1952         struct policy_work *op;
1953         struct cache *cache = container_of(ws, struct cache, migration_worker);
1954         enum busy b;
1955
1956         for (;;) {
1957                 b = spare_migration_bandwidth(cache);
1958
1959                 r = policy_get_background_work(cache->policy, b == IDLE, &op);
1960                 if (r == -ENODATA)
1961                         break;
1962
1963                 if (r) {
1964                         DMERR_LIMIT("%s: policy_background_work failed",
1965                                     cache_device_name(cache));
1966                         break;
1967                 }
1968
1969                 r = mg_start(cache, op, NULL);
1970                 if (r)
1971                         break;
1972
1973                 cond_resched();
1974         }
1975 }
1976
1977 /*----------------------------------------------------------------
1978  * Target methods
1979  *--------------------------------------------------------------*/
1980
1981 /*
1982  * This function gets called on the error paths of the constructor, so we
1983  * have to cope with a partially initialised struct.
1984  */
1985 static void destroy(struct cache *cache)
1986 {
1987         unsigned i;
1988
1989         mempool_exit(&cache->migration_pool);
1990
1991         if (cache->prison)
1992                 dm_bio_prison_destroy_v2(cache->prison);
1993
1994         cancel_delayed_work_sync(&cache->waker);
1995         if (cache->wq)
1996                 destroy_workqueue(cache->wq);
1997
1998         if (cache->dirty_bitset)
1999                 free_bitset(cache->dirty_bitset);
2000
2001         if (cache->discard_bitset)
2002                 free_bitset(cache->discard_bitset);
2003
2004         if (cache->copier)
2005                 dm_kcopyd_client_destroy(cache->copier);
2006
2007         if (cache->cmd)
2008                 dm_cache_metadata_close(cache->cmd);
2009
2010         if (cache->metadata_dev)
2011                 dm_put_device(cache->ti, cache->metadata_dev);
2012
2013         if (cache->origin_dev)
2014                 dm_put_device(cache->ti, cache->origin_dev);
2015
2016         if (cache->cache_dev)
2017                 dm_put_device(cache->ti, cache->cache_dev);
2018
2019         if (cache->policy)
2020                 dm_cache_policy_destroy(cache->policy);
2021
2022         for (i = 0; i < cache->nr_ctr_args ; i++)
2023                 kfree(cache->ctr_args[i]);
2024         kfree(cache->ctr_args);
2025
2026         bioset_exit(&cache->bs);
2027
2028         kfree(cache);
2029 }
2030
2031 static void cache_dtr(struct dm_target *ti)
2032 {
2033         struct cache *cache = ti->private;
2034
2035         destroy(cache);
2036 }
2037
2038 static sector_t get_dev_size(struct dm_dev *dev)
2039 {
2040         return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
2041 }
2042
2043 /*----------------------------------------------------------------*/
2044
2045 /*
2046  * Construct a cache device mapping.
2047  *
2048  * cache <metadata dev> <cache dev> <origin dev> <block size>
2049  *       <#feature args> [<feature arg>]*
2050  *       <policy> <#policy args> [<policy arg>]*
2051  *
2052  * metadata dev    : fast device holding the persistent metadata
2053  * cache dev       : fast device holding cached data blocks
2054  * origin dev      : slow device holding original data blocks
2055  * block size      : cache unit size in sectors
2056  *
2057  * #feature args   : number of feature arguments passed
2058  * feature args    : writethrough.  (The default is writeback.)
2059  *
2060  * policy          : the replacement policy to use
2061  * #policy args    : an even number of policy arguments corresponding
2062  *                   to key/value pairs passed to the policy
2063  * policy args     : key/value pairs passed to the policy
2064  *                   E.g. 'sequential_threshold 1024'
2065  *                   See cache-policies.txt for details.
2066  *
2067  * Optional feature arguments are:
2068  *   writethrough  : write through caching that prohibits cache block
2069  *                   content from being different from origin block content.
2070  *                   Without this argument, the default behaviour is to write
2071  *                   back cache block contents later for performance reasons,
2072  *                   so they may differ from the corresponding origin blocks.
2073  */
2074 struct cache_args {
2075         struct dm_target *ti;
2076
2077         struct dm_dev *metadata_dev;
2078
2079         struct dm_dev *cache_dev;
2080         sector_t cache_sectors;
2081
2082         struct dm_dev *origin_dev;
2083         sector_t origin_sectors;
2084
2085         uint32_t block_size;
2086
2087         const char *policy_name;
2088         int policy_argc;
2089         const char **policy_argv;
2090
2091         struct cache_features features;
2092 };
2093
2094 static void destroy_cache_args(struct cache_args *ca)
2095 {
2096         if (ca->metadata_dev)
2097                 dm_put_device(ca->ti, ca->metadata_dev);
2098
2099         if (ca->cache_dev)
2100                 dm_put_device(ca->ti, ca->cache_dev);
2101
2102         if (ca->origin_dev)
2103                 dm_put_device(ca->ti, ca->origin_dev);
2104
2105         kfree(ca);
2106 }
2107
2108 static bool at_least_one_arg(struct dm_arg_set *as, char **error)
2109 {
2110         if (!as->argc) {
2111                 *error = "Insufficient args";
2112                 return false;
2113         }
2114
2115         return true;
2116 }
2117
2118 static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
2119                               char **error)
2120 {
2121         int r;
2122         sector_t metadata_dev_size;
2123         char b[BDEVNAME_SIZE];
2124
2125         if (!at_least_one_arg(as, error))
2126                 return -EINVAL;
2127
2128         r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2129                           &ca->metadata_dev);
2130         if (r) {
2131                 *error = "Error opening metadata device";
2132                 return r;
2133         }
2134
2135         metadata_dev_size = get_dev_size(ca->metadata_dev);
2136         if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
2137                 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2138                        bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
2139
2140         return 0;
2141 }
2142
2143 static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
2144                            char **error)
2145 {
2146         int r;
2147
2148         if (!at_least_one_arg(as, error))
2149                 return -EINVAL;
2150
2151         r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2152                           &ca->cache_dev);
2153         if (r) {
2154                 *error = "Error opening cache device";
2155                 return r;
2156         }
2157         ca->cache_sectors = get_dev_size(ca->cache_dev);
2158
2159         return 0;
2160 }
2161
2162 static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
2163                             char **error)
2164 {
2165         int r;
2166
2167         if (!at_least_one_arg(as, error))
2168                 return -EINVAL;
2169
2170         r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2171                           &ca->origin_dev);
2172         if (r) {
2173                 *error = "Error opening origin device";
2174                 return r;
2175         }
2176
2177         ca->origin_sectors = get_dev_size(ca->origin_dev);
2178         if (ca->ti->len > ca->origin_sectors) {
2179                 *error = "Device size larger than cached device";
2180                 return -EINVAL;
2181         }
2182
2183         return 0;
2184 }
2185
2186 static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
2187                             char **error)
2188 {
2189         unsigned long block_size;
2190
2191         if (!at_least_one_arg(as, error))
2192                 return -EINVAL;
2193
2194         if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
2195             block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2196             block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2197             block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
2198                 *error = "Invalid data block size";
2199                 return -EINVAL;
2200         }
2201
2202         if (block_size > ca->cache_sectors) {
2203                 *error = "Data block size is larger than the cache device";
2204                 return -EINVAL;
2205         }
2206
2207         ca->block_size = block_size;
2208
2209         return 0;
2210 }
2211
2212 static void init_features(struct cache_features *cf)
2213 {
2214         cf->mode = CM_WRITE;
2215         cf->io_mode = CM_IO_WRITEBACK;
2216         cf->metadata_version = 1;
2217 }
2218
2219 static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2220                           char **error)
2221 {
2222         static const struct dm_arg _args[] = {
2223                 {0, 2, "Invalid number of cache feature arguments"},
2224         };
2225
2226         int r, mode_ctr = 0;
2227         unsigned argc;
2228         const char *arg;
2229         struct cache_features *cf = &ca->features;
2230
2231         init_features(cf);
2232
2233         r = dm_read_arg_group(_args, as, &argc, error);
2234         if (r)
2235                 return -EINVAL;
2236
2237         while (argc--) {
2238                 arg = dm_shift_arg(as);
2239
2240                 if (!strcasecmp(arg, "writeback")) {
2241                         cf->io_mode = CM_IO_WRITEBACK;
2242                         mode_ctr++;
2243                 }
2244
2245                 else if (!strcasecmp(arg, "writethrough")) {
2246                         cf->io_mode = CM_IO_WRITETHROUGH;
2247                         mode_ctr++;
2248                 }
2249
2250                 else if (!strcasecmp(arg, "passthrough")) {
2251                         cf->io_mode = CM_IO_PASSTHROUGH;
2252                         mode_ctr++;
2253                 }
2254
2255                 else if (!strcasecmp(arg, "metadata2"))
2256                         cf->metadata_version = 2;
2257
2258                 else {
2259                         *error = "Unrecognised cache feature requested";
2260                         return -EINVAL;
2261                 }
2262         }
2263
2264         if (mode_ctr > 1) {
2265                 *error = "Duplicate cache io_mode features requested";
2266                 return -EINVAL;
2267         }
2268
2269         return 0;
2270 }
2271
2272 static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2273                         char **error)
2274 {
2275         static const struct dm_arg _args[] = {
2276                 {0, 1024, "Invalid number of policy arguments"},
2277         };
2278
2279         int r;
2280
2281         if (!at_least_one_arg(as, error))
2282                 return -EINVAL;
2283
2284         ca->policy_name = dm_shift_arg(as);
2285
2286         r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2287         if (r)
2288                 return -EINVAL;
2289
2290         ca->policy_argv = (const char **)as->argv;
2291         dm_consume_args(as, ca->policy_argc);
2292
2293         return 0;
2294 }
2295
2296 static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2297                             char **error)
2298 {
2299         int r;
2300         struct dm_arg_set as;
2301
2302         as.argc = argc;
2303         as.argv = argv;
2304
2305         r = parse_metadata_dev(ca, &as, error);
2306         if (r)
2307                 return r;
2308
2309         r = parse_cache_dev(ca, &as, error);
2310         if (r)
2311                 return r;
2312
2313         r = parse_origin_dev(ca, &as, error);
2314         if (r)
2315                 return r;
2316
2317         r = parse_block_size(ca, &as, error);
2318         if (r)
2319                 return r;
2320
2321         r = parse_features(ca, &as, error);
2322         if (r)
2323                 return r;
2324
2325         r = parse_policy(ca, &as, error);
2326         if (r)
2327                 return r;
2328
2329         return 0;
2330 }
2331
2332 /*----------------------------------------------------------------*/
2333
2334 static struct kmem_cache *migration_cache;
2335
2336 #define NOT_CORE_OPTION 1
2337
2338 static int process_config_option(struct cache *cache, const char *key, const char *value)
2339 {
2340         unsigned long tmp;
2341
2342         if (!strcasecmp(key, "migration_threshold")) {
2343                 if (kstrtoul(value, 10, &tmp))
2344                         return -EINVAL;
2345
2346                 cache->migration_threshold = tmp;
2347                 return 0;
2348         }
2349
2350         return NOT_CORE_OPTION;
2351 }
2352
2353 static int set_config_value(struct cache *cache, const char *key, const char *value)
2354 {
2355         int r = process_config_option(cache, key, value);
2356
2357         if (r == NOT_CORE_OPTION)
2358                 r = policy_set_config_value(cache->policy, key, value);
2359
2360         if (r)
2361                 DMWARN("bad config value for %s: %s", key, value);
2362
2363         return r;
2364 }
2365
2366 static int set_config_values(struct cache *cache, int argc, const char **argv)
2367 {
2368         int r = 0;
2369
2370         if (argc & 1) {
2371                 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2372                 return -EINVAL;
2373         }
2374
2375         while (argc) {
2376                 r = set_config_value(cache, argv[0], argv[1]);
2377                 if (r)
2378                         break;
2379
2380                 argc -= 2;
2381                 argv += 2;
2382         }
2383
2384         return r;
2385 }
2386
2387 static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2388                                char **error)
2389 {
2390         struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2391                                                            cache->cache_size,
2392                                                            cache->origin_sectors,
2393                                                            cache->sectors_per_block);
2394         if (IS_ERR(p)) {
2395                 *error = "Error creating cache's policy";
2396                 return PTR_ERR(p);
2397         }
2398         cache->policy = p;
2399         BUG_ON(!cache->policy);
2400
2401         return 0;
2402 }
2403
2404 /*
2405  * We want the discard block size to be at least the size of the cache
2406  * block size and have no more than 2^14 discard blocks across the origin.
2407  */
2408 #define MAX_DISCARD_BLOCKS (1 << 14)
2409
2410 static bool too_many_discard_blocks(sector_t discard_block_size,
2411                                     sector_t origin_size)
2412 {
2413         (void) sector_div(origin_size, discard_block_size);
2414
2415         return origin_size > MAX_DISCARD_BLOCKS;
2416 }
2417
2418 static sector_t calculate_discard_block_size(sector_t cache_block_size,
2419                                              sector_t origin_size)
2420 {
2421         sector_t discard_block_size = cache_block_size;
2422
2423         if (origin_size)
2424                 while (too_many_discard_blocks(discard_block_size, origin_size))
2425                         discard_block_size *= 2;
2426
2427         return discard_block_size;
2428 }
2429
2430 static void set_cache_size(struct cache *cache, dm_cblock_t size)
2431 {
2432         dm_block_t nr_blocks = from_cblock(size);
2433
2434         if (nr_blocks > (1 << 20) && cache->cache_size != size)
2435                 DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
2436                              "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
2437                              "Please consider increasing the cache block size to reduce the overall cache block count.",
2438                              (unsigned long long) nr_blocks);
2439
2440         cache->cache_size = size;
2441 }
2442
2443 static int is_congested(struct dm_dev *dev, int bdi_bits)
2444 {
2445         struct request_queue *q = bdev_get_queue(dev->bdev);
2446         return bdi_congested(q->backing_dev_info, bdi_bits);
2447 }
2448
2449 static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2450 {
2451         struct cache *cache = container_of(cb, struct cache, callbacks);
2452
2453         return is_congested(cache->origin_dev, bdi_bits) ||
2454                 is_congested(cache->cache_dev, bdi_bits);
2455 }
2456
2457 #define DEFAULT_MIGRATION_THRESHOLD 2048
2458
2459 static int cache_create(struct cache_args *ca, struct cache **result)
2460 {
2461         int r = 0;
2462         char **error = &ca->ti->error;
2463         struct cache *cache;
2464         struct dm_target *ti = ca->ti;
2465         dm_block_t origin_blocks;
2466         struct dm_cache_metadata *cmd;
2467         bool may_format = ca->features.mode == CM_WRITE;
2468
2469         cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2470         if (!cache)
2471                 return -ENOMEM;
2472
2473         cache->ti = ca->ti;
2474         ti->private = cache;
2475         ti->num_flush_bios = 2;
2476         ti->flush_supported = true;
2477
2478         ti->num_discard_bios = 1;
2479         ti->discards_supported = true;
2480         ti->split_discard_bios = false;
2481
2482         ti->per_io_data_size = sizeof(struct per_bio_data);
2483
2484         cache->features = ca->features;
2485         if (writethrough_mode(cache)) {
2486                 /* Create bioset for writethrough bios issued to origin */
2487                 r = bioset_init(&cache->bs, BIO_POOL_SIZE, 0, 0);
2488                 if (r)
2489                         goto bad;
2490         }
2491
2492         cache->callbacks.congested_fn = cache_is_congested;
2493         dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2494
2495         cache->metadata_dev = ca->metadata_dev;
2496         cache->origin_dev = ca->origin_dev;
2497         cache->cache_dev = ca->cache_dev;
2498
2499         ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2500
2501         origin_blocks = cache->origin_sectors = ca->origin_sectors;
2502         origin_blocks = block_div(origin_blocks, ca->block_size);
2503         cache->origin_blocks = to_oblock(origin_blocks);
2504
2505         cache->sectors_per_block = ca->block_size;
2506         if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2507                 r = -EINVAL;
2508                 goto bad;
2509         }
2510
2511         if (ca->block_size & (ca->block_size - 1)) {
2512                 dm_block_t cache_size = ca->cache_sectors;
2513
2514                 cache->sectors_per_block_shift = -1;
2515                 cache_size = block_div(cache_size, ca->block_size);
2516                 set_cache_size(cache, to_cblock(cache_size));
2517         } else {
2518                 cache->sectors_per_block_shift = __ffs(ca->block_size);
2519                 set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
2520         }
2521
2522         r = create_cache_policy(cache, ca, error);
2523         if (r)
2524                 goto bad;
2525
2526         cache->policy_nr_args = ca->policy_argc;
2527         cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2528
2529         r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2530         if (r) {
2531                 *error = "Error setting cache policy's config values";
2532                 goto bad;
2533         }
2534
2535         cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2536                                      ca->block_size, may_format,
2537                                      dm_cache_policy_get_hint_size(cache->policy),
2538                                      ca->features.metadata_version);
2539         if (IS_ERR(cmd)) {
2540                 *error = "Error creating metadata object";
2541                 r = PTR_ERR(cmd);
2542                 goto bad;
2543         }
2544         cache->cmd = cmd;
2545         set_cache_mode(cache, CM_WRITE);
2546         if (get_cache_mode(cache) != CM_WRITE) {
2547                 *error = "Unable to get write access to metadata, please check/repair metadata.";
2548                 r = -EINVAL;
2549                 goto bad;
2550         }
2551
2552         if (passthrough_mode(cache)) {
2553                 bool all_clean;
2554
2555                 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2556                 if (r) {
2557                         *error = "dm_cache_metadata_all_clean() failed";
2558                         goto bad;
2559                 }
2560
2561                 if (!all_clean) {
2562                         *error = "Cannot enter passthrough mode unless all blocks are clean";
2563                         r = -EINVAL;
2564                         goto bad;
2565                 }
2566
2567                 policy_allow_migrations(cache->policy, false);
2568         }
2569
2570         spin_lock_init(&cache->lock);
2571         bio_list_init(&cache->deferred_bios);
2572         atomic_set(&cache->nr_allocated_migrations, 0);
2573         atomic_set(&cache->nr_io_migrations, 0);
2574         init_waitqueue_head(&cache->migration_wait);
2575
2576         r = -ENOMEM;
2577         atomic_set(&cache->nr_dirty, 0);
2578         cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2579         if (!cache->dirty_bitset) {
2580                 *error = "could not allocate dirty bitset";
2581                 goto bad;
2582         }
2583         clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2584
2585         cache->discard_block_size =
2586                 calculate_discard_block_size(cache->sectors_per_block,
2587                                              cache->origin_sectors);
2588         cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
2589                                                               cache->discard_block_size));
2590         cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2591         if (!cache->discard_bitset) {
2592                 *error = "could not allocate discard bitset";
2593                 goto bad;
2594         }
2595         clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2596
2597         cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2598         if (IS_ERR(cache->copier)) {
2599                 *error = "could not create kcopyd client";
2600                 r = PTR_ERR(cache->copier);
2601                 goto bad;
2602         }
2603
2604         cache->wq = alloc_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM, 0);
2605         if (!cache->wq) {
2606                 *error = "could not create workqueue for metadata object";
2607                 goto bad;
2608         }
2609         INIT_WORK(&cache->deferred_bio_worker, process_deferred_bios);
2610         INIT_WORK(&cache->migration_worker, check_migrations);
2611         INIT_DELAYED_WORK(&cache->waker, do_waker);
2612
2613         cache->prison = dm_bio_prison_create_v2(cache->wq);
2614         if (!cache->prison) {
2615                 *error = "could not create bio prison";
2616                 goto bad;
2617         }
2618
2619         r = mempool_init_slab_pool(&cache->migration_pool, MIGRATION_POOL_SIZE,
2620                                    migration_cache);
2621         if (r) {
2622                 *error = "Error creating cache's migration mempool";
2623                 goto bad;
2624         }
2625
2626         cache->need_tick_bio = true;
2627         cache->sized = false;
2628         cache->invalidate = false;
2629         cache->commit_requested = false;
2630         cache->loaded_mappings = false;
2631         cache->loaded_discards = false;
2632
2633         load_stats(cache);
2634
2635         atomic_set(&cache->stats.demotion, 0);
2636         atomic_set(&cache->stats.promotion, 0);
2637         atomic_set(&cache->stats.copies_avoided, 0);
2638         atomic_set(&cache->stats.cache_cell_clash, 0);
2639         atomic_set(&cache->stats.commit_count, 0);
2640         atomic_set(&cache->stats.discard_count, 0);
2641
2642         spin_lock_init(&cache->invalidation_lock);
2643         INIT_LIST_HEAD(&cache->invalidation_requests);
2644
2645         batcher_init(&cache->committer, commit_op, cache,
2646                      issue_op, cache, cache->wq);
2647         iot_init(&cache->tracker);
2648
2649         init_rwsem(&cache->background_work_lock);
2650         prevent_background_work(cache);
2651
2652         *result = cache;
2653         return 0;
2654 bad:
2655         destroy(cache);
2656         return r;
2657 }
2658
2659 static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2660 {
2661         unsigned i;
2662         const char **copy;
2663
2664         copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2665         if (!copy)
2666                 return -ENOMEM;
2667         for (i = 0; i < argc; i++) {
2668                 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2669                 if (!copy[i]) {
2670                         while (i--)
2671                                 kfree(copy[i]);
2672                         kfree(copy);
2673                         return -ENOMEM;
2674                 }
2675         }
2676
2677         cache->nr_ctr_args = argc;
2678         cache->ctr_args = copy;
2679
2680         return 0;
2681 }
2682
2683 static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2684 {
2685         int r = -EINVAL;
2686         struct cache_args *ca;
2687         struct cache *cache = NULL;
2688
2689         ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2690         if (!ca) {
2691                 ti->error = "Error allocating memory for cache";
2692                 return -ENOMEM;
2693         }
2694         ca->ti = ti;
2695
2696         r = parse_cache_args(ca, argc, argv, &ti->error);
2697         if (r)
2698                 goto out;
2699
2700         r = cache_create(ca, &cache);
2701         if (r)
2702                 goto out;
2703
2704         r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2705         if (r) {
2706                 destroy(cache);
2707                 goto out;
2708         }
2709
2710         ti->private = cache;
2711 out:
2712         destroy_cache_args(ca);
2713         return r;
2714 }
2715
2716 /*----------------------------------------------------------------*/
2717
2718 static int cache_map(struct dm_target *ti, struct bio *bio)
2719 {
2720         struct cache *cache = ti->private;
2721
2722         int r;
2723         bool commit_needed;
2724         dm_oblock_t block = get_bio_block(cache, bio);
2725
2726         init_per_bio_data(bio);
2727         if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
2728                 /*
2729                  * This can only occur if the io goes to a partial block at
2730                  * the end of the origin device.  We don't cache these.
2731                  * Just remap to the origin and carry on.
2732                  */
2733                 remap_to_origin(cache, bio);
2734                 accounted_begin(cache, bio);
2735                 return DM_MAPIO_REMAPPED;
2736         }
2737
2738         if (discard_or_flush(bio)) {
2739                 defer_bio(cache, bio);
2740                 return DM_MAPIO_SUBMITTED;
2741         }
2742
2743         r = map_bio(cache, bio, block, &commit_needed);
2744         if (commit_needed)
2745                 schedule_commit(&cache->committer);
2746
2747         return r;
2748 }
2749
2750 static int cache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *error)
2751 {
2752         struct cache *cache = ti->private;
2753         unsigned long flags;
2754         struct per_bio_data *pb = get_per_bio_data(bio);
2755
2756         if (pb->tick) {
2757                 policy_tick(cache->policy, false);
2758
2759                 spin_lock_irqsave(&cache->lock, flags);
2760                 cache->need_tick_bio = true;
2761                 spin_unlock_irqrestore(&cache->lock, flags);
2762         }
2763
2764         bio_drop_shared_lock(cache, bio);
2765         accounted_complete(cache, bio);
2766
2767         return DM_ENDIO_DONE;
2768 }
2769
2770 static int write_dirty_bitset(struct cache *cache)
2771 {
2772         int r;
2773
2774         if (get_cache_mode(cache) >= CM_READ_ONLY)
2775                 return -EINVAL;
2776
2777         r = dm_cache_set_dirty_bits(cache->cmd, from_cblock(cache->cache_size), cache->dirty_bitset);
2778         if (r)
2779                 metadata_operation_failed(cache, "dm_cache_set_dirty_bits", r);
2780
2781         return r;
2782 }
2783
2784 static int write_discard_bitset(struct cache *cache)
2785 {
2786         unsigned i, r;
2787
2788         if (get_cache_mode(cache) >= CM_READ_ONLY)
2789                 return -EINVAL;
2790
2791         r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2792                                            cache->discard_nr_blocks);
2793         if (r) {
2794                 DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
2795                 metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
2796                 return r;
2797         }
2798
2799         for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2800                 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2801                                          is_discarded(cache, to_dblock(i)));
2802                 if (r) {
2803                         metadata_operation_failed(cache, "dm_cache_set_discard", r);
2804                         return r;
2805                 }
2806         }
2807
2808         return 0;
2809 }
2810
2811 static int write_hints(struct cache *cache)
2812 {
2813         int r;
2814
2815         if (get_cache_mode(cache) >= CM_READ_ONLY)
2816                 return -EINVAL;
2817
2818         r = dm_cache_write_hints(cache->cmd, cache->policy);
2819         if (r) {
2820                 metadata_operation_failed(cache, "dm_cache_write_hints", r);
2821                 return r;
2822         }
2823
2824         return 0;
2825 }
2826
2827 /*
2828  * returns true on success
2829  */
2830 static bool sync_metadata(struct cache *cache)
2831 {
2832         int r1, r2, r3, r4;
2833
2834         r1 = write_dirty_bitset(cache);
2835         if (r1)
2836                 DMERR("%s: could not write dirty bitset", cache_device_name(cache));
2837
2838         r2 = write_discard_bitset(cache);
2839         if (r2)
2840                 DMERR("%s: could not write discard bitset", cache_device_name(cache));
2841
2842         save_stats(cache);
2843
2844         r3 = write_hints(cache);
2845         if (r3)
2846                 DMERR("%s: could not write hints", cache_device_name(cache));
2847
2848         /*
2849          * If writing the above metadata failed, we still commit, but don't
2850          * set the clean shutdown flag.  This will effectively force every
2851          * dirty bit to be set on reload.
2852          */
2853         r4 = commit(cache, !r1 && !r2 && !r3);
2854         if (r4)
2855                 DMERR("%s: could not write cache metadata", cache_device_name(cache));
2856
2857         return !r1 && !r2 && !r3 && !r4;
2858 }
2859
2860 static void cache_postsuspend(struct dm_target *ti)
2861 {
2862         struct cache *cache = ti->private;
2863
2864         prevent_background_work(cache);
2865         BUG_ON(atomic_read(&cache->nr_io_migrations));
2866
2867         cancel_delayed_work_sync(&cache->waker);
2868         drain_workqueue(cache->wq);
2869         WARN_ON(cache->tracker.in_flight);
2870
2871         /*
2872          * If it's a flush suspend there won't be any deferred bios, so this
2873          * call is harmless.
2874          */
2875         requeue_deferred_bios(cache);
2876
2877         if (get_cache_mode(cache) == CM_WRITE)
2878                 (void) sync_metadata(cache);
2879 }
2880
2881 static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2882                         bool dirty, uint32_t hint, bool hint_valid)
2883 {
2884         int r;
2885         struct cache *cache = context;
2886
2887         if (dirty) {
2888                 set_bit(from_cblock(cblock), cache->dirty_bitset);
2889                 atomic_inc(&cache->nr_dirty);
2890         } else
2891                 clear_bit(from_cblock(cblock), cache->dirty_bitset);
2892
2893         r = policy_load_mapping(cache->policy, oblock, cblock, dirty, hint, hint_valid);
2894         if (r)
2895                 return r;
2896
2897         return 0;
2898 }
2899
2900 /*
2901  * The discard block size in the on disk metadata is not
2902  * neccessarily the same as we're currently using.  So we have to
2903  * be careful to only set the discarded attribute if we know it
2904  * covers a complete block of the new size.
2905  */
2906 struct discard_load_info {
2907         struct cache *cache;
2908
2909         /*
2910          * These blocks are sized using the on disk dblock size, rather
2911          * than the current one.
2912          */
2913         dm_block_t block_size;
2914         dm_block_t discard_begin, discard_end;
2915 };
2916
2917 static void discard_load_info_init(struct cache *cache,
2918                                    struct discard_load_info *li)
2919 {
2920         li->cache = cache;
2921         li->discard_begin = li->discard_end = 0;
2922 }
2923
2924 static void set_discard_range(struct discard_load_info *li)
2925 {
2926         sector_t b, e;
2927
2928         if (li->discard_begin == li->discard_end)
2929                 return;
2930
2931         /*
2932          * Convert to sectors.
2933          */
2934         b = li->discard_begin * li->block_size;
2935         e = li->discard_end * li->block_size;
2936
2937         /*
2938          * Then convert back to the current dblock size.
2939          */
2940         b = dm_sector_div_up(b, li->cache->discard_block_size);
2941         sector_div(e, li->cache->discard_block_size);
2942
2943         /*
2944          * The origin may have shrunk, so we need to check we're still in
2945          * bounds.
2946          */
2947         if (e > from_dblock(li->cache->discard_nr_blocks))
2948                 e = from_dblock(li->cache->discard_nr_blocks);
2949
2950         for (; b < e; b++)
2951                 set_discard(li->cache, to_dblock(b));
2952 }
2953
2954 static int load_discard(void *context, sector_t discard_block_size,
2955                         dm_dblock_t dblock, bool discard)
2956 {
2957         struct discard_load_info *li = context;
2958
2959         li->block_size = discard_block_size;
2960
2961         if (discard) {
2962                 if (from_dblock(dblock) == li->discard_end)
2963                         /*
2964                          * We're already in a discard range, just extend it.
2965                          */
2966                         li->discard_end = li->discard_end + 1ULL;
2967
2968                 else {
2969                         /*
2970                          * Emit the old range and start a new one.
2971                          */
2972                         set_discard_range(li);
2973                         li->discard_begin = from_dblock(dblock);
2974                         li->discard_end = li->discard_begin + 1ULL;
2975                 }
2976         } else {
2977                 set_discard_range(li);
2978                 li->discard_begin = li->discard_end = 0;
2979         }
2980
2981         return 0;
2982 }
2983
2984 static dm_cblock_t get_cache_dev_size(struct cache *cache)
2985 {
2986         sector_t size = get_dev_size(cache->cache_dev);
2987         (void) sector_div(size, cache->sectors_per_block);
2988         return to_cblock(size);
2989 }
2990
2991 static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2992 {
2993         if (from_cblock(new_size) > from_cblock(cache->cache_size)) {
2994                 if (cache->sized) {
2995                         DMERR("%s: unable to extend cache due to missing cache table reload",
2996                               cache_device_name(cache));
2997                         return false;
2998                 }
2999         }
3000
3001         /*
3002          * We can't drop a dirty block when shrinking the cache.
3003          */
3004         while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
3005                 new_size = to_cblock(from_cblock(new_size) + 1);
3006                 if (is_dirty(cache, new_size)) {
3007                         DMERR("%s: unable to shrink cache; cache block %llu is dirty",
3008                               cache_device_name(cache),
3009                               (unsigned long long) from_cblock(new_size));
3010                         return false;
3011                 }
3012         }
3013
3014         return true;
3015 }
3016
3017 static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
3018 {
3019         int r;
3020
3021         r = dm_cache_resize(cache->cmd, new_size);
3022         if (r) {
3023                 DMERR("%s: could not resize cache metadata", cache_device_name(cache));
3024                 metadata_operation_failed(cache, "dm_cache_resize", r);
3025                 return r;
3026         }
3027
3028         set_cache_size(cache, new_size);
3029
3030         return 0;
3031 }
3032
3033 static int cache_preresume(struct dm_target *ti)
3034 {
3035         int r = 0;
3036         struct cache *cache = ti->private;
3037         dm_cblock_t csize = get_cache_dev_size(cache);
3038
3039         /*
3040          * Check to see if the cache has resized.
3041          */
3042         if (!cache->sized) {
3043                 r = resize_cache_dev(cache, csize);
3044                 if (r)
3045                         return r;
3046
3047                 cache->sized = true;
3048
3049         } else if (csize != cache->cache_size) {
3050                 if (!can_resize(cache, csize))
3051                         return -EINVAL;
3052
3053                 r = resize_cache_dev(cache, csize);
3054                 if (r)
3055                         return r;
3056         }
3057
3058         if (!cache->loaded_mappings) {
3059                 r = dm_cache_load_mappings(cache->cmd, cache->policy,
3060                                            load_mapping, cache);
3061                 if (r) {
3062                         DMERR("%s: could not load cache mappings", cache_device_name(cache));
3063                         metadata_operation_failed(cache, "dm_cache_load_mappings", r);
3064                         return r;
3065                 }
3066
3067                 cache->loaded_mappings = true;
3068         }
3069
3070         if (!cache->loaded_discards) {
3071                 struct discard_load_info li;
3072
3073                 /*
3074                  * The discard bitset could have been resized, or the
3075                  * discard block size changed.  To be safe we start by
3076                  * setting every dblock to not discarded.
3077                  */
3078                 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
3079
3080                 discard_load_info_init(cache, &li);
3081                 r = dm_cache_load_discards(cache->cmd, load_discard, &li);
3082                 if (r) {
3083                         DMERR("%s: could not load origin discards", cache_device_name(cache));
3084                         metadata_operation_failed(cache, "dm_cache_load_discards", r);
3085                         return r;
3086                 }
3087                 set_discard_range(&li);
3088
3089                 cache->loaded_discards = true;
3090         }
3091
3092         return r;
3093 }
3094
3095 static void cache_resume(struct dm_target *ti)
3096 {
3097         struct cache *cache = ti->private;
3098
3099         cache->need_tick_bio = true;
3100         allow_background_work(cache);
3101         do_waker(&cache->waker.work);
3102 }
3103
3104 /*
3105  * Status format:
3106  *
3107  * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
3108  * <cache block size> <#used cache blocks>/<#total cache blocks>
3109  * <#read hits> <#read misses> <#write hits> <#write misses>
3110  * <#demotions> <#promotions> <#dirty>
3111  * <#features> <features>*
3112  * <#core args> <core args>
3113  * <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
3114  */
3115 static void cache_status(struct dm_target *ti, status_type_t type,
3116                          unsigned status_flags, char *result, unsigned maxlen)
3117 {
3118         int r = 0;
3119         unsigned i;
3120         ssize_t sz = 0;
3121         dm_block_t nr_free_blocks_metadata = 0;
3122         dm_block_t nr_blocks_metadata = 0;
3123         char buf[BDEVNAME_SIZE];
3124         struct cache *cache = ti->private;
3125         dm_cblock_t residency;
3126         bool needs_check;
3127
3128         switch (type) {
3129         case STATUSTYPE_INFO:
3130                 if (get_cache_mode(cache) == CM_FAIL) {
3131                         DMEMIT("Fail");
3132                         break;
3133                 }
3134
3135                 /* Commit to ensure statistics aren't out-of-date */
3136                 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
3137                         (void) commit(cache, false);
3138
3139                 r = dm_cache_get_free_metadata_block_count(cache->cmd, &nr_free_blocks_metadata);
3140                 if (r) {
3141                         DMERR("%s: dm_cache_get_free_metadata_block_count returned %d",
3142                               cache_device_name(cache), r);
3143                         goto err;
3144                 }
3145
3146                 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
3147                 if (r) {
3148                         DMERR("%s: dm_cache_get_metadata_dev_size returned %d",
3149                               cache_device_name(cache), r);
3150                         goto err;
3151                 }
3152
3153                 residency = policy_residency(cache->policy);
3154
3155                 DMEMIT("%u %llu/%llu %llu %llu/%llu %u %u %u %u %u %u %lu ",
3156                        (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
3157                        (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3158                        (unsigned long long)nr_blocks_metadata,
3159                        (unsigned long long)cache->sectors_per_block,
3160                        (unsigned long long) from_cblock(residency),
3161                        (unsigned long long) from_cblock(cache->cache_size),
3162                        (unsigned) atomic_read(&cache->stats.read_hit),
3163                        (unsigned) atomic_read(&cache->stats.read_miss),
3164                        (unsigned) atomic_read(&cache->stats.write_hit),
3165                        (unsigned) atomic_read(&cache->stats.write_miss),
3166                        (unsigned) atomic_read(&cache->stats.demotion),
3167                        (unsigned) atomic_read(&cache->stats.promotion),
3168                        (unsigned long) atomic_read(&cache->nr_dirty));
3169
3170                 if (cache->features.metadata_version == 2)
3171                         DMEMIT("2 metadata2 ");
3172                 else
3173                         DMEMIT("1 ");
3174
3175                 if (writethrough_mode(cache))
3176                         DMEMIT("writethrough ");
3177
3178                 else if (passthrough_mode(cache))
3179                         DMEMIT("passthrough ");
3180
3181                 else if (writeback_mode(cache))
3182                         DMEMIT("writeback ");
3183
3184                 else {
3185                         DMERR("%s: internal error: unknown io mode: %d",
3186                               cache_device_name(cache), (int) cache->features.io_mode);
3187                         goto err;
3188                 }
3189
3190                 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
3191
3192                 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
3193                 if (sz < maxlen) {
3194                         r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
3195                         if (r)
3196                                 DMERR("%s: policy_emit_config_values returned %d",
3197                                       cache_device_name(cache), r);
3198                 }
3199
3200                 if (get_cache_mode(cache) == CM_READ_ONLY)
3201                         DMEMIT("ro ");
3202                 else
3203                         DMEMIT("rw ");
3204
3205                 r = dm_cache_metadata_needs_check(cache->cmd, &needs_check);
3206
3207                 if (r || needs_check)
3208                         DMEMIT("needs_check ");
3209                 else
3210                         DMEMIT("- ");
3211
3212                 break;
3213
3214         case STATUSTYPE_TABLE:
3215                 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
3216                 DMEMIT("%s ", buf);
3217                 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
3218                 DMEMIT("%s ", buf);
3219                 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
3220                 DMEMIT("%s", buf);
3221
3222                 for (i = 0; i < cache->nr_ctr_args - 1; i++)
3223                         DMEMIT(" %s", cache->ctr_args[i]);
3224                 if (cache->nr_ctr_args)
3225                         DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
3226         }
3227
3228         return;
3229
3230 err:
3231         DMEMIT("Error");
3232 }
3233
3234 /*
3235  * Defines a range of cblocks, begin to (end - 1) are in the range.  end is
3236  * the one-past-the-end value.
3237  */
3238 struct cblock_range {
3239         dm_cblock_t begin;
3240         dm_cblock_t end;
3241 };
3242
3243 /*
3244  * A cache block range can take two forms:
3245  *
3246  * i) A single cblock, eg. '3456'
3247  * ii) A begin and end cblock with a dash between, eg. 123-234
3248  */
3249 static int parse_cblock_range(struct cache *cache, const char *str,
3250                               struct cblock_range *result)
3251 {
3252         char dummy;
3253         uint64_t b, e;
3254         int r;
3255
3256         /*
3257          * Try and parse form (ii) first.
3258          */
3259         r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
3260         if (r < 0)
3261                 return r;
3262
3263         if (r == 2) {
3264                 result->begin = to_cblock(b);
3265                 result->end = to_cblock(e);
3266                 return 0;
3267         }
3268
3269         /*
3270          * That didn't work, try form (i).
3271          */
3272         r = sscanf(str, "%llu%c", &b, &dummy);
3273         if (r < 0)
3274                 return r;
3275
3276         if (r == 1) {
3277                 result->begin = to_cblock(b);
3278                 result->end = to_cblock(from_cblock(result->begin) + 1u);
3279                 return 0;
3280         }
3281
3282         DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
3283         return -EINVAL;
3284 }
3285
3286 static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
3287 {
3288         uint64_t b = from_cblock(range->begin);
3289         uint64_t e = from_cblock(range->end);
3290         uint64_t n = from_cblock(cache->cache_size);
3291
3292         if (b >= n) {
3293                 DMERR("%s: begin cblock out of range: %llu >= %llu",
3294                       cache_device_name(cache), b, n);
3295                 return -EINVAL;
3296         }
3297
3298         if (e > n) {
3299                 DMERR("%s: end cblock out of range: %llu > %llu",
3300                       cache_device_name(cache), e, n);
3301                 return -EINVAL;
3302         }
3303
3304         if (b >= e) {
3305                 DMERR("%s: invalid cblock range: %llu >= %llu",
3306                       cache_device_name(cache), b, e);
3307                 return -EINVAL;
3308         }
3309
3310         return 0;
3311 }
3312
3313 static inline dm_cblock_t cblock_succ(dm_cblock_t b)
3314 {
3315         return to_cblock(from_cblock(b) + 1);
3316 }
3317
3318 static int request_invalidation(struct cache *cache, struct cblock_range *range)
3319 {
3320         int r = 0;
3321
3322         /*
3323          * We don't need to do any locking here because we know we're in
3324          * passthrough mode.  There's is potential for a race between an
3325          * invalidation triggered by an io and an invalidation message.  This
3326          * is harmless, we must not worry if the policy call fails.
3327          */
3328         while (range->begin != range->end) {
3329                 r = invalidate_cblock(cache, range->begin);
3330                 if (r)
3331                         return r;
3332
3333                 range->begin = cblock_succ(range->begin);
3334         }
3335
3336         cache->commit_requested = true;
3337         return r;
3338 }
3339
3340 static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
3341                                               const char **cblock_ranges)
3342 {
3343         int r = 0;
3344         unsigned i;
3345         struct cblock_range range;
3346
3347         if (!passthrough_mode(cache)) {
3348                 DMERR("%s: cache has to be in passthrough mode for invalidation",
3349                       cache_device_name(cache));
3350                 return -EPERM;
3351         }
3352
3353         for (i = 0; i < count; i++) {
3354                 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3355                 if (r)
3356                         break;
3357
3358                 r = validate_cblock_range(cache, &range);
3359                 if (r)
3360                         break;
3361
3362                 /*
3363                  * Pass begin and end origin blocks to the worker and wake it.
3364                  */
3365                 r = request_invalidation(cache, &range);
3366                 if (r)
3367                         break;
3368         }
3369
3370         return r;
3371 }
3372
3373 /*
3374  * Supports
3375  *      "<key> <value>"
3376  * and
3377  *     "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
3378  *
3379  * The key migration_threshold is supported by the cache target core.
3380  */
3381 static int cache_message(struct dm_target *ti, unsigned argc, char **argv,
3382                          char *result, unsigned maxlen)
3383 {
3384         struct cache *cache = ti->private;
3385
3386         if (!argc)
3387                 return -EINVAL;
3388
3389         if (get_cache_mode(cache) >= CM_READ_ONLY) {
3390                 DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
3391                       cache_device_name(cache));
3392                 return -EOPNOTSUPP;
3393         }
3394
3395         if (!strcasecmp(argv[0], "invalidate_cblocks"))
3396                 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3397
3398         if (argc != 2)
3399                 return -EINVAL;
3400
3401         return set_config_value(cache, argv[0], argv[1]);
3402 }
3403
3404 static int cache_iterate_devices(struct dm_target *ti,
3405                                  iterate_devices_callout_fn fn, void *data)
3406 {
3407         int r = 0;
3408         struct cache *cache = ti->private;
3409
3410         r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3411         if (!r)
3412                 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3413
3414         return r;
3415 }
3416
3417 static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3418 {
3419         /*
3420          * FIXME: these limits may be incompatible with the cache device
3421          */
3422         limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
3423                                             cache->origin_sectors);
3424         limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
3425 }
3426
3427 static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3428 {
3429         struct cache *cache = ti->private;
3430         uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3431
3432         /*
3433          * If the system-determined stacked limits are compatible with the
3434          * cache's blocksize (io_opt is a factor) do not override them.
3435          */
3436         if (io_opt_sectors < cache->sectors_per_block ||
3437             do_div(io_opt_sectors, cache->sectors_per_block)) {
3438                 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
3439                 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3440         }
3441         set_discard_limits(cache, limits);
3442 }
3443
3444 /*----------------------------------------------------------------*/
3445
3446 static struct target_type cache_target = {
3447         .name = "cache",
3448         .version = {2, 0, 0},
3449         .module = THIS_MODULE,
3450         .ctr = cache_ctr,
3451         .dtr = cache_dtr,
3452         .map = cache_map,
3453         .end_io = cache_end_io,
3454         .postsuspend = cache_postsuspend,
3455         .preresume = cache_preresume,
3456         .resume = cache_resume,
3457         .status = cache_status,
3458         .message = cache_message,
3459         .iterate_devices = cache_iterate_devices,
3460         .io_hints = cache_io_hints,
3461 };
3462
3463 static int __init dm_cache_init(void)
3464 {
3465         int r;
3466
3467         migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3468         if (!migration_cache)
3469                 return -ENOMEM;
3470
3471         r = dm_register_target(&cache_target);
3472         if (r) {
3473                 DMERR("cache target registration failed: %d", r);
3474                 kmem_cache_destroy(migration_cache);
3475                 return r;
3476         }
3477
3478         return 0;
3479 }
3480
3481 static void __exit dm_cache_exit(void)
3482 {
3483         dm_unregister_target(&cache_target);
3484         kmem_cache_destroy(migration_cache);
3485 }
3486
3487 module_init(dm_cache_init);
3488 module_exit(dm_cache_exit);
3489
3490 MODULE_DESCRIPTION(DM_NAME " cache target");
3491 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3492 MODULE_LICENSE("GPL");