blob: 25d3253e72d09b3c2263d4dfa4d68355b7ebbce1 [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
64#define PRISON_CELLS 1024
65#define MIGRATION_POOL_SIZE 128
66#define COMMIT_PERIOD HZ
67#define MIGRATION_COUNT_WINDOW 10
68
69/*
Mike Snitzer05473042013-08-16 10:54:19 -040070 * The block size of the device holding cache data must be
71 * between 32KB and 1GB.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000072 */
73#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
Mike Snitzer05473042013-08-16 10:54:19 -040074#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000075
76/*
77 * FIXME: the cache is read/write for the time being.
78 */
79enum cache_mode {
80 CM_WRITE, /* metadata may be changed */
81 CM_READ_ONLY, /* metadata may not be changed */
82};
83
84struct cache_features {
85 enum cache_mode mode;
86 bool write_through:1;
87};
88
89struct cache_stats {
90 atomic_t read_hit;
91 atomic_t read_miss;
92 atomic_t write_hit;
93 atomic_t write_miss;
94 atomic_t demotion;
95 atomic_t promotion;
96 atomic_t copies_avoided;
97 atomic_t cache_cell_clash;
98 atomic_t commit_count;
99 atomic_t discard_count;
100};
101
102struct cache {
103 struct dm_target *ti;
104 struct dm_target_callbacks callbacks;
105
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400106 struct dm_cache_metadata *cmd;
107
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000108 /*
109 * Metadata is written to this device.
110 */
111 struct dm_dev *metadata_dev;
112
113 /*
114 * The slower of the two data devices. Typically a spindle.
115 */
116 struct dm_dev *origin_dev;
117
118 /*
119 * The faster of the two data devices. Typically an SSD.
120 */
121 struct dm_dev *cache_dev;
122
123 /*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000124 * Size of the origin device in _complete_ blocks and native sectors.
125 */
126 dm_oblock_t origin_blocks;
127 sector_t origin_sectors;
128
129 /*
130 * Size of the cache device in blocks.
131 */
132 dm_cblock_t cache_size;
133
134 /*
135 * Fields for converting from sectors to blocks.
136 */
137 uint32_t sectors_per_block;
138 int sectors_per_block_shift;
139
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000140 spinlock_t lock;
141 struct bio_list deferred_bios;
142 struct bio_list deferred_flush_bios;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000143 struct bio_list deferred_writethrough_bios;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000144 struct list_head quiesced_migrations;
145 struct list_head completed_migrations;
146 struct list_head need_commit_migrations;
147 sector_t migration_threshold;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000148 wait_queue_head_t migration_wait;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400149 atomic_t nr_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000150
Joe Thornber66cb1912013-10-30 17:11:58 +0000151 wait_queue_head_t quiescing_wait;
152 atomic_t quiescing_ack;
153
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000154 /*
155 * cache_size entries, dirty if set
156 */
157 dm_cblock_t nr_dirty;
158 unsigned long *dirty_bitset;
159
160 /*
161 * origin_blocks entries, discarded if set.
162 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000163 dm_dblock_t discard_nr_blocks;
164 unsigned long *discard_bitset;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400165 uint32_t discard_block_size; /* a power of 2 times sectors per block */
166
167 /*
168 * Rather than reconstructing the table line for the status we just
169 * save it and regurgitate.
170 */
171 unsigned nr_ctr_args;
172 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000173
174 struct dm_kcopyd_client *copier;
175 struct workqueue_struct *wq;
176 struct work_struct worker;
177
178 struct delayed_work waker;
179 unsigned long last_commit_jiffies;
180
181 struct dm_bio_prison *prison;
182 struct dm_deferred_set *all_io_ds;
183
184 mempool_t *migration_pool;
185 struct dm_cache_migration *next_migration;
186
187 struct dm_cache_policy *policy;
188 unsigned policy_nr_args;
189
190 bool need_tick_bio:1;
191 bool sized:1;
192 bool quiescing:1;
193 bool commit_requested:1;
194 bool loaded_mappings:1;
195 bool loaded_discards:1;
196
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000197 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400198 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000199 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400200 struct cache_features features;
201
202 struct cache_stats stats;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000203};
204
205struct per_bio_data {
206 bool tick:1;
207 unsigned req_nr:2;
208 struct dm_deferred_entry *all_io_entry;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000209
Mike Snitzer19b00922013-04-05 15:36:34 +0100210 /*
211 * writethrough fields. These MUST remain at the end of this
212 * structure and the 'cache' member must be the first as it
Joe Thornberaeed14202013-05-10 14:37:18 +0100213 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100214 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000215 struct cache *cache;
216 dm_cblock_t cblock;
217 bio_end_io_t *saved_bi_end_io;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100218 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000219};
220
221struct dm_cache_migration {
222 struct list_head list;
223 struct cache *cache;
224
225 unsigned long start_jiffies;
226 dm_oblock_t old_oblock;
227 dm_oblock_t new_oblock;
228 dm_cblock_t cblock;
229
230 bool err:1;
231 bool writeback:1;
232 bool demote:1;
233 bool promote:1;
234
235 struct dm_bio_prison_cell *old_ocell;
236 struct dm_bio_prison_cell *new_ocell;
237};
238
239/*
240 * Processing a bio in the worker thread may require these memory
241 * allocations. We prealloc to avoid deadlocks (the same worker thread
242 * frees them back to the mempool).
243 */
244struct prealloc {
245 struct dm_cache_migration *mg;
246 struct dm_bio_prison_cell *cell1;
247 struct dm_bio_prison_cell *cell2;
248};
249
250static void wake_worker(struct cache *cache)
251{
252 queue_work(cache->wq, &cache->worker);
253}
254
255/*----------------------------------------------------------------*/
256
257static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
258{
259 /* FIXME: change to use a local slab. */
260 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
261}
262
263static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
264{
265 dm_bio_prison_free_cell(cache->prison, cell);
266}
267
268static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
269{
270 if (!p->mg) {
271 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
272 if (!p->mg)
273 return -ENOMEM;
274 }
275
276 if (!p->cell1) {
277 p->cell1 = alloc_prison_cell(cache);
278 if (!p->cell1)
279 return -ENOMEM;
280 }
281
282 if (!p->cell2) {
283 p->cell2 = alloc_prison_cell(cache);
284 if (!p->cell2)
285 return -ENOMEM;
286 }
287
288 return 0;
289}
290
291static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
292{
293 if (p->cell2)
294 free_prison_cell(cache, p->cell2);
295
296 if (p->cell1)
297 free_prison_cell(cache, p->cell1);
298
299 if (p->mg)
300 mempool_free(p->mg, cache->migration_pool);
301}
302
303static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
304{
305 struct dm_cache_migration *mg = p->mg;
306
307 BUG_ON(!mg);
308 p->mg = NULL;
309
310 return mg;
311}
312
313/*
314 * You must have a cell within the prealloc struct to return. If not this
315 * function will BUG() rather than returning NULL.
316 */
317static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
318{
319 struct dm_bio_prison_cell *r = NULL;
320
321 if (p->cell1) {
322 r = p->cell1;
323 p->cell1 = NULL;
324
325 } else if (p->cell2) {
326 r = p->cell2;
327 p->cell2 = NULL;
328 } else
329 BUG();
330
331 return r;
332}
333
334/*
335 * You can't have more than two cells in a prealloc struct. BUG() will be
336 * called if you try and overfill.
337 */
338static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
339{
340 if (!p->cell2)
341 p->cell2 = cell;
342
343 else if (!p->cell1)
344 p->cell1 = cell;
345
346 else
347 BUG();
348}
349
350/*----------------------------------------------------------------*/
351
352static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
353{
354 key->virtual = 0;
355 key->dev = 0;
356 key->block = from_oblock(oblock);
357}
358
359/*
360 * The caller hands in a preallocated cell, and a free function for it.
361 * The cell will be freed if there's an error, or if it wasn't used because
362 * a cell with that key already exists.
363 */
364typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
365
366static int bio_detain(struct cache *cache, dm_oblock_t oblock,
367 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
368 cell_free_fn free_fn, void *free_context,
369 struct dm_bio_prison_cell **cell_result)
370{
371 int r;
372 struct dm_cell_key key;
373
374 build_key(oblock, &key);
375 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
376 if (r)
377 free_fn(free_context, cell_prealloc);
378
379 return r;
380}
381
382static int get_cell(struct cache *cache,
383 dm_oblock_t oblock,
384 struct prealloc *structs,
385 struct dm_bio_prison_cell **cell_result)
386{
387 int r;
388 struct dm_cell_key key;
389 struct dm_bio_prison_cell *cell_prealloc;
390
391 cell_prealloc = prealloc_get_cell(structs);
392
393 build_key(oblock, &key);
394 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
395 if (r)
396 prealloc_put_cell(structs, cell_prealloc);
397
398 return r;
399}
400
Joe Thornberaeed14202013-05-10 14:37:18 +0100401/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000402
403static bool is_dirty(struct cache *cache, dm_cblock_t b)
404{
405 return test_bit(from_cblock(b), cache->dirty_bitset);
406}
407
408static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
409{
410 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
411 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) + 1);
412 policy_set_dirty(cache->policy, oblock);
413 }
414}
415
416static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
417{
418 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
419 policy_clear_dirty(cache->policy, oblock);
420 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) - 1);
421 if (!from_cblock(cache->nr_dirty))
422 dm_table_event(cache->ti->table);
423 }
424}
425
426/*----------------------------------------------------------------*/
Joe Thornberaeed14202013-05-10 14:37:18 +0100427
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000428static bool block_size_is_power_of_two(struct cache *cache)
429{
430 return cache->sectors_per_block_shift >= 0;
431}
432
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100433/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
434#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
435__always_inline
436#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000437static dm_block_t block_div(dm_block_t b, uint32_t n)
438{
439 do_div(b, n);
440
441 return b;
442}
443
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000444static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
445{
Joe Thornber414dd672013-03-20 17:21:25 +0000446 uint32_t discard_blocks = cache->discard_block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000447 dm_block_t b = from_oblock(oblock);
448
449 if (!block_size_is_power_of_two(cache))
Joe Thornber414dd672013-03-20 17:21:25 +0000450 discard_blocks = discard_blocks / cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000451 else
452 discard_blocks >>= cache->sectors_per_block_shift;
453
Joe Thornber414dd672013-03-20 17:21:25 +0000454 b = block_div(b, discard_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000455
456 return to_dblock(b);
457}
458
459static void set_discard(struct cache *cache, dm_dblock_t b)
460{
461 unsigned long flags;
462
463 atomic_inc(&cache->stats.discard_count);
464
465 spin_lock_irqsave(&cache->lock, flags);
466 set_bit(from_dblock(b), cache->discard_bitset);
467 spin_unlock_irqrestore(&cache->lock, flags);
468}
469
470static void clear_discard(struct cache *cache, dm_dblock_t b)
471{
472 unsigned long flags;
473
474 spin_lock_irqsave(&cache->lock, flags);
475 clear_bit(from_dblock(b), cache->discard_bitset);
476 spin_unlock_irqrestore(&cache->lock, flags);
477}
478
479static bool is_discarded(struct cache *cache, dm_dblock_t b)
480{
481 int r;
482 unsigned long flags;
483
484 spin_lock_irqsave(&cache->lock, flags);
485 r = test_bit(from_dblock(b), cache->discard_bitset);
486 spin_unlock_irqrestore(&cache->lock, flags);
487
488 return r;
489}
490
491static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
492{
493 int r;
494 unsigned long flags;
495
496 spin_lock_irqsave(&cache->lock, flags);
497 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
498 cache->discard_bitset);
499 spin_unlock_irqrestore(&cache->lock, flags);
500
501 return r;
502}
503
504/*----------------------------------------------------------------*/
505
506static void load_stats(struct cache *cache)
507{
508 struct dm_cache_statistics stats;
509
510 dm_cache_metadata_get_stats(cache->cmd, &stats);
511 atomic_set(&cache->stats.read_hit, stats.read_hits);
512 atomic_set(&cache->stats.read_miss, stats.read_misses);
513 atomic_set(&cache->stats.write_hit, stats.write_hits);
514 atomic_set(&cache->stats.write_miss, stats.write_misses);
515}
516
517static void save_stats(struct cache *cache)
518{
519 struct dm_cache_statistics stats;
520
521 stats.read_hits = atomic_read(&cache->stats.read_hit);
522 stats.read_misses = atomic_read(&cache->stats.read_miss);
523 stats.write_hits = atomic_read(&cache->stats.write_hit);
524 stats.write_misses = atomic_read(&cache->stats.write_miss);
525
526 dm_cache_metadata_set_stats(cache->cmd, &stats);
527}
528
529/*----------------------------------------------------------------
530 * Per bio data
531 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100532
533/*
534 * If using writeback, leave out struct per_bio_data's writethrough fields.
535 */
536#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
537#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
538
539static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000540{
Mike Snitzer19b00922013-04-05 15:36:34 +0100541 return cache->features.write_through ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
542}
543
544static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
545{
546 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000547 BUG_ON(!pb);
548 return pb;
549}
550
Mike Snitzer19b00922013-04-05 15:36:34 +0100551static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000552{
Mike Snitzer19b00922013-04-05 15:36:34 +0100553 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000554
555 pb->tick = false;
556 pb->req_nr = dm_bio_get_target_bio_nr(bio);
557 pb->all_io_entry = NULL;
558
559 return pb;
560}
561
562/*----------------------------------------------------------------
563 * Remapping
564 *--------------------------------------------------------------*/
565static void remap_to_origin(struct cache *cache, struct bio *bio)
566{
567 bio->bi_bdev = cache->origin_dev->bdev;
568}
569
570static void remap_to_cache(struct cache *cache, struct bio *bio,
571 dm_cblock_t cblock)
572{
573 sector_t bi_sector = bio->bi_sector;
574
575 bio->bi_bdev = cache->cache_dev->bdev;
576 if (!block_size_is_power_of_two(cache))
577 bio->bi_sector = (from_cblock(cblock) * cache->sectors_per_block) +
578 sector_div(bi_sector, cache->sectors_per_block);
579 else
580 bio->bi_sector = (from_cblock(cblock) << cache->sectors_per_block_shift) |
581 (bi_sector & (cache->sectors_per_block - 1));
582}
583
584static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
585{
586 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100587 size_t pb_data_size = get_per_bio_data_size(cache);
588 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000589
590 spin_lock_irqsave(&cache->lock, flags);
591 if (cache->need_tick_bio &&
592 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
593 pb->tick = true;
594 cache->need_tick_bio = false;
595 }
596 spin_unlock_irqrestore(&cache->lock, flags);
597}
598
599static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
600 dm_oblock_t oblock)
601{
602 check_if_tick_bio_needed(cache, bio);
603 remap_to_origin(cache, bio);
604 if (bio_data_dir(bio) == WRITE)
605 clear_discard(cache, oblock_to_dblock(cache, oblock));
606}
607
608static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
609 dm_oblock_t oblock, dm_cblock_t cblock)
610{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100611 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000612 remap_to_cache(cache, bio, cblock);
613 if (bio_data_dir(bio) == WRITE) {
614 set_dirty(cache, oblock, cblock);
615 clear_discard(cache, oblock_to_dblock(cache, oblock));
616 }
617}
618
619static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
620{
621 sector_t block_nr = bio->bi_sector;
622
623 if (!block_size_is_power_of_two(cache))
624 (void) sector_div(block_nr, cache->sectors_per_block);
625 else
626 block_nr >>= cache->sectors_per_block_shift;
627
628 return to_oblock(block_nr);
629}
630
631static int bio_triggers_commit(struct cache *cache, struct bio *bio)
632{
633 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
634}
635
636static void issue(struct cache *cache, struct bio *bio)
637{
638 unsigned long flags;
639
640 if (!bio_triggers_commit(cache, bio)) {
641 generic_make_request(bio);
642 return;
643 }
644
645 /*
646 * Batch together any bios that trigger commits and then issue a
647 * single commit for them in do_worker().
648 */
649 spin_lock_irqsave(&cache->lock, flags);
650 cache->commit_requested = true;
651 bio_list_add(&cache->deferred_flush_bios, bio);
652 spin_unlock_irqrestore(&cache->lock, flags);
653}
654
Joe Thornbere2e74d62013-03-20 17:21:27 +0000655static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
656{
657 unsigned long flags;
658
659 spin_lock_irqsave(&cache->lock, flags);
660 bio_list_add(&cache->deferred_writethrough_bios, bio);
661 spin_unlock_irqrestore(&cache->lock, flags);
662
663 wake_worker(cache);
664}
665
666static void writethrough_endio(struct bio *bio, int err)
667{
Mike Snitzer19b00922013-04-05 15:36:34 +0100668 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000669 bio->bi_end_io = pb->saved_bi_end_io;
670
671 if (err) {
672 bio_endio(bio, err);
673 return;
674 }
675
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100676 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000677 remap_to_cache(pb->cache, bio, pb->cblock);
678
679 /*
680 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed14202013-05-10 14:37:18 +0100681 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000682 * worker thread.
683 */
684 defer_writethrough_bio(pb->cache, bio);
685}
686
687/*
688 * When running in writethrough mode we need to send writes to clean blocks
689 * to both the cache and origin devices. In future we'd like to clone the
690 * bio and send them in parallel, but for now we're doing them in
691 * series as this is easier.
692 */
693static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
694 dm_oblock_t oblock, dm_cblock_t cblock)
695{
Mike Snitzer19b00922013-04-05 15:36:34 +0100696 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000697
698 pb->cache = cache;
699 pb->cblock = cblock;
700 pb->saved_bi_end_io = bio->bi_end_io;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100701 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000702 bio->bi_end_io = writethrough_endio;
703
704 remap_to_origin_clear_discard(pb->cache, bio, oblock);
705}
706
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000707/*----------------------------------------------------------------
708 * Migration processing
709 *
710 * Migration covers moving data from the origin device to the cache, or
711 * vice versa.
712 *--------------------------------------------------------------*/
713static void free_migration(struct dm_cache_migration *mg)
714{
715 mempool_free(mg, mg->cache->migration_pool);
716}
717
718static void inc_nr_migrations(struct cache *cache)
719{
720 atomic_inc(&cache->nr_migrations);
721}
722
723static void dec_nr_migrations(struct cache *cache)
724{
725 atomic_dec(&cache->nr_migrations);
726
727 /*
728 * Wake the worker in case we're suspending the target.
729 */
730 wake_up(&cache->migration_wait);
731}
732
733static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
734 bool holder)
735{
736 (holder ? dm_cell_release : dm_cell_release_no_holder)
737 (cache->prison, cell, &cache->deferred_bios);
738 free_prison_cell(cache, cell);
739}
740
741static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
742 bool holder)
743{
744 unsigned long flags;
745
746 spin_lock_irqsave(&cache->lock, flags);
747 __cell_defer(cache, cell, holder);
748 spin_unlock_irqrestore(&cache->lock, flags);
749
750 wake_worker(cache);
751}
752
753static void cleanup_migration(struct dm_cache_migration *mg)
754{
Joe Thornber66cb1912013-10-30 17:11:58 +0000755 struct cache *cache = mg->cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000756 free_migration(mg);
Joe Thornber66cb1912013-10-30 17:11:58 +0000757 dec_nr_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000758}
759
760static void migration_failure(struct dm_cache_migration *mg)
761{
762 struct cache *cache = mg->cache;
763
764 if (mg->writeback) {
765 DMWARN_LIMIT("writeback failed; couldn't copy block");
766 set_dirty(cache, mg->old_oblock, mg->cblock);
767 cell_defer(cache, mg->old_ocell, false);
768
769 } else if (mg->demote) {
770 DMWARN_LIMIT("demotion failed; couldn't copy block");
771 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
772
773 cell_defer(cache, mg->old_ocell, mg->promote ? 0 : 1);
774 if (mg->promote)
775 cell_defer(cache, mg->new_ocell, 1);
776 } else {
777 DMWARN_LIMIT("promotion failed; couldn't copy block");
778 policy_remove_mapping(cache->policy, mg->new_oblock);
779 cell_defer(cache, mg->new_ocell, 1);
780 }
781
782 cleanup_migration(mg);
783}
784
785static void migration_success_pre_commit(struct dm_cache_migration *mg)
786{
787 unsigned long flags;
788 struct cache *cache = mg->cache;
789
790 if (mg->writeback) {
791 cell_defer(cache, mg->old_ocell, false);
792 clear_dirty(cache, mg->old_oblock, mg->cblock);
793 cleanup_migration(mg);
794 return;
795
796 } else if (mg->demote) {
797 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
798 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
799 policy_force_mapping(cache->policy, mg->new_oblock,
800 mg->old_oblock);
801 if (mg->promote)
802 cell_defer(cache, mg->new_ocell, true);
803 cleanup_migration(mg);
804 return;
805 }
806 } else {
807 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
808 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
809 policy_remove_mapping(cache->policy, mg->new_oblock);
810 cleanup_migration(mg);
811 return;
812 }
813 }
814
815 spin_lock_irqsave(&cache->lock, flags);
816 list_add_tail(&mg->list, &cache->need_commit_migrations);
817 cache->commit_requested = true;
818 spin_unlock_irqrestore(&cache->lock, flags);
819}
820
821static void migration_success_post_commit(struct dm_cache_migration *mg)
822{
823 unsigned long flags;
824 struct cache *cache = mg->cache;
825
826 if (mg->writeback) {
827 DMWARN("writeback unexpectedly triggered commit");
828 return;
829
830 } else if (mg->demote) {
831 cell_defer(cache, mg->old_ocell, mg->promote ? 0 : 1);
832
833 if (mg->promote) {
834 mg->demote = false;
835
836 spin_lock_irqsave(&cache->lock, flags);
837 list_add_tail(&mg->list, &cache->quiesced_migrations);
838 spin_unlock_irqrestore(&cache->lock, flags);
839
840 } else
841 cleanup_migration(mg);
842
843 } else {
844 cell_defer(cache, mg->new_ocell, true);
845 clear_dirty(cache, mg->new_oblock, mg->cblock);
846 cleanup_migration(mg);
847 }
848}
849
850static void copy_complete(int read_err, unsigned long write_err, void *context)
851{
852 unsigned long flags;
853 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
854 struct cache *cache = mg->cache;
855
856 if (read_err || write_err)
857 mg->err = true;
858
859 spin_lock_irqsave(&cache->lock, flags);
860 list_add_tail(&mg->list, &cache->completed_migrations);
861 spin_unlock_irqrestore(&cache->lock, flags);
862
863 wake_worker(cache);
864}
865
866static void issue_copy_real(struct dm_cache_migration *mg)
867{
868 int r;
869 struct dm_io_region o_region, c_region;
870 struct cache *cache = mg->cache;
871
872 o_region.bdev = cache->origin_dev->bdev;
873 o_region.count = cache->sectors_per_block;
874
875 c_region.bdev = cache->cache_dev->bdev;
876 c_region.sector = from_cblock(mg->cblock) * cache->sectors_per_block;
877 c_region.count = cache->sectors_per_block;
878
879 if (mg->writeback || mg->demote) {
880 /* demote */
881 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
882 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
883 } else {
884 /* promote */
885 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
886 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
887 }
888
889 if (r < 0)
890 migration_failure(mg);
891}
892
893static void avoid_copy(struct dm_cache_migration *mg)
894{
895 atomic_inc(&mg->cache->stats.copies_avoided);
896 migration_success_pre_commit(mg);
897}
898
899static void issue_copy(struct dm_cache_migration *mg)
900{
901 bool avoid;
902 struct cache *cache = mg->cache;
903
904 if (mg->writeback || mg->demote)
905 avoid = !is_dirty(cache, mg->cblock) ||
906 is_discarded_oblock(cache, mg->old_oblock);
907 else
908 avoid = is_discarded_oblock(cache, mg->new_oblock);
909
910 avoid ? avoid_copy(mg) : issue_copy_real(mg);
911}
912
913static void complete_migration(struct dm_cache_migration *mg)
914{
915 if (mg->err)
916 migration_failure(mg);
917 else
918 migration_success_pre_commit(mg);
919}
920
921static void process_migrations(struct cache *cache, struct list_head *head,
922 void (*fn)(struct dm_cache_migration *))
923{
924 unsigned long flags;
925 struct list_head list;
926 struct dm_cache_migration *mg, *tmp;
927
928 INIT_LIST_HEAD(&list);
929 spin_lock_irqsave(&cache->lock, flags);
930 list_splice_init(head, &list);
931 spin_unlock_irqrestore(&cache->lock, flags);
932
933 list_for_each_entry_safe(mg, tmp, &list, list)
934 fn(mg);
935}
936
937static void __queue_quiesced_migration(struct dm_cache_migration *mg)
938{
939 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
940}
941
942static void queue_quiesced_migration(struct dm_cache_migration *mg)
943{
944 unsigned long flags;
945 struct cache *cache = mg->cache;
946
947 spin_lock_irqsave(&cache->lock, flags);
948 __queue_quiesced_migration(mg);
949 spin_unlock_irqrestore(&cache->lock, flags);
950
951 wake_worker(cache);
952}
953
954static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
955{
956 unsigned long flags;
957 struct dm_cache_migration *mg, *tmp;
958
959 spin_lock_irqsave(&cache->lock, flags);
960 list_for_each_entry_safe(mg, tmp, work, list)
961 __queue_quiesced_migration(mg);
962 spin_unlock_irqrestore(&cache->lock, flags);
963
964 wake_worker(cache);
965}
966
967static void check_for_quiesced_migrations(struct cache *cache,
968 struct per_bio_data *pb)
969{
970 struct list_head work;
971
972 if (!pb->all_io_entry)
973 return;
974
975 INIT_LIST_HEAD(&work);
976 if (pb->all_io_entry)
977 dm_deferred_entry_dec(pb->all_io_entry, &work);
978
979 if (!list_empty(&work))
980 queue_quiesced_migrations(cache, &work);
981}
982
983static void quiesce_migration(struct dm_cache_migration *mg)
984{
985 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
986 queue_quiesced_migration(mg);
987}
988
989static void promote(struct cache *cache, struct prealloc *structs,
990 dm_oblock_t oblock, dm_cblock_t cblock,
991 struct dm_bio_prison_cell *cell)
992{
993 struct dm_cache_migration *mg = prealloc_get_migration(structs);
994
995 mg->err = false;
996 mg->writeback = false;
997 mg->demote = false;
998 mg->promote = true;
999 mg->cache = cache;
1000 mg->new_oblock = oblock;
1001 mg->cblock = cblock;
1002 mg->old_ocell = NULL;
1003 mg->new_ocell = cell;
1004 mg->start_jiffies = jiffies;
1005
1006 inc_nr_migrations(cache);
1007 quiesce_migration(mg);
1008}
1009
1010static void writeback(struct cache *cache, struct prealloc *structs,
1011 dm_oblock_t oblock, dm_cblock_t cblock,
1012 struct dm_bio_prison_cell *cell)
1013{
1014 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1015
1016 mg->err = false;
1017 mg->writeback = true;
1018 mg->demote = false;
1019 mg->promote = false;
1020 mg->cache = cache;
1021 mg->old_oblock = oblock;
1022 mg->cblock = cblock;
1023 mg->old_ocell = cell;
1024 mg->new_ocell = NULL;
1025 mg->start_jiffies = jiffies;
1026
1027 inc_nr_migrations(cache);
1028 quiesce_migration(mg);
1029}
1030
1031static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1032 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1033 dm_cblock_t cblock,
1034 struct dm_bio_prison_cell *old_ocell,
1035 struct dm_bio_prison_cell *new_ocell)
1036{
1037 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1038
1039 mg->err = false;
1040 mg->writeback = false;
1041 mg->demote = true;
1042 mg->promote = true;
1043 mg->cache = cache;
1044 mg->old_oblock = old_oblock;
1045 mg->new_oblock = new_oblock;
1046 mg->cblock = cblock;
1047 mg->old_ocell = old_ocell;
1048 mg->new_ocell = new_ocell;
1049 mg->start_jiffies = jiffies;
1050
1051 inc_nr_migrations(cache);
1052 quiesce_migration(mg);
1053}
1054
1055/*----------------------------------------------------------------
1056 * bio processing
1057 *--------------------------------------------------------------*/
1058static void defer_bio(struct cache *cache, struct bio *bio)
1059{
1060 unsigned long flags;
1061
1062 spin_lock_irqsave(&cache->lock, flags);
1063 bio_list_add(&cache->deferred_bios, bio);
1064 spin_unlock_irqrestore(&cache->lock, flags);
1065
1066 wake_worker(cache);
1067}
1068
1069static void process_flush_bio(struct cache *cache, struct bio *bio)
1070{
Mike Snitzer19b00922013-04-05 15:36:34 +01001071 size_t pb_data_size = get_per_bio_data_size(cache);
1072 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001073
1074 BUG_ON(bio->bi_size);
1075 if (!pb->req_nr)
1076 remap_to_origin(cache, bio);
1077 else
1078 remap_to_cache(cache, bio, 0);
1079
1080 issue(cache, bio);
1081}
1082
1083/*
1084 * People generally discard large parts of a device, eg, the whole device
1085 * when formatting. Splitting these large discards up into cache block
1086 * sized ios and then quiescing (always neccessary for discard) takes too
1087 * long.
1088 *
1089 * We keep it simple, and allow any size of discard to come in, and just
1090 * mark off blocks on the discard bitset. No passdown occurs!
1091 *
1092 * To implement passdown we need to change the bio_prison such that a cell
1093 * can have a key that spans many blocks.
1094 */
1095static void process_discard_bio(struct cache *cache, struct bio *bio)
1096{
1097 dm_block_t start_block = dm_sector_div_up(bio->bi_sector,
1098 cache->discard_block_size);
1099 dm_block_t end_block = bio->bi_sector + bio_sectors(bio);
1100 dm_block_t b;
1101
Joe Thornber414dd672013-03-20 17:21:25 +00001102 end_block = block_div(end_block, cache->discard_block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001103
1104 for (b = start_block; b < end_block; b++)
1105 set_discard(cache, to_dblock(b));
1106
1107 bio_endio(bio, 0);
1108}
1109
1110static bool spare_migration_bandwidth(struct cache *cache)
1111{
1112 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1113 cache->sectors_per_block;
1114 return current_volume < cache->migration_threshold;
1115}
1116
1117static bool is_writethrough_io(struct cache *cache, struct bio *bio,
1118 dm_cblock_t cblock)
1119{
1120 return bio_data_dir(bio) == WRITE &&
1121 cache->features.write_through && !is_dirty(cache, cblock);
1122}
1123
1124static void inc_hit_counter(struct cache *cache, struct bio *bio)
1125{
1126 atomic_inc(bio_data_dir(bio) == READ ?
1127 &cache->stats.read_hit : &cache->stats.write_hit);
1128}
1129
1130static void inc_miss_counter(struct cache *cache, struct bio *bio)
1131{
1132 atomic_inc(bio_data_dir(bio) == READ ?
1133 &cache->stats.read_miss : &cache->stats.write_miss);
1134}
1135
1136static void process_bio(struct cache *cache, struct prealloc *structs,
1137 struct bio *bio)
1138{
1139 int r;
1140 bool release_cell = true;
1141 dm_oblock_t block = get_bio_block(cache, bio);
1142 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1143 struct policy_result lookup_result;
Mike Snitzer19b00922013-04-05 15:36:34 +01001144 size_t pb_data_size = get_per_bio_data_size(cache);
1145 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001146 bool discarded_block = is_discarded_oblock(cache, block);
1147 bool can_migrate = discarded_block || spare_migration_bandwidth(cache);
1148
1149 /*
1150 * Check to see if that block is currently migrating.
1151 */
1152 cell_prealloc = prealloc_get_cell(structs);
1153 r = bio_detain(cache, block, bio, cell_prealloc,
1154 (cell_free_fn) prealloc_put_cell,
1155 structs, &new_ocell);
1156 if (r > 0)
1157 return;
1158
1159 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1160 bio, &lookup_result);
1161
1162 if (r == -EWOULDBLOCK)
1163 /* migration has been denied */
1164 lookup_result.op = POLICY_MISS;
1165
1166 switch (lookup_result.op) {
1167 case POLICY_HIT:
1168 inc_hit_counter(cache, bio);
1169 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1170
Joe Thornbere2e74d62013-03-20 17:21:27 +00001171 if (is_writethrough_io(cache, bio, lookup_result.cblock))
1172 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
1173 else
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001174 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1175
1176 issue(cache, bio);
1177 break;
1178
1179 case POLICY_MISS:
1180 inc_miss_counter(cache, bio);
1181 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001182 remap_to_origin_clear_discard(cache, bio, block);
1183 issue(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001184 break;
1185
1186 case POLICY_NEW:
1187 atomic_inc(&cache->stats.promotion);
1188 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1189 release_cell = false;
1190 break;
1191
1192 case POLICY_REPLACE:
1193 cell_prealloc = prealloc_get_cell(structs);
1194 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1195 (cell_free_fn) prealloc_put_cell,
1196 structs, &old_ocell);
1197 if (r > 0) {
1198 /*
1199 * We have to be careful to avoid lock inversion of
1200 * the cells. So we back off, and wait for the
1201 * old_ocell to become free.
1202 */
1203 policy_force_mapping(cache->policy, block,
1204 lookup_result.old_oblock);
1205 atomic_inc(&cache->stats.cache_cell_clash);
1206 break;
1207 }
1208 atomic_inc(&cache->stats.demotion);
1209 atomic_inc(&cache->stats.promotion);
1210
1211 demote_then_promote(cache, structs, lookup_result.old_oblock,
1212 block, lookup_result.cblock,
1213 old_ocell, new_ocell);
1214 release_cell = false;
1215 break;
1216
1217 default:
1218 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1219 (unsigned) lookup_result.op);
1220 bio_io_error(bio);
1221 }
1222
1223 if (release_cell)
1224 cell_defer(cache, new_ocell, false);
1225}
1226
1227static int need_commit_due_to_time(struct cache *cache)
1228{
1229 return jiffies < cache->last_commit_jiffies ||
1230 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1231}
1232
1233static int commit_if_needed(struct cache *cache)
1234{
1235 if (dm_cache_changed_this_transaction(cache->cmd) &&
1236 (cache->commit_requested || need_commit_due_to_time(cache))) {
1237 atomic_inc(&cache->stats.commit_count);
1238 cache->last_commit_jiffies = jiffies;
1239 cache->commit_requested = false;
1240 return dm_cache_commit(cache->cmd, false);
1241 }
1242
1243 return 0;
1244}
1245
1246static void process_deferred_bios(struct cache *cache)
1247{
1248 unsigned long flags;
1249 struct bio_list bios;
1250 struct bio *bio;
1251 struct prealloc structs;
1252
1253 memset(&structs, 0, sizeof(structs));
1254 bio_list_init(&bios);
1255
1256 spin_lock_irqsave(&cache->lock, flags);
1257 bio_list_merge(&bios, &cache->deferred_bios);
1258 bio_list_init(&cache->deferred_bios);
1259 spin_unlock_irqrestore(&cache->lock, flags);
1260
1261 while (!bio_list_empty(&bios)) {
1262 /*
1263 * If we've got no free migration structs, and processing
1264 * this bio might require one, we pause until there are some
1265 * prepared mappings to process.
1266 */
1267 if (prealloc_data_structs(cache, &structs)) {
1268 spin_lock_irqsave(&cache->lock, flags);
1269 bio_list_merge(&cache->deferred_bios, &bios);
1270 spin_unlock_irqrestore(&cache->lock, flags);
1271 break;
1272 }
1273
1274 bio = bio_list_pop(&bios);
1275
1276 if (bio->bi_rw & REQ_FLUSH)
1277 process_flush_bio(cache, bio);
1278 else if (bio->bi_rw & REQ_DISCARD)
1279 process_discard_bio(cache, bio);
1280 else
1281 process_bio(cache, &structs, bio);
1282 }
1283
1284 prealloc_free_structs(cache, &structs);
1285}
1286
1287static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1288{
1289 unsigned long flags;
1290 struct bio_list bios;
1291 struct bio *bio;
1292
1293 bio_list_init(&bios);
1294
1295 spin_lock_irqsave(&cache->lock, flags);
1296 bio_list_merge(&bios, &cache->deferred_flush_bios);
1297 bio_list_init(&cache->deferred_flush_bios);
1298 spin_unlock_irqrestore(&cache->lock, flags);
1299
1300 while ((bio = bio_list_pop(&bios)))
1301 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1302}
1303
Joe Thornbere2e74d62013-03-20 17:21:27 +00001304static void process_deferred_writethrough_bios(struct cache *cache)
1305{
1306 unsigned long flags;
1307 struct bio_list bios;
1308 struct bio *bio;
1309
1310 bio_list_init(&bios);
1311
1312 spin_lock_irqsave(&cache->lock, flags);
1313 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1314 bio_list_init(&cache->deferred_writethrough_bios);
1315 spin_unlock_irqrestore(&cache->lock, flags);
1316
1317 while ((bio = bio_list_pop(&bios)))
1318 generic_make_request(bio);
1319}
1320
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001321static void writeback_some_dirty_blocks(struct cache *cache)
1322{
1323 int r = 0;
1324 dm_oblock_t oblock;
1325 dm_cblock_t cblock;
1326 struct prealloc structs;
1327 struct dm_bio_prison_cell *old_ocell;
1328
1329 memset(&structs, 0, sizeof(structs));
1330
1331 while (spare_migration_bandwidth(cache)) {
1332 if (prealloc_data_structs(cache, &structs))
1333 break;
1334
1335 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1336 if (r)
1337 break;
1338
1339 r = get_cell(cache, oblock, &structs, &old_ocell);
1340 if (r) {
1341 policy_set_dirty(cache->policy, oblock);
1342 break;
1343 }
1344
1345 writeback(cache, &structs, oblock, cblock, old_ocell);
1346 }
1347
1348 prealloc_free_structs(cache, &structs);
1349}
1350
1351/*----------------------------------------------------------------
1352 * Main worker loop
1353 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001354static bool is_quiescing(struct cache *cache)
1355{
1356 int r;
1357 unsigned long flags;
1358
1359 spin_lock_irqsave(&cache->lock, flags);
1360 r = cache->quiescing;
1361 spin_unlock_irqrestore(&cache->lock, flags);
1362
1363 return r;
1364}
1365
Joe Thornber66cb1912013-10-30 17:11:58 +00001366static void ack_quiescing(struct cache *cache)
1367{
1368 if (is_quiescing(cache)) {
1369 atomic_inc(&cache->quiescing_ack);
1370 wake_up(&cache->quiescing_wait);
1371 }
1372}
1373
1374static void wait_for_quiescing_ack(struct cache *cache)
1375{
1376 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1377}
1378
1379static void start_quiescing(struct cache *cache)
1380{
1381 unsigned long flags;
1382
1383 spin_lock_irqsave(&cache->lock, flags);
1384 cache->quiescing = true;
1385 spin_unlock_irqrestore(&cache->lock, flags);
1386
1387 wait_for_quiescing_ack(cache);
1388}
1389
1390static void stop_quiescing(struct cache *cache)
1391{
1392 unsigned long flags;
1393
1394 spin_lock_irqsave(&cache->lock, flags);
1395 cache->quiescing = false;
1396 spin_unlock_irqrestore(&cache->lock, flags);
1397
1398 atomic_set(&cache->quiescing_ack, 0);
1399}
1400
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001401static void wait_for_migrations(struct cache *cache)
1402{
1403 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1404}
1405
1406static void stop_worker(struct cache *cache)
1407{
1408 cancel_delayed_work(&cache->waker);
1409 flush_workqueue(cache->wq);
1410}
1411
1412static void requeue_deferred_io(struct cache *cache)
1413{
1414 struct bio *bio;
1415 struct bio_list bios;
1416
1417 bio_list_init(&bios);
1418 bio_list_merge(&bios, &cache->deferred_bios);
1419 bio_list_init(&cache->deferred_bios);
1420
1421 while ((bio = bio_list_pop(&bios)))
1422 bio_endio(bio, DM_ENDIO_REQUEUE);
1423}
1424
1425static int more_work(struct cache *cache)
1426{
1427 if (is_quiescing(cache))
1428 return !list_empty(&cache->quiesced_migrations) ||
1429 !list_empty(&cache->completed_migrations) ||
1430 !list_empty(&cache->need_commit_migrations);
1431 else
1432 return !bio_list_empty(&cache->deferred_bios) ||
1433 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001434 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001435 !list_empty(&cache->quiesced_migrations) ||
1436 !list_empty(&cache->completed_migrations) ||
1437 !list_empty(&cache->need_commit_migrations);
1438}
1439
1440static void do_worker(struct work_struct *ws)
1441{
1442 struct cache *cache = container_of(ws, struct cache, worker);
1443
1444 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001445 if (!is_quiescing(cache)) {
1446 writeback_some_dirty_blocks(cache);
1447 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001448 process_deferred_bios(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001449 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001450
1451 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1452 process_migrations(cache, &cache->completed_migrations, complete_migration);
1453
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001454 if (commit_if_needed(cache)) {
1455 process_deferred_flush_bios(cache, false);
1456
1457 /*
1458 * FIXME: rollback metadata or just go into a
1459 * failure mode and error everything
1460 */
1461 } else {
1462 process_deferred_flush_bios(cache, true);
1463 process_migrations(cache, &cache->need_commit_migrations,
1464 migration_success_post_commit);
1465 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001466
1467 ack_quiescing(cache);
1468
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001469 } while (more_work(cache));
1470}
1471
1472/*
1473 * We want to commit periodically so that not too much
1474 * unwritten metadata builds up.
1475 */
1476static void do_waker(struct work_struct *ws)
1477{
1478 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001479 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001480 wake_worker(cache);
1481 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1482}
1483
1484/*----------------------------------------------------------------*/
1485
1486static int is_congested(struct dm_dev *dev, int bdi_bits)
1487{
1488 struct request_queue *q = bdev_get_queue(dev->bdev);
1489 return bdi_congested(&q->backing_dev_info, bdi_bits);
1490}
1491
1492static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1493{
1494 struct cache *cache = container_of(cb, struct cache, callbacks);
1495
1496 return is_congested(cache->origin_dev, bdi_bits) ||
1497 is_congested(cache->cache_dev, bdi_bits);
1498}
1499
1500/*----------------------------------------------------------------
1501 * Target methods
1502 *--------------------------------------------------------------*/
1503
1504/*
1505 * This function gets called on the error paths of the constructor, so we
1506 * have to cope with a partially initialised struct.
1507 */
1508static void destroy(struct cache *cache)
1509{
1510 unsigned i;
1511
1512 if (cache->next_migration)
1513 mempool_free(cache->next_migration, cache->migration_pool);
1514
1515 if (cache->migration_pool)
1516 mempool_destroy(cache->migration_pool);
1517
1518 if (cache->all_io_ds)
1519 dm_deferred_set_destroy(cache->all_io_ds);
1520
1521 if (cache->prison)
1522 dm_bio_prison_destroy(cache->prison);
1523
1524 if (cache->wq)
1525 destroy_workqueue(cache->wq);
1526
1527 if (cache->dirty_bitset)
1528 free_bitset(cache->dirty_bitset);
1529
1530 if (cache->discard_bitset)
1531 free_bitset(cache->discard_bitset);
1532
1533 if (cache->copier)
1534 dm_kcopyd_client_destroy(cache->copier);
1535
1536 if (cache->cmd)
1537 dm_cache_metadata_close(cache->cmd);
1538
1539 if (cache->metadata_dev)
1540 dm_put_device(cache->ti, cache->metadata_dev);
1541
1542 if (cache->origin_dev)
1543 dm_put_device(cache->ti, cache->origin_dev);
1544
1545 if (cache->cache_dev)
1546 dm_put_device(cache->ti, cache->cache_dev);
1547
1548 if (cache->policy)
1549 dm_cache_policy_destroy(cache->policy);
1550
1551 for (i = 0; i < cache->nr_ctr_args ; i++)
1552 kfree(cache->ctr_args[i]);
1553 kfree(cache->ctr_args);
1554
1555 kfree(cache);
1556}
1557
1558static void cache_dtr(struct dm_target *ti)
1559{
1560 struct cache *cache = ti->private;
1561
1562 destroy(cache);
1563}
1564
1565static sector_t get_dev_size(struct dm_dev *dev)
1566{
1567 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1568}
1569
1570/*----------------------------------------------------------------*/
1571
1572/*
1573 * Construct a cache device mapping.
1574 *
1575 * cache <metadata dev> <cache dev> <origin dev> <block size>
1576 * <#feature args> [<feature arg>]*
1577 * <policy> <#policy args> [<policy arg>]*
1578 *
1579 * metadata dev : fast device holding the persistent metadata
1580 * cache dev : fast device holding cached data blocks
1581 * origin dev : slow device holding original data blocks
1582 * block size : cache unit size in sectors
1583 *
1584 * #feature args : number of feature arguments passed
1585 * feature args : writethrough. (The default is writeback.)
1586 *
1587 * policy : the replacement policy to use
1588 * #policy args : an even number of policy arguments corresponding
1589 * to key/value pairs passed to the policy
1590 * policy args : key/value pairs passed to the policy
1591 * E.g. 'sequential_threshold 1024'
1592 * See cache-policies.txt for details.
1593 *
1594 * Optional feature arguments are:
1595 * writethrough : write through caching that prohibits cache block
1596 * content from being different from origin block content.
1597 * Without this argument, the default behaviour is to write
1598 * back cache block contents later for performance reasons,
1599 * so they may differ from the corresponding origin blocks.
1600 */
1601struct cache_args {
1602 struct dm_target *ti;
1603
1604 struct dm_dev *metadata_dev;
1605
1606 struct dm_dev *cache_dev;
1607 sector_t cache_sectors;
1608
1609 struct dm_dev *origin_dev;
1610 sector_t origin_sectors;
1611
1612 uint32_t block_size;
1613
1614 const char *policy_name;
1615 int policy_argc;
1616 const char **policy_argv;
1617
1618 struct cache_features features;
1619};
1620
1621static void destroy_cache_args(struct cache_args *ca)
1622{
1623 if (ca->metadata_dev)
1624 dm_put_device(ca->ti, ca->metadata_dev);
1625
1626 if (ca->cache_dev)
1627 dm_put_device(ca->ti, ca->cache_dev);
1628
1629 if (ca->origin_dev)
1630 dm_put_device(ca->ti, ca->origin_dev);
1631
1632 kfree(ca);
1633}
1634
1635static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1636{
1637 if (!as->argc) {
1638 *error = "Insufficient args";
1639 return false;
1640 }
1641
1642 return true;
1643}
1644
1645static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1646 char **error)
1647{
1648 int r;
1649 sector_t metadata_dev_size;
1650 char b[BDEVNAME_SIZE];
1651
1652 if (!at_least_one_arg(as, error))
1653 return -EINVAL;
1654
1655 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1656 &ca->metadata_dev);
1657 if (r) {
1658 *error = "Error opening metadata device";
1659 return r;
1660 }
1661
1662 metadata_dev_size = get_dev_size(ca->metadata_dev);
1663 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1664 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1665 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1666
1667 return 0;
1668}
1669
1670static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1671 char **error)
1672{
1673 int r;
1674
1675 if (!at_least_one_arg(as, error))
1676 return -EINVAL;
1677
1678 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1679 &ca->cache_dev);
1680 if (r) {
1681 *error = "Error opening cache device";
1682 return r;
1683 }
1684 ca->cache_sectors = get_dev_size(ca->cache_dev);
1685
1686 return 0;
1687}
1688
1689static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1690 char **error)
1691{
1692 int r;
1693
1694 if (!at_least_one_arg(as, error))
1695 return -EINVAL;
1696
1697 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1698 &ca->origin_dev);
1699 if (r) {
1700 *error = "Error opening origin device";
1701 return r;
1702 }
1703
1704 ca->origin_sectors = get_dev_size(ca->origin_dev);
1705 if (ca->ti->len > ca->origin_sectors) {
1706 *error = "Device size larger than cached device";
1707 return -EINVAL;
1708 }
1709
1710 return 0;
1711}
1712
1713static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1714 char **error)
1715{
Mike Snitzer05473042013-08-16 10:54:19 -04001716 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001717
1718 if (!at_least_one_arg(as, error))
1719 return -EINVAL;
1720
Mike Snitzer05473042013-08-16 10:54:19 -04001721 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
1722 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1723 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1724 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001725 *error = "Invalid data block size";
1726 return -EINVAL;
1727 }
1728
Mike Snitzer05473042013-08-16 10:54:19 -04001729 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001730 *error = "Data block size is larger than the cache device";
1731 return -EINVAL;
1732 }
1733
Mike Snitzer05473042013-08-16 10:54:19 -04001734 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001735
1736 return 0;
1737}
1738
1739static void init_features(struct cache_features *cf)
1740{
1741 cf->mode = CM_WRITE;
1742 cf->write_through = false;
1743}
1744
1745static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
1746 char **error)
1747{
1748 static struct dm_arg _args[] = {
1749 {0, 1, "Invalid number of cache feature arguments"},
1750 };
1751
1752 int r;
1753 unsigned argc;
1754 const char *arg;
1755 struct cache_features *cf = &ca->features;
1756
1757 init_features(cf);
1758
1759 r = dm_read_arg_group(_args, as, &argc, error);
1760 if (r)
1761 return -EINVAL;
1762
1763 while (argc--) {
1764 arg = dm_shift_arg(as);
1765
1766 if (!strcasecmp(arg, "writeback"))
1767 cf->write_through = false;
1768
1769 else if (!strcasecmp(arg, "writethrough"))
1770 cf->write_through = true;
1771
1772 else {
1773 *error = "Unrecognised cache feature requested";
1774 return -EINVAL;
1775 }
1776 }
1777
1778 return 0;
1779}
1780
1781static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
1782 char **error)
1783{
1784 static struct dm_arg _args[] = {
1785 {0, 1024, "Invalid number of policy arguments"},
1786 };
1787
1788 int r;
1789
1790 if (!at_least_one_arg(as, error))
1791 return -EINVAL;
1792
1793 ca->policy_name = dm_shift_arg(as);
1794
1795 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
1796 if (r)
1797 return -EINVAL;
1798
1799 ca->policy_argv = (const char **)as->argv;
1800 dm_consume_args(as, ca->policy_argc);
1801
1802 return 0;
1803}
1804
1805static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
1806 char **error)
1807{
1808 int r;
1809 struct dm_arg_set as;
1810
1811 as.argc = argc;
1812 as.argv = argv;
1813
1814 r = parse_metadata_dev(ca, &as, error);
1815 if (r)
1816 return r;
1817
1818 r = parse_cache_dev(ca, &as, error);
1819 if (r)
1820 return r;
1821
1822 r = parse_origin_dev(ca, &as, error);
1823 if (r)
1824 return r;
1825
1826 r = parse_block_size(ca, &as, error);
1827 if (r)
1828 return r;
1829
1830 r = parse_features(ca, &as, error);
1831 if (r)
1832 return r;
1833
1834 r = parse_policy(ca, &as, error);
1835 if (r)
1836 return r;
1837
1838 return 0;
1839}
1840
1841/*----------------------------------------------------------------*/
1842
1843static struct kmem_cache *migration_cache;
1844
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01001845#define NOT_CORE_OPTION 1
1846
Joe Thornber2f14f4b2013-05-10 14:37:21 +01001847static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01001848{
1849 unsigned long tmp;
1850
Joe Thornber2f14f4b2013-05-10 14:37:21 +01001851 if (!strcasecmp(key, "migration_threshold")) {
1852 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01001853 return -EINVAL;
1854
1855 cache->migration_threshold = tmp;
1856 return 0;
1857 }
1858
1859 return NOT_CORE_OPTION;
1860}
1861
Joe Thornber2f14f4b2013-05-10 14:37:21 +01001862static int set_config_value(struct cache *cache, const char *key, const char *value)
1863{
1864 int r = process_config_option(cache, key, value);
1865
1866 if (r == NOT_CORE_OPTION)
1867 r = policy_set_config_value(cache->policy, key, value);
1868
1869 if (r)
1870 DMWARN("bad config value for %s: %s", key, value);
1871
1872 return r;
1873}
1874
1875static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001876{
1877 int r = 0;
1878
1879 if (argc & 1) {
1880 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
1881 return -EINVAL;
1882 }
1883
1884 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01001885 r = set_config_value(cache, argv[0], argv[1]);
1886 if (r)
1887 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001888
1889 argc -= 2;
1890 argv += 2;
1891 }
1892
1893 return r;
1894}
1895
1896static int create_cache_policy(struct cache *cache, struct cache_args *ca,
1897 char **error)
1898{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001899 cache->policy = dm_cache_policy_create(ca->policy_name,
1900 cache->cache_size,
1901 cache->origin_sectors,
1902 cache->sectors_per_block);
1903 if (!cache->policy) {
1904 *error = "Error creating cache's policy";
1905 return -ENOMEM;
1906 }
1907
Joe Thornber2f14f4b2013-05-10 14:37:21 +01001908 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001909}
1910
1911/*
1912 * We want the discard block size to be a power of two, at least the size
1913 * of the cache block size, and have no more than 2^14 discard blocks
1914 * across the origin.
1915 */
1916#define MAX_DISCARD_BLOCKS (1 << 14)
1917
1918static bool too_many_discard_blocks(sector_t discard_block_size,
1919 sector_t origin_size)
1920{
1921 (void) sector_div(origin_size, discard_block_size);
1922
1923 return origin_size > MAX_DISCARD_BLOCKS;
1924}
1925
1926static sector_t calculate_discard_block_size(sector_t cache_block_size,
1927 sector_t origin_size)
1928{
1929 sector_t discard_block_size;
1930
1931 discard_block_size = roundup_pow_of_two(cache_block_size);
1932
1933 if (origin_size)
1934 while (too_many_discard_blocks(discard_block_size, origin_size))
1935 discard_block_size *= 2;
1936
1937 return discard_block_size;
1938}
1939
Joe Thornberf8350da2013-05-10 14:37:16 +01001940#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001941
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001942static int cache_create(struct cache_args *ca, struct cache **result)
1943{
1944 int r = 0;
1945 char **error = &ca->ti->error;
1946 struct cache *cache;
1947 struct dm_target *ti = ca->ti;
1948 dm_block_t origin_blocks;
1949 struct dm_cache_metadata *cmd;
1950 bool may_format = ca->features.mode == CM_WRITE;
1951
1952 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
1953 if (!cache)
1954 return -ENOMEM;
1955
1956 cache->ti = ca->ti;
1957 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001958 ti->num_flush_bios = 2;
1959 ti->flush_supported = true;
1960
1961 ti->num_discard_bios = 1;
1962 ti->discards_supported = true;
1963 ti->discard_zeroes_data_unsupported = true;
1964
Joe Thornber8c5008f2013-05-10 14:37:18 +01001965 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01001966 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001967
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001968 cache->callbacks.congested_fn = cache_is_congested;
1969 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
1970
1971 cache->metadata_dev = ca->metadata_dev;
1972 cache->origin_dev = ca->origin_dev;
1973 cache->cache_dev = ca->cache_dev;
1974
1975 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
1976
1977 /* FIXME: factor out this whole section */
1978 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00001979 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001980 cache->origin_blocks = to_oblock(origin_blocks);
1981
1982 cache->sectors_per_block = ca->block_size;
1983 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
1984 r = -EINVAL;
1985 goto bad;
1986 }
1987
1988 if (ca->block_size & (ca->block_size - 1)) {
1989 dm_block_t cache_size = ca->cache_sectors;
1990
1991 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00001992 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001993 cache->cache_size = to_cblock(cache_size);
1994 } else {
1995 cache->sectors_per_block_shift = __ffs(ca->block_size);
1996 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
1997 }
1998
1999 r = create_cache_policy(cache, ca, error);
2000 if (r)
2001 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002002
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002003 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002004 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2005
2006 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2007 if (r) {
2008 *error = "Error setting cache policy's config values";
2009 goto bad;
2010 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002011
2012 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2013 ca->block_size, may_format,
2014 dm_cache_policy_get_hint_size(cache->policy));
2015 if (IS_ERR(cmd)) {
2016 *error = "Error creating metadata object";
2017 r = PTR_ERR(cmd);
2018 goto bad;
2019 }
2020 cache->cmd = cmd;
2021
2022 spin_lock_init(&cache->lock);
2023 bio_list_init(&cache->deferred_bios);
2024 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002025 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002026 INIT_LIST_HEAD(&cache->quiesced_migrations);
2027 INIT_LIST_HEAD(&cache->completed_migrations);
2028 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002029 atomic_set(&cache->nr_migrations, 0);
2030 init_waitqueue_head(&cache->migration_wait);
2031
Joe Thornber66cb1912013-10-30 17:11:58 +00002032 init_waitqueue_head(&cache->quiescing_wait);
2033 atomic_set(&cache->quiescing_ack, 0);
2034
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002035 r = -ENOMEM;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002036 cache->nr_dirty = 0;
2037 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2038 if (!cache->dirty_bitset) {
2039 *error = "could not allocate dirty bitset";
2040 goto bad;
2041 }
2042 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2043
2044 cache->discard_block_size =
2045 calculate_discard_block_size(cache->sectors_per_block,
2046 cache->origin_sectors);
2047 cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks);
2048 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2049 if (!cache->discard_bitset) {
2050 *error = "could not allocate discard bitset";
2051 goto bad;
2052 }
2053 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2054
2055 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2056 if (IS_ERR(cache->copier)) {
2057 *error = "could not create kcopyd client";
2058 r = PTR_ERR(cache->copier);
2059 goto bad;
2060 }
2061
2062 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2063 if (!cache->wq) {
2064 *error = "could not create workqueue for metadata object";
2065 goto bad;
2066 }
2067 INIT_WORK(&cache->worker, do_worker);
2068 INIT_DELAYED_WORK(&cache->waker, do_waker);
2069 cache->last_commit_jiffies = jiffies;
2070
2071 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2072 if (!cache->prison) {
2073 *error = "could not create bio prison";
2074 goto bad;
2075 }
2076
2077 cache->all_io_ds = dm_deferred_set_create();
2078 if (!cache->all_io_ds) {
2079 *error = "could not create all_io deferred set";
2080 goto bad;
2081 }
2082
2083 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2084 migration_cache);
2085 if (!cache->migration_pool) {
2086 *error = "Error creating cache's migration mempool";
2087 goto bad;
2088 }
2089
2090 cache->next_migration = NULL;
2091
2092 cache->need_tick_bio = true;
2093 cache->sized = false;
2094 cache->quiescing = false;
2095 cache->commit_requested = false;
2096 cache->loaded_mappings = false;
2097 cache->loaded_discards = false;
2098
2099 load_stats(cache);
2100
2101 atomic_set(&cache->stats.demotion, 0);
2102 atomic_set(&cache->stats.promotion, 0);
2103 atomic_set(&cache->stats.copies_avoided, 0);
2104 atomic_set(&cache->stats.cache_cell_clash, 0);
2105 atomic_set(&cache->stats.commit_count, 0);
2106 atomic_set(&cache->stats.discard_count, 0);
2107
2108 *result = cache;
2109 return 0;
2110
2111bad:
2112 destroy(cache);
2113 return r;
2114}
2115
2116static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2117{
2118 unsigned i;
2119 const char **copy;
2120
2121 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2122 if (!copy)
2123 return -ENOMEM;
2124 for (i = 0; i < argc; i++) {
2125 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2126 if (!copy[i]) {
2127 while (i--)
2128 kfree(copy[i]);
2129 kfree(copy);
2130 return -ENOMEM;
2131 }
2132 }
2133
2134 cache->nr_ctr_args = argc;
2135 cache->ctr_args = copy;
2136
2137 return 0;
2138}
2139
2140static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2141{
2142 int r = -EINVAL;
2143 struct cache_args *ca;
2144 struct cache *cache = NULL;
2145
2146 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2147 if (!ca) {
2148 ti->error = "Error allocating memory for cache";
2149 return -ENOMEM;
2150 }
2151 ca->ti = ti;
2152
2153 r = parse_cache_args(ca, argc, argv, &ti->error);
2154 if (r)
2155 goto out;
2156
2157 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002158 if (r)
2159 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002160
2161 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2162 if (r) {
2163 destroy(cache);
2164 goto out;
2165 }
2166
2167 ti->private = cache;
2168
2169out:
2170 destroy_cache_args(ca);
2171 return r;
2172}
2173
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002174static int cache_map(struct dm_target *ti, struct bio *bio)
2175{
2176 struct cache *cache = ti->private;
2177
2178 int r;
2179 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002180 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002181 bool can_migrate = false;
2182 bool discarded_block;
2183 struct dm_bio_prison_cell *cell;
2184 struct policy_result lookup_result;
2185 struct per_bio_data *pb;
2186
2187 if (from_oblock(block) > from_oblock(cache->origin_blocks)) {
2188 /*
2189 * This can only occur if the io goes to a partial block at
2190 * the end of the origin device. We don't cache these.
2191 * Just remap to the origin and carry on.
2192 */
2193 remap_to_origin_clear_discard(cache, bio, block);
2194 return DM_MAPIO_REMAPPED;
2195 }
2196
Mike Snitzer19b00922013-04-05 15:36:34 +01002197 pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002198
2199 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2200 defer_bio(cache, bio);
2201 return DM_MAPIO_SUBMITTED;
2202 }
2203
2204 /*
2205 * Check to see if that block is currently migrating.
2206 */
2207 cell = alloc_prison_cell(cache);
2208 if (!cell) {
2209 defer_bio(cache, bio);
2210 return DM_MAPIO_SUBMITTED;
2211 }
2212
2213 r = bio_detain(cache, block, bio, cell,
2214 (cell_free_fn) free_prison_cell,
2215 cache, &cell);
2216 if (r) {
2217 if (r < 0)
2218 defer_bio(cache, bio);
2219
2220 return DM_MAPIO_SUBMITTED;
2221 }
2222
2223 discarded_block = is_discarded_oblock(cache, block);
2224
2225 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2226 bio, &lookup_result);
2227 if (r == -EWOULDBLOCK) {
2228 cell_defer(cache, cell, true);
2229 return DM_MAPIO_SUBMITTED;
2230
2231 } else if (r) {
2232 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
2233 bio_io_error(bio);
2234 return DM_MAPIO_SUBMITTED;
2235 }
2236
2237 switch (lookup_result.op) {
2238 case POLICY_HIT:
2239 inc_hit_counter(cache, bio);
2240 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2241
Joe Thornbere2e74d62013-03-20 17:21:27 +00002242 if (is_writethrough_io(cache, bio, lookup_result.cblock))
2243 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2244 else
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002245 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002246
2247 cell_defer(cache, cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002248 break;
2249
2250 case POLICY_MISS:
2251 inc_miss_counter(cache, bio);
2252 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2253
2254 if (pb->req_nr != 0) {
2255 /*
2256 * This is a duplicate writethrough io that is no
2257 * longer needed because the block has been demoted.
2258 */
2259 bio_endio(bio, 0);
2260 cell_defer(cache, cell, false);
2261 return DM_MAPIO_SUBMITTED;
2262 } else {
2263 remap_to_origin_clear_discard(cache, bio, block);
2264 cell_defer(cache, cell, false);
2265 }
2266 break;
2267
2268 default:
2269 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2270 (unsigned) lookup_result.op);
2271 bio_io_error(bio);
2272 return DM_MAPIO_SUBMITTED;
2273 }
2274
2275 return DM_MAPIO_REMAPPED;
2276}
2277
2278static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2279{
2280 struct cache *cache = ti->private;
2281 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002282 size_t pb_data_size = get_per_bio_data_size(cache);
2283 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002284
2285 if (pb->tick) {
2286 policy_tick(cache->policy);
2287
2288 spin_lock_irqsave(&cache->lock, flags);
2289 cache->need_tick_bio = true;
2290 spin_unlock_irqrestore(&cache->lock, flags);
2291 }
2292
2293 check_for_quiesced_migrations(cache, pb);
2294
2295 return 0;
2296}
2297
2298static int write_dirty_bitset(struct cache *cache)
2299{
2300 unsigned i, r;
2301
2302 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2303 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2304 is_dirty(cache, to_cblock(i)));
2305 if (r)
2306 return r;
2307 }
2308
2309 return 0;
2310}
2311
2312static int write_discard_bitset(struct cache *cache)
2313{
2314 unsigned i, r;
2315
2316 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2317 cache->discard_nr_blocks);
2318 if (r) {
2319 DMERR("could not resize on-disk discard bitset");
2320 return r;
2321 }
2322
2323 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2324 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2325 is_discarded(cache, to_dblock(i)));
2326 if (r)
2327 return r;
2328 }
2329
2330 return 0;
2331}
2332
2333static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock,
2334 uint32_t hint)
2335{
2336 struct cache *cache = context;
2337 return dm_cache_save_hint(cache->cmd, cblock, hint);
2338}
2339
2340static int write_hints(struct cache *cache)
2341{
2342 int r;
2343
2344 r = dm_cache_begin_hints(cache->cmd, cache->policy);
2345 if (r) {
2346 DMERR("dm_cache_begin_hints failed");
2347 return r;
2348 }
2349
2350 r = policy_walk_mappings(cache->policy, save_hint, cache);
2351 if (r)
2352 DMERR("policy_walk_mappings failed");
2353
2354 return r;
2355}
2356
2357/*
2358 * returns true on success
2359 */
2360static bool sync_metadata(struct cache *cache)
2361{
2362 int r1, r2, r3, r4;
2363
2364 r1 = write_dirty_bitset(cache);
2365 if (r1)
2366 DMERR("could not write dirty bitset");
2367
2368 r2 = write_discard_bitset(cache);
2369 if (r2)
2370 DMERR("could not write discard bitset");
2371
2372 save_stats(cache);
2373
2374 r3 = write_hints(cache);
2375 if (r3)
2376 DMERR("could not write hints");
2377
2378 /*
2379 * If writing the above metadata failed, we still commit, but don't
2380 * set the clean shutdown flag. This will effectively force every
2381 * dirty bit to be set on reload.
2382 */
2383 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2384 if (r4)
2385 DMERR("could not write cache metadata. Data loss may occur.");
2386
2387 return !r1 && !r2 && !r3 && !r4;
2388}
2389
2390static void cache_postsuspend(struct dm_target *ti)
2391{
2392 struct cache *cache = ti->private;
2393
2394 start_quiescing(cache);
2395 wait_for_migrations(cache);
2396 stop_worker(cache);
2397 requeue_deferred_io(cache);
2398 stop_quiescing(cache);
2399
2400 (void) sync_metadata(cache);
2401}
2402
2403static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2404 bool dirty, uint32_t hint, bool hint_valid)
2405{
2406 int r;
2407 struct cache *cache = context;
2408
2409 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2410 if (r)
2411 return r;
2412
2413 if (dirty)
2414 set_dirty(cache, oblock, cblock);
2415 else
2416 clear_dirty(cache, oblock, cblock);
2417
2418 return 0;
2419}
2420
2421static int load_discard(void *context, sector_t discard_block_size,
2422 dm_dblock_t dblock, bool discard)
2423{
2424 struct cache *cache = context;
2425
2426 /* FIXME: handle mis-matched block size */
2427
2428 if (discard)
2429 set_discard(cache, dblock);
2430 else
2431 clear_discard(cache, dblock);
2432
2433 return 0;
2434}
2435
2436static int cache_preresume(struct dm_target *ti)
2437{
2438 int r = 0;
2439 struct cache *cache = ti->private;
2440 sector_t actual_cache_size = get_dev_size(cache->cache_dev);
2441 (void) sector_div(actual_cache_size, cache->sectors_per_block);
2442
2443 /*
2444 * Check to see if the cache has resized.
2445 */
2446 if (from_cblock(cache->cache_size) != actual_cache_size || !cache->sized) {
2447 cache->cache_size = to_cblock(actual_cache_size);
2448
2449 r = dm_cache_resize(cache->cmd, cache->cache_size);
2450 if (r) {
2451 DMERR("could not resize cache metadata");
2452 return r;
2453 }
2454
2455 cache->sized = true;
2456 }
2457
2458 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002459 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002460 load_mapping, cache);
2461 if (r) {
2462 DMERR("could not load cache mappings");
2463 return r;
2464 }
2465
2466 cache->loaded_mappings = true;
2467 }
2468
2469 if (!cache->loaded_discards) {
2470 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2471 if (r) {
2472 DMERR("could not load origin discards");
2473 return r;
2474 }
2475
2476 cache->loaded_discards = true;
2477 }
2478
2479 return r;
2480}
2481
2482static void cache_resume(struct dm_target *ti)
2483{
2484 struct cache *cache = ti->private;
2485
2486 cache->need_tick_bio = true;
2487 do_waker(&cache->waker.work);
2488}
2489
2490/*
2491 * Status format:
2492 *
2493 * <#used metadata blocks>/<#total metadata blocks>
2494 * <#read hits> <#read misses> <#write hits> <#write misses>
2495 * <#demotions> <#promotions> <#blocks in cache> <#dirty>
2496 * <#features> <features>*
2497 * <#core args> <core args>
2498 * <#policy args> <policy args>*
2499 */
2500static void cache_status(struct dm_target *ti, status_type_t type,
2501 unsigned status_flags, char *result, unsigned maxlen)
2502{
2503 int r = 0;
2504 unsigned i;
2505 ssize_t sz = 0;
2506 dm_block_t nr_free_blocks_metadata = 0;
2507 dm_block_t nr_blocks_metadata = 0;
2508 char buf[BDEVNAME_SIZE];
2509 struct cache *cache = ti->private;
2510 dm_cblock_t residency;
2511
2512 switch (type) {
2513 case STATUSTYPE_INFO:
2514 /* Commit to ensure statistics aren't out-of-date */
2515 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2516 r = dm_cache_commit(cache->cmd, false);
2517 if (r)
2518 DMERR("could not commit metadata for accurate status");
2519 }
2520
2521 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2522 &nr_free_blocks_metadata);
2523 if (r) {
2524 DMERR("could not get metadata free block count");
2525 goto err;
2526 }
2527
2528 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2529 if (r) {
2530 DMERR("could not get metadata device size");
2531 goto err;
2532 }
2533
2534 residency = policy_residency(cache->policy);
2535
2536 DMEMIT("%llu/%llu %u %u %u %u %u %u %llu %u ",
2537 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2538 (unsigned long long)nr_blocks_metadata,
2539 (unsigned) atomic_read(&cache->stats.read_hit),
2540 (unsigned) atomic_read(&cache->stats.read_miss),
2541 (unsigned) atomic_read(&cache->stats.write_hit),
2542 (unsigned) atomic_read(&cache->stats.write_miss),
2543 (unsigned) atomic_read(&cache->stats.demotion),
2544 (unsigned) atomic_read(&cache->stats.promotion),
2545 (unsigned long long) from_cblock(residency),
2546 cache->nr_dirty);
2547
2548 if (cache->features.write_through)
2549 DMEMIT("1 writethrough ");
2550 else
2551 DMEMIT("0 ");
2552
2553 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
2554 if (sz < maxlen) {
2555 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2556 if (r)
2557 DMERR("policy_emit_config_values returned %d", r);
2558 }
2559
2560 break;
2561
2562 case STATUSTYPE_TABLE:
2563 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2564 DMEMIT("%s ", buf);
2565 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2566 DMEMIT("%s ", buf);
2567 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2568 DMEMIT("%s", buf);
2569
2570 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2571 DMEMIT(" %s", cache->ctr_args[i]);
2572 if (cache->nr_ctr_args)
2573 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2574 }
2575
2576 return;
2577
2578err:
2579 DMEMIT("Error");
2580}
2581
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002582/*
2583 * Supports <key> <value>.
2584 *
2585 * The key migration_threshold is supported by the cache target core.
2586 */
2587static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
2588{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002589 struct cache *cache = ti->private;
2590
2591 if (argc != 2)
2592 return -EINVAL;
2593
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002594 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002595}
2596
2597static int cache_iterate_devices(struct dm_target *ti,
2598 iterate_devices_callout_fn fn, void *data)
2599{
2600 int r = 0;
2601 struct cache *cache = ti->private;
2602
2603 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
2604 if (!r)
2605 r = fn(ti, cache->origin_dev, 0, ti->len, data);
2606
2607 return r;
2608}
2609
2610/*
2611 * We assume I/O is going to the origin (which is the volume
2612 * more likely to have restrictions e.g. by being striped).
2613 * (Looking up the exact location of the data would be expensive
2614 * and could always be out of date by the time the bio is submitted.)
2615 */
2616static int cache_bvec_merge(struct dm_target *ti,
2617 struct bvec_merge_data *bvm,
2618 struct bio_vec *biovec, int max_size)
2619{
2620 struct cache *cache = ti->private;
2621 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
2622
2623 if (!q->merge_bvec_fn)
2624 return max_size;
2625
2626 bvm->bi_bdev = cache->origin_dev->bdev;
2627 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2628}
2629
2630static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
2631{
2632 /*
2633 * FIXME: these limits may be incompatible with the cache device
2634 */
2635 limits->max_discard_sectors = cache->discard_block_size * 1024;
2636 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
2637}
2638
2639static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
2640{
2641 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04002642 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002643
Mike Snitzerf6109372013-08-20 15:02:41 -04002644 /*
2645 * If the system-determined stacked limits are compatible with the
2646 * cache's blocksize (io_opt is a factor) do not override them.
2647 */
2648 if (io_opt_sectors < cache->sectors_per_block ||
2649 do_div(io_opt_sectors, cache->sectors_per_block)) {
2650 blk_limits_io_min(limits, 0);
2651 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
2652 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002653 set_discard_limits(cache, limits);
2654}
2655
2656/*----------------------------------------------------------------*/
2657
2658static struct target_type cache_target = {
2659 .name = "cache",
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002660 .version = {1, 1, 1},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002661 .module = THIS_MODULE,
2662 .ctr = cache_ctr,
2663 .dtr = cache_dtr,
2664 .map = cache_map,
2665 .end_io = cache_end_io,
2666 .postsuspend = cache_postsuspend,
2667 .preresume = cache_preresume,
2668 .resume = cache_resume,
2669 .status = cache_status,
2670 .message = cache_message,
2671 .iterate_devices = cache_iterate_devices,
2672 .merge = cache_bvec_merge,
2673 .io_hints = cache_io_hints,
2674};
2675
2676static int __init dm_cache_init(void)
2677{
2678 int r;
2679
2680 r = dm_register_target(&cache_target);
2681 if (r) {
2682 DMERR("cache target registration failed: %d", r);
2683 return r;
2684 }
2685
2686 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
2687 if (!migration_cache) {
2688 dm_unregister_target(&cache_target);
2689 return -ENOMEM;
2690 }
2691
2692 return 0;
2693}
2694
2695static void __exit dm_cache_exit(void)
2696{
2697 dm_unregister_target(&cache_target);
2698 kmem_cache_destroy(migration_cache);
2699}
2700
2701module_init(dm_cache_init);
2702module_exit(dm_cache_exit);
2703
2704MODULE_DESCRIPTION(DM_NAME " cache target");
2705MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
2706MODULE_LICENSE("GPL");