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