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