blob: 69de8b43ca1287c3568e8cfad90e9ea37aecf246 [file] [log] [blame]
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001/*
2 * Copyright (C) 2012 Red Hat. All rights reserved.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm.h"
8#include "dm-bio-prison.h"
Darrick J. Wongb844fe62013-04-05 15:36:32 +01009#include "dm-bio-record.h"
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000010#include "dm-cache-metadata.h"
11
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
14#include <linux/init.h>
15#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19
20#define DM_MSG_PREFIX "cache"
21
22DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
23 "A percentage of time allocated for copying to and/or from cache");
24
25/*----------------------------------------------------------------*/
26
27/*
28 * Glossary:
29 *
30 * oblock: index of an origin block
31 * cblock: index of a cache block
32 * promotion: movement of a block from origin to cache
33 * demotion: movement of a block from cache to origin
34 * migration: movement of a block between the origin and cache device,
35 * either direction
36 */
37
38/*----------------------------------------------------------------*/
39
40static size_t bitset_size_in_bytes(unsigned nr_entries)
41{
42 return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
43}
44
45static unsigned long *alloc_bitset(unsigned nr_entries)
46{
47 size_t s = bitset_size_in_bytes(nr_entries);
48 return vzalloc(s);
49}
50
51static void clear_bitset(void *bitset, unsigned nr_entries)
52{
53 size_t s = bitset_size_in_bytes(nr_entries);
54 memset(bitset, 0, s);
55}
56
57static void free_bitset(unsigned long *bits)
58{
59 vfree(bits);
60}
61
62/*----------------------------------------------------------------*/
63
Joe Thornberc9d28d52013-10-31 13:55:48 -040064/*
65 * There are a couple of places where we let a bio run, but want to do some
66 * work before calling its endio function. We do this by temporarily
67 * changing the endio fn.
68 */
69struct dm_hook_info {
70 bio_end_io_t *bi_end_io;
71 void *bi_private;
72};
73
74static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
75 bio_end_io_t *bi_end_io, void *bi_private)
76{
77 h->bi_end_io = bio->bi_end_io;
78 h->bi_private = bio->bi_private;
79
80 bio->bi_end_io = bi_end_io;
81 bio->bi_private = bi_private;
82}
83
84static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
85{
86 bio->bi_end_io = h->bi_end_io;
87 bio->bi_private = h->bi_private;
Mike Snitzer8d307262013-12-03 19:16:04 -070088
89 /*
90 * Must bump bi_remaining to allow bio to complete with
91 * restored bi_end_io.
92 */
93 atomic_inc(&bio->bi_remaining);
Joe Thornberc9d28d52013-10-31 13:55:48 -040094}
95
96/*----------------------------------------------------------------*/
97
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000098#define MIGRATION_POOL_SIZE 128
99#define COMMIT_PERIOD HZ
100#define MIGRATION_COUNT_WINDOW 10
101
102/*
Mike Snitzer05473042013-08-16 10:54:19 -0400103 * The block size of the device holding cache data must be
104 * between 32KB and 1GB.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000105 */
106#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
Mike Snitzer05473042013-08-16 10:54:19 -0400107#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000108
109/*
110 * FIXME: the cache is read/write for the time being.
111 */
Joe Thornber2ee57d52013-10-24 14:10:29 -0400112enum cache_metadata_mode {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000113 CM_WRITE, /* metadata may be changed */
114 CM_READ_ONLY, /* metadata may not be changed */
115};
116
Joe Thornber2ee57d52013-10-24 14:10:29 -0400117enum cache_io_mode {
118 /*
119 * Data is written to cached blocks only. These blocks are marked
120 * dirty. If you lose the cache device you will lose data.
121 * Potential performance increase for both reads and writes.
122 */
123 CM_IO_WRITEBACK,
124
125 /*
126 * Data is written to both cache and origin. Blocks are never
127 * dirty. Potential performance benfit for reads only.
128 */
129 CM_IO_WRITETHROUGH,
130
131 /*
132 * A degraded mode useful for various cache coherency situations
133 * (eg, rolling back snapshots). Reads and writes always go to the
134 * origin. If a write goes to a cached oblock, then the cache
135 * block is invalidated.
136 */
137 CM_IO_PASSTHROUGH
138};
139
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000140struct cache_features {
Joe Thornber2ee57d52013-10-24 14:10:29 -0400141 enum cache_metadata_mode mode;
142 enum cache_io_mode io_mode;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000143};
144
145struct cache_stats {
146 atomic_t read_hit;
147 atomic_t read_miss;
148 atomic_t write_hit;
149 atomic_t write_miss;
150 atomic_t demotion;
151 atomic_t promotion;
152 atomic_t copies_avoided;
153 atomic_t cache_cell_clash;
154 atomic_t commit_count;
155 atomic_t discard_count;
156};
157
Joe Thornber65790ff2013-11-08 16:39:50 +0000158/*
159 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
160 * the one-past-the-end value.
161 */
162struct cblock_range {
163 dm_cblock_t begin;
164 dm_cblock_t end;
165};
166
167struct invalidation_request {
168 struct list_head list;
169 struct cblock_range *cblocks;
170
171 atomic_t complete;
172 int err;
173
174 wait_queue_head_t result_wait;
175};
176
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000177struct cache {
178 struct dm_target *ti;
179 struct dm_target_callbacks callbacks;
180
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400181 struct dm_cache_metadata *cmd;
182
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000183 /*
184 * Metadata is written to this device.
185 */
186 struct dm_dev *metadata_dev;
187
188 /*
189 * The slower of the two data devices. Typically a spindle.
190 */
191 struct dm_dev *origin_dev;
192
193 /*
194 * The faster of the two data devices. Typically an SSD.
195 */
196 struct dm_dev *cache_dev;
197
198 /*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000199 * Size of the origin device in _complete_ blocks and native sectors.
200 */
201 dm_oblock_t origin_blocks;
202 sector_t origin_sectors;
203
204 /*
205 * Size of the cache device in blocks.
206 */
207 dm_cblock_t cache_size;
208
209 /*
210 * Fields for converting from sectors to blocks.
211 */
212 uint32_t sectors_per_block;
213 int sectors_per_block_shift;
214
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000215 spinlock_t lock;
216 struct bio_list deferred_bios;
217 struct bio_list deferred_flush_bios;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000218 struct bio_list deferred_writethrough_bios;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000219 struct list_head quiesced_migrations;
220 struct list_head completed_migrations;
221 struct list_head need_commit_migrations;
222 sector_t migration_threshold;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000223 wait_queue_head_t migration_wait;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400224 atomic_t nr_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000225
Joe Thornber66cb1912013-10-30 17:11:58 +0000226 wait_queue_head_t quiescing_wait;
Joe Thornber238f8362013-10-30 17:29:30 +0000227 atomic_t quiescing;
Joe Thornber66cb1912013-10-30 17:11:58 +0000228 atomic_t quiescing_ack;
229
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000230 /*
231 * cache_size entries, dirty if set
232 */
Anssi Hannula44fa8162014-08-01 11:55:47 -0400233 atomic_t nr_dirty;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000234 unsigned long *dirty_bitset;
235
236 /*
237 * origin_blocks entries, discarded if set.
238 */
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400239 dm_oblock_t discard_nr_blocks;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000240 unsigned long *discard_bitset;
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400241
242 /*
243 * Rather than reconstructing the table line for the status we just
244 * save it and regurgitate.
245 */
246 unsigned nr_ctr_args;
247 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000248
249 struct dm_kcopyd_client *copier;
250 struct workqueue_struct *wq;
251 struct work_struct worker;
252
253 struct delayed_work waker;
254 unsigned long last_commit_jiffies;
255
256 struct dm_bio_prison *prison;
257 struct dm_deferred_set *all_io_ds;
258
259 mempool_t *migration_pool;
260 struct dm_cache_migration *next_migration;
261
262 struct dm_cache_policy *policy;
263 unsigned policy_nr_args;
264
265 bool need_tick_bio:1;
266 bool sized:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000267 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000268 bool commit_requested:1;
269 bool loaded_mappings:1;
270 bool loaded_discards:1;
271
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000272 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400273 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000274 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400275 struct cache_features features;
276
277 struct cache_stats stats;
Joe Thornber65790ff2013-11-08 16:39:50 +0000278
279 /*
280 * Invalidation fields.
281 */
282 spinlock_t invalidation_lock;
283 struct list_head invalidation_requests;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000284};
285
286struct per_bio_data {
287 bool tick:1;
288 unsigned req_nr:2;
289 struct dm_deferred_entry *all_io_entry;
Mike Snitzerc6eda5e2014-01-31 14:11:54 -0500290 struct dm_hook_info hook_info;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000291
Mike Snitzer19b00922013-04-05 15:36:34 +0100292 /*
293 * writethrough fields. These MUST remain at the end of this
294 * structure and the 'cache' member must be the first as it
Joe Thornberaeed14202013-05-10 14:37:18 +0100295 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100296 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000297 struct cache *cache;
298 dm_cblock_t cblock;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100299 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000300};
301
302struct dm_cache_migration {
303 struct list_head list;
304 struct cache *cache;
305
306 unsigned long start_jiffies;
307 dm_oblock_t old_oblock;
308 dm_oblock_t new_oblock;
309 dm_cblock_t cblock;
310
311 bool err:1;
312 bool writeback:1;
313 bool demote:1;
314 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400315 bool requeue_holder:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000316 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000317
318 struct dm_bio_prison_cell *old_ocell;
319 struct dm_bio_prison_cell *new_ocell;
320};
321
322/*
323 * Processing a bio in the worker thread may require these memory
324 * allocations. We prealloc to avoid deadlocks (the same worker thread
325 * frees them back to the mempool).
326 */
327struct prealloc {
328 struct dm_cache_migration *mg;
329 struct dm_bio_prison_cell *cell1;
330 struct dm_bio_prison_cell *cell2;
331};
332
333static void wake_worker(struct cache *cache)
334{
335 queue_work(cache->wq, &cache->worker);
336}
337
338/*----------------------------------------------------------------*/
339
340static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
341{
342 /* FIXME: change to use a local slab. */
343 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
344}
345
346static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
347{
348 dm_bio_prison_free_cell(cache->prison, cell);
349}
350
351static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
352{
353 if (!p->mg) {
354 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
355 if (!p->mg)
356 return -ENOMEM;
357 }
358
359 if (!p->cell1) {
360 p->cell1 = alloc_prison_cell(cache);
361 if (!p->cell1)
362 return -ENOMEM;
363 }
364
365 if (!p->cell2) {
366 p->cell2 = alloc_prison_cell(cache);
367 if (!p->cell2)
368 return -ENOMEM;
369 }
370
371 return 0;
372}
373
374static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
375{
376 if (p->cell2)
377 free_prison_cell(cache, p->cell2);
378
379 if (p->cell1)
380 free_prison_cell(cache, p->cell1);
381
382 if (p->mg)
383 mempool_free(p->mg, cache->migration_pool);
384}
385
386static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
387{
388 struct dm_cache_migration *mg = p->mg;
389
390 BUG_ON(!mg);
391 p->mg = NULL;
392
393 return mg;
394}
395
396/*
397 * You must have a cell within the prealloc struct to return. If not this
398 * function will BUG() rather than returning NULL.
399 */
400static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
401{
402 struct dm_bio_prison_cell *r = NULL;
403
404 if (p->cell1) {
405 r = p->cell1;
406 p->cell1 = NULL;
407
408 } else if (p->cell2) {
409 r = p->cell2;
410 p->cell2 = NULL;
411 } else
412 BUG();
413
414 return r;
415}
416
417/*
418 * You can't have more than two cells in a prealloc struct. BUG() will be
419 * called if you try and overfill.
420 */
421static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
422{
423 if (!p->cell2)
424 p->cell2 = cell;
425
426 else if (!p->cell1)
427 p->cell1 = cell;
428
429 else
430 BUG();
431}
432
433/*----------------------------------------------------------------*/
434
435static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
436{
437 key->virtual = 0;
438 key->dev = 0;
439 key->block = from_oblock(oblock);
440}
441
442/*
443 * The caller hands in a preallocated cell, and a free function for it.
444 * The cell will be freed if there's an error, or if it wasn't used because
445 * a cell with that key already exists.
446 */
447typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
448
449static int bio_detain(struct cache *cache, dm_oblock_t oblock,
450 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
451 cell_free_fn free_fn, void *free_context,
452 struct dm_bio_prison_cell **cell_result)
453{
454 int r;
455 struct dm_cell_key key;
456
457 build_key(oblock, &key);
458 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
459 if (r)
460 free_fn(free_context, cell_prealloc);
461
462 return r;
463}
464
465static int get_cell(struct cache *cache,
466 dm_oblock_t oblock,
467 struct prealloc *structs,
468 struct dm_bio_prison_cell **cell_result)
469{
470 int r;
471 struct dm_cell_key key;
472 struct dm_bio_prison_cell *cell_prealloc;
473
474 cell_prealloc = prealloc_get_cell(structs);
475
476 build_key(oblock, &key);
477 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
478 if (r)
479 prealloc_put_cell(structs, cell_prealloc);
480
481 return r;
482}
483
Joe Thornberaeed14202013-05-10 14:37:18 +0100484/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000485
486static bool is_dirty(struct cache *cache, dm_cblock_t b)
487{
488 return test_bit(from_cblock(b), cache->dirty_bitset);
489}
490
491static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
492{
493 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
Anssi Hannula44fa8162014-08-01 11:55:47 -0400494 atomic_inc(&cache->nr_dirty);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000495 policy_set_dirty(cache->policy, oblock);
496 }
497}
498
499static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
500{
501 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
502 policy_clear_dirty(cache->policy, oblock);
Anssi Hannula44fa8162014-08-01 11:55:47 -0400503 if (atomic_dec_return(&cache->nr_dirty) == 0)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000504 dm_table_event(cache->ti->table);
505 }
506}
507
508/*----------------------------------------------------------------*/
Joe Thornberaeed14202013-05-10 14:37:18 +0100509
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000510static bool block_size_is_power_of_two(struct cache *cache)
511{
512 return cache->sectors_per_block_shift >= 0;
513}
514
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100515/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
516#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
517__always_inline
518#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000519static dm_block_t block_div(dm_block_t b, uint32_t n)
520{
521 do_div(b, n);
522
523 return b;
524}
525
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400526static void set_discard(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000527{
528 unsigned long flags;
529
530 atomic_inc(&cache->stats.discard_count);
531
532 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400533 set_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000534 spin_unlock_irqrestore(&cache->lock, flags);
535}
536
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400537static void clear_discard(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000538{
539 unsigned long flags;
540
541 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400542 clear_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000543 spin_unlock_irqrestore(&cache->lock, flags);
544}
545
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400546static bool is_discarded(struct cache *cache, dm_oblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000547{
548 int r;
549 unsigned long flags;
550
551 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400552 r = test_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000553 spin_unlock_irqrestore(&cache->lock, flags);
554
555 return r;
556}
557
558static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
559{
560 int r;
561 unsigned long flags;
562
563 spin_lock_irqsave(&cache->lock, flags);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400564 r = test_bit(from_oblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000565 spin_unlock_irqrestore(&cache->lock, flags);
566
567 return r;
568}
569
570/*----------------------------------------------------------------*/
571
572static void load_stats(struct cache *cache)
573{
574 struct dm_cache_statistics stats;
575
576 dm_cache_metadata_get_stats(cache->cmd, &stats);
577 atomic_set(&cache->stats.read_hit, stats.read_hits);
578 atomic_set(&cache->stats.read_miss, stats.read_misses);
579 atomic_set(&cache->stats.write_hit, stats.write_hits);
580 atomic_set(&cache->stats.write_miss, stats.write_misses);
581}
582
583static void save_stats(struct cache *cache)
584{
585 struct dm_cache_statistics stats;
586
587 stats.read_hits = atomic_read(&cache->stats.read_hit);
588 stats.read_misses = atomic_read(&cache->stats.read_miss);
589 stats.write_hits = atomic_read(&cache->stats.write_hit);
590 stats.write_misses = atomic_read(&cache->stats.write_miss);
591
592 dm_cache_metadata_set_stats(cache->cmd, &stats);
593}
594
595/*----------------------------------------------------------------
596 * Per bio data
597 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100598
599/*
600 * If using writeback, leave out struct per_bio_data's writethrough fields.
601 */
602#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
603#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
604
Joe Thornber2ee57d52013-10-24 14:10:29 -0400605static bool writethrough_mode(struct cache_features *f)
606{
607 return f->io_mode == CM_IO_WRITETHROUGH;
608}
609
610static bool writeback_mode(struct cache_features *f)
611{
612 return f->io_mode == CM_IO_WRITEBACK;
613}
614
615static bool passthrough_mode(struct cache_features *f)
616{
617 return f->io_mode == CM_IO_PASSTHROUGH;
618}
619
Mike Snitzer19b00922013-04-05 15:36:34 +0100620static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000621{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400622 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100623}
624
625static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
626{
627 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000628 BUG_ON(!pb);
629 return pb;
630}
631
Mike Snitzer19b00922013-04-05 15:36:34 +0100632static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000633{
Mike Snitzer19b00922013-04-05 15:36:34 +0100634 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000635
636 pb->tick = false;
637 pb->req_nr = dm_bio_get_target_bio_nr(bio);
638 pb->all_io_entry = NULL;
639
640 return pb;
641}
642
643/*----------------------------------------------------------------
644 * Remapping
645 *--------------------------------------------------------------*/
646static void remap_to_origin(struct cache *cache, struct bio *bio)
647{
648 bio->bi_bdev = cache->origin_dev->bdev;
649}
650
651static void remap_to_cache(struct cache *cache, struct bio *bio,
652 dm_cblock_t cblock)
653{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700654 sector_t bi_sector = bio->bi_iter.bi_sector;
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100655 sector_t block = from_cblock(cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000656
657 bio->bi_bdev = cache->cache_dev->bdev;
658 if (!block_size_is_power_of_two(cache))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700659 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100660 (block * cache->sectors_per_block) +
Kent Overstreet4f024f32013-10-11 15:44:27 -0700661 sector_div(bi_sector, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000662 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700663 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100664 (block << cache->sectors_per_block_shift) |
Kent Overstreet4f024f32013-10-11 15:44:27 -0700665 (bi_sector & (cache->sectors_per_block - 1));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000666}
667
668static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
669{
670 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100671 size_t pb_data_size = get_per_bio_data_size(cache);
672 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000673
674 spin_lock_irqsave(&cache->lock, flags);
675 if (cache->need_tick_bio &&
676 !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
677 pb->tick = true;
678 cache->need_tick_bio = false;
679 }
680 spin_unlock_irqrestore(&cache->lock, flags);
681}
682
683static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
684 dm_oblock_t oblock)
685{
686 check_if_tick_bio_needed(cache, bio);
687 remap_to_origin(cache, bio);
688 if (bio_data_dir(bio) == WRITE)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400689 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000690}
691
692static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
693 dm_oblock_t oblock, dm_cblock_t cblock)
694{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100695 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000696 remap_to_cache(cache, bio, cblock);
697 if (bio_data_dir(bio) == WRITE) {
698 set_dirty(cache, oblock, cblock);
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -0400699 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000700 }
701}
702
703static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
704{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700705 sector_t block_nr = bio->bi_iter.bi_sector;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000706
707 if (!block_size_is_power_of_two(cache))
708 (void) sector_div(block_nr, cache->sectors_per_block);
709 else
710 block_nr >>= cache->sectors_per_block_shift;
711
712 return to_oblock(block_nr);
713}
714
715static int bio_triggers_commit(struct cache *cache, struct bio *bio)
716{
717 return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
718}
719
Joe Thornber8c081b52014-05-13 16:18:38 +0100720/*
721 * You must increment the deferred set whilst the prison cell is held. To
722 * encourage this, we ask for 'cell' to be passed in.
723 */
724static void inc_ds(struct cache *cache, struct bio *bio,
725 struct dm_bio_prison_cell *cell)
726{
727 size_t pb_data_size = get_per_bio_data_size(cache);
728 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
729
730 BUG_ON(!cell);
731 BUG_ON(pb->all_io_entry);
732
733 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
734}
735
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000736static void issue(struct cache *cache, struct bio *bio)
737{
738 unsigned long flags;
739
740 if (!bio_triggers_commit(cache, bio)) {
741 generic_make_request(bio);
742 return;
743 }
744
745 /*
746 * Batch together any bios that trigger commits and then issue a
747 * single commit for them in do_worker().
748 */
749 spin_lock_irqsave(&cache->lock, flags);
750 cache->commit_requested = true;
751 bio_list_add(&cache->deferred_flush_bios, bio);
752 spin_unlock_irqrestore(&cache->lock, flags);
753}
754
Joe Thornber8c081b52014-05-13 16:18:38 +0100755static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
756{
757 inc_ds(cache, bio, cell);
758 issue(cache, bio);
759}
760
Joe Thornbere2e74d62013-03-20 17:21:27 +0000761static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
762{
763 unsigned long flags;
764
765 spin_lock_irqsave(&cache->lock, flags);
766 bio_list_add(&cache->deferred_writethrough_bios, bio);
767 spin_unlock_irqrestore(&cache->lock, flags);
768
769 wake_worker(cache);
770}
771
772static void writethrough_endio(struct bio *bio, int err)
773{
Mike Snitzer19b00922013-04-05 15:36:34 +0100774 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400775
776 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000777
778 if (err) {
779 bio_endio(bio, err);
780 return;
781 }
782
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100783 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000784 remap_to_cache(pb->cache, bio, pb->cblock);
785
786 /*
787 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed14202013-05-10 14:37:18 +0100788 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000789 * worker thread.
790 */
791 defer_writethrough_bio(pb->cache, bio);
792}
793
794/*
795 * When running in writethrough mode we need to send writes to clean blocks
796 * to both the cache and origin devices. In future we'd like to clone the
797 * bio and send them in parallel, but for now we're doing them in
798 * series as this is easier.
799 */
800static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
801 dm_oblock_t oblock, dm_cblock_t cblock)
802{
Mike Snitzer19b00922013-04-05 15:36:34 +0100803 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000804
805 pb->cache = cache;
806 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400807 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100808 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000809
810 remap_to_origin_clear_discard(pb->cache, bio, oblock);
811}
812
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000813/*----------------------------------------------------------------
814 * Migration processing
815 *
816 * Migration covers moving data from the origin device to the cache, or
817 * vice versa.
818 *--------------------------------------------------------------*/
819static void free_migration(struct dm_cache_migration *mg)
820{
821 mempool_free(mg, mg->cache->migration_pool);
822}
823
824static void inc_nr_migrations(struct cache *cache)
825{
826 atomic_inc(&cache->nr_migrations);
827}
828
829static void dec_nr_migrations(struct cache *cache)
830{
831 atomic_dec(&cache->nr_migrations);
832
833 /*
834 * Wake the worker in case we're suspending the target.
835 */
836 wake_up(&cache->migration_wait);
837}
838
839static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
840 bool holder)
841{
842 (holder ? dm_cell_release : dm_cell_release_no_holder)
843 (cache->prison, cell, &cache->deferred_bios);
844 free_prison_cell(cache, cell);
845}
846
847static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
848 bool holder)
849{
850 unsigned long flags;
851
852 spin_lock_irqsave(&cache->lock, flags);
853 __cell_defer(cache, cell, holder);
854 spin_unlock_irqrestore(&cache->lock, flags);
855
856 wake_worker(cache);
857}
858
859static void cleanup_migration(struct dm_cache_migration *mg)
860{
Joe Thornber66cb1912013-10-30 17:11:58 +0000861 struct cache *cache = mg->cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000862 free_migration(mg);
Joe Thornber66cb1912013-10-30 17:11:58 +0000863 dec_nr_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000864}
865
866static void migration_failure(struct dm_cache_migration *mg)
867{
868 struct cache *cache = mg->cache;
869
870 if (mg->writeback) {
871 DMWARN_LIMIT("writeback failed; couldn't copy block");
872 set_dirty(cache, mg->old_oblock, mg->cblock);
873 cell_defer(cache, mg->old_ocell, false);
874
875 } else if (mg->demote) {
876 DMWARN_LIMIT("demotion failed; couldn't copy block");
877 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
878
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200879 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000880 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200881 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000882 } else {
883 DMWARN_LIMIT("promotion failed; couldn't copy block");
884 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200885 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000886 }
887
888 cleanup_migration(mg);
889}
890
891static void migration_success_pre_commit(struct dm_cache_migration *mg)
892{
893 unsigned long flags;
894 struct cache *cache = mg->cache;
895
896 if (mg->writeback) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000897 clear_dirty(cache, mg->old_oblock, mg->cblock);
Anssi Hannula40aa9782014-09-05 03:11:28 +0300898 cell_defer(cache, mg->old_ocell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000899 cleanup_migration(mg);
900 return;
901
902 } else if (mg->demote) {
903 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
904 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
905 policy_force_mapping(cache->policy, mg->new_oblock,
906 mg->old_oblock);
907 if (mg->promote)
908 cell_defer(cache, mg->new_ocell, true);
909 cleanup_migration(mg);
910 return;
911 }
912 } else {
913 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
914 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
915 policy_remove_mapping(cache->policy, mg->new_oblock);
916 cleanup_migration(mg);
917 return;
918 }
919 }
920
921 spin_lock_irqsave(&cache->lock, flags);
922 list_add_tail(&mg->list, &cache->need_commit_migrations);
923 cache->commit_requested = true;
924 spin_unlock_irqrestore(&cache->lock, flags);
925}
926
927static void migration_success_post_commit(struct dm_cache_migration *mg)
928{
929 unsigned long flags;
930 struct cache *cache = mg->cache;
931
932 if (mg->writeback) {
933 DMWARN("writeback unexpectedly triggered commit");
934 return;
935
936 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +0200937 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000938
939 if (mg->promote) {
940 mg->demote = false;
941
942 spin_lock_irqsave(&cache->lock, flags);
943 list_add_tail(&mg->list, &cache->quiesced_migrations);
944 spin_unlock_irqrestore(&cache->lock, flags);
945
Joe Thornber65790ff2013-11-08 16:39:50 +0000946 } else {
947 if (mg->invalidate)
948 policy_remove_mapping(cache->policy, mg->old_oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000949 cleanup_migration(mg);
Joe Thornber65790ff2013-11-08 16:39:50 +0000950 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000951
952 } else {
Anssi Hannula40aa9782014-09-05 03:11:28 +0300953 clear_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400954 if (mg->requeue_holder)
955 cell_defer(cache, mg->new_ocell, true);
956 else {
957 bio_endio(mg->new_ocell->holder, 0);
958 cell_defer(cache, mg->new_ocell, false);
959 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000960 cleanup_migration(mg);
961 }
962}
963
964static void copy_complete(int read_err, unsigned long write_err, void *context)
965{
966 unsigned long flags;
967 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
968 struct cache *cache = mg->cache;
969
970 if (read_err || write_err)
971 mg->err = true;
972
973 spin_lock_irqsave(&cache->lock, flags);
974 list_add_tail(&mg->list, &cache->completed_migrations);
975 spin_unlock_irqrestore(&cache->lock, flags);
976
977 wake_worker(cache);
978}
979
980static void issue_copy_real(struct dm_cache_migration *mg)
981{
982 int r;
983 struct dm_io_region o_region, c_region;
984 struct cache *cache = mg->cache;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +0100985 sector_t cblock = from_cblock(mg->cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000986
987 o_region.bdev = cache->origin_dev->bdev;
988 o_region.count = cache->sectors_per_block;
989
990 c_region.bdev = cache->cache_dev->bdev;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +0100991 c_region.sector = cblock * cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000992 c_region.count = cache->sectors_per_block;
993
994 if (mg->writeback || mg->demote) {
995 /* demote */
996 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
997 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
998 } else {
999 /* promote */
1000 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1001 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1002 }
1003
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001004 if (r < 0) {
1005 DMERR_LIMIT("issuing migration failed");
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001006 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001007 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001008}
1009
Joe Thornberc9d28d52013-10-31 13:55:48 -04001010static void overwrite_endio(struct bio *bio, int err)
1011{
1012 struct dm_cache_migration *mg = bio->bi_private;
1013 struct cache *cache = mg->cache;
1014 size_t pb_data_size = get_per_bio_data_size(cache);
1015 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1016 unsigned long flags;
1017
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001018 dm_unhook_bio(&pb->hook_info, bio);
1019
Joe Thornberc9d28d52013-10-31 13:55:48 -04001020 if (err)
1021 mg->err = true;
1022
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001023 mg->requeue_holder = false;
1024
Joe Thornberc9d28d52013-10-31 13:55:48 -04001025 spin_lock_irqsave(&cache->lock, flags);
1026 list_add_tail(&mg->list, &cache->completed_migrations);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001027 spin_unlock_irqrestore(&cache->lock, flags);
1028
1029 wake_worker(cache);
1030}
1031
1032static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1033{
1034 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1035 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1036
1037 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1038 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001039
1040 /*
1041 * No need to inc_ds() here, since the cell will be held for the
1042 * duration of the io.
1043 */
Joe Thornberc9d28d52013-10-31 13:55:48 -04001044 generic_make_request(bio);
1045}
1046
1047static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1048{
1049 return (bio_data_dir(bio) == WRITE) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001050 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
Joe Thornberc9d28d52013-10-31 13:55:48 -04001051}
1052
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001053static void avoid_copy(struct dm_cache_migration *mg)
1054{
1055 atomic_inc(&mg->cache->stats.copies_avoided);
1056 migration_success_pre_commit(mg);
1057}
1058
1059static void issue_copy(struct dm_cache_migration *mg)
1060{
1061 bool avoid;
1062 struct cache *cache = mg->cache;
1063
1064 if (mg->writeback || mg->demote)
1065 avoid = !is_dirty(cache, mg->cblock) ||
1066 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001067 else {
1068 struct bio *bio = mg->new_ocell->holder;
1069
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001070 avoid = is_discarded_oblock(cache, mg->new_oblock);
1071
Joe Thornberc9d28d52013-10-31 13:55:48 -04001072 if (!avoid && bio_writes_complete_block(cache, bio)) {
1073 issue_overwrite(mg, bio);
1074 return;
1075 }
1076 }
1077
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001078 avoid ? avoid_copy(mg) : issue_copy_real(mg);
1079}
1080
1081static void complete_migration(struct dm_cache_migration *mg)
1082{
1083 if (mg->err)
1084 migration_failure(mg);
1085 else
1086 migration_success_pre_commit(mg);
1087}
1088
1089static void process_migrations(struct cache *cache, struct list_head *head,
1090 void (*fn)(struct dm_cache_migration *))
1091{
1092 unsigned long flags;
1093 struct list_head list;
1094 struct dm_cache_migration *mg, *tmp;
1095
1096 INIT_LIST_HEAD(&list);
1097 spin_lock_irqsave(&cache->lock, flags);
1098 list_splice_init(head, &list);
1099 spin_unlock_irqrestore(&cache->lock, flags);
1100
1101 list_for_each_entry_safe(mg, tmp, &list, list)
1102 fn(mg);
1103}
1104
1105static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1106{
1107 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1108}
1109
1110static void queue_quiesced_migration(struct dm_cache_migration *mg)
1111{
1112 unsigned long flags;
1113 struct cache *cache = mg->cache;
1114
1115 spin_lock_irqsave(&cache->lock, flags);
1116 __queue_quiesced_migration(mg);
1117 spin_unlock_irqrestore(&cache->lock, flags);
1118
1119 wake_worker(cache);
1120}
1121
1122static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1123{
1124 unsigned long flags;
1125 struct dm_cache_migration *mg, *tmp;
1126
1127 spin_lock_irqsave(&cache->lock, flags);
1128 list_for_each_entry_safe(mg, tmp, work, list)
1129 __queue_quiesced_migration(mg);
1130 spin_unlock_irqrestore(&cache->lock, flags);
1131
1132 wake_worker(cache);
1133}
1134
1135static void check_for_quiesced_migrations(struct cache *cache,
1136 struct per_bio_data *pb)
1137{
1138 struct list_head work;
1139
1140 if (!pb->all_io_entry)
1141 return;
1142
1143 INIT_LIST_HEAD(&work);
Joe Thornber8c081b52014-05-13 16:18:38 +01001144 dm_deferred_entry_dec(pb->all_io_entry, &work);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001145
1146 if (!list_empty(&work))
1147 queue_quiesced_migrations(cache, &work);
1148}
1149
1150static void quiesce_migration(struct dm_cache_migration *mg)
1151{
1152 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1153 queue_quiesced_migration(mg);
1154}
1155
1156static void promote(struct cache *cache, struct prealloc *structs,
1157 dm_oblock_t oblock, dm_cblock_t cblock,
1158 struct dm_bio_prison_cell *cell)
1159{
1160 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1161
1162 mg->err = false;
1163 mg->writeback = false;
1164 mg->demote = false;
1165 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001166 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001167 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001168 mg->cache = cache;
1169 mg->new_oblock = oblock;
1170 mg->cblock = cblock;
1171 mg->old_ocell = NULL;
1172 mg->new_ocell = cell;
1173 mg->start_jiffies = jiffies;
1174
1175 inc_nr_migrations(cache);
1176 quiesce_migration(mg);
1177}
1178
1179static void writeback(struct cache *cache, struct prealloc *structs,
1180 dm_oblock_t oblock, dm_cblock_t cblock,
1181 struct dm_bio_prison_cell *cell)
1182{
1183 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1184
1185 mg->err = false;
1186 mg->writeback = true;
1187 mg->demote = false;
1188 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001189 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001190 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001191 mg->cache = cache;
1192 mg->old_oblock = oblock;
1193 mg->cblock = cblock;
1194 mg->old_ocell = cell;
1195 mg->new_ocell = NULL;
1196 mg->start_jiffies = jiffies;
1197
1198 inc_nr_migrations(cache);
1199 quiesce_migration(mg);
1200}
1201
1202static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1203 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1204 dm_cblock_t cblock,
1205 struct dm_bio_prison_cell *old_ocell,
1206 struct dm_bio_prison_cell *new_ocell)
1207{
1208 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1209
1210 mg->err = false;
1211 mg->writeback = false;
1212 mg->demote = true;
1213 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001214 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001215 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001216 mg->cache = cache;
1217 mg->old_oblock = old_oblock;
1218 mg->new_oblock = new_oblock;
1219 mg->cblock = cblock;
1220 mg->old_ocell = old_ocell;
1221 mg->new_ocell = new_ocell;
1222 mg->start_jiffies = jiffies;
1223
1224 inc_nr_migrations(cache);
1225 quiesce_migration(mg);
1226}
1227
Joe Thornber2ee57d52013-10-24 14:10:29 -04001228/*
1229 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1230 * block are thrown away.
1231 */
1232static void invalidate(struct cache *cache, struct prealloc *structs,
1233 dm_oblock_t oblock, dm_cblock_t cblock,
1234 struct dm_bio_prison_cell *cell)
1235{
1236 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1237
1238 mg->err = false;
1239 mg->writeback = false;
1240 mg->demote = true;
1241 mg->promote = false;
1242 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001243 mg->invalidate = true;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001244 mg->cache = cache;
1245 mg->old_oblock = oblock;
1246 mg->cblock = cblock;
1247 mg->old_ocell = cell;
1248 mg->new_ocell = NULL;
1249 mg->start_jiffies = jiffies;
1250
1251 inc_nr_migrations(cache);
1252 quiesce_migration(mg);
1253}
1254
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001255/*----------------------------------------------------------------
1256 * bio processing
1257 *--------------------------------------------------------------*/
1258static void defer_bio(struct cache *cache, struct bio *bio)
1259{
1260 unsigned long flags;
1261
1262 spin_lock_irqsave(&cache->lock, flags);
1263 bio_list_add(&cache->deferred_bios, bio);
1264 spin_unlock_irqrestore(&cache->lock, flags);
1265
1266 wake_worker(cache);
1267}
1268
1269static void process_flush_bio(struct cache *cache, struct bio *bio)
1270{
Mike Snitzer19b00922013-04-05 15:36:34 +01001271 size_t pb_data_size = get_per_bio_data_size(cache);
1272 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001273
Kent Overstreet4f024f32013-10-11 15:44:27 -07001274 BUG_ON(bio->bi_iter.bi_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001275 if (!pb->req_nr)
1276 remap_to_origin(cache, bio);
1277 else
1278 remap_to_cache(cache, bio, 0);
1279
Joe Thornber8c081b52014-05-13 16:18:38 +01001280 /*
1281 * REQ_FLUSH is not directed at any particular block so we don't
1282 * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH
1283 * by dm-core.
1284 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001285 issue(cache, bio);
1286}
1287
1288/*
1289 * People generally discard large parts of a device, eg, the whole device
1290 * when formatting. Splitting these large discards up into cache block
1291 * sized ios and then quiescing (always neccessary for discard) takes too
1292 * long.
1293 *
1294 * We keep it simple, and allow any size of discard to come in, and just
1295 * mark off blocks on the discard bitset. No passdown occurs!
1296 *
1297 * To implement passdown we need to change the bio_prison such that a cell
1298 * can have a key that spans many blocks.
1299 */
1300static void process_discard_bio(struct cache *cache, struct bio *bio)
1301{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001302 dm_block_t start_block = dm_sector_div_up(bio->bi_iter.bi_sector,
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001303 cache->sectors_per_block);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001304 dm_block_t end_block = bio_end_sector(bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001305 dm_block_t b;
1306
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001307 end_block = block_div(end_block, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001308
1309 for (b = start_block; b < end_block; b++)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04001310 set_discard(cache, to_oblock(b));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001311
1312 bio_endio(bio, 0);
1313}
1314
1315static bool spare_migration_bandwidth(struct cache *cache)
1316{
1317 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1318 cache->sectors_per_block;
1319 return current_volume < cache->migration_threshold;
1320}
1321
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001322static void inc_hit_counter(struct cache *cache, struct bio *bio)
1323{
1324 atomic_inc(bio_data_dir(bio) == READ ?
1325 &cache->stats.read_hit : &cache->stats.write_hit);
1326}
1327
1328static void inc_miss_counter(struct cache *cache, struct bio *bio)
1329{
1330 atomic_inc(bio_data_dir(bio) == READ ?
1331 &cache->stats.read_miss : &cache->stats.write_miss);
1332}
1333
1334static void process_bio(struct cache *cache, struct prealloc *structs,
1335 struct bio *bio)
1336{
1337 int r;
1338 bool release_cell = true;
1339 dm_oblock_t block = get_bio_block(cache, bio);
1340 struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
1341 struct policy_result lookup_result;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001342 bool discarded_block = is_discarded_oblock(cache, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001343 bool passthrough = passthrough_mode(&cache->features);
1344 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001345
1346 /*
1347 * Check to see if that block is currently migrating.
1348 */
1349 cell_prealloc = prealloc_get_cell(structs);
1350 r = bio_detain(cache, block, bio, cell_prealloc,
1351 (cell_free_fn) prealloc_put_cell,
1352 structs, &new_ocell);
1353 if (r > 0)
1354 return;
1355
1356 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1357 bio, &lookup_result);
1358
1359 if (r == -EWOULDBLOCK)
1360 /* migration has been denied */
1361 lookup_result.op = POLICY_MISS;
1362
1363 switch (lookup_result.op) {
1364 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001365 if (passthrough) {
1366 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001367
Joe Thornber2ee57d52013-10-24 14:10:29 -04001368 /*
1369 * Passthrough always maps to the origin,
1370 * invalidating any cache blocks that are written
1371 * to.
1372 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001373
Joe Thornber2ee57d52013-10-24 14:10:29 -04001374 if (bio_data_dir(bio) == WRITE) {
1375 atomic_inc(&cache->stats.demotion);
1376 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1377 release_cell = false;
1378
1379 } else {
1380 /* FIXME: factor out issue_origin() */
Joe Thornber2ee57d52013-10-24 14:10:29 -04001381 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001382 inc_and_issue(cache, bio, new_ocell);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001383 }
1384 } else {
1385 inc_hit_counter(cache, bio);
1386
1387 if (bio_data_dir(bio) == WRITE &&
1388 writethrough_mode(&cache->features) &&
1389 !is_dirty(cache, lookup_result.cblock)) {
Joe Thornber2ee57d52013-10-24 14:10:29 -04001390 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001391 inc_and_issue(cache, bio, new_ocell);
1392
1393 } else {
1394 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1395 inc_and_issue(cache, bio, new_ocell);
1396 }
Joe Thornber2ee57d52013-10-24 14:10:29 -04001397 }
1398
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001399 break;
1400
1401 case POLICY_MISS:
1402 inc_miss_counter(cache, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +00001403 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001404 inc_and_issue(cache, bio, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001405 break;
1406
1407 case POLICY_NEW:
1408 atomic_inc(&cache->stats.promotion);
1409 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1410 release_cell = false;
1411 break;
1412
1413 case POLICY_REPLACE:
1414 cell_prealloc = prealloc_get_cell(structs);
1415 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1416 (cell_free_fn) prealloc_put_cell,
1417 structs, &old_ocell);
1418 if (r > 0) {
1419 /*
1420 * We have to be careful to avoid lock inversion of
1421 * the cells. So we back off, and wait for the
1422 * old_ocell to become free.
1423 */
1424 policy_force_mapping(cache->policy, block,
1425 lookup_result.old_oblock);
1426 atomic_inc(&cache->stats.cache_cell_clash);
1427 break;
1428 }
1429 atomic_inc(&cache->stats.demotion);
1430 atomic_inc(&cache->stats.promotion);
1431
1432 demote_then_promote(cache, structs, lookup_result.old_oblock,
1433 block, lookup_result.cblock,
1434 old_ocell, new_ocell);
1435 release_cell = false;
1436 break;
1437
1438 default:
1439 DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
1440 (unsigned) lookup_result.op);
1441 bio_io_error(bio);
1442 }
1443
1444 if (release_cell)
1445 cell_defer(cache, new_ocell, false);
1446}
1447
1448static int need_commit_due_to_time(struct cache *cache)
1449{
1450 return jiffies < cache->last_commit_jiffies ||
1451 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1452}
1453
1454static int commit_if_needed(struct cache *cache)
1455{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001456 int r = 0;
1457
1458 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1459 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001460 atomic_inc(&cache->stats.commit_count);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001461 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001462 r = dm_cache_commit(cache->cmd, false);
1463 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001464 }
1465
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001466 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001467}
1468
1469static void process_deferred_bios(struct cache *cache)
1470{
1471 unsigned long flags;
1472 struct bio_list bios;
1473 struct bio *bio;
1474 struct prealloc structs;
1475
1476 memset(&structs, 0, sizeof(structs));
1477 bio_list_init(&bios);
1478
1479 spin_lock_irqsave(&cache->lock, flags);
1480 bio_list_merge(&bios, &cache->deferred_bios);
1481 bio_list_init(&cache->deferred_bios);
1482 spin_unlock_irqrestore(&cache->lock, flags);
1483
1484 while (!bio_list_empty(&bios)) {
1485 /*
1486 * If we've got no free migration structs, and processing
1487 * this bio might require one, we pause until there are some
1488 * prepared mappings to process.
1489 */
1490 if (prealloc_data_structs(cache, &structs)) {
1491 spin_lock_irqsave(&cache->lock, flags);
1492 bio_list_merge(&cache->deferred_bios, &bios);
1493 spin_unlock_irqrestore(&cache->lock, flags);
1494 break;
1495 }
1496
1497 bio = bio_list_pop(&bios);
1498
1499 if (bio->bi_rw & REQ_FLUSH)
1500 process_flush_bio(cache, bio);
1501 else if (bio->bi_rw & REQ_DISCARD)
1502 process_discard_bio(cache, bio);
1503 else
1504 process_bio(cache, &structs, bio);
1505 }
1506
1507 prealloc_free_structs(cache, &structs);
1508}
1509
1510static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1511{
1512 unsigned long flags;
1513 struct bio_list bios;
1514 struct bio *bio;
1515
1516 bio_list_init(&bios);
1517
1518 spin_lock_irqsave(&cache->lock, flags);
1519 bio_list_merge(&bios, &cache->deferred_flush_bios);
1520 bio_list_init(&cache->deferred_flush_bios);
1521 spin_unlock_irqrestore(&cache->lock, flags);
1522
Joe Thornber8c081b52014-05-13 16:18:38 +01001523 /*
1524 * These bios have already been through inc_ds()
1525 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001526 while ((bio = bio_list_pop(&bios)))
1527 submit_bios ? generic_make_request(bio) : bio_io_error(bio);
1528}
1529
Joe Thornbere2e74d62013-03-20 17:21:27 +00001530static void process_deferred_writethrough_bios(struct cache *cache)
1531{
1532 unsigned long flags;
1533 struct bio_list bios;
1534 struct bio *bio;
1535
1536 bio_list_init(&bios);
1537
1538 spin_lock_irqsave(&cache->lock, flags);
1539 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1540 bio_list_init(&cache->deferred_writethrough_bios);
1541 spin_unlock_irqrestore(&cache->lock, flags);
1542
Joe Thornber8c081b52014-05-13 16:18:38 +01001543 /*
1544 * These bios have already been through inc_ds()
1545 */
Joe Thornbere2e74d62013-03-20 17:21:27 +00001546 while ((bio = bio_list_pop(&bios)))
1547 generic_make_request(bio);
1548}
1549
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001550static void writeback_some_dirty_blocks(struct cache *cache)
1551{
1552 int r = 0;
1553 dm_oblock_t oblock;
1554 dm_cblock_t cblock;
1555 struct prealloc structs;
1556 struct dm_bio_prison_cell *old_ocell;
1557
1558 memset(&structs, 0, sizeof(structs));
1559
1560 while (spare_migration_bandwidth(cache)) {
1561 if (prealloc_data_structs(cache, &structs))
1562 break;
1563
1564 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1565 if (r)
1566 break;
1567
1568 r = get_cell(cache, oblock, &structs, &old_ocell);
1569 if (r) {
1570 policy_set_dirty(cache->policy, oblock);
1571 break;
1572 }
1573
1574 writeback(cache, &structs, oblock, cblock, old_ocell);
1575 }
1576
1577 prealloc_free_structs(cache, &structs);
1578}
1579
1580/*----------------------------------------------------------------
Joe Thornber65790ff2013-11-08 16:39:50 +00001581 * Invalidations.
1582 * Dropping something from the cache *without* writing back.
1583 *--------------------------------------------------------------*/
1584
1585static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1586{
1587 int r = 0;
1588 uint64_t begin = from_cblock(req->cblocks->begin);
1589 uint64_t end = from_cblock(req->cblocks->end);
1590
1591 while (begin != end) {
1592 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1593 if (!r) {
1594 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1595 if (r)
1596 break;
1597
1598 } else if (r == -ENODATA) {
1599 /* harmless, already unmapped */
1600 r = 0;
1601
1602 } else {
1603 DMERR("policy_remove_cblock failed");
1604 break;
1605 }
1606
1607 begin++;
1608 }
1609
1610 cache->commit_requested = true;
1611
1612 req->err = r;
1613 atomic_set(&req->complete, 1);
1614
1615 wake_up(&req->result_wait);
1616}
1617
1618static void process_invalidation_requests(struct cache *cache)
1619{
1620 struct list_head list;
1621 struct invalidation_request *req, *tmp;
1622
1623 INIT_LIST_HEAD(&list);
1624 spin_lock(&cache->invalidation_lock);
1625 list_splice_init(&cache->invalidation_requests, &list);
1626 spin_unlock(&cache->invalidation_lock);
1627
1628 list_for_each_entry_safe (req, tmp, &list, list)
1629 process_invalidation_request(cache, req);
1630}
1631
1632/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001633 * Main worker loop
1634 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001635static bool is_quiescing(struct cache *cache)
1636{
Joe Thornber238f8362013-10-30 17:29:30 +00001637 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001638}
1639
Joe Thornber66cb1912013-10-30 17:11:58 +00001640static void ack_quiescing(struct cache *cache)
1641{
1642 if (is_quiescing(cache)) {
1643 atomic_inc(&cache->quiescing_ack);
1644 wake_up(&cache->quiescing_wait);
1645 }
1646}
1647
1648static void wait_for_quiescing_ack(struct cache *cache)
1649{
1650 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1651}
1652
1653static void start_quiescing(struct cache *cache)
1654{
Joe Thornber238f8362013-10-30 17:29:30 +00001655 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00001656 wait_for_quiescing_ack(cache);
1657}
1658
1659static void stop_quiescing(struct cache *cache)
1660{
Joe Thornber238f8362013-10-30 17:29:30 +00001661 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00001662 atomic_set(&cache->quiescing_ack, 0);
1663}
1664
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001665static void wait_for_migrations(struct cache *cache)
1666{
1667 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1668}
1669
1670static void stop_worker(struct cache *cache)
1671{
1672 cancel_delayed_work(&cache->waker);
1673 flush_workqueue(cache->wq);
1674}
1675
1676static void requeue_deferred_io(struct cache *cache)
1677{
1678 struct bio *bio;
1679 struct bio_list bios;
1680
1681 bio_list_init(&bios);
1682 bio_list_merge(&bios, &cache->deferred_bios);
1683 bio_list_init(&cache->deferred_bios);
1684
1685 while ((bio = bio_list_pop(&bios)))
1686 bio_endio(bio, DM_ENDIO_REQUEUE);
1687}
1688
1689static int more_work(struct cache *cache)
1690{
1691 if (is_quiescing(cache))
1692 return !list_empty(&cache->quiesced_migrations) ||
1693 !list_empty(&cache->completed_migrations) ||
1694 !list_empty(&cache->need_commit_migrations);
1695 else
1696 return !bio_list_empty(&cache->deferred_bios) ||
1697 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00001698 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001699 !list_empty(&cache->quiesced_migrations) ||
1700 !list_empty(&cache->completed_migrations) ||
Joe Thornber65790ff2013-11-08 16:39:50 +00001701 !list_empty(&cache->need_commit_migrations) ||
1702 cache->invalidate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001703}
1704
1705static void do_worker(struct work_struct *ws)
1706{
1707 struct cache *cache = container_of(ws, struct cache, worker);
1708
1709 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00001710 if (!is_quiescing(cache)) {
1711 writeback_some_dirty_blocks(cache);
1712 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001713 process_deferred_bios(cache);
Joe Thornber65790ff2013-11-08 16:39:50 +00001714 process_invalidation_requests(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00001715 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001716
1717 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1718 process_migrations(cache, &cache->completed_migrations, complete_migration);
1719
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001720 if (commit_if_needed(cache)) {
1721 process_deferred_flush_bios(cache, false);
Joe Thornber304affa2014-06-24 15:36:58 -04001722 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001723
1724 /*
1725 * FIXME: rollback metadata or just go into a
1726 * failure mode and error everything
1727 */
1728 } else {
1729 process_deferred_flush_bios(cache, true);
1730 process_migrations(cache, &cache->need_commit_migrations,
1731 migration_success_post_commit);
1732 }
Joe Thornber66cb1912013-10-30 17:11:58 +00001733
1734 ack_quiescing(cache);
1735
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001736 } while (more_work(cache));
1737}
1738
1739/*
1740 * We want to commit periodically so that not too much
1741 * unwritten metadata builds up.
1742 */
1743static void do_waker(struct work_struct *ws)
1744{
1745 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberf8350da2013-05-10 14:37:16 +01001746 policy_tick(cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001747 wake_worker(cache);
1748 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1749}
1750
1751/*----------------------------------------------------------------*/
1752
1753static int is_congested(struct dm_dev *dev, int bdi_bits)
1754{
1755 struct request_queue *q = bdev_get_queue(dev->bdev);
1756 return bdi_congested(&q->backing_dev_info, bdi_bits);
1757}
1758
1759static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1760{
1761 struct cache *cache = container_of(cb, struct cache, callbacks);
1762
1763 return is_congested(cache->origin_dev, bdi_bits) ||
1764 is_congested(cache->cache_dev, bdi_bits);
1765}
1766
1767/*----------------------------------------------------------------
1768 * Target methods
1769 *--------------------------------------------------------------*/
1770
1771/*
1772 * This function gets called on the error paths of the constructor, so we
1773 * have to cope with a partially initialised struct.
1774 */
1775static void destroy(struct cache *cache)
1776{
1777 unsigned i;
1778
1779 if (cache->next_migration)
1780 mempool_free(cache->next_migration, cache->migration_pool);
1781
1782 if (cache->migration_pool)
1783 mempool_destroy(cache->migration_pool);
1784
1785 if (cache->all_io_ds)
1786 dm_deferred_set_destroy(cache->all_io_ds);
1787
1788 if (cache->prison)
1789 dm_bio_prison_destroy(cache->prison);
1790
1791 if (cache->wq)
1792 destroy_workqueue(cache->wq);
1793
1794 if (cache->dirty_bitset)
1795 free_bitset(cache->dirty_bitset);
1796
1797 if (cache->discard_bitset)
1798 free_bitset(cache->discard_bitset);
1799
1800 if (cache->copier)
1801 dm_kcopyd_client_destroy(cache->copier);
1802
1803 if (cache->cmd)
1804 dm_cache_metadata_close(cache->cmd);
1805
1806 if (cache->metadata_dev)
1807 dm_put_device(cache->ti, cache->metadata_dev);
1808
1809 if (cache->origin_dev)
1810 dm_put_device(cache->ti, cache->origin_dev);
1811
1812 if (cache->cache_dev)
1813 dm_put_device(cache->ti, cache->cache_dev);
1814
1815 if (cache->policy)
1816 dm_cache_policy_destroy(cache->policy);
1817
1818 for (i = 0; i < cache->nr_ctr_args ; i++)
1819 kfree(cache->ctr_args[i]);
1820 kfree(cache->ctr_args);
1821
1822 kfree(cache);
1823}
1824
1825static void cache_dtr(struct dm_target *ti)
1826{
1827 struct cache *cache = ti->private;
1828
1829 destroy(cache);
1830}
1831
1832static sector_t get_dev_size(struct dm_dev *dev)
1833{
1834 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1835}
1836
1837/*----------------------------------------------------------------*/
1838
1839/*
1840 * Construct a cache device mapping.
1841 *
1842 * cache <metadata dev> <cache dev> <origin dev> <block size>
1843 * <#feature args> [<feature arg>]*
1844 * <policy> <#policy args> [<policy arg>]*
1845 *
1846 * metadata dev : fast device holding the persistent metadata
1847 * cache dev : fast device holding cached data blocks
1848 * origin dev : slow device holding original data blocks
1849 * block size : cache unit size in sectors
1850 *
1851 * #feature args : number of feature arguments passed
1852 * feature args : writethrough. (The default is writeback.)
1853 *
1854 * policy : the replacement policy to use
1855 * #policy args : an even number of policy arguments corresponding
1856 * to key/value pairs passed to the policy
1857 * policy args : key/value pairs passed to the policy
1858 * E.g. 'sequential_threshold 1024'
1859 * See cache-policies.txt for details.
1860 *
1861 * Optional feature arguments are:
1862 * writethrough : write through caching that prohibits cache block
1863 * content from being different from origin block content.
1864 * Without this argument, the default behaviour is to write
1865 * back cache block contents later for performance reasons,
1866 * so they may differ from the corresponding origin blocks.
1867 */
1868struct cache_args {
1869 struct dm_target *ti;
1870
1871 struct dm_dev *metadata_dev;
1872
1873 struct dm_dev *cache_dev;
1874 sector_t cache_sectors;
1875
1876 struct dm_dev *origin_dev;
1877 sector_t origin_sectors;
1878
1879 uint32_t block_size;
1880
1881 const char *policy_name;
1882 int policy_argc;
1883 const char **policy_argv;
1884
1885 struct cache_features features;
1886};
1887
1888static void destroy_cache_args(struct cache_args *ca)
1889{
1890 if (ca->metadata_dev)
1891 dm_put_device(ca->ti, ca->metadata_dev);
1892
1893 if (ca->cache_dev)
1894 dm_put_device(ca->ti, ca->cache_dev);
1895
1896 if (ca->origin_dev)
1897 dm_put_device(ca->ti, ca->origin_dev);
1898
1899 kfree(ca);
1900}
1901
1902static bool at_least_one_arg(struct dm_arg_set *as, char **error)
1903{
1904 if (!as->argc) {
1905 *error = "Insufficient args";
1906 return false;
1907 }
1908
1909 return true;
1910}
1911
1912static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
1913 char **error)
1914{
1915 int r;
1916 sector_t metadata_dev_size;
1917 char b[BDEVNAME_SIZE];
1918
1919 if (!at_least_one_arg(as, error))
1920 return -EINVAL;
1921
1922 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1923 &ca->metadata_dev);
1924 if (r) {
1925 *error = "Error opening metadata device";
1926 return r;
1927 }
1928
1929 metadata_dev_size = get_dev_size(ca->metadata_dev);
1930 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
1931 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1932 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
1933
1934 return 0;
1935}
1936
1937static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
1938 char **error)
1939{
1940 int r;
1941
1942 if (!at_least_one_arg(as, error))
1943 return -EINVAL;
1944
1945 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1946 &ca->cache_dev);
1947 if (r) {
1948 *error = "Error opening cache device";
1949 return r;
1950 }
1951 ca->cache_sectors = get_dev_size(ca->cache_dev);
1952
1953 return 0;
1954}
1955
1956static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
1957 char **error)
1958{
1959 int r;
1960
1961 if (!at_least_one_arg(as, error))
1962 return -EINVAL;
1963
1964 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1965 &ca->origin_dev);
1966 if (r) {
1967 *error = "Error opening origin device";
1968 return r;
1969 }
1970
1971 ca->origin_sectors = get_dev_size(ca->origin_dev);
1972 if (ca->ti->len > ca->origin_sectors) {
1973 *error = "Device size larger than cached device";
1974 return -EINVAL;
1975 }
1976
1977 return 0;
1978}
1979
1980static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
1981 char **error)
1982{
Mike Snitzer05473042013-08-16 10:54:19 -04001983 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001984
1985 if (!at_least_one_arg(as, error))
1986 return -EINVAL;
1987
Mike Snitzer05473042013-08-16 10:54:19 -04001988 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
1989 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1990 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1991 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001992 *error = "Invalid data block size";
1993 return -EINVAL;
1994 }
1995
Mike Snitzer05473042013-08-16 10:54:19 -04001996 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001997 *error = "Data block size is larger than the cache device";
1998 return -EINVAL;
1999 }
2000
Mike Snitzer05473042013-08-16 10:54:19 -04002001 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002002
2003 return 0;
2004}
2005
2006static void init_features(struct cache_features *cf)
2007{
2008 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04002009 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002010}
2011
2012static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2013 char **error)
2014{
2015 static struct dm_arg _args[] = {
2016 {0, 1, "Invalid number of cache feature arguments"},
2017 };
2018
2019 int r;
2020 unsigned argc;
2021 const char *arg;
2022 struct cache_features *cf = &ca->features;
2023
2024 init_features(cf);
2025
2026 r = dm_read_arg_group(_args, as, &argc, error);
2027 if (r)
2028 return -EINVAL;
2029
2030 while (argc--) {
2031 arg = dm_shift_arg(as);
2032
2033 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002034 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002035
2036 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002037 cf->io_mode = CM_IO_WRITETHROUGH;
2038
2039 else if (!strcasecmp(arg, "passthrough"))
2040 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002041
2042 else {
2043 *error = "Unrecognised cache feature requested";
2044 return -EINVAL;
2045 }
2046 }
2047
2048 return 0;
2049}
2050
2051static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2052 char **error)
2053{
2054 static struct dm_arg _args[] = {
2055 {0, 1024, "Invalid number of policy arguments"},
2056 };
2057
2058 int r;
2059
2060 if (!at_least_one_arg(as, error))
2061 return -EINVAL;
2062
2063 ca->policy_name = dm_shift_arg(as);
2064
2065 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2066 if (r)
2067 return -EINVAL;
2068
2069 ca->policy_argv = (const char **)as->argv;
2070 dm_consume_args(as, ca->policy_argc);
2071
2072 return 0;
2073}
2074
2075static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2076 char **error)
2077{
2078 int r;
2079 struct dm_arg_set as;
2080
2081 as.argc = argc;
2082 as.argv = argv;
2083
2084 r = parse_metadata_dev(ca, &as, error);
2085 if (r)
2086 return r;
2087
2088 r = parse_cache_dev(ca, &as, error);
2089 if (r)
2090 return r;
2091
2092 r = parse_origin_dev(ca, &as, error);
2093 if (r)
2094 return r;
2095
2096 r = parse_block_size(ca, &as, error);
2097 if (r)
2098 return r;
2099
2100 r = parse_features(ca, &as, error);
2101 if (r)
2102 return r;
2103
2104 r = parse_policy(ca, &as, error);
2105 if (r)
2106 return r;
2107
2108 return 0;
2109}
2110
2111/*----------------------------------------------------------------*/
2112
2113static struct kmem_cache *migration_cache;
2114
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002115#define NOT_CORE_OPTION 1
2116
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002117static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002118{
2119 unsigned long tmp;
2120
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002121 if (!strcasecmp(key, "migration_threshold")) {
2122 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002123 return -EINVAL;
2124
2125 cache->migration_threshold = tmp;
2126 return 0;
2127 }
2128
2129 return NOT_CORE_OPTION;
2130}
2131
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002132static int set_config_value(struct cache *cache, const char *key, const char *value)
2133{
2134 int r = process_config_option(cache, key, value);
2135
2136 if (r == NOT_CORE_OPTION)
2137 r = policy_set_config_value(cache->policy, key, value);
2138
2139 if (r)
2140 DMWARN("bad config value for %s: %s", key, value);
2141
2142 return r;
2143}
2144
2145static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002146{
2147 int r = 0;
2148
2149 if (argc & 1) {
2150 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2151 return -EINVAL;
2152 }
2153
2154 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002155 r = set_config_value(cache, argv[0], argv[1]);
2156 if (r)
2157 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002158
2159 argc -= 2;
2160 argv += 2;
2161 }
2162
2163 return r;
2164}
2165
2166static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2167 char **error)
2168{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002169 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2170 cache->cache_size,
2171 cache->origin_sectors,
2172 cache->sectors_per_block);
2173 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002174 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002175 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002176 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002177 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002178
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002179 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002180}
2181
Joe Thornberf8350da2013-05-10 14:37:16 +01002182#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002183
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002184static int cache_create(struct cache_args *ca, struct cache **result)
2185{
2186 int r = 0;
2187 char **error = &ca->ti->error;
2188 struct cache *cache;
2189 struct dm_target *ti = ca->ti;
2190 dm_block_t origin_blocks;
2191 struct dm_cache_metadata *cmd;
2192 bool may_format = ca->features.mode == CM_WRITE;
2193
2194 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2195 if (!cache)
2196 return -ENOMEM;
2197
2198 cache->ti = ca->ti;
2199 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002200 ti->num_flush_bios = 2;
2201 ti->flush_supported = true;
2202
2203 ti->num_discard_bios = 1;
2204 ti->discards_supported = true;
2205 ti->discard_zeroes_data_unsupported = true;
Heinz Mauelshagenf1daa8382014-05-23 14:10:01 -04002206 /* Discard bios must be split on a block boundary */
2207 ti->split_discard_bios = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002208
Joe Thornber8c5008f2013-05-10 14:37:18 +01002209 cache->features = ca->features;
Mike Snitzer19b00922013-04-05 15:36:34 +01002210 ti->per_bio_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002211
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002212 cache->callbacks.congested_fn = cache_is_congested;
2213 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2214
2215 cache->metadata_dev = ca->metadata_dev;
2216 cache->origin_dev = ca->origin_dev;
2217 cache->cache_dev = ca->cache_dev;
2218
2219 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2220
2221 /* FIXME: factor out this whole section */
2222 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002223 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002224 cache->origin_blocks = to_oblock(origin_blocks);
2225
2226 cache->sectors_per_block = ca->block_size;
2227 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2228 r = -EINVAL;
2229 goto bad;
2230 }
2231
2232 if (ca->block_size & (ca->block_size - 1)) {
2233 dm_block_t cache_size = ca->cache_sectors;
2234
2235 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002236 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002237 cache->cache_size = to_cblock(cache_size);
2238 } else {
2239 cache->sectors_per_block_shift = __ffs(ca->block_size);
2240 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2241 }
2242
2243 r = create_cache_policy(cache, ca, error);
2244 if (r)
2245 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002246
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002247 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002248 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2249
2250 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2251 if (r) {
2252 *error = "Error setting cache policy's config values";
2253 goto bad;
2254 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002255
2256 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2257 ca->block_size, may_format,
2258 dm_cache_policy_get_hint_size(cache->policy));
2259 if (IS_ERR(cmd)) {
2260 *error = "Error creating metadata object";
2261 r = PTR_ERR(cmd);
2262 goto bad;
2263 }
2264 cache->cmd = cmd;
2265
Joe Thornber2ee57d52013-10-24 14:10:29 -04002266 if (passthrough_mode(&cache->features)) {
2267 bool all_clean;
2268
2269 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2270 if (r) {
2271 *error = "dm_cache_metadata_all_clean() failed";
2272 goto bad;
2273 }
2274
2275 if (!all_clean) {
2276 *error = "Cannot enter passthrough mode unless all blocks are clean";
2277 r = -EINVAL;
2278 goto bad;
2279 }
2280 }
2281
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002282 spin_lock_init(&cache->lock);
2283 bio_list_init(&cache->deferred_bios);
2284 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002285 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002286 INIT_LIST_HEAD(&cache->quiesced_migrations);
2287 INIT_LIST_HEAD(&cache->completed_migrations);
2288 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002289 atomic_set(&cache->nr_migrations, 0);
2290 init_waitqueue_head(&cache->migration_wait);
2291
Joe Thornber66cb1912013-10-30 17:11:58 +00002292 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002293 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002294 atomic_set(&cache->quiescing_ack, 0);
2295
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002296 r = -ENOMEM;
Anssi Hannula44fa8162014-08-01 11:55:47 -04002297 atomic_set(&cache->nr_dirty, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002298 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2299 if (!cache->dirty_bitset) {
2300 *error = "could not allocate dirty bitset";
2301 goto bad;
2302 }
2303 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2304
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002305 cache->discard_nr_blocks = cache->origin_blocks;
2306 cache->discard_bitset = alloc_bitset(from_oblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002307 if (!cache->discard_bitset) {
2308 *error = "could not allocate discard bitset";
2309 goto bad;
2310 }
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002311 clear_bitset(cache->discard_bitset, from_oblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002312
2313 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2314 if (IS_ERR(cache->copier)) {
2315 *error = "could not create kcopyd client";
2316 r = PTR_ERR(cache->copier);
2317 goto bad;
2318 }
2319
2320 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2321 if (!cache->wq) {
2322 *error = "could not create workqueue for metadata object";
2323 goto bad;
2324 }
2325 INIT_WORK(&cache->worker, do_worker);
2326 INIT_DELAYED_WORK(&cache->waker, do_waker);
2327 cache->last_commit_jiffies = jiffies;
2328
Joe Thornbera195db22014-10-06 16:30:06 -04002329 cache->prison = dm_bio_prison_create();
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002330 if (!cache->prison) {
2331 *error = "could not create bio prison";
2332 goto bad;
2333 }
2334
2335 cache->all_io_ds = dm_deferred_set_create();
2336 if (!cache->all_io_ds) {
2337 *error = "could not create all_io deferred set";
2338 goto bad;
2339 }
2340
2341 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2342 migration_cache);
2343 if (!cache->migration_pool) {
2344 *error = "Error creating cache's migration mempool";
2345 goto bad;
2346 }
2347
2348 cache->next_migration = NULL;
2349
2350 cache->need_tick_bio = true;
2351 cache->sized = false;
Joe Thornber65790ff2013-11-08 16:39:50 +00002352 cache->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002353 cache->commit_requested = false;
2354 cache->loaded_mappings = false;
2355 cache->loaded_discards = false;
2356
2357 load_stats(cache);
2358
2359 atomic_set(&cache->stats.demotion, 0);
2360 atomic_set(&cache->stats.promotion, 0);
2361 atomic_set(&cache->stats.copies_avoided, 0);
2362 atomic_set(&cache->stats.cache_cell_clash, 0);
2363 atomic_set(&cache->stats.commit_count, 0);
2364 atomic_set(&cache->stats.discard_count, 0);
2365
Joe Thornber65790ff2013-11-08 16:39:50 +00002366 spin_lock_init(&cache->invalidation_lock);
2367 INIT_LIST_HEAD(&cache->invalidation_requests);
2368
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002369 *result = cache;
2370 return 0;
2371
2372bad:
2373 destroy(cache);
2374 return r;
2375}
2376
2377static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2378{
2379 unsigned i;
2380 const char **copy;
2381
2382 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2383 if (!copy)
2384 return -ENOMEM;
2385 for (i = 0; i < argc; i++) {
2386 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2387 if (!copy[i]) {
2388 while (i--)
2389 kfree(copy[i]);
2390 kfree(copy);
2391 return -ENOMEM;
2392 }
2393 }
2394
2395 cache->nr_ctr_args = argc;
2396 cache->ctr_args = copy;
2397
2398 return 0;
2399}
2400
2401static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2402{
2403 int r = -EINVAL;
2404 struct cache_args *ca;
2405 struct cache *cache = NULL;
2406
2407 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2408 if (!ca) {
2409 ti->error = "Error allocating memory for cache";
2410 return -ENOMEM;
2411 }
2412 ca->ti = ti;
2413
2414 r = parse_cache_args(ca, argc, argv, &ti->error);
2415 if (r)
2416 goto out;
2417
2418 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00002419 if (r)
2420 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002421
2422 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2423 if (r) {
2424 destroy(cache);
2425 goto out;
2426 }
2427
2428 ti->private = cache;
2429
2430out:
2431 destroy_cache_args(ca);
2432 return r;
2433}
2434
Joe Thornber8c081b52014-05-13 16:18:38 +01002435static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002436{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002437 int r;
2438 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01002439 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002440 bool can_migrate = false;
2441 bool discarded_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002442 struct policy_result lookup_result;
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002443 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002444
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002445 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002446 /*
2447 * This can only occur if the io goes to a partial block at
2448 * the end of the origin device. We don't cache these.
2449 * Just remap to the origin and carry on.
2450 */
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01002451 remap_to_origin(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002452 return DM_MAPIO_REMAPPED;
2453 }
2454
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002455 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
2456 defer_bio(cache, bio);
2457 return DM_MAPIO_SUBMITTED;
2458 }
2459
2460 /*
2461 * Check to see if that block is currently migrating.
2462 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002463 *cell = alloc_prison_cell(cache);
2464 if (!*cell) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002465 defer_bio(cache, bio);
2466 return DM_MAPIO_SUBMITTED;
2467 }
2468
Joe Thornber8c081b52014-05-13 16:18:38 +01002469 r = bio_detain(cache, block, bio, *cell,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002470 (cell_free_fn) free_prison_cell,
Joe Thornber8c081b52014-05-13 16:18:38 +01002471 cache, cell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002472 if (r) {
2473 if (r < 0)
2474 defer_bio(cache, bio);
2475
2476 return DM_MAPIO_SUBMITTED;
2477 }
2478
2479 discarded_block = is_discarded_oblock(cache, block);
2480
2481 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2482 bio, &lookup_result);
2483 if (r == -EWOULDBLOCK) {
Joe Thornber8c081b52014-05-13 16:18:38 +01002484 cell_defer(cache, *cell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002485 return DM_MAPIO_SUBMITTED;
2486
2487 } else if (r) {
2488 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
Joe Thornber8c081b52014-05-13 16:18:38 +01002489 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002490 bio_io_error(bio);
2491 return DM_MAPIO_SUBMITTED;
2492 }
2493
Joe Thornber2ee57d52013-10-24 14:10:29 -04002494 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002495 switch (lookup_result.op) {
2496 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04002497 if (passthrough_mode(&cache->features)) {
2498 if (bio_data_dir(bio) == WRITE) {
2499 /*
2500 * We need to invalidate this block, so
2501 * defer for the worker thread.
2502 */
Joe Thornber8c081b52014-05-13 16:18:38 +01002503 cell_defer(cache, *cell, true);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002504 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002505
Joe Thornber2ee57d52013-10-24 14:10:29 -04002506 } else {
Joe Thornber2ee57d52013-10-24 14:10:29 -04002507 inc_miss_counter(cache, bio);
2508 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002509 }
2510
2511 } else {
2512 inc_hit_counter(cache, bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002513 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2514 !is_dirty(cache, lookup_result.cblock))
2515 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2516 else
2517 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002518 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002519 break;
2520
2521 case POLICY_MISS:
2522 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002523 if (pb->req_nr != 0) {
2524 /*
2525 * This is a duplicate writethrough io that is no
2526 * longer needed because the block has been demoted.
2527 */
2528 bio_endio(bio, 0);
Joe Thornber8c081b52014-05-13 16:18:38 +01002529 cell_defer(cache, *cell, false);
2530 r = DM_MAPIO_SUBMITTED;
2531
2532 } else
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002533 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01002534
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002535 break;
2536
2537 default:
2538 DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
2539 (unsigned) lookup_result.op);
Joe Thornber8c081b52014-05-13 16:18:38 +01002540 cell_defer(cache, *cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002541 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04002542 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002543 }
2544
Joe Thornber2ee57d52013-10-24 14:10:29 -04002545 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002546}
2547
Joe Thornber8c081b52014-05-13 16:18:38 +01002548static int cache_map(struct dm_target *ti, struct bio *bio)
2549{
2550 int r;
2551 struct dm_bio_prison_cell *cell;
2552 struct cache *cache = ti->private;
2553
2554 r = __cache_map(cache, bio, &cell);
2555 if (r == DM_MAPIO_REMAPPED) {
2556 inc_ds(cache, bio, cell);
2557 cell_defer(cache, cell, false);
2558 }
2559
2560 return r;
2561}
2562
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002563static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
2564{
2565 struct cache *cache = ti->private;
2566 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01002567 size_t pb_data_size = get_per_bio_data_size(cache);
2568 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002569
2570 if (pb->tick) {
2571 policy_tick(cache->policy);
2572
2573 spin_lock_irqsave(&cache->lock, flags);
2574 cache->need_tick_bio = true;
2575 spin_unlock_irqrestore(&cache->lock, flags);
2576 }
2577
2578 check_for_quiesced_migrations(cache, pb);
2579
2580 return 0;
2581}
2582
2583static int write_dirty_bitset(struct cache *cache)
2584{
2585 unsigned i, r;
2586
2587 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2588 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2589 is_dirty(cache, to_cblock(i)));
2590 if (r)
2591 return r;
2592 }
2593
2594 return 0;
2595}
2596
2597static int write_discard_bitset(struct cache *cache)
2598{
2599 unsigned i, r;
2600
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002601 r = dm_cache_discard_bitset_resize(cache->cmd, cache->sectors_per_block,
2602 cache->origin_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002603 if (r) {
2604 DMERR("could not resize on-disk discard bitset");
2605 return r;
2606 }
2607
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002608 for (i = 0; i < from_oblock(cache->discard_nr_blocks); i++) {
2609 r = dm_cache_set_discard(cache->cmd, to_oblock(i),
2610 is_discarded(cache, to_oblock(i)));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002611 if (r)
2612 return r;
2613 }
2614
2615 return 0;
2616}
2617
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002618/*
2619 * returns true on success
2620 */
2621static bool sync_metadata(struct cache *cache)
2622{
2623 int r1, r2, r3, r4;
2624
2625 r1 = write_dirty_bitset(cache);
2626 if (r1)
2627 DMERR("could not write dirty bitset");
2628
2629 r2 = write_discard_bitset(cache);
2630 if (r2)
2631 DMERR("could not write discard bitset");
2632
2633 save_stats(cache);
2634
Joe Thornber05966612014-04-03 16:16:44 +01002635 r3 = dm_cache_write_hints(cache->cmd, cache->policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002636 if (r3)
2637 DMERR("could not write hints");
2638
2639 /*
2640 * If writing the above metadata failed, we still commit, but don't
2641 * set the clean shutdown flag. This will effectively force every
2642 * dirty bit to be set on reload.
2643 */
2644 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2645 if (r4)
2646 DMERR("could not write cache metadata. Data loss may occur.");
2647
2648 return !r1 && !r2 && !r3 && !r4;
2649}
2650
2651static void cache_postsuspend(struct dm_target *ti)
2652{
2653 struct cache *cache = ti->private;
2654
2655 start_quiescing(cache);
2656 wait_for_migrations(cache);
2657 stop_worker(cache);
2658 requeue_deferred_io(cache);
2659 stop_quiescing(cache);
2660
2661 (void) sync_metadata(cache);
2662}
2663
2664static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
2665 bool dirty, uint32_t hint, bool hint_valid)
2666{
2667 int r;
2668 struct cache *cache = context;
2669
2670 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2671 if (r)
2672 return r;
2673
2674 if (dirty)
2675 set_dirty(cache, oblock, cblock);
2676 else
2677 clear_dirty(cache, oblock, cblock);
2678
2679 return 0;
2680}
2681
2682static int load_discard(void *context, sector_t discard_block_size,
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002683 dm_oblock_t oblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002684{
2685 struct cache *cache = context;
2686
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002687 if (discard)
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002688 set_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002689 else
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04002690 clear_discard(cache, oblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002691
2692 return 0;
2693}
2694
Joe Thornberf494a9c2013-10-31 13:55:49 -04002695static dm_cblock_t get_cache_dev_size(struct cache *cache)
2696{
2697 sector_t size = get_dev_size(cache->cache_dev);
2698 (void) sector_div(size, cache->sectors_per_block);
2699 return to_cblock(size);
2700}
2701
2702static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2703{
2704 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2705 return true;
2706
2707 /*
2708 * We can't drop a dirty block when shrinking the cache.
2709 */
2710 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2711 new_size = to_cblock(from_cblock(new_size) + 1);
2712 if (is_dirty(cache, new_size)) {
2713 DMERR("unable to shrink cache; cache block %llu is dirty",
2714 (unsigned long long) from_cblock(new_size));
2715 return false;
2716 }
2717 }
2718
2719 return true;
2720}
2721
2722static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2723{
2724 int r;
2725
Vincent Pelletier08844802013-11-30 12:58:42 +01002726 r = dm_cache_resize(cache->cmd, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04002727 if (r) {
2728 DMERR("could not resize cache metadata");
2729 return r;
2730 }
2731
2732 cache->cache_size = new_size;
2733
2734 return 0;
2735}
2736
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002737static int cache_preresume(struct dm_target *ti)
2738{
2739 int r = 0;
2740 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002741 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002742
2743 /*
2744 * Check to see if the cache has resized.
2745 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04002746 if (!cache->sized) {
2747 r = resize_cache_dev(cache, csize);
2748 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002749 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002750
2751 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04002752
2753 } else if (csize != cache->cache_size) {
2754 if (!can_resize(cache, csize))
2755 return -EINVAL;
2756
2757 r = resize_cache_dev(cache, csize);
2758 if (r)
2759 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002760 }
2761
2762 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00002763 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002764 load_mapping, cache);
2765 if (r) {
2766 DMERR("could not load cache mappings");
2767 return r;
2768 }
2769
2770 cache->loaded_mappings = true;
2771 }
2772
2773 if (!cache->loaded_discards) {
2774 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2775 if (r) {
2776 DMERR("could not load origin discards");
2777 return r;
2778 }
2779
2780 cache->loaded_discards = true;
2781 }
2782
2783 return r;
2784}
2785
2786static void cache_resume(struct dm_target *ti)
2787{
2788 struct cache *cache = ti->private;
2789
2790 cache->need_tick_bio = true;
2791 do_waker(&cache->waker.work);
2792}
2793
2794/*
2795 * Status format:
2796 *
Mike Snitzer6a388612014-01-09 16:04:12 -05002797 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
2798 * <cache block size> <#used cache blocks>/<#total cache blocks>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002799 * <#read hits> <#read misses> <#write hits> <#write misses>
Mike Snitzer6a388612014-01-09 16:04:12 -05002800 * <#demotions> <#promotions> <#dirty>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002801 * <#features> <features>*
2802 * <#core args> <core args>
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002803 * <policy name> <#policy args> <policy args>*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002804 */
2805static void cache_status(struct dm_target *ti, status_type_t type,
2806 unsigned status_flags, char *result, unsigned maxlen)
2807{
2808 int r = 0;
2809 unsigned i;
2810 ssize_t sz = 0;
2811 dm_block_t nr_free_blocks_metadata = 0;
2812 dm_block_t nr_blocks_metadata = 0;
2813 char buf[BDEVNAME_SIZE];
2814 struct cache *cache = ti->private;
2815 dm_cblock_t residency;
2816
2817 switch (type) {
2818 case STATUSTYPE_INFO:
2819 /* Commit to ensure statistics aren't out-of-date */
2820 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
2821 r = dm_cache_commit(cache->cmd, false);
2822 if (r)
2823 DMERR("could not commit metadata for accurate status");
2824 }
2825
2826 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2827 &nr_free_blocks_metadata);
2828 if (r) {
2829 DMERR("could not get metadata free block count");
2830 goto err;
2831 }
2832
2833 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2834 if (r) {
2835 DMERR("could not get metadata device size");
2836 goto err;
2837 }
2838
2839 residency = policy_residency(cache->policy);
2840
Anssi Hannula44fa8162014-08-01 11:55:47 -04002841 DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
Mike Snitzer895b47d2014-07-14 15:37:18 -04002842 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002843 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2844 (unsigned long long)nr_blocks_metadata,
Mike Snitzer6a388612014-01-09 16:04:12 -05002845 cache->sectors_per_block,
2846 (unsigned long long) from_cblock(residency),
2847 (unsigned long long) from_cblock(cache->cache_size),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002848 (unsigned) atomic_read(&cache->stats.read_hit),
2849 (unsigned) atomic_read(&cache->stats.read_miss),
2850 (unsigned) atomic_read(&cache->stats.write_hit),
2851 (unsigned) atomic_read(&cache->stats.write_miss),
2852 (unsigned) atomic_read(&cache->stats.demotion),
2853 (unsigned) atomic_read(&cache->stats.promotion),
Anssi Hannula44fa8162014-08-01 11:55:47 -04002854 (unsigned long) atomic_read(&cache->nr_dirty));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002855
Joe Thornber2ee57d52013-10-24 14:10:29 -04002856 if (writethrough_mode(&cache->features))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002857 DMEMIT("1 writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04002858
2859 else if (passthrough_mode(&cache->features))
2860 DMEMIT("1 passthrough ");
2861
2862 else if (writeback_mode(&cache->features))
2863 DMEMIT("1 writeback ");
2864
2865 else {
2866 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2867 goto err;
2868 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002869
2870 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05002871
2872 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002873 if (sz < maxlen) {
2874 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2875 if (r)
2876 DMERR("policy_emit_config_values returned %d", r);
2877 }
2878
2879 break;
2880
2881 case STATUSTYPE_TABLE:
2882 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2883 DMEMIT("%s ", buf);
2884 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2885 DMEMIT("%s ", buf);
2886 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2887 DMEMIT("%s", buf);
2888
2889 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2890 DMEMIT(" %s", cache->ctr_args[i]);
2891 if (cache->nr_ctr_args)
2892 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2893 }
2894
2895 return;
2896
2897err:
2898 DMEMIT("Error");
2899}
2900
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002901/*
Joe Thornber65790ff2013-11-08 16:39:50 +00002902 * A cache block range can take two forms:
2903 *
2904 * i) A single cblock, eg. '3456'
2905 * ii) A begin and end cblock with dots between, eg. 123-234
2906 */
2907static int parse_cblock_range(struct cache *cache, const char *str,
2908 struct cblock_range *result)
2909{
2910 char dummy;
2911 uint64_t b, e;
2912 int r;
2913
2914 /*
2915 * Try and parse form (ii) first.
2916 */
2917 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
2918 if (r < 0)
2919 return r;
2920
2921 if (r == 2) {
2922 result->begin = to_cblock(b);
2923 result->end = to_cblock(e);
2924 return 0;
2925 }
2926
2927 /*
2928 * That didn't work, try form (i).
2929 */
2930 r = sscanf(str, "%llu%c", &b, &dummy);
2931 if (r < 0)
2932 return r;
2933
2934 if (r == 1) {
2935 result->begin = to_cblock(b);
2936 result->end = to_cblock(from_cblock(result->begin) + 1u);
2937 return 0;
2938 }
2939
2940 DMERR("invalid cblock range '%s'", str);
2941 return -EINVAL;
2942}
2943
2944static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
2945{
2946 uint64_t b = from_cblock(range->begin);
2947 uint64_t e = from_cblock(range->end);
2948 uint64_t n = from_cblock(cache->cache_size);
2949
2950 if (b >= n) {
2951 DMERR("begin cblock out of range: %llu >= %llu", b, n);
2952 return -EINVAL;
2953 }
2954
2955 if (e > n) {
2956 DMERR("end cblock out of range: %llu > %llu", e, n);
2957 return -EINVAL;
2958 }
2959
2960 if (b >= e) {
2961 DMERR("invalid cblock range: %llu >= %llu", b, e);
2962 return -EINVAL;
2963 }
2964
2965 return 0;
2966}
2967
2968static int request_invalidation(struct cache *cache, struct cblock_range *range)
2969{
2970 struct invalidation_request req;
2971
2972 INIT_LIST_HEAD(&req.list);
2973 req.cblocks = range;
2974 atomic_set(&req.complete, 0);
2975 req.err = 0;
2976 init_waitqueue_head(&req.result_wait);
2977
2978 spin_lock(&cache->invalidation_lock);
2979 list_add(&req.list, &cache->invalidation_requests);
2980 spin_unlock(&cache->invalidation_lock);
2981 wake_worker(cache);
2982
2983 wait_event(req.result_wait, atomic_read(&req.complete));
2984 return req.err;
2985}
2986
2987static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
2988 const char **cblock_ranges)
2989{
2990 int r = 0;
2991 unsigned i;
2992 struct cblock_range range;
2993
2994 if (!passthrough_mode(&cache->features)) {
2995 DMERR("cache has to be in passthrough mode for invalidation");
2996 return -EPERM;
2997 }
2998
2999 for (i = 0; i < count; i++) {
3000 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3001 if (r)
3002 break;
3003
3004 r = validate_cblock_range(cache, &range);
3005 if (r)
3006 break;
3007
3008 /*
3009 * Pass begin and end origin blocks to the worker and wake it.
3010 */
3011 r = request_invalidation(cache, &range);
3012 if (r)
3013 break;
3014 }
3015
3016 return r;
3017}
3018
3019/*
3020 * Supports
3021 * "<key> <value>"
3022 * and
3023 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003024 *
3025 * The key migration_threshold is supported by the cache target core.
3026 */
3027static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3028{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003029 struct cache *cache = ti->private;
3030
Joe Thornber65790ff2013-11-08 16:39:50 +00003031 if (!argc)
3032 return -EINVAL;
3033
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -05003034 if (!strcasecmp(argv[0], "invalidate_cblocks"))
Joe Thornber65790ff2013-11-08 16:39:50 +00003035 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3036
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003037 if (argc != 2)
3038 return -EINVAL;
3039
Joe Thornber2f14f4b2013-05-10 14:37:21 +01003040 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003041}
3042
3043static int cache_iterate_devices(struct dm_target *ti,
3044 iterate_devices_callout_fn fn, void *data)
3045{
3046 int r = 0;
3047 struct cache *cache = ti->private;
3048
3049 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3050 if (!r)
3051 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3052
3053 return r;
3054}
3055
3056/*
3057 * We assume I/O is going to the origin (which is the volume
3058 * more likely to have restrictions e.g. by being striped).
3059 * (Looking up the exact location of the data would be expensive
3060 * and could always be out of date by the time the bio is submitted.)
3061 */
3062static int cache_bvec_merge(struct dm_target *ti,
3063 struct bvec_merge_data *bvm,
3064 struct bio_vec *biovec, int max_size)
3065{
3066 struct cache *cache = ti->private;
3067 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3068
3069 if (!q->merge_bvec_fn)
3070 return max_size;
3071
3072 bvm->bi_bdev = cache->origin_dev->bdev;
3073 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3074}
3075
3076static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3077{
3078 /*
3079 * FIXME: these limits may be incompatible with the cache device
3080 */
Heinz Mauelshagen64ab3462014-03-27 15:14:10 -04003081 limits->max_discard_sectors = cache->sectors_per_block;
3082 limits->discard_granularity = cache->sectors_per_block << SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003083}
3084
3085static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3086{
3087 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04003088 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003089
Mike Snitzerf6109372013-08-20 15:02:41 -04003090 /*
3091 * If the system-determined stacked limits are compatible with the
3092 * cache's blocksize (io_opt is a factor) do not override them.
3093 */
3094 if (io_opt_sectors < cache->sectors_per_block ||
3095 do_div(io_opt_sectors, cache->sectors_per_block)) {
Mike Snitzerb0246532014-07-19 13:25:46 -04003096 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf6109372013-08-20 15:02:41 -04003097 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3098 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003099 set_discard_limits(cache, limits);
3100}
3101
3102/*----------------------------------------------------------------*/
3103
3104static struct target_type cache_target = {
3105 .name = "cache",
Joe Thornber8c081b52014-05-13 16:18:38 +01003106 .version = {1, 5, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003107 .module = THIS_MODULE,
3108 .ctr = cache_ctr,
3109 .dtr = cache_dtr,
3110 .map = cache_map,
3111 .end_io = cache_end_io,
3112 .postsuspend = cache_postsuspend,
3113 .preresume = cache_preresume,
3114 .resume = cache_resume,
3115 .status = cache_status,
3116 .message = cache_message,
3117 .iterate_devices = cache_iterate_devices,
3118 .merge = cache_bvec_merge,
3119 .io_hints = cache_io_hints,
3120};
3121
3122static int __init dm_cache_init(void)
3123{
3124 int r;
3125
3126 r = dm_register_target(&cache_target);
3127 if (r) {
3128 DMERR("cache target registration failed: %d", r);
3129 return r;
3130 }
3131
3132 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3133 if (!migration_cache) {
3134 dm_unregister_target(&cache_target);
3135 return -ENOMEM;
3136 }
3137
3138 return 0;
3139}
3140
3141static void __exit dm_cache_exit(void)
3142{
3143 dm_unregister_target(&cache_target);
3144 kmem_cache_destroy(migration_cache);
3145}
3146
3147module_init(dm_cache_init);
3148module_exit(dm_cache_exit);
3149
3150MODULE_DESCRIPTION(DM_NAME " cache target");
3151MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3152MODULE_LICENSE("GPL");