blob: 8c0217753cc56e450fb7d0feada655c55463b8ca [file] [log] [blame]
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001/*
2 * Copyright (C) 2012 Red Hat. All rights reserved.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm.h"
8#include "dm-bio-prison.h"
Darrick J. Wongb844fe62013-04-05 15:36:32 +01009#include "dm-bio-record.h"
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000010#include "dm-cache-metadata.h"
11
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
14#include <linux/init.h>
15#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19
20#define DM_MSG_PREFIX "cache"
21
22DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
23 "A percentage of time allocated for copying to and/or from cache");
24
25/*----------------------------------------------------------------*/
26
27/*
28 * Glossary:
29 *
30 * oblock: index of an origin block
31 * cblock: index of a cache block
32 * promotion: movement of a block from origin to cache
33 * demotion: movement of a block from cache to origin
34 * migration: movement of a block between the origin and cache device,
35 * either direction
36 */
37
38/*----------------------------------------------------------------*/
39
40static size_t bitset_size_in_bytes(unsigned nr_entries)
41{
42 return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
43}
44
45static unsigned long *alloc_bitset(unsigned nr_entries)
46{
47 size_t s = bitset_size_in_bytes(nr_entries);
48 return vzalloc(s);
49}
50
51static void clear_bitset(void *bitset, unsigned nr_entries)
52{
53 size_t s = bitset_size_in_bytes(nr_entries);
54 memset(bitset, 0, s);
55}
56
57static void free_bitset(unsigned long *bits)
58{
59 vfree(bits);
60}
61
62/*----------------------------------------------------------------*/
63
Joe Thornberc9d28d52013-10-31 13:55:48 -040064/*
65 * There are a couple of places where we let a bio run, but want to do some
66 * work before calling its endio function. We do this by temporarily
67 * changing the endio fn.
68 */
69struct dm_hook_info {
70 bio_end_io_t *bi_end_io;
71 void *bi_private;
72};
73
74static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
75 bio_end_io_t *bi_end_io, void *bi_private)
76{
77 h->bi_end_io = bio->bi_end_io;
78 h->bi_private = bio->bi_private;
79
80 bio->bi_end_io = bi_end_io;
81 bio->bi_private = bi_private;
82}
83
84static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
85{
86 bio->bi_end_io = h->bi_end_io;
87 bio->bi_private = h->bi_private;
88}
89
90/*----------------------------------------------------------------*/
91
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000092#define PRISON_CELLS 1024
93#define MIGRATION_POOL_SIZE 128
94#define COMMIT_PERIOD HZ
95#define MIGRATION_COUNT_WINDOW 10
96
97/*
Mike Snitzer05473042013-08-16 10:54:19 -040098 * The block size of the device holding cache data must be
99 * between 32KB and 1GB.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000100 */
101#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
Mike Snitzer05473042013-08-16 10:54:19 -0400102#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000103
104/*
105 * FIXME: the cache is read/write for the time being.
106 */
Joe Thornber2ee57d52013-10-24 14:10:29 -0400107enum cache_metadata_mode {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000108 CM_WRITE, /* metadata may be changed */
109 CM_READ_ONLY, /* metadata may not be changed */
110};
111
Joe Thornber2ee57d52013-10-24 14:10:29 -0400112enum cache_io_mode {
113 /*
114 * Data is written to cached blocks only. These blocks are marked
115 * dirty. If you lose the cache device you will lose data.
116 * Potential performance increase for both reads and writes.
117 */
118 CM_IO_WRITEBACK,
119
120 /*
121 * Data is written to both cache and origin. Blocks are never
122 * dirty. Potential performance benfit for reads only.
123 */
124 CM_IO_WRITETHROUGH,
125
126 /*
127 * A degraded mode useful for various cache coherency situations
128 * (eg, rolling back snapshots). Reads and writes always go to the
129 * origin. If a write goes to a cached oblock, then the cache
130 * block is invalidated.
131 */
132 CM_IO_PASSTHROUGH
133};
134
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000135struct cache_features {
Joe Thornber2ee57d52013-10-24 14:10:29 -0400136 enum cache_metadata_mode mode;
137 enum cache_io_mode io_mode;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000138};
139
140struct cache_stats {
141 atomic_t read_hit;
142 atomic_t read_miss;
143 atomic_t write_hit;
144 atomic_t write_miss;
145 atomic_t demotion;
146 atomic_t promotion;
147 atomic_t copies_avoided;
148 atomic_t cache_cell_clash;
149 atomic_t commit_count;
150 atomic_t discard_count;
151};
152
153struct cache {
154 struct dm_target *ti;
155 struct dm_target_callbacks callbacks;
156
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400157 struct dm_cache_metadata *cmd;
158
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000159 /*
160 * Metadata is written to this device.
161 */
162 struct dm_dev *metadata_dev;
163
164 /*
165 * The slower of the two data devices. Typically a spindle.
166 */
167 struct dm_dev *origin_dev;
168
169 /*
170 * The faster of the two data devices. Typically an SSD.
171 */
172 struct dm_dev *cache_dev;
173
174 /*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000175 * Size of the origin device in _complete_ blocks and native sectors.
176 */
177 dm_oblock_t origin_blocks;
178 sector_t origin_sectors;
179
180 /*
181 * Size of the cache device in blocks.
182 */
183 dm_cblock_t cache_size;
184
185 /*
186 * Fields for converting from sectors to blocks.
187 */
188 uint32_t sectors_per_block;
189 int sectors_per_block_shift;
190
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000191 spinlock_t lock;
192 struct bio_list deferred_bios;
193 struct bio_list deferred_flush_bios;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000194 struct bio_list deferred_writethrough_bios;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000195 struct list_head quiesced_migrations;
196 struct list_head completed_migrations;
197 struct list_head need_commit_migrations;
198 sector_t migration_threshold;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000199 wait_queue_head_t migration_wait;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400200 atomic_t nr_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000201
Joe Thornber66cb1912013-10-30 17:11:58 +0000202 wait_queue_head_t quiescing_wait;
Joe Thornber238f8362013-10-30 17:29:30 +0000203 atomic_t quiescing;
Joe Thornber66cb1912013-10-30 17:11:58 +0000204 atomic_t quiescing_ack;
205
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000206 /*
207 * cache_size entries, dirty if set
208 */
209 dm_cblock_t nr_dirty;
210 unsigned long *dirty_bitset;
211
212 /*
213 * origin_blocks entries, discarded if set.
214 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000215 dm_dblock_t discard_nr_blocks;
216 unsigned long *discard_bitset;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400217 uint32_t discard_block_size; /* a power of 2 times sectors per block */
218
219 /*
220 * Rather than reconstructing the table line for the status we just
221 * save it and regurgitate.
222 */
223 unsigned nr_ctr_args;
224 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000225
226 struct dm_kcopyd_client *copier;
227 struct workqueue_struct *wq;
228 struct work_struct worker;
229
230 struct delayed_work waker;
231 unsigned long last_commit_jiffies;
232
233 struct dm_bio_prison *prison;
234 struct dm_deferred_set *all_io_ds;
235
236 mempool_t *migration_pool;
237 struct dm_cache_migration *next_migration;
238
239 struct dm_cache_policy *policy;
240 unsigned policy_nr_args;
241
242 bool need_tick_bio:1;
243 bool sized:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000244 bool commit_requested:1;
245 bool loaded_mappings:1;
246 bool loaded_discards:1;
247
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000248 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400249 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000250 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400251 struct cache_features features;
252
253 struct cache_stats stats;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000254};
255
256struct per_bio_data {
257 bool tick:1;
258 unsigned req_nr:2;
259 struct dm_deferred_entry *all_io_entry;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000260
Mike Snitzer19b00922013-04-05 15:36:34 +0100261 /*
262 * writethrough fields. These MUST remain at the end of this
263 * structure and the 'cache' member must be the first as it
Joe Thornberaeed1422013-05-10 14:37:18 +0100264 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100265 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000266 struct cache *cache;
267 dm_cblock_t cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400268 struct dm_hook_info hook_info;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100269 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000270};
271
272struct dm_cache_migration {
273 struct list_head list;
274 struct cache *cache;
275
276 unsigned long start_jiffies;
277 dm_oblock_t old_oblock;
278 dm_oblock_t new_oblock;
279 dm_cblock_t cblock;
280
281 bool err:1;
282 bool writeback:1;
283 bool demote:1;
284 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400285 bool requeue_holder:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000286
287 struct dm_bio_prison_cell *old_ocell;
288 struct dm_bio_prison_cell *new_ocell;
289};
290
291/*
292 * Processing a bio in the worker thread may require these memory
293 * allocations. We prealloc to avoid deadlocks (the same worker thread
294 * frees them back to the mempool).
295 */
296struct prealloc {
297 struct dm_cache_migration *mg;
298 struct dm_bio_prison_cell *cell1;
299 struct dm_bio_prison_cell *cell2;
300};
301
302static void wake_worker(struct cache *cache)
303{
304 queue_work(cache->wq, &cache->worker);
305}
306
307/*----------------------------------------------------------------*/
308
309static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
310{
311 /* FIXME: change to use a local slab. */
312 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
313}
314
315static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
316{
317 dm_bio_prison_free_cell(cache->prison, cell);
318}
319
320static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
321{
322 if (!p->mg) {
323 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
324 if (!p->mg)
325 return -ENOMEM;
326 }
327
328 if (!p->cell1) {
329 p->cell1 = alloc_prison_cell(cache);
330 if (!p->cell1)
331 return -ENOMEM;
332 }
333
334 if (!p->cell2) {
335 p->cell2 = alloc_prison_cell(cache);
336 if (!p->cell2)
337 return -ENOMEM;
338 }
339
340 return 0;
341}
342
343static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
344{
345 if (p->cell2)
346 free_prison_cell(cache, p->cell2);
347
348 if (p->cell1)
349 free_prison_cell(cache, p->cell1);
350
351 if (p->mg)
352 mempool_free(p->mg, cache->migration_pool);
353}
354
355static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
356{
357 struct dm_cache_migration *mg = p->mg;
358
359 BUG_ON(!mg);
360 p->mg = NULL;
361
362 return mg;
363}
364
365/*
366 * You must have a cell within the prealloc struct to return. If not this
367 * function will BUG() rather than returning NULL.
368 */
369static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
370{
371 struct dm_bio_prison_cell *r = NULL;
372
373 if (p->cell1) {
374 r = p->cell1;
375 p->cell1 = NULL;
376
377 } else if (p->cell2) {
378 r = p->cell2;
379 p->cell2 = NULL;
380 } else
381 BUG();
382
383 return r;
384}
385
386/*
387 * You can't have more than two cells in a prealloc struct. BUG() will be
388 * called if you try and overfill.
389 */
390static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
391{
392 if (!p->cell2)
393 p->cell2 = cell;
394
395 else if (!p->cell1)
396 p->cell1 = cell;
397
398 else
399 BUG();
400}
401
402/*----------------------------------------------------------------*/
403
404static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
405{
406 key->virtual = 0;
407 key->dev = 0;
408 key->block = from_oblock(oblock);
409}
410
411/*
412 * The caller hands in a preallocated cell, and a free function for it.
413 * The cell will be freed if there's an error, or if it wasn't used because
414 * a cell with that key already exists.
415 */
416typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
417
418static int bio_detain(struct cache *cache, dm_oblock_t oblock,
419 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
420 cell_free_fn free_fn, void *free_context,
421 struct dm_bio_prison_cell **cell_result)
422{
423 int r;
424 struct dm_cell_key key;
425
426 build_key(oblock, &key);
427 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
428 if (r)
429 free_fn(free_context, cell_prealloc);
430
431 return r;
432}
433
434static int get_cell(struct cache *cache,
435 dm_oblock_t oblock,
436 struct prealloc *structs,
437 struct dm_bio_prison_cell **cell_result)
438{
439 int r;
440 struct dm_cell_key key;
441 struct dm_bio_prison_cell *cell_prealloc;
442
443 cell_prealloc = prealloc_get_cell(structs);
444
445 build_key(oblock, &key);
446 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
447 if (r)
448 prealloc_put_cell(structs, cell_prealloc);
449
450 return r;
451}
452
Joe Thornberaeed1422013-05-10 14:37:18 +0100453/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000454
455static bool is_dirty(struct cache *cache, dm_cblock_t b)
456{
457 return test_bit(from_cblock(b), cache->dirty_bitset);
458}
459
460static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
461{
462 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
463 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) + 1);
464 policy_set_dirty(cache->policy, oblock);
465 }
466}
467
468static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
469{
470 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
471 policy_clear_dirty(cache->policy, oblock);
472 cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) - 1);
473 if (!from_cblock(cache->nr_dirty))
474 dm_table_event(cache->ti->table);
475 }
476}
477
478/*----------------------------------------------------------------*/
Joe Thornberaeed1422013-05-10 14:37:18 +0100479
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000480static bool block_size_is_power_of_two(struct cache *cache)
481{
482 return cache->sectors_per_block_shift >= 0;
483}
484
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100485/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
486#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
487__always_inline
488#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000489static dm_block_t block_div(dm_block_t b, uint32_t n)
490{
491 do_div(b, n);
492
493 return b;
494}
495
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000496static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
497{
Joe Thornber414dd672013-03-20 17:21:25 +0000498 uint32_t discard_blocks = cache->discard_block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000499 dm_block_t b = from_oblock(oblock);
500
501 if (!block_size_is_power_of_two(cache))
Joe Thornber414dd672013-03-20 17:21:25 +0000502 discard_blocks = discard_blocks / cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000503 else
504 discard_blocks >>= cache->sectors_per_block_shift;
505
Joe Thornber414dd672013-03-20 17:21:25 +0000506 b = block_div(b, discard_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000507
508 return to_dblock(b);
509}
510
511static void set_discard(struct cache *cache, dm_dblock_t b)
512{
513 unsigned long flags;
514
515 atomic_inc(&cache->stats.discard_count);
516
517 spin_lock_irqsave(&cache->lock, flags);
518 set_bit(from_dblock(b), cache->discard_bitset);
519 spin_unlock_irqrestore(&cache->lock, flags);
520}
521
522static void clear_discard(struct cache *cache, dm_dblock_t b)
523{
524 unsigned long flags;
525
526 spin_lock_irqsave(&cache->lock, flags);
527 clear_bit(from_dblock(b), cache->discard_bitset);
528 spin_unlock_irqrestore(&cache->lock, flags);
529}
530
531static bool is_discarded(struct cache *cache, dm_dblock_t b)
532{
533 int r;
534 unsigned long flags;
535
536 spin_lock_irqsave(&cache->lock, flags);
537 r = test_bit(from_dblock(b), cache->discard_bitset);
538 spin_unlock_irqrestore(&cache->lock, flags);
539
540 return r;
541}
542
543static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
544{
545 int r;
546 unsigned long flags;
547
548 spin_lock_irqsave(&cache->lock, flags);
549 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
550 cache->discard_bitset);
551 spin_unlock_irqrestore(&cache->lock, flags);
552
553 return r;
554}
555
556/*----------------------------------------------------------------*/
557
558static void load_stats(struct cache *cache)
559{
560 struct dm_cache_statistics stats;
561
562 dm_cache_metadata_get_stats(cache->cmd, &stats);
563 atomic_set(&cache->stats.read_hit, stats.read_hits);
564 atomic_set(&cache->stats.read_miss, stats.read_misses);
565 atomic_set(&cache->stats.write_hit, stats.write_hits);
566 atomic_set(&cache->stats.write_miss, stats.write_misses);
567}
568
569static void save_stats(struct cache *cache)
570{
571 struct dm_cache_statistics stats;
572
573 stats.read_hits = atomic_read(&cache->stats.read_hit);
574 stats.read_misses = atomic_read(&cache->stats.read_miss);
575 stats.write_hits = atomic_read(&cache->stats.write_hit);
576 stats.write_misses = atomic_read(&cache->stats.write_miss);
577
578 dm_cache_metadata_set_stats(cache->cmd, &stats);
579}
580
581/*----------------------------------------------------------------
582 * Per bio data
583 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100584
585/*
586 * If using writeback, leave out struct per_bio_data's writethrough fields.
587 */
588#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
589#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
590
Joe Thornber2ee57d52013-10-24 14:10:29 -0400591static bool writethrough_mode(struct cache_features *f)
592{
593 return f->io_mode == CM_IO_WRITETHROUGH;
594}
595
596static bool writeback_mode(struct cache_features *f)
597{
598 return f->io_mode == CM_IO_WRITEBACK;
599}
600
601static bool passthrough_mode(struct cache_features *f)
602{
603 return f->io_mode == CM_IO_PASSTHROUGH;
604}
605
Mike Snitzer19b00922013-04-05 15:36:34 +0100606static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000607{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400608 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100609}
610
611static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
612{
613 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000614 BUG_ON(!pb);
615 return pb;
616}
617
Mike Snitzer19b00922013-04-05 15:36:34 +0100618static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000619{
Mike Snitzer19b00922013-04-05 15:36:34 +0100620 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000621
622 pb->tick = false;
623 pb->req_nr = dm_bio_get_target_bio_nr(bio);
624 pb->all_io_entry = NULL;
625
626 return pb;
627}
628
629/*----------------------------------------------------------------
630 * Remapping
631 *--------------------------------------------------------------*/
632static void remap_to_origin(struct cache *cache, struct bio *bio)
633{
634 bio->bi_bdev = cache->origin_dev->bdev;
635}
636
637static void remap_to_cache(struct cache *cache, struct bio *bio,
638 dm_cblock_t cblock)
639{
640 sector_t bi_sector = bio->bi_sector;
641
642 bio->bi_bdev = cache->cache_dev->bdev;
643 if (!block_size_is_power_of_two(cache))
644 bio->bi_sector = (from_cblock(cblock) * cache->sectors_per_block) +
645 sector_div(bi_sector, cache->sectors_per_block);
646 else
647 bio->bi_sector = (from_cblock(cblock) << cache->sectors_per_block_shift) |
648 (bi_sector & (cache->sectors_per_block - 1));
649}
650
651static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
652{
653 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100654 size_t pb_data_size = get_per_bio_data_size(cache);
655 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000656
657 spin_lock_irqsave(&cache->lock, flags);
658 if (cache->need_tick_bio &&
659 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
660 pb->tick = true;
661 cache->need_tick_bio = false;
662 }
663 spin_unlock_irqrestore(&cache->lock, flags);
664}
665
666static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
667 dm_oblock_t oblock)
668{
669 check_if_tick_bio_needed(cache, bio);
670 remap_to_origin(cache, bio);
671 if (bio_data_dir(bio) == WRITE)
672 clear_discard(cache, oblock_to_dblock(cache, oblock));
673}
674
675static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
676 dm_oblock_t oblock, dm_cblock_t cblock)
677{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100678 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000679 remap_to_cache(cache, bio, cblock);
680 if (bio_data_dir(bio) == WRITE) {
681 set_dirty(cache, oblock, cblock);
682 clear_discard(cache, oblock_to_dblock(cache, oblock));
683 }
684}
685
686static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
687{
688 sector_t block_nr = bio->bi_sector;
689
690 if (!block_size_is_power_of_two(cache))
691 (void) sector_div(block_nr, cache->sectors_per_block);
692 else
693 block_nr >>= cache->sectors_per_block_shift;
694
695 return to_oblock(block_nr);
696}
697
698static int bio_triggers_commit(struct cache *cache, struct bio *bio)
699{
700 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
701}
702
703static void issue(struct cache *cache, struct bio *bio)
704{
705 unsigned long flags;
706
707 if (!bio_triggers_commit(cache, bio)) {
708 generic_make_request(bio);
709 return;
710 }
711
712 /*
713 * Batch together any bios that trigger commits and then issue a
714 * single commit for them in do_worker().
715 */
716 spin_lock_irqsave(&cache->lock, flags);
717 cache->commit_requested = true;
718 bio_list_add(&cache->deferred_flush_bios, bio);
719 spin_unlock_irqrestore(&cache->lock, flags);
720}
721
Joe Thornbere2e74d62013-03-20 17:21:27 +0000722static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
723{
724 unsigned long flags;
725
726 spin_lock_irqsave(&cache->lock, flags);
727 bio_list_add(&cache->deferred_writethrough_bios, bio);
728 spin_unlock_irqrestore(&cache->lock, flags);
729
730 wake_worker(cache);
731}
732
733static void writethrough_endio(struct bio *bio, int err)
734{
Mike Snitzer19b00922013-04-05 15:36:34 +0100735 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400736
737 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000738
739 if (err) {
740 bio_endio(bio, err);
741 return;
742 }
743
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100744 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000745 remap_to_cache(pb->cache, bio, pb->cblock);
746
747 /*
748 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed1422013-05-10 14:37:18 +0100749 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000750 * worker thread.
751 */
752 defer_writethrough_bio(pb->cache, bio);
753}
754
755/*
756 * When running in writethrough mode we need to send writes to clean blocks
757 * to both the cache and origin devices. In future we'd like to clone the
758 * bio and send them in parallel, but for now we're doing them in
759 * series as this is easier.
760 */
761static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
762 dm_oblock_t oblock, dm_cblock_t cblock)
763{
Mike Snitzer19b00922013-04-05 15:36:34 +0100764 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000765
766 pb->cache = cache;
767 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400768 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100769 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000770
771 remap_to_origin_clear_discard(pb->cache, bio, oblock);
772}
773
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000774/*----------------------------------------------------------------
775 * Migration processing
776 *
777 * Migration covers moving data from the origin device to the cache, or
778 * vice versa.
779 *--------------------------------------------------------------*/
780static void free_migration(struct dm_cache_migration *mg)
781{
782 mempool_free(mg, mg->cache->migration_pool);
783}
784
785static void inc_nr_migrations(struct cache *cache)
786{
787 atomic_inc(&cache->nr_migrations);
788}
789
790static void dec_nr_migrations(struct cache *cache)
791{
792 atomic_dec(&cache->nr_migrations);
793
794 /*
795 * Wake the worker in case we're suspending the target.
796 */
797 wake_up(&cache->migration_wait);
798}
799
800static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
801 bool holder)
802{
803 (holder ? dm_cell_release : dm_cell_release_no_holder)
804 (cache->prison, cell, &cache->deferred_bios);
805 free_prison_cell(cache, cell);
806}
807
808static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
809 bool holder)
810{
811 unsigned long flags;
812
813 spin_lock_irqsave(&cache->lock, flags);
814 __cell_defer(cache, cell, holder);
815 spin_unlock_irqrestore(&cache->lock, flags);
816
817 wake_worker(cache);
818}
819
820static void cleanup_migration(struct dm_cache_migration *mg)
821{
Joe Thornber66cb1912013-10-30 17:11:58 +0000822 struct cache *cache = mg->cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000823 free_migration(mg);
Joe Thornber66cb1912013-10-30 17:11:58 +0000824 dec_nr_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000825}
826
827static void migration_failure(struct dm_cache_migration *mg)
828{
829 struct cache *cache = mg->cache;
830
831 if (mg->writeback) {
832 DMWARN_LIMIT("writeback failed; couldn't copy block");
833 set_dirty(cache, mg->old_oblock, mg->cblock);
834 cell_defer(cache, mg->old_ocell, false);
835
836 } else if (mg->demote) {
837 DMWARN_LIMIT("demotion failed; couldn't copy block");
838 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
839
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200840 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000841 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200842 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000843 } else {
844 DMWARN_LIMIT("promotion failed; couldn't copy block");
845 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200846 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000847 }
848
849 cleanup_migration(mg);
850}
851
852static void migration_success_pre_commit(struct dm_cache_migration *mg)
853{
854 unsigned long flags;
855 struct cache *cache = mg->cache;
856
857 if (mg->writeback) {
858 cell_defer(cache, mg->old_ocell, false);
859 clear_dirty(cache, mg->old_oblock, mg->cblock);
860 cleanup_migration(mg);
861 return;
862
863 } else if (mg->demote) {
864 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
865 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
866 policy_force_mapping(cache->policy, mg->new_oblock,
867 mg->old_oblock);
868 if (mg->promote)
869 cell_defer(cache, mg->new_ocell, true);
870 cleanup_migration(mg);
871 return;
872 }
873 } else {
874 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
875 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
876 policy_remove_mapping(cache->policy, mg->new_oblock);
877 cleanup_migration(mg);
878 return;
879 }
880 }
881
882 spin_lock_irqsave(&cache->lock, flags);
883 list_add_tail(&mg->list, &cache->need_commit_migrations);
884 cache->commit_requested = true;
885 spin_unlock_irqrestore(&cache->lock, flags);
886}
887
888static void migration_success_post_commit(struct dm_cache_migration *mg)
889{
890 unsigned long flags;
891 struct cache *cache = mg->cache;
892
893 if (mg->writeback) {
894 DMWARN("writeback unexpectedly triggered commit");
895 return;
896
897 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200898 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000899
900 if (mg->promote) {
901 mg->demote = false;
902
903 spin_lock_irqsave(&cache->lock, flags);
904 list_add_tail(&mg->list, &cache->quiesced_migrations);
905 spin_unlock_irqrestore(&cache->lock, flags);
906
907 } else
908 cleanup_migration(mg);
909
910 } else {
Joe Thornberc9d28d52013-10-31 13:55:48 -0400911 if (mg->requeue_holder)
912 cell_defer(cache, mg->new_ocell, true);
913 else {
914 bio_endio(mg->new_ocell->holder, 0);
915 cell_defer(cache, mg->new_ocell, false);
916 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000917 clear_dirty(cache, mg->new_oblock, mg->cblock);
918 cleanup_migration(mg);
919 }
920}
921
922static void copy_complete(int read_err, unsigned long write_err, void *context)
923{
924 unsigned long flags;
925 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
926 struct cache *cache = mg->cache;
927
928 if (read_err || write_err)
929 mg->err = true;
930
931 spin_lock_irqsave(&cache->lock, flags);
932 list_add_tail(&mg->list, &cache->completed_migrations);
933 spin_unlock_irqrestore(&cache->lock, flags);
934
935 wake_worker(cache);
936}
937
938static void issue_copy_real(struct dm_cache_migration *mg)
939{
940 int r;
941 struct dm_io_region o_region, c_region;
942 struct cache *cache = mg->cache;
943
944 o_region.bdev = cache->origin_dev->bdev;
945 o_region.count = cache->sectors_per_block;
946
947 c_region.bdev = cache->cache_dev->bdev;
948 c_region.sector = from_cblock(mg->cblock) * cache->sectors_per_block;
949 c_region.count = cache->sectors_per_block;
950
951 if (mg->writeback || mg->demote) {
952 /* demote */
953 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
954 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
955 } else {
956 /* promote */
957 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
958 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
959 }
960
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +0200961 if (r < 0) {
962 DMERR_LIMIT("issuing migration failed");
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000963 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +0200964 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000965}
966
Joe Thornberc9d28d52013-10-31 13:55:48 -0400967static void overwrite_endio(struct bio *bio, int err)
968{
969 struct dm_cache_migration *mg = bio->bi_private;
970 struct cache *cache = mg->cache;
971 size_t pb_data_size = get_per_bio_data_size(cache);
972 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
973 unsigned long flags;
974
975 if (err)
976 mg->err = true;
977
978 spin_lock_irqsave(&cache->lock, flags);
979 list_add_tail(&mg->list, &cache->completed_migrations);
980 dm_unhook_bio(&pb->hook_info, bio);
981 mg->requeue_holder = false;
982 spin_unlock_irqrestore(&cache->lock, flags);
983
984 wake_worker(cache);
985}
986
987static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
988{
989 size_t pb_data_size = get_per_bio_data_size(mg->cache);
990 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
991
992 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
993 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
994 generic_make_request(bio);
995}
996
997static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
998{
999 return (bio_data_dir(bio) == WRITE) &&
1000 (bio->bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
1001}
1002
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001003static void avoid_copy(struct dm_cache_migration *mg)
1004{
1005 atomic_inc(&mg->cache->stats.copies_avoided);
1006 migration_success_pre_commit(mg);
1007}
1008
1009static void issue_copy(struct dm_cache_migration *mg)
1010{
1011 bool avoid;
1012 struct cache *cache = mg->cache;
1013
1014 if (mg->writeback || mg->demote)
1015 avoid = !is_dirty(cache, mg->cblock) ||
1016 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001017 else {
1018 struct bio *bio = mg->new_ocell->holder;
1019
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001020 avoid = is_discarded_oblock(cache, mg->new_oblock);
1021
Joe Thornberc9d28d52013-10-31 13:55:48 -04001022 if (!avoid && bio_writes_complete_block(cache, bio)) {
1023 issue_overwrite(mg, bio);
1024 return;
1025 }
1026 }
1027
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001028 avoid ? avoid_copy(mg) : issue_copy_real(mg);
1029}
1030
1031static void complete_migration(struct dm_cache_migration *mg)
1032{
1033 if (mg->err)
1034 migration_failure(mg);
1035 else
1036 migration_success_pre_commit(mg);
1037}
1038
1039static void process_migrations(struct cache *cache, struct list_head *head,
1040 void (*fn)(struct dm_cache_migration *))
1041{
1042 unsigned long flags;
1043 struct list_head list;
1044 struct dm_cache_migration *mg, *tmp;
1045
1046 INIT_LIST_HEAD(&list);
1047 spin_lock_irqsave(&cache->lock, flags);
1048 list_splice_init(head, &list);
1049 spin_unlock_irqrestore(&cache->lock, flags);
1050
1051 list_for_each_entry_safe(mg, tmp, &list, list)
1052 fn(mg);
1053}
1054
1055static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1056{
1057 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1058}
1059
1060static void queue_quiesced_migration(struct dm_cache_migration *mg)
1061{
1062 unsigned long flags;
1063 struct cache *cache = mg->cache;
1064
1065 spin_lock_irqsave(&cache->lock, flags);
1066 __queue_quiesced_migration(mg);
1067 spin_unlock_irqrestore(&cache->lock, flags);
1068
1069 wake_worker(cache);
1070}
1071
1072static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1073{
1074 unsigned long flags;
1075 struct dm_cache_migration *mg, *tmp;
1076
1077 spin_lock_irqsave(&cache->lock, flags);
1078 list_for_each_entry_safe(mg, tmp, work, list)
1079 __queue_quiesced_migration(mg);
1080 spin_unlock_irqrestore(&cache->lock, flags);
1081
1082 wake_worker(cache);
1083}
1084
1085static void check_for_quiesced_migrations(struct cache *cache,
1086 struct per_bio_data *pb)
1087{
1088 struct list_head work;
1089
1090 if (!pb->all_io_entry)
1091 return;
1092
1093 INIT_LIST_HEAD(&work);
1094 if (pb->all_io_entry)
1095 dm_deferred_entry_dec(pb->all_io_entry, &work);
1096
1097 if (!list_empty(&work))
1098 queue_quiesced_migrations(cache, &work);
1099}
1100
1101static void quiesce_migration(struct dm_cache_migration *mg)
1102{
1103 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1104 queue_quiesced_migration(mg);
1105}
1106
1107static void promote(struct cache *cache, struct prealloc *structs,
1108 dm_oblock_t oblock, dm_cblock_t cblock,
1109 struct dm_bio_prison_cell *cell)
1110{
1111 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1112
1113 mg->err = false;
1114 mg->writeback = false;
1115 mg->demote = false;
1116 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001117 mg->requeue_holder = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001118 mg->cache = cache;
1119 mg->new_oblock = oblock;
1120 mg->cblock = cblock;
1121 mg->old_ocell = NULL;
1122 mg->new_ocell = cell;
1123 mg->start_jiffies = jiffies;
1124
1125 inc_nr_migrations(cache);
1126 quiesce_migration(mg);
1127}
1128
1129static void writeback(struct cache *cache, struct prealloc *structs,
1130 dm_oblock_t oblock, dm_cblock_t cblock,
1131 struct dm_bio_prison_cell *cell)
1132{
1133 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1134
1135 mg->err = false;
1136 mg->writeback = true;
1137 mg->demote = false;
1138 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001139 mg->requeue_holder = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001140 mg->cache = cache;
1141 mg->old_oblock = oblock;
1142 mg->cblock = cblock;
1143 mg->old_ocell = cell;
1144 mg->new_ocell = NULL;
1145 mg->start_jiffies = jiffies;
1146
1147 inc_nr_migrations(cache);
1148 quiesce_migration(mg);
1149}
1150
1151static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1152 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1153 dm_cblock_t cblock,
1154 struct dm_bio_prison_cell *old_ocell,
1155 struct dm_bio_prison_cell *new_ocell)
1156{
1157 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1158
1159 mg->err = false;
1160 mg->writeback = false;
1161 mg->demote = true;
1162 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001163 mg->requeue_holder = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001164 mg->cache = cache;
1165 mg->old_oblock = old_oblock;
1166 mg->new_oblock = new_oblock;
1167 mg->cblock = cblock;
1168 mg->old_ocell = old_ocell;
1169 mg->new_ocell = new_ocell;
1170 mg->start_jiffies = jiffies;
1171
1172 inc_nr_migrations(cache);
1173 quiesce_migration(mg);
1174}
1175
Joe Thornber2ee57d52013-10-24 14:10:29 -04001176/*
1177 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1178 * block are thrown away.
1179 */
1180static void invalidate(struct cache *cache, struct prealloc *structs,
1181 dm_oblock_t oblock, dm_cblock_t cblock,
1182 struct dm_bio_prison_cell *cell)
1183{
1184 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1185
1186 mg->err = false;
1187 mg->writeback = false;
1188 mg->demote = true;
1189 mg->promote = false;
1190 mg->requeue_holder = true;
1191 mg->cache = cache;
1192 mg->old_oblock = oblock;
1193 mg->cblock = cblock;
1194 mg->old_ocell = cell;
1195 mg->new_ocell = NULL;
1196 mg->start_jiffies = jiffies;
1197
1198 inc_nr_migrations(cache);
1199 quiesce_migration(mg);
1200}
1201
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001202/*----------------------------------------------------------------
1203 * bio processing
1204 *--------------------------------------------------------------*/
1205static void defer_bio(struct cache *cache, struct bio *bio)
1206{
1207 unsigned long flags;
1208
1209 spin_lock_irqsave(&cache->lock, flags);
1210 bio_list_add(&cache->deferred_bios, bio);
1211 spin_unlock_irqrestore(&cache->lock, flags);
1212
1213 wake_worker(cache);
1214}
1215
1216static void process_flush_bio(struct cache *cache, struct bio *bio)
1217{
Mike Snitzer19b00922013-04-05 15:36:34 +01001218 size_t pb_data_size = get_per_bio_data_size(cache);
1219 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001220
1221 BUG_ON(bio->bi_size);
1222 if (!pb->req_nr)
1223 remap_to_origin(cache, bio);
1224 else
1225 remap_to_cache(cache, bio, 0);
1226
1227 issue(cache, bio);
1228}
1229
1230/*
1231 * People generally discard large parts of a device, eg, the whole device
1232 * when formatting. Splitting these large discards up into cache block
1233 * sized ios and then quiescing (always neccessary for discard) takes too
1234 * long.
1235 *
1236 * We keep it simple, and allow any size of discard to come in, and just
1237 * mark off blocks on the discard bitset. No passdown occurs!
1238 *
1239 * To implement passdown we need to change the bio_prison such that a cell
1240 * can have a key that spans many blocks.
1241 */
1242static void process_discard_bio(struct cache *cache, struct bio *bio)
1243{
1244 dm_block_t start_block = dm_sector_div_up(bio->bi_sector,
1245 cache->discard_block_size);
1246 dm_block_t end_block = bio->bi_sector + bio_sectors(bio);
1247 dm_block_t b;
1248
Joe Thornber414dd672013-03-20 17:21:25 +00001249 end_block = block_div(end_block, cache->discard_block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001250
1251 for (b = start_block; b < end_block; b++)
1252 set_discard(cache, to_dblock(b));
1253
1254 bio_endio(bio, 0);
1255}
1256
1257static bool spare_migration_bandwidth(struct cache *cache)
1258{
1259 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1260 cache->sectors_per_block;
1261 return current_volume < cache->migration_threshold;
1262}
1263
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001264static void inc_hit_counter(struct cache *cache, struct bio *bio)
1265{
1266 atomic_inc(bio_data_dir(bio) == READ ?
1267 &cache->stats.read_hit : &cache->stats.write_hit);
1268}
1269
1270static void inc_miss_counter(struct cache *cache, struct bio *bio)
1271{
1272 atomic_inc(bio_data_dir(bio) == READ ?
1273 &cache->stats.read_miss : &cache->stats.write_miss);
1274}
1275
Joe Thornber2ee57d52013-10-24 14:10:29 -04001276static void issue_cache_bio(struct cache *cache, struct bio *bio,
1277 struct per_bio_data *pb,
1278 dm_oblock_t oblock, dm_cblock_t cblock)
1279{
1280 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1281 remap_to_cache_dirty(cache, bio, oblock, cblock);
1282 issue(cache, bio);
1283}
1284
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001285static void process_bio(struct cache *cache, struct prealloc *structs,
1286 struct bio *bio)
1287{
1288 int r;
1289 bool release_cell = true;
1290 dm_oblock_t block = get_bio_block(cache, bio);
1291 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1292 struct policy_result lookup_result;
Mike Snitzer19b00922013-04-05 15:36:34 +01001293 size_t pb_data_size = get_per_bio_data_size(cache);
1294 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001295 bool discarded_block = is_discarded_oblock(cache, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001296 bool passthrough = passthrough_mode(&cache->features);
1297 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001298
1299 /*
1300 * Check to see if that block is currently migrating.
1301 */
1302 cell_prealloc = prealloc_get_cell(structs);
1303 r = bio_detain(cache, block, bio, cell_prealloc,
1304 (cell_free_fn) prealloc_put_cell,
1305 structs, &new_ocell);
1306 if (r > 0)
1307 return;
1308
1309 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1310 bio, &lookup_result);
1311
1312 if (r == -EWOULDBLOCK)
1313 /* migration has been denied */
1314 lookup_result.op = POLICY_MISS;
1315
1316 switch (lookup_result.op) {
1317 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001318 if (passthrough) {
1319 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001320
Joe Thornber2ee57d52013-10-24 14:10:29 -04001321 /*
1322 * Passthrough always maps to the origin,
1323 * invalidating any cache blocks that are written
1324 * to.
1325 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001326
Joe Thornber2ee57d52013-10-24 14:10:29 -04001327 if (bio_data_dir(bio) == WRITE) {
1328 atomic_inc(&cache->stats.demotion);
1329 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1330 release_cell = false;
1331
1332 } else {
1333 /* FIXME: factor out issue_origin() */
1334 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1335 remap_to_origin_clear_discard(cache, bio, block);
1336 issue(cache, bio);
1337 }
1338 } else {
1339 inc_hit_counter(cache, bio);
1340
1341 if (bio_data_dir(bio) == WRITE &&
1342 writethrough_mode(&cache->features) &&
1343 !is_dirty(cache, lookup_result.cblock)) {
1344 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
1345 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
1346 issue(cache, bio);
1347 } else
1348 issue_cache_bio(cache, bio, pb, block, lookup_result.cblock);
1349 }
1350
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001351 break;
1352
1353 case POLICY_MISS:
1354 inc_miss_counter(cache, bio);
1355 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001356 remap_to_origin_clear_discard(cache, bio, block);
1357 issue(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001358 break;
1359
1360 case POLICY_NEW:
1361 atomic_inc(&cache->stats.promotion);
1362 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1363 release_cell = false;
1364 break;
1365
1366 case POLICY_REPLACE:
1367 cell_prealloc = prealloc_get_cell(structs);
1368 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1369 (cell_free_fn) prealloc_put_cell,
1370 structs, &old_ocell);
1371 if (r > 0) {
1372 /*
1373 * We have to be careful to avoid lock inversion of
1374 * the cells. So we back off, and wait for the
1375 * old_ocell to become free.
1376 */
1377 policy_force_mapping(cache->policy, block,
1378 lookup_result.old_oblock);
1379 atomic_inc(&cache->stats.cache_cell_clash);
1380 break;
1381 }
1382 atomic_inc(&cache->stats.demotion);
1383 atomic_inc(&cache->stats.promotion);
1384
1385 demote_then_promote(cache, structs, lookup_result.old_oblock,
1386 block, lookup_result.cblock,
1387 old_ocell, new_ocell);
1388 release_cell = false;
1389 break;
1390
1391 default:
1392 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1393 (unsigned) lookup_result.op);
1394 bio_io_error(bio);
1395 }
1396
1397 if (release_cell)
1398 cell_defer(cache, new_ocell, false);
1399}
1400
1401static int need_commit_due_to_time(struct cache *cache)
1402{
1403 return jiffies < cache->last_commit_jiffies ||
1404 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1405}
1406
1407static int commit_if_needed(struct cache *cache)
1408{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001409 int r = 0;
1410
1411 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1412 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001413 atomic_inc(&cache->stats.commit_count);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001414 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001415 r = dm_cache_commit(cache->cmd, false);
1416 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001417 }
1418
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001419 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001420}
1421
1422static void process_deferred_bios(struct cache *cache)
1423{
1424 unsigned long flags;
1425 struct bio_list bios;
1426 struct bio *bio;
1427 struct prealloc structs;
1428
1429 memset(&structs, 0, sizeof(structs));
1430 bio_list_init(&bios);
1431
1432 spin_lock_irqsave(&cache->lock, flags);
1433 bio_list_merge(&bios, &cache->deferred_bios);
1434 bio_list_init(&cache->deferred_bios);
1435 spin_unlock_irqrestore(&cache->lock, flags);
1436
1437 while (!bio_list_empty(&bios)) {
1438 /*
1439 * If we've got no free migration structs, and processing
1440 * this bio might require one, we pause until there are some
1441 * prepared mappings to process.
1442 */
1443 if (prealloc_data_structs(cache, &structs)) {
1444 spin_lock_irqsave(&cache->lock, flags);
1445 bio_list_merge(&cache->deferred_bios, &bios);
1446 spin_unlock_irqrestore(&cache->lock, flags);
1447 break;
1448 }
1449
1450 bio = bio_list_pop(&bios);
1451
1452 if (bio->bi_rw & REQ_FLUSH)
1453 process_flush_bio(cache, bio);
1454 else if (bio->bi_rw & REQ_DISCARD)
1455 process_discard_bio(cache, bio);
1456 else
1457 process_bio(cache, &structs, bio);
1458 }
1459
1460 prealloc_free_structs(cache, &structs);
1461}
1462
1463static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1464{
1465 unsigned long flags;
1466 struct bio_list bios;
1467 struct bio *bio;
1468
1469 bio_list_init(&bios);
1470
1471 spin_lock_irqsave(&cache->lock, flags);
1472 bio_list_merge(&bios, &cache->deferred_flush_bios);
1473 bio_list_init(&cache->deferred_flush_bios);
1474 spin_unlock_irqrestore(&cache->lock, flags);
1475
1476 while ((bio = bio_list_pop(&bios)))
1477 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1478}
1479
Joe Thornbere2e74d62013-03-20 17:21:27 +00001480static void process_deferred_writethrough_bios(struct cache *cache)
1481{
1482 unsigned long flags;
1483 struct bio_list bios;
1484 struct bio *bio;
1485
1486 bio_list_init(&bios);
1487
1488 spin_lock_irqsave(&cache->lock, flags);
1489 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1490 bio_list_init(&cache->deferred_writethrough_bios);
1491 spin_unlock_irqrestore(&cache->lock, flags);
1492
1493 while ((bio = bio_list_pop(&bios)))
1494 generic_make_request(bio);
1495}
1496
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001497static void writeback_some_dirty_blocks(struct cache *cache)
1498{
1499 int r = 0;
1500 dm_oblock_t oblock;
1501 dm_cblock_t cblock;
1502 struct prealloc structs;
1503 struct dm_bio_prison_cell *old_ocell;
1504
1505 memset(&structs, 0, sizeof(structs));
1506
1507 while (spare_migration_bandwidth(cache)) {
1508 if (prealloc_data_structs(cache, &structs))
1509 break;
1510
1511 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1512 if (r)
1513 break;
1514
1515 r = get_cell(cache, oblock, &structs, &old_ocell);
1516 if (r) {
1517 policy_set_dirty(cache->policy, oblock);
1518 break;
1519 }
1520
1521 writeback(cache, &structs, oblock, cblock, old_ocell);
1522 }
1523
1524 prealloc_free_structs(cache, &structs);
1525}
1526
1527/*----------------------------------------------------------------
1528 * Main worker loop
1529 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001530static bool is_quiescing(struct cache *cache)
1531{
Joe Thornber238f8362013-10-30 17:29:30 +00001532 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001533}
1534
Joe Thornber66cb1912013-10-30 17:11:58 +00001535static void ack_quiescing(struct cache *cache)
1536{
1537 if (is_quiescing(cache)) {
1538 atomic_inc(&cache->quiescing_ack);
1539 wake_up(&cache->quiescing_wait);
1540 }
1541}
1542
1543static void wait_for_quiescing_ack(struct cache *cache)
1544{
1545 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1546}
1547
1548static void start_quiescing(struct cache *cache)
1549{
Joe Thornber238f8362013-10-30 17:29:30 +00001550 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00001551 wait_for_quiescing_ack(cache);
1552}
1553
1554static void stop_quiescing(struct cache *cache)
1555{
Joe Thornber238f8362013-10-30 17:29:30 +00001556 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00001557 atomic_set(&cache->quiescing_ack, 0);
1558}
1559
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001560static void wait_for_migrations(struct cache *cache)
1561{
1562 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1563}
1564
1565static void stop_worker(struct cache *cache)
1566{
1567 cancel_delayed_work(&cache->waker);
1568 flush_workqueue(cache->wq);
1569}
1570
1571static void requeue_deferred_io(struct cache *cache)
1572{
1573 struct bio *bio;
1574 struct bio_list bios;
1575
1576 bio_list_init(&bios);
1577 bio_list_merge(&bios, &cache->deferred_bios);
1578 bio_list_init(&cache->deferred_bios);
1579
1580 while ((bio = bio_list_pop(&bios)))
1581 bio_endio(bio, DM_ENDIO_REQUEUE);
1582}
1583
1584static int more_work(struct cache *cache)
1585{
1586 if (is_quiescing(cache))
1587 return !list_empty(&cache->quiesced_migrations) ||
1588 !list_empty(&cache->completed_migrations) ||
1589 !list_empty(&cache->need_commit_migrations);
1590 else
1591 return !bio_list_empty(&cache->deferred_bios) ||
1592 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001593 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001594 !list_empty(&cache->quiesced_migrations) ||
1595 !list_empty(&cache->completed_migrations) ||
1596 !list_empty(&cache->need_commit_migrations);
1597}
1598
1599static void do_worker(struct work_struct *ws)
1600{
1601 struct cache *cache = container_of(ws, struct cache, worker);
1602
1603 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001604 if (!is_quiescing(cache)) {
1605 writeback_some_dirty_blocks(cache);
1606 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001607 process_deferred_bios(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001608 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001609
1610 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1611 process_migrations(cache, &cache->completed_migrations, complete_migration);
1612
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001613 if (commit_if_needed(cache)) {
1614 process_deferred_flush_bios(cache, false);
1615
1616 /*
1617 * FIXME: rollback metadata or just go into a
1618 * failure mode and error everything
1619 */
1620 } else {
1621 process_deferred_flush_bios(cache, true);
1622 process_migrations(cache, &cache->need_commit_migrations,
1623 migration_success_post_commit);
1624 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001625
1626 ack_quiescing(cache);
1627
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001628 } while (more_work(cache));
1629}
1630
1631/*
1632 * We want to commit periodically so that not too much
1633 * unwritten metadata builds up.
1634 */
1635static void do_waker(struct work_struct *ws)
1636{
1637 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001638 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001639 wake_worker(cache);
1640 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1641}
1642
1643/*----------------------------------------------------------------*/
1644
1645static int is_congested(struct dm_dev *dev, int bdi_bits)
1646{
1647 struct request_queue *q = bdev_get_queue(dev->bdev);
1648 return bdi_congested(&q->backing_dev_info, bdi_bits);
1649}
1650
1651static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1652{
1653 struct cache *cache = container_of(cb, struct cache, callbacks);
1654
1655 return is_congested(cache->origin_dev, bdi_bits) ||
1656 is_congested(cache->cache_dev, bdi_bits);
1657}
1658
1659/*----------------------------------------------------------------
1660 * Target methods
1661 *--------------------------------------------------------------*/
1662
1663/*
1664 * This function gets called on the error paths of the constructor, so we
1665 * have to cope with a partially initialised struct.
1666 */
1667static void destroy(struct cache *cache)
1668{
1669 unsigned i;
1670
1671 if (cache->next_migration)
1672 mempool_free(cache->next_migration, cache->migration_pool);
1673
1674 if (cache->migration_pool)
1675 mempool_destroy(cache->migration_pool);
1676
1677 if (cache->all_io_ds)
1678 dm_deferred_set_destroy(cache->all_io_ds);
1679
1680 if (cache->prison)
1681 dm_bio_prison_destroy(cache->prison);
1682
1683 if (cache->wq)
1684 destroy_workqueue(cache->wq);
1685
1686 if (cache->dirty_bitset)
1687 free_bitset(cache->dirty_bitset);
1688
1689 if (cache->discard_bitset)
1690 free_bitset(cache->discard_bitset);
1691
1692 if (cache->copier)
1693 dm_kcopyd_client_destroy(cache->copier);
1694
1695 if (cache->cmd)
1696 dm_cache_metadata_close(cache->cmd);
1697
1698 if (cache->metadata_dev)
1699 dm_put_device(cache->ti, cache->metadata_dev);
1700
1701 if (cache->origin_dev)
1702 dm_put_device(cache->ti, cache->origin_dev);
1703
1704 if (cache->cache_dev)
1705 dm_put_device(cache->ti, cache->cache_dev);
1706
1707 if (cache->policy)
1708 dm_cache_policy_destroy(cache->policy);
1709
1710 for (i = 0; i < cache->nr_ctr_args ; i++)
1711 kfree(cache->ctr_args[i]);
1712 kfree(cache->ctr_args);
1713
1714 kfree(cache);
1715}
1716
1717static void cache_dtr(struct dm_target *ti)
1718{
1719 struct cache *cache = ti->private;
1720
1721 destroy(cache);
1722}
1723
1724static sector_t get_dev_size(struct dm_dev *dev)
1725{
1726 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1727}
1728
1729/*----------------------------------------------------------------*/
1730
1731/*
1732 * Construct a cache device mapping.
1733 *
1734 * cache <metadata dev> <cache dev> <origin dev> <block size>
1735 * <#feature args> [<feature arg>]*
1736 * <policy> <#policy args> [<policy arg>]*
1737 *
1738 * metadata dev : fast device holding the persistent metadata
1739 * cache dev : fast device holding cached data blocks
1740 * origin dev : slow device holding original data blocks
1741 * block size : cache unit size in sectors
1742 *
1743 * #feature args : number of feature arguments passed
1744 * feature args : writethrough. (The default is writeback.)
1745 *
1746 * policy : the replacement policy to use
1747 * #policy args : an even number of policy arguments corresponding
1748 * to key/value pairs passed to the policy
1749 * policy args : key/value pairs passed to the policy
1750 * E.g. 'sequential_threshold 1024'
1751 * See cache-policies.txt for details.
1752 *
1753 * Optional feature arguments are:
1754 * writethrough : write through caching that prohibits cache block
1755 * content from being different from origin block content.
1756 * Without this argument, the default behaviour is to write
1757 * back cache block contents later for performance reasons,
1758 * so they may differ from the corresponding origin blocks.
1759 */
1760struct cache_args {
1761 struct dm_target *ti;
1762
1763 struct dm_dev *metadata_dev;
1764
1765 struct dm_dev *cache_dev;
1766 sector_t cache_sectors;
1767
1768 struct dm_dev *origin_dev;
1769 sector_t origin_sectors;
1770
1771 uint32_t block_size;
1772
1773 const char *policy_name;
1774 int policy_argc;
1775 const char **policy_argv;
1776
1777 struct cache_features features;
1778};
1779
1780static void destroy_cache_args(struct cache_args *ca)
1781{
1782 if (ca->metadata_dev)
1783 dm_put_device(ca->ti, ca->metadata_dev);
1784
1785 if (ca->cache_dev)
1786 dm_put_device(ca->ti, ca->cache_dev);
1787
1788 if (ca->origin_dev)
1789 dm_put_device(ca->ti, ca->origin_dev);
1790
1791 kfree(ca);
1792}
1793
1794static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1795{
1796 if (!as->argc) {
1797 *error = "Insufficient args";
1798 return false;
1799 }
1800
1801 return true;
1802}
1803
1804static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1805 char **error)
1806{
1807 int r;
1808 sector_t metadata_dev_size;
1809 char b[BDEVNAME_SIZE];
1810
1811 if (!at_least_one_arg(as, error))
1812 return -EINVAL;
1813
1814 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1815 &ca->metadata_dev);
1816 if (r) {
1817 *error = "Error opening metadata device";
1818 return r;
1819 }
1820
1821 metadata_dev_size = get_dev_size(ca->metadata_dev);
1822 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1823 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1824 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1825
1826 return 0;
1827}
1828
1829static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1830 char **error)
1831{
1832 int r;
1833
1834 if (!at_least_one_arg(as, error))
1835 return -EINVAL;
1836
1837 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1838 &ca->cache_dev);
1839 if (r) {
1840 *error = "Error opening cache device";
1841 return r;
1842 }
1843 ca->cache_sectors = get_dev_size(ca->cache_dev);
1844
1845 return 0;
1846}
1847
1848static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1849 char **error)
1850{
1851 int r;
1852
1853 if (!at_least_one_arg(as, error))
1854 return -EINVAL;
1855
1856 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1857 &ca->origin_dev);
1858 if (r) {
1859 *error = "Error opening origin device";
1860 return r;
1861 }
1862
1863 ca->origin_sectors = get_dev_size(ca->origin_dev);
1864 if (ca->ti->len > ca->origin_sectors) {
1865 *error = "Device size larger than cached device";
1866 return -EINVAL;
1867 }
1868
1869 return 0;
1870}
1871
1872static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1873 char **error)
1874{
Mike Snitzer05473042013-08-16 10:54:19 -04001875 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001876
1877 if (!at_least_one_arg(as, error))
1878 return -EINVAL;
1879
Mike Snitzer05473042013-08-16 10:54:19 -04001880 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
1881 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1882 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1883 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001884 *error = "Invalid data block size";
1885 return -EINVAL;
1886 }
1887
Mike Snitzer05473042013-08-16 10:54:19 -04001888 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001889 *error = "Data block size is larger than the cache device";
1890 return -EINVAL;
1891 }
1892
Mike Snitzer05473042013-08-16 10:54:19 -04001893 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001894
1895 return 0;
1896}
1897
1898static void init_features(struct cache_features *cf)
1899{
1900 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001901 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001902}
1903
1904static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
1905 char **error)
1906{
1907 static struct dm_arg _args[] = {
1908 {0, 1, "Invalid number of cache feature arguments"},
1909 };
1910
1911 int r;
1912 unsigned argc;
1913 const char *arg;
1914 struct cache_features *cf = &ca->features;
1915
1916 init_features(cf);
1917
1918 r = dm_read_arg_group(_args, as, &argc, error);
1919 if (r)
1920 return -EINVAL;
1921
1922 while (argc--) {
1923 arg = dm_shift_arg(as);
1924
1925 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04001926 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001927
1928 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04001929 cf->io_mode = CM_IO_WRITETHROUGH;
1930
1931 else if (!strcasecmp(arg, "passthrough"))
1932 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001933
1934 else {
1935 *error = "Unrecognised cache feature requested";
1936 return -EINVAL;
1937 }
1938 }
1939
1940 return 0;
1941}
1942
1943static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
1944 char **error)
1945{
1946 static struct dm_arg _args[] = {
1947 {0, 1024, "Invalid number of policy arguments"},
1948 };
1949
1950 int r;
1951
1952 if (!at_least_one_arg(as, error))
1953 return -EINVAL;
1954
1955 ca->policy_name = dm_shift_arg(as);
1956
1957 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
1958 if (r)
1959 return -EINVAL;
1960
1961 ca->policy_argv = (const char **)as->argv;
1962 dm_consume_args(as, ca->policy_argc);
1963
1964 return 0;
1965}
1966
1967static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
1968 char **error)
1969{
1970 int r;
1971 struct dm_arg_set as;
1972
1973 as.argc = argc;
1974 as.argv = argv;
1975
1976 r = parse_metadata_dev(ca, &as, error);
1977 if (r)
1978 return r;
1979
1980 r = parse_cache_dev(ca, &as, error);
1981 if (r)
1982 return r;
1983
1984 r = parse_origin_dev(ca, &as, error);
1985 if (r)
1986 return r;
1987
1988 r = parse_block_size(ca, &as, error);
1989 if (r)
1990 return r;
1991
1992 r = parse_features(ca, &as, error);
1993 if (r)
1994 return r;
1995
1996 r = parse_policy(ca, &as, error);
1997 if (r)
1998 return r;
1999
2000 return 0;
2001}
2002
2003/*----------------------------------------------------------------*/
2004
2005static struct kmem_cache *migration_cache;
2006
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002007#define NOT_CORE_OPTION 1
2008
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002009static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002010{
2011 unsigned long tmp;
2012
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002013 if (!strcasecmp(key, "migration_threshold")) {
2014 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002015 return -EINVAL;
2016
2017 cache->migration_threshold = tmp;
2018 return 0;
2019 }
2020
2021 return NOT_CORE_OPTION;
2022}
2023
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002024static int set_config_value(struct cache *cache, const char *key, const char *value)
2025{
2026 int r = process_config_option(cache, key, value);
2027
2028 if (r == NOT_CORE_OPTION)
2029 r = policy_set_config_value(cache->policy, key, value);
2030
2031 if (r)
2032 DMWARN("bad config value for %s: %s", key, value);
2033
2034 return r;
2035}
2036
2037static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002038{
2039 int r = 0;
2040
2041 if (argc & 1) {
2042 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2043 return -EINVAL;
2044 }
2045
2046 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002047 r = set_config_value(cache, argv[0], argv[1]);
2048 if (r)
2049 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002050
2051 argc -= 2;
2052 argv += 2;
2053 }
2054
2055 return r;
2056}
2057
2058static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2059 char **error)
2060{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002061 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2062 cache->cache_size,
2063 cache->origin_sectors,
2064 cache->sectors_per_block);
2065 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002066 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002067 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002068 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002069 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002070
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002071 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002072}
2073
2074/*
2075 * We want the discard block size to be a power of two, at least the size
2076 * of the cache block size, and have no more than 2^14 discard blocks
2077 * across the origin.
2078 */
2079#define MAX_DISCARD_BLOCKS (1 << 14)
2080
2081static bool too_many_discard_blocks(sector_t discard_block_size,
2082 sector_t origin_size)
2083{
2084 (void) sector_div(origin_size, discard_block_size);
2085
2086 return origin_size > MAX_DISCARD_BLOCKS;
2087}
2088
2089static sector_t calculate_discard_block_size(sector_t cache_block_size,
2090 sector_t origin_size)
2091{
2092 sector_t discard_block_size;
2093
2094 discard_block_size = roundup_pow_of_two(cache_block_size);
2095
2096 if (origin_size)
2097 while (too_many_discard_blocks(discard_block_size, origin_size))
2098 discard_block_size *= 2;
2099
2100 return discard_block_size;
2101}
2102
Joe Thornberf8350da2013-05-10 14:37:16 +01002103#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002104
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002105static int cache_create(struct cache_args *ca, struct cache **result)
2106{
2107 int r = 0;
2108 char **error = &ca->ti->error;
2109 struct cache *cache;
2110 struct dm_target *ti = ca->ti;
2111 dm_block_t origin_blocks;
2112 struct dm_cache_metadata *cmd;
2113 bool may_format = ca->features.mode == CM_WRITE;
2114
2115 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2116 if (!cache)
2117 return -ENOMEM;
2118
2119 cache->ti = ca->ti;
2120 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002121 ti->num_flush_bios = 2;
2122 ti->flush_supported = true;
2123
2124 ti->num_discard_bios = 1;
2125 ti->discards_supported = true;
2126 ti->discard_zeroes_data_unsupported = true;
2127
Joe Thornber8c5008f2013-05-10 14:37:18 +01002128 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01002129 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002130
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002131 cache->callbacks.congested_fn = cache_is_congested;
2132 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2133
2134 cache->metadata_dev = ca->metadata_dev;
2135 cache->origin_dev = ca->origin_dev;
2136 cache->cache_dev = ca->cache_dev;
2137
2138 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2139
2140 /* FIXME: factor out this whole section */
2141 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002142 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002143 cache->origin_blocks = to_oblock(origin_blocks);
2144
2145 cache->sectors_per_block = ca->block_size;
2146 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2147 r = -EINVAL;
2148 goto bad;
2149 }
2150
2151 if (ca->block_size & (ca->block_size - 1)) {
2152 dm_block_t cache_size = ca->cache_sectors;
2153
2154 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002155 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002156 cache->cache_size = to_cblock(cache_size);
2157 } else {
2158 cache->sectors_per_block_shift = __ffs(ca->block_size);
2159 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2160 }
2161
2162 r = create_cache_policy(cache, ca, error);
2163 if (r)
2164 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002165
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002166 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002167 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2168
2169 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2170 if (r) {
2171 *error = "Error setting cache policy's config values";
2172 goto bad;
2173 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002174
2175 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2176 ca->block_size, may_format,
2177 dm_cache_policy_get_hint_size(cache->policy));
2178 if (IS_ERR(cmd)) {
2179 *error = "Error creating metadata object";
2180 r = PTR_ERR(cmd);
2181 goto bad;
2182 }
2183 cache->cmd = cmd;
2184
Joe Thornber2ee57d52013-10-24 14:10:29 -04002185 if (passthrough_mode(&cache->features)) {
2186 bool all_clean;
2187
2188 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2189 if (r) {
2190 *error = "dm_cache_metadata_all_clean() failed";
2191 goto bad;
2192 }
2193
2194 if (!all_clean) {
2195 *error = "Cannot enter passthrough mode unless all blocks are clean";
2196 r = -EINVAL;
2197 goto bad;
2198 }
2199 }
2200
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002201 spin_lock_init(&cache->lock);
2202 bio_list_init(&cache->deferred_bios);
2203 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002204 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002205 INIT_LIST_HEAD(&cache->quiesced_migrations);
2206 INIT_LIST_HEAD(&cache->completed_migrations);
2207 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002208 atomic_set(&cache->nr_migrations, 0);
2209 init_waitqueue_head(&cache->migration_wait);
2210
Joe Thornber66cb1912013-10-30 17:11:58 +00002211 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002212 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002213 atomic_set(&cache->quiescing_ack, 0);
2214
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002215 r = -ENOMEM;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002216 cache->nr_dirty = 0;
2217 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2218 if (!cache->dirty_bitset) {
2219 *error = "could not allocate dirty bitset";
2220 goto bad;
2221 }
2222 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2223
2224 cache->discard_block_size =
2225 calculate_discard_block_size(cache->sectors_per_block,
2226 cache->origin_sectors);
2227 cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks);
2228 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
2229 if (!cache->discard_bitset) {
2230 *error = "could not allocate discard bitset";
2231 goto bad;
2232 }
2233 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2234
2235 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2236 if (IS_ERR(cache->copier)) {
2237 *error = "could not create kcopyd client";
2238 r = PTR_ERR(cache->copier);
2239 goto bad;
2240 }
2241
2242 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2243 if (!cache->wq) {
2244 *error = "could not create workqueue for metadata object";
2245 goto bad;
2246 }
2247 INIT_WORK(&cache->worker, do_worker);
2248 INIT_DELAYED_WORK(&cache->waker, do_waker);
2249 cache->last_commit_jiffies = jiffies;
2250
2251 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2252 if (!cache->prison) {
2253 *error = "could not create bio prison";
2254 goto bad;
2255 }
2256
2257 cache->all_io_ds = dm_deferred_set_create();
2258 if (!cache->all_io_ds) {
2259 *error = "could not create all_io deferred set";
2260 goto bad;
2261 }
2262
2263 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2264 migration_cache);
2265 if (!cache->migration_pool) {
2266 *error = "Error creating cache's migration mempool";
2267 goto bad;
2268 }
2269
2270 cache->next_migration = NULL;
2271
2272 cache->need_tick_bio = true;
2273 cache->sized = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002274 cache->commit_requested = false;
2275 cache->loaded_mappings = false;
2276 cache->loaded_discards = false;
2277
2278 load_stats(cache);
2279
2280 atomic_set(&cache->stats.demotion, 0);
2281 atomic_set(&cache->stats.promotion, 0);
2282 atomic_set(&cache->stats.copies_avoided, 0);
2283 atomic_set(&cache->stats.cache_cell_clash, 0);
2284 atomic_set(&cache->stats.commit_count, 0);
2285 atomic_set(&cache->stats.discard_count, 0);
2286
2287 *result = cache;
2288 return 0;
2289
2290bad:
2291 destroy(cache);
2292 return r;
2293}
2294
2295static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2296{
2297 unsigned i;
2298 const char **copy;
2299
2300 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2301 if (!copy)
2302 return -ENOMEM;
2303 for (i = 0; i < argc; i++) {
2304 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2305 if (!copy[i]) {
2306 while (i--)
2307 kfree(copy[i]);
2308 kfree(copy);
2309 return -ENOMEM;
2310 }
2311 }
2312
2313 cache->nr_ctr_args = argc;
2314 cache->ctr_args = copy;
2315
2316 return 0;
2317}
2318
2319static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2320{
2321 int r = -EINVAL;
2322 struct cache_args *ca;
2323 struct cache *cache = NULL;
2324
2325 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2326 if (!ca) {
2327 ti->error = "Error allocating memory for cache";
2328 return -ENOMEM;
2329 }
2330 ca->ti = ti;
2331
2332 r = parse_cache_args(ca, argc, argv, &ti->error);
2333 if (r)
2334 goto out;
2335
2336 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002337 if (r)
2338 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002339
2340 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2341 if (r) {
2342 destroy(cache);
2343 goto out;
2344 }
2345
2346 ti->private = cache;
2347
2348out:
2349 destroy_cache_args(ca);
2350 return r;
2351}
2352
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002353static int cache_map(struct dm_target *ti, struct bio *bio)
2354{
2355 struct cache *cache = ti->private;
2356
2357 int r;
2358 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002359 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002360 bool can_migrate = false;
2361 bool discarded_block;
2362 struct dm_bio_prison_cell *cell;
2363 struct policy_result lookup_result;
2364 struct per_bio_data *pb;
2365
2366 if (from_oblock(block) > from_oblock(cache->origin_blocks)) {
2367 /*
2368 * This can only occur if the io goes to a partial block at
2369 * the end of the origin device. We don't cache these.
2370 * Just remap to the origin and carry on.
2371 */
2372 remap_to_origin_clear_discard(cache, bio, block);
2373 return DM_MAPIO_REMAPPED;
2374 }
2375
Mike Snitzer19b00922013-04-05 15:36:34 +01002376 pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002377
2378 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2379 defer_bio(cache, bio);
2380 return DM_MAPIO_SUBMITTED;
2381 }
2382
2383 /*
2384 * Check to see if that block is currently migrating.
2385 */
2386 cell = alloc_prison_cell(cache);
2387 if (!cell) {
2388 defer_bio(cache, bio);
2389 return DM_MAPIO_SUBMITTED;
2390 }
2391
2392 r = bio_detain(cache, block, bio, cell,
2393 (cell_free_fn) free_prison_cell,
2394 cache, &cell);
2395 if (r) {
2396 if (r < 0)
2397 defer_bio(cache, bio);
2398
2399 return DM_MAPIO_SUBMITTED;
2400 }
2401
2402 discarded_block = is_discarded_oblock(cache, block);
2403
2404 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2405 bio, &lookup_result);
2406 if (r == -EWOULDBLOCK) {
2407 cell_defer(cache, cell, true);
2408 return DM_MAPIO_SUBMITTED;
2409
2410 } else if (r) {
2411 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
2412 bio_io_error(bio);
2413 return DM_MAPIO_SUBMITTED;
2414 }
2415
Joe Thornber2ee57d52013-10-24 14:10:29 -04002416 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002417 switch (lookup_result.op) {
2418 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04002419 if (passthrough_mode(&cache->features)) {
2420 if (bio_data_dir(bio) == WRITE) {
2421 /*
2422 * We need to invalidate this block, so
2423 * defer for the worker thread.
2424 */
2425 cell_defer(cache, cell, true);
2426 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002427
Joe Thornber2ee57d52013-10-24 14:10:29 -04002428 } else {
2429 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2430 inc_miss_counter(cache, bio);
2431 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002432
Joe Thornber2ee57d52013-10-24 14:10:29 -04002433 cell_defer(cache, cell, false);
2434 }
2435
2436 } else {
2437 inc_hit_counter(cache, bio);
2438
2439 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2440 !is_dirty(cache, lookup_result.cblock))
2441 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2442 else
2443 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
2444
2445 cell_defer(cache, cell, false);
2446 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002447 break;
2448
2449 case POLICY_MISS:
2450 inc_miss_counter(cache, bio);
2451 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
2452
2453 if (pb->req_nr != 0) {
2454 /*
2455 * This is a duplicate writethrough io that is no
2456 * longer needed because the block has been demoted.
2457 */
2458 bio_endio(bio, 0);
2459 cell_defer(cache, cell, false);
2460 return DM_MAPIO_SUBMITTED;
2461 } else {
2462 remap_to_origin_clear_discard(cache, bio, block);
2463 cell_defer(cache, cell, false);
2464 }
2465 break;
2466
2467 default:
2468 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2469 (unsigned) lookup_result.op);
2470 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002471 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002472 }
2473
Joe Thornber2ee57d52013-10-24 14:10:29 -04002474 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002475}
2476
2477static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2478{
2479 struct cache *cache = ti->private;
2480 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002481 size_t pb_data_size = get_per_bio_data_size(cache);
2482 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002483
2484 if (pb->tick) {
2485 policy_tick(cache->policy);
2486
2487 spin_lock_irqsave(&cache->lock, flags);
2488 cache->need_tick_bio = true;
2489 spin_unlock_irqrestore(&cache->lock, flags);
2490 }
2491
2492 check_for_quiesced_migrations(cache, pb);
2493
2494 return 0;
2495}
2496
2497static int write_dirty_bitset(struct cache *cache)
2498{
2499 unsigned i, r;
2500
2501 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2502 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2503 is_dirty(cache, to_cblock(i)));
2504 if (r)
2505 return r;
2506 }
2507
2508 return 0;
2509}
2510
2511static int write_discard_bitset(struct cache *cache)
2512{
2513 unsigned i, r;
2514
2515 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2516 cache->discard_nr_blocks);
2517 if (r) {
2518 DMERR("could not resize on-disk discard bitset");
2519 return r;
2520 }
2521
2522 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2523 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2524 is_discarded(cache, to_dblock(i)));
2525 if (r)
2526 return r;
2527 }
2528
2529 return 0;
2530}
2531
2532static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock,
2533 uint32_t hint)
2534{
2535 struct cache *cache = context;
2536 return dm_cache_save_hint(cache->cmd, cblock, hint);
2537}
2538
2539static int write_hints(struct cache *cache)
2540{
2541 int r;
2542
2543 r = dm_cache_begin_hints(cache->cmd, cache->policy);
2544 if (r) {
2545 DMERR("dm_cache_begin_hints failed");
2546 return r;
2547 }
2548
2549 r = policy_walk_mappings(cache->policy, save_hint, cache);
2550 if (r)
2551 DMERR("policy_walk_mappings failed");
2552
2553 return r;
2554}
2555
2556/*
2557 * returns true on success
2558 */
2559static bool sync_metadata(struct cache *cache)
2560{
2561 int r1, r2, r3, r4;
2562
2563 r1 = write_dirty_bitset(cache);
2564 if (r1)
2565 DMERR("could not write dirty bitset");
2566
2567 r2 = write_discard_bitset(cache);
2568 if (r2)
2569 DMERR("could not write discard bitset");
2570
2571 save_stats(cache);
2572
2573 r3 = write_hints(cache);
2574 if (r3)
2575 DMERR("could not write hints");
2576
2577 /*
2578 * If writing the above metadata failed, we still commit, but don't
2579 * set the clean shutdown flag. This will effectively force every
2580 * dirty bit to be set on reload.
2581 */
2582 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2583 if (r4)
2584 DMERR("could not write cache metadata. Data loss may occur.");
2585
2586 return !r1 && !r2 && !r3 && !r4;
2587}
2588
2589static void cache_postsuspend(struct dm_target *ti)
2590{
2591 struct cache *cache = ti->private;
2592
2593 start_quiescing(cache);
2594 wait_for_migrations(cache);
2595 stop_worker(cache);
2596 requeue_deferred_io(cache);
2597 stop_quiescing(cache);
2598
2599 (void) sync_metadata(cache);
2600}
2601
2602static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2603 bool dirty, uint32_t hint, bool hint_valid)
2604{
2605 int r;
2606 struct cache *cache = context;
2607
2608 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2609 if (r)
2610 return r;
2611
2612 if (dirty)
2613 set_dirty(cache, oblock, cblock);
2614 else
2615 clear_dirty(cache, oblock, cblock);
2616
2617 return 0;
2618}
2619
2620static int load_discard(void *context, sector_t discard_block_size,
2621 dm_dblock_t dblock, bool discard)
2622{
2623 struct cache *cache = context;
2624
2625 /* FIXME: handle mis-matched block size */
2626
2627 if (discard)
2628 set_discard(cache, dblock);
2629 else
2630 clear_discard(cache, dblock);
2631
2632 return 0;
2633}
2634
Joe Thornberf494a9c2013-10-31 13:55:49 -04002635static dm_cblock_t get_cache_dev_size(struct cache *cache)
2636{
2637 sector_t size = get_dev_size(cache->cache_dev);
2638 (void) sector_div(size, cache->sectors_per_block);
2639 return to_cblock(size);
2640}
2641
2642static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2643{
2644 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2645 return true;
2646
2647 /*
2648 * We can't drop a dirty block when shrinking the cache.
2649 */
2650 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2651 new_size = to_cblock(from_cblock(new_size) + 1);
2652 if (is_dirty(cache, new_size)) {
2653 DMERR("unable to shrink cache; cache block %llu is dirty",
2654 (unsigned long long) from_cblock(new_size));
2655 return false;
2656 }
2657 }
2658
2659 return true;
2660}
2661
2662static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2663{
2664 int r;
2665
2666 r = dm_cache_resize(cache->cmd, cache->cache_size);
2667 if (r) {
2668 DMERR("could not resize cache metadata");
2669 return r;
2670 }
2671
2672 cache->cache_size = new_size;
2673
2674 return 0;
2675}
2676
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002677static int cache_preresume(struct dm_target *ti)
2678{
2679 int r = 0;
2680 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002681 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002682
2683 /*
2684 * Check to see if the cache has resized.
2685 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04002686 if (!cache->sized) {
2687 r = resize_cache_dev(cache, csize);
2688 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002689 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002690
2691 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002692
2693 } else if (csize != cache->cache_size) {
2694 if (!can_resize(cache, csize))
2695 return -EINVAL;
2696
2697 r = resize_cache_dev(cache, csize);
2698 if (r)
2699 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002700 }
2701
2702 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002703 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002704 load_mapping, cache);
2705 if (r) {
2706 DMERR("could not load cache mappings");
2707 return r;
2708 }
2709
2710 cache->loaded_mappings = true;
2711 }
2712
2713 if (!cache->loaded_discards) {
2714 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2715 if (r) {
2716 DMERR("could not load origin discards");
2717 return r;
2718 }
2719
2720 cache->loaded_discards = true;
2721 }
2722
2723 return r;
2724}
2725
2726static void cache_resume(struct dm_target *ti)
2727{
2728 struct cache *cache = ti->private;
2729
2730 cache->need_tick_bio = true;
2731 do_waker(&cache->waker.work);
2732}
2733
2734/*
2735 * Status format:
2736 *
2737 * <#used metadata blocks>/<#total metadata blocks>
2738 * <#read hits> <#read misses> <#write hits> <#write misses>
2739 * <#demotions> <#promotions> <#blocks in cache> <#dirty>
2740 * <#features> <features>*
2741 * <#core args> <core args>
2742 * <#policy args> <policy args>*
2743 */
2744static void cache_status(struct dm_target *ti, status_type_t type,
2745 unsigned status_flags, char *result, unsigned maxlen)
2746{
2747 int r = 0;
2748 unsigned i;
2749 ssize_t sz = 0;
2750 dm_block_t nr_free_blocks_metadata = 0;
2751 dm_block_t nr_blocks_metadata = 0;
2752 char buf[BDEVNAME_SIZE];
2753 struct cache *cache = ti->private;
2754 dm_cblock_t residency;
2755
2756 switch (type) {
2757 case STATUSTYPE_INFO:
2758 /* Commit to ensure statistics aren't out-of-date */
2759 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2760 r = dm_cache_commit(cache->cmd, false);
2761 if (r)
2762 DMERR("could not commit metadata for accurate status");
2763 }
2764
2765 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2766 &nr_free_blocks_metadata);
2767 if (r) {
2768 DMERR("could not get metadata free block count");
2769 goto err;
2770 }
2771
2772 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2773 if (r) {
2774 DMERR("could not get metadata device size");
2775 goto err;
2776 }
2777
2778 residency = policy_residency(cache->policy);
2779
2780 DMEMIT("%llu/%llu %u %u %u %u %u %u %llu %u ",
2781 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2782 (unsigned long long)nr_blocks_metadata,
2783 (unsigned) atomic_read(&cache->stats.read_hit),
2784 (unsigned) atomic_read(&cache->stats.read_miss),
2785 (unsigned) atomic_read(&cache->stats.write_hit),
2786 (unsigned) atomic_read(&cache->stats.write_miss),
2787 (unsigned) atomic_read(&cache->stats.demotion),
2788 (unsigned) atomic_read(&cache->stats.promotion),
2789 (unsigned long long) from_cblock(residency),
2790 cache->nr_dirty);
2791
Joe Thornber2ee57d52013-10-24 14:10:29 -04002792 if (writethrough_mode(&cache->features))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002793 DMEMIT("1 writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04002794
2795 else if (passthrough_mode(&cache->features))
2796 DMEMIT("1 passthrough ");
2797
2798 else if (writeback_mode(&cache->features))
2799 DMEMIT("1 writeback ");
2800
2801 else {
2802 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2803 goto err;
2804 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002805
2806 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
2807 if (sz < maxlen) {
2808 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2809 if (r)
2810 DMERR("policy_emit_config_values returned %d", r);
2811 }
2812
2813 break;
2814
2815 case STATUSTYPE_TABLE:
2816 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2817 DMEMIT("%s ", buf);
2818 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2819 DMEMIT("%s ", buf);
2820 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2821 DMEMIT("%s", buf);
2822
2823 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2824 DMEMIT(" %s", cache->ctr_args[i]);
2825 if (cache->nr_ctr_args)
2826 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2827 }
2828
2829 return;
2830
2831err:
2832 DMEMIT("Error");
2833}
2834
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002835/*
2836 * Supports <key> <value>.
2837 *
2838 * The key migration_threshold is supported by the cache target core.
2839 */
2840static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
2841{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002842 struct cache *cache = ti->private;
2843
2844 if (argc != 2)
2845 return -EINVAL;
2846
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002847 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002848}
2849
2850static int cache_iterate_devices(struct dm_target *ti,
2851 iterate_devices_callout_fn fn, void *data)
2852{
2853 int r = 0;
2854 struct cache *cache = ti->private;
2855
2856 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
2857 if (!r)
2858 r = fn(ti, cache->origin_dev, 0, ti->len, data);
2859
2860 return r;
2861}
2862
2863/*
2864 * We assume I/O is going to the origin (which is the volume
2865 * more likely to have restrictions e.g. by being striped).
2866 * (Looking up the exact location of the data would be expensive
2867 * and could always be out of date by the time the bio is submitted.)
2868 */
2869static int cache_bvec_merge(struct dm_target *ti,
2870 struct bvec_merge_data *bvm,
2871 struct bio_vec *biovec, int max_size)
2872{
2873 struct cache *cache = ti->private;
2874 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
2875
2876 if (!q->merge_bvec_fn)
2877 return max_size;
2878
2879 bvm->bi_bdev = cache->origin_dev->bdev;
2880 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2881}
2882
2883static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
2884{
2885 /*
2886 * FIXME: these limits may be incompatible with the cache device
2887 */
2888 limits->max_discard_sectors = cache->discard_block_size * 1024;
2889 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
2890}
2891
2892static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
2893{
2894 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04002895 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002896
Mike Snitzerf6109372013-08-20 15:02:41 -04002897 /*
2898 * If the system-determined stacked limits are compatible with the
2899 * cache's blocksize (io_opt is a factor) do not override them.
2900 */
2901 if (io_opt_sectors < cache->sectors_per_block ||
2902 do_div(io_opt_sectors, cache->sectors_per_block)) {
2903 blk_limits_io_min(limits, 0);
2904 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
2905 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002906 set_discard_limits(cache, limits);
2907}
2908
2909/*----------------------------------------------------------------*/
2910
2911static struct target_type cache_target = {
2912 .name = "cache",
Joe Thornber2ee57d52013-10-24 14:10:29 -04002913 .version = {1, 2, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002914 .module = THIS_MODULE,
2915 .ctr = cache_ctr,
2916 .dtr = cache_dtr,
2917 .map = cache_map,
2918 .end_io = cache_end_io,
2919 .postsuspend = cache_postsuspend,
2920 .preresume = cache_preresume,
2921 .resume = cache_resume,
2922 .status = cache_status,
2923 .message = cache_message,
2924 .iterate_devices = cache_iterate_devices,
2925 .merge = cache_bvec_merge,
2926 .io_hints = cache_io_hints,
2927};
2928
2929static int __init dm_cache_init(void)
2930{
2931 int r;
2932
2933 r = dm_register_target(&cache_target);
2934 if (r) {
2935 DMERR("cache target registration failed: %d", r);
2936 return r;
2937 }
2938
2939 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
2940 if (!migration_cache) {
2941 dm_unregister_target(&cache_target);
2942 return -ENOMEM;
2943 }
2944
2945 return 0;
2946}
2947
2948static void __exit dm_cache_exit(void)
2949{
2950 dm_unregister_target(&cache_target);
2951 kmem_cache_destroy(migration_cache);
2952}
2953
2954module_init(dm_cache_init);
2955module_exit(dm_cache_exit);
2956
2957MODULE_DESCRIPTION(DM_NAME " cache target");
2958MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
2959MODULE_LICENSE("GPL");