blob: 387b93d81138d4da4a7acead58f72582ca9a7e03 [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 PRISON_CELLS 1024
99#define MIGRATION_POOL_SIZE 128
100#define COMMIT_PERIOD HZ
101#define MIGRATION_COUNT_WINDOW 10
102
103/*
Mike Snitzer05473042013-08-16 10:54:19 -0400104 * The block size of the device holding cache data must be
105 * between 32KB and 1GB.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000106 */
107#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
Mike Snitzer05473042013-08-16 10:54:19 -0400108#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000109
110/*
111 * FIXME: the cache is read/write for the time being.
112 */
Joe Thornber2ee57d52013-10-24 14:10:29 -0400113enum cache_metadata_mode {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000114 CM_WRITE, /* metadata may be changed */
115 CM_READ_ONLY, /* metadata may not be changed */
116};
117
Joe Thornber2ee57d52013-10-24 14:10:29 -0400118enum cache_io_mode {
119 /*
120 * Data is written to cached blocks only. These blocks are marked
121 * dirty. If you lose the cache device you will lose data.
122 * Potential performance increase for both reads and writes.
123 */
124 CM_IO_WRITEBACK,
125
126 /*
127 * Data is written to both cache and origin. Blocks are never
128 * dirty. Potential performance benfit for reads only.
129 */
130 CM_IO_WRITETHROUGH,
131
132 /*
133 * A degraded mode useful for various cache coherency situations
134 * (eg, rolling back snapshots). Reads and writes always go to the
135 * origin. If a write goes to a cached oblock, then the cache
136 * block is invalidated.
137 */
138 CM_IO_PASSTHROUGH
139};
140
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000141struct cache_features {
Joe Thornber2ee57d52013-10-24 14:10:29 -0400142 enum cache_metadata_mode mode;
143 enum cache_io_mode io_mode;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000144};
145
146struct cache_stats {
147 atomic_t read_hit;
148 atomic_t read_miss;
149 atomic_t write_hit;
150 atomic_t write_miss;
151 atomic_t demotion;
152 atomic_t promotion;
153 atomic_t copies_avoided;
154 atomic_t cache_cell_clash;
155 atomic_t commit_count;
156 atomic_t discard_count;
157};
158
Joe Thornber65790ff2013-11-08 16:39:50 +0000159/*
160 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
161 * the one-past-the-end value.
162 */
163struct cblock_range {
164 dm_cblock_t begin;
165 dm_cblock_t end;
166};
167
168struct invalidation_request {
169 struct list_head list;
170 struct cblock_range *cblocks;
171
172 atomic_t complete;
173 int err;
174
175 wait_queue_head_t result_wait;
176};
177
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000178struct cache {
179 struct dm_target *ti;
180 struct dm_target_callbacks callbacks;
181
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400182 struct dm_cache_metadata *cmd;
183
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000184 /*
185 * Metadata is written to this device.
186 */
187 struct dm_dev *metadata_dev;
188
189 /*
190 * The slower of the two data devices. Typically a spindle.
191 */
192 struct dm_dev *origin_dev;
193
194 /*
195 * The faster of the two data devices. Typically an SSD.
196 */
197 struct dm_dev *cache_dev;
198
199 /*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000200 * Size of the origin device in _complete_ blocks and native sectors.
201 */
202 dm_oblock_t origin_blocks;
203 sector_t origin_sectors;
204
205 /*
206 * Size of the cache device in blocks.
207 */
208 dm_cblock_t cache_size;
209
210 /*
211 * Fields for converting from sectors to blocks.
212 */
213 uint32_t sectors_per_block;
214 int sectors_per_block_shift;
215
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000216 spinlock_t lock;
217 struct bio_list deferred_bios;
218 struct bio_list deferred_flush_bios;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000219 struct bio_list deferred_writethrough_bios;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000220 struct list_head quiesced_migrations;
221 struct list_head completed_migrations;
222 struct list_head need_commit_migrations;
223 sector_t migration_threshold;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000224 wait_queue_head_t migration_wait;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400225 atomic_t nr_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000226
Joe Thornber66cb1912013-10-30 17:11:58 +0000227 wait_queue_head_t quiescing_wait;
Joe Thornber238f8362013-10-30 17:29:30 +0000228 atomic_t quiescing;
Joe Thornber66cb1912013-10-30 17:11:58 +0000229 atomic_t quiescing_ack;
230
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000231 /*
232 * cache_size entries, dirty if set
233 */
Anssi Hannula44fa8162014-08-01 11:55:47 -0400234 atomic_t nr_dirty;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000235 unsigned long *dirty_bitset;
236
237 /*
238 * origin_blocks entries, discarded if set.
239 */
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400240 dm_oblock_t discard_nr_blocks;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000241 unsigned long *discard_bitset;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400242
243 /*
244 * Rather than reconstructing the table line for the status we just
245 * save it and regurgitate.
246 */
247 unsigned nr_ctr_args;
248 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000249
250 struct dm_kcopyd_client *copier;
251 struct workqueue_struct *wq;
252 struct work_struct worker;
253
254 struct delayed_work waker;
255 unsigned long last_commit_jiffies;
256
257 struct dm_bio_prison *prison;
258 struct dm_deferred_set *all_io_ds;
259
260 mempool_t *migration_pool;
261 struct dm_cache_migration *next_migration;
262
263 struct dm_cache_policy *policy;
264 unsigned policy_nr_args;
265
266 bool need_tick_bio:1;
267 bool sized:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000268 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000269 bool commit_requested:1;
270 bool loaded_mappings:1;
271 bool loaded_discards:1;
272
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000273 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400274 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000275 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400276 struct cache_features features;
277
278 struct cache_stats stats;
Joe Thornber65790ff2013-11-08 16:39:50 +0000279
280 /*
281 * Invalidation fields.
282 */
283 spinlock_t invalidation_lock;
284 struct list_head invalidation_requests;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000285};
286
287struct per_bio_data {
288 bool tick:1;
289 unsigned req_nr:2;
290 struct dm_deferred_entry *all_io_entry;
Mike Snitzerc6eda5e2014-01-31 14:11:54 -0500291 struct dm_hook_info hook_info;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000292
Mike Snitzer19b00922013-04-05 15:36:34 +0100293 /*
294 * writethrough fields. These MUST remain at the end of this
295 * structure and the 'cache' member must be the first as it
Joe Thornberaeed1422013-05-10 14:37:18 +0100296 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100297 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000298 struct cache *cache;
299 dm_cblock_t cblock;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100300 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000301};
302
303struct dm_cache_migration {
304 struct list_head list;
305 struct cache *cache;
306
307 unsigned long start_jiffies;
308 dm_oblock_t old_oblock;
309 dm_oblock_t new_oblock;
310 dm_cblock_t cblock;
311
312 bool err:1;
313 bool writeback:1;
314 bool demote:1;
315 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400316 bool requeue_holder:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000317 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000318
319 struct dm_bio_prison_cell *old_ocell;
320 struct dm_bio_prison_cell *new_ocell;
321};
322
323/*
324 * Processing a bio in the worker thread may require these memory
325 * allocations. We prealloc to avoid deadlocks (the same worker thread
326 * frees them back to the mempool).
327 */
328struct prealloc {
329 struct dm_cache_migration *mg;
330 struct dm_bio_prison_cell *cell1;
331 struct dm_bio_prison_cell *cell2;
332};
333
334static void wake_worker(struct cache *cache)
335{
336 queue_work(cache->wq, &cache->worker);
337}
338
339/*----------------------------------------------------------------*/
340
341static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
342{
343 /* FIXME: change to use a local slab. */
344 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
345}
346
347static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
348{
349 dm_bio_prison_free_cell(cache->prison, cell);
350}
351
352static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
353{
354 if (!p->mg) {
355 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
356 if (!p->mg)
357 return -ENOMEM;
358 }
359
360 if (!p->cell1) {
361 p->cell1 = alloc_prison_cell(cache);
362 if (!p->cell1)
363 return -ENOMEM;
364 }
365
366 if (!p->cell2) {
367 p->cell2 = alloc_prison_cell(cache);
368 if (!p->cell2)
369 return -ENOMEM;
370 }
371
372 return 0;
373}
374
375static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
376{
377 if (p->cell2)
378 free_prison_cell(cache, p->cell2);
379
380 if (p->cell1)
381 free_prison_cell(cache, p->cell1);
382
383 if (p->mg)
384 mempool_free(p->mg, cache->migration_pool);
385}
386
387static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
388{
389 struct dm_cache_migration *mg = p->mg;
390
391 BUG_ON(!mg);
392 p->mg = NULL;
393
394 return mg;
395}
396
397/*
398 * You must have a cell within the prealloc struct to return. If not this
399 * function will BUG() rather than returning NULL.
400 */
401static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
402{
403 struct dm_bio_prison_cell *r = NULL;
404
405 if (p->cell1) {
406 r = p->cell1;
407 p->cell1 = NULL;
408
409 } else if (p->cell2) {
410 r = p->cell2;
411 p->cell2 = NULL;
412 } else
413 BUG();
414
415 return r;
416}
417
418/*
419 * You can't have more than two cells in a prealloc struct. BUG() will be
420 * called if you try and overfill.
421 */
422static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
423{
424 if (!p->cell2)
425 p->cell2 = cell;
426
427 else if (!p->cell1)
428 p->cell1 = cell;
429
430 else
431 BUG();
432}
433
434/*----------------------------------------------------------------*/
435
436static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
437{
438 key->virtual = 0;
439 key->dev = 0;
440 key->block = from_oblock(oblock);
441}
442
443/*
444 * The caller hands in a preallocated cell, and a free function for it.
445 * The cell will be freed if there's an error, or if it wasn't used because
446 * a cell with that key already exists.
447 */
448typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
449
450static int bio_detain(struct cache *cache, dm_oblock_t oblock,
451 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
452 cell_free_fn free_fn, void *free_context,
453 struct dm_bio_prison_cell **cell_result)
454{
455 int r;
456 struct dm_cell_key key;
457
458 build_key(oblock, &key);
459 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
460 if (r)
461 free_fn(free_context, cell_prealloc);
462
463 return r;
464}
465
466static int get_cell(struct cache *cache,
467 dm_oblock_t oblock,
468 struct prealloc *structs,
469 struct dm_bio_prison_cell **cell_result)
470{
471 int r;
472 struct dm_cell_key key;
473 struct dm_bio_prison_cell *cell_prealloc;
474
475 cell_prealloc = prealloc_get_cell(structs);
476
477 build_key(oblock, &key);
478 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
479 if (r)
480 prealloc_put_cell(structs, cell_prealloc);
481
482 return r;
483}
484
Joe Thornberaeed1422013-05-10 14:37:18 +0100485/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000486
487static bool is_dirty(struct cache *cache, dm_cblock_t b)
488{
489 return test_bit(from_cblock(b), cache->dirty_bitset);
490}
491
492static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
493{
494 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
Anssi Hannula44fa8162014-08-01 11:55:47 -0400495 atomic_inc(&cache->nr_dirty);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000496 policy_set_dirty(cache->policy, oblock);
497 }
498}
499
500static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
501{
502 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
503 policy_clear_dirty(cache->policy, oblock);
Anssi Hannula44fa8162014-08-01 11:55:47 -0400504 if (atomic_dec_return(&cache->nr_dirty) == 0)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000505 dm_table_event(cache->ti->table);
506 }
507}
508
509/*----------------------------------------------------------------*/
Joe Thornberaeed1422013-05-10 14:37:18 +0100510
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000511static bool block_size_is_power_of_two(struct cache *cache)
512{
513 return cache->sectors_per_block_shift >= 0;
514}
515
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100516/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
517#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
518__always_inline
519#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000520static dm_block_t block_div(dm_block_t b, uint32_t n)
521{
522 do_div(b, n);
523
524 return b;
525}
526
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400527static void set_discard(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000528{
529 unsigned long flags;
530
531 atomic_inc(&cache->stats.discard_count);
532
533 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400534 set_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000535 spin_unlock_irqrestore(&cache->lock, flags);
536}
537
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400538static void clear_discard(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000539{
540 unsigned long flags;
541
542 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400543 clear_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000544 spin_unlock_irqrestore(&cache->lock, flags);
545}
546
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400547static bool is_discarded(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000548{
549 int r;
550 unsigned long flags;
551
552 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400553 r = test_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000554 spin_unlock_irqrestore(&cache->lock, flags);
555
556 return r;
557}
558
559static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
560{
561 int r;
562 unsigned long flags;
563
564 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400565 r = test_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000566 spin_unlock_irqrestore(&cache->lock, flags);
567
568 return r;
569}
570
571/*----------------------------------------------------------------*/
572
573static void load_stats(struct cache *cache)
574{
575 struct dm_cache_statistics stats;
576
577 dm_cache_metadata_get_stats(cache->cmd, &stats);
578 atomic_set(&cache->stats.read_hit, stats.read_hits);
579 atomic_set(&cache->stats.read_miss, stats.read_misses);
580 atomic_set(&cache->stats.write_hit, stats.write_hits);
581 atomic_set(&cache->stats.write_miss, stats.write_misses);
582}
583
584static void save_stats(struct cache *cache)
585{
586 struct dm_cache_statistics stats;
587
588 stats.read_hits = atomic_read(&cache->stats.read_hit);
589 stats.read_misses = atomic_read(&cache->stats.read_miss);
590 stats.write_hits = atomic_read(&cache->stats.write_hit);
591 stats.write_misses = atomic_read(&cache->stats.write_miss);
592
593 dm_cache_metadata_set_stats(cache->cmd, &stats);
594}
595
596/*----------------------------------------------------------------
597 * Per bio data
598 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100599
600/*
601 * If using writeback, leave out struct per_bio_data's writethrough fields.
602 */
603#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
604#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
605
Joe Thornber2ee57d52013-10-24 14:10:29 -0400606static bool writethrough_mode(struct cache_features *f)
607{
608 return f->io_mode == CM_IO_WRITETHROUGH;
609}
610
611static bool writeback_mode(struct cache_features *f)
612{
613 return f->io_mode == CM_IO_WRITEBACK;
614}
615
616static bool passthrough_mode(struct cache_features *f)
617{
618 return f->io_mode == CM_IO_PASSTHROUGH;
619}
620
Mike Snitzer19b00922013-04-05 15:36:34 +0100621static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000622{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400623 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100624}
625
626static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
627{
628 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000629 BUG_ON(!pb);
630 return pb;
631}
632
Mike Snitzer19b00922013-04-05 15:36:34 +0100633static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000634{
Mike Snitzer19b00922013-04-05 15:36:34 +0100635 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000636
637 pb->tick = false;
638 pb->req_nr = dm_bio_get_target_bio_nr(bio);
639 pb->all_io_entry = NULL;
640
641 return pb;
642}
643
644/*----------------------------------------------------------------
645 * Remapping
646 *--------------------------------------------------------------*/
647static void remap_to_origin(struct cache *cache, struct bio *bio)
648{
649 bio->bi_bdev = cache->origin_dev->bdev;
650}
651
652static void remap_to_cache(struct cache *cache, struct bio *bio,
653 dm_cblock_t cblock)
654{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700655 sector_t bi_sector = bio->bi_iter.bi_sector;
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100656 sector_t block = from_cblock(cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000657
658 bio->bi_bdev = cache->cache_dev->bdev;
659 if (!block_size_is_power_of_two(cache))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700660 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100661 (block * cache->sectors_per_block) +
Kent Overstreet4f024f32013-10-11 15:44:27 -0700662 sector_div(bi_sector, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000663 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700664 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100665 (block << cache->sectors_per_block_shift) |
Kent Overstreet4f024f32013-10-11 15:44:27 -0700666 (bi_sector & (cache->sectors_per_block - 1));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000667}
668
669static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
670{
671 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100672 size_t pb_data_size = get_per_bio_data_size(cache);
673 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000674
675 spin_lock_irqsave(&cache->lock, flags);
676 if (cache->need_tick_bio &&
677 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
678 pb->tick = true;
679 cache->need_tick_bio = false;
680 }
681 spin_unlock_irqrestore(&cache->lock, flags);
682}
683
684static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
685 dm_oblock_t oblock)
686{
687 check_if_tick_bio_needed(cache, bio);
688 remap_to_origin(cache, bio);
689 if (bio_data_dir(bio) == WRITE)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400690 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000691}
692
693static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
694 dm_oblock_t oblock, dm_cblock_t cblock)
695{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100696 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000697 remap_to_cache(cache, bio, cblock);
698 if (bio_data_dir(bio) == WRITE) {
699 set_dirty(cache, oblock, cblock);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400700 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000701 }
702}
703
704static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
705{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700706 sector_t block_nr = bio->bi_iter.bi_sector;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000707
708 if (!block_size_is_power_of_two(cache))
709 (void) sector_div(block_nr, cache->sectors_per_block);
710 else
711 block_nr >>= cache->sectors_per_block_shift;
712
713 return to_oblock(block_nr);
714}
715
716static int bio_triggers_commit(struct cache *cache, struct bio *bio)
717{
718 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
719}
720
Joe Thornber8c081b52014-05-13 16:18:38 +0100721/*
722 * You must increment the deferred set whilst the prison cell is held. To
723 * encourage this, we ask for 'cell' to be passed in.
724 */
725static void inc_ds(struct cache *cache, struct bio *bio,
726 struct dm_bio_prison_cell *cell)
727{
728 size_t pb_data_size = get_per_bio_data_size(cache);
729 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
730
731 BUG_ON(!cell);
732 BUG_ON(pb->all_io_entry);
733
734 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
735}
736
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000737static void issue(struct cache *cache, struct bio *bio)
738{
739 unsigned long flags;
740
741 if (!bio_triggers_commit(cache, bio)) {
742 generic_make_request(bio);
743 return;
744 }
745
746 /*
747 * Batch together any bios that trigger commits and then issue a
748 * single commit for them in do_worker().
749 */
750 spin_lock_irqsave(&cache->lock, flags);
751 cache->commit_requested = true;
752 bio_list_add(&cache->deferred_flush_bios, bio);
753 spin_unlock_irqrestore(&cache->lock, flags);
754}
755
Joe Thornber8c081b52014-05-13 16:18:38 +0100756static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
757{
758 inc_ds(cache, bio, cell);
759 issue(cache, bio);
760}
761
Joe Thornbere2e74d62013-03-20 17:21:27 +0000762static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
763{
764 unsigned long flags;
765
766 spin_lock_irqsave(&cache->lock, flags);
767 bio_list_add(&cache->deferred_writethrough_bios, bio);
768 spin_unlock_irqrestore(&cache->lock, flags);
769
770 wake_worker(cache);
771}
772
773static void writethrough_endio(struct bio *bio, int err)
774{
Mike Snitzer19b00922013-04-05 15:36:34 +0100775 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400776
777 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000778
779 if (err) {
780 bio_endio(bio, err);
781 return;
782 }
783
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100784 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000785 remap_to_cache(pb->cache, bio, pb->cblock);
786
787 /*
788 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed1422013-05-10 14:37:18 +0100789 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000790 * worker thread.
791 */
792 defer_writethrough_bio(pb->cache, bio);
793}
794
795/*
796 * When running in writethrough mode we need to send writes to clean blocks
797 * to both the cache and origin devices. In future we'd like to clone the
798 * bio and send them in parallel, but for now we're doing them in
799 * series as this is easier.
800 */
801static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
802 dm_oblock_t oblock, dm_cblock_t cblock)
803{
Mike Snitzer19b00922013-04-05 15:36:34 +0100804 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000805
806 pb->cache = cache;
807 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400808 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100809 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000810
811 remap_to_origin_clear_discard(pb->cache, bio, oblock);
812}
813
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000814/*----------------------------------------------------------------
815 * Migration processing
816 *
817 * Migration covers moving data from the origin device to the cache, or
818 * vice versa.
819 *--------------------------------------------------------------*/
820static void free_migration(struct dm_cache_migration *mg)
821{
822 mempool_free(mg, mg->cache->migration_pool);
823}
824
825static void inc_nr_migrations(struct cache *cache)
826{
827 atomic_inc(&cache->nr_migrations);
828}
829
830static void dec_nr_migrations(struct cache *cache)
831{
832 atomic_dec(&cache->nr_migrations);
833
834 /*
835 * Wake the worker in case we're suspending the target.
836 */
837 wake_up(&cache->migration_wait);
838}
839
840static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
841 bool holder)
842{
843 (holder ? dm_cell_release : dm_cell_release_no_holder)
844 (cache->prison, cell, &cache->deferred_bios);
845 free_prison_cell(cache, cell);
846}
847
848static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
849 bool holder)
850{
851 unsigned long flags;
852
853 spin_lock_irqsave(&cache->lock, flags);
854 __cell_defer(cache, cell, holder);
855 spin_unlock_irqrestore(&cache->lock, flags);
856
857 wake_worker(cache);
858}
859
860static void cleanup_migration(struct dm_cache_migration *mg)
861{
Joe Thornber66cb1912013-10-30 17:11:58 +0000862 struct cache *cache = mg->cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000863 free_migration(mg);
Joe Thornber66cb1912013-10-30 17:11:58 +0000864 dec_nr_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000865}
866
867static void migration_failure(struct dm_cache_migration *mg)
868{
869 struct cache *cache = mg->cache;
870
871 if (mg->writeback) {
872 DMWARN_LIMIT("writeback failed; couldn't copy block");
873 set_dirty(cache, mg->old_oblock, mg->cblock);
874 cell_defer(cache, mg->old_ocell, false);
875
876 } else if (mg->demote) {
877 DMWARN_LIMIT("demotion failed; couldn't copy block");
878 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
879
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200880 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000881 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200882 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000883 } else {
884 DMWARN_LIMIT("promotion failed; couldn't copy block");
885 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200886 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000887 }
888
889 cleanup_migration(mg);
890}
891
892static void migration_success_pre_commit(struct dm_cache_migration *mg)
893{
894 unsigned long flags;
895 struct cache *cache = mg->cache;
896
897 if (mg->writeback) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000898 clear_dirty(cache, mg->old_oblock, mg->cblock);
Anssi Hannula40aa9782014-09-05 03:11:28 +0300899 cell_defer(cache, mg->old_ocell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000900 cleanup_migration(mg);
901 return;
902
903 } else if (mg->demote) {
904 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
905 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
906 policy_force_mapping(cache->policy, mg->new_oblock,
907 mg->old_oblock);
908 if (mg->promote)
909 cell_defer(cache, mg->new_ocell, true);
910 cleanup_migration(mg);
911 return;
912 }
913 } else {
914 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
915 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
916 policy_remove_mapping(cache->policy, mg->new_oblock);
917 cleanup_migration(mg);
918 return;
919 }
920 }
921
922 spin_lock_irqsave(&cache->lock, flags);
923 list_add_tail(&mg->list, &cache->need_commit_migrations);
924 cache->commit_requested = true;
925 spin_unlock_irqrestore(&cache->lock, flags);
926}
927
928static void migration_success_post_commit(struct dm_cache_migration *mg)
929{
930 unsigned long flags;
931 struct cache *cache = mg->cache;
932
933 if (mg->writeback) {
934 DMWARN("writeback unexpectedly triggered commit");
935 return;
936
937 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200938 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000939
940 if (mg->promote) {
941 mg->demote = false;
942
943 spin_lock_irqsave(&cache->lock, flags);
944 list_add_tail(&mg->list, &cache->quiesced_migrations);
945 spin_unlock_irqrestore(&cache->lock, flags);
946
Joe Thornber65790ff2013-11-08 16:39:50 +0000947 } else {
948 if (mg->invalidate)
949 policy_remove_mapping(cache->policy, mg->old_oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000950 cleanup_migration(mg);
Joe Thornber65790ff2013-11-08 16:39:50 +0000951 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000952
953 } else {
Joe Thornber7a9cdc42014-11-27 12:26:46 +0000954 if (mg->requeue_holder) {
955 clear_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400956 cell_defer(cache, mg->new_ocell, true);
Joe Thornber7a9cdc42014-11-27 12:26:46 +0000957 } else {
958 /*
959 * The block was promoted via an overwrite, so it's dirty.
960 */
961 set_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400962 bio_endio(mg->new_ocell->holder, 0);
963 cell_defer(cache, mg->new_ocell, false);
964 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000965 cleanup_migration(mg);
966 }
967}
968
969static void copy_complete(int read_err, unsigned long write_err, void *context)
970{
971 unsigned long flags;
972 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
973 struct cache *cache = mg->cache;
974
975 if (read_err || write_err)
976 mg->err = true;
977
978 spin_lock_irqsave(&cache->lock, flags);
979 list_add_tail(&mg->list, &cache->completed_migrations);
980 spin_unlock_irqrestore(&cache->lock, flags);
981
982 wake_worker(cache);
983}
984
985static void issue_copy_real(struct dm_cache_migration *mg)
986{
987 int r;
988 struct dm_io_region o_region, c_region;
989 struct cache *cache = mg->cache;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +0100990 sector_t cblock = from_cblock(mg->cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000991
992 o_region.bdev = cache->origin_dev->bdev;
993 o_region.count = cache->sectors_per_block;
994
995 c_region.bdev = cache->cache_dev->bdev;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +0100996 c_region.sector = cblock * cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000997 c_region.count = cache->sectors_per_block;
998
999 if (mg->writeback || mg->demote) {
1000 /* demote */
1001 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
1002 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
1003 } else {
1004 /* promote */
1005 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1006 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1007 }
1008
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001009 if (r < 0) {
1010 DMERR_LIMIT("issuing migration failed");
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001011 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001012 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001013}
1014
Joe Thornberc9d28d52013-10-31 13:55:48 -04001015static void overwrite_endio(struct bio *bio, int err)
1016{
1017 struct dm_cache_migration *mg = bio->bi_private;
1018 struct cache *cache = mg->cache;
1019 size_t pb_data_size = get_per_bio_data_size(cache);
1020 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1021 unsigned long flags;
1022
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001023 dm_unhook_bio(&pb->hook_info, bio);
1024
Joe Thornberc9d28d52013-10-31 13:55:48 -04001025 if (err)
1026 mg->err = true;
1027
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001028 mg->requeue_holder = false;
1029
Joe Thornberc9d28d52013-10-31 13:55:48 -04001030 spin_lock_irqsave(&cache->lock, flags);
1031 list_add_tail(&mg->list, &cache->completed_migrations);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001032 spin_unlock_irqrestore(&cache->lock, flags);
1033
1034 wake_worker(cache);
1035}
1036
1037static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1038{
1039 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1040 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1041
1042 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1043 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001044
1045 /*
1046 * No need to inc_ds() here, since the cell will be held for the
1047 * duration of the io.
1048 */
Joe Thornberc9d28d52013-10-31 13:55:48 -04001049 generic_make_request(bio);
1050}
1051
1052static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1053{
1054 return (bio_data_dir(bio) == WRITE) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001055 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
Joe Thornberc9d28d52013-10-31 13:55:48 -04001056}
1057
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001058static void avoid_copy(struct dm_cache_migration *mg)
1059{
1060 atomic_inc(&mg->cache->stats.copies_avoided);
1061 migration_success_pre_commit(mg);
1062}
1063
1064static void issue_copy(struct dm_cache_migration *mg)
1065{
1066 bool avoid;
1067 struct cache *cache = mg->cache;
1068
1069 if (mg->writeback || mg->demote)
1070 avoid = !is_dirty(cache, mg->cblock) ||
1071 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001072 else {
1073 struct bio *bio = mg->new_ocell->holder;
1074
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001075 avoid = is_discarded_oblock(cache, mg->new_oblock);
1076
Joe Thornber4df99e32014-11-27 12:21:08 +00001077 if (writeback_mode(&cache->features) &&
1078 !avoid && bio_writes_complete_block(cache, bio)) {
Joe Thornberc9d28d52013-10-31 13:55:48 -04001079 issue_overwrite(mg, bio);
1080 return;
1081 }
1082 }
1083
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001084 avoid ? avoid_copy(mg) : issue_copy_real(mg);
1085}
1086
1087static void complete_migration(struct dm_cache_migration *mg)
1088{
1089 if (mg->err)
1090 migration_failure(mg);
1091 else
1092 migration_success_pre_commit(mg);
1093}
1094
1095static void process_migrations(struct cache *cache, struct list_head *head,
1096 void (*fn)(struct dm_cache_migration *))
1097{
1098 unsigned long flags;
1099 struct list_head list;
1100 struct dm_cache_migration *mg, *tmp;
1101
1102 INIT_LIST_HEAD(&list);
1103 spin_lock_irqsave(&cache->lock, flags);
1104 list_splice_init(head, &list);
1105 spin_unlock_irqrestore(&cache->lock, flags);
1106
1107 list_for_each_entry_safe(mg, tmp, &list, list)
1108 fn(mg);
1109}
1110
1111static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1112{
1113 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1114}
1115
1116static void queue_quiesced_migration(struct dm_cache_migration *mg)
1117{
1118 unsigned long flags;
1119 struct cache *cache = mg->cache;
1120
1121 spin_lock_irqsave(&cache->lock, flags);
1122 __queue_quiesced_migration(mg);
1123 spin_unlock_irqrestore(&cache->lock, flags);
1124
1125 wake_worker(cache);
1126}
1127
1128static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1129{
1130 unsigned long flags;
1131 struct dm_cache_migration *mg, *tmp;
1132
1133 spin_lock_irqsave(&cache->lock, flags);
1134 list_for_each_entry_safe(mg, tmp, work, list)
1135 __queue_quiesced_migration(mg);
1136 spin_unlock_irqrestore(&cache->lock, flags);
1137
1138 wake_worker(cache);
1139}
1140
1141static void check_for_quiesced_migrations(struct cache *cache,
1142 struct per_bio_data *pb)
1143{
1144 struct list_head work;
1145
1146 if (!pb->all_io_entry)
1147 return;
1148
1149 INIT_LIST_HEAD(&work);
Joe Thornber8c081b52014-05-13 16:18:38 +01001150 dm_deferred_entry_dec(pb->all_io_entry, &work);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001151
1152 if (!list_empty(&work))
1153 queue_quiesced_migrations(cache, &work);
1154}
1155
1156static void quiesce_migration(struct dm_cache_migration *mg)
1157{
1158 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1159 queue_quiesced_migration(mg);
1160}
1161
1162static void promote(struct cache *cache, struct prealloc *structs,
1163 dm_oblock_t oblock, dm_cblock_t cblock,
1164 struct dm_bio_prison_cell *cell)
1165{
1166 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1167
1168 mg->err = false;
1169 mg->writeback = false;
1170 mg->demote = false;
1171 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001172 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001173 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001174 mg->cache = cache;
1175 mg->new_oblock = oblock;
1176 mg->cblock = cblock;
1177 mg->old_ocell = NULL;
1178 mg->new_ocell = cell;
1179 mg->start_jiffies = jiffies;
1180
1181 inc_nr_migrations(cache);
1182 quiesce_migration(mg);
1183}
1184
1185static void writeback(struct cache *cache, struct prealloc *structs,
1186 dm_oblock_t oblock, dm_cblock_t cblock,
1187 struct dm_bio_prison_cell *cell)
1188{
1189 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1190
1191 mg->err = false;
1192 mg->writeback = true;
1193 mg->demote = false;
1194 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001195 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001196 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001197 mg->cache = cache;
1198 mg->old_oblock = oblock;
1199 mg->cblock = cblock;
1200 mg->old_ocell = cell;
1201 mg->new_ocell = NULL;
1202 mg->start_jiffies = jiffies;
1203
1204 inc_nr_migrations(cache);
1205 quiesce_migration(mg);
1206}
1207
1208static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1209 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1210 dm_cblock_t cblock,
1211 struct dm_bio_prison_cell *old_ocell,
1212 struct dm_bio_prison_cell *new_ocell)
1213{
1214 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1215
1216 mg->err = false;
1217 mg->writeback = false;
1218 mg->demote = true;
1219 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001220 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001221 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001222 mg->cache = cache;
1223 mg->old_oblock = old_oblock;
1224 mg->new_oblock = new_oblock;
1225 mg->cblock = cblock;
1226 mg->old_ocell = old_ocell;
1227 mg->new_ocell = new_ocell;
1228 mg->start_jiffies = jiffies;
1229
1230 inc_nr_migrations(cache);
1231 quiesce_migration(mg);
1232}
1233
Joe Thornber2ee57d52013-10-24 14:10:29 -04001234/*
1235 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1236 * block are thrown away.
1237 */
1238static void invalidate(struct cache *cache, struct prealloc *structs,
1239 dm_oblock_t oblock, dm_cblock_t cblock,
1240 struct dm_bio_prison_cell *cell)
1241{
1242 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1243
1244 mg->err = false;
1245 mg->writeback = false;
1246 mg->demote = true;
1247 mg->promote = false;
1248 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001249 mg->invalidate = true;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001250 mg->cache = cache;
1251 mg->old_oblock = oblock;
1252 mg->cblock = cblock;
1253 mg->old_ocell = cell;
1254 mg->new_ocell = NULL;
1255 mg->start_jiffies = jiffies;
1256
1257 inc_nr_migrations(cache);
1258 quiesce_migration(mg);
1259}
1260
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001261/*----------------------------------------------------------------
1262 * bio processing
1263 *--------------------------------------------------------------*/
1264static void defer_bio(struct cache *cache, struct bio *bio)
1265{
1266 unsigned long flags;
1267
1268 spin_lock_irqsave(&cache->lock, flags);
1269 bio_list_add(&cache->deferred_bios, bio);
1270 spin_unlock_irqrestore(&cache->lock, flags);
1271
1272 wake_worker(cache);
1273}
1274
1275static void process_flush_bio(struct cache *cache, struct bio *bio)
1276{
Mike Snitzer19b00922013-04-05 15:36:34 +01001277 size_t pb_data_size = get_per_bio_data_size(cache);
1278 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001279
Kent Overstreet4f024f32013-10-11 15:44:27 -07001280 BUG_ON(bio->bi_iter.bi_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001281 if (!pb->req_nr)
1282 remap_to_origin(cache, bio);
1283 else
1284 remap_to_cache(cache, bio, 0);
1285
Joe Thornber8c081b52014-05-13 16:18:38 +01001286 /*
1287 * REQ_FLUSH is not directed at any particular block so we don't
1288 * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH
1289 * by dm-core.
1290 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001291 issue(cache, bio);
1292}
1293
1294/*
1295 * People generally discard large parts of a device, eg, the whole device
1296 * when formatting. Splitting these large discards up into cache block
1297 * sized ios and then quiescing (always neccessary for discard) takes too
1298 * long.
1299 *
1300 * We keep it simple, and allow any size of discard to come in, and just
1301 * mark off blocks on the discard bitset. No passdown occurs!
1302 *
1303 * To implement passdown we need to change the bio_prison such that a cell
1304 * can have a key that spans many blocks.
1305 */
1306static void process_discard_bio(struct cache *cache, struct bio *bio)
1307{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001308 dm_block_t start_block = dm_sector_div_up(bio->bi_iter.bi_sector,
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001309 cache->sectors_per_block);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001310 dm_block_t end_block = bio_end_sector(bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001311 dm_block_t b;
1312
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001313 end_block = block_div(end_block, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001314
1315 for (b = start_block; b < end_block; b++)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001316 set_discard(cache, to_oblock(b));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001317
1318 bio_endio(bio, 0);
1319}
1320
1321static bool spare_migration_bandwidth(struct cache *cache)
1322{
1323 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1324 cache->sectors_per_block;
1325 return current_volume < cache->migration_threshold;
1326}
1327
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001328static void inc_hit_counter(struct cache *cache, struct bio *bio)
1329{
1330 atomic_inc(bio_data_dir(bio) == READ ?
1331 &cache->stats.read_hit : &cache->stats.write_hit);
1332}
1333
1334static void inc_miss_counter(struct cache *cache, struct bio *bio)
1335{
1336 atomic_inc(bio_data_dir(bio) == READ ?
1337 &cache->stats.read_miss : &cache->stats.write_miss);
1338}
1339
1340static void process_bio(struct cache *cache, struct prealloc *structs,
1341 struct bio *bio)
1342{
1343 int r;
1344 bool release_cell = true;
1345 dm_oblock_t block = get_bio_block(cache, bio);
1346 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1347 struct policy_result lookup_result;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001348 bool discarded_block = is_discarded_oblock(cache, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001349 bool passthrough = passthrough_mode(&cache->features);
1350 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001351
1352 /*
1353 * Check to see if that block is currently migrating.
1354 */
1355 cell_prealloc = prealloc_get_cell(structs);
1356 r = bio_detain(cache, block, bio, cell_prealloc,
1357 (cell_free_fn) prealloc_put_cell,
1358 structs, &new_ocell);
1359 if (r > 0)
1360 return;
1361
1362 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1363 bio, &lookup_result);
1364
1365 if (r == -EWOULDBLOCK)
1366 /* migration has been denied */
1367 lookup_result.op = POLICY_MISS;
1368
1369 switch (lookup_result.op) {
1370 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001371 if (passthrough) {
1372 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001373
Joe Thornber2ee57d52013-10-24 14:10:29 -04001374 /*
1375 * Passthrough always maps to the origin,
1376 * invalidating any cache blocks that are written
1377 * to.
1378 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001379
Joe Thornber2ee57d52013-10-24 14:10:29 -04001380 if (bio_data_dir(bio) == WRITE) {
1381 atomic_inc(&cache->stats.demotion);
1382 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1383 release_cell = false;
1384
1385 } else {
1386 /* FIXME: factor out issue_origin() */
Joe Thornber2ee57d52013-10-24 14:10:29 -04001387 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001388 inc_and_issue(cache, bio, new_ocell);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001389 }
1390 } else {
1391 inc_hit_counter(cache, bio);
1392
1393 if (bio_data_dir(bio) == WRITE &&
1394 writethrough_mode(&cache->features) &&
1395 !is_dirty(cache, lookup_result.cblock)) {
Joe Thornber2ee57d52013-10-24 14:10:29 -04001396 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001397 inc_and_issue(cache, bio, new_ocell);
1398
1399 } else {
1400 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1401 inc_and_issue(cache, bio, new_ocell);
1402 }
Joe Thornber2ee57d52013-10-24 14:10:29 -04001403 }
1404
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001405 break;
1406
1407 case POLICY_MISS:
1408 inc_miss_counter(cache, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001409 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001410 inc_and_issue(cache, bio, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001411 break;
1412
1413 case POLICY_NEW:
1414 atomic_inc(&cache->stats.promotion);
1415 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1416 release_cell = false;
1417 break;
1418
1419 case POLICY_REPLACE:
1420 cell_prealloc = prealloc_get_cell(structs);
1421 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1422 (cell_free_fn) prealloc_put_cell,
1423 structs, &old_ocell);
1424 if (r > 0) {
1425 /*
1426 * We have to be careful to avoid lock inversion of
1427 * the cells. So we back off, and wait for the
1428 * old_ocell to become free.
1429 */
1430 policy_force_mapping(cache->policy, block,
1431 lookup_result.old_oblock);
1432 atomic_inc(&cache->stats.cache_cell_clash);
1433 break;
1434 }
1435 atomic_inc(&cache->stats.demotion);
1436 atomic_inc(&cache->stats.promotion);
1437
1438 demote_then_promote(cache, structs, lookup_result.old_oblock,
1439 block, lookup_result.cblock,
1440 old_ocell, new_ocell);
1441 release_cell = false;
1442 break;
1443
1444 default:
1445 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1446 (unsigned) lookup_result.op);
1447 bio_io_error(bio);
1448 }
1449
1450 if (release_cell)
1451 cell_defer(cache, new_ocell, false);
1452}
1453
1454static int need_commit_due_to_time(struct cache *cache)
1455{
1456 return jiffies < cache->last_commit_jiffies ||
1457 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1458}
1459
1460static int commit_if_needed(struct cache *cache)
1461{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001462 int r = 0;
1463
1464 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1465 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001466 atomic_inc(&cache->stats.commit_count);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001467 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001468 r = dm_cache_commit(cache->cmd, false);
1469 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001470 }
1471
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001472 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001473}
1474
1475static void process_deferred_bios(struct cache *cache)
1476{
1477 unsigned long flags;
1478 struct bio_list bios;
1479 struct bio *bio;
1480 struct prealloc structs;
1481
1482 memset(&structs, 0, sizeof(structs));
1483 bio_list_init(&bios);
1484
1485 spin_lock_irqsave(&cache->lock, flags);
1486 bio_list_merge(&bios, &cache->deferred_bios);
1487 bio_list_init(&cache->deferred_bios);
1488 spin_unlock_irqrestore(&cache->lock, flags);
1489
1490 while (!bio_list_empty(&bios)) {
1491 /*
1492 * If we've got no free migration structs, and processing
1493 * this bio might require one, we pause until there are some
1494 * prepared mappings to process.
1495 */
1496 if (prealloc_data_structs(cache, &structs)) {
1497 spin_lock_irqsave(&cache->lock, flags);
1498 bio_list_merge(&cache->deferred_bios, &bios);
1499 spin_unlock_irqrestore(&cache->lock, flags);
1500 break;
1501 }
1502
1503 bio = bio_list_pop(&bios);
1504
1505 if (bio->bi_rw & REQ_FLUSH)
1506 process_flush_bio(cache, bio);
1507 else if (bio->bi_rw & REQ_DISCARD)
1508 process_discard_bio(cache, bio);
1509 else
1510 process_bio(cache, &structs, bio);
1511 }
1512
1513 prealloc_free_structs(cache, &structs);
1514}
1515
1516static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1517{
1518 unsigned long flags;
1519 struct bio_list bios;
1520 struct bio *bio;
1521
1522 bio_list_init(&bios);
1523
1524 spin_lock_irqsave(&cache->lock, flags);
1525 bio_list_merge(&bios, &cache->deferred_flush_bios);
1526 bio_list_init(&cache->deferred_flush_bios);
1527 spin_unlock_irqrestore(&cache->lock, flags);
1528
Joe Thornber8c081b52014-05-13 16:18:38 +01001529 /*
1530 * These bios have already been through inc_ds()
1531 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001532 while ((bio = bio_list_pop(&bios)))
1533 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1534}
1535
Joe Thornbere2e74d62013-03-20 17:21:27 +00001536static void process_deferred_writethrough_bios(struct cache *cache)
1537{
1538 unsigned long flags;
1539 struct bio_list bios;
1540 struct bio *bio;
1541
1542 bio_list_init(&bios);
1543
1544 spin_lock_irqsave(&cache->lock, flags);
1545 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1546 bio_list_init(&cache->deferred_writethrough_bios);
1547 spin_unlock_irqrestore(&cache->lock, flags);
1548
Joe Thornber8c081b52014-05-13 16:18:38 +01001549 /*
1550 * These bios have already been through inc_ds()
1551 */
Joe Thornbere2e74d62013-03-20 17:21:27 +00001552 while ((bio = bio_list_pop(&bios)))
1553 generic_make_request(bio);
1554}
1555
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001556static void writeback_some_dirty_blocks(struct cache *cache)
1557{
1558 int r = 0;
1559 dm_oblock_t oblock;
1560 dm_cblock_t cblock;
1561 struct prealloc structs;
1562 struct dm_bio_prison_cell *old_ocell;
1563
1564 memset(&structs, 0, sizeof(structs));
1565
1566 while (spare_migration_bandwidth(cache)) {
1567 if (prealloc_data_structs(cache, &structs))
1568 break;
1569
1570 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1571 if (r)
1572 break;
1573
1574 r = get_cell(cache, oblock, &structs, &old_ocell);
1575 if (r) {
1576 policy_set_dirty(cache->policy, oblock);
1577 break;
1578 }
1579
1580 writeback(cache, &structs, oblock, cblock, old_ocell);
1581 }
1582
1583 prealloc_free_structs(cache, &structs);
1584}
1585
1586/*----------------------------------------------------------------
Joe Thornber65790ff2013-11-08 16:39:50 +00001587 * Invalidations.
1588 * Dropping something from the cache *without* writing back.
1589 *--------------------------------------------------------------*/
1590
1591static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1592{
1593 int r = 0;
1594 uint64_t begin = from_cblock(req->cblocks->begin);
1595 uint64_t end = from_cblock(req->cblocks->end);
1596
1597 while (begin != end) {
1598 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1599 if (!r) {
1600 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1601 if (r)
1602 break;
1603
1604 } else if (r == -ENODATA) {
1605 /* harmless, already unmapped */
1606 r = 0;
1607
1608 } else {
1609 DMERR("policy_remove_cblock failed");
1610 break;
1611 }
1612
1613 begin++;
1614 }
1615
1616 cache->commit_requested = true;
1617
1618 req->err = r;
1619 atomic_set(&req->complete, 1);
1620
1621 wake_up(&req->result_wait);
1622}
1623
1624static void process_invalidation_requests(struct cache *cache)
1625{
1626 struct list_head list;
1627 struct invalidation_request *req, *tmp;
1628
1629 INIT_LIST_HEAD(&list);
1630 spin_lock(&cache->invalidation_lock);
1631 list_splice_init(&cache->invalidation_requests, &list);
1632 spin_unlock(&cache->invalidation_lock);
1633
1634 list_for_each_entry_safe (req, tmp, &list, list)
1635 process_invalidation_request(cache, req);
1636}
1637
1638/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001639 * Main worker loop
1640 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001641static bool is_quiescing(struct cache *cache)
1642{
Joe Thornber238f8362013-10-30 17:29:30 +00001643 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001644}
1645
Joe Thornber66cb1912013-10-30 17:11:58 +00001646static void ack_quiescing(struct cache *cache)
1647{
1648 if (is_quiescing(cache)) {
1649 atomic_inc(&cache->quiescing_ack);
1650 wake_up(&cache->quiescing_wait);
1651 }
1652}
1653
1654static void wait_for_quiescing_ack(struct cache *cache)
1655{
1656 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1657}
1658
1659static void start_quiescing(struct cache *cache)
1660{
Joe Thornber238f8362013-10-30 17:29:30 +00001661 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00001662 wait_for_quiescing_ack(cache);
1663}
1664
1665static void stop_quiescing(struct cache *cache)
1666{
Joe Thornber238f8362013-10-30 17:29:30 +00001667 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00001668 atomic_set(&cache->quiescing_ack, 0);
1669}
1670
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001671static void wait_for_migrations(struct cache *cache)
1672{
1673 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1674}
1675
1676static void stop_worker(struct cache *cache)
1677{
1678 cancel_delayed_work(&cache->waker);
1679 flush_workqueue(cache->wq);
1680}
1681
1682static void requeue_deferred_io(struct cache *cache)
1683{
1684 struct bio *bio;
1685 struct bio_list bios;
1686
1687 bio_list_init(&bios);
1688 bio_list_merge(&bios, &cache->deferred_bios);
1689 bio_list_init(&cache->deferred_bios);
1690
1691 while ((bio = bio_list_pop(&bios)))
1692 bio_endio(bio, DM_ENDIO_REQUEUE);
1693}
1694
1695static int more_work(struct cache *cache)
1696{
1697 if (is_quiescing(cache))
1698 return !list_empty(&cache->quiesced_migrations) ||
1699 !list_empty(&cache->completed_migrations) ||
1700 !list_empty(&cache->need_commit_migrations);
1701 else
1702 return !bio_list_empty(&cache->deferred_bios) ||
1703 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001704 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001705 !list_empty(&cache->quiesced_migrations) ||
1706 !list_empty(&cache->completed_migrations) ||
Joe Thornber65790ff2013-11-08 16:39:50 +00001707 !list_empty(&cache->need_commit_migrations) ||
1708 cache->invalidate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001709}
1710
1711static void do_worker(struct work_struct *ws)
1712{
1713 struct cache *cache = container_of(ws, struct cache, worker);
1714
1715 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001716 if (!is_quiescing(cache)) {
1717 writeback_some_dirty_blocks(cache);
1718 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001719 process_deferred_bios(cache);
Joe Thornber65790ff2013-11-08 16:39:50 +00001720 process_invalidation_requests(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001721 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001722
1723 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1724 process_migrations(cache, &cache->completed_migrations, complete_migration);
1725
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001726 if (commit_if_needed(cache)) {
1727 process_deferred_flush_bios(cache, false);
Joe Thornber304affa2014-06-24 15:36:58 -04001728 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001729
1730 /*
1731 * FIXME: rollback metadata or just go into a
1732 * failure mode and error everything
1733 */
1734 } else {
1735 process_deferred_flush_bios(cache, true);
1736 process_migrations(cache, &cache->need_commit_migrations,
1737 migration_success_post_commit);
1738 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001739
1740 ack_quiescing(cache);
1741
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001742 } while (more_work(cache));
1743}
1744
1745/*
1746 * We want to commit periodically so that not too much
1747 * unwritten metadata builds up.
1748 */
1749static void do_waker(struct work_struct *ws)
1750{
1751 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001752 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001753 wake_worker(cache);
1754 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1755}
1756
1757/*----------------------------------------------------------------*/
1758
1759static int is_congested(struct dm_dev *dev, int bdi_bits)
1760{
1761 struct request_queue *q = bdev_get_queue(dev->bdev);
1762 return bdi_congested(&q->backing_dev_info, bdi_bits);
1763}
1764
1765static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1766{
1767 struct cache *cache = container_of(cb, struct cache, callbacks);
1768
1769 return is_congested(cache->origin_dev, bdi_bits) ||
1770 is_congested(cache->cache_dev, bdi_bits);
1771}
1772
1773/*----------------------------------------------------------------
1774 * Target methods
1775 *--------------------------------------------------------------*/
1776
1777/*
1778 * This function gets called on the error paths of the constructor, so we
1779 * have to cope with a partially initialised struct.
1780 */
1781static void destroy(struct cache *cache)
1782{
1783 unsigned i;
1784
1785 if (cache->next_migration)
1786 mempool_free(cache->next_migration, cache->migration_pool);
1787
1788 if (cache->migration_pool)
1789 mempool_destroy(cache->migration_pool);
1790
1791 if (cache->all_io_ds)
1792 dm_deferred_set_destroy(cache->all_io_ds);
1793
1794 if (cache->prison)
1795 dm_bio_prison_destroy(cache->prison);
1796
1797 if (cache->wq)
1798 destroy_workqueue(cache->wq);
1799
1800 if (cache->dirty_bitset)
1801 free_bitset(cache->dirty_bitset);
1802
1803 if (cache->discard_bitset)
1804 free_bitset(cache->discard_bitset);
1805
1806 if (cache->copier)
1807 dm_kcopyd_client_destroy(cache->copier);
1808
1809 if (cache->cmd)
1810 dm_cache_metadata_close(cache->cmd);
1811
1812 if (cache->metadata_dev)
1813 dm_put_device(cache->ti, cache->metadata_dev);
1814
1815 if (cache->origin_dev)
1816 dm_put_device(cache->ti, cache->origin_dev);
1817
1818 if (cache->cache_dev)
1819 dm_put_device(cache->ti, cache->cache_dev);
1820
1821 if (cache->policy)
1822 dm_cache_policy_destroy(cache->policy);
1823
1824 for (i = 0; i < cache->nr_ctr_args ; i++)
1825 kfree(cache->ctr_args[i]);
1826 kfree(cache->ctr_args);
1827
1828 kfree(cache);
1829}
1830
1831static void cache_dtr(struct dm_target *ti)
1832{
1833 struct cache *cache = ti->private;
1834
1835 destroy(cache);
1836}
1837
1838static sector_t get_dev_size(struct dm_dev *dev)
1839{
1840 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1841}
1842
1843/*----------------------------------------------------------------*/
1844
1845/*
1846 * Construct a cache device mapping.
1847 *
1848 * cache <metadata dev> <cache dev> <origin dev> <block size>
1849 * <#feature args> [<feature arg>]*
1850 * <policy> <#policy args> [<policy arg>]*
1851 *
1852 * metadata dev : fast device holding the persistent metadata
1853 * cache dev : fast device holding cached data blocks
1854 * origin dev : slow device holding original data blocks
1855 * block size : cache unit size in sectors
1856 *
1857 * #feature args : number of feature arguments passed
1858 * feature args : writethrough. (The default is writeback.)
1859 *
1860 * policy : the replacement policy to use
1861 * #policy args : an even number of policy arguments corresponding
1862 * to key/value pairs passed to the policy
1863 * policy args : key/value pairs passed to the policy
1864 * E.g. 'sequential_threshold 1024'
1865 * See cache-policies.txt for details.
1866 *
1867 * Optional feature arguments are:
1868 * writethrough : write through caching that prohibits cache block
1869 * content from being different from origin block content.
1870 * Without this argument, the default behaviour is to write
1871 * back cache block contents later for performance reasons,
1872 * so they may differ from the corresponding origin blocks.
1873 */
1874struct cache_args {
1875 struct dm_target *ti;
1876
1877 struct dm_dev *metadata_dev;
1878
1879 struct dm_dev *cache_dev;
1880 sector_t cache_sectors;
1881
1882 struct dm_dev *origin_dev;
1883 sector_t origin_sectors;
1884
1885 uint32_t block_size;
1886
1887 const char *policy_name;
1888 int policy_argc;
1889 const char **policy_argv;
1890
1891 struct cache_features features;
1892};
1893
1894static void destroy_cache_args(struct cache_args *ca)
1895{
1896 if (ca->metadata_dev)
1897 dm_put_device(ca->ti, ca->metadata_dev);
1898
1899 if (ca->cache_dev)
1900 dm_put_device(ca->ti, ca->cache_dev);
1901
1902 if (ca->origin_dev)
1903 dm_put_device(ca->ti, ca->origin_dev);
1904
1905 kfree(ca);
1906}
1907
1908static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1909{
1910 if (!as->argc) {
1911 *error = "Insufficient args";
1912 return false;
1913 }
1914
1915 return true;
1916}
1917
1918static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1919 char **error)
1920{
1921 int r;
1922 sector_t metadata_dev_size;
1923 char b[BDEVNAME_SIZE];
1924
1925 if (!at_least_one_arg(as, error))
1926 return -EINVAL;
1927
1928 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1929 &ca->metadata_dev);
1930 if (r) {
1931 *error = "Error opening metadata device";
1932 return r;
1933 }
1934
1935 metadata_dev_size = get_dev_size(ca->metadata_dev);
1936 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1937 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1938 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1939
1940 return 0;
1941}
1942
1943static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1944 char **error)
1945{
1946 int r;
1947
1948 if (!at_least_one_arg(as, error))
1949 return -EINVAL;
1950
1951 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1952 &ca->cache_dev);
1953 if (r) {
1954 *error = "Error opening cache device";
1955 return r;
1956 }
1957 ca->cache_sectors = get_dev_size(ca->cache_dev);
1958
1959 return 0;
1960}
1961
1962static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1963 char **error)
1964{
1965 int r;
1966
1967 if (!at_least_one_arg(as, error))
1968 return -EINVAL;
1969
1970 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1971 &ca->origin_dev);
1972 if (r) {
1973 *error = "Error opening origin device";
1974 return r;
1975 }
1976
1977 ca->origin_sectors = get_dev_size(ca->origin_dev);
1978 if (ca->ti->len > ca->origin_sectors) {
1979 *error = "Device size larger than cached device";
1980 return -EINVAL;
1981 }
1982
1983 return 0;
1984}
1985
1986static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1987 char **error)
1988{
Mike Snitzer05473042013-08-16 10:54:19 -04001989 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001990
1991 if (!at_least_one_arg(as, error))
1992 return -EINVAL;
1993
Mike Snitzer05473042013-08-16 10:54:19 -04001994 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
1995 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1996 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1997 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001998 *error = "Invalid data block size";
1999 return -EINVAL;
2000 }
2001
Mike Snitzer05473042013-08-16 10:54:19 -04002002 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002003 *error = "Data block size is larger than the cache device";
2004 return -EINVAL;
2005 }
2006
Mike Snitzer05473042013-08-16 10:54:19 -04002007 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002008
2009 return 0;
2010}
2011
2012static void init_features(struct cache_features *cf)
2013{
2014 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04002015 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002016}
2017
2018static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2019 char **error)
2020{
2021 static struct dm_arg _args[] = {
2022 {0, 1, "Invalid number of cache feature arguments"},
2023 };
2024
2025 int r;
2026 unsigned argc;
2027 const char *arg;
2028 struct cache_features *cf = &ca->features;
2029
2030 init_features(cf);
2031
2032 r = dm_read_arg_group(_args, as, &argc, error);
2033 if (r)
2034 return -EINVAL;
2035
2036 while (argc--) {
2037 arg = dm_shift_arg(as);
2038
2039 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002040 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002041
2042 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002043 cf->io_mode = CM_IO_WRITETHROUGH;
2044
2045 else if (!strcasecmp(arg, "passthrough"))
2046 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002047
2048 else {
2049 *error = "Unrecognised cache feature requested";
2050 return -EINVAL;
2051 }
2052 }
2053
2054 return 0;
2055}
2056
2057static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2058 char **error)
2059{
2060 static struct dm_arg _args[] = {
2061 {0, 1024, "Invalid number of policy arguments"},
2062 };
2063
2064 int r;
2065
2066 if (!at_least_one_arg(as, error))
2067 return -EINVAL;
2068
2069 ca->policy_name = dm_shift_arg(as);
2070
2071 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2072 if (r)
2073 return -EINVAL;
2074
2075 ca->policy_argv = (const char **)as->argv;
2076 dm_consume_args(as, ca->policy_argc);
2077
2078 return 0;
2079}
2080
2081static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2082 char **error)
2083{
2084 int r;
2085 struct dm_arg_set as;
2086
2087 as.argc = argc;
2088 as.argv = argv;
2089
2090 r = parse_metadata_dev(ca, &as, error);
2091 if (r)
2092 return r;
2093
2094 r = parse_cache_dev(ca, &as, error);
2095 if (r)
2096 return r;
2097
2098 r = parse_origin_dev(ca, &as, error);
2099 if (r)
2100 return r;
2101
2102 r = parse_block_size(ca, &as, error);
2103 if (r)
2104 return r;
2105
2106 r = parse_features(ca, &as, error);
2107 if (r)
2108 return r;
2109
2110 r = parse_policy(ca, &as, error);
2111 if (r)
2112 return r;
2113
2114 return 0;
2115}
2116
2117/*----------------------------------------------------------------*/
2118
2119static struct kmem_cache *migration_cache;
2120
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002121#define NOT_CORE_OPTION 1
2122
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002123static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002124{
2125 unsigned long tmp;
2126
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002127 if (!strcasecmp(key, "migration_threshold")) {
2128 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002129 return -EINVAL;
2130
2131 cache->migration_threshold = tmp;
2132 return 0;
2133 }
2134
2135 return NOT_CORE_OPTION;
2136}
2137
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002138static int set_config_value(struct cache *cache, const char *key, const char *value)
2139{
2140 int r = process_config_option(cache, key, value);
2141
2142 if (r == NOT_CORE_OPTION)
2143 r = policy_set_config_value(cache->policy, key, value);
2144
2145 if (r)
2146 DMWARN("bad config value for %s: %s", key, value);
2147
2148 return r;
2149}
2150
2151static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002152{
2153 int r = 0;
2154
2155 if (argc & 1) {
2156 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2157 return -EINVAL;
2158 }
2159
2160 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002161 r = set_config_value(cache, argv[0], argv[1]);
2162 if (r)
2163 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002164
2165 argc -= 2;
2166 argv += 2;
2167 }
2168
2169 return r;
2170}
2171
2172static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2173 char **error)
2174{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002175 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2176 cache->cache_size,
2177 cache->origin_sectors,
2178 cache->sectors_per_block);
2179 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002180 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002181 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002182 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002183 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002184
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002185 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002186}
2187
Joe Thornberf8350da2013-05-10 14:37:16 +01002188#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002189
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002190static int cache_create(struct cache_args *ca, struct cache **result)
2191{
2192 int r = 0;
2193 char **error = &ca->ti->error;
2194 struct cache *cache;
2195 struct dm_target *ti = ca->ti;
2196 dm_block_t origin_blocks;
2197 struct dm_cache_metadata *cmd;
2198 bool may_format = ca->features.mode == CM_WRITE;
2199
2200 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2201 if (!cache)
2202 return -ENOMEM;
2203
2204 cache->ti = ca->ti;
2205 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002206 ti->num_flush_bios = 2;
2207 ti->flush_supported = true;
2208
2209 ti->num_discard_bios = 1;
2210 ti->discards_supported = true;
2211 ti->discard_zeroes_data_unsupported = true;
Heinz Mauelshagenf1daa8382014-05-23 14:10:01 -04002212 /* Discard bios must be split on a block boundary */
2213 ti->split_discard_bios = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002214
Joe Thornber8c5008f2013-05-10 14:37:18 +01002215 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01002216 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002217
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002218 cache->callbacks.congested_fn = cache_is_congested;
2219 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2220
2221 cache->metadata_dev = ca->metadata_dev;
2222 cache->origin_dev = ca->origin_dev;
2223 cache->cache_dev = ca->cache_dev;
2224
2225 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2226
2227 /* FIXME: factor out this whole section */
2228 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002229 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002230 cache->origin_blocks = to_oblock(origin_blocks);
2231
2232 cache->sectors_per_block = ca->block_size;
2233 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2234 r = -EINVAL;
2235 goto bad;
2236 }
2237
2238 if (ca->block_size & (ca->block_size - 1)) {
2239 dm_block_t cache_size = ca->cache_sectors;
2240
2241 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002242 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002243 cache->cache_size = to_cblock(cache_size);
2244 } else {
2245 cache->sectors_per_block_shift = __ffs(ca->block_size);
2246 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2247 }
2248
2249 r = create_cache_policy(cache, ca, error);
2250 if (r)
2251 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002252
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002253 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002254 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2255
2256 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2257 if (r) {
2258 *error = "Error setting cache policy's config values";
2259 goto bad;
2260 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002261
2262 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2263 ca->block_size, may_format,
2264 dm_cache_policy_get_hint_size(cache->policy));
2265 if (IS_ERR(cmd)) {
2266 *error = "Error creating metadata object";
2267 r = PTR_ERR(cmd);
2268 goto bad;
2269 }
2270 cache->cmd = cmd;
2271
Joe Thornber2ee57d52013-10-24 14:10:29 -04002272 if (passthrough_mode(&cache->features)) {
2273 bool all_clean;
2274
2275 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2276 if (r) {
2277 *error = "dm_cache_metadata_all_clean() failed";
2278 goto bad;
2279 }
2280
2281 if (!all_clean) {
2282 *error = "Cannot enter passthrough mode unless all blocks are clean";
2283 r = -EINVAL;
2284 goto bad;
2285 }
2286 }
2287
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002288 spin_lock_init(&cache->lock);
2289 bio_list_init(&cache->deferred_bios);
2290 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002291 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002292 INIT_LIST_HEAD(&cache->quiesced_migrations);
2293 INIT_LIST_HEAD(&cache->completed_migrations);
2294 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002295 atomic_set(&cache->nr_migrations, 0);
2296 init_waitqueue_head(&cache->migration_wait);
2297
Joe Thornber66cb1912013-10-30 17:11:58 +00002298 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002299 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002300 atomic_set(&cache->quiescing_ack, 0);
2301
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002302 r = -ENOMEM;
Anssi Hannula44fa8162014-08-01 11:55:47 -04002303 atomic_set(&cache->nr_dirty, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002304 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2305 if (!cache->dirty_bitset) {
2306 *error = "could not allocate dirty bitset";
2307 goto bad;
2308 }
2309 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2310
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002311 cache->discard_nr_blocks = cache->origin_blocks;
2312 cache->discard_bitset = alloc_bitset(from_oblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002313 if (!cache->discard_bitset) {
2314 *error = "could not allocate discard bitset";
2315 goto bad;
2316 }
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002317 clear_bitset(cache->discard_bitset, from_oblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002318
2319 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2320 if (IS_ERR(cache->copier)) {
2321 *error = "could not create kcopyd client";
2322 r = PTR_ERR(cache->copier);
2323 goto bad;
2324 }
2325
2326 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2327 if (!cache->wq) {
2328 *error = "could not create workqueue for metadata object";
2329 goto bad;
2330 }
2331 INIT_WORK(&cache->worker, do_worker);
2332 INIT_DELAYED_WORK(&cache->waker, do_waker);
2333 cache->last_commit_jiffies = jiffies;
2334
2335 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2336 if (!cache->prison) {
2337 *error = "could not create bio prison";
2338 goto bad;
2339 }
2340
2341 cache->all_io_ds = dm_deferred_set_create();
2342 if (!cache->all_io_ds) {
2343 *error = "could not create all_io deferred set";
2344 goto bad;
2345 }
2346
2347 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2348 migration_cache);
2349 if (!cache->migration_pool) {
2350 *error = "Error creating cache's migration mempool";
2351 goto bad;
2352 }
2353
2354 cache->next_migration = NULL;
2355
2356 cache->need_tick_bio = true;
2357 cache->sized = false;
Joe Thornber65790ff2013-11-08 16:39:50 +00002358 cache->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002359 cache->commit_requested = false;
2360 cache->loaded_mappings = false;
2361 cache->loaded_discards = false;
2362
2363 load_stats(cache);
2364
2365 atomic_set(&cache->stats.demotion, 0);
2366 atomic_set(&cache->stats.promotion, 0);
2367 atomic_set(&cache->stats.copies_avoided, 0);
2368 atomic_set(&cache->stats.cache_cell_clash, 0);
2369 atomic_set(&cache->stats.commit_count, 0);
2370 atomic_set(&cache->stats.discard_count, 0);
2371
Joe Thornber65790ff2013-11-08 16:39:50 +00002372 spin_lock_init(&cache->invalidation_lock);
2373 INIT_LIST_HEAD(&cache->invalidation_requests);
2374
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002375 *result = cache;
2376 return 0;
2377
2378bad:
2379 destroy(cache);
2380 return r;
2381}
2382
2383static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2384{
2385 unsigned i;
2386 const char **copy;
2387
2388 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2389 if (!copy)
2390 return -ENOMEM;
2391 for (i = 0; i < argc; i++) {
2392 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2393 if (!copy[i]) {
2394 while (i--)
2395 kfree(copy[i]);
2396 kfree(copy);
2397 return -ENOMEM;
2398 }
2399 }
2400
2401 cache->nr_ctr_args = argc;
2402 cache->ctr_args = copy;
2403
2404 return 0;
2405}
2406
2407static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2408{
2409 int r = -EINVAL;
2410 struct cache_args *ca;
2411 struct cache *cache = NULL;
2412
2413 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2414 if (!ca) {
2415 ti->error = "Error allocating memory for cache";
2416 return -ENOMEM;
2417 }
2418 ca->ti = ti;
2419
2420 r = parse_cache_args(ca, argc, argv, &ti->error);
2421 if (r)
2422 goto out;
2423
2424 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002425 if (r)
2426 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002427
2428 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2429 if (r) {
2430 destroy(cache);
2431 goto out;
2432 }
2433
2434 ti->private = cache;
2435
2436out:
2437 destroy_cache_args(ca);
2438 return r;
2439}
2440
Joe Thornber8c081b52014-05-13 16:18:38 +01002441static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002442{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002443 int r;
2444 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002445 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002446 bool can_migrate = false;
2447 bool discarded_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002448 struct policy_result lookup_result;
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002449 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002450
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002451 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002452 /*
2453 * This can only occur if the io goes to a partial block at
2454 * the end of the origin device. We don't cache these.
2455 * Just remap to the origin and carry on.
2456 */
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002457 remap_to_origin(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002458 return DM_MAPIO_REMAPPED;
2459 }
2460
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002461 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2462 defer_bio(cache, bio);
2463 return DM_MAPIO_SUBMITTED;
2464 }
2465
2466 /*
2467 * Check to see if that block is currently migrating.
2468 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002469 *cell = alloc_prison_cell(cache);
2470 if (!*cell) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002471 defer_bio(cache, bio);
2472 return DM_MAPIO_SUBMITTED;
2473 }
2474
Joe Thornber8c081b52014-05-13 16:18:38 +01002475 r = bio_detain(cache, block, bio, *cell,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002476 (cell_free_fn) free_prison_cell,
Joe Thornber8c081b52014-05-13 16:18:38 +01002477 cache, cell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002478 if (r) {
2479 if (r < 0)
2480 defer_bio(cache, bio);
2481
2482 return DM_MAPIO_SUBMITTED;
2483 }
2484
2485 discarded_block = is_discarded_oblock(cache, block);
2486
2487 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2488 bio, &lookup_result);
2489 if (r == -EWOULDBLOCK) {
Joe Thornber8c081b52014-05-13 16:18:38 +01002490 cell_defer(cache, *cell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002491 return DM_MAPIO_SUBMITTED;
2492
2493 } else if (r) {
2494 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
Joe Thornber8c081b52014-05-13 16:18:38 +01002495 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002496 bio_io_error(bio);
2497 return DM_MAPIO_SUBMITTED;
2498 }
2499
Joe Thornber2ee57d52013-10-24 14:10:29 -04002500 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002501 switch (lookup_result.op) {
2502 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04002503 if (passthrough_mode(&cache->features)) {
2504 if (bio_data_dir(bio) == WRITE) {
2505 /*
2506 * We need to invalidate this block, so
2507 * defer for the worker thread.
2508 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002509 cell_defer(cache, *cell, true);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002510 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002511
Joe Thornber2ee57d52013-10-24 14:10:29 -04002512 } else {
Joe Thornber2ee57d52013-10-24 14:10:29 -04002513 inc_miss_counter(cache, bio);
2514 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002515 }
2516
2517 } else {
2518 inc_hit_counter(cache, bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002519 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2520 !is_dirty(cache, lookup_result.cblock))
2521 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2522 else
2523 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002524 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002525 break;
2526
2527 case POLICY_MISS:
2528 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002529 if (pb->req_nr != 0) {
2530 /*
2531 * This is a duplicate writethrough io that is no
2532 * longer needed because the block has been demoted.
2533 */
2534 bio_endio(bio, 0);
Joe Thornber8c081b52014-05-13 16:18:38 +01002535 cell_defer(cache, *cell, false);
2536 r = DM_MAPIO_SUBMITTED;
2537
2538 } else
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002539 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01002540
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002541 break;
2542
2543 default:
2544 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2545 (unsigned) lookup_result.op);
Joe Thornber8c081b52014-05-13 16:18:38 +01002546 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002547 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002548 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002549 }
2550
Joe Thornber2ee57d52013-10-24 14:10:29 -04002551 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002552}
2553
Joe Thornber8c081b52014-05-13 16:18:38 +01002554static int cache_map(struct dm_target *ti, struct bio *bio)
2555{
2556 int r;
2557 struct dm_bio_prison_cell *cell;
2558 struct cache *cache = ti->private;
2559
2560 r = __cache_map(cache, bio, &cell);
2561 if (r == DM_MAPIO_REMAPPED) {
2562 inc_ds(cache, bio, cell);
2563 cell_defer(cache, cell, false);
2564 }
2565
2566 return r;
2567}
2568
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002569static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2570{
2571 struct cache *cache = ti->private;
2572 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002573 size_t pb_data_size = get_per_bio_data_size(cache);
2574 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002575
2576 if (pb->tick) {
2577 policy_tick(cache->policy);
2578
2579 spin_lock_irqsave(&cache->lock, flags);
2580 cache->need_tick_bio = true;
2581 spin_unlock_irqrestore(&cache->lock, flags);
2582 }
2583
2584 check_for_quiesced_migrations(cache, pb);
2585
2586 return 0;
2587}
2588
2589static int write_dirty_bitset(struct cache *cache)
2590{
2591 unsigned i, r;
2592
2593 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2594 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2595 is_dirty(cache, to_cblock(i)));
2596 if (r)
2597 return r;
2598 }
2599
2600 return 0;
2601}
2602
2603static int write_discard_bitset(struct cache *cache)
2604{
2605 unsigned i, r;
2606
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002607 r = dm_cache_discard_bitset_resize(cache->cmd, cache->sectors_per_block,
2608 cache->origin_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002609 if (r) {
2610 DMERR("could not resize on-disk discard bitset");
2611 return r;
2612 }
2613
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002614 for (i = 0; i < from_oblock(cache->discard_nr_blocks); i++) {
2615 r = dm_cache_set_discard(cache->cmd, to_oblock(i),
2616 is_discarded(cache, to_oblock(i)));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002617 if (r)
2618 return r;
2619 }
2620
2621 return 0;
2622}
2623
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002624/*
2625 * returns true on success
2626 */
2627static bool sync_metadata(struct cache *cache)
2628{
2629 int r1, r2, r3, r4;
2630
2631 r1 = write_dirty_bitset(cache);
2632 if (r1)
2633 DMERR("could not write dirty bitset");
2634
2635 r2 = write_discard_bitset(cache);
2636 if (r2)
2637 DMERR("could not write discard bitset");
2638
2639 save_stats(cache);
2640
Joe Thornber05966612014-04-03 16:16:44 +01002641 r3 = dm_cache_write_hints(cache->cmd, cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002642 if (r3)
2643 DMERR("could not write hints");
2644
2645 /*
2646 * If writing the above metadata failed, we still commit, but don't
2647 * set the clean shutdown flag. This will effectively force every
2648 * dirty bit to be set on reload.
2649 */
2650 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2651 if (r4)
2652 DMERR("could not write cache metadata. Data loss may occur.");
2653
2654 return !r1 && !r2 && !r3 && !r4;
2655}
2656
2657static void cache_postsuspend(struct dm_target *ti)
2658{
2659 struct cache *cache = ti->private;
2660
2661 start_quiescing(cache);
2662 wait_for_migrations(cache);
2663 stop_worker(cache);
2664 requeue_deferred_io(cache);
2665 stop_quiescing(cache);
2666
2667 (void) sync_metadata(cache);
2668}
2669
2670static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2671 bool dirty, uint32_t hint, bool hint_valid)
2672{
2673 int r;
2674 struct cache *cache = context;
2675
2676 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2677 if (r)
2678 return r;
2679
2680 if (dirty)
2681 set_dirty(cache, oblock, cblock);
2682 else
2683 clear_dirty(cache, oblock, cblock);
2684
2685 return 0;
2686}
2687
2688static int load_discard(void *context, sector_t discard_block_size,
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002689 dm_oblock_t oblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002690{
2691 struct cache *cache = context;
2692
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002693 if (discard)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002694 set_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002695 else
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002696 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002697
2698 return 0;
2699}
2700
Joe Thornberf494a9c2013-10-31 13:55:49 -04002701static dm_cblock_t get_cache_dev_size(struct cache *cache)
2702{
2703 sector_t size = get_dev_size(cache->cache_dev);
2704 (void) sector_div(size, cache->sectors_per_block);
2705 return to_cblock(size);
2706}
2707
2708static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2709{
2710 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2711 return true;
2712
2713 /*
2714 * We can't drop a dirty block when shrinking the cache.
2715 */
2716 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2717 new_size = to_cblock(from_cblock(new_size) + 1);
2718 if (is_dirty(cache, new_size)) {
2719 DMERR("unable to shrink cache; cache block %llu is dirty",
2720 (unsigned long long) from_cblock(new_size));
2721 return false;
2722 }
2723 }
2724
2725 return true;
2726}
2727
2728static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2729{
2730 int r;
2731
Vincent Pelletier08844802013-11-30 12:58:42 +01002732 r = dm_cache_resize(cache->cmd, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04002733 if (r) {
2734 DMERR("could not resize cache metadata");
2735 return r;
2736 }
2737
2738 cache->cache_size = new_size;
2739
2740 return 0;
2741}
2742
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002743static int cache_preresume(struct dm_target *ti)
2744{
2745 int r = 0;
2746 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002747 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002748
2749 /*
2750 * Check to see if the cache has resized.
2751 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04002752 if (!cache->sized) {
2753 r = resize_cache_dev(cache, csize);
2754 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002755 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002756
2757 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002758
2759 } else if (csize != cache->cache_size) {
2760 if (!can_resize(cache, csize))
2761 return -EINVAL;
2762
2763 r = resize_cache_dev(cache, csize);
2764 if (r)
2765 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002766 }
2767
2768 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002769 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002770 load_mapping, cache);
2771 if (r) {
2772 DMERR("could not load cache mappings");
2773 return r;
2774 }
2775
2776 cache->loaded_mappings = true;
2777 }
2778
2779 if (!cache->loaded_discards) {
2780 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2781 if (r) {
2782 DMERR("could not load origin discards");
2783 return r;
2784 }
2785
2786 cache->loaded_discards = true;
2787 }
2788
2789 return r;
2790}
2791
2792static void cache_resume(struct dm_target *ti)
2793{
2794 struct cache *cache = ti->private;
2795
2796 cache->need_tick_bio = true;
2797 do_waker(&cache->waker.work);
2798}
2799
2800/*
2801 * Status format:
2802 *
Mike Snitzer6a388612014-01-09 16:04:12 -05002803 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
2804 * <cache block size> <#used cache blocks>/<#total cache blocks>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002805 * <#read hits> <#read misses> <#write hits> <#write misses>
Mike Snitzer6a388612014-01-09 16:04:12 -05002806 * <#demotions> <#promotions> <#dirty>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002807 * <#features> <features>*
2808 * <#core args> <core args>
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002809 * <policy name> <#policy args> <policy args>*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002810 */
2811static void cache_status(struct dm_target *ti, status_type_t type,
2812 unsigned status_flags, char *result, unsigned maxlen)
2813{
2814 int r = 0;
2815 unsigned i;
2816 ssize_t sz = 0;
2817 dm_block_t nr_free_blocks_metadata = 0;
2818 dm_block_t nr_blocks_metadata = 0;
2819 char buf[BDEVNAME_SIZE];
2820 struct cache *cache = ti->private;
2821 dm_cblock_t residency;
2822
2823 switch (type) {
2824 case STATUSTYPE_INFO:
2825 /* Commit to ensure statistics aren't out-of-date */
2826 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2827 r = dm_cache_commit(cache->cmd, false);
2828 if (r)
2829 DMERR("could not commit metadata for accurate status");
2830 }
2831
2832 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2833 &nr_free_blocks_metadata);
2834 if (r) {
2835 DMERR("could not get metadata free block count");
2836 goto err;
2837 }
2838
2839 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2840 if (r) {
2841 DMERR("could not get metadata device size");
2842 goto err;
2843 }
2844
2845 residency = policy_residency(cache->policy);
2846
Anssi Hannula44fa8162014-08-01 11:55:47 -04002847 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
Mike Snitzer895b47d2014-07-14 15:37:18 -04002848 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002849 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2850 (unsigned long long)nr_blocks_metadata,
Mike Snitzer6a388612014-01-09 16:04:12 -05002851 cache->sectors_per_block,
2852 (unsigned long long) from_cblock(residency),
2853 (unsigned long long) from_cblock(cache->cache_size),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002854 (unsigned) atomic_read(&cache->stats.read_hit),
2855 (unsigned) atomic_read(&cache->stats.read_miss),
2856 (unsigned) atomic_read(&cache->stats.write_hit),
2857 (unsigned) atomic_read(&cache->stats.write_miss),
2858 (unsigned) atomic_read(&cache->stats.demotion),
2859 (unsigned) atomic_read(&cache->stats.promotion),
Anssi Hannula44fa8162014-08-01 11:55:47 -04002860 (unsigned long) atomic_read(&cache->nr_dirty));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002861
Joe Thornber2ee57d52013-10-24 14:10:29 -04002862 if (writethrough_mode(&cache->features))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002863 DMEMIT("1 writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04002864
2865 else if (passthrough_mode(&cache->features))
2866 DMEMIT("1 passthrough ");
2867
2868 else if (writeback_mode(&cache->features))
2869 DMEMIT("1 writeback ");
2870
2871 else {
2872 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2873 goto err;
2874 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002875
2876 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002877
2878 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002879 if (sz < maxlen) {
2880 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2881 if (r)
2882 DMERR("policy_emit_config_values returned %d", r);
2883 }
2884
2885 break;
2886
2887 case STATUSTYPE_TABLE:
2888 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2889 DMEMIT("%s ", buf);
2890 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2891 DMEMIT("%s ", buf);
2892 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2893 DMEMIT("%s", buf);
2894
2895 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2896 DMEMIT(" %s", cache->ctr_args[i]);
2897 if (cache->nr_ctr_args)
2898 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2899 }
2900
2901 return;
2902
2903err:
2904 DMEMIT("Error");
2905}
2906
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002907/*
Joe Thornber65790ff2013-11-08 16:39:50 +00002908 * A cache block range can take two forms:
2909 *
2910 * i) A single cblock, eg. '3456'
2911 * ii) A begin and end cblock with dots between, eg. 123-234
2912 */
2913static int parse_cblock_range(struct cache *cache, const char *str,
2914 struct cblock_range *result)
2915{
2916 char dummy;
2917 uint64_t b, e;
2918 int r;
2919
2920 /*
2921 * Try and parse form (ii) first.
2922 */
2923 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
2924 if (r < 0)
2925 return r;
2926
2927 if (r == 2) {
2928 result->begin = to_cblock(b);
2929 result->end = to_cblock(e);
2930 return 0;
2931 }
2932
2933 /*
2934 * That didn't work, try form (i).
2935 */
2936 r = sscanf(str, "%llu%c", &b, &dummy);
2937 if (r < 0)
2938 return r;
2939
2940 if (r == 1) {
2941 result->begin = to_cblock(b);
2942 result->end = to_cblock(from_cblock(result->begin) + 1u);
2943 return 0;
2944 }
2945
2946 DMERR("invalid cblock range '%s'", str);
2947 return -EINVAL;
2948}
2949
2950static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
2951{
2952 uint64_t b = from_cblock(range->begin);
2953 uint64_t e = from_cblock(range->end);
2954 uint64_t n = from_cblock(cache->cache_size);
2955
2956 if (b >= n) {
2957 DMERR("begin cblock out of range: %llu >= %llu", b, n);
2958 return -EINVAL;
2959 }
2960
2961 if (e > n) {
2962 DMERR("end cblock out of range: %llu > %llu", e, n);
2963 return -EINVAL;
2964 }
2965
2966 if (b >= e) {
2967 DMERR("invalid cblock range: %llu >= %llu", b, e);
2968 return -EINVAL;
2969 }
2970
2971 return 0;
2972}
2973
2974static int request_invalidation(struct cache *cache, struct cblock_range *range)
2975{
2976 struct invalidation_request req;
2977
2978 INIT_LIST_HEAD(&req.list);
2979 req.cblocks = range;
2980 atomic_set(&req.complete, 0);
2981 req.err = 0;
2982 init_waitqueue_head(&req.result_wait);
2983
2984 spin_lock(&cache->invalidation_lock);
2985 list_add(&req.list, &cache->invalidation_requests);
2986 spin_unlock(&cache->invalidation_lock);
2987 wake_worker(cache);
2988
2989 wait_event(req.result_wait, atomic_read(&req.complete));
2990 return req.err;
2991}
2992
2993static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
2994 const char **cblock_ranges)
2995{
2996 int r = 0;
2997 unsigned i;
2998 struct cblock_range range;
2999
3000 if (!passthrough_mode(&cache->features)) {
3001 DMERR("cache has to be in passthrough mode for invalidation");
3002 return -EPERM;
3003 }
3004
3005 for (i = 0; i < count; i++) {
3006 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3007 if (r)
3008 break;
3009
3010 r = validate_cblock_range(cache, &range);
3011 if (r)
3012 break;
3013
3014 /*
3015 * Pass begin and end origin blocks to the worker and wake it.
3016 */
3017 r = request_invalidation(cache, &range);
3018 if (r)
3019 break;
3020 }
3021
3022 return r;
3023}
3024
3025/*
3026 * Supports
3027 * "<key> <value>"
3028 * and
3029 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003030 *
3031 * The key migration_threshold is supported by the cache target core.
3032 */
3033static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3034{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003035 struct cache *cache = ti->private;
3036
Joe Thornber65790ff2013-11-08 16:39:50 +00003037 if (!argc)
3038 return -EINVAL;
3039
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -05003040 if (!strcasecmp(argv[0], "invalidate_cblocks"))
Joe Thornber65790ff2013-11-08 16:39:50 +00003041 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3042
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003043 if (argc != 2)
3044 return -EINVAL;
3045
Joe Thornber2f14f4b2013-05-10 14:37:21 +01003046 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003047}
3048
3049static int cache_iterate_devices(struct dm_target *ti,
3050 iterate_devices_callout_fn fn, void *data)
3051{
3052 int r = 0;
3053 struct cache *cache = ti->private;
3054
3055 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3056 if (!r)
3057 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3058
3059 return r;
3060}
3061
3062/*
3063 * We assume I/O is going to the origin (which is the volume
3064 * more likely to have restrictions e.g. by being striped).
3065 * (Looking up the exact location of the data would be expensive
3066 * and could always be out of date by the time the bio is submitted.)
3067 */
3068static int cache_bvec_merge(struct dm_target *ti,
3069 struct bvec_merge_data *bvm,
3070 struct bio_vec *biovec, int max_size)
3071{
3072 struct cache *cache = ti->private;
3073 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3074
3075 if (!q->merge_bvec_fn)
3076 return max_size;
3077
3078 bvm->bi_bdev = cache->origin_dev->bdev;
3079 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3080}
3081
3082static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3083{
3084 /*
3085 * FIXME: these limits may be incompatible with the cache device
3086 */
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04003087 limits->max_discard_sectors = cache->sectors_per_block;
3088 limits->discard_granularity = cache->sectors_per_block << SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003089}
3090
3091static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3092{
3093 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04003094 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003095
Mike Snitzerf6109372013-08-20 15:02:41 -04003096 /*
3097 * If the system-determined stacked limits are compatible with the
3098 * cache's blocksize (io_opt is a factor) do not override them.
3099 */
3100 if (io_opt_sectors < cache->sectors_per_block ||
3101 do_div(io_opt_sectors, cache->sectors_per_block)) {
Mike Snitzerb0246532014-07-19 13:25:46 -04003102 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf6109372013-08-20 15:02:41 -04003103 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3104 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003105 set_discard_limits(cache, limits);
3106}
3107
3108/*----------------------------------------------------------------*/
3109
3110static struct target_type cache_target = {
3111 .name = "cache",
Joe Thornber8c081b52014-05-13 16:18:38 +01003112 .version = {1, 5, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003113 .module = THIS_MODULE,
3114 .ctr = cache_ctr,
3115 .dtr = cache_dtr,
3116 .map = cache_map,
3117 .end_io = cache_end_io,
3118 .postsuspend = cache_postsuspend,
3119 .preresume = cache_preresume,
3120 .resume = cache_resume,
3121 .status = cache_status,
3122 .message = cache_message,
3123 .iterate_devices = cache_iterate_devices,
3124 .merge = cache_bvec_merge,
3125 .io_hints = cache_io_hints,
3126};
3127
3128static int __init dm_cache_init(void)
3129{
3130 int r;
3131
3132 r = dm_register_target(&cache_target);
3133 if (r) {
3134 DMERR("cache target registration failed: %d", r);
3135 return r;
3136 }
3137
3138 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3139 if (!migration_cache) {
3140 dm_unregister_target(&cache_target);
3141 return -ENOMEM;
3142 }
3143
3144 return 0;
3145}
3146
3147static void __exit dm_cache_exit(void)
3148{
3149 dm_unregister_target(&cache_target);
3150 kmem_cache_destroy(migration_cache);
3151}
3152
3153module_init(dm_cache_init);
3154module_exit(dm_cache_exit);
3155
3156MODULE_DESCRIPTION(DM_NAME " cache target");
3157MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3158MODULE_LICENSE("GPL");