blob: 3baed67cf26f10d65f222dd09dd070ded7c85071 [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;
Joe Thornber538c2bc2015-01-23 10:16:16 +0000225 atomic_t nr_allocated_migrations;
226
227 /*
228 * The number of in flight migrations that are performing
229 * background io. eg, promotion, writeback.
230 */
231 atomic_t nr_io_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000232
Joe Thornber66cb1912013-10-30 17:11:58 +0000233 wait_queue_head_t quiescing_wait;
Joe Thornber238f8362013-10-30 17:29:30 +0000234 atomic_t quiescing;
Joe Thornber66cb1912013-10-30 17:11:58 +0000235 atomic_t quiescing_ack;
236
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000237 /*
238 * cache_size entries, dirty if set
239 */
Anssi Hannula44fa8162014-08-01 11:55:47 -0400240 atomic_t nr_dirty;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000241 unsigned long *dirty_bitset;
242
243 /*
244 * origin_blocks entries, discarded if set.
245 */
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400246 dm_oblock_t discard_nr_blocks;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000247 unsigned long *discard_bitset;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400248
249 /*
250 * Rather than reconstructing the table line for the status we just
251 * save it and regurgitate.
252 */
253 unsigned nr_ctr_args;
254 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000255
256 struct dm_kcopyd_client *copier;
257 struct workqueue_struct *wq;
258 struct work_struct worker;
259
260 struct delayed_work waker;
261 unsigned long last_commit_jiffies;
262
263 struct dm_bio_prison *prison;
264 struct dm_deferred_set *all_io_ds;
265
266 mempool_t *migration_pool;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000267
268 struct dm_cache_policy *policy;
269 unsigned policy_nr_args;
270
271 bool need_tick_bio:1;
272 bool sized:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000273 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000274 bool commit_requested:1;
275 bool loaded_mappings:1;
276 bool loaded_discards:1;
277
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000278 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400279 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000280 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400281 struct cache_features features;
282
283 struct cache_stats stats;
Joe Thornber65790ff2013-11-08 16:39:50 +0000284
285 /*
286 * Invalidation fields.
287 */
288 spinlock_t invalidation_lock;
289 struct list_head invalidation_requests;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000290};
291
292struct per_bio_data {
293 bool tick:1;
294 unsigned req_nr:2;
295 struct dm_deferred_entry *all_io_entry;
Mike Snitzerc6eda5e2014-01-31 14:11:54 -0500296 struct dm_hook_info hook_info;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000297
Mike Snitzer19b00922013-04-05 15:36:34 +0100298 /*
299 * writethrough fields. These MUST remain at the end of this
300 * structure and the 'cache' member must be the first as it
Joe Thornberaeed1422013-05-10 14:37:18 +0100301 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100302 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000303 struct cache *cache;
304 dm_cblock_t cblock;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100305 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000306};
307
308struct dm_cache_migration {
309 struct list_head list;
310 struct cache *cache;
311
312 unsigned long start_jiffies;
313 dm_oblock_t old_oblock;
314 dm_oblock_t new_oblock;
315 dm_cblock_t cblock;
316
317 bool err:1;
318 bool writeback:1;
319 bool demote:1;
320 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400321 bool requeue_holder:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000322 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000323
324 struct dm_bio_prison_cell *old_ocell;
325 struct dm_bio_prison_cell *new_ocell;
326};
327
328/*
329 * Processing a bio in the worker thread may require these memory
330 * allocations. We prealloc to avoid deadlocks (the same worker thread
331 * frees them back to the mempool).
332 */
333struct prealloc {
334 struct dm_cache_migration *mg;
335 struct dm_bio_prison_cell *cell1;
336 struct dm_bio_prison_cell *cell2;
337};
338
339static void wake_worker(struct cache *cache)
340{
341 queue_work(cache->wq, &cache->worker);
342}
343
344/*----------------------------------------------------------------*/
345
346static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
347{
348 /* FIXME: change to use a local slab. */
349 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
350}
351
352static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
353{
354 dm_bio_prison_free_cell(cache->prison, cell);
355}
356
Joe Thornber538c2bc2015-01-23 10:16:16 +0000357static struct dm_cache_migration *alloc_migration(struct cache *cache)
358{
359 struct dm_cache_migration *mg;
360
361 mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
362 if (mg) {
363 mg->cache = cache;
364 atomic_inc(&mg->cache->nr_allocated_migrations);
365 }
366
367 return mg;
368}
369
370static void free_migration(struct dm_cache_migration *mg)
371{
372 if (atomic_dec_and_test(&mg->cache->nr_allocated_migrations))
373 wake_up(&mg->cache->migration_wait);
374
375 mempool_free(mg, mg->cache->migration_pool);
376}
377
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000378static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
379{
380 if (!p->mg) {
Joe Thornber538c2bc2015-01-23 10:16:16 +0000381 p->mg = alloc_migration(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000382 if (!p->mg)
383 return -ENOMEM;
384 }
385
386 if (!p->cell1) {
387 p->cell1 = alloc_prison_cell(cache);
388 if (!p->cell1)
389 return -ENOMEM;
390 }
391
392 if (!p->cell2) {
393 p->cell2 = alloc_prison_cell(cache);
394 if (!p->cell2)
395 return -ENOMEM;
396 }
397
398 return 0;
399}
400
401static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
402{
403 if (p->cell2)
404 free_prison_cell(cache, p->cell2);
405
406 if (p->cell1)
407 free_prison_cell(cache, p->cell1);
408
409 if (p->mg)
Joe Thornber538c2bc2015-01-23 10:16:16 +0000410 free_migration(p->mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000411}
412
413static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
414{
415 struct dm_cache_migration *mg = p->mg;
416
417 BUG_ON(!mg);
418 p->mg = NULL;
419
420 return mg;
421}
422
423/*
424 * You must have a cell within the prealloc struct to return. If not this
425 * function will BUG() rather than returning NULL.
426 */
427static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
428{
429 struct dm_bio_prison_cell *r = NULL;
430
431 if (p->cell1) {
432 r = p->cell1;
433 p->cell1 = NULL;
434
435 } else if (p->cell2) {
436 r = p->cell2;
437 p->cell2 = NULL;
438 } else
439 BUG();
440
441 return r;
442}
443
444/*
445 * You can't have more than two cells in a prealloc struct. BUG() will be
446 * called if you try and overfill.
447 */
448static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
449{
450 if (!p->cell2)
451 p->cell2 = cell;
452
453 else if (!p->cell1)
454 p->cell1 = cell;
455
456 else
457 BUG();
458}
459
460/*----------------------------------------------------------------*/
461
462static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
463{
464 key->virtual = 0;
465 key->dev = 0;
466 key->block = from_oblock(oblock);
467}
468
469/*
470 * The caller hands in a preallocated cell, and a free function for it.
471 * The cell will be freed if there's an error, or if it wasn't used because
472 * a cell with that key already exists.
473 */
474typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
475
476static int bio_detain(struct cache *cache, dm_oblock_t oblock,
477 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
478 cell_free_fn free_fn, void *free_context,
479 struct dm_bio_prison_cell **cell_result)
480{
481 int r;
482 struct dm_cell_key key;
483
484 build_key(oblock, &key);
485 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
486 if (r)
487 free_fn(free_context, cell_prealloc);
488
489 return r;
490}
491
492static int get_cell(struct cache *cache,
493 dm_oblock_t oblock,
494 struct prealloc *structs,
495 struct dm_bio_prison_cell **cell_result)
496{
497 int r;
498 struct dm_cell_key key;
499 struct dm_bio_prison_cell *cell_prealloc;
500
501 cell_prealloc = prealloc_get_cell(structs);
502
503 build_key(oblock, &key);
504 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
505 if (r)
506 prealloc_put_cell(structs, cell_prealloc);
507
508 return r;
509}
510
Joe Thornberaeed1422013-05-10 14:37:18 +0100511/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000512
513static bool is_dirty(struct cache *cache, dm_cblock_t b)
514{
515 return test_bit(from_cblock(b), cache->dirty_bitset);
516}
517
518static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
519{
520 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
Anssi Hannula44fa8162014-08-01 11:55:47 -0400521 atomic_inc(&cache->nr_dirty);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000522 policy_set_dirty(cache->policy, oblock);
523 }
524}
525
526static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
527{
528 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
529 policy_clear_dirty(cache->policy, oblock);
Anssi Hannula44fa8162014-08-01 11:55:47 -0400530 if (atomic_dec_return(&cache->nr_dirty) == 0)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000531 dm_table_event(cache->ti->table);
532 }
533}
534
535/*----------------------------------------------------------------*/
Joe Thornberaeed1422013-05-10 14:37:18 +0100536
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000537static bool block_size_is_power_of_two(struct cache *cache)
538{
539 return cache->sectors_per_block_shift >= 0;
540}
541
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100542/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
543#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
544__always_inline
545#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000546static dm_block_t block_div(dm_block_t b, uint32_t n)
547{
548 do_div(b, n);
549
550 return b;
551}
552
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400553static void set_discard(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000554{
555 unsigned long flags;
556
557 atomic_inc(&cache->stats.discard_count);
558
559 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400560 set_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000561 spin_unlock_irqrestore(&cache->lock, flags);
562}
563
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400564static void clear_discard(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000565{
566 unsigned long flags;
567
568 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400569 clear_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000570 spin_unlock_irqrestore(&cache->lock, flags);
571}
572
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400573static bool is_discarded(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000574{
575 int r;
576 unsigned long flags;
577
578 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400579 r = test_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000580 spin_unlock_irqrestore(&cache->lock, flags);
581
582 return r;
583}
584
585static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
586{
587 int r;
588 unsigned long flags;
589
590 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400591 r = test_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000592 spin_unlock_irqrestore(&cache->lock, flags);
593
594 return r;
595}
596
597/*----------------------------------------------------------------*/
598
599static void load_stats(struct cache *cache)
600{
601 struct dm_cache_statistics stats;
602
603 dm_cache_metadata_get_stats(cache->cmd, &stats);
604 atomic_set(&cache->stats.read_hit, stats.read_hits);
605 atomic_set(&cache->stats.read_miss, stats.read_misses);
606 atomic_set(&cache->stats.write_hit, stats.write_hits);
607 atomic_set(&cache->stats.write_miss, stats.write_misses);
608}
609
610static void save_stats(struct cache *cache)
611{
612 struct dm_cache_statistics stats;
613
614 stats.read_hits = atomic_read(&cache->stats.read_hit);
615 stats.read_misses = atomic_read(&cache->stats.read_miss);
616 stats.write_hits = atomic_read(&cache->stats.write_hit);
617 stats.write_misses = atomic_read(&cache->stats.write_miss);
618
619 dm_cache_metadata_set_stats(cache->cmd, &stats);
620}
621
622/*----------------------------------------------------------------
623 * Per bio data
624 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100625
626/*
627 * If using writeback, leave out struct per_bio_data's writethrough fields.
628 */
629#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
630#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
631
Joe Thornber2ee57d52013-10-24 14:10:29 -0400632static bool writethrough_mode(struct cache_features *f)
633{
634 return f->io_mode == CM_IO_WRITETHROUGH;
635}
636
637static bool writeback_mode(struct cache_features *f)
638{
639 return f->io_mode == CM_IO_WRITEBACK;
640}
641
642static bool passthrough_mode(struct cache_features *f)
643{
644 return f->io_mode == CM_IO_PASSTHROUGH;
645}
646
Mike Snitzer19b00922013-04-05 15:36:34 +0100647static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000648{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400649 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100650}
651
652static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
653{
654 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000655 BUG_ON(!pb);
656 return pb;
657}
658
Mike Snitzer19b00922013-04-05 15:36:34 +0100659static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000660{
Mike Snitzer19b00922013-04-05 15:36:34 +0100661 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000662
663 pb->tick = false;
664 pb->req_nr = dm_bio_get_target_bio_nr(bio);
665 pb->all_io_entry = NULL;
666
667 return pb;
668}
669
670/*----------------------------------------------------------------
671 * Remapping
672 *--------------------------------------------------------------*/
673static void remap_to_origin(struct cache *cache, struct bio *bio)
674{
675 bio->bi_bdev = cache->origin_dev->bdev;
676}
677
678static void remap_to_cache(struct cache *cache, struct bio *bio,
679 dm_cblock_t cblock)
680{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700681 sector_t bi_sector = bio->bi_iter.bi_sector;
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100682 sector_t block = from_cblock(cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000683
684 bio->bi_bdev = cache->cache_dev->bdev;
685 if (!block_size_is_power_of_two(cache))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700686 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100687 (block * cache->sectors_per_block) +
Kent Overstreet4f024f32013-10-11 15:44:27 -0700688 sector_div(bi_sector, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000689 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700690 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100691 (block << cache->sectors_per_block_shift) |
Kent Overstreet4f024f32013-10-11 15:44:27 -0700692 (bi_sector & (cache->sectors_per_block - 1));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000693}
694
695static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
696{
697 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100698 size_t pb_data_size = get_per_bio_data_size(cache);
699 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000700
701 spin_lock_irqsave(&cache->lock, flags);
702 if (cache->need_tick_bio &&
703 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
704 pb->tick = true;
705 cache->need_tick_bio = false;
706 }
707 spin_unlock_irqrestore(&cache->lock, flags);
708}
709
710static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
711 dm_oblock_t oblock)
712{
713 check_if_tick_bio_needed(cache, bio);
714 remap_to_origin(cache, bio);
715 if (bio_data_dir(bio) == WRITE)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400716 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000717}
718
719static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
720 dm_oblock_t oblock, dm_cblock_t cblock)
721{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100722 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000723 remap_to_cache(cache, bio, cblock);
724 if (bio_data_dir(bio) == WRITE) {
725 set_dirty(cache, oblock, cblock);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400726 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000727 }
728}
729
730static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
731{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700732 sector_t block_nr = bio->bi_iter.bi_sector;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000733
734 if (!block_size_is_power_of_two(cache))
735 (void) sector_div(block_nr, cache->sectors_per_block);
736 else
737 block_nr >>= cache->sectors_per_block_shift;
738
739 return to_oblock(block_nr);
740}
741
742static int bio_triggers_commit(struct cache *cache, struct bio *bio)
743{
744 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
745}
746
Joe Thornber8c081b52014-05-13 16:18:38 +0100747/*
748 * You must increment the deferred set whilst the prison cell is held. To
749 * encourage this, we ask for 'cell' to be passed in.
750 */
751static void inc_ds(struct cache *cache, struct bio *bio,
752 struct dm_bio_prison_cell *cell)
753{
754 size_t pb_data_size = get_per_bio_data_size(cache);
755 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
756
757 BUG_ON(!cell);
758 BUG_ON(pb->all_io_entry);
759
760 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
761}
762
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000763static void issue(struct cache *cache, struct bio *bio)
764{
765 unsigned long flags;
766
767 if (!bio_triggers_commit(cache, bio)) {
768 generic_make_request(bio);
769 return;
770 }
771
772 /*
773 * Batch together any bios that trigger commits and then issue a
774 * single commit for them in do_worker().
775 */
776 spin_lock_irqsave(&cache->lock, flags);
777 cache->commit_requested = true;
778 bio_list_add(&cache->deferred_flush_bios, bio);
779 spin_unlock_irqrestore(&cache->lock, flags);
780}
781
Joe Thornber8c081b52014-05-13 16:18:38 +0100782static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
783{
784 inc_ds(cache, bio, cell);
785 issue(cache, bio);
786}
787
Joe Thornbere2e74d62013-03-20 17:21:27 +0000788static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
789{
790 unsigned long flags;
791
792 spin_lock_irqsave(&cache->lock, flags);
793 bio_list_add(&cache->deferred_writethrough_bios, bio);
794 spin_unlock_irqrestore(&cache->lock, flags);
795
796 wake_worker(cache);
797}
798
799static void writethrough_endio(struct bio *bio, int err)
800{
Mike Snitzer19b00922013-04-05 15:36:34 +0100801 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400802
803 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000804
805 if (err) {
806 bio_endio(bio, err);
807 return;
808 }
809
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100810 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000811 remap_to_cache(pb->cache, bio, pb->cblock);
812
813 /*
814 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed1422013-05-10 14:37:18 +0100815 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000816 * worker thread.
817 */
818 defer_writethrough_bio(pb->cache, bio);
819}
820
821/*
822 * When running in writethrough mode we need to send writes to clean blocks
823 * to both the cache and origin devices. In future we'd like to clone the
824 * bio and send them in parallel, but for now we're doing them in
825 * series as this is easier.
826 */
827static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
828 dm_oblock_t oblock, dm_cblock_t cblock)
829{
Mike Snitzer19b00922013-04-05 15:36:34 +0100830 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000831
832 pb->cache = cache;
833 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400834 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100835 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000836
837 remap_to_origin_clear_discard(pb->cache, bio, oblock);
838}
839
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000840/*----------------------------------------------------------------
841 * Migration processing
842 *
843 * Migration covers moving data from the origin device to the cache, or
844 * vice versa.
845 *--------------------------------------------------------------*/
Joe Thornber538c2bc2015-01-23 10:16:16 +0000846static void inc_io_migrations(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000847{
Joe Thornber538c2bc2015-01-23 10:16:16 +0000848 atomic_inc(&cache->nr_io_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000849}
850
Joe Thornber538c2bc2015-01-23 10:16:16 +0000851static void dec_io_migrations(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000852{
Joe Thornber538c2bc2015-01-23 10:16:16 +0000853 atomic_dec(&cache->nr_io_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000854}
855
856static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
857 bool holder)
858{
859 (holder ? dm_cell_release : dm_cell_release_no_holder)
860 (cache->prison, cell, &cache->deferred_bios);
861 free_prison_cell(cache, cell);
862}
863
864static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
865 bool holder)
866{
867 unsigned long flags;
868
869 spin_lock_irqsave(&cache->lock, flags);
870 __cell_defer(cache, cell, holder);
871 spin_unlock_irqrestore(&cache->lock, flags);
872
873 wake_worker(cache);
874}
875
Joe Thornber538c2bc2015-01-23 10:16:16 +0000876static void free_io_migration(struct dm_cache_migration *mg)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000877{
Joe Thornber538c2bc2015-01-23 10:16:16 +0000878 dec_io_migrations(mg->cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000879 free_migration(mg);
880}
881
882static void migration_failure(struct dm_cache_migration *mg)
883{
884 struct cache *cache = mg->cache;
885
886 if (mg->writeback) {
887 DMWARN_LIMIT("writeback failed; couldn't copy block");
888 set_dirty(cache, mg->old_oblock, mg->cblock);
889 cell_defer(cache, mg->old_ocell, false);
890
891 } else if (mg->demote) {
892 DMWARN_LIMIT("demotion failed; couldn't copy block");
893 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
894
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200895 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000896 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200897 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000898 } else {
899 DMWARN_LIMIT("promotion failed; couldn't copy block");
900 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200901 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000902 }
903
Joe Thornber538c2bc2015-01-23 10:16:16 +0000904 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000905}
906
907static void migration_success_pre_commit(struct dm_cache_migration *mg)
908{
909 unsigned long flags;
910 struct cache *cache = mg->cache;
911
912 if (mg->writeback) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000913 clear_dirty(cache, mg->old_oblock, mg->cblock);
Anssi Hannula40aa9782014-09-05 03:11:28 +0300914 cell_defer(cache, mg->old_ocell, false);
Joe Thornber538c2bc2015-01-23 10:16:16 +0000915 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000916 return;
917
918 } else if (mg->demote) {
919 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
920 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
921 policy_force_mapping(cache->policy, mg->new_oblock,
922 mg->old_oblock);
923 if (mg->promote)
924 cell_defer(cache, mg->new_ocell, true);
Joe Thornber538c2bc2015-01-23 10:16:16 +0000925 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000926 return;
927 }
928 } else {
929 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
930 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
931 policy_remove_mapping(cache->policy, mg->new_oblock);
Joe Thornber538c2bc2015-01-23 10:16:16 +0000932 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000933 return;
934 }
935 }
936
937 spin_lock_irqsave(&cache->lock, flags);
938 list_add_tail(&mg->list, &cache->need_commit_migrations);
939 cache->commit_requested = true;
940 spin_unlock_irqrestore(&cache->lock, flags);
941}
942
943static void migration_success_post_commit(struct dm_cache_migration *mg)
944{
945 unsigned long flags;
946 struct cache *cache = mg->cache;
947
948 if (mg->writeback) {
949 DMWARN("writeback unexpectedly triggered commit");
950 return;
951
952 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200953 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000954
955 if (mg->promote) {
956 mg->demote = false;
957
958 spin_lock_irqsave(&cache->lock, flags);
959 list_add_tail(&mg->list, &cache->quiesced_migrations);
960 spin_unlock_irqrestore(&cache->lock, flags);
961
Joe Thornber65790ff2013-11-08 16:39:50 +0000962 } else {
963 if (mg->invalidate)
964 policy_remove_mapping(cache->policy, mg->old_oblock);
Joe Thornber538c2bc2015-01-23 10:16:16 +0000965 free_io_migration(mg);
Joe Thornber65790ff2013-11-08 16:39:50 +0000966 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000967
968 } else {
Joe Thornber7a9cdc42014-11-27 12:26:46 +0000969 if (mg->requeue_holder) {
970 clear_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400971 cell_defer(cache, mg->new_ocell, true);
Joe Thornber7a9cdc42014-11-27 12:26:46 +0000972 } else {
973 /*
974 * The block was promoted via an overwrite, so it's dirty.
975 */
976 set_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400977 bio_endio(mg->new_ocell->holder, 0);
978 cell_defer(cache, mg->new_ocell, false);
979 }
Joe Thornber538c2bc2015-01-23 10:16:16 +0000980 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000981 }
982}
983
984static void copy_complete(int read_err, unsigned long write_err, void *context)
985{
986 unsigned long flags;
987 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
988 struct cache *cache = mg->cache;
989
990 if (read_err || write_err)
991 mg->err = true;
992
993 spin_lock_irqsave(&cache->lock, flags);
994 list_add_tail(&mg->list, &cache->completed_migrations);
995 spin_unlock_irqrestore(&cache->lock, flags);
996
997 wake_worker(cache);
998}
999
1000static void issue_copy_real(struct dm_cache_migration *mg)
1001{
1002 int r;
1003 struct dm_io_region o_region, c_region;
1004 struct cache *cache = mg->cache;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001005 sector_t cblock = from_cblock(mg->cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001006
1007 o_region.bdev = cache->origin_dev->bdev;
1008 o_region.count = cache->sectors_per_block;
1009
1010 c_region.bdev = cache->cache_dev->bdev;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001011 c_region.sector = cblock * cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001012 c_region.count = cache->sectors_per_block;
1013
1014 if (mg->writeback || mg->demote) {
1015 /* demote */
1016 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
1017 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
1018 } else {
1019 /* promote */
1020 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1021 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1022 }
1023
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001024 if (r < 0) {
1025 DMERR_LIMIT("issuing migration failed");
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001026 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001027 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001028}
1029
Joe Thornberc9d28d52013-10-31 13:55:48 -04001030static void overwrite_endio(struct bio *bio, int err)
1031{
1032 struct dm_cache_migration *mg = bio->bi_private;
1033 struct cache *cache = mg->cache;
1034 size_t pb_data_size = get_per_bio_data_size(cache);
1035 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1036 unsigned long flags;
1037
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001038 dm_unhook_bio(&pb->hook_info, bio);
1039
Joe Thornberc9d28d52013-10-31 13:55:48 -04001040 if (err)
1041 mg->err = true;
1042
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001043 mg->requeue_holder = false;
1044
Joe Thornberc9d28d52013-10-31 13:55:48 -04001045 spin_lock_irqsave(&cache->lock, flags);
1046 list_add_tail(&mg->list, &cache->completed_migrations);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001047 spin_unlock_irqrestore(&cache->lock, flags);
1048
1049 wake_worker(cache);
1050}
1051
1052static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1053{
1054 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1055 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1056
1057 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1058 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001059
1060 /*
1061 * No need to inc_ds() here, since the cell will be held for the
1062 * duration of the io.
1063 */
Joe Thornberc9d28d52013-10-31 13:55:48 -04001064 generic_make_request(bio);
1065}
1066
1067static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1068{
1069 return (bio_data_dir(bio) == WRITE) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001070 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
Joe Thornberc9d28d52013-10-31 13:55:48 -04001071}
1072
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001073static void avoid_copy(struct dm_cache_migration *mg)
1074{
1075 atomic_inc(&mg->cache->stats.copies_avoided);
1076 migration_success_pre_commit(mg);
1077}
1078
1079static void issue_copy(struct dm_cache_migration *mg)
1080{
1081 bool avoid;
1082 struct cache *cache = mg->cache;
1083
1084 if (mg->writeback || mg->demote)
1085 avoid = !is_dirty(cache, mg->cblock) ||
1086 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001087 else {
1088 struct bio *bio = mg->new_ocell->holder;
1089
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001090 avoid = is_discarded_oblock(cache, mg->new_oblock);
1091
Joe Thornber4df99e32014-11-27 12:21:08 +00001092 if (writeback_mode(&cache->features) &&
1093 !avoid && bio_writes_complete_block(cache, bio)) {
Joe Thornberc9d28d52013-10-31 13:55:48 -04001094 issue_overwrite(mg, bio);
1095 return;
1096 }
1097 }
1098
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001099 avoid ? avoid_copy(mg) : issue_copy_real(mg);
1100}
1101
1102static void complete_migration(struct dm_cache_migration *mg)
1103{
1104 if (mg->err)
1105 migration_failure(mg);
1106 else
1107 migration_success_pre_commit(mg);
1108}
1109
1110static void process_migrations(struct cache *cache, struct list_head *head,
1111 void (*fn)(struct dm_cache_migration *))
1112{
1113 unsigned long flags;
1114 struct list_head list;
1115 struct dm_cache_migration *mg, *tmp;
1116
1117 INIT_LIST_HEAD(&list);
1118 spin_lock_irqsave(&cache->lock, flags);
1119 list_splice_init(head, &list);
1120 spin_unlock_irqrestore(&cache->lock, flags);
1121
1122 list_for_each_entry_safe(mg, tmp, &list, list)
1123 fn(mg);
1124}
1125
1126static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1127{
1128 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1129}
1130
1131static void queue_quiesced_migration(struct dm_cache_migration *mg)
1132{
1133 unsigned long flags;
1134 struct cache *cache = mg->cache;
1135
1136 spin_lock_irqsave(&cache->lock, flags);
1137 __queue_quiesced_migration(mg);
1138 spin_unlock_irqrestore(&cache->lock, flags);
1139
1140 wake_worker(cache);
1141}
1142
1143static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1144{
1145 unsigned long flags;
1146 struct dm_cache_migration *mg, *tmp;
1147
1148 spin_lock_irqsave(&cache->lock, flags);
1149 list_for_each_entry_safe(mg, tmp, work, list)
1150 __queue_quiesced_migration(mg);
1151 spin_unlock_irqrestore(&cache->lock, flags);
1152
1153 wake_worker(cache);
1154}
1155
1156static void check_for_quiesced_migrations(struct cache *cache,
1157 struct per_bio_data *pb)
1158{
1159 struct list_head work;
1160
1161 if (!pb->all_io_entry)
1162 return;
1163
1164 INIT_LIST_HEAD(&work);
Joe Thornber8c081b52014-05-13 16:18:38 +01001165 dm_deferred_entry_dec(pb->all_io_entry, &work);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001166
1167 if (!list_empty(&work))
1168 queue_quiesced_migrations(cache, &work);
1169}
1170
1171static void quiesce_migration(struct dm_cache_migration *mg)
1172{
1173 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1174 queue_quiesced_migration(mg);
1175}
1176
1177static void promote(struct cache *cache, struct prealloc *structs,
1178 dm_oblock_t oblock, dm_cblock_t cblock,
1179 struct dm_bio_prison_cell *cell)
1180{
1181 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1182
1183 mg->err = false;
1184 mg->writeback = false;
1185 mg->demote = false;
1186 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001187 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001188 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001189 mg->cache = cache;
1190 mg->new_oblock = oblock;
1191 mg->cblock = cblock;
1192 mg->old_ocell = NULL;
1193 mg->new_ocell = cell;
1194 mg->start_jiffies = jiffies;
1195
Joe Thornber538c2bc2015-01-23 10:16:16 +00001196 inc_io_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001197 quiesce_migration(mg);
1198}
1199
1200static void writeback(struct cache *cache, struct prealloc *structs,
1201 dm_oblock_t oblock, dm_cblock_t cblock,
1202 struct dm_bio_prison_cell *cell)
1203{
1204 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1205
1206 mg->err = false;
1207 mg->writeback = true;
1208 mg->demote = false;
1209 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001210 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001211 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001212 mg->cache = cache;
1213 mg->old_oblock = oblock;
1214 mg->cblock = cblock;
1215 mg->old_ocell = cell;
1216 mg->new_ocell = NULL;
1217 mg->start_jiffies = jiffies;
1218
Joe Thornber538c2bc2015-01-23 10:16:16 +00001219 inc_io_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001220 quiesce_migration(mg);
1221}
1222
1223static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1224 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1225 dm_cblock_t cblock,
1226 struct dm_bio_prison_cell *old_ocell,
1227 struct dm_bio_prison_cell *new_ocell)
1228{
1229 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1230
1231 mg->err = false;
1232 mg->writeback = false;
1233 mg->demote = true;
1234 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001235 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001236 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001237 mg->cache = cache;
1238 mg->old_oblock = old_oblock;
1239 mg->new_oblock = new_oblock;
1240 mg->cblock = cblock;
1241 mg->old_ocell = old_ocell;
1242 mg->new_ocell = new_ocell;
1243 mg->start_jiffies = jiffies;
1244
Joe Thornber538c2bc2015-01-23 10:16:16 +00001245 inc_io_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001246 quiesce_migration(mg);
1247}
1248
Joe Thornber2ee57d52013-10-24 14:10:29 -04001249/*
1250 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1251 * block are thrown away.
1252 */
1253static void invalidate(struct cache *cache, struct prealloc *structs,
1254 dm_oblock_t oblock, dm_cblock_t cblock,
1255 struct dm_bio_prison_cell *cell)
1256{
1257 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1258
1259 mg->err = false;
1260 mg->writeback = false;
1261 mg->demote = true;
1262 mg->promote = false;
1263 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001264 mg->invalidate = true;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001265 mg->cache = cache;
1266 mg->old_oblock = oblock;
1267 mg->cblock = cblock;
1268 mg->old_ocell = cell;
1269 mg->new_ocell = NULL;
1270 mg->start_jiffies = jiffies;
1271
Joe Thornber538c2bc2015-01-23 10:16:16 +00001272 inc_io_migrations(cache);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001273 quiesce_migration(mg);
1274}
1275
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001276/*----------------------------------------------------------------
1277 * bio processing
1278 *--------------------------------------------------------------*/
1279static void defer_bio(struct cache *cache, struct bio *bio)
1280{
1281 unsigned long flags;
1282
1283 spin_lock_irqsave(&cache->lock, flags);
1284 bio_list_add(&cache->deferred_bios, bio);
1285 spin_unlock_irqrestore(&cache->lock, flags);
1286
1287 wake_worker(cache);
1288}
1289
1290static void process_flush_bio(struct cache *cache, struct bio *bio)
1291{
Mike Snitzer19b00922013-04-05 15:36:34 +01001292 size_t pb_data_size = get_per_bio_data_size(cache);
1293 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001294
Kent Overstreet4f024f32013-10-11 15:44:27 -07001295 BUG_ON(bio->bi_iter.bi_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001296 if (!pb->req_nr)
1297 remap_to_origin(cache, bio);
1298 else
1299 remap_to_cache(cache, bio, 0);
1300
Joe Thornber8c081b52014-05-13 16:18:38 +01001301 /*
1302 * REQ_FLUSH is not directed at any particular block so we don't
1303 * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH
1304 * by dm-core.
1305 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001306 issue(cache, bio);
1307}
1308
1309/*
1310 * People generally discard large parts of a device, eg, the whole device
1311 * when formatting. Splitting these large discards up into cache block
1312 * sized ios and then quiescing (always neccessary for discard) takes too
1313 * long.
1314 *
1315 * We keep it simple, and allow any size of discard to come in, and just
1316 * mark off blocks on the discard bitset. No passdown occurs!
1317 *
1318 * To implement passdown we need to change the bio_prison such that a cell
1319 * can have a key that spans many blocks.
1320 */
1321static void process_discard_bio(struct cache *cache, struct bio *bio)
1322{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001323 dm_block_t start_block = dm_sector_div_up(bio->bi_iter.bi_sector,
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001324 cache->sectors_per_block);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001325 dm_block_t end_block = bio_end_sector(bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001326 dm_block_t b;
1327
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001328 end_block = block_div(end_block, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001329
1330 for (b = start_block; b < end_block; b++)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001331 set_discard(cache, to_oblock(b));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001332
1333 bio_endio(bio, 0);
1334}
1335
1336static bool spare_migration_bandwidth(struct cache *cache)
1337{
Joe Thornber538c2bc2015-01-23 10:16:16 +00001338 sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001339 cache->sectors_per_block;
1340 return current_volume < cache->migration_threshold;
1341}
1342
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001343static void inc_hit_counter(struct cache *cache, struct bio *bio)
1344{
1345 atomic_inc(bio_data_dir(bio) == READ ?
1346 &cache->stats.read_hit : &cache->stats.write_hit);
1347}
1348
1349static void inc_miss_counter(struct cache *cache, struct bio *bio)
1350{
1351 atomic_inc(bio_data_dir(bio) == READ ?
1352 &cache->stats.read_miss : &cache->stats.write_miss);
1353}
1354
1355static void process_bio(struct cache *cache, struct prealloc *structs,
1356 struct bio *bio)
1357{
1358 int r;
1359 bool release_cell = true;
1360 dm_oblock_t block = get_bio_block(cache, bio);
1361 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1362 struct policy_result lookup_result;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001363 bool discarded_block = is_discarded_oblock(cache, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001364 bool passthrough = passthrough_mode(&cache->features);
1365 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001366
1367 /*
1368 * Check to see if that block is currently migrating.
1369 */
1370 cell_prealloc = prealloc_get_cell(structs);
1371 r = bio_detain(cache, block, bio, cell_prealloc,
1372 (cell_free_fn) prealloc_put_cell,
1373 structs, &new_ocell);
1374 if (r > 0)
1375 return;
1376
1377 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1378 bio, &lookup_result);
1379
1380 if (r == -EWOULDBLOCK)
1381 /* migration has been denied */
1382 lookup_result.op = POLICY_MISS;
1383
1384 switch (lookup_result.op) {
1385 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001386 if (passthrough) {
1387 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001388
Joe Thornber2ee57d52013-10-24 14:10:29 -04001389 /*
1390 * Passthrough always maps to the origin,
1391 * invalidating any cache blocks that are written
1392 * to.
1393 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001394
Joe Thornber2ee57d52013-10-24 14:10:29 -04001395 if (bio_data_dir(bio) == WRITE) {
1396 atomic_inc(&cache->stats.demotion);
1397 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1398 release_cell = false;
1399
1400 } else {
1401 /* FIXME: factor out issue_origin() */
Joe Thornber2ee57d52013-10-24 14:10:29 -04001402 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001403 inc_and_issue(cache, bio, new_ocell);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001404 }
1405 } else {
1406 inc_hit_counter(cache, bio);
1407
1408 if (bio_data_dir(bio) == WRITE &&
1409 writethrough_mode(&cache->features) &&
1410 !is_dirty(cache, lookup_result.cblock)) {
Joe Thornber2ee57d52013-10-24 14:10:29 -04001411 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001412 inc_and_issue(cache, bio, new_ocell);
1413
1414 } else {
1415 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1416 inc_and_issue(cache, bio, new_ocell);
1417 }
Joe Thornber2ee57d52013-10-24 14:10:29 -04001418 }
1419
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001420 break;
1421
1422 case POLICY_MISS:
1423 inc_miss_counter(cache, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001424 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001425 inc_and_issue(cache, bio, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001426 break;
1427
1428 case POLICY_NEW:
1429 atomic_inc(&cache->stats.promotion);
1430 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1431 release_cell = false;
1432 break;
1433
1434 case POLICY_REPLACE:
1435 cell_prealloc = prealloc_get_cell(structs);
1436 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1437 (cell_free_fn) prealloc_put_cell,
1438 structs, &old_ocell);
1439 if (r > 0) {
1440 /*
1441 * We have to be careful to avoid lock inversion of
1442 * the cells. So we back off, and wait for the
1443 * old_ocell to become free.
1444 */
1445 policy_force_mapping(cache->policy, block,
1446 lookup_result.old_oblock);
1447 atomic_inc(&cache->stats.cache_cell_clash);
1448 break;
1449 }
1450 atomic_inc(&cache->stats.demotion);
1451 atomic_inc(&cache->stats.promotion);
1452
1453 demote_then_promote(cache, structs, lookup_result.old_oblock,
1454 block, lookup_result.cblock,
1455 old_ocell, new_ocell);
1456 release_cell = false;
1457 break;
1458
1459 default:
1460 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1461 (unsigned) lookup_result.op);
1462 bio_io_error(bio);
1463 }
1464
1465 if (release_cell)
1466 cell_defer(cache, new_ocell, false);
1467}
1468
1469static int need_commit_due_to_time(struct cache *cache)
1470{
1471 return jiffies < cache->last_commit_jiffies ||
1472 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1473}
1474
1475static int commit_if_needed(struct cache *cache)
1476{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001477 int r = 0;
1478
1479 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1480 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001481 atomic_inc(&cache->stats.commit_count);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001482 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001483 r = dm_cache_commit(cache->cmd, false);
1484 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001485 }
1486
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001487 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001488}
1489
1490static void process_deferred_bios(struct cache *cache)
1491{
1492 unsigned long flags;
1493 struct bio_list bios;
1494 struct bio *bio;
1495 struct prealloc structs;
1496
1497 memset(&structs, 0, sizeof(structs));
1498 bio_list_init(&bios);
1499
1500 spin_lock_irqsave(&cache->lock, flags);
1501 bio_list_merge(&bios, &cache->deferred_bios);
1502 bio_list_init(&cache->deferred_bios);
1503 spin_unlock_irqrestore(&cache->lock, flags);
1504
1505 while (!bio_list_empty(&bios)) {
1506 /*
1507 * If we've got no free migration structs, and processing
1508 * this bio might require one, we pause until there are some
1509 * prepared mappings to process.
1510 */
1511 if (prealloc_data_structs(cache, &structs)) {
1512 spin_lock_irqsave(&cache->lock, flags);
1513 bio_list_merge(&cache->deferred_bios, &bios);
1514 spin_unlock_irqrestore(&cache->lock, flags);
1515 break;
1516 }
1517
1518 bio = bio_list_pop(&bios);
1519
1520 if (bio->bi_rw & REQ_FLUSH)
1521 process_flush_bio(cache, bio);
1522 else if (bio->bi_rw & REQ_DISCARD)
1523 process_discard_bio(cache, bio);
1524 else
1525 process_bio(cache, &structs, bio);
1526 }
1527
1528 prealloc_free_structs(cache, &structs);
1529}
1530
1531static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1532{
1533 unsigned long flags;
1534 struct bio_list bios;
1535 struct bio *bio;
1536
1537 bio_list_init(&bios);
1538
1539 spin_lock_irqsave(&cache->lock, flags);
1540 bio_list_merge(&bios, &cache->deferred_flush_bios);
1541 bio_list_init(&cache->deferred_flush_bios);
1542 spin_unlock_irqrestore(&cache->lock, flags);
1543
Joe Thornber8c081b52014-05-13 16:18:38 +01001544 /*
1545 * These bios have already been through inc_ds()
1546 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001547 while ((bio = bio_list_pop(&bios)))
1548 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1549}
1550
Joe Thornbere2e74d62013-03-20 17:21:27 +00001551static void process_deferred_writethrough_bios(struct cache *cache)
1552{
1553 unsigned long flags;
1554 struct bio_list bios;
1555 struct bio *bio;
1556
1557 bio_list_init(&bios);
1558
1559 spin_lock_irqsave(&cache->lock, flags);
1560 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1561 bio_list_init(&cache->deferred_writethrough_bios);
1562 spin_unlock_irqrestore(&cache->lock, flags);
1563
Joe Thornber8c081b52014-05-13 16:18:38 +01001564 /*
1565 * These bios have already been through inc_ds()
1566 */
Joe Thornbere2e74d62013-03-20 17:21:27 +00001567 while ((bio = bio_list_pop(&bios)))
1568 generic_make_request(bio);
1569}
1570
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001571static void writeback_some_dirty_blocks(struct cache *cache)
1572{
1573 int r = 0;
1574 dm_oblock_t oblock;
1575 dm_cblock_t cblock;
1576 struct prealloc structs;
1577 struct dm_bio_prison_cell *old_ocell;
1578
1579 memset(&structs, 0, sizeof(structs));
1580
1581 while (spare_migration_bandwidth(cache)) {
1582 if (prealloc_data_structs(cache, &structs))
1583 break;
1584
1585 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1586 if (r)
1587 break;
1588
1589 r = get_cell(cache, oblock, &structs, &old_ocell);
1590 if (r) {
1591 policy_set_dirty(cache->policy, oblock);
1592 break;
1593 }
1594
1595 writeback(cache, &structs, oblock, cblock, old_ocell);
1596 }
1597
1598 prealloc_free_structs(cache, &structs);
1599}
1600
1601/*----------------------------------------------------------------
Joe Thornber65790ff2013-11-08 16:39:50 +00001602 * Invalidations.
1603 * Dropping something from the cache *without* writing back.
1604 *--------------------------------------------------------------*/
1605
1606static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1607{
1608 int r = 0;
1609 uint64_t begin = from_cblock(req->cblocks->begin);
1610 uint64_t end = from_cblock(req->cblocks->end);
1611
1612 while (begin != end) {
1613 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1614 if (!r) {
1615 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1616 if (r)
1617 break;
1618
1619 } else if (r == -ENODATA) {
1620 /* harmless, already unmapped */
1621 r = 0;
1622
1623 } else {
1624 DMERR("policy_remove_cblock failed");
1625 break;
1626 }
1627
1628 begin++;
1629 }
1630
1631 cache->commit_requested = true;
1632
1633 req->err = r;
1634 atomic_set(&req->complete, 1);
1635
1636 wake_up(&req->result_wait);
1637}
1638
1639static void process_invalidation_requests(struct cache *cache)
1640{
1641 struct list_head list;
1642 struct invalidation_request *req, *tmp;
1643
1644 INIT_LIST_HEAD(&list);
1645 spin_lock(&cache->invalidation_lock);
1646 list_splice_init(&cache->invalidation_requests, &list);
1647 spin_unlock(&cache->invalidation_lock);
1648
1649 list_for_each_entry_safe (req, tmp, &list, list)
1650 process_invalidation_request(cache, req);
1651}
1652
1653/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001654 * Main worker loop
1655 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001656static bool is_quiescing(struct cache *cache)
1657{
Joe Thornber238f8362013-10-30 17:29:30 +00001658 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001659}
1660
Joe Thornber66cb1912013-10-30 17:11:58 +00001661static void ack_quiescing(struct cache *cache)
1662{
1663 if (is_quiescing(cache)) {
1664 atomic_inc(&cache->quiescing_ack);
1665 wake_up(&cache->quiescing_wait);
1666 }
1667}
1668
1669static void wait_for_quiescing_ack(struct cache *cache)
1670{
1671 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1672}
1673
1674static void start_quiescing(struct cache *cache)
1675{
Joe Thornber238f8362013-10-30 17:29:30 +00001676 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00001677 wait_for_quiescing_ack(cache);
1678}
1679
1680static void stop_quiescing(struct cache *cache)
1681{
Joe Thornber238f8362013-10-30 17:29:30 +00001682 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00001683 atomic_set(&cache->quiescing_ack, 0);
1684}
1685
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001686static void wait_for_migrations(struct cache *cache)
1687{
Joe Thornber538c2bc2015-01-23 10:16:16 +00001688 wait_event(cache->migration_wait, !atomic_read(&cache->nr_allocated_migrations));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001689}
1690
1691static void stop_worker(struct cache *cache)
1692{
1693 cancel_delayed_work(&cache->waker);
1694 flush_workqueue(cache->wq);
1695}
1696
1697static void requeue_deferred_io(struct cache *cache)
1698{
1699 struct bio *bio;
1700 struct bio_list bios;
1701
1702 bio_list_init(&bios);
1703 bio_list_merge(&bios, &cache->deferred_bios);
1704 bio_list_init(&cache->deferred_bios);
1705
1706 while ((bio = bio_list_pop(&bios)))
1707 bio_endio(bio, DM_ENDIO_REQUEUE);
1708}
1709
1710static int more_work(struct cache *cache)
1711{
1712 if (is_quiescing(cache))
1713 return !list_empty(&cache->quiesced_migrations) ||
1714 !list_empty(&cache->completed_migrations) ||
1715 !list_empty(&cache->need_commit_migrations);
1716 else
1717 return !bio_list_empty(&cache->deferred_bios) ||
1718 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001719 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001720 !list_empty(&cache->quiesced_migrations) ||
1721 !list_empty(&cache->completed_migrations) ||
Joe Thornber65790ff2013-11-08 16:39:50 +00001722 !list_empty(&cache->need_commit_migrations) ||
1723 cache->invalidate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001724}
1725
1726static void do_worker(struct work_struct *ws)
1727{
1728 struct cache *cache = container_of(ws, struct cache, worker);
1729
1730 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001731 if (!is_quiescing(cache)) {
1732 writeback_some_dirty_blocks(cache);
1733 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001734 process_deferred_bios(cache);
Joe Thornber65790ff2013-11-08 16:39:50 +00001735 process_invalidation_requests(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001736 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001737
1738 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1739 process_migrations(cache, &cache->completed_migrations, complete_migration);
1740
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001741 if (commit_if_needed(cache)) {
1742 process_deferred_flush_bios(cache, false);
Joe Thornber304affa2014-06-24 15:36:58 -04001743 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001744
1745 /*
1746 * FIXME: rollback metadata or just go into a
1747 * failure mode and error everything
1748 */
1749 } else {
1750 process_deferred_flush_bios(cache, true);
1751 process_migrations(cache, &cache->need_commit_migrations,
1752 migration_success_post_commit);
1753 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001754
1755 ack_quiescing(cache);
1756
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001757 } while (more_work(cache));
1758}
1759
1760/*
1761 * We want to commit periodically so that not too much
1762 * unwritten metadata builds up.
1763 */
1764static void do_waker(struct work_struct *ws)
1765{
1766 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001767 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001768 wake_worker(cache);
1769 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1770}
1771
1772/*----------------------------------------------------------------*/
1773
1774static int is_congested(struct dm_dev *dev, int bdi_bits)
1775{
1776 struct request_queue *q = bdev_get_queue(dev->bdev);
1777 return bdi_congested(&q->backing_dev_info, bdi_bits);
1778}
1779
1780static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1781{
1782 struct cache *cache = container_of(cb, struct cache, callbacks);
1783
1784 return is_congested(cache->origin_dev, bdi_bits) ||
1785 is_congested(cache->cache_dev, bdi_bits);
1786}
1787
1788/*----------------------------------------------------------------
1789 * Target methods
1790 *--------------------------------------------------------------*/
1791
1792/*
1793 * This function gets called on the error paths of the constructor, so we
1794 * have to cope with a partially initialised struct.
1795 */
1796static void destroy(struct cache *cache)
1797{
1798 unsigned i;
1799
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001800 if (cache->migration_pool)
1801 mempool_destroy(cache->migration_pool);
1802
1803 if (cache->all_io_ds)
1804 dm_deferred_set_destroy(cache->all_io_ds);
1805
1806 if (cache->prison)
1807 dm_bio_prison_destroy(cache->prison);
1808
1809 if (cache->wq)
1810 destroy_workqueue(cache->wq);
1811
1812 if (cache->dirty_bitset)
1813 free_bitset(cache->dirty_bitset);
1814
1815 if (cache->discard_bitset)
1816 free_bitset(cache->discard_bitset);
1817
1818 if (cache->copier)
1819 dm_kcopyd_client_destroy(cache->copier);
1820
1821 if (cache->cmd)
1822 dm_cache_metadata_close(cache->cmd);
1823
1824 if (cache->metadata_dev)
1825 dm_put_device(cache->ti, cache->metadata_dev);
1826
1827 if (cache->origin_dev)
1828 dm_put_device(cache->ti, cache->origin_dev);
1829
1830 if (cache->cache_dev)
1831 dm_put_device(cache->ti, cache->cache_dev);
1832
1833 if (cache->policy)
1834 dm_cache_policy_destroy(cache->policy);
1835
1836 for (i = 0; i < cache->nr_ctr_args ; i++)
1837 kfree(cache->ctr_args[i]);
1838 kfree(cache->ctr_args);
1839
1840 kfree(cache);
1841}
1842
1843static void cache_dtr(struct dm_target *ti)
1844{
1845 struct cache *cache = ti->private;
1846
1847 destroy(cache);
1848}
1849
1850static sector_t get_dev_size(struct dm_dev *dev)
1851{
1852 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1853}
1854
1855/*----------------------------------------------------------------*/
1856
1857/*
1858 * Construct a cache device mapping.
1859 *
1860 * cache <metadata dev> <cache dev> <origin dev> <block size>
1861 * <#feature args> [<feature arg>]*
1862 * <policy> <#policy args> [<policy arg>]*
1863 *
1864 * metadata dev : fast device holding the persistent metadata
1865 * cache dev : fast device holding cached data blocks
1866 * origin dev : slow device holding original data blocks
1867 * block size : cache unit size in sectors
1868 *
1869 * #feature args : number of feature arguments passed
1870 * feature args : writethrough. (The default is writeback.)
1871 *
1872 * policy : the replacement policy to use
1873 * #policy args : an even number of policy arguments corresponding
1874 * to key/value pairs passed to the policy
1875 * policy args : key/value pairs passed to the policy
1876 * E.g. 'sequential_threshold 1024'
1877 * See cache-policies.txt for details.
1878 *
1879 * Optional feature arguments are:
1880 * writethrough : write through caching that prohibits cache block
1881 * content from being different from origin block content.
1882 * Without this argument, the default behaviour is to write
1883 * back cache block contents later for performance reasons,
1884 * so they may differ from the corresponding origin blocks.
1885 */
1886struct cache_args {
1887 struct dm_target *ti;
1888
1889 struct dm_dev *metadata_dev;
1890
1891 struct dm_dev *cache_dev;
1892 sector_t cache_sectors;
1893
1894 struct dm_dev *origin_dev;
1895 sector_t origin_sectors;
1896
1897 uint32_t block_size;
1898
1899 const char *policy_name;
1900 int policy_argc;
1901 const char **policy_argv;
1902
1903 struct cache_features features;
1904};
1905
1906static void destroy_cache_args(struct cache_args *ca)
1907{
1908 if (ca->metadata_dev)
1909 dm_put_device(ca->ti, ca->metadata_dev);
1910
1911 if (ca->cache_dev)
1912 dm_put_device(ca->ti, ca->cache_dev);
1913
1914 if (ca->origin_dev)
1915 dm_put_device(ca->ti, ca->origin_dev);
1916
1917 kfree(ca);
1918}
1919
1920static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1921{
1922 if (!as->argc) {
1923 *error = "Insufficient args";
1924 return false;
1925 }
1926
1927 return true;
1928}
1929
1930static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1931 char **error)
1932{
1933 int r;
1934 sector_t metadata_dev_size;
1935 char b[BDEVNAME_SIZE];
1936
1937 if (!at_least_one_arg(as, error))
1938 return -EINVAL;
1939
1940 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1941 &ca->metadata_dev);
1942 if (r) {
1943 *error = "Error opening metadata device";
1944 return r;
1945 }
1946
1947 metadata_dev_size = get_dev_size(ca->metadata_dev);
1948 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1949 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1950 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1951
1952 return 0;
1953}
1954
1955static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1956 char **error)
1957{
1958 int r;
1959
1960 if (!at_least_one_arg(as, error))
1961 return -EINVAL;
1962
1963 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1964 &ca->cache_dev);
1965 if (r) {
1966 *error = "Error opening cache device";
1967 return r;
1968 }
1969 ca->cache_sectors = get_dev_size(ca->cache_dev);
1970
1971 return 0;
1972}
1973
1974static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1975 char **error)
1976{
1977 int r;
1978
1979 if (!at_least_one_arg(as, error))
1980 return -EINVAL;
1981
1982 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1983 &ca->origin_dev);
1984 if (r) {
1985 *error = "Error opening origin device";
1986 return r;
1987 }
1988
1989 ca->origin_sectors = get_dev_size(ca->origin_dev);
1990 if (ca->ti->len > ca->origin_sectors) {
1991 *error = "Device size larger than cached device";
1992 return -EINVAL;
1993 }
1994
1995 return 0;
1996}
1997
1998static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1999 char **error)
2000{
Mike Snitzer05473042013-08-16 10:54:19 -04002001 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002002
2003 if (!at_least_one_arg(as, error))
2004 return -EINVAL;
2005
Mike Snitzer05473042013-08-16 10:54:19 -04002006 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
2007 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2008 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2009 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002010 *error = "Invalid data block size";
2011 return -EINVAL;
2012 }
2013
Mike Snitzer05473042013-08-16 10:54:19 -04002014 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002015 *error = "Data block size is larger than the cache device";
2016 return -EINVAL;
2017 }
2018
Mike Snitzer05473042013-08-16 10:54:19 -04002019 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002020
2021 return 0;
2022}
2023
2024static void init_features(struct cache_features *cf)
2025{
2026 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04002027 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002028}
2029
2030static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2031 char **error)
2032{
2033 static struct dm_arg _args[] = {
2034 {0, 1, "Invalid number of cache feature arguments"},
2035 };
2036
2037 int r;
2038 unsigned argc;
2039 const char *arg;
2040 struct cache_features *cf = &ca->features;
2041
2042 init_features(cf);
2043
2044 r = dm_read_arg_group(_args, as, &argc, error);
2045 if (r)
2046 return -EINVAL;
2047
2048 while (argc--) {
2049 arg = dm_shift_arg(as);
2050
2051 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002052 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002053
2054 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002055 cf->io_mode = CM_IO_WRITETHROUGH;
2056
2057 else if (!strcasecmp(arg, "passthrough"))
2058 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002059
2060 else {
2061 *error = "Unrecognised cache feature requested";
2062 return -EINVAL;
2063 }
2064 }
2065
2066 return 0;
2067}
2068
2069static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2070 char **error)
2071{
2072 static struct dm_arg _args[] = {
2073 {0, 1024, "Invalid number of policy arguments"},
2074 };
2075
2076 int r;
2077
2078 if (!at_least_one_arg(as, error))
2079 return -EINVAL;
2080
2081 ca->policy_name = dm_shift_arg(as);
2082
2083 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2084 if (r)
2085 return -EINVAL;
2086
2087 ca->policy_argv = (const char **)as->argv;
2088 dm_consume_args(as, ca->policy_argc);
2089
2090 return 0;
2091}
2092
2093static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2094 char **error)
2095{
2096 int r;
2097 struct dm_arg_set as;
2098
2099 as.argc = argc;
2100 as.argv = argv;
2101
2102 r = parse_metadata_dev(ca, &as, error);
2103 if (r)
2104 return r;
2105
2106 r = parse_cache_dev(ca, &as, error);
2107 if (r)
2108 return r;
2109
2110 r = parse_origin_dev(ca, &as, error);
2111 if (r)
2112 return r;
2113
2114 r = parse_block_size(ca, &as, error);
2115 if (r)
2116 return r;
2117
2118 r = parse_features(ca, &as, error);
2119 if (r)
2120 return r;
2121
2122 r = parse_policy(ca, &as, error);
2123 if (r)
2124 return r;
2125
2126 return 0;
2127}
2128
2129/*----------------------------------------------------------------*/
2130
2131static struct kmem_cache *migration_cache;
2132
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002133#define NOT_CORE_OPTION 1
2134
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002135static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002136{
2137 unsigned long tmp;
2138
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002139 if (!strcasecmp(key, "migration_threshold")) {
2140 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002141 return -EINVAL;
2142
2143 cache->migration_threshold = tmp;
2144 return 0;
2145 }
2146
2147 return NOT_CORE_OPTION;
2148}
2149
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002150static int set_config_value(struct cache *cache, const char *key, const char *value)
2151{
2152 int r = process_config_option(cache, key, value);
2153
2154 if (r == NOT_CORE_OPTION)
2155 r = policy_set_config_value(cache->policy, key, value);
2156
2157 if (r)
2158 DMWARN("bad config value for %s: %s", key, value);
2159
2160 return r;
2161}
2162
2163static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002164{
2165 int r = 0;
2166
2167 if (argc & 1) {
2168 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2169 return -EINVAL;
2170 }
2171
2172 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002173 r = set_config_value(cache, argv[0], argv[1]);
2174 if (r)
2175 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002176
2177 argc -= 2;
2178 argv += 2;
2179 }
2180
2181 return r;
2182}
2183
2184static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2185 char **error)
2186{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002187 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2188 cache->cache_size,
2189 cache->origin_sectors,
2190 cache->sectors_per_block);
2191 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002192 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002193 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002194 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002195 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002196
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002197 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002198}
2199
Joe Thornberf8350da2013-05-10 14:37:16 +01002200#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002201
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002202static int cache_create(struct cache_args *ca, struct cache **result)
2203{
2204 int r = 0;
2205 char **error = &ca->ti->error;
2206 struct cache *cache;
2207 struct dm_target *ti = ca->ti;
2208 dm_block_t origin_blocks;
2209 struct dm_cache_metadata *cmd;
2210 bool may_format = ca->features.mode == CM_WRITE;
2211
2212 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2213 if (!cache)
2214 return -ENOMEM;
2215
2216 cache->ti = ca->ti;
2217 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002218 ti->num_flush_bios = 2;
2219 ti->flush_supported = true;
2220
2221 ti->num_discard_bios = 1;
2222 ti->discards_supported = true;
2223 ti->discard_zeroes_data_unsupported = true;
Heinz Mauelshagenf1daa8382014-05-23 14:10:01 -04002224 /* Discard bios must be split on a block boundary */
2225 ti->split_discard_bios = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002226
Joe Thornber8c5008f2013-05-10 14:37:18 +01002227 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01002228 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002229
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002230 cache->callbacks.congested_fn = cache_is_congested;
2231 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2232
2233 cache->metadata_dev = ca->metadata_dev;
2234 cache->origin_dev = ca->origin_dev;
2235 cache->cache_dev = ca->cache_dev;
2236
2237 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2238
2239 /* FIXME: factor out this whole section */
2240 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002241 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002242 cache->origin_blocks = to_oblock(origin_blocks);
2243
2244 cache->sectors_per_block = ca->block_size;
2245 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2246 r = -EINVAL;
2247 goto bad;
2248 }
2249
2250 if (ca->block_size & (ca->block_size - 1)) {
2251 dm_block_t cache_size = ca->cache_sectors;
2252
2253 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002254 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002255 cache->cache_size = to_cblock(cache_size);
2256 } else {
2257 cache->sectors_per_block_shift = __ffs(ca->block_size);
2258 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2259 }
2260
2261 r = create_cache_policy(cache, ca, error);
2262 if (r)
2263 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002264
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002265 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002266 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2267
2268 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2269 if (r) {
2270 *error = "Error setting cache policy's config values";
2271 goto bad;
2272 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002273
2274 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2275 ca->block_size, may_format,
2276 dm_cache_policy_get_hint_size(cache->policy));
2277 if (IS_ERR(cmd)) {
2278 *error = "Error creating metadata object";
2279 r = PTR_ERR(cmd);
2280 goto bad;
2281 }
2282 cache->cmd = cmd;
2283
Joe Thornber2ee57d52013-10-24 14:10:29 -04002284 if (passthrough_mode(&cache->features)) {
2285 bool all_clean;
2286
2287 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2288 if (r) {
2289 *error = "dm_cache_metadata_all_clean() failed";
2290 goto bad;
2291 }
2292
2293 if (!all_clean) {
2294 *error = "Cannot enter passthrough mode unless all blocks are clean";
2295 r = -EINVAL;
2296 goto bad;
2297 }
2298 }
2299
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002300 spin_lock_init(&cache->lock);
2301 bio_list_init(&cache->deferred_bios);
2302 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002303 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002304 INIT_LIST_HEAD(&cache->quiesced_migrations);
2305 INIT_LIST_HEAD(&cache->completed_migrations);
2306 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornber538c2bc2015-01-23 10:16:16 +00002307 atomic_set(&cache->nr_allocated_migrations, 0);
2308 atomic_set(&cache->nr_io_migrations, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002309 init_waitqueue_head(&cache->migration_wait);
2310
Joe Thornber66cb1912013-10-30 17:11:58 +00002311 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002312 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002313 atomic_set(&cache->quiescing_ack, 0);
2314
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002315 r = -ENOMEM;
Anssi Hannula44fa8162014-08-01 11:55:47 -04002316 atomic_set(&cache->nr_dirty, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002317 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2318 if (!cache->dirty_bitset) {
2319 *error = "could not allocate dirty bitset";
2320 goto bad;
2321 }
2322 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2323
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002324 cache->discard_nr_blocks = cache->origin_blocks;
2325 cache->discard_bitset = alloc_bitset(from_oblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002326 if (!cache->discard_bitset) {
2327 *error = "could not allocate discard bitset";
2328 goto bad;
2329 }
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002330 clear_bitset(cache->discard_bitset, from_oblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002331
2332 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2333 if (IS_ERR(cache->copier)) {
2334 *error = "could not create kcopyd client";
2335 r = PTR_ERR(cache->copier);
2336 goto bad;
2337 }
2338
2339 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2340 if (!cache->wq) {
2341 *error = "could not create workqueue for metadata object";
2342 goto bad;
2343 }
2344 INIT_WORK(&cache->worker, do_worker);
2345 INIT_DELAYED_WORK(&cache->waker, do_waker);
2346 cache->last_commit_jiffies = jiffies;
2347
2348 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2349 if (!cache->prison) {
2350 *error = "could not create bio prison";
2351 goto bad;
2352 }
2353
2354 cache->all_io_ds = dm_deferred_set_create();
2355 if (!cache->all_io_ds) {
2356 *error = "could not create all_io deferred set";
2357 goto bad;
2358 }
2359
2360 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2361 migration_cache);
2362 if (!cache->migration_pool) {
2363 *error = "Error creating cache's migration mempool";
2364 goto bad;
2365 }
2366
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002367 cache->need_tick_bio = true;
2368 cache->sized = false;
Joe Thornber65790ff2013-11-08 16:39:50 +00002369 cache->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002370 cache->commit_requested = false;
2371 cache->loaded_mappings = false;
2372 cache->loaded_discards = false;
2373
2374 load_stats(cache);
2375
2376 atomic_set(&cache->stats.demotion, 0);
2377 atomic_set(&cache->stats.promotion, 0);
2378 atomic_set(&cache->stats.copies_avoided, 0);
2379 atomic_set(&cache->stats.cache_cell_clash, 0);
2380 atomic_set(&cache->stats.commit_count, 0);
2381 atomic_set(&cache->stats.discard_count, 0);
2382
Joe Thornber65790ff2013-11-08 16:39:50 +00002383 spin_lock_init(&cache->invalidation_lock);
2384 INIT_LIST_HEAD(&cache->invalidation_requests);
2385
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002386 *result = cache;
2387 return 0;
2388
2389bad:
2390 destroy(cache);
2391 return r;
2392}
2393
2394static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2395{
2396 unsigned i;
2397 const char **copy;
2398
2399 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2400 if (!copy)
2401 return -ENOMEM;
2402 for (i = 0; i < argc; i++) {
2403 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2404 if (!copy[i]) {
2405 while (i--)
2406 kfree(copy[i]);
2407 kfree(copy);
2408 return -ENOMEM;
2409 }
2410 }
2411
2412 cache->nr_ctr_args = argc;
2413 cache->ctr_args = copy;
2414
2415 return 0;
2416}
2417
2418static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2419{
2420 int r = -EINVAL;
2421 struct cache_args *ca;
2422 struct cache *cache = NULL;
2423
2424 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2425 if (!ca) {
2426 ti->error = "Error allocating memory for cache";
2427 return -ENOMEM;
2428 }
2429 ca->ti = ti;
2430
2431 r = parse_cache_args(ca, argc, argv, &ti->error);
2432 if (r)
2433 goto out;
2434
2435 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002436 if (r)
2437 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002438
2439 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2440 if (r) {
2441 destroy(cache);
2442 goto out;
2443 }
2444
2445 ti->private = cache;
2446
2447out:
2448 destroy_cache_args(ca);
2449 return r;
2450}
2451
Joe Thornber8c081b52014-05-13 16:18:38 +01002452static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002453{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002454 int r;
2455 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002456 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002457 bool can_migrate = false;
2458 bool discarded_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002459 struct policy_result lookup_result;
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002460 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002461
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002462 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002463 /*
2464 * This can only occur if the io goes to a partial block at
2465 * the end of the origin device. We don't cache these.
2466 * Just remap to the origin and carry on.
2467 */
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002468 remap_to_origin(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002469 return DM_MAPIO_REMAPPED;
2470 }
2471
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002472 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2473 defer_bio(cache, bio);
2474 return DM_MAPIO_SUBMITTED;
2475 }
2476
2477 /*
2478 * Check to see if that block is currently migrating.
2479 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002480 *cell = alloc_prison_cell(cache);
2481 if (!*cell) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002482 defer_bio(cache, bio);
2483 return DM_MAPIO_SUBMITTED;
2484 }
2485
Joe Thornber8c081b52014-05-13 16:18:38 +01002486 r = bio_detain(cache, block, bio, *cell,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002487 (cell_free_fn) free_prison_cell,
Joe Thornber8c081b52014-05-13 16:18:38 +01002488 cache, cell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002489 if (r) {
2490 if (r < 0)
2491 defer_bio(cache, bio);
2492
2493 return DM_MAPIO_SUBMITTED;
2494 }
2495
2496 discarded_block = is_discarded_oblock(cache, block);
2497
2498 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2499 bio, &lookup_result);
2500 if (r == -EWOULDBLOCK) {
Joe Thornber8c081b52014-05-13 16:18:38 +01002501 cell_defer(cache, *cell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002502 return DM_MAPIO_SUBMITTED;
2503
2504 } else if (r) {
2505 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
Joe Thornber8c081b52014-05-13 16:18:38 +01002506 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002507 bio_io_error(bio);
2508 return DM_MAPIO_SUBMITTED;
2509 }
2510
Joe Thornber2ee57d52013-10-24 14:10:29 -04002511 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002512 switch (lookup_result.op) {
2513 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04002514 if (passthrough_mode(&cache->features)) {
2515 if (bio_data_dir(bio) == WRITE) {
2516 /*
2517 * We need to invalidate this block, so
2518 * defer for the worker thread.
2519 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002520 cell_defer(cache, *cell, true);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002521 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002522
Joe Thornber2ee57d52013-10-24 14:10:29 -04002523 } else {
Joe Thornber2ee57d52013-10-24 14:10:29 -04002524 inc_miss_counter(cache, bio);
2525 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002526 }
2527
2528 } else {
2529 inc_hit_counter(cache, bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002530 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2531 !is_dirty(cache, lookup_result.cblock))
2532 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2533 else
2534 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002535 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002536 break;
2537
2538 case POLICY_MISS:
2539 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002540 if (pb->req_nr != 0) {
2541 /*
2542 * This is a duplicate writethrough io that is no
2543 * longer needed because the block has been demoted.
2544 */
2545 bio_endio(bio, 0);
Joe Thornber8c081b52014-05-13 16:18:38 +01002546 cell_defer(cache, *cell, false);
2547 r = DM_MAPIO_SUBMITTED;
2548
2549 } else
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002550 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01002551
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002552 break;
2553
2554 default:
2555 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2556 (unsigned) lookup_result.op);
Joe Thornber8c081b52014-05-13 16:18:38 +01002557 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002558 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002559 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002560 }
2561
Joe Thornber2ee57d52013-10-24 14:10:29 -04002562 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002563}
2564
Joe Thornber8c081b52014-05-13 16:18:38 +01002565static int cache_map(struct dm_target *ti, struct bio *bio)
2566{
2567 int r;
Joe Thornber734a3fb2014-11-28 09:48:25 +00002568 struct dm_bio_prison_cell *cell = NULL;
Joe Thornber8c081b52014-05-13 16:18:38 +01002569 struct cache *cache = ti->private;
2570
2571 r = __cache_map(cache, bio, &cell);
Joe Thornber734a3fb2014-11-28 09:48:25 +00002572 if (r == DM_MAPIO_REMAPPED && cell) {
Joe Thornber8c081b52014-05-13 16:18:38 +01002573 inc_ds(cache, bio, cell);
2574 cell_defer(cache, cell, false);
2575 }
2576
2577 return r;
2578}
2579
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002580static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2581{
2582 struct cache *cache = ti->private;
2583 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002584 size_t pb_data_size = get_per_bio_data_size(cache);
2585 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002586
2587 if (pb->tick) {
2588 policy_tick(cache->policy);
2589
2590 spin_lock_irqsave(&cache->lock, flags);
2591 cache->need_tick_bio = true;
2592 spin_unlock_irqrestore(&cache->lock, flags);
2593 }
2594
2595 check_for_quiesced_migrations(cache, pb);
2596
2597 return 0;
2598}
2599
2600static int write_dirty_bitset(struct cache *cache)
2601{
2602 unsigned i, r;
2603
2604 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2605 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2606 is_dirty(cache, to_cblock(i)));
2607 if (r)
2608 return r;
2609 }
2610
2611 return 0;
2612}
2613
2614static int write_discard_bitset(struct cache *cache)
2615{
2616 unsigned i, r;
2617
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002618 r = dm_cache_discard_bitset_resize(cache->cmd, cache->sectors_per_block,
2619 cache->origin_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002620 if (r) {
2621 DMERR("could not resize on-disk discard bitset");
2622 return r;
2623 }
2624
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002625 for (i = 0; i < from_oblock(cache->discard_nr_blocks); i++) {
2626 r = dm_cache_set_discard(cache->cmd, to_oblock(i),
2627 is_discarded(cache, to_oblock(i)));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002628 if (r)
2629 return r;
2630 }
2631
2632 return 0;
2633}
2634
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002635/*
2636 * returns true on success
2637 */
2638static bool sync_metadata(struct cache *cache)
2639{
2640 int r1, r2, r3, r4;
2641
2642 r1 = write_dirty_bitset(cache);
2643 if (r1)
2644 DMERR("could not write dirty bitset");
2645
2646 r2 = write_discard_bitset(cache);
2647 if (r2)
2648 DMERR("could not write discard bitset");
2649
2650 save_stats(cache);
2651
Joe Thornber05966612014-04-03 16:16:44 +01002652 r3 = dm_cache_write_hints(cache->cmd, cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002653 if (r3)
2654 DMERR("could not write hints");
2655
2656 /*
2657 * If writing the above metadata failed, we still commit, but don't
2658 * set the clean shutdown flag. This will effectively force every
2659 * dirty bit to be set on reload.
2660 */
2661 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2662 if (r4)
2663 DMERR("could not write cache metadata. Data loss may occur.");
2664
2665 return !r1 && !r2 && !r3 && !r4;
2666}
2667
2668static void cache_postsuspend(struct dm_target *ti)
2669{
2670 struct cache *cache = ti->private;
2671
2672 start_quiescing(cache);
2673 wait_for_migrations(cache);
2674 stop_worker(cache);
2675 requeue_deferred_io(cache);
2676 stop_quiescing(cache);
2677
2678 (void) sync_metadata(cache);
2679}
2680
2681static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2682 bool dirty, uint32_t hint, bool hint_valid)
2683{
2684 int r;
2685 struct cache *cache = context;
2686
2687 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2688 if (r)
2689 return r;
2690
2691 if (dirty)
2692 set_dirty(cache, oblock, cblock);
2693 else
2694 clear_dirty(cache, oblock, cblock);
2695
2696 return 0;
2697}
2698
2699static int load_discard(void *context, sector_t discard_block_size,
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002700 dm_oblock_t oblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002701{
2702 struct cache *cache = context;
2703
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002704 if (discard)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002705 set_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002706 else
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002707 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002708
2709 return 0;
2710}
2711
Joe Thornberf494a9c2013-10-31 13:55:49 -04002712static dm_cblock_t get_cache_dev_size(struct cache *cache)
2713{
2714 sector_t size = get_dev_size(cache->cache_dev);
2715 (void) sector_div(size, cache->sectors_per_block);
2716 return to_cblock(size);
2717}
2718
2719static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2720{
2721 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2722 return true;
2723
2724 /*
2725 * We can't drop a dirty block when shrinking the cache.
2726 */
2727 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2728 new_size = to_cblock(from_cblock(new_size) + 1);
2729 if (is_dirty(cache, new_size)) {
2730 DMERR("unable to shrink cache; cache block %llu is dirty",
2731 (unsigned long long) from_cblock(new_size));
2732 return false;
2733 }
2734 }
2735
2736 return true;
2737}
2738
2739static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2740{
2741 int r;
2742
Vincent Pelletier08844802013-11-30 12:58:42 +01002743 r = dm_cache_resize(cache->cmd, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04002744 if (r) {
2745 DMERR("could not resize cache metadata");
2746 return r;
2747 }
2748
2749 cache->cache_size = new_size;
2750
2751 return 0;
2752}
2753
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002754static int cache_preresume(struct dm_target *ti)
2755{
2756 int r = 0;
2757 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002758 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002759
2760 /*
2761 * Check to see if the cache has resized.
2762 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04002763 if (!cache->sized) {
2764 r = resize_cache_dev(cache, csize);
2765 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002766 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002767
2768 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002769
2770 } else if (csize != cache->cache_size) {
2771 if (!can_resize(cache, csize))
2772 return -EINVAL;
2773
2774 r = resize_cache_dev(cache, csize);
2775 if (r)
2776 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002777 }
2778
2779 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002780 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002781 load_mapping, cache);
2782 if (r) {
2783 DMERR("could not load cache mappings");
2784 return r;
2785 }
2786
2787 cache->loaded_mappings = true;
2788 }
2789
2790 if (!cache->loaded_discards) {
2791 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2792 if (r) {
2793 DMERR("could not load origin discards");
2794 return r;
2795 }
2796
2797 cache->loaded_discards = true;
2798 }
2799
2800 return r;
2801}
2802
2803static void cache_resume(struct dm_target *ti)
2804{
2805 struct cache *cache = ti->private;
2806
2807 cache->need_tick_bio = true;
2808 do_waker(&cache->waker.work);
2809}
2810
2811/*
2812 * Status format:
2813 *
Mike Snitzer6a388612014-01-09 16:04:12 -05002814 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
2815 * <cache block size> <#used cache blocks>/<#total cache blocks>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002816 * <#read hits> <#read misses> <#write hits> <#write misses>
Mike Snitzer6a388612014-01-09 16:04:12 -05002817 * <#demotions> <#promotions> <#dirty>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002818 * <#features> <features>*
2819 * <#core args> <core args>
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002820 * <policy name> <#policy args> <policy args>*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002821 */
2822static void cache_status(struct dm_target *ti, status_type_t type,
2823 unsigned status_flags, char *result, unsigned maxlen)
2824{
2825 int r = 0;
2826 unsigned i;
2827 ssize_t sz = 0;
2828 dm_block_t nr_free_blocks_metadata = 0;
2829 dm_block_t nr_blocks_metadata = 0;
2830 char buf[BDEVNAME_SIZE];
2831 struct cache *cache = ti->private;
2832 dm_cblock_t residency;
2833
2834 switch (type) {
2835 case STATUSTYPE_INFO:
2836 /* Commit to ensure statistics aren't out-of-date */
2837 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2838 r = dm_cache_commit(cache->cmd, false);
2839 if (r)
2840 DMERR("could not commit metadata for accurate status");
2841 }
2842
2843 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2844 &nr_free_blocks_metadata);
2845 if (r) {
2846 DMERR("could not get metadata free block count");
2847 goto err;
2848 }
2849
2850 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2851 if (r) {
2852 DMERR("could not get metadata device size");
2853 goto err;
2854 }
2855
2856 residency = policy_residency(cache->policy);
2857
Anssi Hannula44fa8162014-08-01 11:55:47 -04002858 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
Mike Snitzer895b47d2014-07-14 15:37:18 -04002859 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002860 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2861 (unsigned long long)nr_blocks_metadata,
Mike Snitzer6a388612014-01-09 16:04:12 -05002862 cache->sectors_per_block,
2863 (unsigned long long) from_cblock(residency),
2864 (unsigned long long) from_cblock(cache->cache_size),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002865 (unsigned) atomic_read(&cache->stats.read_hit),
2866 (unsigned) atomic_read(&cache->stats.read_miss),
2867 (unsigned) atomic_read(&cache->stats.write_hit),
2868 (unsigned) atomic_read(&cache->stats.write_miss),
2869 (unsigned) atomic_read(&cache->stats.demotion),
2870 (unsigned) atomic_read(&cache->stats.promotion),
Anssi Hannula44fa8162014-08-01 11:55:47 -04002871 (unsigned long) atomic_read(&cache->nr_dirty));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002872
Joe Thornber2ee57d52013-10-24 14:10:29 -04002873 if (writethrough_mode(&cache->features))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002874 DMEMIT("1 writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04002875
2876 else if (passthrough_mode(&cache->features))
2877 DMEMIT("1 passthrough ");
2878
2879 else if (writeback_mode(&cache->features))
2880 DMEMIT("1 writeback ");
2881
2882 else {
2883 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2884 goto err;
2885 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002886
2887 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002888
2889 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002890 if (sz < maxlen) {
2891 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2892 if (r)
2893 DMERR("policy_emit_config_values returned %d", r);
2894 }
2895
2896 break;
2897
2898 case STATUSTYPE_TABLE:
2899 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2900 DMEMIT("%s ", buf);
2901 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2902 DMEMIT("%s ", buf);
2903 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2904 DMEMIT("%s", buf);
2905
2906 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2907 DMEMIT(" %s", cache->ctr_args[i]);
2908 if (cache->nr_ctr_args)
2909 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2910 }
2911
2912 return;
2913
2914err:
2915 DMEMIT("Error");
2916}
2917
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002918/*
Joe Thornber65790ff2013-11-08 16:39:50 +00002919 * A cache block range can take two forms:
2920 *
2921 * i) A single cblock, eg. '3456'
2922 * ii) A begin and end cblock with dots between, eg. 123-234
2923 */
2924static int parse_cblock_range(struct cache *cache, const char *str,
2925 struct cblock_range *result)
2926{
2927 char dummy;
2928 uint64_t b, e;
2929 int r;
2930
2931 /*
2932 * Try and parse form (ii) first.
2933 */
2934 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
2935 if (r < 0)
2936 return r;
2937
2938 if (r == 2) {
2939 result->begin = to_cblock(b);
2940 result->end = to_cblock(e);
2941 return 0;
2942 }
2943
2944 /*
2945 * That didn't work, try form (i).
2946 */
2947 r = sscanf(str, "%llu%c", &b, &dummy);
2948 if (r < 0)
2949 return r;
2950
2951 if (r == 1) {
2952 result->begin = to_cblock(b);
2953 result->end = to_cblock(from_cblock(result->begin) + 1u);
2954 return 0;
2955 }
2956
2957 DMERR("invalid cblock range '%s'", str);
2958 return -EINVAL;
2959}
2960
2961static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
2962{
2963 uint64_t b = from_cblock(range->begin);
2964 uint64_t e = from_cblock(range->end);
2965 uint64_t n = from_cblock(cache->cache_size);
2966
2967 if (b >= n) {
2968 DMERR("begin cblock out of range: %llu >= %llu", b, n);
2969 return -EINVAL;
2970 }
2971
2972 if (e > n) {
2973 DMERR("end cblock out of range: %llu > %llu", e, n);
2974 return -EINVAL;
2975 }
2976
2977 if (b >= e) {
2978 DMERR("invalid cblock range: %llu >= %llu", b, e);
2979 return -EINVAL;
2980 }
2981
2982 return 0;
2983}
2984
2985static int request_invalidation(struct cache *cache, struct cblock_range *range)
2986{
2987 struct invalidation_request req;
2988
2989 INIT_LIST_HEAD(&req.list);
2990 req.cblocks = range;
2991 atomic_set(&req.complete, 0);
2992 req.err = 0;
2993 init_waitqueue_head(&req.result_wait);
2994
2995 spin_lock(&cache->invalidation_lock);
2996 list_add(&req.list, &cache->invalidation_requests);
2997 spin_unlock(&cache->invalidation_lock);
2998 wake_worker(cache);
2999
3000 wait_event(req.result_wait, atomic_read(&req.complete));
3001 return req.err;
3002}
3003
3004static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
3005 const char **cblock_ranges)
3006{
3007 int r = 0;
3008 unsigned i;
3009 struct cblock_range range;
3010
3011 if (!passthrough_mode(&cache->features)) {
3012 DMERR("cache has to be in passthrough mode for invalidation");
3013 return -EPERM;
3014 }
3015
3016 for (i = 0; i < count; i++) {
3017 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3018 if (r)
3019 break;
3020
3021 r = validate_cblock_range(cache, &range);
3022 if (r)
3023 break;
3024
3025 /*
3026 * Pass begin and end origin blocks to the worker and wake it.
3027 */
3028 r = request_invalidation(cache, &range);
3029 if (r)
3030 break;
3031 }
3032
3033 return r;
3034}
3035
3036/*
3037 * Supports
3038 * "<key> <value>"
3039 * and
3040 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003041 *
3042 * The key migration_threshold is supported by the cache target core.
3043 */
3044static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3045{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003046 struct cache *cache = ti->private;
3047
Joe Thornber65790ff2013-11-08 16:39:50 +00003048 if (!argc)
3049 return -EINVAL;
3050
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -05003051 if (!strcasecmp(argv[0], "invalidate_cblocks"))
Joe Thornber65790ff2013-11-08 16:39:50 +00003052 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3053
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003054 if (argc != 2)
3055 return -EINVAL;
3056
Joe Thornber2f14f4b2013-05-10 14:37:21 +01003057 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003058}
3059
3060static int cache_iterate_devices(struct dm_target *ti,
3061 iterate_devices_callout_fn fn, void *data)
3062{
3063 int r = 0;
3064 struct cache *cache = ti->private;
3065
3066 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3067 if (!r)
3068 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3069
3070 return r;
3071}
3072
3073/*
3074 * We assume I/O is going to the origin (which is the volume
3075 * more likely to have restrictions e.g. by being striped).
3076 * (Looking up the exact location of the data would be expensive
3077 * and could always be out of date by the time the bio is submitted.)
3078 */
3079static int cache_bvec_merge(struct dm_target *ti,
3080 struct bvec_merge_data *bvm,
3081 struct bio_vec *biovec, int max_size)
3082{
3083 struct cache *cache = ti->private;
3084 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3085
3086 if (!q->merge_bvec_fn)
3087 return max_size;
3088
3089 bvm->bi_bdev = cache->origin_dev->bdev;
3090 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3091}
3092
3093static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3094{
3095 /*
3096 * FIXME: these limits may be incompatible with the cache device
3097 */
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04003098 limits->max_discard_sectors = cache->sectors_per_block;
3099 limits->discard_granularity = cache->sectors_per_block << SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003100}
3101
3102static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3103{
3104 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04003105 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003106
Mike Snitzerf6109372013-08-20 15:02:41 -04003107 /*
3108 * If the system-determined stacked limits are compatible with the
3109 * cache's blocksize (io_opt is a factor) do not override them.
3110 */
3111 if (io_opt_sectors < cache->sectors_per_block ||
3112 do_div(io_opt_sectors, cache->sectors_per_block)) {
Mike Snitzerb0246532014-07-19 13:25:46 -04003113 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf6109372013-08-20 15:02:41 -04003114 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3115 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003116 set_discard_limits(cache, limits);
3117}
3118
3119/*----------------------------------------------------------------*/
3120
3121static struct target_type cache_target = {
3122 .name = "cache",
Joe Thornber8c081b52014-05-13 16:18:38 +01003123 .version = {1, 5, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003124 .module = THIS_MODULE,
3125 .ctr = cache_ctr,
3126 .dtr = cache_dtr,
3127 .map = cache_map,
3128 .end_io = cache_end_io,
3129 .postsuspend = cache_postsuspend,
3130 .preresume = cache_preresume,
3131 .resume = cache_resume,
3132 .status = cache_status,
3133 .message = cache_message,
3134 .iterate_devices = cache_iterate_devices,
3135 .merge = cache_bvec_merge,
3136 .io_hints = cache_io_hints,
3137};
3138
3139static int __init dm_cache_init(void)
3140{
3141 int r;
3142
3143 r = dm_register_target(&cache_target);
3144 if (r) {
3145 DMERR("cache target registration failed: %d", r);
3146 return r;
3147 }
3148
3149 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3150 if (!migration_cache) {
3151 dm_unregister_target(&cache_target);
3152 return -ENOMEM;
3153 }
3154
3155 return 0;
3156}
3157
3158static void __exit dm_cache_exit(void)
3159{
3160 dm_unregister_target(&cache_target);
3161 kmem_cache_destroy(migration_cache);
3162}
3163
3164module_init(dm_cache_init);
3165module_exit(dm_cache_exit);
3166
3167MODULE_DESCRIPTION(DM_NAME " cache target");
3168MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3169MODULE_LICENSE("GPL");