blob: ced7fd4adddb53d1de2a0c740708f684ed44b42b [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;
Mike Snitzer8d307262013-12-03 19:16:04 -070088
89 /*
90 * Must bump bi_remaining to allow bio to complete with
91 * restored bi_end_io.
92 */
93 atomic_inc(&bio->bi_remaining);
Joe Thornberc9d28d52013-10-31 13:55:48 -040094}
95
96/*----------------------------------------------------------------*/
97
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000098#define MIGRATION_POOL_SIZE 128
99#define COMMIT_PERIOD HZ
100#define MIGRATION_COUNT_WINDOW 10
101
102/*
Mike Snitzer05473042013-08-16 10:54:19 -0400103 * The block size of the device holding cache data must be
104 * between 32KB and 1GB.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000105 */
106#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
Mike Snitzer05473042013-08-16 10:54:19 -0400107#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000108
109/*
110 * FIXME: the cache is read/write for the time being.
111 */
Joe Thornber2ee57d52013-10-24 14:10:29 -0400112enum cache_metadata_mode {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000113 CM_WRITE, /* metadata may be changed */
114 CM_READ_ONLY, /* metadata may not be changed */
115};
116
Joe Thornber2ee57d52013-10-24 14:10:29 -0400117enum cache_io_mode {
118 /*
119 * Data is written to cached blocks only. These blocks are marked
120 * dirty. If you lose the cache device you will lose data.
121 * Potential performance increase for both reads and writes.
122 */
123 CM_IO_WRITEBACK,
124
125 /*
126 * Data is written to both cache and origin. Blocks are never
127 * dirty. Potential performance benfit for reads only.
128 */
129 CM_IO_WRITETHROUGH,
130
131 /*
132 * A degraded mode useful for various cache coherency situations
133 * (eg, rolling back snapshots). Reads and writes always go to the
134 * origin. If a write goes to a cached oblock, then the cache
135 * block is invalidated.
136 */
137 CM_IO_PASSTHROUGH
138};
139
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000140struct cache_features {
Joe Thornber2ee57d52013-10-24 14:10:29 -0400141 enum cache_metadata_mode mode;
142 enum cache_io_mode io_mode;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000143};
144
145struct cache_stats {
146 atomic_t read_hit;
147 atomic_t read_miss;
148 atomic_t write_hit;
149 atomic_t write_miss;
150 atomic_t demotion;
151 atomic_t promotion;
152 atomic_t copies_avoided;
153 atomic_t cache_cell_clash;
154 atomic_t commit_count;
155 atomic_t discard_count;
156};
157
Joe Thornber65790ff2013-11-08 16:39:50 +0000158/*
159 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
160 * the one-past-the-end value.
161 */
162struct cblock_range {
163 dm_cblock_t begin;
164 dm_cblock_t end;
165};
166
167struct invalidation_request {
168 struct list_head list;
169 struct cblock_range *cblocks;
170
171 atomic_t complete;
172 int err;
173
174 wait_queue_head_t result_wait;
175};
176
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000177struct cache {
178 struct dm_target *ti;
179 struct dm_target_callbacks callbacks;
180
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400181 struct dm_cache_metadata *cmd;
182
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000183 /*
184 * Metadata is written to this device.
185 */
186 struct dm_dev *metadata_dev;
187
188 /*
189 * The slower of the two data devices. Typically a spindle.
190 */
191 struct dm_dev *origin_dev;
192
193 /*
194 * The faster of the two data devices. Typically an SSD.
195 */
196 struct dm_dev *cache_dev;
197
198 /*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000199 * Size of the origin device in _complete_ blocks and native sectors.
200 */
201 dm_oblock_t origin_blocks;
202 sector_t origin_sectors;
203
204 /*
205 * Size of the cache device in blocks.
206 */
207 dm_cblock_t cache_size;
208
209 /*
210 * Fields for converting from sectors to blocks.
211 */
212 uint32_t sectors_per_block;
213 int sectors_per_block_shift;
214
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000215 spinlock_t lock;
216 struct bio_list deferred_bios;
217 struct bio_list deferred_flush_bios;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000218 struct bio_list deferred_writethrough_bios;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000219 struct list_head quiesced_migrations;
220 struct list_head completed_migrations;
221 struct list_head need_commit_migrations;
222 sector_t migration_threshold;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000223 wait_queue_head_t migration_wait;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400224 atomic_t nr_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000225
Joe Thornber66cb1912013-10-30 17:11:58 +0000226 wait_queue_head_t quiescing_wait;
Joe Thornber238f8362013-10-30 17:29:30 +0000227 atomic_t quiescing;
Joe Thornber66cb1912013-10-30 17:11:58 +0000228 atomic_t quiescing_ack;
229
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000230 /*
231 * cache_size entries, dirty if set
232 */
Anssi Hannula44fa8162014-08-01 11:55:47 -0400233 atomic_t nr_dirty;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000234 unsigned long *dirty_bitset;
235
236 /*
237 * origin_blocks entries, discarded if set.
238 */
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000239 dm_dblock_t discard_nr_blocks;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000240 unsigned long *discard_bitset;
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000241 uint32_t discard_block_size;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400242
243 /*
244 * Rather than reconstructing the table line for the status we just
245 * save it and regurgitate.
246 */
247 unsigned nr_ctr_args;
248 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000249
250 struct dm_kcopyd_client *copier;
251 struct workqueue_struct *wq;
252 struct work_struct worker;
253
254 struct delayed_work waker;
255 unsigned long last_commit_jiffies;
256
257 struct dm_bio_prison *prison;
258 struct dm_deferred_set *all_io_ds;
259
260 mempool_t *migration_pool;
261 struct dm_cache_migration *next_migration;
262
263 struct dm_cache_policy *policy;
264 unsigned policy_nr_args;
265
266 bool need_tick_bio:1;
267 bool sized:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000268 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000269 bool commit_requested:1;
270 bool loaded_mappings:1;
271 bool loaded_discards:1;
272
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000273 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400274 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000275 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400276 struct cache_features features;
277
278 struct cache_stats stats;
Joe Thornber65790ff2013-11-08 16:39:50 +0000279
280 /*
281 * Invalidation fields.
282 */
283 spinlock_t invalidation_lock;
284 struct list_head invalidation_requests;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000285};
286
287struct per_bio_data {
288 bool tick:1;
289 unsigned req_nr:2;
290 struct dm_deferred_entry *all_io_entry;
Mike Snitzerc6eda5e2014-01-31 14:11:54 -0500291 struct dm_hook_info hook_info;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000292
Mike Snitzer19b00922013-04-05 15:36:34 +0100293 /*
294 * writethrough fields. These MUST remain at the end of this
295 * structure and the 'cache' member must be the first as it
Joe Thornberaeed1422013-05-10 14:37:18 +0100296 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100297 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000298 struct cache *cache;
299 dm_cblock_t cblock;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100300 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000301};
302
303struct dm_cache_migration {
304 struct list_head list;
305 struct cache *cache;
306
307 unsigned long start_jiffies;
308 dm_oblock_t old_oblock;
309 dm_oblock_t new_oblock;
310 dm_cblock_t cblock;
311
312 bool err:1;
313 bool writeback:1;
314 bool demote:1;
315 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400316 bool requeue_holder:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000317 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000318
319 struct dm_bio_prison_cell *old_ocell;
320 struct dm_bio_prison_cell *new_ocell;
321};
322
323/*
324 * Processing a bio in the worker thread may require these memory
325 * allocations. We prealloc to avoid deadlocks (the same worker thread
326 * frees them back to the mempool).
327 */
328struct prealloc {
329 struct dm_cache_migration *mg;
330 struct dm_bio_prison_cell *cell1;
331 struct dm_bio_prison_cell *cell2;
332};
333
334static void wake_worker(struct cache *cache)
335{
336 queue_work(cache->wq, &cache->worker);
337}
338
339/*----------------------------------------------------------------*/
340
341static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
342{
343 /* FIXME: change to use a local slab. */
344 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
345}
346
347static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
348{
349 dm_bio_prison_free_cell(cache->prison, cell);
350}
351
352static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
353{
354 if (!p->mg) {
355 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
356 if (!p->mg)
357 return -ENOMEM;
358 }
359
360 if (!p->cell1) {
361 p->cell1 = alloc_prison_cell(cache);
362 if (!p->cell1)
363 return -ENOMEM;
364 }
365
366 if (!p->cell2) {
367 p->cell2 = alloc_prison_cell(cache);
368 if (!p->cell2)
369 return -ENOMEM;
370 }
371
372 return 0;
373}
374
375static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
376{
377 if (p->cell2)
378 free_prison_cell(cache, p->cell2);
379
380 if (p->cell1)
381 free_prison_cell(cache, p->cell1);
382
383 if (p->mg)
384 mempool_free(p->mg, cache->migration_pool);
385}
386
387static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
388{
389 struct dm_cache_migration *mg = p->mg;
390
391 BUG_ON(!mg);
392 p->mg = NULL;
393
394 return mg;
395}
396
397/*
398 * You must have a cell within the prealloc struct to return. If not this
399 * function will BUG() rather than returning NULL.
400 */
401static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
402{
403 struct dm_bio_prison_cell *r = NULL;
404
405 if (p->cell1) {
406 r = p->cell1;
407 p->cell1 = NULL;
408
409 } else if (p->cell2) {
410 r = p->cell2;
411 p->cell2 = NULL;
412 } else
413 BUG();
414
415 return r;
416}
417
418/*
419 * You can't have more than two cells in a prealloc struct. BUG() will be
420 * called if you try and overfill.
421 */
422static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
423{
424 if (!p->cell2)
425 p->cell2 = cell;
426
427 else if (!p->cell1)
428 p->cell1 = cell;
429
430 else
431 BUG();
432}
433
434/*----------------------------------------------------------------*/
435
436static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
437{
438 key->virtual = 0;
439 key->dev = 0;
Joe Thornber5f274d82014-09-17 10:17:39 +0100440 key->block_begin = from_oblock(oblock);
441 key->block_end = key->block_begin + 1ULL;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000442}
443
444/*
445 * The caller hands in a preallocated cell, and a free function for it.
446 * The cell will be freed if there's an error, or if it wasn't used because
447 * a cell with that key already exists.
448 */
449typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
450
451static int bio_detain(struct cache *cache, dm_oblock_t oblock,
452 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
453 cell_free_fn free_fn, void *free_context,
454 struct dm_bio_prison_cell **cell_result)
455{
456 int r;
457 struct dm_cell_key key;
458
459 build_key(oblock, &key);
460 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
461 if (r)
462 free_fn(free_context, cell_prealloc);
463
464 return r;
465}
466
467static int get_cell(struct cache *cache,
468 dm_oblock_t oblock,
469 struct prealloc *structs,
470 struct dm_bio_prison_cell **cell_result)
471{
472 int r;
473 struct dm_cell_key key;
474 struct dm_bio_prison_cell *cell_prealloc;
475
476 cell_prealloc = prealloc_get_cell(structs);
477
478 build_key(oblock, &key);
479 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
480 if (r)
481 prealloc_put_cell(structs, cell_prealloc);
482
483 return r;
484}
485
Joe Thornberaeed1422013-05-10 14:37:18 +0100486/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000487
488static bool is_dirty(struct cache *cache, dm_cblock_t b)
489{
490 return test_bit(from_cblock(b), cache->dirty_bitset);
491}
492
493static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
494{
495 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
Anssi Hannula44fa8162014-08-01 11:55:47 -0400496 atomic_inc(&cache->nr_dirty);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000497 policy_set_dirty(cache->policy, oblock);
498 }
499}
500
501static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
502{
503 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
504 policy_clear_dirty(cache->policy, oblock);
Anssi Hannula44fa8162014-08-01 11:55:47 -0400505 if (atomic_dec_return(&cache->nr_dirty) == 0)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000506 dm_table_event(cache->ti->table);
507 }
508}
509
510/*----------------------------------------------------------------*/
Joe Thornberaeed1422013-05-10 14:37:18 +0100511
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000512static bool block_size_is_power_of_two(struct cache *cache)
513{
514 return cache->sectors_per_block_shift >= 0;
515}
516
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100517/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
518#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
519__always_inline
520#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000521static dm_block_t block_div(dm_block_t b, uint32_t n)
522{
523 do_div(b, n);
524
525 return b;
526}
527
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000528static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
529{
530 uint32_t discard_blocks = cache->discard_block_size;
531 dm_block_t b = from_oblock(oblock);
532
533 if (!block_size_is_power_of_two(cache))
534 discard_blocks = discard_blocks / cache->sectors_per_block;
535 else
536 discard_blocks >>= cache->sectors_per_block_shift;
537
538 b = block_div(b, discard_blocks);
539
540 return to_dblock(b);
541}
542
543static void set_discard(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000544{
545 unsigned long flags;
546
547 atomic_inc(&cache->stats.discard_count);
548
549 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000550 set_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000551 spin_unlock_irqrestore(&cache->lock, flags);
552}
553
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000554static void clear_discard(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000555{
556 unsigned long flags;
557
558 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000559 clear_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000560 spin_unlock_irqrestore(&cache->lock, flags);
561}
562
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000563static bool is_discarded(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000564{
565 int r;
566 unsigned long flags;
567
568 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000569 r = test_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000570 spin_unlock_irqrestore(&cache->lock, flags);
571
572 return r;
573}
574
575static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
576{
577 int r;
578 unsigned long flags;
579
580 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000581 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
582 cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000583 spin_unlock_irqrestore(&cache->lock, flags);
584
585 return r;
586}
587
588/*----------------------------------------------------------------*/
589
590static void load_stats(struct cache *cache)
591{
592 struct dm_cache_statistics stats;
593
594 dm_cache_metadata_get_stats(cache->cmd, &stats);
595 atomic_set(&cache->stats.read_hit, stats.read_hits);
596 atomic_set(&cache->stats.read_miss, stats.read_misses);
597 atomic_set(&cache->stats.write_hit, stats.write_hits);
598 atomic_set(&cache->stats.write_miss, stats.write_misses);
599}
600
601static void save_stats(struct cache *cache)
602{
603 struct dm_cache_statistics stats;
604
605 stats.read_hits = atomic_read(&cache->stats.read_hit);
606 stats.read_misses = atomic_read(&cache->stats.read_miss);
607 stats.write_hits = atomic_read(&cache->stats.write_hit);
608 stats.write_misses = atomic_read(&cache->stats.write_miss);
609
610 dm_cache_metadata_set_stats(cache->cmd, &stats);
611}
612
613/*----------------------------------------------------------------
614 * Per bio data
615 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100616
617/*
618 * If using writeback, leave out struct per_bio_data's writethrough fields.
619 */
620#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
621#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
622
Joe Thornber2ee57d52013-10-24 14:10:29 -0400623static bool writethrough_mode(struct cache_features *f)
624{
625 return f->io_mode == CM_IO_WRITETHROUGH;
626}
627
628static bool writeback_mode(struct cache_features *f)
629{
630 return f->io_mode == CM_IO_WRITEBACK;
631}
632
633static bool passthrough_mode(struct cache_features *f)
634{
635 return f->io_mode == CM_IO_PASSTHROUGH;
636}
637
Mike Snitzer19b00922013-04-05 15:36:34 +0100638static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000639{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400640 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100641}
642
643static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
644{
645 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000646 BUG_ON(!pb);
647 return pb;
648}
649
Mike Snitzer19b00922013-04-05 15:36:34 +0100650static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000651{
Mike Snitzer19b00922013-04-05 15:36:34 +0100652 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000653
654 pb->tick = false;
655 pb->req_nr = dm_bio_get_target_bio_nr(bio);
656 pb->all_io_entry = NULL;
657
658 return pb;
659}
660
661/*----------------------------------------------------------------
662 * Remapping
663 *--------------------------------------------------------------*/
664static void remap_to_origin(struct cache *cache, struct bio *bio)
665{
666 bio->bi_bdev = cache->origin_dev->bdev;
667}
668
669static void remap_to_cache(struct cache *cache, struct bio *bio,
670 dm_cblock_t cblock)
671{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700672 sector_t bi_sector = bio->bi_iter.bi_sector;
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100673 sector_t block = from_cblock(cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000674
675 bio->bi_bdev = cache->cache_dev->bdev;
676 if (!block_size_is_power_of_two(cache))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700677 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100678 (block * cache->sectors_per_block) +
Kent Overstreet4f024f32013-10-11 15:44:27 -0700679 sector_div(bi_sector, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000680 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700681 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100682 (block << cache->sectors_per_block_shift) |
Kent Overstreet4f024f32013-10-11 15:44:27 -0700683 (bi_sector & (cache->sectors_per_block - 1));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000684}
685
686static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
687{
688 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100689 size_t pb_data_size = get_per_bio_data_size(cache);
690 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000691
692 spin_lock_irqsave(&cache->lock, flags);
693 if (cache->need_tick_bio &&
694 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
695 pb->tick = true;
696 cache->need_tick_bio = false;
697 }
698 spin_unlock_irqrestore(&cache->lock, flags);
699}
700
701static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
702 dm_oblock_t oblock)
703{
704 check_if_tick_bio_needed(cache, bio);
705 remap_to_origin(cache, bio);
706 if (bio_data_dir(bio) == WRITE)
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000707 clear_discard(cache, oblock_to_dblock(cache, oblock));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000708}
709
710static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
711 dm_oblock_t oblock, dm_cblock_t cblock)
712{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100713 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000714 remap_to_cache(cache, bio, cblock);
715 if (bio_data_dir(bio) == WRITE) {
716 set_dirty(cache, oblock, cblock);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000717 clear_discard(cache, oblock_to_dblock(cache, oblock));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000718 }
719}
720
721static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
722{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700723 sector_t block_nr = bio->bi_iter.bi_sector;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000724
725 if (!block_size_is_power_of_two(cache))
726 (void) sector_div(block_nr, cache->sectors_per_block);
727 else
728 block_nr >>= cache->sectors_per_block_shift;
729
730 return to_oblock(block_nr);
731}
732
733static int bio_triggers_commit(struct cache *cache, struct bio *bio)
734{
735 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
736}
737
Joe Thornber8c081b52014-05-13 16:18:38 +0100738/*
739 * You must increment the deferred set whilst the prison cell is held. To
740 * encourage this, we ask for 'cell' to be passed in.
741 */
742static void inc_ds(struct cache *cache, struct bio *bio,
743 struct dm_bio_prison_cell *cell)
744{
745 size_t pb_data_size = get_per_bio_data_size(cache);
746 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
747
748 BUG_ON(!cell);
749 BUG_ON(pb->all_io_entry);
750
751 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
752}
753
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000754static void issue(struct cache *cache, struct bio *bio)
755{
756 unsigned long flags;
757
758 if (!bio_triggers_commit(cache, bio)) {
759 generic_make_request(bio);
760 return;
761 }
762
763 /*
764 * Batch together any bios that trigger commits and then issue a
765 * single commit for them in do_worker().
766 */
767 spin_lock_irqsave(&cache->lock, flags);
768 cache->commit_requested = true;
769 bio_list_add(&cache->deferred_flush_bios, bio);
770 spin_unlock_irqrestore(&cache->lock, flags);
771}
772
Joe Thornber8c081b52014-05-13 16:18:38 +0100773static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
774{
775 inc_ds(cache, bio, cell);
776 issue(cache, bio);
777}
778
Joe Thornbere2e74d62013-03-20 17:21:27 +0000779static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
780{
781 unsigned long flags;
782
783 spin_lock_irqsave(&cache->lock, flags);
784 bio_list_add(&cache->deferred_writethrough_bios, bio);
785 spin_unlock_irqrestore(&cache->lock, flags);
786
787 wake_worker(cache);
788}
789
790static void writethrough_endio(struct bio *bio, int err)
791{
Mike Snitzer19b00922013-04-05 15:36:34 +0100792 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400793
794 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000795
796 if (err) {
797 bio_endio(bio, err);
798 return;
799 }
800
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100801 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000802 remap_to_cache(pb->cache, bio, pb->cblock);
803
804 /*
805 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed1422013-05-10 14:37:18 +0100806 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000807 * worker thread.
808 */
809 defer_writethrough_bio(pb->cache, bio);
810}
811
812/*
813 * When running in writethrough mode we need to send writes to clean blocks
814 * to both the cache and origin devices. In future we'd like to clone the
815 * bio and send them in parallel, but for now we're doing them in
816 * series as this is easier.
817 */
818static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
819 dm_oblock_t oblock, dm_cblock_t cblock)
820{
Mike Snitzer19b00922013-04-05 15:36:34 +0100821 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000822
823 pb->cache = cache;
824 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400825 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100826 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000827
828 remap_to_origin_clear_discard(pb->cache, bio, oblock);
829}
830
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000831/*----------------------------------------------------------------
832 * Migration processing
833 *
834 * Migration covers moving data from the origin device to the cache, or
835 * vice versa.
836 *--------------------------------------------------------------*/
837static void free_migration(struct dm_cache_migration *mg)
838{
839 mempool_free(mg, mg->cache->migration_pool);
840}
841
842static void inc_nr_migrations(struct cache *cache)
843{
844 atomic_inc(&cache->nr_migrations);
845}
846
847static void dec_nr_migrations(struct cache *cache)
848{
849 atomic_dec(&cache->nr_migrations);
850
851 /*
852 * Wake the worker in case we're suspending the target.
853 */
854 wake_up(&cache->migration_wait);
855}
856
857static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
858 bool holder)
859{
860 (holder ? dm_cell_release : dm_cell_release_no_holder)
861 (cache->prison, cell, &cache->deferred_bios);
862 free_prison_cell(cache, cell);
863}
864
865static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
866 bool holder)
867{
868 unsigned long flags;
869
870 spin_lock_irqsave(&cache->lock, flags);
871 __cell_defer(cache, cell, holder);
872 spin_unlock_irqrestore(&cache->lock, flags);
873
874 wake_worker(cache);
875}
876
877static void cleanup_migration(struct dm_cache_migration *mg)
878{
Joe Thornber66cb1912013-10-30 17:11:58 +0000879 struct cache *cache = mg->cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000880 free_migration(mg);
Joe Thornber66cb1912013-10-30 17:11:58 +0000881 dec_nr_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000882}
883
884static void migration_failure(struct dm_cache_migration *mg)
885{
886 struct cache *cache = mg->cache;
887
888 if (mg->writeback) {
889 DMWARN_LIMIT("writeback failed; couldn't copy block");
890 set_dirty(cache, mg->old_oblock, mg->cblock);
891 cell_defer(cache, mg->old_ocell, false);
892
893 } else if (mg->demote) {
894 DMWARN_LIMIT("demotion failed; couldn't copy block");
895 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
896
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200897 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000898 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200899 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000900 } else {
901 DMWARN_LIMIT("promotion failed; couldn't copy block");
902 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200903 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000904 }
905
906 cleanup_migration(mg);
907}
908
909static void migration_success_pre_commit(struct dm_cache_migration *mg)
910{
911 unsigned long flags;
912 struct cache *cache = mg->cache;
913
914 if (mg->writeback) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000915 clear_dirty(cache, mg->old_oblock, mg->cblock);
Anssi Hannula40aa9782014-09-05 03:11:28 +0300916 cell_defer(cache, mg->old_ocell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000917 cleanup_migration(mg);
918 return;
919
920 } else if (mg->demote) {
921 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
922 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
923 policy_force_mapping(cache->policy, mg->new_oblock,
924 mg->old_oblock);
925 if (mg->promote)
926 cell_defer(cache, mg->new_ocell, true);
927 cleanup_migration(mg);
928 return;
929 }
930 } else {
931 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
932 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
933 policy_remove_mapping(cache->policy, mg->new_oblock);
934 cleanup_migration(mg);
935 return;
936 }
937 }
938
939 spin_lock_irqsave(&cache->lock, flags);
940 list_add_tail(&mg->list, &cache->need_commit_migrations);
941 cache->commit_requested = true;
942 spin_unlock_irqrestore(&cache->lock, flags);
943}
944
945static void migration_success_post_commit(struct dm_cache_migration *mg)
946{
947 unsigned long flags;
948 struct cache *cache = mg->cache;
949
950 if (mg->writeback) {
951 DMWARN("writeback unexpectedly triggered commit");
952 return;
953
954 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200955 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000956
957 if (mg->promote) {
958 mg->demote = false;
959
960 spin_lock_irqsave(&cache->lock, flags);
961 list_add_tail(&mg->list, &cache->quiesced_migrations);
962 spin_unlock_irqrestore(&cache->lock, flags);
963
Joe Thornber65790ff2013-11-08 16:39:50 +0000964 } else {
965 if (mg->invalidate)
966 policy_remove_mapping(cache->policy, mg->old_oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000967 cleanup_migration(mg);
Joe Thornber65790ff2013-11-08 16:39:50 +0000968 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000969
970 } else {
Anssi Hannula40aa9782014-09-05 03:11:28 +0300971 clear_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400972 if (mg->requeue_holder)
973 cell_defer(cache, mg->new_ocell, true);
974 else {
975 bio_endio(mg->new_ocell->holder, 0);
976 cell_defer(cache, mg->new_ocell, false);
977 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000978 cleanup_migration(mg);
979 }
980}
981
982static void copy_complete(int read_err, unsigned long write_err, void *context)
983{
984 unsigned long flags;
985 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
986 struct cache *cache = mg->cache;
987
988 if (read_err || write_err)
989 mg->err = true;
990
991 spin_lock_irqsave(&cache->lock, flags);
992 list_add_tail(&mg->list, &cache->completed_migrations);
993 spin_unlock_irqrestore(&cache->lock, flags);
994
995 wake_worker(cache);
996}
997
998static void issue_copy_real(struct dm_cache_migration *mg)
999{
1000 int r;
1001 struct dm_io_region o_region, c_region;
1002 struct cache *cache = mg->cache;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001003 sector_t cblock = from_cblock(mg->cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001004
1005 o_region.bdev = cache->origin_dev->bdev;
1006 o_region.count = cache->sectors_per_block;
1007
1008 c_region.bdev = cache->cache_dev->bdev;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001009 c_region.sector = cblock * cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001010 c_region.count = cache->sectors_per_block;
1011
1012 if (mg->writeback || mg->demote) {
1013 /* demote */
1014 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
1015 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
1016 } else {
1017 /* promote */
1018 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1019 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1020 }
1021
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001022 if (r < 0) {
1023 DMERR_LIMIT("issuing migration failed");
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001024 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001025 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001026}
1027
Joe Thornberc9d28d52013-10-31 13:55:48 -04001028static void overwrite_endio(struct bio *bio, int err)
1029{
1030 struct dm_cache_migration *mg = bio->bi_private;
1031 struct cache *cache = mg->cache;
1032 size_t pb_data_size = get_per_bio_data_size(cache);
1033 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1034 unsigned long flags;
1035
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001036 dm_unhook_bio(&pb->hook_info, bio);
1037
Joe Thornberc9d28d52013-10-31 13:55:48 -04001038 if (err)
1039 mg->err = true;
1040
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001041 mg->requeue_holder = false;
1042
Joe Thornberc9d28d52013-10-31 13:55:48 -04001043 spin_lock_irqsave(&cache->lock, flags);
1044 list_add_tail(&mg->list, &cache->completed_migrations);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001045 spin_unlock_irqrestore(&cache->lock, flags);
1046
1047 wake_worker(cache);
1048}
1049
1050static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1051{
1052 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1053 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1054
1055 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1056 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001057
1058 /*
1059 * No need to inc_ds() here, since the cell will be held for the
1060 * duration of the io.
1061 */
Joe Thornberc9d28d52013-10-31 13:55:48 -04001062 generic_make_request(bio);
1063}
1064
1065static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1066{
1067 return (bio_data_dir(bio) == WRITE) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001068 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
Joe Thornberc9d28d52013-10-31 13:55:48 -04001069}
1070
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001071static void avoid_copy(struct dm_cache_migration *mg)
1072{
1073 atomic_inc(&mg->cache->stats.copies_avoided);
1074 migration_success_pre_commit(mg);
1075}
1076
1077static void issue_copy(struct dm_cache_migration *mg)
1078{
1079 bool avoid;
1080 struct cache *cache = mg->cache;
1081
1082 if (mg->writeback || mg->demote)
1083 avoid = !is_dirty(cache, mg->cblock) ||
1084 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001085 else {
1086 struct bio *bio = mg->new_ocell->holder;
1087
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001088 avoid = is_discarded_oblock(cache, mg->new_oblock);
1089
Joe Thornberc9d28d52013-10-31 13:55:48 -04001090 if (!avoid && bio_writes_complete_block(cache, bio)) {
1091 issue_overwrite(mg, bio);
1092 return;
1093 }
1094 }
1095
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001096 avoid ? avoid_copy(mg) : issue_copy_real(mg);
1097}
1098
1099static void complete_migration(struct dm_cache_migration *mg)
1100{
1101 if (mg->err)
1102 migration_failure(mg);
1103 else
1104 migration_success_pre_commit(mg);
1105}
1106
1107static void process_migrations(struct cache *cache, struct list_head *head,
1108 void (*fn)(struct dm_cache_migration *))
1109{
1110 unsigned long flags;
1111 struct list_head list;
1112 struct dm_cache_migration *mg, *tmp;
1113
1114 INIT_LIST_HEAD(&list);
1115 spin_lock_irqsave(&cache->lock, flags);
1116 list_splice_init(head, &list);
1117 spin_unlock_irqrestore(&cache->lock, flags);
1118
1119 list_for_each_entry_safe(mg, tmp, &list, list)
1120 fn(mg);
1121}
1122
1123static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1124{
1125 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1126}
1127
1128static void queue_quiesced_migration(struct dm_cache_migration *mg)
1129{
1130 unsigned long flags;
1131 struct cache *cache = mg->cache;
1132
1133 spin_lock_irqsave(&cache->lock, flags);
1134 __queue_quiesced_migration(mg);
1135 spin_unlock_irqrestore(&cache->lock, flags);
1136
1137 wake_worker(cache);
1138}
1139
1140static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1141{
1142 unsigned long flags;
1143 struct dm_cache_migration *mg, *tmp;
1144
1145 spin_lock_irqsave(&cache->lock, flags);
1146 list_for_each_entry_safe(mg, tmp, work, list)
1147 __queue_quiesced_migration(mg);
1148 spin_unlock_irqrestore(&cache->lock, flags);
1149
1150 wake_worker(cache);
1151}
1152
1153static void check_for_quiesced_migrations(struct cache *cache,
1154 struct per_bio_data *pb)
1155{
1156 struct list_head work;
1157
1158 if (!pb->all_io_entry)
1159 return;
1160
1161 INIT_LIST_HEAD(&work);
Joe Thornber8c081b52014-05-13 16:18:38 +01001162 dm_deferred_entry_dec(pb->all_io_entry, &work);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001163
1164 if (!list_empty(&work))
1165 queue_quiesced_migrations(cache, &work);
1166}
1167
1168static void quiesce_migration(struct dm_cache_migration *mg)
1169{
1170 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1171 queue_quiesced_migration(mg);
1172}
1173
1174static void promote(struct cache *cache, struct prealloc *structs,
1175 dm_oblock_t oblock, dm_cblock_t cblock,
1176 struct dm_bio_prison_cell *cell)
1177{
1178 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1179
1180 mg->err = false;
1181 mg->writeback = false;
1182 mg->demote = false;
1183 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001184 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001185 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001186 mg->cache = cache;
1187 mg->new_oblock = oblock;
1188 mg->cblock = cblock;
1189 mg->old_ocell = NULL;
1190 mg->new_ocell = cell;
1191 mg->start_jiffies = jiffies;
1192
1193 inc_nr_migrations(cache);
1194 quiesce_migration(mg);
1195}
1196
1197static void writeback(struct cache *cache, struct prealloc *structs,
1198 dm_oblock_t oblock, dm_cblock_t cblock,
1199 struct dm_bio_prison_cell *cell)
1200{
1201 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1202
1203 mg->err = false;
1204 mg->writeback = true;
1205 mg->demote = false;
1206 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001207 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001208 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001209 mg->cache = cache;
1210 mg->old_oblock = oblock;
1211 mg->cblock = cblock;
1212 mg->old_ocell = cell;
1213 mg->new_ocell = NULL;
1214 mg->start_jiffies = jiffies;
1215
1216 inc_nr_migrations(cache);
1217 quiesce_migration(mg);
1218}
1219
1220static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1221 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1222 dm_cblock_t cblock,
1223 struct dm_bio_prison_cell *old_ocell,
1224 struct dm_bio_prison_cell *new_ocell)
1225{
1226 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1227
1228 mg->err = false;
1229 mg->writeback = false;
1230 mg->demote = true;
1231 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001232 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001233 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001234 mg->cache = cache;
1235 mg->old_oblock = old_oblock;
1236 mg->new_oblock = new_oblock;
1237 mg->cblock = cblock;
1238 mg->old_ocell = old_ocell;
1239 mg->new_ocell = new_ocell;
1240 mg->start_jiffies = jiffies;
1241
1242 inc_nr_migrations(cache);
1243 quiesce_migration(mg);
1244}
1245
Joe Thornber2ee57d52013-10-24 14:10:29 -04001246/*
1247 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1248 * block are thrown away.
1249 */
1250static void invalidate(struct cache *cache, struct prealloc *structs,
1251 dm_oblock_t oblock, dm_cblock_t cblock,
1252 struct dm_bio_prison_cell *cell)
1253{
1254 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1255
1256 mg->err = false;
1257 mg->writeback = false;
1258 mg->demote = true;
1259 mg->promote = false;
1260 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001261 mg->invalidate = true;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001262 mg->cache = cache;
1263 mg->old_oblock = oblock;
1264 mg->cblock = cblock;
1265 mg->old_ocell = cell;
1266 mg->new_ocell = NULL;
1267 mg->start_jiffies = jiffies;
1268
1269 inc_nr_migrations(cache);
1270 quiesce_migration(mg);
1271}
1272
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001273/*----------------------------------------------------------------
1274 * bio processing
1275 *--------------------------------------------------------------*/
1276static void defer_bio(struct cache *cache, struct bio *bio)
1277{
1278 unsigned long flags;
1279
1280 spin_lock_irqsave(&cache->lock, flags);
1281 bio_list_add(&cache->deferred_bios, bio);
1282 spin_unlock_irqrestore(&cache->lock, flags);
1283
1284 wake_worker(cache);
1285}
1286
1287static void process_flush_bio(struct cache *cache, struct bio *bio)
1288{
Mike Snitzer19b00922013-04-05 15:36:34 +01001289 size_t pb_data_size = get_per_bio_data_size(cache);
1290 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001291
Kent Overstreet4f024f32013-10-11 15:44:27 -07001292 BUG_ON(bio->bi_iter.bi_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001293 if (!pb->req_nr)
1294 remap_to_origin(cache, bio);
1295 else
1296 remap_to_cache(cache, bio, 0);
1297
Joe Thornber8c081b52014-05-13 16:18:38 +01001298 /*
1299 * REQ_FLUSH is not directed at any particular block so we don't
1300 * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH
1301 * by dm-core.
1302 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001303 issue(cache, bio);
1304}
1305
1306/*
1307 * People generally discard large parts of a device, eg, the whole device
1308 * when formatting. Splitting these large discards up into cache block
1309 * sized ios and then quiescing (always neccessary for discard) takes too
1310 * long.
1311 *
1312 * We keep it simple, and allow any size of discard to come in, and just
1313 * mark off blocks on the discard bitset. No passdown occurs!
1314 *
1315 * To implement passdown we need to change the bio_prison such that a cell
1316 * can have a key that spans many blocks.
1317 */
1318static void process_discard_bio(struct cache *cache, struct bio *bio)
1319{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001320 dm_block_t start_block = dm_sector_div_up(bio->bi_iter.bi_sector,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001321 cache->discard_block_size);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001322 dm_block_t end_block = bio_end_sector(bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001323 dm_block_t b;
1324
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001325 end_block = block_div(end_block, cache->discard_block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001326
1327 for (b = start_block; b < end_block; b++)
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001328 set_discard(cache, to_dblock(b));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001329
1330 bio_endio(bio, 0);
1331}
1332
1333static bool spare_migration_bandwidth(struct cache *cache)
1334{
1335 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1336 cache->sectors_per_block;
1337 return current_volume < cache->migration_threshold;
1338}
1339
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001340static void inc_hit_counter(struct cache *cache, struct bio *bio)
1341{
1342 atomic_inc(bio_data_dir(bio) == READ ?
1343 &cache->stats.read_hit : &cache->stats.write_hit);
1344}
1345
1346static void inc_miss_counter(struct cache *cache, struct bio *bio)
1347{
1348 atomic_inc(bio_data_dir(bio) == READ ?
1349 &cache->stats.read_miss : &cache->stats.write_miss);
1350}
1351
1352static void process_bio(struct cache *cache, struct prealloc *structs,
1353 struct bio *bio)
1354{
1355 int r;
1356 bool release_cell = true;
1357 dm_oblock_t block = get_bio_block(cache, bio);
1358 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1359 struct policy_result lookup_result;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001360 bool discarded_block = is_discarded_oblock(cache, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001361 bool passthrough = passthrough_mode(&cache->features);
1362 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001363
1364 /*
1365 * Check to see if that block is currently migrating.
1366 */
1367 cell_prealloc = prealloc_get_cell(structs);
1368 r = bio_detain(cache, block, bio, cell_prealloc,
1369 (cell_free_fn) prealloc_put_cell,
1370 structs, &new_ocell);
1371 if (r > 0)
1372 return;
1373
1374 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1375 bio, &lookup_result);
1376
1377 if (r == -EWOULDBLOCK)
1378 /* migration has been denied */
1379 lookup_result.op = POLICY_MISS;
1380
1381 switch (lookup_result.op) {
1382 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001383 if (passthrough) {
1384 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001385
Joe Thornber2ee57d52013-10-24 14:10:29 -04001386 /*
1387 * Passthrough always maps to the origin,
1388 * invalidating any cache blocks that are written
1389 * to.
1390 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001391
Joe Thornber2ee57d52013-10-24 14:10:29 -04001392 if (bio_data_dir(bio) == WRITE) {
1393 atomic_inc(&cache->stats.demotion);
1394 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1395 release_cell = false;
1396
1397 } else {
1398 /* FIXME: factor out issue_origin() */
Joe Thornber2ee57d52013-10-24 14:10:29 -04001399 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001400 inc_and_issue(cache, bio, new_ocell);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001401 }
1402 } else {
1403 inc_hit_counter(cache, bio);
1404
1405 if (bio_data_dir(bio) == WRITE &&
1406 writethrough_mode(&cache->features) &&
1407 !is_dirty(cache, lookup_result.cblock)) {
Joe Thornber2ee57d52013-10-24 14:10:29 -04001408 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001409 inc_and_issue(cache, bio, new_ocell);
1410
1411 } else {
1412 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1413 inc_and_issue(cache, bio, new_ocell);
1414 }
Joe Thornber2ee57d52013-10-24 14:10:29 -04001415 }
1416
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001417 break;
1418
1419 case POLICY_MISS:
1420 inc_miss_counter(cache, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001421 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001422 inc_and_issue(cache, bio, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001423 break;
1424
1425 case POLICY_NEW:
1426 atomic_inc(&cache->stats.promotion);
1427 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1428 release_cell = false;
1429 break;
1430
1431 case POLICY_REPLACE:
1432 cell_prealloc = prealloc_get_cell(structs);
1433 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1434 (cell_free_fn) prealloc_put_cell,
1435 structs, &old_ocell);
1436 if (r > 0) {
1437 /*
1438 * We have to be careful to avoid lock inversion of
1439 * the cells. So we back off, and wait for the
1440 * old_ocell to become free.
1441 */
1442 policy_force_mapping(cache->policy, block,
1443 lookup_result.old_oblock);
1444 atomic_inc(&cache->stats.cache_cell_clash);
1445 break;
1446 }
1447 atomic_inc(&cache->stats.demotion);
1448 atomic_inc(&cache->stats.promotion);
1449
1450 demote_then_promote(cache, structs, lookup_result.old_oblock,
1451 block, lookup_result.cblock,
1452 old_ocell, new_ocell);
1453 release_cell = false;
1454 break;
1455
1456 default:
1457 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1458 (unsigned) lookup_result.op);
1459 bio_io_error(bio);
1460 }
1461
1462 if (release_cell)
1463 cell_defer(cache, new_ocell, false);
1464}
1465
1466static int need_commit_due_to_time(struct cache *cache)
1467{
1468 return jiffies < cache->last_commit_jiffies ||
1469 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1470}
1471
1472static int commit_if_needed(struct cache *cache)
1473{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001474 int r = 0;
1475
1476 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1477 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001478 atomic_inc(&cache->stats.commit_count);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001479 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001480 r = dm_cache_commit(cache->cmd, false);
1481 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001482 }
1483
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001484 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001485}
1486
1487static void process_deferred_bios(struct cache *cache)
1488{
1489 unsigned long flags;
1490 struct bio_list bios;
1491 struct bio *bio;
1492 struct prealloc structs;
1493
1494 memset(&structs, 0, sizeof(structs));
1495 bio_list_init(&bios);
1496
1497 spin_lock_irqsave(&cache->lock, flags);
1498 bio_list_merge(&bios, &cache->deferred_bios);
1499 bio_list_init(&cache->deferred_bios);
1500 spin_unlock_irqrestore(&cache->lock, flags);
1501
1502 while (!bio_list_empty(&bios)) {
1503 /*
1504 * If we've got no free migration structs, and processing
1505 * this bio might require one, we pause until there are some
1506 * prepared mappings to process.
1507 */
1508 if (prealloc_data_structs(cache, &structs)) {
1509 spin_lock_irqsave(&cache->lock, flags);
1510 bio_list_merge(&cache->deferred_bios, &bios);
1511 spin_unlock_irqrestore(&cache->lock, flags);
1512 break;
1513 }
1514
1515 bio = bio_list_pop(&bios);
1516
1517 if (bio->bi_rw & REQ_FLUSH)
1518 process_flush_bio(cache, bio);
1519 else if (bio->bi_rw & REQ_DISCARD)
1520 process_discard_bio(cache, bio);
1521 else
1522 process_bio(cache, &structs, bio);
1523 }
1524
1525 prealloc_free_structs(cache, &structs);
1526}
1527
1528static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1529{
1530 unsigned long flags;
1531 struct bio_list bios;
1532 struct bio *bio;
1533
1534 bio_list_init(&bios);
1535
1536 spin_lock_irqsave(&cache->lock, flags);
1537 bio_list_merge(&bios, &cache->deferred_flush_bios);
1538 bio_list_init(&cache->deferred_flush_bios);
1539 spin_unlock_irqrestore(&cache->lock, flags);
1540
Joe Thornber8c081b52014-05-13 16:18:38 +01001541 /*
1542 * These bios have already been through inc_ds()
1543 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001544 while ((bio = bio_list_pop(&bios)))
1545 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1546}
1547
Joe Thornbere2e74d62013-03-20 17:21:27 +00001548static void process_deferred_writethrough_bios(struct cache *cache)
1549{
1550 unsigned long flags;
1551 struct bio_list bios;
1552 struct bio *bio;
1553
1554 bio_list_init(&bios);
1555
1556 spin_lock_irqsave(&cache->lock, flags);
1557 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1558 bio_list_init(&cache->deferred_writethrough_bios);
1559 spin_unlock_irqrestore(&cache->lock, flags);
1560
Joe Thornber8c081b52014-05-13 16:18:38 +01001561 /*
1562 * These bios have already been through inc_ds()
1563 */
Joe Thornbere2e74d62013-03-20 17:21:27 +00001564 while ((bio = bio_list_pop(&bios)))
1565 generic_make_request(bio);
1566}
1567
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001568static void writeback_some_dirty_blocks(struct cache *cache)
1569{
1570 int r = 0;
1571 dm_oblock_t oblock;
1572 dm_cblock_t cblock;
1573 struct prealloc structs;
1574 struct dm_bio_prison_cell *old_ocell;
1575
1576 memset(&structs, 0, sizeof(structs));
1577
1578 while (spare_migration_bandwidth(cache)) {
1579 if (prealloc_data_structs(cache, &structs))
1580 break;
1581
1582 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1583 if (r)
1584 break;
1585
1586 r = get_cell(cache, oblock, &structs, &old_ocell);
1587 if (r) {
1588 policy_set_dirty(cache->policy, oblock);
1589 break;
1590 }
1591
1592 writeback(cache, &structs, oblock, cblock, old_ocell);
1593 }
1594
1595 prealloc_free_structs(cache, &structs);
1596}
1597
1598/*----------------------------------------------------------------
Joe Thornber65790ff2013-11-08 16:39:50 +00001599 * Invalidations.
1600 * Dropping something from the cache *without* writing back.
1601 *--------------------------------------------------------------*/
1602
1603static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1604{
1605 int r = 0;
1606 uint64_t begin = from_cblock(req->cblocks->begin);
1607 uint64_t end = from_cblock(req->cblocks->end);
1608
1609 while (begin != end) {
1610 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1611 if (!r) {
1612 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1613 if (r)
1614 break;
1615
1616 } else if (r == -ENODATA) {
1617 /* harmless, already unmapped */
1618 r = 0;
1619
1620 } else {
1621 DMERR("policy_remove_cblock failed");
1622 break;
1623 }
1624
1625 begin++;
1626 }
1627
1628 cache->commit_requested = true;
1629
1630 req->err = r;
1631 atomic_set(&req->complete, 1);
1632
1633 wake_up(&req->result_wait);
1634}
1635
1636static void process_invalidation_requests(struct cache *cache)
1637{
1638 struct list_head list;
1639 struct invalidation_request *req, *tmp;
1640
1641 INIT_LIST_HEAD(&list);
1642 spin_lock(&cache->invalidation_lock);
1643 list_splice_init(&cache->invalidation_requests, &list);
1644 spin_unlock(&cache->invalidation_lock);
1645
1646 list_for_each_entry_safe (req, tmp, &list, list)
1647 process_invalidation_request(cache, req);
1648}
1649
1650/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001651 * Main worker loop
1652 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001653static bool is_quiescing(struct cache *cache)
1654{
Joe Thornber238f8362013-10-30 17:29:30 +00001655 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001656}
1657
Joe Thornber66cb1912013-10-30 17:11:58 +00001658static void ack_quiescing(struct cache *cache)
1659{
1660 if (is_quiescing(cache)) {
1661 atomic_inc(&cache->quiescing_ack);
1662 wake_up(&cache->quiescing_wait);
1663 }
1664}
1665
1666static void wait_for_quiescing_ack(struct cache *cache)
1667{
1668 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1669}
1670
1671static void start_quiescing(struct cache *cache)
1672{
Joe Thornber238f8362013-10-30 17:29:30 +00001673 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00001674 wait_for_quiescing_ack(cache);
1675}
1676
1677static void stop_quiescing(struct cache *cache)
1678{
Joe Thornber238f8362013-10-30 17:29:30 +00001679 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00001680 atomic_set(&cache->quiescing_ack, 0);
1681}
1682
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001683static void wait_for_migrations(struct cache *cache)
1684{
1685 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1686}
1687
1688static void stop_worker(struct cache *cache)
1689{
1690 cancel_delayed_work(&cache->waker);
1691 flush_workqueue(cache->wq);
1692}
1693
1694static void requeue_deferred_io(struct cache *cache)
1695{
1696 struct bio *bio;
1697 struct bio_list bios;
1698
1699 bio_list_init(&bios);
1700 bio_list_merge(&bios, &cache->deferred_bios);
1701 bio_list_init(&cache->deferred_bios);
1702
1703 while ((bio = bio_list_pop(&bios)))
1704 bio_endio(bio, DM_ENDIO_REQUEUE);
1705}
1706
1707static int more_work(struct cache *cache)
1708{
1709 if (is_quiescing(cache))
1710 return !list_empty(&cache->quiesced_migrations) ||
1711 !list_empty(&cache->completed_migrations) ||
1712 !list_empty(&cache->need_commit_migrations);
1713 else
1714 return !bio_list_empty(&cache->deferred_bios) ||
1715 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001716 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001717 !list_empty(&cache->quiesced_migrations) ||
1718 !list_empty(&cache->completed_migrations) ||
Joe Thornber65790ff2013-11-08 16:39:50 +00001719 !list_empty(&cache->need_commit_migrations) ||
1720 cache->invalidate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001721}
1722
1723static void do_worker(struct work_struct *ws)
1724{
1725 struct cache *cache = container_of(ws, struct cache, worker);
1726
1727 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001728 if (!is_quiescing(cache)) {
1729 writeback_some_dirty_blocks(cache);
1730 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001731 process_deferred_bios(cache);
Joe Thornber65790ff2013-11-08 16:39:50 +00001732 process_invalidation_requests(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001733 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001734
1735 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1736 process_migrations(cache, &cache->completed_migrations, complete_migration);
1737
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001738 if (commit_if_needed(cache)) {
1739 process_deferred_flush_bios(cache, false);
Joe Thornber304affa2014-06-24 15:36:58 -04001740 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001741
1742 /*
1743 * FIXME: rollback metadata or just go into a
1744 * failure mode and error everything
1745 */
1746 } else {
1747 process_deferred_flush_bios(cache, true);
1748 process_migrations(cache, &cache->need_commit_migrations,
1749 migration_success_post_commit);
1750 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001751
1752 ack_quiescing(cache);
1753
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001754 } while (more_work(cache));
1755}
1756
1757/*
1758 * We want to commit periodically so that not too much
1759 * unwritten metadata builds up.
1760 */
1761static void do_waker(struct work_struct *ws)
1762{
1763 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001764 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001765 wake_worker(cache);
1766 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1767}
1768
1769/*----------------------------------------------------------------*/
1770
1771static int is_congested(struct dm_dev *dev, int bdi_bits)
1772{
1773 struct request_queue *q = bdev_get_queue(dev->bdev);
1774 return bdi_congested(&q->backing_dev_info, bdi_bits);
1775}
1776
1777static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1778{
1779 struct cache *cache = container_of(cb, struct cache, callbacks);
1780
1781 return is_congested(cache->origin_dev, bdi_bits) ||
1782 is_congested(cache->cache_dev, bdi_bits);
1783}
1784
1785/*----------------------------------------------------------------
1786 * Target methods
1787 *--------------------------------------------------------------*/
1788
1789/*
1790 * This function gets called on the error paths of the constructor, so we
1791 * have to cope with a partially initialised struct.
1792 */
1793static void destroy(struct cache *cache)
1794{
1795 unsigned i;
1796
1797 if (cache->next_migration)
1798 mempool_free(cache->next_migration, cache->migration_pool);
1799
1800 if (cache->migration_pool)
1801 mempool_destroy(cache->migration_pool);
1802
1803 if (cache->all_io_ds)
1804 dm_deferred_set_destroy(cache->all_io_ds);
1805
1806 if (cache->prison)
1807 dm_bio_prison_destroy(cache->prison);
1808
1809 if (cache->wq)
1810 destroy_workqueue(cache->wq);
1811
1812 if (cache->dirty_bitset)
1813 free_bitset(cache->dirty_bitset);
1814
1815 if (cache->discard_bitset)
1816 free_bitset(cache->discard_bitset);
1817
1818 if (cache->copier)
1819 dm_kcopyd_client_destroy(cache->copier);
1820
1821 if (cache->cmd)
1822 dm_cache_metadata_close(cache->cmd);
1823
1824 if (cache->metadata_dev)
1825 dm_put_device(cache->ti, cache->metadata_dev);
1826
1827 if (cache->origin_dev)
1828 dm_put_device(cache->ti, cache->origin_dev);
1829
1830 if (cache->cache_dev)
1831 dm_put_device(cache->ti, cache->cache_dev);
1832
1833 if (cache->policy)
1834 dm_cache_policy_destroy(cache->policy);
1835
1836 for (i = 0; i < cache->nr_ctr_args ; i++)
1837 kfree(cache->ctr_args[i]);
1838 kfree(cache->ctr_args);
1839
1840 kfree(cache);
1841}
1842
1843static void cache_dtr(struct dm_target *ti)
1844{
1845 struct cache *cache = ti->private;
1846
1847 destroy(cache);
1848}
1849
1850static sector_t get_dev_size(struct dm_dev *dev)
1851{
1852 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1853}
1854
1855/*----------------------------------------------------------------*/
1856
1857/*
1858 * Construct a cache device mapping.
1859 *
1860 * cache <metadata dev> <cache dev> <origin dev> <block size>
1861 * <#feature args> [<feature arg>]*
1862 * <policy> <#policy args> [<policy arg>]*
1863 *
1864 * metadata dev : fast device holding the persistent metadata
1865 * cache dev : fast device holding cached data blocks
1866 * origin dev : slow device holding original data blocks
1867 * block size : cache unit size in sectors
1868 *
1869 * #feature args : number of feature arguments passed
1870 * feature args : writethrough. (The default is writeback.)
1871 *
1872 * policy : the replacement policy to use
1873 * #policy args : an even number of policy arguments corresponding
1874 * to key/value pairs passed to the policy
1875 * policy args : key/value pairs passed to the policy
1876 * E.g. 'sequential_threshold 1024'
1877 * See cache-policies.txt for details.
1878 *
1879 * Optional feature arguments are:
1880 * writethrough : write through caching that prohibits cache block
1881 * content from being different from origin block content.
1882 * Without this argument, the default behaviour is to write
1883 * back cache block contents later for performance reasons,
1884 * so they may differ from the corresponding origin blocks.
1885 */
1886struct cache_args {
1887 struct dm_target *ti;
1888
1889 struct dm_dev *metadata_dev;
1890
1891 struct dm_dev *cache_dev;
1892 sector_t cache_sectors;
1893
1894 struct dm_dev *origin_dev;
1895 sector_t origin_sectors;
1896
1897 uint32_t block_size;
1898
1899 const char *policy_name;
1900 int policy_argc;
1901 const char **policy_argv;
1902
1903 struct cache_features features;
1904};
1905
1906static void destroy_cache_args(struct cache_args *ca)
1907{
1908 if (ca->metadata_dev)
1909 dm_put_device(ca->ti, ca->metadata_dev);
1910
1911 if (ca->cache_dev)
1912 dm_put_device(ca->ti, ca->cache_dev);
1913
1914 if (ca->origin_dev)
1915 dm_put_device(ca->ti, ca->origin_dev);
1916
1917 kfree(ca);
1918}
1919
1920static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1921{
1922 if (!as->argc) {
1923 *error = "Insufficient args";
1924 return false;
1925 }
1926
1927 return true;
1928}
1929
1930static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1931 char **error)
1932{
1933 int r;
1934 sector_t metadata_dev_size;
1935 char b[BDEVNAME_SIZE];
1936
1937 if (!at_least_one_arg(as, error))
1938 return -EINVAL;
1939
1940 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1941 &ca->metadata_dev);
1942 if (r) {
1943 *error = "Error opening metadata device";
1944 return r;
1945 }
1946
1947 metadata_dev_size = get_dev_size(ca->metadata_dev);
1948 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1949 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1950 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1951
1952 return 0;
1953}
1954
1955static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1956 char **error)
1957{
1958 int r;
1959
1960 if (!at_least_one_arg(as, error))
1961 return -EINVAL;
1962
1963 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1964 &ca->cache_dev);
1965 if (r) {
1966 *error = "Error opening cache device";
1967 return r;
1968 }
1969 ca->cache_sectors = get_dev_size(ca->cache_dev);
1970
1971 return 0;
1972}
1973
1974static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1975 char **error)
1976{
1977 int r;
1978
1979 if (!at_least_one_arg(as, error))
1980 return -EINVAL;
1981
1982 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1983 &ca->origin_dev);
1984 if (r) {
1985 *error = "Error opening origin device";
1986 return r;
1987 }
1988
1989 ca->origin_sectors = get_dev_size(ca->origin_dev);
1990 if (ca->ti->len > ca->origin_sectors) {
1991 *error = "Device size larger than cached device";
1992 return -EINVAL;
1993 }
1994
1995 return 0;
1996}
1997
1998static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1999 char **error)
2000{
Mike Snitzer05473042013-08-16 10:54:19 -04002001 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002002
2003 if (!at_least_one_arg(as, error))
2004 return -EINVAL;
2005
Mike Snitzer05473042013-08-16 10:54:19 -04002006 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
2007 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2008 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2009 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002010 *error = "Invalid data block size";
2011 return -EINVAL;
2012 }
2013
Mike Snitzer05473042013-08-16 10:54:19 -04002014 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002015 *error = "Data block size is larger than the cache device";
2016 return -EINVAL;
2017 }
2018
Mike Snitzer05473042013-08-16 10:54:19 -04002019 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002020
2021 return 0;
2022}
2023
2024static void init_features(struct cache_features *cf)
2025{
2026 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04002027 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002028}
2029
2030static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2031 char **error)
2032{
2033 static struct dm_arg _args[] = {
2034 {0, 1, "Invalid number of cache feature arguments"},
2035 };
2036
2037 int r;
2038 unsigned argc;
2039 const char *arg;
2040 struct cache_features *cf = &ca->features;
2041
2042 init_features(cf);
2043
2044 r = dm_read_arg_group(_args, as, &argc, error);
2045 if (r)
2046 return -EINVAL;
2047
2048 while (argc--) {
2049 arg = dm_shift_arg(as);
2050
2051 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002052 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002053
2054 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002055 cf->io_mode = CM_IO_WRITETHROUGH;
2056
2057 else if (!strcasecmp(arg, "passthrough"))
2058 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002059
2060 else {
2061 *error = "Unrecognised cache feature requested";
2062 return -EINVAL;
2063 }
2064 }
2065
2066 return 0;
2067}
2068
2069static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2070 char **error)
2071{
2072 static struct dm_arg _args[] = {
2073 {0, 1024, "Invalid number of policy arguments"},
2074 };
2075
2076 int r;
2077
2078 if (!at_least_one_arg(as, error))
2079 return -EINVAL;
2080
2081 ca->policy_name = dm_shift_arg(as);
2082
2083 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2084 if (r)
2085 return -EINVAL;
2086
2087 ca->policy_argv = (const char **)as->argv;
2088 dm_consume_args(as, ca->policy_argc);
2089
2090 return 0;
2091}
2092
2093static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2094 char **error)
2095{
2096 int r;
2097 struct dm_arg_set as;
2098
2099 as.argc = argc;
2100 as.argv = argv;
2101
2102 r = parse_metadata_dev(ca, &as, error);
2103 if (r)
2104 return r;
2105
2106 r = parse_cache_dev(ca, &as, error);
2107 if (r)
2108 return r;
2109
2110 r = parse_origin_dev(ca, &as, error);
2111 if (r)
2112 return r;
2113
2114 r = parse_block_size(ca, &as, error);
2115 if (r)
2116 return r;
2117
2118 r = parse_features(ca, &as, error);
2119 if (r)
2120 return r;
2121
2122 r = parse_policy(ca, &as, error);
2123 if (r)
2124 return r;
2125
2126 return 0;
2127}
2128
2129/*----------------------------------------------------------------*/
2130
2131static struct kmem_cache *migration_cache;
2132
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002133#define NOT_CORE_OPTION 1
2134
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002135static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002136{
2137 unsigned long tmp;
2138
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002139 if (!strcasecmp(key, "migration_threshold")) {
2140 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002141 return -EINVAL;
2142
2143 cache->migration_threshold = tmp;
2144 return 0;
2145 }
2146
2147 return NOT_CORE_OPTION;
2148}
2149
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002150static int set_config_value(struct cache *cache, const char *key, const char *value)
2151{
2152 int r = process_config_option(cache, key, value);
2153
2154 if (r == NOT_CORE_OPTION)
2155 r = policy_set_config_value(cache->policy, key, value);
2156
2157 if (r)
2158 DMWARN("bad config value for %s: %s", key, value);
2159
2160 return r;
2161}
2162
2163static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002164{
2165 int r = 0;
2166
2167 if (argc & 1) {
2168 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2169 return -EINVAL;
2170 }
2171
2172 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002173 r = set_config_value(cache, argv[0], argv[1]);
2174 if (r)
2175 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002176
2177 argc -= 2;
2178 argv += 2;
2179 }
2180
2181 return r;
2182}
2183
2184static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2185 char **error)
2186{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002187 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2188 cache->cache_size,
2189 cache->origin_sectors,
2190 cache->sectors_per_block);
2191 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002192 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002193 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002194 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002195 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002196
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002197 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002198}
2199
Joe Thornberf8350da2013-05-10 14:37:16 +01002200#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002201
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002202static int cache_create(struct cache_args *ca, struct cache **result)
2203{
2204 int r = 0;
2205 char **error = &ca->ti->error;
2206 struct cache *cache;
2207 struct dm_target *ti = ca->ti;
2208 dm_block_t origin_blocks;
2209 struct dm_cache_metadata *cmd;
2210 bool may_format = ca->features.mode == CM_WRITE;
2211
2212 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2213 if (!cache)
2214 return -ENOMEM;
2215
2216 cache->ti = ca->ti;
2217 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002218 ti->num_flush_bios = 2;
2219 ti->flush_supported = true;
2220
2221 ti->num_discard_bios = 1;
2222 ti->discards_supported = true;
2223 ti->discard_zeroes_data_unsupported = true;
Heinz Mauelshagenf1daa8382014-05-23 14:10:01 -04002224 /* Discard bios must be split on a block boundary */
2225 ti->split_discard_bios = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002226
Joe Thornber8c5008f2013-05-10 14:37:18 +01002227 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01002228 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002229
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002230 cache->callbacks.congested_fn = cache_is_congested;
2231 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2232
2233 cache->metadata_dev = ca->metadata_dev;
2234 cache->origin_dev = ca->origin_dev;
2235 cache->cache_dev = ca->cache_dev;
2236
2237 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2238
2239 /* FIXME: factor out this whole section */
2240 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002241 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002242 cache->origin_blocks = to_oblock(origin_blocks);
2243
2244 cache->sectors_per_block = ca->block_size;
2245 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2246 r = -EINVAL;
2247 goto bad;
2248 }
2249
2250 if (ca->block_size & (ca->block_size - 1)) {
2251 dm_block_t cache_size = ca->cache_sectors;
2252
2253 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002254 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002255 cache->cache_size = to_cblock(cache_size);
2256 } else {
2257 cache->sectors_per_block_shift = __ffs(ca->block_size);
2258 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2259 }
2260
2261 r = create_cache_policy(cache, ca, error);
2262 if (r)
2263 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002264
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002265 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002266 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2267
2268 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2269 if (r) {
2270 *error = "Error setting cache policy's config values";
2271 goto bad;
2272 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002273
2274 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2275 ca->block_size, may_format,
2276 dm_cache_policy_get_hint_size(cache->policy));
2277 if (IS_ERR(cmd)) {
2278 *error = "Error creating metadata object";
2279 r = PTR_ERR(cmd);
2280 goto bad;
2281 }
2282 cache->cmd = cmd;
2283
Joe Thornber2ee57d52013-10-24 14:10:29 -04002284 if (passthrough_mode(&cache->features)) {
2285 bool all_clean;
2286
2287 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2288 if (r) {
2289 *error = "dm_cache_metadata_all_clean() failed";
2290 goto bad;
2291 }
2292
2293 if (!all_clean) {
2294 *error = "Cannot enter passthrough mode unless all blocks are clean";
2295 r = -EINVAL;
2296 goto bad;
2297 }
2298 }
2299
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002300 spin_lock_init(&cache->lock);
2301 bio_list_init(&cache->deferred_bios);
2302 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002303 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002304 INIT_LIST_HEAD(&cache->quiesced_migrations);
2305 INIT_LIST_HEAD(&cache->completed_migrations);
2306 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002307 atomic_set(&cache->nr_migrations, 0);
2308 init_waitqueue_head(&cache->migration_wait);
2309
Joe Thornber66cb1912013-10-30 17:11:58 +00002310 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002311 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002312 atomic_set(&cache->quiescing_ack, 0);
2313
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002314 r = -ENOMEM;
Anssi Hannula44fa8162014-08-01 11:55:47 -04002315 atomic_set(&cache->nr_dirty, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002316 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2317 if (!cache->dirty_bitset) {
2318 *error = "could not allocate dirty bitset";
2319 goto bad;
2320 }
2321 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2322
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002323 cache->discard_block_size = cache->sectors_per_block;
2324 cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks);
2325 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002326 if (!cache->discard_bitset) {
2327 *error = "could not allocate discard bitset";
2328 goto bad;
2329 }
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002330 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002331
2332 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2333 if (IS_ERR(cache->copier)) {
2334 *error = "could not create kcopyd client";
2335 r = PTR_ERR(cache->copier);
2336 goto bad;
2337 }
2338
2339 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2340 if (!cache->wq) {
2341 *error = "could not create workqueue for metadata object";
2342 goto bad;
2343 }
2344 INIT_WORK(&cache->worker, do_worker);
2345 INIT_DELAYED_WORK(&cache->waker, do_waker);
2346 cache->last_commit_jiffies = jiffies;
2347
Joe Thornbera195db22014-10-06 16:30:06 -04002348 cache->prison = dm_bio_prison_create();
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002349 if (!cache->prison) {
2350 *error = "could not create bio prison";
2351 goto bad;
2352 }
2353
2354 cache->all_io_ds = dm_deferred_set_create();
2355 if (!cache->all_io_ds) {
2356 *error = "could not create all_io deferred set";
2357 goto bad;
2358 }
2359
2360 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2361 migration_cache);
2362 if (!cache->migration_pool) {
2363 *error = "Error creating cache's migration mempool";
2364 goto bad;
2365 }
2366
2367 cache->next_migration = NULL;
2368
2369 cache->need_tick_bio = true;
2370 cache->sized = false;
Joe Thornber65790ff2013-11-08 16:39:50 +00002371 cache->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002372 cache->commit_requested = false;
2373 cache->loaded_mappings = false;
2374 cache->loaded_discards = false;
2375
2376 load_stats(cache);
2377
2378 atomic_set(&cache->stats.demotion, 0);
2379 atomic_set(&cache->stats.promotion, 0);
2380 atomic_set(&cache->stats.copies_avoided, 0);
2381 atomic_set(&cache->stats.cache_cell_clash, 0);
2382 atomic_set(&cache->stats.commit_count, 0);
2383 atomic_set(&cache->stats.discard_count, 0);
2384
Joe Thornber65790ff2013-11-08 16:39:50 +00002385 spin_lock_init(&cache->invalidation_lock);
2386 INIT_LIST_HEAD(&cache->invalidation_requests);
2387
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002388 *result = cache;
2389 return 0;
2390
2391bad:
2392 destroy(cache);
2393 return r;
2394}
2395
2396static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2397{
2398 unsigned i;
2399 const char **copy;
2400
2401 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2402 if (!copy)
2403 return -ENOMEM;
2404 for (i = 0; i < argc; i++) {
2405 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2406 if (!copy[i]) {
2407 while (i--)
2408 kfree(copy[i]);
2409 kfree(copy);
2410 return -ENOMEM;
2411 }
2412 }
2413
2414 cache->nr_ctr_args = argc;
2415 cache->ctr_args = copy;
2416
2417 return 0;
2418}
2419
2420static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2421{
2422 int r = -EINVAL;
2423 struct cache_args *ca;
2424 struct cache *cache = NULL;
2425
2426 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2427 if (!ca) {
2428 ti->error = "Error allocating memory for cache";
2429 return -ENOMEM;
2430 }
2431 ca->ti = ti;
2432
2433 r = parse_cache_args(ca, argc, argv, &ti->error);
2434 if (r)
2435 goto out;
2436
2437 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002438 if (r)
2439 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002440
2441 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2442 if (r) {
2443 destroy(cache);
2444 goto out;
2445 }
2446
2447 ti->private = cache;
2448
2449out:
2450 destroy_cache_args(ca);
2451 return r;
2452}
2453
Joe Thornber8c081b52014-05-13 16:18:38 +01002454static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002455{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002456 int r;
2457 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002458 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002459 bool can_migrate = false;
2460 bool discarded_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002461 struct policy_result lookup_result;
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002462 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002463
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002464 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002465 /*
2466 * This can only occur if the io goes to a partial block at
2467 * the end of the origin device. We don't cache these.
2468 * Just remap to the origin and carry on.
2469 */
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002470 remap_to_origin(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002471 return DM_MAPIO_REMAPPED;
2472 }
2473
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002474 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2475 defer_bio(cache, bio);
2476 return DM_MAPIO_SUBMITTED;
2477 }
2478
2479 /*
2480 * Check to see if that block is currently migrating.
2481 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002482 *cell = alloc_prison_cell(cache);
2483 if (!*cell) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002484 defer_bio(cache, bio);
2485 return DM_MAPIO_SUBMITTED;
2486 }
2487
Joe Thornber8c081b52014-05-13 16:18:38 +01002488 r = bio_detain(cache, block, bio, *cell,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002489 (cell_free_fn) free_prison_cell,
Joe Thornber8c081b52014-05-13 16:18:38 +01002490 cache, cell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002491 if (r) {
2492 if (r < 0)
2493 defer_bio(cache, bio);
2494
2495 return DM_MAPIO_SUBMITTED;
2496 }
2497
2498 discarded_block = is_discarded_oblock(cache, block);
2499
2500 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2501 bio, &lookup_result);
2502 if (r == -EWOULDBLOCK) {
Joe Thornber8c081b52014-05-13 16:18:38 +01002503 cell_defer(cache, *cell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002504 return DM_MAPIO_SUBMITTED;
2505
2506 } else if (r) {
2507 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
Joe Thornber8c081b52014-05-13 16:18:38 +01002508 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002509 bio_io_error(bio);
2510 return DM_MAPIO_SUBMITTED;
2511 }
2512
Joe Thornber2ee57d52013-10-24 14:10:29 -04002513 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002514 switch (lookup_result.op) {
2515 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04002516 if (passthrough_mode(&cache->features)) {
2517 if (bio_data_dir(bio) == WRITE) {
2518 /*
2519 * We need to invalidate this block, so
2520 * defer for the worker thread.
2521 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002522 cell_defer(cache, *cell, true);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002523 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002524
Joe Thornber2ee57d52013-10-24 14:10:29 -04002525 } else {
Joe Thornber2ee57d52013-10-24 14:10:29 -04002526 inc_miss_counter(cache, bio);
2527 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002528 }
2529
2530 } else {
2531 inc_hit_counter(cache, bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002532 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2533 !is_dirty(cache, lookup_result.cblock))
2534 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2535 else
2536 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002537 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002538 break;
2539
2540 case POLICY_MISS:
2541 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002542 if (pb->req_nr != 0) {
2543 /*
2544 * This is a duplicate writethrough io that is no
2545 * longer needed because the block has been demoted.
2546 */
2547 bio_endio(bio, 0);
Joe Thornber8c081b52014-05-13 16:18:38 +01002548 cell_defer(cache, *cell, false);
2549 r = DM_MAPIO_SUBMITTED;
2550
2551 } else
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002552 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01002553
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002554 break;
2555
2556 default:
2557 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2558 (unsigned) lookup_result.op);
Joe Thornber8c081b52014-05-13 16:18:38 +01002559 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002560 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002561 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002562 }
2563
Joe Thornber2ee57d52013-10-24 14:10:29 -04002564 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002565}
2566
Joe Thornber8c081b52014-05-13 16:18:38 +01002567static int cache_map(struct dm_target *ti, struct bio *bio)
2568{
2569 int r;
2570 struct dm_bio_prison_cell *cell;
2571 struct cache *cache = ti->private;
2572
2573 r = __cache_map(cache, bio, &cell);
2574 if (r == DM_MAPIO_REMAPPED) {
2575 inc_ds(cache, bio, cell);
2576 cell_defer(cache, cell, false);
2577 }
2578
2579 return r;
2580}
2581
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002582static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2583{
2584 struct cache *cache = ti->private;
2585 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002586 size_t pb_data_size = get_per_bio_data_size(cache);
2587 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002588
2589 if (pb->tick) {
2590 policy_tick(cache->policy);
2591
2592 spin_lock_irqsave(&cache->lock, flags);
2593 cache->need_tick_bio = true;
2594 spin_unlock_irqrestore(&cache->lock, flags);
2595 }
2596
2597 check_for_quiesced_migrations(cache, pb);
2598
2599 return 0;
2600}
2601
2602static int write_dirty_bitset(struct cache *cache)
2603{
2604 unsigned i, r;
2605
2606 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2607 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2608 is_dirty(cache, to_cblock(i)));
2609 if (r)
2610 return r;
2611 }
2612
2613 return 0;
2614}
2615
2616static int write_discard_bitset(struct cache *cache)
2617{
2618 unsigned i, r;
2619
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002620 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2621 cache->discard_nr_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002622 if (r) {
2623 DMERR("could not resize on-disk discard bitset");
2624 return r;
2625 }
2626
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002627 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2628 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2629 is_discarded(cache, to_dblock(i)));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002630 if (r)
2631 return r;
2632 }
2633
2634 return 0;
2635}
2636
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002637/*
2638 * returns true on success
2639 */
2640static bool sync_metadata(struct cache *cache)
2641{
2642 int r1, r2, r3, r4;
2643
2644 r1 = write_dirty_bitset(cache);
2645 if (r1)
2646 DMERR("could not write dirty bitset");
2647
2648 r2 = write_discard_bitset(cache);
2649 if (r2)
2650 DMERR("could not write discard bitset");
2651
2652 save_stats(cache);
2653
Joe Thornber05966612014-04-03 16:16:44 +01002654 r3 = dm_cache_write_hints(cache->cmd, cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002655 if (r3)
2656 DMERR("could not write hints");
2657
2658 /*
2659 * If writing the above metadata failed, we still commit, but don't
2660 * set the clean shutdown flag. This will effectively force every
2661 * dirty bit to be set on reload.
2662 */
2663 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2664 if (r4)
2665 DMERR("could not write cache metadata. Data loss may occur.");
2666
2667 return !r1 && !r2 && !r3 && !r4;
2668}
2669
2670static void cache_postsuspend(struct dm_target *ti)
2671{
2672 struct cache *cache = ti->private;
2673
2674 start_quiescing(cache);
2675 wait_for_migrations(cache);
2676 stop_worker(cache);
2677 requeue_deferred_io(cache);
2678 stop_quiescing(cache);
2679
2680 (void) sync_metadata(cache);
2681}
2682
2683static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2684 bool dirty, uint32_t hint, bool hint_valid)
2685{
2686 int r;
2687 struct cache *cache = context;
2688
2689 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2690 if (r)
2691 return r;
2692
2693 if (dirty)
2694 set_dirty(cache, oblock, cblock);
2695 else
2696 clear_dirty(cache, oblock, cblock);
2697
2698 return 0;
2699}
2700
2701static int load_discard(void *context, sector_t discard_block_size,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002702 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002703{
2704 struct cache *cache = context;
2705
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002706 /* FIXME: handle mis-matched block size */
2707
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002708 if (discard)
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002709 set_discard(cache, dblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002710 else
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002711 clear_discard(cache, dblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002712
2713 return 0;
2714}
2715
Joe Thornberf494a9c2013-10-31 13:55:49 -04002716static dm_cblock_t get_cache_dev_size(struct cache *cache)
2717{
2718 sector_t size = get_dev_size(cache->cache_dev);
2719 (void) sector_div(size, cache->sectors_per_block);
2720 return to_cblock(size);
2721}
2722
2723static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2724{
2725 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2726 return true;
2727
2728 /*
2729 * We can't drop a dirty block when shrinking the cache.
2730 */
2731 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2732 new_size = to_cblock(from_cblock(new_size) + 1);
2733 if (is_dirty(cache, new_size)) {
2734 DMERR("unable to shrink cache; cache block %llu is dirty",
2735 (unsigned long long) from_cblock(new_size));
2736 return false;
2737 }
2738 }
2739
2740 return true;
2741}
2742
2743static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2744{
2745 int r;
2746
Vincent Pelletier08844802013-11-30 12:58:42 +01002747 r = dm_cache_resize(cache->cmd, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04002748 if (r) {
2749 DMERR("could not resize cache metadata");
2750 return r;
2751 }
2752
2753 cache->cache_size = new_size;
2754
2755 return 0;
2756}
2757
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002758static int cache_preresume(struct dm_target *ti)
2759{
2760 int r = 0;
2761 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002762 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002763
2764 /*
2765 * Check to see if the cache has resized.
2766 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04002767 if (!cache->sized) {
2768 r = resize_cache_dev(cache, csize);
2769 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002770 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002771
2772 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002773
2774 } else if (csize != cache->cache_size) {
2775 if (!can_resize(cache, csize))
2776 return -EINVAL;
2777
2778 r = resize_cache_dev(cache, csize);
2779 if (r)
2780 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002781 }
2782
2783 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002784 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002785 load_mapping, cache);
2786 if (r) {
2787 DMERR("could not load cache mappings");
2788 return r;
2789 }
2790
2791 cache->loaded_mappings = true;
2792 }
2793
2794 if (!cache->loaded_discards) {
2795 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2796 if (r) {
2797 DMERR("could not load origin discards");
2798 return r;
2799 }
2800
2801 cache->loaded_discards = true;
2802 }
2803
2804 return r;
2805}
2806
2807static void cache_resume(struct dm_target *ti)
2808{
2809 struct cache *cache = ti->private;
2810
2811 cache->need_tick_bio = true;
2812 do_waker(&cache->waker.work);
2813}
2814
2815/*
2816 * Status format:
2817 *
Mike Snitzer6a388612014-01-09 16:04:12 -05002818 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
2819 * <cache block size> <#used cache blocks>/<#total cache blocks>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002820 * <#read hits> <#read misses> <#write hits> <#write misses>
Mike Snitzer6a388612014-01-09 16:04:12 -05002821 * <#demotions> <#promotions> <#dirty>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002822 * <#features> <features>*
2823 * <#core args> <core args>
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002824 * <policy name> <#policy args> <policy args>*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002825 */
2826static void cache_status(struct dm_target *ti, status_type_t type,
2827 unsigned status_flags, char *result, unsigned maxlen)
2828{
2829 int r = 0;
2830 unsigned i;
2831 ssize_t sz = 0;
2832 dm_block_t nr_free_blocks_metadata = 0;
2833 dm_block_t nr_blocks_metadata = 0;
2834 char buf[BDEVNAME_SIZE];
2835 struct cache *cache = ti->private;
2836 dm_cblock_t residency;
2837
2838 switch (type) {
2839 case STATUSTYPE_INFO:
2840 /* Commit to ensure statistics aren't out-of-date */
2841 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2842 r = dm_cache_commit(cache->cmd, false);
2843 if (r)
2844 DMERR("could not commit metadata for accurate status");
2845 }
2846
2847 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2848 &nr_free_blocks_metadata);
2849 if (r) {
2850 DMERR("could not get metadata free block count");
2851 goto err;
2852 }
2853
2854 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2855 if (r) {
2856 DMERR("could not get metadata device size");
2857 goto err;
2858 }
2859
2860 residency = policy_residency(cache->policy);
2861
Anssi Hannula44fa8162014-08-01 11:55:47 -04002862 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
Mike Snitzer895b47d2014-07-14 15:37:18 -04002863 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002864 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2865 (unsigned long long)nr_blocks_metadata,
Mike Snitzer6a388612014-01-09 16:04:12 -05002866 cache->sectors_per_block,
2867 (unsigned long long) from_cblock(residency),
2868 (unsigned long long) from_cblock(cache->cache_size),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002869 (unsigned) atomic_read(&cache->stats.read_hit),
2870 (unsigned) atomic_read(&cache->stats.read_miss),
2871 (unsigned) atomic_read(&cache->stats.write_hit),
2872 (unsigned) atomic_read(&cache->stats.write_miss),
2873 (unsigned) atomic_read(&cache->stats.demotion),
2874 (unsigned) atomic_read(&cache->stats.promotion),
Anssi Hannula44fa8162014-08-01 11:55:47 -04002875 (unsigned long) atomic_read(&cache->nr_dirty));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002876
Joe Thornber2ee57d52013-10-24 14:10:29 -04002877 if (writethrough_mode(&cache->features))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002878 DMEMIT("1 writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04002879
2880 else if (passthrough_mode(&cache->features))
2881 DMEMIT("1 passthrough ");
2882
2883 else if (writeback_mode(&cache->features))
2884 DMEMIT("1 writeback ");
2885
2886 else {
2887 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2888 goto err;
2889 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002890
2891 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002892
2893 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002894 if (sz < maxlen) {
2895 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2896 if (r)
2897 DMERR("policy_emit_config_values returned %d", r);
2898 }
2899
2900 break;
2901
2902 case STATUSTYPE_TABLE:
2903 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2904 DMEMIT("%s ", buf);
2905 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2906 DMEMIT("%s ", buf);
2907 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2908 DMEMIT("%s", buf);
2909
2910 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2911 DMEMIT(" %s", cache->ctr_args[i]);
2912 if (cache->nr_ctr_args)
2913 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2914 }
2915
2916 return;
2917
2918err:
2919 DMEMIT("Error");
2920}
2921
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002922/*
Joe Thornber65790ff2013-11-08 16:39:50 +00002923 * A cache block range can take two forms:
2924 *
2925 * i) A single cblock, eg. '3456'
2926 * ii) A begin and end cblock with dots between, eg. 123-234
2927 */
2928static int parse_cblock_range(struct cache *cache, const char *str,
2929 struct cblock_range *result)
2930{
2931 char dummy;
2932 uint64_t b, e;
2933 int r;
2934
2935 /*
2936 * Try and parse form (ii) first.
2937 */
2938 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
2939 if (r < 0)
2940 return r;
2941
2942 if (r == 2) {
2943 result->begin = to_cblock(b);
2944 result->end = to_cblock(e);
2945 return 0;
2946 }
2947
2948 /*
2949 * That didn't work, try form (i).
2950 */
2951 r = sscanf(str, "%llu%c", &b, &dummy);
2952 if (r < 0)
2953 return r;
2954
2955 if (r == 1) {
2956 result->begin = to_cblock(b);
2957 result->end = to_cblock(from_cblock(result->begin) + 1u);
2958 return 0;
2959 }
2960
2961 DMERR("invalid cblock range '%s'", str);
2962 return -EINVAL;
2963}
2964
2965static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
2966{
2967 uint64_t b = from_cblock(range->begin);
2968 uint64_t e = from_cblock(range->end);
2969 uint64_t n = from_cblock(cache->cache_size);
2970
2971 if (b >= n) {
2972 DMERR("begin cblock out of range: %llu >= %llu", b, n);
2973 return -EINVAL;
2974 }
2975
2976 if (e > n) {
2977 DMERR("end cblock out of range: %llu > %llu", e, n);
2978 return -EINVAL;
2979 }
2980
2981 if (b >= e) {
2982 DMERR("invalid cblock range: %llu >= %llu", b, e);
2983 return -EINVAL;
2984 }
2985
2986 return 0;
2987}
2988
2989static int request_invalidation(struct cache *cache, struct cblock_range *range)
2990{
2991 struct invalidation_request req;
2992
2993 INIT_LIST_HEAD(&req.list);
2994 req.cblocks = range;
2995 atomic_set(&req.complete, 0);
2996 req.err = 0;
2997 init_waitqueue_head(&req.result_wait);
2998
2999 spin_lock(&cache->invalidation_lock);
3000 list_add(&req.list, &cache->invalidation_requests);
3001 spin_unlock(&cache->invalidation_lock);
3002 wake_worker(cache);
3003
3004 wait_event(req.result_wait, atomic_read(&req.complete));
3005 return req.err;
3006}
3007
3008static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
3009 const char **cblock_ranges)
3010{
3011 int r = 0;
3012 unsigned i;
3013 struct cblock_range range;
3014
3015 if (!passthrough_mode(&cache->features)) {
3016 DMERR("cache has to be in passthrough mode for invalidation");
3017 return -EPERM;
3018 }
3019
3020 for (i = 0; i < count; i++) {
3021 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3022 if (r)
3023 break;
3024
3025 r = validate_cblock_range(cache, &range);
3026 if (r)
3027 break;
3028
3029 /*
3030 * Pass begin and end origin blocks to the worker and wake it.
3031 */
3032 r = request_invalidation(cache, &range);
3033 if (r)
3034 break;
3035 }
3036
3037 return r;
3038}
3039
3040/*
3041 * Supports
3042 * "<key> <value>"
3043 * and
3044 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003045 *
3046 * The key migration_threshold is supported by the cache target core.
3047 */
3048static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3049{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003050 struct cache *cache = ti->private;
3051
Joe Thornber65790ff2013-11-08 16:39:50 +00003052 if (!argc)
3053 return -EINVAL;
3054
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -05003055 if (!strcasecmp(argv[0], "invalidate_cblocks"))
Joe Thornber65790ff2013-11-08 16:39:50 +00003056 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3057
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003058 if (argc != 2)
3059 return -EINVAL;
3060
Joe Thornber2f14f4b2013-05-10 14:37:21 +01003061 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003062}
3063
3064static int cache_iterate_devices(struct dm_target *ti,
3065 iterate_devices_callout_fn fn, void *data)
3066{
3067 int r = 0;
3068 struct cache *cache = ti->private;
3069
3070 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3071 if (!r)
3072 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3073
3074 return r;
3075}
3076
3077/*
3078 * We assume I/O is going to the origin (which is the volume
3079 * more likely to have restrictions e.g. by being striped).
3080 * (Looking up the exact location of the data would be expensive
3081 * and could always be out of date by the time the bio is submitted.)
3082 */
3083static int cache_bvec_merge(struct dm_target *ti,
3084 struct bvec_merge_data *bvm,
3085 struct bio_vec *biovec, int max_size)
3086{
3087 struct cache *cache = ti->private;
3088 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3089
3090 if (!q->merge_bvec_fn)
3091 return max_size;
3092
3093 bvm->bi_bdev = cache->origin_dev->bdev;
3094 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3095}
3096
3097static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3098{
3099 /*
3100 * FIXME: these limits may be incompatible with the cache device
3101 */
Joe Thornber1bad9bc2014-11-07 14:47:07 +00003102 limits->max_discard_sectors = cache->discard_block_size;
3103 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003104}
3105
3106static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3107{
3108 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04003109 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003110
Mike Snitzerf6109372013-08-20 15:02:41 -04003111 /*
3112 * If the system-determined stacked limits are compatible with the
3113 * cache's blocksize (io_opt is a factor) do not override them.
3114 */
3115 if (io_opt_sectors < cache->sectors_per_block ||
3116 do_div(io_opt_sectors, cache->sectors_per_block)) {
Mike Snitzerb0246532014-07-19 13:25:46 -04003117 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf6109372013-08-20 15:02:41 -04003118 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3119 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003120 set_discard_limits(cache, limits);
3121}
3122
3123/*----------------------------------------------------------------*/
3124
3125static struct target_type cache_target = {
3126 .name = "cache",
Joe Thornber8c081b52014-05-13 16:18:38 +01003127 .version = {1, 5, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003128 .module = THIS_MODULE,
3129 .ctr = cache_ctr,
3130 .dtr = cache_dtr,
3131 .map = cache_map,
3132 .end_io = cache_end_io,
3133 .postsuspend = cache_postsuspend,
3134 .preresume = cache_preresume,
3135 .resume = cache_resume,
3136 .status = cache_status,
3137 .message = cache_message,
3138 .iterate_devices = cache_iterate_devices,
3139 .merge = cache_bvec_merge,
3140 .io_hints = cache_io_hints,
3141};
3142
3143static int __init dm_cache_init(void)
3144{
3145 int r;
3146
3147 r = dm_register_target(&cache_target);
3148 if (r) {
3149 DMERR("cache target registration failed: %d", r);
3150 return r;
3151 }
3152
3153 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3154 if (!migration_cache) {
3155 dm_unregister_target(&cache_target);
3156 return -ENOMEM;
3157 }
3158
3159 return 0;
3160}
3161
3162static void __exit dm_cache_exit(void)
3163{
3164 dm_unregister_target(&cache_target);
3165 kmem_cache_destroy(migration_cache);
3166}
3167
3168module_init(dm_cache_init);
3169module_exit(dm_cache_exit);
3170
3171MODULE_DESCRIPTION(DM_NAME " cache target");
3172MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3173MODULE_LICENSE("GPL");