2 * Copyright (C) 2011-2012 Red Hat UK.
4 * This file is released under the GPL.
7 #include "dm-thin-metadata.h"
10 #include <linux/device-mapper.h>
11 #include <linux/dm-io.h>
12 #include <linux/dm-kcopyd.h>
13 #include <linux/list.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 #define DM_MSG_PREFIX "thin"
23 #define ENDIO_HOOK_POOL_SIZE 1024
24 #define DEFERRED_SET_SIZE 64
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 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 /*----------------------------------------------------------------*/
102 * Sometimes we can't deal with a bio straight away. We put them in prison
103 * where they can't cause any mischief. Bios are put in a cell identified
104 * by a key, multiple bios can be in the same cell. When the cell is
105 * subsequently unlocked the bios become available.
115 struct dm_bio_prison_cell
{
116 struct hlist_node list
;
117 struct bio_prison
*prison
;
120 struct bio_list bios
;
125 mempool_t
*cell_pool
;
129 struct hlist_head
*cells
;
132 static uint32_t calc_nr_buckets(unsigned nr_cells
)
137 nr_cells
= min(nr_cells
, 8192u);
145 static struct kmem_cache
*_cell_cache
;
148 * @nr_cells should be the number of cells you want in use _concurrently_.
149 * Don't confuse it with the number of distinct keys.
151 static struct bio_prison
*prison_create(unsigned nr_cells
)
154 uint32_t nr_buckets
= calc_nr_buckets(nr_cells
);
155 size_t len
= sizeof(struct bio_prison
) +
156 (sizeof(struct hlist_head
) * nr_buckets
);
157 struct bio_prison
*prison
= kmalloc(len
, GFP_KERNEL
);
162 spin_lock_init(&prison
->lock
);
163 prison
->cell_pool
= mempool_create_slab_pool(nr_cells
, _cell_cache
);
164 if (!prison
->cell_pool
) {
169 prison
->nr_buckets
= nr_buckets
;
170 prison
->hash_mask
= nr_buckets
- 1;
171 prison
->cells
= (struct hlist_head
*) (prison
+ 1);
172 for (i
= 0; i
< nr_buckets
; i
++)
173 INIT_HLIST_HEAD(prison
->cells
+ i
);
178 static void prison_destroy(struct bio_prison
*prison
)
180 mempool_destroy(prison
->cell_pool
);
184 static uint32_t hash_key(struct bio_prison
*prison
, struct cell_key
*key
)
186 const unsigned long BIG_PRIME
= 4294967291UL;
187 uint64_t hash
= key
->block
* BIG_PRIME
;
189 return (uint32_t) (hash
& prison
->hash_mask
);
192 static int keys_equal(struct cell_key
*lhs
, struct cell_key
*rhs
)
194 return (lhs
->virtual == rhs
->virtual) &&
195 (lhs
->dev
== rhs
->dev
) &&
196 (lhs
->block
== rhs
->block
);
199 static struct dm_bio_prison_cell
*__search_bucket(struct hlist_head
*bucket
,
200 struct cell_key
*key
)
202 struct dm_bio_prison_cell
*cell
;
203 struct hlist_node
*tmp
;
205 hlist_for_each_entry(cell
, tmp
, bucket
, list
)
206 if (keys_equal(&cell
->key
, key
))
213 * This may block if a new cell needs allocating. You must ensure that
214 * cells will be unlocked even if the calling thread is blocked.
216 * Returns 1 if the cell was already held, 0 if @inmate is the new holder.
218 static int bio_detain(struct bio_prison
*prison
, struct cell_key
*key
,
219 struct bio
*inmate
, struct dm_bio_prison_cell
**ref
)
223 uint32_t hash
= hash_key(prison
, key
);
224 struct dm_bio_prison_cell
*cell
, *cell2
;
226 BUG_ON(hash
> prison
->nr_buckets
);
228 spin_lock_irqsave(&prison
->lock
, flags
);
230 cell
= __search_bucket(prison
->cells
+ hash
, key
);
232 bio_list_add(&cell
->bios
, inmate
);
237 * Allocate a new cell
239 spin_unlock_irqrestore(&prison
->lock
, flags
);
240 cell2
= mempool_alloc(prison
->cell_pool
, GFP_NOIO
);
241 spin_lock_irqsave(&prison
->lock
, flags
);
244 * We've been unlocked, so we have to double check that
245 * nobody else has inserted this cell in the meantime.
247 cell
= __search_bucket(prison
->cells
+ hash
, key
);
249 mempool_free(cell2
, prison
->cell_pool
);
250 bio_list_add(&cell
->bios
, inmate
);
259 cell
->prison
= prison
;
260 memcpy(&cell
->key
, key
, sizeof(cell
->key
));
261 cell
->holder
= inmate
;
262 bio_list_init(&cell
->bios
);
263 hlist_add_head(&cell
->list
, prison
->cells
+ hash
);
268 spin_unlock_irqrestore(&prison
->lock
, flags
);
276 * @inmates must have been initialised prior to this call
278 static void __cell_release(struct dm_bio_prison_cell
*cell
, struct bio_list
*inmates
)
280 struct bio_prison
*prison
= cell
->prison
;
282 hlist_del(&cell
->list
);
285 bio_list_add(inmates
, cell
->holder
);
286 bio_list_merge(inmates
, &cell
->bios
);
289 mempool_free(cell
, prison
->cell_pool
);
292 static void cell_release(struct dm_bio_prison_cell
*cell
, struct bio_list
*bios
)
295 struct bio_prison
*prison
= cell
->prison
;
297 spin_lock_irqsave(&prison
->lock
, flags
);
298 __cell_release(cell
, bios
);
299 spin_unlock_irqrestore(&prison
->lock
, flags
);
303 * There are a couple of places where we put a bio into a cell briefly
304 * before taking it out again. In these situations we know that no other
305 * bio may be in the cell. This function releases the cell, and also does
308 static void __cell_release_singleton(struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
310 BUG_ON(cell
->holder
!= bio
);
311 BUG_ON(!bio_list_empty(&cell
->bios
));
313 __cell_release(cell
, NULL
);
316 static void cell_release_singleton(struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
319 struct bio_prison
*prison
= cell
->prison
;
321 spin_lock_irqsave(&prison
->lock
, flags
);
322 __cell_release_singleton(cell
, bio
);
323 spin_unlock_irqrestore(&prison
->lock
, flags
);
327 * Sometimes we don't want the holder, just the additional bios.
329 static void __cell_release_no_holder(struct dm_bio_prison_cell
*cell
,
330 struct bio_list
*inmates
)
332 struct bio_prison
*prison
= cell
->prison
;
334 hlist_del(&cell
->list
);
335 bio_list_merge(inmates
, &cell
->bios
);
337 mempool_free(cell
, prison
->cell_pool
);
340 static void cell_release_no_holder(struct dm_bio_prison_cell
*cell
,
341 struct bio_list
*inmates
)
344 struct bio_prison
*prison
= cell
->prison
;
346 spin_lock_irqsave(&prison
->lock
, flags
);
347 __cell_release_no_holder(cell
, inmates
);
348 spin_unlock_irqrestore(&prison
->lock
, flags
);
351 static void cell_error(struct dm_bio_prison_cell
*cell
)
353 struct bio_prison
*prison
= cell
->prison
;
354 struct bio_list bios
;
358 bio_list_init(&bios
);
360 spin_lock_irqsave(&prison
->lock
, flags
);
361 __cell_release(cell
, &bios
);
362 spin_unlock_irqrestore(&prison
->lock
, flags
);
364 while ((bio
= bio_list_pop(&bios
)))
368 /*----------------------------------------------------------------*/
371 * We use the deferred set to keep track of pending reads to shared blocks.
372 * We do this to ensure the new mapping caused by a write isn't performed
373 * until these prior reads have completed. Otherwise the insertion of the
374 * new mapping could free the old block that the read bios are mapped to.
378 struct deferred_entry
{
379 struct deferred_set
*ds
;
381 struct list_head work_items
;
384 struct deferred_set
{
386 unsigned current_entry
;
388 struct deferred_entry entries
[DEFERRED_SET_SIZE
];
391 static void ds_init(struct deferred_set
*ds
)
395 spin_lock_init(&ds
->lock
);
396 ds
->current_entry
= 0;
398 for (i
= 0; i
< DEFERRED_SET_SIZE
; i
++) {
399 ds
->entries
[i
].ds
= ds
;
400 ds
->entries
[i
].count
= 0;
401 INIT_LIST_HEAD(&ds
->entries
[i
].work_items
);
405 static struct deferred_entry
*ds_inc(struct deferred_set
*ds
)
408 struct deferred_entry
*entry
;
410 spin_lock_irqsave(&ds
->lock
, flags
);
411 entry
= ds
->entries
+ ds
->current_entry
;
413 spin_unlock_irqrestore(&ds
->lock
, flags
);
418 static unsigned ds_next(unsigned index
)
420 return (index
+ 1) % DEFERRED_SET_SIZE
;
423 static void __sweep(struct deferred_set
*ds
, struct list_head
*head
)
425 while ((ds
->sweeper
!= ds
->current_entry
) &&
426 !ds
->entries
[ds
->sweeper
].count
) {
427 list_splice_init(&ds
->entries
[ds
->sweeper
].work_items
, head
);
428 ds
->sweeper
= ds_next(ds
->sweeper
);
431 if ((ds
->sweeper
== ds
->current_entry
) && !ds
->entries
[ds
->sweeper
].count
)
432 list_splice_init(&ds
->entries
[ds
->sweeper
].work_items
, head
);
435 static void ds_dec(struct deferred_entry
*entry
, struct list_head
*head
)
439 spin_lock_irqsave(&entry
->ds
->lock
, flags
);
440 BUG_ON(!entry
->count
);
442 __sweep(entry
->ds
, head
);
443 spin_unlock_irqrestore(&entry
->ds
->lock
, flags
);
447 * Returns 1 if deferred or 0 if no pending items to delay job.
449 static int ds_add_work(struct deferred_set
*ds
, struct list_head
*work
)
455 spin_lock_irqsave(&ds
->lock
, flags
);
456 if ((ds
->sweeper
== ds
->current_entry
) &&
457 !ds
->entries
[ds
->current_entry
].count
)
460 list_add(work
, &ds
->entries
[ds
->current_entry
].work_items
);
461 next_entry
= ds_next(ds
->current_entry
);
462 if (!ds
->entries
[next_entry
].count
)
463 ds
->current_entry
= next_entry
;
465 spin_unlock_irqrestore(&ds
->lock
, flags
);
470 /*----------------------------------------------------------------*/
475 static void build_data_key(struct dm_thin_device
*td
,
476 dm_block_t b
, struct cell_key
*key
)
479 key
->dev
= dm_thin_dev_id(td
);
483 static void build_virtual_key(struct dm_thin_device
*td
, dm_block_t b
,
484 struct cell_key
*key
)
487 key
->dev
= dm_thin_dev_id(td
);
491 /*----------------------------------------------------------------*/
494 * A pool device ties together a metadata device and a data device. It
495 * also provides the interface for creating and destroying internal
498 struct dm_thin_new_mapping
;
501 * The pool runs in 3 modes. Ordered in degraded order for comparisons.
504 PM_WRITE
, /* metadata may be changed */
505 PM_READ_ONLY
, /* metadata may not be changed */
506 PM_FAIL
, /* all I/O fails */
509 struct pool_features
{
512 bool zero_new_blocks
:1;
513 bool discard_enabled
:1;
514 bool discard_passdown
:1;
518 typedef void (*process_bio_fn
)(struct thin_c
*tc
, struct bio
*bio
);
519 typedef void (*process_mapping_fn
)(struct dm_thin_new_mapping
*m
);
522 struct list_head list
;
523 struct dm_target
*ti
; /* Only set if a pool target is bound */
525 struct mapped_device
*pool_md
;
526 struct block_device
*md_dev
;
527 struct dm_pool_metadata
*pmd
;
529 dm_block_t low_water_blocks
;
530 uint32_t sectors_per_block
;
531 int sectors_per_block_shift
;
533 struct pool_features pf
;
534 unsigned low_water_triggered
:1; /* A dm event has been sent */
535 unsigned no_free_space
:1; /* A -ENOSPC warning has been issued */
537 struct bio_prison
*prison
;
538 struct dm_kcopyd_client
*copier
;
540 struct workqueue_struct
*wq
;
541 struct work_struct worker
;
542 struct delayed_work waker
;
544 unsigned long last_commit_jiffies
;
548 struct bio_list deferred_bios
;
549 struct bio_list deferred_flush_bios
;
550 struct list_head prepared_mappings
;
551 struct list_head prepared_discards
;
553 struct bio_list retry_on_resume_list
;
555 struct deferred_set shared_read_ds
;
556 struct deferred_set all_io_ds
;
558 struct dm_thin_new_mapping
*next_mapping
;
559 mempool_t
*mapping_pool
;
560 mempool_t
*endio_hook_pool
;
562 process_bio_fn process_bio
;
563 process_bio_fn process_discard
;
565 process_mapping_fn process_prepared_mapping
;
566 process_mapping_fn process_prepared_discard
;
569 static enum pool_mode
get_pool_mode(struct pool
*pool
);
570 static void set_pool_mode(struct pool
*pool
, enum pool_mode mode
);
573 * Target context for a pool.
576 struct dm_target
*ti
;
578 struct dm_dev
*data_dev
;
579 struct dm_dev
*metadata_dev
;
580 struct dm_target_callbacks callbacks
;
582 dm_block_t low_water_blocks
;
583 struct pool_features pf
;
587 * Target context for a thin.
590 struct dm_dev
*pool_dev
;
591 struct dm_dev
*origin_dev
;
595 struct dm_thin_device
*td
;
598 /*----------------------------------------------------------------*/
601 * A global list of pools that uses a struct mapped_device as a key.
603 static struct dm_thin_pool_table
{
605 struct list_head pools
;
606 } dm_thin_pool_table
;
608 static void pool_table_init(void)
610 mutex_init(&dm_thin_pool_table
.mutex
);
611 INIT_LIST_HEAD(&dm_thin_pool_table
.pools
);
614 static void __pool_table_insert(struct pool
*pool
)
616 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
617 list_add(&pool
->list
, &dm_thin_pool_table
.pools
);
620 static void __pool_table_remove(struct pool
*pool
)
622 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
623 list_del(&pool
->list
);
626 static struct pool
*__pool_table_lookup(struct mapped_device
*md
)
628 struct pool
*pool
= NULL
, *tmp
;
630 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
632 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
633 if (tmp
->pool_md
== md
) {
642 static struct pool
*__pool_table_lookup_metadata_dev(struct block_device
*md_dev
)
644 struct pool
*pool
= NULL
, *tmp
;
646 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
648 list_for_each_entry(tmp
, &dm_thin_pool_table
.pools
, list
) {
649 if (tmp
->md_dev
== md_dev
) {
658 /*----------------------------------------------------------------*/
660 struct dm_thin_endio_hook
{
662 struct deferred_entry
*shared_read_entry
;
663 struct deferred_entry
*all_io_entry
;
664 struct dm_thin_new_mapping
*overwrite_mapping
;
667 static void __requeue_bio_list(struct thin_c
*tc
, struct bio_list
*master
)
670 struct bio_list bios
;
672 bio_list_init(&bios
);
673 bio_list_merge(&bios
, master
);
674 bio_list_init(master
);
676 while ((bio
= bio_list_pop(&bios
))) {
677 struct dm_thin_endio_hook
*h
= dm_get_mapinfo(bio
)->ptr
;
680 bio_endio(bio
, DM_ENDIO_REQUEUE
);
682 bio_list_add(master
, bio
);
686 static void requeue_io(struct thin_c
*tc
)
688 struct pool
*pool
= tc
->pool
;
691 spin_lock_irqsave(&pool
->lock
, flags
);
692 __requeue_bio_list(tc
, &pool
->deferred_bios
);
693 __requeue_bio_list(tc
, &pool
->retry_on_resume_list
);
694 spin_unlock_irqrestore(&pool
->lock
, flags
);
698 * This section of code contains the logic for processing a thin device's IO.
699 * Much of the code depends on pool object resources (lists, workqueues, etc)
700 * but most is exclusively called from the thin target rather than the thin-pool
704 static dm_block_t
get_bio_block(struct thin_c
*tc
, struct bio
*bio
)
706 sector_t block_nr
= bio
->bi_sector
;
708 if (tc
->pool
->sectors_per_block_shift
< 0)
709 (void) sector_div(block_nr
, tc
->pool
->sectors_per_block
);
711 block_nr
>>= tc
->pool
->sectors_per_block_shift
;
716 static void remap(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
)
718 struct pool
*pool
= tc
->pool
;
719 sector_t bi_sector
= bio
->bi_sector
;
721 bio
->bi_bdev
= tc
->pool_dev
->bdev
;
722 if (tc
->pool
->sectors_per_block_shift
< 0)
723 bio
->bi_sector
= (block
* pool
->sectors_per_block
) +
724 sector_div(bi_sector
, pool
->sectors_per_block
);
726 bio
->bi_sector
= (block
<< pool
->sectors_per_block_shift
) |
727 (bi_sector
& (pool
->sectors_per_block
- 1));
730 static void remap_to_origin(struct thin_c
*tc
, struct bio
*bio
)
732 bio
->bi_bdev
= tc
->origin_dev
->bdev
;
735 static int bio_triggers_commit(struct thin_c
*tc
, struct bio
*bio
)
737 return (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
)) &&
738 dm_thin_changed_this_transaction(tc
->td
);
741 static void issue(struct thin_c
*tc
, struct bio
*bio
)
743 struct pool
*pool
= tc
->pool
;
746 if (!bio_triggers_commit(tc
, bio
)) {
747 generic_make_request(bio
);
752 * Complete bio with an error if earlier I/O caused changes to
753 * the metadata that can't be committed e.g, due to I/O errors
754 * on the metadata device.
756 if (dm_thin_aborted_changes(tc
->td
)) {
762 * Batch together any bios that trigger commits and then issue a
763 * single commit for them in process_deferred_bios().
765 spin_lock_irqsave(&pool
->lock
, flags
);
766 bio_list_add(&pool
->deferred_flush_bios
, bio
);
767 spin_unlock_irqrestore(&pool
->lock
, flags
);
770 static void remap_to_origin_and_issue(struct thin_c
*tc
, struct bio
*bio
)
772 remap_to_origin(tc
, bio
);
776 static void remap_and_issue(struct thin_c
*tc
, struct bio
*bio
,
779 remap(tc
, bio
, block
);
784 * wake_worker() is used when new work is queued and when pool_resume is
785 * ready to continue deferred IO processing.
787 static void wake_worker(struct pool
*pool
)
789 queue_work(pool
->wq
, &pool
->worker
);
792 /*----------------------------------------------------------------*/
795 * Bio endio functions.
797 struct dm_thin_new_mapping
{
798 struct list_head list
;
802 unsigned pass_discard
:1;
805 dm_block_t virt_block
;
806 dm_block_t data_block
;
807 struct dm_bio_prison_cell
*cell
, *cell2
;
811 * If the bio covers the whole area of a block then we can avoid
812 * zeroing or copying. Instead this bio is hooked. The bio will
813 * still be in the cell, so care has to be taken to avoid issuing
817 bio_end_io_t
*saved_bi_end_io
;
820 static void __maybe_add_mapping(struct dm_thin_new_mapping
*m
)
822 struct pool
*pool
= m
->tc
->pool
;
824 if (m
->quiesced
&& m
->prepared
) {
825 list_add(&m
->list
, &pool
->prepared_mappings
);
830 static void copy_complete(int read_err
, unsigned long write_err
, void *context
)
833 struct dm_thin_new_mapping
*m
= context
;
834 struct pool
*pool
= m
->tc
->pool
;
836 m
->err
= read_err
|| write_err
? -EIO
: 0;
838 spin_lock_irqsave(&pool
->lock
, flags
);
840 __maybe_add_mapping(m
);
841 spin_unlock_irqrestore(&pool
->lock
, flags
);
844 static void overwrite_endio(struct bio
*bio
, int err
)
847 struct dm_thin_endio_hook
*h
= dm_get_mapinfo(bio
)->ptr
;
848 struct dm_thin_new_mapping
*m
= h
->overwrite_mapping
;
849 struct pool
*pool
= m
->tc
->pool
;
853 spin_lock_irqsave(&pool
->lock
, flags
);
855 __maybe_add_mapping(m
);
856 spin_unlock_irqrestore(&pool
->lock
, flags
);
859 /*----------------------------------------------------------------*/
866 * Prepared mapping jobs.
870 * This sends the bios in the cell back to the deferred_bios list.
872 static void cell_defer(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
,
873 dm_block_t data_block
)
875 struct pool
*pool
= tc
->pool
;
878 spin_lock_irqsave(&pool
->lock
, flags
);
879 cell_release(cell
, &pool
->deferred_bios
);
880 spin_unlock_irqrestore(&tc
->pool
->lock
, flags
);
886 * Same as cell_defer above, except it omits one particular detainee,
887 * a write bio that covers the block and has already been processed.
889 static void cell_defer_except(struct thin_c
*tc
, struct dm_bio_prison_cell
*cell
)
891 struct bio_list bios
;
892 struct pool
*pool
= tc
->pool
;
895 bio_list_init(&bios
);
897 spin_lock_irqsave(&pool
->lock
, flags
);
898 cell_release_no_holder(cell
, &pool
->deferred_bios
);
899 spin_unlock_irqrestore(&pool
->lock
, flags
);
904 static void process_prepared_mapping_fail(struct dm_thin_new_mapping
*m
)
907 m
->bio
->bi_end_io
= m
->saved_bi_end_io
;
910 mempool_free(m
, m
->tc
->pool
->mapping_pool
);
912 static void process_prepared_mapping(struct dm_thin_new_mapping
*m
)
914 struct thin_c
*tc
= m
->tc
;
920 bio
->bi_end_io
= m
->saved_bi_end_io
;
928 * Commit the prepared block into the mapping btree.
929 * Any I/O for this block arriving after this point will get
930 * remapped to it directly.
932 r
= dm_thin_insert_block(tc
->td
, m
->virt_block
, m
->data_block
);
934 DMERR("dm_thin_insert_block() failed");
940 * Release any bios held while the block was being provisioned.
941 * If we are processing a write bio that completely covers the block,
942 * we already processed it so can ignore it now when processing
943 * the bios in the cell.
946 cell_defer_except(tc
, m
->cell
);
949 cell_defer(tc
, m
->cell
, m
->data_block
);
953 mempool_free(m
, tc
->pool
->mapping_pool
);
956 static void process_prepared_discard_fail(struct dm_thin_new_mapping
*m
)
958 struct thin_c
*tc
= m
->tc
;
960 bio_io_error(m
->bio
);
961 cell_defer_except(tc
, m
->cell
);
962 cell_defer_except(tc
, m
->cell2
);
963 mempool_free(m
, tc
->pool
->mapping_pool
);
966 static void process_prepared_discard_passdown(struct dm_thin_new_mapping
*m
)
968 struct thin_c
*tc
= m
->tc
;
971 remap_and_issue(tc
, m
->bio
, m
->data_block
);
973 bio_endio(m
->bio
, 0);
975 cell_defer_except(tc
, m
->cell
);
976 cell_defer_except(tc
, m
->cell2
);
977 mempool_free(m
, tc
->pool
->mapping_pool
);
980 static void process_prepared_discard(struct dm_thin_new_mapping
*m
)
983 struct thin_c
*tc
= m
->tc
;
985 r
= dm_thin_remove_block(tc
->td
, m
->virt_block
);
987 DMERR("dm_thin_remove_block() failed");
989 process_prepared_discard_passdown(m
);
992 static void process_prepared(struct pool
*pool
, struct list_head
*head
,
993 process_mapping_fn
*fn
)
996 struct list_head maps
;
997 struct dm_thin_new_mapping
*m
, *tmp
;
999 INIT_LIST_HEAD(&maps
);
1000 spin_lock_irqsave(&pool
->lock
, flags
);
1001 list_splice_init(head
, &maps
);
1002 spin_unlock_irqrestore(&pool
->lock
, flags
);
1004 list_for_each_entry_safe(m
, tmp
, &maps
, list
)
1009 * Deferred bio jobs.
1011 static int io_overlaps_block(struct pool
*pool
, struct bio
*bio
)
1013 return bio
->bi_size
== (pool
->sectors_per_block
<< SECTOR_SHIFT
);
1016 static int io_overwrites_block(struct pool
*pool
, struct bio
*bio
)
1018 return (bio_data_dir(bio
) == WRITE
) &&
1019 io_overlaps_block(pool
, bio
);
1022 static void save_and_set_endio(struct bio
*bio
, bio_end_io_t
**save
,
1025 *save
= bio
->bi_end_io
;
1026 bio
->bi_end_io
= fn
;
1029 static int ensure_next_mapping(struct pool
*pool
)
1031 if (pool
->next_mapping
)
1034 pool
->next_mapping
= mempool_alloc(pool
->mapping_pool
, GFP_ATOMIC
);
1036 return pool
->next_mapping
? 0 : -ENOMEM
;
1039 static struct dm_thin_new_mapping
*get_next_mapping(struct pool
*pool
)
1041 struct dm_thin_new_mapping
*r
= pool
->next_mapping
;
1043 BUG_ON(!pool
->next_mapping
);
1045 pool
->next_mapping
= NULL
;
1050 static void schedule_copy(struct thin_c
*tc
, dm_block_t virt_block
,
1051 struct dm_dev
*origin
, dm_block_t data_origin
,
1052 dm_block_t data_dest
,
1053 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
1056 struct pool
*pool
= tc
->pool
;
1057 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
1059 INIT_LIST_HEAD(&m
->list
);
1063 m
->virt_block
= virt_block
;
1064 m
->data_block
= data_dest
;
1069 if (!ds_add_work(&pool
->shared_read_ds
, &m
->list
))
1073 * IO to pool_dev remaps to the pool target's data_dev.
1075 * If the whole block of data is being overwritten, we can issue the
1076 * bio immediately. Otherwise we use kcopyd to clone the data first.
1078 if (io_overwrites_block(pool
, bio
)) {
1079 struct dm_thin_endio_hook
*h
= dm_get_mapinfo(bio
)->ptr
;
1081 h
->overwrite_mapping
= m
;
1083 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
1084 remap_and_issue(tc
, bio
, data_dest
);
1086 struct dm_io_region from
, to
;
1088 from
.bdev
= origin
->bdev
;
1089 from
.sector
= data_origin
* pool
->sectors_per_block
;
1090 from
.count
= pool
->sectors_per_block
;
1092 to
.bdev
= tc
->pool_dev
->bdev
;
1093 to
.sector
= data_dest
* pool
->sectors_per_block
;
1094 to
.count
= pool
->sectors_per_block
;
1096 r
= dm_kcopyd_copy(pool
->copier
, &from
, 1, &to
,
1097 0, copy_complete
, m
);
1099 mempool_free(m
, pool
->mapping_pool
);
1100 DMERR("dm_kcopyd_copy() failed");
1106 static void schedule_internal_copy(struct thin_c
*tc
, dm_block_t virt_block
,
1107 dm_block_t data_origin
, dm_block_t data_dest
,
1108 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
1110 schedule_copy(tc
, virt_block
, tc
->pool_dev
,
1111 data_origin
, data_dest
, cell
, bio
);
1114 static void schedule_external_copy(struct thin_c
*tc
, dm_block_t virt_block
,
1115 dm_block_t data_dest
,
1116 struct dm_bio_prison_cell
*cell
, struct bio
*bio
)
1118 schedule_copy(tc
, virt_block
, tc
->origin_dev
,
1119 virt_block
, data_dest
, cell
, bio
);
1122 static void schedule_zero(struct thin_c
*tc
, dm_block_t virt_block
,
1123 dm_block_t data_block
, struct dm_bio_prison_cell
*cell
,
1126 struct pool
*pool
= tc
->pool
;
1127 struct dm_thin_new_mapping
*m
= get_next_mapping(pool
);
1129 INIT_LIST_HEAD(&m
->list
);
1133 m
->virt_block
= virt_block
;
1134 m
->data_block
= data_block
;
1140 * If the whole block of data is being overwritten or we are not
1141 * zeroing pre-existing data, we can issue the bio immediately.
1142 * Otherwise we use kcopyd to zero the data first.
1144 if (!pool
->pf
.zero_new_blocks
)
1145 process_prepared_mapping(m
);
1147 else if (io_overwrites_block(pool
, bio
)) {
1148 struct dm_thin_endio_hook
*h
= dm_get_mapinfo(bio
)->ptr
;
1150 h
->overwrite_mapping
= m
;
1152 save_and_set_endio(bio
, &m
->saved_bi_end_io
, overwrite_endio
);
1153 remap_and_issue(tc
, bio
, data_block
);
1156 struct dm_io_region to
;
1158 to
.bdev
= tc
->pool_dev
->bdev
;
1159 to
.sector
= data_block
* pool
->sectors_per_block
;
1160 to
.count
= pool
->sectors_per_block
;
1162 r
= dm_kcopyd_zero(pool
->copier
, 1, &to
, 0, copy_complete
, m
);
1164 mempool_free(m
, pool
->mapping_pool
);
1165 DMERR("dm_kcopyd_zero() failed");
1171 static int commit(struct pool
*pool
)
1175 r
= dm_pool_commit_metadata(pool
->pmd
);
1177 DMERR("commit failed, error = %d", r
);
1183 * A non-zero return indicates read_only or fail_io mode.
1184 * Many callers don't care about the return value.
1186 static int commit_or_fallback(struct pool
*pool
)
1190 if (get_pool_mode(pool
) != PM_WRITE
)
1195 set_pool_mode(pool
, PM_READ_ONLY
);
1200 static int alloc_data_block(struct thin_c
*tc
, dm_block_t
*result
)
1203 dm_block_t free_blocks
;
1204 unsigned long flags
;
1205 struct pool
*pool
= tc
->pool
;
1207 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
1211 if (free_blocks
<= pool
->low_water_blocks
&& !pool
->low_water_triggered
) {
1212 DMWARN("%s: reached low water mark, sending event.",
1213 dm_device_name(pool
->pool_md
));
1214 spin_lock_irqsave(&pool
->lock
, flags
);
1215 pool
->low_water_triggered
= 1;
1216 spin_unlock_irqrestore(&pool
->lock
, flags
);
1217 dm_table_event(pool
->ti
->table
);
1221 if (pool
->no_free_space
)
1225 * Try to commit to see if that will free up some
1228 (void) commit_or_fallback(pool
);
1230 r
= dm_pool_get_free_block_count(pool
->pmd
, &free_blocks
);
1235 * If we still have no space we set a flag to avoid
1236 * doing all this checking and return -ENOSPC.
1239 DMWARN("%s: no free space available.",
1240 dm_device_name(pool
->pool_md
));
1241 spin_lock_irqsave(&pool
->lock
, flags
);
1242 pool
->no_free_space
= 1;
1243 spin_unlock_irqrestore(&pool
->lock
, flags
);
1249 r
= dm_pool_alloc_data_block(pool
->pmd
, result
);
1257 * If we have run out of space, queue bios until the device is
1258 * resumed, presumably after having been reloaded with more space.
1260 static void retry_on_resume(struct bio
*bio
)
1262 struct dm_thin_endio_hook
*h
= dm_get_mapinfo(bio
)->ptr
;
1263 struct thin_c
*tc
= h
->tc
;
1264 struct pool
*pool
= tc
->pool
;
1265 unsigned long flags
;
1267 spin_lock_irqsave(&pool
->lock
, flags
);
1268 bio_list_add(&pool
->retry_on_resume_list
, bio
);
1269 spin_unlock_irqrestore(&pool
->lock
, flags
);
1272 static void no_space(struct dm_bio_prison_cell
*cell
)
1275 struct bio_list bios
;
1277 bio_list_init(&bios
);
1278 cell_release(cell
, &bios
);
1280 while ((bio
= bio_list_pop(&bios
)))
1281 retry_on_resume(bio
);
1284 static void process_discard(struct thin_c
*tc
, struct bio
*bio
)
1287 unsigned long flags
;
1288 struct pool
*pool
= tc
->pool
;
1289 struct dm_bio_prison_cell
*cell
, *cell2
;
1290 struct cell_key key
, key2
;
1291 dm_block_t block
= get_bio_block(tc
, bio
);
1292 struct dm_thin_lookup_result lookup_result
;
1293 struct dm_thin_new_mapping
*m
;
1295 build_virtual_key(tc
->td
, block
, &key
);
1296 if (bio_detain(tc
->pool
->prison
, &key
, bio
, &cell
))
1299 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1303 * Check nobody is fiddling with this pool block. This can
1304 * happen if someone's in the process of breaking sharing
1307 build_data_key(tc
->td
, lookup_result
.block
, &key2
);
1308 if (bio_detain(tc
->pool
->prison
, &key2
, bio
, &cell2
)) {
1309 cell_release_singleton(cell
, bio
);
1313 if (io_overlaps_block(pool
, bio
)) {
1315 * IO may still be going to the destination block. We must
1316 * quiesce before we can do the removal.
1318 m
= get_next_mapping(pool
);
1320 m
->pass_discard
= (!lookup_result
.shared
) && pool
->pf
.discard_passdown
;
1321 m
->virt_block
= block
;
1322 m
->data_block
= lookup_result
.block
;
1328 if (!ds_add_work(&pool
->all_io_ds
, &m
->list
)) {
1329 spin_lock_irqsave(&pool
->lock
, flags
);
1330 list_add(&m
->list
, &pool
->prepared_discards
);
1331 spin_unlock_irqrestore(&pool
->lock
, flags
);
1336 * The DM core makes sure that the discard doesn't span
1337 * a block boundary. So we submit the discard of a
1338 * partial block appropriately.
1340 cell_release_singleton(cell
, bio
);
1341 cell_release_singleton(cell2
, bio
);
1342 if ((!lookup_result
.shared
) && pool
->pf
.discard_passdown
)
1343 remap_and_issue(tc
, bio
, lookup_result
.block
);
1351 * It isn't provisioned, just forget it.
1353 cell_release_singleton(cell
, bio
);
1358 DMERR("discard: find block unexpectedly returned %d", r
);
1359 cell_release_singleton(cell
, bio
);
1365 static void break_sharing(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1366 struct cell_key
*key
,
1367 struct dm_thin_lookup_result
*lookup_result
,
1368 struct dm_bio_prison_cell
*cell
)
1371 dm_block_t data_block
;
1373 r
= alloc_data_block(tc
, &data_block
);
1376 schedule_internal_copy(tc
, block
, lookup_result
->block
,
1377 data_block
, cell
, bio
);
1385 DMERR("%s: alloc_data_block() failed, error = %d", __func__
, r
);
1391 static void process_shared_bio(struct thin_c
*tc
, struct bio
*bio
,
1393 struct dm_thin_lookup_result
*lookup_result
)
1395 struct dm_bio_prison_cell
*cell
;
1396 struct pool
*pool
= tc
->pool
;
1397 struct cell_key key
;
1400 * If cell is already occupied, then sharing is already in the process
1401 * of being broken so we have nothing further to do here.
1403 build_data_key(tc
->td
, lookup_result
->block
, &key
);
1404 if (bio_detain(pool
->prison
, &key
, bio
, &cell
))
1407 if (bio_data_dir(bio
) == WRITE
&& bio
->bi_size
)
1408 break_sharing(tc
, bio
, block
, &key
, lookup_result
, cell
);
1410 struct dm_thin_endio_hook
*h
= dm_get_mapinfo(bio
)->ptr
;
1412 h
->shared_read_entry
= ds_inc(&pool
->shared_read_ds
);
1414 cell_release_singleton(cell
, bio
);
1415 remap_and_issue(tc
, bio
, lookup_result
->block
);
1419 static void provision_block(struct thin_c
*tc
, struct bio
*bio
, dm_block_t block
,
1420 struct dm_bio_prison_cell
*cell
)
1423 dm_block_t data_block
;
1426 * Remap empty bios (flushes) immediately, without provisioning.
1428 if (!bio
->bi_size
) {
1429 cell_release_singleton(cell
, bio
);
1430 remap_and_issue(tc
, bio
, 0);
1435 * Fill read bios with zeroes and complete them immediately.
1437 if (bio_data_dir(bio
) == READ
) {
1439 cell_release_singleton(cell
, bio
);
1444 r
= alloc_data_block(tc
, &data_block
);
1448 schedule_external_copy(tc
, block
, data_block
, cell
, bio
);
1450 schedule_zero(tc
, block
, data_block
, cell
, bio
);
1458 DMERR("%s: alloc_data_block() failed, error = %d", __func__
, r
);
1459 set_pool_mode(tc
->pool
, PM_READ_ONLY
);
1465 static void process_bio(struct thin_c
*tc
, struct bio
*bio
)
1468 dm_block_t block
= get_bio_block(tc
, bio
);
1469 struct dm_bio_prison_cell
*cell
;
1470 struct cell_key key
;
1471 struct dm_thin_lookup_result lookup_result
;
1474 * If cell is already occupied, then the block is already
1475 * being provisioned so we have nothing further to do here.
1477 build_virtual_key(tc
->td
, block
, &key
);
1478 if (bio_detain(tc
->pool
->prison
, &key
, bio
, &cell
))
1481 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1485 * We can release this cell now. This thread is the only
1486 * one that puts bios into a cell, and we know there were
1487 * no preceding bios.
1490 * TODO: this will probably have to change when discard goes
1493 cell_release_singleton(cell
, bio
);
1495 if (lookup_result
.shared
)
1496 process_shared_bio(tc
, bio
, block
, &lookup_result
);
1498 remap_and_issue(tc
, bio
, lookup_result
.block
);
1502 if (bio_data_dir(bio
) == READ
&& tc
->origin_dev
) {
1503 cell_release_singleton(cell
, bio
);
1504 remap_to_origin_and_issue(tc
, bio
);
1506 provision_block(tc
, bio
, block
, cell
);
1510 DMERR("dm_thin_find_block() failed, error = %d", r
);
1511 cell_release_singleton(cell
, bio
);
1517 static void process_bio_read_only(struct thin_c
*tc
, struct bio
*bio
)
1520 int rw
= bio_data_dir(bio
);
1521 dm_block_t block
= get_bio_block(tc
, bio
);
1522 struct dm_thin_lookup_result lookup_result
;
1524 r
= dm_thin_find_block(tc
->td
, block
, 1, &lookup_result
);
1527 if (lookup_result
.shared
&& (rw
== WRITE
) && bio
->bi_size
)
1530 remap_and_issue(tc
, bio
, lookup_result
.block
);
1539 if (tc
->origin_dev
) {
1540 remap_to_origin_and_issue(tc
, bio
);
1549 DMERR("dm_thin_find_block() failed, error = %d", r
);
1555 static void process_bio_fail(struct thin_c
*tc
, struct bio
*bio
)
1560 static int need_commit_due_to_time(struct pool
*pool
)
1562 return jiffies
< pool
->last_commit_jiffies
||
1563 jiffies
> pool
->last_commit_jiffies
+ COMMIT_PERIOD
;
1566 static void process_deferred_bios(struct pool
*pool
)
1568 unsigned long flags
;
1570 struct bio_list bios
;
1572 bio_list_init(&bios
);
1574 spin_lock_irqsave(&pool
->lock
, flags
);
1575 bio_list_merge(&bios
, &pool
->deferred_bios
);
1576 bio_list_init(&pool
->deferred_bios
);
1577 spin_unlock_irqrestore(&pool
->lock
, flags
);
1579 while ((bio
= bio_list_pop(&bios
))) {
1580 struct dm_thin_endio_hook
*h
= dm_get_mapinfo(bio
)->ptr
;
1581 struct thin_c
*tc
= h
->tc
;
1584 * If we've got no free new_mapping structs, and processing
1585 * this bio might require one, we pause until there are some
1586 * prepared mappings to process.
1588 if (ensure_next_mapping(pool
)) {
1589 spin_lock_irqsave(&pool
->lock
, flags
);
1590 bio_list_merge(&pool
->deferred_bios
, &bios
);
1591 spin_unlock_irqrestore(&pool
->lock
, flags
);
1596 if (bio
->bi_rw
& REQ_DISCARD
)
1597 pool
->process_discard(tc
, bio
);
1599 pool
->process_bio(tc
, bio
);
1603 * If there are any deferred flush bios, we must commit
1604 * the metadata before issuing them.
1606 bio_list_init(&bios
);
1607 spin_lock_irqsave(&pool
->lock
, flags
);
1608 bio_list_merge(&bios
, &pool
->deferred_flush_bios
);
1609 bio_list_init(&pool
->deferred_flush_bios
);
1610 spin_unlock_irqrestore(&pool
->lock
, flags
);
1612 if (bio_list_empty(&bios
) && !need_commit_due_to_time(pool
))
1615 if (commit_or_fallback(pool
)) {
1616 while ((bio
= bio_list_pop(&bios
)))
1620 pool
->last_commit_jiffies
= jiffies
;
1622 while ((bio
= bio_list_pop(&bios
)))
1623 generic_make_request(bio
);
1626 static void do_worker(struct work_struct
*ws
)
1628 struct pool
*pool
= container_of(ws
, struct pool
, worker
);
1630 process_prepared(pool
, &pool
->prepared_mappings
, &pool
->process_prepared_mapping
);
1631 process_prepared(pool
, &pool
->prepared_discards
, &pool
->process_prepared_discard
);
1632 process_deferred_bios(pool
);
1636 * We want to commit periodically so that not too much
1637 * unwritten data builds up.
1639 static void do_waker(struct work_struct
*ws
)
1641 struct pool
*pool
= container_of(to_delayed_work(ws
), struct pool
, waker
);
1643 queue_delayed_work(pool
->wq
, &pool
->waker
, COMMIT_PERIOD
);
1646 /*----------------------------------------------------------------*/
1648 static enum pool_mode
get_pool_mode(struct pool
*pool
)
1650 return pool
->pf
.mode
;
1653 static void set_pool_mode(struct pool
*pool
, enum pool_mode mode
)
1657 pool
->pf
.mode
= mode
;
1661 DMERR("switching pool to failure mode");
1662 pool
->process_bio
= process_bio_fail
;
1663 pool
->process_discard
= process_bio_fail
;
1664 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1665 pool
->process_prepared_discard
= process_prepared_discard_fail
;
1669 DMERR("switching pool to read-only mode");
1670 r
= dm_pool_abort_metadata(pool
->pmd
);
1672 DMERR("aborting transaction failed");
1673 set_pool_mode(pool
, PM_FAIL
);
1675 dm_pool_metadata_read_only(pool
->pmd
);
1676 pool
->process_bio
= process_bio_read_only
;
1677 pool
->process_discard
= process_discard
;
1678 pool
->process_prepared_mapping
= process_prepared_mapping_fail
;
1679 pool
->process_prepared_discard
= process_prepared_discard_passdown
;
1684 pool
->process_bio
= process_bio
;
1685 pool
->process_discard
= process_discard
;
1686 pool
->process_prepared_mapping
= process_prepared_mapping
;
1687 pool
->process_prepared_discard
= process_prepared_discard
;
1692 /*----------------------------------------------------------------*/
1695 * Mapping functions.
1699 * Called only while mapping a thin bio to hand it over to the workqueue.
1701 static void thin_defer_bio(struct thin_c
*tc
, struct bio
*bio
)
1703 unsigned long flags
;
1704 struct pool
*pool
= tc
->pool
;
1706 spin_lock_irqsave(&pool
->lock
, flags
);
1707 bio_list_add(&pool
->deferred_bios
, bio
);
1708 spin_unlock_irqrestore(&pool
->lock
, flags
);
1713 static struct dm_thin_endio_hook
*thin_hook_bio(struct thin_c
*tc
, struct bio
*bio
)
1715 struct pool
*pool
= tc
->pool
;
1716 struct dm_thin_endio_hook
*h
= mempool_alloc(pool
->endio_hook_pool
, GFP_NOIO
);
1719 h
->shared_read_entry
= NULL
;
1720 h
->all_io_entry
= bio
->bi_rw
& REQ_DISCARD
? NULL
: ds_inc(&pool
->all_io_ds
);
1721 h
->overwrite_mapping
= NULL
;
1727 * Non-blocking function called from the thin target's map function.
1729 static int thin_bio_map(struct dm_target
*ti
, struct bio
*bio
,
1730 union map_info
*map_context
)
1733 struct thin_c
*tc
= ti
->private;
1734 dm_block_t block
= get_bio_block(tc
, bio
);
1735 struct dm_thin_device
*td
= tc
->td
;
1736 struct dm_thin_lookup_result result
;
1738 map_context
->ptr
= thin_hook_bio(tc
, bio
);
1740 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
1742 return DM_MAPIO_SUBMITTED
;
1745 if (bio
->bi_rw
& (REQ_DISCARD
| REQ_FLUSH
| REQ_FUA
)) {
1746 thin_defer_bio(tc
, bio
);
1747 return DM_MAPIO_SUBMITTED
;
1750 r
= dm_thin_find_block(td
, block
, 0, &result
);
1753 * Note that we defer readahead too.
1757 if (unlikely(result
.shared
)) {
1759 * We have a race condition here between the
1760 * result.shared value returned by the lookup and
1761 * snapshot creation, which may cause new
1764 * To avoid this always quiesce the origin before
1765 * taking the snap. You want to do this anyway to
1766 * ensure a consistent application view
1769 * More distant ancestors are irrelevant. The
1770 * shared flag will be set in their case.
1772 thin_defer_bio(tc
, bio
);
1773 r
= DM_MAPIO_SUBMITTED
;
1775 remap(tc
, bio
, result
.block
);
1776 r
= DM_MAPIO_REMAPPED
;
1781 if (get_pool_mode(tc
->pool
) == PM_READ_ONLY
) {
1783 * This block isn't provisioned, and we have no way
1784 * of doing so. Just error it.
1787 r
= DM_MAPIO_SUBMITTED
;
1794 * In future, the failed dm_thin_find_block above could
1795 * provide the hint to load the metadata into cache.
1797 thin_defer_bio(tc
, bio
);
1798 r
= DM_MAPIO_SUBMITTED
;
1803 * Must always call bio_io_error on failure.
1804 * dm_thin_find_block can fail with -EINVAL if the
1805 * pool is switched to fail-io mode.
1808 r
= DM_MAPIO_SUBMITTED
;
1815 static int pool_is_congested(struct dm_target_callbacks
*cb
, int bdi_bits
)
1818 unsigned long flags
;
1819 struct pool_c
*pt
= container_of(cb
, struct pool_c
, callbacks
);
1821 spin_lock_irqsave(&pt
->pool
->lock
, flags
);
1822 r
= !bio_list_empty(&pt
->pool
->retry_on_resume_list
);
1823 spin_unlock_irqrestore(&pt
->pool
->lock
, flags
);
1826 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
1827 r
= bdi_congested(&q
->backing_dev_info
, bdi_bits
);
1833 static void __requeue_bios(struct pool
*pool
)
1835 bio_list_merge(&pool
->deferred_bios
, &pool
->retry_on_resume_list
);
1836 bio_list_init(&pool
->retry_on_resume_list
);
1839 /*----------------------------------------------------------------
1840 * Binding of control targets to a pool object
1841 *--------------------------------------------------------------*/
1842 static bool data_dev_supports_discard(struct pool_c
*pt
)
1844 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
1846 return q
&& blk_queue_discard(q
);
1850 * If discard_passdown was enabled verify that the data device
1851 * supports discards. Disable discard_passdown if not; otherwise
1852 * -EOPNOTSUPP will be returned.
1854 static void disable_passdown_if_not_supported(struct pool_c
*pt
,
1855 struct pool_features
*pf
)
1857 char buf
[BDEVNAME_SIZE
];
1859 if (!pf
->discard_passdown
|| data_dev_supports_discard(pt
))
1862 DMWARN("Discard unsupported by data device (%s): Disabling discard passdown.",
1863 bdevname(pt
->data_dev
->bdev
, buf
));
1865 pf
->discard_passdown
= false;
1868 static int bind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1870 struct pool_c
*pt
= ti
->private;
1873 * We want to make sure that degraded pools are never upgraded.
1875 enum pool_mode old_mode
= pool
->pf
.mode
;
1876 enum pool_mode new_mode
= pt
->pf
.mode
;
1878 if (old_mode
> new_mode
)
1879 new_mode
= old_mode
;
1882 pool
->low_water_blocks
= pt
->low_water_blocks
;
1885 disable_passdown_if_not_supported(pt
, &pool
->pf
);
1886 set_pool_mode(pool
, new_mode
);
1891 static void unbind_control_target(struct pool
*pool
, struct dm_target
*ti
)
1897 /*----------------------------------------------------------------
1899 *--------------------------------------------------------------*/
1900 /* Initialize pool features. */
1901 static void pool_features_init(struct pool_features
*pf
)
1903 pf
->mode
= PM_WRITE
;
1904 pf
->zero_new_blocks
= true;
1905 pf
->discard_enabled
= true;
1906 pf
->discard_passdown
= true;
1909 static void __pool_destroy(struct pool
*pool
)
1911 __pool_table_remove(pool
);
1913 if (dm_pool_metadata_close(pool
->pmd
) < 0)
1914 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
1916 prison_destroy(pool
->prison
);
1917 dm_kcopyd_client_destroy(pool
->copier
);
1920 destroy_workqueue(pool
->wq
);
1922 if (pool
->next_mapping
)
1923 mempool_free(pool
->next_mapping
, pool
->mapping_pool
);
1924 mempool_destroy(pool
->mapping_pool
);
1925 mempool_destroy(pool
->endio_hook_pool
);
1929 static struct kmem_cache
*_new_mapping_cache
;
1930 static struct kmem_cache
*_endio_hook_cache
;
1932 static struct pool
*pool_create(struct mapped_device
*pool_md
,
1933 struct block_device
*metadata_dev
,
1934 unsigned long block_size
,
1935 int read_only
, char **error
)
1940 struct dm_pool_metadata
*pmd
;
1941 bool format_device
= read_only
? false : true;
1943 pmd
= dm_pool_metadata_open(metadata_dev
, block_size
, format_device
);
1945 *error
= "Error creating metadata object";
1946 return (struct pool
*)pmd
;
1949 pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
1951 *error
= "Error allocating memory for pool";
1952 err_p
= ERR_PTR(-ENOMEM
);
1957 pool
->sectors_per_block
= block_size
;
1958 if (block_size
& (block_size
- 1))
1959 pool
->sectors_per_block_shift
= -1;
1961 pool
->sectors_per_block_shift
= __ffs(block_size
);
1962 pool
->low_water_blocks
= 0;
1963 pool_features_init(&pool
->pf
);
1964 pool
->prison
= prison_create(PRISON_CELLS
);
1965 if (!pool
->prison
) {
1966 *error
= "Error creating pool's bio prison";
1967 err_p
= ERR_PTR(-ENOMEM
);
1971 pool
->copier
= dm_kcopyd_client_create();
1972 if (IS_ERR(pool
->copier
)) {
1973 r
= PTR_ERR(pool
->copier
);
1974 *error
= "Error creating pool's kcopyd client";
1976 goto bad_kcopyd_client
;
1980 * Create singlethreaded workqueue that will service all devices
1981 * that use this metadata.
1983 pool
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
1985 *error
= "Error creating pool's workqueue";
1986 err_p
= ERR_PTR(-ENOMEM
);
1990 INIT_WORK(&pool
->worker
, do_worker
);
1991 INIT_DELAYED_WORK(&pool
->waker
, do_waker
);
1992 spin_lock_init(&pool
->lock
);
1993 bio_list_init(&pool
->deferred_bios
);
1994 bio_list_init(&pool
->deferred_flush_bios
);
1995 INIT_LIST_HEAD(&pool
->prepared_mappings
);
1996 INIT_LIST_HEAD(&pool
->prepared_discards
);
1997 pool
->low_water_triggered
= 0;
1998 pool
->no_free_space
= 0;
1999 bio_list_init(&pool
->retry_on_resume_list
);
2000 ds_init(&pool
->shared_read_ds
);
2001 ds_init(&pool
->all_io_ds
);
2003 pool
->next_mapping
= NULL
;
2004 pool
->mapping_pool
= mempool_create_slab_pool(MAPPING_POOL_SIZE
,
2005 _new_mapping_cache
);
2006 if (!pool
->mapping_pool
) {
2007 *error
= "Error creating pool's mapping mempool";
2008 err_p
= ERR_PTR(-ENOMEM
);
2009 goto bad_mapping_pool
;
2012 pool
->endio_hook_pool
= mempool_create_slab_pool(ENDIO_HOOK_POOL_SIZE
,
2014 if (!pool
->endio_hook_pool
) {
2015 *error
= "Error creating pool's endio_hook mempool";
2016 err_p
= ERR_PTR(-ENOMEM
);
2017 goto bad_endio_hook_pool
;
2019 pool
->ref_count
= 1;
2020 pool
->last_commit_jiffies
= jiffies
;
2021 pool
->pool_md
= pool_md
;
2022 pool
->md_dev
= metadata_dev
;
2023 __pool_table_insert(pool
);
2027 bad_endio_hook_pool
:
2028 mempool_destroy(pool
->mapping_pool
);
2030 destroy_workqueue(pool
->wq
);
2032 dm_kcopyd_client_destroy(pool
->copier
);
2034 prison_destroy(pool
->prison
);
2038 if (dm_pool_metadata_close(pmd
))
2039 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
2044 static void __pool_inc(struct pool
*pool
)
2046 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2050 static void __pool_dec(struct pool
*pool
)
2052 BUG_ON(!mutex_is_locked(&dm_thin_pool_table
.mutex
));
2053 BUG_ON(!pool
->ref_count
);
2054 if (!--pool
->ref_count
)
2055 __pool_destroy(pool
);
2058 static struct pool
*__pool_find(struct mapped_device
*pool_md
,
2059 struct block_device
*metadata_dev
,
2060 unsigned long block_size
, int read_only
,
2061 char **error
, int *created
)
2063 struct pool
*pool
= __pool_table_lookup_metadata_dev(metadata_dev
);
2066 if (pool
->pool_md
!= pool_md
) {
2067 *error
= "metadata device already in use by a pool";
2068 return ERR_PTR(-EBUSY
);
2073 pool
= __pool_table_lookup(pool_md
);
2075 if (pool
->md_dev
!= metadata_dev
) {
2076 *error
= "different pool cannot replace a pool";
2077 return ERR_PTR(-EINVAL
);
2082 pool
= pool_create(pool_md
, metadata_dev
, block_size
, read_only
, error
);
2090 /*----------------------------------------------------------------
2091 * Pool target methods
2092 *--------------------------------------------------------------*/
2093 static void pool_dtr(struct dm_target
*ti
)
2095 struct pool_c
*pt
= ti
->private;
2097 mutex_lock(&dm_thin_pool_table
.mutex
);
2099 unbind_control_target(pt
->pool
, ti
);
2100 __pool_dec(pt
->pool
);
2101 dm_put_device(ti
, pt
->metadata_dev
);
2102 dm_put_device(ti
, pt
->data_dev
);
2105 mutex_unlock(&dm_thin_pool_table
.mutex
);
2108 static int parse_pool_features(struct dm_arg_set
*as
, struct pool_features
*pf
,
2109 struct dm_target
*ti
)
2113 const char *arg_name
;
2115 static struct dm_arg _args
[] = {
2116 {0, 3, "Invalid number of pool feature arguments"},
2120 * No feature arguments supplied.
2125 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
2129 while (argc
&& !r
) {
2130 arg_name
= dm_shift_arg(as
);
2133 if (!strcasecmp(arg_name
, "skip_block_zeroing"))
2134 pf
->zero_new_blocks
= false;
2136 else if (!strcasecmp(arg_name
, "ignore_discard"))
2137 pf
->discard_enabled
= false;
2139 else if (!strcasecmp(arg_name
, "no_discard_passdown"))
2140 pf
->discard_passdown
= false;
2142 else if (!strcasecmp(arg_name
, "read_only"))
2143 pf
->mode
= PM_READ_ONLY
;
2146 ti
->error
= "Unrecognised pool feature requested";
2156 * thin-pool <metadata dev> <data dev>
2157 * <data block size (sectors)>
2158 * <low water mark (blocks)>
2159 * [<#feature args> [<arg>]*]
2161 * Optional feature arguments are:
2162 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
2163 * ignore_discard: disable discard
2164 * no_discard_passdown: don't pass discards down to the data device
2166 static int pool_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2168 int r
, pool_created
= 0;
2171 struct pool_features pf
;
2172 struct dm_arg_set as
;
2173 struct dm_dev
*data_dev
;
2174 unsigned long block_size
;
2175 dm_block_t low_water_blocks
;
2176 struct dm_dev
*metadata_dev
;
2177 sector_t metadata_dev_size
;
2178 char b
[BDEVNAME_SIZE
];
2181 * FIXME Remove validation from scope of lock.
2183 mutex_lock(&dm_thin_pool_table
.mutex
);
2186 ti
->error
= "Invalid argument count";
2193 r
= dm_get_device(ti
, argv
[0], FMODE_READ
| FMODE_WRITE
, &metadata_dev
);
2195 ti
->error
= "Error opening metadata block device";
2199 metadata_dev_size
= i_size_read(metadata_dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
2200 if (metadata_dev_size
> THIN_METADATA_MAX_SECTORS_WARNING
)
2201 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2202 bdevname(metadata_dev
->bdev
, b
), THIN_METADATA_MAX_SECTORS
);
2204 r
= dm_get_device(ti
, argv
[1], FMODE_READ
| FMODE_WRITE
, &data_dev
);
2206 ti
->error
= "Error getting data device";
2210 if (kstrtoul(argv
[2], 10, &block_size
) || !block_size
||
2211 block_size
< DATA_DEV_BLOCK_SIZE_MIN_SECTORS
||
2212 block_size
> DATA_DEV_BLOCK_SIZE_MAX_SECTORS
||
2213 block_size
& (DATA_DEV_BLOCK_SIZE_MIN_SECTORS
- 1)) {
2214 ti
->error
= "Invalid block size";
2219 if (kstrtoull(argv
[3], 10, (unsigned long long *)&low_water_blocks
)) {
2220 ti
->error
= "Invalid low water mark";
2226 * Set default pool features.
2228 pool_features_init(&pf
);
2230 dm_consume_args(&as
, 4);
2231 r
= parse_pool_features(&as
, &pf
, ti
);
2235 pt
= kzalloc(sizeof(*pt
), GFP_KERNEL
);
2241 pool
= __pool_find(dm_table_get_md(ti
->table
), metadata_dev
->bdev
,
2242 block_size
, pf
.mode
== PM_READ_ONLY
, &ti
->error
, &pool_created
);
2249 * 'pool_created' reflects whether this is the first table load.
2250 * Top level discard support is not allowed to be changed after
2251 * initial load. This would require a pool reload to trigger thin
2254 if (!pool_created
&& pf
.discard_enabled
!= pool
->pf
.discard_enabled
) {
2255 ti
->error
= "Discard support cannot be disabled once enabled";
2257 goto out_flags_changed
;
2261 * The block layer requires discard_granularity to be a power of 2.
2263 if (pf
.discard_enabled
&& !is_power_of_2(block_size
)) {
2264 ti
->error
= "Discard support must be disabled when the block size is not a power of 2";
2266 goto out_flags_changed
;
2271 pt
->metadata_dev
= metadata_dev
;
2272 pt
->data_dev
= data_dev
;
2273 pt
->low_water_blocks
= low_water_blocks
;
2275 ti
->num_flush_requests
= 1;
2278 * Only need to enable discards if the pool should pass
2279 * them down to the data device. The thin device's discard
2280 * processing will cause mappings to be removed from the btree.
2282 if (pf
.discard_enabled
&& pf
.discard_passdown
) {
2283 ti
->num_discard_requests
= 1;
2286 * Setting 'discards_supported' circumvents the normal
2287 * stacking of discard limits (this keeps the pool and
2288 * thin devices' discard limits consistent).
2290 ti
->discards_supported
= true;
2291 ti
->discard_zeroes_data_unsupported
= true;
2295 pt
->callbacks
.congested_fn
= pool_is_congested
;
2296 dm_table_add_target_callbacks(ti
->table
, &pt
->callbacks
);
2298 mutex_unlock(&dm_thin_pool_table
.mutex
);
2307 dm_put_device(ti
, data_dev
);
2309 dm_put_device(ti
, metadata_dev
);
2311 mutex_unlock(&dm_thin_pool_table
.mutex
);
2316 static int pool_map(struct dm_target
*ti
, struct bio
*bio
,
2317 union map_info
*map_context
)
2320 struct pool_c
*pt
= ti
->private;
2321 struct pool
*pool
= pt
->pool
;
2322 unsigned long flags
;
2325 * As this is a singleton target, ti->begin is always zero.
2327 spin_lock_irqsave(&pool
->lock
, flags
);
2328 bio
->bi_bdev
= pt
->data_dev
->bdev
;
2329 r
= DM_MAPIO_REMAPPED
;
2330 spin_unlock_irqrestore(&pool
->lock
, flags
);
2336 * Retrieves the number of blocks of the data device from
2337 * the superblock and compares it to the actual device size,
2338 * thus resizing the data device in case it has grown.
2340 * This both copes with opening preallocated data devices in the ctr
2341 * being followed by a resume
2343 * calling the resume method individually after userspace has
2344 * grown the data device in reaction to a table event.
2346 static int pool_preresume(struct dm_target
*ti
)
2349 struct pool_c
*pt
= ti
->private;
2350 struct pool
*pool
= pt
->pool
;
2351 sector_t data_size
= ti
->len
;
2352 dm_block_t sb_data_size
;
2355 * Take control of the pool object.
2357 r
= bind_control_target(pool
, ti
);
2361 (void) sector_div(data_size
, pool
->sectors_per_block
);
2363 r
= dm_pool_get_data_dev_size(pool
->pmd
, &sb_data_size
);
2365 DMERR("failed to retrieve data device size");
2369 if (data_size
< sb_data_size
) {
2370 DMERR("pool target too small, is %llu blocks (expected %llu)",
2371 (unsigned long long)data_size
, sb_data_size
);
2374 } else if (data_size
> sb_data_size
) {
2375 r
= dm_pool_resize_data_dev(pool
->pmd
, data_size
);
2377 DMERR("failed to resize data device");
2378 /* FIXME Stricter than necessary: Rollback transaction instead here */
2379 set_pool_mode(pool
, PM_READ_ONLY
);
2383 (void) commit_or_fallback(pool
);
2389 static void pool_resume(struct dm_target
*ti
)
2391 struct pool_c
*pt
= ti
->private;
2392 struct pool
*pool
= pt
->pool
;
2393 unsigned long flags
;
2395 spin_lock_irqsave(&pool
->lock
, flags
);
2396 pool
->low_water_triggered
= 0;
2397 pool
->no_free_space
= 0;
2398 __requeue_bios(pool
);
2399 spin_unlock_irqrestore(&pool
->lock
, flags
);
2401 do_waker(&pool
->waker
.work
);
2404 static void pool_postsuspend(struct dm_target
*ti
)
2406 struct pool_c
*pt
= ti
->private;
2407 struct pool
*pool
= pt
->pool
;
2409 cancel_delayed_work(&pool
->waker
);
2410 flush_workqueue(pool
->wq
);
2411 (void) commit_or_fallback(pool
);
2414 static int check_arg_count(unsigned argc
, unsigned args_required
)
2416 if (argc
!= args_required
) {
2417 DMWARN("Message received with %u arguments instead of %u.",
2418 argc
, args_required
);
2425 static int read_dev_id(char *arg
, dm_thin_id
*dev_id
, int warning
)
2427 if (!kstrtoull(arg
, 10, (unsigned long long *)dev_id
) &&
2428 *dev_id
<= MAX_DEV_ID
)
2432 DMWARN("Message received with invalid device id: %s", arg
);
2437 static int process_create_thin_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2442 r
= check_arg_count(argc
, 2);
2446 r
= read_dev_id(argv
[1], &dev_id
, 1);
2450 r
= dm_pool_create_thin(pool
->pmd
, dev_id
);
2452 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2460 static int process_create_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2463 dm_thin_id origin_dev_id
;
2466 r
= check_arg_count(argc
, 3);
2470 r
= read_dev_id(argv
[1], &dev_id
, 1);
2474 r
= read_dev_id(argv
[2], &origin_dev_id
, 1);
2478 r
= dm_pool_create_snap(pool
->pmd
, dev_id
, origin_dev_id
);
2480 DMWARN("Creation of new snapshot %s of device %s failed.",
2488 static int process_delete_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2493 r
= check_arg_count(argc
, 2);
2497 r
= read_dev_id(argv
[1], &dev_id
, 1);
2501 r
= dm_pool_delete_thin_device(pool
->pmd
, dev_id
);
2503 DMWARN("Deletion of thin device %s failed.", argv
[1]);
2508 static int process_set_transaction_id_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2510 dm_thin_id old_id
, new_id
;
2513 r
= check_arg_count(argc
, 3);
2517 if (kstrtoull(argv
[1], 10, (unsigned long long *)&old_id
)) {
2518 DMWARN("set_transaction_id message: Unrecognised id %s.", argv
[1]);
2522 if (kstrtoull(argv
[2], 10, (unsigned long long *)&new_id
)) {
2523 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv
[2]);
2527 r
= dm_pool_set_metadata_transaction_id(pool
->pmd
, old_id
, new_id
);
2529 DMWARN("Failed to change transaction id from %s to %s.",
2537 static int process_reserve_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2541 r
= check_arg_count(argc
, 1);
2545 (void) commit_or_fallback(pool
);
2547 r
= dm_pool_reserve_metadata_snap(pool
->pmd
);
2549 DMWARN("reserve_metadata_snap message failed.");
2554 static int process_release_metadata_snap_mesg(unsigned argc
, char **argv
, struct pool
*pool
)
2558 r
= check_arg_count(argc
, 1);
2562 r
= dm_pool_release_metadata_snap(pool
->pmd
);
2564 DMWARN("release_metadata_snap message failed.");
2570 * Messages supported:
2571 * create_thin <dev_id>
2572 * create_snap <dev_id> <origin_id>
2574 * trim <dev_id> <new_size_in_sectors>
2575 * set_transaction_id <current_trans_id> <new_trans_id>
2576 * reserve_metadata_snap
2577 * release_metadata_snap
2579 static int pool_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
2582 struct pool_c
*pt
= ti
->private;
2583 struct pool
*pool
= pt
->pool
;
2585 if (!strcasecmp(argv
[0], "create_thin"))
2586 r
= process_create_thin_mesg(argc
, argv
, pool
);
2588 else if (!strcasecmp(argv
[0], "create_snap"))
2589 r
= process_create_snap_mesg(argc
, argv
, pool
);
2591 else if (!strcasecmp(argv
[0], "delete"))
2592 r
= process_delete_mesg(argc
, argv
, pool
);
2594 else if (!strcasecmp(argv
[0], "set_transaction_id"))
2595 r
= process_set_transaction_id_mesg(argc
, argv
, pool
);
2597 else if (!strcasecmp(argv
[0], "reserve_metadata_snap"))
2598 r
= process_reserve_metadata_snap_mesg(argc
, argv
, pool
);
2600 else if (!strcasecmp(argv
[0], "release_metadata_snap"))
2601 r
= process_release_metadata_snap_mesg(argc
, argv
, pool
);
2604 DMWARN("Unrecognised thin pool target message received: %s", argv
[0]);
2607 (void) commit_or_fallback(pool
);
2612 static void emit_flags(struct pool_features
*pf
, char *result
,
2613 unsigned sz
, unsigned maxlen
)
2615 unsigned count
= !pf
->zero_new_blocks
+ !pf
->discard_enabled
+
2616 !pf
->discard_passdown
+ (pf
->mode
== PM_READ_ONLY
);
2617 DMEMIT("%u ", count
);
2619 if (!pf
->zero_new_blocks
)
2620 DMEMIT("skip_block_zeroing ");
2622 if (!pf
->discard_enabled
)
2623 DMEMIT("ignore_discard ");
2625 if (!pf
->discard_passdown
)
2626 DMEMIT("no_discard_passdown ");
2628 if (pf
->mode
== PM_READ_ONLY
)
2629 DMEMIT("read_only ");
2634 * <transaction id> <used metadata sectors>/<total metadata sectors>
2635 * <used data sectors>/<total data sectors> <held metadata root>
2637 static int pool_status(struct dm_target
*ti
, status_type_t type
,
2638 unsigned status_flags
, char *result
, unsigned maxlen
)
2642 uint64_t transaction_id
;
2643 dm_block_t nr_free_blocks_data
;
2644 dm_block_t nr_free_blocks_metadata
;
2645 dm_block_t nr_blocks_data
;
2646 dm_block_t nr_blocks_metadata
;
2647 dm_block_t held_root
;
2648 char buf
[BDEVNAME_SIZE
];
2649 char buf2
[BDEVNAME_SIZE
];
2650 struct pool_c
*pt
= ti
->private;
2651 struct pool
*pool
= pt
->pool
;
2654 case STATUSTYPE_INFO
:
2655 if (get_pool_mode(pool
) == PM_FAIL
) {
2660 /* Commit to ensure statistics aren't out-of-date */
2661 if (!(status_flags
& DM_STATUS_NOFLUSH_FLAG
) && !dm_suspended(ti
))
2662 (void) commit_or_fallback(pool
);
2664 r
= dm_pool_get_metadata_transaction_id(pool
->pmd
,
2669 r
= dm_pool_get_free_metadata_block_count(pool
->pmd
,
2670 &nr_free_blocks_metadata
);
2674 r
= dm_pool_get_metadata_dev_size(pool
->pmd
, &nr_blocks_metadata
);
2678 r
= dm_pool_get_free_block_count(pool
->pmd
,
2679 &nr_free_blocks_data
);
2683 r
= dm_pool_get_data_dev_size(pool
->pmd
, &nr_blocks_data
);
2687 r
= dm_pool_get_metadata_snap(pool
->pmd
, &held_root
);
2691 DMEMIT("%llu %llu/%llu %llu/%llu ",
2692 (unsigned long long)transaction_id
,
2693 (unsigned long long)(nr_blocks_metadata
- nr_free_blocks_metadata
),
2694 (unsigned long long)nr_blocks_metadata
,
2695 (unsigned long long)(nr_blocks_data
- nr_free_blocks_data
),
2696 (unsigned long long)nr_blocks_data
);
2699 DMEMIT("%llu ", held_root
);
2703 if (pool
->pf
.mode
== PM_READ_ONLY
)
2708 if (pool
->pf
.discard_enabled
&& pool
->pf
.discard_passdown
)
2709 DMEMIT("discard_passdown");
2711 DMEMIT("no_discard_passdown");
2715 case STATUSTYPE_TABLE
:
2716 DMEMIT("%s %s %lu %llu ",
2717 format_dev_t(buf
, pt
->metadata_dev
->bdev
->bd_dev
),
2718 format_dev_t(buf2
, pt
->data_dev
->bdev
->bd_dev
),
2719 (unsigned long)pool
->sectors_per_block
,
2720 (unsigned long long)pt
->low_water_blocks
);
2721 emit_flags(&pt
->pf
, result
, sz
, maxlen
);
2728 static int pool_iterate_devices(struct dm_target
*ti
,
2729 iterate_devices_callout_fn fn
, void *data
)
2731 struct pool_c
*pt
= ti
->private;
2733 return fn(ti
, pt
->data_dev
, 0, ti
->len
, data
);
2736 static int pool_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
2737 struct bio_vec
*biovec
, int max_size
)
2739 struct pool_c
*pt
= ti
->private;
2740 struct request_queue
*q
= bdev_get_queue(pt
->data_dev
->bdev
);
2742 if (!q
->merge_bvec_fn
)
2745 bvm
->bi_bdev
= pt
->data_dev
->bdev
;
2747 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
2750 static void set_discard_limits(struct pool
*pool
, struct queue_limits
*limits
)
2753 * FIXME: these limits may be incompatible with the pool's data device
2755 limits
->max_discard_sectors
= pool
->sectors_per_block
;
2758 * This is just a hint, and not enforced. We have to cope with
2759 * bios that cover a block partially. A discard that spans a block
2760 * boundary is not sent to this target.
2762 limits
->discard_granularity
= pool
->sectors_per_block
<< SECTOR_SHIFT
;
2765 static void pool_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
2767 struct pool_c
*pt
= ti
->private;
2768 struct pool
*pool
= pt
->pool
;
2770 blk_limits_io_min(limits
, 0);
2771 blk_limits_io_opt(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
2772 if (pool
->pf
.discard_enabled
)
2773 set_discard_limits(pool
, limits
);
2776 static struct target_type pool_target
= {
2777 .name
= "thin-pool",
2778 .features
= DM_TARGET_SINGLETON
| DM_TARGET_ALWAYS_WRITEABLE
|
2779 DM_TARGET_IMMUTABLE
,
2780 .version
= {1, 3, 0},
2781 .module
= THIS_MODULE
,
2785 .postsuspend
= pool_postsuspend
,
2786 .preresume
= pool_preresume
,
2787 .resume
= pool_resume
,
2788 .message
= pool_message
,
2789 .status
= pool_status
,
2790 .merge
= pool_merge
,
2791 .iterate_devices
= pool_iterate_devices
,
2792 .io_hints
= pool_io_hints
,
2795 /*----------------------------------------------------------------
2796 * Thin target methods
2797 *--------------------------------------------------------------*/
2798 static void thin_dtr(struct dm_target
*ti
)
2800 struct thin_c
*tc
= ti
->private;
2802 mutex_lock(&dm_thin_pool_table
.mutex
);
2804 __pool_dec(tc
->pool
);
2805 dm_pool_close_thin_device(tc
->td
);
2806 dm_put_device(ti
, tc
->pool_dev
);
2808 dm_put_device(ti
, tc
->origin_dev
);
2811 mutex_unlock(&dm_thin_pool_table
.mutex
);
2815 * Thin target parameters:
2817 * <pool_dev> <dev_id> [origin_dev]
2819 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2820 * dev_id: the internal device identifier
2821 * origin_dev: a device external to the pool that should act as the origin
2823 * If the pool device has discards disabled, they get disabled for the thin
2826 static int thin_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2830 struct dm_dev
*pool_dev
, *origin_dev
;
2831 struct mapped_device
*pool_md
;
2833 mutex_lock(&dm_thin_pool_table
.mutex
);
2835 if (argc
!= 2 && argc
!= 3) {
2836 ti
->error
= "Invalid argument count";
2841 tc
= ti
->private = kzalloc(sizeof(*tc
), GFP_KERNEL
);
2843 ti
->error
= "Out of memory";
2849 r
= dm_get_device(ti
, argv
[2], FMODE_READ
, &origin_dev
);
2851 ti
->error
= "Error opening origin device";
2852 goto bad_origin_dev
;
2854 tc
->origin_dev
= origin_dev
;
2857 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &pool_dev
);
2859 ti
->error
= "Error opening pool device";
2862 tc
->pool_dev
= pool_dev
;
2864 if (read_dev_id(argv
[1], (unsigned long long *)&tc
->dev_id
, 0)) {
2865 ti
->error
= "Invalid device id";
2870 pool_md
= dm_get_md(tc
->pool_dev
->bdev
->bd_dev
);
2872 ti
->error
= "Couldn't get pool mapped device";
2877 tc
->pool
= __pool_table_lookup(pool_md
);
2879 ti
->error
= "Couldn't find pool object";
2881 goto bad_pool_lookup
;
2883 __pool_inc(tc
->pool
);
2885 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
2886 ti
->error
= "Couldn't open thin device, Pool is in fail mode";
2890 r
= dm_pool_open_thin_device(tc
->pool
->pmd
, tc
->dev_id
, &tc
->td
);
2892 ti
->error
= "Couldn't open thin internal device";
2896 r
= dm_set_target_max_io_len(ti
, tc
->pool
->sectors_per_block
);
2900 ti
->num_flush_requests
= 1;
2901 ti
->flush_supported
= true;
2903 /* In case the pool supports discards, pass them on. */
2904 if (tc
->pool
->pf
.discard_enabled
) {
2905 ti
->discards_supported
= true;
2906 ti
->num_discard_requests
= 1;
2907 ti
->discard_zeroes_data_unsupported
= true;
2908 /* Discard requests must be split on a block boundary */
2909 ti
->split_discard_requests
= true;
2914 mutex_unlock(&dm_thin_pool_table
.mutex
);
2919 __pool_dec(tc
->pool
);
2923 dm_put_device(ti
, tc
->pool_dev
);
2926 dm_put_device(ti
, tc
->origin_dev
);
2930 mutex_unlock(&dm_thin_pool_table
.mutex
);
2935 static int thin_map(struct dm_target
*ti
, struct bio
*bio
,
2936 union map_info
*map_context
)
2938 bio
->bi_sector
= dm_target_offset(ti
, bio
->bi_sector
);
2940 return thin_bio_map(ti
, bio
, map_context
);
2943 static int thin_endio(struct dm_target
*ti
,
2944 struct bio
*bio
, int err
,
2945 union map_info
*map_context
)
2947 unsigned long flags
;
2948 struct dm_thin_endio_hook
*h
= map_context
->ptr
;
2949 struct list_head work
;
2950 struct dm_thin_new_mapping
*m
, *tmp
;
2951 struct pool
*pool
= h
->tc
->pool
;
2953 if (h
->shared_read_entry
) {
2954 INIT_LIST_HEAD(&work
);
2955 ds_dec(h
->shared_read_entry
, &work
);
2957 spin_lock_irqsave(&pool
->lock
, flags
);
2958 list_for_each_entry_safe(m
, tmp
, &work
, list
) {
2961 __maybe_add_mapping(m
);
2963 spin_unlock_irqrestore(&pool
->lock
, flags
);
2966 if (h
->all_io_entry
) {
2967 INIT_LIST_HEAD(&work
);
2968 ds_dec(h
->all_io_entry
, &work
);
2969 spin_lock_irqsave(&pool
->lock
, flags
);
2970 list_for_each_entry_safe(m
, tmp
, &work
, list
)
2971 list_add(&m
->list
, &pool
->prepared_discards
);
2972 spin_unlock_irqrestore(&pool
->lock
, flags
);
2975 mempool_free(h
, pool
->endio_hook_pool
);
2980 static void thin_postsuspend(struct dm_target
*ti
)
2982 if (dm_noflush_suspending(ti
))
2983 requeue_io((struct thin_c
*)ti
->private);
2987 * <nr mapped sectors> <highest mapped sector>
2989 static int thin_status(struct dm_target
*ti
, status_type_t type
,
2990 unsigned status_flags
, char *result
, unsigned maxlen
)
2994 dm_block_t mapped
, highest
;
2995 char buf
[BDEVNAME_SIZE
];
2996 struct thin_c
*tc
= ti
->private;
2998 if (get_pool_mode(tc
->pool
) == PM_FAIL
) {
3007 case STATUSTYPE_INFO
:
3008 r
= dm_thin_get_mapped_count(tc
->td
, &mapped
);
3012 r
= dm_thin_get_highest_mapped_block(tc
->td
, &highest
);
3016 DMEMIT("%llu ", mapped
* tc
->pool
->sectors_per_block
);
3018 DMEMIT("%llu", ((highest
+ 1) *
3019 tc
->pool
->sectors_per_block
) - 1);
3024 case STATUSTYPE_TABLE
:
3026 format_dev_t(buf
, tc
->pool_dev
->bdev
->bd_dev
),
3027 (unsigned long) tc
->dev_id
);
3029 DMEMIT(" %s", format_dev_t(buf
, tc
->origin_dev
->bdev
->bd_dev
));
3037 static int thin_iterate_devices(struct dm_target
*ti
,
3038 iterate_devices_callout_fn fn
, void *data
)
3041 struct thin_c
*tc
= ti
->private;
3042 struct pool
*pool
= tc
->pool
;
3045 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3046 * we follow a more convoluted path through to the pool's target.
3049 return 0; /* nothing is bound */
3051 blocks
= pool
->ti
->len
;
3052 (void) sector_div(blocks
, pool
->sectors_per_block
);
3054 return fn(ti
, tc
->pool_dev
, 0, pool
->sectors_per_block
* blocks
, data
);
3059 static void thin_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
3061 struct thin_c
*tc
= ti
->private;
3062 struct pool
*pool
= tc
->pool
;
3064 blk_limits_io_min(limits
, 0);
3065 blk_limits_io_opt(limits
, pool
->sectors_per_block
<< SECTOR_SHIFT
);
3066 set_discard_limits(pool
, limits
);
3069 static struct target_type thin_target
= {
3071 .version
= {1, 3, 0},
3072 .module
= THIS_MODULE
,
3076 .end_io
= thin_endio
,
3077 .postsuspend
= thin_postsuspend
,
3078 .status
= thin_status
,
3079 .iterate_devices
= thin_iterate_devices
,
3080 .io_hints
= thin_io_hints
,
3083 /*----------------------------------------------------------------*/
3085 static int __init
dm_thin_init(void)
3091 r
= dm_register_target(&thin_target
);
3095 r
= dm_register_target(&pool_target
);
3097 goto bad_pool_target
;
3101 _cell_cache
= KMEM_CACHE(dm_bio_prison_cell
, 0);
3103 goto bad_cell_cache
;
3105 _new_mapping_cache
= KMEM_CACHE(dm_thin_new_mapping
, 0);
3106 if (!_new_mapping_cache
)
3107 goto bad_new_mapping_cache
;
3109 _endio_hook_cache
= KMEM_CACHE(dm_thin_endio_hook
, 0);
3110 if (!_endio_hook_cache
)
3111 goto bad_endio_hook_cache
;
3115 bad_endio_hook_cache
:
3116 kmem_cache_destroy(_new_mapping_cache
);
3117 bad_new_mapping_cache
:
3118 kmem_cache_destroy(_cell_cache
);
3120 dm_unregister_target(&pool_target
);
3122 dm_unregister_target(&thin_target
);
3127 static void dm_thin_exit(void)
3129 dm_unregister_target(&thin_target
);
3130 dm_unregister_target(&pool_target
);
3132 kmem_cache_destroy(_cell_cache
);
3133 kmem_cache_destroy(_new_mapping_cache
);
3134 kmem_cache_destroy(_endio_hook_cache
);
3137 module_init(dm_thin_init
);
3138 module_exit(dm_thin_exit
);
3140 MODULE_DESCRIPTION(DM_NAME
" thin provisioning target");
3141 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3142 MODULE_LICENSE("GPL");