2 * Copyright (C) 2011 Red Hat UK.
4 * This file is released under the GPL.
7 #include "dm-thin-metadata.h"
9 #include <linux/device-mapper.h>
10 #include <linux/dm-io.h>
11 #include <linux/dm-kcopyd.h>
12 #include <linux/list.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #define DM_MSG_PREFIX "thin"
22 #define ENDIO_HOOK_POOL_SIZE 10240
23 #define DEFERRED_SET_SIZE 64
24 #define MAPPING_POOL_SIZE 1024
25 #define PRISON_CELLS 1024
28 * The block size of the device holding pool data must be
29 * between 64KB and 1GB.
31 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
32 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
35 * The metadata device is currently limited in size. The limitation is
36 * checked lower down in dm-space-map-metadata, but we also check it here
37 * so we can fail early.
39 * We have one block of index, which can hold 255 index entries. Each
40 * index entry contains allocation info about 16k metadata blocks.
42 #define METADATA_DEV_MAX_SECTORS (255 * (1 << 14) * (THIN_METADATA_BLOCK_SIZE / (1 << SECTOR_SHIFT)))
45 * Device id is restricted to 24 bits.
47 #define MAX_DEV_ID ((1 << 24) - 1)
50 * How do we handle breaking sharing of data blocks?
51 * =================================================
53 * We use a standard copy-on-write btree to store the mappings for the
54 * devices (note I'm talking about copy-on-write of the metadata here, not
55 * the data). When you take an internal snapshot you clone the root node
56 * of the origin btree. After this there is no concept of an origin or a
57 * snapshot. They are just two device trees that happen to point to the
60 * When we get a write in we decide if it's to a shared data block using
61 * some timestamp magic. If it is, we have to break sharing.
63 * Let's say we write to a shared block in what was the origin. The
66 * i) plug io further to this physical block. (see bio_prison code).
68 * ii) quiesce any read io to that shared data block. Obviously
69 * including all devices that share this block. (see deferred_set code)
71 * iii) copy the data block to a newly allocate block. This step can be
72 * missed out if the io covers the block. (schedule_copy).
74 * iv) insert the new mapping into the origin's btree
75 * (process_prepared_mappings). This act of inserting breaks some
76 * sharing of btree nodes between the two devices. Breaking sharing only
77 * effects the btree of that specific device. Btrees for the other
78 * devices that share the block never change. The btree for the origin
79 * device as it was after the last commit is untouched, ie. we're using
80 * persistent data structures in the functional programming sense.
82 * v) unplug io to this physical block, including the io that triggered
83 * the breaking of sharing.
85 * Steps (ii) and (iii) occur in parallel.
87 * The metadata _doesn't_ need to be committed before the io continues. We
88 * get away with this because the io is always written to a _new_ block.
89 * If there's a crash, then:
91 * - The origin mapping will point to the old origin block (the shared
92 * one). This will contain the data as it was before the io that triggered
93 * the breaking of sharing came in.
95 * - The snap mapping still points to the old block. As it would after
98 * The downside of this scheme is the timestamp magic isn't perfect, and
99 * will continue to think that data block in the snapshot device is shared
100 * even after the write to the origin has broken sharing. I suspect data
101 * blocks will typically be shared by many different devices, so we're
102 * breaking sharing n + 1 times, rather than n, where n is the number of
103 * devices that reference this data block. At the moment I think the
104 * benefits far, far outweigh the disadvantages.
107 /*----------------------------------------------------------------*/
110 * Sometimes we can't deal with a bio straight away. We put them in prison
111 * where they can't cause any mischief. Bios are put in a cell identified
112 * by a key, multiple bios can be in the same cell. When the cell is
113 * subsequently unlocked the bios become available.
124 struct hlist_node list;
125 struct bio_prison *prison;
128 struct bio_list bios;
133 mempool_t *cell_pool;
137 struct hlist_head *cells;
140 static uint32_t calc_nr_buckets(unsigned nr_cells)
145 nr_cells = min(nr_cells, 8192u);
154 * @nr_cells should be the number of cells you want in use _concurrently_.
155 * Don't confuse it with the number of distinct keys.
157 static struct bio_prison *prison_create(unsigned nr_cells)
160 uint32_t nr_buckets = calc_nr_buckets(nr_cells);
161 size_t len = sizeof(struct bio_prison) +
162 (sizeof(struct hlist_head) * nr_buckets);
163 struct bio_prison *prison = kmalloc(len, GFP_KERNEL);
168 spin_lock_init(&prison->lock);
169 prison->cell_pool = mempool_create_kmalloc_pool(nr_cells,
170 sizeof(struct cell));
171 if (!prison->cell_pool) {
176 prison->nr_buckets = nr_buckets;
177 prison->hash_mask = nr_buckets - 1;
178 prison->cells = (struct hlist_head *) (prison + 1);
179 for (i = 0; i < nr_buckets; i++)
180 INIT_HLIST_HEAD(prison->cells + i);
185 static void prison_destroy(struct bio_prison *prison)
187 mempool_destroy(prison->cell_pool);
191 static uint32_t hash_key(struct bio_prison *prison, struct cell_key *key)
193 const unsigned long BIG_PRIME = 4294967291UL;
194 uint64_t hash = key->block * BIG_PRIME;
196 return (uint32_t) (hash & prison->hash_mask);
199 static int keys_equal(struct cell_key *lhs, struct cell_key *rhs)
201 return (lhs->virtual == rhs->virtual) &&
202 (lhs->dev == rhs->dev) &&
203 (lhs->block == rhs->block);
206 static struct cell *__search_bucket(struct hlist_head *bucket,
207 struct cell_key *key)
210 struct hlist_node *tmp;
212 hlist_for_each_entry(cell, tmp, bucket, list)
213 if (keys_equal(&cell->key, key))
220 * This may block if a new cell needs allocating. You must ensure that
221 * cells will be unlocked even if the calling thread is blocked.
223 * Returns 1 if the cell was already held, 0 if @inmate is the new holder.
225 static int bio_detain(struct bio_prison *prison, struct cell_key *key,
226 struct bio *inmate, struct cell **ref)
230 uint32_t hash = hash_key(prison, key);
231 struct cell *cell, *cell2;
233 BUG_ON(hash > prison->nr_buckets);
235 spin_lock_irqsave(&prison->lock, flags);
237 cell = __search_bucket(prison->cells + hash, key);
239 bio_list_add(&cell->bios, inmate);
244 * Allocate a new cell
246 spin_unlock_irqrestore(&prison->lock, flags);
247 cell2 = mempool_alloc(prison->cell_pool, GFP_NOIO);
248 spin_lock_irqsave(&prison->lock, flags);
251 * We've been unlocked, so we have to double check that
252 * nobody else has inserted this cell in the meantime.
254 cell = __search_bucket(prison->cells + hash, key);
256 mempool_free(cell2, prison->cell_pool);
257 bio_list_add(&cell->bios, inmate);
266 cell->prison = prison;
267 memcpy(&cell->key, key, sizeof(cell->key));
268 cell->holder = inmate;
269 bio_list_init(&cell->bios);
270 hlist_add_head(&cell->list, prison->cells + hash);
275 spin_unlock_irqrestore(&prison->lock, flags);
283 * @inmates must have been initialised prior to this call
285 static void __cell_release(struct cell *cell, struct bio_list *inmates)
287 struct bio_prison *prison = cell->prison;
289 hlist_del(&cell->list);
292 bio_list_add(inmates, cell->holder);
293 bio_list_merge(inmates, &cell->bios);
296 mempool_free(cell, prison->cell_pool);
299 static void cell_release(struct cell *cell, struct bio_list *bios)
302 struct bio_prison *prison = cell->prison;
304 spin_lock_irqsave(&prison->lock, flags);
305 __cell_release(cell, bios);
306 spin_unlock_irqrestore(&prison->lock, flags);
310 * There are a couple of places where we put a bio into a cell briefly
311 * before taking it out again. In these situations we know that no other
312 * bio may be in the cell. This function releases the cell, and also does
315 static void __cell_release_singleton(struct cell *cell, struct bio *bio)
317 BUG_ON(cell->holder != bio);
318 BUG_ON(!bio_list_empty(&cell->bios));
320 __cell_release(cell, NULL);
323 static void cell_release_singleton(struct cell *cell, struct bio *bio)
326 struct bio_prison *prison = cell->prison;
328 spin_lock_irqsave(&prison->lock, flags);
329 __cell_release_singleton(cell, bio);
330 spin_unlock_irqrestore(&prison->lock, flags);
334 * Sometimes we don't want the holder, just the additional bios.
336 static void __cell_release_no_holder(struct cell *cell, struct bio_list *inmates)
338 struct bio_prison *prison = cell->prison;
340 hlist_del(&cell->list);
341 bio_list_merge(inmates, &cell->bios);
343 mempool_free(cell, prison->cell_pool);
346 static void cell_release_no_holder(struct cell *cell, struct bio_list *inmates)
349 struct bio_prison *prison = cell->prison;
351 spin_lock_irqsave(&prison->lock, flags);
352 __cell_release_no_holder(cell, inmates);
353 spin_unlock_irqrestore(&prison->lock, flags);
356 static void cell_error(struct cell *cell)
358 struct bio_prison *prison = cell->prison;
359 struct bio_list bios;
363 bio_list_init(&bios);
365 spin_lock_irqsave(&prison->lock, flags);
366 __cell_release(cell, &bios);
367 spin_unlock_irqrestore(&prison->lock, flags);
369 while ((bio = bio_list_pop(&bios)))
373 /*----------------------------------------------------------------*/
376 * We use the deferred set to keep track of pending reads to shared blocks.
377 * We do this to ensure the new mapping caused by a write isn't performed
378 * until these prior reads have completed. Otherwise the insertion of the
379 * new mapping could free the old block that the read bios are mapped to.
383 struct deferred_entry {
384 struct deferred_set *ds;
386 struct list_head work_items;
389 struct deferred_set {
391 unsigned current_entry;
393 struct deferred_entry entries[DEFERRED_SET_SIZE];
396 static void ds_init(struct deferred_set *ds)
400 spin_lock_init(&ds->lock);
401 ds->current_entry = 0;
403 for (i = 0; i < DEFERRED_SET_SIZE; i++) {
404 ds->entries[i].ds = ds;
405 ds->entries[i].count = 0;
406 INIT_LIST_HEAD(&ds->entries[i].work_items);
410 static struct deferred_entry *ds_inc(struct deferred_set *ds)
413 struct deferred_entry *entry;
415 spin_lock_irqsave(&ds->lock, flags);
416 entry = ds->entries + ds->current_entry;
418 spin_unlock_irqrestore(&ds->lock, flags);
423 static unsigned ds_next(unsigned index)
425 return (index + 1) % DEFERRED_SET_SIZE;
428 static void __sweep(struct deferred_set *ds, struct list_head *head)
430 while ((ds->sweeper != ds->current_entry) &&
431 !ds->entries[ds->sweeper].count) {
432 list_splice_init(&ds->entries[ds->sweeper].work_items, head);
433 ds->sweeper = ds_next(ds->sweeper);
436 if ((ds->sweeper == ds->current_entry) && !ds->entries[ds->sweeper].count)
437 list_splice_init(&ds->entries[ds->sweeper].work_items, head);
440 static void ds_dec(struct deferred_entry *entry, struct list_head *head)
444 spin_lock_irqsave(&entry->ds->lock, flags);
445 BUG_ON(!entry->count);
447 __sweep(entry->ds, head);
448 spin_unlock_irqrestore(&entry->ds->lock, flags);
452 * Returns 1 if deferred or 0 if no pending items to delay job.
454 static int ds_add_work(struct deferred_set *ds, struct list_head *work)
460 spin_lock_irqsave(&ds->lock, flags);
461 if ((ds->sweeper == ds->current_entry) &&
462 !ds->entries[ds->current_entry].count)
465 list_add(work, &ds->entries[ds->current_entry].work_items);
466 next_entry = ds_next(ds->current_entry);
467 if (!ds->entries[next_entry].count)
468 ds->current_entry = next_entry;
470 spin_unlock_irqrestore(&ds->lock, flags);
475 /*----------------------------------------------------------------*/
480 static void build_data_key(struct dm_thin_device *td,
481 dm_block_t b, struct cell_key *key)
484 key->dev = dm_thin_dev_id(td);
488 static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
489 struct cell_key *key)
492 key->dev = dm_thin_dev_id(td);
496 /*----------------------------------------------------------------*/
499 * A pool device ties together a metadata device and a data device. It
500 * also provides the interface for creating and destroying internal
505 struct list_head list;
506 struct dm_target *ti; /* Only set if a pool target is bound */
508 struct mapped_device *pool_md;
509 struct block_device *md_dev;
510 struct dm_pool_metadata *pmd;
512 uint32_t sectors_per_block;
513 unsigned block_shift;
514 dm_block_t offset_mask;
515 dm_block_t low_water_blocks;
517 unsigned zero_new_blocks:1;
518 unsigned low_water_triggered:1; /* A dm event has been sent */
519 unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
521 struct bio_prison *prison;
522 struct dm_kcopyd_client *copier;
524 struct workqueue_struct *wq;
525 struct work_struct worker;
530 struct bio_list deferred_bios;
531 struct bio_list deferred_flush_bios;
532 struct list_head prepared_mappings;
534 struct bio_list retry_on_resume_list;
536 struct deferred_set ds; /* FIXME: move to thin_c */
538 struct new_mapping *next_mapping;
539 mempool_t *mapping_pool;
540 mempool_t *endio_hook_pool;
544 * Target context for a pool.
547 struct dm_target *ti;
549 struct dm_dev *data_dev;
550 struct dm_dev *metadata_dev;
551 struct dm_target_callbacks callbacks;
553 dm_block_t low_water_blocks;
554 unsigned zero_new_blocks:1;
558 * Target context for a thin.
561 struct dm_dev *pool_dev;
565 struct dm_thin_device *td;
568 /*----------------------------------------------------------------*/
571 * A global list of pools that uses a struct mapped_device as a key.
573 static struct dm_thin_pool_table {
575 struct list_head pools;
576 } dm_thin_pool_table;
578 static void pool_table_init(void)
580 mutex_init(&dm_thin_pool_table.mutex);
581 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
584 static void __pool_table_insert(struct pool *pool)
586 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
587 list_add(&pool->list, &dm_thin_pool_table.pools);
590 static void __pool_table_remove(struct pool *pool)
592 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
593 list_del(&pool->list);
596 static struct pool *__pool_table_lookup(struct mapped_device *md)
598 struct pool *pool = NULL, *tmp;
600 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
602 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
603 if (tmp->pool_md == md) {
612 static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
614 struct pool *pool = NULL, *tmp;
616 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
618 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
619 if (tmp->md_dev == md_dev) {
628 /*----------------------------------------------------------------*/
630 static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
633 struct bio_list bios;
635 bio_list_init(&bios);
636 bio_list_merge(&bios, master);
637 bio_list_init(master);
639 while ((bio = bio_list_pop(&bios))) {
640 if (dm_get_mapinfo(bio)->ptr == tc)
641 bio_endio(bio, DM_ENDIO_REQUEUE);
643 bio_list_add(master, bio);
647 static void requeue_io(struct thin_c *tc)
649 struct pool *pool = tc->pool;
652 spin_lock_irqsave(&pool->lock, flags);
653 __requeue_bio_list(tc, &pool->deferred_bios);
654 __requeue_bio_list(tc, &pool->retry_on_resume_list);
655 spin_unlock_irqrestore(&pool->lock, flags);
659 * This section of code contains the logic for processing a thin device's IO.
660 * Much of the code depends on pool object resources (lists, workqueues, etc)
661 * but most is exclusively called from the thin target rather than the thin-pool
665 static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
667 return bio->bi_sector >> tc->pool->block_shift;
670 static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
672 struct pool *pool = tc->pool;
674 bio->bi_bdev = tc->pool_dev->bdev;
675 bio->bi_sector = (block << pool->block_shift) +
676 (bio->bi_sector & pool->offset_mask);
679 static void remap_and_issue(struct thin_c *tc, struct bio *bio,
682 struct pool *pool = tc->pool;
685 remap(tc, bio, block);
688 * Batch together any FUA/FLUSH bios we find and then issue
689 * a single commit for them in process_deferred_bios().
691 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
692 spin_lock_irqsave(&pool->lock, flags);
693 bio_list_add(&pool->deferred_flush_bios, bio);
694 spin_unlock_irqrestore(&pool->lock, flags);
696 generic_make_request(bio);
700 * wake_worker() is used when new work is queued and when pool_resume is
701 * ready to continue deferred IO processing.
703 static void wake_worker(struct pool *pool)
705 queue_work(pool->wq, &pool->worker);
708 /*----------------------------------------------------------------*/
711 * Bio endio functions.
715 bio_end_io_t *saved_bi_end_io;
716 struct deferred_entry *entry;
720 struct list_head list;
725 dm_block_t virt_block;
726 dm_block_t data_block;
731 * If the bio covers the whole area of a block then we can avoid
732 * zeroing or copying. Instead this bio is hooked. The bio will
733 * still be in the cell, so care has to be taken to avoid issuing
737 bio_end_io_t *saved_bi_end_io;
740 static void __maybe_add_mapping(struct new_mapping *m)
742 struct pool *pool = m->tc->pool;
744 if (list_empty(&m->list) && m->prepared) {
745 list_add(&m->list, &pool->prepared_mappings);
750 static void copy_complete(int read_err, unsigned long write_err, void *context)
753 struct new_mapping *m = context;
754 struct pool *pool = m->tc->pool;
756 m->err = read_err || write_err ? -EIO : 0;
758 spin_lock_irqsave(&pool->lock, flags);
760 __maybe_add_mapping(m);
761 spin_unlock_irqrestore(&pool->lock, flags);
764 static void overwrite_endio(struct bio *bio, int err)
767 struct new_mapping *m = dm_get_mapinfo(bio)->ptr;
768 struct pool *pool = m->tc->pool;
772 spin_lock_irqsave(&pool->lock, flags);
774 __maybe_add_mapping(m);
775 spin_unlock_irqrestore(&pool->lock, flags);
778 static void shared_read_endio(struct bio *bio, int err)
780 struct list_head mappings;
781 struct new_mapping *m, *tmp;
782 struct endio_hook *h = dm_get_mapinfo(bio)->ptr;
784 struct pool *pool = h->tc->pool;
786 bio->bi_end_io = h->saved_bi_end_io;
789 INIT_LIST_HEAD(&mappings);
790 ds_dec(h->entry, &mappings);
792 spin_lock_irqsave(&pool->lock, flags);
793 list_for_each_entry_safe(m, tmp, &mappings, list) {
795 INIT_LIST_HEAD(&m->list);
796 __maybe_add_mapping(m);
798 spin_unlock_irqrestore(&pool->lock, flags);
800 mempool_free(h, pool->endio_hook_pool);
803 /*----------------------------------------------------------------*/
810 * Prepared mapping jobs.
814 * This sends the bios in the cell back to the deferred_bios list.
816 static void cell_defer(struct thin_c *tc, struct cell *cell,
817 dm_block_t data_block)
819 struct pool *pool = tc->pool;
822 spin_lock_irqsave(&pool->lock, flags);
823 cell_release(cell, &pool->deferred_bios);
824 spin_unlock_irqrestore(&tc->pool->lock, flags);
830 * Same as cell_defer above, except it omits one particular detainee,
831 * a write bio that covers the block and has already been processed.
833 static void cell_defer_except(struct thin_c *tc, struct cell *cell)
835 struct bio_list bios;
836 struct pool *pool = tc->pool;
839 bio_list_init(&bios);
841 spin_lock_irqsave(&pool->lock, flags);
842 cell_release_no_holder(cell, &pool->deferred_bios);
843 spin_unlock_irqrestore(&pool->lock, flags);
848 static void process_prepared_mapping(struct new_mapping *m)
850 struct thin_c *tc = m->tc;
856 bio->bi_end_io = m->saved_bi_end_io;
864 * Commit the prepared block into the mapping btree.
865 * Any I/O for this block arriving after this point will get
866 * remapped to it directly.
868 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
870 DMERR("dm_thin_insert_block() failed");
876 * Release any bios held while the block was being provisioned.
877 * If we are processing a write bio that completely covers the block,
878 * we already processed it so can ignore it now when processing
879 * the bios in the cell.
882 cell_defer_except(tc, m->cell);
885 cell_defer(tc, m->cell, m->data_block);
888 mempool_free(m, tc->pool->mapping_pool);
891 static void process_prepared_mappings(struct pool *pool)
894 struct list_head maps;
895 struct new_mapping *m, *tmp;
897 INIT_LIST_HEAD(&maps);
898 spin_lock_irqsave(&pool->lock, flags);
899 list_splice_init(&pool->prepared_mappings, &maps);
900 spin_unlock_irqrestore(&pool->lock, flags);
902 list_for_each_entry_safe(m, tmp, &maps, list)
903 process_prepared_mapping(m);
909 static int io_overwrites_block(struct pool *pool, struct bio *bio)
911 return ((bio_data_dir(bio) == WRITE) &&
912 !(bio->bi_sector & pool->offset_mask)) &&
913 (bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT));
916 static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
919 *save = bio->bi_end_io;
923 static int ensure_next_mapping(struct pool *pool)
925 if (pool->next_mapping)
928 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
930 return pool->next_mapping ? 0 : -ENOMEM;
933 static struct new_mapping *get_next_mapping(struct pool *pool)
935 struct new_mapping *r = pool->next_mapping;
937 BUG_ON(!pool->next_mapping);
939 pool->next_mapping = NULL;
944 static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
945 dm_block_t data_origin, dm_block_t data_dest,
946 struct cell *cell, struct bio *bio)
949 struct pool *pool = tc->pool;
950 struct new_mapping *m = get_next_mapping(pool);
952 INIT_LIST_HEAD(&m->list);
955 m->virt_block = virt_block;
956 m->data_block = data_dest;
961 ds_add_work(&pool->ds, &m->list);
964 * IO to pool_dev remaps to the pool target's data_dev.
966 * If the whole block of data is being overwritten, we can issue the
967 * bio immediately. Otherwise we use kcopyd to clone the data first.
969 if (io_overwrites_block(pool, bio)) {
971 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
972 dm_get_mapinfo(bio)->ptr = m;
973 remap_and_issue(tc, bio, data_dest);
975 struct dm_io_region from, to;
977 from.bdev = tc->pool_dev->bdev;
978 from.sector = data_origin * pool->sectors_per_block;
979 from.count = pool->sectors_per_block;
981 to.bdev = tc->pool_dev->bdev;
982 to.sector = data_dest * pool->sectors_per_block;
983 to.count = pool->sectors_per_block;
985 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
986 0, copy_complete, m);
988 mempool_free(m, pool->mapping_pool);
989 DMERR("dm_kcopyd_copy() failed");
995 static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
996 dm_block_t data_block, struct cell *cell,
999 struct pool *pool = tc->pool;
1000 struct new_mapping *m = get_next_mapping(pool);
1002 INIT_LIST_HEAD(&m->list);
1005 m->virt_block = virt_block;
1006 m->data_block = data_block;
1012 * If the whole block of data is being overwritten or we are not
1013 * zeroing pre-existing data, we can issue the bio immediately.
1014 * Otherwise we use kcopyd to zero the data first.
1016 if (!pool->zero_new_blocks)
1017 process_prepared_mapping(m);
1019 else if (io_overwrites_block(pool, bio)) {
1021 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1022 dm_get_mapinfo(bio)->ptr = m;
1023 remap_and_issue(tc, bio, data_block);
1027 struct dm_io_region to;
1029 to.bdev = tc->pool_dev->bdev;
1030 to.sector = data_block * pool->sectors_per_block;
1031 to.count = pool->sectors_per_block;
1033 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
1035 mempool_free(m, pool->mapping_pool);
1036 DMERR("dm_kcopyd_zero() failed");
1042 static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1045 dm_block_t free_blocks;
1046 unsigned long flags;
1047 struct pool *pool = tc->pool;
1049 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1053 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1054 DMWARN("%s: reached low water mark, sending event.",
1055 dm_device_name(pool->pool_md));
1056 spin_lock_irqsave(&pool->lock, flags);
1057 pool->low_water_triggered = 1;
1058 spin_unlock_irqrestore(&pool->lock, flags);
1059 dm_table_event(pool->ti->table);
1063 if (pool->no_free_space)
1067 * Try to commit to see if that will free up some
1070 r = dm_pool_commit_metadata(pool->pmd);
1072 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
1077 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1082 * If we still have no space we set a flag to avoid
1083 * doing all this checking and return -ENOSPC.
1086 DMWARN("%s: no free space available.",
1087 dm_device_name(pool->pool_md));
1088 spin_lock_irqsave(&pool->lock, flags);
1089 pool->no_free_space = 1;
1090 spin_unlock_irqrestore(&pool->lock, flags);
1096 r = dm_pool_alloc_data_block(pool->pmd, result);
1104 * If we have run out of space, queue bios until the device is
1105 * resumed, presumably after having been reloaded with more space.
1107 static void retry_on_resume(struct bio *bio)
1109 struct thin_c *tc = dm_get_mapinfo(bio)->ptr;
1110 struct pool *pool = tc->pool;
1111 unsigned long flags;
1113 spin_lock_irqsave(&pool->lock, flags);
1114 bio_list_add(&pool->retry_on_resume_list, bio);
1115 spin_unlock_irqrestore(&pool->lock, flags);
1118 static void no_space(struct cell *cell)
1121 struct bio_list bios;
1123 bio_list_init(&bios);
1124 cell_release(cell, &bios);
1126 while ((bio = bio_list_pop(&bios)))
1127 retry_on_resume(bio);
1130 static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
1131 struct cell_key *key,
1132 struct dm_thin_lookup_result *lookup_result,
1136 dm_block_t data_block;
1138 r = alloc_data_block(tc, &data_block);
1141 schedule_copy(tc, block, lookup_result->block,
1142 data_block, cell, bio);
1150 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
1156 static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1158 struct dm_thin_lookup_result *lookup_result)
1161 struct pool *pool = tc->pool;
1162 struct cell_key key;
1165 * If cell is already occupied, then sharing is already in the process
1166 * of being broken so we have nothing further to do here.
1168 build_data_key(tc->td, lookup_result->block, &key);
1169 if (bio_detain(pool->prison, &key, bio, &cell))
1172 if (bio_data_dir(bio) == WRITE)
1173 break_sharing(tc, bio, block, &key, lookup_result, cell);
1175 struct endio_hook *h;
1176 h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
1179 h->entry = ds_inc(&pool->ds);
1180 save_and_set_endio(bio, &h->saved_bi_end_io, shared_read_endio);
1181 dm_get_mapinfo(bio)->ptr = h;
1183 cell_release_singleton(cell, bio);
1184 remap_and_issue(tc, bio, lookup_result->block);
1188 static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
1192 dm_block_t data_block;
1195 * Remap empty bios (flushes) immediately, without provisioning.
1197 if (!bio->bi_size) {
1198 cell_release_singleton(cell, bio);
1199 remap_and_issue(tc, bio, 0);
1204 * Fill read bios with zeroes and complete them immediately.
1206 if (bio_data_dir(bio) == READ) {
1208 cell_release_singleton(cell, bio);
1213 r = alloc_data_block(tc, &data_block);
1216 schedule_zero(tc, block, data_block, cell, bio);
1224 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
1230 static void process_bio(struct thin_c *tc, struct bio *bio)
1233 dm_block_t block = get_bio_block(tc, bio);
1235 struct cell_key key;
1236 struct dm_thin_lookup_result lookup_result;
1239 * If cell is already occupied, then the block is already
1240 * being provisioned so we have nothing further to do here.
1242 build_virtual_key(tc->td, block, &key);
1243 if (bio_detain(tc->pool->prison, &key, bio, &cell))
1246 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1250 * We can release this cell now. This thread is the only
1251 * one that puts bios into a cell, and we know there were
1252 * no preceding bios.
1255 * TODO: this will probably have to change when discard goes
1258 cell_release_singleton(cell, bio);
1260 if (lookup_result.shared)
1261 process_shared_bio(tc, bio, block, &lookup_result);
1263 remap_and_issue(tc, bio, lookup_result.block);
1267 provision_block(tc, bio, block, cell);
1271 DMERR("dm_thin_find_block() failed, error = %d", r);
1277 static void process_deferred_bios(struct pool *pool)
1279 unsigned long flags;
1281 struct bio_list bios;
1284 bio_list_init(&bios);
1286 spin_lock_irqsave(&pool->lock, flags);
1287 bio_list_merge(&bios, &pool->deferred_bios);
1288 bio_list_init(&pool->deferred_bios);
1289 spin_unlock_irqrestore(&pool->lock, flags);
1291 while ((bio = bio_list_pop(&bios))) {
1292 struct thin_c *tc = dm_get_mapinfo(bio)->ptr;
1294 * If we've got no free new_mapping structs, and processing
1295 * this bio might require one, we pause until there are some
1296 * prepared mappings to process.
1298 if (ensure_next_mapping(pool)) {
1299 spin_lock_irqsave(&pool->lock, flags);
1300 bio_list_merge(&pool->deferred_bios, &bios);
1301 spin_unlock_irqrestore(&pool->lock, flags);
1305 process_bio(tc, bio);
1309 * If there are any deferred flush bios, we must commit
1310 * the metadata before issuing them.
1312 bio_list_init(&bios);
1313 spin_lock_irqsave(&pool->lock, flags);
1314 bio_list_merge(&bios, &pool->deferred_flush_bios);
1315 bio_list_init(&pool->deferred_flush_bios);
1316 spin_unlock_irqrestore(&pool->lock, flags);
1318 if (bio_list_empty(&bios))
1321 r = dm_pool_commit_metadata(pool->pmd);
1323 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
1325 while ((bio = bio_list_pop(&bios)))
1330 while ((bio = bio_list_pop(&bios)))
1331 generic_make_request(bio);
1334 static void do_worker(struct work_struct *ws)
1336 struct pool *pool = container_of(ws, struct pool, worker);
1338 process_prepared_mappings(pool);
1339 process_deferred_bios(pool);
1342 /*----------------------------------------------------------------*/
1345 * Mapping functions.
1349 * Called only while mapping a thin bio to hand it over to the workqueue.
1351 static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1353 unsigned long flags;
1354 struct pool *pool = tc->pool;
1356 spin_lock_irqsave(&pool->lock, flags);
1357 bio_list_add(&pool->deferred_bios, bio);
1358 spin_unlock_irqrestore(&pool->lock, flags);
1364 * Non-blocking function called from the thin target's map function.
1366 static int thin_bio_map(struct dm_target *ti, struct bio *bio,
1367 union map_info *map_context)
1370 struct thin_c *tc = ti->private;
1371 dm_block_t block = get_bio_block(tc, bio);
1372 struct dm_thin_device *td = tc->td;
1373 struct dm_thin_lookup_result result;
1376 * Save the thin context for easy access from the deferred bio later.
1378 map_context->ptr = tc;
1380 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
1381 thin_defer_bio(tc, bio);
1382 return DM_MAPIO_SUBMITTED;
1385 r = dm_thin_find_block(td, block, 0, &result);
1388 * Note that we defer readahead too.
1392 if (unlikely(result.shared)) {
1394 * We have a race condition here between the
1395 * result.shared value returned by the lookup and
1396 * snapshot creation, which may cause new
1399 * To avoid this always quiesce the origin before
1400 * taking the snap. You want to do this anyway to
1401 * ensure a consistent application view
1404 * More distant ancestors are irrelevant. The
1405 * shared flag will be set in their case.
1407 thin_defer_bio(tc, bio);
1408 r = DM_MAPIO_SUBMITTED;
1410 remap(tc, bio, result.block);
1411 r = DM_MAPIO_REMAPPED;
1417 * In future, the failed dm_thin_find_block above could
1418 * provide the hint to load the metadata into cache.
1421 thin_defer_bio(tc, bio);
1422 r = DM_MAPIO_SUBMITTED;
1429 static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1432 unsigned long flags;
1433 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
1435 spin_lock_irqsave(&pt->pool->lock, flags);
1436 r = !bio_list_empty(&pt->pool->retry_on_resume_list);
1437 spin_unlock_irqrestore(&pt->pool->lock, flags);
1440 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1441 r = bdi_congested(&q->backing_dev_info, bdi_bits);
1447 static void __requeue_bios(struct pool *pool)
1449 bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
1450 bio_list_init(&pool->retry_on_resume_list);
1453 /*----------------------------------------------------------------
1454 * Binding of control targets to a pool object
1455 *--------------------------------------------------------------*/
1456 static int bind_control_target(struct pool *pool, struct dm_target *ti)
1458 struct pool_c *pt = ti->private;
1461 pool->low_water_blocks = pt->low_water_blocks;
1462 pool->zero_new_blocks = pt->zero_new_blocks;
1467 static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1473 /*----------------------------------------------------------------
1475 *--------------------------------------------------------------*/
1476 static void __pool_destroy(struct pool *pool)
1478 __pool_table_remove(pool);
1480 if (dm_pool_metadata_close(pool->pmd) < 0)
1481 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1483 prison_destroy(pool->prison);
1484 dm_kcopyd_client_destroy(pool->copier);
1487 destroy_workqueue(pool->wq);
1489 if (pool->next_mapping)
1490 mempool_free(pool->next_mapping, pool->mapping_pool);
1491 mempool_destroy(pool->mapping_pool);
1492 mempool_destroy(pool->endio_hook_pool);
1496 static struct pool *pool_create(struct mapped_device *pool_md,
1497 struct block_device *metadata_dev,
1498 unsigned long block_size, char **error)
1503 struct dm_pool_metadata *pmd;
1505 pmd = dm_pool_metadata_open(metadata_dev, block_size);
1507 *error = "Error creating metadata object";
1508 return (struct pool *)pmd;
1511 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
1513 *error = "Error allocating memory for pool";
1514 err_p = ERR_PTR(-ENOMEM);
1519 pool->sectors_per_block = block_size;
1520 pool->block_shift = ffs(block_size) - 1;
1521 pool->offset_mask = block_size - 1;
1522 pool->low_water_blocks = 0;
1523 pool->zero_new_blocks = 1;
1524 pool->prison = prison_create(PRISON_CELLS);
1525 if (!pool->prison) {
1526 *error = "Error creating pool's bio prison";
1527 err_p = ERR_PTR(-ENOMEM);
1531 pool->copier = dm_kcopyd_client_create();
1532 if (IS_ERR(pool->copier)) {
1533 r = PTR_ERR(pool->copier);
1534 *error = "Error creating pool's kcopyd client";
1536 goto bad_kcopyd_client;
1540 * Create singlethreaded workqueue that will service all devices
1541 * that use this metadata.
1543 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1545 *error = "Error creating pool's workqueue";
1546 err_p = ERR_PTR(-ENOMEM);
1550 INIT_WORK(&pool->worker, do_worker);
1551 spin_lock_init(&pool->lock);
1552 bio_list_init(&pool->deferred_bios);
1553 bio_list_init(&pool->deferred_flush_bios);
1554 INIT_LIST_HEAD(&pool->prepared_mappings);
1555 pool->low_water_triggered = 0;
1556 pool->no_free_space = 0;
1557 bio_list_init(&pool->retry_on_resume_list);
1560 pool->next_mapping = NULL;
1561 pool->mapping_pool =
1562 mempool_create_kmalloc_pool(MAPPING_POOL_SIZE, sizeof(struct new_mapping));
1563 if (!pool->mapping_pool) {
1564 *error = "Error creating pool's mapping mempool";
1565 err_p = ERR_PTR(-ENOMEM);
1566 goto bad_mapping_pool;
1569 pool->endio_hook_pool =
1570 mempool_create_kmalloc_pool(ENDIO_HOOK_POOL_SIZE, sizeof(struct endio_hook));
1571 if (!pool->endio_hook_pool) {
1572 *error = "Error creating pool's endio_hook mempool";
1573 err_p = ERR_PTR(-ENOMEM);
1574 goto bad_endio_hook_pool;
1576 pool->ref_count = 1;
1577 pool->pool_md = pool_md;
1578 pool->md_dev = metadata_dev;
1579 __pool_table_insert(pool);
1583 bad_endio_hook_pool:
1584 mempool_destroy(pool->mapping_pool);
1586 destroy_workqueue(pool->wq);
1588 dm_kcopyd_client_destroy(pool->copier);
1590 prison_destroy(pool->prison);
1594 if (dm_pool_metadata_close(pmd))
1595 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1600 static void __pool_inc(struct pool *pool)
1602 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1606 static void __pool_dec(struct pool *pool)
1608 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1609 BUG_ON(!pool->ref_count);
1610 if (!--pool->ref_count)
1611 __pool_destroy(pool);
1614 static struct pool *__pool_find(struct mapped_device *pool_md,
1615 struct block_device *metadata_dev,
1616 unsigned long block_size, char **error)
1618 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
1621 if (pool->pool_md != pool_md)
1622 return ERR_PTR(-EBUSY);
1626 pool = __pool_table_lookup(pool_md);
1628 if (pool->md_dev != metadata_dev)
1629 return ERR_PTR(-EINVAL);
1633 pool = pool_create(pool_md, metadata_dev, block_size, error);
1639 /*----------------------------------------------------------------
1640 * Pool target methods
1641 *--------------------------------------------------------------*/
1642 static void pool_dtr(struct dm_target *ti)
1644 struct pool_c *pt = ti->private;
1646 mutex_lock(&dm_thin_pool_table.mutex);
1648 unbind_control_target(pt->pool, ti);
1649 __pool_dec(pt->pool);
1650 dm_put_device(ti, pt->metadata_dev);
1651 dm_put_device(ti, pt->data_dev);
1654 mutex_unlock(&dm_thin_pool_table.mutex);
1657 struct pool_features {
1658 unsigned zero_new_blocks:1;
1661 static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
1662 struct dm_target *ti)
1666 const char *arg_name;
1668 static struct dm_arg _args[] = {
1669 {0, 1, "Invalid number of pool feature arguments"},
1673 * No feature arguments supplied.
1678 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1682 while (argc && !r) {
1683 arg_name = dm_shift_arg(as);
1686 if (!strcasecmp(arg_name, "skip_block_zeroing")) {
1687 pf->zero_new_blocks = 0;
1691 ti->error = "Unrecognised pool feature requested";
1699 * thin-pool <metadata dev> <data dev>
1700 * <data block size (sectors)>
1701 * <low water mark (blocks)>
1702 * [<#feature args> [<arg>]*]
1704 * Optional feature arguments are:
1705 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
1707 static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
1712 struct pool_features pf;
1713 struct dm_arg_set as;
1714 struct dm_dev *data_dev;
1715 unsigned long block_size;
1716 dm_block_t low_water_blocks;
1717 struct dm_dev *metadata_dev;
1718 sector_t metadata_dev_size;
1721 * FIXME Remove validation from scope of lock.
1723 mutex_lock(&dm_thin_pool_table.mutex);
1726 ti->error = "Invalid argument count";
1733 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
1735 ti->error = "Error opening metadata block device";
1739 metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
1740 if (metadata_dev_size > METADATA_DEV_MAX_SECTORS) {
1741 ti->error = "Metadata device is too large";
1746 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
1748 ti->error = "Error getting data device";
1752 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
1753 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1754 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
1755 !is_power_of_2(block_size)) {
1756 ti->error = "Invalid block size";
1761 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
1762 ti->error = "Invalid low water mark";
1768 * Set default pool features.
1770 memset(&pf, 0, sizeof(pf));
1771 pf.zero_new_blocks = 1;
1773 dm_consume_args(&as, 4);
1774 r = parse_pool_features(&as, &pf, ti);
1778 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
1784 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
1785 block_size, &ti->error);
1793 pt->metadata_dev = metadata_dev;
1794 pt->data_dev = data_dev;
1795 pt->low_water_blocks = low_water_blocks;
1796 pt->zero_new_blocks = pf.zero_new_blocks;
1797 ti->num_flush_requests = 1;
1798 ti->num_discard_requests = 0;
1801 pt->callbacks.congested_fn = pool_is_congested;
1802 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
1804 mutex_unlock(&dm_thin_pool_table.mutex);
1811 dm_put_device(ti, data_dev);
1813 dm_put_device(ti, metadata_dev);
1815 mutex_unlock(&dm_thin_pool_table.mutex);
1820 static int pool_map(struct dm_target *ti, struct bio *bio,
1821 union map_info *map_context)
1824 struct pool_c *pt = ti->private;
1825 struct pool *pool = pt->pool;
1826 unsigned long flags;
1829 * As this is a singleton target, ti->begin is always zero.
1831 spin_lock_irqsave(&pool->lock, flags);
1832 bio->bi_bdev = pt->data_dev->bdev;
1833 r = DM_MAPIO_REMAPPED;
1834 spin_unlock_irqrestore(&pool->lock, flags);
1840 * Retrieves the number of blocks of the data device from
1841 * the superblock and compares it to the actual device size,
1842 * thus resizing the data device in case it has grown.
1844 * This both copes with opening preallocated data devices in the ctr
1845 * being followed by a resume
1847 * calling the resume method individually after userspace has
1848 * grown the data device in reaction to a table event.
1850 static int pool_preresume(struct dm_target *ti)
1853 struct pool_c *pt = ti->private;
1854 struct pool *pool = pt->pool;
1855 dm_block_t data_size, sb_data_size;
1858 * Take control of the pool object.
1860 r = bind_control_target(pool, ti);
1864 data_size = ti->len >> pool->block_shift;
1865 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
1867 DMERR("failed to retrieve data device size");
1871 if (data_size < sb_data_size) {
1872 DMERR("pool target too small, is %llu blocks (expected %llu)",
1873 data_size, sb_data_size);
1876 } else if (data_size > sb_data_size) {
1877 r = dm_pool_resize_data_dev(pool->pmd, data_size);
1879 DMERR("failed to resize data device");
1883 r = dm_pool_commit_metadata(pool->pmd);
1885 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
1894 static void pool_resume(struct dm_target *ti)
1896 struct pool_c *pt = ti->private;
1897 struct pool *pool = pt->pool;
1898 unsigned long flags;
1900 spin_lock_irqsave(&pool->lock, flags);
1901 pool->low_water_triggered = 0;
1902 pool->no_free_space = 0;
1903 __requeue_bios(pool);
1904 spin_unlock_irqrestore(&pool->lock, flags);
1909 static void pool_postsuspend(struct dm_target *ti)
1912 struct pool_c *pt = ti->private;
1913 struct pool *pool = pt->pool;
1915 flush_workqueue(pool->wq);
1917 r = dm_pool_commit_metadata(pool->pmd);
1919 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
1921 /* FIXME: invalidate device? error the next FUA or FLUSH bio ?*/
1925 static int check_arg_count(unsigned argc, unsigned args_required)
1927 if (argc != args_required) {
1928 DMWARN("Message received with %u arguments instead of %u.",
1929 argc, args_required);
1936 static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
1938 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
1939 *dev_id <= MAX_DEV_ID)
1943 DMWARN("Message received with invalid device id: %s", arg);
1948 static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
1953 r = check_arg_count(argc, 2);
1957 r = read_dev_id(argv[1], &dev_id, 1);
1961 r = dm_pool_create_thin(pool->pmd, dev_id);
1963 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
1971 static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
1974 dm_thin_id origin_dev_id;
1977 r = check_arg_count(argc, 3);
1981 r = read_dev_id(argv[1], &dev_id, 1);
1985 r = read_dev_id(argv[2], &origin_dev_id, 1);
1989 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
1991 DMWARN("Creation of new snapshot %s of device %s failed.",
1999 static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2004 r = check_arg_count(argc, 2);
2008 r = read_dev_id(argv[1], &dev_id, 1);
2012 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2014 DMWARN("Deletion of thin device %s failed.", argv[1]);
2019 static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2021 dm_thin_id old_id, new_id;
2024 r = check_arg_count(argc, 3);
2028 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2029 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2033 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2034 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2038 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2040 DMWARN("Failed to change transaction id from %s to %s.",
2049 * Messages supported:
2050 * create_thin <dev_id>
2051 * create_snap <dev_id> <origin_id>
2053 * trim <dev_id> <new_size_in_sectors>
2054 * set_transaction_id <current_trans_id> <new_trans_id>
2056 static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2059 struct pool_c *pt = ti->private;
2060 struct pool *pool = pt->pool;
2062 if (!strcasecmp(argv[0], "create_thin"))
2063 r = process_create_thin_mesg(argc, argv, pool);
2065 else if (!strcasecmp(argv[0], "create_snap"))
2066 r = process_create_snap_mesg(argc, argv, pool);
2068 else if (!strcasecmp(argv[0], "delete"))
2069 r = process_delete_mesg(argc, argv, pool);
2071 else if (!strcasecmp(argv[0], "set_transaction_id"))
2072 r = process_set_transaction_id_mesg(argc, argv, pool);
2075 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2078 r = dm_pool_commit_metadata(pool->pmd);
2080 DMERR("%s message: dm_pool_commit_metadata() failed, error = %d",
2089 * <transaction id> <used metadata sectors>/<total metadata sectors>
2090 * <used data sectors>/<total data sectors> <held metadata root>
2092 static int pool_status(struct dm_target *ti, status_type_t type,
2093 char *result, unsigned maxlen)
2097 uint64_t transaction_id;
2098 dm_block_t nr_free_blocks_data;
2099 dm_block_t nr_free_blocks_metadata;
2100 dm_block_t nr_blocks_data;
2101 dm_block_t nr_blocks_metadata;
2102 dm_block_t held_root;
2103 char buf[BDEVNAME_SIZE];
2104 char buf2[BDEVNAME_SIZE];
2105 struct pool_c *pt = ti->private;
2106 struct pool *pool = pt->pool;
2109 case STATUSTYPE_INFO:
2110 r = dm_pool_get_metadata_transaction_id(pool->pmd,
2115 r = dm_pool_get_free_metadata_block_count(pool->pmd,
2116 &nr_free_blocks_metadata);
2120 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
2124 r = dm_pool_get_free_block_count(pool->pmd,
2125 &nr_free_blocks_data);
2129 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
2133 r = dm_pool_get_held_metadata_root(pool->pmd, &held_root);
2137 DMEMIT("%llu %llu/%llu %llu/%llu ",
2138 (unsigned long long)transaction_id,
2139 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2140 (unsigned long long)nr_blocks_metadata,
2141 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
2142 (unsigned long long)nr_blocks_data);
2145 DMEMIT("%llu", held_root);
2151 case STATUSTYPE_TABLE:
2152 DMEMIT("%s %s %lu %llu ",
2153 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
2154 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
2155 (unsigned long)pool->sectors_per_block,
2156 (unsigned long long)pt->low_water_blocks);
2158 DMEMIT("%u ", !pool->zero_new_blocks);
2160 if (!pool->zero_new_blocks)
2161 DMEMIT("skip_block_zeroing ");
2168 static int pool_iterate_devices(struct dm_target *ti,
2169 iterate_devices_callout_fn fn, void *data)
2171 struct pool_c *pt = ti->private;
2173 return fn(ti, pt->data_dev, 0, ti->len, data);
2176 static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2177 struct bio_vec *biovec, int max_size)
2179 struct pool_c *pt = ti->private;
2180 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2182 if (!q->merge_bvec_fn)
2185 bvm->bi_bdev = pt->data_dev->bdev;
2187 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2190 static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
2192 struct pool_c *pt = ti->private;
2193 struct pool *pool = pt->pool;
2195 blk_limits_io_min(limits, 0);
2196 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
2199 static struct target_type pool_target = {
2200 .name = "thin-pool",
2201 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
2202 DM_TARGET_IMMUTABLE,
2203 .version = {1, 0, 0},
2204 .module = THIS_MODULE,
2208 .postsuspend = pool_postsuspend,
2209 .preresume = pool_preresume,
2210 .resume = pool_resume,
2211 .message = pool_message,
2212 .status = pool_status,
2213 .merge = pool_merge,
2214 .iterate_devices = pool_iterate_devices,
2215 .io_hints = pool_io_hints,
2218 /*----------------------------------------------------------------
2219 * Thin target methods
2220 *--------------------------------------------------------------*/
2221 static void thin_dtr(struct dm_target *ti)
2223 struct thin_c *tc = ti->private;
2225 mutex_lock(&dm_thin_pool_table.mutex);
2227 __pool_dec(tc->pool);
2228 dm_pool_close_thin_device(tc->td);
2229 dm_put_device(ti, tc->pool_dev);
2232 mutex_unlock(&dm_thin_pool_table.mutex);
2236 * Thin target parameters:
2238 * <pool_dev> <dev_id>
2240 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2241 * dev_id: the internal device identifier
2243 static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
2247 struct dm_dev *pool_dev;
2248 struct mapped_device *pool_md;
2250 mutex_lock(&dm_thin_pool_table.mutex);
2253 ti->error = "Invalid argument count";
2258 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
2260 ti->error = "Out of memory";
2265 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
2267 ti->error = "Error opening pool device";
2270 tc->pool_dev = pool_dev;
2272 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
2273 ti->error = "Invalid device id";
2278 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
2280 ti->error = "Couldn't get pool mapped device";
2285 tc->pool = __pool_table_lookup(pool_md);
2287 ti->error = "Couldn't find pool object";
2289 goto bad_pool_lookup;
2291 __pool_inc(tc->pool);
2293 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
2295 ti->error = "Couldn't open thin internal device";
2299 ti->split_io = tc->pool->sectors_per_block;
2300 ti->num_flush_requests = 1;
2301 ti->num_discard_requests = 0;
2302 ti->discards_supported = 0;
2306 mutex_unlock(&dm_thin_pool_table.mutex);
2311 __pool_dec(tc->pool);
2315 dm_put_device(ti, tc->pool_dev);
2319 mutex_unlock(&dm_thin_pool_table.mutex);
2324 static int thin_map(struct dm_target *ti, struct bio *bio,
2325 union map_info *map_context)
2327 bio->bi_sector -= ti->begin;
2329 return thin_bio_map(ti, bio, map_context);
2332 static void thin_postsuspend(struct dm_target *ti)
2334 if (dm_noflush_suspending(ti))
2335 requeue_io((struct thin_c *)ti->private);
2339 * <nr mapped sectors> <highest mapped sector>
2341 static int thin_status(struct dm_target *ti, status_type_t type,
2342 char *result, unsigned maxlen)
2346 dm_block_t mapped, highest;
2347 char buf[BDEVNAME_SIZE];
2348 struct thin_c *tc = ti->private;
2354 case STATUSTYPE_INFO:
2355 r = dm_thin_get_mapped_count(tc->td, &mapped);
2359 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
2363 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
2365 DMEMIT("%llu", ((highest + 1) *
2366 tc->pool->sectors_per_block) - 1);
2371 case STATUSTYPE_TABLE:
2373 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
2374 (unsigned long) tc->dev_id);
2382 static int thin_iterate_devices(struct dm_target *ti,
2383 iterate_devices_callout_fn fn, void *data)
2386 struct thin_c *tc = ti->private;
2389 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2390 * we follow a more convoluted path through to the pool's target.
2393 return 0; /* nothing is bound */
2395 blocks = tc->pool->ti->len >> tc->pool->block_shift;
2397 return fn(ti, tc->pool_dev, 0, tc->pool->sectors_per_block * blocks, data);
2402 static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
2404 struct thin_c *tc = ti->private;
2406 blk_limits_io_min(limits, 0);
2407 blk_limits_io_opt(limits, tc->pool->sectors_per_block << SECTOR_SHIFT);
2410 static struct target_type thin_target = {
2412 .version = {1, 0, 0},
2413 .module = THIS_MODULE,
2417 .postsuspend = thin_postsuspend,
2418 .status = thin_status,
2419 .iterate_devices = thin_iterate_devices,
2420 .io_hints = thin_io_hints,
2423 /*----------------------------------------------------------------*/
2425 static int __init dm_thin_init(void)
2431 r = dm_register_target(&thin_target);
2435 r = dm_register_target(&pool_target);
2437 dm_unregister_target(&thin_target);
2442 static void dm_thin_exit(void)
2444 dm_unregister_target(&thin_target);
2445 dm_unregister_target(&pool_target);
2448 module_init(dm_thin_init);
2449 module_exit(dm_thin_exit);
2451 MODULE_DESCRIPTION(DM_NAME "device-mapper thin provisioning target");
2452 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2453 MODULE_LICENSE("GPL");