blob: 09334c275c79e91c7bf4fd41e18e641b2196073a [file] [log] [blame]
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001/*
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.h"
Darrick J. Wongb844fe62013-04-05 15:36:32 +01009#include "dm-bio-record.h"
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000010#include "dm-cache-metadata.h"
11
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
14#include <linux/init.h>
15#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19
20#define DM_MSG_PREFIX "cache"
21
22DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
23 "A percentage of time allocated for copying to and/or from cache");
24
25/*----------------------------------------------------------------*/
26
27/*
28 * Glossary:
29 *
30 * oblock: index of an origin block
31 * cblock: index of a cache block
32 * promotion: movement of a block from origin to cache
33 * demotion: movement of a block from cache to origin
34 * migration: movement of a block between the origin and cache device,
35 * either direction
36 */
37
38/*----------------------------------------------------------------*/
39
40static size_t bitset_size_in_bytes(unsigned nr_entries)
41{
42 return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
43}
44
45static unsigned long *alloc_bitset(unsigned nr_entries)
46{
47 size_t s = bitset_size_in_bytes(nr_entries);
48 return vzalloc(s);
49}
50
51static void clear_bitset(void *bitset, unsigned nr_entries)
52{
53 size_t s = bitset_size_in_bytes(nr_entries);
54 memset(bitset, 0, s);
55}
56
57static void free_bitset(unsigned long *bits)
58{
59 vfree(bits);
60}
61
62/*----------------------------------------------------------------*/
63
Joe Thornberc9d28d52013-10-31 13:55:48 -040064/*
65 * There are a couple of places where we let a bio run, but want to do some
66 * work before calling its endio function. We do this by temporarily
67 * changing the endio fn.
68 */
69struct dm_hook_info {
70 bio_end_io_t *bi_end_io;
71 void *bi_private;
72};
73
74static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
75 bio_end_io_t *bi_end_io, void *bi_private)
76{
77 h->bi_end_io = bio->bi_end_io;
78 h->bi_private = bio->bi_private;
79
80 bio->bi_end_io = bi_end_io;
81 bio->bi_private = bi_private;
82}
83
84static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
85{
86 bio->bi_end_io = h->bi_end_io;
87 bio->bi_private = h->bi_private;
88}
89
90/*----------------------------------------------------------------*/
91
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000092#define PRISON_CELLS 1024
93#define MIGRATION_POOL_SIZE 128
94#define COMMIT_PERIOD HZ
95#define MIGRATION_COUNT_WINDOW 10
96
97/*
Mike Snitzer05473042013-08-16 10:54:19 -040098 * The block size of the device holding cache data must be
99 * between 32KB and 1GB.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000100 */
101#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
Mike Snitzer05473042013-08-16 10:54:19 -0400102#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000103
104/*
105 * FIXME: the cache is read/write for the time being.
106 */
Joe Thornber2ee57d52013-10-24 14:10:29 -0400107enum cache_metadata_mode {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000108 CM_WRITE, /* metadata may be changed */
109 CM_READ_ONLY, /* metadata may not be changed */
110};
111
Joe Thornber2ee57d52013-10-24 14:10:29 -0400112enum cache_io_mode {
113 /*
114 * Data is written to cached blocks only. These blocks are marked
115 * dirty. If you lose the cache device you will lose data.
116 * Potential performance increase for both reads and writes.
117 */
118 CM_IO_WRITEBACK,
119
120 /*
121 * Data is written to both cache and origin. Blocks are never
122 * dirty. Potential performance benfit for reads only.
123 */
124 CM_IO_WRITETHROUGH,
125
126 /*
127 * A degraded mode useful for various cache coherency situations
128 * (eg, rolling back snapshots). Reads and writes always go to the
129 * origin. If a write goes to a cached oblock, then the cache
130 * block is invalidated.
131 */
132 CM_IO_PASSTHROUGH
133};
134
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000135struct cache_features {
Joe Thornber2ee57d52013-10-24 14:10:29 -0400136 enum cache_metadata_mode mode;
137 enum cache_io_mode io_mode;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000138};
139
140struct cache_stats {
141 atomic_t read_hit;
142 atomic_t read_miss;
143 atomic_t write_hit;
144 atomic_t write_miss;
145 atomic_t demotion;
146 atomic_t promotion;
147 atomic_t copies_avoided;
148 atomic_t cache_cell_clash;
149 atomic_t commit_count;
150 atomic_t discard_count;
151};
152
Joe Thornber65790ff2013-11-08 16:39:50 +0000153/*
154 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
155 * the one-past-the-end value.
156 */
157struct cblock_range {
158 dm_cblock_t begin;
159 dm_cblock_t end;
160};
161
162struct invalidation_request {
163 struct list_head list;
164 struct cblock_range *cblocks;
165
166 atomic_t complete;
167 int err;
168
169 wait_queue_head_t result_wait;
170};
171
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000172struct cache {
173 struct dm_target *ti;
174 struct dm_target_callbacks callbacks;
175
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400176 struct dm_cache_metadata *cmd;
177
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000178 /*
179 * Metadata is written to this device.
180 */
181 struct dm_dev *metadata_dev;
182
183 /*
184 * The slower of the two data devices. Typically a spindle.
185 */
186 struct dm_dev *origin_dev;
187
188 /*
189 * The faster of the two data devices. Typically an SSD.
190 */
191 struct dm_dev *cache_dev;
192
193 /*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000194 * Size of the origin device in _complete_ blocks and native sectors.
195 */
196 dm_oblock_t origin_blocks;
197 sector_t origin_sectors;
198
199 /*
200 * Size of the cache device in blocks.
201 */
202 dm_cblock_t cache_size;
203
204 /*
205 * Fields for converting from sectors to blocks.
206 */
207 uint32_t sectors_per_block;
208 int sectors_per_block_shift;
209
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000210 spinlock_t lock;
211 struct bio_list deferred_bios;
212 struct bio_list deferred_flush_bios;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000213 struct bio_list deferred_writethrough_bios;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000214 struct list_head quiesced_migrations;
215 struct list_head completed_migrations;
216 struct list_head need_commit_migrations;
217 sector_t migration_threshold;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000218 wait_queue_head_t migration_wait;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400219 atomic_t nr_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000220
Joe Thornber66cb1912013-10-30 17:11:58 +0000221 wait_queue_head_t quiescing_wait;
Joe Thornber238f8362013-10-30 17:29:30 +0000222 atomic_t quiescing;
Joe Thornber66cb1912013-10-30 17:11:58 +0000223 atomic_t quiescing_ack;
224
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000225 /*
226 * cache_size entries, dirty if set
227 */
228 dm_cblock_t nr_dirty;
229 unsigned long *dirty_bitset;
230
231 /*
232 * origin_blocks entries, discarded if set.
233 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000234 dm_dblock_t discard_nr_blocks;
235 unsigned long *discard_bitset;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400236 uint32_t discard_block_size; /* a power of 2 times sectors per block */
237
238 /*
239 * Rather than reconstructing the table line for the status we just
240 * save it and regurgitate.
241 */
242 unsigned nr_ctr_args;
243 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000244
245 struct dm_kcopyd_client *copier;
246 struct workqueue_struct *wq;
247 struct work_struct worker;
248
249 struct delayed_work waker;
250 unsigned long last_commit_jiffies;
251
252 struct dm_bio_prison *prison;
253 struct dm_deferred_set *all_io_ds;
254
255 mempool_t *migration_pool;
256 struct dm_cache_migration *next_migration;
257
258 struct dm_cache_policy *policy;
259 unsigned policy_nr_args;
260
261 bool need_tick_bio:1;
262 bool sized:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000263 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000264 bool commit_requested:1;
265 bool loaded_mappings:1;
266 bool loaded_discards:1;
267
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000268 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400269 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000270 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400271 struct cache_features features;
272
273 struct cache_stats stats;
Joe Thornber65790ff2013-11-08 16:39:50 +0000274
275 /*
276 * Invalidation fields.
277 */
278 spinlock_t invalidation_lock;
279 struct list_head invalidation_requests;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000280};
281
282struct per_bio_data {
283 bool tick:1;
284 unsigned req_nr:2;
285 struct dm_deferred_entry *all_io_entry;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000286
Mike Snitzer19b00922013-04-05 15:36:34 +0100287 /*
288 * writethrough fields. These MUST remain at the end of this
289 * structure and the 'cache' member must be the first as it
Joe Thornberaeed1422013-05-10 14:37:18 +0100290 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100291 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000292 struct cache *cache;
293 dm_cblock_t cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400294 struct dm_hook_info hook_info;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100295 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000296};
297
298struct dm_cache_migration {
299 struct list_head list;
300 struct cache *cache;
301
302 unsigned long start_jiffies;
303 dm_oblock_t old_oblock;
304 dm_oblock_t new_oblock;
305 dm_cblock_t cblock;
306
307 bool err:1;
308 bool writeback:1;
309 bool demote:1;
310 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400311 bool requeue_holder:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000312 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000313
314 struct dm_bio_prison_cell *old_ocell;
315 struct dm_bio_prison_cell *new_ocell;
316};
317
318/*
319 * Processing a bio in the worker thread may require these memory
320 * allocations. We prealloc to avoid deadlocks (the same worker thread
321 * frees them back to the mempool).
322 */
323struct prealloc {
324 struct dm_cache_migration *mg;
325 struct dm_bio_prison_cell *cell1;
326 struct dm_bio_prison_cell *cell2;
327};
328
329static void wake_worker(struct cache *cache)
330{
331 queue_work(cache->wq, &cache->worker);
332}
333
334/*----------------------------------------------------------------*/
335
336static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
337{
338 /* FIXME: change to use a local slab. */
339 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
340}
341
342static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
343{
344 dm_bio_prison_free_cell(cache->prison, cell);
345}
346
347static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
348{
349 if (!p->mg) {
350 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
351 if (!p->mg)
352 return -ENOMEM;
353 }
354
355 if (!p->cell1) {
356 p->cell1 = alloc_prison_cell(cache);
357 if (!p->cell1)
358 return -ENOMEM;
359 }
360
361 if (!p->cell2) {
362 p->cell2 = alloc_prison_cell(cache);
363 if (!p->cell2)
364 return -ENOMEM;
365 }
366
367 return 0;
368}
369
370static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
371{
372 if (p->cell2)
373 free_prison_cell(cache, p->cell2);
374
375 if (p->cell1)
376 free_prison_cell(cache, p->cell1);
377
378 if (p->mg)
379 mempool_free(p->mg, cache->migration_pool);
380}
381
382static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
383{
384 struct dm_cache_migration *mg = p->mg;
385
386 BUG_ON(!mg);
387 p->mg = NULL;
388
389 return mg;
390}
391
392/*
393 * You must have a cell within the prealloc struct to return. If not this
394 * function will BUG() rather than returning NULL.
395 */
396static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
397{
398 struct dm_bio_prison_cell *r = NULL;
399
400 if (p->cell1) {
401 r = p->cell1;
402 p->cell1 = NULL;
403
404 } else if (p->cell2) {
405 r = p->cell2;
406 p->cell2 = NULL;
407 } else
408 BUG();
409
410 return r;
411}
412
413/*
414 * You can't have more than two cells in a prealloc struct. BUG() will be
415 * called if you try and overfill.
416 */
417static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
418{
419 if (!p->cell2)
420 p->cell2 = cell;
421
422 else if (!p->cell1)
423 p->cell1 = cell;
424
425 else
426 BUG();
427}
428
429/*----------------------------------------------------------------*/
430
431static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
432{
433 key->virtual = 0;
434 key->dev = 0;
435 key->block = from_oblock(oblock);
436}
437
438/*
439 * The caller hands in a preallocated cell, and a free function for it.
440 * The cell will be freed if there's an error, or if it wasn't used because
441 * a cell with that key already exists.
442 */
443typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
444
445static int bio_detain(struct cache *cache, dm_oblock_t oblock,
446 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
447 cell_free_fn free_fn, void *free_context,
448 struct dm_bio_prison_cell **cell_result)
449{
450 int r;
451 struct dm_cell_key key;
452
453 build_key(oblock, &key);
454 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
455 if (r)
456 free_fn(free_context, cell_prealloc);
457
458 return r;
459}
460
461static int get_cell(struct cache *cache,
462 dm_oblock_t oblock,
463 struct prealloc *structs,
464 struct dm_bio_prison_cell **cell_result)
465{
466 int r;
467 struct dm_cell_key key;
468 struct dm_bio_prison_cell *cell_prealloc;
469
470 cell_prealloc = prealloc_get_cell(structs);
471
472 build_key(oblock, &key);
473 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
474 if (r)
475 prealloc_put_cell(structs, cell_prealloc);
476
477 return r;
478}
479
Joe Thornberaeed1422013-05-10 14:37:18 +0100480/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000481
482static bool is_dirty(struct cache *cache, dm_cblock_t b)
483{
484 return test_bit(from_cblock(b), cache->dirty_bitset);
485}
486
487static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
488{
489 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
490 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) + 1);
491 policy_set_dirty(cache->policy, oblock);
492 }
493}
494
495static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
496{
497 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
498 policy_clear_dirty(cache->policy, oblock);
499 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) - 1);
500 if (!from_cblock(cache->nr_dirty))
501 dm_table_event(cache->ti->table);
502 }
503}
504
505/*----------------------------------------------------------------*/
Joe Thornberaeed1422013-05-10 14:37:18 +0100506
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000507static bool block_size_is_power_of_two(struct cache *cache)
508{
509 return cache->sectors_per_block_shift >= 0;
510}
511
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100512/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
513#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
514__always_inline
515#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000516static dm_block_t block_div(dm_block_t b, uint32_t n)
517{
518 do_div(b, n);
519
520 return b;
521}
522
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000523static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
524{
Joe Thornber414dd672013-03-20 17:21:25 +0000525 uint32_t discard_blocks = cache->discard_block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000526 dm_block_t b = from_oblock(oblock);
527
528 if (!block_size_is_power_of_two(cache))
Joe Thornber414dd672013-03-20 17:21:25 +0000529 discard_blocks = discard_blocks / cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000530 else
531 discard_blocks >>= cache->sectors_per_block_shift;
532
Joe Thornber414dd672013-03-20 17:21:25 +0000533 b = block_div(b, discard_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000534
535 return to_dblock(b);
536}
537
538static void set_discard(struct cache *cache, dm_dblock_t b)
539{
540 unsigned long flags;
541
542 atomic_inc(&cache->stats.discard_count);
543
544 spin_lock_irqsave(&cache->lock, flags);
545 set_bit(from_dblock(b), cache->discard_bitset);
546 spin_unlock_irqrestore(&cache->lock, flags);
547}
548
549static void clear_discard(struct cache *cache, dm_dblock_t b)
550{
551 unsigned long flags;
552
553 spin_lock_irqsave(&cache->lock, flags);
554 clear_bit(from_dblock(b), cache->discard_bitset);
555 spin_unlock_irqrestore(&cache->lock, flags);
556}
557
558static bool is_discarded(struct cache *cache, dm_dblock_t b)
559{
560 int r;
561 unsigned long flags;
562
563 spin_lock_irqsave(&cache->lock, flags);
564 r = test_bit(from_dblock(b), cache->discard_bitset);
565 spin_unlock_irqrestore(&cache->lock, flags);
566
567 return r;
568}
569
570static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
571{
572 int r;
573 unsigned long flags;
574
575 spin_lock_irqsave(&cache->lock, flags);
576 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
577 cache->discard_bitset);
578 spin_unlock_irqrestore(&cache->lock, flags);
579
580 return r;
581}
582
583/*----------------------------------------------------------------*/
584
585static void load_stats(struct cache *cache)
586{
587 struct dm_cache_statistics stats;
588
589 dm_cache_metadata_get_stats(cache->cmd, &stats);
590 atomic_set(&cache->stats.read_hit, stats.read_hits);
591 atomic_set(&cache->stats.read_miss, stats.read_misses);
592 atomic_set(&cache->stats.write_hit, stats.write_hits);
593 atomic_set(&cache->stats.write_miss, stats.write_misses);
594}
595
596static void save_stats(struct cache *cache)
597{
598 struct dm_cache_statistics stats;
599
600 stats.read_hits = atomic_read(&cache->stats.read_hit);
601 stats.read_misses = atomic_read(&cache->stats.read_miss);
602 stats.write_hits = atomic_read(&cache->stats.write_hit);
603 stats.write_misses = atomic_read(&cache->stats.write_miss);
604
605 dm_cache_metadata_set_stats(cache->cmd, &stats);
606}
607
608/*----------------------------------------------------------------
609 * Per bio data
610 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100611
612/*
613 * If using writeback, leave out struct per_bio_data's writethrough fields.
614 */
615#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
616#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
617
Joe Thornber2ee57d52013-10-24 14:10:29 -0400618static bool writethrough_mode(struct cache_features *f)
619{
620 return f->io_mode == CM_IO_WRITETHROUGH;
621}
622
623static bool writeback_mode(struct cache_features *f)
624{
625 return f->io_mode == CM_IO_WRITEBACK;
626}
627
628static bool passthrough_mode(struct cache_features *f)
629{
630 return f->io_mode == CM_IO_PASSTHROUGH;
631}
632
Mike Snitzer19b00922013-04-05 15:36:34 +0100633static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000634{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400635 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100636}
637
638static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
639{
640 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000641 BUG_ON(!pb);
642 return pb;
643}
644
Mike Snitzer19b00922013-04-05 15:36:34 +0100645static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000646{
Mike Snitzer19b00922013-04-05 15:36:34 +0100647 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000648
649 pb->tick = false;
650 pb->req_nr = dm_bio_get_target_bio_nr(bio);
651 pb->all_io_entry = NULL;
652
653 return pb;
654}
655
656/*----------------------------------------------------------------
657 * Remapping
658 *--------------------------------------------------------------*/
659static void remap_to_origin(struct cache *cache, struct bio *bio)
660{
661 bio->bi_bdev = cache->origin_dev->bdev;
662}
663
664static void remap_to_cache(struct cache *cache, struct bio *bio,
665 dm_cblock_t cblock)
666{
667 sector_t bi_sector = bio->bi_sector;
668
669 bio->bi_bdev = cache->cache_dev->bdev;
670 if (!block_size_is_power_of_two(cache))
671 bio->bi_sector = (from_cblock(cblock) * cache->sectors_per_block) +
672 sector_div(bi_sector, cache->sectors_per_block);
673 else
674 bio->bi_sector = (from_cblock(cblock) << cache->sectors_per_block_shift) |
675 (bi_sector & (cache->sectors_per_block - 1));
676}
677
678static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
679{
680 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100681 size_t pb_data_size = get_per_bio_data_size(cache);
682 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000683
684 spin_lock_irqsave(&cache->lock, flags);
685 if (cache->need_tick_bio &&
686 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
687 pb->tick = true;
688 cache->need_tick_bio = false;
689 }
690 spin_unlock_irqrestore(&cache->lock, flags);
691}
692
693static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
694 dm_oblock_t oblock)
695{
696 check_if_tick_bio_needed(cache, bio);
697 remap_to_origin(cache, bio);
698 if (bio_data_dir(bio) == WRITE)
699 clear_discard(cache, oblock_to_dblock(cache, oblock));
700}
701
702static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
703 dm_oblock_t oblock, dm_cblock_t cblock)
704{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100705 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000706 remap_to_cache(cache, bio, cblock);
707 if (bio_data_dir(bio) == WRITE) {
708 set_dirty(cache, oblock, cblock);
709 clear_discard(cache, oblock_to_dblock(cache, oblock));
710 }
711}
712
713static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
714{
715 sector_t block_nr = bio->bi_sector;
716
717 if (!block_size_is_power_of_two(cache))
718 (void) sector_div(block_nr, cache->sectors_per_block);
719 else
720 block_nr >>= cache->sectors_per_block_shift;
721
722 return to_oblock(block_nr);
723}
724
725static int bio_triggers_commit(struct cache *cache, struct bio *bio)
726{
727 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
728}
729
730static void issue(struct cache *cache, struct bio *bio)
731{
732 unsigned long flags;
733
734 if (!bio_triggers_commit(cache, bio)) {
735 generic_make_request(bio);
736 return;
737 }
738
739 /*
740 * Batch together any bios that trigger commits and then issue a
741 * single commit for them in do_worker().
742 */
743 spin_lock_irqsave(&cache->lock, flags);
744 cache->commit_requested = true;
745 bio_list_add(&cache->deferred_flush_bios, bio);
746 spin_unlock_irqrestore(&cache->lock, flags);
747}
748
Joe Thornbere2e74d62013-03-20 17:21:27 +0000749static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
750{
751 unsigned long flags;
752
753 spin_lock_irqsave(&cache->lock, flags);
754 bio_list_add(&cache->deferred_writethrough_bios, bio);
755 spin_unlock_irqrestore(&cache->lock, flags);
756
757 wake_worker(cache);
758}
759
760static void writethrough_endio(struct bio *bio, int err)
761{
Mike Snitzer19b00922013-04-05 15:36:34 +0100762 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400763
764 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000765
766 if (err) {
767 bio_endio(bio, err);
768 return;
769 }
770
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100771 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000772 remap_to_cache(pb->cache, bio, pb->cblock);
773
774 /*
775 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed1422013-05-10 14:37:18 +0100776 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000777 * worker thread.
778 */
779 defer_writethrough_bio(pb->cache, bio);
780}
781
782/*
783 * When running in writethrough mode we need to send writes to clean blocks
784 * to both the cache and origin devices. In future we'd like to clone the
785 * bio and send them in parallel, but for now we're doing them in
786 * series as this is easier.
787 */
788static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
789 dm_oblock_t oblock, dm_cblock_t cblock)
790{
Mike Snitzer19b00922013-04-05 15:36:34 +0100791 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000792
793 pb->cache = cache;
794 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400795 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100796 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000797
798 remap_to_origin_clear_discard(pb->cache, bio, oblock);
799}
800
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000801/*----------------------------------------------------------------
802 * Migration processing
803 *
804 * Migration covers moving data from the origin device to the cache, or
805 * vice versa.
806 *--------------------------------------------------------------*/
807static void free_migration(struct dm_cache_migration *mg)
808{
809 mempool_free(mg, mg->cache->migration_pool);
810}
811
812static void inc_nr_migrations(struct cache *cache)
813{
814 atomic_inc(&cache->nr_migrations);
815}
816
817static void dec_nr_migrations(struct cache *cache)
818{
819 atomic_dec(&cache->nr_migrations);
820
821 /*
822 * Wake the worker in case we're suspending the target.
823 */
824 wake_up(&cache->migration_wait);
825}
826
827static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
828 bool holder)
829{
830 (holder ? dm_cell_release : dm_cell_release_no_holder)
831 (cache->prison, cell, &cache->deferred_bios);
832 free_prison_cell(cache, cell);
833}
834
835static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
836 bool holder)
837{
838 unsigned long flags;
839
840 spin_lock_irqsave(&cache->lock, flags);
841 __cell_defer(cache, cell, holder);
842 spin_unlock_irqrestore(&cache->lock, flags);
843
844 wake_worker(cache);
845}
846
847static void cleanup_migration(struct dm_cache_migration *mg)
848{
Joe Thornber66cb1912013-10-30 17:11:58 +0000849 struct cache *cache = mg->cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000850 free_migration(mg);
Joe Thornber66cb1912013-10-30 17:11:58 +0000851 dec_nr_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000852}
853
854static void migration_failure(struct dm_cache_migration *mg)
855{
856 struct cache *cache = mg->cache;
857
858 if (mg->writeback) {
859 DMWARN_LIMIT("writeback failed; couldn't copy block");
860 set_dirty(cache, mg->old_oblock, mg->cblock);
861 cell_defer(cache, mg->old_ocell, false);
862
863 } else if (mg->demote) {
864 DMWARN_LIMIT("demotion failed; couldn't copy block");
865 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
866
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200867 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000868 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200869 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000870 } else {
871 DMWARN_LIMIT("promotion failed; couldn't copy block");
872 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200873 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000874 }
875
876 cleanup_migration(mg);
877}
878
879static void migration_success_pre_commit(struct dm_cache_migration *mg)
880{
881 unsigned long flags;
882 struct cache *cache = mg->cache;
883
884 if (mg->writeback) {
885 cell_defer(cache, mg->old_ocell, false);
886 clear_dirty(cache, mg->old_oblock, mg->cblock);
887 cleanup_migration(mg);
888 return;
889
890 } else if (mg->demote) {
891 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
892 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
893 policy_force_mapping(cache->policy, mg->new_oblock,
894 mg->old_oblock);
895 if (mg->promote)
896 cell_defer(cache, mg->new_ocell, true);
897 cleanup_migration(mg);
898 return;
899 }
900 } else {
901 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
902 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
903 policy_remove_mapping(cache->policy, mg->new_oblock);
904 cleanup_migration(mg);
905 return;
906 }
907 }
908
909 spin_lock_irqsave(&cache->lock, flags);
910 list_add_tail(&mg->list, &cache->need_commit_migrations);
911 cache->commit_requested = true;
912 spin_unlock_irqrestore(&cache->lock, flags);
913}
914
915static void migration_success_post_commit(struct dm_cache_migration *mg)
916{
917 unsigned long flags;
918 struct cache *cache = mg->cache;
919
920 if (mg->writeback) {
921 DMWARN("writeback unexpectedly triggered commit");
922 return;
923
924 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200925 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000926
927 if (mg->promote) {
928 mg->demote = false;
929
930 spin_lock_irqsave(&cache->lock, flags);
931 list_add_tail(&mg->list, &cache->quiesced_migrations);
932 spin_unlock_irqrestore(&cache->lock, flags);
933
Joe Thornber65790ff2013-11-08 16:39:50 +0000934 } else {
935 if (mg->invalidate)
936 policy_remove_mapping(cache->policy, mg->old_oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000937 cleanup_migration(mg);
Joe Thornber65790ff2013-11-08 16:39:50 +0000938 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000939
940 } else {
Joe Thornberc9d28d52013-10-31 13:55:48 -0400941 if (mg->requeue_holder)
942 cell_defer(cache, mg->new_ocell, true);
943 else {
944 bio_endio(mg->new_ocell->holder, 0);
945 cell_defer(cache, mg->new_ocell, false);
946 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000947 clear_dirty(cache, mg->new_oblock, mg->cblock);
948 cleanup_migration(mg);
949 }
950}
951
952static void copy_complete(int read_err, unsigned long write_err, void *context)
953{
954 unsigned long flags;
955 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
956 struct cache *cache = mg->cache;
957
958 if (read_err || write_err)
959 mg->err = true;
960
961 spin_lock_irqsave(&cache->lock, flags);
962 list_add_tail(&mg->list, &cache->completed_migrations);
963 spin_unlock_irqrestore(&cache->lock, flags);
964
965 wake_worker(cache);
966}
967
968static void issue_copy_real(struct dm_cache_migration *mg)
969{
970 int r;
971 struct dm_io_region o_region, c_region;
972 struct cache *cache = mg->cache;
973
974 o_region.bdev = cache->origin_dev->bdev;
975 o_region.count = cache->sectors_per_block;
976
977 c_region.bdev = cache->cache_dev->bdev;
978 c_region.sector = from_cblock(mg->cblock) * cache->sectors_per_block;
979 c_region.count = cache->sectors_per_block;
980
981 if (mg->writeback || mg->demote) {
982 /* demote */
983 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
984 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
985 } else {
986 /* promote */
987 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
988 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
989 }
990
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +0200991 if (r < 0) {
992 DMERR_LIMIT("issuing migration failed");
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000993 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +0200994 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000995}
996
Joe Thornberc9d28d52013-10-31 13:55:48 -0400997static void overwrite_endio(struct bio *bio, int err)
998{
999 struct dm_cache_migration *mg = bio->bi_private;
1000 struct cache *cache = mg->cache;
1001 size_t pb_data_size = get_per_bio_data_size(cache);
1002 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1003 unsigned long flags;
1004
1005 if (err)
1006 mg->err = true;
1007
1008 spin_lock_irqsave(&cache->lock, flags);
1009 list_add_tail(&mg->list, &cache->completed_migrations);
1010 dm_unhook_bio(&pb->hook_info, bio);
1011 mg->requeue_holder = false;
1012 spin_unlock_irqrestore(&cache->lock, flags);
1013
1014 wake_worker(cache);
1015}
1016
1017static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1018{
1019 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1020 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1021
1022 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1023 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
1024 generic_make_request(bio);
1025}
1026
1027static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1028{
1029 return (bio_data_dir(bio) == WRITE) &&
1030 (bio->bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
1031}
1032
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001033static void avoid_copy(struct dm_cache_migration *mg)
1034{
1035 atomic_inc(&mg->cache->stats.copies_avoided);
1036 migration_success_pre_commit(mg);
1037}
1038
1039static void issue_copy(struct dm_cache_migration *mg)
1040{
1041 bool avoid;
1042 struct cache *cache = mg->cache;
1043
1044 if (mg->writeback || mg->demote)
1045 avoid = !is_dirty(cache, mg->cblock) ||
1046 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001047 else {
1048 struct bio *bio = mg->new_ocell->holder;
1049
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001050 avoid = is_discarded_oblock(cache, mg->new_oblock);
1051
Joe Thornberc9d28d52013-10-31 13:55:48 -04001052 if (!avoid && bio_writes_complete_block(cache, bio)) {
1053 issue_overwrite(mg, bio);
1054 return;
1055 }
1056 }
1057
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001058 avoid ? avoid_copy(mg) : issue_copy_real(mg);
1059}
1060
1061static void complete_migration(struct dm_cache_migration *mg)
1062{
1063 if (mg->err)
1064 migration_failure(mg);
1065 else
1066 migration_success_pre_commit(mg);
1067}
1068
1069static void process_migrations(struct cache *cache, struct list_head *head,
1070 void (*fn)(struct dm_cache_migration *))
1071{
1072 unsigned long flags;
1073 struct list_head list;
1074 struct dm_cache_migration *mg, *tmp;
1075
1076 INIT_LIST_HEAD(&list);
1077 spin_lock_irqsave(&cache->lock, flags);
1078 list_splice_init(head, &list);
1079 spin_unlock_irqrestore(&cache->lock, flags);
1080
1081 list_for_each_entry_safe(mg, tmp, &list, list)
1082 fn(mg);
1083}
1084
1085static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1086{
1087 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1088}
1089
1090static void queue_quiesced_migration(struct dm_cache_migration *mg)
1091{
1092 unsigned long flags;
1093 struct cache *cache = mg->cache;
1094
1095 spin_lock_irqsave(&cache->lock, flags);
1096 __queue_quiesced_migration(mg);
1097 spin_unlock_irqrestore(&cache->lock, flags);
1098
1099 wake_worker(cache);
1100}
1101
1102static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1103{
1104 unsigned long flags;
1105 struct dm_cache_migration *mg, *tmp;
1106
1107 spin_lock_irqsave(&cache->lock, flags);
1108 list_for_each_entry_safe(mg, tmp, work, list)
1109 __queue_quiesced_migration(mg);
1110 spin_unlock_irqrestore(&cache->lock, flags);
1111
1112 wake_worker(cache);
1113}
1114
1115static void check_for_quiesced_migrations(struct cache *cache,
1116 struct per_bio_data *pb)
1117{
1118 struct list_head work;
1119
1120 if (!pb->all_io_entry)
1121 return;
1122
1123 INIT_LIST_HEAD(&work);
1124 if (pb->all_io_entry)
1125 dm_deferred_entry_dec(pb->all_io_entry, &work);
1126
1127 if (!list_empty(&work))
1128 queue_quiesced_migrations(cache, &work);
1129}
1130
1131static void quiesce_migration(struct dm_cache_migration *mg)
1132{
1133 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1134 queue_quiesced_migration(mg);
1135}
1136
1137static void promote(struct cache *cache, struct prealloc *structs,
1138 dm_oblock_t oblock, dm_cblock_t cblock,
1139 struct dm_bio_prison_cell *cell)
1140{
1141 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1142
1143 mg->err = false;
1144 mg->writeback = false;
1145 mg->demote = false;
1146 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001147 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001148 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001149 mg->cache = cache;
1150 mg->new_oblock = oblock;
1151 mg->cblock = cblock;
1152 mg->old_ocell = NULL;
1153 mg->new_ocell = cell;
1154 mg->start_jiffies = jiffies;
1155
1156 inc_nr_migrations(cache);
1157 quiesce_migration(mg);
1158}
1159
1160static void writeback(struct cache *cache, struct prealloc *structs,
1161 dm_oblock_t oblock, dm_cblock_t cblock,
1162 struct dm_bio_prison_cell *cell)
1163{
1164 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1165
1166 mg->err = false;
1167 mg->writeback = true;
1168 mg->demote = false;
1169 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001170 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001171 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001172 mg->cache = cache;
1173 mg->old_oblock = oblock;
1174 mg->cblock = cblock;
1175 mg->old_ocell = cell;
1176 mg->new_ocell = NULL;
1177 mg->start_jiffies = jiffies;
1178
1179 inc_nr_migrations(cache);
1180 quiesce_migration(mg);
1181}
1182
1183static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1184 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1185 dm_cblock_t cblock,
1186 struct dm_bio_prison_cell *old_ocell,
1187 struct dm_bio_prison_cell *new_ocell)
1188{
1189 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1190
1191 mg->err = false;
1192 mg->writeback = false;
1193 mg->demote = true;
1194 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001195 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001196 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001197 mg->cache = cache;
1198 mg->old_oblock = old_oblock;
1199 mg->new_oblock = new_oblock;
1200 mg->cblock = cblock;
1201 mg->old_ocell = old_ocell;
1202 mg->new_ocell = new_ocell;
1203 mg->start_jiffies = jiffies;
1204
1205 inc_nr_migrations(cache);
1206 quiesce_migration(mg);
1207}
1208
Joe Thornber2ee57d52013-10-24 14:10:29 -04001209/*
1210 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1211 * block are thrown away.
1212 */
1213static void invalidate(struct cache *cache, struct prealloc *structs,
1214 dm_oblock_t oblock, dm_cblock_t cblock,
1215 struct dm_bio_prison_cell *cell)
1216{
1217 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1218
1219 mg->err = false;
1220 mg->writeback = false;
1221 mg->demote = true;
1222 mg->promote = false;
1223 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001224 mg->invalidate = true;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001225 mg->cache = cache;
1226 mg->old_oblock = oblock;
1227 mg->cblock = cblock;
1228 mg->old_ocell = cell;
1229 mg->new_ocell = NULL;
1230 mg->start_jiffies = jiffies;
1231
1232 inc_nr_migrations(cache);
1233 quiesce_migration(mg);
1234}
1235
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001236/*----------------------------------------------------------------
1237 * bio processing
1238 *--------------------------------------------------------------*/
1239static void defer_bio(struct cache *cache, struct bio *bio)
1240{
1241 unsigned long flags;
1242
1243 spin_lock_irqsave(&cache->lock, flags);
1244 bio_list_add(&cache->deferred_bios, bio);
1245 spin_unlock_irqrestore(&cache->lock, flags);
1246
1247 wake_worker(cache);
1248}
1249
1250static void process_flush_bio(struct cache *cache, struct bio *bio)
1251{
Mike Snitzer19b00922013-04-05 15:36:34 +01001252 size_t pb_data_size = get_per_bio_data_size(cache);
1253 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001254
1255 BUG_ON(bio->bi_size);
1256 if (!pb->req_nr)
1257 remap_to_origin(cache, bio);
1258 else
1259 remap_to_cache(cache, bio, 0);
1260
1261 issue(cache, bio);
1262}
1263
1264/*
1265 * People generally discard large parts of a device, eg, the whole device
1266 * when formatting. Splitting these large discards up into cache block
1267 * sized ios and then quiescing (always neccessary for discard) takes too
1268 * long.
1269 *
1270 * We keep it simple, and allow any size of discard to come in, and just
1271 * mark off blocks on the discard bitset. No passdown occurs!
1272 *
1273 * To implement passdown we need to change the bio_prison such that a cell
1274 * can have a key that spans many blocks.
1275 */
1276static void process_discard_bio(struct cache *cache, struct bio *bio)
1277{
1278 dm_block_t start_block = dm_sector_div_up(bio->bi_sector,
1279 cache->discard_block_size);
1280 dm_block_t end_block = bio->bi_sector + bio_sectors(bio);
1281 dm_block_t b;
1282
Joe Thornber414dd672013-03-20 17:21:25 +00001283 end_block = block_div(end_block, cache->discard_block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001284
1285 for (b = start_block; b < end_block; b++)
1286 set_discard(cache, to_dblock(b));
1287
1288 bio_endio(bio, 0);
1289}
1290
1291static bool spare_migration_bandwidth(struct cache *cache)
1292{
1293 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1294 cache->sectors_per_block;
1295 return current_volume < cache->migration_threshold;
1296}
1297
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001298static void inc_hit_counter(struct cache *cache, struct bio *bio)
1299{
1300 atomic_inc(bio_data_dir(bio) == READ ?
1301 &cache->stats.read_hit : &cache->stats.write_hit);
1302}
1303
1304static void inc_miss_counter(struct cache *cache, struct bio *bio)
1305{
1306 atomic_inc(bio_data_dir(bio) == READ ?
1307 &cache->stats.read_miss : &cache->stats.write_miss);
1308}
1309
Joe Thornber2ee57d52013-10-24 14:10:29 -04001310static void issue_cache_bio(struct cache *cache, struct bio *bio,
1311 struct per_bio_data *pb,
1312 dm_oblock_t oblock, dm_cblock_t cblock)
1313{
1314 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1315 remap_to_cache_dirty(cache, bio, oblock, cblock);
1316 issue(cache, bio);
1317}
1318
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001319static void process_bio(struct cache *cache, struct prealloc *structs,
1320 struct bio *bio)
1321{
1322 int r;
1323 bool release_cell = true;
1324 dm_oblock_t block = get_bio_block(cache, bio);
1325 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1326 struct policy_result lookup_result;
Mike Snitzer19b00922013-04-05 15:36:34 +01001327 size_t pb_data_size = get_per_bio_data_size(cache);
1328 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001329 bool discarded_block = is_discarded_oblock(cache, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001330 bool passthrough = passthrough_mode(&cache->features);
1331 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001332
1333 /*
1334 * Check to see if that block is currently migrating.
1335 */
1336 cell_prealloc = prealloc_get_cell(structs);
1337 r = bio_detain(cache, block, bio, cell_prealloc,
1338 (cell_free_fn) prealloc_put_cell,
1339 structs, &new_ocell);
1340 if (r > 0)
1341 return;
1342
1343 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1344 bio, &lookup_result);
1345
1346 if (r == -EWOULDBLOCK)
1347 /* migration has been denied */
1348 lookup_result.op = POLICY_MISS;
1349
1350 switch (lookup_result.op) {
1351 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001352 if (passthrough) {
1353 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001354
Joe Thornber2ee57d52013-10-24 14:10:29 -04001355 /*
1356 * Passthrough always maps to the origin,
1357 * invalidating any cache blocks that are written
1358 * to.
1359 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001360
Joe Thornber2ee57d52013-10-24 14:10:29 -04001361 if (bio_data_dir(bio) == WRITE) {
1362 atomic_inc(&cache->stats.demotion);
1363 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1364 release_cell = false;
1365
1366 } else {
1367 /* FIXME: factor out issue_origin() */
1368 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1369 remap_to_origin_clear_discard(cache, bio, block);
1370 issue(cache, bio);
1371 }
1372 } else {
1373 inc_hit_counter(cache, bio);
1374
1375 if (bio_data_dir(bio) == WRITE &&
1376 writethrough_mode(&cache->features) &&
1377 !is_dirty(cache, lookup_result.cblock)) {
1378 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1379 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
1380 issue(cache, bio);
1381 } else
1382 issue_cache_bio(cache, bio, pb, block, lookup_result.cblock);
1383 }
1384
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001385 break;
1386
1387 case POLICY_MISS:
1388 inc_miss_counter(cache, bio);
1389 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001390 remap_to_origin_clear_discard(cache, bio, block);
1391 issue(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001392 break;
1393
1394 case POLICY_NEW:
1395 atomic_inc(&cache->stats.promotion);
1396 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1397 release_cell = false;
1398 break;
1399
1400 case POLICY_REPLACE:
1401 cell_prealloc = prealloc_get_cell(structs);
1402 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1403 (cell_free_fn) prealloc_put_cell,
1404 structs, &old_ocell);
1405 if (r > 0) {
1406 /*
1407 * We have to be careful to avoid lock inversion of
1408 * the cells. So we back off, and wait for the
1409 * old_ocell to become free.
1410 */
1411 policy_force_mapping(cache->policy, block,
1412 lookup_result.old_oblock);
1413 atomic_inc(&cache->stats.cache_cell_clash);
1414 break;
1415 }
1416 atomic_inc(&cache->stats.demotion);
1417 atomic_inc(&cache->stats.promotion);
1418
1419 demote_then_promote(cache, structs, lookup_result.old_oblock,
1420 block, lookup_result.cblock,
1421 old_ocell, new_ocell);
1422 release_cell = false;
1423 break;
1424
1425 default:
1426 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1427 (unsigned) lookup_result.op);
1428 bio_io_error(bio);
1429 }
1430
1431 if (release_cell)
1432 cell_defer(cache, new_ocell, false);
1433}
1434
1435static int need_commit_due_to_time(struct cache *cache)
1436{
1437 return jiffies < cache->last_commit_jiffies ||
1438 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1439}
1440
1441static int commit_if_needed(struct cache *cache)
1442{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001443 int r = 0;
1444
1445 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1446 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001447 atomic_inc(&cache->stats.commit_count);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001448 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001449 r = dm_cache_commit(cache->cmd, false);
1450 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001451 }
1452
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001453 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001454}
1455
1456static void process_deferred_bios(struct cache *cache)
1457{
1458 unsigned long flags;
1459 struct bio_list bios;
1460 struct bio *bio;
1461 struct prealloc structs;
1462
1463 memset(&structs, 0, sizeof(structs));
1464 bio_list_init(&bios);
1465
1466 spin_lock_irqsave(&cache->lock, flags);
1467 bio_list_merge(&bios, &cache->deferred_bios);
1468 bio_list_init(&cache->deferred_bios);
1469 spin_unlock_irqrestore(&cache->lock, flags);
1470
1471 while (!bio_list_empty(&bios)) {
1472 /*
1473 * If we've got no free migration structs, and processing
1474 * this bio might require one, we pause until there are some
1475 * prepared mappings to process.
1476 */
1477 if (prealloc_data_structs(cache, &structs)) {
1478 spin_lock_irqsave(&cache->lock, flags);
1479 bio_list_merge(&cache->deferred_bios, &bios);
1480 spin_unlock_irqrestore(&cache->lock, flags);
1481 break;
1482 }
1483
1484 bio = bio_list_pop(&bios);
1485
1486 if (bio->bi_rw & REQ_FLUSH)
1487 process_flush_bio(cache, bio);
1488 else if (bio->bi_rw & REQ_DISCARD)
1489 process_discard_bio(cache, bio);
1490 else
1491 process_bio(cache, &structs, bio);
1492 }
1493
1494 prealloc_free_structs(cache, &structs);
1495}
1496
1497static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1498{
1499 unsigned long flags;
1500 struct bio_list bios;
1501 struct bio *bio;
1502
1503 bio_list_init(&bios);
1504
1505 spin_lock_irqsave(&cache->lock, flags);
1506 bio_list_merge(&bios, &cache->deferred_flush_bios);
1507 bio_list_init(&cache->deferred_flush_bios);
1508 spin_unlock_irqrestore(&cache->lock, flags);
1509
1510 while ((bio = bio_list_pop(&bios)))
1511 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1512}
1513
Joe Thornbere2e74d62013-03-20 17:21:27 +00001514static void process_deferred_writethrough_bios(struct cache *cache)
1515{
1516 unsigned long flags;
1517 struct bio_list bios;
1518 struct bio *bio;
1519
1520 bio_list_init(&bios);
1521
1522 spin_lock_irqsave(&cache->lock, flags);
1523 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1524 bio_list_init(&cache->deferred_writethrough_bios);
1525 spin_unlock_irqrestore(&cache->lock, flags);
1526
1527 while ((bio = bio_list_pop(&bios)))
1528 generic_make_request(bio);
1529}
1530
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001531static void writeback_some_dirty_blocks(struct cache *cache)
1532{
1533 int r = 0;
1534 dm_oblock_t oblock;
1535 dm_cblock_t cblock;
1536 struct prealloc structs;
1537 struct dm_bio_prison_cell *old_ocell;
1538
1539 memset(&structs, 0, sizeof(structs));
1540
1541 while (spare_migration_bandwidth(cache)) {
1542 if (prealloc_data_structs(cache, &structs))
1543 break;
1544
1545 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1546 if (r)
1547 break;
1548
1549 r = get_cell(cache, oblock, &structs, &old_ocell);
1550 if (r) {
1551 policy_set_dirty(cache->policy, oblock);
1552 break;
1553 }
1554
1555 writeback(cache, &structs, oblock, cblock, old_ocell);
1556 }
1557
1558 prealloc_free_structs(cache, &structs);
1559}
1560
1561/*----------------------------------------------------------------
Joe Thornber65790ff2013-11-08 16:39:50 +00001562 * Invalidations.
1563 * Dropping something from the cache *without* writing back.
1564 *--------------------------------------------------------------*/
1565
1566static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1567{
1568 int r = 0;
1569 uint64_t begin = from_cblock(req->cblocks->begin);
1570 uint64_t end = from_cblock(req->cblocks->end);
1571
1572 while (begin != end) {
1573 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1574 if (!r) {
1575 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1576 if (r)
1577 break;
1578
1579 } else if (r == -ENODATA) {
1580 /* harmless, already unmapped */
1581 r = 0;
1582
1583 } else {
1584 DMERR("policy_remove_cblock failed");
1585 break;
1586 }
1587
1588 begin++;
1589 }
1590
1591 cache->commit_requested = true;
1592
1593 req->err = r;
1594 atomic_set(&req->complete, 1);
1595
1596 wake_up(&req->result_wait);
1597}
1598
1599static void process_invalidation_requests(struct cache *cache)
1600{
1601 struct list_head list;
1602 struct invalidation_request *req, *tmp;
1603
1604 INIT_LIST_HEAD(&list);
1605 spin_lock(&cache->invalidation_lock);
1606 list_splice_init(&cache->invalidation_requests, &list);
1607 spin_unlock(&cache->invalidation_lock);
1608
1609 list_for_each_entry_safe (req, tmp, &list, list)
1610 process_invalidation_request(cache, req);
1611}
1612
1613/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001614 * Main worker loop
1615 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001616static bool is_quiescing(struct cache *cache)
1617{
Joe Thornber238f8362013-10-30 17:29:30 +00001618 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001619}
1620
Joe Thornber66cb1912013-10-30 17:11:58 +00001621static void ack_quiescing(struct cache *cache)
1622{
1623 if (is_quiescing(cache)) {
1624 atomic_inc(&cache->quiescing_ack);
1625 wake_up(&cache->quiescing_wait);
1626 }
1627}
1628
1629static void wait_for_quiescing_ack(struct cache *cache)
1630{
1631 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1632}
1633
1634static void start_quiescing(struct cache *cache)
1635{
Joe Thornber238f8362013-10-30 17:29:30 +00001636 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00001637 wait_for_quiescing_ack(cache);
1638}
1639
1640static void stop_quiescing(struct cache *cache)
1641{
Joe Thornber238f8362013-10-30 17:29:30 +00001642 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00001643 atomic_set(&cache->quiescing_ack, 0);
1644}
1645
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001646static void wait_for_migrations(struct cache *cache)
1647{
1648 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1649}
1650
1651static void stop_worker(struct cache *cache)
1652{
1653 cancel_delayed_work(&cache->waker);
1654 flush_workqueue(cache->wq);
1655}
1656
1657static void requeue_deferred_io(struct cache *cache)
1658{
1659 struct bio *bio;
1660 struct bio_list bios;
1661
1662 bio_list_init(&bios);
1663 bio_list_merge(&bios, &cache->deferred_bios);
1664 bio_list_init(&cache->deferred_bios);
1665
1666 while ((bio = bio_list_pop(&bios)))
1667 bio_endio(bio, DM_ENDIO_REQUEUE);
1668}
1669
1670static int more_work(struct cache *cache)
1671{
1672 if (is_quiescing(cache))
1673 return !list_empty(&cache->quiesced_migrations) ||
1674 !list_empty(&cache->completed_migrations) ||
1675 !list_empty(&cache->need_commit_migrations);
1676 else
1677 return !bio_list_empty(&cache->deferred_bios) ||
1678 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001679 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001680 !list_empty(&cache->quiesced_migrations) ||
1681 !list_empty(&cache->completed_migrations) ||
Joe Thornber65790ff2013-11-08 16:39:50 +00001682 !list_empty(&cache->need_commit_migrations) ||
1683 cache->invalidate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001684}
1685
1686static void do_worker(struct work_struct *ws)
1687{
1688 struct cache *cache = container_of(ws, struct cache, worker);
1689
1690 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001691 if (!is_quiescing(cache)) {
1692 writeback_some_dirty_blocks(cache);
1693 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001694 process_deferred_bios(cache);
Joe Thornber65790ff2013-11-08 16:39:50 +00001695 process_invalidation_requests(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001696 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001697
1698 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1699 process_migrations(cache, &cache->completed_migrations, complete_migration);
1700
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001701 if (commit_if_needed(cache)) {
1702 process_deferred_flush_bios(cache, false);
1703
1704 /*
1705 * FIXME: rollback metadata or just go into a
1706 * failure mode and error everything
1707 */
1708 } else {
1709 process_deferred_flush_bios(cache, true);
1710 process_migrations(cache, &cache->need_commit_migrations,
1711 migration_success_post_commit);
1712 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001713
1714 ack_quiescing(cache);
1715
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001716 } while (more_work(cache));
1717}
1718
1719/*
1720 * We want to commit periodically so that not too much
1721 * unwritten metadata builds up.
1722 */
1723static void do_waker(struct work_struct *ws)
1724{
1725 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001726 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001727 wake_worker(cache);
1728 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1729}
1730
1731/*----------------------------------------------------------------*/
1732
1733static int is_congested(struct dm_dev *dev, int bdi_bits)
1734{
1735 struct request_queue *q = bdev_get_queue(dev->bdev);
1736 return bdi_congested(&q->backing_dev_info, bdi_bits);
1737}
1738
1739static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1740{
1741 struct cache *cache = container_of(cb, struct cache, callbacks);
1742
1743 return is_congested(cache->origin_dev, bdi_bits) ||
1744 is_congested(cache->cache_dev, bdi_bits);
1745}
1746
1747/*----------------------------------------------------------------
1748 * Target methods
1749 *--------------------------------------------------------------*/
1750
1751/*
1752 * This function gets called on the error paths of the constructor, so we
1753 * have to cope with a partially initialised struct.
1754 */
1755static void destroy(struct cache *cache)
1756{
1757 unsigned i;
1758
1759 if (cache->next_migration)
1760 mempool_free(cache->next_migration, cache->migration_pool);
1761
1762 if (cache->migration_pool)
1763 mempool_destroy(cache->migration_pool);
1764
1765 if (cache->all_io_ds)
1766 dm_deferred_set_destroy(cache->all_io_ds);
1767
1768 if (cache->prison)
1769 dm_bio_prison_destroy(cache->prison);
1770
1771 if (cache->wq)
1772 destroy_workqueue(cache->wq);
1773
1774 if (cache->dirty_bitset)
1775 free_bitset(cache->dirty_bitset);
1776
1777 if (cache->discard_bitset)
1778 free_bitset(cache->discard_bitset);
1779
1780 if (cache->copier)
1781 dm_kcopyd_client_destroy(cache->copier);
1782
1783 if (cache->cmd)
1784 dm_cache_metadata_close(cache->cmd);
1785
1786 if (cache->metadata_dev)
1787 dm_put_device(cache->ti, cache->metadata_dev);
1788
1789 if (cache->origin_dev)
1790 dm_put_device(cache->ti, cache->origin_dev);
1791
1792 if (cache->cache_dev)
1793 dm_put_device(cache->ti, cache->cache_dev);
1794
1795 if (cache->policy)
1796 dm_cache_policy_destroy(cache->policy);
1797
1798 for (i = 0; i < cache->nr_ctr_args ; i++)
1799 kfree(cache->ctr_args[i]);
1800 kfree(cache->ctr_args);
1801
1802 kfree(cache);
1803}
1804
1805static void cache_dtr(struct dm_target *ti)
1806{
1807 struct cache *cache = ti->private;
1808
1809 destroy(cache);
1810}
1811
1812static sector_t get_dev_size(struct dm_dev *dev)
1813{
1814 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1815}
1816
1817/*----------------------------------------------------------------*/
1818
1819/*
1820 * Construct a cache device mapping.
1821 *
1822 * cache <metadata dev> <cache dev> <origin dev> <block size>
1823 * <#feature args> [<feature arg>]*
1824 * <policy> <#policy args> [<policy arg>]*
1825 *
1826 * metadata dev : fast device holding the persistent metadata
1827 * cache dev : fast device holding cached data blocks
1828 * origin dev : slow device holding original data blocks
1829 * block size : cache unit size in sectors
1830 *
1831 * #feature args : number of feature arguments passed
1832 * feature args : writethrough. (The default is writeback.)
1833 *
1834 * policy : the replacement policy to use
1835 * #policy args : an even number of policy arguments corresponding
1836 * to key/value pairs passed to the policy
1837 * policy args : key/value pairs passed to the policy
1838 * E.g. 'sequential_threshold 1024'
1839 * See cache-policies.txt for details.
1840 *
1841 * Optional feature arguments are:
1842 * writethrough : write through caching that prohibits cache block
1843 * content from being different from origin block content.
1844 * Without this argument, the default behaviour is to write
1845 * back cache block contents later for performance reasons,
1846 * so they may differ from the corresponding origin blocks.
1847 */
1848struct cache_args {
1849 struct dm_target *ti;
1850
1851 struct dm_dev *metadata_dev;
1852
1853 struct dm_dev *cache_dev;
1854 sector_t cache_sectors;
1855
1856 struct dm_dev *origin_dev;
1857 sector_t origin_sectors;
1858
1859 uint32_t block_size;
1860
1861 const char *policy_name;
1862 int policy_argc;
1863 const char **policy_argv;
1864
1865 struct cache_features features;
1866};
1867
1868static void destroy_cache_args(struct cache_args *ca)
1869{
1870 if (ca->metadata_dev)
1871 dm_put_device(ca->ti, ca->metadata_dev);
1872
1873 if (ca->cache_dev)
1874 dm_put_device(ca->ti, ca->cache_dev);
1875
1876 if (ca->origin_dev)
1877 dm_put_device(ca->ti, ca->origin_dev);
1878
1879 kfree(ca);
1880}
1881
1882static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1883{
1884 if (!as->argc) {
1885 *error = "Insufficient args";
1886 return false;
1887 }
1888
1889 return true;
1890}
1891
1892static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1893 char **error)
1894{
1895 int r;
1896 sector_t metadata_dev_size;
1897 char b[BDEVNAME_SIZE];
1898
1899 if (!at_least_one_arg(as, error))
1900 return -EINVAL;
1901
1902 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1903 &ca->metadata_dev);
1904 if (r) {
1905 *error = "Error opening metadata device";
1906 return r;
1907 }
1908
1909 metadata_dev_size = get_dev_size(ca->metadata_dev);
1910 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1911 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1912 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1913
1914 return 0;
1915}
1916
1917static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1918 char **error)
1919{
1920 int r;
1921
1922 if (!at_least_one_arg(as, error))
1923 return -EINVAL;
1924
1925 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1926 &ca->cache_dev);
1927 if (r) {
1928 *error = "Error opening cache device";
1929 return r;
1930 }
1931 ca->cache_sectors = get_dev_size(ca->cache_dev);
1932
1933 return 0;
1934}
1935
1936static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1937 char **error)
1938{
1939 int r;
1940
1941 if (!at_least_one_arg(as, error))
1942 return -EINVAL;
1943
1944 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1945 &ca->origin_dev);
1946 if (r) {
1947 *error = "Error opening origin device";
1948 return r;
1949 }
1950
1951 ca->origin_sectors = get_dev_size(ca->origin_dev);
1952 if (ca->ti->len > ca->origin_sectors) {
1953 *error = "Device size larger than cached device";
1954 return -EINVAL;
1955 }
1956
1957 return 0;
1958}
1959
1960static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1961 char **error)
1962{
Mike Snitzer05473042013-08-16 10:54:19 -04001963 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001964
1965 if (!at_least_one_arg(as, error))
1966 return -EINVAL;
1967
Mike Snitzer05473042013-08-16 10:54:19 -04001968 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
1969 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1970 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1971 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001972 *error = "Invalid data block size";
1973 return -EINVAL;
1974 }
1975
Mike Snitzer05473042013-08-16 10:54:19 -04001976 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001977 *error = "Data block size is larger than the cache device";
1978 return -EINVAL;
1979 }
1980
Mike Snitzer05473042013-08-16 10:54:19 -04001981 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001982
1983 return 0;
1984}
1985
1986static void init_features(struct cache_features *cf)
1987{
1988 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001989 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001990}
1991
1992static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
1993 char **error)
1994{
1995 static struct dm_arg _args[] = {
1996 {0, 1, "Invalid number of cache feature arguments"},
1997 };
1998
1999 int r;
2000 unsigned argc;
2001 const char *arg;
2002 struct cache_features *cf = &ca->features;
2003
2004 init_features(cf);
2005
2006 r = dm_read_arg_group(_args, as, &argc, error);
2007 if (r)
2008 return -EINVAL;
2009
2010 while (argc--) {
2011 arg = dm_shift_arg(as);
2012
2013 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002014 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002015
2016 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002017 cf->io_mode = CM_IO_WRITETHROUGH;
2018
2019 else if (!strcasecmp(arg, "passthrough"))
2020 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002021
2022 else {
2023 *error = "Unrecognised cache feature requested";
2024 return -EINVAL;
2025 }
2026 }
2027
2028 return 0;
2029}
2030
2031static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2032 char **error)
2033{
2034 static struct dm_arg _args[] = {
2035 {0, 1024, "Invalid number of policy arguments"},
2036 };
2037
2038 int r;
2039
2040 if (!at_least_one_arg(as, error))
2041 return -EINVAL;
2042
2043 ca->policy_name = dm_shift_arg(as);
2044
2045 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2046 if (r)
2047 return -EINVAL;
2048
2049 ca->policy_argv = (const char **)as->argv;
2050 dm_consume_args(as, ca->policy_argc);
2051
2052 return 0;
2053}
2054
2055static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2056 char **error)
2057{
2058 int r;
2059 struct dm_arg_set as;
2060
2061 as.argc = argc;
2062 as.argv = argv;
2063
2064 r = parse_metadata_dev(ca, &as, error);
2065 if (r)
2066 return r;
2067
2068 r = parse_cache_dev(ca, &as, error);
2069 if (r)
2070 return r;
2071
2072 r = parse_origin_dev(ca, &as, error);
2073 if (r)
2074 return r;
2075
2076 r = parse_block_size(ca, &as, error);
2077 if (r)
2078 return r;
2079
2080 r = parse_features(ca, &as, error);
2081 if (r)
2082 return r;
2083
2084 r = parse_policy(ca, &as, error);
2085 if (r)
2086 return r;
2087
2088 return 0;
2089}
2090
2091/*----------------------------------------------------------------*/
2092
2093static struct kmem_cache *migration_cache;
2094
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002095#define NOT_CORE_OPTION 1
2096
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002097static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002098{
2099 unsigned long tmp;
2100
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002101 if (!strcasecmp(key, "migration_threshold")) {
2102 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002103 return -EINVAL;
2104
2105 cache->migration_threshold = tmp;
2106 return 0;
2107 }
2108
2109 return NOT_CORE_OPTION;
2110}
2111
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002112static int set_config_value(struct cache *cache, const char *key, const char *value)
2113{
2114 int r = process_config_option(cache, key, value);
2115
2116 if (r == NOT_CORE_OPTION)
2117 r = policy_set_config_value(cache->policy, key, value);
2118
2119 if (r)
2120 DMWARN("bad config value for %s: %s", key, value);
2121
2122 return r;
2123}
2124
2125static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002126{
2127 int r = 0;
2128
2129 if (argc & 1) {
2130 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2131 return -EINVAL;
2132 }
2133
2134 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002135 r = set_config_value(cache, argv[0], argv[1]);
2136 if (r)
2137 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002138
2139 argc -= 2;
2140 argv += 2;
2141 }
2142
2143 return r;
2144}
2145
2146static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2147 char **error)
2148{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002149 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2150 cache->cache_size,
2151 cache->origin_sectors,
2152 cache->sectors_per_block);
2153 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002154 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002155 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002156 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002157 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002158
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002159 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002160}
2161
2162/*
2163 * We want the discard block size to be a power of two, at least the size
2164 * of the cache block size, and have no more than 2^14 discard blocks
2165 * across the origin.
2166 */
2167#define MAX_DISCARD_BLOCKS (1 << 14)
2168
2169static bool too_many_discard_blocks(sector_t discard_block_size,
2170 sector_t origin_size)
2171{
2172 (void) sector_div(origin_size, discard_block_size);
2173
2174 return origin_size > MAX_DISCARD_BLOCKS;
2175}
2176
2177static sector_t calculate_discard_block_size(sector_t cache_block_size,
2178 sector_t origin_size)
2179{
2180 sector_t discard_block_size;
2181
2182 discard_block_size = roundup_pow_of_two(cache_block_size);
2183
2184 if (origin_size)
2185 while (too_many_discard_blocks(discard_block_size, origin_size))
2186 discard_block_size *= 2;
2187
2188 return discard_block_size;
2189}
2190
Joe Thornberf8350da2013-05-10 14:37:16 +01002191#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002192
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002193static int cache_create(struct cache_args *ca, struct cache **result)
2194{
2195 int r = 0;
2196 char **error = &ca->ti->error;
2197 struct cache *cache;
2198 struct dm_target *ti = ca->ti;
2199 dm_block_t origin_blocks;
2200 struct dm_cache_metadata *cmd;
2201 bool may_format = ca->features.mode == CM_WRITE;
2202
2203 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2204 if (!cache)
2205 return -ENOMEM;
2206
2207 cache->ti = ca->ti;
2208 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002209 ti->num_flush_bios = 2;
2210 ti->flush_supported = true;
2211
2212 ti->num_discard_bios = 1;
2213 ti->discards_supported = true;
2214 ti->discard_zeroes_data_unsupported = true;
2215
Joe Thornber8c5008f2013-05-10 14:37:18 +01002216 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01002217 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002218
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002219 cache->callbacks.congested_fn = cache_is_congested;
2220 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2221
2222 cache->metadata_dev = ca->metadata_dev;
2223 cache->origin_dev = ca->origin_dev;
2224 cache->cache_dev = ca->cache_dev;
2225
2226 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2227
2228 /* FIXME: factor out this whole section */
2229 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002230 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002231 cache->origin_blocks = to_oblock(origin_blocks);
2232
2233 cache->sectors_per_block = ca->block_size;
2234 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2235 r = -EINVAL;
2236 goto bad;
2237 }
2238
2239 if (ca->block_size & (ca->block_size - 1)) {
2240 dm_block_t cache_size = ca->cache_sectors;
2241
2242 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002243 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002244 cache->cache_size = to_cblock(cache_size);
2245 } else {
2246 cache->sectors_per_block_shift = __ffs(ca->block_size);
2247 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2248 }
2249
2250 r = create_cache_policy(cache, ca, error);
2251 if (r)
2252 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002253
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002254 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002255 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2256
2257 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2258 if (r) {
2259 *error = "Error setting cache policy's config values";
2260 goto bad;
2261 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002262
2263 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2264 ca->block_size, may_format,
2265 dm_cache_policy_get_hint_size(cache->policy));
2266 if (IS_ERR(cmd)) {
2267 *error = "Error creating metadata object";
2268 r = PTR_ERR(cmd);
2269 goto bad;
2270 }
2271 cache->cmd = cmd;
2272
Joe Thornber2ee57d52013-10-24 14:10:29 -04002273 if (passthrough_mode(&cache->features)) {
2274 bool all_clean;
2275
2276 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2277 if (r) {
2278 *error = "dm_cache_metadata_all_clean() failed";
2279 goto bad;
2280 }
2281
2282 if (!all_clean) {
2283 *error = "Cannot enter passthrough mode unless all blocks are clean";
2284 r = -EINVAL;
2285 goto bad;
2286 }
2287 }
2288
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002289 spin_lock_init(&cache->lock);
2290 bio_list_init(&cache->deferred_bios);
2291 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002292 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002293 INIT_LIST_HEAD(&cache->quiesced_migrations);
2294 INIT_LIST_HEAD(&cache->completed_migrations);
2295 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002296 atomic_set(&cache->nr_migrations, 0);
2297 init_waitqueue_head(&cache->migration_wait);
2298
Joe Thornber66cb1912013-10-30 17:11:58 +00002299 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002300 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002301 atomic_set(&cache->quiescing_ack, 0);
2302
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002303 r = -ENOMEM;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002304 cache->nr_dirty = 0;
2305 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2306 if (!cache->dirty_bitset) {
2307 *error = "could not allocate dirty bitset";
2308 goto bad;
2309 }
2310 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2311
2312 cache->discard_block_size =
2313 calculate_discard_block_size(cache->sectors_per_block,
2314 cache->origin_sectors);
2315 cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks);
2316 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2317 if (!cache->discard_bitset) {
2318 *error = "could not allocate discard bitset";
2319 goto bad;
2320 }
2321 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2322
2323 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2324 if (IS_ERR(cache->copier)) {
2325 *error = "could not create kcopyd client";
2326 r = PTR_ERR(cache->copier);
2327 goto bad;
2328 }
2329
2330 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2331 if (!cache->wq) {
2332 *error = "could not create workqueue for metadata object";
2333 goto bad;
2334 }
2335 INIT_WORK(&cache->worker, do_worker);
2336 INIT_DELAYED_WORK(&cache->waker, do_waker);
2337 cache->last_commit_jiffies = jiffies;
2338
2339 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2340 if (!cache->prison) {
2341 *error = "could not create bio prison";
2342 goto bad;
2343 }
2344
2345 cache->all_io_ds = dm_deferred_set_create();
2346 if (!cache->all_io_ds) {
2347 *error = "could not create all_io deferred set";
2348 goto bad;
2349 }
2350
2351 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2352 migration_cache);
2353 if (!cache->migration_pool) {
2354 *error = "Error creating cache's migration mempool";
2355 goto bad;
2356 }
2357
2358 cache->next_migration = NULL;
2359
2360 cache->need_tick_bio = true;
2361 cache->sized = false;
Joe Thornber65790ff2013-11-08 16:39:50 +00002362 cache->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002363 cache->commit_requested = false;
2364 cache->loaded_mappings = false;
2365 cache->loaded_discards = false;
2366
2367 load_stats(cache);
2368
2369 atomic_set(&cache->stats.demotion, 0);
2370 atomic_set(&cache->stats.promotion, 0);
2371 atomic_set(&cache->stats.copies_avoided, 0);
2372 atomic_set(&cache->stats.cache_cell_clash, 0);
2373 atomic_set(&cache->stats.commit_count, 0);
2374 atomic_set(&cache->stats.discard_count, 0);
2375
Joe Thornber65790ff2013-11-08 16:39:50 +00002376 spin_lock_init(&cache->invalidation_lock);
2377 INIT_LIST_HEAD(&cache->invalidation_requests);
2378
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002379 *result = cache;
2380 return 0;
2381
2382bad:
2383 destroy(cache);
2384 return r;
2385}
2386
2387static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2388{
2389 unsigned i;
2390 const char **copy;
2391
2392 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2393 if (!copy)
2394 return -ENOMEM;
2395 for (i = 0; i < argc; i++) {
2396 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2397 if (!copy[i]) {
2398 while (i--)
2399 kfree(copy[i]);
2400 kfree(copy);
2401 return -ENOMEM;
2402 }
2403 }
2404
2405 cache->nr_ctr_args = argc;
2406 cache->ctr_args = copy;
2407
2408 return 0;
2409}
2410
2411static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2412{
2413 int r = -EINVAL;
2414 struct cache_args *ca;
2415 struct cache *cache = NULL;
2416
2417 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2418 if (!ca) {
2419 ti->error = "Error allocating memory for cache";
2420 return -ENOMEM;
2421 }
2422 ca->ti = ti;
2423
2424 r = parse_cache_args(ca, argc, argv, &ti->error);
2425 if (r)
2426 goto out;
2427
2428 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002429 if (r)
2430 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002431
2432 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2433 if (r) {
2434 destroy(cache);
2435 goto out;
2436 }
2437
2438 ti->private = cache;
2439
2440out:
2441 destroy_cache_args(ca);
2442 return r;
2443}
2444
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002445static int cache_map(struct dm_target *ti, struct bio *bio)
2446{
2447 struct cache *cache = ti->private;
2448
2449 int r;
2450 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002451 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002452 bool can_migrate = false;
2453 bool discarded_block;
2454 struct dm_bio_prison_cell *cell;
2455 struct policy_result lookup_result;
2456 struct per_bio_data *pb;
2457
2458 if (from_oblock(block) > from_oblock(cache->origin_blocks)) {
2459 /*
2460 * This can only occur if the io goes to a partial block at
2461 * the end of the origin device. We don't cache these.
2462 * Just remap to the origin and carry on.
2463 */
2464 remap_to_origin_clear_discard(cache, bio, block);
2465 return DM_MAPIO_REMAPPED;
2466 }
2467
Mike Snitzer19b00922013-04-05 15:36:34 +01002468 pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002469
2470 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2471 defer_bio(cache, bio);
2472 return DM_MAPIO_SUBMITTED;
2473 }
2474
2475 /*
2476 * Check to see if that block is currently migrating.
2477 */
2478 cell = alloc_prison_cell(cache);
2479 if (!cell) {
2480 defer_bio(cache, bio);
2481 return DM_MAPIO_SUBMITTED;
2482 }
2483
2484 r = bio_detain(cache, block, bio, cell,
2485 (cell_free_fn) free_prison_cell,
2486 cache, &cell);
2487 if (r) {
2488 if (r < 0)
2489 defer_bio(cache, bio);
2490
2491 return DM_MAPIO_SUBMITTED;
2492 }
2493
2494 discarded_block = is_discarded_oblock(cache, block);
2495
2496 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2497 bio, &lookup_result);
2498 if (r == -EWOULDBLOCK) {
2499 cell_defer(cache, cell, true);
2500 return DM_MAPIO_SUBMITTED;
2501
2502 } else if (r) {
2503 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
2504 bio_io_error(bio);
2505 return DM_MAPIO_SUBMITTED;
2506 }
2507
Joe Thornber2ee57d52013-10-24 14:10:29 -04002508 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002509 switch (lookup_result.op) {
2510 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04002511 if (passthrough_mode(&cache->features)) {
2512 if (bio_data_dir(bio) == WRITE) {
2513 /*
2514 * We need to invalidate this block, so
2515 * defer for the worker thread.
2516 */
2517 cell_defer(cache, cell, true);
2518 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002519
Joe Thornber2ee57d52013-10-24 14:10:29 -04002520 } else {
2521 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2522 inc_miss_counter(cache, bio);
2523 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002524
Joe Thornber2ee57d52013-10-24 14:10:29 -04002525 cell_defer(cache, cell, false);
2526 }
2527
2528 } else {
2529 inc_hit_counter(cache, bio);
2530
2531 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2532 !is_dirty(cache, lookup_result.cblock))
2533 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2534 else
2535 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
2536
2537 cell_defer(cache, cell, false);
2538 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002539 break;
2540
2541 case POLICY_MISS:
2542 inc_miss_counter(cache, bio);
2543 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2544
2545 if (pb->req_nr != 0) {
2546 /*
2547 * This is a duplicate writethrough io that is no
2548 * longer needed because the block has been demoted.
2549 */
2550 bio_endio(bio, 0);
2551 cell_defer(cache, cell, false);
2552 return DM_MAPIO_SUBMITTED;
2553 } else {
2554 remap_to_origin_clear_discard(cache, bio, block);
2555 cell_defer(cache, cell, false);
2556 }
2557 break;
2558
2559 default:
2560 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2561 (unsigned) lookup_result.op);
2562 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002563 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002564 }
2565
Joe Thornber2ee57d52013-10-24 14:10:29 -04002566 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002567}
2568
2569static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2570{
2571 struct cache *cache = ti->private;
2572 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002573 size_t pb_data_size = get_per_bio_data_size(cache);
2574 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002575
2576 if (pb->tick) {
2577 policy_tick(cache->policy);
2578
2579 spin_lock_irqsave(&cache->lock, flags);
2580 cache->need_tick_bio = true;
2581 spin_unlock_irqrestore(&cache->lock, flags);
2582 }
2583
2584 check_for_quiesced_migrations(cache, pb);
2585
2586 return 0;
2587}
2588
2589static int write_dirty_bitset(struct cache *cache)
2590{
2591 unsigned i, r;
2592
2593 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2594 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2595 is_dirty(cache, to_cblock(i)));
2596 if (r)
2597 return r;
2598 }
2599
2600 return 0;
2601}
2602
2603static int write_discard_bitset(struct cache *cache)
2604{
2605 unsigned i, r;
2606
2607 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2608 cache->discard_nr_blocks);
2609 if (r) {
2610 DMERR("could not resize on-disk discard bitset");
2611 return r;
2612 }
2613
2614 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2615 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2616 is_discarded(cache, to_dblock(i)));
2617 if (r)
2618 return r;
2619 }
2620
2621 return 0;
2622}
2623
2624static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock,
2625 uint32_t hint)
2626{
2627 struct cache *cache = context;
2628 return dm_cache_save_hint(cache->cmd, cblock, hint);
2629}
2630
2631static int write_hints(struct cache *cache)
2632{
2633 int r;
2634
2635 r = dm_cache_begin_hints(cache->cmd, cache->policy);
2636 if (r) {
2637 DMERR("dm_cache_begin_hints failed");
2638 return r;
2639 }
2640
2641 r = policy_walk_mappings(cache->policy, save_hint, cache);
2642 if (r)
2643 DMERR("policy_walk_mappings failed");
2644
2645 return r;
2646}
2647
2648/*
2649 * returns true on success
2650 */
2651static bool sync_metadata(struct cache *cache)
2652{
2653 int r1, r2, r3, r4;
2654
2655 r1 = write_dirty_bitset(cache);
2656 if (r1)
2657 DMERR("could not write dirty bitset");
2658
2659 r2 = write_discard_bitset(cache);
2660 if (r2)
2661 DMERR("could not write discard bitset");
2662
2663 save_stats(cache);
2664
2665 r3 = write_hints(cache);
2666 if (r3)
2667 DMERR("could not write hints");
2668
2669 /*
2670 * If writing the above metadata failed, we still commit, but don't
2671 * set the clean shutdown flag. This will effectively force every
2672 * dirty bit to be set on reload.
2673 */
2674 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2675 if (r4)
2676 DMERR("could not write cache metadata. Data loss may occur.");
2677
2678 return !r1 && !r2 && !r3 && !r4;
2679}
2680
2681static void cache_postsuspend(struct dm_target *ti)
2682{
2683 struct cache *cache = ti->private;
2684
2685 start_quiescing(cache);
2686 wait_for_migrations(cache);
2687 stop_worker(cache);
2688 requeue_deferred_io(cache);
2689 stop_quiescing(cache);
2690
2691 (void) sync_metadata(cache);
2692}
2693
2694static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2695 bool dirty, uint32_t hint, bool hint_valid)
2696{
2697 int r;
2698 struct cache *cache = context;
2699
2700 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2701 if (r)
2702 return r;
2703
2704 if (dirty)
2705 set_dirty(cache, oblock, cblock);
2706 else
2707 clear_dirty(cache, oblock, cblock);
2708
2709 return 0;
2710}
2711
2712static int load_discard(void *context, sector_t discard_block_size,
2713 dm_dblock_t dblock, bool discard)
2714{
2715 struct cache *cache = context;
2716
2717 /* FIXME: handle mis-matched block size */
2718
2719 if (discard)
2720 set_discard(cache, dblock);
2721 else
2722 clear_discard(cache, dblock);
2723
2724 return 0;
2725}
2726
Joe Thornberf494a9c2013-10-31 13:55:49 -04002727static dm_cblock_t get_cache_dev_size(struct cache *cache)
2728{
2729 sector_t size = get_dev_size(cache->cache_dev);
2730 (void) sector_div(size, cache->sectors_per_block);
2731 return to_cblock(size);
2732}
2733
2734static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2735{
2736 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2737 return true;
2738
2739 /*
2740 * We can't drop a dirty block when shrinking the cache.
2741 */
2742 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2743 new_size = to_cblock(from_cblock(new_size) + 1);
2744 if (is_dirty(cache, new_size)) {
2745 DMERR("unable to shrink cache; cache block %llu is dirty",
2746 (unsigned long long) from_cblock(new_size));
2747 return false;
2748 }
2749 }
2750
2751 return true;
2752}
2753
2754static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2755{
2756 int r;
2757
Vincent Pelletier08844802013-11-30 12:58:42 +01002758 r = dm_cache_resize(cache->cmd, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04002759 if (r) {
2760 DMERR("could not resize cache metadata");
2761 return r;
2762 }
2763
2764 cache->cache_size = new_size;
2765
2766 return 0;
2767}
2768
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002769static int cache_preresume(struct dm_target *ti)
2770{
2771 int r = 0;
2772 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002773 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002774
2775 /*
2776 * Check to see if the cache has resized.
2777 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04002778 if (!cache->sized) {
2779 r = resize_cache_dev(cache, csize);
2780 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002781 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002782
2783 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002784
2785 } else if (csize != cache->cache_size) {
2786 if (!can_resize(cache, csize))
2787 return -EINVAL;
2788
2789 r = resize_cache_dev(cache, csize);
2790 if (r)
2791 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002792 }
2793
2794 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002795 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002796 load_mapping, cache);
2797 if (r) {
2798 DMERR("could not load cache mappings");
2799 return r;
2800 }
2801
2802 cache->loaded_mappings = true;
2803 }
2804
2805 if (!cache->loaded_discards) {
2806 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2807 if (r) {
2808 DMERR("could not load origin discards");
2809 return r;
2810 }
2811
2812 cache->loaded_discards = true;
2813 }
2814
2815 return r;
2816}
2817
2818static void cache_resume(struct dm_target *ti)
2819{
2820 struct cache *cache = ti->private;
2821
2822 cache->need_tick_bio = true;
2823 do_waker(&cache->waker.work);
2824}
2825
2826/*
2827 * Status format:
2828 *
Mike Snitzer6a388612014-01-09 16:04:12 -05002829 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
2830 * <cache block size> <#used cache blocks>/<#total cache blocks>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002831 * <#read hits> <#read misses> <#write hits> <#write misses>
Mike Snitzer6a388612014-01-09 16:04:12 -05002832 * <#demotions> <#promotions> <#dirty>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002833 * <#features> <features>*
2834 * <#core args> <core args>
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002835 * <policy name> <#policy args> <policy args>*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002836 */
2837static void cache_status(struct dm_target *ti, status_type_t type,
2838 unsigned status_flags, char *result, unsigned maxlen)
2839{
2840 int r = 0;
2841 unsigned i;
2842 ssize_t sz = 0;
2843 dm_block_t nr_free_blocks_metadata = 0;
2844 dm_block_t nr_blocks_metadata = 0;
2845 char buf[BDEVNAME_SIZE];
2846 struct cache *cache = ti->private;
2847 dm_cblock_t residency;
2848
2849 switch (type) {
2850 case STATUSTYPE_INFO:
2851 /* Commit to ensure statistics aren't out-of-date */
2852 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2853 r = dm_cache_commit(cache->cmd, false);
2854 if (r)
2855 DMERR("could not commit metadata for accurate status");
2856 }
2857
2858 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2859 &nr_free_blocks_metadata);
2860 if (r) {
2861 DMERR("could not get metadata free block count");
2862 goto err;
2863 }
2864
2865 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2866 if (r) {
2867 DMERR("could not get metadata device size");
2868 goto err;
2869 }
2870
2871 residency = policy_residency(cache->policy);
2872
Mike Snitzer6a388612014-01-09 16:04:12 -05002873 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %llu ",
2874 (unsigned)(DM_CACHE_METADATA_BLOCK_SIZE >> SECTOR_SHIFT),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002875 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2876 (unsigned long long)nr_blocks_metadata,
Mike Snitzer6a388612014-01-09 16:04:12 -05002877 cache->sectors_per_block,
2878 (unsigned long long) from_cblock(residency),
2879 (unsigned long long) from_cblock(cache->cache_size),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002880 (unsigned) atomic_read(&cache->stats.read_hit),
2881 (unsigned) atomic_read(&cache->stats.read_miss),
2882 (unsigned) atomic_read(&cache->stats.write_hit),
2883 (unsigned) atomic_read(&cache->stats.write_miss),
2884 (unsigned) atomic_read(&cache->stats.demotion),
2885 (unsigned) atomic_read(&cache->stats.promotion),
Mike Snitzer6a388612014-01-09 16:04:12 -05002886 (unsigned long long) from_cblock(cache->nr_dirty));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002887
Joe Thornber2ee57d52013-10-24 14:10:29 -04002888 if (writethrough_mode(&cache->features))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002889 DMEMIT("1 writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04002890
2891 else if (passthrough_mode(&cache->features))
2892 DMEMIT("1 passthrough ");
2893
2894 else if (writeback_mode(&cache->features))
2895 DMEMIT("1 writeback ");
2896
2897 else {
2898 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2899 goto err;
2900 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002901
2902 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002903
2904 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002905 if (sz < maxlen) {
2906 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2907 if (r)
2908 DMERR("policy_emit_config_values returned %d", r);
2909 }
2910
2911 break;
2912
2913 case STATUSTYPE_TABLE:
2914 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2915 DMEMIT("%s ", buf);
2916 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2917 DMEMIT("%s ", buf);
2918 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2919 DMEMIT("%s", buf);
2920
2921 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2922 DMEMIT(" %s", cache->ctr_args[i]);
2923 if (cache->nr_ctr_args)
2924 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2925 }
2926
2927 return;
2928
2929err:
2930 DMEMIT("Error");
2931}
2932
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002933/*
Joe Thornber65790ff2013-11-08 16:39:50 +00002934 * A cache block range can take two forms:
2935 *
2936 * i) A single cblock, eg. '3456'
2937 * ii) A begin and end cblock with dots between, eg. 123-234
2938 */
2939static int parse_cblock_range(struct cache *cache, const char *str,
2940 struct cblock_range *result)
2941{
2942 char dummy;
2943 uint64_t b, e;
2944 int r;
2945
2946 /*
2947 * Try and parse form (ii) first.
2948 */
2949 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
2950 if (r < 0)
2951 return r;
2952
2953 if (r == 2) {
2954 result->begin = to_cblock(b);
2955 result->end = to_cblock(e);
2956 return 0;
2957 }
2958
2959 /*
2960 * That didn't work, try form (i).
2961 */
2962 r = sscanf(str, "%llu%c", &b, &dummy);
2963 if (r < 0)
2964 return r;
2965
2966 if (r == 1) {
2967 result->begin = to_cblock(b);
2968 result->end = to_cblock(from_cblock(result->begin) + 1u);
2969 return 0;
2970 }
2971
2972 DMERR("invalid cblock range '%s'", str);
2973 return -EINVAL;
2974}
2975
2976static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
2977{
2978 uint64_t b = from_cblock(range->begin);
2979 uint64_t e = from_cblock(range->end);
2980 uint64_t n = from_cblock(cache->cache_size);
2981
2982 if (b >= n) {
2983 DMERR("begin cblock out of range: %llu >= %llu", b, n);
2984 return -EINVAL;
2985 }
2986
2987 if (e > n) {
2988 DMERR("end cblock out of range: %llu > %llu", e, n);
2989 return -EINVAL;
2990 }
2991
2992 if (b >= e) {
2993 DMERR("invalid cblock range: %llu >= %llu", b, e);
2994 return -EINVAL;
2995 }
2996
2997 return 0;
2998}
2999
3000static int request_invalidation(struct cache *cache, struct cblock_range *range)
3001{
3002 struct invalidation_request req;
3003
3004 INIT_LIST_HEAD(&req.list);
3005 req.cblocks = range;
3006 atomic_set(&req.complete, 0);
3007 req.err = 0;
3008 init_waitqueue_head(&req.result_wait);
3009
3010 spin_lock(&cache->invalidation_lock);
3011 list_add(&req.list, &cache->invalidation_requests);
3012 spin_unlock(&cache->invalidation_lock);
3013 wake_worker(cache);
3014
3015 wait_event(req.result_wait, atomic_read(&req.complete));
3016 return req.err;
3017}
3018
3019static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
3020 const char **cblock_ranges)
3021{
3022 int r = 0;
3023 unsigned i;
3024 struct cblock_range range;
3025
3026 if (!passthrough_mode(&cache->features)) {
3027 DMERR("cache has to be in passthrough mode for invalidation");
3028 return -EPERM;
3029 }
3030
3031 for (i = 0; i < count; i++) {
3032 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3033 if (r)
3034 break;
3035
3036 r = validate_cblock_range(cache, &range);
3037 if (r)
3038 break;
3039
3040 /*
3041 * Pass begin and end origin blocks to the worker and wake it.
3042 */
3043 r = request_invalidation(cache, &range);
3044 if (r)
3045 break;
3046 }
3047
3048 return r;
3049}
3050
3051/*
3052 * Supports
3053 * "<key> <value>"
3054 * and
3055 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003056 *
3057 * The key migration_threshold is supported by the cache target core.
3058 */
3059static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3060{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003061 struct cache *cache = ti->private;
3062
Joe Thornber65790ff2013-11-08 16:39:50 +00003063 if (!argc)
3064 return -EINVAL;
3065
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -05003066 if (!strcasecmp(argv[0], "invalidate_cblocks"))
Joe Thornber65790ff2013-11-08 16:39:50 +00003067 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3068
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003069 if (argc != 2)
3070 return -EINVAL;
3071
Joe Thornber2f14f4b2013-05-10 14:37:21 +01003072 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003073}
3074
3075static int cache_iterate_devices(struct dm_target *ti,
3076 iterate_devices_callout_fn fn, void *data)
3077{
3078 int r = 0;
3079 struct cache *cache = ti->private;
3080
3081 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3082 if (!r)
3083 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3084
3085 return r;
3086}
3087
3088/*
3089 * We assume I/O is going to the origin (which is the volume
3090 * more likely to have restrictions e.g. by being striped).
3091 * (Looking up the exact location of the data would be expensive
3092 * and could always be out of date by the time the bio is submitted.)
3093 */
3094static int cache_bvec_merge(struct dm_target *ti,
3095 struct bvec_merge_data *bvm,
3096 struct bio_vec *biovec, int max_size)
3097{
3098 struct cache *cache = ti->private;
3099 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3100
3101 if (!q->merge_bvec_fn)
3102 return max_size;
3103
3104 bvm->bi_bdev = cache->origin_dev->bdev;
3105 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3106}
3107
3108static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3109{
3110 /*
3111 * FIXME: these limits may be incompatible with the cache device
3112 */
3113 limits->max_discard_sectors = cache->discard_block_size * 1024;
3114 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
3115}
3116
3117static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3118{
3119 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04003120 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003121
Mike Snitzerf6109372013-08-20 15:02:41 -04003122 /*
3123 * If the system-determined stacked limits are compatible with the
3124 * cache's blocksize (io_opt is a factor) do not override them.
3125 */
3126 if (io_opt_sectors < cache->sectors_per_block ||
3127 do_div(io_opt_sectors, cache->sectors_per_block)) {
3128 blk_limits_io_min(limits, 0);
3129 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3130 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003131 set_discard_limits(cache, limits);
3132}
3133
3134/*----------------------------------------------------------------*/
3135
3136static struct target_type cache_target = {
3137 .name = "cache",
Mike Snitzer6a388612014-01-09 16:04:12 -05003138 .version = {1, 3, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003139 .module = THIS_MODULE,
3140 .ctr = cache_ctr,
3141 .dtr = cache_dtr,
3142 .map = cache_map,
3143 .end_io = cache_end_io,
3144 .postsuspend = cache_postsuspend,
3145 .preresume = cache_preresume,
3146 .resume = cache_resume,
3147 .status = cache_status,
3148 .message = cache_message,
3149 .iterate_devices = cache_iterate_devices,
3150 .merge = cache_bvec_merge,
3151 .io_hints = cache_io_hints,
3152};
3153
3154static int __init dm_cache_init(void)
3155{
3156 int r;
3157
3158 r = dm_register_target(&cache_target);
3159 if (r) {
3160 DMERR("cache target registration failed: %d", r);
3161 return r;
3162 }
3163
3164 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3165 if (!migration_cache) {
3166 dm_unregister_target(&cache_target);
3167 return -ENOMEM;
3168 }
3169
3170 return 0;
3171}
3172
3173static void __exit dm_cache_exit(void)
3174{
3175 dm_unregister_target(&cache_target);
3176 kmem_cache_destroy(migration_cache);
3177}
3178
3179module_init(dm_cache_init);
3180module_exit(dm_cache_exit);
3181
3182MODULE_DESCRIPTION(DM_NAME " cache target");
3183MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3184MODULE_LICENSE("GPL");