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