blob: ef842feda1015b51e2fd66e53ec4d7cfb42dde3e [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 Thornber08b18452014-11-06 14:38:01 +0000241 uint32_t discard_block_size; /* a power of 2 times sectors per block */
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;
Joe Thornber7ae34e72014-11-06 10:18:04 +0000313 bool discard:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000314 bool writeback:1;
315 bool demote:1;
316 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400317 bool requeue_holder:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000318 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000319
320 struct dm_bio_prison_cell *old_ocell;
321 struct dm_bio_prison_cell *new_ocell;
322};
323
324/*
325 * Processing a bio in the worker thread may require these memory
326 * allocations. We prealloc to avoid deadlocks (the same worker thread
327 * frees them back to the mempool).
328 */
329struct prealloc {
330 struct dm_cache_migration *mg;
331 struct dm_bio_prison_cell *cell1;
332 struct dm_bio_prison_cell *cell2;
333};
334
335static void wake_worker(struct cache *cache)
336{
337 queue_work(cache->wq, &cache->worker);
338}
339
340/*----------------------------------------------------------------*/
341
342static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
343{
344 /* FIXME: change to use a local slab. */
345 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
346}
347
348static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
349{
350 dm_bio_prison_free_cell(cache->prison, cell);
351}
352
353static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
354{
355 if (!p->mg) {
356 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
357 if (!p->mg)
358 return -ENOMEM;
359 }
360
361 if (!p->cell1) {
362 p->cell1 = alloc_prison_cell(cache);
363 if (!p->cell1)
364 return -ENOMEM;
365 }
366
367 if (!p->cell2) {
368 p->cell2 = alloc_prison_cell(cache);
369 if (!p->cell2)
370 return -ENOMEM;
371 }
372
373 return 0;
374}
375
376static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
377{
378 if (p->cell2)
379 free_prison_cell(cache, p->cell2);
380
381 if (p->cell1)
382 free_prison_cell(cache, p->cell1);
383
384 if (p->mg)
385 mempool_free(p->mg, cache->migration_pool);
386}
387
388static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
389{
390 struct dm_cache_migration *mg = p->mg;
391
392 BUG_ON(!mg);
393 p->mg = NULL;
394
395 return mg;
396}
397
398/*
399 * You must have a cell within the prealloc struct to return. If not this
400 * function will BUG() rather than returning NULL.
401 */
402static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
403{
404 struct dm_bio_prison_cell *r = NULL;
405
406 if (p->cell1) {
407 r = p->cell1;
408 p->cell1 = NULL;
409
410 } else if (p->cell2) {
411 r = p->cell2;
412 p->cell2 = NULL;
413 } else
414 BUG();
415
416 return r;
417}
418
419/*
420 * You can't have more than two cells in a prealloc struct. BUG() will be
421 * called if you try and overfill.
422 */
423static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
424{
425 if (!p->cell2)
426 p->cell2 = cell;
427
428 else if (!p->cell1)
429 p->cell1 = cell;
430
431 else
432 BUG();
433}
434
435/*----------------------------------------------------------------*/
436
Joe Thornber7ae34e72014-11-06 10:18:04 +0000437static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key *key)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000438{
439 key->virtual = 0;
440 key->dev = 0;
Joe Thornber7ae34e72014-11-06 10:18:04 +0000441 key->block_begin = from_oblock(begin);
442 key->block_end = from_oblock(end);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000443}
444
445/*
446 * The caller hands in a preallocated cell, and a free function for it.
447 * The cell will be freed if there's an error, or if it wasn't used because
448 * a cell with that key already exists.
449 */
450typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
451
Joe Thornber7ae34e72014-11-06 10:18:04 +0000452static int bio_detain_range(struct cache *cache, dm_oblock_t oblock_begin, dm_oblock_t oblock_end,
453 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
454 cell_free_fn free_fn, void *free_context,
455 struct dm_bio_prison_cell **cell_result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000456{
457 int r;
458 struct dm_cell_key key;
459
Joe Thornber7ae34e72014-11-06 10:18:04 +0000460 build_key(oblock_begin, oblock_end, &key);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000461 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
462 if (r)
463 free_fn(free_context, cell_prealloc);
464
465 return r;
466}
467
Joe Thornber7ae34e72014-11-06 10:18:04 +0000468static int bio_detain(struct cache *cache, dm_oblock_t oblock,
469 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
470 cell_free_fn free_fn, void *free_context,
471 struct dm_bio_prison_cell **cell_result)
472{
473 dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL);
474 return bio_detain_range(cache, oblock, end, bio,
475 cell_prealloc, free_fn, free_context, cell_result);
476}
477
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000478static int get_cell(struct cache *cache,
479 dm_oblock_t oblock,
480 struct prealloc *structs,
481 struct dm_bio_prison_cell **cell_result)
482{
483 int r;
484 struct dm_cell_key key;
485 struct dm_bio_prison_cell *cell_prealloc;
486
487 cell_prealloc = prealloc_get_cell(structs);
488
Joe Thornber7ae34e72014-11-06 10:18:04 +0000489 build_key(oblock, to_oblock(from_oblock(oblock) + 1ULL), &key);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000490 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
491 if (r)
492 prealloc_put_cell(structs, cell_prealloc);
493
494 return r;
495}
496
Joe Thornberaeed1422013-05-10 14:37:18 +0100497/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000498
499static bool is_dirty(struct cache *cache, dm_cblock_t b)
500{
501 return test_bit(from_cblock(b), cache->dirty_bitset);
502}
503
504static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
505{
506 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
Anssi Hannula44fa8162014-08-01 11:55:47 -0400507 atomic_inc(&cache->nr_dirty);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000508 policy_set_dirty(cache->policy, oblock);
509 }
510}
511
512static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
513{
514 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
515 policy_clear_dirty(cache->policy, oblock);
Anssi Hannula44fa8162014-08-01 11:55:47 -0400516 if (atomic_dec_return(&cache->nr_dirty) == 0)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000517 dm_table_event(cache->ti->table);
518 }
519}
520
521/*----------------------------------------------------------------*/
Joe Thornberaeed1422013-05-10 14:37:18 +0100522
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000523static bool block_size_is_power_of_two(struct cache *cache)
524{
525 return cache->sectors_per_block_shift >= 0;
526}
527
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100528/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
529#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
530__always_inline
531#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000532static dm_block_t block_div(dm_block_t b, uint32_t n)
533{
534 do_div(b, n);
535
536 return b;
537}
538
Joe Thornber7ae34e72014-11-06 10:18:04 +0000539static dm_block_t oblocks_per_dblock(struct cache *cache)
540{
541 dm_block_t oblocks = cache->discard_block_size;
542
543 if (block_size_is_power_of_two(cache))
544 oblocks >>= cache->sectors_per_block_shift;
545 else
546 oblocks = block_div(oblocks, cache->sectors_per_block);
547
548 return oblocks;
549}
550
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000551static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
552{
Joe Thornber7ae34e72014-11-06 10:18:04 +0000553 return to_dblock(block_div(from_oblock(oblock),
554 oblocks_per_dblock(cache)));
555}
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000556
Joe Thornber7ae34e72014-11-06 10:18:04 +0000557static dm_oblock_t dblock_to_oblock(struct cache *cache, dm_dblock_t dblock)
558{
559 return to_oblock(from_dblock(dblock) * oblocks_per_dblock(cache));
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000560}
561
562static void set_discard(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000563{
564 unsigned long flags;
565
Joe Thornber7ae34e72014-11-06 10:18:04 +0000566 BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000567 atomic_inc(&cache->stats.discard_count);
568
569 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000570 set_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000571 spin_unlock_irqrestore(&cache->lock, flags);
572}
573
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000574static void clear_discard(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000575{
576 unsigned long flags;
577
578 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000579 clear_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000580 spin_unlock_irqrestore(&cache->lock, flags);
581}
582
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000583static bool is_discarded(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000584{
585 int r;
586 unsigned long flags;
587
588 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000589 r = test_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000590 spin_unlock_irqrestore(&cache->lock, flags);
591
592 return r;
593}
594
595static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
596{
597 int r;
598 unsigned long flags;
599
600 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000601 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
602 cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000603 spin_unlock_irqrestore(&cache->lock, flags);
604
605 return r;
606}
607
608/*----------------------------------------------------------------*/
609
610static void load_stats(struct cache *cache)
611{
612 struct dm_cache_statistics stats;
613
614 dm_cache_metadata_get_stats(cache->cmd, &stats);
615 atomic_set(&cache->stats.read_hit, stats.read_hits);
616 atomic_set(&cache->stats.read_miss, stats.read_misses);
617 atomic_set(&cache->stats.write_hit, stats.write_hits);
618 atomic_set(&cache->stats.write_miss, stats.write_misses);
619}
620
621static void save_stats(struct cache *cache)
622{
623 struct dm_cache_statistics stats;
624
625 stats.read_hits = atomic_read(&cache->stats.read_hit);
626 stats.read_misses = atomic_read(&cache->stats.read_miss);
627 stats.write_hits = atomic_read(&cache->stats.write_hit);
628 stats.write_misses = atomic_read(&cache->stats.write_miss);
629
630 dm_cache_metadata_set_stats(cache->cmd, &stats);
631}
632
633/*----------------------------------------------------------------
634 * Per bio data
635 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100636
637/*
638 * If using writeback, leave out struct per_bio_data's writethrough fields.
639 */
640#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
641#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
642
Joe Thornber2ee57d52013-10-24 14:10:29 -0400643static bool writethrough_mode(struct cache_features *f)
644{
645 return f->io_mode == CM_IO_WRITETHROUGH;
646}
647
648static bool writeback_mode(struct cache_features *f)
649{
650 return f->io_mode == CM_IO_WRITEBACK;
651}
652
653static bool passthrough_mode(struct cache_features *f)
654{
655 return f->io_mode == CM_IO_PASSTHROUGH;
656}
657
Mike Snitzer19b00922013-04-05 15:36:34 +0100658static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000659{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400660 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100661}
662
663static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
664{
665 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000666 BUG_ON(!pb);
667 return pb;
668}
669
Mike Snitzer19b00922013-04-05 15:36:34 +0100670static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000671{
Mike Snitzer19b00922013-04-05 15:36:34 +0100672 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000673
674 pb->tick = false;
675 pb->req_nr = dm_bio_get_target_bio_nr(bio);
676 pb->all_io_entry = NULL;
677
678 return pb;
679}
680
681/*----------------------------------------------------------------
682 * Remapping
683 *--------------------------------------------------------------*/
684static void remap_to_origin(struct cache *cache, struct bio *bio)
685{
686 bio->bi_bdev = cache->origin_dev->bdev;
687}
688
689static void remap_to_cache(struct cache *cache, struct bio *bio,
690 dm_cblock_t cblock)
691{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700692 sector_t bi_sector = bio->bi_iter.bi_sector;
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100693 sector_t block = from_cblock(cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000694
695 bio->bi_bdev = cache->cache_dev->bdev;
696 if (!block_size_is_power_of_two(cache))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700697 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100698 (block * cache->sectors_per_block) +
Kent Overstreet4f024f32013-10-11 15:44:27 -0700699 sector_div(bi_sector, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000700 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700701 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100702 (block << cache->sectors_per_block_shift) |
Kent Overstreet4f024f32013-10-11 15:44:27 -0700703 (bi_sector & (cache->sectors_per_block - 1));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000704}
705
706static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
707{
708 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100709 size_t pb_data_size = get_per_bio_data_size(cache);
710 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000711
712 spin_lock_irqsave(&cache->lock, flags);
713 if (cache->need_tick_bio &&
714 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
715 pb->tick = true;
716 cache->need_tick_bio = false;
717 }
718 spin_unlock_irqrestore(&cache->lock, flags);
719}
720
721static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
722 dm_oblock_t oblock)
723{
724 check_if_tick_bio_needed(cache, bio);
725 remap_to_origin(cache, bio);
726 if (bio_data_dir(bio) == WRITE)
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000727 clear_discard(cache, oblock_to_dblock(cache, oblock));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000728}
729
730static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
731 dm_oblock_t oblock, dm_cblock_t cblock)
732{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100733 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000734 remap_to_cache(cache, bio, cblock);
735 if (bio_data_dir(bio) == WRITE) {
736 set_dirty(cache, oblock, cblock);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000737 clear_discard(cache, oblock_to_dblock(cache, oblock));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000738 }
739}
740
741static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
742{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700743 sector_t block_nr = bio->bi_iter.bi_sector;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000744
745 if (!block_size_is_power_of_two(cache))
746 (void) sector_div(block_nr, cache->sectors_per_block);
747 else
748 block_nr >>= cache->sectors_per_block_shift;
749
750 return to_oblock(block_nr);
751}
752
753static int bio_triggers_commit(struct cache *cache, struct bio *bio)
754{
755 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
756}
757
Joe Thornber8c081b52014-05-13 16:18:38 +0100758/*
759 * You must increment the deferred set whilst the prison cell is held. To
760 * encourage this, we ask for 'cell' to be passed in.
761 */
762static void inc_ds(struct cache *cache, struct bio *bio,
763 struct dm_bio_prison_cell *cell)
764{
765 size_t pb_data_size = get_per_bio_data_size(cache);
766 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
767
768 BUG_ON(!cell);
769 BUG_ON(pb->all_io_entry);
770
771 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
772}
773
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000774static void issue(struct cache *cache, struct bio *bio)
775{
776 unsigned long flags;
777
778 if (!bio_triggers_commit(cache, bio)) {
779 generic_make_request(bio);
780 return;
781 }
782
783 /*
784 * Batch together any bios that trigger commits and then issue a
785 * single commit for them in do_worker().
786 */
787 spin_lock_irqsave(&cache->lock, flags);
788 cache->commit_requested = true;
789 bio_list_add(&cache->deferred_flush_bios, bio);
790 spin_unlock_irqrestore(&cache->lock, flags);
791}
792
Joe Thornber8c081b52014-05-13 16:18:38 +0100793static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
794{
795 inc_ds(cache, bio, cell);
796 issue(cache, bio);
797}
798
Joe Thornbere2e74d62013-03-20 17:21:27 +0000799static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
800{
801 unsigned long flags;
802
803 spin_lock_irqsave(&cache->lock, flags);
804 bio_list_add(&cache->deferred_writethrough_bios, bio);
805 spin_unlock_irqrestore(&cache->lock, flags);
806
807 wake_worker(cache);
808}
809
810static void writethrough_endio(struct bio *bio, int err)
811{
Mike Snitzer19b00922013-04-05 15:36:34 +0100812 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400813
814 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000815
816 if (err) {
817 bio_endio(bio, err);
818 return;
819 }
820
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100821 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000822 remap_to_cache(pb->cache, bio, pb->cblock);
823
824 /*
825 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed1422013-05-10 14:37:18 +0100826 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000827 * worker thread.
828 */
829 defer_writethrough_bio(pb->cache, bio);
830}
831
832/*
833 * When running in writethrough mode we need to send writes to clean blocks
834 * to both the cache and origin devices. In future we'd like to clone the
835 * bio and send them in parallel, but for now we're doing them in
836 * series as this is easier.
837 */
838static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
839 dm_oblock_t oblock, dm_cblock_t cblock)
840{
Mike Snitzer19b00922013-04-05 15:36:34 +0100841 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000842
843 pb->cache = cache;
844 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400845 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100846 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000847
848 remap_to_origin_clear_discard(pb->cache, bio, oblock);
849}
850
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000851/*----------------------------------------------------------------
852 * Migration processing
853 *
854 * Migration covers moving data from the origin device to the cache, or
855 * vice versa.
856 *--------------------------------------------------------------*/
857static void free_migration(struct dm_cache_migration *mg)
858{
859 mempool_free(mg, mg->cache->migration_pool);
860}
861
862static void inc_nr_migrations(struct cache *cache)
863{
864 atomic_inc(&cache->nr_migrations);
865}
866
867static void dec_nr_migrations(struct cache *cache)
868{
869 atomic_dec(&cache->nr_migrations);
870
871 /*
872 * Wake the worker in case we're suspending the target.
873 */
874 wake_up(&cache->migration_wait);
875}
876
877static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
878 bool holder)
879{
880 (holder ? dm_cell_release : dm_cell_release_no_holder)
881 (cache->prison, cell, &cache->deferred_bios);
882 free_prison_cell(cache, cell);
883}
884
885static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
886 bool holder)
887{
888 unsigned long flags;
889
890 spin_lock_irqsave(&cache->lock, flags);
891 __cell_defer(cache, cell, holder);
892 spin_unlock_irqrestore(&cache->lock, flags);
893
894 wake_worker(cache);
895}
896
897static void cleanup_migration(struct dm_cache_migration *mg)
898{
Joe Thornber66cb1912013-10-30 17:11:58 +0000899 struct cache *cache = mg->cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000900 free_migration(mg);
Joe Thornber66cb1912013-10-30 17:11:58 +0000901 dec_nr_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000902}
903
904static void migration_failure(struct dm_cache_migration *mg)
905{
906 struct cache *cache = mg->cache;
907
908 if (mg->writeback) {
909 DMWARN_LIMIT("writeback failed; couldn't copy block");
910 set_dirty(cache, mg->old_oblock, mg->cblock);
911 cell_defer(cache, mg->old_ocell, false);
912
913 } else if (mg->demote) {
914 DMWARN_LIMIT("demotion failed; couldn't copy block");
915 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
916
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200917 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000918 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200919 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000920 } else {
921 DMWARN_LIMIT("promotion failed; couldn't copy block");
922 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200923 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000924 }
925
926 cleanup_migration(mg);
927}
928
929static void migration_success_pre_commit(struct dm_cache_migration *mg)
930{
931 unsigned long flags;
932 struct cache *cache = mg->cache;
933
934 if (mg->writeback) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000935 clear_dirty(cache, mg->old_oblock, mg->cblock);
Anssi Hannula40aa9782014-09-05 03:11:28 +0300936 cell_defer(cache, mg->old_ocell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000937 cleanup_migration(mg);
938 return;
939
940 } else if (mg->demote) {
941 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
942 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
943 policy_force_mapping(cache->policy, mg->new_oblock,
944 mg->old_oblock);
945 if (mg->promote)
946 cell_defer(cache, mg->new_ocell, true);
947 cleanup_migration(mg);
948 return;
949 }
950 } else {
951 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
952 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
953 policy_remove_mapping(cache->policy, mg->new_oblock);
954 cleanup_migration(mg);
955 return;
956 }
957 }
958
959 spin_lock_irqsave(&cache->lock, flags);
960 list_add_tail(&mg->list, &cache->need_commit_migrations);
961 cache->commit_requested = true;
962 spin_unlock_irqrestore(&cache->lock, flags);
963}
964
965static void migration_success_post_commit(struct dm_cache_migration *mg)
966{
967 unsigned long flags;
968 struct cache *cache = mg->cache;
969
970 if (mg->writeback) {
971 DMWARN("writeback unexpectedly triggered commit");
972 return;
973
974 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200975 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000976
977 if (mg->promote) {
978 mg->demote = false;
979
980 spin_lock_irqsave(&cache->lock, flags);
981 list_add_tail(&mg->list, &cache->quiesced_migrations);
982 spin_unlock_irqrestore(&cache->lock, flags);
983
Joe Thornber65790ff2013-11-08 16:39:50 +0000984 } else {
985 if (mg->invalidate)
986 policy_remove_mapping(cache->policy, mg->old_oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000987 cleanup_migration(mg);
Joe Thornber65790ff2013-11-08 16:39:50 +0000988 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000989
990 } else {
Anssi Hannula40aa9782014-09-05 03:11:28 +0300991 clear_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400992 if (mg->requeue_holder)
993 cell_defer(cache, mg->new_ocell, true);
994 else {
995 bio_endio(mg->new_ocell->holder, 0);
996 cell_defer(cache, mg->new_ocell, false);
997 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000998 cleanup_migration(mg);
999 }
1000}
1001
1002static void copy_complete(int read_err, unsigned long write_err, void *context)
1003{
1004 unsigned long flags;
1005 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
1006 struct cache *cache = mg->cache;
1007
1008 if (read_err || write_err)
1009 mg->err = true;
1010
1011 spin_lock_irqsave(&cache->lock, flags);
1012 list_add_tail(&mg->list, &cache->completed_migrations);
1013 spin_unlock_irqrestore(&cache->lock, flags);
1014
1015 wake_worker(cache);
1016}
1017
Joe Thornber7ae34e72014-11-06 10:18:04 +00001018static void issue_copy(struct dm_cache_migration *mg)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001019{
1020 int r;
1021 struct dm_io_region o_region, c_region;
1022 struct cache *cache = mg->cache;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001023 sector_t cblock = from_cblock(mg->cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001024
1025 o_region.bdev = cache->origin_dev->bdev;
1026 o_region.count = cache->sectors_per_block;
1027
1028 c_region.bdev = cache->cache_dev->bdev;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001029 c_region.sector = cblock * cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001030 c_region.count = cache->sectors_per_block;
1031
1032 if (mg->writeback || mg->demote) {
1033 /* demote */
1034 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
1035 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
1036 } else {
1037 /* promote */
1038 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1039 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1040 }
1041
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001042 if (r < 0) {
1043 DMERR_LIMIT("issuing migration failed");
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001044 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001045 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001046}
1047
Joe Thornberc9d28d52013-10-31 13:55:48 -04001048static void overwrite_endio(struct bio *bio, int err)
1049{
1050 struct dm_cache_migration *mg = bio->bi_private;
1051 struct cache *cache = mg->cache;
1052 size_t pb_data_size = get_per_bio_data_size(cache);
1053 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1054 unsigned long flags;
1055
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001056 dm_unhook_bio(&pb->hook_info, bio);
1057
Joe Thornberc9d28d52013-10-31 13:55:48 -04001058 if (err)
1059 mg->err = true;
1060
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001061 mg->requeue_holder = false;
1062
Joe Thornberc9d28d52013-10-31 13:55:48 -04001063 spin_lock_irqsave(&cache->lock, flags);
1064 list_add_tail(&mg->list, &cache->completed_migrations);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001065 spin_unlock_irqrestore(&cache->lock, flags);
1066
1067 wake_worker(cache);
1068}
1069
1070static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1071{
1072 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1073 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1074
1075 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1076 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001077
1078 /*
1079 * No need to inc_ds() here, since the cell will be held for the
1080 * duration of the io.
1081 */
Joe Thornberc9d28d52013-10-31 13:55:48 -04001082 generic_make_request(bio);
1083}
1084
1085static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1086{
1087 return (bio_data_dir(bio) == WRITE) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001088 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
Joe Thornberc9d28d52013-10-31 13:55:48 -04001089}
1090
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001091static void avoid_copy(struct dm_cache_migration *mg)
1092{
1093 atomic_inc(&mg->cache->stats.copies_avoided);
1094 migration_success_pre_commit(mg);
1095}
1096
Joe Thornber7ae34e72014-11-06 10:18:04 +00001097static void calc_discard_block_range(struct cache *cache, struct bio *bio,
1098 dm_dblock_t *b, dm_dblock_t *e)
1099{
1100 sector_t sb = bio->bi_iter.bi_sector;
1101 sector_t se = bio_end_sector(bio);
1102
1103 *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
1104
1105 if (se - sb < cache->discard_block_size)
1106 *e = *b;
1107 else
1108 *e = to_dblock(block_div(se, cache->discard_block_size));
1109}
1110
1111static void issue_discard(struct dm_cache_migration *mg)
1112{
1113 dm_dblock_t b, e;
1114 struct bio *bio = mg->new_ocell->holder;
1115
1116 calc_discard_block_range(mg->cache, bio, &b, &e);
1117 while (b != e) {
1118 set_discard(mg->cache, b);
1119 b = to_dblock(from_dblock(b) + 1);
1120 }
1121
1122 bio_endio(bio, 0);
1123 cell_defer(mg->cache, mg->new_ocell, false);
1124 free_migration(mg);
1125}
1126
1127static void issue_copy_or_discard(struct dm_cache_migration *mg)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001128{
1129 bool avoid;
1130 struct cache *cache = mg->cache;
1131
Joe Thornber7ae34e72014-11-06 10:18:04 +00001132 if (mg->discard) {
1133 issue_discard(mg);
1134 return;
1135 }
1136
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001137 if (mg->writeback || mg->demote)
1138 avoid = !is_dirty(cache, mg->cblock) ||
1139 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001140 else {
1141 struct bio *bio = mg->new_ocell->holder;
1142
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001143 avoid = is_discarded_oblock(cache, mg->new_oblock);
1144
Joe Thornberf29a3142014-11-27 12:21:08 +00001145 if (writeback_mode(&cache->features) &&
1146 !avoid && bio_writes_complete_block(cache, bio)) {
Joe Thornberc9d28d52013-10-31 13:55:48 -04001147 issue_overwrite(mg, bio);
1148 return;
1149 }
1150 }
1151
Joe Thornber7ae34e72014-11-06 10:18:04 +00001152 avoid ? avoid_copy(mg) : issue_copy(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001153}
1154
1155static void complete_migration(struct dm_cache_migration *mg)
1156{
1157 if (mg->err)
1158 migration_failure(mg);
1159 else
1160 migration_success_pre_commit(mg);
1161}
1162
1163static void process_migrations(struct cache *cache, struct list_head *head,
1164 void (*fn)(struct dm_cache_migration *))
1165{
1166 unsigned long flags;
1167 struct list_head list;
1168 struct dm_cache_migration *mg, *tmp;
1169
1170 INIT_LIST_HEAD(&list);
1171 spin_lock_irqsave(&cache->lock, flags);
1172 list_splice_init(head, &list);
1173 spin_unlock_irqrestore(&cache->lock, flags);
1174
1175 list_for_each_entry_safe(mg, tmp, &list, list)
1176 fn(mg);
1177}
1178
1179static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1180{
1181 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1182}
1183
1184static void queue_quiesced_migration(struct dm_cache_migration *mg)
1185{
1186 unsigned long flags;
1187 struct cache *cache = mg->cache;
1188
1189 spin_lock_irqsave(&cache->lock, flags);
1190 __queue_quiesced_migration(mg);
1191 spin_unlock_irqrestore(&cache->lock, flags);
1192
1193 wake_worker(cache);
1194}
1195
1196static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1197{
1198 unsigned long flags;
1199 struct dm_cache_migration *mg, *tmp;
1200
1201 spin_lock_irqsave(&cache->lock, flags);
1202 list_for_each_entry_safe(mg, tmp, work, list)
1203 __queue_quiesced_migration(mg);
1204 spin_unlock_irqrestore(&cache->lock, flags);
1205
1206 wake_worker(cache);
1207}
1208
1209static void check_for_quiesced_migrations(struct cache *cache,
1210 struct per_bio_data *pb)
1211{
1212 struct list_head work;
1213
1214 if (!pb->all_io_entry)
1215 return;
1216
1217 INIT_LIST_HEAD(&work);
Joe Thornber8c081b52014-05-13 16:18:38 +01001218 dm_deferred_entry_dec(pb->all_io_entry, &work);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001219
1220 if (!list_empty(&work))
1221 queue_quiesced_migrations(cache, &work);
1222}
1223
1224static void quiesce_migration(struct dm_cache_migration *mg)
1225{
1226 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1227 queue_quiesced_migration(mg);
1228}
1229
1230static void promote(struct cache *cache, struct prealloc *structs,
1231 dm_oblock_t oblock, dm_cblock_t cblock,
1232 struct dm_bio_prison_cell *cell)
1233{
1234 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1235
1236 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001237 mg->discard = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001238 mg->writeback = false;
1239 mg->demote = false;
1240 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001241 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001242 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001243 mg->cache = cache;
1244 mg->new_oblock = oblock;
1245 mg->cblock = cblock;
1246 mg->old_ocell = NULL;
1247 mg->new_ocell = cell;
1248 mg->start_jiffies = jiffies;
1249
1250 inc_nr_migrations(cache);
1251 quiesce_migration(mg);
1252}
1253
1254static void writeback(struct cache *cache, struct prealloc *structs,
1255 dm_oblock_t oblock, dm_cblock_t cblock,
1256 struct dm_bio_prison_cell *cell)
1257{
1258 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1259
1260 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001261 mg->discard = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001262 mg->writeback = true;
1263 mg->demote = false;
1264 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001265 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001266 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001267 mg->cache = cache;
1268 mg->old_oblock = oblock;
1269 mg->cblock = cblock;
1270 mg->old_ocell = cell;
1271 mg->new_ocell = NULL;
1272 mg->start_jiffies = jiffies;
1273
1274 inc_nr_migrations(cache);
1275 quiesce_migration(mg);
1276}
1277
1278static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1279 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1280 dm_cblock_t cblock,
1281 struct dm_bio_prison_cell *old_ocell,
1282 struct dm_bio_prison_cell *new_ocell)
1283{
1284 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1285
1286 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001287 mg->discard = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001288 mg->writeback = false;
1289 mg->demote = true;
1290 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001291 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001292 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001293 mg->cache = cache;
1294 mg->old_oblock = old_oblock;
1295 mg->new_oblock = new_oblock;
1296 mg->cblock = cblock;
1297 mg->old_ocell = old_ocell;
1298 mg->new_ocell = new_ocell;
1299 mg->start_jiffies = jiffies;
1300
1301 inc_nr_migrations(cache);
1302 quiesce_migration(mg);
1303}
1304
Joe Thornber2ee57d52013-10-24 14:10:29 -04001305/*
1306 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1307 * block are thrown away.
1308 */
1309static void invalidate(struct cache *cache, struct prealloc *structs,
1310 dm_oblock_t oblock, dm_cblock_t cblock,
1311 struct dm_bio_prison_cell *cell)
1312{
1313 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1314
1315 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001316 mg->discard = false;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001317 mg->writeback = false;
1318 mg->demote = true;
1319 mg->promote = false;
1320 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001321 mg->invalidate = true;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001322 mg->cache = cache;
1323 mg->old_oblock = oblock;
1324 mg->cblock = cblock;
1325 mg->old_ocell = cell;
1326 mg->new_ocell = NULL;
1327 mg->start_jiffies = jiffies;
1328
1329 inc_nr_migrations(cache);
1330 quiesce_migration(mg);
1331}
1332
Joe Thornber7ae34e72014-11-06 10:18:04 +00001333static void discard(struct cache *cache, struct prealloc *structs,
1334 struct dm_bio_prison_cell *cell)
1335{
1336 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1337
1338 mg->err = false;
1339 mg->discard = true;
1340 mg->writeback = false;
1341 mg->demote = false;
1342 mg->promote = false;
1343 mg->requeue_holder = false;
1344 mg->invalidate = false;
1345 mg->cache = cache;
1346 mg->old_ocell = NULL;
1347 mg->new_ocell = cell;
1348 mg->start_jiffies = jiffies;
1349
1350 quiesce_migration(mg);
1351}
1352
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001353/*----------------------------------------------------------------
1354 * bio processing
1355 *--------------------------------------------------------------*/
1356static void defer_bio(struct cache *cache, struct bio *bio)
1357{
1358 unsigned long flags;
1359
1360 spin_lock_irqsave(&cache->lock, flags);
1361 bio_list_add(&cache->deferred_bios, bio);
1362 spin_unlock_irqrestore(&cache->lock, flags);
1363
1364 wake_worker(cache);
1365}
1366
1367static void process_flush_bio(struct cache *cache, struct bio *bio)
1368{
Mike Snitzer19b00922013-04-05 15:36:34 +01001369 size_t pb_data_size = get_per_bio_data_size(cache);
1370 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001371
Kent Overstreet4f024f32013-10-11 15:44:27 -07001372 BUG_ON(bio->bi_iter.bi_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001373 if (!pb->req_nr)
1374 remap_to_origin(cache, bio);
1375 else
1376 remap_to_cache(cache, bio, 0);
1377
Joe Thornber8c081b52014-05-13 16:18:38 +01001378 /*
1379 * REQ_FLUSH is not directed at any particular block so we don't
1380 * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH
1381 * by dm-core.
1382 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001383 issue(cache, bio);
1384}
1385
Joe Thornber7ae34e72014-11-06 10:18:04 +00001386static void process_discard_bio(struct cache *cache, struct prealloc *structs,
1387 struct bio *bio)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001388{
Joe Thornber7ae34e72014-11-06 10:18:04 +00001389 int r;
1390 dm_dblock_t b, e;
1391 struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001392
Joe Thornber7ae34e72014-11-06 10:18:04 +00001393 calc_discard_block_range(cache, bio, &b, &e);
1394 if (b == e) {
1395 bio_endio(bio, 0);
1396 return;
1397 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001398
Joe Thornber7ae34e72014-11-06 10:18:04 +00001399 cell_prealloc = prealloc_get_cell(structs);
1400 r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc,
1401 (cell_free_fn) prealloc_put_cell,
1402 structs, &new_ocell);
1403 if (r > 0)
1404 return;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001405
Joe Thornber7ae34e72014-11-06 10:18:04 +00001406 discard(cache, structs, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001407}
1408
1409static bool spare_migration_bandwidth(struct cache *cache)
1410{
1411 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1412 cache->sectors_per_block;
1413 return current_volume < cache->migration_threshold;
1414}
1415
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001416static void inc_hit_counter(struct cache *cache, struct bio *bio)
1417{
1418 atomic_inc(bio_data_dir(bio) == READ ?
1419 &cache->stats.read_hit : &cache->stats.write_hit);
1420}
1421
1422static void inc_miss_counter(struct cache *cache, struct bio *bio)
1423{
1424 atomic_inc(bio_data_dir(bio) == READ ?
1425 &cache->stats.read_miss : &cache->stats.write_miss);
1426}
1427
1428static void process_bio(struct cache *cache, struct prealloc *structs,
1429 struct bio *bio)
1430{
1431 int r;
1432 bool release_cell = true;
1433 dm_oblock_t block = get_bio_block(cache, bio);
1434 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1435 struct policy_result lookup_result;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001436 bool passthrough = passthrough_mode(&cache->features);
Joe Thornber43c32bf2014-11-25 13:14:57 +00001437 bool discarded_block, can_migrate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001438
1439 /*
1440 * Check to see if that block is currently migrating.
1441 */
1442 cell_prealloc = prealloc_get_cell(structs);
1443 r = bio_detain(cache, block, bio, cell_prealloc,
1444 (cell_free_fn) prealloc_put_cell,
1445 structs, &new_ocell);
1446 if (r > 0)
1447 return;
1448
Joe Thornber43c32bf2014-11-25 13:14:57 +00001449 discarded_block = is_discarded_oblock(cache, block);
1450 can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
1451
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001452 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1453 bio, &lookup_result);
1454
1455 if (r == -EWOULDBLOCK)
1456 /* migration has been denied */
1457 lookup_result.op = POLICY_MISS;
1458
1459 switch (lookup_result.op) {
1460 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001461 if (passthrough) {
1462 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001463
Joe Thornber2ee57d52013-10-24 14:10:29 -04001464 /*
1465 * Passthrough always maps to the origin,
1466 * invalidating any cache blocks that are written
1467 * to.
1468 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001469
Joe Thornber2ee57d52013-10-24 14:10:29 -04001470 if (bio_data_dir(bio) == WRITE) {
1471 atomic_inc(&cache->stats.demotion);
1472 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1473 release_cell = false;
1474
1475 } else {
1476 /* FIXME: factor out issue_origin() */
Joe Thornber2ee57d52013-10-24 14:10:29 -04001477 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001478 inc_and_issue(cache, bio, new_ocell);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001479 }
1480 } else {
1481 inc_hit_counter(cache, bio);
1482
1483 if (bio_data_dir(bio) == WRITE &&
1484 writethrough_mode(&cache->features) &&
1485 !is_dirty(cache, lookup_result.cblock)) {
Joe Thornber2ee57d52013-10-24 14:10:29 -04001486 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001487 inc_and_issue(cache, bio, new_ocell);
1488
1489 } else {
1490 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1491 inc_and_issue(cache, bio, new_ocell);
1492 }
Joe Thornber2ee57d52013-10-24 14:10:29 -04001493 }
1494
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001495 break;
1496
1497 case POLICY_MISS:
1498 inc_miss_counter(cache, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001499 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001500 inc_and_issue(cache, bio, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001501 break;
1502
1503 case POLICY_NEW:
1504 atomic_inc(&cache->stats.promotion);
1505 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1506 release_cell = false;
1507 break;
1508
1509 case POLICY_REPLACE:
1510 cell_prealloc = prealloc_get_cell(structs);
1511 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1512 (cell_free_fn) prealloc_put_cell,
1513 structs, &old_ocell);
1514 if (r > 0) {
1515 /*
1516 * We have to be careful to avoid lock inversion of
1517 * the cells. So we back off, and wait for the
1518 * old_ocell to become free.
1519 */
1520 policy_force_mapping(cache->policy, block,
1521 lookup_result.old_oblock);
1522 atomic_inc(&cache->stats.cache_cell_clash);
1523 break;
1524 }
1525 atomic_inc(&cache->stats.demotion);
1526 atomic_inc(&cache->stats.promotion);
1527
1528 demote_then_promote(cache, structs, lookup_result.old_oblock,
1529 block, lookup_result.cblock,
1530 old_ocell, new_ocell);
1531 release_cell = false;
1532 break;
1533
1534 default:
1535 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1536 (unsigned) lookup_result.op);
1537 bio_io_error(bio);
1538 }
1539
1540 if (release_cell)
1541 cell_defer(cache, new_ocell, false);
1542}
1543
1544static int need_commit_due_to_time(struct cache *cache)
1545{
1546 return jiffies < cache->last_commit_jiffies ||
1547 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1548}
1549
1550static int commit_if_needed(struct cache *cache)
1551{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001552 int r = 0;
1553
1554 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1555 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001556 atomic_inc(&cache->stats.commit_count);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001557 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001558 r = dm_cache_commit(cache->cmd, false);
1559 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001560 }
1561
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001562 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001563}
1564
1565static void process_deferred_bios(struct cache *cache)
1566{
1567 unsigned long flags;
1568 struct bio_list bios;
1569 struct bio *bio;
1570 struct prealloc structs;
1571
1572 memset(&structs, 0, sizeof(structs));
1573 bio_list_init(&bios);
1574
1575 spin_lock_irqsave(&cache->lock, flags);
1576 bio_list_merge(&bios, &cache->deferred_bios);
1577 bio_list_init(&cache->deferred_bios);
1578 spin_unlock_irqrestore(&cache->lock, flags);
1579
1580 while (!bio_list_empty(&bios)) {
1581 /*
1582 * If we've got no free migration structs, and processing
1583 * this bio might require one, we pause until there are some
1584 * prepared mappings to process.
1585 */
1586 if (prealloc_data_structs(cache, &structs)) {
1587 spin_lock_irqsave(&cache->lock, flags);
1588 bio_list_merge(&cache->deferred_bios, &bios);
1589 spin_unlock_irqrestore(&cache->lock, flags);
1590 break;
1591 }
1592
1593 bio = bio_list_pop(&bios);
1594
1595 if (bio->bi_rw & REQ_FLUSH)
1596 process_flush_bio(cache, bio);
1597 else if (bio->bi_rw & REQ_DISCARD)
Joe Thornber7ae34e72014-11-06 10:18:04 +00001598 process_discard_bio(cache, &structs, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001599 else
1600 process_bio(cache, &structs, bio);
1601 }
1602
1603 prealloc_free_structs(cache, &structs);
1604}
1605
1606static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1607{
1608 unsigned long flags;
1609 struct bio_list bios;
1610 struct bio *bio;
1611
1612 bio_list_init(&bios);
1613
1614 spin_lock_irqsave(&cache->lock, flags);
1615 bio_list_merge(&bios, &cache->deferred_flush_bios);
1616 bio_list_init(&cache->deferred_flush_bios);
1617 spin_unlock_irqrestore(&cache->lock, flags);
1618
Joe Thornber8c081b52014-05-13 16:18:38 +01001619 /*
1620 * These bios have already been through inc_ds()
1621 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001622 while ((bio = bio_list_pop(&bios)))
1623 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1624}
1625
Joe Thornbere2e74d62013-03-20 17:21:27 +00001626static void process_deferred_writethrough_bios(struct cache *cache)
1627{
1628 unsigned long flags;
1629 struct bio_list bios;
1630 struct bio *bio;
1631
1632 bio_list_init(&bios);
1633
1634 spin_lock_irqsave(&cache->lock, flags);
1635 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1636 bio_list_init(&cache->deferred_writethrough_bios);
1637 spin_unlock_irqrestore(&cache->lock, flags);
1638
Joe Thornber8c081b52014-05-13 16:18:38 +01001639 /*
1640 * These bios have already been through inc_ds()
1641 */
Joe Thornbere2e74d62013-03-20 17:21:27 +00001642 while ((bio = bio_list_pop(&bios)))
1643 generic_make_request(bio);
1644}
1645
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001646static void writeback_some_dirty_blocks(struct cache *cache)
1647{
1648 int r = 0;
1649 dm_oblock_t oblock;
1650 dm_cblock_t cblock;
1651 struct prealloc structs;
1652 struct dm_bio_prison_cell *old_ocell;
1653
1654 memset(&structs, 0, sizeof(structs));
1655
1656 while (spare_migration_bandwidth(cache)) {
1657 if (prealloc_data_structs(cache, &structs))
1658 break;
1659
1660 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1661 if (r)
1662 break;
1663
1664 r = get_cell(cache, oblock, &structs, &old_ocell);
1665 if (r) {
1666 policy_set_dirty(cache->policy, oblock);
1667 break;
1668 }
1669
1670 writeback(cache, &structs, oblock, cblock, old_ocell);
1671 }
1672
1673 prealloc_free_structs(cache, &structs);
1674}
1675
1676/*----------------------------------------------------------------
Joe Thornber65790ff2013-11-08 16:39:50 +00001677 * Invalidations.
1678 * Dropping something from the cache *without* writing back.
1679 *--------------------------------------------------------------*/
1680
1681static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1682{
1683 int r = 0;
1684 uint64_t begin = from_cblock(req->cblocks->begin);
1685 uint64_t end = from_cblock(req->cblocks->end);
1686
1687 while (begin != end) {
1688 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1689 if (!r) {
1690 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1691 if (r)
1692 break;
1693
1694 } else if (r == -ENODATA) {
1695 /* harmless, already unmapped */
1696 r = 0;
1697
1698 } else {
1699 DMERR("policy_remove_cblock failed");
1700 break;
1701 }
1702
1703 begin++;
1704 }
1705
1706 cache->commit_requested = true;
1707
1708 req->err = r;
1709 atomic_set(&req->complete, 1);
1710
1711 wake_up(&req->result_wait);
1712}
1713
1714static void process_invalidation_requests(struct cache *cache)
1715{
1716 struct list_head list;
1717 struct invalidation_request *req, *tmp;
1718
1719 INIT_LIST_HEAD(&list);
1720 spin_lock(&cache->invalidation_lock);
1721 list_splice_init(&cache->invalidation_requests, &list);
1722 spin_unlock(&cache->invalidation_lock);
1723
1724 list_for_each_entry_safe (req, tmp, &list, list)
1725 process_invalidation_request(cache, req);
1726}
1727
1728/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001729 * Main worker loop
1730 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001731static bool is_quiescing(struct cache *cache)
1732{
Joe Thornber238f8362013-10-30 17:29:30 +00001733 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001734}
1735
Joe Thornber66cb1912013-10-30 17:11:58 +00001736static void ack_quiescing(struct cache *cache)
1737{
1738 if (is_quiescing(cache)) {
1739 atomic_inc(&cache->quiescing_ack);
1740 wake_up(&cache->quiescing_wait);
1741 }
1742}
1743
1744static void wait_for_quiescing_ack(struct cache *cache)
1745{
1746 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1747}
1748
1749static void start_quiescing(struct cache *cache)
1750{
Joe Thornber238f8362013-10-30 17:29:30 +00001751 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00001752 wait_for_quiescing_ack(cache);
1753}
1754
1755static void stop_quiescing(struct cache *cache)
1756{
Joe Thornber238f8362013-10-30 17:29:30 +00001757 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00001758 atomic_set(&cache->quiescing_ack, 0);
1759}
1760
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001761static void wait_for_migrations(struct cache *cache)
1762{
1763 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1764}
1765
1766static void stop_worker(struct cache *cache)
1767{
1768 cancel_delayed_work(&cache->waker);
1769 flush_workqueue(cache->wq);
1770}
1771
1772static void requeue_deferred_io(struct cache *cache)
1773{
1774 struct bio *bio;
1775 struct bio_list bios;
1776
1777 bio_list_init(&bios);
1778 bio_list_merge(&bios, &cache->deferred_bios);
1779 bio_list_init(&cache->deferred_bios);
1780
1781 while ((bio = bio_list_pop(&bios)))
1782 bio_endio(bio, DM_ENDIO_REQUEUE);
1783}
1784
1785static int more_work(struct cache *cache)
1786{
1787 if (is_quiescing(cache))
1788 return !list_empty(&cache->quiesced_migrations) ||
1789 !list_empty(&cache->completed_migrations) ||
1790 !list_empty(&cache->need_commit_migrations);
1791 else
1792 return !bio_list_empty(&cache->deferred_bios) ||
1793 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001794 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001795 !list_empty(&cache->quiesced_migrations) ||
1796 !list_empty(&cache->completed_migrations) ||
Joe Thornber65790ff2013-11-08 16:39:50 +00001797 !list_empty(&cache->need_commit_migrations) ||
1798 cache->invalidate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001799}
1800
1801static void do_worker(struct work_struct *ws)
1802{
1803 struct cache *cache = container_of(ws, struct cache, worker);
1804
1805 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001806 if (!is_quiescing(cache)) {
1807 writeback_some_dirty_blocks(cache);
1808 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001809 process_deferred_bios(cache);
Joe Thornber65790ff2013-11-08 16:39:50 +00001810 process_invalidation_requests(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001811 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001812
Joe Thornber7ae34e72014-11-06 10:18:04 +00001813 process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001814 process_migrations(cache, &cache->completed_migrations, complete_migration);
1815
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001816 if (commit_if_needed(cache)) {
1817 process_deferred_flush_bios(cache, false);
Joe Thornber304affa2014-06-24 15:36:58 -04001818 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001819
1820 /*
1821 * FIXME: rollback metadata or just go into a
1822 * failure mode and error everything
1823 */
1824 } else {
1825 process_deferred_flush_bios(cache, true);
1826 process_migrations(cache, &cache->need_commit_migrations,
1827 migration_success_post_commit);
1828 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001829
1830 ack_quiescing(cache);
1831
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001832 } while (more_work(cache));
1833}
1834
1835/*
1836 * We want to commit periodically so that not too much
1837 * unwritten metadata builds up.
1838 */
1839static void do_waker(struct work_struct *ws)
1840{
1841 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001842 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001843 wake_worker(cache);
1844 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1845}
1846
1847/*----------------------------------------------------------------*/
1848
1849static int is_congested(struct dm_dev *dev, int bdi_bits)
1850{
1851 struct request_queue *q = bdev_get_queue(dev->bdev);
1852 return bdi_congested(&q->backing_dev_info, bdi_bits);
1853}
1854
1855static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1856{
1857 struct cache *cache = container_of(cb, struct cache, callbacks);
1858
1859 return is_congested(cache->origin_dev, bdi_bits) ||
1860 is_congested(cache->cache_dev, bdi_bits);
1861}
1862
1863/*----------------------------------------------------------------
1864 * Target methods
1865 *--------------------------------------------------------------*/
1866
1867/*
1868 * This function gets called on the error paths of the constructor, so we
1869 * have to cope with a partially initialised struct.
1870 */
1871static void destroy(struct cache *cache)
1872{
1873 unsigned i;
1874
1875 if (cache->next_migration)
1876 mempool_free(cache->next_migration, cache->migration_pool);
1877
1878 if (cache->migration_pool)
1879 mempool_destroy(cache->migration_pool);
1880
1881 if (cache->all_io_ds)
1882 dm_deferred_set_destroy(cache->all_io_ds);
1883
1884 if (cache->prison)
1885 dm_bio_prison_destroy(cache->prison);
1886
1887 if (cache->wq)
1888 destroy_workqueue(cache->wq);
1889
1890 if (cache->dirty_bitset)
1891 free_bitset(cache->dirty_bitset);
1892
1893 if (cache->discard_bitset)
1894 free_bitset(cache->discard_bitset);
1895
1896 if (cache->copier)
1897 dm_kcopyd_client_destroy(cache->copier);
1898
1899 if (cache->cmd)
1900 dm_cache_metadata_close(cache->cmd);
1901
1902 if (cache->metadata_dev)
1903 dm_put_device(cache->ti, cache->metadata_dev);
1904
1905 if (cache->origin_dev)
1906 dm_put_device(cache->ti, cache->origin_dev);
1907
1908 if (cache->cache_dev)
1909 dm_put_device(cache->ti, cache->cache_dev);
1910
1911 if (cache->policy)
1912 dm_cache_policy_destroy(cache->policy);
1913
1914 for (i = 0; i < cache->nr_ctr_args ; i++)
1915 kfree(cache->ctr_args[i]);
1916 kfree(cache->ctr_args);
1917
1918 kfree(cache);
1919}
1920
1921static void cache_dtr(struct dm_target *ti)
1922{
1923 struct cache *cache = ti->private;
1924
1925 destroy(cache);
1926}
1927
1928static sector_t get_dev_size(struct dm_dev *dev)
1929{
1930 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1931}
1932
1933/*----------------------------------------------------------------*/
1934
1935/*
1936 * Construct a cache device mapping.
1937 *
1938 * cache <metadata dev> <cache dev> <origin dev> <block size>
1939 * <#feature args> [<feature arg>]*
1940 * <policy> <#policy args> [<policy arg>]*
1941 *
1942 * metadata dev : fast device holding the persistent metadata
1943 * cache dev : fast device holding cached data blocks
1944 * origin dev : slow device holding original data blocks
1945 * block size : cache unit size in sectors
1946 *
1947 * #feature args : number of feature arguments passed
1948 * feature args : writethrough. (The default is writeback.)
1949 *
1950 * policy : the replacement policy to use
1951 * #policy args : an even number of policy arguments corresponding
1952 * to key/value pairs passed to the policy
1953 * policy args : key/value pairs passed to the policy
1954 * E.g. 'sequential_threshold 1024'
1955 * See cache-policies.txt for details.
1956 *
1957 * Optional feature arguments are:
1958 * writethrough : write through caching that prohibits cache block
1959 * content from being different from origin block content.
1960 * Without this argument, the default behaviour is to write
1961 * back cache block contents later for performance reasons,
1962 * so they may differ from the corresponding origin blocks.
1963 */
1964struct cache_args {
1965 struct dm_target *ti;
1966
1967 struct dm_dev *metadata_dev;
1968
1969 struct dm_dev *cache_dev;
1970 sector_t cache_sectors;
1971
1972 struct dm_dev *origin_dev;
1973 sector_t origin_sectors;
1974
1975 uint32_t block_size;
1976
1977 const char *policy_name;
1978 int policy_argc;
1979 const char **policy_argv;
1980
1981 struct cache_features features;
1982};
1983
1984static void destroy_cache_args(struct cache_args *ca)
1985{
1986 if (ca->metadata_dev)
1987 dm_put_device(ca->ti, ca->metadata_dev);
1988
1989 if (ca->cache_dev)
1990 dm_put_device(ca->ti, ca->cache_dev);
1991
1992 if (ca->origin_dev)
1993 dm_put_device(ca->ti, ca->origin_dev);
1994
1995 kfree(ca);
1996}
1997
1998static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1999{
2000 if (!as->argc) {
2001 *error = "Insufficient args";
2002 return false;
2003 }
2004
2005 return true;
2006}
2007
2008static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
2009 char **error)
2010{
2011 int r;
2012 sector_t metadata_dev_size;
2013 char b[BDEVNAME_SIZE];
2014
2015 if (!at_least_one_arg(as, error))
2016 return -EINVAL;
2017
2018 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2019 &ca->metadata_dev);
2020 if (r) {
2021 *error = "Error opening metadata device";
2022 return r;
2023 }
2024
2025 metadata_dev_size = get_dev_size(ca->metadata_dev);
2026 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
2027 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2028 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
2029
2030 return 0;
2031}
2032
2033static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
2034 char **error)
2035{
2036 int r;
2037
2038 if (!at_least_one_arg(as, error))
2039 return -EINVAL;
2040
2041 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2042 &ca->cache_dev);
2043 if (r) {
2044 *error = "Error opening cache device";
2045 return r;
2046 }
2047 ca->cache_sectors = get_dev_size(ca->cache_dev);
2048
2049 return 0;
2050}
2051
2052static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
2053 char **error)
2054{
2055 int r;
2056
2057 if (!at_least_one_arg(as, error))
2058 return -EINVAL;
2059
2060 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2061 &ca->origin_dev);
2062 if (r) {
2063 *error = "Error opening origin device";
2064 return r;
2065 }
2066
2067 ca->origin_sectors = get_dev_size(ca->origin_dev);
2068 if (ca->ti->len > ca->origin_sectors) {
2069 *error = "Device size larger than cached device";
2070 return -EINVAL;
2071 }
2072
2073 return 0;
2074}
2075
2076static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
2077 char **error)
2078{
Mike Snitzer05473042013-08-16 10:54:19 -04002079 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002080
2081 if (!at_least_one_arg(as, error))
2082 return -EINVAL;
2083
Mike Snitzer05473042013-08-16 10:54:19 -04002084 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
2085 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2086 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2087 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002088 *error = "Invalid data block size";
2089 return -EINVAL;
2090 }
2091
Mike Snitzer05473042013-08-16 10:54:19 -04002092 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002093 *error = "Data block size is larger than the cache device";
2094 return -EINVAL;
2095 }
2096
Mike Snitzer05473042013-08-16 10:54:19 -04002097 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002098
2099 return 0;
2100}
2101
2102static void init_features(struct cache_features *cf)
2103{
2104 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04002105 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002106}
2107
2108static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2109 char **error)
2110{
2111 static struct dm_arg _args[] = {
2112 {0, 1, "Invalid number of cache feature arguments"},
2113 };
2114
2115 int r;
2116 unsigned argc;
2117 const char *arg;
2118 struct cache_features *cf = &ca->features;
2119
2120 init_features(cf);
2121
2122 r = dm_read_arg_group(_args, as, &argc, error);
2123 if (r)
2124 return -EINVAL;
2125
2126 while (argc--) {
2127 arg = dm_shift_arg(as);
2128
2129 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002130 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002131
2132 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002133 cf->io_mode = CM_IO_WRITETHROUGH;
2134
2135 else if (!strcasecmp(arg, "passthrough"))
2136 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002137
2138 else {
2139 *error = "Unrecognised cache feature requested";
2140 return -EINVAL;
2141 }
2142 }
2143
2144 return 0;
2145}
2146
2147static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2148 char **error)
2149{
2150 static struct dm_arg _args[] = {
2151 {0, 1024, "Invalid number of policy arguments"},
2152 };
2153
2154 int r;
2155
2156 if (!at_least_one_arg(as, error))
2157 return -EINVAL;
2158
2159 ca->policy_name = dm_shift_arg(as);
2160
2161 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2162 if (r)
2163 return -EINVAL;
2164
2165 ca->policy_argv = (const char **)as->argv;
2166 dm_consume_args(as, ca->policy_argc);
2167
2168 return 0;
2169}
2170
2171static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2172 char **error)
2173{
2174 int r;
2175 struct dm_arg_set as;
2176
2177 as.argc = argc;
2178 as.argv = argv;
2179
2180 r = parse_metadata_dev(ca, &as, error);
2181 if (r)
2182 return r;
2183
2184 r = parse_cache_dev(ca, &as, error);
2185 if (r)
2186 return r;
2187
2188 r = parse_origin_dev(ca, &as, error);
2189 if (r)
2190 return r;
2191
2192 r = parse_block_size(ca, &as, error);
2193 if (r)
2194 return r;
2195
2196 r = parse_features(ca, &as, error);
2197 if (r)
2198 return r;
2199
2200 r = parse_policy(ca, &as, error);
2201 if (r)
2202 return r;
2203
2204 return 0;
2205}
2206
2207/*----------------------------------------------------------------*/
2208
2209static struct kmem_cache *migration_cache;
2210
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002211#define NOT_CORE_OPTION 1
2212
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002213static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002214{
2215 unsigned long tmp;
2216
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002217 if (!strcasecmp(key, "migration_threshold")) {
2218 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002219 return -EINVAL;
2220
2221 cache->migration_threshold = tmp;
2222 return 0;
2223 }
2224
2225 return NOT_CORE_OPTION;
2226}
2227
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002228static int set_config_value(struct cache *cache, const char *key, const char *value)
2229{
2230 int r = process_config_option(cache, key, value);
2231
2232 if (r == NOT_CORE_OPTION)
2233 r = policy_set_config_value(cache->policy, key, value);
2234
2235 if (r)
2236 DMWARN("bad config value for %s: %s", key, value);
2237
2238 return r;
2239}
2240
2241static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002242{
2243 int r = 0;
2244
2245 if (argc & 1) {
2246 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2247 return -EINVAL;
2248 }
2249
2250 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002251 r = set_config_value(cache, argv[0], argv[1]);
2252 if (r)
2253 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002254
2255 argc -= 2;
2256 argv += 2;
2257 }
2258
2259 return r;
2260}
2261
2262static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2263 char **error)
2264{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002265 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2266 cache->cache_size,
2267 cache->origin_sectors,
2268 cache->sectors_per_block);
2269 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002270 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002271 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002272 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002273 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002274
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002275 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002276}
2277
Joe Thornber08b18452014-11-06 14:38:01 +00002278/*
Joe Thornber2bb812d2014-11-26 16:07:50 +00002279 * We want the discard block size to be at least the size of the cache
2280 * block size and have no more than 2^14 discard blocks across the origin.
Joe Thornber08b18452014-11-06 14:38:01 +00002281 */
2282#define MAX_DISCARD_BLOCKS (1 << 14)
2283
2284static bool too_many_discard_blocks(sector_t discard_block_size,
2285 sector_t origin_size)
2286{
2287 (void) sector_div(origin_size, discard_block_size);
2288
2289 return origin_size > MAX_DISCARD_BLOCKS;
2290}
2291
2292static sector_t calculate_discard_block_size(sector_t cache_block_size,
2293 sector_t origin_size)
2294{
Joe Thornber2bb812d2014-11-26 16:07:50 +00002295 sector_t discard_block_size = cache_block_size;
Joe Thornber08b18452014-11-06 14:38:01 +00002296
2297 if (origin_size)
2298 while (too_many_discard_blocks(discard_block_size, origin_size))
2299 discard_block_size *= 2;
2300
2301 return discard_block_size;
2302}
2303
Joe Thornberd1d92202014-11-11 11:58:32 +00002304static void set_cache_size(struct cache *cache, dm_cblock_t size)
2305{
2306 dm_block_t nr_blocks = from_cblock(size);
2307
2308 if (nr_blocks > (1 << 20) && cache->cache_size != size)
2309 DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
2310 "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
2311 "Please consider increasing the cache block size to reduce the overall cache block count.",
2312 (unsigned long long) nr_blocks);
2313
2314 cache->cache_size = size;
2315}
2316
Joe Thornberf8350da2013-05-10 14:37:16 +01002317#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002318
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002319static int cache_create(struct cache_args *ca, struct cache **result)
2320{
2321 int r = 0;
2322 char **error = &ca->ti->error;
2323 struct cache *cache;
2324 struct dm_target *ti = ca->ti;
2325 dm_block_t origin_blocks;
2326 struct dm_cache_metadata *cmd;
2327 bool may_format = ca->features.mode == CM_WRITE;
2328
2329 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2330 if (!cache)
2331 return -ENOMEM;
2332
2333 cache->ti = ca->ti;
2334 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002335 ti->num_flush_bios = 2;
2336 ti->flush_supported = true;
2337
2338 ti->num_discard_bios = 1;
2339 ti->discards_supported = true;
2340 ti->discard_zeroes_data_unsupported = true;
Joe Thornber25726292014-11-24 14:05:16 +00002341 ti->split_discard_bios = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002342
Joe Thornber8c5008f2013-05-10 14:37:18 +01002343 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01002344 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002345
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002346 cache->callbacks.congested_fn = cache_is_congested;
2347 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2348
2349 cache->metadata_dev = ca->metadata_dev;
2350 cache->origin_dev = ca->origin_dev;
2351 cache->cache_dev = ca->cache_dev;
2352
2353 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2354
2355 /* FIXME: factor out this whole section */
2356 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002357 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002358 cache->origin_blocks = to_oblock(origin_blocks);
2359
2360 cache->sectors_per_block = ca->block_size;
2361 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2362 r = -EINVAL;
2363 goto bad;
2364 }
2365
2366 if (ca->block_size & (ca->block_size - 1)) {
2367 dm_block_t cache_size = ca->cache_sectors;
2368
2369 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002370 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberd1d92202014-11-11 11:58:32 +00002371 set_cache_size(cache, to_cblock(cache_size));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002372 } else {
2373 cache->sectors_per_block_shift = __ffs(ca->block_size);
Joe Thornberd1d92202014-11-11 11:58:32 +00002374 set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002375 }
2376
2377 r = create_cache_policy(cache, ca, error);
2378 if (r)
2379 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002380
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002381 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002382 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2383
2384 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2385 if (r) {
2386 *error = "Error setting cache policy's config values";
2387 goto bad;
2388 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002389
2390 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2391 ca->block_size, may_format,
2392 dm_cache_policy_get_hint_size(cache->policy));
2393 if (IS_ERR(cmd)) {
2394 *error = "Error creating metadata object";
2395 r = PTR_ERR(cmd);
2396 goto bad;
2397 }
2398 cache->cmd = cmd;
2399
Joe Thornber2ee57d52013-10-24 14:10:29 -04002400 if (passthrough_mode(&cache->features)) {
2401 bool all_clean;
2402
2403 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2404 if (r) {
2405 *error = "dm_cache_metadata_all_clean() failed";
2406 goto bad;
2407 }
2408
2409 if (!all_clean) {
2410 *error = "Cannot enter passthrough mode unless all blocks are clean";
2411 r = -EINVAL;
2412 goto bad;
2413 }
2414 }
2415
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002416 spin_lock_init(&cache->lock);
2417 bio_list_init(&cache->deferred_bios);
2418 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002419 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002420 INIT_LIST_HEAD(&cache->quiesced_migrations);
2421 INIT_LIST_HEAD(&cache->completed_migrations);
2422 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002423 atomic_set(&cache->nr_migrations, 0);
2424 init_waitqueue_head(&cache->migration_wait);
2425
Joe Thornber66cb1912013-10-30 17:11:58 +00002426 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002427 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002428 atomic_set(&cache->quiescing_ack, 0);
2429
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002430 r = -ENOMEM;
Anssi Hannula44fa8162014-08-01 11:55:47 -04002431 atomic_set(&cache->nr_dirty, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002432 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2433 if (!cache->dirty_bitset) {
2434 *error = "could not allocate dirty bitset";
2435 goto bad;
2436 }
2437 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2438
Joe Thornber08b18452014-11-06 14:38:01 +00002439 cache->discard_block_size =
2440 calculate_discard_block_size(cache->sectors_per_block,
2441 cache->origin_sectors);
Joe Thornber25726292014-11-24 14:05:16 +00002442 cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
2443 cache->discard_block_size));
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002444 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002445 if (!cache->discard_bitset) {
2446 *error = "could not allocate discard bitset";
2447 goto bad;
2448 }
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002449 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002450
2451 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2452 if (IS_ERR(cache->copier)) {
2453 *error = "could not create kcopyd client";
2454 r = PTR_ERR(cache->copier);
2455 goto bad;
2456 }
2457
2458 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2459 if (!cache->wq) {
2460 *error = "could not create workqueue for metadata object";
2461 goto bad;
2462 }
2463 INIT_WORK(&cache->worker, do_worker);
2464 INIT_DELAYED_WORK(&cache->waker, do_waker);
2465 cache->last_commit_jiffies = jiffies;
2466
Joe Thornbera195db22014-10-06 16:30:06 -04002467 cache->prison = dm_bio_prison_create();
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002468 if (!cache->prison) {
2469 *error = "could not create bio prison";
2470 goto bad;
2471 }
2472
2473 cache->all_io_ds = dm_deferred_set_create();
2474 if (!cache->all_io_ds) {
2475 *error = "could not create all_io deferred set";
2476 goto bad;
2477 }
2478
2479 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2480 migration_cache);
2481 if (!cache->migration_pool) {
2482 *error = "Error creating cache's migration mempool";
2483 goto bad;
2484 }
2485
2486 cache->next_migration = NULL;
2487
2488 cache->need_tick_bio = true;
2489 cache->sized = false;
Joe Thornber65790ff2013-11-08 16:39:50 +00002490 cache->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002491 cache->commit_requested = false;
2492 cache->loaded_mappings = false;
2493 cache->loaded_discards = false;
2494
2495 load_stats(cache);
2496
2497 atomic_set(&cache->stats.demotion, 0);
2498 atomic_set(&cache->stats.promotion, 0);
2499 atomic_set(&cache->stats.copies_avoided, 0);
2500 atomic_set(&cache->stats.cache_cell_clash, 0);
2501 atomic_set(&cache->stats.commit_count, 0);
2502 atomic_set(&cache->stats.discard_count, 0);
2503
Joe Thornber65790ff2013-11-08 16:39:50 +00002504 spin_lock_init(&cache->invalidation_lock);
2505 INIT_LIST_HEAD(&cache->invalidation_requests);
2506
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002507 *result = cache;
2508 return 0;
2509
2510bad:
2511 destroy(cache);
2512 return r;
2513}
2514
2515static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2516{
2517 unsigned i;
2518 const char **copy;
2519
2520 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2521 if (!copy)
2522 return -ENOMEM;
2523 for (i = 0; i < argc; i++) {
2524 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2525 if (!copy[i]) {
2526 while (i--)
2527 kfree(copy[i]);
2528 kfree(copy);
2529 return -ENOMEM;
2530 }
2531 }
2532
2533 cache->nr_ctr_args = argc;
2534 cache->ctr_args = copy;
2535
2536 return 0;
2537}
2538
2539static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2540{
2541 int r = -EINVAL;
2542 struct cache_args *ca;
2543 struct cache *cache = NULL;
2544
2545 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2546 if (!ca) {
2547 ti->error = "Error allocating memory for cache";
2548 return -ENOMEM;
2549 }
2550 ca->ti = ti;
2551
2552 r = parse_cache_args(ca, argc, argv, &ti->error);
2553 if (r)
2554 goto out;
2555
2556 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002557 if (r)
2558 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002559
2560 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2561 if (r) {
2562 destroy(cache);
2563 goto out;
2564 }
2565
2566 ti->private = cache;
2567
2568out:
2569 destroy_cache_args(ca);
2570 return r;
2571}
2572
Joe Thornber8c081b52014-05-13 16:18:38 +01002573static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002574{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002575 int r;
2576 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002577 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002578 bool can_migrate = false;
2579 bool discarded_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002580 struct policy_result lookup_result;
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002581 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002582
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002583 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002584 /*
2585 * This can only occur if the io goes to a partial block at
2586 * the end of the origin device. We don't cache these.
2587 * Just remap to the origin and carry on.
2588 */
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002589 remap_to_origin(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002590 return DM_MAPIO_REMAPPED;
2591 }
2592
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002593 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2594 defer_bio(cache, bio);
2595 return DM_MAPIO_SUBMITTED;
2596 }
2597
2598 /*
2599 * Check to see if that block is currently migrating.
2600 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002601 *cell = alloc_prison_cell(cache);
2602 if (!*cell) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002603 defer_bio(cache, bio);
2604 return DM_MAPIO_SUBMITTED;
2605 }
2606
Joe Thornber8c081b52014-05-13 16:18:38 +01002607 r = bio_detain(cache, block, bio, *cell,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002608 (cell_free_fn) free_prison_cell,
Joe Thornber8c081b52014-05-13 16:18:38 +01002609 cache, cell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002610 if (r) {
2611 if (r < 0)
2612 defer_bio(cache, bio);
2613
2614 return DM_MAPIO_SUBMITTED;
2615 }
2616
2617 discarded_block = is_discarded_oblock(cache, block);
2618
2619 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2620 bio, &lookup_result);
2621 if (r == -EWOULDBLOCK) {
Joe Thornber8c081b52014-05-13 16:18:38 +01002622 cell_defer(cache, *cell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002623 return DM_MAPIO_SUBMITTED;
2624
2625 } else if (r) {
2626 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
Joe Thornber8c081b52014-05-13 16:18:38 +01002627 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002628 bio_io_error(bio);
2629 return DM_MAPIO_SUBMITTED;
2630 }
2631
Joe Thornber2ee57d52013-10-24 14:10:29 -04002632 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002633 switch (lookup_result.op) {
2634 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04002635 if (passthrough_mode(&cache->features)) {
2636 if (bio_data_dir(bio) == WRITE) {
2637 /*
2638 * We need to invalidate this block, so
2639 * defer for the worker thread.
2640 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002641 cell_defer(cache, *cell, true);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002642 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002643
Joe Thornber2ee57d52013-10-24 14:10:29 -04002644 } else {
Joe Thornber2ee57d52013-10-24 14:10:29 -04002645 inc_miss_counter(cache, bio);
2646 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002647 }
2648
2649 } else {
2650 inc_hit_counter(cache, bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002651 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2652 !is_dirty(cache, lookup_result.cblock))
2653 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2654 else
2655 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002656 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002657 break;
2658
2659 case POLICY_MISS:
2660 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002661 if (pb->req_nr != 0) {
2662 /*
2663 * This is a duplicate writethrough io that is no
2664 * longer needed because the block has been demoted.
2665 */
2666 bio_endio(bio, 0);
Joe Thornber8c081b52014-05-13 16:18:38 +01002667 cell_defer(cache, *cell, false);
2668 r = DM_MAPIO_SUBMITTED;
2669
2670 } else
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002671 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01002672
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002673 break;
2674
2675 default:
2676 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2677 (unsigned) lookup_result.op);
Joe Thornber8c081b52014-05-13 16:18:38 +01002678 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002679 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002680 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002681 }
2682
Joe Thornber2ee57d52013-10-24 14:10:29 -04002683 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002684}
2685
Joe Thornber8c081b52014-05-13 16:18:38 +01002686static int cache_map(struct dm_target *ti, struct bio *bio)
2687{
2688 int r;
2689 struct dm_bio_prison_cell *cell;
2690 struct cache *cache = ti->private;
2691
2692 r = __cache_map(cache, bio, &cell);
2693 if (r == DM_MAPIO_REMAPPED) {
2694 inc_ds(cache, bio, cell);
2695 cell_defer(cache, cell, false);
2696 }
2697
2698 return r;
2699}
2700
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002701static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2702{
2703 struct cache *cache = ti->private;
2704 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002705 size_t pb_data_size = get_per_bio_data_size(cache);
2706 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002707
2708 if (pb->tick) {
2709 policy_tick(cache->policy);
2710
2711 spin_lock_irqsave(&cache->lock, flags);
2712 cache->need_tick_bio = true;
2713 spin_unlock_irqrestore(&cache->lock, flags);
2714 }
2715
2716 check_for_quiesced_migrations(cache, pb);
2717
2718 return 0;
2719}
2720
2721static int write_dirty_bitset(struct cache *cache)
2722{
2723 unsigned i, r;
2724
2725 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2726 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2727 is_dirty(cache, to_cblock(i)));
2728 if (r)
2729 return r;
2730 }
2731
2732 return 0;
2733}
2734
2735static int write_discard_bitset(struct cache *cache)
2736{
2737 unsigned i, r;
2738
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002739 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
2740 cache->discard_nr_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002741 if (r) {
2742 DMERR("could not resize on-disk discard bitset");
2743 return r;
2744 }
2745
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002746 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
2747 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
2748 is_discarded(cache, to_dblock(i)));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002749 if (r)
2750 return r;
2751 }
2752
2753 return 0;
2754}
2755
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002756/*
2757 * returns true on success
2758 */
2759static bool sync_metadata(struct cache *cache)
2760{
2761 int r1, r2, r3, r4;
2762
2763 r1 = write_dirty_bitset(cache);
2764 if (r1)
2765 DMERR("could not write dirty bitset");
2766
2767 r2 = write_discard_bitset(cache);
2768 if (r2)
2769 DMERR("could not write discard bitset");
2770
2771 save_stats(cache);
2772
Joe Thornber05966612014-04-03 16:16:44 +01002773 r3 = dm_cache_write_hints(cache->cmd, cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002774 if (r3)
2775 DMERR("could not write hints");
2776
2777 /*
2778 * If writing the above metadata failed, we still commit, but don't
2779 * set the clean shutdown flag. This will effectively force every
2780 * dirty bit to be set on reload.
2781 */
2782 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2783 if (r4)
2784 DMERR("could not write cache metadata. Data loss may occur.");
2785
2786 return !r1 && !r2 && !r3 && !r4;
2787}
2788
2789static void cache_postsuspend(struct dm_target *ti)
2790{
2791 struct cache *cache = ti->private;
2792
2793 start_quiescing(cache);
2794 wait_for_migrations(cache);
2795 stop_worker(cache);
2796 requeue_deferred_io(cache);
2797 stop_quiescing(cache);
2798
2799 (void) sync_metadata(cache);
2800}
2801
2802static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2803 bool dirty, uint32_t hint, bool hint_valid)
2804{
2805 int r;
2806 struct cache *cache = context;
2807
2808 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2809 if (r)
2810 return r;
2811
2812 if (dirty)
2813 set_dirty(cache, oblock, cblock);
2814 else
2815 clear_dirty(cache, oblock, cblock);
2816
2817 return 0;
2818}
2819
Joe Thornber3e2e1c32014-11-24 14:06:22 +00002820/*
2821 * The discard block size in the on disk metadata is not
2822 * neccessarily the same as we're currently using. So we have to
2823 * be careful to only set the discarded attribute if we know it
2824 * covers a complete block of the new size.
2825 */
2826struct discard_load_info {
2827 struct cache *cache;
2828
2829 /*
2830 * These blocks are sized using the on disk dblock size, rather
2831 * than the current one.
2832 */
2833 dm_block_t block_size;
2834 dm_block_t discard_begin, discard_end;
2835};
2836
2837static void discard_load_info_init(struct cache *cache,
2838 struct discard_load_info *li)
2839{
2840 li->cache = cache;
2841 li->discard_begin = li->discard_end = 0;
2842}
2843
2844static void set_discard_range(struct discard_load_info *li)
2845{
2846 sector_t b, e;
2847
2848 if (li->discard_begin == li->discard_end)
2849 return;
2850
2851 /*
2852 * Convert to sectors.
2853 */
2854 b = li->discard_begin * li->block_size;
2855 e = li->discard_end * li->block_size;
2856
2857 /*
2858 * Then convert back to the current dblock size.
2859 */
2860 b = dm_sector_div_up(b, li->cache->discard_block_size);
2861 sector_div(e, li->cache->discard_block_size);
2862
2863 /*
2864 * The origin may have shrunk, so we need to check we're still in
2865 * bounds.
2866 */
2867 if (e > from_dblock(li->cache->discard_nr_blocks))
2868 e = from_dblock(li->cache->discard_nr_blocks);
2869
2870 for (; b < e; b++)
2871 set_discard(li->cache, to_dblock(b));
2872}
2873
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002874static int load_discard(void *context, sector_t discard_block_size,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002875 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002876{
Joe Thornber3e2e1c32014-11-24 14:06:22 +00002877 struct discard_load_info *li = context;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002878
Joe Thornber3e2e1c32014-11-24 14:06:22 +00002879 li->block_size = discard_block_size;
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002880
Joe Thornber3e2e1c32014-11-24 14:06:22 +00002881 if (discard) {
2882 if (from_dblock(dblock) == li->discard_end)
2883 /*
2884 * We're already in a discard range, just extend it.
2885 */
2886 li->discard_end = li->discard_end + 1ULL;
2887
2888 else {
2889 /*
2890 * Emit the old range and start a new one.
2891 */
2892 set_discard_range(li);
2893 li->discard_begin = from_dblock(dblock);
2894 li->discard_end = li->discard_begin + 1ULL;
2895 }
2896 } else {
2897 set_discard_range(li);
2898 li->discard_begin = li->discard_end = 0;
2899 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002900
2901 return 0;
2902}
2903
Joe Thornberf494a9c2013-10-31 13:55:49 -04002904static dm_cblock_t get_cache_dev_size(struct cache *cache)
2905{
2906 sector_t size = get_dev_size(cache->cache_dev);
2907 (void) sector_div(size, cache->sectors_per_block);
2908 return to_cblock(size);
2909}
2910
2911static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2912{
2913 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2914 return true;
2915
2916 /*
2917 * We can't drop a dirty block when shrinking the cache.
2918 */
2919 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2920 new_size = to_cblock(from_cblock(new_size) + 1);
2921 if (is_dirty(cache, new_size)) {
2922 DMERR("unable to shrink cache; cache block %llu is dirty",
2923 (unsigned long long) from_cblock(new_size));
2924 return false;
2925 }
2926 }
2927
2928 return true;
2929}
2930
2931static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2932{
2933 int r;
2934
Vincent Pelletier08844802013-11-30 12:58:42 +01002935 r = dm_cache_resize(cache->cmd, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04002936 if (r) {
2937 DMERR("could not resize cache metadata");
2938 return r;
2939 }
2940
Joe Thornberd1d92202014-11-11 11:58:32 +00002941 set_cache_size(cache, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04002942
2943 return 0;
2944}
2945
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002946static int cache_preresume(struct dm_target *ti)
2947{
2948 int r = 0;
2949 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002950 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002951
2952 /*
2953 * Check to see if the cache has resized.
2954 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04002955 if (!cache->sized) {
2956 r = resize_cache_dev(cache, csize);
2957 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002958 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002959
2960 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002961
2962 } else if (csize != cache->cache_size) {
2963 if (!can_resize(cache, csize))
2964 return -EINVAL;
2965
2966 r = resize_cache_dev(cache, csize);
2967 if (r)
2968 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002969 }
2970
2971 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002972 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002973 load_mapping, cache);
2974 if (r) {
2975 DMERR("could not load cache mappings");
2976 return r;
2977 }
2978
2979 cache->loaded_mappings = true;
2980 }
2981
2982 if (!cache->loaded_discards) {
Joe Thornber3e2e1c32014-11-24 14:06:22 +00002983 struct discard_load_info li;
2984
2985 /*
2986 * The discard bitset could have been resized, or the
2987 * discard block size changed. To be safe we start by
2988 * setting every dblock to not discarded.
2989 */
2990 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
2991
2992 discard_load_info_init(cache, &li);
2993 r = dm_cache_load_discards(cache->cmd, load_discard, &li);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002994 if (r) {
2995 DMERR("could not load origin discards");
2996 return r;
2997 }
Joe Thornber3e2e1c32014-11-24 14:06:22 +00002998 set_discard_range(&li);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002999
3000 cache->loaded_discards = true;
3001 }
3002
3003 return r;
3004}
3005
3006static void cache_resume(struct dm_target *ti)
3007{
3008 struct cache *cache = ti->private;
3009
3010 cache->need_tick_bio = true;
3011 do_waker(&cache->waker.work);
3012}
3013
3014/*
3015 * Status format:
3016 *
Mike Snitzer6a388612014-01-09 16:04:12 -05003017 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
3018 * <cache block size> <#used cache blocks>/<#total cache blocks>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003019 * <#read hits> <#read misses> <#write hits> <#write misses>
Mike Snitzer6a388612014-01-09 16:04:12 -05003020 * <#demotions> <#promotions> <#dirty>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003021 * <#features> <features>*
3022 * <#core args> <core args>
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05003023 * <policy name> <#policy args> <policy args>*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003024 */
3025static void cache_status(struct dm_target *ti, status_type_t type,
3026 unsigned status_flags, char *result, unsigned maxlen)
3027{
3028 int r = 0;
3029 unsigned i;
3030 ssize_t sz = 0;
3031 dm_block_t nr_free_blocks_metadata = 0;
3032 dm_block_t nr_blocks_metadata = 0;
3033 char buf[BDEVNAME_SIZE];
3034 struct cache *cache = ti->private;
3035 dm_cblock_t residency;
3036
3037 switch (type) {
3038 case STATUSTYPE_INFO:
3039 /* Commit to ensure statistics aren't out-of-date */
3040 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
3041 r = dm_cache_commit(cache->cmd, false);
3042 if (r)
3043 DMERR("could not commit metadata for accurate status");
3044 }
3045
3046 r = dm_cache_get_free_metadata_block_count(cache->cmd,
3047 &nr_free_blocks_metadata);
3048 if (r) {
3049 DMERR("could not get metadata free block count");
3050 goto err;
3051 }
3052
3053 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
3054 if (r) {
3055 DMERR("could not get metadata device size");
3056 goto err;
3057 }
3058
3059 residency = policy_residency(cache->policy);
3060
Anssi Hannula44fa8162014-08-01 11:55:47 -04003061 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
Mike Snitzer895b47d2014-07-14 15:37:18 -04003062 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003063 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3064 (unsigned long long)nr_blocks_metadata,
Mike Snitzer6a388612014-01-09 16:04:12 -05003065 cache->sectors_per_block,
3066 (unsigned long long) from_cblock(residency),
3067 (unsigned long long) from_cblock(cache->cache_size),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003068 (unsigned) atomic_read(&cache->stats.read_hit),
3069 (unsigned) atomic_read(&cache->stats.read_miss),
3070 (unsigned) atomic_read(&cache->stats.write_hit),
3071 (unsigned) atomic_read(&cache->stats.write_miss),
3072 (unsigned) atomic_read(&cache->stats.demotion),
3073 (unsigned) atomic_read(&cache->stats.promotion),
Anssi Hannula44fa8162014-08-01 11:55:47 -04003074 (unsigned long) atomic_read(&cache->nr_dirty));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003075
Joe Thornber2ee57d52013-10-24 14:10:29 -04003076 if (writethrough_mode(&cache->features))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003077 DMEMIT("1 writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04003078
3079 else if (passthrough_mode(&cache->features))
3080 DMEMIT("1 passthrough ");
3081
3082 else if (writeback_mode(&cache->features))
3083 DMEMIT("1 writeback ");
3084
3085 else {
3086 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
3087 goto err;
3088 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003089
3090 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05003091
3092 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003093 if (sz < maxlen) {
3094 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
3095 if (r)
3096 DMERR("policy_emit_config_values returned %d", r);
3097 }
3098
3099 break;
3100
3101 case STATUSTYPE_TABLE:
3102 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
3103 DMEMIT("%s ", buf);
3104 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
3105 DMEMIT("%s ", buf);
3106 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
3107 DMEMIT("%s", buf);
3108
3109 for (i = 0; i < cache->nr_ctr_args - 1; i++)
3110 DMEMIT(" %s", cache->ctr_args[i]);
3111 if (cache->nr_ctr_args)
3112 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
3113 }
3114
3115 return;
3116
3117err:
3118 DMEMIT("Error");
3119}
3120
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003121/*
Joe Thornber65790ff2013-11-08 16:39:50 +00003122 * A cache block range can take two forms:
3123 *
3124 * i) A single cblock, eg. '3456'
3125 * ii) A begin and end cblock with dots between, eg. 123-234
3126 */
3127static int parse_cblock_range(struct cache *cache, const char *str,
3128 struct cblock_range *result)
3129{
3130 char dummy;
3131 uint64_t b, e;
3132 int r;
3133
3134 /*
3135 * Try and parse form (ii) first.
3136 */
3137 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
3138 if (r < 0)
3139 return r;
3140
3141 if (r == 2) {
3142 result->begin = to_cblock(b);
3143 result->end = to_cblock(e);
3144 return 0;
3145 }
3146
3147 /*
3148 * That didn't work, try form (i).
3149 */
3150 r = sscanf(str, "%llu%c", &b, &dummy);
3151 if (r < 0)
3152 return r;
3153
3154 if (r == 1) {
3155 result->begin = to_cblock(b);
3156 result->end = to_cblock(from_cblock(result->begin) + 1u);
3157 return 0;
3158 }
3159
3160 DMERR("invalid cblock range '%s'", str);
3161 return -EINVAL;
3162}
3163
3164static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
3165{
3166 uint64_t b = from_cblock(range->begin);
3167 uint64_t e = from_cblock(range->end);
3168 uint64_t n = from_cblock(cache->cache_size);
3169
3170 if (b >= n) {
3171 DMERR("begin cblock out of range: %llu >= %llu", b, n);
3172 return -EINVAL;
3173 }
3174
3175 if (e > n) {
3176 DMERR("end cblock out of range: %llu > %llu", e, n);
3177 return -EINVAL;
3178 }
3179
3180 if (b >= e) {
3181 DMERR("invalid cblock range: %llu >= %llu", b, e);
3182 return -EINVAL;
3183 }
3184
3185 return 0;
3186}
3187
3188static int request_invalidation(struct cache *cache, struct cblock_range *range)
3189{
3190 struct invalidation_request req;
3191
3192 INIT_LIST_HEAD(&req.list);
3193 req.cblocks = range;
3194 atomic_set(&req.complete, 0);
3195 req.err = 0;
3196 init_waitqueue_head(&req.result_wait);
3197
3198 spin_lock(&cache->invalidation_lock);
3199 list_add(&req.list, &cache->invalidation_requests);
3200 spin_unlock(&cache->invalidation_lock);
3201 wake_worker(cache);
3202
3203 wait_event(req.result_wait, atomic_read(&req.complete));
3204 return req.err;
3205}
3206
3207static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
3208 const char **cblock_ranges)
3209{
3210 int r = 0;
3211 unsigned i;
3212 struct cblock_range range;
3213
3214 if (!passthrough_mode(&cache->features)) {
3215 DMERR("cache has to be in passthrough mode for invalidation");
3216 return -EPERM;
3217 }
3218
3219 for (i = 0; i < count; i++) {
3220 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3221 if (r)
3222 break;
3223
3224 r = validate_cblock_range(cache, &range);
3225 if (r)
3226 break;
3227
3228 /*
3229 * Pass begin and end origin blocks to the worker and wake it.
3230 */
3231 r = request_invalidation(cache, &range);
3232 if (r)
3233 break;
3234 }
3235
3236 return r;
3237}
3238
3239/*
3240 * Supports
3241 * "<key> <value>"
3242 * and
3243 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003244 *
3245 * The key migration_threshold is supported by the cache target core.
3246 */
3247static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3248{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003249 struct cache *cache = ti->private;
3250
Joe Thornber65790ff2013-11-08 16:39:50 +00003251 if (!argc)
3252 return -EINVAL;
3253
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -05003254 if (!strcasecmp(argv[0], "invalidate_cblocks"))
Joe Thornber65790ff2013-11-08 16:39:50 +00003255 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3256
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003257 if (argc != 2)
3258 return -EINVAL;
3259
Joe Thornber2f14f4b2013-05-10 14:37:21 +01003260 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003261}
3262
3263static int cache_iterate_devices(struct dm_target *ti,
3264 iterate_devices_callout_fn fn, void *data)
3265{
3266 int r = 0;
3267 struct cache *cache = ti->private;
3268
3269 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3270 if (!r)
3271 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3272
3273 return r;
3274}
3275
3276/*
3277 * We assume I/O is going to the origin (which is the volume
3278 * more likely to have restrictions e.g. by being striped).
3279 * (Looking up the exact location of the data would be expensive
3280 * and could always be out of date by the time the bio is submitted.)
3281 */
3282static int cache_bvec_merge(struct dm_target *ti,
3283 struct bvec_merge_data *bvm,
3284 struct bio_vec *biovec, int max_size)
3285{
3286 struct cache *cache = ti->private;
3287 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3288
3289 if (!q->merge_bvec_fn)
3290 return max_size;
3291
3292 bvm->bi_bdev = cache->origin_dev->bdev;
3293 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3294}
3295
3296static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3297{
3298 /*
3299 * FIXME: these limits may be incompatible with the cache device
3300 */
Joe Thornber7ae34e72014-11-06 10:18:04 +00003301 limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
3302 cache->origin_sectors);
Joe Thornber1bad9bc2014-11-07 14:47:07 +00003303 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003304}
3305
3306static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3307{
3308 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04003309 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003310
Mike Snitzerf6109372013-08-20 15:02:41 -04003311 /*
3312 * If the system-determined stacked limits are compatible with the
3313 * cache's blocksize (io_opt is a factor) do not override them.
3314 */
3315 if (io_opt_sectors < cache->sectors_per_block ||
3316 do_div(io_opt_sectors, cache->sectors_per_block)) {
Mike Snitzerb0246532014-07-19 13:25:46 -04003317 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf6109372013-08-20 15:02:41 -04003318 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3319 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003320 set_discard_limits(cache, limits);
3321}
3322
3323/*----------------------------------------------------------------*/
3324
3325static struct target_type cache_target = {
3326 .name = "cache",
Joe Thornber7ae34e72014-11-06 10:18:04 +00003327 .version = {1, 6, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003328 .module = THIS_MODULE,
3329 .ctr = cache_ctr,
3330 .dtr = cache_dtr,
3331 .map = cache_map,
3332 .end_io = cache_end_io,
3333 .postsuspend = cache_postsuspend,
3334 .preresume = cache_preresume,
3335 .resume = cache_resume,
3336 .status = cache_status,
3337 .message = cache_message,
3338 .iterate_devices = cache_iterate_devices,
3339 .merge = cache_bvec_merge,
3340 .io_hints = cache_io_hints,
3341};
3342
3343static int __init dm_cache_init(void)
3344{
3345 int r;
3346
3347 r = dm_register_target(&cache_target);
3348 if (r) {
3349 DMERR("cache target registration failed: %d", r);
3350 return r;
3351 }
3352
3353 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3354 if (!migration_cache) {
3355 dm_unregister_target(&cache_target);
3356 return -ENOMEM;
3357 }
3358
3359 return 0;
3360}
3361
3362static void __exit dm_cache_exit(void)
3363{
3364 dm_unregister_target(&cache_target);
3365 kmem_cache_destroy(migration_cache);
3366}
3367
3368module_init(dm_cache_init);
3369module_exit(dm_cache_exit);
3370
3371MODULE_DESCRIPTION(DM_NAME " cache target");
3372MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3373MODULE_LICENSE("GPL");