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