dm persistent data: use DMERR_LIMIT for errors
[linux-2.6-block.git] / drivers / md / dm-thin.c
CommitLineData
991d9fa0 1/*
e49e5829 2 * Copyright (C) 2011-2012 Red Hat UK.
991d9fa0
JT
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-thin-metadata.h"
4f81a417 8#include "dm-bio-prison.h"
1f4e0ff0 9#include "dm.h"
991d9fa0
JT
10
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
14#include <linux/list.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18
19#define DM_MSG_PREFIX "thin"
20
21/*
22 * Tunable constants
23 */
7768ed33 24#define ENDIO_HOOK_POOL_SIZE 1024
991d9fa0
JT
25#define MAPPING_POOL_SIZE 1024
26#define PRISON_CELLS 1024
905e51b3 27#define COMMIT_PERIOD HZ
991d9fa0
JT
28
29/*
30 * The block size of the device holding pool data must be
31 * between 64KB and 1GB.
32 */
33#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
34#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
35
991d9fa0
JT
36/*
37 * Device id is restricted to 24 bits.
38 */
39#define MAX_DEV_ID ((1 << 24) - 1)
40
41/*
42 * How do we handle breaking sharing of data blocks?
43 * =================================================
44 *
45 * We use a standard copy-on-write btree to store the mappings for the
46 * devices (note I'm talking about copy-on-write of the metadata here, not
47 * the data). When you take an internal snapshot you clone the root node
48 * of the origin btree. After this there is no concept of an origin or a
49 * snapshot. They are just two device trees that happen to point to the
50 * same data blocks.
51 *
52 * When we get a write in we decide if it's to a shared data block using
53 * some timestamp magic. If it is, we have to break sharing.
54 *
55 * Let's say we write to a shared block in what was the origin. The
56 * steps are:
57 *
58 * i) plug io further to this physical block. (see bio_prison code).
59 *
60 * ii) quiesce any read io to that shared data block. Obviously
44feb387 61 * including all devices that share this block. (see dm_deferred_set code)
991d9fa0
JT
62 *
63 * iii) copy the data block to a newly allocate block. This step can be
64 * missed out if the io covers the block. (schedule_copy).
65 *
66 * iv) insert the new mapping into the origin's btree
fe878f34 67 * (process_prepared_mapping). This act of inserting breaks some
991d9fa0
JT
68 * sharing of btree nodes between the two devices. Breaking sharing only
69 * effects the btree of that specific device. Btrees for the other
70 * devices that share the block never change. The btree for the origin
71 * device as it was after the last commit is untouched, ie. we're using
72 * persistent data structures in the functional programming sense.
73 *
74 * v) unplug io to this physical block, including the io that triggered
75 * the breaking of sharing.
76 *
77 * Steps (ii) and (iii) occur in parallel.
78 *
79 * The metadata _doesn't_ need to be committed before the io continues. We
80 * get away with this because the io is always written to a _new_ block.
81 * If there's a crash, then:
82 *
83 * - The origin mapping will point to the old origin block (the shared
84 * one). This will contain the data as it was before the io that triggered
85 * the breaking of sharing came in.
86 *
87 * - The snap mapping still points to the old block. As it would after
88 * the commit.
89 *
90 * The downside of this scheme is the timestamp magic isn't perfect, and
91 * will continue to think that data block in the snapshot device is shared
92 * even after the write to the origin has broken sharing. I suspect data
93 * blocks will typically be shared by many different devices, so we're
94 * breaking sharing n + 1 times, rather than n, where n is the number of
95 * devices that reference this data block. At the moment I think the
96 * benefits far, far outweigh the disadvantages.
97 */
98
99/*----------------------------------------------------------------*/
100
991d9fa0
JT
101/*
102 * Key building.
103 */
104static void build_data_key(struct dm_thin_device *td,
44feb387 105 dm_block_t b, struct dm_cell_key *key)
991d9fa0
JT
106{
107 key->virtual = 0;
108 key->dev = dm_thin_dev_id(td);
109 key->block = b;
110}
111
112static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
44feb387 113 struct dm_cell_key *key)
991d9fa0
JT
114{
115 key->virtual = 1;
116 key->dev = dm_thin_dev_id(td);
117 key->block = b;
118}
119
120/*----------------------------------------------------------------*/
121
122/*
123 * A pool device ties together a metadata device and a data device. It
124 * also provides the interface for creating and destroying internal
125 * devices.
126 */
a24c2569 127struct dm_thin_new_mapping;
67e2e2b2 128
e49e5829
JT
129/*
130 * The pool runs in 3 modes. Ordered in degraded order for comparisons.
131 */
132enum pool_mode {
133 PM_WRITE, /* metadata may be changed */
134 PM_READ_ONLY, /* metadata may not be changed */
135 PM_FAIL, /* all I/O fails */
136};
137
67e2e2b2 138struct pool_features {
e49e5829
JT
139 enum pool_mode mode;
140
9bc142dd
MS
141 bool zero_new_blocks:1;
142 bool discard_enabled:1;
143 bool discard_passdown:1;
67e2e2b2
JT
144};
145
e49e5829
JT
146struct thin_c;
147typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
148typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
149
991d9fa0
JT
150struct pool {
151 struct list_head list;
152 struct dm_target *ti; /* Only set if a pool target is bound */
153
154 struct mapped_device *pool_md;
155 struct block_device *md_dev;
156 struct dm_pool_metadata *pmd;
157
991d9fa0 158 dm_block_t low_water_blocks;
55f2b8bd 159 uint32_t sectors_per_block;
f9a8e0cd 160 int sectors_per_block_shift;
991d9fa0 161
67e2e2b2 162 struct pool_features pf;
991d9fa0
JT
163 unsigned low_water_triggered:1; /* A dm event has been sent */
164 unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
165
44feb387 166 struct dm_bio_prison *prison;
991d9fa0
JT
167 struct dm_kcopyd_client *copier;
168
169 struct workqueue_struct *wq;
170 struct work_struct worker;
905e51b3 171 struct delayed_work waker;
991d9fa0 172
905e51b3 173 unsigned long last_commit_jiffies;
55f2b8bd 174 unsigned ref_count;
991d9fa0
JT
175
176 spinlock_t lock;
177 struct bio_list deferred_bios;
178 struct bio_list deferred_flush_bios;
179 struct list_head prepared_mappings;
104655fd 180 struct list_head prepared_discards;
991d9fa0
JT
181
182 struct bio_list retry_on_resume_list;
183
44feb387
MS
184 struct dm_deferred_set *shared_read_ds;
185 struct dm_deferred_set *all_io_ds;
991d9fa0 186
a24c2569 187 struct dm_thin_new_mapping *next_mapping;
991d9fa0
JT
188 mempool_t *mapping_pool;
189 mempool_t *endio_hook_pool;
e49e5829
JT
190
191 process_bio_fn process_bio;
192 process_bio_fn process_discard;
193
194 process_mapping_fn process_prepared_mapping;
195 process_mapping_fn process_prepared_discard;
991d9fa0
JT
196};
197
e49e5829
JT
198static enum pool_mode get_pool_mode(struct pool *pool);
199static void set_pool_mode(struct pool *pool, enum pool_mode mode);
200
991d9fa0
JT
201/*
202 * Target context for a pool.
203 */
204struct pool_c {
205 struct dm_target *ti;
206 struct pool *pool;
207 struct dm_dev *data_dev;
208 struct dm_dev *metadata_dev;
209 struct dm_target_callbacks callbacks;
210
211 dm_block_t low_water_blocks;
0424caa1
MS
212 struct pool_features requested_pf; /* Features requested during table load */
213 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
991d9fa0
JT
214};
215
216/*
217 * Target context for a thin.
218 */
219struct thin_c {
220 struct dm_dev *pool_dev;
2dd9c257 221 struct dm_dev *origin_dev;
991d9fa0
JT
222 dm_thin_id dev_id;
223
224 struct pool *pool;
225 struct dm_thin_device *td;
226};
227
228/*----------------------------------------------------------------*/
229
230/*
231 * A global list of pools that uses a struct mapped_device as a key.
232 */
233static struct dm_thin_pool_table {
234 struct mutex mutex;
235 struct list_head pools;
236} dm_thin_pool_table;
237
238static void pool_table_init(void)
239{
240 mutex_init(&dm_thin_pool_table.mutex);
241 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
242}
243
244static void __pool_table_insert(struct pool *pool)
245{
246 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
247 list_add(&pool->list, &dm_thin_pool_table.pools);
248}
249
250static void __pool_table_remove(struct pool *pool)
251{
252 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
253 list_del(&pool->list);
254}
255
256static struct pool *__pool_table_lookup(struct mapped_device *md)
257{
258 struct pool *pool = NULL, *tmp;
259
260 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
261
262 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
263 if (tmp->pool_md == md) {
264 pool = tmp;
265 break;
266 }
267 }
268
269 return pool;
270}
271
272static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
273{
274 struct pool *pool = NULL, *tmp;
275
276 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
277
278 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
279 if (tmp->md_dev == md_dev) {
280 pool = tmp;
281 break;
282 }
283 }
284
285 return pool;
286}
287
288/*----------------------------------------------------------------*/
289
a24c2569 290struct dm_thin_endio_hook {
eb2aa48d 291 struct thin_c *tc;
44feb387
MS
292 struct dm_deferred_entry *shared_read_entry;
293 struct dm_deferred_entry *all_io_entry;
a24c2569 294 struct dm_thin_new_mapping *overwrite_mapping;
eb2aa48d
JT
295};
296
991d9fa0
JT
297static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
298{
299 struct bio *bio;
300 struct bio_list bios;
301
302 bio_list_init(&bios);
303 bio_list_merge(&bios, master);
304 bio_list_init(master);
305
306 while ((bio = bio_list_pop(&bios))) {
a24c2569
MS
307 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
308
eb2aa48d 309 if (h->tc == tc)
991d9fa0
JT
310 bio_endio(bio, DM_ENDIO_REQUEUE);
311 else
312 bio_list_add(master, bio);
313 }
314}
315
316static void requeue_io(struct thin_c *tc)
317{
318 struct pool *pool = tc->pool;
319 unsigned long flags;
320
321 spin_lock_irqsave(&pool->lock, flags);
322 __requeue_bio_list(tc, &pool->deferred_bios);
323 __requeue_bio_list(tc, &pool->retry_on_resume_list);
324 spin_unlock_irqrestore(&pool->lock, flags);
325}
326
327/*
328 * This section of code contains the logic for processing a thin device's IO.
329 * Much of the code depends on pool object resources (lists, workqueues, etc)
330 * but most is exclusively called from the thin target rather than the thin-pool
331 * target.
332 */
333
334static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
335{
55f2b8bd
MS
336 sector_t block_nr = bio->bi_sector;
337
f9a8e0cd
MP
338 if (tc->pool->sectors_per_block_shift < 0)
339 (void) sector_div(block_nr, tc->pool->sectors_per_block);
340 else
341 block_nr >>= tc->pool->sectors_per_block_shift;
55f2b8bd
MS
342
343 return block_nr;
991d9fa0
JT
344}
345
346static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
347{
348 struct pool *pool = tc->pool;
55f2b8bd 349 sector_t bi_sector = bio->bi_sector;
991d9fa0
JT
350
351 bio->bi_bdev = tc->pool_dev->bdev;
f9a8e0cd
MP
352 if (tc->pool->sectors_per_block_shift < 0)
353 bio->bi_sector = (block * pool->sectors_per_block) +
354 sector_div(bi_sector, pool->sectors_per_block);
355 else
356 bio->bi_sector = (block << pool->sectors_per_block_shift) |
357 (bi_sector & (pool->sectors_per_block - 1));
991d9fa0
JT
358}
359
2dd9c257
JT
360static void remap_to_origin(struct thin_c *tc, struct bio *bio)
361{
362 bio->bi_bdev = tc->origin_dev->bdev;
363}
364
4afdd680
JT
365static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
366{
367 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
368 dm_thin_changed_this_transaction(tc->td);
369}
370
e8088073
JT
371static void inc_all_io_entry(struct pool *pool, struct bio *bio)
372{
373 struct dm_thin_endio_hook *h;
374
375 if (bio->bi_rw & REQ_DISCARD)
376 return;
377
378 h = dm_get_mapinfo(bio)->ptr;
379 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
380}
381
2dd9c257 382static void issue(struct thin_c *tc, struct bio *bio)
991d9fa0
JT
383{
384 struct pool *pool = tc->pool;
385 unsigned long flags;
386
e49e5829
JT
387 if (!bio_triggers_commit(tc, bio)) {
388 generic_make_request(bio);
389 return;
390 }
391
991d9fa0 392 /*
e49e5829
JT
393 * Complete bio with an error if earlier I/O caused changes to
394 * the metadata that can't be committed e.g, due to I/O errors
395 * on the metadata device.
991d9fa0 396 */
e49e5829
JT
397 if (dm_thin_aborted_changes(tc->td)) {
398 bio_io_error(bio);
399 return;
400 }
401
402 /*
403 * Batch together any bios that trigger commits and then issue a
404 * single commit for them in process_deferred_bios().
405 */
406 spin_lock_irqsave(&pool->lock, flags);
407 bio_list_add(&pool->deferred_flush_bios, bio);
408 spin_unlock_irqrestore(&pool->lock, flags);
991d9fa0
JT
409}
410
2dd9c257
JT
411static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
412{
413 remap_to_origin(tc, bio);
414 issue(tc, bio);
415}
416
417static void remap_and_issue(struct thin_c *tc, struct bio *bio,
418 dm_block_t block)
419{
420 remap(tc, bio, block);
421 issue(tc, bio);
422}
423
991d9fa0
JT
424/*
425 * wake_worker() is used when new work is queued and when pool_resume is
426 * ready to continue deferred IO processing.
427 */
428static void wake_worker(struct pool *pool)
429{
430 queue_work(pool->wq, &pool->worker);
431}
432
433/*----------------------------------------------------------------*/
434
435/*
436 * Bio endio functions.
437 */
a24c2569 438struct dm_thin_new_mapping {
991d9fa0
JT
439 struct list_head list;
440
eb2aa48d
JT
441 unsigned quiesced:1;
442 unsigned prepared:1;
104655fd 443 unsigned pass_discard:1;
991d9fa0
JT
444
445 struct thin_c *tc;
446 dm_block_t virt_block;
447 dm_block_t data_block;
a24c2569 448 struct dm_bio_prison_cell *cell, *cell2;
991d9fa0
JT
449 int err;
450
451 /*
452 * If the bio covers the whole area of a block then we can avoid
453 * zeroing or copying. Instead this bio is hooked. The bio will
454 * still be in the cell, so care has to be taken to avoid issuing
455 * the bio twice.
456 */
457 struct bio *bio;
458 bio_end_io_t *saved_bi_end_io;
459};
460
a24c2569 461static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
991d9fa0
JT
462{
463 struct pool *pool = m->tc->pool;
464
eb2aa48d 465 if (m->quiesced && m->prepared) {
991d9fa0
JT
466 list_add(&m->list, &pool->prepared_mappings);
467 wake_worker(pool);
468 }
469}
470
471static void copy_complete(int read_err, unsigned long write_err, void *context)
472{
473 unsigned long flags;
a24c2569 474 struct dm_thin_new_mapping *m = context;
991d9fa0
JT
475 struct pool *pool = m->tc->pool;
476
477 m->err = read_err || write_err ? -EIO : 0;
478
479 spin_lock_irqsave(&pool->lock, flags);
480 m->prepared = 1;
481 __maybe_add_mapping(m);
482 spin_unlock_irqrestore(&pool->lock, flags);
483}
484
485static void overwrite_endio(struct bio *bio, int err)
486{
487 unsigned long flags;
a24c2569
MS
488 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
489 struct dm_thin_new_mapping *m = h->overwrite_mapping;
991d9fa0
JT
490 struct pool *pool = m->tc->pool;
491
492 m->err = err;
493
494 spin_lock_irqsave(&pool->lock, flags);
495 m->prepared = 1;
496 __maybe_add_mapping(m);
497 spin_unlock_irqrestore(&pool->lock, flags);
498}
499
991d9fa0
JT
500/*----------------------------------------------------------------*/
501
502/*
503 * Workqueue.
504 */
505
506/*
507 * Prepared mapping jobs.
508 */
509
510/*
511 * This sends the bios in the cell back to the deferred_bios list.
512 */
2aab3850 513static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell)
991d9fa0
JT
514{
515 struct pool *pool = tc->pool;
516 unsigned long flags;
517
518 spin_lock_irqsave(&pool->lock, flags);
44feb387 519 dm_cell_release(cell, &pool->deferred_bios);
991d9fa0
JT
520 spin_unlock_irqrestore(&tc->pool->lock, flags);
521
522 wake_worker(pool);
523}
524
525/*
b7ca9c92 526 * Same as cell_defer except it omits the original holder of the cell.
991d9fa0 527 */
f286ba0e 528static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
991d9fa0 529{
991d9fa0
JT
530 struct pool *pool = tc->pool;
531 unsigned long flags;
532
991d9fa0 533 spin_lock_irqsave(&pool->lock, flags);
44feb387 534 dm_cell_release_no_holder(cell, &pool->deferred_bios);
991d9fa0
JT
535 spin_unlock_irqrestore(&pool->lock, flags);
536
537 wake_worker(pool);
538}
539
e49e5829
JT
540static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
541{
542 if (m->bio)
543 m->bio->bi_end_io = m->saved_bi_end_io;
44feb387 544 dm_cell_error(m->cell);
e49e5829
JT
545 list_del(&m->list);
546 mempool_free(m, m->tc->pool->mapping_pool);
547}
a24c2569 548static void process_prepared_mapping(struct dm_thin_new_mapping *m)
991d9fa0
JT
549{
550 struct thin_c *tc = m->tc;
551 struct bio *bio;
552 int r;
553
554 bio = m->bio;
555 if (bio)
556 bio->bi_end_io = m->saved_bi_end_io;
557
558 if (m->err) {
44feb387 559 dm_cell_error(m->cell);
905386f8 560 goto out;
991d9fa0
JT
561 }
562
563 /*
564 * Commit the prepared block into the mapping btree.
565 * Any I/O for this block arriving after this point will get
566 * remapped to it directly.
567 */
568 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
569 if (r) {
570 DMERR("dm_thin_insert_block() failed");
44feb387 571 dm_cell_error(m->cell);
905386f8 572 goto out;
991d9fa0
JT
573 }
574
575 /*
576 * Release any bios held while the block was being provisioned.
577 * If we are processing a write bio that completely covers the block,
578 * we already processed it so can ignore it now when processing
579 * the bios in the cell.
580 */
581 if (bio) {
f286ba0e 582 cell_defer_no_holder(tc, m->cell);
991d9fa0
JT
583 bio_endio(bio, 0);
584 } else
2aab3850 585 cell_defer(tc, m->cell);
991d9fa0 586
905386f8 587out:
991d9fa0
JT
588 list_del(&m->list);
589 mempool_free(m, tc->pool->mapping_pool);
590}
591
e49e5829 592static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
104655fd 593{
104655fd
JT
594 struct thin_c *tc = m->tc;
595
e49e5829 596 bio_io_error(m->bio);
f286ba0e
JT
597 cell_defer_no_holder(tc, m->cell);
598 cell_defer_no_holder(tc, m->cell2);
e49e5829
JT
599 mempool_free(m, tc->pool->mapping_pool);
600}
601
602static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
603{
604 struct thin_c *tc = m->tc;
104655fd 605
e8088073 606 inc_all_io_entry(tc->pool, m->bio);
f286ba0e
JT
607 cell_defer_no_holder(tc, m->cell);
608 cell_defer_no_holder(tc, m->cell2);
e8088073 609
104655fd
JT
610 if (m->pass_discard)
611 remap_and_issue(tc, m->bio, m->data_block);
612 else
613 bio_endio(m->bio, 0);
614
104655fd
JT
615 mempool_free(m, tc->pool->mapping_pool);
616}
617
e49e5829
JT
618static void process_prepared_discard(struct dm_thin_new_mapping *m)
619{
620 int r;
621 struct thin_c *tc = m->tc;
622
623 r = dm_thin_remove_block(tc->td, m->virt_block);
624 if (r)
625 DMERR("dm_thin_remove_block() failed");
626
627 process_prepared_discard_passdown(m);
628}
629
104655fd 630static void process_prepared(struct pool *pool, struct list_head *head,
e49e5829 631 process_mapping_fn *fn)
991d9fa0
JT
632{
633 unsigned long flags;
634 struct list_head maps;
a24c2569 635 struct dm_thin_new_mapping *m, *tmp;
991d9fa0
JT
636
637 INIT_LIST_HEAD(&maps);
638 spin_lock_irqsave(&pool->lock, flags);
104655fd 639 list_splice_init(head, &maps);
991d9fa0
JT
640 spin_unlock_irqrestore(&pool->lock, flags);
641
642 list_for_each_entry_safe(m, tmp, &maps, list)
e49e5829 643 (*fn)(m);
991d9fa0
JT
644}
645
646/*
647 * Deferred bio jobs.
648 */
104655fd 649static int io_overlaps_block(struct pool *pool, struct bio *bio)
991d9fa0 650{
f9a8e0cd 651 return bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT);
104655fd
JT
652}
653
654static int io_overwrites_block(struct pool *pool, struct bio *bio)
655{
656 return (bio_data_dir(bio) == WRITE) &&
657 io_overlaps_block(pool, bio);
991d9fa0
JT
658}
659
660static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
661 bio_end_io_t *fn)
662{
663 *save = bio->bi_end_io;
664 bio->bi_end_io = fn;
665}
666
667static int ensure_next_mapping(struct pool *pool)
668{
669 if (pool->next_mapping)
670 return 0;
671
672 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
673
674 return pool->next_mapping ? 0 : -ENOMEM;
675}
676
a24c2569 677static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
991d9fa0 678{
a24c2569 679 struct dm_thin_new_mapping *r = pool->next_mapping;
991d9fa0
JT
680
681 BUG_ON(!pool->next_mapping);
682
683 pool->next_mapping = NULL;
684
685 return r;
686}
687
688static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
2dd9c257
JT
689 struct dm_dev *origin, dm_block_t data_origin,
690 dm_block_t data_dest,
a24c2569 691 struct dm_bio_prison_cell *cell, struct bio *bio)
991d9fa0
JT
692{
693 int r;
694 struct pool *pool = tc->pool;
a24c2569 695 struct dm_thin_new_mapping *m = get_next_mapping(pool);
991d9fa0
JT
696
697 INIT_LIST_HEAD(&m->list);
eb2aa48d 698 m->quiesced = 0;
991d9fa0
JT
699 m->prepared = 0;
700 m->tc = tc;
701 m->virt_block = virt_block;
702 m->data_block = data_dest;
703 m->cell = cell;
704 m->err = 0;
705 m->bio = NULL;
706
44feb387 707 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
eb2aa48d 708 m->quiesced = 1;
991d9fa0
JT
709
710 /*
711 * IO to pool_dev remaps to the pool target's data_dev.
712 *
713 * If the whole block of data is being overwritten, we can issue the
714 * bio immediately. Otherwise we use kcopyd to clone the data first.
715 */
716 if (io_overwrites_block(pool, bio)) {
a24c2569
MS
717 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
718
eb2aa48d 719 h->overwrite_mapping = m;
991d9fa0
JT
720 m->bio = bio;
721 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
e8088073 722 inc_all_io_entry(pool, bio);
991d9fa0
JT
723 remap_and_issue(tc, bio, data_dest);
724 } else {
725 struct dm_io_region from, to;
726
2dd9c257 727 from.bdev = origin->bdev;
991d9fa0
JT
728 from.sector = data_origin * pool->sectors_per_block;
729 from.count = pool->sectors_per_block;
730
731 to.bdev = tc->pool_dev->bdev;
732 to.sector = data_dest * pool->sectors_per_block;
733 to.count = pool->sectors_per_block;
734
735 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
736 0, copy_complete, m);
737 if (r < 0) {
738 mempool_free(m, pool->mapping_pool);
739 DMERR("dm_kcopyd_copy() failed");
44feb387 740 dm_cell_error(cell);
991d9fa0
JT
741 }
742 }
743}
744
2dd9c257
JT
745static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
746 dm_block_t data_origin, dm_block_t data_dest,
a24c2569 747 struct dm_bio_prison_cell *cell, struct bio *bio)
2dd9c257
JT
748{
749 schedule_copy(tc, virt_block, tc->pool_dev,
750 data_origin, data_dest, cell, bio);
751}
752
753static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
754 dm_block_t data_dest,
a24c2569 755 struct dm_bio_prison_cell *cell, struct bio *bio)
2dd9c257
JT
756{
757 schedule_copy(tc, virt_block, tc->origin_dev,
758 virt_block, data_dest, cell, bio);
759}
760
991d9fa0 761static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
a24c2569 762 dm_block_t data_block, struct dm_bio_prison_cell *cell,
991d9fa0
JT
763 struct bio *bio)
764{
765 struct pool *pool = tc->pool;
a24c2569 766 struct dm_thin_new_mapping *m = get_next_mapping(pool);
991d9fa0
JT
767
768 INIT_LIST_HEAD(&m->list);
eb2aa48d 769 m->quiesced = 1;
991d9fa0
JT
770 m->prepared = 0;
771 m->tc = tc;
772 m->virt_block = virt_block;
773 m->data_block = data_block;
774 m->cell = cell;
775 m->err = 0;
776 m->bio = NULL;
777
778 /*
779 * If the whole block of data is being overwritten or we are not
780 * zeroing pre-existing data, we can issue the bio immediately.
781 * Otherwise we use kcopyd to zero the data first.
782 */
67e2e2b2 783 if (!pool->pf.zero_new_blocks)
991d9fa0
JT
784 process_prepared_mapping(m);
785
786 else if (io_overwrites_block(pool, bio)) {
a24c2569
MS
787 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
788
eb2aa48d 789 h->overwrite_mapping = m;
991d9fa0
JT
790 m->bio = bio;
791 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
e8088073 792 inc_all_io_entry(pool, bio);
991d9fa0 793 remap_and_issue(tc, bio, data_block);
991d9fa0
JT
794 } else {
795 int r;
796 struct dm_io_region to;
797
798 to.bdev = tc->pool_dev->bdev;
799 to.sector = data_block * pool->sectors_per_block;
800 to.count = pool->sectors_per_block;
801
802 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
803 if (r < 0) {
804 mempool_free(m, pool->mapping_pool);
805 DMERR("dm_kcopyd_zero() failed");
44feb387 806 dm_cell_error(cell);
991d9fa0
JT
807 }
808 }
809}
810
e49e5829
JT
811static int commit(struct pool *pool)
812{
813 int r;
814
815 r = dm_pool_commit_metadata(pool->pmd);
816 if (r)
817 DMERR("commit failed, error = %d", r);
818
819 return r;
820}
821
822/*
823 * A non-zero return indicates read_only or fail_io mode.
824 * Many callers don't care about the return value.
825 */
826static int commit_or_fallback(struct pool *pool)
827{
828 int r;
829
830 if (get_pool_mode(pool) != PM_WRITE)
831 return -EINVAL;
832
833 r = commit(pool);
834 if (r)
835 set_pool_mode(pool, PM_READ_ONLY);
836
837 return r;
838}
839
991d9fa0
JT
840static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
841{
842 int r;
843 dm_block_t free_blocks;
844 unsigned long flags;
845 struct pool *pool = tc->pool;
846
847 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
848 if (r)
849 return r;
850
851 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
852 DMWARN("%s: reached low water mark, sending event.",
853 dm_device_name(pool->pool_md));
854 spin_lock_irqsave(&pool->lock, flags);
855 pool->low_water_triggered = 1;
856 spin_unlock_irqrestore(&pool->lock, flags);
857 dm_table_event(pool->ti->table);
858 }
859
860 if (!free_blocks) {
861 if (pool->no_free_space)
862 return -ENOSPC;
863 else {
864 /*
865 * Try to commit to see if that will free up some
866 * more space.
867 */
e49e5829 868 (void) commit_or_fallback(pool);
991d9fa0
JT
869
870 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
871 if (r)
872 return r;
873
874 /*
875 * If we still have no space we set a flag to avoid
876 * doing all this checking and return -ENOSPC.
877 */
878 if (!free_blocks) {
879 DMWARN("%s: no free space available.",
880 dm_device_name(pool->pool_md));
881 spin_lock_irqsave(&pool->lock, flags);
882 pool->no_free_space = 1;
883 spin_unlock_irqrestore(&pool->lock, flags);
884 return -ENOSPC;
885 }
886 }
887 }
888
889 r = dm_pool_alloc_data_block(pool->pmd, result);
890 if (r)
891 return r;
892
893 return 0;
894}
895
896/*
897 * If we have run out of space, queue bios until the device is
898 * resumed, presumably after having been reloaded with more space.
899 */
900static void retry_on_resume(struct bio *bio)
901{
a24c2569 902 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
eb2aa48d 903 struct thin_c *tc = h->tc;
991d9fa0
JT
904 struct pool *pool = tc->pool;
905 unsigned long flags;
906
907 spin_lock_irqsave(&pool->lock, flags);
908 bio_list_add(&pool->retry_on_resume_list, bio);
909 spin_unlock_irqrestore(&pool->lock, flags);
910}
911
a24c2569 912static void no_space(struct dm_bio_prison_cell *cell)
991d9fa0
JT
913{
914 struct bio *bio;
915 struct bio_list bios;
916
917 bio_list_init(&bios);
44feb387 918 dm_cell_release(cell, &bios);
991d9fa0
JT
919
920 while ((bio = bio_list_pop(&bios)))
921 retry_on_resume(bio);
922}
923
104655fd
JT
924static void process_discard(struct thin_c *tc, struct bio *bio)
925{
926 int r;
c3a0ce2e 927 unsigned long flags;
104655fd 928 struct pool *pool = tc->pool;
a24c2569 929 struct dm_bio_prison_cell *cell, *cell2;
44feb387 930 struct dm_cell_key key, key2;
104655fd
JT
931 dm_block_t block = get_bio_block(tc, bio);
932 struct dm_thin_lookup_result lookup_result;
a24c2569 933 struct dm_thin_new_mapping *m;
104655fd
JT
934
935 build_virtual_key(tc->td, block, &key);
44feb387 936 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
104655fd
JT
937 return;
938
939 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
940 switch (r) {
941 case 0:
942 /*
943 * Check nobody is fiddling with this pool block. This can
944 * happen if someone's in the process of breaking sharing
945 * on this block.
946 */
947 build_data_key(tc->td, lookup_result.block, &key2);
44feb387 948 if (dm_bio_detain(tc->pool->prison, &key2, bio, &cell2)) {
f286ba0e 949 cell_defer_no_holder(tc, cell);
104655fd
JT
950 break;
951 }
952
953 if (io_overlaps_block(pool, bio)) {
954 /*
955 * IO may still be going to the destination block. We must
956 * quiesce before we can do the removal.
957 */
958 m = get_next_mapping(pool);
959 m->tc = tc;
17b7d63f 960 m->pass_discard = (!lookup_result.shared) && pool->pf.discard_passdown;
104655fd
JT
961 m->virt_block = block;
962 m->data_block = lookup_result.block;
963 m->cell = cell;
964 m->cell2 = cell2;
965 m->err = 0;
966 m->bio = bio;
967
44feb387 968 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
c3a0ce2e 969 spin_lock_irqsave(&pool->lock, flags);
104655fd 970 list_add(&m->list, &pool->prepared_discards);
c3a0ce2e 971 spin_unlock_irqrestore(&pool->lock, flags);
104655fd
JT
972 wake_worker(pool);
973 }
974 } else {
e8088073 975 inc_all_io_entry(pool, bio);
f286ba0e
JT
976 cell_defer_no_holder(tc, cell);
977 cell_defer_no_holder(tc, cell2);
e8088073 978
104655fd 979 /*
49296309
MP
980 * The DM core makes sure that the discard doesn't span
981 * a block boundary. So we submit the discard of a
982 * partial block appropriately.
104655fd 983 */
650d2a06
MP
984 if ((!lookup_result.shared) && pool->pf.discard_passdown)
985 remap_and_issue(tc, bio, lookup_result.block);
986 else
987 bio_endio(bio, 0);
104655fd
JT
988 }
989 break;
990
991 case -ENODATA:
992 /*
993 * It isn't provisioned, just forget it.
994 */
f286ba0e 995 cell_defer_no_holder(tc, cell);
104655fd
JT
996 bio_endio(bio, 0);
997 break;
998
999 default:
1000 DMERR("discard: find block unexpectedly returned %d", r);
f286ba0e 1001 cell_defer_no_holder(tc, cell);
104655fd
JT
1002 bio_io_error(bio);
1003 break;
1004 }
1005}
1006
991d9fa0 1007static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
44feb387 1008 struct dm_cell_key *key,
991d9fa0 1009 struct dm_thin_lookup_result *lookup_result,
a24c2569 1010 struct dm_bio_prison_cell *cell)
991d9fa0
JT
1011{
1012 int r;
1013 dm_block_t data_block;
1014
1015 r = alloc_data_block(tc, &data_block);
1016 switch (r) {
1017 case 0:
2dd9c257
JT
1018 schedule_internal_copy(tc, block, lookup_result->block,
1019 data_block, cell, bio);
991d9fa0
JT
1020 break;
1021
1022 case -ENOSPC:
1023 no_space(cell);
1024 break;
1025
1026 default:
1027 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
44feb387 1028 dm_cell_error(cell);
991d9fa0
JT
1029 break;
1030 }
1031}
1032
1033static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1034 dm_block_t block,
1035 struct dm_thin_lookup_result *lookup_result)
1036{
a24c2569 1037 struct dm_bio_prison_cell *cell;
991d9fa0 1038 struct pool *pool = tc->pool;
44feb387 1039 struct dm_cell_key key;
991d9fa0
JT
1040
1041 /*
1042 * If cell is already occupied, then sharing is already in the process
1043 * of being broken so we have nothing further to do here.
1044 */
1045 build_data_key(tc->td, lookup_result->block, &key);
44feb387 1046 if (dm_bio_detain(pool->prison, &key, bio, &cell))
991d9fa0
JT
1047 return;
1048
60049701 1049 if (bio_data_dir(bio) == WRITE && bio->bi_size)
991d9fa0
JT
1050 break_sharing(tc, bio, block, &key, lookup_result, cell);
1051 else {
a24c2569 1052 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
991d9fa0 1053
44feb387 1054 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
e8088073 1055 inc_all_io_entry(pool, bio);
f286ba0e 1056 cell_defer_no_holder(tc, cell);
e8088073 1057
991d9fa0
JT
1058 remap_and_issue(tc, bio, lookup_result->block);
1059 }
1060}
1061
1062static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
a24c2569 1063 struct dm_bio_prison_cell *cell)
991d9fa0
JT
1064{
1065 int r;
1066 dm_block_t data_block;
1067
1068 /*
1069 * Remap empty bios (flushes) immediately, without provisioning.
1070 */
1071 if (!bio->bi_size) {
e8088073 1072 inc_all_io_entry(tc->pool, bio);
f286ba0e 1073 cell_defer_no_holder(tc, cell);
e8088073 1074
991d9fa0
JT
1075 remap_and_issue(tc, bio, 0);
1076 return;
1077 }
1078
1079 /*
1080 * Fill read bios with zeroes and complete them immediately.
1081 */
1082 if (bio_data_dir(bio) == READ) {
1083 zero_fill_bio(bio);
f286ba0e 1084 cell_defer_no_holder(tc, cell);
991d9fa0
JT
1085 bio_endio(bio, 0);
1086 return;
1087 }
1088
1089 r = alloc_data_block(tc, &data_block);
1090 switch (r) {
1091 case 0:
2dd9c257
JT
1092 if (tc->origin_dev)
1093 schedule_external_copy(tc, block, data_block, cell, bio);
1094 else
1095 schedule_zero(tc, block, data_block, cell, bio);
991d9fa0
JT
1096 break;
1097
1098 case -ENOSPC:
1099 no_space(cell);
1100 break;
1101
1102 default:
1103 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
e49e5829 1104 set_pool_mode(tc->pool, PM_READ_ONLY);
44feb387 1105 dm_cell_error(cell);
991d9fa0
JT
1106 break;
1107 }
1108}
1109
1110static void process_bio(struct thin_c *tc, struct bio *bio)
1111{
1112 int r;
1113 dm_block_t block = get_bio_block(tc, bio);
a24c2569 1114 struct dm_bio_prison_cell *cell;
44feb387 1115 struct dm_cell_key key;
991d9fa0
JT
1116 struct dm_thin_lookup_result lookup_result;
1117
1118 /*
1119 * If cell is already occupied, then the block is already
1120 * being provisioned so we have nothing further to do here.
1121 */
1122 build_virtual_key(tc->td, block, &key);
44feb387 1123 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
991d9fa0
JT
1124 return;
1125
1126 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1127 switch (r) {
1128 case 0:
e8088073 1129 if (lookup_result.shared) {
991d9fa0 1130 process_shared_bio(tc, bio, block, &lookup_result);
f286ba0e 1131 cell_defer_no_holder(tc, cell);
e8088073
JT
1132 } else {
1133 inc_all_io_entry(tc->pool, bio);
f286ba0e 1134 cell_defer_no_holder(tc, cell);
e8088073 1135
991d9fa0 1136 remap_and_issue(tc, bio, lookup_result.block);
e8088073 1137 }
991d9fa0
JT
1138 break;
1139
1140 case -ENODATA:
2dd9c257 1141 if (bio_data_dir(bio) == READ && tc->origin_dev) {
e8088073 1142 inc_all_io_entry(tc->pool, bio);
f286ba0e 1143 cell_defer_no_holder(tc, cell);
e8088073 1144
2dd9c257
JT
1145 remap_to_origin_and_issue(tc, bio);
1146 } else
1147 provision_block(tc, bio, block, cell);
991d9fa0
JT
1148 break;
1149
1150 default:
1151 DMERR("dm_thin_find_block() failed, error = %d", r);
f286ba0e 1152 cell_defer_no_holder(tc, cell);
991d9fa0
JT
1153 bio_io_error(bio);
1154 break;
1155 }
1156}
1157
e49e5829
JT
1158static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1159{
1160 int r;
1161 int rw = bio_data_dir(bio);
1162 dm_block_t block = get_bio_block(tc, bio);
1163 struct dm_thin_lookup_result lookup_result;
1164
1165 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1166 switch (r) {
1167 case 0:
1168 if (lookup_result.shared && (rw == WRITE) && bio->bi_size)
1169 bio_io_error(bio);
e8088073
JT
1170 else {
1171 inc_all_io_entry(tc->pool, bio);
e49e5829 1172 remap_and_issue(tc, bio, lookup_result.block);
e8088073 1173 }
e49e5829
JT
1174 break;
1175
1176 case -ENODATA:
1177 if (rw != READ) {
1178 bio_io_error(bio);
1179 break;
1180 }
1181
1182 if (tc->origin_dev) {
e8088073 1183 inc_all_io_entry(tc->pool, bio);
e49e5829
JT
1184 remap_to_origin_and_issue(tc, bio);
1185 break;
1186 }
1187
1188 zero_fill_bio(bio);
1189 bio_endio(bio, 0);
1190 break;
1191
1192 default:
1193 DMERR("dm_thin_find_block() failed, error = %d", r);
1194 bio_io_error(bio);
1195 break;
1196 }
1197}
1198
1199static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1200{
1201 bio_io_error(bio);
1202}
1203
905e51b3
JT
1204static int need_commit_due_to_time(struct pool *pool)
1205{
1206 return jiffies < pool->last_commit_jiffies ||
1207 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1208}
1209
991d9fa0
JT
1210static void process_deferred_bios(struct pool *pool)
1211{
1212 unsigned long flags;
1213 struct bio *bio;
1214 struct bio_list bios;
991d9fa0
JT
1215
1216 bio_list_init(&bios);
1217
1218 spin_lock_irqsave(&pool->lock, flags);
1219 bio_list_merge(&bios, &pool->deferred_bios);
1220 bio_list_init(&pool->deferred_bios);
1221 spin_unlock_irqrestore(&pool->lock, flags);
1222
1223 while ((bio = bio_list_pop(&bios))) {
a24c2569 1224 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
eb2aa48d
JT
1225 struct thin_c *tc = h->tc;
1226
991d9fa0
JT
1227 /*
1228 * If we've got no free new_mapping structs, and processing
1229 * this bio might require one, we pause until there are some
1230 * prepared mappings to process.
1231 */
1232 if (ensure_next_mapping(pool)) {
1233 spin_lock_irqsave(&pool->lock, flags);
1234 bio_list_merge(&pool->deferred_bios, &bios);
1235 spin_unlock_irqrestore(&pool->lock, flags);
1236
1237 break;
1238 }
104655fd
JT
1239
1240 if (bio->bi_rw & REQ_DISCARD)
e49e5829 1241 pool->process_discard(tc, bio);
104655fd 1242 else
e49e5829 1243 pool->process_bio(tc, bio);
991d9fa0
JT
1244 }
1245
1246 /*
1247 * If there are any deferred flush bios, we must commit
1248 * the metadata before issuing them.
1249 */
1250 bio_list_init(&bios);
1251 spin_lock_irqsave(&pool->lock, flags);
1252 bio_list_merge(&bios, &pool->deferred_flush_bios);
1253 bio_list_init(&pool->deferred_flush_bios);
1254 spin_unlock_irqrestore(&pool->lock, flags);
1255
905e51b3 1256 if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
991d9fa0
JT
1257 return;
1258
e49e5829 1259 if (commit_or_fallback(pool)) {
991d9fa0
JT
1260 while ((bio = bio_list_pop(&bios)))
1261 bio_io_error(bio);
1262 return;
1263 }
905e51b3 1264 pool->last_commit_jiffies = jiffies;
991d9fa0
JT
1265
1266 while ((bio = bio_list_pop(&bios)))
1267 generic_make_request(bio);
1268}
1269
1270static void do_worker(struct work_struct *ws)
1271{
1272 struct pool *pool = container_of(ws, struct pool, worker);
1273
e49e5829
JT
1274 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1275 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
991d9fa0
JT
1276 process_deferred_bios(pool);
1277}
1278
905e51b3
JT
1279/*
1280 * We want to commit periodically so that not too much
1281 * unwritten data builds up.
1282 */
1283static void do_waker(struct work_struct *ws)
1284{
1285 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1286 wake_worker(pool);
1287 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1288}
1289
991d9fa0
JT
1290/*----------------------------------------------------------------*/
1291
e49e5829
JT
1292static enum pool_mode get_pool_mode(struct pool *pool)
1293{
1294 return pool->pf.mode;
1295}
1296
1297static void set_pool_mode(struct pool *pool, enum pool_mode mode)
1298{
1299 int r;
1300
1301 pool->pf.mode = mode;
1302
1303 switch (mode) {
1304 case PM_FAIL:
1305 DMERR("switching pool to failure mode");
1306 pool->process_bio = process_bio_fail;
1307 pool->process_discard = process_bio_fail;
1308 pool->process_prepared_mapping = process_prepared_mapping_fail;
1309 pool->process_prepared_discard = process_prepared_discard_fail;
1310 break;
1311
1312 case PM_READ_ONLY:
1313 DMERR("switching pool to read-only mode");
1314 r = dm_pool_abort_metadata(pool->pmd);
1315 if (r) {
1316 DMERR("aborting transaction failed");
1317 set_pool_mode(pool, PM_FAIL);
1318 } else {
1319 dm_pool_metadata_read_only(pool->pmd);
1320 pool->process_bio = process_bio_read_only;
1321 pool->process_discard = process_discard;
1322 pool->process_prepared_mapping = process_prepared_mapping_fail;
1323 pool->process_prepared_discard = process_prepared_discard_passdown;
1324 }
1325 break;
1326
1327 case PM_WRITE:
1328 pool->process_bio = process_bio;
1329 pool->process_discard = process_discard;
1330 pool->process_prepared_mapping = process_prepared_mapping;
1331 pool->process_prepared_discard = process_prepared_discard;
1332 break;
1333 }
1334}
1335
1336/*----------------------------------------------------------------*/
1337
991d9fa0
JT
1338/*
1339 * Mapping functions.
1340 */
1341
1342/*
1343 * Called only while mapping a thin bio to hand it over to the workqueue.
1344 */
1345static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1346{
1347 unsigned long flags;
1348 struct pool *pool = tc->pool;
1349
1350 spin_lock_irqsave(&pool->lock, flags);
1351 bio_list_add(&pool->deferred_bios, bio);
1352 spin_unlock_irqrestore(&pool->lock, flags);
1353
1354 wake_worker(pool);
1355}
1356
a24c2569 1357static struct dm_thin_endio_hook *thin_hook_bio(struct thin_c *tc, struct bio *bio)
eb2aa48d
JT
1358{
1359 struct pool *pool = tc->pool;
a24c2569 1360 struct dm_thin_endio_hook *h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
eb2aa48d
JT
1361
1362 h->tc = tc;
1363 h->shared_read_entry = NULL;
e8088073 1364 h->all_io_entry = NULL;
eb2aa48d
JT
1365 h->overwrite_mapping = NULL;
1366
1367 return h;
1368}
1369
991d9fa0
JT
1370/*
1371 * Non-blocking function called from the thin target's map function.
1372 */
1373static int thin_bio_map(struct dm_target *ti, struct bio *bio,
1374 union map_info *map_context)
1375{
1376 int r;
1377 struct thin_c *tc = ti->private;
1378 dm_block_t block = get_bio_block(tc, bio);
1379 struct dm_thin_device *td = tc->td;
1380 struct dm_thin_lookup_result result;
e8088073
JT
1381 struct dm_bio_prison_cell *cell1, *cell2;
1382 struct dm_cell_key key;
991d9fa0 1383
eb2aa48d 1384 map_context->ptr = thin_hook_bio(tc, bio);
e49e5829
JT
1385
1386 if (get_pool_mode(tc->pool) == PM_FAIL) {
1387 bio_io_error(bio);
1388 return DM_MAPIO_SUBMITTED;
1389 }
1390
104655fd 1391 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
991d9fa0
JT
1392 thin_defer_bio(tc, bio);
1393 return DM_MAPIO_SUBMITTED;
1394 }
1395
1396 r = dm_thin_find_block(td, block, 0, &result);
1397
1398 /*
1399 * Note that we defer readahead too.
1400 */
1401 switch (r) {
1402 case 0:
1403 if (unlikely(result.shared)) {
1404 /*
1405 * We have a race condition here between the
1406 * result.shared value returned by the lookup and
1407 * snapshot creation, which may cause new
1408 * sharing.
1409 *
1410 * To avoid this always quiesce the origin before
1411 * taking the snap. You want to do this anyway to
1412 * ensure a consistent application view
1413 * (i.e. lockfs).
1414 *
1415 * More distant ancestors are irrelevant. The
1416 * shared flag will be set in their case.
1417 */
1418 thin_defer_bio(tc, bio);
e8088073 1419 return DM_MAPIO_SUBMITTED;
991d9fa0 1420 }
e8088073
JT
1421
1422 build_virtual_key(tc->td, block, &key);
1423 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1))
1424 return DM_MAPIO_SUBMITTED;
1425
1426 build_data_key(tc->td, result.block, &key);
1427 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2)) {
f286ba0e 1428 cell_defer_no_holder(tc, cell1);
e8088073
JT
1429 return DM_MAPIO_SUBMITTED;
1430 }
1431
1432 inc_all_io_entry(tc->pool, bio);
f286ba0e
JT
1433 cell_defer_no_holder(tc, cell2);
1434 cell_defer_no_holder(tc, cell1);
e8088073
JT
1435
1436 remap(tc, bio, result.block);
1437 return DM_MAPIO_REMAPPED;
991d9fa0
JT
1438
1439 case -ENODATA:
e49e5829
JT
1440 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
1441 /*
1442 * This block isn't provisioned, and we have no way
1443 * of doing so. Just error it.
1444 */
1445 bio_io_error(bio);
2aab3850 1446 return DM_MAPIO_SUBMITTED;
e49e5829
JT
1447 }
1448 /* fall through */
1449
1450 case -EWOULDBLOCK:
991d9fa0
JT
1451 /*
1452 * In future, the failed dm_thin_find_block above could
1453 * provide the hint to load the metadata into cache.
1454 */
991d9fa0 1455 thin_defer_bio(tc, bio);
2aab3850 1456 return DM_MAPIO_SUBMITTED;
e49e5829
JT
1457
1458 default:
1459 /*
1460 * Must always call bio_io_error on failure.
1461 * dm_thin_find_block can fail with -EINVAL if the
1462 * pool is switched to fail-io mode.
1463 */
1464 bio_io_error(bio);
2aab3850 1465 return DM_MAPIO_SUBMITTED;
991d9fa0 1466 }
991d9fa0
JT
1467}
1468
1469static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1470{
1471 int r;
1472 unsigned long flags;
1473 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
1474
1475 spin_lock_irqsave(&pt->pool->lock, flags);
1476 r = !bio_list_empty(&pt->pool->retry_on_resume_list);
1477 spin_unlock_irqrestore(&pt->pool->lock, flags);
1478
1479 if (!r) {
1480 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1481 r = bdi_congested(&q->backing_dev_info, bdi_bits);
1482 }
1483
1484 return r;
1485}
1486
1487static void __requeue_bios(struct pool *pool)
1488{
1489 bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
1490 bio_list_init(&pool->retry_on_resume_list);
1491}
1492
1493/*----------------------------------------------------------------
1494 * Binding of control targets to a pool object
1495 *--------------------------------------------------------------*/
9bc142dd
MS
1496static bool data_dev_supports_discard(struct pool_c *pt)
1497{
1498 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1499
1500 return q && blk_queue_discard(q);
1501}
1502
1503/*
1504 * If discard_passdown was enabled verify that the data device
0424caa1 1505 * supports discards. Disable discard_passdown if not.
9bc142dd 1506 */
0424caa1 1507static void disable_passdown_if_not_supported(struct pool_c *pt)
9bc142dd 1508{
0424caa1
MS
1509 struct pool *pool = pt->pool;
1510 struct block_device *data_bdev = pt->data_dev->bdev;
1511 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
1512 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
1513 const char *reason = NULL;
9bc142dd
MS
1514 char buf[BDEVNAME_SIZE];
1515
0424caa1 1516 if (!pt->adjusted_pf.discard_passdown)
9bc142dd
MS
1517 return;
1518
0424caa1
MS
1519 if (!data_dev_supports_discard(pt))
1520 reason = "discard unsupported";
1521
1522 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
1523 reason = "max discard sectors smaller than a block";
9bc142dd 1524
0424caa1
MS
1525 else if (data_limits->discard_granularity > block_size)
1526 reason = "discard granularity larger than a block";
1527
1528 else if (block_size & (data_limits->discard_granularity - 1))
1529 reason = "discard granularity not a factor of block size";
1530
1531 if (reason) {
1532 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
1533 pt->adjusted_pf.discard_passdown = false;
1534 }
9bc142dd
MS
1535}
1536
991d9fa0
JT
1537static int bind_control_target(struct pool *pool, struct dm_target *ti)
1538{
1539 struct pool_c *pt = ti->private;
1540
e49e5829
JT
1541 /*
1542 * We want to make sure that degraded pools are never upgraded.
1543 */
1544 enum pool_mode old_mode = pool->pf.mode;
0424caa1 1545 enum pool_mode new_mode = pt->adjusted_pf.mode;
e49e5829
JT
1546
1547 if (old_mode > new_mode)
1548 new_mode = old_mode;
1549
991d9fa0
JT
1550 pool->ti = ti;
1551 pool->low_water_blocks = pt->low_water_blocks;
0424caa1 1552 pool->pf = pt->adjusted_pf;
991d9fa0 1553
9bc142dd 1554 set_pool_mode(pool, new_mode);
f402693d 1555
991d9fa0
JT
1556 return 0;
1557}
1558
1559static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1560{
1561 if (pool->ti == ti)
1562 pool->ti = NULL;
1563}
1564
1565/*----------------------------------------------------------------
1566 * Pool creation
1567 *--------------------------------------------------------------*/
67e2e2b2
JT
1568/* Initialize pool features. */
1569static void pool_features_init(struct pool_features *pf)
1570{
e49e5829 1571 pf->mode = PM_WRITE;
9bc142dd
MS
1572 pf->zero_new_blocks = true;
1573 pf->discard_enabled = true;
1574 pf->discard_passdown = true;
67e2e2b2
JT
1575}
1576
991d9fa0
JT
1577static void __pool_destroy(struct pool *pool)
1578{
1579 __pool_table_remove(pool);
1580
1581 if (dm_pool_metadata_close(pool->pmd) < 0)
1582 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1583
44feb387 1584 dm_bio_prison_destroy(pool->prison);
991d9fa0
JT
1585 dm_kcopyd_client_destroy(pool->copier);
1586
1587 if (pool->wq)
1588 destroy_workqueue(pool->wq);
1589
1590 if (pool->next_mapping)
1591 mempool_free(pool->next_mapping, pool->mapping_pool);
1592 mempool_destroy(pool->mapping_pool);
1593 mempool_destroy(pool->endio_hook_pool);
44feb387
MS
1594 dm_deferred_set_destroy(pool->shared_read_ds);
1595 dm_deferred_set_destroy(pool->all_io_ds);
991d9fa0
JT
1596 kfree(pool);
1597}
1598
a24c2569
MS
1599static struct kmem_cache *_new_mapping_cache;
1600static struct kmem_cache *_endio_hook_cache;
1601
991d9fa0
JT
1602static struct pool *pool_create(struct mapped_device *pool_md,
1603 struct block_device *metadata_dev,
e49e5829
JT
1604 unsigned long block_size,
1605 int read_only, char **error)
991d9fa0
JT
1606{
1607 int r;
1608 void *err_p;
1609 struct pool *pool;
1610 struct dm_pool_metadata *pmd;
e49e5829 1611 bool format_device = read_only ? false : true;
991d9fa0 1612
e49e5829 1613 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
991d9fa0
JT
1614 if (IS_ERR(pmd)) {
1615 *error = "Error creating metadata object";
1616 return (struct pool *)pmd;
1617 }
1618
1619 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
1620 if (!pool) {
1621 *error = "Error allocating memory for pool";
1622 err_p = ERR_PTR(-ENOMEM);
1623 goto bad_pool;
1624 }
1625
1626 pool->pmd = pmd;
1627 pool->sectors_per_block = block_size;
f9a8e0cd
MP
1628 if (block_size & (block_size - 1))
1629 pool->sectors_per_block_shift = -1;
1630 else
1631 pool->sectors_per_block_shift = __ffs(block_size);
991d9fa0 1632 pool->low_water_blocks = 0;
67e2e2b2 1633 pool_features_init(&pool->pf);
44feb387 1634 pool->prison = dm_bio_prison_create(PRISON_CELLS);
991d9fa0
JT
1635 if (!pool->prison) {
1636 *error = "Error creating pool's bio prison";
1637 err_p = ERR_PTR(-ENOMEM);
1638 goto bad_prison;
1639 }
1640
1641 pool->copier = dm_kcopyd_client_create();
1642 if (IS_ERR(pool->copier)) {
1643 r = PTR_ERR(pool->copier);
1644 *error = "Error creating pool's kcopyd client";
1645 err_p = ERR_PTR(r);
1646 goto bad_kcopyd_client;
1647 }
1648
1649 /*
1650 * Create singlethreaded workqueue that will service all devices
1651 * that use this metadata.
1652 */
1653 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1654 if (!pool->wq) {
1655 *error = "Error creating pool's workqueue";
1656 err_p = ERR_PTR(-ENOMEM);
1657 goto bad_wq;
1658 }
1659
1660 INIT_WORK(&pool->worker, do_worker);
905e51b3 1661 INIT_DELAYED_WORK(&pool->waker, do_waker);
991d9fa0
JT
1662 spin_lock_init(&pool->lock);
1663 bio_list_init(&pool->deferred_bios);
1664 bio_list_init(&pool->deferred_flush_bios);
1665 INIT_LIST_HEAD(&pool->prepared_mappings);
104655fd 1666 INIT_LIST_HEAD(&pool->prepared_discards);
991d9fa0
JT
1667 pool->low_water_triggered = 0;
1668 pool->no_free_space = 0;
1669 bio_list_init(&pool->retry_on_resume_list);
44feb387
MS
1670
1671 pool->shared_read_ds = dm_deferred_set_create();
1672 if (!pool->shared_read_ds) {
1673 *error = "Error creating pool's shared read deferred set";
1674 err_p = ERR_PTR(-ENOMEM);
1675 goto bad_shared_read_ds;
1676 }
1677
1678 pool->all_io_ds = dm_deferred_set_create();
1679 if (!pool->all_io_ds) {
1680 *error = "Error creating pool's all io deferred set";
1681 err_p = ERR_PTR(-ENOMEM);
1682 goto bad_all_io_ds;
1683 }
991d9fa0
JT
1684
1685 pool->next_mapping = NULL;
a24c2569
MS
1686 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
1687 _new_mapping_cache);
991d9fa0
JT
1688 if (!pool->mapping_pool) {
1689 *error = "Error creating pool's mapping mempool";
1690 err_p = ERR_PTR(-ENOMEM);
1691 goto bad_mapping_pool;
1692 }
1693
a24c2569
MS
1694 pool->endio_hook_pool = mempool_create_slab_pool(ENDIO_HOOK_POOL_SIZE,
1695 _endio_hook_cache);
991d9fa0
JT
1696 if (!pool->endio_hook_pool) {
1697 *error = "Error creating pool's endio_hook mempool";
1698 err_p = ERR_PTR(-ENOMEM);
1699 goto bad_endio_hook_pool;
1700 }
1701 pool->ref_count = 1;
905e51b3 1702 pool->last_commit_jiffies = jiffies;
991d9fa0
JT
1703 pool->pool_md = pool_md;
1704 pool->md_dev = metadata_dev;
1705 __pool_table_insert(pool);
1706
1707 return pool;
1708
1709bad_endio_hook_pool:
1710 mempool_destroy(pool->mapping_pool);
1711bad_mapping_pool:
44feb387
MS
1712 dm_deferred_set_destroy(pool->all_io_ds);
1713bad_all_io_ds:
1714 dm_deferred_set_destroy(pool->shared_read_ds);
1715bad_shared_read_ds:
991d9fa0
JT
1716 destroy_workqueue(pool->wq);
1717bad_wq:
1718 dm_kcopyd_client_destroy(pool->copier);
1719bad_kcopyd_client:
44feb387 1720 dm_bio_prison_destroy(pool->prison);
991d9fa0
JT
1721bad_prison:
1722 kfree(pool);
1723bad_pool:
1724 if (dm_pool_metadata_close(pmd))
1725 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1726
1727 return err_p;
1728}
1729
1730static void __pool_inc(struct pool *pool)
1731{
1732 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1733 pool->ref_count++;
1734}
1735
1736static void __pool_dec(struct pool *pool)
1737{
1738 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1739 BUG_ON(!pool->ref_count);
1740 if (!--pool->ref_count)
1741 __pool_destroy(pool);
1742}
1743
1744static struct pool *__pool_find(struct mapped_device *pool_md,
1745 struct block_device *metadata_dev,
e49e5829
JT
1746 unsigned long block_size, int read_only,
1747 char **error, int *created)
991d9fa0
JT
1748{
1749 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
1750
1751 if (pool) {
f09996c9
MS
1752 if (pool->pool_md != pool_md) {
1753 *error = "metadata device already in use by a pool";
991d9fa0 1754 return ERR_PTR(-EBUSY);
f09996c9 1755 }
991d9fa0
JT
1756 __pool_inc(pool);
1757
1758 } else {
1759 pool = __pool_table_lookup(pool_md);
1760 if (pool) {
f09996c9
MS
1761 if (pool->md_dev != metadata_dev) {
1762 *error = "different pool cannot replace a pool";
991d9fa0 1763 return ERR_PTR(-EINVAL);
f09996c9 1764 }
991d9fa0
JT
1765 __pool_inc(pool);
1766
67e2e2b2 1767 } else {
e49e5829 1768 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
67e2e2b2
JT
1769 *created = 1;
1770 }
991d9fa0
JT
1771 }
1772
1773 return pool;
1774}
1775
1776/*----------------------------------------------------------------
1777 * Pool target methods
1778 *--------------------------------------------------------------*/
1779static void pool_dtr(struct dm_target *ti)
1780{
1781 struct pool_c *pt = ti->private;
1782
1783 mutex_lock(&dm_thin_pool_table.mutex);
1784
1785 unbind_control_target(pt->pool, ti);
1786 __pool_dec(pt->pool);
1787 dm_put_device(ti, pt->metadata_dev);
1788 dm_put_device(ti, pt->data_dev);
1789 kfree(pt);
1790
1791 mutex_unlock(&dm_thin_pool_table.mutex);
1792}
1793
991d9fa0
JT
1794static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
1795 struct dm_target *ti)
1796{
1797 int r;
1798 unsigned argc;
1799 const char *arg_name;
1800
1801 static struct dm_arg _args[] = {
67e2e2b2 1802 {0, 3, "Invalid number of pool feature arguments"},
991d9fa0
JT
1803 };
1804
1805 /*
1806 * No feature arguments supplied.
1807 */
1808 if (!as->argc)
1809 return 0;
1810
1811 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1812 if (r)
1813 return -EINVAL;
1814
1815 while (argc && !r) {
1816 arg_name = dm_shift_arg(as);
1817 argc--;
1818
e49e5829 1819 if (!strcasecmp(arg_name, "skip_block_zeroing"))
9bc142dd 1820 pf->zero_new_blocks = false;
e49e5829
JT
1821
1822 else if (!strcasecmp(arg_name, "ignore_discard"))
9bc142dd 1823 pf->discard_enabled = false;
e49e5829
JT
1824
1825 else if (!strcasecmp(arg_name, "no_discard_passdown"))
9bc142dd 1826 pf->discard_passdown = false;
991d9fa0 1827
e49e5829
JT
1828 else if (!strcasecmp(arg_name, "read_only"))
1829 pf->mode = PM_READ_ONLY;
1830
1831 else {
1832 ti->error = "Unrecognised pool feature requested";
1833 r = -EINVAL;
1834 break;
1835 }
991d9fa0
JT
1836 }
1837
1838 return r;
1839}
1840
1841/*
1842 * thin-pool <metadata dev> <data dev>
1843 * <data block size (sectors)>
1844 * <low water mark (blocks)>
1845 * [<#feature args> [<arg>]*]
1846 *
1847 * Optional feature arguments are:
1848 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
67e2e2b2
JT
1849 * ignore_discard: disable discard
1850 * no_discard_passdown: don't pass discards down to the data device
991d9fa0
JT
1851 */
1852static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
1853{
67e2e2b2 1854 int r, pool_created = 0;
991d9fa0
JT
1855 struct pool_c *pt;
1856 struct pool *pool;
1857 struct pool_features pf;
1858 struct dm_arg_set as;
1859 struct dm_dev *data_dev;
1860 unsigned long block_size;
1861 dm_block_t low_water_blocks;
1862 struct dm_dev *metadata_dev;
1863 sector_t metadata_dev_size;
c4a69ecd 1864 char b[BDEVNAME_SIZE];
991d9fa0
JT
1865
1866 /*
1867 * FIXME Remove validation from scope of lock.
1868 */
1869 mutex_lock(&dm_thin_pool_table.mutex);
1870
1871 if (argc < 4) {
1872 ti->error = "Invalid argument count";
1873 r = -EINVAL;
1874 goto out_unlock;
1875 }
1876 as.argc = argc;
1877 as.argv = argv;
1878
1879 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
1880 if (r) {
1881 ti->error = "Error opening metadata block device";
1882 goto out_unlock;
1883 }
1884
1885 metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
c4a69ecd
MS
1886 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
1887 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1888 bdevname(metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
991d9fa0
JT
1889
1890 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
1891 if (r) {
1892 ti->error = "Error getting data device";
1893 goto out_metadata;
1894 }
1895
1896 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
1897 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1898 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
55f2b8bd 1899 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
991d9fa0
JT
1900 ti->error = "Invalid block size";
1901 r = -EINVAL;
1902 goto out;
1903 }
1904
1905 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
1906 ti->error = "Invalid low water mark";
1907 r = -EINVAL;
1908 goto out;
1909 }
1910
1911 /*
1912 * Set default pool features.
1913 */
67e2e2b2 1914 pool_features_init(&pf);
991d9fa0
JT
1915
1916 dm_consume_args(&as, 4);
1917 r = parse_pool_features(&as, &pf, ti);
1918 if (r)
1919 goto out;
1920
1921 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
1922 if (!pt) {
1923 r = -ENOMEM;
1924 goto out;
1925 }
1926
1927 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
e49e5829 1928 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
991d9fa0
JT
1929 if (IS_ERR(pool)) {
1930 r = PTR_ERR(pool);
1931 goto out_free_pt;
1932 }
1933
67e2e2b2
JT
1934 /*
1935 * 'pool_created' reflects whether this is the first table load.
1936 * Top level discard support is not allowed to be changed after
1937 * initial load. This would require a pool reload to trigger thin
1938 * device changes.
1939 */
1940 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
1941 ti->error = "Discard support cannot be disabled once enabled";
1942 r = -EINVAL;
1943 goto out_flags_changed;
1944 }
1945
991d9fa0
JT
1946 pt->pool = pool;
1947 pt->ti = ti;
1948 pt->metadata_dev = metadata_dev;
1949 pt->data_dev = data_dev;
1950 pt->low_water_blocks = low_water_blocks;
0424caa1 1951 pt->adjusted_pf = pt->requested_pf = pf;
991d9fa0 1952 ti->num_flush_requests = 1;
9bc142dd 1953
67e2e2b2
JT
1954 /*
1955 * Only need to enable discards if the pool should pass
1956 * them down to the data device. The thin device's discard
1957 * processing will cause mappings to be removed from the btree.
1958 */
1959 if (pf.discard_enabled && pf.discard_passdown) {
1960 ti->num_discard_requests = 1;
9bc142dd 1961
67e2e2b2
JT
1962 /*
1963 * Setting 'discards_supported' circumvents the normal
1964 * stacking of discard limits (this keeps the pool and
1965 * thin devices' discard limits consistent).
1966 */
0ac55489 1967 ti->discards_supported = true;
307615a2 1968 ti->discard_zeroes_data_unsupported = true;
67e2e2b2 1969 }
991d9fa0
JT
1970 ti->private = pt;
1971
1972 pt->callbacks.congested_fn = pool_is_congested;
1973 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
1974
1975 mutex_unlock(&dm_thin_pool_table.mutex);
1976
1977 return 0;
1978
67e2e2b2
JT
1979out_flags_changed:
1980 __pool_dec(pool);
991d9fa0
JT
1981out_free_pt:
1982 kfree(pt);
1983out:
1984 dm_put_device(ti, data_dev);
1985out_metadata:
1986 dm_put_device(ti, metadata_dev);
1987out_unlock:
1988 mutex_unlock(&dm_thin_pool_table.mutex);
1989
1990 return r;
1991}
1992
1993static int pool_map(struct dm_target *ti, struct bio *bio,
1994 union map_info *map_context)
1995{
1996 int r;
1997 struct pool_c *pt = ti->private;
1998 struct pool *pool = pt->pool;
1999 unsigned long flags;
2000
2001 /*
2002 * As this is a singleton target, ti->begin is always zero.
2003 */
2004 spin_lock_irqsave(&pool->lock, flags);
2005 bio->bi_bdev = pt->data_dev->bdev;
2006 r = DM_MAPIO_REMAPPED;
2007 spin_unlock_irqrestore(&pool->lock, flags);
2008
2009 return r;
2010}
2011
2012/*
2013 * Retrieves the number of blocks of the data device from
2014 * the superblock and compares it to the actual device size,
2015 * thus resizing the data device in case it has grown.
2016 *
2017 * This both copes with opening preallocated data devices in the ctr
2018 * being followed by a resume
2019 * -and-
2020 * calling the resume method individually after userspace has
2021 * grown the data device in reaction to a table event.
2022 */
2023static int pool_preresume(struct dm_target *ti)
2024{
2025 int r;
2026 struct pool_c *pt = ti->private;
2027 struct pool *pool = pt->pool;
55f2b8bd
MS
2028 sector_t data_size = ti->len;
2029 dm_block_t sb_data_size;
991d9fa0
JT
2030
2031 /*
2032 * Take control of the pool object.
2033 */
2034 r = bind_control_target(pool, ti);
2035 if (r)
2036 return r;
2037
55f2b8bd
MS
2038 (void) sector_div(data_size, pool->sectors_per_block);
2039
991d9fa0
JT
2040 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2041 if (r) {
2042 DMERR("failed to retrieve data device size");
2043 return r;
2044 }
2045
2046 if (data_size < sb_data_size) {
2047 DMERR("pool target too small, is %llu blocks (expected %llu)",
55f2b8bd 2048 (unsigned long long)data_size, sb_data_size);
991d9fa0
JT
2049 return -EINVAL;
2050
2051 } else if (data_size > sb_data_size) {
2052 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2053 if (r) {
2054 DMERR("failed to resize data device");
e49e5829
JT
2055 /* FIXME Stricter than necessary: Rollback transaction instead here */
2056 set_pool_mode(pool, PM_READ_ONLY);
991d9fa0
JT
2057 return r;
2058 }
2059
e49e5829 2060 (void) commit_or_fallback(pool);
991d9fa0
JT
2061 }
2062
2063 return 0;
2064}
2065
2066static void pool_resume(struct dm_target *ti)
2067{
2068 struct pool_c *pt = ti->private;
2069 struct pool *pool = pt->pool;
2070 unsigned long flags;
2071
2072 spin_lock_irqsave(&pool->lock, flags);
2073 pool->low_water_triggered = 0;
2074 pool->no_free_space = 0;
2075 __requeue_bios(pool);
2076 spin_unlock_irqrestore(&pool->lock, flags);
2077
905e51b3 2078 do_waker(&pool->waker.work);
991d9fa0
JT
2079}
2080
2081static void pool_postsuspend(struct dm_target *ti)
2082{
991d9fa0
JT
2083 struct pool_c *pt = ti->private;
2084 struct pool *pool = pt->pool;
2085
905e51b3 2086 cancel_delayed_work(&pool->waker);
991d9fa0 2087 flush_workqueue(pool->wq);
e49e5829 2088 (void) commit_or_fallback(pool);
991d9fa0
JT
2089}
2090
2091static int check_arg_count(unsigned argc, unsigned args_required)
2092{
2093 if (argc != args_required) {
2094 DMWARN("Message received with %u arguments instead of %u.",
2095 argc, args_required);
2096 return -EINVAL;
2097 }
2098
2099 return 0;
2100}
2101
2102static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2103{
2104 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2105 *dev_id <= MAX_DEV_ID)
2106 return 0;
2107
2108 if (warning)
2109 DMWARN("Message received with invalid device id: %s", arg);
2110
2111 return -EINVAL;
2112}
2113
2114static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2115{
2116 dm_thin_id dev_id;
2117 int r;
2118
2119 r = check_arg_count(argc, 2);
2120 if (r)
2121 return r;
2122
2123 r = read_dev_id(argv[1], &dev_id, 1);
2124 if (r)
2125 return r;
2126
2127 r = dm_pool_create_thin(pool->pmd, dev_id);
2128 if (r) {
2129 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2130 argv[1]);
2131 return r;
2132 }
2133
2134 return 0;
2135}
2136
2137static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2138{
2139 dm_thin_id dev_id;
2140 dm_thin_id origin_dev_id;
2141 int r;
2142
2143 r = check_arg_count(argc, 3);
2144 if (r)
2145 return r;
2146
2147 r = read_dev_id(argv[1], &dev_id, 1);
2148 if (r)
2149 return r;
2150
2151 r = read_dev_id(argv[2], &origin_dev_id, 1);
2152 if (r)
2153 return r;
2154
2155 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2156 if (r) {
2157 DMWARN("Creation of new snapshot %s of device %s failed.",
2158 argv[1], argv[2]);
2159 return r;
2160 }
2161
2162 return 0;
2163}
2164
2165static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2166{
2167 dm_thin_id dev_id;
2168 int r;
2169
2170 r = check_arg_count(argc, 2);
2171 if (r)
2172 return r;
2173
2174 r = read_dev_id(argv[1], &dev_id, 1);
2175 if (r)
2176 return r;
2177
2178 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2179 if (r)
2180 DMWARN("Deletion of thin device %s failed.", argv[1]);
2181
2182 return r;
2183}
2184
2185static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2186{
2187 dm_thin_id old_id, new_id;
2188 int r;
2189
2190 r = check_arg_count(argc, 3);
2191 if (r)
2192 return r;
2193
2194 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2195 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2196 return -EINVAL;
2197 }
2198
2199 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2200 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2201 return -EINVAL;
2202 }
2203
2204 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2205 if (r) {
2206 DMWARN("Failed to change transaction id from %s to %s.",
2207 argv[1], argv[2]);
2208 return r;
2209 }
2210
2211 return 0;
2212}
2213
cc8394d8
JT
2214static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2215{
2216 int r;
2217
2218 r = check_arg_count(argc, 1);
2219 if (r)
2220 return r;
2221
e49e5829 2222 (void) commit_or_fallback(pool);
0d200aef 2223
cc8394d8
JT
2224 r = dm_pool_reserve_metadata_snap(pool->pmd);
2225 if (r)
2226 DMWARN("reserve_metadata_snap message failed.");
2227
2228 return r;
2229}
2230
2231static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2232{
2233 int r;
2234
2235 r = check_arg_count(argc, 1);
2236 if (r)
2237 return r;
2238
2239 r = dm_pool_release_metadata_snap(pool->pmd);
2240 if (r)
2241 DMWARN("release_metadata_snap message failed.");
2242
2243 return r;
2244}
2245
991d9fa0
JT
2246/*
2247 * Messages supported:
2248 * create_thin <dev_id>
2249 * create_snap <dev_id> <origin_id>
2250 * delete <dev_id>
2251 * trim <dev_id> <new_size_in_sectors>
2252 * set_transaction_id <current_trans_id> <new_trans_id>
cc8394d8
JT
2253 * reserve_metadata_snap
2254 * release_metadata_snap
991d9fa0
JT
2255 */
2256static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2257{
2258 int r = -EINVAL;
2259 struct pool_c *pt = ti->private;
2260 struct pool *pool = pt->pool;
2261
2262 if (!strcasecmp(argv[0], "create_thin"))
2263 r = process_create_thin_mesg(argc, argv, pool);
2264
2265 else if (!strcasecmp(argv[0], "create_snap"))
2266 r = process_create_snap_mesg(argc, argv, pool);
2267
2268 else if (!strcasecmp(argv[0], "delete"))
2269 r = process_delete_mesg(argc, argv, pool);
2270
2271 else if (!strcasecmp(argv[0], "set_transaction_id"))
2272 r = process_set_transaction_id_mesg(argc, argv, pool);
2273
cc8394d8
JT
2274 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
2275 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
2276
2277 else if (!strcasecmp(argv[0], "release_metadata_snap"))
2278 r = process_release_metadata_snap_mesg(argc, argv, pool);
2279
991d9fa0
JT
2280 else
2281 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2282
e49e5829
JT
2283 if (!r)
2284 (void) commit_or_fallback(pool);
991d9fa0
JT
2285
2286 return r;
2287}
2288
e49e5829
JT
2289static void emit_flags(struct pool_features *pf, char *result,
2290 unsigned sz, unsigned maxlen)
2291{
2292 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
2293 !pf->discard_passdown + (pf->mode == PM_READ_ONLY);
2294 DMEMIT("%u ", count);
2295
2296 if (!pf->zero_new_blocks)
2297 DMEMIT("skip_block_zeroing ");
2298
2299 if (!pf->discard_enabled)
2300 DMEMIT("ignore_discard ");
2301
2302 if (!pf->discard_passdown)
2303 DMEMIT("no_discard_passdown ");
2304
2305 if (pf->mode == PM_READ_ONLY)
2306 DMEMIT("read_only ");
2307}
2308
991d9fa0
JT
2309/*
2310 * Status line is:
2311 * <transaction id> <used metadata sectors>/<total metadata sectors>
2312 * <used data sectors>/<total data sectors> <held metadata root>
2313 */
2314static int pool_status(struct dm_target *ti, status_type_t type,
1f4e0ff0 2315 unsigned status_flags, char *result, unsigned maxlen)
991d9fa0 2316{
e49e5829 2317 int r;
991d9fa0
JT
2318 unsigned sz = 0;
2319 uint64_t transaction_id;
2320 dm_block_t nr_free_blocks_data;
2321 dm_block_t nr_free_blocks_metadata;
2322 dm_block_t nr_blocks_data;
2323 dm_block_t nr_blocks_metadata;
2324 dm_block_t held_root;
2325 char buf[BDEVNAME_SIZE];
2326 char buf2[BDEVNAME_SIZE];
2327 struct pool_c *pt = ti->private;
2328 struct pool *pool = pt->pool;
2329
2330 switch (type) {
2331 case STATUSTYPE_INFO:
e49e5829
JT
2332 if (get_pool_mode(pool) == PM_FAIL) {
2333 DMEMIT("Fail");
2334 break;
2335 }
2336
1f4e0ff0
AK
2337 /* Commit to ensure statistics aren't out-of-date */
2338 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
2339 (void) commit_or_fallback(pool);
2340
991d9fa0
JT
2341 r = dm_pool_get_metadata_transaction_id(pool->pmd,
2342 &transaction_id);
2343 if (r)
2344 return r;
2345
2346 r = dm_pool_get_free_metadata_block_count(pool->pmd,
2347 &nr_free_blocks_metadata);
2348 if (r)
2349 return r;
2350
2351 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
2352 if (r)
2353 return r;
2354
2355 r = dm_pool_get_free_block_count(pool->pmd,
2356 &nr_free_blocks_data);
2357 if (r)
2358 return r;
2359
2360 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
2361 if (r)
2362 return r;
2363
cc8394d8 2364 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
991d9fa0
JT
2365 if (r)
2366 return r;
2367
2368 DMEMIT("%llu %llu/%llu %llu/%llu ",
2369 (unsigned long long)transaction_id,
2370 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2371 (unsigned long long)nr_blocks_metadata,
2372 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
2373 (unsigned long long)nr_blocks_data);
2374
2375 if (held_root)
e49e5829
JT
2376 DMEMIT("%llu ", held_root);
2377 else
2378 DMEMIT("- ");
2379
2380 if (pool->pf.mode == PM_READ_ONLY)
2381 DMEMIT("ro ");
991d9fa0 2382 else
e49e5829
JT
2383 DMEMIT("rw ");
2384
018debea
MS
2385 if (!pool->pf.discard_enabled)
2386 DMEMIT("ignore_discard");
2387 else if (pool->pf.discard_passdown)
e49e5829
JT
2388 DMEMIT("discard_passdown");
2389 else
2390 DMEMIT("no_discard_passdown");
991d9fa0
JT
2391
2392 break;
2393
2394 case STATUSTYPE_TABLE:
2395 DMEMIT("%s %s %lu %llu ",
2396 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
2397 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
2398 (unsigned long)pool->sectors_per_block,
2399 (unsigned long long)pt->low_water_blocks);
0424caa1 2400 emit_flags(&pt->requested_pf, result, sz, maxlen);
991d9fa0
JT
2401 break;
2402 }
2403
2404 return 0;
2405}
2406
2407static int pool_iterate_devices(struct dm_target *ti,
2408 iterate_devices_callout_fn fn, void *data)
2409{
2410 struct pool_c *pt = ti->private;
2411
2412 return fn(ti, pt->data_dev, 0, ti->len, data);
2413}
2414
2415static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2416 struct bio_vec *biovec, int max_size)
2417{
2418 struct pool_c *pt = ti->private;
2419 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2420
2421 if (!q->merge_bvec_fn)
2422 return max_size;
2423
2424 bvm->bi_bdev = pt->data_dev->bdev;
2425
2426 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2427}
2428
28eed34e
MS
2429static bool block_size_is_power_of_two(struct pool *pool)
2430{
2431 return pool->sectors_per_block_shift >= 0;
2432}
2433
0424caa1 2434static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
104655fd 2435{
0424caa1
MS
2436 struct pool *pool = pt->pool;
2437 struct queue_limits *data_limits;
2438
104655fd
JT
2439 limits->max_discard_sectors = pool->sectors_per_block;
2440
2441 /*
0424caa1 2442 * discard_granularity is just a hint, and not enforced.
104655fd 2443 */
0424caa1
MS
2444 if (pt->adjusted_pf.discard_passdown) {
2445 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
2446 limits->discard_granularity = data_limits->discard_granularity;
28eed34e 2447 } else if (block_size_is_power_of_two(pool))
0424caa1 2448 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
28eed34e
MS
2449 else
2450 /*
2451 * Use largest power of 2 that is a factor of sectors_per_block
2452 * but at least DATA_DEV_BLOCK_SIZE_MIN_SECTORS.
2453 */
2454 limits->discard_granularity = max(1 << (ffs(pool->sectors_per_block) - 1),
2455 DATA_DEV_BLOCK_SIZE_MIN_SECTORS) << SECTOR_SHIFT;
104655fd
JT
2456}
2457
991d9fa0
JT
2458static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
2459{
2460 struct pool_c *pt = ti->private;
2461 struct pool *pool = pt->pool;
2462
2463 blk_limits_io_min(limits, 0);
2464 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
0424caa1
MS
2465
2466 /*
2467 * pt->adjusted_pf is a staging area for the actual features to use.
2468 * They get transferred to the live pool in bind_control_target()
2469 * called from pool_preresume().
2470 */
2471 if (!pt->adjusted_pf.discard_enabled)
2472 return;
2473
2474 disable_passdown_if_not_supported(pt);
2475
2476 set_discard_limits(pt, limits);
991d9fa0
JT
2477}
2478
2479static struct target_type pool_target = {
2480 .name = "thin-pool",
2481 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
2482 DM_TARGET_IMMUTABLE,
018debea 2483 .version = {1, 6, 0},
991d9fa0
JT
2484 .module = THIS_MODULE,
2485 .ctr = pool_ctr,
2486 .dtr = pool_dtr,
2487 .map = pool_map,
2488 .postsuspend = pool_postsuspend,
2489 .preresume = pool_preresume,
2490 .resume = pool_resume,
2491 .message = pool_message,
2492 .status = pool_status,
2493 .merge = pool_merge,
2494 .iterate_devices = pool_iterate_devices,
2495 .io_hints = pool_io_hints,
2496};
2497
2498/*----------------------------------------------------------------
2499 * Thin target methods
2500 *--------------------------------------------------------------*/
2501static void thin_dtr(struct dm_target *ti)
2502{
2503 struct thin_c *tc = ti->private;
2504
2505 mutex_lock(&dm_thin_pool_table.mutex);
2506
2507 __pool_dec(tc->pool);
2508 dm_pool_close_thin_device(tc->td);
2509 dm_put_device(ti, tc->pool_dev);
2dd9c257
JT
2510 if (tc->origin_dev)
2511 dm_put_device(ti, tc->origin_dev);
991d9fa0
JT
2512 kfree(tc);
2513
2514 mutex_unlock(&dm_thin_pool_table.mutex);
2515}
2516
2517/*
2518 * Thin target parameters:
2519 *
2dd9c257 2520 * <pool_dev> <dev_id> [origin_dev]
991d9fa0
JT
2521 *
2522 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2523 * dev_id: the internal device identifier
2dd9c257 2524 * origin_dev: a device external to the pool that should act as the origin
67e2e2b2
JT
2525 *
2526 * If the pool device has discards disabled, they get disabled for the thin
2527 * device as well.
991d9fa0
JT
2528 */
2529static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
2530{
2531 int r;
2532 struct thin_c *tc;
2dd9c257 2533 struct dm_dev *pool_dev, *origin_dev;
991d9fa0
JT
2534 struct mapped_device *pool_md;
2535
2536 mutex_lock(&dm_thin_pool_table.mutex);
2537
2dd9c257 2538 if (argc != 2 && argc != 3) {
991d9fa0
JT
2539 ti->error = "Invalid argument count";
2540 r = -EINVAL;
2541 goto out_unlock;
2542 }
2543
2544 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
2545 if (!tc) {
2546 ti->error = "Out of memory";
2547 r = -ENOMEM;
2548 goto out_unlock;
2549 }
2550
2dd9c257
JT
2551 if (argc == 3) {
2552 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
2553 if (r) {
2554 ti->error = "Error opening origin device";
2555 goto bad_origin_dev;
2556 }
2557 tc->origin_dev = origin_dev;
2558 }
2559
991d9fa0
JT
2560 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
2561 if (r) {
2562 ti->error = "Error opening pool device";
2563 goto bad_pool_dev;
2564 }
2565 tc->pool_dev = pool_dev;
2566
2567 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
2568 ti->error = "Invalid device id";
2569 r = -EINVAL;
2570 goto bad_common;
2571 }
2572
2573 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
2574 if (!pool_md) {
2575 ti->error = "Couldn't get pool mapped device";
2576 r = -EINVAL;
2577 goto bad_common;
2578 }
2579
2580 tc->pool = __pool_table_lookup(pool_md);
2581 if (!tc->pool) {
2582 ti->error = "Couldn't find pool object";
2583 r = -EINVAL;
2584 goto bad_pool_lookup;
2585 }
2586 __pool_inc(tc->pool);
2587
e49e5829
JT
2588 if (get_pool_mode(tc->pool) == PM_FAIL) {
2589 ti->error = "Couldn't open thin device, Pool is in fail mode";
2590 goto bad_thin_open;
2591 }
2592
991d9fa0
JT
2593 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
2594 if (r) {
2595 ti->error = "Couldn't open thin internal device";
2596 goto bad_thin_open;
2597 }
2598
542f9038
MS
2599 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
2600 if (r)
2601 goto bad_thin_open;
2602
991d9fa0 2603 ti->num_flush_requests = 1;
16ad3d10 2604 ti->flush_supported = true;
67e2e2b2
JT
2605
2606 /* In case the pool supports discards, pass them on. */
2607 if (tc->pool->pf.discard_enabled) {
0ac55489 2608 ti->discards_supported = true;
67e2e2b2 2609 ti->num_discard_requests = 1;
0ac55489 2610 ti->discard_zeroes_data_unsupported = true;
49296309 2611 /* Discard requests must be split on a block boundary */
0ac55489 2612 ti->split_discard_requests = true;
67e2e2b2 2613 }
991d9fa0
JT
2614
2615 dm_put(pool_md);
2616
2617 mutex_unlock(&dm_thin_pool_table.mutex);
2618
2619 return 0;
2620
2621bad_thin_open:
2622 __pool_dec(tc->pool);
2623bad_pool_lookup:
2624 dm_put(pool_md);
2625bad_common:
2626 dm_put_device(ti, tc->pool_dev);
2627bad_pool_dev:
2dd9c257
JT
2628 if (tc->origin_dev)
2629 dm_put_device(ti, tc->origin_dev);
2630bad_origin_dev:
991d9fa0
JT
2631 kfree(tc);
2632out_unlock:
2633 mutex_unlock(&dm_thin_pool_table.mutex);
2634
2635 return r;
2636}
2637
2638static int thin_map(struct dm_target *ti, struct bio *bio,
2639 union map_info *map_context)
2640{
6efd6e83 2641 bio->bi_sector = dm_target_offset(ti, bio->bi_sector);
991d9fa0
JT
2642
2643 return thin_bio_map(ti, bio, map_context);
2644}
2645
eb2aa48d
JT
2646static int thin_endio(struct dm_target *ti,
2647 struct bio *bio, int err,
2648 union map_info *map_context)
2649{
2650 unsigned long flags;
a24c2569 2651 struct dm_thin_endio_hook *h = map_context->ptr;
eb2aa48d 2652 struct list_head work;
a24c2569 2653 struct dm_thin_new_mapping *m, *tmp;
eb2aa48d
JT
2654 struct pool *pool = h->tc->pool;
2655
2656 if (h->shared_read_entry) {
2657 INIT_LIST_HEAD(&work);
44feb387 2658 dm_deferred_entry_dec(h->shared_read_entry, &work);
eb2aa48d
JT
2659
2660 spin_lock_irqsave(&pool->lock, flags);
2661 list_for_each_entry_safe(m, tmp, &work, list) {
2662 list_del(&m->list);
2663 m->quiesced = 1;
2664 __maybe_add_mapping(m);
2665 }
2666 spin_unlock_irqrestore(&pool->lock, flags);
2667 }
2668
104655fd
JT
2669 if (h->all_io_entry) {
2670 INIT_LIST_HEAD(&work);
44feb387 2671 dm_deferred_entry_dec(h->all_io_entry, &work);
563af186
JT
2672 if (!list_empty(&work)) {
2673 spin_lock_irqsave(&pool->lock, flags);
2674 list_for_each_entry_safe(m, tmp, &work, list)
2675 list_add(&m->list, &pool->prepared_discards);
2676 spin_unlock_irqrestore(&pool->lock, flags);
2677 wake_worker(pool);
2678 }
104655fd
JT
2679 }
2680
eb2aa48d
JT
2681 mempool_free(h, pool->endio_hook_pool);
2682
2683 return 0;
2684}
2685
991d9fa0
JT
2686static void thin_postsuspend(struct dm_target *ti)
2687{
2688 if (dm_noflush_suspending(ti))
2689 requeue_io((struct thin_c *)ti->private);
2690}
2691
2692/*
2693 * <nr mapped sectors> <highest mapped sector>
2694 */
2695static int thin_status(struct dm_target *ti, status_type_t type,
1f4e0ff0 2696 unsigned status_flags, char *result, unsigned maxlen)
991d9fa0
JT
2697{
2698 int r;
2699 ssize_t sz = 0;
2700 dm_block_t mapped, highest;
2701 char buf[BDEVNAME_SIZE];
2702 struct thin_c *tc = ti->private;
2703
e49e5829
JT
2704 if (get_pool_mode(tc->pool) == PM_FAIL) {
2705 DMEMIT("Fail");
2706 return 0;
2707 }
2708
991d9fa0
JT
2709 if (!tc->td)
2710 DMEMIT("-");
2711 else {
2712 switch (type) {
2713 case STATUSTYPE_INFO:
2714 r = dm_thin_get_mapped_count(tc->td, &mapped);
2715 if (r)
2716 return r;
2717
2718 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
2719 if (r < 0)
2720 return r;
2721
2722 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
2723 if (r)
2724 DMEMIT("%llu", ((highest + 1) *
2725 tc->pool->sectors_per_block) - 1);
2726 else
2727 DMEMIT("-");
2728 break;
2729
2730 case STATUSTYPE_TABLE:
2731 DMEMIT("%s %lu",
2732 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
2733 (unsigned long) tc->dev_id);
2dd9c257
JT
2734 if (tc->origin_dev)
2735 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
991d9fa0
JT
2736 break;
2737 }
2738 }
2739
2740 return 0;
2741}
2742
2743static int thin_iterate_devices(struct dm_target *ti,
2744 iterate_devices_callout_fn fn, void *data)
2745{
55f2b8bd 2746 sector_t blocks;
991d9fa0 2747 struct thin_c *tc = ti->private;
55f2b8bd 2748 struct pool *pool = tc->pool;
991d9fa0
JT
2749
2750 /*
2751 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2752 * we follow a more convoluted path through to the pool's target.
2753 */
55f2b8bd 2754 if (!pool->ti)
991d9fa0
JT
2755 return 0; /* nothing is bound */
2756
55f2b8bd
MS
2757 blocks = pool->ti->len;
2758 (void) sector_div(blocks, pool->sectors_per_block);
991d9fa0 2759 if (blocks)
55f2b8bd 2760 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
991d9fa0
JT
2761
2762 return 0;
2763}
2764
0424caa1
MS
2765/*
2766 * A thin device always inherits its queue limits from its pool.
2767 */
991d9fa0
JT
2768static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
2769{
2770 struct thin_c *tc = ti->private;
2771
0424caa1 2772 *limits = bdev_get_queue(tc->pool_dev->bdev)->limits;
991d9fa0
JT
2773}
2774
2775static struct target_type thin_target = {
2776 .name = "thin",
4f81a417 2777 .version = {1, 5, 0},
991d9fa0
JT
2778 .module = THIS_MODULE,
2779 .ctr = thin_ctr,
2780 .dtr = thin_dtr,
2781 .map = thin_map,
eb2aa48d 2782 .end_io = thin_endio,
991d9fa0
JT
2783 .postsuspend = thin_postsuspend,
2784 .status = thin_status,
2785 .iterate_devices = thin_iterate_devices,
2786 .io_hints = thin_io_hints,
2787};
2788
2789/*----------------------------------------------------------------*/
2790
2791static int __init dm_thin_init(void)
2792{
2793 int r;
2794
2795 pool_table_init();
2796
2797 r = dm_register_target(&thin_target);
2798 if (r)
2799 return r;
2800
2801 r = dm_register_target(&pool_target);
2802 if (r)
a24c2569
MS
2803 goto bad_pool_target;
2804
2805 r = -ENOMEM;
2806
a24c2569
MS
2807 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
2808 if (!_new_mapping_cache)
2809 goto bad_new_mapping_cache;
2810
2811 _endio_hook_cache = KMEM_CACHE(dm_thin_endio_hook, 0);
2812 if (!_endio_hook_cache)
2813 goto bad_endio_hook_cache;
2814
2815 return 0;
2816
2817bad_endio_hook_cache:
2818 kmem_cache_destroy(_new_mapping_cache);
2819bad_new_mapping_cache:
a24c2569
MS
2820 dm_unregister_target(&pool_target);
2821bad_pool_target:
2822 dm_unregister_target(&thin_target);
991d9fa0
JT
2823
2824 return r;
2825}
2826
2827static void dm_thin_exit(void)
2828{
2829 dm_unregister_target(&thin_target);
2830 dm_unregister_target(&pool_target);
a24c2569 2831
a24c2569
MS
2832 kmem_cache_destroy(_new_mapping_cache);
2833 kmem_cache_destroy(_endio_hook_cache);
991d9fa0
JT
2834}
2835
2836module_init(dm_thin_init);
2837module_exit(dm_thin_exit);
2838
7cab8bf1 2839MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
991d9fa0
JT
2840MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2841MODULE_LICENSE("GPL");