]> Git Repo - linux.git/blame - drivers/md/dm-thin.c
dm thin: use __blkdev_issue_discard for async discard support
[linux.git] / drivers / md / dm-thin.c
CommitLineData
991d9fa0 1/*
e49e5829 2 * Copyright (C) 2011-2012 Red Hat UK.
991d9fa0
JT
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-thin-metadata.h"
4f81a417 8#include "dm-bio-prison.h"
1f4e0ff0 9#include "dm.h"
991d9fa0
JT
10
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
0f30af98 14#include <linux/jiffies.h>
604ea906 15#include <linux/log2.h>
991d9fa0 16#include <linux/list.h>
c140e1c4 17#include <linux/rculist.h>
991d9fa0
JT
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/slab.h>
a822c83e 21#include <linux/vmalloc.h>
ac4c3f34 22#include <linux/sort.h>
67324ea1 23#include <linux/rbtree.h>
991d9fa0
JT
24
25#define DM_MSG_PREFIX "thin"
26
27/*
28 * Tunable constants
29 */
7768ed33 30#define ENDIO_HOOK_POOL_SIZE 1024
991d9fa0 31#define MAPPING_POOL_SIZE 1024
905e51b3 32#define COMMIT_PERIOD HZ
80c57893
MS
33#define NO_SPACE_TIMEOUT_SECS 60
34
35static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
991d9fa0 36
df5d2e90
MP
37DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
38 "A percentage of time allocated for copy on write");
39
991d9fa0
JT
40/*
41 * The block size of the device holding pool data must be
42 * between 64KB and 1GB.
43 */
44#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
45#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
46
991d9fa0
JT
47/*
48 * Device id is restricted to 24 bits.
49 */
50#define MAX_DEV_ID ((1 << 24) - 1)
51
52/*
53 * How do we handle breaking sharing of data blocks?
54 * =================================================
55 *
56 * We use a standard copy-on-write btree to store the mappings for the
57 * devices (note I'm talking about copy-on-write of the metadata here, not
58 * the data). When you take an internal snapshot you clone the root node
59 * of the origin btree. After this there is no concept of an origin or a
60 * snapshot. They are just two device trees that happen to point to the
61 * same data blocks.
62 *
63 * When we get a write in we decide if it's to a shared data block using
64 * some timestamp magic. If it is, we have to break sharing.
65 *
66 * Let's say we write to a shared block in what was the origin. The
67 * steps are:
68 *
69 * i) plug io further to this physical block. (see bio_prison code).
70 *
71 * ii) quiesce any read io to that shared data block. Obviously
44feb387 72 * including all devices that share this block. (see dm_deferred_set code)
991d9fa0
JT
73 *
74 * iii) copy the data block to a newly allocate block. This step can be
75 * missed out if the io covers the block. (schedule_copy).
76 *
77 * iv) insert the new mapping into the origin's btree
fe878f34 78 * (process_prepared_mapping). This act of inserting breaks some
991d9fa0
JT
79 * sharing of btree nodes between the two devices. Breaking sharing only
80 * effects the btree of that specific device. Btrees for the other
81 * devices that share the block never change. The btree for the origin
82 * device as it was after the last commit is untouched, ie. we're using
83 * persistent data structures in the functional programming sense.
84 *
85 * v) unplug io to this physical block, including the io that triggered
86 * the breaking of sharing.
87 *
88 * Steps (ii) and (iii) occur in parallel.
89 *
90 * The metadata _doesn't_ need to be committed before the io continues. We
91 * get away with this because the io is always written to a _new_ block.
92 * If there's a crash, then:
93 *
94 * - The origin mapping will point to the old origin block (the shared
95 * one). This will contain the data as it was before the io that triggered
96 * the breaking of sharing came in.
97 *
98 * - The snap mapping still points to the old block. As it would after
99 * the commit.
100 *
101 * The downside of this scheme is the timestamp magic isn't perfect, and
102 * will continue to think that data block in the snapshot device is shared
103 * even after the write to the origin has broken sharing. I suspect data
104 * blocks will typically be shared by many different devices, so we're
105 * breaking sharing n + 1 times, rather than n, where n is the number of
106 * devices that reference this data block. At the moment I think the
107 * benefits far, far outweigh the disadvantages.
108 */
109
110/*----------------------------------------------------------------*/
111
991d9fa0
JT
112/*
113 * Key building.
114 */
34fbcf62
JT
115enum lock_space {
116 VIRTUAL,
117 PHYSICAL
118};
119
120static void build_key(struct dm_thin_device *td, enum lock_space ls,
121 dm_block_t b, dm_block_t e, struct dm_cell_key *key)
991d9fa0 122{
34fbcf62 123 key->virtual = (ls == VIRTUAL);
991d9fa0 124 key->dev = dm_thin_dev_id(td);
5f274d88 125 key->block_begin = b;
34fbcf62
JT
126 key->block_end = e;
127}
128
129static void build_data_key(struct dm_thin_device *td, dm_block_t b,
130 struct dm_cell_key *key)
131{
132 build_key(td, PHYSICAL, b, b + 1llu, key);
991d9fa0
JT
133}
134
135static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
44feb387 136 struct dm_cell_key *key)
991d9fa0 137{
34fbcf62 138 build_key(td, VIRTUAL, b, b + 1llu, key);
991d9fa0
JT
139}
140
141/*----------------------------------------------------------------*/
142
7d327fe0
JT
143#define THROTTLE_THRESHOLD (1 * HZ)
144
145struct throttle {
146 struct rw_semaphore lock;
147 unsigned long threshold;
148 bool throttle_applied;
149};
150
151static void throttle_init(struct throttle *t)
152{
153 init_rwsem(&t->lock);
154 t->throttle_applied = false;
155}
156
157static void throttle_work_start(struct throttle *t)
158{
159 t->threshold = jiffies + THROTTLE_THRESHOLD;
160}
161
162static void throttle_work_update(struct throttle *t)
163{
164 if (!t->throttle_applied && jiffies > t->threshold) {
165 down_write(&t->lock);
166 t->throttle_applied = true;
167 }
168}
169
170static void throttle_work_complete(struct throttle *t)
171{
172 if (t->throttle_applied) {
173 t->throttle_applied = false;
174 up_write(&t->lock);
175 }
176}
177
178static void throttle_lock(struct throttle *t)
179{
180 down_read(&t->lock);
181}
182
183static void throttle_unlock(struct throttle *t)
184{
185 up_read(&t->lock);
186}
187
188/*----------------------------------------------------------------*/
189
991d9fa0
JT
190/*
191 * A pool device ties together a metadata device and a data device. It
192 * also provides the interface for creating and destroying internal
193 * devices.
194 */
a24c2569 195struct dm_thin_new_mapping;
67e2e2b2 196
e49e5829 197/*
3e1a0699 198 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
e49e5829
JT
199 */
200enum pool_mode {
201 PM_WRITE, /* metadata may be changed */
3e1a0699 202 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
e49e5829
JT
203 PM_READ_ONLY, /* metadata may not be changed */
204 PM_FAIL, /* all I/O fails */
205};
206
67e2e2b2 207struct pool_features {
e49e5829
JT
208 enum pool_mode mode;
209
9bc142dd
MS
210 bool zero_new_blocks:1;
211 bool discard_enabled:1;
212 bool discard_passdown:1;
787a996c 213 bool error_if_no_space:1;
67e2e2b2
JT
214};
215
e49e5829
JT
216struct thin_c;
217typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
a374bb21 218typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
e49e5829
JT
219typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
220
ac4c3f34
JT
221#define CELL_SORT_ARRAY_SIZE 8192
222
991d9fa0
JT
223struct pool {
224 struct list_head list;
225 struct dm_target *ti; /* Only set if a pool target is bound */
226
227 struct mapped_device *pool_md;
228 struct block_device *md_dev;
229 struct dm_pool_metadata *pmd;
230
991d9fa0 231 dm_block_t low_water_blocks;
55f2b8bd 232 uint32_t sectors_per_block;
f9a8e0cd 233 int sectors_per_block_shift;
991d9fa0 234
67e2e2b2 235 struct pool_features pf;
88a6621b 236 bool low_water_triggered:1; /* A dm event has been sent */
80e96c54 237 bool suspended:1;
c3667cc6 238 bool out_of_data_space:1;
991d9fa0 239
44feb387 240 struct dm_bio_prison *prison;
991d9fa0
JT
241 struct dm_kcopyd_client *copier;
242
243 struct workqueue_struct *wq;
7d327fe0 244 struct throttle throttle;
991d9fa0 245 struct work_struct worker;
905e51b3 246 struct delayed_work waker;
85ad643b 247 struct delayed_work no_space_timeout;
991d9fa0 248
905e51b3 249 unsigned long last_commit_jiffies;
55f2b8bd 250 unsigned ref_count;
991d9fa0
JT
251
252 spinlock_t lock;
991d9fa0
JT
253 struct bio_list deferred_flush_bios;
254 struct list_head prepared_mappings;
104655fd 255 struct list_head prepared_discards;
c140e1c4 256 struct list_head active_thins;
991d9fa0 257
44feb387
MS
258 struct dm_deferred_set *shared_read_ds;
259 struct dm_deferred_set *all_io_ds;
991d9fa0 260
a24c2569 261 struct dm_thin_new_mapping *next_mapping;
991d9fa0 262 mempool_t *mapping_pool;
e49e5829
JT
263
264 process_bio_fn process_bio;
265 process_bio_fn process_discard;
266
a374bb21
JT
267 process_cell_fn process_cell;
268 process_cell_fn process_discard_cell;
269
e49e5829
JT
270 process_mapping_fn process_prepared_mapping;
271 process_mapping_fn process_prepared_discard;
ac4c3f34 272
a822c83e 273 struct dm_bio_prison_cell **cell_sort_array;
991d9fa0
JT
274};
275
e49e5829 276static enum pool_mode get_pool_mode(struct pool *pool);
b5330655 277static void metadata_operation_failed(struct pool *pool, const char *op, int r);
e49e5829 278
991d9fa0
JT
279/*
280 * Target context for a pool.
281 */
282struct pool_c {
283 struct dm_target *ti;
284 struct pool *pool;
285 struct dm_dev *data_dev;
286 struct dm_dev *metadata_dev;
287 struct dm_target_callbacks callbacks;
288
289 dm_block_t low_water_blocks;
0424caa1
MS
290 struct pool_features requested_pf; /* Features requested during table load */
291 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
991d9fa0
JT
292};
293
294/*
295 * Target context for a thin.
296 */
297struct thin_c {
c140e1c4 298 struct list_head list;
991d9fa0 299 struct dm_dev *pool_dev;
2dd9c257 300 struct dm_dev *origin_dev;
e5aea7b4 301 sector_t origin_size;
991d9fa0
JT
302 dm_thin_id dev_id;
303
304 struct pool *pool;
305 struct dm_thin_device *td;
583024d2
MS
306 struct mapped_device *thin_md;
307
738211f7 308 bool requeue_mode:1;
c140e1c4 309 spinlock_t lock;
a374bb21 310 struct list_head deferred_cells;
c140e1c4
MS
311 struct bio_list deferred_bio_list;
312 struct bio_list retry_on_resume_list;
67324ea1 313 struct rb_root sort_bio_list; /* sorted list of deferred bios */
b10ebd34
JT
314
315 /*
316 * Ensures the thin is not destroyed until the worker has finished
317 * iterating the active_thins list.
318 */
319 atomic_t refcount;
320 struct completion can_destroy;
991d9fa0
JT
321};
322
323/*----------------------------------------------------------------*/
324
34fbcf62
JT
325static bool block_size_is_power_of_two(struct pool *pool)
326{
327 return pool->sectors_per_block_shift >= 0;
328}
329
330static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
331{
332 return block_size_is_power_of_two(pool) ?
333 (b << pool->sectors_per_block_shift) :
334 (b * pool->sectors_per_block);
335}
336
337static int issue_discard(struct thin_c *tc, dm_block_t data_b, dm_block_t data_e,
338 struct bio *parent_bio)
339{
3dba53a9 340 int type = REQ_WRITE | REQ_DISCARD;
34fbcf62
JT
341 sector_t s = block_to_sectors(tc->pool, data_b);
342 sector_t len = block_to_sectors(tc->pool, data_e - data_b);
3dba53a9
MS
343 struct bio *bio = NULL;
344 struct blk_plug plug;
345 int ret;
346
347 blk_start_plug(&plug);
348 ret = __blkdev_issue_discard(tc->pool_dev->bdev, s, len,
349 GFP_NOWAIT, type, &bio);
350 if (!ret && bio) {
351 bio_chain(bio, parent_bio);
352 submit_bio(type, bio);
353 }
354 blk_finish_plug(&plug);
34fbcf62 355
3dba53a9 356 return ret;
34fbcf62
JT
357}
358
359/*----------------------------------------------------------------*/
360
025b9685
JT
361/*
362 * wake_worker() is used when new work is queued and when pool_resume is
363 * ready to continue deferred IO processing.
364 */
365static void wake_worker(struct pool *pool)
366{
367 queue_work(pool->wq, &pool->worker);
368}
369
370/*----------------------------------------------------------------*/
371
6beca5eb
JT
372static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
373 struct dm_bio_prison_cell **cell_result)
374{
375 int r;
376 struct dm_bio_prison_cell *cell_prealloc;
377
378 /*
379 * Allocate a cell from the prison's mempool.
380 * This might block but it can't fail.
381 */
382 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
383
384 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
385 if (r)
386 /*
387 * We reused an old cell; we can get rid of
388 * the new one.
389 */
390 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
391
392 return r;
393}
394
395static void cell_release(struct pool *pool,
396 struct dm_bio_prison_cell *cell,
397 struct bio_list *bios)
398{
399 dm_cell_release(pool->prison, cell, bios);
400 dm_bio_prison_free_cell(pool->prison, cell);
401}
402
2d759a46
JT
403static void cell_visit_release(struct pool *pool,
404 void (*fn)(void *, struct dm_bio_prison_cell *),
405 void *context,
406 struct dm_bio_prison_cell *cell)
407{
408 dm_cell_visit_release(pool->prison, fn, context, cell);
409 dm_bio_prison_free_cell(pool->prison, cell);
410}
411
6beca5eb
JT
412static void cell_release_no_holder(struct pool *pool,
413 struct dm_bio_prison_cell *cell,
414 struct bio_list *bios)
415{
416 dm_cell_release_no_holder(pool->prison, cell, bios);
417 dm_bio_prison_free_cell(pool->prison, cell);
418}
419
af91805a
MS
420static void cell_error_with_code(struct pool *pool,
421 struct dm_bio_prison_cell *cell, int error_code)
6beca5eb 422{
af91805a 423 dm_cell_error(pool->prison, cell, error_code);
6beca5eb
JT
424 dm_bio_prison_free_cell(pool->prison, cell);
425}
426
c3667cc6
MS
427static int get_pool_io_error_code(struct pool *pool)
428{
429 return pool->out_of_data_space ? -ENOSPC : -EIO;
430}
431
af91805a
MS
432static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
433{
c3667cc6
MS
434 int error = get_pool_io_error_code(pool);
435
436 cell_error_with_code(pool, cell, error);
af91805a
MS
437}
438
a374bb21
JT
439static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
440{
441 cell_error_with_code(pool, cell, 0);
442}
443
444static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
445{
446 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
447}
448
6beca5eb
JT
449/*----------------------------------------------------------------*/
450
991d9fa0
JT
451/*
452 * A global list of pools that uses a struct mapped_device as a key.
453 */
454static struct dm_thin_pool_table {
455 struct mutex mutex;
456 struct list_head pools;
457} dm_thin_pool_table;
458
459static void pool_table_init(void)
460{
461 mutex_init(&dm_thin_pool_table.mutex);
462 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
463}
464
465static void __pool_table_insert(struct pool *pool)
466{
467 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
468 list_add(&pool->list, &dm_thin_pool_table.pools);
469}
470
471static void __pool_table_remove(struct pool *pool)
472{
473 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
474 list_del(&pool->list);
475}
476
477static struct pool *__pool_table_lookup(struct mapped_device *md)
478{
479 struct pool *pool = NULL, *tmp;
480
481 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
482
483 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
484 if (tmp->pool_md == md) {
485 pool = tmp;
486 break;
487 }
488 }
489
490 return pool;
491}
492
493static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
494{
495 struct pool *pool = NULL, *tmp;
496
497 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
498
499 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
500 if (tmp->md_dev == md_dev) {
501 pool = tmp;
502 break;
503 }
504 }
505
506 return pool;
507}
508
509/*----------------------------------------------------------------*/
510
a24c2569 511struct dm_thin_endio_hook {
eb2aa48d 512 struct thin_c *tc;
44feb387
MS
513 struct dm_deferred_entry *shared_read_entry;
514 struct dm_deferred_entry *all_io_entry;
a24c2569 515 struct dm_thin_new_mapping *overwrite_mapping;
67324ea1 516 struct rb_node rb_node;
34fbcf62 517 struct dm_bio_prison_cell *cell;
eb2aa48d
JT
518};
519
42d6a8ce
MS
520static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
521{
522 bio_list_merge(bios, master);
523 bio_list_init(master);
524}
525
526static void error_bio_list(struct bio_list *bios, int error)
991d9fa0
JT
527{
528 struct bio *bio;
42d6a8ce 529
4246a0b6
CH
530 while ((bio = bio_list_pop(bios))) {
531 bio->bi_error = error;
532 bio_endio(bio);
533 }
42d6a8ce
MS
534}
535
536static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
537{
991d9fa0 538 struct bio_list bios;
18adc577 539 unsigned long flags;
991d9fa0
JT
540
541 bio_list_init(&bios);
18adc577 542
c140e1c4 543 spin_lock_irqsave(&tc->lock, flags);
42d6a8ce 544 __merge_bio_list(&bios, master);
c140e1c4 545 spin_unlock_irqrestore(&tc->lock, flags);
991d9fa0 546
42d6a8ce 547 error_bio_list(&bios, error);
991d9fa0
JT
548}
549
a374bb21
JT
550static void requeue_deferred_cells(struct thin_c *tc)
551{
552 struct pool *pool = tc->pool;
553 unsigned long flags;
554 struct list_head cells;
555 struct dm_bio_prison_cell *cell, *tmp;
556
557 INIT_LIST_HEAD(&cells);
558
559 spin_lock_irqsave(&tc->lock, flags);
560 list_splice_init(&tc->deferred_cells, &cells);
561 spin_unlock_irqrestore(&tc->lock, flags);
562
563 list_for_each_entry_safe(cell, tmp, &cells, user_list)
564 cell_requeue(pool, cell);
565}
566
991d9fa0
JT
567static void requeue_io(struct thin_c *tc)
568{
3e1a0699 569 struct bio_list bios;
42d6a8ce 570 unsigned long flags;
3e1a0699
JT
571
572 bio_list_init(&bios);
573
c140e1c4 574 spin_lock_irqsave(&tc->lock, flags);
42d6a8ce
MS
575 __merge_bio_list(&bios, &tc->deferred_bio_list);
576 __merge_bio_list(&bios, &tc->retry_on_resume_list);
c140e1c4 577 spin_unlock_irqrestore(&tc->lock, flags);
3e1a0699 578
42d6a8ce
MS
579 error_bio_list(&bios, DM_ENDIO_REQUEUE);
580 requeue_deferred_cells(tc);
3e1a0699
JT
581}
582
0a927c2f 583static void error_retry_list_with_code(struct pool *pool, int error)
c140e1c4
MS
584{
585 struct thin_c *tc;
586
587 rcu_read_lock();
588 list_for_each_entry_rcu(tc, &pool->active_thins, list)
0a927c2f 589 error_thin_bio_list(tc, &tc->retry_on_resume_list, error);
c140e1c4
MS
590 rcu_read_unlock();
591}
592
0a927c2f
MS
593static void error_retry_list(struct pool *pool)
594{
c3667cc6
MS
595 int error = get_pool_io_error_code(pool);
596
813923b1 597 error_retry_list_with_code(pool, error);
0a927c2f
MS
598}
599
991d9fa0
JT
600/*
601 * This section of code contains the logic for processing a thin device's IO.
602 * Much of the code depends on pool object resources (lists, workqueues, etc)
603 * but most is exclusively called from the thin target rather than the thin-pool
604 * target.
605 */
606
607static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
608{
58f77a21 609 struct pool *pool = tc->pool;
4f024f37 610 sector_t block_nr = bio->bi_iter.bi_sector;
55f2b8bd 611
58f77a21
MS
612 if (block_size_is_power_of_two(pool))
613 block_nr >>= pool->sectors_per_block_shift;
f9a8e0cd 614 else
58f77a21 615 (void) sector_div(block_nr, pool->sectors_per_block);
55f2b8bd
MS
616
617 return block_nr;
991d9fa0
JT
618}
619
34fbcf62
JT
620/*
621 * Returns the _complete_ blocks that this bio covers.
622 */
623static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
624 dm_block_t *begin, dm_block_t *end)
625{
626 struct pool *pool = tc->pool;
627 sector_t b = bio->bi_iter.bi_sector;
628 sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
629
630 b += pool->sectors_per_block - 1ull; /* so we round up */
631
632 if (block_size_is_power_of_two(pool)) {
633 b >>= pool->sectors_per_block_shift;
634 e >>= pool->sectors_per_block_shift;
635 } else {
636 (void) sector_div(b, pool->sectors_per_block);
637 (void) sector_div(e, pool->sectors_per_block);
638 }
639
640 if (e < b)
641 /* Can happen if the bio is within a single block. */
642 e = b;
643
644 *begin = b;
645 *end = e;
646}
647
991d9fa0
JT
648static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
649{
650 struct pool *pool = tc->pool;
4f024f37 651 sector_t bi_sector = bio->bi_iter.bi_sector;
991d9fa0
JT
652
653 bio->bi_bdev = tc->pool_dev->bdev;
58f77a21 654 if (block_size_is_power_of_two(pool))
4f024f37
KO
655 bio->bi_iter.bi_sector =
656 (block << pool->sectors_per_block_shift) |
657 (bi_sector & (pool->sectors_per_block - 1));
58f77a21 658 else
4f024f37 659 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
58f77a21 660 sector_div(bi_sector, pool->sectors_per_block);
991d9fa0
JT
661}
662
2dd9c257
JT
663static void remap_to_origin(struct thin_c *tc, struct bio *bio)
664{
665 bio->bi_bdev = tc->origin_dev->bdev;
666}
667
4afdd680
JT
668static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
669{
670 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
671 dm_thin_changed_this_transaction(tc->td);
672}
673
e8088073
JT
674static void inc_all_io_entry(struct pool *pool, struct bio *bio)
675{
676 struct dm_thin_endio_hook *h;
677
678 if (bio->bi_rw & REQ_DISCARD)
679 return;
680
59c3d2c6 681 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
e8088073
JT
682 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
683}
684
2dd9c257 685static void issue(struct thin_c *tc, struct bio *bio)
991d9fa0
JT
686{
687 struct pool *pool = tc->pool;
688 unsigned long flags;
689
e49e5829
JT
690 if (!bio_triggers_commit(tc, bio)) {
691 generic_make_request(bio);
692 return;
693 }
694
991d9fa0 695 /*
e49e5829
JT
696 * Complete bio with an error if earlier I/O caused changes to
697 * the metadata that can't be committed e.g, due to I/O errors
698 * on the metadata device.
991d9fa0 699 */
e49e5829
JT
700 if (dm_thin_aborted_changes(tc->td)) {
701 bio_io_error(bio);
702 return;
703 }
704
705 /*
706 * Batch together any bios that trigger commits and then issue a
707 * single commit for them in process_deferred_bios().
708 */
709 spin_lock_irqsave(&pool->lock, flags);
710 bio_list_add(&pool->deferred_flush_bios, bio);
711 spin_unlock_irqrestore(&pool->lock, flags);
991d9fa0
JT
712}
713
2dd9c257
JT
714static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
715{
716 remap_to_origin(tc, bio);
717 issue(tc, bio);
718}
719
720static void remap_and_issue(struct thin_c *tc, struct bio *bio,
721 dm_block_t block)
722{
723 remap(tc, bio, block);
724 issue(tc, bio);
725}
726
991d9fa0
JT
727/*----------------------------------------------------------------*/
728
729/*
730 * Bio endio functions.
731 */
a24c2569 732struct dm_thin_new_mapping {
991d9fa0
JT
733 struct list_head list;
734
7f214665 735 bool pass_discard:1;
34fbcf62 736 bool maybe_shared:1;
991d9fa0 737
50f3c3ef
JT
738 /*
739 * Track quiescing, copying and zeroing preparation actions. When this
740 * counter hits zero the block is prepared and can be inserted into the
741 * btree.
742 */
743 atomic_t prepare_actions;
744
7f214665 745 int err;
991d9fa0 746 struct thin_c *tc;
34fbcf62 747 dm_block_t virt_begin, virt_end;
991d9fa0 748 dm_block_t data_block;
34fbcf62 749 struct dm_bio_prison_cell *cell;
991d9fa0
JT
750
751 /*
752 * If the bio covers the whole area of a block then we can avoid
753 * zeroing or copying. Instead this bio is hooked. The bio will
754 * still be in the cell, so care has to be taken to avoid issuing
755 * the bio twice.
756 */
757 struct bio *bio;
758 bio_end_io_t *saved_bi_end_io;
759};
760
50f3c3ef 761static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
991d9fa0
JT
762{
763 struct pool *pool = m->tc->pool;
764
50f3c3ef 765 if (atomic_dec_and_test(&m->prepare_actions)) {
daec338b 766 list_add_tail(&m->list, &pool->prepared_mappings);
991d9fa0
JT
767 wake_worker(pool);
768 }
769}
770
e5aea7b4 771static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
991d9fa0
JT
772{
773 unsigned long flags;
991d9fa0
JT
774 struct pool *pool = m->tc->pool;
775
991d9fa0 776 spin_lock_irqsave(&pool->lock, flags);
50f3c3ef 777 __complete_mapping_preparation(m);
991d9fa0
JT
778 spin_unlock_irqrestore(&pool->lock, flags);
779}
780
e5aea7b4
JT
781static void copy_complete(int read_err, unsigned long write_err, void *context)
782{
783 struct dm_thin_new_mapping *m = context;
784
785 m->err = read_err || write_err ? -EIO : 0;
786 complete_mapping_preparation(m);
787}
788
4246a0b6 789static void overwrite_endio(struct bio *bio)
991d9fa0 790{
59c3d2c6 791 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
a24c2569 792 struct dm_thin_new_mapping *m = h->overwrite_mapping;
991d9fa0 793
8b908f8e
MS
794 bio->bi_end_io = m->saved_bi_end_io;
795
4246a0b6 796 m->err = bio->bi_error;
e5aea7b4 797 complete_mapping_preparation(m);
991d9fa0
JT
798}
799
991d9fa0
JT
800/*----------------------------------------------------------------*/
801
802/*
803 * Workqueue.
804 */
805
806/*
807 * Prepared mapping jobs.
808 */
809
810/*
2d759a46
JT
811 * This sends the bios in the cell, except the original holder, back
812 * to the deferred_bios list.
991d9fa0 813 */
f286ba0e 814static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
991d9fa0 815{
991d9fa0
JT
816 struct pool *pool = tc->pool;
817 unsigned long flags;
818
c140e1c4
MS
819 spin_lock_irqsave(&tc->lock, flags);
820 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
821 spin_unlock_irqrestore(&tc->lock, flags);
991d9fa0
JT
822
823 wake_worker(pool);
824}
825
a374bb21
JT
826static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
827
2d759a46
JT
828struct remap_info {
829 struct thin_c *tc;
830 struct bio_list defer_bios;
831 struct bio_list issue_bios;
832};
833
834static void __inc_remap_and_issue_cell(void *context,
835 struct dm_bio_prison_cell *cell)
a374bb21 836{
2d759a46 837 struct remap_info *info = context;
a374bb21 838 struct bio *bio;
a374bb21 839
2d759a46 840 while ((bio = bio_list_pop(&cell->bios))) {
a374bb21 841 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
2d759a46 842 bio_list_add(&info->defer_bios, bio);
a374bb21 843 else {
2d759a46
JT
844 inc_all_io_entry(info->tc->pool, bio);
845
846 /*
847 * We can't issue the bios with the bio prison lock
848 * held, so we add them to a list to issue on
849 * return from this function.
850 */
851 bio_list_add(&info->issue_bios, bio);
a374bb21
JT
852 }
853 }
854}
855
2d759a46
JT
856static void inc_remap_and_issue_cell(struct thin_c *tc,
857 struct dm_bio_prison_cell *cell,
858 dm_block_t block)
859{
860 struct bio *bio;
861 struct remap_info info;
862
863 info.tc = tc;
864 bio_list_init(&info.defer_bios);
865 bio_list_init(&info.issue_bios);
866
867 /*
868 * We have to be careful to inc any bios we're about to issue
869 * before the cell is released, and avoid a race with new bios
870 * being added to the cell.
871 */
872 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
873 &info, cell);
874
875 while ((bio = bio_list_pop(&info.defer_bios)))
876 thin_defer_bio(tc, bio);
877
878 while ((bio = bio_list_pop(&info.issue_bios)))
879 remap_and_issue(info.tc, bio, block);
880}
881
e49e5829
JT
882static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
883{
6beca5eb 884 cell_error(m->tc->pool, m->cell);
e49e5829
JT
885 list_del(&m->list);
886 mempool_free(m, m->tc->pool->mapping_pool);
887}
025b9685 888
a24c2569 889static void process_prepared_mapping(struct dm_thin_new_mapping *m)
991d9fa0
JT
890{
891 struct thin_c *tc = m->tc;
6beca5eb 892 struct pool *pool = tc->pool;
8b908f8e 893 struct bio *bio = m->bio;
991d9fa0
JT
894 int r;
895
991d9fa0 896 if (m->err) {
6beca5eb 897 cell_error(pool, m->cell);
905386f8 898 goto out;
991d9fa0
JT
899 }
900
901 /*
902 * Commit the prepared block into the mapping btree.
903 * Any I/O for this block arriving after this point will get
904 * remapped to it directly.
905 */
34fbcf62 906 r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
991d9fa0 907 if (r) {
b5330655 908 metadata_operation_failed(pool, "dm_thin_insert_block", r);
6beca5eb 909 cell_error(pool, m->cell);
905386f8 910 goto out;
991d9fa0
JT
911 }
912
913 /*
914 * Release any bios held while the block was being provisioned.
915 * If we are processing a write bio that completely covers the block,
916 * we already processed it so can ignore it now when processing
917 * the bios in the cell.
918 */
919 if (bio) {
2d759a46 920 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
4246a0b6 921 bio_endio(bio);
2d759a46
JT
922 } else {
923 inc_all_io_entry(tc->pool, m->cell->holder);
924 remap_and_issue(tc, m->cell->holder, m->data_block);
925 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
926 }
991d9fa0 927
905386f8 928out:
991d9fa0 929 list_del(&m->list);
6beca5eb 930 mempool_free(m, pool->mapping_pool);
991d9fa0
JT
931}
932
34fbcf62
JT
933/*----------------------------------------------------------------*/
934
935static void free_discard_mapping(struct dm_thin_new_mapping *m)
104655fd 936{
104655fd 937 struct thin_c *tc = m->tc;
34fbcf62
JT
938 if (m->cell)
939 cell_defer_no_holder(tc, m->cell);
940 mempool_free(m, tc->pool->mapping_pool);
941}
104655fd 942
34fbcf62
JT
943static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
944{
e49e5829 945 bio_io_error(m->bio);
34fbcf62
JT
946 free_discard_mapping(m);
947}
948
949static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
950{
4246a0b6 951 bio_endio(m->bio);
34fbcf62
JT
952 free_discard_mapping(m);
953}
954
955static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
956{
957 int r;
958 struct thin_c *tc = m->tc;
959
960 r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
961 if (r) {
962 metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
963 bio_io_error(m->bio);
964 } else
4246a0b6 965 bio_endio(m->bio);
34fbcf62 966
f286ba0e 967 cell_defer_no_holder(tc, m->cell);
e49e5829
JT
968 mempool_free(m, tc->pool->mapping_pool);
969}
970
34fbcf62 971static int passdown_double_checking_shared_status(struct dm_thin_new_mapping *m)
e49e5829 972{
34fbcf62
JT
973 /*
974 * We've already unmapped this range of blocks, but before we
975 * passdown we have to check that these blocks are now unused.
976 */
977 int r;
978 bool used = true;
e49e5829 979 struct thin_c *tc = m->tc;
34fbcf62
JT
980 struct pool *pool = tc->pool;
981 dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
104655fd 982
34fbcf62
JT
983 while (b != end) {
984 /* find start of unmapped run */
985 for (; b < end; b++) {
986 r = dm_pool_block_is_used(pool->pmd, b, &used);
987 if (r)
988 return r;
e8088073 989
34fbcf62
JT
990 if (!used)
991 break;
19fa1a67 992 }
104655fd 993
34fbcf62
JT
994 if (b == end)
995 break;
996
997 /* find end of run */
998 for (e = b + 1; e != end; e++) {
999 r = dm_pool_block_is_used(pool->pmd, e, &used);
1000 if (r)
1001 return r;
1002
1003 if (used)
1004 break;
1005 }
1006
1007 r = issue_discard(tc, b, e, m->bio);
1008 if (r)
1009 return r;
1010
1011 b = e;
1012 }
1013
1014 return 0;
104655fd
JT
1015}
1016
34fbcf62 1017static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
e49e5829
JT
1018{
1019 int r;
1020 struct thin_c *tc = m->tc;
34fbcf62 1021 struct pool *pool = tc->pool;
e49e5829 1022
34fbcf62 1023 r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
e49e5829 1024 if (r)
34fbcf62
JT
1025 metadata_operation_failed(pool, "dm_thin_remove_range", r);
1026
1027 else if (m->maybe_shared)
1028 r = passdown_double_checking_shared_status(m);
1029 else
1030 r = issue_discard(tc, m->data_block, m->data_block + (m->virt_end - m->virt_begin), m->bio);
e49e5829 1031
34fbcf62
JT
1032 /*
1033 * Even if r is set, there could be sub discards in flight that we
1034 * need to wait for.
1035 */
4246a0b6
CH
1036 m->bio->bi_error = r;
1037 bio_endio(m->bio);
34fbcf62
JT
1038 cell_defer_no_holder(tc, m->cell);
1039 mempool_free(m, pool->mapping_pool);
e49e5829
JT
1040}
1041
104655fd 1042static void process_prepared(struct pool *pool, struct list_head *head,
e49e5829 1043 process_mapping_fn *fn)
991d9fa0
JT
1044{
1045 unsigned long flags;
1046 struct list_head maps;
a24c2569 1047 struct dm_thin_new_mapping *m, *tmp;
991d9fa0
JT
1048
1049 INIT_LIST_HEAD(&maps);
1050 spin_lock_irqsave(&pool->lock, flags);
104655fd 1051 list_splice_init(head, &maps);
991d9fa0
JT
1052 spin_unlock_irqrestore(&pool->lock, flags);
1053
1054 list_for_each_entry_safe(m, tmp, &maps, list)
e49e5829 1055 (*fn)(m);
991d9fa0
JT
1056}
1057
1058/*
1059 * Deferred bio jobs.
1060 */
104655fd 1061static int io_overlaps_block(struct pool *pool, struct bio *bio)
991d9fa0 1062{
4f024f37
KO
1063 return bio->bi_iter.bi_size ==
1064 (pool->sectors_per_block << SECTOR_SHIFT);
104655fd
JT
1065}
1066
1067static int io_overwrites_block(struct pool *pool, struct bio *bio)
1068{
1069 return (bio_data_dir(bio) == WRITE) &&
1070 io_overlaps_block(pool, bio);
991d9fa0
JT
1071}
1072
1073static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1074 bio_end_io_t *fn)
1075{
1076 *save = bio->bi_end_io;
1077 bio->bi_end_io = fn;
1078}
1079
1080static int ensure_next_mapping(struct pool *pool)
1081{
1082 if (pool->next_mapping)
1083 return 0;
1084
1085 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
1086
1087 return pool->next_mapping ? 0 : -ENOMEM;
1088}
1089
a24c2569 1090static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
991d9fa0 1091{
16961b04 1092 struct dm_thin_new_mapping *m = pool->next_mapping;
991d9fa0
JT
1093
1094 BUG_ON(!pool->next_mapping);
1095
16961b04
MS
1096 memset(m, 0, sizeof(struct dm_thin_new_mapping));
1097 INIT_LIST_HEAD(&m->list);
1098 m->bio = NULL;
1099
991d9fa0
JT
1100 pool->next_mapping = NULL;
1101
16961b04 1102 return m;
991d9fa0
JT
1103}
1104
e5aea7b4
JT
1105static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1106 sector_t begin, sector_t end)
1107{
1108 int r;
1109 struct dm_io_region to;
1110
1111 to.bdev = tc->pool_dev->bdev;
1112 to.sector = begin;
1113 to.count = end - begin;
1114
1115 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
1116 if (r < 0) {
1117 DMERR_LIMIT("dm_kcopyd_zero() failed");
1118 copy_complete(1, 1, m);
1119 }
1120}
1121
452d7a62 1122static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
34fbcf62 1123 dm_block_t data_begin,
452d7a62
MS
1124 struct dm_thin_new_mapping *m)
1125{
1126 struct pool *pool = tc->pool;
1127 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1128
1129 h->overwrite_mapping = m;
1130 m->bio = bio;
1131 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1132 inc_all_io_entry(pool, bio);
34fbcf62 1133 remap_and_issue(tc, bio, data_begin);
452d7a62
MS
1134}
1135
e5aea7b4
JT
1136/*
1137 * A partial copy also needs to zero the uncopied region.
1138 */
991d9fa0 1139static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
2dd9c257
JT
1140 struct dm_dev *origin, dm_block_t data_origin,
1141 dm_block_t data_dest,
e5aea7b4
JT
1142 struct dm_bio_prison_cell *cell, struct bio *bio,
1143 sector_t len)
991d9fa0
JT
1144{
1145 int r;
1146 struct pool *pool = tc->pool;
a24c2569 1147 struct dm_thin_new_mapping *m = get_next_mapping(pool);
991d9fa0 1148
991d9fa0 1149 m->tc = tc;
34fbcf62
JT
1150 m->virt_begin = virt_block;
1151 m->virt_end = virt_block + 1u;
991d9fa0
JT
1152 m->data_block = data_dest;
1153 m->cell = cell;
991d9fa0 1154
e5aea7b4
JT
1155 /*
1156 * quiesce action + copy action + an extra reference held for the
1157 * duration of this function (we may need to inc later for a
1158 * partial zero).
1159 */
1160 atomic_set(&m->prepare_actions, 3);
1161
44feb387 1162 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
e5aea7b4 1163 complete_mapping_preparation(m); /* already quiesced */
991d9fa0
JT
1164
1165 /*
1166 * IO to pool_dev remaps to the pool target's data_dev.
1167 *
1168 * If the whole block of data is being overwritten, we can issue the
1169 * bio immediately. Otherwise we use kcopyd to clone the data first.
1170 */
452d7a62
MS
1171 if (io_overwrites_block(pool, bio))
1172 remap_and_issue_overwrite(tc, bio, data_dest, m);
1173 else {
991d9fa0
JT
1174 struct dm_io_region from, to;
1175
2dd9c257 1176 from.bdev = origin->bdev;
991d9fa0 1177 from.sector = data_origin * pool->sectors_per_block;
e5aea7b4 1178 from.count = len;
991d9fa0
JT
1179
1180 to.bdev = tc->pool_dev->bdev;
1181 to.sector = data_dest * pool->sectors_per_block;
e5aea7b4 1182 to.count = len;
991d9fa0
JT
1183
1184 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1185 0, copy_complete, m);
1186 if (r < 0) {
c397741c 1187 DMERR_LIMIT("dm_kcopyd_copy() failed");
e5aea7b4
JT
1188 copy_complete(1, 1, m);
1189
1190 /*
1191 * We allow the zero to be issued, to simplify the
1192 * error path. Otherwise we'd need to start
1193 * worrying about decrementing the prepare_actions
1194 * counter.
1195 */
1196 }
1197
1198 /*
1199 * Do we need to zero a tail region?
1200 */
1201 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1202 atomic_inc(&m->prepare_actions);
1203 ll_zero(tc, m,
1204 data_dest * pool->sectors_per_block + len,
1205 (data_dest + 1) * pool->sectors_per_block);
991d9fa0
JT
1206 }
1207 }
e5aea7b4
JT
1208
1209 complete_mapping_preparation(m); /* drop our ref */
991d9fa0
JT
1210}
1211
2dd9c257
JT
1212static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1213 dm_block_t data_origin, dm_block_t data_dest,
a24c2569 1214 struct dm_bio_prison_cell *cell, struct bio *bio)
2dd9c257
JT
1215{
1216 schedule_copy(tc, virt_block, tc->pool_dev,
e5aea7b4
JT
1217 data_origin, data_dest, cell, bio,
1218 tc->pool->sectors_per_block);
2dd9c257
JT
1219}
1220
991d9fa0 1221static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
a24c2569 1222 dm_block_t data_block, struct dm_bio_prison_cell *cell,
991d9fa0
JT
1223 struct bio *bio)
1224{
1225 struct pool *pool = tc->pool;
a24c2569 1226 struct dm_thin_new_mapping *m = get_next_mapping(pool);
991d9fa0 1227
50f3c3ef 1228 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
991d9fa0 1229 m->tc = tc;
34fbcf62
JT
1230 m->virt_begin = virt_block;
1231 m->virt_end = virt_block + 1u;
991d9fa0
JT
1232 m->data_block = data_block;
1233 m->cell = cell;
991d9fa0
JT
1234
1235 /*
1236 * If the whole block of data is being overwritten or we are not
1237 * zeroing pre-existing data, we can issue the bio immediately.
1238 * Otherwise we use kcopyd to zero the data first.
1239 */
f8ae7525
MS
1240 if (pool->pf.zero_new_blocks) {
1241 if (io_overwrites_block(pool, bio))
1242 remap_and_issue_overwrite(tc, bio, data_block, m);
1243 else
1244 ll_zero(tc, m, data_block * pool->sectors_per_block,
1245 (data_block + 1) * pool->sectors_per_block);
1246 } else
991d9fa0 1247 process_prepared_mapping(m);
e5aea7b4 1248}
991d9fa0 1249
e5aea7b4
JT
1250static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1251 dm_block_t data_dest,
1252 struct dm_bio_prison_cell *cell, struct bio *bio)
1253{
1254 struct pool *pool = tc->pool;
1255 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1256 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1257
1258 if (virt_block_end <= tc->origin_size)
1259 schedule_copy(tc, virt_block, tc->origin_dev,
1260 virt_block, data_dest, cell, bio,
1261 pool->sectors_per_block);
1262
1263 else if (virt_block_begin < tc->origin_size)
1264 schedule_copy(tc, virt_block, tc->origin_dev,
1265 virt_block, data_dest, cell, bio,
1266 tc->origin_size - virt_block_begin);
1267
1268 else
1269 schedule_zero(tc, virt_block, data_dest, cell, bio);
991d9fa0
JT
1270}
1271
2c43fd26
JT
1272static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1273
1274static void check_for_space(struct pool *pool)
1275{
1276 int r;
1277 dm_block_t nr_free;
1278
1279 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1280 return;
1281
1282 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1283 if (r)
1284 return;
1285
1286 if (nr_free)
1287 set_pool_mode(pool, PM_WRITE);
1288}
1289
e49e5829
JT
1290/*
1291 * A non-zero return indicates read_only or fail_io mode.
1292 * Many callers don't care about the return value.
1293 */
020cc3b5 1294static int commit(struct pool *pool)
e49e5829
JT
1295{
1296 int r;
1297
8d07e8a5 1298 if (get_pool_mode(pool) >= PM_READ_ONLY)
e49e5829
JT
1299 return -EINVAL;
1300
020cc3b5 1301 r = dm_pool_commit_metadata(pool->pmd);
b5330655
JT
1302 if (r)
1303 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
2c43fd26
JT
1304 else
1305 check_for_space(pool);
e49e5829
JT
1306
1307 return r;
1308}
1309
88a6621b
JT
1310static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1311{
1312 unsigned long flags;
1313
1314 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1315 DMWARN("%s: reached low water mark for data device: sending event.",
1316 dm_device_name(pool->pool_md));
1317 spin_lock_irqsave(&pool->lock, flags);
1318 pool->low_water_triggered = true;
1319 spin_unlock_irqrestore(&pool->lock, flags);
1320 dm_table_event(pool->ti->table);
1321 }
1322}
1323
991d9fa0
JT
1324static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1325{
1326 int r;
1327 dm_block_t free_blocks;
991d9fa0
JT
1328 struct pool *pool = tc->pool;
1329
3e1a0699 1330 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
8d30abff
JT
1331 return -EINVAL;
1332
991d9fa0 1333 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
b5330655
JT
1334 if (r) {
1335 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
991d9fa0 1336 return r;
b5330655 1337 }
991d9fa0 1338
88a6621b 1339 check_low_water_mark(pool, free_blocks);
991d9fa0
JT
1340
1341 if (!free_blocks) {
94563bad
MS
1342 /*
1343 * Try to commit to see if that will free up some
1344 * more space.
1345 */
020cc3b5
JT
1346 r = commit(pool);
1347 if (r)
1348 return r;
991d9fa0 1349
94563bad 1350 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
b5330655
JT
1351 if (r) {
1352 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
94563bad 1353 return r;
b5330655 1354 }
991d9fa0 1355
94563bad 1356 if (!free_blocks) {
3e1a0699 1357 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
94563bad 1358 return -ENOSPC;
991d9fa0
JT
1359 }
1360 }
1361
1362 r = dm_pool_alloc_data_block(pool->pmd, result);
4a02b34e 1363 if (r) {
b5330655 1364 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
991d9fa0 1365 return r;
4a02b34e 1366 }
991d9fa0
JT
1367
1368 return 0;
1369}
1370
1371/*
1372 * If we have run out of space, queue bios until the device is
1373 * resumed, presumably after having been reloaded with more space.
1374 */
1375static void retry_on_resume(struct bio *bio)
1376{
59c3d2c6 1377 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
eb2aa48d 1378 struct thin_c *tc = h->tc;
991d9fa0
JT
1379 unsigned long flags;
1380
c140e1c4
MS
1381 spin_lock_irqsave(&tc->lock, flags);
1382 bio_list_add(&tc->retry_on_resume_list, bio);
1383 spin_unlock_irqrestore(&tc->lock, flags);
991d9fa0
JT
1384}
1385
af91805a 1386static int should_error_unserviceable_bio(struct pool *pool)
8c0f0e8c 1387{
3e1a0699
JT
1388 enum pool_mode m = get_pool_mode(pool);
1389
1390 switch (m) {
1391 case PM_WRITE:
1392 /* Shouldn't get here */
1393 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
af91805a 1394 return -EIO;
3e1a0699
JT
1395
1396 case PM_OUT_OF_DATA_SPACE:
af91805a 1397 return pool->pf.error_if_no_space ? -ENOSPC : 0;
3e1a0699
JT
1398
1399 case PM_READ_ONLY:
1400 case PM_FAIL:
af91805a 1401 return -EIO;
3e1a0699
JT
1402 default:
1403 /* Shouldn't get here */
1404 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
af91805a 1405 return -EIO;
3e1a0699
JT
1406 }
1407}
8c0f0e8c 1408
3e1a0699
JT
1409static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1410{
af91805a
MS
1411 int error = should_error_unserviceable_bio(pool);
1412
4246a0b6
CH
1413 if (error) {
1414 bio->bi_error = error;
1415 bio_endio(bio);
1416 } else
6d16202b 1417 retry_on_resume(bio);
8c0f0e8c
MS
1418}
1419
399caddf 1420static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
991d9fa0
JT
1421{
1422 struct bio *bio;
1423 struct bio_list bios;
af91805a 1424 int error;
991d9fa0 1425
af91805a
MS
1426 error = should_error_unserviceable_bio(pool);
1427 if (error) {
1428 cell_error_with_code(pool, cell, error);
3e1a0699
JT
1429 return;
1430 }
1431
991d9fa0 1432 bio_list_init(&bios);
6beca5eb 1433 cell_release(pool, cell, &bios);
991d9fa0 1434
9d094eeb
MS
1435 while ((bio = bio_list_pop(&bios)))
1436 retry_on_resume(bio);
991d9fa0
JT
1437}
1438
34fbcf62
JT
1439static void process_discard_cell_no_passdown(struct thin_c *tc,
1440 struct dm_bio_prison_cell *virt_cell)
104655fd 1441{
104655fd 1442 struct pool *pool = tc->pool;
34fbcf62 1443 struct dm_thin_new_mapping *m = get_next_mapping(pool);
104655fd 1444
34fbcf62
JT
1445 /*
1446 * We don't need to lock the data blocks, since there's no
1447 * passdown. We only lock data blocks for allocation and breaking sharing.
1448 */
1449 m->tc = tc;
1450 m->virt_begin = virt_cell->key.block_begin;
1451 m->virt_end = virt_cell->key.block_end;
1452 m->cell = virt_cell;
1453 m->bio = virt_cell->holder;
104655fd 1454
34fbcf62
JT
1455 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1456 pool->process_prepared_discard(m);
1457}
104655fd 1458
34fbcf62
JT
1459static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1460 struct bio *bio)
1461{
1462 struct pool *pool = tc->pool;
1463
1464 int r;
1465 bool maybe_shared;
1466 struct dm_cell_key data_key;
1467 struct dm_bio_prison_cell *data_cell;
1468 struct dm_thin_new_mapping *m;
1469 dm_block_t virt_begin, virt_end, data_begin;
1470
1471 while (begin != end) {
1472 r = ensure_next_mapping(pool);
1473 if (r)
1474 /* we did our best */
1475 return;
e8088073 1476
34fbcf62
JT
1477 r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1478 &data_begin, &maybe_shared);
1479 if (r)
104655fd 1480 /*
34fbcf62
JT
1481 * Silently fail, letting any mappings we've
1482 * created complete.
104655fd 1483 */
34fbcf62
JT
1484 break;
1485
1486 build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1487 if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1488 /* contention, we'll give up with this range */
1489 begin = virt_end;
1490 continue;
104655fd 1491 }
104655fd 1492
104655fd 1493 /*
34fbcf62
JT
1494 * IO may still be going to the destination block. We must
1495 * quiesce before we can do the removal.
104655fd 1496 */
34fbcf62
JT
1497 m = get_next_mapping(pool);
1498 m->tc = tc;
1499 m->maybe_shared = maybe_shared;
1500 m->virt_begin = virt_begin;
1501 m->virt_end = virt_end;
1502 m->data_block = data_begin;
1503 m->cell = data_cell;
1504 m->bio = bio;
104655fd 1505
34fbcf62
JT
1506 /*
1507 * The parent bio must not complete before sub discard bios are
3dba53a9 1508 * chained to it (see issue_discard's bio_chain)!
34fbcf62
JT
1509 *
1510 * This per-mapping bi_remaining increment is paired with
1511 * the implicit decrement that occurs via bio_endio() in
3dba53a9 1512 * process_prepared_discard_passdown().
34fbcf62 1513 */
13e4f8a6 1514 bio_inc_remaining(bio);
34fbcf62
JT
1515 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1516 pool->process_prepared_discard(m);
1517
1518 begin = virt_end;
104655fd
JT
1519 }
1520}
1521
34fbcf62
JT
1522static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1523{
1524 struct bio *bio = virt_cell->holder;
1525 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1526
1527 /*
1528 * The virt_cell will only get freed once the origin bio completes.
1529 * This means it will remain locked while all the individual
1530 * passdown bios are in flight.
1531 */
1532 h->cell = virt_cell;
1533 break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1534
1535 /*
1536 * We complete the bio now, knowing that the bi_remaining field
1537 * will prevent completion until the sub range discards have
1538 * completed.
1539 */
4246a0b6 1540 bio_endio(bio);
34fbcf62
JT
1541}
1542
a374bb21
JT
1543static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1544{
34fbcf62
JT
1545 dm_block_t begin, end;
1546 struct dm_cell_key virt_key;
1547 struct dm_bio_prison_cell *virt_cell;
a374bb21 1548
34fbcf62
JT
1549 get_bio_block_range(tc, bio, &begin, &end);
1550 if (begin == end) {
1551 /*
1552 * The discard covers less than a block.
1553 */
4246a0b6 1554 bio_endio(bio);
a374bb21 1555 return;
34fbcf62 1556 }
a374bb21 1557
34fbcf62
JT
1558 build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1559 if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1560 /*
1561 * Potential starvation issue: We're relying on the
1562 * fs/application being well behaved, and not trying to
1563 * send IO to a region at the same time as discarding it.
1564 * If they do this persistently then it's possible this
1565 * cell will never be granted.
1566 */
1567 return;
1568
1569 tc->pool->process_discard_cell(tc, virt_cell);
a374bb21
JT
1570}
1571
991d9fa0 1572static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
44feb387 1573 struct dm_cell_key *key,
991d9fa0 1574 struct dm_thin_lookup_result *lookup_result,
a24c2569 1575 struct dm_bio_prison_cell *cell)
991d9fa0
JT
1576{
1577 int r;
1578 dm_block_t data_block;
d6fc2042 1579 struct pool *pool = tc->pool;
991d9fa0
JT
1580
1581 r = alloc_data_block(tc, &data_block);
1582 switch (r) {
1583 case 0:
2dd9c257
JT
1584 schedule_internal_copy(tc, block, lookup_result->block,
1585 data_block, cell, bio);
991d9fa0
JT
1586 break;
1587
1588 case -ENOSPC:
399caddf 1589 retry_bios_on_resume(pool, cell);
991d9fa0
JT
1590 break;
1591
1592 default:
c397741c
MS
1593 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1594 __func__, r);
d6fc2042 1595 cell_error(pool, cell);
991d9fa0
JT
1596 break;
1597 }
1598}
1599
23ca2bb6
JT
1600static void __remap_and_issue_shared_cell(void *context,
1601 struct dm_bio_prison_cell *cell)
1602{
1603 struct remap_info *info = context;
1604 struct bio *bio;
1605
1606 while ((bio = bio_list_pop(&cell->bios))) {
1607 if ((bio_data_dir(bio) == WRITE) ||
1608 (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
1609 bio_list_add(&info->defer_bios, bio);
1610 else {
1611 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1612
1613 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1614 inc_all_io_entry(info->tc->pool, bio);
1615 bio_list_add(&info->issue_bios, bio);
1616 }
1617 }
1618}
1619
1620static void remap_and_issue_shared_cell(struct thin_c *tc,
1621 struct dm_bio_prison_cell *cell,
1622 dm_block_t block)
1623{
1624 struct bio *bio;
1625 struct remap_info info;
1626
1627 info.tc = tc;
1628 bio_list_init(&info.defer_bios);
1629 bio_list_init(&info.issue_bios);
1630
1631 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1632 &info, cell);
1633
1634 while ((bio = bio_list_pop(&info.defer_bios)))
1635 thin_defer_bio(tc, bio);
1636
1637 while ((bio = bio_list_pop(&info.issue_bios)))
1638 remap_and_issue(tc, bio, block);
1639}
1640
991d9fa0
JT
1641static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1642 dm_block_t block,
23ca2bb6
JT
1643 struct dm_thin_lookup_result *lookup_result,
1644 struct dm_bio_prison_cell *virt_cell)
991d9fa0 1645{
23ca2bb6 1646 struct dm_bio_prison_cell *data_cell;
991d9fa0 1647 struct pool *pool = tc->pool;
44feb387 1648 struct dm_cell_key key;
991d9fa0
JT
1649
1650 /*
1651 * If cell is already occupied, then sharing is already in the process
1652 * of being broken so we have nothing further to do here.
1653 */
1654 build_data_key(tc->td, lookup_result->block, &key);
23ca2bb6
JT
1655 if (bio_detain(pool, &key, bio, &data_cell)) {
1656 cell_defer_no_holder(tc, virt_cell);
991d9fa0 1657 return;
23ca2bb6 1658 }
991d9fa0 1659
23ca2bb6
JT
1660 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1661 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1662 cell_defer_no_holder(tc, virt_cell);
1663 } else {
59c3d2c6 1664 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
991d9fa0 1665
44feb387 1666 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
e8088073 1667 inc_all_io_entry(pool, bio);
991d9fa0 1668 remap_and_issue(tc, bio, lookup_result->block);
23ca2bb6
JT
1669
1670 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1671 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
991d9fa0
JT
1672 }
1673}
1674
1675static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
a24c2569 1676 struct dm_bio_prison_cell *cell)
991d9fa0
JT
1677{
1678 int r;
1679 dm_block_t data_block;
6beca5eb 1680 struct pool *pool = tc->pool;
991d9fa0
JT
1681
1682 /*
1683 * Remap empty bios (flushes) immediately, without provisioning.
1684 */
4f024f37 1685 if (!bio->bi_iter.bi_size) {
6beca5eb 1686 inc_all_io_entry(pool, bio);
f286ba0e 1687 cell_defer_no_holder(tc, cell);
e8088073 1688
991d9fa0
JT
1689 remap_and_issue(tc, bio, 0);
1690 return;
1691 }
1692
1693 /*
1694 * Fill read bios with zeroes and complete them immediately.
1695 */
1696 if (bio_data_dir(bio) == READ) {
1697 zero_fill_bio(bio);
f286ba0e 1698 cell_defer_no_holder(tc, cell);
4246a0b6 1699 bio_endio(bio);
991d9fa0
JT
1700 return;
1701 }
1702
1703 r = alloc_data_block(tc, &data_block);
1704 switch (r) {
1705 case 0:
2dd9c257
JT
1706 if (tc->origin_dev)
1707 schedule_external_copy(tc, block, data_block, cell, bio);
1708 else
1709 schedule_zero(tc, block, data_block, cell, bio);
991d9fa0
JT
1710 break;
1711
1712 case -ENOSPC:
399caddf 1713 retry_bios_on_resume(pool, cell);
991d9fa0
JT
1714 break;
1715
1716 default:
c397741c
MS
1717 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1718 __func__, r);
6beca5eb 1719 cell_error(pool, cell);
991d9fa0
JT
1720 break;
1721 }
1722}
1723
a374bb21 1724static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
991d9fa0
JT
1725{
1726 int r;
6beca5eb 1727 struct pool *pool = tc->pool;
a374bb21 1728 struct bio *bio = cell->holder;
991d9fa0 1729 dm_block_t block = get_bio_block(tc, bio);
991d9fa0
JT
1730 struct dm_thin_lookup_result lookup_result;
1731
a374bb21
JT
1732 if (tc->requeue_mode) {
1733 cell_requeue(pool, cell);
991d9fa0 1734 return;
a374bb21 1735 }
991d9fa0
JT
1736
1737 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1738 switch (r) {
1739 case 0:
23ca2bb6
JT
1740 if (lookup_result.shared)
1741 process_shared_bio(tc, bio, block, &lookup_result, cell);
1742 else {
6beca5eb 1743 inc_all_io_entry(pool, bio);
991d9fa0 1744 remap_and_issue(tc, bio, lookup_result.block);
a374bb21 1745 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
e8088073 1746 }
991d9fa0
JT
1747 break;
1748
1749 case -ENODATA:
2dd9c257 1750 if (bio_data_dir(bio) == READ && tc->origin_dev) {
6beca5eb 1751 inc_all_io_entry(pool, bio);
f286ba0e 1752 cell_defer_no_holder(tc, cell);
e8088073 1753
e5aea7b4
JT
1754 if (bio_end_sector(bio) <= tc->origin_size)
1755 remap_to_origin_and_issue(tc, bio);
1756
1757 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1758 zero_fill_bio(bio);
1759 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1760 remap_to_origin_and_issue(tc, bio);
1761
1762 } else {
1763 zero_fill_bio(bio);
4246a0b6 1764 bio_endio(bio);
e5aea7b4 1765 }
2dd9c257
JT
1766 } else
1767 provision_block(tc, bio, block, cell);
991d9fa0
JT
1768 break;
1769
1770 default:
c397741c
MS
1771 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1772 __func__, r);
f286ba0e 1773 cell_defer_no_holder(tc, cell);
991d9fa0
JT
1774 bio_io_error(bio);
1775 break;
1776 }
1777}
1778
a374bb21
JT
1779static void process_bio(struct thin_c *tc, struct bio *bio)
1780{
1781 struct pool *pool = tc->pool;
1782 dm_block_t block = get_bio_block(tc, bio);
1783 struct dm_bio_prison_cell *cell;
1784 struct dm_cell_key key;
1785
1786 /*
1787 * If cell is already occupied, then the block is already
1788 * being provisioned so we have nothing further to do here.
1789 */
1790 build_virtual_key(tc->td, block, &key);
1791 if (bio_detain(pool, &key, bio, &cell))
1792 return;
1793
1794 process_cell(tc, cell);
1795}
1796
1797static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1798 struct dm_bio_prison_cell *cell)
e49e5829
JT
1799{
1800 int r;
1801 int rw = bio_data_dir(bio);
1802 dm_block_t block = get_bio_block(tc, bio);
1803 struct dm_thin_lookup_result lookup_result;
1804
1805 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1806 switch (r) {
1807 case 0:
a374bb21 1808 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
8c0f0e8c 1809 handle_unserviceable_bio(tc->pool, bio);
a374bb21
JT
1810 if (cell)
1811 cell_defer_no_holder(tc, cell);
1812 } else {
e8088073 1813 inc_all_io_entry(tc->pool, bio);
e49e5829 1814 remap_and_issue(tc, bio, lookup_result.block);
a374bb21
JT
1815 if (cell)
1816 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
e8088073 1817 }
e49e5829
JT
1818 break;
1819
1820 case -ENODATA:
a374bb21
JT
1821 if (cell)
1822 cell_defer_no_holder(tc, cell);
e49e5829 1823 if (rw != READ) {
8c0f0e8c 1824 handle_unserviceable_bio(tc->pool, bio);
e49e5829
JT
1825 break;
1826 }
1827
1828 if (tc->origin_dev) {
e8088073 1829 inc_all_io_entry(tc->pool, bio);
e49e5829
JT
1830 remap_to_origin_and_issue(tc, bio);
1831 break;
1832 }
1833
1834 zero_fill_bio(bio);
4246a0b6 1835 bio_endio(bio);
e49e5829
JT
1836 break;
1837
1838 default:
c397741c
MS
1839 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1840 __func__, r);
a374bb21
JT
1841 if (cell)
1842 cell_defer_no_holder(tc, cell);
e49e5829
JT
1843 bio_io_error(bio);
1844 break;
1845 }
1846}
1847
a374bb21
JT
1848static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1849{
1850 __process_bio_read_only(tc, bio, NULL);
1851}
1852
1853static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1854{
1855 __process_bio_read_only(tc, cell->holder, cell);
1856}
1857
3e1a0699
JT
1858static void process_bio_success(struct thin_c *tc, struct bio *bio)
1859{
4246a0b6 1860 bio_endio(bio);
3e1a0699
JT
1861}
1862
e49e5829
JT
1863static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1864{
1865 bio_io_error(bio);
1866}
1867
a374bb21
JT
1868static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1869{
1870 cell_success(tc->pool, cell);
1871}
1872
1873static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1874{
1875 cell_error(tc->pool, cell);
1876}
1877
ac8c3f3d
JT
1878/*
1879 * FIXME: should we also commit due to size of transaction, measured in
1880 * metadata blocks?
1881 */
905e51b3
JT
1882static int need_commit_due_to_time(struct pool *pool)
1883{
0f30af98
MS
1884 return !time_in_range(jiffies, pool->last_commit_jiffies,
1885 pool->last_commit_jiffies + COMMIT_PERIOD);
905e51b3
JT
1886}
1887
67324ea1
MS
1888#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1889#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1890
1891static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1892{
1893 struct rb_node **rbp, *parent;
1894 struct dm_thin_endio_hook *pbd;
1895 sector_t bi_sector = bio->bi_iter.bi_sector;
1896
1897 rbp = &tc->sort_bio_list.rb_node;
1898 parent = NULL;
1899 while (*rbp) {
1900 parent = *rbp;
1901 pbd = thin_pbd(parent);
1902
1903 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1904 rbp = &(*rbp)->rb_left;
1905 else
1906 rbp = &(*rbp)->rb_right;
1907 }
1908
1909 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1910 rb_link_node(&pbd->rb_node, parent, rbp);
1911 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
1912}
1913
1914static void __extract_sorted_bios(struct thin_c *tc)
1915{
1916 struct rb_node *node;
1917 struct dm_thin_endio_hook *pbd;
1918 struct bio *bio;
1919
1920 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
1921 pbd = thin_pbd(node);
1922 bio = thin_bio(pbd);
1923
1924 bio_list_add(&tc->deferred_bio_list, bio);
1925 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
1926 }
1927
1928 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
1929}
1930
1931static void __sort_thin_deferred_bios(struct thin_c *tc)
1932{
1933 struct bio *bio;
1934 struct bio_list bios;
1935
1936 bio_list_init(&bios);
1937 bio_list_merge(&bios, &tc->deferred_bio_list);
1938 bio_list_init(&tc->deferred_bio_list);
1939
1940 /* Sort deferred_bio_list using rb-tree */
1941 while ((bio = bio_list_pop(&bios)))
1942 __thin_bio_rb_add(tc, bio);
1943
1944 /*
1945 * Transfer the sorted bios in sort_bio_list back to
1946 * deferred_bio_list to allow lockless submission of
1947 * all bios.
1948 */
1949 __extract_sorted_bios(tc);
1950}
1951
c140e1c4 1952static void process_thin_deferred_bios(struct thin_c *tc)
991d9fa0 1953{
c140e1c4 1954 struct pool *pool = tc->pool;
991d9fa0
JT
1955 unsigned long flags;
1956 struct bio *bio;
1957 struct bio_list bios;
67324ea1 1958 struct blk_plug plug;
8a01a6af 1959 unsigned count = 0;
991d9fa0 1960
c140e1c4 1961 if (tc->requeue_mode) {
42d6a8ce 1962 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
c140e1c4
MS
1963 return;
1964 }
1965
991d9fa0
JT
1966 bio_list_init(&bios);
1967
c140e1c4 1968 spin_lock_irqsave(&tc->lock, flags);
67324ea1
MS
1969
1970 if (bio_list_empty(&tc->deferred_bio_list)) {
1971 spin_unlock_irqrestore(&tc->lock, flags);
1972 return;
1973 }
1974
1975 __sort_thin_deferred_bios(tc);
1976
c140e1c4
MS
1977 bio_list_merge(&bios, &tc->deferred_bio_list);
1978 bio_list_init(&tc->deferred_bio_list);
67324ea1 1979
c140e1c4 1980 spin_unlock_irqrestore(&tc->lock, flags);
991d9fa0 1981
67324ea1 1982 blk_start_plug(&plug);
991d9fa0 1983 while ((bio = bio_list_pop(&bios))) {
991d9fa0
JT
1984 /*
1985 * If we've got no free new_mapping structs, and processing
1986 * this bio might require one, we pause until there are some
1987 * prepared mappings to process.
1988 */
1989 if (ensure_next_mapping(pool)) {
c140e1c4
MS
1990 spin_lock_irqsave(&tc->lock, flags);
1991 bio_list_add(&tc->deferred_bio_list, bio);
1992 bio_list_merge(&tc->deferred_bio_list, &bios);
1993 spin_unlock_irqrestore(&tc->lock, flags);
991d9fa0
JT
1994 break;
1995 }
104655fd
JT
1996
1997 if (bio->bi_rw & REQ_DISCARD)
e49e5829 1998 pool->process_discard(tc, bio);
104655fd 1999 else
e49e5829 2000 pool->process_bio(tc, bio);
8a01a6af
JT
2001
2002 if ((count++ & 127) == 0) {
7d327fe0 2003 throttle_work_update(&pool->throttle);
8a01a6af
JT
2004 dm_pool_issue_prefetches(pool->pmd);
2005 }
991d9fa0 2006 }
67324ea1 2007 blk_finish_plug(&plug);
c140e1c4
MS
2008}
2009
ac4c3f34
JT
2010static int cmp_cells(const void *lhs, const void *rhs)
2011{
2012 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2013 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2014
2015 BUG_ON(!lhs_cell->holder);
2016 BUG_ON(!rhs_cell->holder);
2017
2018 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2019 return -1;
2020
2021 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2022 return 1;
2023
2024 return 0;
2025}
2026
2027static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2028{
2029 unsigned count = 0;
2030 struct dm_bio_prison_cell *cell, *tmp;
2031
2032 list_for_each_entry_safe(cell, tmp, cells, user_list) {
2033 if (count >= CELL_SORT_ARRAY_SIZE)
2034 break;
2035
2036 pool->cell_sort_array[count++] = cell;
2037 list_del(&cell->user_list);
2038 }
2039
2040 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2041
2042 return count;
2043}
2044
a374bb21
JT
2045static void process_thin_deferred_cells(struct thin_c *tc)
2046{
2047 struct pool *pool = tc->pool;
2048 unsigned long flags;
2049 struct list_head cells;
ac4c3f34
JT
2050 struct dm_bio_prison_cell *cell;
2051 unsigned i, j, count;
a374bb21
JT
2052
2053 INIT_LIST_HEAD(&cells);
2054
2055 spin_lock_irqsave(&tc->lock, flags);
2056 list_splice_init(&tc->deferred_cells, &cells);
2057 spin_unlock_irqrestore(&tc->lock, flags);
2058
2059 if (list_empty(&cells))
2060 return;
2061
ac4c3f34
JT
2062 do {
2063 count = sort_cells(tc->pool, &cells);
a374bb21 2064
ac4c3f34
JT
2065 for (i = 0; i < count; i++) {
2066 cell = pool->cell_sort_array[i];
2067 BUG_ON(!cell->holder);
a374bb21 2068
ac4c3f34
JT
2069 /*
2070 * If we've got no free new_mapping structs, and processing
2071 * this bio might require one, we pause until there are some
2072 * prepared mappings to process.
2073 */
2074 if (ensure_next_mapping(pool)) {
2075 for (j = i; j < count; j++)
2076 list_add(&pool->cell_sort_array[j]->user_list, &cells);
2077
2078 spin_lock_irqsave(&tc->lock, flags);
2079 list_splice(&cells, &tc->deferred_cells);
2080 spin_unlock_irqrestore(&tc->lock, flags);
2081 return;
2082 }
2083
2084 if (cell->holder->bi_rw & REQ_DISCARD)
2085 pool->process_discard_cell(tc, cell);
2086 else
2087 pool->process_cell(tc, cell);
2088 }
2089 } while (!list_empty(&cells));
a374bb21
JT
2090}
2091
b10ebd34
JT
2092static void thin_get(struct thin_c *tc);
2093static void thin_put(struct thin_c *tc);
2094
2095/*
2096 * We can't hold rcu_read_lock() around code that can block. So we
2097 * find a thin with the rcu lock held; bump a refcount; then drop
2098 * the lock.
2099 */
2100static struct thin_c *get_first_thin(struct pool *pool)
2101{
2102 struct thin_c *tc = NULL;
2103
2104 rcu_read_lock();
2105 if (!list_empty(&pool->active_thins)) {
2106 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2107 thin_get(tc);
2108 }
2109 rcu_read_unlock();
2110
2111 return tc;
2112}
2113
2114static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2115{
2116 struct thin_c *old_tc = tc;
2117
2118 rcu_read_lock();
2119 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2120 thin_get(tc);
2121 thin_put(old_tc);
2122 rcu_read_unlock();
2123 return tc;
2124 }
2125 thin_put(old_tc);
2126 rcu_read_unlock();
2127
2128 return NULL;
2129}
2130
c140e1c4
MS
2131static void process_deferred_bios(struct pool *pool)
2132{
2133 unsigned long flags;
2134 struct bio *bio;
2135 struct bio_list bios;
2136 struct thin_c *tc;
2137
b10ebd34
JT
2138 tc = get_first_thin(pool);
2139 while (tc) {
a374bb21 2140 process_thin_deferred_cells(tc);
c140e1c4 2141 process_thin_deferred_bios(tc);
b10ebd34
JT
2142 tc = get_next_thin(pool, tc);
2143 }
991d9fa0
JT
2144
2145 /*
2146 * If there are any deferred flush bios, we must commit
2147 * the metadata before issuing them.
2148 */
2149 bio_list_init(&bios);
2150 spin_lock_irqsave(&pool->lock, flags);
2151 bio_list_merge(&bios, &pool->deferred_flush_bios);
2152 bio_list_init(&pool->deferred_flush_bios);
2153 spin_unlock_irqrestore(&pool->lock, flags);
2154
4d1662a3
MS
2155 if (bio_list_empty(&bios) &&
2156 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
991d9fa0
JT
2157 return;
2158
020cc3b5 2159 if (commit(pool)) {
991d9fa0
JT
2160 while ((bio = bio_list_pop(&bios)))
2161 bio_io_error(bio);
2162 return;
2163 }
905e51b3 2164 pool->last_commit_jiffies = jiffies;
991d9fa0
JT
2165
2166 while ((bio = bio_list_pop(&bios)))
2167 generic_make_request(bio);
2168}
2169
2170static void do_worker(struct work_struct *ws)
2171{
2172 struct pool *pool = container_of(ws, struct pool, worker);
2173
7d327fe0 2174 throttle_work_start(&pool->throttle);
8a01a6af 2175 dm_pool_issue_prefetches(pool->pmd);
7d327fe0 2176 throttle_work_update(&pool->throttle);
e49e5829 2177 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
7d327fe0 2178 throttle_work_update(&pool->throttle);
e49e5829 2179 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
7d327fe0 2180 throttle_work_update(&pool->throttle);
991d9fa0 2181 process_deferred_bios(pool);
7d327fe0 2182 throttle_work_complete(&pool->throttle);
991d9fa0
JT
2183}
2184
905e51b3
JT
2185/*
2186 * We want to commit periodically so that not too much
2187 * unwritten data builds up.
2188 */
2189static void do_waker(struct work_struct *ws)
2190{
2191 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2192 wake_worker(pool);
2193 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2194}
2195
bcc696fa
MS
2196static void notify_of_pool_mode_change_to_oods(struct pool *pool);
2197
85ad643b
JT
2198/*
2199 * We're holding onto IO to allow userland time to react. After the
2200 * timeout either the pool will have been resized (and thus back in
bcc696fa 2201 * PM_WRITE mode), or we degrade to PM_OUT_OF_DATA_SPACE w/ error_if_no_space.
85ad643b
JT
2202 */
2203static void do_no_space_timeout(struct work_struct *ws)
2204{
2205 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2206 no_space_timeout);
2207
bcc696fa
MS
2208 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) {
2209 pool->pf.error_if_no_space = true;
2210 notify_of_pool_mode_change_to_oods(pool);
0a927c2f 2211 error_retry_list_with_code(pool, -ENOSPC);
bcc696fa 2212 }
85ad643b
JT
2213}
2214
991d9fa0
JT
2215/*----------------------------------------------------------------*/
2216
e7a3e871 2217struct pool_work {
738211f7 2218 struct work_struct worker;
e7a3e871
JT
2219 struct completion complete;
2220};
2221
2222static struct pool_work *to_pool_work(struct work_struct *ws)
2223{
2224 return container_of(ws, struct pool_work, worker);
2225}
2226
2227static void pool_work_complete(struct pool_work *pw)
2228{
2229 complete(&pw->complete);
2230}
738211f7 2231
e7a3e871
JT
2232static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2233 void (*fn)(struct work_struct *))
2234{
2235 INIT_WORK_ONSTACK(&pw->worker, fn);
2236 init_completion(&pw->complete);
2237 queue_work(pool->wq, &pw->worker);
2238 wait_for_completion(&pw->complete);
2239}
2240
2241/*----------------------------------------------------------------*/
2242
2243struct noflush_work {
2244 struct pool_work pw;
2245 struct thin_c *tc;
738211f7
JT
2246};
2247
e7a3e871 2248static struct noflush_work *to_noflush(struct work_struct *ws)
738211f7 2249{
e7a3e871 2250 return container_of(to_pool_work(ws), struct noflush_work, pw);
738211f7
JT
2251}
2252
2253static void do_noflush_start(struct work_struct *ws)
2254{
e7a3e871 2255 struct noflush_work *w = to_noflush(ws);
738211f7
JT
2256 w->tc->requeue_mode = true;
2257 requeue_io(w->tc);
e7a3e871 2258 pool_work_complete(&w->pw);
738211f7
JT
2259}
2260
2261static void do_noflush_stop(struct work_struct *ws)
2262{
e7a3e871 2263 struct noflush_work *w = to_noflush(ws);
738211f7 2264 w->tc->requeue_mode = false;
e7a3e871 2265 pool_work_complete(&w->pw);
738211f7
JT
2266}
2267
2268static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2269{
2270 struct noflush_work w;
2271
738211f7 2272 w.tc = tc;
e7a3e871 2273 pool_work_wait(&w.pw, tc->pool, fn);
738211f7
JT
2274}
2275
2276/*----------------------------------------------------------------*/
2277
e49e5829
JT
2278static enum pool_mode get_pool_mode(struct pool *pool)
2279{
2280 return pool->pf.mode;
2281}
2282
3e1a0699
JT
2283static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2284{
2285 dm_table_event(pool->ti->table);
2286 DMINFO("%s: switching pool to %s mode",
2287 dm_device_name(pool->pool_md), new_mode);
2288}
2289
bcc696fa
MS
2290static void notify_of_pool_mode_change_to_oods(struct pool *pool)
2291{
2292 if (!pool->pf.error_if_no_space)
2293 notify_of_pool_mode_change(pool, "out-of-data-space (queue IO)");
2294 else
2295 notify_of_pool_mode_change(pool, "out-of-data-space (error IO)");
2296}
2297
34fbcf62
JT
2298static bool passdown_enabled(struct pool_c *pt)
2299{
2300 return pt->adjusted_pf.discard_passdown;
2301}
2302
2303static void set_discard_callbacks(struct pool *pool)
2304{
2305 struct pool_c *pt = pool->ti->private;
2306
2307 if (passdown_enabled(pt)) {
2308 pool->process_discard_cell = process_discard_cell_passdown;
2309 pool->process_prepared_discard = process_prepared_discard_passdown;
2310 } else {
2311 pool->process_discard_cell = process_discard_cell_no_passdown;
2312 pool->process_prepared_discard = process_prepared_discard_no_passdown;
2313 }
2314}
2315
8b64e881 2316static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
e49e5829 2317{
cdc2b415 2318 struct pool_c *pt = pool->ti->private;
07f2b6e0
MS
2319 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2320 enum pool_mode old_mode = get_pool_mode(pool);
80c57893 2321 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
07f2b6e0
MS
2322
2323 /*
2324 * Never allow the pool to transition to PM_WRITE mode if user
2325 * intervention is required to verify metadata and data consistency.
2326 */
2327 if (new_mode == PM_WRITE && needs_check) {
2328 DMERR("%s: unable to switch pool to write mode until repaired.",
2329 dm_device_name(pool->pool_md));
2330 if (old_mode != new_mode)
2331 new_mode = old_mode;
2332 else
2333 new_mode = PM_READ_ONLY;
2334 }
2335 /*
2336 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2337 * not going to recover without a thin_repair. So we never let the
2338 * pool move out of the old mode.
2339 */
2340 if (old_mode == PM_FAIL)
2341 new_mode = old_mode;
e49e5829 2342
8b64e881 2343 switch (new_mode) {
e49e5829 2344 case PM_FAIL:
8b64e881 2345 if (old_mode != new_mode)
3e1a0699 2346 notify_of_pool_mode_change(pool, "failure");
5383ef3a 2347 dm_pool_metadata_read_only(pool->pmd);
e49e5829
JT
2348 pool->process_bio = process_bio_fail;
2349 pool->process_discard = process_bio_fail;
a374bb21
JT
2350 pool->process_cell = process_cell_fail;
2351 pool->process_discard_cell = process_cell_fail;
e49e5829
JT
2352 pool->process_prepared_mapping = process_prepared_mapping_fail;
2353 pool->process_prepared_discard = process_prepared_discard_fail;
3e1a0699
JT
2354
2355 error_retry_list(pool);
e49e5829
JT
2356 break;
2357
2358 case PM_READ_ONLY:
8b64e881 2359 if (old_mode != new_mode)
3e1a0699
JT
2360 notify_of_pool_mode_change(pool, "read-only");
2361 dm_pool_metadata_read_only(pool->pmd);
2362 pool->process_bio = process_bio_read_only;
2363 pool->process_discard = process_bio_success;
a374bb21
JT
2364 pool->process_cell = process_cell_read_only;
2365 pool->process_discard_cell = process_cell_success;
3e1a0699 2366 pool->process_prepared_mapping = process_prepared_mapping_fail;
34fbcf62 2367 pool->process_prepared_discard = process_prepared_discard_success;
3e1a0699
JT
2368
2369 error_retry_list(pool);
2370 break;
2371
2372 case PM_OUT_OF_DATA_SPACE:
2373 /*
2374 * Ideally we'd never hit this state; the low water mark
2375 * would trigger userland to extend the pool before we
2376 * completely run out of data space. However, many small
2377 * IOs to unprovisioned space can consume data space at an
2378 * alarming rate. Adjust your low water mark if you're
2379 * frequently seeing this mode.
2380 */
2381 if (old_mode != new_mode)
bcc696fa 2382 notify_of_pool_mode_change_to_oods(pool);
c3667cc6 2383 pool->out_of_data_space = true;
3e1a0699 2384 pool->process_bio = process_bio_read_only;
a374bb21
JT
2385 pool->process_discard = process_discard_bio;
2386 pool->process_cell = process_cell_read_only;
3e1a0699 2387 pool->process_prepared_mapping = process_prepared_mapping;
34fbcf62 2388 set_discard_callbacks(pool);
85ad643b 2389
80c57893
MS
2390 if (!pool->pf.error_if_no_space && no_space_timeout)
2391 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
e49e5829
JT
2392 break;
2393
2394 case PM_WRITE:
8b64e881 2395 if (old_mode != new_mode)
3e1a0699 2396 notify_of_pool_mode_change(pool, "write");
c3667cc6 2397 pool->out_of_data_space = false;
172c2386 2398 pool->pf.error_if_no_space = pt->requested_pf.error_if_no_space;
9b7aaa64 2399 dm_pool_metadata_read_write(pool->pmd);
e49e5829 2400 pool->process_bio = process_bio;
a374bb21
JT
2401 pool->process_discard = process_discard_bio;
2402 pool->process_cell = process_cell;
e49e5829 2403 pool->process_prepared_mapping = process_prepared_mapping;
34fbcf62 2404 set_discard_callbacks(pool);
e49e5829
JT
2405 break;
2406 }
8b64e881
MS
2407
2408 pool->pf.mode = new_mode;
cdc2b415
MS
2409 /*
2410 * The pool mode may have changed, sync it so bind_control_target()
2411 * doesn't cause an unexpected mode transition on resume.
2412 */
2413 pt->adjusted_pf.mode = new_mode;
e49e5829
JT
2414}
2415
07f2b6e0 2416static void abort_transaction(struct pool *pool)
b5330655 2417{
07f2b6e0
MS
2418 const char *dev_name = dm_device_name(pool->pool_md);
2419
2420 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2421 if (dm_pool_abort_metadata(pool->pmd)) {
2422 DMERR("%s: failed to abort metadata transaction", dev_name);
2423 set_pool_mode(pool, PM_FAIL);
2424 }
2425
2426 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2427 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2428 set_pool_mode(pool, PM_FAIL);
2429 }
2430}
399caddf 2431
07f2b6e0
MS
2432static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2433{
b5330655
JT
2434 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2435 dm_device_name(pool->pool_md), op, r);
2436
07f2b6e0 2437 abort_transaction(pool);
b5330655
JT
2438 set_pool_mode(pool, PM_READ_ONLY);
2439}
2440
e49e5829
JT
2441/*----------------------------------------------------------------*/
2442
991d9fa0
JT
2443/*
2444 * Mapping functions.
2445 */
2446
2447/*
2448 * Called only while mapping a thin bio to hand it over to the workqueue.
2449 */
2450static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2451{
2452 unsigned long flags;
2453 struct pool *pool = tc->pool;
2454
c140e1c4
MS
2455 spin_lock_irqsave(&tc->lock, flags);
2456 bio_list_add(&tc->deferred_bio_list, bio);
2457 spin_unlock_irqrestore(&tc->lock, flags);
991d9fa0
JT
2458
2459 wake_worker(pool);
2460}
2461
7d327fe0
JT
2462static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2463{
2464 struct pool *pool = tc->pool;
2465
2466 throttle_lock(&pool->throttle);
2467 thin_defer_bio(tc, bio);
2468 throttle_unlock(&pool->throttle);
2469}
2470
a374bb21
JT
2471static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2472{
2473 unsigned long flags;
2474 struct pool *pool = tc->pool;
2475
2476 throttle_lock(&pool->throttle);
2477 spin_lock_irqsave(&tc->lock, flags);
2478 list_add_tail(&cell->user_list, &tc->deferred_cells);
2479 spin_unlock_irqrestore(&tc->lock, flags);
2480 throttle_unlock(&pool->throttle);
2481
2482 wake_worker(pool);
2483}
2484
59c3d2c6 2485static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
eb2aa48d 2486{
59c3d2c6 2487 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
eb2aa48d
JT
2488
2489 h->tc = tc;
2490 h->shared_read_entry = NULL;
e8088073 2491 h->all_io_entry = NULL;
eb2aa48d 2492 h->overwrite_mapping = NULL;
34fbcf62 2493 h->cell = NULL;
eb2aa48d
JT
2494}
2495
991d9fa0
JT
2496/*
2497 * Non-blocking function called from the thin target's map function.
2498 */
7de3ee57 2499static int thin_bio_map(struct dm_target *ti, struct bio *bio)
991d9fa0
JT
2500{
2501 int r;
2502 struct thin_c *tc = ti->private;
2503 dm_block_t block = get_bio_block(tc, bio);
2504 struct dm_thin_device *td = tc->td;
2505 struct dm_thin_lookup_result result;
a374bb21 2506 struct dm_bio_prison_cell *virt_cell, *data_cell;
e8088073 2507 struct dm_cell_key key;
991d9fa0 2508
59c3d2c6 2509 thin_hook_bio(tc, bio);
e49e5829 2510
738211f7 2511 if (tc->requeue_mode) {
4246a0b6
CH
2512 bio->bi_error = DM_ENDIO_REQUEUE;
2513 bio_endio(bio);
738211f7
JT
2514 return DM_MAPIO_SUBMITTED;
2515 }
2516
e49e5829
JT
2517 if (get_pool_mode(tc->pool) == PM_FAIL) {
2518 bio_io_error(bio);
2519 return DM_MAPIO_SUBMITTED;
2520 }
2521
104655fd 2522 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
7d327fe0 2523 thin_defer_bio_with_throttle(tc, bio);
991d9fa0
JT
2524 return DM_MAPIO_SUBMITTED;
2525 }
2526
c822ed96
JT
2527 /*
2528 * We must hold the virtual cell before doing the lookup, otherwise
2529 * there's a race with discard.
2530 */
2531 build_virtual_key(tc->td, block, &key);
a374bb21 2532 if (bio_detain(tc->pool, &key, bio, &virt_cell))
c822ed96
JT
2533 return DM_MAPIO_SUBMITTED;
2534
991d9fa0
JT
2535 r = dm_thin_find_block(td, block, 0, &result);
2536
2537 /*
2538 * Note that we defer readahead too.
2539 */
2540 switch (r) {
2541 case 0:
2542 if (unlikely(result.shared)) {
2543 /*
2544 * We have a race condition here between the
2545 * result.shared value returned by the lookup and
2546 * snapshot creation, which may cause new
2547 * sharing.
2548 *
2549 * To avoid this always quiesce the origin before
2550 * taking the snap. You want to do this anyway to
2551 * ensure a consistent application view
2552 * (i.e. lockfs).
2553 *
2554 * More distant ancestors are irrelevant. The
2555 * shared flag will be set in their case.
2556 */
a374bb21 2557 thin_defer_cell(tc, virt_cell);
e8088073 2558 return DM_MAPIO_SUBMITTED;
991d9fa0 2559 }
e8088073 2560
e8088073 2561 build_data_key(tc->td, result.block, &key);
a374bb21
JT
2562 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2563 cell_defer_no_holder(tc, virt_cell);
e8088073
JT
2564 return DM_MAPIO_SUBMITTED;
2565 }
2566
2567 inc_all_io_entry(tc->pool, bio);
a374bb21
JT
2568 cell_defer_no_holder(tc, data_cell);
2569 cell_defer_no_holder(tc, virt_cell);
e8088073
JT
2570
2571 remap(tc, bio, result.block);
2572 return DM_MAPIO_REMAPPED;
991d9fa0
JT
2573
2574 case -ENODATA:
e49e5829 2575 case -EWOULDBLOCK:
a374bb21 2576 thin_defer_cell(tc, virt_cell);
2aab3850 2577 return DM_MAPIO_SUBMITTED;
e49e5829
JT
2578
2579 default:
2580 /*
2581 * Must always call bio_io_error on failure.
2582 * dm_thin_find_block can fail with -EINVAL if the
2583 * pool is switched to fail-io mode.
2584 */
2585 bio_io_error(bio);
a374bb21 2586 cell_defer_no_holder(tc, virt_cell);
2aab3850 2587 return DM_MAPIO_SUBMITTED;
991d9fa0 2588 }
991d9fa0
JT
2589}
2590
2591static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2592{
991d9fa0 2593 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
760fe67e 2594 struct request_queue *q;
991d9fa0 2595
760fe67e
MS
2596 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2597 return 1;
991d9fa0 2598
760fe67e
MS
2599 q = bdev_get_queue(pt->data_dev->bdev);
2600 return bdi_congested(&q->backing_dev_info, bdi_bits);
991d9fa0
JT
2601}
2602
c140e1c4 2603static void requeue_bios(struct pool *pool)
991d9fa0 2604{
c140e1c4
MS
2605 unsigned long flags;
2606 struct thin_c *tc;
2607
2608 rcu_read_lock();
2609 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2610 spin_lock_irqsave(&tc->lock, flags);
2611 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2612 bio_list_init(&tc->retry_on_resume_list);
2613 spin_unlock_irqrestore(&tc->lock, flags);
2614 }
2615 rcu_read_unlock();
991d9fa0
JT
2616}
2617
2618/*----------------------------------------------------------------
2619 * Binding of control targets to a pool object
2620 *--------------------------------------------------------------*/
9bc142dd
MS
2621static bool data_dev_supports_discard(struct pool_c *pt)
2622{
2623 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2624
2625 return q && blk_queue_discard(q);
2626}
2627
58051b94
JT
2628static bool is_factor(sector_t block_size, uint32_t n)
2629{
2630 return !sector_div(block_size, n);
2631}
2632
9bc142dd
MS
2633/*
2634 * If discard_passdown was enabled verify that the data device
0424caa1 2635 * supports discards. Disable discard_passdown if not.
9bc142dd 2636 */
0424caa1 2637static void disable_passdown_if_not_supported(struct pool_c *pt)
9bc142dd 2638{
0424caa1
MS
2639 struct pool *pool = pt->pool;
2640 struct block_device *data_bdev = pt->data_dev->bdev;
2641 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
0424caa1 2642 const char *reason = NULL;
9bc142dd
MS
2643 char buf[BDEVNAME_SIZE];
2644
0424caa1 2645 if (!pt->adjusted_pf.discard_passdown)
9bc142dd
MS
2646 return;
2647
0424caa1
MS
2648 if (!data_dev_supports_discard(pt))
2649 reason = "discard unsupported";
2650
2651 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2652 reason = "max discard sectors smaller than a block";
9bc142dd 2653
0424caa1
MS
2654 if (reason) {
2655 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2656 pt->adjusted_pf.discard_passdown = false;
2657 }
9bc142dd
MS
2658}
2659
991d9fa0
JT
2660static int bind_control_target(struct pool *pool, struct dm_target *ti)
2661{
2662 struct pool_c *pt = ti->private;
2663
e49e5829 2664 /*
9b7aaa64 2665 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
e49e5829 2666 */
07f2b6e0 2667 enum pool_mode old_mode = get_pool_mode(pool);
0424caa1 2668 enum pool_mode new_mode = pt->adjusted_pf.mode;
e49e5829 2669
8b64e881
MS
2670 /*
2671 * Don't change the pool's mode until set_pool_mode() below.
2672 * Otherwise the pool's process_* function pointers may
2673 * not match the desired pool mode.
2674 */
2675 pt->adjusted_pf.mode = old_mode;
2676
2677 pool->ti = ti;
2678 pool->pf = pt->adjusted_pf;
2679 pool->low_water_blocks = pt->low_water_blocks;
2680
9bc142dd 2681 set_pool_mode(pool, new_mode);
f402693d 2682
991d9fa0
JT
2683 return 0;
2684}
2685
2686static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2687{
2688 if (pool->ti == ti)
2689 pool->ti = NULL;
2690}
2691
2692/*----------------------------------------------------------------
2693 * Pool creation
2694 *--------------------------------------------------------------*/
67e2e2b2
JT
2695/* Initialize pool features. */
2696static void pool_features_init(struct pool_features *pf)
2697{
e49e5829 2698 pf->mode = PM_WRITE;
9bc142dd
MS
2699 pf->zero_new_blocks = true;
2700 pf->discard_enabled = true;
2701 pf->discard_passdown = true;
787a996c 2702 pf->error_if_no_space = false;
67e2e2b2
JT
2703}
2704
991d9fa0
JT
2705static void __pool_destroy(struct pool *pool)
2706{
2707 __pool_table_remove(pool);
2708
a822c83e 2709 vfree(pool->cell_sort_array);
991d9fa0
JT
2710 if (dm_pool_metadata_close(pool->pmd) < 0)
2711 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2712
44feb387 2713 dm_bio_prison_destroy(pool->prison);
991d9fa0
JT
2714 dm_kcopyd_client_destroy(pool->copier);
2715
2716 if (pool->wq)
2717 destroy_workqueue(pool->wq);
2718
2719 if (pool->next_mapping)
2720 mempool_free(pool->next_mapping, pool->mapping_pool);
2721 mempool_destroy(pool->mapping_pool);
44feb387
MS
2722 dm_deferred_set_destroy(pool->shared_read_ds);
2723 dm_deferred_set_destroy(pool->all_io_ds);
991d9fa0
JT
2724 kfree(pool);
2725}
2726
a24c2569 2727static struct kmem_cache *_new_mapping_cache;
a24c2569 2728
991d9fa0
JT
2729static struct pool *pool_create(struct mapped_device *pool_md,
2730 struct block_device *metadata_dev,
e49e5829
JT
2731 unsigned long block_size,
2732 int read_only, char **error)
991d9fa0
JT
2733{
2734 int r;
2735 void *err_p;
2736 struct pool *pool;
2737 struct dm_pool_metadata *pmd;
e49e5829 2738 bool format_device = read_only ? false : true;
991d9fa0 2739
e49e5829 2740 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
991d9fa0
JT
2741 if (IS_ERR(pmd)) {
2742 *error = "Error creating metadata object";
2743 return (struct pool *)pmd;
2744 }
2745
2746 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2747 if (!pool) {
2748 *error = "Error allocating memory for pool";
2749 err_p = ERR_PTR(-ENOMEM);
2750 goto bad_pool;
2751 }
2752
2753 pool->pmd = pmd;
2754 pool->sectors_per_block = block_size;
f9a8e0cd
MP
2755 if (block_size & (block_size - 1))
2756 pool->sectors_per_block_shift = -1;
2757 else
2758 pool->sectors_per_block_shift = __ffs(block_size);
991d9fa0 2759 pool->low_water_blocks = 0;
67e2e2b2 2760 pool_features_init(&pool->pf);
a195db2d 2761 pool->prison = dm_bio_prison_create();
991d9fa0
JT
2762 if (!pool->prison) {
2763 *error = "Error creating pool's bio prison";
2764 err_p = ERR_PTR(-ENOMEM);
2765 goto bad_prison;
2766 }
2767
df5d2e90 2768 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
991d9fa0
JT
2769 if (IS_ERR(pool->copier)) {
2770 r = PTR_ERR(pool->copier);
2771 *error = "Error creating pool's kcopyd client";
2772 err_p = ERR_PTR(r);
2773 goto bad_kcopyd_client;
2774 }
2775
2776 /*
2777 * Create singlethreaded workqueue that will service all devices
2778 * that use this metadata.
2779 */
2780 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2781 if (!pool->wq) {
2782 *error = "Error creating pool's workqueue";
2783 err_p = ERR_PTR(-ENOMEM);
2784 goto bad_wq;
2785 }
2786
7d327fe0 2787 throttle_init(&pool->throttle);
991d9fa0 2788 INIT_WORK(&pool->worker, do_worker);
905e51b3 2789 INIT_DELAYED_WORK(&pool->waker, do_waker);
85ad643b 2790 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
991d9fa0 2791 spin_lock_init(&pool->lock);
991d9fa0
JT
2792 bio_list_init(&pool->deferred_flush_bios);
2793 INIT_LIST_HEAD(&pool->prepared_mappings);
104655fd 2794 INIT_LIST_HEAD(&pool->prepared_discards);
c140e1c4 2795 INIT_LIST_HEAD(&pool->active_thins);
88a6621b 2796 pool->low_water_triggered = false;
80e96c54 2797 pool->suspended = true;
c3667cc6 2798 pool->out_of_data_space = false;
44feb387
MS
2799
2800 pool->shared_read_ds = dm_deferred_set_create();
2801 if (!pool->shared_read_ds) {
2802 *error = "Error creating pool's shared read deferred set";
2803 err_p = ERR_PTR(-ENOMEM);
2804 goto bad_shared_read_ds;
2805 }
2806
2807 pool->all_io_ds = dm_deferred_set_create();
2808 if (!pool->all_io_ds) {
2809 *error = "Error creating pool's all io deferred set";
2810 err_p = ERR_PTR(-ENOMEM);
2811 goto bad_all_io_ds;
2812 }
991d9fa0
JT
2813
2814 pool->next_mapping = NULL;
a24c2569
MS
2815 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2816 _new_mapping_cache);
991d9fa0
JT
2817 if (!pool->mapping_pool) {
2818 *error = "Error creating pool's mapping mempool";
2819 err_p = ERR_PTR(-ENOMEM);
2820 goto bad_mapping_pool;
2821 }
2822
a822c83e
JT
2823 pool->cell_sort_array = vmalloc(sizeof(*pool->cell_sort_array) * CELL_SORT_ARRAY_SIZE);
2824 if (!pool->cell_sort_array) {
2825 *error = "Error allocating cell sort array";
2826 err_p = ERR_PTR(-ENOMEM);
2827 goto bad_sort_array;
2828 }
2829
991d9fa0 2830 pool->ref_count = 1;
905e51b3 2831 pool->last_commit_jiffies = jiffies;
991d9fa0
JT
2832 pool->pool_md = pool_md;
2833 pool->md_dev = metadata_dev;
2834 __pool_table_insert(pool);
2835
2836 return pool;
2837
a822c83e
JT
2838bad_sort_array:
2839 mempool_destroy(pool->mapping_pool);
991d9fa0 2840bad_mapping_pool:
44feb387
MS
2841 dm_deferred_set_destroy(pool->all_io_ds);
2842bad_all_io_ds:
2843 dm_deferred_set_destroy(pool->shared_read_ds);
2844bad_shared_read_ds:
991d9fa0
JT
2845 destroy_workqueue(pool->wq);
2846bad_wq:
2847 dm_kcopyd_client_destroy(pool->copier);
2848bad_kcopyd_client:
44feb387 2849 dm_bio_prison_destroy(pool->prison);
991d9fa0
JT
2850bad_prison:
2851 kfree(pool);
2852bad_pool:
2853 if (dm_pool_metadata_close(pmd))
2854 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2855
2856 return err_p;
2857}
2858
2859static void __pool_inc(struct pool *pool)
2860{
2861 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2862 pool->ref_count++;
2863}
2864
2865static void __pool_dec(struct pool *pool)
2866{
2867 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2868 BUG_ON(!pool->ref_count);
2869 if (!--pool->ref_count)
2870 __pool_destroy(pool);
2871}
2872
2873static struct pool *__pool_find(struct mapped_device *pool_md,
2874 struct block_device *metadata_dev,
e49e5829
JT
2875 unsigned long block_size, int read_only,
2876 char **error, int *created)
991d9fa0
JT
2877{
2878 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2879
2880 if (pool) {
f09996c9
MS
2881 if (pool->pool_md != pool_md) {
2882 *error = "metadata device already in use by a pool";
991d9fa0 2883 return ERR_PTR(-EBUSY);
f09996c9 2884 }
991d9fa0
JT
2885 __pool_inc(pool);
2886
2887 } else {
2888 pool = __pool_table_lookup(pool_md);
2889 if (pool) {
f09996c9
MS
2890 if (pool->md_dev != metadata_dev) {
2891 *error = "different pool cannot replace a pool";
991d9fa0 2892 return ERR_PTR(-EINVAL);
f09996c9 2893 }
991d9fa0
JT
2894 __pool_inc(pool);
2895
67e2e2b2 2896 } else {
e49e5829 2897 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
67e2e2b2
JT
2898 *created = 1;
2899 }
991d9fa0
JT
2900 }
2901
2902 return pool;
2903}
2904
2905/*----------------------------------------------------------------
2906 * Pool target methods
2907 *--------------------------------------------------------------*/
2908static void pool_dtr(struct dm_target *ti)
2909{
2910 struct pool_c *pt = ti->private;
2911
2912 mutex_lock(&dm_thin_pool_table.mutex);
2913
2914 unbind_control_target(pt->pool, ti);
2915 __pool_dec(pt->pool);
2916 dm_put_device(ti, pt->metadata_dev);
2917 dm_put_device(ti, pt->data_dev);
2918 kfree(pt);
2919
2920 mutex_unlock(&dm_thin_pool_table.mutex);
2921}
2922
991d9fa0
JT
2923static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2924 struct dm_target *ti)
2925{
2926 int r;
2927 unsigned argc;
2928 const char *arg_name;
2929
2930 static struct dm_arg _args[] = {
74aa45c3 2931 {0, 4, "Invalid number of pool feature arguments"},
991d9fa0
JT
2932 };
2933
2934 /*
2935 * No feature arguments supplied.
2936 */
2937 if (!as->argc)
2938 return 0;
2939
2940 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2941 if (r)
2942 return -EINVAL;
2943
2944 while (argc && !r) {
2945 arg_name = dm_shift_arg(as);
2946 argc--;
2947
e49e5829 2948 if (!strcasecmp(arg_name, "skip_block_zeroing"))
9bc142dd 2949 pf->zero_new_blocks = false;
e49e5829
JT
2950
2951 else if (!strcasecmp(arg_name, "ignore_discard"))
9bc142dd 2952 pf->discard_enabled = false;
e49e5829
JT
2953
2954 else if (!strcasecmp(arg_name, "no_discard_passdown"))
9bc142dd 2955 pf->discard_passdown = false;
991d9fa0 2956
e49e5829
JT
2957 else if (!strcasecmp(arg_name, "read_only"))
2958 pf->mode = PM_READ_ONLY;
2959
787a996c
MS
2960 else if (!strcasecmp(arg_name, "error_if_no_space"))
2961 pf->error_if_no_space = true;
2962
e49e5829
JT
2963 else {
2964 ti->error = "Unrecognised pool feature requested";
2965 r = -EINVAL;
2966 break;
2967 }
991d9fa0
JT
2968 }
2969
2970 return r;
2971}
2972
ac8c3f3d
JT
2973static void metadata_low_callback(void *context)
2974{
2975 struct pool *pool = context;
2976
2977 DMWARN("%s: reached low water mark for metadata device: sending event.",
2978 dm_device_name(pool->pool_md));
2979
2980 dm_table_event(pool->ti->table);
2981}
2982
7d48935e
MS
2983static sector_t get_dev_size(struct block_device *bdev)
2984{
2985 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2986}
2987
2988static void warn_if_metadata_device_too_big(struct block_device *bdev)
b17446df 2989{
7d48935e 2990 sector_t metadata_dev_size = get_dev_size(bdev);
b17446df
JT
2991 char buffer[BDEVNAME_SIZE];
2992
7d48935e 2993 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
b17446df
JT
2994 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2995 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
7d48935e
MS
2996}
2997
2998static sector_t get_metadata_dev_size(struct block_device *bdev)
2999{
3000 sector_t metadata_dev_size = get_dev_size(bdev);
3001
3002 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3003 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
b17446df
JT
3004
3005 return metadata_dev_size;
3006}
3007
24347e95
JT
3008static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3009{
3010 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3011
7d48935e 3012 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
24347e95
JT
3013
3014 return metadata_dev_size;
3015}
3016
ac8c3f3d
JT
3017/*
3018 * When a metadata threshold is crossed a dm event is triggered, and
3019 * userland should respond by growing the metadata device. We could let
3020 * userland set the threshold, like we do with the data threshold, but I'm
3021 * not sure they know enough to do this well.
3022 */
3023static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3024{
3025 /*
3026 * 4M is ample for all ops with the possible exception of thin
3027 * device deletion which is harmless if it fails (just retry the
3028 * delete after you've grown the device).
3029 */
3030 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3031 return min((dm_block_t)1024ULL /* 4M */, quarter);
3032}
3033
991d9fa0
JT
3034/*
3035 * thin-pool <metadata dev> <data dev>
3036 * <data block size (sectors)>
3037 * <low water mark (blocks)>
3038 * [<#feature args> [<arg>]*]
3039 *
3040 * Optional feature arguments are:
3041 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
67e2e2b2
JT
3042 * ignore_discard: disable discard
3043 * no_discard_passdown: don't pass discards down to the data device
787a996c
MS
3044 * read_only: Don't allow any changes to be made to the pool metadata.
3045 * error_if_no_space: error IOs, instead of queueing, if no space.
991d9fa0
JT
3046 */
3047static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3048{
67e2e2b2 3049 int r, pool_created = 0;
991d9fa0
JT
3050 struct pool_c *pt;
3051 struct pool *pool;
3052 struct pool_features pf;
3053 struct dm_arg_set as;
3054 struct dm_dev *data_dev;
3055 unsigned long block_size;
3056 dm_block_t low_water_blocks;
3057 struct dm_dev *metadata_dev;
5d0db96d 3058 fmode_t metadata_mode;
991d9fa0
JT
3059
3060 /*
3061 * FIXME Remove validation from scope of lock.
3062 */
3063 mutex_lock(&dm_thin_pool_table.mutex);
3064
3065 if (argc < 4) {
3066 ti->error = "Invalid argument count";
3067 r = -EINVAL;
3068 goto out_unlock;
3069 }
5d0db96d 3070
991d9fa0
JT
3071 as.argc = argc;
3072 as.argv = argv;
3073
5d0db96d
JT
3074 /*
3075 * Set default pool features.
3076 */
3077 pool_features_init(&pf);
3078
3079 dm_consume_args(&as, 4);
3080 r = parse_pool_features(&as, &pf, ti);
3081 if (r)
3082 goto out_unlock;
3083
3084 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3085 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
991d9fa0
JT
3086 if (r) {
3087 ti->error = "Error opening metadata block device";
3088 goto out_unlock;
3089 }
7d48935e 3090 warn_if_metadata_device_too_big(metadata_dev->bdev);
991d9fa0
JT
3091
3092 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3093 if (r) {
3094 ti->error = "Error getting data device";
3095 goto out_metadata;
3096 }
3097
3098 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3099 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3100 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
55f2b8bd 3101 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
991d9fa0
JT
3102 ti->error = "Invalid block size";
3103 r = -EINVAL;
3104 goto out;
3105 }
3106
3107 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3108 ti->error = "Invalid low water mark";
3109 r = -EINVAL;
3110 goto out;
3111 }
3112
991d9fa0
JT
3113 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3114 if (!pt) {
3115 r = -ENOMEM;
3116 goto out;
3117 }
3118
3119 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
e49e5829 3120 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
991d9fa0
JT
3121 if (IS_ERR(pool)) {
3122 r = PTR_ERR(pool);
3123 goto out_free_pt;
3124 }
3125
67e2e2b2
JT
3126 /*
3127 * 'pool_created' reflects whether this is the first table load.
3128 * Top level discard support is not allowed to be changed after
3129 * initial load. This would require a pool reload to trigger thin
3130 * device changes.
3131 */
3132 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3133 ti->error = "Discard support cannot be disabled once enabled";
3134 r = -EINVAL;
3135 goto out_flags_changed;
3136 }
3137
991d9fa0
JT
3138 pt->pool = pool;
3139 pt->ti = ti;
3140 pt->metadata_dev = metadata_dev;
3141 pt->data_dev = data_dev;
3142 pt->low_water_blocks = low_water_blocks;
0424caa1 3143 pt->adjusted_pf = pt->requested_pf = pf;
55a62eef 3144 ti->num_flush_bios = 1;
9bc142dd 3145
67e2e2b2
JT
3146 /*
3147 * Only need to enable discards if the pool should pass
3148 * them down to the data device. The thin device's discard
3149 * processing will cause mappings to be removed from the btree.
3150 */
b60ab990 3151 ti->discard_zeroes_data_unsupported = true;
67e2e2b2 3152 if (pf.discard_enabled && pf.discard_passdown) {
55a62eef 3153 ti->num_discard_bios = 1;
9bc142dd 3154
67e2e2b2
JT
3155 /*
3156 * Setting 'discards_supported' circumvents the normal
3157 * stacking of discard limits (this keeps the pool and
3158 * thin devices' discard limits consistent).
3159 */
0ac55489 3160 ti->discards_supported = true;
67e2e2b2 3161 }
991d9fa0
JT
3162 ti->private = pt;
3163
ac8c3f3d
JT
3164 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3165 calc_metadata_threshold(pt),
3166 metadata_low_callback,
3167 pool);
3168 if (r)
ba30670f 3169 goto out_flags_changed;
ac8c3f3d 3170
991d9fa0
JT
3171 pt->callbacks.congested_fn = pool_is_congested;
3172 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
3173
3174 mutex_unlock(&dm_thin_pool_table.mutex);
3175
3176 return 0;
3177
67e2e2b2
JT
3178out_flags_changed:
3179 __pool_dec(pool);
991d9fa0
JT
3180out_free_pt:
3181 kfree(pt);
3182out:
3183 dm_put_device(ti, data_dev);
3184out_metadata:
3185 dm_put_device(ti, metadata_dev);
3186out_unlock:
3187 mutex_unlock(&dm_thin_pool_table.mutex);
3188
3189 return r;
3190}
3191
7de3ee57 3192static int pool_map(struct dm_target *ti, struct bio *bio)
991d9fa0
JT
3193{
3194 int r;
3195 struct pool_c *pt = ti->private;
3196 struct pool *pool = pt->pool;
3197 unsigned long flags;
3198
3199 /*
3200 * As this is a singleton target, ti->begin is always zero.
3201 */
3202 spin_lock_irqsave(&pool->lock, flags);
3203 bio->bi_bdev = pt->data_dev->bdev;
3204 r = DM_MAPIO_REMAPPED;
3205 spin_unlock_irqrestore(&pool->lock, flags);
3206
3207 return r;
3208}
3209
b17446df 3210static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
991d9fa0
JT
3211{
3212 int r;
3213 struct pool_c *pt = ti->private;
3214 struct pool *pool = pt->pool;
55f2b8bd
MS
3215 sector_t data_size = ti->len;
3216 dm_block_t sb_data_size;
991d9fa0 3217
b17446df 3218 *need_commit = false;
991d9fa0 3219
55f2b8bd
MS
3220 (void) sector_div(data_size, pool->sectors_per_block);
3221
991d9fa0
JT
3222 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3223 if (r) {
4fa5971a
MS
3224 DMERR("%s: failed to retrieve data device size",
3225 dm_device_name(pool->pool_md));
991d9fa0
JT
3226 return r;
3227 }
3228
3229 if (data_size < sb_data_size) {
4fa5971a
MS
3230 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3231 dm_device_name(pool->pool_md),
55f2b8bd 3232 (unsigned long long)data_size, sb_data_size);
991d9fa0
JT
3233 return -EINVAL;
3234
3235 } else if (data_size > sb_data_size) {
07f2b6e0
MS
3236 if (dm_pool_metadata_needs_check(pool->pmd)) {
3237 DMERR("%s: unable to grow the data device until repaired.",
3238 dm_device_name(pool->pool_md));
3239 return 0;
3240 }
3241
6f7f51d4
MS
3242 if (sb_data_size)
3243 DMINFO("%s: growing the data device from %llu to %llu blocks",
3244 dm_device_name(pool->pool_md),
3245 sb_data_size, (unsigned long long)data_size);
991d9fa0
JT
3246 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3247 if (r) {
b5330655 3248 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
991d9fa0
JT
3249 return r;
3250 }
3251
b17446df 3252 *need_commit = true;
991d9fa0
JT
3253 }
3254
3255 return 0;
3256}
3257
24347e95
JT
3258static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3259{
3260 int r;
3261 struct pool_c *pt = ti->private;
3262 struct pool *pool = pt->pool;
3263 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3264
3265 *need_commit = false;
3266
610bba8b 3267 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
24347e95
JT
3268
3269 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3270 if (r) {
4fa5971a
MS
3271 DMERR("%s: failed to retrieve metadata device size",
3272 dm_device_name(pool->pool_md));
24347e95
JT
3273 return r;
3274 }
3275
3276 if (metadata_dev_size < sb_metadata_dev_size) {
4fa5971a
MS
3277 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3278 dm_device_name(pool->pool_md),
24347e95
JT
3279 metadata_dev_size, sb_metadata_dev_size);
3280 return -EINVAL;
3281
3282 } else if (metadata_dev_size > sb_metadata_dev_size) {
07f2b6e0
MS
3283 if (dm_pool_metadata_needs_check(pool->pmd)) {
3284 DMERR("%s: unable to grow the metadata device until repaired.",
3285 dm_device_name(pool->pool_md));
3286 return 0;
3287 }
3288
7d48935e 3289 warn_if_metadata_device_too_big(pool->md_dev);
6f7f51d4
MS
3290 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3291 dm_device_name(pool->pool_md),
3292 sb_metadata_dev_size, metadata_dev_size);
24347e95
JT
3293 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3294 if (r) {
b5330655 3295 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
24347e95
JT
3296 return r;
3297 }
3298
3299 *need_commit = true;
3300 }
3301
3302 return 0;
3303}
3304
b17446df
JT
3305/*
3306 * Retrieves the number of blocks of the data device from
3307 * the superblock and compares it to the actual device size,
3308 * thus resizing the data device in case it has grown.
3309 *
3310 * This both copes with opening preallocated data devices in the ctr
3311 * being followed by a resume
3312 * -and-
3313 * calling the resume method individually after userspace has
3314 * grown the data device in reaction to a table event.
3315 */
3316static int pool_preresume(struct dm_target *ti)
3317{
3318 int r;
24347e95 3319 bool need_commit1, need_commit2;
b17446df
JT
3320 struct pool_c *pt = ti->private;
3321 struct pool *pool = pt->pool;
3322
3323 /*
3324 * Take control of the pool object.
3325 */
3326 r = bind_control_target(pool, ti);
3327 if (r)
3328 return r;
3329
3330 r = maybe_resize_data_dev(ti, &need_commit1);
3331 if (r)
3332 return r;
3333
24347e95
JT
3334 r = maybe_resize_metadata_dev(ti, &need_commit2);
3335 if (r)
3336 return r;
3337
3338 if (need_commit1 || need_commit2)
020cc3b5 3339 (void) commit(pool);
b17446df
JT
3340
3341 return 0;
3342}
3343
583024d2
MS
3344static void pool_suspend_active_thins(struct pool *pool)
3345{
3346 struct thin_c *tc;
3347
3348 /* Suspend all active thin devices */
3349 tc = get_first_thin(pool);
3350 while (tc) {
3351 dm_internal_suspend_noflush(tc->thin_md);
3352 tc = get_next_thin(pool, tc);
3353 }
3354}
3355
3356static void pool_resume_active_thins(struct pool *pool)
3357{
3358 struct thin_c *tc;
3359
3360 /* Resume all active thin devices */
3361 tc = get_first_thin(pool);
3362 while (tc) {
3363 dm_internal_resume(tc->thin_md);
3364 tc = get_next_thin(pool, tc);
3365 }
3366}
3367
991d9fa0
JT
3368static void pool_resume(struct dm_target *ti)
3369{
3370 struct pool_c *pt = ti->private;
3371 struct pool *pool = pt->pool;
3372 unsigned long flags;
3373
583024d2
MS
3374 /*
3375 * Must requeue active_thins' bios and then resume
3376 * active_thins _before_ clearing 'suspend' flag.
3377 */
3378 requeue_bios(pool);
3379 pool_resume_active_thins(pool);
3380
991d9fa0 3381 spin_lock_irqsave(&pool->lock, flags);
88a6621b 3382 pool->low_water_triggered = false;
80e96c54 3383 pool->suspended = false;
991d9fa0 3384 spin_unlock_irqrestore(&pool->lock, flags);
80e96c54 3385
905e51b3 3386 do_waker(&pool->waker.work);
991d9fa0
JT
3387}
3388
80e96c54
MS
3389static void pool_presuspend(struct dm_target *ti)
3390{
3391 struct pool_c *pt = ti->private;
3392 struct pool *pool = pt->pool;
3393 unsigned long flags;
3394
3395 spin_lock_irqsave(&pool->lock, flags);
3396 pool->suspended = true;
3397 spin_unlock_irqrestore(&pool->lock, flags);
583024d2
MS
3398
3399 pool_suspend_active_thins(pool);
80e96c54
MS
3400}
3401
3402static void pool_presuspend_undo(struct dm_target *ti)
3403{
3404 struct pool_c *pt = ti->private;
3405 struct pool *pool = pt->pool;
3406 unsigned long flags;
3407
583024d2
MS
3408 pool_resume_active_thins(pool);
3409
80e96c54
MS
3410 spin_lock_irqsave(&pool->lock, flags);
3411 pool->suspended = false;
3412 spin_unlock_irqrestore(&pool->lock, flags);
3413}
3414
991d9fa0
JT
3415static void pool_postsuspend(struct dm_target *ti)
3416{
991d9fa0
JT
3417 struct pool_c *pt = ti->private;
3418 struct pool *pool = pt->pool;
3419
18d03e8c
NB
3420 cancel_delayed_work_sync(&pool->waker);
3421 cancel_delayed_work_sync(&pool->no_space_timeout);
991d9fa0 3422 flush_workqueue(pool->wq);
020cc3b5 3423 (void) commit(pool);
991d9fa0
JT
3424}
3425
3426static int check_arg_count(unsigned argc, unsigned args_required)
3427{
3428 if (argc != args_required) {
3429 DMWARN("Message received with %u arguments instead of %u.",
3430 argc, args_required);
3431 return -EINVAL;
3432 }
3433
3434 return 0;
3435}
3436
3437static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3438{
3439 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3440 *dev_id <= MAX_DEV_ID)
3441 return 0;
3442
3443 if (warning)
3444 DMWARN("Message received with invalid device id: %s", arg);
3445
3446 return -EINVAL;
3447}
3448
3449static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3450{
3451 dm_thin_id dev_id;
3452 int r;
3453
3454 r = check_arg_count(argc, 2);
3455 if (r)
3456 return r;
3457
3458 r = read_dev_id(argv[1], &dev_id, 1);
3459 if (r)
3460 return r;
3461
3462 r = dm_pool_create_thin(pool->pmd, dev_id);
3463 if (r) {
3464 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3465 argv[1]);
3466 return r;
3467 }
3468
3469 return 0;
3470}
3471
3472static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3473{
3474 dm_thin_id dev_id;
3475 dm_thin_id origin_dev_id;
3476 int r;
3477
3478 r = check_arg_count(argc, 3);
3479 if (r)
3480 return r;
3481
3482 r = read_dev_id(argv[1], &dev_id, 1);
3483 if (r)
3484 return r;
3485
3486 r = read_dev_id(argv[2], &origin_dev_id, 1);
3487 if (r)
3488 return r;
3489
3490 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3491 if (r) {
3492 DMWARN("Creation of new snapshot %s of device %s failed.",
3493 argv[1], argv[2]);
3494 return r;
3495 }
3496
3497 return 0;
3498}
3499
3500static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3501{
3502 dm_thin_id dev_id;
3503 int r;
3504
3505 r = check_arg_count(argc, 2);
3506 if (r)
3507 return r;
3508
3509 r = read_dev_id(argv[1], &dev_id, 1);
3510 if (r)
3511 return r;
3512
3513 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3514 if (r)
3515 DMWARN("Deletion of thin device %s failed.", argv[1]);
3516
3517 return r;
3518}
3519
3520static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3521{
3522 dm_thin_id old_id, new_id;
3523 int r;
3524
3525 r = check_arg_count(argc, 3);
3526 if (r)
3527 return r;
3528
3529 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3530 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3531 return -EINVAL;
3532 }
3533
3534 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3535 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3536 return -EINVAL;
3537 }
3538
3539 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3540 if (r) {
3541 DMWARN("Failed to change transaction id from %s to %s.",
3542 argv[1], argv[2]);
3543 return r;
3544 }
3545
3546 return 0;
3547}
3548
cc8394d8
JT
3549static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3550{
3551 int r;
3552
3553 r = check_arg_count(argc, 1);
3554 if (r)
3555 return r;
3556
020cc3b5 3557 (void) commit(pool);
0d200aef 3558
cc8394d8
JT
3559 r = dm_pool_reserve_metadata_snap(pool->pmd);
3560 if (r)
3561 DMWARN("reserve_metadata_snap message failed.");
3562
3563 return r;
3564}
3565
3566static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3567{
3568 int r;
3569
3570 r = check_arg_count(argc, 1);
3571 if (r)
3572 return r;
3573
3574 r = dm_pool_release_metadata_snap(pool->pmd);
3575 if (r)
3576 DMWARN("release_metadata_snap message failed.");
3577
3578 return r;
3579}
3580
991d9fa0
JT
3581/*
3582 * Messages supported:
3583 * create_thin <dev_id>
3584 * create_snap <dev_id> <origin_id>
3585 * delete <dev_id>
991d9fa0 3586 * set_transaction_id <current_trans_id> <new_trans_id>
cc8394d8
JT
3587 * reserve_metadata_snap
3588 * release_metadata_snap
991d9fa0
JT
3589 */
3590static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3591{
3592 int r = -EINVAL;
3593 struct pool_c *pt = ti->private;
3594 struct pool *pool = pt->pool;
3595
2a7eaea0
JT
3596 if (get_pool_mode(pool) >= PM_READ_ONLY) {
3597 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3598 dm_device_name(pool->pool_md));
fd467696 3599 return -EOPNOTSUPP;
2a7eaea0
JT
3600 }
3601
991d9fa0
JT
3602 if (!strcasecmp(argv[0], "create_thin"))
3603 r = process_create_thin_mesg(argc, argv, pool);
3604
3605 else if (!strcasecmp(argv[0], "create_snap"))
3606 r = process_create_snap_mesg(argc, argv, pool);
3607
3608 else if (!strcasecmp(argv[0], "delete"))
3609 r = process_delete_mesg(argc, argv, pool);
3610
3611 else if (!strcasecmp(argv[0], "set_transaction_id"))
3612 r = process_set_transaction_id_mesg(argc, argv, pool);
3613
cc8394d8
JT
3614 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3615 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3616
3617 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3618 r = process_release_metadata_snap_mesg(argc, argv, pool);
3619
991d9fa0
JT
3620 else
3621 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3622
e49e5829 3623 if (!r)
020cc3b5 3624 (void) commit(pool);
991d9fa0
JT
3625
3626 return r;
3627}
3628
e49e5829
JT
3629static void emit_flags(struct pool_features *pf, char *result,
3630 unsigned sz, unsigned maxlen)
3631{
3632 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
787a996c
MS
3633 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3634 pf->error_if_no_space;
e49e5829
JT
3635 DMEMIT("%u ", count);
3636
3637 if (!pf->zero_new_blocks)
3638 DMEMIT("skip_block_zeroing ");
3639
3640 if (!pf->discard_enabled)
3641 DMEMIT("ignore_discard ");
3642
3643 if (!pf->discard_passdown)
3644 DMEMIT("no_discard_passdown ");
3645
3646 if (pf->mode == PM_READ_ONLY)
3647 DMEMIT("read_only ");
787a996c
MS
3648
3649 if (pf->error_if_no_space)
3650 DMEMIT("error_if_no_space ");
e49e5829
JT
3651}
3652
991d9fa0
JT
3653/*
3654 * Status line is:
3655 * <transaction id> <used metadata sectors>/<total metadata sectors>
3656 * <used data sectors>/<total data sectors> <held metadata root>
e4c78e21 3657 * <pool mode> <discard config> <no space config> <needs_check>
991d9fa0 3658 */
fd7c092e
MP
3659static void pool_status(struct dm_target *ti, status_type_t type,
3660 unsigned status_flags, char *result, unsigned maxlen)
991d9fa0 3661{
e49e5829 3662 int r;
991d9fa0
JT
3663 unsigned sz = 0;
3664 uint64_t transaction_id;
3665 dm_block_t nr_free_blocks_data;
3666 dm_block_t nr_free_blocks_metadata;
3667 dm_block_t nr_blocks_data;
3668 dm_block_t nr_blocks_metadata;
3669 dm_block_t held_root;
3670 char buf[BDEVNAME_SIZE];
3671 char buf2[BDEVNAME_SIZE];
3672 struct pool_c *pt = ti->private;
3673 struct pool *pool = pt->pool;
3674
3675 switch (type) {
3676 case STATUSTYPE_INFO:
e49e5829
JT
3677 if (get_pool_mode(pool) == PM_FAIL) {
3678 DMEMIT("Fail");
3679 break;
3680 }
3681
1f4e0ff0
AK
3682 /* Commit to ensure statistics aren't out-of-date */
3683 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
020cc3b5 3684 (void) commit(pool);
1f4e0ff0 3685
fd7c092e
MP
3686 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3687 if (r) {
4fa5971a
MS
3688 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3689 dm_device_name(pool->pool_md), r);
fd7c092e
MP
3690 goto err;
3691 }
991d9fa0 3692
fd7c092e
MP
3693 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3694 if (r) {
4fa5971a
MS
3695 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3696 dm_device_name(pool->pool_md), r);
fd7c092e
MP
3697 goto err;
3698 }
991d9fa0
JT
3699
3700 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
fd7c092e 3701 if (r) {
4fa5971a
MS
3702 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3703 dm_device_name(pool->pool_md), r);
fd7c092e
MP
3704 goto err;
3705 }
991d9fa0 3706
fd7c092e
MP
3707 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3708 if (r) {
4fa5971a
MS
3709 DMERR("%s: dm_pool_get_free_block_count returned %d",
3710 dm_device_name(pool->pool_md), r);
fd7c092e
MP
3711 goto err;
3712 }
991d9fa0
JT
3713
3714 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
fd7c092e 3715 if (r) {
4fa5971a
MS
3716 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3717 dm_device_name(pool->pool_md), r);
fd7c092e
MP
3718 goto err;
3719 }
991d9fa0 3720
cc8394d8 3721 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
fd7c092e 3722 if (r) {
4fa5971a
MS
3723 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3724 dm_device_name(pool->pool_md), r);
fd7c092e
MP
3725 goto err;
3726 }
991d9fa0
JT
3727
3728 DMEMIT("%llu %llu/%llu %llu/%llu ",
3729 (unsigned long long)transaction_id,
3730 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3731 (unsigned long long)nr_blocks_metadata,
3732 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3733 (unsigned long long)nr_blocks_data);
3734
3735 if (held_root)
e49e5829
JT
3736 DMEMIT("%llu ", held_root);
3737 else
3738 DMEMIT("- ");
3739
3e1a0699
JT
3740 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3741 DMEMIT("out_of_data_space ");
3742 else if (pool->pf.mode == PM_READ_ONLY)
e49e5829 3743 DMEMIT("ro ");
991d9fa0 3744 else
e49e5829
JT
3745 DMEMIT("rw ");
3746
018debea 3747 if (!pool->pf.discard_enabled)
787a996c 3748 DMEMIT("ignore_discard ");
018debea 3749 else if (pool->pf.discard_passdown)
787a996c
MS
3750 DMEMIT("discard_passdown ");
3751 else
3752 DMEMIT("no_discard_passdown ");
3753
3754 if (pool->pf.error_if_no_space)
3755 DMEMIT("error_if_no_space ");
e49e5829 3756 else
787a996c 3757 DMEMIT("queue_if_no_space ");
991d9fa0 3758
e4c78e21
MS
3759 if (dm_pool_metadata_needs_check(pool->pmd))
3760 DMEMIT("needs_check ");
3761 else
3762 DMEMIT("- ");
3763
991d9fa0
JT
3764 break;
3765
3766 case STATUSTYPE_TABLE:
3767 DMEMIT("%s %s %lu %llu ",
3768 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3769 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3770 (unsigned long)pool->sectors_per_block,
3771 (unsigned long long)pt->low_water_blocks);
0424caa1 3772 emit_flags(&pt->requested_pf, result, sz, maxlen);
991d9fa0
JT
3773 break;
3774 }
fd7c092e 3775 return;
991d9fa0 3776
fd7c092e
MP
3777err:
3778 DMEMIT("Error");
991d9fa0
JT
3779}
3780
3781static int pool_iterate_devices(struct dm_target *ti,
3782 iterate_devices_callout_fn fn, void *data)
3783{
3784 struct pool_c *pt = ti->private;
3785
3786 return fn(ti, pt->data_dev, 0, ti->len, data);
3787}
3788
991d9fa0
JT
3789static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3790{
3791 struct pool_c *pt = ti->private;
3792 struct pool *pool = pt->pool;
604ea906
MS
3793 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3794
3795 /*
d200c30e
MS
3796 * If max_sectors is smaller than pool->sectors_per_block adjust it
3797 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3798 * This is especially beneficial when the pool's data device is a RAID
3799 * device that has a full stripe width that matches pool->sectors_per_block
3800 * -- because even though partial RAID stripe-sized IOs will be issued to a
3801 * single RAID stripe; when aggregated they will end on a full RAID stripe
3802 * boundary.. which avoids additional partial RAID stripe writes cascading
604ea906 3803 */
604ea906
MS
3804 if (limits->max_sectors < pool->sectors_per_block) {
3805 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3806 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3807 limits->max_sectors--;
3808 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3809 }
604ea906 3810 }
991d9fa0 3811
0cc67cd9
MS
3812 /*
3813 * If the system-determined stacked limits are compatible with the
3814 * pool's blocksize (io_opt is a factor) do not override them.
3815 */
3816 if (io_opt_sectors < pool->sectors_per_block ||
604ea906
MS
3817 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3818 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3819 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3820 else
3821 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
0cc67cd9
MS
3822 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3823 }
0424caa1
MS
3824
3825 /*
3826 * pt->adjusted_pf is a staging area for the actual features to use.
3827 * They get transferred to the live pool in bind_control_target()
3828 * called from pool_preresume().
3829 */
b60ab990
MS
3830 if (!pt->adjusted_pf.discard_enabled) {
3831 /*
3832 * Must explicitly disallow stacking discard limits otherwise the
3833 * block layer will stack them if pool's data device has support.
3834 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3835 * user to see that, so make sure to set all discard limits to 0.
3836 */
3837 limits->discard_granularity = 0;
0424caa1 3838 return;
b60ab990 3839 }
0424caa1
MS
3840
3841 disable_passdown_if_not_supported(pt);
3842
34fbcf62
JT
3843 /*
3844 * The pool uses the same discard limits as the underlying data
3845 * device. DM core has already set this up.
3846 */
991d9fa0
JT
3847}
3848
3849static struct target_type pool_target = {
3850 .name = "thin-pool",
3851 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3852 DM_TARGET_IMMUTABLE,
c3667cc6 3853 .version = {1, 18, 0},
991d9fa0
JT
3854 .module = THIS_MODULE,
3855 .ctr = pool_ctr,
3856 .dtr = pool_dtr,
3857 .map = pool_map,
80e96c54
MS
3858 .presuspend = pool_presuspend,
3859 .presuspend_undo = pool_presuspend_undo,
991d9fa0
JT
3860 .postsuspend = pool_postsuspend,
3861 .preresume = pool_preresume,
3862 .resume = pool_resume,
3863 .message = pool_message,
3864 .status = pool_status,
991d9fa0
JT
3865 .iterate_devices = pool_iterate_devices,
3866 .io_hints = pool_io_hints,
3867};
3868
3869/*----------------------------------------------------------------
3870 * Thin target methods
3871 *--------------------------------------------------------------*/
b10ebd34
JT
3872static void thin_get(struct thin_c *tc)
3873{
3874 atomic_inc(&tc->refcount);
3875}
3876
3877static void thin_put(struct thin_c *tc)
3878{
3879 if (atomic_dec_and_test(&tc->refcount))
3880 complete(&tc->can_destroy);
3881}
3882
991d9fa0
JT
3883static void thin_dtr(struct dm_target *ti)
3884{
3885 struct thin_c *tc = ti->private;
c140e1c4
MS
3886 unsigned long flags;
3887
3888 spin_lock_irqsave(&tc->pool->lock, flags);
3889 list_del_rcu(&tc->list);
3890 spin_unlock_irqrestore(&tc->pool->lock, flags);
3891 synchronize_rcu();
991d9fa0 3892
17181fb7
MP
3893 thin_put(tc);
3894 wait_for_completion(&tc->can_destroy);
3895
991d9fa0
JT
3896 mutex_lock(&dm_thin_pool_table.mutex);
3897
3898 __pool_dec(tc->pool);
3899 dm_pool_close_thin_device(tc->td);
3900 dm_put_device(ti, tc->pool_dev);
2dd9c257
JT
3901 if (tc->origin_dev)
3902 dm_put_device(ti, tc->origin_dev);
991d9fa0
JT
3903 kfree(tc);
3904
3905 mutex_unlock(&dm_thin_pool_table.mutex);
3906}
3907
3908/*
3909 * Thin target parameters:
3910 *
2dd9c257 3911 * <pool_dev> <dev_id> [origin_dev]
991d9fa0
JT
3912 *
3913 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3914 * dev_id: the internal device identifier
2dd9c257 3915 * origin_dev: a device external to the pool that should act as the origin
67e2e2b2
JT
3916 *
3917 * If the pool device has discards disabled, they get disabled for the thin
3918 * device as well.
991d9fa0
JT
3919 */
3920static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3921{
3922 int r;
3923 struct thin_c *tc;
2dd9c257 3924 struct dm_dev *pool_dev, *origin_dev;
991d9fa0 3925 struct mapped_device *pool_md;
5e3283e2 3926 unsigned long flags;
991d9fa0
JT
3927
3928 mutex_lock(&dm_thin_pool_table.mutex);
3929
2dd9c257 3930 if (argc != 2 && argc != 3) {
991d9fa0
JT
3931 ti->error = "Invalid argument count";
3932 r = -EINVAL;
3933 goto out_unlock;
3934 }
3935
3936 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3937 if (!tc) {
3938 ti->error = "Out of memory";
3939 r = -ENOMEM;
3940 goto out_unlock;
3941 }
583024d2 3942 tc->thin_md = dm_table_get_md(ti->table);
c140e1c4 3943 spin_lock_init(&tc->lock);
a374bb21 3944 INIT_LIST_HEAD(&tc->deferred_cells);
c140e1c4
MS
3945 bio_list_init(&tc->deferred_bio_list);
3946 bio_list_init(&tc->retry_on_resume_list);
67324ea1 3947 tc->sort_bio_list = RB_ROOT;
991d9fa0 3948
2dd9c257
JT
3949 if (argc == 3) {
3950 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3951 if (r) {
3952 ti->error = "Error opening origin device";
3953 goto bad_origin_dev;
3954 }
3955 tc->origin_dev = origin_dev;
3956 }
3957
991d9fa0
JT
3958 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3959 if (r) {
3960 ti->error = "Error opening pool device";
3961 goto bad_pool_dev;
3962 }
3963 tc->pool_dev = pool_dev;
3964
3965 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3966 ti->error = "Invalid device id";
3967 r = -EINVAL;
3968 goto bad_common;
3969 }
3970
3971 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3972 if (!pool_md) {
3973 ti->error = "Couldn't get pool mapped device";
3974 r = -EINVAL;
3975 goto bad_common;
3976 }
3977
3978 tc->pool = __pool_table_lookup(pool_md);
3979 if (!tc->pool) {
3980 ti->error = "Couldn't find pool object";
3981 r = -EINVAL;
3982 goto bad_pool_lookup;
3983 }
3984 __pool_inc(tc->pool);
3985
e49e5829
JT
3986 if (get_pool_mode(tc->pool) == PM_FAIL) {
3987 ti->error = "Couldn't open thin device, Pool is in fail mode";
1acacc07 3988 r = -EINVAL;
80e96c54 3989 goto bad_pool;
e49e5829
JT
3990 }
3991
991d9fa0
JT
3992 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3993 if (r) {
3994 ti->error = "Couldn't open thin internal device";
80e96c54 3995 goto bad_pool;
991d9fa0
JT
3996 }
3997
542f9038
MS
3998 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3999 if (r)
80e96c54 4000 goto bad;
542f9038 4001
55a62eef 4002 ti->num_flush_bios = 1;
16ad3d10 4003 ti->flush_supported = true;
30187e1d 4004 ti->per_io_data_size = sizeof(struct dm_thin_endio_hook);
67e2e2b2
JT
4005
4006 /* In case the pool supports discards, pass them on. */
b60ab990 4007 ti->discard_zeroes_data_unsupported = true;
67e2e2b2 4008 if (tc->pool->pf.discard_enabled) {
0ac55489 4009 ti->discards_supported = true;
55a62eef 4010 ti->num_discard_bios = 1;
34fbcf62 4011 ti->split_discard_bios = false;
67e2e2b2 4012 }
991d9fa0 4013
991d9fa0
JT
4014 mutex_unlock(&dm_thin_pool_table.mutex);
4015
5e3283e2 4016 spin_lock_irqsave(&tc->pool->lock, flags);
80e96c54
MS
4017 if (tc->pool->suspended) {
4018 spin_unlock_irqrestore(&tc->pool->lock, flags);
4019 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4020 ti->error = "Unable to activate thin device while pool is suspended";
4021 r = -EINVAL;
4022 goto bad;
4023 }
2b94e896
MD
4024 atomic_set(&tc->refcount, 1);
4025 init_completion(&tc->can_destroy);
c140e1c4 4026 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
5e3283e2 4027 spin_unlock_irqrestore(&tc->pool->lock, flags);
c140e1c4
MS
4028 /*
4029 * This synchronize_rcu() call is needed here otherwise we risk a
4030 * wake_worker() call finding no bios to process (because the newly
4031 * added tc isn't yet visible). So this reduces latency since we
4032 * aren't then dependent on the periodic commit to wake_worker().
4033 */
4034 synchronize_rcu();
4035
80e96c54
MS
4036 dm_put(pool_md);
4037
991d9fa0
JT
4038 return 0;
4039
80e96c54 4040bad:
1acacc07 4041 dm_pool_close_thin_device(tc->td);
80e96c54 4042bad_pool:
991d9fa0
JT
4043 __pool_dec(tc->pool);
4044bad_pool_lookup:
4045 dm_put(pool_md);
4046bad_common:
4047 dm_put_device(ti, tc->pool_dev);
4048bad_pool_dev:
2dd9c257
JT
4049 if (tc->origin_dev)
4050 dm_put_device(ti, tc->origin_dev);
4051bad_origin_dev:
991d9fa0
JT
4052 kfree(tc);
4053out_unlock:
4054 mutex_unlock(&dm_thin_pool_table.mutex);
4055
4056 return r;
4057}
4058
7de3ee57 4059static int thin_map(struct dm_target *ti, struct bio *bio)
991d9fa0 4060{
4f024f37 4061 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
991d9fa0 4062
7de3ee57 4063 return thin_bio_map(ti, bio);
991d9fa0
JT
4064}
4065
7de3ee57 4066static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
eb2aa48d
JT
4067{
4068 unsigned long flags;
59c3d2c6 4069 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
eb2aa48d 4070 struct list_head work;
a24c2569 4071 struct dm_thin_new_mapping *m, *tmp;
eb2aa48d
JT
4072 struct pool *pool = h->tc->pool;
4073
4074 if (h->shared_read_entry) {
4075 INIT_LIST_HEAD(&work);
44feb387 4076 dm_deferred_entry_dec(h->shared_read_entry, &work);
eb2aa48d
JT
4077
4078 spin_lock_irqsave(&pool->lock, flags);
4079 list_for_each_entry_safe(m, tmp, &work, list) {
4080 list_del(&m->list);
50f3c3ef 4081 __complete_mapping_preparation(m);
eb2aa48d
JT
4082 }
4083 spin_unlock_irqrestore(&pool->lock, flags);
4084 }
4085
104655fd
JT
4086 if (h->all_io_entry) {
4087 INIT_LIST_HEAD(&work);
44feb387 4088 dm_deferred_entry_dec(h->all_io_entry, &work);
563af186
JT
4089 if (!list_empty(&work)) {
4090 spin_lock_irqsave(&pool->lock, flags);
4091 list_for_each_entry_safe(m, tmp, &work, list)
daec338b 4092 list_add_tail(&m->list, &pool->prepared_discards);
563af186
JT
4093 spin_unlock_irqrestore(&pool->lock, flags);
4094 wake_worker(pool);
4095 }
104655fd
JT
4096 }
4097
34fbcf62
JT
4098 if (h->cell)
4099 cell_defer_no_holder(h->tc, h->cell);
4100
eb2aa48d
JT
4101 return 0;
4102}
4103
738211f7 4104static void thin_presuspend(struct dm_target *ti)
991d9fa0 4105{
738211f7
JT
4106 struct thin_c *tc = ti->private;
4107
991d9fa0 4108 if (dm_noflush_suspending(ti))
738211f7
JT
4109 noflush_work(tc, do_noflush_start);
4110}
4111
4112static void thin_postsuspend(struct dm_target *ti)
4113{
4114 struct thin_c *tc = ti->private;
4115
4116 /*
4117 * The dm_noflush_suspending flag has been cleared by now, so
4118 * unfortunately we must always run this.
4119 */
4120 noflush_work(tc, do_noflush_stop);
991d9fa0
JT
4121}
4122
e5aea7b4
JT
4123static int thin_preresume(struct dm_target *ti)
4124{
4125 struct thin_c *tc = ti->private;
4126
4127 if (tc->origin_dev)
4128 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4129
4130 return 0;
4131}
4132
991d9fa0
JT
4133/*
4134 * <nr mapped sectors> <highest mapped sector>
4135 */
fd7c092e
MP
4136static void thin_status(struct dm_target *ti, status_type_t type,
4137 unsigned status_flags, char *result, unsigned maxlen)
991d9fa0
JT
4138{
4139 int r;
4140 ssize_t sz = 0;
4141 dm_block_t mapped, highest;
4142 char buf[BDEVNAME_SIZE];
4143 struct thin_c *tc = ti->private;
4144
e49e5829
JT
4145 if (get_pool_mode(tc->pool) == PM_FAIL) {
4146 DMEMIT("Fail");
fd7c092e 4147 return;
e49e5829
JT
4148 }
4149
991d9fa0
JT
4150 if (!tc->td)
4151 DMEMIT("-");
4152 else {
4153 switch (type) {
4154 case STATUSTYPE_INFO:
4155 r = dm_thin_get_mapped_count(tc->td, &mapped);
fd7c092e
MP
4156 if (r) {
4157 DMERR("dm_thin_get_mapped_count returned %d", r);
4158 goto err;
4159 }
991d9fa0
JT
4160
4161 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
fd7c092e
MP
4162 if (r < 0) {
4163 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4164 goto err;
4165 }
991d9fa0
JT
4166
4167 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4168 if (r)
4169 DMEMIT("%llu", ((highest + 1) *
4170 tc->pool->sectors_per_block) - 1);
4171 else
4172 DMEMIT("-");
4173 break;
4174
4175 case STATUSTYPE_TABLE:
4176 DMEMIT("%s %lu",
4177 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4178 (unsigned long) tc->dev_id);
2dd9c257
JT
4179 if (tc->origin_dev)
4180 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
991d9fa0
JT
4181 break;
4182 }
4183 }
4184
fd7c092e
MP
4185 return;
4186
4187err:
4188 DMEMIT("Error");
991d9fa0
JT
4189}
4190
4191static int thin_iterate_devices(struct dm_target *ti,
4192 iterate_devices_callout_fn fn, void *data)
4193{
55f2b8bd 4194 sector_t blocks;
991d9fa0 4195 struct thin_c *tc = ti->private;
55f2b8bd 4196 struct pool *pool = tc->pool;
991d9fa0
JT
4197
4198 /*
4199 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4200 * we follow a more convoluted path through to the pool's target.
4201 */
55f2b8bd 4202 if (!pool->ti)
991d9fa0
JT
4203 return 0; /* nothing is bound */
4204
55f2b8bd
MS
4205 blocks = pool->ti->len;
4206 (void) sector_div(blocks, pool->sectors_per_block);
991d9fa0 4207 if (blocks)
55f2b8bd 4208 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
991d9fa0
JT
4209
4210 return 0;
4211}
4212
34fbcf62
JT
4213static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4214{
4215 struct thin_c *tc = ti->private;
4216 struct pool *pool = tc->pool;
21607670 4217
0fcb04d5
MS
4218 if (!pool->pf.discard_enabled)
4219 return;
34fbcf62
JT
4220
4221 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4222 limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4223}
4224
991d9fa0
JT
4225static struct target_type thin_target = {
4226 .name = "thin",
c3667cc6 4227 .version = {1, 18, 0},
991d9fa0
JT
4228 .module = THIS_MODULE,
4229 .ctr = thin_ctr,
4230 .dtr = thin_dtr,
4231 .map = thin_map,
eb2aa48d 4232 .end_io = thin_endio,
e5aea7b4 4233 .preresume = thin_preresume,
738211f7 4234 .presuspend = thin_presuspend,
991d9fa0
JT
4235 .postsuspend = thin_postsuspend,
4236 .status = thin_status,
4237 .iterate_devices = thin_iterate_devices,
34fbcf62 4238 .io_hints = thin_io_hints,
991d9fa0
JT
4239};
4240
4241/*----------------------------------------------------------------*/
4242
4243static int __init dm_thin_init(void)
4244{
4245 int r;
4246
4247 pool_table_init();
4248
4249 r = dm_register_target(&thin_target);
4250 if (r)
4251 return r;
4252
4253 r = dm_register_target(&pool_target);
4254 if (r)
a24c2569
MS
4255 goto bad_pool_target;
4256
4257 r = -ENOMEM;
4258
a24c2569
MS
4259 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4260 if (!_new_mapping_cache)
4261 goto bad_new_mapping_cache;
4262
a24c2569
MS
4263 return 0;
4264
a24c2569 4265bad_new_mapping_cache:
a24c2569
MS
4266 dm_unregister_target(&pool_target);
4267bad_pool_target:
4268 dm_unregister_target(&thin_target);
991d9fa0
JT
4269
4270 return r;
4271}
4272
4273static void dm_thin_exit(void)
4274{
4275 dm_unregister_target(&thin_target);
4276 dm_unregister_target(&pool_target);
a24c2569 4277
a24c2569 4278 kmem_cache_destroy(_new_mapping_cache);
991d9fa0
JT
4279}
4280
4281module_init(dm_thin_init);
4282module_exit(dm_thin_exit);
4283
80c57893
MS
4284module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4285MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4286
7cab8bf1 4287MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
991d9fa0
JT
4288MODULE_AUTHOR("Joe Thornber <[email protected]>");
4289MODULE_LICENSE("GPL");
This page took 0.900669 seconds and 4 git commands to generate.