Commit | Line | Data |
---|---|---|
c6b4fcba JT |
1 | /* |
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" | |
b844fe69 | 9 | #include "dm-bio-record.h" |
c6b4fcba JT |
10 | #include "dm-cache-metadata.h" |
11 | ||
12 | #include <linux/dm-io.h> | |
13 | #include <linux/dm-kcopyd.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/mempool.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/vmalloc.h> | |
19 | ||
20 | #define DM_MSG_PREFIX "cache" | |
21 | ||
22 | DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle, | |
23 | "A percentage of time allocated for copying to and/or from cache"); | |
24 | ||
25 | /*----------------------------------------------------------------*/ | |
26 | ||
27 | /* | |
28 | * Glossary: | |
29 | * | |
30 | * oblock: index of an origin block | |
31 | * cblock: index of a cache block | |
32 | * promotion: movement of a block from origin to cache | |
33 | * demotion: movement of a block from cache to origin | |
34 | * migration: movement of a block between the origin and cache device, | |
35 | * either direction | |
36 | */ | |
37 | ||
38 | /*----------------------------------------------------------------*/ | |
39 | ||
40 | static size_t bitset_size_in_bytes(unsigned nr_entries) | |
41 | { | |
42 | return sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG); | |
43 | } | |
44 | ||
45 | static unsigned long *alloc_bitset(unsigned nr_entries) | |
46 | { | |
47 | size_t s = bitset_size_in_bytes(nr_entries); | |
48 | return vzalloc(s); | |
49 | } | |
50 | ||
51 | static void clear_bitset(void *bitset, unsigned nr_entries) | |
52 | { | |
53 | size_t s = bitset_size_in_bytes(nr_entries); | |
54 | memset(bitset, 0, s); | |
55 | } | |
56 | ||
57 | static void free_bitset(unsigned long *bits) | |
58 | { | |
59 | vfree(bits); | |
60 | } | |
61 | ||
62 | /*----------------------------------------------------------------*/ | |
63 | ||
c9d28d5d JT |
64 | /* |
65 | * There are a couple of places where we let a bio run, but want to do some | |
66 | * work before calling its endio function. We do this by temporarily | |
67 | * changing the endio fn. | |
68 | */ | |
69 | struct dm_hook_info { | |
70 | bio_end_io_t *bi_end_io; | |
71 | void *bi_private; | |
72 | }; | |
73 | ||
74 | static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio, | |
75 | bio_end_io_t *bi_end_io, void *bi_private) | |
76 | { | |
77 | h->bi_end_io = bio->bi_end_io; | |
78 | h->bi_private = bio->bi_private; | |
79 | ||
80 | bio->bi_end_io = bi_end_io; | |
81 | bio->bi_private = bi_private; | |
82 | } | |
83 | ||
84 | static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio) | |
85 | { | |
86 | bio->bi_end_io = h->bi_end_io; | |
87 | bio->bi_private = h->bi_private; | |
8d307269 MS |
88 | |
89 | /* | |
90 | * Must bump bi_remaining to allow bio to complete with | |
91 | * restored bi_end_io. | |
92 | */ | |
93 | atomic_inc(&bio->bi_remaining); | |
c9d28d5d JT |
94 | } |
95 | ||
96 | /*----------------------------------------------------------------*/ | |
97 | ||
c6b4fcba JT |
98 | #define MIGRATION_POOL_SIZE 128 |
99 | #define COMMIT_PERIOD HZ | |
100 | #define MIGRATION_COUNT_WINDOW 10 | |
101 | ||
102 | /* | |
05473044 MS |
103 | * The block size of the device holding cache data must be |
104 | * between 32KB and 1GB. | |
c6b4fcba JT |
105 | */ |
106 | #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT) | |
05473044 | 107 | #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT) |
c6b4fcba JT |
108 | |
109 | /* | |
110 | * FIXME: the cache is read/write for the time being. | |
111 | */ | |
2ee57d58 | 112 | enum cache_metadata_mode { |
c6b4fcba JT |
113 | CM_WRITE, /* metadata may be changed */ |
114 | CM_READ_ONLY, /* metadata may not be changed */ | |
115 | }; | |
116 | ||
2ee57d58 JT |
117 | enum cache_io_mode { |
118 | /* | |
119 | * Data is written to cached blocks only. These blocks are marked | |
120 | * dirty. If you lose the cache device you will lose data. | |
121 | * Potential performance increase for both reads and writes. | |
122 | */ | |
123 | CM_IO_WRITEBACK, | |
124 | ||
125 | /* | |
126 | * Data is written to both cache and origin. Blocks are never | |
127 | * dirty. Potential performance benfit for reads only. | |
128 | */ | |
129 | CM_IO_WRITETHROUGH, | |
130 | ||
131 | /* | |
132 | * A degraded mode useful for various cache coherency situations | |
133 | * (eg, rolling back snapshots). Reads and writes always go to the | |
134 | * origin. If a write goes to a cached oblock, then the cache | |
135 | * block is invalidated. | |
136 | */ | |
137 | CM_IO_PASSTHROUGH | |
138 | }; | |
139 | ||
c6b4fcba | 140 | struct cache_features { |
2ee57d58 JT |
141 | enum cache_metadata_mode mode; |
142 | enum cache_io_mode io_mode; | |
c6b4fcba JT |
143 | }; |
144 | ||
145 | struct cache_stats { | |
146 | atomic_t read_hit; | |
147 | atomic_t read_miss; | |
148 | atomic_t write_hit; | |
149 | atomic_t write_miss; | |
150 | atomic_t demotion; | |
151 | atomic_t promotion; | |
152 | atomic_t copies_avoided; | |
153 | atomic_t cache_cell_clash; | |
154 | atomic_t commit_count; | |
155 | atomic_t discard_count; | |
156 | }; | |
157 | ||
65790ff9 JT |
158 | /* |
159 | * Defines a range of cblocks, begin to (end - 1) are in the range. end is | |
160 | * the one-past-the-end value. | |
161 | */ | |
162 | struct cblock_range { | |
163 | dm_cblock_t begin; | |
164 | dm_cblock_t end; | |
165 | }; | |
166 | ||
167 | struct invalidation_request { | |
168 | struct list_head list; | |
169 | struct cblock_range *cblocks; | |
170 | ||
171 | atomic_t complete; | |
172 | int err; | |
173 | ||
174 | wait_queue_head_t result_wait; | |
175 | }; | |
176 | ||
c6b4fcba JT |
177 | struct cache { |
178 | struct dm_target *ti; | |
179 | struct dm_target_callbacks callbacks; | |
180 | ||
c9ec5d7c MS |
181 | struct dm_cache_metadata *cmd; |
182 | ||
c6b4fcba JT |
183 | /* |
184 | * Metadata is written to this device. | |
185 | */ | |
186 | struct dm_dev *metadata_dev; | |
187 | ||
188 | /* | |
189 | * The slower of the two data devices. Typically a spindle. | |
190 | */ | |
191 | struct dm_dev *origin_dev; | |
192 | ||
193 | /* | |
194 | * The faster of the two data devices. Typically an SSD. | |
195 | */ | |
196 | struct dm_dev *cache_dev; | |
197 | ||
c6b4fcba JT |
198 | /* |
199 | * Size of the origin device in _complete_ blocks and native sectors. | |
200 | */ | |
201 | dm_oblock_t origin_blocks; | |
202 | sector_t origin_sectors; | |
203 | ||
204 | /* | |
205 | * Size of the cache device in blocks. | |
206 | */ | |
207 | dm_cblock_t cache_size; | |
208 | ||
209 | /* | |
210 | * Fields for converting from sectors to blocks. | |
211 | */ | |
212 | uint32_t sectors_per_block; | |
213 | int sectors_per_block_shift; | |
214 | ||
c6b4fcba JT |
215 | spinlock_t lock; |
216 | struct bio_list deferred_bios; | |
217 | struct bio_list deferred_flush_bios; | |
e2e74d61 | 218 | struct bio_list deferred_writethrough_bios; |
c6b4fcba JT |
219 | struct list_head quiesced_migrations; |
220 | struct list_head completed_migrations; | |
221 | struct list_head need_commit_migrations; | |
222 | sector_t migration_threshold; | |
c6b4fcba | 223 | wait_queue_head_t migration_wait; |
c9ec5d7c | 224 | atomic_t nr_migrations; |
c6b4fcba | 225 | |
66cb1910 | 226 | wait_queue_head_t quiescing_wait; |
238f8363 | 227 | atomic_t quiescing; |
66cb1910 JT |
228 | atomic_t quiescing_ack; |
229 | ||
c6b4fcba JT |
230 | /* |
231 | * cache_size entries, dirty if set | |
232 | */ | |
44fa816b | 233 | atomic_t nr_dirty; |
c6b4fcba JT |
234 | unsigned long *dirty_bitset; |
235 | ||
236 | /* | |
237 | * origin_blocks entries, discarded if set. | |
238 | */ | |
1bad9bc4 | 239 | dm_dblock_t discard_nr_blocks; |
c6b4fcba | 240 | unsigned long *discard_bitset; |
08b18451 | 241 | uint32_t discard_block_size; /* a power of 2 times sectors per block */ |
c9ec5d7c MS |
242 | |
243 | /* | |
244 | * Rather than reconstructing the table line for the status we just | |
245 | * save it and regurgitate. | |
246 | */ | |
247 | unsigned nr_ctr_args; | |
248 | const char **ctr_args; | |
c6b4fcba JT |
249 | |
250 | struct dm_kcopyd_client *copier; | |
251 | struct workqueue_struct *wq; | |
252 | struct work_struct worker; | |
253 | ||
254 | struct delayed_work waker; | |
255 | unsigned long last_commit_jiffies; | |
256 | ||
257 | struct dm_bio_prison *prison; | |
258 | struct dm_deferred_set *all_io_ds; | |
259 | ||
260 | mempool_t *migration_pool; | |
261 | struct dm_cache_migration *next_migration; | |
262 | ||
263 | struct dm_cache_policy *policy; | |
264 | unsigned policy_nr_args; | |
265 | ||
266 | bool need_tick_bio:1; | |
267 | bool sized:1; | |
65790ff9 | 268 | bool invalidate:1; |
c6b4fcba JT |
269 | bool commit_requested:1; |
270 | bool loaded_mappings:1; | |
271 | bool loaded_discards:1; | |
272 | ||
c6b4fcba | 273 | /* |
c9ec5d7c | 274 | * Cache features such as write-through. |
c6b4fcba | 275 | */ |
c9ec5d7c MS |
276 | struct cache_features features; |
277 | ||
278 | struct cache_stats stats; | |
65790ff9 JT |
279 | |
280 | /* | |
281 | * Invalidation fields. | |
282 | */ | |
283 | spinlock_t invalidation_lock; | |
284 | struct list_head invalidation_requests; | |
c6b4fcba JT |
285 | }; |
286 | ||
287 | struct per_bio_data { | |
288 | bool tick:1; | |
289 | unsigned req_nr:2; | |
290 | struct dm_deferred_entry *all_io_entry; | |
c6eda5e8 | 291 | struct dm_hook_info hook_info; |
e2e74d61 | 292 | |
19b0092e MS |
293 | /* |
294 | * writethrough fields. These MUST remain at the end of this | |
295 | * structure and the 'cache' member must be the first as it | |
aeed1420 | 296 | * is used to determine the offset of the writethrough fields. |
19b0092e | 297 | */ |
e2e74d61 JT |
298 | struct cache *cache; |
299 | dm_cblock_t cblock; | |
b844fe69 | 300 | struct dm_bio_details bio_details; |
c6b4fcba JT |
301 | }; |
302 | ||
303 | struct dm_cache_migration { | |
304 | struct list_head list; | |
305 | struct cache *cache; | |
306 | ||
307 | unsigned long start_jiffies; | |
308 | dm_oblock_t old_oblock; | |
309 | dm_oblock_t new_oblock; | |
310 | dm_cblock_t cblock; | |
311 | ||
312 | bool err:1; | |
7ae34e77 | 313 | bool discard:1; |
c6b4fcba JT |
314 | bool writeback:1; |
315 | bool demote:1; | |
316 | bool promote:1; | |
c9d28d5d | 317 | bool requeue_holder:1; |
65790ff9 | 318 | bool invalidate:1; |
c6b4fcba JT |
319 | |
320 | struct dm_bio_prison_cell *old_ocell; | |
321 | struct dm_bio_prison_cell *new_ocell; | |
322 | }; | |
323 | ||
324 | /* | |
325 | * Processing a bio in the worker thread may require these memory | |
326 | * allocations. We prealloc to avoid deadlocks (the same worker thread | |
327 | * frees them back to the mempool). | |
328 | */ | |
329 | struct prealloc { | |
330 | struct dm_cache_migration *mg; | |
331 | struct dm_bio_prison_cell *cell1; | |
332 | struct dm_bio_prison_cell *cell2; | |
333 | }; | |
334 | ||
335 | static void wake_worker(struct cache *cache) | |
336 | { | |
337 | queue_work(cache->wq, &cache->worker); | |
338 | } | |
339 | ||
340 | /*----------------------------------------------------------------*/ | |
341 | ||
342 | static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache) | |
343 | { | |
344 | /* FIXME: change to use a local slab. */ | |
345 | return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT); | |
346 | } | |
347 | ||
348 | static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell) | |
349 | { | |
350 | dm_bio_prison_free_cell(cache->prison, cell); | |
351 | } | |
352 | ||
353 | static int prealloc_data_structs(struct cache *cache, struct prealloc *p) | |
354 | { | |
355 | if (!p->mg) { | |
356 | p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT); | |
357 | if (!p->mg) | |
358 | return -ENOMEM; | |
359 | } | |
360 | ||
361 | if (!p->cell1) { | |
362 | p->cell1 = alloc_prison_cell(cache); | |
363 | if (!p->cell1) | |
364 | return -ENOMEM; | |
365 | } | |
366 | ||
367 | if (!p->cell2) { | |
368 | p->cell2 = alloc_prison_cell(cache); | |
369 | if (!p->cell2) | |
370 | return -ENOMEM; | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | static void prealloc_free_structs(struct cache *cache, struct prealloc *p) | |
377 | { | |
378 | if (p->cell2) | |
379 | free_prison_cell(cache, p->cell2); | |
380 | ||
381 | if (p->cell1) | |
382 | free_prison_cell(cache, p->cell1); | |
383 | ||
384 | if (p->mg) | |
385 | mempool_free(p->mg, cache->migration_pool); | |
386 | } | |
387 | ||
388 | static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p) | |
389 | { | |
390 | struct dm_cache_migration *mg = p->mg; | |
391 | ||
392 | BUG_ON(!mg); | |
393 | p->mg = NULL; | |
394 | ||
395 | return mg; | |
396 | } | |
397 | ||
398 | /* | |
399 | * You must have a cell within the prealloc struct to return. If not this | |
400 | * function will BUG() rather than returning NULL. | |
401 | */ | |
402 | static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p) | |
403 | { | |
404 | struct dm_bio_prison_cell *r = NULL; | |
405 | ||
406 | if (p->cell1) { | |
407 | r = p->cell1; | |
408 | p->cell1 = NULL; | |
409 | ||
410 | } else if (p->cell2) { | |
411 | r = p->cell2; | |
412 | p->cell2 = NULL; | |
413 | } else | |
414 | BUG(); | |
415 | ||
416 | return r; | |
417 | } | |
418 | ||
419 | /* | |
420 | * You can't have more than two cells in a prealloc struct. BUG() will be | |
421 | * called if you try and overfill. | |
422 | */ | |
423 | static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell) | |
424 | { | |
425 | if (!p->cell2) | |
426 | p->cell2 = cell; | |
427 | ||
428 | else if (!p->cell1) | |
429 | p->cell1 = cell; | |
430 | ||
431 | else | |
432 | BUG(); | |
433 | } | |
434 | ||
435 | /*----------------------------------------------------------------*/ | |
436 | ||
7ae34e77 | 437 | static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key *key) |
c6b4fcba JT |
438 | { |
439 | key->virtual = 0; | |
440 | key->dev = 0; | |
7ae34e77 JT |
441 | key->block_begin = from_oblock(begin); |
442 | key->block_end = from_oblock(end); | |
c6b4fcba JT |
443 | } |
444 | ||
445 | /* | |
446 | * The caller hands in a preallocated cell, and a free function for it. | |
447 | * The cell will be freed if there's an error, or if it wasn't used because | |
448 | * a cell with that key already exists. | |
449 | */ | |
450 | typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell); | |
451 | ||
7ae34e77 JT |
452 | static int bio_detain_range(struct cache *cache, dm_oblock_t oblock_begin, dm_oblock_t oblock_end, |
453 | struct bio *bio, struct dm_bio_prison_cell *cell_prealloc, | |
454 | cell_free_fn free_fn, void *free_context, | |
455 | struct dm_bio_prison_cell **cell_result) | |
c6b4fcba JT |
456 | { |
457 | int r; | |
458 | struct dm_cell_key key; | |
459 | ||
7ae34e77 | 460 | build_key(oblock_begin, oblock_end, &key); |
c6b4fcba JT |
461 | r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result); |
462 | if (r) | |
463 | free_fn(free_context, cell_prealloc); | |
464 | ||
465 | return r; | |
466 | } | |
467 | ||
7ae34e77 JT |
468 | static int bio_detain(struct cache *cache, dm_oblock_t oblock, |
469 | struct bio *bio, struct dm_bio_prison_cell *cell_prealloc, | |
470 | cell_free_fn free_fn, void *free_context, | |
471 | struct dm_bio_prison_cell **cell_result) | |
472 | { | |
473 | dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL); | |
474 | return bio_detain_range(cache, oblock, end, bio, | |
475 | cell_prealloc, free_fn, free_context, cell_result); | |
476 | } | |
477 | ||
c6b4fcba JT |
478 | static int get_cell(struct cache *cache, |
479 | dm_oblock_t oblock, | |
480 | struct prealloc *structs, | |
481 | struct dm_bio_prison_cell **cell_result) | |
482 | { | |
483 | int r; | |
484 | struct dm_cell_key key; | |
485 | struct dm_bio_prison_cell *cell_prealloc; | |
486 | ||
487 | cell_prealloc = prealloc_get_cell(structs); | |
488 | ||
7ae34e77 | 489 | build_key(oblock, to_oblock(from_oblock(oblock) + 1ULL), &key); |
c6b4fcba JT |
490 | r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result); |
491 | if (r) | |
492 | prealloc_put_cell(structs, cell_prealloc); | |
493 | ||
494 | return r; | |
495 | } | |
496 | ||
aeed1420 | 497 | /*----------------------------------------------------------------*/ |
c6b4fcba JT |
498 | |
499 | static bool is_dirty(struct cache *cache, dm_cblock_t b) | |
500 | { | |
501 | return test_bit(from_cblock(b), cache->dirty_bitset); | |
502 | } | |
503 | ||
504 | static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock) | |
505 | { | |
506 | if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) { | |
44fa816b | 507 | atomic_inc(&cache->nr_dirty); |
c6b4fcba JT |
508 | policy_set_dirty(cache->policy, oblock); |
509 | } | |
510 | } | |
511 | ||
512 | static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock) | |
513 | { | |
514 | if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) { | |
515 | policy_clear_dirty(cache->policy, oblock); | |
44fa816b | 516 | if (atomic_dec_return(&cache->nr_dirty) == 0) |
c6b4fcba JT |
517 | dm_table_event(cache->ti->table); |
518 | } | |
519 | } | |
520 | ||
521 | /*----------------------------------------------------------------*/ | |
aeed1420 | 522 | |
c6b4fcba JT |
523 | static bool block_size_is_power_of_two(struct cache *cache) |
524 | { | |
525 | return cache->sectors_per_block_shift >= 0; | |
526 | } | |
527 | ||
43aeaa29 MP |
528 | /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */ |
529 | #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6 | |
530 | __always_inline | |
531 | #endif | |
414dd67d JT |
532 | static dm_block_t block_div(dm_block_t b, uint32_t n) |
533 | { | |
534 | do_div(b, n); | |
535 | ||
536 | return b; | |
537 | } | |
538 | ||
7ae34e77 | 539 | static dm_block_t oblocks_per_dblock(struct cache *cache) |
1bad9bc4 | 540 | { |
7ae34e77 | 541 | dm_block_t oblocks = cache->discard_block_size; |
1bad9bc4 | 542 | |
7ae34e77 JT |
543 | if (block_size_is_power_of_two(cache)) |
544 | oblocks >>= cache->sectors_per_block_shift; | |
1bad9bc4 | 545 | else |
7ae34e77 | 546 | oblocks = block_div(oblocks, cache->sectors_per_block); |
1bad9bc4 | 547 | |
7ae34e77 JT |
548 | return oblocks; |
549 | } | |
550 | ||
551 | static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock) | |
552 | { | |
553 | return to_dblock(block_div(from_oblock(oblock), | |
554 | oblocks_per_dblock(cache))); | |
555 | } | |
1bad9bc4 | 556 | |
7ae34e77 JT |
557 | static dm_oblock_t dblock_to_oblock(struct cache *cache, dm_dblock_t dblock) |
558 | { | |
559 | return to_oblock(from_dblock(dblock) * oblocks_per_dblock(cache)); | |
1bad9bc4 JT |
560 | } |
561 | ||
562 | static void set_discard(struct cache *cache, dm_dblock_t b) | |
c6b4fcba JT |
563 | { |
564 | unsigned long flags; | |
565 | ||
7ae34e77 | 566 | BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks)); |
c6b4fcba JT |
567 | atomic_inc(&cache->stats.discard_count); |
568 | ||
569 | spin_lock_irqsave(&cache->lock, flags); | |
1bad9bc4 | 570 | set_bit(from_dblock(b), cache->discard_bitset); |
c6b4fcba JT |
571 | spin_unlock_irqrestore(&cache->lock, flags); |
572 | } | |
573 | ||
1bad9bc4 | 574 | static void clear_discard(struct cache *cache, dm_dblock_t b) |
c6b4fcba JT |
575 | { |
576 | unsigned long flags; | |
577 | ||
578 | spin_lock_irqsave(&cache->lock, flags); | |
1bad9bc4 | 579 | clear_bit(from_dblock(b), cache->discard_bitset); |
c6b4fcba JT |
580 | spin_unlock_irqrestore(&cache->lock, flags); |
581 | } | |
582 | ||
1bad9bc4 | 583 | static bool is_discarded(struct cache *cache, dm_dblock_t b) |
c6b4fcba JT |
584 | { |
585 | int r; | |
586 | unsigned long flags; | |
587 | ||
588 | spin_lock_irqsave(&cache->lock, flags); | |
1bad9bc4 | 589 | r = test_bit(from_dblock(b), cache->discard_bitset); |
c6b4fcba JT |
590 | spin_unlock_irqrestore(&cache->lock, flags); |
591 | ||
592 | return r; | |
593 | } | |
594 | ||
595 | static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b) | |
596 | { | |
597 | int r; | |
598 | unsigned long flags; | |
599 | ||
600 | spin_lock_irqsave(&cache->lock, flags); | |
1bad9bc4 JT |
601 | r = test_bit(from_dblock(oblock_to_dblock(cache, b)), |
602 | cache->discard_bitset); | |
c6b4fcba JT |
603 | spin_unlock_irqrestore(&cache->lock, flags); |
604 | ||
605 | return r; | |
606 | } | |
607 | ||
608 | /*----------------------------------------------------------------*/ | |
609 | ||
610 | static void load_stats(struct cache *cache) | |
611 | { | |
612 | struct dm_cache_statistics stats; | |
613 | ||
614 | dm_cache_metadata_get_stats(cache->cmd, &stats); | |
615 | atomic_set(&cache->stats.read_hit, stats.read_hits); | |
616 | atomic_set(&cache->stats.read_miss, stats.read_misses); | |
617 | atomic_set(&cache->stats.write_hit, stats.write_hits); | |
618 | atomic_set(&cache->stats.write_miss, stats.write_misses); | |
619 | } | |
620 | ||
621 | static void save_stats(struct cache *cache) | |
622 | { | |
623 | struct dm_cache_statistics stats; | |
624 | ||
625 | stats.read_hits = atomic_read(&cache->stats.read_hit); | |
626 | stats.read_misses = atomic_read(&cache->stats.read_miss); | |
627 | stats.write_hits = atomic_read(&cache->stats.write_hit); | |
628 | stats.write_misses = atomic_read(&cache->stats.write_miss); | |
629 | ||
630 | dm_cache_metadata_set_stats(cache->cmd, &stats); | |
631 | } | |
632 | ||
633 | /*---------------------------------------------------------------- | |
634 | * Per bio data | |
635 | *--------------------------------------------------------------*/ | |
19b0092e MS |
636 | |
637 | /* | |
638 | * If using writeback, leave out struct per_bio_data's writethrough fields. | |
639 | */ | |
640 | #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache)) | |
641 | #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data)) | |
642 | ||
2ee57d58 JT |
643 | static bool writethrough_mode(struct cache_features *f) |
644 | { | |
645 | return f->io_mode == CM_IO_WRITETHROUGH; | |
646 | } | |
647 | ||
648 | static bool writeback_mode(struct cache_features *f) | |
649 | { | |
650 | return f->io_mode == CM_IO_WRITEBACK; | |
651 | } | |
652 | ||
653 | static bool passthrough_mode(struct cache_features *f) | |
654 | { | |
655 | return f->io_mode == CM_IO_PASSTHROUGH; | |
656 | } | |
657 | ||
19b0092e MS |
658 | static size_t get_per_bio_data_size(struct cache *cache) |
659 | { | |
2ee57d58 | 660 | return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB; |
19b0092e MS |
661 | } |
662 | ||
663 | static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size) | |
c6b4fcba | 664 | { |
19b0092e | 665 | struct per_bio_data *pb = dm_per_bio_data(bio, data_size); |
c6b4fcba JT |
666 | BUG_ON(!pb); |
667 | return pb; | |
668 | } | |
669 | ||
19b0092e | 670 | static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size) |
c6b4fcba | 671 | { |
19b0092e | 672 | struct per_bio_data *pb = get_per_bio_data(bio, data_size); |
c6b4fcba JT |
673 | |
674 | pb->tick = false; | |
675 | pb->req_nr = dm_bio_get_target_bio_nr(bio); | |
676 | pb->all_io_entry = NULL; | |
677 | ||
678 | return pb; | |
679 | } | |
680 | ||
681 | /*---------------------------------------------------------------- | |
682 | * Remapping | |
683 | *--------------------------------------------------------------*/ | |
684 | static void remap_to_origin(struct cache *cache, struct bio *bio) | |
685 | { | |
686 | bio->bi_bdev = cache->origin_dev->bdev; | |
687 | } | |
688 | ||
689 | static void remap_to_cache(struct cache *cache, struct bio *bio, | |
690 | dm_cblock_t cblock) | |
691 | { | |
4f024f37 | 692 | sector_t bi_sector = bio->bi_iter.bi_sector; |
e0d849fa | 693 | sector_t block = from_cblock(cblock); |
c6b4fcba JT |
694 | |
695 | bio->bi_bdev = cache->cache_dev->bdev; | |
696 | if (!block_size_is_power_of_two(cache)) | |
4f024f37 | 697 | bio->bi_iter.bi_sector = |
e0d849fa | 698 | (block * cache->sectors_per_block) + |
4f024f37 | 699 | sector_div(bi_sector, cache->sectors_per_block); |
c6b4fcba | 700 | else |
4f024f37 | 701 | bio->bi_iter.bi_sector = |
e0d849fa | 702 | (block << cache->sectors_per_block_shift) | |
4f024f37 | 703 | (bi_sector & (cache->sectors_per_block - 1)); |
c6b4fcba JT |
704 | } |
705 | ||
706 | static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio) | |
707 | { | |
708 | unsigned long flags; | |
19b0092e MS |
709 | size_t pb_data_size = get_per_bio_data_size(cache); |
710 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); | |
c6b4fcba JT |
711 | |
712 | spin_lock_irqsave(&cache->lock, flags); | |
713 | if (cache->need_tick_bio && | |
714 | !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) { | |
715 | pb->tick = true; | |
716 | cache->need_tick_bio = false; | |
717 | } | |
718 | spin_unlock_irqrestore(&cache->lock, flags); | |
719 | } | |
720 | ||
721 | static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio, | |
722 | dm_oblock_t oblock) | |
723 | { | |
724 | check_if_tick_bio_needed(cache, bio); | |
725 | remap_to_origin(cache, bio); | |
726 | if (bio_data_dir(bio) == WRITE) | |
1bad9bc4 | 727 | clear_discard(cache, oblock_to_dblock(cache, oblock)); |
c6b4fcba JT |
728 | } |
729 | ||
730 | static void remap_to_cache_dirty(struct cache *cache, struct bio *bio, | |
731 | dm_oblock_t oblock, dm_cblock_t cblock) | |
732 | { | |
f8e5f01a | 733 | check_if_tick_bio_needed(cache, bio); |
c6b4fcba JT |
734 | remap_to_cache(cache, bio, cblock); |
735 | if (bio_data_dir(bio) == WRITE) { | |
736 | set_dirty(cache, oblock, cblock); | |
1bad9bc4 | 737 | clear_discard(cache, oblock_to_dblock(cache, oblock)); |
c6b4fcba JT |
738 | } |
739 | } | |
740 | ||
741 | static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio) | |
742 | { | |
4f024f37 | 743 | sector_t block_nr = bio->bi_iter.bi_sector; |
c6b4fcba JT |
744 | |
745 | if (!block_size_is_power_of_two(cache)) | |
746 | (void) sector_div(block_nr, cache->sectors_per_block); | |
747 | else | |
748 | block_nr >>= cache->sectors_per_block_shift; | |
749 | ||
750 | return to_oblock(block_nr); | |
751 | } | |
752 | ||
753 | static int bio_triggers_commit(struct cache *cache, struct bio *bio) | |
754 | { | |
755 | return bio->bi_rw & (REQ_FLUSH | REQ_FUA); | |
756 | } | |
757 | ||
8c081b52 JT |
758 | /* |
759 | * You must increment the deferred set whilst the prison cell is held. To | |
760 | * encourage this, we ask for 'cell' to be passed in. | |
761 | */ | |
762 | static void inc_ds(struct cache *cache, struct bio *bio, | |
763 | struct dm_bio_prison_cell *cell) | |
764 | { | |
765 | size_t pb_data_size = get_per_bio_data_size(cache); | |
766 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); | |
767 | ||
768 | BUG_ON(!cell); | |
769 | BUG_ON(pb->all_io_entry); | |
770 | ||
771 | pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds); | |
772 | } | |
773 | ||
c6b4fcba JT |
774 | static void issue(struct cache *cache, struct bio *bio) |
775 | { | |
776 | unsigned long flags; | |
777 | ||
778 | if (!bio_triggers_commit(cache, bio)) { | |
779 | generic_make_request(bio); | |
780 | return; | |
781 | } | |
782 | ||
783 | /* | |
784 | * Batch together any bios that trigger commits and then issue a | |
785 | * single commit for them in do_worker(). | |
786 | */ | |
787 | spin_lock_irqsave(&cache->lock, flags); | |
788 | cache->commit_requested = true; | |
789 | bio_list_add(&cache->deferred_flush_bios, bio); | |
790 | spin_unlock_irqrestore(&cache->lock, flags); | |
791 | } | |
792 | ||
8c081b52 JT |
793 | static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell) |
794 | { | |
795 | inc_ds(cache, bio, cell); | |
796 | issue(cache, bio); | |
797 | } | |
798 | ||
e2e74d61 JT |
799 | static void defer_writethrough_bio(struct cache *cache, struct bio *bio) |
800 | { | |
801 | unsigned long flags; | |
802 | ||
803 | spin_lock_irqsave(&cache->lock, flags); | |
804 | bio_list_add(&cache->deferred_writethrough_bios, bio); | |
805 | spin_unlock_irqrestore(&cache->lock, flags); | |
806 | ||
807 | wake_worker(cache); | |
808 | } | |
809 | ||
810 | static void writethrough_endio(struct bio *bio, int err) | |
811 | { | |
19b0092e | 812 | struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT); |
c9d28d5d JT |
813 | |
814 | dm_unhook_bio(&pb->hook_info, bio); | |
e2e74d61 JT |
815 | |
816 | if (err) { | |
817 | bio_endio(bio, err); | |
818 | return; | |
819 | } | |
820 | ||
b844fe69 | 821 | dm_bio_restore(&pb->bio_details, bio); |
e2e74d61 JT |
822 | remap_to_cache(pb->cache, bio, pb->cblock); |
823 | ||
824 | /* | |
825 | * We can't issue this bio directly, since we're in interrupt | |
aeed1420 | 826 | * context. So it gets put on a bio list for processing by the |
e2e74d61 JT |
827 | * worker thread. |
828 | */ | |
829 | defer_writethrough_bio(pb->cache, bio); | |
830 | } | |
831 | ||
832 | /* | |
833 | * When running in writethrough mode we need to send writes to clean blocks | |
834 | * to both the cache and origin devices. In future we'd like to clone the | |
835 | * bio and send them in parallel, but for now we're doing them in | |
836 | * series as this is easier. | |
837 | */ | |
838 | static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio, | |
839 | dm_oblock_t oblock, dm_cblock_t cblock) | |
840 | { | |
19b0092e | 841 | struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT); |
e2e74d61 JT |
842 | |
843 | pb->cache = cache; | |
844 | pb->cblock = cblock; | |
c9d28d5d | 845 | dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL); |
b844fe69 | 846 | dm_bio_record(&pb->bio_details, bio); |
e2e74d61 JT |
847 | |
848 | remap_to_origin_clear_discard(pb->cache, bio, oblock); | |
849 | } | |
850 | ||
c6b4fcba JT |
851 | /*---------------------------------------------------------------- |
852 | * Migration processing | |
853 | * | |
854 | * Migration covers moving data from the origin device to the cache, or | |
855 | * vice versa. | |
856 | *--------------------------------------------------------------*/ | |
857 | static void free_migration(struct dm_cache_migration *mg) | |
858 | { | |
859 | mempool_free(mg, mg->cache->migration_pool); | |
860 | } | |
861 | ||
862 | static void inc_nr_migrations(struct cache *cache) | |
863 | { | |
864 | atomic_inc(&cache->nr_migrations); | |
865 | } | |
866 | ||
867 | static void dec_nr_migrations(struct cache *cache) | |
868 | { | |
869 | atomic_dec(&cache->nr_migrations); | |
870 | ||
871 | /* | |
872 | * Wake the worker in case we're suspending the target. | |
873 | */ | |
874 | wake_up(&cache->migration_wait); | |
875 | } | |
876 | ||
877 | static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, | |
878 | bool holder) | |
879 | { | |
880 | (holder ? dm_cell_release : dm_cell_release_no_holder) | |
881 | (cache->prison, cell, &cache->deferred_bios); | |
882 | free_prison_cell(cache, cell); | |
883 | } | |
884 | ||
885 | static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, | |
886 | bool holder) | |
887 | { | |
888 | unsigned long flags; | |
889 | ||
890 | spin_lock_irqsave(&cache->lock, flags); | |
891 | __cell_defer(cache, cell, holder); | |
892 | spin_unlock_irqrestore(&cache->lock, flags); | |
893 | ||
894 | wake_worker(cache); | |
895 | } | |
896 | ||
897 | static void cleanup_migration(struct dm_cache_migration *mg) | |
898 | { | |
66cb1910 | 899 | struct cache *cache = mg->cache; |
c6b4fcba | 900 | free_migration(mg); |
66cb1910 | 901 | dec_nr_migrations(cache); |
c6b4fcba JT |
902 | } |
903 | ||
904 | static void migration_failure(struct dm_cache_migration *mg) | |
905 | { | |
906 | struct cache *cache = mg->cache; | |
907 | ||
908 | if (mg->writeback) { | |
909 | DMWARN_LIMIT("writeback failed; couldn't copy block"); | |
910 | set_dirty(cache, mg->old_oblock, mg->cblock); | |
911 | cell_defer(cache, mg->old_ocell, false); | |
912 | ||
913 | } else if (mg->demote) { | |
914 | DMWARN_LIMIT("demotion failed; couldn't copy block"); | |
915 | policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock); | |
916 | ||
80f659f3 | 917 | cell_defer(cache, mg->old_ocell, mg->promote ? false : true); |
c6b4fcba | 918 | if (mg->promote) |
80f659f3 | 919 | cell_defer(cache, mg->new_ocell, true); |
c6b4fcba JT |
920 | } else { |
921 | DMWARN_LIMIT("promotion failed; couldn't copy block"); | |
922 | policy_remove_mapping(cache->policy, mg->new_oblock); | |
80f659f3 | 923 | cell_defer(cache, mg->new_ocell, true); |
c6b4fcba JT |
924 | } |
925 | ||
926 | cleanup_migration(mg); | |
927 | } | |
928 | ||
929 | static void migration_success_pre_commit(struct dm_cache_migration *mg) | |
930 | { | |
931 | unsigned long flags; | |
932 | struct cache *cache = mg->cache; | |
933 | ||
934 | if (mg->writeback) { | |
c6b4fcba | 935 | clear_dirty(cache, mg->old_oblock, mg->cblock); |
40aa978e | 936 | cell_defer(cache, mg->old_ocell, false); |
c6b4fcba JT |
937 | cleanup_migration(mg); |
938 | return; | |
939 | ||
940 | } else if (mg->demote) { | |
941 | if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) { | |
942 | DMWARN_LIMIT("demotion failed; couldn't update on disk metadata"); | |
943 | policy_force_mapping(cache->policy, mg->new_oblock, | |
944 | mg->old_oblock); | |
945 | if (mg->promote) | |
946 | cell_defer(cache, mg->new_ocell, true); | |
947 | cleanup_migration(mg); | |
948 | return; | |
949 | } | |
950 | } else { | |
951 | if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) { | |
952 | DMWARN_LIMIT("promotion failed; couldn't update on disk metadata"); | |
953 | policy_remove_mapping(cache->policy, mg->new_oblock); | |
954 | cleanup_migration(mg); | |
955 | return; | |
956 | } | |
957 | } | |
958 | ||
959 | spin_lock_irqsave(&cache->lock, flags); | |
960 | list_add_tail(&mg->list, &cache->need_commit_migrations); | |
961 | cache->commit_requested = true; | |
962 | spin_unlock_irqrestore(&cache->lock, flags); | |
963 | } | |
964 | ||
965 | static void migration_success_post_commit(struct dm_cache_migration *mg) | |
966 | { | |
967 | unsigned long flags; | |
968 | struct cache *cache = mg->cache; | |
969 | ||
970 | if (mg->writeback) { | |
971 | DMWARN("writeback unexpectedly triggered commit"); | |
972 | return; | |
973 | ||
974 | } else if (mg->demote) { | |
80f659f3 | 975 | cell_defer(cache, mg->old_ocell, mg->promote ? false : true); |
c6b4fcba JT |
976 | |
977 | if (mg->promote) { | |
978 | mg->demote = false; | |
979 | ||
980 | spin_lock_irqsave(&cache->lock, flags); | |
981 | list_add_tail(&mg->list, &cache->quiesced_migrations); | |
982 | spin_unlock_irqrestore(&cache->lock, flags); | |
983 | ||
65790ff9 JT |
984 | } else { |
985 | if (mg->invalidate) | |
986 | policy_remove_mapping(cache->policy, mg->old_oblock); | |
c6b4fcba | 987 | cleanup_migration(mg); |
65790ff9 | 988 | } |
c6b4fcba JT |
989 | |
990 | } else { | |
40aa978e | 991 | clear_dirty(cache, mg->new_oblock, mg->cblock); |
c9d28d5d JT |
992 | if (mg->requeue_holder) |
993 | cell_defer(cache, mg->new_ocell, true); | |
994 | else { | |
995 | bio_endio(mg->new_ocell->holder, 0); | |
996 | cell_defer(cache, mg->new_ocell, false); | |
997 | } | |
c6b4fcba JT |
998 | cleanup_migration(mg); |
999 | } | |
1000 | } | |
1001 | ||
1002 | static void copy_complete(int read_err, unsigned long write_err, void *context) | |
1003 | { | |
1004 | unsigned long flags; | |
1005 | struct dm_cache_migration *mg = (struct dm_cache_migration *) context; | |
1006 | struct cache *cache = mg->cache; | |
1007 | ||
1008 | if (read_err || write_err) | |
1009 | mg->err = true; | |
1010 | ||
1011 | spin_lock_irqsave(&cache->lock, flags); | |
1012 | list_add_tail(&mg->list, &cache->completed_migrations); | |
1013 | spin_unlock_irqrestore(&cache->lock, flags); | |
1014 | ||
1015 | wake_worker(cache); | |
1016 | } | |
1017 | ||
7ae34e77 | 1018 | static void issue_copy(struct dm_cache_migration *mg) |
c6b4fcba JT |
1019 | { |
1020 | int r; | |
1021 | struct dm_io_region o_region, c_region; | |
1022 | struct cache *cache = mg->cache; | |
8b9d9666 | 1023 | sector_t cblock = from_cblock(mg->cblock); |
c6b4fcba JT |
1024 | |
1025 | o_region.bdev = cache->origin_dev->bdev; | |
1026 | o_region.count = cache->sectors_per_block; | |
1027 | ||
1028 | c_region.bdev = cache->cache_dev->bdev; | |
8b9d9666 | 1029 | c_region.sector = cblock * cache->sectors_per_block; |
c6b4fcba JT |
1030 | c_region.count = cache->sectors_per_block; |
1031 | ||
1032 | if (mg->writeback || mg->demote) { | |
1033 | /* demote */ | |
1034 | o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block; | |
1035 | r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg); | |
1036 | } else { | |
1037 | /* promote */ | |
1038 | o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block; | |
1039 | r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg); | |
1040 | } | |
1041 | ||
2c2263c9 HM |
1042 | if (r < 0) { |
1043 | DMERR_LIMIT("issuing migration failed"); | |
c6b4fcba | 1044 | migration_failure(mg); |
2c2263c9 | 1045 | } |
c6b4fcba JT |
1046 | } |
1047 | ||
c9d28d5d JT |
1048 | static void overwrite_endio(struct bio *bio, int err) |
1049 | { | |
1050 | struct dm_cache_migration *mg = bio->bi_private; | |
1051 | struct cache *cache = mg->cache; | |
1052 | size_t pb_data_size = get_per_bio_data_size(cache); | |
1053 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); | |
1054 | unsigned long flags; | |
1055 | ||
80ae49aa MS |
1056 | dm_unhook_bio(&pb->hook_info, bio); |
1057 | ||
c9d28d5d JT |
1058 | if (err) |
1059 | mg->err = true; | |
1060 | ||
80ae49aa MS |
1061 | mg->requeue_holder = false; |
1062 | ||
c9d28d5d JT |
1063 | spin_lock_irqsave(&cache->lock, flags); |
1064 | list_add_tail(&mg->list, &cache->completed_migrations); | |
c9d28d5d JT |
1065 | spin_unlock_irqrestore(&cache->lock, flags); |
1066 | ||
1067 | wake_worker(cache); | |
1068 | } | |
1069 | ||
1070 | static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio) | |
1071 | { | |
1072 | size_t pb_data_size = get_per_bio_data_size(mg->cache); | |
1073 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); | |
1074 | ||
1075 | dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg); | |
1076 | remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock); | |
8c081b52 JT |
1077 | |
1078 | /* | |
1079 | * No need to inc_ds() here, since the cell will be held for the | |
1080 | * duration of the io. | |
1081 | */ | |
c9d28d5d JT |
1082 | generic_make_request(bio); |
1083 | } | |
1084 | ||
1085 | static bool bio_writes_complete_block(struct cache *cache, struct bio *bio) | |
1086 | { | |
1087 | return (bio_data_dir(bio) == WRITE) && | |
4f024f37 | 1088 | (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT)); |
c9d28d5d JT |
1089 | } |
1090 | ||
c6b4fcba JT |
1091 | static void avoid_copy(struct dm_cache_migration *mg) |
1092 | { | |
1093 | atomic_inc(&mg->cache->stats.copies_avoided); | |
1094 | migration_success_pre_commit(mg); | |
1095 | } | |
1096 | ||
7ae34e77 JT |
1097 | static void calc_discard_block_range(struct cache *cache, struct bio *bio, |
1098 | dm_dblock_t *b, dm_dblock_t *e) | |
1099 | { | |
1100 | sector_t sb = bio->bi_iter.bi_sector; | |
1101 | sector_t se = bio_end_sector(bio); | |
1102 | ||
1103 | *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size)); | |
1104 | ||
1105 | if (se - sb < cache->discard_block_size) | |
1106 | *e = *b; | |
1107 | else | |
1108 | *e = to_dblock(block_div(se, cache->discard_block_size)); | |
1109 | } | |
1110 | ||
1111 | static void issue_discard(struct dm_cache_migration *mg) | |
1112 | { | |
1113 | dm_dblock_t b, e; | |
1114 | struct bio *bio = mg->new_ocell->holder; | |
1115 | ||
1116 | calc_discard_block_range(mg->cache, bio, &b, &e); | |
1117 | while (b != e) { | |
1118 | set_discard(mg->cache, b); | |
1119 | b = to_dblock(from_dblock(b) + 1); | |
1120 | } | |
1121 | ||
1122 | bio_endio(bio, 0); | |
1123 | cell_defer(mg->cache, mg->new_ocell, false); | |
1124 | free_migration(mg); | |
1125 | } | |
1126 | ||
1127 | static void issue_copy_or_discard(struct dm_cache_migration *mg) | |
c6b4fcba JT |
1128 | { |
1129 | bool avoid; | |
1130 | struct cache *cache = mg->cache; | |
1131 | ||
7ae34e77 JT |
1132 | if (mg->discard) { |
1133 | issue_discard(mg); | |
1134 | return; | |
1135 | } | |
1136 | ||
c6b4fcba JT |
1137 | if (mg->writeback || mg->demote) |
1138 | avoid = !is_dirty(cache, mg->cblock) || | |
1139 | is_discarded_oblock(cache, mg->old_oblock); | |
c9d28d5d JT |
1140 | else { |
1141 | struct bio *bio = mg->new_ocell->holder; | |
1142 | ||
c6b4fcba JT |
1143 | avoid = is_discarded_oblock(cache, mg->new_oblock); |
1144 | ||
c9d28d5d JT |
1145 | if (!avoid && bio_writes_complete_block(cache, bio)) { |
1146 | issue_overwrite(mg, bio); | |
1147 | return; | |
1148 | } | |
1149 | } | |
1150 | ||
7ae34e77 | 1151 | avoid ? avoid_copy(mg) : issue_copy(mg); |
c6b4fcba JT |
1152 | } |
1153 | ||
1154 | static void complete_migration(struct dm_cache_migration *mg) | |
1155 | { | |
1156 | if (mg->err) | |
1157 | migration_failure(mg); | |
1158 | else | |
1159 | migration_success_pre_commit(mg); | |
1160 | } | |
1161 | ||
1162 | static void process_migrations(struct cache *cache, struct list_head *head, | |
1163 | void (*fn)(struct dm_cache_migration *)) | |
1164 | { | |
1165 | unsigned long flags; | |
1166 | struct list_head list; | |
1167 | struct dm_cache_migration *mg, *tmp; | |
1168 | ||
1169 | INIT_LIST_HEAD(&list); | |
1170 | spin_lock_irqsave(&cache->lock, flags); | |
1171 | list_splice_init(head, &list); | |
1172 | spin_unlock_irqrestore(&cache->lock, flags); | |
1173 | ||
1174 | list_for_each_entry_safe(mg, tmp, &list, list) | |
1175 | fn(mg); | |
1176 | } | |
1177 | ||
1178 | static void __queue_quiesced_migration(struct dm_cache_migration *mg) | |
1179 | { | |
1180 | list_add_tail(&mg->list, &mg->cache->quiesced_migrations); | |
1181 | } | |
1182 | ||
1183 | static void queue_quiesced_migration(struct dm_cache_migration *mg) | |
1184 | { | |
1185 | unsigned long flags; | |
1186 | struct cache *cache = mg->cache; | |
1187 | ||
1188 | spin_lock_irqsave(&cache->lock, flags); | |
1189 | __queue_quiesced_migration(mg); | |
1190 | spin_unlock_irqrestore(&cache->lock, flags); | |
1191 | ||
1192 | wake_worker(cache); | |
1193 | } | |
1194 | ||
1195 | static void queue_quiesced_migrations(struct cache *cache, struct list_head *work) | |
1196 | { | |
1197 | unsigned long flags; | |
1198 | struct dm_cache_migration *mg, *tmp; | |
1199 | ||
1200 | spin_lock_irqsave(&cache->lock, flags); | |
1201 | list_for_each_entry_safe(mg, tmp, work, list) | |
1202 | __queue_quiesced_migration(mg); | |
1203 | spin_unlock_irqrestore(&cache->lock, flags); | |
1204 | ||
1205 | wake_worker(cache); | |
1206 | } | |
1207 | ||
1208 | static void check_for_quiesced_migrations(struct cache *cache, | |
1209 | struct per_bio_data *pb) | |
1210 | { | |
1211 | struct list_head work; | |
1212 | ||
1213 | if (!pb->all_io_entry) | |
1214 | return; | |
1215 | ||
1216 | INIT_LIST_HEAD(&work); | |
8c081b52 | 1217 | dm_deferred_entry_dec(pb->all_io_entry, &work); |
c6b4fcba JT |
1218 | |
1219 | if (!list_empty(&work)) | |
1220 | queue_quiesced_migrations(cache, &work); | |
1221 | } | |
1222 | ||
1223 | static void quiesce_migration(struct dm_cache_migration *mg) | |
1224 | { | |
1225 | if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list)) | |
1226 | queue_quiesced_migration(mg); | |
1227 | } | |
1228 | ||
1229 | static void promote(struct cache *cache, struct prealloc *structs, | |
1230 | dm_oblock_t oblock, dm_cblock_t cblock, | |
1231 | struct dm_bio_prison_cell *cell) | |
1232 | { | |
1233 | struct dm_cache_migration *mg = prealloc_get_migration(structs); | |
1234 | ||
1235 | mg->err = false; | |
7ae34e77 | 1236 | mg->discard = false; |
c6b4fcba JT |
1237 | mg->writeback = false; |
1238 | mg->demote = false; | |
1239 | mg->promote = true; | |
c9d28d5d | 1240 | mg->requeue_holder = true; |
65790ff9 | 1241 | mg->invalidate = false; |
c6b4fcba JT |
1242 | mg->cache = cache; |
1243 | mg->new_oblock = oblock; | |
1244 | mg->cblock = cblock; | |
1245 | mg->old_ocell = NULL; | |
1246 | mg->new_ocell = cell; | |
1247 | mg->start_jiffies = jiffies; | |
1248 | ||
1249 | inc_nr_migrations(cache); | |
1250 | quiesce_migration(mg); | |
1251 | } | |
1252 | ||
1253 | static void writeback(struct cache *cache, struct prealloc *structs, | |
1254 | dm_oblock_t oblock, dm_cblock_t cblock, | |
1255 | struct dm_bio_prison_cell *cell) | |
1256 | { | |
1257 | struct dm_cache_migration *mg = prealloc_get_migration(structs); | |
1258 | ||
1259 | mg->err = false; | |
7ae34e77 | 1260 | mg->discard = false; |
c6b4fcba JT |
1261 | mg->writeback = true; |
1262 | mg->demote = false; | |
1263 | mg->promote = false; | |
c9d28d5d | 1264 | mg->requeue_holder = true; |
65790ff9 | 1265 | mg->invalidate = false; |
c6b4fcba JT |
1266 | mg->cache = cache; |
1267 | mg->old_oblock = oblock; | |
1268 | mg->cblock = cblock; | |
1269 | mg->old_ocell = cell; | |
1270 | mg->new_ocell = NULL; | |
1271 | mg->start_jiffies = jiffies; | |
1272 | ||
1273 | inc_nr_migrations(cache); | |
1274 | quiesce_migration(mg); | |
1275 | } | |
1276 | ||
1277 | static void demote_then_promote(struct cache *cache, struct prealloc *structs, | |
1278 | dm_oblock_t old_oblock, dm_oblock_t new_oblock, | |
1279 | dm_cblock_t cblock, | |
1280 | struct dm_bio_prison_cell *old_ocell, | |
1281 | struct dm_bio_prison_cell *new_ocell) | |
1282 | { | |
1283 | struct dm_cache_migration *mg = prealloc_get_migration(structs); | |
1284 | ||
1285 | mg->err = false; | |
7ae34e77 | 1286 | mg->discard = false; |
c6b4fcba JT |
1287 | mg->writeback = false; |
1288 | mg->demote = true; | |
1289 | mg->promote = true; | |
c9d28d5d | 1290 | mg->requeue_holder = true; |
65790ff9 | 1291 | mg->invalidate = false; |
c6b4fcba JT |
1292 | mg->cache = cache; |
1293 | mg->old_oblock = old_oblock; | |
1294 | mg->new_oblock = new_oblock; | |
1295 | mg->cblock = cblock; | |
1296 | mg->old_ocell = old_ocell; | |
1297 | mg->new_ocell = new_ocell; | |
1298 | mg->start_jiffies = jiffies; | |
1299 | ||
1300 | inc_nr_migrations(cache); | |
1301 | quiesce_migration(mg); | |
1302 | } | |
1303 | ||
2ee57d58 JT |
1304 | /* |
1305 | * Invalidate a cache entry. No writeback occurs; any changes in the cache | |
1306 | * block are thrown away. | |
1307 | */ | |
1308 | static void invalidate(struct cache *cache, struct prealloc *structs, | |
1309 | dm_oblock_t oblock, dm_cblock_t cblock, | |
1310 | struct dm_bio_prison_cell *cell) | |
1311 | { | |
1312 | struct dm_cache_migration *mg = prealloc_get_migration(structs); | |
1313 | ||
1314 | mg->err = false; | |
7ae34e77 | 1315 | mg->discard = false; |
2ee57d58 JT |
1316 | mg->writeback = false; |
1317 | mg->demote = true; | |
1318 | mg->promote = false; | |
1319 | mg->requeue_holder = true; | |
65790ff9 | 1320 | mg->invalidate = true; |
2ee57d58 JT |
1321 | mg->cache = cache; |
1322 | mg->old_oblock = oblock; | |
1323 | mg->cblock = cblock; | |
1324 | mg->old_ocell = cell; | |
1325 | mg->new_ocell = NULL; | |
1326 | mg->start_jiffies = jiffies; | |
1327 | ||
1328 | inc_nr_migrations(cache); | |
1329 | quiesce_migration(mg); | |
1330 | } | |
1331 | ||
7ae34e77 JT |
1332 | static void discard(struct cache *cache, struct prealloc *structs, |
1333 | struct dm_bio_prison_cell *cell) | |
1334 | { | |
1335 | struct dm_cache_migration *mg = prealloc_get_migration(structs); | |
1336 | ||
1337 | mg->err = false; | |
1338 | mg->discard = true; | |
1339 | mg->writeback = false; | |
1340 | mg->demote = false; | |
1341 | mg->promote = false; | |
1342 | mg->requeue_holder = false; | |
1343 | mg->invalidate = false; | |
1344 | mg->cache = cache; | |
1345 | mg->old_ocell = NULL; | |
1346 | mg->new_ocell = cell; | |
1347 | mg->start_jiffies = jiffies; | |
1348 | ||
1349 | quiesce_migration(mg); | |
1350 | } | |
1351 | ||
c6b4fcba JT |
1352 | /*---------------------------------------------------------------- |
1353 | * bio processing | |
1354 | *--------------------------------------------------------------*/ | |
1355 | static void defer_bio(struct cache *cache, struct bio *bio) | |
1356 | { | |
1357 | unsigned long flags; | |
1358 | ||
1359 | spin_lock_irqsave(&cache->lock, flags); | |
1360 | bio_list_add(&cache->deferred_bios, bio); | |
1361 | spin_unlock_irqrestore(&cache->lock, flags); | |
1362 | ||
1363 | wake_worker(cache); | |
1364 | } | |
1365 | ||
1366 | static void process_flush_bio(struct cache *cache, struct bio *bio) | |
1367 | { | |
19b0092e MS |
1368 | size_t pb_data_size = get_per_bio_data_size(cache); |
1369 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); | |
c6b4fcba | 1370 | |
4f024f37 | 1371 | BUG_ON(bio->bi_iter.bi_size); |
c6b4fcba JT |
1372 | if (!pb->req_nr) |
1373 | remap_to_origin(cache, bio); | |
1374 | else | |
1375 | remap_to_cache(cache, bio, 0); | |
1376 | ||
8c081b52 JT |
1377 | /* |
1378 | * REQ_FLUSH is not directed at any particular block so we don't | |
1379 | * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH | |
1380 | * by dm-core. | |
1381 | */ | |
c6b4fcba JT |
1382 | issue(cache, bio); |
1383 | } | |
1384 | ||
7ae34e77 JT |
1385 | static void process_discard_bio(struct cache *cache, struct prealloc *structs, |
1386 | struct bio *bio) | |
c6b4fcba | 1387 | { |
7ae34e77 JT |
1388 | int r; |
1389 | dm_dblock_t b, e; | |
1390 | struct dm_bio_prison_cell *cell_prealloc, *new_ocell; | |
c6b4fcba | 1391 | |
7ae34e77 JT |
1392 | calc_discard_block_range(cache, bio, &b, &e); |
1393 | if (b == e) { | |
1394 | bio_endio(bio, 0); | |
1395 | return; | |
1396 | } | |
c6b4fcba | 1397 | |
7ae34e77 JT |
1398 | cell_prealloc = prealloc_get_cell(structs); |
1399 | r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc, | |
1400 | (cell_free_fn) prealloc_put_cell, | |
1401 | structs, &new_ocell); | |
1402 | if (r > 0) | |
1403 | return; | |
c6b4fcba | 1404 | |
7ae34e77 | 1405 | discard(cache, structs, new_ocell); |
c6b4fcba JT |
1406 | } |
1407 | ||
1408 | static bool spare_migration_bandwidth(struct cache *cache) | |
1409 | { | |
1410 | sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) * | |
1411 | cache->sectors_per_block; | |
1412 | return current_volume < cache->migration_threshold; | |
1413 | } | |
1414 | ||
c6b4fcba JT |
1415 | static void inc_hit_counter(struct cache *cache, struct bio *bio) |
1416 | { | |
1417 | atomic_inc(bio_data_dir(bio) == READ ? | |
1418 | &cache->stats.read_hit : &cache->stats.write_hit); | |
1419 | } | |
1420 | ||
1421 | static void inc_miss_counter(struct cache *cache, struct bio *bio) | |
1422 | { | |
1423 | atomic_inc(bio_data_dir(bio) == READ ? | |
1424 | &cache->stats.read_miss : &cache->stats.write_miss); | |
1425 | } | |
1426 | ||
1427 | static void process_bio(struct cache *cache, struct prealloc *structs, | |
1428 | struct bio *bio) | |
1429 | { | |
1430 | int r; | |
1431 | bool release_cell = true; | |
1432 | dm_oblock_t block = get_bio_block(cache, bio); | |
1433 | struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell; | |
1434 | struct policy_result lookup_result; | |
2ee57d58 | 1435 | bool passthrough = passthrough_mode(&cache->features); |
43c32bf2 | 1436 | bool discarded_block, can_migrate; |
c6b4fcba JT |
1437 | |
1438 | /* | |
1439 | * Check to see if that block is currently migrating. | |
1440 | */ | |
1441 | cell_prealloc = prealloc_get_cell(structs); | |
1442 | r = bio_detain(cache, block, bio, cell_prealloc, | |
1443 | (cell_free_fn) prealloc_put_cell, | |
1444 | structs, &new_ocell); | |
1445 | if (r > 0) | |
1446 | return; | |
1447 | ||
43c32bf2 JT |
1448 | discarded_block = is_discarded_oblock(cache, block); |
1449 | can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache)); | |
1450 | ||
c6b4fcba JT |
1451 | r = policy_map(cache->policy, block, true, can_migrate, discarded_block, |
1452 | bio, &lookup_result); | |
1453 | ||
1454 | if (r == -EWOULDBLOCK) | |
1455 | /* migration has been denied */ | |
1456 | lookup_result.op = POLICY_MISS; | |
1457 | ||
1458 | switch (lookup_result.op) { | |
1459 | case POLICY_HIT: | |
2ee57d58 JT |
1460 | if (passthrough) { |
1461 | inc_miss_counter(cache, bio); | |
c6b4fcba | 1462 | |
2ee57d58 JT |
1463 | /* |
1464 | * Passthrough always maps to the origin, | |
1465 | * invalidating any cache blocks that are written | |
1466 | * to. | |
1467 | */ | |
1468 | ||
1469 | if (bio_data_dir(bio) == WRITE) { | |
1470 | atomic_inc(&cache->stats.demotion); | |
1471 | invalidate(cache, structs, block, lookup_result.cblock, new_ocell); | |
1472 | release_cell = false; | |
1473 | ||
1474 | } else { | |
1475 | /* FIXME: factor out issue_origin() */ | |
2ee57d58 | 1476 | remap_to_origin_clear_discard(cache, bio, block); |
8c081b52 | 1477 | inc_and_issue(cache, bio, new_ocell); |
2ee57d58 JT |
1478 | } |
1479 | } else { | |
1480 | inc_hit_counter(cache, bio); | |
1481 | ||
1482 | if (bio_data_dir(bio) == WRITE && | |
1483 | writethrough_mode(&cache->features) && | |
1484 | !is_dirty(cache, lookup_result.cblock)) { | |
2ee57d58 | 1485 | remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock); |
8c081b52 JT |
1486 | inc_and_issue(cache, bio, new_ocell); |
1487 | ||
1488 | } else { | |
1489 | remap_to_cache_dirty(cache, bio, block, lookup_result.cblock); | |
1490 | inc_and_issue(cache, bio, new_ocell); | |
1491 | } | |
2ee57d58 | 1492 | } |
c6b4fcba | 1493 | |
c6b4fcba JT |
1494 | break; |
1495 | ||
1496 | case POLICY_MISS: | |
1497 | inc_miss_counter(cache, bio); | |
e2e74d61 | 1498 | remap_to_origin_clear_discard(cache, bio, block); |
8c081b52 | 1499 | inc_and_issue(cache, bio, new_ocell); |
c6b4fcba JT |
1500 | break; |
1501 | ||
1502 | case POLICY_NEW: | |
1503 | atomic_inc(&cache->stats.promotion); | |
1504 | promote(cache, structs, block, lookup_result.cblock, new_ocell); | |
1505 | release_cell = false; | |
1506 | break; | |
1507 | ||
1508 | case POLICY_REPLACE: | |
1509 | cell_prealloc = prealloc_get_cell(structs); | |
1510 | r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc, | |
1511 | (cell_free_fn) prealloc_put_cell, | |
1512 | structs, &old_ocell); | |
1513 | if (r > 0) { | |
1514 | /* | |
1515 | * We have to be careful to avoid lock inversion of | |
1516 | * the cells. So we back off, and wait for the | |
1517 | * old_ocell to become free. | |
1518 | */ | |
1519 | policy_force_mapping(cache->policy, block, | |
1520 | lookup_result.old_oblock); | |
1521 | atomic_inc(&cache->stats.cache_cell_clash); | |
1522 | break; | |
1523 | } | |
1524 | atomic_inc(&cache->stats.demotion); | |
1525 | atomic_inc(&cache->stats.promotion); | |
1526 | ||
1527 | demote_then_promote(cache, structs, lookup_result.old_oblock, | |
1528 | block, lookup_result.cblock, | |
1529 | old_ocell, new_ocell); | |
1530 | release_cell = false; | |
1531 | break; | |
1532 | ||
1533 | default: | |
1534 | DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__, | |
1535 | (unsigned) lookup_result.op); | |
1536 | bio_io_error(bio); | |
1537 | } | |
1538 | ||
1539 | if (release_cell) | |
1540 | cell_defer(cache, new_ocell, false); | |
1541 | } | |
1542 | ||
1543 | static int need_commit_due_to_time(struct cache *cache) | |
1544 | { | |
1545 | return jiffies < cache->last_commit_jiffies || | |
1546 | jiffies > cache->last_commit_jiffies + COMMIT_PERIOD; | |
1547 | } | |
1548 | ||
1549 | static int commit_if_needed(struct cache *cache) | |
1550 | { | |
ffcbcb67 HM |
1551 | int r = 0; |
1552 | ||
1553 | if ((cache->commit_requested || need_commit_due_to_time(cache)) && | |
1554 | dm_cache_changed_this_transaction(cache->cmd)) { | |
c6b4fcba | 1555 | atomic_inc(&cache->stats.commit_count); |
c6b4fcba | 1556 | cache->commit_requested = false; |
ffcbcb67 HM |
1557 | r = dm_cache_commit(cache->cmd, false); |
1558 | cache->last_commit_jiffies = jiffies; | |
c6b4fcba JT |
1559 | } |
1560 | ||
ffcbcb67 | 1561 | return r; |
c6b4fcba JT |
1562 | } |
1563 | ||
1564 | static void process_deferred_bios(struct cache *cache) | |
1565 | { | |
1566 | unsigned long flags; | |
1567 | struct bio_list bios; | |
1568 | struct bio *bio; | |
1569 | struct prealloc structs; | |
1570 | ||
1571 | memset(&structs, 0, sizeof(structs)); | |
1572 | bio_list_init(&bios); | |
1573 | ||
1574 | spin_lock_irqsave(&cache->lock, flags); | |
1575 | bio_list_merge(&bios, &cache->deferred_bios); | |
1576 | bio_list_init(&cache->deferred_bios); | |
1577 | spin_unlock_irqrestore(&cache->lock, flags); | |
1578 | ||
1579 | while (!bio_list_empty(&bios)) { | |
1580 | /* | |
1581 | * If we've got no free migration structs, and processing | |
1582 | * this bio might require one, we pause until there are some | |
1583 | * prepared mappings to process. | |
1584 | */ | |
1585 | if (prealloc_data_structs(cache, &structs)) { | |
1586 | spin_lock_irqsave(&cache->lock, flags); | |
1587 | bio_list_merge(&cache->deferred_bios, &bios); | |
1588 | spin_unlock_irqrestore(&cache->lock, flags); | |
1589 | break; | |
1590 | } | |
1591 | ||
1592 | bio = bio_list_pop(&bios); | |
1593 | ||
1594 | if (bio->bi_rw & REQ_FLUSH) | |
1595 | process_flush_bio(cache, bio); | |
1596 | else if (bio->bi_rw & REQ_DISCARD) | |
7ae34e77 | 1597 | process_discard_bio(cache, &structs, bio); |
c6b4fcba JT |
1598 | else |
1599 | process_bio(cache, &structs, bio); | |
1600 | } | |
1601 | ||
1602 | prealloc_free_structs(cache, &structs); | |
1603 | } | |
1604 | ||
1605 | static void process_deferred_flush_bios(struct cache *cache, bool submit_bios) | |
1606 | { | |
1607 | unsigned long flags; | |
1608 | struct bio_list bios; | |
1609 | struct bio *bio; | |
1610 | ||
1611 | bio_list_init(&bios); | |
1612 | ||
1613 | spin_lock_irqsave(&cache->lock, flags); | |
1614 | bio_list_merge(&bios, &cache->deferred_flush_bios); | |
1615 | bio_list_init(&cache->deferred_flush_bios); | |
1616 | spin_unlock_irqrestore(&cache->lock, flags); | |
1617 | ||
8c081b52 JT |
1618 | /* |
1619 | * These bios have already been through inc_ds() | |
1620 | */ | |
c6b4fcba JT |
1621 | while ((bio = bio_list_pop(&bios))) |
1622 | submit_bios ? generic_make_request(bio) : bio_io_error(bio); | |
1623 | } | |
1624 | ||
e2e74d61 JT |
1625 | static void process_deferred_writethrough_bios(struct cache *cache) |
1626 | { | |
1627 | unsigned long flags; | |
1628 | struct bio_list bios; | |
1629 | struct bio *bio; | |
1630 | ||
1631 | bio_list_init(&bios); | |
1632 | ||
1633 | spin_lock_irqsave(&cache->lock, flags); | |
1634 | bio_list_merge(&bios, &cache->deferred_writethrough_bios); | |
1635 | bio_list_init(&cache->deferred_writethrough_bios); | |
1636 | spin_unlock_irqrestore(&cache->lock, flags); | |
1637 | ||
8c081b52 JT |
1638 | /* |
1639 | * These bios have already been through inc_ds() | |
1640 | */ | |
e2e74d61 JT |
1641 | while ((bio = bio_list_pop(&bios))) |
1642 | generic_make_request(bio); | |
1643 | } | |
1644 | ||
c6b4fcba JT |
1645 | static void writeback_some_dirty_blocks(struct cache *cache) |
1646 | { | |
1647 | int r = 0; | |
1648 | dm_oblock_t oblock; | |
1649 | dm_cblock_t cblock; | |
1650 | struct prealloc structs; | |
1651 | struct dm_bio_prison_cell *old_ocell; | |
1652 | ||
1653 | memset(&structs, 0, sizeof(structs)); | |
1654 | ||
1655 | while (spare_migration_bandwidth(cache)) { | |
1656 | if (prealloc_data_structs(cache, &structs)) | |
1657 | break; | |
1658 | ||
1659 | r = policy_writeback_work(cache->policy, &oblock, &cblock); | |
1660 | if (r) | |
1661 | break; | |
1662 | ||
1663 | r = get_cell(cache, oblock, &structs, &old_ocell); | |
1664 | if (r) { | |
1665 | policy_set_dirty(cache->policy, oblock); | |
1666 | break; | |
1667 | } | |
1668 | ||
1669 | writeback(cache, &structs, oblock, cblock, old_ocell); | |
1670 | } | |
1671 | ||
1672 | prealloc_free_structs(cache, &structs); | |
1673 | } | |
1674 | ||
65790ff9 JT |
1675 | /*---------------------------------------------------------------- |
1676 | * Invalidations. | |
1677 | * Dropping something from the cache *without* writing back. | |
1678 | *--------------------------------------------------------------*/ | |
1679 | ||
1680 | static void process_invalidation_request(struct cache *cache, struct invalidation_request *req) | |
1681 | { | |
1682 | int r = 0; | |
1683 | uint64_t begin = from_cblock(req->cblocks->begin); | |
1684 | uint64_t end = from_cblock(req->cblocks->end); | |
1685 | ||
1686 | while (begin != end) { | |
1687 | r = policy_remove_cblock(cache->policy, to_cblock(begin)); | |
1688 | if (!r) { | |
1689 | r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin)); | |
1690 | if (r) | |
1691 | break; | |
1692 | ||
1693 | } else if (r == -ENODATA) { | |
1694 | /* harmless, already unmapped */ | |
1695 | r = 0; | |
1696 | ||
1697 | } else { | |
1698 | DMERR("policy_remove_cblock failed"); | |
1699 | break; | |
1700 | } | |
1701 | ||
1702 | begin++; | |
1703 | } | |
1704 | ||
1705 | cache->commit_requested = true; | |
1706 | ||
1707 | req->err = r; | |
1708 | atomic_set(&req->complete, 1); | |
1709 | ||
1710 | wake_up(&req->result_wait); | |
1711 | } | |
1712 | ||
1713 | static void process_invalidation_requests(struct cache *cache) | |
1714 | { | |
1715 | struct list_head list; | |
1716 | struct invalidation_request *req, *tmp; | |
1717 | ||
1718 | INIT_LIST_HEAD(&list); | |
1719 | spin_lock(&cache->invalidation_lock); | |
1720 | list_splice_init(&cache->invalidation_requests, &list); | |
1721 | spin_unlock(&cache->invalidation_lock); | |
1722 | ||
1723 | list_for_each_entry_safe (req, tmp, &list, list) | |
1724 | process_invalidation_request(cache, req); | |
1725 | } | |
1726 | ||
c6b4fcba JT |
1727 | /*---------------------------------------------------------------- |
1728 | * Main worker loop | |
1729 | *--------------------------------------------------------------*/ | |
66cb1910 | 1730 | static bool is_quiescing(struct cache *cache) |
c6b4fcba | 1731 | { |
238f8363 | 1732 | return atomic_read(&cache->quiescing); |
c6b4fcba JT |
1733 | } |
1734 | ||
66cb1910 JT |
1735 | static void ack_quiescing(struct cache *cache) |
1736 | { | |
1737 | if (is_quiescing(cache)) { | |
1738 | atomic_inc(&cache->quiescing_ack); | |
1739 | wake_up(&cache->quiescing_wait); | |
1740 | } | |
1741 | } | |
1742 | ||
1743 | static void wait_for_quiescing_ack(struct cache *cache) | |
1744 | { | |
1745 | wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack)); | |
1746 | } | |
1747 | ||
1748 | static void start_quiescing(struct cache *cache) | |
c6b4fcba | 1749 | { |
238f8363 | 1750 | atomic_inc(&cache->quiescing); |
66cb1910 | 1751 | wait_for_quiescing_ack(cache); |
c6b4fcba JT |
1752 | } |
1753 | ||
66cb1910 | 1754 | static void stop_quiescing(struct cache *cache) |
c6b4fcba | 1755 | { |
238f8363 | 1756 | atomic_set(&cache->quiescing, 0); |
66cb1910 | 1757 | atomic_set(&cache->quiescing_ack, 0); |
c6b4fcba JT |
1758 | } |
1759 | ||
1760 | static void wait_for_migrations(struct cache *cache) | |
1761 | { | |
1762 | wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations)); | |
1763 | } | |
1764 | ||
1765 | static void stop_worker(struct cache *cache) | |
1766 | { | |
1767 | cancel_delayed_work(&cache->waker); | |
1768 | flush_workqueue(cache->wq); | |
1769 | } | |
1770 | ||
1771 | static void requeue_deferred_io(struct cache *cache) | |
1772 | { | |
1773 | struct bio *bio; | |
1774 | struct bio_list bios; | |
1775 | ||
1776 | bio_list_init(&bios); | |
1777 | bio_list_merge(&bios, &cache->deferred_bios); | |
1778 | bio_list_init(&cache->deferred_bios); | |
1779 | ||
1780 | while ((bio = bio_list_pop(&bios))) | |
1781 | bio_endio(bio, DM_ENDIO_REQUEUE); | |
1782 | } | |
1783 | ||
1784 | static int more_work(struct cache *cache) | |
1785 | { | |
1786 | if (is_quiescing(cache)) | |
1787 | return !list_empty(&cache->quiesced_migrations) || | |
1788 | !list_empty(&cache->completed_migrations) || | |
1789 | !list_empty(&cache->need_commit_migrations); | |
1790 | else | |
1791 | return !bio_list_empty(&cache->deferred_bios) || | |
1792 | !bio_list_empty(&cache->deferred_flush_bios) || | |
e2e74d61 | 1793 | !bio_list_empty(&cache->deferred_writethrough_bios) || |
c6b4fcba JT |
1794 | !list_empty(&cache->quiesced_migrations) || |
1795 | !list_empty(&cache->completed_migrations) || | |
65790ff9 JT |
1796 | !list_empty(&cache->need_commit_migrations) || |
1797 | cache->invalidate; | |
c6b4fcba JT |
1798 | } |
1799 | ||
1800 | static void do_worker(struct work_struct *ws) | |
1801 | { | |
1802 | struct cache *cache = container_of(ws, struct cache, worker); | |
1803 | ||
1804 | do { | |
66cb1910 JT |
1805 | if (!is_quiescing(cache)) { |
1806 | writeback_some_dirty_blocks(cache); | |
1807 | process_deferred_writethrough_bios(cache); | |
c6b4fcba | 1808 | process_deferred_bios(cache); |
65790ff9 | 1809 | process_invalidation_requests(cache); |
66cb1910 | 1810 | } |
c6b4fcba | 1811 | |
7ae34e77 | 1812 | process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard); |
c6b4fcba JT |
1813 | process_migrations(cache, &cache->completed_migrations, complete_migration); |
1814 | ||
c6b4fcba JT |
1815 | if (commit_if_needed(cache)) { |
1816 | process_deferred_flush_bios(cache, false); | |
304affaa | 1817 | process_migrations(cache, &cache->need_commit_migrations, migration_failure); |
c6b4fcba JT |
1818 | |
1819 | /* | |
1820 | * FIXME: rollback metadata or just go into a | |
1821 | * failure mode and error everything | |
1822 | */ | |
1823 | } else { | |
1824 | process_deferred_flush_bios(cache, true); | |
1825 | process_migrations(cache, &cache->need_commit_migrations, | |
1826 | migration_success_post_commit); | |
1827 | } | |
66cb1910 JT |
1828 | |
1829 | ack_quiescing(cache); | |
1830 | ||
c6b4fcba JT |
1831 | } while (more_work(cache)); |
1832 | } | |
1833 | ||
1834 | /* | |
1835 | * We want to commit periodically so that not too much | |
1836 | * unwritten metadata builds up. | |
1837 | */ | |
1838 | static void do_waker(struct work_struct *ws) | |
1839 | { | |
1840 | struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker); | |
f8350daf | 1841 | policy_tick(cache->policy); |
c6b4fcba JT |
1842 | wake_worker(cache); |
1843 | queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD); | |
1844 | } | |
1845 | ||
1846 | /*----------------------------------------------------------------*/ | |
1847 | ||
1848 | static int is_congested(struct dm_dev *dev, int bdi_bits) | |
1849 | { | |
1850 | struct request_queue *q = bdev_get_queue(dev->bdev); | |
1851 | return bdi_congested(&q->backing_dev_info, bdi_bits); | |
1852 | } | |
1853 | ||
1854 | static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits) | |
1855 | { | |
1856 | struct cache *cache = container_of(cb, struct cache, callbacks); | |
1857 | ||
1858 | return is_congested(cache->origin_dev, bdi_bits) || | |
1859 | is_congested(cache->cache_dev, bdi_bits); | |
1860 | } | |
1861 | ||
1862 | /*---------------------------------------------------------------- | |
1863 | * Target methods | |
1864 | *--------------------------------------------------------------*/ | |
1865 | ||
1866 | /* | |
1867 | * This function gets called on the error paths of the constructor, so we | |
1868 | * have to cope with a partially initialised struct. | |
1869 | */ | |
1870 | static void destroy(struct cache *cache) | |
1871 | { | |
1872 | unsigned i; | |
1873 | ||
1874 | if (cache->next_migration) | |
1875 | mempool_free(cache->next_migration, cache->migration_pool); | |
1876 | ||
1877 | if (cache->migration_pool) | |
1878 | mempool_destroy(cache->migration_pool); | |
1879 | ||
1880 | if (cache->all_io_ds) | |
1881 | dm_deferred_set_destroy(cache->all_io_ds); | |
1882 | ||
1883 | if (cache->prison) | |
1884 | dm_bio_prison_destroy(cache->prison); | |
1885 | ||
1886 | if (cache->wq) | |
1887 | destroy_workqueue(cache->wq); | |
1888 | ||
1889 | if (cache->dirty_bitset) | |
1890 | free_bitset(cache->dirty_bitset); | |
1891 | ||
1892 | if (cache->discard_bitset) | |
1893 | free_bitset(cache->discard_bitset); | |
1894 | ||
1895 | if (cache->copier) | |
1896 | dm_kcopyd_client_destroy(cache->copier); | |
1897 | ||
1898 | if (cache->cmd) | |
1899 | dm_cache_metadata_close(cache->cmd); | |
1900 | ||
1901 | if (cache->metadata_dev) | |
1902 | dm_put_device(cache->ti, cache->metadata_dev); | |
1903 | ||
1904 | if (cache->origin_dev) | |
1905 | dm_put_device(cache->ti, cache->origin_dev); | |
1906 | ||
1907 | if (cache->cache_dev) | |
1908 | dm_put_device(cache->ti, cache->cache_dev); | |
1909 | ||
1910 | if (cache->policy) | |
1911 | dm_cache_policy_destroy(cache->policy); | |
1912 | ||
1913 | for (i = 0; i < cache->nr_ctr_args ; i++) | |
1914 | kfree(cache->ctr_args[i]); | |
1915 | kfree(cache->ctr_args); | |
1916 | ||
1917 | kfree(cache); | |
1918 | } | |
1919 | ||
1920 | static void cache_dtr(struct dm_target *ti) | |
1921 | { | |
1922 | struct cache *cache = ti->private; | |
1923 | ||
1924 | destroy(cache); | |
1925 | } | |
1926 | ||
1927 | static sector_t get_dev_size(struct dm_dev *dev) | |
1928 | { | |
1929 | return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT; | |
1930 | } | |
1931 | ||
1932 | /*----------------------------------------------------------------*/ | |
1933 | ||
1934 | /* | |
1935 | * Construct a cache device mapping. | |
1936 | * | |
1937 | * cache <metadata dev> <cache dev> <origin dev> <block size> | |
1938 | * <#feature args> [<feature arg>]* | |
1939 | * <policy> <#policy args> [<policy arg>]* | |
1940 | * | |
1941 | * metadata dev : fast device holding the persistent metadata | |
1942 | * cache dev : fast device holding cached data blocks | |
1943 | * origin dev : slow device holding original data blocks | |
1944 | * block size : cache unit size in sectors | |
1945 | * | |
1946 | * #feature args : number of feature arguments passed | |
1947 | * feature args : writethrough. (The default is writeback.) | |
1948 | * | |
1949 | * policy : the replacement policy to use | |
1950 | * #policy args : an even number of policy arguments corresponding | |
1951 | * to key/value pairs passed to the policy | |
1952 | * policy args : key/value pairs passed to the policy | |
1953 | * E.g. 'sequential_threshold 1024' | |
1954 | * See cache-policies.txt for details. | |
1955 | * | |
1956 | * Optional feature arguments are: | |
1957 | * writethrough : write through caching that prohibits cache block | |
1958 | * content from being different from origin block content. | |
1959 | * Without this argument, the default behaviour is to write | |
1960 | * back cache block contents later for performance reasons, | |
1961 | * so they may differ from the corresponding origin blocks. | |
1962 | */ | |
1963 | struct cache_args { | |
1964 | struct dm_target *ti; | |
1965 | ||
1966 | struct dm_dev *metadata_dev; | |
1967 | ||
1968 | struct dm_dev *cache_dev; | |
1969 | sector_t cache_sectors; | |
1970 | ||
1971 | struct dm_dev *origin_dev; | |
1972 | sector_t origin_sectors; | |
1973 | ||
1974 | uint32_t block_size; | |
1975 | ||
1976 | const char *policy_name; | |
1977 | int policy_argc; | |
1978 | const char **policy_argv; | |
1979 | ||
1980 | struct cache_features features; | |
1981 | }; | |
1982 | ||
1983 | static void destroy_cache_args(struct cache_args *ca) | |
1984 | { | |
1985 | if (ca->metadata_dev) | |
1986 | dm_put_device(ca->ti, ca->metadata_dev); | |
1987 | ||
1988 | if (ca->cache_dev) | |
1989 | dm_put_device(ca->ti, ca->cache_dev); | |
1990 | ||
1991 | if (ca->origin_dev) | |
1992 | dm_put_device(ca->ti, ca->origin_dev); | |
1993 | ||
1994 | kfree(ca); | |
1995 | } | |
1996 | ||
1997 | static bool at_least_one_arg(struct dm_arg_set *as, char **error) | |
1998 | { | |
1999 | if (!as->argc) { | |
2000 | *error = "Insufficient args"; | |
2001 | return false; | |
2002 | } | |
2003 | ||
2004 | return true; | |
2005 | } | |
2006 | ||
2007 | static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as, | |
2008 | char **error) | |
2009 | { | |
2010 | int r; | |
2011 | sector_t metadata_dev_size; | |
2012 | char b[BDEVNAME_SIZE]; | |
2013 | ||
2014 | if (!at_least_one_arg(as, error)) | |
2015 | return -EINVAL; | |
2016 | ||
2017 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, | |
2018 | &ca->metadata_dev); | |
2019 | if (r) { | |
2020 | *error = "Error opening metadata device"; | |
2021 | return r; | |
2022 | } | |
2023 | ||
2024 | metadata_dev_size = get_dev_size(ca->metadata_dev); | |
2025 | if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING) | |
2026 | DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.", | |
2027 | bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS); | |
2028 | ||
2029 | return 0; | |
2030 | } | |
2031 | ||
2032 | static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as, | |
2033 | char **error) | |
2034 | { | |
2035 | int r; | |
2036 | ||
2037 | if (!at_least_one_arg(as, error)) | |
2038 | return -EINVAL; | |
2039 | ||
2040 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, | |
2041 | &ca->cache_dev); | |
2042 | if (r) { | |
2043 | *error = "Error opening cache device"; | |
2044 | return r; | |
2045 | } | |
2046 | ca->cache_sectors = get_dev_size(ca->cache_dev); | |
2047 | ||
2048 | return 0; | |
2049 | } | |
2050 | ||
2051 | static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as, | |
2052 | char **error) | |
2053 | { | |
2054 | int r; | |
2055 | ||
2056 | if (!at_least_one_arg(as, error)) | |
2057 | return -EINVAL; | |
2058 | ||
2059 | r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE, | |
2060 | &ca->origin_dev); | |
2061 | if (r) { | |
2062 | *error = "Error opening origin device"; | |
2063 | return r; | |
2064 | } | |
2065 | ||
2066 | ca->origin_sectors = get_dev_size(ca->origin_dev); | |
2067 | if (ca->ti->len > ca->origin_sectors) { | |
2068 | *error = "Device size larger than cached device"; | |
2069 | return -EINVAL; | |
2070 | } | |
2071 | ||
2072 | return 0; | |
2073 | } | |
2074 | ||
2075 | static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as, | |
2076 | char **error) | |
2077 | { | |
05473044 | 2078 | unsigned long block_size; |
c6b4fcba JT |
2079 | |
2080 | if (!at_least_one_arg(as, error)) | |
2081 | return -EINVAL; | |
2082 | ||
05473044 MS |
2083 | if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size || |
2084 | block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS || | |
2085 | block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS || | |
2086 | block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) { | |
c6b4fcba JT |
2087 | *error = "Invalid data block size"; |
2088 | return -EINVAL; | |
2089 | } | |
2090 | ||
05473044 | 2091 | if (block_size > ca->cache_sectors) { |
c6b4fcba JT |
2092 | *error = "Data block size is larger than the cache device"; |
2093 | return -EINVAL; | |
2094 | } | |
2095 | ||
05473044 | 2096 | ca->block_size = block_size; |
c6b4fcba JT |
2097 | |
2098 | return 0; | |
2099 | } | |
2100 | ||
2101 | static void init_features(struct cache_features *cf) | |
2102 | { | |
2103 | cf->mode = CM_WRITE; | |
2ee57d58 | 2104 | cf->io_mode = CM_IO_WRITEBACK; |
c6b4fcba JT |
2105 | } |
2106 | ||
2107 | static int parse_features(struct cache_args *ca, struct dm_arg_set *as, | |
2108 | char **error) | |
2109 | { | |
2110 | static struct dm_arg _args[] = { | |
2111 | {0, 1, "Invalid number of cache feature arguments"}, | |
2112 | }; | |
2113 | ||
2114 | int r; | |
2115 | unsigned argc; | |
2116 | const char *arg; | |
2117 | struct cache_features *cf = &ca->features; | |
2118 | ||
2119 | init_features(cf); | |
2120 | ||
2121 | r = dm_read_arg_group(_args, as, &argc, error); | |
2122 | if (r) | |
2123 | return -EINVAL; | |
2124 | ||
2125 | while (argc--) { | |
2126 | arg = dm_shift_arg(as); | |
2127 | ||
2128 | if (!strcasecmp(arg, "writeback")) | |
2ee57d58 | 2129 | cf->io_mode = CM_IO_WRITEBACK; |
c6b4fcba JT |
2130 | |
2131 | else if (!strcasecmp(arg, "writethrough")) | |
2ee57d58 JT |
2132 | cf->io_mode = CM_IO_WRITETHROUGH; |
2133 | ||
2134 | else if (!strcasecmp(arg, "passthrough")) | |
2135 | cf->io_mode = CM_IO_PASSTHROUGH; | |
c6b4fcba JT |
2136 | |
2137 | else { | |
2138 | *error = "Unrecognised cache feature requested"; | |
2139 | return -EINVAL; | |
2140 | } | |
2141 | } | |
2142 | ||
2143 | return 0; | |
2144 | } | |
2145 | ||
2146 | static int parse_policy(struct cache_args *ca, struct dm_arg_set *as, | |
2147 | char **error) | |
2148 | { | |
2149 | static struct dm_arg _args[] = { | |
2150 | {0, 1024, "Invalid number of policy arguments"}, | |
2151 | }; | |
2152 | ||
2153 | int r; | |
2154 | ||
2155 | if (!at_least_one_arg(as, error)) | |
2156 | return -EINVAL; | |
2157 | ||
2158 | ca->policy_name = dm_shift_arg(as); | |
2159 | ||
2160 | r = dm_read_arg_group(_args, as, &ca->policy_argc, error); | |
2161 | if (r) | |
2162 | return -EINVAL; | |
2163 | ||
2164 | ca->policy_argv = (const char **)as->argv; | |
2165 | dm_consume_args(as, ca->policy_argc); | |
2166 | ||
2167 | return 0; | |
2168 | } | |
2169 | ||
2170 | static int parse_cache_args(struct cache_args *ca, int argc, char **argv, | |
2171 | char **error) | |
2172 | { | |
2173 | int r; | |
2174 | struct dm_arg_set as; | |
2175 | ||
2176 | as.argc = argc; | |
2177 | as.argv = argv; | |
2178 | ||
2179 | r = parse_metadata_dev(ca, &as, error); | |
2180 | if (r) | |
2181 | return r; | |
2182 | ||
2183 | r = parse_cache_dev(ca, &as, error); | |
2184 | if (r) | |
2185 | return r; | |
2186 | ||
2187 | r = parse_origin_dev(ca, &as, error); | |
2188 | if (r) | |
2189 | return r; | |
2190 | ||
2191 | r = parse_block_size(ca, &as, error); | |
2192 | if (r) | |
2193 | return r; | |
2194 | ||
2195 | r = parse_features(ca, &as, error); | |
2196 | if (r) | |
2197 | return r; | |
2198 | ||
2199 | r = parse_policy(ca, &as, error); | |
2200 | if (r) | |
2201 | return r; | |
2202 | ||
2203 | return 0; | |
2204 | } | |
2205 | ||
2206 | /*----------------------------------------------------------------*/ | |
2207 | ||
2208 | static struct kmem_cache *migration_cache; | |
2209 | ||
2c73c471 AK |
2210 | #define NOT_CORE_OPTION 1 |
2211 | ||
2f14f4b5 | 2212 | static int process_config_option(struct cache *cache, const char *key, const char *value) |
2c73c471 AK |
2213 | { |
2214 | unsigned long tmp; | |
2215 | ||
2f14f4b5 JT |
2216 | if (!strcasecmp(key, "migration_threshold")) { |
2217 | if (kstrtoul(value, 10, &tmp)) | |
2c73c471 AK |
2218 | return -EINVAL; |
2219 | ||
2220 | cache->migration_threshold = tmp; | |
2221 | return 0; | |
2222 | } | |
2223 | ||
2224 | return NOT_CORE_OPTION; | |
2225 | } | |
2226 | ||
2f14f4b5 JT |
2227 | static int set_config_value(struct cache *cache, const char *key, const char *value) |
2228 | { | |
2229 | int r = process_config_option(cache, key, value); | |
2230 | ||
2231 | if (r == NOT_CORE_OPTION) | |
2232 | r = policy_set_config_value(cache->policy, key, value); | |
2233 | ||
2234 | if (r) | |
2235 | DMWARN("bad config value for %s: %s", key, value); | |
2236 | ||
2237 | return r; | |
2238 | } | |
2239 | ||
2240 | static int set_config_values(struct cache *cache, int argc, const char **argv) | |
c6b4fcba JT |
2241 | { |
2242 | int r = 0; | |
2243 | ||
2244 | if (argc & 1) { | |
2245 | DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs."); | |
2246 | return -EINVAL; | |
2247 | } | |
2248 | ||
2249 | while (argc) { | |
2f14f4b5 JT |
2250 | r = set_config_value(cache, argv[0], argv[1]); |
2251 | if (r) | |
2252 | break; | |
c6b4fcba JT |
2253 | |
2254 | argc -= 2; | |
2255 | argv += 2; | |
2256 | } | |
2257 | ||
2258 | return r; | |
2259 | } | |
2260 | ||
2261 | static int create_cache_policy(struct cache *cache, struct cache_args *ca, | |
2262 | char **error) | |
2263 | { | |
4cb3e1db MP |
2264 | struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name, |
2265 | cache->cache_size, | |
2266 | cache->origin_sectors, | |
2267 | cache->sectors_per_block); | |
2268 | if (IS_ERR(p)) { | |
c6b4fcba | 2269 | *error = "Error creating cache's policy"; |
4cb3e1db | 2270 | return PTR_ERR(p); |
c6b4fcba | 2271 | } |
4cb3e1db | 2272 | cache->policy = p; |
c6b4fcba | 2273 | |
2f14f4b5 | 2274 | return 0; |
c6b4fcba JT |
2275 | } |
2276 | ||
08b18451 JT |
2277 | /* |
2278 | * We want the discard block size to be a power of two, at least the size | |
2279 | * of the cache block size, and have no more than 2^14 discard blocks | |
2280 | * across the origin. | |
2281 | */ | |
2282 | #define MAX_DISCARD_BLOCKS (1 << 14) | |
2283 | ||
2284 | static bool too_many_discard_blocks(sector_t discard_block_size, | |
2285 | sector_t origin_size) | |
2286 | { | |
2287 | (void) sector_div(origin_size, discard_block_size); | |
2288 | ||
2289 | return origin_size > MAX_DISCARD_BLOCKS; | |
2290 | } | |
2291 | ||
2292 | static sector_t calculate_discard_block_size(sector_t cache_block_size, | |
2293 | sector_t origin_size) | |
2294 | { | |
2295 | sector_t discard_block_size; | |
2296 | ||
2297 | discard_block_size = roundup_pow_of_two(cache_block_size); | |
2298 | ||
2299 | if (origin_size) | |
2300 | while (too_many_discard_blocks(discard_block_size, origin_size)) | |
2301 | discard_block_size *= 2; | |
2302 | ||
2303 | return discard_block_size; | |
2304 | } | |
2305 | ||
d1d9220c JT |
2306 | static void set_cache_size(struct cache *cache, dm_cblock_t size) |
2307 | { | |
2308 | dm_block_t nr_blocks = from_cblock(size); | |
2309 | ||
2310 | if (nr_blocks > (1 << 20) && cache->cache_size != size) | |
2311 | DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n" | |
2312 | "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n" | |
2313 | "Please consider increasing the cache block size to reduce the overall cache block count.", | |
2314 | (unsigned long long) nr_blocks); | |
2315 | ||
2316 | cache->cache_size = size; | |
2317 | } | |
2318 | ||
f8350daf | 2319 | #define DEFAULT_MIGRATION_THRESHOLD 2048 |
c6b4fcba | 2320 | |
c6b4fcba JT |
2321 | static int cache_create(struct cache_args *ca, struct cache **result) |
2322 | { | |
2323 | int r = 0; | |
2324 | char **error = &ca->ti->error; | |
2325 | struct cache *cache; | |
2326 | struct dm_target *ti = ca->ti; | |
2327 | dm_block_t origin_blocks; | |
2328 | struct dm_cache_metadata *cmd; | |
2329 | bool may_format = ca->features.mode == CM_WRITE; | |
2330 | ||
2331 | cache = kzalloc(sizeof(*cache), GFP_KERNEL); | |
2332 | if (!cache) | |
2333 | return -ENOMEM; | |
2334 | ||
2335 | cache->ti = ca->ti; | |
2336 | ti->private = cache; | |
c6b4fcba JT |
2337 | ti->num_flush_bios = 2; |
2338 | ti->flush_supported = true; | |
2339 | ||
2340 | ti->num_discard_bios = 1; | |
2341 | ti->discards_supported = true; | |
2342 | ti->discard_zeroes_data_unsupported = true; | |
2572629a | 2343 | ti->split_discard_bios = false; |
c6b4fcba | 2344 | |
8c5008fa | 2345 | cache->features = ca->features; |
19b0092e | 2346 | ti->per_bio_data_size = get_per_bio_data_size(cache); |
c6b4fcba | 2347 | |
c6b4fcba JT |
2348 | cache->callbacks.congested_fn = cache_is_congested; |
2349 | dm_table_add_target_callbacks(ti->table, &cache->callbacks); | |
2350 | ||
2351 | cache->metadata_dev = ca->metadata_dev; | |
2352 | cache->origin_dev = ca->origin_dev; | |
2353 | cache->cache_dev = ca->cache_dev; | |
2354 | ||
2355 | ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL; | |
2356 | ||
2357 | /* FIXME: factor out this whole section */ | |
2358 | origin_blocks = cache->origin_sectors = ca->origin_sectors; | |
414dd67d | 2359 | origin_blocks = block_div(origin_blocks, ca->block_size); |
c6b4fcba JT |
2360 | cache->origin_blocks = to_oblock(origin_blocks); |
2361 | ||
2362 | cache->sectors_per_block = ca->block_size; | |
2363 | if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) { | |
2364 | r = -EINVAL; | |
2365 | goto bad; | |
2366 | } | |
2367 | ||
2368 | if (ca->block_size & (ca->block_size - 1)) { | |
2369 | dm_block_t cache_size = ca->cache_sectors; | |
2370 | ||
2371 | cache->sectors_per_block_shift = -1; | |
414dd67d | 2372 | cache_size = block_div(cache_size, ca->block_size); |
d1d9220c | 2373 | set_cache_size(cache, to_cblock(cache_size)); |
c6b4fcba JT |
2374 | } else { |
2375 | cache->sectors_per_block_shift = __ffs(ca->block_size); | |
d1d9220c | 2376 | set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift)); |
c6b4fcba JT |
2377 | } |
2378 | ||
2379 | r = create_cache_policy(cache, ca, error); | |
2380 | if (r) | |
2381 | goto bad; | |
2f14f4b5 | 2382 | |
c6b4fcba | 2383 | cache->policy_nr_args = ca->policy_argc; |
2f14f4b5 JT |
2384 | cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD; |
2385 | ||
2386 | r = set_config_values(cache, ca->policy_argc, ca->policy_argv); | |
2387 | if (r) { | |
2388 | *error = "Error setting cache policy's config values"; | |
2389 | goto bad; | |
2390 | } | |
c6b4fcba JT |
2391 | |
2392 | cmd = dm_cache_metadata_open(cache->metadata_dev->bdev, | |
2393 | ca->block_size, may_format, | |
2394 | dm_cache_policy_get_hint_size(cache->policy)); | |
2395 | if (IS_ERR(cmd)) { | |
2396 | *error = "Error creating metadata object"; | |
2397 | r = PTR_ERR(cmd); | |
2398 | goto bad; | |
2399 | } | |
2400 | cache->cmd = cmd; | |
2401 | ||
2ee57d58 JT |
2402 | if (passthrough_mode(&cache->features)) { |
2403 | bool all_clean; | |
2404 | ||
2405 | r = dm_cache_metadata_all_clean(cache->cmd, &all_clean); | |
2406 | if (r) { | |
2407 | *error = "dm_cache_metadata_all_clean() failed"; | |
2408 | goto bad; | |
2409 | } | |
2410 | ||
2411 | if (!all_clean) { | |
2412 | *error = "Cannot enter passthrough mode unless all blocks are clean"; | |
2413 | r = -EINVAL; | |
2414 | goto bad; | |
2415 | } | |
2416 | } | |
2417 | ||
c6b4fcba JT |
2418 | spin_lock_init(&cache->lock); |
2419 | bio_list_init(&cache->deferred_bios); | |
2420 | bio_list_init(&cache->deferred_flush_bios); | |
e2e74d61 | 2421 | bio_list_init(&cache->deferred_writethrough_bios); |
c6b4fcba JT |
2422 | INIT_LIST_HEAD(&cache->quiesced_migrations); |
2423 | INIT_LIST_HEAD(&cache->completed_migrations); | |
2424 | INIT_LIST_HEAD(&cache->need_commit_migrations); | |
c6b4fcba JT |
2425 | atomic_set(&cache->nr_migrations, 0); |
2426 | init_waitqueue_head(&cache->migration_wait); | |
2427 | ||
66cb1910 | 2428 | init_waitqueue_head(&cache->quiescing_wait); |
238f8363 | 2429 | atomic_set(&cache->quiescing, 0); |
66cb1910 JT |
2430 | atomic_set(&cache->quiescing_ack, 0); |
2431 | ||
fa4d683a | 2432 | r = -ENOMEM; |
44fa816b | 2433 | atomic_set(&cache->nr_dirty, 0); |
c6b4fcba JT |
2434 | cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size)); |
2435 | if (!cache->dirty_bitset) { | |
2436 | *error = "could not allocate dirty bitset"; | |
2437 | goto bad; | |
2438 | } | |
2439 | clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size)); | |
2440 | ||
08b18451 JT |
2441 | cache->discard_block_size = |
2442 | calculate_discard_block_size(cache->sectors_per_block, | |
2443 | cache->origin_sectors); | |
2572629a JT |
2444 | cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors, |
2445 | cache->discard_block_size)); | |
1bad9bc4 | 2446 | cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks)); |
c6b4fcba JT |
2447 | if (!cache->discard_bitset) { |
2448 | *error = "could not allocate discard bitset"; | |
2449 | goto bad; | |
2450 | } | |
1bad9bc4 | 2451 | clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks)); |
c6b4fcba JT |
2452 | |
2453 | cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle); | |
2454 | if (IS_ERR(cache->copier)) { | |
2455 | *error = "could not create kcopyd client"; | |
2456 | r = PTR_ERR(cache->copier); | |
2457 | goto bad; | |
2458 | } | |
2459 | ||
2460 | cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); | |
2461 | if (!cache->wq) { | |
2462 | *error = "could not create workqueue for metadata object"; | |
2463 | goto bad; | |
2464 | } | |
2465 | INIT_WORK(&cache->worker, do_worker); | |
2466 | INIT_DELAYED_WORK(&cache->waker, do_waker); | |
2467 | cache->last_commit_jiffies = jiffies; | |
2468 | ||
a195db2d | 2469 | cache->prison = dm_bio_prison_create(); |
c6b4fcba JT |
2470 | if (!cache->prison) { |
2471 | *error = "could not create bio prison"; | |
2472 | goto bad; | |
2473 | } | |
2474 | ||
2475 | cache->all_io_ds = dm_deferred_set_create(); | |
2476 | if (!cache->all_io_ds) { | |
2477 | *error = "could not create all_io deferred set"; | |
2478 | goto bad; | |
2479 | } | |
2480 | ||
2481 | cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE, | |
2482 | migration_cache); | |
2483 | if (!cache->migration_pool) { | |
2484 | *error = "Error creating cache's migration mempool"; | |
2485 | goto bad; | |
2486 | } | |
2487 | ||
2488 | cache->next_migration = NULL; | |
2489 | ||
2490 | cache->need_tick_bio = true; | |
2491 | cache->sized = false; | |
65790ff9 | 2492 | cache->invalidate = false; |
c6b4fcba JT |
2493 | cache->commit_requested = false; |
2494 | cache->loaded_mappings = false; | |
2495 | cache->loaded_discards = false; | |
2496 | ||
2497 | load_stats(cache); | |
2498 | ||
2499 | atomic_set(&cache->stats.demotion, 0); | |
2500 | atomic_set(&cache->stats.promotion, 0); | |
2501 | atomic_set(&cache->stats.copies_avoided, 0); | |
2502 | atomic_set(&cache->stats.cache_cell_clash, 0); | |
2503 | atomic_set(&cache->stats.commit_count, 0); | |
2504 | atomic_set(&cache->stats.discard_count, 0); | |
2505 | ||
65790ff9 JT |
2506 | spin_lock_init(&cache->invalidation_lock); |
2507 | INIT_LIST_HEAD(&cache->invalidation_requests); | |
2508 | ||
c6b4fcba JT |
2509 | *result = cache; |
2510 | return 0; | |
2511 | ||
2512 | bad: | |
2513 | destroy(cache); | |
2514 | return r; | |
2515 | } | |
2516 | ||
2517 | static int copy_ctr_args(struct cache *cache, int argc, const char **argv) | |
2518 | { | |
2519 | unsigned i; | |
2520 | const char **copy; | |
2521 | ||
2522 | copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL); | |
2523 | if (!copy) | |
2524 | return -ENOMEM; | |
2525 | for (i = 0; i < argc; i++) { | |
2526 | copy[i] = kstrdup(argv[i], GFP_KERNEL); | |
2527 | if (!copy[i]) { | |
2528 | while (i--) | |
2529 | kfree(copy[i]); | |
2530 | kfree(copy); | |
2531 | return -ENOMEM; | |
2532 | } | |
2533 | } | |
2534 | ||
2535 | cache->nr_ctr_args = argc; | |
2536 | cache->ctr_args = copy; | |
2537 | ||
2538 | return 0; | |
2539 | } | |
2540 | ||
2541 | static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv) | |
2542 | { | |
2543 | int r = -EINVAL; | |
2544 | struct cache_args *ca; | |
2545 | struct cache *cache = NULL; | |
2546 | ||
2547 | ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
2548 | if (!ca) { | |
2549 | ti->error = "Error allocating memory for cache"; | |
2550 | return -ENOMEM; | |
2551 | } | |
2552 | ca->ti = ti; | |
2553 | ||
2554 | r = parse_cache_args(ca, argc, argv, &ti->error); | |
2555 | if (r) | |
2556 | goto out; | |
2557 | ||
2558 | r = cache_create(ca, &cache); | |
617a0b89 HM |
2559 | if (r) |
2560 | goto out; | |
c6b4fcba JT |
2561 | |
2562 | r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3); | |
2563 | if (r) { | |
2564 | destroy(cache); | |
2565 | goto out; | |
2566 | } | |
2567 | ||
2568 | ti->private = cache; | |
2569 | ||
2570 | out: | |
2571 | destroy_cache_args(ca); | |
2572 | return r; | |
2573 | } | |
2574 | ||
8c081b52 | 2575 | static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell) |
c6b4fcba | 2576 | { |
c6b4fcba JT |
2577 | int r; |
2578 | dm_oblock_t block = get_bio_block(cache, bio); | |
19b0092e | 2579 | size_t pb_data_size = get_per_bio_data_size(cache); |
c6b4fcba JT |
2580 | bool can_migrate = false; |
2581 | bool discarded_block; | |
c6b4fcba | 2582 | struct policy_result lookup_result; |
e893fba9 | 2583 | struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size); |
c6b4fcba | 2584 | |
e893fba9 | 2585 | if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) { |
c6b4fcba JT |
2586 | /* |
2587 | * This can only occur if the io goes to a partial block at | |
2588 | * the end of the origin device. We don't cache these. | |
2589 | * Just remap to the origin and carry on. | |
2590 | */ | |
e893fba9 | 2591 | remap_to_origin(cache, bio); |
c6b4fcba JT |
2592 | return DM_MAPIO_REMAPPED; |
2593 | } | |
2594 | ||
c6b4fcba JT |
2595 | if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) { |
2596 | defer_bio(cache, bio); | |
2597 | return DM_MAPIO_SUBMITTED; | |
2598 | } | |
2599 | ||
2600 | /* | |
2601 | * Check to see if that block is currently migrating. | |
2602 | */ | |
8c081b52 JT |
2603 | *cell = alloc_prison_cell(cache); |
2604 | if (!*cell) { | |
c6b4fcba JT |
2605 | defer_bio(cache, bio); |
2606 | return DM_MAPIO_SUBMITTED; | |
2607 | } | |
2608 | ||
8c081b52 | 2609 | r = bio_detain(cache, block, bio, *cell, |
c6b4fcba | 2610 | (cell_free_fn) free_prison_cell, |
8c081b52 | 2611 | cache, cell); |
c6b4fcba JT |
2612 | if (r) { |
2613 | if (r < 0) | |
2614 | defer_bio(cache, bio); | |
2615 | ||
2616 | return DM_MAPIO_SUBMITTED; | |
2617 | } | |
2618 | ||
2619 | discarded_block = is_discarded_oblock(cache, block); | |
2620 | ||
2621 | r = policy_map(cache->policy, block, false, can_migrate, discarded_block, | |
2622 | bio, &lookup_result); | |
2623 | if (r == -EWOULDBLOCK) { | |
8c081b52 | 2624 | cell_defer(cache, *cell, true); |
c6b4fcba JT |
2625 | return DM_MAPIO_SUBMITTED; |
2626 | ||
2627 | } else if (r) { | |
2628 | DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r); | |
8c081b52 | 2629 | cell_defer(cache, *cell, false); |
c6b4fcba JT |
2630 | bio_io_error(bio); |
2631 | return DM_MAPIO_SUBMITTED; | |
2632 | } | |
2633 | ||
2ee57d58 | 2634 | r = DM_MAPIO_REMAPPED; |
c6b4fcba JT |
2635 | switch (lookup_result.op) { |
2636 | case POLICY_HIT: | |
2ee57d58 JT |
2637 | if (passthrough_mode(&cache->features)) { |
2638 | if (bio_data_dir(bio) == WRITE) { | |
2639 | /* | |
2640 | * We need to invalidate this block, so | |
2641 | * defer for the worker thread. | |
2642 | */ | |
8c081b52 | 2643 | cell_defer(cache, *cell, true); |
2ee57d58 JT |
2644 | r = DM_MAPIO_SUBMITTED; |
2645 | ||
2646 | } else { | |
2ee57d58 JT |
2647 | inc_miss_counter(cache, bio); |
2648 | remap_to_origin_clear_discard(cache, bio, block); | |
2ee57d58 | 2649 | } |
c6b4fcba | 2650 | |
2ee57d58 JT |
2651 | } else { |
2652 | inc_hit_counter(cache, bio); | |
2ee57d58 JT |
2653 | if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) && |
2654 | !is_dirty(cache, lookup_result.cblock)) | |
2655 | remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock); | |
2656 | else | |
2657 | remap_to_cache_dirty(cache, bio, block, lookup_result.cblock); | |
2ee57d58 | 2658 | } |
c6b4fcba JT |
2659 | break; |
2660 | ||
2661 | case POLICY_MISS: | |
2662 | inc_miss_counter(cache, bio); | |
c6b4fcba JT |
2663 | if (pb->req_nr != 0) { |
2664 | /* | |
2665 | * This is a duplicate writethrough io that is no | |
2666 | * longer needed because the block has been demoted. | |
2667 | */ | |
2668 | bio_endio(bio, 0); | |
8c081b52 JT |
2669 | cell_defer(cache, *cell, false); |
2670 | r = DM_MAPIO_SUBMITTED; | |
2671 | ||
2672 | } else | |
c6b4fcba | 2673 | remap_to_origin_clear_discard(cache, bio, block); |
8c081b52 | 2674 | |
c6b4fcba JT |
2675 | break; |
2676 | ||
2677 | default: | |
2678 | DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__, | |
2679 | (unsigned) lookup_result.op); | |
8c081b52 | 2680 | cell_defer(cache, *cell, false); |
c6b4fcba | 2681 | bio_io_error(bio); |
2ee57d58 | 2682 | r = DM_MAPIO_SUBMITTED; |
c6b4fcba JT |
2683 | } |
2684 | ||
2ee57d58 | 2685 | return r; |
c6b4fcba JT |
2686 | } |
2687 | ||
8c081b52 JT |
2688 | static int cache_map(struct dm_target *ti, struct bio *bio) |
2689 | { | |
2690 | int r; | |
2691 | struct dm_bio_prison_cell *cell; | |
2692 | struct cache *cache = ti->private; | |
2693 | ||
2694 | r = __cache_map(cache, bio, &cell); | |
2695 | if (r == DM_MAPIO_REMAPPED) { | |
2696 | inc_ds(cache, bio, cell); | |
2697 | cell_defer(cache, cell, false); | |
2698 | } | |
2699 | ||
2700 | return r; | |
2701 | } | |
2702 | ||
c6b4fcba JT |
2703 | static int cache_end_io(struct dm_target *ti, struct bio *bio, int error) |
2704 | { | |
2705 | struct cache *cache = ti->private; | |
2706 | unsigned long flags; | |
19b0092e MS |
2707 | size_t pb_data_size = get_per_bio_data_size(cache); |
2708 | struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size); | |
c6b4fcba JT |
2709 | |
2710 | if (pb->tick) { | |
2711 | policy_tick(cache->policy); | |
2712 | ||
2713 | spin_lock_irqsave(&cache->lock, flags); | |
2714 | cache->need_tick_bio = true; | |
2715 | spin_unlock_irqrestore(&cache->lock, flags); | |
2716 | } | |
2717 | ||
2718 | check_for_quiesced_migrations(cache, pb); | |
2719 | ||
2720 | return 0; | |
2721 | } | |
2722 | ||
2723 | static int write_dirty_bitset(struct cache *cache) | |
2724 | { | |
2725 | unsigned i, r; | |
2726 | ||
2727 | for (i = 0; i < from_cblock(cache->cache_size); i++) { | |
2728 | r = dm_cache_set_dirty(cache->cmd, to_cblock(i), | |
2729 | is_dirty(cache, to_cblock(i))); | |
2730 | if (r) | |
2731 | return r; | |
2732 | } | |
2733 | ||
2734 | return 0; | |
2735 | } | |
2736 | ||
2737 | static int write_discard_bitset(struct cache *cache) | |
2738 | { | |
2739 | unsigned i, r; | |
2740 | ||
1bad9bc4 JT |
2741 | r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size, |
2742 | cache->discard_nr_blocks); | |
c6b4fcba JT |
2743 | if (r) { |
2744 | DMERR("could not resize on-disk discard bitset"); | |
2745 | return r; | |
2746 | } | |
2747 | ||
1bad9bc4 JT |
2748 | for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) { |
2749 | r = dm_cache_set_discard(cache->cmd, to_dblock(i), | |
2750 | is_discarded(cache, to_dblock(i))); | |
c6b4fcba JT |
2751 | if (r) |
2752 | return r; | |
2753 | } | |
2754 | ||
2755 | return 0; | |
2756 | } | |
2757 | ||
c6b4fcba JT |
2758 | /* |
2759 | * returns true on success | |
2760 | */ | |
2761 | static bool sync_metadata(struct cache *cache) | |
2762 | { | |
2763 | int r1, r2, r3, r4; | |
2764 | ||
2765 | r1 = write_dirty_bitset(cache); | |
2766 | if (r1) | |
2767 | DMERR("could not write dirty bitset"); | |
2768 | ||
2769 | r2 = write_discard_bitset(cache); | |
2770 | if (r2) | |
2771 | DMERR("could not write discard bitset"); | |
2772 | ||
2773 | save_stats(cache); | |
2774 | ||
0596661f | 2775 | r3 = dm_cache_write_hints(cache->cmd, cache->policy); |
c6b4fcba JT |
2776 | if (r3) |
2777 | DMERR("could not write hints"); | |
2778 | ||
2779 | /* | |
2780 | * If writing the above metadata failed, we still commit, but don't | |
2781 | * set the clean shutdown flag. This will effectively force every | |
2782 | * dirty bit to be set on reload. | |
2783 | */ | |
2784 | r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3); | |
2785 | if (r4) | |
2786 | DMERR("could not write cache metadata. Data loss may occur."); | |
2787 | ||
2788 | return !r1 && !r2 && !r3 && !r4; | |
2789 | } | |
2790 | ||
2791 | static void cache_postsuspend(struct dm_target *ti) | |
2792 | { | |
2793 | struct cache *cache = ti->private; | |
2794 | ||
2795 | start_quiescing(cache); | |
2796 | wait_for_migrations(cache); | |
2797 | stop_worker(cache); | |
2798 | requeue_deferred_io(cache); | |
2799 | stop_quiescing(cache); | |
2800 | ||
2801 | (void) sync_metadata(cache); | |
2802 | } | |
2803 | ||
2804 | static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock, | |
2805 | bool dirty, uint32_t hint, bool hint_valid) | |
2806 | { | |
2807 | int r; | |
2808 | struct cache *cache = context; | |
2809 | ||
2810 | r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid); | |
2811 | if (r) | |
2812 | return r; | |
2813 | ||
2814 | if (dirty) | |
2815 | set_dirty(cache, oblock, cblock); | |
2816 | else | |
2817 | clear_dirty(cache, oblock, cblock); | |
2818 | ||
2819 | return 0; | |
2820 | } | |
2821 | ||
3e2e1c30 JT |
2822 | /* |
2823 | * The discard block size in the on disk metadata is not | |
2824 | * neccessarily the same as we're currently using. So we have to | |
2825 | * be careful to only set the discarded attribute if we know it | |
2826 | * covers a complete block of the new size. | |
2827 | */ | |
2828 | struct discard_load_info { | |
2829 | struct cache *cache; | |
2830 | ||
2831 | /* | |
2832 | * These blocks are sized using the on disk dblock size, rather | |
2833 | * than the current one. | |
2834 | */ | |
2835 | dm_block_t block_size; | |
2836 | dm_block_t discard_begin, discard_end; | |
2837 | }; | |
2838 | ||
2839 | static void discard_load_info_init(struct cache *cache, | |
2840 | struct discard_load_info *li) | |
2841 | { | |
2842 | li->cache = cache; | |
2843 | li->discard_begin = li->discard_end = 0; | |
2844 | } | |
2845 | ||
2846 | static void set_discard_range(struct discard_load_info *li) | |
2847 | { | |
2848 | sector_t b, e; | |
2849 | ||
2850 | if (li->discard_begin == li->discard_end) | |
2851 | return; | |
2852 | ||
2853 | /* | |
2854 | * Convert to sectors. | |
2855 | */ | |
2856 | b = li->discard_begin * li->block_size; | |
2857 | e = li->discard_end * li->block_size; | |
2858 | ||
2859 | /* | |
2860 | * Then convert back to the current dblock size. | |
2861 | */ | |
2862 | b = dm_sector_div_up(b, li->cache->discard_block_size); | |
2863 | sector_div(e, li->cache->discard_block_size); | |
2864 | ||
2865 | /* | |
2866 | * The origin may have shrunk, so we need to check we're still in | |
2867 | * bounds. | |
2868 | */ | |
2869 | if (e > from_dblock(li->cache->discard_nr_blocks)) | |
2870 | e = from_dblock(li->cache->discard_nr_blocks); | |
2871 | ||
2872 | for (; b < e; b++) | |
2873 | set_discard(li->cache, to_dblock(b)); | |
2874 | } | |
2875 | ||
c6b4fcba | 2876 | static int load_discard(void *context, sector_t discard_block_size, |
1bad9bc4 | 2877 | dm_dblock_t dblock, bool discard) |
c6b4fcba | 2878 | { |
3e2e1c30 | 2879 | struct discard_load_info *li = context; |
c6b4fcba | 2880 | |
3e2e1c30 | 2881 | li->block_size = discard_block_size; |
1bad9bc4 | 2882 | |
3e2e1c30 JT |
2883 | if (discard) { |
2884 | if (from_dblock(dblock) == li->discard_end) | |
2885 | /* | |
2886 | * We're already in a discard range, just extend it. | |
2887 | */ | |
2888 | li->discard_end = li->discard_end + 1ULL; | |
2889 | ||
2890 | else { | |
2891 | /* | |
2892 | * Emit the old range and start a new one. | |
2893 | */ | |
2894 | set_discard_range(li); | |
2895 | li->discard_begin = from_dblock(dblock); | |
2896 | li->discard_end = li->discard_begin + 1ULL; | |
2897 | } | |
2898 | } else { | |
2899 | set_discard_range(li); | |
2900 | li->discard_begin = li->discard_end = 0; | |
2901 | } | |
c6b4fcba JT |
2902 | |
2903 | return 0; | |
2904 | } | |
2905 | ||
f494a9c6 JT |
2906 | static dm_cblock_t get_cache_dev_size(struct cache *cache) |
2907 | { | |
2908 | sector_t size = get_dev_size(cache->cache_dev); | |
2909 | (void) sector_div(size, cache->sectors_per_block); | |
2910 | return to_cblock(size); | |
2911 | } | |
2912 | ||
2913 | static bool can_resize(struct cache *cache, dm_cblock_t new_size) | |
2914 | { | |
2915 | if (from_cblock(new_size) > from_cblock(cache->cache_size)) | |
2916 | return true; | |
2917 | ||
2918 | /* | |
2919 | * We can't drop a dirty block when shrinking the cache. | |
2920 | */ | |
2921 | while (from_cblock(new_size) < from_cblock(cache->cache_size)) { | |
2922 | new_size = to_cblock(from_cblock(new_size) + 1); | |
2923 | if (is_dirty(cache, new_size)) { | |
2924 | DMERR("unable to shrink cache; cache block %llu is dirty", | |
2925 | (unsigned long long) from_cblock(new_size)); | |
2926 | return false; | |
2927 | } | |
2928 | } | |
2929 | ||
2930 | return true; | |
2931 | } | |
2932 | ||
2933 | static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size) | |
2934 | { | |
2935 | int r; | |
2936 | ||
08844800 | 2937 | r = dm_cache_resize(cache->cmd, new_size); |
f494a9c6 JT |
2938 | if (r) { |
2939 | DMERR("could not resize cache metadata"); | |
2940 | return r; | |
2941 | } | |
2942 | ||
d1d9220c | 2943 | set_cache_size(cache, new_size); |
f494a9c6 JT |
2944 | |
2945 | return 0; | |
2946 | } | |
2947 | ||
c6b4fcba JT |
2948 | static int cache_preresume(struct dm_target *ti) |
2949 | { | |
2950 | int r = 0; | |
2951 | struct cache *cache = ti->private; | |
f494a9c6 | 2952 | dm_cblock_t csize = get_cache_dev_size(cache); |
c6b4fcba JT |
2953 | |
2954 | /* | |
2955 | * Check to see if the cache has resized. | |
2956 | */ | |
f494a9c6 JT |
2957 | if (!cache->sized) { |
2958 | r = resize_cache_dev(cache, csize); | |
2959 | if (r) | |
c6b4fcba | 2960 | return r; |
c6b4fcba JT |
2961 | |
2962 | cache->sized = true; | |
f494a9c6 JT |
2963 | |
2964 | } else if (csize != cache->cache_size) { | |
2965 | if (!can_resize(cache, csize)) | |
2966 | return -EINVAL; | |
2967 | ||
2968 | r = resize_cache_dev(cache, csize); | |
2969 | if (r) | |
2970 | return r; | |
c6b4fcba JT |
2971 | } |
2972 | ||
2973 | if (!cache->loaded_mappings) { | |
ea2dd8c1 | 2974 | r = dm_cache_load_mappings(cache->cmd, cache->policy, |
c6b4fcba JT |
2975 | load_mapping, cache); |
2976 | if (r) { | |
2977 | DMERR("could not load cache mappings"); | |
2978 | return r; | |
2979 | } | |
2980 | ||
2981 | cache->loaded_mappings = true; | |
2982 | } | |
2983 | ||
2984 | if (!cache->loaded_discards) { | |
3e2e1c30 JT |
2985 | struct discard_load_info li; |
2986 | ||
2987 | /* | |
2988 | * The discard bitset could have been resized, or the | |
2989 | * discard block size changed. To be safe we start by | |
2990 | * setting every dblock to not discarded. | |
2991 | */ | |
2992 | clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks)); | |
2993 | ||
2994 | discard_load_info_init(cache, &li); | |
2995 | r = dm_cache_load_discards(cache->cmd, load_discard, &li); | |
c6b4fcba JT |
2996 | if (r) { |
2997 | DMERR("could not load origin discards"); | |
2998 | return r; | |
2999 | } | |
3e2e1c30 | 3000 | set_discard_range(&li); |
c6b4fcba JT |
3001 | |
3002 | cache->loaded_discards = true; | |
3003 | } | |
3004 | ||
3005 | return r; | |
3006 | } | |
3007 | ||
3008 | static void cache_resume(struct dm_target *ti) | |
3009 | { | |
3010 | struct cache *cache = ti->private; | |
3011 | ||
3012 | cache->need_tick_bio = true; | |
3013 | do_waker(&cache->waker.work); | |
3014 | } | |
3015 | ||
3016 | /* | |
3017 | * Status format: | |
3018 | * | |
6a388618 MS |
3019 | * <metadata block size> <#used metadata blocks>/<#total metadata blocks> |
3020 | * <cache block size> <#used cache blocks>/<#total cache blocks> | |
c6b4fcba | 3021 | * <#read hits> <#read misses> <#write hits> <#write misses> |
6a388618 | 3022 | * <#demotions> <#promotions> <#dirty> |
c6b4fcba JT |
3023 | * <#features> <features>* |
3024 | * <#core args> <core args> | |
2e68c4e6 | 3025 | * <policy name> <#policy args> <policy args>* |
c6b4fcba JT |
3026 | */ |
3027 | static void cache_status(struct dm_target *ti, status_type_t type, | |
3028 | unsigned status_flags, char *result, unsigned maxlen) | |
3029 | { | |
3030 | int r = 0; | |
3031 | unsigned i; | |
3032 | ssize_t sz = 0; | |
3033 | dm_block_t nr_free_blocks_metadata = 0; | |
3034 | dm_block_t nr_blocks_metadata = 0; | |
3035 | char buf[BDEVNAME_SIZE]; | |
3036 | struct cache *cache = ti->private; | |
3037 | dm_cblock_t residency; | |
3038 | ||
3039 | switch (type) { | |
3040 | case STATUSTYPE_INFO: | |
3041 | /* Commit to ensure statistics aren't out-of-date */ | |
3042 | if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) { | |
3043 | r = dm_cache_commit(cache->cmd, false); | |
3044 | if (r) | |
3045 | DMERR("could not commit metadata for accurate status"); | |
3046 | } | |
3047 | ||
3048 | r = dm_cache_get_free_metadata_block_count(cache->cmd, | |
3049 | &nr_free_blocks_metadata); | |
3050 | if (r) { | |
3051 | DMERR("could not get metadata free block count"); | |
3052 | goto err; | |
3053 | } | |
3054 | ||
3055 | r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata); | |
3056 | if (r) { | |
3057 | DMERR("could not get metadata device size"); | |
3058 | goto err; | |
3059 | } | |
3060 | ||
3061 | residency = policy_residency(cache->policy); | |
3062 | ||
44fa816b | 3063 | DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ", |
895b47d7 | 3064 | (unsigned)DM_CACHE_METADATA_BLOCK_SIZE, |
c6b4fcba JT |
3065 | (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata), |
3066 | (unsigned long long)nr_blocks_metadata, | |
6a388618 MS |
3067 | cache->sectors_per_block, |
3068 | (unsigned long long) from_cblock(residency), | |
3069 | (unsigned long long) from_cblock(cache->cache_size), | |
c6b4fcba JT |
3070 | (unsigned) atomic_read(&cache->stats.read_hit), |
3071 | (unsigned) atomic_read(&cache->stats.read_miss), | |
3072 | (unsigned) atomic_read(&cache->stats.write_hit), | |
3073 | (unsigned) atomic_read(&cache->stats.write_miss), | |
3074 | (unsigned) atomic_read(&cache->stats.demotion), | |
3075 | (unsigned) atomic_read(&cache->stats.promotion), | |
44fa816b | 3076 | (unsigned long) atomic_read(&cache->nr_dirty)); |
c6b4fcba | 3077 | |
2ee57d58 | 3078 | if (writethrough_mode(&cache->features)) |
c6b4fcba | 3079 | DMEMIT("1 writethrough "); |
2ee57d58 JT |
3080 | |
3081 | else if (passthrough_mode(&cache->features)) | |
3082 | DMEMIT("1 passthrough "); | |
3083 | ||
3084 | else if (writeback_mode(&cache->features)) | |
3085 | DMEMIT("1 writeback "); | |
3086 | ||
3087 | else { | |
3088 | DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode); | |
3089 | goto err; | |
3090 | } | |
c6b4fcba JT |
3091 | |
3092 | DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold); | |
2e68c4e6 MS |
3093 | |
3094 | DMEMIT("%s ", dm_cache_policy_get_name(cache->policy)); | |
c6b4fcba JT |
3095 | if (sz < maxlen) { |
3096 | r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz); | |
3097 | if (r) | |
3098 | DMERR("policy_emit_config_values returned %d", r); | |
3099 | } | |
3100 | ||
3101 | break; | |
3102 | ||
3103 | case STATUSTYPE_TABLE: | |
3104 | format_dev_t(buf, cache->metadata_dev->bdev->bd_dev); | |
3105 | DMEMIT("%s ", buf); | |
3106 | format_dev_t(buf, cache->cache_dev->bdev->bd_dev); | |
3107 | DMEMIT("%s ", buf); | |
3108 | format_dev_t(buf, cache->origin_dev->bdev->bd_dev); | |
3109 | DMEMIT("%s", buf); | |
3110 | ||
3111 | for (i = 0; i < cache->nr_ctr_args - 1; i++) | |
3112 | DMEMIT(" %s", cache->ctr_args[i]); | |
3113 | if (cache->nr_ctr_args) | |
3114 | DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]); | |
3115 | } | |
3116 | ||
3117 | return; | |
3118 | ||
3119 | err: | |
3120 | DMEMIT("Error"); | |
3121 | } | |
3122 | ||
c6b4fcba | 3123 | /* |
65790ff9 JT |
3124 | * A cache block range can take two forms: |
3125 | * | |
3126 | * i) A single cblock, eg. '3456' | |
3127 | * ii) A begin and end cblock with dots between, eg. 123-234 | |
3128 | */ | |
3129 | static int parse_cblock_range(struct cache *cache, const char *str, | |
3130 | struct cblock_range *result) | |
3131 | { | |
3132 | char dummy; | |
3133 | uint64_t b, e; | |
3134 | int r; | |
3135 | ||
3136 | /* | |
3137 | * Try and parse form (ii) first. | |
3138 | */ | |
3139 | r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy); | |
3140 | if (r < 0) | |
3141 | return r; | |
3142 | ||
3143 | if (r == 2) { | |
3144 | result->begin = to_cblock(b); | |
3145 | result->end = to_cblock(e); | |
3146 | return 0; | |
3147 | } | |
3148 | ||
3149 | /* | |
3150 | * That didn't work, try form (i). | |
3151 | */ | |
3152 | r = sscanf(str, "%llu%c", &b, &dummy); | |
3153 | if (r < 0) | |
3154 | return r; | |
3155 | ||
3156 | if (r == 1) { | |
3157 | result->begin = to_cblock(b); | |
3158 | result->end = to_cblock(from_cblock(result->begin) + 1u); | |
3159 | return 0; | |
3160 | } | |
3161 | ||
3162 | DMERR("invalid cblock range '%s'", str); | |
3163 | return -EINVAL; | |
3164 | } | |
3165 | ||
3166 | static int validate_cblock_range(struct cache *cache, struct cblock_range *range) | |
3167 | { | |
3168 | uint64_t b = from_cblock(range->begin); | |
3169 | uint64_t e = from_cblock(range->end); | |
3170 | uint64_t n = from_cblock(cache->cache_size); | |
3171 | ||
3172 | if (b >= n) { | |
3173 | DMERR("begin cblock out of range: %llu >= %llu", b, n); | |
3174 | return -EINVAL; | |
3175 | } | |
3176 | ||
3177 | if (e > n) { | |
3178 | DMERR("end cblock out of range: %llu > %llu", e, n); | |
3179 | return -EINVAL; | |
3180 | } | |
3181 | ||
3182 | if (b >= e) { | |
3183 | DMERR("invalid cblock range: %llu >= %llu", b, e); | |
3184 | return -EINVAL; | |
3185 | } | |
3186 | ||
3187 | return 0; | |
3188 | } | |
3189 | ||
3190 | static int request_invalidation(struct cache *cache, struct cblock_range *range) | |
3191 | { | |
3192 | struct invalidation_request req; | |
3193 | ||
3194 | INIT_LIST_HEAD(&req.list); | |
3195 | req.cblocks = range; | |
3196 | atomic_set(&req.complete, 0); | |
3197 | req.err = 0; | |
3198 | init_waitqueue_head(&req.result_wait); | |
3199 | ||
3200 | spin_lock(&cache->invalidation_lock); | |
3201 | list_add(&req.list, &cache->invalidation_requests); | |
3202 | spin_unlock(&cache->invalidation_lock); | |
3203 | wake_worker(cache); | |
3204 | ||
3205 | wait_event(req.result_wait, atomic_read(&req.complete)); | |
3206 | return req.err; | |
3207 | } | |
3208 | ||
3209 | static int process_invalidate_cblocks_message(struct cache *cache, unsigned count, | |
3210 | const char **cblock_ranges) | |
3211 | { | |
3212 | int r = 0; | |
3213 | unsigned i; | |
3214 | struct cblock_range range; | |
3215 | ||
3216 | if (!passthrough_mode(&cache->features)) { | |
3217 | DMERR("cache has to be in passthrough mode for invalidation"); | |
3218 | return -EPERM; | |
3219 | } | |
3220 | ||
3221 | for (i = 0; i < count; i++) { | |
3222 | r = parse_cblock_range(cache, cblock_ranges[i], &range); | |
3223 | if (r) | |
3224 | break; | |
3225 | ||
3226 | r = validate_cblock_range(cache, &range); | |
3227 | if (r) | |
3228 | break; | |
3229 | ||
3230 | /* | |
3231 | * Pass begin and end origin blocks to the worker and wake it. | |
3232 | */ | |
3233 | r = request_invalidation(cache, &range); | |
3234 | if (r) | |
3235 | break; | |
3236 | } | |
3237 | ||
3238 | return r; | |
3239 | } | |
3240 | ||
3241 | /* | |
3242 | * Supports | |
3243 | * "<key> <value>" | |
3244 | * and | |
3245 | * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]* | |
c6b4fcba JT |
3246 | * |
3247 | * The key migration_threshold is supported by the cache target core. | |
3248 | */ | |
3249 | static int cache_message(struct dm_target *ti, unsigned argc, char **argv) | |
3250 | { | |
c6b4fcba JT |
3251 | struct cache *cache = ti->private; |
3252 | ||
65790ff9 JT |
3253 | if (!argc) |
3254 | return -EINVAL; | |
3255 | ||
7b6b2bc9 | 3256 | if (!strcasecmp(argv[0], "invalidate_cblocks")) |
65790ff9 JT |
3257 | return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1); |
3258 | ||
c6b4fcba JT |
3259 | if (argc != 2) |
3260 | return -EINVAL; | |
3261 | ||
2f14f4b5 | 3262 | return set_config_value(cache, argv[0], argv[1]); |
c6b4fcba JT |
3263 | } |
3264 | ||
3265 | static int cache_iterate_devices(struct dm_target *ti, | |
3266 | iterate_devices_callout_fn fn, void *data) | |
3267 | { | |
3268 | int r = 0; | |
3269 | struct cache *cache = ti->private; | |
3270 | ||
3271 | r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data); | |
3272 | if (!r) | |
3273 | r = fn(ti, cache->origin_dev, 0, ti->len, data); | |
3274 | ||
3275 | return r; | |
3276 | } | |
3277 | ||
3278 | /* | |
3279 | * We assume I/O is going to the origin (which is the volume | |
3280 | * more likely to have restrictions e.g. by being striped). | |
3281 | * (Looking up the exact location of the data would be expensive | |
3282 | * and could always be out of date by the time the bio is submitted.) | |
3283 | */ | |
3284 | static int cache_bvec_merge(struct dm_target *ti, | |
3285 | struct bvec_merge_data *bvm, | |
3286 | struct bio_vec *biovec, int max_size) | |
3287 | { | |
3288 | struct cache *cache = ti->private; | |
3289 | struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev); | |
3290 | ||
3291 | if (!q->merge_bvec_fn) | |
3292 | return max_size; | |
3293 | ||
3294 | bvm->bi_bdev = cache->origin_dev->bdev; | |
3295 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); | |
3296 | } | |
3297 | ||
3298 | static void set_discard_limits(struct cache *cache, struct queue_limits *limits) | |
3299 | { | |
3300 | /* | |
3301 | * FIXME: these limits may be incompatible with the cache device | |
3302 | */ | |
7ae34e77 JT |
3303 | limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024, |
3304 | cache->origin_sectors); | |
1bad9bc4 | 3305 | limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT; |
c6b4fcba JT |
3306 | } |
3307 | ||
3308 | static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits) | |
3309 | { | |
3310 | struct cache *cache = ti->private; | |
f6109372 | 3311 | uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT; |
c6b4fcba | 3312 | |
f6109372 MS |
3313 | /* |
3314 | * If the system-determined stacked limits are compatible with the | |
3315 | * cache's blocksize (io_opt is a factor) do not override them. | |
3316 | */ | |
3317 | if (io_opt_sectors < cache->sectors_per_block || | |
3318 | do_div(io_opt_sectors, cache->sectors_per_block)) { | |
b0246530 | 3319 | blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT); |
f6109372 MS |
3320 | blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT); |
3321 | } | |
c6b4fcba JT |
3322 | set_discard_limits(cache, limits); |
3323 | } | |
3324 | ||
3325 | /*----------------------------------------------------------------*/ | |
3326 | ||
3327 | static struct target_type cache_target = { | |
3328 | .name = "cache", | |
7ae34e77 | 3329 | .version = {1, 6, 0}, |
c6b4fcba JT |
3330 | .module = THIS_MODULE, |
3331 | .ctr = cache_ctr, | |
3332 | .dtr = cache_dtr, | |
3333 | .map = cache_map, | |
3334 | .end_io = cache_end_io, | |
3335 | .postsuspend = cache_postsuspend, | |
3336 | .preresume = cache_preresume, | |
3337 | .resume = cache_resume, | |
3338 | .status = cache_status, | |
3339 | .message = cache_message, | |
3340 | .iterate_devices = cache_iterate_devices, | |
3341 | .merge = cache_bvec_merge, | |
3342 | .io_hints = cache_io_hints, | |
3343 | }; | |
3344 | ||
3345 | static int __init dm_cache_init(void) | |
3346 | { | |
3347 | int r; | |
3348 | ||
3349 | r = dm_register_target(&cache_target); | |
3350 | if (r) { | |
3351 | DMERR("cache target registration failed: %d", r); | |
3352 | return r; | |
3353 | } | |
3354 | ||
3355 | migration_cache = KMEM_CACHE(dm_cache_migration, 0); | |
3356 | if (!migration_cache) { | |
3357 | dm_unregister_target(&cache_target); | |
3358 | return -ENOMEM; | |
3359 | } | |
3360 | ||
3361 | return 0; | |
3362 | } | |
3363 | ||
3364 | static void __exit dm_cache_exit(void) | |
3365 | { | |
3366 | dm_unregister_target(&cache_target); | |
3367 | kmem_cache_destroy(migration_cache); | |
3368 | } | |
3369 | ||
3370 | module_init(dm_cache_init); | |
3371 | module_exit(dm_cache_exit); | |
3372 | ||
3373 | MODULE_DESCRIPTION(DM_NAME " cache target"); | |
3374 | MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>"); | |
3375 | MODULE_LICENSE("GPL"); |