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