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