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