blob: 5813d2a7eefe4f1121a3b5509eeca232785974f8 [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>
Manuel Schölling0f30af92014-05-22 22:42:37 +020014#include <linux/jiffies.h>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000015#include <linux/init.h>
16#include <linux/mempool.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
20
21#define DM_MSG_PREFIX "cache"
22
23DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
24 "A percentage of time allocated for copying to and/or from cache");
25
26/*----------------------------------------------------------------*/
27
Joe Thornber77289d32015-05-15 13:45:30 +010028#define IOT_RESOLUTION 4
29
30struct io_tracker {
31 spinlock_t lock;
32
33 /*
34 * Sectors of in-flight IO.
35 */
36 sector_t in_flight;
37
38 /*
39 * The time, in jiffies, when this device became idle (if it is
40 * indeed idle).
41 */
42 unsigned long idle_time;
43 unsigned long last_update_time;
44};
45
46static void iot_init(struct io_tracker *iot)
47{
48 spin_lock_init(&iot->lock);
49 iot->in_flight = 0ul;
50 iot->idle_time = 0ul;
51 iot->last_update_time = jiffies;
52}
53
54static bool __iot_idle_for(struct io_tracker *iot, unsigned long jifs)
55{
56 if (iot->in_flight)
57 return false;
58
59 return time_after(jiffies, iot->idle_time + jifs);
60}
61
62static bool iot_idle_for(struct io_tracker *iot, unsigned long jifs)
63{
64 bool r;
65 unsigned long flags;
66
67 spin_lock_irqsave(&iot->lock, flags);
68 r = __iot_idle_for(iot, jifs);
69 spin_unlock_irqrestore(&iot->lock, flags);
70
71 return r;
72}
73
74static void iot_io_begin(struct io_tracker *iot, sector_t len)
75{
76 unsigned long flags;
77
78 spin_lock_irqsave(&iot->lock, flags);
79 iot->in_flight += len;
80 spin_unlock_irqrestore(&iot->lock, flags);
81}
82
83static void __iot_io_end(struct io_tracker *iot, sector_t len)
84{
85 iot->in_flight -= len;
86 if (!iot->in_flight)
87 iot->idle_time = jiffies;
88}
89
90static void iot_io_end(struct io_tracker *iot, sector_t len)
91{
92 unsigned long flags;
93
94 spin_lock_irqsave(&iot->lock, flags);
95 __iot_io_end(iot, len);
96 spin_unlock_irqrestore(&iot->lock, flags);
97}
98
99/*----------------------------------------------------------------*/
100
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000101/*
102 * Glossary:
103 *
104 * oblock: index of an origin block
105 * cblock: index of a cache block
106 * promotion: movement of a block from origin to cache
107 * demotion: movement of a block from cache to origin
108 * migration: movement of a block between the origin and cache device,
109 * either direction
110 */
111
112/*----------------------------------------------------------------*/
113
Joe Thornberc9d28d52013-10-31 13:55:48 -0400114/*
115 * There are a couple of places where we let a bio run, but want to do some
116 * work before calling its endio function. We do this by temporarily
117 * changing the endio fn.
118 */
119struct dm_hook_info {
120 bio_end_io_t *bi_end_io;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400121};
122
123static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
124 bio_end_io_t *bi_end_io, void *bi_private)
125{
126 h->bi_end_io = bio->bi_end_io;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400127
128 bio->bi_end_io = bi_end_io;
129 bio->bi_private = bi_private;
130}
131
132static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
133{
134 bio->bi_end_io = h->bi_end_io;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400135}
136
137/*----------------------------------------------------------------*/
138
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000139#define MIGRATION_POOL_SIZE 128
140#define COMMIT_PERIOD HZ
141#define MIGRATION_COUNT_WINDOW 10
142
143/*
Mike Snitzer05473042013-08-16 10:54:19 -0400144 * The block size of the device holding cache data must be
145 * between 32KB and 1GB.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000146 */
147#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
Mike Snitzer05473042013-08-16 10:54:19 -0400148#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000149
Joe Thornber2ee57d52013-10-24 14:10:29 -0400150enum cache_metadata_mode {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000151 CM_WRITE, /* metadata may be changed */
152 CM_READ_ONLY, /* metadata may not be changed */
Joe Thornber028ae9f2015-04-22 16:42:35 -0400153 CM_FAIL
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000154};
155
Joe Thornber2ee57d52013-10-24 14:10:29 -0400156enum cache_io_mode {
157 /*
158 * Data is written to cached blocks only. These blocks are marked
159 * dirty. If you lose the cache device you will lose data.
160 * Potential performance increase for both reads and writes.
161 */
162 CM_IO_WRITEBACK,
163
164 /*
165 * Data is written to both cache and origin. Blocks are never
166 * dirty. Potential performance benfit for reads only.
167 */
168 CM_IO_WRITETHROUGH,
169
170 /*
171 * A degraded mode useful for various cache coherency situations
172 * (eg, rolling back snapshots). Reads and writes always go to the
173 * origin. If a write goes to a cached oblock, then the cache
174 * block is invalidated.
175 */
176 CM_IO_PASSTHROUGH
177};
178
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000179struct cache_features {
Joe Thornber2ee57d52013-10-24 14:10:29 -0400180 enum cache_metadata_mode mode;
181 enum cache_io_mode io_mode;
Joe Thornber629d0a82016-09-22 06:15:21 -0400182 unsigned metadata_version;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000183};
184
185struct cache_stats {
186 atomic_t read_hit;
187 atomic_t read_miss;
188 atomic_t write_hit;
189 atomic_t write_miss;
190 atomic_t demotion;
191 atomic_t promotion;
192 atomic_t copies_avoided;
193 atomic_t cache_cell_clash;
194 atomic_t commit_count;
195 atomic_t discard_count;
196};
197
Joe Thornber65790ff2013-11-08 16:39:50 +0000198/*
199 * Defines a range of cblocks, begin to (end - 1) are in the range. end is
200 * the one-past-the-end value.
201 */
202struct cblock_range {
203 dm_cblock_t begin;
204 dm_cblock_t end;
205};
206
207struct invalidation_request {
208 struct list_head list;
209 struct cblock_range *cblocks;
210
211 atomic_t complete;
212 int err;
213
214 wait_queue_head_t result_wait;
215};
216
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000217struct cache {
218 struct dm_target *ti;
219 struct dm_target_callbacks callbacks;
220
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400221 struct dm_cache_metadata *cmd;
222
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000223 /*
224 * Metadata is written to this device.
225 */
226 struct dm_dev *metadata_dev;
227
228 /*
229 * The slower of the two data devices. Typically a spindle.
230 */
231 struct dm_dev *origin_dev;
232
233 /*
234 * The faster of the two data devices. Typically an SSD.
235 */
236 struct dm_dev *cache_dev;
237
238 /*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000239 * Size of the origin device in _complete_ blocks and native sectors.
240 */
241 dm_oblock_t origin_blocks;
242 sector_t origin_sectors;
243
244 /*
245 * Size of the cache device in blocks.
246 */
247 dm_cblock_t cache_size;
248
249 /*
250 * Fields for converting from sectors to blocks.
251 */
Joe Thornberca763d02017-02-09 11:46:18 -0500252 sector_t sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000253 int sectors_per_block_shift;
254
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000255 spinlock_t lock;
Joe Thornber651f5fa2015-05-15 15:26:08 +0100256 struct list_head deferred_cells;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000257 struct bio_list deferred_bios;
258 struct bio_list deferred_flush_bios;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000259 struct bio_list deferred_writethrough_bios;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000260 struct list_head quiesced_migrations;
261 struct list_head completed_migrations;
262 struct list_head need_commit_migrations;
263 sector_t migration_threshold;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000264 wait_queue_head_t migration_wait;
Joe Thornbera59db672015-01-23 10:16:16 +0000265 atomic_t nr_allocated_migrations;
266
267 /*
268 * The number of in flight migrations that are performing
269 * background io. eg, promotion, writeback.
270 */
271 atomic_t nr_io_migrations;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000272
Joe Thornber66cb1912013-10-30 17:11:58 +0000273 wait_queue_head_t quiescing_wait;
Joe Thornber238f8362013-10-30 17:29:30 +0000274 atomic_t quiescing;
Joe Thornber66cb1912013-10-30 17:11:58 +0000275 atomic_t quiescing_ack;
276
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000277 /*
278 * cache_size entries, dirty if set
279 */
Anssi Hannula44fa8162014-08-01 11:55:47 -0400280 atomic_t nr_dirty;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000281 unsigned long *dirty_bitset;
282
283 /*
284 * origin_blocks entries, discarded if set.
285 */
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000286 dm_dblock_t discard_nr_blocks;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000287 unsigned long *discard_bitset;
Joe Thornber08b18452014-11-06 14:38:01 +0000288 uint32_t discard_block_size; /* a power of 2 times sectors per block */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400289
290 /*
291 * Rather than reconstructing the table line for the status we just
292 * save it and regurgitate.
293 */
294 unsigned nr_ctr_args;
295 const char **ctr_args;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000296
297 struct dm_kcopyd_client *copier;
298 struct workqueue_struct *wq;
299 struct work_struct worker;
300
301 struct delayed_work waker;
302 unsigned long last_commit_jiffies;
303
304 struct dm_bio_prison *prison;
305 struct dm_deferred_set *all_io_ds;
306
307 mempool_t *migration_pool;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000308
309 struct dm_cache_policy *policy;
310 unsigned policy_nr_args;
311
312 bool need_tick_bio:1;
313 bool sized:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000314 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000315 bool commit_requested:1;
316 bool loaded_mappings:1;
317 bool loaded_discards:1;
318
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000319 /*
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400320 * Cache features such as write-through.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000321 */
Mike Snitzerc9ec5d72013-08-16 10:54:21 -0400322 struct cache_features features;
323
324 struct cache_stats stats;
Joe Thornber65790ff2013-11-08 16:39:50 +0000325
326 /*
327 * Invalidation fields.
328 */
329 spinlock_t invalidation_lock;
330 struct list_head invalidation_requests;
Joe Thornber066dbaa32015-05-15 15:18:01 +0100331
332 struct io_tracker origin_tracker;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000333};
334
335struct per_bio_data {
336 bool tick:1;
337 unsigned req_nr:2;
338 struct dm_deferred_entry *all_io_entry;
Mike Snitzerc6eda5e2014-01-31 14:11:54 -0500339 struct dm_hook_info hook_info;
Joe Thornber066dbaa32015-05-15 15:18:01 +0100340 sector_t len;
Joe Thornbere2e74d62013-03-20 17:21:27 +0000341
Mike Snitzer19b00922013-04-05 15:36:34 +0100342 /*
343 * writethrough fields. These MUST remain at the end of this
344 * structure and the 'cache' member must be the first as it
Joe Thornberaeed1422013-05-10 14:37:18 +0100345 * is used to determine the offset of the writethrough fields.
Mike Snitzer19b00922013-04-05 15:36:34 +0100346 */
Joe Thornbere2e74d62013-03-20 17:21:27 +0000347 struct cache *cache;
348 dm_cblock_t cblock;
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100349 struct dm_bio_details bio_details;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000350};
351
352struct dm_cache_migration {
353 struct list_head list;
354 struct cache *cache;
355
356 unsigned long start_jiffies;
357 dm_oblock_t old_oblock;
358 dm_oblock_t new_oblock;
359 dm_cblock_t cblock;
360
361 bool err:1;
Joe Thornber7ae34e72014-11-06 10:18:04 +0000362 bool discard:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000363 bool writeback:1;
364 bool demote:1;
365 bool promote:1;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400366 bool requeue_holder:1;
Joe Thornber65790ff2013-11-08 16:39:50 +0000367 bool invalidate:1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000368
369 struct dm_bio_prison_cell *old_ocell;
370 struct dm_bio_prison_cell *new_ocell;
371};
372
373/*
374 * Processing a bio in the worker thread may require these memory
375 * allocations. We prealloc to avoid deadlocks (the same worker thread
376 * frees them back to the mempool).
377 */
378struct prealloc {
379 struct dm_cache_migration *mg;
380 struct dm_bio_prison_cell *cell1;
381 struct dm_bio_prison_cell *cell2;
382};
383
Joe Thornber028ae9f2015-04-22 16:42:35 -0400384static enum cache_metadata_mode get_cache_mode(struct cache *cache);
385
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000386static void wake_worker(struct cache *cache)
387{
388 queue_work(cache->wq, &cache->worker);
389}
390
391/*----------------------------------------------------------------*/
392
393static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
394{
395 /* FIXME: change to use a local slab. */
396 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
397}
398
399static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
400{
401 dm_bio_prison_free_cell(cache->prison, cell);
402}
403
Joe Thornbera59db672015-01-23 10:16:16 +0000404static struct dm_cache_migration *alloc_migration(struct cache *cache)
405{
406 struct dm_cache_migration *mg;
407
408 mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
409 if (mg) {
410 mg->cache = cache;
411 atomic_inc(&mg->cache->nr_allocated_migrations);
412 }
413
414 return mg;
415}
416
417static void free_migration(struct dm_cache_migration *mg)
418{
Joe Thornber88bf51842015-05-27 15:39:45 +0100419 struct cache *cache = mg->cache;
Joe Thornbera59db672015-01-23 10:16:16 +0000420
Joe Thornber88bf51842015-05-27 15:39:45 +0100421 if (atomic_dec_and_test(&cache->nr_allocated_migrations))
422 wake_up(&cache->migration_wait);
423
424 mempool_free(mg, cache->migration_pool);
Joe Thornbera59db672015-01-23 10:16:16 +0000425}
426
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000427static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
428{
429 if (!p->mg) {
Joe Thornbera59db672015-01-23 10:16:16 +0000430 p->mg = alloc_migration(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000431 if (!p->mg)
432 return -ENOMEM;
433 }
434
435 if (!p->cell1) {
436 p->cell1 = alloc_prison_cell(cache);
437 if (!p->cell1)
438 return -ENOMEM;
439 }
440
441 if (!p->cell2) {
442 p->cell2 = alloc_prison_cell(cache);
443 if (!p->cell2)
444 return -ENOMEM;
445 }
446
447 return 0;
448}
449
450static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
451{
452 if (p->cell2)
453 free_prison_cell(cache, p->cell2);
454
455 if (p->cell1)
456 free_prison_cell(cache, p->cell1);
457
458 if (p->mg)
Joe Thornbera59db672015-01-23 10:16:16 +0000459 free_migration(p->mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000460}
461
462static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
463{
464 struct dm_cache_migration *mg = p->mg;
465
466 BUG_ON(!mg);
467 p->mg = NULL;
468
469 return mg;
470}
471
472/*
473 * You must have a cell within the prealloc struct to return. If not this
474 * function will BUG() rather than returning NULL.
475 */
476static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
477{
478 struct dm_bio_prison_cell *r = NULL;
479
480 if (p->cell1) {
481 r = p->cell1;
482 p->cell1 = NULL;
483
484 } else if (p->cell2) {
485 r = p->cell2;
486 p->cell2 = NULL;
487 } else
488 BUG();
489
490 return r;
491}
492
493/*
494 * You can't have more than two cells in a prealloc struct. BUG() will be
495 * called if you try and overfill.
496 */
497static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
498{
499 if (!p->cell2)
500 p->cell2 = cell;
501
502 else if (!p->cell1)
503 p->cell1 = cell;
504
505 else
506 BUG();
507}
508
509/*----------------------------------------------------------------*/
510
Joe Thornber7ae34e72014-11-06 10:18:04 +0000511static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key *key)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000512{
513 key->virtual = 0;
514 key->dev = 0;
Joe Thornber7ae34e72014-11-06 10:18:04 +0000515 key->block_begin = from_oblock(begin);
516 key->block_end = from_oblock(end);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000517}
518
519/*
520 * The caller hands in a preallocated cell, and a free function for it.
521 * The cell will be freed if there's an error, or if it wasn't used because
522 * a cell with that key already exists.
523 */
524typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
525
Joe Thornber7ae34e72014-11-06 10:18:04 +0000526static int bio_detain_range(struct cache *cache, dm_oblock_t oblock_begin, dm_oblock_t oblock_end,
527 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
528 cell_free_fn free_fn, void *free_context,
529 struct dm_bio_prison_cell **cell_result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000530{
531 int r;
532 struct dm_cell_key key;
533
Joe Thornber7ae34e72014-11-06 10:18:04 +0000534 build_key(oblock_begin, oblock_end, &key);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000535 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
536 if (r)
537 free_fn(free_context, cell_prealloc);
538
539 return r;
540}
541
Joe Thornber7ae34e72014-11-06 10:18:04 +0000542static int bio_detain(struct cache *cache, dm_oblock_t oblock,
543 struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
544 cell_free_fn free_fn, void *free_context,
545 struct dm_bio_prison_cell **cell_result)
546{
547 dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL);
548 return bio_detain_range(cache, oblock, end, bio,
549 cell_prealloc, free_fn, free_context, cell_result);
550}
551
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000552static int get_cell(struct cache *cache,
553 dm_oblock_t oblock,
554 struct prealloc *structs,
555 struct dm_bio_prison_cell **cell_result)
556{
557 int r;
558 struct dm_cell_key key;
559 struct dm_bio_prison_cell *cell_prealloc;
560
561 cell_prealloc = prealloc_get_cell(structs);
562
Joe Thornber7ae34e72014-11-06 10:18:04 +0000563 build_key(oblock, to_oblock(from_oblock(oblock) + 1ULL), &key);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000564 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
565 if (r)
566 prealloc_put_cell(structs, cell_prealloc);
567
568 return r;
569}
570
Joe Thornberaeed1422013-05-10 14:37:18 +0100571/*----------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000572
573static bool is_dirty(struct cache *cache, dm_cblock_t b)
574{
575 return test_bit(from_cblock(b), cache->dirty_bitset);
576}
577
578static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
579{
580 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
Anssi Hannula44fa8162014-08-01 11:55:47 -0400581 atomic_inc(&cache->nr_dirty);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000582 policy_set_dirty(cache->policy, oblock);
583 }
584}
585
586static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
587{
588 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
589 policy_clear_dirty(cache->policy, oblock);
Anssi Hannula44fa8162014-08-01 11:55:47 -0400590 if (atomic_dec_return(&cache->nr_dirty) == 0)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000591 dm_table_event(cache->ti->table);
592 }
593}
594
595/*----------------------------------------------------------------*/
Joe Thornberaeed1422013-05-10 14:37:18 +0100596
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000597static bool block_size_is_power_of_two(struct cache *cache)
598{
599 return cache->sectors_per_block_shift >= 0;
600}
601
Mikulas Patocka43aeaa22013-07-10 23:41:17 +0100602/* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
603#if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
604__always_inline
605#endif
Joe Thornber414dd672013-03-20 17:21:25 +0000606static dm_block_t block_div(dm_block_t b, uint32_t n)
607{
608 do_div(b, n);
609
610 return b;
611}
612
Joe Thornber7ae34e72014-11-06 10:18:04 +0000613static dm_block_t oblocks_per_dblock(struct cache *cache)
614{
615 dm_block_t oblocks = cache->discard_block_size;
616
617 if (block_size_is_power_of_two(cache))
618 oblocks >>= cache->sectors_per_block_shift;
619 else
620 oblocks = block_div(oblocks, cache->sectors_per_block);
621
622 return oblocks;
623}
624
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000625static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
626{
Joe Thornber7ae34e72014-11-06 10:18:04 +0000627 return to_dblock(block_div(from_oblock(oblock),
628 oblocks_per_dblock(cache)));
629}
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000630
Joe Thornber7ae34e72014-11-06 10:18:04 +0000631static dm_oblock_t dblock_to_oblock(struct cache *cache, dm_dblock_t dblock)
632{
633 return to_oblock(from_dblock(dblock) * oblocks_per_dblock(cache));
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000634}
635
636static void set_discard(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000637{
638 unsigned long flags;
639
Joe Thornber7ae34e72014-11-06 10:18:04 +0000640 BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000641 atomic_inc(&cache->stats.discard_count);
642
643 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000644 set_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000645 spin_unlock_irqrestore(&cache->lock, flags);
646}
647
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000648static void clear_discard(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000649{
650 unsigned long flags;
651
652 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000653 clear_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000654 spin_unlock_irqrestore(&cache->lock, flags);
655}
656
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000657static bool is_discarded(struct cache *cache, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000658{
659 int r;
660 unsigned long flags;
661
662 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000663 r = test_bit(from_dblock(b), cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000664 spin_unlock_irqrestore(&cache->lock, flags);
665
666 return r;
667}
668
669static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
670{
671 int r;
672 unsigned long flags;
673
674 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000675 r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
676 cache->discard_bitset);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000677 spin_unlock_irqrestore(&cache->lock, flags);
678
679 return r;
680}
681
682/*----------------------------------------------------------------*/
683
684static void load_stats(struct cache *cache)
685{
686 struct dm_cache_statistics stats;
687
688 dm_cache_metadata_get_stats(cache->cmd, &stats);
689 atomic_set(&cache->stats.read_hit, stats.read_hits);
690 atomic_set(&cache->stats.read_miss, stats.read_misses);
691 atomic_set(&cache->stats.write_hit, stats.write_hits);
692 atomic_set(&cache->stats.write_miss, stats.write_misses);
693}
694
695static void save_stats(struct cache *cache)
696{
697 struct dm_cache_statistics stats;
698
Joe Thornber028ae9f2015-04-22 16:42:35 -0400699 if (get_cache_mode(cache) >= CM_READ_ONLY)
700 return;
701
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000702 stats.read_hits = atomic_read(&cache->stats.read_hit);
703 stats.read_misses = atomic_read(&cache->stats.read_miss);
704 stats.write_hits = atomic_read(&cache->stats.write_hit);
705 stats.write_misses = atomic_read(&cache->stats.write_miss);
706
707 dm_cache_metadata_set_stats(cache->cmd, &stats);
708}
709
710/*----------------------------------------------------------------
711 * Per bio data
712 *--------------------------------------------------------------*/
Mike Snitzer19b00922013-04-05 15:36:34 +0100713
714/*
715 * If using writeback, leave out struct per_bio_data's writethrough fields.
716 */
717#define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
718#define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
719
Joe Thornber2ee57d52013-10-24 14:10:29 -0400720static bool writethrough_mode(struct cache_features *f)
721{
722 return f->io_mode == CM_IO_WRITETHROUGH;
723}
724
725static bool writeback_mode(struct cache_features *f)
726{
727 return f->io_mode == CM_IO_WRITEBACK;
728}
729
730static bool passthrough_mode(struct cache_features *f)
731{
732 return f->io_mode == CM_IO_PASSTHROUGH;
733}
734
Mike Snitzer19b00922013-04-05 15:36:34 +0100735static size_t get_per_bio_data_size(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000736{
Joe Thornber2ee57d52013-10-24 14:10:29 -0400737 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
Mike Snitzer19b00922013-04-05 15:36:34 +0100738}
739
740static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
741{
742 struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000743 BUG_ON(!pb);
744 return pb;
745}
746
Mike Snitzer19b00922013-04-05 15:36:34 +0100747static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000748{
Mike Snitzer19b00922013-04-05 15:36:34 +0100749 struct per_bio_data *pb = get_per_bio_data(bio, data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000750
751 pb->tick = false;
752 pb->req_nr = dm_bio_get_target_bio_nr(bio);
753 pb->all_io_entry = NULL;
Joe Thornber066dbaa32015-05-15 15:18:01 +0100754 pb->len = 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000755
756 return pb;
757}
758
759/*----------------------------------------------------------------
760 * Remapping
761 *--------------------------------------------------------------*/
762static void remap_to_origin(struct cache *cache, struct bio *bio)
763{
764 bio->bi_bdev = cache->origin_dev->bdev;
765}
766
767static void remap_to_cache(struct cache *cache, struct bio *bio,
768 dm_cblock_t cblock)
769{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700770 sector_t bi_sector = bio->bi_iter.bi_sector;
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100771 sector_t block = from_cblock(cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000772
773 bio->bi_bdev = cache->cache_dev->bdev;
774 if (!block_size_is_power_of_two(cache))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700775 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100776 (block * cache->sectors_per_block) +
Kent Overstreet4f024f32013-10-11 15:44:27 -0700777 sector_div(bi_sector, cache->sectors_per_block);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000778 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700779 bio->bi_iter.bi_sector =
Heinz Mauelshagene0d849f2014-02-27 22:46:48 +0100780 (block << cache->sectors_per_block_shift) |
Kent Overstreet4f024f32013-10-11 15:44:27 -0700781 (bi_sector & (cache->sectors_per_block - 1));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000782}
783
784static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
785{
786 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +0100787 size_t pb_data_size = get_per_bio_data_size(cache);
788 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000789
790 spin_lock_irqsave(&cache->lock, flags);
791 if (cache->need_tick_bio &&
Jens Axboe1eff9d32016-08-05 15:35:16 -0600792 !(bio->bi_opf & (REQ_FUA | REQ_PREFLUSH)) &&
Mike Christiee6047142016-06-05 14:32:04 -0500793 bio_op(bio) != REQ_OP_DISCARD) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000794 pb->tick = true;
795 cache->need_tick_bio = false;
796 }
797 spin_unlock_irqrestore(&cache->lock, flags);
798}
799
800static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
801 dm_oblock_t oblock)
802{
803 check_if_tick_bio_needed(cache, bio);
804 remap_to_origin(cache, bio);
805 if (bio_data_dir(bio) == WRITE)
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000806 clear_discard(cache, oblock_to_dblock(cache, oblock));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000807}
808
809static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
810 dm_oblock_t oblock, dm_cblock_t cblock)
811{
Joe Thornberf8e5f012013-10-21 12:51:45 +0100812 check_if_tick_bio_needed(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000813 remap_to_cache(cache, bio, cblock);
814 if (bio_data_dir(bio) == WRITE) {
815 set_dirty(cache, oblock, cblock);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000816 clear_discard(cache, oblock_to_dblock(cache, oblock));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000817 }
818}
819
820static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
821{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700822 sector_t block_nr = bio->bi_iter.bi_sector;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000823
824 if (!block_size_is_power_of_two(cache))
825 (void) sector_div(block_nr, cache->sectors_per_block);
826 else
827 block_nr >>= cache->sectors_per_block_shift;
828
829 return to_oblock(block_nr);
830}
831
832static int bio_triggers_commit(struct cache *cache, struct bio *bio)
833{
Jens Axboe1eff9d32016-08-05 15:35:16 -0600834 return bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000835}
836
Joe Thornber8c081b52014-05-13 16:18:38 +0100837/*
838 * You must increment the deferred set whilst the prison cell is held. To
839 * encourage this, we ask for 'cell' to be passed in.
840 */
841static void inc_ds(struct cache *cache, struct bio *bio,
842 struct dm_bio_prison_cell *cell)
843{
844 size_t pb_data_size = get_per_bio_data_size(cache);
845 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
846
847 BUG_ON(!cell);
848 BUG_ON(pb->all_io_entry);
849
850 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
851}
852
Joe Thornber066dbaa32015-05-15 15:18:01 +0100853static bool accountable_bio(struct cache *cache, struct bio *bio)
854{
855 return ((bio->bi_bdev == cache->origin_dev->bdev) &&
Mike Christiee6047142016-06-05 14:32:04 -0500856 bio_op(bio) != REQ_OP_DISCARD);
Joe Thornber066dbaa32015-05-15 15:18:01 +0100857}
858
859static void accounted_begin(struct cache *cache, struct bio *bio)
860{
861 size_t pb_data_size = get_per_bio_data_size(cache);
862 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
863
864 if (accountable_bio(cache, bio)) {
865 pb->len = bio_sectors(bio);
866 iot_io_begin(&cache->origin_tracker, pb->len);
867 }
868}
869
870static void accounted_complete(struct cache *cache, struct bio *bio)
871{
872 size_t pb_data_size = get_per_bio_data_size(cache);
873 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
874
875 iot_io_end(&cache->origin_tracker, pb->len);
876}
877
878static void accounted_request(struct cache *cache, struct bio *bio)
879{
880 accounted_begin(cache, bio);
881 generic_make_request(bio);
882}
883
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000884static void issue(struct cache *cache, struct bio *bio)
885{
886 unsigned long flags;
887
888 if (!bio_triggers_commit(cache, bio)) {
Joe Thornber066dbaa32015-05-15 15:18:01 +0100889 accounted_request(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000890 return;
891 }
892
893 /*
894 * Batch together any bios that trigger commits and then issue a
895 * single commit for them in do_worker().
896 */
897 spin_lock_irqsave(&cache->lock, flags);
898 cache->commit_requested = true;
899 bio_list_add(&cache->deferred_flush_bios, bio);
900 spin_unlock_irqrestore(&cache->lock, flags);
901}
902
Joe Thornber8c081b52014-05-13 16:18:38 +0100903static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
904{
905 inc_ds(cache, bio, cell);
906 issue(cache, bio);
907}
908
Joe Thornbere2e74d62013-03-20 17:21:27 +0000909static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
910{
911 unsigned long flags;
912
913 spin_lock_irqsave(&cache->lock, flags);
914 bio_list_add(&cache->deferred_writethrough_bios, bio);
915 spin_unlock_irqrestore(&cache->lock, flags);
916
917 wake_worker(cache);
918}
919
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200920static void writethrough_endio(struct bio *bio)
Joe Thornbere2e74d62013-03-20 17:21:27 +0000921{
Mike Snitzer19b00922013-04-05 15:36:34 +0100922 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornberc9d28d52013-10-31 13:55:48 -0400923
924 dm_unhook_bio(&pb->hook_info, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000925
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200926 if (bio->bi_error) {
927 bio_endio(bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000928 return;
929 }
930
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100931 dm_bio_restore(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000932 remap_to_cache(pb->cache, bio, pb->cblock);
933
934 /*
935 * We can't issue this bio directly, since we're in interrupt
Joe Thornberaeed1422013-05-10 14:37:18 +0100936 * context. So it gets put on a bio list for processing by the
Joe Thornbere2e74d62013-03-20 17:21:27 +0000937 * worker thread.
938 */
939 defer_writethrough_bio(pb->cache, bio);
940}
941
942/*
943 * When running in writethrough mode we need to send writes to clean blocks
944 * to both the cache and origin devices. In future we'd like to clone the
945 * bio and send them in parallel, but for now we're doing them in
946 * series as this is easier.
947 */
948static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
949 dm_oblock_t oblock, dm_cblock_t cblock)
950{
Mike Snitzer19b00922013-04-05 15:36:34 +0100951 struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000952
953 pb->cache = cache;
954 pb->cblock = cblock;
Joe Thornberc9d28d52013-10-31 13:55:48 -0400955 dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
Darrick J. Wongb844fe62013-04-05 15:36:32 +0100956 dm_bio_record(&pb->bio_details, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +0000957
958 remap_to_origin_clear_discard(pb->cache, bio, oblock);
959}
960
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000961/*----------------------------------------------------------------
Joe Thornber028ae9f2015-04-22 16:42:35 -0400962 * Failure modes
963 *--------------------------------------------------------------*/
964static enum cache_metadata_mode get_cache_mode(struct cache *cache)
965{
966 return cache->features.mode;
967}
968
Mike Snitzerb61d9502015-04-22 17:25:56 -0400969static const char *cache_device_name(struct cache *cache)
970{
971 return dm_device_name(dm_table_get_md(cache->ti->table));
972}
973
Joe Thornber028ae9f2015-04-22 16:42:35 -0400974static void notify_mode_switch(struct cache *cache, enum cache_metadata_mode mode)
975{
976 const char *descs[] = {
977 "write",
978 "read-only",
979 "fail"
980 };
981
982 dm_table_event(cache->ti->table);
Mike Snitzerb61d9502015-04-22 17:25:56 -0400983 DMINFO("%s: switching cache to %s mode",
984 cache_device_name(cache), descs[(int)mode]);
Joe Thornber028ae9f2015-04-22 16:42:35 -0400985}
986
987static void set_cache_mode(struct cache *cache, enum cache_metadata_mode new_mode)
988{
Joe Thornberd14fcf32016-03-10 16:20:58 +0000989 bool needs_check;
Joe Thornber028ae9f2015-04-22 16:42:35 -0400990 enum cache_metadata_mode old_mode = get_cache_mode(cache);
991
Joe Thornberd14fcf32016-03-10 16:20:58 +0000992 if (dm_cache_metadata_needs_check(cache->cmd, &needs_check)) {
Mike Snitzer23cab262016-10-04 12:04:08 -0400993 DMERR("%s: unable to read needs_check flag, setting failure mode.",
994 cache_device_name(cache));
Joe Thornberd14fcf32016-03-10 16:20:58 +0000995 new_mode = CM_FAIL;
996 }
997
Joe Thornber028ae9f2015-04-22 16:42:35 -0400998 if (new_mode == CM_WRITE && needs_check) {
Mike Snitzerb61d9502015-04-22 17:25:56 -0400999 DMERR("%s: unable to switch cache to write mode until repaired.",
1000 cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04001001 if (old_mode != new_mode)
1002 new_mode = old_mode;
1003 else
1004 new_mode = CM_READ_ONLY;
1005 }
1006
1007 /* Never move out of fail mode */
1008 if (old_mode == CM_FAIL)
1009 new_mode = CM_FAIL;
1010
1011 switch (new_mode) {
1012 case CM_FAIL:
1013 case CM_READ_ONLY:
1014 dm_cache_metadata_set_read_only(cache->cmd);
1015 break;
1016
1017 case CM_WRITE:
1018 dm_cache_metadata_set_read_write(cache->cmd);
1019 break;
1020 }
1021
1022 cache->features.mode = new_mode;
1023
1024 if (new_mode != old_mode)
1025 notify_mode_switch(cache, new_mode);
1026}
1027
1028static void abort_transaction(struct cache *cache)
1029{
Mike Snitzerb61d9502015-04-22 17:25:56 -04001030 const char *dev_name = cache_device_name(cache);
1031
Joe Thornber028ae9f2015-04-22 16:42:35 -04001032 if (get_cache_mode(cache) >= CM_READ_ONLY)
1033 return;
1034
1035 if (dm_cache_metadata_set_needs_check(cache->cmd)) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001036 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001037 set_cache_mode(cache, CM_FAIL);
1038 }
1039
Mike Snitzerb61d9502015-04-22 17:25:56 -04001040 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001041 if (dm_cache_metadata_abort(cache->cmd)) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001042 DMERR("%s: failed to abort metadata transaction", dev_name);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001043 set_cache_mode(cache, CM_FAIL);
1044 }
1045}
1046
1047static void metadata_operation_failed(struct cache *cache, const char *op, int r)
1048{
Mike Snitzerb61d9502015-04-22 17:25:56 -04001049 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
1050 cache_device_name(cache), op, r);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001051 abort_transaction(cache);
1052 set_cache_mode(cache, CM_READ_ONLY);
1053}
1054
1055/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001056 * Migration processing
1057 *
1058 * Migration covers moving data from the origin device to the cache, or
1059 * vice versa.
1060 *--------------------------------------------------------------*/
Joe Thornbera59db672015-01-23 10:16:16 +00001061static void inc_io_migrations(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001062{
Joe Thornbera59db672015-01-23 10:16:16 +00001063 atomic_inc(&cache->nr_io_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001064}
1065
Joe Thornbera59db672015-01-23 10:16:16 +00001066static void dec_io_migrations(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001067{
Joe Thornbera59db672015-01-23 10:16:16 +00001068 atomic_dec(&cache->nr_io_migrations);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001069}
1070
Joe Thornber651f5fa2015-05-15 15:26:08 +01001071static bool discard_or_flush(struct bio *bio)
1072{
Mike Christiee6047142016-06-05 14:32:04 -05001073 return bio_op(bio) == REQ_OP_DISCARD ||
Jens Axboe1eff9d32016-08-05 15:35:16 -06001074 bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Joe Thornber651f5fa2015-05-15 15:26:08 +01001075}
1076
1077static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell)
1078{
Mike Snitzerdc9cee52015-08-31 15:41:34 -04001079 if (discard_or_flush(cell->holder)) {
Joe Thornber651f5fa2015-05-15 15:26:08 +01001080 /*
Mike Snitzerdc9cee52015-08-31 15:41:34 -04001081 * We have to handle these bios individually.
Joe Thornber651f5fa2015-05-15 15:26:08 +01001082 */
Mike Snitzerdc9cee52015-08-31 15:41:34 -04001083 dm_cell_release(cache->prison, cell, &cache->deferred_bios);
1084 free_prison_cell(cache, cell);
1085 } else
Joe Thornber651f5fa2015-05-15 15:26:08 +01001086 list_add_tail(&cell->user_list, &cache->deferred_cells);
1087}
1088
1089static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, bool holder)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001090{
1091 unsigned long flags;
1092
Joe Thornber651f5fa2015-05-15 15:26:08 +01001093 if (!holder && dm_cell_promote_or_release(cache->prison, cell)) {
1094 /*
1095 * There was no prisoner to promote to holder, the
1096 * cell has been released.
1097 */
1098 free_prison_cell(cache, cell);
1099 return;
1100 }
1101
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001102 spin_lock_irqsave(&cache->lock, flags);
Joe Thornber651f5fa2015-05-15 15:26:08 +01001103 __cell_defer(cache, cell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001104 spin_unlock_irqrestore(&cache->lock, flags);
1105
1106 wake_worker(cache);
1107}
1108
Joe Thornber651f5fa2015-05-15 15:26:08 +01001109static void cell_error_with_code(struct cache *cache, struct dm_bio_prison_cell *cell, int err)
1110{
1111 dm_cell_error(cache->prison, cell, err);
Mike Snitzerdc9cee52015-08-31 15:41:34 -04001112 free_prison_cell(cache, cell);
Joe Thornber651f5fa2015-05-15 15:26:08 +01001113}
1114
1115static void cell_requeue(struct cache *cache, struct dm_bio_prison_cell *cell)
1116{
1117 cell_error_with_code(cache, cell, DM_ENDIO_REQUEUE);
1118}
1119
Joe Thornbera59db672015-01-23 10:16:16 +00001120static void free_io_migration(struct dm_cache_migration *mg)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001121{
Joe Thornbercc7da0b2015-09-01 11:38:19 +01001122 struct cache *cache = mg->cache;
1123
1124 dec_io_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001125 free_migration(mg);
Joe Thornbercc7da0b2015-09-01 11:38:19 +01001126 wake_worker(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001127}
1128
1129static void migration_failure(struct dm_cache_migration *mg)
1130{
1131 struct cache *cache = mg->cache;
Mike Snitzerb61d9502015-04-22 17:25:56 -04001132 const char *dev_name = cache_device_name(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001133
1134 if (mg->writeback) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001135 DMERR_LIMIT("%s: writeback failed; couldn't copy block", dev_name);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001136 set_dirty(cache, mg->old_oblock, mg->cblock);
1137 cell_defer(cache, mg->old_ocell, false);
1138
1139 } else if (mg->demote) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001140 DMERR_LIMIT("%s: demotion failed; couldn't copy block", dev_name);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001141 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
1142
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +02001143 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001144 if (mg->promote)
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +02001145 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001146 } else {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001147 DMERR_LIMIT("%s: promotion failed; couldn't copy block", dev_name);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001148 policy_remove_mapping(cache->policy, mg->new_oblock);
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +02001149 cell_defer(cache, mg->new_ocell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001150 }
1151
Joe Thornbera59db672015-01-23 10:16:16 +00001152 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001153}
1154
1155static void migration_success_pre_commit(struct dm_cache_migration *mg)
1156{
Joe Thornber028ae9f2015-04-22 16:42:35 -04001157 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001158 unsigned long flags;
1159 struct cache *cache = mg->cache;
1160
1161 if (mg->writeback) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001162 clear_dirty(cache, mg->old_oblock, mg->cblock);
Anssi Hannula40aa9782014-09-05 03:11:28 +03001163 cell_defer(cache, mg->old_ocell, false);
Joe Thornbera59db672015-01-23 10:16:16 +00001164 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001165 return;
1166
1167 } else if (mg->demote) {
Joe Thornber028ae9f2015-04-22 16:42:35 -04001168 r = dm_cache_remove_mapping(cache->cmd, mg->cblock);
1169 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001170 DMERR_LIMIT("%s: demotion failed; couldn't update on disk metadata",
1171 cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04001172 metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001173 policy_force_mapping(cache->policy, mg->new_oblock,
1174 mg->old_oblock);
1175 if (mg->promote)
1176 cell_defer(cache, mg->new_ocell, true);
Joe Thornbera59db672015-01-23 10:16:16 +00001177 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001178 return;
1179 }
1180 } else {
Joe Thornber028ae9f2015-04-22 16:42:35 -04001181 r = dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock);
1182 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001183 DMERR_LIMIT("%s: promotion failed; couldn't update on disk metadata",
1184 cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04001185 metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001186 policy_remove_mapping(cache->policy, mg->new_oblock);
Joe Thornbera59db672015-01-23 10:16:16 +00001187 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001188 return;
1189 }
1190 }
1191
1192 spin_lock_irqsave(&cache->lock, flags);
1193 list_add_tail(&mg->list, &cache->need_commit_migrations);
1194 cache->commit_requested = true;
1195 spin_unlock_irqrestore(&cache->lock, flags);
1196}
1197
1198static void migration_success_post_commit(struct dm_cache_migration *mg)
1199{
1200 unsigned long flags;
1201 struct cache *cache = mg->cache;
1202
1203 if (mg->writeback) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001204 DMWARN_LIMIT("%s: writeback unexpectedly triggered commit",
1205 cache_device_name(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001206 return;
1207
1208 } else if (mg->demote) {
Heinz Mauelshagen80f659f2013-10-14 17:10:47 +02001209 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001210
1211 if (mg->promote) {
1212 mg->demote = false;
1213
1214 spin_lock_irqsave(&cache->lock, flags);
1215 list_add_tail(&mg->list, &cache->quiesced_migrations);
1216 spin_unlock_irqrestore(&cache->lock, flags);
1217
Joe Thornber65790ff2013-11-08 16:39:50 +00001218 } else {
1219 if (mg->invalidate)
1220 policy_remove_mapping(cache->policy, mg->old_oblock);
Joe Thornbera59db672015-01-23 10:16:16 +00001221 free_io_migration(mg);
Joe Thornber65790ff2013-11-08 16:39:50 +00001222 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001223
1224 } else {
Joe Thornber1e321342014-11-27 12:26:46 +00001225 if (mg->requeue_holder) {
1226 clear_dirty(cache, mg->new_oblock, mg->cblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001227 cell_defer(cache, mg->new_ocell, true);
Joe Thornber1e321342014-11-27 12:26:46 +00001228 } else {
1229 /*
1230 * The block was promoted via an overwrite, so it's dirty.
1231 */
1232 set_dirty(cache, mg->new_oblock, mg->cblock);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001233 bio_endio(mg->new_ocell->holder);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001234 cell_defer(cache, mg->new_ocell, false);
1235 }
Joe Thornbera59db672015-01-23 10:16:16 +00001236 free_io_migration(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001237 }
1238}
1239
1240static void copy_complete(int read_err, unsigned long write_err, void *context)
1241{
1242 unsigned long flags;
1243 struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
1244 struct cache *cache = mg->cache;
1245
1246 if (read_err || write_err)
1247 mg->err = true;
1248
1249 spin_lock_irqsave(&cache->lock, flags);
1250 list_add_tail(&mg->list, &cache->completed_migrations);
1251 spin_unlock_irqrestore(&cache->lock, flags);
1252
1253 wake_worker(cache);
1254}
1255
Joe Thornber7ae34e72014-11-06 10:18:04 +00001256static void issue_copy(struct dm_cache_migration *mg)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001257{
1258 int r;
1259 struct dm_io_region o_region, c_region;
1260 struct cache *cache = mg->cache;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001261 sector_t cblock = from_cblock(mg->cblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001262
1263 o_region.bdev = cache->origin_dev->bdev;
1264 o_region.count = cache->sectors_per_block;
1265
1266 c_region.bdev = cache->cache_dev->bdev;
Heinz Mauelshagen8b9d9662014-03-12 00:40:05 +01001267 c_region.sector = cblock * cache->sectors_per_block;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001268 c_region.count = cache->sectors_per_block;
1269
1270 if (mg->writeback || mg->demote) {
1271 /* demote */
1272 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
1273 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
1274 } else {
1275 /* promote */
1276 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1277 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1278 }
1279
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001280 if (r < 0) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04001281 DMERR_LIMIT("%s: issuing migration failed", cache_device_name(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001282 migration_failure(mg);
Heinz Mauelshagen2c2263c2013-10-14 17:14:45 +02001283 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001284}
1285
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001286static void overwrite_endio(struct bio *bio)
Joe Thornberc9d28d52013-10-31 13:55:48 -04001287{
1288 struct dm_cache_migration *mg = bio->bi_private;
1289 struct cache *cache = mg->cache;
1290 size_t pb_data_size = get_per_bio_data_size(cache);
1291 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1292 unsigned long flags;
1293
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001294 dm_unhook_bio(&pb->hook_info, bio);
1295
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001296 if (bio->bi_error)
Joe Thornberc9d28d52013-10-31 13:55:48 -04001297 mg->err = true;
1298
Mike Snitzer80ae49a2014-01-31 14:30:37 -05001299 mg->requeue_holder = false;
1300
Joe Thornberc9d28d52013-10-31 13:55:48 -04001301 spin_lock_irqsave(&cache->lock, flags);
1302 list_add_tail(&mg->list, &cache->completed_migrations);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001303 spin_unlock_irqrestore(&cache->lock, flags);
1304
1305 wake_worker(cache);
1306}
1307
1308static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
1309{
1310 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1311 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1312
1313 dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
1314 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001315
1316 /*
1317 * No need to inc_ds() here, since the cell will be held for the
1318 * duration of the io.
1319 */
Joe Thornber066dbaa32015-05-15 15:18:01 +01001320 accounted_request(mg->cache, bio);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001321}
1322
1323static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1324{
1325 return (bio_data_dir(bio) == WRITE) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001326 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
Joe Thornberc9d28d52013-10-31 13:55:48 -04001327}
1328
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001329static void avoid_copy(struct dm_cache_migration *mg)
1330{
1331 atomic_inc(&mg->cache->stats.copies_avoided);
1332 migration_success_pre_commit(mg);
1333}
1334
Joe Thornber7ae34e72014-11-06 10:18:04 +00001335static void calc_discard_block_range(struct cache *cache, struct bio *bio,
1336 dm_dblock_t *b, dm_dblock_t *e)
1337{
1338 sector_t sb = bio->bi_iter.bi_sector;
1339 sector_t se = bio_end_sector(bio);
1340
1341 *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
1342
1343 if (se - sb < cache->discard_block_size)
1344 *e = *b;
1345 else
1346 *e = to_dblock(block_div(se, cache->discard_block_size));
1347}
1348
1349static void issue_discard(struct dm_cache_migration *mg)
1350{
1351 dm_dblock_t b, e;
1352 struct bio *bio = mg->new_ocell->holder;
Joe Thornbercc7da0b2015-09-01 11:38:19 +01001353 struct cache *cache = mg->cache;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001354
Joe Thornbercc7da0b2015-09-01 11:38:19 +01001355 calc_discard_block_range(cache, bio, &b, &e);
Joe Thornber7ae34e72014-11-06 10:18:04 +00001356 while (b != e) {
Joe Thornbercc7da0b2015-09-01 11:38:19 +01001357 set_discard(cache, b);
Joe Thornber7ae34e72014-11-06 10:18:04 +00001358 b = to_dblock(from_dblock(b) + 1);
1359 }
1360
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001361 bio_endio(bio);
Joe Thornbercc7da0b2015-09-01 11:38:19 +01001362 cell_defer(cache, mg->new_ocell, false);
Joe Thornber7ae34e72014-11-06 10:18:04 +00001363 free_migration(mg);
Joe Thornbercc7da0b2015-09-01 11:38:19 +01001364 wake_worker(cache);
Joe Thornber7ae34e72014-11-06 10:18:04 +00001365}
1366
1367static void issue_copy_or_discard(struct dm_cache_migration *mg)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001368{
1369 bool avoid;
1370 struct cache *cache = mg->cache;
1371
Joe Thornber7ae34e72014-11-06 10:18:04 +00001372 if (mg->discard) {
1373 issue_discard(mg);
1374 return;
1375 }
1376
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001377 if (mg->writeback || mg->demote)
1378 avoid = !is_dirty(cache, mg->cblock) ||
1379 is_discarded_oblock(cache, mg->old_oblock);
Joe Thornberc9d28d52013-10-31 13:55:48 -04001380 else {
1381 struct bio *bio = mg->new_ocell->holder;
1382
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001383 avoid = is_discarded_oblock(cache, mg->new_oblock);
1384
Joe Thornberf29a3142014-11-27 12:21:08 +00001385 if (writeback_mode(&cache->features) &&
1386 !avoid && bio_writes_complete_block(cache, bio)) {
Joe Thornberc9d28d52013-10-31 13:55:48 -04001387 issue_overwrite(mg, bio);
1388 return;
1389 }
1390 }
1391
Joe Thornber7ae34e72014-11-06 10:18:04 +00001392 avoid ? avoid_copy(mg) : issue_copy(mg);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001393}
1394
1395static void complete_migration(struct dm_cache_migration *mg)
1396{
1397 if (mg->err)
1398 migration_failure(mg);
1399 else
1400 migration_success_pre_commit(mg);
1401}
1402
1403static void process_migrations(struct cache *cache, struct list_head *head,
1404 void (*fn)(struct dm_cache_migration *))
1405{
1406 unsigned long flags;
1407 struct list_head list;
1408 struct dm_cache_migration *mg, *tmp;
1409
1410 INIT_LIST_HEAD(&list);
1411 spin_lock_irqsave(&cache->lock, flags);
1412 list_splice_init(head, &list);
1413 spin_unlock_irqrestore(&cache->lock, flags);
1414
1415 list_for_each_entry_safe(mg, tmp, &list, list)
1416 fn(mg);
1417}
1418
1419static void __queue_quiesced_migration(struct dm_cache_migration *mg)
1420{
1421 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1422}
1423
1424static void queue_quiesced_migration(struct dm_cache_migration *mg)
1425{
1426 unsigned long flags;
1427 struct cache *cache = mg->cache;
1428
1429 spin_lock_irqsave(&cache->lock, flags);
1430 __queue_quiesced_migration(mg);
1431 spin_unlock_irqrestore(&cache->lock, flags);
1432
1433 wake_worker(cache);
1434}
1435
1436static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1437{
1438 unsigned long flags;
1439 struct dm_cache_migration *mg, *tmp;
1440
1441 spin_lock_irqsave(&cache->lock, flags);
1442 list_for_each_entry_safe(mg, tmp, work, list)
1443 __queue_quiesced_migration(mg);
1444 spin_unlock_irqrestore(&cache->lock, flags);
1445
1446 wake_worker(cache);
1447}
1448
1449static void check_for_quiesced_migrations(struct cache *cache,
1450 struct per_bio_data *pb)
1451{
1452 struct list_head work;
1453
1454 if (!pb->all_io_entry)
1455 return;
1456
1457 INIT_LIST_HEAD(&work);
Joe Thornber8c081b52014-05-13 16:18:38 +01001458 dm_deferred_entry_dec(pb->all_io_entry, &work);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001459
1460 if (!list_empty(&work))
1461 queue_quiesced_migrations(cache, &work);
1462}
1463
1464static void quiesce_migration(struct dm_cache_migration *mg)
1465{
1466 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1467 queue_quiesced_migration(mg);
1468}
1469
1470static void promote(struct cache *cache, struct prealloc *structs,
1471 dm_oblock_t oblock, dm_cblock_t cblock,
1472 struct dm_bio_prison_cell *cell)
1473{
1474 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1475
1476 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001477 mg->discard = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001478 mg->writeback = false;
1479 mg->demote = false;
1480 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001481 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001482 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001483 mg->cache = cache;
1484 mg->new_oblock = oblock;
1485 mg->cblock = cblock;
1486 mg->old_ocell = NULL;
1487 mg->new_ocell = cell;
1488 mg->start_jiffies = jiffies;
1489
Joe Thornbera59db672015-01-23 10:16:16 +00001490 inc_io_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001491 quiesce_migration(mg);
1492}
1493
1494static void writeback(struct cache *cache, struct prealloc *structs,
1495 dm_oblock_t oblock, dm_cblock_t cblock,
1496 struct dm_bio_prison_cell *cell)
1497{
1498 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1499
1500 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001501 mg->discard = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001502 mg->writeback = true;
1503 mg->demote = false;
1504 mg->promote = false;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001505 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001506 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001507 mg->cache = cache;
1508 mg->old_oblock = oblock;
1509 mg->cblock = cblock;
1510 mg->old_ocell = cell;
1511 mg->new_ocell = NULL;
1512 mg->start_jiffies = jiffies;
1513
Joe Thornbera59db672015-01-23 10:16:16 +00001514 inc_io_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001515 quiesce_migration(mg);
1516}
1517
1518static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1519 dm_oblock_t old_oblock, dm_oblock_t new_oblock,
1520 dm_cblock_t cblock,
1521 struct dm_bio_prison_cell *old_ocell,
1522 struct dm_bio_prison_cell *new_ocell)
1523{
1524 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1525
1526 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001527 mg->discard = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001528 mg->writeback = false;
1529 mg->demote = true;
1530 mg->promote = true;
Joe Thornberc9d28d52013-10-31 13:55:48 -04001531 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001532 mg->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001533 mg->cache = cache;
1534 mg->old_oblock = old_oblock;
1535 mg->new_oblock = new_oblock;
1536 mg->cblock = cblock;
1537 mg->old_ocell = old_ocell;
1538 mg->new_ocell = new_ocell;
1539 mg->start_jiffies = jiffies;
1540
Joe Thornbera59db672015-01-23 10:16:16 +00001541 inc_io_migrations(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001542 quiesce_migration(mg);
1543}
1544
Joe Thornber2ee57d52013-10-24 14:10:29 -04001545/*
1546 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1547 * block are thrown away.
1548 */
1549static void invalidate(struct cache *cache, struct prealloc *structs,
1550 dm_oblock_t oblock, dm_cblock_t cblock,
1551 struct dm_bio_prison_cell *cell)
1552{
1553 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1554
1555 mg->err = false;
Joe Thornber7ae34e72014-11-06 10:18:04 +00001556 mg->discard = false;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001557 mg->writeback = false;
1558 mg->demote = true;
1559 mg->promote = false;
1560 mg->requeue_holder = true;
Joe Thornber65790ff2013-11-08 16:39:50 +00001561 mg->invalidate = true;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001562 mg->cache = cache;
1563 mg->old_oblock = oblock;
1564 mg->cblock = cblock;
1565 mg->old_ocell = cell;
1566 mg->new_ocell = NULL;
1567 mg->start_jiffies = jiffies;
1568
Joe Thornbera59db672015-01-23 10:16:16 +00001569 inc_io_migrations(cache);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001570 quiesce_migration(mg);
1571}
1572
Joe Thornber7ae34e72014-11-06 10:18:04 +00001573static void discard(struct cache *cache, struct prealloc *structs,
1574 struct dm_bio_prison_cell *cell)
1575{
1576 struct dm_cache_migration *mg = prealloc_get_migration(structs);
1577
1578 mg->err = false;
1579 mg->discard = true;
1580 mg->writeback = false;
1581 mg->demote = false;
1582 mg->promote = false;
1583 mg->requeue_holder = false;
1584 mg->invalidate = false;
1585 mg->cache = cache;
1586 mg->old_ocell = NULL;
1587 mg->new_ocell = cell;
1588 mg->start_jiffies = jiffies;
1589
1590 quiesce_migration(mg);
1591}
1592
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001593/*----------------------------------------------------------------
1594 * bio processing
1595 *--------------------------------------------------------------*/
1596static void defer_bio(struct cache *cache, struct bio *bio)
1597{
1598 unsigned long flags;
1599
1600 spin_lock_irqsave(&cache->lock, flags);
1601 bio_list_add(&cache->deferred_bios, bio);
1602 spin_unlock_irqrestore(&cache->lock, flags);
1603
1604 wake_worker(cache);
1605}
1606
1607static void process_flush_bio(struct cache *cache, struct bio *bio)
1608{
Mike Snitzer19b00922013-04-05 15:36:34 +01001609 size_t pb_data_size = get_per_bio_data_size(cache);
1610 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001611
Kent Overstreet4f024f32013-10-11 15:44:27 -07001612 BUG_ON(bio->bi_iter.bi_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001613 if (!pb->req_nr)
1614 remap_to_origin(cache, bio);
1615 else
1616 remap_to_cache(cache, bio, 0);
1617
Joe Thornber8c081b52014-05-13 16:18:38 +01001618 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05001619 * REQ_PREFLUSH is not directed at any particular block so we don't
1620 * need to inc_ds(). REQ_FUA's are split into a write + REQ_PREFLUSH
Joe Thornber8c081b52014-05-13 16:18:38 +01001621 * by dm-core.
1622 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001623 issue(cache, bio);
1624}
1625
Joe Thornber7ae34e72014-11-06 10:18:04 +00001626static void process_discard_bio(struct cache *cache, struct prealloc *structs,
1627 struct bio *bio)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001628{
Joe Thornber7ae34e72014-11-06 10:18:04 +00001629 int r;
1630 dm_dblock_t b, e;
1631 struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001632
Joe Thornber7ae34e72014-11-06 10:18:04 +00001633 calc_discard_block_range(cache, bio, &b, &e);
1634 if (b == e) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001635 bio_endio(bio);
Joe Thornber7ae34e72014-11-06 10:18:04 +00001636 return;
1637 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001638
Joe Thornber7ae34e72014-11-06 10:18:04 +00001639 cell_prealloc = prealloc_get_cell(structs);
1640 r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc,
1641 (cell_free_fn) prealloc_put_cell,
1642 structs, &new_ocell);
1643 if (r > 0)
1644 return;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001645
Joe Thornber7ae34e72014-11-06 10:18:04 +00001646 discard(cache, structs, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001647}
1648
1649static bool spare_migration_bandwidth(struct cache *cache)
1650{
Joe Thornbera59db672015-01-23 10:16:16 +00001651 sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001652 cache->sectors_per_block;
1653 return current_volume < cache->migration_threshold;
1654}
1655
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001656static void inc_hit_counter(struct cache *cache, struct bio *bio)
1657{
1658 atomic_inc(bio_data_dir(bio) == READ ?
1659 &cache->stats.read_hit : &cache->stats.write_hit);
1660}
1661
1662static void inc_miss_counter(struct cache *cache, struct bio *bio)
1663{
1664 atomic_inc(bio_data_dir(bio) == READ ?
1665 &cache->stats.read_miss : &cache->stats.write_miss);
1666}
1667
Joe Thornberfb4100a2015-05-20 10:30:32 +01001668/*----------------------------------------------------------------*/
1669
Joe Thornber651f5fa2015-05-15 15:26:08 +01001670struct inc_detail {
1671 struct cache *cache;
1672 struct bio_list bios_for_issue;
1673 struct bio_list unhandled_bios;
1674 bool any_writes;
1675};
1676
1677static void inc_fn(void *context, struct dm_bio_prison_cell *cell)
1678{
1679 struct bio *bio;
1680 struct inc_detail *detail = context;
1681 struct cache *cache = detail->cache;
1682
1683 inc_ds(cache, cell->holder, cell);
1684 if (bio_data_dir(cell->holder) == WRITE)
1685 detail->any_writes = true;
1686
1687 while ((bio = bio_list_pop(&cell->bios))) {
1688 if (discard_or_flush(bio)) {
1689 bio_list_add(&detail->unhandled_bios, bio);
1690 continue;
1691 }
1692
1693 if (bio_data_dir(bio) == WRITE)
1694 detail->any_writes = true;
1695
1696 bio_list_add(&detail->bios_for_issue, bio);
1697 inc_ds(cache, bio, cell);
1698 }
1699}
1700
1701// FIXME: refactor these two
1702static void remap_cell_to_origin_clear_discard(struct cache *cache,
1703 struct dm_bio_prison_cell *cell,
1704 dm_oblock_t oblock, bool issue_holder)
1705{
1706 struct bio *bio;
1707 unsigned long flags;
1708 struct inc_detail detail;
1709
1710 detail.cache = cache;
1711 bio_list_init(&detail.bios_for_issue);
1712 bio_list_init(&detail.unhandled_bios);
1713 detail.any_writes = false;
1714
1715 spin_lock_irqsave(&cache->lock, flags);
1716 dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
1717 bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
1718 spin_unlock_irqrestore(&cache->lock, flags);
1719
1720 remap_to_origin(cache, cell->holder);
1721 if (issue_holder)
1722 issue(cache, cell->holder);
1723 else
1724 accounted_begin(cache, cell->holder);
1725
1726 if (detail.any_writes)
1727 clear_discard(cache, oblock_to_dblock(cache, oblock));
1728
1729 while ((bio = bio_list_pop(&detail.bios_for_issue))) {
1730 remap_to_origin(cache, bio);
1731 issue(cache, bio);
1732 }
Joe Thornber9153df72015-08-31 18:20:08 +01001733
1734 free_prison_cell(cache, cell);
Joe Thornber651f5fa2015-05-15 15:26:08 +01001735}
1736
1737static void remap_cell_to_cache_dirty(struct cache *cache, struct dm_bio_prison_cell *cell,
1738 dm_oblock_t oblock, dm_cblock_t cblock, bool issue_holder)
1739{
1740 struct bio *bio;
1741 unsigned long flags;
1742 struct inc_detail detail;
1743
1744 detail.cache = cache;
1745 bio_list_init(&detail.bios_for_issue);
1746 bio_list_init(&detail.unhandled_bios);
1747 detail.any_writes = false;
1748
1749 spin_lock_irqsave(&cache->lock, flags);
1750 dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
1751 bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
1752 spin_unlock_irqrestore(&cache->lock, flags);
1753
1754 remap_to_cache(cache, cell->holder, cblock);
1755 if (issue_holder)
1756 issue(cache, cell->holder);
1757 else
1758 accounted_begin(cache, cell->holder);
1759
1760 if (detail.any_writes) {
1761 set_dirty(cache, oblock, cblock);
1762 clear_discard(cache, oblock_to_dblock(cache, oblock));
1763 }
1764
1765 while ((bio = bio_list_pop(&detail.bios_for_issue))) {
1766 remap_to_cache(cache, bio, cblock);
1767 issue(cache, bio);
1768 }
Joe Thornber9153df72015-08-31 18:20:08 +01001769
1770 free_prison_cell(cache, cell);
Joe Thornber651f5fa2015-05-15 15:26:08 +01001771}
1772
1773/*----------------------------------------------------------------*/
1774
Joe Thornberfb4100a2015-05-20 10:30:32 +01001775struct old_oblock_lock {
1776 struct policy_locker locker;
1777 struct cache *cache;
1778 struct prealloc *structs;
1779 struct dm_bio_prison_cell *cell;
1780};
1781
1782static int null_locker(struct policy_locker *locker, dm_oblock_t b)
1783{
1784 /* This should never be called */
1785 BUG();
1786 return 0;
1787}
1788
1789static int cell_locker(struct policy_locker *locker, dm_oblock_t b)
1790{
1791 struct old_oblock_lock *l = container_of(locker, struct old_oblock_lock, locker);
1792 struct dm_bio_prison_cell *cell_prealloc = prealloc_get_cell(l->structs);
1793
1794 return bio_detain(l->cache, b, NULL, cell_prealloc,
1795 (cell_free_fn) prealloc_put_cell,
1796 l->structs, &l->cell);
1797}
1798
Joe Thornber651f5fa2015-05-15 15:26:08 +01001799static void process_cell(struct cache *cache, struct prealloc *structs,
1800 struct dm_bio_prison_cell *new_ocell)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001801{
1802 int r;
1803 bool release_cell = true;
Joe Thornber651f5fa2015-05-15 15:26:08 +01001804 struct bio *bio = new_ocell->holder;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001805 dm_oblock_t block = get_bio_block(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001806 struct policy_result lookup_result;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001807 bool passthrough = passthrough_mode(&cache->features);
Joe Thornber40775252015-05-15 15:29:58 +01001808 bool fast_promotion, can_migrate;
Joe Thornberfb4100a2015-05-20 10:30:32 +01001809 struct old_oblock_lock ool;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001810
Joe Thornber40775252015-05-15 15:29:58 +01001811 fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
1812 can_migrate = !passthrough && (fast_promotion || spare_migration_bandwidth(cache));
Joe Thornber43c32bf2014-11-25 13:14:57 +00001813
Joe Thornberfb4100a2015-05-20 10:30:32 +01001814 ool.locker.fn = cell_locker;
1815 ool.cache = cache;
1816 ool.structs = structs;
1817 ool.cell = NULL;
Joe Thornber40775252015-05-15 15:29:58 +01001818 r = policy_map(cache->policy, block, true, can_migrate, fast_promotion,
Joe Thornberfb4100a2015-05-20 10:30:32 +01001819 bio, &ool.locker, &lookup_result);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001820
1821 if (r == -EWOULDBLOCK)
1822 /* migration has been denied */
1823 lookup_result.op = POLICY_MISS;
1824
1825 switch (lookup_result.op) {
1826 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04001827 if (passthrough) {
1828 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001829
Joe Thornber2ee57d52013-10-24 14:10:29 -04001830 /*
1831 * Passthrough always maps to the origin,
1832 * invalidating any cache blocks that are written
1833 * to.
1834 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001835
Joe Thornber2ee57d52013-10-24 14:10:29 -04001836 if (bio_data_dir(bio) == WRITE) {
1837 atomic_inc(&cache->stats.demotion);
1838 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1839 release_cell = false;
1840
1841 } else {
1842 /* FIXME: factor out issue_origin() */
Joe Thornber2ee57d52013-10-24 14:10:29 -04001843 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber8c081b52014-05-13 16:18:38 +01001844 inc_and_issue(cache, bio, new_ocell);
Joe Thornber2ee57d52013-10-24 14:10:29 -04001845 }
1846 } else {
1847 inc_hit_counter(cache, bio);
1848
1849 if (bio_data_dir(bio) == WRITE &&
1850 writethrough_mode(&cache->features) &&
1851 !is_dirty(cache, lookup_result.cblock)) {
Joe Thornber2ee57d52013-10-24 14:10:29 -04001852 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
Joe Thornber8c081b52014-05-13 16:18:38 +01001853 inc_and_issue(cache, bio, new_ocell);
1854
Joe Thornber651f5fa2015-05-15 15:26:08 +01001855 } else {
1856 remap_cell_to_cache_dirty(cache, new_ocell, block, lookup_result.cblock, true);
1857 release_cell = false;
Joe Thornber8c081b52014-05-13 16:18:38 +01001858 }
Joe Thornber2ee57d52013-10-24 14:10:29 -04001859 }
1860
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001861 break;
1862
1863 case POLICY_MISS:
1864 inc_miss_counter(cache, bio);
Joe Thornber651f5fa2015-05-15 15:26:08 +01001865 remap_cell_to_origin_clear_discard(cache, new_ocell, block, true);
1866 release_cell = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001867 break;
1868
1869 case POLICY_NEW:
1870 atomic_inc(&cache->stats.promotion);
1871 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1872 release_cell = false;
1873 break;
1874
1875 case POLICY_REPLACE:
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001876 atomic_inc(&cache->stats.demotion);
1877 atomic_inc(&cache->stats.promotion);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001878 demote_then_promote(cache, structs, lookup_result.old_oblock,
1879 block, lookup_result.cblock,
Joe Thornberfb4100a2015-05-20 10:30:32 +01001880 ool.cell, new_ocell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001881 release_cell = false;
1882 break;
1883
1884 default:
Mike Snitzerb61d9502015-04-22 17:25:56 -04001885 DMERR_LIMIT("%s: %s: erroring bio, unknown policy op: %u",
1886 cache_device_name(cache), __func__,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001887 (unsigned) lookup_result.op);
1888 bio_io_error(bio);
1889 }
1890
1891 if (release_cell)
1892 cell_defer(cache, new_ocell, false);
1893}
1894
Joe Thornber651f5fa2015-05-15 15:26:08 +01001895static void process_bio(struct cache *cache, struct prealloc *structs,
1896 struct bio *bio)
1897{
1898 int r;
1899 dm_oblock_t block = get_bio_block(cache, bio);
1900 struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
1901
1902 /*
1903 * Check to see if that block is currently migrating.
1904 */
1905 cell_prealloc = prealloc_get_cell(structs);
1906 r = bio_detain(cache, block, bio, cell_prealloc,
1907 (cell_free_fn) prealloc_put_cell,
1908 structs, &new_ocell);
1909 if (r > 0)
1910 return;
1911
1912 process_cell(cache, structs, new_ocell);
1913}
1914
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001915static int need_commit_due_to_time(struct cache *cache)
1916{
Joe Thornber651f5fa2015-05-15 15:26:08 +01001917 return jiffies < cache->last_commit_jiffies ||
1918 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001919}
1920
Joe Thornber028ae9f2015-04-22 16:42:35 -04001921/*
1922 * A non-zero return indicates read_only or fail_io mode.
1923 */
1924static int commit(struct cache *cache, bool clean_shutdown)
1925{
1926 int r;
1927
1928 if (get_cache_mode(cache) >= CM_READ_ONLY)
1929 return -EINVAL;
1930
1931 atomic_inc(&cache->stats.commit_count);
1932 r = dm_cache_commit(cache->cmd, clean_shutdown);
1933 if (r)
1934 metadata_operation_failed(cache, "dm_cache_commit", r);
1935
1936 return r;
1937}
1938
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001939static int commit_if_needed(struct cache *cache)
1940{
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001941 int r = 0;
1942
1943 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1944 dm_cache_changed_this_transaction(cache->cmd)) {
Joe Thornber028ae9f2015-04-22 16:42:35 -04001945 r = commit(cache, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001946 cache->commit_requested = false;
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001947 cache->last_commit_jiffies = jiffies;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001948 }
1949
Heinz Mauelshagenffcbcb62013-10-14 17:24:43 +02001950 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001951}
1952
1953static void process_deferred_bios(struct cache *cache)
1954{
Mike Snitzer665022d2015-07-16 21:48:55 -04001955 bool prealloc_used = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001956 unsigned long flags;
1957 struct bio_list bios;
1958 struct bio *bio;
1959 struct prealloc structs;
1960
1961 memset(&structs, 0, sizeof(structs));
1962 bio_list_init(&bios);
1963
1964 spin_lock_irqsave(&cache->lock, flags);
1965 bio_list_merge(&bios, &cache->deferred_bios);
1966 bio_list_init(&cache->deferred_bios);
1967 spin_unlock_irqrestore(&cache->lock, flags);
1968
1969 while (!bio_list_empty(&bios)) {
1970 /*
1971 * If we've got no free migration structs, and processing
1972 * this bio might require one, we pause until there are some
1973 * prepared mappings to process.
1974 */
Mike Snitzer795e6332015-07-29 13:48:23 -04001975 prealloc_used = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001976 if (prealloc_data_structs(cache, &structs)) {
1977 spin_lock_irqsave(&cache->lock, flags);
1978 bio_list_merge(&cache->deferred_bios, &bios);
1979 spin_unlock_irqrestore(&cache->lock, flags);
1980 break;
1981 }
1982
1983 bio = bio_list_pop(&bios);
1984
Jens Axboe1eff9d32016-08-05 15:35:16 -06001985 if (bio->bi_opf & REQ_PREFLUSH)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001986 process_flush_bio(cache, bio);
Mike Christiee6047142016-06-05 14:32:04 -05001987 else if (bio_op(bio) == REQ_OP_DISCARD)
Joe Thornber7ae34e72014-11-06 10:18:04 +00001988 process_discard_bio(cache, &structs, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001989 else
1990 process_bio(cache, &structs, bio);
1991 }
1992
Mike Snitzer665022d2015-07-16 21:48:55 -04001993 if (prealloc_used)
1994 prealloc_free_structs(cache, &structs);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001995}
1996
Joe Thornber651f5fa2015-05-15 15:26:08 +01001997static void process_deferred_cells(struct cache *cache)
1998{
Mike Snitzer665022d2015-07-16 21:48:55 -04001999 bool prealloc_used = false;
Joe Thornber651f5fa2015-05-15 15:26:08 +01002000 unsigned long flags;
2001 struct dm_bio_prison_cell *cell, *tmp;
2002 struct list_head cells;
2003 struct prealloc structs;
2004
2005 memset(&structs, 0, sizeof(structs));
2006
2007 INIT_LIST_HEAD(&cells);
2008
2009 spin_lock_irqsave(&cache->lock, flags);
2010 list_splice_init(&cache->deferred_cells, &cells);
2011 spin_unlock_irqrestore(&cache->lock, flags);
2012
2013 list_for_each_entry_safe(cell, tmp, &cells, user_list) {
2014 /*
2015 * If we've got no free migration structs, and processing
2016 * this bio might require one, we pause until there are some
2017 * prepared mappings to process.
2018 */
Mike Snitzer795e6332015-07-29 13:48:23 -04002019 prealloc_used = true;
Joe Thornber651f5fa2015-05-15 15:26:08 +01002020 if (prealloc_data_structs(cache, &structs)) {
2021 spin_lock_irqsave(&cache->lock, flags);
2022 list_splice(&cells, &cache->deferred_cells);
2023 spin_unlock_irqrestore(&cache->lock, flags);
2024 break;
2025 }
2026
2027 process_cell(cache, &structs, cell);
2028 }
2029
Mike Snitzer665022d2015-07-16 21:48:55 -04002030 if (prealloc_used)
2031 prealloc_free_structs(cache, &structs);
Joe Thornber651f5fa2015-05-15 15:26:08 +01002032}
2033
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002034static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
2035{
2036 unsigned long flags;
2037 struct bio_list bios;
2038 struct bio *bio;
2039
2040 bio_list_init(&bios);
2041
2042 spin_lock_irqsave(&cache->lock, flags);
2043 bio_list_merge(&bios, &cache->deferred_flush_bios);
2044 bio_list_init(&cache->deferred_flush_bios);
2045 spin_unlock_irqrestore(&cache->lock, flags);
2046
Joe Thornber8c081b52014-05-13 16:18:38 +01002047 /*
2048 * These bios have already been through inc_ds()
2049 */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002050 while ((bio = bio_list_pop(&bios)))
Joe Thornber066dbaa32015-05-15 15:18:01 +01002051 submit_bios ? accounted_request(cache, bio) : bio_io_error(bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002052}
2053
Joe Thornbere2e74d62013-03-20 17:21:27 +00002054static void process_deferred_writethrough_bios(struct cache *cache)
2055{
2056 unsigned long flags;
2057 struct bio_list bios;
2058 struct bio *bio;
2059
2060 bio_list_init(&bios);
2061
2062 spin_lock_irqsave(&cache->lock, flags);
2063 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
2064 bio_list_init(&cache->deferred_writethrough_bios);
2065 spin_unlock_irqrestore(&cache->lock, flags);
2066
Joe Thornber8c081b52014-05-13 16:18:38 +01002067 /*
2068 * These bios have already been through inc_ds()
2069 */
Joe Thornbere2e74d62013-03-20 17:21:27 +00002070 while ((bio = bio_list_pop(&bios)))
Joe Thornber066dbaa32015-05-15 15:18:01 +01002071 accounted_request(cache, bio);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002072}
2073
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002074static void writeback_some_dirty_blocks(struct cache *cache)
2075{
Mike Snitzer665022d2015-07-16 21:48:55 -04002076 bool prealloc_used = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002077 dm_oblock_t oblock;
2078 dm_cblock_t cblock;
2079 struct prealloc structs;
2080 struct dm_bio_prison_cell *old_ocell;
Joe Thornber20f68142015-05-15 15:20:09 +01002081 bool busy = !iot_idle_for(&cache->origin_tracker, HZ);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002082
2083 memset(&structs, 0, sizeof(structs));
2084
2085 while (spare_migration_bandwidth(cache)) {
Mike Snitzere782eff2015-07-16 21:26:10 -04002086 if (policy_writeback_work(cache->policy, &oblock, &cblock, busy))
2087 break; /* no work to do */
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002088
Mike Snitzer795e6332015-07-29 13:48:23 -04002089 prealloc_used = true;
Mike Snitzere782eff2015-07-16 21:26:10 -04002090 if (prealloc_data_structs(cache, &structs) ||
2091 get_cell(cache, oblock, &structs, &old_ocell)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002092 policy_set_dirty(cache->policy, oblock);
2093 break;
2094 }
2095
2096 writeback(cache, &structs, oblock, cblock, old_ocell);
2097 }
2098
Mike Snitzer665022d2015-07-16 21:48:55 -04002099 if (prealloc_used)
2100 prealloc_free_structs(cache, &structs);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002101}
2102
2103/*----------------------------------------------------------------
Joe Thornber65790ff2013-11-08 16:39:50 +00002104 * Invalidations.
2105 * Dropping something from the cache *without* writing back.
2106 *--------------------------------------------------------------*/
2107
2108static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
2109{
2110 int r = 0;
2111 uint64_t begin = from_cblock(req->cblocks->begin);
2112 uint64_t end = from_cblock(req->cblocks->end);
2113
2114 while (begin != end) {
2115 r = policy_remove_cblock(cache->policy, to_cblock(begin));
2116 if (!r) {
2117 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
Joe Thornber028ae9f2015-04-22 16:42:35 -04002118 if (r) {
2119 metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
Joe Thornber65790ff2013-11-08 16:39:50 +00002120 break;
Joe Thornber028ae9f2015-04-22 16:42:35 -04002121 }
Joe Thornber65790ff2013-11-08 16:39:50 +00002122
2123 } else if (r == -ENODATA) {
2124 /* harmless, already unmapped */
2125 r = 0;
2126
2127 } else {
Mike Snitzerb61d9502015-04-22 17:25:56 -04002128 DMERR("%s: policy_remove_cblock failed", cache_device_name(cache));
Joe Thornber65790ff2013-11-08 16:39:50 +00002129 break;
2130 }
2131
2132 begin++;
2133 }
2134
2135 cache->commit_requested = true;
2136
2137 req->err = r;
2138 atomic_set(&req->complete, 1);
2139
2140 wake_up(&req->result_wait);
2141}
2142
2143static void process_invalidation_requests(struct cache *cache)
2144{
2145 struct list_head list;
2146 struct invalidation_request *req, *tmp;
2147
2148 INIT_LIST_HEAD(&list);
2149 spin_lock(&cache->invalidation_lock);
2150 list_splice_init(&cache->invalidation_requests, &list);
2151 spin_unlock(&cache->invalidation_lock);
2152
2153 list_for_each_entry_safe (req, tmp, &list, list)
2154 process_invalidation_request(cache, req);
2155}
2156
2157/*----------------------------------------------------------------
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002158 * Main worker loop
2159 *--------------------------------------------------------------*/
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002160static bool is_quiescing(struct cache *cache)
2161{
Joe Thornber238f8362013-10-30 17:29:30 +00002162 return atomic_read(&cache->quiescing);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002163}
2164
Joe Thornber66cb1912013-10-30 17:11:58 +00002165static void ack_quiescing(struct cache *cache)
2166{
2167 if (is_quiescing(cache)) {
2168 atomic_inc(&cache->quiescing_ack);
2169 wake_up(&cache->quiescing_wait);
2170 }
2171}
2172
2173static void wait_for_quiescing_ack(struct cache *cache)
2174{
2175 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
2176}
2177
2178static void start_quiescing(struct cache *cache)
2179{
Joe Thornber238f8362013-10-30 17:29:30 +00002180 atomic_inc(&cache->quiescing);
Joe Thornber66cb1912013-10-30 17:11:58 +00002181 wait_for_quiescing_ack(cache);
2182}
2183
2184static void stop_quiescing(struct cache *cache)
2185{
Joe Thornber238f8362013-10-30 17:29:30 +00002186 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002187 atomic_set(&cache->quiescing_ack, 0);
2188}
2189
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002190static void wait_for_migrations(struct cache *cache)
2191{
Joe Thornbera59db672015-01-23 10:16:16 +00002192 wait_event(cache->migration_wait, !atomic_read(&cache->nr_allocated_migrations));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002193}
2194
2195static void stop_worker(struct cache *cache)
2196{
2197 cancel_delayed_work(&cache->waker);
2198 flush_workqueue(cache->wq);
2199}
2200
Joe Thornber651f5fa2015-05-15 15:26:08 +01002201static void requeue_deferred_cells(struct cache *cache)
2202{
2203 unsigned long flags;
2204 struct list_head cells;
2205 struct dm_bio_prison_cell *cell, *tmp;
2206
2207 INIT_LIST_HEAD(&cells);
2208 spin_lock_irqsave(&cache->lock, flags);
2209 list_splice_init(&cache->deferred_cells, &cells);
2210 spin_unlock_irqrestore(&cache->lock, flags);
2211
2212 list_for_each_entry_safe(cell, tmp, &cells, user_list)
2213 cell_requeue(cache, cell);
2214}
2215
2216static void requeue_deferred_bios(struct cache *cache)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002217{
2218 struct bio *bio;
2219 struct bio_list bios;
2220
2221 bio_list_init(&bios);
2222 bio_list_merge(&bios, &cache->deferred_bios);
2223 bio_list_init(&cache->deferred_bios);
2224
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002225 while ((bio = bio_list_pop(&bios))) {
2226 bio->bi_error = DM_ENDIO_REQUEUE;
2227 bio_endio(bio);
2228 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002229}
2230
2231static int more_work(struct cache *cache)
2232{
2233 if (is_quiescing(cache))
2234 return !list_empty(&cache->quiesced_migrations) ||
2235 !list_empty(&cache->completed_migrations) ||
2236 !list_empty(&cache->need_commit_migrations);
2237 else
2238 return !bio_list_empty(&cache->deferred_bios) ||
Joe Thornber651f5fa2015-05-15 15:26:08 +01002239 !list_empty(&cache->deferred_cells) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002240 !bio_list_empty(&cache->deferred_flush_bios) ||
Joe Thornbere2e74d62013-03-20 17:21:27 +00002241 !bio_list_empty(&cache->deferred_writethrough_bios) ||
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002242 !list_empty(&cache->quiesced_migrations) ||
2243 !list_empty(&cache->completed_migrations) ||
Joe Thornber65790ff2013-11-08 16:39:50 +00002244 !list_empty(&cache->need_commit_migrations) ||
2245 cache->invalidate;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002246}
2247
2248static void do_worker(struct work_struct *ws)
2249{
2250 struct cache *cache = container_of(ws, struct cache, worker);
2251
2252 do {
Joe Thornber66cb1912013-10-30 17:11:58 +00002253 if (!is_quiescing(cache)) {
2254 writeback_some_dirty_blocks(cache);
2255 process_deferred_writethrough_bios(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002256 process_deferred_bios(cache);
Joe Thornber651f5fa2015-05-15 15:26:08 +01002257 process_deferred_cells(cache);
Joe Thornber65790ff2013-11-08 16:39:50 +00002258 process_invalidation_requests(cache);
Joe Thornber66cb1912013-10-30 17:11:58 +00002259 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002260
Joe Thornber7ae34e72014-11-06 10:18:04 +00002261 process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002262 process_migrations(cache, &cache->completed_migrations, complete_migration);
2263
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002264 if (commit_if_needed(cache)) {
2265 process_deferred_flush_bios(cache, false);
Joe Thornber304affa2014-06-24 15:36:58 -04002266 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002267 } else {
2268 process_deferred_flush_bios(cache, true);
2269 process_migrations(cache, &cache->need_commit_migrations,
2270 migration_success_post_commit);
2271 }
Joe Thornber66cb1912013-10-30 17:11:58 +00002272
2273 ack_quiescing(cache);
2274
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002275 } while (more_work(cache));
2276}
2277
2278/*
2279 * We want to commit periodically so that not too much
2280 * unwritten metadata builds up.
2281 */
2282static void do_waker(struct work_struct *ws)
2283{
2284 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
Joe Thornberfba10102015-05-29 10:20:56 +01002285 policy_tick(cache->policy, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002286 wake_worker(cache);
2287 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
2288}
2289
2290/*----------------------------------------------------------------*/
2291
2292static int is_congested(struct dm_dev *dev, int bdi_bits)
2293{
2294 struct request_queue *q = bdev_get_queue(dev->bdev);
2295 return bdi_congested(&q->backing_dev_info, bdi_bits);
2296}
2297
2298static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2299{
2300 struct cache *cache = container_of(cb, struct cache, callbacks);
2301
2302 return is_congested(cache->origin_dev, bdi_bits) ||
2303 is_congested(cache->cache_dev, bdi_bits);
2304}
2305
2306/*----------------------------------------------------------------
2307 * Target methods
2308 *--------------------------------------------------------------*/
2309
2310/*
2311 * This function gets called on the error paths of the constructor, so we
2312 * have to cope with a partially initialised struct.
2313 */
2314static void destroy(struct cache *cache)
2315{
2316 unsigned i;
2317
Julia Lawall6f659852015-09-13 14:15:05 +02002318 mempool_destroy(cache->migration_pool);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002319
2320 if (cache->all_io_ds)
2321 dm_deferred_set_destroy(cache->all_io_ds);
2322
2323 if (cache->prison)
2324 dm_bio_prison_destroy(cache->prison);
2325
2326 if (cache->wq)
2327 destroy_workqueue(cache->wq);
2328
2329 if (cache->dirty_bitset)
2330 free_bitset(cache->dirty_bitset);
2331
2332 if (cache->discard_bitset)
2333 free_bitset(cache->discard_bitset);
2334
2335 if (cache->copier)
2336 dm_kcopyd_client_destroy(cache->copier);
2337
2338 if (cache->cmd)
2339 dm_cache_metadata_close(cache->cmd);
2340
2341 if (cache->metadata_dev)
2342 dm_put_device(cache->ti, cache->metadata_dev);
2343
2344 if (cache->origin_dev)
2345 dm_put_device(cache->ti, cache->origin_dev);
2346
2347 if (cache->cache_dev)
2348 dm_put_device(cache->ti, cache->cache_dev);
2349
2350 if (cache->policy)
2351 dm_cache_policy_destroy(cache->policy);
2352
2353 for (i = 0; i < cache->nr_ctr_args ; i++)
2354 kfree(cache->ctr_args[i]);
2355 kfree(cache->ctr_args);
2356
2357 kfree(cache);
2358}
2359
2360static void cache_dtr(struct dm_target *ti)
2361{
2362 struct cache *cache = ti->private;
2363
2364 destroy(cache);
2365}
2366
2367static sector_t get_dev_size(struct dm_dev *dev)
2368{
2369 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
2370}
2371
2372/*----------------------------------------------------------------*/
2373
2374/*
2375 * Construct a cache device mapping.
2376 *
2377 * cache <metadata dev> <cache dev> <origin dev> <block size>
2378 * <#feature args> [<feature arg>]*
2379 * <policy> <#policy args> [<policy arg>]*
2380 *
2381 * metadata dev : fast device holding the persistent metadata
2382 * cache dev : fast device holding cached data blocks
2383 * origin dev : slow device holding original data blocks
2384 * block size : cache unit size in sectors
2385 *
2386 * #feature args : number of feature arguments passed
2387 * feature args : writethrough. (The default is writeback.)
2388 *
2389 * policy : the replacement policy to use
2390 * #policy args : an even number of policy arguments corresponding
2391 * to key/value pairs passed to the policy
2392 * policy args : key/value pairs passed to the policy
2393 * E.g. 'sequential_threshold 1024'
2394 * See cache-policies.txt for details.
2395 *
2396 * Optional feature arguments are:
2397 * writethrough : write through caching that prohibits cache block
2398 * content from being different from origin block content.
2399 * Without this argument, the default behaviour is to write
2400 * back cache block contents later for performance reasons,
2401 * so they may differ from the corresponding origin blocks.
2402 */
2403struct cache_args {
2404 struct dm_target *ti;
2405
2406 struct dm_dev *metadata_dev;
2407
2408 struct dm_dev *cache_dev;
2409 sector_t cache_sectors;
2410
2411 struct dm_dev *origin_dev;
2412 sector_t origin_sectors;
2413
2414 uint32_t block_size;
2415
2416 const char *policy_name;
2417 int policy_argc;
2418 const char **policy_argv;
2419
2420 struct cache_features features;
2421};
2422
2423static void destroy_cache_args(struct cache_args *ca)
2424{
2425 if (ca->metadata_dev)
2426 dm_put_device(ca->ti, ca->metadata_dev);
2427
2428 if (ca->cache_dev)
2429 dm_put_device(ca->ti, ca->cache_dev);
2430
2431 if (ca->origin_dev)
2432 dm_put_device(ca->ti, ca->origin_dev);
2433
2434 kfree(ca);
2435}
2436
2437static bool at_least_one_arg(struct dm_arg_set *as, char **error)
2438{
2439 if (!as->argc) {
2440 *error = "Insufficient args";
2441 return false;
2442 }
2443
2444 return true;
2445}
2446
2447static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
2448 char **error)
2449{
2450 int r;
2451 sector_t metadata_dev_size;
2452 char b[BDEVNAME_SIZE];
2453
2454 if (!at_least_one_arg(as, error))
2455 return -EINVAL;
2456
2457 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2458 &ca->metadata_dev);
2459 if (r) {
2460 *error = "Error opening metadata device";
2461 return r;
2462 }
2463
2464 metadata_dev_size = get_dev_size(ca->metadata_dev);
2465 if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
2466 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2467 bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
2468
2469 return 0;
2470}
2471
2472static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
2473 char **error)
2474{
2475 int r;
2476
2477 if (!at_least_one_arg(as, error))
2478 return -EINVAL;
2479
2480 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2481 &ca->cache_dev);
2482 if (r) {
2483 *error = "Error opening cache device";
2484 return r;
2485 }
2486 ca->cache_sectors = get_dev_size(ca->cache_dev);
2487
2488 return 0;
2489}
2490
2491static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
2492 char **error)
2493{
2494 int r;
2495
2496 if (!at_least_one_arg(as, error))
2497 return -EINVAL;
2498
2499 r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
2500 &ca->origin_dev);
2501 if (r) {
2502 *error = "Error opening origin device";
2503 return r;
2504 }
2505
2506 ca->origin_sectors = get_dev_size(ca->origin_dev);
2507 if (ca->ti->len > ca->origin_sectors) {
2508 *error = "Device size larger than cached device";
2509 return -EINVAL;
2510 }
2511
2512 return 0;
2513}
2514
2515static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
2516 char **error)
2517{
Mike Snitzer05473042013-08-16 10:54:19 -04002518 unsigned long block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002519
2520 if (!at_least_one_arg(as, error))
2521 return -EINVAL;
2522
Mike Snitzer05473042013-08-16 10:54:19 -04002523 if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
2524 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2525 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
2526 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002527 *error = "Invalid data block size";
2528 return -EINVAL;
2529 }
2530
Mike Snitzer05473042013-08-16 10:54:19 -04002531 if (block_size > ca->cache_sectors) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002532 *error = "Data block size is larger than the cache device";
2533 return -EINVAL;
2534 }
2535
Mike Snitzer05473042013-08-16 10:54:19 -04002536 ca->block_size = block_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002537
2538 return 0;
2539}
2540
2541static void init_features(struct cache_features *cf)
2542{
2543 cf->mode = CM_WRITE;
Joe Thornber2ee57d52013-10-24 14:10:29 -04002544 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornber629d0a82016-09-22 06:15:21 -04002545 cf->metadata_version = 1;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002546}
2547
2548static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2549 char **error)
2550{
2551 static struct dm_arg _args[] = {
Joe Thornber629d0a82016-09-22 06:15:21 -04002552 {0, 2, "Invalid number of cache feature arguments"},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002553 };
2554
2555 int r;
2556 unsigned argc;
2557 const char *arg;
2558 struct cache_features *cf = &ca->features;
2559
2560 init_features(cf);
2561
2562 r = dm_read_arg_group(_args, as, &argc, error);
2563 if (r)
2564 return -EINVAL;
2565
2566 while (argc--) {
2567 arg = dm_shift_arg(as);
2568
2569 if (!strcasecmp(arg, "writeback"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002570 cf->io_mode = CM_IO_WRITEBACK;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002571
2572 else if (!strcasecmp(arg, "writethrough"))
Joe Thornber2ee57d52013-10-24 14:10:29 -04002573 cf->io_mode = CM_IO_WRITETHROUGH;
2574
2575 else if (!strcasecmp(arg, "passthrough"))
2576 cf->io_mode = CM_IO_PASSTHROUGH;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002577
Joe Thornber629d0a82016-09-22 06:15:21 -04002578 else if (!strcasecmp(arg, "metadata2"))
2579 cf->metadata_version = 2;
2580
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002581 else {
2582 *error = "Unrecognised cache feature requested";
2583 return -EINVAL;
2584 }
2585 }
2586
2587 return 0;
2588}
2589
2590static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
2591 char **error)
2592{
2593 static struct dm_arg _args[] = {
2594 {0, 1024, "Invalid number of policy arguments"},
2595 };
2596
2597 int r;
2598
2599 if (!at_least_one_arg(as, error))
2600 return -EINVAL;
2601
2602 ca->policy_name = dm_shift_arg(as);
2603
2604 r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
2605 if (r)
2606 return -EINVAL;
2607
2608 ca->policy_argv = (const char **)as->argv;
2609 dm_consume_args(as, ca->policy_argc);
2610
2611 return 0;
2612}
2613
2614static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
2615 char **error)
2616{
2617 int r;
2618 struct dm_arg_set as;
2619
2620 as.argc = argc;
2621 as.argv = argv;
2622
2623 r = parse_metadata_dev(ca, &as, error);
2624 if (r)
2625 return r;
2626
2627 r = parse_cache_dev(ca, &as, error);
2628 if (r)
2629 return r;
2630
2631 r = parse_origin_dev(ca, &as, error);
2632 if (r)
2633 return r;
2634
2635 r = parse_block_size(ca, &as, error);
2636 if (r)
2637 return r;
2638
2639 r = parse_features(ca, &as, error);
2640 if (r)
2641 return r;
2642
2643 r = parse_policy(ca, &as, error);
2644 if (r)
2645 return r;
2646
2647 return 0;
2648}
2649
2650/*----------------------------------------------------------------*/
2651
2652static struct kmem_cache *migration_cache;
2653
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002654#define NOT_CORE_OPTION 1
2655
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002656static int process_config_option(struct cache *cache, const char *key, const char *value)
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002657{
2658 unsigned long tmp;
2659
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002660 if (!strcasecmp(key, "migration_threshold")) {
2661 if (kstrtoul(value, 10, &tmp))
Alasdair G Kergon2c73c472013-05-10 14:37:21 +01002662 return -EINVAL;
2663
2664 cache->migration_threshold = tmp;
2665 return 0;
2666 }
2667
2668 return NOT_CORE_OPTION;
2669}
2670
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002671static int set_config_value(struct cache *cache, const char *key, const char *value)
2672{
2673 int r = process_config_option(cache, key, value);
2674
2675 if (r == NOT_CORE_OPTION)
2676 r = policy_set_config_value(cache->policy, key, value);
2677
2678 if (r)
2679 DMWARN("bad config value for %s: %s", key, value);
2680
2681 return r;
2682}
2683
2684static int set_config_values(struct cache *cache, int argc, const char **argv)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002685{
2686 int r = 0;
2687
2688 if (argc & 1) {
2689 DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
2690 return -EINVAL;
2691 }
2692
2693 while (argc) {
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002694 r = set_config_value(cache, argv[0], argv[1]);
2695 if (r)
2696 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002697
2698 argc -= 2;
2699 argv += 2;
2700 }
2701
2702 return r;
2703}
2704
2705static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2706 char **error)
2707{
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002708 struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
2709 cache->cache_size,
2710 cache->origin_sectors,
2711 cache->sectors_per_block);
2712 if (IS_ERR(p)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002713 *error = "Error creating cache's policy";
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002714 return PTR_ERR(p);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002715 }
Mikulas Patocka4cb3e1d2013-10-01 18:35:39 -04002716 cache->policy = p;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002717
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002718 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002719}
2720
Joe Thornber08b18452014-11-06 14:38:01 +00002721/*
Joe Thornber2bb812d2014-11-26 16:07:50 +00002722 * We want the discard block size to be at least the size of the cache
2723 * block size and have no more than 2^14 discard blocks across the origin.
Joe Thornber08b18452014-11-06 14:38:01 +00002724 */
2725#define MAX_DISCARD_BLOCKS (1 << 14)
2726
2727static bool too_many_discard_blocks(sector_t discard_block_size,
2728 sector_t origin_size)
2729{
2730 (void) sector_div(origin_size, discard_block_size);
2731
2732 return origin_size > MAX_DISCARD_BLOCKS;
2733}
2734
2735static sector_t calculate_discard_block_size(sector_t cache_block_size,
2736 sector_t origin_size)
2737{
Joe Thornber2bb812d2014-11-26 16:07:50 +00002738 sector_t discard_block_size = cache_block_size;
Joe Thornber08b18452014-11-06 14:38:01 +00002739
2740 if (origin_size)
2741 while (too_many_discard_blocks(discard_block_size, origin_size))
2742 discard_block_size *= 2;
2743
2744 return discard_block_size;
2745}
2746
Joe Thornberd1d92202014-11-11 11:58:32 +00002747static void set_cache_size(struct cache *cache, dm_cblock_t size)
2748{
2749 dm_block_t nr_blocks = from_cblock(size);
2750
2751 if (nr_blocks > (1 << 20) && cache->cache_size != size)
2752 DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
2753 "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
2754 "Please consider increasing the cache block size to reduce the overall cache block count.",
2755 (unsigned long long) nr_blocks);
2756
2757 cache->cache_size = size;
2758}
2759
Joe Thornberf8350da2013-05-10 14:37:16 +01002760#define DEFAULT_MIGRATION_THRESHOLD 2048
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002761
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002762static int cache_create(struct cache_args *ca, struct cache **result)
2763{
2764 int r = 0;
2765 char **error = &ca->ti->error;
2766 struct cache *cache;
2767 struct dm_target *ti = ca->ti;
2768 dm_block_t origin_blocks;
2769 struct dm_cache_metadata *cmd;
2770 bool may_format = ca->features.mode == CM_WRITE;
2771
2772 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2773 if (!cache)
2774 return -ENOMEM;
2775
2776 cache->ti = ca->ti;
2777 ti->private = cache;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002778 ti->num_flush_bios = 2;
2779 ti->flush_supported = true;
2780
2781 ti->num_discard_bios = 1;
2782 ti->discards_supported = true;
2783 ti->discard_zeroes_data_unsupported = true;
Joe Thornber25726292014-11-24 14:05:16 +00002784 ti->split_discard_bios = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002785
Joe Thornber8c5008f2013-05-10 14:37:18 +01002786 cache->features = ca->features;
Mike Snitzer30187e12016-01-31 13:28:26 -05002787 ti->per_io_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002788
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002789 cache->callbacks.congested_fn = cache_is_congested;
2790 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2791
2792 cache->metadata_dev = ca->metadata_dev;
2793 cache->origin_dev = ca->origin_dev;
2794 cache->cache_dev = ca->cache_dev;
2795
2796 ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
2797
2798 /* FIXME: factor out this whole section */
2799 origin_blocks = cache->origin_sectors = ca->origin_sectors;
Joe Thornber414dd672013-03-20 17:21:25 +00002800 origin_blocks = block_div(origin_blocks, ca->block_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002801 cache->origin_blocks = to_oblock(origin_blocks);
2802
2803 cache->sectors_per_block = ca->block_size;
2804 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2805 r = -EINVAL;
2806 goto bad;
2807 }
2808
2809 if (ca->block_size & (ca->block_size - 1)) {
2810 dm_block_t cache_size = ca->cache_sectors;
2811
2812 cache->sectors_per_block_shift = -1;
Joe Thornber414dd672013-03-20 17:21:25 +00002813 cache_size = block_div(cache_size, ca->block_size);
Joe Thornberd1d92202014-11-11 11:58:32 +00002814 set_cache_size(cache, to_cblock(cache_size));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002815 } else {
2816 cache->sectors_per_block_shift = __ffs(ca->block_size);
Joe Thornberd1d92202014-11-11 11:58:32 +00002817 set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002818 }
2819
2820 r = create_cache_policy(cache, ca, error);
2821 if (r)
2822 goto bad;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002823
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002824 cache->policy_nr_args = ca->policy_argc;
Joe Thornber2f14f4b2013-05-10 14:37:21 +01002825 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2826
2827 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2828 if (r) {
2829 *error = "Error setting cache policy's config values";
2830 goto bad;
2831 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002832
2833 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2834 ca->block_size, may_format,
Joe Thornber629d0a82016-09-22 06:15:21 -04002835 dm_cache_policy_get_hint_size(cache->policy),
2836 ca->features.metadata_version);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002837 if (IS_ERR(cmd)) {
2838 *error = "Error creating metadata object";
2839 r = PTR_ERR(cmd);
2840 goto bad;
2841 }
2842 cache->cmd = cmd;
Joe Thornber028ae9f2015-04-22 16:42:35 -04002843 set_cache_mode(cache, CM_WRITE);
2844 if (get_cache_mode(cache) != CM_WRITE) {
2845 *error = "Unable to get write access to metadata, please check/repair metadata.";
2846 r = -EINVAL;
2847 goto bad;
2848 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002849
Joe Thornber2ee57d52013-10-24 14:10:29 -04002850 if (passthrough_mode(&cache->features)) {
2851 bool all_clean;
2852
2853 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2854 if (r) {
2855 *error = "dm_cache_metadata_all_clean() failed";
2856 goto bad;
2857 }
2858
2859 if (!all_clean) {
2860 *error = "Cannot enter passthrough mode unless all blocks are clean";
2861 r = -EINVAL;
2862 goto bad;
2863 }
2864 }
2865
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002866 spin_lock_init(&cache->lock);
Joe Thornber651f5fa2015-05-15 15:26:08 +01002867 INIT_LIST_HEAD(&cache->deferred_cells);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002868 bio_list_init(&cache->deferred_bios);
2869 bio_list_init(&cache->deferred_flush_bios);
Joe Thornbere2e74d62013-03-20 17:21:27 +00002870 bio_list_init(&cache->deferred_writethrough_bios);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002871 INIT_LIST_HEAD(&cache->quiesced_migrations);
2872 INIT_LIST_HEAD(&cache->completed_migrations);
2873 INIT_LIST_HEAD(&cache->need_commit_migrations);
Joe Thornbera59db672015-01-23 10:16:16 +00002874 atomic_set(&cache->nr_allocated_migrations, 0);
2875 atomic_set(&cache->nr_io_migrations, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002876 init_waitqueue_head(&cache->migration_wait);
2877
Joe Thornber66cb1912013-10-30 17:11:58 +00002878 init_waitqueue_head(&cache->quiescing_wait);
Joe Thornber238f8362013-10-30 17:29:30 +00002879 atomic_set(&cache->quiescing, 0);
Joe Thornber66cb1912013-10-30 17:11:58 +00002880 atomic_set(&cache->quiescing_ack, 0);
2881
Wei Yongjunfa4d6832013-05-10 14:37:14 +01002882 r = -ENOMEM;
Anssi Hannula44fa8162014-08-01 11:55:47 -04002883 atomic_set(&cache->nr_dirty, 0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002884 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2885 if (!cache->dirty_bitset) {
2886 *error = "could not allocate dirty bitset";
2887 goto bad;
2888 }
2889 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2890
Joe Thornber08b18452014-11-06 14:38:01 +00002891 cache->discard_block_size =
2892 calculate_discard_block_size(cache->sectors_per_block,
2893 cache->origin_sectors);
Joe Thornber25726292014-11-24 14:05:16 +00002894 cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
2895 cache->discard_block_size));
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002896 cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002897 if (!cache->discard_bitset) {
2898 *error = "could not allocate discard bitset";
2899 goto bad;
2900 }
Joe Thornber1bad9bc2014-11-07 14:47:07 +00002901 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002902
2903 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2904 if (IS_ERR(cache->copier)) {
2905 *error = "could not create kcopyd client";
2906 r = PTR_ERR(cache->copier);
2907 goto bad;
2908 }
2909
2910 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2911 if (!cache->wq) {
2912 *error = "could not create workqueue for metadata object";
2913 goto bad;
2914 }
2915 INIT_WORK(&cache->worker, do_worker);
2916 INIT_DELAYED_WORK(&cache->waker, do_waker);
2917 cache->last_commit_jiffies = jiffies;
2918
Joe Thornbera195db22014-10-06 16:30:06 -04002919 cache->prison = dm_bio_prison_create();
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002920 if (!cache->prison) {
2921 *error = "could not create bio prison";
2922 goto bad;
2923 }
2924
2925 cache->all_io_ds = dm_deferred_set_create();
2926 if (!cache->all_io_ds) {
2927 *error = "could not create all_io deferred set";
2928 goto bad;
2929 }
2930
2931 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2932 migration_cache);
2933 if (!cache->migration_pool) {
2934 *error = "Error creating cache's migration mempool";
2935 goto bad;
2936 }
2937
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002938 cache->need_tick_bio = true;
2939 cache->sized = false;
Joe Thornber65790ff2013-11-08 16:39:50 +00002940 cache->invalidate = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002941 cache->commit_requested = false;
2942 cache->loaded_mappings = false;
2943 cache->loaded_discards = false;
2944
2945 load_stats(cache);
2946
2947 atomic_set(&cache->stats.demotion, 0);
2948 atomic_set(&cache->stats.promotion, 0);
2949 atomic_set(&cache->stats.copies_avoided, 0);
2950 atomic_set(&cache->stats.cache_cell_clash, 0);
2951 atomic_set(&cache->stats.commit_count, 0);
2952 atomic_set(&cache->stats.discard_count, 0);
2953
Joe Thornber65790ff2013-11-08 16:39:50 +00002954 spin_lock_init(&cache->invalidation_lock);
2955 INIT_LIST_HEAD(&cache->invalidation_requests);
2956
Joe Thornber066dbaa32015-05-15 15:18:01 +01002957 iot_init(&cache->origin_tracker);
2958
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00002959 *result = cache;
2960 return 0;
2961
2962bad:
2963 destroy(cache);
2964 return r;
2965}
2966
2967static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2968{
2969 unsigned i;
2970 const char **copy;
2971
2972 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
2973 if (!copy)
2974 return -ENOMEM;
2975 for (i = 0; i < argc; i++) {
2976 copy[i] = kstrdup(argv[i], GFP_KERNEL);
2977 if (!copy[i]) {
2978 while (i--)
2979 kfree(copy[i]);
2980 kfree(copy);
2981 return -ENOMEM;
2982 }
2983 }
2984
2985 cache->nr_ctr_args = argc;
2986 cache->ctr_args = copy;
2987
2988 return 0;
2989}
2990
2991static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2992{
2993 int r = -EINVAL;
2994 struct cache_args *ca;
2995 struct cache *cache = NULL;
2996
2997 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2998 if (!ca) {
2999 ti->error = "Error allocating memory for cache";
3000 return -ENOMEM;
3001 }
3002 ca->ti = ti;
3003
3004 r = parse_cache_args(ca, argc, argv, &ti->error);
3005 if (r)
3006 goto out;
3007
3008 r = cache_create(ca, &cache);
Heinz Mauelshagen617a0b82013-03-20 17:21:26 +00003009 if (r)
3010 goto out;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003011
3012 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
3013 if (r) {
3014 destroy(cache);
3015 goto out;
3016 }
3017
3018 ti->private = cache;
3019
3020out:
3021 destroy_cache_args(ca);
3022 return r;
3023}
3024
Joe Thornber651f5fa2015-05-15 15:26:08 +01003025/*----------------------------------------------------------------*/
3026
3027static int cache_map(struct dm_target *ti, struct bio *bio)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003028{
Joe Thornber651f5fa2015-05-15 15:26:08 +01003029 struct cache *cache = ti->private;
3030
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003031 int r;
Joe Thornber651f5fa2015-05-15 15:26:08 +01003032 struct dm_bio_prison_cell *cell = NULL;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003033 dm_oblock_t block = get_bio_block(cache, bio);
Mike Snitzer19b00922013-04-05 15:36:34 +01003034 size_t pb_data_size = get_per_bio_data_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003035 bool can_migrate = false;
Joe Thornber40775252015-05-15 15:29:58 +01003036 bool fast_promotion;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003037 struct policy_result lookup_result;
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01003038 struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
Joe Thornberfb4100a2015-05-20 10:30:32 +01003039 struct old_oblock_lock ool;
3040
3041 ool.locker.fn = null_locker;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003042
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01003043 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003044 /*
3045 * This can only occur if the io goes to a partial block at
3046 * the end of the origin device. We don't cache these.
3047 * Just remap to the origin and carry on.
3048 */
Heinz Mauelshagene893fba2014-03-12 16:13:39 +01003049 remap_to_origin(cache, bio);
Joe Thornber651f5fa2015-05-15 15:26:08 +01003050 accounted_begin(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003051 return DM_MAPIO_REMAPPED;
3052 }
3053
Joe Thornber651f5fa2015-05-15 15:26:08 +01003054 if (discard_or_flush(bio)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003055 defer_bio(cache, bio);
3056 return DM_MAPIO_SUBMITTED;
3057 }
3058
3059 /*
3060 * Check to see if that block is currently migrating.
3061 */
Joe Thornber651f5fa2015-05-15 15:26:08 +01003062 cell = alloc_prison_cell(cache);
3063 if (!cell) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003064 defer_bio(cache, bio);
3065 return DM_MAPIO_SUBMITTED;
3066 }
3067
Joe Thornber651f5fa2015-05-15 15:26:08 +01003068 r = bio_detain(cache, block, bio, cell,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003069 (cell_free_fn) free_prison_cell,
Joe Thornber651f5fa2015-05-15 15:26:08 +01003070 cache, &cell);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003071 if (r) {
3072 if (r < 0)
3073 defer_bio(cache, bio);
3074
3075 return DM_MAPIO_SUBMITTED;
3076 }
3077
Joe Thornber40775252015-05-15 15:29:58 +01003078 fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003079
Joe Thornber40775252015-05-15 15:29:58 +01003080 r = policy_map(cache->policy, block, false, can_migrate, fast_promotion,
Joe Thornberfb4100a2015-05-20 10:30:32 +01003081 bio, &ool.locker, &lookup_result);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003082 if (r == -EWOULDBLOCK) {
Joe Thornber651f5fa2015-05-15 15:26:08 +01003083 cell_defer(cache, cell, true);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003084 return DM_MAPIO_SUBMITTED;
3085
3086 } else if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003087 DMERR_LIMIT("%s: Unexpected return from cache replacement policy: %d",
3088 cache_device_name(cache), r);
Joe Thornber651f5fa2015-05-15 15:26:08 +01003089 cell_defer(cache, cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003090 bio_io_error(bio);
3091 return DM_MAPIO_SUBMITTED;
3092 }
3093
Joe Thornber2ee57d52013-10-24 14:10:29 -04003094 r = DM_MAPIO_REMAPPED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003095 switch (lookup_result.op) {
3096 case POLICY_HIT:
Joe Thornber2ee57d52013-10-24 14:10:29 -04003097 if (passthrough_mode(&cache->features)) {
3098 if (bio_data_dir(bio) == WRITE) {
3099 /*
3100 * We need to invalidate this block, so
3101 * defer for the worker thread.
3102 */
Joe Thornber651f5fa2015-05-15 15:26:08 +01003103 cell_defer(cache, cell, true);
Joe Thornber2ee57d52013-10-24 14:10:29 -04003104 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003105
Joe Thornber2ee57d52013-10-24 14:10:29 -04003106 } else {
Joe Thornber2ee57d52013-10-24 14:10:29 -04003107 inc_miss_counter(cache, bio);
3108 remap_to_origin_clear_discard(cache, bio, block);
Joe Thornber651f5fa2015-05-15 15:26:08 +01003109 accounted_begin(cache, bio);
3110 inc_ds(cache, bio, cell);
3111 // FIXME: we want to remap hits or misses straight
3112 // away rather than passing over to the worker.
3113 cell_defer(cache, cell, false);
Joe Thornber2ee57d52013-10-24 14:10:29 -04003114 }
3115
3116 } else {
3117 inc_hit_counter(cache, bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04003118 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
Joe Thornber651f5fa2015-05-15 15:26:08 +01003119 !is_dirty(cache, lookup_result.cblock)) {
Joe Thornber2ee57d52013-10-24 14:10:29 -04003120 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
Joe Thornber651f5fa2015-05-15 15:26:08 +01003121 accounted_begin(cache, bio);
3122 inc_ds(cache, bio, cell);
3123 cell_defer(cache, cell, false);
3124
3125 } else
3126 remap_cell_to_cache_dirty(cache, cell, block, lookup_result.cblock, false);
Joe Thornber2ee57d52013-10-24 14:10:29 -04003127 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003128 break;
3129
3130 case POLICY_MISS:
3131 inc_miss_counter(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003132 if (pb->req_nr != 0) {
3133 /*
3134 * This is a duplicate writethrough io that is no
3135 * longer needed because the block has been demoted.
3136 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003137 bio_endio(bio);
Joe Thornber651f5fa2015-05-15 15:26:08 +01003138 // FIXME: remap everything as a miss
3139 cell_defer(cache, cell, false);
Joe Thornber8c081b52014-05-13 16:18:38 +01003140 r = DM_MAPIO_SUBMITTED;
3141
3142 } else
Joe Thornber651f5fa2015-05-15 15:26:08 +01003143 remap_cell_to_origin_clear_discard(cache, cell, block, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003144 break;
3145
3146 default:
Mike Snitzerb61d9502015-04-22 17:25:56 -04003147 DMERR_LIMIT("%s: %s: erroring bio: unknown policy op: %u",
3148 cache_device_name(cache), __func__,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003149 (unsigned) lookup_result.op);
Joe Thornber651f5fa2015-05-15 15:26:08 +01003150 cell_defer(cache, cell, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003151 bio_io_error(bio);
Joe Thornber2ee57d52013-10-24 14:10:29 -04003152 r = DM_MAPIO_SUBMITTED;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003153 }
3154
Joe Thornber2ee57d52013-10-24 14:10:29 -04003155 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003156}
3157
3158static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
3159{
3160 struct cache *cache = ti->private;
3161 unsigned long flags;
Mike Snitzer19b00922013-04-05 15:36:34 +01003162 size_t pb_data_size = get_per_bio_data_size(cache);
3163 struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003164
3165 if (pb->tick) {
Joe Thornberfba10102015-05-29 10:20:56 +01003166 policy_tick(cache->policy, false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003167
3168 spin_lock_irqsave(&cache->lock, flags);
3169 cache->need_tick_bio = true;
3170 spin_unlock_irqrestore(&cache->lock, flags);
3171 }
3172
3173 check_for_quiesced_migrations(cache, pb);
Joe Thornber066dbaa32015-05-15 15:18:01 +01003174 accounted_complete(cache, bio);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003175
3176 return 0;
3177}
3178
3179static int write_dirty_bitset(struct cache *cache)
3180{
Joe Thornber629d0a82016-09-22 06:15:21 -04003181 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003182
Joe Thornber028ae9f2015-04-22 16:42:35 -04003183 if (get_cache_mode(cache) >= CM_READ_ONLY)
3184 return -EINVAL;
3185
Joe Thornber629d0a82016-09-22 06:15:21 -04003186 r = dm_cache_set_dirty_bits(cache->cmd, from_cblock(cache->cache_size), cache->dirty_bitset);
3187 if (r)
3188 metadata_operation_failed(cache, "dm_cache_set_dirty_bits", r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003189
Joe Thornber629d0a82016-09-22 06:15:21 -04003190 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003191}
3192
3193static int write_discard_bitset(struct cache *cache)
3194{
3195 unsigned i, r;
3196
Joe Thornber028ae9f2015-04-22 16:42:35 -04003197 if (get_cache_mode(cache) >= CM_READ_ONLY)
3198 return -EINVAL;
3199
Joe Thornber1bad9bc2014-11-07 14:47:07 +00003200 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
3201 cache->discard_nr_blocks);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003202 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003203 DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04003204 metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003205 return r;
3206 }
3207
Joe Thornber1bad9bc2014-11-07 14:47:07 +00003208 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
3209 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
3210 is_discarded(cache, to_dblock(i)));
Joe Thornber028ae9f2015-04-22 16:42:35 -04003211 if (r) {
3212 metadata_operation_failed(cache, "dm_cache_set_discard", r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003213 return r;
Joe Thornber028ae9f2015-04-22 16:42:35 -04003214 }
3215 }
3216
3217 return 0;
3218}
3219
3220static int write_hints(struct cache *cache)
3221{
3222 int r;
3223
3224 if (get_cache_mode(cache) >= CM_READ_ONLY)
3225 return -EINVAL;
3226
3227 r = dm_cache_write_hints(cache->cmd, cache->policy);
3228 if (r) {
3229 metadata_operation_failed(cache, "dm_cache_write_hints", r);
3230 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003231 }
3232
3233 return 0;
3234}
3235
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003236/*
3237 * returns true on success
3238 */
3239static bool sync_metadata(struct cache *cache)
3240{
3241 int r1, r2, r3, r4;
3242
3243 r1 = write_dirty_bitset(cache);
3244 if (r1)
Mike Snitzerb61d9502015-04-22 17:25:56 -04003245 DMERR("%s: could not write dirty bitset", cache_device_name(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003246
3247 r2 = write_discard_bitset(cache);
3248 if (r2)
Mike Snitzerb61d9502015-04-22 17:25:56 -04003249 DMERR("%s: could not write discard bitset", cache_device_name(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003250
3251 save_stats(cache);
3252
Joe Thornber028ae9f2015-04-22 16:42:35 -04003253 r3 = write_hints(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003254 if (r3)
Mike Snitzerb61d9502015-04-22 17:25:56 -04003255 DMERR("%s: could not write hints", cache_device_name(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003256
3257 /*
3258 * If writing the above metadata failed, we still commit, but don't
3259 * set the clean shutdown flag. This will effectively force every
3260 * dirty bit to be set on reload.
3261 */
Joe Thornber028ae9f2015-04-22 16:42:35 -04003262 r4 = commit(cache, !r1 && !r2 && !r3);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003263 if (r4)
Mike Snitzerb61d9502015-04-22 17:25:56 -04003264 DMERR("%s: could not write cache metadata", cache_device_name(cache));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003265
3266 return !r1 && !r2 && !r3 && !r4;
3267}
3268
3269static void cache_postsuspend(struct dm_target *ti)
3270{
3271 struct cache *cache = ti->private;
3272
3273 start_quiescing(cache);
3274 wait_for_migrations(cache);
3275 stop_worker(cache);
Joe Thornber651f5fa2015-05-15 15:26:08 +01003276 requeue_deferred_bios(cache);
3277 requeue_deferred_cells(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003278 stop_quiescing(cache);
3279
Joe Thornber028ae9f2015-04-22 16:42:35 -04003280 if (get_cache_mode(cache) == CM_WRITE)
3281 (void) sync_metadata(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003282}
3283
3284static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
3285 bool dirty, uint32_t hint, bool hint_valid)
3286{
3287 int r;
3288 struct cache *cache = context;
3289
3290 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
3291 if (r)
3292 return r;
3293
3294 if (dirty)
3295 set_dirty(cache, oblock, cblock);
3296 else
3297 clear_dirty(cache, oblock, cblock);
3298
3299 return 0;
3300}
3301
Joe Thornber3e2e1c32014-11-24 14:06:22 +00003302/*
3303 * The discard block size in the on disk metadata is not
3304 * neccessarily the same as we're currently using. So we have to
3305 * be careful to only set the discarded attribute if we know it
3306 * covers a complete block of the new size.
3307 */
3308struct discard_load_info {
3309 struct cache *cache;
3310
3311 /*
3312 * These blocks are sized using the on disk dblock size, rather
3313 * than the current one.
3314 */
3315 dm_block_t block_size;
3316 dm_block_t discard_begin, discard_end;
3317};
3318
3319static void discard_load_info_init(struct cache *cache,
3320 struct discard_load_info *li)
3321{
3322 li->cache = cache;
3323 li->discard_begin = li->discard_end = 0;
3324}
3325
3326static void set_discard_range(struct discard_load_info *li)
3327{
3328 sector_t b, e;
3329
3330 if (li->discard_begin == li->discard_end)
3331 return;
3332
3333 /*
3334 * Convert to sectors.
3335 */
3336 b = li->discard_begin * li->block_size;
3337 e = li->discard_end * li->block_size;
3338
3339 /*
3340 * Then convert back to the current dblock size.
3341 */
3342 b = dm_sector_div_up(b, li->cache->discard_block_size);
3343 sector_div(e, li->cache->discard_block_size);
3344
3345 /*
3346 * The origin may have shrunk, so we need to check we're still in
3347 * bounds.
3348 */
3349 if (e > from_dblock(li->cache->discard_nr_blocks))
3350 e = from_dblock(li->cache->discard_nr_blocks);
3351
3352 for (; b < e; b++)
3353 set_discard(li->cache, to_dblock(b));
3354}
3355
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003356static int load_discard(void *context, sector_t discard_block_size,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00003357 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003358{
Joe Thornber3e2e1c32014-11-24 14:06:22 +00003359 struct discard_load_info *li = context;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003360
Joe Thornber3e2e1c32014-11-24 14:06:22 +00003361 li->block_size = discard_block_size;
Joe Thornber1bad9bc2014-11-07 14:47:07 +00003362
Joe Thornber3e2e1c32014-11-24 14:06:22 +00003363 if (discard) {
3364 if (from_dblock(dblock) == li->discard_end)
3365 /*
3366 * We're already in a discard range, just extend it.
3367 */
3368 li->discard_end = li->discard_end + 1ULL;
3369
3370 else {
3371 /*
3372 * Emit the old range and start a new one.
3373 */
3374 set_discard_range(li);
3375 li->discard_begin = from_dblock(dblock);
3376 li->discard_end = li->discard_begin + 1ULL;
3377 }
3378 } else {
3379 set_discard_range(li);
3380 li->discard_begin = li->discard_end = 0;
3381 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003382
3383 return 0;
3384}
3385
Joe Thornberf494a9c2013-10-31 13:55:49 -04003386static dm_cblock_t get_cache_dev_size(struct cache *cache)
3387{
3388 sector_t size = get_dev_size(cache->cache_dev);
3389 (void) sector_div(size, cache->sectors_per_block);
3390 return to_cblock(size);
3391}
3392
3393static bool can_resize(struct cache *cache, dm_cblock_t new_size)
3394{
3395 if (from_cblock(new_size) > from_cblock(cache->cache_size))
3396 return true;
3397
3398 /*
3399 * We can't drop a dirty block when shrinking the cache.
3400 */
3401 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
3402 new_size = to_cblock(from_cblock(new_size) + 1);
3403 if (is_dirty(cache, new_size)) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003404 DMERR("%s: unable to shrink cache; cache block %llu is dirty",
3405 cache_device_name(cache),
Joe Thornberf494a9c2013-10-31 13:55:49 -04003406 (unsigned long long) from_cblock(new_size));
3407 return false;
3408 }
3409 }
3410
3411 return true;
3412}
3413
3414static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
3415{
3416 int r;
3417
Vincent Pelletier08844802013-11-30 12:58:42 +01003418 r = dm_cache_resize(cache->cmd, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04003419 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003420 DMERR("%s: could not resize cache metadata", cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04003421 metadata_operation_failed(cache, "dm_cache_resize", r);
Joe Thornberf494a9c2013-10-31 13:55:49 -04003422 return r;
3423 }
3424
Joe Thornberd1d92202014-11-11 11:58:32 +00003425 set_cache_size(cache, new_size);
Joe Thornberf494a9c2013-10-31 13:55:49 -04003426
3427 return 0;
3428}
3429
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003430static int cache_preresume(struct dm_target *ti)
3431{
3432 int r = 0;
3433 struct cache *cache = ti->private;
Joe Thornberf494a9c2013-10-31 13:55:49 -04003434 dm_cblock_t csize = get_cache_dev_size(cache);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003435
3436 /*
3437 * Check to see if the cache has resized.
3438 */
Joe Thornberf494a9c2013-10-31 13:55:49 -04003439 if (!cache->sized) {
3440 r = resize_cache_dev(cache, csize);
3441 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003442 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003443
3444 cache->sized = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04003445
3446 } else if (csize != cache->cache_size) {
3447 if (!can_resize(cache, csize))
3448 return -EINVAL;
3449
3450 r = resize_cache_dev(cache, csize);
3451 if (r)
3452 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003453 }
3454
3455 if (!cache->loaded_mappings) {
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00003456 r = dm_cache_load_mappings(cache->cmd, cache->policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003457 load_mapping, cache);
3458 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003459 DMERR("%s: could not load cache mappings", cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04003460 metadata_operation_failed(cache, "dm_cache_load_mappings", r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003461 return r;
3462 }
3463
3464 cache->loaded_mappings = true;
3465 }
3466
3467 if (!cache->loaded_discards) {
Joe Thornber3e2e1c32014-11-24 14:06:22 +00003468 struct discard_load_info li;
3469
3470 /*
3471 * The discard bitset could have been resized, or the
3472 * discard block size changed. To be safe we start by
3473 * setting every dblock to not discarded.
3474 */
3475 clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
3476
3477 discard_load_info_init(cache, &li);
3478 r = dm_cache_load_discards(cache->cmd, load_discard, &li);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003479 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003480 DMERR("%s: could not load origin discards", cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04003481 metadata_operation_failed(cache, "dm_cache_load_discards", r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003482 return r;
3483 }
Joe Thornber3e2e1c32014-11-24 14:06:22 +00003484 set_discard_range(&li);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003485
3486 cache->loaded_discards = true;
3487 }
3488
3489 return r;
3490}
3491
3492static void cache_resume(struct dm_target *ti)
3493{
3494 struct cache *cache = ti->private;
3495
3496 cache->need_tick_bio = true;
3497 do_waker(&cache->waker.work);
3498}
3499
3500/*
3501 * Status format:
3502 *
Mike Snitzer6a388612014-01-09 16:04:12 -05003503 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
3504 * <cache block size> <#used cache blocks>/<#total cache blocks>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003505 * <#read hits> <#read misses> <#write hits> <#write misses>
Mike Snitzer6a388612014-01-09 16:04:12 -05003506 * <#demotions> <#promotions> <#dirty>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003507 * <#features> <features>*
3508 * <#core args> <core args>
Mike Snitzer255eac22015-07-15 11:42:59 -04003509 * <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003510 */
3511static void cache_status(struct dm_target *ti, status_type_t type,
3512 unsigned status_flags, char *result, unsigned maxlen)
3513{
3514 int r = 0;
3515 unsigned i;
3516 ssize_t sz = 0;
3517 dm_block_t nr_free_blocks_metadata = 0;
3518 dm_block_t nr_blocks_metadata = 0;
3519 char buf[BDEVNAME_SIZE];
3520 struct cache *cache = ti->private;
3521 dm_cblock_t residency;
Joe Thornberd14fcf32016-03-10 16:20:58 +00003522 bool needs_check;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003523
3524 switch (type) {
3525 case STATUSTYPE_INFO:
Joe Thornber028ae9f2015-04-22 16:42:35 -04003526 if (get_cache_mode(cache) == CM_FAIL) {
3527 DMEMIT("Fail");
3528 break;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003529 }
3530
Joe Thornber028ae9f2015-04-22 16:42:35 -04003531 /* Commit to ensure statistics aren't out-of-date */
3532 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
3533 (void) commit(cache, false);
3534
Mike Snitzerb61d9502015-04-22 17:25:56 -04003535 r = dm_cache_get_free_metadata_block_count(cache->cmd, &nr_free_blocks_metadata);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003536 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003537 DMERR("%s: dm_cache_get_free_metadata_block_count returned %d",
3538 cache_device_name(cache), r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003539 goto err;
3540 }
3541
3542 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
3543 if (r) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003544 DMERR("%s: dm_cache_get_metadata_dev_size returned %d",
3545 cache_device_name(cache), r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003546 goto err;
3547 }
3548
3549 residency = policy_residency(cache->policy);
3550
Joe Thornberca763d02017-02-09 11:46:18 -05003551 DMEMIT("%u %llu/%llu %llu %llu/%llu %u %u %u %u %u %u %lu ",
Mike Snitzer895b47d2014-07-14 15:37:18 -04003552 (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003553 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3554 (unsigned long long)nr_blocks_metadata,
Joe Thornberca763d02017-02-09 11:46:18 -05003555 (unsigned long long)cache->sectors_per_block,
Mike Snitzer6a388612014-01-09 16:04:12 -05003556 (unsigned long long) from_cblock(residency),
3557 (unsigned long long) from_cblock(cache->cache_size),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003558 (unsigned) atomic_read(&cache->stats.read_hit),
3559 (unsigned) atomic_read(&cache->stats.read_miss),
3560 (unsigned) atomic_read(&cache->stats.write_hit),
3561 (unsigned) atomic_read(&cache->stats.write_miss),
3562 (unsigned) atomic_read(&cache->stats.demotion),
3563 (unsigned) atomic_read(&cache->stats.promotion),
Anssi Hannula44fa8162014-08-01 11:55:47 -04003564 (unsigned long) atomic_read(&cache->nr_dirty));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003565
Joe Thornber629d0a82016-09-22 06:15:21 -04003566 if (cache->features.metadata_version == 2)
3567 DMEMIT("2 metadata2 ");
3568 else
3569 DMEMIT("1 ");
3570
Joe Thornber2ee57d52013-10-24 14:10:29 -04003571 if (writethrough_mode(&cache->features))
Joe Thornber629d0a82016-09-22 06:15:21 -04003572 DMEMIT("writethrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04003573
3574 else if (passthrough_mode(&cache->features))
Joe Thornber629d0a82016-09-22 06:15:21 -04003575 DMEMIT("passthrough ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04003576
3577 else if (writeback_mode(&cache->features))
Joe Thornber629d0a82016-09-22 06:15:21 -04003578 DMEMIT("writeback ");
Joe Thornber2ee57d52013-10-24 14:10:29 -04003579
3580 else {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003581 DMERR("%s: internal error: unknown io mode: %d",
3582 cache_device_name(cache), (int) cache->features.io_mode);
Joe Thornber2ee57d52013-10-24 14:10:29 -04003583 goto err;
3584 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003585
3586 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05003587
3588 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003589 if (sz < maxlen) {
Joe Thornber028ae9f2015-04-22 16:42:35 -04003590 r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003591 if (r)
Mike Snitzerb61d9502015-04-22 17:25:56 -04003592 DMERR("%s: policy_emit_config_values returned %d",
3593 cache_device_name(cache), r);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003594 }
3595
Joe Thornber028ae9f2015-04-22 16:42:35 -04003596 if (get_cache_mode(cache) == CM_READ_ONLY)
3597 DMEMIT("ro ");
3598 else
3599 DMEMIT("rw ");
3600
Joe Thornberd14fcf32016-03-10 16:20:58 +00003601 r = dm_cache_metadata_needs_check(cache->cmd, &needs_check);
3602
3603 if (r || needs_check)
Mike Snitzer255eac22015-07-15 11:42:59 -04003604 DMEMIT("needs_check ");
3605 else
3606 DMEMIT("- ");
3607
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003608 break;
3609
3610 case STATUSTYPE_TABLE:
3611 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
3612 DMEMIT("%s ", buf);
3613 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
3614 DMEMIT("%s ", buf);
3615 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
3616 DMEMIT("%s", buf);
3617
3618 for (i = 0; i < cache->nr_ctr_args - 1; i++)
3619 DMEMIT(" %s", cache->ctr_args[i]);
3620 if (cache->nr_ctr_args)
3621 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
3622 }
3623
3624 return;
3625
3626err:
3627 DMEMIT("Error");
3628}
3629
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003630/*
Joe Thornber65790ff2013-11-08 16:39:50 +00003631 * A cache block range can take two forms:
3632 *
3633 * i) A single cblock, eg. '3456'
3634 * ii) A begin and end cblock with dots between, eg. 123-234
3635 */
3636static int parse_cblock_range(struct cache *cache, const char *str,
3637 struct cblock_range *result)
3638{
3639 char dummy;
3640 uint64_t b, e;
3641 int r;
3642
3643 /*
3644 * Try and parse form (ii) first.
3645 */
3646 r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
3647 if (r < 0)
3648 return r;
3649
3650 if (r == 2) {
3651 result->begin = to_cblock(b);
3652 result->end = to_cblock(e);
3653 return 0;
3654 }
3655
3656 /*
3657 * That didn't work, try form (i).
3658 */
3659 r = sscanf(str, "%llu%c", &b, &dummy);
3660 if (r < 0)
3661 return r;
3662
3663 if (r == 1) {
3664 result->begin = to_cblock(b);
3665 result->end = to_cblock(from_cblock(result->begin) + 1u);
3666 return 0;
3667 }
3668
Mike Snitzerb61d9502015-04-22 17:25:56 -04003669 DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
Joe Thornber65790ff2013-11-08 16:39:50 +00003670 return -EINVAL;
3671}
3672
3673static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
3674{
3675 uint64_t b = from_cblock(range->begin);
3676 uint64_t e = from_cblock(range->end);
3677 uint64_t n = from_cblock(cache->cache_size);
3678
3679 if (b >= n) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003680 DMERR("%s: begin cblock out of range: %llu >= %llu",
3681 cache_device_name(cache), b, n);
Joe Thornber65790ff2013-11-08 16:39:50 +00003682 return -EINVAL;
3683 }
3684
3685 if (e > n) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003686 DMERR("%s: end cblock out of range: %llu > %llu",
3687 cache_device_name(cache), e, n);
Joe Thornber65790ff2013-11-08 16:39:50 +00003688 return -EINVAL;
3689 }
3690
3691 if (b >= e) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003692 DMERR("%s: invalid cblock range: %llu >= %llu",
3693 cache_device_name(cache), b, e);
Joe Thornber65790ff2013-11-08 16:39:50 +00003694 return -EINVAL;
3695 }
3696
3697 return 0;
3698}
3699
3700static int request_invalidation(struct cache *cache, struct cblock_range *range)
3701{
3702 struct invalidation_request req;
3703
3704 INIT_LIST_HEAD(&req.list);
3705 req.cblocks = range;
3706 atomic_set(&req.complete, 0);
3707 req.err = 0;
3708 init_waitqueue_head(&req.result_wait);
3709
3710 spin_lock(&cache->invalidation_lock);
3711 list_add(&req.list, &cache->invalidation_requests);
3712 spin_unlock(&cache->invalidation_lock);
3713 wake_worker(cache);
3714
3715 wait_event(req.result_wait, atomic_read(&req.complete));
3716 return req.err;
3717}
3718
3719static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
3720 const char **cblock_ranges)
3721{
3722 int r = 0;
3723 unsigned i;
3724 struct cblock_range range;
3725
3726 if (!passthrough_mode(&cache->features)) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003727 DMERR("%s: cache has to be in passthrough mode for invalidation",
3728 cache_device_name(cache));
Joe Thornber65790ff2013-11-08 16:39:50 +00003729 return -EPERM;
3730 }
3731
3732 for (i = 0; i < count; i++) {
3733 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3734 if (r)
3735 break;
3736
3737 r = validate_cblock_range(cache, &range);
3738 if (r)
3739 break;
3740
3741 /*
3742 * Pass begin and end origin blocks to the worker and wake it.
3743 */
3744 r = request_invalidation(cache, &range);
3745 if (r)
3746 break;
3747 }
3748
3749 return r;
3750}
3751
3752/*
3753 * Supports
3754 * "<key> <value>"
3755 * and
3756 * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003757 *
3758 * The key migration_threshold is supported by the cache target core.
3759 */
3760static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3761{
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003762 struct cache *cache = ti->private;
3763
Joe Thornber65790ff2013-11-08 16:39:50 +00003764 if (!argc)
3765 return -EINVAL;
3766
Joe Thornber028ae9f2015-04-22 16:42:35 -04003767 if (get_cache_mode(cache) >= CM_READ_ONLY) {
Mike Snitzerb61d9502015-04-22 17:25:56 -04003768 DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
3769 cache_device_name(cache));
Joe Thornber028ae9f2015-04-22 16:42:35 -04003770 return -EOPNOTSUPP;
3771 }
3772
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -05003773 if (!strcasecmp(argv[0], "invalidate_cblocks"))
Joe Thornber65790ff2013-11-08 16:39:50 +00003774 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3775
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003776 if (argc != 2)
3777 return -EINVAL;
3778
Joe Thornber2f14f4b2013-05-10 14:37:21 +01003779 return set_config_value(cache, argv[0], argv[1]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003780}
3781
3782static int cache_iterate_devices(struct dm_target *ti,
3783 iterate_devices_callout_fn fn, void *data)
3784{
3785 int r = 0;
3786 struct cache *cache = ti->private;
3787
3788 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3789 if (!r)
3790 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3791
3792 return r;
3793}
3794
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003795static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3796{
3797 /*
3798 * FIXME: these limits may be incompatible with the cache device
3799 */
Joe Thornber7ae34e72014-11-06 10:18:04 +00003800 limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
3801 cache->origin_sectors);
Joe Thornber1bad9bc2014-11-07 14:47:07 +00003802 limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003803}
3804
3805static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3806{
3807 struct cache *cache = ti->private;
Mike Snitzerf6109372013-08-20 15:02:41 -04003808 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003809
Mike Snitzerf6109372013-08-20 15:02:41 -04003810 /*
3811 * If the system-determined stacked limits are compatible with the
3812 * cache's blocksize (io_opt is a factor) do not override them.
3813 */
3814 if (io_opt_sectors < cache->sectors_per_block ||
3815 do_div(io_opt_sectors, cache->sectors_per_block)) {
Mike Snitzerb0246532014-07-19 13:25:46 -04003816 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf6109372013-08-20 15:02:41 -04003817 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3818 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003819 set_discard_limits(cache, limits);
3820}
3821
3822/*----------------------------------------------------------------*/
3823
3824static struct target_type cache_target = {
3825 .name = "cache",
Joe Thornber629d0a82016-09-22 06:15:21 -04003826 .version = {1, 10, 0},
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003827 .module = THIS_MODULE,
3828 .ctr = cache_ctr,
3829 .dtr = cache_dtr,
3830 .map = cache_map,
3831 .end_io = cache_end_io,
3832 .postsuspend = cache_postsuspend,
3833 .preresume = cache_preresume,
3834 .resume = cache_resume,
3835 .status = cache_status,
3836 .message = cache_message,
3837 .iterate_devices = cache_iterate_devices,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00003838 .io_hints = cache_io_hints,
3839};
3840
3841static int __init dm_cache_init(void)
3842{
3843 int r;
3844
3845 r = dm_register_target(&cache_target);
3846 if (r) {
3847 DMERR("cache target registration failed: %d", r);
3848 return r;
3849 }
3850
3851 migration_cache = KMEM_CACHE(dm_cache_migration, 0);
3852 if (!migration_cache) {
3853 dm_unregister_target(&cache_target);
3854 return -ENOMEM;
3855 }
3856
3857 return 0;
3858}
3859
3860static void __exit dm_cache_exit(void)
3861{
3862 dm_unregister_target(&cache_target);
3863 kmem_cache_destroy(migration_cache);
3864}
3865
3866module_init(dm_cache_init);
3867module_exit(dm_cache_exit);
3868
3869MODULE_DESCRIPTION(DM_NAME " cache target");
3870MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
3871MODULE_LICENSE("GPL");