2 * Copyright (C) 2011-2012 Red Hat UK.
4 * This file is released under the GPL.
7 #include "dm-thin-metadata.h"
8 #include "dm-bio-prison.h"
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>
19 #define DM_MSG_PREFIX "thin"
24 #define ENDIO_HOOK_POOL_SIZE 1024
25 #define MAPPING_POOL_SIZE 1024
26 #define PRISON_CELLS 1024
27 #define COMMIT_PERIOD HZ
30 * The block size of the device holding pool data must be
31 * between 64KB and 1GB.
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)
37 * Device id is restricted to 24 bits.
39 #define MAX_DEV_ID ((1 << 24) - 1)
42 * How do we handle breaking sharing of data blocks?
43 * =================================================
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
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.
55 * Let's say we write to a shared block in what was the origin. The
58 * i) plug io further to this physical block. (see bio_prison code).
60 * ii) quiesce any read io to that shared data block. Obviously
61 * including all devices that share this block. (see dm_deferred_set code)
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).
66 * iv) insert the new mapping into the origin's btree
67 * (process_prepared_mapping). This act of inserting breaks some
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.
74 * v) unplug io to this physical block, including the io that triggered
75 * the breaking of sharing.
77 * Steps (ii) and (iii) occur in parallel.
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:
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.
87 * - The snap mapping still points to the old block. As it would after
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.
99 /*----------------------------------------------------------------*/
104 static void build_data_key(struct dm_thin_device *td,
105 dm_block_t b, struct dm_cell_key *key)
108 key->dev = dm_thin_dev_id(td);
112 static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
113 struct dm_cell_key *key)
116 key->dev = dm_thin_dev_id(td);
120 /*----------------------------------------------------------------*/
123 * A pool device ties together a metadata device and a data device. It
124 * also provides the interface for creating and destroying internal
127 struct dm_thin_new_mapping;
130 * The pool runs in 3 modes. Ordered in degraded order for comparisons.
133 PM_WRITE, /* metadata may be changed */
134 PM_READ_ONLY, /* metadata may not be changed */
135 PM_FAIL, /* all I/O fails */
138 struct pool_features {
141 bool zero_new_blocks:1;
142 bool discard_enabled:1;
143 bool discard_passdown:1;
147 typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
148 typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
151 struct list_head list;
152 struct dm_target *ti; /* Only set if a pool target is bound */
154 struct mapped_device *pool_md;
155 struct block_device *md_dev;
156 struct dm_pool_metadata *pmd;
158 dm_block_t low_water_blocks;
159 uint32_t sectors_per_block;
160 int sectors_per_block_shift;
162 struct pool_features pf;
163 unsigned low_water_triggered:1; /* A dm event has been sent */
164 unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
166 struct dm_bio_prison *prison;
167 struct dm_kcopyd_client *copier;
169 struct workqueue_struct *wq;
170 struct work_struct worker;
171 struct delayed_work waker;
173 unsigned long last_commit_jiffies;
177 struct bio_list deferred_bios;
178 struct bio_list deferred_flush_bios;
179 struct list_head prepared_mappings;
180 struct list_head prepared_discards;
182 struct bio_list retry_on_resume_list;
184 struct dm_deferred_set *shared_read_ds;
185 struct dm_deferred_set *all_io_ds;
187 struct dm_thin_new_mapping *next_mapping;
188 mempool_t *mapping_pool;
189 mempool_t *endio_hook_pool;
191 process_bio_fn process_bio;
192 process_bio_fn process_discard;
194 process_mapping_fn process_prepared_mapping;
195 process_mapping_fn process_prepared_discard;
198 static enum pool_mode get_pool_mode(struct pool *pool);
199 static void set_pool_mode(struct pool *pool, enum pool_mode mode);
202 * Target context for a pool.
205 struct dm_target *ti;
207 struct dm_dev *data_dev;
208 struct dm_dev *metadata_dev;
209 struct dm_target_callbacks callbacks;
211 dm_block_t low_water_blocks;
212 struct pool_features requested_pf; /* Features requested during table load */
213 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
217 * Target context for a thin.
220 struct dm_dev *pool_dev;
221 struct dm_dev *origin_dev;
225 struct dm_thin_device *td;
228 /*----------------------------------------------------------------*/
231 * A global list of pools that uses a struct mapped_device as a key.
233 static struct dm_thin_pool_table {
235 struct list_head pools;
236 } dm_thin_pool_table;
238 static void pool_table_init(void)
240 mutex_init(&dm_thin_pool_table.mutex);
241 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
244 static void __pool_table_insert(struct pool *pool)
246 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
247 list_add(&pool->list, &dm_thin_pool_table.pools);
250 static void __pool_table_remove(struct pool *pool)
252 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
253 list_del(&pool->list);
256 static struct pool *__pool_table_lookup(struct mapped_device *md)
258 struct pool *pool = NULL, *tmp;
260 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
262 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
263 if (tmp->pool_md == md) {
272 static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
274 struct pool *pool = NULL, *tmp;
276 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
278 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
279 if (tmp->md_dev == md_dev) {
288 /*----------------------------------------------------------------*/
290 struct dm_thin_endio_hook {
292 struct dm_deferred_entry *shared_read_entry;
293 struct dm_deferred_entry *all_io_entry;
294 struct dm_thin_new_mapping *overwrite_mapping;
297 static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
300 struct bio_list bios;
302 bio_list_init(&bios);
303 bio_list_merge(&bios, master);
304 bio_list_init(master);
306 while ((bio = bio_list_pop(&bios))) {
307 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
310 bio_endio(bio, DM_ENDIO_REQUEUE);
312 bio_list_add(master, bio);
316 static void requeue_io(struct thin_c *tc)
318 struct pool *pool = tc->pool;
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);
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
334 static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
336 sector_t block_nr = bio->bi_sector;
338 if (tc->pool->sectors_per_block_shift < 0)
339 (void) sector_div(block_nr, tc->pool->sectors_per_block);
341 block_nr >>= tc->pool->sectors_per_block_shift;
346 static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
348 struct pool *pool = tc->pool;
349 sector_t bi_sector = bio->bi_sector;
351 bio->bi_bdev = tc->pool_dev->bdev;
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);
356 bio->bi_sector = (block << pool->sectors_per_block_shift) |
357 (bi_sector & (pool->sectors_per_block - 1));
360 static void remap_to_origin(struct thin_c *tc, struct bio *bio)
362 bio->bi_bdev = tc->origin_dev->bdev;
365 static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
367 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
368 dm_thin_changed_this_transaction(tc->td);
371 static void inc_all_io_entry(struct pool *pool, struct bio *bio)
373 struct dm_thin_endio_hook *h;
375 if (bio->bi_rw & REQ_DISCARD)
378 h = dm_get_mapinfo(bio)->ptr;
379 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
382 static void issue(struct thin_c *tc, struct bio *bio)
384 struct pool *pool = tc->pool;
387 if (!bio_triggers_commit(tc, bio)) {
388 generic_make_request(bio);
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.
397 if (dm_thin_aborted_changes(tc->td)) {
403 * Batch together any bios that trigger commits and then issue a
404 * single commit for them in process_deferred_bios().
406 spin_lock_irqsave(&pool->lock, flags);
407 bio_list_add(&pool->deferred_flush_bios, bio);
408 spin_unlock_irqrestore(&pool->lock, flags);
411 static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
413 remap_to_origin(tc, bio);
417 static void remap_and_issue(struct thin_c *tc, struct bio *bio,
420 remap(tc, bio, block);
425 * wake_worker() is used when new work is queued and when pool_resume is
426 * ready to continue deferred IO processing.
428 static void wake_worker(struct pool *pool)
430 queue_work(pool->wq, &pool->worker);
433 /*----------------------------------------------------------------*/
436 * Bio endio functions.
438 struct dm_thin_new_mapping {
439 struct list_head list;
443 unsigned pass_discard:1;
446 dm_block_t virt_block;
447 dm_block_t data_block;
448 struct dm_bio_prison_cell *cell, *cell2;
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
458 bio_end_io_t *saved_bi_end_io;
461 static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
463 struct pool *pool = m->tc->pool;
465 if (m->quiesced && m->prepared) {
466 list_add(&m->list, &pool->prepared_mappings);
471 static void copy_complete(int read_err, unsigned long write_err, void *context)
474 struct dm_thin_new_mapping *m = context;
475 struct pool *pool = m->tc->pool;
477 m->err = read_err || write_err ? -EIO : 0;
479 spin_lock_irqsave(&pool->lock, flags);
481 __maybe_add_mapping(m);
482 spin_unlock_irqrestore(&pool->lock, flags);
485 static void overwrite_endio(struct bio *bio, int err)
488 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
489 struct dm_thin_new_mapping *m = h->overwrite_mapping;
490 struct pool *pool = m->tc->pool;
494 spin_lock_irqsave(&pool->lock, flags);
496 __maybe_add_mapping(m);
497 spin_unlock_irqrestore(&pool->lock, flags);
500 /*----------------------------------------------------------------*/
507 * Prepared mapping jobs.
511 * This sends the bios in the cell back to the deferred_bios list.
513 static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell)
515 struct pool *pool = tc->pool;
518 spin_lock_irqsave(&pool->lock, flags);
519 dm_cell_release(cell, &pool->deferred_bios);
520 spin_unlock_irqrestore(&tc->pool->lock, flags);
526 * Same as cell_defer except it omits the original holder of the cell.
528 static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
530 struct pool *pool = tc->pool;
533 spin_lock_irqsave(&pool->lock, flags);
534 dm_cell_release_no_holder(cell, &pool->deferred_bios);
535 spin_unlock_irqrestore(&pool->lock, flags);
540 static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
543 m->bio->bi_end_io = m->saved_bi_end_io;
544 dm_cell_error(m->cell);
546 mempool_free(m, m->tc->pool->mapping_pool);
548 static void process_prepared_mapping(struct dm_thin_new_mapping *m)
550 struct thin_c *tc = m->tc;
556 bio->bi_end_io = m->saved_bi_end_io;
559 dm_cell_error(m->cell);
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.
568 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
570 DMERR("dm_thin_insert_block() failed");
571 dm_cell_error(m->cell);
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.
582 cell_defer_no_holder(tc, m->cell);
585 cell_defer(tc, m->cell);
589 mempool_free(m, tc->pool->mapping_pool);
592 static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
594 struct thin_c *tc = m->tc;
596 bio_io_error(m->bio);
597 cell_defer_no_holder(tc, m->cell);
598 cell_defer_no_holder(tc, m->cell2);
599 mempool_free(m, tc->pool->mapping_pool);
602 static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
604 struct thin_c *tc = m->tc;
606 inc_all_io_entry(tc->pool, m->bio);
607 cell_defer_no_holder(tc, m->cell);
608 cell_defer_no_holder(tc, m->cell2);
611 remap_and_issue(tc, m->bio, m->data_block);
613 bio_endio(m->bio, 0);
615 mempool_free(m, tc->pool->mapping_pool);
618 static void process_prepared_discard(struct dm_thin_new_mapping *m)
621 struct thin_c *tc = m->tc;
623 r = dm_thin_remove_block(tc->td, m->virt_block);
625 DMERR("dm_thin_remove_block() failed");
627 process_prepared_discard_passdown(m);
630 static void process_prepared(struct pool *pool, struct list_head *head,
631 process_mapping_fn *fn)
634 struct list_head maps;
635 struct dm_thin_new_mapping *m, *tmp;
637 INIT_LIST_HEAD(&maps);
638 spin_lock_irqsave(&pool->lock, flags);
639 list_splice_init(head, &maps);
640 spin_unlock_irqrestore(&pool->lock, flags);
642 list_for_each_entry_safe(m, tmp, &maps, list)
649 static int io_overlaps_block(struct pool *pool, struct bio *bio)
651 return bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT);
654 static int io_overwrites_block(struct pool *pool, struct bio *bio)
656 return (bio_data_dir(bio) == WRITE) &&
657 io_overlaps_block(pool, bio);
660 static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
663 *save = bio->bi_end_io;
667 static int ensure_next_mapping(struct pool *pool)
669 if (pool->next_mapping)
672 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
674 return pool->next_mapping ? 0 : -ENOMEM;
677 static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
679 struct dm_thin_new_mapping *r = pool->next_mapping;
681 BUG_ON(!pool->next_mapping);
683 pool->next_mapping = NULL;
688 static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
689 struct dm_dev *origin, dm_block_t data_origin,
690 dm_block_t data_dest,
691 struct dm_bio_prison_cell *cell, struct bio *bio)
694 struct pool *pool = tc->pool;
695 struct dm_thin_new_mapping *m = get_next_mapping(pool);
697 INIT_LIST_HEAD(&m->list);
701 m->virt_block = virt_block;
702 m->data_block = data_dest;
707 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
711 * IO to pool_dev remaps to the pool target's data_dev.
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.
716 if (io_overwrites_block(pool, bio)) {
717 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
719 h->overwrite_mapping = m;
721 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
722 inc_all_io_entry(pool, bio);
723 remap_and_issue(tc, bio, data_dest);
725 struct dm_io_region from, to;
727 from.bdev = origin->bdev;
728 from.sector = data_origin * pool->sectors_per_block;
729 from.count = pool->sectors_per_block;
731 to.bdev = tc->pool_dev->bdev;
732 to.sector = data_dest * pool->sectors_per_block;
733 to.count = pool->sectors_per_block;
735 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
736 0, copy_complete, m);
738 mempool_free(m, pool->mapping_pool);
739 DMERR("dm_kcopyd_copy() failed");
745 static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
746 dm_block_t data_origin, dm_block_t data_dest,
747 struct dm_bio_prison_cell *cell, struct bio *bio)
749 schedule_copy(tc, virt_block, tc->pool_dev,
750 data_origin, data_dest, cell, bio);
753 static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
754 dm_block_t data_dest,
755 struct dm_bio_prison_cell *cell, struct bio *bio)
757 schedule_copy(tc, virt_block, tc->origin_dev,
758 virt_block, data_dest, cell, bio);
761 static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
762 dm_block_t data_block, struct dm_bio_prison_cell *cell,
765 struct pool *pool = tc->pool;
766 struct dm_thin_new_mapping *m = get_next_mapping(pool);
768 INIT_LIST_HEAD(&m->list);
772 m->virt_block = virt_block;
773 m->data_block = data_block;
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.
783 if (!pool->pf.zero_new_blocks)
784 process_prepared_mapping(m);
786 else if (io_overwrites_block(pool, bio)) {
787 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
789 h->overwrite_mapping = m;
791 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
792 inc_all_io_entry(pool, bio);
793 remap_and_issue(tc, bio, data_block);
796 struct dm_io_region to;
798 to.bdev = tc->pool_dev->bdev;
799 to.sector = data_block * pool->sectors_per_block;
800 to.count = pool->sectors_per_block;
802 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
804 mempool_free(m, pool->mapping_pool);
805 DMERR("dm_kcopyd_zero() failed");
811 static int commit(struct pool *pool)
815 r = dm_pool_commit_metadata(pool->pmd);
817 DMERR("commit failed, error = %d", r);
823 * A non-zero return indicates read_only or fail_io mode.
824 * Many callers don't care about the return value.
826 static int commit_or_fallback(struct pool *pool)
830 if (get_pool_mode(pool) != PM_WRITE)
835 set_pool_mode(pool, PM_READ_ONLY);
840 static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
843 dm_block_t free_blocks;
845 struct pool *pool = tc->pool;
847 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
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);
861 if (pool->no_free_space)
865 * Try to commit to see if that will free up some
868 (void) commit_or_fallback(pool);
870 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
875 * If we still have no space we set a flag to avoid
876 * doing all this checking and return -ENOSPC.
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);
889 r = dm_pool_alloc_data_block(pool->pmd, result);
897 * If we have run out of space, queue bios until the device is
898 * resumed, presumably after having been reloaded with more space.
900 static void retry_on_resume(struct bio *bio)
902 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
903 struct thin_c *tc = h->tc;
904 struct pool *pool = tc->pool;
907 spin_lock_irqsave(&pool->lock, flags);
908 bio_list_add(&pool->retry_on_resume_list, bio);
909 spin_unlock_irqrestore(&pool->lock, flags);
912 static void no_space(struct dm_bio_prison_cell *cell)
915 struct bio_list bios;
917 bio_list_init(&bios);
918 dm_cell_release(cell, &bios);
920 while ((bio = bio_list_pop(&bios)))
921 retry_on_resume(bio);
924 static void process_discard(struct thin_c *tc, struct bio *bio)
928 struct pool *pool = tc->pool;
929 struct dm_bio_prison_cell *cell, *cell2;
930 struct dm_cell_key key, key2;
931 dm_block_t block = get_bio_block(tc, bio);
932 struct dm_thin_lookup_result lookup_result;
933 struct dm_thin_new_mapping *m;
935 build_virtual_key(tc->td, block, &key);
936 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
939 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
943 * Check nobody is fiddling with this pool block. This can
944 * happen if someone's in the process of breaking sharing
947 build_data_key(tc->td, lookup_result.block, &key2);
948 if (dm_bio_detain(tc->pool->prison, &key2, bio, &cell2)) {
949 cell_defer_no_holder(tc, cell);
953 if (io_overlaps_block(pool, bio)) {
955 * IO may still be going to the destination block. We must
956 * quiesce before we can do the removal.
958 m = get_next_mapping(pool);
960 m->pass_discard = (!lookup_result.shared) && pool->pf.discard_passdown;
961 m->virt_block = block;
962 m->data_block = lookup_result.block;
968 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
969 spin_lock_irqsave(&pool->lock, flags);
970 list_add(&m->list, &pool->prepared_discards);
971 spin_unlock_irqrestore(&pool->lock, flags);
975 inc_all_io_entry(pool, bio);
976 cell_defer_no_holder(tc, cell);
977 cell_defer_no_holder(tc, cell2);
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.
984 if ((!lookup_result.shared) && pool->pf.discard_passdown)
985 remap_and_issue(tc, bio, lookup_result.block);
993 * It isn't provisioned, just forget it.
995 cell_defer_no_holder(tc, cell);
1000 DMERR("discard: find block unexpectedly returned %d", r);
1001 cell_defer_no_holder(tc, cell);
1007 static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
1008 struct dm_cell_key *key,
1009 struct dm_thin_lookup_result *lookup_result,
1010 struct dm_bio_prison_cell *cell)
1013 dm_block_t data_block;
1015 r = alloc_data_block(tc, &data_block);
1018 schedule_internal_copy(tc, block, lookup_result->block,
1019 data_block, cell, bio);
1027 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
1028 dm_cell_error(cell);
1033 static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1035 struct dm_thin_lookup_result *lookup_result)
1037 struct dm_bio_prison_cell *cell;
1038 struct pool *pool = tc->pool;
1039 struct dm_cell_key key;
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.
1045 build_data_key(tc->td, lookup_result->block, &key);
1046 if (dm_bio_detain(pool->prison, &key, bio, &cell))
1049 if (bio_data_dir(bio) == WRITE && bio->bi_size)
1050 break_sharing(tc, bio, block, &key, lookup_result, cell);
1052 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
1054 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
1055 inc_all_io_entry(pool, bio);
1056 cell_defer_no_holder(tc, cell);
1058 remap_and_issue(tc, bio, lookup_result->block);
1062 static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
1063 struct dm_bio_prison_cell *cell)
1066 dm_block_t data_block;
1069 * Remap empty bios (flushes) immediately, without provisioning.
1071 if (!bio->bi_size) {
1072 inc_all_io_entry(tc->pool, bio);
1073 cell_defer_no_holder(tc, cell);
1075 remap_and_issue(tc, bio, 0);
1080 * Fill read bios with zeroes and complete them immediately.
1082 if (bio_data_dir(bio) == READ) {
1084 cell_defer_no_holder(tc, cell);
1089 r = alloc_data_block(tc, &data_block);
1093 schedule_external_copy(tc, block, data_block, cell, bio);
1095 schedule_zero(tc, block, data_block, cell, bio);
1103 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
1104 set_pool_mode(tc->pool, PM_READ_ONLY);
1105 dm_cell_error(cell);
1110 static void process_bio(struct thin_c *tc, struct bio *bio)
1113 dm_block_t block = get_bio_block(tc, bio);
1114 struct dm_bio_prison_cell *cell;
1115 struct dm_cell_key key;
1116 struct dm_thin_lookup_result lookup_result;
1119 * If cell is already occupied, then the block is already
1120 * being provisioned so we have nothing further to do here.
1122 build_virtual_key(tc->td, block, &key);
1123 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
1126 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1129 if (lookup_result.shared) {
1130 process_shared_bio(tc, bio, block, &lookup_result);
1131 cell_defer_no_holder(tc, cell);
1133 inc_all_io_entry(tc->pool, bio);
1134 cell_defer_no_holder(tc, cell);
1136 remap_and_issue(tc, bio, lookup_result.block);
1141 if (bio_data_dir(bio) == READ && tc->origin_dev) {
1142 inc_all_io_entry(tc->pool, bio);
1143 cell_defer_no_holder(tc, cell);
1145 remap_to_origin_and_issue(tc, bio);
1147 provision_block(tc, bio, block, cell);
1151 DMERR("dm_thin_find_block() failed, error = %d", r);
1152 cell_defer_no_holder(tc, cell);
1158 static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
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;
1165 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1168 if (lookup_result.shared && (rw == WRITE) && bio->bi_size)
1171 inc_all_io_entry(tc->pool, bio);
1172 remap_and_issue(tc, bio, lookup_result.block);
1182 if (tc->origin_dev) {
1183 inc_all_io_entry(tc->pool, bio);
1184 remap_to_origin_and_issue(tc, bio);
1193 DMERR("dm_thin_find_block() failed, error = %d", r);
1199 static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1204 static int need_commit_due_to_time(struct pool *pool)
1206 return jiffies < pool->last_commit_jiffies ||
1207 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1210 static void process_deferred_bios(struct pool *pool)
1212 unsigned long flags;
1214 struct bio_list bios;
1216 bio_list_init(&bios);
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);
1223 while ((bio = bio_list_pop(&bios))) {
1224 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
1225 struct thin_c *tc = h->tc;
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.
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);
1240 if (bio->bi_rw & REQ_DISCARD)
1241 pool->process_discard(tc, bio);
1243 pool->process_bio(tc, bio);
1247 * If there are any deferred flush bios, we must commit
1248 * the metadata before issuing them.
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);
1256 if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
1259 if (commit_or_fallback(pool)) {
1260 while ((bio = bio_list_pop(&bios)))
1264 pool->last_commit_jiffies = jiffies;
1266 while ((bio = bio_list_pop(&bios)))
1267 generic_make_request(bio);
1270 static void do_worker(struct work_struct *ws)
1272 struct pool *pool = container_of(ws, struct pool, worker);
1274 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1275 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
1276 process_deferred_bios(pool);
1280 * We want to commit periodically so that not too much
1281 * unwritten data builds up.
1283 static void do_waker(struct work_struct *ws)
1285 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1287 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1290 /*----------------------------------------------------------------*/
1292 static enum pool_mode get_pool_mode(struct pool *pool)
1294 return pool->pf.mode;
1297 static void set_pool_mode(struct pool *pool, enum pool_mode mode)
1301 pool->pf.mode = mode;
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;
1313 DMERR("switching pool to read-only mode");
1314 r = dm_pool_abort_metadata(pool->pmd);
1316 DMERR("aborting transaction failed");
1317 set_pool_mode(pool, PM_FAIL);
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;
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;
1336 /*----------------------------------------------------------------*/
1339 * Mapping functions.
1343 * Called only while mapping a thin bio to hand it over to the workqueue.
1345 static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1347 unsigned long flags;
1348 struct pool *pool = tc->pool;
1350 spin_lock_irqsave(&pool->lock, flags);
1351 bio_list_add(&pool->deferred_bios, bio);
1352 spin_unlock_irqrestore(&pool->lock, flags);
1357 static struct dm_thin_endio_hook *thin_hook_bio(struct thin_c *tc, struct bio *bio)
1359 struct pool *pool = tc->pool;
1360 struct dm_thin_endio_hook *h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
1363 h->shared_read_entry = NULL;
1364 h->all_io_entry = NULL;
1365 h->overwrite_mapping = NULL;
1371 * Non-blocking function called from the thin target's map function.
1373 static int thin_bio_map(struct dm_target *ti, struct bio *bio,
1374 union map_info *map_context)
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;
1381 struct dm_bio_prison_cell *cell1, *cell2;
1382 struct dm_cell_key key;
1384 map_context->ptr = thin_hook_bio(tc, bio);
1386 if (get_pool_mode(tc->pool) == PM_FAIL) {
1388 return DM_MAPIO_SUBMITTED;
1391 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
1392 thin_defer_bio(tc, bio);
1393 return DM_MAPIO_SUBMITTED;
1396 r = dm_thin_find_block(td, block, 0, &result);
1399 * Note that we defer readahead too.
1403 if (unlikely(result.shared)) {
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
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
1415 * More distant ancestors are irrelevant. The
1416 * shared flag will be set in their case.
1418 thin_defer_bio(tc, bio);
1419 return DM_MAPIO_SUBMITTED;
1422 build_virtual_key(tc->td, block, &key);
1423 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1))
1424 return DM_MAPIO_SUBMITTED;
1426 build_data_key(tc->td, result.block, &key);
1427 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2)) {
1428 cell_defer_no_holder(tc, cell1);
1429 return DM_MAPIO_SUBMITTED;
1432 inc_all_io_entry(tc->pool, bio);
1433 cell_defer_no_holder(tc, cell2);
1434 cell_defer_no_holder(tc, cell1);
1436 remap(tc, bio, result.block);
1437 return DM_MAPIO_REMAPPED;
1440 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
1442 * This block isn't provisioned, and we have no way
1443 * of doing so. Just error it.
1446 return DM_MAPIO_SUBMITTED;
1452 * In future, the failed dm_thin_find_block above could
1453 * provide the hint to load the metadata into cache.
1455 thin_defer_bio(tc, bio);
1456 return DM_MAPIO_SUBMITTED;
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.
1465 return DM_MAPIO_SUBMITTED;
1469 static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1472 unsigned long flags;
1473 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
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);
1480 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1481 r = bdi_congested(&q->backing_dev_info, bdi_bits);
1487 static void __requeue_bios(struct pool *pool)
1489 bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
1490 bio_list_init(&pool->retry_on_resume_list);
1493 /*----------------------------------------------------------------
1494 * Binding of control targets to a pool object
1495 *--------------------------------------------------------------*/
1496 static bool data_dev_supports_discard(struct pool_c *pt)
1498 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1500 return q && blk_queue_discard(q);
1504 * If discard_passdown was enabled verify that the data device
1505 * supports discards. Disable discard_passdown if not.
1507 static void disable_passdown_if_not_supported(struct pool_c *pt)
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;
1514 char buf[BDEVNAME_SIZE];
1516 if (!pt->adjusted_pf.discard_passdown)
1519 if (!data_dev_supports_discard(pt))
1520 reason = "discard unsupported";
1522 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
1523 reason = "max discard sectors smaller than a block";
1525 else if (data_limits->discard_granularity > block_size)
1526 reason = "discard granularity larger than a block";
1528 else if (block_size & (data_limits->discard_granularity - 1))
1529 reason = "discard granularity not a factor of block size";
1532 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
1533 pt->adjusted_pf.discard_passdown = false;
1537 static int bind_control_target(struct pool *pool, struct dm_target *ti)
1539 struct pool_c *pt = ti->private;
1542 * We want to make sure that degraded pools are never upgraded.
1544 enum pool_mode old_mode = pool->pf.mode;
1545 enum pool_mode new_mode = pt->adjusted_pf.mode;
1547 if (old_mode > new_mode)
1548 new_mode = old_mode;
1551 pool->low_water_blocks = pt->low_water_blocks;
1552 pool->pf = pt->adjusted_pf;
1554 set_pool_mode(pool, new_mode);
1559 static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1565 /*----------------------------------------------------------------
1567 *--------------------------------------------------------------*/
1568 /* Initialize pool features. */
1569 static void pool_features_init(struct pool_features *pf)
1571 pf->mode = PM_WRITE;
1572 pf->zero_new_blocks = true;
1573 pf->discard_enabled = true;
1574 pf->discard_passdown = true;
1577 static void __pool_destroy(struct pool *pool)
1579 __pool_table_remove(pool);
1581 if (dm_pool_metadata_close(pool->pmd) < 0)
1582 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1584 dm_bio_prison_destroy(pool->prison);
1585 dm_kcopyd_client_destroy(pool->copier);
1588 destroy_workqueue(pool->wq);
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);
1594 dm_deferred_set_destroy(pool->shared_read_ds);
1595 dm_deferred_set_destroy(pool->all_io_ds);
1599 static struct kmem_cache *_new_mapping_cache;
1600 static struct kmem_cache *_endio_hook_cache;
1602 static struct pool *pool_create(struct mapped_device *pool_md,
1603 struct block_device *metadata_dev,
1604 unsigned long block_size,
1605 int read_only, char **error)
1610 struct dm_pool_metadata *pmd;
1611 bool format_device = read_only ? false : true;
1613 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
1615 *error = "Error creating metadata object";
1616 return (struct pool *)pmd;
1619 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
1621 *error = "Error allocating memory for pool";
1622 err_p = ERR_PTR(-ENOMEM);
1627 pool->sectors_per_block = block_size;
1628 if (block_size & (block_size - 1))
1629 pool->sectors_per_block_shift = -1;
1631 pool->sectors_per_block_shift = __ffs(block_size);
1632 pool->low_water_blocks = 0;
1633 pool_features_init(&pool->pf);
1634 pool->prison = dm_bio_prison_create(PRISON_CELLS);
1635 if (!pool->prison) {
1636 *error = "Error creating pool's bio prison";
1637 err_p = ERR_PTR(-ENOMEM);
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";
1646 goto bad_kcopyd_client;
1650 * Create singlethreaded workqueue that will service all devices
1651 * that use this metadata.
1653 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1655 *error = "Error creating pool's workqueue";
1656 err_p = ERR_PTR(-ENOMEM);
1660 INIT_WORK(&pool->worker, do_worker);
1661 INIT_DELAYED_WORK(&pool->waker, do_waker);
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);
1666 INIT_LIST_HEAD(&pool->prepared_discards);
1667 pool->low_water_triggered = 0;
1668 pool->no_free_space = 0;
1669 bio_list_init(&pool->retry_on_resume_list);
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;
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);
1685 pool->next_mapping = NULL;
1686 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
1687 _new_mapping_cache);
1688 if (!pool->mapping_pool) {
1689 *error = "Error creating pool's mapping mempool";
1690 err_p = ERR_PTR(-ENOMEM);
1691 goto bad_mapping_pool;
1694 pool->endio_hook_pool = mempool_create_slab_pool(ENDIO_HOOK_POOL_SIZE,
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;
1701 pool->ref_count = 1;
1702 pool->last_commit_jiffies = jiffies;
1703 pool->pool_md = pool_md;
1704 pool->md_dev = metadata_dev;
1705 __pool_table_insert(pool);
1709 bad_endio_hook_pool:
1710 mempool_destroy(pool->mapping_pool);
1712 dm_deferred_set_destroy(pool->all_io_ds);
1714 dm_deferred_set_destroy(pool->shared_read_ds);
1716 destroy_workqueue(pool->wq);
1718 dm_kcopyd_client_destroy(pool->copier);
1720 dm_bio_prison_destroy(pool->prison);
1724 if (dm_pool_metadata_close(pmd))
1725 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1730 static void __pool_inc(struct pool *pool)
1732 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1736 static void __pool_dec(struct pool *pool)
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);
1744 static struct pool *__pool_find(struct mapped_device *pool_md,
1745 struct block_device *metadata_dev,
1746 unsigned long block_size, int read_only,
1747 char **error, int *created)
1749 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
1752 if (pool->pool_md != pool_md) {
1753 *error = "metadata device already in use by a pool";
1754 return ERR_PTR(-EBUSY);
1759 pool = __pool_table_lookup(pool_md);
1761 if (pool->md_dev != metadata_dev) {
1762 *error = "different pool cannot replace a pool";
1763 return ERR_PTR(-EINVAL);
1768 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
1776 /*----------------------------------------------------------------
1777 * Pool target methods
1778 *--------------------------------------------------------------*/
1779 static void pool_dtr(struct dm_target *ti)
1781 struct pool_c *pt = ti->private;
1783 mutex_lock(&dm_thin_pool_table.mutex);
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);
1791 mutex_unlock(&dm_thin_pool_table.mutex);
1794 static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
1795 struct dm_target *ti)
1799 const char *arg_name;
1801 static struct dm_arg _args[] = {
1802 {0, 3, "Invalid number of pool feature arguments"},
1806 * No feature arguments supplied.
1811 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1815 while (argc && !r) {
1816 arg_name = dm_shift_arg(as);
1819 if (!strcasecmp(arg_name, "skip_block_zeroing"))
1820 pf->zero_new_blocks = false;
1822 else if (!strcasecmp(arg_name, "ignore_discard"))
1823 pf->discard_enabled = false;
1825 else if (!strcasecmp(arg_name, "no_discard_passdown"))
1826 pf->discard_passdown = false;
1828 else if (!strcasecmp(arg_name, "read_only"))
1829 pf->mode = PM_READ_ONLY;
1832 ti->error = "Unrecognised pool feature requested";
1842 * thin-pool <metadata dev> <data dev>
1843 * <data block size (sectors)>
1844 * <low water mark (blocks)>
1845 * [<#feature args> [<arg>]*]
1847 * Optional feature arguments are:
1848 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
1849 * ignore_discard: disable discard
1850 * no_discard_passdown: don't pass discards down to the data device
1852 static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
1854 int r, pool_created = 0;
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;
1864 char b[BDEVNAME_SIZE];
1867 * FIXME Remove validation from scope of lock.
1869 mutex_lock(&dm_thin_pool_table.mutex);
1872 ti->error = "Invalid argument count";
1879 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
1881 ti->error = "Error opening metadata block device";
1885 metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
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);
1890 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
1892 ti->error = "Error getting data device";
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 ||
1899 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
1900 ti->error = "Invalid block size";
1905 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
1906 ti->error = "Invalid low water mark";
1912 * Set default pool features.
1914 pool_features_init(&pf);
1916 dm_consume_args(&as, 4);
1917 r = parse_pool_features(&as, &pf, ti);
1921 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
1927 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
1928 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
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
1940 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
1941 ti->error = "Discard support cannot be disabled once enabled";
1943 goto out_flags_changed;
1948 pt->metadata_dev = metadata_dev;
1949 pt->data_dev = data_dev;
1950 pt->low_water_blocks = low_water_blocks;
1951 pt->adjusted_pf = pt->requested_pf = pf;
1952 ti->num_flush_requests = 1;
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.
1959 if (pf.discard_enabled && pf.discard_passdown) {
1960 ti->num_discard_requests = 1;
1963 * Setting 'discards_supported' circumvents the normal
1964 * stacking of discard limits (this keeps the pool and
1965 * thin devices' discard limits consistent).
1967 ti->discards_supported = true;
1968 ti->discard_zeroes_data_unsupported = true;
1972 pt->callbacks.congested_fn = pool_is_congested;
1973 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
1975 mutex_unlock(&dm_thin_pool_table.mutex);
1984 dm_put_device(ti, data_dev);
1986 dm_put_device(ti, metadata_dev);
1988 mutex_unlock(&dm_thin_pool_table.mutex);
1993 static int pool_map(struct dm_target *ti, struct bio *bio,
1994 union map_info *map_context)
1997 struct pool_c *pt = ti->private;
1998 struct pool *pool = pt->pool;
1999 unsigned long flags;
2002 * As this is a singleton target, ti->begin is always zero.
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);
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.
2017 * This both copes with opening preallocated data devices in the ctr
2018 * being followed by a resume
2020 * calling the resume method individually after userspace has
2021 * grown the data device in reaction to a table event.
2023 static int pool_preresume(struct dm_target *ti)
2026 struct pool_c *pt = ti->private;
2027 struct pool *pool = pt->pool;
2028 sector_t data_size = ti->len;
2029 dm_block_t sb_data_size;
2032 * Take control of the pool object.
2034 r = bind_control_target(pool, ti);
2038 (void) sector_div(data_size, pool->sectors_per_block);
2040 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2042 DMERR("failed to retrieve data device size");
2046 if (data_size < sb_data_size) {
2047 DMERR("pool target too small, is %llu blocks (expected %llu)",
2048 (unsigned long long)data_size, sb_data_size);
2051 } else if (data_size > sb_data_size) {
2052 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2054 DMERR("failed to resize data device");
2055 /* FIXME Stricter than necessary: Rollback transaction instead here */
2056 set_pool_mode(pool, PM_READ_ONLY);
2060 (void) commit_or_fallback(pool);
2066 static void pool_resume(struct dm_target *ti)
2068 struct pool_c *pt = ti->private;
2069 struct pool *pool = pt->pool;
2070 unsigned long flags;
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);
2078 do_waker(&pool->waker.work);
2081 static void pool_postsuspend(struct dm_target *ti)
2083 struct pool_c *pt = ti->private;
2084 struct pool *pool = pt->pool;
2086 cancel_delayed_work(&pool->waker);
2087 flush_workqueue(pool->wq);
2088 (void) commit_or_fallback(pool);
2091 static int check_arg_count(unsigned argc, unsigned args_required)
2093 if (argc != args_required) {
2094 DMWARN("Message received with %u arguments instead of %u.",
2095 argc, args_required);
2102 static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2104 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2105 *dev_id <= MAX_DEV_ID)
2109 DMWARN("Message received with invalid device id: %s", arg);
2114 static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2119 r = check_arg_count(argc, 2);
2123 r = read_dev_id(argv[1], &dev_id, 1);
2127 r = dm_pool_create_thin(pool->pmd, dev_id);
2129 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2137 static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2140 dm_thin_id origin_dev_id;
2143 r = check_arg_count(argc, 3);
2147 r = read_dev_id(argv[1], &dev_id, 1);
2151 r = read_dev_id(argv[2], &origin_dev_id, 1);
2155 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2157 DMWARN("Creation of new snapshot %s of device %s failed.",
2165 static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2170 r = check_arg_count(argc, 2);
2174 r = read_dev_id(argv[1], &dev_id, 1);
2178 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2180 DMWARN("Deletion of thin device %s failed.", argv[1]);
2185 static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2187 dm_thin_id old_id, new_id;
2190 r = check_arg_count(argc, 3);
2194 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2195 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2199 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2200 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2204 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2206 DMWARN("Failed to change transaction id from %s to %s.",
2214 static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2218 r = check_arg_count(argc, 1);
2222 (void) commit_or_fallback(pool);
2224 r = dm_pool_reserve_metadata_snap(pool->pmd);
2226 DMWARN("reserve_metadata_snap message failed.");
2231 static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2235 r = check_arg_count(argc, 1);
2239 r = dm_pool_release_metadata_snap(pool->pmd);
2241 DMWARN("release_metadata_snap message failed.");
2247 * Messages supported:
2248 * create_thin <dev_id>
2249 * create_snap <dev_id> <origin_id>
2251 * trim <dev_id> <new_size_in_sectors>
2252 * set_transaction_id <current_trans_id> <new_trans_id>
2253 * reserve_metadata_snap
2254 * release_metadata_snap
2256 static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2259 struct pool_c *pt = ti->private;
2260 struct pool *pool = pt->pool;
2262 if (!strcasecmp(argv[0], "create_thin"))
2263 r = process_create_thin_mesg(argc, argv, pool);
2265 else if (!strcasecmp(argv[0], "create_snap"))
2266 r = process_create_snap_mesg(argc, argv, pool);
2268 else if (!strcasecmp(argv[0], "delete"))
2269 r = process_delete_mesg(argc, argv, pool);
2271 else if (!strcasecmp(argv[0], "set_transaction_id"))
2272 r = process_set_transaction_id_mesg(argc, argv, pool);
2274 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
2275 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
2277 else if (!strcasecmp(argv[0], "release_metadata_snap"))
2278 r = process_release_metadata_snap_mesg(argc, argv, pool);
2281 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2284 (void) commit_or_fallback(pool);
2289 static void emit_flags(struct pool_features *pf, char *result,
2290 unsigned sz, unsigned maxlen)
2292 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
2293 !pf->discard_passdown + (pf->mode == PM_READ_ONLY);
2294 DMEMIT("%u ", count);
2296 if (!pf->zero_new_blocks)
2297 DMEMIT("skip_block_zeroing ");
2299 if (!pf->discard_enabled)
2300 DMEMIT("ignore_discard ");
2302 if (!pf->discard_passdown)
2303 DMEMIT("no_discard_passdown ");
2305 if (pf->mode == PM_READ_ONLY)
2306 DMEMIT("read_only ");
2311 * <transaction id> <used metadata sectors>/<total metadata sectors>
2312 * <used data sectors>/<total data sectors> <held metadata root>
2314 static int pool_status(struct dm_target *ti, status_type_t type,
2315 unsigned status_flags, char *result, unsigned maxlen)
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;
2331 case STATUSTYPE_INFO:
2332 if (get_pool_mode(pool) == PM_FAIL) {
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);
2341 r = dm_pool_get_metadata_transaction_id(pool->pmd,
2346 r = dm_pool_get_free_metadata_block_count(pool->pmd,
2347 &nr_free_blocks_metadata);
2351 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
2355 r = dm_pool_get_free_block_count(pool->pmd,
2356 &nr_free_blocks_data);
2360 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
2364 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
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);
2376 DMEMIT("%llu ", held_root);
2380 if (pool->pf.mode == PM_READ_ONLY)
2385 if (!pool->pf.discard_enabled)
2386 DMEMIT("ignore_discard");
2387 else if (pool->pf.discard_passdown)
2388 DMEMIT("discard_passdown");
2390 DMEMIT("no_discard_passdown");
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);
2400 emit_flags(&pt->requested_pf, result, sz, maxlen);
2407 static int pool_iterate_devices(struct dm_target *ti,
2408 iterate_devices_callout_fn fn, void *data)
2410 struct pool_c *pt = ti->private;
2412 return fn(ti, pt->data_dev, 0, ti->len, data);
2415 static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2416 struct bio_vec *biovec, int max_size)
2418 struct pool_c *pt = ti->private;
2419 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2421 if (!q->merge_bvec_fn)
2424 bvm->bi_bdev = pt->data_dev->bdev;
2426 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2429 static bool block_size_is_power_of_two(struct pool *pool)
2431 return pool->sectors_per_block_shift >= 0;
2434 static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
2436 struct pool *pool = pt->pool;
2437 struct queue_limits *data_limits;
2439 limits->max_discard_sectors = pool->sectors_per_block;
2442 * discard_granularity is just a hint, and not enforced.
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;
2447 } else if (block_size_is_power_of_two(pool))
2448 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
2451 * Use largest power of 2 that is a factor of sectors_per_block
2452 * but at least DATA_DEV_BLOCK_SIZE_MIN_SECTORS.
2454 limits->discard_granularity = max(1 << (ffs(pool->sectors_per_block) - 1),
2455 DATA_DEV_BLOCK_SIZE_MIN_SECTORS) << SECTOR_SHIFT;
2458 static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
2460 struct pool_c *pt = ti->private;
2461 struct pool *pool = pt->pool;
2463 blk_limits_io_min(limits, 0);
2464 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
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().
2471 if (!pt->adjusted_pf.discard_enabled)
2474 disable_passdown_if_not_supported(pt);
2476 set_discard_limits(pt, limits);
2479 static struct target_type pool_target = {
2480 .name = "thin-pool",
2481 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
2482 DM_TARGET_IMMUTABLE,
2483 .version = {1, 6, 0},
2484 .module = THIS_MODULE,
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,
2498 /*----------------------------------------------------------------
2499 * Thin target methods
2500 *--------------------------------------------------------------*/
2501 static void thin_dtr(struct dm_target *ti)
2503 struct thin_c *tc = ti->private;
2505 mutex_lock(&dm_thin_pool_table.mutex);
2507 __pool_dec(tc->pool);
2508 dm_pool_close_thin_device(tc->td);
2509 dm_put_device(ti, tc->pool_dev);
2511 dm_put_device(ti, tc->origin_dev);
2514 mutex_unlock(&dm_thin_pool_table.mutex);
2518 * Thin target parameters:
2520 * <pool_dev> <dev_id> [origin_dev]
2522 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2523 * dev_id: the internal device identifier
2524 * origin_dev: a device external to the pool that should act as the origin
2526 * If the pool device has discards disabled, they get disabled for the thin
2529 static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
2533 struct dm_dev *pool_dev, *origin_dev;
2534 struct mapped_device *pool_md;
2536 mutex_lock(&dm_thin_pool_table.mutex);
2538 if (argc != 2 && argc != 3) {
2539 ti->error = "Invalid argument count";
2544 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
2546 ti->error = "Out of memory";
2552 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
2554 ti->error = "Error opening origin device";
2555 goto bad_origin_dev;
2557 tc->origin_dev = origin_dev;
2560 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
2562 ti->error = "Error opening pool device";
2565 tc->pool_dev = pool_dev;
2567 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
2568 ti->error = "Invalid device id";
2573 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
2575 ti->error = "Couldn't get pool mapped device";
2580 tc->pool = __pool_table_lookup(pool_md);
2582 ti->error = "Couldn't find pool object";
2584 goto bad_pool_lookup;
2586 __pool_inc(tc->pool);
2588 if (get_pool_mode(tc->pool) == PM_FAIL) {
2589 ti->error = "Couldn't open thin device, Pool is in fail mode";
2593 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
2595 ti->error = "Couldn't open thin internal device";
2599 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
2603 ti->num_flush_requests = 1;
2604 ti->flush_supported = true;
2606 /* In case the pool supports discards, pass them on. */
2607 if (tc->pool->pf.discard_enabled) {
2608 ti->discards_supported = true;
2609 ti->num_discard_requests = 1;
2610 ti->discard_zeroes_data_unsupported = true;
2611 /* Discard requests must be split on a block boundary */
2612 ti->split_discard_requests = true;
2617 mutex_unlock(&dm_thin_pool_table.mutex);
2622 __pool_dec(tc->pool);
2626 dm_put_device(ti, tc->pool_dev);
2629 dm_put_device(ti, tc->origin_dev);
2633 mutex_unlock(&dm_thin_pool_table.mutex);
2638 static int thin_map(struct dm_target *ti, struct bio *bio,
2639 union map_info *map_context)
2641 bio->bi_sector = dm_target_offset(ti, bio->bi_sector);
2643 return thin_bio_map(ti, bio, map_context);
2646 static int thin_endio(struct dm_target *ti,
2647 struct bio *bio, int err,
2648 union map_info *map_context)
2650 unsigned long flags;
2651 struct dm_thin_endio_hook *h = map_context->ptr;
2652 struct list_head work;
2653 struct dm_thin_new_mapping *m, *tmp;
2654 struct pool *pool = h->tc->pool;
2656 if (h->shared_read_entry) {
2657 INIT_LIST_HEAD(&work);
2658 dm_deferred_entry_dec(h->shared_read_entry, &work);
2660 spin_lock_irqsave(&pool->lock, flags);
2661 list_for_each_entry_safe(m, tmp, &work, list) {
2664 __maybe_add_mapping(m);
2666 spin_unlock_irqrestore(&pool->lock, flags);
2669 if (h->all_io_entry) {
2670 INIT_LIST_HEAD(&work);
2671 dm_deferred_entry_dec(h->all_io_entry, &work);
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);
2681 mempool_free(h, pool->endio_hook_pool);
2686 static void thin_postsuspend(struct dm_target *ti)
2688 if (dm_noflush_suspending(ti))
2689 requeue_io((struct thin_c *)ti->private);
2693 * <nr mapped sectors> <highest mapped sector>
2695 static int thin_status(struct dm_target *ti, status_type_t type,
2696 unsigned status_flags, char *result, unsigned maxlen)
2700 dm_block_t mapped, highest;
2701 char buf[BDEVNAME_SIZE];
2702 struct thin_c *tc = ti->private;
2704 if (get_pool_mode(tc->pool) == PM_FAIL) {
2713 case STATUSTYPE_INFO:
2714 r = dm_thin_get_mapped_count(tc->td, &mapped);
2718 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
2722 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
2724 DMEMIT("%llu", ((highest + 1) *
2725 tc->pool->sectors_per_block) - 1);
2730 case STATUSTYPE_TABLE:
2732 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
2733 (unsigned long) tc->dev_id);
2735 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
2743 static int thin_iterate_devices(struct dm_target *ti,
2744 iterate_devices_callout_fn fn, void *data)
2747 struct thin_c *tc = ti->private;
2748 struct pool *pool = tc->pool;
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.
2755 return 0; /* nothing is bound */
2757 blocks = pool->ti->len;
2758 (void) sector_div(blocks, pool->sectors_per_block);
2760 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
2766 * A thin device always inherits its queue limits from its pool.
2768 static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
2770 struct thin_c *tc = ti->private;
2772 *limits = bdev_get_queue(tc->pool_dev->bdev)->limits;
2775 static struct target_type thin_target = {
2777 .version = {1, 5, 0},
2778 .module = THIS_MODULE,
2782 .end_io = thin_endio,
2783 .postsuspend = thin_postsuspend,
2784 .status = thin_status,
2785 .iterate_devices = thin_iterate_devices,
2786 .io_hints = thin_io_hints,
2789 /*----------------------------------------------------------------*/
2791 static int __init dm_thin_init(void)
2797 r = dm_register_target(&thin_target);
2801 r = dm_register_target(&pool_target);
2803 goto bad_pool_target;
2807 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
2808 if (!_new_mapping_cache)
2809 goto bad_new_mapping_cache;
2811 _endio_hook_cache = KMEM_CACHE(dm_thin_endio_hook, 0);
2812 if (!_endio_hook_cache)
2813 goto bad_endio_hook_cache;
2817 bad_endio_hook_cache:
2818 kmem_cache_destroy(_new_mapping_cache);
2819 bad_new_mapping_cache:
2820 dm_unregister_target(&pool_target);
2822 dm_unregister_target(&thin_target);
2827 static void dm_thin_exit(void)
2829 dm_unregister_target(&thin_target);
2830 dm_unregister_target(&pool_target);
2832 kmem_cache_destroy(_new_mapping_cache);
2833 kmem_cache_destroy(_endio_hook_cache);
2836 module_init(dm_thin_init);
2837 module_exit(dm_thin_exit);
2839 MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
2840 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2841 MODULE_LICENSE("GPL");