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